1 // SPDX-License-Identifier: GPL-2.0-only 2 /**************************************************************************** 3 * Driver for Solarflare network controllers and boards 4 * Copyright 2018 Solarflare Communications Inc. 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 as published 8 * by the Free Software Foundation, incorporated herein by reference. 9 */ 10 11 #include "net_driver.h" 12 #include <linux/module.h> 13 #include <linux/filter.h> 14 #include "efx_channels.h" 15 #include "efx.h" 16 #include "efx_common.h" 17 #include "tx_common.h" 18 #include "rx_common.h" 19 #include "nic.h" 20 #include "sriov.h" 21 #include "workarounds.h" 22 23 /* This is the first interrupt mode to try out of: 24 * 0 => MSI-X 25 * 1 => MSI 26 * 2 => legacy 27 */ 28 unsigned int efx_interrupt_mode = EFX_INT_MODE_MSIX; 29 30 /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS), 31 * i.e. the number of CPUs among which we may distribute simultaneous 32 * interrupt handling. 33 * 34 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt. 35 * The default (0) means to assign an interrupt to each core. 36 */ 37 unsigned int rss_cpus; 38 39 static unsigned int irq_adapt_low_thresh = 8000; 40 module_param(irq_adapt_low_thresh, uint, 0644); 41 MODULE_PARM_DESC(irq_adapt_low_thresh, 42 "Threshold score for reducing IRQ moderation"); 43 44 static unsigned int irq_adapt_high_thresh = 16000; 45 module_param(irq_adapt_high_thresh, uint, 0644); 46 MODULE_PARM_DESC(irq_adapt_high_thresh, 47 "Threshold score for increasing IRQ moderation"); 48 49 /* This is the weight assigned to each of the (per-channel) virtual 50 * NAPI devices. 51 */ 52 static int napi_weight = 64; 53 54 static const struct efx_channel_type efx_default_channel_type; 55 56 /************* 57 * INTERRUPTS 58 *************/ 59 60 static unsigned int count_online_cores(struct efx_nic *efx, bool local_node) 61 { 62 cpumask_var_t filter_mask; 63 unsigned int count; 64 int cpu; 65 66 if (unlikely(!zalloc_cpumask_var(&filter_mask, GFP_KERNEL))) { 67 netif_warn(efx, probe, efx->net_dev, 68 "RSS disabled due to allocation failure\n"); 69 return 1; 70 } 71 72 cpumask_copy(filter_mask, cpu_online_mask); 73 if (local_node) 74 cpumask_and(filter_mask, filter_mask, 75 cpumask_of_pcibus(efx->pci_dev->bus)); 76 77 count = 0; 78 for_each_cpu(cpu, filter_mask) { 79 ++count; 80 cpumask_andnot(filter_mask, filter_mask, topology_sibling_cpumask(cpu)); 81 } 82 83 free_cpumask_var(filter_mask); 84 85 return count; 86 } 87 88 static unsigned int efx_wanted_parallelism(struct efx_nic *efx) 89 { 90 unsigned int count; 91 92 if (rss_cpus) { 93 count = rss_cpus; 94 } else { 95 count = count_online_cores(efx, true); 96 97 /* If no online CPUs in local node, fallback to any online CPUs */ 98 if (count == 0) 99 count = count_online_cores(efx, false); 100 } 101 102 if (count > EFX_MAX_RX_QUEUES) { 103 netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn, 104 "Reducing number of rx queues from %u to %u.\n", 105 count, EFX_MAX_RX_QUEUES); 106 count = EFX_MAX_RX_QUEUES; 107 } 108 109 /* If RSS is requested for the PF *and* VFs then we can't write RSS 110 * table entries that are inaccessible to VFs 111 */ 112 #ifdef CONFIG_SFC_SRIOV 113 if (efx->type->sriov_wanted) { 114 if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 && 115 count > efx_vf_size(efx)) { 116 netif_warn(efx, probe, efx->net_dev, 117 "Reducing number of RSS channels from %u to %u for " 118 "VF support. Increase vf-msix-limit to use more " 119 "channels on the PF.\n", 120 count, efx_vf_size(efx)); 121 count = efx_vf_size(efx); 122 } 123 } 124 #endif 125 126 return count; 127 } 128 129 static int efx_allocate_msix_channels(struct efx_nic *efx, 130 unsigned int max_channels, 131 unsigned int extra_channels, 132 unsigned int parallelism) 133 { 134 unsigned int n_channels = parallelism; 135 int vec_count; 136 int tx_per_ev; 137 int n_xdp_tx; 138 int n_xdp_ev; 139 140 if (efx_separate_tx_channels) 141 n_channels *= 2; 142 n_channels += extra_channels; 143 144 /* To allow XDP transmit to happen from arbitrary NAPI contexts 145 * we allocate a TX queue per CPU. We share event queues across 146 * multiple tx queues, assuming tx and ev queues are both 147 * maximum size. 148 */ 149 tx_per_ev = EFX_MAX_EVQ_SIZE / EFX_TXQ_MAX_ENT(efx); 150 tx_per_ev = min(tx_per_ev, EFX_MAX_TXQ_PER_CHANNEL); 151 n_xdp_tx = num_possible_cpus(); 152 n_xdp_ev = DIV_ROUND_UP(n_xdp_tx, tx_per_ev); 153 154 vec_count = pci_msix_vec_count(efx->pci_dev); 155 if (vec_count < 0) 156 return vec_count; 157 158 max_channels = min_t(unsigned int, vec_count, max_channels); 159 160 /* Check resources. 161 * We need a channel per event queue, plus a VI per tx queue. 162 * This may be more pessimistic than it needs to be. 163 */ 164 if (n_channels >= max_channels) { 165 efx->xdp_txq_queues_mode = EFX_XDP_TX_QUEUES_BORROWED; 166 netif_warn(efx, drv, efx->net_dev, 167 "Insufficient resources for %d XDP event queues (%d other channels, max %d)\n", 168 n_xdp_ev, n_channels, max_channels); 169 netif_warn(efx, drv, efx->net_dev, 170 "XDP_TX and XDP_REDIRECT might decrease device's performance\n"); 171 } else if (n_channels + n_xdp_tx > efx->max_vis) { 172 efx->xdp_txq_queues_mode = EFX_XDP_TX_QUEUES_BORROWED; 173 netif_warn(efx, drv, efx->net_dev, 174 "Insufficient resources for %d XDP TX queues (%d other channels, max VIs %d)\n", 175 n_xdp_tx, n_channels, efx->max_vis); 176 netif_warn(efx, drv, efx->net_dev, 177 "XDP_TX and XDP_REDIRECT might decrease device's performance\n"); 178 } else if (n_channels + n_xdp_ev > max_channels) { 179 efx->xdp_txq_queues_mode = EFX_XDP_TX_QUEUES_SHARED; 180 netif_warn(efx, drv, efx->net_dev, 181 "Insufficient resources for %d XDP event queues (%d other channels, max %d)\n", 182 n_xdp_ev, n_channels, max_channels); 183 184 n_xdp_ev = max_channels - n_channels; 185 netif_warn(efx, drv, efx->net_dev, 186 "XDP_TX and XDP_REDIRECT will work with reduced performance (%d cpus/tx_queue)\n", 187 DIV_ROUND_UP(n_xdp_tx, tx_per_ev * n_xdp_ev)); 188 } else { 189 efx->xdp_txq_queues_mode = EFX_XDP_TX_QUEUES_DEDICATED; 190 } 191 192 if (efx->xdp_txq_queues_mode != EFX_XDP_TX_QUEUES_BORROWED) { 193 efx->n_xdp_channels = n_xdp_ev; 194 efx->xdp_tx_per_channel = tx_per_ev; 195 efx->xdp_tx_queue_count = n_xdp_tx; 196 n_channels += n_xdp_ev; 197 netif_dbg(efx, drv, efx->net_dev, 198 "Allocating %d TX and %d event queues for XDP\n", 199 n_xdp_ev * tx_per_ev, n_xdp_ev); 200 } else { 201 efx->n_xdp_channels = 0; 202 efx->xdp_tx_per_channel = 0; 203 efx->xdp_tx_queue_count = n_xdp_tx; 204 } 205 206 if (vec_count < n_channels) { 207 netif_err(efx, drv, efx->net_dev, 208 "WARNING: Insufficient MSI-X vectors available (%d < %u).\n", 209 vec_count, n_channels); 210 netif_err(efx, drv, efx->net_dev, 211 "WARNING: Performance may be reduced.\n"); 212 n_channels = vec_count; 213 } 214 215 n_channels = min(n_channels, max_channels); 216 217 efx->n_channels = n_channels; 218 219 /* Ignore XDP tx channels when creating rx channels. */ 220 n_channels -= efx->n_xdp_channels; 221 222 if (efx_separate_tx_channels) { 223 efx->n_tx_channels = 224 min(max(n_channels / 2, 1U), 225 efx->max_tx_channels); 226 efx->tx_channel_offset = 227 n_channels - efx->n_tx_channels; 228 efx->n_rx_channels = 229 max(n_channels - 230 efx->n_tx_channels, 1U); 231 } else { 232 efx->n_tx_channels = min(n_channels, efx->max_tx_channels); 233 efx->tx_channel_offset = 0; 234 efx->n_rx_channels = n_channels; 235 } 236 237 efx->n_rx_channels = min(efx->n_rx_channels, parallelism); 238 efx->n_tx_channels = min(efx->n_tx_channels, parallelism); 239 240 efx->xdp_channel_offset = n_channels; 241 242 netif_dbg(efx, drv, efx->net_dev, 243 "Allocating %u RX channels\n", 244 efx->n_rx_channels); 245 246 return efx->n_channels; 247 } 248 249 /* Probe the number and type of interrupts we are able to obtain, and 250 * the resulting numbers of channels and RX queues. 251 */ 252 int efx_probe_interrupts(struct efx_nic *efx) 253 { 254 unsigned int extra_channels = 0; 255 unsigned int rss_spread; 256 unsigned int i, j; 257 int rc; 258 259 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) 260 if (efx->extra_channel_type[i]) 261 ++extra_channels; 262 263 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) { 264 unsigned int parallelism = efx_wanted_parallelism(efx); 265 struct msix_entry xentries[EFX_MAX_CHANNELS]; 266 unsigned int n_channels; 267 268 rc = efx_allocate_msix_channels(efx, efx->max_channels, 269 extra_channels, parallelism); 270 if (rc >= 0) { 271 n_channels = rc; 272 for (i = 0; i < n_channels; i++) 273 xentries[i].entry = i; 274 rc = pci_enable_msix_range(efx->pci_dev, xentries, 1, 275 n_channels); 276 } 277 if (rc < 0) { 278 /* Fall back to single channel MSI */ 279 netif_err(efx, drv, efx->net_dev, 280 "could not enable MSI-X\n"); 281 if (efx->type->min_interrupt_mode >= EFX_INT_MODE_MSI) 282 efx->interrupt_mode = EFX_INT_MODE_MSI; 283 else 284 return rc; 285 } else if (rc < n_channels) { 286 netif_err(efx, drv, efx->net_dev, 287 "WARNING: Insufficient MSI-X vectors" 288 " available (%d < %u).\n", rc, n_channels); 289 netif_err(efx, drv, efx->net_dev, 290 "WARNING: Performance may be reduced.\n"); 291 n_channels = rc; 292 } 293 294 if (rc > 0) { 295 for (i = 0; i < efx->n_channels; i++) 296 efx_get_channel(efx, i)->irq = 297 xentries[i].vector; 298 } 299 } 300 301 /* Try single interrupt MSI */ 302 if (efx->interrupt_mode == EFX_INT_MODE_MSI) { 303 efx->n_channels = 1; 304 efx->n_rx_channels = 1; 305 efx->n_tx_channels = 1; 306 efx->n_xdp_channels = 0; 307 efx->xdp_channel_offset = efx->n_channels; 308 rc = pci_enable_msi(efx->pci_dev); 309 if (rc == 0) { 310 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq; 311 } else { 312 netif_err(efx, drv, efx->net_dev, 313 "could not enable MSI\n"); 314 if (efx->type->min_interrupt_mode >= EFX_INT_MODE_LEGACY) 315 efx->interrupt_mode = EFX_INT_MODE_LEGACY; 316 else 317 return rc; 318 } 319 } 320 321 /* Assume legacy interrupts */ 322 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) { 323 efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0); 324 efx->n_rx_channels = 1; 325 efx->n_tx_channels = 1; 326 efx->n_xdp_channels = 0; 327 efx->xdp_channel_offset = efx->n_channels; 328 efx->legacy_irq = efx->pci_dev->irq; 329 } 330 331 /* Assign extra channels if possible, before XDP channels */ 332 efx->n_extra_tx_channels = 0; 333 j = efx->xdp_channel_offset; 334 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) { 335 if (!efx->extra_channel_type[i]) 336 continue; 337 if (j <= efx->tx_channel_offset + efx->n_tx_channels) { 338 efx->extra_channel_type[i]->handle_no_channel(efx); 339 } else { 340 --j; 341 efx_get_channel(efx, j)->type = 342 efx->extra_channel_type[i]; 343 if (efx_channel_has_tx_queues(efx_get_channel(efx, j))) 344 efx->n_extra_tx_channels++; 345 } 346 } 347 348 rss_spread = efx->n_rx_channels; 349 /* RSS might be usable on VFs even if it is disabled on the PF */ 350 #ifdef CONFIG_SFC_SRIOV 351 if (efx->type->sriov_wanted) { 352 efx->rss_spread = ((rss_spread > 1 || 353 !efx->type->sriov_wanted(efx)) ? 354 rss_spread : efx_vf_size(efx)); 355 return 0; 356 } 357 #endif 358 efx->rss_spread = rss_spread; 359 360 return 0; 361 } 362 363 #if defined(CONFIG_SMP) 364 void efx_set_interrupt_affinity(struct efx_nic *efx) 365 { 366 const struct cpumask *numa_mask = cpumask_of_pcibus(efx->pci_dev->bus); 367 struct efx_channel *channel; 368 unsigned int cpu; 369 370 /* If no online CPUs in local node, fallback to any online CPU */ 371 if (cpumask_first_and(cpu_online_mask, numa_mask) >= nr_cpu_ids) 372 numa_mask = cpu_online_mask; 373 374 cpu = -1; 375 efx_for_each_channel(channel, efx) { 376 cpu = cpumask_next_and(cpu, cpu_online_mask, numa_mask); 377 if (cpu >= nr_cpu_ids) 378 cpu = cpumask_first_and(cpu_online_mask, numa_mask); 379 irq_set_affinity_hint(channel->irq, cpumask_of(cpu)); 380 } 381 } 382 383 void efx_clear_interrupt_affinity(struct efx_nic *efx) 384 { 385 struct efx_channel *channel; 386 387 efx_for_each_channel(channel, efx) 388 irq_set_affinity_hint(channel->irq, NULL); 389 } 390 #else 391 void 392 efx_set_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused))) 393 { 394 } 395 396 void 397 efx_clear_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused))) 398 { 399 } 400 #endif /* CONFIG_SMP */ 401 402 void efx_remove_interrupts(struct efx_nic *efx) 403 { 404 struct efx_channel *channel; 405 406 /* Remove MSI/MSI-X interrupts */ 407 efx_for_each_channel(channel, efx) 408 channel->irq = 0; 409 pci_disable_msi(efx->pci_dev); 410 pci_disable_msix(efx->pci_dev); 411 412 /* Remove legacy interrupt */ 413 efx->legacy_irq = 0; 414 } 415 416 /*************** 417 * EVENT QUEUES 418 ***************/ 419 420 /* Create event queue 421 * Event queue memory allocations are done only once. If the channel 422 * is reset, the memory buffer will be reused; this guards against 423 * errors during channel reset and also simplifies interrupt handling. 424 */ 425 int efx_probe_eventq(struct efx_channel *channel) 426 { 427 struct efx_nic *efx = channel->efx; 428 unsigned long entries; 429 430 netif_dbg(efx, probe, efx->net_dev, 431 "chan %d create event queue\n", channel->channel); 432 433 /* Build an event queue with room for one event per tx and rx buffer, 434 * plus some extra for link state events and MCDI completions. 435 */ 436 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128); 437 EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE); 438 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1; 439 440 return efx_nic_probe_eventq(channel); 441 } 442 443 /* Prepare channel's event queue */ 444 int efx_init_eventq(struct efx_channel *channel) 445 { 446 struct efx_nic *efx = channel->efx; 447 int rc; 448 449 EFX_WARN_ON_PARANOID(channel->eventq_init); 450 451 netif_dbg(efx, drv, efx->net_dev, 452 "chan %d init event queue\n", channel->channel); 453 454 rc = efx_nic_init_eventq(channel); 455 if (rc == 0) { 456 efx->type->push_irq_moderation(channel); 457 channel->eventq_read_ptr = 0; 458 channel->eventq_init = true; 459 } 460 return rc; 461 } 462 463 /* Enable event queue processing and NAPI */ 464 void efx_start_eventq(struct efx_channel *channel) 465 { 466 netif_dbg(channel->efx, ifup, channel->efx->net_dev, 467 "chan %d start event queue\n", channel->channel); 468 469 /* Make sure the NAPI handler sees the enabled flag set */ 470 channel->enabled = true; 471 smp_wmb(); 472 473 napi_enable(&channel->napi_str); 474 efx_nic_eventq_read_ack(channel); 475 } 476 477 /* Disable event queue processing and NAPI */ 478 void efx_stop_eventq(struct efx_channel *channel) 479 { 480 if (!channel->enabled) 481 return; 482 483 napi_disable(&channel->napi_str); 484 channel->enabled = false; 485 } 486 487 void efx_fini_eventq(struct efx_channel *channel) 488 { 489 if (!channel->eventq_init) 490 return; 491 492 netif_dbg(channel->efx, drv, channel->efx->net_dev, 493 "chan %d fini event queue\n", channel->channel); 494 495 efx_nic_fini_eventq(channel); 496 channel->eventq_init = false; 497 } 498 499 void efx_remove_eventq(struct efx_channel *channel) 500 { 501 netif_dbg(channel->efx, drv, channel->efx->net_dev, 502 "chan %d remove event queue\n", channel->channel); 503 504 efx_nic_remove_eventq(channel); 505 } 506 507 /************************************************************************** 508 * 509 * Channel handling 510 * 511 *************************************************************************/ 512 513 #ifdef CONFIG_RFS_ACCEL 514 static void efx_filter_rfs_expire(struct work_struct *data) 515 { 516 struct delayed_work *dwork = to_delayed_work(data); 517 struct efx_channel *channel; 518 unsigned int time, quota; 519 520 channel = container_of(dwork, struct efx_channel, filter_work); 521 time = jiffies - channel->rfs_last_expiry; 522 quota = channel->rfs_filter_count * time / (30 * HZ); 523 if (quota >= 20 && __efx_filter_rfs_expire(channel, min(channel->rfs_filter_count, quota))) 524 channel->rfs_last_expiry += time; 525 /* Ensure we do more work eventually even if NAPI poll is not happening */ 526 schedule_delayed_work(dwork, 30 * HZ); 527 } 528 #endif 529 530 /* Allocate and initialise a channel structure. */ 531 static struct efx_channel *efx_alloc_channel(struct efx_nic *efx, int i) 532 { 533 struct efx_rx_queue *rx_queue; 534 struct efx_tx_queue *tx_queue; 535 struct efx_channel *channel; 536 int j; 537 538 channel = kzalloc(sizeof(*channel), GFP_KERNEL); 539 if (!channel) 540 return NULL; 541 542 channel->efx = efx; 543 channel->channel = i; 544 channel->type = &efx_default_channel_type; 545 546 for (j = 0; j < EFX_MAX_TXQ_PER_CHANNEL; j++) { 547 tx_queue = &channel->tx_queue[j]; 548 tx_queue->efx = efx; 549 tx_queue->queue = -1; 550 tx_queue->label = j; 551 tx_queue->channel = channel; 552 } 553 554 #ifdef CONFIG_RFS_ACCEL 555 INIT_DELAYED_WORK(&channel->filter_work, efx_filter_rfs_expire); 556 #endif 557 558 rx_queue = &channel->rx_queue; 559 rx_queue->efx = efx; 560 timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0); 561 562 return channel; 563 } 564 565 int efx_init_channels(struct efx_nic *efx) 566 { 567 unsigned int i; 568 569 for (i = 0; i < EFX_MAX_CHANNELS; i++) { 570 efx->channel[i] = efx_alloc_channel(efx, i); 571 if (!efx->channel[i]) 572 return -ENOMEM; 573 efx->msi_context[i].efx = efx; 574 efx->msi_context[i].index = i; 575 } 576 577 /* Higher numbered interrupt modes are less capable! */ 578 efx->interrupt_mode = min(efx->type->min_interrupt_mode, 579 efx_interrupt_mode); 580 581 efx->max_channels = EFX_MAX_CHANNELS; 582 efx->max_tx_channels = EFX_MAX_CHANNELS; 583 584 return 0; 585 } 586 587 void efx_fini_channels(struct efx_nic *efx) 588 { 589 unsigned int i; 590 591 for (i = 0; i < EFX_MAX_CHANNELS; i++) 592 if (efx->channel[i]) { 593 kfree(efx->channel[i]); 594 efx->channel[i] = NULL; 595 } 596 } 597 598 /* Allocate and initialise a channel structure, copying parameters 599 * (but not resources) from an old channel structure. 600 */ 601 static 602 struct efx_channel *efx_copy_channel(const struct efx_channel *old_channel) 603 { 604 struct efx_rx_queue *rx_queue; 605 struct efx_tx_queue *tx_queue; 606 struct efx_channel *channel; 607 int j; 608 609 channel = kmalloc(sizeof(*channel), GFP_KERNEL); 610 if (!channel) 611 return NULL; 612 613 *channel = *old_channel; 614 615 channel->napi_dev = NULL; 616 INIT_HLIST_NODE(&channel->napi_str.napi_hash_node); 617 channel->napi_str.napi_id = 0; 618 channel->napi_str.state = 0; 619 memset(&channel->eventq, 0, sizeof(channel->eventq)); 620 621 for (j = 0; j < EFX_MAX_TXQ_PER_CHANNEL; j++) { 622 tx_queue = &channel->tx_queue[j]; 623 if (tx_queue->channel) 624 tx_queue->channel = channel; 625 tx_queue->buffer = NULL; 626 tx_queue->cb_page = NULL; 627 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd)); 628 } 629 630 rx_queue = &channel->rx_queue; 631 rx_queue->buffer = NULL; 632 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd)); 633 timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0); 634 #ifdef CONFIG_RFS_ACCEL 635 INIT_DELAYED_WORK(&channel->filter_work, efx_filter_rfs_expire); 636 #endif 637 638 return channel; 639 } 640 641 static int efx_probe_channel(struct efx_channel *channel) 642 { 643 struct efx_tx_queue *tx_queue; 644 struct efx_rx_queue *rx_queue; 645 int rc; 646 647 netif_dbg(channel->efx, probe, channel->efx->net_dev, 648 "creating channel %d\n", channel->channel); 649 650 rc = channel->type->pre_probe(channel); 651 if (rc) 652 goto fail; 653 654 rc = efx_probe_eventq(channel); 655 if (rc) 656 goto fail; 657 658 efx_for_each_channel_tx_queue(tx_queue, channel) { 659 rc = efx_probe_tx_queue(tx_queue); 660 if (rc) 661 goto fail; 662 } 663 664 efx_for_each_channel_rx_queue(rx_queue, channel) { 665 rc = efx_probe_rx_queue(rx_queue); 666 if (rc) 667 goto fail; 668 } 669 670 channel->rx_list = NULL; 671 672 return 0; 673 674 fail: 675 efx_remove_channel(channel); 676 return rc; 677 } 678 679 static void efx_get_channel_name(struct efx_channel *channel, char *buf, 680 size_t len) 681 { 682 struct efx_nic *efx = channel->efx; 683 const char *type; 684 int number; 685 686 number = channel->channel; 687 688 if (number >= efx->xdp_channel_offset && 689 !WARN_ON_ONCE(!efx->n_xdp_channels)) { 690 type = "-xdp"; 691 number -= efx->xdp_channel_offset; 692 } else if (efx->tx_channel_offset == 0) { 693 type = ""; 694 } else if (number < efx->tx_channel_offset) { 695 type = "-rx"; 696 } else { 697 type = "-tx"; 698 number -= efx->tx_channel_offset; 699 } 700 snprintf(buf, len, "%s%s-%d", efx->name, type, number); 701 } 702 703 void efx_set_channel_names(struct efx_nic *efx) 704 { 705 struct efx_channel *channel; 706 707 efx_for_each_channel(channel, efx) 708 channel->type->get_name(channel, 709 efx->msi_context[channel->channel].name, 710 sizeof(efx->msi_context[0].name)); 711 } 712 713 int efx_probe_channels(struct efx_nic *efx) 714 { 715 struct efx_channel *channel; 716 int rc; 717 718 /* Restart special buffer allocation */ 719 efx->next_buffer_table = 0; 720 721 /* Probe channels in reverse, so that any 'extra' channels 722 * use the start of the buffer table. This allows the traffic 723 * channels to be resized without moving them or wasting the 724 * entries before them. 725 */ 726 efx_for_each_channel_rev(channel, efx) { 727 rc = efx_probe_channel(channel); 728 if (rc) { 729 netif_err(efx, probe, efx->net_dev, 730 "failed to create channel %d\n", 731 channel->channel); 732 goto fail; 733 } 734 } 735 efx_set_channel_names(efx); 736 737 return 0; 738 739 fail: 740 efx_remove_channels(efx); 741 return rc; 742 } 743 744 void efx_remove_channel(struct efx_channel *channel) 745 { 746 struct efx_tx_queue *tx_queue; 747 struct efx_rx_queue *rx_queue; 748 749 netif_dbg(channel->efx, drv, channel->efx->net_dev, 750 "destroy chan %d\n", channel->channel); 751 752 efx_for_each_channel_rx_queue(rx_queue, channel) 753 efx_remove_rx_queue(rx_queue); 754 efx_for_each_channel_tx_queue(tx_queue, channel) 755 efx_remove_tx_queue(tx_queue); 756 efx_remove_eventq(channel); 757 channel->type->post_remove(channel); 758 } 759 760 void efx_remove_channels(struct efx_nic *efx) 761 { 762 struct efx_channel *channel; 763 764 efx_for_each_channel(channel, efx) 765 efx_remove_channel(channel); 766 767 kfree(efx->xdp_tx_queues); 768 } 769 770 static int efx_set_xdp_tx_queue(struct efx_nic *efx, int xdp_queue_number, 771 struct efx_tx_queue *tx_queue) 772 { 773 if (xdp_queue_number >= efx->xdp_tx_queue_count) 774 return -EINVAL; 775 776 netif_dbg(efx, drv, efx->net_dev, 777 "Channel %u TXQ %u is XDP %u, HW %u\n", 778 tx_queue->channel->channel, tx_queue->label, 779 xdp_queue_number, tx_queue->queue); 780 efx->xdp_tx_queues[xdp_queue_number] = tx_queue; 781 return 0; 782 } 783 784 static void efx_set_xdp_channels(struct efx_nic *efx) 785 { 786 struct efx_tx_queue *tx_queue; 787 struct efx_channel *channel; 788 unsigned int next_queue = 0; 789 int xdp_queue_number = 0; 790 int rc; 791 792 /* We need to mark which channels really have RX and TX 793 * queues, and adjust the TX queue numbers if we have separate 794 * RX-only and TX-only channels. 795 */ 796 efx_for_each_channel(channel, efx) { 797 if (channel->channel < efx->tx_channel_offset) 798 continue; 799 800 if (efx_channel_is_xdp_tx(channel)) { 801 efx_for_each_channel_tx_queue(tx_queue, channel) { 802 tx_queue->queue = next_queue++; 803 rc = efx_set_xdp_tx_queue(efx, xdp_queue_number, 804 tx_queue); 805 if (rc == 0) 806 xdp_queue_number++; 807 } 808 } else { 809 efx_for_each_channel_tx_queue(tx_queue, channel) { 810 tx_queue->queue = next_queue++; 811 netif_dbg(efx, drv, efx->net_dev, 812 "Channel %u TXQ %u is HW %u\n", 813 channel->channel, tx_queue->label, 814 tx_queue->queue); 815 } 816 817 /* If XDP is borrowing queues from net stack, it must 818 * use the queue with no csum offload, which is the 819 * first one of the channel 820 * (note: tx_queue_by_type is not initialized yet) 821 */ 822 if (efx->xdp_txq_queues_mode == 823 EFX_XDP_TX_QUEUES_BORROWED) { 824 tx_queue = &channel->tx_queue[0]; 825 rc = efx_set_xdp_tx_queue(efx, xdp_queue_number, 826 tx_queue); 827 if (rc == 0) 828 xdp_queue_number++; 829 } 830 } 831 } 832 WARN_ON(efx->xdp_txq_queues_mode == EFX_XDP_TX_QUEUES_DEDICATED && 833 xdp_queue_number != efx->xdp_tx_queue_count); 834 WARN_ON(efx->xdp_txq_queues_mode != EFX_XDP_TX_QUEUES_DEDICATED && 835 xdp_queue_number > efx->xdp_tx_queue_count); 836 837 /* If we have more CPUs than assigned XDP TX queues, assign the already 838 * existing queues to the exceeding CPUs 839 */ 840 next_queue = 0; 841 while (xdp_queue_number < efx->xdp_tx_queue_count) { 842 tx_queue = efx->xdp_tx_queues[next_queue++]; 843 rc = efx_set_xdp_tx_queue(efx, xdp_queue_number, tx_queue); 844 if (rc == 0) 845 xdp_queue_number++; 846 } 847 } 848 849 int efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries) 850 { 851 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel; 852 unsigned int i, next_buffer_table = 0; 853 u32 old_rxq_entries, old_txq_entries; 854 int rc, rc2; 855 856 rc = efx_check_disabled(efx); 857 if (rc) 858 return rc; 859 860 /* Not all channels should be reallocated. We must avoid 861 * reallocating their buffer table entries. 862 */ 863 efx_for_each_channel(channel, efx) { 864 struct efx_rx_queue *rx_queue; 865 struct efx_tx_queue *tx_queue; 866 867 if (channel->type->copy) 868 continue; 869 next_buffer_table = max(next_buffer_table, 870 channel->eventq.index + 871 channel->eventq.entries); 872 efx_for_each_channel_rx_queue(rx_queue, channel) 873 next_buffer_table = max(next_buffer_table, 874 rx_queue->rxd.index + 875 rx_queue->rxd.entries); 876 efx_for_each_channel_tx_queue(tx_queue, channel) 877 next_buffer_table = max(next_buffer_table, 878 tx_queue->txd.index + 879 tx_queue->txd.entries); 880 } 881 882 efx_device_detach_sync(efx); 883 efx_stop_all(efx); 884 efx_soft_disable_interrupts(efx); 885 886 /* Clone channels (where possible) */ 887 memset(other_channel, 0, sizeof(other_channel)); 888 for (i = 0; i < efx->n_channels; i++) { 889 channel = efx->channel[i]; 890 if (channel->type->copy) 891 channel = channel->type->copy(channel); 892 if (!channel) { 893 rc = -ENOMEM; 894 goto out; 895 } 896 other_channel[i] = channel; 897 } 898 899 /* Swap entry counts and channel pointers */ 900 old_rxq_entries = efx->rxq_entries; 901 old_txq_entries = efx->txq_entries; 902 efx->rxq_entries = rxq_entries; 903 efx->txq_entries = txq_entries; 904 for (i = 0; i < efx->n_channels; i++) 905 swap(efx->channel[i], other_channel[i]); 906 907 /* Restart buffer table allocation */ 908 efx->next_buffer_table = next_buffer_table; 909 910 for (i = 0; i < efx->n_channels; i++) { 911 channel = efx->channel[i]; 912 if (!channel->type->copy) 913 continue; 914 rc = efx_probe_channel(channel); 915 if (rc) 916 goto rollback; 917 efx_init_napi_channel(efx->channel[i]); 918 } 919 920 efx_set_xdp_channels(efx); 921 out: 922 /* Destroy unused channel structures */ 923 for (i = 0; i < efx->n_channels; i++) { 924 channel = other_channel[i]; 925 if (channel && channel->type->copy) { 926 efx_fini_napi_channel(channel); 927 efx_remove_channel(channel); 928 kfree(channel); 929 } 930 } 931 932 rc2 = efx_soft_enable_interrupts(efx); 933 if (rc2) { 934 rc = rc ? rc : rc2; 935 netif_err(efx, drv, efx->net_dev, 936 "unable to restart interrupts on channel reallocation\n"); 937 efx_schedule_reset(efx, RESET_TYPE_DISABLE); 938 } else { 939 efx_start_all(efx); 940 efx_device_attach_if_not_resetting(efx); 941 } 942 return rc; 943 944 rollback: 945 /* Swap back */ 946 efx->rxq_entries = old_rxq_entries; 947 efx->txq_entries = old_txq_entries; 948 for (i = 0; i < efx->n_channels; i++) 949 swap(efx->channel[i], other_channel[i]); 950 goto out; 951 } 952 953 int efx_set_channels(struct efx_nic *efx) 954 { 955 struct efx_channel *channel; 956 int rc; 957 958 efx->tx_channel_offset = 959 efx_separate_tx_channels ? 960 efx->n_channels - efx->n_tx_channels : 0; 961 962 if (efx->xdp_tx_queue_count) { 963 EFX_WARN_ON_PARANOID(efx->xdp_tx_queues); 964 965 /* Allocate array for XDP TX queue lookup. */ 966 efx->xdp_tx_queues = kcalloc(efx->xdp_tx_queue_count, 967 sizeof(*efx->xdp_tx_queues), 968 GFP_KERNEL); 969 if (!efx->xdp_tx_queues) 970 return -ENOMEM; 971 } 972 973 efx_for_each_channel(channel, efx) { 974 if (channel->channel < efx->n_rx_channels) 975 channel->rx_queue.core_index = channel->channel; 976 else 977 channel->rx_queue.core_index = -1; 978 } 979 980 efx_set_xdp_channels(efx); 981 982 rc = netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels); 983 if (rc) 984 return rc; 985 return netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels); 986 } 987 988 static bool efx_default_channel_want_txqs(struct efx_channel *channel) 989 { 990 return channel->channel - channel->efx->tx_channel_offset < 991 channel->efx->n_tx_channels; 992 } 993 994 /************* 995 * START/STOP 996 *************/ 997 998 int efx_soft_enable_interrupts(struct efx_nic *efx) 999 { 1000 struct efx_channel *channel, *end_channel; 1001 int rc; 1002 1003 BUG_ON(efx->state == STATE_DISABLED); 1004 1005 efx->irq_soft_enabled = true; 1006 smp_wmb(); 1007 1008 efx_for_each_channel(channel, efx) { 1009 if (!channel->type->keep_eventq) { 1010 rc = efx_init_eventq(channel); 1011 if (rc) 1012 goto fail; 1013 } 1014 efx_start_eventq(channel); 1015 } 1016 1017 efx_mcdi_mode_event(efx); 1018 1019 return 0; 1020 fail: 1021 end_channel = channel; 1022 efx_for_each_channel(channel, efx) { 1023 if (channel == end_channel) 1024 break; 1025 efx_stop_eventq(channel); 1026 if (!channel->type->keep_eventq) 1027 efx_fini_eventq(channel); 1028 } 1029 1030 return rc; 1031 } 1032 1033 void efx_soft_disable_interrupts(struct efx_nic *efx) 1034 { 1035 struct efx_channel *channel; 1036 1037 if (efx->state == STATE_DISABLED) 1038 return; 1039 1040 efx_mcdi_mode_poll(efx); 1041 1042 efx->irq_soft_enabled = false; 1043 smp_wmb(); 1044 1045 if (efx->legacy_irq) 1046 synchronize_irq(efx->legacy_irq); 1047 1048 efx_for_each_channel(channel, efx) { 1049 if (channel->irq) 1050 synchronize_irq(channel->irq); 1051 1052 efx_stop_eventq(channel); 1053 if (!channel->type->keep_eventq) 1054 efx_fini_eventq(channel); 1055 } 1056 1057 /* Flush the asynchronous MCDI request queue */ 1058 efx_mcdi_flush_async(efx); 1059 } 1060 1061 int efx_enable_interrupts(struct efx_nic *efx) 1062 { 1063 struct efx_channel *channel, *end_channel; 1064 int rc; 1065 1066 /* TODO: Is this really a bug? */ 1067 BUG_ON(efx->state == STATE_DISABLED); 1068 1069 if (efx->eeh_disabled_legacy_irq) { 1070 enable_irq(efx->legacy_irq); 1071 efx->eeh_disabled_legacy_irq = false; 1072 } 1073 1074 efx->type->irq_enable_master(efx); 1075 1076 efx_for_each_channel(channel, efx) { 1077 if (channel->type->keep_eventq) { 1078 rc = efx_init_eventq(channel); 1079 if (rc) 1080 goto fail; 1081 } 1082 } 1083 1084 rc = efx_soft_enable_interrupts(efx); 1085 if (rc) 1086 goto fail; 1087 1088 return 0; 1089 1090 fail: 1091 end_channel = channel; 1092 efx_for_each_channel(channel, efx) { 1093 if (channel == end_channel) 1094 break; 1095 if (channel->type->keep_eventq) 1096 efx_fini_eventq(channel); 1097 } 1098 1099 efx->type->irq_disable_non_ev(efx); 1100 1101 return rc; 1102 } 1103 1104 void efx_disable_interrupts(struct efx_nic *efx) 1105 { 1106 struct efx_channel *channel; 1107 1108 efx_soft_disable_interrupts(efx); 1109 1110 efx_for_each_channel(channel, efx) { 1111 if (channel->type->keep_eventq) 1112 efx_fini_eventq(channel); 1113 } 1114 1115 efx->type->irq_disable_non_ev(efx); 1116 } 1117 1118 void efx_start_channels(struct efx_nic *efx) 1119 { 1120 struct efx_tx_queue *tx_queue; 1121 struct efx_rx_queue *rx_queue; 1122 struct efx_channel *channel; 1123 1124 efx_for_each_channel_rev(channel, efx) { 1125 efx_for_each_channel_tx_queue(tx_queue, channel) { 1126 efx_init_tx_queue(tx_queue); 1127 atomic_inc(&efx->active_queues); 1128 } 1129 1130 efx_for_each_channel_rx_queue(rx_queue, channel) { 1131 efx_init_rx_queue(rx_queue); 1132 atomic_inc(&efx->active_queues); 1133 efx_stop_eventq(channel); 1134 efx_fast_push_rx_descriptors(rx_queue, false); 1135 efx_start_eventq(channel); 1136 } 1137 1138 WARN_ON(channel->rx_pkt_n_frags); 1139 } 1140 } 1141 1142 void efx_stop_channels(struct efx_nic *efx) 1143 { 1144 struct efx_tx_queue *tx_queue; 1145 struct efx_rx_queue *rx_queue; 1146 struct efx_channel *channel; 1147 int rc = 0; 1148 1149 /* Stop RX refill */ 1150 efx_for_each_channel(channel, efx) { 1151 efx_for_each_channel_rx_queue(rx_queue, channel) 1152 rx_queue->refill_enabled = false; 1153 } 1154 1155 efx_for_each_channel(channel, efx) { 1156 /* RX packet processing is pipelined, so wait for the 1157 * NAPI handler to complete. At least event queue 0 1158 * might be kept active by non-data events, so don't 1159 * use napi_synchronize() but actually disable NAPI 1160 * temporarily. 1161 */ 1162 if (efx_channel_has_rx_queue(channel)) { 1163 efx_stop_eventq(channel); 1164 efx_start_eventq(channel); 1165 } 1166 } 1167 1168 if (efx->type->fini_dmaq) 1169 rc = efx->type->fini_dmaq(efx); 1170 1171 if (rc) { 1172 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n"); 1173 } else { 1174 netif_dbg(efx, drv, efx->net_dev, 1175 "successfully flushed all queues\n"); 1176 } 1177 1178 efx_for_each_channel(channel, efx) { 1179 efx_for_each_channel_rx_queue(rx_queue, channel) 1180 efx_fini_rx_queue(rx_queue); 1181 efx_for_each_channel_tx_queue(tx_queue, channel) 1182 efx_fini_tx_queue(tx_queue); 1183 } 1184 } 1185 1186 /************************************************************************** 1187 * 1188 * NAPI interface 1189 * 1190 *************************************************************************/ 1191 1192 /* Process channel's event queue 1193 * 1194 * This function is responsible for processing the event queue of a 1195 * single channel. The caller must guarantee that this function will 1196 * never be concurrently called more than once on the same channel, 1197 * though different channels may be being processed concurrently. 1198 */ 1199 static int efx_process_channel(struct efx_channel *channel, int budget) 1200 { 1201 struct efx_tx_queue *tx_queue; 1202 struct list_head rx_list; 1203 int spent; 1204 1205 if (unlikely(!channel->enabled)) 1206 return 0; 1207 1208 /* Prepare the batch receive list */ 1209 EFX_WARN_ON_PARANOID(channel->rx_list != NULL); 1210 INIT_LIST_HEAD(&rx_list); 1211 channel->rx_list = &rx_list; 1212 1213 efx_for_each_channel_tx_queue(tx_queue, channel) { 1214 tx_queue->pkts_compl = 0; 1215 tx_queue->bytes_compl = 0; 1216 } 1217 1218 spent = efx_nic_process_eventq(channel, budget); 1219 if (spent && efx_channel_has_rx_queue(channel)) { 1220 struct efx_rx_queue *rx_queue = 1221 efx_channel_get_rx_queue(channel); 1222 1223 efx_rx_flush_packet(channel); 1224 efx_fast_push_rx_descriptors(rx_queue, true); 1225 } 1226 1227 /* Update BQL */ 1228 efx_for_each_channel_tx_queue(tx_queue, channel) { 1229 if (tx_queue->bytes_compl) { 1230 netdev_tx_completed_queue(tx_queue->core_txq, 1231 tx_queue->pkts_compl, 1232 tx_queue->bytes_compl); 1233 } 1234 } 1235 1236 /* Receive any packets we queued up */ 1237 netif_receive_skb_list(channel->rx_list); 1238 channel->rx_list = NULL; 1239 1240 return spent; 1241 } 1242 1243 static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel) 1244 { 1245 int step = efx->irq_mod_step_us; 1246 1247 if (channel->irq_mod_score < irq_adapt_low_thresh) { 1248 if (channel->irq_moderation_us > step) { 1249 channel->irq_moderation_us -= step; 1250 efx->type->push_irq_moderation(channel); 1251 } 1252 } else if (channel->irq_mod_score > irq_adapt_high_thresh) { 1253 if (channel->irq_moderation_us < 1254 efx->irq_rx_moderation_us) { 1255 channel->irq_moderation_us += step; 1256 efx->type->push_irq_moderation(channel); 1257 } 1258 } 1259 1260 channel->irq_count = 0; 1261 channel->irq_mod_score = 0; 1262 } 1263 1264 /* NAPI poll handler 1265 * 1266 * NAPI guarantees serialisation of polls of the same device, which 1267 * provides the guarantee required by efx_process_channel(). 1268 */ 1269 static int efx_poll(struct napi_struct *napi, int budget) 1270 { 1271 struct efx_channel *channel = 1272 container_of(napi, struct efx_channel, napi_str); 1273 struct efx_nic *efx = channel->efx; 1274 #ifdef CONFIG_RFS_ACCEL 1275 unsigned int time; 1276 #endif 1277 int spent; 1278 1279 netif_vdbg(efx, intr, efx->net_dev, 1280 "channel %d NAPI poll executing on CPU %d\n", 1281 channel->channel, raw_smp_processor_id()); 1282 1283 spent = efx_process_channel(channel, budget); 1284 1285 xdp_do_flush_map(); 1286 1287 if (spent < budget) { 1288 if (efx_channel_has_rx_queue(channel) && 1289 efx->irq_rx_adaptive && 1290 unlikely(++channel->irq_count == 1000)) { 1291 efx_update_irq_mod(efx, channel); 1292 } 1293 1294 #ifdef CONFIG_RFS_ACCEL 1295 /* Perhaps expire some ARFS filters */ 1296 time = jiffies - channel->rfs_last_expiry; 1297 /* Would our quota be >= 20? */ 1298 if (channel->rfs_filter_count * time >= 600 * HZ) 1299 mod_delayed_work(system_wq, &channel->filter_work, 0); 1300 #endif 1301 1302 /* There is no race here; although napi_disable() will 1303 * only wait for napi_complete(), this isn't a problem 1304 * since efx_nic_eventq_read_ack() will have no effect if 1305 * interrupts have already been disabled. 1306 */ 1307 if (napi_complete_done(napi, spent)) 1308 efx_nic_eventq_read_ack(channel); 1309 } 1310 1311 return spent; 1312 } 1313 1314 void efx_init_napi_channel(struct efx_channel *channel) 1315 { 1316 struct efx_nic *efx = channel->efx; 1317 1318 channel->napi_dev = efx->net_dev; 1319 netif_napi_add_weight(channel->napi_dev, &channel->napi_str, efx_poll, 1320 napi_weight); 1321 } 1322 1323 void efx_init_napi(struct efx_nic *efx) 1324 { 1325 struct efx_channel *channel; 1326 1327 efx_for_each_channel(channel, efx) 1328 efx_init_napi_channel(channel); 1329 } 1330 1331 void efx_fini_napi_channel(struct efx_channel *channel) 1332 { 1333 if (channel->napi_dev) 1334 netif_napi_del(&channel->napi_str); 1335 1336 channel->napi_dev = NULL; 1337 } 1338 1339 void efx_fini_napi(struct efx_nic *efx) 1340 { 1341 struct efx_channel *channel; 1342 1343 efx_for_each_channel(channel, efx) 1344 efx_fini_napi_channel(channel); 1345 } 1346 1347 /*************** 1348 * Housekeeping 1349 ***************/ 1350 1351 static int efx_channel_dummy_op_int(struct efx_channel *channel) 1352 { 1353 return 0; 1354 } 1355 1356 void efx_channel_dummy_op_void(struct efx_channel *channel) 1357 { 1358 } 1359 1360 static const struct efx_channel_type efx_default_channel_type = { 1361 .pre_probe = efx_channel_dummy_op_int, 1362 .post_remove = efx_channel_dummy_op_void, 1363 .get_name = efx_get_channel_name, 1364 .copy = efx_copy_channel, 1365 .want_txqs = efx_default_channel_want_txqs, 1366 .keep_eventq = false, 1367 .want_pio = true, 1368 }; 1369