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