1 /*
2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 *
18 */
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/pci.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/if.h>
32 #include <linux/if_ether.h>
33 #include <linux/if_vlan.h>
34 #include <linux/in.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/tcp.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/prefetch.h>
40 #include <net/ip6_checksum.h>
41 #include <linux/ktime.h>
42 #include <linux/numa.h>
43 #ifdef CONFIG_RFS_ACCEL
44 #include <linux/cpu_rmap.h>
45 #endif
46 #include <linux/crash_dump.h>
47 #include <net/busy_poll.h>
48 #include <net/vxlan.h>
49 #include <net/netdev_queues.h>
50
51 #include "cq_enet_desc.h"
52 #include "vnic_dev.h"
53 #include "vnic_intr.h"
54 #include "vnic_stats.h"
55 #include "vnic_vic.h"
56 #include "enic_res.h"
57 #include "enic.h"
58 #include "enic_dev.h"
59 #include "enic_pp.h"
60 #include "enic_clsf.h"
61 #include "enic_rq.h"
62 #include "enic_wq.h"
63 #include "enic_admin.h"
64 #include "enic_mbox.h"
65
66 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
67
68 #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
69 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
70 #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF 0x0071 /* enet SRIOV VF */
71 #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF_V2 0x02b7 /* enet SRIOV V2 VF */
72 #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF_USNIC 0x00cf /* enet USNIC VF */
73
74 /* Supported devices */
75 static const struct pci_device_id enic_id_table[] = {
76 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
77 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
78 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
79 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF_V2) },
80 { 0, } /* end of table */
81 };
82
83 MODULE_DESCRIPTION(DRV_DESCRIPTION);
84 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
85 MODULE_LICENSE("GPL");
86 MODULE_DEVICE_TABLE(pci, enic_id_table);
87
88 #define ENIC_LARGE_PKT_THRESHOLD 1000
89 #define ENIC_MAX_COALESCE_TIMERS 10
90 /* Interrupt moderation table, which will be used to decide the
91 * coalescing timer values
92 * {rx_rate in Mbps, mapping percentage of the range}
93 */
94 static struct enic_intr_mod_table mod_table[ENIC_MAX_COALESCE_TIMERS + 1] = {
95 {4000, 0},
96 {4400, 10},
97 {5060, 20},
98 {5230, 30},
99 {5540, 40},
100 {5820, 50},
101 {6120, 60},
102 {6435, 70},
103 {6745, 80},
104 {7000, 90},
105 {0xFFFFFFFF, 100}
106 };
107
108 /* This table helps the driver to pick different ranges for rx coalescing
109 * timer depending on the link speed.
110 */
111 static struct enic_intr_mod_range mod_range[ENIC_MAX_LINK_SPEEDS] = {
112 {0, 0}, /* 0 - 4 Gbps */
113 {0, 3}, /* 4 - 10 Gbps */
114 {3, 6}, /* 10+ Gbps */
115 };
116
enic_init_affinity_hint(struct enic * enic)117 static void enic_init_affinity_hint(struct enic *enic)
118 {
119 int numa_node = dev_to_node(&enic->pdev->dev);
120 int i;
121
122 for (i = 0; i < enic->intr_count; i++) {
123 if (enic_is_err_intr(enic, i) || enic_is_notify_intr(enic, i) ||
124 (cpumask_available(enic->msix[i].affinity_mask) &&
125 !cpumask_empty(enic->msix[i].affinity_mask)))
126 continue;
127 if (zalloc_cpumask_var(&enic->msix[i].affinity_mask,
128 GFP_KERNEL))
129 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
130 enic->msix[i].affinity_mask);
131 }
132 }
133
enic_free_affinity_hint(struct enic * enic)134 static void enic_free_affinity_hint(struct enic *enic)
135 {
136 int i;
137
138 for (i = 0; i < enic->intr_count; i++) {
139 if (enic_is_err_intr(enic, i) || enic_is_notify_intr(enic, i))
140 continue;
141 free_cpumask_var(enic->msix[i].affinity_mask);
142 }
143 }
144
enic_set_affinity_hint(struct enic * enic)145 static void enic_set_affinity_hint(struct enic *enic)
146 {
147 int i;
148 int err;
149
150 for (i = 0; i < enic->intr_count; i++) {
151 if (enic_is_err_intr(enic, i) ||
152 enic_is_notify_intr(enic, i) ||
153 !cpumask_available(enic->msix[i].affinity_mask) ||
154 cpumask_empty(enic->msix[i].affinity_mask))
155 continue;
156 err = irq_update_affinity_hint(enic->msix_entry[i].vector,
157 enic->msix[i].affinity_mask);
158 if (err)
159 netdev_warn(enic->netdev, "irq_update_affinity_hint failed, err %d\n",
160 err);
161 }
162
163 for (i = 0; i < enic->wq_count; i++) {
164 int wq_intr = enic_msix_wq_intr(enic, i);
165
166 if (cpumask_available(enic->msix[wq_intr].affinity_mask) &&
167 !cpumask_empty(enic->msix[wq_intr].affinity_mask))
168 netif_set_xps_queue(enic->netdev,
169 enic->msix[wq_intr].affinity_mask,
170 i);
171 }
172 }
173
enic_unset_affinity_hint(struct enic * enic)174 static void enic_unset_affinity_hint(struct enic *enic)
175 {
176 int i;
177
178 for (i = 0; i < enic->intr_count; i++)
179 irq_update_affinity_hint(enic->msix_entry[i].vector, NULL);
180 }
181
enic_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)182 static int enic_udp_tunnel_set_port(struct net_device *netdev,
183 unsigned int table, unsigned int entry,
184 struct udp_tunnel_info *ti)
185 {
186 struct enic *enic = netdev_priv(netdev);
187 int err;
188
189 spin_lock_bh(&enic->devcmd_lock);
190
191 err = vnic_dev_overlay_offload_cfg(enic->vdev,
192 OVERLAY_CFG_VXLAN_PORT_UPDATE,
193 ntohs(ti->port));
194 if (err)
195 goto error;
196
197 err = vnic_dev_overlay_offload_ctrl(enic->vdev, OVERLAY_FEATURE_VXLAN,
198 enic->vxlan.patch_level);
199 if (err)
200 goto error;
201
202 enic->vxlan.vxlan_udp_port_number = ntohs(ti->port);
203 error:
204 spin_unlock_bh(&enic->devcmd_lock);
205
206 return err;
207 }
208
enic_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)209 static int enic_udp_tunnel_unset_port(struct net_device *netdev,
210 unsigned int table, unsigned int entry,
211 struct udp_tunnel_info *ti)
212 {
213 struct enic *enic = netdev_priv(netdev);
214 int err;
215
216 spin_lock_bh(&enic->devcmd_lock);
217
218 err = vnic_dev_overlay_offload_ctrl(enic->vdev, OVERLAY_FEATURE_VXLAN,
219 OVERLAY_OFFLOAD_DISABLE);
220 if (err)
221 goto unlock;
222
223 enic->vxlan.vxlan_udp_port_number = 0;
224
225 unlock:
226 spin_unlock_bh(&enic->devcmd_lock);
227
228 return err;
229 }
230
231 static const struct udp_tunnel_nic_info enic_udp_tunnels = {
232 .set_port = enic_udp_tunnel_set_port,
233 .unset_port = enic_udp_tunnel_unset_port,
234 .tables = {
235 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
236 },
237 }, enic_udp_tunnels_v4 = {
238 .set_port = enic_udp_tunnel_set_port,
239 .unset_port = enic_udp_tunnel_unset_port,
240 .flags = UDP_TUNNEL_NIC_INFO_IPV4_ONLY,
241 .tables = {
242 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
243 },
244 };
245
enic_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)246 static netdev_features_t enic_features_check(struct sk_buff *skb,
247 struct net_device *dev,
248 netdev_features_t features)
249 {
250 const struct ethhdr *eth = (struct ethhdr *)skb_inner_mac_header(skb);
251 struct enic *enic = netdev_priv(dev);
252 struct udphdr *udph;
253 u16 port = 0;
254 u8 proto;
255
256 if (!skb->encapsulation)
257 return features;
258
259 features = vxlan_features_check(skb, features);
260
261 switch (vlan_get_protocol(skb)) {
262 case htons(ETH_P_IPV6):
263 if (!(enic->vxlan.flags & ENIC_VXLAN_OUTER_IPV6))
264 goto out;
265 proto = ipv6_hdr(skb)->nexthdr;
266 break;
267 case htons(ETH_P_IP):
268 proto = ip_hdr(skb)->protocol;
269 break;
270 default:
271 goto out;
272 }
273
274 switch (eth->h_proto) {
275 case ntohs(ETH_P_IPV6):
276 if (!(enic->vxlan.flags & ENIC_VXLAN_INNER_IPV6))
277 goto out;
278 fallthrough;
279 case ntohs(ETH_P_IP):
280 break;
281 default:
282 goto out;
283 }
284
285
286 if (proto == IPPROTO_UDP) {
287 udph = udp_hdr(skb);
288 port = be16_to_cpu(udph->dest);
289 }
290
291 /* HW supports offload of only one UDP port. Remove CSUM and GSO MASK
292 * for other UDP port tunnels
293 */
294 if (port != enic->vxlan.vxlan_udp_port_number)
295 goto out;
296
297 return features;
298
299 out:
300 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
301 }
302
enic_is_dynamic(struct enic * enic)303 int enic_is_dynamic(struct enic *enic)
304 {
305 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
306 }
307
enic_sriov_enabled(struct enic * enic)308 int enic_sriov_enabled(struct enic *enic)
309 {
310 return (enic->priv_flags & ENIC_SRIOV_ENABLED) ? 1 : 0;
311 }
312
enic_is_sriov_vf(struct enic * enic)313 static int enic_is_sriov_vf(struct enic *enic)
314 {
315 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF ||
316 enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF_V2;
317 }
318
enic_is_sriov_vf_v2(struct enic * enic)319 int enic_is_sriov_vf_v2(struct enic *enic)
320 {
321 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF_V2;
322 }
323
enic_is_valid_vf(struct enic * enic,int vf)324 int enic_is_valid_vf(struct enic *enic, int vf)
325 {
326 #ifdef CONFIG_PCI_IOV
327 return vf >= 0 && vf < enic->num_vfs;
328 #else
329 return 0;
330 #endif
331 }
332
enic_log_q_error(struct enic * enic)333 static bool enic_log_q_error(struct enic *enic)
334 {
335 unsigned int i;
336 u32 error_status;
337 bool err = false;
338
339 for (i = 0; i < enic->wq_count; i++) {
340 error_status = vnic_wq_error_status(&enic->wq[i].vwq);
341 err |= error_status;
342 if (error_status)
343 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
344 i, error_status);
345 }
346
347 for (i = 0; i < enic->rq_count; i++) {
348 error_status = vnic_rq_error_status(&enic->rq[i].vrq);
349 err |= error_status;
350 if (error_status)
351 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
352 i, error_status);
353 }
354
355 return err;
356 }
357
enic_msglvl_check(struct enic * enic)358 static void enic_msglvl_check(struct enic *enic)
359 {
360 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
361
362 if (msg_enable != enic->msg_enable) {
363 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
364 enic->msg_enable, msg_enable);
365 enic->msg_enable = msg_enable;
366 }
367 }
368
enic_mtu_check(struct enic * enic)369 static void enic_mtu_check(struct enic *enic)
370 {
371 u32 mtu = vnic_dev_mtu(enic->vdev);
372 struct net_device *netdev = enic->netdev;
373
374 if (mtu && mtu != enic->port_mtu) {
375 enic->port_mtu = mtu;
376 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
377 mtu = max_t(int, ENIC_MIN_MTU,
378 min_t(int, ENIC_MAX_MTU, mtu));
379 if (mtu != netdev->mtu)
380 schedule_work(&enic->change_mtu_work);
381 } else {
382 if (mtu < netdev->mtu)
383 netdev_warn(netdev,
384 "interface MTU (%d) set higher "
385 "than switch port MTU (%d)\n",
386 netdev->mtu, mtu);
387 }
388 }
389 }
390
enic_set_rx_coal_setting(struct enic * enic)391 static void enic_set_rx_coal_setting(struct enic *enic)
392 {
393 unsigned int speed;
394 int index = -1;
395 struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
396
397 /* 1. Read the link speed from fw
398 * 2. Pick the default range for the speed
399 * 3. Update it in enic->rx_coalesce_setting
400 */
401 speed = vnic_dev_port_speed(enic->vdev);
402 if (speed > ENIC_LINK_SPEED_10G)
403 index = ENIC_LINK_40G_INDEX;
404 else if (speed > ENIC_LINK_SPEED_4G)
405 index = ENIC_LINK_10G_INDEX;
406 else
407 index = ENIC_LINK_4G_INDEX;
408
409 rx_coal->small_pkt_range_start = mod_range[index].small_pkt_range_start;
410 rx_coal->large_pkt_range_start = mod_range[index].large_pkt_range_start;
411 rx_coal->range_end = ENIC_RX_COALESCE_RANGE_END;
412
413 /* Start with the value provided by UCSM */
414 for (index = 0; index < enic->rq_count; index++)
415 enic->cq[index].cur_rx_coal_timeval =
416 enic->config.intr_timer_usec;
417
418 rx_coal->use_adaptive_rx_coalesce = 1;
419 }
420
enic_link_notify_work_handler(struct work_struct * work)421 static void enic_link_notify_work_handler(struct work_struct *work)
422 {
423 struct enic *enic = container_of(work, struct enic,
424 link_notify_work);
425 u32 state;
426 u16 i;
427
428 if (!enic_sriov_enabled(enic) || !enic->vf_state)
429 return;
430
431 state = netif_carrier_ok(enic->netdev) ?
432 ENIC_MBOX_LINK_STATE_ENABLE :
433 ENIC_MBOX_LINK_STATE_DISABLE;
434
435 for (i = 0; i < enic->num_vfs; i++)
436 enic_mbox_send_link_state(enic, i, state);
437 }
438
enic_link_check(struct enic * enic)439 static void enic_link_check(struct enic *enic)
440 {
441 int link_status;
442 int carrier_ok;
443
444 /* A V2 SR-IOV VF's carrier is driven by PF link-state MBOX
445 * notifications, not by its own vnic link status; skip the
446 * autonomous check so it cannot flap the VF carrier.
447 */
448 if (enic_is_sriov_vf_v2(enic))
449 return;
450
451 link_status = vnic_dev_link_status(enic->vdev);
452 carrier_ok = netif_carrier_ok(enic->netdev);
453
454 if (link_status && !carrier_ok) {
455 netdev_info(enic->netdev, "Link UP\n");
456 netif_carrier_on(enic->netdev);
457 enic_set_rx_coal_setting(enic);
458 if (enic_sriov_enabled(enic) && enic->vf_state)
459 schedule_work(&enic->link_notify_work);
460 } else if (!link_status && carrier_ok) {
461 netdev_info(enic->netdev, "Link DOWN\n");
462 netif_carrier_off(enic->netdev);
463 if (enic_sriov_enabled(enic) && enic->vf_state)
464 schedule_work(&enic->link_notify_work);
465 }
466 }
467
enic_notify_check(struct enic * enic)468 static void enic_notify_check(struct enic *enic)
469 {
470 enic_msglvl_check(enic);
471 enic_mtu_check(enic);
472 enic_link_check(enic);
473 }
474
475 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
476
enic_isr_legacy(int irq,void * data)477 static irqreturn_t enic_isr_legacy(int irq, void *data)
478 {
479 struct net_device *netdev = data;
480 struct enic *enic = netdev_priv(netdev);
481 unsigned int io_intr = ENIC_LEGACY_IO_INTR;
482 unsigned int err_intr = ENIC_LEGACY_ERR_INTR;
483 unsigned int notify_intr = ENIC_LEGACY_NOTIFY_INTR;
484 u32 pba;
485
486 vnic_intr_mask(&enic->intr[io_intr]);
487
488 pba = vnic_intr_legacy_pba(enic->legacy_pba);
489 if (!pba) {
490 vnic_intr_unmask(&enic->intr[io_intr]);
491 return IRQ_NONE; /* not our interrupt */
492 }
493
494 if (ENIC_TEST_INTR(pba, notify_intr)) {
495 enic_notify_check(enic);
496 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
497 }
498
499 if (ENIC_TEST_INTR(pba, err_intr)) {
500 vnic_intr_return_all_credits(&enic->intr[err_intr]);
501 enic_log_q_error(enic);
502 /* schedule recovery from WQ/RQ error */
503 schedule_work(&enic->reset);
504 return IRQ_HANDLED;
505 }
506
507 if (ENIC_TEST_INTR(pba, io_intr))
508 napi_schedule_irqoff(&enic->napi[0]);
509 else
510 vnic_intr_unmask(&enic->intr[io_intr]);
511
512 return IRQ_HANDLED;
513 }
514
enic_isr_msi(int irq,void * data)515 static irqreturn_t enic_isr_msi(int irq, void *data)
516 {
517 struct enic *enic = data;
518
519 /* With MSI, there is no sharing of interrupts, so this is
520 * our interrupt and there is no need to ack it. The device
521 * is not providing per-vector masking, so the OS will not
522 * write to PCI config space to mask/unmask the interrupt.
523 * We're using mask_on_assertion for MSI, so the device
524 * automatically masks the interrupt when the interrupt is
525 * generated. Later, when exiting polling, the interrupt
526 * will be unmasked (see enic_poll).
527 *
528 * Also, the device uses the same PCIe Traffic Class (TC)
529 * for Memory Write data and MSI, so there are no ordering
530 * issues; the MSI will always arrive at the Root Complex
531 * _after_ corresponding Memory Writes (i.e. descriptor
532 * writes).
533 */
534
535 napi_schedule_irqoff(&enic->napi[0]);
536
537 return IRQ_HANDLED;
538 }
539
enic_isr_msix(int irq,void * data)540 static irqreturn_t enic_isr_msix(int irq, void *data)
541 {
542 struct napi_struct *napi = data;
543
544 napi_schedule_irqoff(napi);
545
546 return IRQ_HANDLED;
547 }
548
enic_isr_msix_err(int irq,void * data)549 static irqreturn_t enic_isr_msix_err(int irq, void *data)
550 {
551 struct enic *enic = data;
552 unsigned int intr = enic_msix_err_intr(enic);
553
554 vnic_intr_return_all_credits(&enic->intr[intr]);
555
556 if (enic_log_q_error(enic))
557 /* schedule recovery from WQ/RQ error */
558 schedule_work(&enic->reset);
559
560 return IRQ_HANDLED;
561 }
562
enic_isr_msix_notify(int irq,void * data)563 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
564 {
565 struct enic *enic = data;
566 unsigned int intr = enic_msix_notify_intr(enic);
567
568 enic_notify_check(enic);
569 vnic_intr_return_all_credits(&enic->intr[intr]);
570
571 return IRQ_HANDLED;
572 }
573
enic_queue_wq_skb_cont(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,unsigned int len_left,int loopback)574 static int enic_queue_wq_skb_cont(struct enic *enic, struct vnic_wq *wq,
575 struct sk_buff *skb, unsigned int len_left,
576 int loopback)
577 {
578 const skb_frag_t *frag;
579 dma_addr_t dma_addr;
580
581 /* Queue additional data fragments */
582 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
583 len_left -= skb_frag_size(frag);
584 dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag, 0,
585 skb_frag_size(frag),
586 DMA_TO_DEVICE);
587 if (unlikely(enic_dma_map_check(enic, dma_addr)))
588 return -ENOMEM;
589 enic_queue_wq_desc_cont(wq, skb, dma_addr, skb_frag_size(frag),
590 (len_left == 0), /* EOP? */
591 loopback);
592 }
593
594 return 0;
595 }
596
enic_queue_wq_skb_vlan(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,int vlan_tag_insert,unsigned int vlan_tag,int loopback)597 static int enic_queue_wq_skb_vlan(struct enic *enic, struct vnic_wq *wq,
598 struct sk_buff *skb, int vlan_tag_insert,
599 unsigned int vlan_tag, int loopback)
600 {
601 unsigned int head_len = skb_headlen(skb);
602 unsigned int len_left = skb->len - head_len;
603 int eop = (len_left == 0);
604 dma_addr_t dma_addr;
605 int err = 0;
606
607 dma_addr = dma_map_single(&enic->pdev->dev, skb->data, head_len,
608 DMA_TO_DEVICE);
609 if (unlikely(enic_dma_map_check(enic, dma_addr)))
610 return -ENOMEM;
611
612 /* Queue the main skb fragment. The fragments are no larger
613 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
614 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
615 * per fragment is queued.
616 */
617 enic_queue_wq_desc(wq, skb, dma_addr, head_len, vlan_tag_insert,
618 vlan_tag, eop, loopback);
619
620 if (!eop)
621 err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
622
623 /* The enic_queue_wq_desc() above does not do HW checksum */
624 enic->wq[wq->index].stats.csum_none++;
625 enic->wq[wq->index].stats.packets++;
626 enic->wq[wq->index].stats.bytes += skb->len;
627
628 return err;
629 }
630
enic_queue_wq_skb_csum_l4(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,int vlan_tag_insert,unsigned int vlan_tag,int loopback)631 static int enic_queue_wq_skb_csum_l4(struct enic *enic, struct vnic_wq *wq,
632 struct sk_buff *skb, int vlan_tag_insert,
633 unsigned int vlan_tag, int loopback)
634 {
635 unsigned int head_len = skb_headlen(skb);
636 unsigned int len_left = skb->len - head_len;
637 unsigned int hdr_len = skb_checksum_start_offset(skb);
638 unsigned int csum_offset = hdr_len + skb->csum_offset;
639 int eop = (len_left == 0);
640 dma_addr_t dma_addr;
641 int err = 0;
642
643 dma_addr = dma_map_single(&enic->pdev->dev, skb->data, head_len,
644 DMA_TO_DEVICE);
645 if (unlikely(enic_dma_map_check(enic, dma_addr)))
646 return -ENOMEM;
647
648 /* Queue the main skb fragment. The fragments are no larger
649 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
650 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
651 * per fragment is queued.
652 */
653 enic_queue_wq_desc_csum_l4(wq, skb, dma_addr, head_len, csum_offset,
654 hdr_len, vlan_tag_insert, vlan_tag, eop,
655 loopback);
656
657 if (!eop)
658 err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
659
660 enic->wq[wq->index].stats.csum_partial++;
661 enic->wq[wq->index].stats.packets++;
662 enic->wq[wq->index].stats.bytes += skb->len;
663
664 return err;
665 }
666
enic_preload_tcp_csum_encap(struct sk_buff * skb)667 static void enic_preload_tcp_csum_encap(struct sk_buff *skb)
668 {
669 const struct ethhdr *eth = (struct ethhdr *)skb_inner_mac_header(skb);
670
671 switch (eth->h_proto) {
672 case ntohs(ETH_P_IP):
673 inner_ip_hdr(skb)->check = 0;
674 inner_tcp_hdr(skb)->check =
675 ~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr,
676 inner_ip_hdr(skb)->daddr, 0,
677 IPPROTO_TCP, 0);
678 break;
679 case ntohs(ETH_P_IPV6):
680 inner_tcp_hdr(skb)->check =
681 ~csum_ipv6_magic(&inner_ipv6_hdr(skb)->saddr,
682 &inner_ipv6_hdr(skb)->daddr, 0,
683 IPPROTO_TCP, 0);
684 break;
685 default:
686 WARN_ONCE(1, "Non ipv4/ipv6 inner pkt for encap offload");
687 break;
688 }
689 }
690
enic_preload_tcp_csum(struct sk_buff * skb)691 static void enic_preload_tcp_csum(struct sk_buff *skb)
692 {
693 /* Preload TCP csum field with IP pseudo hdr calculated
694 * with IP length set to zero. HW will later add in length
695 * to each TCP segment resulting from the TSO.
696 */
697
698 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
699 ip_hdr(skb)->check = 0;
700 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
701 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
702 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
703 tcp_v6_gso_csum_prep(skb);
704 }
705 }
706
enic_queue_wq_skb_tso(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,unsigned int mss,int vlan_tag_insert,unsigned int vlan_tag,int loopback)707 static int enic_queue_wq_skb_tso(struct enic *enic, struct vnic_wq *wq,
708 struct sk_buff *skb, unsigned int mss,
709 int vlan_tag_insert, unsigned int vlan_tag,
710 int loopback)
711 {
712 unsigned int frag_len_left = skb_headlen(skb);
713 unsigned int len_left = skb->len - frag_len_left;
714 int eop = (len_left == 0);
715 unsigned int offset = 0;
716 unsigned int hdr_len;
717 dma_addr_t dma_addr;
718 unsigned int pkts;
719 unsigned int len;
720 skb_frag_t *frag;
721
722 if (skb->encapsulation) {
723 hdr_len = skb_inner_tcp_all_headers(skb);
724 enic_preload_tcp_csum_encap(skb);
725 enic->wq[wq->index].stats.encap_tso++;
726 } else {
727 hdr_len = skb_tcp_all_headers(skb);
728 enic_preload_tcp_csum(skb);
729 enic->wq[wq->index].stats.tso++;
730 }
731
732 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
733 * for the main skb fragment
734 */
735 while (frag_len_left) {
736 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
737 dma_addr = dma_map_single(&enic->pdev->dev,
738 skb->data + offset, len,
739 DMA_TO_DEVICE);
740 if (unlikely(enic_dma_map_check(enic, dma_addr)))
741 return -ENOMEM;
742 enic_queue_wq_desc_tso(wq, skb, dma_addr, len, mss, hdr_len,
743 vlan_tag_insert, vlan_tag,
744 eop && (len == frag_len_left), loopback);
745 frag_len_left -= len;
746 offset += len;
747 }
748
749 if (eop)
750 goto tso_out_stats;
751
752 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
753 * for additional data fragments
754 */
755 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
756 len_left -= skb_frag_size(frag);
757 frag_len_left = skb_frag_size(frag);
758 offset = 0;
759
760 while (frag_len_left) {
761 len = min(frag_len_left,
762 (unsigned int)WQ_ENET_MAX_DESC_LEN);
763 dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag,
764 offset, len,
765 DMA_TO_DEVICE);
766 if (unlikely(enic_dma_map_check(enic, dma_addr)))
767 return -ENOMEM;
768 enic_queue_wq_desc_cont(wq, skb, dma_addr, len,
769 (len_left == 0) &&
770 (len == frag_len_left),/*EOP*/
771 loopback);
772 frag_len_left -= len;
773 offset += len;
774 }
775 }
776
777 tso_out_stats:
778 /* calculate how many packets tso sent */
779 len = skb->len - hdr_len;
780 pkts = len / mss;
781 if ((len % mss) > 0)
782 pkts++;
783 enic->wq[wq->index].stats.packets += pkts;
784 enic->wq[wq->index].stats.bytes += (len + (pkts * hdr_len));
785
786 return 0;
787 }
788
enic_queue_wq_skb_encap(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,int vlan_tag_insert,unsigned int vlan_tag,int loopback)789 static inline int enic_queue_wq_skb_encap(struct enic *enic, struct vnic_wq *wq,
790 struct sk_buff *skb,
791 int vlan_tag_insert,
792 unsigned int vlan_tag, int loopback)
793 {
794 unsigned int head_len = skb_headlen(skb);
795 unsigned int len_left = skb->len - head_len;
796 /* Hardware will overwrite the checksum fields, calculating from
797 * scratch and ignoring the value placed by software.
798 * Offload mode = 00
799 * mss[2], mss[1], mss[0] bits are set
800 */
801 unsigned int mss_or_csum = 7;
802 int eop = (len_left == 0);
803 dma_addr_t dma_addr;
804 int err = 0;
805
806 dma_addr = dma_map_single(&enic->pdev->dev, skb->data, head_len,
807 DMA_TO_DEVICE);
808 if (unlikely(enic_dma_map_check(enic, dma_addr)))
809 return -ENOMEM;
810
811 enic_queue_wq_desc_ex(wq, skb, dma_addr, head_len, mss_or_csum, 0,
812 vlan_tag_insert, vlan_tag,
813 WQ_ENET_OFFLOAD_MODE_CSUM, eop, 1 /* SOP */, eop,
814 loopback);
815 if (!eop)
816 err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
817
818 enic->wq[wq->index].stats.encap_csum++;
819 enic->wq[wq->index].stats.packets++;
820 enic->wq[wq->index].stats.bytes += skb->len;
821
822 return err;
823 }
824
enic_queue_wq_skb(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb)825 static inline int enic_queue_wq_skb(struct enic *enic,
826 struct vnic_wq *wq, struct sk_buff *skb)
827 {
828 unsigned int mss = skb_shinfo(skb)->gso_size;
829 unsigned int vlan_tag = 0;
830 int vlan_tag_insert = 0;
831 int loopback = 0;
832 int err;
833
834 if (skb_vlan_tag_present(skb)) {
835 /* VLAN tag from trunking driver */
836 vlan_tag_insert = 1;
837 vlan_tag = skb_vlan_tag_get(skb);
838 enic->wq[wq->index].stats.add_vlan++;
839 } else if (enic->loop_enable) {
840 vlan_tag = enic->loop_tag;
841 loopback = 1;
842 }
843
844 if (mss)
845 err = enic_queue_wq_skb_tso(enic, wq, skb, mss,
846 vlan_tag_insert, vlan_tag,
847 loopback);
848 else if (skb->encapsulation)
849 err = enic_queue_wq_skb_encap(enic, wq, skb, vlan_tag_insert,
850 vlan_tag, loopback);
851 else if (skb->ip_summed == CHECKSUM_PARTIAL)
852 err = enic_queue_wq_skb_csum_l4(enic, wq, skb, vlan_tag_insert,
853 vlan_tag, loopback);
854 else
855 err = enic_queue_wq_skb_vlan(enic, wq, skb, vlan_tag_insert,
856 vlan_tag, loopback);
857 if (unlikely(err)) {
858 struct vnic_wq_buf *buf;
859
860 buf = wq->to_use->prev;
861 /* while not EOP of previous pkt && queue not empty.
862 * For all non EOP bufs, os_buf is NULL.
863 */
864 while (!buf->os_buf && (buf->next != wq->to_clean)) {
865 enic_free_wq_buf(wq, buf);
866 wq->ring.desc_avail++;
867 buf = buf->prev;
868 }
869 wq->to_use = buf->next;
870 dev_kfree_skb(skb);
871 }
872 return err;
873 }
874
875 /* netif_tx_lock held, process context with BHs disabled, or BH */
enic_hard_start_xmit(struct sk_buff * skb,struct net_device * netdev)876 static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
877 struct net_device *netdev)
878 {
879 struct enic *enic = netdev_priv(netdev);
880 struct vnic_wq *wq;
881 unsigned int txq_map;
882 struct netdev_queue *txq;
883
884 txq_map = skb_get_queue_mapping(skb) % enic->wq_count;
885 wq = &enic->wq[txq_map].vwq;
886
887 if (skb->len <= 0) {
888 dev_kfree_skb_any(skb);
889 enic->wq[wq->index].stats.null_pkt++;
890 return NETDEV_TX_OK;
891 }
892
893 txq = netdev_get_tx_queue(netdev, txq_map);
894
895 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
896 * which is very likely. In the off chance it's going to take
897 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
898 */
899
900 if (skb_shinfo(skb)->gso_size == 0 &&
901 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
902 skb_linearize(skb)) {
903 dev_kfree_skb_any(skb);
904 enic->wq[wq->index].stats.skb_linear_fail++;
905 return NETDEV_TX_OK;
906 }
907
908 spin_lock(&enic->wq[txq_map].lock);
909
910 if (vnic_wq_desc_avail(wq) <
911 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
912 netif_tx_stop_queue(txq);
913 /* This is a hard error, log it */
914 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
915 spin_unlock(&enic->wq[txq_map].lock);
916 enic->wq[wq->index].stats.desc_full_awake++;
917 return NETDEV_TX_BUSY;
918 }
919
920 if (enic_queue_wq_skb(enic, wq, skb))
921 goto error;
922
923 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS) {
924 netif_tx_stop_queue(txq);
925 enic->wq[wq->index].stats.stopped++;
926 }
927 skb_tx_timestamp(skb);
928 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
929 vnic_wq_doorbell(wq);
930
931 error:
932 spin_unlock(&enic->wq[txq_map].lock);
933
934 return NETDEV_TX_OK;
935 }
936
937 /* rcu_read_lock potentially held, nominally process context */
enic_get_stats(struct net_device * netdev,struct rtnl_link_stats64 * net_stats)938 static void enic_get_stats(struct net_device *netdev,
939 struct rtnl_link_stats64 *net_stats)
940 {
941 struct enic *enic = netdev_priv(netdev);
942 struct vnic_stats *stats;
943 u64 pkt_truncated = 0;
944 u64 bad_fcs = 0;
945 int err;
946 int i;
947
948 err = enic_dev_stats_dump(enic, &stats);
949 /* return only when dma_alloc_coherent fails in vnic_dev_stats_dump
950 * For other failures, like devcmd failure, we return previously
951 * recorded stats.
952 */
953 if (err == -ENOMEM)
954 return;
955
956 net_stats->tx_packets = stats->tx.tx_frames_ok;
957 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
958 net_stats->tx_errors = stats->tx.tx_errors;
959 net_stats->tx_dropped = stats->tx.tx_drops;
960
961 net_stats->rx_packets = stats->rx.rx_frames_ok;
962 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
963 net_stats->rx_errors = stats->rx.rx_errors;
964 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
965
966 for (i = 0; i < enic->rq_count; i++) {
967 struct enic_rq_stats *rqs = &enic->rq[i].stats;
968
969 if (!enic->rq[i].vrq.ctrl)
970 break;
971 pkt_truncated += rqs->pkt_truncated;
972 bad_fcs += rqs->bad_fcs;
973 }
974 net_stats->rx_over_errors = pkt_truncated;
975 net_stats->rx_crc_errors = bad_fcs;
976 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
977 }
978
enic_mc_sync(struct net_device * netdev,const u8 * mc_addr)979 static int enic_mc_sync(struct net_device *netdev, const u8 *mc_addr)
980 {
981 struct enic *enic = netdev_priv(netdev);
982
983 if (enic->mc_count == ENIC_MULTICAST_PERFECT_FILTERS) {
984 unsigned int mc_count = netdev_mc_count(netdev);
985
986 netdev_warn(netdev, "Registering only %d out of %d multicast addresses\n",
987 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
988
989 return -ENOSPC;
990 }
991
992 enic_dev_add_addr(enic, mc_addr);
993 enic->mc_count++;
994
995 return 0;
996 }
997
enic_mc_unsync(struct net_device * netdev,const u8 * mc_addr)998 static int enic_mc_unsync(struct net_device *netdev, const u8 *mc_addr)
999 {
1000 struct enic *enic = netdev_priv(netdev);
1001
1002 enic_dev_del_addr(enic, mc_addr);
1003 enic->mc_count--;
1004
1005 return 0;
1006 }
1007
enic_uc_sync(struct net_device * netdev,const u8 * uc_addr)1008 static int enic_uc_sync(struct net_device *netdev, const u8 *uc_addr)
1009 {
1010 struct enic *enic = netdev_priv(netdev);
1011
1012 if (enic->uc_count == ENIC_UNICAST_PERFECT_FILTERS) {
1013 unsigned int uc_count = netdev_uc_count(netdev);
1014
1015 netdev_warn(netdev, "Registering only %d out of %d unicast addresses\n",
1016 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
1017
1018 return -ENOSPC;
1019 }
1020
1021 enic_dev_add_addr(enic, uc_addr);
1022 enic->uc_count++;
1023
1024 return 0;
1025 }
1026
enic_uc_unsync(struct net_device * netdev,const u8 * uc_addr)1027 static int enic_uc_unsync(struct net_device *netdev, const u8 *uc_addr)
1028 {
1029 struct enic *enic = netdev_priv(netdev);
1030
1031 enic_dev_del_addr(enic, uc_addr);
1032 enic->uc_count--;
1033
1034 return 0;
1035 }
1036
enic_reset_addr_lists(struct enic * enic)1037 void enic_reset_addr_lists(struct enic *enic)
1038 {
1039 struct net_device *netdev = enic->netdev;
1040
1041 __dev_uc_unsync(netdev, NULL);
1042 __dev_mc_unsync(netdev, NULL);
1043
1044 enic->mc_count = 0;
1045 enic->uc_count = 0;
1046 enic->flags = 0;
1047 }
1048
enic_set_mac_addr(struct net_device * netdev,char * addr)1049 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
1050 {
1051 struct enic *enic = netdev_priv(netdev);
1052
1053 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
1054 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
1055 return -EADDRNOTAVAIL;
1056 } else {
1057 if (!is_valid_ether_addr(addr))
1058 return -EADDRNOTAVAIL;
1059 }
1060
1061 eth_hw_addr_set(netdev, addr);
1062
1063 return 0;
1064 }
1065
enic_set_mac_address_dynamic(struct net_device * netdev,void * p)1066 static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
1067 {
1068 struct enic *enic = netdev_priv(netdev);
1069 struct sockaddr *saddr = p;
1070 char *addr = saddr->sa_data;
1071 int err;
1072
1073 if (netif_running(enic->netdev)) {
1074 err = enic_dev_del_station_addr(enic);
1075 if (err)
1076 return err;
1077 }
1078
1079 err = enic_set_mac_addr(netdev, addr);
1080 if (err)
1081 return err;
1082
1083 if (netif_running(enic->netdev)) {
1084 err = enic_dev_add_station_addr(enic);
1085 if (err)
1086 return err;
1087 }
1088
1089 return err;
1090 }
1091
enic_set_mac_address(struct net_device * netdev,void * p)1092 static int enic_set_mac_address(struct net_device *netdev, void *p)
1093 {
1094 struct sockaddr *saddr = p;
1095 char *addr = saddr->sa_data;
1096 struct enic *enic = netdev_priv(netdev);
1097 int err;
1098
1099 err = enic_dev_del_station_addr(enic);
1100 if (err)
1101 return err;
1102
1103 err = enic_set_mac_addr(netdev, addr);
1104 if (err)
1105 return err;
1106
1107 return enic_dev_add_station_addr(enic);
1108 }
1109
1110 /* netif_tx_lock held, BHs disabled */
enic_set_rx_mode(struct net_device * netdev)1111 static void enic_set_rx_mode(struct net_device *netdev)
1112 {
1113 struct enic *enic = netdev_priv(netdev);
1114 int directed = 1;
1115 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1116 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1117 int promisc = (netdev->flags & IFF_PROMISC) ||
1118 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1119 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1120 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1121 unsigned int flags = netdev->flags |
1122 (allmulti ? IFF_ALLMULTI : 0) |
1123 (promisc ? IFF_PROMISC : 0);
1124
1125 if (enic->flags != flags) {
1126 enic->flags = flags;
1127 enic_dev_packet_filter(enic, directed,
1128 multicast, broadcast, promisc, allmulti);
1129 }
1130
1131 if (!promisc) {
1132 __dev_uc_sync(netdev, enic_uc_sync, enic_uc_unsync);
1133 if (!allmulti)
1134 __dev_mc_sync(netdev, enic_mc_sync, enic_mc_unsync);
1135 }
1136 }
1137
1138 /* netif_tx_lock held, BHs disabled */
enic_tx_timeout(struct net_device * netdev,unsigned int txqueue)1139 static void enic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1140 {
1141 struct enic *enic = netdev_priv(netdev);
1142 schedule_work(&enic->tx_hang_reset);
1143 }
1144
enic_set_vf_mac(struct net_device * netdev,int vf,u8 * mac)1145 static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1146 {
1147 struct enic *enic = netdev_priv(netdev);
1148 struct enic_port_profile *pp;
1149 int err;
1150
1151 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1152 if (err)
1153 return err;
1154
1155 if (is_valid_ether_addr(mac) || is_zero_ether_addr(mac)) {
1156 if (vf == PORT_SELF_VF) {
1157 memcpy(pp->vf_mac, mac, ETH_ALEN);
1158 return 0;
1159 } else {
1160 /*
1161 * For sriov vf's set the mac in hw
1162 */
1163 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
1164 vnic_dev_set_mac_addr, mac);
1165 return enic_dev_status_to_errno(err);
1166 }
1167 } else
1168 return -EINVAL;
1169 }
1170
enic_set_vf_port(struct net_device * netdev,int vf,struct nlattr * port[])1171 static int enic_set_vf_port(struct net_device *netdev, int vf,
1172 struct nlattr *port[])
1173 {
1174 static const u8 zero_addr[ETH_ALEN] = {};
1175 struct enic *enic = netdev_priv(netdev);
1176 struct enic_port_profile prev_pp;
1177 struct enic_port_profile *pp;
1178 int err = 0, restore_pp = 1;
1179
1180 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1181 if (err)
1182 return err;
1183
1184 if (!port[IFLA_PORT_REQUEST])
1185 return -EOPNOTSUPP;
1186
1187 memcpy(&prev_pp, pp, sizeof(*enic->pp));
1188 memset(pp, 0, sizeof(*enic->pp));
1189
1190 pp->set |= ENIC_SET_REQUEST;
1191 pp->request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1192
1193 if (port[IFLA_PORT_PROFILE]) {
1194 if (nla_len(port[IFLA_PORT_PROFILE]) != PORT_PROFILE_MAX) {
1195 memcpy(pp, &prev_pp, sizeof(*pp));
1196 return -EINVAL;
1197 }
1198 pp->set |= ENIC_SET_NAME;
1199 memcpy(pp->name, nla_data(port[IFLA_PORT_PROFILE]),
1200 PORT_PROFILE_MAX);
1201 }
1202
1203 if (port[IFLA_PORT_INSTANCE_UUID]) {
1204 if (nla_len(port[IFLA_PORT_INSTANCE_UUID]) != PORT_UUID_MAX) {
1205 memcpy(pp, &prev_pp, sizeof(*pp));
1206 return -EINVAL;
1207 }
1208 pp->set |= ENIC_SET_INSTANCE;
1209 memcpy(pp->instance_uuid,
1210 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1211 }
1212
1213 if (port[IFLA_PORT_HOST_UUID]) {
1214 if (nla_len(port[IFLA_PORT_HOST_UUID]) != PORT_UUID_MAX) {
1215 memcpy(pp, &prev_pp, sizeof(*pp));
1216 return -EINVAL;
1217 }
1218 pp->set |= ENIC_SET_HOST;
1219 memcpy(pp->host_uuid,
1220 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1221 }
1222
1223 if (vf == PORT_SELF_VF) {
1224 /* Special case handling: mac came from IFLA_VF_MAC */
1225 if (!is_zero_ether_addr(prev_pp.vf_mac))
1226 memcpy(pp->mac_addr, prev_pp.vf_mac, ETH_ALEN);
1227
1228 if (is_zero_ether_addr(netdev->dev_addr))
1229 eth_hw_addr_random(netdev);
1230 } else {
1231 /* SR-IOV VF: get mac from adapter */
1232 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
1233 vnic_dev_get_mac_addr, pp->mac_addr);
1234 if (err) {
1235 netdev_err(netdev, "Error getting mac for vf %d\n", vf);
1236 memcpy(pp, &prev_pp, sizeof(*pp));
1237 return enic_dev_status_to_errno(err);
1238 }
1239 }
1240
1241 err = enic_process_set_pp_request(enic, vf, &prev_pp, &restore_pp);
1242 if (err) {
1243 if (restore_pp) {
1244 /* Things are still the way they were: Implicit
1245 * DISASSOCIATE failed
1246 */
1247 memcpy(pp, &prev_pp, sizeof(*pp));
1248 } else {
1249 memset(pp, 0, sizeof(*pp));
1250 if (vf == PORT_SELF_VF)
1251 eth_hw_addr_set(netdev, zero_addr);
1252 }
1253 } else {
1254 /* Set flag to indicate that the port assoc/disassoc
1255 * request has been sent out to fw
1256 */
1257 pp->set |= ENIC_PORT_REQUEST_APPLIED;
1258
1259 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1260 if (pp->request == PORT_REQUEST_DISASSOCIATE) {
1261 eth_zero_addr(pp->mac_addr);
1262 if (vf == PORT_SELF_VF)
1263 eth_hw_addr_set(netdev, zero_addr);
1264 }
1265 }
1266
1267 if (vf == PORT_SELF_VF)
1268 eth_zero_addr(pp->vf_mac);
1269
1270 return err;
1271 }
1272
enic_get_vf_port(struct net_device * netdev,int vf,struct sk_buff * skb)1273 static int enic_get_vf_port(struct net_device *netdev, int vf,
1274 struct sk_buff *skb)
1275 {
1276 struct enic *enic = netdev_priv(netdev);
1277 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
1278 struct enic_port_profile *pp;
1279 int err;
1280
1281 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1282 if (err)
1283 return err;
1284
1285 if (!(pp->set & ENIC_PORT_REQUEST_APPLIED))
1286 return -ENODATA;
1287
1288 err = enic_process_get_pp_request(enic, vf, pp->request, &response);
1289 if (err)
1290 return err;
1291
1292 if (nla_put_u16(skb, IFLA_PORT_REQUEST, pp->request) ||
1293 nla_put_u16(skb, IFLA_PORT_RESPONSE, response) ||
1294 ((pp->set & ENIC_SET_NAME) &&
1295 nla_put(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX, pp->name)) ||
1296 ((pp->set & ENIC_SET_INSTANCE) &&
1297 nla_put(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1298 pp->instance_uuid)) ||
1299 ((pp->set & ENIC_SET_HOST) &&
1300 nla_put(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX, pp->host_uuid)))
1301 goto nla_put_failure;
1302 return 0;
1303
1304 nla_put_failure:
1305 return -EMSGSIZE;
1306 }
1307
enic_set_int_moderation(struct enic * enic,struct vnic_rq * rq)1308 static void enic_set_int_moderation(struct enic *enic, struct vnic_rq *rq)
1309 {
1310 unsigned int intr = enic_msix_rq_intr(enic, rq->index);
1311 struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1312 u32 timer = cq->tobe_rx_coal_timeval;
1313
1314 if (cq->tobe_rx_coal_timeval != cq->cur_rx_coal_timeval) {
1315 vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
1316 cq->cur_rx_coal_timeval = cq->tobe_rx_coal_timeval;
1317 }
1318 }
1319
enic_calc_int_moderation(struct enic * enic,struct vnic_rq * rq)1320 static void enic_calc_int_moderation(struct enic *enic, struct vnic_rq *rq)
1321 {
1322 struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1323 struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1324 struct vnic_rx_bytes_counter *pkt_size_counter = &cq->pkt_size_counter;
1325 int index;
1326 u32 timer;
1327 u32 range_start;
1328 u32 traffic;
1329 u64 delta;
1330 ktime_t now = ktime_get();
1331
1332 delta = ktime_us_delta(now, cq->prev_ts);
1333 if (delta < ENIC_AIC_TS_BREAK)
1334 return;
1335 cq->prev_ts = now;
1336
1337 traffic = pkt_size_counter->large_pkt_bytes_cnt +
1338 pkt_size_counter->small_pkt_bytes_cnt;
1339 /* The table takes Mbps
1340 * traffic *= 8 => bits
1341 * traffic *= (10^6 / delta) => bps
1342 * traffic /= 10^6 => Mbps
1343 *
1344 * Combining, traffic *= (8 / delta)
1345 */
1346
1347 traffic <<= 3;
1348 traffic = delta > UINT_MAX ? 0 : traffic / (u32)delta;
1349
1350 for (index = 0; index < ENIC_MAX_COALESCE_TIMERS; index++)
1351 if (traffic < mod_table[index].rx_rate)
1352 break;
1353 range_start = (pkt_size_counter->small_pkt_bytes_cnt >
1354 pkt_size_counter->large_pkt_bytes_cnt << 1) ?
1355 rx_coal->small_pkt_range_start :
1356 rx_coal->large_pkt_range_start;
1357 timer = range_start + ((rx_coal->range_end - range_start) *
1358 mod_table[index].range_percent / 100);
1359 /* Damping */
1360 cq->tobe_rx_coal_timeval = (timer + cq->tobe_rx_coal_timeval) >> 1;
1361
1362 pkt_size_counter->large_pkt_bytes_cnt = 0;
1363 pkt_size_counter->small_pkt_bytes_cnt = 0;
1364 }
1365
enic_poll(struct napi_struct * napi,int budget)1366 static int enic_poll(struct napi_struct *napi, int budget)
1367 {
1368 struct net_device *netdev = napi->dev;
1369 struct enic *enic = netdev_priv(netdev);
1370 unsigned int cq_rq = enic_cq_rq(enic, 0);
1371 unsigned int cq_wq = enic_cq_wq(enic, 0);
1372 unsigned int intr = ENIC_LEGACY_IO_INTR;
1373 unsigned int rq_work_to_do = budget;
1374 unsigned int wq_work_to_do = ENIC_WQ_NAPI_BUDGET;
1375 unsigned int work_done, rq_work_done = 0, wq_work_done;
1376 int err;
1377
1378 wq_work_done = enic_wq_cq_service(enic, cq_wq, wq_work_to_do);
1379
1380 if (budget > 0)
1381 rq_work_done = enic_rq_cq_service(enic, cq_rq, rq_work_to_do);
1382
1383 /* Accumulate intr event credits for this polling
1384 * cycle. An intr event is the completion of a
1385 * a WQ or RQ packet.
1386 */
1387
1388 work_done = rq_work_done + wq_work_done;
1389
1390 if (work_done > 0)
1391 vnic_intr_return_credits(&enic->intr[intr],
1392 work_done,
1393 0 /* don't unmask intr */,
1394 0 /* don't reset intr timer */);
1395
1396 err = vnic_rq_fill(&enic->rq[0].vrq, enic_rq_alloc_buf);
1397
1398 /* Buffer allocation failed. Stay in polling
1399 * mode so we can try to fill the ring again.
1400 */
1401
1402 if (err)
1403 rq_work_done = rq_work_to_do;
1404 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1405 /* Call the function which refreshes the intr coalescing timer
1406 * value based on the traffic.
1407 */
1408 enic_calc_int_moderation(enic, &enic->rq[0].vrq);
1409
1410 if ((rq_work_done < budget) && napi_complete_done(napi, rq_work_done)) {
1411
1412 /* Some work done, but not enough to stay in polling,
1413 * exit polling
1414 */
1415
1416 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1417 enic_set_int_moderation(enic, &enic->rq[0].vrq);
1418 vnic_intr_unmask(&enic->intr[intr]);
1419 enic->rq[0].stats.napi_complete++;
1420 } else {
1421 enic->rq[0].stats.napi_repoll++;
1422 }
1423
1424 return rq_work_done;
1425 }
1426
1427 #ifdef CONFIG_RFS_ACCEL
enic_free_rx_cpu_rmap(struct enic * enic)1428 static void enic_free_rx_cpu_rmap(struct enic *enic)
1429 {
1430 free_irq_cpu_rmap(enic->netdev->rx_cpu_rmap);
1431 enic->netdev->rx_cpu_rmap = NULL;
1432 }
1433
enic_set_rx_cpu_rmap(struct enic * enic)1434 static void enic_set_rx_cpu_rmap(struct enic *enic)
1435 {
1436 int i, res;
1437
1438 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) {
1439 enic->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(enic->rq_count);
1440 if (unlikely(!enic->netdev->rx_cpu_rmap))
1441 return;
1442 for (i = 0; i < enic->rq_count; i++) {
1443 res = irq_cpu_rmap_add(enic->netdev->rx_cpu_rmap,
1444 enic->msix_entry[i].vector);
1445 if (unlikely(res)) {
1446 enic_free_rx_cpu_rmap(enic);
1447 return;
1448 }
1449 }
1450 }
1451 }
1452
1453 #else
1454
enic_free_rx_cpu_rmap(struct enic * enic)1455 static void enic_free_rx_cpu_rmap(struct enic *enic)
1456 {
1457 }
1458
enic_set_rx_cpu_rmap(struct enic * enic)1459 static void enic_set_rx_cpu_rmap(struct enic *enic)
1460 {
1461 }
1462
1463 #endif /* CONFIG_RFS_ACCEL */
1464
enic_poll_msix_wq(struct napi_struct * napi,int budget)1465 static int enic_poll_msix_wq(struct napi_struct *napi, int budget)
1466 {
1467 struct net_device *netdev = napi->dev;
1468 struct enic *enic = netdev_priv(netdev);
1469 unsigned int wq_index = (napi - &enic->napi[0]) - enic->rq_count;
1470 struct vnic_wq *wq = &enic->wq[wq_index].vwq;
1471 unsigned int cq;
1472 unsigned int intr;
1473 unsigned int wq_work_to_do = ENIC_WQ_NAPI_BUDGET;
1474 unsigned int wq_work_done;
1475 unsigned int wq_irq;
1476
1477 wq_irq = wq->index;
1478 cq = enic_cq_wq(enic, wq_irq);
1479 intr = enic_msix_wq_intr(enic, wq_irq);
1480
1481 wq_work_done = enic_wq_cq_service(enic, cq, wq_work_to_do);
1482
1483 vnic_intr_return_credits(&enic->intr[intr], wq_work_done,
1484 0 /* don't unmask intr */,
1485 1 /* reset intr timer */);
1486 if (!wq_work_done) {
1487 napi_complete(napi);
1488 vnic_intr_unmask(&enic->intr[intr]);
1489 return 0;
1490 }
1491
1492 return budget;
1493 }
1494
enic_poll_msix_rq(struct napi_struct * napi,int budget)1495 static int enic_poll_msix_rq(struct napi_struct *napi, int budget)
1496 {
1497 struct net_device *netdev = napi->dev;
1498 struct enic *enic = netdev_priv(netdev);
1499 unsigned int rq = (napi - &enic->napi[0]);
1500 unsigned int cq = enic_cq_rq(enic, rq);
1501 unsigned int intr = enic_msix_rq_intr(enic, rq);
1502 unsigned int work_to_do = budget;
1503 unsigned int work_done = 0;
1504 int err;
1505
1506 /* Service RQ
1507 */
1508
1509 if (budget > 0)
1510 work_done = enic_rq_cq_service(enic, cq, work_to_do);
1511
1512 /* Return intr event credits for this polling
1513 * cycle. An intr event is the completion of a
1514 * RQ packet.
1515 */
1516
1517 if (work_done > 0)
1518 vnic_intr_return_credits(&enic->intr[intr],
1519 work_done,
1520 0 /* don't unmask intr */,
1521 0 /* don't reset intr timer */);
1522
1523 err = vnic_rq_fill(&enic->rq[rq].vrq, enic_rq_alloc_buf);
1524
1525 /* Buffer allocation failed. Stay in polling mode
1526 * so we can try to fill the ring again.
1527 */
1528
1529 if (err)
1530 work_done = work_to_do;
1531 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1532 /* Call the function which refreshes the intr coalescing timer
1533 * value based on the traffic.
1534 */
1535 enic_calc_int_moderation(enic, &enic->rq[rq].vrq);
1536
1537 if ((work_done < budget) && napi_complete_done(napi, work_done)) {
1538
1539 /* Some work done, but not enough to stay in polling,
1540 * exit polling
1541 */
1542
1543 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1544 enic_set_int_moderation(enic, &enic->rq[rq].vrq);
1545 vnic_intr_unmask(&enic->intr[intr]);
1546 enic->rq[rq].stats.napi_complete++;
1547 } else {
1548 enic->rq[rq].stats.napi_repoll++;
1549 }
1550
1551 return work_done;
1552 }
1553
enic_notify_timer(struct timer_list * t)1554 static void enic_notify_timer(struct timer_list *t)
1555 {
1556 struct enic *enic = timer_container_of(enic, t, notify_timer);
1557
1558 enic_notify_check(enic);
1559
1560 mod_timer(&enic->notify_timer,
1561 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1562 }
1563
enic_free_intr(struct enic * enic)1564 static void enic_free_intr(struct enic *enic)
1565 {
1566 struct net_device *netdev = enic->netdev;
1567 unsigned int i;
1568
1569 enic_free_rx_cpu_rmap(enic);
1570 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1571 case VNIC_DEV_INTR_MODE_INTX:
1572 free_irq(enic->pdev->irq, netdev);
1573 break;
1574 case VNIC_DEV_INTR_MODE_MSI:
1575 free_irq(enic->pdev->irq, enic);
1576 break;
1577 case VNIC_DEV_INTR_MODE_MSIX:
1578 for (i = 0; i < enic->intr_count; i++)
1579 if (enic->msix[i].requested)
1580 free_irq(enic->msix_entry[i].vector,
1581 enic->msix[i].devid);
1582 break;
1583 default:
1584 break;
1585 }
1586 }
1587
enic_request_intr(struct enic * enic)1588 static int enic_request_intr(struct enic *enic)
1589 {
1590 struct net_device *netdev = enic->netdev;
1591 unsigned int i, intr;
1592 int err = 0;
1593
1594 enic_set_rx_cpu_rmap(enic);
1595 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1596
1597 case VNIC_DEV_INTR_MODE_INTX:
1598
1599 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1600 IRQF_SHARED, netdev->name, netdev);
1601 break;
1602
1603 case VNIC_DEV_INTR_MODE_MSI:
1604
1605 err = request_irq(enic->pdev->irq, enic_isr_msi,
1606 0, netdev->name, enic);
1607 break;
1608
1609 case VNIC_DEV_INTR_MODE_MSIX:
1610
1611 for (i = 0; i < enic->rq_count; i++) {
1612 intr = enic_msix_rq_intr(enic, i);
1613 snprintf(enic->msix[intr].devname,
1614 sizeof(enic->msix[intr].devname),
1615 "%s-rx-%u", netdev->name, i);
1616 enic->msix[intr].isr = enic_isr_msix;
1617 enic->msix[intr].devid = &enic->napi[i];
1618 }
1619
1620 for (i = 0; i < enic->wq_count; i++) {
1621 int wq = enic_cq_wq(enic, i);
1622
1623 intr = enic_msix_wq_intr(enic, i);
1624 snprintf(enic->msix[intr].devname,
1625 sizeof(enic->msix[intr].devname),
1626 "%s-tx-%u", netdev->name, i);
1627 enic->msix[intr].isr = enic_isr_msix;
1628 enic->msix[intr].devid = &enic->napi[wq];
1629 }
1630
1631 intr = enic_msix_err_intr(enic);
1632 snprintf(enic->msix[intr].devname,
1633 sizeof(enic->msix[intr].devname),
1634 "%s-err", netdev->name);
1635 enic->msix[intr].isr = enic_isr_msix_err;
1636 enic->msix[intr].devid = enic;
1637
1638 intr = enic_msix_notify_intr(enic);
1639 snprintf(enic->msix[intr].devname,
1640 sizeof(enic->msix[intr].devname),
1641 "%s-notify", netdev->name);
1642 enic->msix[intr].isr = enic_isr_msix_notify;
1643 enic->msix[intr].devid = enic;
1644
1645 for (i = 0; i < enic->intr_count; i++)
1646 enic->msix[i].requested = 0;
1647
1648 for (i = 0; i < enic->intr_count; i++) {
1649 err = request_irq(enic->msix_entry[i].vector,
1650 enic->msix[i].isr, 0,
1651 enic->msix[i].devname,
1652 enic->msix[i].devid);
1653 if (err) {
1654 enic_free_intr(enic);
1655 break;
1656 }
1657 enic->msix[i].requested = 1;
1658 }
1659
1660 break;
1661
1662 default:
1663 break;
1664 }
1665
1666 return err;
1667 }
1668
enic_synchronize_irqs(struct enic * enic)1669 static void enic_synchronize_irqs(struct enic *enic)
1670 {
1671 unsigned int i;
1672
1673 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1674 case VNIC_DEV_INTR_MODE_INTX:
1675 case VNIC_DEV_INTR_MODE_MSI:
1676 synchronize_irq(enic->pdev->irq);
1677 break;
1678 case VNIC_DEV_INTR_MODE_MSIX:
1679 for (i = 0; i < enic->intr_count; i++)
1680 synchronize_irq(enic->msix_entry[i].vector);
1681 break;
1682 default:
1683 break;
1684 }
1685 }
1686
enic_dev_notify_set(struct enic * enic)1687 static int enic_dev_notify_set(struct enic *enic)
1688 {
1689 int err;
1690
1691 spin_lock_bh(&enic->devcmd_lock);
1692 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1693 case VNIC_DEV_INTR_MODE_INTX:
1694 err = vnic_dev_notify_set(enic->vdev, ENIC_LEGACY_NOTIFY_INTR);
1695 break;
1696 case VNIC_DEV_INTR_MODE_MSIX:
1697 err = vnic_dev_notify_set(enic->vdev,
1698 enic_msix_notify_intr(enic));
1699 break;
1700 default:
1701 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1702 break;
1703 }
1704 spin_unlock_bh(&enic->devcmd_lock);
1705
1706 return err;
1707 }
1708
enic_notify_timer_start(struct enic * enic)1709 static void enic_notify_timer_start(struct enic *enic)
1710 {
1711 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1712 case VNIC_DEV_INTR_MODE_MSI:
1713 mod_timer(&enic->notify_timer, jiffies);
1714 break;
1715 default:
1716 /* Using intr for notification for INTx/MSI-X */
1717 break;
1718 }
1719 }
1720
1721 /* rtnl lock is held, process context */
enic_open(struct net_device * netdev)1722 static int enic_open(struct net_device *netdev)
1723 {
1724 struct enic *enic = netdev_priv(netdev);
1725 unsigned int i;
1726 int err, ret;
1727 unsigned int max_pkt_len = netdev->mtu + VLAN_ETH_HLEN;
1728 struct page_pool_params pp_params = {
1729 .order = get_order(max_pkt_len),
1730 .pool_size = enic->config.rq_desc_count,
1731 .nid = dev_to_node(&enic->pdev->dev),
1732 .dev = &enic->pdev->dev,
1733 .dma_dir = DMA_FROM_DEVICE,
1734 .max_len = (max_pkt_len > PAGE_SIZE) ? max_pkt_len : PAGE_SIZE,
1735 .netdev = netdev,
1736 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
1737 };
1738
1739 err = enic_request_intr(enic);
1740 if (err) {
1741 netdev_err(netdev, "Unable to request irq.\n");
1742 return err;
1743 }
1744 enic_init_affinity_hint(enic);
1745 enic_set_affinity_hint(enic);
1746
1747 err = enic_dev_notify_set(enic);
1748 if (err) {
1749 netdev_err(netdev,
1750 "Failed to alloc notify buffer, aborting.\n");
1751 goto err_out_free_intr;
1752 }
1753
1754 for (i = 0; i < enic->rq_count; i++) {
1755 /* create a page pool for each RQ */
1756 pp_params.napi = &enic->napi[i];
1757 pp_params.queue_idx = i;
1758 enic->rq[i].pool = page_pool_create(&pp_params);
1759 if (IS_ERR(enic->rq[i].pool)) {
1760 err = PTR_ERR(enic->rq[i].pool);
1761 enic->rq[i].pool = NULL;
1762 goto err_out_free_rq;
1763 }
1764
1765 /* enable rq before updating rq desc */
1766 vnic_rq_enable(&enic->rq[i].vrq);
1767 vnic_rq_fill(&enic->rq[i].vrq, enic_rq_alloc_buf);
1768 /* Need at least one buffer on ring to get going */
1769 if (vnic_rq_desc_used(&enic->rq[i].vrq) == 0) {
1770 netdev_err(netdev, "Unable to alloc receive buffers\n");
1771 err = -ENOMEM;
1772 goto err_out_free_rq;
1773 }
1774 }
1775
1776 for (i = 0; i < enic->wq_count; i++)
1777 vnic_wq_enable(&enic->wq[i].vwq);
1778
1779 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1780 enic_dev_add_station_addr(enic);
1781
1782 enic_set_rx_mode(netdev);
1783
1784 netif_tx_wake_all_queues(netdev);
1785
1786 for (i = 0; i < enic->rq_count; i++)
1787 napi_enable(&enic->napi[i]);
1788
1789 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
1790 for (i = 0; i < enic->wq_count; i++)
1791 napi_enable(&enic->napi[enic_cq_wq(enic, i)]);
1792 err = enic_dev_enable(enic);
1793 if (err) {
1794 netdev_err(netdev, "Failed to enable device: %d\n", err);
1795 goto err_out_dev_enable;
1796 }
1797
1798 for (i = 0; i < enic->intr_count; i++)
1799 vnic_intr_unmask(&enic->intr[i]);
1800
1801 enic_notify_timer_start(enic);
1802 enic_rfs_timer_start(enic);
1803
1804 return 0;
1805
1806 err_out_dev_enable:
1807 for (i = 0; i < enic->rq_count; i++)
1808 napi_disable(&enic->napi[i]);
1809 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
1810 for (i = 0; i < enic->wq_count; i++)
1811 napi_disable(&enic->napi[enic_cq_wq(enic, i)]);
1812 netif_tx_disable(netdev);
1813 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1814 enic_dev_del_station_addr(enic);
1815 for (i = 0; i < enic->wq_count; i++)
1816 vnic_wq_disable(&enic->wq[i].vwq);
1817 err_out_free_rq:
1818 for (i = 0; i < enic->rq_count; i++) {
1819 ret = vnic_rq_disable(&enic->rq[i].vrq);
1820 if (!ret) {
1821 vnic_rq_clean(&enic->rq[i].vrq, enic_free_rq_buf);
1822 page_pool_destroy(enic->rq[i].pool);
1823 enic->rq[i].pool = NULL;
1824 }
1825 }
1826 enic_dev_notify_unset(enic);
1827 err_out_free_intr:
1828 enic_unset_affinity_hint(enic);
1829 enic_free_intr(enic);
1830
1831 return err;
1832 }
1833
1834 /* rtnl lock is held, process context */
enic_stop(struct net_device * netdev)1835 static int enic_stop(struct net_device *netdev)
1836 {
1837 struct enic *enic = netdev_priv(netdev);
1838 unsigned int i;
1839 int err;
1840
1841 for (i = 0; i < enic->intr_count; i++) {
1842 vnic_intr_mask(&enic->intr[i]);
1843 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1844 }
1845
1846 enic_synchronize_irqs(enic);
1847
1848 timer_delete_sync(&enic->notify_timer);
1849 enic_rfs_flw_tbl_free(enic);
1850
1851 enic_dev_disable(enic);
1852
1853 for (i = 0; i < enic->rq_count; i++)
1854 napi_disable(&enic->napi[i]);
1855
1856 netif_carrier_off(netdev);
1857 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
1858 for (i = 0; i < enic->wq_count; i++)
1859 napi_disable(&enic->napi[enic_cq_wq(enic, i)]);
1860 netif_tx_disable(netdev);
1861
1862 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1863 enic_dev_del_station_addr(enic);
1864
1865 for (i = 0; i < enic->wq_count; i++) {
1866 err = vnic_wq_disable(&enic->wq[i].vwq);
1867 if (err)
1868 return err;
1869 }
1870 for (i = 0; i < enic->rq_count; i++) {
1871 err = vnic_rq_disable(&enic->rq[i].vrq);
1872 if (err)
1873 return err;
1874 }
1875
1876 enic_dev_notify_unset(enic);
1877 enic_unset_affinity_hint(enic);
1878 enic_free_intr(enic);
1879
1880 for (i = 0; i < enic->wq_count; i++)
1881 vnic_wq_clean(&enic->wq[i].vwq, enic_free_wq_buf);
1882 for (i = 0; i < enic->rq_count; i++) {
1883 vnic_rq_clean(&enic->rq[i].vrq, enic_free_rq_buf);
1884 page_pool_destroy(enic->rq[i].pool);
1885 enic->rq[i].pool = NULL;
1886 }
1887 for (i = 0; i < enic->cq_count; i++)
1888 vnic_cq_clean(&enic->cq[i]);
1889 for (i = 0; i < enic->intr_count; i++)
1890 vnic_intr_clean(&enic->intr[i]);
1891
1892 return 0;
1893 }
1894
_enic_change_mtu(struct net_device * netdev,int new_mtu)1895 static int _enic_change_mtu(struct net_device *netdev, int new_mtu)
1896 {
1897 bool running = netif_running(netdev);
1898 int err = 0;
1899
1900 ASSERT_RTNL();
1901 if (running) {
1902 err = enic_stop(netdev);
1903 if (err)
1904 return err;
1905 }
1906
1907 WRITE_ONCE(netdev->mtu, new_mtu);
1908
1909 if (running) {
1910 err = enic_open(netdev);
1911 if (err)
1912 return err;
1913 }
1914
1915 return 0;
1916 }
1917
enic_change_mtu(struct net_device * netdev,int new_mtu)1918 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1919 {
1920 struct enic *enic = netdev_priv(netdev);
1921
1922 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
1923 return -EOPNOTSUPP;
1924
1925 if (new_mtu > enic->port_mtu)
1926 netdev_warn(netdev,
1927 "interface MTU (%d) set higher than port MTU (%d)\n",
1928 new_mtu, enic->port_mtu);
1929
1930 return _enic_change_mtu(netdev, new_mtu);
1931 }
1932
enic_change_mtu_work(struct work_struct * work)1933 static void enic_change_mtu_work(struct work_struct *work)
1934 {
1935 struct enic *enic = container_of(work, struct enic, change_mtu_work);
1936 struct net_device *netdev = enic->netdev;
1937 int new_mtu = vnic_dev_mtu(enic->vdev);
1938
1939 rtnl_lock();
1940 (void)_enic_change_mtu(netdev, new_mtu);
1941 rtnl_unlock();
1942
1943 netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1944 }
1945
1946 #ifdef CONFIG_NET_POLL_CONTROLLER
enic_poll_controller(struct net_device * netdev)1947 static void enic_poll_controller(struct net_device *netdev)
1948 {
1949 struct enic *enic = netdev_priv(netdev);
1950 struct vnic_dev *vdev = enic->vdev;
1951 unsigned int i, intr;
1952
1953 switch (vnic_dev_get_intr_mode(vdev)) {
1954 case VNIC_DEV_INTR_MODE_MSIX:
1955 for (i = 0; i < enic->rq_count; i++) {
1956 intr = enic_msix_rq_intr(enic, i);
1957 enic_isr_msix(enic->msix_entry[intr].vector,
1958 &enic->napi[i]);
1959 }
1960
1961 for (i = 0; i < enic->wq_count; i++) {
1962 intr = enic_msix_wq_intr(enic, i);
1963 enic_isr_msix(enic->msix_entry[intr].vector,
1964 &enic->napi[enic_cq_wq(enic, i)]);
1965 }
1966
1967 break;
1968 case VNIC_DEV_INTR_MODE_MSI:
1969 enic_isr_msi(enic->pdev->irq, enic);
1970 break;
1971 case VNIC_DEV_INTR_MODE_INTX:
1972 enic_isr_legacy(enic->pdev->irq, netdev);
1973 break;
1974 default:
1975 break;
1976 }
1977 }
1978 #endif
1979
enic_dev_wait(struct vnic_dev * vdev,int (* start)(struct vnic_dev *,int),int (* finished)(struct vnic_dev *,int *),int arg)1980 static int enic_dev_wait(struct vnic_dev *vdev,
1981 int (*start)(struct vnic_dev *, int),
1982 int (*finished)(struct vnic_dev *, int *),
1983 int arg)
1984 {
1985 unsigned long time;
1986 int done;
1987 int err;
1988
1989 err = start(vdev, arg);
1990 if (err)
1991 return err;
1992
1993 /* Wait for func to complete...2 seconds max
1994 */
1995
1996 time = jiffies + (HZ * 2);
1997 do {
1998
1999 err = finished(vdev, &done);
2000 if (err)
2001 return err;
2002
2003 if (done)
2004 return 0;
2005
2006 schedule_timeout_uninterruptible(HZ / 10);
2007
2008 } while (time_after(time, jiffies));
2009
2010 return -ETIMEDOUT;
2011 }
2012
enic_dev_open(struct enic * enic)2013 static int enic_dev_open(struct enic *enic)
2014 {
2015 int err;
2016 u32 flags = CMD_OPENF_IG_DESCCACHE;
2017
2018 err = enic_dev_wait(enic->vdev, vnic_dev_open,
2019 vnic_dev_open_done, flags);
2020 if (err)
2021 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
2022 err);
2023
2024 return err;
2025 }
2026
enic_dev_soft_reset(struct enic * enic)2027 static int enic_dev_soft_reset(struct enic *enic)
2028 {
2029 int err;
2030
2031 err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset,
2032 vnic_dev_soft_reset_done, 0);
2033 if (err)
2034 netdev_err(enic->netdev, "vNIC soft reset failed, err %d\n",
2035 err);
2036
2037 return err;
2038 }
2039
enic_dev_hang_reset(struct enic * enic)2040 static int enic_dev_hang_reset(struct enic *enic)
2041 {
2042 int err;
2043
2044 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
2045 vnic_dev_hang_reset_done, 0);
2046 if (err)
2047 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
2048 err);
2049
2050 return err;
2051 }
2052
__enic_set_rsskey(struct enic * enic)2053 int __enic_set_rsskey(struct enic *enic)
2054 {
2055 union vnic_rss_key *rss_key_buf_va;
2056 dma_addr_t rss_key_buf_pa;
2057 int i, kidx, bidx, err;
2058
2059 rss_key_buf_va = dma_alloc_coherent(&enic->pdev->dev,
2060 sizeof(union vnic_rss_key),
2061 &rss_key_buf_pa, GFP_ATOMIC);
2062 if (!rss_key_buf_va)
2063 return -ENOMEM;
2064
2065 for (i = 0; i < ENIC_RSS_LEN; i++) {
2066 kidx = i / ENIC_RSS_BYTES_PER_KEY;
2067 bidx = i % ENIC_RSS_BYTES_PER_KEY;
2068 rss_key_buf_va->key[kidx].b[bidx] = enic->rss_key[i];
2069 }
2070 spin_lock_bh(&enic->devcmd_lock);
2071 err = enic_set_rss_key(enic,
2072 rss_key_buf_pa,
2073 sizeof(union vnic_rss_key));
2074 spin_unlock_bh(&enic->devcmd_lock);
2075
2076 dma_free_coherent(&enic->pdev->dev, sizeof(union vnic_rss_key),
2077 rss_key_buf_va, rss_key_buf_pa);
2078
2079 return err;
2080 }
2081
enic_set_rsskey(struct enic * enic)2082 static int enic_set_rsskey(struct enic *enic)
2083 {
2084 netdev_rss_key_fill(enic->rss_key, ENIC_RSS_LEN);
2085
2086 return __enic_set_rsskey(enic);
2087 }
2088
enic_set_rsscpu(struct enic * enic,u8 rss_hash_bits)2089 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
2090 {
2091 dma_addr_t rss_cpu_buf_pa;
2092 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
2093 unsigned int i;
2094 int err;
2095
2096 rss_cpu_buf_va = dma_alloc_coherent(&enic->pdev->dev,
2097 sizeof(union vnic_rss_cpu),
2098 &rss_cpu_buf_pa, GFP_ATOMIC);
2099 if (!rss_cpu_buf_va)
2100 return -ENOMEM;
2101
2102 for (i = 0; i < (1 << rss_hash_bits); i++)
2103 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
2104
2105 spin_lock_bh(&enic->devcmd_lock);
2106 err = enic_set_rss_cpu(enic,
2107 rss_cpu_buf_pa,
2108 sizeof(union vnic_rss_cpu));
2109 spin_unlock_bh(&enic->devcmd_lock);
2110
2111 dma_free_coherent(&enic->pdev->dev, sizeof(union vnic_rss_cpu),
2112 rss_cpu_buf_va, rss_cpu_buf_pa);
2113
2114 return err;
2115 }
2116
enic_set_niccfg(struct enic * enic,u8 rss_default_cpu,u8 rss_hash_type,u8 rss_hash_bits,u8 rss_base_cpu,u8 rss_enable)2117 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
2118 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
2119 {
2120 const u8 tso_ipid_split_en = 0;
2121 const u8 ig_vlan_strip_en = 1;
2122 int err;
2123
2124 /* Enable VLAN tag stripping.
2125 */
2126
2127 spin_lock_bh(&enic->devcmd_lock);
2128 err = enic_set_nic_cfg(enic,
2129 rss_default_cpu, rss_hash_type,
2130 rss_hash_bits, rss_base_cpu,
2131 rss_enable, tso_ipid_split_en,
2132 ig_vlan_strip_en);
2133 spin_unlock_bh(&enic->devcmd_lock);
2134
2135 return err;
2136 }
2137
enic_set_rss_nic_cfg(struct enic * enic)2138 static int enic_set_rss_nic_cfg(struct enic *enic)
2139 {
2140 struct device *dev = enic_get_dev(enic);
2141 const u8 rss_default_cpu = 0;
2142 const u8 rss_hash_bits = 7;
2143 const u8 rss_base_cpu = 0;
2144 u8 rss_hash_type;
2145 int res;
2146 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
2147
2148 spin_lock_bh(&enic->devcmd_lock);
2149 res = vnic_dev_capable_rss_hash_type(enic->vdev, &rss_hash_type);
2150 spin_unlock_bh(&enic->devcmd_lock);
2151 if (res) {
2152 /* defaults for old adapters
2153 */
2154 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
2155 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
2156 NIC_CFG_RSS_HASH_TYPE_IPV6 |
2157 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
2158 }
2159
2160 if (rss_enable) {
2161 if (!enic_set_rsskey(enic)) {
2162 if (enic_set_rsscpu(enic, rss_hash_bits)) {
2163 rss_enable = 0;
2164 dev_warn(dev, "RSS disabled, "
2165 "Failed to set RSS cpu indirection table.");
2166 }
2167 } else {
2168 rss_enable = 0;
2169 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
2170 }
2171 }
2172
2173 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
2174 rss_hash_bits, rss_base_cpu, rss_enable);
2175 }
2176
enic_set_api_busy(struct enic * enic,bool busy)2177 static void enic_set_api_busy(struct enic *enic, bool busy)
2178 {
2179 spin_lock(&enic->enic_api_lock);
2180 enic->enic_api_busy = busy;
2181 spin_unlock(&enic->enic_api_lock);
2182 }
2183
2184 /* The admin/MBOX channel exists on a V2 PF while SR-IOV is enabled and on
2185 * every V2 VF. A reset wipes the admin WQ/RQ/CQ, so such devices must tear
2186 * the channel down before the reset and re-establish it afterwards.
2187 */
enic_has_admin_chan(struct enic * enic)2188 static bool enic_has_admin_chan(struct enic *enic)
2189 {
2190 return enic_is_sriov_vf_v2(enic) ||
2191 (enic_sriov_enabled(enic) && enic->vf_type == ENIC_VF_TYPE_V2);
2192 }
2193
2194 /* Re-establish the admin/MBOX channel after a reset has re-created the data
2195 * path. Mirrors the relevant part of the probe / SR-IOV-enable sequence:
2196 * reinitialise MBOX and reopen the channel, then for a VF re-run the PF
2197 * handshake (the reset wiped the VF's admin QP, so the VF must register
2198 * again), or for a PF re-push the current link state to registered VFs.
2199 */
enic_admin_chan_reopen(struct enic * enic)2200 static void enic_admin_chan_reopen(struct enic *enic)
2201 {
2202 int err;
2203
2204 /* Install the MBOX receive handler and reset the sequence number
2205 * before opening the channel, so the handler is in place before the
2206 * admin interrupt is unmasked and no early completion is dropped.
2207 */
2208 enic_mbox_init(enic);
2209
2210 /* A reset destroys the VF's local admin QP, so the VF can no longer
2211 * rely on its previous registration. The PF may retain stale software
2212 * registration state until the VF successfully registers again.
2213 * Clear the local flag before reopening so a failed reopen or
2214 * re-handshake cannot leave the VF believing it has a usable PF
2215 * registration over a dead channel.
2216 */
2217 if (enic_is_sriov_vf_v2(enic))
2218 enic->vf_registered = false;
2219
2220 err = enic_admin_channel_open(enic);
2221 if (err) {
2222 netdev_err(enic->netdev,
2223 "admin channel reopen after reset failed: %d\n", err);
2224 return;
2225 }
2226
2227 if (enic_is_sriov_vf_v2(enic)) {
2228 err = enic_mbox_vf_capability_check(enic);
2229 if (err) {
2230 netdev_err(enic->netdev,
2231 "MBOX capability check after reset failed: %d\n",
2232 err);
2233 enic_admin_channel_close(enic);
2234 return;
2235 }
2236 err = enic_mbox_vf_register(enic);
2237 if (err) {
2238 netdev_err(enic->netdev,
2239 "MBOX VF re-registration after reset failed: %d\n",
2240 err);
2241 enic_admin_channel_close(enic);
2242 }
2243 } else {
2244 /* The link came back up during enic_open() above while MBOX
2245 * sends were still disabled (channel not yet reopened), so that
2246 * link-notify was dropped. Re-push current link state now.
2247 */
2248 schedule_work(&enic->link_notify_work);
2249 }
2250 }
2251
enic_reset(struct work_struct * work)2252 static void enic_reset(struct work_struct *work)
2253 {
2254 struct enic *enic = container_of(work, struct enic, reset);
2255
2256 if (!netif_running(enic->netdev))
2257 return;
2258
2259 rtnl_lock();
2260
2261 /* Stop any activity from infiniband */
2262 enic_set_api_busy(enic, true);
2263
2264 /* Fully tear down the V2 admin/MBOX channel before the soft reset.
2265 * The reset wipes all hardware queues including the admin WQ/RQ;
2266 * closing first tells firmware to stop the admin QP (so it no longer
2267 * DMAs from the about-to-be-reset rings) and frees the admin resources
2268 * so they are cleanly re-allocated afterwards.
2269 */
2270 if (enic_has_admin_chan(enic))
2271 enic_admin_channel_close(enic);
2272
2273 enic_stop(enic->netdev);
2274
2275 enic_dev_soft_reset(enic);
2276 enic_reset_addr_lists(enic);
2277 enic_init_vnic_resources(enic);
2278 enic_set_rss_nic_cfg(enic);
2279 enic_dev_set_ig_vlan_rewrite_mode(enic);
2280 enic_ext_cq(enic);
2281
2282 enic_open(enic->netdev);
2283
2284 /* Re-establish the admin/MBOX channel after the data path is back up.
2285 * It was fully torn down by enic_admin_channel_close() above;
2286 * enic_admin_chan_reopen() reopens it and, for a PF re-pushes link
2287 * state, or for a VF re-runs the probe-time PF handshake.
2288 */
2289 if (enic_has_admin_chan(enic))
2290 enic_admin_chan_reopen(enic);
2291
2292 /* Allow infiniband to fiddle with the device again */
2293 enic_set_api_busy(enic, false);
2294
2295 call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
2296
2297 rtnl_unlock();
2298 }
2299
enic_tx_hang_reset(struct work_struct * work)2300 static void enic_tx_hang_reset(struct work_struct *work)
2301 {
2302 struct enic *enic = container_of(work, struct enic, tx_hang_reset);
2303
2304 rtnl_lock();
2305
2306 /* Stop any activity from infiniband */
2307 enic_set_api_busy(enic, true);
2308
2309 /* Fully tear down the V2 admin/MBOX channel before the hang reset, for
2310 * the same reason as the soft reset path: stop the admin QP and free
2311 * the admin resources before the hardware queues are wiped.
2312 */
2313 if (enic_has_admin_chan(enic))
2314 enic_admin_channel_close(enic);
2315
2316 enic_dev_hang_notify(enic);
2317 enic_stop(enic->netdev);
2318
2319 enic_dev_hang_reset(enic);
2320 enic_reset_addr_lists(enic);
2321 enic_init_vnic_resources(enic);
2322 enic_set_rss_nic_cfg(enic);
2323 enic_dev_set_ig_vlan_rewrite_mode(enic);
2324 enic_ext_cq(enic);
2325
2326 enic_open(enic->netdev);
2327
2328 /* Re-establish the admin/MBOX channel after the data path is back up.
2329 * It was fully torn down by enic_admin_channel_close() above;
2330 * enic_admin_chan_reopen() reopens it and, for a PF re-pushes link
2331 * state, or for a VF re-runs the probe-time PF handshake.
2332 */
2333 if (enic_has_admin_chan(enic))
2334 enic_admin_chan_reopen(enic);
2335
2336 /* Allow infiniband to fiddle with the device again */
2337 enic_set_api_busy(enic, false);
2338
2339 call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
2340
2341 rtnl_unlock();
2342 }
2343
enic_set_intr_mode(struct enic * enic)2344 static int enic_set_intr_mode(struct enic *enic)
2345 {
2346 unsigned int admin_reserve = enic->has_admin_channel ? 1 : 0;
2347 unsigned int min_intr = ENIC_MSIX_MIN_INTR + admin_reserve;
2348 unsigned int i;
2349 int num_intr;
2350
2351 /* Set interrupt mode (INTx, MSI, MSI-X) depending
2352 * on system capabilities.
2353 *
2354 * Try MSI-X first
2355 */
2356
2357 if (enic->config.intr_mode < 1 &&
2358 enic->intr_avail >= min_intr) {
2359 for (i = 0; i < enic->intr_avail; i++)
2360 enic->msix_entry[i].entry = i;
2361
2362 num_intr = pci_enable_msix_range(enic->pdev, enic->msix_entry,
2363 min_intr,
2364 enic->intr_avail);
2365 if (num_intr > 0) {
2366 vnic_dev_set_intr_mode(enic->vdev,
2367 VNIC_DEV_INTR_MODE_MSIX);
2368 enic->intr_avail = num_intr;
2369 return 0;
2370 }
2371 }
2372
2373 /* Next try MSI
2374 *
2375 * We need 1 INTR
2376 */
2377
2378 if (enic->config.intr_mode < 2 &&
2379 enic->intr_avail >= 1 &&
2380 !pci_enable_msi(enic->pdev)) {
2381 enic->intr_avail = 1;
2382 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2383 return 0;
2384 }
2385
2386 /* Next try INTx
2387 *
2388 * We need 3 INTRs
2389 * (the first INTR is used for WQ/RQ)
2390 * (the second INTR is used for WQ/RQ errors)
2391 * (the last INTR is used for notifications)
2392 */
2393
2394 if (enic->config.intr_mode < 3 &&
2395 enic->intr_avail >= 3) {
2396 enic->intr_avail = 3;
2397 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2398 return 0;
2399 }
2400
2401 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2402
2403 return -EINVAL;
2404 }
2405
enic_clear_intr_mode(struct enic * enic)2406 static void enic_clear_intr_mode(struct enic *enic)
2407 {
2408 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2409 case VNIC_DEV_INTR_MODE_MSIX:
2410 pci_disable_msix(enic->pdev);
2411 break;
2412 case VNIC_DEV_INTR_MODE_MSI:
2413 pci_disable_msi(enic->pdev);
2414 break;
2415 default:
2416 break;
2417 }
2418
2419 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2420 }
2421
enic_adjust_resources(struct enic * enic)2422 static int enic_adjust_resources(struct enic *enic)
2423 {
2424 unsigned int max_queues;
2425 unsigned int rq_default;
2426 unsigned int rq_avail;
2427 unsigned int wq_avail;
2428
2429 if (enic->rq_avail < 1 || enic->wq_avail < 1 || enic->cq_avail < 2) {
2430 dev_err(enic_get_dev(enic),
2431 "Not enough resources available rq: %d wq: %d cq: %d\n",
2432 enic->rq_avail, enic->wq_avail,
2433 enic->cq_avail);
2434 return -ENOSPC;
2435 }
2436
2437 if (is_kdump_kernel()) {
2438 dev_info(enic_get_dev(enic), "Running from within kdump kernel. Using minimal resources\n");
2439 enic->rq_avail = 1;
2440 enic->wq_avail = 1;
2441 enic->config.rq_desc_count = ENIC_MIN_RQ_DESCS;
2442 enic->config.wq_desc_count = ENIC_MIN_WQ_DESCS;
2443 enic->config.mtu = min_t(u16, 1500, enic->config.mtu);
2444 }
2445
2446 /* if RSS isn't set, then we can only use one RQ */
2447 if (!ENIC_SETTING(enic, RSS))
2448 enic->rq_avail = 1;
2449
2450 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2451 case VNIC_DEV_INTR_MODE_INTX:
2452 case VNIC_DEV_INTR_MODE_MSI:
2453 enic->rq_count = 1;
2454 enic->wq_count = 1;
2455 enic->cq_count = 2;
2456 enic->intr_count = enic->intr_avail;
2457 break;
2458 case VNIC_DEV_INTR_MODE_MSIX: {
2459 /* Adjust the number of wqs/rqs/cqs/interrupts that will be
2460 * used based on which resource is the most constrained.
2461 * Reserve one extra MSI-X slot for the admin channel INTR
2462 * when has_admin_channel is set so that
2463 * enic_admin_setup_intr() can allocate at intr_count
2464 * within the intr_avail bounds even when the data queue
2465 * count is maxed out. intr_count counts only the data-path
2466 * IRQs (registered by enic_request_intr()); the admin INTR
2467 * lives at msix index intr_count and is set up later by
2468 * enic_admin_setup_intr().
2469 */
2470 unsigned int admin_reserve = enic->has_admin_channel ? 1 : 0;
2471
2472 wq_avail = min(enic->wq_avail, ENIC_WQ_MAX);
2473 rq_default = max(netif_get_num_default_rss_queues(),
2474 ENIC_RQ_MIN_DEFAULT);
2475 rq_avail = min3(enic->rq_avail, ENIC_RQ_MAX, rq_default);
2476 max_queues = min(enic->cq_avail,
2477 enic->intr_avail - ENIC_MSIX_RESERVED_INTR -
2478 admin_reserve);
2479 if (wq_avail + rq_avail <= max_queues) {
2480 enic->rq_count = rq_avail;
2481 enic->wq_count = wq_avail;
2482 } else {
2483 /* recalculate wq/rq count */
2484 if (rq_avail < wq_avail) {
2485 enic->rq_count = min(rq_avail, max_queues / 2);
2486 enic->wq_count = max_queues - enic->rq_count;
2487 } else {
2488 enic->wq_count = min(wq_avail, max_queues / 2);
2489 enic->rq_count = max_queues - enic->wq_count;
2490 }
2491 }
2492 enic->cq_count = enic->rq_count + enic->wq_count;
2493 enic->intr_count = enic->cq_count + ENIC_MSIX_RESERVED_INTR;
2494
2495 break;
2496 }
2497 default:
2498 dev_err(enic_get_dev(enic), "Unknown interrupt mode\n");
2499 return -EINVAL;
2500 }
2501
2502 return 0;
2503 }
2504
enic_get_queue_stats_rx(struct net_device * dev,int idx,struct netdev_queue_stats_rx * rxs)2505 static void enic_get_queue_stats_rx(struct net_device *dev, int idx,
2506 struct netdev_queue_stats_rx *rxs)
2507 {
2508 struct enic *enic = netdev_priv(dev);
2509 struct enic_rq_stats *rqstats = &enic->rq[idx].stats;
2510
2511 rxs->bytes = rqstats->bytes;
2512 rxs->packets = rqstats->packets;
2513 rxs->hw_drops = rqstats->bad_fcs + rqstats->pkt_truncated;
2514 rxs->hw_drop_overruns = rqstats->pkt_truncated;
2515 rxs->csum_unnecessary = rqstats->csum_unnecessary +
2516 rqstats->csum_unnecessary_encap;
2517 rxs->alloc_fail = rqstats->pp_alloc_fail;
2518 }
2519
enic_get_queue_stats_tx(struct net_device * dev,int idx,struct netdev_queue_stats_tx * txs)2520 static void enic_get_queue_stats_tx(struct net_device *dev, int idx,
2521 struct netdev_queue_stats_tx *txs)
2522 {
2523 struct enic *enic = netdev_priv(dev);
2524 struct enic_wq_stats *wqstats = &enic->wq[idx].stats;
2525
2526 txs->bytes = wqstats->bytes;
2527 txs->packets = wqstats->packets;
2528 txs->csum_none = wqstats->csum_none;
2529 txs->needs_csum = wqstats->csum_partial + wqstats->encap_csum +
2530 wqstats->tso;
2531 txs->hw_gso_packets = wqstats->tso;
2532 txs->stop = wqstats->stopped;
2533 txs->wake = wqstats->wake;
2534 }
2535
enic_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rxs,struct netdev_queue_stats_tx * txs)2536 static void enic_get_base_stats(struct net_device *dev,
2537 struct netdev_queue_stats_rx *rxs,
2538 struct netdev_queue_stats_tx *txs)
2539 {
2540 rxs->bytes = 0;
2541 rxs->packets = 0;
2542 rxs->hw_drops = 0;
2543 rxs->hw_drop_overruns = 0;
2544 rxs->csum_unnecessary = 0;
2545 rxs->alloc_fail = 0;
2546 txs->bytes = 0;
2547 txs->packets = 0;
2548 txs->csum_none = 0;
2549 txs->needs_csum = 0;
2550 txs->hw_gso_packets = 0;
2551 txs->stop = 0;
2552 txs->wake = 0;
2553 }
2554
2555 static const struct net_device_ops enic_netdev_dynamic_ops = {
2556 .ndo_open = enic_open,
2557 .ndo_stop = enic_stop,
2558 .ndo_start_xmit = enic_hard_start_xmit,
2559 .ndo_get_stats64 = enic_get_stats,
2560 .ndo_validate_addr = eth_validate_addr,
2561 .ndo_set_rx_mode = enic_set_rx_mode,
2562 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2563 .ndo_change_mtu = enic_change_mtu,
2564 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2565 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2566 .ndo_tx_timeout = enic_tx_timeout,
2567 .ndo_set_vf_port = enic_set_vf_port,
2568 .ndo_get_vf_port = enic_get_vf_port,
2569 .ndo_set_vf_mac = enic_set_vf_mac,
2570 #ifdef CONFIG_NET_POLL_CONTROLLER
2571 .ndo_poll_controller = enic_poll_controller,
2572 #endif
2573 #ifdef CONFIG_RFS_ACCEL
2574 .ndo_rx_flow_steer = enic_rx_flow_steer,
2575 #endif
2576 .ndo_features_check = enic_features_check,
2577 };
2578
2579 static const struct net_device_ops enic_netdev_ops = {
2580 .ndo_open = enic_open,
2581 .ndo_stop = enic_stop,
2582 .ndo_start_xmit = enic_hard_start_xmit,
2583 .ndo_get_stats64 = enic_get_stats,
2584 .ndo_validate_addr = eth_validate_addr,
2585 .ndo_set_mac_address = enic_set_mac_address,
2586 .ndo_set_rx_mode = enic_set_rx_mode,
2587 .ndo_change_mtu = enic_change_mtu,
2588 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2589 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2590 .ndo_tx_timeout = enic_tx_timeout,
2591 .ndo_set_vf_port = enic_set_vf_port,
2592 .ndo_get_vf_port = enic_get_vf_port,
2593 .ndo_set_vf_mac = enic_set_vf_mac,
2594 #ifdef CONFIG_NET_POLL_CONTROLLER
2595 .ndo_poll_controller = enic_poll_controller,
2596 #endif
2597 #ifdef CONFIG_RFS_ACCEL
2598 .ndo_rx_flow_steer = enic_rx_flow_steer,
2599 #endif
2600 .ndo_features_check = enic_features_check,
2601 };
2602
2603 static const struct netdev_stat_ops enic_netdev_stat_ops = {
2604 .get_queue_stats_rx = enic_get_queue_stats_rx,
2605 .get_queue_stats_tx = enic_get_queue_stats_tx,
2606 .get_base_stats = enic_get_base_stats,
2607 };
2608
enic_free_enic_resources(struct enic * enic)2609 static void enic_free_enic_resources(struct enic *enic)
2610 {
2611 kfree(enic->wq);
2612 enic->wq = NULL;
2613
2614 kfree(enic->rq);
2615 enic->rq = NULL;
2616
2617 kfree(enic->cq);
2618 enic->cq = NULL;
2619
2620 kfree(enic->napi);
2621 enic->napi = NULL;
2622
2623 kfree(enic->msix_entry);
2624 enic->msix_entry = NULL;
2625
2626 kfree(enic->msix);
2627 enic->msix = NULL;
2628
2629 kfree(enic->intr);
2630 enic->intr = NULL;
2631 }
2632
enic_alloc_enic_resources(struct enic * enic)2633 static int enic_alloc_enic_resources(struct enic *enic)
2634 {
2635 enic->wq = kzalloc_objs(struct enic_wq, enic->wq_avail);
2636 if (!enic->wq)
2637 goto free_queues;
2638
2639 enic->rq = kzalloc_objs(struct enic_rq, enic->rq_avail);
2640 if (!enic->rq)
2641 goto free_queues;
2642
2643 enic->cq = kzalloc_objs(struct vnic_cq, enic->cq_avail);
2644 if (!enic->cq)
2645 goto free_queues;
2646
2647 enic->napi = kzalloc_objs(struct napi_struct,
2648 enic->wq_avail + enic->rq_avail);
2649 if (!enic->napi)
2650 goto free_queues;
2651
2652 enic->msix_entry = kzalloc_objs(struct msix_entry, enic->intr_avail);
2653 if (!enic->msix_entry)
2654 goto free_queues;
2655
2656 enic->msix = kzalloc_objs(struct enic_msix_entry, enic->intr_avail);
2657 if (!enic->msix)
2658 goto free_queues;
2659
2660 enic->intr = kzalloc_objs(struct vnic_intr, enic->intr_avail);
2661 if (!enic->intr)
2662 goto free_queues;
2663
2664 return 0;
2665
2666 free_queues:
2667 enic_free_enic_resources(enic);
2668 return -ENOMEM;
2669 }
2670
enic_dev_deinit(struct enic * enic)2671 static void enic_dev_deinit(struct enic *enic)
2672 {
2673 unsigned int i;
2674
2675 for (i = 0; i < enic->rq_count; i++)
2676 __netif_napi_del(&enic->napi[i]);
2677
2678 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
2679 for (i = 0; i < enic->wq_count; i++)
2680 __netif_napi_del(&enic->napi[enic_cq_wq(enic, i)]);
2681
2682 /* observe RCU grace period after __netif_napi_del() calls */
2683 synchronize_net();
2684
2685 enic_free_vnic_resources(enic);
2686 enic_clear_intr_mode(enic);
2687 enic_free_affinity_hint(enic);
2688 enic_free_enic_resources(enic);
2689 }
2690
enic_dev_init(struct enic * enic)2691 static int enic_dev_init(struct enic *enic)
2692 {
2693 struct device *dev = enic_get_dev(enic);
2694 struct net_device *netdev = enic->netdev;
2695 unsigned int i;
2696 int err;
2697
2698 /* Get interrupt coalesce timer info */
2699 err = enic_dev_intr_coal_timer_info(enic);
2700 if (err) {
2701 dev_warn(dev, "Using default conversion factor for "
2702 "interrupt coalesce timer\n");
2703 vnic_dev_intr_coal_timer_info_default(enic->vdev);
2704 }
2705
2706 /* Get vNIC configuration
2707 */
2708
2709 err = enic_get_vnic_config(enic);
2710 if (err) {
2711 dev_err(dev, "Get vNIC configuration failed, aborting\n");
2712 return err;
2713 }
2714
2715 /* Get available resource counts
2716 */
2717
2718 enic_get_res_counts(enic);
2719
2720 enic_ext_cq(enic);
2721
2722 err = enic_alloc_enic_resources(enic);
2723 if (err) {
2724 dev_err(dev, "Failed to allocate enic resources\n");
2725 return err;
2726 }
2727
2728 /* Set interrupt mode based on system capabilities */
2729
2730 err = enic_set_intr_mode(enic);
2731 if (err) {
2732 dev_err(dev, "Failed to set intr mode based on resource "
2733 "counts and system capabilities, aborting\n");
2734 goto err_out_free_vnic_resources;
2735 }
2736
2737 /* Adjust resource counts based on most constrained resources */
2738 err = enic_adjust_resources(enic);
2739 if (err) {
2740 dev_err(dev, "Failed to adjust resources\n");
2741 goto err_out_free_vnic_resources;
2742 }
2743
2744 /* Allocate and configure vNIC resources
2745 */
2746
2747 err = enic_alloc_vnic_resources(enic);
2748 if (err) {
2749 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2750 goto err_out_free_vnic_resources;
2751 }
2752
2753 enic_init_vnic_resources(enic);
2754
2755 err = enic_set_rss_nic_cfg(enic);
2756 if (err) {
2757 dev_err(dev, "Failed to config nic, aborting\n");
2758 goto err_out_free_vnic_resources;
2759 }
2760
2761 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2762 default:
2763 netif_napi_add(netdev, &enic->napi[0], enic_poll);
2764 break;
2765 case VNIC_DEV_INTR_MODE_MSIX:
2766 for (i = 0; i < enic->rq_count; i++) {
2767 netif_napi_add(netdev, &enic->napi[i],
2768 enic_poll_msix_rq);
2769 }
2770 for (i = 0; i < enic->wq_count; i++)
2771 netif_napi_add(netdev,
2772 &enic->napi[enic_cq_wq(enic, i)],
2773 enic_poll_msix_wq);
2774 break;
2775 }
2776
2777 return 0;
2778
2779 err_out_free_vnic_resources:
2780 enic_free_affinity_hint(enic);
2781 enic_clear_intr_mode(enic);
2782 enic_free_vnic_resources(enic);
2783 enic_free_enic_resources(enic);
2784
2785 return err;
2786 }
2787
enic_iounmap(struct enic * enic)2788 static void enic_iounmap(struct enic *enic)
2789 {
2790 unsigned int i;
2791
2792 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2793 if (enic->bar[i].vaddr)
2794 iounmap(enic->bar[i].vaddr);
2795 }
2796
2797 #ifdef CONFIG_PCI_IOV
enic_sriov_detect_vf_type(struct enic * enic)2798 static void enic_sriov_detect_vf_type(struct enic *enic)
2799 {
2800 struct pci_dev *pdev = enic->pdev;
2801 u64 supported_versions, a1 = 0;
2802 u16 vf_dev_id;
2803 int pos;
2804 int err;
2805
2806 if (enic_is_sriov_vf(enic) || enic_is_dynamic(enic))
2807 return;
2808
2809 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
2810 if (!pos) {
2811 enic->vf_type = ENIC_VF_TYPE_NONE;
2812 return;
2813 }
2814
2815 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &vf_dev_id);
2816
2817 switch (vf_dev_id) {
2818 case PCI_DEVICE_ID_CISCO_VIC_ENET_VF:
2819 enic->vf_type = ENIC_VF_TYPE_V1;
2820 break;
2821 case PCI_DEVICE_ID_CISCO_VIC_ENET_VF_USNIC:
2822 enic->vf_type = ENIC_VF_TYPE_USNIC;
2823 break;
2824 case PCI_DEVICE_ID_CISCO_VIC_ENET_VF_V2:
2825 enic->vf_type = ENIC_VF_TYPE_V2;
2826 break;
2827 default:
2828 enic->vf_type = ENIC_VF_TYPE_NONE;
2829 break;
2830 }
2831
2832 if (enic->vf_type != ENIC_VF_TYPE_V2)
2833 return;
2834
2835 /* A successful command means firmware recognizes
2836 * VIC_FEATURE_SRIOV; supported_versions is available
2837 * for sub-feature versioning in the future.
2838 */
2839 err = vnic_dev_get_supported_feature_ver(enic->vdev,
2840 VIC_FEATURE_SRIOV,
2841 &supported_versions,
2842 &a1);
2843 if (err) {
2844 dev_warn(&pdev->dev,
2845 "SR-IOV V2 not supported by current firmware. Upgrade to VIC FW 5.3(4.72) or higher.\n");
2846 enic->vf_type = ENIC_VF_TYPE_NONE;
2847 }
2848 }
2849
2850 static int __maybe_unused
enic_sriov_v2_enable(struct enic * enic,int num_vfs)2851 enic_sriov_v2_enable(struct enic *enic, int num_vfs)
2852 {
2853 int err;
2854
2855 if (!enic->has_admin_channel) {
2856 netdev_err(enic->netdev,
2857 "V2 SR-IOV requires admin channel resources\n");
2858 return -EOPNOTSUPP;
2859 }
2860
2861 enic->vf_state = kcalloc(num_vfs, sizeof(*enic->vf_state), GFP_KERNEL);
2862 if (!enic->vf_state)
2863 return -ENOMEM;
2864
2865 /* Install the MBOX receive handler before the admin interrupt is
2866 * unmasked in enic_admin_channel_open(), so no early completion is
2867 * dropped.
2868 */
2869 enic_mbox_init(enic);
2870
2871 err = enic_admin_channel_open(enic);
2872 if (err) {
2873 netdev_err(enic->netdev,
2874 "Failed to open admin channel: %d\n", err);
2875 goto free_vf_state;
2876 }
2877
2878 enic->num_vfs = num_vfs;
2879
2880 err = pci_enable_sriov(enic->pdev, num_vfs);
2881 if (err) {
2882 netdev_err(enic->netdev,
2883 "pci_enable_sriov failed: %d\n", err);
2884 goto close_admin;
2885 }
2886
2887 enic->priv_flags |= ENIC_SRIOV_ENABLED;
2888 return num_vfs;
2889
2890 close_admin:
2891 enic->num_vfs = 0;
2892 enic_admin_channel_close(enic);
2893 free_vf_state:
2894 kfree(enic->vf_state);
2895 enic->vf_state = NULL;
2896 return err;
2897 }
2898
enic_sriov_v2_disable(struct enic * enic)2899 static void enic_sriov_v2_disable(struct enic *enic)
2900 {
2901 /* Stop new VF link-state broadcasts before tearing down vf_state.
2902 * Clearing ENIC_SRIOV_ENABLED makes enic_link_check() (called from
2903 * the notify timer/ISR) skip the VF notify path, and cancelling
2904 * link_notify_work ensures any already-queued broadcast has finished
2905 * before vf_state is freed, closing a use-after-free window.
2906 */
2907 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2908 cancel_work_sync(&enic->link_notify_work);
2909
2910 pci_disable_sriov(enic->pdev);
2911 enic_admin_channel_close(enic);
2912 kfree(enic->vf_state);
2913 enic->vf_state = NULL;
2914 enic->num_vfs = 0;
2915 }
2916
2917 /*
2918 * enic_sriov_configure() and its V2 helpers are defined but not yet wired
2919 * into enic_driver via .sriov_configure (see the __maybe_unused annotations);
2920 * V2 enable/disable is activated in a follow-up series. Because the callback
2921 * is not registered, it cannot run concurrently with the rtnl-protected reset
2922 * paths (enic_reset(), enic_tx_hang_reset()) yet. Serialization against those
2923 * paths is added together with the .sriov_configure wiring in that series.
2924 */
2925 static int __maybe_unused
enic_sriov_configure(struct pci_dev * pdev,int num_vfs)2926 enic_sriov_configure(struct pci_dev *pdev, int num_vfs)
2927 {
2928 struct net_device *netdev = pci_get_drvdata(pdev);
2929 struct enic *enic = netdev_priv(netdev);
2930 struct enic_port_profile *pp;
2931 int err;
2932
2933 if (num_vfs > 0) {
2934 if (enic->config.mq_subvnic_count) {
2935 netdev_err(netdev,
2936 "SR-IOV not supported with multi-queue sub-vnics\n");
2937 return -EOPNOTSUPP;
2938 }
2939
2940 if (enic->vf_type == ENIC_VF_TYPE_NONE) {
2941 netdev_err(netdev,
2942 "SR-IOV not supported on this firmware version\n");
2943 return -EOPNOTSUPP;
2944 }
2945
2946 if (enic->vf_type == ENIC_VF_TYPE_V2)
2947 return enic_sriov_v2_enable(enic, num_vfs);
2948
2949 pp = kcalloc(num_vfs, sizeof(*pp), GFP_KERNEL);
2950 if (!pp)
2951 return -ENOMEM;
2952
2953 err = pci_enable_sriov(pdev, num_vfs);
2954 if (err) {
2955 kfree(pp);
2956 return err;
2957 }
2958
2959 kfree(enic->pp);
2960 enic->pp = pp;
2961 enic->num_vfs = num_vfs;
2962 enic->priv_flags |= ENIC_SRIOV_ENABLED;
2963 return num_vfs;
2964 }
2965
2966 if (!enic_sriov_enabled(enic))
2967 return 0;
2968
2969 if (enic->vf_type == ENIC_VF_TYPE_V2) {
2970 enic_sriov_v2_disable(enic);
2971 return 0;
2972 }
2973
2974 pp = kzalloc_obj(*enic->pp, GFP_KERNEL);
2975 if (!pp)
2976 return -ENOMEM;
2977
2978 pci_disable_sriov(pdev);
2979 enic->num_vfs = 0;
2980 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2981
2982 kfree(enic->pp);
2983 enic->pp = pp;
2984
2985 return 0;
2986 }
2987 #endif
2988
enic_probe(struct pci_dev * pdev,const struct pci_device_id * ent)2989 static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2990 {
2991 struct device *dev = &pdev->dev;
2992 struct net_device *netdev;
2993 struct enic *enic;
2994 int using_dac = 0;
2995 unsigned int i;
2996 int err;
2997 #ifdef CONFIG_PCI_IOV
2998 int pos = 0;
2999 #endif
3000 int num_pps = 1;
3001
3002 /* Allocate net device structure and initialize. Private
3003 * instance data is initialized to zero.
3004 */
3005
3006 netdev = alloc_etherdev_mqs(sizeof(struct enic),
3007 ENIC_RQ_MAX, ENIC_WQ_MAX);
3008 if (!netdev)
3009 return -ENOMEM;
3010
3011 pci_set_drvdata(pdev, netdev);
3012
3013 SET_NETDEV_DEV(netdev, &pdev->dev);
3014
3015 enic = netdev_priv(netdev);
3016 enic->netdev = netdev;
3017 enic->pdev = pdev;
3018
3019 /* Setup PCI resources
3020 */
3021
3022 err = pci_enable_device_mem(pdev);
3023 if (err) {
3024 dev_err(dev, "Cannot enable PCI device, aborting\n");
3025 goto err_out_free_netdev;
3026 }
3027
3028 err = pci_request_regions(pdev, DRV_NAME);
3029 if (err) {
3030 dev_err(dev, "Cannot request PCI regions, aborting\n");
3031 goto err_out_disable_device;
3032 }
3033
3034 pci_set_master(pdev);
3035
3036 /* Query PCI controller on system for DMA addressing
3037 * limitation for the device. Try 47-bit first, and
3038 * fail to 32-bit.
3039 */
3040
3041 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(47));
3042 if (err) {
3043 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3044 if (err) {
3045 dev_err(dev, "No usable DMA configuration, aborting\n");
3046 goto err_out_release_regions;
3047 }
3048 } else {
3049 using_dac = 1;
3050 }
3051
3052 /* Map vNIC resources from BAR0-5
3053 */
3054
3055 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
3056 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
3057 continue;
3058 enic->bar[i].len = pci_resource_len(pdev, i);
3059 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
3060 if (!enic->bar[i].vaddr) {
3061 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
3062 err = -ENODEV;
3063 goto err_out_iounmap;
3064 }
3065 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
3066 }
3067
3068 /* Register vNIC device
3069 */
3070
3071 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
3072 ARRAY_SIZE(enic->bar));
3073 if (!enic->vdev) {
3074 dev_err(dev, "vNIC registration failed, aborting\n");
3075 err = -ENODEV;
3076 goto err_out_iounmap;
3077 }
3078
3079 err = vnic_devcmd_init(enic->vdev);
3080
3081 if (err)
3082 goto err_out_vnic_unregister;
3083
3084 #ifdef CONFIG_PCI_IOV
3085 enic_sriov_detect_vf_type(enic);
3086
3087 /* Auto-enable SR-IOV only for the legacy VF types. V2 VFs require
3088 * the admin channel, which is not yet set up at probe time (V2 SR-IOV
3089 * will be enabled through the sysfs .sriov_configure callback once a
3090 * follow-up series wires it up); and a V2-capable device whose
3091 * firmware lacks V2 support is downgraded to ENIC_VF_TYPE_NONE by
3092 * enic_sriov_detect_vf_type() and must not be brought up through the
3093 * legacy pci_enable_sriov() path either.
3094 */
3095 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
3096 if (pos) {
3097 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF,
3098 &enic->num_vfs);
3099 if (enic->num_vfs &&
3100 (enic->vf_type == ENIC_VF_TYPE_V1 ||
3101 enic->vf_type == ENIC_VF_TYPE_USNIC)) {
3102 err = pci_enable_sriov(pdev, enic->num_vfs);
3103 if (err) {
3104 dev_err(dev, "SRIOV enable failed, aborting."
3105 " pci_enable_sriov() returned %d\n",
3106 err);
3107 goto err_out_vnic_unregister;
3108 }
3109 enic->priv_flags |= ENIC_SRIOV_ENABLED;
3110 num_pps = enic->num_vfs;
3111 }
3112 }
3113 #endif
3114
3115 /* Allocate structure for port profiles */
3116 enic->pp = kzalloc_objs(*enic->pp, num_pps);
3117 if (!enic->pp) {
3118 err = -ENOMEM;
3119 goto err_out_disable_sriov_pp;
3120 }
3121
3122 /* Issue device open to get device in known state
3123 */
3124
3125 err = enic_dev_open(enic);
3126 if (err) {
3127 dev_err(dev, "vNIC dev open failed, aborting\n");
3128 goto err_out_disable_sriov;
3129 }
3130
3131 /* Setup devcmd lock
3132 */
3133
3134 spin_lock_init(&enic->devcmd_lock);
3135 spin_lock_init(&enic->enic_api_lock);
3136
3137 /*
3138 * Set ingress vlan rewrite mode before vnic initialization
3139 */
3140
3141 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
3142 if (err) {
3143 dev_err(dev,
3144 "Failed to set ingress vlan rewrite mode, aborting.\n");
3145 goto err_out_dev_close;
3146 }
3147
3148 /* Issue device init to initialize the vnic-to-switch link.
3149 * We'll start with carrier off and wait for link UP
3150 * notification later to turn on carrier. We don't need
3151 * to wait here for the vnic-to-switch link initialization
3152 * to complete; link UP notification is the indication that
3153 * the process is complete.
3154 */
3155
3156 netif_carrier_off(netdev);
3157
3158 /* Do not call dev_init for a dynamic vnic.
3159 * For a dynamic vnic, init_prov_info will be
3160 * called later by an upper layer.
3161 */
3162
3163 if (!enic_is_dynamic(enic)) {
3164 err = vnic_dev_init(enic->vdev, 0);
3165 if (err) {
3166 dev_err(dev, "vNIC dev init failed, aborting\n");
3167 goto err_out_dev_close;
3168 }
3169 }
3170
3171 err = enic_dev_init(enic);
3172 if (err) {
3173 dev_err(dev, "Device initialization failed, aborting\n");
3174 goto err_out_dev_close;
3175 }
3176
3177 /* Initialise link_notify_work before the V2-VF admin-open block below:
3178 * its error path (err_out_admin_close -> enic_admin_channel_close() ->
3179 * cancel_work_sync()) would otherwise act on an uninitialised work.
3180 */
3181 INIT_WORK(&enic->link_notify_work, enic_link_notify_work_handler);
3182
3183 /* V2 VF: open admin channel and register with PF.
3184 * Must happen before register_netdev so the VF is fully
3185 * initialized before the interface is visible to userspace.
3186 *
3187 * enic_mbox_init() installs the receive handler and resets the
3188 * sequence number; it must run before enic_admin_channel_open()
3189 * unmasks the admin interrupt so an early completion is not dropped.
3190 */
3191 if (enic_is_sriov_vf_v2(enic)) {
3192 enic_mbox_init(enic);
3193 err = enic_admin_channel_open(enic);
3194 if (err) {
3195 dev_err(dev,
3196 "Failed to open admin channel: %d\n", err);
3197 goto err_out_dev_deinit;
3198 }
3199 err = enic_mbox_vf_capability_check(enic);
3200 if (err) {
3201 dev_err(dev,
3202 "MBOX capability check failed: %d\n", err);
3203 goto err_out_admin_close;
3204 }
3205 err = enic_mbox_vf_register(enic);
3206 if (err) {
3207 dev_err(dev,
3208 "MBOX VF registration failed: %d\n", err);
3209 goto err_out_admin_close;
3210 }
3211 }
3212
3213 netif_set_real_num_tx_queues(netdev, enic->wq_count);
3214 netif_set_real_num_rx_queues(netdev, enic->rq_count);
3215
3216 /* Setup notification timer, HW reset task, and wq locks
3217 */
3218
3219 timer_setup(&enic->notify_timer, enic_notify_timer, 0);
3220
3221 enic_rfs_flw_tbl_init(enic);
3222 INIT_WORK(&enic->reset, enic_reset);
3223 INIT_WORK(&enic->tx_hang_reset, enic_tx_hang_reset);
3224 INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
3225
3226 for (i = 0; i < enic->wq_count; i++)
3227 spin_lock_init(&enic->wq[i].lock);
3228
3229 /* Register net device
3230 */
3231
3232 enic->port_mtu = enic->config.mtu;
3233
3234 err = enic_set_mac_addr(netdev, enic->mac_addr);
3235 if (err) {
3236 dev_err(dev, "Invalid MAC address, aborting\n");
3237 goto err_out_admin_close;
3238 }
3239
3240 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
3241 /* rx coalesce time already got initialized. This gets used
3242 * if adaptive coal is turned off
3243 */
3244 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
3245
3246 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
3247 netdev->netdev_ops = &enic_netdev_dynamic_ops;
3248 else
3249 netdev->netdev_ops = &enic_netdev_ops;
3250 netdev->stat_ops = &enic_netdev_stat_ops;
3251
3252 netdev->watchdog_timeo = 2 * HZ;
3253 enic_set_ethtool_ops(netdev);
3254
3255 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
3256 if (ENIC_SETTING(enic, LOOP)) {
3257 netdev->features &= ~NETIF_F_HW_VLAN_CTAG_TX;
3258 enic->loop_enable = 1;
3259 enic->loop_tag = enic->config.loop_tag;
3260 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
3261 }
3262 if (ENIC_SETTING(enic, TXCSUM))
3263 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3264 if (ENIC_SETTING(enic, TSO))
3265 netdev->hw_features |= NETIF_F_TSO |
3266 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
3267 if (ENIC_SETTING(enic, RSS))
3268 netdev->hw_features |= NETIF_F_RXHASH;
3269 if (ENIC_SETTING(enic, RXCSUM))
3270 netdev->hw_features |= NETIF_F_RXCSUM;
3271 if (ENIC_SETTING(enic, VXLAN)) {
3272 u64 patch_level;
3273 u64 a1 = 0;
3274
3275 netdev->hw_enc_features |= NETIF_F_RXCSUM |
3276 NETIF_F_TSO |
3277 NETIF_F_TSO6 |
3278 NETIF_F_TSO_ECN |
3279 NETIF_F_GSO_UDP_TUNNEL |
3280 NETIF_F_HW_CSUM |
3281 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3282 netdev->hw_features |= netdev->hw_enc_features;
3283 /* get bit mask from hw about supported offload bit level
3284 * BIT(0) = fw supports patch_level 0
3285 * fcoe bit = encap
3286 * fcoe_fc_crc_ok = outer csum ok
3287 * BIT(1) = always set by fw
3288 * BIT(2) = fw supports patch_level 2
3289 * BIT(0) in rss_hash = encap
3290 * BIT(1,2) in rss_hash = outer_ip_csum_ok/
3291 * outer_tcp_csum_ok
3292 * used in enic_rq_indicate_buf
3293 */
3294 err = vnic_dev_get_supported_feature_ver(enic->vdev,
3295 VIC_FEATURE_VXLAN,
3296 &patch_level, &a1);
3297 if (err)
3298 patch_level = 0;
3299 enic->vxlan.flags = (u8)a1;
3300 /* mask bits that are supported by driver
3301 */
3302 patch_level &= BIT_ULL(0) | BIT_ULL(2);
3303 patch_level = fls(patch_level);
3304 patch_level = patch_level ? patch_level - 1 : 0;
3305 enic->vxlan.patch_level = patch_level;
3306
3307 if (vnic_dev_get_res_count(enic->vdev, RES_TYPE_WQ) == 1 ||
3308 enic->vxlan.flags & ENIC_VXLAN_MULTI_WQ) {
3309 netdev->udp_tunnel_nic_info = &enic_udp_tunnels_v4;
3310 if (enic->vxlan.flags & ENIC_VXLAN_OUTER_IPV6)
3311 netdev->udp_tunnel_nic_info = &enic_udp_tunnels;
3312 }
3313 }
3314
3315 netdev->features |= netdev->hw_features;
3316 netdev->vlan_features |= netdev->features;
3317
3318 #ifdef CONFIG_RFS_ACCEL
3319 netdev->hw_features |= NETIF_F_NTUPLE;
3320 #endif
3321
3322 if (using_dac)
3323 netdev->features |= NETIF_F_HIGHDMA;
3324
3325 netdev->priv_flags |= IFF_UNICAST_FLT;
3326
3327 /* MTU range: 68 - 9000 */
3328 netdev->min_mtu = ENIC_MIN_MTU;
3329 netdev->max_mtu = ENIC_MAX_MTU;
3330 netdev->mtu = enic->port_mtu;
3331
3332 err = register_netdev(netdev);
3333 if (err) {
3334 dev_err(dev, "Cannot register net device, aborting\n");
3335 goto err_out_admin_close;
3336 }
3337
3338 return 0;
3339
3340 err_out_admin_close:
3341 if (enic_is_sriov_vf_v2(enic)) {
3342 if (enic->vf_registered) {
3343 int unreg_err = enic_mbox_vf_unregister(enic);
3344
3345 if (unreg_err)
3346 netdev_warn(netdev,
3347 "Failed to unregister from PF: %d\n",
3348 unreg_err);
3349 }
3350 enic_admin_channel_close(enic);
3351 }
3352 err_out_dev_deinit:
3353 enic_dev_deinit(enic);
3354 err_out_dev_close:
3355 vnic_dev_close(enic->vdev);
3356 err_out_disable_sriov:
3357 kfree(enic->pp);
3358 err_out_disable_sriov_pp:
3359 #ifdef CONFIG_PCI_IOV
3360 if (enic_sriov_enabled(enic)) {
3361 pci_disable_sriov(pdev);
3362 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
3363 }
3364 #endif
3365 err_out_vnic_unregister:
3366 vnic_dev_unregister(enic->vdev);
3367 err_out_iounmap:
3368 enic_iounmap(enic);
3369 err_out_release_regions:
3370 pci_release_regions(pdev);
3371 err_out_disable_device:
3372 pci_disable_device(pdev);
3373 err_out_free_netdev:
3374 free_netdev(netdev);
3375
3376 return err;
3377 }
3378
enic_remove(struct pci_dev * pdev)3379 static void enic_remove(struct pci_dev *pdev)
3380 {
3381 struct net_device *netdev = pci_get_drvdata(pdev);
3382
3383 if (netdev) {
3384 struct enic *enic = netdev_priv(netdev);
3385
3386 disable_work_sync(&enic->reset);
3387 disable_work_sync(&enic->tx_hang_reset);
3388 disable_work_sync(&enic->change_mtu_work);
3389
3390 /* Close the admin channel and unregister from the PF before
3391 * unregister_netdev() to prevent a late PF notification from
3392 * touching a netdev that is being torn down.
3393 */
3394 if (enic_is_sriov_vf_v2(enic)) {
3395 if (enic->vf_registered) {
3396 int unreg_err = enic_mbox_vf_unregister(enic);
3397
3398 if (unreg_err)
3399 netdev_warn(netdev,
3400 "Failed to unregister from PF: %d\n",
3401 unreg_err);
3402 }
3403 enic_admin_channel_close(enic);
3404 }
3405
3406 unregister_netdev(netdev);
3407 /* unregister_netdev() -> enic_stop() stops the notify timer, so
3408 * no new link_notify_work can be queued past this point. Cancel
3409 * unconditionally to cover the narrow window where
3410 * enic_link_check() scheduled it just as SR-IOV was disabled.
3411 */
3412 cancel_work_sync(&enic->link_notify_work);
3413 #ifdef CONFIG_PCI_IOV
3414 if (enic_sriov_enabled(enic)) {
3415 if (enic->vf_type == ENIC_VF_TYPE_V2)
3416 enic_sriov_v2_disable(enic);
3417 else
3418 pci_disable_sriov(pdev);
3419 }
3420 #endif
3421 enic_dev_deinit(enic);
3422 vnic_dev_close(enic->vdev);
3423 kfree(enic->pp);
3424 vnic_dev_unregister(enic->vdev);
3425 enic_iounmap(enic);
3426 pci_release_regions(pdev);
3427 pci_disable_device(pdev);
3428 free_netdev(netdev);
3429 }
3430 }
3431
3432 static struct pci_driver enic_driver = {
3433 .name = DRV_NAME,
3434 .id_table = enic_id_table,
3435 .probe = enic_probe,
3436 .remove = enic_remove,
3437 };
3438
3439 module_pci_driver(enic_driver);
3440