xhci-mem.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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 0;
  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 0;
  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 0;
  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 0;
  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. 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. 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. static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
  271. struct xhci_virt_ep *ep)
  272. {
  273. init_timer(&ep->stop_cmd_timer);
  274. ep->stop_cmd_timer.data = (unsigned long) ep;
  275. ep->stop_cmd_timer.function = xhci_stop_endpoint_command_watchdog;
  276. ep->xhci = xhci;
  277. }
  278. /* All the xhci_tds in the ring's TD list should be freed at this point */
  279. void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
  280. {
  281. struct xhci_virt_device *dev;
  282. int i;
  283. /* Slot ID 0 is reserved */
  284. if (slot_id == 0 || !xhci->devs[slot_id])
  285. return;
  286. dev = xhci->devs[slot_id];
  287. xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
  288. if (!dev)
  289. return;
  290. for (i = 0; i < 31; ++i)
  291. if (dev->eps[i].ring)
  292. xhci_ring_free(xhci, dev->eps[i].ring);
  293. if (dev->ring_cache) {
  294. for (i = 0; i < dev->num_rings_cached; i++)
  295. xhci_ring_free(xhci, dev->ring_cache[i]);
  296. kfree(dev->ring_cache);
  297. }
  298. if (dev->in_ctx)
  299. xhci_free_container_ctx(xhci, dev->in_ctx);
  300. if (dev->out_ctx)
  301. xhci_free_container_ctx(xhci, dev->out_ctx);
  302. kfree(xhci->devs[slot_id]);
  303. xhci->devs[slot_id] = 0;
  304. }
  305. int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
  306. struct usb_device *udev, gfp_t flags)
  307. {
  308. struct xhci_virt_device *dev;
  309. int i;
  310. /* Slot ID 0 is reserved */
  311. if (slot_id == 0 || xhci->devs[slot_id]) {
  312. xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
  313. return 0;
  314. }
  315. xhci->devs[slot_id] = kzalloc(sizeof(*xhci->devs[slot_id]), flags);
  316. if (!xhci->devs[slot_id])
  317. return 0;
  318. dev = xhci->devs[slot_id];
  319. /* Allocate the (output) device context that will be used in the HC. */
  320. dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
  321. if (!dev->out_ctx)
  322. goto fail;
  323. xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
  324. (unsigned long long)dev->out_ctx->dma);
  325. /* Allocate the (input) device context for address device command */
  326. dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags);
  327. if (!dev->in_ctx)
  328. goto fail;
  329. xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
  330. (unsigned long long)dev->in_ctx->dma);
  331. /* Initialize the cancellation list and watchdog timers for each ep */
  332. for (i = 0; i < 31; i++) {
  333. xhci_init_endpoint_timer(xhci, &dev->eps[i]);
  334. INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
  335. }
  336. /* Allocate endpoint 0 ring */
  337. dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags);
  338. if (!dev->eps[0].ring)
  339. goto fail;
  340. /* Allocate pointers to the ring cache */
  341. dev->ring_cache = kzalloc(
  342. sizeof(struct xhci_ring *)*XHCI_MAX_RINGS_CACHED,
  343. flags);
  344. if (!dev->ring_cache)
  345. goto fail;
  346. dev->num_rings_cached = 0;
  347. init_completion(&dev->cmd_completion);
  348. INIT_LIST_HEAD(&dev->cmd_list);
  349. /* Point to output device context in dcbaa. */
  350. xhci->dcbaa->dev_context_ptrs[slot_id] = dev->out_ctx->dma;
  351. xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
  352. slot_id,
  353. &xhci->dcbaa->dev_context_ptrs[slot_id],
  354. (unsigned long long) xhci->dcbaa->dev_context_ptrs[slot_id]);
  355. return 1;
  356. fail:
  357. xhci_free_virt_device(xhci, slot_id);
  358. return 0;
  359. }
  360. /* Setup an xHCI virtual device for a Set Address command */
  361. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
  362. {
  363. struct xhci_virt_device *dev;
  364. struct xhci_ep_ctx *ep0_ctx;
  365. struct usb_device *top_dev;
  366. struct xhci_slot_ctx *slot_ctx;
  367. struct xhci_input_control_ctx *ctrl_ctx;
  368. dev = xhci->devs[udev->slot_id];
  369. /* Slot ID 0 is reserved */
  370. if (udev->slot_id == 0 || !dev) {
  371. xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
  372. udev->slot_id);
  373. return -EINVAL;
  374. }
  375. ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
  376. ctrl_ctx = xhci_get_input_control_ctx(xhci, dev->in_ctx);
  377. slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
  378. /* 2) New slot context and endpoint 0 context are valid*/
  379. ctrl_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
  380. /* 3) Only the control endpoint is valid - one endpoint context */
  381. slot_ctx->dev_info |= LAST_CTX(1);
  382. slot_ctx->dev_info |= (u32) udev->route;
  383. switch (udev->speed) {
  384. case USB_SPEED_SUPER:
  385. slot_ctx->dev_info |= (u32) SLOT_SPEED_SS;
  386. break;
  387. case USB_SPEED_HIGH:
  388. slot_ctx->dev_info |= (u32) SLOT_SPEED_HS;
  389. break;
  390. case USB_SPEED_FULL:
  391. slot_ctx->dev_info |= (u32) SLOT_SPEED_FS;
  392. break;
  393. case USB_SPEED_LOW:
  394. slot_ctx->dev_info |= (u32) SLOT_SPEED_LS;
  395. break;
  396. case USB_SPEED_WIRELESS:
  397. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  398. return -EINVAL;
  399. break;
  400. default:
  401. /* Speed was set earlier, this shouldn't happen. */
  402. BUG();
  403. }
  404. /* Find the root hub port this device is under */
  405. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  406. top_dev = top_dev->parent)
  407. /* Found device below root hub */;
  408. slot_ctx->dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
  409. xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
  410. /* Is this a LS/FS device under a HS hub? */
  411. if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
  412. udev->tt) {
  413. slot_ctx->tt_info = udev->tt->hub->slot_id;
  414. slot_ctx->tt_info |= udev->ttport << 8;
  415. if (udev->tt->multi)
  416. slot_ctx->dev_info |= DEV_MTT;
  417. }
  418. xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
  419. xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
  420. /* Step 4 - ring already allocated */
  421. /* Step 5 */
  422. ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
  423. /*
  424. * XXX: Not sure about wireless USB devices.
  425. */
  426. switch (udev->speed) {
  427. case USB_SPEED_SUPER:
  428. ep0_ctx->ep_info2 |= MAX_PACKET(512);
  429. break;
  430. case USB_SPEED_HIGH:
  431. /* USB core guesses at a 64-byte max packet first for FS devices */
  432. case USB_SPEED_FULL:
  433. ep0_ctx->ep_info2 |= MAX_PACKET(64);
  434. break;
  435. case USB_SPEED_LOW:
  436. ep0_ctx->ep_info2 |= MAX_PACKET(8);
  437. break;
  438. case USB_SPEED_WIRELESS:
  439. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  440. return -EINVAL;
  441. break;
  442. default:
  443. /* New speed? */
  444. BUG();
  445. }
  446. /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
  447. ep0_ctx->ep_info2 |= MAX_BURST(0);
  448. ep0_ctx->ep_info2 |= ERROR_COUNT(3);
  449. ep0_ctx->deq =
  450. dev->eps[0].ring->first_seg->dma;
  451. ep0_ctx->deq |= dev->eps[0].ring->cycle_state;
  452. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  453. return 0;
  454. }
  455. /* Return the polling or NAK interval.
  456. *
  457. * The polling interval is expressed in "microframes". If xHCI's Interval field
  458. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  459. *
  460. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  461. * is set to 0.
  462. */
  463. static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  464. struct usb_host_endpoint *ep)
  465. {
  466. unsigned int interval = 0;
  467. switch (udev->speed) {
  468. case USB_SPEED_HIGH:
  469. /* Max NAK rate */
  470. if (usb_endpoint_xfer_control(&ep->desc) ||
  471. usb_endpoint_xfer_bulk(&ep->desc))
  472. interval = ep->desc.bInterval;
  473. /* Fall through - SS and HS isoc/int have same decoding */
  474. case USB_SPEED_SUPER:
  475. if (usb_endpoint_xfer_int(&ep->desc) ||
  476. usb_endpoint_xfer_isoc(&ep->desc)) {
  477. if (ep->desc.bInterval == 0)
  478. interval = 0;
  479. else
  480. interval = ep->desc.bInterval - 1;
  481. if (interval > 15)
  482. interval = 15;
  483. if (interval != ep->desc.bInterval + 1)
  484. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  485. ep->desc.bEndpointAddress, 1 << interval);
  486. }
  487. break;
  488. /* Convert bInterval (in 1-255 frames) to microframes and round down to
  489. * nearest power of 2.
  490. */
  491. case USB_SPEED_FULL:
  492. case USB_SPEED_LOW:
  493. if (usb_endpoint_xfer_int(&ep->desc) ||
  494. usb_endpoint_xfer_isoc(&ep->desc)) {
  495. interval = fls(8*ep->desc.bInterval) - 1;
  496. if (interval > 10)
  497. interval = 10;
  498. if (interval < 3)
  499. interval = 3;
  500. if ((1 << interval) != 8*ep->desc.bInterval)
  501. dev_warn(&udev->dev,
  502. "ep %#x - rounding interval"
  503. " to %d microframes, "
  504. "ep desc says %d microframes\n",
  505. ep->desc.bEndpointAddress,
  506. 1 << interval,
  507. 8*ep->desc.bInterval);
  508. }
  509. break;
  510. default:
  511. BUG();
  512. }
  513. return EP_INTERVAL(interval);
  514. }
  515. /* The "Mult" field in the endpoint context is only set for SuperSpeed devices.
  516. * High speed endpoint descriptors can define "the number of additional
  517. * transaction opportunities per microframe", but that goes in the Max Burst
  518. * endpoint context field.
  519. */
  520. static inline u32 xhci_get_endpoint_mult(struct usb_device *udev,
  521. struct usb_host_endpoint *ep)
  522. {
  523. if (udev->speed != USB_SPEED_SUPER || !ep->ss_ep_comp)
  524. return 0;
  525. return ep->ss_ep_comp->desc.bmAttributes;
  526. }
  527. static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
  528. struct usb_host_endpoint *ep)
  529. {
  530. int in;
  531. u32 type;
  532. in = usb_endpoint_dir_in(&ep->desc);
  533. if (usb_endpoint_xfer_control(&ep->desc)) {
  534. type = EP_TYPE(CTRL_EP);
  535. } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
  536. if (in)
  537. type = EP_TYPE(BULK_IN_EP);
  538. else
  539. type = EP_TYPE(BULK_OUT_EP);
  540. } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
  541. if (in)
  542. type = EP_TYPE(ISOC_IN_EP);
  543. else
  544. type = EP_TYPE(ISOC_OUT_EP);
  545. } else if (usb_endpoint_xfer_int(&ep->desc)) {
  546. if (in)
  547. type = EP_TYPE(INT_IN_EP);
  548. else
  549. type = EP_TYPE(INT_OUT_EP);
  550. } else {
  551. BUG();
  552. }
  553. return type;
  554. }
  555. int xhci_endpoint_init(struct xhci_hcd *xhci,
  556. struct xhci_virt_device *virt_dev,
  557. struct usb_device *udev,
  558. struct usb_host_endpoint *ep,
  559. gfp_t mem_flags)
  560. {
  561. unsigned int ep_index;
  562. struct xhci_ep_ctx *ep_ctx;
  563. struct xhci_ring *ep_ring;
  564. unsigned int max_packet;
  565. unsigned int max_burst;
  566. ep_index = xhci_get_endpoint_index(&ep->desc);
  567. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  568. /* Set up the endpoint ring */
  569. virt_dev->eps[ep_index].new_ring =
  570. xhci_ring_alloc(xhci, 1, true, mem_flags);
  571. if (!virt_dev->eps[ep_index].new_ring) {
  572. /* Attempt to use the ring cache */
  573. if (virt_dev->num_rings_cached == 0)
  574. return -ENOMEM;
  575. virt_dev->eps[ep_index].new_ring =
  576. virt_dev->ring_cache[virt_dev->num_rings_cached];
  577. virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
  578. virt_dev->num_rings_cached--;
  579. xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring);
  580. }
  581. ep_ring = virt_dev->eps[ep_index].new_ring;
  582. ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
  583. ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
  584. ep_ctx->ep_info |= EP_MULT(xhci_get_endpoint_mult(udev, ep));
  585. /* FIXME dig Mult and streams info out of ep companion desc */
  586. /* Allow 3 retries for everything but isoc;
  587. * error count = 0 means infinite retries.
  588. */
  589. if (!usb_endpoint_xfer_isoc(&ep->desc))
  590. ep_ctx->ep_info2 = ERROR_COUNT(3);
  591. else
  592. ep_ctx->ep_info2 = ERROR_COUNT(1);
  593. ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
  594. /* Set the max packet size and max burst */
  595. switch (udev->speed) {
  596. case USB_SPEED_SUPER:
  597. max_packet = ep->desc.wMaxPacketSize;
  598. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  599. /* dig out max burst from ep companion desc */
  600. if (!ep->ss_ep_comp) {
  601. xhci_warn(xhci, "WARN no SS endpoint companion descriptor.\n");
  602. max_packet = 0;
  603. } else {
  604. max_packet = ep->ss_ep_comp->desc.bMaxBurst;
  605. }
  606. ep_ctx->ep_info2 |= MAX_BURST(max_packet);
  607. break;
  608. case USB_SPEED_HIGH:
  609. /* bits 11:12 specify the number of additional transaction
  610. * opportunities per microframe (USB 2.0, section 9.6.6)
  611. */
  612. if (usb_endpoint_xfer_isoc(&ep->desc) ||
  613. usb_endpoint_xfer_int(&ep->desc)) {
  614. max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
  615. ep_ctx->ep_info2 |= MAX_BURST(max_burst);
  616. }
  617. /* Fall through */
  618. case USB_SPEED_FULL:
  619. case USB_SPEED_LOW:
  620. max_packet = ep->desc.wMaxPacketSize & 0x3ff;
  621. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  622. break;
  623. default:
  624. BUG();
  625. }
  626. /* FIXME Debug endpoint context */
  627. return 0;
  628. }
  629. void xhci_endpoint_zero(struct xhci_hcd *xhci,
  630. struct xhci_virt_device *virt_dev,
  631. struct usb_host_endpoint *ep)
  632. {
  633. unsigned int ep_index;
  634. struct xhci_ep_ctx *ep_ctx;
  635. ep_index = xhci_get_endpoint_index(&ep->desc);
  636. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  637. ep_ctx->ep_info = 0;
  638. ep_ctx->ep_info2 = 0;
  639. ep_ctx->deq = 0;
  640. ep_ctx->tx_info = 0;
  641. /* Don't free the endpoint ring until the set interface or configuration
  642. * request succeeds.
  643. */
  644. }
  645. /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
  646. * Useful when you want to change one particular aspect of the endpoint and then
  647. * issue a configure endpoint command.
  648. */
  649. void xhci_endpoint_copy(struct xhci_hcd *xhci,
  650. struct xhci_container_ctx *in_ctx,
  651. struct xhci_container_ctx *out_ctx,
  652. unsigned int ep_index)
  653. {
  654. struct xhci_ep_ctx *out_ep_ctx;
  655. struct xhci_ep_ctx *in_ep_ctx;
  656. out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  657. in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  658. in_ep_ctx->ep_info = out_ep_ctx->ep_info;
  659. in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
  660. in_ep_ctx->deq = out_ep_ctx->deq;
  661. in_ep_ctx->tx_info = out_ep_ctx->tx_info;
  662. }
  663. /* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
  664. * Useful when you want to change one particular aspect of the endpoint and then
  665. * issue a configure endpoint command. Only the context entries field matters,
  666. * but we'll copy the whole thing anyway.
  667. */
  668. void xhci_slot_copy(struct xhci_hcd *xhci,
  669. struct xhci_container_ctx *in_ctx,
  670. struct xhci_container_ctx *out_ctx)
  671. {
  672. struct xhci_slot_ctx *in_slot_ctx;
  673. struct xhci_slot_ctx *out_slot_ctx;
  674. in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  675. out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
  676. in_slot_ctx->dev_info = out_slot_ctx->dev_info;
  677. in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
  678. in_slot_ctx->tt_info = out_slot_ctx->tt_info;
  679. in_slot_ctx->dev_state = out_slot_ctx->dev_state;
  680. }
  681. /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
  682. static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
  683. {
  684. int i;
  685. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  686. int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  687. xhci_dbg(xhci, "Allocating %d scratchpad buffers\n", num_sp);
  688. if (!num_sp)
  689. return 0;
  690. xhci->scratchpad = kzalloc(sizeof(*xhci->scratchpad), flags);
  691. if (!xhci->scratchpad)
  692. goto fail_sp;
  693. xhci->scratchpad->sp_array =
  694. pci_alloc_consistent(to_pci_dev(dev),
  695. num_sp * sizeof(u64),
  696. &xhci->scratchpad->sp_dma);
  697. if (!xhci->scratchpad->sp_array)
  698. goto fail_sp2;
  699. xhci->scratchpad->sp_buffers = kzalloc(sizeof(void *) * num_sp, flags);
  700. if (!xhci->scratchpad->sp_buffers)
  701. goto fail_sp3;
  702. xhci->scratchpad->sp_dma_buffers =
  703. kzalloc(sizeof(dma_addr_t) * num_sp, flags);
  704. if (!xhci->scratchpad->sp_dma_buffers)
  705. goto fail_sp4;
  706. xhci->dcbaa->dev_context_ptrs[0] = xhci->scratchpad->sp_dma;
  707. for (i = 0; i < num_sp; i++) {
  708. dma_addr_t dma;
  709. void *buf = pci_alloc_consistent(to_pci_dev(dev),
  710. xhci->page_size, &dma);
  711. if (!buf)
  712. goto fail_sp5;
  713. xhci->scratchpad->sp_array[i] = dma;
  714. xhci->scratchpad->sp_buffers[i] = buf;
  715. xhci->scratchpad->sp_dma_buffers[i] = dma;
  716. }
  717. return 0;
  718. fail_sp5:
  719. for (i = i - 1; i >= 0; i--) {
  720. pci_free_consistent(to_pci_dev(dev), xhci->page_size,
  721. xhci->scratchpad->sp_buffers[i],
  722. xhci->scratchpad->sp_dma_buffers[i]);
  723. }
  724. kfree(xhci->scratchpad->sp_dma_buffers);
  725. fail_sp4:
  726. kfree(xhci->scratchpad->sp_buffers);
  727. fail_sp3:
  728. pci_free_consistent(to_pci_dev(dev), num_sp * sizeof(u64),
  729. xhci->scratchpad->sp_array,
  730. xhci->scratchpad->sp_dma);
  731. fail_sp2:
  732. kfree(xhci->scratchpad);
  733. xhci->scratchpad = NULL;
  734. fail_sp:
  735. return -ENOMEM;
  736. }
  737. static void scratchpad_free(struct xhci_hcd *xhci)
  738. {
  739. int num_sp;
  740. int i;
  741. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  742. if (!xhci->scratchpad)
  743. return;
  744. num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  745. for (i = 0; i < num_sp; i++) {
  746. pci_free_consistent(pdev, xhci->page_size,
  747. xhci->scratchpad->sp_buffers[i],
  748. xhci->scratchpad->sp_dma_buffers[i]);
  749. }
  750. kfree(xhci->scratchpad->sp_dma_buffers);
  751. kfree(xhci->scratchpad->sp_buffers);
  752. pci_free_consistent(pdev, num_sp * sizeof(u64),
  753. xhci->scratchpad->sp_array,
  754. xhci->scratchpad->sp_dma);
  755. kfree(xhci->scratchpad);
  756. xhci->scratchpad = NULL;
  757. }
  758. struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
  759. bool allocate_in_ctx, bool allocate_completion,
  760. gfp_t mem_flags)
  761. {
  762. struct xhci_command *command;
  763. command = kzalloc(sizeof(*command), mem_flags);
  764. if (!command)
  765. return NULL;
  766. if (allocate_in_ctx) {
  767. command->in_ctx =
  768. xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT,
  769. mem_flags);
  770. if (!command->in_ctx) {
  771. kfree(command);
  772. return NULL;
  773. }
  774. }
  775. if (allocate_completion) {
  776. command->completion =
  777. kzalloc(sizeof(struct completion), mem_flags);
  778. if (!command->completion) {
  779. xhci_free_container_ctx(xhci, command->in_ctx);
  780. kfree(command);
  781. return NULL;
  782. }
  783. init_completion(command->completion);
  784. }
  785. command->status = 0;
  786. INIT_LIST_HEAD(&command->cmd_list);
  787. return command;
  788. }
  789. void xhci_free_command(struct xhci_hcd *xhci,
  790. struct xhci_command *command)
  791. {
  792. xhci_free_container_ctx(xhci,
  793. command->in_ctx);
  794. kfree(command->completion);
  795. kfree(command);
  796. }
  797. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  798. {
  799. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  800. int size;
  801. int i;
  802. /* Free the Event Ring Segment Table and the actual Event Ring */
  803. if (xhci->ir_set) {
  804. xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
  805. xhci_write_64(xhci, 0, &xhci->ir_set->erst_base);
  806. xhci_write_64(xhci, 0, &xhci->ir_set->erst_dequeue);
  807. }
  808. size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
  809. if (xhci->erst.entries)
  810. pci_free_consistent(pdev, size,
  811. xhci->erst.entries, xhci->erst.erst_dma_addr);
  812. xhci->erst.entries = NULL;
  813. xhci_dbg(xhci, "Freed ERST\n");
  814. if (xhci->event_ring)
  815. xhci_ring_free(xhci, xhci->event_ring);
  816. xhci->event_ring = NULL;
  817. xhci_dbg(xhci, "Freed event ring\n");
  818. xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
  819. if (xhci->cmd_ring)
  820. xhci_ring_free(xhci, xhci->cmd_ring);
  821. xhci->cmd_ring = NULL;
  822. xhci_dbg(xhci, "Freed command ring\n");
  823. for (i = 1; i < MAX_HC_SLOTS; ++i)
  824. xhci_free_virt_device(xhci, i);
  825. if (xhci->segment_pool)
  826. dma_pool_destroy(xhci->segment_pool);
  827. xhci->segment_pool = NULL;
  828. xhci_dbg(xhci, "Freed segment pool\n");
  829. if (xhci->device_pool)
  830. dma_pool_destroy(xhci->device_pool);
  831. xhci->device_pool = NULL;
  832. xhci_dbg(xhci, "Freed device context pool\n");
  833. xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
  834. if (xhci->dcbaa)
  835. pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
  836. xhci->dcbaa, xhci->dcbaa->dma);
  837. xhci->dcbaa = NULL;
  838. scratchpad_free(xhci);
  839. xhci->page_size = 0;
  840. xhci->page_shift = 0;
  841. }
  842. static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
  843. struct xhci_segment *input_seg,
  844. union xhci_trb *start_trb,
  845. union xhci_trb *end_trb,
  846. dma_addr_t input_dma,
  847. struct xhci_segment *result_seg,
  848. char *test_name, int test_number)
  849. {
  850. unsigned long long start_dma;
  851. unsigned long long end_dma;
  852. struct xhci_segment *seg;
  853. start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
  854. end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
  855. seg = trb_in_td(input_seg, start_trb, end_trb, input_dma);
  856. if (seg != result_seg) {
  857. xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
  858. test_name, test_number);
  859. xhci_warn(xhci, "Tested TRB math w/ seg %p and "
  860. "input DMA 0x%llx\n",
  861. input_seg,
  862. (unsigned long long) input_dma);
  863. xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
  864. "ending TRB %p (0x%llx DMA)\n",
  865. start_trb, start_dma,
  866. end_trb, end_dma);
  867. xhci_warn(xhci, "Expected seg %p, got seg %p\n",
  868. result_seg, seg);
  869. return -1;
  870. }
  871. return 0;
  872. }
  873. /* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
  874. static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
  875. {
  876. struct {
  877. dma_addr_t input_dma;
  878. struct xhci_segment *result_seg;
  879. } simple_test_vector [] = {
  880. /* A zeroed DMA field should fail */
  881. { 0, NULL },
  882. /* One TRB before the ring start should fail */
  883. { xhci->event_ring->first_seg->dma - 16, NULL },
  884. /* One byte before the ring start should fail */
  885. { xhci->event_ring->first_seg->dma - 1, NULL },
  886. /* Starting TRB should succeed */
  887. { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
  888. /* Ending TRB should succeed */
  889. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
  890. xhci->event_ring->first_seg },
  891. /* One byte after the ring end should fail */
  892. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
  893. /* One TRB after the ring end should fail */
  894. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
  895. /* An address of all ones should fail */
  896. { (dma_addr_t) (~0), NULL },
  897. };
  898. struct {
  899. struct xhci_segment *input_seg;
  900. union xhci_trb *start_trb;
  901. union xhci_trb *end_trb;
  902. dma_addr_t input_dma;
  903. struct xhci_segment *result_seg;
  904. } complex_test_vector [] = {
  905. /* Test feeding a valid DMA address from a different ring */
  906. { .input_seg = xhci->event_ring->first_seg,
  907. .start_trb = xhci->event_ring->first_seg->trbs,
  908. .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  909. .input_dma = xhci->cmd_ring->first_seg->dma,
  910. .result_seg = NULL,
  911. },
  912. /* Test feeding a valid end TRB from a different ring */
  913. { .input_seg = xhci->event_ring->first_seg,
  914. .start_trb = xhci->event_ring->first_seg->trbs,
  915. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  916. .input_dma = xhci->cmd_ring->first_seg->dma,
  917. .result_seg = NULL,
  918. },
  919. /* Test feeding a valid start and end TRB from a different ring */
  920. { .input_seg = xhci->event_ring->first_seg,
  921. .start_trb = xhci->cmd_ring->first_seg->trbs,
  922. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  923. .input_dma = xhci->cmd_ring->first_seg->dma,
  924. .result_seg = NULL,
  925. },
  926. /* TRB in this ring, but after this TD */
  927. { .input_seg = xhci->event_ring->first_seg,
  928. .start_trb = &xhci->event_ring->first_seg->trbs[0],
  929. .end_trb = &xhci->event_ring->first_seg->trbs[3],
  930. .input_dma = xhci->event_ring->first_seg->dma + 4*16,
  931. .result_seg = NULL,
  932. },
  933. /* TRB in this ring, but before this TD */
  934. { .input_seg = xhci->event_ring->first_seg,
  935. .start_trb = &xhci->event_ring->first_seg->trbs[3],
  936. .end_trb = &xhci->event_ring->first_seg->trbs[6],
  937. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  938. .result_seg = NULL,
  939. },
  940. /* TRB in this ring, but after this wrapped TD */
  941. { .input_seg = xhci->event_ring->first_seg,
  942. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  943. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  944. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  945. .result_seg = NULL,
  946. },
  947. /* TRB in this ring, but before this wrapped TD */
  948. { .input_seg = xhci->event_ring->first_seg,
  949. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  950. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  951. .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
  952. .result_seg = NULL,
  953. },
  954. /* TRB not in this ring, and we have a wrapped TD */
  955. { .input_seg = xhci->event_ring->first_seg,
  956. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  957. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  958. .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
  959. .result_seg = NULL,
  960. },
  961. };
  962. unsigned int num_tests;
  963. int i, ret;
  964. num_tests = sizeof(simple_test_vector) / sizeof(simple_test_vector[0]);
  965. for (i = 0; i < num_tests; i++) {
  966. ret = xhci_test_trb_in_td(xhci,
  967. xhci->event_ring->first_seg,
  968. xhci->event_ring->first_seg->trbs,
  969. &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  970. simple_test_vector[i].input_dma,
  971. simple_test_vector[i].result_seg,
  972. "Simple", i);
  973. if (ret < 0)
  974. return ret;
  975. }
  976. num_tests = sizeof(complex_test_vector) / sizeof(complex_test_vector[0]);
  977. for (i = 0; i < num_tests; i++) {
  978. ret = xhci_test_trb_in_td(xhci,
  979. complex_test_vector[i].input_seg,
  980. complex_test_vector[i].start_trb,
  981. complex_test_vector[i].end_trb,
  982. complex_test_vector[i].input_dma,
  983. complex_test_vector[i].result_seg,
  984. "Complex", i);
  985. if (ret < 0)
  986. return ret;
  987. }
  988. xhci_dbg(xhci, "TRB math tests passed.\n");
  989. return 0;
  990. }
  991. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  992. {
  993. dma_addr_t dma;
  994. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  995. unsigned int val, val2;
  996. u64 val_64;
  997. struct xhci_segment *seg;
  998. u32 page_size;
  999. int i;
  1000. page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
  1001. xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
  1002. for (i = 0; i < 16; i++) {
  1003. if ((0x1 & page_size) != 0)
  1004. break;
  1005. page_size = page_size >> 1;
  1006. }
  1007. if (i < 16)
  1008. xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
  1009. else
  1010. xhci_warn(xhci, "WARN: no supported page size\n");
  1011. /* Use 4K pages, since that's common and the minimum the HC supports */
  1012. xhci->page_shift = 12;
  1013. xhci->page_size = 1 << xhci->page_shift;
  1014. xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
  1015. /*
  1016. * Program the Number of Device Slots Enabled field in the CONFIG
  1017. * register with the max value of slots the HC can handle.
  1018. */
  1019. val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
  1020. xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
  1021. (unsigned int) val);
  1022. val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
  1023. val |= (val2 & ~HCS_SLOTS_MASK);
  1024. xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
  1025. (unsigned int) val);
  1026. xhci_writel(xhci, val, &xhci->op_regs->config_reg);
  1027. /*
  1028. * Section 5.4.8 - doorbell array must be
  1029. * "physically contiguous and 64-byte (cache line) aligned".
  1030. */
  1031. xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
  1032. sizeof(*xhci->dcbaa), &dma);
  1033. if (!xhci->dcbaa)
  1034. goto fail;
  1035. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  1036. xhci->dcbaa->dma = dma;
  1037. xhci_dbg(xhci, "// Device context base array address = 0x%llx (DMA), %p (virt)\n",
  1038. (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
  1039. xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
  1040. /*
  1041. * Initialize the ring segment pool. The ring must be a contiguous
  1042. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  1043. * however, the command ring segment needs 64-byte aligned segments,
  1044. * so we pick the greater alignment need.
  1045. */
  1046. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  1047. SEGMENT_SIZE, 64, xhci->page_size);
  1048. /* See Table 46 and Note on Figure 55 */
  1049. xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
  1050. 2112, 64, xhci->page_size);
  1051. if (!xhci->segment_pool || !xhci->device_pool)
  1052. goto fail;
  1053. /* Set up the command ring to have one segments for now. */
  1054. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
  1055. if (!xhci->cmd_ring)
  1056. goto fail;
  1057. xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
  1058. xhci_dbg(xhci, "First segment DMA is 0x%llx\n",
  1059. (unsigned long long)xhci->cmd_ring->first_seg->dma);
  1060. /* Set the address in the Command Ring Control register */
  1061. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  1062. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  1063. (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
  1064. xhci->cmd_ring->cycle_state;
  1065. xhci_dbg(xhci, "// Setting command ring address to 0x%x\n", val);
  1066. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  1067. xhci_dbg_cmd_ptrs(xhci);
  1068. val = xhci_readl(xhci, &xhci->cap_regs->db_off);
  1069. val &= DBOFF_MASK;
  1070. xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
  1071. " from cap regs base addr\n", val);
  1072. xhci->dba = (void *) xhci->cap_regs + val;
  1073. xhci_dbg_regs(xhci);
  1074. xhci_print_run_regs(xhci);
  1075. /* Set ir_set to interrupt register set 0 */
  1076. xhci->ir_set = (void *) xhci->run_regs->ir_set;
  1077. /*
  1078. * Event ring setup: Allocate a normal ring, but also setup
  1079. * the event ring segment table (ERST). Section 4.9.3.
  1080. */
  1081. xhci_dbg(xhci, "// Allocating event ring\n");
  1082. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
  1083. if (!xhci->event_ring)
  1084. goto fail;
  1085. if (xhci_check_trb_in_td_math(xhci, flags) < 0)
  1086. goto fail;
  1087. xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
  1088. sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
  1089. if (!xhci->erst.entries)
  1090. goto fail;
  1091. xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
  1092. (unsigned long long)dma);
  1093. memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
  1094. xhci->erst.num_entries = ERST_NUM_SEGS;
  1095. xhci->erst.erst_dma_addr = dma;
  1096. xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = %p, dma addr = 0x%llx\n",
  1097. xhci->erst.num_entries,
  1098. xhci->erst.entries,
  1099. (unsigned long long)xhci->erst.erst_dma_addr);
  1100. /* set ring base address and size for each segment table entry */
  1101. for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
  1102. struct xhci_erst_entry *entry = &xhci->erst.entries[val];
  1103. entry->seg_addr = seg->dma;
  1104. entry->seg_size = TRBS_PER_SEGMENT;
  1105. entry->rsvd = 0;
  1106. seg = seg->next;
  1107. }
  1108. /* set ERST count with the number of entries in the segment table */
  1109. val = xhci_readl(xhci, &xhci->ir_set->erst_size);
  1110. val &= ERST_SIZE_MASK;
  1111. val |= ERST_NUM_SEGS;
  1112. xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
  1113. val);
  1114. xhci_writel(xhci, val, &xhci->ir_set->erst_size);
  1115. xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
  1116. /* set the segment table base address */
  1117. xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%llx\n",
  1118. (unsigned long long)xhci->erst.erst_dma_addr);
  1119. val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  1120. val_64 &= ERST_PTR_MASK;
  1121. val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
  1122. xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
  1123. /* Set the event ring dequeue address */
  1124. xhci_set_hc_event_deq(xhci);
  1125. xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
  1126. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  1127. /*
  1128. * XXX: Might need to set the Interrupter Moderation Register to
  1129. * something other than the default (~1ms minimum between interrupts).
  1130. * See section 5.5.1.2.
  1131. */
  1132. init_completion(&xhci->addr_dev);
  1133. for (i = 0; i < MAX_HC_SLOTS; ++i)
  1134. xhci->devs[i] = 0;
  1135. if (scratchpad_alloc(xhci, flags))
  1136. goto fail;
  1137. return 0;
  1138. fail:
  1139. xhci_warn(xhci, "Couldn't initialize memory\n");
  1140. xhci_mem_cleanup(xhci);
  1141. return -ENOMEM;
  1142. }