ehci-mem.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2001 by David Brownell
  4. */
  5. /* this file is part of ehci-hcd.c */
  6. /*-------------------------------------------------------------------------*/
  7. /*
  8. * There's basically three types of memory:
  9. * - data used only by the HCD ... kmalloc is fine
  10. * - async and periodic schedules, shared by HC and HCD ... these
  11. * need to use dma_pool or dma_alloc_coherent
  12. * - driver buffers, read/written by HC ... single shot DMA mapped
  13. *
  14. * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
  15. * No memory seen by this driver is pageable.
  16. */
  17. /*-------------------------------------------------------------------------*/
  18. /* Allocate the key transfer structures from the previously allocated pool */
  19. static inline void ehci_qtd_init(struct ehci_hcd *ehci, struct ehci_qtd *qtd,
  20. dma_addr_t dma)
  21. {
  22. memset (qtd, 0, sizeof *qtd);
  23. qtd->qtd_dma = dma;
  24. qtd->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
  25. qtd->hw_next = EHCI_LIST_END(ehci);
  26. qtd->hw_alt_next = EHCI_LIST_END(ehci);
  27. INIT_LIST_HEAD (&qtd->qtd_list);
  28. }
  29. static struct ehci_qtd *ehci_qtd_alloc (struct ehci_hcd *ehci, gfp_t flags)
  30. {
  31. struct ehci_qtd *qtd;
  32. dma_addr_t dma;
  33. qtd = dma_pool_alloc (ehci->qtd_pool, flags, &dma);
  34. if (qtd != NULL) {
  35. ehci_qtd_init(ehci, qtd, dma);
  36. }
  37. return qtd;
  38. }
  39. static inline void ehci_qtd_free (struct ehci_hcd *ehci, struct ehci_qtd *qtd)
  40. {
  41. dma_pool_free (ehci->qtd_pool, qtd, qtd->qtd_dma);
  42. }
  43. static void qh_destroy(struct ehci_hcd *ehci, struct ehci_qh *qh)
  44. {
  45. /* clean qtds first, and know this is not linked */
  46. if (!list_empty (&qh->qtd_list) || qh->qh_next.ptr) {
  47. ehci_dbg (ehci, "unused qh not empty!\n");
  48. BUG ();
  49. }
  50. if (qh->dummy)
  51. ehci_qtd_free (ehci, qh->dummy);
  52. dma_pool_free(ehci->qh_pool, qh->hw, qh->qh_dma);
  53. kfree(qh);
  54. }
  55. static struct ehci_qh *ehci_qh_alloc (struct ehci_hcd *ehci, gfp_t flags)
  56. {
  57. struct ehci_qh *qh;
  58. dma_addr_t dma;
  59. qh = kzalloc(sizeof *qh, GFP_ATOMIC);
  60. if (!qh)
  61. goto done;
  62. qh->hw = (struct ehci_qh_hw *)
  63. dma_pool_alloc(ehci->qh_pool, flags, &dma);
  64. if (!qh->hw)
  65. goto fail;
  66. memset(qh->hw, 0, sizeof *qh->hw);
  67. qh->qh_dma = dma;
  68. // INIT_LIST_HEAD (&qh->qh_list);
  69. INIT_LIST_HEAD (&qh->qtd_list);
  70. INIT_LIST_HEAD(&qh->unlink_node);
  71. /* dummy td enables safe urb queuing */
  72. qh->dummy = ehci_qtd_alloc (ehci, flags);
  73. if (qh->dummy == NULL) {
  74. ehci_dbg (ehci, "no dummy td\n");
  75. goto fail1;
  76. }
  77. done:
  78. return qh;
  79. fail1:
  80. dma_pool_free(ehci->qh_pool, qh->hw, qh->qh_dma);
  81. fail:
  82. kfree(qh);
  83. return NULL;
  84. }
  85. /*-------------------------------------------------------------------------*/
  86. /* The queue heads and transfer descriptors are managed from pools tied
  87. * to each of the "per device" structures.
  88. * This is the initialisation and cleanup code.
  89. */
  90. static void ehci_mem_cleanup (struct ehci_hcd *ehci)
  91. {
  92. if (ehci->async)
  93. qh_destroy(ehci, ehci->async);
  94. ehci->async = NULL;
  95. if (ehci->dummy)
  96. qh_destroy(ehci, ehci->dummy);
  97. ehci->dummy = NULL;
  98. /* DMA consistent memory and pools */
  99. dma_pool_destroy(ehci->qtd_pool);
  100. ehci->qtd_pool = NULL;
  101. dma_pool_destroy(ehci->qh_pool);
  102. ehci->qh_pool = NULL;
  103. dma_pool_destroy(ehci->itd_pool);
  104. ehci->itd_pool = NULL;
  105. dma_pool_destroy(ehci->sitd_pool);
  106. ehci->sitd_pool = NULL;
  107. if (ehci->periodic)
  108. dma_free_coherent(ehci_to_hcd(ehci)->self.sysdev,
  109. ehci->periodic_size * sizeof (u32),
  110. ehci->periodic, ehci->periodic_dma);
  111. ehci->periodic = NULL;
  112. /* shadow periodic table */
  113. kfree(ehci->pshadow);
  114. ehci->pshadow = NULL;
  115. }
  116. /* remember to add cleanup code (above) if you add anything here */
  117. static int ehci_mem_init (struct ehci_hcd *ehci, gfp_t flags)
  118. {
  119. int i;
  120. /* QTDs for control/bulk/intr transfers */
  121. ehci->qtd_pool = dma_pool_create ("ehci_qtd",
  122. ehci_to_hcd(ehci)->self.sysdev,
  123. sizeof (struct ehci_qtd),
  124. 32 /* byte alignment (for hw parts) */,
  125. 4096 /* can't cross 4K */);
  126. if (!ehci->qtd_pool) {
  127. goto fail;
  128. }
  129. /* QHs for control/bulk/intr transfers */
  130. ehci->qh_pool = dma_pool_create ("ehci_qh",
  131. ehci_to_hcd(ehci)->self.sysdev,
  132. sizeof(struct ehci_qh_hw),
  133. 32 /* byte alignment (for hw parts) */,
  134. 4096 /* can't cross 4K */);
  135. if (!ehci->qh_pool) {
  136. goto fail;
  137. }
  138. ehci->async = ehci_qh_alloc (ehci, flags);
  139. if (!ehci->async) {
  140. goto fail;
  141. }
  142. /* ITD for high speed ISO transfers */
  143. ehci->itd_pool = dma_pool_create ("ehci_itd",
  144. ehci_to_hcd(ehci)->self.sysdev,
  145. sizeof (struct ehci_itd),
  146. 32 /* byte alignment (for hw parts) */,
  147. 4096 /* can't cross 4K */);
  148. if (!ehci->itd_pool) {
  149. goto fail;
  150. }
  151. /* SITD for full/low speed split ISO transfers */
  152. ehci->sitd_pool = dma_pool_create ("ehci_sitd",
  153. ehci_to_hcd(ehci)->self.sysdev,
  154. sizeof (struct ehci_sitd),
  155. 32 /* byte alignment (for hw parts) */,
  156. 4096 /* can't cross 4K */);
  157. if (!ehci->sitd_pool) {
  158. goto fail;
  159. }
  160. /* Hardware periodic table */
  161. ehci->periodic = (__le32 *)
  162. dma_alloc_coherent(ehci_to_hcd(ehci)->self.sysdev,
  163. ehci->periodic_size * sizeof(__le32),
  164. &ehci->periodic_dma, flags);
  165. if (ehci->periodic == NULL) {
  166. goto fail;
  167. }
  168. if (ehci->use_dummy_qh) {
  169. struct ehci_qh_hw *hw;
  170. ehci->dummy = ehci_qh_alloc(ehci, flags);
  171. if (!ehci->dummy)
  172. goto fail;
  173. hw = ehci->dummy->hw;
  174. hw->hw_next = EHCI_LIST_END(ehci);
  175. hw->hw_qtd_next = EHCI_LIST_END(ehci);
  176. hw->hw_alt_next = EHCI_LIST_END(ehci);
  177. ehci->dummy->hw = hw;
  178. for (i = 0; i < ehci->periodic_size; i++)
  179. ehci->periodic[i] = cpu_to_hc32(ehci,
  180. ehci->dummy->qh_dma);
  181. } else {
  182. for (i = 0; i < ehci->periodic_size; i++)
  183. ehci->periodic[i] = EHCI_LIST_END(ehci);
  184. }
  185. /* software shadow of hardware table */
  186. ehci->pshadow = kcalloc(ehci->periodic_size, sizeof(void *), flags);
  187. if (ehci->pshadow != NULL)
  188. return 0;
  189. fail:
  190. ehci_dbg (ehci, "couldn't init memory\n");
  191. ehci_mem_cleanup (ehci);
  192. return -ENOMEM;
  193. }