blob: c65a1ec3d2e48f906e9bf269c14e9c36d9250a13 [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"
dea31012005-04-17 16:05:31 -050043#include "lpfc_crtn.h"
James Smartcff261f2013-12-17 20:29:47 -050044#include "lpfc_logmsg.h"
dea31012005-04-17 16:05:31 -050045
46#define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
47#define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
James Smart1ba981f2014-02-20 09:56:45 -050048#define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */
dea31012005-04-17 16:05:31 -050049
James Smartcff261f2013-12-17 20:29:47 -050050int
51lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
52 size_t bytes;
53 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
54
55 if (max_xri <= 0)
56 return -ENOMEM;
57 bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) *
58 sizeof(unsigned long);
59 phba->cfg_rrq_xri_bitmap_sz = bytes;
60 phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
61 bytes);
62 if (!phba->active_rrq_pool)
63 return -ENOMEM;
64 else
65 return 0;
66}
James Smart2e0fef82007-06-17 19:56:36 -050067
James Smarte59058c2008-08-24 21:49:00 -040068/**
James Smart3621a712009-04-06 18:47:14 -040069 * lpfc_mem_alloc - create and allocate all PCI and memory pools
James Smarte59058c2008-08-24 21:49:00 -040070 * @phba: HBA to allocate pools for
71 *
James Smart895427b2017-02-12 13:52:30 -080072 * Description: Creates and allocates PCI pools lpfc_sg_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -040073 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
James Smarte59058c2008-08-24 21:49:00 -040074 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
75 *
76 * Notes: Not interrupt-safe. Must be called with no locks held. If any
77 * allocation fails, frees all successfully allocated memory before returning.
78 *
79 * Returns:
80 * 0 on success
81 * -ENOMEM on failure (if any memory allocations fail)
82 **/
dea31012005-04-17 16:05:31 -050083int
James Smartda0436e2009-05-22 14:51:39 -040084lpfc_mem_alloc(struct lpfc_hba *phba, int align)
dea31012005-04-17 16:05:31 -050085{
86 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
87 int i;
88
James Smart96f70772013-04-17 20:16:15 -040089 if (phba->sli_rev == LPFC_SLI_REV4) {
90 /* Calculate alignment */
91 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
92 i = phba->cfg_sg_dma_buf_size;
93 else
94 i = SLI4_PAGE_SIZE;
95
James Smart895427b2017-02-12 13:52:30 -080096 phba->lpfc_sg_dma_buf_pool =
97 pci_pool_create("lpfc_sg_dma_buf_pool",
98 phba->pcidev,
99 phba->cfg_sg_dma_buf_size,
100 i, 0);
101 if (!phba->lpfc_sg_dma_buf_pool)
102 goto fail;
James Smart96f70772013-04-17 20:16:15 -0400103
James Smart895427b2017-02-12 13:52:30 -0800104 } else {
105 phba->lpfc_sg_dma_buf_pool =
106 pci_pool_create("lpfc_sg_dma_buf_pool",
107 phba->pcidev, phba->cfg_sg_dma_buf_size,
108 align, 0);
109
110 if (!phba->lpfc_sg_dma_buf_pool)
111 goto fail;
112 }
dea31012005-04-17 16:05:31 -0500113
114 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
James Smartda0436e2009-05-22 14:51:39 -0400115 LPFC_BPL_SIZE,
116 align, 0);
dea31012005-04-17 16:05:31 -0500117 if (!phba->lpfc_mbuf_pool)
118 goto fail_free_dma_buf_pool;
119
120 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
121 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100122 if (!pool->elements)
123 goto fail_free_lpfc_mbuf_pool;
124
dea31012005-04-17 16:05:31 -0500125 pool->max_count = 0;
126 pool->current_count = 0;
127 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
128 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
129 GFP_KERNEL, &pool->elements[i].phys);
130 if (!pool->elements[i].virt)
131 goto fail_free_mbuf_pool;
132 pool->max_count++;
133 pool->current_count++;
134 }
135
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800136 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
137 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -0500138 if (!phba->mbox_mem_pool)
139 goto fail_free_mbuf_pool;
140
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800141 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
142 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -0500143 if (!phba->nlp_mem_pool)
144 goto fail_free_mbox_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400145
146 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart19ca7602010-11-20 23:11:55 -0500147 phba->rrq_pool =
148 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
149 sizeof(struct lpfc_node_rrq));
150 if (!phba->rrq_pool)
151 goto fail_free_nlp_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400152 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400153 phba->pcidev,
154 LPFC_HDR_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400155 if (!phba->lpfc_hrb_pool)
James Smart19ca7602010-11-20 23:11:55 -0500156 goto fail_free_rrq_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400157
158 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400159 phba->pcidev,
160 LPFC_DATA_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400161 if (!phba->lpfc_drb_pool)
162 goto fail_free_hrb_pool;
163 phba->lpfc_hbq_pool = NULL;
164 } else {
165 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
166 phba->pcidev, LPFC_BPL_SIZE, align, 0);
167 if (!phba->lpfc_hbq_pool)
168 goto fail_free_nlp_mem_pool;
169 phba->lpfc_hrb_pool = NULL;
170 phba->lpfc_drb_pool = NULL;
171 }
James Smart92d7f7b2007-06-17 19:56:38 -0500172
James Smart1ba981f2014-02-20 09:56:45 -0500173 if (phba->cfg_EnableXLane) {
174 phba->device_data_mem_pool = mempool_create_kmalloc_pool(
175 LPFC_DEVICE_DATA_POOL_SIZE,
176 sizeof(struct lpfc_device_data));
177 if (!phba->device_data_mem_pool)
James Smart895427b2017-02-12 13:52:30 -0800178 goto fail_free_drb_pool;
James Smart1ba981f2014-02-20 09:56:45 -0500179 } else {
180 phba->device_data_mem_pool = NULL;
181 }
182
dea31012005-04-17 16:05:31 -0500183 return 0;
James Smart895427b2017-02-12 13:52:30 -0800184fail_free_drb_pool:
185 pci_pool_destroy(phba->lpfc_drb_pool);
186 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400187 fail_free_hrb_pool:
James Smartda0436e2009-05-22 14:51:39 -0400188 pci_pool_destroy(phba->lpfc_hrb_pool);
189 phba->lpfc_hrb_pool = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500190 fail_free_rrq_mem_pool:
191 mempool_destroy(phba->rrq_pool);
192 phba->rrq_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500193 fail_free_nlp_mem_pool:
194 mempool_destroy(phba->nlp_mem_pool);
195 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500196 fail_free_mbox_pool:
197 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500198 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500199 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100200 while (i--)
dea31012005-04-17 16:05:31 -0500201 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
202 pool->elements[i].phys);
203 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100204 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500205 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500206 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500207 fail_free_dma_buf_pool:
James Smart895427b2017-02-12 13:52:30 -0800208 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
209 phba->lpfc_sg_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500210 fail:
211 return -ENOMEM;
212}
213
James Smarte59058c2008-08-24 21:49:00 -0400214/**
James Smartda0436e2009-05-22 14:51:39 -0400215 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400216 * @phba: HBA to free memory for
217 *
James Smartda0436e2009-05-22 14:51:39 -0400218 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
219 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400220 *
221 * Returns: None
222 **/
dea31012005-04-17 16:05:31 -0500223void
James Smartda0436e2009-05-22 14:51:39 -0400224lpfc_mem_free(struct lpfc_hba *phba)
225{
226 int i;
227 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart1ba981f2014-02-20 09:56:45 -0500228 struct lpfc_device_data *device_data;
James Smartda0436e2009-05-22 14:51:39 -0400229
James Smartda0436e2009-05-22 14:51:39 -0400230 /* Free HBQ pools */
231 lpfc_sli_hbqbuf_free_all(phba);
James Smart8568a4d2009-07-19 10:01:16 -0400232 if (phba->lpfc_drb_pool)
233 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400234 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400235 if (phba->lpfc_hrb_pool)
236 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400237 phba->lpfc_hrb_pool = NULL;
James Smart895427b2017-02-12 13:52:30 -0800238 if (phba->txrdy_payload_pool)
239 pci_pool_destroy(phba->txrdy_payload_pool);
240 phba->txrdy_payload_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400241
James Smart8568a4d2009-07-19 10:01:16 -0400242 if (phba->lpfc_hbq_pool)
243 pci_pool_destroy(phba->lpfc_hbq_pool);
244 phba->lpfc_hbq_pool = NULL;
Martin K. Petersend65c8ff2016-05-09 21:39:43 -0400245
246 if (phba->rrq_pool)
247 mempool_destroy(phba->rrq_pool);
James Smart21a688c2012-08-03 12:34:15 -0400248 phba->rrq_pool = NULL;
249
James Smartda0436e2009-05-22 14:51:39 -0400250 /* Free NLP memory pool */
251 mempool_destroy(phba->nlp_mem_pool);
252 phba->nlp_mem_pool = NULL;
Martin K. Petersend65c8ff2016-05-09 21:39:43 -0400253 if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
James Smartcff261f2013-12-17 20:29:47 -0500254 mempool_destroy(phba->active_rrq_pool);
255 phba->active_rrq_pool = NULL;
256 }
James Smartda0436e2009-05-22 14:51:39 -0400257
258 /* Free mbox memory pool */
259 mempool_destroy(phba->mbox_mem_pool);
260 phba->mbox_mem_pool = NULL;
261
262 /* Free MBUF memory pool */
263 for (i = 0; i < pool->current_count; i++)
264 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
265 pool->elements[i].phys);
266 kfree(pool->elements);
267
268 pci_pool_destroy(phba->lpfc_mbuf_pool);
269 phba->lpfc_mbuf_pool = NULL;
270
271 /* Free DMA buffer memory pool */
James Smart895427b2017-02-12 13:52:30 -0800272 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
273 phba->lpfc_sg_dma_buf_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400274
James Smart1ba981f2014-02-20 09:56:45 -0500275 /* Free Device Data memory pool */
276 if (phba->device_data_mem_pool) {
277 /* Ensure all objects have been returned to the pool */
278 while (!list_empty(&phba->luns)) {
279 device_data = list_first_entry(&phba->luns,
280 struct lpfc_device_data,
281 listentry);
282 list_del(&device_data->listentry);
283 mempool_free(device_data, phba->device_data_mem_pool);
284 }
285 mempool_destroy(phba->device_data_mem_pool);
286 }
287 phba->device_data_mem_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400288 return;
289}
290
291/**
292 * lpfc_mem_free_all - Frees all PCI and driver memory
293 * @phba: HBA to free memory for
294 *
295 * Description: Free memory from PCI and driver memory pools and also those
James Smart895427b2017-02-12 13:52:30 -0800296 * used : lpfc_sg_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
James Smartda0436e2009-05-22 14:51:39 -0400297 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
298 * the VPI bitmask.
299 *
300 * Returns: None
301 **/
302void
303lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500304{
305 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500306 LPFC_MBOXQ_t *mbox, *next_mbox;
307 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500308
James Smartda0436e2009-05-22 14:51:39 -0400309 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500310 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
311 mp = (struct lpfc_dmabuf *) (mbox->context1);
312 if (mp) {
313 lpfc_mbuf_free(phba, mp->virt, mp->phys);
314 kfree(mp);
315 }
316 list_del(&mbox->list);
317 mempool_free(mbox, phba->mbox_mem_pool);
318 }
James Smartda0436e2009-05-22 14:51:39 -0400319 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500320 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
321 mp = (struct lpfc_dmabuf *) (mbox->context1);
322 if (mp) {
323 lpfc_mbuf_free(phba, mp->virt, mp->phys);
324 kfree(mp);
325 }
326 list_del(&mbox->list);
327 mempool_free(mbox, phba->mbox_mem_pool);
328 }
James Smartda0436e2009-05-22 14:51:39 -0400329 /* Free the active mailbox command back to the mailbox memory pool */
330 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500331 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400332 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500333 if (psli->mbox_active) {
334 mbox = psli->mbox_active;
335 mp = (struct lpfc_dmabuf *) (mbox->context1);
336 if (mp) {
337 lpfc_mbuf_free(phba, mp->virt, mp->phys);
338 kfree(mp);
339 }
340 mempool_free(mbox, phba->mbox_mem_pool);
341 psli->mbox_active = NULL;
342 }
343
James Smartda0436e2009-05-22 14:51:39 -0400344 /* Free and destroy all the allocated memory pools */
345 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500346
James Smarte59058c2008-08-24 21:49:00 -0400347 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400348 kfree(psli->iocbq_lookup);
349 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400350
351 return;
dea31012005-04-17 16:05:31 -0500352}
353
James Smarte59058c2008-08-24 21:49:00 -0400354/**
James Smart3621a712009-04-06 18:47:14 -0400355 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400356 * @phba: HBA which owns the pool to allocate from
357 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
358 * @handle: used to return the DMA-mapped address of the mbuf
359 *
360 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
361 * Allocates from generic pci_pool_alloc function first and if that fails and
362 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
363 * HBA's pool.
364 *
365 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
366 * phba->hbalock.
367 *
368 * Returns:
369 * pointer to the allocated mbuf on success
370 * NULL on failure
371 **/
dea31012005-04-17 16:05:31 -0500372void *
373lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
374{
375 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500376 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500377 void *ret;
378
379 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
380
James Smart2e0fef82007-06-17 19:56:36 -0500381 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500382 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500383 pool->current_count--;
384 ret = pool->elements[pool->current_count].virt;
385 *handle = pool->elements[pool->current_count].phys;
386 }
James Smart2e0fef82007-06-17 19:56:36 -0500387 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500388 return ret;
389}
390
James Smarte59058c2008-08-24 21:49:00 -0400391/**
James Smart3621a712009-04-06 18:47:14 -0400392 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400393 * @phba: HBA which owns the pool to return to
394 * @virt: mbuf to free
395 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
396 *
397 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
398 * it is below its max_count, frees the mbuf otherwise.
399 *
400 * Notes: Must be called with phba->hbalock held to synchronize access to
401 * lpfc_mbuf_safety_pool.
402 *
403 * Returns: None
404 **/
dea31012005-04-17 16:05:31 -0500405void
James Smart2e0fef82007-06-17 19:56:36 -0500406__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500407{
408 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
409
410 if (pool->current_count < pool->max_count) {
411 pool->elements[pool->current_count].virt = virt;
412 pool->elements[pool->current_count].phys = dma;
413 pool->current_count++;
414 } else {
415 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
416 }
417 return;
418}
James Smart2e0fef82007-06-17 19:56:36 -0500419
James Smarte59058c2008-08-24 21:49:00 -0400420/**
James Smart3621a712009-04-06 18:47:14 -0400421 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400422 * @phba: HBA which owns the pool to return to
423 * @virt: mbuf to free
424 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
425 *
426 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
427 * it is below its max_count, frees the mbuf otherwise.
428 *
429 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
430 *
431 * Returns: None
432 **/
James Smart2e0fef82007-06-17 19:56:36 -0500433void
434lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
435{
436 unsigned long iflags;
437
438 spin_lock_irqsave(&phba->hbalock, iflags);
439 __lpfc_mbuf_free(phba, virt, dma);
440 spin_unlock_irqrestore(&phba->hbalock, iflags);
441 return;
442}
James Smarted957682007-06-17 19:56:37 -0500443
James Smarte59058c2008-08-24 21:49:00 -0400444/**
James Smart3621a712009-04-06 18:47:14 -0400445 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400446 * @phba: HBA to allocate HBQ buffer for
447 *
James Smartda0436e2009-05-22 14:51:39 -0400448 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400449 * pool along a non-DMA-mapped container for it.
450 *
451 * Notes: Not interrupt-safe. Must be called with no locks held.
452 *
453 * Returns:
454 * pointer to HBQ on success
455 * NULL on failure
456 **/
James Smart51ef4c22007-08-02 11:10:31 -0400457struct hbq_dmabuf *
458lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500459{
James Smart51ef4c22007-08-02 11:10:31 -0400460 struct hbq_dmabuf *hbqbp;
461
James Smart2e90f4b2011-12-13 13:22:37 -0500462 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smart51ef4c22007-08-02 11:10:31 -0400463 if (!hbqbp)
464 return NULL;
465
James Smart8568a4d2009-07-19 10:01:16 -0400466 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400467 &hbqbp->dbuf.phys);
468 if (!hbqbp->dbuf.virt) {
469 kfree(hbqbp);
470 return NULL;
471 }
James Smart895427b2017-02-12 13:52:30 -0800472 hbqbp->total_size = LPFC_BPL_SIZE;
James Smart51ef4c22007-08-02 11:10:31 -0400473 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500474}
475
James Smarte59058c2008-08-24 21:49:00 -0400476/**
James Smartda0436e2009-05-22 14:51:39 -0400477 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400478 * @phba: HBA buffer was allocated for
479 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
480 *
481 * Description: Frees both the container and the DMA-mapped buffer returned by
482 * lpfc_els_hbq_alloc.
483 *
484 * Notes: Can be called with or without locks held.
485 *
486 * Returns: None
487 **/
James Smarted957682007-06-17 19:56:37 -0500488void
James Smart51ef4c22007-08-02 11:10:31 -0400489lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500490{
James Smart8568a4d2009-07-19 10:01:16 -0400491 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400492 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500493 return;
494}
495
James Smarte59058c2008-08-24 21:49:00 -0400496/**
James Smartda0436e2009-05-22 14:51:39 -0400497 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
498 * @phba: HBA to allocate a receive buffer for
499 *
500 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
501 * pool along a non-DMA-mapped container for it.
502 *
503 * Notes: Not interrupt-safe. Must be called with no locks held.
504 *
505 * Returns:
506 * pointer to HBQ on success
507 * NULL on failure
508 **/
509struct hbq_dmabuf *
510lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
511{
512 struct hbq_dmabuf *dma_buf;
513
James Smart2e90f4b2011-12-13 13:22:37 -0500514 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -0400515 if (!dma_buf)
516 return NULL;
517
518 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
519 &dma_buf->hbuf.phys);
520 if (!dma_buf->hbuf.virt) {
521 kfree(dma_buf);
522 return NULL;
523 }
524 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
525 &dma_buf->dbuf.phys);
526 if (!dma_buf->dbuf.virt) {
527 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
528 dma_buf->hbuf.phys);
529 kfree(dma_buf);
530 return NULL;
531 }
James Smart895427b2017-02-12 13:52:30 -0800532 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -0400533 return dma_buf;
534}
535
536/**
537 * lpfc_sli4_rb_free - Frees a receive buffer
538 * @phba: HBA buffer was allocated for
539 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
540 *
541 * Description: Frees both the container and the DMA-mapped buffers returned by
542 * lpfc_sli4_rb_alloc.
543 *
544 * Notes: Can be called with or without locks held.
545 *
546 * Returns: None
547 **/
548void
549lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
550{
551 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
552 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
553 kfree(dmab);
James Smartda0436e2009-05-22 14:51:39 -0400554}
555
556/**
James Smart3621a712009-04-06 18:47:14 -0400557 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400558 * @phba: HBA buffer is associated with
559 * @mp: Buffer to free
560 *
561 * Description: Frees the given DMA buffer in the appropriate way given if the
562 * HBA is running in SLI3 mode with HBQs enabled.
563 *
564 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
565 *
566 * Returns: None
567 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500568void
569lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
570{
571 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500572 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500573
James Smart7f5f3d02008-02-08 18:50:14 -0500574 if (!mp)
575 return;
576
James Smart92d7f7b2007-06-17 19:56:38 -0500577 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart895427b2017-02-12 13:52:30 -0800578 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500579 /* Check whether HBQ is still in use */
580 spin_lock_irqsave(&phba->hbalock, flags);
581 if (!phba->hbq_in_use) {
582 spin_unlock_irqrestore(&phba->hbalock, flags);
583 return;
584 }
James Smart3163f722008-02-08 18:50:25 -0500585 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500586 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400587 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
588 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500589 } else {
590 lpfc_sli_free_hbq(phba, hbq_entry);
591 }
James Smart3163f722008-02-08 18:50:25 -0500592 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500593 } else {
594 lpfc_mbuf_free(phba, mp->virt, mp->phys);
595 kfree(mp);
596 }
597 return;
598}