lpfc_mem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2009 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_hw4.h"
  28. #include "lpfc_hw.h"
  29. #include "lpfc_sli.h"
  30. #include "lpfc_sli4.h"
  31. #include "lpfc_nl.h"
  32. #include "lpfc_disc.h"
  33. #include "lpfc_scsi.h"
  34. #include "lpfc.h"
  35. #include "lpfc_crtn.h"
  36. #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
  37. #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
  38. /**
  39. * lpfc_mem_alloc - create and allocate all PCI and memory pools
  40. * @phba: HBA to allocate pools for
  41. *
  42. * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
  43. * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
  44. * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
  45. *
  46. * Notes: Not interrupt-safe. Must be called with no locks held. If any
  47. * allocation fails, frees all successfully allocated memory before returning.
  48. *
  49. * Returns:
  50. * 0 on success
  51. * -ENOMEM on failure (if any memory allocations fail)
  52. **/
  53. int
  54. lpfc_mem_alloc(struct lpfc_hba *phba, int align)
  55. {
  56. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  57. int longs;
  58. int i;
  59. if (phba->sli_rev == LPFC_SLI_REV4)
  60. phba->lpfc_scsi_dma_buf_pool =
  61. pci_pool_create("lpfc_scsi_dma_buf_pool",
  62. phba->pcidev,
  63. phba->cfg_sg_dma_buf_size,
  64. phba->cfg_sg_dma_buf_size,
  65. 0);
  66. else
  67. phba->lpfc_scsi_dma_buf_pool =
  68. pci_pool_create("lpfc_scsi_dma_buf_pool",
  69. phba->pcidev, phba->cfg_sg_dma_buf_size,
  70. align, 0);
  71. if (!phba->lpfc_scsi_dma_buf_pool)
  72. goto fail;
  73. phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
  74. LPFC_BPL_SIZE,
  75. align, 0);
  76. if (!phba->lpfc_mbuf_pool)
  77. goto fail_free_dma_buf_pool;
  78. pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
  79. LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
  80. if (!pool->elements)
  81. goto fail_free_lpfc_mbuf_pool;
  82. pool->max_count = 0;
  83. pool->current_count = 0;
  84. for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
  85. pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
  86. GFP_KERNEL, &pool->elements[i].phys);
  87. if (!pool->elements[i].virt)
  88. goto fail_free_mbuf_pool;
  89. pool->max_count++;
  90. pool->current_count++;
  91. }
  92. phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  93. sizeof(LPFC_MBOXQ_t));
  94. if (!phba->mbox_mem_pool)
  95. goto fail_free_mbuf_pool;
  96. phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  97. sizeof(struct lpfc_nodelist));
  98. if (!phba->nlp_mem_pool)
  99. goto fail_free_mbox_pool;
  100. phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
  101. phba->pcidev,
  102. LPFC_HDR_BUF_SIZE, align, 0);
  103. if (!phba->lpfc_hrb_pool)
  104. goto fail_free_nlp_mem_pool;
  105. phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
  106. phba->pcidev,
  107. LPFC_DATA_BUF_SIZE, align, 0);
  108. if (!phba->lpfc_drb_pool)
  109. goto fail_free_hbq_pool;
  110. /* vpi zero is reserved for the physical port so add 1 to max */
  111. longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  112. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
  113. if (!phba->vpi_bmask)
  114. goto fail_free_dbq_pool;
  115. return 0;
  116. fail_free_dbq_pool:
  117. pci_pool_destroy(phba->lpfc_drb_pool);
  118. phba->lpfc_drb_pool = NULL;
  119. fail_free_hbq_pool:
  120. pci_pool_destroy(phba->lpfc_hrb_pool);
  121. phba->lpfc_hrb_pool = NULL;
  122. fail_free_nlp_mem_pool:
  123. mempool_destroy(phba->nlp_mem_pool);
  124. phba->nlp_mem_pool = NULL;
  125. fail_free_mbox_pool:
  126. mempool_destroy(phba->mbox_mem_pool);
  127. phba->mbox_mem_pool = NULL;
  128. fail_free_mbuf_pool:
  129. while (i--)
  130. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  131. pool->elements[i].phys);
  132. kfree(pool->elements);
  133. fail_free_lpfc_mbuf_pool:
  134. pci_pool_destroy(phba->lpfc_mbuf_pool);
  135. phba->lpfc_mbuf_pool = NULL;
  136. fail_free_dma_buf_pool:
  137. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  138. phba->lpfc_scsi_dma_buf_pool = NULL;
  139. fail:
  140. return -ENOMEM;
  141. }
  142. /**
  143. * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
  144. * @phba: HBA to free memory for
  145. *
  146. * Description: Free the memory allocated by lpfc_mem_alloc routine. This
  147. * routine is a the counterpart of lpfc_mem_alloc.
  148. *
  149. * Returns: None
  150. **/
  151. void
  152. lpfc_mem_free(struct lpfc_hba *phba)
  153. {
  154. int i;
  155. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  156. /* Free VPI bitmask memory */
  157. kfree(phba->vpi_bmask);
  158. /* Free HBQ pools */
  159. lpfc_sli_hbqbuf_free_all(phba);
  160. pci_pool_destroy(phba->lpfc_drb_pool);
  161. phba->lpfc_drb_pool = NULL;
  162. pci_pool_destroy(phba->lpfc_hrb_pool);
  163. phba->lpfc_hrb_pool = NULL;
  164. /* Free NLP memory pool */
  165. mempool_destroy(phba->nlp_mem_pool);
  166. phba->nlp_mem_pool = NULL;
  167. /* Free mbox memory pool */
  168. mempool_destroy(phba->mbox_mem_pool);
  169. phba->mbox_mem_pool = NULL;
  170. /* Free MBUF memory pool */
  171. for (i = 0; i < pool->current_count; i++)
  172. pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  173. pool->elements[i].phys);
  174. kfree(pool->elements);
  175. pci_pool_destroy(phba->lpfc_mbuf_pool);
  176. phba->lpfc_mbuf_pool = NULL;
  177. /* Free DMA buffer memory pool */
  178. pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
  179. phba->lpfc_scsi_dma_buf_pool = NULL;
  180. return;
  181. }
  182. /**
  183. * lpfc_mem_free_all - Frees all PCI and driver memory
  184. * @phba: HBA to free memory for
  185. *
  186. * Description: Free memory from PCI and driver memory pools and also those
  187. * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
  188. * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
  189. * the VPI bitmask.
  190. *
  191. * Returns: None
  192. **/
  193. void
  194. lpfc_mem_free_all(struct lpfc_hba *phba)
  195. {
  196. struct lpfc_sli *psli = &phba->sli;
  197. LPFC_MBOXQ_t *mbox, *next_mbox;
  198. struct lpfc_dmabuf *mp;
  199. /* Free memory used in mailbox queue back to mailbox memory pool */
  200. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
  201. mp = (struct lpfc_dmabuf *) (mbox->context1);
  202. if (mp) {
  203. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  204. kfree(mp);
  205. }
  206. list_del(&mbox->list);
  207. mempool_free(mbox, phba->mbox_mem_pool);
  208. }
  209. /* Free memory used in mailbox cmpl list back to mailbox memory pool */
  210. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
  211. mp = (struct lpfc_dmabuf *) (mbox->context1);
  212. if (mp) {
  213. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  214. kfree(mp);
  215. }
  216. list_del(&mbox->list);
  217. mempool_free(mbox, phba->mbox_mem_pool);
  218. }
  219. /* Free the active mailbox command back to the mailbox memory pool */
  220. spin_lock_irq(&phba->hbalock);
  221. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  222. spin_unlock_irq(&phba->hbalock);
  223. if (psli->mbox_active) {
  224. mbox = psli->mbox_active;
  225. mp = (struct lpfc_dmabuf *) (mbox->context1);
  226. if (mp) {
  227. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  228. kfree(mp);
  229. }
  230. mempool_free(mbox, phba->mbox_mem_pool);
  231. psli->mbox_active = NULL;
  232. }
  233. /* Free and destroy all the allocated memory pools */
  234. lpfc_mem_free(phba);
  235. /* Free the iocb lookup array */
  236. kfree(psli->iocbq_lookup);
  237. psli->iocbq_lookup = NULL;
  238. return;
  239. }
  240. /**
  241. * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
  242. * @phba: HBA which owns the pool to allocate from
  243. * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
  244. * @handle: used to return the DMA-mapped address of the mbuf
  245. *
  246. * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
  247. * Allocates from generic pci_pool_alloc function first and if that fails and
  248. * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
  249. * HBA's pool.
  250. *
  251. * Notes: Not interrupt-safe. Must be called with no locks held. Takes
  252. * phba->hbalock.
  253. *
  254. * Returns:
  255. * pointer to the allocated mbuf on success
  256. * NULL on failure
  257. **/
  258. void *
  259. lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  260. {
  261. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  262. unsigned long iflags;
  263. void *ret;
  264. ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
  265. spin_lock_irqsave(&phba->hbalock, iflags);
  266. if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
  267. pool->current_count--;
  268. ret = pool->elements[pool->current_count].virt;
  269. *handle = pool->elements[pool->current_count].phys;
  270. }
  271. spin_unlock_irqrestore(&phba->hbalock, iflags);
  272. return ret;
  273. }
  274. /**
  275. * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
  276. * @phba: HBA which owns the pool to return to
  277. * @virt: mbuf to free
  278. * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
  279. *
  280. * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
  281. * it is below its max_count, frees the mbuf otherwise.
  282. *
  283. * Notes: Must be called with phba->hbalock held to synchronize access to
  284. * lpfc_mbuf_safety_pool.
  285. *
  286. * Returns: None
  287. **/
  288. void
  289. __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  290. {
  291. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  292. if (pool->current_count < pool->max_count) {
  293. pool->elements[pool->current_count].virt = virt;
  294. pool->elements[pool->current_count].phys = dma;
  295. pool->current_count++;
  296. } else {
  297. pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
  298. }
  299. return;
  300. }
  301. /**
  302. * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
  303. * @phba: HBA which owns the pool to return to
  304. * @virt: mbuf to free
  305. * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
  306. *
  307. * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
  308. * it is below its max_count, frees the mbuf otherwise.
  309. *
  310. * Notes: Takes phba->hbalock. Can be called with or without other locks held.
  311. *
  312. * Returns: None
  313. **/
  314. void
  315. lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  316. {
  317. unsigned long iflags;
  318. spin_lock_irqsave(&phba->hbalock, iflags);
  319. __lpfc_mbuf_free(phba, virt, dma);
  320. spin_unlock_irqrestore(&phba->hbalock, iflags);
  321. return;
  322. }
  323. /**
  324. * lpfc_els_hbq_alloc - Allocate an HBQ buffer
  325. * @phba: HBA to allocate HBQ buffer for
  326. *
  327. * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
  328. * pool along a non-DMA-mapped container for it.
  329. *
  330. * Notes: Not interrupt-safe. Must be called with no locks held.
  331. *
  332. * Returns:
  333. * pointer to HBQ on success
  334. * NULL on failure
  335. **/
  336. struct hbq_dmabuf *
  337. lpfc_els_hbq_alloc(struct lpfc_hba *phba)
  338. {
  339. struct hbq_dmabuf *hbqbp;
  340. hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
  341. if (!hbqbp)
  342. return NULL;
  343. hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
  344. &hbqbp->dbuf.phys);
  345. if (!hbqbp->dbuf.virt) {
  346. kfree(hbqbp);
  347. return NULL;
  348. }
  349. hbqbp->size = LPFC_BPL_SIZE;
  350. return hbqbp;
  351. }
  352. /**
  353. * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
  354. * @phba: HBA buffer was allocated for
  355. * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
  356. *
  357. * Description: Frees both the container and the DMA-mapped buffer returned by
  358. * lpfc_els_hbq_alloc.
  359. *
  360. * Notes: Can be called with or without locks held.
  361. *
  362. * Returns: None
  363. **/
  364. void
  365. lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
  366. {
  367. pci_pool_free(phba->lpfc_hrb_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
  368. kfree(hbqbp);
  369. return;
  370. }
  371. /**
  372. * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
  373. * @phba: HBA to allocate a receive buffer for
  374. *
  375. * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
  376. * pool along a non-DMA-mapped container for it.
  377. *
  378. * Notes: Not interrupt-safe. Must be called with no locks held.
  379. *
  380. * Returns:
  381. * pointer to HBQ on success
  382. * NULL on failure
  383. **/
  384. struct hbq_dmabuf *
  385. lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
  386. {
  387. struct hbq_dmabuf *dma_buf;
  388. dma_buf = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
  389. if (!dma_buf)
  390. return NULL;
  391. dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
  392. &dma_buf->hbuf.phys);
  393. if (!dma_buf->hbuf.virt) {
  394. kfree(dma_buf);
  395. return NULL;
  396. }
  397. dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
  398. &dma_buf->dbuf.phys);
  399. if (!dma_buf->dbuf.virt) {
  400. pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
  401. dma_buf->hbuf.phys);
  402. kfree(dma_buf);
  403. return NULL;
  404. }
  405. dma_buf->size = LPFC_BPL_SIZE;
  406. return dma_buf;
  407. }
  408. /**
  409. * lpfc_sli4_rb_free - Frees a receive buffer
  410. * @phba: HBA buffer was allocated for
  411. * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
  412. *
  413. * Description: Frees both the container and the DMA-mapped buffers returned by
  414. * lpfc_sli4_rb_alloc.
  415. *
  416. * Notes: Can be called with or without locks held.
  417. *
  418. * Returns: None
  419. **/
  420. void
  421. lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
  422. {
  423. pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
  424. pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
  425. kfree(dmab);
  426. return;
  427. }
  428. /**
  429. * lpfc_in_buf_free - Free a DMA buffer
  430. * @phba: HBA buffer is associated with
  431. * @mp: Buffer to free
  432. *
  433. * Description: Frees the given DMA buffer in the appropriate way given if the
  434. * HBA is running in SLI3 mode with HBQs enabled.
  435. *
  436. * Notes: Takes phba->hbalock. Can be called with or without other locks held.
  437. *
  438. * Returns: None
  439. **/
  440. void
  441. lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
  442. {
  443. struct hbq_dmabuf *hbq_entry;
  444. unsigned long flags;
  445. if (!mp)
  446. return;
  447. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  448. /* Check whether HBQ is still in use */
  449. spin_lock_irqsave(&phba->hbalock, flags);
  450. if (!phba->hbq_in_use) {
  451. spin_unlock_irqrestore(&phba->hbalock, flags);
  452. return;
  453. }
  454. hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
  455. list_del(&hbq_entry->dbuf.list);
  456. if (hbq_entry->tag == -1) {
  457. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  458. (phba, hbq_entry);
  459. } else {
  460. lpfc_sli_free_hbq(phba, hbq_entry);
  461. }
  462. spin_unlock_irqrestore(&phba->hbalock, flags);
  463. } else {
  464. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  465. kfree(mp);
  466. }
  467. return;
  468. }