net/mlx4_en: Extend usage of napi_gro_frags
[deliverable/linux.git] / drivers / net / ethernet / mellanox / mlx4 / en_netdev.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
31 *
32 */
33
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>
40#include <net/ip.h>
41#include <net/busy_poll.h>
42#include <net/vxlan.h>
43
44#include <linux/mlx4/driver.h>
45#include <linux/mlx4/device.h>
46#include <linux/mlx4/cmd.h>
47#include <linux/mlx4/cq.h>
48
49#include "mlx4_en.h"
50#include "en_port.h"
51
52int mlx4_en_setup_tc(struct net_device *dev, u8 up)
53{
54 struct mlx4_en_priv *priv = netdev_priv(dev);
55 int i;
56 unsigned int offset = 0;
57
58 if (up && up != MLX4_EN_NUM_UP)
59 return -EINVAL;
60
61 netdev_set_num_tc(dev, up);
62
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;
67 }
68
69 return 0;
70}
71
72#ifdef CONFIG_NET_RX_BUSY_POLL
73/* must be called with local_bh_disable()d */
74static int mlx4_en_low_latency_recv(struct napi_struct *napi)
75{
76 struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
77 struct net_device *dev = cq->dev;
78 struct mlx4_en_priv *priv = netdev_priv(dev);
79 struct mlx4_en_rx_ring *rx_ring = priv->rx_ring[cq->ring];
80 int done;
81
82 if (!priv->port_up)
83 return LL_FLUSH_FAILED;
84
85 if (!mlx4_en_cq_lock_poll(cq))
86 return LL_FLUSH_BUSY;
87
88 done = mlx4_en_process_rx_cq(dev, cq, 4);
89 if (likely(done))
90 rx_ring->cleaned += done;
91 else
92 rx_ring->misses++;
93
94 mlx4_en_cq_unlock_poll(cq);
95
96 return done;
97}
98#endif /* CONFIG_NET_RX_BUSY_POLL */
99
100#ifdef CONFIG_RFS_ACCEL
101
102struct mlx4_en_filter {
103 struct list_head next;
104 struct work_struct work;
105
106 u8 ip_proto;
107 __be32 src_ip;
108 __be32 dst_ip;
109 __be16 src_port;
110 __be16 dst_port;
111
112 int rxq_index;
113 struct mlx4_en_priv *priv;
114 u32 flow_id; /* RFS infrastructure id */
115 int id; /* mlx4_en driver id */
116 u64 reg_id; /* Flow steering API id */
117 u8 activated; /* Used to prevent expiry before filter
118 * is attached
119 */
120 struct hlist_node filter_chain;
121};
122
123static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
124
125static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
126{
127 switch (ip_proto) {
128 case IPPROTO_UDP:
129 return MLX4_NET_TRANS_RULE_ID_UDP;
130 case IPPROTO_TCP:
131 return MLX4_NET_TRANS_RULE_ID_TCP;
132 default:
133 return MLX4_NET_TRANS_RULE_NUM;
134 }
135};
136
137static void mlx4_en_filter_work(struct work_struct *work)
138{
139 struct mlx4_en_filter *filter = container_of(work,
140 struct mlx4_en_filter,
141 work);
142 struct mlx4_en_priv *priv = filter->priv;
143 struct mlx4_spec_list spec_tcp_udp = {
144 .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
145 {
146 .tcp_udp = {
147 .dst_port = filter->dst_port,
148 .dst_port_msk = (__force __be16)-1,
149 .src_port = filter->src_port,
150 .src_port_msk = (__force __be16)-1,
151 },
152 },
153 };
154 struct mlx4_spec_list spec_ip = {
155 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
156 {
157 .ipv4 = {
158 .dst_ip = filter->dst_ip,
159 .dst_ip_msk = (__force __be32)-1,
160 .src_ip = filter->src_ip,
161 .src_ip_msk = (__force __be32)-1,
162 },
163 },
164 };
165 struct mlx4_spec_list spec_eth = {
166 .id = MLX4_NET_TRANS_RULE_ID_ETH,
167 };
168 struct mlx4_net_trans_rule rule = {
169 .list = LIST_HEAD_INIT(rule.list),
170 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
171 .exclusive = 1,
172 .allow_loopback = 1,
173 .promisc_mode = MLX4_FS_REGULAR,
174 .port = priv->port,
175 .priority = MLX4_DOMAIN_RFS,
176 };
177 int rc;
178 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
179
180 if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
181 en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
182 filter->ip_proto);
183 goto ignore;
184 }
185 list_add_tail(&spec_eth.list, &rule.list);
186 list_add_tail(&spec_ip.list, &rule.list);
187 list_add_tail(&spec_tcp_udp.list, &rule.list);
188
189 rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
190 memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
191 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
192
193 filter->activated = 0;
194
195 if (filter->reg_id) {
196 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
197 if (rc && rc != -ENOENT)
198 en_err(priv, "Error detaching flow. rc = %d\n", rc);
199 }
200
201 rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
202 if (rc)
203 en_err(priv, "Error attaching flow. err = %d\n", rc);
204
205ignore:
206 mlx4_en_filter_rfs_expire(priv);
207
208 filter->activated = 1;
209}
210
211static inline struct hlist_head *
212filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
213 __be16 src_port, __be16 dst_port)
214{
215 unsigned long l;
216 int bucket_idx;
217
218 l = (__force unsigned long)src_port |
219 ((__force unsigned long)dst_port << 2);
220 l ^= (__force unsigned long)(src_ip ^ dst_ip);
221
222 bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
223
224 return &priv->filter_hash[bucket_idx];
225}
226
227static struct mlx4_en_filter *
228mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
229 __be32 dst_ip, u8 ip_proto, __be16 src_port,
230 __be16 dst_port, u32 flow_id)
231{
232 struct mlx4_en_filter *filter = NULL;
233
234 filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
235 if (!filter)
236 return NULL;
237
238 filter->priv = priv;
239 filter->rxq_index = rxq_index;
240 INIT_WORK(&filter->work, mlx4_en_filter_work);
241
242 filter->src_ip = src_ip;
243 filter->dst_ip = dst_ip;
244 filter->ip_proto = ip_proto;
245 filter->src_port = src_port;
246 filter->dst_port = dst_port;
247
248 filter->flow_id = flow_id;
249
250 filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
251
252 list_add_tail(&filter->next, &priv->filters);
253 hlist_add_head(&filter->filter_chain,
254 filter_hash_bucket(priv, src_ip, dst_ip, src_port,
255 dst_port));
256
257 return filter;
258}
259
260static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
261{
262 struct mlx4_en_priv *priv = filter->priv;
263 int rc;
264
265 list_del(&filter->next);
266
267 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
268 if (rc && rc != -ENOENT)
269 en_err(priv, "Error detaching flow. rc = %d\n", rc);
270
271 kfree(filter);
272}
273
274static inline struct mlx4_en_filter *
275mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
276 u8 ip_proto, __be16 src_port, __be16 dst_port)
277{
278 struct mlx4_en_filter *filter;
279 struct mlx4_en_filter *ret = NULL;
280
281 hlist_for_each_entry(filter,
282 filter_hash_bucket(priv, src_ip, dst_ip,
283 src_port, dst_port),
284 filter_chain) {
285 if (filter->src_ip == src_ip &&
286 filter->dst_ip == dst_ip &&
287 filter->ip_proto == ip_proto &&
288 filter->src_port == src_port &&
289 filter->dst_port == dst_port) {
290 ret = filter;
291 break;
292 }
293 }
294
295 return ret;
296}
297
298static int
299mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
300 u16 rxq_index, u32 flow_id)
301{
302 struct mlx4_en_priv *priv = netdev_priv(net_dev);
303 struct mlx4_en_filter *filter;
304 const struct iphdr *ip;
305 const __be16 *ports;
306 u8 ip_proto;
307 __be32 src_ip;
308 __be32 dst_ip;
309 __be16 src_port;
310 __be16 dst_port;
311 int nhoff = skb_network_offset(skb);
312 int ret = 0;
313
314 if (skb->protocol != htons(ETH_P_IP))
315 return -EPROTONOSUPPORT;
316
317 ip = (const struct iphdr *)(skb->data + nhoff);
318 if (ip_is_fragment(ip))
319 return -EPROTONOSUPPORT;
320
321 if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
322 return -EPROTONOSUPPORT;
323 ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
324
325 ip_proto = ip->protocol;
326 src_ip = ip->saddr;
327 dst_ip = ip->daddr;
328 src_port = ports[0];
329 dst_port = ports[1];
330
331 spin_lock_bh(&priv->filters_lock);
332 filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
333 src_port, dst_port);
334 if (filter) {
335 if (filter->rxq_index == rxq_index)
336 goto out;
337
338 filter->rxq_index = rxq_index;
339 } else {
340 filter = mlx4_en_filter_alloc(priv, rxq_index,
341 src_ip, dst_ip, ip_proto,
342 src_port, dst_port, flow_id);
343 if (!filter) {
344 ret = -ENOMEM;
345 goto err;
346 }
347 }
348
349 queue_work(priv->mdev->workqueue, &filter->work);
350
351out:
352 ret = filter->id;
353err:
354 spin_unlock_bh(&priv->filters_lock);
355
356 return ret;
357}
358
359void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
360{
361 struct mlx4_en_filter *filter, *tmp;
362 LIST_HEAD(del_list);
363
364 spin_lock_bh(&priv->filters_lock);
365 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
366 list_move(&filter->next, &del_list);
367 hlist_del(&filter->filter_chain);
368 }
369 spin_unlock_bh(&priv->filters_lock);
370
371 list_for_each_entry_safe(filter, tmp, &del_list, next) {
372 cancel_work_sync(&filter->work);
373 mlx4_en_filter_free(filter);
374 }
375}
376
377static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
378{
379 struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
380 LIST_HEAD(del_list);
381 int i = 0;
382
383 spin_lock_bh(&priv->filters_lock);
384 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
385 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
386 break;
387
388 if (filter->activated &&
389 !work_pending(&filter->work) &&
390 rps_may_expire_flow(priv->dev,
391 filter->rxq_index, filter->flow_id,
392 filter->id)) {
393 list_move(&filter->next, &del_list);
394 hlist_del(&filter->filter_chain);
395 } else
396 last_filter = filter;
397
398 i++;
399 }
400
401 if (last_filter && (&last_filter->next != priv->filters.next))
402 list_move(&priv->filters, &last_filter->next);
403
404 spin_unlock_bh(&priv->filters_lock);
405
406 list_for_each_entry_safe(filter, tmp, &del_list, next)
407 mlx4_en_filter_free(filter);
408}
409#endif
410
411static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
412 __be16 proto, u16 vid)
413{
414 struct mlx4_en_priv *priv = netdev_priv(dev);
415 struct mlx4_en_dev *mdev = priv->mdev;
416 int err;
417 int idx;
418
419 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
420
421 set_bit(vid, priv->active_vlans);
422
423 /* Add VID to port VLAN filter */
424 mutex_lock(&mdev->state_lock);
425 if (mdev->device_up && priv->port_up) {
426 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
427 if (err)
428 en_err(priv, "Failed configuring VLAN filter\n");
429 }
430 if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
431 en_dbg(HW, priv, "failed adding vlan %d\n", vid);
432 mutex_unlock(&mdev->state_lock);
433
434 return 0;
435}
436
437static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
438 __be16 proto, u16 vid)
439{
440 struct mlx4_en_priv *priv = netdev_priv(dev);
441 struct mlx4_en_dev *mdev = priv->mdev;
442 int err;
443
444 en_dbg(HW, priv, "Killing VID:%d\n", vid);
445
446 clear_bit(vid, priv->active_vlans);
447
448 /* Remove VID from port VLAN filter */
449 mutex_lock(&mdev->state_lock);
450 mlx4_unregister_vlan(mdev->dev, priv->port, vid);
451
452 if (mdev->device_up && priv->port_up) {
453 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
454 if (err)
455 en_err(priv, "Failed configuring VLAN filter\n");
456 }
457 mutex_unlock(&mdev->state_lock);
458
459 return 0;
460}
461
462static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
463{
464 int i;
465 for (i = ETH_ALEN - 1; i >= 0; --i) {
466 dst_mac[i] = src_mac & 0xff;
467 src_mac >>= 8;
468 }
469 memset(&dst_mac[ETH_ALEN], 0, 2);
470}
471
472
473static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
474 int qpn, u64 *reg_id)
475{
476 int err;
477
478 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
479 return 0; /* do nothing */
480
481 err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
482 MLX4_DOMAIN_NIC, reg_id);
483 if (err) {
484 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
485 return err;
486 }
487 en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
488 return 0;
489}
490
491
492static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
493 unsigned char *mac, int *qpn, u64 *reg_id)
494{
495 struct mlx4_en_dev *mdev = priv->mdev;
496 struct mlx4_dev *dev = mdev->dev;
497 int err;
498
499 switch (dev->caps.steering_mode) {
500 case MLX4_STEERING_MODE_B0: {
501 struct mlx4_qp qp;
502 u8 gid[16] = {0};
503
504 qp.qpn = *qpn;
505 memcpy(&gid[10], mac, ETH_ALEN);
506 gid[5] = priv->port;
507
508 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
509 break;
510 }
511 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
512 struct mlx4_spec_list spec_eth = { {NULL} };
513 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
514
515 struct mlx4_net_trans_rule rule = {
516 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
517 .exclusive = 0,
518 .allow_loopback = 1,
519 .promisc_mode = MLX4_FS_REGULAR,
520 .priority = MLX4_DOMAIN_NIC,
521 };
522
523 rule.port = priv->port;
524 rule.qpn = *qpn;
525 INIT_LIST_HEAD(&rule.list);
526
527 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
528 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
529 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
530 list_add_tail(&spec_eth.list, &rule.list);
531
532 err = mlx4_flow_attach(dev, &rule, reg_id);
533 break;
534 }
535 default:
536 return -EINVAL;
537 }
538 if (err)
539 en_warn(priv, "Failed Attaching Unicast\n");
540
541 return err;
542}
543
544static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
545 unsigned char *mac, int qpn, u64 reg_id)
546{
547 struct mlx4_en_dev *mdev = priv->mdev;
548 struct mlx4_dev *dev = mdev->dev;
549
550 switch (dev->caps.steering_mode) {
551 case MLX4_STEERING_MODE_B0: {
552 struct mlx4_qp qp;
553 u8 gid[16] = {0};
554
555 qp.qpn = qpn;
556 memcpy(&gid[10], mac, ETH_ALEN);
557 gid[5] = priv->port;
558
559 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
560 break;
561 }
562 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
563 mlx4_flow_detach(dev, reg_id);
564 break;
565 }
566 default:
567 en_err(priv, "Invalid steering mode.\n");
568 }
569}
570
571static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
572{
573 struct mlx4_en_dev *mdev = priv->mdev;
574 struct mlx4_dev *dev = mdev->dev;
575 struct mlx4_mac_entry *entry;
576 int index = 0;
577 int err = 0;
578 u64 reg_id = 0;
579 int *qpn = &priv->base_qpn;
580 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
581
582 en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
583 priv->dev->dev_addr);
584 index = mlx4_register_mac(dev, priv->port, mac);
585 if (index < 0) {
586 err = index;
587 en_err(priv, "Failed adding MAC: %pM\n",
588 priv->dev->dev_addr);
589 return err;
590 }
591
592 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
593 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
594 *qpn = base_qpn + index;
595 return 0;
596 }
597
598 err = mlx4_qp_reserve_range(dev, 1, 1, qpn);
599 en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
600 if (err) {
601 en_err(priv, "Failed to reserve qp for mac registration\n");
602 goto qp_err;
603 }
604
605 err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, &reg_id);
606 if (err)
607 goto steer_err;
608
609 err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
610 &priv->tunnel_reg_id);
611 if (err)
612 goto tunnel_err;
613
614 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
615 if (!entry) {
616 err = -ENOMEM;
617 goto alloc_err;
618 }
619 memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
620 memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
621 entry->reg_id = reg_id;
622
623 hlist_add_head_rcu(&entry->hlist,
624 &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
625
626 return 0;
627
628alloc_err:
629 if (priv->tunnel_reg_id)
630 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
631tunnel_err:
632 mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
633
634steer_err:
635 mlx4_qp_release_range(dev, *qpn, 1);
636
637qp_err:
638 mlx4_unregister_mac(dev, priv->port, mac);
639 return err;
640}
641
642static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
643{
644 struct mlx4_en_dev *mdev = priv->mdev;
645 struct mlx4_dev *dev = mdev->dev;
646 int qpn = priv->base_qpn;
647 u64 mac;
648
649 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
650 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
651 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
652 priv->dev->dev_addr);
653 mlx4_unregister_mac(dev, priv->port, mac);
654 } else {
655 struct mlx4_mac_entry *entry;
656 struct hlist_node *tmp;
657 struct hlist_head *bucket;
658 unsigned int i;
659
660 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
661 bucket = &priv->mac_hash[i];
662 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
663 mac = mlx4_mac_to_u64(entry->mac);
664 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
665 entry->mac);
666 mlx4_en_uc_steer_release(priv, entry->mac,
667 qpn, entry->reg_id);
668
669 mlx4_unregister_mac(dev, priv->port, mac);
670 hlist_del_rcu(&entry->hlist);
671 kfree_rcu(entry, rcu);
672 }
673 }
674
675 if (priv->tunnel_reg_id) {
676 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
677 priv->tunnel_reg_id = 0;
678 }
679
680 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
681 priv->port, qpn);
682 mlx4_qp_release_range(dev, qpn, 1);
683 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
684 }
685}
686
687static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
688 unsigned char *new_mac, unsigned char *prev_mac)
689{
690 struct mlx4_en_dev *mdev = priv->mdev;
691 struct mlx4_dev *dev = mdev->dev;
692 int err = 0;
693 u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
694
695 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
696 struct hlist_head *bucket;
697 unsigned int mac_hash;
698 struct mlx4_mac_entry *entry;
699 struct hlist_node *tmp;
700 u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
701
702 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
703 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
704 if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
705 mlx4_en_uc_steer_release(priv, entry->mac,
706 qpn, entry->reg_id);
707 mlx4_unregister_mac(dev, priv->port,
708 prev_mac_u64);
709 hlist_del_rcu(&entry->hlist);
710 synchronize_rcu();
711 memcpy(entry->mac, new_mac, ETH_ALEN);
712 entry->reg_id = 0;
713 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
714 hlist_add_head_rcu(&entry->hlist,
715 &priv->mac_hash[mac_hash]);
716 mlx4_register_mac(dev, priv->port, new_mac_u64);
717 err = mlx4_en_uc_steer_add(priv, new_mac,
718 &qpn,
719 &entry->reg_id);
720 if (err)
721 return err;
722 if (priv->tunnel_reg_id) {
723 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
724 priv->tunnel_reg_id = 0;
725 }
726 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
727 &priv->tunnel_reg_id);
728 return err;
729 }
730 }
731 return -EINVAL;
732 }
733
734 return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
735}
736
737static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
738 unsigned char new_mac[ETH_ALEN + 2])
739{
740 int err = 0;
741
742 if (priv->port_up) {
743 /* Remove old MAC and insert the new one */
744 err = mlx4_en_replace_mac(priv, priv->base_qpn,
745 new_mac, priv->current_mac);
746 if (err)
747 en_err(priv, "Failed changing HW MAC address\n");
748 } else
749 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
750
751 if (!err)
752 memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
753
754 return err;
755}
756
757static int mlx4_en_set_mac(struct net_device *dev, void *addr)
758{
759 struct mlx4_en_priv *priv = netdev_priv(dev);
760 struct mlx4_en_dev *mdev = priv->mdev;
761 struct sockaddr *saddr = addr;
762 unsigned char new_mac[ETH_ALEN + 2];
763 int err;
764
765 if (!is_valid_ether_addr(saddr->sa_data))
766 return -EADDRNOTAVAIL;
767
768 mutex_lock(&mdev->state_lock);
769 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
770 err = mlx4_en_do_set_mac(priv, new_mac);
771 if (!err)
772 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
773 mutex_unlock(&mdev->state_lock);
774
775 return err;
776}
777
778static void mlx4_en_clear_list(struct net_device *dev)
779{
780 struct mlx4_en_priv *priv = netdev_priv(dev);
781 struct mlx4_en_mc_list *tmp, *mc_to_del;
782
783 list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
784 list_del(&mc_to_del->list);
785 kfree(mc_to_del);
786 }
787}
788
789static void mlx4_en_cache_mclist(struct net_device *dev)
790{
791 struct mlx4_en_priv *priv = netdev_priv(dev);
792 struct netdev_hw_addr *ha;
793 struct mlx4_en_mc_list *tmp;
794
795 mlx4_en_clear_list(dev);
796 netdev_for_each_mc_addr(ha, dev) {
797 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
798 if (!tmp) {
799 mlx4_en_clear_list(dev);
800 return;
801 }
802 memcpy(tmp->addr, ha->addr, ETH_ALEN);
803 list_add_tail(&tmp->list, &priv->mc_list);
804 }
805}
806
807static void update_mclist_flags(struct mlx4_en_priv *priv,
808 struct list_head *dst,
809 struct list_head *src)
810{
811 struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
812 bool found;
813
814 /* Find all the entries that should be removed from dst,
815 * These are the entries that are not found in src
816 */
817 list_for_each_entry(dst_tmp, dst, list) {
818 found = false;
819 list_for_each_entry(src_tmp, src, list) {
820 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
821 found = true;
822 break;
823 }
824 }
825 if (!found)
826 dst_tmp->action = MCLIST_REM;
827 }
828
829 /* Add entries that exist in src but not in dst
830 * mark them as need to add
831 */
832 list_for_each_entry(src_tmp, src, list) {
833 found = false;
834 list_for_each_entry(dst_tmp, dst, list) {
835 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
836 dst_tmp->action = MCLIST_NONE;
837 found = true;
838 break;
839 }
840 }
841 if (!found) {
842 new_mc = kmemdup(src_tmp,
843 sizeof(struct mlx4_en_mc_list),
844 GFP_KERNEL);
845 if (!new_mc)
846 return;
847
848 new_mc->action = MCLIST_ADD;
849 list_add_tail(&new_mc->list, dst);
850 }
851 }
852}
853
854static void mlx4_en_set_rx_mode(struct net_device *dev)
855{
856 struct mlx4_en_priv *priv = netdev_priv(dev);
857
858 if (!priv->port_up)
859 return;
860
861 queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
862}
863
864static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
865 struct mlx4_en_dev *mdev)
866{
867 int err = 0;
868
869 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
870 if (netif_msg_rx_status(priv))
871 en_warn(priv, "Entering promiscuous mode\n");
872 priv->flags |= MLX4_EN_FLAG_PROMISC;
873
874 /* Enable promiscouos mode */
875 switch (mdev->dev->caps.steering_mode) {
876 case MLX4_STEERING_MODE_DEVICE_MANAGED:
877 err = mlx4_flow_steer_promisc_add(mdev->dev,
878 priv->port,
879 priv->base_qpn,
880 MLX4_FS_ALL_DEFAULT);
881 if (err)
882 en_err(priv, "Failed enabling promiscuous mode\n");
883 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
884 break;
885
886 case MLX4_STEERING_MODE_B0:
887 err = mlx4_unicast_promisc_add(mdev->dev,
888 priv->base_qpn,
889 priv->port);
890 if (err)
891 en_err(priv, "Failed enabling unicast promiscuous mode\n");
892
893 /* Add the default qp number as multicast
894 * promisc
895 */
896 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
897 err = mlx4_multicast_promisc_add(mdev->dev,
898 priv->base_qpn,
899 priv->port);
900 if (err)
901 en_err(priv, "Failed enabling multicast promiscuous mode\n");
902 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
903 }
904 break;
905
906 case MLX4_STEERING_MODE_A0:
907 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
908 priv->port,
909 priv->base_qpn,
910 1);
911 if (err)
912 en_err(priv, "Failed enabling promiscuous mode\n");
913 break;
914 }
915
916 /* Disable port multicast filter (unconditionally) */
917 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
918 0, MLX4_MCAST_DISABLE);
919 if (err)
920 en_err(priv, "Failed disabling multicast filter\n");
921 }
922}
923
924static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
925 struct mlx4_en_dev *mdev)
926{
927 int err = 0;
928
929 if (netif_msg_rx_status(priv))
930 en_warn(priv, "Leaving promiscuous mode\n");
931 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
932
933 /* Disable promiscouos mode */
934 switch (mdev->dev->caps.steering_mode) {
935 case MLX4_STEERING_MODE_DEVICE_MANAGED:
936 err = mlx4_flow_steer_promisc_remove(mdev->dev,
937 priv->port,
938 MLX4_FS_ALL_DEFAULT);
939 if (err)
940 en_err(priv, "Failed disabling promiscuous mode\n");
941 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
942 break;
943
944 case MLX4_STEERING_MODE_B0:
945 err = mlx4_unicast_promisc_remove(mdev->dev,
946 priv->base_qpn,
947 priv->port);
948 if (err)
949 en_err(priv, "Failed disabling unicast promiscuous mode\n");
950 /* Disable Multicast promisc */
951 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
952 err = mlx4_multicast_promisc_remove(mdev->dev,
953 priv->base_qpn,
954 priv->port);
955 if (err)
956 en_err(priv, "Failed disabling multicast promiscuous mode\n");
957 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
958 }
959 break;
960
961 case MLX4_STEERING_MODE_A0:
962 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
963 priv->port,
964 priv->base_qpn, 0);
965 if (err)
966 en_err(priv, "Failed disabling promiscuous mode\n");
967 break;
968 }
969}
970
971static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
972 struct net_device *dev,
973 struct mlx4_en_dev *mdev)
974{
975 struct mlx4_en_mc_list *mclist, *tmp;
976 u64 mcast_addr = 0;
977 u8 mc_list[16] = {0};
978 int err = 0;
979
980 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
981 if (dev->flags & IFF_ALLMULTI) {
982 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
983 0, MLX4_MCAST_DISABLE);
984 if (err)
985 en_err(priv, "Failed disabling multicast filter\n");
986
987 /* Add the default qp number as multicast promisc */
988 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
989 switch (mdev->dev->caps.steering_mode) {
990 case MLX4_STEERING_MODE_DEVICE_MANAGED:
991 err = mlx4_flow_steer_promisc_add(mdev->dev,
992 priv->port,
993 priv->base_qpn,
994 MLX4_FS_MC_DEFAULT);
995 break;
996
997 case MLX4_STEERING_MODE_B0:
998 err = mlx4_multicast_promisc_add(mdev->dev,
999 priv->base_qpn,
1000 priv->port);
1001 break;
1002
1003 case MLX4_STEERING_MODE_A0:
1004 break;
1005 }
1006 if (err)
1007 en_err(priv, "Failed entering multicast promisc mode\n");
1008 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
1009 }
1010 } else {
1011 /* Disable Multicast promisc */
1012 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1013 switch (mdev->dev->caps.steering_mode) {
1014 case MLX4_STEERING_MODE_DEVICE_MANAGED:
1015 err = mlx4_flow_steer_promisc_remove(mdev->dev,
1016 priv->port,
1017 MLX4_FS_MC_DEFAULT);
1018 break;
1019
1020 case MLX4_STEERING_MODE_B0:
1021 err = mlx4_multicast_promisc_remove(mdev->dev,
1022 priv->base_qpn,
1023 priv->port);
1024 break;
1025
1026 case MLX4_STEERING_MODE_A0:
1027 break;
1028 }
1029 if (err)
1030 en_err(priv, "Failed disabling multicast promiscuous mode\n");
1031 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1032 }
1033
1034 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1035 0, MLX4_MCAST_DISABLE);
1036 if (err)
1037 en_err(priv, "Failed disabling multicast filter\n");
1038
1039 /* Flush mcast filter and init it with broadcast address */
1040 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
1041 1, MLX4_MCAST_CONFIG);
1042
1043 /* Update multicast list - we cache all addresses so they won't
1044 * change while HW is updated holding the command semaphor */
1045 netif_addr_lock_bh(dev);
1046 mlx4_en_cache_mclist(dev);
1047 netif_addr_unlock_bh(dev);
1048 list_for_each_entry(mclist, &priv->mc_list, list) {
1049 mcast_addr = mlx4_mac_to_u64(mclist->addr);
1050 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
1051 mcast_addr, 0, MLX4_MCAST_CONFIG);
1052 }
1053 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1054 0, MLX4_MCAST_ENABLE);
1055 if (err)
1056 en_err(priv, "Failed enabling multicast filter\n");
1057
1058 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
1059 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1060 if (mclist->action == MCLIST_REM) {
1061 /* detach this address and delete from list */
1062 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1063 mc_list[5] = priv->port;
1064 err = mlx4_multicast_detach(mdev->dev,
1065 &priv->rss_map.indir_qp,
1066 mc_list,
1067 MLX4_PROT_ETH,
1068 mclist->reg_id);
1069 if (err)
1070 en_err(priv, "Fail to detach multicast address\n");
1071
1072 if (mclist->tunnel_reg_id) {
1073 err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1074 if (err)
1075 en_err(priv, "Failed to detach multicast address\n");
1076 }
1077
1078 /* remove from list */
1079 list_del(&mclist->list);
1080 kfree(mclist);
1081 } else if (mclist->action == MCLIST_ADD) {
1082 /* attach the address */
1083 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1084 /* needed for B0 steering support */
1085 mc_list[5] = priv->port;
1086 err = mlx4_multicast_attach(mdev->dev,
1087 &priv->rss_map.indir_qp,
1088 mc_list,
1089 priv->port, 0,
1090 MLX4_PROT_ETH,
1091 &mclist->reg_id);
1092 if (err)
1093 en_err(priv, "Fail to attach multicast address\n");
1094
1095 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1096 &mclist->tunnel_reg_id);
1097 if (err)
1098 en_err(priv, "Failed to attach multicast address\n");
1099 }
1100 }
1101 }
1102}
1103
1104static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1105 struct net_device *dev,
1106 struct mlx4_en_dev *mdev)
1107{
1108 struct netdev_hw_addr *ha;
1109 struct mlx4_mac_entry *entry;
1110 struct hlist_node *tmp;
1111 bool found;
1112 u64 mac;
1113 int err = 0;
1114 struct hlist_head *bucket;
1115 unsigned int i;
1116 int removed = 0;
1117 u32 prev_flags;
1118
1119 /* Note that we do not need to protect our mac_hash traversal with rcu,
1120 * since all modification code is protected by mdev->state_lock
1121 */
1122
1123 /* find what to remove */
1124 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1125 bucket = &priv->mac_hash[i];
1126 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1127 found = false;
1128 netdev_for_each_uc_addr(ha, dev) {
1129 if (ether_addr_equal_64bits(entry->mac,
1130 ha->addr)) {
1131 found = true;
1132 break;
1133 }
1134 }
1135
1136 /* MAC address of the port is not in uc list */
1137 if (ether_addr_equal_64bits(entry->mac,
1138 priv->current_mac))
1139 found = true;
1140
1141 if (!found) {
1142 mac = mlx4_mac_to_u64(entry->mac);
1143 mlx4_en_uc_steer_release(priv, entry->mac,
1144 priv->base_qpn,
1145 entry->reg_id);
1146 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1147
1148 hlist_del_rcu(&entry->hlist);
1149 kfree_rcu(entry, rcu);
1150 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1151 entry->mac, priv->port);
1152 ++removed;
1153 }
1154 }
1155 }
1156
1157 /* if we didn't remove anything, there is no use in trying to add
1158 * again once we are in a forced promisc mode state
1159 */
1160 if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1161 return;
1162
1163 prev_flags = priv->flags;
1164 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1165
1166 /* find what to add */
1167 netdev_for_each_uc_addr(ha, dev) {
1168 found = false;
1169 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1170 hlist_for_each_entry(entry, bucket, hlist) {
1171 if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1172 found = true;
1173 break;
1174 }
1175 }
1176
1177 if (!found) {
1178 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1179 if (!entry) {
1180 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1181 ha->addr, priv->port);
1182 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1183 break;
1184 }
1185 mac = mlx4_mac_to_u64(ha->addr);
1186 memcpy(entry->mac, ha->addr, ETH_ALEN);
1187 err = mlx4_register_mac(mdev->dev, priv->port, mac);
1188 if (err < 0) {
1189 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1190 ha->addr, priv->port, err);
1191 kfree(entry);
1192 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1193 break;
1194 }
1195 err = mlx4_en_uc_steer_add(priv, ha->addr,
1196 &priv->base_qpn,
1197 &entry->reg_id);
1198 if (err) {
1199 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1200 ha->addr, priv->port, err);
1201 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1202 kfree(entry);
1203 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1204 break;
1205 } else {
1206 unsigned int mac_hash;
1207 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1208 ha->addr, priv->port);
1209 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1210 bucket = &priv->mac_hash[mac_hash];
1211 hlist_add_head_rcu(&entry->hlist, bucket);
1212 }
1213 }
1214 }
1215
1216 if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1217 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1218 priv->port);
1219 } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1220 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1221 priv->port);
1222 }
1223}
1224
1225static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1226{
1227 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1228 rx_mode_task);
1229 struct mlx4_en_dev *mdev = priv->mdev;
1230 struct net_device *dev = priv->dev;
1231
1232 mutex_lock(&mdev->state_lock);
1233 if (!mdev->device_up) {
1234 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1235 goto out;
1236 }
1237 if (!priv->port_up) {
1238 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1239 goto out;
1240 }
1241
1242 if (!netif_carrier_ok(dev)) {
1243 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1244 if (priv->port_state.link_state) {
1245 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1246 netif_carrier_on(dev);
1247 en_dbg(LINK, priv, "Link Up\n");
1248 }
1249 }
1250 }
1251
1252 if (dev->priv_flags & IFF_UNICAST_FLT)
1253 mlx4_en_do_uc_filter(priv, dev, mdev);
1254
1255 /* Promsicuous mode: disable all filters */
1256 if ((dev->flags & IFF_PROMISC) ||
1257 (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1258 mlx4_en_set_promisc_mode(priv, mdev);
1259 goto out;
1260 }
1261
1262 /* Not in promiscuous mode */
1263 if (priv->flags & MLX4_EN_FLAG_PROMISC)
1264 mlx4_en_clear_promisc_mode(priv, mdev);
1265
1266 mlx4_en_do_multicast(priv, dev, mdev);
1267out:
1268 mutex_unlock(&mdev->state_lock);
1269}
1270
1271#ifdef CONFIG_NET_POLL_CONTROLLER
1272static void mlx4_en_netpoll(struct net_device *dev)
1273{
1274 struct mlx4_en_priv *priv = netdev_priv(dev);
1275 struct mlx4_en_cq *cq;
1276 int i;
1277
1278 for (i = 0; i < priv->rx_ring_num; i++) {
1279 cq = priv->rx_cq[i];
1280 napi_schedule(&cq->napi);
1281 }
1282}
1283#endif
1284
1285static void mlx4_en_tx_timeout(struct net_device *dev)
1286{
1287 struct mlx4_en_priv *priv = netdev_priv(dev);
1288 struct mlx4_en_dev *mdev = priv->mdev;
1289 int i;
1290
1291 if (netif_msg_timer(priv))
1292 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1293
1294 for (i = 0; i < priv->tx_ring_num; i++) {
1295 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1296 continue;
1297 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1298 i, priv->tx_ring[i]->qpn, priv->tx_ring[i]->cqn,
1299 priv->tx_ring[i]->cons, priv->tx_ring[i]->prod);
1300 }
1301
1302 priv->port_stats.tx_timeout++;
1303 en_dbg(DRV, priv, "Scheduling watchdog\n");
1304 queue_work(mdev->workqueue, &priv->watchdog_task);
1305}
1306
1307
1308static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
1309{
1310 struct mlx4_en_priv *priv = netdev_priv(dev);
1311
1312 spin_lock_bh(&priv->stats_lock);
1313 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
1314 spin_unlock_bh(&priv->stats_lock);
1315
1316 return &priv->ret_stats;
1317}
1318
1319static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1320{
1321 struct mlx4_en_cq *cq;
1322 int i;
1323
1324 /* If we haven't received a specific coalescing setting
1325 * (module param), we set the moderation parameters as follows:
1326 * - moder_cnt is set to the number of mtu sized packets to
1327 * satisfy our coalescing target.
1328 * - moder_time is set to a fixed value.
1329 */
1330 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1331 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1332 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1333 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1334 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1335 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1336
1337 /* Setup cq moderation params */
1338 for (i = 0; i < priv->rx_ring_num; i++) {
1339 cq = priv->rx_cq[i];
1340 cq->moder_cnt = priv->rx_frames;
1341 cq->moder_time = priv->rx_usecs;
1342 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1343 priv->last_moder_packets[i] = 0;
1344 priv->last_moder_bytes[i] = 0;
1345 }
1346
1347 for (i = 0; i < priv->tx_ring_num; i++) {
1348 cq = priv->tx_cq[i];
1349 cq->moder_cnt = priv->tx_frames;
1350 cq->moder_time = priv->tx_usecs;
1351 }
1352
1353 /* Reset auto-moderation params */
1354 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1355 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1356 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1357 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1358 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1359 priv->adaptive_rx_coal = 1;
1360 priv->last_moder_jiffies = 0;
1361 priv->last_moder_tx_packets = 0;
1362}
1363
1364static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1365{
1366 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1367 struct mlx4_en_cq *cq;
1368 unsigned long packets;
1369 unsigned long rate;
1370 unsigned long avg_pkt_size;
1371 unsigned long rx_packets;
1372 unsigned long rx_bytes;
1373 unsigned long rx_pkt_diff;
1374 int moder_time;
1375 int ring, err;
1376
1377 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1378 return;
1379
1380 for (ring = 0; ring < priv->rx_ring_num; ring++) {
1381 spin_lock_bh(&priv->stats_lock);
1382 rx_packets = priv->rx_ring[ring]->packets;
1383 rx_bytes = priv->rx_ring[ring]->bytes;
1384 spin_unlock_bh(&priv->stats_lock);
1385
1386 rx_pkt_diff = ((unsigned long) (rx_packets -
1387 priv->last_moder_packets[ring]));
1388 packets = rx_pkt_diff;
1389 rate = packets * HZ / period;
1390 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1391 priv->last_moder_bytes[ring])) / packets : 0;
1392
1393 /* Apply auto-moderation only when packet rate
1394 * exceeds a rate that it matters */
1395 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1396 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1397 if (rate < priv->pkt_rate_low)
1398 moder_time = priv->rx_usecs_low;
1399 else if (rate > priv->pkt_rate_high)
1400 moder_time = priv->rx_usecs_high;
1401 else
1402 moder_time = (rate - priv->pkt_rate_low) *
1403 (priv->rx_usecs_high - priv->rx_usecs_low) /
1404 (priv->pkt_rate_high - priv->pkt_rate_low) +
1405 priv->rx_usecs_low;
1406 } else {
1407 moder_time = priv->rx_usecs_low;
1408 }
1409
1410 if (moder_time != priv->last_moder_time[ring]) {
1411 priv->last_moder_time[ring] = moder_time;
1412 cq = priv->rx_cq[ring];
1413 cq->moder_time = moder_time;
1414 cq->moder_cnt = priv->rx_frames;
1415 err = mlx4_en_set_cq_moder(priv, cq);
1416 if (err)
1417 en_err(priv, "Failed modifying moderation for cq:%d\n",
1418 ring);
1419 }
1420 priv->last_moder_packets[ring] = rx_packets;
1421 priv->last_moder_bytes[ring] = rx_bytes;
1422 }
1423
1424 priv->last_moder_jiffies = jiffies;
1425}
1426
1427static void mlx4_en_do_get_stats(struct work_struct *work)
1428{
1429 struct delayed_work *delay = to_delayed_work(work);
1430 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1431 stats_task);
1432 struct mlx4_en_dev *mdev = priv->mdev;
1433 int err;
1434
1435 mutex_lock(&mdev->state_lock);
1436 if (mdev->device_up) {
1437 if (priv->port_up) {
1438 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1439 if (err)
1440 en_dbg(HW, priv, "Could not update stats\n");
1441
1442 mlx4_en_auto_moderation(priv);
1443 }
1444
1445 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1446 }
1447 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1448 mlx4_en_do_set_mac(priv, priv->current_mac);
1449 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1450 }
1451 mutex_unlock(&mdev->state_lock);
1452}
1453
1454/* mlx4_en_service_task - Run service task for tasks that needed to be done
1455 * periodically
1456 */
1457static void mlx4_en_service_task(struct work_struct *work)
1458{
1459 struct delayed_work *delay = to_delayed_work(work);
1460 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1461 service_task);
1462 struct mlx4_en_dev *mdev = priv->mdev;
1463
1464 mutex_lock(&mdev->state_lock);
1465 if (mdev->device_up) {
1466 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1467 mlx4_en_ptp_overflow_check(mdev);
1468
1469 queue_delayed_work(mdev->workqueue, &priv->service_task,
1470 SERVICE_TASK_DELAY);
1471 }
1472 mutex_unlock(&mdev->state_lock);
1473}
1474
1475static void mlx4_en_linkstate(struct work_struct *work)
1476{
1477 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1478 linkstate_task);
1479 struct mlx4_en_dev *mdev = priv->mdev;
1480 int linkstate = priv->link_state;
1481
1482 mutex_lock(&mdev->state_lock);
1483 /* If observable port state changed set carrier state and
1484 * report to system log */
1485 if (priv->last_link_state != linkstate) {
1486 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1487 en_info(priv, "Link Down\n");
1488 netif_carrier_off(priv->dev);
1489 } else {
1490 en_info(priv, "Link Up\n");
1491 netif_carrier_on(priv->dev);
1492 }
1493 }
1494 priv->last_link_state = linkstate;
1495 mutex_unlock(&mdev->state_lock);
1496}
1497
1498static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1499{
1500 struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
1501 int numa_node = priv->mdev->dev->numa_node;
1502 int ret = 0;
1503
1504 if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
1505 return -ENOMEM;
1506
1507 ret = cpumask_set_cpu_local_first(ring_idx, numa_node,
1508 ring->affinity_mask);
1509 if (ret)
1510 free_cpumask_var(ring->affinity_mask);
1511
1512 return ret;
1513}
1514
1515static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1516{
1517 free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
1518}
1519
1520int mlx4_en_start_port(struct net_device *dev)
1521{
1522 struct mlx4_en_priv *priv = netdev_priv(dev);
1523 struct mlx4_en_dev *mdev = priv->mdev;
1524 struct mlx4_en_cq *cq;
1525 struct mlx4_en_tx_ring *tx_ring;
1526 int rx_index = 0;
1527 int tx_index = 0;
1528 int err = 0;
1529 int i;
1530 int j;
1531 u8 mc_list[16] = {0};
1532
1533 if (priv->port_up) {
1534 en_dbg(DRV, priv, "start port called while port already up\n");
1535 return 0;
1536 }
1537
1538 INIT_LIST_HEAD(&priv->mc_list);
1539 INIT_LIST_HEAD(&priv->curr_list);
1540 INIT_LIST_HEAD(&priv->ethtool_list);
1541 memset(&priv->ethtool_rules[0], 0,
1542 sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1543
1544 /* Calculate Rx buf size */
1545 dev->mtu = min(dev->mtu, priv->max_mtu);
1546 mlx4_en_calc_rx_buf(dev);
1547 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1548
1549 /* Configure rx cq's and rings */
1550 err = mlx4_en_activate_rx_rings(priv);
1551 if (err) {
1552 en_err(priv, "Failed to activate RX rings\n");
1553 return err;
1554 }
1555 for (i = 0; i < priv->rx_ring_num; i++) {
1556 cq = priv->rx_cq[i];
1557
1558 mlx4_en_cq_init_lock(cq);
1559
1560 err = mlx4_en_init_affinity_hint(priv, i);
1561 if (err) {
1562 en_err(priv, "Failed preparing IRQ affinity hint\n");
1563 goto cq_err;
1564 }
1565
1566 err = mlx4_en_activate_cq(priv, cq, i);
1567 if (err) {
1568 en_err(priv, "Failed activating Rx CQ\n");
1569 mlx4_en_free_affinity_hint(priv, i);
1570 goto cq_err;
1571 }
1572 for (j = 0; j < cq->size; j++)
1573 cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1574 err = mlx4_en_set_cq_moder(priv, cq);
1575 if (err) {
1576 en_err(priv, "Failed setting cq moderation parameters\n");
1577 mlx4_en_deactivate_cq(priv, cq);
1578 mlx4_en_free_affinity_hint(priv, i);
1579 goto cq_err;
1580 }
1581 mlx4_en_arm_cq(priv, cq);
1582 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1583 ++rx_index;
1584 }
1585
1586 /* Set qp number */
1587 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1588 err = mlx4_en_get_qp(priv);
1589 if (err) {
1590 en_err(priv, "Failed getting eth qp\n");
1591 goto cq_err;
1592 }
1593 mdev->mac_removed[priv->port] = 0;
1594
1595 err = mlx4_en_config_rss_steer(priv);
1596 if (err) {
1597 en_err(priv, "Failed configuring rss steering\n");
1598 goto mac_err;
1599 }
1600
1601 err = mlx4_en_create_drop_qp(priv);
1602 if (err)
1603 goto rss_err;
1604
1605 /* Configure tx cq's and rings */
1606 for (i = 0; i < priv->tx_ring_num; i++) {
1607 /* Configure cq */
1608 cq = priv->tx_cq[i];
1609 err = mlx4_en_activate_cq(priv, cq, i);
1610 if (err) {
1611 en_err(priv, "Failed allocating Tx CQ\n");
1612 goto tx_err;
1613 }
1614 err = mlx4_en_set_cq_moder(priv, cq);
1615 if (err) {
1616 en_err(priv, "Failed setting cq moderation parameters\n");
1617 mlx4_en_deactivate_cq(priv, cq);
1618 goto tx_err;
1619 }
1620 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
1621 cq->buf->wqe_index = cpu_to_be16(0xffff);
1622
1623 /* Configure ring */
1624 tx_ring = priv->tx_ring[i];
1625 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
1626 i / priv->num_tx_rings_p_up);
1627 if (err) {
1628 en_err(priv, "Failed allocating Tx ring\n");
1629 mlx4_en_deactivate_cq(priv, cq);
1630 goto tx_err;
1631 }
1632 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1633
1634 /* Arm CQ for TX completions */
1635 mlx4_en_arm_cq(priv, cq);
1636
1637 /* Set initial ownership of all Tx TXBBs to SW (1) */
1638 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1639 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
1640 ++tx_index;
1641 }
1642
1643 /* Configure port */
1644 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1645 priv->rx_skb_size + ETH_FCS_LEN,
1646 priv->prof->tx_pause,
1647 priv->prof->tx_ppp,
1648 priv->prof->rx_pause,
1649 priv->prof->rx_ppp);
1650 if (err) {
1651 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1652 priv->port, err);
1653 goto tx_err;
1654 }
1655 /* Set default qp number */
1656 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1657 if (err) {
1658 en_err(priv, "Failed setting default qp numbers\n");
1659 goto tx_err;
1660 }
1661
1662 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1663 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
1664 if (err) {
1665 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1666 err);
1667 goto tx_err;
1668 }
1669 }
1670
1671 /* Init port */
1672 en_dbg(HW, priv, "Initializing port\n");
1673 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1674 if (err) {
1675 en_err(priv, "Failed Initializing port\n");
1676 goto tx_err;
1677 }
1678
1679 /* Attach rx QP to bradcast address */
1680 memset(&mc_list[10], 0xff, ETH_ALEN);
1681 mc_list[5] = priv->port; /* needed for B0 steering support */
1682 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1683 priv->port, 0, MLX4_PROT_ETH,
1684 &priv->broadcast_id))
1685 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1686
1687 /* Must redo promiscuous mode setup. */
1688 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1689
1690 /* Schedule multicast task to populate multicast list */
1691 queue_work(mdev->workqueue, &priv->rx_mode_task);
1692
1693 mlx4_set_stats_bitmap(mdev->dev, &priv->stats_bitmap);
1694
1695#ifdef CONFIG_MLX4_EN_VXLAN
1696 if (priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VXLAN_OFFLOADS)
1697 vxlan_get_rx_port(dev);
1698#endif
1699 priv->port_up = true;
1700 netif_tx_start_all_queues(dev);
1701 netif_device_attach(dev);
1702
1703 return 0;
1704
1705tx_err:
1706 while (tx_index--) {
1707 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
1708 mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
1709 }
1710 mlx4_en_destroy_drop_qp(priv);
1711rss_err:
1712 mlx4_en_release_rss_steer(priv);
1713mac_err:
1714 mlx4_en_put_qp(priv);
1715cq_err:
1716 while (rx_index--) {
1717 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1718 mlx4_en_free_affinity_hint(priv, i);
1719 }
1720 for (i = 0; i < priv->rx_ring_num; i++)
1721 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1722
1723 return err; /* need to close devices */
1724}
1725
1726
1727void mlx4_en_stop_port(struct net_device *dev, int detach)
1728{
1729 struct mlx4_en_priv *priv = netdev_priv(dev);
1730 struct mlx4_en_dev *mdev = priv->mdev;
1731 struct mlx4_en_mc_list *mclist, *tmp;
1732 struct ethtool_flow_id *flow, *tmp_flow;
1733 int i;
1734 u8 mc_list[16] = {0};
1735
1736 if (!priv->port_up) {
1737 en_dbg(DRV, priv, "stop port called while port already down\n");
1738 return;
1739 }
1740
1741 /* close port*/
1742 mlx4_CLOSE_PORT(mdev->dev, priv->port);
1743
1744 /* Synchronize with tx routine */
1745 netif_tx_lock_bh(dev);
1746 if (detach)
1747 netif_device_detach(dev);
1748 netif_tx_stop_all_queues(dev);
1749 netif_tx_unlock_bh(dev);
1750
1751 netif_tx_disable(dev);
1752
1753 /* Set port as not active */
1754 priv->port_up = false;
1755
1756 /* Promsicuous mode */
1757 if (mdev->dev->caps.steering_mode ==
1758 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1759 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1760 MLX4_EN_FLAG_MC_PROMISC);
1761 mlx4_flow_steer_promisc_remove(mdev->dev,
1762 priv->port,
1763 MLX4_FS_ALL_DEFAULT);
1764 mlx4_flow_steer_promisc_remove(mdev->dev,
1765 priv->port,
1766 MLX4_FS_MC_DEFAULT);
1767 } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1768 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1769
1770 /* Disable promiscouos mode */
1771 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1772 priv->port);
1773
1774 /* Disable Multicast promisc */
1775 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1776 mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1777 priv->port);
1778 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1779 }
1780 }
1781
1782 /* Detach All multicasts */
1783 memset(&mc_list[10], 0xff, ETH_ALEN);
1784 mc_list[5] = priv->port; /* needed for B0 steering support */
1785 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1786 MLX4_PROT_ETH, priv->broadcast_id);
1787 list_for_each_entry(mclist, &priv->curr_list, list) {
1788 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1789 mc_list[5] = priv->port;
1790 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1791 mc_list, MLX4_PROT_ETH, mclist->reg_id);
1792 if (mclist->tunnel_reg_id)
1793 mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1794 }
1795 mlx4_en_clear_list(dev);
1796 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1797 list_del(&mclist->list);
1798 kfree(mclist);
1799 }
1800
1801 /* Flush multicast filter */
1802 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1803
1804 /* Remove flow steering rules for the port*/
1805 if (mdev->dev->caps.steering_mode ==
1806 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1807 ASSERT_RTNL();
1808 list_for_each_entry_safe(flow, tmp_flow,
1809 &priv->ethtool_list, list) {
1810 mlx4_flow_detach(mdev->dev, flow->id);
1811 list_del(&flow->list);
1812 }
1813 }
1814
1815 mlx4_en_destroy_drop_qp(priv);
1816
1817 /* Free TX Rings */
1818 for (i = 0; i < priv->tx_ring_num; i++) {
1819 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
1820 mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
1821 }
1822 msleep(10);
1823
1824 for (i = 0; i < priv->tx_ring_num; i++)
1825 mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
1826
1827 /* Free RSS qps */
1828 mlx4_en_release_rss_steer(priv);
1829
1830 /* Unregister Mac address for the port */
1831 mlx4_en_put_qp(priv);
1832 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1833 mdev->mac_removed[priv->port] = 1;
1834
1835 /* Free RX Rings */
1836 for (i = 0; i < priv->rx_ring_num; i++) {
1837 struct mlx4_en_cq *cq = priv->rx_cq[i];
1838
1839 local_bh_disable();
1840 while (!mlx4_en_cq_lock_napi(cq)) {
1841 pr_info("CQ %d locked\n", i);
1842 mdelay(1);
1843 }
1844 local_bh_enable();
1845
1846 napi_synchronize(&cq->napi);
1847 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1848 mlx4_en_deactivate_cq(priv, cq);
1849
1850 mlx4_en_free_affinity_hint(priv, i);
1851 }
1852}
1853
1854static void mlx4_en_restart(struct work_struct *work)
1855{
1856 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1857 watchdog_task);
1858 struct mlx4_en_dev *mdev = priv->mdev;
1859 struct net_device *dev = priv->dev;
1860
1861 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1862
1863 mutex_lock(&mdev->state_lock);
1864 if (priv->port_up) {
1865 mlx4_en_stop_port(dev, 1);
1866 if (mlx4_en_start_port(dev))
1867 en_err(priv, "Failed restarting port %d\n", priv->port);
1868 }
1869 mutex_unlock(&mdev->state_lock);
1870}
1871
1872static void mlx4_en_clear_stats(struct net_device *dev)
1873{
1874 struct mlx4_en_priv *priv = netdev_priv(dev);
1875 struct mlx4_en_dev *mdev = priv->mdev;
1876 int i;
1877
1878 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1879 en_dbg(HW, priv, "Failed dumping statistics\n");
1880
1881 memset(&priv->stats, 0, sizeof(priv->stats));
1882 memset(&priv->pstats, 0, sizeof(priv->pstats));
1883 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1884 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1885
1886 for (i = 0; i < priv->tx_ring_num; i++) {
1887 priv->tx_ring[i]->bytes = 0;
1888 priv->tx_ring[i]->packets = 0;
1889 priv->tx_ring[i]->tx_csum = 0;
1890 }
1891 for (i = 0; i < priv->rx_ring_num; i++) {
1892 priv->rx_ring[i]->bytes = 0;
1893 priv->rx_ring[i]->packets = 0;
1894 priv->rx_ring[i]->csum_ok = 0;
1895 priv->rx_ring[i]->csum_none = 0;
1896 }
1897}
1898
1899static int mlx4_en_open(struct net_device *dev)
1900{
1901 struct mlx4_en_priv *priv = netdev_priv(dev);
1902 struct mlx4_en_dev *mdev = priv->mdev;
1903 int err = 0;
1904
1905 mutex_lock(&mdev->state_lock);
1906
1907 if (!mdev->device_up) {
1908 en_err(priv, "Cannot open - device down/disabled\n");
1909 err = -EBUSY;
1910 goto out;
1911 }
1912
1913 /* Reset HW statistics and SW counters */
1914 mlx4_en_clear_stats(dev);
1915
1916 err = mlx4_en_start_port(dev);
1917 if (err)
1918 en_err(priv, "Failed starting port:%d\n", priv->port);
1919
1920out:
1921 mutex_unlock(&mdev->state_lock);
1922 return err;
1923}
1924
1925
1926static int mlx4_en_close(struct net_device *dev)
1927{
1928 struct mlx4_en_priv *priv = netdev_priv(dev);
1929 struct mlx4_en_dev *mdev = priv->mdev;
1930
1931 en_dbg(IFDOWN, priv, "Close port called\n");
1932
1933 mutex_lock(&mdev->state_lock);
1934
1935 mlx4_en_stop_port(dev, 0);
1936 netif_carrier_off(dev);
1937
1938 mutex_unlock(&mdev->state_lock);
1939 return 0;
1940}
1941
1942void mlx4_en_free_resources(struct mlx4_en_priv *priv)
1943{
1944 int i;
1945
1946#ifdef CONFIG_RFS_ACCEL
1947 free_irq_cpu_rmap(priv->dev->rx_cpu_rmap);
1948 priv->dev->rx_cpu_rmap = NULL;
1949#endif
1950
1951 for (i = 0; i < priv->tx_ring_num; i++) {
1952 if (priv->tx_ring && priv->tx_ring[i])
1953 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
1954 if (priv->tx_cq && priv->tx_cq[i])
1955 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
1956 }
1957
1958 for (i = 0; i < priv->rx_ring_num; i++) {
1959 if (priv->rx_ring[i])
1960 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
1961 priv->prof->rx_ring_size, priv->stride);
1962 if (priv->rx_cq[i])
1963 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
1964 }
1965
1966 if (priv->base_tx_qpn) {
1967 mlx4_qp_release_range(priv->mdev->dev, priv->base_tx_qpn, priv->tx_ring_num);
1968 priv->base_tx_qpn = 0;
1969 }
1970}
1971
1972int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
1973{
1974 struct mlx4_en_port_profile *prof = priv->prof;
1975 int i;
1976 int err;
1977 int node;
1978
1979 err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &priv->base_tx_qpn);
1980 if (err) {
1981 en_err(priv, "failed reserving range for TX rings\n");
1982 return err;
1983 }
1984
1985 /* Create tx Rings */
1986 for (i = 0; i < priv->tx_ring_num; i++) {
1987 node = cpu_to_node(i % num_online_cpus());
1988 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
1989 prof->tx_ring_size, i, TX, node))
1990 goto err;
1991
1992 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
1993 priv->base_tx_qpn + i,
1994 prof->tx_ring_size, TXBB_SIZE,
1995 node, i))
1996 goto err;
1997 }
1998
1999 /* Create rx Rings */
2000 for (i = 0; i < priv->rx_ring_num; i++) {
2001 node = cpu_to_node(i % num_online_cpus());
2002 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2003 prof->rx_ring_size, i, RX, node))
2004 goto err;
2005
2006 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2007 prof->rx_ring_size, priv->stride,
2008 node))
2009 goto err;
2010 }
2011
2012#ifdef CONFIG_RFS_ACCEL
2013 if (priv->mdev->dev->caps.comp_pool) {
2014 priv->dev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->mdev->dev->caps.comp_pool);
2015 if (!priv->dev->rx_cpu_rmap)
2016 goto err;
2017 }
2018#endif
2019
2020 return 0;
2021
2022err:
2023 en_err(priv, "Failed to allocate NIC resources\n");
2024 for (i = 0; i < priv->rx_ring_num; i++) {
2025 if (priv->rx_ring[i])
2026 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2027 prof->rx_ring_size,
2028 priv->stride);
2029 if (priv->rx_cq[i])
2030 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2031 }
2032 for (i = 0; i < priv->tx_ring_num; i++) {
2033 if (priv->tx_ring[i])
2034 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2035 if (priv->tx_cq[i])
2036 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2037 }
2038 return -ENOMEM;
2039}
2040
2041
2042void mlx4_en_destroy_netdev(struct net_device *dev)
2043{
2044 struct mlx4_en_priv *priv = netdev_priv(dev);
2045 struct mlx4_en_dev *mdev = priv->mdev;
2046
2047 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2048
2049 /* Unregister device - this will close the port if it was up */
2050 if (priv->registered)
2051 unregister_netdev(dev);
2052
2053 if (priv->allocated)
2054 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2055
2056 cancel_delayed_work(&priv->stats_task);
2057 cancel_delayed_work(&priv->service_task);
2058 /* flush any pending task for this netdev */
2059 flush_workqueue(mdev->workqueue);
2060
2061 /* Detach the netdev so tasks would not attempt to access it */
2062 mutex_lock(&mdev->state_lock);
2063 mdev->pndev[priv->port] = NULL;
2064 mutex_unlock(&mdev->state_lock);
2065
2066 mlx4_en_free_resources(priv);
2067
2068 kfree(priv->tx_ring);
2069 kfree(priv->tx_cq);
2070
2071 free_netdev(dev);
2072}
2073
2074static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2075{
2076 struct mlx4_en_priv *priv = netdev_priv(dev);
2077 struct mlx4_en_dev *mdev = priv->mdev;
2078 int err = 0;
2079
2080 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2081 dev->mtu, new_mtu);
2082
2083 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
2084 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
2085 return -EPERM;
2086 }
2087 dev->mtu = new_mtu;
2088
2089 if (netif_running(dev)) {
2090 mutex_lock(&mdev->state_lock);
2091 if (!mdev->device_up) {
2092 /* NIC is probably restarting - let watchdog task reset
2093 * the port */
2094 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2095 } else {
2096 mlx4_en_stop_port(dev, 1);
2097 err = mlx4_en_start_port(dev);
2098 if (err) {
2099 en_err(priv, "Failed restarting port:%d\n",
2100 priv->port);
2101 queue_work(mdev->workqueue, &priv->watchdog_task);
2102 }
2103 }
2104 mutex_unlock(&mdev->state_lock);
2105 }
2106 return 0;
2107}
2108
2109static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2110{
2111 struct mlx4_en_priv *priv = netdev_priv(dev);
2112 struct mlx4_en_dev *mdev = priv->mdev;
2113 struct hwtstamp_config config;
2114
2115 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2116 return -EFAULT;
2117
2118 /* reserved for future extensions */
2119 if (config.flags)
2120 return -EINVAL;
2121
2122 /* device doesn't support time stamping */
2123 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2124 return -EINVAL;
2125
2126 /* TX HW timestamp */
2127 switch (config.tx_type) {
2128 case HWTSTAMP_TX_OFF:
2129 case HWTSTAMP_TX_ON:
2130 break;
2131 default:
2132 return -ERANGE;
2133 }
2134
2135 /* RX HW timestamp */
2136 switch (config.rx_filter) {
2137 case HWTSTAMP_FILTER_NONE:
2138 break;
2139 case HWTSTAMP_FILTER_ALL:
2140 case HWTSTAMP_FILTER_SOME:
2141 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2142 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2143 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2144 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2145 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2146 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2147 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2148 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2149 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2150 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2151 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2152 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2153 config.rx_filter = HWTSTAMP_FILTER_ALL;
2154 break;
2155 default:
2156 return -ERANGE;
2157 }
2158
2159 if (mlx4_en_reset_config(dev, config, dev->features)) {
2160 config.tx_type = HWTSTAMP_TX_OFF;
2161 config.rx_filter = HWTSTAMP_FILTER_NONE;
2162 }
2163
2164 return copy_to_user(ifr->ifr_data, &config,
2165 sizeof(config)) ? -EFAULT : 0;
2166}
2167
2168static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2169{
2170 struct mlx4_en_priv *priv = netdev_priv(dev);
2171
2172 return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2173 sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2174}
2175
2176static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2177{
2178 switch (cmd) {
2179 case SIOCSHWTSTAMP:
2180 return mlx4_en_hwtstamp_set(dev, ifr);
2181 case SIOCGHWTSTAMP:
2182 return mlx4_en_hwtstamp_get(dev, ifr);
2183 default:
2184 return -EOPNOTSUPP;
2185 }
2186}
2187
2188static int mlx4_en_set_features(struct net_device *netdev,
2189 netdev_features_t features)
2190{
2191 struct mlx4_en_priv *priv = netdev_priv(netdev);
2192 int ret = 0;
2193
2194 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2195 en_info(priv, "Turn %s RX vlan strip offload\n",
2196 (features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
2197 ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
2198 features);
2199 if (ret)
2200 return ret;
2201 }
2202
2203 if (features & NETIF_F_LOOPBACK)
2204 priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
2205 else
2206 priv->ctrl_flags &=
2207 cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
2208
2209 mlx4_en_update_loopback_state(netdev, features);
2210
2211 return 0;
2212
2213}
2214
2215static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2216{
2217 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2218 struct mlx4_en_dev *mdev = en_priv->mdev;
2219 u64 mac_u64 = mlx4_mac_to_u64(mac);
2220
2221 if (!is_valid_ether_addr(mac))
2222 return -EINVAL;
2223
2224 return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2225}
2226
2227static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2228{
2229 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2230 struct mlx4_en_dev *mdev = en_priv->mdev;
2231
2232 return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos);
2233}
2234
2235static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2236{
2237 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2238 struct mlx4_en_dev *mdev = en_priv->mdev;
2239
2240 return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2241}
2242
2243static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2244{
2245 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2246 struct mlx4_en_dev *mdev = en_priv->mdev;
2247
2248 return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2249}
2250
2251static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2252{
2253 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2254 struct mlx4_en_dev *mdev = en_priv->mdev;
2255
2256 return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2257}
2258
2259#define PORT_ID_BYTE_LEN 8
2260static int mlx4_en_get_phys_port_id(struct net_device *dev,
2261 struct netdev_phys_port_id *ppid)
2262{
2263 struct mlx4_en_priv *priv = netdev_priv(dev);
2264 struct mlx4_dev *mdev = priv->mdev->dev;
2265 int i;
2266 u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2267
2268 if (!phys_port_id)
2269 return -EOPNOTSUPP;
2270
2271 ppid->id_len = sizeof(phys_port_id);
2272 for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2273 ppid->id[i] = phys_port_id & 0xff;
2274 phys_port_id >>= 8;
2275 }
2276 return 0;
2277}
2278
2279#ifdef CONFIG_MLX4_EN_VXLAN
2280static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
2281{
2282 int ret;
2283 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2284 vxlan_add_task);
2285
2286 ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
2287 if (ret)
2288 goto out;
2289
2290 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2291 VXLAN_STEER_BY_OUTER_MAC, 1);
2292out:
2293 if (ret)
2294 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2295}
2296
2297static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
2298{
2299 int ret;
2300 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2301 vxlan_del_task);
2302
2303 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2304 VXLAN_STEER_BY_OUTER_MAC, 0);
2305 if (ret)
2306 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2307
2308 priv->vxlan_port = 0;
2309}
2310
2311static void mlx4_en_add_vxlan_port(struct net_device *dev,
2312 sa_family_t sa_family, __be16 port)
2313{
2314 struct mlx4_en_priv *priv = netdev_priv(dev);
2315 __be16 current_port;
2316
2317 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2318 return;
2319
2320 if (sa_family == AF_INET6)
2321 return;
2322
2323 current_port = priv->vxlan_port;
2324 if (current_port && current_port != port) {
2325 en_warn(priv, "vxlan port %d configured, can't add port %d\n",
2326 ntohs(current_port), ntohs(port));
2327 return;
2328 }
2329
2330 priv->vxlan_port = port;
2331 queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
2332}
2333
2334static void mlx4_en_del_vxlan_port(struct net_device *dev,
2335 sa_family_t sa_family, __be16 port)
2336{
2337 struct mlx4_en_priv *priv = netdev_priv(dev);
2338 __be16 current_port;
2339
2340 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2341 return;
2342
2343 if (sa_family == AF_INET6)
2344 return;
2345
2346 current_port = priv->vxlan_port;
2347 if (current_port != port) {
2348 en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
2349 return;
2350 }
2351
2352 queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
2353}
2354#endif
2355
2356static const struct net_device_ops mlx4_netdev_ops = {
2357 .ndo_open = mlx4_en_open,
2358 .ndo_stop = mlx4_en_close,
2359 .ndo_start_xmit = mlx4_en_xmit,
2360 .ndo_select_queue = mlx4_en_select_queue,
2361 .ndo_get_stats = mlx4_en_get_stats,
2362 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2363 .ndo_set_mac_address = mlx4_en_set_mac,
2364 .ndo_validate_addr = eth_validate_addr,
2365 .ndo_change_mtu = mlx4_en_change_mtu,
2366 .ndo_do_ioctl = mlx4_en_ioctl,
2367 .ndo_tx_timeout = mlx4_en_tx_timeout,
2368 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2369 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2370#ifdef CONFIG_NET_POLL_CONTROLLER
2371 .ndo_poll_controller = mlx4_en_netpoll,
2372#endif
2373 .ndo_set_features = mlx4_en_set_features,
2374 .ndo_setup_tc = mlx4_en_setup_tc,
2375#ifdef CONFIG_RFS_ACCEL
2376 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2377#endif
2378#ifdef CONFIG_NET_RX_BUSY_POLL
2379 .ndo_busy_poll = mlx4_en_low_latency_recv,
2380#endif
2381 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2382#ifdef CONFIG_MLX4_EN_VXLAN
2383 .ndo_add_vxlan_port = mlx4_en_add_vxlan_port,
2384 .ndo_del_vxlan_port = mlx4_en_del_vxlan_port,
2385#endif
2386};
2387
2388static const struct net_device_ops mlx4_netdev_ops_master = {
2389 .ndo_open = mlx4_en_open,
2390 .ndo_stop = mlx4_en_close,
2391 .ndo_start_xmit = mlx4_en_xmit,
2392 .ndo_select_queue = mlx4_en_select_queue,
2393 .ndo_get_stats = mlx4_en_get_stats,
2394 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2395 .ndo_set_mac_address = mlx4_en_set_mac,
2396 .ndo_validate_addr = eth_validate_addr,
2397 .ndo_change_mtu = mlx4_en_change_mtu,
2398 .ndo_tx_timeout = mlx4_en_tx_timeout,
2399 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2400 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2401 .ndo_set_vf_mac = mlx4_en_set_vf_mac,
2402 .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
2403 .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
2404 .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
2405 .ndo_get_vf_config = mlx4_en_get_vf_config,
2406#ifdef CONFIG_NET_POLL_CONTROLLER
2407 .ndo_poll_controller = mlx4_en_netpoll,
2408#endif
2409 .ndo_set_features = mlx4_en_set_features,
2410 .ndo_setup_tc = mlx4_en_setup_tc,
2411#ifdef CONFIG_RFS_ACCEL
2412 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2413#endif
2414 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2415};
2416
2417int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
2418 struct mlx4_en_port_profile *prof)
2419{
2420 struct net_device *dev;
2421 struct mlx4_en_priv *priv;
2422 int i;
2423 int err;
2424 u64 mac_u64;
2425
2426 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
2427 MAX_TX_RINGS, MAX_RX_RINGS);
2428 if (dev == NULL)
2429 return -ENOMEM;
2430
2431 netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
2432 netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
2433
2434 SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
2435 dev->dev_port = port - 1;
2436
2437 /*
2438 * Initialize driver private data
2439 */
2440
2441 priv = netdev_priv(dev);
2442 memset(priv, 0, sizeof(struct mlx4_en_priv));
2443 spin_lock_init(&priv->stats_lock);
2444 INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
2445 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
2446 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
2447 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
2448 INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
2449#ifdef CONFIG_MLX4_EN_VXLAN
2450 INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
2451 INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
2452#endif
2453#ifdef CONFIG_RFS_ACCEL
2454 INIT_LIST_HEAD(&priv->filters);
2455 spin_lock_init(&priv->filters_lock);
2456#endif
2457
2458 priv->dev = dev;
2459 priv->mdev = mdev;
2460 priv->ddev = &mdev->pdev->dev;
2461 priv->prof = prof;
2462 priv->port = port;
2463 priv->port_up = false;
2464 priv->flags = prof->flags;
2465 priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
2466 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
2467 MLX4_WQE_CTRL_SOLICITED);
2468 priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
2469 priv->tx_ring_num = prof->tx_ring_num;
2470 priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
2471
2472 priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
2473 GFP_KERNEL);
2474 if (!priv->tx_ring) {
2475 err = -ENOMEM;
2476 goto out;
2477 }
2478 priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
2479 GFP_KERNEL);
2480 if (!priv->tx_cq) {
2481 err = -ENOMEM;
2482 goto out;
2483 }
2484 priv->rx_ring_num = prof->rx_ring_num;
2485 priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
2486 priv->cqe_size = mdev->dev->caps.cqe_size;
2487 priv->mac_index = -1;
2488 priv->msg_enable = MLX4_EN_MSG_LEVEL;
2489#ifdef CONFIG_MLX4_EN_DCB
2490 if (!mlx4_is_slave(priv->mdev->dev)) {
2491 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_SET_ETH_SCHED) {
2492 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
2493 } else {
2494 en_info(priv, "enabling only PFC DCB ops\n");
2495 dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
2496 }
2497 }
2498#endif
2499
2500 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
2501 INIT_HLIST_HEAD(&priv->mac_hash[i]);
2502
2503 /* Query for default mac and max mtu */
2504 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
2505
2506 /* Set default MAC */
2507 dev->addr_len = ETH_ALEN;
2508 mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
2509 if (!is_valid_ether_addr(dev->dev_addr)) {
2510 if (mlx4_is_slave(priv->mdev->dev)) {
2511 eth_hw_addr_random(dev);
2512 en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
2513 mac_u64 = mlx4_mac_to_u64(dev->dev_addr);
2514 mdev->dev->caps.def_mac[priv->port] = mac_u64;
2515 } else {
2516 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
2517 priv->port, dev->dev_addr);
2518 err = -EINVAL;
2519 goto out;
2520 }
2521 }
2522
2523 memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
2524
2525 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2526 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2527 err = mlx4_en_alloc_resources(priv);
2528 if (err)
2529 goto out;
2530
2531 /* Initialize time stamping config */
2532 priv->hwtstamp_config.flags = 0;
2533 priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
2534 priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2535
2536 /* Allocate page for receive rings */
2537 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
2538 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
2539 if (err) {
2540 en_err(priv, "Failed to allocate page for rx qps\n");
2541 goto out;
2542 }
2543 priv->allocated = 1;
2544
2545 /*
2546 * Initialize netdev entry points
2547 */
2548 if (mlx4_is_master(priv->mdev->dev))
2549 dev->netdev_ops = &mlx4_netdev_ops_master;
2550 else
2551 dev->netdev_ops = &mlx4_netdev_ops;
2552 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
2553 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
2554 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
2555
2556 dev->ethtool_ops = &mlx4_en_ethtool_ops;
2557
2558 /*
2559 * Set driver features
2560 */
2561 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2562 if (mdev->LSO_support)
2563 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2564
2565 dev->vlan_features = dev->hw_features;
2566
2567 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
2568 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
2569 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
2570 NETIF_F_HW_VLAN_CTAG_FILTER;
2571 dev->hw_features |= NETIF_F_LOOPBACK |
2572 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2573
2574 if (mdev->dev->caps.steering_mode ==
2575 MLX4_STEERING_MODE_DEVICE_MANAGED)
2576 dev->hw_features |= NETIF_F_NTUPLE;
2577
2578 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
2579 dev->priv_flags |= IFF_UNICAST_FLT;
2580
2581 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
2582 dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
2583 NETIF_F_TSO | NETIF_F_GSO_UDP_TUNNEL;
2584 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
2585 dev->features |= NETIF_F_GSO_UDP_TUNNEL;
2586 }
2587
2588 mdev->pndev[port] = dev;
2589
2590 netif_carrier_off(dev);
2591 mlx4_en_set_default_moderation(priv);
2592
2593 err = register_netdev(dev);
2594 if (err) {
2595 en_err(priv, "Netdev registration failed for port %d\n", port);
2596 goto out;
2597 }
2598 priv->registered = 1;
2599
2600 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
2601 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
2602
2603 mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
2604
2605 /* Configure port */
2606 mlx4_en_calc_rx_buf(dev);
2607 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
2608 priv->rx_skb_size + ETH_FCS_LEN,
2609 prof->tx_pause, prof->tx_ppp,
2610 prof->rx_pause, prof->rx_ppp);
2611 if (err) {
2612 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
2613 priv->port, err);
2614 goto out;
2615 }
2616
2617 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
2618 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
2619 if (err) {
2620 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
2621 err);
2622 goto out;
2623 }
2624 }
2625
2626 /* Init port */
2627 en_warn(priv, "Initializing port\n");
2628 err = mlx4_INIT_PORT(mdev->dev, priv->port);
2629 if (err) {
2630 en_err(priv, "Failed Initializing port\n");
2631 goto out;
2632 }
2633 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
2634
2635 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2636 queue_delayed_work(mdev->workqueue, &priv->service_task,
2637 SERVICE_TASK_DELAY);
2638
2639 return 0;
2640
2641out:
2642 mlx4_en_destroy_netdev(dev);
2643 return err;
2644}
2645
2646int mlx4_en_reset_config(struct net_device *dev,
2647 struct hwtstamp_config ts_config,
2648 netdev_features_t features)
2649{
2650 struct mlx4_en_priv *priv = netdev_priv(dev);
2651 struct mlx4_en_dev *mdev = priv->mdev;
2652 int port_up = 0;
2653 int err = 0;
2654
2655 if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
2656 priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
2657 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX))
2658 return 0; /* Nothing to change */
2659
2660 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
2661 (features & NETIF_F_HW_VLAN_CTAG_RX) &&
2662 (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
2663 en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
2664 return -EINVAL;
2665 }
2666
2667 mutex_lock(&mdev->state_lock);
2668 if (priv->port_up) {
2669 port_up = 1;
2670 mlx4_en_stop_port(dev, 1);
2671 }
2672
2673 mlx4_en_free_resources(priv);
2674
2675 en_warn(priv, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
2676 ts_config.rx_filter, !!(features & NETIF_F_HW_VLAN_CTAG_RX));
2677
2678 priv->hwtstamp_config.tx_type = ts_config.tx_type;
2679 priv->hwtstamp_config.rx_filter = ts_config.rx_filter;
2680
2681 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2682 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2683 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
2684 else
2685 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
2686 } else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
2687 /* RX time-stamping is OFF, update the RX vlan offload
2688 * to the latest wanted state
2689 */
2690 if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
2691 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
2692 else
2693 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
2694 }
2695
2696 /* RX vlan offload and RX time-stamping can't co-exist !
2697 * Regardless of the caller's choice,
2698 * Turn Off RX vlan offload in case of time-stamping is ON
2699 */
2700 if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
2701 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
2702 en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
2703 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
2704 }
2705
2706 err = mlx4_en_alloc_resources(priv);
2707 if (err) {
2708 en_err(priv, "Failed reallocating port resources\n");
2709 goto out;
2710 }
2711 if (port_up) {
2712 err = mlx4_en_start_port(dev);
2713 if (err)
2714 en_err(priv, "Failed starting port\n");
2715 }
2716
2717out:
2718 mutex_unlock(&mdev->state_lock);
2719 netdev_features_change(dev);
2720 return err;
2721}
This page took 0.036322 seconds and 5 git commands to generate.