xhci-mem.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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/dmapool.h>
  25. #include "xhci.h"
  26. /*
  27. * Allocates a generic ring segment from the ring pool, sets the dma address,
  28. * initializes the segment to zero, and sets the private next pointer to NULL.
  29. *
  30. * Section 4.11.1.1:
  31. * "All components of all Command and Transfer TRBs shall be initialized to '0'"
  32. */
  33. static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, gfp_t flags)
  34. {
  35. struct xhci_segment *seg;
  36. dma_addr_t dma;
  37. seg = kzalloc(sizeof *seg, flags);
  38. if (!seg)
  39. return 0;
  40. xhci_dbg(xhci, "Allocating priv segment structure at %p\n", seg);
  41. seg->trbs = dma_pool_alloc(xhci->segment_pool, flags, &dma);
  42. if (!seg->trbs) {
  43. kfree(seg);
  44. return 0;
  45. }
  46. xhci_dbg(xhci, "// Allocating segment at %p (virtual) 0x%llx (DMA)\n",
  47. seg->trbs, (unsigned long long)dma);
  48. memset(seg->trbs, 0, SEGMENT_SIZE);
  49. seg->dma = dma;
  50. seg->next = NULL;
  51. return seg;
  52. }
  53. static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
  54. {
  55. if (!seg)
  56. return;
  57. if (seg->trbs) {
  58. xhci_dbg(xhci, "Freeing DMA segment at %p (virtual) 0x%llx (DMA)\n",
  59. seg->trbs, (unsigned long long)seg->dma);
  60. dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
  61. seg->trbs = NULL;
  62. }
  63. xhci_dbg(xhci, "Freeing priv segment structure at %p\n", seg);
  64. kfree(seg);
  65. }
  66. /*
  67. * Make the prev segment point to the next segment.
  68. *
  69. * Change the last TRB in the prev segment to be a Link TRB which points to the
  70. * DMA address of the next segment. The caller needs to set any Link TRB
  71. * related flags, such as End TRB, Toggle Cycle, and no snoop.
  72. */
  73. static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
  74. struct xhci_segment *next, bool link_trbs)
  75. {
  76. u32 val;
  77. if (!prev || !next)
  78. return;
  79. prev->next = next;
  80. if (link_trbs) {
  81. prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = next->dma;
  82. /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
  83. val = prev->trbs[TRBS_PER_SEGMENT-1].link.control;
  84. val &= ~TRB_TYPE_BITMASK;
  85. val |= TRB_TYPE(TRB_LINK);
  86. /* Always set the chain bit with 0.95 hardware */
  87. if (xhci_link_trb_quirk(xhci))
  88. val |= TRB_CHAIN;
  89. prev->trbs[TRBS_PER_SEGMENT-1].link.control = val;
  90. }
  91. xhci_dbg(xhci, "Linking segment 0x%llx to segment 0x%llx (DMA)\n",
  92. (unsigned long long)prev->dma,
  93. (unsigned long long)next->dma);
  94. }
  95. /* XXX: Do we need the hcd structure in all these functions? */
  96. void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
  97. {
  98. struct xhci_segment *seg;
  99. struct xhci_segment *first_seg;
  100. if (!ring || !ring->first_seg)
  101. return;
  102. first_seg = ring->first_seg;
  103. seg = first_seg->next;
  104. xhci_dbg(xhci, "Freeing ring at %p\n", ring);
  105. while (seg != first_seg) {
  106. struct xhci_segment *next = seg->next;
  107. xhci_segment_free(xhci, seg);
  108. seg = next;
  109. }
  110. xhci_segment_free(xhci, first_seg);
  111. ring->first_seg = NULL;
  112. kfree(ring);
  113. }
  114. /**
  115. * Create a new ring with zero or more segments.
  116. *
  117. * Link each segment together into a ring.
  118. * Set the end flag and the cycle toggle bit on the last segment.
  119. * See section 4.9.1 and figures 15 and 16.
  120. */
  121. static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
  122. unsigned int num_segs, bool link_trbs, gfp_t flags)
  123. {
  124. struct xhci_ring *ring;
  125. struct xhci_segment *prev;
  126. ring = kzalloc(sizeof *(ring), flags);
  127. xhci_dbg(xhci, "Allocating ring at %p\n", ring);
  128. if (!ring)
  129. return 0;
  130. INIT_LIST_HEAD(&ring->td_list);
  131. if (num_segs == 0)
  132. return ring;
  133. ring->first_seg = xhci_segment_alloc(xhci, flags);
  134. if (!ring->first_seg)
  135. goto fail;
  136. num_segs--;
  137. prev = ring->first_seg;
  138. while (num_segs > 0) {
  139. struct xhci_segment *next;
  140. next = xhci_segment_alloc(xhci, flags);
  141. if (!next)
  142. goto fail;
  143. xhci_link_segments(xhci, prev, next, link_trbs);
  144. prev = next;
  145. num_segs--;
  146. }
  147. xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
  148. if (link_trbs) {
  149. /* See section 4.9.2.1 and 6.4.4.1 */
  150. prev->trbs[TRBS_PER_SEGMENT-1].link.control |= (LINK_TOGGLE);
  151. xhci_dbg(xhci, "Wrote link toggle flag to"
  152. " segment %p (virtual), 0x%llx (DMA)\n",
  153. prev, (unsigned long long)prev->dma);
  154. }
  155. /* The ring is empty, so the enqueue pointer == dequeue pointer */
  156. ring->enqueue = ring->first_seg->trbs;
  157. ring->enq_seg = ring->first_seg;
  158. ring->dequeue = ring->enqueue;
  159. ring->deq_seg = ring->first_seg;
  160. /* The ring is initialized to 0. The producer must write 1 to the cycle
  161. * bit to handover ownership of the TRB, so PCS = 1. The consumer must
  162. * compare CCS to the cycle bit to check ownership, so CCS = 1.
  163. */
  164. ring->cycle_state = 1;
  165. return ring;
  166. fail:
  167. xhci_ring_free(xhci, ring);
  168. return 0;
  169. }
  170. #define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32)
  171. struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
  172. int type, gfp_t flags)
  173. {
  174. struct xhci_container_ctx *ctx = kzalloc(sizeof(*ctx), flags);
  175. if (!ctx)
  176. return NULL;
  177. BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
  178. ctx->type = type;
  179. ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024;
  180. if (type == XHCI_CTX_TYPE_INPUT)
  181. ctx->size += CTX_SIZE(xhci->hcc_params);
  182. ctx->bytes = dma_pool_alloc(xhci->device_pool, flags, &ctx->dma);
  183. memset(ctx->bytes, 0, ctx->size);
  184. return ctx;
  185. }
  186. void xhci_free_container_ctx(struct xhci_hcd *xhci,
  187. struct xhci_container_ctx *ctx)
  188. {
  189. dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma);
  190. kfree(ctx);
  191. }
  192. struct xhci_input_control_ctx *xhci_get_input_control_ctx(struct xhci_hcd *xhci,
  193. struct xhci_container_ctx *ctx)
  194. {
  195. BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
  196. return (struct xhci_input_control_ctx *)ctx->bytes;
  197. }
  198. struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci,
  199. struct xhci_container_ctx *ctx)
  200. {
  201. if (ctx->type == XHCI_CTX_TYPE_DEVICE)
  202. return (struct xhci_slot_ctx *)ctx->bytes;
  203. return (struct xhci_slot_ctx *)
  204. (ctx->bytes + CTX_SIZE(xhci->hcc_params));
  205. }
  206. struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
  207. struct xhci_container_ctx *ctx,
  208. unsigned int ep_index)
  209. {
  210. /* increment ep index by offset of start of ep ctx array */
  211. ep_index++;
  212. if (ctx->type == XHCI_CTX_TYPE_INPUT)
  213. ep_index++;
  214. return (struct xhci_ep_ctx *)
  215. (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
  216. }
  217. static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
  218. struct xhci_virt_ep *ep)
  219. {
  220. init_timer(&ep->stop_cmd_timer);
  221. ep->stop_cmd_timer.data = (unsigned long) ep;
  222. ep->stop_cmd_timer.function = xhci_stop_endpoint_command_watchdog;
  223. ep->xhci = xhci;
  224. }
  225. /* All the xhci_tds in the ring's TD list should be freed at this point */
  226. void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
  227. {
  228. struct xhci_virt_device *dev;
  229. int i;
  230. /* Slot ID 0 is reserved */
  231. if (slot_id == 0 || !xhci->devs[slot_id])
  232. return;
  233. dev = xhci->devs[slot_id];
  234. xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
  235. if (!dev)
  236. return;
  237. for (i = 0; i < 31; ++i)
  238. if (dev->eps[i].ring)
  239. xhci_ring_free(xhci, dev->eps[i].ring);
  240. if (dev->in_ctx)
  241. xhci_free_container_ctx(xhci, dev->in_ctx);
  242. if (dev->out_ctx)
  243. xhci_free_container_ctx(xhci, dev->out_ctx);
  244. kfree(xhci->devs[slot_id]);
  245. xhci->devs[slot_id] = 0;
  246. }
  247. int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
  248. struct usb_device *udev, gfp_t flags)
  249. {
  250. struct xhci_virt_device *dev;
  251. int i;
  252. /* Slot ID 0 is reserved */
  253. if (slot_id == 0 || xhci->devs[slot_id]) {
  254. xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
  255. return 0;
  256. }
  257. xhci->devs[slot_id] = kzalloc(sizeof(*xhci->devs[slot_id]), flags);
  258. if (!xhci->devs[slot_id])
  259. return 0;
  260. dev = xhci->devs[slot_id];
  261. /* Allocate the (output) device context that will be used in the HC. */
  262. dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
  263. if (!dev->out_ctx)
  264. goto fail;
  265. xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
  266. (unsigned long long)dev->out_ctx->dma);
  267. /* Allocate the (input) device context for address device command */
  268. dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags);
  269. if (!dev->in_ctx)
  270. goto fail;
  271. xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
  272. (unsigned long long)dev->in_ctx->dma);
  273. /* Initialize the cancellation list and watchdog timers for each ep */
  274. for (i = 0; i < 31; i++) {
  275. xhci_init_endpoint_timer(xhci, &dev->eps[i]);
  276. INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
  277. }
  278. /* Allocate endpoint 0 ring */
  279. dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags);
  280. if (!dev->eps[0].ring)
  281. goto fail;
  282. init_completion(&dev->cmd_completion);
  283. INIT_LIST_HEAD(&dev->cmd_list);
  284. /* Point to output device context in dcbaa. */
  285. xhci->dcbaa->dev_context_ptrs[slot_id] = dev->out_ctx->dma;
  286. xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
  287. slot_id,
  288. &xhci->dcbaa->dev_context_ptrs[slot_id],
  289. (unsigned long long) xhci->dcbaa->dev_context_ptrs[slot_id]);
  290. return 1;
  291. fail:
  292. xhci_free_virt_device(xhci, slot_id);
  293. return 0;
  294. }
  295. /* Setup an xHCI virtual device for a Set Address command */
  296. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
  297. {
  298. struct xhci_virt_device *dev;
  299. struct xhci_ep_ctx *ep0_ctx;
  300. struct usb_device *top_dev;
  301. struct xhci_slot_ctx *slot_ctx;
  302. struct xhci_input_control_ctx *ctrl_ctx;
  303. dev = xhci->devs[udev->slot_id];
  304. /* Slot ID 0 is reserved */
  305. if (udev->slot_id == 0 || !dev) {
  306. xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
  307. udev->slot_id);
  308. return -EINVAL;
  309. }
  310. ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
  311. ctrl_ctx = xhci_get_input_control_ctx(xhci, dev->in_ctx);
  312. slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
  313. /* 2) New slot context and endpoint 0 context are valid*/
  314. ctrl_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
  315. /* 3) Only the control endpoint is valid - one endpoint context */
  316. slot_ctx->dev_info |= LAST_CTX(1);
  317. slot_ctx->dev_info |= (u32) udev->route;
  318. switch (udev->speed) {
  319. case USB_SPEED_SUPER:
  320. slot_ctx->dev_info |= (u32) SLOT_SPEED_SS;
  321. break;
  322. case USB_SPEED_HIGH:
  323. slot_ctx->dev_info |= (u32) SLOT_SPEED_HS;
  324. break;
  325. case USB_SPEED_FULL:
  326. slot_ctx->dev_info |= (u32) SLOT_SPEED_FS;
  327. break;
  328. case USB_SPEED_LOW:
  329. slot_ctx->dev_info |= (u32) SLOT_SPEED_LS;
  330. break;
  331. case USB_SPEED_VARIABLE:
  332. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  333. return -EINVAL;
  334. break;
  335. default:
  336. /* Speed was set earlier, this shouldn't happen. */
  337. BUG();
  338. }
  339. /* Find the root hub port this device is under */
  340. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  341. top_dev = top_dev->parent)
  342. /* Found device below root hub */;
  343. slot_ctx->dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
  344. xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
  345. /* Is this a LS/FS device under a HS hub? */
  346. if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
  347. udev->tt) {
  348. slot_ctx->tt_info = udev->tt->hub->slot_id;
  349. slot_ctx->tt_info |= udev->ttport << 8;
  350. if (udev->tt->multi)
  351. slot_ctx->dev_info |= DEV_MTT;
  352. }
  353. xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
  354. xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
  355. /* Step 4 - ring already allocated */
  356. /* Step 5 */
  357. ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
  358. /*
  359. * XXX: Not sure about wireless USB devices.
  360. */
  361. switch (udev->speed) {
  362. case USB_SPEED_SUPER:
  363. ep0_ctx->ep_info2 |= MAX_PACKET(512);
  364. break;
  365. case USB_SPEED_HIGH:
  366. /* USB core guesses at a 64-byte max packet first for FS devices */
  367. case USB_SPEED_FULL:
  368. ep0_ctx->ep_info2 |= MAX_PACKET(64);
  369. break;
  370. case USB_SPEED_LOW:
  371. ep0_ctx->ep_info2 |= MAX_PACKET(8);
  372. break;
  373. case USB_SPEED_VARIABLE:
  374. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  375. return -EINVAL;
  376. break;
  377. default:
  378. /* New speed? */
  379. BUG();
  380. }
  381. /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
  382. ep0_ctx->ep_info2 |= MAX_BURST(0);
  383. ep0_ctx->ep_info2 |= ERROR_COUNT(3);
  384. ep0_ctx->deq =
  385. dev->eps[0].ring->first_seg->dma;
  386. ep0_ctx->deq |= dev->eps[0].ring->cycle_state;
  387. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  388. return 0;
  389. }
  390. /* Return the polling or NAK interval.
  391. *
  392. * The polling interval is expressed in "microframes". If xHCI's Interval field
  393. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  394. *
  395. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  396. * is set to 0.
  397. */
  398. static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  399. struct usb_host_endpoint *ep)
  400. {
  401. unsigned int interval = 0;
  402. switch (udev->speed) {
  403. case USB_SPEED_HIGH:
  404. /* Max NAK rate */
  405. if (usb_endpoint_xfer_control(&ep->desc) ||
  406. usb_endpoint_xfer_bulk(&ep->desc))
  407. interval = ep->desc.bInterval;
  408. /* Fall through - SS and HS isoc/int have same decoding */
  409. case USB_SPEED_SUPER:
  410. if (usb_endpoint_xfer_int(&ep->desc) ||
  411. usb_endpoint_xfer_isoc(&ep->desc)) {
  412. if (ep->desc.bInterval == 0)
  413. interval = 0;
  414. else
  415. interval = ep->desc.bInterval - 1;
  416. if (interval > 15)
  417. interval = 15;
  418. if (interval != ep->desc.bInterval + 1)
  419. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  420. ep->desc.bEndpointAddress, 1 << interval);
  421. }
  422. break;
  423. /* Convert bInterval (in 1-255 frames) to microframes and round down to
  424. * nearest power of 2.
  425. */
  426. case USB_SPEED_FULL:
  427. case USB_SPEED_LOW:
  428. if (usb_endpoint_xfer_int(&ep->desc) ||
  429. usb_endpoint_xfer_isoc(&ep->desc)) {
  430. interval = fls(8*ep->desc.bInterval) - 1;
  431. if (interval > 10)
  432. interval = 10;
  433. if (interval < 3)
  434. interval = 3;
  435. if ((1 << interval) != 8*ep->desc.bInterval)
  436. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  437. ep->desc.bEndpointAddress, 1 << interval);
  438. }
  439. break;
  440. default:
  441. BUG();
  442. }
  443. return EP_INTERVAL(interval);
  444. }
  445. static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
  446. struct usb_host_endpoint *ep)
  447. {
  448. int in;
  449. u32 type;
  450. in = usb_endpoint_dir_in(&ep->desc);
  451. if (usb_endpoint_xfer_control(&ep->desc)) {
  452. type = EP_TYPE(CTRL_EP);
  453. } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
  454. if (in)
  455. type = EP_TYPE(BULK_IN_EP);
  456. else
  457. type = EP_TYPE(BULK_OUT_EP);
  458. } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
  459. if (in)
  460. type = EP_TYPE(ISOC_IN_EP);
  461. else
  462. type = EP_TYPE(ISOC_OUT_EP);
  463. } else if (usb_endpoint_xfer_int(&ep->desc)) {
  464. if (in)
  465. type = EP_TYPE(INT_IN_EP);
  466. else
  467. type = EP_TYPE(INT_OUT_EP);
  468. } else {
  469. BUG();
  470. }
  471. return type;
  472. }
  473. int xhci_endpoint_init(struct xhci_hcd *xhci,
  474. struct xhci_virt_device *virt_dev,
  475. struct usb_device *udev,
  476. struct usb_host_endpoint *ep,
  477. gfp_t mem_flags)
  478. {
  479. unsigned int ep_index;
  480. struct xhci_ep_ctx *ep_ctx;
  481. struct xhci_ring *ep_ring;
  482. unsigned int max_packet;
  483. unsigned int max_burst;
  484. ep_index = xhci_get_endpoint_index(&ep->desc);
  485. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  486. /* Set up the endpoint ring */
  487. virt_dev->eps[ep_index].new_ring =
  488. xhci_ring_alloc(xhci, 1, true, mem_flags);
  489. if (!virt_dev->eps[ep_index].new_ring)
  490. return -ENOMEM;
  491. ep_ring = virt_dev->eps[ep_index].new_ring;
  492. ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
  493. ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
  494. /* FIXME dig Mult and streams info out of ep companion desc */
  495. /* Allow 3 retries for everything but isoc;
  496. * error count = 0 means infinite retries.
  497. */
  498. if (!usb_endpoint_xfer_isoc(&ep->desc))
  499. ep_ctx->ep_info2 = ERROR_COUNT(3);
  500. else
  501. ep_ctx->ep_info2 = ERROR_COUNT(1);
  502. ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
  503. /* Set the max packet size and max burst */
  504. switch (udev->speed) {
  505. case USB_SPEED_SUPER:
  506. max_packet = ep->desc.wMaxPacketSize;
  507. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  508. /* dig out max burst from ep companion desc */
  509. if (!ep->ss_ep_comp) {
  510. xhci_warn(xhci, "WARN no SS endpoint companion descriptor.\n");
  511. max_packet = 0;
  512. } else {
  513. max_packet = ep->ss_ep_comp->desc.bMaxBurst;
  514. }
  515. ep_ctx->ep_info2 |= MAX_BURST(max_packet);
  516. break;
  517. case USB_SPEED_HIGH:
  518. /* bits 11:12 specify the number of additional transaction
  519. * opportunities per microframe (USB 2.0, section 9.6.6)
  520. */
  521. if (usb_endpoint_xfer_isoc(&ep->desc) ||
  522. usb_endpoint_xfer_int(&ep->desc)) {
  523. max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
  524. ep_ctx->ep_info2 |= MAX_BURST(max_burst);
  525. }
  526. /* Fall through */
  527. case USB_SPEED_FULL:
  528. case USB_SPEED_LOW:
  529. max_packet = ep->desc.wMaxPacketSize & 0x3ff;
  530. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  531. break;
  532. default:
  533. BUG();
  534. }
  535. /* FIXME Debug endpoint context */
  536. return 0;
  537. }
  538. void xhci_endpoint_zero(struct xhci_hcd *xhci,
  539. struct xhci_virt_device *virt_dev,
  540. struct usb_host_endpoint *ep)
  541. {
  542. unsigned int ep_index;
  543. struct xhci_ep_ctx *ep_ctx;
  544. ep_index = xhci_get_endpoint_index(&ep->desc);
  545. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  546. ep_ctx->ep_info = 0;
  547. ep_ctx->ep_info2 = 0;
  548. ep_ctx->deq = 0;
  549. ep_ctx->tx_info = 0;
  550. /* Don't free the endpoint ring until the set interface or configuration
  551. * request succeeds.
  552. */
  553. }
  554. /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
  555. * Useful when you want to change one particular aspect of the endpoint and then
  556. * issue a configure endpoint command.
  557. */
  558. void xhci_endpoint_copy(struct xhci_hcd *xhci,
  559. struct xhci_container_ctx *in_ctx,
  560. struct xhci_container_ctx *out_ctx,
  561. unsigned int ep_index)
  562. {
  563. struct xhci_ep_ctx *out_ep_ctx;
  564. struct xhci_ep_ctx *in_ep_ctx;
  565. out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  566. in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  567. in_ep_ctx->ep_info = out_ep_ctx->ep_info;
  568. in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
  569. in_ep_ctx->deq = out_ep_ctx->deq;
  570. in_ep_ctx->tx_info = out_ep_ctx->tx_info;
  571. }
  572. /* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
  573. * Useful when you want to change one particular aspect of the endpoint and then
  574. * issue a configure endpoint command. Only the context entries field matters,
  575. * but we'll copy the whole thing anyway.
  576. */
  577. void xhci_slot_copy(struct xhci_hcd *xhci,
  578. struct xhci_container_ctx *in_ctx,
  579. struct xhci_container_ctx *out_ctx)
  580. {
  581. struct xhci_slot_ctx *in_slot_ctx;
  582. struct xhci_slot_ctx *out_slot_ctx;
  583. in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  584. out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
  585. in_slot_ctx->dev_info = out_slot_ctx->dev_info;
  586. in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
  587. in_slot_ctx->tt_info = out_slot_ctx->tt_info;
  588. in_slot_ctx->dev_state = out_slot_ctx->dev_state;
  589. }
  590. /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
  591. static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
  592. {
  593. int i;
  594. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  595. int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  596. xhci_dbg(xhci, "Allocating %d scratchpad buffers\n", num_sp);
  597. if (!num_sp)
  598. return 0;
  599. xhci->scratchpad = kzalloc(sizeof(*xhci->scratchpad), flags);
  600. if (!xhci->scratchpad)
  601. goto fail_sp;
  602. xhci->scratchpad->sp_array =
  603. pci_alloc_consistent(to_pci_dev(dev),
  604. num_sp * sizeof(u64),
  605. &xhci->scratchpad->sp_dma);
  606. if (!xhci->scratchpad->sp_array)
  607. goto fail_sp2;
  608. xhci->scratchpad->sp_buffers = kzalloc(sizeof(void *) * num_sp, flags);
  609. if (!xhci->scratchpad->sp_buffers)
  610. goto fail_sp3;
  611. xhci->scratchpad->sp_dma_buffers =
  612. kzalloc(sizeof(dma_addr_t) * num_sp, flags);
  613. if (!xhci->scratchpad->sp_dma_buffers)
  614. goto fail_sp4;
  615. xhci->dcbaa->dev_context_ptrs[0] = xhci->scratchpad->sp_dma;
  616. for (i = 0; i < num_sp; i++) {
  617. dma_addr_t dma;
  618. void *buf = pci_alloc_consistent(to_pci_dev(dev),
  619. xhci->page_size, &dma);
  620. if (!buf)
  621. goto fail_sp5;
  622. xhci->scratchpad->sp_array[i] = dma;
  623. xhci->scratchpad->sp_buffers[i] = buf;
  624. xhci->scratchpad->sp_dma_buffers[i] = dma;
  625. }
  626. return 0;
  627. fail_sp5:
  628. for (i = i - 1; i >= 0; i--) {
  629. pci_free_consistent(to_pci_dev(dev), xhci->page_size,
  630. xhci->scratchpad->sp_buffers[i],
  631. xhci->scratchpad->sp_dma_buffers[i]);
  632. }
  633. kfree(xhci->scratchpad->sp_dma_buffers);
  634. fail_sp4:
  635. kfree(xhci->scratchpad->sp_buffers);
  636. fail_sp3:
  637. pci_free_consistent(to_pci_dev(dev), num_sp * sizeof(u64),
  638. xhci->scratchpad->sp_array,
  639. xhci->scratchpad->sp_dma);
  640. fail_sp2:
  641. kfree(xhci->scratchpad);
  642. xhci->scratchpad = NULL;
  643. fail_sp:
  644. return -ENOMEM;
  645. }
  646. static void scratchpad_free(struct xhci_hcd *xhci)
  647. {
  648. int num_sp;
  649. int i;
  650. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  651. if (!xhci->scratchpad)
  652. return;
  653. num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
  654. for (i = 0; i < num_sp; i++) {
  655. pci_free_consistent(pdev, xhci->page_size,
  656. xhci->scratchpad->sp_buffers[i],
  657. xhci->scratchpad->sp_dma_buffers[i]);
  658. }
  659. kfree(xhci->scratchpad->sp_dma_buffers);
  660. kfree(xhci->scratchpad->sp_buffers);
  661. pci_free_consistent(pdev, num_sp * sizeof(u64),
  662. xhci->scratchpad->sp_array,
  663. xhci->scratchpad->sp_dma);
  664. kfree(xhci->scratchpad);
  665. xhci->scratchpad = NULL;
  666. }
  667. struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
  668. bool allocate_completion, gfp_t mem_flags)
  669. {
  670. struct xhci_command *command;
  671. command = kzalloc(sizeof(*command), mem_flags);
  672. if (!command)
  673. return NULL;
  674. command->in_ctx =
  675. xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, mem_flags);
  676. if (!command->in_ctx) {
  677. kfree(command);
  678. return NULL;
  679. }
  680. if (allocate_completion) {
  681. command->completion =
  682. kzalloc(sizeof(struct completion), mem_flags);
  683. if (!command->completion) {
  684. xhci_free_container_ctx(xhci, command->in_ctx);
  685. kfree(command);
  686. return NULL;
  687. }
  688. init_completion(command->completion);
  689. }
  690. command->status = 0;
  691. INIT_LIST_HEAD(&command->cmd_list);
  692. return command;
  693. }
  694. void xhci_free_command(struct xhci_hcd *xhci,
  695. struct xhci_command *command)
  696. {
  697. xhci_free_container_ctx(xhci,
  698. command->in_ctx);
  699. kfree(command->completion);
  700. kfree(command);
  701. }
  702. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  703. {
  704. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  705. int size;
  706. int i;
  707. /* Free the Event Ring Segment Table and the actual Event Ring */
  708. if (xhci->ir_set) {
  709. xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
  710. xhci_write_64(xhci, 0, &xhci->ir_set->erst_base);
  711. xhci_write_64(xhci, 0, &xhci->ir_set->erst_dequeue);
  712. }
  713. size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
  714. if (xhci->erst.entries)
  715. pci_free_consistent(pdev, size,
  716. xhci->erst.entries, xhci->erst.erst_dma_addr);
  717. xhci->erst.entries = NULL;
  718. xhci_dbg(xhci, "Freed ERST\n");
  719. if (xhci->event_ring)
  720. xhci_ring_free(xhci, xhci->event_ring);
  721. xhci->event_ring = NULL;
  722. xhci_dbg(xhci, "Freed event ring\n");
  723. xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
  724. if (xhci->cmd_ring)
  725. xhci_ring_free(xhci, xhci->cmd_ring);
  726. xhci->cmd_ring = NULL;
  727. xhci_dbg(xhci, "Freed command ring\n");
  728. for (i = 1; i < MAX_HC_SLOTS; ++i)
  729. xhci_free_virt_device(xhci, i);
  730. if (xhci->segment_pool)
  731. dma_pool_destroy(xhci->segment_pool);
  732. xhci->segment_pool = NULL;
  733. xhci_dbg(xhci, "Freed segment pool\n");
  734. if (xhci->device_pool)
  735. dma_pool_destroy(xhci->device_pool);
  736. xhci->device_pool = NULL;
  737. xhci_dbg(xhci, "Freed device context pool\n");
  738. xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
  739. if (xhci->dcbaa)
  740. pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
  741. xhci->dcbaa, xhci->dcbaa->dma);
  742. xhci->dcbaa = NULL;
  743. scratchpad_free(xhci);
  744. xhci->page_size = 0;
  745. xhci->page_shift = 0;
  746. }
  747. static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
  748. struct xhci_segment *input_seg,
  749. union xhci_trb *start_trb,
  750. union xhci_trb *end_trb,
  751. dma_addr_t input_dma,
  752. struct xhci_segment *result_seg,
  753. char *test_name, int test_number)
  754. {
  755. unsigned long long start_dma;
  756. unsigned long long end_dma;
  757. struct xhci_segment *seg;
  758. start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
  759. end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
  760. seg = trb_in_td(input_seg, start_trb, end_trb, input_dma);
  761. if (seg != result_seg) {
  762. xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
  763. test_name, test_number);
  764. xhci_warn(xhci, "Tested TRB math w/ seg %p and "
  765. "input DMA 0x%llx\n",
  766. input_seg,
  767. (unsigned long long) input_dma);
  768. xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
  769. "ending TRB %p (0x%llx DMA)\n",
  770. start_trb, start_dma,
  771. end_trb, end_dma);
  772. xhci_warn(xhci, "Expected seg %p, got seg %p\n",
  773. result_seg, seg);
  774. return -1;
  775. }
  776. return 0;
  777. }
  778. /* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
  779. static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
  780. {
  781. struct {
  782. dma_addr_t input_dma;
  783. struct xhci_segment *result_seg;
  784. } simple_test_vector [] = {
  785. /* A zeroed DMA field should fail */
  786. { 0, NULL },
  787. /* One TRB before the ring start should fail */
  788. { xhci->event_ring->first_seg->dma - 16, NULL },
  789. /* One byte before the ring start should fail */
  790. { xhci->event_ring->first_seg->dma - 1, NULL },
  791. /* Starting TRB should succeed */
  792. { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
  793. /* Ending TRB should succeed */
  794. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
  795. xhci->event_ring->first_seg },
  796. /* One byte after the ring end should fail */
  797. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
  798. /* One TRB after the ring end should fail */
  799. { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
  800. /* An address of all ones should fail */
  801. { (dma_addr_t) (~0), NULL },
  802. };
  803. struct {
  804. struct xhci_segment *input_seg;
  805. union xhci_trb *start_trb;
  806. union xhci_trb *end_trb;
  807. dma_addr_t input_dma;
  808. struct xhci_segment *result_seg;
  809. } complex_test_vector [] = {
  810. /* Test feeding a valid DMA address from a different ring */
  811. { .input_seg = xhci->event_ring->first_seg,
  812. .start_trb = xhci->event_ring->first_seg->trbs,
  813. .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  814. .input_dma = xhci->cmd_ring->first_seg->dma,
  815. .result_seg = NULL,
  816. },
  817. /* Test feeding a valid end TRB from a different ring */
  818. { .input_seg = xhci->event_ring->first_seg,
  819. .start_trb = xhci->event_ring->first_seg->trbs,
  820. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  821. .input_dma = xhci->cmd_ring->first_seg->dma,
  822. .result_seg = NULL,
  823. },
  824. /* Test feeding a valid start and end TRB from a different ring */
  825. { .input_seg = xhci->event_ring->first_seg,
  826. .start_trb = xhci->cmd_ring->first_seg->trbs,
  827. .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  828. .input_dma = xhci->cmd_ring->first_seg->dma,
  829. .result_seg = NULL,
  830. },
  831. /* TRB in this ring, but after this TD */
  832. { .input_seg = xhci->event_ring->first_seg,
  833. .start_trb = &xhci->event_ring->first_seg->trbs[0],
  834. .end_trb = &xhci->event_ring->first_seg->trbs[3],
  835. .input_dma = xhci->event_ring->first_seg->dma + 4*16,
  836. .result_seg = NULL,
  837. },
  838. /* TRB in this ring, but before this TD */
  839. { .input_seg = xhci->event_ring->first_seg,
  840. .start_trb = &xhci->event_ring->first_seg->trbs[3],
  841. .end_trb = &xhci->event_ring->first_seg->trbs[6],
  842. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  843. .result_seg = NULL,
  844. },
  845. /* TRB in this ring, but after this wrapped TD */
  846. { .input_seg = xhci->event_ring->first_seg,
  847. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  848. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  849. .input_dma = xhci->event_ring->first_seg->dma + 2*16,
  850. .result_seg = NULL,
  851. },
  852. /* TRB in this ring, but before this wrapped TD */
  853. { .input_seg = xhci->event_ring->first_seg,
  854. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  855. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  856. .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
  857. .result_seg = NULL,
  858. },
  859. /* TRB not in this ring, and we have a wrapped TD */
  860. { .input_seg = xhci->event_ring->first_seg,
  861. .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
  862. .end_trb = &xhci->event_ring->first_seg->trbs[1],
  863. .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
  864. .result_seg = NULL,
  865. },
  866. };
  867. unsigned int num_tests;
  868. int i, ret;
  869. num_tests = sizeof(simple_test_vector) / sizeof(simple_test_vector[0]);
  870. for (i = 0; i < num_tests; i++) {
  871. ret = xhci_test_trb_in_td(xhci,
  872. xhci->event_ring->first_seg,
  873. xhci->event_ring->first_seg->trbs,
  874. &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
  875. simple_test_vector[i].input_dma,
  876. simple_test_vector[i].result_seg,
  877. "Simple", i);
  878. if (ret < 0)
  879. return ret;
  880. }
  881. num_tests = sizeof(complex_test_vector) / sizeof(complex_test_vector[0]);
  882. for (i = 0; i < num_tests; i++) {
  883. ret = xhci_test_trb_in_td(xhci,
  884. complex_test_vector[i].input_seg,
  885. complex_test_vector[i].start_trb,
  886. complex_test_vector[i].end_trb,
  887. complex_test_vector[i].input_dma,
  888. complex_test_vector[i].result_seg,
  889. "Complex", i);
  890. if (ret < 0)
  891. return ret;
  892. }
  893. xhci_dbg(xhci, "TRB math tests passed.\n");
  894. return 0;
  895. }
  896. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  897. {
  898. dma_addr_t dma;
  899. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  900. unsigned int val, val2;
  901. u64 val_64;
  902. struct xhci_segment *seg;
  903. u32 page_size;
  904. int i;
  905. page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
  906. xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
  907. for (i = 0; i < 16; i++) {
  908. if ((0x1 & page_size) != 0)
  909. break;
  910. page_size = page_size >> 1;
  911. }
  912. if (i < 16)
  913. xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
  914. else
  915. xhci_warn(xhci, "WARN: no supported page size\n");
  916. /* Use 4K pages, since that's common and the minimum the HC supports */
  917. xhci->page_shift = 12;
  918. xhci->page_size = 1 << xhci->page_shift;
  919. xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
  920. /*
  921. * Program the Number of Device Slots Enabled field in the CONFIG
  922. * register with the max value of slots the HC can handle.
  923. */
  924. val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
  925. xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
  926. (unsigned int) val);
  927. val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
  928. val |= (val2 & ~HCS_SLOTS_MASK);
  929. xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
  930. (unsigned int) val);
  931. xhci_writel(xhci, val, &xhci->op_regs->config_reg);
  932. /*
  933. * Section 5.4.8 - doorbell array must be
  934. * "physically contiguous and 64-byte (cache line) aligned".
  935. */
  936. xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
  937. sizeof(*xhci->dcbaa), &dma);
  938. if (!xhci->dcbaa)
  939. goto fail;
  940. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  941. xhci->dcbaa->dma = dma;
  942. xhci_dbg(xhci, "// Device context base array address = 0x%llx (DMA), %p (virt)\n",
  943. (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
  944. xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
  945. /*
  946. * Initialize the ring segment pool. The ring must be a contiguous
  947. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  948. * however, the command ring segment needs 64-byte aligned segments,
  949. * so we pick the greater alignment need.
  950. */
  951. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  952. SEGMENT_SIZE, 64, xhci->page_size);
  953. /* See Table 46 and Note on Figure 55 */
  954. xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
  955. 2112, 64, xhci->page_size);
  956. if (!xhci->segment_pool || !xhci->device_pool)
  957. goto fail;
  958. /* Set up the command ring to have one segments for now. */
  959. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
  960. if (!xhci->cmd_ring)
  961. goto fail;
  962. xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
  963. xhci_dbg(xhci, "First segment DMA is 0x%llx\n",
  964. (unsigned long long)xhci->cmd_ring->first_seg->dma);
  965. /* Set the address in the Command Ring Control register */
  966. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  967. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  968. (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
  969. xhci->cmd_ring->cycle_state;
  970. xhci_dbg(xhci, "// Setting command ring address to 0x%x\n", val);
  971. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  972. xhci_dbg_cmd_ptrs(xhci);
  973. val = xhci_readl(xhci, &xhci->cap_regs->db_off);
  974. val &= DBOFF_MASK;
  975. xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
  976. " from cap regs base addr\n", val);
  977. xhci->dba = (void *) xhci->cap_regs + val;
  978. xhci_dbg_regs(xhci);
  979. xhci_print_run_regs(xhci);
  980. /* Set ir_set to interrupt register set 0 */
  981. xhci->ir_set = (void *) xhci->run_regs->ir_set;
  982. /*
  983. * Event ring setup: Allocate a normal ring, but also setup
  984. * the event ring segment table (ERST). Section 4.9.3.
  985. */
  986. xhci_dbg(xhci, "// Allocating event ring\n");
  987. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
  988. if (!xhci->event_ring)
  989. goto fail;
  990. if (xhci_check_trb_in_td_math(xhci, flags) < 0)
  991. goto fail;
  992. xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
  993. sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
  994. if (!xhci->erst.entries)
  995. goto fail;
  996. xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
  997. (unsigned long long)dma);
  998. memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
  999. xhci->erst.num_entries = ERST_NUM_SEGS;
  1000. xhci->erst.erst_dma_addr = dma;
  1001. xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = %p, dma addr = 0x%llx\n",
  1002. xhci->erst.num_entries,
  1003. xhci->erst.entries,
  1004. (unsigned long long)xhci->erst.erst_dma_addr);
  1005. /* set ring base address and size for each segment table entry */
  1006. for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
  1007. struct xhci_erst_entry *entry = &xhci->erst.entries[val];
  1008. entry->seg_addr = seg->dma;
  1009. entry->seg_size = TRBS_PER_SEGMENT;
  1010. entry->rsvd = 0;
  1011. seg = seg->next;
  1012. }
  1013. /* set ERST count with the number of entries in the segment table */
  1014. val = xhci_readl(xhci, &xhci->ir_set->erst_size);
  1015. val &= ERST_SIZE_MASK;
  1016. val |= ERST_NUM_SEGS;
  1017. xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
  1018. val);
  1019. xhci_writel(xhci, val, &xhci->ir_set->erst_size);
  1020. xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
  1021. /* set the segment table base address */
  1022. xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%llx\n",
  1023. (unsigned long long)xhci->erst.erst_dma_addr);
  1024. val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  1025. val_64 &= ERST_PTR_MASK;
  1026. val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
  1027. xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
  1028. /* Set the event ring dequeue address */
  1029. xhci_set_hc_event_deq(xhci);
  1030. xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
  1031. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  1032. /*
  1033. * XXX: Might need to set the Interrupter Moderation Register to
  1034. * something other than the default (~1ms minimum between interrupts).
  1035. * See section 5.5.1.2.
  1036. */
  1037. init_completion(&xhci->addr_dev);
  1038. for (i = 0; i < MAX_HC_SLOTS; ++i)
  1039. xhci->devs[i] = 0;
  1040. if (scratchpad_alloc(xhci, flags))
  1041. goto fail;
  1042. return 0;
  1043. fail:
  1044. xhci_warn(xhci, "Couldn't initialize memory\n");
  1045. xhci_mem_cleanup(xhci);
  1046. return -ENOMEM;
  1047. }