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