source: mainline/uspace/drv/bus/usb/xhci/commands.c@ 2770b66

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2770b66 was a0be5d0, checked in by Michal Staruch <salmelu@…>, 8 years ago

Moved ring_doorbell, added address check

  • Property mode set to 100644
File size: 12.8 KB
RevLine 
[c9c0e41]1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
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 Command sending functions.
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 "commands.h"
41#include "debug.h"
42#include "hc.h"
[8db42f7]43#include "hw_struct/context.h"
[c9c0e41]44#include "hw_struct/trb.h"
45
[1b78a7c1]46#define TRB_SET_TCS(trb, tcs) (trb).control |= host2xhci(32, ((tcs &0x1) << 9))
47#define TRB_SET_TYPE(trb, type) (trb).control |= host2xhci(32, (type) << 10)
48#define TRB_SET_EP(trb, ep) (trb).control |= host2xhci(32, ((ep) & 0x5) << 16)
[0cabd10]49#define TRB_SET_STREAM(trb, st) (trb).control |= host2xhci(32, ((st) & 0xFFFF) << 16)
[1b78a7c1]50#define TRB_SET_SUSP(trb, susp) (trb).control |= host2xhci(32, ((susp) & 0x1) << 23)
51#define TRB_SET_SLOT(trb, slot) (trb).control |= host2xhci(32, (slot) << 24)
52
[0cabd10]53/**
54 * TODO: Not sure about SCT and DCS (see section 6.4.3.9).
55 */
56#define TRB_SET_DEQUEUE_PTR(trb, dptr) (trb).parameter |= host2xhci(64, (dptr))
[548c123]57#define TRB_SET_ICTX(trb, phys) (trb).parameter |= host2xhci(64, phys_addr & (~0xF))
[1b78a7c1]58
59#define TRB_GET_CODE(trb) XHCI_DWORD_EXTRACT((trb).status, 31, 24)
60#define TRB_GET_SLOT(trb) XHCI_DWORD_EXTRACT((trb).control, 31, 24)
61#define TRB_GET_PHYS(trb) (XHCI_QWORD_EXTRACT((trb).parameter, 63, 4) << 4)
62
[110d795]63int xhci_init_commands(xhci_hc_t *hc)
64{
65 assert(hc);
66
67 list_initialize(&hc->commands);
68 return EOK;
69}
70
[c46c356]71void xhci_fini_commands(xhci_hc_t *hc)
72{
73 // Note: Untested.
74 assert(hc);
75}
76
[04df063]77int xhci_cmd_wait(xhci_cmd_t *cmd, suseconds_t timeout)
[110d795]78{
[4688350b]79 int rv = EOK;
80
81 fibril_mutex_lock(&cmd->completed_mtx);
[110d795]82 while (!cmd->completed) {
[4688350b]83 usb_log_debug2("Waiting for event completion: going to sleep.");
84 rv = fibril_condvar_wait_timeout(&cmd->completed_cv, &cmd->completed_mtx, timeout);
[110d795]85
[4688350b]86 usb_log_debug2("Waiting for event completion: woken: %s", str_error(rv));
87 if (rv == ETIMEOUT)
88 break;
[110d795]89 }
[1f76b7d]90 fibril_mutex_unlock(&cmd->completed_mtx);
[110d795]91
[4688350b]92 return rv;
[110d795]93}
94
[04df063]95xhci_cmd_t *xhci_cmd_alloc(void)
[110d795]96{
[078e0e6]97 xhci_cmd_t *cmd = malloc(sizeof(xhci_cmd_t));
[4688350b]98 xhci_cmd_init(cmd);
[04df063]99
100 usb_log_debug2("Allocating cmd on the heap. Don't forget to deallocate it!");
[4688350b]101 return cmd;
102}
103
104void xhci_cmd_init(xhci_cmd_t *cmd)
105{
106 memset(cmd, 0, sizeof(*cmd));
[110d795]107
108 link_initialize(&cmd->link);
109
[4688350b]110 fibril_mutex_initialize(&cmd->completed_mtx);
111 fibril_condvar_initialize(&cmd->completed_cv);
[110d795]112}
113
[04df063]114void xhci_cmd_fini(xhci_cmd_t *cmd)
[110d795]115{
[9304b66]116 list_remove(&cmd->link);
[04df063]117}
[9304b66]118
[04df063]119void xhci_cmd_free(xhci_cmd_t *cmd)
120{
121 xhci_cmd_fini(cmd);
[078e0e6]122 free(cmd);
[110d795]123}
124
[2fa43d1]125static inline xhci_cmd_t *get_command(xhci_hc_t *hc, uint64_t phys)
[110d795]126{
127 link_t *cmd_link = list_first(&hc->commands);
128
[2fa43d1]129 while (cmd_link != NULL) {
130 xhci_cmd_t *cmd = list_get_instance(cmd_link, xhci_cmd_t, link);
131
[548c123]132 if (cmd->trb_phys == phys)
[2fa43d1]133 break;
134
135 cmd_link = list_next(cmd_link, &hc->commands);
136 }
137
[110d795]138 if (cmd_link != NULL) {
139 list_remove(cmd_link);
[9f5b613]140
[110d795]141 return list_get_instance(cmd_link, xhci_cmd_t, link);
142 }
143
144 return NULL;
145}
146
[548c123]147static inline int enqueue_command(xhci_hc_t *hc, xhci_cmd_t *cmd, unsigned doorbell, unsigned target)
[481af21e]148{
[c058a388]149 assert(hc);
[548c123]150 assert(cmd);
151
152 list_append(&cmd->link, &hc->commands);
[c058a388]153
[548c123]154 xhci_trb_ring_enqueue(&hc->command_ring, &cmd->trb, &cmd->trb_phys);
[a0be5d0]155 hc_ring_doorbell(hc, doorbell, target);
[481af21e]156
[548c123]157 usb_log_debug2("HC(%p): Sent command:", hc);
158 xhci_dump_trb(&cmd->trb);
[481af21e]159
160 return EOK;
161}
162
[3dc519f]163void xhci_stop_command_ring(xhci_hc_t *hc)
164{
165 assert(hc);
166
167 XHCI_REG_SET(hc->op_regs, XHCI_OP_CS, 1);
168
169 /**
170 * Note: There is a bug in qemu that checks CS only when CRCR_HI
171 * is written, this (and the read/write in abort) ensures
172 * the command rings stops.
173 */
174 XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, XHCI_REG_RD(hc->op_regs, XHCI_OP_CRCR_HI));
175}
176
177void xhci_abort_command_ring(xhci_hc_t *hc)
178{
179 assert(hc);
180
181 XHCI_REG_WR(hc->op_regs, XHCI_OP_CA, 1);
182 XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, XHCI_REG_RD(hc->op_regs, XHCI_OP_CRCR_HI));
183}
184
185void xhci_start_command_ring(xhci_hc_t *hc)
186{
187 assert(hc);
188
189 XHCI_REG_WR(hc->op_regs, XHCI_OP_CRR, 1);
[a0be5d0]190 hc_ring_doorbell(hc, 0, 0);
[3dc519f]191}
192
[4fa5342]193static const char *trb_codes [] = {
194#define TRBC(t) [XHCI_TRBC_##t] = #t
195 TRBC(INVALID),
196 TRBC(SUCCESS),
197 TRBC(DATA_BUFFER_ERROR),
198 TRBC(BABBLE_DETECTED_ERROR),
199 TRBC(USB_TRANSACTION_ERROR),
200 TRBC(TRB_ERROR),
201 TRBC(STALL_ERROR),
202 TRBC(RESOURCE_ERROR),
203 TRBC(BANDWIDTH_ERROR),
204 TRBC(NO_SLOTS_ERROR),
205 TRBC(INVALID_STREAM_ERROR),
206 TRBC(SLOT_NOT_ENABLED_ERROR),
207 TRBC(EP_NOT_ENABLED_ERROR),
208 TRBC(SHORT_PACKET),
209 TRBC(RING_UNDERRUN),
210 TRBC(RING_OVERRUN),
211 TRBC(VF_EVENT_RING_FULL),
212 TRBC(PARAMETER_ERROR),
213 TRBC(BANDWIDTH_OVERRUN_ERROR),
214 TRBC(CONTEXT_STATE_ERROR),
215 TRBC(NO_PING_RESPONSE_ERROR),
216 TRBC(EVENT_RING_FULL_ERROR),
217 TRBC(INCOMPATIBLE_DEVICE_ERROR),
218 TRBC(MISSED_SERVICE_ERROR),
219 TRBC(COMMAND_RING_STOPPED),
220 TRBC(COMMAND_ABORTED),
221 TRBC(STOPPED),
222 TRBC(STOPPED_LENGTH_INVALID),
223 TRBC(STOPPED_SHORT_PACKET),
224 TRBC(MAX_EXIT_LATENCY_TOO_LARGE_ERROR),
225 [30] = "<reserved>",
226 TRBC(ISOCH_BUFFER_OVERRUN),
227 TRBC(EVENT_LOST_ERROR),
228 TRBC(UNDEFINED_ERROR),
229 TRBC(INVALID_STREAM_ID_ERROR),
230 TRBC(SECONDARY_BANDWIDTH_ERROR),
231 TRBC(SPLIT_TRANSACTION_ERROR),
232 [XHCI_TRBC_MAX] = NULL
233#undef TRBC
234};
235
236static void report_error(int code)
237{
238 if (code < XHCI_TRBC_MAX && trb_codes[code] != NULL)
239 usb_log_error("Command resulted in error: %s.", trb_codes[code]);
240 else
241 usb_log_error("Command resulted in reserved or vendor specific error.");
242}
243
[110d795]244int xhci_send_no_op_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
[c9c0e41]245{
[c058a388]246 assert(hc);
247
[548c123]248 xhci_trb_clean(&cmd->trb);
[c9c0e41]249
[548c123]250 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_NO_OP_CMD);
[110d795]251
[548c123]252 return enqueue_command(hc, cmd, 0, 0);
[c9c0e41]253}
254
[110d795]255int xhci_send_enable_slot_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
[c9c0e41]256{
[c058a388]257 assert(hc);
258
[548c123]259 xhci_trb_clean(&cmd->trb);
[c9c0e41]260
[548c123]261 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_ENABLE_SLOT_CMD);
262 cmd->trb.control |= host2xhci(32, XHCI_REG_RD(hc->xecp, XHCI_EC_SP_SLOT_TYPE) << 16);
[110d795]263
[548c123]264 return enqueue_command(hc, cmd, 0, 0);
[5ac5eb1]265}
266
[110d795]267int xhci_send_disable_slot_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
[5ac5eb1]268{
[c058a388]269 assert(hc);
[110d795]270 assert(cmd);
[c058a388]271
[548c123]272 xhci_trb_clean(&cmd->trb);
[5ac5eb1]273
[548c123]274 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_DISABLE_SLOT_CMD);
275 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
[110d795]276
[548c123]277 return enqueue_command(hc, cmd, 0, 0);
[c9c0e41]278}
279
[04df063]280int xhci_send_address_device_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
[8db42f7]281{
[c058a388]282 assert(hc);
[110d795]283 assert(cmd);
[04df063]284 assert(ictx);
[c058a388]285
[8db42f7]286 /**
287 * TODO: Requirements for this command:
288 * dcbaa[slot_id] is properly sized and initialized
289 * ictx has valids slot context and endpoint 0, all
290 * other should be ignored at this point (see section 4.6.5).
291 */
[04df063]292
[548c123]293 xhci_trb_clean(&cmd->trb);
[8db42f7]294
[04df063]295 uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
[548c123]296 TRB_SET_ICTX(cmd->trb, phys_addr);
[8db42f7]297
298 /**
299 * Note: According to section 6.4.3.4, we can set the 9th bit
300 * of the control field of the trb (BSR) to 1 and then the xHC
301 * will not issue the SET_ADDRESS request to the USB device.
302 * This can be used to provide compatibility with legacy USB devices
303 * that require their device descriptor to be read before such request.
304 */
[548c123]305 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_ADDRESS_DEVICE_CMD);
306 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
[8db42f7]307
[548c123]308 return enqueue_command(hc, cmd, 0, 0);
[8db42f7]309}
310
[04df063]311int xhci_send_configure_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
[665bf3c]312{
[c058a388]313 assert(hc);
[110d795]314 assert(cmd);
[04df063]315 assert(ictx);
[c058a388]316
[548c123]317 xhci_trb_clean(&cmd->trb);
[665bf3c]318
[04df063]319 uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
[548c123]320 TRB_SET_ICTX(cmd->trb, phys_addr);
[110d795]321
[548c123]322 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_CONFIGURE_ENDPOINT_CMD);
323 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
[665bf3c]324
[548c123]325 return enqueue_command(hc, cmd, 0, 0);
[665bf3c]326}
327
[04df063]328int xhci_send_evaluate_context_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
[c9ce62ae]329{
[c058a388]330 assert(hc);
[110d795]331 assert(cmd);
[04df063]332 assert(ictx);
[c058a388]333
[c9ce62ae]334 /**
335 * Note: All Drop Context flags of the input context shall be 0,
336 * all Add Context flags shall be initialize to indicate IDs
337 * of the contexts affected by the command.
338 * Refer to sections 6.2.2.3 and 6.3.3.3 for further info.
339 */
[548c123]340 xhci_trb_clean(&cmd->trb);
[c9ce62ae]341
[04df063]342 uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
[548c123]343 TRB_SET_ICTX(cmd->trb, phys_addr);
[c9ce62ae]344
[548c123]345 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD);
346 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
[110d795]347
[548c123]348 return enqueue_command(hc, cmd, 0, 0);
[c9ce62ae]349}
350
[110d795]351int xhci_send_reset_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, uint32_t ep_id, uint8_t tcs)
[05aeee0e]352{
[c058a388]353 assert(hc);
[110d795]354 assert(cmd);
[c058a388]355
[05aeee0e]356 /**
357 * Note: TCS can have values 0 or 1. If it is set to 0, see sectuon 4.5.8 for
358 * information about this flag.
359 */
[548c123]360 xhci_trb_clean(&cmd->trb);
[05aeee0e]361
[548c123]362 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_RESET_ENDPOINT_CMD);
363 TRB_SET_TCS(cmd->trb, tcs);
364 TRB_SET_EP(cmd->trb, ep_id);
365 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
[c9bec1c]366
[548c123]367 return enqueue_command(hc, cmd, 0, 0);
[05aeee0e]368}
369
[110d795]370int xhci_send_stop_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, uint32_t ep_id, uint8_t susp)
[05aeee0e]371{
[c058a388]372 assert(hc);
[110d795]373 assert(cmd);
[c058a388]374
[548c123]375 xhci_trb_clean(&cmd->trb);
[110d795]376
[548c123]377 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_STOP_ENDPOINT_CMD);
378 TRB_SET_EP(cmd->trb, ep_id);
379 TRB_SET_SUSP(cmd->trb, susp);
380 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
[05aeee0e]381
[548c123]382 return enqueue_command(hc, cmd, 0, 0);
[c058a388]383}
[05aeee0e]384
[0cabd10]385int xhci_send_set_dequeue_ptr_command(xhci_hc_t *hc, xhci_cmd_t *cmd,
386 uintptr_t dequeue_ptr, uint16_t stream_id,
387 uint32_t ep_id)
388{
389 assert(hc);
390 assert(cmd);
391
[548c123]392 xhci_trb_clean(&cmd->trb);
[0cabd10]393
[548c123]394 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_SET_TR_DEQUEUE_POINTER_CMD);
395 TRB_SET_EP(cmd->trb, ep_id);
396 TRB_SET_STREAM(cmd->trb, stream_id);
397 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
398 TRB_SET_DEQUEUE_PTR(cmd->trb, dequeue_ptr);
[0cabd10]399
400 /**
401 * TODO: Set DCS (see section 4.6.10).
402 */
403
[548c123]404 return enqueue_command(hc, cmd, 0, 0);
[0cabd10]405}
406
[110d795]407int xhci_send_reset_device_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
[c058a388]408{
409 assert(hc);
[110d795]410 assert(cmd);
[c058a388]411
[548c123]412 xhci_trb_clean(&cmd->trb);
[c058a388]413
[548c123]414 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_RESET_DEVICE_CMD);
415 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
[c9bec1c]416
[548c123]417 return enqueue_command(hc, cmd, 0, 0);
[05aeee0e]418}
419
[f9e7fe8]420int xhci_handle_command_completion(xhci_hc_t *hc, xhci_trb_t *trb)
421{
[110d795]422 // TODO: Update dequeue ptrs.
[c058a388]423 assert(hc);
424 assert(trb);
425
[5ac5eb1]426 usb_log_debug("HC(%p) Command completed.", hc);
[f9e7fe8]427
[5ac5eb1]428 int code;
[2fa43d1]429 uint64_t phys;
[110d795]430 xhci_cmd_t *command;
[f711f06]431
[1b78a7c1]432 code = TRB_GET_CODE(*trb);
433 phys = TRB_GET_PHYS(*trb);;
[2fa43d1]434 command = get_command(hc, phys);
435 if (command == NULL) {
436 // TODO: STOP & ABORT may not have command structs in the list!
[04df063]437 usb_log_debug("No command struct for this completion event found.");
[2fa43d1]438
439 if (code != XHCI_TRBC_SUCCESS)
440 report_error(code);
441
442 return EOK;
443 }
[110d795]444
445 command->status = code;
[1b78a7c1]446 command->slot_id = TRB_GET_SLOT(*trb);
[110d795]447
[548c123]448 usb_log_debug2("Completed command trb: %s", xhci_trb_str_type(TRB_TYPE(command->trb)));
449 if (TRB_TYPE(command->trb) != XHCI_TRB_TYPE_NO_OP_CMD) {
[665bf3c]450 if (code != XHCI_TRBC_SUCCESS) {
[4fa5342]451 report_error(code);
[548c123]452 xhci_dump_trb(&command->trb);
[665bf3c]453 }
454 }
[c362127]455
[548c123]456 switch (TRB_TYPE(command->trb)) {
[c362127]457 case XHCI_TRB_TYPE_NO_OP_CMD:
[9f5b613]458 assert(code == XHCI_TRBC_TRB_ERROR);
[110d795]459 break;
[c362127]460 case XHCI_TRB_TYPE_ENABLE_SLOT_CMD:
[110d795]461 break;
[5ac5eb1]462 case XHCI_TRB_TYPE_DISABLE_SLOT_CMD:
[110d795]463 break;
[8db42f7]464 case XHCI_TRB_TYPE_ADDRESS_DEVICE_CMD:
[110d795]465 break;
[665bf3c]466 case XHCI_TRB_TYPE_CONFIGURE_ENDPOINT_CMD:
[110d795]467 break;
[c9ce62ae]468 case XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD:
[110d795]469 break;
[05aeee0e]470 case XHCI_TRB_TYPE_RESET_ENDPOINT_CMD:
[110d795]471 break;
[05aeee0e]472 case XHCI_TRB_TYPE_STOP_ENDPOINT_CMD:
473 // Note: If the endpoint was in the middle of a transfer, then the xHC
474 // will add a Transfer TRB before the Event TRB, research that and
475 // handle it appropriately!
[110d795]476 break;
[c058a388]477 case XHCI_TRB_TYPE_RESET_DEVICE_CMD:
[110d795]478 break;
[c362127]479 default:
[548c123]480 usb_log_debug2("Unsupported command trb: %s", xhci_trb_str_type(TRB_TYPE(command->trb)));
[110d795]481
482 command->completed = true;
[665bf3c]483 return ENAK;
[f711f06]484 }
[110d795]485
[4688350b]486 fibril_mutex_lock(&command->completed_mtx);
[110d795]487 command->completed = true;
[4688350b]488 fibril_condvar_broadcast(&command->completed_cv);
489 fibril_mutex_unlock(&command->completed_mtx);
490
[110d795]491 return EOK;
[f9e7fe8]492}
[c9c0e41]493
494
495/**
496 * @}
497 */
Note: See TracBrowser for help on using the repository browser.