blob: cbb48ee8b0bb05359fbed0afa6cdd2719b8ff00b [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 Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 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"
41
42#define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
43#define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
44
James Smart2e0fef82007-06-17 19:56:36 -050045
James Smarte59058c2008-08-24 21:49:00 -040046/**
James Smart3621a712009-04-06 18:47:14 -040047 * lpfc_mem_alloc - create and allocate all PCI and memory pools
James Smarte59058c2008-08-24 21:49:00 -040048 * @phba: HBA to allocate pools for
49 *
50 * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -040051 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
James Smarte59058c2008-08-24 21:49:00 -040052 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
53 *
54 * Notes: Not interrupt-safe. Must be called with no locks held. If any
55 * allocation fails, frees all successfully allocated memory before returning.
56 *
57 * Returns:
58 * 0 on success
59 * -ENOMEM on failure (if any memory allocations fail)
60 **/
dea31012005-04-17 16:05:31 -050061int
James Smartda0436e2009-05-22 14:51:39 -040062lpfc_mem_alloc(struct lpfc_hba *phba, int align)
dea31012005-04-17 16:05:31 -050063{
64 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart92d7f7b2007-06-17 19:56:38 -050065 int longs;
dea31012005-04-17 16:05:31 -050066 int i;
67
James Smartda0436e2009-05-22 14:51:39 -040068 if (phba->sli_rev == LPFC_SLI_REV4)
69 phba->lpfc_scsi_dma_buf_pool =
70 pci_pool_create("lpfc_scsi_dma_buf_pool",
71 phba->pcidev,
72 phba->cfg_sg_dma_buf_size,
73 phba->cfg_sg_dma_buf_size,
74 0);
75 else
76 phba->lpfc_scsi_dma_buf_pool =
77 pci_pool_create("lpfc_scsi_dma_buf_pool",
78 phba->pcidev, phba->cfg_sg_dma_buf_size,
79 align, 0);
dea31012005-04-17 16:05:31 -050080 if (!phba->lpfc_scsi_dma_buf_pool)
81 goto fail;
82
83 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
James Smartda0436e2009-05-22 14:51:39 -040084 LPFC_BPL_SIZE,
85 align, 0);
dea31012005-04-17 16:05:31 -050086 if (!phba->lpfc_mbuf_pool)
87 goto fail_free_dma_buf_pool;
88
89 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
90 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +010091 if (!pool->elements)
92 goto fail_free_lpfc_mbuf_pool;
93
dea31012005-04-17 16:05:31 -050094 pool->max_count = 0;
95 pool->current_count = 0;
96 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
97 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
98 GFP_KERNEL, &pool->elements[i].phys);
99 if (!pool->elements[i].virt)
100 goto fail_free_mbuf_pool;
101 pool->max_count++;
102 pool->current_count++;
103 }
104
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800105 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
106 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -0500107 if (!phba->mbox_mem_pool)
108 goto fail_free_mbuf_pool;
109
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800110 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
111 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -0500112 if (!phba->nlp_mem_pool)
113 goto fail_free_mbox_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400114
115 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart19ca7602010-11-20 23:11:55 -0500116 phba->rrq_pool =
117 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
118 sizeof(struct lpfc_node_rrq));
119 if (!phba->rrq_pool)
120 goto fail_free_nlp_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400121 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400122 phba->pcidev,
123 LPFC_HDR_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400124 if (!phba->lpfc_hrb_pool)
James Smart19ca7602010-11-20 23:11:55 -0500125 goto fail_free_rrq_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400126
127 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400128 phba->pcidev,
129 LPFC_DATA_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400130 if (!phba->lpfc_drb_pool)
131 goto fail_free_hrb_pool;
132 phba->lpfc_hbq_pool = NULL;
133 } else {
134 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
135 phba->pcidev, LPFC_BPL_SIZE, align, 0);
136 if (!phba->lpfc_hbq_pool)
137 goto fail_free_nlp_mem_pool;
138 phba->lpfc_hrb_pool = NULL;
139 phba->lpfc_drb_pool = NULL;
140 }
James Smart858c9f62007-06-17 19:56:39 -0500141 /* vpi zero is reserved for the physical port so add 1 to max */
142 longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG;
James Smart92d7f7b2007-06-17 19:56:38 -0500143 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
144 if (!phba->vpi_bmask)
James Smartda0436e2009-05-22 14:51:39 -0400145 goto fail_free_dbq_pool;
James Smart92d7f7b2007-06-17 19:56:38 -0500146
dea31012005-04-17 16:05:31 -0500147 return 0;
148
James Smartda0436e2009-05-22 14:51:39 -0400149 fail_free_dbq_pool:
150 pci_pool_destroy(phba->lpfc_drb_pool);
151 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400152 fail_free_hrb_pool:
James Smartda0436e2009-05-22 14:51:39 -0400153 pci_pool_destroy(phba->lpfc_hrb_pool);
154 phba->lpfc_hrb_pool = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500155 fail_free_rrq_mem_pool:
156 mempool_destroy(phba->rrq_pool);
157 phba->rrq_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500158 fail_free_nlp_mem_pool:
159 mempool_destroy(phba->nlp_mem_pool);
160 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500161 fail_free_mbox_pool:
162 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500163 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500164 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100165 while (i--)
dea31012005-04-17 16:05:31 -0500166 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
167 pool->elements[i].phys);
168 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100169 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500170 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500171 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500172 fail_free_dma_buf_pool:
173 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500174 phba->lpfc_scsi_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500175 fail:
176 return -ENOMEM;
177}
178
James Smarte59058c2008-08-24 21:49:00 -0400179/**
James Smartda0436e2009-05-22 14:51:39 -0400180 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400181 * @phba: HBA to free memory for
182 *
James Smartda0436e2009-05-22 14:51:39 -0400183 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
184 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400185 *
186 * Returns: None
187 **/
dea31012005-04-17 16:05:31 -0500188void
James Smartda0436e2009-05-22 14:51:39 -0400189lpfc_mem_free(struct lpfc_hba *phba)
190{
191 int i;
192 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
193
194 /* Free VPI bitmask memory */
195 kfree(phba->vpi_bmask);
196
197 /* Free HBQ pools */
198 lpfc_sli_hbqbuf_free_all(phba);
James Smart8568a4d2009-07-19 10:01:16 -0400199 if (phba->lpfc_drb_pool)
200 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400201 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400202 if (phba->lpfc_hrb_pool)
203 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400204 phba->lpfc_hrb_pool = NULL;
205
James Smart8568a4d2009-07-19 10:01:16 -0400206 if (phba->lpfc_hbq_pool)
207 pci_pool_destroy(phba->lpfc_hbq_pool);
208 phba->lpfc_hbq_pool = NULL;
209
James Smartda0436e2009-05-22 14:51:39 -0400210 /* Free NLP memory pool */
211 mempool_destroy(phba->nlp_mem_pool);
212 phba->nlp_mem_pool = NULL;
213
214 /* Free mbox memory pool */
215 mempool_destroy(phba->mbox_mem_pool);
216 phba->mbox_mem_pool = NULL;
217
218 /* Free MBUF memory pool */
219 for (i = 0; i < pool->current_count; i++)
220 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
221 pool->elements[i].phys);
222 kfree(pool->elements);
223
224 pci_pool_destroy(phba->lpfc_mbuf_pool);
225 phba->lpfc_mbuf_pool = NULL;
226
227 /* Free DMA buffer memory pool */
228 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
229 phba->lpfc_scsi_dma_buf_pool = NULL;
230
231 return;
232}
233
234/**
235 * lpfc_mem_free_all - Frees all PCI and driver memory
236 * @phba: HBA to free memory for
237 *
238 * Description: Free memory from PCI and driver memory pools and also those
239 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
240 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
241 * the VPI bitmask.
242 *
243 * Returns: None
244 **/
245void
246lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500247{
248 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500249 LPFC_MBOXQ_t *mbox, *next_mbox;
250 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500251
James Smartda0436e2009-05-22 14:51:39 -0400252 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500253 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
254 mp = (struct lpfc_dmabuf *) (mbox->context1);
255 if (mp) {
256 lpfc_mbuf_free(phba, mp->virt, mp->phys);
257 kfree(mp);
258 }
259 list_del(&mbox->list);
260 mempool_free(mbox, phba->mbox_mem_pool);
261 }
James Smartda0436e2009-05-22 14:51:39 -0400262 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500263 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
264 mp = (struct lpfc_dmabuf *) (mbox->context1);
265 if (mp) {
266 lpfc_mbuf_free(phba, mp->virt, mp->phys);
267 kfree(mp);
268 }
269 list_del(&mbox->list);
270 mempool_free(mbox, phba->mbox_mem_pool);
271 }
James Smartda0436e2009-05-22 14:51:39 -0400272 /* Free the active mailbox command back to the mailbox memory pool */
273 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500274 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400275 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500276 if (psli->mbox_active) {
277 mbox = psli->mbox_active;
278 mp = (struct lpfc_dmabuf *) (mbox->context1);
279 if (mp) {
280 lpfc_mbuf_free(phba, mp->virt, mp->phys);
281 kfree(mp);
282 }
283 mempool_free(mbox, phba->mbox_mem_pool);
284 psli->mbox_active = NULL;
285 }
286
James Smartda0436e2009-05-22 14:51:39 -0400287 /* Free and destroy all the allocated memory pools */
288 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500289
James Smarte59058c2008-08-24 21:49:00 -0400290 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400291 kfree(psli->iocbq_lookup);
292 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400293
294 return;
dea31012005-04-17 16:05:31 -0500295}
296
James Smarte59058c2008-08-24 21:49:00 -0400297/**
James Smart3621a712009-04-06 18:47:14 -0400298 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400299 * @phba: HBA which owns the pool to allocate from
300 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
301 * @handle: used to return the DMA-mapped address of the mbuf
302 *
303 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
304 * Allocates from generic pci_pool_alloc function first and if that fails and
305 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
306 * HBA's pool.
307 *
308 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
309 * phba->hbalock.
310 *
311 * Returns:
312 * pointer to the allocated mbuf on success
313 * NULL on failure
314 **/
dea31012005-04-17 16:05:31 -0500315void *
316lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
317{
318 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500319 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500320 void *ret;
321
322 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
323
James Smart2e0fef82007-06-17 19:56:36 -0500324 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500325 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500326 pool->current_count--;
327 ret = pool->elements[pool->current_count].virt;
328 *handle = pool->elements[pool->current_count].phys;
329 }
James Smart2e0fef82007-06-17 19:56:36 -0500330 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500331 return ret;
332}
333
James Smarte59058c2008-08-24 21:49:00 -0400334/**
James Smart3621a712009-04-06 18:47:14 -0400335 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400336 * @phba: HBA which owns the pool to return to
337 * @virt: mbuf to free
338 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
339 *
340 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
341 * it is below its max_count, frees the mbuf otherwise.
342 *
343 * Notes: Must be called with phba->hbalock held to synchronize access to
344 * lpfc_mbuf_safety_pool.
345 *
346 * Returns: None
347 **/
dea31012005-04-17 16:05:31 -0500348void
James Smart2e0fef82007-06-17 19:56:36 -0500349__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500350{
351 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
352
353 if (pool->current_count < pool->max_count) {
354 pool->elements[pool->current_count].virt = virt;
355 pool->elements[pool->current_count].phys = dma;
356 pool->current_count++;
357 } else {
358 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
359 }
360 return;
361}
James Smart2e0fef82007-06-17 19:56:36 -0500362
James Smarte59058c2008-08-24 21:49:00 -0400363/**
James Smart3621a712009-04-06 18:47:14 -0400364 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400365 * @phba: HBA which owns the pool to return to
366 * @virt: mbuf to free
367 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
368 *
369 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
370 * it is below its max_count, frees the mbuf otherwise.
371 *
372 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
373 *
374 * Returns: None
375 **/
James Smart2e0fef82007-06-17 19:56:36 -0500376void
377lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
378{
379 unsigned long iflags;
380
381 spin_lock_irqsave(&phba->hbalock, iflags);
382 __lpfc_mbuf_free(phba, virt, dma);
383 spin_unlock_irqrestore(&phba->hbalock, iflags);
384 return;
385}
James Smarted957682007-06-17 19:56:37 -0500386
James Smarte59058c2008-08-24 21:49:00 -0400387/**
James Smart3621a712009-04-06 18:47:14 -0400388 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400389 * @phba: HBA to allocate HBQ buffer for
390 *
James Smartda0436e2009-05-22 14:51:39 -0400391 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400392 * pool along a non-DMA-mapped container for it.
393 *
394 * Notes: Not interrupt-safe. Must be called with no locks held.
395 *
396 * Returns:
397 * pointer to HBQ on success
398 * NULL on failure
399 **/
James Smart51ef4c22007-08-02 11:10:31 -0400400struct hbq_dmabuf *
401lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500402{
James Smart51ef4c22007-08-02 11:10:31 -0400403 struct hbq_dmabuf *hbqbp;
404
405 hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
406 if (!hbqbp)
407 return NULL;
408
James Smart8568a4d2009-07-19 10:01:16 -0400409 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400410 &hbqbp->dbuf.phys);
411 if (!hbqbp->dbuf.virt) {
412 kfree(hbqbp);
413 return NULL;
414 }
415 hbqbp->size = LPFC_BPL_SIZE;
416 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500417}
418
James Smarte59058c2008-08-24 21:49:00 -0400419/**
James Smartda0436e2009-05-22 14:51:39 -0400420 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400421 * @phba: HBA buffer was allocated for
422 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
423 *
424 * Description: Frees both the container and the DMA-mapped buffer returned by
425 * lpfc_els_hbq_alloc.
426 *
427 * Notes: Can be called with or without locks held.
428 *
429 * Returns: None
430 **/
James Smarted957682007-06-17 19:56:37 -0500431void
James Smart51ef4c22007-08-02 11:10:31 -0400432lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500433{
James Smart8568a4d2009-07-19 10:01:16 -0400434 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400435 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500436 return;
437}
438
James Smarte59058c2008-08-24 21:49:00 -0400439/**
James Smartda0436e2009-05-22 14:51:39 -0400440 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
441 * @phba: HBA to allocate a receive buffer for
442 *
443 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
444 * pool along a non-DMA-mapped container for it.
445 *
446 * Notes: Not interrupt-safe. Must be called with no locks held.
447 *
448 * Returns:
449 * pointer to HBQ on success
450 * NULL on failure
451 **/
452struct hbq_dmabuf *
453lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
454{
455 struct hbq_dmabuf *dma_buf;
456
457 dma_buf = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
458 if (!dma_buf)
459 return NULL;
460
461 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
462 &dma_buf->hbuf.phys);
463 if (!dma_buf->hbuf.virt) {
464 kfree(dma_buf);
465 return NULL;
466 }
467 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
468 &dma_buf->dbuf.phys);
469 if (!dma_buf->dbuf.virt) {
470 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
471 dma_buf->hbuf.phys);
472 kfree(dma_buf);
473 return NULL;
474 }
475 dma_buf->size = LPFC_BPL_SIZE;
476 return dma_buf;
477}
478
479/**
480 * lpfc_sli4_rb_free - Frees a receive buffer
481 * @phba: HBA buffer was allocated for
482 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
483 *
484 * Description: Frees both the container and the DMA-mapped buffers returned by
485 * lpfc_sli4_rb_alloc.
486 *
487 * Notes: Can be called with or without locks held.
488 *
489 * Returns: None
490 **/
491void
492lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
493{
494 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
495 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
496 kfree(dmab);
497 return;
498}
499
500/**
James Smart3621a712009-04-06 18:47:14 -0400501 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400502 * @phba: HBA buffer is associated with
503 * @mp: Buffer to free
504 *
505 * Description: Frees the given DMA buffer in the appropriate way given if the
506 * HBA is running in SLI3 mode with HBQs enabled.
507 *
508 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
509 *
510 * Returns: None
511 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500512void
513lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
514{
515 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500516 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500517
James Smart7f5f3d02008-02-08 18:50:14 -0500518 if (!mp)
519 return;
520
James Smart92d7f7b2007-06-17 19:56:38 -0500521 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart3163f722008-02-08 18:50:25 -0500522 /* Check whether HBQ is still in use */
523 spin_lock_irqsave(&phba->hbalock, flags);
524 if (!phba->hbq_in_use) {
525 spin_unlock_irqrestore(&phba->hbalock, flags);
526 return;
527 }
James Smart92d7f7b2007-06-17 19:56:38 -0500528 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500529 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500530 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400531 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
532 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500533 } else {
534 lpfc_sli_free_hbq(phba, hbq_entry);
535 }
James Smart3163f722008-02-08 18:50:25 -0500536 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500537 } else {
538 lpfc_mbuf_free(phba, mp->virt, mp->phys);
539 kfree(mp);
540 }
541 return;
542}