lpfc_mem.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2017-2022 Broadcom. All Rights Reserved. The term *
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
  6. * Copyright (C) 2004-2014 Emulex. All rights reserved. *
  7. * EMULEX and SLI are trademarks of Emulex. *
  8. * www.broadcom.com *
  9. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  10. * *
  11. * This program is free software; you can redistribute it and/or *
  12. * modify it under the terms of version 2 of the GNU General *
  13. * Public License as published by the Free Software Foundation. *
  14. * This program is distributed in the hope that it will be useful. *
  15. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  16. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  17. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  18. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  20. * more details, a copy of which can be found in the file COPYING *
  21. * included with this package. *
  22. *******************************************************************/
  23. #include <linux/mempool.h>
  24. #include <linux/slab.h>
  25. #include <linux/pci.h>
  26. #include <linux/interrupt.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_transport_fc.h>
  30. #include <scsi/fc/fc_fs.h>
  31. #include "lpfc_hw4.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_sli4.h"
  35. #include "lpfc_nl.h"
  36. #include "lpfc_disc.h"
  37. #include "lpfc.h"
  38. #include "lpfc_scsi.h"
  39. #include "lpfc_crtn.h"
  40. #include "lpfc_logmsg.h"
  41. #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
  42. #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
  43. #define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */
  44. #define LPFC_RRQ_POOL_SIZE 256 /* max elements in non-DMA pool */
  45. #define LPFC_MBX_POOL_SIZE 256 /* max elements in MBX non-DMA pool */
  46. int
  47. lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
  48. size_t bytes;
  49. int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
  50. if (max_xri <= 0)
  51. return -ENOMEM;
  52. bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) *
  53. sizeof(unsigned long);
  54. phba->cfg_rrq_xri_bitmap_sz = bytes;
  55. phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  56. bytes);
  57. if (!phba->active_rrq_pool)
  58. return -ENOMEM;
  59. else
  60. return 0;
  61. }
  62. /**
  63. * lpfc_mem_alloc - create and allocate all PCI and memory pools
  64. * @phba: HBA to allocate pools for
  65. * @align: alignment requirement for blocks; must be a power of two
  66. *
  67. * Description: Creates and allocates PCI pools lpfc_mbuf_pool,
  68. * lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
  69. * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
  70. *
  71. * Notes: Not interrupt-safe. Must be called with no locks held. If any
  72. * allocation fails, frees all successfully allocated memory before returning.
  73. *
  74. * Returns:
  75. * 0 on success
  76. * -ENOMEM on failure (if any memory allocations fail)
  77. **/
  78. int
  79. lpfc_mem_alloc(struct lpfc_hba *phba, int align)
  80. {
  81. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  82. int i;
  83. phba->lpfc_mbuf_pool = dma_pool_create("lpfc_mbuf_pool", &phba->pcidev->dev,
  84. LPFC_BPL_SIZE,
  85. align, 0);
  86. if (!phba->lpfc_mbuf_pool)
  87. goto fail;
  88. pool->elements = kmalloc_array(LPFC_MBUF_POOL_SIZE,
  89. sizeof(struct lpfc_dmabuf),
  90. GFP_KERNEL);
  91. if (!pool->elements)
  92. goto fail_free_lpfc_mbuf_pool;
  93. pool->max_count = 0;
  94. pool->current_count = 0;
  95. for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
  96. pool->elements[i].virt = dma_pool_alloc(phba->lpfc_mbuf_pool,
  97. GFP_KERNEL, &pool->elements[i].phys);
  98. if (!pool->elements[i].virt)
  99. goto fail_free_mbuf_pool;
  100. pool->max_count++;
  101. pool->current_count++;
  102. }
  103. phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MBX_POOL_SIZE,
  104. sizeof(LPFC_MBOXQ_t));
  105. if (!phba->mbox_mem_pool)
  106. goto fail_free_mbuf_pool;
  107. phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
  108. sizeof(struct lpfc_nodelist));
  109. if (!phba->nlp_mem_pool)
  110. goto fail_free_mbox_pool;
  111. if (phba->sli_rev == LPFC_SLI_REV4) {
  112. phba->rrq_pool =
  113. mempool_create_kmalloc_pool(LPFC_RRQ_POOL_SIZE,
  114. sizeof(struct lpfc_node_rrq));
  115. if (!phba->rrq_pool)
  116. goto fail_free_nlp_mem_pool;
  117. phba->lpfc_hrb_pool = dma_pool_create("lpfc_hrb_pool",
  118. &phba->pcidev->dev,
  119. LPFC_HDR_BUF_SIZE, align, 0);
  120. if (!phba->lpfc_hrb_pool)
  121. goto fail_free_rrq_mem_pool;
  122. phba->lpfc_drb_pool = dma_pool_create("lpfc_drb_pool",
  123. &phba->pcidev->dev,
  124. LPFC_DATA_BUF_SIZE, align, 0);
  125. if (!phba->lpfc_drb_pool)
  126. goto fail_free_hrb_pool;
  127. phba->lpfc_hbq_pool = NULL;
  128. } else {
  129. phba->lpfc_hbq_pool = dma_pool_create("lpfc_hbq_pool",
  130. &phba->pcidev->dev, LPFC_BPL_SIZE, align, 0);
  131. if (!phba->lpfc_hbq_pool)
  132. goto fail_free_nlp_mem_pool;
  133. phba->lpfc_hrb_pool = NULL;
  134. phba->lpfc_drb_pool = NULL;
  135. }
  136. if (phba->cfg_EnableXLane) {
  137. phba->device_data_mem_pool = mempool_create_kmalloc_pool(
  138. LPFC_DEVICE_DATA_POOL_SIZE,
  139. sizeof(struct lpfc_device_data));
  140. if (!phba->device_data_mem_pool)
  141. goto fail_free_drb_pool;
  142. } else {
  143. phba->device_data_mem_pool = NULL;
  144. }
  145. return 0;
  146. fail_free_drb_pool:
  147. dma_pool_destroy(phba->lpfc_drb_pool);
  148. phba->lpfc_drb_pool = NULL;
  149. fail_free_hrb_pool:
  150. dma_pool_destroy(phba->lpfc_hrb_pool);
  151. phba->lpfc_hrb_pool = NULL;
  152. fail_free_rrq_mem_pool:
  153. mempool_destroy(phba->rrq_pool);
  154. phba->rrq_pool = NULL;
  155. fail_free_nlp_mem_pool:
  156. mempool_destroy(phba->nlp_mem_pool);
  157. phba->nlp_mem_pool = NULL;
  158. fail_free_mbox_pool:
  159. mempool_destroy(phba->mbox_mem_pool);
  160. phba->mbox_mem_pool = NULL;
  161. fail_free_mbuf_pool:
  162. while (i--)
  163. dma_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  164. pool->elements[i].phys);
  165. kfree(pool->elements);
  166. fail_free_lpfc_mbuf_pool:
  167. dma_pool_destroy(phba->lpfc_mbuf_pool);
  168. phba->lpfc_mbuf_pool = NULL;
  169. fail:
  170. return -ENOMEM;
  171. }
  172. int
  173. lpfc_nvmet_mem_alloc(struct lpfc_hba *phba)
  174. {
  175. phba->lpfc_nvmet_drb_pool =
  176. dma_pool_create("lpfc_nvmet_drb_pool",
  177. &phba->pcidev->dev, LPFC_NVMET_DATA_BUF_SIZE,
  178. SGL_ALIGN_SZ, 0);
  179. if (!phba->lpfc_nvmet_drb_pool) {
  180. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  181. "6024 Can't enable NVME Target - no memory\n");
  182. return -ENOMEM;
  183. }
  184. return 0;
  185. }
  186. /**
  187. * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
  188. * @phba: HBA to free memory for
  189. *
  190. * Description: Free the memory allocated by lpfc_mem_alloc routine. This
  191. * routine is a the counterpart of lpfc_mem_alloc.
  192. *
  193. * Returns: None
  194. **/
  195. void
  196. lpfc_mem_free(struct lpfc_hba *phba)
  197. {
  198. int i;
  199. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  200. struct lpfc_device_data *device_data;
  201. /* Free HBQ pools */
  202. lpfc_sli_hbqbuf_free_all(phba);
  203. dma_pool_destroy(phba->lpfc_nvmet_drb_pool);
  204. phba->lpfc_nvmet_drb_pool = NULL;
  205. dma_pool_destroy(phba->lpfc_drb_pool);
  206. phba->lpfc_drb_pool = NULL;
  207. dma_pool_destroy(phba->lpfc_hrb_pool);
  208. phba->lpfc_hrb_pool = NULL;
  209. dma_pool_destroy(phba->lpfc_hbq_pool);
  210. phba->lpfc_hbq_pool = NULL;
  211. mempool_destroy(phba->rrq_pool);
  212. phba->rrq_pool = NULL;
  213. /* Free NLP memory pool */
  214. mempool_destroy(phba->nlp_mem_pool);
  215. phba->nlp_mem_pool = NULL;
  216. if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
  217. mempool_destroy(phba->active_rrq_pool);
  218. phba->active_rrq_pool = NULL;
  219. }
  220. /* Free mbox memory pool */
  221. mempool_destroy(phba->mbox_mem_pool);
  222. phba->mbox_mem_pool = NULL;
  223. /* Free MBUF memory pool */
  224. for (i = 0; i < pool->current_count; i++)
  225. dma_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
  226. pool->elements[i].phys);
  227. kfree(pool->elements);
  228. dma_pool_destroy(phba->lpfc_mbuf_pool);
  229. phba->lpfc_mbuf_pool = NULL;
  230. /* Free Device Data memory pool */
  231. if (phba->device_data_mem_pool) {
  232. /* Ensure all objects have been returned to the pool */
  233. while (!list_empty(&phba->luns)) {
  234. device_data = list_first_entry(&phba->luns,
  235. struct lpfc_device_data,
  236. listentry);
  237. list_del(&device_data->listentry);
  238. mempool_free(device_data, phba->device_data_mem_pool);
  239. }
  240. mempool_destroy(phba->device_data_mem_pool);
  241. }
  242. phba->device_data_mem_pool = NULL;
  243. return;
  244. }
  245. /**
  246. * lpfc_mem_free_all - Frees all PCI and driver memory
  247. * @phba: HBA to free memory for
  248. *
  249. * Description: Free memory from PCI and driver memory pools and also those
  250. * used : lpfc_sg_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
  251. * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
  252. * the VPI bitmask.
  253. *
  254. * Returns: None
  255. **/
  256. void
  257. lpfc_mem_free_all(struct lpfc_hba *phba)
  258. {
  259. struct lpfc_sli *psli = &phba->sli;
  260. LPFC_MBOXQ_t *mbox, *next_mbox;
  261. struct lpfc_dmabuf *mp;
  262. /* Free memory used in mailbox queue back to mailbox memory pool */
  263. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
  264. mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
  265. if (mp) {
  266. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  267. kfree(mp);
  268. }
  269. list_del(&mbox->list);
  270. mempool_free(mbox, phba->mbox_mem_pool);
  271. }
  272. /* Free memory used in mailbox cmpl list back to mailbox memory pool */
  273. list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
  274. mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
  275. if (mp) {
  276. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  277. kfree(mp);
  278. }
  279. list_del(&mbox->list);
  280. mempool_free(mbox, phba->mbox_mem_pool);
  281. }
  282. /* Free the active mailbox command back to the mailbox memory pool */
  283. spin_lock_irq(&phba->hbalock);
  284. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  285. spin_unlock_irq(&phba->hbalock);
  286. if (psli->mbox_active) {
  287. mbox = psli->mbox_active;
  288. mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
  289. if (mp) {
  290. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  291. kfree(mp);
  292. }
  293. mempool_free(mbox, phba->mbox_mem_pool);
  294. psli->mbox_active = NULL;
  295. }
  296. /* Free and destroy all the allocated memory pools */
  297. lpfc_mem_free(phba);
  298. /* Free DMA buffer memory pool */
  299. dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
  300. phba->lpfc_sg_dma_buf_pool = NULL;
  301. dma_pool_destroy(phba->lpfc_cmd_rsp_buf_pool);
  302. phba->lpfc_cmd_rsp_buf_pool = NULL;
  303. /* Free Congestion Data buffer */
  304. if (phba->cgn_i) {
  305. dma_free_coherent(&phba->pcidev->dev,
  306. sizeof(struct lpfc_cgn_info),
  307. phba->cgn_i->virt, phba->cgn_i->phys);
  308. kfree(phba->cgn_i);
  309. phba->cgn_i = NULL;
  310. }
  311. /* Free RX Monitor */
  312. if (phba->rx_monitor) {
  313. lpfc_rx_monitor_destroy_ring(phba->rx_monitor);
  314. kfree(phba->rx_monitor);
  315. phba->rx_monitor = NULL;
  316. }
  317. /* Free the iocb lookup array */
  318. kfree(psli->iocbq_lookup);
  319. psli->iocbq_lookup = NULL;
  320. return;
  321. }
  322. /**
  323. * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
  324. * @phba: HBA which owns the pool to allocate from
  325. * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
  326. * @handle: used to return the DMA-mapped address of the mbuf
  327. *
  328. * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
  329. * Allocates from generic dma_pool_alloc function first and if that fails and
  330. * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
  331. * HBA's pool.
  332. *
  333. * Notes: Not interrupt-safe. Must be called with no locks held. Takes
  334. * phba->hbalock.
  335. *
  336. * Returns:
  337. * pointer to the allocated mbuf on success
  338. * NULL on failure
  339. **/
  340. void *
  341. lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  342. {
  343. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  344. unsigned long iflags;
  345. void *ret;
  346. ret = dma_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
  347. spin_lock_irqsave(&phba->hbalock, iflags);
  348. if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
  349. pool->current_count--;
  350. ret = pool->elements[pool->current_count].virt;
  351. *handle = pool->elements[pool->current_count].phys;
  352. }
  353. spin_unlock_irqrestore(&phba->hbalock, iflags);
  354. return ret;
  355. }
  356. /**
  357. * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
  358. * @phba: HBA which owns the pool to return to
  359. * @virt: mbuf to free
  360. * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
  361. *
  362. * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
  363. * it is below its max_count, frees the mbuf otherwise.
  364. *
  365. * Notes: Must be called with phba->hbalock held to synchronize access to
  366. * lpfc_mbuf_safety_pool.
  367. *
  368. * Returns: None
  369. **/
  370. void
  371. __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  372. {
  373. struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
  374. if (pool->current_count < pool->max_count) {
  375. pool->elements[pool->current_count].virt = virt;
  376. pool->elements[pool->current_count].phys = dma;
  377. pool->current_count++;
  378. } else {
  379. dma_pool_free(phba->lpfc_mbuf_pool, virt, dma);
  380. }
  381. return;
  382. }
  383. /**
  384. * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
  385. * @phba: HBA which owns the pool to return to
  386. * @virt: mbuf to free
  387. * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
  388. *
  389. * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
  390. * it is below its max_count, frees the mbuf otherwise.
  391. *
  392. * Notes: Takes phba->hbalock. Can be called with or without other locks held.
  393. *
  394. * Returns: None
  395. **/
  396. void
  397. lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
  398. {
  399. unsigned long iflags;
  400. spin_lock_irqsave(&phba->hbalock, iflags);
  401. __lpfc_mbuf_free(phba, virt, dma);
  402. spin_unlock_irqrestore(&phba->hbalock, iflags);
  403. return;
  404. }
  405. /**
  406. * lpfc_nvmet_buf_alloc - Allocate an nvmet_buf from the
  407. * lpfc_sg_dma_buf_pool PCI pool
  408. * @phba: HBA which owns the pool to allocate from
  409. * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
  410. * @handle: used to return the DMA-mapped address of the nvmet_buf
  411. *
  412. * Description: Allocates a DMA-mapped buffer from the lpfc_sg_dma_buf_pool
  413. * PCI pool. Allocates from generic dma_pool_alloc function.
  414. *
  415. * Returns:
  416. * pointer to the allocated nvmet_buf on success
  417. * NULL on failure
  418. **/
  419. void *
  420. lpfc_nvmet_buf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
  421. {
  422. void *ret;
  423. ret = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool, GFP_KERNEL, handle);
  424. return ret;
  425. }
  426. /**
  427. * lpfc_nvmet_buf_free - Free an nvmet_buf from the lpfc_sg_dma_buf_pool
  428. * PCI pool
  429. * @phba: HBA which owns the pool to return to
  430. * @virt: nvmet_buf to free
  431. * @dma: the DMA-mapped address of the lpfc_sg_dma_buf_pool to be freed
  432. *
  433. * Returns: None
  434. **/
  435. void
  436. lpfc_nvmet_buf_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma)
  437. {
  438. dma_pool_free(phba->lpfc_sg_dma_buf_pool, virt, dma);
  439. }
  440. /**
  441. * lpfc_els_hbq_alloc - Allocate an HBQ buffer
  442. * @phba: HBA to allocate HBQ buffer for
  443. *
  444. * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
  445. * pool along a non-DMA-mapped container for it.
  446. *
  447. * Notes: Not interrupt-safe. Must be called with no locks held.
  448. *
  449. * Returns:
  450. * pointer to HBQ on success
  451. * NULL on failure
  452. **/
  453. struct hbq_dmabuf *
  454. lpfc_els_hbq_alloc(struct lpfc_hba *phba)
  455. {
  456. struct hbq_dmabuf *hbqbp;
  457. hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
  458. if (!hbqbp)
  459. return NULL;
  460. hbqbp->dbuf.virt = dma_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
  461. &hbqbp->dbuf.phys);
  462. if (!hbqbp->dbuf.virt) {
  463. kfree(hbqbp);
  464. return NULL;
  465. }
  466. hbqbp->total_size = LPFC_BPL_SIZE;
  467. return hbqbp;
  468. }
  469. /**
  470. * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
  471. * @phba: HBA buffer was allocated for
  472. * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
  473. *
  474. * Description: Frees both the container and the DMA-mapped buffer returned by
  475. * lpfc_els_hbq_alloc.
  476. *
  477. * Notes: Can be called with or without locks held.
  478. *
  479. * Returns: None
  480. **/
  481. void
  482. lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
  483. {
  484. dma_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
  485. kfree(hbqbp);
  486. return;
  487. }
  488. /**
  489. * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
  490. * @phba: HBA to allocate a receive buffer for
  491. *
  492. * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
  493. * pool along a non-DMA-mapped container for it.
  494. *
  495. * Notes: Not interrupt-safe. Must be called with no locks held.
  496. *
  497. * Returns:
  498. * pointer to HBQ on success
  499. * NULL on failure
  500. **/
  501. struct hbq_dmabuf *
  502. lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
  503. {
  504. struct hbq_dmabuf *dma_buf;
  505. dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
  506. if (!dma_buf)
  507. return NULL;
  508. dma_buf->hbuf.virt = dma_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
  509. &dma_buf->hbuf.phys);
  510. if (!dma_buf->hbuf.virt) {
  511. kfree(dma_buf);
  512. return NULL;
  513. }
  514. dma_buf->dbuf.virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
  515. &dma_buf->dbuf.phys);
  516. if (!dma_buf->dbuf.virt) {
  517. dma_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
  518. dma_buf->hbuf.phys);
  519. kfree(dma_buf);
  520. return NULL;
  521. }
  522. dma_buf->total_size = LPFC_DATA_BUF_SIZE;
  523. return dma_buf;
  524. }
  525. /**
  526. * lpfc_sli4_rb_free - Frees a receive buffer
  527. * @phba: HBA buffer was allocated for
  528. * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
  529. *
  530. * Description: Frees both the container and the DMA-mapped buffers returned by
  531. * lpfc_sli4_rb_alloc.
  532. *
  533. * Notes: Can be called with or without locks held.
  534. *
  535. * Returns: None
  536. **/
  537. void
  538. lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
  539. {
  540. dma_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
  541. dma_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
  542. kfree(dmab);
  543. }
  544. /**
  545. * lpfc_sli4_nvmet_alloc - Allocate an SLI4 Receive buffer
  546. * @phba: HBA to allocate a receive buffer for
  547. *
  548. * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
  549. * pool along a non-DMA-mapped container for it.
  550. *
  551. * Returns:
  552. * pointer to HBQ on success
  553. * NULL on failure
  554. **/
  555. struct rqb_dmabuf *
  556. lpfc_sli4_nvmet_alloc(struct lpfc_hba *phba)
  557. {
  558. struct rqb_dmabuf *dma_buf;
  559. dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL);
  560. if (!dma_buf)
  561. return NULL;
  562. dma_buf->hbuf.virt = dma_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
  563. &dma_buf->hbuf.phys);
  564. if (!dma_buf->hbuf.virt) {
  565. kfree(dma_buf);
  566. return NULL;
  567. }
  568. dma_buf->dbuf.virt = dma_pool_alloc(phba->lpfc_nvmet_drb_pool,
  569. GFP_KERNEL, &dma_buf->dbuf.phys);
  570. if (!dma_buf->dbuf.virt) {
  571. dma_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
  572. dma_buf->hbuf.phys);
  573. kfree(dma_buf);
  574. return NULL;
  575. }
  576. dma_buf->total_size = LPFC_NVMET_DATA_BUF_SIZE;
  577. return dma_buf;
  578. }
  579. /**
  580. * lpfc_sli4_nvmet_free - Frees a receive buffer
  581. * @phba: HBA buffer was allocated for
  582. * @dmab: DMA Buffer container returned by lpfc_sli4_rbq_alloc
  583. *
  584. * Description: Frees both the container and the DMA-mapped buffers returned by
  585. * lpfc_sli4_nvmet_alloc.
  586. *
  587. * Notes: Can be called with or without locks held.
  588. *
  589. * Returns: None
  590. **/
  591. void
  592. lpfc_sli4_nvmet_free(struct lpfc_hba *phba, struct rqb_dmabuf *dmab)
  593. {
  594. dma_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
  595. dma_pool_free(phba->lpfc_nvmet_drb_pool,
  596. dmab->dbuf.virt, dmab->dbuf.phys);
  597. kfree(dmab);
  598. }
  599. /**
  600. * lpfc_in_buf_free - Free a DMA buffer
  601. * @phba: HBA buffer is associated with
  602. * @mp: Buffer to free
  603. *
  604. * Description: Frees the given DMA buffer in the appropriate way given if the
  605. * HBA is running in SLI3 mode with HBQs enabled.
  606. *
  607. * Notes: Takes phba->hbalock. Can be called with or without other locks held.
  608. *
  609. * Returns: None
  610. **/
  611. void
  612. lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
  613. {
  614. struct hbq_dmabuf *hbq_entry;
  615. unsigned long flags;
  616. if (!mp)
  617. return;
  618. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  619. hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
  620. /* Check whether HBQ is still in use */
  621. spin_lock_irqsave(&phba->hbalock, flags);
  622. if (!phba->hbq_in_use) {
  623. spin_unlock_irqrestore(&phba->hbalock, flags);
  624. return;
  625. }
  626. list_del(&hbq_entry->dbuf.list);
  627. if (hbq_entry->tag == -1) {
  628. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  629. (phba, hbq_entry);
  630. } else {
  631. lpfc_sli_free_hbq(phba, hbq_entry);
  632. }
  633. spin_unlock_irqrestore(&phba->hbalock, flags);
  634. } else {
  635. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  636. kfree(mp);
  637. }
  638. return;
  639. }
  640. /**
  641. * lpfc_rq_buf_free - Free a RQ DMA buffer
  642. * @phba: HBA buffer is associated with
  643. * @mp: Buffer to free
  644. *
  645. * Description: Frees the given DMA buffer in the appropriate way given by
  646. * reposting it to its associated RQ so it can be reused.
  647. *
  648. * Notes: Takes phba->hbalock. Can be called with or without other locks held.
  649. *
  650. * Returns: None
  651. **/
  652. void
  653. lpfc_rq_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
  654. {
  655. struct lpfc_rqb *rqbp;
  656. struct lpfc_rqe hrqe;
  657. struct lpfc_rqe drqe;
  658. struct rqb_dmabuf *rqb_entry;
  659. unsigned long flags;
  660. int rc;
  661. if (!mp)
  662. return;
  663. rqb_entry = container_of(mp, struct rqb_dmabuf, hbuf);
  664. rqbp = rqb_entry->hrq->rqbp;
  665. spin_lock_irqsave(&phba->hbalock, flags);
  666. list_del(&rqb_entry->hbuf.list);
  667. hrqe.address_lo = putPaddrLow(rqb_entry->hbuf.phys);
  668. hrqe.address_hi = putPaddrHigh(rqb_entry->hbuf.phys);
  669. drqe.address_lo = putPaddrLow(rqb_entry->dbuf.phys);
  670. drqe.address_hi = putPaddrHigh(rqb_entry->dbuf.phys);
  671. rc = lpfc_sli4_rq_put(rqb_entry->hrq, rqb_entry->drq, &hrqe, &drqe);
  672. if (rc < 0) {
  673. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  674. "6409 Cannot post to HRQ %d: %x %x %x "
  675. "DRQ %x %x\n",
  676. rqb_entry->hrq->queue_id,
  677. rqb_entry->hrq->host_index,
  678. rqb_entry->hrq->hba_index,
  679. rqb_entry->hrq->entry_count,
  680. rqb_entry->drq->host_index,
  681. rqb_entry->drq->hba_index);
  682. (rqbp->rqb_free_buffer)(phba, rqb_entry);
  683. } else {
  684. list_add_tail(&rqb_entry->hbuf.list, &rqbp->rqb_buffer_list);
  685. rqbp->buffer_count++;
  686. }
  687. spin_unlock_irqrestore(&phba->hbalock, flags);
  688. }