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
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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
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>
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;
54 mlx4_dbg(HW, priv, "Registering VLAN group:%p\n", 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);
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;
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);
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;
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);
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)
117 for (i = 0; i < ETH_ALEN; i++) {
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);
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,
143 struct mlx4_en_dev *mdev = priv->mdev;
146 mutex_lock(&mdev->state_lock);
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);
153 mlx4_err(mdev, "Failed changing HW MAC address\n");
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;
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);
185 mlx4_err(mdev, "failed to allocate multicast list\n");
186 mlx4_en_clear_list(dev);
189 memcpy(tmp, mclist, sizeof(struct dev_mc_list));
200 static void mlx4_en_set_multicast(struct net_device *dev)
202 struct mlx4_en_priv *priv = netdev_priv(dev);
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,
214 struct mlx4_en_dev *mdev = priv->mdev;
215 struct net_device *dev = priv->dev;
216 struct dev_mc_list *mclist;
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");
226 if (!priv->port_up) {
227 mlx4_dbg(HW, priv, "Port is down, ignoring "
228 "multicast change.\n");
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",
241 priv->flags |= MLX4_EN_FLAG_PROMISC;
243 /* Enable promiscouos mode */
244 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
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);
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);
260 mlx4_err(mdev, "Failed disabling "
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",
274 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
276 /* Disable promiscouos mode */
277 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
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);
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);
293 mlx4_err(mdev, "Failed disabling multicast filter\n");
295 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
296 0, MLX4_MCAST_DISABLE);
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);
317 mlx4_err(mdev, "Failed enabling multicast filter\n");
319 mlx4_en_clear_list(dev);
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;
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);
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 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);
359 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
361 struct mlx4_en_priv *priv = netdev_priv(dev);
363 spin_lock_bh(&priv->stats_lock);
364 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
365 spin_unlock_bh(&priv->stats_lock);
367 return &priv->ret_stats;
370 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
372 struct mlx4_en_cq *cq;
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.
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);
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;
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;
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;
414 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
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;
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;
430 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
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);
439 if (!priv->last_moder_jiffies || !period)
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;
451 /* Apply auto-moderation only when packet rate exceeds a rate that
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;
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;
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) +
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;
477 mlx4_dbg(INTR, priv, "tx rate:%lu rx_rate:%lu\n",
478 tx_pkt_diff * HZ / period, rx_pkt_diff * HZ / period);
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,
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);
492 mlx4_err(mdev, "Failed modifying moderation for cq:%d "
493 "on port:%d\n", i, priv->port);
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;
506 static void mlx4_en_do_get_stats(struct work_struct *work)
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,
511 struct mlx4_en_dev *mdev = priv->mdev;
514 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
516 mlx4_dbg(HW, priv, "Could not update stats for "
517 "port:%d\n", priv->port);
519 mutex_lock(&mdev->state_lock);
520 if (mdev->device_up) {
522 mlx4_en_auto_moderation(priv);
524 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
526 mutex_unlock(&mdev->state_lock);
529 static void mlx4_en_linkstate(struct work_struct *work)
531 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
533 struct mlx4_en_dev *mdev = priv->mdev;
534 int linkstate = priv->link_state;
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);
545 if (netif_msg_link(priv))
546 mlx4_info(mdev, "Port %d - link up\n", priv->port);
547 netif_carrier_on(priv->dev);
550 priv->last_link_state = linkstate;
551 mutex_unlock(&mdev->state_lock);
555 int mlx4_en_start_port(struct net_device *dev)
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;
570 mlx4_dbg(DRV, priv, "start port called while port already up\n");
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];
585 err = mlx4_en_activate_cq(priv, cq);
587 mlx4_err(mdev, "Failed activating Rx CQ\n");
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);
594 mlx4_err(mdev, "Failed setting cq moderation parameters");
595 mlx4_en_deactivate_cq(priv, cq);
598 mlx4_en_arm_cq(priv, cq);
603 err = mlx4_en_activate_rx_rings(priv);
605 mlx4_err(mdev, "Failed to activate RX rings\n");
609 err = mlx4_en_config_rss_steer(priv);
611 mlx4_err(mdev, "Failed configuring rss steering\n");
615 /* Configure tx cq's and rings */
616 for (i = 0; i < priv->tx_ring_num; i++) {
618 cq = &priv->tx_cq[i];
619 err = mlx4_en_activate_cq(priv, cq);
621 mlx4_err(mdev, "Failed allocating Tx CQ\n");
624 err = mlx4_en_set_cq_moder(priv, cq);
626 mlx4_err(mdev, "Failed setting cq moderation parameters");
627 mlx4_en_deactivate_cq(priv, cq);
630 mlx4_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
631 cq->buf->wqe_index = cpu_to_be16(0xffff);
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);
638 mlx4_err(mdev, "Failed allocating Tx ring\n");
639 mlx4_en_deactivate_cq(priv, cq);
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;
649 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
650 priv->rx_skb_size + ETH_FCS_LEN,
651 priv->prof->tx_pause,
653 priv->prof->rx_pause,
656 mlx4_err(mdev, "Failed setting port general configurations"
657 " for port %d, with error %d\n", priv->port, err);
660 /* Set default qp number */
661 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
663 mlx4_err(mdev, "Failed setting default qp numbers\n");
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);
671 mlx4_err(mdev, "Failed setting port mac\n");
676 mlx4_dbg(HW, priv, "Initializing port\n");
677 err = mlx4_INIT_PORT(mdev->dev, priv->port);
679 mlx4_err(mdev, "Failed Initializing port\n");
683 /* Schedule multicast task to populate multicast list */
684 queue_work(mdev->workqueue, &priv->mcast_task);
686 priv->port_up = true;
687 netif_start_queue(dev);
691 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
694 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
695 mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
698 mlx4_en_release_rss_steer(priv);
700 for (i = 0; i < priv->rx_ring_num; i++)
701 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
704 mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
706 return err; /* need to close devices */
710 void mlx4_en_stop_port(struct net_device *dev)
712 struct mlx4_en_priv *priv = netdev_priv(dev);
713 struct mlx4_en_dev *mdev = priv->mdev;
716 if (!priv->port_up) {
717 mlx4_dbg(DRV, priv, "stop port (%d) called while port already down\n",
721 netif_stop_queue(dev);
723 /* Synchronize with tx routine */
724 netif_tx_lock_bh(dev);
725 priv->port_up = false;
726 netif_tx_unlock_bh(dev);
729 mlx4_CLOSE_PORT(mdev->dev, priv->port);
731 /* Unregister Mac address for the port */
732 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
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]);
741 for (i = 0; i < priv->tx_ring_num; i++)
742 mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
745 mlx4_en_release_rss_steer(priv);
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))
752 mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
756 static void mlx4_en_restart(struct work_struct *work)
758 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
760 struct mlx4_en_dev *mdev = priv->mdev;
761 struct net_device *dev = priv->dev;
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);
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;
777 mutex_lock(&mdev->state_lock);
779 if (!mdev->device_up) {
780 mlx4_err(mdev, "Cannot open - device down/disabled\n");
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);
804 mlx4_err(mdev, "Failed starting port:%d\n", priv->port);
807 mutex_unlock(&mdev->state_lock);
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);
829 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
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;
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))
860 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
861 prof->tx_ring_size, TXBB_SIZE))
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))
871 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
872 prof->rx_ring_size, priv->stride))
879 mlx4_err(mdev, "Failed to allocate NIC resources\n");
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);
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);
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;
918 mlx4_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
921 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
922 mlx4_err(mdev, "Bad MTU size:%d.\n", 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
932 mlx4_dbg(DRV, priv, "Change MTU called with card down!?\n");
934 mlx4_en_stop_port(dev);
935 mlx4_en_set_default_moderation(priv);
936 err = mlx4_en_start_port(dev);
938 mlx4_err(mdev, "Failed restarting port:%d\n",
940 queue_work(mdev->workqueue, &priv->watchdog_task);
943 mutex_unlock(&mdev->state_lock);
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,
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;
974 dev = alloc_etherdev(sizeof(struct mlx4_en_priv));
976 mlx4_err(mdev, "Net device allocation failed\n");
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));
992 priv->port_up = false;
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);
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);
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);
1031 mlx4_err(mdev, "Failed to allocate page for rx qps\n");
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_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;
1069 mdev->pndev[port] = dev;
1071 netif_carrier_off(dev);
1072 err = register_netdev(dev);
1074 mlx4_err(mdev, "Netdev registration failed\n");
1077 priv->registered = 1;
1078 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1082 mlx4_en_destroy_netdev(dev);