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