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_vlan_rx_add_vid(struct net_device
*dev
, unsigned short vid
)
50 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
51 struct mlx4_en_dev
*mdev
= priv
->mdev
;
55 en_dbg(HW
, priv
, "adding VLAN:%d\n", vid
);
57 set_bit(vid
, priv
->active_vlans
);
59 /* Add VID to port VLAN filter */
60 mutex_lock(&mdev
->state_lock
);
61 if (mdev
->device_up
&& priv
->port_up
) {
62 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
);
64 en_err(priv
, "Failed configuring VLAN filter\n");
66 if (mlx4_register_vlan(mdev
->dev
, priv
->port
, vid
, &idx
))
67 en_err(priv
, "failed adding vlan %d\n", vid
);
68 mutex_unlock(&mdev
->state_lock
);
73 static int mlx4_en_vlan_rx_kill_vid(struct net_device
*dev
, unsigned short vid
)
75 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
76 struct mlx4_en_dev
*mdev
= priv
->mdev
;
80 en_dbg(HW
, priv
, "Killing VID:%d\n", vid
);
82 clear_bit(vid
, priv
->active_vlans
);
84 /* Remove VID from port VLAN filter */
85 mutex_lock(&mdev
->state_lock
);
86 if (!mlx4_find_cached_vlan(mdev
->dev
, priv
->port
, vid
, &idx
))
87 mlx4_unregister_vlan(mdev
->dev
, priv
->port
, idx
);
89 en_err(priv
, "could not find vid %d in cache\n", vid
);
91 if (mdev
->device_up
&& priv
->port_up
) {
92 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
);
94 en_err(priv
, "Failed configuring VLAN filter\n");
96 mutex_unlock(&mdev
->state_lock
);
101 u64
mlx4_en_mac_to_u64(u8
*addr
)
106 for (i
= 0; i
< ETH_ALEN
; i
++) {
113 static int mlx4_en_set_mac(struct net_device
*dev
, void *addr
)
115 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
116 struct mlx4_en_dev
*mdev
= priv
->mdev
;
117 struct sockaddr
*saddr
= addr
;
119 if (!is_valid_ether_addr(saddr
->sa_data
))
120 return -EADDRNOTAVAIL
;
122 memcpy(dev
->dev_addr
, saddr
->sa_data
, ETH_ALEN
);
123 priv
->mac
= mlx4_en_mac_to_u64(dev
->dev_addr
);
124 queue_work(mdev
->workqueue
, &priv
->mac_task
);
128 static void mlx4_en_do_set_mac(struct work_struct
*work
)
130 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
132 struct mlx4_en_dev
*mdev
= priv
->mdev
;
135 mutex_lock(&mdev
->state_lock
);
137 /* Remove old MAC and insert the new one */
138 err
= mlx4_replace_mac(mdev
->dev
, priv
->port
,
139 priv
->base_qpn
, priv
->mac
);
141 en_err(priv
, "Failed changing HW MAC address\n");
143 en_dbg(HW
, priv
, "Port is down while "
144 "registering mac, exiting...\n");
146 mutex_unlock(&mdev
->state_lock
);
149 static void mlx4_en_clear_list(struct net_device
*dev
)
151 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
153 kfree(priv
->mc_addrs
);
154 priv
->mc_addrs
= NULL
;
155 priv
->mc_addrs_cnt
= 0;
158 static void mlx4_en_cache_mclist(struct net_device
*dev
)
160 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
161 struct netdev_hw_addr
*ha
;
163 int mc_addrs_cnt
= netdev_mc_count(dev
);
166 mc_addrs
= kmalloc(mc_addrs_cnt
* ETH_ALEN
, GFP_ATOMIC
);
168 en_err(priv
, "failed to allocate multicast list\n");
172 netdev_for_each_mc_addr(ha
, dev
)
173 memcpy(mc_addrs
+ i
++ * ETH_ALEN
, ha
->addr
, ETH_ALEN
);
174 mlx4_en_clear_list(dev
);
175 priv
->mc_addrs
= mc_addrs
;
176 priv
->mc_addrs_cnt
= mc_addrs_cnt
;
180 static void mlx4_en_set_multicast(struct net_device
*dev
)
182 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
187 queue_work(priv
->mdev
->workqueue
, &priv
->mcast_task
);
190 static void mlx4_en_do_set_multicast(struct work_struct
*work
)
192 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
194 struct mlx4_en_dev
*mdev
= priv
->mdev
;
195 struct net_device
*dev
= priv
->dev
;
197 u8 mc_list
[16] = {0};
200 mutex_lock(&mdev
->state_lock
);
201 if (!mdev
->device_up
) {
202 en_dbg(HW
, priv
, "Card is not up, "
203 "ignoring multicast change.\n");
206 if (!priv
->port_up
) {
207 en_dbg(HW
, priv
, "Port is down, "
208 "ignoring multicast change.\n");
212 if (!netif_carrier_ok(dev
)) {
213 if (!mlx4_en_QUERY_PORT(mdev
, priv
->port
)) {
214 if (priv
->port_state
.link_state
) {
215 priv
->last_link_state
= MLX4_DEV_EVENT_PORT_UP
;
216 netif_carrier_on(dev
);
217 en_dbg(LINK
, priv
, "Link Up\n");
223 * Promsicuous mode: disable all filters
226 if (dev
->flags
& IFF_PROMISC
) {
227 if (!(priv
->flags
& MLX4_EN_FLAG_PROMISC
)) {
228 if (netif_msg_rx_status(priv
))
229 en_warn(priv
, "Entering promiscuous mode\n");
230 priv
->flags
|= MLX4_EN_FLAG_PROMISC
;
232 /* Enable promiscouos mode */
233 if (!(mdev
->dev
->caps
.flags
&
234 MLX4_DEV_CAP_FLAG_VEP_UC_STEER
))
235 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
, priv
->port
,
238 err
= mlx4_unicast_promisc_add(mdev
->dev
, priv
->base_qpn
,
241 en_err(priv
, "Failed enabling "
242 "promiscuous mode\n");
244 /* Disable port multicast filter (unconditionally) */
245 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
246 0, MLX4_MCAST_DISABLE
);
248 en_err(priv
, "Failed disabling "
249 "multicast filter\n");
251 /* Add the default qp number as multicast promisc */
252 if (!(priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
)) {
253 err
= mlx4_multicast_promisc_add(mdev
->dev
, priv
->base_qpn
,
256 en_err(priv
, "Failed entering multicast promisc mode\n");
257 priv
->flags
|= MLX4_EN_FLAG_MC_PROMISC
;
260 /* Disable port VLAN filter */
261 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
);
263 en_err(priv
, "Failed disabling VLAN filter\n");
269 * Not in promiscuous mode
272 if (priv
->flags
& MLX4_EN_FLAG_PROMISC
) {
273 if (netif_msg_rx_status(priv
))
274 en_warn(priv
, "Leaving promiscuous mode\n");
275 priv
->flags
&= ~MLX4_EN_FLAG_PROMISC
;
277 /* Disable promiscouos mode */
278 if (!(mdev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_VEP_UC_STEER
))
279 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
, priv
->port
,
282 err
= mlx4_unicast_promisc_remove(mdev
->dev
, priv
->base_qpn
,
285 en_err(priv
, "Failed disabling promiscuous mode\n");
287 /* Disable Multicast promisc */
288 if (priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
) {
289 err
= mlx4_multicast_promisc_remove(mdev
->dev
, priv
->base_qpn
,
292 en_err(priv
, "Failed disabling multicast promiscuous mode\n");
293 priv
->flags
&= ~MLX4_EN_FLAG_MC_PROMISC
;
296 /* Enable port VLAN filter */
297 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
);
299 en_err(priv
, "Failed enabling VLAN filter\n");
302 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
303 if (dev
->flags
& IFF_ALLMULTI
) {
304 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
305 0, MLX4_MCAST_DISABLE
);
307 en_err(priv
, "Failed disabling multicast filter\n");
309 /* Add the default qp number as multicast promisc */
310 if (!(priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
)) {
311 err
= mlx4_multicast_promisc_add(mdev
->dev
, priv
->base_qpn
,
314 en_err(priv
, "Failed entering multicast promisc mode\n");
315 priv
->flags
|= MLX4_EN_FLAG_MC_PROMISC
;
319 /* Disable Multicast promisc */
320 if (priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
) {
321 err
= mlx4_multicast_promisc_remove(mdev
->dev
, priv
->base_qpn
,
324 en_err(priv
, "Failed disabling multicast promiscuous mode\n");
325 priv
->flags
&= ~MLX4_EN_FLAG_MC_PROMISC
;
328 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
329 0, MLX4_MCAST_DISABLE
);
331 en_err(priv
, "Failed disabling multicast filter\n");
333 /* Detach our qp from all the multicast addresses */
334 for (i
= 0; i
< priv
->mc_addrs_cnt
; i
++) {
335 memcpy(&mc_list
[10], priv
->mc_addrs
+ i
* ETH_ALEN
, ETH_ALEN
);
336 mc_list
[5] = priv
->port
;
337 mlx4_multicast_detach(mdev
->dev
, &priv
->rss_map
.indir_qp
,
338 mc_list
, MLX4_PROT_ETH
);
340 /* Flush mcast filter and init it with broadcast address */
341 mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, ETH_BCAST
,
342 1, MLX4_MCAST_CONFIG
);
344 /* Update multicast list - we cache all addresses so they won't
345 * change while HW is updated holding the command semaphor */
346 netif_tx_lock_bh(dev
);
347 mlx4_en_cache_mclist(dev
);
348 netif_tx_unlock_bh(dev
);
349 for (i
= 0; i
< priv
->mc_addrs_cnt
; i
++) {
351 mlx4_en_mac_to_u64(priv
->mc_addrs
+ i
* ETH_ALEN
);
352 memcpy(&mc_list
[10], priv
->mc_addrs
+ i
* ETH_ALEN
, ETH_ALEN
);
353 mc_list
[5] = priv
->port
;
354 mlx4_multicast_attach(mdev
->dev
, &priv
->rss_map
.indir_qp
,
355 mc_list
, 0, MLX4_PROT_ETH
);
356 mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
,
357 mcast_addr
, 0, MLX4_MCAST_CONFIG
);
359 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
360 0, MLX4_MCAST_ENABLE
);
362 en_err(priv
, "Failed enabling multicast filter\n");
365 mutex_unlock(&mdev
->state_lock
);
368 #ifdef CONFIG_NET_POLL_CONTROLLER
369 static void mlx4_en_netpoll(struct net_device
*dev
)
371 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
372 struct mlx4_en_cq
*cq
;
375 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
376 cq
= &priv
->rx_cq
[i
];
377 napi_schedule(&cq
->napi
);
382 static void mlx4_en_tx_timeout(struct net_device
*dev
)
384 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
385 struct mlx4_en_dev
*mdev
= priv
->mdev
;
387 if (netif_msg_timer(priv
))
388 en_warn(priv
, "Tx timeout called on port:%d\n", priv
->port
);
390 priv
->port_stats
.tx_timeout
++;
391 en_dbg(DRV
, priv
, "Scheduling watchdog\n");
392 queue_work(mdev
->workqueue
, &priv
->watchdog_task
);
396 static struct net_device_stats
*mlx4_en_get_stats(struct net_device
*dev
)
398 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
400 spin_lock_bh(&priv
->stats_lock
);
401 memcpy(&priv
->ret_stats
, &priv
->stats
, sizeof(priv
->stats
));
402 spin_unlock_bh(&priv
->stats_lock
);
404 return &priv
->ret_stats
;
407 static void mlx4_en_set_default_moderation(struct mlx4_en_priv
*priv
)
409 struct mlx4_en_cq
*cq
;
412 /* If we haven't received a specific coalescing setting
413 * (module param), we set the moderation parameters as follows:
414 * - moder_cnt is set to the number of mtu sized packets to
415 * satisfy our coelsing target.
416 * - moder_time is set to a fixed value.
418 priv
->rx_frames
= MLX4_EN_RX_COAL_TARGET
;
419 priv
->rx_usecs
= MLX4_EN_RX_COAL_TIME
;
420 en_dbg(INTR
, priv
, "Default coalesing params for mtu:%d - "
421 "rx_frames:%d rx_usecs:%d\n",
422 priv
->dev
->mtu
, priv
->rx_frames
, priv
->rx_usecs
);
424 /* Setup cq moderation params */
425 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
426 cq
= &priv
->rx_cq
[i
];
427 cq
->moder_cnt
= priv
->rx_frames
;
428 cq
->moder_time
= priv
->rx_usecs
;
429 priv
->last_moder_time
[i
] = MLX4_EN_AUTO_CONF
;
430 priv
->last_moder_packets
[i
] = 0;
431 priv
->last_moder_bytes
[i
] = 0;
434 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
435 cq
= &priv
->tx_cq
[i
];
436 cq
->moder_cnt
= MLX4_EN_TX_COAL_PKTS
;
437 cq
->moder_time
= MLX4_EN_TX_COAL_TIME
;
440 /* Reset auto-moderation params */
441 priv
->pkt_rate_low
= MLX4_EN_RX_RATE_LOW
;
442 priv
->rx_usecs_low
= MLX4_EN_RX_COAL_TIME_LOW
;
443 priv
->pkt_rate_high
= MLX4_EN_RX_RATE_HIGH
;
444 priv
->rx_usecs_high
= MLX4_EN_RX_COAL_TIME_HIGH
;
445 priv
->sample_interval
= MLX4_EN_SAMPLE_INTERVAL
;
446 priv
->adaptive_rx_coal
= 1;
447 priv
->last_moder_jiffies
= 0;
448 priv
->last_moder_tx_packets
= 0;
451 static void mlx4_en_auto_moderation(struct mlx4_en_priv
*priv
)
453 unsigned long period
= (unsigned long) (jiffies
- priv
->last_moder_jiffies
);
454 struct mlx4_en_cq
*cq
;
455 unsigned long packets
;
457 unsigned long avg_pkt_size
;
458 unsigned long rx_packets
;
459 unsigned long rx_bytes
;
460 unsigned long rx_pkt_diff
;
464 if (!priv
->adaptive_rx_coal
|| period
< priv
->sample_interval
* HZ
)
467 for (ring
= 0; ring
< priv
->rx_ring_num
; ring
++) {
468 spin_lock_bh(&priv
->stats_lock
);
469 rx_packets
= priv
->rx_ring
[ring
].packets
;
470 rx_bytes
= priv
->rx_ring
[ring
].bytes
;
471 spin_unlock_bh(&priv
->stats_lock
);
473 rx_pkt_diff
= ((unsigned long) (rx_packets
-
474 priv
->last_moder_packets
[ring
]));
475 packets
= rx_pkt_diff
;
476 rate
= packets
* HZ
/ period
;
477 avg_pkt_size
= packets
? ((unsigned long) (rx_bytes
-
478 priv
->last_moder_bytes
[ring
])) / packets
: 0;
480 /* Apply auto-moderation only when packet rate
481 * exceeds a rate that it matters */
482 if (rate
> (MLX4_EN_RX_RATE_THRESH
/ priv
->rx_ring_num
) &&
483 avg_pkt_size
> MLX4_EN_AVG_PKT_SMALL
) {
484 if (rate
< priv
->pkt_rate_low
)
485 moder_time
= priv
->rx_usecs_low
;
486 else if (rate
> priv
->pkt_rate_high
)
487 moder_time
= priv
->rx_usecs_high
;
489 moder_time
= (rate
- priv
->pkt_rate_low
) *
490 (priv
->rx_usecs_high
- priv
->rx_usecs_low
) /
491 (priv
->pkt_rate_high
- priv
->pkt_rate_low
) +
494 moder_time
= priv
->rx_usecs_low
;
497 if (moder_time
!= priv
->last_moder_time
[ring
]) {
498 priv
->last_moder_time
[ring
] = moder_time
;
499 cq
= &priv
->rx_cq
[ring
];
500 cq
->moder_time
= moder_time
;
501 err
= mlx4_en_set_cq_moder(priv
, cq
);
503 en_err(priv
, "Failed modifying moderation "
504 "for cq:%d\n", ring
);
506 priv
->last_moder_packets
[ring
] = rx_packets
;
507 priv
->last_moder_bytes
[ring
] = rx_bytes
;
510 priv
->last_moder_jiffies
= jiffies
;
513 static void mlx4_en_do_get_stats(struct work_struct
*work
)
515 struct delayed_work
*delay
= to_delayed_work(work
);
516 struct mlx4_en_priv
*priv
= container_of(delay
, struct mlx4_en_priv
,
518 struct mlx4_en_dev
*mdev
= priv
->mdev
;
521 err
= mlx4_en_DUMP_ETH_STATS(mdev
, priv
->port
, 0);
523 en_dbg(HW
, priv
, "Could not update stats\n");
525 mutex_lock(&mdev
->state_lock
);
526 if (mdev
->device_up
) {
528 mlx4_en_auto_moderation(priv
);
530 queue_delayed_work(mdev
->workqueue
, &priv
->stats_task
, STATS_DELAY
);
532 if (mdev
->mac_removed
[MLX4_MAX_PORTS
+ 1 - priv
->port
]) {
533 queue_work(mdev
->workqueue
, &priv
->mac_task
);
534 mdev
->mac_removed
[MLX4_MAX_PORTS
+ 1 - priv
->port
] = 0;
536 mutex_unlock(&mdev
->state_lock
);
539 static void mlx4_en_linkstate(struct work_struct
*work
)
541 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
543 struct mlx4_en_dev
*mdev
= priv
->mdev
;
544 int linkstate
= priv
->link_state
;
546 mutex_lock(&mdev
->state_lock
);
547 /* If observable port state changed set carrier state and
548 * report to system log */
549 if (priv
->last_link_state
!= linkstate
) {
550 if (linkstate
== MLX4_DEV_EVENT_PORT_DOWN
) {
551 en_info(priv
, "Link Down\n");
552 netif_carrier_off(priv
->dev
);
554 en_info(priv
, "Link Up\n");
555 netif_carrier_on(priv
->dev
);
558 priv
->last_link_state
= linkstate
;
559 mutex_unlock(&mdev
->state_lock
);
563 int mlx4_en_start_port(struct net_device
*dev
)
565 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
566 struct mlx4_en_dev
*mdev
= priv
->mdev
;
567 struct mlx4_en_cq
*cq
;
568 struct mlx4_en_tx_ring
*tx_ring
;
574 u8 mc_list
[16] = {0};
577 en_dbg(DRV
, priv
, "start port called while port already up\n");
581 /* Calculate Rx buf size */
582 dev
->mtu
= min(dev
->mtu
, priv
->max_mtu
);
583 mlx4_en_calc_rx_buf(dev
);
584 en_dbg(DRV
, priv
, "Rx buf size:%d\n", priv
->rx_skb_size
);
586 /* Configure rx cq's and rings */
587 err
= mlx4_en_activate_rx_rings(priv
);
589 en_err(priv
, "Failed to activate RX rings\n");
592 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
593 cq
= &priv
->rx_cq
[i
];
595 err
= mlx4_en_activate_cq(priv
, cq
, i
);
597 en_err(priv
, "Failed activating Rx CQ\n");
600 for (j
= 0; j
< cq
->size
; j
++)
601 cq
->buf
[j
].owner_sr_opcode
= MLX4_CQE_OWNER_MASK
;
602 err
= mlx4_en_set_cq_moder(priv
, cq
);
604 en_err(priv
, "Failed setting cq moderation parameters");
605 mlx4_en_deactivate_cq(priv
, cq
);
608 mlx4_en_arm_cq(priv
, cq
);
609 priv
->rx_ring
[i
].cqn
= cq
->mcq
.cqn
;
614 en_dbg(DRV
, priv
, "Getting qp number for port %d\n", priv
->port
);
615 err
= mlx4_get_eth_qp(mdev
->dev
, priv
->port
,
616 priv
->mac
, &priv
->base_qpn
);
618 en_err(priv
, "Failed getting eth qp\n");
621 mdev
->mac_removed
[priv
->port
] = 0;
623 err
= mlx4_en_config_rss_steer(priv
);
625 en_err(priv
, "Failed configuring rss steering\n");
629 /* Configure tx cq's and rings */
630 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
632 cq
= &priv
->tx_cq
[i
];
633 err
= mlx4_en_activate_cq(priv
, cq
, i
);
635 en_err(priv
, "Failed allocating Tx CQ\n");
638 err
= mlx4_en_set_cq_moder(priv
, cq
);
640 en_err(priv
, "Failed setting cq moderation parameters");
641 mlx4_en_deactivate_cq(priv
, cq
);
644 en_dbg(DRV
, priv
, "Resetting index of collapsed CQ:%d to -1\n", i
);
645 cq
->buf
->wqe_index
= cpu_to_be16(0xffff);
648 tx_ring
= &priv
->tx_ring
[i
];
649 err
= mlx4_en_activate_tx_ring(priv
, tx_ring
, cq
->mcq
.cqn
);
651 en_err(priv
, "Failed allocating Tx ring\n");
652 mlx4_en_deactivate_cq(priv
, cq
);
655 /* Set initial ownership of all Tx TXBBs to SW (1) */
656 for (j
= 0; j
< tx_ring
->buf_size
; j
+= STAMP_STRIDE
)
657 *((u32
*) (tx_ring
->buf
+ j
)) = 0xffffffff;
662 err
= mlx4_SET_PORT_general(mdev
->dev
, priv
->port
,
663 priv
->rx_skb_size
+ ETH_FCS_LEN
,
664 priv
->prof
->tx_pause
,
666 priv
->prof
->rx_pause
,
669 en_err(priv
, "Failed setting port general configurations "
670 "for port %d, with error %d\n", priv
->port
, err
);
673 /* Set default qp number */
674 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
, priv
->port
, priv
->base_qpn
, 0);
676 en_err(priv
, "Failed setting default qp numbers\n");
681 en_dbg(HW
, priv
, "Initializing port\n");
682 err
= mlx4_INIT_PORT(mdev
->dev
, priv
->port
);
684 en_err(priv
, "Failed Initializing port\n");
688 /* Attach rx QP to bradcast address */
689 memset(&mc_list
[10], 0xff, ETH_ALEN
);
690 mc_list
[5] = priv
->port
;
691 if (mlx4_multicast_attach(mdev
->dev
, &priv
->rss_map
.indir_qp
, mc_list
,
693 mlx4_warn(mdev
, "Failed Attaching Broadcast\n");
695 /* Must redo promiscuous mode setup. */
696 priv
->flags
&= ~(MLX4_EN_FLAG_PROMISC
| MLX4_EN_FLAG_MC_PROMISC
);
698 /* Schedule multicast task to populate multicast list */
699 queue_work(mdev
->workqueue
, &priv
->mcast_task
);
701 mlx4_set_stats_bitmap(mdev
->dev
, &priv
->stats_bitmap
);
703 priv
->port_up
= true;
704 netif_tx_start_all_queues(dev
);
709 mlx4_en_deactivate_tx_ring(priv
, &priv
->tx_ring
[tx_index
]);
710 mlx4_en_deactivate_cq(priv
, &priv
->tx_cq
[tx_index
]);
713 mlx4_en_release_rss_steer(priv
);
715 mlx4_put_eth_qp(mdev
->dev
, priv
->port
, priv
->mac
, priv
->base_qpn
);
718 mlx4_en_deactivate_cq(priv
, &priv
->rx_cq
[rx_index
]);
719 for (i
= 0; i
< priv
->rx_ring_num
; i
++)
720 mlx4_en_deactivate_rx_ring(priv
, &priv
->rx_ring
[i
]);
722 return err
; /* need to close devices */
726 void mlx4_en_stop_port(struct net_device
*dev
)
728 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
729 struct mlx4_en_dev
*mdev
= priv
->mdev
;
731 u8 mc_list
[16] = {0};
733 if (!priv
->port_up
) {
734 en_dbg(DRV
, priv
, "stop port called while port already down\n");
738 /* Synchronize with tx routine */
739 netif_tx_lock_bh(dev
);
740 netif_tx_stop_all_queues(dev
);
741 netif_tx_unlock_bh(dev
);
743 /* Set port as not active */
744 priv
->port_up
= false;
746 /* Detach All multicasts */
747 memset(&mc_list
[10], 0xff, ETH_ALEN
);
748 mc_list
[5] = priv
->port
;
749 mlx4_multicast_detach(mdev
->dev
, &priv
->rss_map
.indir_qp
, mc_list
,
751 for (i
= 0; i
< priv
->mc_addrs_cnt
; i
++) {
752 memcpy(&mc_list
[10], priv
->mc_addrs
+ i
* ETH_ALEN
, ETH_ALEN
);
753 mc_list
[5] = priv
->port
;
754 mlx4_multicast_detach(mdev
->dev
, &priv
->rss_map
.indir_qp
,
755 mc_list
, MLX4_PROT_ETH
);
757 mlx4_en_clear_list(dev
);
758 /* Flush multicast filter */
759 mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0, 1, MLX4_MCAST_CONFIG
);
762 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
763 mlx4_en_deactivate_tx_ring(priv
, &priv
->tx_ring
[i
]);
764 mlx4_en_deactivate_cq(priv
, &priv
->tx_cq
[i
]);
768 for (i
= 0; i
< priv
->tx_ring_num
; i
++)
769 mlx4_en_free_tx_buf(dev
, &priv
->tx_ring
[i
]);
772 mlx4_en_release_rss_steer(priv
);
774 /* Unregister Mac address for the port */
775 mlx4_put_eth_qp(mdev
->dev
, priv
->port
, priv
->mac
, priv
->base_qpn
);
776 mdev
->mac_removed
[priv
->port
] = 1;
779 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
780 mlx4_en_deactivate_rx_ring(priv
, &priv
->rx_ring
[i
]);
781 while (test_bit(NAPI_STATE_SCHED
, &priv
->rx_cq
[i
].napi
.state
))
783 mlx4_en_deactivate_cq(priv
, &priv
->rx_cq
[i
]);
787 mlx4_CLOSE_PORT(mdev
->dev
, priv
->port
);
790 static void mlx4_en_restart(struct work_struct
*work
)
792 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
794 struct mlx4_en_dev
*mdev
= priv
->mdev
;
795 struct net_device
*dev
= priv
->dev
;
797 en_dbg(DRV
, priv
, "Watchdog task called for port %d\n", priv
->port
);
799 mutex_lock(&mdev
->state_lock
);
801 mlx4_en_stop_port(dev
);
802 if (mlx4_en_start_port(dev
))
803 en_err(priv
, "Failed restarting port %d\n", priv
->port
);
805 mutex_unlock(&mdev
->state_lock
);
808 static void mlx4_en_clear_stats(struct net_device
*dev
)
810 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
811 struct mlx4_en_dev
*mdev
= priv
->mdev
;
814 if (mlx4_en_DUMP_ETH_STATS(mdev
, priv
->port
, 1))
815 en_dbg(HW
, priv
, "Failed dumping statistics\n");
817 memset(&priv
->stats
, 0, sizeof(priv
->stats
));
818 memset(&priv
->pstats
, 0, sizeof(priv
->pstats
));
819 memset(&priv
->pkstats
, 0, sizeof(priv
->pkstats
));
820 memset(&priv
->port_stats
, 0, sizeof(priv
->port_stats
));
822 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
823 priv
->tx_ring
[i
].bytes
= 0;
824 priv
->tx_ring
[i
].packets
= 0;
825 priv
->tx_ring
[i
].tx_csum
= 0;
827 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
828 priv
->rx_ring
[i
].bytes
= 0;
829 priv
->rx_ring
[i
].packets
= 0;
830 priv
->rx_ring
[i
].csum_ok
= 0;
831 priv
->rx_ring
[i
].csum_none
= 0;
835 static int mlx4_en_open(struct net_device
*dev
)
837 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
838 struct mlx4_en_dev
*mdev
= priv
->mdev
;
841 mutex_lock(&mdev
->state_lock
);
843 if (!mdev
->device_up
) {
844 en_err(priv
, "Cannot open - device down/disabled\n");
849 /* Reset HW statistics and SW counters */
850 mlx4_en_clear_stats(dev
);
852 err
= mlx4_en_start_port(dev
);
854 en_err(priv
, "Failed starting port:%d\n", priv
->port
);
857 mutex_unlock(&mdev
->state_lock
);
862 static int mlx4_en_close(struct net_device
*dev
)
864 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
865 struct mlx4_en_dev
*mdev
= priv
->mdev
;
867 en_dbg(IFDOWN
, priv
, "Close port called\n");
869 mutex_lock(&mdev
->state_lock
);
871 mlx4_en_stop_port(dev
);
872 netif_carrier_off(dev
);
874 mutex_unlock(&mdev
->state_lock
);
878 void mlx4_en_free_resources(struct mlx4_en_priv
*priv
)
882 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
883 if (priv
->tx_ring
[i
].tx_info
)
884 mlx4_en_destroy_tx_ring(priv
, &priv
->tx_ring
[i
]);
885 if (priv
->tx_cq
[i
].buf
)
886 mlx4_en_destroy_cq(priv
, &priv
->tx_cq
[i
]);
889 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
890 if (priv
->rx_ring
[i
].rx_info
)
891 mlx4_en_destroy_rx_ring(priv
, &priv
->rx_ring
[i
],
892 priv
->prof
->rx_ring_size
, priv
->stride
);
893 if (priv
->rx_cq
[i
].buf
)
894 mlx4_en_destroy_cq(priv
, &priv
->rx_cq
[i
]);
898 int mlx4_en_alloc_resources(struct mlx4_en_priv
*priv
)
900 struct mlx4_en_port_profile
*prof
= priv
->prof
;
902 int base_tx_qpn
, err
;
904 err
= mlx4_qp_reserve_range(priv
->mdev
->dev
, priv
->tx_ring_num
, 256, &base_tx_qpn
);
906 en_err(priv
, "failed reserving range for TX rings\n");
910 /* Create tx Rings */
911 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
912 if (mlx4_en_create_cq(priv
, &priv
->tx_cq
[i
],
913 prof
->tx_ring_size
, i
, TX
))
916 if (mlx4_en_create_tx_ring(priv
, &priv
->tx_ring
[i
], base_tx_qpn
+ i
,
917 prof
->tx_ring_size
, TXBB_SIZE
))
921 /* Create rx Rings */
922 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
923 if (mlx4_en_create_cq(priv
, &priv
->rx_cq
[i
],
924 prof
->rx_ring_size
, i
, RX
))
927 if (mlx4_en_create_rx_ring(priv
, &priv
->rx_ring
[i
],
928 prof
->rx_ring_size
, priv
->stride
))
935 en_err(priv
, "Failed to allocate NIC resources\n");
936 mlx4_qp_release_range(priv
->mdev
->dev
, base_tx_qpn
, priv
->tx_ring_num
);
941 void mlx4_en_destroy_netdev(struct net_device
*dev
)
943 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
944 struct mlx4_en_dev
*mdev
= priv
->mdev
;
946 en_dbg(DRV
, priv
, "Destroying netdev on port:%d\n", priv
->port
);
948 /* Unregister device - this will close the port if it was up */
949 if (priv
->registered
)
950 unregister_netdev(dev
);
953 mlx4_free_hwq_res(mdev
->dev
, &priv
->res
, MLX4_EN_PAGE_SIZE
);
955 cancel_delayed_work(&priv
->stats_task
);
956 /* flush any pending task for this netdev */
957 flush_workqueue(mdev
->workqueue
);
959 /* Detach the netdev so tasks would not attempt to access it */
960 mutex_lock(&mdev
->state_lock
);
961 mdev
->pndev
[priv
->port
] = NULL
;
962 mutex_unlock(&mdev
->state_lock
);
964 mlx4_en_free_resources(priv
);
968 static int mlx4_en_change_mtu(struct net_device
*dev
, int new_mtu
)
970 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
971 struct mlx4_en_dev
*mdev
= priv
->mdev
;
974 en_dbg(DRV
, priv
, "Change MTU called - current:%d new:%d\n",
977 if ((new_mtu
< MLX4_EN_MIN_MTU
) || (new_mtu
> priv
->max_mtu
)) {
978 en_err(priv
, "Bad MTU size:%d.\n", new_mtu
);
983 if (netif_running(dev
)) {
984 mutex_lock(&mdev
->state_lock
);
985 if (!mdev
->device_up
) {
986 /* NIC is probably restarting - let watchdog task reset
988 en_dbg(DRV
, priv
, "Change MTU called with card down!?\n");
990 mlx4_en_stop_port(dev
);
991 err
= mlx4_en_start_port(dev
);
993 en_err(priv
, "Failed restarting port:%d\n",
995 queue_work(mdev
->workqueue
, &priv
->watchdog_task
);
998 mutex_unlock(&mdev
->state_lock
);
1003 static int mlx4_en_set_features(struct net_device
*netdev
,
1004 netdev_features_t features
)
1006 struct mlx4_en_priv
*priv
= netdev_priv(netdev
);
1008 if (features
& NETIF_F_LOOPBACK
)
1009 priv
->ctrl_flags
|= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK
);
1012 cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK
);
1018 static const struct net_device_ops mlx4_netdev_ops
= {
1019 .ndo_open
= mlx4_en_open
,
1020 .ndo_stop
= mlx4_en_close
,
1021 .ndo_start_xmit
= mlx4_en_xmit
,
1022 .ndo_select_queue
= mlx4_en_select_queue
,
1023 .ndo_get_stats
= mlx4_en_get_stats
,
1024 .ndo_set_rx_mode
= mlx4_en_set_multicast
,
1025 .ndo_set_mac_address
= mlx4_en_set_mac
,
1026 .ndo_validate_addr
= eth_validate_addr
,
1027 .ndo_change_mtu
= mlx4_en_change_mtu
,
1028 .ndo_tx_timeout
= mlx4_en_tx_timeout
,
1029 .ndo_vlan_rx_add_vid
= mlx4_en_vlan_rx_add_vid
,
1030 .ndo_vlan_rx_kill_vid
= mlx4_en_vlan_rx_kill_vid
,
1031 #ifdef CONFIG_NET_POLL_CONTROLLER
1032 .ndo_poll_controller
= mlx4_en_netpoll
,
1034 .ndo_set_features
= mlx4_en_set_features
,
1037 int mlx4_en_init_netdev(struct mlx4_en_dev
*mdev
, int port
,
1038 struct mlx4_en_port_profile
*prof
)
1040 struct net_device
*dev
;
1041 struct mlx4_en_priv
*priv
;
1045 dev
= alloc_etherdev_mqs(sizeof(struct mlx4_en_priv
),
1046 prof
->tx_ring_num
, prof
->rx_ring_num
);
1050 SET_NETDEV_DEV(dev
, &mdev
->dev
->pdev
->dev
);
1051 dev
->dev_id
= port
- 1;
1054 * Initialize driver private data
1057 priv
= netdev_priv(dev
);
1058 memset(priv
, 0, sizeof(struct mlx4_en_priv
));
1061 priv
->ddev
= &mdev
->pdev
->dev
;
1064 priv
->port_up
= false;
1065 priv
->flags
= prof
->flags
;
1066 priv
->ctrl_flags
= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE
|
1067 MLX4_WQE_CTRL_SOLICITED
);
1068 priv
->tx_ring_num
= prof
->tx_ring_num
;
1069 priv
->rx_ring_num
= prof
->rx_ring_num
;
1070 priv
->mac_index
= -1;
1071 priv
->msg_enable
= MLX4_EN_MSG_LEVEL
;
1072 spin_lock_init(&priv
->stats_lock
);
1073 INIT_WORK(&priv
->mcast_task
, mlx4_en_do_set_multicast
);
1074 INIT_WORK(&priv
->mac_task
, mlx4_en_do_set_mac
);
1075 INIT_WORK(&priv
->watchdog_task
, mlx4_en_restart
);
1076 INIT_WORK(&priv
->linkstate_task
, mlx4_en_linkstate
);
1077 INIT_DELAYED_WORK(&priv
->stats_task
, mlx4_en_do_get_stats
);
1079 /* Query for default mac and max mtu */
1080 priv
->max_mtu
= mdev
->dev
->caps
.eth_mtu_cap
[priv
->port
];
1081 priv
->mac
= mdev
->dev
->caps
.def_mac
[priv
->port
];
1082 if (ILLEGAL_MAC(priv
->mac
)) {
1083 en_err(priv
, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
1084 priv
->port
, priv
->mac
);
1089 priv
->stride
= roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc
) +
1090 DS_SIZE
* MLX4_EN_MAX_RX_FRAGS
);
1091 err
= mlx4_en_alloc_resources(priv
);
1095 /* Allocate page for receive rings */
1096 err
= mlx4_alloc_hwq_res(mdev
->dev
, &priv
->res
,
1097 MLX4_EN_PAGE_SIZE
, MLX4_EN_PAGE_SIZE
);
1099 en_err(priv
, "Failed to allocate page for rx qps\n");
1102 priv
->allocated
= 1;
1105 * Initialize netdev entry points
1107 dev
->netdev_ops
= &mlx4_netdev_ops
;
1108 dev
->watchdog_timeo
= MLX4_EN_WATCHDOG_TIMEOUT
;
1109 netif_set_real_num_tx_queues(dev
, priv
->tx_ring_num
);
1110 netif_set_real_num_rx_queues(dev
, priv
->rx_ring_num
);
1112 SET_ETHTOOL_OPS(dev
, &mlx4_en_ethtool_ops
);
1114 /* Set defualt MAC */
1115 dev
->addr_len
= ETH_ALEN
;
1116 for (i
= 0; i
< ETH_ALEN
; i
++) {
1117 dev
->dev_addr
[ETH_ALEN
- 1 - i
] = (u8
) (priv
->mac
>> (8 * i
));
1118 dev
->perm_addr
[ETH_ALEN
- 1 - i
] = (u8
) (priv
->mac
>> (8 * i
));
1122 * Set driver features
1124 dev
->hw_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
1125 if (mdev
->LSO_support
)
1126 dev
->hw_features
|= NETIF_F_TSO
| NETIF_F_TSO6
;
1128 dev
->vlan_features
= dev
->hw_features
;
1130 dev
->hw_features
|= NETIF_F_RXCSUM
| NETIF_F_RXHASH
;
1131 dev
->features
= dev
->hw_features
| NETIF_F_HIGHDMA
|
1132 NETIF_F_HW_VLAN_TX
| NETIF_F_HW_VLAN_RX
|
1133 NETIF_F_HW_VLAN_FILTER
;
1134 dev
->hw_features
|= NETIF_F_LOOPBACK
;
1136 mdev
->pndev
[port
] = dev
;
1138 netif_carrier_off(dev
);
1139 err
= register_netdev(dev
);
1141 en_err(priv
, "Netdev registration failed for port %d\n", port
);
1144 priv
->registered
= 1;
1146 en_warn(priv
, "Using %d TX rings\n", prof
->tx_ring_num
);
1147 en_warn(priv
, "Using %d RX rings\n", prof
->rx_ring_num
);
1149 /* Configure port */
1150 err
= mlx4_SET_PORT_general(mdev
->dev
, priv
->port
,
1154 en_err(priv
, "Failed setting port general configurations "
1155 "for port %d, with error %d\n", priv
->port
, err
);
1160 en_warn(priv
, "Initializing port\n");
1161 err
= mlx4_INIT_PORT(mdev
->dev
, priv
->port
);
1163 en_err(priv
, "Failed Initializing port\n");
1166 mlx4_en_set_default_moderation(priv
);
1167 queue_delayed_work(mdev
->workqueue
, &priv
->stats_task
, STATS_DELAY
);
1171 mlx4_en_destroy_netdev(dev
);