1 // SPDX-License-Identifier: GPL-2.0
3 * Wireless Host Controller (WHC) asynchronous schedule management.
5 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
7 #include <linux/kernel.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/uwb/umc.h>
11 #include <linux/usb.h>
13 #include "../../wusbcore/wusbhc.h"
17 static void qset_get_next_prev(struct whc
*whc
, struct whc_qset
*qset
,
18 struct whc_qset
**next
, struct whc_qset
**prev
)
20 struct list_head
*n
, *p
;
22 BUG_ON(list_empty(&whc
->async_list
));
24 n
= qset
->list_node
.next
;
25 if (n
== &whc
->async_list
)
27 p
= qset
->list_node
.prev
;
28 if (p
== &whc
->async_list
)
31 *next
= container_of(n
, struct whc_qset
, list_node
);
32 *prev
= container_of(p
, struct whc_qset
, list_node
);
36 static void asl_qset_insert_begin(struct whc
*whc
, struct whc_qset
*qset
)
38 list_move(&qset
->list_node
, &whc
->async_list
);
39 qset
->in_sw_list
= true;
42 static void asl_qset_insert(struct whc
*whc
, struct whc_qset
*qset
)
44 struct whc_qset
*next
, *prev
;
46 qset_clear(whc
, qset
);
49 qset_get_next_prev(whc
, qset
, &next
, &prev
);
50 whc_qset_set_link_ptr(&qset
->qh
.link
, next
->qset_dma
);
51 whc_qset_set_link_ptr(&prev
->qh
.link
, qset
->qset_dma
);
52 qset
->in_hw_list
= true;
55 static void asl_qset_remove(struct whc
*whc
, struct whc_qset
*qset
)
57 struct whc_qset
*prev
, *next
;
59 qset_get_next_prev(whc
, qset
, &next
, &prev
);
61 list_move(&qset
->list_node
, &whc
->async_removed_list
);
62 qset
->in_sw_list
= false;
65 * No more qsets in the ASL? The caller must stop the ASL as
66 * it's no longer valid.
68 if (list_empty(&whc
->async_list
))
71 /* Remove from ASL. */
72 whc_qset_set_link_ptr(&prev
->qh
.link
, next
->qset_dma
);
73 qset
->in_hw_list
= false;
77 * process_qset - process any recently inactivated or halted qTDs in a
80 * After inactive qTDs are removed, new qTDs can be added if the
81 * urb queue still contains URBs.
83 * Returns any additional WUSBCMD bits for the ASL sync command (i.e.,
84 * WUSBCMD_ASYNC_QSET_RM if a halted qset was removed).
86 static uint32_t process_qset(struct whc
*whc
, struct whc_qset
*qset
)
88 enum whc_update update
= 0;
94 td
= &qset
->qtd
[qset
->td_start
];
95 status
= le32_to_cpu(td
->status
);
98 * Nothing to do with a still active qTD.
100 if (status
& QTD_STS_ACTIVE
)
103 if (status
& QTD_STS_HALTED
) {
105 process_halted_qtd(whc
, qset
, td
);
106 /* A halted qTD always triggers an update
107 because the qset was either removed or
109 update
|= WHC_UPDATE_UPDATED
;
113 /* Mmm, a completed qTD. */
114 process_inactive_qtd(whc
, qset
, td
);
118 update
|= qset_add_qtds(whc
, qset
);
122 * Remove this qset from the ASL if requested, but only if has
125 if (qset
->remove
&& qset
->ntds
== 0) {
126 asl_qset_remove(whc
, qset
);
127 update
|= WHC_UPDATE_REMOVED
;
132 void asl_start(struct whc
*whc
)
134 struct whc_qset
*qset
;
136 qset
= list_first_entry(&whc
->async_list
, struct whc_qset
, list_node
);
138 le_writeq(qset
->qset_dma
| QH_LINK_NTDS(8), whc
->base
+ WUSBASYNCLISTADDR
);
140 whc_write_wusbcmd(whc
, WUSBCMD_ASYNC_EN
, WUSBCMD_ASYNC_EN
);
141 whci_wait_for(&whc
->umc
->dev
, whc
->base
+ WUSBSTS
,
142 WUSBSTS_ASYNC_SCHED
, WUSBSTS_ASYNC_SCHED
,
146 void asl_stop(struct whc
*whc
)
148 whc_write_wusbcmd(whc
, WUSBCMD_ASYNC_EN
, 0);
149 whci_wait_for(&whc
->umc
->dev
, whc
->base
+ WUSBSTS
,
150 WUSBSTS_ASYNC_SCHED
, 0,
155 * asl_update - request an ASL update and wait for the hardware to be synced
157 * @wusbcmd: WUSBCMD value to start the update.
159 * If the WUSB HC is inactive (i.e., the ASL is stopped) then the
160 * update must be skipped as the hardware may not respond to update
163 void asl_update(struct whc
*whc
, uint32_t wusbcmd
)
165 struct wusbhc
*wusbhc
= &whc
->wusbhc
;
168 mutex_lock(&wusbhc
->mutex
);
169 if (wusbhc
->active
) {
170 whc_write_wusbcmd(whc
, wusbcmd
, wusbcmd
);
171 t
= wait_event_timeout(
173 (le_readl(whc
->base
+ WUSBCMD
) & WUSBCMD_ASYNC_UPDATED
) == 0,
174 msecs_to_jiffies(1000));
176 whc_hw_error(whc
, "ASL update timeout");
178 mutex_unlock(&wusbhc
->mutex
);
182 * scan_async_work - scan the ASL for qsets to process.
184 * Process each qset in the ASL in turn and then signal the WHC that
185 * the ASL has been updated.
187 * Then start, stop or update the asynchronous schedule as required.
189 void scan_async_work(struct work_struct
*work
)
191 struct whc
*whc
= container_of(work
, struct whc
, async_work
);
192 struct whc_qset
*qset
, *t
;
193 enum whc_update update
= 0;
195 spin_lock_irq(&whc
->lock
);
198 * Transerve the software list backwards so new qsets can be
199 * safely inserted into the ASL without making it non-circular.
201 list_for_each_entry_safe_reverse(qset
, t
, &whc
->async_list
, list_node
) {
202 if (!qset
->in_hw_list
) {
203 asl_qset_insert(whc
, qset
);
204 update
|= WHC_UPDATE_ADDED
;
207 update
|= process_qset(whc
, qset
);
210 spin_unlock_irq(&whc
->lock
);
213 uint32_t wusbcmd
= WUSBCMD_ASYNC_UPDATED
| WUSBCMD_ASYNC_SYNCED_DB
;
214 if (update
& WHC_UPDATE_REMOVED
)
215 wusbcmd
|= WUSBCMD_ASYNC_QSET_RM
;
216 asl_update(whc
, wusbcmd
);
220 * Now that the ASL is updated, complete the removal of any
223 * If the qset was to be reset, do so and reinsert it into the
224 * ASL if it has pending transfers.
226 spin_lock_irq(&whc
->lock
);
228 list_for_each_entry_safe(qset
, t
, &whc
->async_removed_list
, list_node
) {
229 qset_remove_complete(whc
, qset
);
231 qset_reset(whc
, qset
);
232 if (!list_empty(&qset
->stds
)) {
233 asl_qset_insert_begin(whc
, qset
);
234 queue_work(whc
->workqueue
, &whc
->async_work
);
239 spin_unlock_irq(&whc
->lock
);
243 * asl_urb_enqueue - queue an URB onto the asynchronous list (ASL).
244 * @whc: the WHCI host controller
245 * @urb: the URB to enqueue
246 * @mem_flags: flags for any memory allocations
248 * The qset for the endpoint is obtained and the urb queued on to it.
250 * Work is scheduled to update the hardware's view of the ASL.
252 int asl_urb_enqueue(struct whc
*whc
, struct urb
*urb
, gfp_t mem_flags
)
254 struct whc_qset
*qset
;
258 spin_lock_irqsave(&whc
->lock
, flags
);
260 err
= usb_hcd_link_urb_to_ep(&whc
->wusbhc
.usb_hcd
, urb
);
262 spin_unlock_irqrestore(&whc
->lock
, flags
);
266 qset
= get_qset(whc
, urb
, GFP_ATOMIC
);
270 err
= qset_add_urb(whc
, qset
, urb
, GFP_ATOMIC
);
272 if (!qset
->in_sw_list
&& !qset
->remove
)
273 asl_qset_insert_begin(whc
, qset
);
275 usb_hcd_unlink_urb_from_ep(&whc
->wusbhc
.usb_hcd
, urb
);
277 spin_unlock_irqrestore(&whc
->lock
, flags
);
280 queue_work(whc
->workqueue
, &whc
->async_work
);
286 * asl_urb_dequeue - remove an URB (qset) from the async list.
287 * @whc: the WHCI host controller
288 * @urb: the URB to dequeue
289 * @status: the current status of the URB
291 * URBs that do yet have qTDs can simply be removed from the software
292 * queue, otherwise the qset must be removed from the ASL so the qTDs
295 int asl_urb_dequeue(struct whc
*whc
, struct urb
*urb
, int status
)
297 struct whc_urb
*wurb
= urb
->hcpriv
;
298 struct whc_qset
*qset
= wurb
->qset
;
299 struct whc_std
*std
, *t
;
300 bool has_qtd
= false;
304 spin_lock_irqsave(&whc
->lock
, flags
);
306 ret
= usb_hcd_check_unlink_urb(&whc
->wusbhc
.usb_hcd
, urb
, status
);
310 list_for_each_entry_safe(std
, t
, &qset
->stds
, list_node
) {
311 if (std
->urb
== urb
) {
314 qset_free_std(whc
, std
);
316 std
->qtd
= NULL
; /* so this std is re-added when the qset is */
320 asl_qset_remove(whc
, qset
);
321 wurb
->status
= status
;
322 wurb
->is_async
= true;
323 queue_work(whc
->workqueue
, &wurb
->dequeue_work
);
325 qset_remove_urb(whc
, qset
, urb
, status
);
327 spin_unlock_irqrestore(&whc
->lock
, flags
);
333 * asl_qset_delete - delete a qset from the ASL
335 void asl_qset_delete(struct whc
*whc
, struct whc_qset
*qset
)
338 queue_work(whc
->workqueue
, &whc
->async_work
);
339 qset_delete(whc
, qset
);
343 * asl_init - initialize the asynchronous schedule list
345 * A dummy qset with no qTDs is added to the ASL to simplify removing
346 * qsets (no need to stop the ASL when the last qset is removed).
348 int asl_init(struct whc
*whc
)
350 struct whc_qset
*qset
;
352 qset
= qset_alloc(whc
, GFP_KERNEL
);
356 asl_qset_insert_begin(whc
, qset
);
357 asl_qset_insert(whc
, qset
);
363 * asl_clean_up - free ASL resources
365 * The ASL is stopped and empty except for the dummy qset.
367 void asl_clean_up(struct whc
*whc
)
369 struct whc_qset
*qset
;
371 if (!list_empty(&whc
->async_list
)) {
372 qset
= list_first_entry(&whc
->async_list
, struct whc_qset
, list_node
);
373 list_del(&qset
->list_node
);
374 qset_free(whc
, qset
);