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>
39 #include <linux/hash.h>
41 #include <net/busy_poll.h>
42 #include <net/vxlan.h>
44 #include <linux/mlx4/driver.h>
45 #include <linux/mlx4/device.h>
46 #include <linux/mlx4/cmd.h>
47 #include <linux/mlx4/cq.h>
52 int mlx4_en_setup_tc(struct net_device
*dev
, u8 up
)
54 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
56 unsigned int offset
= 0;
58 if (up
&& up
!= MLX4_EN_NUM_UP
)
61 netdev_set_num_tc(dev
, up
);
63 /* Partition Tx queues evenly amongst UP's */
64 for (i
= 0; i
< up
; i
++) {
65 netdev_set_tc_queue(dev
, i
, priv
->num_tx_rings_p_up
, offset
);
66 offset
+= priv
->num_tx_rings_p_up
;
72 #ifdef CONFIG_RFS_ACCEL
74 struct mlx4_en_filter
{
75 struct list_head next
;
76 struct work_struct work
;
85 struct mlx4_en_priv
*priv
;
86 u32 flow_id
; /* RFS infrastructure id */
87 int id
; /* mlx4_en driver id */
88 u64 reg_id
; /* Flow steering API id */
89 u8 activated
; /* Used to prevent expiry before filter
92 struct hlist_node filter_chain
;
95 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv
*priv
);
97 static enum mlx4_net_trans_rule_id
mlx4_ip_proto_to_trans_rule_id(u8 ip_proto
)
101 return MLX4_NET_TRANS_RULE_ID_UDP
;
103 return MLX4_NET_TRANS_RULE_ID_TCP
;
105 return MLX4_NET_TRANS_RULE_NUM
;
109 static void mlx4_en_filter_work(struct work_struct
*work
)
111 struct mlx4_en_filter
*filter
= container_of(work
,
112 struct mlx4_en_filter
,
114 struct mlx4_en_priv
*priv
= filter
->priv
;
115 struct mlx4_spec_list spec_tcp_udp
= {
116 .id
= mlx4_ip_proto_to_trans_rule_id(filter
->ip_proto
),
119 .dst_port
= filter
->dst_port
,
120 .dst_port_msk
= (__force __be16
)-1,
121 .src_port
= filter
->src_port
,
122 .src_port_msk
= (__force __be16
)-1,
126 struct mlx4_spec_list spec_ip
= {
127 .id
= MLX4_NET_TRANS_RULE_ID_IPV4
,
130 .dst_ip
= filter
->dst_ip
,
131 .dst_ip_msk
= (__force __be32
)-1,
132 .src_ip
= filter
->src_ip
,
133 .src_ip_msk
= (__force __be32
)-1,
137 struct mlx4_spec_list spec_eth
= {
138 .id
= MLX4_NET_TRANS_RULE_ID_ETH
,
140 struct mlx4_net_trans_rule rule
= {
141 .list
= LIST_HEAD_INIT(rule
.list
),
142 .queue_mode
= MLX4_NET_TRANS_Q_LIFO
,
145 .promisc_mode
= MLX4_FS_REGULAR
,
147 .priority
= MLX4_DOMAIN_RFS
,
150 __be64 mac_mask
= cpu_to_be64(MLX4_MAC_MASK
<< 16);
152 if (spec_tcp_udp
.id
>= MLX4_NET_TRANS_RULE_NUM
) {
153 en_warn(priv
, "RFS: ignoring unsupported ip protocol (%d)\n",
157 list_add_tail(&spec_eth
.list
, &rule
.list
);
158 list_add_tail(&spec_ip
.list
, &rule
.list
);
159 list_add_tail(&spec_tcp_udp
.list
, &rule
.list
);
161 rule
.qpn
= priv
->rss_map
.qps
[filter
->rxq_index
].qpn
;
162 memcpy(spec_eth
.eth
.dst_mac
, priv
->dev
->dev_addr
, ETH_ALEN
);
163 memcpy(spec_eth
.eth
.dst_mac_msk
, &mac_mask
, ETH_ALEN
);
165 filter
->activated
= 0;
167 if (filter
->reg_id
) {
168 rc
= mlx4_flow_detach(priv
->mdev
->dev
, filter
->reg_id
);
169 if (rc
&& rc
!= -ENOENT
)
170 en_err(priv
, "Error detaching flow. rc = %d\n", rc
);
173 rc
= mlx4_flow_attach(priv
->mdev
->dev
, &rule
, &filter
->reg_id
);
175 en_err(priv
, "Error attaching flow. err = %d\n", rc
);
178 mlx4_en_filter_rfs_expire(priv
);
180 filter
->activated
= 1;
183 static inline struct hlist_head
*
184 filter_hash_bucket(struct mlx4_en_priv
*priv
, __be32 src_ip
, __be32 dst_ip
,
185 __be16 src_port
, __be16 dst_port
)
190 l
= (__force
unsigned long)src_port
|
191 ((__force
unsigned long)dst_port
<< 2);
192 l
^= (__force
unsigned long)(src_ip
^ dst_ip
);
194 bucket_idx
= hash_long(l
, MLX4_EN_FILTER_HASH_SHIFT
);
196 return &priv
->filter_hash
[bucket_idx
];
199 static struct mlx4_en_filter
*
200 mlx4_en_filter_alloc(struct mlx4_en_priv
*priv
, int rxq_index
, __be32 src_ip
,
201 __be32 dst_ip
, u8 ip_proto
, __be16 src_port
,
202 __be16 dst_port
, u32 flow_id
)
204 struct mlx4_en_filter
*filter
= NULL
;
206 filter
= kzalloc(sizeof(struct mlx4_en_filter
), GFP_ATOMIC
);
211 filter
->rxq_index
= rxq_index
;
212 INIT_WORK(&filter
->work
, mlx4_en_filter_work
);
214 filter
->src_ip
= src_ip
;
215 filter
->dst_ip
= dst_ip
;
216 filter
->ip_proto
= ip_proto
;
217 filter
->src_port
= src_port
;
218 filter
->dst_port
= dst_port
;
220 filter
->flow_id
= flow_id
;
222 filter
->id
= priv
->last_filter_id
++ % RPS_NO_FILTER
;
224 list_add_tail(&filter
->next
, &priv
->filters
);
225 hlist_add_head(&filter
->filter_chain
,
226 filter_hash_bucket(priv
, src_ip
, dst_ip
, src_port
,
232 static void mlx4_en_filter_free(struct mlx4_en_filter
*filter
)
234 struct mlx4_en_priv
*priv
= filter
->priv
;
237 list_del(&filter
->next
);
239 rc
= mlx4_flow_detach(priv
->mdev
->dev
, filter
->reg_id
);
240 if (rc
&& rc
!= -ENOENT
)
241 en_err(priv
, "Error detaching flow. rc = %d\n", rc
);
246 static inline struct mlx4_en_filter
*
247 mlx4_en_filter_find(struct mlx4_en_priv
*priv
, __be32 src_ip
, __be32 dst_ip
,
248 u8 ip_proto
, __be16 src_port
, __be16 dst_port
)
250 struct mlx4_en_filter
*filter
;
251 struct mlx4_en_filter
*ret
= NULL
;
253 hlist_for_each_entry(filter
,
254 filter_hash_bucket(priv
, src_ip
, dst_ip
,
257 if (filter
->src_ip
== src_ip
&&
258 filter
->dst_ip
== dst_ip
&&
259 filter
->ip_proto
== ip_proto
&&
260 filter
->src_port
== src_port
&&
261 filter
->dst_port
== dst_port
) {
271 mlx4_en_filter_rfs(struct net_device
*net_dev
, const struct sk_buff
*skb
,
272 u16 rxq_index
, u32 flow_id
)
274 struct mlx4_en_priv
*priv
= netdev_priv(net_dev
);
275 struct mlx4_en_filter
*filter
;
276 const struct iphdr
*ip
;
283 int nhoff
= skb_network_offset(skb
);
286 if (skb
->protocol
!= htons(ETH_P_IP
))
287 return -EPROTONOSUPPORT
;
289 ip
= (const struct iphdr
*)(skb
->data
+ nhoff
);
290 if (ip_is_fragment(ip
))
291 return -EPROTONOSUPPORT
;
293 if ((ip
->protocol
!= IPPROTO_TCP
) && (ip
->protocol
!= IPPROTO_UDP
))
294 return -EPROTONOSUPPORT
;
295 ports
= (const __be16
*)(skb
->data
+ nhoff
+ 4 * ip
->ihl
);
297 ip_proto
= ip
->protocol
;
303 spin_lock_bh(&priv
->filters_lock
);
304 filter
= mlx4_en_filter_find(priv
, src_ip
, dst_ip
, ip_proto
,
307 if (filter
->rxq_index
== rxq_index
)
310 filter
->rxq_index
= rxq_index
;
312 filter
= mlx4_en_filter_alloc(priv
, rxq_index
,
313 src_ip
, dst_ip
, ip_proto
,
314 src_port
, dst_port
, flow_id
);
321 queue_work(priv
->mdev
->workqueue
, &filter
->work
);
326 spin_unlock_bh(&priv
->filters_lock
);
331 void mlx4_en_cleanup_filters(struct mlx4_en_priv
*priv
)
333 struct mlx4_en_filter
*filter
, *tmp
;
336 spin_lock_bh(&priv
->filters_lock
);
337 list_for_each_entry_safe(filter
, tmp
, &priv
->filters
, next
) {
338 list_move(&filter
->next
, &del_list
);
339 hlist_del(&filter
->filter_chain
);
341 spin_unlock_bh(&priv
->filters_lock
);
343 list_for_each_entry_safe(filter
, tmp
, &del_list
, next
) {
344 cancel_work_sync(&filter
->work
);
345 mlx4_en_filter_free(filter
);
349 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv
*priv
)
351 struct mlx4_en_filter
*filter
= NULL
, *tmp
, *last_filter
= NULL
;
355 spin_lock_bh(&priv
->filters_lock
);
356 list_for_each_entry_safe(filter
, tmp
, &priv
->filters
, next
) {
357 if (i
> MLX4_EN_FILTER_EXPIRY_QUOTA
)
360 if (filter
->activated
&&
361 !work_pending(&filter
->work
) &&
362 rps_may_expire_flow(priv
->dev
,
363 filter
->rxq_index
, filter
->flow_id
,
365 list_move(&filter
->next
, &del_list
);
366 hlist_del(&filter
->filter_chain
);
368 last_filter
= filter
;
373 if (last_filter
&& (&last_filter
->next
!= priv
->filters
.next
))
374 list_move(&priv
->filters
, &last_filter
->next
);
376 spin_unlock_bh(&priv
->filters_lock
);
378 list_for_each_entry_safe(filter
, tmp
, &del_list
, next
)
379 mlx4_en_filter_free(filter
);
383 static int mlx4_en_vlan_rx_add_vid(struct net_device
*dev
,
384 __be16 proto
, u16 vid
)
386 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
387 struct mlx4_en_dev
*mdev
= priv
->mdev
;
391 en_dbg(HW
, priv
, "adding VLAN:%d\n", vid
);
393 set_bit(vid
, priv
->active_vlans
);
395 /* Add VID to port VLAN filter */
396 mutex_lock(&mdev
->state_lock
);
397 if (mdev
->device_up
&& priv
->port_up
) {
398 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
);
400 en_err(priv
, "Failed configuring VLAN filter\n");
402 if (mlx4_register_vlan(mdev
->dev
, priv
->port
, vid
, &idx
))
403 en_dbg(HW
, priv
, "failed adding vlan %d\n", vid
);
404 mutex_unlock(&mdev
->state_lock
);
409 static int mlx4_en_vlan_rx_kill_vid(struct net_device
*dev
,
410 __be16 proto
, u16 vid
)
412 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
413 struct mlx4_en_dev
*mdev
= priv
->mdev
;
416 en_dbg(HW
, priv
, "Killing VID:%d\n", vid
);
418 clear_bit(vid
, priv
->active_vlans
);
420 /* Remove VID from port VLAN filter */
421 mutex_lock(&mdev
->state_lock
);
422 mlx4_unregister_vlan(mdev
->dev
, priv
->port
, vid
);
424 if (mdev
->device_up
&& priv
->port_up
) {
425 err
= mlx4_SET_VLAN_FLTR(mdev
->dev
, priv
);
427 en_err(priv
, "Failed configuring VLAN filter\n");
429 mutex_unlock(&mdev
->state_lock
);
434 static void mlx4_en_u64_to_mac(unsigned char dst_mac
[ETH_ALEN
+ 2], u64 src_mac
)
437 for (i
= ETH_ALEN
- 1; i
>= 0; --i
) {
438 dst_mac
[i
] = src_mac
& 0xff;
441 memset(&dst_mac
[ETH_ALEN
], 0, 2);
445 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv
*priv
, unsigned char *addr
,
446 int qpn
, u64
*reg_id
)
450 if (priv
->mdev
->dev
->caps
.tunnel_offload_mode
!= MLX4_TUNNEL_OFFLOAD_MODE_VXLAN
||
451 priv
->mdev
->dev
->caps
.dmfs_high_steer_mode
== MLX4_STEERING_DMFS_A0_STATIC
)
452 return 0; /* do nothing */
454 err
= mlx4_tunnel_steer_add(priv
->mdev
->dev
, addr
, priv
->port
, qpn
,
455 MLX4_DOMAIN_NIC
, reg_id
);
457 en_err(priv
, "failed to add vxlan steering rule, err %d\n", err
);
460 en_dbg(DRV
, priv
, "added vxlan steering rule, mac %pM reg_id %llx\n", addr
, *reg_id
);
465 static int mlx4_en_uc_steer_add(struct mlx4_en_priv
*priv
,
466 unsigned char *mac
, int *qpn
, u64
*reg_id
)
468 struct mlx4_en_dev
*mdev
= priv
->mdev
;
469 struct mlx4_dev
*dev
= mdev
->dev
;
472 switch (dev
->caps
.steering_mode
) {
473 case MLX4_STEERING_MODE_B0
: {
478 memcpy(&gid
[10], mac
, ETH_ALEN
);
481 err
= mlx4_unicast_attach(dev
, &qp
, gid
, 0, MLX4_PROT_ETH
);
484 case MLX4_STEERING_MODE_DEVICE_MANAGED
: {
485 struct mlx4_spec_list spec_eth
= { {NULL
} };
486 __be64 mac_mask
= cpu_to_be64(MLX4_MAC_MASK
<< 16);
488 struct mlx4_net_trans_rule rule
= {
489 .queue_mode
= MLX4_NET_TRANS_Q_FIFO
,
492 .promisc_mode
= MLX4_FS_REGULAR
,
493 .priority
= MLX4_DOMAIN_NIC
,
496 rule
.port
= priv
->port
;
498 INIT_LIST_HEAD(&rule
.list
);
500 spec_eth
.id
= MLX4_NET_TRANS_RULE_ID_ETH
;
501 memcpy(spec_eth
.eth
.dst_mac
, mac
, ETH_ALEN
);
502 memcpy(spec_eth
.eth
.dst_mac_msk
, &mac_mask
, ETH_ALEN
);
503 list_add_tail(&spec_eth
.list
, &rule
.list
);
505 err
= mlx4_flow_attach(dev
, &rule
, reg_id
);
512 en_warn(priv
, "Failed Attaching Unicast\n");
517 static void mlx4_en_uc_steer_release(struct mlx4_en_priv
*priv
,
518 unsigned char *mac
, int qpn
, u64 reg_id
)
520 struct mlx4_en_dev
*mdev
= priv
->mdev
;
521 struct mlx4_dev
*dev
= mdev
->dev
;
523 switch (dev
->caps
.steering_mode
) {
524 case MLX4_STEERING_MODE_B0
: {
529 memcpy(&gid
[10], mac
, ETH_ALEN
);
532 mlx4_unicast_detach(dev
, &qp
, gid
, MLX4_PROT_ETH
);
535 case MLX4_STEERING_MODE_DEVICE_MANAGED
: {
536 mlx4_flow_detach(dev
, reg_id
);
540 en_err(priv
, "Invalid steering mode.\n");
544 static int mlx4_en_get_qp(struct mlx4_en_priv
*priv
)
546 struct mlx4_en_dev
*mdev
= priv
->mdev
;
547 struct mlx4_dev
*dev
= mdev
->dev
;
550 int *qpn
= &priv
->base_qpn
;
551 u64 mac
= mlx4_mac_to_u64(priv
->dev
->dev_addr
);
553 en_dbg(DRV
, priv
, "Registering MAC: %pM for adding\n",
554 priv
->dev
->dev_addr
);
555 index
= mlx4_register_mac(dev
, priv
->port
, mac
);
558 en_err(priv
, "Failed adding MAC: %pM\n",
559 priv
->dev
->dev_addr
);
563 if (dev
->caps
.steering_mode
== MLX4_STEERING_MODE_A0
) {
564 int base_qpn
= mlx4_get_base_qpn(dev
, priv
->port
);
565 *qpn
= base_qpn
+ index
;
569 err
= mlx4_qp_reserve_range(dev
, 1, 1, qpn
, MLX4_RESERVE_A0_QP
);
570 en_dbg(DRV
, priv
, "Reserved qp %d\n", *qpn
);
572 en_err(priv
, "Failed to reserve qp for mac registration\n");
573 mlx4_unregister_mac(dev
, priv
->port
, mac
);
580 static void mlx4_en_put_qp(struct mlx4_en_priv
*priv
)
582 struct mlx4_en_dev
*mdev
= priv
->mdev
;
583 struct mlx4_dev
*dev
= mdev
->dev
;
584 int qpn
= priv
->base_qpn
;
586 if (dev
->caps
.steering_mode
== MLX4_STEERING_MODE_A0
) {
587 u64 mac
= mlx4_mac_to_u64(priv
->dev
->dev_addr
);
588 en_dbg(DRV
, priv
, "Registering MAC: %pM for deleting\n",
589 priv
->dev
->dev_addr
);
590 mlx4_unregister_mac(dev
, priv
->port
, mac
);
592 en_dbg(DRV
, priv
, "Releasing qp: port %d, qpn %d\n",
594 mlx4_qp_release_range(dev
, qpn
, 1);
595 priv
->flags
&= ~MLX4_EN_FLAG_FORCE_PROMISC
;
599 static int mlx4_en_replace_mac(struct mlx4_en_priv
*priv
, int qpn
,
600 unsigned char *new_mac
, unsigned char *prev_mac
)
602 struct mlx4_en_dev
*mdev
= priv
->mdev
;
603 struct mlx4_dev
*dev
= mdev
->dev
;
605 u64 new_mac_u64
= mlx4_mac_to_u64(new_mac
);
607 if (dev
->caps
.steering_mode
!= MLX4_STEERING_MODE_A0
) {
608 struct hlist_head
*bucket
;
609 unsigned int mac_hash
;
610 struct mlx4_mac_entry
*entry
;
611 struct hlist_node
*tmp
;
612 u64 prev_mac_u64
= mlx4_mac_to_u64(prev_mac
);
614 bucket
= &priv
->mac_hash
[prev_mac
[MLX4_EN_MAC_HASH_IDX
]];
615 hlist_for_each_entry_safe(entry
, tmp
, bucket
, hlist
) {
616 if (ether_addr_equal_64bits(entry
->mac
, prev_mac
)) {
617 mlx4_en_uc_steer_release(priv
, entry
->mac
,
619 mlx4_unregister_mac(dev
, priv
->port
,
621 hlist_del_rcu(&entry
->hlist
);
623 memcpy(entry
->mac
, new_mac
, ETH_ALEN
);
625 mac_hash
= new_mac
[MLX4_EN_MAC_HASH_IDX
];
626 hlist_add_head_rcu(&entry
->hlist
,
627 &priv
->mac_hash
[mac_hash
]);
628 mlx4_register_mac(dev
, priv
->port
, new_mac_u64
);
629 err
= mlx4_en_uc_steer_add(priv
, new_mac
,
634 if (priv
->tunnel_reg_id
) {
635 mlx4_flow_detach(priv
->mdev
->dev
, priv
->tunnel_reg_id
);
636 priv
->tunnel_reg_id
= 0;
638 err
= mlx4_en_tunnel_steer_add(priv
, new_mac
, qpn
,
639 &priv
->tunnel_reg_id
);
646 return __mlx4_replace_mac(dev
, priv
->port
, qpn
, new_mac_u64
);
649 static int mlx4_en_do_set_mac(struct mlx4_en_priv
*priv
,
650 unsigned char new_mac
[ETH_ALEN
+ 2])
655 /* Remove old MAC and insert the new one */
656 err
= mlx4_en_replace_mac(priv
, priv
->base_qpn
,
657 new_mac
, priv
->current_mac
);
659 en_err(priv
, "Failed changing HW MAC address\n");
661 en_dbg(HW
, priv
, "Port is down while registering mac, exiting...\n");
664 memcpy(priv
->current_mac
, new_mac
, sizeof(priv
->current_mac
));
669 static int mlx4_en_set_mac(struct net_device
*dev
, void *addr
)
671 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
672 struct mlx4_en_dev
*mdev
= priv
->mdev
;
673 struct sockaddr
*saddr
= addr
;
674 unsigned char new_mac
[ETH_ALEN
+ 2];
677 if (!is_valid_ether_addr(saddr
->sa_data
))
678 return -EADDRNOTAVAIL
;
680 mutex_lock(&mdev
->state_lock
);
681 memcpy(new_mac
, saddr
->sa_data
, ETH_ALEN
);
682 err
= mlx4_en_do_set_mac(priv
, new_mac
);
684 memcpy(dev
->dev_addr
, saddr
->sa_data
, ETH_ALEN
);
685 mutex_unlock(&mdev
->state_lock
);
690 static void mlx4_en_clear_list(struct net_device
*dev
)
692 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
693 struct mlx4_en_mc_list
*tmp
, *mc_to_del
;
695 list_for_each_entry_safe(mc_to_del
, tmp
, &priv
->mc_list
, list
) {
696 list_del(&mc_to_del
->list
);
701 static void mlx4_en_cache_mclist(struct net_device
*dev
)
703 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
704 struct netdev_hw_addr
*ha
;
705 struct mlx4_en_mc_list
*tmp
;
707 mlx4_en_clear_list(dev
);
708 netdev_for_each_mc_addr(ha
, dev
) {
709 tmp
= kzalloc(sizeof(struct mlx4_en_mc_list
), GFP_ATOMIC
);
711 mlx4_en_clear_list(dev
);
714 memcpy(tmp
->addr
, ha
->addr
, ETH_ALEN
);
715 list_add_tail(&tmp
->list
, &priv
->mc_list
);
719 static void update_mclist_flags(struct mlx4_en_priv
*priv
,
720 struct list_head
*dst
,
721 struct list_head
*src
)
723 struct mlx4_en_mc_list
*dst_tmp
, *src_tmp
, *new_mc
;
726 /* Find all the entries that should be removed from dst,
727 * These are the entries that are not found in src
729 list_for_each_entry(dst_tmp
, dst
, list
) {
731 list_for_each_entry(src_tmp
, src
, list
) {
732 if (ether_addr_equal(dst_tmp
->addr
, src_tmp
->addr
)) {
738 dst_tmp
->action
= MCLIST_REM
;
741 /* Add entries that exist in src but not in dst
742 * mark them as need to add
744 list_for_each_entry(src_tmp
, src
, list
) {
746 list_for_each_entry(dst_tmp
, dst
, list
) {
747 if (ether_addr_equal(dst_tmp
->addr
, src_tmp
->addr
)) {
748 dst_tmp
->action
= MCLIST_NONE
;
754 new_mc
= kmemdup(src_tmp
,
755 sizeof(struct mlx4_en_mc_list
),
760 new_mc
->action
= MCLIST_ADD
;
761 list_add_tail(&new_mc
->list
, dst
);
766 static void mlx4_en_set_rx_mode(struct net_device
*dev
)
768 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
773 queue_work(priv
->mdev
->workqueue
, &priv
->rx_mode_task
);
776 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv
*priv
,
777 struct mlx4_en_dev
*mdev
)
781 if (!(priv
->flags
& MLX4_EN_FLAG_PROMISC
)) {
782 if (netif_msg_rx_status(priv
))
783 en_warn(priv
, "Entering promiscuous mode\n");
784 priv
->flags
|= MLX4_EN_FLAG_PROMISC
;
786 /* Enable promiscouos mode */
787 switch (mdev
->dev
->caps
.steering_mode
) {
788 case MLX4_STEERING_MODE_DEVICE_MANAGED
:
789 err
= mlx4_flow_steer_promisc_add(mdev
->dev
,
792 MLX4_FS_ALL_DEFAULT
);
794 en_err(priv
, "Failed enabling promiscuous mode\n");
795 priv
->flags
|= MLX4_EN_FLAG_MC_PROMISC
;
798 case MLX4_STEERING_MODE_B0
:
799 err
= mlx4_unicast_promisc_add(mdev
->dev
,
803 en_err(priv
, "Failed enabling unicast promiscuous mode\n");
805 /* Add the default qp number as multicast
808 if (!(priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
)) {
809 err
= mlx4_multicast_promisc_add(mdev
->dev
,
813 en_err(priv
, "Failed enabling multicast promiscuous mode\n");
814 priv
->flags
|= MLX4_EN_FLAG_MC_PROMISC
;
818 case MLX4_STEERING_MODE_A0
:
819 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
,
824 en_err(priv
, "Failed enabling promiscuous mode\n");
828 /* Disable port multicast filter (unconditionally) */
829 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
830 0, MLX4_MCAST_DISABLE
);
832 en_err(priv
, "Failed disabling multicast filter\n");
836 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv
*priv
,
837 struct mlx4_en_dev
*mdev
)
841 if (netif_msg_rx_status(priv
))
842 en_warn(priv
, "Leaving promiscuous mode\n");
843 priv
->flags
&= ~MLX4_EN_FLAG_PROMISC
;
845 /* Disable promiscouos mode */
846 switch (mdev
->dev
->caps
.steering_mode
) {
847 case MLX4_STEERING_MODE_DEVICE_MANAGED
:
848 err
= mlx4_flow_steer_promisc_remove(mdev
->dev
,
850 MLX4_FS_ALL_DEFAULT
);
852 en_err(priv
, "Failed disabling promiscuous mode\n");
853 priv
->flags
&= ~MLX4_EN_FLAG_MC_PROMISC
;
856 case MLX4_STEERING_MODE_B0
:
857 err
= mlx4_unicast_promisc_remove(mdev
->dev
,
861 en_err(priv
, "Failed disabling unicast promiscuous mode\n");
862 /* Disable Multicast promisc */
863 if (priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
) {
864 err
= mlx4_multicast_promisc_remove(mdev
->dev
,
868 en_err(priv
, "Failed disabling multicast promiscuous mode\n");
869 priv
->flags
&= ~MLX4_EN_FLAG_MC_PROMISC
;
873 case MLX4_STEERING_MODE_A0
:
874 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
,
878 en_err(priv
, "Failed disabling promiscuous mode\n");
883 static void mlx4_en_do_multicast(struct mlx4_en_priv
*priv
,
884 struct net_device
*dev
,
885 struct mlx4_en_dev
*mdev
)
887 struct mlx4_en_mc_list
*mclist
, *tmp
;
889 u8 mc_list
[16] = {0};
892 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
893 if (dev
->flags
& IFF_ALLMULTI
) {
894 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
895 0, MLX4_MCAST_DISABLE
);
897 en_err(priv
, "Failed disabling multicast filter\n");
899 /* Add the default qp number as multicast promisc */
900 if (!(priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
)) {
901 switch (mdev
->dev
->caps
.steering_mode
) {
902 case MLX4_STEERING_MODE_DEVICE_MANAGED
:
903 err
= mlx4_flow_steer_promisc_add(mdev
->dev
,
909 case MLX4_STEERING_MODE_B0
:
910 err
= mlx4_multicast_promisc_add(mdev
->dev
,
915 case MLX4_STEERING_MODE_A0
:
919 en_err(priv
, "Failed entering multicast promisc mode\n");
920 priv
->flags
|= MLX4_EN_FLAG_MC_PROMISC
;
923 /* Disable Multicast promisc */
924 if (priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
) {
925 switch (mdev
->dev
->caps
.steering_mode
) {
926 case MLX4_STEERING_MODE_DEVICE_MANAGED
:
927 err
= mlx4_flow_steer_promisc_remove(mdev
->dev
,
932 case MLX4_STEERING_MODE_B0
:
933 err
= mlx4_multicast_promisc_remove(mdev
->dev
,
938 case MLX4_STEERING_MODE_A0
:
942 en_err(priv
, "Failed disabling multicast promiscuous mode\n");
943 priv
->flags
&= ~MLX4_EN_FLAG_MC_PROMISC
;
946 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
947 0, MLX4_MCAST_DISABLE
);
949 en_err(priv
, "Failed disabling multicast filter\n");
951 /* Flush mcast filter and init it with broadcast address */
952 mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, ETH_BCAST
,
953 1, MLX4_MCAST_CONFIG
);
955 /* Update multicast list - we cache all addresses so they won't
956 * change while HW is updated holding the command semaphor */
957 netif_addr_lock_bh(dev
);
958 mlx4_en_cache_mclist(dev
);
959 netif_addr_unlock_bh(dev
);
960 list_for_each_entry(mclist
, &priv
->mc_list
, list
) {
961 mcast_addr
= mlx4_mac_to_u64(mclist
->addr
);
962 mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
,
963 mcast_addr
, 0, MLX4_MCAST_CONFIG
);
965 err
= mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0,
966 0, MLX4_MCAST_ENABLE
);
968 en_err(priv
, "Failed enabling multicast filter\n");
970 update_mclist_flags(priv
, &priv
->curr_list
, &priv
->mc_list
);
971 list_for_each_entry_safe(mclist
, tmp
, &priv
->curr_list
, list
) {
972 if (mclist
->action
== MCLIST_REM
) {
973 /* detach this address and delete from list */
974 memcpy(&mc_list
[10], mclist
->addr
, ETH_ALEN
);
975 mc_list
[5] = priv
->port
;
976 err
= mlx4_multicast_detach(mdev
->dev
,
977 &priv
->rss_map
.indir_qp
,
982 en_err(priv
, "Fail to detach multicast address\n");
984 if (mclist
->tunnel_reg_id
) {
985 err
= mlx4_flow_detach(priv
->mdev
->dev
, mclist
->tunnel_reg_id
);
987 en_err(priv
, "Failed to detach multicast address\n");
990 /* remove from list */
991 list_del(&mclist
->list
);
993 } else if (mclist
->action
== MCLIST_ADD
) {
994 /* attach the address */
995 memcpy(&mc_list
[10], mclist
->addr
, ETH_ALEN
);
996 /* needed for B0 steering support */
997 mc_list
[5] = priv
->port
;
998 err
= mlx4_multicast_attach(mdev
->dev
,
999 &priv
->rss_map
.indir_qp
,
1005 en_err(priv
, "Fail to attach multicast address\n");
1007 err
= mlx4_en_tunnel_steer_add(priv
, &mc_list
[10], priv
->base_qpn
,
1008 &mclist
->tunnel_reg_id
);
1010 en_err(priv
, "Failed to attach multicast address\n");
1016 static void mlx4_en_do_uc_filter(struct mlx4_en_priv
*priv
,
1017 struct net_device
*dev
,
1018 struct mlx4_en_dev
*mdev
)
1020 struct netdev_hw_addr
*ha
;
1021 struct mlx4_mac_entry
*entry
;
1022 struct hlist_node
*tmp
;
1026 struct hlist_head
*bucket
;
1031 /* Note that we do not need to protect our mac_hash traversal with rcu,
1032 * since all modification code is protected by mdev->state_lock
1035 /* find what to remove */
1036 for (i
= 0; i
< MLX4_EN_MAC_HASH_SIZE
; ++i
) {
1037 bucket
= &priv
->mac_hash
[i
];
1038 hlist_for_each_entry_safe(entry
, tmp
, bucket
, hlist
) {
1040 netdev_for_each_uc_addr(ha
, dev
) {
1041 if (ether_addr_equal_64bits(entry
->mac
,
1048 /* MAC address of the port is not in uc list */
1049 if (ether_addr_equal_64bits(entry
->mac
,
1054 mac
= mlx4_mac_to_u64(entry
->mac
);
1055 mlx4_en_uc_steer_release(priv
, entry
->mac
,
1058 mlx4_unregister_mac(mdev
->dev
, priv
->port
, mac
);
1060 hlist_del_rcu(&entry
->hlist
);
1061 kfree_rcu(entry
, rcu
);
1062 en_dbg(DRV
, priv
, "Removed MAC %pM on port:%d\n",
1063 entry
->mac
, priv
->port
);
1069 /* if we didn't remove anything, there is no use in trying to add
1070 * again once we are in a forced promisc mode state
1072 if ((priv
->flags
& MLX4_EN_FLAG_FORCE_PROMISC
) && 0 == removed
)
1075 prev_flags
= priv
->flags
;
1076 priv
->flags
&= ~MLX4_EN_FLAG_FORCE_PROMISC
;
1078 /* find what to add */
1079 netdev_for_each_uc_addr(ha
, dev
) {
1081 bucket
= &priv
->mac_hash
[ha
->addr
[MLX4_EN_MAC_HASH_IDX
]];
1082 hlist_for_each_entry(entry
, bucket
, hlist
) {
1083 if (ether_addr_equal_64bits(entry
->mac
, ha
->addr
)) {
1090 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1092 en_err(priv
, "Failed adding MAC %pM on port:%d (out of memory)\n",
1093 ha
->addr
, priv
->port
);
1094 priv
->flags
|= MLX4_EN_FLAG_FORCE_PROMISC
;
1097 mac
= mlx4_mac_to_u64(ha
->addr
);
1098 memcpy(entry
->mac
, ha
->addr
, ETH_ALEN
);
1099 err
= mlx4_register_mac(mdev
->dev
, priv
->port
, mac
);
1101 en_err(priv
, "Failed registering MAC %pM on port %d: %d\n",
1102 ha
->addr
, priv
->port
, err
);
1104 priv
->flags
|= MLX4_EN_FLAG_FORCE_PROMISC
;
1107 err
= mlx4_en_uc_steer_add(priv
, ha
->addr
,
1111 en_err(priv
, "Failed adding MAC %pM on port %d: %d\n",
1112 ha
->addr
, priv
->port
, err
);
1113 mlx4_unregister_mac(mdev
->dev
, priv
->port
, mac
);
1115 priv
->flags
|= MLX4_EN_FLAG_FORCE_PROMISC
;
1118 unsigned int mac_hash
;
1119 en_dbg(DRV
, priv
, "Added MAC %pM on port:%d\n",
1120 ha
->addr
, priv
->port
);
1121 mac_hash
= ha
->addr
[MLX4_EN_MAC_HASH_IDX
];
1122 bucket
= &priv
->mac_hash
[mac_hash
];
1123 hlist_add_head_rcu(&entry
->hlist
, bucket
);
1128 if (priv
->flags
& MLX4_EN_FLAG_FORCE_PROMISC
) {
1129 en_warn(priv
, "Forcing promiscuous mode on port:%d\n",
1131 } else if (prev_flags
& MLX4_EN_FLAG_FORCE_PROMISC
) {
1132 en_warn(priv
, "Stop forcing promiscuous mode on port:%d\n",
1137 static void mlx4_en_do_set_rx_mode(struct work_struct
*work
)
1139 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
1141 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1142 struct net_device
*dev
= priv
->dev
;
1144 mutex_lock(&mdev
->state_lock
);
1145 if (!mdev
->device_up
) {
1146 en_dbg(HW
, priv
, "Card is not up, ignoring rx mode change.\n");
1149 if (!priv
->port_up
) {
1150 en_dbg(HW
, priv
, "Port is down, ignoring rx mode change.\n");
1154 if (!netif_carrier_ok(dev
)) {
1155 if (!mlx4_en_QUERY_PORT(mdev
, priv
->port
)) {
1156 if (priv
->port_state
.link_state
) {
1157 priv
->last_link_state
= MLX4_DEV_EVENT_PORT_UP
;
1158 netif_carrier_on(dev
);
1159 en_dbg(LINK
, priv
, "Link Up\n");
1164 if (dev
->priv_flags
& IFF_UNICAST_FLT
)
1165 mlx4_en_do_uc_filter(priv
, dev
, mdev
);
1167 /* Promsicuous mode: disable all filters */
1168 if ((dev
->flags
& IFF_PROMISC
) ||
1169 (priv
->flags
& MLX4_EN_FLAG_FORCE_PROMISC
)) {
1170 mlx4_en_set_promisc_mode(priv
, mdev
);
1174 /* Not in promiscuous mode */
1175 if (priv
->flags
& MLX4_EN_FLAG_PROMISC
)
1176 mlx4_en_clear_promisc_mode(priv
, mdev
);
1178 mlx4_en_do_multicast(priv
, dev
, mdev
);
1180 mutex_unlock(&mdev
->state_lock
);
1183 #ifdef CONFIG_NET_POLL_CONTROLLER
1184 static void mlx4_en_netpoll(struct net_device
*dev
)
1186 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
1187 struct mlx4_en_cq
*cq
;
1190 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
1191 cq
= priv
->rx_cq
[i
];
1192 napi_schedule(&cq
->napi
);
1197 static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv
*priv
)
1201 int *qpn
= &priv
->base_qpn
;
1202 struct mlx4_mac_entry
*entry
;
1204 err
= mlx4_en_uc_steer_add(priv
, priv
->dev
->dev_addr
, qpn
, ®_id
);
1208 err
= mlx4_en_tunnel_steer_add(priv
, priv
->dev
->dev_addr
, *qpn
,
1209 &priv
->tunnel_reg_id
);
1213 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1219 memcpy(entry
->mac
, priv
->dev
->dev_addr
, sizeof(entry
->mac
));
1220 memcpy(priv
->current_mac
, entry
->mac
, sizeof(priv
->current_mac
));
1221 entry
->reg_id
= reg_id
;
1222 hlist_add_head_rcu(&entry
->hlist
,
1223 &priv
->mac_hash
[entry
->mac
[MLX4_EN_MAC_HASH_IDX
]]);
1228 if (priv
->tunnel_reg_id
)
1229 mlx4_flow_detach(priv
->mdev
->dev
, priv
->tunnel_reg_id
);
1232 mlx4_en_uc_steer_release(priv
, priv
->dev
->dev_addr
, *qpn
, reg_id
);
1236 static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv
*priv
)
1240 int qpn
= priv
->base_qpn
;
1241 struct hlist_head
*bucket
;
1242 struct hlist_node
*tmp
;
1243 struct mlx4_mac_entry
*entry
;
1245 for (i
= 0; i
< MLX4_EN_MAC_HASH_SIZE
; ++i
) {
1246 bucket
= &priv
->mac_hash
[i
];
1247 hlist_for_each_entry_safe(entry
, tmp
, bucket
, hlist
) {
1248 mac
= mlx4_mac_to_u64(entry
->mac
);
1249 en_dbg(DRV
, priv
, "Registering MAC:%pM for deleting\n",
1251 mlx4_en_uc_steer_release(priv
, entry
->mac
,
1252 qpn
, entry
->reg_id
);
1254 mlx4_unregister_mac(priv
->mdev
->dev
, priv
->port
, mac
);
1255 hlist_del_rcu(&entry
->hlist
);
1256 kfree_rcu(entry
, rcu
);
1260 if (priv
->tunnel_reg_id
) {
1261 mlx4_flow_detach(priv
->mdev
->dev
, priv
->tunnel_reg_id
);
1262 priv
->tunnel_reg_id
= 0;
1266 static void mlx4_en_tx_timeout(struct net_device
*dev
)
1268 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
1269 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1272 if (netif_msg_timer(priv
))
1273 en_warn(priv
, "Tx timeout called on port:%d\n", priv
->port
);
1275 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
1276 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev
, i
)))
1278 en_warn(priv
, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1279 i
, priv
->tx_ring
[i
]->qpn
, priv
->tx_ring
[i
]->cqn
,
1280 priv
->tx_ring
[i
]->cons
, priv
->tx_ring
[i
]->prod
);
1283 priv
->port_stats
.tx_timeout
++;
1284 en_dbg(DRV
, priv
, "Scheduling watchdog\n");
1285 queue_work(mdev
->workqueue
, &priv
->watchdog_task
);
1289 static struct net_device_stats
*mlx4_en_get_stats(struct net_device
*dev
)
1291 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
1293 spin_lock_bh(&priv
->stats_lock
);
1294 memcpy(&priv
->ret_stats
, &priv
->stats
, sizeof(priv
->stats
));
1295 spin_unlock_bh(&priv
->stats_lock
);
1297 return &priv
->ret_stats
;
1300 static void mlx4_en_set_default_moderation(struct mlx4_en_priv
*priv
)
1302 struct mlx4_en_cq
*cq
;
1305 /* If we haven't received a specific coalescing setting
1306 * (module param), we set the moderation parameters as follows:
1307 * - moder_cnt is set to the number of mtu sized packets to
1308 * satisfy our coalescing target.
1309 * - moder_time is set to a fixed value.
1311 priv
->rx_frames
= MLX4_EN_RX_COAL_TARGET
;
1312 priv
->rx_usecs
= MLX4_EN_RX_COAL_TIME
;
1313 priv
->tx_frames
= MLX4_EN_TX_COAL_PKTS
;
1314 priv
->tx_usecs
= MLX4_EN_TX_COAL_TIME
;
1315 en_dbg(INTR
, priv
, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1316 priv
->dev
->mtu
, priv
->rx_frames
, priv
->rx_usecs
);
1318 /* Setup cq moderation params */
1319 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
1320 cq
= priv
->rx_cq
[i
];
1321 cq
->moder_cnt
= priv
->rx_frames
;
1322 cq
->moder_time
= priv
->rx_usecs
;
1323 priv
->last_moder_time
[i
] = MLX4_EN_AUTO_CONF
;
1324 priv
->last_moder_packets
[i
] = 0;
1325 priv
->last_moder_bytes
[i
] = 0;
1328 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
1329 cq
= priv
->tx_cq
[i
];
1330 cq
->moder_cnt
= priv
->tx_frames
;
1331 cq
->moder_time
= priv
->tx_usecs
;
1334 /* Reset auto-moderation params */
1335 priv
->pkt_rate_low
= MLX4_EN_RX_RATE_LOW
;
1336 priv
->rx_usecs_low
= MLX4_EN_RX_COAL_TIME_LOW
;
1337 priv
->pkt_rate_high
= MLX4_EN_RX_RATE_HIGH
;
1338 priv
->rx_usecs_high
= MLX4_EN_RX_COAL_TIME_HIGH
;
1339 priv
->sample_interval
= MLX4_EN_SAMPLE_INTERVAL
;
1340 priv
->adaptive_rx_coal
= 1;
1341 priv
->last_moder_jiffies
= 0;
1342 priv
->last_moder_tx_packets
= 0;
1345 static void mlx4_en_auto_moderation(struct mlx4_en_priv
*priv
)
1347 unsigned long period
= (unsigned long) (jiffies
- priv
->last_moder_jiffies
);
1348 struct mlx4_en_cq
*cq
;
1349 unsigned long packets
;
1351 unsigned long avg_pkt_size
;
1352 unsigned long rx_packets
;
1353 unsigned long rx_bytes
;
1354 unsigned long rx_pkt_diff
;
1358 if (!priv
->adaptive_rx_coal
|| period
< priv
->sample_interval
* HZ
)
1361 for (ring
= 0; ring
< priv
->rx_ring_num
; ring
++) {
1362 spin_lock_bh(&priv
->stats_lock
);
1363 rx_packets
= priv
->rx_ring
[ring
]->packets
;
1364 rx_bytes
= priv
->rx_ring
[ring
]->bytes
;
1365 spin_unlock_bh(&priv
->stats_lock
);
1367 rx_pkt_diff
= ((unsigned long) (rx_packets
-
1368 priv
->last_moder_packets
[ring
]));
1369 packets
= rx_pkt_diff
;
1370 rate
= packets
* HZ
/ period
;
1371 avg_pkt_size
= packets
? ((unsigned long) (rx_bytes
-
1372 priv
->last_moder_bytes
[ring
])) / packets
: 0;
1374 /* Apply auto-moderation only when packet rate
1375 * exceeds a rate that it matters */
1376 if (rate
> (MLX4_EN_RX_RATE_THRESH
/ priv
->rx_ring_num
) &&
1377 avg_pkt_size
> MLX4_EN_AVG_PKT_SMALL
) {
1378 if (rate
< priv
->pkt_rate_low
)
1379 moder_time
= priv
->rx_usecs_low
;
1380 else if (rate
> priv
->pkt_rate_high
)
1381 moder_time
= priv
->rx_usecs_high
;
1383 moder_time
= (rate
- priv
->pkt_rate_low
) *
1384 (priv
->rx_usecs_high
- priv
->rx_usecs_low
) /
1385 (priv
->pkt_rate_high
- priv
->pkt_rate_low
) +
1388 moder_time
= priv
->rx_usecs_low
;
1391 if (moder_time
!= priv
->last_moder_time
[ring
]) {
1392 priv
->last_moder_time
[ring
] = moder_time
;
1393 cq
= priv
->rx_cq
[ring
];
1394 cq
->moder_time
= moder_time
;
1395 cq
->moder_cnt
= priv
->rx_frames
;
1396 err
= mlx4_en_set_cq_moder(priv
, cq
);
1398 en_err(priv
, "Failed modifying moderation for cq:%d\n",
1401 priv
->last_moder_packets
[ring
] = rx_packets
;
1402 priv
->last_moder_bytes
[ring
] = rx_bytes
;
1405 priv
->last_moder_jiffies
= jiffies
;
1408 static void mlx4_en_do_get_stats(struct work_struct
*work
)
1410 struct delayed_work
*delay
= to_delayed_work(work
);
1411 struct mlx4_en_priv
*priv
= container_of(delay
, struct mlx4_en_priv
,
1413 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1416 mutex_lock(&mdev
->state_lock
);
1417 if (mdev
->device_up
) {
1418 if (priv
->port_up
) {
1419 err
= mlx4_en_DUMP_ETH_STATS(mdev
, priv
->port
, 0);
1421 en_dbg(HW
, priv
, "Could not update stats\n");
1423 mlx4_en_auto_moderation(priv
);
1426 queue_delayed_work(mdev
->workqueue
, &priv
->stats_task
, STATS_DELAY
);
1428 if (mdev
->mac_removed
[MLX4_MAX_PORTS
+ 1 - priv
->port
]) {
1429 mlx4_en_do_set_mac(priv
, priv
->current_mac
);
1430 mdev
->mac_removed
[MLX4_MAX_PORTS
+ 1 - priv
->port
] = 0;
1432 mutex_unlock(&mdev
->state_lock
);
1435 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1438 static void mlx4_en_service_task(struct work_struct
*work
)
1440 struct delayed_work
*delay
= to_delayed_work(work
);
1441 struct mlx4_en_priv
*priv
= container_of(delay
, struct mlx4_en_priv
,
1443 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1445 mutex_lock(&mdev
->state_lock
);
1446 if (mdev
->device_up
) {
1447 if (mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_TS
)
1448 mlx4_en_ptp_overflow_check(mdev
);
1450 mlx4_en_recover_from_oom(priv
);
1451 queue_delayed_work(mdev
->workqueue
, &priv
->service_task
,
1452 SERVICE_TASK_DELAY
);
1454 mutex_unlock(&mdev
->state_lock
);
1457 static void mlx4_en_linkstate(struct work_struct
*work
)
1459 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
1461 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1462 int linkstate
= priv
->link_state
;
1464 mutex_lock(&mdev
->state_lock
);
1465 /* If observable port state changed set carrier state and
1466 * report to system log */
1467 if (priv
->last_link_state
!= linkstate
) {
1468 if (linkstate
== MLX4_DEV_EVENT_PORT_DOWN
) {
1469 en_info(priv
, "Link Down\n");
1470 netif_carrier_off(priv
->dev
);
1472 en_info(priv
, "Link Up\n");
1473 netif_carrier_on(priv
->dev
);
1476 priv
->last_link_state
= linkstate
;
1477 mutex_unlock(&mdev
->state_lock
);
1480 static int mlx4_en_init_affinity_hint(struct mlx4_en_priv
*priv
, int ring_idx
)
1482 struct mlx4_en_rx_ring
*ring
= priv
->rx_ring
[ring_idx
];
1483 int numa_node
= priv
->mdev
->dev
->numa_node
;
1485 if (!zalloc_cpumask_var(&ring
->affinity_mask
, GFP_KERNEL
))
1488 cpumask_set_cpu(cpumask_local_spread(ring_idx
, numa_node
),
1489 ring
->affinity_mask
);
1493 static void mlx4_en_free_affinity_hint(struct mlx4_en_priv
*priv
, int ring_idx
)
1495 free_cpumask_var(priv
->rx_ring
[ring_idx
]->affinity_mask
);
1498 int mlx4_en_start_port(struct net_device
*dev
)
1500 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
1501 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1502 struct mlx4_en_cq
*cq
;
1503 struct mlx4_en_tx_ring
*tx_ring
;
1509 u8 mc_list
[16] = {0};
1511 if (priv
->port_up
) {
1512 en_dbg(DRV
, priv
, "start port called while port already up\n");
1516 INIT_LIST_HEAD(&priv
->mc_list
);
1517 INIT_LIST_HEAD(&priv
->curr_list
);
1518 INIT_LIST_HEAD(&priv
->ethtool_list
);
1519 memset(&priv
->ethtool_rules
[0], 0,
1520 sizeof(struct ethtool_flow_id
) * MAX_NUM_OF_FS_RULES
);
1522 /* Calculate Rx buf size */
1523 dev
->mtu
= min(dev
->mtu
, priv
->max_mtu
);
1524 mlx4_en_calc_rx_buf(dev
);
1525 en_dbg(DRV
, priv
, "Rx buf size:%d\n", priv
->rx_skb_size
);
1527 /* Configure rx cq's and rings */
1528 err
= mlx4_en_activate_rx_rings(priv
);
1530 en_err(priv
, "Failed to activate RX rings\n");
1533 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
1534 cq
= priv
->rx_cq
[i
];
1536 err
= mlx4_en_init_affinity_hint(priv
, i
);
1538 en_err(priv
, "Failed preparing IRQ affinity hint\n");
1542 err
= mlx4_en_activate_cq(priv
, cq
, i
);
1544 en_err(priv
, "Failed activating Rx CQ\n");
1545 mlx4_en_free_affinity_hint(priv
, i
);
1549 for (j
= 0; j
< cq
->size
; j
++) {
1550 struct mlx4_cqe
*cqe
= NULL
;
1552 cqe
= mlx4_en_get_cqe(cq
->buf
, j
, priv
->cqe_size
) +
1554 cqe
->owner_sr_opcode
= MLX4_CQE_OWNER_MASK
;
1557 err
= mlx4_en_set_cq_moder(priv
, cq
);
1559 en_err(priv
, "Failed setting cq moderation parameters\n");
1560 mlx4_en_deactivate_cq(priv
, cq
);
1561 mlx4_en_free_affinity_hint(priv
, i
);
1564 mlx4_en_arm_cq(priv
, cq
);
1565 priv
->rx_ring
[i
]->cqn
= cq
->mcq
.cqn
;
1570 en_dbg(DRV
, priv
, "Getting qp number for port %d\n", priv
->port
);
1571 err
= mlx4_en_get_qp(priv
);
1573 en_err(priv
, "Failed getting eth qp\n");
1576 mdev
->mac_removed
[priv
->port
] = 0;
1578 priv
->counter_index
=
1579 mlx4_get_default_counter_index(mdev
->dev
, priv
->port
);
1581 err
= mlx4_en_config_rss_steer(priv
);
1583 en_err(priv
, "Failed configuring rss steering\n");
1587 err
= mlx4_en_create_drop_qp(priv
);
1591 /* Configure tx cq's and rings */
1592 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
1594 cq
= priv
->tx_cq
[i
];
1595 err
= mlx4_en_activate_cq(priv
, cq
, i
);
1597 en_err(priv
, "Failed allocating Tx CQ\n");
1600 err
= mlx4_en_set_cq_moder(priv
, cq
);
1602 en_err(priv
, "Failed setting cq moderation parameters\n");
1603 mlx4_en_deactivate_cq(priv
, cq
);
1606 en_dbg(DRV
, priv
, "Resetting index of collapsed CQ:%d to -1\n", i
);
1607 cq
->buf
->wqe_index
= cpu_to_be16(0xffff);
1609 /* Configure ring */
1610 tx_ring
= priv
->tx_ring
[i
];
1611 err
= mlx4_en_activate_tx_ring(priv
, tx_ring
, cq
->mcq
.cqn
,
1612 i
/ priv
->num_tx_rings_p_up
);
1614 en_err(priv
, "Failed allocating Tx ring\n");
1615 mlx4_en_deactivate_cq(priv
, cq
);
1618 tx_ring
->tx_queue
= netdev_get_tx_queue(dev
, i
);
1620 /* Arm CQ for TX completions */
1621 mlx4_en_arm_cq(priv
, cq
);
1623 /* Set initial ownership of all Tx TXBBs to SW (1) */
1624 for (j
= 0; j
< tx_ring
->buf_size
; j
+= STAMP_STRIDE
)
1625 *((u32
*) (tx_ring
->buf
+ j
)) = 0xffffffff;
1629 /* Configure port */
1630 err
= mlx4_SET_PORT_general(mdev
->dev
, priv
->port
,
1631 priv
->rx_skb_size
+ ETH_FCS_LEN
,
1632 priv
->prof
->tx_pause
,
1634 priv
->prof
->rx_pause
,
1635 priv
->prof
->rx_ppp
);
1637 en_err(priv
, "Failed setting port general configurations for port %d, with error %d\n",
1641 /* Set default qp number */
1642 err
= mlx4_SET_PORT_qpn_calc(mdev
->dev
, priv
->port
, priv
->base_qpn
, 0);
1644 en_err(priv
, "Failed setting default qp numbers\n");
1648 if (mdev
->dev
->caps
.tunnel_offload_mode
== MLX4_TUNNEL_OFFLOAD_MODE_VXLAN
) {
1649 err
= mlx4_SET_PORT_VXLAN(mdev
->dev
, priv
->port
, VXLAN_STEER_BY_OUTER_MAC
, 1);
1651 en_err(priv
, "Failed setting port L2 tunnel configuration, err %d\n",
1658 en_dbg(HW
, priv
, "Initializing port\n");
1659 err
= mlx4_INIT_PORT(mdev
->dev
, priv
->port
);
1661 en_err(priv
, "Failed Initializing port\n");
1665 /* Set Unicast and VXLAN steering rules */
1666 if (mdev
->dev
->caps
.steering_mode
!= MLX4_STEERING_MODE_A0
&&
1667 mlx4_en_set_rss_steer_rules(priv
))
1668 mlx4_warn(mdev
, "Failed setting steering rules\n");
1670 /* Attach rx QP to bradcast address */
1671 eth_broadcast_addr(&mc_list
[10]);
1672 mc_list
[5] = priv
->port
; /* needed for B0 steering support */
1673 if (mlx4_multicast_attach(mdev
->dev
, &priv
->rss_map
.indir_qp
, mc_list
,
1674 priv
->port
, 0, MLX4_PROT_ETH
,
1675 &priv
->broadcast_id
))
1676 mlx4_warn(mdev
, "Failed Attaching Broadcast\n");
1678 /* Must redo promiscuous mode setup. */
1679 priv
->flags
&= ~(MLX4_EN_FLAG_PROMISC
| MLX4_EN_FLAG_MC_PROMISC
);
1681 /* Schedule multicast task to populate multicast list */
1682 queue_work(mdev
->workqueue
, &priv
->rx_mode_task
);
1684 #ifdef CONFIG_MLX4_EN_VXLAN
1685 if (priv
->mdev
->dev
->caps
.tunnel_offload_mode
== MLX4_TUNNEL_OFFLOAD_MODE_VXLAN
)
1686 vxlan_get_rx_port(dev
);
1688 priv
->port_up
= true;
1689 netif_tx_start_all_queues(dev
);
1690 netif_device_attach(dev
);
1695 while (tx_index
--) {
1696 mlx4_en_deactivate_tx_ring(priv
, priv
->tx_ring
[tx_index
]);
1697 mlx4_en_deactivate_cq(priv
, priv
->tx_cq
[tx_index
]);
1699 mlx4_en_destroy_drop_qp(priv
);
1701 mlx4_en_release_rss_steer(priv
);
1703 mlx4_en_put_qp(priv
);
1705 while (rx_index
--) {
1706 mlx4_en_deactivate_cq(priv
, priv
->rx_cq
[rx_index
]);
1707 mlx4_en_free_affinity_hint(priv
, rx_index
);
1709 for (i
= 0; i
< priv
->rx_ring_num
; i
++)
1710 mlx4_en_deactivate_rx_ring(priv
, priv
->rx_ring
[i
]);
1712 return err
; /* need to close devices */
1716 void mlx4_en_stop_port(struct net_device
*dev
, int detach
)
1718 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
1719 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1720 struct mlx4_en_mc_list
*mclist
, *tmp
;
1721 struct ethtool_flow_id
*flow
, *tmp_flow
;
1723 u8 mc_list
[16] = {0};
1725 if (!priv
->port_up
) {
1726 en_dbg(DRV
, priv
, "stop port called while port already down\n");
1731 mlx4_CLOSE_PORT(mdev
->dev
, priv
->port
);
1733 /* Synchronize with tx routine */
1734 netif_tx_lock_bh(dev
);
1736 netif_device_detach(dev
);
1737 netif_tx_stop_all_queues(dev
);
1738 netif_tx_unlock_bh(dev
);
1740 netif_tx_disable(dev
);
1742 /* Set port as not active */
1743 priv
->port_up
= false;
1744 priv
->counter_index
= MLX4_SINK_COUNTER_INDEX(mdev
->dev
);
1746 /* Promsicuous mode */
1747 if (mdev
->dev
->caps
.steering_mode
==
1748 MLX4_STEERING_MODE_DEVICE_MANAGED
) {
1749 priv
->flags
&= ~(MLX4_EN_FLAG_PROMISC
|
1750 MLX4_EN_FLAG_MC_PROMISC
);
1751 mlx4_flow_steer_promisc_remove(mdev
->dev
,
1753 MLX4_FS_ALL_DEFAULT
);
1754 mlx4_flow_steer_promisc_remove(mdev
->dev
,
1756 MLX4_FS_MC_DEFAULT
);
1757 } else if (priv
->flags
& MLX4_EN_FLAG_PROMISC
) {
1758 priv
->flags
&= ~MLX4_EN_FLAG_PROMISC
;
1760 /* Disable promiscouos mode */
1761 mlx4_unicast_promisc_remove(mdev
->dev
, priv
->base_qpn
,
1764 /* Disable Multicast promisc */
1765 if (priv
->flags
& MLX4_EN_FLAG_MC_PROMISC
) {
1766 mlx4_multicast_promisc_remove(mdev
->dev
, priv
->base_qpn
,
1768 priv
->flags
&= ~MLX4_EN_FLAG_MC_PROMISC
;
1772 /* Detach All multicasts */
1773 eth_broadcast_addr(&mc_list
[10]);
1774 mc_list
[5] = priv
->port
; /* needed for B0 steering support */
1775 mlx4_multicast_detach(mdev
->dev
, &priv
->rss_map
.indir_qp
, mc_list
,
1776 MLX4_PROT_ETH
, priv
->broadcast_id
);
1777 list_for_each_entry(mclist
, &priv
->curr_list
, list
) {
1778 memcpy(&mc_list
[10], mclist
->addr
, ETH_ALEN
);
1779 mc_list
[5] = priv
->port
;
1780 mlx4_multicast_detach(mdev
->dev
, &priv
->rss_map
.indir_qp
,
1781 mc_list
, MLX4_PROT_ETH
, mclist
->reg_id
);
1782 if (mclist
->tunnel_reg_id
)
1783 mlx4_flow_detach(mdev
->dev
, mclist
->tunnel_reg_id
);
1785 mlx4_en_clear_list(dev
);
1786 list_for_each_entry_safe(mclist
, tmp
, &priv
->curr_list
, list
) {
1787 list_del(&mclist
->list
);
1791 /* Flush multicast filter */
1792 mlx4_SET_MCAST_FLTR(mdev
->dev
, priv
->port
, 0, 1, MLX4_MCAST_CONFIG
);
1794 /* Remove flow steering rules for the port*/
1795 if (mdev
->dev
->caps
.steering_mode
==
1796 MLX4_STEERING_MODE_DEVICE_MANAGED
) {
1798 list_for_each_entry_safe(flow
, tmp_flow
,
1799 &priv
->ethtool_list
, list
) {
1800 mlx4_flow_detach(mdev
->dev
, flow
->id
);
1801 list_del(&flow
->list
);
1805 mlx4_en_destroy_drop_qp(priv
);
1808 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
1809 mlx4_en_deactivate_tx_ring(priv
, priv
->tx_ring
[i
]);
1810 mlx4_en_deactivate_cq(priv
, priv
->tx_cq
[i
]);
1814 for (i
= 0; i
< priv
->tx_ring_num
; i
++)
1815 mlx4_en_free_tx_buf(dev
, priv
->tx_ring
[i
]);
1817 if (mdev
->dev
->caps
.steering_mode
!= MLX4_STEERING_MODE_A0
)
1818 mlx4_en_delete_rss_steer_rules(priv
);
1821 mlx4_en_release_rss_steer(priv
);
1823 /* Unregister Mac address for the port */
1824 mlx4_en_put_qp(priv
);
1825 if (!(mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN
))
1826 mdev
->mac_removed
[priv
->port
] = 1;
1829 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
1830 struct mlx4_en_cq
*cq
= priv
->rx_cq
[i
];
1832 napi_synchronize(&cq
->napi
);
1833 mlx4_en_deactivate_rx_ring(priv
, priv
->rx_ring
[i
]);
1834 mlx4_en_deactivate_cq(priv
, cq
);
1836 mlx4_en_free_affinity_hint(priv
, i
);
1840 static void mlx4_en_restart(struct work_struct
*work
)
1842 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
1844 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1845 struct net_device
*dev
= priv
->dev
;
1847 en_dbg(DRV
, priv
, "Watchdog task called for port %d\n", priv
->port
);
1849 mutex_lock(&mdev
->state_lock
);
1850 if (priv
->port_up
) {
1851 mlx4_en_stop_port(dev
, 1);
1852 if (mlx4_en_start_port(dev
))
1853 en_err(priv
, "Failed restarting port %d\n", priv
->port
);
1855 mutex_unlock(&mdev
->state_lock
);
1858 static void mlx4_en_clear_stats(struct net_device
*dev
)
1860 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
1861 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1864 if (mlx4_en_DUMP_ETH_STATS(mdev
, priv
->port
, 1))
1865 en_dbg(HW
, priv
, "Failed dumping statistics\n");
1867 memset(&priv
->stats
, 0, sizeof(priv
->stats
));
1868 memset(&priv
->pstats
, 0, sizeof(priv
->pstats
));
1869 memset(&priv
->pkstats
, 0, sizeof(priv
->pkstats
));
1870 memset(&priv
->port_stats
, 0, sizeof(priv
->port_stats
));
1871 memset(&priv
->rx_flowstats
, 0, sizeof(priv
->rx_flowstats
));
1872 memset(&priv
->tx_flowstats
, 0, sizeof(priv
->tx_flowstats
));
1873 memset(&priv
->rx_priority_flowstats
, 0,
1874 sizeof(priv
->rx_priority_flowstats
));
1875 memset(&priv
->tx_priority_flowstats
, 0,
1876 sizeof(priv
->tx_priority_flowstats
));
1877 memset(&priv
->pf_stats
, 0, sizeof(priv
->pf_stats
));
1879 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
1880 priv
->tx_ring
[i
]->bytes
= 0;
1881 priv
->tx_ring
[i
]->packets
= 0;
1882 priv
->tx_ring
[i
]->tx_csum
= 0;
1884 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
1885 priv
->rx_ring
[i
]->bytes
= 0;
1886 priv
->rx_ring
[i
]->packets
= 0;
1887 priv
->rx_ring
[i
]->csum_ok
= 0;
1888 priv
->rx_ring
[i
]->csum_none
= 0;
1889 priv
->rx_ring
[i
]->csum_complete
= 0;
1893 static int mlx4_en_open(struct net_device
*dev
)
1895 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
1896 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1899 mutex_lock(&mdev
->state_lock
);
1901 if (!mdev
->device_up
) {
1902 en_err(priv
, "Cannot open - device down/disabled\n");
1907 /* Reset HW statistics and SW counters */
1908 mlx4_en_clear_stats(dev
);
1910 err
= mlx4_en_start_port(dev
);
1912 en_err(priv
, "Failed starting port:%d\n", priv
->port
);
1915 mutex_unlock(&mdev
->state_lock
);
1920 static int mlx4_en_close(struct net_device
*dev
)
1922 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
1923 struct mlx4_en_dev
*mdev
= priv
->mdev
;
1925 en_dbg(IFDOWN
, priv
, "Close port called\n");
1927 mutex_lock(&mdev
->state_lock
);
1929 mlx4_en_stop_port(dev
, 0);
1930 netif_carrier_off(dev
);
1932 mutex_unlock(&mdev
->state_lock
);
1936 void mlx4_en_free_resources(struct mlx4_en_priv
*priv
)
1940 #ifdef CONFIG_RFS_ACCEL
1941 priv
->dev
->rx_cpu_rmap
= NULL
;
1944 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
1945 if (priv
->tx_ring
&& priv
->tx_ring
[i
])
1946 mlx4_en_destroy_tx_ring(priv
, &priv
->tx_ring
[i
]);
1947 if (priv
->tx_cq
&& priv
->tx_cq
[i
])
1948 mlx4_en_destroy_cq(priv
, &priv
->tx_cq
[i
]);
1951 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
1952 if (priv
->rx_ring
[i
])
1953 mlx4_en_destroy_rx_ring(priv
, &priv
->rx_ring
[i
],
1954 priv
->prof
->rx_ring_size
, priv
->stride
);
1956 mlx4_en_destroy_cq(priv
, &priv
->rx_cq
[i
]);
1961 int mlx4_en_alloc_resources(struct mlx4_en_priv
*priv
)
1963 struct mlx4_en_port_profile
*prof
= priv
->prof
;
1967 /* Create tx Rings */
1968 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
1969 node
= cpu_to_node(i
% num_online_cpus());
1970 if (mlx4_en_create_cq(priv
, &priv
->tx_cq
[i
],
1971 prof
->tx_ring_size
, i
, TX
, node
))
1974 if (mlx4_en_create_tx_ring(priv
, &priv
->tx_ring
[i
],
1975 prof
->tx_ring_size
, TXBB_SIZE
,
1980 /* Create rx Rings */
1981 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
1982 node
= cpu_to_node(i
% num_online_cpus());
1983 if (mlx4_en_create_cq(priv
, &priv
->rx_cq
[i
],
1984 prof
->rx_ring_size
, i
, RX
, node
))
1987 if (mlx4_en_create_rx_ring(priv
, &priv
->rx_ring
[i
],
1988 prof
->rx_ring_size
, priv
->stride
,
1993 #ifdef CONFIG_RFS_ACCEL
1994 priv
->dev
->rx_cpu_rmap
= mlx4_get_cpu_rmap(priv
->mdev
->dev
, priv
->port
);
2000 en_err(priv
, "Failed to allocate NIC resources\n");
2001 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
2002 if (priv
->rx_ring
[i
])
2003 mlx4_en_destroy_rx_ring(priv
, &priv
->rx_ring
[i
],
2007 mlx4_en_destroy_cq(priv
, &priv
->rx_cq
[i
]);
2009 for (i
= 0; i
< priv
->tx_ring_num
; i
++) {
2010 if (priv
->tx_ring
[i
])
2011 mlx4_en_destroy_tx_ring(priv
, &priv
->tx_ring
[i
]);
2013 mlx4_en_destroy_cq(priv
, &priv
->tx_cq
[i
]);
2019 void mlx4_en_destroy_netdev(struct net_device
*dev
)
2021 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
2022 struct mlx4_en_dev
*mdev
= priv
->mdev
;
2024 en_dbg(DRV
, priv
, "Destroying netdev on port:%d\n", priv
->port
);
2026 /* Unregister device - this will close the port if it was up */
2027 if (priv
->registered
)
2028 unregister_netdev(dev
);
2030 if (priv
->allocated
)
2031 mlx4_free_hwq_res(mdev
->dev
, &priv
->res
, MLX4_EN_PAGE_SIZE
);
2033 cancel_delayed_work(&priv
->stats_task
);
2034 cancel_delayed_work(&priv
->service_task
);
2035 /* flush any pending task for this netdev */
2036 flush_workqueue(mdev
->workqueue
);
2038 /* Detach the netdev so tasks would not attempt to access it */
2039 mutex_lock(&mdev
->state_lock
);
2040 mdev
->pndev
[priv
->port
] = NULL
;
2041 mdev
->upper
[priv
->port
] = NULL
;
2042 mutex_unlock(&mdev
->state_lock
);
2044 mlx4_en_free_resources(priv
);
2046 kfree(priv
->tx_ring
);
2052 static int mlx4_en_change_mtu(struct net_device
*dev
, int new_mtu
)
2054 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
2055 struct mlx4_en_dev
*mdev
= priv
->mdev
;
2058 en_dbg(DRV
, priv
, "Change MTU called - current:%d new:%d\n",
2061 if ((new_mtu
< MLX4_EN_MIN_MTU
) || (new_mtu
> priv
->max_mtu
)) {
2062 en_err(priv
, "Bad MTU size:%d.\n", new_mtu
);
2067 if (netif_running(dev
)) {
2068 mutex_lock(&mdev
->state_lock
);
2069 if (!mdev
->device_up
) {
2070 /* NIC is probably restarting - let watchdog task reset
2072 en_dbg(DRV
, priv
, "Change MTU called with card down!?\n");
2074 mlx4_en_stop_port(dev
, 1);
2075 err
= mlx4_en_start_port(dev
);
2077 en_err(priv
, "Failed restarting port:%d\n",
2079 queue_work(mdev
->workqueue
, &priv
->watchdog_task
);
2082 mutex_unlock(&mdev
->state_lock
);
2087 static int mlx4_en_hwtstamp_set(struct net_device
*dev
, struct ifreq
*ifr
)
2089 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
2090 struct mlx4_en_dev
*mdev
= priv
->mdev
;
2091 struct hwtstamp_config config
;
2093 if (copy_from_user(&config
, ifr
->ifr_data
, sizeof(config
)))
2096 /* reserved for future extensions */
2100 /* device doesn't support time stamping */
2101 if (!(mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_TS
))
2104 /* TX HW timestamp */
2105 switch (config
.tx_type
) {
2106 case HWTSTAMP_TX_OFF
:
2107 case HWTSTAMP_TX_ON
:
2113 /* RX HW timestamp */
2114 switch (config
.rx_filter
) {
2115 case HWTSTAMP_FILTER_NONE
:
2117 case HWTSTAMP_FILTER_ALL
:
2118 case HWTSTAMP_FILTER_SOME
:
2119 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
2120 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
2121 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
2122 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
2123 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
2124 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
2125 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
2126 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
2127 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
2128 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
2129 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
2130 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
2131 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
2137 if (mlx4_en_reset_config(dev
, config
, dev
->features
)) {
2138 config
.tx_type
= HWTSTAMP_TX_OFF
;
2139 config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
2142 return copy_to_user(ifr
->ifr_data
, &config
,
2143 sizeof(config
)) ? -EFAULT
: 0;
2146 static int mlx4_en_hwtstamp_get(struct net_device
*dev
, struct ifreq
*ifr
)
2148 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
2150 return copy_to_user(ifr
->ifr_data
, &priv
->hwtstamp_config
,
2151 sizeof(priv
->hwtstamp_config
)) ? -EFAULT
: 0;
2154 static int mlx4_en_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
2158 return mlx4_en_hwtstamp_set(dev
, ifr
);
2160 return mlx4_en_hwtstamp_get(dev
, ifr
);
2166 static netdev_features_t
mlx4_en_fix_features(struct net_device
*netdev
,
2167 netdev_features_t features
)
2169 struct mlx4_en_priv
*en_priv
= netdev_priv(netdev
);
2170 struct mlx4_en_dev
*mdev
= en_priv
->mdev
;
2172 /* Since there is no support for separate RX C-TAG/S-TAG vlan accel
2173 * enable/disable make sure S-TAG flag is always in same state as
2176 if (features
& NETIF_F_HW_VLAN_CTAG_RX
&&
2177 !(mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN
))
2178 features
|= NETIF_F_HW_VLAN_STAG_RX
;
2180 features
&= ~NETIF_F_HW_VLAN_STAG_RX
;
2185 static int mlx4_en_set_features(struct net_device
*netdev
,
2186 netdev_features_t features
)
2188 struct mlx4_en_priv
*priv
= netdev_priv(netdev
);
2192 if (DEV_FEATURE_CHANGED(netdev
, features
, NETIF_F_RXFCS
)) {
2193 en_info(priv
, "Turn %s RX-FCS\n",
2194 (features
& NETIF_F_RXFCS
) ? "ON" : "OFF");
2198 if (DEV_FEATURE_CHANGED(netdev
, features
, NETIF_F_RXALL
)) {
2199 u8 ignore_fcs_value
= (features
& NETIF_F_RXALL
) ? 1 : 0;
2201 en_info(priv
, "Turn %s RX-ALL\n",
2202 ignore_fcs_value
? "ON" : "OFF");
2203 ret
= mlx4_SET_PORT_fcs_check(priv
->mdev
->dev
,
2204 priv
->port
, ignore_fcs_value
);
2209 if (DEV_FEATURE_CHANGED(netdev
, features
, NETIF_F_HW_VLAN_CTAG_RX
)) {
2210 en_info(priv
, "Turn %s RX vlan strip offload\n",
2211 (features
& NETIF_F_HW_VLAN_CTAG_RX
) ? "ON" : "OFF");
2215 if (DEV_FEATURE_CHANGED(netdev
, features
, NETIF_F_HW_VLAN_CTAG_TX
))
2216 en_info(priv
, "Turn %s TX vlan strip offload\n",
2217 (features
& NETIF_F_HW_VLAN_CTAG_TX
) ? "ON" : "OFF");
2219 if (DEV_FEATURE_CHANGED(netdev
, features
, NETIF_F_HW_VLAN_STAG_TX
))
2220 en_info(priv
, "Turn %s TX S-VLAN strip offload\n",
2221 (features
& NETIF_F_HW_VLAN_STAG_TX
) ? "ON" : "OFF");
2223 if (DEV_FEATURE_CHANGED(netdev
, features
, NETIF_F_LOOPBACK
)) {
2224 en_info(priv
, "Turn %s loopback\n",
2225 (features
& NETIF_F_LOOPBACK
) ? "ON" : "OFF");
2226 mlx4_en_update_loopback_state(netdev
, features
);
2230 ret
= mlx4_en_reset_config(netdev
, priv
->hwtstamp_config
,
2239 static int mlx4_en_set_vf_mac(struct net_device
*dev
, int queue
, u8
*mac
)
2241 struct mlx4_en_priv
*en_priv
= netdev_priv(dev
);
2242 struct mlx4_en_dev
*mdev
= en_priv
->mdev
;
2243 u64 mac_u64
= mlx4_mac_to_u64(mac
);
2245 if (!is_valid_ether_addr(mac
))
2248 return mlx4_set_vf_mac(mdev
->dev
, en_priv
->port
, queue
, mac_u64
);
2251 static int mlx4_en_set_vf_vlan(struct net_device
*dev
, int vf
, u16 vlan
, u8 qos
)
2253 struct mlx4_en_priv
*en_priv
= netdev_priv(dev
);
2254 struct mlx4_en_dev
*mdev
= en_priv
->mdev
;
2256 return mlx4_set_vf_vlan(mdev
->dev
, en_priv
->port
, vf
, vlan
, qos
);
2259 static int mlx4_en_set_vf_rate(struct net_device
*dev
, int vf
, int min_tx_rate
,
2262 struct mlx4_en_priv
*en_priv
= netdev_priv(dev
);
2263 struct mlx4_en_dev
*mdev
= en_priv
->mdev
;
2265 return mlx4_set_vf_rate(mdev
->dev
, en_priv
->port
, vf
, min_tx_rate
,
2269 static int mlx4_en_set_vf_spoofchk(struct net_device
*dev
, int vf
, bool setting
)
2271 struct mlx4_en_priv
*en_priv
= netdev_priv(dev
);
2272 struct mlx4_en_dev
*mdev
= en_priv
->mdev
;
2274 return mlx4_set_vf_spoofchk(mdev
->dev
, en_priv
->port
, vf
, setting
);
2277 static int mlx4_en_get_vf_config(struct net_device
*dev
, int vf
, struct ifla_vf_info
*ivf
)
2279 struct mlx4_en_priv
*en_priv
= netdev_priv(dev
);
2280 struct mlx4_en_dev
*mdev
= en_priv
->mdev
;
2282 return mlx4_get_vf_config(mdev
->dev
, en_priv
->port
, vf
, ivf
);
2285 static int mlx4_en_set_vf_link_state(struct net_device
*dev
, int vf
, int link_state
)
2287 struct mlx4_en_priv
*en_priv
= netdev_priv(dev
);
2288 struct mlx4_en_dev
*mdev
= en_priv
->mdev
;
2290 return mlx4_set_vf_link_state(mdev
->dev
, en_priv
->port
, vf
, link_state
);
2293 static int mlx4_en_get_vf_stats(struct net_device
*dev
, int vf
,
2294 struct ifla_vf_stats
*vf_stats
)
2296 struct mlx4_en_priv
*en_priv
= netdev_priv(dev
);
2297 struct mlx4_en_dev
*mdev
= en_priv
->mdev
;
2299 return mlx4_get_vf_stats(mdev
->dev
, en_priv
->port
, vf
, vf_stats
);
2302 #define PORT_ID_BYTE_LEN 8
2303 static int mlx4_en_get_phys_port_id(struct net_device
*dev
,
2304 struct netdev_phys_item_id
*ppid
)
2306 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
2307 struct mlx4_dev
*mdev
= priv
->mdev
->dev
;
2309 u64 phys_port_id
= mdev
->caps
.phys_port_id
[priv
->port
];
2314 ppid
->id_len
= sizeof(phys_port_id
);
2315 for (i
= PORT_ID_BYTE_LEN
- 1; i
>= 0; --i
) {
2316 ppid
->id
[i
] = phys_port_id
& 0xff;
2322 #ifdef CONFIG_MLX4_EN_VXLAN
2323 static void mlx4_en_add_vxlan_offloads(struct work_struct
*work
)
2326 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
2329 ret
= mlx4_config_vxlan_port(priv
->mdev
->dev
, priv
->vxlan_port
);
2333 ret
= mlx4_SET_PORT_VXLAN(priv
->mdev
->dev
, priv
->port
,
2334 VXLAN_STEER_BY_OUTER_MAC
, 1);
2337 en_err(priv
, "failed setting L2 tunnel configuration ret %d\n", ret
);
2342 priv
->dev
->hw_enc_features
|= NETIF_F_IP_CSUM
| NETIF_F_RXCSUM
|
2343 NETIF_F_TSO
| NETIF_F_GSO_UDP_TUNNEL
;
2344 priv
->dev
->hw_features
|= NETIF_F_GSO_UDP_TUNNEL
;
2345 priv
->dev
->features
|= NETIF_F_GSO_UDP_TUNNEL
;
2348 static void mlx4_en_del_vxlan_offloads(struct work_struct
*work
)
2351 struct mlx4_en_priv
*priv
= container_of(work
, struct mlx4_en_priv
,
2353 /* unset offloads */
2354 priv
->dev
->hw_enc_features
&= ~(NETIF_F_IP_CSUM
| NETIF_F_RXCSUM
|
2355 NETIF_F_TSO
| NETIF_F_GSO_UDP_TUNNEL
);
2356 priv
->dev
->hw_features
&= ~NETIF_F_GSO_UDP_TUNNEL
;
2357 priv
->dev
->features
&= ~NETIF_F_GSO_UDP_TUNNEL
;
2359 ret
= mlx4_SET_PORT_VXLAN(priv
->mdev
->dev
, priv
->port
,
2360 VXLAN_STEER_BY_OUTER_MAC
, 0);
2362 en_err(priv
, "failed setting L2 tunnel configuration ret %d\n", ret
);
2364 priv
->vxlan_port
= 0;
2367 static void mlx4_en_add_vxlan_port(struct net_device
*dev
,
2368 sa_family_t sa_family
, __be16 port
)
2370 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
2371 __be16 current_port
;
2373 if (priv
->mdev
->dev
->caps
.tunnel_offload_mode
!= MLX4_TUNNEL_OFFLOAD_MODE_VXLAN
)
2376 if (sa_family
== AF_INET6
)
2379 current_port
= priv
->vxlan_port
;
2380 if (current_port
&& current_port
!= port
) {
2381 en_warn(priv
, "vxlan port %d configured, can't add port %d\n",
2382 ntohs(current_port
), ntohs(port
));
2386 priv
->vxlan_port
= port
;
2387 queue_work(priv
->mdev
->workqueue
, &priv
->vxlan_add_task
);
2390 static void mlx4_en_del_vxlan_port(struct net_device
*dev
,
2391 sa_family_t sa_family
, __be16 port
)
2393 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
2394 __be16 current_port
;
2396 if (priv
->mdev
->dev
->caps
.tunnel_offload_mode
!= MLX4_TUNNEL_OFFLOAD_MODE_VXLAN
)
2399 if (sa_family
== AF_INET6
)
2402 current_port
= priv
->vxlan_port
;
2403 if (current_port
!= port
) {
2404 en_dbg(DRV
, priv
, "vxlan port %d isn't configured, ignoring\n", ntohs(port
));
2408 queue_work(priv
->mdev
->workqueue
, &priv
->vxlan_del_task
);
2411 static netdev_features_t
mlx4_en_features_check(struct sk_buff
*skb
,
2412 struct net_device
*dev
,
2413 netdev_features_t features
)
2415 features
= vlan_features_check(skb
, features
);
2416 return vxlan_features_check(skb
, features
);
2420 static int mlx4_en_set_tx_maxrate(struct net_device
*dev
, int queue_index
, u32 maxrate
)
2422 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
2423 struct mlx4_en_tx_ring
*tx_ring
= priv
->tx_ring
[queue_index
];
2424 struct mlx4_update_qp_params params
;
2427 if (!(priv
->mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT
))
2430 /* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
2431 if (maxrate
>> 12) {
2432 params
.rate_unit
= MLX4_QP_RATE_LIMIT_GBS
;
2433 params
.rate_val
= maxrate
/ 1000;
2434 } else if (maxrate
) {
2435 params
.rate_unit
= MLX4_QP_RATE_LIMIT_MBS
;
2436 params
.rate_val
= maxrate
;
2437 } else { /* zero serves to revoke the QP rate-limitation */
2438 params
.rate_unit
= 0;
2439 params
.rate_val
= 0;
2442 err
= mlx4_update_qp(priv
->mdev
->dev
, tx_ring
->qpn
, MLX4_UPDATE_QP_RATE_LIMIT
,
2447 static const struct net_device_ops mlx4_netdev_ops
= {
2448 .ndo_open
= mlx4_en_open
,
2449 .ndo_stop
= mlx4_en_close
,
2450 .ndo_start_xmit
= mlx4_en_xmit
,
2451 .ndo_select_queue
= mlx4_en_select_queue
,
2452 .ndo_get_stats
= mlx4_en_get_stats
,
2453 .ndo_set_rx_mode
= mlx4_en_set_rx_mode
,
2454 .ndo_set_mac_address
= mlx4_en_set_mac
,
2455 .ndo_validate_addr
= eth_validate_addr
,
2456 .ndo_change_mtu
= mlx4_en_change_mtu
,
2457 .ndo_do_ioctl
= mlx4_en_ioctl
,
2458 .ndo_tx_timeout
= mlx4_en_tx_timeout
,
2459 .ndo_vlan_rx_add_vid
= mlx4_en_vlan_rx_add_vid
,
2460 .ndo_vlan_rx_kill_vid
= mlx4_en_vlan_rx_kill_vid
,
2461 #ifdef CONFIG_NET_POLL_CONTROLLER
2462 .ndo_poll_controller
= mlx4_en_netpoll
,
2464 .ndo_set_features
= mlx4_en_set_features
,
2465 .ndo_fix_features
= mlx4_en_fix_features
,
2466 .ndo_setup_tc
= mlx4_en_setup_tc
,
2467 #ifdef CONFIG_RFS_ACCEL
2468 .ndo_rx_flow_steer
= mlx4_en_filter_rfs
,
2470 .ndo_get_phys_port_id
= mlx4_en_get_phys_port_id
,
2471 #ifdef CONFIG_MLX4_EN_VXLAN
2472 .ndo_add_vxlan_port
= mlx4_en_add_vxlan_port
,
2473 .ndo_del_vxlan_port
= mlx4_en_del_vxlan_port
,
2474 .ndo_features_check
= mlx4_en_features_check
,
2476 .ndo_set_tx_maxrate
= mlx4_en_set_tx_maxrate
,
2479 static const struct net_device_ops mlx4_netdev_ops_master
= {
2480 .ndo_open
= mlx4_en_open
,
2481 .ndo_stop
= mlx4_en_close
,
2482 .ndo_start_xmit
= mlx4_en_xmit
,
2483 .ndo_select_queue
= mlx4_en_select_queue
,
2484 .ndo_get_stats
= mlx4_en_get_stats
,
2485 .ndo_set_rx_mode
= mlx4_en_set_rx_mode
,
2486 .ndo_set_mac_address
= mlx4_en_set_mac
,
2487 .ndo_validate_addr
= eth_validate_addr
,
2488 .ndo_change_mtu
= mlx4_en_change_mtu
,
2489 .ndo_tx_timeout
= mlx4_en_tx_timeout
,
2490 .ndo_vlan_rx_add_vid
= mlx4_en_vlan_rx_add_vid
,
2491 .ndo_vlan_rx_kill_vid
= mlx4_en_vlan_rx_kill_vid
,
2492 .ndo_set_vf_mac
= mlx4_en_set_vf_mac
,
2493 .ndo_set_vf_vlan
= mlx4_en_set_vf_vlan
,
2494 .ndo_set_vf_rate
= mlx4_en_set_vf_rate
,
2495 .ndo_set_vf_spoofchk
= mlx4_en_set_vf_spoofchk
,
2496 .ndo_set_vf_link_state
= mlx4_en_set_vf_link_state
,
2497 .ndo_get_vf_stats
= mlx4_en_get_vf_stats
,
2498 .ndo_get_vf_config
= mlx4_en_get_vf_config
,
2499 #ifdef CONFIG_NET_POLL_CONTROLLER
2500 .ndo_poll_controller
= mlx4_en_netpoll
,
2502 .ndo_set_features
= mlx4_en_set_features
,
2503 .ndo_fix_features
= mlx4_en_fix_features
,
2504 .ndo_setup_tc
= mlx4_en_setup_tc
,
2505 #ifdef CONFIG_RFS_ACCEL
2506 .ndo_rx_flow_steer
= mlx4_en_filter_rfs
,
2508 .ndo_get_phys_port_id
= mlx4_en_get_phys_port_id
,
2509 #ifdef CONFIG_MLX4_EN_VXLAN
2510 .ndo_add_vxlan_port
= mlx4_en_add_vxlan_port
,
2511 .ndo_del_vxlan_port
= mlx4_en_del_vxlan_port
,
2512 .ndo_features_check
= mlx4_en_features_check
,
2514 .ndo_set_tx_maxrate
= mlx4_en_set_tx_maxrate
,
2517 struct mlx4_en_bond
{
2518 struct work_struct work
;
2519 struct mlx4_en_priv
*priv
;
2521 struct mlx4_port_map port_map
;
2524 static void mlx4_en_bond_work(struct work_struct
*work
)
2526 struct mlx4_en_bond
*bond
= container_of(work
,
2527 struct mlx4_en_bond
,
2530 struct mlx4_dev
*dev
= bond
->priv
->mdev
->dev
;
2532 if (bond
->is_bonded
) {
2533 if (!mlx4_is_bonded(dev
)) {
2534 err
= mlx4_bond(dev
);
2536 en_err(bond
->priv
, "Fail to bond device\n");
2539 err
= mlx4_port_map_set(dev
, &bond
->port_map
);
2541 en_err(bond
->priv
, "Fail to set port map [%d][%d]: %d\n",
2542 bond
->port_map
.port1
,
2543 bond
->port_map
.port2
,
2546 } else if (mlx4_is_bonded(dev
)) {
2547 err
= mlx4_unbond(dev
);
2549 en_err(bond
->priv
, "Fail to unbond device\n");
2551 dev_put(bond
->priv
->dev
);
2555 static int mlx4_en_queue_bond_work(struct mlx4_en_priv
*priv
, int is_bonded
,
2556 u8 v2p_p1
, u8 v2p_p2
)
2558 struct mlx4_en_bond
*bond
= NULL
;
2560 bond
= kzalloc(sizeof(*bond
), GFP_ATOMIC
);
2564 INIT_WORK(&bond
->work
, mlx4_en_bond_work
);
2566 bond
->is_bonded
= is_bonded
;
2567 bond
->port_map
.port1
= v2p_p1
;
2568 bond
->port_map
.port2
= v2p_p2
;
2569 dev_hold(priv
->dev
);
2570 queue_work(priv
->mdev
->workqueue
, &bond
->work
);
2574 int mlx4_en_netdev_event(struct notifier_block
*this,
2575 unsigned long event
, void *ptr
)
2577 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
2579 struct mlx4_en_dev
*mdev
;
2580 struct mlx4_dev
*dev
;
2581 int i
, num_eth_ports
= 0;
2582 bool do_bond
= true;
2583 struct mlx4_en_priv
*priv
;
2587 if (!net_eq(dev_net(ndev
), &init_net
))
2590 mdev
= container_of(this, struct mlx4_en_dev
, nb
);
2593 /* Go into this mode only when two network devices set on two ports
2594 * of the same mlx4 device are slaves of the same bonding master
2596 mlx4_foreach_port(i
, dev
, MLX4_PORT_TYPE_ETH
) {
2598 if (!port
&& (mdev
->pndev
[i
] == ndev
))
2600 mdev
->upper
[i
] = mdev
->pndev
[i
] ?
2601 netdev_master_upper_dev_get(mdev
->pndev
[i
]) : NULL
;
2602 /* condition not met: network device is a slave */
2603 if (!mdev
->upper
[i
])
2605 if (num_eth_ports
< 2)
2607 /* condition not met: same master */
2608 if (mdev
->upper
[i
] != mdev
->upper
[i
-1])
2611 /* condition not met: 2 salves */
2612 do_bond
= (num_eth_ports
== 2) ? do_bond
: false;
2614 /* handle only events that come with enough info */
2615 if ((do_bond
&& (event
!= NETDEV_BONDING_INFO
)) || !port
)
2618 priv
= netdev_priv(ndev
);
2620 struct netdev_notifier_bonding_info
*notifier_info
= ptr
;
2621 struct netdev_bonding_info
*bonding_info
=
2622 ¬ifier_info
->bonding_info
;
2624 /* required mode 1, 2 or 4 */
2625 if ((bonding_info
->master
.bond_mode
!= BOND_MODE_ACTIVEBACKUP
) &&
2626 (bonding_info
->master
.bond_mode
!= BOND_MODE_XOR
) &&
2627 (bonding_info
->master
.bond_mode
!= BOND_MODE_8023AD
))
2630 /* require exactly 2 slaves */
2631 if (bonding_info
->master
.num_slaves
!= 2)
2636 if (bonding_info
->master
.bond_mode
==
2637 BOND_MODE_ACTIVEBACKUP
) {
2638 /* in active-backup mode virtual ports are
2639 * mapped to the physical port of the active
2641 if (bonding_info
->slave
.state
==
2642 BOND_STATE_BACKUP
) {
2650 } else { /* BOND_STATE_ACTIVE */
2659 } else { /* Active-Active */
2660 /* in active-active mode a virtual port is
2661 * mapped to the native physical port if and only
2662 * if the physical port is up */
2663 __s8 link
= bonding_info
->slave
.link
;
2669 if ((link
== BOND_LINK_UP
) ||
2670 (link
== BOND_LINK_FAIL
)) {
2675 } else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
2685 mlx4_en_queue_bond_work(priv
, do_bond
,
2686 v2p_port1
, v2p_port2
);
2691 void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev
*dev
,
2692 struct mlx4_en_stats_bitmap
*stats_bitmap
,
2693 u8 rx_ppp
, u8 rx_pause
,
2694 u8 tx_ppp
, u8 tx_pause
)
2696 int last_i
= NUM_MAIN_STATS
+ NUM_PORT_STATS
+ NUM_PF_STATS
;
2698 if (!mlx4_is_slave(dev
) &&
2699 (dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN
)) {
2700 mutex_lock(&stats_bitmap
->mutex
);
2701 bitmap_clear(stats_bitmap
->bitmap
, last_i
, NUM_FLOW_STATS
);
2704 bitmap_set(stats_bitmap
->bitmap
, last_i
,
2705 NUM_FLOW_PRIORITY_STATS_RX
);
2706 last_i
+= NUM_FLOW_PRIORITY_STATS_RX
;
2708 if (rx_pause
&& !(rx_ppp
))
2709 bitmap_set(stats_bitmap
->bitmap
, last_i
,
2711 last_i
+= NUM_FLOW_STATS_RX
;
2714 bitmap_set(stats_bitmap
->bitmap
, last_i
,
2715 NUM_FLOW_PRIORITY_STATS_TX
);
2716 last_i
+= NUM_FLOW_PRIORITY_STATS_TX
;
2718 if (tx_pause
&& !(tx_ppp
))
2719 bitmap_set(stats_bitmap
->bitmap
, last_i
,
2721 last_i
+= NUM_FLOW_STATS_TX
;
2723 mutex_unlock(&stats_bitmap
->mutex
);
2727 void mlx4_en_set_stats_bitmap(struct mlx4_dev
*dev
,
2728 struct mlx4_en_stats_bitmap
*stats_bitmap
,
2729 u8 rx_ppp
, u8 rx_pause
,
2730 u8 tx_ppp
, u8 tx_pause
)
2734 mutex_init(&stats_bitmap
->mutex
);
2735 bitmap_zero(stats_bitmap
->bitmap
, NUM_ALL_STATS
);
2737 if (mlx4_is_slave(dev
)) {
2738 bitmap_set(stats_bitmap
->bitmap
, last_i
+
2739 MLX4_FIND_NETDEV_STAT(rx_packets
), 1);
2740 bitmap_set(stats_bitmap
->bitmap
, last_i
+
2741 MLX4_FIND_NETDEV_STAT(tx_packets
), 1);
2742 bitmap_set(stats_bitmap
->bitmap
, last_i
+
2743 MLX4_FIND_NETDEV_STAT(rx_bytes
), 1);
2744 bitmap_set(stats_bitmap
->bitmap
, last_i
+
2745 MLX4_FIND_NETDEV_STAT(tx_bytes
), 1);
2746 bitmap_set(stats_bitmap
->bitmap
, last_i
+
2747 MLX4_FIND_NETDEV_STAT(rx_dropped
), 1);
2748 bitmap_set(stats_bitmap
->bitmap
, last_i
+
2749 MLX4_FIND_NETDEV_STAT(tx_dropped
), 1);
2751 bitmap_set(stats_bitmap
->bitmap
, last_i
, NUM_MAIN_STATS
);
2753 last_i
+= NUM_MAIN_STATS
;
2755 bitmap_set(stats_bitmap
->bitmap
, last_i
, NUM_PORT_STATS
);
2756 last_i
+= NUM_PORT_STATS
;
2758 if (mlx4_is_master(dev
))
2759 bitmap_set(stats_bitmap
->bitmap
, last_i
,
2761 last_i
+= NUM_PF_STATS
;
2763 mlx4_en_update_pfc_stats_bitmap(dev
, stats_bitmap
,
2766 last_i
+= NUM_FLOW_STATS
;
2768 if (!mlx4_is_slave(dev
))
2769 bitmap_set(stats_bitmap
->bitmap
, last_i
, NUM_PKT_STATS
);
2772 int mlx4_en_init_netdev(struct mlx4_en_dev
*mdev
, int port
,
2773 struct mlx4_en_port_profile
*prof
)
2775 struct net_device
*dev
;
2776 struct mlx4_en_priv
*priv
;
2780 dev
= alloc_etherdev_mqs(sizeof(struct mlx4_en_priv
),
2781 MAX_TX_RINGS
, MAX_RX_RINGS
);
2785 netif_set_real_num_tx_queues(dev
, prof
->tx_ring_num
);
2786 netif_set_real_num_rx_queues(dev
, prof
->rx_ring_num
);
2788 SET_NETDEV_DEV(dev
, &mdev
->dev
->persist
->pdev
->dev
);
2789 dev
->dev_port
= port
- 1;
2792 * Initialize driver private data
2795 priv
= netdev_priv(dev
);
2796 memset(priv
, 0, sizeof(struct mlx4_en_priv
));
2797 priv
->counter_index
= MLX4_SINK_COUNTER_INDEX(mdev
->dev
);
2798 spin_lock_init(&priv
->stats_lock
);
2799 INIT_WORK(&priv
->rx_mode_task
, mlx4_en_do_set_rx_mode
);
2800 INIT_WORK(&priv
->watchdog_task
, mlx4_en_restart
);
2801 INIT_WORK(&priv
->linkstate_task
, mlx4_en_linkstate
);
2802 INIT_DELAYED_WORK(&priv
->stats_task
, mlx4_en_do_get_stats
);
2803 INIT_DELAYED_WORK(&priv
->service_task
, mlx4_en_service_task
);
2804 #ifdef CONFIG_MLX4_EN_VXLAN
2805 INIT_WORK(&priv
->vxlan_add_task
, mlx4_en_add_vxlan_offloads
);
2806 INIT_WORK(&priv
->vxlan_del_task
, mlx4_en_del_vxlan_offloads
);
2808 #ifdef CONFIG_RFS_ACCEL
2809 INIT_LIST_HEAD(&priv
->filters
);
2810 spin_lock_init(&priv
->filters_lock
);
2815 priv
->ddev
= &mdev
->pdev
->dev
;
2818 priv
->port_up
= false;
2819 priv
->flags
= prof
->flags
;
2820 priv
->pflags
= MLX4_EN_PRIV_FLAGS_BLUEFLAME
;
2821 priv
->ctrl_flags
= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE
|
2822 MLX4_WQE_CTRL_SOLICITED
);
2823 priv
->num_tx_rings_p_up
= mdev
->profile
.num_tx_rings_p_up
;
2824 priv
->tx_ring_num
= prof
->tx_ring_num
;
2825 priv
->tx_work_limit
= MLX4_EN_DEFAULT_TX_WORK
;
2826 netdev_rss_key_fill(priv
->rss_key
, sizeof(priv
->rss_key
));
2828 priv
->tx_ring
= kzalloc(sizeof(struct mlx4_en_tx_ring
*) * MAX_TX_RINGS
,
2830 if (!priv
->tx_ring
) {
2834 priv
->tx_cq
= kzalloc(sizeof(struct mlx4_en_cq
*) * MAX_TX_RINGS
,
2840 priv
->rx_ring_num
= prof
->rx_ring_num
;
2841 priv
->cqe_factor
= (mdev
->dev
->caps
.cqe_size
== 64) ? 1 : 0;
2842 priv
->cqe_size
= mdev
->dev
->caps
.cqe_size
;
2843 priv
->mac_index
= -1;
2844 priv
->msg_enable
= MLX4_EN_MSG_LEVEL
;
2845 #ifdef CONFIG_MLX4_EN_DCB
2846 if (!mlx4_is_slave(priv
->mdev
->dev
)) {
2847 if (mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_ETS_CFG
) {
2848 dev
->dcbnl_ops
= &mlx4_en_dcbnl_ops
;
2850 en_info(priv
, "enabling only PFC DCB ops\n");
2851 dev
->dcbnl_ops
= &mlx4_en_dcbnl_pfc_ops
;
2856 for (i
= 0; i
< MLX4_EN_MAC_HASH_SIZE
; ++i
)
2857 INIT_HLIST_HEAD(&priv
->mac_hash
[i
]);
2859 /* Query for default mac and max mtu */
2860 priv
->max_mtu
= mdev
->dev
->caps
.eth_mtu_cap
[priv
->port
];
2862 if (mdev
->dev
->caps
.rx_checksum_flags_port
[priv
->port
] &
2863 MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP
)
2864 priv
->flags
|= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP
;
2866 /* Set default MAC */
2867 dev
->addr_len
= ETH_ALEN
;
2868 mlx4_en_u64_to_mac(dev
->dev_addr
, mdev
->dev
->caps
.def_mac
[priv
->port
]);
2869 if (!is_valid_ether_addr(dev
->dev_addr
)) {
2870 en_err(priv
, "Port: %d, invalid mac burned: %pM, quiting\n",
2871 priv
->port
, dev
->dev_addr
);
2874 } else if (mlx4_is_slave(priv
->mdev
->dev
) &&
2875 (priv
->mdev
->dev
->port_random_macs
& 1 << priv
->port
)) {
2876 /* Random MAC was assigned in mlx4_slave_cap
2877 * in mlx4_core module
2879 dev
->addr_assign_type
|= NET_ADDR_RANDOM
;
2880 en_warn(priv
, "Assigned random MAC address %pM\n", dev
->dev_addr
);
2883 memcpy(priv
->current_mac
, dev
->dev_addr
, sizeof(priv
->current_mac
));
2885 priv
->stride
= roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc
) +
2886 DS_SIZE
* MLX4_EN_MAX_RX_FRAGS
);
2887 err
= mlx4_en_alloc_resources(priv
);
2891 /* Initialize time stamping config */
2892 priv
->hwtstamp_config
.flags
= 0;
2893 priv
->hwtstamp_config
.tx_type
= HWTSTAMP_TX_OFF
;
2894 priv
->hwtstamp_config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
2896 /* Allocate page for receive rings */
2897 err
= mlx4_alloc_hwq_res(mdev
->dev
, &priv
->res
,
2898 MLX4_EN_PAGE_SIZE
, MLX4_EN_PAGE_SIZE
);
2900 en_err(priv
, "Failed to allocate page for rx qps\n");
2903 priv
->allocated
= 1;
2906 * Initialize netdev entry points
2908 if (mlx4_is_master(priv
->mdev
->dev
))
2909 dev
->netdev_ops
= &mlx4_netdev_ops_master
;
2911 dev
->netdev_ops
= &mlx4_netdev_ops
;
2912 dev
->watchdog_timeo
= MLX4_EN_WATCHDOG_TIMEOUT
;
2913 netif_set_real_num_tx_queues(dev
, priv
->tx_ring_num
);
2914 netif_set_real_num_rx_queues(dev
, priv
->rx_ring_num
);
2916 dev
->ethtool_ops
= &mlx4_en_ethtool_ops
;
2919 * Set driver features
2921 dev
->hw_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
2922 if (mdev
->LSO_support
)
2923 dev
->hw_features
|= NETIF_F_TSO
| NETIF_F_TSO6
;
2925 dev
->vlan_features
= dev
->hw_features
;
2927 dev
->hw_features
|= NETIF_F_RXCSUM
| NETIF_F_RXHASH
;
2928 dev
->features
= dev
->hw_features
| NETIF_F_HIGHDMA
|
2929 NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_RX
|
2930 NETIF_F_HW_VLAN_CTAG_FILTER
;
2931 dev
->hw_features
|= NETIF_F_LOOPBACK
|
2932 NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_RX
;
2934 if (!(mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN
)) {
2935 dev
->features
|= NETIF_F_HW_VLAN_STAG_RX
|
2936 NETIF_F_HW_VLAN_STAG_FILTER
;
2937 dev
->hw_features
|= NETIF_F_HW_VLAN_STAG_RX
;
2940 if (mlx4_is_slave(mdev
->dev
)) {
2943 err
= get_phv_bit(mdev
->dev
, port
, &phv
);
2945 dev
->hw_features
|= NETIF_F_HW_VLAN_STAG_TX
;
2946 priv
->pflags
|= MLX4_EN_PRIV_FLAGS_PHV
;
2949 if (mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_PHV_EN
&&
2950 !(mdev
->dev
->caps
.flags2
&
2951 MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN
))
2952 dev
->hw_features
|= NETIF_F_HW_VLAN_STAG_TX
;
2955 if (mdev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_FCS_KEEP
)
2956 dev
->hw_features
|= NETIF_F_RXFCS
;
2958 if (mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_IGNORE_FCS
)
2959 dev
->hw_features
|= NETIF_F_RXALL
;
2961 if (mdev
->dev
->caps
.steering_mode
==
2962 MLX4_STEERING_MODE_DEVICE_MANAGED
&&
2963 mdev
->dev
->caps
.dmfs_high_steer_mode
!= MLX4_STEERING_DMFS_A0_STATIC
)
2964 dev
->hw_features
|= NETIF_F_NTUPLE
;
2966 if (mdev
->dev
->caps
.steering_mode
!= MLX4_STEERING_MODE_A0
)
2967 dev
->priv_flags
|= IFF_UNICAST_FLT
;
2969 /* Setting a default hash function value */
2970 if (mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_RSS_TOP
) {
2971 priv
->rss_hash_fn
= ETH_RSS_HASH_TOP
;
2972 } else if (mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_RSS_XOR
) {
2973 priv
->rss_hash_fn
= ETH_RSS_HASH_XOR
;
2976 "No RSS hash capabilities exposed, using Toeplitz\n");
2977 priv
->rss_hash_fn
= ETH_RSS_HASH_TOP
;
2980 mdev
->pndev
[port
] = dev
;
2981 mdev
->upper
[port
] = NULL
;
2983 netif_carrier_off(dev
);
2984 mlx4_en_set_default_moderation(priv
);
2986 en_warn(priv
, "Using %d TX rings\n", prof
->tx_ring_num
);
2987 en_warn(priv
, "Using %d RX rings\n", prof
->rx_ring_num
);
2989 mlx4_en_update_loopback_state(priv
->dev
, priv
->dev
->features
);
2991 /* Configure port */
2992 mlx4_en_calc_rx_buf(dev
);
2993 err
= mlx4_SET_PORT_general(mdev
->dev
, priv
->port
,
2994 priv
->rx_skb_size
+ ETH_FCS_LEN
,
2995 prof
->tx_pause
, prof
->tx_ppp
,
2996 prof
->rx_pause
, prof
->rx_ppp
);
2998 en_err(priv
, "Failed setting port general configurations for port %d, with error %d\n",
3003 if (mdev
->dev
->caps
.tunnel_offload_mode
== MLX4_TUNNEL_OFFLOAD_MODE_VXLAN
) {
3004 err
= mlx4_SET_PORT_VXLAN(mdev
->dev
, priv
->port
, VXLAN_STEER_BY_OUTER_MAC
, 1);
3006 en_err(priv
, "Failed setting port L2 tunnel configuration, err %d\n",
3013 en_warn(priv
, "Initializing port\n");
3014 err
= mlx4_INIT_PORT(mdev
->dev
, priv
->port
);
3016 en_err(priv
, "Failed Initializing port\n");
3019 queue_delayed_work(mdev
->workqueue
, &priv
->stats_task
, STATS_DELAY
);
3021 if (mdev
->dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_TS
)
3022 queue_delayed_work(mdev
->workqueue
, &priv
->service_task
,
3023 SERVICE_TASK_DELAY
);
3025 mlx4_en_set_stats_bitmap(mdev
->dev
, &priv
->stats_bitmap
,
3026 mdev
->profile
.prof
[priv
->port
].rx_ppp
,
3027 mdev
->profile
.prof
[priv
->port
].rx_pause
,
3028 mdev
->profile
.prof
[priv
->port
].tx_ppp
,
3029 mdev
->profile
.prof
[priv
->port
].tx_pause
);
3031 err
= register_netdev(dev
);
3033 en_err(priv
, "Netdev registration failed for port %d\n", port
);
3037 priv
->registered
= 1;
3042 mlx4_en_destroy_netdev(dev
);
3046 int mlx4_en_reset_config(struct net_device
*dev
,
3047 struct hwtstamp_config ts_config
,
3048 netdev_features_t features
)
3050 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
3051 struct mlx4_en_dev
*mdev
= priv
->mdev
;
3055 if (priv
->hwtstamp_config
.tx_type
== ts_config
.tx_type
&&
3056 priv
->hwtstamp_config
.rx_filter
== ts_config
.rx_filter
&&
3057 !DEV_FEATURE_CHANGED(dev
, features
, NETIF_F_HW_VLAN_CTAG_RX
) &&
3058 !DEV_FEATURE_CHANGED(dev
, features
, NETIF_F_RXFCS
))
3059 return 0; /* Nothing to change */
3061 if (DEV_FEATURE_CHANGED(dev
, features
, NETIF_F_HW_VLAN_CTAG_RX
) &&
3062 (features
& NETIF_F_HW_VLAN_CTAG_RX
) &&
3063 (priv
->hwtstamp_config
.rx_filter
!= HWTSTAMP_FILTER_NONE
)) {
3064 en_warn(priv
, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
3068 mutex_lock(&mdev
->state_lock
);
3069 if (priv
->port_up
) {
3071 mlx4_en_stop_port(dev
, 1);
3074 mlx4_en_free_resources(priv
);
3076 en_warn(priv
, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
3077 ts_config
.rx_filter
, !!(features
& NETIF_F_HW_VLAN_CTAG_RX
));
3079 priv
->hwtstamp_config
.tx_type
= ts_config
.tx_type
;
3080 priv
->hwtstamp_config
.rx_filter
= ts_config
.rx_filter
;
3082 if (DEV_FEATURE_CHANGED(dev
, features
, NETIF_F_HW_VLAN_CTAG_RX
)) {
3083 if (features
& NETIF_F_HW_VLAN_CTAG_RX
)
3084 dev
->features
|= NETIF_F_HW_VLAN_CTAG_RX
;
3086 dev
->features
&= ~NETIF_F_HW_VLAN_CTAG_RX
;
3087 } else if (ts_config
.rx_filter
== HWTSTAMP_FILTER_NONE
) {
3088 /* RX time-stamping is OFF, update the RX vlan offload
3089 * to the latest wanted state
3091 if (dev
->wanted_features
& NETIF_F_HW_VLAN_CTAG_RX
)
3092 dev
->features
|= NETIF_F_HW_VLAN_CTAG_RX
;
3094 dev
->features
&= ~NETIF_F_HW_VLAN_CTAG_RX
;
3097 if (DEV_FEATURE_CHANGED(dev
, features
, NETIF_F_RXFCS
)) {
3098 if (features
& NETIF_F_RXFCS
)
3099 dev
->features
|= NETIF_F_RXFCS
;
3101 dev
->features
&= ~NETIF_F_RXFCS
;
3104 /* RX vlan offload and RX time-stamping can't co-exist !
3105 * Regardless of the caller's choice,
3106 * Turn Off RX vlan offload in case of time-stamping is ON
3108 if (ts_config
.rx_filter
!= HWTSTAMP_FILTER_NONE
) {
3109 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_RX
)
3110 en_warn(priv
, "Turning off RX vlan offload since RX time-stamping is ON\n");
3111 dev
->features
&= ~NETIF_F_HW_VLAN_CTAG_RX
;
3114 err
= mlx4_en_alloc_resources(priv
);
3116 en_err(priv
, "Failed reallocating port resources\n");
3120 err
= mlx4_en_start_port(dev
);
3122 en_err(priv
, "Failed starting port\n");
3126 mutex_unlock(&mdev
->state_lock
);
3127 netdev_features_change(dev
);