blob: 91060afc97211b03e9ac9c51c2d95e9a16ce0fec [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd080abe2017-02-12 13:52:39 -08004 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smartb857ff32014-04-04 13:52:50 -04006 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * 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. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/mempool.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050026#include <linux/pci.h>
27#include <linux/interrupt.h>
28
James Smart895427b2017-02-12 13:52:30 -080029#include <scsi/scsi.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040030#include <scsi/scsi_device.h>
31#include <scsi/scsi_transport_fc.h>
James Smart895427b2017-02-12 13:52:30 -080032#include <scsi/fc/fc_fs.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040033
James Smart895427b2017-02-12 13:52:30 -080034#include <linux/nvme-fc-driver.h>
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080043#include "lpfc_scsi.h"
44#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080045#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_crtn.h"
James Smartcff261f2013-12-17 20:29:47 -050047#include "lpfc_logmsg.h"
dea31012005-04-17 16:05:31 -050048
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 */
James Smart1ba981f2014-02-20 09:56:45 -050051#define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */
dea31012005-04-17 16:05:31 -050052
James Smartcff261f2013-12-17 20:29:47 -050053int
54lpfc_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}
James Smart2e0fef82007-06-17 19:56:36 -050070
James Smarte59058c2008-08-24 21:49:00 -040071/**
James Smart3621a712009-04-06 18:47:14 -040072 * lpfc_mem_alloc - create and allocate all PCI and memory pools
James Smarte59058c2008-08-24 21:49:00 -040073 * @phba: HBA to allocate pools for
74 *
James Smart895427b2017-02-12 13:52:30 -080075 * Description: Creates and allocates PCI pools lpfc_sg_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -040076 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
James Smarte59058c2008-08-24 21:49:00 -040077 * 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 **/
dea31012005-04-17 16:05:31 -050086int
James Smartda0436e2009-05-22 14:51:39 -040087lpfc_mem_alloc(struct lpfc_hba *phba, int align)
dea31012005-04-17 16:05:31 -050088{
89 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
90 int i;
91
James Smart96f70772013-04-17 20:16:15 -040092 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
James Smart895427b2017-02-12 13:52:30 -080099 phba->lpfc_sg_dma_buf_pool =
100 pci_pool_create("lpfc_sg_dma_buf_pool",
101 phba->pcidev,
102 phba->cfg_sg_dma_buf_size,
103 i, 0);
104 if (!phba->lpfc_sg_dma_buf_pool)
105 goto fail;
James Smart96f70772013-04-17 20:16:15 -0400106
James Smart895427b2017-02-12 13:52:30 -0800107 } else {
108 phba->lpfc_sg_dma_buf_pool =
109 pci_pool_create("lpfc_sg_dma_buf_pool",
110 phba->pcidev, phba->cfg_sg_dma_buf_size,
111 align, 0);
112
113 if (!phba->lpfc_sg_dma_buf_pool)
114 goto fail;
115 }
dea31012005-04-17 16:05:31 -0500116
117 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
James Smartda0436e2009-05-22 14:51:39 -0400118 LPFC_BPL_SIZE,
119 align, 0);
dea31012005-04-17 16:05:31 -0500120 if (!phba->lpfc_mbuf_pool)
121 goto fail_free_dma_buf_pool;
122
123 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
124 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100125 if (!pool->elements)
126 goto fail_free_lpfc_mbuf_pool;
127
dea31012005-04-17 16:05:31 -0500128 pool->max_count = 0;
129 pool->current_count = 0;
130 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
131 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
132 GFP_KERNEL, &pool->elements[i].phys);
133 if (!pool->elements[i].virt)
134 goto fail_free_mbuf_pool;
135 pool->max_count++;
136 pool->current_count++;
137 }
138
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800139 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
140 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -0500141 if (!phba->mbox_mem_pool)
142 goto fail_free_mbuf_pool;
143
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800144 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
145 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -0500146 if (!phba->nlp_mem_pool)
147 goto fail_free_mbox_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400148
149 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart19ca7602010-11-20 23:11:55 -0500150 phba->rrq_pool =
151 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
152 sizeof(struct lpfc_node_rrq));
153 if (!phba->rrq_pool)
154 goto fail_free_nlp_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400155 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400156 phba->pcidev,
157 LPFC_HDR_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400158 if (!phba->lpfc_hrb_pool)
James Smart19ca7602010-11-20 23:11:55 -0500159 goto fail_free_rrq_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400160
161 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400162 phba->pcidev,
163 LPFC_DATA_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400164 if (!phba->lpfc_drb_pool)
165 goto fail_free_hrb_pool;
166 phba->lpfc_hbq_pool = NULL;
167 } else {
168 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
169 phba->pcidev, LPFC_BPL_SIZE, align, 0);
170 if (!phba->lpfc_hbq_pool)
171 goto fail_free_nlp_mem_pool;
172 phba->lpfc_hrb_pool = NULL;
173 phba->lpfc_drb_pool = NULL;
174 }
James Smart92d7f7b2007-06-17 19:56:38 -0500175
James Smart1ba981f2014-02-20 09:56:45 -0500176 if (phba->cfg_EnableXLane) {
177 phba->device_data_mem_pool = mempool_create_kmalloc_pool(
178 LPFC_DEVICE_DATA_POOL_SIZE,
179 sizeof(struct lpfc_device_data));
180 if (!phba->device_data_mem_pool)
James Smart895427b2017-02-12 13:52:30 -0800181 goto fail_free_drb_pool;
James Smart1ba981f2014-02-20 09:56:45 -0500182 } else {
183 phba->device_data_mem_pool = NULL;
184 }
185
dea31012005-04-17 16:05:31 -0500186 return 0;
James Smart895427b2017-02-12 13:52:30 -0800187fail_free_drb_pool:
188 pci_pool_destroy(phba->lpfc_drb_pool);
189 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400190 fail_free_hrb_pool:
James Smartda0436e2009-05-22 14:51:39 -0400191 pci_pool_destroy(phba->lpfc_hrb_pool);
192 phba->lpfc_hrb_pool = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500193 fail_free_rrq_mem_pool:
194 mempool_destroy(phba->rrq_pool);
195 phba->rrq_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500196 fail_free_nlp_mem_pool:
197 mempool_destroy(phba->nlp_mem_pool);
198 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500199 fail_free_mbox_pool:
200 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500201 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500202 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100203 while (i--)
dea31012005-04-17 16:05:31 -0500204 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
205 pool->elements[i].phys);
206 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100207 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500208 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500209 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500210 fail_free_dma_buf_pool:
James Smart895427b2017-02-12 13:52:30 -0800211 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
212 phba->lpfc_sg_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500213 fail:
214 return -ENOMEM;
215}
216
James Smart3c603be2017-05-15 15:20:44 -0700217int
218lpfc_nvmet_mem_alloc(struct lpfc_hba *phba)
219{
220 phba->lpfc_nvmet_drb_pool =
221 pci_pool_create("lpfc_nvmet_drb_pool",
222 phba->pcidev, LPFC_NVMET_DATA_BUF_SIZE,
223 SGL_ALIGN_SZ, 0);
224 if (!phba->lpfc_nvmet_drb_pool) {
225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
226 "6024 Can't enable NVME Target - no memory\n");
227 return -ENOMEM;
228 }
229 return 0;
230}
231
James Smarte59058c2008-08-24 21:49:00 -0400232/**
James Smartda0436e2009-05-22 14:51:39 -0400233 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400234 * @phba: HBA to free memory for
235 *
James Smartda0436e2009-05-22 14:51:39 -0400236 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
237 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400238 *
239 * Returns: None
240 **/
dea31012005-04-17 16:05:31 -0500241void
James Smartda0436e2009-05-22 14:51:39 -0400242lpfc_mem_free(struct lpfc_hba *phba)
243{
244 int i;
245 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart1ba981f2014-02-20 09:56:45 -0500246 struct lpfc_device_data *device_data;
James Smartda0436e2009-05-22 14:51:39 -0400247
James Smartda0436e2009-05-22 14:51:39 -0400248 /* Free HBQ pools */
249 lpfc_sli_hbqbuf_free_all(phba);
James Smart3c603be2017-05-15 15:20:44 -0700250 if (phba->lpfc_nvmet_drb_pool)
251 pci_pool_destroy(phba->lpfc_nvmet_drb_pool);
252 phba->lpfc_nvmet_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400253 if (phba->lpfc_drb_pool)
254 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400255 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400256 if (phba->lpfc_hrb_pool)
257 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400258 phba->lpfc_hrb_pool = NULL;
James Smart895427b2017-02-12 13:52:30 -0800259 if (phba->txrdy_payload_pool)
260 pci_pool_destroy(phba->txrdy_payload_pool);
261 phba->txrdy_payload_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400262
James Smart8568a4d2009-07-19 10:01:16 -0400263 if (phba->lpfc_hbq_pool)
264 pci_pool_destroy(phba->lpfc_hbq_pool);
265 phba->lpfc_hbq_pool = NULL;
Martin K. Petersend65c8ff2016-05-09 21:39:43 -0400266
267 if (phba->rrq_pool)
268 mempool_destroy(phba->rrq_pool);
James Smart21a688c2012-08-03 12:34:15 -0400269 phba->rrq_pool = NULL;
270
James Smartda0436e2009-05-22 14:51:39 -0400271 /* Free NLP memory pool */
272 mempool_destroy(phba->nlp_mem_pool);
273 phba->nlp_mem_pool = NULL;
Martin K. Petersend65c8ff2016-05-09 21:39:43 -0400274 if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
James Smartcff261f2013-12-17 20:29:47 -0500275 mempool_destroy(phba->active_rrq_pool);
276 phba->active_rrq_pool = NULL;
277 }
James Smartda0436e2009-05-22 14:51:39 -0400278
279 /* Free mbox memory pool */
280 mempool_destroy(phba->mbox_mem_pool);
281 phba->mbox_mem_pool = NULL;
282
283 /* Free MBUF memory pool */
284 for (i = 0; i < pool->current_count; i++)
285 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
286 pool->elements[i].phys);
287 kfree(pool->elements);
288
289 pci_pool_destroy(phba->lpfc_mbuf_pool);
290 phba->lpfc_mbuf_pool = NULL;
291
292 /* Free DMA buffer memory pool */
James Smart895427b2017-02-12 13:52:30 -0800293 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
294 phba->lpfc_sg_dma_buf_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400295
James Smart1ba981f2014-02-20 09:56:45 -0500296 /* Free Device Data memory pool */
297 if (phba->device_data_mem_pool) {
298 /* Ensure all objects have been returned to the pool */
299 while (!list_empty(&phba->luns)) {
300 device_data = list_first_entry(&phba->luns,
301 struct lpfc_device_data,
302 listentry);
303 list_del(&device_data->listentry);
304 mempool_free(device_data, phba->device_data_mem_pool);
305 }
306 mempool_destroy(phba->device_data_mem_pool);
307 }
308 phba->device_data_mem_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400309 return;
310}
311
312/**
313 * lpfc_mem_free_all - Frees all PCI and driver memory
314 * @phba: HBA to free memory for
315 *
316 * Description: Free memory from PCI and driver memory pools and also those
James Smart895427b2017-02-12 13:52:30 -0800317 * used : lpfc_sg_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
James Smartda0436e2009-05-22 14:51:39 -0400318 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
319 * the VPI bitmask.
320 *
321 * Returns: None
322 **/
323void
324lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500325{
326 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500327 LPFC_MBOXQ_t *mbox, *next_mbox;
328 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500329
James Smartda0436e2009-05-22 14:51:39 -0400330 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500331 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
332 mp = (struct lpfc_dmabuf *) (mbox->context1);
333 if (mp) {
334 lpfc_mbuf_free(phba, mp->virt, mp->phys);
335 kfree(mp);
336 }
337 list_del(&mbox->list);
338 mempool_free(mbox, phba->mbox_mem_pool);
339 }
James Smartda0436e2009-05-22 14:51:39 -0400340 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500341 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
342 mp = (struct lpfc_dmabuf *) (mbox->context1);
343 if (mp) {
344 lpfc_mbuf_free(phba, mp->virt, mp->phys);
345 kfree(mp);
346 }
347 list_del(&mbox->list);
348 mempool_free(mbox, phba->mbox_mem_pool);
349 }
James Smartda0436e2009-05-22 14:51:39 -0400350 /* Free the active mailbox command back to the mailbox memory pool */
351 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500352 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400353 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500354 if (psli->mbox_active) {
355 mbox = psli->mbox_active;
356 mp = (struct lpfc_dmabuf *) (mbox->context1);
357 if (mp) {
358 lpfc_mbuf_free(phba, mp->virt, mp->phys);
359 kfree(mp);
360 }
361 mempool_free(mbox, phba->mbox_mem_pool);
362 psli->mbox_active = NULL;
363 }
364
James Smartda0436e2009-05-22 14:51:39 -0400365 /* Free and destroy all the allocated memory pools */
366 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500367
James Smarte59058c2008-08-24 21:49:00 -0400368 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400369 kfree(psli->iocbq_lookup);
370 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400371
372 return;
dea31012005-04-17 16:05:31 -0500373}
374
James Smarte59058c2008-08-24 21:49:00 -0400375/**
James Smart3621a712009-04-06 18:47:14 -0400376 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400377 * @phba: HBA which owns the pool to allocate from
378 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
379 * @handle: used to return the DMA-mapped address of the mbuf
380 *
381 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
382 * Allocates from generic pci_pool_alloc function first and if that fails and
383 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
384 * HBA's pool.
385 *
386 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
387 * phba->hbalock.
388 *
389 * Returns:
390 * pointer to the allocated mbuf on success
391 * NULL on failure
392 **/
dea31012005-04-17 16:05:31 -0500393void *
394lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
395{
396 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500397 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500398 void *ret;
399
400 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
401
James Smart2e0fef82007-06-17 19:56:36 -0500402 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500403 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500404 pool->current_count--;
405 ret = pool->elements[pool->current_count].virt;
406 *handle = pool->elements[pool->current_count].phys;
407 }
James Smart2e0fef82007-06-17 19:56:36 -0500408 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500409 return ret;
410}
411
James Smarte59058c2008-08-24 21:49:00 -0400412/**
James Smart3621a712009-04-06 18:47:14 -0400413 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400414 * @phba: HBA which owns the pool to return to
415 * @virt: mbuf to free
416 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
417 *
418 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
419 * it is below its max_count, frees the mbuf otherwise.
420 *
421 * Notes: Must be called with phba->hbalock held to synchronize access to
422 * lpfc_mbuf_safety_pool.
423 *
424 * Returns: None
425 **/
dea31012005-04-17 16:05:31 -0500426void
James Smart2e0fef82007-06-17 19:56:36 -0500427__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500428{
429 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
430
431 if (pool->current_count < pool->max_count) {
432 pool->elements[pool->current_count].virt = virt;
433 pool->elements[pool->current_count].phys = dma;
434 pool->current_count++;
435 } else {
436 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
437 }
438 return;
439}
James Smart2e0fef82007-06-17 19:56:36 -0500440
James Smarte59058c2008-08-24 21:49:00 -0400441/**
James Smart3621a712009-04-06 18:47:14 -0400442 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400443 * @phba: HBA which owns the pool to return to
444 * @virt: mbuf to free
445 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
446 *
447 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
448 * it is below its max_count, frees the mbuf otherwise.
449 *
450 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
451 *
452 * Returns: None
453 **/
James Smart2e0fef82007-06-17 19:56:36 -0500454void
455lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
456{
457 unsigned long iflags;
458
459 spin_lock_irqsave(&phba->hbalock, iflags);
460 __lpfc_mbuf_free(phba, virt, dma);
461 spin_unlock_irqrestore(&phba->hbalock, iflags);
462 return;
463}
James Smarted957682007-06-17 19:56:37 -0500464
James Smarte59058c2008-08-24 21:49:00 -0400465/**
James Smartf358dd02017-02-12 13:52:34 -0800466 * lpfc_nvmet_buf_alloc - Allocate an nvmet_buf from the
467 * lpfc_sg_dma_buf_pool PCI pool
468 * @phba: HBA which owns the pool to allocate from
469 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
470 * @handle: used to return the DMA-mapped address of the nvmet_buf
471 *
472 * Description: Allocates a DMA-mapped buffer from the lpfc_sg_dma_buf_pool
473 * PCI pool. Allocates from generic pci_pool_alloc function.
474 *
475 * Returns:
476 * pointer to the allocated nvmet_buf on success
477 * NULL on failure
478 **/
479void *
480lpfc_nvmet_buf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
481{
482 void *ret;
483
484 ret = pci_pool_alloc(phba->lpfc_sg_dma_buf_pool, GFP_KERNEL, handle);
485 return ret;
486}
487
488/**
489 * lpfc_nvmet_buf_free - Free an nvmet_buf from the lpfc_sg_dma_buf_pool
490 * PCI pool
491 * @phba: HBA which owns the pool to return to
492 * @virt: nvmet_buf to free
493 * @dma: the DMA-mapped address of the lpfc_sg_dma_buf_pool to be freed
494 *
495 * Returns: None
496 **/
497void
498lpfc_nvmet_buf_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma)
499{
500 pci_pool_free(phba->lpfc_sg_dma_buf_pool, virt, dma);
501}
502
503/**
James Smart3621a712009-04-06 18:47:14 -0400504 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400505 * @phba: HBA to allocate HBQ buffer for
506 *
James Smartda0436e2009-05-22 14:51:39 -0400507 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400508 * pool along a non-DMA-mapped container for it.
509 *
510 * Notes: Not interrupt-safe. Must be called with no locks held.
511 *
512 * Returns:
513 * pointer to HBQ on success
514 * NULL on failure
515 **/
James Smart51ef4c22007-08-02 11:10:31 -0400516struct hbq_dmabuf *
517lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500518{
James Smart51ef4c22007-08-02 11:10:31 -0400519 struct hbq_dmabuf *hbqbp;
520
James Smart2e90f4b2011-12-13 13:22:37 -0500521 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smart51ef4c22007-08-02 11:10:31 -0400522 if (!hbqbp)
523 return NULL;
524
James Smart8568a4d2009-07-19 10:01:16 -0400525 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400526 &hbqbp->dbuf.phys);
527 if (!hbqbp->dbuf.virt) {
528 kfree(hbqbp);
529 return NULL;
530 }
James Smart895427b2017-02-12 13:52:30 -0800531 hbqbp->total_size = LPFC_BPL_SIZE;
James Smart51ef4c22007-08-02 11:10:31 -0400532 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500533}
534
James Smarte59058c2008-08-24 21:49:00 -0400535/**
James Smartda0436e2009-05-22 14:51:39 -0400536 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400537 * @phba: HBA buffer was allocated for
538 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
539 *
540 * Description: Frees both the container and the DMA-mapped buffer returned by
541 * lpfc_els_hbq_alloc.
542 *
543 * Notes: Can be called with or without locks held.
544 *
545 * Returns: None
546 **/
James Smarted957682007-06-17 19:56:37 -0500547void
James Smart51ef4c22007-08-02 11:10:31 -0400548lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500549{
James Smart8568a4d2009-07-19 10:01:16 -0400550 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400551 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500552 return;
553}
554
James Smarte59058c2008-08-24 21:49:00 -0400555/**
James Smartda0436e2009-05-22 14:51:39 -0400556 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
557 * @phba: HBA to allocate a receive buffer for
558 *
559 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
560 * pool along a non-DMA-mapped container for it.
561 *
562 * Notes: Not interrupt-safe. Must be called with no locks held.
563 *
564 * Returns:
565 * pointer to HBQ on success
566 * NULL on failure
567 **/
568struct hbq_dmabuf *
569lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
570{
571 struct hbq_dmabuf *dma_buf;
572
James Smart2e90f4b2011-12-13 13:22:37 -0500573 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -0400574 if (!dma_buf)
575 return NULL;
576
577 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
578 &dma_buf->hbuf.phys);
579 if (!dma_buf->hbuf.virt) {
580 kfree(dma_buf);
581 return NULL;
582 }
583 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
584 &dma_buf->dbuf.phys);
585 if (!dma_buf->dbuf.virt) {
586 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
587 dma_buf->hbuf.phys);
588 kfree(dma_buf);
589 return NULL;
590 }
James Smart895427b2017-02-12 13:52:30 -0800591 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -0400592 return dma_buf;
593}
594
595/**
596 * lpfc_sli4_rb_free - Frees a receive buffer
597 * @phba: HBA buffer was allocated for
598 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
599 *
600 * Description: Frees both the container and the DMA-mapped buffers returned by
601 * lpfc_sli4_rb_alloc.
602 *
603 * Notes: Can be called with or without locks held.
604 *
605 * Returns: None
606 **/
607void
608lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
609{
610 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
611 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
612 kfree(dmab);
James Smartda0436e2009-05-22 14:51:39 -0400613}
614
615/**
James Smartf358dd02017-02-12 13:52:34 -0800616 * lpfc_sli4_nvmet_alloc - Allocate an SLI4 Receive buffer
617 * @phba: HBA to allocate a receive buffer for
618 *
619 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
620 * pool along a non-DMA-mapped container for it.
621 *
622 * Notes: Not interrupt-safe. Must be called with no locks held.
623 *
624 * Returns:
625 * pointer to HBQ on success
626 * NULL on failure
627 **/
628struct rqb_dmabuf *
629lpfc_sli4_nvmet_alloc(struct lpfc_hba *phba)
630{
631 struct rqb_dmabuf *dma_buf;
632 struct lpfc_iocbq *nvmewqe;
633 union lpfc_wqe128 *wqe;
634
635 dma_buf = kzalloc(sizeof(struct rqb_dmabuf), GFP_KERNEL);
636 if (!dma_buf)
637 return NULL;
638
639 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
640 &dma_buf->hbuf.phys);
641 if (!dma_buf->hbuf.virt) {
642 kfree(dma_buf);
643 return NULL;
644 }
James Smart3c603be2017-05-15 15:20:44 -0700645 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_nvmet_drb_pool,
646 GFP_KERNEL, &dma_buf->dbuf.phys);
James Smartf358dd02017-02-12 13:52:34 -0800647 if (!dma_buf->dbuf.virt) {
648 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
649 dma_buf->hbuf.phys);
650 kfree(dma_buf);
651 return NULL;
652 }
James Smart3c603be2017-05-15 15:20:44 -0700653 dma_buf->total_size = LPFC_NVMET_DATA_BUF_SIZE;
James Smartf358dd02017-02-12 13:52:34 -0800654
655 dma_buf->context = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx),
656 GFP_KERNEL);
657 if (!dma_buf->context) {
James Smart3c603be2017-05-15 15:20:44 -0700658 pci_pool_free(phba->lpfc_nvmet_drb_pool, dma_buf->dbuf.virt,
James Smartf358dd02017-02-12 13:52:34 -0800659 dma_buf->dbuf.phys);
660 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
661 dma_buf->hbuf.phys);
662 kfree(dma_buf);
663 return NULL;
664 }
665
666 dma_buf->iocbq = lpfc_sli_get_iocbq(phba);
James Smartf358dd02017-02-12 13:52:34 -0800667 if (!dma_buf->iocbq) {
668 kfree(dma_buf->context);
James Smart3c603be2017-05-15 15:20:44 -0700669 pci_pool_free(phba->lpfc_nvmet_drb_pool, dma_buf->dbuf.virt,
James Smartf358dd02017-02-12 13:52:34 -0800670 dma_buf->dbuf.phys);
671 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
672 dma_buf->hbuf.phys);
673 kfree(dma_buf);
674 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
675 "2621 Ran out of nvmet iocb/WQEs\n");
676 return NULL;
677 }
James Smart332ba3b2017-03-04 09:30:23 -0800678 dma_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
James Smartf358dd02017-02-12 13:52:34 -0800679 nvmewqe = dma_buf->iocbq;
680 wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
681 /* Initialize WQE */
682 memset(wqe, 0, sizeof(union lpfc_wqe));
683 /* Word 7 */
684 bf_set(wqe_ct, &wqe->generic.wqe_com, SLI4_CT_RPI);
685 bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
686 bf_set(wqe_pu, &wqe->generic.wqe_com, 1);
687 /* Word 10 */
688 bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
689 bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
690 bf_set(wqe_qosd, &wqe->generic.wqe_com, 0);
691
692 dma_buf->iocbq->context1 = NULL;
693 spin_lock(&phba->sli4_hba.sgl_list_lock);
694 dma_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, dma_buf->iocbq);
695 spin_unlock(&phba->sli4_hba.sgl_list_lock);
696 if (!dma_buf->sglq) {
697 lpfc_sli_release_iocbq(phba, dma_buf->iocbq);
698 kfree(dma_buf->context);
James Smart3c603be2017-05-15 15:20:44 -0700699 pci_pool_free(phba->lpfc_nvmet_drb_pool, dma_buf->dbuf.virt,
James Smartf358dd02017-02-12 13:52:34 -0800700 dma_buf->dbuf.phys);
701 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
702 dma_buf->hbuf.phys);
703 kfree(dma_buf);
704 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
705 "6132 Ran out of nvmet XRIs\n");
706 return NULL;
707 }
708 return dma_buf;
709}
710
711/**
712 * lpfc_sli4_nvmet_free - Frees a receive buffer
713 * @phba: HBA buffer was allocated for
714 * @dmab: DMA Buffer container returned by lpfc_sli4_rbq_alloc
715 *
716 * Description: Frees both the container and the DMA-mapped buffers returned by
717 * lpfc_sli4_nvmet_alloc.
718 *
719 * Notes: Can be called with or without locks held.
720 *
721 * Returns: None
722 **/
723void
724lpfc_sli4_nvmet_free(struct lpfc_hba *phba, struct rqb_dmabuf *dmab)
725{
726 unsigned long flags;
727
728 __lpfc_clear_active_sglq(phba, dmab->sglq->sli4_lxritag);
729 dmab->sglq->state = SGL_FREED;
730 dmab->sglq->ndlp = NULL;
731
732 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, flags);
733 list_add_tail(&dmab->sglq->list, &phba->sli4_hba.lpfc_nvmet_sgl_list);
734 spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, flags);
735
736 lpfc_sli_release_iocbq(phba, dmab->iocbq);
737 kfree(dmab->context);
738 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
James Smart3c603be2017-05-15 15:20:44 -0700739 pci_pool_free(phba->lpfc_nvmet_drb_pool,
740 dmab->dbuf.virt, dmab->dbuf.phys);
James Smartf358dd02017-02-12 13:52:34 -0800741 kfree(dmab);
742}
743
744/**
James Smart3621a712009-04-06 18:47:14 -0400745 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400746 * @phba: HBA buffer is associated with
747 * @mp: Buffer to free
748 *
749 * Description: Frees the given DMA buffer in the appropriate way given if the
750 * HBA is running in SLI3 mode with HBQs enabled.
751 *
752 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
753 *
754 * Returns: None
755 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500756void
757lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
758{
759 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500760 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500761
James Smart7f5f3d02008-02-08 18:50:14 -0500762 if (!mp)
763 return;
764
James Smart92d7f7b2007-06-17 19:56:38 -0500765 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart895427b2017-02-12 13:52:30 -0800766 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500767 /* Check whether HBQ is still in use */
768 spin_lock_irqsave(&phba->hbalock, flags);
769 if (!phba->hbq_in_use) {
770 spin_unlock_irqrestore(&phba->hbalock, flags);
771 return;
772 }
James Smart3163f722008-02-08 18:50:25 -0500773 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500774 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400775 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
776 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500777 } else {
778 lpfc_sli_free_hbq(phba, hbq_entry);
779 }
James Smart3163f722008-02-08 18:50:25 -0500780 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500781 } else {
782 lpfc_mbuf_free(phba, mp->virt, mp->phys);
783 kfree(mp);
784 }
785 return;
786}
James Smartd613b6a2017-02-12 13:52:37 -0800787
788/**
789 * lpfc_rq_buf_free - Free a RQ DMA buffer
790 * @phba: HBA buffer is associated with
791 * @mp: Buffer to free
792 *
793 * Description: Frees the given DMA buffer in the appropriate way given by
794 * reposting it to its associated RQ so it can be reused.
795 *
796 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
797 *
798 * Returns: None
799 **/
800void
801lpfc_rq_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
802{
803 struct lpfc_rqb *rqbp;
804 struct lpfc_rqe hrqe;
805 struct lpfc_rqe drqe;
806 struct rqb_dmabuf *rqb_entry;
807 unsigned long flags;
808 int rc;
809
810 if (!mp)
811 return;
812
813 rqb_entry = container_of(mp, struct rqb_dmabuf, hbuf);
814 rqbp = rqb_entry->hrq->rqbp;
815
816 spin_lock_irqsave(&phba->hbalock, flags);
817 list_del(&rqb_entry->hbuf.list);
818 hrqe.address_lo = putPaddrLow(rqb_entry->hbuf.phys);
819 hrqe.address_hi = putPaddrHigh(rqb_entry->hbuf.phys);
820 drqe.address_lo = putPaddrLow(rqb_entry->dbuf.phys);
821 drqe.address_hi = putPaddrHigh(rqb_entry->dbuf.phys);
822 rc = lpfc_sli4_rq_put(rqb_entry->hrq, rqb_entry->drq, &hrqe, &drqe);
823 if (rc < 0) {
824 (rqbp->rqb_free_buffer)(phba, rqb_entry);
825 } else {
826 list_add_tail(&rqb_entry->hbuf.list, &rqbp->rqb_buffer_list);
827 rqbp->buffer_count++;
828 }
829
830 spin_unlock_irqrestore(&phba->hbalock, flags);
831}