source: mainline/uspace/drv/bus/usb/xhci/commands.c@ 7e74911

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7e74911 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
Line 
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"
43#include "hw_struct/context.h"
44#include "hw_struct/trb.h"
45
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)
49#define TRB_SET_STREAM(trb, st) (trb).control |= host2xhci(32, ((st) & 0xFFFF) << 16)
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
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))
57#define TRB_SET_ICTX(trb, phys) (trb).parameter |= host2xhci(64, phys_addr & (~0xF))
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
63int xhci_init_commands(xhci_hc_t *hc)
64{
65 assert(hc);
66
67 list_initialize(&hc->commands);
68 return EOK;
69}
70
71void xhci_fini_commands(xhci_hc_t *hc)
72{
73 // Note: Untested.
74 assert(hc);
75}
76
77int xhci_cmd_wait(xhci_cmd_t *cmd, suseconds_t timeout)
78{
79 int rv = EOK;
80
81 fibril_mutex_lock(&cmd->completed_mtx);
82 while (!cmd->completed) {
83 usb_log_debug2("Waiting for event completion: going to sleep.");
84 rv = fibril_condvar_wait_timeout(&cmd->completed_cv, &cmd->completed_mtx, timeout);
85
86 usb_log_debug2("Waiting for event completion: woken: %s", str_error(rv));
87 if (rv == ETIMEOUT)
88 break;
89 }
90 fibril_mutex_unlock(&cmd->completed_mtx);
91
92 return rv;
93}
94
95xhci_cmd_t *xhci_cmd_alloc(void)
96{
97 xhci_cmd_t *cmd = malloc(sizeof(xhci_cmd_t));
98 xhci_cmd_init(cmd);
99
100 usb_log_debug2("Allocating cmd on the heap. Don't forget to deallocate it!");
101 return cmd;
102}
103
104void xhci_cmd_init(xhci_cmd_t *cmd)
105{
106 memset(cmd, 0, sizeof(*cmd));
107
108 link_initialize(&cmd->link);
109
110 fibril_mutex_initialize(&cmd->completed_mtx);
111 fibril_condvar_initialize(&cmd->completed_cv);
112}
113
114void xhci_cmd_fini(xhci_cmd_t *cmd)
115{
116 list_remove(&cmd->link);
117}
118
119void xhci_cmd_free(xhci_cmd_t *cmd)
120{
121 xhci_cmd_fini(cmd);
122 free(cmd);
123}
124
125static inline xhci_cmd_t *get_command(xhci_hc_t *hc, uint64_t phys)
126{
127 link_t *cmd_link = list_first(&hc->commands);
128
129 while (cmd_link != NULL) {
130 xhci_cmd_t *cmd = list_get_instance(cmd_link, xhci_cmd_t, link);
131
132 if (cmd->trb_phys == phys)
133 break;
134
135 cmd_link = list_next(cmd_link, &hc->commands);
136 }
137
138 if (cmd_link != NULL) {
139 list_remove(cmd_link);
140
141 return list_get_instance(cmd_link, xhci_cmd_t, link);
142 }
143
144 return NULL;
145}
146
147static inline int enqueue_command(xhci_hc_t *hc, xhci_cmd_t *cmd, unsigned doorbell, unsigned target)
148{
149 assert(hc);
150 assert(cmd);
151
152 list_append(&cmd->link, &hc->commands);
153
154 xhci_trb_ring_enqueue(&hc->command_ring, &cmd->trb, &cmd->trb_phys);
155 hc_ring_doorbell(hc, doorbell, target);
156
157 usb_log_debug2("HC(%p): Sent command:", hc);
158 xhci_dump_trb(&cmd->trb);
159
160 return EOK;
161}
162
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);
190 hc_ring_doorbell(hc, 0, 0);
191}
192
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
244int xhci_send_no_op_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
245{
246 assert(hc);
247
248 xhci_trb_clean(&cmd->trb);
249
250 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_NO_OP_CMD);
251
252 return enqueue_command(hc, cmd, 0, 0);
253}
254
255int xhci_send_enable_slot_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
256{
257 assert(hc);
258
259 xhci_trb_clean(&cmd->trb);
260
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);
263
264 return enqueue_command(hc, cmd, 0, 0);
265}
266
267int xhci_send_disable_slot_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
268{
269 assert(hc);
270 assert(cmd);
271
272 xhci_trb_clean(&cmd->trb);
273
274 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_DISABLE_SLOT_CMD);
275 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
276
277 return enqueue_command(hc, cmd, 0, 0);
278}
279
280int xhci_send_address_device_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
281{
282 assert(hc);
283 assert(cmd);
284 assert(ictx);
285
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 */
292
293 xhci_trb_clean(&cmd->trb);
294
295 uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
296 TRB_SET_ICTX(cmd->trb, phys_addr);
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 */
305 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_ADDRESS_DEVICE_CMD);
306 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
307
308 return enqueue_command(hc, cmd, 0, 0);
309}
310
311int xhci_send_configure_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
312{
313 assert(hc);
314 assert(cmd);
315 assert(ictx);
316
317 xhci_trb_clean(&cmd->trb);
318
319 uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
320 TRB_SET_ICTX(cmd->trb, phys_addr);
321
322 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_CONFIGURE_ENDPOINT_CMD);
323 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
324
325 return enqueue_command(hc, cmd, 0, 0);
326}
327
328int xhci_send_evaluate_context_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
329{
330 assert(hc);
331 assert(cmd);
332 assert(ictx);
333
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 */
340 xhci_trb_clean(&cmd->trb);
341
342 uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
343 TRB_SET_ICTX(cmd->trb, phys_addr);
344
345 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD);
346 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
347
348 return enqueue_command(hc, cmd, 0, 0);
349}
350
351int xhci_send_reset_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, uint32_t ep_id, uint8_t tcs)
352{
353 assert(hc);
354 assert(cmd);
355
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 */
360 xhci_trb_clean(&cmd->trb);
361
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);
366
367 return enqueue_command(hc, cmd, 0, 0);
368}
369
370int xhci_send_stop_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, uint32_t ep_id, uint8_t susp)
371{
372 assert(hc);
373 assert(cmd);
374
375 xhci_trb_clean(&cmd->trb);
376
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);
381
382 return enqueue_command(hc, cmd, 0, 0);
383}
384
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
392 xhci_trb_clean(&cmd->trb);
393
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);
399
400 /**
401 * TODO: Set DCS (see section 4.6.10).
402 */
403
404 return enqueue_command(hc, cmd, 0, 0);
405}
406
407int xhci_send_reset_device_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
408{
409 assert(hc);
410 assert(cmd);
411
412 xhci_trb_clean(&cmd->trb);
413
414 TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_RESET_DEVICE_CMD);
415 TRB_SET_SLOT(cmd->trb, cmd->slot_id);
416
417 return enqueue_command(hc, cmd, 0, 0);
418}
419
420int xhci_handle_command_completion(xhci_hc_t *hc, xhci_trb_t *trb)
421{
422 // TODO: Update dequeue ptrs.
423 assert(hc);
424 assert(trb);
425
426 usb_log_debug("HC(%p) Command completed.", hc);
427
428 int code;
429 uint64_t phys;
430 xhci_cmd_t *command;
431
432 code = TRB_GET_CODE(*trb);
433 phys = TRB_GET_PHYS(*trb);;
434 command = get_command(hc, phys);
435 if (command == NULL) {
436 // TODO: STOP & ABORT may not have command structs in the list!
437 usb_log_debug("No command struct for this completion event found.");
438
439 if (code != XHCI_TRBC_SUCCESS)
440 report_error(code);
441
442 return EOK;
443 }
444
445 command->status = code;
446 command->slot_id = TRB_GET_SLOT(*trb);
447
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) {
450 if (code != XHCI_TRBC_SUCCESS) {
451 report_error(code);
452 xhci_dump_trb(&command->trb);
453 }
454 }
455
456 switch (TRB_TYPE(command->trb)) {
457 case XHCI_TRB_TYPE_NO_OP_CMD:
458 assert(code == XHCI_TRBC_TRB_ERROR);
459 break;
460 case XHCI_TRB_TYPE_ENABLE_SLOT_CMD:
461 break;
462 case XHCI_TRB_TYPE_DISABLE_SLOT_CMD:
463 break;
464 case XHCI_TRB_TYPE_ADDRESS_DEVICE_CMD:
465 break;
466 case XHCI_TRB_TYPE_CONFIGURE_ENDPOINT_CMD:
467 break;
468 case XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD:
469 break;
470 case XHCI_TRB_TYPE_RESET_ENDPOINT_CMD:
471 break;
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!
476 break;
477 case XHCI_TRB_TYPE_RESET_DEVICE_CMD:
478 break;
479 default:
480 usb_log_debug2("Unsupported command trb: %s", xhci_trb_str_type(TRB_TYPE(command->trb)));
481
482 command->completed = true;
483 return ENAK;
484 }
485
486 fibril_mutex_lock(&command->completed_mtx);
487 command->completed = true;
488 fibril_condvar_broadcast(&command->completed_cv);
489 fibril_mutex_unlock(&command->completed_mtx);
490
491 return EOK;
492}
493
494
495/**
496 * @}
497 */
Note: See TracBrowser for help on using the repository browser.