blob: 4fb3581d4614c55cc50b9d5c711c18805c7cb050 [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.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040027#include <scsi/scsi_device.h>
28#include <scsi/scsi_transport_fc.h>
29
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040030#include <scsi/scsi.h>
31
James Smartda0436e2009-05-22 14:51:39 -040032#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050033#include "lpfc_hw.h"
34#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040035#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040036#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_disc.h"
38#include "lpfc_scsi.h"
39#include "lpfc.h"
40#include "lpfc_crtn.h"
James Smartcff261f2013-12-17 20:29:47 -050041#include "lpfc_logmsg.h"
dea31012005-04-17 16:05:31 -050042
43#define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
44#define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
James Smart1ba981f2014-02-20 09:56:45 -050045#define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */
dea31012005-04-17 16:05:31 -050046
James Smartcff261f2013-12-17 20:29:47 -050047int
48lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
49 size_t bytes;
50 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
51
52 if (max_xri <= 0)
53 return -ENOMEM;
54 bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) *
55 sizeof(unsigned long);
56 phba->cfg_rrq_xri_bitmap_sz = bytes;
57 phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
58 bytes);
59 if (!phba->active_rrq_pool)
60 return -ENOMEM;
61 else
62 return 0;
63}
James Smart2e0fef82007-06-17 19:56:36 -050064
James Smarte59058c2008-08-24 21:49:00 -040065/**
James Smart3621a712009-04-06 18:47:14 -040066 * lpfc_mem_alloc - create and allocate all PCI and memory pools
James Smarte59058c2008-08-24 21:49:00 -040067 * @phba: HBA to allocate pools for
68 *
69 * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -040070 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
James Smarte59058c2008-08-24 21:49:00 -040071 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
72 *
73 * Notes: Not interrupt-safe. Must be called with no locks held. If any
74 * allocation fails, frees all successfully allocated memory before returning.
75 *
76 * Returns:
77 * 0 on success
78 * -ENOMEM on failure (if any memory allocations fail)
79 **/
dea31012005-04-17 16:05:31 -050080int
James Smartda0436e2009-05-22 14:51:39 -040081lpfc_mem_alloc(struct lpfc_hba *phba, int align)
dea31012005-04-17 16:05:31 -050082{
83 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
84 int i;
85
James Smart96f70772013-04-17 20:16:15 -040086 if (phba->sli_rev == LPFC_SLI_REV4) {
87 /* Calculate alignment */
88 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
89 i = phba->cfg_sg_dma_buf_size;
90 else
91 i = SLI4_PAGE_SIZE;
92
James Smartda0436e2009-05-22 14:51:39 -040093 phba->lpfc_scsi_dma_buf_pool =
94 pci_pool_create("lpfc_scsi_dma_buf_pool",
95 phba->pcidev,
96 phba->cfg_sg_dma_buf_size,
James Smart96f70772013-04-17 20:16:15 -040097 i,
James Smartda0436e2009-05-22 14:51:39 -040098 0);
James Smart96f70772013-04-17 20:16:15 -040099 } else {
James Smartda0436e2009-05-22 14:51:39 -0400100 phba->lpfc_scsi_dma_buf_pool =
101 pci_pool_create("lpfc_scsi_dma_buf_pool",
102 phba->pcidev, phba->cfg_sg_dma_buf_size,
103 align, 0);
James Smart96f70772013-04-17 20:16:15 -0400104 }
105
dea31012005-04-17 16:05:31 -0500106 if (!phba->lpfc_scsi_dma_buf_pool)
107 goto fail;
108
109 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
James Smartda0436e2009-05-22 14:51:39 -0400110 LPFC_BPL_SIZE,
111 align, 0);
dea31012005-04-17 16:05:31 -0500112 if (!phba->lpfc_mbuf_pool)
113 goto fail_free_dma_buf_pool;
114
115 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
116 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100117 if (!pool->elements)
118 goto fail_free_lpfc_mbuf_pool;
119
dea31012005-04-17 16:05:31 -0500120 pool->max_count = 0;
121 pool->current_count = 0;
122 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
123 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
124 GFP_KERNEL, &pool->elements[i].phys);
125 if (!pool->elements[i].virt)
126 goto fail_free_mbuf_pool;
127 pool->max_count++;
128 pool->current_count++;
129 }
130
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800131 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
132 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -0500133 if (!phba->mbox_mem_pool)
134 goto fail_free_mbuf_pool;
135
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800136 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
137 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -0500138 if (!phba->nlp_mem_pool)
139 goto fail_free_mbox_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400140
141 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart19ca7602010-11-20 23:11:55 -0500142 phba->rrq_pool =
143 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
144 sizeof(struct lpfc_node_rrq));
145 if (!phba->rrq_pool)
146 goto fail_free_nlp_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400147 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400148 phba->pcidev,
149 LPFC_HDR_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400150 if (!phba->lpfc_hrb_pool)
James Smart19ca7602010-11-20 23:11:55 -0500151 goto fail_free_rrq_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400152
153 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400154 phba->pcidev,
155 LPFC_DATA_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400156 if (!phba->lpfc_drb_pool)
157 goto fail_free_hrb_pool;
158 phba->lpfc_hbq_pool = NULL;
159 } else {
160 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
161 phba->pcidev, LPFC_BPL_SIZE, align, 0);
162 if (!phba->lpfc_hbq_pool)
163 goto fail_free_nlp_mem_pool;
164 phba->lpfc_hrb_pool = NULL;
165 phba->lpfc_drb_pool = NULL;
166 }
James Smart92d7f7b2007-06-17 19:56:38 -0500167
James Smart1ba981f2014-02-20 09:56:45 -0500168 if (phba->cfg_EnableXLane) {
169 phba->device_data_mem_pool = mempool_create_kmalloc_pool(
170 LPFC_DEVICE_DATA_POOL_SIZE,
171 sizeof(struct lpfc_device_data));
172 if (!phba->device_data_mem_pool)
173 goto fail_free_hrb_pool;
174 } else {
175 phba->device_data_mem_pool = NULL;
176 }
177
dea31012005-04-17 16:05:31 -0500178 return 0;
James Smart8568a4d2009-07-19 10:01:16 -0400179 fail_free_hrb_pool:
James Smartda0436e2009-05-22 14:51:39 -0400180 pci_pool_destroy(phba->lpfc_hrb_pool);
181 phba->lpfc_hrb_pool = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500182 fail_free_rrq_mem_pool:
183 mempool_destroy(phba->rrq_pool);
184 phba->rrq_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500185 fail_free_nlp_mem_pool:
186 mempool_destroy(phba->nlp_mem_pool);
187 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500188 fail_free_mbox_pool:
189 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500190 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500191 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100192 while (i--)
dea31012005-04-17 16:05:31 -0500193 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
194 pool->elements[i].phys);
195 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100196 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500197 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500198 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500199 fail_free_dma_buf_pool:
200 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500201 phba->lpfc_scsi_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500202 fail:
203 return -ENOMEM;
204}
205
James Smarte59058c2008-08-24 21:49:00 -0400206/**
James Smartda0436e2009-05-22 14:51:39 -0400207 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400208 * @phba: HBA to free memory for
209 *
James Smartda0436e2009-05-22 14:51:39 -0400210 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
211 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400212 *
213 * Returns: None
214 **/
dea31012005-04-17 16:05:31 -0500215void
James Smartda0436e2009-05-22 14:51:39 -0400216lpfc_mem_free(struct lpfc_hba *phba)
217{
218 int i;
219 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart1ba981f2014-02-20 09:56:45 -0500220 struct lpfc_device_data *device_data;
James Smartda0436e2009-05-22 14:51:39 -0400221
James Smartda0436e2009-05-22 14:51:39 -0400222 /* Free HBQ pools */
223 lpfc_sli_hbqbuf_free_all(phba);
James Smart8568a4d2009-07-19 10:01:16 -0400224 if (phba->lpfc_drb_pool)
225 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400226 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400227 if (phba->lpfc_hrb_pool)
228 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400229 phba->lpfc_hrb_pool = NULL;
230
James Smart8568a4d2009-07-19 10:01:16 -0400231 if (phba->lpfc_hbq_pool)
232 pci_pool_destroy(phba->lpfc_hbq_pool);
233 phba->lpfc_hbq_pool = NULL;
Markus Elfring9be32182015-12-16 18:12:06 -0500234 mempool_destroy(phba->rrq_pool);
James Smart21a688c2012-08-03 12:34:15 -0400235 phba->rrq_pool = NULL;
236
James Smartda0436e2009-05-22 14:51:39 -0400237 /* Free NLP memory pool */
238 mempool_destroy(phba->nlp_mem_pool);
239 phba->nlp_mem_pool = NULL;
Markus Elfring9be32182015-12-16 18:12:06 -0500240 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartcff261f2013-12-17 20:29:47 -0500241 mempool_destroy(phba->active_rrq_pool);
242 phba->active_rrq_pool = NULL;
243 }
James Smartda0436e2009-05-22 14:51:39 -0400244
245 /* Free mbox memory pool */
246 mempool_destroy(phba->mbox_mem_pool);
247 phba->mbox_mem_pool = NULL;
248
249 /* Free MBUF memory pool */
250 for (i = 0; i < pool->current_count; i++)
251 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
252 pool->elements[i].phys);
253 kfree(pool->elements);
254
255 pci_pool_destroy(phba->lpfc_mbuf_pool);
256 phba->lpfc_mbuf_pool = NULL;
257
258 /* Free DMA buffer memory pool */
259 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
260 phba->lpfc_scsi_dma_buf_pool = NULL;
261
James Smart1ba981f2014-02-20 09:56:45 -0500262 /* Free Device Data memory pool */
263 if (phba->device_data_mem_pool) {
264 /* Ensure all objects have been returned to the pool */
265 while (!list_empty(&phba->luns)) {
266 device_data = list_first_entry(&phba->luns,
267 struct lpfc_device_data,
268 listentry);
269 list_del(&device_data->listentry);
270 mempool_free(device_data, phba->device_data_mem_pool);
271 }
272 mempool_destroy(phba->device_data_mem_pool);
273 }
274 phba->device_data_mem_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400275 return;
276}
277
278/**
279 * lpfc_mem_free_all - Frees all PCI and driver memory
280 * @phba: HBA to free memory for
281 *
282 * Description: Free memory from PCI and driver memory pools and also those
283 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
284 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
285 * the VPI bitmask.
286 *
287 * Returns: None
288 **/
289void
290lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500291{
292 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500293 LPFC_MBOXQ_t *mbox, *next_mbox;
294 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500295
James Smartda0436e2009-05-22 14:51:39 -0400296 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500297 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
298 mp = (struct lpfc_dmabuf *) (mbox->context1);
299 if (mp) {
300 lpfc_mbuf_free(phba, mp->virt, mp->phys);
301 kfree(mp);
302 }
303 list_del(&mbox->list);
304 mempool_free(mbox, phba->mbox_mem_pool);
305 }
James Smartda0436e2009-05-22 14:51:39 -0400306 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500307 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
308 mp = (struct lpfc_dmabuf *) (mbox->context1);
309 if (mp) {
310 lpfc_mbuf_free(phba, mp->virt, mp->phys);
311 kfree(mp);
312 }
313 list_del(&mbox->list);
314 mempool_free(mbox, phba->mbox_mem_pool);
315 }
James Smartda0436e2009-05-22 14:51:39 -0400316 /* Free the active mailbox command back to the mailbox memory pool */
317 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500318 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400319 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500320 if (psli->mbox_active) {
321 mbox = psli->mbox_active;
322 mp = (struct lpfc_dmabuf *) (mbox->context1);
323 if (mp) {
324 lpfc_mbuf_free(phba, mp->virt, mp->phys);
325 kfree(mp);
326 }
327 mempool_free(mbox, phba->mbox_mem_pool);
328 psli->mbox_active = NULL;
329 }
330
James Smartda0436e2009-05-22 14:51:39 -0400331 /* Free and destroy all the allocated memory pools */
332 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500333
James Smarte59058c2008-08-24 21:49:00 -0400334 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400335 kfree(psli->iocbq_lookup);
336 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400337
338 return;
dea31012005-04-17 16:05:31 -0500339}
340
James Smarte59058c2008-08-24 21:49:00 -0400341/**
James Smart3621a712009-04-06 18:47:14 -0400342 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400343 * @phba: HBA which owns the pool to allocate from
344 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
345 * @handle: used to return the DMA-mapped address of the mbuf
346 *
347 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
348 * Allocates from generic pci_pool_alloc function first and if that fails and
349 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
350 * HBA's pool.
351 *
352 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
353 * phba->hbalock.
354 *
355 * Returns:
356 * pointer to the allocated mbuf on success
357 * NULL on failure
358 **/
dea31012005-04-17 16:05:31 -0500359void *
360lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
361{
362 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500363 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500364 void *ret;
365
366 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
367
James Smart2e0fef82007-06-17 19:56:36 -0500368 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500369 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500370 pool->current_count--;
371 ret = pool->elements[pool->current_count].virt;
372 *handle = pool->elements[pool->current_count].phys;
373 }
James Smart2e0fef82007-06-17 19:56:36 -0500374 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500375 return ret;
376}
377
James Smarte59058c2008-08-24 21:49:00 -0400378/**
James Smart3621a712009-04-06 18:47:14 -0400379 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400380 * @phba: HBA which owns the pool to return to
381 * @virt: mbuf to free
382 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
383 *
384 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
385 * it is below its max_count, frees the mbuf otherwise.
386 *
387 * Notes: Must be called with phba->hbalock held to synchronize access to
388 * lpfc_mbuf_safety_pool.
389 *
390 * Returns: None
391 **/
dea31012005-04-17 16:05:31 -0500392void
James Smart2e0fef82007-06-17 19:56:36 -0500393__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500394{
395 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
396
397 if (pool->current_count < pool->max_count) {
398 pool->elements[pool->current_count].virt = virt;
399 pool->elements[pool->current_count].phys = dma;
400 pool->current_count++;
401 } else {
402 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
403 }
404 return;
405}
James Smart2e0fef82007-06-17 19:56:36 -0500406
James Smarte59058c2008-08-24 21:49:00 -0400407/**
James Smart3621a712009-04-06 18:47:14 -0400408 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400409 * @phba: HBA which owns the pool to return to
410 * @virt: mbuf to free
411 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
412 *
413 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
414 * it is below its max_count, frees the mbuf otherwise.
415 *
416 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
417 *
418 * Returns: None
419 **/
James Smart2e0fef82007-06-17 19:56:36 -0500420void
421lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
422{
423 unsigned long iflags;
424
425 spin_lock_irqsave(&phba->hbalock, iflags);
426 __lpfc_mbuf_free(phba, virt, dma);
427 spin_unlock_irqrestore(&phba->hbalock, iflags);
428 return;
429}
James Smarted957682007-06-17 19:56:37 -0500430
James Smarte59058c2008-08-24 21:49:00 -0400431/**
James Smart3621a712009-04-06 18:47:14 -0400432 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400433 * @phba: HBA to allocate HBQ buffer for
434 *
James Smartda0436e2009-05-22 14:51:39 -0400435 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400436 * pool along a non-DMA-mapped container for it.
437 *
438 * Notes: Not interrupt-safe. Must be called with no locks held.
439 *
440 * Returns:
441 * pointer to HBQ on success
442 * NULL on failure
443 **/
James Smart51ef4c22007-08-02 11:10:31 -0400444struct hbq_dmabuf *
445lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500446{
James Smart51ef4c22007-08-02 11:10:31 -0400447 struct hbq_dmabuf *hbqbp;
448
James Smart2e90f4b2011-12-13 13:22:37 -0500449 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smart51ef4c22007-08-02 11:10:31 -0400450 if (!hbqbp)
451 return NULL;
452
James Smart8568a4d2009-07-19 10:01:16 -0400453 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400454 &hbqbp->dbuf.phys);
455 if (!hbqbp->dbuf.virt) {
456 kfree(hbqbp);
457 return NULL;
458 }
459 hbqbp->size = LPFC_BPL_SIZE;
460 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500461}
462
James Smarte59058c2008-08-24 21:49:00 -0400463/**
James Smartda0436e2009-05-22 14:51:39 -0400464 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400465 * @phba: HBA buffer was allocated for
466 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
467 *
468 * Description: Frees both the container and the DMA-mapped buffer returned by
469 * lpfc_els_hbq_alloc.
470 *
471 * Notes: Can be called with or without locks held.
472 *
473 * Returns: None
474 **/
James Smarted957682007-06-17 19:56:37 -0500475void
James Smart51ef4c22007-08-02 11:10:31 -0400476lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500477{
James Smart8568a4d2009-07-19 10:01:16 -0400478 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400479 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500480 return;
481}
482
James Smarte59058c2008-08-24 21:49:00 -0400483/**
James Smartda0436e2009-05-22 14:51:39 -0400484 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
485 * @phba: HBA to allocate a receive buffer for
486 *
487 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
488 * 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 **/
496struct hbq_dmabuf *
497lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
498{
499 struct hbq_dmabuf *dma_buf;
500
James Smart2e90f4b2011-12-13 13:22:37 -0500501 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -0400502 if (!dma_buf)
503 return NULL;
504
505 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
506 &dma_buf->hbuf.phys);
507 if (!dma_buf->hbuf.virt) {
508 kfree(dma_buf);
509 return NULL;
510 }
511 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
512 &dma_buf->dbuf.phys);
513 if (!dma_buf->dbuf.virt) {
514 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
515 dma_buf->hbuf.phys);
516 kfree(dma_buf);
517 return NULL;
518 }
519 dma_buf->size = LPFC_BPL_SIZE;
520 return dma_buf;
521}
522
523/**
524 * lpfc_sli4_rb_free - Frees a receive buffer
525 * @phba: HBA buffer was allocated for
526 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
527 *
528 * Description: Frees both the container and the DMA-mapped buffers returned by
529 * lpfc_sli4_rb_alloc.
530 *
531 * Notes: Can be called with or without locks held.
532 *
533 * Returns: None
534 **/
535void
536lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
537{
538 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
539 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
540 kfree(dmab);
541 return;
542}
543
544/**
James Smart3621a712009-04-06 18:47:14 -0400545 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400546 * @phba: HBA buffer is associated with
547 * @mp: Buffer to free
548 *
549 * Description: Frees the given DMA buffer in the appropriate way given if the
550 * HBA is running in SLI3 mode with HBQs enabled.
551 *
552 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
553 *
554 * Returns: None
555 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500556void
557lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
558{
559 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500560 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500561
James Smart7f5f3d02008-02-08 18:50:14 -0500562 if (!mp)
563 return;
564
James Smart92d7f7b2007-06-17 19:56:38 -0500565 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart3163f722008-02-08 18:50:25 -0500566 /* Check whether HBQ is still in use */
567 spin_lock_irqsave(&phba->hbalock, flags);
568 if (!phba->hbq_in_use) {
569 spin_unlock_irqrestore(&phba->hbalock, flags);
570 return;
571 }
James Smart92d7f7b2007-06-17 19:56:38 -0500572 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500573 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500574 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400575 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
576 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500577 } else {
578 lpfc_sli_free_hbq(phba, hbq_entry);
579 }
James Smart3163f722008-02-08 18:50:25 -0500580 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500581 } else {
582 lpfc_mbuf_free(phba, mp->virt, mp->phys);
583 kfree(mp);
584 }
585 return;
586}