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