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>
49 static void mlx4_en_vlan_rx_register(struct net_device
*dev
, struct vlan_group
*grp
)
51 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
52 struct mlx4_en_dev
*mdev
= priv
->mdev
;
55 en_dbg(HW
, priv
, "Registering VLAN group:%p\n", grp
);
58 mutex_lock(&mdev
->state_lock
);
59 if (mdev
->device_up
&& priv
->port_up
) {
60 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
->port
, grp
);
62 en_err(priv
, "Failed configuring VLAN filter\n");
64 mutex_unlock(&mdev
->state_lock
);
67 static void mlx4_en_vlan_rx_add_vid(struct net_device
*dev
, unsigned short vid
)
69 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
70 struct mlx4_en_dev
*mdev
= priv
->mdev
;
77 en_dbg(HW
, priv
, "adding VLAN:%d (vlgrp entry:%p)\n",
78 vid
, vlan_group_get_device(priv
->vlgrp
, vid
));
80 /* Add VID to port VLAN filter */
81 mutex_lock(&mdev
->state_lock
);
82 if (mdev
->device_up
&& priv
->port_up
) {
83 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
->port
, priv
->vlgrp
);
85 en_err(priv
, "Failed configuring VLAN filter\n");
87 if (mlx4_register_vlan(mdev
->dev
, priv
->port
, vid
, &idx
))
88 en_err(priv
, "failed adding vlan %d\n", vid
);
89 mutex_unlock(&mdev
->state_lock
);
93 static void mlx4_en_vlan_rx_kill_vid(struct net_device
*dev
, unsigned short vid
)
95 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
96 struct mlx4_en_dev
*mdev
= priv
->mdev
;
103 en_dbg(HW
, priv
, "Killing VID:%d (vlgrp:%p vlgrp entry:%p)\n",
104 vid
, priv
->vlgrp
, vlan_group_get_device(priv
->vlgrp
, vid
));
105 vlan_group_set_device(priv
->vlgrp
, vid
, NULL
);
107 /* Remove VID from port VLAN filter */
108 mutex_lock(&mdev
->state_lock
);
109 if (!mlx4_find_cached_vlan(mdev
->dev
, priv
->port
, vid
, &idx
))
110 mlx4_unregister_vlan(mdev
->dev
, priv
->port
, idx
);
112 en_err(priv
, "could not find vid %d in cache\n", vid
);
114 if (mdev
->device_up
&& priv
->port_up
) {
115 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
->port
, priv
->vlgrp
);
117 en_err(priv
, "Failed configuring VLAN filter\n");
119 mutex_unlock(&mdev
->state_lock
);
122 u64
mlx4_en_mac_to_u64(u8
*addr
)
127 for (i
= 0; i
< ETH_ALEN
; i
++) {
134 static int mlx4_en_set_mac(struct net_device
*dev
, void *addr
)
136 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
137 struct mlx4_en_dev
*mdev
= priv
->mdev
;
138 struct sockaddr
*saddr
= addr
;
140 if (!is_valid_ether_addr(saddr
->sa_data
))
141 return -EADDRNOTAVAIL
;
143 memcpy(dev
->dev_addr
, saddr
->sa_data
, ETH_ALEN
);
144 priv
->mac
= mlx4_en_mac_to_u64(dev
->dev_addr
);
145 queue_work(mdev
->workqueue
, &priv
->mac_task
);
149 static void mlx4_en_do_set_mac(struct work_struct
*work
)
151 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
153 struct mlx4_en_dev
*mdev
= priv
->mdev
;
156 mutex_lock(&mdev
->state_lock
);
158 /* Remove old MAC and insert the new one */
159 mlx4_unregister_mac(mdev
->dev
, priv
->port
, priv
->mac_index
);
160 err
= mlx4_register_mac(mdev
->dev
, priv
->port
,
161 priv
->mac
, &priv
->mac_index
);
163 en_err(priv
, "Failed changing HW MAC address\n");
165 en_dbg(HW
, priv
, "Port is down while "
166 "registering mac, exiting...\n");
168 mutex_unlock(&mdev
->state_lock
);
171 static void mlx4_en_clear_list(struct net_device
*dev
)
173 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
175 kfree(priv
->mc_addrs
);
176 priv
->mc_addrs_cnt
= 0;
179 static void mlx4_en_cache_mclist(struct net_device
*dev
)
181 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
182 struct netdev_hw_addr
*ha
;
184 int mc_addrs_cnt
= netdev_mc_count(dev
);
187 mc_addrs
= kmalloc(mc_addrs_cnt
* ETH_ALEN
, GFP_ATOMIC
);
189 en_err(priv
, "failed to allocate multicast list\n");
193 netdev_for_each_mc_addr(ha
, dev
)
194 memcpy(mc_addrs
+ i
++ * ETH_ALEN
, ha
->addr
, ETH_ALEN
);
195 priv
->mc_addrs
= mc_addrs
;
196 priv
->mc_addrs_cnt
= mc_addrs_cnt
;
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
;
219 mutex_lock(&mdev
->state_lock
);
220 if (!mdev
->device_up
) {
221 en_dbg(HW
, priv
, "Card is not up, "
222 "ignoring multicast change.\n");
225 if (!priv
->port_up
) {
226 en_dbg(HW
, priv
, "Port is down, "
227 "ignoring multicast change.\n");
232 * Promsicuous mode: disable all filters
235 if (dev
->flags
& IFF_PROMISC
) {
236 if (!(priv
->flags
& MLX4_EN_FLAG_PROMISC
)) {
237 if (netif_msg_rx_status(priv
))
238 en_warn(priv
, "Entering promiscuous mode\n");
239 priv
->flags
|= MLX4_EN_FLAG_PROMISC
;
241 /* Enable promiscouos mode */
242 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
, priv
->port
,
245 en_err(priv
, "Failed enabling "
246 "promiscous mode\n");
248 /* Disable port multicast filter (unconditionally) */
249 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
250 0, MLX4_MCAST_DISABLE
);
252 en_err(priv
, "Failed disabling "
253 "multicast filter\n");
255 /* Disable port VLAN filter */
256 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
->port
, NULL
);
258 en_err(priv
, "Failed disabling VLAN filter\n");
264 * Not in promiscous mode
267 if (priv
->flags
& MLX4_EN_FLAG_PROMISC
) {
268 if (netif_msg_rx_status(priv
))
269 en_warn(priv
, "Leaving promiscuous mode\n");
270 priv
->flags
&= ~MLX4_EN_FLAG_PROMISC
;
272 /* Disable promiscouos mode */
273 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
, priv
->port
,
276 en_err(priv
, "Failed disabling promiscous mode\n");
278 /* Enable port VLAN filter */
279 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
->port
, priv
->vlgrp
);
281 en_err(priv
, "Failed enabling VLAN filter\n");
284 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
285 if (dev
->flags
& IFF_ALLMULTI
) {
286 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
287 0, MLX4_MCAST_DISABLE
);
289 en_err(priv
, "Failed disabling multicast filter\n");
293 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
294 0, MLX4_MCAST_DISABLE
);
296 en_err(priv
, "Failed disabling multicast filter\n");
298 /* Flush mcast filter and init it with broadcast address */
299 mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, ETH_BCAST
,
300 1, MLX4_MCAST_CONFIG
);
302 /* Update multicast list - we cache all addresses so they won't
303 * change while HW is updated holding the command semaphor */
304 netif_tx_lock_bh(dev
);
305 mlx4_en_cache_mclist(dev
);
306 netif_tx_unlock_bh(dev
);
307 for (i
= 0; i
< priv
->mc_addrs_cnt
; i
++) {
309 mlx4_en_mac_to_u64(priv
->mc_addrs
+ i
* ETH_ALEN
);
310 mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
,
311 mcast_addr
, 0, MLX4_MCAST_CONFIG
);
313 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
314 0, MLX4_MCAST_ENABLE
);
316 en_err(priv
, "Failed enabling multicast filter\n");
318 mlx4_en_clear_list(dev
);
321 mutex_unlock(&mdev
->state_lock
);
324 #ifdef CONFIG_NET_POLL_CONTROLLER
325 static void mlx4_en_netpoll(struct net_device
*dev
)
327 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
328 struct mlx4_en_cq
*cq
;
332 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
333 cq
= &priv
->rx_cq
[i
];
334 spin_lock_irqsave(&cq
->lock
, flags
);
335 napi_synchronize(&cq
->napi
);
336 mlx4_en_process_rx_cq(dev
, cq
, 0);
337 spin_unlock_irqrestore(&cq
->lock
, flags
);
342 static void mlx4_en_tx_timeout(struct net_device
*dev
)
344 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
345 struct mlx4_en_dev
*mdev
= priv
->mdev
;
347 if (netif_msg_timer(priv
))
348 en_warn(priv
, "Tx timeout called on port:%d\n", priv
->port
);
350 priv
->port_stats
.tx_timeout
++;
351 en_dbg(DRV
, priv
, "Scheduling watchdog\n");
352 queue_work(mdev
->workqueue
, &priv
->watchdog_task
);
356 static struct net_device_stats
*mlx4_en_get_stats(struct net_device
*dev
)
358 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
360 spin_lock_bh(&priv
->stats_lock
);
361 memcpy(&priv
->ret_stats
, &priv
->stats
, sizeof(priv
->stats
));
362 spin_unlock_bh(&priv
->stats_lock
);
364 return &priv
->ret_stats
;
367 static void mlx4_en_set_default_moderation(struct mlx4_en_priv
*priv
)
369 struct mlx4_en_cq
*cq
;
372 /* If we haven't received a specific coalescing setting
373 * (module param), we set the moderation parameters as follows:
374 * - moder_cnt is set to the number of mtu sized packets to
375 * satisfy our coelsing target.
376 * - moder_time is set to a fixed value.
378 priv
->rx_frames
= MLX4_EN_RX_COAL_TARGET
;
379 priv
->rx_usecs
= MLX4_EN_RX_COAL_TIME
;
380 en_dbg(INTR
, priv
, "Default coalesing params for mtu:%d - "
381 "rx_frames:%d rx_usecs:%d\n",
382 priv
->dev
->mtu
, priv
->rx_frames
, priv
->rx_usecs
);
384 /* Setup cq moderation params */
385 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
386 cq
= &priv
->rx_cq
[i
];
387 cq
->moder_cnt
= priv
->rx_frames
;
388 cq
->moder_time
= priv
->rx_usecs
;
391 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
392 cq
= &priv
->tx_cq
[i
];
393 cq
->moder_cnt
= MLX4_EN_TX_COAL_PKTS
;
394 cq
->moder_time
= MLX4_EN_TX_COAL_TIME
;
397 /* Reset auto-moderation params */
398 priv
->pkt_rate_low
= MLX4_EN_RX_RATE_LOW
;
399 priv
->rx_usecs_low
= MLX4_EN_RX_COAL_TIME_LOW
;
400 priv
->pkt_rate_high
= MLX4_EN_RX_RATE_HIGH
;
401 priv
->rx_usecs_high
= MLX4_EN_RX_COAL_TIME_HIGH
;
402 priv
->sample_interval
= MLX4_EN_SAMPLE_INTERVAL
;
403 priv
->adaptive_rx_coal
= 1;
404 priv
->last_moder_time
= MLX4_EN_AUTO_CONF
;
405 priv
->last_moder_jiffies
= 0;
406 priv
->last_moder_packets
= 0;
407 priv
->last_moder_tx_packets
= 0;
408 priv
->last_moder_bytes
= 0;
411 static void mlx4_en_auto_moderation(struct mlx4_en_priv
*priv
)
413 unsigned long period
= (unsigned long) (jiffies
- priv
->last_moder_jiffies
);
414 struct mlx4_en_cq
*cq
;
415 unsigned long packets
;
417 unsigned long avg_pkt_size
;
418 unsigned long rx_packets
;
419 unsigned long rx_bytes
;
420 unsigned long tx_packets
;
421 unsigned long tx_pkt_diff
;
422 unsigned long rx_pkt_diff
;
426 if (!priv
->adaptive_rx_coal
|| period
< priv
->sample_interval
* HZ
)
429 spin_lock_bh(&priv
->stats_lock
);
430 rx_packets
= priv
->stats
.rx_packets
;
431 rx_bytes
= priv
->stats
.rx_bytes
;
432 tx_packets
= priv
->stats
.tx_packets
;
433 spin_unlock_bh(&priv
->stats_lock
);
435 if (!priv
->last_moder_jiffies
|| !period
)
438 tx_pkt_diff
= ((unsigned long) (tx_packets
-
439 priv
->last_moder_tx_packets
));
440 rx_pkt_diff
= ((unsigned long) (rx_packets
-
441 priv
->last_moder_packets
));
442 packets
= max(tx_pkt_diff
, rx_pkt_diff
);
443 rate
= packets
* HZ
/ period
;
444 avg_pkt_size
= packets
? ((unsigned long) (rx_bytes
-
445 priv
->last_moder_bytes
)) / packets
: 0;
447 /* Apply auto-moderation only when packet rate exceeds a rate that
449 if (rate
> MLX4_EN_RX_RATE_THRESH
&& avg_pkt_size
> MLX4_EN_AVG_PKT_SMALL
) {
450 /* If tx and rx packet rates are not balanced, assume that
451 * traffic is mainly BW bound and apply maximum moderation.
452 * Otherwise, moderate according to packet rate */
453 if (2 * tx_pkt_diff
> 3 * rx_pkt_diff
||
454 2 * rx_pkt_diff
> 3 * tx_pkt_diff
) {
455 moder_time
= priv
->rx_usecs_high
;
457 if (rate
< priv
->pkt_rate_low
)
458 moder_time
= priv
->rx_usecs_low
;
459 else if (rate
> priv
->pkt_rate_high
)
460 moder_time
= priv
->rx_usecs_high
;
462 moder_time
= (rate
- priv
->pkt_rate_low
) *
463 (priv
->rx_usecs_high
- priv
->rx_usecs_low
) /
464 (priv
->pkt_rate_high
- priv
->pkt_rate_low
) +
468 moder_time
= priv
->rx_usecs_low
;
471 en_dbg(INTR
, priv
, "tx rate:%lu rx_rate:%lu\n",
472 tx_pkt_diff
* HZ
/ period
, rx_pkt_diff
* HZ
/ period
);
474 en_dbg(INTR
, priv
, "Rx moder_time changed from:%d to %d period:%lu "
475 "[jiff] packets:%lu avg_pkt_size:%lu rate:%lu [p/s])\n",
476 priv
->last_moder_time
, moder_time
, period
, packets
,
479 if (moder_time
!= priv
->last_moder_time
) {
480 priv
->last_moder_time
= moder_time
;
481 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
482 cq
= &priv
->rx_cq
[i
];
483 cq
->moder_time
= moder_time
;
484 err
= mlx4_en_set_cq_moder(priv
, cq
);
486 en_err(priv
, "Failed modifying moderation for cq:%d\n", i
);
493 priv
->last_moder_packets
= rx_packets
;
494 priv
->last_moder_tx_packets
= tx_packets
;
495 priv
->last_moder_bytes
= rx_bytes
;
496 priv
->last_moder_jiffies
= jiffies
;
499 static void mlx4_en_do_get_stats(struct work_struct
*work
)
501 struct delayed_work
*delay
= to_delayed_work(work
);
502 struct mlx4_en_priv
*priv
= container_of(delay
, struct mlx4_en_priv
,
504 struct mlx4_en_dev
*mdev
= priv
->mdev
;
507 err
= mlx4_en_DUMP_ETH_STATS(mdev
, priv
->port
, 0);
509 en_dbg(HW
, priv
, "Could not update stats\n");
511 mutex_lock(&mdev
->state_lock
);
512 if (mdev
->device_up
) {
514 mlx4_en_auto_moderation(priv
);
516 queue_delayed_work(mdev
->workqueue
, &priv
->stats_task
, STATS_DELAY
);
518 if (mdev
->mac_removed
[MLX4_MAX_PORTS
+ 1 - priv
->port
]) {
519 queue_work(mdev
->workqueue
, &priv
->mac_task
);
520 mdev
->mac_removed
[MLX4_MAX_PORTS
+ 1 - priv
->port
] = 0;
522 mutex_unlock(&mdev
->state_lock
);
525 static void mlx4_en_linkstate(struct work_struct
*work
)
527 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
529 struct mlx4_en_dev
*mdev
= priv
->mdev
;
530 int linkstate
= priv
->link_state
;
532 mutex_lock(&mdev
->state_lock
);
533 /* If observable port state changed set carrier state and
534 * report to system log */
535 if (priv
->last_link_state
!= linkstate
) {
536 if (linkstate
== MLX4_DEV_EVENT_PORT_DOWN
) {
537 en_info(priv
, "Link Down\n");
538 netif_carrier_off(priv
->dev
);
540 en_info(priv
, "Link Up\n");
541 netif_carrier_on(priv
->dev
);
544 priv
->last_link_state
= linkstate
;
545 mutex_unlock(&mdev
->state_lock
);
549 int mlx4_en_start_port(struct net_device
*dev
)
551 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
552 struct mlx4_en_dev
*mdev
= priv
->mdev
;
553 struct mlx4_en_cq
*cq
;
554 struct mlx4_en_tx_ring
*tx_ring
;
563 en_dbg(DRV
, priv
, "start port called while port already up\n");
567 /* Calculate Rx buf size */
568 dev
->mtu
= min(dev
->mtu
, priv
->max_mtu
);
569 mlx4_en_calc_rx_buf(dev
);
570 en_dbg(DRV
, priv
, "Rx buf size:%d\n", priv
->rx_skb_size
);
572 /* Configure rx cq's and rings */
573 err
= mlx4_en_activate_rx_rings(priv
);
575 en_err(priv
, "Failed to activate RX rings\n");
578 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
579 cq
= &priv
->rx_cq
[i
];
581 err
= mlx4_en_activate_cq(priv
, cq
);
583 en_err(priv
, "Failed activating Rx CQ\n");
586 for (j
= 0; j
< cq
->size
; j
++)
587 cq
->buf
[j
].owner_sr_opcode
= MLX4_CQE_OWNER_MASK
;
588 err
= mlx4_en_set_cq_moder(priv
, cq
);
590 en_err(priv
, "Failed setting cq moderation parameters");
591 mlx4_en_deactivate_cq(priv
, cq
);
594 mlx4_en_arm_cq(priv
, cq
);
595 priv
->rx_ring
[i
].cqn
= cq
->mcq
.cqn
;
599 err
= mlx4_en_config_rss_steer(priv
);
601 en_err(priv
, "Failed configuring rss steering\n");
605 if (mdev
->dev
->caps
.comp_pool
&& !priv
->tx_vector
) {
606 sprintf(name
, "%s-tx", priv
->dev
->name
);
607 if (mlx4_assign_eq(mdev
->dev
, name
, &priv
->tx_vector
)) {
608 mlx4_warn(mdev
, "Failed Assigning an EQ to "
609 "%s_tx ,Falling back to legacy "
610 "EQ's\n", priv
->dev
->name
);
613 /* Configure tx cq's and rings */
614 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
616 cq
= &priv
->tx_cq
[i
];
617 cq
->vector
= priv
->tx_vector
;
618 err
= mlx4_en_activate_cq(priv
, cq
);
620 en_err(priv
, "Failed allocating Tx CQ\n");
623 err
= mlx4_en_set_cq_moder(priv
, cq
);
625 en_err(priv
, "Failed setting cq moderation parameters");
626 mlx4_en_deactivate_cq(priv
, cq
);
629 en_dbg(DRV
, priv
, "Resetting index of collapsed CQ:%d to -1\n", i
);
630 cq
->buf
->wqe_index
= cpu_to_be16(0xffff);
633 tx_ring
= &priv
->tx_ring
[i
];
634 err
= mlx4_en_activate_tx_ring(priv
, tx_ring
, cq
->mcq
.cqn
);
636 en_err(priv
, "Failed allocating Tx ring\n");
637 mlx4_en_deactivate_cq(priv
, cq
);
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;
647 err
= mlx4_SET_PORT_general(mdev
->dev
, priv
->port
,
648 priv
->rx_skb_size
+ ETH_FCS_LEN
,
649 priv
->prof
->tx_pause
,
651 priv
->prof
->rx_pause
,
654 en_err(priv
, "Failed setting port general configurations "
655 "for port %d, with error %d\n", priv
->port
, err
);
658 /* Set default qp number */
659 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
, priv
->port
, priv
->base_qpn
, 0);
661 en_err(priv
, "Failed setting default qp numbers\n");
664 /* Set port mac number */
665 en_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
);
669 en_err(priv
, "Failed setting port mac\n");
672 mdev
->mac_removed
[priv
->port
] = 0;
675 en_dbg(HW
, priv
, "Initializing port\n");
676 err
= mlx4_INIT_PORT(mdev
->dev
, priv
->port
);
678 en_err(priv
, "Failed Initializing port\n");
682 /* Schedule multicast task to populate multicast list */
683 queue_work(mdev
->workqueue
, &priv
->mcast_task
);
685 priv
->port_up
= true;
686 netif_tx_start_all_queues(dev
);
690 mlx4_unregister_mac(mdev
->dev
, priv
->port
, priv
->mac_index
);
693 mlx4_en_deactivate_tx_ring(priv
, &priv
->tx_ring
[tx_index
]);
694 mlx4_en_deactivate_cq(priv
, &priv
->tx_cq
[tx_index
]);
697 mlx4_en_release_rss_steer(priv
);
700 mlx4_en_deactivate_cq(priv
, &priv
->rx_cq
[rx_index
]);
701 for (i
= 0; i
< priv
->rx_ring_num
; i
++)
702 mlx4_en_deactivate_rx_ring(priv
, &priv
->rx_ring
[i
]);
704 return err
; /* need to close devices */
708 void mlx4_en_stop_port(struct net_device
*dev
)
710 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
711 struct mlx4_en_dev
*mdev
= priv
->mdev
;
714 if (!priv
->port_up
) {
715 en_dbg(DRV
, priv
, "stop port called while port already down\n");
719 /* Synchronize with tx routine */
720 netif_tx_lock_bh(dev
);
721 netif_tx_stop_all_queues(dev
);
722 netif_tx_unlock_bh(dev
);
724 /* Set port as not active */
725 priv
->port_up
= false;
727 /* Unregister Mac address for the port */
728 mlx4_unregister_mac(mdev
->dev
, priv
->port
, priv
->mac_index
);
729 mdev
->mac_removed
[priv
->port
] = 1;
732 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
733 mlx4_en_deactivate_tx_ring(priv
, &priv
->tx_ring
[i
]);
734 mlx4_en_deactivate_cq(priv
, &priv
->tx_cq
[i
]);
738 for (i
= 0; i
< priv
->tx_ring_num
; i
++)
739 mlx4_en_free_tx_buf(dev
, &priv
->tx_ring
[i
]);
742 mlx4_en_release_rss_steer(priv
);
745 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
746 mlx4_en_deactivate_rx_ring(priv
, &priv
->rx_ring
[i
]);
747 while (test_bit(NAPI_STATE_SCHED
, &priv
->rx_cq
[i
].napi
.state
))
749 mlx4_en_deactivate_cq(priv
, &priv
->rx_cq
[i
]);
753 mlx4_CLOSE_PORT(mdev
->dev
, priv
->port
);
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 en_dbg(DRV
, priv
, "Watchdog task called for port %d\n", priv
->port
);
765 mutex_lock(&mdev
->state_lock
);
767 mlx4_en_stop_port(dev
);
768 if (mlx4_en_start_port(dev
))
769 en_err(priv
, "Failed restarting port %d\n", priv
->port
);
771 mutex_unlock(&mdev
->state_lock
);
775 static int mlx4_en_open(struct net_device
*dev
)
777 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
778 struct mlx4_en_dev
*mdev
= priv
->mdev
;
782 mutex_lock(&mdev
->state_lock
);
784 if (!mdev
->device_up
) {
785 en_err(priv
, "Cannot open - device down/disabled\n");
790 /* Reset HW statistics and performance counters */
791 if (mlx4_en_DUMP_ETH_STATS(mdev
, priv
->port
, 1))
792 en_dbg(HW
, priv
, "Failed dumping statistics\n");
794 memset(&priv
->stats
, 0, sizeof(priv
->stats
));
795 memset(&priv
->pstats
, 0, sizeof(priv
->pstats
));
797 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
798 priv
->tx_ring
[i
].bytes
= 0;
799 priv
->tx_ring
[i
].packets
= 0;
801 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
802 priv
->rx_ring
[i
].bytes
= 0;
803 priv
->rx_ring
[i
].packets
= 0;
806 err
= mlx4_en_start_port(dev
);
808 en_err(priv
, "Failed starting port:%d\n", priv
->port
);
811 mutex_unlock(&mdev
->state_lock
);
816 static int mlx4_en_close(struct net_device
*dev
)
818 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
819 struct mlx4_en_dev
*mdev
= priv
->mdev
;
821 en_dbg(IFDOWN
, priv
, "Close port called\n");
823 mutex_lock(&mdev
->state_lock
);
825 mlx4_en_stop_port(dev
);
826 netif_carrier_off(dev
);
828 mutex_unlock(&mdev
->state_lock
);
832 void mlx4_en_free_resources(struct mlx4_en_priv
*priv
, bool reserve_vectors
)
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
], reserve_vectors
);
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
], reserve_vectors
);
851 int mlx4_en_alloc_resources(struct mlx4_en_priv
*priv
)
853 struct mlx4_en_port_profile
*prof
= priv
->prof
;
856 /* Create tx Rings */
857 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
858 if (mlx4_en_create_cq(priv
, &priv
->tx_cq
[i
],
859 prof
->tx_ring_size
, i
, TX
))
862 if (mlx4_en_create_tx_ring(priv
, &priv
->tx_ring
[i
],
863 prof
->tx_ring_size
, TXBB_SIZE
))
867 /* Create rx Rings */
868 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
869 if (mlx4_en_create_cq(priv
, &priv
->rx_cq
[i
],
870 prof
->rx_ring_size
, i
, RX
))
873 if (mlx4_en_create_rx_ring(priv
, &priv
->rx_ring
[i
],
874 prof
->rx_ring_size
, priv
->stride
))
881 en_err(priv
, "Failed to allocate NIC resources\n");
886 void mlx4_en_destroy_netdev(struct net_device
*dev
)
888 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
889 struct mlx4_en_dev
*mdev
= priv
->mdev
;
891 en_dbg(DRV
, priv
, "Destroying netdev on port:%d\n", priv
->port
);
893 /* Unregister device - this will close the port if it was up */
894 if (priv
->registered
)
895 unregister_netdev(dev
);
898 mlx4_free_hwq_res(mdev
->dev
, &priv
->res
, MLX4_EN_PAGE_SIZE
);
900 cancel_delayed_work(&priv
->stats_task
);
901 /* flush any pending task for this netdev */
902 flush_workqueue(mdev
->workqueue
);
904 /* Detach the netdev so tasks would not attempt to access it */
905 mutex_lock(&mdev
->state_lock
);
906 mdev
->pndev
[priv
->port
] = NULL
;
907 mutex_unlock(&mdev
->state_lock
);
909 mlx4_en_free_resources(priv
, false);
913 static int mlx4_en_change_mtu(struct net_device
*dev
, int new_mtu
)
915 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
916 struct mlx4_en_dev
*mdev
= priv
->mdev
;
919 en_dbg(DRV
, priv
, "Change MTU called - current:%d new:%d\n",
922 if ((new_mtu
< MLX4_EN_MIN_MTU
) || (new_mtu
> priv
->max_mtu
)) {
923 en_err(priv
, "Bad MTU size:%d.\n", new_mtu
);
928 if (netif_running(dev
)) {
929 mutex_lock(&mdev
->state_lock
);
930 if (!mdev
->device_up
) {
931 /* NIC is probably restarting - let watchdog task reset
933 en_dbg(DRV
, priv
, "Change MTU called with card down!?\n");
935 mlx4_en_stop_port(dev
);
936 err
= mlx4_en_start_port(dev
);
938 en_err(priv
, "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_select_queue
= mlx4_en_select_queue
,
953 .ndo_get_stats
= mlx4_en_get_stats
,
954 .ndo_set_multicast_list
= mlx4_en_set_multicast
,
955 .ndo_set_mac_address
= mlx4_en_set_mac
,
956 .ndo_validate_addr
= eth_validate_addr
,
957 .ndo_change_mtu
= mlx4_en_change_mtu
,
958 .ndo_tx_timeout
= mlx4_en_tx_timeout
,
959 .ndo_vlan_rx_register
= mlx4_en_vlan_rx_register
,
960 .ndo_vlan_rx_add_vid
= mlx4_en_vlan_rx_add_vid
,
961 .ndo_vlan_rx_kill_vid
= mlx4_en_vlan_rx_kill_vid
,
962 #ifdef CONFIG_NET_POLL_CONTROLLER
963 .ndo_poll_controller
= mlx4_en_netpoll
,
967 int mlx4_en_init_netdev(struct mlx4_en_dev
*mdev
, int port
,
968 struct mlx4_en_port_profile
*prof
)
970 struct net_device
*dev
;
971 struct mlx4_en_priv
*priv
;
975 dev
= alloc_etherdev_mqs(sizeof(struct mlx4_en_priv
),
976 prof
->tx_ring_num
, prof
->rx_ring_num
);
978 mlx4_err(mdev
, "Net device allocation failed\n");
982 SET_NETDEV_DEV(dev
, &mdev
->dev
->pdev
->dev
);
983 dev
->dev_id
= port
- 1;
986 * Initialize driver private data
989 priv
= netdev_priv(dev
);
990 memset(priv
, 0, sizeof(struct mlx4_en_priv
));
995 priv
->port_up
= false;
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
->mac_index
= -1;
1001 priv
->msg_enable
= MLX4_EN_MSG_LEVEL
;
1002 spin_lock_init(&priv
->stats_lock
);
1003 INIT_WORK(&priv
->mcast_task
, mlx4_en_do_set_multicast
);
1004 INIT_WORK(&priv
->mac_task
, mlx4_en_do_set_mac
);
1005 INIT_WORK(&priv
->watchdog_task
, mlx4_en_restart
);
1006 INIT_WORK(&priv
->linkstate_task
, mlx4_en_linkstate
);
1007 INIT_DELAYED_WORK(&priv
->stats_task
, mlx4_en_do_get_stats
);
1009 /* Query for default mac and max mtu */
1010 priv
->max_mtu
= mdev
->dev
->caps
.eth_mtu_cap
[priv
->port
];
1011 priv
->mac
= mdev
->dev
->caps
.def_mac
[priv
->port
];
1012 if (ILLEGAL_MAC(priv
->mac
)) {
1013 en_err(priv
, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
1014 priv
->port
, priv
->mac
);
1019 priv
->stride
= roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc
) +
1020 DS_SIZE
* MLX4_EN_MAX_RX_FRAGS
);
1021 err
= mlx4_en_alloc_resources(priv
);
1025 /* Allocate page for receive rings */
1026 err
= mlx4_alloc_hwq_res(mdev
->dev
, &priv
->res
,
1027 MLX4_EN_PAGE_SIZE
, MLX4_EN_PAGE_SIZE
);
1029 en_err(priv
, "Failed to allocate page for rx qps\n");
1032 priv
->allocated
= 1;
1035 * Initialize netdev entry points
1037 dev
->netdev_ops
= &mlx4_netdev_ops
;
1038 dev
->watchdog_timeo
= MLX4_EN_WATCHDOG_TIMEOUT
;
1039 netif_set_real_num_tx_queues(dev
, priv
->tx_ring_num
);
1040 netif_set_real_num_rx_queues(dev
, priv
->rx_ring_num
);
1042 SET_ETHTOOL_OPS(dev
, &mlx4_en_ethtool_ops
);
1044 /* Set defualt MAC */
1045 dev
->addr_len
= ETH_ALEN
;
1046 for (i
= 0; i
< ETH_ALEN
; i
++) {
1047 dev
->dev_addr
[ETH_ALEN
- 1 - i
] = (u8
) (priv
->mac
>> (8 * i
));
1048 dev
->perm_addr
[ETH_ALEN
- 1 - i
] = (u8
) (priv
->mac
>> (8 * i
));
1052 * Set driver features
1054 dev
->features
|= NETIF_F_SG
;
1055 dev
->vlan_features
|= NETIF_F_SG
;
1056 dev
->features
|= NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
1057 dev
->vlan_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 dev
->features
|= NETIF_F_GRO
;
1063 if (mdev
->LSO_support
) {
1064 dev
->features
|= NETIF_F_TSO
;
1065 dev
->features
|= NETIF_F_TSO6
;
1066 dev
->vlan_features
|= NETIF_F_TSO
;
1067 dev
->vlan_features
|= NETIF_F_TSO6
;
1070 mdev
->pndev
[port
] = dev
;
1072 netif_carrier_off(dev
);
1073 err
= register_netdev(dev
);
1075 en_err(priv
, "Netdev registration failed for port %d\n", port
);
1079 en_warn(priv
, "Using %d TX rings\n", prof
->tx_ring_num
);
1080 en_warn(priv
, "Using %d RX rings\n", prof
->rx_ring_num
);
1082 /* Configure port */
1083 err
= mlx4_SET_PORT_general(mdev
->dev
, priv
->port
,
1087 en_err(priv
, "Failed setting port general configurations "
1088 "for port %d, with error %d\n", priv
->port
, err
);
1093 en_warn(priv
, "Initializing port\n");
1094 err
= mlx4_INIT_PORT(mdev
->dev
, priv
->port
);
1096 en_err(priv
, "Failed Initializing port\n");
1099 priv
->registered
= 1;
1100 mlx4_en_set_default_moderation(priv
);
1101 queue_delayed_work(mdev
->workqueue
, &priv
->stats_task
, STATS_DELAY
);
1105 mlx4_en_destroy_netdev(dev
);