xref: /dpdk/drivers/net/sfc/sfc_ev.c (revision fb73e096110a41b77448fe27fd9be8c489ec5d82)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2016-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9 
10 #include <rte_debug.h>
11 #include <rte_cycles.h>
12 #include <rte_alarm.h>
13 #include <rte_branch_prediction.h>
14 
15 #include "efx.h"
16 
17 #include "sfc.h"
18 #include "sfc_debug.h"
19 #include "sfc_log.h"
20 #include "sfc_ev.h"
21 #include "sfc_rx.h"
22 #include "sfc_tx.h"
23 #include "sfc_kvargs.h"
24 
25 
26 /* Initial delay when waiting for event queue init complete event */
27 #define SFC_EVQ_INIT_BACKOFF_START_US	(1)
28 /* Maximum delay between event queue polling attempts */
29 #define SFC_EVQ_INIT_BACKOFF_MAX_US	(10 * 1000)
30 /* Event queue init approx timeout */
31 #define SFC_EVQ_INIT_TIMEOUT_US		(2 * US_PER_S)
32 
33 /* Management event queue polling period in microseconds */
34 #define SFC_MGMT_EV_QPOLL_PERIOD_US	(US_PER_S)
35 
36 static const char *
37 sfc_evq_type2str(enum sfc_evq_type type)
38 {
39 	switch (type) {
40 	case SFC_EVQ_TYPE_MGMT:
41 		return "mgmt-evq";
42 	case SFC_EVQ_TYPE_RX:
43 		return "rx-evq";
44 	case SFC_EVQ_TYPE_TX:
45 		return "tx-evq";
46 	default:
47 		SFC_ASSERT(B_FALSE);
48 		return NULL;
49 	}
50 }
51 
52 static boolean_t
53 sfc_ev_initialized(void *arg)
54 {
55 	struct sfc_evq *evq = arg;
56 
57 	/* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
58 	SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
59 		   evq->init_state == SFC_EVQ_STARTED);
60 
61 	evq->init_state = SFC_EVQ_STARTED;
62 
63 	return B_FALSE;
64 }
65 
66 static boolean_t
67 sfc_ev_nop_rx(void *arg, uint32_t label, uint32_t id,
68 	      uint32_t size, uint16_t flags)
69 {
70 	struct sfc_evq *evq = arg;
71 
72 	sfc_err(evq->sa,
73 		"EVQ %u unexpected Rx event label=%u id=%#x size=%u flags=%#x",
74 		evq->evq_index, label, id, size, flags);
75 	return B_TRUE;
76 }
77 
78 static boolean_t
79 sfc_ev_efx_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
80 	      uint32_t size, uint16_t flags)
81 {
82 	struct sfc_evq *evq = arg;
83 	struct sfc_efx_rxq *rxq;
84 	unsigned int stop;
85 	unsigned int pending_id;
86 	unsigned int delta;
87 	unsigned int i;
88 	struct sfc_efx_rx_sw_desc *rxd;
89 
90 	if (unlikely(evq->exception))
91 		goto done;
92 
93 	rxq = sfc_efx_rxq_by_dp_rxq(evq->dp_rxq);
94 
95 	SFC_ASSERT(rxq != NULL);
96 	SFC_ASSERT(rxq->evq == evq);
97 	SFC_ASSERT(rxq->flags & SFC_EFX_RXQ_FLAG_STARTED);
98 
99 	stop = (id + 1) & rxq->ptr_mask;
100 	pending_id = rxq->pending & rxq->ptr_mask;
101 	delta = (stop >= pending_id) ? (stop - pending_id) :
102 		(rxq->ptr_mask + 1 - pending_id + stop);
103 
104 	if (delta == 0) {
105 		/*
106 		 * Rx event with no new descriptors done and zero length
107 		 * is used to abort scattered packet when there is no room
108 		 * for the tail.
109 		 */
110 		if (unlikely(size != 0)) {
111 			evq->exception = B_TRUE;
112 			sfc_err(evq->sa,
113 				"EVQ %u RxQ %u invalid RX abort "
114 				"(id=%#x size=%u flags=%#x); needs restart",
115 				evq->evq_index, rxq->dp.dpq.queue_id,
116 				id, size, flags);
117 			goto done;
118 		}
119 
120 		/* Add discard flag to the first fragment */
121 		rxq->sw_desc[pending_id].flags |= EFX_DISCARD;
122 		/* Remove continue flag from the last fragment */
123 		rxq->sw_desc[id].flags &= ~EFX_PKT_CONT;
124 	} else if (unlikely(delta > rxq->batch_max)) {
125 		evq->exception = B_TRUE;
126 
127 		sfc_err(evq->sa,
128 			"EVQ %u RxQ %u completion out of order "
129 			"(id=%#x delta=%u flags=%#x); needs restart",
130 			evq->evq_index, rxq->dp.dpq.queue_id,
131 			id, delta, flags);
132 
133 		goto done;
134 	}
135 
136 	for (i = pending_id; i != stop; i = (i + 1) & rxq->ptr_mask) {
137 		rxd = &rxq->sw_desc[i];
138 
139 		rxd->flags = flags;
140 
141 		SFC_ASSERT(size < (1 << 16));
142 		rxd->size = (uint16_t)size;
143 	}
144 
145 	rxq->pending += delta;
146 
147 done:
148 	return B_FALSE;
149 }
150 
151 static boolean_t
152 sfc_ev_dp_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
153 	     __rte_unused uint32_t size, __rte_unused uint16_t flags)
154 {
155 	struct sfc_evq *evq = arg;
156 	struct sfc_dp_rxq *dp_rxq;
157 
158 	dp_rxq = evq->dp_rxq;
159 	SFC_ASSERT(dp_rxq != NULL);
160 
161 	SFC_ASSERT(evq->sa->dp_rx->qrx_ev != NULL);
162 	return evq->sa->dp_rx->qrx_ev(dp_rxq, id);
163 }
164 
165 static boolean_t
166 sfc_ev_nop_tx(void *arg, uint32_t label, uint32_t id)
167 {
168 	struct sfc_evq *evq = arg;
169 
170 	sfc_err(evq->sa, "EVQ %u unexpected Tx event label=%u id=%#x",
171 		evq->evq_index, label, id);
172 	return B_TRUE;
173 }
174 
175 static boolean_t
176 sfc_ev_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
177 {
178 	struct sfc_evq *evq = arg;
179 	struct sfc_dp_txq *dp_txq;
180 	struct sfc_efx_txq *txq;
181 	unsigned int stop;
182 	unsigned int delta;
183 
184 	dp_txq = evq->dp_txq;
185 	SFC_ASSERT(dp_txq != NULL);
186 
187 	txq = sfc_efx_txq_by_dp_txq(dp_txq);
188 	SFC_ASSERT(txq->evq == evq);
189 
190 	if (unlikely((txq->flags & SFC_EFX_TXQ_FLAG_STARTED) == 0))
191 		goto done;
192 
193 	stop = (id + 1) & txq->ptr_mask;
194 	id = txq->pending & txq->ptr_mask;
195 
196 	delta = (stop >= id) ? (stop - id) : (txq->ptr_mask + 1 - id + stop);
197 
198 	txq->pending += delta;
199 
200 done:
201 	return B_FALSE;
202 }
203 
204 static boolean_t
205 sfc_ev_dp_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
206 {
207 	struct sfc_evq *evq = arg;
208 	struct sfc_dp_txq *dp_txq;
209 
210 	dp_txq = evq->dp_txq;
211 	SFC_ASSERT(dp_txq != NULL);
212 
213 	SFC_ASSERT(evq->sa->dp_tx->qtx_ev != NULL);
214 	return evq->sa->dp_tx->qtx_ev(dp_txq, id);
215 }
216 
217 static boolean_t
218 sfc_ev_exception(void *arg, uint32_t code, __rte_unused uint32_t data)
219 {
220 	struct sfc_evq *evq = arg;
221 
222 	if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
223 		return B_FALSE;
224 
225 	evq->exception = B_TRUE;
226 	sfc_warn(evq->sa,
227 		 "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
228 		 " needs recovery",
229 		 (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
230 		 (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
231 		 (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
232 		 (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
233 		 (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
234 		 (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
235 		 (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
236 		 (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
237 		 "UNKNOWN",
238 		 code, data, evq->evq_index);
239 
240 	return B_TRUE;
241 }
242 
243 static boolean_t
244 sfc_ev_nop_rxq_flush_done(void *arg, uint32_t rxq_hw_index)
245 {
246 	struct sfc_evq *evq = arg;
247 
248 	sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush done",
249 		evq->evq_index, rxq_hw_index);
250 	return B_TRUE;
251 }
252 
253 static boolean_t
254 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
255 {
256 	struct sfc_evq *evq = arg;
257 	struct sfc_dp_rxq *dp_rxq;
258 	struct sfc_rxq *rxq;
259 
260 	dp_rxq = evq->dp_rxq;
261 	SFC_ASSERT(dp_rxq != NULL);
262 
263 	rxq = sfc_rxq_by_dp_rxq(dp_rxq);
264 	SFC_ASSERT(rxq != NULL);
265 	SFC_ASSERT(rxq->hw_index == rxq_hw_index);
266 	SFC_ASSERT(rxq->evq == evq);
267 	sfc_rx_qflush_done(rxq);
268 
269 	return B_FALSE;
270 }
271 
272 static boolean_t
273 sfc_ev_nop_rxq_flush_failed(void *arg, uint32_t rxq_hw_index)
274 {
275 	struct sfc_evq *evq = arg;
276 
277 	sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush failed",
278 		evq->evq_index, rxq_hw_index);
279 	return B_TRUE;
280 }
281 
282 static boolean_t
283 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
284 {
285 	struct sfc_evq *evq = arg;
286 	struct sfc_dp_rxq *dp_rxq;
287 	struct sfc_rxq *rxq;
288 
289 	dp_rxq = evq->dp_rxq;
290 	SFC_ASSERT(dp_rxq != NULL);
291 
292 	rxq = sfc_rxq_by_dp_rxq(dp_rxq);
293 	SFC_ASSERT(rxq != NULL);
294 	SFC_ASSERT(rxq->hw_index == rxq_hw_index);
295 	SFC_ASSERT(rxq->evq == evq);
296 	sfc_rx_qflush_failed(rxq);
297 
298 	return B_FALSE;
299 }
300 
301 static boolean_t
302 sfc_ev_nop_txq_flush_done(void *arg, uint32_t txq_hw_index)
303 {
304 	struct sfc_evq *evq = arg;
305 
306 	sfc_err(evq->sa, "EVQ %u unexpected TxQ %u flush done",
307 		evq->evq_index, txq_hw_index);
308 	return B_TRUE;
309 }
310 
311 static boolean_t
312 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
313 {
314 	struct sfc_evq *evq = arg;
315 	struct sfc_dp_txq *dp_txq;
316 	struct sfc_txq *txq;
317 
318 	dp_txq = evq->dp_txq;
319 	SFC_ASSERT(dp_txq != NULL);
320 
321 	txq = sfc_txq_by_dp_txq(dp_txq);
322 	SFC_ASSERT(txq != NULL);
323 	SFC_ASSERT(txq->hw_index == txq_hw_index);
324 	SFC_ASSERT(txq->evq == evq);
325 	sfc_tx_qflush_done(txq);
326 
327 	return B_FALSE;
328 }
329 
330 static boolean_t
331 sfc_ev_software(void *arg, uint16_t magic)
332 {
333 	struct sfc_evq *evq = arg;
334 
335 	sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
336 		evq->evq_index, magic);
337 	return B_TRUE;
338 }
339 
340 static boolean_t
341 sfc_ev_sram(void *arg, uint32_t code)
342 {
343 	struct sfc_evq *evq = arg;
344 
345 	sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
346 		evq->evq_index, code);
347 	return B_TRUE;
348 }
349 
350 static boolean_t
351 sfc_ev_wake_up(void *arg, uint32_t index)
352 {
353 	struct sfc_evq *evq = arg;
354 
355 	sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
356 		evq->evq_index, index);
357 	return B_TRUE;
358 }
359 
360 static boolean_t
361 sfc_ev_timer(void *arg, uint32_t index)
362 {
363 	struct sfc_evq *evq = arg;
364 
365 	sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
366 		evq->evq_index, index);
367 	return B_TRUE;
368 }
369 
370 static boolean_t
371 sfc_ev_nop_link_change(void *arg, __rte_unused efx_link_mode_t link_mode)
372 {
373 	struct sfc_evq *evq = arg;
374 
375 	sfc_err(evq->sa, "EVQ %u unexpected link change event",
376 		evq->evq_index);
377 	return B_TRUE;
378 }
379 
380 static boolean_t
381 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
382 {
383 	struct sfc_evq *evq = arg;
384 	struct sfc_adapter *sa = evq->sa;
385 	struct rte_eth_link new_link;
386 
387 	sfc_port_link_mode_to_info(link_mode, &new_link);
388 	if (rte_eth_linkstatus_set(sa->eth_dev, &new_link))
389 		evq->sa->port.lsc_seq++;
390 
391 	return B_FALSE;
392 }
393 
394 static const efx_ev_callbacks_t sfc_ev_callbacks = {
395 	.eec_initialized	= sfc_ev_initialized,
396 	.eec_rx			= sfc_ev_nop_rx,
397 	.eec_tx			= sfc_ev_nop_tx,
398 	.eec_exception		= sfc_ev_exception,
399 	.eec_rxq_flush_done	= sfc_ev_nop_rxq_flush_done,
400 	.eec_rxq_flush_failed	= sfc_ev_nop_rxq_flush_failed,
401 	.eec_txq_flush_done	= sfc_ev_nop_txq_flush_done,
402 	.eec_software		= sfc_ev_software,
403 	.eec_sram		= sfc_ev_sram,
404 	.eec_wake_up		= sfc_ev_wake_up,
405 	.eec_timer		= sfc_ev_timer,
406 	.eec_link_change	= sfc_ev_link_change,
407 };
408 
409 static const efx_ev_callbacks_t sfc_ev_callbacks_efx_rx = {
410 	.eec_initialized	= sfc_ev_initialized,
411 	.eec_rx			= sfc_ev_efx_rx,
412 	.eec_tx			= sfc_ev_nop_tx,
413 	.eec_exception		= sfc_ev_exception,
414 	.eec_rxq_flush_done	= sfc_ev_rxq_flush_done,
415 	.eec_rxq_flush_failed	= sfc_ev_rxq_flush_failed,
416 	.eec_txq_flush_done	= sfc_ev_nop_txq_flush_done,
417 	.eec_software		= sfc_ev_software,
418 	.eec_sram		= sfc_ev_sram,
419 	.eec_wake_up		= sfc_ev_wake_up,
420 	.eec_timer		= sfc_ev_timer,
421 	.eec_link_change	= sfc_ev_nop_link_change,
422 };
423 
424 static const efx_ev_callbacks_t sfc_ev_callbacks_dp_rx = {
425 	.eec_initialized	= sfc_ev_initialized,
426 	.eec_rx			= sfc_ev_dp_rx,
427 	.eec_tx			= sfc_ev_nop_tx,
428 	.eec_exception		= sfc_ev_exception,
429 	.eec_rxq_flush_done	= sfc_ev_rxq_flush_done,
430 	.eec_rxq_flush_failed	= sfc_ev_rxq_flush_failed,
431 	.eec_txq_flush_done	= sfc_ev_nop_txq_flush_done,
432 	.eec_software		= sfc_ev_software,
433 	.eec_sram		= sfc_ev_sram,
434 	.eec_wake_up		= sfc_ev_wake_up,
435 	.eec_timer		= sfc_ev_timer,
436 	.eec_link_change	= sfc_ev_nop_link_change,
437 };
438 
439 static const efx_ev_callbacks_t sfc_ev_callbacks_efx_tx = {
440 	.eec_initialized	= sfc_ev_initialized,
441 	.eec_rx			= sfc_ev_nop_rx,
442 	.eec_tx			= sfc_ev_tx,
443 	.eec_exception		= sfc_ev_exception,
444 	.eec_rxq_flush_done	= sfc_ev_nop_rxq_flush_done,
445 	.eec_rxq_flush_failed	= sfc_ev_nop_rxq_flush_failed,
446 	.eec_txq_flush_done	= sfc_ev_txq_flush_done,
447 	.eec_software		= sfc_ev_software,
448 	.eec_sram		= sfc_ev_sram,
449 	.eec_wake_up		= sfc_ev_wake_up,
450 	.eec_timer		= sfc_ev_timer,
451 	.eec_link_change	= sfc_ev_nop_link_change,
452 };
453 
454 static const efx_ev_callbacks_t sfc_ev_callbacks_dp_tx = {
455 	.eec_initialized	= sfc_ev_initialized,
456 	.eec_rx			= sfc_ev_nop_rx,
457 	.eec_tx			= sfc_ev_dp_tx,
458 	.eec_exception		= sfc_ev_exception,
459 	.eec_rxq_flush_done	= sfc_ev_nop_rxq_flush_done,
460 	.eec_rxq_flush_failed	= sfc_ev_nop_rxq_flush_failed,
461 	.eec_txq_flush_done	= sfc_ev_txq_flush_done,
462 	.eec_software		= sfc_ev_software,
463 	.eec_sram		= sfc_ev_sram,
464 	.eec_wake_up		= sfc_ev_wake_up,
465 	.eec_timer		= sfc_ev_timer,
466 	.eec_link_change	= sfc_ev_nop_link_change,
467 };
468 
469 
470 void
471 sfc_ev_qpoll(struct sfc_evq *evq)
472 {
473 	SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
474 		   evq->init_state == SFC_EVQ_STARTING);
475 
476 	/* Synchronize the DMA memory for reading not required */
477 
478 	efx_ev_qpoll(evq->common, &evq->read_ptr, evq->callbacks, evq);
479 
480 	if (unlikely(evq->exception) && sfc_adapter_trylock(evq->sa)) {
481 		struct sfc_adapter *sa = evq->sa;
482 		int rc;
483 
484 		if (evq->dp_rxq != NULL) {
485 			unsigned int rxq_sw_index;
486 
487 			rxq_sw_index = evq->dp_rxq->dpq.queue_id;
488 
489 			sfc_warn(sa,
490 				 "restart RxQ %u because of exception on its EvQ %u",
491 				 rxq_sw_index, evq->evq_index);
492 
493 			sfc_rx_qstop(sa, rxq_sw_index);
494 			rc = sfc_rx_qstart(sa, rxq_sw_index);
495 			if (rc != 0)
496 				sfc_err(sa, "cannot restart RxQ %u",
497 					rxq_sw_index);
498 		}
499 
500 		if (evq->dp_txq != NULL) {
501 			unsigned int txq_sw_index;
502 
503 			txq_sw_index = evq->dp_txq->dpq.queue_id;
504 
505 			sfc_warn(sa,
506 				 "restart TxQ %u because of exception on its EvQ %u",
507 				 txq_sw_index, evq->evq_index);
508 
509 			sfc_tx_qstop(sa, txq_sw_index);
510 			rc = sfc_tx_qstart(sa, txq_sw_index);
511 			if (rc != 0)
512 				sfc_err(sa, "cannot restart TxQ %u",
513 					txq_sw_index);
514 		}
515 
516 		if (evq->exception)
517 			sfc_panic(sa, "unrecoverable exception on EvQ %u",
518 				  evq->evq_index);
519 
520 		sfc_adapter_unlock(sa);
521 	}
522 
523 	/* Poll-mode driver does not re-prime the event queue for interrupts */
524 }
525 
526 void
527 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
528 {
529 	if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
530 		if (sa->mgmt_evq_running)
531 			sfc_ev_qpoll(sa->mgmt_evq);
532 
533 		rte_spinlock_unlock(&sa->mgmt_evq_lock);
534 	}
535 }
536 
537 int
538 sfc_ev_qprime(struct sfc_evq *evq)
539 {
540 	SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
541 	return efx_ev_qprime(evq->common, evq->read_ptr);
542 }
543 
544 /* Event queue HW index allocation scheme is described in sfc_ev.h. */
545 int
546 sfc_ev_qstart(struct sfc_evq *evq, unsigned int hw_index)
547 {
548 	struct sfc_adapter *sa = evq->sa;
549 	efsys_mem_t *esmp;
550 	uint32_t evq_flags = sa->evq_flags;
551 	unsigned int total_delay_us;
552 	unsigned int delay_us;
553 	int rc;
554 
555 	sfc_log_init(sa, "hw_index=%u", hw_index);
556 
557 	esmp = &evq->mem;
558 
559 	evq->evq_index = hw_index;
560 
561 	/* Clear all events */
562 	(void)memset((void *)esmp->esm_base, 0xff, EFX_EVQ_SIZE(evq->entries));
563 
564 	if (sa->intr.lsc_intr && hw_index == sa->mgmt_evq_index)
565 		evq_flags |= EFX_EVQ_FLAGS_NOTIFY_INTERRUPT;
566 	else
567 		evq_flags |= EFX_EVQ_FLAGS_NOTIFY_DISABLED;
568 
569 	/* Create the common code event queue */
570 	rc = efx_ev_qcreate(sa->nic, hw_index, esmp, evq->entries,
571 			    0 /* unused on EF10 */, 0, evq_flags,
572 			    &evq->common);
573 	if (rc != 0)
574 		goto fail_ev_qcreate;
575 
576 	SFC_ASSERT(evq->dp_rxq == NULL || evq->dp_txq == NULL);
577 	if (evq->dp_rxq != 0) {
578 		if (strcmp(sa->dp_rx->dp.name, SFC_KVARG_DATAPATH_EFX) == 0)
579 			evq->callbacks = &sfc_ev_callbacks_efx_rx;
580 		else
581 			evq->callbacks = &sfc_ev_callbacks_dp_rx;
582 	} else if (evq->dp_txq != 0) {
583 		if (strcmp(sa->dp_tx->dp.name, SFC_KVARG_DATAPATH_EFX) == 0)
584 			evq->callbacks = &sfc_ev_callbacks_efx_tx;
585 		else
586 			evq->callbacks = &sfc_ev_callbacks_dp_tx;
587 	} else {
588 		evq->callbacks = &sfc_ev_callbacks;
589 	}
590 
591 	evq->init_state = SFC_EVQ_STARTING;
592 
593 	/* Wait for the initialization event */
594 	total_delay_us = 0;
595 	delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
596 	do {
597 		(void)sfc_ev_qpoll(evq);
598 
599 		/* Check to see if the initialization complete indication
600 		 * posted by the hardware.
601 		 */
602 		if (evq->init_state == SFC_EVQ_STARTED)
603 			goto done;
604 
605 		/* Give event queue some time to init */
606 		rte_delay_us(delay_us);
607 
608 		total_delay_us += delay_us;
609 
610 		/* Exponential backoff */
611 		delay_us *= 2;
612 		if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
613 			delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
614 
615 	} while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
616 
617 	rc = ETIMEDOUT;
618 	goto fail_timedout;
619 
620 done:
621 	return 0;
622 
623 fail_timedout:
624 	evq->init_state = SFC_EVQ_INITIALIZED;
625 	efx_ev_qdestroy(evq->common);
626 
627 fail_ev_qcreate:
628 	sfc_log_init(sa, "failed %d", rc);
629 	return rc;
630 }
631 
632 void
633 sfc_ev_qstop(struct sfc_evq *evq)
634 {
635 	if (evq == NULL)
636 		return;
637 
638 	sfc_log_init(evq->sa, "hw_index=%u", evq->evq_index);
639 
640 	if (evq->init_state != SFC_EVQ_STARTED)
641 		return;
642 
643 	evq->init_state = SFC_EVQ_INITIALIZED;
644 	evq->callbacks = NULL;
645 	evq->read_ptr = 0;
646 	evq->exception = B_FALSE;
647 
648 	efx_ev_qdestroy(evq->common);
649 
650 	evq->evq_index = 0;
651 }
652 
653 static void
654 sfc_ev_mgmt_periodic_qpoll(void *arg)
655 {
656 	struct sfc_adapter *sa = arg;
657 	int rc;
658 
659 	sfc_ev_mgmt_qpoll(sa);
660 
661 	rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
662 			       sfc_ev_mgmt_periodic_qpoll, sa);
663 	if (rc == -ENOTSUP) {
664 		sfc_warn(sa, "alarms are not supported");
665 		sfc_warn(sa, "management EVQ must be polled indirectly using no-wait link status update");
666 	} else if (rc != 0) {
667 		sfc_err(sa,
668 			"cannot rearm management EVQ polling alarm (rc=%d)",
669 			rc);
670 	}
671 }
672 
673 static void
674 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
675 {
676 	sfc_ev_mgmt_periodic_qpoll(sa);
677 }
678 
679 static void
680 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
681 {
682 	rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
683 }
684 
685 int
686 sfc_ev_start(struct sfc_adapter *sa)
687 {
688 	int rc;
689 
690 	sfc_log_init(sa, "entry");
691 
692 	rc = efx_ev_init(sa->nic);
693 	if (rc != 0)
694 		goto fail_ev_init;
695 
696 	/* Start management EVQ used for global events */
697 
698 	/*
699 	 * Management event queue start polls the queue, but it cannot
700 	 * interfere with other polling contexts since mgmt_evq_running
701 	 * is false yet.
702 	 */
703 	rc = sfc_ev_qstart(sa->mgmt_evq, sa->mgmt_evq_index);
704 	if (rc != 0)
705 		goto fail_mgmt_evq_start;
706 
707 	rte_spinlock_lock(&sa->mgmt_evq_lock);
708 	sa->mgmt_evq_running = true;
709 	rte_spinlock_unlock(&sa->mgmt_evq_lock);
710 
711 	if (sa->intr.lsc_intr) {
712 		rc = sfc_ev_qprime(sa->mgmt_evq);
713 		if (rc != 0)
714 			goto fail_mgmt_evq_prime;
715 	}
716 
717 	/*
718 	 * Start management EVQ polling. If interrupts are disabled
719 	 * (not used), it is required to process link status change
720 	 * and other device level events to avoid unrecoverable
721 	 * error because the event queue overflow.
722 	 */
723 	sfc_ev_mgmt_periodic_qpoll_start(sa);
724 
725 	/*
726 	 * Rx/Tx event queues are started/stopped when corresponding
727 	 * Rx/Tx queue is started/stopped.
728 	 */
729 
730 	return 0;
731 
732 fail_mgmt_evq_prime:
733 	sfc_ev_qstop(sa->mgmt_evq);
734 
735 fail_mgmt_evq_start:
736 	efx_ev_fini(sa->nic);
737 
738 fail_ev_init:
739 	sfc_log_init(sa, "failed %d", rc);
740 	return rc;
741 }
742 
743 void
744 sfc_ev_stop(struct sfc_adapter *sa)
745 {
746 	sfc_log_init(sa, "entry");
747 
748 	sfc_ev_mgmt_periodic_qpoll_stop(sa);
749 
750 	rte_spinlock_lock(&sa->mgmt_evq_lock);
751 	sa->mgmt_evq_running = false;
752 	rte_spinlock_unlock(&sa->mgmt_evq_lock);
753 
754 	sfc_ev_qstop(sa->mgmt_evq);
755 
756 	efx_ev_fini(sa->nic);
757 }
758 
759 int
760 sfc_ev_qinit(struct sfc_adapter *sa,
761 	     enum sfc_evq_type type, unsigned int type_index,
762 	     unsigned int entries, int socket_id, struct sfc_evq **evqp)
763 {
764 	struct sfc_evq *evq;
765 	int rc;
766 
767 	sfc_log_init(sa, "type=%s type_index=%u",
768 		     sfc_evq_type2str(type), type_index);
769 
770 	SFC_ASSERT(rte_is_power_of_2(entries));
771 
772 	rc = ENOMEM;
773 	evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
774 				 socket_id);
775 	if (evq == NULL)
776 		goto fail_evq_alloc;
777 
778 	evq->sa = sa;
779 	evq->type = type;
780 	evq->entries = entries;
781 
782 	/* Allocate DMA space */
783 	rc = sfc_dma_alloc(sa, sfc_evq_type2str(type), type_index,
784 			   EFX_EVQ_SIZE(evq->entries), socket_id, &evq->mem);
785 	if (rc != 0)
786 		goto fail_dma_alloc;
787 
788 	evq->init_state = SFC_EVQ_INITIALIZED;
789 
790 	sa->evq_count++;
791 
792 	*evqp = evq;
793 
794 	return 0;
795 
796 fail_dma_alloc:
797 	rte_free(evq);
798 
799 fail_evq_alloc:
800 
801 	sfc_log_init(sa, "failed %d", rc);
802 	return rc;
803 }
804 
805 void
806 sfc_ev_qfini(struct sfc_evq *evq)
807 {
808 	struct sfc_adapter *sa = evq->sa;
809 
810 	SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
811 
812 	sfc_dma_free(sa, &evq->mem);
813 
814 	rte_free(evq);
815 
816 	SFC_ASSERT(sa->evq_count > 0);
817 	sa->evq_count--;
818 }
819 
820 static int
821 sfc_kvarg_perf_profile_handler(__rte_unused const char *key,
822 			       const char *value_str, void *opaque)
823 {
824 	uint32_t *value = opaque;
825 
826 	if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_THROUGHPUT) == 0)
827 		*value = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
828 	else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_LOW_LATENCY) == 0)
829 		*value = EFX_EVQ_FLAGS_TYPE_LOW_LATENCY;
830 	else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_AUTO) == 0)
831 		*value = EFX_EVQ_FLAGS_TYPE_AUTO;
832 	else
833 		return -EINVAL;
834 
835 	return 0;
836 }
837 
838 int
839 sfc_ev_attach(struct sfc_adapter *sa)
840 {
841 	int rc;
842 
843 	sfc_log_init(sa, "entry");
844 
845 	sa->evq_flags = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
846 	rc = sfc_kvargs_process(sa, SFC_KVARG_PERF_PROFILE,
847 				sfc_kvarg_perf_profile_handler,
848 				&sa->evq_flags);
849 	if (rc != 0) {
850 		sfc_err(sa, "invalid %s parameter value",
851 			SFC_KVARG_PERF_PROFILE);
852 		goto fail_kvarg_perf_profile;
853 	}
854 
855 	sa->mgmt_evq_index = 0;
856 	rte_spinlock_init(&sa->mgmt_evq_lock);
857 
858 	rc = sfc_ev_qinit(sa, SFC_EVQ_TYPE_MGMT, 0, SFC_MGMT_EVQ_ENTRIES,
859 			  sa->socket_id, &sa->mgmt_evq);
860 	if (rc != 0)
861 		goto fail_mgmt_evq_init;
862 
863 	/*
864 	 * Rx/Tx event queues are created/destroyed when corresponding
865 	 * Rx/Tx queue is created/destroyed.
866 	 */
867 
868 	return 0;
869 
870 fail_mgmt_evq_init:
871 
872 fail_kvarg_perf_profile:
873 	sfc_log_init(sa, "failed %d", rc);
874 	return rc;
875 }
876 
877 void
878 sfc_ev_detach(struct sfc_adapter *sa)
879 {
880 	sfc_log_init(sa, "entry");
881 
882 	sfc_ev_qfini(sa->mgmt_evq);
883 
884 	if (sa->evq_count != 0)
885 		sfc_err(sa, "%u EvQs are not destroyed before detach",
886 			sa->evq_count);
887 }
888