1 /*
2  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 
34 #include <linux/cpumask.h>
35 #include <linux/module.h>
36 #include <linux/delay.h>
37 #include <linux/netdevice.h>
38 #include <linux/slab.h>
39 
40 #include <linux/mlx4/driver.h>
41 #include <linux/mlx4/device.h>
42 #include <linux/mlx4/cmd.h>
43 
44 #include "mlx4_en.h"
45 
46 MODULE_AUTHOR("Liran Liss, Yevgeny Petrilin");
47 MODULE_DESCRIPTION("Mellanox ConnectX HCA Ethernet driver");
48 MODULE_LICENSE("Dual BSD/GPL");
49 MODULE_VERSION(DRV_VERSION);
50 
51 static const char mlx4_en_version[] =
52 	DRV_NAME ": Mellanox ConnectX HCA Ethernet driver v"
53 	DRV_VERSION "\n";
54 
55 #define MLX4_EN_PARM_INT(X, def_val, desc) \
56 	static unsigned int X = def_val;\
57 	module_param(X , uint, 0444); \
58 	MODULE_PARM_DESC(X, desc);
59 
60 
61 /*
62  * Device scope module parameters
63  */
64 
65 /* Enable RSS UDP traffic */
66 MLX4_EN_PARM_INT(udp_rss, 1,
67 		 "Enable RSS for incoming UDP traffic or disabled (0)");
68 
69 /* Priority pausing */
70 MLX4_EN_PARM_INT(pfctx, 0, "Priority based Flow Control policy on TX[7:0]."
71 			   " Per priority bit mask");
72 MLX4_EN_PARM_INT(pfcrx, 0, "Priority based Flow Control policy on RX[7:0]."
73 			   " Per priority bit mask");
74 
75 MLX4_EN_PARM_INT(inline_thold, MAX_INLINE,
76 		 "Threshold for using inline data (range: 17-104, default: 104)");
77 
78 #define MAX_PFC_TX     0xff
79 #define MAX_PFC_RX     0xff
80 
81 void en_print(const char *level, const struct mlx4_en_priv *priv,
82 	      const char *format, ...)
83 {
84 	va_list args;
85 	struct va_format vaf;
86 
87 	va_start(args, format);
88 
89 	vaf.fmt = format;
90 	vaf.va = &args;
91 	if (priv->registered)
92 		printk("%s%s: %s: %pV",
93 		       level, DRV_NAME, priv->dev->name, &vaf);
94 	else
95 		printk("%s%s: %s: Port %d: %pV",
96 		       level, DRV_NAME, dev_name(&priv->mdev->pdev->dev),
97 		       priv->port, &vaf);
98 	va_end(args);
99 }
100 
101 void mlx4_en_update_loopback_state(struct net_device *dev,
102 				   netdev_features_t features)
103 {
104 	struct mlx4_en_priv *priv = netdev_priv(dev);
105 
106 	if (features & NETIF_F_LOOPBACK)
107 		priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
108 	else
109 		priv->ctrl_flags &= cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
110 
111 	priv->flags &= ~(MLX4_EN_FLAG_RX_FILTER_NEEDED|
112 			MLX4_EN_FLAG_ENABLE_HW_LOOPBACK);
113 
114 	/* Drop the packet if SRIOV is not enabled
115 	 * and not performing the selftest or flb disabled
116 	 */
117 	if (mlx4_is_mfunc(priv->mdev->dev) &&
118 	    !(features & NETIF_F_LOOPBACK) && !priv->validate_loopback)
119 		priv->flags |= MLX4_EN_FLAG_RX_FILTER_NEEDED;
120 
121 	/* Set dmac in Tx WQE if we are in SRIOV mode or if loopback selftest
122 	 * is requested
123 	 */
124 	if (mlx4_is_mfunc(priv->mdev->dev) || priv->validate_loopback)
125 		priv->flags |= MLX4_EN_FLAG_ENABLE_HW_LOOPBACK;
126 
127 	mutex_lock(&priv->mdev->state_lock);
128 	if ((priv->mdev->dev->caps.flags2 &
129 	     MLX4_DEV_CAP_FLAG2_UPDATE_QP_SRC_CHECK_LB) &&
130 	    priv->rss_map.indir_qp && priv->rss_map.indir_qp->qpn) {
131 		int i;
132 		int err = 0;
133 		int loopback = !!(features & NETIF_F_LOOPBACK);
134 
135 		for (i = 0; i < priv->rx_ring_num; i++) {
136 			int ret;
137 
138 			ret = mlx4_en_change_mcast_lb(priv,
139 						      &priv->rss_map.qps[i],
140 						      loopback);
141 			if (!err)
142 				err = ret;
143 		}
144 		if (err)
145 			mlx4_warn(priv->mdev, "failed to change mcast loopback\n");
146 	}
147 	mutex_unlock(&priv->mdev->state_lock);
148 }
149 
150 static void mlx4_en_get_profile(struct mlx4_en_dev *mdev)
151 {
152 	struct mlx4_en_profile *params = &mdev->profile;
153 	int i;
154 
155 	params->udp_rss = udp_rss;
156 	params->max_num_tx_rings_p_up = mlx4_low_memory_profile() ?
157 		MLX4_EN_MIN_TX_RING_P_UP :
158 		min_t(int, num_online_cpus(), MLX4_EN_MAX_TX_RING_P_UP);
159 
160 	if (params->udp_rss && !(mdev->dev->caps.flags
161 					& MLX4_DEV_CAP_FLAG_UDP_RSS)) {
162 		mlx4_warn(mdev, "UDP RSS is not supported on this device\n");
163 		params->udp_rss = 0;
164 	}
165 	for (i = 1; i <= MLX4_MAX_PORTS; i++) {
166 		params->prof[i].rx_pause = 1;
167 		params->prof[i].rx_ppp = pfcrx;
168 		params->prof[i].tx_pause = 1;
169 		params->prof[i].tx_ppp = pfctx;
170 		params->prof[i].tx_ring_size = MLX4_EN_DEF_TX_RING_SIZE;
171 		params->prof[i].rx_ring_size = MLX4_EN_DEF_RX_RING_SIZE;
172 		params->prof[i].num_up = MLX4_EN_NUM_UP_LOW;
173 		params->prof[i].num_tx_rings_p_up = params->max_num_tx_rings_p_up;
174 		params->prof[i].tx_ring_num[TX] = params->max_num_tx_rings_p_up *
175 			params->prof[i].num_up;
176 		params->prof[i].rss_rings = 0;
177 		params->prof[i].inline_thold = inline_thold;
178 	}
179 }
180 
181 static void *mlx4_en_get_netdev(struct mlx4_dev *dev, void *ctx, u8 port)
182 {
183 	struct mlx4_en_dev *endev = ctx;
184 
185 	return endev->pndev[port];
186 }
187 
188 static void mlx4_en_event(struct mlx4_dev *dev, void *endev_ptr,
189 			  enum mlx4_dev_event event, unsigned long port)
190 {
191 	struct mlx4_en_dev *mdev = (struct mlx4_en_dev *) endev_ptr;
192 	struct mlx4_en_priv *priv;
193 
194 	switch (event) {
195 	case MLX4_DEV_EVENT_PORT_UP:
196 	case MLX4_DEV_EVENT_PORT_DOWN:
197 		if (!mdev->pndev[port])
198 			return;
199 		priv = netdev_priv(mdev->pndev[port]);
200 		/* To prevent races, we poll the link state in a separate
201 		  task rather than changing it here */
202 		priv->link_state = event;
203 		queue_work(mdev->workqueue, &priv->linkstate_task);
204 		break;
205 
206 	case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
207 		mlx4_err(mdev, "Internal error detected, restarting device\n");
208 		break;
209 
210 	case MLX4_DEV_EVENT_SLAVE_INIT:
211 	case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
212 		break;
213 	default:
214 		if (port < 1 || port > dev->caps.num_ports ||
215 		    !mdev->pndev[port])
216 			return;
217 		mlx4_warn(mdev, "Unhandled event %d for port %d\n", event,
218 			  (int) port);
219 	}
220 }
221 
222 static void mlx4_en_remove(struct mlx4_dev *dev, void *endev_ptr)
223 {
224 	struct mlx4_en_dev *mdev = endev_ptr;
225 	int i;
226 
227 	mutex_lock(&mdev->state_lock);
228 	mdev->device_up = false;
229 	mutex_unlock(&mdev->state_lock);
230 
231 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
232 		if (mdev->pndev[i])
233 			mlx4_en_destroy_netdev(mdev->pndev[i]);
234 
235 	flush_workqueue(mdev->workqueue);
236 	destroy_workqueue(mdev->workqueue);
237 	(void) mlx4_mr_free(dev, &mdev->mr);
238 	iounmap(mdev->uar_map);
239 	mlx4_uar_free(dev, &mdev->priv_uar);
240 	mlx4_pd_free(dev, mdev->priv_pdn);
241 	if (mdev->nb.notifier_call)
242 		unregister_netdevice_notifier(&mdev->nb);
243 	kfree(mdev);
244 }
245 
246 static void mlx4_en_activate(struct mlx4_dev *dev, void *ctx)
247 {
248 	int i;
249 	struct mlx4_en_dev *mdev = ctx;
250 
251 	/* Create a netdev for each port */
252 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
253 		mlx4_info(mdev, "Activating port:%d\n", i);
254 		if (mlx4_en_init_netdev(mdev, i, &mdev->profile.prof[i]))
255 			mdev->pndev[i] = NULL;
256 	}
257 
258 	/* register notifier */
259 	mdev->nb.notifier_call = mlx4_en_netdev_event;
260 	if (register_netdevice_notifier(&mdev->nb)) {
261 		mdev->nb.notifier_call = NULL;
262 		mlx4_err(mdev, "Failed to create notifier\n");
263 	}
264 }
265 
266 static void *mlx4_en_add(struct mlx4_dev *dev)
267 {
268 	struct mlx4_en_dev *mdev;
269 	int i;
270 
271 	printk_once(KERN_INFO "%s", mlx4_en_version);
272 
273 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
274 	if (!mdev)
275 		goto err_free_res;
276 
277 	if (mlx4_pd_alloc(dev, &mdev->priv_pdn))
278 		goto err_free_dev;
279 
280 	if (mlx4_uar_alloc(dev, &mdev->priv_uar))
281 		goto err_pd;
282 
283 	mdev->uar_map = ioremap((phys_addr_t) mdev->priv_uar.pfn << PAGE_SHIFT,
284 				PAGE_SIZE);
285 	if (!mdev->uar_map)
286 		goto err_uar;
287 	spin_lock_init(&mdev->uar_lock);
288 
289 	mdev->dev = dev;
290 	mdev->dma_device = &dev->persist->pdev->dev;
291 	mdev->pdev = dev->persist->pdev;
292 	mdev->device_up = false;
293 
294 	mdev->LSO_support = !!(dev->caps.flags & (1 << 15));
295 	if (!mdev->LSO_support)
296 		mlx4_warn(mdev, "LSO not supported, please upgrade to later FW version to enable LSO\n");
297 
298 	if (mlx4_mr_alloc(mdev->dev, mdev->priv_pdn, 0, ~0ull,
299 			 MLX4_PERM_LOCAL_WRITE |  MLX4_PERM_LOCAL_READ,
300 			 0, 0, &mdev->mr)) {
301 		mlx4_err(mdev, "Failed allocating memory region\n");
302 		goto err_map;
303 	}
304 	if (mlx4_mr_enable(mdev->dev, &mdev->mr)) {
305 		mlx4_err(mdev, "Failed enabling memory region\n");
306 		goto err_mr;
307 	}
308 
309 	/* Build device profile according to supplied module parameters */
310 	mlx4_en_get_profile(mdev);
311 
312 	/* Configure which ports to start according to module parameters */
313 	mdev->port_cnt = 0;
314 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
315 		mdev->port_cnt++;
316 
317 	/* Set default number of RX rings*/
318 	mlx4_en_set_num_rx_rings(mdev);
319 
320 	/* Create our own workqueue for reset/multicast tasks
321 	 * Note: we cannot use the shared workqueue because of deadlocks caused
322 	 *       by the rtnl lock */
323 	mdev->workqueue = create_singlethread_workqueue("mlx4_en");
324 	if (!mdev->workqueue)
325 		goto err_mr;
326 
327 	/* At this stage all non-port specific tasks are complete:
328 	 * mark the card state as up */
329 	mutex_init(&mdev->state_lock);
330 	mdev->device_up = true;
331 
332 	return mdev;
333 
334 err_mr:
335 	(void) mlx4_mr_free(dev, &mdev->mr);
336 err_map:
337 	if (mdev->uar_map)
338 		iounmap(mdev->uar_map);
339 err_uar:
340 	mlx4_uar_free(dev, &mdev->priv_uar);
341 err_pd:
342 	mlx4_pd_free(dev, mdev->priv_pdn);
343 err_free_dev:
344 	kfree(mdev);
345 err_free_res:
346 	return NULL;
347 }
348 
349 static struct mlx4_interface mlx4_en_interface = {
350 	.add		= mlx4_en_add,
351 	.remove		= mlx4_en_remove,
352 	.event		= mlx4_en_event,
353 	.get_dev	= mlx4_en_get_netdev,
354 	.protocol	= MLX4_PROT_ETH,
355 	.activate	= mlx4_en_activate,
356 };
357 
358 static void mlx4_en_verify_params(void)
359 {
360 	if (pfctx > MAX_PFC_TX) {
361 		pr_warn("mlx4_en: WARNING: illegal module parameter pfctx 0x%x - should be in range 0-0x%x, will be changed to default (0)\n",
362 			pfctx, MAX_PFC_TX);
363 		pfctx = 0;
364 	}
365 
366 	if (pfcrx > MAX_PFC_RX) {
367 		pr_warn("mlx4_en: WARNING: illegal module parameter pfcrx 0x%x - should be in range 0-0x%x, will be changed to default (0)\n",
368 			pfcrx, MAX_PFC_RX);
369 		pfcrx = 0;
370 	}
371 
372 	if (inline_thold < MIN_PKT_LEN || inline_thold > MAX_INLINE) {
373 		pr_warn("mlx4_en: WARNING: illegal module parameter inline_thold %d - should be in range %d-%d, will be changed to default (%d)\n",
374 			inline_thold, MIN_PKT_LEN, MAX_INLINE, MAX_INLINE);
375 		inline_thold = MAX_INLINE;
376 	}
377 }
378 
379 static int __init mlx4_en_init(void)
380 {
381 	mlx4_en_verify_params();
382 	mlx4_en_init_ptys2ethtool_map();
383 
384 	return mlx4_register_interface(&mlx4_en_interface);
385 }
386 
387 static void __exit mlx4_en_cleanup(void)
388 {
389 	mlx4_unregister_interface(&mlx4_en_interface);
390 }
391 
392 module_init(mlx4_en_init);
393 module_exit(mlx4_en_cleanup);
394 
395