1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
6 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
7 * EMULEX and SLI are trademarks of Emulex. *
9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
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 *******************************************************************/
24 #include <linux/mempool.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_transport_fc.h>
32 #include <scsi/fc/fc_fs.h>
34 #include <linux/nvme-fc-driver.h>
39 #include "lpfc_sli4.h"
41 #include "lpfc_disc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc_nvme.h"
45 #include "lpfc_nvmet.h"
46 #include "lpfc_crtn.h"
47 #include "lpfc_logmsg.h"
49 #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
50 #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
51 #define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */
54 lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba
*phba
) {
56 int max_xri
= phba
->sli4_hba
.max_cfg_param
.max_xri
;
60 bytes
= ((BITS_PER_LONG
- 1 + max_xri
) / BITS_PER_LONG
) *
61 sizeof(unsigned long);
62 phba
->cfg_rrq_xri_bitmap_sz
= bytes
;
63 phba
->active_rrq_pool
= mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE
,
65 if (!phba
->active_rrq_pool
)
72 * lpfc_mem_alloc - create and allocate all PCI and memory pools
73 * @phba: HBA to allocate pools for
75 * Description: Creates and allocates PCI pools lpfc_sg_dma_buf_pool,
76 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
77 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
79 * Notes: Not interrupt-safe. Must be called with no locks held. If any
80 * allocation fails, frees all successfully allocated memory before returning.
84 * -ENOMEM on failure (if any memory allocations fail)
87 lpfc_mem_alloc(struct lpfc_hba
*phba
, int align
)
89 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
92 if (phba
->sli_rev
== LPFC_SLI_REV4
) {
93 /* Calculate alignment */
94 if (phba
->cfg_sg_dma_buf_size
< SLI4_PAGE_SIZE
)
95 i
= phba
->cfg_sg_dma_buf_size
;
99 phba
->lpfc_sg_dma_buf_pool
=
100 pci_pool_create("lpfc_sg_dma_buf_pool",
102 phba
->cfg_sg_dma_buf_size
,
104 if (!phba
->lpfc_sg_dma_buf_pool
)
108 phba
->lpfc_sg_dma_buf_pool
=
109 pci_pool_create("lpfc_sg_dma_buf_pool",
110 phba
->pcidev
, phba
->cfg_sg_dma_buf_size
,
113 if (!phba
->lpfc_sg_dma_buf_pool
)
117 phba
->lpfc_mbuf_pool
= pci_pool_create("lpfc_mbuf_pool", phba
->pcidev
,
120 if (!phba
->lpfc_mbuf_pool
)
121 goto fail_free_dma_buf_pool
;
123 pool
->elements
= kmalloc(sizeof(struct lpfc_dmabuf
) *
124 LPFC_MBUF_POOL_SIZE
, GFP_KERNEL
);
126 goto fail_free_lpfc_mbuf_pool
;
129 pool
->current_count
= 0;
130 for ( i
= 0; i
< LPFC_MBUF_POOL_SIZE
; i
++) {
131 pool
->elements
[i
].virt
= pci_pool_alloc(phba
->lpfc_mbuf_pool
,
132 GFP_KERNEL
, &pool
->elements
[i
].phys
);
133 if (!pool
->elements
[i
].virt
)
134 goto fail_free_mbuf_pool
;
136 pool
->current_count
++;
139 phba
->mbox_mem_pool
= mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE
,
140 sizeof(LPFC_MBOXQ_t
));
141 if (!phba
->mbox_mem_pool
)
142 goto fail_free_mbuf_pool
;
144 phba
->nlp_mem_pool
= mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE
,
145 sizeof(struct lpfc_nodelist
));
146 if (!phba
->nlp_mem_pool
)
147 goto fail_free_mbox_pool
;
149 if (phba
->sli_rev
== LPFC_SLI_REV4
) {
151 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE
,
152 sizeof(struct lpfc_node_rrq
));
154 goto fail_free_nlp_mem_pool
;
155 phba
->lpfc_hrb_pool
= pci_pool_create("lpfc_hrb_pool",
157 LPFC_HDR_BUF_SIZE
, align
, 0);
158 if (!phba
->lpfc_hrb_pool
)
159 goto fail_free_rrq_mem_pool
;
161 phba
->lpfc_drb_pool
= pci_pool_create("lpfc_drb_pool",
163 LPFC_DATA_BUF_SIZE
, align
, 0);
164 if (!phba
->lpfc_drb_pool
)
165 goto fail_free_hrb_pool
;
166 phba
->lpfc_hbq_pool
= NULL
;
168 phba
->lpfc_hbq_pool
= pci_pool_create("lpfc_hbq_pool",
169 phba
->pcidev
, LPFC_BPL_SIZE
, align
, 0);
170 if (!phba
->lpfc_hbq_pool
)
171 goto fail_free_nlp_mem_pool
;
172 phba
->lpfc_hrb_pool
= NULL
;
173 phba
->lpfc_drb_pool
= NULL
;
176 if (phba
->cfg_EnableXLane
) {
177 phba
->device_data_mem_pool
= mempool_create_kmalloc_pool(
178 LPFC_DEVICE_DATA_POOL_SIZE
,
179 sizeof(struct lpfc_device_data
));
180 if (!phba
->device_data_mem_pool
)
181 goto fail_free_drb_pool
;
183 phba
->device_data_mem_pool
= NULL
;
188 pci_pool_destroy(phba
->lpfc_drb_pool
);
189 phba
->lpfc_drb_pool
= NULL
;
191 pci_pool_destroy(phba
->lpfc_hrb_pool
);
192 phba
->lpfc_hrb_pool
= NULL
;
193 fail_free_rrq_mem_pool
:
194 mempool_destroy(phba
->rrq_pool
);
195 phba
->rrq_pool
= NULL
;
196 fail_free_nlp_mem_pool
:
197 mempool_destroy(phba
->nlp_mem_pool
);
198 phba
->nlp_mem_pool
= NULL
;
200 mempool_destroy(phba
->mbox_mem_pool
);
201 phba
->mbox_mem_pool
= NULL
;
204 pci_pool_free(phba
->lpfc_mbuf_pool
, pool
->elements
[i
].virt
,
205 pool
->elements
[i
].phys
);
206 kfree(pool
->elements
);
207 fail_free_lpfc_mbuf_pool
:
208 pci_pool_destroy(phba
->lpfc_mbuf_pool
);
209 phba
->lpfc_mbuf_pool
= NULL
;
210 fail_free_dma_buf_pool
:
211 pci_pool_destroy(phba
->lpfc_sg_dma_buf_pool
);
212 phba
->lpfc_sg_dma_buf_pool
= NULL
;
218 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
219 * @phba: HBA to free memory for
221 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
222 * routine is a the counterpart of lpfc_mem_alloc.
227 lpfc_mem_free(struct lpfc_hba
*phba
)
230 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
231 struct lpfc_device_data
*device_data
;
234 lpfc_sli_hbqbuf_free_all(phba
);
235 if (phba
->lpfc_drb_pool
)
236 pci_pool_destroy(phba
->lpfc_drb_pool
);
237 phba
->lpfc_drb_pool
= NULL
;
238 if (phba
->lpfc_hrb_pool
)
239 pci_pool_destroy(phba
->lpfc_hrb_pool
);
240 phba
->lpfc_hrb_pool
= NULL
;
241 if (phba
->txrdy_payload_pool
)
242 pci_pool_destroy(phba
->txrdy_payload_pool
);
243 phba
->txrdy_payload_pool
= NULL
;
245 if (phba
->lpfc_hbq_pool
)
246 pci_pool_destroy(phba
->lpfc_hbq_pool
);
247 phba
->lpfc_hbq_pool
= NULL
;
250 mempool_destroy(phba
->rrq_pool
);
251 phba
->rrq_pool
= NULL
;
253 /* Free NLP memory pool */
254 mempool_destroy(phba
->nlp_mem_pool
);
255 phba
->nlp_mem_pool
= NULL
;
256 if (phba
->sli_rev
== LPFC_SLI_REV4
&& phba
->active_rrq_pool
) {
257 mempool_destroy(phba
->active_rrq_pool
);
258 phba
->active_rrq_pool
= NULL
;
261 /* Free mbox memory pool */
262 mempool_destroy(phba
->mbox_mem_pool
);
263 phba
->mbox_mem_pool
= NULL
;
265 /* Free MBUF memory pool */
266 for (i
= 0; i
< pool
->current_count
; i
++)
267 pci_pool_free(phba
->lpfc_mbuf_pool
, pool
->elements
[i
].virt
,
268 pool
->elements
[i
].phys
);
269 kfree(pool
->elements
);
271 pci_pool_destroy(phba
->lpfc_mbuf_pool
);
272 phba
->lpfc_mbuf_pool
= NULL
;
274 /* Free DMA buffer memory pool */
275 pci_pool_destroy(phba
->lpfc_sg_dma_buf_pool
);
276 phba
->lpfc_sg_dma_buf_pool
= NULL
;
278 /* Free Device Data memory pool */
279 if (phba
->device_data_mem_pool
) {
280 /* Ensure all objects have been returned to the pool */
281 while (!list_empty(&phba
->luns
)) {
282 device_data
= list_first_entry(&phba
->luns
,
283 struct lpfc_device_data
,
285 list_del(&device_data
->listentry
);
286 mempool_free(device_data
, phba
->device_data_mem_pool
);
288 mempool_destroy(phba
->device_data_mem_pool
);
290 phba
->device_data_mem_pool
= NULL
;
295 * lpfc_mem_free_all - Frees all PCI and driver memory
296 * @phba: HBA to free memory for
298 * Description: Free memory from PCI and driver memory pools and also those
299 * used : lpfc_sg_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
300 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
306 lpfc_mem_free_all(struct lpfc_hba
*phba
)
308 struct lpfc_sli
*psli
= &phba
->sli
;
309 LPFC_MBOXQ_t
*mbox
, *next_mbox
;
310 struct lpfc_dmabuf
*mp
;
312 /* Free memory used in mailbox queue back to mailbox memory pool */
313 list_for_each_entry_safe(mbox
, next_mbox
, &psli
->mboxq
, list
) {
314 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
316 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
319 list_del(&mbox
->list
);
320 mempool_free(mbox
, phba
->mbox_mem_pool
);
322 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
323 list_for_each_entry_safe(mbox
, next_mbox
, &psli
->mboxq_cmpl
, list
) {
324 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
326 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
329 list_del(&mbox
->list
);
330 mempool_free(mbox
, phba
->mbox_mem_pool
);
332 /* Free the active mailbox command back to the mailbox memory pool */
333 spin_lock_irq(&phba
->hbalock
);
334 psli
->sli_flag
&= ~LPFC_SLI_MBOX_ACTIVE
;
335 spin_unlock_irq(&phba
->hbalock
);
336 if (psli
->mbox_active
) {
337 mbox
= psli
->mbox_active
;
338 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
340 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
343 mempool_free(mbox
, phba
->mbox_mem_pool
);
344 psli
->mbox_active
= NULL
;
347 /* Free and destroy all the allocated memory pools */
350 /* Free the iocb lookup array */
351 kfree(psli
->iocbq_lookup
);
352 psli
->iocbq_lookup
= NULL
;
358 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
359 * @phba: HBA which owns the pool to allocate from
360 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
361 * @handle: used to return the DMA-mapped address of the mbuf
363 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
364 * Allocates from generic pci_pool_alloc function first and if that fails and
365 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
368 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
372 * pointer to the allocated mbuf on success
376 lpfc_mbuf_alloc(struct lpfc_hba
*phba
, int mem_flags
, dma_addr_t
*handle
)
378 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
379 unsigned long iflags
;
382 ret
= pci_pool_alloc(phba
->lpfc_mbuf_pool
, GFP_KERNEL
, handle
);
384 spin_lock_irqsave(&phba
->hbalock
, iflags
);
385 if (!ret
&& (mem_flags
& MEM_PRI
) && pool
->current_count
) {
386 pool
->current_count
--;
387 ret
= pool
->elements
[pool
->current_count
].virt
;
388 *handle
= pool
->elements
[pool
->current_count
].phys
;
390 spin_unlock_irqrestore(&phba
->hbalock
, iflags
);
395 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
396 * @phba: HBA which owns the pool to return to
397 * @virt: mbuf to free
398 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
400 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
401 * it is below its max_count, frees the mbuf otherwise.
403 * Notes: Must be called with phba->hbalock held to synchronize access to
404 * lpfc_mbuf_safety_pool.
409 __lpfc_mbuf_free(struct lpfc_hba
* phba
, void *virt
, dma_addr_t dma
)
411 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
413 if (pool
->current_count
< pool
->max_count
) {
414 pool
->elements
[pool
->current_count
].virt
= virt
;
415 pool
->elements
[pool
->current_count
].phys
= dma
;
416 pool
->current_count
++;
418 pci_pool_free(phba
->lpfc_mbuf_pool
, virt
, dma
);
424 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
425 * @phba: HBA which owns the pool to return to
426 * @virt: mbuf to free
427 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
429 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
430 * it is below its max_count, frees the mbuf otherwise.
432 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
437 lpfc_mbuf_free(struct lpfc_hba
* phba
, void *virt
, dma_addr_t dma
)
439 unsigned long iflags
;
441 spin_lock_irqsave(&phba
->hbalock
, iflags
);
442 __lpfc_mbuf_free(phba
, virt
, dma
);
443 spin_unlock_irqrestore(&phba
->hbalock
, iflags
);
448 * lpfc_nvmet_buf_alloc - Allocate an nvmet_buf from the
449 * lpfc_sg_dma_buf_pool PCI pool
450 * @phba: HBA which owns the pool to allocate from
451 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
452 * @handle: used to return the DMA-mapped address of the nvmet_buf
454 * Description: Allocates a DMA-mapped buffer from the lpfc_sg_dma_buf_pool
455 * PCI pool. Allocates from generic pci_pool_alloc function.
458 * pointer to the allocated nvmet_buf on success
462 lpfc_nvmet_buf_alloc(struct lpfc_hba
*phba
, int mem_flags
, dma_addr_t
*handle
)
466 ret
= pci_pool_alloc(phba
->lpfc_sg_dma_buf_pool
, GFP_KERNEL
, handle
);
471 * lpfc_nvmet_buf_free - Free an nvmet_buf from the lpfc_sg_dma_buf_pool
473 * @phba: HBA which owns the pool to return to
474 * @virt: nvmet_buf to free
475 * @dma: the DMA-mapped address of the lpfc_sg_dma_buf_pool to be freed
480 lpfc_nvmet_buf_free(struct lpfc_hba
*phba
, void *virt
, dma_addr_t dma
)
482 pci_pool_free(phba
->lpfc_sg_dma_buf_pool
, virt
, dma
);
486 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
487 * @phba: HBA to allocate HBQ buffer for
489 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
490 * pool along a non-DMA-mapped container for it.
492 * Notes: Not interrupt-safe. Must be called with no locks held.
495 * pointer to HBQ on success
499 lpfc_els_hbq_alloc(struct lpfc_hba
*phba
)
501 struct hbq_dmabuf
*hbqbp
;
503 hbqbp
= kzalloc(sizeof(struct hbq_dmabuf
), GFP_KERNEL
);
507 hbqbp
->dbuf
.virt
= pci_pool_alloc(phba
->lpfc_hbq_pool
, GFP_KERNEL
,
509 if (!hbqbp
->dbuf
.virt
) {
513 hbqbp
->total_size
= LPFC_BPL_SIZE
;
518 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
519 * @phba: HBA buffer was allocated for
520 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
522 * Description: Frees both the container and the DMA-mapped buffer returned by
523 * lpfc_els_hbq_alloc.
525 * Notes: Can be called with or without locks held.
530 lpfc_els_hbq_free(struct lpfc_hba
*phba
, struct hbq_dmabuf
*hbqbp
)
532 pci_pool_free(phba
->lpfc_hbq_pool
, hbqbp
->dbuf
.virt
, hbqbp
->dbuf
.phys
);
538 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
539 * @phba: HBA to allocate a receive buffer for
541 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
542 * pool along a non-DMA-mapped container for it.
544 * Notes: Not interrupt-safe. Must be called with no locks held.
547 * pointer to HBQ on success
551 lpfc_sli4_rb_alloc(struct lpfc_hba
*phba
)
553 struct hbq_dmabuf
*dma_buf
;
555 dma_buf
= kzalloc(sizeof(struct hbq_dmabuf
), GFP_KERNEL
);
559 dma_buf
->hbuf
.virt
= pci_pool_alloc(phba
->lpfc_hrb_pool
, GFP_KERNEL
,
560 &dma_buf
->hbuf
.phys
);
561 if (!dma_buf
->hbuf
.virt
) {
565 dma_buf
->dbuf
.virt
= pci_pool_alloc(phba
->lpfc_drb_pool
, GFP_KERNEL
,
566 &dma_buf
->dbuf
.phys
);
567 if (!dma_buf
->dbuf
.virt
) {
568 pci_pool_free(phba
->lpfc_hrb_pool
, dma_buf
->hbuf
.virt
,
573 dma_buf
->total_size
= LPFC_DATA_BUF_SIZE
;
578 * lpfc_sli4_rb_free - Frees a receive buffer
579 * @phba: HBA buffer was allocated for
580 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
582 * Description: Frees both the container and the DMA-mapped buffers returned by
583 * lpfc_sli4_rb_alloc.
585 * Notes: Can be called with or without locks held.
590 lpfc_sli4_rb_free(struct lpfc_hba
*phba
, struct hbq_dmabuf
*dmab
)
592 pci_pool_free(phba
->lpfc_hrb_pool
, dmab
->hbuf
.virt
, dmab
->hbuf
.phys
);
593 pci_pool_free(phba
->lpfc_drb_pool
, dmab
->dbuf
.virt
, dmab
->dbuf
.phys
);
598 * lpfc_sli4_nvmet_alloc - Allocate an SLI4 Receive buffer
599 * @phba: HBA to allocate a receive buffer for
601 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
602 * pool along a non-DMA-mapped container for it.
604 * Notes: Not interrupt-safe. Must be called with no locks held.
607 * pointer to HBQ on success
611 lpfc_sli4_nvmet_alloc(struct lpfc_hba
*phba
)
613 struct rqb_dmabuf
*dma_buf
;
614 struct lpfc_iocbq
*nvmewqe
;
615 union lpfc_wqe128
*wqe
;
617 dma_buf
= kzalloc(sizeof(struct rqb_dmabuf
), GFP_KERNEL
);
621 dma_buf
->hbuf
.virt
= pci_pool_alloc(phba
->lpfc_hrb_pool
, GFP_KERNEL
,
622 &dma_buf
->hbuf
.phys
);
623 if (!dma_buf
->hbuf
.virt
) {
627 dma_buf
->dbuf
.virt
= pci_pool_alloc(phba
->lpfc_drb_pool
, GFP_KERNEL
,
628 &dma_buf
->dbuf
.phys
);
629 if (!dma_buf
->dbuf
.virt
) {
630 pci_pool_free(phba
->lpfc_hrb_pool
, dma_buf
->hbuf
.virt
,
635 dma_buf
->total_size
= LPFC_DATA_BUF_SIZE
;
637 dma_buf
->context
= kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx
),
639 if (!dma_buf
->context
) {
640 pci_pool_free(phba
->lpfc_drb_pool
, dma_buf
->dbuf
.virt
,
642 pci_pool_free(phba
->lpfc_hrb_pool
, dma_buf
->hbuf
.virt
,
648 dma_buf
->iocbq
= lpfc_sli_get_iocbq(phba
);
649 dma_buf
->iocbq
->iocb_flag
= LPFC_IO_NVMET
;
650 if (!dma_buf
->iocbq
) {
651 kfree(dma_buf
->context
);
652 pci_pool_free(phba
->lpfc_drb_pool
, dma_buf
->dbuf
.virt
,
654 pci_pool_free(phba
->lpfc_hrb_pool
, dma_buf
->hbuf
.virt
,
657 lpfc_printf_log(phba
, KERN_ERR
, LOG_NVME
,
658 "2621 Ran out of nvmet iocb/WQEs\n");
661 nvmewqe
= dma_buf
->iocbq
;
662 wqe
= (union lpfc_wqe128
*)&nvmewqe
->wqe
;
664 memset(wqe
, 0, sizeof(union lpfc_wqe
));
666 bf_set(wqe_ct
, &wqe
->generic
.wqe_com
, SLI4_CT_RPI
);
667 bf_set(wqe_class
, &wqe
->generic
.wqe_com
, CLASS3
);
668 bf_set(wqe_pu
, &wqe
->generic
.wqe_com
, 1);
670 bf_set(wqe_nvme
, &wqe
->fcp_tsend
.wqe_com
, 1);
671 bf_set(wqe_ebde_cnt
, &wqe
->generic
.wqe_com
, 0);
672 bf_set(wqe_qosd
, &wqe
->generic
.wqe_com
, 0);
674 dma_buf
->iocbq
->context1
= NULL
;
675 spin_lock(&phba
->sli4_hba
.sgl_list_lock
);
676 dma_buf
->sglq
= __lpfc_sli_get_nvmet_sglq(phba
, dma_buf
->iocbq
);
677 spin_unlock(&phba
->sli4_hba
.sgl_list_lock
);
678 if (!dma_buf
->sglq
) {
679 lpfc_sli_release_iocbq(phba
, dma_buf
->iocbq
);
680 kfree(dma_buf
->context
);
681 pci_pool_free(phba
->lpfc_drb_pool
, dma_buf
->dbuf
.virt
,
683 pci_pool_free(phba
->lpfc_hrb_pool
, dma_buf
->hbuf
.virt
,
686 lpfc_printf_log(phba
, KERN_ERR
, LOG_NVME
,
687 "6132 Ran out of nvmet XRIs\n");
694 * lpfc_sli4_nvmet_free - Frees a receive buffer
695 * @phba: HBA buffer was allocated for
696 * @dmab: DMA Buffer container returned by lpfc_sli4_rbq_alloc
698 * Description: Frees both the container and the DMA-mapped buffers returned by
699 * lpfc_sli4_nvmet_alloc.
701 * Notes: Can be called with or without locks held.
706 lpfc_sli4_nvmet_free(struct lpfc_hba
*phba
, struct rqb_dmabuf
*dmab
)
710 __lpfc_clear_active_sglq(phba
, dmab
->sglq
->sli4_lxritag
);
711 dmab
->sglq
->state
= SGL_FREED
;
712 dmab
->sglq
->ndlp
= NULL
;
714 spin_lock_irqsave(&phba
->sli4_hba
.sgl_list_lock
, flags
);
715 list_add_tail(&dmab
->sglq
->list
, &phba
->sli4_hba
.lpfc_nvmet_sgl_list
);
716 spin_unlock_irqrestore(&phba
->sli4_hba
.sgl_list_lock
, flags
);
718 lpfc_sli_release_iocbq(phba
, dmab
->iocbq
);
719 kfree(dmab
->context
);
720 pci_pool_free(phba
->lpfc_hrb_pool
, dmab
->hbuf
.virt
, dmab
->hbuf
.phys
);
721 pci_pool_free(phba
->lpfc_drb_pool
, dmab
->dbuf
.virt
, dmab
->dbuf
.phys
);
726 * lpfc_in_buf_free - Free a DMA buffer
727 * @phba: HBA buffer is associated with
728 * @mp: Buffer to free
730 * Description: Frees the given DMA buffer in the appropriate way given if the
731 * HBA is running in SLI3 mode with HBQs enabled.
733 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
738 lpfc_in_buf_free(struct lpfc_hba
*phba
, struct lpfc_dmabuf
*mp
)
740 struct hbq_dmabuf
*hbq_entry
;
746 if (phba
->sli3_options
& LPFC_SLI3_HBQ_ENABLED
) {
747 hbq_entry
= container_of(mp
, struct hbq_dmabuf
, dbuf
);
748 /* Check whether HBQ is still in use */
749 spin_lock_irqsave(&phba
->hbalock
, flags
);
750 if (!phba
->hbq_in_use
) {
751 spin_unlock_irqrestore(&phba
->hbalock
, flags
);
754 list_del(&hbq_entry
->dbuf
.list
);
755 if (hbq_entry
->tag
== -1) {
756 (phba
->hbqs
[LPFC_ELS_HBQ
].hbq_free_buffer
)
759 lpfc_sli_free_hbq(phba
, hbq_entry
);
761 spin_unlock_irqrestore(&phba
->hbalock
, flags
);
763 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
770 * lpfc_rq_buf_free - Free a RQ DMA buffer
771 * @phba: HBA buffer is associated with
772 * @mp: Buffer to free
774 * Description: Frees the given DMA buffer in the appropriate way given by
775 * reposting it to its associated RQ so it can be reused.
777 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
782 lpfc_rq_buf_free(struct lpfc_hba
*phba
, struct lpfc_dmabuf
*mp
)
784 struct lpfc_rqb
*rqbp
;
785 struct lpfc_rqe hrqe
;
786 struct lpfc_rqe drqe
;
787 struct rqb_dmabuf
*rqb_entry
;
794 rqb_entry
= container_of(mp
, struct rqb_dmabuf
, hbuf
);
795 rqbp
= rqb_entry
->hrq
->rqbp
;
797 spin_lock_irqsave(&phba
->hbalock
, flags
);
798 list_del(&rqb_entry
->hbuf
.list
);
799 hrqe
.address_lo
= putPaddrLow(rqb_entry
->hbuf
.phys
);
800 hrqe
.address_hi
= putPaddrHigh(rqb_entry
->hbuf
.phys
);
801 drqe
.address_lo
= putPaddrLow(rqb_entry
->dbuf
.phys
);
802 drqe
.address_hi
= putPaddrHigh(rqb_entry
->dbuf
.phys
);
803 rc
= lpfc_sli4_rq_put(rqb_entry
->hrq
, rqb_entry
->drq
, &hrqe
, &drqe
);
805 (rqbp
->rqb_free_buffer
)(phba
, rqb_entry
);
807 list_add_tail(&rqb_entry
->hbuf
.list
, &rqbp
->rqb_buffer_list
);
808 rqbp
->buffer_count
++;
811 spin_unlock_irqrestore(&phba
->hbalock
, flags
);