1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * xHCI host controller sideband support
5 *
6 * Copyright (c) 2023-2025, Intel Corporation.
7 *
8 * Author: Mathias Nyman
9 */
10
11 #include <linux/usb/xhci-sideband.h>
12 #include <linux/dma-direct.h>
13
14 #include "xhci.h"
15
16 /* sideband internal helpers */
17 static struct sg_table *
xhci_ring_to_sgtable(struct xhci_sideband * sb,struct xhci_ring * ring)18 xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring)
19 {
20 struct xhci_segment *seg;
21 struct sg_table *sgt;
22 unsigned int n_pages;
23 struct page **pages;
24 struct device *dev;
25 size_t sz;
26 int i;
27
28 dev = xhci_to_hcd(sb->xhci)->self.sysdev;
29 sz = ring->num_segs * TRB_SEGMENT_SIZE;
30 n_pages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
31 pages = kvmalloc_array(n_pages, sizeof(struct page *), GFP_KERNEL);
32 if (!pages)
33 return NULL;
34
35 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
36 if (!sgt) {
37 kvfree(pages);
38 return NULL;
39 }
40
41 seg = ring->first_seg;
42 if (!seg)
43 goto err;
44 /*
45 * Rings can potentially have multiple segments, create an array that
46 * carries page references to allocated segments. Utilize the
47 * sg_alloc_table_from_pages() to create the sg table, and to ensure
48 * that page links are created.
49 */
50 for (i = 0; i < ring->num_segs; i++) {
51 dma_get_sgtable(dev, sgt, seg->trbs, seg->dma,
52 TRB_SEGMENT_SIZE);
53 pages[i] = sg_page(sgt->sgl);
54 sg_free_table(sgt);
55 seg = seg->next;
56 }
57
58 if (sg_alloc_table_from_pages(sgt, pages, n_pages, 0, sz, GFP_KERNEL))
59 goto err;
60
61 /*
62 * Save first segment dma address to sg dma_address field for the sideband
63 * client to have access to the IOVA of the ring.
64 */
65 sg_dma_address(sgt->sgl) = ring->first_seg->dma;
66
67 return sgt;
68
69 err:
70 kvfree(pages);
71 kfree(sgt);
72
73 return NULL;
74 }
75
76 /* Caller must hold sb->mutex */
77 static void
__xhci_sideband_remove_endpoint(struct xhci_sideband * sb,struct xhci_virt_ep * ep)78 __xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep)
79 {
80 lockdep_assert_held(&sb->mutex);
81
82 /*
83 * Issue a stop endpoint command when an endpoint is removed.
84 * The stop ep cmd handler will handle the ring cleanup.
85 */
86 xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL);
87
88 ep->sideband = NULL;
89 sb->eps[ep->ep_index] = NULL;
90 }
91
92 /* Caller must hold sb->mutex */
93 static void
__xhci_sideband_remove_interrupter(struct xhci_sideband * sb)94 __xhci_sideband_remove_interrupter(struct xhci_sideband *sb)
95 {
96 struct usb_device *udev;
97
98 lockdep_assert_held(&sb->mutex);
99
100 if (!sb->ir)
101 return;
102
103 xhci_remove_secondary_interrupter(xhci_to_hcd(sb->xhci), sb->ir);
104 sb->ir = NULL;
105 udev = sb->vdev->udev;
106
107 if (udev->state != USB_STATE_NOTATTACHED)
108 usb_offload_put(udev);
109 }
110
111 /* sideband api functions */
112
113 /**
114 * xhci_sideband_notify_ep_ring_free - notify client of xfer ring free
115 * @sb: sideband instance for this usb device
116 * @ep_index: usb endpoint index
117 *
118 * Notifies the xHCI sideband client driver of a xHCI transfer ring free
119 * routine. This will allow for the client to ensure that all transfers
120 * are completed.
121 *
122 * The callback should be synchronous, as the ring free happens after.
123 */
xhci_sideband_notify_ep_ring_free(struct xhci_sideband * sb,unsigned int ep_index)124 void xhci_sideband_notify_ep_ring_free(struct xhci_sideband *sb,
125 unsigned int ep_index)
126 {
127 struct xhci_sideband_event evt;
128
129 evt.type = XHCI_SIDEBAND_XFER_RING_FREE;
130 evt.evt_data = &ep_index;
131
132 if (sb->notify_client)
133 sb->notify_client(sb->intf, &evt);
134 }
135 EXPORT_SYMBOL_GPL(xhci_sideband_notify_ep_ring_free);
136
137 /**
138 * xhci_sideband_add_endpoint - add endpoint to sideband access list
139 * @sb: sideband instance for this usb device
140 * @host_ep: usb host endpoint
141 *
142 * Adds an endpoint to the list of sideband accessed endpoints for this usb
143 * device.
144 * After an endpoint is added the sideband client can get the endpoint transfer
145 * ring buffer by calling xhci_sideband_endpoint_buffer()
146 *
147 * Return: 0 on success, negative error otherwise.
148 */
149 int
xhci_sideband_add_endpoint(struct xhci_sideband * sb,struct usb_host_endpoint * host_ep)150 xhci_sideband_add_endpoint(struct xhci_sideband *sb,
151 struct usb_host_endpoint *host_ep)
152 {
153 struct xhci_virt_ep *ep;
154 unsigned int ep_index;
155
156 guard(mutex)(&sb->mutex);
157
158 if (!sb->vdev)
159 return -ENODEV;
160
161 ep_index = xhci_get_endpoint_index(&host_ep->desc);
162 ep = &sb->vdev->eps[ep_index];
163
164 if (ep->ep_state & EP_HAS_STREAMS)
165 return -EINVAL;
166
167 /*
168 * Note, we don't know the DMA mask of the audio DSP device, if its
169 * smaller than for xhci it won't be able to access the endpoint ring
170 * buffer. This could be solved by not allowing the audio class driver
171 * to add the endpoint the normal way, but instead offload it immediately,
172 * and let this function add the endpoint and allocate the ring buffer
173 * with the smallest common DMA mask
174 */
175 if (sb->eps[ep_index] || ep->sideband)
176 return -EBUSY;
177
178 ep->sideband = sb;
179 sb->eps[ep_index] = ep;
180
181 return 0;
182 }
183 EXPORT_SYMBOL_GPL(xhci_sideband_add_endpoint);
184
185 /**
186 * xhci_sideband_remove_endpoint - remove endpoint from sideband access list
187 * @sb: sideband instance for this usb device
188 * @host_ep: usb host endpoint
189 *
190 * Removes an endpoint from the list of sideband accessed endpoints for this usb
191 * device.
192 * sideband client should no longer touch the endpoint transfer buffer after
193 * calling this.
194 *
195 * Return: 0 on success, negative error otherwise.
196 */
197 int
xhci_sideband_remove_endpoint(struct xhci_sideband * sb,struct usb_host_endpoint * host_ep)198 xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
199 struct usb_host_endpoint *host_ep)
200 {
201 struct xhci_virt_ep *ep;
202 unsigned int ep_index;
203
204 guard(mutex)(&sb->mutex);
205
206 ep_index = xhci_get_endpoint_index(&host_ep->desc);
207 ep = sb->eps[ep_index];
208
209 if (!ep || !ep->sideband || ep->sideband != sb)
210 return -ENODEV;
211
212 __xhci_sideband_remove_endpoint(sb, ep);
213 xhci_initialize_ring_info(ep->ring);
214
215 return 0;
216 }
217 EXPORT_SYMBOL_GPL(xhci_sideband_remove_endpoint);
218
219 int
xhci_sideband_stop_endpoint(struct xhci_sideband * sb,struct usb_host_endpoint * host_ep)220 xhci_sideband_stop_endpoint(struct xhci_sideband *sb,
221 struct usb_host_endpoint *host_ep)
222 {
223 struct xhci_virt_ep *ep;
224 unsigned int ep_index;
225
226 ep_index = xhci_get_endpoint_index(&host_ep->desc);
227 ep = sb->eps[ep_index];
228
229 if (!ep || !ep->sideband || ep->sideband != sb)
230 return -EINVAL;
231
232 return xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL);
233 }
234 EXPORT_SYMBOL_GPL(xhci_sideband_stop_endpoint);
235
236 /**
237 * xhci_sideband_get_endpoint_buffer - gets the endpoint transfer buffer address
238 * @sb: sideband instance for this usb device
239 * @host_ep: usb host endpoint
240 *
241 * Returns the address of the endpoint buffer where xHC controller reads queued
242 * transfer TRBs from. This is the starting address of the ringbuffer where the
243 * sideband client should write TRBs to.
244 *
245 * Caller needs to free the returned sg_table
246 *
247 * Return: struct sg_table * if successful. NULL otherwise.
248 */
249 struct sg_table *
xhci_sideband_get_endpoint_buffer(struct xhci_sideband * sb,struct usb_host_endpoint * host_ep)250 xhci_sideband_get_endpoint_buffer(struct xhci_sideband *sb,
251 struct usb_host_endpoint *host_ep)
252 {
253 struct xhci_virt_ep *ep;
254 unsigned int ep_index;
255
256 ep_index = xhci_get_endpoint_index(&host_ep->desc);
257 ep = sb->eps[ep_index];
258
259 if (!ep || !ep->ring || !ep->sideband || ep->sideband != sb)
260 return NULL;
261
262 return xhci_ring_to_sgtable(sb, ep->ring);
263 }
264 EXPORT_SYMBOL_GPL(xhci_sideband_get_endpoint_buffer);
265
266 /**
267 * xhci_sideband_get_event_buffer - return the event buffer for this device
268 * @sb: sideband instance for this usb device
269 *
270 * If a secondary xhci interupter is set up for this usb device then this
271 * function returns the address of the event buffer where xHC writes
272 * the transfer completion events.
273 *
274 * Caller needs to free the returned sg_table
275 *
276 * Return: struct sg_table * if successful. NULL otherwise.
277 */
278 struct sg_table *
xhci_sideband_get_event_buffer(struct xhci_sideband * sb)279 xhci_sideband_get_event_buffer(struct xhci_sideband *sb)
280 {
281 if (!sb || !sb->ir)
282 return NULL;
283
284 return xhci_ring_to_sgtable(sb, sb->ir->event_ring);
285 }
286 EXPORT_SYMBOL_GPL(xhci_sideband_get_event_buffer);
287
288 /**
289 * xhci_sideband_check - check the existence of active sidebands
290 * @hcd: the host controller driver associated with the target host controller
291 *
292 * Allow other drivers, such as usb controller driver, to check if there are
293 * any sideband activity on the host controller. This information could be used
294 * for power management or other forms of resource management. The caller should
295 * ensure downstream usb devices are all either suspended or marked as
296 * "offload_at_suspend" to ensure the correctness of the return value.
297 *
298 * Returns true on any active sideband existence, false otherwise.
299 */
xhci_sideband_check(struct usb_hcd * hcd)300 bool xhci_sideband_check(struct usb_hcd *hcd)
301 {
302 struct usb_device *udev = hcd->self.root_hub;
303 bool active;
304
305 usb_lock_device(udev);
306 active = usb_offload_check(udev);
307 usb_unlock_device(udev);
308
309 return active;
310 }
311 EXPORT_SYMBOL_GPL(xhci_sideband_check);
312
313 /**
314 * xhci_sideband_create_interrupter - creates a new interrupter for this sideband
315 * @sb: sideband instance for this usb device
316 * @num_seg: number of event ring segments to allocate
317 * @ip_autoclear: IP autoclearing support such as MSI implemented
318 *
319 * Sets up a xhci interrupter that can be used for this sideband accessed usb
320 * device. Transfer events for this device can be routed to this interrupters
321 * event ring by setting the 'Interrupter Target' field correctly when queueing
322 * the transfer TRBs.
323 * Once this interrupter is created the interrupter target ID can be obtained
324 * by calling xhci_sideband_interrupter_id()
325 *
326 * Returns 0 on success, negative error otherwise
327 */
328 int
xhci_sideband_create_interrupter(struct xhci_sideband * sb,int num_seg,bool ip_autoclear,u32 imod_interval,int intr_num)329 xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg,
330 bool ip_autoclear, u32 imod_interval, int intr_num)
331 {
332 int ret = 0;
333 struct usb_device *udev;
334
335 if (!sb || !sb->xhci)
336 return -ENODEV;
337
338 guard(mutex)(&sb->mutex);
339
340 if (!sb->vdev)
341 return -ENODEV;
342
343 if (sb->ir)
344 return -EBUSY;
345
346 sb->ir = xhci_create_secondary_interrupter(xhci_to_hcd(sb->xhci),
347 num_seg, imod_interval,
348 intr_num);
349 if (!sb->ir)
350 return -ENOMEM;
351
352 udev = sb->vdev->udev;
353 ret = usb_offload_get(udev);
354
355 sb->ir->ip_autoclear = ip_autoclear;
356
357 return ret;
358 }
359 EXPORT_SYMBOL_GPL(xhci_sideband_create_interrupter);
360
361 /**
362 * xhci_sideband_remove_interrupter - remove the interrupter from a sideband
363 * @sb: sideband instance for this usb device
364 *
365 * Removes a registered interrupt for a sideband. This would allow for other
366 * sideband users to utilize this interrupter.
367 */
368 void
xhci_sideband_remove_interrupter(struct xhci_sideband * sb)369 xhci_sideband_remove_interrupter(struct xhci_sideband *sb)
370 {
371 if (!sb)
372 return;
373
374 guard(mutex)(&sb->mutex);
375
376 __xhci_sideband_remove_interrupter(sb);
377 }
378 EXPORT_SYMBOL_GPL(xhci_sideband_remove_interrupter);
379
380 /**
381 * xhci_sideband_interrupter_id - return the interrupter target id
382 * @sb: sideband instance for this usb device
383 *
384 * If a secondary xhci interrupter is set up for this usb device then this
385 * function returns the ID used by the interrupter. The sideband client
386 * needs to write this ID to the 'Interrupter Target' field of the transfer TRBs
387 * it queues on the endpoints transfer ring to ensure transfer completion event
388 * are written by xHC to the correct interrupter event ring.
389 *
390 * Returns interrupter id on success, negative error othgerwise
391 */
392 int
xhci_sideband_interrupter_id(struct xhci_sideband * sb)393 xhci_sideband_interrupter_id(struct xhci_sideband *sb)
394 {
395 if (!sb || !sb->ir)
396 return -ENODEV;
397
398 return sb->ir->intr_num;
399 }
400 EXPORT_SYMBOL_GPL(xhci_sideband_interrupter_id);
401
402 /**
403 * xhci_sideband_register - register a sideband for a usb device
404 * @intf: usb interface associated with the sideband device
405 *
406 * Allows for clients to utilize XHCI interrupters and fetch transfer and event
407 * ring parameters for executing data transfers.
408 *
409 * Return: pointer to a new xhci_sideband instance if successful. NULL otherwise.
410 */
411 struct xhci_sideband *
xhci_sideband_register(struct usb_interface * intf,enum xhci_sideband_type type,int (* notify_client)(struct usb_interface * intf,struct xhci_sideband_event * evt))412 xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type,
413 int (*notify_client)(struct usb_interface *intf,
414 struct xhci_sideband_event *evt))
415 {
416 struct usb_device *udev = interface_to_usbdev(intf);
417 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
418 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
419 struct xhci_virt_device *vdev;
420 struct xhci_sideband *sb;
421
422 /*
423 * Make sure the usb device is connected to a xhci controller. Fail
424 * registration if the type is anything other than XHCI_SIDEBAND_VENDOR,
425 * as this is the only type that is currently supported by xhci-sideband.
426 */
427 if (!udev->slot_id || type != XHCI_SIDEBAND_VENDOR)
428 return NULL;
429
430 sb = kzalloc_node(sizeof(*sb), GFP_KERNEL, dev_to_node(hcd->self.sysdev));
431 if (!sb)
432 return NULL;
433
434 mutex_init(&sb->mutex);
435
436 /* check this device isn't already controlled via sideband */
437 spin_lock_irq(&xhci->lock);
438
439 vdev = xhci->devs[udev->slot_id];
440
441 if (!vdev || vdev->sideband) {
442 xhci_warn(xhci, "XHCI sideband for slot %d already in use\n",
443 udev->slot_id);
444 spin_unlock_irq(&xhci->lock);
445 kfree(sb);
446 return NULL;
447 }
448
449 sb->xhci = xhci;
450 sb->vdev = vdev;
451 sb->intf = intf;
452 sb->type = type;
453 sb->notify_client = notify_client;
454 vdev->sideband = sb;
455
456 spin_unlock_irq(&xhci->lock);
457
458 return sb;
459 }
460 EXPORT_SYMBOL_GPL(xhci_sideband_register);
461
462 /**
463 * xhci_sideband_unregister - unregister sideband access to a usb device
464 * @sb: sideband instance to be unregistered
465 *
466 * Unregisters sideband access to a usb device and frees the sideband
467 * instance.
468 * After this the endpoint and interrupter event buffers should no longer
469 * be accessed via sideband. The xhci driver can now take over handling
470 * the buffers.
471 */
472 void
xhci_sideband_unregister(struct xhci_sideband * sb)473 xhci_sideband_unregister(struct xhci_sideband *sb)
474 {
475 struct xhci_virt_device *vdev;
476 struct xhci_hcd *xhci;
477 int i;
478
479 if (!sb)
480 return;
481
482 xhci = sb->xhci;
483
484 scoped_guard(mutex, &sb->mutex) {
485 vdev = sb->vdev;
486 if (!vdev)
487 return;
488
489 for (i = 0; i < EP_CTX_PER_DEV; i++)
490 if (sb->eps[i])
491 __xhci_sideband_remove_endpoint(sb, sb->eps[i]);
492
493 __xhci_sideband_remove_interrupter(sb);
494
495 sb->vdev = NULL;
496 }
497
498 spin_lock_irq(&xhci->lock);
499 sb->xhci = NULL;
500 vdev->sideband = NULL;
501 spin_unlock_irq(&xhci->lock);
502
503 kfree(sb);
504 }
505 EXPORT_SYMBOL_GPL(xhci_sideband_unregister);
506 MODULE_DESCRIPTION("xHCI sideband driver for secondary interrupter management");
507 MODULE_LICENSE("GPL");
508