xref: /dpdk/drivers/net/sfc/sfc_ev.c (revision 8e582cf5391a5166521e20f3f7e6e0eba80560e2)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2021 Xilinx, Inc.
4  * Copyright(c) 2016-2019 Solarflare Communications Inc.
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 *
sfc_evq_type2str(enum sfc_evq_type type)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
sfc_ev_initialized(void * arg)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
sfc_ev_nop_rx(void * arg,uint32_t label,uint32_t id,uint32_t size,uint16_t flags)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
sfc_ev_efx_rx(void * arg,__rte_unused uint32_t label,uint32_t id,uint32_t size,uint16_t flags)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
sfc_ev_dp_rx(void * arg,__rte_unused uint32_t label,uint32_t id,__rte_unused uint32_t size,__rte_unused uint16_t flags)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->priv.dp_rx->qrx_ev != NULL);
162 	return evq->sa->priv.dp_rx->qrx_ev(dp_rxq, id);
163 }
164 
165 static boolean_t
sfc_ev_nop_rx_packets(void * arg,uint32_t label,unsigned int num_packets,uint32_t flags)166 sfc_ev_nop_rx_packets(void *arg, uint32_t label, unsigned int num_packets,
167 		      uint32_t flags)
168 {
169 	struct sfc_evq *evq = arg;
170 
171 	sfc_err(evq->sa,
172 		"EVQ %u unexpected Rx packets event label=%u num=%u flags=%#x",
173 		evq->evq_index, label, num_packets, flags);
174 	return B_TRUE;
175 }
176 
177 static boolean_t
sfc_ev_dp_rx_packets(void * arg,__rte_unused uint32_t label,unsigned int num_packets,__rte_unused uint32_t flags)178 sfc_ev_dp_rx_packets(void *arg, __rte_unused uint32_t label,
179 		     unsigned int num_packets, __rte_unused uint32_t flags)
180 {
181 	struct sfc_evq *evq = arg;
182 	struct sfc_dp_rxq *dp_rxq;
183 
184 	dp_rxq = evq->dp_rxq;
185 	SFC_ASSERT(dp_rxq != NULL);
186 
187 	SFC_ASSERT(evq->sa->priv.dp_rx->qrx_ev != NULL);
188 	return evq->sa->priv.dp_rx->qrx_ev(dp_rxq, num_packets);
189 }
190 
191 static boolean_t
sfc_ev_nop_rx_ps(void * arg,uint32_t label,uint32_t id,uint32_t pkt_count,uint16_t flags)192 sfc_ev_nop_rx_ps(void *arg, uint32_t label, uint32_t id,
193 		 uint32_t pkt_count, uint16_t flags)
194 {
195 	struct sfc_evq *evq = arg;
196 
197 	sfc_err(evq->sa,
198 		"EVQ %u unexpected packed stream Rx event label=%u id=%#x pkt_count=%u flags=%#x",
199 		evq->evq_index, label, id, pkt_count, flags);
200 	return B_TRUE;
201 }
202 
203 /* It is not actually used on datapath, but required on RxQ flush */
204 static boolean_t
sfc_ev_dp_rx_ps(void * arg,__rte_unused uint32_t label,uint32_t id,__rte_unused uint32_t pkt_count,__rte_unused uint16_t flags)205 sfc_ev_dp_rx_ps(void *arg, __rte_unused uint32_t label, uint32_t id,
206 		__rte_unused uint32_t pkt_count, __rte_unused uint16_t flags)
207 {
208 	struct sfc_evq *evq = arg;
209 	struct sfc_dp_rxq *dp_rxq;
210 
211 	dp_rxq = evq->dp_rxq;
212 	SFC_ASSERT(dp_rxq != NULL);
213 
214 	if (evq->sa->priv.dp_rx->qrx_ps_ev != NULL)
215 		return evq->sa->priv.dp_rx->qrx_ps_ev(dp_rxq, id);
216 	else
217 		return B_FALSE;
218 }
219 
220 static boolean_t
sfc_ev_nop_tx(void * arg,uint32_t label,uint32_t id)221 sfc_ev_nop_tx(void *arg, uint32_t label, uint32_t id)
222 {
223 	struct sfc_evq *evq = arg;
224 
225 	sfc_err(evq->sa, "EVQ %u unexpected Tx event label=%u id=%#x",
226 		evq->evq_index, label, id);
227 	return B_TRUE;
228 }
229 
230 static boolean_t
sfc_ev_tx(void * arg,__rte_unused uint32_t label,uint32_t id)231 sfc_ev_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
232 {
233 	struct sfc_evq *evq = arg;
234 	struct sfc_dp_txq *dp_txq;
235 	struct sfc_efx_txq *txq;
236 	unsigned int stop;
237 	unsigned int delta;
238 
239 	dp_txq = evq->dp_txq;
240 	SFC_ASSERT(dp_txq != NULL);
241 
242 	txq = sfc_efx_txq_by_dp_txq(dp_txq);
243 	SFC_ASSERT(txq->evq == evq);
244 
245 	if (unlikely((txq->flags & SFC_EFX_TXQ_FLAG_STARTED) == 0))
246 		goto done;
247 
248 	stop = (id + 1) & txq->ptr_mask;
249 	id = txq->pending & txq->ptr_mask;
250 
251 	delta = (stop >= id) ? (stop - id) : (txq->ptr_mask + 1 - id + stop);
252 
253 	txq->pending += delta;
254 
255 done:
256 	return B_FALSE;
257 }
258 
259 static boolean_t
sfc_ev_dp_tx(void * arg,__rte_unused uint32_t label,uint32_t id)260 sfc_ev_dp_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
261 {
262 	struct sfc_evq *evq = arg;
263 	struct sfc_dp_txq *dp_txq;
264 
265 	dp_txq = evq->dp_txq;
266 	SFC_ASSERT(dp_txq != NULL);
267 
268 	SFC_ASSERT(evq->sa->priv.dp_tx->qtx_ev != NULL);
269 	return evq->sa->priv.dp_tx->qtx_ev(dp_txq, id);
270 }
271 
272 static boolean_t
sfc_ev_nop_tx_ndescs(void * arg,uint32_t label,unsigned int ndescs)273 sfc_ev_nop_tx_ndescs(void *arg, uint32_t label, unsigned int ndescs)
274 {
275 	struct sfc_evq *evq = arg;
276 
277 	sfc_err(evq->sa, "EVQ %u unexpected Tx event label=%u ndescs=%#x",
278 		evq->evq_index, label, ndescs);
279 	return B_TRUE;
280 }
281 
282 static boolean_t
sfc_ev_dp_tx_ndescs(void * arg,__rte_unused uint32_t label,unsigned int ndescs)283 sfc_ev_dp_tx_ndescs(void *arg, __rte_unused uint32_t label,
284 		      unsigned int ndescs)
285 {
286 	struct sfc_evq *evq = arg;
287 	struct sfc_dp_txq *dp_txq;
288 
289 	dp_txq = evq->dp_txq;
290 	SFC_ASSERT(dp_txq != NULL);
291 
292 	SFC_ASSERT(evq->sa->priv.dp_tx->qtx_ev != NULL);
293 	return evq->sa->priv.dp_tx->qtx_ev(dp_txq, ndescs);
294 }
295 
296 static boolean_t
sfc_ev_exception(void * arg,uint32_t code,__rte_unused uint32_t data)297 sfc_ev_exception(void *arg, uint32_t code, __rte_unused uint32_t data)
298 {
299 	struct sfc_evq *evq = arg;
300 
301 	if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
302 		return B_FALSE;
303 
304 	evq->exception = B_TRUE;
305 	sfc_warn(evq->sa,
306 		 "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
307 		 " needs recovery",
308 		 (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
309 		 (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
310 		 (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
311 		 (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
312 		 (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
313 		 (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
314 		 (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
315 		 (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
316 		 "UNKNOWN",
317 		 code, data, evq->evq_index);
318 
319 	return B_TRUE;
320 }
321 
322 static boolean_t
sfc_ev_nop_rxq_flush_done(void * arg,uint32_t rxq_hw_index)323 sfc_ev_nop_rxq_flush_done(void *arg, uint32_t rxq_hw_index)
324 {
325 	struct sfc_evq *evq = arg;
326 
327 	sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush done",
328 		evq->evq_index, rxq_hw_index);
329 	return B_TRUE;
330 }
331 
332 static boolean_t
sfc_ev_rxq_flush_done(void * arg,__rte_unused uint32_t rxq_hw_index)333 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
334 {
335 	struct sfc_evq *evq = arg;
336 	struct sfc_dp_rxq *dp_rxq;
337 	struct sfc_rxq *rxq;
338 
339 	dp_rxq = evq->dp_rxq;
340 	SFC_ASSERT(dp_rxq != NULL);
341 
342 	rxq = sfc_rxq_by_dp_rxq(dp_rxq);
343 	SFC_ASSERT(rxq != NULL);
344 	SFC_ASSERT(rxq->hw_index == rxq_hw_index);
345 	SFC_ASSERT(rxq->evq == evq);
346 	RTE_SET_USED(rxq);
347 
348 	sfc_rx_qflush_done(sfc_rxq_info_by_dp_rxq(dp_rxq));
349 
350 	return B_FALSE;
351 }
352 
353 static boolean_t
sfc_ev_nop_rxq_flush_failed(void * arg,uint32_t rxq_hw_index)354 sfc_ev_nop_rxq_flush_failed(void *arg, uint32_t rxq_hw_index)
355 {
356 	struct sfc_evq *evq = arg;
357 
358 	sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush failed",
359 		evq->evq_index, rxq_hw_index);
360 	return B_TRUE;
361 }
362 
363 static boolean_t
sfc_ev_rxq_flush_failed(void * arg,__rte_unused uint32_t rxq_hw_index)364 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
365 {
366 	struct sfc_evq *evq = arg;
367 	struct sfc_dp_rxq *dp_rxq;
368 	struct sfc_rxq *rxq;
369 
370 	dp_rxq = evq->dp_rxq;
371 	SFC_ASSERT(dp_rxq != NULL);
372 
373 	rxq = sfc_rxq_by_dp_rxq(dp_rxq);
374 	SFC_ASSERT(rxq != NULL);
375 	SFC_ASSERT(rxq->hw_index == rxq_hw_index);
376 	SFC_ASSERT(rxq->evq == evq);
377 	RTE_SET_USED(rxq);
378 
379 	sfc_rx_qflush_failed(sfc_rxq_info_by_dp_rxq(dp_rxq));
380 
381 	return B_FALSE;
382 }
383 
384 static boolean_t
sfc_ev_nop_txq_flush_done(void * arg,uint32_t txq_hw_index)385 sfc_ev_nop_txq_flush_done(void *arg, uint32_t txq_hw_index)
386 {
387 	struct sfc_evq *evq = arg;
388 
389 	sfc_err(evq->sa, "EVQ %u unexpected TxQ %u flush done",
390 		evq->evq_index, txq_hw_index);
391 	return B_TRUE;
392 }
393 
394 static boolean_t
sfc_ev_txq_flush_done(void * arg,__rte_unused uint32_t txq_hw_index)395 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
396 {
397 	struct sfc_evq *evq = arg;
398 	struct sfc_dp_txq *dp_txq;
399 	struct sfc_txq *txq;
400 
401 	dp_txq = evq->dp_txq;
402 	SFC_ASSERT(dp_txq != NULL);
403 
404 	txq = sfc_txq_by_dp_txq(dp_txq);
405 	SFC_ASSERT(txq != NULL);
406 	SFC_ASSERT(txq->hw_index == txq_hw_index);
407 	SFC_ASSERT(txq->evq == evq);
408 	RTE_SET_USED(txq);
409 
410 	sfc_tx_qflush_done(sfc_txq_info_by_dp_txq(dp_txq));
411 
412 	return B_FALSE;
413 }
414 
415 static boolean_t
sfc_ev_software(void * arg,uint16_t magic)416 sfc_ev_software(void *arg, uint16_t magic)
417 {
418 	struct sfc_evq *evq = arg;
419 
420 	sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
421 		evq->evq_index, magic);
422 	return B_TRUE;
423 }
424 
425 static boolean_t
sfc_ev_sram(void * arg,uint32_t code)426 sfc_ev_sram(void *arg, uint32_t code)
427 {
428 	struct sfc_evq *evq = arg;
429 
430 	sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
431 		evq->evq_index, code);
432 	return B_TRUE;
433 }
434 
435 static boolean_t
sfc_ev_wake_up(void * arg,uint32_t index)436 sfc_ev_wake_up(void *arg, uint32_t index)
437 {
438 	struct sfc_evq *evq = arg;
439 
440 	sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
441 		evq->evq_index, index);
442 	return B_TRUE;
443 }
444 
445 static boolean_t
sfc_ev_timer(void * arg,uint32_t index)446 sfc_ev_timer(void *arg, uint32_t index)
447 {
448 	struct sfc_evq *evq = arg;
449 
450 	sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
451 		evq->evq_index, index);
452 	return B_TRUE;
453 }
454 
455 static boolean_t
sfc_ev_nop_link_change(void * arg,__rte_unused efx_link_mode_t link_mode)456 sfc_ev_nop_link_change(void *arg, __rte_unused efx_link_mode_t link_mode)
457 {
458 	struct sfc_evq *evq = arg;
459 
460 	sfc_err(evq->sa, "EVQ %u unexpected link change event",
461 		evq->evq_index);
462 	return B_TRUE;
463 }
464 
465 static boolean_t
sfc_ev_link_change(void * arg,efx_link_mode_t link_mode)466 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
467 {
468 	struct sfc_evq *evq = arg;
469 	struct sfc_adapter *sa = evq->sa;
470 	struct rte_eth_link new_link;
471 
472 	sfc_port_link_mode_to_info(link_mode, &new_link);
473 	if (rte_eth_linkstatus_set(sa->eth_dev, &new_link) == 0)
474 		evq->sa->port.lsc_seq++;
475 
476 	return B_FALSE;
477 }
478 
479 static const efx_ev_callbacks_t sfc_ev_callbacks = {
480 	.eec_initialized	= sfc_ev_initialized,
481 	.eec_rx			= sfc_ev_nop_rx,
482 	.eec_rx_packets		= sfc_ev_nop_rx_packets,
483 	.eec_rx_ps		= sfc_ev_nop_rx_ps,
484 	.eec_tx			= sfc_ev_nop_tx,
485 	.eec_tx_ndescs		= sfc_ev_nop_tx_ndescs,
486 	.eec_exception		= sfc_ev_exception,
487 	.eec_rxq_flush_done	= sfc_ev_nop_rxq_flush_done,
488 	.eec_rxq_flush_failed	= sfc_ev_nop_rxq_flush_failed,
489 	.eec_txq_flush_done	= sfc_ev_nop_txq_flush_done,
490 	.eec_software		= sfc_ev_software,
491 	.eec_sram		= sfc_ev_sram,
492 	.eec_wake_up		= sfc_ev_wake_up,
493 	.eec_timer		= sfc_ev_timer,
494 	.eec_link_change	= sfc_ev_link_change,
495 };
496 
497 static const efx_ev_callbacks_t sfc_ev_callbacks_efx_rx = {
498 	.eec_initialized	= sfc_ev_initialized,
499 	.eec_rx			= sfc_ev_efx_rx,
500 	.eec_rx_packets		= sfc_ev_nop_rx_packets,
501 	.eec_rx_ps		= sfc_ev_nop_rx_ps,
502 	.eec_tx			= sfc_ev_nop_tx,
503 	.eec_tx_ndescs		= sfc_ev_nop_tx_ndescs,
504 	.eec_exception		= sfc_ev_exception,
505 	.eec_rxq_flush_done	= sfc_ev_rxq_flush_done,
506 	.eec_rxq_flush_failed	= sfc_ev_rxq_flush_failed,
507 	.eec_txq_flush_done	= sfc_ev_nop_txq_flush_done,
508 	.eec_software		= sfc_ev_software,
509 	.eec_sram		= sfc_ev_sram,
510 	.eec_wake_up		= sfc_ev_wake_up,
511 	.eec_timer		= sfc_ev_timer,
512 	.eec_link_change	= sfc_ev_nop_link_change,
513 };
514 
515 static const efx_ev_callbacks_t sfc_ev_callbacks_dp_rx = {
516 	.eec_initialized	= sfc_ev_initialized,
517 	.eec_rx			= sfc_ev_dp_rx,
518 	.eec_rx_packets		= sfc_ev_dp_rx_packets,
519 	.eec_rx_ps		= sfc_ev_dp_rx_ps,
520 	.eec_tx			= sfc_ev_nop_tx,
521 	.eec_tx_ndescs		= sfc_ev_nop_tx_ndescs,
522 	.eec_exception		= sfc_ev_exception,
523 	.eec_rxq_flush_done	= sfc_ev_rxq_flush_done,
524 	.eec_rxq_flush_failed	= sfc_ev_rxq_flush_failed,
525 	.eec_txq_flush_done	= sfc_ev_nop_txq_flush_done,
526 	.eec_software		= sfc_ev_software,
527 	.eec_sram		= sfc_ev_sram,
528 	.eec_wake_up		= sfc_ev_wake_up,
529 	.eec_timer		= sfc_ev_timer,
530 	.eec_link_change	= sfc_ev_nop_link_change,
531 };
532 
533 static const efx_ev_callbacks_t sfc_ev_callbacks_efx_tx = {
534 	.eec_initialized	= sfc_ev_initialized,
535 	.eec_rx			= sfc_ev_nop_rx,
536 	.eec_rx_packets		= sfc_ev_nop_rx_packets,
537 	.eec_rx_ps		= sfc_ev_nop_rx_ps,
538 	.eec_tx			= sfc_ev_tx,
539 	.eec_tx_ndescs		= sfc_ev_nop_tx_ndescs,
540 	.eec_exception		= sfc_ev_exception,
541 	.eec_rxq_flush_done	= sfc_ev_nop_rxq_flush_done,
542 	.eec_rxq_flush_failed	= sfc_ev_nop_rxq_flush_failed,
543 	.eec_txq_flush_done	= sfc_ev_txq_flush_done,
544 	.eec_software		= sfc_ev_software,
545 	.eec_sram		= sfc_ev_sram,
546 	.eec_wake_up		= sfc_ev_wake_up,
547 	.eec_timer		= sfc_ev_timer,
548 	.eec_link_change	= sfc_ev_nop_link_change,
549 };
550 
551 static const efx_ev_callbacks_t sfc_ev_callbacks_dp_tx = {
552 	.eec_initialized	= sfc_ev_initialized,
553 	.eec_rx			= sfc_ev_nop_rx,
554 	.eec_rx_packets		= sfc_ev_nop_rx_packets,
555 	.eec_rx_ps		= sfc_ev_nop_rx_ps,
556 	.eec_tx			= sfc_ev_dp_tx,
557 	.eec_tx_ndescs		= sfc_ev_dp_tx_ndescs,
558 	.eec_exception		= sfc_ev_exception,
559 	.eec_rxq_flush_done	= sfc_ev_nop_rxq_flush_done,
560 	.eec_rxq_flush_failed	= sfc_ev_nop_rxq_flush_failed,
561 	.eec_txq_flush_done	= sfc_ev_txq_flush_done,
562 	.eec_software		= sfc_ev_software,
563 	.eec_sram		= sfc_ev_sram,
564 	.eec_wake_up		= sfc_ev_wake_up,
565 	.eec_timer		= sfc_ev_timer,
566 	.eec_link_change	= sfc_ev_nop_link_change,
567 };
568 
569 
570 void
sfc_ev_qpoll(struct sfc_evq * evq)571 sfc_ev_qpoll(struct sfc_evq *evq)
572 {
573 	struct sfc_adapter *sa;
574 
575 	SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
576 		   evq->init_state == SFC_EVQ_STARTING);
577 
578 	/* Synchronize the DMA memory for reading not required */
579 
580 	efx_ev_qpoll(evq->common, &evq->read_ptr, evq->callbacks, evq);
581 
582 	sa = evq->sa;
583 	if (unlikely(evq->exception) && sfc_adapter_trylock(sa)) {
584 		int rc;
585 
586 		if (evq->dp_rxq != NULL) {
587 			sfc_sw_index_t rxq_sw_index;
588 
589 			rxq_sw_index = evq->dp_rxq->dpq.queue_id;
590 
591 			sfc_warn(sa,
592 				 "restart RxQ %u because of exception on its EvQ %u",
593 				 rxq_sw_index, evq->evq_index);
594 
595 			sfc_rx_qstop(sa, rxq_sw_index);
596 			rc = sfc_rx_qstart(sa, rxq_sw_index);
597 			if (rc != 0)
598 				sfc_err(sa, "cannot restart RxQ %u",
599 					rxq_sw_index);
600 		}
601 
602 		if (evq->dp_txq != NULL) {
603 			sfc_sw_index_t txq_sw_index;
604 
605 			txq_sw_index = evq->dp_txq->dpq.queue_id;
606 
607 			sfc_warn(sa,
608 				 "restart TxQ %u because of exception on its EvQ %u",
609 				 txq_sw_index, evq->evq_index);
610 
611 			sfc_tx_qstop(sa, txq_sw_index);
612 			rc = sfc_tx_qstart(sa, txq_sw_index);
613 			if (rc != 0)
614 				sfc_err(sa, "cannot restart TxQ %u",
615 					txq_sw_index);
616 		}
617 
618 		if (evq->exception)
619 			sfc_panic(sa, "unrecoverable exception on EvQ %u",
620 				  evq->evq_index);
621 
622 		sfc_adapter_unlock(sa);
623 	}
624 
625 	/* Poll-mode driver does not re-prime the event queue for interrupts */
626 }
627 
628 void
sfc_ev_mgmt_qpoll(struct sfc_adapter * sa)629 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
630 {
631 	if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
632 		if (sa->mgmt_evq_running)
633 			sfc_ev_qpoll(sa->mgmt_evq);
634 
635 		rte_spinlock_unlock(&sa->mgmt_evq_lock);
636 	}
637 }
638 
639 int
sfc_ev_qprime(struct sfc_evq * evq)640 sfc_ev_qprime(struct sfc_evq *evq)
641 {
642 	SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
643 	return efx_ev_qprime(evq->common, evq->read_ptr);
644 }
645 
646 /* Event queue HW index allocation scheme is described in sfc_ev.h. */
647 int
sfc_ev_qstart(struct sfc_evq * evq,unsigned int hw_index)648 sfc_ev_qstart(struct sfc_evq *evq, unsigned int hw_index)
649 {
650 	struct sfc_adapter *sa = evq->sa;
651 	efsys_mem_t *esmp;
652 	uint32_t evq_flags = sa->evq_flags;
653 	uint32_t irq = 0;
654 	unsigned int total_delay_us;
655 	unsigned int delay_us;
656 	int rc;
657 
658 	sfc_log_init(sa, "hw_index=%u", hw_index);
659 
660 	esmp = &evq->mem;
661 
662 	evq->evq_index = hw_index;
663 
664 	/* Clear all events */
665 	(void)memset((void *)esmp->esm_base, 0xff,
666 		     efx_evq_size(sa->nic, evq->entries, evq_flags));
667 
668 	if (sa->intr.lsc_intr && hw_index == sa->mgmt_evq_index) {
669 		evq_flags |= EFX_EVQ_FLAGS_NOTIFY_INTERRUPT;
670 		irq = 0;
671 	} else if (sa->intr.rxq_intr && evq->dp_rxq != NULL) {
672 		sfc_ethdev_qid_t ethdev_qid;
673 
674 		ethdev_qid =
675 			sfc_ethdev_rx_qid_by_rxq_sw_index(sfc_sa2shared(sa),
676 				evq->dp_rxq->dpq.queue_id);
677 		if (ethdev_qid != SFC_ETHDEV_QID_INVALID) {
678 			evq_flags |= EFX_EVQ_FLAGS_NOTIFY_INTERRUPT;
679 			/*
680 			 * The first interrupt is used for management EvQ
681 			 * (LSC etc). RxQ interrupts follow it.
682 			 */
683 			irq = 1 + ethdev_qid;
684 		} else {
685 			evq_flags |= EFX_EVQ_FLAGS_NOTIFY_DISABLED;
686 		}
687 	} else {
688 		evq_flags |= EFX_EVQ_FLAGS_NOTIFY_DISABLED;
689 	}
690 
691 	evq->init_state = SFC_EVQ_STARTING;
692 
693 	/* Create the common code event queue */
694 	rc = efx_ev_qcreate_irq(sa->nic, hw_index, esmp, evq->entries,
695 				0 /* unused on EF10 */, 0, evq_flags,
696 				irq, &evq->common);
697 	if (rc != 0)
698 		goto fail_ev_qcreate;
699 
700 	SFC_ASSERT(evq->dp_rxq == NULL || evq->dp_txq == NULL);
701 	if (evq->dp_rxq != 0) {
702 		if (strcmp(sa->priv.dp_rx->dp.name,
703 			   SFC_KVARG_DATAPATH_EFX) == 0)
704 			evq->callbacks = &sfc_ev_callbacks_efx_rx;
705 		else
706 			evq->callbacks = &sfc_ev_callbacks_dp_rx;
707 	} else if (evq->dp_txq != 0) {
708 		if (strcmp(sa->priv.dp_tx->dp.name,
709 			   SFC_KVARG_DATAPATH_EFX) == 0)
710 			evq->callbacks = &sfc_ev_callbacks_efx_tx;
711 		else
712 			evq->callbacks = &sfc_ev_callbacks_dp_tx;
713 	} else {
714 		evq->callbacks = &sfc_ev_callbacks;
715 	}
716 
717 	/*
718 	 * Poll once to ensure that eec_initialized callback is invoked in
719 	 * case if the hardware does not support INIT_DONE events. If the
720 	 * hardware supports INIT_DONE events, this will do nothing, and the
721 	 * corresponding event will be processed by sfc_ev_qpoll() below.
722 	 */
723 	efx_ev_qcreate_check_init_done(evq->common, evq->callbacks, evq);
724 
725 	/* Wait for the initialization event */
726 	total_delay_us = 0;
727 	delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
728 	do {
729 		(void)sfc_ev_qpoll(evq);
730 
731 		/* Check to see if the initialization complete indication
732 		 * posted by the hardware.
733 		 */
734 		if (evq->init_state == SFC_EVQ_STARTED)
735 			goto done;
736 
737 		/* Give event queue some time to init */
738 		rte_delay_us(delay_us);
739 
740 		total_delay_us += delay_us;
741 
742 		/* Exponential backoff */
743 		delay_us *= 2;
744 		if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
745 			delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
746 
747 	} while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
748 
749 	rc = ETIMEDOUT;
750 	goto fail_timedout;
751 
752 done:
753 	return 0;
754 
755 fail_timedout:
756 	efx_ev_qdestroy(evq->common);
757 
758 fail_ev_qcreate:
759 	evq->init_state = SFC_EVQ_INITIALIZED;
760 	sfc_log_init(sa, "failed %d", rc);
761 	return rc;
762 }
763 
764 void
sfc_ev_qstop(struct sfc_evq * evq)765 sfc_ev_qstop(struct sfc_evq *evq)
766 {
767 	if (evq == NULL)
768 		return;
769 
770 	sfc_log_init(evq->sa, "hw_index=%u", evq->evq_index);
771 
772 	if (evq->init_state != SFC_EVQ_STARTED)
773 		return;
774 
775 	evq->init_state = SFC_EVQ_INITIALIZED;
776 	evq->callbacks = NULL;
777 	evq->read_ptr = 0;
778 	evq->exception = B_FALSE;
779 
780 	efx_ev_qdestroy(evq->common);
781 
782 	evq->evq_index = 0;
783 }
784 
785 static void
sfc_ev_mgmt_periodic_qpoll(void * arg)786 sfc_ev_mgmt_periodic_qpoll(void *arg)
787 {
788 	struct sfc_adapter *sa = arg;
789 	int rc;
790 
791 	sfc_ev_mgmt_qpoll(sa);
792 
793 	rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
794 			       sfc_ev_mgmt_periodic_qpoll, sa);
795 	if (rc == -ENOTSUP) {
796 		sfc_warn(sa, "alarms are not supported");
797 		sfc_warn(sa, "management EVQ must be polled indirectly using no-wait link status update");
798 	} else if (rc != 0) {
799 		sfc_err(sa,
800 			"cannot rearm management EVQ polling alarm (rc=%d)",
801 			rc);
802 	}
803 }
804 
805 static void
sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter * sa)806 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
807 {
808 	sfc_ev_mgmt_periodic_qpoll(sa);
809 }
810 
811 static void
sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter * sa)812 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
813 {
814 	rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
815 }
816 
817 int
sfc_ev_start(struct sfc_adapter * sa)818 sfc_ev_start(struct sfc_adapter *sa)
819 {
820 	int rc;
821 
822 	sfc_log_init(sa, "entry");
823 
824 	rc = efx_ev_init(sa->nic);
825 	if (rc != 0)
826 		goto fail_ev_init;
827 
828 	/* Start management EVQ used for global events */
829 
830 	/*
831 	 * Management event queue start polls the queue, but it cannot
832 	 * interfere with other polling contexts since mgmt_evq_running
833 	 * is false yet.
834 	 */
835 	rc = sfc_ev_qstart(sa->mgmt_evq, sa->mgmt_evq_index);
836 	if (rc != 0)
837 		goto fail_mgmt_evq_start;
838 
839 	rte_spinlock_lock(&sa->mgmt_evq_lock);
840 	sa->mgmt_evq_running = true;
841 	rte_spinlock_unlock(&sa->mgmt_evq_lock);
842 
843 	if (sa->intr.lsc_intr) {
844 		rc = sfc_ev_qprime(sa->mgmt_evq);
845 		if (rc != 0)
846 			goto fail_mgmt_evq_prime;
847 	}
848 
849 	/*
850 	 * Start management EVQ polling. If interrupts are disabled
851 	 * (not used), it is required to process link status change
852 	 * and other device level events to avoid unrecoverable
853 	 * error because the event queue overflow.
854 	 */
855 	sfc_ev_mgmt_periodic_qpoll_start(sa);
856 
857 	/*
858 	 * Rx/Tx event queues are started/stopped when corresponding
859 	 * Rx/Tx queue is started/stopped.
860 	 */
861 
862 	return 0;
863 
864 fail_mgmt_evq_prime:
865 	sfc_ev_qstop(sa->mgmt_evq);
866 
867 fail_mgmt_evq_start:
868 	efx_ev_fini(sa->nic);
869 
870 fail_ev_init:
871 	sfc_log_init(sa, "failed %d", rc);
872 	return rc;
873 }
874 
875 void
sfc_ev_stop(struct sfc_adapter * sa)876 sfc_ev_stop(struct sfc_adapter *sa)
877 {
878 	sfc_log_init(sa, "entry");
879 
880 	sfc_ev_mgmt_periodic_qpoll_stop(sa);
881 
882 	rte_spinlock_lock(&sa->mgmt_evq_lock);
883 	sa->mgmt_evq_running = false;
884 	rte_spinlock_unlock(&sa->mgmt_evq_lock);
885 
886 	sfc_ev_qstop(sa->mgmt_evq);
887 
888 	efx_ev_fini(sa->nic);
889 }
890 
891 int
sfc_ev_qinit(struct sfc_adapter * sa,enum sfc_evq_type type,unsigned int type_index,unsigned int entries,int socket_id,struct sfc_evq ** evqp)892 sfc_ev_qinit(struct sfc_adapter *sa,
893 	     enum sfc_evq_type type, unsigned int type_index,
894 	     unsigned int entries, int socket_id, struct sfc_evq **evqp)
895 {
896 	struct sfc_evq *evq;
897 	int rc;
898 
899 	sfc_log_init(sa, "type=%s type_index=%u",
900 		     sfc_evq_type2str(type), type_index);
901 
902 	SFC_ASSERT(rte_is_power_of_2(entries));
903 
904 	rc = ENOMEM;
905 	evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
906 				 socket_id);
907 	if (evq == NULL)
908 		goto fail_evq_alloc;
909 
910 	evq->sa = sa;
911 	evq->type = type;
912 	evq->entries = entries;
913 
914 	/* Allocate DMA space */
915 	rc = sfc_dma_alloc(sa, sfc_evq_type2str(type), type_index,
916 			   EFX_NIC_DMA_ADDR_EVENT_RING,
917 			   efx_evq_size(sa->nic, evq->entries, sa->evq_flags),
918 			   socket_id, &evq->mem);
919 	if (rc != 0)
920 		goto fail_dma_alloc;
921 
922 	evq->init_state = SFC_EVQ_INITIALIZED;
923 
924 	sa->evq_count++;
925 
926 	*evqp = evq;
927 
928 	return 0;
929 
930 fail_dma_alloc:
931 	rte_free(evq);
932 
933 fail_evq_alloc:
934 
935 	sfc_log_init(sa, "failed %d", rc);
936 	return rc;
937 }
938 
939 void
sfc_ev_qfini(struct sfc_evq * evq)940 sfc_ev_qfini(struct sfc_evq *evq)
941 {
942 	struct sfc_adapter *sa = evq->sa;
943 
944 	SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
945 
946 	sfc_dma_free(sa, &evq->mem);
947 
948 	rte_free(evq);
949 
950 	SFC_ASSERT(sa->evq_count > 0);
951 	sa->evq_count--;
952 }
953 
954 static int
sfc_kvarg_perf_profile_handler(__rte_unused const char * key,const char * value_str,void * opaque)955 sfc_kvarg_perf_profile_handler(__rte_unused const char *key,
956 			       const char *value_str, void *opaque)
957 {
958 	uint32_t *value = opaque;
959 
960 	if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_THROUGHPUT) == 0)
961 		*value = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
962 	else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_LOW_LATENCY) == 0)
963 		*value = EFX_EVQ_FLAGS_TYPE_LOW_LATENCY;
964 	else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_AUTO) == 0)
965 		*value = EFX_EVQ_FLAGS_TYPE_AUTO;
966 	else
967 		return -EINVAL;
968 
969 	return 0;
970 }
971 
972 int
sfc_ev_attach(struct sfc_adapter * sa)973 sfc_ev_attach(struct sfc_adapter *sa)
974 {
975 	int rc;
976 
977 	sfc_log_init(sa, "entry");
978 
979 	sa->evq_flags = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
980 	rc = sfc_kvargs_process(sa, SFC_KVARG_PERF_PROFILE,
981 				sfc_kvarg_perf_profile_handler,
982 				&sa->evq_flags);
983 	if (rc != 0) {
984 		sfc_err(sa, "invalid %s parameter value",
985 			SFC_KVARG_PERF_PROFILE);
986 		goto fail_kvarg_perf_profile;
987 	}
988 
989 	sa->mgmt_evq_index = sfc_mgmt_evq_sw_index(sfc_sa2shared(sa));
990 	rte_spinlock_init(&sa->mgmt_evq_lock);
991 
992 	rc = sfc_ev_qinit(sa, SFC_EVQ_TYPE_MGMT, 0, sa->evq_min_entries,
993 			  sa->socket_id, &sa->mgmt_evq);
994 	if (rc != 0)
995 		goto fail_mgmt_evq_init;
996 
997 	/*
998 	 * Rx/Tx event queues are created/destroyed when corresponding
999 	 * Rx/Tx queue is created/destroyed.
1000 	 */
1001 
1002 	return 0;
1003 
1004 fail_mgmt_evq_init:
1005 
1006 fail_kvarg_perf_profile:
1007 	sfc_log_init(sa, "failed %d", rc);
1008 	return rc;
1009 }
1010 
1011 void
sfc_ev_detach(struct sfc_adapter * sa)1012 sfc_ev_detach(struct sfc_adapter *sa)
1013 {
1014 	sfc_log_init(sa, "entry");
1015 
1016 	sfc_ev_qfini(sa->mgmt_evq);
1017 
1018 	if (sa->evq_count != 0)
1019 		sfc_err(sa, "%u EvQs are not destroyed before detach",
1020 			sa->evq_count);
1021 }
1022