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