1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 * Copyright(c) 2018 - 2020 Intel Corporation.
12 * msix_initialize() - Calculate, request and configure MSIx IRQs
13 * @dd: valid hfi1 devdata
16 int msix_initialize(struct hfi1_devdata
*dd
)
20 struct hfi1_msix_entry
*entries
;
23 * MSIx interrupt count:
24 * one for the general, "slow path" interrupt
25 * one per used SDMA engine
26 * one per kernel receive context
27 * one for each VNIC context
28 * ...any new IRQs should be added here.
30 total
= 1 + dd
->num_sdma
+ dd
->n_krcv_queues
+ dd
->num_netdev_contexts
;
32 if (total
>= CCE_NUM_MSIX_VECTORS
)
35 ret
= pci_alloc_irq_vectors(dd
->pcidev
, total
, total
, PCI_IRQ_MSIX
);
37 dd_dev_err(dd
, "pci_alloc_irq_vectors() failed: %d\n", ret
);
41 entries
= kcalloc(total
, sizeof(*dd
->msix_info
.msix_entries
),
44 pci_free_irq_vectors(dd
->pcidev
);
48 dd
->msix_info
.msix_entries
= entries
;
49 spin_lock_init(&dd
->msix_info
.msix_lock
);
50 bitmap_zero(dd
->msix_info
.in_use_msix
, total
);
51 dd
->msix_info
.max_requested
= total
;
52 dd_dev_info(dd
, "%u MSI-X interrupts allocated\n", total
);
58 * msix_request_irq() - Allocate a free MSIx IRQ
60 * @arg: context information for the IRQ
61 * @handler: IRQ handler
62 * @thread: IRQ thread handler (could be NULL)
63 * @type: affinty IRQ type
66 * Allocated an MSIx vector if available, and then create the appropriate
67 * meta data needed to keep track of the pci IRQ request.
74 static int msix_request_irq(struct hfi1_devdata
*dd
, void *arg
,
75 irq_handler_t handler
, irq_handler_t thread
,
76 enum irq_type type
, const char *name
)
81 struct hfi1_msix_entry
*me
;
83 /* Allocate an MSIx vector */
84 spin_lock(&dd
->msix_info
.msix_lock
);
85 nr
= find_first_zero_bit(dd
->msix_info
.in_use_msix
,
86 dd
->msix_info
.max_requested
);
87 if (nr
< dd
->msix_info
.max_requested
)
88 __set_bit(nr
, dd
->msix_info
.in_use_msix
);
89 spin_unlock(&dd
->msix_info
.msix_lock
);
91 if (nr
== dd
->msix_info
.max_requested
)
94 if (type
< IRQ_SDMA
|| type
>= IRQ_OTHER
)
97 irq
= pci_irq_vector(dd
->pcidev
, nr
);
98 ret
= pci_request_irq(dd
->pcidev
, nr
, handler
, thread
, arg
, name
);
101 "%s: request for IRQ %d failed, MSIx %lx, err %d\n",
103 spin_lock(&dd
->msix_info
.msix_lock
);
104 __clear_bit(nr
, dd
->msix_info
.in_use_msix
);
105 spin_unlock(&dd
->msix_info
.msix_lock
);
110 * assign arg after pci_request_irq call, so it will be
113 me
= &dd
->msix_info
.msix_entries
[nr
];
118 /* This is a request, so a failure is not fatal */
119 ret
= hfi1_get_irq_affinity(dd
, me
);
121 dd_dev_err(dd
, "%s: unable to pin IRQ %d\n", name
, ret
);
126 static int msix_request_rcd_irq_common(struct hfi1_ctxtdata
*rcd
,
127 irq_handler_t handler
,
128 irq_handler_t thread
,
131 int nr
= msix_request_irq(rcd
->dd
, rcd
, handler
, thread
,
132 rcd
->is_vnic
? IRQ_NETDEVCTXT
: IRQ_RCVCTXT
,
138 * Set the interrupt register and mask for this
139 * context's interrupt.
141 rcd
->ireg
= (IS_RCVAVAIL_START
+ rcd
->ctxt
) / 64;
142 rcd
->imask
= ((u64
)1) << ((IS_RCVAVAIL_START
+ rcd
->ctxt
) % 64);
144 remap_intr(rcd
->dd
, IS_RCVAVAIL_START
+ rcd
->ctxt
, nr
);
150 * msix_request_rcd_irq() - Helper function for RCVAVAIL IRQs
151 * @rcd: valid rcd context
154 int msix_request_rcd_irq(struct hfi1_ctxtdata
*rcd
)
156 char name
[MAX_NAME_SIZE
];
158 snprintf(name
, sizeof(name
), DRIVER_NAME
"_%d kctxt%d",
159 rcd
->dd
->unit
, rcd
->ctxt
);
161 return msix_request_rcd_irq_common(rcd
, receive_context_interrupt
,
162 receive_context_thread
, name
);
166 * msix_netdev_request_rcd_irq - Helper function for RCVAVAIL IRQs
168 * @rcd: valid netdev contexti
170 int msix_netdev_request_rcd_irq(struct hfi1_ctxtdata
*rcd
)
172 char name
[MAX_NAME_SIZE
];
174 snprintf(name
, sizeof(name
), DRIVER_NAME
"_%d nd kctxt%d",
175 rcd
->dd
->unit
, rcd
->ctxt
);
176 return msix_request_rcd_irq_common(rcd
, receive_context_interrupt_napi
,
181 * msix_request_sdma_irq - Helper for getting SDMA IRQ resources
182 * @sde: valid sdma engine
185 int msix_request_sdma_irq(struct sdma_engine
*sde
)
188 char name
[MAX_NAME_SIZE
];
190 snprintf(name
, sizeof(name
), DRIVER_NAME
"_%d sdma%d",
191 sde
->dd
->unit
, sde
->this_idx
);
192 nr
= msix_request_irq(sde
->dd
, sde
, sdma_interrupt
, NULL
,
197 remap_sdma_interrupts(sde
->dd
, sde
->this_idx
, nr
);
203 * msix_request_general_irq - Helper for getting general IRQ
205 * @dd: valid device data
207 int msix_request_general_irq(struct hfi1_devdata
*dd
)
210 char name
[MAX_NAME_SIZE
];
212 snprintf(name
, sizeof(name
), DRIVER_NAME
"_%d", dd
->unit
);
213 nr
= msix_request_irq(dd
, dd
, general_interrupt
, NULL
, IRQ_GENERAL
,
218 /* general interrupt must be MSIx vector 0 */
220 msix_free_irq(dd
, (u8
)nr
);
221 dd_dev_err(dd
, "Invalid index %d for GENERAL IRQ\n", nr
);
229 * enable_sdma_srcs - Helper to enable SDMA IRQ srcs
230 * @dd: valid devdata structure
231 * @i: index of SDMA engine
233 static void enable_sdma_srcs(struct hfi1_devdata
*dd
, int i
)
235 set_intr_bits(dd
, IS_SDMA_START
+ i
, IS_SDMA_START
+ i
, true);
236 set_intr_bits(dd
, IS_SDMA_PROGRESS_START
+ i
,
237 IS_SDMA_PROGRESS_START
+ i
, true);
238 set_intr_bits(dd
, IS_SDMA_IDLE_START
+ i
, IS_SDMA_IDLE_START
+ i
, true);
239 set_intr_bits(dd
, IS_SDMAENG_ERR_START
+ i
, IS_SDMAENG_ERR_START
+ i
,
244 * msix_request_irqs() - Allocate all MSIx IRQs
245 * @dd: valid devdata structure
247 * Helper function to request the used MSIx IRQs.
250 int msix_request_irqs(struct hfi1_devdata
*dd
)
253 int ret
= msix_request_general_irq(dd
);
258 for (i
= 0; i
< dd
->num_sdma
; i
++) {
259 struct sdma_engine
*sde
= &dd
->per_sdma
[i
];
261 ret
= msix_request_sdma_irq(sde
);
264 enable_sdma_srcs(sde
->dd
, i
);
267 for (i
= 0; i
< dd
->n_krcv_queues
; i
++) {
268 struct hfi1_ctxtdata
*rcd
= hfi1_rcd_get_by_index_safe(dd
, i
);
271 ret
= msix_request_rcd_irq(rcd
);
281 * msix_free_irq() - Free the specified MSIx resources and IRQ
283 * @msix_intr: MSIx vector to free.
286 void msix_free_irq(struct hfi1_devdata
*dd
, u8 msix_intr
)
288 struct hfi1_msix_entry
*me
;
290 if (msix_intr
>= dd
->msix_info
.max_requested
)
293 me
= &dd
->msix_info
.msix_entries
[msix_intr
];
295 if (!me
->arg
) /* => no irq, no affinity */
298 hfi1_put_irq_affinity(dd
, me
);
299 pci_free_irq(dd
->pcidev
, msix_intr
, me
->arg
);
303 spin_lock(&dd
->msix_info
.msix_lock
);
304 __clear_bit(msix_intr
, dd
->msix_info
.in_use_msix
);
305 spin_unlock(&dd
->msix_info
.msix_lock
);
309 * msix_clean_up_interrupts - Free all MSIx IRQ resources
310 * @dd: valid device data data structure
312 * Free the MSIx and associated PCI resources, if they have been allocated.
314 void msix_clean_up_interrupts(struct hfi1_devdata
*dd
)
317 struct hfi1_msix_entry
*me
= dd
->msix_info
.msix_entries
;
319 /* remove irqs - must happen before disabling/turning off */
320 for (i
= 0; i
< dd
->msix_info
.max_requested
; i
++, me
++)
321 msix_free_irq(dd
, i
);
323 /* clean structures */
324 kfree(dd
->msix_info
.msix_entries
);
325 dd
->msix_info
.msix_entries
= NULL
;
326 dd
->msix_info
.max_requested
= 0;
328 pci_free_irq_vectors(dd
->pcidev
);
332 * msix_netdev_synchronize_irq - netdev IRQ synchronize
335 void msix_netdev_synchronize_irq(struct hfi1_devdata
*dd
)
338 int ctxt_count
= hfi1_netdev_ctxt_count(dd
);
340 for (i
= 0; i
< ctxt_count
; i
++) {
341 struct hfi1_ctxtdata
*rcd
= hfi1_netdev_get_ctxt(dd
, i
);
342 struct hfi1_msix_entry
*me
;
344 me
= &dd
->msix_info
.msix_entries
[rcd
->msix_intr
];
346 synchronize_irq(me
->irq
);