lpfc_mem.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 i;
  40. phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
  41. phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
  42. if (!phba->lpfc_scsi_dma_buf_pool)
  43. goto fail;
  44. phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
  45. LPFC_BPL_SIZE, 8,0);
  46. if (!phba->lpfc_mbuf_pool)
  47. goto fail_free_dma_buf_pool;
  48. pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
  49. LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
  50. if (!pool->elements)
  51. goto fail_free_lpfc_mbuf_pool;
  52. pool->max_count = 0;
  53. pool->current_count = 0;
  54. for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
  55. pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
  56. GFP_KERNEL, &pool->elements[i].phys);
  57. if (!pool->elements[i].virt)
  58. goto fail_free_mbuf_pool;
  59. pool->max_count++;
  60. pool->current_count++;
  61. }
  62. phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  63. sizeof(LPFC_MBOXQ_t));
  64. if (!phba->mbox_mem_pool)
  65. goto fail_free_mbuf_pool;
  66. phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  67. sizeof(struct lpfc_nodelist));
  68. if (!phba->nlp_mem_pool)
  69. goto fail_free_mbox_pool;
  70. phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev,
  71. LPFC_BPL_SIZE, 8, 0);
  72. if (!phba->lpfc_hbq_pool)
  73. goto fail_free_nlp_mem_pool;
  74. return 0;
  75. fail_free_nlp_mem_pool:
  76. mempool_destroy(phba->nlp_mem_pool);
  77. phba->nlp_mem_pool = NULL;
  78. fail_free_mbox_pool:
  79. mempool_destroy(phba->mbox_mem_pool);
  80. phba->mbox_mem_pool = NULL;
  81. fail_free_mbuf_pool:
  82. while (i--)
  83. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  84. pool->elements[i].phys);
  85. kfree(pool->elements);
  86. fail_free_lpfc_mbuf_pool:
  87. pci_pool_destroy(phba->lpfc_mbuf_pool);
  88. phba->lpfc_mbuf_pool = NULL;
  89. fail_free_dma_buf_pool:
  90. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  91. phba->lpfc_scsi_dma_buf_pool = NULL;
  92. fail:
  93. return -ENOMEM;
  94. }
  95. void
  96. lpfc_mem_free(struct lpfc_hba * phba)
  97. {
  98. struct lpfc_sli *psli = &phba->sli;
  99. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  100. LPFC_MBOXQ_t *mbox, *next_mbox;
  101. struct lpfc_dmabuf *mp;
  102. int i;
  103. lpfc_sli_hbqbuf_free_all(phba);
  104. spin_lock_irq(&phba->hbalock);
  105. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
  106. mp = (struct lpfc_dmabuf *) (mbox->context1);
  107. if (mp) {
  108. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  109. kfree(mp);
  110. }
  111. list_del(&mbox->list);
  112. mempool_free(mbox, phba->mbox_mem_pool);
  113. }
  114. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  115. spin_unlock_irq(&phba->hbalock);
  116. if (psli->mbox_active) {
  117. mbox = psli->mbox_active;
  118. mp = (struct lpfc_dmabuf *) (mbox->context1);
  119. if (mp) {
  120. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  121. kfree(mp);
  122. }
  123. mempool_free(mbox, phba->mbox_mem_pool);
  124. psli->mbox_active = NULL;
  125. }
  126. for (i = 0; i < pool->current_count; i++)
  127. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  128. pool->elements[i].phys);
  129. kfree(pool->elements);
  130. pci_pool_destroy(phba->lpfc_hbq_pool);
  131. mempool_destroy(phba->nlp_mem_pool);
  132. mempool_destroy(phba->mbox_mem_pool);
  133. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  134. pci_pool_destroy(phba->lpfc_mbuf_pool);
  135. phba->lpfc_hbq_pool = NULL;
  136. phba->nlp_mem_pool = NULL;
  137. phba->mbox_mem_pool = NULL;
  138. phba->lpfc_scsi_dma_buf_pool = NULL;
  139. phba->lpfc_mbuf_pool = NULL;
  140. /* Free the iocb lookup array */
  141. kfree(psli->iocbq_lookup);
  142. psli->iocbq_lookup = NULL;
  143. }
  144. void *
  145. lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  146. {
  147. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  148. unsigned long iflags;
  149. void *ret;
  150. ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
  151. spin_lock_irqsave(&phba->hbalock, iflags);
  152. if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) {
  153. pool->current_count--;
  154. ret = pool->elements[pool->current_count].virt;
  155. *handle = pool->elements[pool->current_count].phys;
  156. }
  157. spin_unlock_irqrestore(&phba->hbalock, iflags);
  158. return ret;
  159. }
  160. void
  161. __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  162. {
  163. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  164. if (pool->current_count < pool->max_count) {
  165. pool->elements[pool->current_count].virt = virt;
  166. pool->elements[pool->current_count].phys = dma;
  167. pool->current_count++;
  168. } else {
  169. pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
  170. }
  171. return;
  172. }
  173. void
  174. lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  175. {
  176. unsigned long iflags;
  177. spin_lock_irqsave(&phba->hbalock, iflags);
  178. __lpfc_mbuf_free(phba, virt, dma);
  179. spin_unlock_irqrestore(&phba->hbalock, iflags);
  180. return;
  181. }
  182. void *
  183. lpfc_hbq_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  184. {
  185. void *ret;
  186. ret = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_ATOMIC, handle);
  187. return ret;
  188. }
  189. void
  190. lpfc_hbq_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma)
  191. {
  192. pci_pool_free(phba->lpfc_hbq_pool, virt, dma);
  193. return;
  194. }