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