xhci-mem.c 37 KB

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