1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2019 Netronome Systems, Inc. */
3
4 /*
5 * nfp_net_common.c
6 * Netronome network device driver: Common functions between PF and VF
7 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
8 * Jason McMullan <jason.mcmullan@netronome.com>
9 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
10 * Brad Petrus <brad.petrus@netronome.com>
11 * Chris Telfer <chris.telfer@netronome.com>
12 */
13
14 #include <linux/bitfield.h>
15 #include <linux/bpf.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/fs.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/interrupt.h>
23 #include <linux/ip.h>
24 #include <linux/ipv6.h>
25 #include <linux/mm.h>
26 #include <linux/overflow.h>
27 #include <linux/page_ref.h>
28 #include <linux/pci.h>
29 #include <linux/pci_regs.h>
30 #include <linux/msi.h>
31 #include <linux/ethtool.h>
32 #include <linux/log2.h>
33 #include <linux/if_vlan.h>
34 #include <linux/random.h>
35 #include <linux/vmalloc.h>
36 #include <linux/ktime.h>
37
38 #include <net/tls.h>
39 #include <net/vxlan.h>
40 #include <net/xdp_sock_drv.h>
41
42 #include "nfpcore/nfp_dev.h"
43 #include "nfpcore/nfp_nsp.h"
44 #include "ccm.h"
45 #include "nfp_app.h"
46 #include "nfp_net_ctrl.h"
47 #include "nfp_net.h"
48 #include "nfp_net_dp.h"
49 #include "nfp_net_sriov.h"
50 #include "nfp_net_xsk.h"
51 #include "nfp_port.h"
52 #include "crypto/crypto.h"
53 #include "crypto/fw.h"
54
55 /**
56 * nfp_net_get_fw_version() - Read and parse the FW version
57 * @fw_ver: Output fw_version structure to read to
58 * @ctrl_bar: Mapped address of the control BAR
59 */
nfp_net_get_fw_version(struct nfp_net_fw_version * fw_ver,void __iomem * ctrl_bar)60 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
61 void __iomem *ctrl_bar)
62 {
63 u32 reg;
64
65 reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
66 put_unaligned_le32(reg, fw_ver);
67 }
68
nfp_qcp_queue_offset(const struct nfp_dev_info * dev_info,u16 queue)69 u32 nfp_qcp_queue_offset(const struct nfp_dev_info *dev_info, u16 queue)
70 {
71 queue &= dev_info->qc_idx_mask;
72 return dev_info->qc_addr_offset + NFP_QCP_QUEUE_ADDR_SZ * queue;
73 }
74
75 /* Firmware reconfig
76 *
77 * Firmware reconfig may take a while so we have two versions of it -
78 * synchronous and asynchronous (posted). All synchronous callers are holding
79 * RTNL so we don't have to worry about serializing them.
80 */
nfp_net_reconfig_start(struct nfp_net * nn,u32 update)81 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
82 {
83 nn_writel(nn, NFP_NET_CFG_UPDATE, update);
84 /* ensure update is written before pinging HW */
85 nn_pci_flush(nn);
86 nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
87 nn->reconfig_in_progress_update = update;
88 }
89
90 /* Pass 0 as update to run posted reconfigs. */
nfp_net_reconfig_start_async(struct nfp_net * nn,u32 update)91 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
92 {
93 update |= nn->reconfig_posted;
94 nn->reconfig_posted = 0;
95
96 nfp_net_reconfig_start(nn, update);
97
98 nn->reconfig_timer_active = true;
99 mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
100 }
101
nfp_net_reconfig_check_done(struct nfp_net * nn,bool last_check)102 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
103 {
104 u32 reg;
105
106 reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
107 if (reg == 0)
108 return true;
109 if (reg & NFP_NET_CFG_UPDATE_ERR) {
110 nn_err(nn, "Reconfig error (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
111 reg, nn->reconfig_in_progress_update,
112 nn_readl(nn, NFP_NET_CFG_CTRL));
113 return true;
114 } else if (last_check) {
115 nn_err(nn, "Reconfig timeout (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
116 reg, nn->reconfig_in_progress_update,
117 nn_readl(nn, NFP_NET_CFG_CTRL));
118 return true;
119 }
120
121 return false;
122 }
123
__nfp_net_reconfig_wait(struct nfp_net * nn,unsigned long deadline)124 static bool __nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
125 {
126 bool timed_out = false;
127 int i;
128
129 /* Poll update field, waiting for NFP to ack the config.
130 * Do an opportunistic wait-busy loop, afterward sleep.
131 */
132 for (i = 0; i < 50; i++) {
133 if (nfp_net_reconfig_check_done(nn, false))
134 return false;
135 udelay(4);
136 }
137
138 while (!nfp_net_reconfig_check_done(nn, timed_out)) {
139 usleep_range(250, 500);
140 timed_out = time_is_before_eq_jiffies(deadline);
141 }
142
143 return timed_out;
144 }
145
nfp_net_reconfig_wait(struct nfp_net * nn,unsigned long deadline)146 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
147 {
148 if (__nfp_net_reconfig_wait(nn, deadline))
149 return -EIO;
150
151 if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
152 return -EIO;
153
154 return 0;
155 }
156
nfp_net_reconfig_timer(struct timer_list * t)157 static void nfp_net_reconfig_timer(struct timer_list *t)
158 {
159 struct nfp_net *nn = from_timer(nn, t, reconfig_timer);
160
161 spin_lock_bh(&nn->reconfig_lock);
162
163 nn->reconfig_timer_active = false;
164
165 /* If sync caller is present it will take over from us */
166 if (nn->reconfig_sync_present)
167 goto done;
168
169 /* Read reconfig status and report errors */
170 nfp_net_reconfig_check_done(nn, true);
171
172 if (nn->reconfig_posted)
173 nfp_net_reconfig_start_async(nn, 0);
174 done:
175 spin_unlock_bh(&nn->reconfig_lock);
176 }
177
178 /**
179 * nfp_net_reconfig_post() - Post async reconfig request
180 * @nn: NFP Net device to reconfigure
181 * @update: The value for the update field in the BAR config
182 *
183 * Record FW reconfiguration request. Reconfiguration will be kicked off
184 * whenever reconfiguration machinery is idle. Multiple requests can be
185 * merged together!
186 */
nfp_net_reconfig_post(struct nfp_net * nn,u32 update)187 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
188 {
189 spin_lock_bh(&nn->reconfig_lock);
190
191 /* Sync caller will kick off async reconf when it's done, just post */
192 if (nn->reconfig_sync_present) {
193 nn->reconfig_posted |= update;
194 goto done;
195 }
196
197 /* Opportunistically check if the previous command is done */
198 if (!nn->reconfig_timer_active ||
199 nfp_net_reconfig_check_done(nn, false))
200 nfp_net_reconfig_start_async(nn, update);
201 else
202 nn->reconfig_posted |= update;
203 done:
204 spin_unlock_bh(&nn->reconfig_lock);
205 }
206
nfp_net_reconfig_sync_enter(struct nfp_net * nn)207 static void nfp_net_reconfig_sync_enter(struct nfp_net *nn)
208 {
209 bool cancelled_timer = false;
210 u32 pre_posted_requests;
211
212 spin_lock_bh(&nn->reconfig_lock);
213
214 WARN_ON(nn->reconfig_sync_present);
215 nn->reconfig_sync_present = true;
216
217 if (nn->reconfig_timer_active) {
218 nn->reconfig_timer_active = false;
219 cancelled_timer = true;
220 }
221 pre_posted_requests = nn->reconfig_posted;
222 nn->reconfig_posted = 0;
223
224 spin_unlock_bh(&nn->reconfig_lock);
225
226 if (cancelled_timer) {
227 del_timer_sync(&nn->reconfig_timer);
228 nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
229 }
230
231 /* Run the posted reconfigs which were issued before we started */
232 if (pre_posted_requests) {
233 nfp_net_reconfig_start(nn, pre_posted_requests);
234 nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
235 }
236 }
237
nfp_net_reconfig_wait_posted(struct nfp_net * nn)238 static void nfp_net_reconfig_wait_posted(struct nfp_net *nn)
239 {
240 nfp_net_reconfig_sync_enter(nn);
241
242 spin_lock_bh(&nn->reconfig_lock);
243 nn->reconfig_sync_present = false;
244 spin_unlock_bh(&nn->reconfig_lock);
245 }
246
247 /**
248 * __nfp_net_reconfig() - Reconfigure the firmware
249 * @nn: NFP Net device to reconfigure
250 * @update: The value for the update field in the BAR config
251 *
252 * Write the update word to the BAR and ping the reconfig queue. The
253 * poll until the firmware has acknowledged the update by zeroing the
254 * update word.
255 *
256 * Return: Negative errno on error, 0 on success
257 */
__nfp_net_reconfig(struct nfp_net * nn,u32 update)258 int __nfp_net_reconfig(struct nfp_net *nn, u32 update)
259 {
260 int ret;
261
262 nfp_net_reconfig_sync_enter(nn);
263
264 nfp_net_reconfig_start(nn, update);
265 ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
266
267 spin_lock_bh(&nn->reconfig_lock);
268
269 if (nn->reconfig_posted)
270 nfp_net_reconfig_start_async(nn, 0);
271
272 nn->reconfig_sync_present = false;
273
274 spin_unlock_bh(&nn->reconfig_lock);
275
276 return ret;
277 }
278
nfp_net_reconfig(struct nfp_net * nn,u32 update)279 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
280 {
281 int ret;
282
283 nn_ctrl_bar_lock(nn);
284 ret = __nfp_net_reconfig(nn, update);
285 nn_ctrl_bar_unlock(nn);
286
287 return ret;
288 }
289
nfp_net_mbox_lock(struct nfp_net * nn,unsigned int data_size)290 int nfp_net_mbox_lock(struct nfp_net *nn, unsigned int data_size)
291 {
292 if (nn->tlv_caps.mbox_len < NFP_NET_CFG_MBOX_SIMPLE_VAL + data_size) {
293 nn_err(nn, "mailbox too small for %u of data (%u)\n",
294 data_size, nn->tlv_caps.mbox_len);
295 return -EIO;
296 }
297
298 nn_ctrl_bar_lock(nn);
299 return 0;
300 }
301
302 /**
303 * nfp_net_mbox_reconfig() - Reconfigure the firmware via the mailbox
304 * @nn: NFP Net device to reconfigure
305 * @mbox_cmd: The value for the mailbox command
306 *
307 * Helper function for mailbox updates
308 *
309 * Return: Negative errno on error, 0 on success
310 */
nfp_net_mbox_reconfig(struct nfp_net * nn,u32 mbox_cmd)311 int nfp_net_mbox_reconfig(struct nfp_net *nn, u32 mbox_cmd)
312 {
313 u32 mbox = nn->tlv_caps.mbox_off;
314 int ret;
315
316 nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
317
318 ret = __nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MBOX);
319 if (ret) {
320 nn_err(nn, "Mailbox update error\n");
321 return ret;
322 }
323
324 return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
325 }
326
nfp_net_mbox_reconfig_post(struct nfp_net * nn,u32 mbox_cmd)327 void nfp_net_mbox_reconfig_post(struct nfp_net *nn, u32 mbox_cmd)
328 {
329 u32 mbox = nn->tlv_caps.mbox_off;
330
331 nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
332
333 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_MBOX);
334 }
335
nfp_net_mbox_reconfig_wait_posted(struct nfp_net * nn)336 int nfp_net_mbox_reconfig_wait_posted(struct nfp_net *nn)
337 {
338 u32 mbox = nn->tlv_caps.mbox_off;
339
340 nfp_net_reconfig_wait_posted(nn);
341
342 return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
343 }
344
nfp_net_mbox_reconfig_and_unlock(struct nfp_net * nn,u32 mbox_cmd)345 int nfp_net_mbox_reconfig_and_unlock(struct nfp_net *nn, u32 mbox_cmd)
346 {
347 int ret;
348
349 ret = nfp_net_mbox_reconfig(nn, mbox_cmd);
350 nn_ctrl_bar_unlock(nn);
351 return ret;
352 }
353
354 /* Interrupt configuration and handling
355 */
356
357 /**
358 * nfp_net_irqs_alloc() - allocates MSI-X irqs
359 * @pdev: PCI device structure
360 * @irq_entries: Array to be initialized and used to hold the irq entries
361 * @min_irqs: Minimal acceptable number of interrupts
362 * @wanted_irqs: Target number of interrupts to allocate
363 *
364 * Return: Number of irqs obtained or 0 on error.
365 */
366 unsigned int
nfp_net_irqs_alloc(struct pci_dev * pdev,struct msix_entry * irq_entries,unsigned int min_irqs,unsigned int wanted_irqs)367 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
368 unsigned int min_irqs, unsigned int wanted_irqs)
369 {
370 unsigned int i;
371 int got_irqs;
372
373 for (i = 0; i < wanted_irqs; i++)
374 irq_entries[i].entry = i;
375
376 got_irqs = pci_enable_msix_range(pdev, irq_entries,
377 min_irqs, wanted_irqs);
378 if (got_irqs < 0) {
379 dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
380 min_irqs, wanted_irqs, got_irqs);
381 return 0;
382 }
383
384 if (got_irqs < wanted_irqs)
385 dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
386 wanted_irqs, got_irqs);
387
388 return got_irqs;
389 }
390
391 /**
392 * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
393 * @nn: NFP Network structure
394 * @irq_entries: Table of allocated interrupts
395 * @n: Size of @irq_entries (number of entries to grab)
396 *
397 * After interrupts are allocated with nfp_net_irqs_alloc() this function
398 * should be called to assign them to a specific netdev (port).
399 */
400 void
nfp_net_irqs_assign(struct nfp_net * nn,struct msix_entry * irq_entries,unsigned int n)401 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
402 unsigned int n)
403 {
404 struct nfp_net_dp *dp = &nn->dp;
405
406 nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
407 dp->num_r_vecs = nn->max_r_vecs;
408
409 memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
410
411 if (dp->num_rx_rings > dp->num_r_vecs ||
412 dp->num_tx_rings > dp->num_r_vecs)
413 dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
414 dp->num_rx_rings, dp->num_tx_rings,
415 dp->num_r_vecs);
416
417 dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
418 dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
419 dp->num_stack_tx_rings = dp->num_tx_rings;
420 }
421
422 /**
423 * nfp_net_irqs_disable() - Disable interrupts
424 * @pdev: PCI device structure
425 *
426 * Undoes what @nfp_net_irqs_alloc() does.
427 */
nfp_net_irqs_disable(struct pci_dev * pdev)428 void nfp_net_irqs_disable(struct pci_dev *pdev)
429 {
430 pci_disable_msix(pdev);
431 }
432
433 /**
434 * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
435 * @irq: Interrupt
436 * @data: Opaque data structure
437 *
438 * Return: Indicate if the interrupt has been handled.
439 */
nfp_net_irq_rxtx(int irq,void * data)440 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
441 {
442 struct nfp_net_r_vector *r_vec = data;
443
444 /* Currently we cannot tell if it's a rx or tx interrupt,
445 * since dim does not need accurate event_ctr to calculate,
446 * we just use this counter for both rx and tx dim.
447 */
448 r_vec->event_ctr++;
449
450 napi_schedule_irqoff(&r_vec->napi);
451
452 /* The FW auto-masks any interrupt, either via the MASK bit in
453 * the MSI-X table or via the per entry ICR field. So there
454 * is no need to disable interrupts here.
455 */
456 return IRQ_HANDLED;
457 }
458
nfp_ctrl_irq_rxtx(int irq,void * data)459 static irqreturn_t nfp_ctrl_irq_rxtx(int irq, void *data)
460 {
461 struct nfp_net_r_vector *r_vec = data;
462
463 tasklet_schedule(&r_vec->tasklet);
464
465 return IRQ_HANDLED;
466 }
467
468 /**
469 * nfp_net_read_link_status() - Reread link status from control BAR
470 * @nn: NFP Network structure
471 */
nfp_net_read_link_status(struct nfp_net * nn)472 static void nfp_net_read_link_status(struct nfp_net *nn)
473 {
474 unsigned long flags;
475 bool link_up;
476 u32 sts;
477
478 spin_lock_irqsave(&nn->link_status_lock, flags);
479
480 sts = nn_readl(nn, NFP_NET_CFG_STS);
481 link_up = !!(sts & NFP_NET_CFG_STS_LINK);
482
483 if (nn->link_up == link_up)
484 goto out;
485
486 nn->link_up = link_up;
487 if (nn->port)
488 set_bit(NFP_PORT_CHANGED, &nn->port->flags);
489
490 if (nn->link_up) {
491 netif_carrier_on(nn->dp.netdev);
492 netdev_info(nn->dp.netdev, "NIC Link is Up\n");
493 } else {
494 netif_carrier_off(nn->dp.netdev);
495 netdev_info(nn->dp.netdev, "NIC Link is Down\n");
496 }
497 out:
498 spin_unlock_irqrestore(&nn->link_status_lock, flags);
499 }
500
501 /**
502 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
503 * @irq: Interrupt
504 * @data: Opaque data structure
505 *
506 * Return: Indicate if the interrupt has been handled.
507 */
nfp_net_irq_lsc(int irq,void * data)508 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
509 {
510 struct nfp_net *nn = data;
511 struct msix_entry *entry;
512
513 entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
514
515 nfp_net_read_link_status(nn);
516
517 nfp_net_irq_unmask(nn, entry->entry);
518
519 return IRQ_HANDLED;
520 }
521
522 /**
523 * nfp_net_irq_exn() - Interrupt service routine for exceptions
524 * @irq: Interrupt
525 * @data: Opaque data structure
526 *
527 * Return: Indicate if the interrupt has been handled.
528 */
nfp_net_irq_exn(int irq,void * data)529 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
530 {
531 struct nfp_net *nn = data;
532
533 nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
534 /* XXX TO BE IMPLEMENTED */
535 return IRQ_HANDLED;
536 }
537
538 /**
539 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
540 * @nn: NFP Network structure
541 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
542 * @format: printf-style format to construct the interrupt name
543 * @name: Pointer to allocated space for interrupt name
544 * @name_sz: Size of space for interrupt name
545 * @vector_idx: Index of MSI-X vector used for this interrupt
546 * @handler: IRQ handler to register for this interrupt
547 */
548 static int
nfp_net_aux_irq_request(struct nfp_net * nn,u32 ctrl_offset,const char * format,char * name,size_t name_sz,unsigned int vector_idx,irq_handler_t handler)549 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
550 const char *format, char *name, size_t name_sz,
551 unsigned int vector_idx, irq_handler_t handler)
552 {
553 struct msix_entry *entry;
554 int err;
555
556 entry = &nn->irq_entries[vector_idx];
557
558 snprintf(name, name_sz, format, nfp_net_name(nn));
559 err = request_irq(entry->vector, handler, 0, name, nn);
560 if (err) {
561 nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
562 entry->vector, err);
563 return err;
564 }
565 nn_writeb(nn, ctrl_offset, entry->entry);
566 nfp_net_irq_unmask(nn, entry->entry);
567
568 return 0;
569 }
570
571 /**
572 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
573 * @nn: NFP Network structure
574 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
575 * @vector_idx: Index of MSI-X vector used for this interrupt
576 */
nfp_net_aux_irq_free(struct nfp_net * nn,u32 ctrl_offset,unsigned int vector_idx)577 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
578 unsigned int vector_idx)
579 {
580 nn_writeb(nn, ctrl_offset, 0xff);
581 nn_pci_flush(nn);
582 free_irq(nn->irq_entries[vector_idx].vector, nn);
583 }
584
585 struct sk_buff *
nfp_net_tls_tx(struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,struct sk_buff * skb,u64 * tls_handle,int * nr_frags)586 nfp_net_tls_tx(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
587 struct sk_buff *skb, u64 *tls_handle, int *nr_frags)
588 {
589 #ifdef CONFIG_TLS_DEVICE
590 struct nfp_net_tls_offload_ctx *ntls;
591 struct sk_buff *nskb;
592 bool resync_pending;
593 u32 datalen, seq;
594
595 if (likely(!dp->ktls_tx))
596 return skb;
597 if (!skb->sk || !tls_is_sk_tx_device_offloaded(skb->sk))
598 return skb;
599
600 datalen = skb->len - (skb_transport_offset(skb) + tcp_hdrlen(skb));
601 seq = ntohl(tcp_hdr(skb)->seq);
602 ntls = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
603 resync_pending = tls_offload_tx_resync_pending(skb->sk);
604 if (unlikely(resync_pending || ntls->next_seq != seq)) {
605 /* Pure ACK out of order already */
606 if (!datalen)
607 return skb;
608
609 u64_stats_update_begin(&r_vec->tx_sync);
610 r_vec->tls_tx_fallback++;
611 u64_stats_update_end(&r_vec->tx_sync);
612
613 nskb = tls_encrypt_skb(skb);
614 if (!nskb) {
615 u64_stats_update_begin(&r_vec->tx_sync);
616 r_vec->tls_tx_no_fallback++;
617 u64_stats_update_end(&r_vec->tx_sync);
618 return NULL;
619 }
620 /* encryption wasn't necessary */
621 if (nskb == skb)
622 return skb;
623 /* we don't re-check ring space */
624 if (unlikely(skb_is_nonlinear(nskb))) {
625 nn_dp_warn(dp, "tls_encrypt_skb() produced fragmented frame\n");
626 u64_stats_update_begin(&r_vec->tx_sync);
627 r_vec->tx_errors++;
628 u64_stats_update_end(&r_vec->tx_sync);
629 dev_kfree_skb_any(nskb);
630 return NULL;
631 }
632
633 /* jump forward, a TX may have gotten lost, need to sync TX */
634 if (!resync_pending && seq - ntls->next_seq < U32_MAX / 4)
635 tls_offload_tx_resync_request(nskb->sk, seq,
636 ntls->next_seq);
637
638 *nr_frags = 0;
639 return nskb;
640 }
641
642 if (datalen) {
643 u64_stats_update_begin(&r_vec->tx_sync);
644 if (!skb_is_gso(skb))
645 r_vec->hw_tls_tx++;
646 else
647 r_vec->hw_tls_tx += skb_shinfo(skb)->gso_segs;
648 u64_stats_update_end(&r_vec->tx_sync);
649 }
650
651 memcpy(tls_handle, ntls->fw_handle, sizeof(ntls->fw_handle));
652 ntls->next_seq += datalen;
653 #endif
654 return skb;
655 }
656
nfp_net_tls_tx_undo(struct sk_buff * skb,u64 tls_handle)657 void nfp_net_tls_tx_undo(struct sk_buff *skb, u64 tls_handle)
658 {
659 #ifdef CONFIG_TLS_DEVICE
660 struct nfp_net_tls_offload_ctx *ntls;
661 u32 datalen, seq;
662
663 if (!tls_handle)
664 return;
665 if (WARN_ON_ONCE(!skb->sk || !tls_is_sk_tx_device_offloaded(skb->sk)))
666 return;
667
668 datalen = skb->len - (skb_transport_offset(skb) + tcp_hdrlen(skb));
669 seq = ntohl(tcp_hdr(skb)->seq);
670
671 ntls = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
672 if (ntls->next_seq == seq + datalen)
673 ntls->next_seq = seq;
674 else
675 WARN_ON_ONCE(1);
676 #endif
677 }
678
nfp_net_tx_timeout(struct net_device * netdev,unsigned int txqueue)679 static void nfp_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
680 {
681 struct nfp_net *nn = netdev_priv(netdev);
682
683 nn_warn(nn, "TX watchdog timeout on ring: %u\n", txqueue);
684 }
685
686 /* Receive processing */
687 static unsigned int
nfp_net_calc_fl_bufsz_data(struct nfp_net_dp * dp)688 nfp_net_calc_fl_bufsz_data(struct nfp_net_dp *dp)
689 {
690 unsigned int fl_bufsz = 0;
691
692 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
693 fl_bufsz += NFP_NET_MAX_PREPEND;
694 else
695 fl_bufsz += dp->rx_offset;
696 fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
697
698 return fl_bufsz;
699 }
700
nfp_net_calc_fl_bufsz(struct nfp_net_dp * dp)701 static unsigned int nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
702 {
703 unsigned int fl_bufsz;
704
705 fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
706 fl_bufsz += dp->rx_dma_off;
707 fl_bufsz += nfp_net_calc_fl_bufsz_data(dp);
708
709 fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
710 fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
711
712 return fl_bufsz;
713 }
714
nfp_net_calc_fl_bufsz_xsk(struct nfp_net_dp * dp)715 static unsigned int nfp_net_calc_fl_bufsz_xsk(struct nfp_net_dp *dp)
716 {
717 unsigned int fl_bufsz;
718
719 fl_bufsz = XDP_PACKET_HEADROOM;
720 fl_bufsz += nfp_net_calc_fl_bufsz_data(dp);
721
722 return fl_bufsz;
723 }
724
725 /* Setup and Configuration
726 */
727
728 /**
729 * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
730 * @nn: NFP Network structure
731 */
nfp_net_vecs_init(struct nfp_net * nn)732 static void nfp_net_vecs_init(struct nfp_net *nn)
733 {
734 struct nfp_net_r_vector *r_vec;
735 int r;
736
737 nn->lsc_handler = nfp_net_irq_lsc;
738 nn->exn_handler = nfp_net_irq_exn;
739
740 for (r = 0; r < nn->max_r_vecs; r++) {
741 struct msix_entry *entry;
742
743 entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
744
745 r_vec = &nn->r_vecs[r];
746 r_vec->nfp_net = nn;
747 r_vec->irq_entry = entry->entry;
748 r_vec->irq_vector = entry->vector;
749
750 if (nn->dp.netdev) {
751 r_vec->handler = nfp_net_irq_rxtx;
752 } else {
753 r_vec->handler = nfp_ctrl_irq_rxtx;
754
755 __skb_queue_head_init(&r_vec->queue);
756 spin_lock_init(&r_vec->lock);
757 tasklet_setup(&r_vec->tasklet, nn->dp.ops->ctrl_poll);
758 tasklet_disable(&r_vec->tasklet);
759 }
760
761 cpumask_set_cpu(r, &r_vec->affinity_mask);
762 }
763 }
764
765 static void
nfp_net_napi_add(struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,int idx)766 nfp_net_napi_add(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec, int idx)
767 {
768 if (dp->netdev)
769 netif_napi_add(dp->netdev, &r_vec->napi,
770 nfp_net_has_xsk_pool_slow(dp, idx) ?
771 dp->ops->xsk_poll : dp->ops->poll,
772 NAPI_POLL_WEIGHT);
773 else
774 tasklet_enable(&r_vec->tasklet);
775 }
776
777 static void
nfp_net_napi_del(struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec)778 nfp_net_napi_del(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec)
779 {
780 if (dp->netdev)
781 netif_napi_del(&r_vec->napi);
782 else
783 tasklet_disable(&r_vec->tasklet);
784 }
785
786 static void
nfp_net_vector_assign_rings(struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,int idx)787 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
788 struct nfp_net_r_vector *r_vec, int idx)
789 {
790 r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
791 r_vec->tx_ring =
792 idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
793
794 r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
795 &dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
796
797 if (nfp_net_has_xsk_pool_slow(dp, idx) || r_vec->xsk_pool) {
798 r_vec->xsk_pool = dp->xdp_prog ? dp->xsk_pools[idx] : NULL;
799
800 if (r_vec->xsk_pool)
801 xsk_pool_set_rxq_info(r_vec->xsk_pool,
802 &r_vec->rx_ring->xdp_rxq);
803
804 nfp_net_napi_del(dp, r_vec);
805 nfp_net_napi_add(dp, r_vec, idx);
806 }
807 }
808
809 static int
nfp_net_prepare_vector(struct nfp_net * nn,struct nfp_net_r_vector * r_vec,int idx)810 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
811 int idx)
812 {
813 int err;
814
815 nfp_net_napi_add(&nn->dp, r_vec, idx);
816
817 snprintf(r_vec->name, sizeof(r_vec->name),
818 "%s-rxtx-%d", nfp_net_name(nn), idx);
819 err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
820 r_vec);
821 if (err) {
822 nfp_net_napi_del(&nn->dp, r_vec);
823 nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
824 return err;
825 }
826 disable_irq(r_vec->irq_vector);
827
828 irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
829
830 nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
831 r_vec->irq_entry);
832
833 return 0;
834 }
835
836 static void
nfp_net_cleanup_vector(struct nfp_net * nn,struct nfp_net_r_vector * r_vec)837 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
838 {
839 irq_set_affinity_hint(r_vec->irq_vector, NULL);
840 nfp_net_napi_del(&nn->dp, r_vec);
841 free_irq(r_vec->irq_vector, r_vec);
842 }
843
844 /**
845 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
846 * @nn: NFP Net device to reconfigure
847 */
nfp_net_rss_write_itbl(struct nfp_net * nn)848 void nfp_net_rss_write_itbl(struct nfp_net *nn)
849 {
850 int i;
851
852 for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
853 nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
854 get_unaligned_le32(nn->rss_itbl + i));
855 }
856
857 /**
858 * nfp_net_rss_write_key() - Write RSS hash key to device
859 * @nn: NFP Net device to reconfigure
860 */
nfp_net_rss_write_key(struct nfp_net * nn)861 void nfp_net_rss_write_key(struct nfp_net *nn)
862 {
863 int i;
864
865 for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
866 nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
867 get_unaligned_le32(nn->rss_key + i));
868 }
869
870 /**
871 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
872 * @nn: NFP Net device to reconfigure
873 */
nfp_net_coalesce_write_cfg(struct nfp_net * nn)874 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
875 {
876 u8 i;
877 u32 factor;
878 u32 value;
879
880 /* Compute factor used to convert coalesce '_usecs' parameters to
881 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
882 * count.
883 */
884 factor = nn->tlv_caps.me_freq_mhz / 16;
885
886 /* copy RX interrupt coalesce parameters */
887 value = (nn->rx_coalesce_max_frames << 16) |
888 (factor * nn->rx_coalesce_usecs);
889 for (i = 0; i < nn->dp.num_rx_rings; i++)
890 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
891
892 /* copy TX interrupt coalesce parameters */
893 value = (nn->tx_coalesce_max_frames << 16) |
894 (factor * nn->tx_coalesce_usecs);
895 for (i = 0; i < nn->dp.num_tx_rings; i++)
896 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
897 }
898
899 /**
900 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
901 * @nn: NFP Net device to reconfigure
902 * @addr: MAC address to write
903 *
904 * Writes the MAC address from the netdev to the device control BAR. Does not
905 * perform the required reconfig. We do a bit of byte swapping dance because
906 * firmware is LE.
907 */
nfp_net_write_mac_addr(struct nfp_net * nn,const u8 * addr)908 static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
909 {
910 nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
911 nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
912 }
913
914 /**
915 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
916 * @nn: NFP Net device to reconfigure
917 *
918 * Warning: must be fully idempotent.
919 */
nfp_net_clear_config_and_disable(struct nfp_net * nn)920 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
921 {
922 u32 new_ctrl, update;
923 unsigned int r;
924 int err;
925
926 new_ctrl = nn->dp.ctrl;
927 new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
928 update = NFP_NET_CFG_UPDATE_GEN;
929 update |= NFP_NET_CFG_UPDATE_MSIX;
930 update |= NFP_NET_CFG_UPDATE_RING;
931
932 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
933 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
934
935 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
936 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
937
938 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
939 err = nfp_net_reconfig(nn, update);
940 if (err)
941 nn_err(nn, "Could not disable device: %d\n", err);
942
943 for (r = 0; r < nn->dp.num_rx_rings; r++) {
944 nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
945 if (nfp_net_has_xsk_pool_slow(&nn->dp, nn->dp.rx_rings[r].idx))
946 nfp_net_xsk_rx_bufs_free(&nn->dp.rx_rings[r]);
947 }
948 for (r = 0; r < nn->dp.num_tx_rings; r++)
949 nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
950 for (r = 0; r < nn->dp.num_r_vecs; r++)
951 nfp_net_vec_clear_ring_data(nn, r);
952
953 nn->dp.ctrl = new_ctrl;
954 }
955
956 /**
957 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
958 * @nn: NFP Net device to reconfigure
959 */
nfp_net_set_config_and_enable(struct nfp_net * nn)960 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
961 {
962 u32 bufsz, new_ctrl, update = 0;
963 unsigned int r;
964 int err;
965
966 new_ctrl = nn->dp.ctrl;
967
968 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
969 nfp_net_rss_write_key(nn);
970 nfp_net_rss_write_itbl(nn);
971 nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
972 update |= NFP_NET_CFG_UPDATE_RSS;
973 }
974
975 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
976 nfp_net_coalesce_write_cfg(nn);
977 update |= NFP_NET_CFG_UPDATE_IRQMOD;
978 }
979
980 for (r = 0; r < nn->dp.num_tx_rings; r++)
981 nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
982 for (r = 0; r < nn->dp.num_rx_rings; r++)
983 nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
984
985 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE,
986 U64_MAX >> (64 - nn->dp.num_tx_rings));
987
988 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE,
989 U64_MAX >> (64 - nn->dp.num_rx_rings));
990
991 if (nn->dp.netdev)
992 nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
993
994 nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
995
996 bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
997 nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
998
999 /* Enable device */
1000 new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
1001 update |= NFP_NET_CFG_UPDATE_GEN;
1002 update |= NFP_NET_CFG_UPDATE_MSIX;
1003 update |= NFP_NET_CFG_UPDATE_RING;
1004 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
1005 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
1006
1007 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1008 err = nfp_net_reconfig(nn, update);
1009 if (err) {
1010 nfp_net_clear_config_and_disable(nn);
1011 return err;
1012 }
1013
1014 nn->dp.ctrl = new_ctrl;
1015
1016 for (r = 0; r < nn->dp.num_rx_rings; r++)
1017 nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
1018
1019 return 0;
1020 }
1021
1022 /**
1023 * nfp_net_close_stack() - Quiesce the stack (part of close)
1024 * @nn: NFP Net device to reconfigure
1025 */
nfp_net_close_stack(struct nfp_net * nn)1026 static void nfp_net_close_stack(struct nfp_net *nn)
1027 {
1028 struct nfp_net_r_vector *r_vec;
1029 unsigned int r;
1030
1031 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1032 netif_carrier_off(nn->dp.netdev);
1033 nn->link_up = false;
1034
1035 for (r = 0; r < nn->dp.num_r_vecs; r++) {
1036 r_vec = &nn->r_vecs[r];
1037
1038 disable_irq(r_vec->irq_vector);
1039 napi_disable(&r_vec->napi);
1040
1041 if (r_vec->rx_ring)
1042 cancel_work_sync(&r_vec->rx_dim.work);
1043
1044 if (r_vec->tx_ring)
1045 cancel_work_sync(&r_vec->tx_dim.work);
1046 }
1047
1048 netif_tx_disable(nn->dp.netdev);
1049 }
1050
1051 /**
1052 * nfp_net_close_free_all() - Free all runtime resources
1053 * @nn: NFP Net device to reconfigure
1054 */
nfp_net_close_free_all(struct nfp_net * nn)1055 static void nfp_net_close_free_all(struct nfp_net *nn)
1056 {
1057 unsigned int r;
1058
1059 nfp_net_tx_rings_free(&nn->dp);
1060 nfp_net_rx_rings_free(&nn->dp);
1061
1062 for (r = 0; r < nn->dp.num_r_vecs; r++)
1063 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1064
1065 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
1066 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
1067 }
1068
1069 /**
1070 * nfp_net_netdev_close() - Called when the device is downed
1071 * @netdev: netdev structure
1072 */
nfp_net_netdev_close(struct net_device * netdev)1073 static int nfp_net_netdev_close(struct net_device *netdev)
1074 {
1075 struct nfp_net *nn = netdev_priv(netdev);
1076
1077 /* Step 1: Disable RX and TX rings from the Linux kernel perspective
1078 */
1079 nfp_net_close_stack(nn);
1080
1081 /* Step 2: Tell NFP
1082 */
1083 nfp_net_clear_config_and_disable(nn);
1084 nfp_port_configure(netdev, false);
1085
1086 /* Step 3: Free resources
1087 */
1088 nfp_net_close_free_all(nn);
1089
1090 nn_dbg(nn, "%s down", netdev->name);
1091 return 0;
1092 }
1093
nfp_ctrl_close(struct nfp_net * nn)1094 void nfp_ctrl_close(struct nfp_net *nn)
1095 {
1096 int r;
1097
1098 rtnl_lock();
1099
1100 for (r = 0; r < nn->dp.num_r_vecs; r++) {
1101 disable_irq(nn->r_vecs[r].irq_vector);
1102 tasklet_disable(&nn->r_vecs[r].tasklet);
1103 }
1104
1105 nfp_net_clear_config_and_disable(nn);
1106
1107 nfp_net_close_free_all(nn);
1108
1109 rtnl_unlock();
1110 }
1111
nfp_net_rx_dim_work(struct work_struct * work)1112 static void nfp_net_rx_dim_work(struct work_struct *work)
1113 {
1114 struct nfp_net_r_vector *r_vec;
1115 unsigned int factor, value;
1116 struct dim_cq_moder moder;
1117 struct nfp_net *nn;
1118 struct dim *dim;
1119
1120 dim = container_of(work, struct dim, work);
1121 moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
1122 r_vec = container_of(dim, struct nfp_net_r_vector, rx_dim);
1123 nn = r_vec->nfp_net;
1124
1125 /* Compute factor used to convert coalesce '_usecs' parameters to
1126 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
1127 * count.
1128 */
1129 factor = nn->tlv_caps.me_freq_mhz / 16;
1130 if (nfp_net_coalesce_para_check(factor * moder.usec, moder.pkts))
1131 return;
1132
1133 /* copy RX interrupt coalesce parameters */
1134 value = (moder.pkts << 16) | (factor * moder.usec);
1135 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(r_vec->rx_ring->idx), value);
1136 (void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1137
1138 dim->state = DIM_START_MEASURE;
1139 }
1140
nfp_net_tx_dim_work(struct work_struct * work)1141 static void nfp_net_tx_dim_work(struct work_struct *work)
1142 {
1143 struct nfp_net_r_vector *r_vec;
1144 unsigned int factor, value;
1145 struct dim_cq_moder moder;
1146 struct nfp_net *nn;
1147 struct dim *dim;
1148
1149 dim = container_of(work, struct dim, work);
1150 moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
1151 r_vec = container_of(dim, struct nfp_net_r_vector, tx_dim);
1152 nn = r_vec->nfp_net;
1153
1154 /* Compute factor used to convert coalesce '_usecs' parameters to
1155 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
1156 * count.
1157 */
1158 factor = nn->tlv_caps.me_freq_mhz / 16;
1159 if (nfp_net_coalesce_para_check(factor * moder.usec, moder.pkts))
1160 return;
1161
1162 /* copy TX interrupt coalesce parameters */
1163 value = (moder.pkts << 16) | (factor * moder.usec);
1164 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(r_vec->tx_ring->idx), value);
1165 (void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1166
1167 dim->state = DIM_START_MEASURE;
1168 }
1169
1170 /**
1171 * nfp_net_open_stack() - Start the device from stack's perspective
1172 * @nn: NFP Net device to reconfigure
1173 */
nfp_net_open_stack(struct nfp_net * nn)1174 static void nfp_net_open_stack(struct nfp_net *nn)
1175 {
1176 struct nfp_net_r_vector *r_vec;
1177 unsigned int r;
1178
1179 for (r = 0; r < nn->dp.num_r_vecs; r++) {
1180 r_vec = &nn->r_vecs[r];
1181
1182 if (r_vec->rx_ring) {
1183 INIT_WORK(&r_vec->rx_dim.work, nfp_net_rx_dim_work);
1184 r_vec->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1185 }
1186
1187 if (r_vec->tx_ring) {
1188 INIT_WORK(&r_vec->tx_dim.work, nfp_net_tx_dim_work);
1189 r_vec->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1190 }
1191
1192 napi_enable(&r_vec->napi);
1193 enable_irq(r_vec->irq_vector);
1194 }
1195
1196 netif_tx_wake_all_queues(nn->dp.netdev);
1197
1198 enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1199 nfp_net_read_link_status(nn);
1200 }
1201
nfp_net_open_alloc_all(struct nfp_net * nn)1202 static int nfp_net_open_alloc_all(struct nfp_net *nn)
1203 {
1204 int err, r;
1205
1206 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
1207 nn->exn_name, sizeof(nn->exn_name),
1208 NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
1209 if (err)
1210 return err;
1211 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
1212 nn->lsc_name, sizeof(nn->lsc_name),
1213 NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
1214 if (err)
1215 goto err_free_exn;
1216 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1217
1218 for (r = 0; r < nn->dp.num_r_vecs; r++) {
1219 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1220 if (err)
1221 goto err_cleanup_vec_p;
1222 }
1223
1224 err = nfp_net_rx_rings_prepare(nn, &nn->dp);
1225 if (err)
1226 goto err_cleanup_vec;
1227
1228 err = nfp_net_tx_rings_prepare(nn, &nn->dp);
1229 if (err)
1230 goto err_free_rx_rings;
1231
1232 for (r = 0; r < nn->max_r_vecs; r++)
1233 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1234
1235 return 0;
1236
1237 err_free_rx_rings:
1238 nfp_net_rx_rings_free(&nn->dp);
1239 err_cleanup_vec:
1240 r = nn->dp.num_r_vecs;
1241 err_cleanup_vec_p:
1242 while (r--)
1243 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1244 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
1245 err_free_exn:
1246 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
1247 return err;
1248 }
1249
nfp_net_netdev_open(struct net_device * netdev)1250 static int nfp_net_netdev_open(struct net_device *netdev)
1251 {
1252 struct nfp_net *nn = netdev_priv(netdev);
1253 int err;
1254
1255 /* Step 1: Allocate resources for rings and the like
1256 * - Request interrupts
1257 * - Allocate RX and TX ring resources
1258 * - Setup initial RSS table
1259 */
1260 err = nfp_net_open_alloc_all(nn);
1261 if (err)
1262 return err;
1263
1264 err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
1265 if (err)
1266 goto err_free_all;
1267
1268 err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
1269 if (err)
1270 goto err_free_all;
1271
1272 /* Step 2: Configure the NFP
1273 * - Ifup the physical interface if it exists
1274 * - Enable rings from 0 to tx_rings/rx_rings - 1.
1275 * - Write MAC address (in case it changed)
1276 * - Set the MTU
1277 * - Set the Freelist buffer size
1278 * - Enable the FW
1279 */
1280 err = nfp_port_configure(netdev, true);
1281 if (err)
1282 goto err_free_all;
1283
1284 err = nfp_net_set_config_and_enable(nn);
1285 if (err)
1286 goto err_port_disable;
1287
1288 /* Step 3: Enable for kernel
1289 * - put some freelist descriptors on each RX ring
1290 * - enable NAPI on each ring
1291 * - enable all TX queues
1292 * - set link state
1293 */
1294 nfp_net_open_stack(nn);
1295
1296 return 0;
1297
1298 err_port_disable:
1299 nfp_port_configure(netdev, false);
1300 err_free_all:
1301 nfp_net_close_free_all(nn);
1302 return err;
1303 }
1304
nfp_ctrl_open(struct nfp_net * nn)1305 int nfp_ctrl_open(struct nfp_net *nn)
1306 {
1307 int err, r;
1308
1309 /* ring dumping depends on vNICs being opened/closed under rtnl */
1310 rtnl_lock();
1311
1312 err = nfp_net_open_alloc_all(nn);
1313 if (err)
1314 goto err_unlock;
1315
1316 err = nfp_net_set_config_and_enable(nn);
1317 if (err)
1318 goto err_free_all;
1319
1320 for (r = 0; r < nn->dp.num_r_vecs; r++)
1321 enable_irq(nn->r_vecs[r].irq_vector);
1322
1323 rtnl_unlock();
1324
1325 return 0;
1326
1327 err_free_all:
1328 nfp_net_close_free_all(nn);
1329 err_unlock:
1330 rtnl_unlock();
1331 return err;
1332 }
1333
nfp_net_set_rx_mode(struct net_device * netdev)1334 static void nfp_net_set_rx_mode(struct net_device *netdev)
1335 {
1336 struct nfp_net *nn = netdev_priv(netdev);
1337 u32 new_ctrl;
1338
1339 new_ctrl = nn->dp.ctrl;
1340
1341 if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
1342 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
1343 else
1344 new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
1345
1346 if (netdev->flags & IFF_PROMISC) {
1347 if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
1348 new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
1349 else
1350 nn_warn(nn, "FW does not support promiscuous mode\n");
1351 } else {
1352 new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
1353 }
1354
1355 if (new_ctrl == nn->dp.ctrl)
1356 return;
1357
1358 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1359 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
1360
1361 nn->dp.ctrl = new_ctrl;
1362 }
1363
nfp_net_rss_init_itbl(struct nfp_net * nn)1364 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
1365 {
1366 int i;
1367
1368 for (i = 0; i < sizeof(nn->rss_itbl); i++)
1369 nn->rss_itbl[i] =
1370 ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
1371 }
1372
nfp_net_dp_swap(struct nfp_net * nn,struct nfp_net_dp * dp)1373 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
1374 {
1375 struct nfp_net_dp new_dp = *dp;
1376
1377 *dp = nn->dp;
1378 nn->dp = new_dp;
1379
1380 nn->dp.netdev->mtu = new_dp.mtu;
1381
1382 if (!netif_is_rxfh_configured(nn->dp.netdev))
1383 nfp_net_rss_init_itbl(nn);
1384 }
1385
nfp_net_dp_swap_enable(struct nfp_net * nn,struct nfp_net_dp * dp)1386 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
1387 {
1388 unsigned int r;
1389 int err;
1390
1391 nfp_net_dp_swap(nn, dp);
1392
1393 for (r = 0; r < nn->max_r_vecs; r++)
1394 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1395
1396 err = netif_set_real_num_queues(nn->dp.netdev,
1397 nn->dp.num_stack_tx_rings,
1398 nn->dp.num_rx_rings);
1399 if (err)
1400 return err;
1401
1402 return nfp_net_set_config_and_enable(nn);
1403 }
1404
nfp_net_clone_dp(struct nfp_net * nn)1405 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
1406 {
1407 struct nfp_net_dp *new;
1408
1409 new = kmalloc(sizeof(*new), GFP_KERNEL);
1410 if (!new)
1411 return NULL;
1412
1413 *new = nn->dp;
1414
1415 new->xsk_pools = kmemdup(new->xsk_pools,
1416 array_size(nn->max_r_vecs,
1417 sizeof(new->xsk_pools)),
1418 GFP_KERNEL);
1419 if (!new->xsk_pools) {
1420 kfree(new);
1421 return NULL;
1422 }
1423
1424 /* Clear things which need to be recomputed */
1425 new->fl_bufsz = 0;
1426 new->tx_rings = NULL;
1427 new->rx_rings = NULL;
1428 new->num_r_vecs = 0;
1429 new->num_stack_tx_rings = 0;
1430 new->txrwb = NULL;
1431 new->txrwb_dma = 0;
1432
1433 return new;
1434 }
1435
nfp_net_free_dp(struct nfp_net_dp * dp)1436 static void nfp_net_free_dp(struct nfp_net_dp *dp)
1437 {
1438 kfree(dp->xsk_pools);
1439 kfree(dp);
1440 }
1441
1442 static int
nfp_net_check_config(struct nfp_net * nn,struct nfp_net_dp * dp,struct netlink_ext_ack * extack)1443 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
1444 struct netlink_ext_ack *extack)
1445 {
1446 unsigned int r, xsk_min_fl_bufsz;
1447
1448 /* XDP-enabled tests */
1449 if (!dp->xdp_prog)
1450 return 0;
1451 if (dp->fl_bufsz > PAGE_SIZE) {
1452 NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
1453 return -EINVAL;
1454 }
1455 if (dp->num_tx_rings > nn->max_tx_rings) {
1456 NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
1457 return -EINVAL;
1458 }
1459
1460 xsk_min_fl_bufsz = nfp_net_calc_fl_bufsz_xsk(dp);
1461 for (r = 0; r < nn->max_r_vecs; r++) {
1462 if (!dp->xsk_pools[r])
1463 continue;
1464
1465 if (xsk_pool_get_rx_frame_size(dp->xsk_pools[r]) < xsk_min_fl_bufsz) {
1466 NL_SET_ERR_MSG_MOD(extack,
1467 "XSK buffer pool chunk size too small");
1468 return -EINVAL;
1469 }
1470 }
1471
1472 return 0;
1473 }
1474
nfp_net_ring_reconfig(struct nfp_net * nn,struct nfp_net_dp * dp,struct netlink_ext_ack * extack)1475 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
1476 struct netlink_ext_ack *extack)
1477 {
1478 int r, err;
1479
1480 dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
1481
1482 dp->num_stack_tx_rings = dp->num_tx_rings;
1483 if (dp->xdp_prog)
1484 dp->num_stack_tx_rings -= dp->num_rx_rings;
1485
1486 dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
1487
1488 err = nfp_net_check_config(nn, dp, extack);
1489 if (err)
1490 goto exit_free_dp;
1491
1492 if (!netif_running(dp->netdev)) {
1493 nfp_net_dp_swap(nn, dp);
1494 err = 0;
1495 goto exit_free_dp;
1496 }
1497
1498 /* Prepare new rings */
1499 for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
1500 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1501 if (err) {
1502 dp->num_r_vecs = r;
1503 goto err_cleanup_vecs;
1504 }
1505 }
1506
1507 err = nfp_net_rx_rings_prepare(nn, dp);
1508 if (err)
1509 goto err_cleanup_vecs;
1510
1511 err = nfp_net_tx_rings_prepare(nn, dp);
1512 if (err)
1513 goto err_free_rx;
1514
1515 /* Stop device, swap in new rings, try to start the firmware */
1516 nfp_net_close_stack(nn);
1517 nfp_net_clear_config_and_disable(nn);
1518
1519 err = nfp_net_dp_swap_enable(nn, dp);
1520 if (err) {
1521 int err2;
1522
1523 nfp_net_clear_config_and_disable(nn);
1524
1525 /* Try with old configuration and old rings */
1526 err2 = nfp_net_dp_swap_enable(nn, dp);
1527 if (err2)
1528 nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
1529 err, err2);
1530 }
1531 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1532 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1533
1534 nfp_net_rx_rings_free(dp);
1535 nfp_net_tx_rings_free(dp);
1536
1537 nfp_net_open_stack(nn);
1538 exit_free_dp:
1539 nfp_net_free_dp(dp);
1540
1541 return err;
1542
1543 err_free_rx:
1544 nfp_net_rx_rings_free(dp);
1545 err_cleanup_vecs:
1546 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1547 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1548 nfp_net_free_dp(dp);
1549 return err;
1550 }
1551
nfp_net_change_mtu(struct net_device * netdev,int new_mtu)1552 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
1553 {
1554 struct nfp_net *nn = netdev_priv(netdev);
1555 struct nfp_net_dp *dp;
1556 int err;
1557
1558 err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
1559 if (err)
1560 return err;
1561
1562 dp = nfp_net_clone_dp(nn);
1563 if (!dp)
1564 return -ENOMEM;
1565
1566 dp->mtu = new_mtu;
1567
1568 return nfp_net_ring_reconfig(nn, dp, NULL);
1569 }
1570
1571 static int
nfp_net_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)1572 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
1573 {
1574 const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD;
1575 struct nfp_net *nn = netdev_priv(netdev);
1576 int err;
1577
1578 /* Priority tagged packets with vlan id 0 are processed by the
1579 * NFP as untagged packets
1580 */
1581 if (!vid)
1582 return 0;
1583
1584 err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1585 if (err)
1586 return err;
1587
1588 nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1589 nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1590 ETH_P_8021Q);
1591
1592 return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1593 }
1594
1595 static int
nfp_net_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)1596 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
1597 {
1598 const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL;
1599 struct nfp_net *nn = netdev_priv(netdev);
1600 int err;
1601
1602 /* Priority tagged packets with vlan id 0 are processed by the
1603 * NFP as untagged packets
1604 */
1605 if (!vid)
1606 return 0;
1607
1608 err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1609 if (err)
1610 return err;
1611
1612 nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1613 nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1614 ETH_P_8021Q);
1615
1616 return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1617 }
1618
nfp_net_stat64(struct net_device * netdev,struct rtnl_link_stats64 * stats)1619 static void nfp_net_stat64(struct net_device *netdev,
1620 struct rtnl_link_stats64 *stats)
1621 {
1622 struct nfp_net *nn = netdev_priv(netdev);
1623 int r;
1624
1625 /* Collect software stats */
1626 for (r = 0; r < nn->max_r_vecs; r++) {
1627 struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
1628 u64 data[3];
1629 unsigned int start;
1630
1631 do {
1632 start = u64_stats_fetch_begin_irq(&r_vec->rx_sync);
1633 data[0] = r_vec->rx_pkts;
1634 data[1] = r_vec->rx_bytes;
1635 data[2] = r_vec->rx_drops;
1636 } while (u64_stats_fetch_retry_irq(&r_vec->rx_sync, start));
1637 stats->rx_packets += data[0];
1638 stats->rx_bytes += data[1];
1639 stats->rx_dropped += data[2];
1640
1641 do {
1642 start = u64_stats_fetch_begin_irq(&r_vec->tx_sync);
1643 data[0] = r_vec->tx_pkts;
1644 data[1] = r_vec->tx_bytes;
1645 data[2] = r_vec->tx_errors;
1646 } while (u64_stats_fetch_retry_irq(&r_vec->tx_sync, start));
1647 stats->tx_packets += data[0];
1648 stats->tx_bytes += data[1];
1649 stats->tx_errors += data[2];
1650 }
1651
1652 /* Add in device stats */
1653 stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
1654 stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
1655 stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
1656
1657 stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
1658 stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
1659 }
1660
nfp_net_set_features(struct net_device * netdev,netdev_features_t features)1661 static int nfp_net_set_features(struct net_device *netdev,
1662 netdev_features_t features)
1663 {
1664 netdev_features_t changed = netdev->features ^ features;
1665 struct nfp_net *nn = netdev_priv(netdev);
1666 u32 new_ctrl;
1667 int err;
1668
1669 /* Assume this is not called with features we have not advertised */
1670
1671 new_ctrl = nn->dp.ctrl;
1672
1673 if (changed & NETIF_F_RXCSUM) {
1674 if (features & NETIF_F_RXCSUM)
1675 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
1676 else
1677 new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
1678 }
1679
1680 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1681 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
1682 new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
1683 else
1684 new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
1685 }
1686
1687 if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
1688 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
1689 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
1690 NFP_NET_CFG_CTRL_LSO;
1691 else
1692 new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
1693 }
1694
1695 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1696 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1697 new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
1698 else
1699 new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
1700 }
1701
1702 if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
1703 if (features & NETIF_F_HW_VLAN_CTAG_TX)
1704 new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
1705 else
1706 new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
1707 }
1708
1709 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
1710 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1711 new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
1712 else
1713 new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
1714 }
1715
1716 if (changed & NETIF_F_SG) {
1717 if (features & NETIF_F_SG)
1718 new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
1719 else
1720 new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
1721 }
1722
1723 err = nfp_port_set_features(netdev, features);
1724 if (err)
1725 return err;
1726
1727 nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
1728 netdev->features, features, changed);
1729
1730 if (new_ctrl == nn->dp.ctrl)
1731 return 0;
1732
1733 nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
1734 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1735 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
1736 if (err)
1737 return err;
1738
1739 nn->dp.ctrl = new_ctrl;
1740
1741 return 0;
1742 }
1743
1744 static netdev_features_t
nfp_net_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)1745 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
1746 netdev_features_t features)
1747 {
1748 u8 l4_hdr;
1749
1750 /* We can't do TSO over double tagged packets (802.1AD) */
1751 features &= vlan_features_check(skb, features);
1752
1753 if (!skb->encapsulation)
1754 return features;
1755
1756 /* Ensure that inner L4 header offset fits into TX descriptor field */
1757 if (skb_is_gso(skb)) {
1758 u32 hdrlen;
1759
1760 hdrlen = skb_inner_transport_header(skb) - skb->data +
1761 inner_tcp_hdrlen(skb);
1762
1763 /* Assume worst case scenario of having longest possible
1764 * metadata prepend - 8B
1765 */
1766 if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ - 8))
1767 features &= ~NETIF_F_GSO_MASK;
1768 }
1769
1770 /* VXLAN/GRE check */
1771 switch (vlan_get_protocol(skb)) {
1772 case htons(ETH_P_IP):
1773 l4_hdr = ip_hdr(skb)->protocol;
1774 break;
1775 case htons(ETH_P_IPV6):
1776 l4_hdr = ipv6_hdr(skb)->nexthdr;
1777 break;
1778 default:
1779 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1780 }
1781
1782 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
1783 skb->inner_protocol != htons(ETH_P_TEB) ||
1784 (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
1785 (l4_hdr == IPPROTO_UDP &&
1786 (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
1787 sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
1788 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1789
1790 return features;
1791 }
1792
1793 static int
nfp_net_get_phys_port_name(struct net_device * netdev,char * name,size_t len)1794 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
1795 {
1796 struct nfp_net *nn = netdev_priv(netdev);
1797 int n;
1798
1799 /* If port is defined, devlink_port is registered and devlink core
1800 * is taking care of name formatting.
1801 */
1802 if (nn->port)
1803 return -EOPNOTSUPP;
1804
1805 if (nn->dp.is_vf || nn->vnic_no_name)
1806 return -EOPNOTSUPP;
1807
1808 n = snprintf(name, len, "n%d", nn->id);
1809 if (n >= len)
1810 return -EINVAL;
1811
1812 return 0;
1813 }
1814
nfp_net_xdp_setup_drv(struct nfp_net * nn,struct netdev_bpf * bpf)1815 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
1816 {
1817 struct bpf_prog *prog = bpf->prog;
1818 struct nfp_net_dp *dp;
1819 int err;
1820
1821 if (!prog == !nn->dp.xdp_prog) {
1822 WRITE_ONCE(nn->dp.xdp_prog, prog);
1823 xdp_attachment_setup(&nn->xdp, bpf);
1824 return 0;
1825 }
1826
1827 dp = nfp_net_clone_dp(nn);
1828 if (!dp)
1829 return -ENOMEM;
1830
1831 dp->xdp_prog = prog;
1832 dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
1833 dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1834 dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
1835
1836 /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
1837 err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
1838 if (err)
1839 return err;
1840
1841 xdp_attachment_setup(&nn->xdp, bpf);
1842 return 0;
1843 }
1844
nfp_net_xdp_setup_hw(struct nfp_net * nn,struct netdev_bpf * bpf)1845 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
1846 {
1847 int err;
1848
1849 err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
1850 if (err)
1851 return err;
1852
1853 xdp_attachment_setup(&nn->xdp_hw, bpf);
1854 return 0;
1855 }
1856
nfp_net_xdp(struct net_device * netdev,struct netdev_bpf * xdp)1857 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
1858 {
1859 struct nfp_net *nn = netdev_priv(netdev);
1860
1861 switch (xdp->command) {
1862 case XDP_SETUP_PROG:
1863 return nfp_net_xdp_setup_drv(nn, xdp);
1864 case XDP_SETUP_PROG_HW:
1865 return nfp_net_xdp_setup_hw(nn, xdp);
1866 case XDP_SETUP_XSK_POOL:
1867 return nfp_net_xsk_setup_pool(netdev, xdp->xsk.pool,
1868 xdp->xsk.queue_id);
1869 default:
1870 return nfp_app_bpf(nn->app, nn, xdp);
1871 }
1872 }
1873
nfp_net_set_mac_address(struct net_device * netdev,void * addr)1874 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
1875 {
1876 struct nfp_net *nn = netdev_priv(netdev);
1877 struct sockaddr *saddr = addr;
1878 int err;
1879
1880 err = eth_prepare_mac_addr_change(netdev, addr);
1881 if (err)
1882 return err;
1883
1884 nfp_net_write_mac_addr(nn, saddr->sa_data);
1885
1886 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
1887 if (err)
1888 return err;
1889
1890 eth_commit_mac_addr_change(netdev, addr);
1891
1892 return 0;
1893 }
1894
1895 const struct net_device_ops nfp_nfd3_netdev_ops = {
1896 .ndo_init = nfp_app_ndo_init,
1897 .ndo_uninit = nfp_app_ndo_uninit,
1898 .ndo_open = nfp_net_netdev_open,
1899 .ndo_stop = nfp_net_netdev_close,
1900 .ndo_start_xmit = nfp_net_tx,
1901 .ndo_get_stats64 = nfp_net_stat64,
1902 .ndo_vlan_rx_add_vid = nfp_net_vlan_rx_add_vid,
1903 .ndo_vlan_rx_kill_vid = nfp_net_vlan_rx_kill_vid,
1904 .ndo_set_vf_mac = nfp_app_set_vf_mac,
1905 .ndo_set_vf_vlan = nfp_app_set_vf_vlan,
1906 .ndo_set_vf_rate = nfp_app_set_vf_rate,
1907 .ndo_set_vf_spoofchk = nfp_app_set_vf_spoofchk,
1908 .ndo_set_vf_trust = nfp_app_set_vf_trust,
1909 .ndo_get_vf_config = nfp_app_get_vf_config,
1910 .ndo_set_vf_link_state = nfp_app_set_vf_link_state,
1911 .ndo_setup_tc = nfp_port_setup_tc,
1912 .ndo_tx_timeout = nfp_net_tx_timeout,
1913 .ndo_set_rx_mode = nfp_net_set_rx_mode,
1914 .ndo_change_mtu = nfp_net_change_mtu,
1915 .ndo_set_mac_address = nfp_net_set_mac_address,
1916 .ndo_set_features = nfp_net_set_features,
1917 .ndo_features_check = nfp_net_features_check,
1918 .ndo_get_phys_port_name = nfp_net_get_phys_port_name,
1919 .ndo_bpf = nfp_net_xdp,
1920 .ndo_xsk_wakeup = nfp_net_xsk_wakeup,
1921 .ndo_get_devlink_port = nfp_devlink_get_devlink_port,
1922 };
1923
1924 const struct net_device_ops nfp_nfdk_netdev_ops = {
1925 .ndo_init = nfp_app_ndo_init,
1926 .ndo_uninit = nfp_app_ndo_uninit,
1927 .ndo_open = nfp_net_netdev_open,
1928 .ndo_stop = nfp_net_netdev_close,
1929 .ndo_start_xmit = nfp_net_tx,
1930 .ndo_get_stats64 = nfp_net_stat64,
1931 .ndo_vlan_rx_add_vid = nfp_net_vlan_rx_add_vid,
1932 .ndo_vlan_rx_kill_vid = nfp_net_vlan_rx_kill_vid,
1933 .ndo_set_vf_mac = nfp_app_set_vf_mac,
1934 .ndo_set_vf_vlan = nfp_app_set_vf_vlan,
1935 .ndo_set_vf_spoofchk = nfp_app_set_vf_spoofchk,
1936 .ndo_set_vf_trust = nfp_app_set_vf_trust,
1937 .ndo_get_vf_config = nfp_app_get_vf_config,
1938 .ndo_set_vf_link_state = nfp_app_set_vf_link_state,
1939 .ndo_setup_tc = nfp_port_setup_tc,
1940 .ndo_tx_timeout = nfp_net_tx_timeout,
1941 .ndo_set_rx_mode = nfp_net_set_rx_mode,
1942 .ndo_change_mtu = nfp_net_change_mtu,
1943 .ndo_set_mac_address = nfp_net_set_mac_address,
1944 .ndo_set_features = nfp_net_set_features,
1945 .ndo_features_check = nfp_net_features_check,
1946 .ndo_get_phys_port_name = nfp_net_get_phys_port_name,
1947 .ndo_bpf = nfp_net_xdp,
1948 .ndo_get_devlink_port = nfp_devlink_get_devlink_port,
1949 };
1950
nfp_udp_tunnel_sync(struct net_device * netdev,unsigned int table)1951 static int nfp_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
1952 {
1953 struct nfp_net *nn = netdev_priv(netdev);
1954 int i;
1955
1956 BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
1957 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2) {
1958 struct udp_tunnel_info ti0, ti1;
1959
1960 udp_tunnel_nic_get_port(netdev, table, i, &ti0);
1961 udp_tunnel_nic_get_port(netdev, table, i + 1, &ti1);
1962
1963 nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(ti0.port),
1964 be16_to_cpu(ti1.port) << 16 | be16_to_cpu(ti0.port));
1965 }
1966
1967 return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_VXLAN);
1968 }
1969
1970 static const struct udp_tunnel_nic_info nfp_udp_tunnels = {
1971 .sync_table = nfp_udp_tunnel_sync,
1972 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
1973 UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
1974 .tables = {
1975 {
1976 .n_entries = NFP_NET_N_VXLAN_PORTS,
1977 .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,
1978 },
1979 },
1980 };
1981
1982 /**
1983 * nfp_net_info() - Print general info about the NIC
1984 * @nn: NFP Net device to reconfigure
1985 */
nfp_net_info(struct nfp_net * nn)1986 void nfp_net_info(struct nfp_net *nn)
1987 {
1988 nn_info(nn, "NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
1989 nn->dp.is_vf ? "VF " : "",
1990 nn->dp.num_tx_rings, nn->max_tx_rings,
1991 nn->dp.num_rx_rings, nn->max_rx_rings);
1992 nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
1993 nn->fw_ver.extend, nn->fw_ver.class,
1994 nn->fw_ver.major, nn->fw_ver.minor,
1995 nn->max_mtu);
1996 nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1997 nn->cap,
1998 nn->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
1999 nn->cap & NFP_NET_CFG_CTRL_L2BC ? "L2BCFILT " : "",
2000 nn->cap & NFP_NET_CFG_CTRL_L2MC ? "L2MCFILT " : "",
2001 nn->cap & NFP_NET_CFG_CTRL_RXCSUM ? "RXCSUM " : "",
2002 nn->cap & NFP_NET_CFG_CTRL_TXCSUM ? "TXCSUM " : "",
2003 nn->cap & NFP_NET_CFG_CTRL_RXVLAN ? "RXVLAN " : "",
2004 nn->cap & NFP_NET_CFG_CTRL_TXVLAN ? "TXVLAN " : "",
2005 nn->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
2006 nn->cap & NFP_NET_CFG_CTRL_GATHER ? "GATHER " : "",
2007 nn->cap & NFP_NET_CFG_CTRL_LSO ? "TSO1 " : "",
2008 nn->cap & NFP_NET_CFG_CTRL_LSO2 ? "TSO2 " : "",
2009 nn->cap & NFP_NET_CFG_CTRL_RSS ? "RSS1 " : "",
2010 nn->cap & NFP_NET_CFG_CTRL_RSS2 ? "RSS2 " : "",
2011 nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
2012 nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
2013 nn->cap & NFP_NET_CFG_CTRL_IRQMOD ? "IRQMOD " : "",
2014 nn->cap & NFP_NET_CFG_CTRL_TXRWB ? "TXRWB " : "",
2015 nn->cap & NFP_NET_CFG_CTRL_VXLAN ? "VXLAN " : "",
2016 nn->cap & NFP_NET_CFG_CTRL_NVGRE ? "NVGRE " : "",
2017 nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
2018 "RXCSUM_COMPLETE " : "",
2019 nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
2020 nfp_app_extra_cap(nn->app, nn));
2021 }
2022
2023 /**
2024 * nfp_net_alloc() - Allocate netdev and related structure
2025 * @pdev: PCI device
2026 * @dev_info: NFP ASIC params
2027 * @ctrl_bar: PCI IOMEM with vNIC config memory
2028 * @needs_netdev: Whether to allocate a netdev for this vNIC
2029 * @max_tx_rings: Maximum number of TX rings supported by device
2030 * @max_rx_rings: Maximum number of RX rings supported by device
2031 *
2032 * This function allocates a netdev device and fills in the initial
2033 * part of the @struct nfp_net structure. In case of control device
2034 * nfp_net structure is allocated without the netdev.
2035 *
2036 * Return: NFP Net device structure, or ERR_PTR on error.
2037 */
2038 struct nfp_net *
nfp_net_alloc(struct pci_dev * pdev,const struct nfp_dev_info * dev_info,void __iomem * ctrl_bar,bool needs_netdev,unsigned int max_tx_rings,unsigned int max_rx_rings)2039 nfp_net_alloc(struct pci_dev *pdev, const struct nfp_dev_info *dev_info,
2040 void __iomem *ctrl_bar, bool needs_netdev,
2041 unsigned int max_tx_rings, unsigned int max_rx_rings)
2042 {
2043 struct nfp_net *nn;
2044 int err;
2045
2046 if (needs_netdev) {
2047 struct net_device *netdev;
2048
2049 netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
2050 max_tx_rings, max_rx_rings);
2051 if (!netdev)
2052 return ERR_PTR(-ENOMEM);
2053
2054 SET_NETDEV_DEV(netdev, &pdev->dev);
2055 nn = netdev_priv(netdev);
2056 nn->dp.netdev = netdev;
2057 } else {
2058 nn = vzalloc(sizeof(*nn));
2059 if (!nn)
2060 return ERR_PTR(-ENOMEM);
2061 }
2062
2063 nn->dp.dev = &pdev->dev;
2064 nn->dp.ctrl_bar = ctrl_bar;
2065 nn->dev_info = dev_info;
2066 nn->pdev = pdev;
2067 nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar);
2068
2069 switch (FIELD_GET(NFP_NET_CFG_VERSION_DP_MASK, nn->fw_ver.extend)) {
2070 case NFP_NET_CFG_VERSION_DP_NFD3:
2071 nn->dp.ops = &nfp_nfd3_ops;
2072 break;
2073 case NFP_NET_CFG_VERSION_DP_NFDK:
2074 if (nn->fw_ver.major < 5) {
2075 dev_err(&pdev->dev,
2076 "NFDK must use ABI 5 or newer, found: %d\n",
2077 nn->fw_ver.major);
2078 err = -EINVAL;
2079 goto err_free_nn;
2080 }
2081 nn->dp.ops = &nfp_nfdk_ops;
2082 break;
2083 default:
2084 err = -EINVAL;
2085 goto err_free_nn;
2086 }
2087
2088 nn->max_tx_rings = max_tx_rings;
2089 nn->max_rx_rings = max_rx_rings;
2090
2091 nn->dp.num_tx_rings = min_t(unsigned int,
2092 max_tx_rings, num_online_cpus());
2093 nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
2094 netif_get_num_default_rss_queues());
2095
2096 nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
2097 nn->dp.num_r_vecs = min_t(unsigned int,
2098 nn->dp.num_r_vecs, num_online_cpus());
2099 nn->max_r_vecs = nn->dp.num_r_vecs;
2100
2101 nn->dp.xsk_pools = kcalloc(nn->max_r_vecs, sizeof(nn->dp.xsk_pools),
2102 GFP_KERNEL);
2103 if (!nn->dp.xsk_pools) {
2104 err = -ENOMEM;
2105 goto err_free_nn;
2106 }
2107
2108 nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
2109 nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
2110
2111 sema_init(&nn->bar_lock, 1);
2112
2113 spin_lock_init(&nn->reconfig_lock);
2114 spin_lock_init(&nn->link_status_lock);
2115
2116 timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
2117
2118 err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
2119 &nn->tlv_caps);
2120 if (err)
2121 goto err_free_nn;
2122
2123 err = nfp_ccm_mbox_alloc(nn);
2124 if (err)
2125 goto err_free_nn;
2126
2127 return nn;
2128
2129 err_free_nn:
2130 if (nn->dp.netdev)
2131 free_netdev(nn->dp.netdev);
2132 else
2133 vfree(nn);
2134 return ERR_PTR(err);
2135 }
2136
2137 /**
2138 * nfp_net_free() - Undo what @nfp_net_alloc() did
2139 * @nn: NFP Net device to reconfigure
2140 */
nfp_net_free(struct nfp_net * nn)2141 void nfp_net_free(struct nfp_net *nn)
2142 {
2143 WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
2144 nfp_ccm_mbox_free(nn);
2145
2146 kfree(nn->dp.xsk_pools);
2147 if (nn->dp.netdev)
2148 free_netdev(nn->dp.netdev);
2149 else
2150 vfree(nn);
2151 }
2152
2153 /**
2154 * nfp_net_rss_key_sz() - Get current size of the RSS key
2155 * @nn: NFP Net device instance
2156 *
2157 * Return: size of the RSS key for currently selected hash function.
2158 */
nfp_net_rss_key_sz(struct nfp_net * nn)2159 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
2160 {
2161 switch (nn->rss_hfunc) {
2162 case ETH_RSS_HASH_TOP:
2163 return NFP_NET_CFG_RSS_KEY_SZ;
2164 case ETH_RSS_HASH_XOR:
2165 return 0;
2166 case ETH_RSS_HASH_CRC32:
2167 return 4;
2168 }
2169
2170 nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
2171 return 0;
2172 }
2173
2174 /**
2175 * nfp_net_rss_init() - Set the initial RSS parameters
2176 * @nn: NFP Net device to reconfigure
2177 */
nfp_net_rss_init(struct nfp_net * nn)2178 static void nfp_net_rss_init(struct nfp_net *nn)
2179 {
2180 unsigned long func_bit, rss_cap_hfunc;
2181 u32 reg;
2182
2183 /* Read the RSS function capability and select first supported func */
2184 reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
2185 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
2186 if (!rss_cap_hfunc)
2187 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
2188 NFP_NET_CFG_RSS_TOEPLITZ);
2189
2190 func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
2191 if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
2192 dev_warn(nn->dp.dev,
2193 "Bad RSS config, defaulting to Toeplitz hash\n");
2194 func_bit = ETH_RSS_HASH_TOP_BIT;
2195 }
2196 nn->rss_hfunc = 1 << func_bit;
2197
2198 netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
2199
2200 nfp_net_rss_init_itbl(nn);
2201
2202 /* Enable IPv4/IPv6 TCP by default */
2203 nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
2204 NFP_NET_CFG_RSS_IPV6_TCP |
2205 FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
2206 NFP_NET_CFG_RSS_MASK;
2207 }
2208
2209 /**
2210 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
2211 * @nn: NFP Net device to reconfigure
2212 */
nfp_net_irqmod_init(struct nfp_net * nn)2213 static void nfp_net_irqmod_init(struct nfp_net *nn)
2214 {
2215 nn->rx_coalesce_usecs = 50;
2216 nn->rx_coalesce_max_frames = 64;
2217 nn->tx_coalesce_usecs = 50;
2218 nn->tx_coalesce_max_frames = 64;
2219
2220 nn->rx_coalesce_adapt_on = true;
2221 nn->tx_coalesce_adapt_on = true;
2222 }
2223
nfp_net_netdev_init(struct nfp_net * nn)2224 static void nfp_net_netdev_init(struct nfp_net *nn)
2225 {
2226 struct net_device *netdev = nn->dp.netdev;
2227
2228 nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2229
2230 netdev->mtu = nn->dp.mtu;
2231
2232 /* Advertise/enable offloads based on capabilities
2233 *
2234 * Note: netdev->features show the currently enabled features
2235 * and netdev->hw_features advertises which features are
2236 * supported. By default we enable most features.
2237 */
2238 if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
2239 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2240
2241 netdev->hw_features = NETIF_F_HIGHDMA;
2242 if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
2243 netdev->hw_features |= NETIF_F_RXCSUM;
2244 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
2245 }
2246 if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
2247 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2248 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2249 }
2250 if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
2251 netdev->hw_features |= NETIF_F_SG;
2252 nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
2253 }
2254 if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
2255 nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2256 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2257 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
2258 NFP_NET_CFG_CTRL_LSO;
2259 }
2260 if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
2261 netdev->hw_features |= NETIF_F_RXHASH;
2262 if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
2263 if (nn->cap & NFP_NET_CFG_CTRL_LSO) {
2264 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
2265 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2266 NETIF_F_GSO_PARTIAL;
2267 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
2268 }
2269 netdev->udp_tunnel_nic_info = &nfp_udp_tunnels;
2270 nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
2271 }
2272 if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
2273 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
2274 netdev->hw_features |= NETIF_F_GSO_GRE;
2275 nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
2276 }
2277 if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
2278 netdev->hw_enc_features = netdev->hw_features;
2279
2280 netdev->vlan_features = netdev->hw_features;
2281
2282 if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
2283 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2284 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
2285 }
2286 if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
2287 if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2288 nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
2289 } else {
2290 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2291 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
2292 }
2293 }
2294 if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
2295 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2296 nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
2297 }
2298
2299 netdev->features = netdev->hw_features;
2300
2301 if (nfp_app_has_tc(nn->app) && nn->port)
2302 netdev->hw_features |= NETIF_F_HW_TC;
2303
2304 /* Advertise but disable TSO by default. */
2305 netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
2306 nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
2307
2308 /* Finalise the netdev setup */
2309 switch (nn->dp.ops->version) {
2310 case NFP_NFD_VER_NFD3:
2311 netdev->netdev_ops = &nfp_nfd3_netdev_ops;
2312 break;
2313 case NFP_NFD_VER_NFDK:
2314 netdev->netdev_ops = &nfp_nfdk_netdev_ops;
2315 break;
2316 }
2317
2318 netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
2319
2320 /* MTU range: 68 - hw-specific max */
2321 netdev->min_mtu = ETH_MIN_MTU;
2322 netdev->max_mtu = nn->max_mtu;
2323
2324 netif_set_tso_max_segs(netdev, NFP_NET_LSO_MAX_SEGS);
2325
2326 netif_carrier_off(netdev);
2327
2328 nfp_net_set_ethtool_ops(netdev);
2329 }
2330
nfp_net_read_caps(struct nfp_net * nn)2331 static int nfp_net_read_caps(struct nfp_net *nn)
2332 {
2333 /* Get some of the read-only fields from the BAR */
2334 nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
2335 nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
2336
2337 /* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
2338 * we allow use of non-chained metadata if RSS(v1) is the only
2339 * advertised capability requiring metadata.
2340 */
2341 nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
2342 !nn->dp.netdev ||
2343 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
2344 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
2345 /* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
2346 * it has the same meaning as RSSv2.
2347 */
2348 if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
2349 nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
2350
2351 /* Determine RX packet/metadata boundary offset */
2352 if (nn->fw_ver.major >= 2) {
2353 u32 reg;
2354
2355 reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
2356 if (reg > NFP_NET_MAX_PREPEND) {
2357 nn_err(nn, "Invalid rx offset: %d\n", reg);
2358 return -EINVAL;
2359 }
2360 nn->dp.rx_offset = reg;
2361 } else {
2362 nn->dp.rx_offset = NFP_NET_RX_OFFSET;
2363 }
2364
2365 /* Mask out NFD-version-specific features */
2366 nn->cap &= nn->dp.ops->cap_mask;
2367
2368 /* For control vNICs mask out the capabilities app doesn't want. */
2369 if (!nn->dp.netdev)
2370 nn->cap &= nn->app->type->ctrl_cap_mask;
2371
2372 return 0;
2373 }
2374
2375 /**
2376 * nfp_net_init() - Initialise/finalise the nfp_net structure
2377 * @nn: NFP Net device structure
2378 *
2379 * Return: 0 on success or negative errno on error.
2380 */
nfp_net_init(struct nfp_net * nn)2381 int nfp_net_init(struct nfp_net *nn)
2382 {
2383 int err;
2384
2385 nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
2386
2387 err = nfp_net_read_caps(nn);
2388 if (err)
2389 return err;
2390
2391 /* Set default MTU and Freelist buffer size */
2392 if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
2393 nn->dp.mtu = min(nn->app->ctrl_mtu, nn->max_mtu);
2394 } else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
2395 nn->dp.mtu = nn->max_mtu;
2396 } else {
2397 nn->dp.mtu = NFP_NET_DEFAULT_MTU;
2398 }
2399 nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
2400
2401 if (nfp_app_ctrl_uses_data_vnics(nn->app))
2402 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
2403
2404 if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
2405 nfp_net_rss_init(nn);
2406 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
2407 NFP_NET_CFG_CTRL_RSS;
2408 }
2409
2410 /* Allow L2 Broadcast and Multicast through by default, if supported */
2411 if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
2412 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
2413
2414 /* Allow IRQ moderation, if supported */
2415 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2416 nfp_net_irqmod_init(nn);
2417 nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2418 }
2419
2420 /* Enable TX pointer writeback, if supported */
2421 if (nn->cap & NFP_NET_CFG_CTRL_TXRWB)
2422 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXRWB;
2423
2424 /* Stash the re-configuration queue away. First odd queue in TX Bar */
2425 nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
2426
2427 /* Make sure the FW knows the netdev is supposed to be disabled here */
2428 nn_writel(nn, NFP_NET_CFG_CTRL, 0);
2429 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2430 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2431 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
2432 NFP_NET_CFG_UPDATE_GEN);
2433 if (err)
2434 return err;
2435
2436 if (nn->dp.netdev) {
2437 nfp_net_netdev_init(nn);
2438
2439 err = nfp_ccm_mbox_init(nn);
2440 if (err)
2441 return err;
2442
2443 err = nfp_net_tls_init(nn);
2444 if (err)
2445 goto err_clean_mbox;
2446 }
2447
2448 nfp_net_vecs_init(nn);
2449
2450 if (!nn->dp.netdev)
2451 return 0;
2452 return register_netdev(nn->dp.netdev);
2453
2454 err_clean_mbox:
2455 nfp_ccm_mbox_clean(nn);
2456 return err;
2457 }
2458
2459 /**
2460 * nfp_net_clean() - Undo what nfp_net_init() did.
2461 * @nn: NFP Net device structure
2462 */
nfp_net_clean(struct nfp_net * nn)2463 void nfp_net_clean(struct nfp_net *nn)
2464 {
2465 if (!nn->dp.netdev)
2466 return;
2467
2468 unregister_netdev(nn->dp.netdev);
2469 nfp_ccm_mbox_clean(nn);
2470 nfp_net_reconfig_wait_posted(nn);
2471 }
2472