]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/ethernet/netronome/nfp/nfp_net_common.c
nfp: don't use netdev_warn() before netdev is registered
[mirror_ubuntu-bionic-kernel.git] / drivers / net / ethernet / netronome / nfp / nfp_net_common.c
1 /*
2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
3 *
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34 /*
35 * nfp_net_common.c
36 * Netronome network device driver: Common functions between PF and VF
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
40 * Brad Petrus <brad.petrus@netronome.com>
41 * Chris Telfer <chris.telfer@netronome.com>
42 */
43
44 #include <linux/bitfield.h>
45 #include <linux/bpf.h>
46 #include <linux/bpf_trace.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/init.h>
50 #include <linux/fs.h>
51 #include <linux/netdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/interrupt.h>
54 #include <linux/ip.h>
55 #include <linux/ipv6.h>
56 #include <linux/page_ref.h>
57 #include <linux/pci.h>
58 #include <linux/pci_regs.h>
59 #include <linux/msi.h>
60 #include <linux/ethtool.h>
61 #include <linux/log2.h>
62 #include <linux/if_vlan.h>
63 #include <linux/random.h>
64
65 #include <linux/ktime.h>
66
67 #include <net/pkt_cls.h>
68 #include <net/vxlan.h>
69
70 #include "nfpcore/nfp_nsp_eth.h"
71 #include "nfp_net_ctrl.h"
72 #include "nfp_net.h"
73
74 /**
75 * nfp_net_get_fw_version() - Read and parse the FW version
76 * @fw_ver: Output fw_version structure to read to
77 * @ctrl_bar: Mapped address of the control BAR
78 */
79 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
80 void __iomem *ctrl_bar)
81 {
82 u32 reg;
83
84 reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
85 put_unaligned_le32(reg, fw_ver);
86 }
87
88 static dma_addr_t nfp_net_dma_map_rx(struct nfp_net_dp *dp, void *frag)
89 {
90 return dma_map_single(dp->dev, frag + NFP_NET_RX_BUF_HEADROOM,
91 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
92 dp->rx_dma_dir);
93 }
94
95 static void nfp_net_dma_unmap_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr)
96 {
97 dma_unmap_single(dp->dev, dma_addr,
98 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
99 dp->rx_dma_dir);
100 }
101
102 /* Firmware reconfig
103 *
104 * Firmware reconfig may take a while so we have two versions of it -
105 * synchronous and asynchronous (posted). All synchronous callers are holding
106 * RTNL so we don't have to worry about serializing them.
107 */
108 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
109 {
110 nn_writel(nn, NFP_NET_CFG_UPDATE, update);
111 /* ensure update is written before pinging HW */
112 nn_pci_flush(nn);
113 nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
114 }
115
116 /* Pass 0 as update to run posted reconfigs. */
117 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
118 {
119 update |= nn->reconfig_posted;
120 nn->reconfig_posted = 0;
121
122 nfp_net_reconfig_start(nn, update);
123
124 nn->reconfig_timer_active = true;
125 mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
126 }
127
128 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
129 {
130 u32 reg;
131
132 reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
133 if (reg == 0)
134 return true;
135 if (reg & NFP_NET_CFG_UPDATE_ERR) {
136 nn_err(nn, "Reconfig error: 0x%08x\n", reg);
137 return true;
138 } else if (last_check) {
139 nn_err(nn, "Reconfig timeout: 0x%08x\n", reg);
140 return true;
141 }
142
143 return false;
144 }
145
146 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
147 {
148 bool timed_out = false;
149
150 /* Poll update field, waiting for NFP to ack the config */
151 while (!nfp_net_reconfig_check_done(nn, timed_out)) {
152 msleep(1);
153 timed_out = time_is_before_eq_jiffies(deadline);
154 }
155
156 if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
157 return -EIO;
158
159 return timed_out ? -EIO : 0;
160 }
161
162 static void nfp_net_reconfig_timer(unsigned long data)
163 {
164 struct nfp_net *nn = (void *)data;
165
166 spin_lock_bh(&nn->reconfig_lock);
167
168 nn->reconfig_timer_active = false;
169
170 /* If sync caller is present it will take over from us */
171 if (nn->reconfig_sync_present)
172 goto done;
173
174 /* Read reconfig status and report errors */
175 nfp_net_reconfig_check_done(nn, true);
176
177 if (nn->reconfig_posted)
178 nfp_net_reconfig_start_async(nn, 0);
179 done:
180 spin_unlock_bh(&nn->reconfig_lock);
181 }
182
183 /**
184 * nfp_net_reconfig_post() - Post async reconfig request
185 * @nn: NFP Net device to reconfigure
186 * @update: The value for the update field in the BAR config
187 *
188 * Record FW reconfiguration request. Reconfiguration will be kicked off
189 * whenever reconfiguration machinery is idle. Multiple requests can be
190 * merged together!
191 */
192 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
193 {
194 spin_lock_bh(&nn->reconfig_lock);
195
196 /* Sync caller will kick off async reconf when it's done, just post */
197 if (nn->reconfig_sync_present) {
198 nn->reconfig_posted |= update;
199 goto done;
200 }
201
202 /* Opportunistically check if the previous command is done */
203 if (!nn->reconfig_timer_active ||
204 nfp_net_reconfig_check_done(nn, false))
205 nfp_net_reconfig_start_async(nn, update);
206 else
207 nn->reconfig_posted |= update;
208 done:
209 spin_unlock_bh(&nn->reconfig_lock);
210 }
211
212 /**
213 * nfp_net_reconfig() - Reconfigure the firmware
214 * @nn: NFP Net device to reconfigure
215 * @update: The value for the update field in the BAR config
216 *
217 * Write the update word to the BAR and ping the reconfig queue. The
218 * poll until the firmware has acknowledged the update by zeroing the
219 * update word.
220 *
221 * Return: Negative errno on error, 0 on success
222 */
223 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
224 {
225 bool cancelled_timer = false;
226 u32 pre_posted_requests;
227 int ret;
228
229 spin_lock_bh(&nn->reconfig_lock);
230
231 nn->reconfig_sync_present = true;
232
233 if (nn->reconfig_timer_active) {
234 del_timer(&nn->reconfig_timer);
235 nn->reconfig_timer_active = false;
236 cancelled_timer = true;
237 }
238 pre_posted_requests = nn->reconfig_posted;
239 nn->reconfig_posted = 0;
240
241 spin_unlock_bh(&nn->reconfig_lock);
242
243 if (cancelled_timer)
244 nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
245
246 /* Run the posted reconfigs which were issued before we started */
247 if (pre_posted_requests) {
248 nfp_net_reconfig_start(nn, pre_posted_requests);
249 nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
250 }
251
252 nfp_net_reconfig_start(nn, update);
253 ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
254
255 spin_lock_bh(&nn->reconfig_lock);
256
257 if (nn->reconfig_posted)
258 nfp_net_reconfig_start_async(nn, 0);
259
260 nn->reconfig_sync_present = false;
261
262 spin_unlock_bh(&nn->reconfig_lock);
263
264 return ret;
265 }
266
267 /* Interrupt configuration and handling
268 */
269
270 /**
271 * nfp_net_irq_unmask() - Unmask automasked interrupt
272 * @nn: NFP Network structure
273 * @entry_nr: MSI-X table entry
274 *
275 * Clear the ICR for the IRQ entry.
276 */
277 static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
278 {
279 nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
280 nn_pci_flush(nn);
281 }
282
283 /**
284 * nfp_net_irqs_alloc() - allocates MSI-X irqs
285 * @pdev: PCI device structure
286 * @irq_entries: Array to be initialized and used to hold the irq entries
287 * @min_irqs: Minimal acceptable number of interrupts
288 * @wanted_irqs: Target number of interrupts to allocate
289 *
290 * Return: Number of irqs obtained or 0 on error.
291 */
292 unsigned int
293 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
294 unsigned int min_irqs, unsigned int wanted_irqs)
295 {
296 unsigned int i;
297 int got_irqs;
298
299 for (i = 0; i < wanted_irqs; i++)
300 irq_entries[i].entry = i;
301
302 got_irqs = pci_enable_msix_range(pdev, irq_entries,
303 min_irqs, wanted_irqs);
304 if (got_irqs < 0) {
305 dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
306 min_irqs, wanted_irqs, got_irqs);
307 return 0;
308 }
309
310 if (got_irqs < wanted_irqs)
311 dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
312 wanted_irqs, got_irqs);
313
314 return got_irqs;
315 }
316
317 /**
318 * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
319 * @nn: NFP Network structure
320 * @irq_entries: Table of allocated interrupts
321 * @n: Size of @irq_entries (number of entries to grab)
322 *
323 * After interrupts are allocated with nfp_net_irqs_alloc() this function
324 * should be called to assign them to a specific netdev (port).
325 */
326 void
327 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
328 unsigned int n)
329 {
330 struct nfp_net_dp *dp = &nn->dp;
331
332 nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
333 dp->num_r_vecs = nn->max_r_vecs;
334
335 memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
336
337 if (dp->num_rx_rings > dp->num_r_vecs ||
338 dp->num_tx_rings > dp->num_r_vecs)
339 dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
340 dp->num_rx_rings, dp->num_tx_rings,
341 dp->num_r_vecs);
342
343 dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
344 dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
345 dp->num_stack_tx_rings = dp->num_tx_rings;
346 }
347
348 /**
349 * nfp_net_irqs_disable() - Disable interrupts
350 * @pdev: PCI device structure
351 *
352 * Undoes what @nfp_net_irqs_alloc() does.
353 */
354 void nfp_net_irqs_disable(struct pci_dev *pdev)
355 {
356 pci_disable_msix(pdev);
357 }
358
359 /**
360 * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
361 * @irq: Interrupt
362 * @data: Opaque data structure
363 *
364 * Return: Indicate if the interrupt has been handled.
365 */
366 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
367 {
368 struct nfp_net_r_vector *r_vec = data;
369
370 napi_schedule_irqoff(&r_vec->napi);
371
372 /* The FW auto-masks any interrupt, either via the MASK bit in
373 * the MSI-X table or via the per entry ICR field. So there
374 * is no need to disable interrupts here.
375 */
376 return IRQ_HANDLED;
377 }
378
379 /**
380 * nfp_net_read_link_status() - Reread link status from control BAR
381 * @nn: NFP Network structure
382 */
383 static void nfp_net_read_link_status(struct nfp_net *nn)
384 {
385 unsigned long flags;
386 bool link_up;
387 u32 sts;
388
389 spin_lock_irqsave(&nn->link_status_lock, flags);
390
391 sts = nn_readl(nn, NFP_NET_CFG_STS);
392 link_up = !!(sts & NFP_NET_CFG_STS_LINK);
393
394 if (nn->link_up == link_up)
395 goto out;
396
397 nn->link_up = link_up;
398
399 if (nn->link_up) {
400 netif_carrier_on(nn->dp.netdev);
401 netdev_info(nn->dp.netdev, "NIC Link is Up\n");
402 } else {
403 netif_carrier_off(nn->dp.netdev);
404 netdev_info(nn->dp.netdev, "NIC Link is Down\n");
405 }
406 out:
407 spin_unlock_irqrestore(&nn->link_status_lock, flags);
408 }
409
410 /**
411 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
412 * @irq: Interrupt
413 * @data: Opaque data structure
414 *
415 * Return: Indicate if the interrupt has been handled.
416 */
417 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
418 {
419 struct nfp_net *nn = data;
420 struct msix_entry *entry;
421
422 entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
423
424 nfp_net_read_link_status(nn);
425
426 nfp_net_irq_unmask(nn, entry->entry);
427
428 return IRQ_HANDLED;
429 }
430
431 /**
432 * nfp_net_irq_exn() - Interrupt service routine for exceptions
433 * @irq: Interrupt
434 * @data: Opaque data structure
435 *
436 * Return: Indicate if the interrupt has been handled.
437 */
438 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
439 {
440 struct nfp_net *nn = data;
441
442 nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
443 /* XXX TO BE IMPLEMENTED */
444 return IRQ_HANDLED;
445 }
446
447 /**
448 * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
449 * @tx_ring: TX ring structure
450 * @r_vec: IRQ vector servicing this ring
451 * @idx: Ring index
452 */
453 static void
454 nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
455 struct nfp_net_r_vector *r_vec, unsigned int idx)
456 {
457 struct nfp_net *nn = r_vec->nfp_net;
458
459 tx_ring->idx = idx;
460 tx_ring->r_vec = r_vec;
461
462 tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
463 tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
464 }
465
466 /**
467 * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
468 * @rx_ring: RX ring structure
469 * @r_vec: IRQ vector servicing this ring
470 * @idx: Ring index
471 */
472 static void
473 nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
474 struct nfp_net_r_vector *r_vec, unsigned int idx)
475 {
476 struct nfp_net *nn = r_vec->nfp_net;
477
478 rx_ring->idx = idx;
479 rx_ring->r_vec = r_vec;
480
481 rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
482 rx_ring->rx_qcidx = rx_ring->fl_qcidx + (nn->stride_rx - 1);
483
484 rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
485 rx_ring->qcp_rx = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->rx_qcidx);
486 }
487
488 /**
489 * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
490 * @netdev: netdev structure
491 */
492 static void nfp_net_vecs_init(struct net_device *netdev)
493 {
494 struct nfp_net *nn = netdev_priv(netdev);
495 struct nfp_net_r_vector *r_vec;
496 int r;
497
498 nn->lsc_handler = nfp_net_irq_lsc;
499 nn->exn_handler = nfp_net_irq_exn;
500
501 for (r = 0; r < nn->max_r_vecs; r++) {
502 struct msix_entry *entry;
503
504 entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
505
506 r_vec = &nn->r_vecs[r];
507 r_vec->nfp_net = nn;
508 r_vec->handler = nfp_net_irq_rxtx;
509 r_vec->irq_entry = entry->entry;
510 r_vec->irq_vector = entry->vector;
511
512 cpumask_set_cpu(r, &r_vec->affinity_mask);
513 }
514 }
515
516 /**
517 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
518 * @nn: NFP Network structure
519 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
520 * @format: printf-style format to construct the interrupt name
521 * @name: Pointer to allocated space for interrupt name
522 * @name_sz: Size of space for interrupt name
523 * @vector_idx: Index of MSI-X vector used for this interrupt
524 * @handler: IRQ handler to register for this interrupt
525 */
526 static int
527 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
528 const char *format, char *name, size_t name_sz,
529 unsigned int vector_idx, irq_handler_t handler)
530 {
531 struct msix_entry *entry;
532 int err;
533
534 entry = &nn->irq_entries[vector_idx];
535
536 snprintf(name, name_sz, format, netdev_name(nn->dp.netdev));
537 err = request_irq(entry->vector, handler, 0, name, nn);
538 if (err) {
539 nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
540 entry->vector, err);
541 return err;
542 }
543 nn_writeb(nn, ctrl_offset, entry->entry);
544
545 return 0;
546 }
547
548 /**
549 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
550 * @nn: NFP Network structure
551 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
552 * @vector_idx: Index of MSI-X vector used for this interrupt
553 */
554 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
555 unsigned int vector_idx)
556 {
557 nn_writeb(nn, ctrl_offset, 0xff);
558 free_irq(nn->irq_entries[vector_idx].vector, nn);
559 }
560
561 /* Transmit
562 *
563 * One queue controller peripheral queue is used for transmit. The
564 * driver en-queues packets for transmit by advancing the write
565 * pointer. The device indicates that packets have transmitted by
566 * advancing the read pointer. The driver maintains a local copy of
567 * the read and write pointer in @struct nfp_net_tx_ring. The driver
568 * keeps @wr_p in sync with the queue controller write pointer and can
569 * determine how many packets have been transmitted by comparing its
570 * copy of the read pointer @rd_p with the read pointer maintained by
571 * the queue controller peripheral.
572 */
573
574 /**
575 * nfp_net_tx_full() - Check if the TX ring is full
576 * @tx_ring: TX ring to check
577 * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
578 *
579 * This function checks, based on the *host copy* of read/write
580 * pointer if a given TX ring is full. The real TX queue may have
581 * some newly made available slots.
582 *
583 * Return: True if the ring is full.
584 */
585 static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
586 {
587 return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
588 }
589
590 /* Wrappers for deciding when to stop and restart TX queues */
591 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
592 {
593 return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
594 }
595
596 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
597 {
598 return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
599 }
600
601 /**
602 * nfp_net_tx_ring_stop() - stop tx ring
603 * @nd_q: netdev queue
604 * @tx_ring: driver tx queue structure
605 *
606 * Safely stop TX ring. Remember that while we are running .start_xmit()
607 * someone else may be cleaning the TX ring completions so we need to be
608 * extra careful here.
609 */
610 static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
611 struct nfp_net_tx_ring *tx_ring)
612 {
613 netif_tx_stop_queue(nd_q);
614
615 /* We can race with the TX completion out of NAPI so recheck */
616 smp_mb();
617 if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
618 netif_tx_start_queue(nd_q);
619 }
620
621 /**
622 * nfp_net_tx_tso() - Set up Tx descriptor for LSO
623 * @r_vec: per-ring structure
624 * @txbuf: Pointer to driver soft TX descriptor
625 * @txd: Pointer to HW TX descriptor
626 * @skb: Pointer to SKB
627 *
628 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
629 * Return error on packet header greater than maximum supported LSO header size.
630 */
631 static void nfp_net_tx_tso(struct nfp_net_r_vector *r_vec,
632 struct nfp_net_tx_buf *txbuf,
633 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
634 {
635 u32 hdrlen;
636 u16 mss;
637
638 if (!skb_is_gso(skb))
639 return;
640
641 if (!skb->encapsulation)
642 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
643 else
644 hdrlen = skb_inner_transport_header(skb) - skb->data +
645 inner_tcp_hdrlen(skb);
646
647 txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
648 txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
649
650 mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
651 txd->l4_offset = hdrlen;
652 txd->mss = cpu_to_le16(mss);
653 txd->flags |= PCIE_DESC_TX_LSO;
654
655 u64_stats_update_begin(&r_vec->tx_sync);
656 r_vec->tx_lso++;
657 u64_stats_update_end(&r_vec->tx_sync);
658 }
659
660 /**
661 * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
662 * @dp: NFP Net data path struct
663 * @r_vec: per-ring structure
664 * @txbuf: Pointer to driver soft TX descriptor
665 * @txd: Pointer to TX descriptor
666 * @skb: Pointer to SKB
667 *
668 * This function sets the TX checksum flags in the TX descriptor based
669 * on the configuration and the protocol of the packet to be transmitted.
670 */
671 static void nfp_net_tx_csum(struct nfp_net_dp *dp,
672 struct nfp_net_r_vector *r_vec,
673 struct nfp_net_tx_buf *txbuf,
674 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
675 {
676 struct ipv6hdr *ipv6h;
677 struct iphdr *iph;
678 u8 l4_hdr;
679
680 if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
681 return;
682
683 if (skb->ip_summed != CHECKSUM_PARTIAL)
684 return;
685
686 txd->flags |= PCIE_DESC_TX_CSUM;
687 if (skb->encapsulation)
688 txd->flags |= PCIE_DESC_TX_ENCAP;
689
690 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
691 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
692
693 if (iph->version == 4) {
694 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
695 l4_hdr = iph->protocol;
696 } else if (ipv6h->version == 6) {
697 l4_hdr = ipv6h->nexthdr;
698 } else {
699 nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
700 return;
701 }
702
703 switch (l4_hdr) {
704 case IPPROTO_TCP:
705 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
706 break;
707 case IPPROTO_UDP:
708 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
709 break;
710 default:
711 nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
712 return;
713 }
714
715 u64_stats_update_begin(&r_vec->tx_sync);
716 if (skb->encapsulation)
717 r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
718 else
719 r_vec->hw_csum_tx += txbuf->pkt_cnt;
720 u64_stats_update_end(&r_vec->tx_sync);
721 }
722
723 static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
724 {
725 wmb();
726 nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
727 tx_ring->wr_ptr_add = 0;
728 }
729
730 /**
731 * nfp_net_tx() - Main transmit entry point
732 * @skb: SKB to transmit
733 * @netdev: netdev structure
734 *
735 * Return: NETDEV_TX_OK on success.
736 */
737 static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
738 {
739 struct nfp_net *nn = netdev_priv(netdev);
740 const struct skb_frag_struct *frag;
741 struct nfp_net_tx_desc *txd, txdg;
742 struct nfp_net_tx_ring *tx_ring;
743 struct nfp_net_r_vector *r_vec;
744 struct nfp_net_tx_buf *txbuf;
745 struct netdev_queue *nd_q;
746 struct nfp_net_dp *dp;
747 dma_addr_t dma_addr;
748 unsigned int fsize;
749 int f, nr_frags;
750 int wr_idx;
751 u16 qidx;
752
753 dp = &nn->dp;
754 qidx = skb_get_queue_mapping(skb);
755 tx_ring = &dp->tx_rings[qidx];
756 r_vec = tx_ring->r_vec;
757 nd_q = netdev_get_tx_queue(dp->netdev, qidx);
758
759 nr_frags = skb_shinfo(skb)->nr_frags;
760
761 if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
762 nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
763 qidx, tx_ring->wr_p, tx_ring->rd_p);
764 netif_tx_stop_queue(nd_q);
765 u64_stats_update_begin(&r_vec->tx_sync);
766 r_vec->tx_busy++;
767 u64_stats_update_end(&r_vec->tx_sync);
768 return NETDEV_TX_BUSY;
769 }
770
771 /* Start with the head skbuf */
772 dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
773 DMA_TO_DEVICE);
774 if (dma_mapping_error(dp->dev, dma_addr))
775 goto err_free;
776
777 wr_idx = tx_ring->wr_p & (tx_ring->cnt - 1);
778
779 /* Stash the soft descriptor of the head then initialize it */
780 txbuf = &tx_ring->txbufs[wr_idx];
781 txbuf->skb = skb;
782 txbuf->dma_addr = dma_addr;
783 txbuf->fidx = -1;
784 txbuf->pkt_cnt = 1;
785 txbuf->real_len = skb->len;
786
787 /* Build TX descriptor */
788 txd = &tx_ring->txds[wr_idx];
789 txd->offset_eop = (nr_frags == 0) ? PCIE_DESC_TX_EOP : 0;
790 txd->dma_len = cpu_to_le16(skb_headlen(skb));
791 nfp_desc_set_dma_addr(txd, dma_addr);
792 txd->data_len = cpu_to_le16(skb->len);
793
794 txd->flags = 0;
795 txd->mss = 0;
796 txd->l4_offset = 0;
797
798 nfp_net_tx_tso(r_vec, txbuf, txd, skb);
799
800 nfp_net_tx_csum(dp, r_vec, txbuf, txd, skb);
801
802 if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
803 txd->flags |= PCIE_DESC_TX_VLAN;
804 txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
805 }
806
807 /* Gather DMA */
808 if (nr_frags > 0) {
809 /* all descs must match except for in addr, length and eop */
810 txdg = *txd;
811
812 for (f = 0; f < nr_frags; f++) {
813 frag = &skb_shinfo(skb)->frags[f];
814 fsize = skb_frag_size(frag);
815
816 dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
817 fsize, DMA_TO_DEVICE);
818 if (dma_mapping_error(dp->dev, dma_addr))
819 goto err_unmap;
820
821 wr_idx = (wr_idx + 1) & (tx_ring->cnt - 1);
822 tx_ring->txbufs[wr_idx].skb = skb;
823 tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
824 tx_ring->txbufs[wr_idx].fidx = f;
825
826 txd = &tx_ring->txds[wr_idx];
827 *txd = txdg;
828 txd->dma_len = cpu_to_le16(fsize);
829 nfp_desc_set_dma_addr(txd, dma_addr);
830 txd->offset_eop =
831 (f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0;
832 }
833
834 u64_stats_update_begin(&r_vec->tx_sync);
835 r_vec->tx_gather++;
836 u64_stats_update_end(&r_vec->tx_sync);
837 }
838
839 netdev_tx_sent_queue(nd_q, txbuf->real_len);
840
841 tx_ring->wr_p += nr_frags + 1;
842 if (nfp_net_tx_ring_should_stop(tx_ring))
843 nfp_net_tx_ring_stop(nd_q, tx_ring);
844
845 tx_ring->wr_ptr_add += nr_frags + 1;
846 if (!skb->xmit_more || netif_xmit_stopped(nd_q))
847 nfp_net_tx_xmit_more_flush(tx_ring);
848
849 skb_tx_timestamp(skb);
850
851 return NETDEV_TX_OK;
852
853 err_unmap:
854 --f;
855 while (f >= 0) {
856 frag = &skb_shinfo(skb)->frags[f];
857 dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
858 skb_frag_size(frag), DMA_TO_DEVICE);
859 tx_ring->txbufs[wr_idx].skb = NULL;
860 tx_ring->txbufs[wr_idx].dma_addr = 0;
861 tx_ring->txbufs[wr_idx].fidx = -2;
862 wr_idx = wr_idx - 1;
863 if (wr_idx < 0)
864 wr_idx += tx_ring->cnt;
865 }
866 dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
867 skb_headlen(skb), DMA_TO_DEVICE);
868 tx_ring->txbufs[wr_idx].skb = NULL;
869 tx_ring->txbufs[wr_idx].dma_addr = 0;
870 tx_ring->txbufs[wr_idx].fidx = -2;
871 err_free:
872 nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
873 u64_stats_update_begin(&r_vec->tx_sync);
874 r_vec->tx_errors++;
875 u64_stats_update_end(&r_vec->tx_sync);
876 dev_kfree_skb_any(skb);
877 return NETDEV_TX_OK;
878 }
879
880 /**
881 * nfp_net_tx_complete() - Handled completed TX packets
882 * @tx_ring: TX ring structure
883 *
884 * Return: Number of completed TX descriptors
885 */
886 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring)
887 {
888 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
889 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
890 const struct skb_frag_struct *frag;
891 struct netdev_queue *nd_q;
892 u32 done_pkts = 0, done_bytes = 0;
893 struct sk_buff *skb;
894 int todo, nr_frags;
895 u32 qcp_rd_p;
896 int fidx;
897 int idx;
898
899 /* Work out how many descriptors have been transmitted */
900 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
901
902 if (qcp_rd_p == tx_ring->qcp_rd_p)
903 return;
904
905 if (qcp_rd_p > tx_ring->qcp_rd_p)
906 todo = qcp_rd_p - tx_ring->qcp_rd_p;
907 else
908 todo = qcp_rd_p + tx_ring->cnt - tx_ring->qcp_rd_p;
909
910 while (todo--) {
911 idx = tx_ring->rd_p & (tx_ring->cnt - 1);
912 tx_ring->rd_p++;
913
914 skb = tx_ring->txbufs[idx].skb;
915 if (!skb)
916 continue;
917
918 nr_frags = skb_shinfo(skb)->nr_frags;
919 fidx = tx_ring->txbufs[idx].fidx;
920
921 if (fidx == -1) {
922 /* unmap head */
923 dma_unmap_single(dp->dev, tx_ring->txbufs[idx].dma_addr,
924 skb_headlen(skb), DMA_TO_DEVICE);
925
926 done_pkts += tx_ring->txbufs[idx].pkt_cnt;
927 done_bytes += tx_ring->txbufs[idx].real_len;
928 } else {
929 /* unmap fragment */
930 frag = &skb_shinfo(skb)->frags[fidx];
931 dma_unmap_page(dp->dev, tx_ring->txbufs[idx].dma_addr,
932 skb_frag_size(frag), DMA_TO_DEVICE);
933 }
934
935 /* check for last gather fragment */
936 if (fidx == nr_frags - 1)
937 dev_kfree_skb_any(skb);
938
939 tx_ring->txbufs[idx].dma_addr = 0;
940 tx_ring->txbufs[idx].skb = NULL;
941 tx_ring->txbufs[idx].fidx = -2;
942 }
943
944 tx_ring->qcp_rd_p = qcp_rd_p;
945
946 u64_stats_update_begin(&r_vec->tx_sync);
947 r_vec->tx_bytes += done_bytes;
948 r_vec->tx_pkts += done_pkts;
949 u64_stats_update_end(&r_vec->tx_sync);
950
951 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
952 netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
953 if (nfp_net_tx_ring_should_wake(tx_ring)) {
954 /* Make sure TX thread will see updated tx_ring->rd_p */
955 smp_mb();
956
957 if (unlikely(netif_tx_queue_stopped(nd_q)))
958 netif_tx_wake_queue(nd_q);
959 }
960
961 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
962 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
963 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
964 }
965
966 static void nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
967 {
968 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
969 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
970 u32 done_pkts = 0, done_bytes = 0;
971 int idx, todo;
972 u32 qcp_rd_p;
973
974 /* Work out how many descriptors have been transmitted */
975 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
976
977 if (qcp_rd_p == tx_ring->qcp_rd_p)
978 return;
979
980 if (qcp_rd_p > tx_ring->qcp_rd_p)
981 todo = qcp_rd_p - tx_ring->qcp_rd_p;
982 else
983 todo = qcp_rd_p + tx_ring->cnt - tx_ring->qcp_rd_p;
984
985 while (todo--) {
986 idx = tx_ring->rd_p & (tx_ring->cnt - 1);
987 tx_ring->rd_p++;
988
989 if (!tx_ring->txbufs[idx].frag)
990 continue;
991
992 nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[idx].dma_addr);
993 __free_page(virt_to_page(tx_ring->txbufs[idx].frag));
994
995 done_pkts++;
996 done_bytes += tx_ring->txbufs[idx].real_len;
997
998 tx_ring->txbufs[idx].dma_addr = 0;
999 tx_ring->txbufs[idx].frag = NULL;
1000 tx_ring->txbufs[idx].fidx = -2;
1001 }
1002
1003 tx_ring->qcp_rd_p = qcp_rd_p;
1004
1005 u64_stats_update_begin(&r_vec->tx_sync);
1006 r_vec->tx_bytes += done_bytes;
1007 r_vec->tx_pkts += done_pkts;
1008 u64_stats_update_end(&r_vec->tx_sync);
1009
1010 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1011 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1012 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1013 }
1014
1015 /**
1016 * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1017 * @dp: NFP Net data path struct
1018 * @tx_ring: TX ring structure
1019 *
1020 * Assumes that the device is stopped
1021 */
1022 static void
1023 nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1024 {
1025 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1026 const struct skb_frag_struct *frag;
1027 struct netdev_queue *nd_q;
1028
1029 while (tx_ring->rd_p != tx_ring->wr_p) {
1030 struct nfp_net_tx_buf *tx_buf;
1031 int idx;
1032
1033 idx = tx_ring->rd_p & (tx_ring->cnt - 1);
1034 tx_buf = &tx_ring->txbufs[idx];
1035
1036 if (tx_ring == r_vec->xdp_ring) {
1037 nfp_net_dma_unmap_rx(dp, tx_buf->dma_addr);
1038 __free_page(virt_to_page(tx_ring->txbufs[idx].frag));
1039 } else {
1040 struct sk_buff *skb = tx_ring->txbufs[idx].skb;
1041 int nr_frags = skb_shinfo(skb)->nr_frags;
1042
1043 if (tx_buf->fidx == -1) {
1044 /* unmap head */
1045 dma_unmap_single(dp->dev, tx_buf->dma_addr,
1046 skb_headlen(skb),
1047 DMA_TO_DEVICE);
1048 } else {
1049 /* unmap fragment */
1050 frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
1051 dma_unmap_page(dp->dev, tx_buf->dma_addr,
1052 skb_frag_size(frag),
1053 DMA_TO_DEVICE);
1054 }
1055
1056 /* check for last gather fragment */
1057 if (tx_buf->fidx == nr_frags - 1)
1058 dev_kfree_skb_any(skb);
1059 }
1060
1061 tx_buf->dma_addr = 0;
1062 tx_buf->skb = NULL;
1063 tx_buf->fidx = -2;
1064
1065 tx_ring->qcp_rd_p++;
1066 tx_ring->rd_p++;
1067 }
1068
1069 memset(tx_ring->txds, 0, sizeof(*tx_ring->txds) * tx_ring->cnt);
1070 tx_ring->wr_p = 0;
1071 tx_ring->rd_p = 0;
1072 tx_ring->qcp_rd_p = 0;
1073 tx_ring->wr_ptr_add = 0;
1074
1075 if (tx_ring == r_vec->xdp_ring)
1076 return;
1077
1078 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1079 netdev_tx_reset_queue(nd_q);
1080 }
1081
1082 static void nfp_net_tx_timeout(struct net_device *netdev)
1083 {
1084 struct nfp_net *nn = netdev_priv(netdev);
1085 int i;
1086
1087 for (i = 0; i < nn->dp.netdev->real_num_tx_queues; i++) {
1088 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
1089 continue;
1090 nn_warn(nn, "TX timeout on ring: %d\n", i);
1091 }
1092 nn_warn(nn, "TX watchdog timeout\n");
1093 }
1094
1095 /* Receive processing
1096 */
1097 static unsigned int
1098 nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
1099 {
1100 unsigned int fl_bufsz;
1101
1102 fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1103 fl_bufsz += dp->rx_dma_off;
1104 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1105 fl_bufsz += NFP_NET_MAX_PREPEND;
1106 else
1107 fl_bufsz += dp->rx_offset;
1108 fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
1109
1110 fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
1111 fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1112
1113 return fl_bufsz;
1114 }
1115
1116 static void
1117 nfp_net_free_frag(void *frag, bool xdp)
1118 {
1119 if (!xdp)
1120 skb_free_frag(frag);
1121 else
1122 __free_page(virt_to_page(frag));
1123 }
1124
1125 /**
1126 * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1127 * @dp: NFP Net data path struct
1128 * @rx_ring: RX ring structure of the skb
1129 * @dma_addr: Pointer to storage for DMA address (output param)
1130 *
1131 * This function will allcate a new page frag, map it for DMA.
1132 *
1133 * Return: allocated page frag or NULL on failure.
1134 */
1135 static void *
1136 nfp_net_rx_alloc_one(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1137 dma_addr_t *dma_addr)
1138 {
1139 void *frag;
1140
1141 if (!dp->xdp_prog)
1142 frag = netdev_alloc_frag(dp->fl_bufsz);
1143 else
1144 frag = page_address(alloc_page(GFP_KERNEL | __GFP_COLD));
1145 if (!frag) {
1146 nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1147 return NULL;
1148 }
1149
1150 *dma_addr = nfp_net_dma_map_rx(dp, frag);
1151 if (dma_mapping_error(dp->dev, *dma_addr)) {
1152 nfp_net_free_frag(frag, dp->xdp_prog);
1153 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1154 return NULL;
1155 }
1156
1157 return frag;
1158 }
1159
1160 static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1161 {
1162 void *frag;
1163
1164 if (!dp->xdp_prog)
1165 frag = napi_alloc_frag(dp->fl_bufsz);
1166 else
1167 frag = page_address(alloc_page(GFP_ATOMIC | __GFP_COLD));
1168 if (!frag) {
1169 nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1170 return NULL;
1171 }
1172
1173 *dma_addr = nfp_net_dma_map_rx(dp, frag);
1174 if (dma_mapping_error(dp->dev, *dma_addr)) {
1175 nfp_net_free_frag(frag, dp->xdp_prog);
1176 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1177 return NULL;
1178 }
1179
1180 return frag;
1181 }
1182
1183 /**
1184 * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1185 * @dp: NFP Net data path struct
1186 * @rx_ring: RX ring structure
1187 * @frag: page fragment buffer
1188 * @dma_addr: DMA address of skb mapping
1189 */
1190 static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
1191 struct nfp_net_rx_ring *rx_ring,
1192 void *frag, dma_addr_t dma_addr)
1193 {
1194 unsigned int wr_idx;
1195
1196 wr_idx = rx_ring->wr_p & (rx_ring->cnt - 1);
1197
1198 /* Stash SKB and DMA address away */
1199 rx_ring->rxbufs[wr_idx].frag = frag;
1200 rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1201
1202 /* Fill freelist descriptor */
1203 rx_ring->rxds[wr_idx].fld.reserved = 0;
1204 rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1205 nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
1206 dma_addr + dp->rx_dma_off);
1207
1208 rx_ring->wr_p++;
1209 rx_ring->wr_ptr_add++;
1210 if (rx_ring->wr_ptr_add >= NFP_NET_FL_BATCH) {
1211 /* Update write pointer of the freelist queue. Make
1212 * sure all writes are flushed before telling the hardware.
1213 */
1214 wmb();
1215 nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, rx_ring->wr_ptr_add);
1216 rx_ring->wr_ptr_add = 0;
1217 }
1218 }
1219
1220 /**
1221 * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1222 * @rx_ring: RX ring structure
1223 *
1224 * Warning: Do *not* call if ring buffers were never put on the FW freelist
1225 * (i.e. device was not enabled)!
1226 */
1227 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1228 {
1229 unsigned int wr_idx, last_idx;
1230
1231 /* Move the empty entry to the end of the list */
1232 wr_idx = rx_ring->wr_p & (rx_ring->cnt - 1);
1233 last_idx = rx_ring->cnt - 1;
1234 rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1235 rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1236 rx_ring->rxbufs[last_idx].dma_addr = 0;
1237 rx_ring->rxbufs[last_idx].frag = NULL;
1238
1239 memset(rx_ring->rxds, 0, sizeof(*rx_ring->rxds) * rx_ring->cnt);
1240 rx_ring->wr_p = 0;
1241 rx_ring->rd_p = 0;
1242 rx_ring->wr_ptr_add = 0;
1243 }
1244
1245 /**
1246 * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1247 * @dp: NFP Net data path struct
1248 * @rx_ring: RX ring to remove buffers from
1249 *
1250 * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1251 * entries. After device is disabled nfp_net_rx_ring_reset() must be called
1252 * to restore required ring geometry.
1253 */
1254 static void
1255 nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
1256 struct nfp_net_rx_ring *rx_ring)
1257 {
1258 unsigned int i;
1259
1260 for (i = 0; i < rx_ring->cnt - 1; i++) {
1261 /* NULL skb can only happen when initial filling of the ring
1262 * fails to allocate enough buffers and calls here to free
1263 * already allocated ones.
1264 */
1265 if (!rx_ring->rxbufs[i].frag)
1266 continue;
1267
1268 nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
1269 nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
1270 rx_ring->rxbufs[i].dma_addr = 0;
1271 rx_ring->rxbufs[i].frag = NULL;
1272 }
1273 }
1274
1275 /**
1276 * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1277 * @dp: NFP Net data path struct
1278 * @rx_ring: RX ring to remove buffers from
1279 */
1280 static int
1281 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
1282 struct nfp_net_rx_ring *rx_ring)
1283 {
1284 struct nfp_net_rx_buf *rxbufs;
1285 unsigned int i;
1286
1287 rxbufs = rx_ring->rxbufs;
1288
1289 for (i = 0; i < rx_ring->cnt - 1; i++) {
1290 rxbufs[i].frag =
1291 nfp_net_rx_alloc_one(dp, rx_ring, &rxbufs[i].dma_addr);
1292 if (!rxbufs[i].frag) {
1293 nfp_net_rx_ring_bufs_free(dp, rx_ring);
1294 return -ENOMEM;
1295 }
1296 }
1297
1298 return 0;
1299 }
1300
1301 /**
1302 * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1303 * @dp: NFP Net data path struct
1304 * @rx_ring: RX ring to fill
1305 */
1306 static void
1307 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
1308 struct nfp_net_rx_ring *rx_ring)
1309 {
1310 unsigned int i;
1311
1312 for (i = 0; i < rx_ring->cnt - 1; i++)
1313 nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
1314 rx_ring->rxbufs[i].dma_addr);
1315 }
1316
1317 /**
1318 * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1319 * @flags: RX descriptor flags field in CPU byte order
1320 */
1321 static int nfp_net_rx_csum_has_errors(u16 flags)
1322 {
1323 u16 csum_all_checked, csum_all_ok;
1324
1325 csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1326 csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1327
1328 return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1329 }
1330
1331 /**
1332 * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1333 * @dp: NFP Net data path struct
1334 * @r_vec: per-ring structure
1335 * @rxd: Pointer to RX descriptor
1336 * @skb: Pointer to SKB
1337 */
1338 static void nfp_net_rx_csum(struct nfp_net_dp *dp,
1339 struct nfp_net_r_vector *r_vec,
1340 struct nfp_net_rx_desc *rxd, struct sk_buff *skb)
1341 {
1342 skb_checksum_none_assert(skb);
1343
1344 if (!(dp->netdev->features & NETIF_F_RXCSUM))
1345 return;
1346
1347 if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1348 u64_stats_update_begin(&r_vec->rx_sync);
1349 r_vec->hw_csum_rx_error++;
1350 u64_stats_update_end(&r_vec->rx_sync);
1351 return;
1352 }
1353
1354 /* Assume that the firmware will never report inner CSUM_OK unless outer
1355 * L4 headers were successfully parsed. FW will always report zero UDP
1356 * checksum as CSUM_OK.
1357 */
1358 if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1359 rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1360 __skb_incr_checksum_unnecessary(skb);
1361 u64_stats_update_begin(&r_vec->rx_sync);
1362 r_vec->hw_csum_rx_ok++;
1363 u64_stats_update_end(&r_vec->rx_sync);
1364 }
1365
1366 if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1367 rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1368 __skb_incr_checksum_unnecessary(skb);
1369 u64_stats_update_begin(&r_vec->rx_sync);
1370 r_vec->hw_csum_rx_inner_ok++;
1371 u64_stats_update_end(&r_vec->rx_sync);
1372 }
1373 }
1374
1375 static void nfp_net_set_hash(struct net_device *netdev, struct sk_buff *skb,
1376 unsigned int type, __be32 *hash)
1377 {
1378 if (!(netdev->features & NETIF_F_RXHASH))
1379 return;
1380
1381 switch (type) {
1382 case NFP_NET_RSS_IPV4:
1383 case NFP_NET_RSS_IPV6:
1384 case NFP_NET_RSS_IPV6_EX:
1385 skb_set_hash(skb, get_unaligned_be32(hash), PKT_HASH_TYPE_L3);
1386 break;
1387 default:
1388 skb_set_hash(skb, get_unaligned_be32(hash), PKT_HASH_TYPE_L4);
1389 break;
1390 }
1391 }
1392
1393 static void
1394 nfp_net_set_hash_desc(struct net_device *netdev, struct sk_buff *skb,
1395 void *data, struct nfp_net_rx_desc *rxd)
1396 {
1397 struct nfp_net_rx_hash *rx_hash = data;
1398
1399 if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1400 return;
1401
1402 nfp_net_set_hash(netdev, skb, get_unaligned_be32(&rx_hash->hash_type),
1403 &rx_hash->hash);
1404 }
1405
1406 static void *
1407 nfp_net_parse_meta(struct net_device *netdev, struct sk_buff *skb,
1408 void *data, int meta_len)
1409 {
1410 u32 meta_info;
1411
1412 meta_info = get_unaligned_be32(data);
1413 data += 4;
1414
1415 while (meta_info) {
1416 switch (meta_info & NFP_NET_META_FIELD_MASK) {
1417 case NFP_NET_META_HASH:
1418 meta_info >>= NFP_NET_META_FIELD_SIZE;
1419 nfp_net_set_hash(netdev, skb,
1420 meta_info & NFP_NET_META_FIELD_MASK,
1421 (__be32 *)data);
1422 data += 4;
1423 break;
1424 case NFP_NET_META_MARK:
1425 skb->mark = get_unaligned_be32(data);
1426 data += 4;
1427 break;
1428 default:
1429 return NULL;
1430 }
1431
1432 meta_info >>= NFP_NET_META_FIELD_SIZE;
1433 }
1434
1435 return data;
1436 }
1437
1438 static void
1439 nfp_net_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
1440 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
1441 struct sk_buff *skb)
1442 {
1443 u64_stats_update_begin(&r_vec->rx_sync);
1444 r_vec->rx_drops++;
1445 u64_stats_update_end(&r_vec->rx_sync);
1446
1447 /* skb is build based on the frag, free_skb() would free the frag
1448 * so to be able to reuse it we need an extra ref.
1449 */
1450 if (skb && rxbuf && skb->head == rxbuf->frag)
1451 page_ref_inc(virt_to_head_page(rxbuf->frag));
1452 if (rxbuf)
1453 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
1454 if (skb)
1455 dev_kfree_skb_any(skb);
1456 }
1457
1458 static bool
1459 nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1460 struct nfp_net_tx_ring *tx_ring,
1461 struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
1462 unsigned int pkt_len)
1463 {
1464 struct nfp_net_tx_buf *txbuf;
1465 struct nfp_net_tx_desc *txd;
1466 dma_addr_t new_dma_addr;
1467 void *new_frag;
1468 int wr_idx;
1469
1470 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1471 nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf, NULL);
1472 return false;
1473 }
1474
1475 new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1476 if (unlikely(!new_frag)) {
1477 nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf, NULL);
1478 return false;
1479 }
1480 nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1481
1482 wr_idx = tx_ring->wr_p & (tx_ring->cnt - 1);
1483
1484 /* Stash the soft descriptor of the head then initialize it */
1485 txbuf = &tx_ring->txbufs[wr_idx];
1486 txbuf->frag = rxbuf->frag;
1487 txbuf->dma_addr = rxbuf->dma_addr;
1488 txbuf->fidx = -1;
1489 txbuf->pkt_cnt = 1;
1490 txbuf->real_len = pkt_len;
1491
1492 dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
1493 pkt_len, DMA_BIDIRECTIONAL);
1494
1495 /* Build TX descriptor */
1496 txd = &tx_ring->txds[wr_idx];
1497 txd->offset_eop = PCIE_DESC_TX_EOP;
1498 txd->dma_len = cpu_to_le16(pkt_len);
1499 nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
1500 txd->data_len = cpu_to_le16(pkt_len);
1501
1502 txd->flags = 0;
1503 txd->mss = 0;
1504 txd->l4_offset = 0;
1505
1506 tx_ring->wr_p++;
1507 tx_ring->wr_ptr_add++;
1508 return true;
1509 }
1510
1511 static int nfp_net_run_xdp(struct bpf_prog *prog, void *data, void *hard_start,
1512 unsigned int *off, unsigned int *len)
1513 {
1514 struct xdp_buff xdp;
1515 void *orig_data;
1516 int ret;
1517
1518 xdp.data_hard_start = hard_start;
1519 xdp.data = data + *off;
1520 xdp.data_end = data + *off + *len;
1521
1522 orig_data = xdp.data;
1523 ret = bpf_prog_run_xdp(prog, &xdp);
1524
1525 *len -= xdp.data - orig_data;
1526 *off += xdp.data - orig_data;
1527
1528 return ret;
1529 }
1530
1531 /**
1532 * nfp_net_rx() - receive up to @budget packets on @rx_ring
1533 * @rx_ring: RX ring to receive from
1534 * @budget: NAPI budget
1535 *
1536 * Note, this function is separated out from the napi poll function to
1537 * more cleanly separate packet receive code from other bookkeeping
1538 * functions performed in the napi poll function.
1539 *
1540 * Return: Number of packets received.
1541 */
1542 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1543 {
1544 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1545 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1546 struct nfp_net_tx_ring *tx_ring;
1547 struct bpf_prog *xdp_prog;
1548 unsigned int true_bufsz;
1549 struct sk_buff *skb;
1550 int pkts_polled = 0;
1551 int idx;
1552
1553 rcu_read_lock();
1554 xdp_prog = READ_ONCE(dp->xdp_prog);
1555 true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1556 tx_ring = r_vec->xdp_ring;
1557
1558 while (pkts_polled < budget) {
1559 unsigned int meta_len, data_len, data_off, pkt_len;
1560 u8 meta_prepend[NFP_NET_MAX_PREPEND];
1561 struct nfp_net_rx_buf *rxbuf;
1562 struct nfp_net_rx_desc *rxd;
1563 dma_addr_t new_dma_addr;
1564 void *new_frag;
1565 u8 *meta;
1566
1567 idx = rx_ring->rd_p & (rx_ring->cnt - 1);
1568
1569 rxd = &rx_ring->rxds[idx];
1570 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1571 break;
1572
1573 /* Memory barrier to ensure that we won't do other reads
1574 * before the DD bit.
1575 */
1576 dma_rmb();
1577
1578 rx_ring->rd_p++;
1579 pkts_polled++;
1580
1581 rxbuf = &rx_ring->rxbufs[idx];
1582 /* < meta_len >
1583 * <-- [rx_offset] -->
1584 * ---------------------------------------------------------
1585 * | [XX] | metadata | packet | XXXX |
1586 * ---------------------------------------------------------
1587 * <---------------- data_len --------------->
1588 *
1589 * The rx_offset is fixed for all packets, the meta_len can vary
1590 * on a packet by packet basis. If rx_offset is set to zero
1591 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1592 * buffer and is immediately followed by the packet (no [XX]).
1593 */
1594 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1595 data_len = le16_to_cpu(rxd->rxd.data_len);
1596 pkt_len = data_len - meta_len;
1597
1598 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1599 data_off = NFP_NET_RX_BUF_HEADROOM + meta_len;
1600 else
1601 data_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_offset;
1602 data_off += dp->rx_dma_off;
1603
1604 /* Stats update */
1605 u64_stats_update_begin(&r_vec->rx_sync);
1606 r_vec->rx_pkts++;
1607 r_vec->rx_bytes += pkt_len;
1608 u64_stats_update_end(&r_vec->rx_sync);
1609
1610 /* Pointer to start of metadata */
1611 meta = rxbuf->frag + data_off - meta_len;
1612
1613 if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1614 (dp->rx_offset && meta_len > dp->rx_offset))) {
1615 nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1616 meta_len);
1617 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1618 continue;
1619 }
1620
1621 if (xdp_prog && !(rxd->rxd.flags & PCIE_DESC_RX_BPF &&
1622 dp->bpf_offload_xdp)) {
1623 unsigned int dma_off;
1624 void *hard_start;
1625 int act;
1626
1627 hard_start = rxbuf->frag + NFP_NET_RX_BUF_HEADROOM;
1628 dma_off = data_off - NFP_NET_RX_BUF_HEADROOM;
1629 dma_sync_single_for_cpu(dp->dev, rxbuf->dma_addr,
1630 dma_off + pkt_len,
1631 DMA_BIDIRECTIONAL);
1632
1633 /* Move prepend out of the way */
1634 if (xdp_prog->xdp_adjust_head) {
1635 memcpy(meta_prepend, meta, meta_len);
1636 meta = meta_prepend;
1637 }
1638
1639 act = nfp_net_run_xdp(xdp_prog, rxbuf->frag, hard_start,
1640 &data_off, &pkt_len);
1641 switch (act) {
1642 case XDP_PASS:
1643 break;
1644 case XDP_TX:
1645 dma_off = data_off - NFP_NET_RX_BUF_HEADROOM;
1646 if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
1647 tx_ring, rxbuf,
1648 dma_off,
1649 pkt_len)))
1650 trace_xdp_exception(dp->netdev,
1651 xdp_prog, act);
1652 continue;
1653 default:
1654 bpf_warn_invalid_xdp_action(act);
1655 case XDP_ABORTED:
1656 trace_xdp_exception(dp->netdev, xdp_prog, act);
1657 case XDP_DROP:
1658 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1659 rxbuf->dma_addr);
1660 continue;
1661 }
1662 }
1663
1664 skb = build_skb(rxbuf->frag, true_bufsz);
1665 if (unlikely(!skb)) {
1666 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1667 continue;
1668 }
1669 new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1670 if (unlikely(!new_frag)) {
1671 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1672 continue;
1673 }
1674
1675 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1676
1677 nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1678
1679 skb_reserve(skb, data_off);
1680 skb_put(skb, pkt_len);
1681
1682 if (!dp->chained_metadata_format) {
1683 nfp_net_set_hash_desc(dp->netdev, skb, meta, rxd);
1684 } else if (meta_len) {
1685 void *end;
1686
1687 end = nfp_net_parse_meta(dp->netdev, skb, meta,
1688 meta_len);
1689 if (unlikely(end != meta + meta_len)) {
1690 nn_dp_warn(dp, "invalid RX packet metadata\n");
1691 nfp_net_rx_drop(dp, r_vec, rx_ring, NULL, skb);
1692 continue;
1693 }
1694 }
1695
1696 skb_record_rx_queue(skb, rx_ring->idx);
1697 skb->protocol = eth_type_trans(skb, dp->netdev);
1698
1699 nfp_net_rx_csum(dp, r_vec, rxd, skb);
1700
1701 if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
1702 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1703 le16_to_cpu(rxd->rxd.vlan));
1704
1705 napi_gro_receive(&rx_ring->r_vec->napi, skb);
1706 }
1707
1708 if (xdp_prog && tx_ring->wr_ptr_add)
1709 nfp_net_tx_xmit_more_flush(tx_ring);
1710 rcu_read_unlock();
1711
1712 return pkts_polled;
1713 }
1714
1715 /**
1716 * nfp_net_poll() - napi poll function
1717 * @napi: NAPI structure
1718 * @budget: NAPI budget
1719 *
1720 * Return: number of packets polled.
1721 */
1722 static int nfp_net_poll(struct napi_struct *napi, int budget)
1723 {
1724 struct nfp_net_r_vector *r_vec =
1725 container_of(napi, struct nfp_net_r_vector, napi);
1726 unsigned int pkts_polled = 0;
1727
1728 if (r_vec->tx_ring)
1729 nfp_net_tx_complete(r_vec->tx_ring);
1730 if (r_vec->rx_ring) {
1731 pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
1732 if (r_vec->xdp_ring)
1733 nfp_net_xdp_complete(r_vec->xdp_ring);
1734 }
1735
1736 if (pkts_polled < budget)
1737 if (napi_complete_done(napi, pkts_polled))
1738 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1739
1740 return pkts_polled;
1741 }
1742
1743 /* Setup and Configuration
1744 */
1745
1746 /**
1747 * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
1748 * @tx_ring: TX ring to free
1749 */
1750 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
1751 {
1752 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1753 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1754
1755 kfree(tx_ring->txbufs);
1756
1757 if (tx_ring->txds)
1758 dma_free_coherent(dp->dev, tx_ring->size,
1759 tx_ring->txds, tx_ring->dma);
1760
1761 tx_ring->cnt = 0;
1762 tx_ring->txbufs = NULL;
1763 tx_ring->txds = NULL;
1764 tx_ring->dma = 0;
1765 tx_ring->size = 0;
1766 }
1767
1768 /**
1769 * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
1770 * @dp: NFP Net data path struct
1771 * @tx_ring: TX Ring structure to allocate
1772 * @is_xdp: True if ring will be used for XDP
1773 *
1774 * Return: 0 on success, negative errno otherwise.
1775 */
1776 static int
1777 nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring,
1778 bool is_xdp)
1779 {
1780 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1781 int sz;
1782
1783 tx_ring->cnt = dp->txd_cnt;
1784
1785 tx_ring->size = sizeof(*tx_ring->txds) * tx_ring->cnt;
1786 tx_ring->txds = dma_zalloc_coherent(dp->dev, tx_ring->size,
1787 &tx_ring->dma, GFP_KERNEL);
1788 if (!tx_ring->txds)
1789 goto err_alloc;
1790
1791 sz = sizeof(*tx_ring->txbufs) * tx_ring->cnt;
1792 tx_ring->txbufs = kzalloc(sz, GFP_KERNEL);
1793 if (!tx_ring->txbufs)
1794 goto err_alloc;
1795
1796 if (!is_xdp)
1797 netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
1798 tx_ring->idx);
1799
1800 return 0;
1801
1802 err_alloc:
1803 nfp_net_tx_ring_free(tx_ring);
1804 return -ENOMEM;
1805 }
1806
1807 static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
1808 {
1809 unsigned int r;
1810
1811 dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
1812 GFP_KERNEL);
1813 if (!dp->tx_rings)
1814 return -ENOMEM;
1815
1816 for (r = 0; r < dp->num_tx_rings; r++) {
1817 int bias = 0;
1818
1819 if (r >= dp->num_stack_tx_rings)
1820 bias = dp->num_stack_tx_rings;
1821
1822 nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
1823 r);
1824
1825 if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r], bias))
1826 goto err_free_prev;
1827 }
1828
1829 return 0;
1830
1831 err_free_prev:
1832 while (r--)
1833 nfp_net_tx_ring_free(&dp->tx_rings[r]);
1834 kfree(dp->tx_rings);
1835 return -ENOMEM;
1836 }
1837
1838 static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
1839 {
1840 unsigned int r;
1841
1842 for (r = 0; r < dp->num_tx_rings; r++)
1843 nfp_net_tx_ring_free(&dp->tx_rings[r]);
1844
1845 kfree(dp->tx_rings);
1846 }
1847
1848 /**
1849 * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
1850 * @rx_ring: RX ring to free
1851 */
1852 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
1853 {
1854 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1855 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1856
1857 kfree(rx_ring->rxbufs);
1858
1859 if (rx_ring->rxds)
1860 dma_free_coherent(dp->dev, rx_ring->size,
1861 rx_ring->rxds, rx_ring->dma);
1862
1863 rx_ring->cnt = 0;
1864 rx_ring->rxbufs = NULL;
1865 rx_ring->rxds = NULL;
1866 rx_ring->dma = 0;
1867 rx_ring->size = 0;
1868 }
1869
1870 /**
1871 * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
1872 * @dp: NFP Net data path struct
1873 * @rx_ring: RX ring to allocate
1874 *
1875 * Return: 0 on success, negative errno otherwise.
1876 */
1877 static int
1878 nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
1879 {
1880 int sz;
1881
1882 rx_ring->cnt = dp->rxd_cnt;
1883 rx_ring->size = sizeof(*rx_ring->rxds) * rx_ring->cnt;
1884 rx_ring->rxds = dma_zalloc_coherent(dp->dev, rx_ring->size,
1885 &rx_ring->dma, GFP_KERNEL);
1886 if (!rx_ring->rxds)
1887 goto err_alloc;
1888
1889 sz = sizeof(*rx_ring->rxbufs) * rx_ring->cnt;
1890 rx_ring->rxbufs = kzalloc(sz, GFP_KERNEL);
1891 if (!rx_ring->rxbufs)
1892 goto err_alloc;
1893
1894 return 0;
1895
1896 err_alloc:
1897 nfp_net_rx_ring_free(rx_ring);
1898 return -ENOMEM;
1899 }
1900
1901 static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
1902 {
1903 unsigned int r;
1904
1905 dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
1906 GFP_KERNEL);
1907 if (!dp->rx_rings)
1908 return -ENOMEM;
1909
1910 for (r = 0; r < dp->num_rx_rings; r++) {
1911 nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
1912
1913 if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
1914 goto err_free_prev;
1915
1916 if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
1917 goto err_free_ring;
1918 }
1919
1920 return 0;
1921
1922 err_free_prev:
1923 while (r--) {
1924 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
1925 err_free_ring:
1926 nfp_net_rx_ring_free(&dp->rx_rings[r]);
1927 }
1928 kfree(dp->rx_rings);
1929 return -ENOMEM;
1930 }
1931
1932 static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
1933 {
1934 unsigned int r;
1935
1936 for (r = 0; r < dp->num_rx_rings; r++) {
1937 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
1938 nfp_net_rx_ring_free(&dp->rx_rings[r]);
1939 }
1940
1941 kfree(dp->rx_rings);
1942 }
1943
1944 static void
1945 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
1946 struct nfp_net_r_vector *r_vec, int idx)
1947 {
1948 r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
1949 r_vec->tx_ring =
1950 idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
1951
1952 r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
1953 &dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
1954 }
1955
1956 static int
1957 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1958 int idx)
1959 {
1960 int err;
1961
1962 /* Setup NAPI */
1963 netif_napi_add(nn->dp.netdev, &r_vec->napi,
1964 nfp_net_poll, NAPI_POLL_WEIGHT);
1965
1966 snprintf(r_vec->name, sizeof(r_vec->name),
1967 "%s-rxtx-%d", nn->dp.netdev->name, idx);
1968 err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
1969 r_vec);
1970 if (err) {
1971 netif_napi_del(&r_vec->napi);
1972 nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
1973 return err;
1974 }
1975 disable_irq(r_vec->irq_vector);
1976
1977 irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
1978
1979 nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
1980 r_vec->irq_entry);
1981
1982 return 0;
1983 }
1984
1985 static void
1986 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
1987 {
1988 irq_set_affinity_hint(r_vec->irq_vector, NULL);
1989 netif_napi_del(&r_vec->napi);
1990 free_irq(r_vec->irq_vector, r_vec);
1991 }
1992
1993 /**
1994 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
1995 * @nn: NFP Net device to reconfigure
1996 */
1997 void nfp_net_rss_write_itbl(struct nfp_net *nn)
1998 {
1999 int i;
2000
2001 for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
2002 nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
2003 get_unaligned_le32(nn->rss_itbl + i));
2004 }
2005
2006 /**
2007 * nfp_net_rss_write_key() - Write RSS hash key to device
2008 * @nn: NFP Net device to reconfigure
2009 */
2010 void nfp_net_rss_write_key(struct nfp_net *nn)
2011 {
2012 int i;
2013
2014 for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
2015 nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
2016 get_unaligned_le32(nn->rss_key + i));
2017 }
2018
2019 /**
2020 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2021 * @nn: NFP Net device to reconfigure
2022 */
2023 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
2024 {
2025 u8 i;
2026 u32 factor;
2027 u32 value;
2028
2029 /* Compute factor used to convert coalesce '_usecs' parameters to
2030 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
2031 * count.
2032 */
2033 factor = nn->me_freq_mhz / 16;
2034
2035 /* copy RX interrupt coalesce parameters */
2036 value = (nn->rx_coalesce_max_frames << 16) |
2037 (factor * nn->rx_coalesce_usecs);
2038 for (i = 0; i < nn->dp.num_rx_rings; i++)
2039 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
2040
2041 /* copy TX interrupt coalesce parameters */
2042 value = (nn->tx_coalesce_max_frames << 16) |
2043 (factor * nn->tx_coalesce_usecs);
2044 for (i = 0; i < nn->dp.num_tx_rings; i++)
2045 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
2046 }
2047
2048 /**
2049 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2050 * @nn: NFP Net device to reconfigure
2051 *
2052 * Writes the MAC address from the netdev to the device control BAR. Does not
2053 * perform the required reconfig. We do a bit of byte swapping dance because
2054 * firmware is LE.
2055 */
2056 static void nfp_net_write_mac_addr(struct nfp_net *nn)
2057 {
2058 nn_writel(nn, NFP_NET_CFG_MACADDR + 0,
2059 get_unaligned_be32(nn->dp.netdev->dev_addr));
2060 nn_writew(nn, NFP_NET_CFG_MACADDR + 6,
2061 get_unaligned_be16(nn->dp.netdev->dev_addr + 4));
2062 }
2063
2064 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
2065 {
2066 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
2067 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
2068 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
2069
2070 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
2071 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
2072 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
2073 }
2074
2075 /**
2076 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2077 * @nn: NFP Net device to reconfigure
2078 */
2079 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
2080 {
2081 u32 new_ctrl, update;
2082 unsigned int r;
2083 int err;
2084
2085 new_ctrl = nn->dp.ctrl;
2086 new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
2087 update = NFP_NET_CFG_UPDATE_GEN;
2088 update |= NFP_NET_CFG_UPDATE_MSIX;
2089 update |= NFP_NET_CFG_UPDATE_RING;
2090
2091 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2092 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
2093
2094 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2095 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2096
2097 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2098 err = nfp_net_reconfig(nn, update);
2099 if (err)
2100 nn_err(nn, "Could not disable device: %d\n", err);
2101
2102 for (r = 0; r < nn->dp.num_rx_rings; r++)
2103 nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
2104 for (r = 0; r < nn->dp.num_tx_rings; r++)
2105 nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
2106 for (r = 0; r < nn->dp.num_r_vecs; r++)
2107 nfp_net_vec_clear_ring_data(nn, r);
2108
2109 nn->dp.ctrl = new_ctrl;
2110 }
2111
2112 static void
2113 nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
2114 struct nfp_net_rx_ring *rx_ring, unsigned int idx)
2115 {
2116 /* Write the DMA address, size and MSI-X info to the device */
2117 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
2118 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
2119 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
2120 }
2121
2122 static void
2123 nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
2124 struct nfp_net_tx_ring *tx_ring, unsigned int idx)
2125 {
2126 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
2127 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
2128 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
2129 }
2130
2131 static int __nfp_net_set_config_and_enable(struct nfp_net *nn)
2132 {
2133 u32 new_ctrl, update = 0;
2134 unsigned int r;
2135 int err;
2136
2137 new_ctrl = nn->dp.ctrl;
2138
2139 if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
2140 nfp_net_rss_write_key(nn);
2141 nfp_net_rss_write_itbl(nn);
2142 nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
2143 update |= NFP_NET_CFG_UPDATE_RSS;
2144 }
2145
2146 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2147 nfp_net_coalesce_write_cfg(nn);
2148
2149 new_ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2150 update |= NFP_NET_CFG_UPDATE_IRQMOD;
2151 }
2152
2153 for (r = 0; r < nn->dp.num_tx_rings; r++)
2154 nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
2155 for (r = 0; r < nn->dp.num_rx_rings; r++)
2156 nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
2157
2158 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
2159 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
2160
2161 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
2162 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
2163
2164 nfp_net_write_mac_addr(nn);
2165
2166 nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.netdev->mtu);
2167 nn_writel(nn, NFP_NET_CFG_FLBUFSZ,
2168 nn->dp.fl_bufsz - NFP_NET_RX_BUF_NON_DATA);
2169
2170 /* Enable device */
2171 new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
2172 update |= NFP_NET_CFG_UPDATE_GEN;
2173 update |= NFP_NET_CFG_UPDATE_MSIX;
2174 update |= NFP_NET_CFG_UPDATE_RING;
2175 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2176 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
2177
2178 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2179 err = nfp_net_reconfig(nn, update);
2180
2181 nn->dp.ctrl = new_ctrl;
2182
2183 for (r = 0; r < nn->dp.num_rx_rings; r++)
2184 nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
2185
2186 /* Since reconfiguration requests while NFP is down are ignored we
2187 * have to wipe the entire VXLAN configuration and reinitialize it.
2188 */
2189 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2190 memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
2191 memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2192 udp_tunnel_get_rx_info(nn->dp.netdev);
2193 }
2194
2195 return err;
2196 }
2197
2198 /**
2199 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2200 * @nn: NFP Net device to reconfigure
2201 */
2202 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2203 {
2204 int err;
2205
2206 err = __nfp_net_set_config_and_enable(nn);
2207 if (err)
2208 nfp_net_clear_config_and_disable(nn);
2209
2210 return err;
2211 }
2212
2213 /**
2214 * nfp_net_open_stack() - Start the device from stack's perspective
2215 * @nn: NFP Net device to reconfigure
2216 */
2217 static void nfp_net_open_stack(struct nfp_net *nn)
2218 {
2219 unsigned int r;
2220
2221 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2222 napi_enable(&nn->r_vecs[r].napi);
2223 enable_irq(nn->r_vecs[r].irq_vector);
2224 }
2225
2226 netif_tx_wake_all_queues(nn->dp.netdev);
2227
2228 enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2229 nfp_net_read_link_status(nn);
2230 }
2231
2232 static int nfp_net_netdev_open(struct net_device *netdev)
2233 {
2234 struct nfp_net *nn = netdev_priv(netdev);
2235 int err, r;
2236
2237 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_ENABLE) {
2238 nn_err(nn, "Dev is already enabled: 0x%08x\n", nn->dp.ctrl);
2239 return -EBUSY;
2240 }
2241
2242 /* Step 1: Allocate resources for rings and the like
2243 * - Request interrupts
2244 * - Allocate RX and TX ring resources
2245 * - Setup initial RSS table
2246 */
2247 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
2248 nn->exn_name, sizeof(nn->exn_name),
2249 NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
2250 if (err)
2251 return err;
2252 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
2253 nn->lsc_name, sizeof(nn->lsc_name),
2254 NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
2255 if (err)
2256 goto err_free_exn;
2257 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2258
2259 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2260 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2261 if (err)
2262 goto err_cleanup_vec_p;
2263 }
2264
2265 err = nfp_net_rx_rings_prepare(nn, &nn->dp);
2266 if (err)
2267 goto err_cleanup_vec;
2268
2269 err = nfp_net_tx_rings_prepare(nn, &nn->dp);
2270 if (err)
2271 goto err_free_rx_rings;
2272
2273 for (r = 0; r < nn->max_r_vecs; r++)
2274 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2275
2276 err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
2277 if (err)
2278 goto err_free_rings;
2279
2280 err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
2281 if (err)
2282 goto err_free_rings;
2283
2284 /* Step 2: Configure the NFP
2285 * - Enable rings from 0 to tx_rings/rx_rings - 1.
2286 * - Write MAC address (in case it changed)
2287 * - Set the MTU
2288 * - Set the Freelist buffer size
2289 * - Enable the FW
2290 */
2291 err = nfp_net_set_config_and_enable(nn);
2292 if (err)
2293 goto err_free_rings;
2294
2295 /* Step 3: Enable for kernel
2296 * - put some freelist descriptors on each RX ring
2297 * - enable NAPI on each ring
2298 * - enable all TX queues
2299 * - set link state
2300 */
2301 nfp_net_open_stack(nn);
2302
2303 return 0;
2304
2305 err_free_rings:
2306 nfp_net_tx_rings_free(&nn->dp);
2307 err_free_rx_rings:
2308 nfp_net_rx_rings_free(&nn->dp);
2309 err_cleanup_vec:
2310 r = nn->dp.num_r_vecs;
2311 err_cleanup_vec_p:
2312 while (r--)
2313 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2314 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2315 err_free_exn:
2316 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2317 return err;
2318 }
2319
2320 /**
2321 * nfp_net_close_stack() - Quiescent the stack (part of close)
2322 * @nn: NFP Net device to reconfigure
2323 */
2324 static void nfp_net_close_stack(struct nfp_net *nn)
2325 {
2326 unsigned int r;
2327
2328 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2329 netif_carrier_off(nn->dp.netdev);
2330 nn->link_up = false;
2331
2332 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2333 disable_irq(nn->r_vecs[r].irq_vector);
2334 napi_disable(&nn->r_vecs[r].napi);
2335 }
2336
2337 netif_tx_disable(nn->dp.netdev);
2338 }
2339
2340 /**
2341 * nfp_net_close_free_all() - Free all runtime resources
2342 * @nn: NFP Net device to reconfigure
2343 */
2344 static void nfp_net_close_free_all(struct nfp_net *nn)
2345 {
2346 unsigned int r;
2347
2348 for (r = 0; r < nn->dp.num_rx_rings; r++) {
2349 nfp_net_rx_ring_bufs_free(&nn->dp, &nn->dp.rx_rings[r]);
2350 nfp_net_rx_ring_free(&nn->dp.rx_rings[r]);
2351 }
2352 for (r = 0; r < nn->dp.num_tx_rings; r++)
2353 nfp_net_tx_ring_free(&nn->dp.tx_rings[r]);
2354 for (r = 0; r < nn->dp.num_r_vecs; r++)
2355 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2356
2357 kfree(nn->dp.rx_rings);
2358 kfree(nn->dp.tx_rings);
2359
2360 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2361 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2362 }
2363
2364 /**
2365 * nfp_net_netdev_close() - Called when the device is downed
2366 * @netdev: netdev structure
2367 */
2368 static int nfp_net_netdev_close(struct net_device *netdev)
2369 {
2370 struct nfp_net *nn = netdev_priv(netdev);
2371
2372 if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_ENABLE)) {
2373 nn_err(nn, "Dev is not up: 0x%08x\n", nn->dp.ctrl);
2374 return 0;
2375 }
2376
2377 /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2378 */
2379 nfp_net_close_stack(nn);
2380
2381 /* Step 2: Tell NFP
2382 */
2383 nfp_net_clear_config_and_disable(nn);
2384
2385 /* Step 3: Free resources
2386 */
2387 nfp_net_close_free_all(nn);
2388
2389 nn_dbg(nn, "%s down", netdev->name);
2390 return 0;
2391 }
2392
2393 static void nfp_net_set_rx_mode(struct net_device *netdev)
2394 {
2395 struct nfp_net *nn = netdev_priv(netdev);
2396 u32 new_ctrl;
2397
2398 new_ctrl = nn->dp.ctrl;
2399
2400 if (netdev->flags & IFF_PROMISC) {
2401 if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
2402 new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
2403 else
2404 nn_warn(nn, "FW does not support promiscuous mode\n");
2405 } else {
2406 new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
2407 }
2408
2409 if (new_ctrl == nn->dp.ctrl)
2410 return;
2411
2412 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2413 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2414
2415 nn->dp.ctrl = new_ctrl;
2416 }
2417
2418 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
2419 {
2420 int i;
2421
2422 for (i = 0; i < sizeof(nn->rss_itbl); i++)
2423 nn->rss_itbl[i] =
2424 ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
2425 }
2426
2427 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
2428 {
2429 struct nfp_net_dp new_dp = *dp;
2430
2431 *dp = nn->dp;
2432 nn->dp = new_dp;
2433
2434 nn->dp.netdev->mtu = new_dp.mtu;
2435
2436 if (!netif_is_rxfh_configured(nn->dp.netdev))
2437 nfp_net_rss_init_itbl(nn);
2438 }
2439
2440 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
2441 {
2442 unsigned int r;
2443 int err;
2444
2445 nfp_net_dp_swap(nn, dp);
2446
2447 for (r = 0; r < nn->max_r_vecs; r++)
2448 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2449
2450 err = netif_set_real_num_rx_queues(nn->dp.netdev, nn->dp.num_rx_rings);
2451 if (err)
2452 return err;
2453
2454 if (nn->dp.netdev->real_num_tx_queues != nn->dp.num_stack_tx_rings) {
2455 err = netif_set_real_num_tx_queues(nn->dp.netdev,
2456 nn->dp.num_stack_tx_rings);
2457 if (err)
2458 return err;
2459 }
2460
2461 return __nfp_net_set_config_and_enable(nn);
2462 }
2463
2464 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
2465 {
2466 struct nfp_net_dp *new;
2467
2468 new = kmalloc(sizeof(*new), GFP_KERNEL);
2469 if (!new)
2470 return NULL;
2471
2472 *new = nn->dp;
2473
2474 /* Clear things which need to be recomputed */
2475 new->fl_bufsz = 0;
2476 new->tx_rings = NULL;
2477 new->rx_rings = NULL;
2478 new->num_r_vecs = 0;
2479 new->num_stack_tx_rings = 0;
2480
2481 return new;
2482 }
2483
2484 static int nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp)
2485 {
2486 /* XDP-enabled tests */
2487 if (!dp->xdp_prog)
2488 return 0;
2489 if (dp->fl_bufsz > PAGE_SIZE) {
2490 nn_warn(nn, "MTU too large w/ XDP enabled\n");
2491 return -EINVAL;
2492 }
2493 if (dp->num_tx_rings > nn->max_tx_rings) {
2494 nn_warn(nn, "Insufficient number of TX rings w/ XDP enabled\n");
2495 return -EINVAL;
2496 }
2497
2498 return 0;
2499 }
2500
2501 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp)
2502 {
2503 int r, err;
2504
2505 dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
2506
2507 dp->num_stack_tx_rings = dp->num_tx_rings;
2508 if (dp->xdp_prog)
2509 dp->num_stack_tx_rings -= dp->num_rx_rings;
2510
2511 dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
2512
2513 err = nfp_net_check_config(nn, dp);
2514 if (err)
2515 goto exit_free_dp;
2516
2517 if (!netif_running(dp->netdev)) {
2518 nfp_net_dp_swap(nn, dp);
2519 err = 0;
2520 goto exit_free_dp;
2521 }
2522
2523 /* Prepare new rings */
2524 for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
2525 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2526 if (err) {
2527 dp->num_r_vecs = r;
2528 goto err_cleanup_vecs;
2529 }
2530 }
2531
2532 err = nfp_net_rx_rings_prepare(nn, dp);
2533 if (err)
2534 goto err_cleanup_vecs;
2535
2536 err = nfp_net_tx_rings_prepare(nn, dp);
2537 if (err)
2538 goto err_free_rx;
2539
2540 /* Stop device, swap in new rings, try to start the firmware */
2541 nfp_net_close_stack(nn);
2542 nfp_net_clear_config_and_disable(nn);
2543
2544 err = nfp_net_dp_swap_enable(nn, dp);
2545 if (err) {
2546 int err2;
2547
2548 nfp_net_clear_config_and_disable(nn);
2549
2550 /* Try with old configuration and old rings */
2551 err2 = nfp_net_dp_swap_enable(nn, dp);
2552 if (err2)
2553 nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
2554 err, err2);
2555 }
2556 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
2557 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2558
2559 nfp_net_rx_rings_free(dp);
2560 nfp_net_tx_rings_free(dp);
2561
2562 nfp_net_open_stack(nn);
2563 exit_free_dp:
2564 kfree(dp);
2565
2566 return err;
2567
2568 err_free_rx:
2569 nfp_net_rx_rings_free(dp);
2570 err_cleanup_vecs:
2571 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
2572 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2573 kfree(dp);
2574 return err;
2575 }
2576
2577 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
2578 {
2579 struct nfp_net *nn = netdev_priv(netdev);
2580 struct nfp_net_dp *dp;
2581
2582 dp = nfp_net_clone_dp(nn);
2583 if (!dp)
2584 return -ENOMEM;
2585
2586 dp->mtu = new_mtu;
2587
2588 return nfp_net_ring_reconfig(nn, dp);
2589 }
2590
2591 static void nfp_net_stat64(struct net_device *netdev,
2592 struct rtnl_link_stats64 *stats)
2593 {
2594 struct nfp_net *nn = netdev_priv(netdev);
2595 int r;
2596
2597 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2598 struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
2599 u64 data[3];
2600 unsigned int start;
2601
2602 do {
2603 start = u64_stats_fetch_begin(&r_vec->rx_sync);
2604 data[0] = r_vec->rx_pkts;
2605 data[1] = r_vec->rx_bytes;
2606 data[2] = r_vec->rx_drops;
2607 } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
2608 stats->rx_packets += data[0];
2609 stats->rx_bytes += data[1];
2610 stats->rx_dropped += data[2];
2611
2612 do {
2613 start = u64_stats_fetch_begin(&r_vec->tx_sync);
2614 data[0] = r_vec->tx_pkts;
2615 data[1] = r_vec->tx_bytes;
2616 data[2] = r_vec->tx_errors;
2617 } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
2618 stats->tx_packets += data[0];
2619 stats->tx_bytes += data[1];
2620 stats->tx_errors += data[2];
2621 }
2622 }
2623
2624 static bool nfp_net_ebpf_capable(struct nfp_net *nn)
2625 {
2626 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
2627 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
2628 return true;
2629 return false;
2630 }
2631
2632 static int
2633 nfp_net_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
2634 struct tc_to_netdev *tc)
2635 {
2636 struct nfp_net *nn = netdev_priv(netdev);
2637
2638 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2639 return -ENOTSUPP;
2640 if (proto != htons(ETH_P_ALL))
2641 return -ENOTSUPP;
2642
2643 if (tc->type == TC_SETUP_CLSBPF && nfp_net_ebpf_capable(nn)) {
2644 if (!nn->dp.bpf_offload_xdp)
2645 return nfp_net_bpf_offload(nn, tc->cls_bpf);
2646 else
2647 return -EBUSY;
2648 }
2649
2650 return -EINVAL;
2651 }
2652
2653 static int nfp_net_set_features(struct net_device *netdev,
2654 netdev_features_t features)
2655 {
2656 netdev_features_t changed = netdev->features ^ features;
2657 struct nfp_net *nn = netdev_priv(netdev);
2658 u32 new_ctrl;
2659 int err;
2660
2661 /* Assume this is not called with features we have not advertised */
2662
2663 new_ctrl = nn->dp.ctrl;
2664
2665 if (changed & NETIF_F_RXCSUM) {
2666 if (features & NETIF_F_RXCSUM)
2667 new_ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
2668 else
2669 new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM;
2670 }
2671
2672 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2673 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
2674 new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2675 else
2676 new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
2677 }
2678
2679 if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
2680 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
2681 new_ctrl |= NFP_NET_CFG_CTRL_LSO;
2682 else
2683 new_ctrl &= ~NFP_NET_CFG_CTRL_LSO;
2684 }
2685
2686 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2687 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2688 new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
2689 else
2690 new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
2691 }
2692
2693 if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
2694 if (features & NETIF_F_HW_VLAN_CTAG_TX)
2695 new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
2696 else
2697 new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
2698 }
2699
2700 if (changed & NETIF_F_SG) {
2701 if (features & NETIF_F_SG)
2702 new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
2703 else
2704 new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
2705 }
2706
2707 if (changed & NETIF_F_HW_TC && nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
2708 nn_err(nn, "Cannot disable HW TC offload while in use\n");
2709 return -EBUSY;
2710 }
2711
2712 nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
2713 netdev->features, features, changed);
2714
2715 if (new_ctrl == nn->dp.ctrl)
2716 return 0;
2717
2718 nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
2719 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2720 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
2721 if (err)
2722 return err;
2723
2724 nn->dp.ctrl = new_ctrl;
2725
2726 return 0;
2727 }
2728
2729 static netdev_features_t
2730 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
2731 netdev_features_t features)
2732 {
2733 u8 l4_hdr;
2734
2735 /* We can't do TSO over double tagged packets (802.1AD) */
2736 features &= vlan_features_check(skb, features);
2737
2738 if (!skb->encapsulation)
2739 return features;
2740
2741 /* Ensure that inner L4 header offset fits into TX descriptor field */
2742 if (skb_is_gso(skb)) {
2743 u32 hdrlen;
2744
2745 hdrlen = skb_inner_transport_header(skb) - skb->data +
2746 inner_tcp_hdrlen(skb);
2747
2748 if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
2749 features &= ~NETIF_F_GSO_MASK;
2750 }
2751
2752 /* VXLAN/GRE check */
2753 switch (vlan_get_protocol(skb)) {
2754 case htons(ETH_P_IP):
2755 l4_hdr = ip_hdr(skb)->protocol;
2756 break;
2757 case htons(ETH_P_IPV6):
2758 l4_hdr = ipv6_hdr(skb)->nexthdr;
2759 break;
2760 default:
2761 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2762 }
2763
2764 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
2765 skb->inner_protocol != htons(ETH_P_TEB) ||
2766 (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
2767 (l4_hdr == IPPROTO_UDP &&
2768 (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
2769 sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
2770 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2771
2772 return features;
2773 }
2774
2775 static int
2776 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
2777 {
2778 struct nfp_net *nn = netdev_priv(netdev);
2779 int err;
2780
2781 if (!nn->eth_port)
2782 return -EOPNOTSUPP;
2783
2784 if (!nn->eth_port->is_split)
2785 err = snprintf(name, len, "p%d", nn->eth_port->label_port);
2786 else
2787 err = snprintf(name, len, "p%ds%d", nn->eth_port->label_port,
2788 nn->eth_port->label_subport);
2789 if (err >= len)
2790 return -EINVAL;
2791
2792 return 0;
2793 }
2794
2795 /**
2796 * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
2797 * @nn: NFP Net device to reconfigure
2798 * @idx: Index into the port table where new port should be written
2799 * @port: UDP port to configure (pass zero to remove VXLAN port)
2800 */
2801 static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
2802 {
2803 int i;
2804
2805 nn->vxlan_ports[idx] = port;
2806
2807 if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN))
2808 return;
2809
2810 BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
2811 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
2812 nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
2813 be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
2814 be16_to_cpu(nn->vxlan_ports[i]));
2815
2816 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
2817 }
2818
2819 /**
2820 * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
2821 * @nn: NFP Network structure
2822 * @port: UDP port to look for
2823 *
2824 * Return: if the port is already in the table -- it's position;
2825 * if the port is not in the table -- free position to use;
2826 * if the table is full -- -ENOSPC.
2827 */
2828 static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
2829 {
2830 int i, free_idx = -ENOSPC;
2831
2832 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
2833 if (nn->vxlan_ports[i] == port)
2834 return i;
2835 if (!nn->vxlan_usecnt[i])
2836 free_idx = i;
2837 }
2838
2839 return free_idx;
2840 }
2841
2842 static void nfp_net_add_vxlan_port(struct net_device *netdev,
2843 struct udp_tunnel_info *ti)
2844 {
2845 struct nfp_net *nn = netdev_priv(netdev);
2846 int idx;
2847
2848 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2849 return;
2850
2851 idx = nfp_net_find_vxlan_idx(nn, ti->port);
2852 if (idx == -ENOSPC)
2853 return;
2854
2855 if (!nn->vxlan_usecnt[idx]++)
2856 nfp_net_set_vxlan_port(nn, idx, ti->port);
2857 }
2858
2859 static void nfp_net_del_vxlan_port(struct net_device *netdev,
2860 struct udp_tunnel_info *ti)
2861 {
2862 struct nfp_net *nn = netdev_priv(netdev);
2863 int idx;
2864
2865 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2866 return;
2867
2868 idx = nfp_net_find_vxlan_idx(nn, ti->port);
2869 if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
2870 return;
2871
2872 if (!--nn->vxlan_usecnt[idx])
2873 nfp_net_set_vxlan_port(nn, idx, 0);
2874 }
2875
2876 static int nfp_net_xdp_offload(struct nfp_net *nn, struct bpf_prog *prog)
2877 {
2878 struct tc_cls_bpf_offload cmd = {
2879 .prog = prog,
2880 };
2881 int ret;
2882
2883 if (!nfp_net_ebpf_capable(nn))
2884 return -EINVAL;
2885
2886 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
2887 if (!nn->dp.bpf_offload_xdp)
2888 return prog ? -EBUSY : 0;
2889 cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
2890 } else {
2891 if (!prog)
2892 return 0;
2893 cmd.command = TC_CLSBPF_ADD;
2894 }
2895
2896 ret = nfp_net_bpf_offload(nn, &cmd);
2897 /* Stop offload if replace not possible */
2898 if (ret && cmd.command == TC_CLSBPF_REPLACE)
2899 nfp_net_xdp_offload(nn, NULL);
2900 nn->dp.bpf_offload_xdp = prog && !ret;
2901 return ret;
2902 }
2903
2904 static int nfp_net_xdp_setup(struct nfp_net *nn, struct bpf_prog *prog)
2905 {
2906 struct bpf_prog *old_prog = nn->dp.xdp_prog;
2907 struct nfp_net_dp *dp;
2908 int err;
2909
2910 if (!prog && !nn->dp.xdp_prog)
2911 return 0;
2912 if (prog && nn->dp.xdp_prog) {
2913 prog = xchg(&nn->dp.xdp_prog, prog);
2914 bpf_prog_put(prog);
2915 nfp_net_xdp_offload(nn, nn->dp.xdp_prog);
2916 return 0;
2917 }
2918
2919 dp = nfp_net_clone_dp(nn);
2920 if (!dp)
2921 return -ENOMEM;
2922
2923 dp->xdp_prog = prog;
2924 dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
2925 dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
2926 if (prog)
2927 dp->rx_dma_off = XDP_PACKET_HEADROOM -
2928 (nn->dp.rx_offset ?: NFP_NET_MAX_PREPEND);
2929 else
2930 dp->rx_dma_off = 0;
2931
2932 /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
2933 err = nfp_net_ring_reconfig(nn, dp);
2934 if (err)
2935 return err;
2936
2937 if (old_prog)
2938 bpf_prog_put(old_prog);
2939
2940 nfp_net_xdp_offload(nn, nn->dp.xdp_prog);
2941
2942 return 0;
2943 }
2944
2945 static int nfp_net_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
2946 {
2947 struct nfp_net *nn = netdev_priv(netdev);
2948
2949 switch (xdp->command) {
2950 case XDP_SETUP_PROG:
2951 return nfp_net_xdp_setup(nn, xdp->prog);
2952 case XDP_QUERY_PROG:
2953 xdp->prog_attached = !!nn->dp.xdp_prog;
2954 return 0;
2955 default:
2956 return -EINVAL;
2957 }
2958 }
2959
2960 static const struct net_device_ops nfp_net_netdev_ops = {
2961 .ndo_open = nfp_net_netdev_open,
2962 .ndo_stop = nfp_net_netdev_close,
2963 .ndo_start_xmit = nfp_net_tx,
2964 .ndo_get_stats64 = nfp_net_stat64,
2965 .ndo_setup_tc = nfp_net_setup_tc,
2966 .ndo_tx_timeout = nfp_net_tx_timeout,
2967 .ndo_set_rx_mode = nfp_net_set_rx_mode,
2968 .ndo_change_mtu = nfp_net_change_mtu,
2969 .ndo_set_mac_address = eth_mac_addr,
2970 .ndo_set_features = nfp_net_set_features,
2971 .ndo_features_check = nfp_net_features_check,
2972 .ndo_get_phys_port_name = nfp_net_get_phys_port_name,
2973 .ndo_udp_tunnel_add = nfp_net_add_vxlan_port,
2974 .ndo_udp_tunnel_del = nfp_net_del_vxlan_port,
2975 .ndo_xdp = nfp_net_xdp,
2976 };
2977
2978 /**
2979 * nfp_net_info() - Print general info about the NIC
2980 * @nn: NFP Net device to reconfigure
2981 */
2982 void nfp_net_info(struct nfp_net *nn)
2983 {
2984 nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
2985 nn->dp.is_vf ? "VF " : "",
2986 nn->dp.num_tx_rings, nn->max_tx_rings,
2987 nn->dp.num_rx_rings, nn->max_rx_rings);
2988 nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
2989 nn->fw_ver.resv, nn->fw_ver.class,
2990 nn->fw_ver.major, nn->fw_ver.minor,
2991 nn->max_mtu);
2992 nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2993 nn->cap,
2994 nn->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
2995 nn->cap & NFP_NET_CFG_CTRL_L2BC ? "L2BCFILT " : "",
2996 nn->cap & NFP_NET_CFG_CTRL_L2MC ? "L2MCFILT " : "",
2997 nn->cap & NFP_NET_CFG_CTRL_RXCSUM ? "RXCSUM " : "",
2998 nn->cap & NFP_NET_CFG_CTRL_TXCSUM ? "TXCSUM " : "",
2999 nn->cap & NFP_NET_CFG_CTRL_RXVLAN ? "RXVLAN " : "",
3000 nn->cap & NFP_NET_CFG_CTRL_TXVLAN ? "TXVLAN " : "",
3001 nn->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
3002 nn->cap & NFP_NET_CFG_CTRL_GATHER ? "GATHER " : "",
3003 nn->cap & NFP_NET_CFG_CTRL_LSO ? "TSO " : "",
3004 nn->cap & NFP_NET_CFG_CTRL_RSS ? "RSS " : "",
3005 nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
3006 nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
3007 nn->cap & NFP_NET_CFG_CTRL_IRQMOD ? "IRQMOD " : "",
3008 nn->cap & NFP_NET_CFG_CTRL_VXLAN ? "VXLAN " : "",
3009 nn->cap & NFP_NET_CFG_CTRL_NVGRE ? "NVGRE " : "",
3010 nfp_net_ebpf_capable(nn) ? "BPF " : "");
3011 }
3012
3013 /**
3014 * nfp_net_netdev_alloc() - Allocate netdev and related structure
3015 * @pdev: PCI device
3016 * @max_tx_rings: Maximum number of TX rings supported by device
3017 * @max_rx_rings: Maximum number of RX rings supported by device
3018 *
3019 * This function allocates a netdev device and fills in the initial
3020 * part of the @struct nfp_net structure.
3021 *
3022 * Return: NFP Net device structure, or ERR_PTR on error.
3023 */
3024 struct nfp_net *nfp_net_netdev_alloc(struct pci_dev *pdev,
3025 unsigned int max_tx_rings,
3026 unsigned int max_rx_rings)
3027 {
3028 struct net_device *netdev;
3029 struct nfp_net *nn;
3030
3031 netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
3032 max_tx_rings, max_rx_rings);
3033 if (!netdev)
3034 return ERR_PTR(-ENOMEM);
3035
3036 SET_NETDEV_DEV(netdev, &pdev->dev);
3037 nn = netdev_priv(netdev);
3038
3039 nn->dp.netdev = netdev;
3040 nn->dp.dev = &pdev->dev;
3041 nn->pdev = pdev;
3042
3043 nn->max_tx_rings = max_tx_rings;
3044 nn->max_rx_rings = max_rx_rings;
3045
3046 nn->dp.num_tx_rings = min_t(unsigned int,
3047 max_tx_rings, num_online_cpus());
3048 nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
3049 netif_get_num_default_rss_queues());
3050
3051 nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
3052 nn->dp.num_r_vecs = min_t(unsigned int,
3053 nn->dp.num_r_vecs, num_online_cpus());
3054
3055 nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
3056 nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
3057
3058 spin_lock_init(&nn->reconfig_lock);
3059 spin_lock_init(&nn->rx_filter_lock);
3060 spin_lock_init(&nn->link_status_lock);
3061
3062 setup_timer(&nn->reconfig_timer,
3063 nfp_net_reconfig_timer, (unsigned long)nn);
3064 setup_timer(&nn->rx_filter_stats_timer,
3065 nfp_net_filter_stats_timer, (unsigned long)nn);
3066
3067 return nn;
3068 }
3069
3070 /**
3071 * nfp_net_netdev_free() - Undo what @nfp_net_netdev_alloc() did
3072 * @nn: NFP Net device to reconfigure
3073 */
3074 void nfp_net_netdev_free(struct nfp_net *nn)
3075 {
3076 free_netdev(nn->dp.netdev);
3077 }
3078
3079 /**
3080 * nfp_net_rss_key_sz() - Get current size of the RSS key
3081 * @nn: NFP Net device instance
3082 *
3083 * Return: size of the RSS key for currently selected hash function.
3084 */
3085 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
3086 {
3087 switch (nn->rss_hfunc) {
3088 case ETH_RSS_HASH_TOP:
3089 return NFP_NET_CFG_RSS_KEY_SZ;
3090 case ETH_RSS_HASH_XOR:
3091 return 0;
3092 case ETH_RSS_HASH_CRC32:
3093 return 4;
3094 }
3095
3096 nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
3097 return 0;
3098 }
3099
3100 /**
3101 * nfp_net_rss_init() - Set the initial RSS parameters
3102 * @nn: NFP Net device to reconfigure
3103 */
3104 static void nfp_net_rss_init(struct nfp_net *nn)
3105 {
3106 unsigned long func_bit, rss_cap_hfunc;
3107 u32 reg;
3108
3109 /* Read the RSS function capability and select first supported func */
3110 reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
3111 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
3112 if (!rss_cap_hfunc)
3113 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
3114 NFP_NET_CFG_RSS_TOEPLITZ);
3115
3116 func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
3117 if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
3118 dev_warn(nn->dp.dev,
3119 "Bad RSS config, defaulting to Toeplitz hash\n");
3120 func_bit = ETH_RSS_HASH_TOP_BIT;
3121 }
3122 nn->rss_hfunc = 1 << func_bit;
3123
3124 netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
3125
3126 nfp_net_rss_init_itbl(nn);
3127
3128 /* Enable IPv4/IPv6 TCP by default */
3129 nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
3130 NFP_NET_CFG_RSS_IPV6_TCP |
3131 FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
3132 NFP_NET_CFG_RSS_MASK;
3133 }
3134
3135 /**
3136 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3137 * @nn: NFP Net device to reconfigure
3138 */
3139 static void nfp_net_irqmod_init(struct nfp_net *nn)
3140 {
3141 nn->rx_coalesce_usecs = 50;
3142 nn->rx_coalesce_max_frames = 64;
3143 nn->tx_coalesce_usecs = 50;
3144 nn->tx_coalesce_max_frames = 64;
3145 }
3146
3147 /**
3148 * nfp_net_netdev_init() - Initialise/finalise the netdev structure
3149 * @netdev: netdev structure
3150 *
3151 * Return: 0 on success or negative errno on error.
3152 */
3153 int nfp_net_netdev_init(struct net_device *netdev)
3154 {
3155 struct nfp_net *nn = netdev_priv(netdev);
3156 int err;
3157
3158 /* XDP calls for 256 byte packet headroom which wouldn't fit in a u8.
3159 * We, however, reuse the metadata prepend space for XDP buffers which
3160 * is at least 1 byte long and as long as XDP headroom doesn't increase
3161 * above 256 the *extra* XDP headroom will fit on 8 bits.
3162 */
3163 BUILD_BUG_ON(XDP_PACKET_HEADROOM > 256);
3164
3165 nn->dp.chained_metadata_format = nn->fw_ver.major > 3;
3166
3167 nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
3168
3169 /* Get some of the read-only fields from the BAR */
3170 nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
3171 nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
3172
3173 nfp_net_write_mac_addr(nn);
3174
3175 /* Determine RX packet/metadata boundary offset */
3176 if (nn->fw_ver.major >= 2) {
3177 u32 reg;
3178
3179 reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
3180 if (reg > NFP_NET_MAX_PREPEND) {
3181 nn_err(nn, "Invalid rx offset: %d\n", reg);
3182 return -EINVAL;
3183 }
3184 nn->dp.rx_offset = reg;
3185 } else {
3186 nn->dp.rx_offset = NFP_NET_RX_OFFSET;
3187 }
3188
3189 /* Set default MTU and Freelist buffer size */
3190 if (nn->max_mtu < NFP_NET_DEFAULT_MTU)
3191 netdev->mtu = nn->max_mtu;
3192 else
3193 netdev->mtu = NFP_NET_DEFAULT_MTU;
3194 nn->dp.mtu = netdev->mtu;
3195 nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
3196
3197 /* Advertise/enable offloads based on capabilities
3198 *
3199 * Note: netdev->features show the currently enabled features
3200 * and netdev->hw_features advertises which features are
3201 * supported. By default we enable most features.
3202 */
3203 netdev->hw_features = NETIF_F_HIGHDMA;
3204 if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM) {
3205 netdev->hw_features |= NETIF_F_RXCSUM;
3206 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
3207 }
3208 if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
3209 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3210 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3211 }
3212 if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
3213 netdev->hw_features |= NETIF_F_SG;
3214 nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
3215 }
3216 if ((nn->cap & NFP_NET_CFG_CTRL_LSO) && nn->fw_ver.major > 2) {
3217 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3218 nn->dp.ctrl |= NFP_NET_CFG_CTRL_LSO;
3219 }
3220 if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
3221 netdev->hw_features |= NETIF_F_RXHASH;
3222 nfp_net_rss_init(nn);
3223 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RSS;
3224 }
3225 if (nn->cap & NFP_NET_CFG_CTRL_VXLAN &&
3226 nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
3227 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3228 netdev->hw_features |= NETIF_F_GSO_GRE |
3229 NETIF_F_GSO_UDP_TUNNEL;
3230 nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE;
3231
3232 netdev->hw_enc_features = netdev->hw_features;
3233 }
3234
3235 netdev->vlan_features = netdev->hw_features;
3236
3237 if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
3238 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3239 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3240 }
3241 if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
3242 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3243 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3244 }
3245
3246 netdev->features = netdev->hw_features;
3247
3248 if (nfp_net_ebpf_capable(nn))
3249 netdev->hw_features |= NETIF_F_HW_TC;
3250
3251 /* Advertise but disable TSO by default. */
3252 netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
3253
3254 /* Allow L2 Broadcast and Multicast through by default, if supported */
3255 if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
3256 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
3257 if (nn->cap & NFP_NET_CFG_CTRL_L2MC)
3258 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2MC;
3259
3260 /* Allow IRQ moderation, if supported */
3261 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
3262 nfp_net_irqmod_init(nn);
3263 nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
3264 }
3265
3266 /* Stash the re-configuration queue away. First odd queue in TX Bar */
3267 nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
3268
3269 /* Make sure the FW knows the netdev is supposed to be disabled here */
3270 nn_writel(nn, NFP_NET_CFG_CTRL, 0);
3271 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
3272 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
3273 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
3274 NFP_NET_CFG_UPDATE_GEN);
3275 if (err)
3276 return err;
3277
3278 /* Finalise the netdev setup */
3279 netdev->netdev_ops = &nfp_net_netdev_ops;
3280 netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
3281
3282 /* MTU range: 68 - hw-specific max */
3283 netdev->min_mtu = ETH_MIN_MTU;
3284 netdev->max_mtu = nn->max_mtu;
3285
3286 netif_carrier_off(netdev);
3287
3288 nfp_net_set_ethtool_ops(netdev);
3289 nfp_net_vecs_init(netdev);
3290
3291 return register_netdev(netdev);
3292 }
3293
3294 /**
3295 * nfp_net_netdev_clean() - Undo what nfp_net_netdev_init() did.
3296 * @netdev: netdev structure
3297 */
3298 void nfp_net_netdev_clean(struct net_device *netdev)
3299 {
3300 struct nfp_net *nn = netdev_priv(netdev);
3301
3302 if (nn->dp.xdp_prog)
3303 bpf_prog_put(nn->dp.xdp_prog);
3304 if (nn->dp.bpf_offload_xdp)
3305 nfp_net_xdp_offload(nn, NULL);
3306 unregister_netdev(nn->dp.netdev);
3307 }