xhci-mem.c 36 KB

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