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