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