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