Merge tag 'usb-4.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[sfrench/cifs-2.6.git] / drivers / usb / host / whci / pzl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Wireless Host Controller (WHC) periodic 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 update_pzl_pointers(struct whc *whc, int period, u64 addr)
18 {
19         switch (period) {
20         case 0:
21                 whc_qset_set_link_ptr(&whc->pz_list[0], addr);
22                 whc_qset_set_link_ptr(&whc->pz_list[2], addr);
23                 whc_qset_set_link_ptr(&whc->pz_list[4], addr);
24                 whc_qset_set_link_ptr(&whc->pz_list[6], addr);
25                 whc_qset_set_link_ptr(&whc->pz_list[8], addr);
26                 whc_qset_set_link_ptr(&whc->pz_list[10], addr);
27                 whc_qset_set_link_ptr(&whc->pz_list[12], addr);
28                 whc_qset_set_link_ptr(&whc->pz_list[14], addr);
29                 break;
30         case 1:
31                 whc_qset_set_link_ptr(&whc->pz_list[1], addr);
32                 whc_qset_set_link_ptr(&whc->pz_list[5], addr);
33                 whc_qset_set_link_ptr(&whc->pz_list[9], addr);
34                 whc_qset_set_link_ptr(&whc->pz_list[13], addr);
35                 break;
36         case 2:
37                 whc_qset_set_link_ptr(&whc->pz_list[3], addr);
38                 whc_qset_set_link_ptr(&whc->pz_list[11], addr);
39                 break;
40         case 3:
41                 whc_qset_set_link_ptr(&whc->pz_list[7], addr);
42                 break;
43         case 4:
44                 whc_qset_set_link_ptr(&whc->pz_list[15], addr);
45                 break;
46         }
47 }
48
49 /*
50  * Return the 'period' to use for this qset.  The minimum interval for
51  * the endpoint is used so whatever urbs are submitted the device is
52  * polled often enough.
53  */
54 static int qset_get_period(struct whc *whc, struct whc_qset *qset)
55 {
56         uint8_t bInterval = qset->ep->desc.bInterval;
57
58         if (bInterval < 6)
59                 bInterval = 6;
60         if (bInterval > 10)
61                 bInterval = 10;
62         return bInterval - 6;
63 }
64
65 static void qset_insert_in_sw_list(struct whc *whc, struct whc_qset *qset)
66 {
67         int period;
68
69         period = qset_get_period(whc, qset);
70
71         qset_clear(whc, qset);
72         list_move(&qset->list_node, &whc->periodic_list[period]);
73         qset->in_sw_list = true;
74 }
75
76 static void pzl_qset_remove(struct whc *whc, struct whc_qset *qset)
77 {
78         list_move(&qset->list_node, &whc->periodic_removed_list);
79         qset->in_hw_list = false;
80         qset->in_sw_list = false;
81 }
82
83 /**
84  * pzl_process_qset - process any recently inactivated or halted qTDs
85  * in a qset.
86  *
87  * After inactive qTDs are removed, new qTDs can be added if the
88  * urb queue still contains URBs.
89  *
90  * Returns the schedule updates required.
91  */
92 static enum whc_update pzl_process_qset(struct whc *whc, struct whc_qset *qset)
93 {
94         enum whc_update update = 0;
95         uint32_t status = 0;
96
97         while (qset->ntds) {
98                 struct whc_qtd *td;
99                 int t;
100
101                 t = qset->td_start;
102                 td = &qset->qtd[qset->td_start];
103                 status = le32_to_cpu(td->status);
104
105                 /*
106                  * Nothing to do with a still active qTD.
107                  */
108                 if (status & QTD_STS_ACTIVE)
109                         break;
110
111                 if (status & QTD_STS_HALTED) {
112                         /* Ug, an error. */
113                         process_halted_qtd(whc, qset, td);
114                         /* A halted qTD always triggers an update
115                            because the qset was either removed or
116                            reactivated. */
117                         update |= WHC_UPDATE_UPDATED;
118                         goto done;
119                 }
120
121                 /* Mmm, a completed qTD. */
122                 process_inactive_qtd(whc, qset, td);
123         }
124
125         if (!qset->remove)
126                 update |= qset_add_qtds(whc, qset);
127
128 done:
129         /*
130          * If there are no qTDs in this qset, remove it from the PZL.
131          */
132         if (qset->remove && qset->ntds == 0) {
133                 pzl_qset_remove(whc, qset);
134                 update |= WHC_UPDATE_REMOVED;
135         }
136
137         return update;
138 }
139
140 /**
141  * pzl_start - start the periodic schedule
142  * @whc: the WHCI host controller
143  *
144  * The PZL must be valid (e.g., all entries in the list should have
145  * the T bit set).
146  */
147 void pzl_start(struct whc *whc)
148 {
149         le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
150
151         whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, WUSBCMD_PERIODIC_EN);
152         whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
153                       WUSBSTS_PERIODIC_SCHED, WUSBSTS_PERIODIC_SCHED,
154                       1000, "start PZL");
155 }
156
157 /**
158  * pzl_stop - stop the periodic schedule
159  * @whc: the WHCI host controller
160  */
161 void pzl_stop(struct whc *whc)
162 {
163         whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, 0);
164         whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
165                       WUSBSTS_PERIODIC_SCHED, 0,
166                       1000, "stop PZL");
167 }
168
169 /**
170  * pzl_update - request a PZL update and wait for the hardware to be synced
171  * @whc: the WHCI HC
172  * @wusbcmd: WUSBCMD value to start the update.
173  *
174  * If the WUSB HC is inactive (i.e., the PZL is stopped) then the
175  * update must be skipped as the hardware may not respond to update
176  * requests.
177  */
178 void pzl_update(struct whc *whc, uint32_t wusbcmd)
179 {
180         struct wusbhc *wusbhc = &whc->wusbhc;
181         long t;
182
183         mutex_lock(&wusbhc->mutex);
184         if (wusbhc->active) {
185                 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
186                 t = wait_event_timeout(
187                         whc->periodic_list_wq,
188                         (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0,
189                         msecs_to_jiffies(1000));
190                 if (t == 0)
191                         whc_hw_error(whc, "PZL update timeout");
192         }
193         mutex_unlock(&wusbhc->mutex);
194 }
195
196 static void update_pzl_hw_view(struct whc *whc)
197 {
198         struct whc_qset *qset, *t;
199         int period;
200         u64 tmp_qh = 0;
201
202         for (period = 0; period < 5; period++) {
203                 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
204                         whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
205                         tmp_qh = qset->qset_dma;
206                         qset->in_hw_list = true;
207                 }
208                 update_pzl_pointers(whc, period, tmp_qh);
209         }
210 }
211
212 /**
213  * scan_periodic_work - scan the PZL for qsets to process.
214  *
215  * Process each qset in the PZL in turn and then signal the WHC that
216  * the PZL has been updated.
217  *
218  * Then start, stop or update the periodic schedule as required.
219  */
220 void scan_periodic_work(struct work_struct *work)
221 {
222         struct whc *whc = container_of(work, struct whc, periodic_work);
223         struct whc_qset *qset, *t;
224         enum whc_update update = 0;
225         int period;
226
227         spin_lock_irq(&whc->lock);
228
229         for (period = 4; period >= 0; period--) {
230                 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
231                         if (!qset->in_hw_list)
232                                 update |= WHC_UPDATE_ADDED;
233                         update |= pzl_process_qset(whc, qset);
234                 }
235         }
236
237         if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
238                 update_pzl_hw_view(whc);
239
240         spin_unlock_irq(&whc->lock);
241
242         if (update) {
243                 uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
244                 if (update & WHC_UPDATE_REMOVED)
245                         wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
246                 pzl_update(whc, wusbcmd);
247         }
248
249         /*
250          * Now that the PZL is updated, complete the removal of any
251          * removed qsets.
252          *
253          * If the qset was to be reset, do so and reinsert it into the
254          * PZL if it has pending transfers.
255          */
256         spin_lock_irq(&whc->lock);
257
258         list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
259                 qset_remove_complete(whc, qset);
260                 if (qset->reset) {
261                         qset_reset(whc, qset);
262                         if (!list_empty(&qset->stds)) {
263                                 qset_insert_in_sw_list(whc, qset);
264                                 queue_work(whc->workqueue, &whc->periodic_work);
265                         }
266                 }
267         }
268
269         spin_unlock_irq(&whc->lock);
270 }
271
272 /**
273  * pzl_urb_enqueue - queue an URB onto the periodic list (PZL)
274  * @whc: the WHCI host controller
275  * @urb: the URB to enqueue
276  * @mem_flags: flags for any memory allocations
277  *
278  * The qset for the endpoint is obtained and the urb queued on to it.
279  *
280  * Work is scheduled to update the hardware's view of the PZL.
281  */
282 int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
283 {
284         struct whc_qset *qset;
285         int err;
286         unsigned long flags;
287
288         spin_lock_irqsave(&whc->lock, flags);
289
290         err = usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
291         if (err < 0) {
292                 spin_unlock_irqrestore(&whc->lock, flags);
293                 return err;
294         }
295
296         qset = get_qset(whc, urb, GFP_ATOMIC);
297         if (qset == NULL)
298                 err = -ENOMEM;
299         else
300                 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
301         if (!err) {
302                 if (!qset->in_sw_list && !qset->remove)
303                         qset_insert_in_sw_list(whc, qset);
304         } else
305                 usb_hcd_unlink_urb_from_ep(&whc->wusbhc.usb_hcd, urb);
306
307         spin_unlock_irqrestore(&whc->lock, flags);
308
309         if (!err)
310                 queue_work(whc->workqueue, &whc->periodic_work);
311
312         return err;
313 }
314
315 /**
316  * pzl_urb_dequeue - remove an URB (qset) from the periodic list
317  * @whc: the WHCI host controller
318  * @urb: the URB to dequeue
319  * @status: the current status of the URB
320  *
321  * URBs that do yet have qTDs can simply be removed from the software
322  * queue, otherwise the qset must be removed so the qTDs can be safely
323  * removed.
324  */
325 int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
326 {
327         struct whc_urb *wurb = urb->hcpriv;
328         struct whc_qset *qset = wurb->qset;
329         struct whc_std *std, *t;
330         bool has_qtd = false;
331         int ret;
332         unsigned long flags;
333
334         spin_lock_irqsave(&whc->lock, flags);
335
336         ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
337         if (ret < 0)
338                 goto out;
339
340         list_for_each_entry_safe(std, t, &qset->stds, list_node) {
341                 if (std->urb == urb) {
342                         if (std->qtd)
343                                 has_qtd = true;
344                         qset_free_std(whc, std);
345                 } else
346                         std->qtd = NULL; /* so this std is re-added when the qset is */
347         }
348
349         if (has_qtd) {
350                 pzl_qset_remove(whc, qset);
351                 update_pzl_hw_view(whc);
352                 wurb->status = status;
353                 wurb->is_async = false;
354                 queue_work(whc->workqueue, &wurb->dequeue_work);
355         } else
356                 qset_remove_urb(whc, qset, urb, status);
357 out:
358         spin_unlock_irqrestore(&whc->lock, flags);
359
360         return ret;
361 }
362
363 /**
364  * pzl_qset_delete - delete a qset from the PZL
365  */
366 void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
367 {
368         qset->remove = 1;
369         queue_work(whc->workqueue, &whc->periodic_work);
370         qset_delete(whc, qset);
371 }
372
373 /**
374  * pzl_init - initialize the periodic zone list
375  * @whc: the WHCI host controller
376  */
377 int pzl_init(struct whc *whc)
378 {
379         int i;
380
381         whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
382                                           &whc->pz_list_dma, GFP_KERNEL);
383         if (whc->pz_list == NULL)
384                 return -ENOMEM;
385
386         /* Set T bit on all elements in PZL. */
387         for (i = 0; i < 16; i++)
388                 whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
389
390         le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
391
392         return 0;
393 }
394
395 /**
396  * pzl_clean_up - free PZL resources
397  * @whc: the WHCI host controller
398  *
399  * The PZL is stopped and empty.
400  */
401 void pzl_clean_up(struct whc *whc)
402 {
403         if (whc->pz_list)
404                 dma_free_coherent(&whc->umc->dev,  sizeof(u64) * 16, whc->pz_list,
405                                   whc->pz_list_dma);
406 }