lpfc_mem.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Enterprise Fibre Channel Host Bus Adapters. *
  4. * Refer to the README file included with this package for *
  5. * driver version and adapter support. *
  6. * Copyright (C) 2004 Emulex Corporation. *
  7. * www.emulex.com *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of the GNU General Public License *
  11. * as published by the Free Software Foundation; either version 2 *
  12. * of the License, or (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details, a copy of which *
  18. * can be found in the file COPYING included with this package. *
  19. *******************************************************************/
  20. /*
  21. * $Id: lpfc_mem.c 1.79 2005/04/13 14:25:50EDT sf_support Exp $
  22. */
  23. #include <linux/mempool.h>
  24. #include <linux/pci.h>
  25. #include <linux/interrupt.h>
  26. #include "lpfc_hw.h"
  27. #include "lpfc_sli.h"
  28. #include "lpfc_disc.h"
  29. #include "lpfc_scsi.h"
  30. #include "lpfc.h"
  31. #include "lpfc_crtn.h"
  32. #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
  33. #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
  34. static void *
  35. lpfc_pool_kmalloc(unsigned int gfp_flags, void *data)
  36. {
  37. return kmalloc((unsigned long)data, gfp_flags);
  38. }
  39. static void
  40. lpfc_pool_kfree(void *obj, void *data)
  41. {
  42. kfree(obj);
  43. }
  44. int
  45. lpfc_mem_alloc(struct lpfc_hba * phba)
  46. {
  47. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  48. int i;
  49. phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
  50. phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
  51. if (!phba->lpfc_scsi_dma_buf_pool)
  52. goto fail;
  53. phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
  54. LPFC_BPL_SIZE, 8,0);
  55. if (!phba->lpfc_mbuf_pool)
  56. goto fail_free_dma_buf_pool;
  57. pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
  58. LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
  59. pool->max_count = 0;
  60. pool->current_count = 0;
  61. for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
  62. pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
  63. GFP_KERNEL, &pool->elements[i].phys);
  64. if (!pool->elements[i].virt)
  65. goto fail_free_mbuf_pool;
  66. pool->max_count++;
  67. pool->current_count++;
  68. }
  69. phba->mbox_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
  70. lpfc_pool_kmalloc, lpfc_pool_kfree,
  71. (void *)(unsigned long)sizeof(LPFC_MBOXQ_t));
  72. if (!phba->mbox_mem_pool)
  73. goto fail_free_mbuf_pool;
  74. phba->nlp_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
  75. lpfc_pool_kmalloc, lpfc_pool_kfree,
  76. (void *)(unsigned long)sizeof(struct lpfc_nodelist));
  77. if (!phba->nlp_mem_pool)
  78. goto fail_free_mbox_pool;
  79. return 0;
  80. fail_free_mbox_pool:
  81. mempool_destroy(phba->mbox_mem_pool);
  82. fail_free_mbuf_pool:
  83. while (--i)
  84. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  85. pool->elements[i].phys);
  86. kfree(pool->elements);
  87. pci_pool_destroy(phba->lpfc_mbuf_pool);
  88. fail_free_dma_buf_pool:
  89. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  90. fail:
  91. return -ENOMEM;
  92. }
  93. void
  94. lpfc_mem_free(struct lpfc_hba * phba)
  95. {
  96. struct lpfc_sli *psli = &phba->sli;
  97. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  98. LPFC_MBOXQ_t *mbox, *next_mbox;
  99. struct lpfc_dmabuf *mp;
  100. int i;
  101. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
  102. mp = (struct lpfc_dmabuf *) (mbox->context1);
  103. if (mp) {
  104. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  105. kfree(mp);
  106. }
  107. list_del(&mbox->list);
  108. mempool_free(mbox, phba->mbox_mem_pool);
  109. }
  110. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  111. if (psli->mbox_active) {
  112. mbox = psli->mbox_active;
  113. mp = (struct lpfc_dmabuf *) (mbox->context1);
  114. if (mp) {
  115. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  116. kfree(mp);
  117. }
  118. mempool_free(mbox, phba->mbox_mem_pool);
  119. psli->mbox_active = NULL;
  120. }
  121. for (i = 0; i < pool->current_count; i++)
  122. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  123. pool->elements[i].phys);
  124. kfree(pool->elements);
  125. mempool_destroy(phba->nlp_mem_pool);
  126. mempool_destroy(phba->mbox_mem_pool);
  127. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  128. pci_pool_destroy(phba->lpfc_mbuf_pool);
  129. }
  130. void *
  131. lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  132. {
  133. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  134. void *ret;
  135. ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
  136. if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) {
  137. pool->current_count--;
  138. ret = pool->elements[pool->current_count].virt;
  139. *handle = pool->elements[pool->current_count].phys;
  140. }
  141. return ret;
  142. }
  143. void
  144. lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  145. {
  146. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  147. if (pool->current_count < pool->max_count) {
  148. pool->elements[pool->current_count].virt = virt;
  149. pool->elements[pool->current_count].phys = dma;
  150. pool->current_count++;
  151. } else {
  152. pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
  153. }
  154. return;
  155. }