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