1 /*
2 * Copyright (C) 2015 Cavium, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
7 */
8
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/pci.h>
12 #include <linux/netdevice.h>
13 #include <linux/if_vlan.h>
14 #include <linux/etherdevice.h>
15 #include <linux/ethtool.h>
16 #include <linux/log2.h>
17 #include <linux/prefetch.h>
18 #include <linux/irq.h>
19 #include <linux/iommu.h>
20 #include <linux/bpf.h>
21 #include <linux/bpf_trace.h>
22 #include <linux/filter.h>
23
24 #include "nic_reg.h"
25 #include "nic.h"
26 #include "nicvf_queues.h"
27 #include "thunder_bgx.h"
28
29 #define DRV_NAME "thunder-nicvf"
30 #define DRV_VERSION "1.0"
31
32 /* NOTE: Packets bigger than 1530 are split across multiple pages and XDP needs
33 * the buffer to be contiguous. Allow XDP to be set up only if we don't exceed
34 * this value, keeping headroom for the 14 byte Ethernet header and two
35 * VLAN tags (for QinQ)
36 */
37 #define MAX_XDP_MTU (1530 - ETH_HLEN - VLAN_HLEN * 2)
38
39 /* Supported devices */
40 static const struct pci_device_id nicvf_id_table[] = {
41 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
42 PCI_DEVICE_ID_THUNDER_NIC_VF,
43 PCI_VENDOR_ID_CAVIUM,
44 PCI_SUBSYS_DEVID_88XX_NIC_VF) },
45 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
46 PCI_DEVICE_ID_THUNDER_PASS1_NIC_VF,
47 PCI_VENDOR_ID_CAVIUM,
48 PCI_SUBSYS_DEVID_88XX_PASS1_NIC_VF) },
49 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
50 PCI_DEVICE_ID_THUNDER_NIC_VF,
51 PCI_VENDOR_ID_CAVIUM,
52 PCI_SUBSYS_DEVID_81XX_NIC_VF) },
53 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
54 PCI_DEVICE_ID_THUNDER_NIC_VF,
55 PCI_VENDOR_ID_CAVIUM,
56 PCI_SUBSYS_DEVID_83XX_NIC_VF) },
57 { 0, } /* end of table */
58 };
59
60 MODULE_AUTHOR("Sunil Goutham");
61 MODULE_DESCRIPTION("Cavium Thunder NIC Virtual Function Driver");
62 MODULE_LICENSE("GPL v2");
63 MODULE_VERSION(DRV_VERSION);
64 MODULE_DEVICE_TABLE(pci, nicvf_id_table);
65
66 static int debug = 0x00;
67 module_param(debug, int, 0644);
68 MODULE_PARM_DESC(debug, "Debug message level bitmap");
69
70 static int cpi_alg = CPI_ALG_NONE;
71 module_param(cpi_alg, int, S_IRUGO);
72 MODULE_PARM_DESC(cpi_alg,
73 "PFC algorithm (0=none, 1=VLAN, 2=VLAN16, 3=IP Diffserv)");
74
nicvf_netdev_qidx(struct nicvf * nic,u8 qidx)75 static inline u8 nicvf_netdev_qidx(struct nicvf *nic, u8 qidx)
76 {
77 if (nic->sqs_mode)
78 return qidx + ((nic->sqs_id + 1) * MAX_CMP_QUEUES_PER_QS);
79 else
80 return qidx;
81 }
82
83 /* The Cavium ThunderX network controller can *only* be found in SoCs
84 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
85 * registers on this platform are implicitly strongly ordered with respect
86 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
87 * with no memory barriers in this driver. The readq()/writeq() functions add
88 * explicit ordering operation which in this case are redundant, and only
89 * add overhead.
90 */
91
92 /* Register read/write APIs */
nicvf_reg_write(struct nicvf * nic,u64 offset,u64 val)93 void nicvf_reg_write(struct nicvf *nic, u64 offset, u64 val)
94 {
95 writeq_relaxed(val, nic->reg_base + offset);
96 }
97
nicvf_reg_read(struct nicvf * nic,u64 offset)98 u64 nicvf_reg_read(struct nicvf *nic, u64 offset)
99 {
100 return readq_relaxed(nic->reg_base + offset);
101 }
102
nicvf_queue_reg_write(struct nicvf * nic,u64 offset,u64 qidx,u64 val)103 void nicvf_queue_reg_write(struct nicvf *nic, u64 offset,
104 u64 qidx, u64 val)
105 {
106 void __iomem *addr = nic->reg_base + offset;
107
108 writeq_relaxed(val, addr + (qidx << NIC_Q_NUM_SHIFT));
109 }
110
nicvf_queue_reg_read(struct nicvf * nic,u64 offset,u64 qidx)111 u64 nicvf_queue_reg_read(struct nicvf *nic, u64 offset, u64 qidx)
112 {
113 void __iomem *addr = nic->reg_base + offset;
114
115 return readq_relaxed(addr + (qidx << NIC_Q_NUM_SHIFT));
116 }
117
118 /* VF -> PF mailbox communication */
nicvf_write_to_mbx(struct nicvf * nic,union nic_mbx * mbx)119 static void nicvf_write_to_mbx(struct nicvf *nic, union nic_mbx *mbx)
120 {
121 u64 *msg = (u64 *)mbx;
122
123 nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 0, msg[0]);
124 nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 8, msg[1]);
125 }
126
nicvf_send_msg_to_pf(struct nicvf * nic,union nic_mbx * mbx)127 int nicvf_send_msg_to_pf(struct nicvf *nic, union nic_mbx *mbx)
128 {
129 int timeout = NIC_MBOX_MSG_TIMEOUT;
130 int sleep = 10;
131
132 nic->pf_acked = false;
133 nic->pf_nacked = false;
134
135 nicvf_write_to_mbx(nic, mbx);
136
137 /* Wait for previous message to be acked, timeout 2sec */
138 while (!nic->pf_acked) {
139 if (nic->pf_nacked) {
140 netdev_err(nic->netdev,
141 "PF NACK to mbox msg 0x%02x from VF%d\n",
142 (mbx->msg.msg & 0xFF), nic->vf_id);
143 return -EINVAL;
144 }
145 msleep(sleep);
146 if (nic->pf_acked)
147 break;
148 timeout -= sleep;
149 if (!timeout) {
150 netdev_err(nic->netdev,
151 "PF didn't ACK to mbox msg 0x%02x from VF%d\n",
152 (mbx->msg.msg & 0xFF), nic->vf_id);
153 return -EBUSY;
154 }
155 }
156 return 0;
157 }
158
159 /* Checks if VF is able to comminicate with PF
160 * and also gets the VNIC number this VF is associated to.
161 */
nicvf_check_pf_ready(struct nicvf * nic)162 static int nicvf_check_pf_ready(struct nicvf *nic)
163 {
164 union nic_mbx mbx = {};
165
166 mbx.msg.msg = NIC_MBOX_MSG_READY;
167 if (nicvf_send_msg_to_pf(nic, &mbx)) {
168 netdev_err(nic->netdev,
169 "PF didn't respond to READY msg\n");
170 return 0;
171 }
172
173 return 1;
174 }
175
nicvf_send_cfg_done(struct nicvf * nic)176 static void nicvf_send_cfg_done(struct nicvf *nic)
177 {
178 union nic_mbx mbx = {};
179
180 mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
181 if (nicvf_send_msg_to_pf(nic, &mbx)) {
182 netdev_err(nic->netdev,
183 "PF didn't respond to CFG DONE msg\n");
184 }
185 }
186
nicvf_read_bgx_stats(struct nicvf * nic,struct bgx_stats_msg * bgx)187 static void nicvf_read_bgx_stats(struct nicvf *nic, struct bgx_stats_msg *bgx)
188 {
189 if (bgx->rx)
190 nic->bgx_stats.rx_stats[bgx->idx] = bgx->stats;
191 else
192 nic->bgx_stats.tx_stats[bgx->idx] = bgx->stats;
193 }
194
nicvf_handle_mbx_intr(struct nicvf * nic)195 static void nicvf_handle_mbx_intr(struct nicvf *nic)
196 {
197 union nic_mbx mbx = {};
198 u64 *mbx_data;
199 u64 mbx_addr;
200 int i;
201
202 mbx_addr = NIC_VF_PF_MAILBOX_0_1;
203 mbx_data = (u64 *)&mbx;
204
205 for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
206 *mbx_data = nicvf_reg_read(nic, mbx_addr);
207 mbx_data++;
208 mbx_addr += sizeof(u64);
209 }
210
211 netdev_dbg(nic->netdev, "Mbox message: msg: 0x%x\n", mbx.msg.msg);
212 switch (mbx.msg.msg) {
213 case NIC_MBOX_MSG_READY:
214 nic->pf_acked = true;
215 nic->vf_id = mbx.nic_cfg.vf_id & 0x7F;
216 nic->tns_mode = mbx.nic_cfg.tns_mode & 0x7F;
217 nic->node = mbx.nic_cfg.node_id;
218 if (!nic->set_mac_pending)
219 ether_addr_copy(nic->netdev->dev_addr,
220 mbx.nic_cfg.mac_addr);
221 nic->sqs_mode = mbx.nic_cfg.sqs_mode;
222 nic->loopback_supported = mbx.nic_cfg.loopback_supported;
223 nic->link_up = false;
224 nic->duplex = 0;
225 nic->speed = 0;
226 break;
227 case NIC_MBOX_MSG_ACK:
228 nic->pf_acked = true;
229 break;
230 case NIC_MBOX_MSG_NACK:
231 nic->pf_nacked = true;
232 break;
233 case NIC_MBOX_MSG_RSS_SIZE:
234 nic->rss_info.rss_size = mbx.rss_size.ind_tbl_size;
235 nic->pf_acked = true;
236 break;
237 case NIC_MBOX_MSG_BGX_STATS:
238 nicvf_read_bgx_stats(nic, &mbx.bgx_stats);
239 nic->pf_acked = true;
240 break;
241 case NIC_MBOX_MSG_BGX_LINK_CHANGE:
242 nic->pf_acked = true;
243 nic->link_up = mbx.link_status.link_up;
244 nic->duplex = mbx.link_status.duplex;
245 nic->speed = mbx.link_status.speed;
246 nic->mac_type = mbx.link_status.mac_type;
247 if (nic->link_up) {
248 netdev_info(nic->netdev, "Link is Up %d Mbps %s duplex\n",
249 nic->speed,
250 nic->duplex == DUPLEX_FULL ?
251 "Full" : "Half");
252 netif_carrier_on(nic->netdev);
253 netif_tx_start_all_queues(nic->netdev);
254 } else {
255 netdev_info(nic->netdev, "Link is Down\n");
256 netif_carrier_off(nic->netdev);
257 netif_tx_stop_all_queues(nic->netdev);
258 }
259 break;
260 case NIC_MBOX_MSG_ALLOC_SQS:
261 nic->sqs_count = mbx.sqs_alloc.qs_count;
262 nic->pf_acked = true;
263 break;
264 case NIC_MBOX_MSG_SNICVF_PTR:
265 /* Primary VF: make note of secondary VF's pointer
266 * to be used while packet transmission.
267 */
268 nic->snicvf[mbx.nicvf.sqs_id] =
269 (struct nicvf *)mbx.nicvf.nicvf;
270 nic->pf_acked = true;
271 break;
272 case NIC_MBOX_MSG_PNICVF_PTR:
273 /* Secondary VF/Qset: make note of primary VF's pointer
274 * to be used while packet reception, to handover packet
275 * to primary VF's netdev.
276 */
277 nic->pnicvf = (struct nicvf *)mbx.nicvf.nicvf;
278 nic->pf_acked = true;
279 break;
280 case NIC_MBOX_MSG_PFC:
281 nic->pfc.autoneg = mbx.pfc.autoneg;
282 nic->pfc.fc_rx = mbx.pfc.fc_rx;
283 nic->pfc.fc_tx = mbx.pfc.fc_tx;
284 nic->pf_acked = true;
285 break;
286 default:
287 netdev_err(nic->netdev,
288 "Invalid message from PF, msg 0x%x\n", mbx.msg.msg);
289 break;
290 }
291 nicvf_clear_intr(nic, NICVF_INTR_MBOX, 0);
292 }
293
nicvf_hw_set_mac_addr(struct nicvf * nic,struct net_device * netdev)294 static int nicvf_hw_set_mac_addr(struct nicvf *nic, struct net_device *netdev)
295 {
296 union nic_mbx mbx = {};
297
298 mbx.mac.msg = NIC_MBOX_MSG_SET_MAC;
299 mbx.mac.vf_id = nic->vf_id;
300 ether_addr_copy(mbx.mac.mac_addr, netdev->dev_addr);
301
302 return nicvf_send_msg_to_pf(nic, &mbx);
303 }
304
nicvf_config_cpi(struct nicvf * nic)305 static void nicvf_config_cpi(struct nicvf *nic)
306 {
307 union nic_mbx mbx = {};
308
309 mbx.cpi_cfg.msg = NIC_MBOX_MSG_CPI_CFG;
310 mbx.cpi_cfg.vf_id = nic->vf_id;
311 mbx.cpi_cfg.cpi_alg = nic->cpi_alg;
312 mbx.cpi_cfg.rq_cnt = nic->qs->rq_cnt;
313
314 nicvf_send_msg_to_pf(nic, &mbx);
315 }
316
nicvf_get_rss_size(struct nicvf * nic)317 static void nicvf_get_rss_size(struct nicvf *nic)
318 {
319 union nic_mbx mbx = {};
320
321 mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
322 mbx.rss_size.vf_id = nic->vf_id;
323 nicvf_send_msg_to_pf(nic, &mbx);
324 }
325
nicvf_config_rss(struct nicvf * nic)326 void nicvf_config_rss(struct nicvf *nic)
327 {
328 union nic_mbx mbx = {};
329 struct nicvf_rss_info *rss = &nic->rss_info;
330 int ind_tbl_len = rss->rss_size;
331 int i, nextq = 0;
332
333 mbx.rss_cfg.vf_id = nic->vf_id;
334 mbx.rss_cfg.hash_bits = rss->hash_bits;
335 while (ind_tbl_len) {
336 mbx.rss_cfg.tbl_offset = nextq;
337 mbx.rss_cfg.tbl_len = min(ind_tbl_len,
338 RSS_IND_TBL_LEN_PER_MBX_MSG);
339 mbx.rss_cfg.msg = mbx.rss_cfg.tbl_offset ?
340 NIC_MBOX_MSG_RSS_CFG_CONT : NIC_MBOX_MSG_RSS_CFG;
341
342 for (i = 0; i < mbx.rss_cfg.tbl_len; i++)
343 mbx.rss_cfg.ind_tbl[i] = rss->ind_tbl[nextq++];
344
345 nicvf_send_msg_to_pf(nic, &mbx);
346
347 ind_tbl_len -= mbx.rss_cfg.tbl_len;
348 }
349 }
350
nicvf_set_rss_key(struct nicvf * nic)351 void nicvf_set_rss_key(struct nicvf *nic)
352 {
353 struct nicvf_rss_info *rss = &nic->rss_info;
354 u64 key_addr = NIC_VNIC_RSS_KEY_0_4;
355 int idx;
356
357 for (idx = 0; idx < RSS_HASH_KEY_SIZE; idx++) {
358 nicvf_reg_write(nic, key_addr, rss->key[idx]);
359 key_addr += sizeof(u64);
360 }
361 }
362
nicvf_rss_init(struct nicvf * nic)363 static int nicvf_rss_init(struct nicvf *nic)
364 {
365 struct nicvf_rss_info *rss = &nic->rss_info;
366 int idx;
367
368 nicvf_get_rss_size(nic);
369
370 if (cpi_alg != CPI_ALG_NONE) {
371 rss->enable = false;
372 rss->hash_bits = 0;
373 return 0;
374 }
375
376 rss->enable = true;
377
378 netdev_rss_key_fill(rss->key, RSS_HASH_KEY_SIZE * sizeof(u64));
379 nicvf_set_rss_key(nic);
380
381 rss->cfg = RSS_IP_HASH_ENA | RSS_TCP_HASH_ENA | RSS_UDP_HASH_ENA;
382 nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss->cfg);
383
384 rss->hash_bits = ilog2(rounddown_pow_of_two(rss->rss_size));
385
386 for (idx = 0; idx < rss->rss_size; idx++)
387 rss->ind_tbl[idx] = ethtool_rxfh_indir_default(idx,
388 nic->rx_queues);
389 nicvf_config_rss(nic);
390 return 1;
391 }
392
393 /* Request PF to allocate additional Qsets */
nicvf_request_sqs(struct nicvf * nic)394 static void nicvf_request_sqs(struct nicvf *nic)
395 {
396 union nic_mbx mbx = {};
397 int sqs;
398 int sqs_count = nic->sqs_count;
399 int rx_queues = 0, tx_queues = 0;
400
401 /* Only primary VF should request */
402 if (nic->sqs_mode || !nic->sqs_count)
403 return;
404
405 mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
406 mbx.sqs_alloc.vf_id = nic->vf_id;
407 mbx.sqs_alloc.qs_count = nic->sqs_count;
408 if (nicvf_send_msg_to_pf(nic, &mbx)) {
409 /* No response from PF */
410 nic->sqs_count = 0;
411 return;
412 }
413
414 /* Return if no Secondary Qsets available */
415 if (!nic->sqs_count)
416 return;
417
418 if (nic->rx_queues > MAX_RCV_QUEUES_PER_QS)
419 rx_queues = nic->rx_queues - MAX_RCV_QUEUES_PER_QS;
420
421 tx_queues = nic->tx_queues + nic->xdp_tx_queues;
422 if (tx_queues > MAX_SND_QUEUES_PER_QS)
423 tx_queues = tx_queues - MAX_SND_QUEUES_PER_QS;
424
425 /* Set no of Rx/Tx queues in each of the SQsets */
426 for (sqs = 0; sqs < nic->sqs_count; sqs++) {
427 mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
428 mbx.nicvf.vf_id = nic->vf_id;
429 mbx.nicvf.sqs_id = sqs;
430 nicvf_send_msg_to_pf(nic, &mbx);
431
432 nic->snicvf[sqs]->sqs_id = sqs;
433 if (rx_queues > MAX_RCV_QUEUES_PER_QS) {
434 nic->snicvf[sqs]->qs->rq_cnt = MAX_RCV_QUEUES_PER_QS;
435 rx_queues -= MAX_RCV_QUEUES_PER_QS;
436 } else {
437 nic->snicvf[sqs]->qs->rq_cnt = rx_queues;
438 rx_queues = 0;
439 }
440
441 if (tx_queues > MAX_SND_QUEUES_PER_QS) {
442 nic->snicvf[sqs]->qs->sq_cnt = MAX_SND_QUEUES_PER_QS;
443 tx_queues -= MAX_SND_QUEUES_PER_QS;
444 } else {
445 nic->snicvf[sqs]->qs->sq_cnt = tx_queues;
446 tx_queues = 0;
447 }
448
449 nic->snicvf[sqs]->qs->cq_cnt =
450 max(nic->snicvf[sqs]->qs->rq_cnt, nic->snicvf[sqs]->qs->sq_cnt);
451
452 /* Initialize secondary Qset's queues and its interrupts */
453 nicvf_open(nic->snicvf[sqs]->netdev);
454 }
455
456 /* Update stack with actual Rx/Tx queue count allocated */
457 if (sqs_count != nic->sqs_count)
458 nicvf_set_real_num_queues(nic->netdev,
459 nic->tx_queues, nic->rx_queues);
460 }
461
462 /* Send this Qset's nicvf pointer to PF.
463 * PF inturn sends primary VF's nicvf struct to secondary Qsets/VFs
464 * so that packets received by these Qsets can use primary VF's netdev
465 */
nicvf_send_vf_struct(struct nicvf * nic)466 static void nicvf_send_vf_struct(struct nicvf *nic)
467 {
468 union nic_mbx mbx = {};
469
470 mbx.nicvf.msg = NIC_MBOX_MSG_NICVF_PTR;
471 mbx.nicvf.sqs_mode = nic->sqs_mode;
472 mbx.nicvf.nicvf = (u64)nic;
473 nicvf_send_msg_to_pf(nic, &mbx);
474 }
475
nicvf_get_primary_vf_struct(struct nicvf * nic)476 static void nicvf_get_primary_vf_struct(struct nicvf *nic)
477 {
478 union nic_mbx mbx = {};
479
480 mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
481 nicvf_send_msg_to_pf(nic, &mbx);
482 }
483
nicvf_set_real_num_queues(struct net_device * netdev,int tx_queues,int rx_queues)484 int nicvf_set_real_num_queues(struct net_device *netdev,
485 int tx_queues, int rx_queues)
486 {
487 int err = 0;
488
489 err = netif_set_real_num_tx_queues(netdev, tx_queues);
490 if (err) {
491 netdev_err(netdev,
492 "Failed to set no of Tx queues: %d\n", tx_queues);
493 return err;
494 }
495
496 err = netif_set_real_num_rx_queues(netdev, rx_queues);
497 if (err)
498 netdev_err(netdev,
499 "Failed to set no of Rx queues: %d\n", rx_queues);
500 return err;
501 }
502
nicvf_init_resources(struct nicvf * nic)503 static int nicvf_init_resources(struct nicvf *nic)
504 {
505 int err;
506
507 /* Enable Qset */
508 nicvf_qset_config(nic, true);
509
510 /* Initialize queues and HW for data transfer */
511 err = nicvf_config_data_transfer(nic, true);
512 if (err) {
513 netdev_err(nic->netdev,
514 "Failed to alloc/config VF's QSet resources\n");
515 return err;
516 }
517
518 return 0;
519 }
520
nicvf_xdp_rx(struct nicvf * nic,struct bpf_prog * prog,struct cqe_rx_t * cqe_rx,struct snd_queue * sq,struct sk_buff ** skb)521 static inline bool nicvf_xdp_rx(struct nicvf *nic, struct bpf_prog *prog,
522 struct cqe_rx_t *cqe_rx, struct snd_queue *sq,
523 struct sk_buff **skb)
524 {
525 struct xdp_buff xdp;
526 struct page *page;
527 u32 action;
528 u16 len, offset = 0;
529 u64 dma_addr, cpu_addr;
530 void *orig_data;
531
532 /* Retrieve packet buffer's DMA address and length */
533 len = *((u16 *)((void *)cqe_rx + (3 * sizeof(u64))));
534 dma_addr = *((u64 *)((void *)cqe_rx + (7 * sizeof(u64))));
535
536 cpu_addr = nicvf_iova_to_phys(nic, dma_addr);
537 if (!cpu_addr)
538 return false;
539 cpu_addr = (u64)phys_to_virt(cpu_addr);
540 page = virt_to_page((void *)cpu_addr);
541
542 xdp.data_hard_start = page_address(page);
543 xdp.data = (void *)cpu_addr;
544 xdp.data_end = xdp.data + len;
545 orig_data = xdp.data;
546
547 rcu_read_lock();
548 action = bpf_prog_run_xdp(prog, &xdp);
549 rcu_read_unlock();
550
551 /* Check if XDP program has changed headers */
552 if (orig_data != xdp.data) {
553 len = xdp.data_end - xdp.data;
554 offset = orig_data - xdp.data;
555 dma_addr -= offset;
556 }
557
558 switch (action) {
559 case XDP_PASS:
560 /* Check if it's a recycled page, if not
561 * unmap the DMA mapping.
562 *
563 * Recycled page holds an extra reference.
564 */
565 if (page_ref_count(page) == 1) {
566 dma_addr &= PAGE_MASK;
567 dma_unmap_page_attrs(&nic->pdev->dev, dma_addr,
568 RCV_FRAG_LEN + XDP_PACKET_HEADROOM,
569 DMA_FROM_DEVICE,
570 DMA_ATTR_SKIP_CPU_SYNC);
571 }
572
573 /* Build SKB and pass on packet to network stack */
574 *skb = build_skb(xdp.data,
575 RCV_FRAG_LEN - cqe_rx->align_pad + offset);
576 if (!*skb)
577 put_page(page);
578 else
579 skb_put(*skb, len);
580 return false;
581 case XDP_TX:
582 nicvf_xdp_sq_append_pkt(nic, sq, (u64)xdp.data, dma_addr, len);
583 return true;
584 default:
585 bpf_warn_invalid_xdp_action(action);
586 /* fall through */
587 case XDP_ABORTED:
588 trace_xdp_exception(nic->netdev, prog, action);
589 /* fall through */
590 case XDP_DROP:
591 /* Check if it's a recycled page, if not
592 * unmap the DMA mapping.
593 *
594 * Recycled page holds an extra reference.
595 */
596 if (page_ref_count(page) == 1) {
597 dma_addr &= PAGE_MASK;
598 dma_unmap_page_attrs(&nic->pdev->dev, dma_addr,
599 RCV_FRAG_LEN + XDP_PACKET_HEADROOM,
600 DMA_FROM_DEVICE,
601 DMA_ATTR_SKIP_CPU_SYNC);
602 }
603 put_page(page);
604 return true;
605 }
606 return false;
607 }
608
nicvf_snd_pkt_handler(struct net_device * netdev,struct cqe_send_t * cqe_tx,int budget,int * subdesc_cnt,unsigned int * tx_pkts,unsigned int * tx_bytes)609 static void nicvf_snd_pkt_handler(struct net_device *netdev,
610 struct cqe_send_t *cqe_tx,
611 int budget, int *subdesc_cnt,
612 unsigned int *tx_pkts, unsigned int *tx_bytes)
613 {
614 struct sk_buff *skb = NULL;
615 struct page *page;
616 struct nicvf *nic = netdev_priv(netdev);
617 struct snd_queue *sq;
618 struct sq_hdr_subdesc *hdr;
619 struct sq_hdr_subdesc *tso_sqe;
620
621 sq = &nic->qs->sq[cqe_tx->sq_idx];
622
623 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, cqe_tx->sqe_ptr);
624 if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER)
625 return;
626
627 /* Check for errors */
628 if (cqe_tx->send_status)
629 nicvf_check_cqe_tx_errs(nic->pnicvf, cqe_tx);
630
631 /* Is this a XDP designated Tx queue */
632 if (sq->is_xdp) {
633 page = (struct page *)sq->xdp_page[cqe_tx->sqe_ptr];
634 /* Check if it's recycled page or else unmap DMA mapping */
635 if (page && (page_ref_count(page) == 1))
636 nicvf_unmap_sndq_buffers(nic, sq, cqe_tx->sqe_ptr,
637 hdr->subdesc_cnt);
638
639 /* Release page reference for recycling */
640 if (page)
641 put_page(page);
642 sq->xdp_page[cqe_tx->sqe_ptr] = (u64)NULL;
643 *subdesc_cnt += hdr->subdesc_cnt + 1;
644 return;
645 }
646
647 skb = (struct sk_buff *)sq->skbuff[cqe_tx->sqe_ptr];
648 if (skb) {
649 /* Check for dummy descriptor used for HW TSO offload on 88xx */
650 if (hdr->dont_send) {
651 /* Get actual TSO descriptors and free them */
652 tso_sqe =
653 (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, hdr->rsvd2);
654 nicvf_unmap_sndq_buffers(nic, sq, hdr->rsvd2,
655 tso_sqe->subdesc_cnt);
656 *subdesc_cnt += tso_sqe->subdesc_cnt + 1;
657 } else {
658 nicvf_unmap_sndq_buffers(nic, sq, cqe_tx->sqe_ptr,
659 hdr->subdesc_cnt);
660 }
661 *subdesc_cnt += hdr->subdesc_cnt + 1;
662 prefetch(skb);
663 (*tx_pkts)++;
664 *tx_bytes += skb->len;
665 napi_consume_skb(skb, budget);
666 sq->skbuff[cqe_tx->sqe_ptr] = (u64)NULL;
667 } else {
668 /* In case of SW TSO on 88xx, only last segment will have
669 * a SKB attached, so just free SQEs here.
670 */
671 if (!nic->hw_tso)
672 *subdesc_cnt += hdr->subdesc_cnt + 1;
673 }
674 }
675
nicvf_set_rxhash(struct net_device * netdev,struct cqe_rx_t * cqe_rx,struct sk_buff * skb)676 static inline void nicvf_set_rxhash(struct net_device *netdev,
677 struct cqe_rx_t *cqe_rx,
678 struct sk_buff *skb)
679 {
680 u8 hash_type;
681 u32 hash;
682
683 if (!(netdev->features & NETIF_F_RXHASH))
684 return;
685
686 switch (cqe_rx->rss_alg) {
687 case RSS_ALG_TCP_IP:
688 case RSS_ALG_UDP_IP:
689 hash_type = PKT_HASH_TYPE_L4;
690 hash = cqe_rx->rss_tag;
691 break;
692 case RSS_ALG_IP:
693 hash_type = PKT_HASH_TYPE_L3;
694 hash = cqe_rx->rss_tag;
695 break;
696 default:
697 hash_type = PKT_HASH_TYPE_NONE;
698 hash = 0;
699 }
700
701 skb_set_hash(skb, hash, hash_type);
702 }
703
nicvf_rcv_pkt_handler(struct net_device * netdev,struct napi_struct * napi,struct cqe_rx_t * cqe_rx,struct snd_queue * sq)704 static void nicvf_rcv_pkt_handler(struct net_device *netdev,
705 struct napi_struct *napi,
706 struct cqe_rx_t *cqe_rx, struct snd_queue *sq)
707 {
708 struct sk_buff *skb = NULL;
709 struct nicvf *nic = netdev_priv(netdev);
710 struct nicvf *snic = nic;
711 int err = 0;
712 int rq_idx;
713
714 rq_idx = nicvf_netdev_qidx(nic, cqe_rx->rq_idx);
715
716 if (nic->sqs_mode) {
717 /* Use primary VF's 'nicvf' struct */
718 nic = nic->pnicvf;
719 netdev = nic->netdev;
720 }
721
722 /* Check for errors */
723 if (cqe_rx->err_level || cqe_rx->err_opcode) {
724 err = nicvf_check_cqe_rx_errs(nic, cqe_rx);
725 if (err && !cqe_rx->rb_cnt)
726 return;
727 }
728
729 /* For XDP, ignore pkts spanning multiple pages */
730 if (nic->xdp_prog && (cqe_rx->rb_cnt == 1)) {
731 /* Packet consumed by XDP */
732 if (nicvf_xdp_rx(snic, nic->xdp_prog, cqe_rx, sq, &skb))
733 return;
734 } else {
735 skb = nicvf_get_rcv_skb(snic, cqe_rx,
736 nic->xdp_prog ? true : false);
737 }
738
739 if (!skb)
740 return;
741
742 if (netif_msg_pktdata(nic)) {
743 netdev_info(nic->netdev, "skb 0x%p, len=%d\n", skb, skb->len);
744 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
745 skb->data, skb->len, true);
746 }
747
748 /* If error packet, drop it here */
749 if (err) {
750 dev_kfree_skb_any(skb);
751 return;
752 }
753
754 nicvf_set_rxhash(netdev, cqe_rx, skb);
755
756 skb_record_rx_queue(skb, rq_idx);
757 if (netdev->hw_features & NETIF_F_RXCSUM) {
758 /* HW by default verifies TCP/UDP/SCTP checksums */
759 skb->ip_summed = CHECKSUM_UNNECESSARY;
760 } else {
761 skb_checksum_none_assert(skb);
762 }
763
764 skb->protocol = eth_type_trans(skb, netdev);
765
766 /* Check for stripped VLAN */
767 if (cqe_rx->vlan_found && cqe_rx->vlan_stripped)
768 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
769 ntohs((__force __be16)cqe_rx->vlan_tci));
770
771 if (napi && (netdev->features & NETIF_F_GRO))
772 napi_gro_receive(napi, skb);
773 else
774 netif_receive_skb(skb);
775 }
776
nicvf_cq_intr_handler(struct net_device * netdev,u8 cq_idx,struct napi_struct * napi,int budget)777 static int nicvf_cq_intr_handler(struct net_device *netdev, u8 cq_idx,
778 struct napi_struct *napi, int budget)
779 {
780 int processed_cqe, work_done = 0, tx_done = 0;
781 int cqe_count, cqe_head;
782 int subdesc_cnt = 0;
783 struct nicvf *nic = netdev_priv(netdev);
784 struct queue_set *qs = nic->qs;
785 struct cmp_queue *cq = &qs->cq[cq_idx];
786 struct cqe_rx_t *cq_desc;
787 struct netdev_queue *txq;
788 struct snd_queue *sq = &qs->sq[cq_idx];
789 unsigned int tx_pkts = 0, tx_bytes = 0, txq_idx;
790
791 spin_lock_bh(&cq->lock);
792 loop:
793 processed_cqe = 0;
794 /* Get no of valid CQ entries to process */
795 cqe_count = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, cq_idx);
796 cqe_count &= CQ_CQE_COUNT;
797 if (!cqe_count)
798 goto done;
799
800 /* Get head of the valid CQ entries */
801 cqe_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, cq_idx) >> 9;
802 cqe_head &= 0xFFFF;
803
804 while (processed_cqe < cqe_count) {
805 /* Get the CQ descriptor */
806 cq_desc = (struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head);
807 cqe_head++;
808 cqe_head &= (cq->dmem.q_len - 1);
809 /* Initiate prefetch for next descriptor */
810 prefetch((struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head));
811
812 if ((work_done >= budget) && napi &&
813 (cq_desc->cqe_type != CQE_TYPE_SEND)) {
814 break;
815 }
816
817 switch (cq_desc->cqe_type) {
818 case CQE_TYPE_RX:
819 nicvf_rcv_pkt_handler(netdev, napi, cq_desc, sq);
820 work_done++;
821 break;
822 case CQE_TYPE_SEND:
823 nicvf_snd_pkt_handler(netdev, (void *)cq_desc,
824 budget, &subdesc_cnt,
825 &tx_pkts, &tx_bytes);
826 tx_done++;
827 break;
828 case CQE_TYPE_INVALID:
829 case CQE_TYPE_RX_SPLIT:
830 case CQE_TYPE_RX_TCP:
831 case CQE_TYPE_SEND_PTP:
832 /* Ignore for now */
833 break;
834 }
835 processed_cqe++;
836 }
837
838 /* Ring doorbell to inform H/W to reuse processed CQEs */
839 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_DOOR,
840 cq_idx, processed_cqe);
841
842 if ((work_done < budget) && napi)
843 goto loop;
844
845 done:
846 /* Update SQ's descriptor free count */
847 if (subdesc_cnt)
848 nicvf_put_sq_desc(sq, subdesc_cnt);
849
850 txq_idx = nicvf_netdev_qidx(nic, cq_idx);
851 /* Handle XDP TX queues */
852 if (nic->pnicvf->xdp_prog) {
853 if (txq_idx < nic->pnicvf->xdp_tx_queues) {
854 nicvf_xdp_sq_doorbell(nic, sq, cq_idx);
855 goto out;
856 }
857 nic = nic->pnicvf;
858 txq_idx -= nic->pnicvf->xdp_tx_queues;
859 }
860
861 /* Wakeup TXQ if its stopped earlier due to SQ full */
862 if (tx_done ||
863 (atomic_read(&sq->free_cnt) >= MIN_SQ_DESC_PER_PKT_XMIT)) {
864 netdev = nic->pnicvf->netdev;
865 txq = netdev_get_tx_queue(netdev, txq_idx);
866 if (tx_pkts)
867 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
868
869 /* To read updated queue and carrier status */
870 smp_mb();
871 if (netif_tx_queue_stopped(txq) && netif_carrier_ok(netdev)) {
872 netif_tx_wake_queue(txq);
873 nic = nic->pnicvf;
874 this_cpu_inc(nic->drv_stats->txq_wake);
875 netif_warn(nic, tx_err, netdev,
876 "Transmit queue wakeup SQ%d\n", txq_idx);
877 }
878 }
879
880 out:
881 spin_unlock_bh(&cq->lock);
882 return work_done;
883 }
884
nicvf_poll(struct napi_struct * napi,int budget)885 static int nicvf_poll(struct napi_struct *napi, int budget)
886 {
887 u64 cq_head;
888 int work_done = 0;
889 struct net_device *netdev = napi->dev;
890 struct nicvf *nic = netdev_priv(netdev);
891 struct nicvf_cq_poll *cq;
892
893 cq = container_of(napi, struct nicvf_cq_poll, napi);
894 work_done = nicvf_cq_intr_handler(netdev, cq->cq_idx, napi, budget);
895
896 if (work_done < budget) {
897 /* Slow packet rate, exit polling */
898 napi_complete_done(napi, work_done);
899 /* Re-enable interrupts */
900 cq_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD,
901 cq->cq_idx);
902 nicvf_clear_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
903 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_HEAD,
904 cq->cq_idx, cq_head);
905 nicvf_enable_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
906 }
907 return work_done;
908 }
909
910 /* Qset error interrupt handler
911 *
912 * As of now only CQ errors are handled
913 */
nicvf_handle_qs_err(unsigned long data)914 static void nicvf_handle_qs_err(unsigned long data)
915 {
916 struct nicvf *nic = (struct nicvf *)data;
917 struct queue_set *qs = nic->qs;
918 int qidx;
919 u64 status;
920
921 netif_tx_disable(nic->netdev);
922
923 /* Check if it is CQ err */
924 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
925 status = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS,
926 qidx);
927 if (!(status & CQ_ERR_MASK))
928 continue;
929 /* Process already queued CQEs and reconfig CQ */
930 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
931 nicvf_sq_disable(nic, qidx);
932 nicvf_cq_intr_handler(nic->netdev, qidx, NULL, 0);
933 nicvf_cmp_queue_config(nic, qs, qidx, true);
934 nicvf_sq_free_used_descs(nic->netdev, &qs->sq[qidx], qidx);
935 nicvf_sq_enable(nic, &qs->sq[qidx], qidx);
936
937 nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
938 }
939
940 netif_tx_start_all_queues(nic->netdev);
941 /* Re-enable Qset error interrupt */
942 nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
943 }
944
nicvf_dump_intr_status(struct nicvf * nic)945 static void nicvf_dump_intr_status(struct nicvf *nic)
946 {
947 netif_info(nic, intr, nic->netdev, "interrupt status 0x%llx\n",
948 nicvf_reg_read(nic, NIC_VF_INT));
949 }
950
nicvf_misc_intr_handler(int irq,void * nicvf_irq)951 static irqreturn_t nicvf_misc_intr_handler(int irq, void *nicvf_irq)
952 {
953 struct nicvf *nic = (struct nicvf *)nicvf_irq;
954 u64 intr;
955
956 nicvf_dump_intr_status(nic);
957
958 intr = nicvf_reg_read(nic, NIC_VF_INT);
959 /* Check for spurious interrupt */
960 if (!(intr & NICVF_INTR_MBOX_MASK))
961 return IRQ_HANDLED;
962
963 nicvf_handle_mbx_intr(nic);
964
965 return IRQ_HANDLED;
966 }
967
nicvf_intr_handler(int irq,void * cq_irq)968 static irqreturn_t nicvf_intr_handler(int irq, void *cq_irq)
969 {
970 struct nicvf_cq_poll *cq_poll = (struct nicvf_cq_poll *)cq_irq;
971 struct nicvf *nic = cq_poll->nicvf;
972 int qidx = cq_poll->cq_idx;
973
974 nicvf_dump_intr_status(nic);
975
976 /* Disable interrupts */
977 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
978
979 /* Schedule NAPI */
980 napi_schedule_irqoff(&cq_poll->napi);
981
982 /* Clear interrupt */
983 nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
984
985 return IRQ_HANDLED;
986 }
987
nicvf_rbdr_intr_handler(int irq,void * nicvf_irq)988 static irqreturn_t nicvf_rbdr_intr_handler(int irq, void *nicvf_irq)
989 {
990 struct nicvf *nic = (struct nicvf *)nicvf_irq;
991 u8 qidx;
992
993
994 nicvf_dump_intr_status(nic);
995
996 /* Disable RBDR interrupt and schedule softirq */
997 for (qidx = 0; qidx < nic->qs->rbdr_cnt; qidx++) {
998 if (!nicvf_is_intr_enabled(nic, NICVF_INTR_RBDR, qidx))
999 continue;
1000 nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
1001 tasklet_hi_schedule(&nic->rbdr_task);
1002 /* Clear interrupt */
1003 nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
1004 }
1005
1006 return IRQ_HANDLED;
1007 }
1008
nicvf_qs_err_intr_handler(int irq,void * nicvf_irq)1009 static irqreturn_t nicvf_qs_err_intr_handler(int irq, void *nicvf_irq)
1010 {
1011 struct nicvf *nic = (struct nicvf *)nicvf_irq;
1012
1013 nicvf_dump_intr_status(nic);
1014
1015 /* Disable Qset err interrupt and schedule softirq */
1016 nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
1017 tasklet_hi_schedule(&nic->qs_err_task);
1018 nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
1019
1020 return IRQ_HANDLED;
1021 }
1022
nicvf_set_irq_affinity(struct nicvf * nic)1023 static void nicvf_set_irq_affinity(struct nicvf *nic)
1024 {
1025 int vec, cpu;
1026
1027 for (vec = 0; vec < nic->num_vec; vec++) {
1028 if (!nic->irq_allocated[vec])
1029 continue;
1030
1031 if (!zalloc_cpumask_var(&nic->affinity_mask[vec], GFP_KERNEL))
1032 return;
1033 /* CQ interrupts */
1034 if (vec < NICVF_INTR_ID_SQ)
1035 /* Leave CPU0 for RBDR and other interrupts */
1036 cpu = nicvf_netdev_qidx(nic, vec) + 1;
1037 else
1038 cpu = 0;
1039
1040 cpumask_set_cpu(cpumask_local_spread(cpu, nic->node),
1041 nic->affinity_mask[vec]);
1042 irq_set_affinity_hint(pci_irq_vector(nic->pdev, vec),
1043 nic->affinity_mask[vec]);
1044 }
1045 }
1046
nicvf_register_interrupts(struct nicvf * nic)1047 static int nicvf_register_interrupts(struct nicvf *nic)
1048 {
1049 int irq, ret = 0;
1050
1051 for_each_cq_irq(irq)
1052 sprintf(nic->irq_name[irq], "%s-rxtx-%d",
1053 nic->pnicvf->netdev->name,
1054 nicvf_netdev_qidx(nic, irq));
1055
1056 for_each_sq_irq(irq)
1057 sprintf(nic->irq_name[irq], "%s-sq-%d",
1058 nic->pnicvf->netdev->name,
1059 nicvf_netdev_qidx(nic, irq - NICVF_INTR_ID_SQ));
1060
1061 for_each_rbdr_irq(irq)
1062 sprintf(nic->irq_name[irq], "%s-rbdr-%d",
1063 nic->pnicvf->netdev->name,
1064 nic->sqs_mode ? (nic->sqs_id + 1) : 0);
1065
1066 /* Register CQ interrupts */
1067 for (irq = 0; irq < nic->qs->cq_cnt; irq++) {
1068 ret = request_irq(pci_irq_vector(nic->pdev, irq),
1069 nicvf_intr_handler,
1070 0, nic->irq_name[irq], nic->napi[irq]);
1071 if (ret)
1072 goto err;
1073 nic->irq_allocated[irq] = true;
1074 }
1075
1076 /* Register RBDR interrupt */
1077 for (irq = NICVF_INTR_ID_RBDR;
1078 irq < (NICVF_INTR_ID_RBDR + nic->qs->rbdr_cnt); irq++) {
1079 ret = request_irq(pci_irq_vector(nic->pdev, irq),
1080 nicvf_rbdr_intr_handler,
1081 0, nic->irq_name[irq], nic);
1082 if (ret)
1083 goto err;
1084 nic->irq_allocated[irq] = true;
1085 }
1086
1087 /* Register QS error interrupt */
1088 sprintf(nic->irq_name[NICVF_INTR_ID_QS_ERR], "%s-qset-err-%d",
1089 nic->pnicvf->netdev->name,
1090 nic->sqs_mode ? (nic->sqs_id + 1) : 0);
1091 irq = NICVF_INTR_ID_QS_ERR;
1092 ret = request_irq(pci_irq_vector(nic->pdev, irq),
1093 nicvf_qs_err_intr_handler,
1094 0, nic->irq_name[irq], nic);
1095 if (ret)
1096 goto err;
1097
1098 nic->irq_allocated[irq] = true;
1099
1100 /* Set IRQ affinities */
1101 nicvf_set_irq_affinity(nic);
1102
1103 err:
1104 if (ret)
1105 netdev_err(nic->netdev, "request_irq failed, vector %d\n", irq);
1106
1107 return ret;
1108 }
1109
nicvf_unregister_interrupts(struct nicvf * nic)1110 static void nicvf_unregister_interrupts(struct nicvf *nic)
1111 {
1112 struct pci_dev *pdev = nic->pdev;
1113 int irq;
1114
1115 /* Free registered interrupts */
1116 for (irq = 0; irq < nic->num_vec; irq++) {
1117 if (!nic->irq_allocated[irq])
1118 continue;
1119
1120 irq_set_affinity_hint(pci_irq_vector(pdev, irq), NULL);
1121 free_cpumask_var(nic->affinity_mask[irq]);
1122
1123 if (irq < NICVF_INTR_ID_SQ)
1124 free_irq(pci_irq_vector(pdev, irq), nic->napi[irq]);
1125 else
1126 free_irq(pci_irq_vector(pdev, irq), nic);
1127
1128 nic->irq_allocated[irq] = false;
1129 }
1130
1131 /* Disable MSI-X */
1132 pci_free_irq_vectors(pdev);
1133 nic->num_vec = 0;
1134 }
1135
1136 /* Initialize MSIX vectors and register MISC interrupt.
1137 * Send READY message to PF to check if its alive
1138 */
nicvf_register_misc_interrupt(struct nicvf * nic)1139 static int nicvf_register_misc_interrupt(struct nicvf *nic)
1140 {
1141 int ret = 0;
1142 int irq = NICVF_INTR_ID_MISC;
1143
1144 /* Return if mailbox interrupt is already registered */
1145 if (nic->pdev->msix_enabled)
1146 return 0;
1147
1148 /* Enable MSI-X */
1149 nic->num_vec = pci_msix_vec_count(nic->pdev);
1150 ret = pci_alloc_irq_vectors(nic->pdev, nic->num_vec, nic->num_vec,
1151 PCI_IRQ_MSIX);
1152 if (ret < 0) {
1153 netdev_err(nic->netdev,
1154 "Req for #%d msix vectors failed\n", nic->num_vec);
1155 return 1;
1156 }
1157
1158 sprintf(nic->irq_name[irq], "%s Mbox", "NICVF");
1159 /* Register Misc interrupt */
1160 ret = request_irq(pci_irq_vector(nic->pdev, irq),
1161 nicvf_misc_intr_handler, 0, nic->irq_name[irq], nic);
1162
1163 if (ret)
1164 return ret;
1165 nic->irq_allocated[irq] = true;
1166
1167 /* Enable mailbox interrupt */
1168 nicvf_enable_intr(nic, NICVF_INTR_MBOX, 0);
1169
1170 /* Check if VF is able to communicate with PF */
1171 if (!nicvf_check_pf_ready(nic)) {
1172 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
1173 nicvf_unregister_interrupts(nic);
1174 return 1;
1175 }
1176
1177 return 0;
1178 }
1179
nicvf_xmit(struct sk_buff * skb,struct net_device * netdev)1180 static netdev_tx_t nicvf_xmit(struct sk_buff *skb, struct net_device *netdev)
1181 {
1182 struct nicvf *nic = netdev_priv(netdev);
1183 int qid = skb_get_queue_mapping(skb);
1184 struct netdev_queue *txq = netdev_get_tx_queue(netdev, qid);
1185 struct nicvf *snic;
1186 struct snd_queue *sq;
1187 int tmp;
1188
1189 /* Check for minimum packet length */
1190 if (skb->len <= ETH_HLEN) {
1191 dev_kfree_skb(skb);
1192 return NETDEV_TX_OK;
1193 }
1194
1195 /* In XDP case, initial HW tx queues are used for XDP,
1196 * but stack's queue mapping starts at '0', so skip the
1197 * Tx queues attached to Rx queues for XDP.
1198 */
1199 if (nic->xdp_prog)
1200 qid += nic->xdp_tx_queues;
1201
1202 snic = nic;
1203 /* Get secondary Qset's SQ structure */
1204 if (qid >= MAX_SND_QUEUES_PER_QS) {
1205 tmp = qid / MAX_SND_QUEUES_PER_QS;
1206 snic = (struct nicvf *)nic->snicvf[tmp - 1];
1207 if (!snic) {
1208 netdev_warn(nic->netdev,
1209 "Secondary Qset#%d's ptr not initialized\n",
1210 tmp - 1);
1211 dev_kfree_skb(skb);
1212 return NETDEV_TX_OK;
1213 }
1214 qid = qid % MAX_SND_QUEUES_PER_QS;
1215 }
1216
1217 sq = &snic->qs->sq[qid];
1218 if (!netif_tx_queue_stopped(txq) &&
1219 !nicvf_sq_append_skb(snic, sq, skb, qid)) {
1220 netif_tx_stop_queue(txq);
1221
1222 /* Barrier, so that stop_queue visible to other cpus */
1223 smp_mb();
1224
1225 /* Check again, incase another cpu freed descriptors */
1226 if (atomic_read(&sq->free_cnt) > MIN_SQ_DESC_PER_PKT_XMIT) {
1227 netif_tx_wake_queue(txq);
1228 } else {
1229 this_cpu_inc(nic->drv_stats->txq_stop);
1230 netif_warn(nic, tx_err, netdev,
1231 "Transmit ring full, stopping SQ%d\n", qid);
1232 }
1233 return NETDEV_TX_BUSY;
1234 }
1235
1236 return NETDEV_TX_OK;
1237 }
1238
nicvf_free_cq_poll(struct nicvf * nic)1239 static inline void nicvf_free_cq_poll(struct nicvf *nic)
1240 {
1241 struct nicvf_cq_poll *cq_poll;
1242 int qidx;
1243
1244 for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
1245 cq_poll = nic->napi[qidx];
1246 if (!cq_poll)
1247 continue;
1248 nic->napi[qidx] = NULL;
1249 kfree(cq_poll);
1250 }
1251 }
1252
nicvf_stop(struct net_device * netdev)1253 int nicvf_stop(struct net_device *netdev)
1254 {
1255 int irq, qidx;
1256 struct nicvf *nic = netdev_priv(netdev);
1257 struct queue_set *qs = nic->qs;
1258 struct nicvf_cq_poll *cq_poll = NULL;
1259 union nic_mbx mbx = {};
1260
1261 mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
1262 nicvf_send_msg_to_pf(nic, &mbx);
1263
1264 netif_carrier_off(netdev);
1265 netif_tx_stop_all_queues(nic->netdev);
1266 nic->link_up = false;
1267
1268 /* Teardown secondary qsets first */
1269 if (!nic->sqs_mode) {
1270 for (qidx = 0; qidx < nic->sqs_count; qidx++) {
1271 if (!nic->snicvf[qidx])
1272 continue;
1273 nicvf_stop(nic->snicvf[qidx]->netdev);
1274 nic->snicvf[qidx] = NULL;
1275 }
1276 }
1277
1278 /* Disable RBDR & QS error interrupts */
1279 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
1280 nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
1281 nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
1282 }
1283 nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
1284 nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
1285
1286 /* Wait for pending IRQ handlers to finish */
1287 for (irq = 0; irq < nic->num_vec; irq++)
1288 synchronize_irq(pci_irq_vector(nic->pdev, irq));
1289
1290 tasklet_kill(&nic->rbdr_task);
1291 tasklet_kill(&nic->qs_err_task);
1292 if (nic->rb_work_scheduled)
1293 cancel_delayed_work_sync(&nic->rbdr_work);
1294
1295 for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
1296 cq_poll = nic->napi[qidx];
1297 if (!cq_poll)
1298 continue;
1299 napi_synchronize(&cq_poll->napi);
1300 /* CQ intr is enabled while napi_complete,
1301 * so disable it now
1302 */
1303 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
1304 nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
1305 napi_disable(&cq_poll->napi);
1306 netif_napi_del(&cq_poll->napi);
1307 }
1308
1309 netif_tx_disable(netdev);
1310
1311 for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
1312 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
1313
1314 /* Free resources */
1315 nicvf_config_data_transfer(nic, false);
1316
1317 /* Disable HW Qset */
1318 nicvf_qset_config(nic, false);
1319
1320 /* disable mailbox interrupt */
1321 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
1322
1323 nicvf_unregister_interrupts(nic);
1324
1325 nicvf_free_cq_poll(nic);
1326
1327 /* Clear multiqset info */
1328 nic->pnicvf = nic;
1329
1330 return 0;
1331 }
1332
nicvf_update_hw_max_frs(struct nicvf * nic,int mtu)1333 static int nicvf_update_hw_max_frs(struct nicvf *nic, int mtu)
1334 {
1335 union nic_mbx mbx = {};
1336
1337 mbx.frs.msg = NIC_MBOX_MSG_SET_MAX_FRS;
1338 mbx.frs.max_frs = mtu;
1339 mbx.frs.vf_id = nic->vf_id;
1340
1341 return nicvf_send_msg_to_pf(nic, &mbx);
1342 }
1343
nicvf_open(struct net_device * netdev)1344 int nicvf_open(struct net_device *netdev)
1345 {
1346 int cpu, err, qidx;
1347 struct nicvf *nic = netdev_priv(netdev);
1348 struct queue_set *qs = nic->qs;
1349 struct nicvf_cq_poll *cq_poll = NULL;
1350
1351 netif_carrier_off(netdev);
1352
1353 err = nicvf_register_misc_interrupt(nic);
1354 if (err)
1355 return err;
1356
1357 /* Register NAPI handler for processing CQEs */
1358 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
1359 cq_poll = kzalloc(sizeof(*cq_poll), GFP_KERNEL);
1360 if (!cq_poll) {
1361 err = -ENOMEM;
1362 goto napi_del;
1363 }
1364 cq_poll->cq_idx = qidx;
1365 cq_poll->nicvf = nic;
1366 netif_napi_add(netdev, &cq_poll->napi, nicvf_poll,
1367 NAPI_POLL_WEIGHT);
1368 napi_enable(&cq_poll->napi);
1369 nic->napi[qidx] = cq_poll;
1370 }
1371
1372 /* Check if we got MAC address from PF or else generate a radom MAC */
1373 if (!nic->sqs_mode && is_zero_ether_addr(netdev->dev_addr)) {
1374 eth_hw_addr_random(netdev);
1375 nicvf_hw_set_mac_addr(nic, netdev);
1376 }
1377
1378 if (nic->set_mac_pending) {
1379 nic->set_mac_pending = false;
1380 nicvf_hw_set_mac_addr(nic, netdev);
1381 }
1382
1383 /* Init tasklet for handling Qset err interrupt */
1384 tasklet_init(&nic->qs_err_task, nicvf_handle_qs_err,
1385 (unsigned long)nic);
1386
1387 /* Init RBDR tasklet which will refill RBDR */
1388 tasklet_init(&nic->rbdr_task, nicvf_rbdr_task,
1389 (unsigned long)nic);
1390 INIT_DELAYED_WORK(&nic->rbdr_work, nicvf_rbdr_work);
1391
1392 /* Configure CPI alorithm */
1393 nic->cpi_alg = cpi_alg;
1394 if (!nic->sqs_mode)
1395 nicvf_config_cpi(nic);
1396
1397 nicvf_request_sqs(nic);
1398 if (nic->sqs_mode)
1399 nicvf_get_primary_vf_struct(nic);
1400
1401 /* Configure receive side scaling and MTU */
1402 if (!nic->sqs_mode) {
1403 nicvf_rss_init(nic);
1404 err = nicvf_update_hw_max_frs(nic, netdev->mtu);
1405 if (err)
1406 goto cleanup;
1407
1408 /* Clear percpu stats */
1409 for_each_possible_cpu(cpu)
1410 memset(per_cpu_ptr(nic->drv_stats, cpu), 0,
1411 sizeof(struct nicvf_drv_stats));
1412 }
1413
1414 err = nicvf_register_interrupts(nic);
1415 if (err)
1416 goto cleanup;
1417
1418 /* Initialize the queues */
1419 err = nicvf_init_resources(nic);
1420 if (err)
1421 goto cleanup;
1422
1423 /* Make sure queue initialization is written */
1424 wmb();
1425
1426 nicvf_reg_write(nic, NIC_VF_INT, -1);
1427 /* Enable Qset err interrupt */
1428 nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
1429
1430 /* Enable completion queue interrupt */
1431 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
1432 nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
1433
1434 /* Enable RBDR threshold interrupt */
1435 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
1436 nicvf_enable_intr(nic, NICVF_INTR_RBDR, qidx);
1437
1438 /* Send VF config done msg to PF */
1439 nicvf_send_cfg_done(nic);
1440
1441 return 0;
1442 cleanup:
1443 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
1444 nicvf_unregister_interrupts(nic);
1445 tasklet_kill(&nic->qs_err_task);
1446 tasklet_kill(&nic->rbdr_task);
1447 napi_del:
1448 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
1449 cq_poll = nic->napi[qidx];
1450 if (!cq_poll)
1451 continue;
1452 napi_disable(&cq_poll->napi);
1453 netif_napi_del(&cq_poll->napi);
1454 }
1455 nicvf_free_cq_poll(nic);
1456 return err;
1457 }
1458
nicvf_change_mtu(struct net_device * netdev,int new_mtu)1459 static int nicvf_change_mtu(struct net_device *netdev, int new_mtu)
1460 {
1461 struct nicvf *nic = netdev_priv(netdev);
1462 int orig_mtu = netdev->mtu;
1463
1464 /* For now just support only the usual MTU sized frames,
1465 * plus some headroom for VLAN, QinQ.
1466 */
1467 if (nic->xdp_prog && new_mtu > MAX_XDP_MTU) {
1468 netdev_warn(netdev, "Jumbo frames not yet supported with XDP, current MTU %d.\n",
1469 netdev->mtu);
1470 return -EINVAL;
1471 }
1472
1473 netdev->mtu = new_mtu;
1474
1475 if (!netif_running(netdev))
1476 return 0;
1477
1478 if (nicvf_update_hw_max_frs(nic, new_mtu)) {
1479 netdev->mtu = orig_mtu;
1480 return -EINVAL;
1481 }
1482
1483 return 0;
1484 }
1485
nicvf_set_mac_address(struct net_device * netdev,void * p)1486 static int nicvf_set_mac_address(struct net_device *netdev, void *p)
1487 {
1488 struct sockaddr *addr = p;
1489 struct nicvf *nic = netdev_priv(netdev);
1490
1491 if (!is_valid_ether_addr(addr->sa_data))
1492 return -EADDRNOTAVAIL;
1493
1494 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1495
1496 if (nic->pdev->msix_enabled) {
1497 if (nicvf_hw_set_mac_addr(nic, netdev))
1498 return -EBUSY;
1499 } else {
1500 nic->set_mac_pending = true;
1501 }
1502
1503 return 0;
1504 }
1505
nicvf_update_lmac_stats(struct nicvf * nic)1506 void nicvf_update_lmac_stats(struct nicvf *nic)
1507 {
1508 int stat = 0;
1509 union nic_mbx mbx = {};
1510
1511 if (!netif_running(nic->netdev))
1512 return;
1513
1514 mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
1515 mbx.bgx_stats.vf_id = nic->vf_id;
1516 /* Rx stats */
1517 mbx.bgx_stats.rx = 1;
1518 while (stat < BGX_RX_STATS_COUNT) {
1519 mbx.bgx_stats.idx = stat;
1520 if (nicvf_send_msg_to_pf(nic, &mbx))
1521 return;
1522 stat++;
1523 }
1524
1525 stat = 0;
1526
1527 /* Tx stats */
1528 mbx.bgx_stats.rx = 0;
1529 while (stat < BGX_TX_STATS_COUNT) {
1530 mbx.bgx_stats.idx = stat;
1531 if (nicvf_send_msg_to_pf(nic, &mbx))
1532 return;
1533 stat++;
1534 }
1535 }
1536
nicvf_update_stats(struct nicvf * nic)1537 void nicvf_update_stats(struct nicvf *nic)
1538 {
1539 int qidx, cpu;
1540 u64 tmp_stats = 0;
1541 struct nicvf_hw_stats *stats = &nic->hw_stats;
1542 struct nicvf_drv_stats *drv_stats;
1543 struct queue_set *qs = nic->qs;
1544
1545 #define GET_RX_STATS(reg) \
1546 nicvf_reg_read(nic, NIC_VNIC_RX_STAT_0_13 | (reg << 3))
1547 #define GET_TX_STATS(reg) \
1548 nicvf_reg_read(nic, NIC_VNIC_TX_STAT_0_4 | (reg << 3))
1549
1550 stats->rx_bytes = GET_RX_STATS(RX_OCTS);
1551 stats->rx_ucast_frames = GET_RX_STATS(RX_UCAST);
1552 stats->rx_bcast_frames = GET_RX_STATS(RX_BCAST);
1553 stats->rx_mcast_frames = GET_RX_STATS(RX_MCAST);
1554 stats->rx_fcs_errors = GET_RX_STATS(RX_FCS);
1555 stats->rx_l2_errors = GET_RX_STATS(RX_L2ERR);
1556 stats->rx_drop_red = GET_RX_STATS(RX_RED);
1557 stats->rx_drop_red_bytes = GET_RX_STATS(RX_RED_OCTS);
1558 stats->rx_drop_overrun = GET_RX_STATS(RX_ORUN);
1559 stats->rx_drop_overrun_bytes = GET_RX_STATS(RX_ORUN_OCTS);
1560 stats->rx_drop_bcast = GET_RX_STATS(RX_DRP_BCAST);
1561 stats->rx_drop_mcast = GET_RX_STATS(RX_DRP_MCAST);
1562 stats->rx_drop_l3_bcast = GET_RX_STATS(RX_DRP_L3BCAST);
1563 stats->rx_drop_l3_mcast = GET_RX_STATS(RX_DRP_L3MCAST);
1564
1565 stats->tx_bytes = GET_TX_STATS(TX_OCTS);
1566 stats->tx_ucast_frames = GET_TX_STATS(TX_UCAST);
1567 stats->tx_bcast_frames = GET_TX_STATS(TX_BCAST);
1568 stats->tx_mcast_frames = GET_TX_STATS(TX_MCAST);
1569 stats->tx_drops = GET_TX_STATS(TX_DROP);
1570
1571 /* On T88 pass 2.0, the dummy SQE added for TSO notification
1572 * via CQE has 'dont_send' set. Hence HW drops the pkt pointed
1573 * pointed by dummy SQE and results in tx_drops counter being
1574 * incremented. Subtracting it from tx_tso counter will give
1575 * exact tx_drops counter.
1576 */
1577 if (nic->t88 && nic->hw_tso) {
1578 for_each_possible_cpu(cpu) {
1579 drv_stats = per_cpu_ptr(nic->drv_stats, cpu);
1580 tmp_stats += drv_stats->tx_tso;
1581 }
1582 stats->tx_drops = tmp_stats - stats->tx_drops;
1583 }
1584 stats->tx_frames = stats->tx_ucast_frames +
1585 stats->tx_bcast_frames +
1586 stats->tx_mcast_frames;
1587 stats->rx_frames = stats->rx_ucast_frames +
1588 stats->rx_bcast_frames +
1589 stats->rx_mcast_frames;
1590 stats->rx_drops = stats->rx_drop_red +
1591 stats->rx_drop_overrun;
1592
1593 /* Update RQ and SQ stats */
1594 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
1595 nicvf_update_rq_stats(nic, qidx);
1596 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
1597 nicvf_update_sq_stats(nic, qidx);
1598 }
1599
nicvf_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)1600 static void nicvf_get_stats64(struct net_device *netdev,
1601 struct rtnl_link_stats64 *stats)
1602 {
1603 struct nicvf *nic = netdev_priv(netdev);
1604 struct nicvf_hw_stats *hw_stats = &nic->hw_stats;
1605
1606 nicvf_update_stats(nic);
1607
1608 stats->rx_bytes = hw_stats->rx_bytes;
1609 stats->rx_packets = hw_stats->rx_frames;
1610 stats->rx_dropped = hw_stats->rx_drops;
1611 stats->multicast = hw_stats->rx_mcast_frames;
1612
1613 stats->tx_bytes = hw_stats->tx_bytes;
1614 stats->tx_packets = hw_stats->tx_frames;
1615 stats->tx_dropped = hw_stats->tx_drops;
1616
1617 }
1618
nicvf_tx_timeout(struct net_device * dev)1619 static void nicvf_tx_timeout(struct net_device *dev)
1620 {
1621 struct nicvf *nic = netdev_priv(dev);
1622
1623 netif_warn(nic, tx_err, dev, "Transmit timed out, resetting\n");
1624
1625 this_cpu_inc(nic->drv_stats->tx_timeout);
1626 schedule_work(&nic->reset_task);
1627 }
1628
nicvf_reset_task(struct work_struct * work)1629 static void nicvf_reset_task(struct work_struct *work)
1630 {
1631 struct nicvf *nic;
1632
1633 nic = container_of(work, struct nicvf, reset_task);
1634
1635 if (!netif_running(nic->netdev))
1636 return;
1637
1638 nicvf_stop(nic->netdev);
1639 nicvf_open(nic->netdev);
1640 netif_trans_update(nic->netdev);
1641 }
1642
nicvf_config_loopback(struct nicvf * nic,netdev_features_t features)1643 static int nicvf_config_loopback(struct nicvf *nic,
1644 netdev_features_t features)
1645 {
1646 union nic_mbx mbx = {};
1647
1648 mbx.lbk.msg = NIC_MBOX_MSG_LOOPBACK;
1649 mbx.lbk.vf_id = nic->vf_id;
1650 mbx.lbk.enable = (features & NETIF_F_LOOPBACK) != 0;
1651
1652 return nicvf_send_msg_to_pf(nic, &mbx);
1653 }
1654
nicvf_fix_features(struct net_device * netdev,netdev_features_t features)1655 static netdev_features_t nicvf_fix_features(struct net_device *netdev,
1656 netdev_features_t features)
1657 {
1658 struct nicvf *nic = netdev_priv(netdev);
1659
1660 if ((features & NETIF_F_LOOPBACK) &&
1661 netif_running(netdev) && !nic->loopback_supported)
1662 features &= ~NETIF_F_LOOPBACK;
1663
1664 return features;
1665 }
1666
nicvf_set_features(struct net_device * netdev,netdev_features_t features)1667 static int nicvf_set_features(struct net_device *netdev,
1668 netdev_features_t features)
1669 {
1670 struct nicvf *nic = netdev_priv(netdev);
1671 netdev_features_t changed = features ^ netdev->features;
1672
1673 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
1674 nicvf_config_vlan_stripping(nic, features);
1675
1676 if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
1677 return nicvf_config_loopback(nic, features);
1678
1679 return 0;
1680 }
1681
nicvf_set_xdp_queues(struct nicvf * nic,bool bpf_attached)1682 static void nicvf_set_xdp_queues(struct nicvf *nic, bool bpf_attached)
1683 {
1684 u8 cq_count, txq_count;
1685
1686 /* Set XDP Tx queue count same as Rx queue count */
1687 if (!bpf_attached)
1688 nic->xdp_tx_queues = 0;
1689 else
1690 nic->xdp_tx_queues = nic->rx_queues;
1691
1692 /* If queue count > MAX_CMP_QUEUES_PER_QS, then additional qsets
1693 * needs to be allocated, check how many.
1694 */
1695 txq_count = nic->xdp_tx_queues + nic->tx_queues;
1696 cq_count = max(nic->rx_queues, txq_count);
1697 if (cq_count > MAX_CMP_QUEUES_PER_QS) {
1698 nic->sqs_count = roundup(cq_count, MAX_CMP_QUEUES_PER_QS);
1699 nic->sqs_count = (nic->sqs_count / MAX_CMP_QUEUES_PER_QS) - 1;
1700 } else {
1701 nic->sqs_count = 0;
1702 }
1703
1704 /* Set primary Qset's resources */
1705 nic->qs->rq_cnt = min_t(u8, nic->rx_queues, MAX_RCV_QUEUES_PER_QS);
1706 nic->qs->sq_cnt = min_t(u8, txq_count, MAX_SND_QUEUES_PER_QS);
1707 nic->qs->cq_cnt = max_t(u8, nic->qs->rq_cnt, nic->qs->sq_cnt);
1708
1709 /* Update stack */
1710 nicvf_set_real_num_queues(nic->netdev, nic->tx_queues, nic->rx_queues);
1711 }
1712
nicvf_xdp_setup(struct nicvf * nic,struct bpf_prog * prog)1713 static int nicvf_xdp_setup(struct nicvf *nic, struct bpf_prog *prog)
1714 {
1715 struct net_device *dev = nic->netdev;
1716 bool if_up = netif_running(nic->netdev);
1717 struct bpf_prog *old_prog;
1718 bool bpf_attached = false;
1719 int ret = 0;
1720
1721 /* For now just support only the usual MTU sized frames,
1722 * plus some headroom for VLAN, QinQ.
1723 */
1724 if (prog && dev->mtu > MAX_XDP_MTU) {
1725 netdev_warn(dev, "Jumbo frames not yet supported with XDP, current MTU %d.\n",
1726 dev->mtu);
1727 return -EOPNOTSUPP;
1728 }
1729
1730 /* ALL SQs attached to CQs i.e same as RQs, are treated as
1731 * XDP Tx queues and more Tx queues are allocated for
1732 * network stack to send pkts out.
1733 *
1734 * No of Tx queues are either same as Rx queues or whatever
1735 * is left in max no of queues possible.
1736 */
1737 if ((nic->rx_queues + nic->tx_queues) > nic->max_queues) {
1738 netdev_warn(dev,
1739 "Failed to attach BPF prog, RXQs + TXQs > Max %d\n",
1740 nic->max_queues);
1741 return -ENOMEM;
1742 }
1743
1744 if (if_up)
1745 nicvf_stop(nic->netdev);
1746
1747 old_prog = xchg(&nic->xdp_prog, prog);
1748 /* Detach old prog, if any */
1749 if (old_prog)
1750 bpf_prog_put(old_prog);
1751
1752 if (nic->xdp_prog) {
1753 /* Attach BPF program */
1754 nic->xdp_prog = bpf_prog_add(nic->xdp_prog, nic->rx_queues - 1);
1755 if (!IS_ERR(nic->xdp_prog)) {
1756 bpf_attached = true;
1757 } else {
1758 ret = PTR_ERR(nic->xdp_prog);
1759 nic->xdp_prog = NULL;
1760 }
1761 }
1762
1763 /* Calculate Tx queues needed for XDP and network stack */
1764 nicvf_set_xdp_queues(nic, bpf_attached);
1765
1766 if (if_up) {
1767 /* Reinitialize interface, clean slate */
1768 nicvf_open(nic->netdev);
1769 netif_trans_update(nic->netdev);
1770 }
1771
1772 return ret;
1773 }
1774
nicvf_xdp(struct net_device * netdev,struct netdev_xdp * xdp)1775 static int nicvf_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
1776 {
1777 struct nicvf *nic = netdev_priv(netdev);
1778
1779 /* To avoid checks while retrieving buffer address from CQE_RX,
1780 * do not support XDP for T88 pass1.x silicons which are anyway
1781 * not in use widely.
1782 */
1783 if (pass1_silicon(nic->pdev))
1784 return -EOPNOTSUPP;
1785
1786 switch (xdp->command) {
1787 case XDP_SETUP_PROG:
1788 return nicvf_xdp_setup(nic, xdp->prog);
1789 case XDP_QUERY_PROG:
1790 xdp->prog_attached = !!nic->xdp_prog;
1791 xdp->prog_id = nic->xdp_prog ? nic->xdp_prog->aux->id : 0;
1792 return 0;
1793 default:
1794 return -EINVAL;
1795 }
1796 }
1797
1798 static const struct net_device_ops nicvf_netdev_ops = {
1799 .ndo_open = nicvf_open,
1800 .ndo_stop = nicvf_stop,
1801 .ndo_start_xmit = nicvf_xmit,
1802 .ndo_change_mtu = nicvf_change_mtu,
1803 .ndo_set_mac_address = nicvf_set_mac_address,
1804 .ndo_get_stats64 = nicvf_get_stats64,
1805 .ndo_tx_timeout = nicvf_tx_timeout,
1806 .ndo_fix_features = nicvf_fix_features,
1807 .ndo_set_features = nicvf_set_features,
1808 .ndo_xdp = nicvf_xdp,
1809 };
1810
nicvf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)1811 static int nicvf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1812 {
1813 struct device *dev = &pdev->dev;
1814 struct net_device *netdev;
1815 struct nicvf *nic;
1816 int err, qcount;
1817 u16 sdevid;
1818
1819 err = pci_enable_device(pdev);
1820 if (err) {
1821 dev_err(dev, "Failed to enable PCI device\n");
1822 return err;
1823 }
1824
1825 err = pci_request_regions(pdev, DRV_NAME);
1826 if (err) {
1827 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1828 goto err_disable_device;
1829 }
1830
1831 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
1832 if (err) {
1833 dev_err(dev, "Unable to get usable DMA configuration\n");
1834 goto err_release_regions;
1835 }
1836
1837 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
1838 if (err) {
1839 dev_err(dev, "unable to get 48-bit DMA for consistent allocations\n");
1840 goto err_release_regions;
1841 }
1842
1843 qcount = netif_get_num_default_rss_queues();
1844
1845 /* Restrict multiqset support only for host bound VFs */
1846 if (pdev->is_virtfn) {
1847 /* Set max number of queues per VF */
1848 qcount = min_t(int, num_online_cpus(),
1849 (MAX_SQS_PER_VF + 1) * MAX_CMP_QUEUES_PER_QS);
1850 }
1851
1852 netdev = alloc_etherdev_mqs(sizeof(struct nicvf), qcount, qcount);
1853 if (!netdev) {
1854 err = -ENOMEM;
1855 goto err_release_regions;
1856 }
1857
1858 pci_set_drvdata(pdev, netdev);
1859
1860 SET_NETDEV_DEV(netdev, &pdev->dev);
1861
1862 nic = netdev_priv(netdev);
1863 nic->netdev = netdev;
1864 nic->pdev = pdev;
1865 nic->pnicvf = nic;
1866 nic->max_queues = qcount;
1867 /* If no of CPUs are too low, there won't be any queues left
1868 * for XDP_TX, hence double it.
1869 */
1870 if (!nic->t88)
1871 nic->max_queues *= 2;
1872
1873 /* MAP VF's configuration registers */
1874 nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1875 if (!nic->reg_base) {
1876 dev_err(dev, "Cannot map config register space, aborting\n");
1877 err = -ENOMEM;
1878 goto err_free_netdev;
1879 }
1880
1881 nic->drv_stats = netdev_alloc_pcpu_stats(struct nicvf_drv_stats);
1882 if (!nic->drv_stats) {
1883 err = -ENOMEM;
1884 goto err_free_netdev;
1885 }
1886
1887 err = nicvf_set_qset_resources(nic);
1888 if (err)
1889 goto err_free_netdev;
1890
1891 /* Check if PF is alive and get MAC address for this VF */
1892 err = nicvf_register_misc_interrupt(nic);
1893 if (err)
1894 goto err_free_netdev;
1895
1896 nicvf_send_vf_struct(nic);
1897
1898 if (!pass1_silicon(nic->pdev))
1899 nic->hw_tso = true;
1900
1901 /* Get iommu domain for iova to physical addr conversion */
1902 nic->iommu_domain = iommu_get_domain_for_dev(dev);
1903
1904 pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
1905 if (sdevid == 0xA134)
1906 nic->t88 = true;
1907
1908 /* Check if this VF is in QS only mode */
1909 if (nic->sqs_mode)
1910 return 0;
1911
1912 err = nicvf_set_real_num_queues(netdev, nic->tx_queues, nic->rx_queues);
1913 if (err)
1914 goto err_unregister_interrupts;
1915
1916 netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_SG |
1917 NETIF_F_TSO | NETIF_F_GRO | NETIF_F_TSO6 |
1918 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1919 NETIF_F_HW_VLAN_CTAG_RX);
1920
1921 netdev->hw_features |= NETIF_F_RXHASH;
1922
1923 netdev->features |= netdev->hw_features;
1924 netdev->hw_features |= NETIF_F_LOOPBACK;
1925
1926 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1927 NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6;
1928
1929 netdev->netdev_ops = &nicvf_netdev_ops;
1930 netdev->watchdog_timeo = NICVF_TX_TIMEOUT;
1931
1932 /* MTU range: 64 - 9200 */
1933 netdev->min_mtu = NIC_HW_MIN_FRS;
1934 netdev->max_mtu = NIC_HW_MAX_FRS;
1935
1936 INIT_WORK(&nic->reset_task, nicvf_reset_task);
1937
1938 err = register_netdev(netdev);
1939 if (err) {
1940 dev_err(dev, "Failed to register netdevice\n");
1941 goto err_unregister_interrupts;
1942 }
1943
1944 nic->msg_enable = debug;
1945
1946 nicvf_set_ethtool_ops(netdev);
1947
1948 return 0;
1949
1950 err_unregister_interrupts:
1951 nicvf_unregister_interrupts(nic);
1952 err_free_netdev:
1953 pci_set_drvdata(pdev, NULL);
1954 if (nic->drv_stats)
1955 free_percpu(nic->drv_stats);
1956 free_netdev(netdev);
1957 err_release_regions:
1958 pci_release_regions(pdev);
1959 err_disable_device:
1960 pci_disable_device(pdev);
1961 return err;
1962 }
1963
nicvf_remove(struct pci_dev * pdev)1964 static void nicvf_remove(struct pci_dev *pdev)
1965 {
1966 struct net_device *netdev = pci_get_drvdata(pdev);
1967 struct nicvf *nic;
1968 struct net_device *pnetdev;
1969
1970 if (!netdev)
1971 return;
1972
1973 nic = netdev_priv(netdev);
1974 pnetdev = nic->pnicvf->netdev;
1975
1976 /* Check if this Qset is assigned to different VF.
1977 * If yes, clean primary and all secondary Qsets.
1978 */
1979 if (pnetdev && (pnetdev->reg_state == NETREG_REGISTERED))
1980 unregister_netdev(pnetdev);
1981 nicvf_unregister_interrupts(nic);
1982 pci_set_drvdata(pdev, NULL);
1983 if (nic->drv_stats)
1984 free_percpu(nic->drv_stats);
1985 free_netdev(netdev);
1986 pci_release_regions(pdev);
1987 pci_disable_device(pdev);
1988 }
1989
nicvf_shutdown(struct pci_dev * pdev)1990 static void nicvf_shutdown(struct pci_dev *pdev)
1991 {
1992 nicvf_remove(pdev);
1993 }
1994
1995 static struct pci_driver nicvf_driver = {
1996 .name = DRV_NAME,
1997 .id_table = nicvf_id_table,
1998 .probe = nicvf_probe,
1999 .remove = nicvf_remove,
2000 .shutdown = nicvf_shutdown,
2001 };
2002
nicvf_init_module(void)2003 static int __init nicvf_init_module(void)
2004 {
2005 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
2006
2007 return pci_register_driver(&nicvf_driver);
2008 }
2009
nicvf_cleanup_module(void)2010 static void __exit nicvf_cleanup_module(void)
2011 {
2012 pci_unregister_driver(&nicvf_driver);
2013 }
2014
2015 module_init(nicvf_init_module);
2016 module_exit(nicvf_cleanup_module);
2017