Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
[deliverable/linux.git] / drivers / net / mlx4 / en_netdev.c
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
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
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
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
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
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
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
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
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
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
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
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
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 priv->port_stats.tx_timeout++;
352 mlx4_dbg(DRV, priv, "Scheduling watchdog\n");
353 queue_work(mdev->workqueue, &priv->watchdog_task);
354 }
355
356
357 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
358 {
359 struct mlx4_en_priv *priv = netdev_priv(dev);
360
361 spin_lock_bh(&priv->stats_lock);
362 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
363 spin_unlock_bh(&priv->stats_lock);
364
365 return &priv->ret_stats;
366 }
367
368 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
369 {
370 struct mlx4_en_cq *cq;
371 int i;
372
373 /* If we haven't received a specific coalescing setting
374 * (module param), we set the moderation paramters as follows:
375 * - moder_cnt is set to the number of mtu sized packets to
376 * satisfy our coelsing target.
377 * - moder_time is set to a fixed value.
378 */
379 priv->rx_frames = MLX4_EN_RX_COAL_TARGET / priv->dev->mtu + 1;
380 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
381 mlx4_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
382 "rx_frames:%d rx_usecs:%d\n",
383 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
384
385 /* Setup cq moderation params */
386 for (i = 0; i < priv->rx_ring_num; i++) {
387 cq = &priv->rx_cq[i];
388 cq->moder_cnt = priv->rx_frames;
389 cq->moder_time = priv->rx_usecs;
390 }
391
392 for (i = 0; i < priv->tx_ring_num; i++) {
393 cq = &priv->tx_cq[i];
394 cq->moder_cnt = MLX4_EN_TX_COAL_PKTS;
395 cq->moder_time = MLX4_EN_TX_COAL_TIME;
396 }
397
398 /* Reset auto-moderation params */
399 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
400 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
401 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
402 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
403 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
404 priv->adaptive_rx_coal = 1;
405 priv->last_moder_time = MLX4_EN_AUTO_CONF;
406 priv->last_moder_jiffies = 0;
407 priv->last_moder_packets = 0;
408 priv->last_moder_tx_packets = 0;
409 priv->last_moder_bytes = 0;
410 }
411
412 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
413 {
414 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
415 struct mlx4_en_dev *mdev = priv->mdev;
416 struct mlx4_en_cq *cq;
417 unsigned long packets;
418 unsigned long rate;
419 unsigned long avg_pkt_size;
420 unsigned long rx_packets;
421 unsigned long rx_bytes;
422 unsigned long tx_packets;
423 unsigned long tx_pkt_diff;
424 unsigned long rx_pkt_diff;
425 int moder_time;
426 int i, err;
427
428 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
429 return;
430
431 spin_lock_bh(&priv->stats_lock);
432 rx_packets = priv->stats.rx_packets;
433 rx_bytes = priv->stats.rx_bytes;
434 tx_packets = priv->stats.tx_packets;
435 spin_unlock_bh(&priv->stats_lock);
436
437 if (!priv->last_moder_jiffies || !period)
438 goto out;
439
440 tx_pkt_diff = ((unsigned long) (tx_packets -
441 priv->last_moder_tx_packets));
442 rx_pkt_diff = ((unsigned long) (rx_packets -
443 priv->last_moder_packets));
444 packets = max(tx_pkt_diff, rx_pkt_diff);
445 rate = packets * HZ / period;
446 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
447 priv->last_moder_bytes)) / packets : 0;
448
449 /* Apply auto-moderation only when packet rate exceeds a rate that
450 * it matters */
451 if (rate > MLX4_EN_RX_RATE_THRESH) {
452 /* If tx and rx packet rates are not balanced, assume that
453 * traffic is mainly BW bound and apply maximum moderation.
454 * Otherwise, moderate according to packet rate */
455 if (2 * tx_pkt_diff > 3 * rx_pkt_diff ||
456 2 * rx_pkt_diff > 3 * tx_pkt_diff) {
457 moder_time = priv->rx_usecs_high;
458 } else {
459 if (rate < priv->pkt_rate_low)
460 moder_time = priv->rx_usecs_low;
461 else if (rate > priv->pkt_rate_high)
462 moder_time = priv->rx_usecs_high;
463 else
464 moder_time = (rate - priv->pkt_rate_low) *
465 (priv->rx_usecs_high - priv->rx_usecs_low) /
466 (priv->pkt_rate_high - priv->pkt_rate_low) +
467 priv->rx_usecs_low;
468 }
469 } else {
470 /* When packet rate is low, use default moderation rather than
471 * 0 to prevent interrupt storms if traffic suddenly increases */
472 moder_time = priv->rx_usecs;
473 }
474
475 mlx4_dbg(INTR, priv, "tx rate:%lu rx_rate:%lu\n",
476 tx_pkt_diff * HZ / period, rx_pkt_diff * HZ / period);
477
478 mlx4_dbg(INTR, priv, "Rx moder_time changed from:%d to %d period:%lu "
479 "[jiff] packets:%lu avg_pkt_size:%lu rate:%lu [p/s])\n",
480 priv->last_moder_time, moder_time, period, packets,
481 avg_pkt_size, rate);
482
483 if (moder_time != priv->last_moder_time) {
484 priv->last_moder_time = moder_time;
485 for (i = 0; i < priv->rx_ring_num; i++) {
486 cq = &priv->rx_cq[i];
487 cq->moder_time = moder_time;
488 err = mlx4_en_set_cq_moder(priv, cq);
489 if (err) {
490 mlx4_err(mdev, "Failed modifying moderation for cq:%d "
491 "on port:%d\n", i, priv->port);
492 break;
493 }
494 }
495 }
496
497 out:
498 priv->last_moder_packets = rx_packets;
499 priv->last_moder_tx_packets = tx_packets;
500 priv->last_moder_bytes = rx_bytes;
501 priv->last_moder_jiffies = jiffies;
502 }
503
504 static void mlx4_en_do_get_stats(struct work_struct *work)
505 {
506 struct delayed_work *delay = to_delayed_work(work);
507 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
508 stats_task);
509 struct mlx4_en_dev *mdev = priv->mdev;
510 int err;
511
512 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
513 if (err)
514 mlx4_dbg(HW, priv, "Could not update stats for "
515 "port:%d\n", priv->port);
516
517 mutex_lock(&mdev->state_lock);
518 if (mdev->device_up) {
519 if (priv->port_up)
520 mlx4_en_auto_moderation(priv);
521
522 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
523 }
524 mutex_unlock(&mdev->state_lock);
525 }
526
527 static void mlx4_en_linkstate(struct work_struct *work)
528 {
529 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
530 linkstate_task);
531 struct mlx4_en_dev *mdev = priv->mdev;
532 int linkstate = priv->link_state;
533
534 mutex_lock(&mdev->state_lock);
535 /* If observable port state changed set carrier state and
536 * report to system log */
537 if (priv->last_link_state != linkstate) {
538 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
539 if (netif_msg_link(priv))
540 mlx4_info(mdev, "Port %d - link down\n", priv->port);
541 netif_carrier_off(priv->dev);
542 } else {
543 if (netif_msg_link(priv))
544 mlx4_info(mdev, "Port %d - link up\n", priv->port);
545 netif_carrier_on(priv->dev);
546 }
547 }
548 priv->last_link_state = linkstate;
549 mutex_unlock(&mdev->state_lock);
550 }
551
552
553 int mlx4_en_start_port(struct net_device *dev)
554 {
555 struct mlx4_en_priv *priv = netdev_priv(dev);
556 struct mlx4_en_dev *mdev = priv->mdev;
557 struct mlx4_en_cq *cq;
558 struct mlx4_en_tx_ring *tx_ring;
559 struct mlx4_en_rx_ring *rx_ring;
560 int rx_index = 0;
561 int tx_index = 0;
562 u16 stride;
563 int err = 0;
564 int i;
565 int j;
566
567 if (priv->port_up) {
568 mlx4_dbg(DRV, priv, "start port called while port already up\n");
569 return 0;
570 }
571
572 /* Calculate Rx buf size */
573 dev->mtu = min(dev->mtu, priv->max_mtu);
574 mlx4_en_calc_rx_buf(dev);
575 mlx4_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
576 stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
577 DS_SIZE * priv->num_frags);
578 /* Configure rx cq's and rings */
579 for (i = 0; i < priv->rx_ring_num; i++) {
580 cq = &priv->rx_cq[i];
581 rx_ring = &priv->rx_ring[i];
582
583 err = mlx4_en_activate_cq(priv, cq);
584 if (err) {
585 mlx4_err(mdev, "Failed activating Rx CQ\n");
586 goto rx_err;
587 }
588 for (j = 0; j < cq->size; j++)
589 cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
590 err = mlx4_en_set_cq_moder(priv, cq);
591 if (err) {
592 mlx4_err(mdev, "Failed setting cq moderation parameters");
593 mlx4_en_deactivate_cq(priv, cq);
594 goto cq_err;
595 }
596 mlx4_en_arm_cq(priv, cq);
597
598 ++rx_index;
599 }
600
601 err = mlx4_en_activate_rx_rings(priv);
602 if (err) {
603 mlx4_err(mdev, "Failed to activate RX rings\n");
604 goto cq_err;
605 }
606
607 err = mlx4_en_config_rss_steer(priv);
608 if (err) {
609 mlx4_err(mdev, "Failed configuring rss steering\n");
610 goto rx_err;
611 }
612
613 /* Configure tx cq's and rings */
614 for (i = 0; i < priv->tx_ring_num; i++) {
615 /* Configure cq */
616 cq = &priv->tx_cq[i];
617 err = mlx4_en_activate_cq(priv, cq);
618 if (err) {
619 mlx4_err(mdev, "Failed allocating Tx CQ\n");
620 goto tx_err;
621 }
622 err = mlx4_en_set_cq_moder(priv, cq);
623 if (err) {
624 mlx4_err(mdev, "Failed setting cq moderation parameters");
625 mlx4_en_deactivate_cq(priv, cq);
626 goto tx_err;
627 }
628 mlx4_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
629 cq->buf->wqe_index = cpu_to_be16(0xffff);
630
631 /* Configure ring */
632 tx_ring = &priv->tx_ring[i];
633 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
634 priv->rx_ring[0].srq.srqn);
635 if (err) {
636 mlx4_err(mdev, "Failed allocating Tx ring\n");
637 mlx4_en_deactivate_cq(priv, cq);
638 goto tx_err;
639 }
640 /* Set initial ownership of all Tx TXBBs to SW (1) */
641 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
642 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
643 ++tx_index;
644 }
645
646 /* Configure port */
647 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
648 priv->rx_skb_size + ETH_FCS_LEN,
649 priv->prof->tx_pause,
650 priv->prof->tx_ppp,
651 priv->prof->rx_pause,
652 priv->prof->rx_ppp);
653 if (err) {
654 mlx4_err(mdev, "Failed setting port general configurations"
655 " for port %d, with error %d\n", priv->port, err);
656 goto tx_err;
657 }
658 /* Set default qp number */
659 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
660 if (err) {
661 mlx4_err(mdev, "Failed setting default qp numbers\n");
662 goto tx_err;
663 }
664 /* Set port mac number */
665 mlx4_dbg(DRV, priv, "Setting mac for port %d\n", priv->port);
666 err = mlx4_register_mac(mdev->dev, priv->port,
667 priv->mac, &priv->mac_index);
668 if (err) {
669 mlx4_err(mdev, "Failed setting port mac\n");
670 goto tx_err;
671 }
672
673 /* Init port */
674 mlx4_dbg(HW, priv, "Initializing port\n");
675 err = mlx4_INIT_PORT(mdev->dev, priv->port);
676 if (err) {
677 mlx4_err(mdev, "Failed Initializing port\n");
678 goto mac_err;
679 }
680
681 /* Schedule multicast task to populate multicast list */
682 queue_work(mdev->workqueue, &priv->mcast_task);
683
684 priv->port_up = true;
685 netif_start_queue(dev);
686 return 0;
687
688 mac_err:
689 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
690 tx_err:
691 while (tx_index--) {
692 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
693 mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
694 }
695
696 mlx4_en_release_rss_steer(priv);
697 rx_err:
698 for (i = 0; i < priv->rx_ring_num; i++)
699 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
700 cq_err:
701 while (rx_index--)
702 mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
703
704 return err; /* need to close devices */
705 }
706
707
708 void mlx4_en_stop_port(struct net_device *dev)
709 {
710 struct mlx4_en_priv *priv = netdev_priv(dev);
711 struct mlx4_en_dev *mdev = priv->mdev;
712 int i;
713
714 if (!priv->port_up) {
715 mlx4_dbg(DRV, priv, "stop port (%d) called while port already down\n",
716 priv->port);
717 return;
718 }
719 netif_stop_queue(dev);
720
721 /* Synchronize with tx routine */
722 netif_tx_lock_bh(dev);
723 priv->port_up = false;
724 netif_tx_unlock_bh(dev);
725
726 /* close port*/
727 mlx4_CLOSE_PORT(mdev->dev, priv->port);
728
729 /* Unregister Mac address for the port */
730 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
731
732 /* Free TX Rings */
733 for (i = 0; i < priv->tx_ring_num; i++) {
734 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
735 mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
736 }
737 msleep(10);
738
739 for (i = 0; i < priv->tx_ring_num; i++)
740 mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
741
742 /* Free RSS qps */
743 mlx4_en_release_rss_steer(priv);
744
745 /* Free RX Rings */
746 for (i = 0; i < priv->rx_ring_num; i++) {
747 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
748 while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
749 msleep(1);
750 mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
751 }
752 }
753
754 static void mlx4_en_restart(struct work_struct *work)
755 {
756 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
757 watchdog_task);
758 struct mlx4_en_dev *mdev = priv->mdev;
759 struct net_device *dev = priv->dev;
760
761 mlx4_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
762
763 mutex_lock(&mdev->state_lock);
764 if (priv->port_up) {
765 mlx4_en_stop_port(dev);
766 if (mlx4_en_start_port(dev))
767 mlx4_err(mdev, "Failed restarting port %d\n", priv->port);
768 }
769 mutex_unlock(&mdev->state_lock);
770 }
771
772
773 static int mlx4_en_open(struct net_device *dev)
774 {
775 struct mlx4_en_priv *priv = netdev_priv(dev);
776 struct mlx4_en_dev *mdev = priv->mdev;
777 int i;
778 int err = 0;
779
780 mutex_lock(&mdev->state_lock);
781
782 if (!mdev->device_up) {
783 mlx4_err(mdev, "Cannot open - device down/disabled\n");
784 err = -EBUSY;
785 goto out;
786 }
787
788 /* Reset HW statistics and performance counters */
789 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
790 mlx4_dbg(HW, priv, "Failed dumping statistics\n");
791
792 memset(&priv->stats, 0, sizeof(priv->stats));
793 memset(&priv->pstats, 0, sizeof(priv->pstats));
794
795 for (i = 0; i < priv->tx_ring_num; i++) {
796 priv->tx_ring[i].bytes = 0;
797 priv->tx_ring[i].packets = 0;
798 }
799 for (i = 0; i < priv->rx_ring_num; i++) {
800 priv->rx_ring[i].bytes = 0;
801 priv->rx_ring[i].packets = 0;
802 }
803
804 mlx4_en_set_default_moderation(priv);
805 err = mlx4_en_start_port(dev);
806 if (err)
807 mlx4_err(mdev, "Failed starting port:%d\n", priv->port);
808
809 out:
810 mutex_unlock(&mdev->state_lock);
811 return err;
812 }
813
814
815 static int mlx4_en_close(struct net_device *dev)
816 {
817 struct mlx4_en_priv *priv = netdev_priv(dev);
818 struct mlx4_en_dev *mdev = priv->mdev;
819
820 if (netif_msg_ifdown(priv))
821 mlx4_info(mdev, "Close called for port:%d\n", priv->port);
822
823 mutex_lock(&mdev->state_lock);
824
825 mlx4_en_stop_port(dev);
826 netif_carrier_off(dev);
827
828 mutex_unlock(&mdev->state_lock);
829 return 0;
830 }
831
832 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
833 {
834 int i;
835
836 for (i = 0; i < priv->tx_ring_num; i++) {
837 if (priv->tx_ring[i].tx_info)
838 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
839 if (priv->tx_cq[i].buf)
840 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
841 }
842
843 for (i = 0; i < priv->rx_ring_num; i++) {
844 if (priv->rx_ring[i].rx_info)
845 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i]);
846 if (priv->rx_cq[i].buf)
847 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
848 }
849 }
850
851 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
852 {
853 struct mlx4_en_dev *mdev = priv->mdev;
854 struct mlx4_en_port_profile *prof = priv->prof;
855 int i;
856
857 /* Create tx Rings */
858 for (i = 0; i < priv->tx_ring_num; i++) {
859 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
860 prof->tx_ring_size, i, TX))
861 goto err;
862
863 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
864 prof->tx_ring_size, TXBB_SIZE))
865 goto err;
866 }
867
868 /* Create rx Rings */
869 for (i = 0; i < priv->rx_ring_num; i++) {
870 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
871 prof->rx_ring_size, i, RX))
872 goto err;
873
874 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
875 prof->rx_ring_size, priv->stride))
876 goto err;
877 }
878
879 return 0;
880
881 err:
882 mlx4_err(mdev, "Failed to allocate NIC resources\n");
883 return -ENOMEM;
884 }
885
886
887 void mlx4_en_destroy_netdev(struct net_device *dev)
888 {
889 struct mlx4_en_priv *priv = netdev_priv(dev);
890 struct mlx4_en_dev *mdev = priv->mdev;
891
892 mlx4_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
893
894 /* Unregister device - this will close the port if it was up */
895 if (priv->registered)
896 unregister_netdev(dev);
897
898 if (priv->allocated)
899 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
900
901 cancel_delayed_work(&priv->stats_task);
902 cancel_delayed_work(&priv->refill_task);
903 /* flush any pending task for this netdev */
904 flush_workqueue(mdev->workqueue);
905
906 /* Detach the netdev so tasks would not attempt to access it */
907 mutex_lock(&mdev->state_lock);
908 mdev->pndev[priv->port] = NULL;
909 mutex_unlock(&mdev->state_lock);
910
911 mlx4_en_free_resources(priv);
912 free_netdev(dev);
913 }
914
915 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
916 {
917 struct mlx4_en_priv *priv = netdev_priv(dev);
918 struct mlx4_en_dev *mdev = priv->mdev;
919 int err = 0;
920
921 mlx4_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
922 dev->mtu, new_mtu);
923
924 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
925 mlx4_err(mdev, "Bad MTU size:%d.\n", new_mtu);
926 return -EPERM;
927 }
928 dev->mtu = new_mtu;
929
930 if (netif_running(dev)) {
931 mutex_lock(&mdev->state_lock);
932 if (!mdev->device_up) {
933 /* NIC is probably restarting - let watchdog task reset
934 * the port */
935 mlx4_dbg(DRV, priv, "Change MTU called with card down!?\n");
936 } else {
937 mlx4_en_stop_port(dev);
938 mlx4_en_set_default_moderation(priv);
939 err = mlx4_en_start_port(dev);
940 if (err) {
941 mlx4_err(mdev, "Failed restarting port:%d\n",
942 priv->port);
943 queue_work(mdev->workqueue, &priv->watchdog_task);
944 }
945 }
946 mutex_unlock(&mdev->state_lock);
947 }
948 return 0;
949 }
950
951 static const struct net_device_ops mlx4_netdev_ops = {
952 .ndo_open = mlx4_en_open,
953 .ndo_stop = mlx4_en_close,
954 .ndo_start_xmit = mlx4_en_xmit,
955 .ndo_get_stats = mlx4_en_get_stats,
956 .ndo_set_multicast_list = mlx4_en_set_multicast,
957 .ndo_set_mac_address = mlx4_en_set_mac,
958 .ndo_validate_addr = eth_validate_addr,
959 .ndo_change_mtu = mlx4_en_change_mtu,
960 .ndo_tx_timeout = mlx4_en_tx_timeout,
961 .ndo_vlan_rx_register = mlx4_en_vlan_rx_register,
962 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
963 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
964 #ifdef CONFIG_NET_POLL_CONTROLLER
965 .ndo_poll_controller = mlx4_en_netpoll,
966 #endif
967 };
968
969 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
970 struct mlx4_en_port_profile *prof)
971 {
972 struct net_device *dev;
973 struct mlx4_en_priv *priv;
974 int i;
975 int err;
976
977 dev = alloc_etherdev(sizeof(struct mlx4_en_priv));
978 if (dev == NULL) {
979 mlx4_err(mdev, "Net device allocation failed\n");
980 return -ENOMEM;
981 }
982
983 SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
984
985 /*
986 * Initialize driver private data
987 */
988
989 priv = netdev_priv(dev);
990 memset(priv, 0, sizeof(struct mlx4_en_priv));
991 priv->dev = dev;
992 priv->mdev = mdev;
993 priv->prof = prof;
994 priv->port = port;
995 priv->port_up = false;
996 priv->rx_csum = 1;
997 priv->flags = prof->flags;
998 priv->tx_ring_num = prof->tx_ring_num;
999 priv->rx_ring_num = prof->rx_ring_num;
1000 priv->mc_list = NULL;
1001 priv->mac_index = -1;
1002 priv->msg_enable = MLX4_EN_MSG_LEVEL;
1003 spin_lock_init(&priv->stats_lock);
1004 INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
1005 INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
1006 INIT_DELAYED_WORK(&priv->refill_task, mlx4_en_rx_refill);
1007 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
1008 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
1009 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
1010
1011 /* Query for default mac and max mtu */
1012 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
1013 priv->mac = mdev->dev->caps.def_mac[priv->port];
1014 if (ILLEGAL_MAC(priv->mac)) {
1015 mlx4_err(mdev, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
1016 priv->port, priv->mac);
1017 err = -EINVAL;
1018 goto out;
1019 }
1020
1021 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
1022 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
1023 err = mlx4_en_alloc_resources(priv);
1024 if (err)
1025 goto out;
1026
1027 /* Populate Rx default RSS mappings */
1028 mlx4_en_set_default_rss_map(priv, &priv->rss_map, priv->rx_ring_num *
1029 RSS_FACTOR, priv->rx_ring_num);
1030 /* Allocate page for receive rings */
1031 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
1032 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
1033 if (err) {
1034 mlx4_err(mdev, "Failed to allocate page for rx qps\n");
1035 goto out;
1036 }
1037 priv->allocated = 1;
1038
1039 /* Populate Tx priority mappings */
1040 mlx4_en_set_prio_map(priv, priv->tx_prio_map, prof->tx_ring_num);
1041
1042 /*
1043 * Initialize netdev entry points
1044 */
1045 dev->netdev_ops = &mlx4_netdev_ops;
1046 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
1047
1048 SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
1049
1050 /* Set defualt MAC */
1051 dev->addr_len = ETH_ALEN;
1052 for (i = 0; i < ETH_ALEN; i++)
1053 dev->dev_addr[ETH_ALEN - 1 - i] =
1054 (u8) (priv->mac >> (8 * i));
1055
1056 /*
1057 * Set driver features
1058 */
1059 dev->features |= NETIF_F_SG;
1060 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1061 dev->features |= NETIF_F_HIGHDMA;
1062 dev->features |= NETIF_F_HW_VLAN_TX |
1063 NETIF_F_HW_VLAN_RX |
1064 NETIF_F_HW_VLAN_FILTER;
1065 if (mdev->profile.num_lro)
1066 dev->features |= NETIF_F_LRO;
1067 if (mdev->LSO_support) {
1068 dev->features |= NETIF_F_TSO;
1069 dev->features |= NETIF_F_TSO6;
1070 }
1071
1072 mdev->pndev[port] = dev;
1073
1074 netif_carrier_off(dev);
1075 err = register_netdev(dev);
1076 if (err) {
1077 mlx4_err(mdev, "Netdev registration failed\n");
1078 goto out;
1079 }
1080 priv->registered = 1;
1081 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1082 return 0;
1083
1084 out:
1085 mlx4_en_destroy_netdev(dev);
1086 return err;
1087 }
1088
This page took 0.053297 seconds and 6 git commands to generate.