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>
38 #include <linux/slab.h>
40 #include <linux/mlx4/driver.h>
41 #include <linux/mlx4/device.h>
42 #include <linux/mlx4/cmd.h>
43 #include <linux/mlx4/cq.h>
48 static int mlx4_en_setup_tc(struct net_device *dev, u8 up)
50 if (up != MLX4_EN_NUM_UP)
56 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
58 struct mlx4_en_priv *priv = netdev_priv(dev);
59 struct mlx4_en_dev *mdev = priv->mdev;
63 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
65 set_bit(vid, priv->active_vlans);
67 /* Add VID to port VLAN filter */
68 mutex_lock(&mdev->state_lock);
69 if (mdev->device_up && priv->port_up) {
70 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
72 en_err(priv, "Failed configuring VLAN filter\n");
74 if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
75 en_err(priv, "failed adding vlan %d\n", vid);
76 mutex_unlock(&mdev->state_lock);
81 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
83 struct mlx4_en_priv *priv = netdev_priv(dev);
84 struct mlx4_en_dev *mdev = priv->mdev;
88 en_dbg(HW, priv, "Killing VID:%d\n", vid);
90 clear_bit(vid, priv->active_vlans);
92 /* Remove VID from port VLAN filter */
93 mutex_lock(&mdev->state_lock);
94 if (!mlx4_find_cached_vlan(mdev->dev, priv->port, vid, &idx))
95 mlx4_unregister_vlan(mdev->dev, priv->port, idx);
97 en_err(priv, "could not find vid %d in cache\n", vid);
99 if (mdev->device_up && priv->port_up) {
100 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
102 en_err(priv, "Failed configuring VLAN filter\n");
104 mutex_unlock(&mdev->state_lock);
109 u64 mlx4_en_mac_to_u64(u8 *addr)
114 for (i = 0; i < ETH_ALEN; i++) {
121 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
123 struct mlx4_en_priv *priv = netdev_priv(dev);
124 struct mlx4_en_dev *mdev = priv->mdev;
125 struct sockaddr *saddr = addr;
127 if (!is_valid_ether_addr(saddr->sa_data))
128 return -EADDRNOTAVAIL;
130 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
131 priv->mac = mlx4_en_mac_to_u64(dev->dev_addr);
132 queue_work(mdev->workqueue, &priv->mac_task);
136 static void mlx4_en_do_set_mac(struct work_struct *work)
138 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
140 struct mlx4_en_dev *mdev = priv->mdev;
143 mutex_lock(&mdev->state_lock);
145 /* Remove old MAC and insert the new one */
146 err = mlx4_replace_mac(mdev->dev, priv->port,
147 priv->base_qpn, priv->mac);
149 en_err(priv, "Failed changing HW MAC address\n");
151 en_dbg(HW, priv, "Port is down while "
152 "registering mac, exiting...\n");
154 mutex_unlock(&mdev->state_lock);
157 static void mlx4_en_clear_list(struct net_device *dev)
159 struct mlx4_en_priv *priv = netdev_priv(dev);
161 kfree(priv->mc_addrs);
162 priv->mc_addrs = NULL;
163 priv->mc_addrs_cnt = 0;
166 static void mlx4_en_cache_mclist(struct net_device *dev)
168 struct mlx4_en_priv *priv = netdev_priv(dev);
169 struct netdev_hw_addr *ha;
171 int mc_addrs_cnt = netdev_mc_count(dev);
174 mc_addrs = kmalloc(mc_addrs_cnt * ETH_ALEN, GFP_ATOMIC);
176 en_err(priv, "failed to allocate multicast list\n");
180 netdev_for_each_mc_addr(ha, dev)
181 memcpy(mc_addrs + i++ * ETH_ALEN, ha->addr, ETH_ALEN);
182 mlx4_en_clear_list(dev);
183 priv->mc_addrs = mc_addrs;
184 priv->mc_addrs_cnt = mc_addrs_cnt;
188 static void mlx4_en_set_multicast(struct net_device *dev)
190 struct mlx4_en_priv *priv = netdev_priv(dev);
195 queue_work(priv->mdev->workqueue, &priv->mcast_task);
198 static void mlx4_en_do_set_multicast(struct work_struct *work)
200 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
202 struct mlx4_en_dev *mdev = priv->mdev;
203 struct net_device *dev = priv->dev;
205 u8 mc_list[16] = {0};
208 mutex_lock(&mdev->state_lock);
209 if (!mdev->device_up) {
210 en_dbg(HW, priv, "Card is not up, "
211 "ignoring multicast change.\n");
214 if (!priv->port_up) {
215 en_dbg(HW, priv, "Port is down, "
216 "ignoring multicast change.\n");
220 if (!netif_carrier_ok(dev)) {
221 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
222 if (priv->port_state.link_state) {
223 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
224 netif_carrier_on(dev);
225 en_dbg(LINK, priv, "Link Up\n");
231 * Promsicuous mode: disable all filters
234 if (dev->flags & IFF_PROMISC) {
235 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
236 if (netif_msg_rx_status(priv))
237 en_warn(priv, "Entering promiscuous mode\n");
238 priv->flags |= MLX4_EN_FLAG_PROMISC;
240 /* Enable promiscouos mode */
241 if (!(mdev->dev->caps.flags &
242 MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
243 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
246 err = mlx4_unicast_promisc_add(mdev->dev, priv->base_qpn,
249 en_err(priv, "Failed enabling "
250 "promiscuous mode\n");
252 /* Disable port multicast filter (unconditionally) */
253 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
254 0, MLX4_MCAST_DISABLE);
256 en_err(priv, "Failed disabling "
257 "multicast filter\n");
259 /* Add the default qp number as multicast promisc */
260 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
261 err = mlx4_multicast_promisc_add(mdev->dev, priv->base_qpn,
264 en_err(priv, "Failed entering multicast promisc mode\n");
265 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
268 /* Disable port VLAN filter */
269 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
271 en_err(priv, "Failed disabling VLAN filter\n");
277 * Not in promiscuous mode
280 if (priv->flags & MLX4_EN_FLAG_PROMISC) {
281 if (netif_msg_rx_status(priv))
282 en_warn(priv, "Leaving promiscuous mode\n");
283 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
285 /* Disable promiscouos mode */
286 if (!(mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
287 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
290 err = mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
293 en_err(priv, "Failed disabling promiscuous mode\n");
295 /* Disable Multicast promisc */
296 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
297 err = mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
300 en_err(priv, "Failed disabling multicast promiscuous mode\n");
301 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
304 /* Enable port VLAN filter */
305 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
307 en_err(priv, "Failed enabling VLAN filter\n");
310 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
311 if (dev->flags & IFF_ALLMULTI) {
312 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
313 0, MLX4_MCAST_DISABLE);
315 en_err(priv, "Failed disabling multicast filter\n");
317 /* Add the default qp number as multicast promisc */
318 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
319 err = mlx4_multicast_promisc_add(mdev->dev, priv->base_qpn,
322 en_err(priv, "Failed entering multicast promisc mode\n");
323 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
327 /* Disable Multicast promisc */
328 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
329 err = mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
332 en_err(priv, "Failed disabling multicast promiscuous mode\n");
333 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
336 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
337 0, MLX4_MCAST_DISABLE);
339 en_err(priv, "Failed disabling multicast filter\n");
341 /* Detach our qp from all the multicast addresses */
342 for (i = 0; i < priv->mc_addrs_cnt; i++) {
343 memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
344 mc_list[5] = priv->port;
345 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
346 mc_list, MLX4_PROT_ETH);
348 /* Flush mcast filter and init it with broadcast address */
349 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
350 1, MLX4_MCAST_CONFIG);
352 /* Update multicast list - we cache all addresses so they won't
353 * change while HW is updated holding the command semaphor */
354 netif_tx_lock_bh(dev);
355 mlx4_en_cache_mclist(dev);
356 netif_tx_unlock_bh(dev);
357 for (i = 0; i < priv->mc_addrs_cnt; i++) {
359 mlx4_en_mac_to_u64(priv->mc_addrs + i * ETH_ALEN);
360 memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
361 mc_list[5] = priv->port;
362 mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp,
363 mc_list, 0, MLX4_PROT_ETH);
364 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
365 mcast_addr, 0, MLX4_MCAST_CONFIG);
367 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
368 0, MLX4_MCAST_ENABLE);
370 en_err(priv, "Failed enabling multicast filter\n");
373 mutex_unlock(&mdev->state_lock);
376 #ifdef CONFIG_NET_POLL_CONTROLLER
377 static void mlx4_en_netpoll(struct net_device *dev)
379 struct mlx4_en_priv *priv = netdev_priv(dev);
380 struct mlx4_en_cq *cq;
384 for (i = 0; i < priv->rx_ring_num; i++) {
385 cq = &priv->rx_cq[i];
386 spin_lock_irqsave(&cq->lock, flags);
387 napi_synchronize(&cq->napi);
388 mlx4_en_process_rx_cq(dev, cq, 0);
389 spin_unlock_irqrestore(&cq->lock, flags);
394 static void mlx4_en_tx_timeout(struct net_device *dev)
396 struct mlx4_en_priv *priv = netdev_priv(dev);
397 struct mlx4_en_dev *mdev = priv->mdev;
399 if (netif_msg_timer(priv))
400 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
402 priv->port_stats.tx_timeout++;
403 en_dbg(DRV, priv, "Scheduling watchdog\n");
404 queue_work(mdev->workqueue, &priv->watchdog_task);
408 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
410 struct mlx4_en_priv *priv = netdev_priv(dev);
412 spin_lock_bh(&priv->stats_lock);
413 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
414 spin_unlock_bh(&priv->stats_lock);
416 return &priv->ret_stats;
419 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
421 struct mlx4_en_cq *cq;
424 /* If we haven't received a specific coalescing setting
425 * (module param), we set the moderation parameters as follows:
426 * - moder_cnt is set to the number of mtu sized packets to
427 * satisfy our coelsing target.
428 * - moder_time is set to a fixed value.
430 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
431 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
432 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
433 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
434 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
435 "rx_frames:%d rx_usecs:%d\n",
436 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
438 /* Setup cq moderation params */
439 for (i = 0; i < priv->rx_ring_num; i++) {
440 cq = &priv->rx_cq[i];
441 cq->moder_cnt = priv->rx_frames;
442 cq->moder_time = priv->rx_usecs;
443 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
444 priv->last_moder_packets[i] = 0;
445 priv->last_moder_bytes[i] = 0;
448 for (i = 0; i < priv->tx_ring_num; i++) {
449 cq = &priv->tx_cq[i];
450 cq->moder_cnt = priv->tx_frames;
451 cq->moder_time = priv->tx_usecs;
454 /* Reset auto-moderation params */
455 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
456 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
457 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
458 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
459 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
460 priv->adaptive_rx_coal = 1;
461 priv->last_moder_jiffies = 0;
462 priv->last_moder_tx_packets = 0;
465 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
467 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
468 struct mlx4_en_cq *cq;
469 unsigned long packets;
471 unsigned long avg_pkt_size;
472 unsigned long rx_packets;
473 unsigned long rx_bytes;
474 unsigned long rx_pkt_diff;
478 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
481 for (ring = 0; ring < priv->rx_ring_num; ring++) {
482 spin_lock_bh(&priv->stats_lock);
483 rx_packets = priv->rx_ring[ring].packets;
484 rx_bytes = priv->rx_ring[ring].bytes;
485 spin_unlock_bh(&priv->stats_lock);
487 rx_pkt_diff = ((unsigned long) (rx_packets -
488 priv->last_moder_packets[ring]));
489 packets = rx_pkt_diff;
490 rate = packets * HZ / period;
491 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
492 priv->last_moder_bytes[ring])) / packets : 0;
494 /* Apply auto-moderation only when packet rate
495 * exceeds a rate that it matters */
496 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
497 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
498 if (rate < priv->pkt_rate_low)
499 moder_time = priv->rx_usecs_low;
500 else if (rate > priv->pkt_rate_high)
501 moder_time = priv->rx_usecs_high;
503 moder_time = (rate - priv->pkt_rate_low) *
504 (priv->rx_usecs_high - priv->rx_usecs_low) /
505 (priv->pkt_rate_high - priv->pkt_rate_low) +
508 moder_time = priv->rx_usecs_low;
511 if (moder_time != priv->last_moder_time[ring]) {
512 priv->last_moder_time[ring] = moder_time;
513 cq = &priv->rx_cq[ring];
514 cq->moder_time = moder_time;
515 err = mlx4_en_set_cq_moder(priv, cq);
517 en_err(priv, "Failed modifying moderation "
518 "for cq:%d\n", ring);
520 priv->last_moder_packets[ring] = rx_packets;
521 priv->last_moder_bytes[ring] = rx_bytes;
524 priv->last_moder_jiffies = jiffies;
527 static void mlx4_en_do_get_stats(struct work_struct *work)
529 struct delayed_work *delay = to_delayed_work(work);
530 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
532 struct mlx4_en_dev *mdev = priv->mdev;
535 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
537 en_dbg(HW, priv, "Could not update stats\n");
539 mutex_lock(&mdev->state_lock);
540 if (mdev->device_up) {
542 mlx4_en_auto_moderation(priv);
544 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
546 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
547 queue_work(mdev->workqueue, &priv->mac_task);
548 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
550 mutex_unlock(&mdev->state_lock);
553 static void mlx4_en_linkstate(struct work_struct *work)
555 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
557 struct mlx4_en_dev *mdev = priv->mdev;
558 int linkstate = priv->link_state;
560 mutex_lock(&mdev->state_lock);
561 /* If observable port state changed set carrier state and
562 * report to system log */
563 if (priv->last_link_state != linkstate) {
564 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
565 en_info(priv, "Link Down\n");
566 netif_carrier_off(priv->dev);
568 en_info(priv, "Link Up\n");
569 netif_carrier_on(priv->dev);
572 priv->last_link_state = linkstate;
573 mutex_unlock(&mdev->state_lock);
577 int mlx4_en_start_port(struct net_device *dev)
579 struct mlx4_en_priv *priv = netdev_priv(dev);
580 struct mlx4_en_dev *mdev = priv->mdev;
581 struct mlx4_en_cq *cq;
582 struct mlx4_en_tx_ring *tx_ring;
588 u8 mc_list[16] = {0};
591 en_dbg(DRV, priv, "start port called while port already up\n");
595 /* Calculate Rx buf size */
596 dev->mtu = min(dev->mtu, priv->max_mtu);
597 mlx4_en_calc_rx_buf(dev);
598 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
600 /* Configure rx cq's and rings */
601 err = mlx4_en_activate_rx_rings(priv);
603 en_err(priv, "Failed to activate RX rings\n");
606 for (i = 0; i < priv->rx_ring_num; i++) {
607 cq = &priv->rx_cq[i];
609 err = mlx4_en_activate_cq(priv, cq, i);
611 en_err(priv, "Failed activating Rx CQ\n");
614 for (j = 0; j < cq->size; j++)
615 cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
616 err = mlx4_en_set_cq_moder(priv, cq);
618 en_err(priv, "Failed setting cq moderation parameters");
619 mlx4_en_deactivate_cq(priv, cq);
622 mlx4_en_arm_cq(priv, cq);
623 priv->rx_ring[i].cqn = cq->mcq.cqn;
628 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
629 err = mlx4_get_eth_qp(mdev->dev, priv->port,
630 priv->mac, &priv->base_qpn);
632 en_err(priv, "Failed getting eth qp\n");
635 mdev->mac_removed[priv->port] = 0;
637 err = mlx4_en_config_rss_steer(priv);
639 en_err(priv, "Failed configuring rss steering\n");
643 /* Configure tx cq's and rings */
644 for (i = 0; i < priv->tx_ring_num; i++) {
646 cq = &priv->tx_cq[i];
647 err = mlx4_en_activate_cq(priv, cq, i);
649 en_err(priv, "Failed allocating Tx CQ\n");
652 err = mlx4_en_set_cq_moder(priv, cq);
654 en_err(priv, "Failed setting cq moderation parameters");
655 mlx4_en_deactivate_cq(priv, cq);
658 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
659 cq->buf->wqe_index = cpu_to_be16(0xffff);
662 tx_ring = &priv->tx_ring[i];
663 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
664 max(0, i - MLX4_EN_NUM_TX_RINGS));
666 en_err(priv, "Failed allocating Tx ring\n");
667 mlx4_en_deactivate_cq(priv, cq);
670 /* Set initial ownership of all Tx TXBBs to SW (1) */
671 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
672 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
677 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
678 priv->rx_skb_size + ETH_FCS_LEN,
679 priv->prof->tx_pause,
681 priv->prof->rx_pause,
684 en_err(priv, "Failed setting port general configurations "
685 "for port %d, with error %d\n", priv->port, err);
688 /* Set default qp number */
689 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
691 en_err(priv, "Failed setting default qp numbers\n");
696 en_dbg(HW, priv, "Initializing port\n");
697 err = mlx4_INIT_PORT(mdev->dev, priv->port);
699 en_err(priv, "Failed Initializing port\n");
703 /* Attach rx QP to bradcast address */
704 memset(&mc_list[10], 0xff, ETH_ALEN);
705 mc_list[5] = priv->port;
706 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
708 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
710 /* Must redo promiscuous mode setup. */
711 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
713 /* Schedule multicast task to populate multicast list */
714 queue_work(mdev->workqueue, &priv->mcast_task);
716 mlx4_set_stats_bitmap(mdev->dev, &priv->stats_bitmap);
718 priv->port_up = true;
719 netif_tx_start_all_queues(dev);
724 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
725 mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
728 mlx4_en_release_rss_steer(priv);
730 mlx4_put_eth_qp(mdev->dev, priv->port, priv->mac, priv->base_qpn);
733 mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
734 for (i = 0; i < priv->rx_ring_num; i++)
735 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
737 return err; /* need to close devices */
741 void mlx4_en_stop_port(struct net_device *dev)
743 struct mlx4_en_priv *priv = netdev_priv(dev);
744 struct mlx4_en_dev *mdev = priv->mdev;
746 u8 mc_list[16] = {0};
748 if (!priv->port_up) {
749 en_dbg(DRV, priv, "stop port called while port already down\n");
753 /* Synchronize with tx routine */
754 netif_tx_lock_bh(dev);
755 netif_tx_stop_all_queues(dev);
756 netif_tx_unlock_bh(dev);
758 /* Set port as not active */
759 priv->port_up = false;
761 /* Detach All multicasts */
762 memset(&mc_list[10], 0xff, ETH_ALEN);
763 mc_list[5] = priv->port;
764 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
766 for (i = 0; i < priv->mc_addrs_cnt; i++) {
767 memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
768 mc_list[5] = priv->port;
769 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
770 mc_list, MLX4_PROT_ETH);
772 mlx4_en_clear_list(dev);
773 /* Flush multicast filter */
774 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
777 for (i = 0; i < priv->tx_ring_num; i++) {
778 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
779 mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
783 for (i = 0; i < priv->tx_ring_num; i++)
784 mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
787 mlx4_en_release_rss_steer(priv);
789 /* Unregister Mac address for the port */
790 mlx4_put_eth_qp(mdev->dev, priv->port, priv->mac, priv->base_qpn);
791 mdev->mac_removed[priv->port] = 1;
794 for (i = 0; i < priv->rx_ring_num; i++) {
795 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
796 while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
798 mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
802 mlx4_CLOSE_PORT(mdev->dev, priv->port);
805 static void mlx4_en_restart(struct work_struct *work)
807 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
809 struct mlx4_en_dev *mdev = priv->mdev;
810 struct net_device *dev = priv->dev;
812 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
814 mutex_lock(&mdev->state_lock);
816 mlx4_en_stop_port(dev);
817 if (mlx4_en_start_port(dev))
818 en_err(priv, "Failed restarting port %d\n", priv->port);
820 mutex_unlock(&mdev->state_lock);
823 static void mlx4_en_clear_stats(struct net_device *dev)
825 struct mlx4_en_priv *priv = netdev_priv(dev);
826 struct mlx4_en_dev *mdev = priv->mdev;
829 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
830 en_dbg(HW, priv, "Failed dumping statistics\n");
832 memset(&priv->stats, 0, sizeof(priv->stats));
833 memset(&priv->pstats, 0, sizeof(priv->pstats));
834 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
835 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
837 for (i = 0; i < priv->tx_ring_num; i++) {
838 priv->tx_ring[i].bytes = 0;
839 priv->tx_ring[i].packets = 0;
840 priv->tx_ring[i].tx_csum = 0;
842 for (i = 0; i < priv->rx_ring_num; i++) {
843 priv->rx_ring[i].bytes = 0;
844 priv->rx_ring[i].packets = 0;
845 priv->rx_ring[i].csum_ok = 0;
846 priv->rx_ring[i].csum_none = 0;
850 static int mlx4_en_open(struct net_device *dev)
852 struct mlx4_en_priv *priv = netdev_priv(dev);
853 struct mlx4_en_dev *mdev = priv->mdev;
856 mutex_lock(&mdev->state_lock);
858 if (!mdev->device_up) {
859 en_err(priv, "Cannot open - device down/disabled\n");
864 /* Reset HW statistics and SW counters */
865 mlx4_en_clear_stats(dev);
867 err = mlx4_en_start_port(dev);
869 en_err(priv, "Failed starting port:%d\n", priv->port);
872 mutex_unlock(&mdev->state_lock);
877 static int mlx4_en_close(struct net_device *dev)
879 struct mlx4_en_priv *priv = netdev_priv(dev);
880 struct mlx4_en_dev *mdev = priv->mdev;
882 en_dbg(IFDOWN, priv, "Close port called\n");
884 mutex_lock(&mdev->state_lock);
886 mlx4_en_stop_port(dev);
887 netif_carrier_off(dev);
889 mutex_unlock(&mdev->state_lock);
893 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
897 for (i = 0; i < priv->tx_ring_num; i++) {
898 if (priv->tx_ring[i].tx_info)
899 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
900 if (priv->tx_cq[i].buf)
901 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
904 for (i = 0; i < priv->rx_ring_num; i++) {
905 if (priv->rx_ring[i].rx_info)
906 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
907 priv->prof->rx_ring_size, priv->stride);
908 if (priv->rx_cq[i].buf)
909 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
913 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
915 struct mlx4_en_port_profile *prof = priv->prof;
917 int base_tx_qpn, err;
919 err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &base_tx_qpn);
921 en_err(priv, "failed reserving range for TX rings\n");
925 /* Create tx Rings */
926 for (i = 0; i < priv->tx_ring_num; i++) {
927 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
928 prof->tx_ring_size, i, TX))
931 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i], base_tx_qpn + i,
932 prof->tx_ring_size, TXBB_SIZE))
936 /* Create rx Rings */
937 for (i = 0; i < priv->rx_ring_num; i++) {
938 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
939 prof->rx_ring_size, i, RX))
942 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
943 prof->rx_ring_size, priv->stride))
950 en_err(priv, "Failed to allocate NIC resources\n");
951 mlx4_qp_release_range(priv->mdev->dev, base_tx_qpn, priv->tx_ring_num);
956 void mlx4_en_destroy_netdev(struct net_device *dev)
958 struct mlx4_en_priv *priv = netdev_priv(dev);
959 struct mlx4_en_dev *mdev = priv->mdev;
961 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
963 /* Unregister device - this will close the port if it was up */
964 if (priv->registered)
965 unregister_netdev(dev);
968 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
970 cancel_delayed_work(&priv->stats_task);
971 /* flush any pending task for this netdev */
972 flush_workqueue(mdev->workqueue);
974 /* Detach the netdev so tasks would not attempt to access it */
975 mutex_lock(&mdev->state_lock);
976 mdev->pndev[priv->port] = NULL;
977 mutex_unlock(&mdev->state_lock);
979 mlx4_en_free_resources(priv);
984 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
986 struct mlx4_en_priv *priv = netdev_priv(dev);
987 struct mlx4_en_dev *mdev = priv->mdev;
990 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
993 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
994 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
999 if (netif_running(dev)) {
1000 mutex_lock(&mdev->state_lock);
1001 if (!mdev->device_up) {
1002 /* NIC is probably restarting - let watchdog task reset
1004 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
1006 mlx4_en_stop_port(dev);
1007 err = mlx4_en_start_port(dev);
1009 en_err(priv, "Failed restarting port:%d\n",
1011 queue_work(mdev->workqueue, &priv->watchdog_task);
1014 mutex_unlock(&mdev->state_lock);
1019 static int mlx4_en_set_features(struct net_device *netdev,
1020 netdev_features_t features)
1022 struct mlx4_en_priv *priv = netdev_priv(netdev);
1024 if (features & NETIF_F_LOOPBACK)
1025 priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
1028 cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
1034 static const struct net_device_ops mlx4_netdev_ops = {
1035 .ndo_open = mlx4_en_open,
1036 .ndo_stop = mlx4_en_close,
1037 .ndo_start_xmit = mlx4_en_xmit,
1038 .ndo_select_queue = mlx4_en_select_queue,
1039 .ndo_get_stats = mlx4_en_get_stats,
1040 .ndo_set_rx_mode = mlx4_en_set_multicast,
1041 .ndo_set_mac_address = mlx4_en_set_mac,
1042 .ndo_validate_addr = eth_validate_addr,
1043 .ndo_change_mtu = mlx4_en_change_mtu,
1044 .ndo_tx_timeout = mlx4_en_tx_timeout,
1045 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
1046 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
1047 #ifdef CONFIG_NET_POLL_CONTROLLER
1048 .ndo_poll_controller = mlx4_en_netpoll,
1050 .ndo_set_features = mlx4_en_set_features,
1051 .ndo_setup_tc = mlx4_en_setup_tc,
1054 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
1055 struct mlx4_en_port_profile *prof)
1057 struct net_device *dev;
1058 struct mlx4_en_priv *priv;
1062 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
1063 prof->tx_ring_num, prof->rx_ring_num);
1067 SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
1068 dev->dev_id = port - 1;
1071 * Initialize driver private data
1074 priv = netdev_priv(dev);
1075 memset(priv, 0, sizeof(struct mlx4_en_priv));
1078 priv->ddev = &mdev->pdev->dev;
1081 priv->port_up = false;
1082 priv->flags = prof->flags;
1083 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
1084 MLX4_WQE_CTRL_SOLICITED);
1085 priv->tx_ring_num = prof->tx_ring_num;
1086 priv->rx_ring_num = prof->rx_ring_num;
1087 priv->mac_index = -1;
1088 priv->msg_enable = MLX4_EN_MSG_LEVEL;
1089 spin_lock_init(&priv->stats_lock);
1090 INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
1091 INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
1092 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
1093 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
1094 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
1095 #ifdef CONFIG_MLX4_EN_DCB
1096 if (!mlx4_is_slave(priv->mdev->dev))
1097 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
1100 /* Query for default mac and max mtu */
1101 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
1102 priv->mac = mdev->dev->caps.def_mac[priv->port];
1103 if (ILLEGAL_MAC(priv->mac)) {
1104 en_err(priv, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
1105 priv->port, priv->mac);
1110 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
1111 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
1112 err = mlx4_en_alloc_resources(priv);
1116 /* Allocate page for receive rings */
1117 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
1118 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
1120 en_err(priv, "Failed to allocate page for rx qps\n");
1123 priv->allocated = 1;
1126 * Initialize netdev entry points
1128 dev->netdev_ops = &mlx4_netdev_ops;
1129 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
1130 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
1131 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
1133 netdev_set_num_tc(dev, MLX4_EN_NUM_UP);
1135 /* First 9 rings are for UP 0 */
1136 netdev_set_tc_queue(dev, 0, MLX4_EN_NUM_TX_RINGS + 1, 0);
1138 /* Partition Tx queues evenly amongst UP's 1-7 */
1139 for (i = 1; i < MLX4_EN_NUM_UP; i++)
1140 netdev_set_tc_queue(dev, i, 1, MLX4_EN_NUM_TX_RINGS + i);
1142 SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
1144 /* Set defualt MAC */
1145 dev->addr_len = ETH_ALEN;
1146 for (i = 0; i < ETH_ALEN; i++) {
1147 dev->dev_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
1148 dev->perm_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
1152 * Set driver features
1154 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1155 if (mdev->LSO_support)
1156 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1158 dev->vlan_features = dev->hw_features;
1160 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
1161 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
1162 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
1163 NETIF_F_HW_VLAN_FILTER;
1164 dev->hw_features |= NETIF_F_LOOPBACK;
1166 mdev->pndev[port] = dev;
1168 netif_carrier_off(dev);
1169 err = register_netdev(dev);
1171 en_err(priv, "Netdev registration failed for port %d\n", port);
1174 priv->registered = 1;
1176 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
1177 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
1179 /* Configure port */
1180 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1184 en_err(priv, "Failed setting port general configurations "
1185 "for port %d, with error %d\n", priv->port, err);
1190 en_warn(priv, "Initializing port\n");
1191 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1193 en_err(priv, "Failed Initializing port\n");
1196 mlx4_en_set_default_moderation(priv);
1197 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1201 mlx4_en_destroy_netdev(dev);