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