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