ehci-mem.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_zalloc(ehci->qh_pool, flags, &dma);
  64. if (!qh->hw)
  65. goto fail;
  66. qh->qh_dma = dma;
  67. // INIT_LIST_HEAD (&qh->qh_list);
  68. INIT_LIST_HEAD (&qh->qtd_list);
  69. INIT_LIST_HEAD(&qh->unlink_node);
  70. /* dummy td enables safe urb queuing */
  71. qh->dummy = ehci_qtd_alloc (ehci, flags);
  72. if (qh->dummy == NULL) {
  73. ehci_dbg (ehci, "no dummy td\n");
  74. goto fail1;
  75. }
  76. done:
  77. return qh;
  78. fail1:
  79. dma_pool_free(ehci->qh_pool, qh->hw, qh->qh_dma);
  80. fail:
  81. kfree(qh);
  82. return NULL;
  83. }
  84. /*-------------------------------------------------------------------------*/
  85. /* The queue heads and transfer descriptors are managed from pools tied
  86. * to each of the "per device" structures.
  87. * This is the initialisation and cleanup code.
  88. */
  89. static void ehci_mem_cleanup (struct ehci_hcd *ehci)
  90. {
  91. if (ehci->async)
  92. qh_destroy(ehci, ehci->async);
  93. ehci->async = NULL;
  94. if (ehci->dummy)
  95. qh_destroy(ehci, ehci->dummy);
  96. ehci->dummy = NULL;
  97. /* DMA consistent memory and pools */
  98. dma_pool_destroy(ehci->qtd_pool);
  99. ehci->qtd_pool = NULL;
  100. dma_pool_destroy(ehci->qh_pool);
  101. ehci->qh_pool = NULL;
  102. dma_pool_destroy(ehci->itd_pool);
  103. ehci->itd_pool = NULL;
  104. dma_pool_destroy(ehci->sitd_pool);
  105. ehci->sitd_pool = NULL;
  106. if (ehci->periodic)
  107. dma_free_coherent(ehci_to_hcd(ehci)->self.sysdev,
  108. ehci->periodic_size * sizeof (u32),
  109. ehci->periodic, ehci->periodic_dma);
  110. ehci->periodic = NULL;
  111. /* shadow periodic table */
  112. kfree(ehci->pshadow);
  113. ehci->pshadow = NULL;
  114. }
  115. /* remember to add cleanup code (above) if you add anything here */
  116. static int ehci_mem_init (struct ehci_hcd *ehci, gfp_t flags)
  117. {
  118. int i;
  119. /* QTDs for control/bulk/intr transfers */
  120. ehci->qtd_pool = dma_pool_create ("ehci_qtd",
  121. ehci_to_hcd(ehci)->self.sysdev,
  122. sizeof (struct ehci_qtd),
  123. 32 /* byte alignment (for hw parts) */,
  124. 4096 /* can't cross 4K */);
  125. if (!ehci->qtd_pool) {
  126. goto fail;
  127. }
  128. /* QHs for control/bulk/intr transfers */
  129. ehci->qh_pool = dma_pool_create ("ehci_qh",
  130. ehci_to_hcd(ehci)->self.sysdev,
  131. sizeof(struct ehci_qh_hw),
  132. 32 /* byte alignment (for hw parts) */,
  133. 4096 /* can't cross 4K */);
  134. if (!ehci->qh_pool) {
  135. goto fail;
  136. }
  137. ehci->async = ehci_qh_alloc (ehci, flags);
  138. if (!ehci->async) {
  139. goto fail;
  140. }
  141. /* ITD for high speed ISO transfers */
  142. ehci->itd_pool = dma_pool_create ("ehci_itd",
  143. ehci_to_hcd(ehci)->self.sysdev,
  144. sizeof (struct ehci_itd),
  145. 32 /* byte alignment (for hw parts) */,
  146. 4096 /* can't cross 4K */);
  147. if (!ehci->itd_pool) {
  148. goto fail;
  149. }
  150. /* SITD for full/low speed split ISO transfers */
  151. ehci->sitd_pool = dma_pool_create ("ehci_sitd",
  152. ehci_to_hcd(ehci)->self.sysdev,
  153. sizeof (struct ehci_sitd),
  154. 32 /* byte alignment (for hw parts) */,
  155. 4096 /* can't cross 4K */);
  156. if (!ehci->sitd_pool) {
  157. goto fail;
  158. }
  159. /* Hardware periodic table */
  160. ehci->periodic = (__le32 *)
  161. dma_alloc_coherent(ehci_to_hcd(ehci)->self.sysdev,
  162. ehci->periodic_size * sizeof(__le32),
  163. &ehci->periodic_dma, flags);
  164. if (ehci->periodic == NULL) {
  165. goto fail;
  166. }
  167. if (ehci->use_dummy_qh) {
  168. struct ehci_qh_hw *hw;
  169. ehci->dummy = ehci_qh_alloc(ehci, flags);
  170. if (!ehci->dummy)
  171. goto fail;
  172. hw = ehci->dummy->hw;
  173. hw->hw_next = EHCI_LIST_END(ehci);
  174. hw->hw_qtd_next = EHCI_LIST_END(ehci);
  175. hw->hw_alt_next = EHCI_LIST_END(ehci);
  176. ehci->dummy->hw = hw;
  177. for (i = 0; i < ehci->periodic_size; i++)
  178. ehci->periodic[i] = cpu_to_hc32(ehci,
  179. ehci->dummy->qh_dma);
  180. } else {
  181. for (i = 0; i < ehci->periodic_size; i++)
  182. ehci->periodic[i] = EHCI_LIST_END(ehci);
  183. }
  184. /* software shadow of hardware table */
  185. ehci->pshadow = kcalloc(ehci->periodic_size, sizeof(void *), flags);
  186. if (ehci->pshadow != NULL)
  187. return 0;
  188. fail:
  189. ehci_dbg (ehci, "couldn't init memory\n");
  190. ehci_mem_cleanup (ehci);
  191. return -ENOMEM;
  192. }