blob: 8d6bdf2f8015a93f57e841a80631b2a13b2d19c9 [file] [log] [blame]
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/usb.h>
Sarah Sharp0ebbab32009-04-27 19:52:34 -070024#include <linux/pci.h>
Sarah Sharp527c6d72009-04-29 19:06:56 -070025#include <linux/dmapool.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070026
27#include "xhci.h"
28
Sarah Sharp0ebbab32009-04-27 19:52:34 -070029/*
30 * Allocates a generic ring segment from the ring pool, sets the dma address,
31 * initializes the segment to zero, and sets the private next pointer to NULL.
32 *
33 * Section 4.11.1.1:
34 * "All components of all Command and Transfer TRBs shall be initialized to '0'"
35 */
36static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, gfp_t flags)
37{
38 struct xhci_segment *seg;
39 dma_addr_t dma;
40
41 seg = kzalloc(sizeof *seg, flags);
42 if (!seg)
43 return 0;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -070044 xhci_dbg(xhci, "Allocating priv segment structure at %p\n", seg);
Sarah Sharp0ebbab32009-04-27 19:52:34 -070045
46 seg->trbs = dma_pool_alloc(xhci->segment_pool, flags, &dma);
47 if (!seg->trbs) {
48 kfree(seg);
49 return 0;
50 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -070051 xhci_dbg(xhci, "// Allocating segment at %p (virtual) 0x%llx (DMA)\n",
52 seg->trbs, (unsigned long long)dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -070053
54 memset(seg->trbs, 0, SEGMENT_SIZE);
55 seg->dma = dma;
56 seg->next = NULL;
57
58 return seg;
59}
60
61static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
62{
63 if (!seg)
64 return;
65 if (seg->trbs) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -070066 xhci_dbg(xhci, "Freeing DMA segment at %p (virtual) 0x%llx (DMA)\n",
67 seg->trbs, (unsigned long long)seg->dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -070068 dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
69 seg->trbs = NULL;
70 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -070071 xhci_dbg(xhci, "Freeing priv segment structure at %p\n", seg);
Sarah Sharp0ebbab32009-04-27 19:52:34 -070072 kfree(seg);
73}
74
75/*
76 * Make the prev segment point to the next segment.
77 *
78 * Change the last TRB in the prev segment to be a Link TRB which points to the
79 * DMA address of the next segment. The caller needs to set any Link TRB
80 * related flags, such as End TRB, Toggle Cycle, and no snoop.
81 */
82static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
83 struct xhci_segment *next, bool link_trbs)
84{
85 u32 val;
86
87 if (!prev || !next)
88 return;
89 prev->next = next;
90 if (link_trbs) {
Sarah Sharp8e595a52009-07-27 12:03:31 -070091 prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = next->dma;
Sarah Sharp0ebbab32009-04-27 19:52:34 -070092
93 /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
94 val = prev->trbs[TRBS_PER_SEGMENT-1].link.control;
95 val &= ~TRB_TYPE_BITMASK;
96 val |= TRB_TYPE(TRB_LINK);
97 prev->trbs[TRBS_PER_SEGMENT-1].link.control = val;
98 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -070099 xhci_dbg(xhci, "Linking segment 0x%llx to segment 0x%llx (DMA)\n",
100 (unsigned long long)prev->dma,
101 (unsigned long long)next->dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700102}
103
104/* XXX: Do we need the hcd structure in all these functions? */
Sarah Sharpf94e01862009-04-27 19:58:38 -0700105void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700106{
107 struct xhci_segment *seg;
108 struct xhci_segment *first_seg;
109
110 if (!ring || !ring->first_seg)
111 return;
112 first_seg = ring->first_seg;
113 seg = first_seg->next;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700114 xhci_dbg(xhci, "Freeing ring at %p\n", ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700115 while (seg != first_seg) {
116 struct xhci_segment *next = seg->next;
117 xhci_segment_free(xhci, seg);
118 seg = next;
119 }
120 xhci_segment_free(xhci, first_seg);
121 ring->first_seg = NULL;
122 kfree(ring);
123}
124
125/**
126 * Create a new ring with zero or more segments.
127 *
128 * Link each segment together into a ring.
129 * Set the end flag and the cycle toggle bit on the last segment.
130 * See section 4.9.1 and figures 15 and 16.
131 */
132static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
133 unsigned int num_segs, bool link_trbs, gfp_t flags)
134{
135 struct xhci_ring *ring;
136 struct xhci_segment *prev;
137
138 ring = kzalloc(sizeof *(ring), flags);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700139 xhci_dbg(xhci, "Allocating ring at %p\n", ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700140 if (!ring)
141 return 0;
142
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700143 INIT_LIST_HEAD(&ring->td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700144 INIT_LIST_HEAD(&ring->cancelled_td_list);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700145 if (num_segs == 0)
146 return ring;
147
148 ring->first_seg = xhci_segment_alloc(xhci, flags);
149 if (!ring->first_seg)
150 goto fail;
151 num_segs--;
152
153 prev = ring->first_seg;
154 while (num_segs > 0) {
155 struct xhci_segment *next;
156
157 next = xhci_segment_alloc(xhci, flags);
158 if (!next)
159 goto fail;
160 xhci_link_segments(xhci, prev, next, link_trbs);
161
162 prev = next;
163 num_segs--;
164 }
165 xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
166
167 if (link_trbs) {
168 /* See section 4.9.2.1 and 6.4.4.1 */
169 prev->trbs[TRBS_PER_SEGMENT-1].link.control |= (LINK_TOGGLE);
170 xhci_dbg(xhci, "Wrote link toggle flag to"
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700171 " segment %p (virtual), 0x%llx (DMA)\n",
172 prev, (unsigned long long)prev->dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700173 }
174 /* The ring is empty, so the enqueue pointer == dequeue pointer */
175 ring->enqueue = ring->first_seg->trbs;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700176 ring->enq_seg = ring->first_seg;
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700177 ring->dequeue = ring->enqueue;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700178 ring->deq_seg = ring->first_seg;
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700179 /* The ring is initialized to 0. The producer must write 1 to the cycle
180 * bit to handover ownership of the TRB, so PCS = 1. The consumer must
181 * compare CCS to the cycle bit to check ownership, so CCS = 1.
182 */
183 ring->cycle_state = 1;
184
185 return ring;
186
187fail:
188 xhci_ring_free(xhci, ring);
189 return 0;
190}
191
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700192/* All the xhci_tds in the ring's TD list should be freed at this point */
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700193void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
194{
195 struct xhci_virt_device *dev;
196 int i;
197
198 /* Slot ID 0 is reserved */
199 if (slot_id == 0 || !xhci->devs[slot_id])
200 return;
201
202 dev = xhci->devs[slot_id];
Sarah Sharp8e595a52009-07-27 12:03:31 -0700203 xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700204 if (!dev)
205 return;
206
207 for (i = 0; i < 31; ++i)
208 if (dev->ep_rings[i])
209 xhci_ring_free(xhci, dev->ep_rings[i]);
210
211 if (dev->in_ctx)
212 dma_pool_free(xhci->device_pool,
213 dev->in_ctx, dev->in_ctx_dma);
214 if (dev->out_ctx)
215 dma_pool_free(xhci->device_pool,
216 dev->out_ctx, dev->out_ctx_dma);
217 kfree(xhci->devs[slot_id]);
218 xhci->devs[slot_id] = 0;
219}
220
221int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
222 struct usb_device *udev, gfp_t flags)
223{
224 dma_addr_t dma;
225 struct xhci_virt_device *dev;
226
227 /* Slot ID 0 is reserved */
228 if (slot_id == 0 || xhci->devs[slot_id]) {
229 xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
230 return 0;
231 }
232
233 xhci->devs[slot_id] = kzalloc(sizeof(*xhci->devs[slot_id]), flags);
234 if (!xhci->devs[slot_id])
235 return 0;
236 dev = xhci->devs[slot_id];
237
Sarah Sharp28c2d2e2009-07-27 12:05:08 -0700238 /* Allocate the (output) device context that will be used in the HC.
239 * The structure is 32 bytes smaller than the input context, but that's
240 * fine.
241 */
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700242 dev->out_ctx = dma_pool_alloc(xhci->device_pool, flags, &dma);
243 if (!dev->out_ctx)
244 goto fail;
245 dev->out_ctx_dma = dma;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700246 xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
247 (unsigned long long)dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700248 memset(dev->out_ctx, 0, sizeof(*dev->out_ctx));
249
250 /* Allocate the (input) device context for address device command */
251 dev->in_ctx = dma_pool_alloc(xhci->device_pool, flags, &dma);
252 if (!dev->in_ctx)
253 goto fail;
254 dev->in_ctx_dma = dma;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700255 xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
256 (unsigned long long)dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700257 memset(dev->in_ctx, 0, sizeof(*dev->in_ctx));
258
259 /* Allocate endpoint 0 ring */
260 dev->ep_rings[0] = xhci_ring_alloc(xhci, 1, true, flags);
261 if (!dev->ep_rings[0])
262 goto fail;
263
Sarah Sharpf94e01862009-04-27 19:58:38 -0700264 init_completion(&dev->cmd_completion);
265
Sarah Sharp28c2d2e2009-07-27 12:05:08 -0700266 /* Point to output device context in dcbaa. */
267 xhci->dcbaa->dev_context_ptrs[slot_id] = dev->out_ctx_dma;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700268 xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700269 slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -0700270 &xhci->dcbaa->dev_context_ptrs[slot_id],
Sarah Sharp28c2d2e2009-07-27 12:05:08 -0700271 (unsigned long long) xhci->dcbaa->dev_context_ptrs[slot_id]);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700272
273 return 1;
274fail:
275 xhci_free_virt_device(xhci, slot_id);
276 return 0;
277}
278
279/* Setup an xHCI virtual device for a Set Address command */
280int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
281{
282 struct xhci_virt_device *dev;
283 struct xhci_ep_ctx *ep0_ctx;
284 struct usb_device *top_dev;
285
286 dev = xhci->devs[udev->slot_id];
287 /* Slot ID 0 is reserved */
288 if (udev->slot_id == 0 || !dev) {
289 xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
290 udev->slot_id);
291 return -EINVAL;
292 }
293 ep0_ctx = &dev->in_ctx->ep[0];
294
295 /* 2) New slot context and endpoint 0 context are valid*/
296 dev->in_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
297
298 /* 3) Only the control endpoint is valid - one endpoint context */
299 dev->in_ctx->slot.dev_info |= LAST_CTX(1);
300
301 switch (udev->speed) {
302 case USB_SPEED_SUPER:
303 dev->in_ctx->slot.dev_info |= (u32) udev->route;
304 dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_SS;
305 break;
306 case USB_SPEED_HIGH:
307 dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_HS;
308 break;
309 case USB_SPEED_FULL:
310 dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_FS;
311 break;
312 case USB_SPEED_LOW:
313 dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_LS;
314 break;
315 case USB_SPEED_VARIABLE:
316 xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
317 return -EINVAL;
318 break;
319 default:
320 /* Speed was set earlier, this shouldn't happen. */
321 BUG();
322 }
323 /* Find the root hub port this device is under */
324 for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
325 top_dev = top_dev->parent)
326 /* Found device below root hub */;
327 dev->in_ctx->slot.dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
328 xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
329
330 /* Is this a LS/FS device under a HS hub? */
331 /*
332 * FIXME: I don't think this is right, where does the TT info for the
333 * roothub or parent hub come from?
334 */
335 if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
336 udev->tt) {
337 dev->in_ctx->slot.tt_info = udev->tt->hub->slot_id;
338 dev->in_ctx->slot.tt_info |= udev->ttport << 8;
339 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700340 xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700341 xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
342
343 /* Step 4 - ring already allocated */
344 /* Step 5 */
345 ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
346 /*
347 * See section 4.3 bullet 6:
348 * The default Max Packet size for ep0 is "8 bytes for a USB2
349 * LS/FS/HS device or 512 bytes for a USB3 SS device"
350 * XXX: Not sure about wireless USB devices.
351 */
352 if (udev->speed == USB_SPEED_SUPER)
353 ep0_ctx->ep_info2 |= MAX_PACKET(512);
354 else
355 ep0_ctx->ep_info2 |= MAX_PACKET(8);
356 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
357 ep0_ctx->ep_info2 |= MAX_BURST(0);
358 ep0_ctx->ep_info2 |= ERROR_COUNT(3);
359
Sarah Sharp8e595a52009-07-27 12:03:31 -0700360 ep0_ctx->deq =
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700361 dev->ep_rings[0]->first_seg->dma;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700362 ep0_ctx->deq |= dev->ep_rings[0]->cycle_state;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700363
364 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
365
366 return 0;
367}
368
Sarah Sharpf94e01862009-04-27 19:58:38 -0700369/* Return the polling or NAK interval.
370 *
371 * The polling interval is expressed in "microframes". If xHCI's Interval field
372 * is set to N, it will service the endpoint every 2^(Interval)*125us.
373 *
374 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
375 * is set to 0.
376 */
377static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
378 struct usb_host_endpoint *ep)
379{
380 unsigned int interval = 0;
381
382 switch (udev->speed) {
383 case USB_SPEED_HIGH:
384 /* Max NAK rate */
385 if (usb_endpoint_xfer_control(&ep->desc) ||
386 usb_endpoint_xfer_bulk(&ep->desc))
387 interval = ep->desc.bInterval;
388 /* Fall through - SS and HS isoc/int have same decoding */
389 case USB_SPEED_SUPER:
390 if (usb_endpoint_xfer_int(&ep->desc) ||
391 usb_endpoint_xfer_isoc(&ep->desc)) {
392 if (ep->desc.bInterval == 0)
393 interval = 0;
394 else
395 interval = ep->desc.bInterval - 1;
396 if (interval > 15)
397 interval = 15;
398 if (interval != ep->desc.bInterval + 1)
399 dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
400 ep->desc.bEndpointAddress, 1 << interval);
401 }
402 break;
403 /* Convert bInterval (in 1-255 frames) to microframes and round down to
404 * nearest power of 2.
405 */
406 case USB_SPEED_FULL:
407 case USB_SPEED_LOW:
408 if (usb_endpoint_xfer_int(&ep->desc) ||
409 usb_endpoint_xfer_isoc(&ep->desc)) {
410 interval = fls(8*ep->desc.bInterval) - 1;
411 if (interval > 10)
412 interval = 10;
413 if (interval < 3)
414 interval = 3;
415 if ((1 << interval) != 8*ep->desc.bInterval)
416 dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
417 ep->desc.bEndpointAddress, 1 << interval);
418 }
419 break;
420 default:
421 BUG();
422 }
423 return EP_INTERVAL(interval);
424}
425
426static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
427 struct usb_host_endpoint *ep)
428{
429 int in;
430 u32 type;
431
432 in = usb_endpoint_dir_in(&ep->desc);
433 if (usb_endpoint_xfer_control(&ep->desc)) {
434 type = EP_TYPE(CTRL_EP);
435 } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
436 if (in)
437 type = EP_TYPE(BULK_IN_EP);
438 else
439 type = EP_TYPE(BULK_OUT_EP);
440 } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
441 if (in)
442 type = EP_TYPE(ISOC_IN_EP);
443 else
444 type = EP_TYPE(ISOC_OUT_EP);
445 } else if (usb_endpoint_xfer_int(&ep->desc)) {
446 if (in)
447 type = EP_TYPE(INT_IN_EP);
448 else
449 type = EP_TYPE(INT_OUT_EP);
450 } else {
451 BUG();
452 }
453 return type;
454}
455
456int xhci_endpoint_init(struct xhci_hcd *xhci,
457 struct xhci_virt_device *virt_dev,
458 struct usb_device *udev,
Sarah Sharpf88ba782009-05-14 11:44:22 -0700459 struct usb_host_endpoint *ep,
460 gfp_t mem_flags)
Sarah Sharpf94e01862009-04-27 19:58:38 -0700461{
462 unsigned int ep_index;
463 struct xhci_ep_ctx *ep_ctx;
464 struct xhci_ring *ep_ring;
465 unsigned int max_packet;
466 unsigned int max_burst;
467
468 ep_index = xhci_get_endpoint_index(&ep->desc);
469 ep_ctx = &virt_dev->in_ctx->ep[ep_index];
470
471 /* Set up the endpoint ring */
Sarah Sharpf88ba782009-05-14 11:44:22 -0700472 virt_dev->new_ep_rings[ep_index] = xhci_ring_alloc(xhci, 1, true, mem_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700473 if (!virt_dev->new_ep_rings[ep_index])
474 return -ENOMEM;
475 ep_ring = virt_dev->new_ep_rings[ep_index];
Sarah Sharp8e595a52009-07-27 12:03:31 -0700476 ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700477
478 ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
479
480 /* FIXME dig Mult and streams info out of ep companion desc */
481
Sarah Sharp47692d12009-07-27 12:04:27 -0700482 /* Allow 3 retries for everything but isoc;
483 * error count = 0 means infinite retries.
484 */
Sarah Sharpf94e01862009-04-27 19:58:38 -0700485 if (!usb_endpoint_xfer_isoc(&ep->desc))
486 ep_ctx->ep_info2 = ERROR_COUNT(3);
487 else
Sarah Sharp47692d12009-07-27 12:04:27 -0700488 ep_ctx->ep_info2 = ERROR_COUNT(1);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700489
490 ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
491
492 /* Set the max packet size and max burst */
493 switch (udev->speed) {
494 case USB_SPEED_SUPER:
495 max_packet = ep->desc.wMaxPacketSize;
496 ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
Sarah Sharpb10de142009-04-27 19:58:50 -0700497 /* dig out max burst from ep companion desc */
Sarah Sharpb7d6d992009-07-27 12:04:38 -0700498 if (!ep->ss_ep_comp) {
499 xhci_warn(xhci, "WARN no SS endpoint companion descriptor.\n");
500 max_packet = 0;
501 } else {
502 max_packet = ep->ss_ep_comp->desc.bMaxBurst;
503 }
Sarah Sharpb10de142009-04-27 19:58:50 -0700504 ep_ctx->ep_info2 |= MAX_BURST(max_packet);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700505 break;
506 case USB_SPEED_HIGH:
507 /* bits 11:12 specify the number of additional transaction
508 * opportunities per microframe (USB 2.0, section 9.6.6)
509 */
510 if (usb_endpoint_xfer_isoc(&ep->desc) ||
511 usb_endpoint_xfer_int(&ep->desc)) {
512 max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
513 ep_ctx->ep_info2 |= MAX_BURST(max_burst);
514 }
515 /* Fall through */
516 case USB_SPEED_FULL:
517 case USB_SPEED_LOW:
518 max_packet = ep->desc.wMaxPacketSize & 0x3ff;
519 ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
520 break;
521 default:
522 BUG();
523 }
524 /* FIXME Debug endpoint context */
525 return 0;
526}
527
528void xhci_endpoint_zero(struct xhci_hcd *xhci,
529 struct xhci_virt_device *virt_dev,
530 struct usb_host_endpoint *ep)
531{
532 unsigned int ep_index;
533 struct xhci_ep_ctx *ep_ctx;
534
535 ep_index = xhci_get_endpoint_index(&ep->desc);
536 ep_ctx = &virt_dev->in_ctx->ep[ep_index];
537
538 ep_ctx->ep_info = 0;
539 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700540 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700541 ep_ctx->tx_info = 0;
542 /* Don't free the endpoint ring until the set interface or configuration
543 * request succeeds.
544 */
545}
546
John Youn254c80a2009-07-27 12:05:03 -0700547/* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
548static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
549{
550 int i;
551 struct device *dev = xhci_to_hcd(xhci)->self.controller;
552 int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
553
554 xhci_dbg(xhci, "Allocating %d scratchpad buffers\n", num_sp);
555
556 if (!num_sp)
557 return 0;
558
559 xhci->scratchpad = kzalloc(sizeof(*xhci->scratchpad), flags);
560 if (!xhci->scratchpad)
561 goto fail_sp;
562
563 xhci->scratchpad->sp_array =
564 pci_alloc_consistent(to_pci_dev(dev),
565 num_sp * sizeof(u64),
566 &xhci->scratchpad->sp_dma);
567 if (!xhci->scratchpad->sp_array)
568 goto fail_sp2;
569
570 xhci->scratchpad->sp_buffers = kzalloc(sizeof(void *) * num_sp, flags);
571 if (!xhci->scratchpad->sp_buffers)
572 goto fail_sp3;
573
574 xhci->scratchpad->sp_dma_buffers =
575 kzalloc(sizeof(dma_addr_t) * num_sp, flags);
576
577 if (!xhci->scratchpad->sp_dma_buffers)
578 goto fail_sp4;
579
580 xhci->dcbaa->dev_context_ptrs[0] = xhci->scratchpad->sp_dma;
581 for (i = 0; i < num_sp; i++) {
582 dma_addr_t dma;
583 void *buf = pci_alloc_consistent(to_pci_dev(dev),
584 xhci->page_size, &dma);
585 if (!buf)
586 goto fail_sp5;
587
588 xhci->scratchpad->sp_array[i] = dma;
589 xhci->scratchpad->sp_buffers[i] = buf;
590 xhci->scratchpad->sp_dma_buffers[i] = dma;
591 }
592
593 return 0;
594
595 fail_sp5:
596 for (i = i - 1; i >= 0; i--) {
597 pci_free_consistent(to_pci_dev(dev), xhci->page_size,
598 xhci->scratchpad->sp_buffers[i],
599 xhci->scratchpad->sp_dma_buffers[i]);
600 }
601 kfree(xhci->scratchpad->sp_dma_buffers);
602
603 fail_sp4:
604 kfree(xhci->scratchpad->sp_buffers);
605
606 fail_sp3:
607 pci_free_consistent(to_pci_dev(dev), num_sp * sizeof(u64),
608 xhci->scratchpad->sp_array,
609 xhci->scratchpad->sp_dma);
610
611 fail_sp2:
612 kfree(xhci->scratchpad);
613 xhci->scratchpad = NULL;
614
615 fail_sp:
616 return -ENOMEM;
617}
618
619static void scratchpad_free(struct xhci_hcd *xhci)
620{
621 int num_sp;
622 int i;
623 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
624
625 if (!xhci->scratchpad)
626 return;
627
628 num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
629
630 for (i = 0; i < num_sp; i++) {
631 pci_free_consistent(pdev, xhci->page_size,
632 xhci->scratchpad->sp_buffers[i],
633 xhci->scratchpad->sp_dma_buffers[i]);
634 }
635 kfree(xhci->scratchpad->sp_dma_buffers);
636 kfree(xhci->scratchpad->sp_buffers);
637 pci_free_consistent(pdev, num_sp * sizeof(u64),
638 xhci->scratchpad->sp_array,
639 xhci->scratchpad->sp_dma);
640 kfree(xhci->scratchpad);
641 xhci->scratchpad = NULL;
642}
643
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700644void xhci_mem_cleanup(struct xhci_hcd *xhci)
645{
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700646 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
647 int size;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700648 int i;
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700649
650 /* Free the Event Ring Segment Table and the actual Event Ring */
651 xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700652 xhci_write_64(xhci, 0, &xhci->ir_set->erst_base);
653 xhci_write_64(xhci, 0, &xhci->ir_set->erst_dequeue);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700654 size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
655 if (xhci->erst.entries)
656 pci_free_consistent(pdev, size,
657 xhci->erst.entries, xhci->erst.erst_dma_addr);
658 xhci->erst.entries = NULL;
659 xhci_dbg(xhci, "Freed ERST\n");
660 if (xhci->event_ring)
661 xhci_ring_free(xhci, xhci->event_ring);
662 xhci->event_ring = NULL;
663 xhci_dbg(xhci, "Freed event ring\n");
664
Sarah Sharp8e595a52009-07-27 12:03:31 -0700665 xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700666 if (xhci->cmd_ring)
667 xhci_ring_free(xhci, xhci->cmd_ring);
668 xhci->cmd_ring = NULL;
669 xhci_dbg(xhci, "Freed command ring\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700670
671 for (i = 1; i < MAX_HC_SLOTS; ++i)
672 xhci_free_virt_device(xhci, i);
673
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700674 if (xhci->segment_pool)
675 dma_pool_destroy(xhci->segment_pool);
676 xhci->segment_pool = NULL;
677 xhci_dbg(xhci, "Freed segment pool\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700678
679 if (xhci->device_pool)
680 dma_pool_destroy(xhci->device_pool);
681 xhci->device_pool = NULL;
682 xhci_dbg(xhci, "Freed device context pool\n");
683
Sarah Sharp8e595a52009-07-27 12:03:31 -0700684 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
Sarah Sharpa74588f2009-04-27 19:53:42 -0700685 if (xhci->dcbaa)
686 pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
687 xhci->dcbaa, xhci->dcbaa->dma);
688 xhci->dcbaa = NULL;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700689
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700690 xhci->page_size = 0;
691 xhci->page_shift = 0;
John Youn254c80a2009-07-27 12:05:03 -0700692 scratchpad_free(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700693}
694
695int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
696{
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700697 dma_addr_t dma;
698 struct device *dev = xhci_to_hcd(xhci)->self.controller;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700699 unsigned int val, val2;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700700 u64 val_64;
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700701 struct xhci_segment *seg;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700702 u32 page_size;
703 int i;
704
705 page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
706 xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
707 for (i = 0; i < 16; i++) {
708 if ((0x1 & page_size) != 0)
709 break;
710 page_size = page_size >> 1;
711 }
712 if (i < 16)
713 xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
714 else
715 xhci_warn(xhci, "WARN: no supported page size\n");
716 /* Use 4K pages, since that's common and the minimum the HC supports */
717 xhci->page_shift = 12;
718 xhci->page_size = 1 << xhci->page_shift;
719 xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
720
721 /*
722 * Program the Number of Device Slots Enabled field in the CONFIG
723 * register with the max value of slots the HC can handle.
724 */
725 val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
726 xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
727 (unsigned int) val);
728 val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
729 val |= (val2 & ~HCS_SLOTS_MASK);
730 xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
731 (unsigned int) val);
732 xhci_writel(xhci, val, &xhci->op_regs->config_reg);
733
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700734 /*
Sarah Sharpa74588f2009-04-27 19:53:42 -0700735 * Section 5.4.8 - doorbell array must be
736 * "physically contiguous and 64-byte (cache line) aligned".
737 */
738 xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
739 sizeof(*xhci->dcbaa), &dma);
740 if (!xhci->dcbaa)
741 goto fail;
742 memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
743 xhci->dcbaa->dma = dma;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700744 xhci_dbg(xhci, "// Device context base array address = 0x%llx (DMA), %p (virt)\n",
745 (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700746 xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
Sarah Sharpa74588f2009-04-27 19:53:42 -0700747
748 /*
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700749 * Initialize the ring segment pool. The ring must be a contiguous
750 * structure comprised of TRBs. The TRBs must be 16 byte aligned,
751 * however, the command ring segment needs 64-byte aligned segments,
752 * so we pick the greater alignment need.
753 */
754 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
755 SEGMENT_SIZE, 64, xhci->page_size);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700756 /* See Table 46 and Note on Figure 55 */
757 /* FIXME support 64-byte contexts */
758 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
759 sizeof(struct xhci_device_control),
760 64, xhci->page_size);
761 if (!xhci->segment_pool || !xhci->device_pool)
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700762 goto fail;
763
764 /* Set up the command ring to have one segments for now. */
765 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
766 if (!xhci->cmd_ring)
767 goto fail;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700768 xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
769 xhci_dbg(xhci, "First segment DMA is 0x%llx\n",
770 (unsigned long long)xhci->cmd_ring->first_seg->dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700771
772 /* Set the address in the Command Ring Control register */
Sarah Sharp8e595a52009-07-27 12:03:31 -0700773 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
774 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
775 (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700776 xhci->cmd_ring->cycle_state;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700777 xhci_dbg(xhci, "// Setting command ring address to 0x%x\n", val);
778 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700779 xhci_dbg_cmd_ptrs(xhci);
780
781 val = xhci_readl(xhci, &xhci->cap_regs->db_off);
782 val &= DBOFF_MASK;
783 xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
784 " from cap regs base addr\n", val);
785 xhci->dba = (void *) xhci->cap_regs + val;
786 xhci_dbg_regs(xhci);
787 xhci_print_run_regs(xhci);
788 /* Set ir_set to interrupt register set 0 */
789 xhci->ir_set = (void *) xhci->run_regs->ir_set;
790
791 /*
792 * Event ring setup: Allocate a normal ring, but also setup
793 * the event ring segment table (ERST). Section 4.9.3.
794 */
795 xhci_dbg(xhci, "// Allocating event ring\n");
796 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
797 if (!xhci->event_ring)
798 goto fail;
799
800 xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
801 sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
802 if (!xhci->erst.entries)
803 goto fail;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700804 xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
805 (unsigned long long)dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700806
807 memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
808 xhci->erst.num_entries = ERST_NUM_SEGS;
809 xhci->erst.erst_dma_addr = dma;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700810 xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = %p, dma addr = 0x%llx\n",
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700811 xhci->erst.num_entries,
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700812 xhci->erst.entries,
813 (unsigned long long)xhci->erst.erst_dma_addr);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700814
815 /* set ring base address and size for each segment table entry */
816 for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
817 struct xhci_erst_entry *entry = &xhci->erst.entries[val];
Sarah Sharp8e595a52009-07-27 12:03:31 -0700818 entry->seg_addr = seg->dma;
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700819 entry->seg_size = TRBS_PER_SEGMENT;
820 entry->rsvd = 0;
821 seg = seg->next;
822 }
823
824 /* set ERST count with the number of entries in the segment table */
825 val = xhci_readl(xhci, &xhci->ir_set->erst_size);
826 val &= ERST_SIZE_MASK;
827 val |= ERST_NUM_SEGS;
828 xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
829 val);
830 xhci_writel(xhci, val, &xhci->ir_set->erst_size);
831
832 xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
833 /* set the segment table base address */
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700834 xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%llx\n",
835 (unsigned long long)xhci->erst.erst_dma_addr);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700836 val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
837 val_64 &= ERST_PTR_MASK;
838 val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
839 xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700840
841 /* Set the event ring dequeue address */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700842 xhci_set_hc_event_deq(xhci);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700843 xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
844 xhci_print_ir_set(xhci, xhci->ir_set, 0);
845
846 /*
847 * XXX: Might need to set the Interrupter Moderation Register to
848 * something other than the default (~1ms minimum between interrupts).
849 * See section 5.5.1.2.
850 */
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700851 init_completion(&xhci->addr_dev);
852 for (i = 0; i < MAX_HC_SLOTS; ++i)
853 xhci->devs[i] = 0;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700854
John Youn254c80a2009-07-27 12:05:03 -0700855 if (scratchpad_alloc(xhci, flags))
856 goto fail;
857
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700858 return 0;
John Youn254c80a2009-07-27 12:05:03 -0700859
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700860fail:
861 xhci_warn(xhci, "Couldn't initialize memory\n");
862 xhci_mem_cleanup(xhci);
863 return -ENOMEM;
864}