• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/etherdevice.h>
35 #include <linux/tcp.h>
36 #include <linux/if_vlan.h>
37 #include <linux/delay.h>
38 
39 #include <linux/mlx4/driver.h>
40 #include <linux/mlx4/device.h>
41 #include <linux/mlx4/cmd.h>
42 #include <linux/mlx4/cq.h>
43 
44 #include "mlx4_en.h"
45 #include "en_port.h"
46 
47 
mlx4_en_vlan_rx_register(struct net_device * dev,struct vlan_group * grp)48 static void mlx4_en_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
49 {
50 	struct mlx4_en_priv *priv = netdev_priv(dev);
51 	struct mlx4_en_dev *mdev = priv->mdev;
52 	int err;
53 
54 	mlx4_dbg(HW, priv, "Registering VLAN group:%p\n", grp);
55 	priv->vlgrp = grp;
56 
57 	mutex_lock(&mdev->state_lock);
58 	if (mdev->device_up && priv->port_up) {
59 		err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, grp);
60 		if (err)
61 			mlx4_err(mdev, "Failed configuring VLAN filter\n");
62 	}
63 	mutex_unlock(&mdev->state_lock);
64 }
65 
mlx4_en_vlan_rx_add_vid(struct net_device * dev,unsigned short vid)66 static void mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
67 {
68 	struct mlx4_en_priv *priv = netdev_priv(dev);
69 	struct mlx4_en_dev *mdev = priv->mdev;
70 	int err;
71 
72 	if (!priv->vlgrp)
73 		return;
74 
75 	mlx4_dbg(HW, priv, "adding VLAN:%d (vlgrp entry:%p)\n",
76 		 vid, vlan_group_get_device(priv->vlgrp, vid));
77 
78 	/* Add VID to port VLAN filter */
79 	mutex_lock(&mdev->state_lock);
80 	if (mdev->device_up && priv->port_up) {
81 		err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
82 		if (err)
83 			mlx4_err(mdev, "Failed configuring VLAN filter\n");
84 	}
85 	mutex_unlock(&mdev->state_lock);
86 }
87 
mlx4_en_vlan_rx_kill_vid(struct net_device * dev,unsigned short vid)88 static void mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
89 {
90 	struct mlx4_en_priv *priv = netdev_priv(dev);
91 	struct mlx4_en_dev *mdev = priv->mdev;
92 	int err;
93 
94 	if (!priv->vlgrp)
95 		return;
96 
97 	mlx4_dbg(HW, priv, "Killing VID:%d (vlgrp:%p vlgrp "
98 		 "entry:%p)\n", vid, priv->vlgrp,
99 		 vlan_group_get_device(priv->vlgrp, vid));
100 	vlan_group_set_device(priv->vlgrp, vid, NULL);
101 
102 	/* Remove VID from port VLAN filter */
103 	mutex_lock(&mdev->state_lock);
104 	if (mdev->device_up && priv->port_up) {
105 		err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
106 		if (err)
107 			mlx4_err(mdev, "Failed configuring VLAN filter\n");
108 	}
109 	mutex_unlock(&mdev->state_lock);
110 }
111 
mlx4_en_mac_to_u64(u8 * addr)112 static u64 mlx4_en_mac_to_u64(u8 *addr)
113 {
114 	u64 mac = 0;
115 	int i;
116 
117 	for (i = 0; i < ETH_ALEN; i++) {
118 		mac <<= 8;
119 		mac |= addr[i];
120 	}
121 	return mac;
122 }
123 
mlx4_en_set_mac(struct net_device * dev,void * addr)124 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
125 {
126 	struct mlx4_en_priv *priv = netdev_priv(dev);
127 	struct mlx4_en_dev *mdev = priv->mdev;
128 	struct sockaddr *saddr = addr;
129 
130 	if (!is_valid_ether_addr(saddr->sa_data))
131 		return -EADDRNOTAVAIL;
132 
133 	memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
134 	priv->mac = mlx4_en_mac_to_u64(dev->dev_addr);
135 	queue_work(mdev->workqueue, &priv->mac_task);
136 	return 0;
137 }
138 
mlx4_en_do_set_mac(struct work_struct * work)139 static void mlx4_en_do_set_mac(struct work_struct *work)
140 {
141 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
142 						 mac_task);
143 	struct mlx4_en_dev *mdev = priv->mdev;
144 	int err = 0;
145 
146 	mutex_lock(&mdev->state_lock);
147 	if (priv->port_up) {
148 		/* Remove old MAC and insert the new one */
149 		mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
150 		err = mlx4_register_mac(mdev->dev, priv->port,
151 					priv->mac, &priv->mac_index);
152 		if (err)
153 			mlx4_err(mdev, "Failed changing HW MAC address\n");
154 	} else
155 		mlx4_dbg(HW, priv, "Port is down, exiting...\n");
156 
157 	mutex_unlock(&mdev->state_lock);
158 }
159 
mlx4_en_clear_list(struct net_device * dev)160 static void mlx4_en_clear_list(struct net_device *dev)
161 {
162 	struct mlx4_en_priv *priv = netdev_priv(dev);
163 	struct dev_mc_list *plist = priv->mc_list;
164 	struct dev_mc_list *next;
165 
166 	while (plist) {
167 		next = plist->next;
168 		kfree(plist);
169 		plist = next;
170 	}
171 	priv->mc_list = NULL;
172 }
173 
mlx4_en_cache_mclist(struct net_device * dev)174 static void mlx4_en_cache_mclist(struct net_device *dev)
175 {
176 	struct mlx4_en_priv *priv = netdev_priv(dev);
177 	struct mlx4_en_dev *mdev = priv->mdev;
178 	struct dev_mc_list *mclist;
179 	struct dev_mc_list *tmp;
180 	struct dev_mc_list *plist = NULL;
181 
182 	for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
183 		tmp = kmalloc(sizeof(struct dev_mc_list), GFP_ATOMIC);
184 		if (!tmp) {
185 			mlx4_err(mdev, "failed to allocate multicast list\n");
186 			mlx4_en_clear_list(dev);
187 			return;
188 		}
189 		memcpy(tmp, mclist, sizeof(struct dev_mc_list));
190 		tmp->next = NULL;
191 		if (plist)
192 			plist->next = tmp;
193 		else
194 			priv->mc_list = tmp;
195 		plist = tmp;
196 	}
197 }
198 
199 
mlx4_en_set_multicast(struct net_device * dev)200 static void mlx4_en_set_multicast(struct net_device *dev)
201 {
202 	struct mlx4_en_priv *priv = netdev_priv(dev);
203 
204 	if (!priv->port_up)
205 		return;
206 
207 	queue_work(priv->mdev->workqueue, &priv->mcast_task);
208 }
209 
mlx4_en_do_set_multicast(struct work_struct * work)210 static void mlx4_en_do_set_multicast(struct work_struct *work)
211 {
212 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
213 						 mcast_task);
214 	struct mlx4_en_dev *mdev = priv->mdev;
215 	struct net_device *dev = priv->dev;
216 	struct dev_mc_list *mclist;
217 	u64 mcast_addr = 0;
218 	int err;
219 
220 	mutex_lock(&mdev->state_lock);
221 	if (!mdev->device_up) {
222 		mlx4_dbg(HW, priv, "Card is not up, ignoring "
223 				   "multicast change.\n");
224 		goto out;
225 	}
226 	if (!priv->port_up) {
227 		mlx4_dbg(HW, priv, "Port is down, ignoring "
228 				   "multicast change.\n");
229 		goto out;
230 	}
231 
232 	/*
233 	 * Promsicuous mode: disable all filters
234 	 */
235 
236 	if (dev->flags & IFF_PROMISC) {
237 		if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
238 			if (netif_msg_rx_status(priv))
239 				mlx4_warn(mdev, "Port:%d entering promiscuous mode\n",
240 					  priv->port);
241 			priv->flags |= MLX4_EN_FLAG_PROMISC;
242 
243 			/* Enable promiscouos mode */
244 			err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
245 						     priv->base_qpn, 1);
246 			if (err)
247 				mlx4_err(mdev, "Failed enabling "
248 					 "promiscous mode\n");
249 
250 			/* Disable port multicast filter (unconditionally) */
251 			err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
252 						  0, MLX4_MCAST_DISABLE);
253 			if (err)
254 				mlx4_err(mdev, "Failed disabling "
255 					 "multicast filter\n");
256 
257 			/* Disable port VLAN filter */
258 			err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, NULL);
259 			if (err)
260 				mlx4_err(mdev, "Failed disabling "
261 					 "VLAN filter\n");
262 		}
263 		goto out;
264 	}
265 
266 	/*
267 	 * Not in promiscous mode
268 	 */
269 
270 	if (priv->flags & MLX4_EN_FLAG_PROMISC) {
271 		if (netif_msg_rx_status(priv))
272 			mlx4_warn(mdev, "Port:%d leaving promiscuous mode\n",
273 				  priv->port);
274 		priv->flags &= ~MLX4_EN_FLAG_PROMISC;
275 
276 		/* Disable promiscouos mode */
277 		err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
278 					     priv->base_qpn, 0);
279 		if (err)
280 			mlx4_err(mdev, "Failed disabling promiscous mode\n");
281 
282 		/* Enable port VLAN filter */
283 		err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
284 		if (err)
285 			mlx4_err(mdev, "Failed enabling VLAN filter\n");
286 	}
287 
288 	/* Enable/disable the multicast filter according to IFF_ALLMULTI */
289 	if (dev->flags & IFF_ALLMULTI) {
290 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
291 					  0, MLX4_MCAST_DISABLE);
292 		if (err)
293 			mlx4_err(mdev, "Failed disabling multicast filter\n");
294 	} else {
295 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
296 					  0, MLX4_MCAST_DISABLE);
297 		if (err)
298 			mlx4_err(mdev, "Failed disabling multicast filter\n");
299 
300 		/* Flush mcast filter and init it with broadcast address */
301 		mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
302 				    1, MLX4_MCAST_CONFIG);
303 
304 		/* Update multicast list - we cache all addresses so they won't
305 		 * change while HW is updated holding the command semaphor */
306 		netif_tx_lock_bh(dev);
307 		mlx4_en_cache_mclist(dev);
308 		netif_tx_unlock_bh(dev);
309 		for (mclist = priv->mc_list; mclist; mclist = mclist->next) {
310 			mcast_addr = mlx4_en_mac_to_u64(mclist->dmi_addr);
311 			mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
312 					    mcast_addr, 0, MLX4_MCAST_CONFIG);
313 		}
314 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
315 					  0, MLX4_MCAST_ENABLE);
316 		if (err)
317 			mlx4_err(mdev, "Failed enabling multicast filter\n");
318 
319 		mlx4_en_clear_list(dev);
320 	}
321 out:
322 	mutex_unlock(&mdev->state_lock);
323 }
324 
325 #ifdef CONFIG_NET_POLL_CONTROLLER
mlx4_en_netpoll(struct net_device * dev)326 static void mlx4_en_netpoll(struct net_device *dev)
327 {
328 	struct mlx4_en_priv *priv = netdev_priv(dev);
329 	struct mlx4_en_cq *cq;
330 	unsigned long flags;
331 	int i;
332 
333 	for (i = 0; i < priv->rx_ring_num; i++) {
334 		cq = &priv->rx_cq[i];
335 		spin_lock_irqsave(&cq->lock, flags);
336 		napi_synchronize(&cq->napi);
337 		mlx4_en_process_rx_cq(dev, cq, 0);
338 		spin_unlock_irqrestore(&cq->lock, flags);
339 	}
340 }
341 #endif
342 
mlx4_en_tx_timeout(struct net_device * dev)343 static void mlx4_en_tx_timeout(struct net_device *dev)
344 {
345 	struct mlx4_en_priv *priv = netdev_priv(dev);
346 	struct mlx4_en_dev *mdev = priv->mdev;
347 
348 	if (netif_msg_timer(priv))
349 		mlx4_warn(mdev, "Tx timeout called on port:%d\n", priv->port);
350 
351 	if (netif_carrier_ok(dev)) {
352 		priv->port_stats.tx_timeout++;
353 		mlx4_dbg(DRV, priv, "Scheduling watchdog\n");
354 		queue_work(mdev->workqueue, &priv->watchdog_task);
355 	}
356 }
357 
358 
mlx4_en_get_stats(struct net_device * dev)359 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
360 {
361 	struct mlx4_en_priv *priv = netdev_priv(dev);
362 
363 	spin_lock_bh(&priv->stats_lock);
364 	memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
365 	spin_unlock_bh(&priv->stats_lock);
366 
367 	return &priv->ret_stats;
368 }
369 
mlx4_en_set_default_moderation(struct mlx4_en_priv * priv)370 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
371 {
372 	struct mlx4_en_cq *cq;
373 	int i;
374 
375 	/* If we haven't received a specific coalescing setting
376 	 * (module param), we set the moderation paramters as follows:
377 	 * - moder_cnt is set to the number of mtu sized packets to
378 	 *   satisfy our coelsing target.
379 	 * - moder_time is set to a fixed value.
380 	 */
381 	priv->rx_frames = MLX4_EN_RX_COAL_TARGET / priv->dev->mtu + 1;
382 	priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
383 	mlx4_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
384 			     "rx_frames:%d rx_usecs:%d\n",
385 		 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
386 
387 	/* Setup cq moderation params */
388 	for (i = 0; i < priv->rx_ring_num; i++) {
389 		cq = &priv->rx_cq[i];
390 		cq->moder_cnt = priv->rx_frames;
391 		cq->moder_time = priv->rx_usecs;
392 	}
393 
394 	for (i = 0; i < priv->tx_ring_num; i++) {
395 		cq = &priv->tx_cq[i];
396 		cq->moder_cnt = MLX4_EN_TX_COAL_PKTS;
397 		cq->moder_time = MLX4_EN_TX_COAL_TIME;
398 	}
399 
400 	/* Reset auto-moderation params */
401 	priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
402 	priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
403 	priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
404 	priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
405 	priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
406 	priv->adaptive_rx_coal = 1;
407 	priv->last_moder_time = MLX4_EN_AUTO_CONF;
408 	priv->last_moder_jiffies = 0;
409 	priv->last_moder_packets = 0;
410 	priv->last_moder_tx_packets = 0;
411 	priv->last_moder_bytes = 0;
412 }
413 
mlx4_en_auto_moderation(struct mlx4_en_priv * priv)414 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
415 {
416 	unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
417 	struct mlx4_en_dev *mdev = priv->mdev;
418 	struct mlx4_en_cq *cq;
419 	unsigned long packets;
420 	unsigned long rate;
421 	unsigned long avg_pkt_size;
422 	unsigned long rx_packets;
423 	unsigned long rx_bytes;
424 	unsigned long tx_packets;
425 	unsigned long tx_pkt_diff;
426 	unsigned long rx_pkt_diff;
427 	int moder_time;
428 	int i, err;
429 
430 	if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
431 		return;
432 
433 	spin_lock_bh(&priv->stats_lock);
434 	rx_packets = priv->stats.rx_packets;
435 	rx_bytes = priv->stats.rx_bytes;
436 	tx_packets = priv->stats.tx_packets;
437 	spin_unlock_bh(&priv->stats_lock);
438 
439 	if (!priv->last_moder_jiffies || !period)
440 		goto out;
441 
442 	tx_pkt_diff = ((unsigned long) (tx_packets -
443 					priv->last_moder_tx_packets));
444 	rx_pkt_diff = ((unsigned long) (rx_packets -
445 					priv->last_moder_packets));
446 	packets = max(tx_pkt_diff, rx_pkt_diff);
447 	rate = packets * HZ / period;
448 	avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
449 				 priv->last_moder_bytes)) / packets : 0;
450 
451 	/* Apply auto-moderation only when packet rate exceeds a rate that
452 	 * it matters */
453 	if (rate > MLX4_EN_RX_RATE_THRESH) {
454 		/* If tx and rx packet rates are not balanced, assume that
455 		 * traffic is mainly BW bound and apply maximum moderation.
456 		 * Otherwise, moderate according to packet rate */
457 		if (2 * tx_pkt_diff > 3 * rx_pkt_diff ||
458 		    2 * rx_pkt_diff > 3 * tx_pkt_diff) {
459 			moder_time = priv->rx_usecs_high;
460 		} else {
461 			if (rate < priv->pkt_rate_low)
462 				moder_time = priv->rx_usecs_low;
463 			else if (rate > priv->pkt_rate_high)
464 				moder_time = priv->rx_usecs_high;
465 			else
466 				moder_time = (rate - priv->pkt_rate_low) *
467 					(priv->rx_usecs_high - priv->rx_usecs_low) /
468 					(priv->pkt_rate_high - priv->pkt_rate_low) +
469 					priv->rx_usecs_low;
470 		}
471 	} else {
472 		/* When packet rate is low, use default moderation rather than
473 		 * 0 to prevent interrupt storms if traffic suddenly increases */
474 		moder_time = priv->rx_usecs;
475 	}
476 
477 	mlx4_dbg(INTR, priv, "tx rate:%lu rx_rate:%lu\n",
478 		 tx_pkt_diff * HZ / period, rx_pkt_diff * HZ / period);
479 
480 	mlx4_dbg(INTR, priv, "Rx moder_time changed from:%d to %d period:%lu "
481 		 "[jiff] packets:%lu avg_pkt_size:%lu rate:%lu [p/s])\n",
482 		 priv->last_moder_time, moder_time, period, packets,
483 		 avg_pkt_size, rate);
484 
485 	if (moder_time != priv->last_moder_time) {
486 		priv->last_moder_time = moder_time;
487 		for (i = 0; i < priv->rx_ring_num; i++) {
488 			cq = &priv->rx_cq[i];
489 			cq->moder_time = moder_time;
490 			err = mlx4_en_set_cq_moder(priv, cq);
491 			if (err) {
492 				mlx4_err(mdev, "Failed modifying moderation for cq:%d "
493 					 "on port:%d\n", i, priv->port);
494 				break;
495 			}
496 		}
497 	}
498 
499 out:
500 	priv->last_moder_packets = rx_packets;
501 	priv->last_moder_tx_packets = tx_packets;
502 	priv->last_moder_bytes = rx_bytes;
503 	priv->last_moder_jiffies = jiffies;
504 }
505 
mlx4_en_do_get_stats(struct work_struct * work)506 static void mlx4_en_do_get_stats(struct work_struct *work)
507 {
508 	struct delayed_work *delay = container_of(work, struct delayed_work, work);
509 	struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
510 						 stats_task);
511 	struct mlx4_en_dev *mdev = priv->mdev;
512 	int err;
513 
514 	err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
515 	if (err)
516 		mlx4_dbg(HW, priv, "Could not update stats for "
517 				   "port:%d\n", priv->port);
518 
519 	mutex_lock(&mdev->state_lock);
520 	if (mdev->device_up) {
521 		if (priv->port_up)
522 			mlx4_en_auto_moderation(priv);
523 
524 		queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
525 	}
526 	mutex_unlock(&mdev->state_lock);
527 }
528 
mlx4_en_linkstate(struct work_struct * work)529 static void mlx4_en_linkstate(struct work_struct *work)
530 {
531 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
532 						 linkstate_task);
533 	struct mlx4_en_dev *mdev = priv->mdev;
534 	int linkstate = priv->link_state;
535 
536 	mutex_lock(&mdev->state_lock);
537 	/* If observable port state changed set carrier state and
538 	 * report to system log */
539 	if (priv->last_link_state != linkstate) {
540 		if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
541 			if (netif_msg_link(priv))
542 				mlx4_info(mdev, "Port %d - link down\n", priv->port);
543 			netif_carrier_off(priv->dev);
544 		} else {
545 			if (netif_msg_link(priv))
546 				mlx4_info(mdev, "Port %d - link up\n", priv->port);
547 			netif_carrier_on(priv->dev);
548 		}
549 	}
550 	priv->last_link_state = linkstate;
551 	mutex_unlock(&mdev->state_lock);
552 }
553 
554 
mlx4_en_start_port(struct net_device * dev)555 int mlx4_en_start_port(struct net_device *dev)
556 {
557 	struct mlx4_en_priv *priv = netdev_priv(dev);
558 	struct mlx4_en_dev *mdev = priv->mdev;
559 	struct mlx4_en_cq *cq;
560 	struct mlx4_en_tx_ring *tx_ring;
561 	struct mlx4_en_rx_ring *rx_ring;
562 	int rx_index = 0;
563 	int tx_index = 0;
564 	u16 stride;
565 	int err = 0;
566 	int i;
567 	int j;
568 
569 	if (priv->port_up) {
570 		mlx4_dbg(DRV, priv, "start port called while port already up\n");
571 		return 0;
572 	}
573 
574 	/* Calculate Rx buf size */
575 	dev->mtu = min(dev->mtu, priv->max_mtu);
576 	mlx4_en_calc_rx_buf(dev);
577 	mlx4_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
578 	stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
579 				    DS_SIZE * priv->num_frags);
580 	/* Configure rx cq's and rings */
581 	for (i = 0; i < priv->rx_ring_num; i++) {
582 		cq = &priv->rx_cq[i];
583 		rx_ring = &priv->rx_ring[i];
584 
585 		err = mlx4_en_activate_cq(priv, cq);
586 		if (err) {
587 			mlx4_err(mdev, "Failed activating Rx CQ\n");
588 			goto rx_err;
589 		}
590 		for (j = 0; j < cq->size; j++)
591 			cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
592 		err = mlx4_en_set_cq_moder(priv, cq);
593 		if (err) {
594 			mlx4_err(mdev, "Failed setting cq moderation parameters");
595 			mlx4_en_deactivate_cq(priv, cq);
596 			goto cq_err;
597 		}
598 		mlx4_en_arm_cq(priv, cq);
599 
600 		++rx_index;
601 	}
602 
603 	err = mlx4_en_activate_rx_rings(priv);
604 	if (err) {
605 		mlx4_err(mdev, "Failed to activate RX rings\n");
606 		goto cq_err;
607 	}
608 
609 	err = mlx4_en_config_rss_steer(priv);
610 	if (err) {
611 		mlx4_err(mdev, "Failed configuring rss steering\n");
612 		goto rx_err;
613 	}
614 
615 	/* Configure tx cq's and rings */
616 	for (i = 0; i < priv->tx_ring_num; i++) {
617 		/* Configure cq */
618 		cq = &priv->tx_cq[i];
619 		err = mlx4_en_activate_cq(priv, cq);
620 		if (err) {
621 			mlx4_err(mdev, "Failed allocating Tx CQ\n");
622 			goto tx_err;
623 		}
624 		err = mlx4_en_set_cq_moder(priv, cq);
625 		if (err) {
626 			mlx4_err(mdev, "Failed setting cq moderation parameters");
627 			mlx4_en_deactivate_cq(priv, cq);
628 			goto tx_err;
629 		}
630 		mlx4_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
631 		cq->buf->wqe_index = cpu_to_be16(0xffff);
632 
633 		/* Configure ring */
634 		tx_ring = &priv->tx_ring[i];
635 		err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
636 					       priv->rx_ring[0].srq.srqn);
637 		if (err) {
638 			mlx4_err(mdev, "Failed allocating Tx ring\n");
639 			mlx4_en_deactivate_cq(priv, cq);
640 			goto tx_err;
641 		}
642 		/* Set initial ownership of all Tx TXBBs to SW (1) */
643 		for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
644 			*((u32 *) (tx_ring->buf + j)) = 0xffffffff;
645 		++tx_index;
646 	}
647 
648 	/* Configure port */
649 	err = mlx4_SET_PORT_general(mdev->dev, priv->port,
650 				    priv->rx_skb_size + ETH_FCS_LEN,
651 				    priv->prof->tx_pause,
652 				    priv->prof->tx_ppp,
653 				    priv->prof->rx_pause,
654 				    priv->prof->rx_ppp);
655 	if (err) {
656 		mlx4_err(mdev, "Failed setting port general configurations"
657 			       " for port %d, with error %d\n", priv->port, err);
658 		goto tx_err;
659 	}
660 	/* Set default qp number */
661 	err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
662 	if (err) {
663 		mlx4_err(mdev, "Failed setting default qp numbers\n");
664 		goto tx_err;
665 	}
666 	/* Set port mac number */
667 	mlx4_dbg(DRV, priv, "Setting mac for port %d\n", priv->port);
668 	err = mlx4_register_mac(mdev->dev, priv->port,
669 				priv->mac, &priv->mac_index);
670 	if (err) {
671 		mlx4_err(mdev, "Failed setting port mac\n");
672 		goto tx_err;
673 	}
674 
675 	/* Init port */
676 	mlx4_dbg(HW, priv, "Initializing port\n");
677 	err = mlx4_INIT_PORT(mdev->dev, priv->port);
678 	if (err) {
679 		mlx4_err(mdev, "Failed Initializing port\n");
680 		goto mac_err;
681 	}
682 
683 	/* Schedule multicast task to populate multicast list */
684 	queue_work(mdev->workqueue, &priv->mcast_task);
685 
686 	priv->port_up = true;
687 	netif_start_queue(dev);
688 	return 0;
689 
690 mac_err:
691 	mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
692 tx_err:
693 	while (tx_index--) {
694 		mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
695 		mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
696 	}
697 
698 	mlx4_en_release_rss_steer(priv);
699 rx_err:
700 	for (i = 0; i < priv->rx_ring_num; i++)
701 		mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
702 cq_err:
703 	while (rx_index--)
704 		mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
705 
706 	return err; /* need to close devices */
707 }
708 
709 
mlx4_en_stop_port(struct net_device * dev)710 void mlx4_en_stop_port(struct net_device *dev)
711 {
712 	struct mlx4_en_priv *priv = netdev_priv(dev);
713 	struct mlx4_en_dev *mdev = priv->mdev;
714 	int i;
715 
716 	if (!priv->port_up) {
717 		mlx4_dbg(DRV, priv, "stop port (%d) called while port already down\n",
718 			 priv->port);
719 		return;
720 	}
721 	netif_stop_queue(dev);
722 
723 	/* Synchronize with tx routine */
724 	netif_tx_lock_bh(dev);
725 	priv->port_up = false;
726 	netif_tx_unlock_bh(dev);
727 
728 	/* close port*/
729 	mlx4_CLOSE_PORT(mdev->dev, priv->port);
730 
731 	/* Unregister Mac address for the port */
732 	mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
733 
734 	/* Free TX Rings */
735 	for (i = 0; i < priv->tx_ring_num; i++) {
736 		mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
737 		mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
738 	}
739 	msleep(10);
740 
741 	for (i = 0; i < priv->tx_ring_num; i++)
742 		mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
743 
744 	/* Free RSS qps */
745 	mlx4_en_release_rss_steer(priv);
746 
747 	/* Free RX Rings */
748 	for (i = 0; i < priv->rx_ring_num; i++) {
749 		mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
750 		while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
751 			msleep(1);
752 		mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
753 	}
754 }
755 
mlx4_en_restart(struct work_struct * work)756 static void mlx4_en_restart(struct work_struct *work)
757 {
758 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
759 						 watchdog_task);
760 	struct mlx4_en_dev *mdev = priv->mdev;
761 	struct net_device *dev = priv->dev;
762 
763 	mlx4_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
764 	mlx4_en_stop_port(dev);
765 	if (mlx4_en_start_port(dev))
766 	    mlx4_err(mdev, "Failed restarting port %d\n", priv->port);
767 }
768 
769 
mlx4_en_open(struct net_device * dev)770 static int mlx4_en_open(struct net_device *dev)
771 {
772 	struct mlx4_en_priv *priv = netdev_priv(dev);
773 	struct mlx4_en_dev *mdev = priv->mdev;
774 	int i;
775 	int err = 0;
776 
777 	mutex_lock(&mdev->state_lock);
778 
779 	if (!mdev->device_up) {
780 		mlx4_err(mdev, "Cannot open - device down/disabled\n");
781 		err = -EBUSY;
782 		goto out;
783 	}
784 
785 	/* Reset HW statistics and performance counters */
786 	if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
787 		mlx4_dbg(HW, priv, "Failed dumping statistics\n");
788 
789 	memset(&priv->stats, 0, sizeof(priv->stats));
790 	memset(&priv->pstats, 0, sizeof(priv->pstats));
791 
792 	for (i = 0; i < priv->tx_ring_num; i++) {
793 		priv->tx_ring[i].bytes = 0;
794 		priv->tx_ring[i].packets = 0;
795 	}
796 	for (i = 0; i < priv->rx_ring_num; i++) {
797 		priv->rx_ring[i].bytes = 0;
798 		priv->rx_ring[i].packets = 0;
799 	}
800 
801 	mlx4_en_set_default_moderation(priv);
802 	err = mlx4_en_start_port(dev);
803 	if (err)
804 		mlx4_err(mdev, "Failed starting port:%d\n", priv->port);
805 
806 out:
807 	mutex_unlock(&mdev->state_lock);
808 	return err;
809 }
810 
811 
mlx4_en_close(struct net_device * dev)812 static int mlx4_en_close(struct net_device *dev)
813 {
814 	struct mlx4_en_priv *priv = netdev_priv(dev);
815 	struct mlx4_en_dev *mdev = priv->mdev;
816 
817 	if (netif_msg_ifdown(priv))
818 		mlx4_info(mdev, "Close called for port:%d\n", priv->port);
819 
820 	mutex_lock(&mdev->state_lock);
821 
822 	mlx4_en_stop_port(dev);
823 	netif_carrier_off(dev);
824 
825 	mutex_unlock(&mdev->state_lock);
826 	return 0;
827 }
828 
mlx4_en_free_resources(struct mlx4_en_priv * priv)829 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
830 {
831 	int i;
832 
833 	for (i = 0; i < priv->tx_ring_num; i++) {
834 		if (priv->tx_ring[i].tx_info)
835 			mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
836 		if (priv->tx_cq[i].buf)
837 			mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
838 	}
839 
840 	for (i = 0; i < priv->rx_ring_num; i++) {
841 		if (priv->rx_ring[i].rx_info)
842 			mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i]);
843 		if (priv->rx_cq[i].buf)
844 			mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
845 	}
846 }
847 
mlx4_en_alloc_resources(struct mlx4_en_priv * priv)848 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
849 {
850 	struct mlx4_en_dev *mdev = priv->mdev;
851 	struct mlx4_en_port_profile *prof = priv->prof;
852 	int i;
853 
854 	/* Create tx Rings */
855 	for (i = 0; i < priv->tx_ring_num; i++) {
856 		if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
857 				      prof->tx_ring_size, i, TX))
858 			goto err;
859 
860 		if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
861 					   prof->tx_ring_size, TXBB_SIZE))
862 			goto err;
863 	}
864 
865 	/* Create rx Rings */
866 	for (i = 0; i < priv->rx_ring_num; i++) {
867 		if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
868 				      prof->rx_ring_size, i, RX))
869 			goto err;
870 
871 		if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
872 					   prof->rx_ring_size, priv->stride))
873 			goto err;
874 	}
875 
876 	return 0;
877 
878 err:
879 	mlx4_err(mdev, "Failed to allocate NIC resources\n");
880 	return -ENOMEM;
881 }
882 
883 
mlx4_en_destroy_netdev(struct net_device * dev)884 void mlx4_en_destroy_netdev(struct net_device *dev)
885 {
886 	struct mlx4_en_priv *priv = netdev_priv(dev);
887 	struct mlx4_en_dev *mdev = priv->mdev;
888 
889 	mlx4_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
890 
891 	/* Unregister device - this will close the port if it was up */
892 	if (priv->registered)
893 		unregister_netdev(dev);
894 
895 	if (priv->allocated)
896 		mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
897 
898 	cancel_delayed_work(&priv->stats_task);
899 	cancel_delayed_work(&priv->refill_task);
900 	/* flush any pending task for this netdev */
901 	flush_workqueue(mdev->workqueue);
902 
903 	/* Detach the netdev so tasks would not attempt to access it */
904 	mutex_lock(&mdev->state_lock);
905 	mdev->pndev[priv->port] = NULL;
906 	mutex_unlock(&mdev->state_lock);
907 
908 	mlx4_en_free_resources(priv);
909 	free_netdev(dev);
910 }
911 
mlx4_en_change_mtu(struct net_device * dev,int new_mtu)912 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
913 {
914 	struct mlx4_en_priv *priv = netdev_priv(dev);
915 	struct mlx4_en_dev *mdev = priv->mdev;
916 	int err = 0;
917 
918 	mlx4_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
919 		 dev->mtu, new_mtu);
920 
921 	if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
922 		mlx4_err(mdev, "Bad MTU size:%d.\n", new_mtu);
923 		return -EPERM;
924 	}
925 	dev->mtu = new_mtu;
926 
927 	if (netif_running(dev)) {
928 		mutex_lock(&mdev->state_lock);
929 		if (!mdev->device_up) {
930 			/* NIC is probably restarting - let watchdog task reset
931 			 * the port */
932 			mlx4_dbg(DRV, priv, "Change MTU called with card down!?\n");
933 		} else {
934 			mlx4_en_stop_port(dev);
935 			mlx4_en_set_default_moderation(priv);
936 			err = mlx4_en_start_port(dev);
937 			if (err) {
938 				mlx4_err(mdev, "Failed restarting port:%d\n",
939 					 priv->port);
940 				queue_work(mdev->workqueue, &priv->watchdog_task);
941 			}
942 		}
943 		mutex_unlock(&mdev->state_lock);
944 	}
945 	return 0;
946 }
947 
948 static const struct net_device_ops mlx4_netdev_ops = {
949 	.ndo_open		= mlx4_en_open,
950 	.ndo_stop		= mlx4_en_close,
951 	.ndo_start_xmit		= mlx4_en_xmit,
952 	.ndo_get_stats		= mlx4_en_get_stats,
953 	.ndo_set_multicast_list	= mlx4_en_set_multicast,
954 	.ndo_set_mac_address	= mlx4_en_set_mac,
955 	.ndo_validate_addr	= eth_validate_addr,
956 	.ndo_change_mtu		= mlx4_en_change_mtu,
957 	.ndo_tx_timeout		= mlx4_en_tx_timeout,
958 	.ndo_vlan_rx_register	= mlx4_en_vlan_rx_register,
959 	.ndo_vlan_rx_add_vid	= mlx4_en_vlan_rx_add_vid,
960 	.ndo_vlan_rx_kill_vid	= mlx4_en_vlan_rx_kill_vid,
961 #ifdef CONFIG_NET_POLL_CONTROLLER
962 	.ndo_poll_controller	= mlx4_en_netpoll,
963 #endif
964 };
965 
mlx4_en_init_netdev(struct mlx4_en_dev * mdev,int port,struct mlx4_en_port_profile * prof)966 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
967 			struct mlx4_en_port_profile *prof)
968 {
969 	struct net_device *dev;
970 	struct mlx4_en_priv *priv;
971 	int i;
972 	int err;
973 
974 	dev = alloc_etherdev(sizeof(struct mlx4_en_priv));
975 	if (dev == NULL) {
976 		mlx4_err(mdev, "Net device allocation failed\n");
977 		return -ENOMEM;
978 	}
979 
980 	SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
981 
982 	/*
983 	 * Initialize driver private data
984 	 */
985 
986 	priv = netdev_priv(dev);
987 	memset(priv, 0, sizeof(struct mlx4_en_priv));
988 	priv->dev = dev;
989 	priv->mdev = mdev;
990 	priv->prof = prof;
991 	priv->port = port;
992 	priv->port_up = false;
993 	priv->rx_csum = 1;
994 	priv->flags = prof->flags;
995 	priv->tx_ring_num = prof->tx_ring_num;
996 	priv->rx_ring_num = prof->rx_ring_num;
997 	priv->mc_list = NULL;
998 	priv->mac_index = -1;
999 	priv->msg_enable = MLX4_EN_MSG_LEVEL;
1000 	spin_lock_init(&priv->stats_lock);
1001 	INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
1002 	INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
1003 	INIT_DELAYED_WORK(&priv->refill_task, mlx4_en_rx_refill);
1004 	INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
1005 	INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
1006 	INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
1007 
1008 	/* Query for default mac and max mtu */
1009 	priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
1010 	priv->mac = mdev->dev->caps.def_mac[priv->port];
1011 	if (ILLEGAL_MAC(priv->mac)) {
1012 		mlx4_err(mdev, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
1013 			 priv->port, priv->mac);
1014 		err = -EINVAL;
1015 		goto out;
1016 	}
1017 
1018 	priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
1019 					  DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
1020 	err = mlx4_en_alloc_resources(priv);
1021 	if (err)
1022 		goto out;
1023 
1024 	/* Populate Rx default RSS mappings */
1025 	mlx4_en_set_default_rss_map(priv, &priv->rss_map, priv->rx_ring_num *
1026 						RSS_FACTOR, priv->rx_ring_num);
1027 	/* Allocate page for receive rings */
1028 	err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
1029 				MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
1030 	if (err) {
1031 		mlx4_err(mdev, "Failed to allocate page for rx qps\n");
1032 		goto out;
1033 	}
1034 	priv->allocated = 1;
1035 
1036 	/* Populate Tx priority mappings */
1037 	mlx4_en_set_prio_map(priv, priv->tx_prio_map, prof->tx_ring_num);
1038 
1039 	/*
1040 	 * Initialize netdev entry points
1041 	 */
1042 	dev->netdev_ops = &mlx4_netdev_ops;
1043 	dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
1044 
1045 	SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
1046 
1047 	/* Set defualt MAC */
1048 	dev->addr_len = ETH_ALEN;
1049 	for (i = 0; i < ETH_ALEN; i++)
1050 		dev->dev_addr[ETH_ALEN - 1 - i] =
1051 		(u8) (priv->mac >> (8 * i));
1052 
1053 	/*
1054 	 * Set driver features
1055 	 */
1056 	dev->features |= NETIF_F_SG;
1057 	dev->features |= NETIF_F_HW_CSUM;
1058 	dev->features |= NETIF_F_HIGHDMA;
1059 	dev->features |= NETIF_F_HW_VLAN_TX |
1060 			 NETIF_F_HW_VLAN_RX |
1061 			 NETIF_F_HW_VLAN_FILTER;
1062 	if (mdev->profile.num_lro)
1063 		dev->features |= NETIF_F_LRO;
1064 	if (mdev->LSO_support) {
1065 		dev->features |= NETIF_F_TSO;
1066 		dev->features |= NETIF_F_TSO6;
1067 	}
1068 
1069 	mdev->pndev[port] = dev;
1070 
1071 	netif_carrier_off(dev);
1072 	err = register_netdev(dev);
1073 	if (err) {
1074 		mlx4_err(mdev, "Netdev registration failed\n");
1075 		goto out;
1076 	}
1077 	priv->registered = 1;
1078 	queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1079 	return 0;
1080 
1081 out:
1082 	mlx4_en_destroy_netdev(dev);
1083 	return err;
1084 }
1085 
1086