source: mainline/uspace/drv/bus/usb/xhci/hc.c@ 74b852b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 74b852b was f270ecb, checked in by Petr Manek <petr.manek@…>, 8 years ago

Implemented disable slot for device detachment.

  • Property mode set to 100644
File size: 18.5 KB
Line 
1/*
2 * Copyright (c) 2017 Ondrej Hlavaty
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 drvusbxhci
30 * @{
31 */
32/** @file
33 * @brief The host controller data bookkeeping.
34 */
35
36#include <errno.h>
37#include <str_error.h>
38#include <usb/debug.h>
39#include <usb/host/utils/malloc32.h>
40#include "debug.h"
41#include "hc.h"
42#include "rh.h"
43#include "hw_struct/trb.h"
44#include "commands.h"
45#include "transfers.h"
46#include "trb_ring.h"
47
48/**
49 * Default USB Speed ID mapping: Table 157
50 */
51#define PSI_TO_BPS(psie, psim) (((uint64_t) psim) << (10 * psie))
52#define PORT_SPEED(mjr, psie, psim) { \
53 .name = "USB ", \
54 .major = mjr, \
55 .minor = 0, \
56 .rx_bps = PSI_TO_BPS(psie, psim), \
57 .tx_bps = PSI_TO_BPS(psie, psim) \
58}
59static const xhci_port_speed_t ps_default_full = PORT_SPEED(2, 2, 12);
60static const xhci_port_speed_t ps_default_low = PORT_SPEED(2, 1, 1500);
61static const xhci_port_speed_t ps_default_high = PORT_SPEED(2, 2, 480);
62static const xhci_port_speed_t ps_default_super = PORT_SPEED(3, 3, 5);
63
64/**
65 * Walk the list of extended capabilities.
66 */
67static int hc_parse_ec(xhci_hc_t *hc)
68{
69 unsigned psic, major, minor;
70 xhci_sp_name_t name;
71
72 xhci_port_speed_t *speeds = hc->rh.speeds;
73
74 for (xhci_extcap_t *ec = hc->xecp; ec; ec = xhci_extcap_next(ec)) {
75 xhci_dump_extcap(ec);
76 switch (XHCI_REG_RD(ec, XHCI_EC_CAP_ID)) {
77 case XHCI_EC_USB_LEGACY:
78 assert(hc->legsup == NULL);
79 hc->legsup = (xhci_legsup_t *) ec;
80 break;
81 case XHCI_EC_SUPPORTED_PROTOCOL:
82 psic = XHCI_REG_RD(ec, XHCI_EC_SP_PSIC);
83 major = XHCI_REG_RD(ec, XHCI_EC_SP_MAJOR);
84 minor = XHCI_REG_RD(ec, XHCI_EC_SP_MINOR);
85 name.packed = host2uint32_t_le(XHCI_REG_RD(ec, XHCI_EC_SP_NAME));
86
87 if (name.packed != xhci_name_usb.packed) {
88 /**
89 * The detection of such protocol would work,
90 * but the rest of the implementation is made
91 * for the USB protocol only.
92 */
93 usb_log_error("Unknown protocol %.4s.", name.str);
94 return ENOTSUP;
95 }
96
97 // "Implied" speed
98 if (psic == 0) {
99 assert(minor == 0);
100
101 if (major == 2) {
102 speeds[1] = ps_default_full;
103 speeds[2] = ps_default_low;
104 speeds[3] = ps_default_high;
105 } else if (major == 3) {
106 speeds[4] = ps_default_super;
107 } else {
108 return EINVAL;
109 }
110
111 usb_log_debug2("Implied speed of USB %u.0 set up.", major);
112 } else {
113 for (unsigned i = 0; i < psic; i++) {
114 xhci_psi_t *psi = xhci_extcap_psi(ec, i);
115 unsigned sim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
116 unsigned psiv = XHCI_REG_RD(psi, XHCI_PSI_PSIV);
117 unsigned psie = XHCI_REG_RD(psi, XHCI_PSI_PSIE);
118 unsigned psim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
119
120 speeds[psiv].major = major;
121 speeds[psiv].minor = minor;
122 str_ncpy(speeds[psiv].name, 4, name.str, 4);
123
124 uint64_t bps = PSI_TO_BPS(psie, psim);
125
126 if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_RX)
127 speeds[psiv].rx_bps = bps;
128 if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_TX) {
129 speeds[psiv].tx_bps = bps;
130 usb_log_debug2("Speed %u set up for bps %" PRIu64 " / %" PRIu64 ".", psiv, speeds[psiv].rx_bps, speeds[psiv].tx_bps);
131 }
132 }
133 }
134 }
135 }
136 return EOK;
137}
138
139int hc_init_mmio(xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
140{
141 int err;
142
143 if (hw_res->mem_ranges.count != 1) {
144 usb_log_error("Unexpected MMIO area, bailing out.");
145 return EINVAL;
146 }
147
148 hc->mmio_range = hw_res->mem_ranges.ranges[0];
149
150 usb_log_debug("MMIO area at %p (size %zu), IRQ %d.\n",
151 RNGABSPTR(hc->mmio_range), RNGSZ(hc->mmio_range), hw_res->irqs.irqs[0]);
152
153 if (RNGSZ(hc->mmio_range) < sizeof(xhci_cap_regs_t))
154 return EOVERFLOW;
155
156 void *base;
157 if ((err = pio_enable_range(&hc->mmio_range, &base)))
158 return err;
159
160 hc->reg_base = base;
161 hc->cap_regs = (xhci_cap_regs_t *) base;
162 hc->op_regs = (xhci_op_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH));
163 hc->rt_regs = (xhci_rt_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF));
164 hc->db_arry = (xhci_doorbell_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_DBOFF));
165
166 uintptr_t xec_offset = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_XECP) * sizeof(xhci_dword_t);
167 if (xec_offset > 0)
168 hc->xecp = (xhci_extcap_t *) (base + xec_offset);
169
170 usb_log_debug2("Initialized MMIO reg areas:");
171 usb_log_debug2("\tCapability regs: %p", hc->cap_regs);
172 usb_log_debug2("\tOperational regs: %p", hc->op_regs);
173 usb_log_debug2("\tRuntime regs: %p", hc->rt_regs);
174 usb_log_debug2("\tDoorbell array base: %p", hc->db_arry);
175
176 xhci_dump_cap_regs(hc->cap_regs);
177
178 hc->ac64 = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_AC64);
179 hc->max_slots = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SLOTS);
180
181 if ((err = hc_parse_ec(hc))) {
182 pio_disable(hc->reg_base, RNGSZ(hc->mmio_range));
183 return err;
184 }
185
186 return EOK;
187}
188
189int hc_init_memory(xhci_hc_t *hc, ddf_dev_t *device)
190{
191 int err;
192
193 hc->dcbaa = malloc32((1 + hc->max_slots) * sizeof(uint64_t));
194 if (!hc->dcbaa)
195 return ENOMEM;
196
197 hc->dcbaa_virt = malloc((1 + hc->max_slots) * sizeof(xhci_virt_device_ctx_t));
198 if (!hc->dcbaa_virt) {
199 err = ENOMEM;
200 goto err_dcbaa;
201 }
202
203 if ((err = xhci_trb_ring_init(&hc->command_ring)))
204 goto err_dcbaa_virt;
205
206 if ((err = xhci_event_ring_init(&hc->event_ring)))
207 goto err_cmd_ring;
208
209 if ((err = xhci_scratchpad_alloc(hc)))
210 goto err_event_ring;
211
212 if ((err = xhci_init_commands(hc)))
213 goto err_scratch;
214
215 if ((err = xhci_init_transfers(hc)))
216 goto err_cmd;
217
218 if ((err = xhci_rh_init(&hc->rh, hc, device)))
219 goto err_transfers;
220
221 if ((err = xhci_bus_init(&hc->bus)))
222 goto err_rh;
223
224
225 return EOK;
226
227err_rh:
228 xhci_rh_fini(&hc->rh);
229err_transfers:
230 xhci_fini_transfers(hc);
231err_cmd:
232 xhci_fini_commands(hc);
233err_scratch:
234 xhci_scratchpad_free(hc);
235err_event_ring:
236 xhci_event_ring_fini(&hc->event_ring);
237err_cmd_ring:
238 xhci_trb_ring_fini(&hc->command_ring);
239err_dcbaa_virt:
240 free32(hc->dcbaa_virt);
241err_dcbaa:
242 free32(hc->dcbaa);
243 return err;
244}
245
246/*
247 * Pseudocode:
248 * ip = read(intr[0].iman)
249 * if (ip) {
250 * status = read(usbsts)
251 * assert status
252 * assert ip
253 * accept (passing status)
254 * }
255 * decline
256 */
257static const irq_cmd_t irq_commands[] = {
258 {
259 .cmd = CMD_PIO_READ_32,
260 .dstarg = 3,
261 .addr = NULL /* intr[0].iman */
262 },
263 {
264 .cmd = CMD_AND,
265 .srcarg = 3,
266 .dstarg = 4,
267 .value = 0 /* host2xhci(32, 1) */
268 },
269 {
270 .cmd = CMD_PREDICATE,
271 .srcarg = 4,
272 .value = 5
273 },
274 {
275 .cmd = CMD_PIO_READ_32,
276 .dstarg = 1,
277 .addr = NULL /* usbsts */
278 },
279 {
280 .cmd = CMD_AND,
281 .srcarg = 1,
282 .dstarg = 2,
283 .value = 0 /* host2xhci(32, XHCI_STATUS_ACK_MASK) */
284 },
285 {
286 .cmd = CMD_PIO_WRITE_A_32,
287 .srcarg = 2,
288 .addr = NULL /* usbsts */
289 },
290 {
291 .cmd = CMD_PIO_WRITE_A_32,
292 .srcarg = 3,
293 .addr = NULL /* intr[0].iman */
294 },
295 {
296 .cmd = CMD_ACCEPT
297 },
298 {
299 .cmd = CMD_DECLINE
300 }
301};
302
303
304/**
305 * Generates code to accept interrupts. The xHCI is designed primarily for
306 * MSI/MSI-X, but we use PCI Interrupt Pin. In this mode, all the Interrupters
307 * (except 0) are disabled.
308 */
309int hc_irq_code_gen(irq_code_t *code, xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
310{
311 assert(code);
312 assert(hw_res);
313
314 if (hw_res->irqs.count != 1) {
315 usb_log_info("Unexpected HW resources to enable interrupts.");
316 return EINVAL;
317 }
318
319 code->ranges = malloc(sizeof(irq_pio_range_t));
320 if (code->ranges == NULL)
321 return ENOMEM;
322
323 code->cmds = malloc(sizeof(irq_commands));
324 if (code->cmds == NULL) {
325 free(code->ranges);
326 return ENOMEM;
327 }
328
329 code->rangecount = 1;
330 code->ranges[0] = (irq_pio_range_t) {
331 .base = RNGABS(hc->mmio_range),
332 .size = RNGSZ(hc->mmio_range),
333 };
334
335 code->cmdcount = ARRAY_SIZE(irq_commands);
336 memcpy(code->cmds, irq_commands, sizeof(irq_commands));
337
338 void *intr0_iman = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF) + offsetof(xhci_rt_regs_t, ir[0]);
339 void *usbsts = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH) + offsetof(xhci_op_regs_t, usbsts);
340 code->cmds[0].addr = intr0_iman;
341 code->cmds[1].value = host2xhci(32, 1);
342 code->cmds[3].addr = usbsts;
343 code->cmds[4].value = host2xhci(32, XHCI_STATUS_ACK_MASK);
344 code->cmds[5].addr = usbsts;
345 code->cmds[6].addr = intr0_iman;
346
347 return hw_res->irqs.irqs[0];
348}
349
350int hc_claim(xhci_hc_t *hc, ddf_dev_t *dev)
351{
352 /* No legacy support capability, the controller is solely for us */
353 if (!hc->legsup)
354 return EOK;
355
356 /* Section 4.22.1 */
357 /* TODO: Test this with USB3-aware BIOS */
358 usb_log_debug2("LEGSUP: bios: %x, os: %x", hc->legsup->sem_bios, hc->legsup->sem_os);
359 XHCI_REG_WR(hc->legsup, XHCI_LEGSUP_SEM_OS, 1);
360 for (int i = 0; i <= (XHCI_LEGSUP_BIOS_TIMEOUT_US / XHCI_LEGSUP_POLLING_DELAY_1MS); i++) {
361 usb_log_debug2("LEGSUP: elapsed: %i ms, bios: %x, os: %x", i,
362 XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS),
363 XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS));
364 if (XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS) == 0) {
365 assert(XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS) == 1);
366 return EOK;
367 }
368 async_usleep(XHCI_LEGSUP_POLLING_DELAY_1MS);
369 }
370 usb_log_error("BIOS did not release XHCI legacy hold!\n");
371
372 return ENOTSUP;
373}
374
375static int hc_reset(xhci_hc_t *hc)
376{
377 /* Stop the HC: set R/S to 0 */
378 XHCI_REG_CLR(hc->op_regs, XHCI_OP_RS, 1);
379
380 /* Wait 16 ms until the HC is halted */
381 async_usleep(16000);
382 assert(XHCI_REG_RD(hc->op_regs, XHCI_OP_HCH));
383
384 /* Reset */
385 XHCI_REG_SET(hc->op_regs, XHCI_OP_HCRST, 1);
386
387 /* Wait until the reset is complete */
388 while (XHCI_REG_RD(hc->op_regs, XHCI_OP_HCRST))
389 async_usleep(1000);
390
391 return EOK;
392}
393
394/**
395 * Initialize the HC: section 4.2
396 */
397int hc_start(xhci_hc_t *hc, bool irq)
398{
399 int err;
400
401 if ((err = hc_reset(hc)))
402 return err;
403
404 while (XHCI_REG_RD(hc->op_regs, XHCI_OP_CNR))
405 async_usleep(1000);
406
407 uint64_t dcbaaptr = addr_to_phys(hc->dcbaa);
408 XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_LO, LOWER32(dcbaaptr));
409 XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_HI, UPPER32(dcbaaptr));
410 XHCI_REG_WR(hc->op_regs, XHCI_OP_MAX_SLOTS_EN, 0);
411
412 uint64_t crptr = xhci_trb_ring_get_dequeue_ptr(&hc->command_ring);
413 XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_LO, LOWER32(crptr) >> 6);
414 XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, UPPER32(crptr));
415
416 uint64_t erstptr = addr_to_phys(hc->event_ring.erst);
417 uint64_t erdp = hc->event_ring.dequeue_ptr;
418 xhci_interrupter_regs_t *intr0 = &hc->rt_regs->ir[0];
419 XHCI_REG_WR(intr0, XHCI_INTR_ERSTSZ, hc->event_ring.segment_count);
420 XHCI_REG_WR(intr0, XHCI_INTR_ERDP_LO, LOWER32(erdp));
421 XHCI_REG_WR(intr0, XHCI_INTR_ERDP_HI, UPPER32(erdp));
422 XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_LO, LOWER32(erstptr));
423 XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_HI, UPPER32(erstptr));
424
425 if (irq) {
426 XHCI_REG_SET(intr0, XHCI_INTR_IE, 1);
427 XHCI_REG_SET(hc->op_regs, XHCI_OP_INTE, 1);
428 }
429
430 XHCI_REG_SET(hc->op_regs, XHCI_OP_RS, 1);
431
432 /* The reset changed status of all ports, and SW originated reason does
433 * not cause an interrupt.
434 */
435 xhci_rh_handle_port_change(&hc->rh);
436
437 return EOK;
438}
439
440/**
441 * Used only when polling. Shall supplement the irq_commands.
442 */
443int hc_status(xhci_hc_t *hc, uint32_t *status)
444{
445 int ip = XHCI_REG_RD(hc->rt_regs->ir, XHCI_INTR_IP);
446 if (ip) {
447 *status = XHCI_REG_RD(hc->op_regs, XHCI_OP_STATUS);
448 XHCI_REG_WR(hc->op_regs, XHCI_OP_STATUS, *status & XHCI_STATUS_ACK_MASK);
449 XHCI_REG_WR(hc->rt_regs->ir, XHCI_INTR_IP, 1);
450
451 /* interrupt handler expects status from irq_commands, which is
452 * in xhci order. */
453 *status = host2xhci(32, *status);
454 }
455
456 usb_log_debug2("HC(%p): Polled status: %x", hc, *status);
457 return EOK;
458}
459
460int hc_schedule(xhci_hc_t *hc, usb_transfer_batch_t *batch)
461{
462 assert(batch);
463
464 usb_log_debug2("Endpoint(%d:%d) started %s transfer of size %lu.",
465 batch->ep->target.address, batch->ep->target.endpoint,
466 usb_str_transfer_type(batch->ep->transfer_type),
467 batch->buffer_size);
468
469 if (!batch->ep->target.address) {
470 usb_log_error("Attempted to schedule transfer to address 0.");
471 return EINVAL;
472 }
473
474 switch (batch->ep->transfer_type) {
475 case USB_TRANSFER_CONTROL:
476 return xhci_schedule_control_transfer(hc, batch);
477 case USB_TRANSFER_ISOCHRONOUS:
478 return xhci_schedule_isochronous_transfer(hc, batch);
479 case USB_TRANSFER_BULK:
480 return xhci_schedule_bulk_transfer(hc, batch);
481 case USB_TRANSFER_INTERRUPT:
482 return xhci_schedule_interrupt_transfer(hc, batch);
483 }
484
485 return EOK;
486}
487
488typedef int (*event_handler) (xhci_hc_t *, xhci_trb_t *trb);
489
490static event_handler event_handlers [] = {
491 [XHCI_TRB_TYPE_COMMAND_COMPLETION_EVENT] = &xhci_handle_command_completion,
492 [XHCI_TRB_TYPE_PORT_STATUS_CHANGE_EVENT] = &xhci_rh_handle_port_status_change_event,
493 [XHCI_TRB_TYPE_TRANSFER_EVENT] = &xhci_handle_transfer_event,
494};
495
496static int hc_handle_event(xhci_hc_t *hc, xhci_trb_t *trb, xhci_interrupter_regs_t *intr)
497{
498 unsigned type = TRB_TYPE(*trb);
499 if (type >= ARRAY_SIZE(event_handlers) || !event_handlers[type])
500 return ENOTSUP;
501
502 return event_handlers[type](hc, trb);
503}
504
505static void hc_run_event_ring(xhci_hc_t *hc, xhci_event_ring_t *event_ring, xhci_interrupter_regs_t *intr)
506{
507 int err;
508 ssize_t size = 16;
509 xhci_trb_t *queue = malloc(sizeof(xhci_trb_t) * size);
510 if (!queue) {
511 usb_log_error("Not enough memory to run the event ring.");
512 return;
513 }
514
515 xhci_trb_t *head = queue;
516
517 while ((err = xhci_event_ring_dequeue(event_ring, head)) != ENOENT) {
518 if (err != EOK) {
519 usb_log_warning("Error while accessing event ring: %s", str_error(err));
520 break;
521 }
522
523 usb_log_debug2("Dequeued trb from event ring: %s", xhci_trb_str_type(TRB_TYPE(*head)));
524 head++;
525
526 /* Expand the array if needed. */
527 if (head - queue >= size) {
528 size *= 2;
529 xhci_trb_t *new_queue = realloc(queue, size);
530 if (new_queue == NULL)
531 break; /* Will process only those TRBs we have memory for. */
532
533 head = new_queue + (head - queue);
534 }
535 }
536
537 /* Update the ERDP to make room in the ring. */
538 usb_log_debug2("Copying from ring finished, updating ERDP.");
539 hc->event_ring.dequeue_ptr = host2xhci(64, addr_to_phys(hc->event_ring.dequeue_trb));
540 uint64_t erdp = hc->event_ring.dequeue_ptr;
541 XHCI_REG_WR(intr, XHCI_INTR_ERDP_LO, LOWER32(erdp));
542 XHCI_REG_WR(intr, XHCI_INTR_ERDP_HI, UPPER32(erdp));
543 XHCI_REG_SET(intr, XHCI_INTR_ERDP_EHB, 1);
544
545 /* Handle all of the collected events if possible. */
546 if (head == queue)
547 usb_log_warning("No events to be handled!");
548
549 for (xhci_trb_t *tail = queue; tail != head; tail++) {
550 if ((err = hc_handle_event(hc, tail, intr)) != EOK) {
551 usb_log_error("Failed to handle event: %s", str_error(err));
552 }
553 }
554
555 free(queue);
556 usb_log_debug2("Event ring run finished.");
557}
558
559void hc_interrupt(xhci_hc_t *hc, uint32_t status)
560{
561 status = xhci2host(32, status);
562
563 if (status & XHCI_REG_MASK(XHCI_OP_PCD)) {
564 usb_log_debug2("Root hub interrupt.");
565 xhci_rh_handle_port_change(&hc->rh);
566 status &= ~XHCI_REG_MASK(XHCI_OP_PCD);
567 }
568
569 if (status & XHCI_REG_MASK(XHCI_OP_HSE)) {
570 usb_log_error("Host controller error occured. Bad things gonna happen...");
571 status &= ~XHCI_REG_MASK(XHCI_OP_HSE);
572 }
573
574 if (status & XHCI_REG_MASK(XHCI_OP_EINT)) {
575 usb_log_debug2("Event interrupt, running the event ring.");
576 hc_run_event_ring(hc, &hc->event_ring, &hc->rt_regs->ir[0]);
577 status &= ~XHCI_REG_MASK(XHCI_OP_EINT);
578 }
579
580 if (status & XHCI_REG_MASK(XHCI_OP_SRE)) {
581 usb_log_error("Save/Restore error occured. WTF, S/R mechanism not implemented!");
582 status &= ~XHCI_REG_MASK(XHCI_OP_SRE);
583 }
584
585 if (status) {
586 usb_log_error("Non-zero status after interrupt handling (%08x) - missing something?", status);
587 }
588}
589
590static void hc_dcbaa_fini(xhci_hc_t *hc)
591{
592 xhci_trb_ring_t* trb_ring;
593 xhci_scratchpad_free(hc);
594
595 /* Idx 0 already deallocated by xhci_scratchpad_free. */
596 for (unsigned i = 1; i < hc->max_slots + 1; ++i) {
597 if (hc->dcbaa_virt[i].dev_ctx) {
598 free32(hc->dcbaa_virt[i].dev_ctx);
599 hc->dcbaa_virt[i].dev_ctx = NULL;
600 }
601
602 for (unsigned i = 0; i < XHCI_EP_COUNT; ++i) {
603 trb_ring = hc->dcbaa_virt[i].trs[i];
604 if (trb_ring) {
605 hc->dcbaa_virt[i].trs[i] = NULL;
606 xhci_trb_ring_fini(trb_ring);
607 free32(trb_ring);
608 }
609 }
610 }
611
612 free32(hc->dcbaa);
613 free32(hc->dcbaa_virt);
614}
615
616void hc_fini(xhci_hc_t *hc)
617{
618 xhci_bus_fini(&hc->bus);
619 xhci_trb_ring_fini(&hc->command_ring);
620 xhci_event_ring_fini(&hc->event_ring);
621 hc_dcbaa_fini(hc);
622 xhci_fini_transfers(hc);
623 xhci_fini_commands(hc);
624 xhci_rh_fini(&hc->rh);
625 pio_disable(hc->reg_base, RNGSZ(hc->mmio_range));
626 usb_log_info("HC(%p): Finalized.", hc);
627}
628
629int hc_ring_doorbell(xhci_hc_t *hc, unsigned doorbell, unsigned target)
630{
631 assert(hc);
632 uint32_t v = host2xhci(32, target & BIT_RRANGE(uint32_t, 7));
633 pio_write_32(&hc->db_arry[doorbell], v);
634 usb_log_debug2("Ringing doorbell %d (target: %d)", doorbell, target);
635 return EOK;
636}
637
638int hc_enable_slot(xhci_hc_t *hc, uint32_t *slot_id)
639{
640 assert(hc);
641
642 int err;
643 xhci_cmd_t cmd;
644 xhci_cmd_init(&cmd);
645
646 if ((err = xhci_send_enable_slot_command(hc, &cmd)) != EOK)
647 return err;
648
649 if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT)) != EOK)
650 return err;
651
652 if (slot_id)
653 *slot_id = cmd.slot_id;
654
655 xhci_cmd_fini(&cmd);
656 return EOK;
657}
658
659int hc_disable_slot(xhci_hc_t *hc, uint32_t slot_id)
660{
661 assert(hc);
662
663 int err;
664 xhci_cmd_t cmd;
665 xhci_cmd_init(&cmd);
666
667 cmd.slot_id = slot_id;
668
669 if ((err = xhci_send_disable_slot_command(hc, &cmd)) != EOK)
670 return err;
671
672 if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT)) != EOK)
673 return err;
674
675 xhci_cmd_fini(&cmd);
676 return EOK;
677}
678
679int hc_address_device(xhci_hc_t *hc, uint32_t slot_id, xhci_input_ctx_t *ictx)
680{
681 assert(hc);
682
683 int err;
684 xhci_cmd_t cmd;
685 xhci_cmd_init(&cmd);
686
687 cmd.slot_id = slot_id;
688
689 if ((err = xhci_send_address_device_command(hc, &cmd, ictx)) != EOK)
690 return err;
691
692 if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT)) != EOK)
693 return err;
694
695 xhci_cmd_fini(&cmd);
696 return EOK;
697}
698
699/**
700 * @}
701 */
Note: See TracBrowser for help on using the repository browser.