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