xref: /freebsd/sys/dev/isci/isci_interrupt.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
1f11c7f63SJim Harris /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
3*718cf2ccSPedro F. Giffuni  *
4f11c7f63SJim Harris  * BSD LICENSE
5f11c7f63SJim Harris  *
6f11c7f63SJim Harris  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
7f11c7f63SJim Harris  * All rights reserved.
8f11c7f63SJim Harris  *
9f11c7f63SJim Harris  * Redistribution and use in source and binary forms, with or without
10f11c7f63SJim Harris  * modification, are permitted provided that the following conditions
11f11c7f63SJim Harris  * are met:
12f11c7f63SJim Harris  *
13f11c7f63SJim Harris  *   * Redistributions of source code must retain the above copyright
14f11c7f63SJim Harris  *     notice, this list of conditions and the following disclaimer.
15f11c7f63SJim Harris  *   * Redistributions in binary form must reproduce the above copyright
16f11c7f63SJim Harris  *     notice, this list of conditions and the following disclaimer in
17f11c7f63SJim Harris  *     the documentation and/or other materials provided with the
18f11c7f63SJim Harris  *     distribution.
19f11c7f63SJim Harris  *
20f11c7f63SJim Harris  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f11c7f63SJim Harris  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22f11c7f63SJim Harris  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23f11c7f63SJim Harris  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24f11c7f63SJim Harris  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25f11c7f63SJim Harris  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26f11c7f63SJim Harris  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27f11c7f63SJim Harris  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28f11c7f63SJim Harris  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29f11c7f63SJim Harris  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30f11c7f63SJim Harris  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31f11c7f63SJim Harris  */
32f11c7f63SJim Harris 
33f11c7f63SJim Harris #include <sys/cdefs.h>
34f11c7f63SJim Harris __FBSDID("$FreeBSD$");
35f11c7f63SJim Harris 
36f11c7f63SJim Harris #include <dev/isci/isci.h>
37f11c7f63SJim Harris 
38f11c7f63SJim Harris #include <dev/pci/pcireg.h>
39f11c7f63SJim Harris #include <dev/pci/pcivar.h>
40f11c7f63SJim Harris 
41f11c7f63SJim Harris #include <dev/isci/scil/scif_controller.h>
42f11c7f63SJim Harris 
43f11c7f63SJim Harris void isci_interrupt_legacy_handler(void *arg);
44f11c7f63SJim Harris void isci_interrupt_msix_handler(void *arg);
45f11c7f63SJim Harris 
46f11c7f63SJim Harris static int
47f11c7f63SJim Harris isci_interrupt_setup_legacy(struct isci_softc *isci)
48f11c7f63SJim Harris {
49f11c7f63SJim Harris 	struct ISCI_INTERRUPT_INFO *interrupt_info = &isci->interrupt_info[0];
50f11c7f63SJim Harris 
51f11c7f63SJim Harris 	isci->num_interrupts = 1;
52f11c7f63SJim Harris 
53f11c7f63SJim Harris 	scic_controller_get_handler_methods(SCIC_LEGACY_LINE_INTERRUPT_TYPE,
54f11c7f63SJim Harris 	    0, &isci->handlers[0]);
55f11c7f63SJim Harris 
56f11c7f63SJim Harris 	interrupt_info->handlers = &isci->handlers[0];
57f11c7f63SJim Harris 	interrupt_info->rid = 0;
58f11c7f63SJim Harris 	interrupt_info->interrupt_target_handle = (void *)isci;
59f11c7f63SJim Harris 
60f11c7f63SJim Harris 	interrupt_info->res = bus_alloc_resource_any(isci->device, SYS_RES_IRQ,
61f11c7f63SJim Harris 	    &interrupt_info->rid, RF_SHAREABLE|RF_ACTIVE);
62f11c7f63SJim Harris 
63f11c7f63SJim Harris 	if (interrupt_info->res == NULL) {
64f11c7f63SJim Harris 		isci_log_message(0, "ISCI", "bus_alloc_resource failed\n");
65f11c7f63SJim Harris 		return (-1);
66f11c7f63SJim Harris 	}
67f11c7f63SJim Harris 
68f11c7f63SJim Harris 	interrupt_info->tag = NULL;
69f11c7f63SJim Harris 	if (bus_setup_intr(isci->device, interrupt_info->res,
70f11c7f63SJim Harris 	    INTR_TYPE_CAM | INTR_MPSAFE, NULL, isci_interrupt_legacy_handler,
71f11c7f63SJim Harris 	    interrupt_info, &interrupt_info->tag)) {
72f11c7f63SJim Harris 		isci_log_message(0, "ISCI", "bus_setup_intr failed\n");
73f11c7f63SJim Harris 		return (-1);
74f11c7f63SJim Harris 	}
75f11c7f63SJim Harris 
76f11c7f63SJim Harris 	return (0);
77f11c7f63SJim Harris }
78f11c7f63SJim Harris 
79f11c7f63SJim Harris static int
80f11c7f63SJim Harris isci_interrupt_setup_msix(struct isci_softc *isci)
81f11c7f63SJim Harris {
82f11c7f63SJim Harris 	uint32_t controller_index;
83f11c7f63SJim Harris 
84f11c7f63SJim Harris 	scic_controller_get_handler_methods(SCIC_MSIX_INTERRUPT_TYPE,
85f11c7f63SJim Harris 	    SCI_MAX_MSIX_MESSAGES_PER_CONTROLLER, &isci->handlers[0]);
86f11c7f63SJim Harris 
87f11c7f63SJim Harris 	for (controller_index = 0; controller_index < isci->controller_count;
88f11c7f63SJim Harris 	    controller_index++) {
89f11c7f63SJim Harris 		uint32_t msix_index;
90f11c7f63SJim Harris 		uint8_t base_index = controller_index *
91f11c7f63SJim Harris 		    SCI_MAX_MSIX_MESSAGES_PER_CONTROLLER;
92f11c7f63SJim Harris 
93f11c7f63SJim Harris 		for (msix_index = 0; msix_index < SCI_MAX_MSIX_MESSAGES_PER_CONTROLLER;
94f11c7f63SJim Harris 		    msix_index++) {
95f11c7f63SJim Harris 			struct ISCI_INTERRUPT_INFO *info =
96f11c7f63SJim Harris 			    &isci->interrupt_info[base_index+msix_index];
97f11c7f63SJim Harris 
98f11c7f63SJim Harris 			info->handlers = &isci->handlers[msix_index];
99f11c7f63SJim Harris 			info->interrupt_target_handle =
100f11c7f63SJim Harris 			    &isci->controllers[controller_index];
101f11c7f63SJim Harris 
102f11c7f63SJim Harris 			info->rid = base_index+msix_index+1;
103f11c7f63SJim Harris 
104f11c7f63SJim Harris 			info->res = bus_alloc_resource_any(isci->device,
105f11c7f63SJim Harris 			    SYS_RES_IRQ, &info->rid, RF_ACTIVE);
106f11c7f63SJim Harris 			if (info->res == NULL) {
107f11c7f63SJim Harris 				isci_log_message(0, "ISCI",
108f11c7f63SJim Harris 				    "bus_alloc_resource failed\n");
109f11c7f63SJim Harris 				return (-1);
110f11c7f63SJim Harris 			}
111f11c7f63SJim Harris 
112f11c7f63SJim Harris 			info->tag = NULL;
113f11c7f63SJim Harris 			if (bus_setup_intr(isci->device, info->res,
114f11c7f63SJim Harris 			    INTR_TYPE_CAM | INTR_MPSAFE, NULL,
115f11c7f63SJim Harris 			    isci_interrupt_msix_handler, info, &info->tag)) {
116f11c7f63SJim Harris 				isci_log_message(0, "ISCI",
117f11c7f63SJim Harris 				    "bus_setup_intr failed\n");
118f11c7f63SJim Harris 				return (-1);
119f11c7f63SJim Harris 			}
120f11c7f63SJim Harris 		}
121f11c7f63SJim Harris 	}
122f11c7f63SJim Harris 
123f11c7f63SJim Harris 	return (0);
124f11c7f63SJim Harris }
125f11c7f63SJim Harris 
126f11c7f63SJim Harris void
127f11c7f63SJim Harris isci_interrupt_setup(struct isci_softc *isci)
128f11c7f63SJim Harris {
129f11c7f63SJim Harris 	uint8_t max_msix_messages = SCI_MAX_MSIX_MESSAGES_PER_CONTROLLER *
130f11c7f63SJim Harris 	    isci->controller_count;
131f11c7f63SJim Harris 	BOOL use_msix = FALSE;
132f11c7f63SJim Harris 	uint32_t force_legacy_interrupts = 0;
133f11c7f63SJim Harris 
134f11c7f63SJim Harris 	TUNABLE_INT_FETCH("hw.isci.force_legacy_interrupts",
135f11c7f63SJim Harris 	    &force_legacy_interrupts);
136f11c7f63SJim Harris 
137f11c7f63SJim Harris 	if (!force_legacy_interrupts &&
138f11c7f63SJim Harris 	    pci_msix_count(isci->device) >= max_msix_messages) {
139f11c7f63SJim Harris 
140f11c7f63SJim Harris 		isci->num_interrupts = max_msix_messages;
141854c3198SJim Harris 		if (pci_alloc_msix(isci->device, &isci->num_interrupts) == 0 &&
142854c3198SJim Harris 		    isci->num_interrupts == max_msix_messages)
143f11c7f63SJim Harris 			use_msix = TRUE;
144f11c7f63SJim Harris 	}
145f11c7f63SJim Harris 
146f11c7f63SJim Harris 	if (use_msix == TRUE)
147f11c7f63SJim Harris 		isci_interrupt_setup_msix(isci);
148f11c7f63SJim Harris 	else
149f11c7f63SJim Harris 		isci_interrupt_setup_legacy(isci);
150f11c7f63SJim Harris }
151f11c7f63SJim Harris 
152f11c7f63SJim Harris void
153f11c7f63SJim Harris isci_interrupt_legacy_handler(void *arg)
154f11c7f63SJim Harris {
155f11c7f63SJim Harris 	struct ISCI_INTERRUPT_INFO *interrupt_info =
156f11c7f63SJim Harris 	    (struct ISCI_INTERRUPT_INFO *)arg;
157f11c7f63SJim Harris 	struct isci_softc *isci =
158f11c7f63SJim Harris 	    (struct isci_softc *)interrupt_info->interrupt_target_handle;
159f11c7f63SJim Harris 	SCIC_CONTROLLER_INTERRUPT_HANDLER  interrupt_handler;
160f11c7f63SJim Harris 	SCIC_CONTROLLER_COMPLETION_HANDLER completion_handler;
161f11c7f63SJim Harris 	int index;
162f11c7f63SJim Harris 
163f11c7f63SJim Harris 	interrupt_handler =  interrupt_info->handlers->interrupt_handler;
164f11c7f63SJim Harris 	completion_handler = interrupt_info->handlers->completion_handler;
165f11c7f63SJim Harris 
166f11c7f63SJim Harris 	for (index = 0; index < isci->controller_count; index++) {
167f11c7f63SJim Harris 		struct ISCI_CONTROLLER *controller = &isci->controllers[index];
168f11c7f63SJim Harris 
169f11c7f63SJim Harris 		/* If controller_count > 0, we will get interrupts here for
170f11c7f63SJim Harris 		 *  controller 0 before controller 1 has even started.  So
171f11c7f63SJim Harris 		 *  we need to make sure we don't call the completion handler
172f11c7f63SJim Harris 		 *  for a non-started controller.
173f11c7f63SJim Harris 		 */
174f11c7f63SJim Harris 		if (controller->is_started == TRUE) {
175f11c7f63SJim Harris 			SCI_CONTROLLER_HANDLE_T scic_controller_handle =
176f11c7f63SJim Harris 			    scif_controller_get_scic_handle(
177f11c7f63SJim Harris 				controller->scif_controller_handle);
178f11c7f63SJim Harris 
179f11c7f63SJim Harris 			if (interrupt_handler(scic_controller_handle)) {
180f11c7f63SJim Harris 				mtx_lock(&controller->lock);
181f11c7f63SJim Harris 				completion_handler(scic_controller_handle);
182dd925305SJim Harris 				if (controller->release_queued_ccbs == TRUE)
183dd925305SJim Harris 					isci_controller_release_queued_ccbs(
184dd925305SJim Harris 					    controller);
185f11c7f63SJim Harris 				mtx_unlock(&controller->lock);
186f11c7f63SJim Harris 			}
187f11c7f63SJim Harris 		}
188f11c7f63SJim Harris 	}
189f11c7f63SJim Harris }
190f11c7f63SJim Harris 
191f11c7f63SJim Harris void
192f11c7f63SJim Harris isci_interrupt_msix_handler(void *arg)
193f11c7f63SJim Harris {
194f11c7f63SJim Harris 	struct ISCI_INTERRUPT_INFO *interrupt_info =
195f11c7f63SJim Harris 	    (struct ISCI_INTERRUPT_INFO *)arg;
196f11c7f63SJim Harris 	struct ISCI_CONTROLLER *controller =
197f11c7f63SJim Harris 	    (struct ISCI_CONTROLLER *)interrupt_info->interrupt_target_handle;
198f11c7f63SJim Harris 	SCIC_CONTROLLER_INTERRUPT_HANDLER  interrupt_handler;
199f11c7f63SJim Harris 	SCIC_CONTROLLER_COMPLETION_HANDLER completion_handler;
200f11c7f63SJim Harris 
201f11c7f63SJim Harris 	interrupt_handler =  interrupt_info->handlers->interrupt_handler;
202f11c7f63SJim Harris 	completion_handler = interrupt_info->handlers->completion_handler;
203f11c7f63SJim Harris 
204f11c7f63SJim Harris 	SCI_CONTROLLER_HANDLE_T scic_controller_handle;
205f11c7f63SJim Harris 
206f11c7f63SJim Harris 	scic_controller_handle = scif_controller_get_scic_handle(
207f11c7f63SJim Harris 	    controller->scif_controller_handle);
208f11c7f63SJim Harris 
209f11c7f63SJim Harris 	if (interrupt_handler(scic_controller_handle)) {
210f11c7f63SJim Harris 		mtx_lock(&controller->lock);
211f11c7f63SJim Harris 		completion_handler(scic_controller_handle);
212dd925305SJim Harris 		/*
213dd925305SJim Harris 		 * isci_controller_release_queued_ccb() is a relatively
214dd925305SJim Harris 		 *  expensive routine, so we don't call it until the controller
215dd925305SJim Harris 		 *  level flag is set to TRUE.
216dd925305SJim Harris 		 */
217dd925305SJim Harris 		if (controller->release_queued_ccbs == TRUE)
218dd925305SJim Harris 			isci_controller_release_queued_ccbs(controller);
219f11c7f63SJim Harris 		mtx_unlock(&controller->lock);
220f11c7f63SJim Harris 	}
221f11c7f63SJim Harris }
222f11c7f63SJim Harris 
223f11c7f63SJim Harris void
224f11c7f63SJim Harris isci_interrupt_poll_handler(struct ISCI_CONTROLLER *controller)
225f11c7f63SJim Harris {
226f11c7f63SJim Harris 	SCI_CONTROLLER_HANDLE_T scic_controller =
227f11c7f63SJim Harris 	    scif_controller_get_scic_handle(controller->scif_controller_handle);
228f11c7f63SJim Harris 	SCIC_CONTROLLER_HANDLER_METHODS_T handlers;
229f11c7f63SJim Harris 
230f11c7f63SJim Harris 	scic_controller_get_handler_methods(SCIC_NO_INTERRUPTS, 0x0, &handlers);
231f11c7f63SJim Harris 
232f11c7f63SJim Harris 	if(handlers.interrupt_handler(scic_controller) == TRUE) {
233f11c7f63SJim Harris 		/* Do not acquire controller lock in this path. xpt
234f11c7f63SJim Harris 		 *  poll routine will get called with this lock already
235f11c7f63SJim Harris 		 *  held, so we can't acquire it again here.  Other users
236f11c7f63SJim Harris 		 *  of this function must acquire the lock explicitly
237f11c7f63SJim Harris 		 *  before calling this handler.
238f11c7f63SJim Harris 		 */
239f11c7f63SJim Harris 		handlers.completion_handler(scic_controller);
240f11c7f63SJim Harris 	}
241f11c7f63SJim Harris }
242