]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/usb/host/whci/asl.c
USB: host: whci: Remove redundant license text
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / host / whci / asl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Wireless Host Controller (WHC) asynchronous schedule management.
4 *
5 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
6 */
7 #include <linux/kernel.h>
8 #include <linux/gfp.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/uwb/umc.h>
11 #include <linux/usb.h>
12
13 #include "../../wusbcore/wusbhc.h"
14
15 #include "whcd.h"
16
17 static void qset_get_next_prev(struct whc *whc, struct whc_qset *qset,
18 struct whc_qset **next, struct whc_qset **prev)
19 {
20 struct list_head *n, *p;
21
22 BUG_ON(list_empty(&whc->async_list));
23
24 n = qset->list_node.next;
25 if (n == &whc->async_list)
26 n = n->next;
27 p = qset->list_node.prev;
28 if (p == &whc->async_list)
29 p = p->prev;
30
31 *next = container_of(n, struct whc_qset, list_node);
32 *prev = container_of(p, struct whc_qset, list_node);
33
34 }
35
36 static void asl_qset_insert_begin(struct whc *whc, struct whc_qset *qset)
37 {
38 list_move(&qset->list_node, &whc->async_list);
39 qset->in_sw_list = true;
40 }
41
42 static void asl_qset_insert(struct whc *whc, struct whc_qset *qset)
43 {
44 struct whc_qset *next, *prev;
45
46 qset_clear(whc, qset);
47
48 /* Link into ASL. */
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;
53 }
54
55 static void asl_qset_remove(struct whc *whc, struct whc_qset *qset)
56 {
57 struct whc_qset *prev, *next;
58
59 qset_get_next_prev(whc, qset, &next, &prev);
60
61 list_move(&qset->list_node, &whc->async_removed_list);
62 qset->in_sw_list = false;
63
64 /*
65 * No more qsets in the ASL? The caller must stop the ASL as
66 * it's no longer valid.
67 */
68 if (list_empty(&whc->async_list))
69 return;
70
71 /* Remove from ASL. */
72 whc_qset_set_link_ptr(&prev->qh.link, next->qset_dma);
73 qset->in_hw_list = false;
74 }
75
76 /**
77 * process_qset - process any recently inactivated or halted qTDs in a
78 * qset.
79 *
80 * After inactive qTDs are removed, new qTDs can be added if the
81 * urb queue still contains URBs.
82 *
83 * Returns any additional WUSBCMD bits for the ASL sync command (i.e.,
84 * WUSBCMD_ASYNC_QSET_RM if a halted qset was removed).
85 */
86 static uint32_t process_qset(struct whc *whc, struct whc_qset *qset)
87 {
88 enum whc_update update = 0;
89 uint32_t status = 0;
90
91 while (qset->ntds) {
92 struct whc_qtd *td;
93 int t;
94
95 t = qset->td_start;
96 td = &qset->qtd[qset->td_start];
97 status = le32_to_cpu(td->status);
98
99 /*
100 * Nothing to do with a still active qTD.
101 */
102 if (status & QTD_STS_ACTIVE)
103 break;
104
105 if (status & QTD_STS_HALTED) {
106 /* Ug, an error. */
107 process_halted_qtd(whc, qset, td);
108 /* A halted qTD always triggers an update
109 because the qset was either removed or
110 reactivated. */
111 update |= WHC_UPDATE_UPDATED;
112 goto done;
113 }
114
115 /* Mmm, a completed qTD. */
116 process_inactive_qtd(whc, qset, td);
117 }
118
119 if (!qset->remove)
120 update |= qset_add_qtds(whc, qset);
121
122 done:
123 /*
124 * Remove this qset from the ASL if requested, but only if has
125 * no qTDs.
126 */
127 if (qset->remove && qset->ntds == 0) {
128 asl_qset_remove(whc, qset);
129 update |= WHC_UPDATE_REMOVED;
130 }
131 return update;
132 }
133
134 void asl_start(struct whc *whc)
135 {
136 struct whc_qset *qset;
137
138 qset = list_first_entry(&whc->async_list, struct whc_qset, list_node);
139
140 le_writeq(qset->qset_dma | QH_LINK_NTDS(8), whc->base + WUSBASYNCLISTADDR);
141
142 whc_write_wusbcmd(whc, WUSBCMD_ASYNC_EN, WUSBCMD_ASYNC_EN);
143 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
144 WUSBSTS_ASYNC_SCHED, WUSBSTS_ASYNC_SCHED,
145 1000, "start ASL");
146 }
147
148 void asl_stop(struct whc *whc)
149 {
150 whc_write_wusbcmd(whc, WUSBCMD_ASYNC_EN, 0);
151 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
152 WUSBSTS_ASYNC_SCHED, 0,
153 1000, "stop ASL");
154 }
155
156 /**
157 * asl_update - request an ASL update and wait for the hardware to be synced
158 * @whc: the WHCI HC
159 * @wusbcmd: WUSBCMD value to start the update.
160 *
161 * If the WUSB HC is inactive (i.e., the ASL is stopped) then the
162 * update must be skipped as the hardware may not respond to update
163 * requests.
164 */
165 void asl_update(struct whc *whc, uint32_t wusbcmd)
166 {
167 struct wusbhc *wusbhc = &whc->wusbhc;
168 long t;
169
170 mutex_lock(&wusbhc->mutex);
171 if (wusbhc->active) {
172 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
173 t = wait_event_timeout(
174 whc->async_list_wq,
175 (le_readl(whc->base + WUSBCMD) & WUSBCMD_ASYNC_UPDATED) == 0,
176 msecs_to_jiffies(1000));
177 if (t == 0)
178 whc_hw_error(whc, "ASL update timeout");
179 }
180 mutex_unlock(&wusbhc->mutex);
181 }
182
183 /**
184 * scan_async_work - scan the ASL for qsets to process.
185 *
186 * Process each qset in the ASL in turn and then signal the WHC that
187 * the ASL has been updated.
188 *
189 * Then start, stop or update the asynchronous schedule as required.
190 */
191 void scan_async_work(struct work_struct *work)
192 {
193 struct whc *whc = container_of(work, struct whc, async_work);
194 struct whc_qset *qset, *t;
195 enum whc_update update = 0;
196
197 spin_lock_irq(&whc->lock);
198
199 /*
200 * Transerve the software list backwards so new qsets can be
201 * safely inserted into the ASL without making it non-circular.
202 */
203 list_for_each_entry_safe_reverse(qset, t, &whc->async_list, list_node) {
204 if (!qset->in_hw_list) {
205 asl_qset_insert(whc, qset);
206 update |= WHC_UPDATE_ADDED;
207 }
208
209 update |= process_qset(whc, qset);
210 }
211
212 spin_unlock_irq(&whc->lock);
213
214 if (update) {
215 uint32_t wusbcmd = WUSBCMD_ASYNC_UPDATED | WUSBCMD_ASYNC_SYNCED_DB;
216 if (update & WHC_UPDATE_REMOVED)
217 wusbcmd |= WUSBCMD_ASYNC_QSET_RM;
218 asl_update(whc, wusbcmd);
219 }
220
221 /*
222 * Now that the ASL is updated, complete the removal of any
223 * removed qsets.
224 *
225 * If the qset was to be reset, do so and reinsert it into the
226 * ASL if it has pending transfers.
227 */
228 spin_lock_irq(&whc->lock);
229
230 list_for_each_entry_safe(qset, t, &whc->async_removed_list, list_node) {
231 qset_remove_complete(whc, qset);
232 if (qset->reset) {
233 qset_reset(whc, qset);
234 if (!list_empty(&qset->stds)) {
235 asl_qset_insert_begin(whc, qset);
236 queue_work(whc->workqueue, &whc->async_work);
237 }
238 }
239 }
240
241 spin_unlock_irq(&whc->lock);
242 }
243
244 /**
245 * asl_urb_enqueue - queue an URB onto the asynchronous list (ASL).
246 * @whc: the WHCI host controller
247 * @urb: the URB to enqueue
248 * @mem_flags: flags for any memory allocations
249 *
250 * The qset for the endpoint is obtained and the urb queued on to it.
251 *
252 * Work is scheduled to update the hardware's view of the ASL.
253 */
254 int asl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
255 {
256 struct whc_qset *qset;
257 int err;
258 unsigned long flags;
259
260 spin_lock_irqsave(&whc->lock, flags);
261
262 err = usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
263 if (err < 0) {
264 spin_unlock_irqrestore(&whc->lock, flags);
265 return err;
266 }
267
268 qset = get_qset(whc, urb, GFP_ATOMIC);
269 if (qset == NULL)
270 err = -ENOMEM;
271 else
272 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
273 if (!err) {
274 if (!qset->in_sw_list && !qset->remove)
275 asl_qset_insert_begin(whc, qset);
276 } else
277 usb_hcd_unlink_urb_from_ep(&whc->wusbhc.usb_hcd, urb);
278
279 spin_unlock_irqrestore(&whc->lock, flags);
280
281 if (!err)
282 queue_work(whc->workqueue, &whc->async_work);
283
284 return err;
285 }
286
287 /**
288 * asl_urb_dequeue - remove an URB (qset) from the async list.
289 * @whc: the WHCI host controller
290 * @urb: the URB to dequeue
291 * @status: the current status of the URB
292 *
293 * URBs that do yet have qTDs can simply be removed from the software
294 * queue, otherwise the qset must be removed from the ASL so the qTDs
295 * can be removed.
296 */
297 int asl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
298 {
299 struct whc_urb *wurb = urb->hcpriv;
300 struct whc_qset *qset = wurb->qset;
301 struct whc_std *std, *t;
302 bool has_qtd = false;
303 int ret;
304 unsigned long flags;
305
306 spin_lock_irqsave(&whc->lock, flags);
307
308 ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
309 if (ret < 0)
310 goto out;
311
312 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
313 if (std->urb == urb) {
314 if (std->qtd)
315 has_qtd = true;
316 qset_free_std(whc, std);
317 } else
318 std->qtd = NULL; /* so this std is re-added when the qset is */
319 }
320
321 if (has_qtd) {
322 asl_qset_remove(whc, qset);
323 wurb->status = status;
324 wurb->is_async = true;
325 queue_work(whc->workqueue, &wurb->dequeue_work);
326 } else
327 qset_remove_urb(whc, qset, urb, status);
328 out:
329 spin_unlock_irqrestore(&whc->lock, flags);
330
331 return ret;
332 }
333
334 /**
335 * asl_qset_delete - delete a qset from the ASL
336 */
337 void asl_qset_delete(struct whc *whc, struct whc_qset *qset)
338 {
339 qset->remove = 1;
340 queue_work(whc->workqueue, &whc->async_work);
341 qset_delete(whc, qset);
342 }
343
344 /**
345 * asl_init - initialize the asynchronous schedule list
346 *
347 * A dummy qset with no qTDs is added to the ASL to simplify removing
348 * qsets (no need to stop the ASL when the last qset is removed).
349 */
350 int asl_init(struct whc *whc)
351 {
352 struct whc_qset *qset;
353
354 qset = qset_alloc(whc, GFP_KERNEL);
355 if (qset == NULL)
356 return -ENOMEM;
357
358 asl_qset_insert_begin(whc, qset);
359 asl_qset_insert(whc, qset);
360
361 return 0;
362 }
363
364 /**
365 * asl_clean_up - free ASL resources
366 *
367 * The ASL is stopped and empty except for the dummy qset.
368 */
369 void asl_clean_up(struct whc *whc)
370 {
371 struct whc_qset *qset;
372
373 if (!list_empty(&whc->async_list)) {
374 qset = list_first_entry(&whc->async_list, struct whc_qset, list_node);
375 list_del(&qset->list_node);
376 qset_free(whc, qset);
377 }
378 }