lpfc_mem.c 12 KB

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