lpfc_mem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2008 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_nl.h"
  30. #include "lpfc_disc.h"
  31. #include "lpfc_scsi.h"
  32. #include "lpfc.h"
  33. #include "lpfc_crtn.h"
  34. #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
  35. #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
  36. /**
  37. * lpfc_mem_alloc: create and allocate all PCI and memory pools
  38. * @phba: HBA to allocate pools for
  39. *
  40. * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
  41. * lpfc_mbuf_pool, lpfc_hbq_pool. Creates and allocates kmalloc-backed mempools
  42. * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
  43. *
  44. * Notes: Not interrupt-safe. Must be called with no locks held. If any
  45. * allocation fails, frees all successfully allocated memory before returning.
  46. *
  47. * Returns:
  48. * 0 on success
  49. * -ENOMEM on failure (if any memory allocations fail)
  50. **/
  51. int
  52. lpfc_mem_alloc(struct lpfc_hba * phba)
  53. {
  54. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  55. int longs;
  56. int i;
  57. phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
  58. phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
  59. if (!phba->lpfc_scsi_dma_buf_pool)
  60. goto fail;
  61. phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
  62. LPFC_BPL_SIZE, 8,0);
  63. if (!phba->lpfc_mbuf_pool)
  64. goto fail_free_dma_buf_pool;
  65. pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
  66. LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
  67. if (!pool->elements)
  68. goto fail_free_lpfc_mbuf_pool;
  69. pool->max_count = 0;
  70. pool->current_count = 0;
  71. for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
  72. pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
  73. GFP_KERNEL, &pool->elements[i].phys);
  74. if (!pool->elements[i].virt)
  75. goto fail_free_mbuf_pool;
  76. pool->max_count++;
  77. pool->current_count++;
  78. }
  79. phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  80. sizeof(LPFC_MBOXQ_t));
  81. if (!phba->mbox_mem_pool)
  82. goto fail_free_mbuf_pool;
  83. phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  84. sizeof(struct lpfc_nodelist));
  85. if (!phba->nlp_mem_pool)
  86. goto fail_free_mbox_pool;
  87. phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev,
  88. LPFC_BPL_SIZE, 8, 0);
  89. if (!phba->lpfc_hbq_pool)
  90. goto fail_free_nlp_mem_pool;
  91. /* vpi zero is reserved for the physical port so add 1 to max */
  92. longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  93. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
  94. if (!phba->vpi_bmask)
  95. goto fail_free_hbq_pool;
  96. return 0;
  97. fail_free_hbq_pool:
  98. lpfc_sli_hbqbuf_free_all(phba);
  99. pci_pool_destroy(phba->lpfc_hbq_pool);
  100. fail_free_nlp_mem_pool:
  101. mempool_destroy(phba->nlp_mem_pool);
  102. phba->nlp_mem_pool = NULL;
  103. fail_free_mbox_pool:
  104. mempool_destroy(phba->mbox_mem_pool);
  105. phba->mbox_mem_pool = NULL;
  106. fail_free_mbuf_pool:
  107. while (i--)
  108. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  109. pool->elements[i].phys);
  110. kfree(pool->elements);
  111. fail_free_lpfc_mbuf_pool:
  112. pci_pool_destroy(phba->lpfc_mbuf_pool);
  113. phba->lpfc_mbuf_pool = NULL;
  114. fail_free_dma_buf_pool:
  115. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  116. phba->lpfc_scsi_dma_buf_pool = NULL;
  117. fail:
  118. return -ENOMEM;
  119. }
  120. /**
  121. * lpfc_mem_free: Frees all PCI and memory allocated by lpfc_mem_alloc
  122. * @phba: HBA to free memory for
  123. *
  124. * Description: Frees PCI pools lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool,
  125. * lpfc_hbq_pool. Frees kmalloc-backed mempools for LPFC_MBOXQ_t and
  126. * lpfc_nodelist. Also frees the VPI bitmask.
  127. *
  128. * Returns: None
  129. **/
  130. void
  131. lpfc_mem_free(struct lpfc_hba * phba)
  132. {
  133. struct lpfc_sli *psli = &phba->sli;
  134. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  135. LPFC_MBOXQ_t *mbox, *next_mbox;
  136. struct lpfc_dmabuf *mp;
  137. int i;
  138. kfree(phba->vpi_bmask);
  139. lpfc_sli_hbqbuf_free_all(phba);
  140. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
  141. mp = (struct lpfc_dmabuf *) (mbox->context1);
  142. if (mp) {
  143. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  144. kfree(mp);
  145. }
  146. list_del(&mbox->list);
  147. mempool_free(mbox, phba->mbox_mem_pool);
  148. }
  149. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
  150. mp = (struct lpfc_dmabuf *) (mbox->context1);
  151. if (mp) {
  152. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  153. kfree(mp);
  154. }
  155. list_del(&mbox->list);
  156. mempool_free(mbox, phba->mbox_mem_pool);
  157. }
  158. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  159. if (psli->mbox_active) {
  160. mbox = psli->mbox_active;
  161. mp = (struct lpfc_dmabuf *) (mbox->context1);
  162. if (mp) {
  163. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  164. kfree(mp);
  165. }
  166. mempool_free(mbox, phba->mbox_mem_pool);
  167. psli->mbox_active = NULL;
  168. }
  169. for (i = 0; i < pool->current_count; i++)
  170. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  171. pool->elements[i].phys);
  172. kfree(pool->elements);
  173. pci_pool_destroy(phba->lpfc_hbq_pool);
  174. mempool_destroy(phba->nlp_mem_pool);
  175. mempool_destroy(phba->mbox_mem_pool);
  176. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  177. pci_pool_destroy(phba->lpfc_mbuf_pool);
  178. phba->lpfc_hbq_pool = NULL;
  179. phba->nlp_mem_pool = NULL;
  180. phba->mbox_mem_pool = NULL;
  181. phba->lpfc_scsi_dma_buf_pool = NULL;
  182. phba->lpfc_mbuf_pool = NULL;
  183. /* Free the iocb lookup array */
  184. kfree(psli->iocbq_lookup);
  185. psli->iocbq_lookup = NULL;
  186. }
  187. /**
  188. * lpfc_mbuf_alloc: Allocate an mbuf from the lpfc_mbuf_pool PCI pool
  189. * @phba: HBA which owns the pool to allocate from
  190. * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
  191. * @handle: used to return the DMA-mapped address of the mbuf
  192. *
  193. * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
  194. * Allocates from generic pci_pool_alloc function first and if that fails and
  195. * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
  196. * HBA's pool.
  197. *
  198. * Notes: Not interrupt-safe. Must be called with no locks held. Takes
  199. * phba->hbalock.
  200. *
  201. * Returns:
  202. * pointer to the allocated mbuf on success
  203. * NULL on failure
  204. **/
  205. void *
  206. lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  207. {
  208. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  209. unsigned long iflags;
  210. void *ret;
  211. ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
  212. spin_lock_irqsave(&phba->hbalock, iflags);
  213. if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
  214. pool->current_count--;
  215. ret = pool->elements[pool->current_count].virt;
  216. *handle = pool->elements[pool->current_count].phys;
  217. }
  218. spin_unlock_irqrestore(&phba->hbalock, iflags);
  219. return ret;
  220. }
  221. /**
  222. * __lpfc_mem_free: Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
  223. * @phba: HBA which owns the pool to return to
  224. * @virt: mbuf to free
  225. * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
  226. *
  227. * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
  228. * it is below its max_count, frees the mbuf otherwise.
  229. *
  230. * Notes: Must be called with phba->hbalock held to synchronize access to
  231. * lpfc_mbuf_safety_pool.
  232. *
  233. * Returns: None
  234. **/
  235. void
  236. __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  237. {
  238. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  239. if (pool->current_count < pool->max_count) {
  240. pool->elements[pool->current_count].virt = virt;
  241. pool->elements[pool->current_count].phys = dma;
  242. pool->current_count++;
  243. } else {
  244. pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
  245. }
  246. return;
  247. }
  248. /**
  249. * lpfc_mem_free: Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
  250. * @phba: HBA which owns the pool to return to
  251. * @virt: mbuf to free
  252. * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
  253. *
  254. * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
  255. * it is below its max_count, frees the mbuf otherwise.
  256. *
  257. * Notes: Takes phba->hbalock. Can be called with or without other locks held.
  258. *
  259. * Returns: None
  260. **/
  261. void
  262. lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  263. {
  264. unsigned long iflags;
  265. spin_lock_irqsave(&phba->hbalock, iflags);
  266. __lpfc_mbuf_free(phba, virt, dma);
  267. spin_unlock_irqrestore(&phba->hbalock, iflags);
  268. return;
  269. }
  270. /**
  271. * lpfc_els_hbq_alloc: Allocate an HBQ buffer
  272. * @phba: HBA to allocate HBQ buffer for
  273. *
  274. * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hbq_pool PCI
  275. * pool along a non-DMA-mapped container for it.
  276. *
  277. * Notes: Not interrupt-safe. Must be called with no locks held.
  278. *
  279. * Returns:
  280. * pointer to HBQ on success
  281. * NULL on failure
  282. **/
  283. struct hbq_dmabuf *
  284. lpfc_els_hbq_alloc(struct lpfc_hba *phba)
  285. {
  286. struct hbq_dmabuf *hbqbp;
  287. hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
  288. if (!hbqbp)
  289. return NULL;
  290. hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
  291. &hbqbp->dbuf.phys);
  292. if (!hbqbp->dbuf.virt) {
  293. kfree(hbqbp);
  294. return NULL;
  295. }
  296. hbqbp->size = LPFC_BPL_SIZE;
  297. return hbqbp;
  298. }
  299. /**
  300. * lpfc_mem_hbq_free: Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
  301. * @phba: HBA buffer was allocated for
  302. * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
  303. *
  304. * Description: Frees both the container and the DMA-mapped buffer returned by
  305. * lpfc_els_hbq_alloc.
  306. *
  307. * Notes: Can be called with or without locks held.
  308. *
  309. * Returns: None
  310. **/
  311. void
  312. lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
  313. {
  314. pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
  315. kfree(hbqbp);
  316. return;
  317. }
  318. /**
  319. * lpfc_in_buf_free: Free a DMA buffer
  320. * @phba: HBA buffer is associated with
  321. * @mp: Buffer to free
  322. *
  323. * Description: Frees the given DMA buffer in the appropriate way given if the
  324. * HBA is running in SLI3 mode with HBQs enabled.
  325. *
  326. * Notes: Takes phba->hbalock. Can be called with or without other locks held.
  327. *
  328. * Returns: None
  329. **/
  330. void
  331. lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
  332. {
  333. struct hbq_dmabuf *hbq_entry;
  334. unsigned long flags;
  335. if (!mp)
  336. return;
  337. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  338. /* Check whether HBQ is still in use */
  339. spin_lock_irqsave(&phba->hbalock, flags);
  340. if (!phba->hbq_in_use) {
  341. spin_unlock_irqrestore(&phba->hbalock, flags);
  342. return;
  343. }
  344. hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
  345. list_del(&hbq_entry->dbuf.list);
  346. if (hbq_entry->tag == -1) {
  347. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  348. (phba, hbq_entry);
  349. } else {
  350. lpfc_sli_free_hbq(phba, hbq_entry);
  351. }
  352. spin_unlock_irqrestore(&phba->hbalock, flags);
  353. } else {
  354. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  355. kfree(mp);
  356. }
  357. return;
  358. }