lpfc_mem.c 15 KB

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