lpfc_mem.c 15 KB

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