lpfc_mem.c 15 KB

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