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