PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/usb/host/xhci-mem.c

https://gitlab.com/stalker-android/linux-omap3
C | 1988 lines | 1427 code | 214 blank | 347 comment | 234 complexity | 832d1d33d8bc42a9e289d3da886992a6 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  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. #include <linux/usb.h>
  23. #include <linux/pci.h>
  24. #include <linux/slab.h>
  25. #include <linux/dmapool.h>
  26. #include "xhci.h"
  27. /*
  28. * Allocates a generic ring segment from the ring pool, sets the dma address,
  29. * initializes the segment to zero, and sets the private next pointer to NULL.
  30. *
  31. * Section 4.11.1.1:
  32. * "All components of all Command and Transfer TRBs shall be initialized to '0'"
  33. */
  34. static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, gfp_t flags)
  35. {
  36. struct xhci_segment *seg;
  37. dma_addr_t dma;
  38. seg = kzalloc(sizeof *seg, flags);
  39. if (!seg)
  40. return NULL;
  41. xhci_dbg(xhci, "Allocating priv segment structure at %p\n", seg);
  42. seg->trbs = dma_pool_alloc(xhci->segment_pool, flags, &dma);
  43. if (!seg->trbs) {
  44. kfree(seg);
  45. return NULL;
  46. }
  47. xhci_dbg(xhci, "// Allocating segment at %p (virtual) 0x%llx (DMA)\n",
  48. seg->trbs, (unsigned long long)dma);
  49. memset(seg->trbs, 0, SEGMENT_SIZE);
  50. seg->dma = dma;
  51. seg->next = NULL;
  52. return seg;
  53. }
  54. static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
  55. {
  56. if (!seg)
  57. return;
  58. if (seg->trbs) {
  59. xhci_dbg(xhci, "Freeing DMA segment at %p (virtual) 0x%llx (DMA)\n",
  60. seg->trbs, (unsigned long long)seg->dma);
  61. dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
  62. seg->trbs = NULL;
  63. }
  64. xhci_dbg(xhci, "Freeing priv segment structure at %p\n", seg);
  65. kfree(seg);
  66. }
  67. /*
  68. * Make the prev segment point to the next segment.
  69. *
  70. * Change the last TRB in the prev segment to be a Link TRB which points to the
  71. * DMA address of the next segment. The caller needs to set any Link TRB
  72. * related flags, such as End TRB, Toggle Cycle, and no snoop.
  73. */
  74. static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
  75. struct xhci_segment *next, bool link_trbs)
  76. {
  77. u32 val;
  78. if (!prev || !next)
  79. return;
  80. prev->next = next;
  81. if (link_trbs) {
  82. prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = next->dma;
  83. /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
  84. val = prev->trbs[TRBS_PER_SEGMENT-1].link.control;
  85. val &= ~TRB_TYPE_BITMASK;
  86. val |= TRB_TYPE(TRB_LINK);
  87. /* Always set the chain bit with 0.95 hardware */
  88. if (xhci_link_trb_quirk(xhci))
  89. val |= TRB_CHAIN;
  90. prev->trbs[TRBS_PER_SEGMENT-1].link.control = val;
  91. }
  92. xhci_dbg(xhci, "Linking segment 0x%llx to segment 0x%llx (DMA)\n",
  93. (unsigned long long)prev->dma,
  94. (unsigned long long)next->dma);
  95. }
  96. /* XXX: Do we need the hcd structure in all these functions? */
  97. void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
  98. {
  99. struct xhci_segment *seg;
  100. struct xhci_segment *first_seg;
  101. if (!ring || !ring->first_seg)
  102. return;
  103. first_seg = ring->first_seg;
  104. seg = first_seg->next;
  105. xhci_dbg(xhci, "Freeing ring at %p\n", ring);
  106. while (seg != first_seg) {
  107. struct xhci_segment *next = seg->next;
  108. xhci_segment_free(xhci, seg);
  109. seg = next;
  110. }
  111. xhci_segment_free(xhci, first_seg);
  112. ring->first_seg = NULL;
  113. kfree(ring);
  114. }
  115. static void xhci_initialize_ring_info(struct xhci_ring *ring)
  116. {
  117. /* The ring is empty, so the enqueue pointer == dequeue pointer */
  118. ring->enqueue = ring->first_seg->trbs;
  119. ring->enq_seg = ring->first_seg;
  120. ring->dequeue = ring->enqueue;
  121. ring->deq_seg = ring->first_seg;
  122. /* The ring is initialized to 0. The producer must write 1 to the cycle
  123. * bit to handover ownership of the TRB, so PCS = 1. The consumer must
  124. * compare CCS to the cycle bit to check ownership, so CCS = 1.
  125. */
  126. ring->cycle_state = 1;
  127. /* Not necessary for new rings, but needed for re-initialized rings */
  128. ring->enq_updates = 0;
  129. ring->deq_updates = 0;
  130. }
  131. /**
  132. * Create a new ring with zero or more segments.
  133. *
  134. * Link each segment together into a ring.
  135. * Set the end flag and the cycle toggle bit on the last segment.
  136. * See section 4.9.1 and figures 15 and 16.
  137. */
  138. static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
  139. unsigned int num_segs, bool link_trbs, gfp_t flags)
  140. {
  141. struct xhci_ring *ring;
  142. struct xhci_segment *prev;
  143. ring = kzalloc(sizeof *(ring), flags);
  144. xhci_dbg(xhci, "Allocating ring at %p\n", ring);
  145. if (!ring)
  146. return NULL;
  147. INIT_LIST_HEAD(&ring->td_list);
  148. if (num_segs == 0)
  149. return ring;
  150. ring->first_seg = xhci_segment_alloc(xhci, flags);
  151. if (!ring->first_seg)
  152. goto fail;
  153. num_segs--;
  154. prev = ring->first_seg;
  155. while (num_segs > 0) {
  156. struct xhci_segment *next;
  157. next = xhci_segment_alloc(xhci, flags);
  158. if (!next)
  159. goto fail;
  160. xhci_link_segments(xhci, prev, next, link_trbs);
  161. prev = next;
  162. num_segs--;
  163. }
  164. xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
  165. if (link_trbs) {
  166. /* See section 4.9.2.1 and 6.4.4.1 */
  167. prev->trbs[TRBS_PER_SEGMENT-1].link.control |= (LINK_TOGGLE);
  168. xhci_dbg(xhci, "Wrote link toggle flag to"
  169. " segment %p (virtual), 0x%llx (DMA)\n",
  170. prev, (unsigned long long)prev->dma);
  171. }
  172. xhci_initialize_ring_info(ring);
  173. return ring;
  174. fail:
  175. xhci_ring_free(xhci, ring);
  176. return NULL;
  177. }
  178. void xhci_free_or_cache_endpoint_ring(struct xhci_hcd *xhci,
  179. struct xhci_virt_device *virt_dev,
  180. unsigned int ep_index)
  181. {
  182. int rings_cached;
  183. rings_cached = virt_dev->num_rings_cached;
  184. if (rings_cached < XHCI_MAX_RINGS_CACHED) {
  185. virt_dev->num_rings_cached++;
  186. rings_cached = virt_dev->num_rings_cached;
  187. virt_dev->ring_cache[rings_cached] =
  188. virt_dev->eps[ep_index].ring;
  189. xhci_dbg(xhci, "Cached old ring, "
  190. "%d ring%s cached\n",
  191. rings_cached,
  192. (rings_cached > 1) ? "s" : "");
  193. } else {
  194. xhci_ring_free(xhci, virt_dev->eps[ep_index].ring);
  195. xhci_dbg(xhci, "Ring cache full (%d rings), "
  196. "freeing ring\n",
  197. virt_dev->num_rings_cached);
  198. }
  199. virt_dev->eps[ep_index].ring = NULL;
  200. }
  201. /* Zero an endpoint ring (except for link TRBs) and move the enqueue and dequeue
  202. * pointers to the beginning of the ring.
  203. */
  204. static void xhci_reinit_cached_ring(struct xhci_hcd *xhci,
  205. struct xhci_ring *ring)
  206. {
  207. struct xhci_segment *seg = ring->first_seg;
  208. do {
  209. memset(seg->trbs, 0,
  210. sizeof(union xhci_trb)*TRBS_PER_SEGMENT);
  211. /* All endpoint rings have link TRBs */
  212. xhci_link_segments(xhci, seg, seg->next, 1);
  213. seg = seg->next;
  214. } while (seg != ring->first_seg);
  215. xhci_initialize_ring_info(ring);
  216. /* td list should be empty since all URBs have been cancelled,
  217. * but just in case...
  218. */
  219. INIT_LIST_HEAD(&ring->td_list);
  220. }
  221. #define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32)
  222. static struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
  223. int type, gfp_t flags)
  224. {
  225. struct xhci_container_ctx *ctx = kzalloc(sizeof(*ctx), flags);
  226. if (!ctx)
  227. return NULL;
  228. BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
  229. ctx->type = type;
  230. ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024;
  231. if (type == XHCI_CTX_TYPE_INPUT)
  232. ctx->size += CTX_SIZE(xhci->hcc_params);
  233. ctx->bytes = dma_pool_alloc(xhci->device_pool, flags, &ctx->dma);
  234. memset(ctx->bytes, 0, ctx->size);
  235. return ctx;
  236. }
  237. static void xhci_free_container_ctx(struct xhci_hcd *xhci,
  238. struct xhci_container_ctx *ctx)
  239. {
  240. if (!ctx)
  241. return;
  242. dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma);
  243. kfree(ctx);
  244. }
  245. struct xhci_input_control_ctx *xhci_get_input_control_ctx(struct xhci_hcd *xhci,
  246. struct xhci_container_ctx *ctx)
  247. {
  248. BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
  249. return (struct xhci_input_control_ctx *)ctx->bytes;
  250. }
  251. struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci,
  252. struct xhci_container_ctx *ctx)
  253. {
  254. if (ctx->type == XHCI_CTX_TYPE_DEVICE)
  255. return (struct xhci_slot_ctx *)ctx->bytes;
  256. return (struct xhci_slot_ctx *)
  257. (ctx->bytes + CTX_SIZE(xhci->hcc_params));
  258. }
  259. struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
  260. struct xhci_container_ctx *ctx,
  261. unsigned int ep_index)
  262. {
  263. /* increment ep index by offset of start of ep ctx array */
  264. ep_index++;
  265. if (ctx->type == XHCI_CTX_TYPE_INPUT)
  266. ep_index++;
  267. return (struct xhci_ep_ctx *)
  268. (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
  269. }
  270. /***************** Streams structures manipulation *************************/
  271. void xhci_free_stream_ctx(struct xhci_hcd *xhci,
  272. unsigned int num_stream_ctxs,
  273. struct xhci_stream_ctx *stream_ctx, dma_addr_t dma)
  274. {
  275. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  276. if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE)
  277. pci_free_consistent(pdev,
  278. sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
  279. stream_ctx, dma);
  280. else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE)
  281. return dma_pool_free(xhci->small_streams_pool,
  282. stream_ctx, dma);
  283. else
  284. return dma_pool_free(xhci->medium_streams_pool,
  285. stream_ctx, dma);
  286. }
  287. /*
  288. * The stream context array for each endpoint with bulk streams enabled can
  289. * vary in size, based on:
  290. * - how many streams the endpoint supports,
  291. * - the maximum primary stream array size the host controller supports,
  292. * - and how many streams the device driver asks for.
  293. *
  294. * The stream context array must be a power of 2, and can be as small as
  295. * 64 bytes or as large as 1MB.
  296. */
  297. struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
  298. unsigned int num_stream_ctxs, dma_addr_t *dma,
  299. gfp_t mem_flags)
  300. {
  301. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  302. if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE)
  303. return pci_alloc_consistent(pdev,
  304. sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
  305. dma);
  306. else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE)
  307. return dma_pool_alloc(xhci->small_streams_pool,
  308. mem_flags, dma);
  309. else
  310. return dma_pool_alloc(xhci->medium_streams_pool,
  311. mem_flags, dma);
  312. }
  313. struct xhci_ring *xhci_dma_to_transfer_ring(
  314. struct xhci_virt_ep *ep,
  315. u64 address)
  316. {
  317. if (ep->ep_state & EP_HAS_STREAMS)
  318. return radix_tree_lookup(&ep->stream_info->trb_address_map,
  319. address >> SEGMENT_SHIFT);
  320. return ep->ring;
  321. }
  322. /* Only use this when you know stream_info is valid */
  323. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  324. static struct xhci_ring *dma_to_stream_ring(
  325. struct xhci_stream_info *stream_info,
  326. u64 address)
  327. {
  328. return radix_tree_lookup(&stream_info->trb_address_map,
  329. address >> SEGMENT_SHIFT);
  330. }
  331. #endif /* CONFIG_USB_XHCI_HCD_DEBUGGING */
  332. struct xhci_ring *xhci_stream_id_to_ring(
  333. struct xhci_virt_device *dev,
  334. unsigned int ep_index,
  335. unsigned int stream_id)
  336. {
  337. struct xhci_virt_ep *ep = &dev->eps[ep_index];
  338. if (stream_id == 0)
  339. return ep->ring;
  340. if (!ep->stream_info)
  341. return NULL;
  342. if (stream_id > ep->stream_info->num_streams)
  343. return NULL;
  344. return ep->stream_info->stream_rings[stream_id];
  345. }
  346. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  347. static int xhci_test_radix_tree(struct xhci_hcd *xhci,
  348. unsigned int num_streams,
  349. struct xhci_stream_info *stream_info)
  350. {
  351. u32 cur_stream;
  352. struct xhci_ring *cur_ring;
  353. u64 addr;
  354. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  355. struct xhci_ring *mapped_ring;
  356. int trb_size = sizeof(union xhci_trb);
  357. cur_ring = stream_info->stream_rings[cur_stream];
  358. for (addr = cur_ring->first_seg->dma;
  359. addr < cur_ring->first_seg->dma + SEGMENT_SIZE;
  360. addr += trb_size) {
  361. mapped_ring = dma_to_stream_ring(stream_info, addr);
  362. if (cur_ring != mapped_ring) {
  363. xhci_warn(xhci, "WARN: DMA address 0x%08llx "
  364. "didn't map to stream ID %u; "
  365. "mapped to ring %p\n",
  366. (unsigned long long) addr,
  367. cur_stream,
  368. mapped_ring);
  369. return -EINVAL;
  370. }
  371. }
  372. /* One TRB after the end of the ring segment shouldn't return a
  373. * pointer to the current ring (although it may be a part of a
  374. * different ring).
  375. */
  376. mapped_ring = dma_to_stream_ring(stream_info, addr);
  377. if (mapped_ring != cur_ring) {
  378. /* One TRB before should also fail */
  379. addr = cur_ring->first_seg->dma - trb_size;
  380. mapped_ring = dma_to_stream_ring(stream_info, addr);
  381. }
  382. if (mapped_ring == cur_ring) {
  383. xhci_warn(xhci, "WARN: Bad DMA address 0x%08llx "
  384. "mapped to valid stream ID %u; "
  385. "mapped ring = %p\n",
  386. (unsigned long long) addr,
  387. cur_stream,
  388. mapped_ring);
  389. return -EINVAL;
  390. }
  391. }
  392. return 0;
  393. }
  394. #endif /* CONFIG_USB_XHCI_HCD_DEBUGGING */
  395. /*
  396. * Change an endpoint's internal structure so it supports stream IDs. The
  397. * number of requested streams includes stream 0, which cannot be used by device
  398. * drivers.
  399. *
  400. * The number of stream contexts in the stream context array may be bigger than
  401. * the number of streams the driver wants to use. This is because the number of
  402. * stream context array entries must be a power of two.
  403. *
  404. * We need a radix tree for mapping physical addresses of TRBs to which stream
  405. * ID they belong to. We need to do this because the host controller won't tell
  406. * us which stream ring the TRB came from. We could store the stream ID in an
  407. * event data TRB, but that doesn't help us for the cancellation case, since the
  408. * endpoint may stop before it reaches that event data TRB.
  409. *
  410. * The radix tree maps the upper portion of the TRB DMA address to a ring
  411. * segment that has the same upper portion of DMA addresses. For example, say I
  412. * have segments of size 1KB, that are always 64-byte aligned. A segment may
  413. * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
  414. * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
  415. * pass the radix tree a key to get the right stream ID:
  416. *
  417. * 0x10c90fff >> 10 = 0x43243
  418. * 0x10c912c0 >> 10 = 0x43244
  419. * 0x10c91400 >> 10 = 0x43245
  420. *
  421. * Obviously, only those TRBs with DMA addresses that are within the segment
  422. * will make the radix tree return the stream ID for that ring.
  423. *
  424. * Caveats for the radix tree:
  425. *
  426. * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
  427. * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
  428. * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
  429. * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
  430. * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
  431. * extended systems (where the DMA address can be bigger than 32-bits),
  432. * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
  433. */
  434. struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
  435. unsigned int num_stream_ctxs,
  436. unsigned int num_streams, gfp_t mem_flags)
  437. {
  438. struct xhci_stream_info *stream_info;
  439. u32 cur_stream;
  440. struct xhci_ring *cur_ring;
  441. unsigned long key;
  442. u64 addr;
  443. int ret;
  444. xhci_dbg(xhci, "Allocating %u streams and %u "
  445. "stream context array entries.\n",
  446. num_streams, num_stream_ctxs);
  447. if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) {
  448. xhci_dbg(xhci, "Command ring has no reserved TRBs available\n");
  449. return NULL;
  450. }
  451. xhci->cmd_ring_reserved_trbs++;
  452. stream_info = kzalloc(sizeof(struct xhci_stream_info), mem_flags);
  453. if (!stream_info)
  454. goto cleanup_trbs;
  455. stream_info->num_streams = num_streams;
  456. stream_info->num_stream_ctxs = num_stream_ctxs;
  457. /* Initialize the array of virtual pointers to stream rings. */
  458. stream_info->stream_rings = kzalloc(
  459. sizeof(struct xhci_ring *)*num_streams,
  460. mem_flags);
  461. if (!stream_info->stream_rings)
  462. goto cleanup_info;
  463. /* Initialize the array of DMA addresses for stream rings for the HW. */
  464. stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci,
  465. num_stream_ctxs, &stream_info->ctx_array_dma,
  466. mem_flags);
  467. if (!stream_info->stream_ctx_array)
  468. goto cleanup_ctx;
  469. memset(stream_info->stream_ctx_array, 0,
  470. sizeof(struct xhci_stream_ctx)*num_stream_ctxs);
  471. /* Allocate everything needed to free the stream rings later */
  472. stream_info->free_streams_command =
  473. xhci_alloc_command(xhci, true, true, mem_flags);
  474. if (!stream_info->free_streams_command)
  475. goto cleanup_ctx;
  476. INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
  477. /* Allocate rings for all the streams that the driver will use,
  478. * and add their segment DMA addresses to the radix tree.
  479. * Stream 0 is reserved.
  480. */
  481. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  482. stream_info->stream_rings[cur_stream] =
  483. xhci_ring_alloc(xhci, 1, true, mem_flags);
  484. cur_ring = stream_info->stream_rings[cur_stream];
  485. if (!cur_ring)
  486. goto cleanup_rings;
  487. cur_ring->stream_id = cur_stream;
  488. /* Set deq ptr, cycle bit, and stream context type */
  489. addr = cur_ring->first_seg->dma |
  490. SCT_FOR_CTX(SCT_PRI_TR) |
  491. cur_ring->cycle_state;
  492. stream_info->stream_ctx_array[cur_stream].stream_ring = addr;
  493. xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n",
  494. cur_stream, (unsigned long long) addr);
  495. key = (unsigned long)
  496. (cur_ring->first_seg->dma >> SEGMENT_SHIFT);
  497. ret = radix_tree_insert(&stream_info->trb_address_map,
  498. key, cur_ring);
  499. if (ret) {
  500. xhci_ring_free(xhci, cur_ring);
  501. stream_info->stream_rings[cur_stream] = NULL;
  502. goto cleanup_rings;
  503. }
  504. }
  505. /* Leave the other unused stream ring pointers in the stream context
  506. * array initialized to zero. This will cause the xHC to give us an
  507. * error if the device asks for a stream ID we don't have setup (if it
  508. * was any other way, the host controller would assume the ring is
  509. * "empty" and wait forever for data to be queued to that stream ID).
  510. */
  511. #if XHCI_DEBUG
  512. /* Do a little test on the radix tree to make sure it returns the
  513. * correct values.
  514. */
  515. if (xhci_test_radix_tree(xhci, num_streams, stream_info))
  516. goto cleanup_rings;
  517. #endif
  518. return stream_info;
  519. cleanup_rings:
  520. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  521. cur_ring = stream_info->stream_rings[cur_stream];
  522. if (cur_ring) {
  523. addr = cur_ring->first_seg->dma;
  524. radix_tree_delete(&stream_info->trb_address_map,
  525. addr >> SEGMENT_SHIFT);
  526. xhci_ring_free(xhci, cur_ring);
  527. stream_info->stream_rings[cur_stream] = NULL;
  528. }
  529. }
  530. xhci_free_command(xhci, stream_info->free_streams_command);
  531. cleanup_ctx:
  532. kfree(stream_info->stream_rings);
  533. cleanup_info:
  534. kfree(stream_info);
  535. cleanup_trbs:
  536. xhci->cmd_ring_reserved_trbs--;
  537. return NULL;
  538. }
  539. /*
  540. * Sets the MaxPStreams field and the Linear Stream Array field.
  541. * Sets the dequeue pointer to the stream context array.
  542. */
  543. void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci,
  544. struct xhci_ep_ctx *ep_ctx,
  545. struct xhci_stream_info *stream_info)
  546. {
  547. u32 max_primary_streams;
  548. /* MaxPStreams is the number of stream context array entries, not the
  549. * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
  550. * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
  551. */
  552. max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
  553. xhci_dbg(xhci, "Setting number of stream ctx array entries to %u\n",
  554. 1 << (max_primary_streams + 1));
  555. ep_ctx->ep_info &= ~EP_MAXPSTREAMS_MASK;
  556. ep_ctx->ep_info |= EP_MAXPSTREAMS(max_primary_streams);
  557. ep_ctx->ep_info |= EP_HAS_LSA;
  558. ep_ctx->deq = stream_info->ctx_array_dma;
  559. }
  560. /*
  561. * Sets the MaxPStreams field and the Linear Stream Array field to 0.
  562. * Reinstalls the "normal" endpoint ring (at its previous dequeue mark,
  563. * not at the beginning of the ring).
  564. */
  565. void xhci_setup_no_streams_ep_input_ctx(struct xhci_hcd *xhci,
  566. struct xhci_ep_ctx *ep_ctx,
  567. struct xhci_virt_ep *ep)
  568. {
  569. dma_addr_t addr;
  570. ep_ctx->ep_info &= ~EP_MAXPSTREAMS_MASK;
  571. ep_ctx->ep_info &= ~EP_HAS_LSA;
  572. addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue);
  573. ep_ctx->deq = addr | ep->ring->cycle_state;
  574. }
  575. /* Frees all stream contexts associated with the endpoint,
  576. *
  577. * Caller should fix the endpoint context streams fields.
  578. */
  579. void xhci_free_stream_info(struct xhci_hcd *xhci,
  580. struct xhci_stream_info *stream_info)
  581. {
  582. int cur_stream;
  583. struct xhci_ring *cur_ring;
  584. dma_addr_t addr;
  585. if (!stream_info)
  586. return;
  587. for (cur_stream = 1; cur_stream < stream_info->num_streams;
  588. cur_stream++) {
  589. cur_ring = stream_info->stream_rings[cur_stream];
  590. if (cur_ring) {
  591. addr = cur_ring->first_seg->dma;
  592. radix_tree_delete(&stream_info->trb_address_map,
  593. addr >> SEGMENT_SHIFT);
  594. xhci_ring_free(xhci, cur_ring);
  595. stream_info->stream_rings[cur_stream] = NULL;
  596. }
  597. }
  598. xhci_free_command(xhci, stream_info->free_streams_command);
  599. xhci->cmd_ring_reserved_trbs--;
  600. if (stream_info->stream_ctx_array)
  601. xhci_free_stream_ctx(xhci,
  602. stream_info->num_stream_ctxs,
  603. stream_info->stream_ctx_array,
  604. stream_info->ctx_array_dma);
  605. if (stream_info)
  606. kfree(stream_info->stream_rings);
  607. kfree(stream_info);
  608. }
  609. /***************** Device context manipulation *************************/
  610. static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
  611. struct xhci_virt_ep *ep)
  612. {
  613. init_timer(&ep->stop_cmd_timer);
  614. ep->stop_cmd_timer.data = (unsigned long) ep;
  615. ep->stop_cmd_timer.function = xhci_stop_endpoint_command_watchdog;
  616. ep->xhci = xhci;
  617. }
  618. /* All the xhci_tds in the ring's TD list should be freed at this point */
  619. void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
  620. {
  621. struct xhci_virt_device *dev;
  622. int i;
  623. /* Slot ID 0 is reserved */
  624. if (slot_id == 0 || !xhci->devs[slot_id])
  625. return;
  626. dev = xhci->devs[slot_id];
  627. xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
  628. if (!dev)
  629. return;
  630. for (i = 0; i < 31; ++i) {
  631. if (dev->eps[i].ring)
  632. xhci_ring_free(xhci, dev->eps[i].ring);
  633. if (dev->eps[i].stream_info)
  634. xhci_free_stream_info(xhci,
  635. dev->eps[i].stream_info);
  636. }
  637. if (dev->ring_cache) {
  638. for (i = 0; i < dev->num_rings_cached; i++)
  639. xhci_ring_free(xhci, dev->ring_cache[i]);
  640. kfree(dev->ring_cache);
  641. }
  642. if (dev->in_ctx)
  643. xhci_free_container_ctx(xhci, dev->in_ctx);
  644. if (dev->out_ctx)
  645. xhci_free_container_ctx(xhci, dev->out_ctx);
  646. kfree(xhci->devs[slot_id]);
  647. xhci->devs[slot_id] = NULL;
  648. }
  649. int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
  650. struct usb_device *udev, gfp_t flags)
  651. {
  652. struct xhci_virt_device *dev;
  653. int i;
  654. /* Slot ID 0 is reserved */
  655. if (slot_id == 0 || xhci->devs[slot_id]) {
  656. xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
  657. return 0;
  658. }
  659. xhci->devs[slot_id] = kzalloc(sizeof(*xhci->devs[slot_id]), flags);
  660. if (!xhci->devs[slot_id])
  661. return 0;
  662. dev = xhci->devs[slot_id];
  663. /* Allocate the (output) device context that will be used in the HC. */
  664. dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
  665. if (!dev->out_ctx)
  666. goto fail;
  667. xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
  668. (unsigned long long)dev->out_ctx->dma);
  669. /* Allocate the (input) device context for address device command */
  670. dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags);
  671. if (!dev->in_ctx)
  672. goto fail;
  673. xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
  674. (unsigned long long)dev->in_ctx->dma);
  675. /* Initialize the cancellation list and watchdog timers for each ep */
  676. for (i = 0; i < 31; i++) {
  677. xhci_init_endpoint_timer(xhci, &dev->eps[i]);
  678. INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
  679. }
  680. /* Allocate endpoint 0 ring */
  681. dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags);
  682. if (!dev->eps[0].ring)
  683. goto fail;
  684. /* Allocate pointers to the ring cache */
  685. dev->ring_cache = kzalloc(
  686. sizeof(struct xhci_ring *)*XHCI_MAX_RINGS_CACHED,
  687. flags);
  688. if (!dev->ring_cache)
  689. goto fail;
  690. dev->num_rings_cached = 0;
  691. init_completion(&dev->cmd_completion);
  692. INIT_LIST_HEAD(&dev->cmd_list);
  693. dev->udev = udev;
  694. /* Point to output device context in dcbaa. */
  695. xhci->dcbaa->dev_context_ptrs[slot_id] = dev->out_ctx->dma;
  696. xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
  697. slot_id,
  698. &xhci->dcbaa->dev_context_ptrs[slot_id],
  699. (unsigned long long) xhci->dcbaa->dev_context_ptrs[slot_id]);
  700. return 1;
  701. fail:
  702. xhci_free_virt_device(xhci, slot_id);
  703. return 0;
  704. }
  705. void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
  706. struct usb_device *udev)
  707. {
  708. struct xhci_virt_device *virt_dev;
  709. struct xhci_ep_ctx *ep0_ctx;
  710. struct xhci_ring *ep_ring;
  711. virt_dev = xhci->devs[udev->slot_id];
  712. ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0);
  713. ep_ring = virt_dev->eps[0].ring;
  714. /*
  715. * FIXME we don't keep track of the dequeue pointer very well after a
  716. * Set TR dequeue pointer, so we're setting the dequeue pointer of the
  717. * host to our enqueue pointer. This should only be called after a
  718. * configured device has reset, so all control transfers should have
  719. * been completed or cancelled before the reset.
  720. */
  721. ep0_ctx->deq = xhci_trb_virt_to_dma(ep_ring->enq_seg, ep_ring->enqueue);
  722. ep0_ctx->deq |= ep_ring->cycle_state;
  723. }
  724. /* Setup an xHCI virtual device for a Set Address command */
  725. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
  726. {
  727. struct xhci_virt_device *dev;
  728. struct xhci_ep_ctx *ep0_ctx;
  729. struct usb_device *top_dev;
  730. struct xhci_slot_ctx *slot_ctx;
  731. struct xhci_input_control_ctx *ctrl_ctx;
  732. dev = xhci->devs[udev->slot_id];
  733. /* Slot ID 0 is reserved */
  734. if (udev->slot_id == 0 || !dev) {
  735. xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
  736. udev->slot_id);
  737. return -EINVAL;
  738. }
  739. ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
  740. ctrl_ctx = xhci_get_input_control_ctx(xhci, dev->in_ctx);
  741. slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
  742. /* 2) New slot context and endpoint 0 context are valid*/
  743. ctrl_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
  744. /* 3) Only the control endpoint is valid - one endpoint context */
  745. slot_ctx->dev_info |= LAST_CTX(1);
  746. slot_ctx->dev_info |= (u32) udev->route;
  747. switch (udev->speed) {
  748. case USB_SPEED_SUPER:
  749. slot_ctx->dev_info |= (u32) SLOT_SPEED_SS;
  750. break;
  751. case USB_SPEED_HIGH:
  752. slot_ctx->dev_info |= (u32) SLOT_SPEED_HS;
  753. break;
  754. case USB_SPEED_FULL:
  755. slot_ctx->dev_info |= (u32) SLOT_SPEED_FS;
  756. break;
  757. case USB_SPEED_LOW:
  758. slot_ctx->dev_info |= (u32) SLOT_SPEED_LS;
  759. break;
  760. case USB_SPEED_WIRELESS:
  761. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  762. return -EINVAL;
  763. break;
  764. default:
  765. /* Speed was set earlier, this shouldn't happen. */
  766. BUG();
  767. }
  768. /* Find the root hub port this device is under */
  769. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  770. top_dev = top_dev->parent)
  771. /* Found device below root hub */;
  772. slot_ctx->dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
  773. dev->port = top_dev->portnum;
  774. xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
  775. /* Is this a LS/FS device under a HS hub? */
  776. if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
  777. udev->tt) {
  778. slot_ctx->tt_info = udev->tt->hub->slot_id;
  779. slot_ctx->tt_info |= udev->ttport << 8;
  780. if (udev->tt->multi)
  781. slot_ctx->dev_info |= DEV_MTT;
  782. }
  783. xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
  784. xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
  785. /* Step 4 - ring already allocated */
  786. /* Step 5 */
  787. ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
  788. /*
  789. * XXX: Not sure about wireless USB devices.
  790. */
  791. switch (udev->speed) {
  792. case USB_SPEED_SUPER:
  793. ep0_ctx->ep_info2 |= MAX_PACKET(512);
  794. break;
  795. case USB_SPEED_HIGH:
  796. /* USB core guesses at a 64-byte max packet first for FS devices */
  797. case USB_SPEED_FULL:
  798. ep0_ctx->ep_info2 |= MAX_PACKET(64);
  799. break;
  800. case USB_SPEED_LOW:
  801. ep0_ctx->ep_info2 |= MAX_PACKET(8);
  802. break;
  803. case USB_SPEED_WIRELESS:
  804. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  805. return -EINVAL;
  806. break;
  807. default:
  808. /* New speed? */
  809. BUG();
  810. }
  811. /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
  812. ep0_ctx->ep_info2 |= MAX_BURST(0);
  813. ep0_ctx->ep_info2 |= ERROR_COUNT(3);
  814. ep0_ctx->deq =
  815. dev->eps[0].ring->first_seg->dma;
  816. ep0_ctx->deq |= dev->eps[0].ring->cycle_state;
  817. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  818. return 0;
  819. }
  820. /* Return the polling or NAK interval.
  821. *
  822. * The polling interval is expressed in "microframes". If xHCI's Interval field
  823. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  824. *
  825. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  826. * is set to 0.
  827. */
  828. static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  829. struct usb_host_endpoint *ep)
  830. {
  831. unsigned int interval = 0;
  832. switch (udev->speed) {
  833. case USB_SPEED_HIGH:
  834. /* Max NAK rate */
  835. if (usb_endpoint_xfer_control(&ep->desc) ||
  836. usb_endpoint_xfer_bulk(&ep->desc))
  837. interval = ep->desc.bInterval;
  838. /* Fall through - SS and HS isoc/int have same decoding */
  839. case USB_SPEED_SUPER:
  840. if (usb_endpoint_xfer_int(&ep->desc) ||
  841. usb_endpoint_xfer_isoc(&ep->desc)) {
  842. if (ep->desc.bInterval == 0)
  843. interval = 0;
  844. else
  845. interval = ep->desc.bInterval - 1;
  846. if (interval > 15)
  847. interval = 15;
  848. if (interval != ep->desc.bInterval + 1)
  849. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  850. ep->desc.bEndpointAddress, 1 << interval);
  851. }
  852. break;
  853. /* Convert bInterval (in 1-255 frames) to microframes and round down to
  854. * nearest power of 2.
  855. */
  856. case USB_SPEED_FULL:
  857. case USB_SPEED_LOW:
  858. if (usb_endpoint_xfer_int(&ep->desc) ||
  859. usb_endpoint_xfer_isoc(&ep->desc)) {
  860. interval = fls(8*ep->desc.bInterval) - 1;
  861. if (interval > 10)
  862. interval = 10;
  863. if (interval < 3)
  864. interval = 3;
  865. if ((1 << interval) != 8*ep->desc.bInterval)
  866. dev_warn(&udev->dev,
  867. "ep %#x - rounding interval"
  868. " to %d microframes, "
  869. "ep desc says %d microframes\n",
  870. ep->desc.bEndpointAddress,
  871. 1 << interval,
  872. 8*ep->desc.bInterval);
  873. }
  874. break;
  875. default:
  876. BUG();
  877. }
  878. return EP_INTERVAL(interval);
  879. }
  880. /* The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
  881. * High speed endpoint descriptors can define "the number of additional
  882. * transaction opportunities per microframe", but that goes in the Max Burst
  883. * endpoint context field.
  884. */
  885. static inline u32 xhci_get_endpoint_mult(struct usb_device *udev,
  886. struct usb_host_endpoint *ep)
  887. {
  888. if (udev->speed != USB_SPEED_SUPER ||
  889. !usb_endpoint_xfer_isoc(&ep->desc))
  890. return 0;
  891. return ep->ss_ep_comp.bmAttributes;
  892. }
  893. static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
  894. struct usb_host_endpoint *ep)
  895. {
  896. int in;
  897. u32 type;
  898. in = usb_endpoint_dir_in(&ep->desc);
  899. if (usb_endpoint_xfer_control(&ep->desc)) {
  900. type = EP_TYPE(CTRL_EP);
  901. } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
  902. if (in)
  903. type = EP_TYPE(BULK_IN_EP);
  904. else
  905. type = EP_TYPE(BULK_OUT_EP);
  906. } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
  907. if (in)
  908. type = EP_TYPE(ISOC_IN_EP);
  909. else
  910. type = EP_TYPE(ISOC_OUT_EP);
  911. } else if (usb_endpoint_xfer_int(&ep->desc)) {
  912. if (in)
  913. type = EP_TYPE(INT_IN_EP);
  914. else
  915. type = EP_TYPE(INT_OUT_EP);
  916. } else {
  917. BUG();
  918. }
  919. return type;
  920. }
  921. /* Return the maximum endpoint service interval time (ESIT) payload.
  922. * Basically, this is the maxpacket size, multiplied by the burst size
  923. * and mult size.
  924. */
  925. static inline u32 xhci_get_max_esit_payload(struct xhci_hcd *xhci,
  926. struct usb_device *udev,
  927. struct usb_host_endpoint *ep)
  928. {
  929. int max_burst;
  930. int max_packet;
  931. /* Only applies for interrupt or isochronous endpoints */
  932. if (usb_endpoint_xfer_control(&ep->desc) ||
  933. usb_endpoint_xfer_bulk(&ep->desc))
  934. return 0;
  935. if (udev->speed == USB_SPEED_SUPER)
  936. return ep->ss_ep_comp.wBytesPerInterval;
  937. max_packet = GET_MAX_PACKET(ep->desc.wMaxPacketSize);
  938. max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
  939. /* A 0 in max burst means 1 transfer per ESIT */
  940. return max_packet * (max_burst + 1);
  941. }
  942. /* Set up an endpoint with one ring segment. Do not allocate stream rings.
  943. * Drivers will have to call usb_alloc_streams() to do that.
  944. */
  945. int xhci_endpoint_init(struct xhci_hcd *xhci,
  946. struct xhci_virt_device *virt_dev,
  947. struct usb_device *udev,
  948. struct usb_host_endpoint *ep,
  949. gfp_t mem_flags)
  950. {
  951. unsigned int ep_index;
  952. struct xhci_ep_ctx *ep_ctx;
  953. struct xhci_ring *ep_ring;
  954. unsigned int max_packet;
  955. unsigned int max_burst;
  956. u32 max_esit_payload;
  957. ep_index = xhci_get_endpoint_index(&ep->desc);
  958. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  959. /* Set up the endpoint ring */
  960. /*
  961. * Isochronous endpoint ring needs bigger size because one isoc URB
  962. * carries multiple packets and it will insert multiple tds to the
  963. * ring.
  964. * This should be replaced with dynamic ring resizing in the future.
  965. */
  966. if (usb_endpoint_xfer_isoc(&ep->desc))
  967. virt_dev->eps[ep_index].new_ring =
  968. xhci_ring_alloc(xhci, 8, true, mem_flags);
  969. else
  970. virt_dev->eps[ep_index].new_ring =
  971. xhci_ring_alloc(xhci, 1, true, mem_flags);
  972. if (!virt_dev->eps[ep_index].new_ring) {
  973. /* Attempt to use the ring cache */
  974. if (virt_dev->num_rings_cached == 0)
  975. return -ENOMEM;
  976. virt_dev->eps[ep_index].new_ring =
  977. virt_dev->ring_cache[virt_dev->num_rings_cached];
  978. virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
  979. virt_dev->num_rings_cached--;
  980. xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring);
  981. }
  982. virt_dev->eps[ep_index].skip = false;
  983. ep_ring = virt_dev->eps[ep_index].new_ring;
  984. ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
  985. ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
  986. ep_ctx->ep_info |= EP_MULT(xhci_get_endpoint_mult(udev, ep));
  987. /* FIXME dig Mult and streams info out of ep companion desc */
  988. /* Allow 3 retries for everything but isoc;
  989. * error count = 0 means infinite retries.
  990. */
  991. if (!usb_endpoint_xfer_isoc(&ep->desc))
  992. ep_ctx->ep_info2 = ERROR_COUNT(3);
  993. else
  994. ep_ctx->ep_info2 = ERROR_COUNT(1);
  995. ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
  996. /* Set the max packet size and max burst */
  997. switch (udev->speed) {
  998. case USB_SPEED_SUPER:
  999. max_packet = ep->desc.wMaxPacketSize;
  1000. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  1001. /* dig out max burst from ep companion desc */
  1002. max_packet = ep->ss_ep_comp.bMaxBurst;
  1003. if (!max_packet)
  1004. xhci_warn(xhci, "WARN no SS endpoint bMaxBurst\n");
  1005. ep_ctx->ep_info2 |= MAX_BURST(max_packet);
  1006. break;
  1007. case USB_SPEED_HIGH:
  1008. /* bits 11:12 specify the number of additional transaction
  1009. * opportunities per microframe (USB 2.0, section 9.6.6)
  1010. */
  1011. if (usb_endpoint_xfer_isoc(&ep->desc) ||
  1012. usb_endpoint_xfer_int(&ep->desc)) {
  1013. max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
  1014. ep_ctx->ep_info2 |= MAX_BURST(max_burst);
  1015. }
  1016. /* Fall through */
  1017. case USB_SPEED_FULL:
  1018. case USB_SPEED_LOW:
  1019. max_packet = GET_MAX_PACKET(ep->desc.wMaxPacketSize);
  1020. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  1021. break;
  1022. default:
  1023. BUG();
  1024. }
  1025. max_esit_payload = xhci_get_max_esit_payload(xhci, udev, ep);
  1026. ep_ctx->tx_info = MAX_ESIT_PAYLOAD_FOR_EP(max_esit_payload);
  1027. /*
  1028. * XXX no idea how to calculate the average TRB buffer length for bulk
  1029. * endpoints, as the driver gives us no clue how big each scatter gather
  1030. * list entry (or buffer) is going to be.
  1031. *
  1032. * For isochronous and interrupt endpoints, we set it to the max
  1033. * available, until we have new API in the USB core to allow drivers to
  1034. * declare how much bandwidth they actually need.
  1035. *
  1036. * Normally, it would be calculated by taking the total of the buffer
  1037. * lengths in the TD and then dividing by the number of TRBs in a TD,
  1038. * including link TRBs, No-op TRBs, and Event data TRBs. Since we don't
  1039. * use Event Data TRBs, and we don't chain in a link TRB on short
  1040. * transfers, we're basically dividing by 1.
  1041. */
  1042. ep_ctx->tx_info |= AVG_TRB_LENGTH_FOR_EP(max_esit_payload);
  1043. /* FIXME Debug endpoint context */
  1044. return 0;
  1045. }
  1046. void xhci_endpoint_zero(struct xhci_hcd *xhci,
  1047. struct xhci_virt_device *virt_dev,
  1048. struct usb_host_endpoint *ep)
  1049. {
  1050. unsigned int ep_index;
  1051. struct xhci_ep_ctx *ep_ctx;
  1052. ep_index = xhci_get_endpoint_index(&ep->desc);
  1053. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  1054. ep_ctx->ep_info = 0;
  1055. ep_ctx->ep_info2 = 0;
  1056. ep_ctx->deq = 0;
  1057. ep_ctx->tx_info = 0;
  1058. /* Don't free the endpoint ring until the set interface or configuration
  1059. * request succeeds.
  1060. */
  1061. }
  1062. /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
  1063. * Useful when you want to change one particular aspect of the endpoint and then
  1064. * issue a configure endpoint command.
  1065. */
  1066. void xhci_endpoint_copy(struct xhci_hcd *xhci,
  1067. struct xhci_container_ctx *in_ctx,
  1068. struct xhci_container_ctx *out_ctx,
  1069. unsigned int ep_index)
  1070. {
  1071. struct xhci_ep_ctx *out_ep_ctx;
  1072. struct xhci_ep_ctx *in_ep_ctx;
  1073. out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1074. in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  1075. in_ep_ctx->ep_info = out_ep_ctx->ep_info;
  1076. in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
  1077. in_ep_ctx->deq = out_ep_ctx->deq;
  1078. in_ep_ctx->tx_info = out_ep_ctx->tx_info;
  1079. }
  1080. /* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
  1081. * Useful when you want to change one particular aspect of the endpoint and then
  1082. * issue a configure endpoint command. Only the context entries field matters,
  1083. * but we'll copy the whole thing anyway.
  1084. */
  1085. void xhci_slot_copy(struct xhci_hcd *xhci,
  1086. struct xhci_container_ctx *in_ctx,
  1087. struct xhci_container_ctx *out_ctx)
  1088. {
  1089. struct xhci_slot_ctx *in_slot_ctx;
  1090. struct xhci_slot_ctx *out_slot_ctx;
  1091. in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1092. out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
  1093. in_slot_ctx->dev_info = out_slot_ctx->dev_info;
  1094. in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
  1095. in_slot_ctx->tt_info = out_slot_ctx->tt_info;
  1096. in_slot_ctx->dev_state = out_slot_ctx->dev_state;
  1097. }
  1098. /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
  1099. static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
  1100. {
  1101. int i;
  1102. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  1103. int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  1104. xhci_dbg(xhci, "Allocating %d scratchpad buffers\n", num_sp);
  1105. if (!num_sp)
  1106. return 0;
  1107. xhci->scratchpad = kzalloc(sizeof(*xhci->scratchpad), flags);
  1108. if (!xhci->scratchpad)
  1109. goto fail_sp;
  1110. xhci->scratchpad->sp_array =
  1111. pci_alloc_consistent(to_pci_dev(dev),
  1112. num_sp * sizeof(u64),
  1113. &xhci->scratchpad->sp_dma);
  1114. if (!xhci->scratchpad->sp_array)
  1115. goto fail_sp2;
  1116. xhci->scratchpad->sp_buffers = kzalloc(sizeof(void *) * num_sp, flags);
  1117. if (!xhci->scratchpad->sp_buffers)
  1118. goto fail_sp3;
  1119. xhci->scratchpad->sp_dma_buffers =
  1120. kzalloc(sizeof(dma_addr_t) * num_sp, flags);
  1121. if (!xhci->scratchpad->sp_dma_buffers)
  1122. goto fail_sp4;
  1123. xhci->dcbaa->dev_context_ptrs[0] = xhci->scratchpad->sp_dma;
  1124. for (i = 0; i < num_sp; i++) {
  1125. dma_addr_t dma;
  1126. void *buf = pci_alloc_consistent(to_pci_dev(dev),
  1127. xhci->page_size, &dma);
  1128. if (!buf)
  1129. goto fail_sp5;
  1130. xhci->scratchpad->sp_array[i] = dma;
  1131. xhci->scratchpad->sp_buffers[i] = buf;
  1132. xhci->scratchpad->sp_dma_buffers[i] = dma;
  1133. }
  1134. return 0;
  1135. fail_sp5:
  1136. for (i = i - 1; i >= 0; i--) {
  1137. pci_free_consistent(to_pci_dev(dev), xhci->page_size,
  1138. xhci->scratchpad->sp_buffers[i],
  1139. xhci->scratchpad->sp_dma_buffers[i]);
  1140. }
  1141. kfree(xhci->scratchpad->sp_dma_buffers);
  1142. fail_sp4:
  1143. kfree(xhci->scratchpad->sp_buffers);
  1144. fail_sp3:
  1145. pci_free_consistent(to_pci_dev(dev), num_sp * sizeof(u64),
  1146. xhci->scratchpad->sp_array,
  1147. xhci->scratchpad->sp_dma);
  1148. fail_sp2:
  1149. kfree(xhci->scratchpad);
  1150. xhci->scratchpad = NULL;
  1151. fail_sp:
  1152. return -ENOMEM;
  1153. }
  1154. static void scratchpad_free(struct xhci_hcd *xhci)
  1155. {
  1156. int num_sp;
  1157. int i;
  1158. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  1159. if (!xhci->scratchpad)
  1160. return;
  1161. num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  1162. for (i = 0; i < num_sp; i++) {
  1163. pci_free_consistent(pdev, xhci->page_size,
  1164. xhci->scratchpad->sp_buffers[i],
  1165. xhci->scratchpad->sp_dma_buffers[i]);
  1166. }
  1167. kfree(xhci->scratchpad->sp_dma_buffers);
  1168. kfree(xhci->scratchpad->sp_buffers);
  1169. pci_free_consistent(pdev, num_sp * sizeof(u64),
  1170. xhci->scratchpad->sp_array,
  1171. xhci->scratchpad->sp_dma);
  1172. kfree(xhci->scratchpad);
  1173. xhci->scratchpad = NULL;
  1174. }
  1175. struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
  1176. bool allocate_in_ctx, bool allocate_completion,
  1177. gfp_t mem_flags)
  1178. {
  1179. struct xhci_command *command;
  1180. command = kzalloc(sizeof(*command), mem_flags);
  1181. if (!command)
  1182. return NULL;
  1183. if (allocate_in_ctx) {
  1184. command->in_ctx =
  1185. xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT,
  1186. mem_flags);
  1187. if (!command->in_ctx) {
  1188. kfree(command);
  1189. return NULL;
  1190. }
  1191. }
  1192. if (allocate_completion) {
  1193. command->completion =
  1194. kzalloc(sizeof(struct completion), mem_flags);
  1195. if (!command->completion) {
  1196. xhci_free_container_ctx(xhci, command->in_ctx);
  1197. kfree(command);
  1198. return NULL;
  1199. }
  1200. init_completion(command->completion);
  1201. }
  1202. command->status = 0;
  1203. INIT_LIST_HEAD(&command->cmd_list);
  1204. return command;
  1205. }
  1206. void xhci_urb_free_priv(struct xhci_hcd *xhci, struct urb_priv *urb_priv)
  1207. {
  1208. int last;
  1209. if (!urb_priv)
  1210. return;
  1211. last = urb_priv->length - 1;
  1212. if (last >= 0) {
  1213. int i;
  1214. for (i = 0; i <= last; i++)
  1215. kfree(urb_priv->td[i]);
  1216. }
  1217. kfree(urb_priv);
  1218. }
  1219. void xhci_free_command(struct xhci_hcd *xhci,
  1220. struct xhci_command *command)
  1221. {
  1222. xhci_free_container_ctx(xhci,
  1223. command->in_ctx);
  1224. kfree(command->completion);
  1225. kfree(command);
  1226. }
  1227. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  1228. {
  1229. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  1230. int size;
  1231. int i;
  1232. /* Free the Event Ring Segment Table and the actual Event Ring */
  1233. if (xhci->ir_set) {
  1234. xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
  1235. xhci_write_64(xhci, 0, &xhci->ir_set->erst_base);
  1236. xhci_write_64(xhci, 0, &xhci->ir_set->erst_dequeue);
  1237. }
  1238. size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
  1239. if (xhci->erst.entries)
  1240. pci_free_consistent(pdev, size,
  1241. xhci->erst.entries, xhci->erst.erst_dma_addr);
  1242. xhci->erst.entries = NULL;
  1243. xhci_dbg(xhci, "Freed ERST\n");
  1244. if (xhci->event_ring)
  1245. xhci_ring_free(xhci, xhci->event_ring);
  1246. xhci->event_ring = NULL;
  1247. xhci_dbg(xhci, "Freed event ring\n");
  1248. xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
  1249. if (xhci->cmd_ring)
  1250. xhci_ring_free(xhci, xhci->cmd_ring);
  1251. xhci->cmd_ring = NULL;
  1252. xhci_dbg(xhci, "Freed command ring\n");
  1253. for (i = 1; i < MAX_HC_SLOTS; ++i)
  1254. xhci_free_virt_device(xhci, i);
  1255. if (xhci->segment_pool)
  1256. dma_pool_destroy(xhci->segment_pool);
  1257. xhci->segment_pool = NULL;
  1258. xhci_dbg(xhci, "Freed segment pool\n");
  1259. if (xhci->device_pool)
  1260. dma_pool_destroy(xhci->device_pool);
  1261. xhci->device_pool = NULL;
  1262. xhci_dbg(xhci, "Freed device context pool\n");
  1263. if (xhci->small_streams_pool)
  1264. dma_pool_destroy(xhci->small_streams_pool);
  1265. xhci->small_streams_pool = NULL;
  1266. xhci_dbg(xhci, "Freed small stream array pool\n");
  1267. if (xhci->medium_streams_pool)
  1268. dma_pool_destroy(xhci->medium_streams_pool);
  1269. xhci->medium_streams_pool = NULL;
  1270. xhci_dbg(xhci, "Freed medium stream array pool\n");
  1271. xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
  1272. if (xhci->dcbaa)
  1273. pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
  1274. xhci->dcbaa, xhci->dcbaa->dma);
  1275. xhci->dcbaa = NULL;
  1276. scratchpad_free(xhci);
  1277. xhci->num_usb2_ports = 0;
  1278. xhci->num_usb3_ports = 0;
  1279. kfree(xhci->usb2_ports);
  1280. kfree(xhci->usb3_ports);
  1281. kfree(xhci->port_array);
  1282. xhci->page_size = 0;
  1283. xhci->page_shift = 0;
  1284. xhci->bus_suspended = 0;
  1285. }
  1286. static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
  1287. struct xhci_segment *input_seg,
  1288. union xhci_trb *start_trb,
  1289. union xhci_trb *end_trb,
  1290. dma_addr_t input_dma,
  1291. struct xhci_segment *result_seg,
  1292. char *test_name, int test_number)
  1293. {
  1294. unsigned long long start_dma;
  1295. unsigned long long end_dma;
  1296. struct xhci_segment *seg;
  1297. start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
  1298. end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
  1299. seg = trb_in_td(input_seg, start_trb, end_trb, input_dma);
  1300. if (seg != result_seg) {
  1301. xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
  1302. test_name, test_number);
  1303. xhci_warn(xhci, "Tested TRB math w/ seg %p and "
  1304. "input DMA 0x%llx\n",
  1305. input_seg,
  1306. (unsigned long long) input_dma);
  1307. xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
  1308. "ending TRB %p (0x%llx DMA)\n",
  1309. start_trb, start_dma,
  1310. end_trb, end_dma);
  1311. xhci_warn(xhci, "Expected seg %p, got seg %p\n",
  1312. result_seg, seg);
  1313. return -1;
  1314. }
  1315. return 0;
  1316. }
  1317. /* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
  1318. static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
  1319. {
  1320. struct {
  1321. dma_addr_t input_dma;
  1322. struct xhci_segment *result_seg;
  1323. } simple_test_vector [] = {
  1324. /* A zeroed DMA field should fail */
  1325. { 0, NULL },
  1326. /* One TRB before the ring start should fail */
  1327. { xhci->event_ring->first_seg->dma - 16, NULL },
  1328. /* One byte before the ring start should fail */
  1329. { xhci->event_ring->first_seg->dma - 1, NULL },
  1330. /* Starting TRB should succeed */
  1331. { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
  1332. /* Ending TRB should succeed */
  1333. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
  1334. xhci->event_ring->first_seg },
  1335. /* One byte after the ring end should fail */
  1336. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
  1337. /* One TRB after the ring end should fail */
  1338. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
  1339. /* An address of all ones should fail */
  1340. { (dma_addr_t) (~0), NULL },
  1341. };
  1342. struct {
  1343. struct xhci_segment *input_seg;
  1344. union xhci_trb *start_trb;
  1345. union xhci_trb *end_trb;
  1346. dma_addr_t input_dma;
  1347. struct xhci_segment *result_seg;
  1348. } complex_test_vector [] = {
  1349. /* Test feeding a valid DMA address from a different ring */
  1350. { .input_seg = xhci->event_ring->first_seg,
  1351. .start_trb = xhci->event_ring->first_seg->trbs,
  1352. .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1353. .input_dma = xhci->cmd_ring->first_seg->dma,
  1354. .result_seg = NULL,
  1355. },
  1356. /* Test feeding a valid end TRB from a different ring */
  1357. { .input_seg = xhci->event_ring->first_seg,
  1358. .start_trb = xhci->event_ring->first_seg->trbs,
  1359. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1360. .input_dma = xhci->cmd_ring->first_seg->dma,
  1361. .result_seg = NULL,
  1362. },
  1363. /* Test feeding a valid start and end TRB from a different ring */
  1364. { .input_seg = xhci->event_ring->first_seg,
  1365. .start_trb = xhci->cmd_ring->first_seg->trbs,
  1366. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1367. .input_dma = xhci->cmd_ring->first_seg->dma,
  1368. .result_seg = NULL,
  1369. },
  1370. /* TRB in this ring, but after this TD */
  1371. { .input_seg = xhci->event_ring->first_seg,
  1372. .start_trb = &xhci->event_ring->first_seg->trbs[0],
  1373. .end_trb = &xhci->event_ring->first_seg->trbs[3],
  1374. .input_dma = xhci->event_ring->first_seg->dma + 4*16,
  1375. .result_seg = NULL,
  1376. },
  1377. /* TRB in this ring, but before this TD */
  1378. { .input_seg = xhci->event_ring->first_seg,
  1379. .start_trb = &xhci->event_ring->first_seg->trbs[3],
  1380. .end_trb = &xhci->event_ring->first_seg->trbs[6],
  1381. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  1382. .result_seg = NULL,
  1383. },
  1384. /* TRB in this ring, but after this wrapped TD */
  1385. { .input_seg = xhci->event_ring->first_seg,
  1386. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1387. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1388. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  1389. .result_seg = NULL,
  1390. },
  1391. /* TRB in this ring, but before this wrapped TD */
  1392. { .input_seg = xhci->event_ring->first_seg,
  1393. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1394. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1395. .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
  1396. .result_seg = NULL,
  1397. },
  1398. /* TRB not in this ring, and we have a wrapped TD */
  1399. { .input_seg = xhci->event_ring->first_seg,
  1400. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  1401. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  1402. .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
  1403. .result_seg = NULL,
  1404. },
  1405. };
  1406. unsigned int num_tests;
  1407. int i, ret;
  1408. num_tests = ARRAY_SIZE(simple_test_vector);
  1409. for (i = 0; i < num_tests; i++) {
  1410. ret = xhci_test_trb_in_td(xhci,
  1411. xhci->event_ring->first_seg,
  1412. xhci->event_ring->first_seg->trbs,
  1413. &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  1414. simple_test_vector[i].input_dma,
  1415. simple_test_vector[i].result_seg,
  1416. "Simple", i);
  1417. if (ret < 0)
  1418. return ret;
  1419. }
  1420. num_tests = ARRAY_SIZE(complex_test_vector);
  1421. for (i = 0; i < num_tests; i++) {
  1422. ret = xhci_test_trb_in_td(xhci,
  1423. complex_test_vector[i].input_seg,
  1424. complex_test_vector[i].start_trb,
  1425. complex_test_vector[i].end_trb,
  1426. complex_test_vector[i].input_dma,
  1427. complex_test_vector[i].result_seg,
  1428. "Complex", i);
  1429. if (ret < 0)
  1430. return ret;
  1431. }
  1432. xhci_dbg(xhci, "TRB math tests passed.\n");
  1433. return 0;
  1434. }
  1435. static void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
  1436. {
  1437. u64 temp;
  1438. dma_addr_t deq;
  1439. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  1440. xhci->event_ring->dequeue);
  1441. if (deq == 0 && !in_interrupt())
  1442. xhci_warn(xhci, "WARN something wrong with SW event ring "
  1443. "dequeue ptr.\n");
  1444. /* Update HC event ring dequeue pointer */
  1445. temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  1446. temp &= ERST_PTR_MASK;
  1447. /* Don't clear the EHB bit (which is RW1C) because
  1448. * there might be more events to service.
  1449. */
  1450. temp &= ~ERST_EHB;
  1451. xhci_dbg(xhci, "// Write event ring dequeue pointer, "
  1452. "preserving EHB bit\n");
  1453. xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
  1454. &xhci->ir_set->erst_dequeue);
  1455. }
  1456. static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports,
  1457. u32 __iomem *addr, u8 major_revision)
  1458. {
  1459. u32 temp, port_offset, port_count;
  1460. int i;
  1461. if (major_revision > 0x03) {
  1462. xhci_warn(xhci, "Ignoring unknown port speed, "
  1463. "Ext Cap %p, revision = 0x%x\n",
  1464. addr, major_revision);
  1465. /* Ignoring port protocol we can't understand. FIXME */
  1466. return;
  1467. }
  1468. /* Port offset and count in the third dword, see section 7.2 */
  1469. temp = xhci_readl(xhci, addr + 2);
  1470. port_offset = XHCI_EXT_PORT_OFF(temp);
  1471. port_count = XHCI_EXT_PORT_COUNT(temp);
  1472. xhci_dbg(xhci, "Ext Cap %p, port offset = %u, "
  1473. "count = %u, revision = 0x%x\n",
  1474. addr, port_offset, port_count, major_revision);
  1475. /* Port count includes the current port offset */
  1476. if (port_offset == 0 || (port_offset + port_count - 1) > num_ports)
  1477. /* WTF? "Valid values are ‘1’ to MaxPorts" */
  1478. return;
  1479. port_offset--;
  1480. for (i = port_offset; i < (port_offset + port_count); i++) {
  1481. /* Duplicate entry. Ignore the port if the revisions differ. */
  1482. if (xhci->port_array[i] != 0) {
  1483. xhci_warn(xhci, "Duplicate port entry, Ext Cap %p,"
  1484. " port %u\n", addr, i);
  1485. xhci_warn(xhci, "Port was marked as USB %u, "
  1486. "duplicated as USB %u\n",
  1487. xhci->port_array[i], major_revision);
  1488. /* Only adjust the roothub port counts if we haven't
  1489. * found a similar duplicate.
  1490. */
  1491. if (xhci->port_array[i] != major_revision &&
  1492. xhci->port_array[i] != (u8) -1) {
  1493. if (xhci->port_array[i] == 0x03)
  1494. xhci->num_usb3_ports--;
  1495. else
  1496. xhci->num_usb2_ports--;
  1497. xhci->port_array[i] = (u8) -1;
  1498. }
  1499. /* FIXME: Should we disable the port? */
  1500. continue;
  1501. }
  1502. xhci->port_array[i] = major_revision;
  1503. if (major_revision == 0x03)
  1504. xhci->num_usb3_ports++;
  1505. else
  1506. xhci->num_usb2_ports++;
  1507. }
  1508. /* FIXME: Should we disable ports not in the Extended Capabilities? */
  1509. }
  1510. /*
  1511. * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that
  1512. * specify what speeds each port is supposed to be. We can't count on the port
  1513. * speed bits in the PORTSC register being correct until a device is connected,
  1514. * but we need to set up the two fake roothubs with the correct number of USB
  1515. * 3.0 and USB 2.0 ports at host controller initialization time.
  1516. */
  1517. static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)
  1518. {
  1519. u32 __iomem *addr;
  1520. u32 offset;
  1521. unsigned int num_ports;
  1522. int i, port_index;
  1523. addr = &xhci->cap_regs->hcc_params;
  1524. offset = XHCI_HCC_EXT_CAPS(xhci_readl(xhci, addr));
  1525. if (offset == 0) {
  1526. xhci_err(xhci, "No Extended Capability registers, "
  1527. "unable to set up roothub.\n");
  1528. return -ENODEV;
  1529. }
  1530. num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
  1531. xhci->port_array = kzalloc(sizeof(*xhci->port_array)*num_ports, flags);
  1532. if (!xhci->port_array)
  1533. return -ENOMEM;
  1534. /*
  1535. * For whatever reason, the first capability offset is from the
  1536. * capability register base, not from the HCCPARAMS register.
  1537. * See section 5.3.6 for offset calculation.
  1538. */
  1539. addr = &xhci->cap_regs->hc_capbase + offset;
  1540. while (1) {
  1541. u32 cap_id;
  1542. cap_id = xhci_readl(xhci, addr);
  1543. if (XHCI_EXT_CAPS_ID(cap_id) == XHCI_EXT_CAPS_PROTOCOL)
  1544. xhci_add_in_port(xhci, num_ports, addr,
  1545. (u8) XHCI_EXT_PORT_MAJOR(cap_id));
  1546. offset = XHCI_EXT_CAPS_NEXT(cap_id);
  1547. if (!offset || (xhci->num_usb2_ports + xhci->num_usb3_ports)
  1548. == num_ports)
  1549. break;
  1550. /*
  1551. * Once you're into the Extended Capabilities, the offset is
  1552. * always relative to the register holding the offset.
  1553. */
  1554. addr += offset;
  1555. }
  1556. if (xhci->num_usb2_ports == 0 && xhci->num_usb3_ports == 0) {
  1557. xhci_warn(xhci, "No ports on the roothubs?\n");
  1558. return -ENODEV;
  1559. }
  1560. xhci_dbg(xhci, "Found %u USB 2.0 ports and %u USB 3.0 ports.\n",
  1561. xhci->num_usb2_ports, xhci->num_usb3_ports);
  1562. /*
  1563. * Note we could have all USB 3.0 ports, or all USB 2.0 ports.
  1564. * Not sure how the USB core will handle a hub with no ports...
  1565. */
  1566. if (xhci->num_usb2_ports) {
  1567. xhci->usb2_ports = kmalloc(sizeof(*xhci->usb2_ports)*
  1568. xhci->num_usb2_ports, flags);
  1569. if (!xhci->usb2_ports)
  1570. return -ENOMEM;
  1571. port_index = 0;
  1572. for (i = 0; i < num_ports; i++) {
  1573. if (xhci->port_array[i] == 0x03 ||
  1574. xhci->port_array[i] == 0 ||
  1575. xhci->port_array[i] == -1)
  1576. continue;
  1577. xhci->usb2_ports[port_index] =
  1578. &xhci->op_regs->port_status_base +
  1579. NUM_PORT_REGS*i;
  1580. xhci_dbg(xhci, "USB 2.0 port at index %u, "
  1581. "addr = %p\n", i,
  1582. xhci->usb2_ports[port_index]);
  1583. port_index++;
  1584. }
  1585. }
  1586. if (xhci->num_usb3_ports) {
  1587. xhci->usb3_ports = kmalloc(sizeof(*xhci->usb3_ports)*
  1588. xhci->num_usb3_ports, flags);
  1589. if (!xhci->usb3_ports)
  1590. return -ENOMEM;
  1591. port_index = 0;
  1592. for (i = 0; i < num_ports; i++)
  1593. if (xhci->port_array[i] == 0x03) {
  1594. xhci->usb3_ports[port_index] =
  1595. &xhci->op_regs->port_status_base +
  1596. NUM_PORT_REGS*i;
  1597. xhci_dbg(xhci, "USB 3.0 port at index %u, "
  1598. "addr = %p\n", i,
  1599. xhci->usb3_ports[port_index]);
  1600. port_index++;
  1601. }
  1602. }
  1603. return 0;
  1604. }
  1605. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  1606. {
  1607. dma_addr_t dma;
  1608. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  1609. unsigned int val, val2;
  1610. u64 val_64;
  1611. struct xhci_segment *seg;
  1612. u32 page_size;
  1613. int i;
  1614. page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
  1615. xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
  1616. for (i = 0; i < 16; i++) {
  1617. if ((0x1 & page_size) != 0)
  1618. break;
  1619. page_size = page_size >> 1;
  1620. }
  1621. if (i < 16)
  1622. xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
  1623. else
  1624. xhci_warn(xhci, "WARN: no supported page size\n");
  1625. /* Use 4K pages, since that's common and the minimum the HC supports */
  1626. xhci->page_shift = 12;
  1627. xhci->page_size = 1 << xhci->page_shift;
  1628. xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
  1629. /*
  1630. * Program the Number of Device Slots Enabled field in the CONFIG
  1631. * register with the max value of slots the HC can handle.
  1632. */
  1633. val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
  1634. xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
  1635. (unsigned int) val);
  1636. val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
  1637. val |= (val2 & ~HCS_SLOTS_MASK);
  1638. xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
  1639. (unsigned int) val);
  1640. xhci_writel(xhci, val, &xhci->op_regs->config_reg);
  1641. /*
  1642. * Section 5.4.8 - doorbell array must be
  1643. * "physically contiguous and 64-byte (cache line) aligned".
  1644. */
  1645. xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
  1646. sizeof(*xhci->dcbaa), &dma);
  1647. if (!xhci->dcbaa)
  1648. goto fail;
  1649. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  1650. xhci->dcbaa->dma = dma;
  1651. xhci_dbg(xhci, "// Device context base array address = 0x%llx (DMA), %p (virt)\n",
  1652. (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
  1653. xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
  1654. /*
  1655. * Initialize the ring segment pool. The ring must be a contiguous
  1656. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  1657. * however, the command ring segment needs 64-byte aligned segments,
  1658. * so we pick the greater alignment need.
  1659. */
  1660. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  1661. SEGMENT_SIZE, 64, xhci->page_size);
  1662. /* See Table 46 and Note on Figure 55 */
  1663. xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
  1664. 2112, 64, xhci->page_size);
  1665. if (!xhci->segment_pool || !xhci->device_pool)
  1666. goto fail;
  1667. /* Linear stream context arrays don't have any boundary restrictions,
  1668. * and only need to be 16-byte aligned.
  1669. */
  1670. xhci->small_streams_pool =
  1671. dma_pool_create("xHCI 256 byte stream ctx arrays",
  1672. dev, SMALL_STREAM_ARRAY_SIZE, 16, 0);
  1673. xhci->medium_streams_pool =
  1674. dma_pool_create("xHCI 1KB stream ctx arrays",
  1675. dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
  1676. /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
  1677. * will be allocated with pci_alloc_consistent()
  1678. */
  1679. if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
  1680. goto fail;
  1681. /* Set up the command ring to have one segments for now. */
  1682. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
  1683. if (!xhci->cmd_ring)
  1684. goto fail;
  1685. xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
  1686. xhci_dbg(xhci, "First segment DMA is 0x%llx\n",
  1687. (unsigned long long)xhci->cmd_ring->first_seg->dma);
  1688. /* Set the address in the Command Ring Control register */
  1689. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  1690. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  1691. (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
  1692. xhci->cmd_ring->cycle_state;
  1693. xhci_dbg(xhci, "// Setting command ring address to 0x%x\n", val);
  1694. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  1695. xhci_dbg_cmd_ptrs(xhci);
  1696. val = xhci_readl(xhci, &xhci->cap_regs->db_off);
  1697. val &= DBOFF_MASK;
  1698. xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
  1699. " from cap regs base addr\n", val);
  1700. xhci->dba = (void *) xhci->cap_regs + val;
  1701. xhci_dbg_regs(xhci);
  1702. xhci_print_run_regs(xhci);
  1703. /* Set ir_set to interrupt register set 0 */
  1704. xhci->ir_set = (void *) xhci->run_regs->ir_set;
  1705. /*
  1706. * Event ring setup: Allocate a normal ring, but also setup
  1707. * the event ring segment table (ERST). Section 4.9.3.
  1708. */
  1709. xhci_dbg(xhci, "// Allocating event ring\n");
  1710. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
  1711. if (!xhci->event_ring)
  1712. goto fail;
  1713. if (xhci_check_trb_in_td_math(xhci, flags) < 0)
  1714. goto fail;
  1715. xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
  1716. sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
  1717. if (!xhci->erst.entries)
  1718. goto fail;
  1719. xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
  1720. (unsigned long long)dma);
  1721. memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
  1722. xhci->erst.num_entries = ERST_NUM_SEGS;
  1723. xhci->erst.erst_dma_addr = dma;
  1724. xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = %p, dma addr = 0x%llx\n",
  1725. xhci->erst.num_entries,
  1726. xhci->erst.entries,
  1727. (unsigned long long)xhci->erst.erst_dma_addr);
  1728. /* set ring base address and size for each segment table entry */
  1729. for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
  1730. struct xhci_erst_entry *entry = &xhci->erst.entries[val];
  1731. entry->seg_addr = seg->dma;
  1732. entry->seg_size = TRBS_PER_SEGMENT;
  1733. entry->rsvd = 0;
  1734. seg = seg->next;
  1735. }
  1736. /* set ERST count with the number of entries in the segment table */
  1737. val = xhci_readl(xhci, &xhci->ir_set->erst_size);
  1738. val &= ERST_SIZE_MASK;
  1739. val |= ERST_NUM_SEGS;
  1740. xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
  1741. val);
  1742. xhci_writel(xhci, val, &xhci->ir_set->erst_size);
  1743. xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
  1744. /* set the segment table base address */
  1745. xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%llx\n",
  1746. (unsigned long long)xhci->erst.erst_dma_addr);
  1747. val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  1748. val_64 &= ERST_PTR_MASK;
  1749. val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
  1750. xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
  1751. /* Set the event ring dequeue address */
  1752. xhci_set_hc_event_deq(xhci);
  1753. xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
  1754. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  1755. /*
  1756. * XXX: Might need to set the Interrupter Moderation Register to
  1757. * something other than the default (~1ms minimum between interrupts).
  1758. * See section 5.5.1.2.
  1759. */
  1760. init_completion(&xhci->addr_dev);
  1761. for (i = 0; i < MAX_HC_SLOTS; ++i)
  1762. xhci->devs[i] = NULL;
  1763. for (i = 0; i < MAX_HC_PORTS; ++i)
  1764. xhci->resume_done[i] = 0;
  1765. if (scratchpad_alloc(xhci, flags))
  1766. goto fail;
  1767. if (xhci_setup_port_arrays(xhci, flags))
  1768. goto fail;
  1769. return 0;
  1770. fail:
  1771. xhci_warn(xhci, "Couldn't initialize memory\n");
  1772. xhci_mem_cleanup(xhci);
  1773. return -ENOMEM;
  1774. }