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