source: mainline/uspace/drv/bus/usb/uhci/hc.c@ 217e981

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 217e981 was 129b821f, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhub: be aware of its own speed

This resulted in a bunch of changes just because the roothubs in older
HC's are virtual, and need to be aware of their own speed too.

  • Property mode set to 100644
File size: 17.5 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup drvusbuhcihc
30 * @{
31 */
32/** @file
33 * @brief UHCI Host controller driver routines
34 */
35
36#include <adt/list.h>
37#include <assert.h>
38#include <async.h>
39#include <ddi.h>
40#include <device/hw_res_parsed.h>
41#include <fibril.h>
42#include <errno.h>
43#include <macros.h>
44#include <mem.h>
45#include <stdlib.h>
46#include <stdint.h>
47#include <str_error.h>
48
49#include <usb/debug.h>
50#include <usb/usb.h>
51#include <usb/host/utils/malloc32.h>
52#include <usb/host/bandwidth.h>
53#include <usb/host/utility.h>
54
55#include "uhci_batch.h"
56#include "transfer_list.h"
57#include "hc.h"
58
59#define UHCI_INTR_ALLOW_INTERRUPTS \
60 (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
61#define UHCI_STATUS_USED_INTERRUPTS \
62 (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
63
64static const irq_pio_range_t uhci_irq_pio_ranges[] = {
65 {
66 .base = 0,
67 .size = sizeof(uhci_regs_t)
68 }
69};
70
71static const irq_cmd_t uhci_irq_commands[] = {
72 {
73 .cmd = CMD_PIO_READ_16,
74 .dstarg = 1,
75 .addr = NULL
76 },
77 {
78 .cmd = CMD_AND,
79 .srcarg = 1,
80 .dstarg = 2,
81 .value = UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS
82 },
83 {
84 .cmd = CMD_PREDICATE,
85 .srcarg = 2,
86 .value = 2
87 },
88 {
89 .cmd = CMD_PIO_WRITE_A_16,
90 .srcarg = 1,
91 .addr = NULL
92 },
93 {
94 .cmd = CMD_ACCEPT
95 }
96};
97
98static void hc_init_hw(const hc_t *instance);
99static int hc_init_mem_structures(hc_t *instance, hc_device_t *);
100static int hc_init_transfer_lists(hc_t *instance);
101
102static int hc_debug_checker(void *arg);
103
104
105/** Generate IRQ code.
106 * @param[out] code IRQ code structure.
107 * @param[in] hw_res Device's resources.
108 *
109 * @return Error code.
110 */
111int hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
112{
113 assert(code);
114 assert(hw_res);
115
116 if (hw_res->irqs.count != 1 || hw_res->io_ranges.count != 1)
117 return EINVAL;
118 const addr_range_t regs = hw_res->io_ranges.ranges[0];
119
120 if (RNGSZ(regs) < sizeof(uhci_regs_t))
121 return EOVERFLOW;
122
123 code->ranges = malloc(sizeof(uhci_irq_pio_ranges));
124 if (code->ranges == NULL)
125 return ENOMEM;
126
127 code->cmds = malloc(sizeof(uhci_irq_commands));
128 if (code->cmds == NULL) {
129 free(code->ranges);
130 return ENOMEM;
131 }
132
133 code->rangecount = ARRAY_SIZE(uhci_irq_pio_ranges);
134 code->cmdcount = ARRAY_SIZE(uhci_irq_commands);
135
136 memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
137 code->ranges[0].base = RNGABS(regs);
138
139 memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
140 uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(regs);
141 code->cmds[0].addr = (void*)&registers->usbsts;
142 code->cmds[3].addr = (void*)&registers->usbsts;
143
144 usb_log_debug("I/O regs at %p (size %zu), IRQ %d.",
145 RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
146
147 return hw_res->irqs.irqs[0];
148}
149
150/** Take action based on the interrupt cause.
151 *
152 * @param[in] hcd HCD structure to use.
153 * @param[in] status Value of the status register at the time of interrupt.
154 *
155 * Interrupt might indicate:
156 * - transaction completed, either by triggering IOC, SPD, or an error
157 * - some kind of device error
158 * - resume from suspend state (not implemented)
159 */
160static void hc_interrupt(bus_t *bus, uint32_t status)
161{
162 hc_t *instance = bus_to_hc(bus);
163
164 /* Lower 2 bits are transaction error and transaction complete */
165 if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
166 LIST_INITIALIZE(done);
167 transfer_list_remove_finished(
168 &instance->transfers_interrupt, &done);
169 transfer_list_remove_finished(
170 &instance->transfers_control_slow, &done);
171 transfer_list_remove_finished(
172 &instance->transfers_control_full, &done);
173 transfer_list_remove_finished(
174 &instance->transfers_bulk_full, &done);
175
176 list_foreach_safe(done, current, next) {
177 list_remove(current);
178 uhci_transfer_batch_t *batch =
179 uhci_transfer_batch_from_link(current);
180 usb_transfer_batch_finish(&batch->base);
181 }
182 }
183 /* Resume interrupts are not supported */
184 if (status & UHCI_STATUS_RESUME) {
185 usb_log_error("Resume interrupt!");
186 }
187
188 /* Bits 4 and 5 indicate hc error */
189 if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
190 usb_log_error("UHCI hardware failure!.");
191 ++instance->hw_failures;
192 transfer_list_abort_all(&instance->transfers_interrupt);
193 transfer_list_abort_all(&instance->transfers_control_slow);
194 transfer_list_abort_all(&instance->transfers_control_full);
195 transfer_list_abort_all(&instance->transfers_bulk_full);
196
197 if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
198 /* reinitialize hw, this triggers virtual disconnect*/
199 hc_init_hw(instance);
200 } else {
201 usb_log_fatal("Too many UHCI hardware failures!.");
202 hc_gone(&instance->base);
203 }
204 }
205}
206
207/** Initialize UHCI hc driver structure
208 *
209 * @param[in] instance Memory place to initialize.
210 * @param[in] regs Range of device's I/O control registers.
211 * @param[in] interrupts True if hw interrupts should be used.
212 * @return Error code.
213 * @note Should be called only once on any structure.
214 *
215 * Initializes memory structures, starts up hw, and launches debugger and
216 * interrupt fibrils.
217 */
218int hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
219{
220 hc_t *instance = hcd_to_hc(hcd);
221 assert(hw_res);
222 if (hw_res->io_ranges.count != 1 ||
223 hw_res->io_ranges.ranges[0].size < sizeof(uhci_regs_t))
224 return EINVAL;
225
226 instance->hw_failures = 0;
227
228 /* allow access to hc control registers */
229 int ret = pio_enable_range(&hw_res->io_ranges.ranges[0],
230 (void **) &instance->registers);
231 if (ret != EOK) {
232 usb_log_error("Failed to gain access to registers: %s.",
233 str_error(ret));
234 return ret;
235 }
236
237 usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
238 hw_res->io_ranges.ranges[0].address.absolute,
239 hw_res->io_ranges.ranges[0].size);
240
241 ret = hc_init_mem_structures(instance, hcd);
242 if (ret != EOK) {
243 usb_log_error("Failed to init UHCI memory structures: %s.",
244 str_error(ret));
245 // TODO: we should disable pio here
246 return ret;
247 }
248
249 return EOK;
250}
251
252int hc_start(hc_device_t *hcd)
253{
254 hc_t *instance = hcd_to_hc(hcd);
255 hc_init_hw(instance);
256 (void)hc_debug_checker;
257
258 return uhci_rh_init(&instance->rh, instance->registers->ports, "uhci");
259}
260
261int hc_setup_roothub(hc_device_t *hcd)
262{
263 return hc_setup_virtual_root_hub(hcd, USB_SPEED_FULL);
264}
265
266/** Safely dispose host controller internal structures
267 *
268 * @param[in] instance Host controller structure to use.
269 */
270int hc_gone(hc_device_t *instance)
271{
272 assert(instance);
273 //TODO Implement
274 return ENOTSUP;
275}
276
277/** Initialize UHCI hc hw resources.
278 *
279 * @param[in] instance UHCI structure to use.
280 * For magic values see UHCI Design Guide
281 */
282void hc_init_hw(const hc_t *instance)
283{
284 assert(instance);
285 uhci_regs_t *registers = instance->registers;
286
287 /* Reset everything, who knows what touched it before us */
288 pio_write_16(&registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
289 async_usleep(50000); /* 50ms according to USB spec(root hub reset) */
290 pio_write_16(&registers->usbcmd, 0);
291
292 /* Reset hc, all states and counters. Hope that hw is not broken */
293 pio_write_16(&registers->usbcmd, UHCI_CMD_HCRESET);
294 do { async_usleep(10); }
295 while ((pio_read_16(&registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
296
297 /* Set frame to exactly 1ms */
298 pio_write_8(&registers->sofmod, 64);
299
300 /* Set frame list pointer */
301 const uint32_t pa = addr_to_phys(instance->frame_list);
302 pio_write_32(&registers->flbaseadd, pa);
303
304 if (instance->base.irq_cap >= 0) {
305 /* Enable all interrupts, but resume interrupt */
306 pio_write_16(&instance->registers->usbintr,
307 UHCI_INTR_ALLOW_INTERRUPTS);
308 }
309
310 const uint16_t cmd = pio_read_16(&registers->usbcmd);
311 if (cmd != 0)
312 usb_log_warning("Previous command value: %x.", cmd);
313
314 /* Start the hc with large(64B) packet FSBR */
315 pio_write_16(&registers->usbcmd,
316 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
317}
318
319static usb_transfer_batch_t *create_transfer_batch(endpoint_t *ep)
320{
321 uhci_transfer_batch_t *batch = uhci_transfer_batch_create(ep);
322 return &batch->base;
323}
324
325static void destroy_transfer_batch(usb_transfer_batch_t *batch)
326{
327 uhci_transfer_batch_destroy(uhci_transfer_batch_get(batch));
328}
329
330static void endpoint_unregister(endpoint_t *ep)
331{
332 hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
333 usb2_bus_ops.endpoint_unregister(ep);
334
335 uhci_transfer_batch_t *batch = NULL;
336
337 // Check for the roothub, as it does not schedule into lists
338 if (ep->device->address == uhci_rh_get_address(&hc->rh)) {
339 // FIXME: We shall check the roothub for active transfer. But
340 // as it is polling, there is no way to make it stop doing so.
341 // Return after rewriting uhci rh.
342 return;
343 }
344
345 transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
346 assert(list);
347
348 // To avoid ABBA deadlock, we need to take the list first
349 fibril_mutex_lock(&list->guard);
350 fibril_mutex_lock(&ep->guard);
351 if (ep->active_batch) {
352 batch = uhci_transfer_batch_get(ep->active_batch);
353 endpoint_deactivate_locked(ep);
354 transfer_list_remove_batch(list, batch);
355 }
356 fibril_mutex_unlock(&ep->guard);
357 fibril_mutex_unlock(&list->guard);
358
359 if (batch) {
360 // The HW could have been looking at the batch.
361 // Better wait two frames before we release the buffers.
362 async_usleep(2000);
363 batch->base.error = EINTR;
364 batch->base.transfered_size = 0;
365 usb_transfer_batch_finish(&batch->base);
366 }
367}
368
369static int hc_status(bus_t *, uint32_t *);
370static int hc_schedule(usb_transfer_batch_t *);
371
372static const bus_ops_t uhci_bus_ops = {
373 .parent = &usb2_bus_ops,
374
375 .interrupt = hc_interrupt,
376 .status = hc_status,
377
378 .endpoint_unregister = endpoint_unregister,
379 .endpoint_count_bw = bandwidth_count_usb11,
380
381 .batch_create = create_transfer_batch,
382 .batch_schedule = hc_schedule,
383 .batch_destroy = destroy_transfer_batch,
384};
385
386/** Initialize UHCI hc memory structures.
387 *
388 * @param[in] instance UHCI structure to use.
389 * @return Error code
390 * @note Should be called only once on any structure.
391 *
392 * Structures:
393 * - transfer lists (queue heads need to be accessible by the hw)
394 * - frame list page (needs to be one UHCI hw accessible 4K page)
395 */
396int hc_init_mem_structures(hc_t *instance, hc_device_t *hcd)
397{
398 assert(instance);
399
400 usb2_bus_init(&instance->bus, BANDWIDTH_AVAILABLE_USB11);
401
402 bus_t *bus = (bus_t *) &instance->bus;
403 bus->ops = &uhci_bus_ops;
404
405 hc_device_setup(&instance->base, bus);
406
407 /* Init USB frame list page */
408 instance->frame_list = get_page();
409 if (!instance->frame_list) {
410 return ENOMEM;
411 }
412 usb_log_debug("Initialized frame list at %p.", instance->frame_list);
413
414 /* Init transfer lists */
415 int ret = hc_init_transfer_lists(instance);
416 if (ret != EOK) {
417 usb_log_error("Failed to initialize transfer lists.");
418 return_page(instance->frame_list);
419 return ENOMEM;
420 }
421 usb_log_debug("Initialized transfer lists.");
422
423
424 /* Set all frames to point to the first queue head */
425 const uint32_t queue = LINK_POINTER_QH(
426 addr_to_phys(instance->transfers_interrupt.queue_head));
427
428 for (unsigned i = 0; i < UHCI_FRAME_LIST_COUNT; ++i) {
429 instance->frame_list[i] = queue;
430 }
431
432 return EOK;
433}
434
435/** Initialize UHCI hc transfer lists.
436 *
437 * @param[in] instance UHCI structure to use.
438 * @return Error code
439 * @note Should be called only once on any structure.
440 *
441 * Initializes transfer lists and sets them in one chain to support proper
442 * USB scheduling. Sets pointer table for quick access.
443 */
444int hc_init_transfer_lists(hc_t *instance)
445{
446 assert(instance);
447#define SETUP_TRANSFER_LIST(type, name) \
448do { \
449 int ret = transfer_list_init(&instance->transfers_##type, name); \
450 if (ret != EOK) { \
451 usb_log_error("Failed to setup %s transfer list: %s.", \
452 name, str_error(ret)); \
453 transfer_list_fini(&instance->transfers_bulk_full); \
454 transfer_list_fini(&instance->transfers_control_full); \
455 transfer_list_fini(&instance->transfers_control_slow); \
456 transfer_list_fini(&instance->transfers_interrupt); \
457 return ret; \
458 } \
459} while (0)
460
461 SETUP_TRANSFER_LIST(bulk_full, "BULK FULL");
462 SETUP_TRANSFER_LIST(control_full, "CONTROL FULL");
463 SETUP_TRANSFER_LIST(control_slow, "CONTROL LOW");
464 SETUP_TRANSFER_LIST(interrupt, "INTERRUPT");
465#undef SETUP_TRANSFER_LIST
466 /* Connect lists into one schedule */
467 transfer_list_set_next(&instance->transfers_control_full,
468 &instance->transfers_bulk_full);
469 transfer_list_set_next(&instance->transfers_control_slow,
470 &instance->transfers_control_full);
471 transfer_list_set_next(&instance->transfers_interrupt,
472 &instance->transfers_control_slow);
473
474 /*FSBR, This feature is not needed (adds no benefit) and is supposedly
475 * buggy on certain hw, enable at your own risk. */
476#ifdef FSBR
477 transfer_list_set_next(&instance->transfers_bulk_full,
478 &instance->transfers_control_full);
479#endif
480
481 /* Assign pointers to be used during scheduling */
482 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
483 &instance->transfers_interrupt;
484 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
485 &instance->transfers_interrupt;
486 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
487 &instance->transfers_control_full;
488 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
489 &instance->transfers_control_slow;
490 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
491 &instance->transfers_bulk_full;
492
493 return EOK;
494}
495
496static int hc_status(bus_t *bus, uint32_t *status)
497{
498 hc_t *instance = bus_to_hc(bus);
499 assert(status);
500
501 *status = 0;
502 if (instance->registers) {
503 uint16_t s = pio_read_16(&instance->registers->usbsts);
504 pio_write_16(&instance->registers->usbsts, s);
505 *status = s;
506 }
507 return EOK;
508}
509
510/** Schedule batch for execution.
511 *
512 * @param[in] instance UHCI structure to use.
513 * @param[in] batch Transfer batch to schedule.
514 * @return Error code
515 *
516 * Checks for bandwidth availability and appends the batch to the proper queue.
517 */
518static int hc_schedule(usb_transfer_batch_t *batch)
519{
520 uhci_transfer_batch_t *uhci_batch = uhci_transfer_batch_get(batch);
521 endpoint_t *ep = batch->ep;
522 hc_t *hc = bus_to_hc(endpoint_get_bus(ep));
523
524 if (batch->target.address == uhci_rh_get_address(&hc->rh))
525 return uhci_rh_schedule(&hc->rh, batch);
526
527
528 const int err = uhci_transfer_batch_prepare(uhci_batch);
529 if (err)
530 return err;
531
532 transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
533 assert(list);
534 transfer_list_add_batch(list, uhci_batch);
535
536 return EOK;
537}
538
539int hc_unschedule_batch(usb_transfer_batch_t *batch)
540{
541
542 return EOK;
543}
544
545/** Debug function, checks consistency of memory structures.
546 *
547 * @param[in] arg UHCI structure to use.
548 * @return EOK (should never return)
549 */
550int hc_debug_checker(void *arg)
551{
552 hc_t *instance = arg;
553 assert(instance);
554
555#define QH(queue) \
556 instance->transfers_##queue.queue_head
557
558 while (1) {
559 const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
560 const uint16_t sts = pio_read_16(&instance->registers->usbsts);
561 const uint16_t intr =
562 pio_read_16(&instance->registers->usbintr);
563
564 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
565 usb_log_debug2("Command: %X Status: %X Intr: %x",
566 cmd, sts, intr);
567 }
568
569 const uintptr_t frame_list =
570 pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
571 if (frame_list != addr_to_phys(instance->frame_list)) {
572 usb_log_debug("Framelist address: %p vs. %p.",
573 (void *) frame_list,
574 (void *) addr_to_phys(instance->frame_list));
575 }
576
577 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
578
579 uintptr_t expected_pa = instance->frame_list[frnum]
580 & LINK_POINTER_ADDRESS_MASK;
581 uintptr_t real_pa = addr_to_phys(QH(interrupt));
582 if (expected_pa != real_pa) {
583 usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.",
584 (void *) expected_pa, frnum, (void *) real_pa);
585 }
586
587 expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
588 real_pa = addr_to_phys(QH(control_slow));
589 if (expected_pa != real_pa) {
590 usb_log_debug("Control Slow QH: %p vs. %p.",
591 (void *) expected_pa, (void *) real_pa);
592 }
593
594 expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
595 real_pa = addr_to_phys(QH(control_full));
596 if (expected_pa != real_pa) {
597 usb_log_debug("Control Full QH: %p vs. %p.",
598 (void *) expected_pa, (void *) real_pa);
599 }
600
601 expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
602 real_pa = addr_to_phys(QH(bulk_full));
603 if (expected_pa != real_pa ) {
604 usb_log_debug("Bulk QH: %p vs. %p.",
605 (void *) expected_pa, (void *) real_pa);
606 }
607 async_usleep(UHCI_DEBUGER_TIMEOUT);
608 }
609 return EOK;
610#undef QH
611}
612/**
613 * @}
614 */
Note: See TracBrowser for help on using the repository browser.