blob: 5986c7957199df6ef97343a3c0402931cbdeb7ad [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 Smarte59058c2008-08-24 21:49:00 -0400217/**
James Smartda0436e2009-05-22 14:51:39 -0400218 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400219 * @phba: HBA to free memory for
220 *
James Smartda0436e2009-05-22 14:51:39 -0400221 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
222 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400223 *
224 * Returns: None
225 **/
dea31012005-04-17 16:05:31 -0500226void
James Smartda0436e2009-05-22 14:51:39 -0400227lpfc_mem_free(struct lpfc_hba *phba)
228{
229 int i;
230 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart1ba981f2014-02-20 09:56:45 -0500231 struct lpfc_device_data *device_data;
James Smartda0436e2009-05-22 14:51:39 -0400232
James Smartda0436e2009-05-22 14:51:39 -0400233 /* Free HBQ pools */
234 lpfc_sli_hbqbuf_free_all(phba);
James Smart8568a4d2009-07-19 10:01:16 -0400235 if (phba->lpfc_drb_pool)
236 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400237 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400238 if (phba->lpfc_hrb_pool)
239 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400240 phba->lpfc_hrb_pool = NULL;
James Smart895427b2017-02-12 13:52:30 -0800241 if (phba->txrdy_payload_pool)
242 pci_pool_destroy(phba->txrdy_payload_pool);
243 phba->txrdy_payload_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400244
James Smart8568a4d2009-07-19 10:01:16 -0400245 if (phba->lpfc_hbq_pool)
246 pci_pool_destroy(phba->lpfc_hbq_pool);
247 phba->lpfc_hbq_pool = NULL;
Martin K. Petersend65c8ff2016-05-09 21:39:43 -0400248
249 if (phba->rrq_pool)
250 mempool_destroy(phba->rrq_pool);
James Smart21a688c2012-08-03 12:34:15 -0400251 phba->rrq_pool = NULL;
252
James Smartda0436e2009-05-22 14:51:39 -0400253 /* Free NLP memory pool */
254 mempool_destroy(phba->nlp_mem_pool);
255 phba->nlp_mem_pool = NULL;
Martin K. Petersend65c8ff2016-05-09 21:39:43 -0400256 if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
James Smartcff261f2013-12-17 20:29:47 -0500257 mempool_destroy(phba->active_rrq_pool);
258 phba->active_rrq_pool = NULL;
259 }
James Smartda0436e2009-05-22 14:51:39 -0400260
261 /* Free mbox memory pool */
262 mempool_destroy(phba->mbox_mem_pool);
263 phba->mbox_mem_pool = NULL;
264
265 /* Free MBUF memory pool */
266 for (i = 0; i < pool->current_count; i++)
267 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
268 pool->elements[i].phys);
269 kfree(pool->elements);
270
271 pci_pool_destroy(phba->lpfc_mbuf_pool);
272 phba->lpfc_mbuf_pool = NULL;
273
274 /* Free DMA buffer memory pool */
James Smart895427b2017-02-12 13:52:30 -0800275 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
276 phba->lpfc_sg_dma_buf_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400277
James Smart1ba981f2014-02-20 09:56:45 -0500278 /* Free Device Data memory pool */
279 if (phba->device_data_mem_pool) {
280 /* Ensure all objects have been returned to the pool */
281 while (!list_empty(&phba->luns)) {
282 device_data = list_first_entry(&phba->luns,
283 struct lpfc_device_data,
284 listentry);
285 list_del(&device_data->listentry);
286 mempool_free(device_data, phba->device_data_mem_pool);
287 }
288 mempool_destroy(phba->device_data_mem_pool);
289 }
290 phba->device_data_mem_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400291 return;
292}
293
294/**
295 * lpfc_mem_free_all - Frees all PCI and driver memory
296 * @phba: HBA to free memory for
297 *
298 * Description: Free memory from PCI and driver memory pools and also those
James Smart895427b2017-02-12 13:52:30 -0800299 * used : lpfc_sg_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
James Smartda0436e2009-05-22 14:51:39 -0400300 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
301 * the VPI bitmask.
302 *
303 * Returns: None
304 **/
305void
306lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500307{
308 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500309 LPFC_MBOXQ_t *mbox, *next_mbox;
310 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500311
James Smartda0436e2009-05-22 14:51:39 -0400312 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500313 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
314 mp = (struct lpfc_dmabuf *) (mbox->context1);
315 if (mp) {
316 lpfc_mbuf_free(phba, mp->virt, mp->phys);
317 kfree(mp);
318 }
319 list_del(&mbox->list);
320 mempool_free(mbox, phba->mbox_mem_pool);
321 }
James Smartda0436e2009-05-22 14:51:39 -0400322 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500323 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
324 mp = (struct lpfc_dmabuf *) (mbox->context1);
325 if (mp) {
326 lpfc_mbuf_free(phba, mp->virt, mp->phys);
327 kfree(mp);
328 }
329 list_del(&mbox->list);
330 mempool_free(mbox, phba->mbox_mem_pool);
331 }
James Smartda0436e2009-05-22 14:51:39 -0400332 /* Free the active mailbox command back to the mailbox memory pool */
333 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500334 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400335 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500336 if (psli->mbox_active) {
337 mbox = psli->mbox_active;
338 mp = (struct lpfc_dmabuf *) (mbox->context1);
339 if (mp) {
340 lpfc_mbuf_free(phba, mp->virt, mp->phys);
341 kfree(mp);
342 }
343 mempool_free(mbox, phba->mbox_mem_pool);
344 psli->mbox_active = NULL;
345 }
346
James Smartda0436e2009-05-22 14:51:39 -0400347 /* Free and destroy all the allocated memory pools */
348 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500349
James Smarte59058c2008-08-24 21:49:00 -0400350 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400351 kfree(psli->iocbq_lookup);
352 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400353
354 return;
dea31012005-04-17 16:05:31 -0500355}
356
James Smarte59058c2008-08-24 21:49:00 -0400357/**
James Smart3621a712009-04-06 18:47:14 -0400358 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400359 * @phba: HBA which owns the pool to allocate from
360 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
361 * @handle: used to return the DMA-mapped address of the mbuf
362 *
363 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
364 * Allocates from generic pci_pool_alloc function first and if that fails and
365 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
366 * HBA's pool.
367 *
368 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
369 * phba->hbalock.
370 *
371 * Returns:
372 * pointer to the allocated mbuf on success
373 * NULL on failure
374 **/
dea31012005-04-17 16:05:31 -0500375void *
376lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
377{
378 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500379 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500380 void *ret;
381
382 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
383
James Smart2e0fef82007-06-17 19:56:36 -0500384 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500385 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500386 pool->current_count--;
387 ret = pool->elements[pool->current_count].virt;
388 *handle = pool->elements[pool->current_count].phys;
389 }
James Smart2e0fef82007-06-17 19:56:36 -0500390 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500391 return ret;
392}
393
James Smarte59058c2008-08-24 21:49:00 -0400394/**
James Smart3621a712009-04-06 18:47:14 -0400395 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400396 * @phba: HBA which owns the pool to return to
397 * @virt: mbuf to free
398 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
399 *
400 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
401 * it is below its max_count, frees the mbuf otherwise.
402 *
403 * Notes: Must be called with phba->hbalock held to synchronize access to
404 * lpfc_mbuf_safety_pool.
405 *
406 * Returns: None
407 **/
dea31012005-04-17 16:05:31 -0500408void
James Smart2e0fef82007-06-17 19:56:36 -0500409__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500410{
411 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
412
413 if (pool->current_count < pool->max_count) {
414 pool->elements[pool->current_count].virt = virt;
415 pool->elements[pool->current_count].phys = dma;
416 pool->current_count++;
417 } else {
418 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
419 }
420 return;
421}
James Smart2e0fef82007-06-17 19:56:36 -0500422
James Smarte59058c2008-08-24 21:49:00 -0400423/**
James Smart3621a712009-04-06 18:47:14 -0400424 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400425 * @phba: HBA which owns the pool to return to
426 * @virt: mbuf to free
427 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
428 *
429 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
430 * it is below its max_count, frees the mbuf otherwise.
431 *
432 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
433 *
434 * Returns: None
435 **/
James Smart2e0fef82007-06-17 19:56:36 -0500436void
437lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
438{
439 unsigned long iflags;
440
441 spin_lock_irqsave(&phba->hbalock, iflags);
442 __lpfc_mbuf_free(phba, virt, dma);
443 spin_unlock_irqrestore(&phba->hbalock, iflags);
444 return;
445}
James Smarted957682007-06-17 19:56:37 -0500446
James Smarte59058c2008-08-24 21:49:00 -0400447/**
James Smartf358dd02017-02-12 13:52:34 -0800448 * lpfc_nvmet_buf_alloc - Allocate an nvmet_buf from the
449 * lpfc_sg_dma_buf_pool PCI pool
450 * @phba: HBA which owns the pool to allocate from
451 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
452 * @handle: used to return the DMA-mapped address of the nvmet_buf
453 *
454 * Description: Allocates a DMA-mapped buffer from the lpfc_sg_dma_buf_pool
455 * PCI pool. Allocates from generic pci_pool_alloc function.
456 *
457 * Returns:
458 * pointer to the allocated nvmet_buf on success
459 * NULL on failure
460 **/
461void *
462lpfc_nvmet_buf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
463{
464 void *ret;
465
466 ret = pci_pool_alloc(phba->lpfc_sg_dma_buf_pool, GFP_KERNEL, handle);
467 return ret;
468}
469
470/**
471 * lpfc_nvmet_buf_free - Free an nvmet_buf from the lpfc_sg_dma_buf_pool
472 * PCI pool
473 * @phba: HBA which owns the pool to return to
474 * @virt: nvmet_buf to free
475 * @dma: the DMA-mapped address of the lpfc_sg_dma_buf_pool to be freed
476 *
477 * Returns: None
478 **/
479void
480lpfc_nvmet_buf_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma)
481{
482 pci_pool_free(phba->lpfc_sg_dma_buf_pool, virt, dma);
483}
484
485/**
James Smart3621a712009-04-06 18:47:14 -0400486 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400487 * @phba: HBA to allocate HBQ buffer for
488 *
James Smartda0436e2009-05-22 14:51:39 -0400489 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400490 * pool along a non-DMA-mapped container for it.
491 *
492 * Notes: Not interrupt-safe. Must be called with no locks held.
493 *
494 * Returns:
495 * pointer to HBQ on success
496 * NULL on failure
497 **/
James Smart51ef4c22007-08-02 11:10:31 -0400498struct hbq_dmabuf *
499lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500500{
James Smart51ef4c22007-08-02 11:10:31 -0400501 struct hbq_dmabuf *hbqbp;
502
James Smart2e90f4b2011-12-13 13:22:37 -0500503 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smart51ef4c22007-08-02 11:10:31 -0400504 if (!hbqbp)
505 return NULL;
506
James Smart8568a4d2009-07-19 10:01:16 -0400507 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400508 &hbqbp->dbuf.phys);
509 if (!hbqbp->dbuf.virt) {
510 kfree(hbqbp);
511 return NULL;
512 }
James Smart895427b2017-02-12 13:52:30 -0800513 hbqbp->total_size = LPFC_BPL_SIZE;
James Smart51ef4c22007-08-02 11:10:31 -0400514 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500515}
516
James Smarte59058c2008-08-24 21:49:00 -0400517/**
James Smartda0436e2009-05-22 14:51:39 -0400518 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400519 * @phba: HBA buffer was allocated for
520 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
521 *
522 * Description: Frees both the container and the DMA-mapped buffer returned by
523 * lpfc_els_hbq_alloc.
524 *
525 * Notes: Can be called with or without locks held.
526 *
527 * Returns: None
528 **/
James Smarted957682007-06-17 19:56:37 -0500529void
James Smart51ef4c22007-08-02 11:10:31 -0400530lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500531{
James Smart8568a4d2009-07-19 10:01:16 -0400532 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400533 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500534 return;
535}
536
James Smarte59058c2008-08-24 21:49:00 -0400537/**
James Smartda0436e2009-05-22 14:51:39 -0400538 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
539 * @phba: HBA to allocate a receive buffer for
540 *
541 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
542 * pool along a non-DMA-mapped container for it.
543 *
544 * Notes: Not interrupt-safe. Must be called with no locks held.
545 *
546 * Returns:
547 * pointer to HBQ on success
548 * NULL on failure
549 **/
550struct hbq_dmabuf *
551lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
552{
553 struct hbq_dmabuf *dma_buf;
554
James Smart2e90f4b2011-12-13 13:22:37 -0500555 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -0400556 if (!dma_buf)
557 return NULL;
558
559 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
560 &dma_buf->hbuf.phys);
561 if (!dma_buf->hbuf.virt) {
562 kfree(dma_buf);
563 return NULL;
564 }
565 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
566 &dma_buf->dbuf.phys);
567 if (!dma_buf->dbuf.virt) {
568 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
569 dma_buf->hbuf.phys);
570 kfree(dma_buf);
571 return NULL;
572 }
James Smart895427b2017-02-12 13:52:30 -0800573 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -0400574 return dma_buf;
575}
576
577/**
578 * lpfc_sli4_rb_free - Frees a receive buffer
579 * @phba: HBA buffer was allocated for
580 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
581 *
582 * Description: Frees both the container and the DMA-mapped buffers returned by
583 * lpfc_sli4_rb_alloc.
584 *
585 * Notes: Can be called with or without locks held.
586 *
587 * Returns: None
588 **/
589void
590lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
591{
592 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
593 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
594 kfree(dmab);
James Smartda0436e2009-05-22 14:51:39 -0400595}
596
597/**
James Smartf358dd02017-02-12 13:52:34 -0800598 * lpfc_sli4_nvmet_alloc - Allocate an SLI4 Receive buffer
599 * @phba: HBA to allocate a receive buffer for
600 *
601 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
602 * pool along a non-DMA-mapped container for it.
603 *
604 * Notes: Not interrupt-safe. Must be called with no locks held.
605 *
606 * Returns:
607 * pointer to HBQ on success
608 * NULL on failure
609 **/
610struct rqb_dmabuf *
611lpfc_sli4_nvmet_alloc(struct lpfc_hba *phba)
612{
613 struct rqb_dmabuf *dma_buf;
614 struct lpfc_iocbq *nvmewqe;
615 union lpfc_wqe128 *wqe;
616
617 dma_buf = kzalloc(sizeof(struct rqb_dmabuf), GFP_KERNEL);
618 if (!dma_buf)
619 return NULL;
620
621 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
622 &dma_buf->hbuf.phys);
623 if (!dma_buf->hbuf.virt) {
624 kfree(dma_buf);
625 return NULL;
626 }
627 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
628 &dma_buf->dbuf.phys);
629 if (!dma_buf->dbuf.virt) {
630 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
631 dma_buf->hbuf.phys);
632 kfree(dma_buf);
633 return NULL;
634 }
635 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
636
637 dma_buf->context = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx),
638 GFP_KERNEL);
639 if (!dma_buf->context) {
640 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
641 dma_buf->dbuf.phys);
642 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
643 dma_buf->hbuf.phys);
644 kfree(dma_buf);
645 return NULL;
646 }
647
648 dma_buf->iocbq = lpfc_sli_get_iocbq(phba);
James Smartf358dd02017-02-12 13:52:34 -0800649 if (!dma_buf->iocbq) {
650 kfree(dma_buf->context);
651 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
652 dma_buf->dbuf.phys);
653 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
654 dma_buf->hbuf.phys);
655 kfree(dma_buf);
656 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
657 "2621 Ran out of nvmet iocb/WQEs\n");
658 return NULL;
659 }
James Smart332ba3b2017-03-04 09:30:23 -0800660 dma_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
James Smartf358dd02017-02-12 13:52:34 -0800661 nvmewqe = dma_buf->iocbq;
662 wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
663 /* Initialize WQE */
664 memset(wqe, 0, sizeof(union lpfc_wqe));
665 /* Word 7 */
666 bf_set(wqe_ct, &wqe->generic.wqe_com, SLI4_CT_RPI);
667 bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
668 bf_set(wqe_pu, &wqe->generic.wqe_com, 1);
669 /* Word 10 */
670 bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
671 bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
672 bf_set(wqe_qosd, &wqe->generic.wqe_com, 0);
673
674 dma_buf->iocbq->context1 = NULL;
675 spin_lock(&phba->sli4_hba.sgl_list_lock);
676 dma_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, dma_buf->iocbq);
677 spin_unlock(&phba->sli4_hba.sgl_list_lock);
678 if (!dma_buf->sglq) {
679 lpfc_sli_release_iocbq(phba, dma_buf->iocbq);
680 kfree(dma_buf->context);
681 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
682 dma_buf->dbuf.phys);
683 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
684 dma_buf->hbuf.phys);
685 kfree(dma_buf);
686 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
687 "6132 Ran out of nvmet XRIs\n");
688 return NULL;
689 }
690 return dma_buf;
691}
692
693/**
694 * lpfc_sli4_nvmet_free - Frees a receive buffer
695 * @phba: HBA buffer was allocated for
696 * @dmab: DMA Buffer container returned by lpfc_sli4_rbq_alloc
697 *
698 * Description: Frees both the container and the DMA-mapped buffers returned by
699 * lpfc_sli4_nvmet_alloc.
700 *
701 * Notes: Can be called with or without locks held.
702 *
703 * Returns: None
704 **/
705void
706lpfc_sli4_nvmet_free(struct lpfc_hba *phba, struct rqb_dmabuf *dmab)
707{
708 unsigned long flags;
709
710 __lpfc_clear_active_sglq(phba, dmab->sglq->sli4_lxritag);
711 dmab->sglq->state = SGL_FREED;
712 dmab->sglq->ndlp = NULL;
713
714 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, flags);
715 list_add_tail(&dmab->sglq->list, &phba->sli4_hba.lpfc_nvmet_sgl_list);
716 spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, flags);
717
718 lpfc_sli_release_iocbq(phba, dmab->iocbq);
719 kfree(dmab->context);
720 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
721 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
722 kfree(dmab);
723}
724
725/**
James Smart3621a712009-04-06 18:47:14 -0400726 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400727 * @phba: HBA buffer is associated with
728 * @mp: Buffer to free
729 *
730 * Description: Frees the given DMA buffer in the appropriate way given if the
731 * HBA is running in SLI3 mode with HBQs enabled.
732 *
733 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
734 *
735 * Returns: None
736 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500737void
738lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
739{
740 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500741 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500742
James Smart7f5f3d02008-02-08 18:50:14 -0500743 if (!mp)
744 return;
745
James Smart92d7f7b2007-06-17 19:56:38 -0500746 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart895427b2017-02-12 13:52:30 -0800747 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500748 /* Check whether HBQ is still in use */
749 spin_lock_irqsave(&phba->hbalock, flags);
750 if (!phba->hbq_in_use) {
751 spin_unlock_irqrestore(&phba->hbalock, flags);
752 return;
753 }
James Smart3163f722008-02-08 18:50:25 -0500754 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500755 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400756 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
757 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500758 } else {
759 lpfc_sli_free_hbq(phba, hbq_entry);
760 }
James Smart3163f722008-02-08 18:50:25 -0500761 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500762 } else {
763 lpfc_mbuf_free(phba, mp->virt, mp->phys);
764 kfree(mp);
765 }
766 return;
767}
James Smartd613b6a2017-02-12 13:52:37 -0800768
769/**
770 * lpfc_rq_buf_free - Free a RQ DMA buffer
771 * @phba: HBA buffer is associated with
772 * @mp: Buffer to free
773 *
774 * Description: Frees the given DMA buffer in the appropriate way given by
775 * reposting it to its associated RQ so it can be reused.
776 *
777 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
778 *
779 * Returns: None
780 **/
781void
782lpfc_rq_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
783{
784 struct lpfc_rqb *rqbp;
785 struct lpfc_rqe hrqe;
786 struct lpfc_rqe drqe;
787 struct rqb_dmabuf *rqb_entry;
788 unsigned long flags;
789 int rc;
790
791 if (!mp)
792 return;
793
794 rqb_entry = container_of(mp, struct rqb_dmabuf, hbuf);
795 rqbp = rqb_entry->hrq->rqbp;
796
797 spin_lock_irqsave(&phba->hbalock, flags);
798 list_del(&rqb_entry->hbuf.list);
799 hrqe.address_lo = putPaddrLow(rqb_entry->hbuf.phys);
800 hrqe.address_hi = putPaddrHigh(rqb_entry->hbuf.phys);
801 drqe.address_lo = putPaddrLow(rqb_entry->dbuf.phys);
802 drqe.address_hi = putPaddrHigh(rqb_entry->dbuf.phys);
803 rc = lpfc_sli4_rq_put(rqb_entry->hrq, rqb_entry->drq, &hrqe, &drqe);
804 if (rc < 0) {
805 (rqbp->rqb_free_buffer)(phba, rqb_entry);
806 } else {
807 list_add_tail(&rqb_entry->hbuf.list, &rqbp->rqb_buffer_list);
808 rqbp->buffer_count++;
809 }
810
811 spin_unlock_irqrestore(&phba->hbalock, flags);
812}