]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mlx4/mlx4_core/mlx4_intf.c
Update the mlx4 core and mlx4en(4) modules towards Linux v4.9.
[FreeBSD/FreeBSD.git] / sys / dev / mlx4 / mlx4_core / mlx4_intf.c
1 /*
2  * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2007, 2008, 2014 Mellanox Technologies. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/slab.h>
35 #include <linux/module.h>
36
37 #include "mlx4.h"
38
39 struct mlx4_device_context {
40         struct list_head        list;
41         struct list_head        bond_list;
42         struct mlx4_interface  *intf;
43         void                   *context;
44 };
45
46 static LIST_HEAD(intf_list);
47 static LIST_HEAD(dev_list);
48 static DEFINE_MUTEX(intf_mutex);
49
50 static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
51 {
52         struct mlx4_device_context *dev_ctx;
53
54         dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
55         if (!dev_ctx)
56                 return;
57
58         dev_ctx->intf    = intf;
59         dev_ctx->context = intf->add(&priv->dev);
60
61         if (dev_ctx->context) {
62                 spin_lock_irq(&priv->ctx_lock);
63                 list_add_tail(&dev_ctx->list, &priv->ctx_list);
64                 spin_unlock_irq(&priv->ctx_lock);
65                 if (intf->activate)
66                         intf->activate(&priv->dev, dev_ctx->context);
67         } else
68                 kfree(dev_ctx);
69 }
70
71 static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
72 {
73         struct mlx4_device_context *dev_ctx;
74
75         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
76                 if (dev_ctx->intf == intf) {
77                         spin_lock_irq(&priv->ctx_lock);
78                         list_del(&dev_ctx->list);
79                         spin_unlock_irq(&priv->ctx_lock);
80
81                         intf->remove(&priv->dev, dev_ctx->context);
82                         kfree(dev_ctx);
83                         return;
84                 }
85 }
86
87 int mlx4_register_interface(struct mlx4_interface *intf)
88 {
89         struct mlx4_priv *priv;
90
91         if (!intf->add || !intf->remove)
92                 return -EINVAL;
93
94         mutex_lock(&intf_mutex);
95
96         list_add_tail(&intf->list, &intf_list);
97         list_for_each_entry(priv, &dev_list, dev_list) {
98                 if (mlx4_is_mfunc(&priv->dev) && (intf->flags & MLX4_INTFF_BONDING)) {
99                         mlx4_dbg(&priv->dev,
100                                  "SRIOV, disabling HA mode for intf proto %d\n", intf->protocol);
101                         intf->flags &= ~MLX4_INTFF_BONDING;
102                 }
103                 mlx4_add_device(intf, priv);
104         }
105
106         mutex_unlock(&intf_mutex);
107
108         return 0;
109 }
110 EXPORT_SYMBOL_GPL(mlx4_register_interface);
111
112 void mlx4_unregister_interface(struct mlx4_interface *intf)
113 {
114         struct mlx4_priv *priv;
115
116         mutex_lock(&intf_mutex);
117
118         list_for_each_entry(priv, &dev_list, dev_list)
119                 mlx4_remove_device(intf, priv);
120
121         list_del(&intf->list);
122
123         mutex_unlock(&intf_mutex);
124 }
125 EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
126
127 int mlx4_do_bond(struct mlx4_dev *dev, bool enable)
128 {
129         struct mlx4_priv *priv = mlx4_priv(dev);
130         struct mlx4_device_context *dev_ctx = NULL, *temp_dev_ctx;
131         unsigned long flags;
132         int ret;
133         LIST_HEAD(bond_list);
134
135         if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PORT_REMAP))
136                 return -ENOTSUPP;
137
138         ret = mlx4_disable_rx_port_check(dev, enable);
139         if (ret) {
140                 mlx4_err(dev, "Fail to %s rx port check\n",
141                          enable ? "enable" : "disable");
142                 return ret;
143         }
144         if (enable) {
145                 dev->flags |= MLX4_FLAG_BONDED;
146         } else {
147                 ret = mlx4_virt2phy_port_map(dev, 1, 2);
148                 if (ret) {
149                         mlx4_err(dev, "Fail to reset port map\n");
150                         return ret;
151                 }
152                 dev->flags &= ~MLX4_FLAG_BONDED;
153         }
154
155         spin_lock_irqsave(&priv->ctx_lock, flags);
156         list_for_each_entry_safe(dev_ctx, temp_dev_ctx, &priv->ctx_list, list) {
157                 if (dev_ctx->intf->flags & MLX4_INTFF_BONDING) {
158                         list_add_tail(&dev_ctx->bond_list, &bond_list);
159                         list_del(&dev_ctx->list);
160                 }
161         }
162         spin_unlock_irqrestore(&priv->ctx_lock, flags);
163
164         list_for_each_entry(dev_ctx, &bond_list, bond_list) {
165                 dev_ctx->intf->remove(dev, dev_ctx->context);
166                 dev_ctx->context =  dev_ctx->intf->add(dev);
167
168                 spin_lock_irqsave(&priv->ctx_lock, flags);
169                 list_add_tail(&dev_ctx->list, &priv->ctx_list);
170                 spin_unlock_irqrestore(&priv->ctx_lock, flags);
171
172                 mlx4_dbg(dev, "Inrerface for protocol %d restarted with when bonded mode is %s\n",
173                          dev_ctx->intf->protocol, enable ?
174                          "enabled" : "disabled");
175         }
176         return 0;
177 }
178
179 void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type,
180                          unsigned long param)
181 {
182         struct mlx4_priv *priv = mlx4_priv(dev);
183         struct mlx4_device_context *dev_ctx;
184         unsigned long flags;
185
186         spin_lock_irqsave(&priv->ctx_lock, flags);
187
188         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
189                 if (dev_ctx->intf->event)
190                         dev_ctx->intf->event(dev, dev_ctx->context, type, param);
191
192         spin_unlock_irqrestore(&priv->ctx_lock, flags);
193 }
194
195 int mlx4_register_device(struct mlx4_dev *dev)
196 {
197         struct mlx4_priv *priv = mlx4_priv(dev);
198         struct mlx4_interface *intf;
199
200         mutex_lock(&intf_mutex);
201
202         dev->persist->interface_state |= MLX4_INTERFACE_STATE_UP;
203         list_add_tail(&priv->dev_list, &dev_list);
204         list_for_each_entry(intf, &intf_list, list)
205                 mlx4_add_device(intf, priv);
206
207         mutex_unlock(&intf_mutex);
208         mlx4_start_catas_poll(dev);
209
210         return 0;
211 }
212
213 void mlx4_unregister_device(struct mlx4_dev *dev)
214 {
215         struct mlx4_priv *priv = mlx4_priv(dev);
216         struct mlx4_interface *intf;
217
218         if (!(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP))
219                 return;
220
221         mlx4_stop_catas_poll(dev);
222         mutex_lock(&intf_mutex);
223
224         list_for_each_entry(intf, &intf_list, list)
225                 mlx4_remove_device(intf, priv);
226
227         list_del(&priv->dev_list);
228         dev->persist->interface_state &= ~MLX4_INTERFACE_STATE_UP;
229
230         mutex_unlock(&intf_mutex);
231 }
232
233 void *mlx4_get_protocol_dev(struct mlx4_dev *dev, enum mlx4_protocol proto, int port)
234 {
235         struct mlx4_priv *priv = mlx4_priv(dev);
236         struct mlx4_device_context *dev_ctx;
237         unsigned long flags;
238         void *result = NULL;
239
240         spin_lock_irqsave(&priv->ctx_lock, flags);
241
242         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
243                 if (dev_ctx->intf->protocol == proto && dev_ctx->intf->get_dev) {
244                         result = dev_ctx->intf->get_dev(dev, dev_ctx->context, port);
245                         break;
246                 }
247
248         spin_unlock_irqrestore(&priv->ctx_lock, flags);
249
250         return result;
251 }
252 EXPORT_SYMBOL_GPL(mlx4_get_protocol_dev);
253