xref: /illumos-gate/usr/src/uts/common/io/i40e/i40e_intr.c (revision 396505af)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2016 Joyent, Inc.
14  * Copyright 2017 Tegile Systems, Inc.  All rights reserved.
15  */
16 
17 /*
18  * -------------------------
19  * Interrupt Handling Theory
20  * -------------------------
21  *
22  * There are a couple different sets of interrupts that we need to worry about:
23  *
24  *   - Interrupts from receive queues
25  *   - Interrupts from transmit queues
26  *   - 'Other Interrupts', such as the administrative queue
27  *
28  * 'Other Interrupts' are asynchronous events such as a link status change event
29  * being posted to the administrative queue, unrecoverable ECC errors, and more.
30  * If we have something being posted to the administrative queue, then we go
31  * through and process it, because it's generally enabled as a separate logical
32  * interrupt. Note, we may need to do more here eventually. To re-enable the
33  * interrupts from the 'Other Interrupts' section, we need to clear the PBA and
34  * write ENA to PFINT_ICR0.
35  *
36  * Interrupts from the transmit and receive queues indicates that our requests
37  * have been processed. In the rx case, it means that we have data that we
38  * should take a look at and send up the stack. In the tx case, it means that
39  * data which we got from MAC has now been sent out on the wire and we can free
40  * the associated data. Most of the logic for acting upon the presence of this
41  * data can be found in i40e_transciever.c which handles all of the DMA, rx, and
42  * tx operations. This file is dedicated to handling and dealing with interrupt
43  * processing.
44  *
45  * All devices supported by this driver support three kinds of interrupts:
46  *
47  *   o Extended Message Signaled Interrupts (MSI-X)
48  *   o Message Signaled Interrupts (MSI)
49  *   o Legacy PCI interrupts (INTx)
50  *
51  * Generally speaking the hardware logically handles MSI and INTx the same and
52  * restricts us to only using a single interrupt, which isn't the interesting
53  * case. With MSI-X available, each physical function of the device provides the
54  * opportunity for multiple interrupts which is what we'll focus on.
55  *
56  * --------------------
57  * Interrupt Management
58  * --------------------
59  *
60  * By default, the admin queue, which consists of the asynchronous other
61  * interrupts is always bound to MSI-X vector zero. Next, we spread out all of
62  * the other interrupts that we have available to us over the remaining
63  * interrupt vectors.
64  *
65  * This means that there may be multiple queues, both tx and rx, which are
66  * mapped to the same interrupt. When the interrupt fires, we'll have to check
67  * all of them for servicing, before we go through and indicate that the
68  * interrupt is claimed.
69  *
70  * The hardware provides the means of mapping various queues to MSI-X interrupts
71  * by programming the I40E_QINT_RQCTL() and I4OE_QINT_TQCTL() registers. These
72  * registers can also be used to enable and disable whether or not the queue is
73  * a source of interrupts. As part of this, the hardware requires that we
74  * maintain a linked list of queues for each interrupt vector. While it may seem
75  * like this is only there for the purproses of ITRs, that's not the case. The
76  * first queue must be programmed in I40E_QINT_LNKLSTN(%vector) register. Each
77  * queue defines the next one in either the I40E_QINT_RQCTL or I40E_QINT_TQCTL
78  * register.
79  *
80  * Finally, the individual interrupt vector itself has the ability to be enabled
81  * and disabled. The overall interrupt is controlled through the
82  * I40E_PFINT_DYN_CTLN() register. This is used to turn on and off the interrupt
83  * as a whole.
84  *
85  * Note that this means that both the individual queue and the interrupt as a
86  * whole can be toggled and re-enabled.
87  *
88  * -------------------
89  * Non-MSIX Management
90  * -------------------
91  *
92  * We may have a case where the Operating System is unable to actually allocate
93  * any MSI-X to the system. In such a world, there is only one transmit/receive
94  * queue pair and it is bound to the same interrupt with index zero. The
95  * hardware doesn't allow us access to additional interrupt vectors in these
96  * modes. Note that technically we could support more transmit/receive queues if
97  * we wanted.
98  *
99  * In this world, because the interrupts for the admin queue and traffic are
100  * mixed together, we have to consult ICR0 to determine what has occurred. The
101  * QINT_TQCTL and QINT_RQCTL registers have a field, 'MSI-X 0 index' which
102  * allows us to set a specific bit in ICR0. There are up to seven such bits;
103  * however, we only use the bit 0 and 1 for the rx and tx queue respectively.
104  * These are contained by the I40E_INTR_NOTX_{R|T}X_QUEUE and
105  * I40E_INTR_NOTX_{R|T}X_MASK registers respectively.
106  *
107  * Unfortunately, these corresponding queue bits have no corresponding entry in
108  * the ICR0_ENA register. So instead, when enabling interrupts on the queues, we
109  * end up enabling it on the queue registers rather than on the MSI-X registers.
110  * In the MSI-X world, because they can be enabled and disabled, this is
111  * different and the queues can always be enabled and disabled, but the
112  * interrupts themselves are toggled (ignoring the question of interrupt
113  * blanking for polling on rings).
114  *
115  * Finally, we still have to set up the interrupt linked list, but the list is
116  * instead rooted at the register I40E_PFINT_LNKLST0, rather than being tied to
117  * one of the other MSI-X registers.
118  *
119  * --------------------
120  * Interrupt Moderation
121  * --------------------
122  *
123  * The XL710 hardware has three different interrupt moderation registers per
124  * interrupt. Unsurprisingly, we use these for:
125  *
126  *   o RX interrupts
127  *   o TX interrupts
128  *   o 'Other interrupts' (link status change, admin queue, etc.)
129  *
130  * By default, we throttle 'other interrupts' the most, then TX interrupts, and
131  * then RX interrupts. The default values for these were based on trying to
132  * reason about both the importance and frequency of events. Generally speaking
133  * 'other interrupts' are not very frequent and they're not important for the
134  * I/O data path in and of itself (though they may indicate issues with the I/O
135  * data path).
136  *
137  * On the flip side, when we're not polling, RX interrupts are very important.
138  * The longer we wait for them, the more latency that we inject into the system.
139  * However, if we allow interrupts to occur too frequently, we risk a few
140  * problems:
141  *
142  *  1) Abusing system resources. Without proper interrupt blanking and polling,
143  *     we can see upwards of 200k-300k interrupts per second on the system.
144  *
145  *  2) Not enough data coalescing to enable polling. In other words, the more
146  *     data that we allow to build up, the more likely we'll be able to enable
147  *     polling mode and allowing us to better handle bulk data.
148  *
149  * In-between the 'other interrupts' and the TX interrupts we have the
150  * reclamation of TX buffers. This operation is not quite as important as we
151  * generally size the ring large enough that we should be able to reclaim a
152  * substantial amount of the descriptors that we have used per interrupt. So
153  * while it's important that this interrupt occur, we don't necessarily need it
154  * firing as frequently as RX; it doesn't, on its own, induce additional latency
155  * into the system.
156  *
157  * Based on all this we currently assign static ITR values for the system. While
158  * we could move to a dynamic system (the hardware supports that), we'd want to
159  * make sure that we're seeing problems from this that we believe would be
160  * generally helped by the added complexity.
161  *
162  * Based on this, the default values that we have allow for the following
163  * interrupt thresholds:
164  *
165  *    o 20k interrupts/s for RX
166  *    o 5k interrupts/s for TX
167  *    o 2k interupts/s for 'Other Interrupts'
168  */
169 
170 #include "i40e_sw.h"
171 
172 #define	I40E_INTR_NOTX_QUEUE	0
173 #define	I40E_INTR_NOTX_INTR	0
174 #define	I40E_INTR_NOTX_RX_QUEUE	0
175 #define	I40E_INTR_NOTX_RX_MASK	(1 << I40E_PFINT_ICR0_QUEUE_0_SHIFT)
176 #define	I40E_INTR_NOTX_TX_QUEUE	1
177 #define	I40E_INTR_NOTX_TX_MASK	(1 << I40E_PFINT_ICR0_QUEUE_1_SHIFT)
178 
179 void
180 i40e_intr_set_itr(i40e_t *i40e, i40e_itr_index_t itr, uint_t val)
181 {
182 	int i;
183 	i40e_hw_t *hw = &i40e->i40e_hw_space;
184 
185 	VERIFY3U(val, <=, I40E_MAX_ITR);
186 	VERIFY3U(itr, <, I40E_ITR_INDEX_NONE);
187 
188 	/*
189 	 * No matter the interrupt mode, the ITR for other interrupts is always
190 	 * on interrupt zero and the same is true if we're not using MSI-X.
191 	 */
192 	if (itr == I40E_ITR_INDEX_OTHER ||
193 	    i40e->i40e_intr_type != DDI_INTR_TYPE_MSIX) {
194 		I40E_WRITE_REG(hw, I40E_PFINT_ITR0(itr), val);
195 		return;
196 	}
197 
198 	for (i = 0; i < i40e->i40e_num_trqpairs; i++) {
199 		I40E_WRITE_REG(hw, I40E_PFINT_ITRN(itr, i), val);
200 	}
201 }
202 
203 /*
204  * Re-enable the adminq. Note that the adminq doesn't have a traditional queue
205  * associated with it from an interrupt perspective and just lives on ICR0.
206  * However when MSI-X interrupts are not being used, then this also enables and
207  * disables those interrupts.
208  */
209 static void
210 i40e_intr_adminq_enable(i40e_t *i40e)
211 {
212 	i40e_hw_t *hw = &i40e->i40e_hw_space;
213 	uint32_t reg;
214 
215 	reg = I40E_PFINT_DYN_CTL0_INTENA_MASK |
216 	    I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
217 	    (I40E_ITR_INDEX_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
218 	I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTL0, reg);
219 	i40e_flush(hw);
220 }
221 
222 static void
223 i40e_intr_adminq_disable(i40e_t *i40e)
224 {
225 	i40e_hw_t *hw = &i40e->i40e_hw_space;
226 	uint32_t reg;
227 
228 	reg = I40E_ITR_INDEX_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT;
229 	I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTL0, reg);
230 }
231 
232 static void
233 i40e_intr_io_enable(i40e_t *i40e, int vector)
234 {
235 	uint32_t reg;
236 	i40e_hw_t *hw = &i40e->i40e_hw_space;
237 
238 	reg = I40E_PFINT_DYN_CTLN_INTENA_MASK |
239 	    I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
240 	    (I40E_ITR_INDEX_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
241 	I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTLN(vector - 1), reg);
242 }
243 
244 static void
245 i40e_intr_io_disable(i40e_t *i40e, int vector)
246 {
247 	uint32_t reg;
248 	i40e_hw_t *hw = &i40e->i40e_hw_space;
249 
250 	reg = I40E_ITR_INDEX_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
251 	I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTLN(vector - 1), reg);
252 }
253 
254 /*
255  * When MSI-X interrupts are being used, then we can enable the actual
256  * interrupts themselves. However, when they are not, we instead have to turn
257  * towards the queue's CAUSE_ENA bit and enable that.
258  */
259 void
260 i40e_intr_io_enable_all(i40e_t *i40e)
261 {
262 	if (i40e->i40e_intr_type == DDI_INTR_TYPE_MSIX) {
263 		int i;
264 
265 		for (i = 1; i < i40e->i40e_intr_count; i++) {
266 			i40e_intr_io_enable(i40e, i);
267 		}
268 	} else {
269 		uint32_t reg;
270 		i40e_hw_t *hw = &i40e->i40e_hw_space;
271 
272 		reg = I40E_READ_REG(hw, I40E_QINT_RQCTL(I40E_INTR_NOTX_QUEUE));
273 		reg |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
274 		I40E_WRITE_REG(hw, I40E_QINT_RQCTL(I40E_INTR_NOTX_QUEUE), reg);
275 
276 		reg = I40E_READ_REG(hw, I40E_QINT_TQCTL(I40E_INTR_NOTX_QUEUE));
277 		reg |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
278 		I40E_WRITE_REG(hw, I40E_QINT_TQCTL(I40E_INTR_NOTX_QUEUE), reg);
279 	}
280 }
281 
282 /*
283  * When MSI-X interrupts are being used, then we can disable the actual
284  * interrupts themselves. However, when they are not, we instead have to turn
285  * towards the queue's CAUSE_ENA bit and disable that.
286  */
287 void
288 i40e_intr_io_disable_all(i40e_t *i40e)
289 {
290 	if (i40e->i40e_intr_type == DDI_INTR_TYPE_MSIX) {
291 		int i;
292 
293 		for (i = 1; i < i40e->i40e_intr_count; i++) {
294 			i40e_intr_io_disable(i40e, i);
295 		}
296 	} else {
297 		uint32_t reg;
298 		i40e_hw_t *hw = &i40e->i40e_hw_space;
299 
300 		reg = I40E_READ_REG(hw, I40E_QINT_RQCTL(I40E_INTR_NOTX_QUEUE));
301 		reg &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
302 		I40E_WRITE_REG(hw, I40E_QINT_RQCTL(I40E_INTR_NOTX_QUEUE), reg);
303 
304 		reg = I40E_READ_REG(hw, I40E_QINT_TQCTL(I40E_INTR_NOTX_QUEUE));
305 		reg &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
306 		I40E_WRITE_REG(hw, I40E_QINT_TQCTL(I40E_INTR_NOTX_QUEUE), reg);
307 	}
308 }
309 
310 /*
311  * As part of disabling the tx and rx queue's we're technically supposed to
312  * remove the linked list entries. The simplest way is to clear the LNKLSTN
313  * register by setting it to I40E_QUEUE_TYPE_EOL (0x7FF).
314  *
315  * Note all of the FM register access checks are performed by the caller.
316  */
317 void
318 i40e_intr_io_clear_cause(i40e_t *i40e)
319 {
320 	int i;
321 	i40e_hw_t *hw = &i40e->i40e_hw_space;
322 
323 	if (i40e->i40e_intr_type != DDI_INTR_TYPE_MSIX) {
324 		uint32_t reg;
325 		reg = I40E_QUEUE_TYPE_EOL;
326 		I40E_WRITE_REG(hw, I40E_PFINT_LNKLST0, reg);
327 		return;
328 	}
329 
330 	for (i = 0; i < i40e->i40e_num_trqpairs; i++) {
331 		uint32_t reg;
332 #ifdef DEBUG
333 		/*
334 		 * Verify that the interrupt in question is disabled. This is a
335 		 * prerequisite of modifying the data in question.
336 		 */
337 		reg = I40E_READ_REG(hw, I40E_PFINT_DYN_CTLN(i));
338 		VERIFY0(reg & I40E_PFINT_DYN_CTLN_INTENA_MASK);
339 #endif
340 		reg = I40E_QUEUE_TYPE_EOL;
341 		I40E_WRITE_REG(hw, I40E_PFINT_LNKLSTN(i), reg);
342 	}
343 
344 	i40e_flush(hw);
345 }
346 
347 /*
348  * Finalize interrupt handling. Mostly this disables the admin queue.
349  */
350 void
351 i40e_intr_chip_fini(i40e_t *i40e)
352 {
353 #ifdef DEBUG
354 	int i;
355 	uint32_t reg;
356 
357 	i40e_hw_t *hw = &i40e->i40e_hw_space;
358 
359 	/*
360 	 * Take a look and verify that all other interrupts have been disabled
361 	 * and the interrupt linked lists have been zeroed.
362 	 */
363 	if (i40e->i40e_intr_type == DDI_INTR_TYPE_MSIX) {
364 		for (i = 0; i < i40e->i40e_num_trqpairs; i++) {
365 			reg = I40E_READ_REG(hw, I40E_PFINT_DYN_CTLN(i));
366 			VERIFY0(reg & I40E_PFINT_DYN_CTLN_INTENA_MASK);
367 
368 			reg = I40E_READ_REG(hw, I40E_PFINT_LNKLSTN(i));
369 			VERIFY3U(reg, ==, I40E_QUEUE_TYPE_EOL);
370 		}
371 	}
372 #endif
373 
374 	i40e_intr_adminq_disable(i40e);
375 }
376 
377 /*
378  * Enable all of the queues and set the corresponding LNKLSTN registers. Note
379  * that we always enable queues as interrupt sources, even though we don't
380  * enable the MSI-X interrupt vectors.
381  */
382 static void
383 i40e_intr_init_queue_msix(i40e_t *i40e)
384 {
385 	i40e_hw_t *hw = &i40e->i40e_hw_space;
386 	uint32_t reg;
387 	int i;
388 
389 	/*
390 	 * Map queues to MSI-X interrupts. Queue i is mapped to vector i + 1.
391 	 * Note that we skip the ITR logic for the moment, just to make our
392 	 * lives as explicit and simple as possible.
393 	 */
394 	for (i = 0; i < i40e->i40e_num_trqpairs; i++) {
395 		i40e_trqpair_t *itrq = &i40e->i40e_trqpairs[i];
396 
397 		reg = (i << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT) |
398 		    (I40E_QUEUE_TYPE_RX <<
399 		    I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
400 		I40E_WRITE_REG(hw, I40E_PFINT_LNKLSTN(i), reg);
401 
402 		reg =
403 		    (itrq->itrq_rx_intrvec << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
404 		    (I40E_ITR_INDEX_RX << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
405 		    (i << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
406 		    (I40E_QUEUE_TYPE_TX << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
407 		    I40E_QINT_RQCTL_CAUSE_ENA_MASK;
408 
409 		I40E_WRITE_REG(hw, I40E_QINT_RQCTL(i), reg);
410 
411 		reg =
412 		    (itrq->itrq_tx_intrvec << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
413 		    (I40E_ITR_INDEX_TX << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
414 		    (I40E_QUEUE_TYPE_EOL << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
415 		    (I40E_QUEUE_TYPE_RX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT) |
416 		    I40E_QINT_TQCTL_CAUSE_ENA_MASK;
417 
418 		I40E_WRITE_REG(hw, I40E_QINT_TQCTL(i), reg);
419 	}
420 
421 }
422 
423 /*
424  * Set up a single queue to share the admin queue interrupt in the non-MSI-X
425  * world. Note we do not enable the queue as an interrupt cause at this time. We
426  * don't have any other vector of control here, unlike with the MSI-X interrupt
427  * case.
428  */
429 static void
430 i40e_intr_init_queue_shared(i40e_t *i40e)
431 {
432 	i40e_hw_t *hw = &i40e->i40e_hw_space;
433 	uint32_t reg;
434 
435 	VERIFY(i40e->i40e_intr_type == DDI_INTR_TYPE_FIXED ||
436 	    i40e->i40e_intr_type == DDI_INTR_TYPE_MSI);
437 
438 	reg = (I40E_INTR_NOTX_QUEUE << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT) |
439 	    (I40E_QUEUE_TYPE_RX << I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
440 	I40E_WRITE_REG(hw, I40E_PFINT_LNKLST0, reg);
441 
442 	reg = (I40E_INTR_NOTX_INTR << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
443 	    (I40E_ITR_INDEX_RX << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
444 	    (I40E_INTR_NOTX_RX_QUEUE << I40E_QINT_RQCTL_MSIX0_INDX_SHIFT) |
445 	    (I40E_INTR_NOTX_QUEUE << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
446 	    (I40E_QUEUE_TYPE_TX << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
447 
448 	I40E_WRITE_REG(hw, I40E_QINT_RQCTL(I40E_INTR_NOTX_QUEUE), reg);
449 
450 	reg = (I40E_INTR_NOTX_INTR << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
451 	    (I40E_ITR_INDEX_TX << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
452 	    (I40E_INTR_NOTX_TX_QUEUE << I40E_QINT_TQCTL_MSIX0_INDX_SHIFT) |
453 	    (I40E_QUEUE_TYPE_EOL << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
454 	    (I40E_QUEUE_TYPE_RX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
455 
456 	I40E_WRITE_REG(hw, I40E_QINT_TQCTL(I40E_INTR_NOTX_QUEUE), reg);
457 }
458 
459 /*
460  * Enable the specified queue as a valid source of interrupts. Note, this should
461  * only be used as part of the GLDv3's interrupt blanking routines. The debug
462  * build assertions are specific to that.
463  */
464 void
465 i40e_intr_rx_queue_enable(i40e_t *i40e, uint_t queue)
466 {
467 	uint32_t reg;
468 	i40e_hw_t *hw = &i40e->i40e_hw_space;
469 
470 	ASSERT(MUTEX_HELD(&i40e->i40e_general_lock));
471 	ASSERT(queue < i40e->i40e_num_trqpairs);
472 
473 	reg = I40E_READ_REG(hw, I40E_QINT_RQCTL(queue));
474 	ASSERT0(reg & I40E_QINT_RQCTL_CAUSE_ENA_MASK);
475 	reg |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
476 	I40E_WRITE_REG(hw, I40E_QINT_RQCTL(queue), reg);
477 }
478 
479 /*
480  * Disable the specified queue as a valid source of interrupts. Note, this
481  * should only be used as part of the GLDv3's interrupt blanking routines. The
482  * debug build assertions are specific to that.
483  */
484 void
485 i40e_intr_rx_queue_disable(i40e_t *i40e, uint_t queue)
486 {
487 	uint32_t reg;
488 	i40e_hw_t *hw = &i40e->i40e_hw_space;
489 
490 	ASSERT(MUTEX_HELD(&i40e->i40e_general_lock));
491 	ASSERT(queue < i40e->i40e_num_trqpairs);
492 
493 	reg = I40E_READ_REG(hw, I40E_QINT_RQCTL(queue));
494 	ASSERT3U(reg & I40E_QINT_RQCTL_CAUSE_ENA_MASK, ==,
495 	    I40E_QINT_RQCTL_CAUSE_ENA_MASK);
496 	reg &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
497 	I40E_WRITE_REG(hw, I40E_QINT_RQCTL(queue), reg);
498 }
499 
500 /*
501  * Start up the various chip's interrupt handling. We not only configure the
502  * adminq here, but we also go through and configure all of the actual queues,
503  * the interrupt linked lists, and others.
504  */
505 void
506 i40e_intr_chip_init(i40e_t *i40e)
507 {
508 	i40e_hw_t *hw = &i40e->i40e_hw_space;
509 	uint32_t reg;
510 
511 	/*
512 	 * Ensure that all non adminq interrupts are disabled at the chip level.
513 	 */
514 	i40e_intr_io_disable_all(i40e);
515 
516 	I40E_WRITE_REG(hw, I40E_PFINT_ICR0_ENA, 0);
517 	(void) I40E_READ_REG(hw, I40E_PFINT_ICR0);
518 
519 	/*
520 	 * Always enable all of the other-class interrupts to be on their own
521 	 * ITR. This only needs to be set on interrupt zero, which has its own
522 	 * special setting.
523 	 */
524 	reg = I40E_ITR_INDEX_OTHER << I40E_PFINT_STAT_CTL0_OTHER_ITR_INDX_SHIFT;
525 	I40E_WRITE_REG(hw, I40E_PFINT_STAT_CTL0, reg);
526 
527 	/*
528 	 * Enable interrupt types we expect to receive. At the moment, this
529 	 * is limited to the adminq; however, we'll want to review 11.2.2.9.22
530 	 * for more types here as we add support for detecting them, handling
531 	 * them, and resetting the device as appropriate.
532 	 */
533 	reg = I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
534 	I40E_WRITE_REG(hw, I40E_PFINT_ICR0_ENA, reg);
535 
536 	/*
537 	 * Always set the interrupt linked list to empty. We'll come back and
538 	 * change this if MSI-X are actually on the scene.
539 	 */
540 	I40E_WRITE_REG(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_TYPE_EOL);
541 
542 	i40e_intr_adminq_enable(i40e);
543 
544 	/*
545 	 * Set up all of the queues and map them to interrupts based on the bit
546 	 * assignments.
547 	 */
548 	if (i40e->i40e_intr_type == DDI_INTR_TYPE_MSIX) {
549 		i40e_intr_init_queue_msix(i40e);
550 	} else {
551 		i40e_intr_init_queue_shared(i40e);
552 	}
553 
554 	/*
555 	 * Finally set all of the default ITRs for the interrupts. Note that the
556 	 * queues will have been set up above.
557 	 */
558 	i40e_intr_set_itr(i40e, I40E_ITR_INDEX_RX, i40e->i40e_rx_itr);
559 	i40e_intr_set_itr(i40e, I40E_ITR_INDEX_TX, i40e->i40e_tx_itr);
560 	i40e_intr_set_itr(i40e, I40E_ITR_INDEX_OTHER, i40e->i40e_other_itr);
561 }
562 
563 static void
564 i40e_intr_adminq_work(i40e_t *i40e)
565 {
566 	struct i40e_hw *hw = &i40e->i40e_hw_space;
567 	struct i40e_arq_event_info evt;
568 	uint16_t remain = 1;
569 
570 	bzero(&evt, sizeof (struct i40e_arq_event_info));
571 	evt.buf_len = I40E_ADMINQ_BUFSZ;
572 	evt.msg_buf = i40e->i40e_aqbuf;
573 
574 	while (remain != 0) {
575 		enum i40e_status_code ret;
576 		uint16_t opcode;
577 
578 		/*
579 		 * At the moment, the only error code that seems to be returned
580 		 * is one saying that there's no work. In such a case we leave
581 		 * this be.
582 		 */
583 		ret = i40e_clean_arq_element(hw, &evt, &remain);
584 		if (ret != I40E_SUCCESS)
585 			break;
586 
587 		opcode = LE_16(evt.desc.opcode);
588 		switch (opcode) {
589 		case i40e_aqc_opc_get_link_status:
590 			mutex_enter(&i40e->i40e_general_lock);
591 			i40e_link_check(i40e);
592 			mutex_exit(&i40e->i40e_general_lock);
593 			break;
594 		default:
595 			/*
596 			 * Longer term we'll want to enable other causes here
597 			 * and get these cleaned up and doing something.
598 			 */
599 			break;
600 		}
601 	}
602 }
603 
604 static void
605 i40e_intr_rx_work(i40e_t *i40e, int queue)
606 {
607 	mblk_t *mp = NULL;
608 	i40e_trqpair_t *itrq;
609 
610 	ASSERT(queue < i40e->i40e_num_trqpairs);
611 	itrq = &i40e->i40e_trqpairs[queue];
612 
613 	mutex_enter(&itrq->itrq_rx_lock);
614 	if (!itrq->itrq_intr_poll)
615 		mp = i40e_ring_rx(itrq, I40E_POLL_NULL);
616 	mutex_exit(&itrq->itrq_rx_lock);
617 
618 	if (mp != NULL) {
619 		mac_rx_ring(i40e->i40e_mac_hdl, itrq->itrq_macrxring, mp,
620 		    itrq->itrq_rxgen);
621 	}
622 }
623 
624 static void
625 i40e_intr_tx_work(i40e_t *i40e, int queue)
626 {
627 	i40e_trqpair_t *itrq;
628 
629 	itrq = &i40e->i40e_trqpairs[queue];
630 	i40e_tx_recycle_ring(itrq);
631 }
632 
633 /*
634  * At the moment, the only 'other' interrupt on ICR0 that we handle is the
635  * adminq. We should go through and support the other notifications at some
636  * point.
637  */
638 static void
639 i40e_intr_other_work(i40e_t *i40e)
640 {
641 	struct i40e_hw *hw = &i40e->i40e_hw_space;
642 	uint32_t reg;
643 
644 	reg = I40E_READ_REG(hw, I40E_PFINT_ICR0);
645 	if (i40e_check_acc_handle(i40e->i40e_osdep_space.ios_reg_handle) !=
646 	    DDI_FM_OK) {
647 		ddi_fm_service_impact(i40e->i40e_dip, DDI_SERVICE_DEGRADED);
648 		atomic_or_32(&i40e->i40e_state, I40E_ERROR);
649 		return;
650 	}
651 
652 	if (reg & I40E_PFINT_ICR0_ADMINQ_MASK)
653 		i40e_intr_adminq_work(i40e);
654 
655 	/*
656 	 * Make sure that the adminq interrupt is not masked and then explicitly
657 	 * enable the adminq and thus the other interrupt.
658 	 */
659 	reg = I40E_READ_REG(hw, I40E_PFINT_ICR0_ENA);
660 	reg |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
661 	I40E_WRITE_REG(hw, I40E_PFINT_ICR0_ENA, reg);
662 
663 	i40e_intr_adminq_enable(i40e);
664 }
665 
666 uint_t
667 i40e_intr_msix(void *arg1, void *arg2)
668 {
669 	i40e_t *i40e = (i40e_t *)arg1;
670 	int vector_idx = (int)(uintptr_t)arg2;
671 
672 	/*
673 	 * When using MSI-X interrupts, vector 0 is always reserved for the
674 	 * adminq at this time. Though longer term, we'll want to also bridge
675 	 * some I/O to them.
676 	 */
677 	if (vector_idx == 0) {
678 		i40e_intr_other_work(i40e);
679 		return (DDI_INTR_CLAIMED);
680 	}
681 
682 	i40e_intr_rx_work(i40e, vector_idx - 1);
683 	i40e_intr_tx_work(i40e, vector_idx - 1);
684 	i40e_intr_io_enable(i40e, vector_idx);
685 
686 	return (DDI_INTR_CLAIMED);
687 }
688 
689 static uint_t
690 i40e_intr_notx(i40e_t *i40e, boolean_t shared)
691 {
692 	i40e_hw_t *hw = &i40e->i40e_hw_space;
693 	uint32_t reg;
694 	int ret = DDI_INTR_CLAIMED;
695 
696 	if (shared == B_TRUE) {
697 		mutex_enter(&i40e->i40e_general_lock);
698 		if (i40e->i40e_state & I40E_SUSPENDED) {
699 			mutex_exit(&i40e->i40e_general_lock);
700 			return (DDI_INTR_UNCLAIMED);
701 		}
702 		mutex_exit(&i40e->i40e_general_lock);
703 	}
704 
705 	reg = I40E_READ_REG(hw, I40E_PFINT_ICR0);
706 	if (i40e_check_acc_handle(i40e->i40e_osdep_space.ios_reg_handle) !=
707 	    DDI_FM_OK) {
708 		ddi_fm_service_impact(i40e->i40e_dip, DDI_SERVICE_DEGRADED);
709 		atomic_or_32(&i40e->i40e_state, I40E_ERROR);
710 		return (DDI_INTR_CLAIMED);
711 	}
712 
713 	if (reg == 0) {
714 		if (shared == B_TRUE)
715 			ret = DDI_INTR_UNCLAIMED;
716 		goto done;
717 	}
718 
719 	if (reg & I40E_PFINT_ICR0_ADMINQ_MASK)
720 		i40e_intr_adminq_work(i40e);
721 
722 	if (reg & I40E_INTR_NOTX_RX_MASK)
723 		i40e_intr_rx_work(i40e, 0);
724 
725 	if (reg & I40E_INTR_NOTX_TX_MASK)
726 		i40e_intr_tx_work(i40e, 0);
727 
728 done:
729 	i40e_intr_adminq_enable(i40e);
730 	return (ret);
731 
732 }
733 
734 /* ARGSUSED */
735 uint_t
736 i40e_intr_msi(void *arg1, void *arg2)
737 {
738 	i40e_t *i40e = (i40e_t *)arg1;
739 
740 	return (i40e_intr_notx(i40e, B_FALSE));
741 }
742 
743 /* ARGSUSED */
744 uint_t
745 i40e_intr_legacy(void *arg1, void *arg2)
746 {
747 	i40e_t *i40e = (i40e_t *)arg1;
748 
749 	return (i40e_intr_notx(i40e, B_TRUE));
750 }
751