blob: 2437ec5f48631d28bea20c3c96e66ae851949e73 [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 Smartb857ff32014-04-04 13:52:50 -04004 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/mempool.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050024#include <linux/pci.h>
25#include <linux/interrupt.h>
26
James Smart895427b2017-02-12 13:52:30 -080027#include <scsi/scsi.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040028#include <scsi/scsi_device.h>
29#include <scsi/scsi_transport_fc.h>
James Smart895427b2017-02-12 13:52:30 -080030#include <scsi/fc/fc_fs.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031
James Smart895427b2017-02-12 13:52:30 -080032#include <linux/nvme-fc-driver.h>
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040033
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_hw.h"
36#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040038#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080041#include "lpfc_scsi.h"
42#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080043#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_crtn.h"
James Smartcff261f2013-12-17 20:29:47 -050045#include "lpfc_logmsg.h"
dea31012005-04-17 16:05:31 -050046
47#define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
48#define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
James Smart1ba981f2014-02-20 09:56:45 -050049#define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */
dea31012005-04-17 16:05:31 -050050
James Smartcff261f2013-12-17 20:29:47 -050051int
52lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
53 size_t bytes;
54 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
55
56 if (max_xri <= 0)
57 return -ENOMEM;
58 bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) *
59 sizeof(unsigned long);
60 phba->cfg_rrq_xri_bitmap_sz = bytes;
61 phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
62 bytes);
63 if (!phba->active_rrq_pool)
64 return -ENOMEM;
65 else
66 return 0;
67}
James Smart2e0fef82007-06-17 19:56:36 -050068
James Smarte59058c2008-08-24 21:49:00 -040069/**
James Smart3621a712009-04-06 18:47:14 -040070 * lpfc_mem_alloc - create and allocate all PCI and memory pools
James Smarte59058c2008-08-24 21:49:00 -040071 * @phba: HBA to allocate pools for
72 *
James Smart895427b2017-02-12 13:52:30 -080073 * Description: Creates and allocates PCI pools lpfc_sg_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -040074 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
James Smarte59058c2008-08-24 21:49:00 -040075 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
76 *
77 * Notes: Not interrupt-safe. Must be called with no locks held. If any
78 * allocation fails, frees all successfully allocated memory before returning.
79 *
80 * Returns:
81 * 0 on success
82 * -ENOMEM on failure (if any memory allocations fail)
83 **/
dea31012005-04-17 16:05:31 -050084int
James Smartda0436e2009-05-22 14:51:39 -040085lpfc_mem_alloc(struct lpfc_hba *phba, int align)
dea31012005-04-17 16:05:31 -050086{
87 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
88 int i;
89
James Smart96f70772013-04-17 20:16:15 -040090 if (phba->sli_rev == LPFC_SLI_REV4) {
91 /* Calculate alignment */
92 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
93 i = phba->cfg_sg_dma_buf_size;
94 else
95 i = SLI4_PAGE_SIZE;
96
James Smart895427b2017-02-12 13:52:30 -080097 phba->lpfc_sg_dma_buf_pool =
98 pci_pool_create("lpfc_sg_dma_buf_pool",
99 phba->pcidev,
100 phba->cfg_sg_dma_buf_size,
101 i, 0);
102 if (!phba->lpfc_sg_dma_buf_pool)
103 goto fail;
James Smart96f70772013-04-17 20:16:15 -0400104
James Smart895427b2017-02-12 13:52:30 -0800105 } else {
106 phba->lpfc_sg_dma_buf_pool =
107 pci_pool_create("lpfc_sg_dma_buf_pool",
108 phba->pcidev, phba->cfg_sg_dma_buf_size,
109 align, 0);
110
111 if (!phba->lpfc_sg_dma_buf_pool)
112 goto fail;
113 }
dea31012005-04-17 16:05:31 -0500114
115 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
James Smartda0436e2009-05-22 14:51:39 -0400116 LPFC_BPL_SIZE,
117 align, 0);
dea31012005-04-17 16:05:31 -0500118 if (!phba->lpfc_mbuf_pool)
119 goto fail_free_dma_buf_pool;
120
121 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
122 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100123 if (!pool->elements)
124 goto fail_free_lpfc_mbuf_pool;
125
dea31012005-04-17 16:05:31 -0500126 pool->max_count = 0;
127 pool->current_count = 0;
128 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
129 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
130 GFP_KERNEL, &pool->elements[i].phys);
131 if (!pool->elements[i].virt)
132 goto fail_free_mbuf_pool;
133 pool->max_count++;
134 pool->current_count++;
135 }
136
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800137 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
138 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -0500139 if (!phba->mbox_mem_pool)
140 goto fail_free_mbuf_pool;
141
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800142 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
143 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -0500144 if (!phba->nlp_mem_pool)
145 goto fail_free_mbox_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400146
147 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart19ca7602010-11-20 23:11:55 -0500148 phba->rrq_pool =
149 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
150 sizeof(struct lpfc_node_rrq));
151 if (!phba->rrq_pool)
152 goto fail_free_nlp_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400153 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400154 phba->pcidev,
155 LPFC_HDR_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400156 if (!phba->lpfc_hrb_pool)
James Smart19ca7602010-11-20 23:11:55 -0500157 goto fail_free_rrq_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400158
159 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400160 phba->pcidev,
161 LPFC_DATA_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400162 if (!phba->lpfc_drb_pool)
163 goto fail_free_hrb_pool;
164 phba->lpfc_hbq_pool = NULL;
165 } else {
166 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
167 phba->pcidev, LPFC_BPL_SIZE, align, 0);
168 if (!phba->lpfc_hbq_pool)
169 goto fail_free_nlp_mem_pool;
170 phba->lpfc_hrb_pool = NULL;
171 phba->lpfc_drb_pool = NULL;
172 }
James Smart92d7f7b2007-06-17 19:56:38 -0500173
James Smart1ba981f2014-02-20 09:56:45 -0500174 if (phba->cfg_EnableXLane) {
175 phba->device_data_mem_pool = mempool_create_kmalloc_pool(
176 LPFC_DEVICE_DATA_POOL_SIZE,
177 sizeof(struct lpfc_device_data));
178 if (!phba->device_data_mem_pool)
James Smart895427b2017-02-12 13:52:30 -0800179 goto fail_free_drb_pool;
James Smart1ba981f2014-02-20 09:56:45 -0500180 } else {
181 phba->device_data_mem_pool = NULL;
182 }
183
dea31012005-04-17 16:05:31 -0500184 return 0;
James Smart895427b2017-02-12 13:52:30 -0800185fail_free_drb_pool:
186 pci_pool_destroy(phba->lpfc_drb_pool);
187 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400188 fail_free_hrb_pool:
James Smartda0436e2009-05-22 14:51:39 -0400189 pci_pool_destroy(phba->lpfc_hrb_pool);
190 phba->lpfc_hrb_pool = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500191 fail_free_rrq_mem_pool:
192 mempool_destroy(phba->rrq_pool);
193 phba->rrq_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500194 fail_free_nlp_mem_pool:
195 mempool_destroy(phba->nlp_mem_pool);
196 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500197 fail_free_mbox_pool:
198 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500199 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500200 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100201 while (i--)
dea31012005-04-17 16:05:31 -0500202 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
203 pool->elements[i].phys);
204 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100205 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500206 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500207 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500208 fail_free_dma_buf_pool:
James Smart895427b2017-02-12 13:52:30 -0800209 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
210 phba->lpfc_sg_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500211 fail:
212 return -ENOMEM;
213}
214
James Smarte59058c2008-08-24 21:49:00 -0400215/**
James Smartda0436e2009-05-22 14:51:39 -0400216 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400217 * @phba: HBA to free memory for
218 *
James Smartda0436e2009-05-22 14:51:39 -0400219 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
220 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400221 *
222 * Returns: None
223 **/
dea31012005-04-17 16:05:31 -0500224void
James Smartda0436e2009-05-22 14:51:39 -0400225lpfc_mem_free(struct lpfc_hba *phba)
226{
227 int i;
228 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart1ba981f2014-02-20 09:56:45 -0500229 struct lpfc_device_data *device_data;
James Smartda0436e2009-05-22 14:51:39 -0400230
James Smartda0436e2009-05-22 14:51:39 -0400231 /* Free HBQ pools */
232 lpfc_sli_hbqbuf_free_all(phba);
James Smart8568a4d2009-07-19 10:01:16 -0400233 if (phba->lpfc_drb_pool)
234 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400235 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400236 if (phba->lpfc_hrb_pool)
237 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400238 phba->lpfc_hrb_pool = NULL;
James Smart895427b2017-02-12 13:52:30 -0800239 if (phba->txrdy_payload_pool)
240 pci_pool_destroy(phba->txrdy_payload_pool);
241 phba->txrdy_payload_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400242
James Smart8568a4d2009-07-19 10:01:16 -0400243 if (phba->lpfc_hbq_pool)
244 pci_pool_destroy(phba->lpfc_hbq_pool);
245 phba->lpfc_hbq_pool = NULL;
Martin K. Petersend65c8ff2016-05-09 21:39:43 -0400246
247 if (phba->rrq_pool)
248 mempool_destroy(phba->rrq_pool);
James Smart21a688c2012-08-03 12:34:15 -0400249 phba->rrq_pool = NULL;
250
James Smartda0436e2009-05-22 14:51:39 -0400251 /* Free NLP memory pool */
252 mempool_destroy(phba->nlp_mem_pool);
253 phba->nlp_mem_pool = NULL;
Martin K. Petersend65c8ff2016-05-09 21:39:43 -0400254 if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
James Smartcff261f2013-12-17 20:29:47 -0500255 mempool_destroy(phba->active_rrq_pool);
256 phba->active_rrq_pool = NULL;
257 }
James Smartda0436e2009-05-22 14:51:39 -0400258
259 /* Free mbox memory pool */
260 mempool_destroy(phba->mbox_mem_pool);
261 phba->mbox_mem_pool = NULL;
262
263 /* Free MBUF memory pool */
264 for (i = 0; i < pool->current_count; i++)
265 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
266 pool->elements[i].phys);
267 kfree(pool->elements);
268
269 pci_pool_destroy(phba->lpfc_mbuf_pool);
270 phba->lpfc_mbuf_pool = NULL;
271
272 /* Free DMA buffer memory pool */
James Smart895427b2017-02-12 13:52:30 -0800273 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
274 phba->lpfc_sg_dma_buf_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400275
James Smart1ba981f2014-02-20 09:56:45 -0500276 /* Free Device Data memory pool */
277 if (phba->device_data_mem_pool) {
278 /* Ensure all objects have been returned to the pool */
279 while (!list_empty(&phba->luns)) {
280 device_data = list_first_entry(&phba->luns,
281 struct lpfc_device_data,
282 listentry);
283 list_del(&device_data->listentry);
284 mempool_free(device_data, phba->device_data_mem_pool);
285 }
286 mempool_destroy(phba->device_data_mem_pool);
287 }
288 phba->device_data_mem_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400289 return;
290}
291
292/**
293 * lpfc_mem_free_all - Frees all PCI and driver memory
294 * @phba: HBA to free memory for
295 *
296 * Description: Free memory from PCI and driver memory pools and also those
James Smart895427b2017-02-12 13:52:30 -0800297 * used : lpfc_sg_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
James Smartda0436e2009-05-22 14:51:39 -0400298 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
299 * the VPI bitmask.
300 *
301 * Returns: None
302 **/
303void
304lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500305{
306 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500307 LPFC_MBOXQ_t *mbox, *next_mbox;
308 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500309
James Smartda0436e2009-05-22 14:51:39 -0400310 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500311 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
312 mp = (struct lpfc_dmabuf *) (mbox->context1);
313 if (mp) {
314 lpfc_mbuf_free(phba, mp->virt, mp->phys);
315 kfree(mp);
316 }
317 list_del(&mbox->list);
318 mempool_free(mbox, phba->mbox_mem_pool);
319 }
James Smartda0436e2009-05-22 14:51:39 -0400320 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500321 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
322 mp = (struct lpfc_dmabuf *) (mbox->context1);
323 if (mp) {
324 lpfc_mbuf_free(phba, mp->virt, mp->phys);
325 kfree(mp);
326 }
327 list_del(&mbox->list);
328 mempool_free(mbox, phba->mbox_mem_pool);
329 }
James Smartda0436e2009-05-22 14:51:39 -0400330 /* Free the active mailbox command back to the mailbox memory pool */
331 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500332 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400333 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500334 if (psli->mbox_active) {
335 mbox = psli->mbox_active;
336 mp = (struct lpfc_dmabuf *) (mbox->context1);
337 if (mp) {
338 lpfc_mbuf_free(phba, mp->virt, mp->phys);
339 kfree(mp);
340 }
341 mempool_free(mbox, phba->mbox_mem_pool);
342 psli->mbox_active = NULL;
343 }
344
James Smartda0436e2009-05-22 14:51:39 -0400345 /* Free and destroy all the allocated memory pools */
346 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500347
James Smarte59058c2008-08-24 21:49:00 -0400348 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400349 kfree(psli->iocbq_lookup);
350 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400351
352 return;
dea31012005-04-17 16:05:31 -0500353}
354
James Smarte59058c2008-08-24 21:49:00 -0400355/**
James Smart3621a712009-04-06 18:47:14 -0400356 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400357 * @phba: HBA which owns the pool to allocate from
358 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
359 * @handle: used to return the DMA-mapped address of the mbuf
360 *
361 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
362 * Allocates from generic pci_pool_alloc function first and if that fails and
363 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
364 * HBA's pool.
365 *
366 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
367 * phba->hbalock.
368 *
369 * Returns:
370 * pointer to the allocated mbuf on success
371 * NULL on failure
372 **/
dea31012005-04-17 16:05:31 -0500373void *
374lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
375{
376 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500377 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500378 void *ret;
379
380 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
381
James Smart2e0fef82007-06-17 19:56:36 -0500382 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500383 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500384 pool->current_count--;
385 ret = pool->elements[pool->current_count].virt;
386 *handle = pool->elements[pool->current_count].phys;
387 }
James Smart2e0fef82007-06-17 19:56:36 -0500388 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500389 return ret;
390}
391
James Smarte59058c2008-08-24 21:49:00 -0400392/**
James Smart3621a712009-04-06 18:47:14 -0400393 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400394 * @phba: HBA which owns the pool to return to
395 * @virt: mbuf to free
396 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
397 *
398 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
399 * it is below its max_count, frees the mbuf otherwise.
400 *
401 * Notes: Must be called with phba->hbalock held to synchronize access to
402 * lpfc_mbuf_safety_pool.
403 *
404 * Returns: None
405 **/
dea31012005-04-17 16:05:31 -0500406void
James Smart2e0fef82007-06-17 19:56:36 -0500407__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500408{
409 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
410
411 if (pool->current_count < pool->max_count) {
412 pool->elements[pool->current_count].virt = virt;
413 pool->elements[pool->current_count].phys = dma;
414 pool->current_count++;
415 } else {
416 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
417 }
418 return;
419}
James Smart2e0fef82007-06-17 19:56:36 -0500420
James Smarte59058c2008-08-24 21:49:00 -0400421/**
James Smart3621a712009-04-06 18:47:14 -0400422 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400423 * @phba: HBA which owns the pool to return to
424 * @virt: mbuf to free
425 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
426 *
427 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
428 * it is below its max_count, frees the mbuf otherwise.
429 *
430 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
431 *
432 * Returns: None
433 **/
James Smart2e0fef82007-06-17 19:56:36 -0500434void
435lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
436{
437 unsigned long iflags;
438
439 spin_lock_irqsave(&phba->hbalock, iflags);
440 __lpfc_mbuf_free(phba, virt, dma);
441 spin_unlock_irqrestore(&phba->hbalock, iflags);
442 return;
443}
James Smarted957682007-06-17 19:56:37 -0500444
James Smarte59058c2008-08-24 21:49:00 -0400445/**
James Smartf358dd02017-02-12 13:52:34 -0800446 * lpfc_nvmet_buf_alloc - Allocate an nvmet_buf from the
447 * lpfc_sg_dma_buf_pool PCI pool
448 * @phba: HBA which owns the pool to allocate from
449 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
450 * @handle: used to return the DMA-mapped address of the nvmet_buf
451 *
452 * Description: Allocates a DMA-mapped buffer from the lpfc_sg_dma_buf_pool
453 * PCI pool. Allocates from generic pci_pool_alloc function.
454 *
455 * Returns:
456 * pointer to the allocated nvmet_buf on success
457 * NULL on failure
458 **/
459void *
460lpfc_nvmet_buf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
461{
462 void *ret;
463
464 ret = pci_pool_alloc(phba->lpfc_sg_dma_buf_pool, GFP_KERNEL, handle);
465 return ret;
466}
467
468/**
469 * lpfc_nvmet_buf_free - Free an nvmet_buf from the lpfc_sg_dma_buf_pool
470 * PCI pool
471 * @phba: HBA which owns the pool to return to
472 * @virt: nvmet_buf to free
473 * @dma: the DMA-mapped address of the lpfc_sg_dma_buf_pool to be freed
474 *
475 * Returns: None
476 **/
477void
478lpfc_nvmet_buf_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma)
479{
480 pci_pool_free(phba->lpfc_sg_dma_buf_pool, virt, dma);
481}
482
483/**
James Smart3621a712009-04-06 18:47:14 -0400484 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400485 * @phba: HBA to allocate HBQ buffer for
486 *
James Smartda0436e2009-05-22 14:51:39 -0400487 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400488 * pool along a non-DMA-mapped container for it.
489 *
490 * Notes: Not interrupt-safe. Must be called with no locks held.
491 *
492 * Returns:
493 * pointer to HBQ on success
494 * NULL on failure
495 **/
James Smart51ef4c22007-08-02 11:10:31 -0400496struct hbq_dmabuf *
497lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500498{
James Smart51ef4c22007-08-02 11:10:31 -0400499 struct hbq_dmabuf *hbqbp;
500
James Smart2e90f4b2011-12-13 13:22:37 -0500501 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smart51ef4c22007-08-02 11:10:31 -0400502 if (!hbqbp)
503 return NULL;
504
James Smart8568a4d2009-07-19 10:01:16 -0400505 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400506 &hbqbp->dbuf.phys);
507 if (!hbqbp->dbuf.virt) {
508 kfree(hbqbp);
509 return NULL;
510 }
James Smart895427b2017-02-12 13:52:30 -0800511 hbqbp->total_size = LPFC_BPL_SIZE;
James Smart51ef4c22007-08-02 11:10:31 -0400512 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500513}
514
James Smarte59058c2008-08-24 21:49:00 -0400515/**
James Smartda0436e2009-05-22 14:51:39 -0400516 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400517 * @phba: HBA buffer was allocated for
518 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
519 *
520 * Description: Frees both the container and the DMA-mapped buffer returned by
521 * lpfc_els_hbq_alloc.
522 *
523 * Notes: Can be called with or without locks held.
524 *
525 * Returns: None
526 **/
James Smarted957682007-06-17 19:56:37 -0500527void
James Smart51ef4c22007-08-02 11:10:31 -0400528lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500529{
James Smart8568a4d2009-07-19 10:01:16 -0400530 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400531 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500532 return;
533}
534
James Smarte59058c2008-08-24 21:49:00 -0400535/**
James Smartda0436e2009-05-22 14:51:39 -0400536 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
537 * @phba: HBA to allocate a receive buffer for
538 *
539 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
540 * pool along a non-DMA-mapped container for it.
541 *
542 * Notes: Not interrupt-safe. Must be called with no locks held.
543 *
544 * Returns:
545 * pointer to HBQ on success
546 * NULL on failure
547 **/
548struct hbq_dmabuf *
549lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
550{
551 struct hbq_dmabuf *dma_buf;
552
James Smart2e90f4b2011-12-13 13:22:37 -0500553 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -0400554 if (!dma_buf)
555 return NULL;
556
557 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
558 &dma_buf->hbuf.phys);
559 if (!dma_buf->hbuf.virt) {
560 kfree(dma_buf);
561 return NULL;
562 }
563 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
564 &dma_buf->dbuf.phys);
565 if (!dma_buf->dbuf.virt) {
566 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
567 dma_buf->hbuf.phys);
568 kfree(dma_buf);
569 return NULL;
570 }
James Smart895427b2017-02-12 13:52:30 -0800571 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -0400572 return dma_buf;
573}
574
575/**
576 * lpfc_sli4_rb_free - Frees a receive buffer
577 * @phba: HBA buffer was allocated for
578 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
579 *
580 * Description: Frees both the container and the DMA-mapped buffers returned by
581 * lpfc_sli4_rb_alloc.
582 *
583 * Notes: Can be called with or without locks held.
584 *
585 * Returns: None
586 **/
587void
588lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
589{
590 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
591 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
592 kfree(dmab);
James Smartda0436e2009-05-22 14:51:39 -0400593}
594
595/**
James Smartf358dd02017-02-12 13:52:34 -0800596 * lpfc_sli4_nvmet_alloc - Allocate an SLI4 Receive buffer
597 * @phba: HBA to allocate a receive buffer for
598 *
599 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
600 * pool along a non-DMA-mapped container for it.
601 *
602 * Notes: Not interrupt-safe. Must be called with no locks held.
603 *
604 * Returns:
605 * pointer to HBQ on success
606 * NULL on failure
607 **/
608struct rqb_dmabuf *
609lpfc_sli4_nvmet_alloc(struct lpfc_hba *phba)
610{
611 struct rqb_dmabuf *dma_buf;
612 struct lpfc_iocbq *nvmewqe;
613 union lpfc_wqe128 *wqe;
614
615 dma_buf = kzalloc(sizeof(struct rqb_dmabuf), GFP_KERNEL);
616 if (!dma_buf)
617 return NULL;
618
619 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
620 &dma_buf->hbuf.phys);
621 if (!dma_buf->hbuf.virt) {
622 kfree(dma_buf);
623 return NULL;
624 }
625 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
626 &dma_buf->dbuf.phys);
627 if (!dma_buf->dbuf.virt) {
628 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
629 dma_buf->hbuf.phys);
630 kfree(dma_buf);
631 return NULL;
632 }
633 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
634
635 dma_buf->context = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx),
636 GFP_KERNEL);
637 if (!dma_buf->context) {
638 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
639 dma_buf->dbuf.phys);
640 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
641 dma_buf->hbuf.phys);
642 kfree(dma_buf);
643 return NULL;
644 }
645
646 dma_buf->iocbq = lpfc_sli_get_iocbq(phba);
647 dma_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
648 if (!dma_buf->iocbq) {
649 kfree(dma_buf->context);
650 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
651 dma_buf->dbuf.phys);
652 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
653 dma_buf->hbuf.phys);
654 kfree(dma_buf);
655 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
656 "2621 Ran out of nvmet iocb/WQEs\n");
657 return NULL;
658 }
659 nvmewqe = dma_buf->iocbq;
660 wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
661 /* Initialize WQE */
662 memset(wqe, 0, sizeof(union lpfc_wqe));
663 /* Word 7 */
664 bf_set(wqe_ct, &wqe->generic.wqe_com, SLI4_CT_RPI);
665 bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
666 bf_set(wqe_pu, &wqe->generic.wqe_com, 1);
667 /* Word 10 */
668 bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
669 bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
670 bf_set(wqe_qosd, &wqe->generic.wqe_com, 0);
671
672 dma_buf->iocbq->context1 = NULL;
673 spin_lock(&phba->sli4_hba.sgl_list_lock);
674 dma_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, dma_buf->iocbq);
675 spin_unlock(&phba->sli4_hba.sgl_list_lock);
676 if (!dma_buf->sglq) {
677 lpfc_sli_release_iocbq(phba, dma_buf->iocbq);
678 kfree(dma_buf->context);
679 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
680 dma_buf->dbuf.phys);
681 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
682 dma_buf->hbuf.phys);
683 kfree(dma_buf);
684 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
685 "6132 Ran out of nvmet XRIs\n");
686 return NULL;
687 }
688 return dma_buf;
689}
690
691/**
692 * lpfc_sli4_nvmet_free - Frees a receive buffer
693 * @phba: HBA buffer was allocated for
694 * @dmab: DMA Buffer container returned by lpfc_sli4_rbq_alloc
695 *
696 * Description: Frees both the container and the DMA-mapped buffers returned by
697 * lpfc_sli4_nvmet_alloc.
698 *
699 * Notes: Can be called with or without locks held.
700 *
701 * Returns: None
702 **/
703void
704lpfc_sli4_nvmet_free(struct lpfc_hba *phba, struct rqb_dmabuf *dmab)
705{
706 unsigned long flags;
707
708 __lpfc_clear_active_sglq(phba, dmab->sglq->sli4_lxritag);
709 dmab->sglq->state = SGL_FREED;
710 dmab->sglq->ndlp = NULL;
711
712 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, flags);
713 list_add_tail(&dmab->sglq->list, &phba->sli4_hba.lpfc_nvmet_sgl_list);
714 spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, flags);
715
716 lpfc_sli_release_iocbq(phba, dmab->iocbq);
717 kfree(dmab->context);
718 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
719 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
720 kfree(dmab);
721}
722
723/**
James Smart3621a712009-04-06 18:47:14 -0400724 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400725 * @phba: HBA buffer is associated with
726 * @mp: Buffer to free
727 *
728 * Description: Frees the given DMA buffer in the appropriate way given if the
729 * HBA is running in SLI3 mode with HBQs enabled.
730 *
731 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
732 *
733 * Returns: None
734 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500735void
736lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
737{
738 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500739 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500740
James Smart7f5f3d02008-02-08 18:50:14 -0500741 if (!mp)
742 return;
743
James Smart92d7f7b2007-06-17 19:56:38 -0500744 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart895427b2017-02-12 13:52:30 -0800745 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500746 /* Check whether HBQ is still in use */
747 spin_lock_irqsave(&phba->hbalock, flags);
748 if (!phba->hbq_in_use) {
749 spin_unlock_irqrestore(&phba->hbalock, flags);
750 return;
751 }
James Smart3163f722008-02-08 18:50:25 -0500752 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500753 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400754 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
755 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500756 } else {
757 lpfc_sli_free_hbq(phba, hbq_entry);
758 }
James Smart3163f722008-02-08 18:50:25 -0500759 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500760 } else {
761 lpfc_mbuf_free(phba, mp->virt, mp->phys);
762 kfree(mp);
763 }
764 return;
765}
James Smartd613b6a2017-02-12 13:52:37 -0800766
767/**
768 * lpfc_rq_buf_free - Free a RQ DMA buffer
769 * @phba: HBA buffer is associated with
770 * @mp: Buffer to free
771 *
772 * Description: Frees the given DMA buffer in the appropriate way given by
773 * reposting it to its associated RQ so it can be reused.
774 *
775 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
776 *
777 * Returns: None
778 **/
779void
780lpfc_rq_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
781{
782 struct lpfc_rqb *rqbp;
783 struct lpfc_rqe hrqe;
784 struct lpfc_rqe drqe;
785 struct rqb_dmabuf *rqb_entry;
786 unsigned long flags;
787 int rc;
788
789 if (!mp)
790 return;
791
792 rqb_entry = container_of(mp, struct rqb_dmabuf, hbuf);
793 rqbp = rqb_entry->hrq->rqbp;
794
795 spin_lock_irqsave(&phba->hbalock, flags);
796 list_del(&rqb_entry->hbuf.list);
797 hrqe.address_lo = putPaddrLow(rqb_entry->hbuf.phys);
798 hrqe.address_hi = putPaddrHigh(rqb_entry->hbuf.phys);
799 drqe.address_lo = putPaddrLow(rqb_entry->dbuf.phys);
800 drqe.address_hi = putPaddrHigh(rqb_entry->dbuf.phys);
801 rc = lpfc_sli4_rq_put(rqb_entry->hrq, rqb_entry->drq, &hrqe, &drqe);
802 if (rc < 0) {
803 (rqbp->rqb_free_buffer)(phba, rqb_entry);
804 } else {
805 list_add_tail(&rqb_entry->hbuf.list, &rqbp->rqb_buffer_list);
806 rqbp->buffer_count++;
807 }
808
809 spin_unlock_irqrestore(&phba->hbalock, flags);
810}