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