source: mainline/uspace/drv/bus/usb/xhci/transfers.c@ 61e27e80

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 61e27e80 was 47e9494, checked in by Salmelu <salmelu@…>, 8 years ago

xhci: stub for streams support

  • Property mode set to 100644
File size: 12.7 KB
Line 
1/*
2 * Copyright (c) 2017 Michal Staruch
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 transfer ring management
34 */
35
36#include <usb/debug.h>
37#include <usb/request.h>
38#include "endpoint.h"
39#include "hc.h"
40#include "hw_struct/trb.h"
41#include "streams.h"
42#include "transfers.h"
43#include "trb_ring.h"
44
45typedef enum {
46 STAGE_OUT,
47 STAGE_IN,
48} stage_dir_flag_t;
49
50#define REQUEST_TYPE_DTD (0x80)
51#define REQUEST_TYPE_IS_DEVICE_TO_HOST(rq) ((rq) & REQUEST_TYPE_DTD)
52
53
54/** Get direction flag of data stage.
55 * See Table 7 of xHCI specification.
56 */
57static inline stage_dir_flag_t get_status_direction_flag(xhci_trb_t* trb,
58 uint8_t bmRequestType, uint16_t wLength)
59{
60 /* See Table 7 of xHCI specification */
61 return REQUEST_TYPE_IS_DEVICE_TO_HOST(bmRequestType) && (wLength > 0)
62 ? STAGE_OUT
63 : STAGE_IN;
64}
65
66typedef enum {
67 DATA_STAGE_NO = 0,
68 DATA_STAGE_OUT = 2,
69 DATA_STAGE_IN = 3,
70} data_stage_type_t;
71
72/** Get transfer type flag.
73 * See Table 8 of xHCI specification.
74 */
75static inline data_stage_type_t get_transfer_type(xhci_trb_t* trb, uint8_t
76 bmRequestType, uint16_t wLength)
77{
78 if (wLength == 0)
79 return DATA_STAGE_NO;
80
81 /* See Table 7 of xHCI specification */
82 return REQUEST_TYPE_IS_DEVICE_TO_HOST(bmRequestType)
83 ? DATA_STAGE_IN
84 : DATA_STAGE_NO;
85}
86
87static inline bool configure_endpoint_needed(usb_device_request_setup_packet_t *setup)
88{
89 usb_request_type_t request_type = SETUP_REQUEST_TYPE_GET_TYPE(setup->request_type);
90
91 return request_type == USB_REQUEST_TYPE_STANDARD &&
92 (setup->request == USB_DEVREQ_SET_CONFIGURATION
93 || setup->request == USB_DEVREQ_SET_INTERFACE);
94}
95
96/**
97 * Create a xHCI-specific transfer batch.
98 *
99 * Bus callback.
100 */
101usb_transfer_batch_t * xhci_transfer_create(endpoint_t* ep)
102{
103 xhci_transfer_t *transfer = calloc(1, sizeof(xhci_transfer_t));
104 if (!transfer)
105 return NULL;
106
107 usb_transfer_batch_init(&transfer->batch, ep);
108 return &transfer->batch;
109}
110
111/**
112 * Destroy a xHCI transfer.
113 */
114void xhci_transfer_destroy(usb_transfer_batch_t* batch)
115{
116 xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
117
118 dma_buffer_free(&transfer->hc_buffer);
119 free(transfer);
120}
121
122static xhci_trb_ring_t *get_ring(xhci_hc_t *hc, xhci_transfer_t *transfer)
123{
124 xhci_endpoint_t *ep = xhci_endpoint_get(transfer->batch.ep);
125 if (ep->primary_stream_data_size == 0) return &ep->ring;
126 uint32_t stream_id = transfer->batch.target.stream;
127
128 xhci_stream_data_t *stream_data = xhci_get_stream_ctx_data(ep, stream_id);
129 if (stream_data == NULL) {
130 usb_log_warning("No transfer ring was found for stream %u.", stream_id);
131 return NULL;
132 }
133
134 return &stream_data->ring;
135}
136
137static int schedule_control(xhci_hc_t* hc, xhci_transfer_t* transfer)
138{
139 usb_transfer_batch_t *batch = &transfer->batch;
140 xhci_trb_ring_t *ring = get_ring(hc, transfer);
141 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(transfer->batch.ep);
142
143 usb_device_request_setup_packet_t* setup = &batch->setup.packet;
144
145 xhci_trb_t trbs[3];
146 int trbs_used = 0;
147
148 xhci_trb_t *trb_setup = trbs + trbs_used++;
149 xhci_trb_clean(trb_setup);
150
151 TRB_CTRL_SET_SETUP_WVALUE(*trb_setup, setup->value);
152 TRB_CTRL_SET_SETUP_WLENGTH(*trb_setup, setup->length);
153 TRB_CTRL_SET_SETUP_WINDEX(*trb_setup, setup->index);
154 TRB_CTRL_SET_SETUP_BREQ(*trb_setup, setup->request);
155 TRB_CTRL_SET_SETUP_BMREQTYPE(*trb_setup, setup->request_type);
156
157 /* Size of the setup packet is always 8 */
158 TRB_CTRL_SET_XFER_LEN(*trb_setup, 8);
159
160 /* Immediate data */
161 TRB_CTRL_SET_IDT(*trb_setup, 1);
162 TRB_CTRL_SET_TRB_TYPE(*trb_setup, XHCI_TRB_TYPE_SETUP_STAGE);
163 TRB_CTRL_SET_TRT(*trb_setup, get_transfer_type(trb_setup, setup->request_type, setup->length));
164
165 /* Data stage */
166 xhci_trb_t *trb_data = NULL;
167 if (setup->length > 0) {
168 trb_data = trbs + trbs_used++;
169 xhci_trb_clean(trb_data);
170
171 trb_data->parameter = host2xhci(64, transfer->hc_buffer.phys);
172
173 // data size (sent for OUT, or buffer size)
174 TRB_CTRL_SET_XFER_LEN(*trb_data, batch->buffer_size);
175 // FIXME: TD size 4.11.2.4
176 TRB_CTRL_SET_TD_SIZE(*trb_data, 1);
177
178 // Some more fields here, no idea what they mean
179 TRB_CTRL_SET_TRB_TYPE(*trb_data, XHCI_TRB_TYPE_DATA_STAGE);
180
181 int stage_dir = REQUEST_TYPE_IS_DEVICE_TO_HOST(setup->request_type)
182 ? STAGE_IN : STAGE_OUT;
183 TRB_CTRL_SET_DIR(*trb_data, stage_dir);
184 }
185
186 /* Status stage */
187 xhci_trb_t *trb_status = trbs + trbs_used++;
188 xhci_trb_clean(trb_status);
189
190 // FIXME: Evaluate next TRB? 4.12.3
191 // TRB_CTRL_SET_ENT(*trb_status, 1);
192
193 TRB_CTRL_SET_IOC(*trb_status, 1);
194 TRB_CTRL_SET_TRB_TYPE(*trb_status, XHCI_TRB_TYPE_STATUS_STAGE);
195 TRB_CTRL_SET_DIR(*trb_status, get_status_direction_flag(trb_setup, setup->request_type, setup->length));
196
197 // Issue a Configure Endpoint command, if needed.
198 if (configure_endpoint_needed(setup)) {
199 const int err = hc_configure_device(hc, xhci_ep_to_dev(xhci_ep)->slot_id);
200 if (err)
201 return err;
202 }
203
204 return xhci_trb_ring_enqueue_multiple(ring, trbs, trbs_used, &transfer->interrupt_trb_phys);
205}
206
207static int schedule_bulk(xhci_hc_t* hc, xhci_transfer_t *transfer)
208{
209 xhci_trb_t trb;
210 xhci_trb_clean(&trb);
211 trb.parameter = host2xhci(64, transfer->hc_buffer.phys);
212
213 // data size (sent for OUT, or buffer size)
214 TRB_CTRL_SET_XFER_LEN(trb, transfer->batch.buffer_size);
215
216 /* The stream-enabled endpoints need to chain ED trb */
217 xhci_endpoint_t *ep = xhci_endpoint_get(transfer->batch.ep);
218 if (!ep->primary_stream_data_size) {
219 // FIXME: TD size 4.11.2.4
220 TRB_CTRL_SET_TD_SIZE(trb, 1);
221
222 // we want an interrupt after this td is done
223 TRB_CTRL_SET_IOC(trb, 1);
224 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
225
226 xhci_trb_ring_t* ring = get_ring(hc, transfer);
227 return xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
228 }
229 else {
230 TRB_CTRL_SET_TD_SIZE(trb, 2);
231 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
232 TRB_CTRL_SET_CHAIN(trb, 1);
233 TRB_CTRL_SET_ENT(trb, 1);
234
235 xhci_trb_ring_t* ring = get_ring(hc, transfer);
236 int err = xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
237
238 if (err) {
239 return err;
240 }
241
242 xhci_trb_clean(&trb);
243 trb.parameter = host2xhci(64, (uintptr_t) transfer);
244 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_EVENT_DATA);
245 TRB_CTRL_SET_IOC(trb, 1);
246
247 return xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
248 }
249}
250
251static int schedule_interrupt(xhci_hc_t* hc, xhci_transfer_t* transfer)
252{
253 xhci_trb_t trb;
254 xhci_trb_clean(&trb);
255 trb.parameter = host2xhci(64, transfer->hc_buffer.phys);
256
257 // data size (sent for OUT, or buffer size)
258 TRB_CTRL_SET_XFER_LEN(trb, transfer->batch.buffer_size);
259 // FIXME: TD size 4.11.2.4
260 TRB_CTRL_SET_TD_SIZE(trb, 1);
261
262 // we want an interrupt after this td is done
263 TRB_CTRL_SET_IOC(trb, 1);
264
265 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
266
267 xhci_trb_ring_t* ring = get_ring(hc, transfer);
268
269 return xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
270}
271
272static int schedule_isochronous(xhci_transfer_t* transfer)
273{
274 endpoint_t *ep = transfer->batch.ep;
275
276 return ep->direction == USB_DIRECTION_OUT
277 ? isoch_schedule_out(transfer)
278 : isoch_schedule_in(transfer);
279}
280
281int xhci_handle_transfer_event(xhci_hc_t* hc, xhci_trb_t* trb)
282{
283 uintptr_t addr = trb->parameter;
284 const unsigned slot_id = XHCI_DWORD_EXTRACT(trb->control, 31, 24);
285 const unsigned ep_dci = XHCI_DWORD_EXTRACT(trb->control, 20, 16);
286
287 xhci_device_t *dev = hc->bus.devices_by_slot[slot_id];
288 if (!dev) {
289 usb_log_error("Transfer event on disabled slot %u", slot_id);
290 return ENOENT;
291 }
292
293 const usb_endpoint_t ep_num = ep_dci / 2;
294 const usb_endpoint_t dir = ep_dci % 2 ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
295 endpoint_t *ep_base = bus_find_endpoint(&dev->base, ep_num, dir);
296 if (!ep_base) {
297 usb_log_error("Transfer event on dropped endpoint %u %s of device "
298 XHCI_DEV_FMT, ep_num, usb_str_direction(dir), XHCI_DEV_ARGS(*dev));
299 return ENOENT;
300 }
301 xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
302
303 usb_transfer_batch_t *batch;
304 xhci_transfer_t *transfer;
305
306 if (TRB_EVENT_DATA(*trb)) {
307 assert(ep->base.transfer_type != USB_TRANSFER_ISOCHRONOUS);
308 /* We are received transfer pointer instead - work with that */
309 transfer = (xhci_transfer_t *) addr;
310 xhci_trb_ring_t * ring = get_ring(hc, transfer);
311 xhci_trb_ring_update_dequeue(ring, transfer->interrupt_trb_phys);
312 batch = &transfer->batch;
313
314 fibril_mutex_lock(&ep->base.guard);
315 endpoint_deactivate_locked(&ep->base);
316 fibril_mutex_unlock(&ep->base.guard);
317 }
318 else {
319 xhci_trb_ring_update_dequeue(&ep->ring, addr);
320
321 if (ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
322 isoch_handle_transfer_event(hc, ep, trb);
323 endpoint_del_ref(&ep->base);
324 return EOK;
325 }
326
327 fibril_mutex_lock(&ep->base.guard);
328 batch = ep->base.active_batch;
329 if (!batch) {
330 fibril_mutex_unlock(&ep->base.guard);
331 endpoint_del_ref(&ep->base);
332 return ENOENT;
333 }
334
335 transfer = xhci_transfer_from_batch(batch);
336
337 endpoint_deactivate_locked(&ep->base);
338 fibril_mutex_unlock(&ep->base.guard);
339 }
340
341 const xhci_trb_completion_code_t completion_code = TRB_COMPLETION_CODE(*trb);
342 switch (completion_code) {
343 case XHCI_TRBC_SHORT_PACKET:
344 case XHCI_TRBC_SUCCESS:
345 batch->error = EOK;
346 batch->transfered_size = batch->buffer_size - TRB_TRANSFER_LENGTH(*trb);
347 break;
348
349 default:
350 usb_log_warning("Transfer not successfull: %u", completion_code);
351 batch->error = EIO;
352 }
353
354 if (batch->dir == USB_DIRECTION_IN) {
355 assert(batch->buffer);
356 assert(batch->transfered_size <= batch->buffer_size);
357 memcpy(batch->buffer, transfer->hc_buffer.virt, batch->transfered_size);
358 }
359
360 usb_transfer_batch_finish(batch);
361 endpoint_del_ref(&ep->base);
362 return EOK;
363}
364
365typedef int (*transfer_handler)(xhci_hc_t *, xhci_transfer_t *);
366
367static const transfer_handler transfer_handlers[] = {
368 [USB_TRANSFER_CONTROL] = schedule_control,
369 [USB_TRANSFER_ISOCHRONOUS] = NULL,
370 [USB_TRANSFER_BULK] = schedule_bulk,
371 [USB_TRANSFER_INTERRUPT] = schedule_interrupt,
372};
373
374int xhci_transfer_schedule(xhci_hc_t *hc, usb_transfer_batch_t *batch)
375{
376 assert(hc);
377 endpoint_t *ep = batch->ep;
378
379 xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
380 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
381 xhci_device_t *xhci_dev = xhci_ep_to_dev(xhci_ep);
382
383 // FIXME: find a better way to check if the ring is not initialized
384 if (!xhci_ep->ring.segment_count) {
385 usb_log_error("Ring not initialized for endpoint " XHCI_EP_FMT,
386 XHCI_EP_ARGS(*xhci_ep));
387 return EINVAL;
388 }
389
390 // Isochronous transfer needs to be handled differently
391 if (batch->ep->transfer_type == USB_TRANSFER_ISOCHRONOUS) {
392 return schedule_isochronous(transfer);
393 }
394
395 const usb_transfer_type_t type = batch->ep->transfer_type;
396 assert(transfer_handlers[type]);
397
398 if (batch->buffer_size > 0) {
399 if (dma_buffer_alloc(&transfer->hc_buffer, batch->buffer_size))
400 return ENOMEM;
401 }
402
403 if (batch->dir != USB_DIRECTION_IN) {
404 // Sending stuff from host to device, we need to copy the actual data.
405 memcpy(transfer->hc_buffer.virt, batch->buffer, batch->buffer_size);
406 }
407
408 fibril_mutex_lock(&ep->guard);
409 endpoint_activate_locked(ep, batch);
410 const int err = transfer_handlers[batch->ep->transfer_type](hc, transfer);
411
412 if (err) {
413 endpoint_deactivate_locked(ep);
414 fibril_mutex_unlock(&ep->guard);
415 return err;
416 }
417
418 /* After the critical section, the transfer can already be finished or aborted. */
419 transfer = NULL; batch = NULL;
420 fibril_mutex_unlock(&ep->guard);
421
422 const uint8_t slot_id = xhci_dev->slot_id;
423 /* EP Doorbells start at 1 */
424 const uint8_t target = (xhci_endpoint_index(xhci_ep) + 1) | (batch->target.stream << 16);
425 hc_ring_doorbell(hc, slot_id, target);
426 return EOK;
427}
Note: See TracBrowser for help on using the repository browser.