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