lpfc_mem.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2005 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 "lpfc_hw.h"
  25. #include "lpfc_sli.h"
  26. #include "lpfc_disc.h"
  27. #include "lpfc_scsi.h"
  28. #include "lpfc.h"
  29. #include "lpfc_crtn.h"
  30. #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
  31. #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
  32. static void *
  33. lpfc_pool_kmalloc(unsigned int gfp_flags, void *data)
  34. {
  35. return kmalloc((unsigned long)data, gfp_flags);
  36. }
  37. static void
  38. lpfc_pool_kfree(void *obj, void *data)
  39. {
  40. kfree(obj);
  41. }
  42. int
  43. lpfc_mem_alloc(struct lpfc_hba * phba)
  44. {
  45. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  46. int i;
  47. phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
  48. phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
  49. if (!phba->lpfc_scsi_dma_buf_pool)
  50. goto fail;
  51. phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
  52. LPFC_BPL_SIZE, 8,0);
  53. if (!phba->lpfc_mbuf_pool)
  54. goto fail_free_dma_buf_pool;
  55. pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
  56. LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
  57. pool->max_count = 0;
  58. pool->current_count = 0;
  59. for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
  60. pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
  61. GFP_KERNEL, &pool->elements[i].phys);
  62. if (!pool->elements[i].virt)
  63. goto fail_free_mbuf_pool;
  64. pool->max_count++;
  65. pool->current_count++;
  66. }
  67. phba->mbox_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
  68. lpfc_pool_kmalloc, lpfc_pool_kfree,
  69. (void *)(unsigned long)sizeof(LPFC_MBOXQ_t));
  70. if (!phba->mbox_mem_pool)
  71. goto fail_free_mbuf_pool;
  72. phba->nlp_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
  73. lpfc_pool_kmalloc, lpfc_pool_kfree,
  74. (void *)(unsigned long)sizeof(struct lpfc_nodelist));
  75. if (!phba->nlp_mem_pool)
  76. goto fail_free_mbox_pool;
  77. return 0;
  78. fail_free_mbox_pool:
  79. mempool_destroy(phba->mbox_mem_pool);
  80. fail_free_mbuf_pool:
  81. while (--i)
  82. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  83. pool->elements[i].phys);
  84. kfree(pool->elements);
  85. pci_pool_destroy(phba->lpfc_mbuf_pool);
  86. fail_free_dma_buf_pool:
  87. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  88. fail:
  89. return -ENOMEM;
  90. }
  91. void
  92. lpfc_mem_free(struct lpfc_hba * phba)
  93. {
  94. struct lpfc_sli *psli = &phba->sli;
  95. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  96. LPFC_MBOXQ_t *mbox, *next_mbox;
  97. struct lpfc_dmabuf *mp;
  98. int i;
  99. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
  100. mp = (struct lpfc_dmabuf *) (mbox->context1);
  101. if (mp) {
  102. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  103. kfree(mp);
  104. }
  105. list_del(&mbox->list);
  106. mempool_free(mbox, phba->mbox_mem_pool);
  107. }
  108. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  109. if (psli->mbox_active) {
  110. mbox = psli->mbox_active;
  111. mp = (struct lpfc_dmabuf *) (mbox->context1);
  112. if (mp) {
  113. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  114. kfree(mp);
  115. }
  116. mempool_free(mbox, phba->mbox_mem_pool);
  117. psli->mbox_active = NULL;
  118. }
  119. for (i = 0; i < pool->current_count; i++)
  120. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  121. pool->elements[i].phys);
  122. kfree(pool->elements);
  123. mempool_destroy(phba->nlp_mem_pool);
  124. mempool_destroy(phba->mbox_mem_pool);
  125. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  126. pci_pool_destroy(phba->lpfc_mbuf_pool);
  127. }
  128. void *
  129. lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  130. {
  131. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  132. void *ret;
  133. ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
  134. if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) {
  135. pool->current_count--;
  136. ret = pool->elements[pool->current_count].virt;
  137. *handle = pool->elements[pool->current_count].phys;
  138. }
  139. return ret;
  140. }
  141. void
  142. lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  143. {
  144. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  145. if (pool->current_count < pool->max_count) {
  146. pool->elements[pool->current_count].virt = virt;
  147. pool->elements[pool->current_count].phys = dma;
  148. pool->current_count++;
  149. } else {
  150. pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
  151. }
  152. return;
  153. }