blob: 1f1f8a0f2e66d3a8aa6f167384361f92f354b9a9 [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);
Sarah Sharpb0567b32009-08-07 14:04:36 -070097 /* Always set the chain bit with 0.95 hardware */
98 if (xhci_link_trb_quirk(xhci))
99 val |= TRB_CHAIN;
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700100 prev->trbs[TRBS_PER_SEGMENT-1].link.control = val;
101 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700102 xhci_dbg(xhci, "Linking segment 0x%llx to segment 0x%llx (DMA)\n",
103 (unsigned long long)prev->dma,
104 (unsigned long long)next->dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700105}
106
107/* XXX: Do we need the hcd structure in all these functions? */
Sarah Sharpf94e01862009-04-27 19:58:38 -0700108void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700109{
110 struct xhci_segment *seg;
111 struct xhci_segment *first_seg;
112
113 if (!ring || !ring->first_seg)
114 return;
115 first_seg = ring->first_seg;
116 seg = first_seg->next;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700117 xhci_dbg(xhci, "Freeing ring at %p\n", ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700118 while (seg != first_seg) {
119 struct xhci_segment *next = seg->next;
120 xhci_segment_free(xhci, seg);
121 seg = next;
122 }
123 xhci_segment_free(xhci, first_seg);
124 ring->first_seg = NULL;
125 kfree(ring);
126}
127
Sarah Sharp74f9fe22009-12-03 09:44:29 -0800128static void xhci_initialize_ring_info(struct xhci_ring *ring)
129{
130 /* The ring is empty, so the enqueue pointer == dequeue pointer */
131 ring->enqueue = ring->first_seg->trbs;
132 ring->enq_seg = ring->first_seg;
133 ring->dequeue = ring->enqueue;
134 ring->deq_seg = ring->first_seg;
135 /* The ring is initialized to 0. The producer must write 1 to the cycle
136 * bit to handover ownership of the TRB, so PCS = 1. The consumer must
137 * compare CCS to the cycle bit to check ownership, so CCS = 1.
138 */
139 ring->cycle_state = 1;
140 /* Not necessary for new rings, but needed for re-initialized rings */
141 ring->enq_updates = 0;
142 ring->deq_updates = 0;
143}
144
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700145/**
146 * Create a new ring with zero or more segments.
147 *
148 * Link each segment together into a ring.
149 * Set the end flag and the cycle toggle bit on the last segment.
150 * See section 4.9.1 and figures 15 and 16.
151 */
152static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
153 unsigned int num_segs, bool link_trbs, gfp_t flags)
154{
155 struct xhci_ring *ring;
156 struct xhci_segment *prev;
157
158 ring = kzalloc(sizeof *(ring), flags);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700159 xhci_dbg(xhci, "Allocating ring at %p\n", ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700160 if (!ring)
161 return 0;
162
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700163 INIT_LIST_HEAD(&ring->td_list);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700164 if (num_segs == 0)
165 return ring;
166
167 ring->first_seg = xhci_segment_alloc(xhci, flags);
168 if (!ring->first_seg)
169 goto fail;
170 num_segs--;
171
172 prev = ring->first_seg;
173 while (num_segs > 0) {
174 struct xhci_segment *next;
175
176 next = xhci_segment_alloc(xhci, flags);
177 if (!next)
178 goto fail;
179 xhci_link_segments(xhci, prev, next, link_trbs);
180
181 prev = next;
182 num_segs--;
183 }
184 xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
185
186 if (link_trbs) {
187 /* See section 4.9.2.1 and 6.4.4.1 */
188 prev->trbs[TRBS_PER_SEGMENT-1].link.control |= (LINK_TOGGLE);
189 xhci_dbg(xhci, "Wrote link toggle flag to"
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700190 " segment %p (virtual), 0x%llx (DMA)\n",
191 prev, (unsigned long long)prev->dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700192 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -0800193 xhci_initialize_ring_info(ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700194 return ring;
195
196fail:
197 xhci_ring_free(xhci, ring);
198 return 0;
199}
200
Sarah Sharp412566b2009-12-09 15:59:01 -0800201void xhci_free_or_cache_endpoint_ring(struct xhci_hcd *xhci,
202 struct xhci_virt_device *virt_dev,
203 unsigned int ep_index)
204{
205 int rings_cached;
206
207 rings_cached = virt_dev->num_rings_cached;
208 if (rings_cached < XHCI_MAX_RINGS_CACHED) {
209 virt_dev->num_rings_cached++;
210 rings_cached = virt_dev->num_rings_cached;
211 virt_dev->ring_cache[rings_cached] =
212 virt_dev->eps[ep_index].ring;
213 xhci_dbg(xhci, "Cached old ring, "
214 "%d ring%s cached\n",
215 rings_cached,
216 (rings_cached > 1) ? "s" : "");
217 } else {
218 xhci_ring_free(xhci, virt_dev->eps[ep_index].ring);
219 xhci_dbg(xhci, "Ring cache full (%d rings), "
220 "freeing ring\n",
221 virt_dev->num_rings_cached);
222 }
223 virt_dev->eps[ep_index].ring = NULL;
224}
225
Sarah Sharp74f9fe22009-12-03 09:44:29 -0800226/* Zero an endpoint ring (except for link TRBs) and move the enqueue and dequeue
227 * pointers to the beginning of the ring.
228 */
229static void xhci_reinit_cached_ring(struct xhci_hcd *xhci,
230 struct xhci_ring *ring)
231{
232 struct xhci_segment *seg = ring->first_seg;
233 do {
234 memset(seg->trbs, 0,
235 sizeof(union xhci_trb)*TRBS_PER_SEGMENT);
236 /* All endpoint rings have link TRBs */
237 xhci_link_segments(xhci, seg, seg->next, 1);
238 seg = seg->next;
239 } while (seg != ring->first_seg);
240 xhci_initialize_ring_info(ring);
241 /* td list should be empty since all URBs have been cancelled,
242 * but just in case...
243 */
244 INIT_LIST_HEAD(&ring->td_list);
245}
246
John Yound115b042009-07-27 12:05:15 -0700247#define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32)
248
249struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
250 int type, gfp_t flags)
251{
252 struct xhci_container_ctx *ctx = kzalloc(sizeof(*ctx), flags);
253 if (!ctx)
254 return NULL;
255
256 BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
257 ctx->type = type;
258 ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024;
259 if (type == XHCI_CTX_TYPE_INPUT)
260 ctx->size += CTX_SIZE(xhci->hcc_params);
261
262 ctx->bytes = dma_pool_alloc(xhci->device_pool, flags, &ctx->dma);
263 memset(ctx->bytes, 0, ctx->size);
264 return ctx;
265}
266
267void xhci_free_container_ctx(struct xhci_hcd *xhci,
268 struct xhci_container_ctx *ctx)
269{
270 dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma);
271 kfree(ctx);
272}
273
274struct xhci_input_control_ctx *xhci_get_input_control_ctx(struct xhci_hcd *xhci,
275 struct xhci_container_ctx *ctx)
276{
277 BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
278 return (struct xhci_input_control_ctx *)ctx->bytes;
279}
280
281struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci,
282 struct xhci_container_ctx *ctx)
283{
284 if (ctx->type == XHCI_CTX_TYPE_DEVICE)
285 return (struct xhci_slot_ctx *)ctx->bytes;
286
287 return (struct xhci_slot_ctx *)
288 (ctx->bytes + CTX_SIZE(xhci->hcc_params));
289}
290
291struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
292 struct xhci_container_ctx *ctx,
293 unsigned int ep_index)
294{
295 /* increment ep index by offset of start of ep ctx array */
296 ep_index++;
297 if (ctx->type == XHCI_CTX_TYPE_INPUT)
298 ep_index++;
299
300 return (struct xhci_ep_ctx *)
301 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
302}
303
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700304static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
305 struct xhci_virt_ep *ep)
306{
307 init_timer(&ep->stop_cmd_timer);
308 ep->stop_cmd_timer.data = (unsigned long) ep;
309 ep->stop_cmd_timer.function = xhci_stop_endpoint_command_watchdog;
310 ep->xhci = xhci;
311}
312
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700313/* All the xhci_tds in the ring's TD list should be freed at this point */
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700314void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
315{
316 struct xhci_virt_device *dev;
317 int i;
318
319 /* Slot ID 0 is reserved */
320 if (slot_id == 0 || !xhci->devs[slot_id])
321 return;
322
323 dev = xhci->devs[slot_id];
Sarah Sharp8e595a52009-07-27 12:03:31 -0700324 xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700325 if (!dev)
326 return;
327
328 for (i = 0; i < 31; ++i)
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700329 if (dev->eps[i].ring)
330 xhci_ring_free(xhci, dev->eps[i].ring);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700331
Sarah Sharp74f9fe22009-12-03 09:44:29 -0800332 if (dev->ring_cache) {
333 for (i = 0; i < dev->num_rings_cached; i++)
334 xhci_ring_free(xhci, dev->ring_cache[i]);
335 kfree(dev->ring_cache);
336 }
337
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700338 if (dev->in_ctx)
John Yound115b042009-07-27 12:05:15 -0700339 xhci_free_container_ctx(xhci, dev->in_ctx);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700340 if (dev->out_ctx)
John Yound115b042009-07-27 12:05:15 -0700341 xhci_free_container_ctx(xhci, dev->out_ctx);
342
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700343 kfree(xhci->devs[slot_id]);
344 xhci->devs[slot_id] = 0;
345}
346
347int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
348 struct usb_device *udev, gfp_t flags)
349{
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700350 struct xhci_virt_device *dev;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700351 int i;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700352
353 /* Slot ID 0 is reserved */
354 if (slot_id == 0 || xhci->devs[slot_id]) {
355 xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
356 return 0;
357 }
358
359 xhci->devs[slot_id] = kzalloc(sizeof(*xhci->devs[slot_id]), flags);
360 if (!xhci->devs[slot_id])
361 return 0;
362 dev = xhci->devs[slot_id];
363
John Yound115b042009-07-27 12:05:15 -0700364 /* Allocate the (output) device context that will be used in the HC. */
365 dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700366 if (!dev->out_ctx)
367 goto fail;
John Yound115b042009-07-27 12:05:15 -0700368
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700369 xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
John Yound115b042009-07-27 12:05:15 -0700370 (unsigned long long)dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700371
372 /* Allocate the (input) device context for address device command */
John Yound115b042009-07-27 12:05:15 -0700373 dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700374 if (!dev->in_ctx)
375 goto fail;
John Yound115b042009-07-27 12:05:15 -0700376
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700377 xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
John Yound115b042009-07-27 12:05:15 -0700378 (unsigned long long)dev->in_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700379
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700380 /* Initialize the cancellation list and watchdog timers for each ep */
381 for (i = 0; i < 31; i++) {
382 xhci_init_endpoint_timer(xhci, &dev->eps[i]);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700383 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700384 }
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700385
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700386 /* Allocate endpoint 0 ring */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700387 dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags);
388 if (!dev->eps[0].ring)
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700389 goto fail;
390
Sarah Sharp74f9fe22009-12-03 09:44:29 -0800391 /* Allocate pointers to the ring cache */
392 dev->ring_cache = kzalloc(
393 sizeof(struct xhci_ring *)*XHCI_MAX_RINGS_CACHED,
394 flags);
395 if (!dev->ring_cache)
396 goto fail;
397 dev->num_rings_cached = 0;
398
Sarah Sharpf94e01862009-04-27 19:58:38 -0700399 init_completion(&dev->cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -0700400 INIT_LIST_HEAD(&dev->cmd_list);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700401
Sarah Sharp28c2d2e2009-07-27 12:05:08 -0700402 /* Point to output device context in dcbaa. */
John Yound115b042009-07-27 12:05:15 -0700403 xhci->dcbaa->dev_context_ptrs[slot_id] = dev->out_ctx->dma;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700404 xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700405 slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -0700406 &xhci->dcbaa->dev_context_ptrs[slot_id],
Sarah Sharp28c2d2e2009-07-27 12:05:08 -0700407 (unsigned long long) xhci->dcbaa->dev_context_ptrs[slot_id]);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700408
409 return 1;
410fail:
411 xhci_free_virt_device(xhci, slot_id);
412 return 0;
413}
414
415/* Setup an xHCI virtual device for a Set Address command */
416int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
417{
418 struct xhci_virt_device *dev;
419 struct xhci_ep_ctx *ep0_ctx;
420 struct usb_device *top_dev;
John Yound115b042009-07-27 12:05:15 -0700421 struct xhci_slot_ctx *slot_ctx;
422 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700423
424 dev = xhci->devs[udev->slot_id];
425 /* Slot ID 0 is reserved */
426 if (udev->slot_id == 0 || !dev) {
427 xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
428 udev->slot_id);
429 return -EINVAL;
430 }
John Yound115b042009-07-27 12:05:15 -0700431 ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
432 ctrl_ctx = xhci_get_input_control_ctx(xhci, dev->in_ctx);
433 slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700434
435 /* 2) New slot context and endpoint 0 context are valid*/
John Yound115b042009-07-27 12:05:15 -0700436 ctrl_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700437
438 /* 3) Only the control endpoint is valid - one endpoint context */
John Yound115b042009-07-27 12:05:15 -0700439 slot_ctx->dev_info |= LAST_CTX(1);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700440
Sarah Sharp4a0cd962009-09-04 10:53:17 -0700441 slot_ctx->dev_info |= (u32) udev->route;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700442 switch (udev->speed) {
443 case USB_SPEED_SUPER:
John Yound115b042009-07-27 12:05:15 -0700444 slot_ctx->dev_info |= (u32) SLOT_SPEED_SS;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700445 break;
446 case USB_SPEED_HIGH:
John Yound115b042009-07-27 12:05:15 -0700447 slot_ctx->dev_info |= (u32) SLOT_SPEED_HS;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700448 break;
449 case USB_SPEED_FULL:
John Yound115b042009-07-27 12:05:15 -0700450 slot_ctx->dev_info |= (u32) SLOT_SPEED_FS;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700451 break;
452 case USB_SPEED_LOW:
John Yound115b042009-07-27 12:05:15 -0700453 slot_ctx->dev_info |= (u32) SLOT_SPEED_LS;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700454 break;
455 case USB_SPEED_VARIABLE:
456 xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
457 return -EINVAL;
458 break;
459 default:
460 /* Speed was set earlier, this shouldn't happen. */
461 BUG();
462 }
463 /* Find the root hub port this device is under */
464 for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
465 top_dev = top_dev->parent)
466 /* Found device below root hub */;
John Yound115b042009-07-27 12:05:15 -0700467 slot_ctx->dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700468 xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
469
470 /* Is this a LS/FS device under a HS hub? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700471 if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
472 udev->tt) {
John Yound115b042009-07-27 12:05:15 -0700473 slot_ctx->tt_info = udev->tt->hub->slot_id;
474 slot_ctx->tt_info |= udev->ttport << 8;
Sarah Sharp07b6de12009-09-04 10:53:19 -0700475 if (udev->tt->multi)
476 slot_ctx->dev_info |= DEV_MTT;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700477 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700478 xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700479 xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
480
481 /* Step 4 - ring already allocated */
482 /* Step 5 */
483 ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
484 /*
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700485 * XXX: Not sure about wireless USB devices.
486 */
Sarah Sharp47aded82009-08-07 14:04:46 -0700487 switch (udev->speed) {
488 case USB_SPEED_SUPER:
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700489 ep0_ctx->ep_info2 |= MAX_PACKET(512);
Sarah Sharp47aded82009-08-07 14:04:46 -0700490 break;
491 case USB_SPEED_HIGH:
492 /* USB core guesses at a 64-byte max packet first for FS devices */
493 case USB_SPEED_FULL:
494 ep0_ctx->ep_info2 |= MAX_PACKET(64);
495 break;
496 case USB_SPEED_LOW:
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700497 ep0_ctx->ep_info2 |= MAX_PACKET(8);
Sarah Sharp47aded82009-08-07 14:04:46 -0700498 break;
499 case USB_SPEED_VARIABLE:
500 xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
501 return -EINVAL;
502 break;
503 default:
504 /* New speed? */
505 BUG();
506 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700507 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
508 ep0_ctx->ep_info2 |= MAX_BURST(0);
509 ep0_ctx->ep_info2 |= ERROR_COUNT(3);
510
Sarah Sharp8e595a52009-07-27 12:03:31 -0700511 ep0_ctx->deq =
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700512 dev->eps[0].ring->first_seg->dma;
513 ep0_ctx->deq |= dev->eps[0].ring->cycle_state;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700514
515 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
516
517 return 0;
518}
519
Sarah Sharpf94e01862009-04-27 19:58:38 -0700520/* Return the polling or NAK interval.
521 *
522 * The polling interval is expressed in "microframes". If xHCI's Interval field
523 * is set to N, it will service the endpoint every 2^(Interval)*125us.
524 *
525 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
526 * is set to 0.
527 */
528static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
529 struct usb_host_endpoint *ep)
530{
531 unsigned int interval = 0;
532
533 switch (udev->speed) {
534 case USB_SPEED_HIGH:
535 /* Max NAK rate */
536 if (usb_endpoint_xfer_control(&ep->desc) ||
537 usb_endpoint_xfer_bulk(&ep->desc))
538 interval = ep->desc.bInterval;
539 /* Fall through - SS and HS isoc/int have same decoding */
540 case USB_SPEED_SUPER:
541 if (usb_endpoint_xfer_int(&ep->desc) ||
542 usb_endpoint_xfer_isoc(&ep->desc)) {
543 if (ep->desc.bInterval == 0)
544 interval = 0;
545 else
546 interval = ep->desc.bInterval - 1;
547 if (interval > 15)
548 interval = 15;
549 if (interval != ep->desc.bInterval + 1)
550 dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
551 ep->desc.bEndpointAddress, 1 << interval);
552 }
553 break;
554 /* Convert bInterval (in 1-255 frames) to microframes and round down to
555 * nearest power of 2.
556 */
557 case USB_SPEED_FULL:
558 case USB_SPEED_LOW:
559 if (usb_endpoint_xfer_int(&ep->desc) ||
560 usb_endpoint_xfer_isoc(&ep->desc)) {
561 interval = fls(8*ep->desc.bInterval) - 1;
562 if (interval > 10)
563 interval = 10;
564 if (interval < 3)
565 interval = 3;
566 if ((1 << interval) != 8*ep->desc.bInterval)
567 dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
568 ep->desc.bEndpointAddress, 1 << interval);
569 }
570 break;
571 default:
572 BUG();
573 }
574 return EP_INTERVAL(interval);
575}
576
577static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
578 struct usb_host_endpoint *ep)
579{
580 int in;
581 u32 type;
582
583 in = usb_endpoint_dir_in(&ep->desc);
584 if (usb_endpoint_xfer_control(&ep->desc)) {
585 type = EP_TYPE(CTRL_EP);
586 } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
587 if (in)
588 type = EP_TYPE(BULK_IN_EP);
589 else
590 type = EP_TYPE(BULK_OUT_EP);
591 } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
592 if (in)
593 type = EP_TYPE(ISOC_IN_EP);
594 else
595 type = EP_TYPE(ISOC_OUT_EP);
596 } else if (usb_endpoint_xfer_int(&ep->desc)) {
597 if (in)
598 type = EP_TYPE(INT_IN_EP);
599 else
600 type = EP_TYPE(INT_OUT_EP);
601 } else {
602 BUG();
603 }
604 return type;
605}
606
607int xhci_endpoint_init(struct xhci_hcd *xhci,
608 struct xhci_virt_device *virt_dev,
609 struct usb_device *udev,
Sarah Sharpf88ba782009-05-14 11:44:22 -0700610 struct usb_host_endpoint *ep,
611 gfp_t mem_flags)
Sarah Sharpf94e01862009-04-27 19:58:38 -0700612{
613 unsigned int ep_index;
614 struct xhci_ep_ctx *ep_ctx;
615 struct xhci_ring *ep_ring;
616 unsigned int max_packet;
617 unsigned int max_burst;
618
619 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -0700620 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700621
622 /* Set up the endpoint ring */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700623 virt_dev->eps[ep_index].new_ring =
624 xhci_ring_alloc(xhci, 1, true, mem_flags);
Sarah Sharp74f9fe22009-12-03 09:44:29 -0800625 if (!virt_dev->eps[ep_index].new_ring) {
626 /* Attempt to use the ring cache */
627 if (virt_dev->num_rings_cached == 0)
628 return -ENOMEM;
629 virt_dev->eps[ep_index].new_ring =
630 virt_dev->ring_cache[virt_dev->num_rings_cached];
631 virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
632 virt_dev->num_rings_cached--;
633 xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring);
634 }
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700635 ep_ring = virt_dev->eps[ep_index].new_ring;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700636 ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700637
638 ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
639
640 /* FIXME dig Mult and streams info out of ep companion desc */
641
Sarah Sharp47692d12009-07-27 12:04:27 -0700642 /* Allow 3 retries for everything but isoc;
643 * error count = 0 means infinite retries.
644 */
Sarah Sharpf94e01862009-04-27 19:58:38 -0700645 if (!usb_endpoint_xfer_isoc(&ep->desc))
646 ep_ctx->ep_info2 = ERROR_COUNT(3);
647 else
Sarah Sharp47692d12009-07-27 12:04:27 -0700648 ep_ctx->ep_info2 = ERROR_COUNT(1);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700649
650 ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
651
652 /* Set the max packet size and max burst */
653 switch (udev->speed) {
654 case USB_SPEED_SUPER:
655 max_packet = ep->desc.wMaxPacketSize;
656 ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
Sarah Sharpb10de142009-04-27 19:58:50 -0700657 /* dig out max burst from ep companion desc */
Sarah Sharpb7d6d992009-07-27 12:04:38 -0700658 if (!ep->ss_ep_comp) {
659 xhci_warn(xhci, "WARN no SS endpoint companion descriptor.\n");
660 max_packet = 0;
661 } else {
662 max_packet = ep->ss_ep_comp->desc.bMaxBurst;
663 }
Sarah Sharpb10de142009-04-27 19:58:50 -0700664 ep_ctx->ep_info2 |= MAX_BURST(max_packet);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700665 break;
666 case USB_SPEED_HIGH:
667 /* bits 11:12 specify the number of additional transaction
668 * opportunities per microframe (USB 2.0, section 9.6.6)
669 */
670 if (usb_endpoint_xfer_isoc(&ep->desc) ||
671 usb_endpoint_xfer_int(&ep->desc)) {
672 max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
673 ep_ctx->ep_info2 |= MAX_BURST(max_burst);
674 }
675 /* Fall through */
676 case USB_SPEED_FULL:
677 case USB_SPEED_LOW:
678 max_packet = ep->desc.wMaxPacketSize & 0x3ff;
679 ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
680 break;
681 default:
682 BUG();
683 }
684 /* FIXME Debug endpoint context */
685 return 0;
686}
687
688void xhci_endpoint_zero(struct xhci_hcd *xhci,
689 struct xhci_virt_device *virt_dev,
690 struct usb_host_endpoint *ep)
691{
692 unsigned int ep_index;
693 struct xhci_ep_ctx *ep_ctx;
694
695 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -0700696 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700697
698 ep_ctx->ep_info = 0;
699 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700700 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700701 ep_ctx->tx_info = 0;
702 /* Don't free the endpoint ring until the set interface or configuration
703 * request succeeds.
704 */
705}
706
Sarah Sharpf2217e82009-08-07 14:04:43 -0700707/* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
708 * Useful when you want to change one particular aspect of the endpoint and then
709 * issue a configure endpoint command.
710 */
711void xhci_endpoint_copy(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -0700712 struct xhci_container_ctx *in_ctx,
713 struct xhci_container_ctx *out_ctx,
714 unsigned int ep_index)
Sarah Sharpf2217e82009-08-07 14:04:43 -0700715{
716 struct xhci_ep_ctx *out_ep_ctx;
717 struct xhci_ep_ctx *in_ep_ctx;
718
Sarah Sharp913a8a32009-09-04 10:53:13 -0700719 out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
720 in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
Sarah Sharpf2217e82009-08-07 14:04:43 -0700721
722 in_ep_ctx->ep_info = out_ep_ctx->ep_info;
723 in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
724 in_ep_ctx->deq = out_ep_ctx->deq;
725 in_ep_ctx->tx_info = out_ep_ctx->tx_info;
726}
727
728/* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
729 * Useful when you want to change one particular aspect of the endpoint and then
730 * issue a configure endpoint command. Only the context entries field matters,
731 * but we'll copy the whole thing anyway.
732 */
Sarah Sharp913a8a32009-09-04 10:53:13 -0700733void xhci_slot_copy(struct xhci_hcd *xhci,
734 struct xhci_container_ctx *in_ctx,
735 struct xhci_container_ctx *out_ctx)
Sarah Sharpf2217e82009-08-07 14:04:43 -0700736{
737 struct xhci_slot_ctx *in_slot_ctx;
738 struct xhci_slot_ctx *out_slot_ctx;
739
Sarah Sharp913a8a32009-09-04 10:53:13 -0700740 in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
741 out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -0700742
743 in_slot_ctx->dev_info = out_slot_ctx->dev_info;
744 in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
745 in_slot_ctx->tt_info = out_slot_ctx->tt_info;
746 in_slot_ctx->dev_state = out_slot_ctx->dev_state;
747}
748
John Youn254c80a2009-07-27 12:05:03 -0700749/* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
750static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
751{
752 int i;
753 struct device *dev = xhci_to_hcd(xhci)->self.controller;
754 int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
755
756 xhci_dbg(xhci, "Allocating %d scratchpad buffers\n", num_sp);
757
758 if (!num_sp)
759 return 0;
760
761 xhci->scratchpad = kzalloc(sizeof(*xhci->scratchpad), flags);
762 if (!xhci->scratchpad)
763 goto fail_sp;
764
765 xhci->scratchpad->sp_array =
766 pci_alloc_consistent(to_pci_dev(dev),
767 num_sp * sizeof(u64),
768 &xhci->scratchpad->sp_dma);
769 if (!xhci->scratchpad->sp_array)
770 goto fail_sp2;
771
772 xhci->scratchpad->sp_buffers = kzalloc(sizeof(void *) * num_sp, flags);
773 if (!xhci->scratchpad->sp_buffers)
774 goto fail_sp3;
775
776 xhci->scratchpad->sp_dma_buffers =
777 kzalloc(sizeof(dma_addr_t) * num_sp, flags);
778
779 if (!xhci->scratchpad->sp_dma_buffers)
780 goto fail_sp4;
781
782 xhci->dcbaa->dev_context_ptrs[0] = xhci->scratchpad->sp_dma;
783 for (i = 0; i < num_sp; i++) {
784 dma_addr_t dma;
785 void *buf = pci_alloc_consistent(to_pci_dev(dev),
786 xhci->page_size, &dma);
787 if (!buf)
788 goto fail_sp5;
789
790 xhci->scratchpad->sp_array[i] = dma;
791 xhci->scratchpad->sp_buffers[i] = buf;
792 xhci->scratchpad->sp_dma_buffers[i] = dma;
793 }
794
795 return 0;
796
797 fail_sp5:
798 for (i = i - 1; i >= 0; i--) {
799 pci_free_consistent(to_pci_dev(dev), xhci->page_size,
800 xhci->scratchpad->sp_buffers[i],
801 xhci->scratchpad->sp_dma_buffers[i]);
802 }
803 kfree(xhci->scratchpad->sp_dma_buffers);
804
805 fail_sp4:
806 kfree(xhci->scratchpad->sp_buffers);
807
808 fail_sp3:
809 pci_free_consistent(to_pci_dev(dev), num_sp * sizeof(u64),
810 xhci->scratchpad->sp_array,
811 xhci->scratchpad->sp_dma);
812
813 fail_sp2:
814 kfree(xhci->scratchpad);
815 xhci->scratchpad = NULL;
816
817 fail_sp:
818 return -ENOMEM;
819}
820
821static void scratchpad_free(struct xhci_hcd *xhci)
822{
823 int num_sp;
824 int i;
825 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
826
827 if (!xhci->scratchpad)
828 return;
829
830 num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
831
832 for (i = 0; i < num_sp; i++) {
833 pci_free_consistent(pdev, xhci->page_size,
834 xhci->scratchpad->sp_buffers[i],
835 xhci->scratchpad->sp_dma_buffers[i]);
836 }
837 kfree(xhci->scratchpad->sp_dma_buffers);
838 kfree(xhci->scratchpad->sp_buffers);
839 pci_free_consistent(pdev, num_sp * sizeof(u64),
840 xhci->scratchpad->sp_array,
841 xhci->scratchpad->sp_dma);
842 kfree(xhci->scratchpad);
843 xhci->scratchpad = NULL;
844}
845
Sarah Sharp913a8a32009-09-04 10:53:13 -0700846struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
847 bool allocate_completion, gfp_t mem_flags)
848{
849 struct xhci_command *command;
850
851 command = kzalloc(sizeof(*command), mem_flags);
852 if (!command)
853 return NULL;
854
855 command->in_ctx =
856 xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, mem_flags);
Julia Lawall06e18292009-11-21 12:51:47 +0100857 if (!command->in_ctx) {
858 kfree(command);
Sarah Sharp913a8a32009-09-04 10:53:13 -0700859 return NULL;
Julia Lawall06e18292009-11-21 12:51:47 +0100860 }
Sarah Sharp913a8a32009-09-04 10:53:13 -0700861
862 if (allocate_completion) {
863 command->completion =
864 kzalloc(sizeof(struct completion), mem_flags);
865 if (!command->completion) {
866 xhci_free_container_ctx(xhci, command->in_ctx);
Julia Lawall06e18292009-11-21 12:51:47 +0100867 kfree(command);
Sarah Sharp913a8a32009-09-04 10:53:13 -0700868 return NULL;
869 }
870 init_completion(command->completion);
871 }
872
873 command->status = 0;
874 INIT_LIST_HEAD(&command->cmd_list);
875 return command;
876}
877
878void xhci_free_command(struct xhci_hcd *xhci,
879 struct xhci_command *command)
880{
881 xhci_free_container_ctx(xhci,
882 command->in_ctx);
883 kfree(command->completion);
884 kfree(command);
885}
886
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700887void xhci_mem_cleanup(struct xhci_hcd *xhci)
888{
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700889 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
890 int size;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700891 int i;
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700892
893 /* Free the Event Ring Segment Table and the actual Event Ring */
Sarah Sharpd94c05e2009-11-03 22:02:22 -0800894 if (xhci->ir_set) {
895 xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
896 xhci_write_64(xhci, 0, &xhci->ir_set->erst_base);
897 xhci_write_64(xhci, 0, &xhci->ir_set->erst_dequeue);
898 }
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700899 size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
900 if (xhci->erst.entries)
901 pci_free_consistent(pdev, size,
902 xhci->erst.entries, xhci->erst.erst_dma_addr);
903 xhci->erst.entries = NULL;
904 xhci_dbg(xhci, "Freed ERST\n");
905 if (xhci->event_ring)
906 xhci_ring_free(xhci, xhci->event_ring);
907 xhci->event_ring = NULL;
908 xhci_dbg(xhci, "Freed event ring\n");
909
Sarah Sharp8e595a52009-07-27 12:03:31 -0700910 xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700911 if (xhci->cmd_ring)
912 xhci_ring_free(xhci, xhci->cmd_ring);
913 xhci->cmd_ring = NULL;
914 xhci_dbg(xhci, "Freed command ring\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700915
916 for (i = 1; i < MAX_HC_SLOTS; ++i)
917 xhci_free_virt_device(xhci, i);
918
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700919 if (xhci->segment_pool)
920 dma_pool_destroy(xhci->segment_pool);
921 xhci->segment_pool = NULL;
922 xhci_dbg(xhci, "Freed segment pool\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700923
924 if (xhci->device_pool)
925 dma_pool_destroy(xhci->device_pool);
926 xhci->device_pool = NULL;
927 xhci_dbg(xhci, "Freed device context pool\n");
928
Sarah Sharp8e595a52009-07-27 12:03:31 -0700929 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
Sarah Sharpa74588f2009-04-27 19:53:42 -0700930 if (xhci->dcbaa)
931 pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
932 xhci->dcbaa, xhci->dcbaa->dma);
933 xhci->dcbaa = NULL;
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700934
Sarah Sharp5294bea2009-11-04 11:22:19 -0800935 scratchpad_free(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700936 xhci->page_size = 0;
937 xhci->page_shift = 0;
938}
939
Sarah Sharp6648f292009-11-09 13:35:23 -0800940static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
941 struct xhci_segment *input_seg,
942 union xhci_trb *start_trb,
943 union xhci_trb *end_trb,
944 dma_addr_t input_dma,
945 struct xhci_segment *result_seg,
946 char *test_name, int test_number)
947{
948 unsigned long long start_dma;
949 unsigned long long end_dma;
950 struct xhci_segment *seg;
951
952 start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
953 end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
954
955 seg = trb_in_td(input_seg, start_trb, end_trb, input_dma);
956 if (seg != result_seg) {
957 xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
958 test_name, test_number);
959 xhci_warn(xhci, "Tested TRB math w/ seg %p and "
960 "input DMA 0x%llx\n",
961 input_seg,
962 (unsigned long long) input_dma);
963 xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
964 "ending TRB %p (0x%llx DMA)\n",
965 start_trb, start_dma,
966 end_trb, end_dma);
967 xhci_warn(xhci, "Expected seg %p, got seg %p\n",
968 result_seg, seg);
969 return -1;
970 }
971 return 0;
972}
973
974/* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
975static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
976{
977 struct {
978 dma_addr_t input_dma;
979 struct xhci_segment *result_seg;
980 } simple_test_vector [] = {
981 /* A zeroed DMA field should fail */
982 { 0, NULL },
983 /* One TRB before the ring start should fail */
984 { xhci->event_ring->first_seg->dma - 16, NULL },
985 /* One byte before the ring start should fail */
986 { xhci->event_ring->first_seg->dma - 1, NULL },
987 /* Starting TRB should succeed */
988 { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
989 /* Ending TRB should succeed */
990 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
991 xhci->event_ring->first_seg },
992 /* One byte after the ring end should fail */
993 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
994 /* One TRB after the ring end should fail */
995 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
996 /* An address of all ones should fail */
997 { (dma_addr_t) (~0), NULL },
998 };
999 struct {
1000 struct xhci_segment *input_seg;
1001 union xhci_trb *start_trb;
1002 union xhci_trb *end_trb;
1003 dma_addr_t input_dma;
1004 struct xhci_segment *result_seg;
1005 } complex_test_vector [] = {
1006 /* Test feeding a valid DMA address from a different ring */
1007 { .input_seg = xhci->event_ring->first_seg,
1008 .start_trb = xhci->event_ring->first_seg->trbs,
1009 .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
1010 .input_dma = xhci->cmd_ring->first_seg->dma,
1011 .result_seg = NULL,
1012 },
1013 /* Test feeding a valid end TRB from a different ring */
1014 { .input_seg = xhci->event_ring->first_seg,
1015 .start_trb = xhci->event_ring->first_seg->trbs,
1016 .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
1017 .input_dma = xhci->cmd_ring->first_seg->dma,
1018 .result_seg = NULL,
1019 },
1020 /* Test feeding a valid start and end TRB from a different ring */
1021 { .input_seg = xhci->event_ring->first_seg,
1022 .start_trb = xhci->cmd_ring->first_seg->trbs,
1023 .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
1024 .input_dma = xhci->cmd_ring->first_seg->dma,
1025 .result_seg = NULL,
1026 },
1027 /* TRB in this ring, but after this TD */
1028 { .input_seg = xhci->event_ring->first_seg,
1029 .start_trb = &xhci->event_ring->first_seg->trbs[0],
1030 .end_trb = &xhci->event_ring->first_seg->trbs[3],
1031 .input_dma = xhci->event_ring->first_seg->dma + 4*16,
1032 .result_seg = NULL,
1033 },
1034 /* TRB in this ring, but before this TD */
1035 { .input_seg = xhci->event_ring->first_seg,
1036 .start_trb = &xhci->event_ring->first_seg->trbs[3],
1037 .end_trb = &xhci->event_ring->first_seg->trbs[6],
1038 .input_dma = xhci->event_ring->first_seg->dma + 2*16,
1039 .result_seg = NULL,
1040 },
1041 /* TRB in this ring, but after this wrapped TD */
1042 { .input_seg = xhci->event_ring->first_seg,
1043 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
1044 .end_trb = &xhci->event_ring->first_seg->trbs[1],
1045 .input_dma = xhci->event_ring->first_seg->dma + 2*16,
1046 .result_seg = NULL,
1047 },
1048 /* TRB in this ring, but before this wrapped TD */
1049 { .input_seg = xhci->event_ring->first_seg,
1050 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
1051 .end_trb = &xhci->event_ring->first_seg->trbs[1],
1052 .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
1053 .result_seg = NULL,
1054 },
1055 /* TRB not in this ring, and we have a wrapped TD */
1056 { .input_seg = xhci->event_ring->first_seg,
1057 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
1058 .end_trb = &xhci->event_ring->first_seg->trbs[1],
1059 .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
1060 .result_seg = NULL,
1061 },
1062 };
1063
1064 unsigned int num_tests;
1065 int i, ret;
1066
1067 num_tests = sizeof(simple_test_vector) / sizeof(simple_test_vector[0]);
1068 for (i = 0; i < num_tests; i++) {
1069 ret = xhci_test_trb_in_td(xhci,
1070 xhci->event_ring->first_seg,
1071 xhci->event_ring->first_seg->trbs,
1072 &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
1073 simple_test_vector[i].input_dma,
1074 simple_test_vector[i].result_seg,
1075 "Simple", i);
1076 if (ret < 0)
1077 return ret;
1078 }
1079
1080 num_tests = sizeof(complex_test_vector) / sizeof(complex_test_vector[0]);
1081 for (i = 0; i < num_tests; i++) {
1082 ret = xhci_test_trb_in_td(xhci,
1083 complex_test_vector[i].input_seg,
1084 complex_test_vector[i].start_trb,
1085 complex_test_vector[i].end_trb,
1086 complex_test_vector[i].input_dma,
1087 complex_test_vector[i].result_seg,
1088 "Complex", i);
1089 if (ret < 0)
1090 return ret;
1091 }
1092 xhci_dbg(xhci, "TRB math tests passed.\n");
1093 return 0;
1094}
1095
1096
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001097int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
1098{
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001099 dma_addr_t dma;
1100 struct device *dev = xhci_to_hcd(xhci)->self.controller;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001101 unsigned int val, val2;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001102 u64 val_64;
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001103 struct xhci_segment *seg;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001104 u32 page_size;
1105 int i;
1106
1107 page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
1108 xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
1109 for (i = 0; i < 16; i++) {
1110 if ((0x1 & page_size) != 0)
1111 break;
1112 page_size = page_size >> 1;
1113 }
1114 if (i < 16)
1115 xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
1116 else
1117 xhci_warn(xhci, "WARN: no supported page size\n");
1118 /* Use 4K pages, since that's common and the minimum the HC supports */
1119 xhci->page_shift = 12;
1120 xhci->page_size = 1 << xhci->page_shift;
1121 xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
1122
1123 /*
1124 * Program the Number of Device Slots Enabled field in the CONFIG
1125 * register with the max value of slots the HC can handle.
1126 */
1127 val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
1128 xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
1129 (unsigned int) val);
1130 val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
1131 val |= (val2 & ~HCS_SLOTS_MASK);
1132 xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
1133 (unsigned int) val);
1134 xhci_writel(xhci, val, &xhci->op_regs->config_reg);
1135
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001136 /*
Sarah Sharpa74588f2009-04-27 19:53:42 -07001137 * Section 5.4.8 - doorbell array must be
1138 * "physically contiguous and 64-byte (cache line) aligned".
1139 */
1140 xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
1141 sizeof(*xhci->dcbaa), &dma);
1142 if (!xhci->dcbaa)
1143 goto fail;
1144 memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
1145 xhci->dcbaa->dma = dma;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001146 xhci_dbg(xhci, "// Device context base array address = 0x%llx (DMA), %p (virt)\n",
1147 (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
Sarah Sharp8e595a52009-07-27 12:03:31 -07001148 xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
Sarah Sharpa74588f2009-04-27 19:53:42 -07001149
1150 /*
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001151 * Initialize the ring segment pool. The ring must be a contiguous
1152 * structure comprised of TRBs. The TRBs must be 16 byte aligned,
1153 * however, the command ring segment needs 64-byte aligned segments,
1154 * so we pick the greater alignment need.
1155 */
1156 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
1157 SEGMENT_SIZE, 64, xhci->page_size);
John Yound115b042009-07-27 12:05:15 -07001158
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001159 /* See Table 46 and Note on Figure 55 */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001160 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
John Yound115b042009-07-27 12:05:15 -07001161 2112, 64, xhci->page_size);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001162 if (!xhci->segment_pool || !xhci->device_pool)
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001163 goto fail;
1164
1165 /* Set up the command ring to have one segments for now. */
1166 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
1167 if (!xhci->cmd_ring)
1168 goto fail;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001169 xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
1170 xhci_dbg(xhci, "First segment DMA is 0x%llx\n",
1171 (unsigned long long)xhci->cmd_ring->first_seg->dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001172
1173 /* Set the address in the Command Ring Control register */
Sarah Sharp8e595a52009-07-27 12:03:31 -07001174 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
1175 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
1176 (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001177 xhci->cmd_ring->cycle_state;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001178 xhci_dbg(xhci, "// Setting command ring address to 0x%x\n", val);
1179 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001180 xhci_dbg_cmd_ptrs(xhci);
1181
1182 val = xhci_readl(xhci, &xhci->cap_regs->db_off);
1183 val &= DBOFF_MASK;
1184 xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
1185 " from cap regs base addr\n", val);
1186 xhci->dba = (void *) xhci->cap_regs + val;
1187 xhci_dbg_regs(xhci);
1188 xhci_print_run_regs(xhci);
1189 /* Set ir_set to interrupt register set 0 */
1190 xhci->ir_set = (void *) xhci->run_regs->ir_set;
1191
1192 /*
1193 * Event ring setup: Allocate a normal ring, but also setup
1194 * the event ring segment table (ERST). Section 4.9.3.
1195 */
1196 xhci_dbg(xhci, "// Allocating event ring\n");
1197 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
1198 if (!xhci->event_ring)
1199 goto fail;
Sarah Sharp6648f292009-11-09 13:35:23 -08001200 if (xhci_check_trb_in_td_math(xhci, flags) < 0)
1201 goto fail;
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001202
1203 xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
1204 sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
1205 if (!xhci->erst.entries)
1206 goto fail;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001207 xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
1208 (unsigned long long)dma);
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001209
1210 memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
1211 xhci->erst.num_entries = ERST_NUM_SEGS;
1212 xhci->erst.erst_dma_addr = dma;
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001213 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 -07001214 xhci->erst.num_entries,
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001215 xhci->erst.entries,
1216 (unsigned long long)xhci->erst.erst_dma_addr);
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001217
1218 /* set ring base address and size for each segment table entry */
1219 for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
1220 struct xhci_erst_entry *entry = &xhci->erst.entries[val];
Sarah Sharp8e595a52009-07-27 12:03:31 -07001221 entry->seg_addr = seg->dma;
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001222 entry->seg_size = TRBS_PER_SEGMENT;
1223 entry->rsvd = 0;
1224 seg = seg->next;
1225 }
1226
1227 /* set ERST count with the number of entries in the segment table */
1228 val = xhci_readl(xhci, &xhci->ir_set->erst_size);
1229 val &= ERST_SIZE_MASK;
1230 val |= ERST_NUM_SEGS;
1231 xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
1232 val);
1233 xhci_writel(xhci, val, &xhci->ir_set->erst_size);
1234
1235 xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
1236 /* set the segment table base address */
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001237 xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%llx\n",
1238 (unsigned long long)xhci->erst.erst_dma_addr);
Sarah Sharp8e595a52009-07-27 12:03:31 -07001239 val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
1240 val_64 &= ERST_PTR_MASK;
1241 val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
1242 xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001243
1244 /* Set the event ring dequeue address */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001245 xhci_set_hc_event_deq(xhci);
Sarah Sharp0ebbab32009-04-27 19:52:34 -07001246 xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
1247 xhci_print_ir_set(xhci, xhci->ir_set, 0);
1248
1249 /*
1250 * XXX: Might need to set the Interrupter Moderation Register to
1251 * something other than the default (~1ms minimum between interrupts).
1252 * See section 5.5.1.2.
1253 */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001254 init_completion(&xhci->addr_dev);
1255 for (i = 0; i < MAX_HC_SLOTS; ++i)
1256 xhci->devs[i] = 0;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001257
John Youn254c80a2009-07-27 12:05:03 -07001258 if (scratchpad_alloc(xhci, flags))
1259 goto fail;
1260
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001261 return 0;
John Youn254c80a2009-07-27 12:05:03 -07001262
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001263fail:
1264 xhci_warn(xhci, "Couldn't initialize memory\n");
1265 xhci_mem_cleanup(xhci);
1266 return -ENOMEM;
1267}