efx_ev.c revision 300607
1208747Sraj/*-
2208747Sraj * Copyright (c) 2007-2016 Solarflare Communications Inc.
3208747Sraj * All rights reserved.
4208747Sraj *
5208747Sraj * Redistribution and use in source and binary forms, with or without
6208747Sraj * modification, are permitted provided that the following conditions are met:
7208747Sraj *
8208747Sraj * 1. Redistributions of source code must retain the above copyright notice,
9208747Sraj *    this list of conditions and the following disclaimer.
10208747Sraj * 2. Redistributions in binary form must reproduce the above copyright notice,
11208747Sraj *    this list of conditions and the following disclaimer in the documentation
12208747Sraj *    and/or other materials provided with the distribution.
13208747Sraj *
14208747Sraj * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15208747Sraj * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16208747Sraj * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17208747Sraj * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18208747Sraj * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19208747Sraj * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20208747Sraj * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21208747Sraj * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22208747Sraj * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23208747Sraj * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24208747Sraj * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25208747Sraj *
26208747Sraj * The views and conclusions contained in the software and documentation are
27208747Sraj * those of the authors and should not be interpreted as representing official
28208747Sraj * policies, either expressed or implied, of the FreeBSD Project.
29208747Sraj */
30208747Sraj
31208747Sraj#include <sys/cdefs.h>
32208747Sraj__FBSDID("$FreeBSD: head/sys/dev/sfxge/common/efx_ev.c 300607 2016-05-24 12:16:57Z arybchik $");
33208747Sraj
34208747Sraj#include "efx.h"
35208747Sraj#include "efx_impl.h"
36208747Sraj#if EFSYS_OPT_MON_MCDI
37208747Sraj#include "mcdi_mon.h"
38208747Sraj#endif
39208747Sraj
40208747Sraj#if EFSYS_OPT_QSTATS
41208747Sraj#define	EFX_EV_QSTAT_INCR(_eep, _stat)					\
42218077Smarcel	do {								\
43208747Sraj		(_eep)->ee_stat[_stat]++;				\
44208747Sraj	_NOTE(CONSTANTCONDITION)					\
45208747Sraj	} while (B_FALSE)
46208747Sraj#else
47218077Smarcel#define	EFX_EV_QSTAT_INCR(_eep, _stat)
48208747Sraj#endif
49208747Sraj
50208747Sraj#define	EFX_EV_PRESENT(_qword)						\
51208747Sraj	(EFX_QWORD_FIELD((_qword), EFX_DWORD_0) != 0xffffffff &&	\
52208747Sraj	EFX_QWORD_FIELD((_qword), EFX_DWORD_1) != 0xffffffff)
53208747Sraj
54208747Sraj
55208747Sraj
56208747Sraj#if EFSYS_OPT_SIENA
57208747Sraj
58208747Srajstatic	__checkReturn	efx_rc_t
59208747Srajsiena_ev_init(
60208747Sraj	__in		efx_nic_t *enp);
61208747Sraj
62208747Srajstatic			void
63208747Srajsiena_ev_fini(
64208747Sraj	__in		efx_nic_t *enp);
65208747Sraj
66208747Srajstatic	__checkReturn	efx_rc_t
67208747Srajsiena_ev_qcreate(
68208747Sraj	__in		efx_nic_t *enp,
69208747Sraj	__in		unsigned int index,
70208747Sraj	__in		efsys_mem_t *esmp,
71208747Sraj	__in		size_t n,
72208747Sraj	__in		uint32_t id,
73208747Sraj	__in		efx_evq_t *eep);
74208747Sraj
75208747Srajstatic			void
76208747Srajsiena_ev_qdestroy(
77208747Sraj	__in		efx_evq_t *eep);
78208747Sraj
79208747Srajstatic	__checkReturn	efx_rc_t
80208747Srajsiena_ev_qprime(
81208747Sraj	__in		efx_evq_t *eep,
82208747Sraj	__in		unsigned int count);
83208747Sraj
84208747Srajstatic			void
85208747Srajsiena_ev_qpoll(
86208747Sraj	__in		efx_evq_t *eep,
87208747Sraj	__inout		unsigned int *countp,
88208747Sraj	__in		const efx_ev_callbacks_t *eecp,
89208747Sraj	__in_opt	void *arg);
90208747Sraj
91208747Srajstatic			void
92208747Srajsiena_ev_qpost(
93208747Sraj	__in	efx_evq_t *eep,
94208747Sraj	__in	uint16_t data);
95208747Sraj
96208747Srajstatic	__checkReturn	efx_rc_t
97208747Srajsiena_ev_qmoderate(
98208747Sraj	__in		efx_evq_t *eep,
99208747Sraj	__in		unsigned int us);
100208747Sraj
101208747Sraj#if EFSYS_OPT_QSTATS
102208747Srajstatic			void
103208747Srajsiena_ev_qstats_update(
104208747Sraj	__in				efx_evq_t *eep,
105208747Sraj	__inout_ecount(EV_NQSTATS)	efsys_stat_t *stat);
106208747Sraj
107208747Sraj#endif
108208747Sraj
109208747Sraj#endif /* EFSYS_OPT_SIENA */
110208747Sraj
111208747Sraj#if EFSYS_OPT_SIENA
112208747Srajstatic const efx_ev_ops_t	__efx_ev_siena_ops = {
113208747Sraj	siena_ev_init,				/* eevo_init */
114208747Sraj	siena_ev_fini,				/* eevo_fini */
115208747Sraj	siena_ev_qcreate,			/* eevo_qcreate */
116208747Sraj	siena_ev_qdestroy,			/* eevo_qdestroy */
117208747Sraj	siena_ev_qprime,			/* eevo_qprime */
118208747Sraj	siena_ev_qpost,				/* eevo_qpost */
119208747Sraj	siena_ev_qmoderate,			/* eevo_qmoderate */
120208747Sraj#if EFSYS_OPT_QSTATS
121208747Sraj	siena_ev_qstats_update,			/* eevo_qstats_update */
122208747Sraj#endif
123208747Sraj};
124208747Sraj#endif /* EFSYS_OPT_SIENA */
125208747Sraj
126208747Sraj#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
127208747Srajstatic const efx_ev_ops_t	__efx_ev_ef10_ops = {
128208747Sraj	ef10_ev_init,				/* eevo_init */
129208747Sraj	ef10_ev_fini,				/* eevo_fini */
130208747Sraj	ef10_ev_qcreate,			/* eevo_qcreate */
131208747Sraj	ef10_ev_qdestroy,			/* eevo_qdestroy */
132208747Sraj	ef10_ev_qprime,				/* eevo_qprime */
133208747Sraj	ef10_ev_qpost,				/* eevo_qpost */
134208747Sraj	ef10_ev_qmoderate,			/* eevo_qmoderate */
135208747Sraj#if EFSYS_OPT_QSTATS
136208747Sraj	ef10_ev_qstats_update,			/* eevo_qstats_update */
137208747Sraj#endif
138208747Sraj};
139208747Sraj#endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */
140208747Sraj
141208747Sraj
142208747Sraj	__checkReturn	efx_rc_t
143208747Srajefx_ev_init(
144208747Sraj	__in		efx_nic_t *enp)
145208747Sraj{
146208747Sraj	const efx_ev_ops_t *eevop;
147208747Sraj	efx_rc_t rc;
148208747Sraj
149208747Sraj	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
150208747Sraj	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
151208747Sraj
152208747Sraj	if (enp->en_mod_flags & EFX_MOD_EV) {
153208747Sraj		rc = EINVAL;
154208747Sraj		goto fail1;
155208747Sraj	}
156208747Sraj
157208747Sraj	switch (enp->en_family) {
158208747Sraj#if EFSYS_OPT_SIENA
159208747Sraj	case EFX_FAMILY_SIENA:
160208747Sraj		eevop = &__efx_ev_siena_ops;
161208747Sraj		break;
162208747Sraj#endif /* EFSYS_OPT_SIENA */
163218077Smarcel
164218077Smarcel#if EFSYS_OPT_HUNTINGTON
165208747Sraj	case EFX_FAMILY_HUNTINGTON:
166208747Sraj		eevop = &__efx_ev_ef10_ops;
167208747Sraj		break;
168208747Sraj#endif /* EFSYS_OPT_HUNTINGTON */
169208747Sraj
170208747Sraj#if EFSYS_OPT_MEDFORD
171208747Sraj	case EFX_FAMILY_MEDFORD:
172208747Sraj		eevop = &__efx_ev_ef10_ops;
173208747Sraj		break;
174208747Sraj#endif /* EFSYS_OPT_MEDFORD */
175208747Sraj
176208747Sraj	default:
177208747Sraj		EFSYS_ASSERT(0);
178208747Sraj		rc = ENOTSUP;
179208747Sraj		goto fail1;
180208747Sraj	}
181208747Sraj
182208747Sraj	EFSYS_ASSERT3U(enp->en_ev_qcount, ==, 0);
183208747Sraj
184208747Sraj	if ((rc = eevop->eevo_init(enp)) != 0)
185208747Sraj		goto fail2;
186208747Sraj
187208747Sraj	enp->en_eevop = eevop;
188208747Sraj	enp->en_mod_flags |= EFX_MOD_EV;
189208747Sraj	return (0);
190208747Sraj
191208747Srajfail2:
192208747Sraj	EFSYS_PROBE(fail2);
193208747Sraj
194208747Srajfail1:
195208747Sraj	EFSYS_PROBE1(fail1, efx_rc_t, rc);
196208747Sraj
197208747Sraj	enp->en_eevop = NULL;
198208747Sraj	enp->en_mod_flags &= ~EFX_MOD_EV;
199208747Sraj	return (rc);
200208747Sraj}
201208747Sraj
202208747Sraj		void
203208747Srajefx_ev_fini(
204208747Sraj	__in	efx_nic_t *enp)
205208747Sraj{
206208747Sraj	const efx_ev_ops_t *eevop = enp->en_eevop;
207208747Sraj
208208747Sraj	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
209208747Sraj	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
210218077Smarcel	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_EV);
211218077Smarcel	EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_RX));
212208747Sraj	EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TX));
213218077Smarcel	EFSYS_ASSERT3U(enp->en_ev_qcount, ==, 0);
214218077Smarcel
215218077Smarcel	eevop->eevo_fini(enp);
216208747Sraj
217218077Smarcel	enp->en_eevop = NULL;
218208747Sraj	enp->en_mod_flags &= ~EFX_MOD_EV;
219208747Sraj}
220208747Sraj
221208747Sraj
222218077Smarcel	__checkReturn	efx_rc_t
223218077Smarcelefx_ev_qcreate(
224208747Sraj	__in		efx_nic_t *enp,
225208747Sraj	__in		unsigned int index,
226208747Sraj	__in		efsys_mem_t *esmp,
227208747Sraj	__in		size_t n,
228218077Smarcel	__in		uint32_t id,
229208747Sraj	__deref_out	efx_evq_t **eepp)
230208747Sraj{
231208747Sraj	const efx_ev_ops_t *eevop = enp->en_eevop;
232208747Sraj	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
233208747Sraj	efx_evq_t *eep;
234208747Sraj	efx_rc_t rc;
235208747Sraj
236208747Sraj	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
237218077Smarcel	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_EV);
238218077Smarcel
239218077Smarcel	EFSYS_ASSERT3U(enp->en_ev_qcount + 1, <, encp->enc_evq_limit);
240218077Smarcel
241208747Sraj	/* Allocate an EVQ object */
242208747Sraj	EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (efx_evq_t), eep);
243208747Sraj	if (eep == NULL) {
244208747Sraj		rc = ENOMEM;
245208747Sraj		goto fail1;
246208747Sraj	}
247208747Sraj
248208747Sraj	eep->ee_magic = EFX_EVQ_MAGIC;
249208747Sraj	eep->ee_enp = enp;
250208747Sraj	eep->ee_index = index;
251208747Sraj	eep->ee_mask = n - 1;
252208747Sraj	eep->ee_esmp = esmp;
253208747Sraj
254208747Sraj	/*
255208747Sraj	 * Set outputs before the queue is created because interrupts may be
256208747Sraj	 * raised for events immediately after the queue is created, before the
257208747Sraj	 * function call below returns. See bug58606.
258208747Sraj	 *
259208747Sraj	 * The eepp pointer passed in by the client must therefore point to data
260208747Sraj	 * shared with the client's event processing context.
261208747Sraj	 */
262208747Sraj	enp->en_ev_qcount++;
263208747Sraj	*eepp = eep;
264208747Sraj
265208747Sraj	if ((rc = eevop->eevo_qcreate(enp, index, esmp, n, id, eep)) != 0)
266208747Sraj		goto fail2;
267208747Sraj
268208747Sraj	return (0);
269208747Sraj
270208747Srajfail2:
271208747Sraj	EFSYS_PROBE(fail2);
272208747Sraj
273208747Sraj	*eepp = NULL;
274208747Sraj	enp->en_ev_qcount--;
275208747Sraj	EFSYS_KMEM_FREE(enp->en_esip, sizeof (efx_evq_t), eep);
276208747Srajfail1:
277208747Sraj	EFSYS_PROBE1(fail1, efx_rc_t, rc);
278208747Sraj	return (rc);
279208747Sraj}
280208747Sraj
281208747Sraj		void
282208747Srajefx_ev_qdestroy(
283208747Sraj	__in	efx_evq_t *eep)
284208747Sraj{
285208747Sraj	efx_nic_t *enp = eep->ee_enp;
286208747Sraj	const efx_ev_ops_t *eevop = enp->en_eevop;
287208747Sraj
288208747Sraj	EFSYS_ASSERT3U(eep->ee_magic, ==, EFX_EVQ_MAGIC);
289208747Sraj
290208747Sraj	EFSYS_ASSERT(enp->en_ev_qcount != 0);
291208747Sraj	--enp->en_ev_qcount;
292208747Sraj
293208747Sraj	eevop->eevo_qdestroy(eep);
294208747Sraj
295208747Sraj	/* Free the EVQ object */
296208747Sraj	EFSYS_KMEM_FREE(enp->en_esip, sizeof (efx_evq_t), eep);
297208747Sraj}
298208747Sraj
299208747Sraj	__checkReturn	efx_rc_t
300208747Srajefx_ev_qprime(
301208747Sraj	__in		efx_evq_t *eep,
302208747Sraj	__in		unsigned int count)
303208747Sraj{
304208747Sraj	efx_nic_t *enp = eep->ee_enp;
305208747Sraj	const efx_ev_ops_t *eevop = enp->en_eevop;
306208747Sraj	efx_rc_t rc;
307208747Sraj
308208747Sraj	EFSYS_ASSERT3U(eep->ee_magic, ==, EFX_EVQ_MAGIC);
309208747Sraj
310208747Sraj	if (!(enp->en_mod_flags & EFX_MOD_INTR)) {
311208747Sraj		rc = EINVAL;
312208747Sraj		goto fail1;
313208747Sraj	}
314209908Sraj
315218077Smarcel	if ((rc = eevop->eevo_qprime(eep, count)) != 0)
316218077Smarcel		goto fail2;
317209908Sraj
318208747Sraj	return (0);
319208747Sraj
320208747Srajfail2:
321208747Sraj	EFSYS_PROBE(fail2);
322208747Srajfail1:
323208747Sraj	EFSYS_PROBE1(fail1, efx_rc_t, rc);
324208747Sraj	return (rc);
325208747Sraj}
326208747Sraj
327208747Sraj	__checkReturn	boolean_t
328208747Srajefx_ev_qpending(
329208747Sraj	__in		efx_evq_t *eep,
330208747Sraj	__in		unsigned int count)
331208747Sraj{
332208747Sraj	size_t offset;
333208747Sraj	efx_qword_t qword;
334208747Sraj
335208747Sraj	EFSYS_ASSERT3U(eep->ee_magic, ==, EFX_EVQ_MAGIC);
336208747Sraj
337208747Sraj	offset = (count & eep->ee_mask) * sizeof (efx_qword_t);
338208747Sraj	EFSYS_MEM_READQ(eep->ee_esmp, offset, &qword);
339208747Sraj
340208747Sraj	return (EFX_EV_PRESENT(qword));
341208747Sraj}
342208747Sraj
343208747Sraj#if EFSYS_OPT_EV_PREFETCH
344208747Sraj
345208747Sraj			void
346208747Srajefx_ev_qprefetch(
347208747Sraj	__in		efx_evq_t *eep,
348208747Sraj	__in		unsigned int count)
349208747Sraj{
350208747Sraj	efx_nic_t *enp = eep->ee_enp;
351208747Sraj	unsigned int offset;
352208747Sraj
353208747Sraj	EFSYS_ASSERT3U(eep->ee_magic, ==, EFX_EVQ_MAGIC);
354218077Smarcel
355218077Smarcel	offset = (count & eep->ee_mask) * sizeof (efx_qword_t);
356218077Smarcel	EFSYS_MEM_PREFETCH(eep->ee_esmp, offset);
357218077Smarcel}
358218077Smarcel
359218077Smarcel#endif	/* EFSYS_OPT_EV_PREFETCH */
360218077Smarcel
361218077Smarcel			void
362218077Smarcelefx_ev_qpoll(
363218077Smarcel	__in		efx_evq_t *eep,
364218077Smarcel	__inout		unsigned int *countp,
365218077Smarcel	__in		const efx_ev_callbacks_t *eecp,
366218077Smarcel	__in_opt	void *arg)
367218077Smarcel{
368218077Smarcel	EFSYS_ASSERT3U(eep->ee_magic, ==, EFX_EVQ_MAGIC);
369218077Smarcel
370218077Smarcel	/*
371218077Smarcel	 * FIXME: Huntington will require support for hardware event batching
372218077Smarcel	 * and merging, which will need a different ev_qpoll implementation.
373218077Smarcel	 *
374218077Smarcel	 * Without those features the Falcon/Siena code can be used unchanged.
375218077Smarcel	 */
376218077Smarcel	EFX_STATIC_ASSERT(ESF_DZ_EV_CODE_LBN == FSF_AZ_EV_CODE_LBN);
377218077Smarcel	EFX_STATIC_ASSERT(ESF_DZ_EV_CODE_WIDTH == FSF_AZ_EV_CODE_WIDTH);
378218077Smarcel
379218077Smarcel	EFX_STATIC_ASSERT(ESE_DZ_EV_CODE_RX_EV == FSE_AZ_EV_CODE_RX_EV);
380218077Smarcel	EFX_STATIC_ASSERT(ESE_DZ_EV_CODE_TX_EV == FSE_AZ_EV_CODE_TX_EV);
381218077Smarcel	EFX_STATIC_ASSERT(ESE_DZ_EV_CODE_DRIVER_EV == FSE_AZ_EV_CODE_DRIVER_EV);
382218077Smarcel	EFX_STATIC_ASSERT(ESE_DZ_EV_CODE_DRV_GEN_EV ==
383218077Smarcel	    FSE_AZ_EV_CODE_DRV_GEN_EV);
384218077Smarcel#if EFSYS_OPT_MCDI
385218077Smarcel	EFX_STATIC_ASSERT(ESE_DZ_EV_CODE_MCDI_EV ==
386218077Smarcel	    FSE_AZ_EV_CODE_MCDI_EVRESPONSE);
387218077Smarcel#endif
388218077Smarcel	siena_ev_qpoll(eep, countp, eecp, arg);
389218077Smarcel}
390218077Smarcel
391218077Smarcel			void
392218077Smarcelefx_ev_qpost(
393218077Smarcel	__in	efx_evq_t *eep,
394218077Smarcel	__in	uint16_t data)
395218077Smarcel{
396218077Smarcel	efx_nic_t *enp = eep->ee_enp;
397218077Smarcel	const efx_ev_ops_t *eevop = enp->en_eevop;
398218077Smarcel
399218077Smarcel	EFSYS_ASSERT3U(eep->ee_magic, ==, EFX_EVQ_MAGIC);
400218077Smarcel
401218077Smarcel	EFSYS_ASSERT(eevop != NULL &&
402218077Smarcel	    eevop->eevo_qpost != NULL);
403218077Smarcel
404218077Smarcel	eevop->eevo_qpost(eep, data);
405218077Smarcel}
406218077Smarcel
407218077Smarcel	__checkReturn	efx_rc_t
408218077Smarcelefx_ev_qmoderate(
409218077Smarcel	__in		efx_evq_t *eep,
410218077Smarcel	__in		unsigned int us)
411218077Smarcel{
412218077Smarcel	efx_nic_t *enp = eep->ee_enp;
413218077Smarcel	const efx_ev_ops_t *eevop = enp->en_eevop;
414218077Smarcel	efx_rc_t rc;
415218077Smarcel
416218077Smarcel	EFSYS_ASSERT3U(eep->ee_magic, ==, EFX_EVQ_MAGIC);
417218077Smarcel
418218077Smarcel	if ((rc = eevop->eevo_qmoderate(eep, us)) != 0)
419218077Smarcel		goto fail1;
420218077Smarcel
421218077Smarcel	return (0);
422218077Smarcel
423218077Smarcelfail1:
424218077Smarcel	EFSYS_PROBE1(fail1, efx_rc_t, rc);
425218077Smarcel	return (rc);
426218077Smarcel}
427218077Smarcel
428218077Smarcel#if EFSYS_OPT_QSTATS
429218077Smarcel					void
430218077Smarcelefx_ev_qstats_update(
431218077Smarcel	__in				efx_evq_t *eep,
432218077Smarcel	__inout_ecount(EV_NQSTATS)	efsys_stat_t *stat)
433218077Smarcel
434218077Smarcel{	efx_nic_t *enp = eep->ee_enp;
435218077Smarcel	const efx_ev_ops_t *eevop = enp->en_eevop;
436218077Smarcel
437218077Smarcel	EFSYS_ASSERT3U(eep->ee_magic, ==, EFX_EVQ_MAGIC);
438218077Smarcel
439218077Smarcel	eevop->eevo_qstats_update(eep, stat);
440218077Smarcel}
441218077Smarcel
442218077Smarcel#endif	/* EFSYS_OPT_QSTATS */
443218077Smarcel
444218077Smarcel#if EFSYS_OPT_SIENA
445218077Smarcel
446218077Smarcelstatic	__checkReturn	efx_rc_t
447218077Smarcelsiena_ev_init(
448218077Smarcel	__in		efx_nic_t *enp)
449218077Smarcel{
450218077Smarcel	efx_oword_t oword;
451218077Smarcel
452218077Smarcel	/*
453218077Smarcel	 * Program the event queue for receive and transmit queue
454218077Smarcel	 * flush events.
455218077Smarcel	 */
456218077Smarcel	EFX_BAR_READO(enp, FR_AZ_DP_CTRL_REG, &oword);
457218077Smarcel	EFX_SET_OWORD_FIELD(oword, FRF_AZ_FLS_EVQ_ID, 0);
458218077Smarcel	EFX_BAR_WRITEO(enp, FR_AZ_DP_CTRL_REG, &oword);
459218077Smarcel
460218077Smarcel	return (0);
461218077Smarcel
462218077Smarcel}
463218077Smarcel
464218077Smarcelstatic  __checkReturn   boolean_t
465218077Smarcelsiena_ev_rx_not_ok(
466218077Smarcel	__in		efx_evq_t *eep,
467218077Smarcel	__in		efx_qword_t *eqp,
468218077Smarcel	__in		uint32_t label,
469218077Smarcel	__in		uint32_t id,
470218077Smarcel	__inout		uint16_t *flagsp)
471218077Smarcel{
472218077Smarcel	boolean_t ignore = B_FALSE;
473218077Smarcel
474218077Smarcel	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_TOBE_DISC) != 0) {
475218077Smarcel		EFX_EV_QSTAT_INCR(eep, EV_RX_TOBE_DISC);
476218077Smarcel		EFSYS_PROBE(tobe_disc);
477218077Smarcel		/*
478218077Smarcel		 * Assume this is a unicast address mismatch, unless below
479218077Smarcel		 * we find either FSF_AZ_RX_EV_ETH_CRC_ERR or
480218077Smarcel		 * EV_RX_PAUSE_FRM_ERR is set.
481218077Smarcel		 */
482218077Smarcel		(*flagsp) |= EFX_ADDR_MISMATCH;
483218077Smarcel	}
484218077Smarcel
485218077Smarcel	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_FRM_TRUNC) != 0) {
486218077Smarcel		EFSYS_PROBE2(frm_trunc, uint32_t, label, uint32_t, id);
487218077Smarcel		EFX_EV_QSTAT_INCR(eep, EV_RX_FRM_TRUNC);
488218077Smarcel		(*flagsp) |= EFX_DISCARD;
489218077Smarcel
490218077Smarcel#if EFSYS_OPT_RX_SCATTER
491218077Smarcel		/*
492218077Smarcel		 * Lookout for payload queue ran dry errors and ignore them.
493218077Smarcel		 *
494218077Smarcel		 * Sadly for the header/data split cases, the descriptor
495218077Smarcel		 * pointer in this event refers to the header queue and
496218077Smarcel		 * therefore cannot be easily detected as duplicate.
497218077Smarcel		 * So we drop these and rely on the receive processing seeing
498218077Smarcel		 * a subsequent packet with FSF_AZ_RX_EV_SOP set to discard
499218077Smarcel		 * the partially received packet.
500218077Smarcel		 */
501218077Smarcel		if ((EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_SOP) == 0) &&
502218077Smarcel		    (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_JUMBO_CONT) == 0) &&
503218077Smarcel		    (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_BYTE_CNT) == 0))
504218077Smarcel			ignore = B_TRUE;
505218077Smarcel#endif	/* EFSYS_OPT_RX_SCATTER */
506218077Smarcel	}
507218077Smarcel
508218077Smarcel	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_ETH_CRC_ERR) != 0) {
509218077Smarcel		EFX_EV_QSTAT_INCR(eep, EV_RX_ETH_CRC_ERR);
510218077Smarcel		EFSYS_PROBE(crc_err);
511218077Smarcel		(*flagsp) &= ~EFX_ADDR_MISMATCH;
512218077Smarcel		(*flagsp) |= EFX_DISCARD;
513218077Smarcel	}
514218077Smarcel
515218077Smarcel	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_PAUSE_FRM_ERR) != 0) {
516218077Smarcel		EFX_EV_QSTAT_INCR(eep, EV_RX_PAUSE_FRM_ERR);
517218077Smarcel		EFSYS_PROBE(pause_frm_err);
518218077Smarcel		(*flagsp) &= ~EFX_ADDR_MISMATCH;
519218077Smarcel		(*flagsp) |= EFX_DISCARD;
520218077Smarcel	}
521218077Smarcel
522218077Smarcel	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_BUF_OWNER_ID_ERR) != 0) {
523218077Smarcel		EFX_EV_QSTAT_INCR(eep, EV_RX_BUF_OWNER_ID_ERR);
524218077Smarcel		EFSYS_PROBE(owner_id_err);
525218077Smarcel		(*flagsp) |= EFX_DISCARD;
526218077Smarcel	}
527218077Smarcel
528218077Smarcel	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_IP_HDR_CHKSUM_ERR) != 0) {
529218077Smarcel		EFX_EV_QSTAT_INCR(eep, EV_RX_IPV4_HDR_CHKSUM_ERR);
530218077Smarcel		EFSYS_PROBE(ipv4_err);
531218077Smarcel		(*flagsp) &= ~EFX_CKSUM_IPV4;
532218077Smarcel	}
533218077Smarcel
534218077Smarcel	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_TCP_UDP_CHKSUM_ERR) != 0) {
535218077Smarcel		EFX_EV_QSTAT_INCR(eep, EV_RX_TCP_UDP_CHKSUM_ERR);
536218077Smarcel		EFSYS_PROBE(udp_chk_err);
537218077Smarcel		(*flagsp) &= ~EFX_CKSUM_TCPUDP;
538218077Smarcel	}
539218077Smarcel
540218077Smarcel	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_IP_FRAG_ERR) != 0) {
541218077Smarcel		EFX_EV_QSTAT_INCR(eep, EV_RX_IP_FRAG_ERR);
542218077Smarcel
543218077Smarcel		/*
544218077Smarcel		 * If IP is fragmented FSF_AZ_RX_EV_IP_FRAG_ERR is set. This
545218077Smarcel		 * causes FSF_AZ_RX_EV_PKT_OK to be clear. This is not an error
546218077Smarcel		 * condition.
547218077Smarcel		 */
548218077Smarcel		(*flagsp) &= ~(EFX_PKT_TCP | EFX_PKT_UDP | EFX_CKSUM_TCPUDP);
549218077Smarcel	}
550218077Smarcel
551218077Smarcel	return (ignore);
552218077Smarcel}
553218077Smarcel
554218077Smarcelstatic	__checkReturn	boolean_t
555218077Smarcelsiena_ev_rx(
556218077Smarcel	__in		efx_evq_t *eep,
557218077Smarcel	__in		efx_qword_t *eqp,
558218077Smarcel	__in		const efx_ev_callbacks_t *eecp,
559218077Smarcel	__in_opt	void *arg)
560218077Smarcel{
561218077Smarcel	uint32_t id;
562218077Smarcel	uint32_t size;
563218077Smarcel	uint32_t label;
564218077Smarcel	boolean_t ok;
565218077Smarcel#if EFSYS_OPT_RX_SCATTER
566218077Smarcel	boolean_t sop;
567218077Smarcel	boolean_t jumbo_cont;
568218077Smarcel#endif	/* EFSYS_OPT_RX_SCATTER */
569	uint32_t hdr_type;
570	boolean_t is_v6;
571	uint16_t flags;
572	boolean_t ignore;
573	boolean_t should_abort;
574
575	EFX_EV_QSTAT_INCR(eep, EV_RX);
576
577	/* Basic packet information */
578	id = EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_DESC_PTR);
579	size = EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_BYTE_CNT);
580	label = EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_Q_LABEL);
581	ok = (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_PKT_OK) != 0);
582
583#if EFSYS_OPT_RX_SCATTER
584	sop = (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_SOP) != 0);
585	jumbo_cont = (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_JUMBO_CONT) != 0);
586#endif	/* EFSYS_OPT_RX_SCATTER */
587
588	hdr_type = EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_HDR_TYPE);
589
590	is_v6 = (EFX_QWORD_FIELD(*eqp, FSF_CZ_RX_EV_IPV6_PKT) != 0);
591
592	/*
593	 * If packet is marked as OK and packet type is TCP/IP or
594	 * UDP/IP or other IP, then we can rely on the hardware checksums.
595	 */
596	switch (hdr_type) {
597	case FSE_AZ_RX_EV_HDR_TYPE_IPV4V6_TCP:
598		flags = EFX_PKT_TCP | EFX_CKSUM_TCPUDP;
599		if (is_v6) {
600			EFX_EV_QSTAT_INCR(eep, EV_RX_TCP_IPV6);
601			flags |= EFX_PKT_IPV6;
602		} else {
603			EFX_EV_QSTAT_INCR(eep, EV_RX_TCP_IPV4);
604			flags |= EFX_PKT_IPV4 | EFX_CKSUM_IPV4;
605		}
606		break;
607
608	case FSE_AZ_RX_EV_HDR_TYPE_IPV4V6_UDP:
609		flags = EFX_PKT_UDP | EFX_CKSUM_TCPUDP;
610		if (is_v6) {
611			EFX_EV_QSTAT_INCR(eep, EV_RX_UDP_IPV6);
612			flags |= EFX_PKT_IPV6;
613		} else {
614			EFX_EV_QSTAT_INCR(eep, EV_RX_UDP_IPV4);
615			flags |= EFX_PKT_IPV4 | EFX_CKSUM_IPV4;
616		}
617		break;
618
619	case FSE_AZ_RX_EV_HDR_TYPE_IPV4V6_OTHER:
620		if (is_v6) {
621			EFX_EV_QSTAT_INCR(eep, EV_RX_OTHER_IPV6);
622			flags = EFX_PKT_IPV6;
623		} else {
624			EFX_EV_QSTAT_INCR(eep, EV_RX_OTHER_IPV4);
625			flags = EFX_PKT_IPV4 | EFX_CKSUM_IPV4;
626		}
627		break;
628
629	case FSE_AZ_RX_EV_HDR_TYPE_OTHER:
630		EFX_EV_QSTAT_INCR(eep, EV_RX_NON_IP);
631		flags = 0;
632		break;
633
634	default:
635		EFSYS_ASSERT(B_FALSE);
636		flags = 0;
637		break;
638	}
639
640#if EFSYS_OPT_RX_SCATTER
641	/* Report scatter and header/lookahead split buffer flags */
642	if (sop)
643		flags |= EFX_PKT_START;
644	if (jumbo_cont)
645		flags |= EFX_PKT_CONT;
646#endif	/* EFSYS_OPT_RX_SCATTER */
647
648	/* Detect errors included in the FSF_AZ_RX_EV_PKT_OK indication */
649	if (!ok) {
650		ignore = siena_ev_rx_not_ok(eep, eqp, label, id, &flags);
651		if (ignore) {
652			EFSYS_PROBE4(rx_complete, uint32_t, label, uint32_t, id,
653			    uint32_t, size, uint16_t, flags);
654
655			return (B_FALSE);
656		}
657	}
658
659	/* If we're not discarding the packet then it is ok */
660	if (~flags & EFX_DISCARD)
661		EFX_EV_QSTAT_INCR(eep, EV_RX_OK);
662
663	/* Detect multicast packets that didn't match the filter */
664	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_MCAST_PKT) != 0) {
665		EFX_EV_QSTAT_INCR(eep, EV_RX_MCAST_PKT);
666
667		if (EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_MCAST_HASH_MATCH) != 0) {
668			EFX_EV_QSTAT_INCR(eep, EV_RX_MCAST_HASH_MATCH);
669		} else {
670			EFSYS_PROBE(mcast_mismatch);
671			flags |= EFX_ADDR_MISMATCH;
672		}
673	} else {
674		flags |= EFX_PKT_UNICAST;
675	}
676
677	/*
678	 * The packet parser in Siena can abort parsing packets under
679	 * certain error conditions, setting the PKT_NOT_PARSED bit
680	 * (which clears PKT_OK). If this is set, then don't trust
681	 * the PKT_TYPE field.
682	 */
683	if (!ok) {
684		uint32_t parse_err;
685
686		parse_err = EFX_QWORD_FIELD(*eqp, FSF_CZ_RX_EV_PKT_NOT_PARSED);
687		if (parse_err != 0)
688			flags |= EFX_CHECK_VLAN;
689	}
690
691	if (~flags & EFX_CHECK_VLAN) {
692		uint32_t pkt_type;
693
694		pkt_type = EFX_QWORD_FIELD(*eqp, FSF_AZ_RX_EV_PKT_TYPE);
695		if (pkt_type >= FSE_AZ_RX_EV_PKT_TYPE_VLAN)
696			flags |= EFX_PKT_VLAN_TAGGED;
697	}
698
699	EFSYS_PROBE4(rx_complete, uint32_t, label, uint32_t, id,
700	    uint32_t, size, uint16_t, flags);
701
702	EFSYS_ASSERT(eecp->eec_rx != NULL);
703	should_abort = eecp->eec_rx(arg, label, id, size, flags);
704
705	return (should_abort);
706}
707
708static	__checkReturn	boolean_t
709siena_ev_tx(
710	__in		efx_evq_t *eep,
711	__in		efx_qword_t *eqp,
712	__in		const efx_ev_callbacks_t *eecp,
713	__in_opt	void *arg)
714{
715	uint32_t id;
716	uint32_t label;
717	boolean_t should_abort;
718
719	EFX_EV_QSTAT_INCR(eep, EV_TX);
720
721	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_COMP) != 0 &&
722	    EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_PKT_ERR) == 0 &&
723	    EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_PKT_TOO_BIG) == 0 &&
724	    EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_WQ_FF_FULL) == 0) {
725
726		id = EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_DESC_PTR);
727		label = EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_Q_LABEL);
728
729		EFSYS_PROBE2(tx_complete, uint32_t, label, uint32_t, id);
730
731		EFSYS_ASSERT(eecp->eec_tx != NULL);
732		should_abort = eecp->eec_tx(arg, label, id);
733
734		return (should_abort);
735	}
736
737	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_COMP) != 0)
738		EFSYS_PROBE3(bad_event, unsigned int, eep->ee_index,
739			    uint32_t, EFX_QWORD_FIELD(*eqp, EFX_DWORD_1),
740			    uint32_t, EFX_QWORD_FIELD(*eqp, EFX_DWORD_0));
741
742	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_PKT_ERR) != 0)
743		EFX_EV_QSTAT_INCR(eep, EV_TX_PKT_ERR);
744
745	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_PKT_TOO_BIG) != 0)
746		EFX_EV_QSTAT_INCR(eep, EV_TX_PKT_TOO_BIG);
747
748	if (EFX_QWORD_FIELD(*eqp, FSF_AZ_TX_EV_WQ_FF_FULL) != 0)
749		EFX_EV_QSTAT_INCR(eep, EV_TX_WQ_FF_FULL);
750
751	EFX_EV_QSTAT_INCR(eep, EV_TX_UNEXPECTED);
752	return (B_FALSE);
753}
754
755static	__checkReturn	boolean_t
756siena_ev_global(
757	__in		efx_evq_t *eep,
758	__in		efx_qword_t *eqp,
759	__in		const efx_ev_callbacks_t *eecp,
760	__in_opt	void *arg)
761{
762	_NOTE(ARGUNUSED(eqp, eecp, arg))
763
764	EFX_EV_QSTAT_INCR(eep, EV_GLOBAL);
765
766	return (B_FALSE);
767}
768
769static	__checkReturn	boolean_t
770siena_ev_driver(
771	__in		efx_evq_t *eep,
772	__in		efx_qword_t *eqp,
773	__in		const efx_ev_callbacks_t *eecp,
774	__in_opt	void *arg)
775{
776	boolean_t should_abort;
777
778	EFX_EV_QSTAT_INCR(eep, EV_DRIVER);
779	should_abort = B_FALSE;
780
781	switch (EFX_QWORD_FIELD(*eqp, FSF_AZ_DRIVER_EV_SUBCODE)) {
782	case FSE_AZ_TX_DESCQ_FLS_DONE_EV: {
783		uint32_t txq_index;
784
785		EFX_EV_QSTAT_INCR(eep, EV_DRIVER_TX_DESCQ_FLS_DONE);
786
787		txq_index = EFX_QWORD_FIELD(*eqp, FSF_AZ_DRIVER_EV_SUBDATA);
788
789		EFSYS_PROBE1(tx_descq_fls_done, uint32_t, txq_index);
790
791		EFSYS_ASSERT(eecp->eec_txq_flush_done != NULL);
792		should_abort = eecp->eec_txq_flush_done(arg, txq_index);
793
794		break;
795	}
796	case FSE_AZ_RX_DESCQ_FLS_DONE_EV: {
797		uint32_t rxq_index;
798		uint32_t failed;
799
800		rxq_index = EFX_QWORD_FIELD(*eqp, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
801		failed = EFX_QWORD_FIELD(*eqp, FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
802
803		EFSYS_ASSERT(eecp->eec_rxq_flush_done != NULL);
804		EFSYS_ASSERT(eecp->eec_rxq_flush_failed != NULL);
805
806		if (failed) {
807			EFX_EV_QSTAT_INCR(eep, EV_DRIVER_RX_DESCQ_FLS_FAILED);
808
809			EFSYS_PROBE1(rx_descq_fls_failed, uint32_t, rxq_index);
810
811			should_abort = eecp->eec_rxq_flush_failed(arg,
812								    rxq_index);
813		} else {
814			EFX_EV_QSTAT_INCR(eep, EV_DRIVER_RX_DESCQ_FLS_DONE);
815
816			EFSYS_PROBE1(rx_descq_fls_done, uint32_t, rxq_index);
817
818			should_abort = eecp->eec_rxq_flush_done(arg, rxq_index);
819		}
820
821		break;
822	}
823	case FSE_AZ_EVQ_INIT_DONE_EV:
824		EFSYS_ASSERT(eecp->eec_initialized != NULL);
825		should_abort = eecp->eec_initialized(arg);
826
827		break;
828
829	case FSE_AZ_EVQ_NOT_EN_EV:
830		EFSYS_PROBE(evq_not_en);
831		break;
832
833	case FSE_AZ_SRM_UPD_DONE_EV: {
834		uint32_t code;
835
836		EFX_EV_QSTAT_INCR(eep, EV_DRIVER_SRM_UPD_DONE);
837
838		code = EFX_QWORD_FIELD(*eqp, FSF_AZ_DRIVER_EV_SUBDATA);
839
840		EFSYS_ASSERT(eecp->eec_sram != NULL);
841		should_abort = eecp->eec_sram(arg, code);
842
843		break;
844	}
845	case FSE_AZ_WAKE_UP_EV: {
846		uint32_t id;
847
848		id = EFX_QWORD_FIELD(*eqp, FSF_AZ_DRIVER_EV_SUBDATA);
849
850		EFSYS_ASSERT(eecp->eec_wake_up != NULL);
851		should_abort = eecp->eec_wake_up(arg, id);
852
853		break;
854	}
855	case FSE_AZ_TX_PKT_NON_TCP_UDP:
856		EFSYS_PROBE(tx_pkt_non_tcp_udp);
857		break;
858
859	case FSE_AZ_TIMER_EV: {
860		uint32_t id;
861
862		id = EFX_QWORD_FIELD(*eqp, FSF_AZ_DRIVER_EV_SUBDATA);
863
864		EFSYS_ASSERT(eecp->eec_timer != NULL);
865		should_abort = eecp->eec_timer(arg, id);
866
867		break;
868	}
869	case FSE_AZ_RX_DSC_ERROR_EV:
870		EFX_EV_QSTAT_INCR(eep, EV_DRIVER_RX_DSC_ERROR);
871
872		EFSYS_PROBE(rx_dsc_error);
873
874		EFSYS_ASSERT(eecp->eec_exception != NULL);
875		should_abort = eecp->eec_exception(arg,
876			EFX_EXCEPTION_RX_DSC_ERROR, 0);
877
878		break;
879
880	case FSE_AZ_TX_DSC_ERROR_EV:
881		EFX_EV_QSTAT_INCR(eep, EV_DRIVER_TX_DSC_ERROR);
882
883		EFSYS_PROBE(tx_dsc_error);
884
885		EFSYS_ASSERT(eecp->eec_exception != NULL);
886		should_abort = eecp->eec_exception(arg,
887			EFX_EXCEPTION_TX_DSC_ERROR, 0);
888
889		break;
890
891	default:
892		break;
893	}
894
895	return (should_abort);
896}
897
898static	__checkReturn	boolean_t
899siena_ev_drv_gen(
900	__in		efx_evq_t *eep,
901	__in		efx_qword_t *eqp,
902	__in		const efx_ev_callbacks_t *eecp,
903	__in_opt	void *arg)
904{
905	uint32_t data;
906	boolean_t should_abort;
907
908	EFX_EV_QSTAT_INCR(eep, EV_DRV_GEN);
909
910	data = EFX_QWORD_FIELD(*eqp, FSF_AZ_EV_DATA_DW0);
911	if (data >= ((uint32_t)1 << 16)) {
912		EFSYS_PROBE3(bad_event, unsigned int, eep->ee_index,
913			    uint32_t, EFX_QWORD_FIELD(*eqp, EFX_DWORD_1),
914			    uint32_t, EFX_QWORD_FIELD(*eqp, EFX_DWORD_0));
915		return (B_TRUE);
916	}
917
918	EFSYS_ASSERT(eecp->eec_software != NULL);
919	should_abort = eecp->eec_software(arg, (uint16_t)data);
920
921	return (should_abort);
922}
923
924#if EFSYS_OPT_MCDI
925
926static	__checkReturn	boolean_t
927siena_ev_mcdi(
928	__in		efx_evq_t *eep,
929	__in		efx_qword_t *eqp,
930	__in		const efx_ev_callbacks_t *eecp,
931	__in_opt	void *arg)
932{
933	efx_nic_t *enp = eep->ee_enp;
934	unsigned code;
935	boolean_t should_abort = B_FALSE;
936
937	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
938
939	if (enp->en_family != EFX_FAMILY_SIENA)
940		goto out;
941
942	EFSYS_ASSERT(eecp->eec_link_change != NULL);
943	EFSYS_ASSERT(eecp->eec_exception != NULL);
944#if EFSYS_OPT_MON_STATS
945	EFSYS_ASSERT(eecp->eec_monitor != NULL);
946#endif
947
948	EFX_EV_QSTAT_INCR(eep, EV_MCDI_RESPONSE);
949
950	code = EFX_QWORD_FIELD(*eqp, MCDI_EVENT_CODE);
951	switch (code) {
952	case MCDI_EVENT_CODE_BADSSERT:
953		efx_mcdi_ev_death(enp, EINTR);
954		break;
955
956	case MCDI_EVENT_CODE_CMDDONE:
957		efx_mcdi_ev_cpl(enp,
958		    MCDI_EV_FIELD(eqp, CMDDONE_SEQ),
959		    MCDI_EV_FIELD(eqp, CMDDONE_DATALEN),
960		    MCDI_EV_FIELD(eqp, CMDDONE_ERRNO));
961		break;
962
963	case MCDI_EVENT_CODE_LINKCHANGE: {
964		efx_link_mode_t link_mode;
965
966		siena_phy_link_ev(enp, eqp, &link_mode);
967		should_abort = eecp->eec_link_change(arg, link_mode);
968		break;
969	}
970	case MCDI_EVENT_CODE_SENSOREVT: {
971#if EFSYS_OPT_MON_STATS
972		efx_mon_stat_t id;
973		efx_mon_stat_value_t value;
974		efx_rc_t rc;
975
976		if ((rc = mcdi_mon_ev(enp, eqp, &id, &value)) == 0)
977			should_abort = eecp->eec_monitor(arg, id, value);
978		else if (rc == ENOTSUP) {
979			should_abort = eecp->eec_exception(arg,
980				EFX_EXCEPTION_UNKNOWN_SENSOREVT,
981				MCDI_EV_FIELD(eqp, DATA));
982		} else
983			EFSYS_ASSERT(rc == ENODEV);	/* Wrong port */
984#else
985		should_abort = B_FALSE;
986#endif
987		break;
988	}
989	case MCDI_EVENT_CODE_SCHEDERR:
990		/* Informational only */
991		break;
992
993	case MCDI_EVENT_CODE_REBOOT:
994		efx_mcdi_ev_death(enp, EIO);
995		break;
996
997	case MCDI_EVENT_CODE_MAC_STATS_DMA:
998#if EFSYS_OPT_MAC_STATS
999		if (eecp->eec_mac_stats != NULL) {
1000			eecp->eec_mac_stats(arg,
1001			    MCDI_EV_FIELD(eqp, MAC_STATS_DMA_GENERATION));
1002		}
1003#endif
1004		break;
1005
1006	case MCDI_EVENT_CODE_FWALERT: {
1007		uint32_t reason = MCDI_EV_FIELD(eqp, FWALERT_REASON);
1008
1009		if (reason == MCDI_EVENT_FWALERT_REASON_SRAM_ACCESS)
1010			should_abort = eecp->eec_exception(arg,
1011				EFX_EXCEPTION_FWALERT_SRAM,
1012				MCDI_EV_FIELD(eqp, FWALERT_DATA));
1013		else
1014			should_abort = eecp->eec_exception(arg,
1015				EFX_EXCEPTION_UNKNOWN_FWALERT,
1016				MCDI_EV_FIELD(eqp, DATA));
1017		break;
1018	}
1019
1020	default:
1021		EFSYS_PROBE1(mc_pcol_error, int, code);
1022		break;
1023	}
1024
1025out:
1026	return (should_abort);
1027}
1028
1029#endif	/* EFSYS_OPT_MCDI */
1030
1031static	__checkReturn	efx_rc_t
1032siena_ev_qprime(
1033	__in		efx_evq_t *eep,
1034	__in		unsigned int count)
1035{
1036	efx_nic_t *enp = eep->ee_enp;
1037	uint32_t rptr;
1038	efx_dword_t dword;
1039
1040	rptr = count & eep->ee_mask;
1041
1042	EFX_POPULATE_DWORD_1(dword, FRF_AZ_EVQ_RPTR, rptr);
1043
1044	EFX_BAR_TBL_WRITED(enp, FR_AZ_EVQ_RPTR_REG, eep->ee_index,
1045			    &dword, B_FALSE);
1046
1047	return (0);
1048}
1049
1050#define	EFX_EV_BATCH	8
1051
1052static			void
1053siena_ev_qpoll(
1054	__in		efx_evq_t *eep,
1055	__inout		unsigned int *countp,
1056	__in		const efx_ev_callbacks_t *eecp,
1057	__in_opt	void *arg)
1058{
1059	efx_qword_t ev[EFX_EV_BATCH];
1060	unsigned int batch;
1061	unsigned int total;
1062	unsigned int count;
1063	unsigned int index;
1064	size_t offset;
1065
1066	EFSYS_ASSERT(countp != NULL);
1067	EFSYS_ASSERT(eecp != NULL);
1068
1069	count = *countp;
1070	do {
1071		/* Read up until the end of the batch period */
1072		batch = EFX_EV_BATCH - (count & (EFX_EV_BATCH - 1));
1073		offset = (count & eep->ee_mask) * sizeof (efx_qword_t);
1074		for (total = 0; total < batch; ++total) {
1075			EFSYS_MEM_READQ(eep->ee_esmp, offset, &(ev[total]));
1076
1077			if (!EFX_EV_PRESENT(ev[total]))
1078				break;
1079
1080			EFSYS_PROBE3(event, unsigned int, eep->ee_index,
1081			    uint32_t, EFX_QWORD_FIELD(ev[total], EFX_DWORD_1),
1082			    uint32_t, EFX_QWORD_FIELD(ev[total], EFX_DWORD_0));
1083
1084			offset += sizeof (efx_qword_t);
1085		}
1086
1087#if EFSYS_OPT_EV_PREFETCH && (EFSYS_OPT_EV_PREFETCH_PERIOD > 1)
1088		/*
1089		 * Prefetch the next batch when we get within PREFETCH_PERIOD
1090		 * of a completed batch. If the batch is smaller, then prefetch
1091		 * immediately.
1092		 */
1093		if (total == batch && total < EFSYS_OPT_EV_PREFETCH_PERIOD)
1094			EFSYS_MEM_PREFETCH(eep->ee_esmp, offset);
1095#endif	/* EFSYS_OPT_EV_PREFETCH */
1096
1097		/* Process the batch of events */
1098		for (index = 0; index < total; ++index) {
1099			boolean_t should_abort;
1100			uint32_t code;
1101
1102#if EFSYS_OPT_EV_PREFETCH
1103			/* Prefetch if we've now reached the batch period */
1104			if (total == batch &&
1105			    index + EFSYS_OPT_EV_PREFETCH_PERIOD == total) {
1106				offset = (count + batch) & eep->ee_mask;
1107				offset *= sizeof (efx_qword_t);
1108
1109				EFSYS_MEM_PREFETCH(eep->ee_esmp, offset);
1110			}
1111#endif	/* EFSYS_OPT_EV_PREFETCH */
1112
1113			EFX_EV_QSTAT_INCR(eep, EV_ALL);
1114
1115			code = EFX_QWORD_FIELD(ev[index], FSF_AZ_EV_CODE);
1116			switch (code) {
1117			case FSE_AZ_EV_CODE_RX_EV:
1118				should_abort = eep->ee_rx(eep,
1119				    &(ev[index]), eecp, arg);
1120				break;
1121			case FSE_AZ_EV_CODE_TX_EV:
1122				should_abort = eep->ee_tx(eep,
1123				    &(ev[index]), eecp, arg);
1124				break;
1125			case FSE_AZ_EV_CODE_DRIVER_EV:
1126				should_abort = eep->ee_driver(eep,
1127				    &(ev[index]), eecp, arg);
1128				break;
1129			case FSE_AZ_EV_CODE_DRV_GEN_EV:
1130				should_abort = eep->ee_drv_gen(eep,
1131				    &(ev[index]), eecp, arg);
1132				break;
1133#if EFSYS_OPT_MCDI
1134			case FSE_AZ_EV_CODE_MCDI_EVRESPONSE:
1135				should_abort = eep->ee_mcdi(eep,
1136				    &(ev[index]), eecp, arg);
1137				break;
1138#endif
1139			case FSE_AZ_EV_CODE_GLOBAL_EV:
1140				if (eep->ee_global) {
1141					should_abort = eep->ee_global(eep,
1142					    &(ev[index]), eecp, arg);
1143					break;
1144				}
1145				/* else fallthrough */
1146			default:
1147				EFSYS_PROBE3(bad_event,
1148				    unsigned int, eep->ee_index,
1149				    uint32_t,
1150				    EFX_QWORD_FIELD(ev[index], EFX_DWORD_1),
1151				    uint32_t,
1152				    EFX_QWORD_FIELD(ev[index], EFX_DWORD_0));
1153
1154				EFSYS_ASSERT(eecp->eec_exception != NULL);
1155				(void) eecp->eec_exception(arg,
1156					EFX_EXCEPTION_EV_ERROR, code);
1157				should_abort = B_TRUE;
1158			}
1159			if (should_abort) {
1160				/* Ignore subsequent events */
1161				total = index + 1;
1162				break;
1163			}
1164		}
1165
1166		/*
1167		 * Now that the hardware has most likely moved onto dma'ing
1168		 * into the next cache line, clear the processed events. Take
1169		 * care to only clear out events that we've processed
1170		 */
1171		EFX_SET_QWORD(ev[0]);
1172		offset = (count & eep->ee_mask) * sizeof (efx_qword_t);
1173		for (index = 0; index < total; ++index) {
1174			EFSYS_MEM_WRITEQ(eep->ee_esmp, offset, &(ev[0]));
1175			offset += sizeof (efx_qword_t);
1176		}
1177
1178		count += total;
1179
1180	} while (total == batch);
1181
1182	*countp = count;
1183}
1184
1185static		void
1186siena_ev_qpost(
1187	__in	efx_evq_t *eep,
1188	__in	uint16_t data)
1189{
1190	efx_nic_t *enp = eep->ee_enp;
1191	efx_qword_t ev;
1192	efx_oword_t oword;
1193
1194	EFX_POPULATE_QWORD_2(ev, FSF_AZ_EV_CODE, FSE_AZ_EV_CODE_DRV_GEN_EV,
1195	    FSF_AZ_EV_DATA_DW0, (uint32_t)data);
1196
1197	EFX_POPULATE_OWORD_3(oword, FRF_AZ_DRV_EV_QID, eep->ee_index,
1198	    EFX_DWORD_0, EFX_QWORD_FIELD(ev, EFX_DWORD_0),
1199	    EFX_DWORD_1, EFX_QWORD_FIELD(ev, EFX_DWORD_1));
1200
1201	EFX_BAR_WRITEO(enp, FR_AZ_DRV_EV_REG, &oword);
1202}
1203
1204static	__checkReturn	efx_rc_t
1205siena_ev_qmoderate(
1206	__in		efx_evq_t *eep,
1207	__in		unsigned int us)
1208{
1209	efx_nic_t *enp = eep->ee_enp;
1210	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1211	unsigned int locked;
1212	efx_dword_t dword;
1213	efx_rc_t rc;
1214
1215	if (us > encp->enc_evq_timer_max_us) {
1216		rc = EINVAL;
1217		goto fail1;
1218	}
1219
1220	/* If the value is zero then disable the timer */
1221	if (us == 0) {
1222		EFX_POPULATE_DWORD_2(dword,
1223		    FRF_CZ_TC_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS,
1224		    FRF_CZ_TC_TIMER_VAL, 0);
1225	} else {
1226		uint32_t timer_val;
1227
1228		/* Calculate the timer value in quanta */
1229		timer_val = us * 1000 / encp->enc_evq_timer_quantum_ns;
1230
1231		/* Moderation value is base 0 so we need to deduct 1 */
1232		if (timer_val > 0)
1233			timer_val--;
1234
1235		EFX_POPULATE_DWORD_2(dword,
1236		    FRF_CZ_TC_TIMER_MODE, FFE_CZ_TIMER_MODE_INT_HLDOFF,
1237		    FRF_CZ_TC_TIMER_VAL, timer_val);
1238	}
1239
1240	locked = (eep->ee_index == 0) ? 1 : 0;
1241
1242	EFX_BAR_TBL_WRITED(enp, FR_BZ_TIMER_COMMAND_REGP0,
1243	    eep->ee_index, &dword, locked);
1244
1245	return (0);
1246
1247fail1:
1248	EFSYS_PROBE1(fail1, efx_rc_t, rc);
1249
1250	return (rc);
1251}
1252
1253static	__checkReturn	efx_rc_t
1254siena_ev_qcreate(
1255	__in		efx_nic_t *enp,
1256	__in		unsigned int index,
1257	__in		efsys_mem_t *esmp,
1258	__in		size_t n,
1259	__in		uint32_t id,
1260	__in		efx_evq_t *eep)
1261{
1262	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1263	uint32_t size;
1264	efx_oword_t oword;
1265	efx_rc_t rc;
1266
1267	EFX_STATIC_ASSERT(ISP2(EFX_EVQ_MAXNEVS));
1268	EFX_STATIC_ASSERT(ISP2(EFX_EVQ_MINNEVS));
1269
1270	if (!ISP2(n) || (n < EFX_EVQ_MINNEVS) || (n > EFX_EVQ_MAXNEVS)) {
1271		rc = EINVAL;
1272		goto fail1;
1273	}
1274	if (index >= encp->enc_evq_limit) {
1275		rc = EINVAL;
1276		goto fail2;
1277	}
1278#if EFSYS_OPT_RX_SCALE
1279	if (enp->en_intr.ei_type == EFX_INTR_LINE &&
1280	    index >= EFX_MAXRSS_LEGACY) {
1281		rc = EINVAL;
1282		goto fail3;
1283	}
1284#endif
1285	for (size = 0; (1 << size) <= (EFX_EVQ_MAXNEVS / EFX_EVQ_MINNEVS);
1286	    size++)
1287		if ((1 << size) == (int)(n / EFX_EVQ_MINNEVS))
1288			break;
1289	if (id + (1 << size) >= encp->enc_buftbl_limit) {
1290		rc = EINVAL;
1291		goto fail4;
1292	}
1293
1294	/* Set up the handler table */
1295	eep->ee_rx	= siena_ev_rx;
1296	eep->ee_tx	= siena_ev_tx;
1297	eep->ee_driver	= siena_ev_driver;
1298	eep->ee_global	= siena_ev_global;
1299	eep->ee_drv_gen	= siena_ev_drv_gen;
1300#if EFSYS_OPT_MCDI
1301	eep->ee_mcdi	= siena_ev_mcdi;
1302#endif	/* EFSYS_OPT_MCDI */
1303
1304	/* Set up the new event queue */
1305	EFX_POPULATE_OWORD_1(oword, FRF_CZ_TIMER_Q_EN, 1);
1306	EFX_BAR_TBL_WRITEO(enp, FR_AZ_TIMER_TBL, index, &oword, B_TRUE);
1307
1308	EFX_POPULATE_OWORD_3(oword, FRF_AZ_EVQ_EN, 1, FRF_AZ_EVQ_SIZE, size,
1309	    FRF_AZ_EVQ_BUF_BASE_ID, id);
1310
1311	EFX_BAR_TBL_WRITEO(enp, FR_AZ_EVQ_PTR_TBL, index, &oword, B_TRUE);
1312
1313	return (0);
1314
1315fail4:
1316	EFSYS_PROBE(fail4);
1317#if EFSYS_OPT_RX_SCALE
1318fail3:
1319	EFSYS_PROBE(fail3);
1320#endif
1321fail2:
1322	EFSYS_PROBE(fail2);
1323fail1:
1324	EFSYS_PROBE1(fail1, efx_rc_t, rc);
1325
1326	return (rc);
1327}
1328
1329#endif /* EFSYS_OPT_SIENA */
1330
1331#if EFSYS_OPT_QSTATS
1332#if EFSYS_OPT_NAMES
1333/* START MKCONFIG GENERATED EfxEventQueueStatNamesBlock b693ddf85aee1bfd */
1334static const char 	*__efx_ev_qstat_name[] = {
1335	"all",
1336	"rx",
1337	"rx_ok",
1338	"rx_frm_trunc",
1339	"rx_tobe_disc",
1340	"rx_pause_frm_err",
1341	"rx_buf_owner_id_err",
1342	"rx_ipv4_hdr_chksum_err",
1343	"rx_tcp_udp_chksum_err",
1344	"rx_eth_crc_err",
1345	"rx_ip_frag_err",
1346	"rx_mcast_pkt",
1347	"rx_mcast_hash_match",
1348	"rx_tcp_ipv4",
1349	"rx_tcp_ipv6",
1350	"rx_udp_ipv4",
1351	"rx_udp_ipv6",
1352	"rx_other_ipv4",
1353	"rx_other_ipv6",
1354	"rx_non_ip",
1355	"rx_batch",
1356	"tx",
1357	"tx_wq_ff_full",
1358	"tx_pkt_err",
1359	"tx_pkt_too_big",
1360	"tx_unexpected",
1361	"global",
1362	"global_mnt",
1363	"driver",
1364	"driver_srm_upd_done",
1365	"driver_tx_descq_fls_done",
1366	"driver_rx_descq_fls_done",
1367	"driver_rx_descq_fls_failed",
1368	"driver_rx_dsc_error",
1369	"driver_tx_dsc_error",
1370	"drv_gen",
1371	"mcdi_response",
1372};
1373/* END MKCONFIG GENERATED EfxEventQueueStatNamesBlock */
1374
1375		const char *
1376efx_ev_qstat_name(
1377	__in	efx_nic_t *enp,
1378	__in	unsigned int id)
1379{
1380	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
1381	EFSYS_ASSERT3U(id, <, EV_NQSTATS);
1382
1383	return (__efx_ev_qstat_name[id]);
1384}
1385#endif	/* EFSYS_OPT_NAMES */
1386#endif	/* EFSYS_OPT_QSTATS */
1387
1388#if EFSYS_OPT_SIENA
1389
1390#if EFSYS_OPT_QSTATS
1391static					void
1392siena_ev_qstats_update(
1393	__in				efx_evq_t *eep,
1394	__inout_ecount(EV_NQSTATS)	efsys_stat_t *stat)
1395{
1396	unsigned int id;
1397
1398	for (id = 0; id < EV_NQSTATS; id++) {
1399		efsys_stat_t *essp = &stat[id];
1400
1401		EFSYS_STAT_INCR(essp, eep->ee_stat[id]);
1402		eep->ee_stat[id] = 0;
1403	}
1404}
1405#endif	/* EFSYS_OPT_QSTATS */
1406
1407static		void
1408siena_ev_qdestroy(
1409	__in	efx_evq_t *eep)
1410{
1411	efx_nic_t *enp = eep->ee_enp;
1412	efx_oword_t oword;
1413
1414	/* Purge event queue */
1415	EFX_ZERO_OWORD(oword);
1416
1417	EFX_BAR_TBL_WRITEO(enp, FR_AZ_EVQ_PTR_TBL,
1418	    eep->ee_index, &oword, B_TRUE);
1419
1420	EFX_ZERO_OWORD(oword);
1421	EFX_BAR_TBL_WRITEO(enp, FR_AZ_TIMER_TBL, eep->ee_index, &oword, B_TRUE);
1422}
1423
1424static		void
1425siena_ev_fini(
1426	__in	efx_nic_t *enp)
1427{
1428	_NOTE(ARGUNUSED(enp))
1429}
1430
1431#endif /* EFSYS_OPT_SIENA */
1432