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