mlx4_en: Removed redundant stride variable
[linux-2.6/verdex.git] / drivers / net / mlx4 / en_netdev.c
blobbe487fa8d9a7c93e5c621b1e4715bcdfe07cc166
1 /*
2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
34 #include <linux/etherdevice.h>
35 #include <linux/tcp.h>
36 #include <linux/if_vlan.h>
37 #include <linux/delay.h>
39 #include <linux/mlx4/driver.h>
40 #include <linux/mlx4/device.h>
41 #include <linux/mlx4/cmd.h>
42 #include <linux/mlx4/cq.h>
44 #include "mlx4_en.h"
45 #include "en_port.h"
48 static void mlx4_en_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
50 struct mlx4_en_priv *priv = netdev_priv(dev);
51 struct mlx4_en_dev *mdev = priv->mdev;
52 int err;
54 mlx4_dbg(HW, priv, "Registering VLAN group:%p\n", grp);
55 priv->vlgrp = grp;
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");
63 mutex_unlock(&mdev->state_lock);
66 static void mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
68 struct mlx4_en_priv *priv = netdev_priv(dev);
69 struct mlx4_en_dev *mdev = priv->mdev;
70 int err;
72 if (!priv->vlgrp)
73 return;
75 mlx4_dbg(HW, priv, "adding VLAN:%d (vlgrp entry:%p)\n",
76 vid, vlan_group_get_device(priv->vlgrp, vid));
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");
85 mutex_unlock(&mdev->state_lock);
88 static void mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
90 struct mlx4_en_priv *priv = netdev_priv(dev);
91 struct mlx4_en_dev *mdev = priv->mdev;
92 int err;
94 if (!priv->vlgrp)
95 return;
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);
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");
109 mutex_unlock(&mdev->state_lock);
112 static u64 mlx4_en_mac_to_u64(u8 *addr)
114 u64 mac = 0;
115 int i;
117 for (i = 0; i < ETH_ALEN; i++) {
118 mac <<= 8;
119 mac |= addr[i];
121 return mac;
124 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
126 struct mlx4_en_priv *priv = netdev_priv(dev);
127 struct mlx4_en_dev *mdev = priv->mdev;
128 struct sockaddr *saddr = addr;
130 if (!is_valid_ether_addr(saddr->sa_data))
131 return -EADDRNOTAVAIL;
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;
139 static void mlx4_en_do_set_mac(struct work_struct *work)
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;
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");
157 mutex_unlock(&mdev->state_lock);
160 static void mlx4_en_clear_list(struct net_device *dev)
162 struct mlx4_en_priv *priv = netdev_priv(dev);
163 struct dev_mc_list *plist = priv->mc_list;
164 struct dev_mc_list *next;
166 while (plist) {
167 next = plist->next;
168 kfree(plist);
169 plist = next;
171 priv->mc_list = NULL;
174 static void mlx4_en_cache_mclist(struct net_device *dev)
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;
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;
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;
200 static void mlx4_en_set_multicast(struct net_device *dev)
202 struct mlx4_en_priv *priv = netdev_priv(dev);
204 if (!priv->port_up)
205 return;
207 queue_work(priv->mdev->workqueue, &priv->mcast_task);
210 static void mlx4_en_do_set_multicast(struct work_struct *work)
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;
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;
226 if (!priv->port_up) {
227 mlx4_dbg(HW, priv, "Port is down, ignoring "
228 "multicast change.\n");
229 goto out;
233 * Promsicuous mode: disable all filters
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;
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");
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");
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");
263 goto out;
267 * Not in promiscous mode
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;
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");
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");
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");
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);
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);
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");
319 mlx4_en_clear_list(dev);
321 out:
322 mutex_unlock(&mdev->state_lock);
325 #ifdef CONFIG_NET_POLL_CONTROLLER
326 static void mlx4_en_netpoll(struct net_device *dev)
328 struct mlx4_en_priv *priv = netdev_priv(dev);
329 struct mlx4_en_cq *cq;
330 unsigned long flags;
331 int i;
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);
341 #endif
343 static void mlx4_en_tx_timeout(struct net_device *dev)
345 struct mlx4_en_priv *priv = netdev_priv(dev);
346 struct mlx4_en_dev *mdev = priv->mdev;
348 if (netif_msg_timer(priv))
349 mlx4_warn(mdev, "Tx timeout called on port:%d\n", priv->port);
351 priv->port_stats.tx_timeout++;
352 mlx4_dbg(DRV, priv, "Scheduling watchdog\n");
353 queue_work(mdev->workqueue, &priv->watchdog_task);
357 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
359 struct mlx4_en_priv *priv = netdev_priv(dev);
361 spin_lock_bh(&priv->stats_lock);
362 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
363 spin_unlock_bh(&priv->stats_lock);
365 return &priv->ret_stats;
368 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
370 struct mlx4_en_cq *cq;
371 int i;
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.
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);
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;
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;
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;
412 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
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;
428 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
429 return;
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);
437 if (!priv->last_moder_jiffies || !period)
438 goto out;
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;
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;
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;
475 mlx4_dbg(INTR, priv, "tx rate:%lu rx_rate:%lu\n",
476 tx_pkt_diff * HZ / period, rx_pkt_diff * HZ / period);
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);
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;
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;
504 static void mlx4_en_do_get_stats(struct work_struct *work)
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;
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);
517 mutex_lock(&mdev->state_lock);
518 if (mdev->device_up) {
519 if (priv->port_up)
520 mlx4_en_auto_moderation(priv);
522 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
524 mutex_unlock(&mdev->state_lock);
527 static void mlx4_en_linkstate(struct work_struct *work)
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;
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);
548 priv->last_link_state = linkstate;
549 mutex_unlock(&mdev->state_lock);
553 int mlx4_en_start_port(struct net_device *dev)
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 int err = 0;
563 int i;
564 int j;
566 if (priv->port_up) {
567 mlx4_dbg(DRV, priv, "start port called while port already up\n");
568 return 0;
571 /* Calculate Rx buf size */
572 dev->mtu = min(dev->mtu, priv->max_mtu);
573 mlx4_en_calc_rx_buf(dev);
574 mlx4_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
575 /* Configure rx cq's and rings */
576 for (i = 0; i < priv->rx_ring_num; i++) {
577 cq = &priv->rx_cq[i];
578 rx_ring = &priv->rx_ring[i];
580 err = mlx4_en_activate_cq(priv, cq);
581 if (err) {
582 mlx4_err(mdev, "Failed activating Rx CQ\n");
583 goto cq_err;
585 for (j = 0; j < cq->size; j++)
586 cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
587 err = mlx4_en_set_cq_moder(priv, cq);
588 if (err) {
589 mlx4_err(mdev, "Failed setting cq moderation parameters");
590 mlx4_en_deactivate_cq(priv, cq);
591 goto cq_err;
593 mlx4_en_arm_cq(priv, cq);
595 ++rx_index;
598 err = mlx4_en_activate_rx_rings(priv);
599 if (err) {
600 mlx4_err(mdev, "Failed to activate RX rings\n");
601 goto cq_err;
604 err = mlx4_en_config_rss_steer(priv);
605 if (err) {
606 mlx4_err(mdev, "Failed configuring rss steering\n");
607 goto rx_err;
610 /* Configure tx cq's and rings */
611 for (i = 0; i < priv->tx_ring_num; i++) {
612 /* Configure cq */
613 cq = &priv->tx_cq[i];
614 err = mlx4_en_activate_cq(priv, cq);
615 if (err) {
616 mlx4_err(mdev, "Failed allocating Tx CQ\n");
617 goto tx_err;
619 err = mlx4_en_set_cq_moder(priv, cq);
620 if (err) {
621 mlx4_err(mdev, "Failed setting cq moderation parameters");
622 mlx4_en_deactivate_cq(priv, cq);
623 goto tx_err;
625 mlx4_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
626 cq->buf->wqe_index = cpu_to_be16(0xffff);
628 /* Configure ring */
629 tx_ring = &priv->tx_ring[i];
630 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
631 priv->rx_ring[0].srq.srqn);
632 if (err) {
633 mlx4_err(mdev, "Failed allocating Tx ring\n");
634 mlx4_en_deactivate_cq(priv, cq);
635 goto tx_err;
637 /* Set initial ownership of all Tx TXBBs to SW (1) */
638 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
639 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
640 ++tx_index;
643 /* Configure port */
644 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
645 priv->rx_skb_size + ETH_FCS_LEN,
646 priv->prof->tx_pause,
647 priv->prof->tx_ppp,
648 priv->prof->rx_pause,
649 priv->prof->rx_ppp);
650 if (err) {
651 mlx4_err(mdev, "Failed setting port general configurations"
652 " for port %d, with error %d\n", priv->port, err);
653 goto tx_err;
655 /* Set default qp number */
656 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
657 if (err) {
658 mlx4_err(mdev, "Failed setting default qp numbers\n");
659 goto tx_err;
661 /* Set port mac number */
662 mlx4_dbg(DRV, priv, "Setting mac for port %d\n", priv->port);
663 err = mlx4_register_mac(mdev->dev, priv->port,
664 priv->mac, &priv->mac_index);
665 if (err) {
666 mlx4_err(mdev, "Failed setting port mac\n");
667 goto tx_err;
670 /* Init port */
671 mlx4_dbg(HW, priv, "Initializing port\n");
672 err = mlx4_INIT_PORT(mdev->dev, priv->port);
673 if (err) {
674 mlx4_err(mdev, "Failed Initializing port\n");
675 goto mac_err;
678 /* Schedule multicast task to populate multicast list */
679 queue_work(mdev->workqueue, &priv->mcast_task);
681 priv->port_up = true;
682 netif_start_queue(dev);
683 return 0;
685 mac_err:
686 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
687 tx_err:
688 while (tx_index--) {
689 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
690 mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
693 mlx4_en_release_rss_steer(priv);
694 rx_err:
695 for (i = 0; i < priv->rx_ring_num; i++)
696 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
697 cq_err:
698 while (rx_index--)
699 mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
701 return err; /* need to close devices */
705 void mlx4_en_stop_port(struct net_device *dev)
707 struct mlx4_en_priv *priv = netdev_priv(dev);
708 struct mlx4_en_dev *mdev = priv->mdev;
709 int i;
711 if (!priv->port_up) {
712 mlx4_dbg(DRV, priv, "stop port (%d) called while port already down\n",
713 priv->port);
714 return;
716 netif_stop_queue(dev);
718 /* Synchronize with tx routine */
719 netif_tx_lock_bh(dev);
720 priv->port_up = false;
721 netif_tx_unlock_bh(dev);
723 /* close port*/
724 mlx4_CLOSE_PORT(mdev->dev, priv->port);
726 /* Unregister Mac address for the port */
727 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
729 /* Free TX Rings */
730 for (i = 0; i < priv->tx_ring_num; i++) {
731 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
732 mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
734 msleep(10);
736 for (i = 0; i < priv->tx_ring_num; i++)
737 mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
739 /* Free RSS qps */
740 mlx4_en_release_rss_steer(priv);
742 /* Free RX Rings */
743 for (i = 0; i < priv->rx_ring_num; i++) {
744 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
745 while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
746 msleep(1);
747 mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
751 static void mlx4_en_restart(struct work_struct *work)
753 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
754 watchdog_task);
755 struct mlx4_en_dev *mdev = priv->mdev;
756 struct net_device *dev = priv->dev;
758 mlx4_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
760 mutex_lock(&mdev->state_lock);
761 if (priv->port_up) {
762 mlx4_en_stop_port(dev);
763 if (mlx4_en_start_port(dev))
764 mlx4_err(mdev, "Failed restarting port %d\n", priv->port);
766 mutex_unlock(&mdev->state_lock);
770 static int mlx4_en_open(struct net_device *dev)
772 struct mlx4_en_priv *priv = netdev_priv(dev);
773 struct mlx4_en_dev *mdev = priv->mdev;
774 int i;
775 int err = 0;
777 mutex_lock(&mdev->state_lock);
779 if (!mdev->device_up) {
780 mlx4_err(mdev, "Cannot open - device down/disabled\n");
781 err = -EBUSY;
782 goto out;
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");
789 memset(&priv->stats, 0, sizeof(priv->stats));
790 memset(&priv->pstats, 0, sizeof(priv->pstats));
792 for (i = 0; i < priv->tx_ring_num; i++) {
793 priv->tx_ring[i].bytes = 0;
794 priv->tx_ring[i].packets = 0;
796 for (i = 0; i < priv->rx_ring_num; i++) {
797 priv->rx_ring[i].bytes = 0;
798 priv->rx_ring[i].packets = 0;
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);
806 out:
807 mutex_unlock(&mdev->state_lock);
808 return err;
812 static int mlx4_en_close(struct net_device *dev)
814 struct mlx4_en_priv *priv = netdev_priv(dev);
815 struct mlx4_en_dev *mdev = priv->mdev;
817 if (netif_msg_ifdown(priv))
818 mlx4_info(mdev, "Close called for port:%d\n", priv->port);
820 mutex_lock(&mdev->state_lock);
822 mlx4_en_stop_port(dev);
823 netif_carrier_off(dev);
825 mutex_unlock(&mdev->state_lock);
826 return 0;
829 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
831 int i;
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]);
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]);
848 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
850 struct mlx4_en_dev *mdev = priv->mdev;
851 struct mlx4_en_port_profile *prof = priv->prof;
852 int i;
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;
860 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
861 prof->tx_ring_size, TXBB_SIZE))
862 goto err;
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;
871 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
872 prof->rx_ring_size, priv->stride))
873 goto err;
876 return 0;
878 err:
879 mlx4_err(mdev, "Failed to allocate NIC resources\n");
880 return -ENOMEM;
884 void mlx4_en_destroy_netdev(struct net_device *dev)
886 struct mlx4_en_priv *priv = netdev_priv(dev);
887 struct mlx4_en_dev *mdev = priv->mdev;
889 mlx4_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
891 /* Unregister device - this will close the port if it was up */
892 if (priv->registered)
893 unregister_netdev(dev);
895 if (priv->allocated)
896 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
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);
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);
908 mlx4_en_free_resources(priv);
909 free_netdev(dev);
912 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
914 struct mlx4_en_priv *priv = netdev_priv(dev);
915 struct mlx4_en_dev *mdev = priv->mdev;
916 int err = 0;
918 mlx4_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
919 dev->mtu, new_mtu);
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;
925 dev->mtu = new_mtu;
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);
943 mutex_unlock(&mdev->state_lock);
945 return 0;
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
966 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
967 struct mlx4_en_port_profile *prof)
969 struct net_device *dev;
970 struct mlx4_en_priv *priv;
971 int i;
972 int err;
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;
980 SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
983 * Initialize driver private data
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);
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;
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;
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;
1034 priv->allocated = 1;
1036 /* Populate Tx priority mappings */
1037 mlx4_en_set_prio_map(priv, priv->tx_prio_map, prof->tx_ring_num);
1040 * Initialize netdev entry points
1042 dev->netdev_ops = &mlx4_netdev_ops;
1043 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
1045 SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
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));
1054 * Set driver features
1056 dev->features |= NETIF_F_SG;
1057 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_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;
1069 mdev->pndev[port] = dev;
1071 netif_carrier_off(dev);
1072 err = register_netdev(dev);
1073 if (err) {
1074 mlx4_err(mdev, "Netdev registration failed\n");
1075 goto out;
1077 priv->registered = 1;
1078 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1079 return 0;
1081 out:
1082 mlx4_en_destroy_netdev(dev);
1083 return err;