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 #include <dev/isci/isci.h>
35f11c7f63SJim Harris
36f11c7f63SJim Harris #include <dev/pci/pcireg.h>
37f11c7f63SJim Harris #include <dev/pci/pcivar.h>
38f11c7f63SJim Harris
39f11c7f63SJim Harris #include <dev/isci/scil/scif_controller.h>
40f11c7f63SJim Harris
41f11c7f63SJim Harris void isci_interrupt_legacy_handler(void *arg);
42f11c7f63SJim Harris void isci_interrupt_msix_handler(void *arg);
43f11c7f63SJim Harris
44f11c7f63SJim Harris static int
isci_interrupt_setup_legacy(struct isci_softc * isci)45f11c7f63SJim Harris isci_interrupt_setup_legacy(struct isci_softc *isci)
46f11c7f63SJim Harris {
47f11c7f63SJim Harris struct ISCI_INTERRUPT_INFO *interrupt_info = &isci->interrupt_info[0];
48f11c7f63SJim Harris
49f11c7f63SJim Harris isci->num_interrupts = 1;
50f11c7f63SJim Harris
51f11c7f63SJim Harris scic_controller_get_handler_methods(SCIC_LEGACY_LINE_INTERRUPT_TYPE,
52f11c7f63SJim Harris 0, &isci->handlers[0]);
53f11c7f63SJim Harris
54f11c7f63SJim Harris interrupt_info->handlers = &isci->handlers[0];
55f11c7f63SJim Harris interrupt_info->rid = 0;
56f11c7f63SJim Harris interrupt_info->interrupt_target_handle = (void *)isci;
57f11c7f63SJim Harris
58f11c7f63SJim Harris interrupt_info->res = bus_alloc_resource_any(isci->device, SYS_RES_IRQ,
59f11c7f63SJim Harris &interrupt_info->rid, RF_SHAREABLE|RF_ACTIVE);
60f11c7f63SJim Harris
61f11c7f63SJim Harris if (interrupt_info->res == NULL) {
62f11c7f63SJim Harris isci_log_message(0, "ISCI", "bus_alloc_resource failed\n");
63f11c7f63SJim Harris return (-1);
64f11c7f63SJim Harris }
65f11c7f63SJim Harris
66f11c7f63SJim Harris interrupt_info->tag = NULL;
67f11c7f63SJim Harris if (bus_setup_intr(isci->device, interrupt_info->res,
68f11c7f63SJim Harris INTR_TYPE_CAM | INTR_MPSAFE, NULL, isci_interrupt_legacy_handler,
69f11c7f63SJim Harris interrupt_info, &interrupt_info->tag)) {
70f11c7f63SJim Harris isci_log_message(0, "ISCI", "bus_setup_intr failed\n");
71f11c7f63SJim Harris return (-1);
72f11c7f63SJim Harris }
73f11c7f63SJim Harris
74f11c7f63SJim Harris return (0);
75f11c7f63SJim Harris }
76f11c7f63SJim Harris
77f11c7f63SJim Harris static int
isci_interrupt_setup_msix(struct isci_softc * isci)78f11c7f63SJim Harris isci_interrupt_setup_msix(struct isci_softc *isci)
79f11c7f63SJim Harris {
80f11c7f63SJim Harris uint32_t controller_index;
81f11c7f63SJim Harris
82f11c7f63SJim Harris scic_controller_get_handler_methods(SCIC_MSIX_INTERRUPT_TYPE,
83f11c7f63SJim Harris SCI_MAX_MSIX_MESSAGES_PER_CONTROLLER, &isci->handlers[0]);
84f11c7f63SJim Harris
85f11c7f63SJim Harris for (controller_index = 0; controller_index < isci->controller_count;
86f11c7f63SJim Harris controller_index++) {
87f11c7f63SJim Harris uint32_t msix_index;
88f11c7f63SJim Harris uint8_t base_index = controller_index *
89f11c7f63SJim Harris SCI_MAX_MSIX_MESSAGES_PER_CONTROLLER;
90f11c7f63SJim Harris
91f11c7f63SJim Harris for (msix_index = 0; msix_index < SCI_MAX_MSIX_MESSAGES_PER_CONTROLLER;
92f11c7f63SJim Harris msix_index++) {
93f11c7f63SJim Harris struct ISCI_INTERRUPT_INFO *info =
94f11c7f63SJim Harris &isci->interrupt_info[base_index+msix_index];
95f11c7f63SJim Harris
96f11c7f63SJim Harris info->handlers = &isci->handlers[msix_index];
97f11c7f63SJim Harris info->interrupt_target_handle =
98f11c7f63SJim Harris &isci->controllers[controller_index];
99f11c7f63SJim Harris
100f11c7f63SJim Harris info->rid = base_index+msix_index+1;
101f11c7f63SJim Harris
102f11c7f63SJim Harris info->res = bus_alloc_resource_any(isci->device,
103f11c7f63SJim Harris SYS_RES_IRQ, &info->rid, RF_ACTIVE);
104f11c7f63SJim Harris if (info->res == NULL) {
105f11c7f63SJim Harris isci_log_message(0, "ISCI",
106f11c7f63SJim Harris "bus_alloc_resource failed\n");
107f11c7f63SJim Harris return (-1);
108f11c7f63SJim Harris }
109f11c7f63SJim Harris
110f11c7f63SJim Harris info->tag = NULL;
111f11c7f63SJim Harris if (bus_setup_intr(isci->device, info->res,
112f11c7f63SJim Harris INTR_TYPE_CAM | INTR_MPSAFE, NULL,
113f11c7f63SJim Harris isci_interrupt_msix_handler, info, &info->tag)) {
114f11c7f63SJim Harris isci_log_message(0, "ISCI",
115f11c7f63SJim Harris "bus_setup_intr failed\n");
116f11c7f63SJim Harris return (-1);
117f11c7f63SJim Harris }
118f11c7f63SJim Harris }
119f11c7f63SJim Harris }
120f11c7f63SJim Harris
121f11c7f63SJim Harris return (0);
122f11c7f63SJim Harris }
123f11c7f63SJim Harris
124f11c7f63SJim Harris void
isci_interrupt_setup(struct isci_softc * isci)125f11c7f63SJim Harris isci_interrupt_setup(struct isci_softc *isci)
126f11c7f63SJim Harris {
127f11c7f63SJim Harris uint8_t max_msix_messages = SCI_MAX_MSIX_MESSAGES_PER_CONTROLLER *
128f11c7f63SJim Harris isci->controller_count;
129f11c7f63SJim Harris BOOL use_msix = FALSE;
130f11c7f63SJim Harris uint32_t force_legacy_interrupts = 0;
131f11c7f63SJim Harris
132f11c7f63SJim Harris TUNABLE_INT_FETCH("hw.isci.force_legacy_interrupts",
133f11c7f63SJim Harris &force_legacy_interrupts);
134f11c7f63SJim Harris
135f11c7f63SJim Harris if (!force_legacy_interrupts &&
136f11c7f63SJim Harris pci_msix_count(isci->device) >= max_msix_messages) {
137f11c7f63SJim Harris
138f11c7f63SJim Harris isci->num_interrupts = max_msix_messages;
139854c3198SJim Harris if (pci_alloc_msix(isci->device, &isci->num_interrupts) == 0 &&
140854c3198SJim Harris isci->num_interrupts == max_msix_messages)
141f11c7f63SJim Harris use_msix = TRUE;
142f11c7f63SJim Harris }
143f11c7f63SJim Harris
144f11c7f63SJim Harris if (use_msix == TRUE)
145f11c7f63SJim Harris isci_interrupt_setup_msix(isci);
146f11c7f63SJim Harris else
147f11c7f63SJim Harris isci_interrupt_setup_legacy(isci);
148f11c7f63SJim Harris }
149f11c7f63SJim Harris
150f11c7f63SJim Harris void
isci_interrupt_legacy_handler(void * arg)151f11c7f63SJim Harris isci_interrupt_legacy_handler(void *arg)
152f11c7f63SJim Harris {
153f11c7f63SJim Harris struct ISCI_INTERRUPT_INFO *interrupt_info =
154f11c7f63SJim Harris (struct ISCI_INTERRUPT_INFO *)arg;
155f11c7f63SJim Harris struct isci_softc *isci =
156f11c7f63SJim Harris (struct isci_softc *)interrupt_info->interrupt_target_handle;
157f11c7f63SJim Harris SCIC_CONTROLLER_INTERRUPT_HANDLER interrupt_handler;
158f11c7f63SJim Harris SCIC_CONTROLLER_COMPLETION_HANDLER completion_handler;
159f11c7f63SJim Harris int index;
160f11c7f63SJim Harris
161f11c7f63SJim Harris interrupt_handler = interrupt_info->handlers->interrupt_handler;
162f11c7f63SJim Harris completion_handler = interrupt_info->handlers->completion_handler;
163f11c7f63SJim Harris
164f11c7f63SJim Harris for (index = 0; index < isci->controller_count; index++) {
165f11c7f63SJim Harris struct ISCI_CONTROLLER *controller = &isci->controllers[index];
166f11c7f63SJim Harris
167f11c7f63SJim Harris /* If controller_count > 0, we will get interrupts here for
168f11c7f63SJim Harris * controller 0 before controller 1 has even started. So
169f11c7f63SJim Harris * we need to make sure we don't call the completion handler
170f11c7f63SJim Harris * for a non-started controller.
171f11c7f63SJim Harris */
172f11c7f63SJim Harris if (controller->is_started == TRUE) {
173f11c7f63SJim Harris SCI_CONTROLLER_HANDLE_T scic_controller_handle =
174f11c7f63SJim Harris scif_controller_get_scic_handle(
175f11c7f63SJim Harris controller->scif_controller_handle);
176f11c7f63SJim Harris
177f11c7f63SJim Harris if (interrupt_handler(scic_controller_handle)) {
178f11c7f63SJim Harris mtx_lock(&controller->lock);
179f11c7f63SJim Harris completion_handler(scic_controller_handle);
180dd925305SJim Harris if (controller->release_queued_ccbs == TRUE)
181dd925305SJim Harris isci_controller_release_queued_ccbs(
182dd925305SJim Harris controller);
183f11c7f63SJim Harris mtx_unlock(&controller->lock);
184f11c7f63SJim Harris }
185f11c7f63SJim Harris }
186f11c7f63SJim Harris }
187f11c7f63SJim Harris }
188f11c7f63SJim Harris
189f11c7f63SJim Harris void
isci_interrupt_msix_handler(void * arg)190f11c7f63SJim Harris isci_interrupt_msix_handler(void *arg)
191f11c7f63SJim Harris {
192f11c7f63SJim Harris struct ISCI_INTERRUPT_INFO *interrupt_info =
193f11c7f63SJim Harris (struct ISCI_INTERRUPT_INFO *)arg;
194f11c7f63SJim Harris struct ISCI_CONTROLLER *controller =
195f11c7f63SJim Harris (struct ISCI_CONTROLLER *)interrupt_info->interrupt_target_handle;
196f11c7f63SJim Harris SCIC_CONTROLLER_INTERRUPT_HANDLER interrupt_handler;
197f11c7f63SJim Harris SCIC_CONTROLLER_COMPLETION_HANDLER completion_handler;
198f11c7f63SJim Harris
199f11c7f63SJim Harris interrupt_handler = interrupt_info->handlers->interrupt_handler;
200f11c7f63SJim Harris completion_handler = interrupt_info->handlers->completion_handler;
201f11c7f63SJim Harris
202f11c7f63SJim Harris SCI_CONTROLLER_HANDLE_T scic_controller_handle;
203f11c7f63SJim Harris
204f11c7f63SJim Harris scic_controller_handle = scif_controller_get_scic_handle(
205f11c7f63SJim Harris controller->scif_controller_handle);
206f11c7f63SJim Harris
207f11c7f63SJim Harris if (interrupt_handler(scic_controller_handle)) {
208f11c7f63SJim Harris mtx_lock(&controller->lock);
209f11c7f63SJim Harris completion_handler(scic_controller_handle);
210dd925305SJim Harris /*
211dd925305SJim Harris * isci_controller_release_queued_ccb() is a relatively
212dd925305SJim Harris * expensive routine, so we don't call it until the controller
213dd925305SJim Harris * level flag is set to TRUE.
214dd925305SJim Harris */
215dd925305SJim Harris if (controller->release_queued_ccbs == TRUE)
216dd925305SJim Harris isci_controller_release_queued_ccbs(controller);
217f11c7f63SJim Harris mtx_unlock(&controller->lock);
218f11c7f63SJim Harris }
219f11c7f63SJim Harris }
220f11c7f63SJim Harris
221f11c7f63SJim Harris void
isci_interrupt_poll_handler(struct ISCI_CONTROLLER * controller)222f11c7f63SJim Harris isci_interrupt_poll_handler(struct ISCI_CONTROLLER *controller)
223f11c7f63SJim Harris {
224f11c7f63SJim Harris SCI_CONTROLLER_HANDLE_T scic_controller =
225f11c7f63SJim Harris scif_controller_get_scic_handle(controller->scif_controller_handle);
226f11c7f63SJim Harris SCIC_CONTROLLER_HANDLER_METHODS_T handlers;
227f11c7f63SJim Harris
228f11c7f63SJim Harris scic_controller_get_handler_methods(SCIC_NO_INTERRUPTS, 0x0, &handlers);
229f11c7f63SJim Harris
230f11c7f63SJim Harris if(handlers.interrupt_handler(scic_controller) == TRUE) {
231f11c7f63SJim Harris /* Do not acquire controller lock in this path. xpt
232f11c7f63SJim Harris * poll routine will get called with this lock already
233f11c7f63SJim Harris * held, so we can't acquire it again here. Other users
234f11c7f63SJim Harris * of this function must acquire the lock explicitly
235f11c7f63SJim Harris * before calling this handler.
236f11c7f63SJim Harris */
237f11c7f63SJim Harris handlers.completion_handler(scic_controller);
238f11c7f63SJim Harris }
239f11c7f63SJim Harris }
240