lpfc_mem.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2006 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/mempool.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_transport_fc.h>
  26. #include <scsi/scsi.h>
  27. #include "lpfc_hw.h"
  28. #include "lpfc_sli.h"
  29. #include "lpfc_disc.h"
  30. #include "lpfc_scsi.h"
  31. #include "lpfc.h"
  32. #include "lpfc_crtn.h"
  33. #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
  34. #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
  35. int
  36. lpfc_mem_alloc(struct lpfc_hba * phba)
  37. {
  38. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  39. int longs;
  40. int i;
  41. phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
  42. phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
  43. if (!phba->lpfc_scsi_dma_buf_pool)
  44. goto fail;
  45. phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
  46. LPFC_BPL_SIZE, 8,0);
  47. if (!phba->lpfc_mbuf_pool)
  48. goto fail_free_dma_buf_pool;
  49. pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
  50. LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
  51. if (!pool->elements)
  52. goto fail_free_lpfc_mbuf_pool;
  53. pool->max_count = 0;
  54. pool->current_count = 0;
  55. for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
  56. pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
  57. GFP_KERNEL, &pool->elements[i].phys);
  58. if (!pool->elements[i].virt)
  59. goto fail_free_mbuf_pool;
  60. pool->max_count++;
  61. pool->current_count++;
  62. }
  63. phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  64. sizeof(LPFC_MBOXQ_t));
  65. if (!phba->mbox_mem_pool)
  66. goto fail_free_mbuf_pool;
  67. phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  68. sizeof(struct lpfc_nodelist));
  69. if (!phba->nlp_mem_pool)
  70. goto fail_free_mbox_pool;
  71. phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev,
  72. LPFC_BPL_SIZE, 8, 0);
  73. if (!phba->lpfc_hbq_pool)
  74. goto fail_free_nlp_mem_pool;
  75. /* vpi zero is reserved for the physical port so add 1 to max */
  76. longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  77. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
  78. if (!phba->vpi_bmask)
  79. goto fail_free_hbq_pool;
  80. return 0;
  81. fail_free_hbq_pool:
  82. lpfc_sli_hbqbuf_free_all(phba);
  83. pci_pool_destroy(phba->lpfc_hbq_pool);
  84. fail_free_nlp_mem_pool:
  85. mempool_destroy(phba->nlp_mem_pool);
  86. phba->nlp_mem_pool = NULL;
  87. fail_free_mbox_pool:
  88. mempool_destroy(phba->mbox_mem_pool);
  89. phba->mbox_mem_pool = NULL;
  90. fail_free_mbuf_pool:
  91. while (i--)
  92. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  93. pool->elements[i].phys);
  94. kfree(pool->elements);
  95. fail_free_lpfc_mbuf_pool:
  96. pci_pool_destroy(phba->lpfc_mbuf_pool);
  97. phba->lpfc_mbuf_pool = NULL;
  98. fail_free_dma_buf_pool:
  99. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  100. phba->lpfc_scsi_dma_buf_pool = NULL;
  101. fail:
  102. return -ENOMEM;
  103. }
  104. void
  105. lpfc_mem_free(struct lpfc_hba * phba)
  106. {
  107. struct lpfc_sli *psli = &phba->sli;
  108. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  109. LPFC_MBOXQ_t *mbox, *next_mbox;
  110. struct lpfc_dmabuf *mp;
  111. int i;
  112. kfree(phba->vpi_bmask);
  113. lpfc_sli_hbqbuf_free_all(phba);
  114. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
  115. mp = (struct lpfc_dmabuf *) (mbox->context1);
  116. if (mp) {
  117. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  118. kfree(mp);
  119. }
  120. list_del(&mbox->list);
  121. mempool_free(mbox, phba->mbox_mem_pool);
  122. }
  123. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
  124. mp = (struct lpfc_dmabuf *) (mbox->context1);
  125. if (mp) {
  126. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  127. kfree(mp);
  128. }
  129. list_del(&mbox->list);
  130. mempool_free(mbox, phba->mbox_mem_pool);
  131. }
  132. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  133. if (psli->mbox_active) {
  134. mbox = psli->mbox_active;
  135. mp = (struct lpfc_dmabuf *) (mbox->context1);
  136. if (mp) {
  137. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  138. kfree(mp);
  139. }
  140. mempool_free(mbox, phba->mbox_mem_pool);
  141. psli->mbox_active = NULL;
  142. }
  143. for (i = 0; i < pool->current_count; i++)
  144. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  145. pool->elements[i].phys);
  146. kfree(pool->elements);
  147. pci_pool_destroy(phba->lpfc_hbq_pool);
  148. mempool_destroy(phba->nlp_mem_pool);
  149. mempool_destroy(phba->mbox_mem_pool);
  150. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  151. pci_pool_destroy(phba->lpfc_mbuf_pool);
  152. phba->lpfc_hbq_pool = NULL;
  153. phba->nlp_mem_pool = NULL;
  154. phba->mbox_mem_pool = NULL;
  155. phba->lpfc_scsi_dma_buf_pool = NULL;
  156. phba->lpfc_mbuf_pool = NULL;
  157. /* Free the iocb lookup array */
  158. kfree(psli->iocbq_lookup);
  159. psli->iocbq_lookup = NULL;
  160. }
  161. void *
  162. lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  163. {
  164. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  165. unsigned long iflags;
  166. void *ret;
  167. ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
  168. spin_lock_irqsave(&phba->hbalock, iflags);
  169. if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
  170. pool->current_count--;
  171. ret = pool->elements[pool->current_count].virt;
  172. *handle = pool->elements[pool->current_count].phys;
  173. }
  174. spin_unlock_irqrestore(&phba->hbalock, iflags);
  175. return ret;
  176. }
  177. void
  178. __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  179. {
  180. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  181. if (pool->current_count < pool->max_count) {
  182. pool->elements[pool->current_count].virt = virt;
  183. pool->elements[pool->current_count].phys = dma;
  184. pool->current_count++;
  185. } else {
  186. pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
  187. }
  188. return;
  189. }
  190. void
  191. lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  192. {
  193. unsigned long iflags;
  194. spin_lock_irqsave(&phba->hbalock, iflags);
  195. __lpfc_mbuf_free(phba, virt, dma);
  196. spin_unlock_irqrestore(&phba->hbalock, iflags);
  197. return;
  198. }
  199. struct hbq_dmabuf *
  200. lpfc_els_hbq_alloc(struct lpfc_hba *phba)
  201. {
  202. struct hbq_dmabuf *hbqbp;
  203. hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
  204. if (!hbqbp)
  205. return NULL;
  206. hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
  207. &hbqbp->dbuf.phys);
  208. if (!hbqbp->dbuf.virt) {
  209. kfree(hbqbp);
  210. return NULL;
  211. }
  212. hbqbp->size = LPFC_BPL_SIZE;
  213. return hbqbp;
  214. }
  215. void
  216. lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
  217. {
  218. pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
  219. kfree(hbqbp);
  220. return;
  221. }
  222. /* This is ONLY called for the LPFC_ELS_HBQ */
  223. void
  224. lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
  225. {
  226. struct hbq_dmabuf *hbq_entry;
  227. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  228. hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
  229. if (hbq_entry->tag == -1) {
  230. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  231. (phba, hbq_entry);
  232. } else {
  233. lpfc_sli_free_hbq(phba, hbq_entry);
  234. }
  235. } else {
  236. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  237. kfree(mp);
  238. }
  239. return;
  240. }