lpfc_mem.c 15 KB

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