blob: e198c917c13ecc55d91839d8ca64a8051ee7f097 [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>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040026#include <scsi/scsi_device.h>
27#include <scsi/scsi_transport_fc.h>
28
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
30
James Smartda0436e2009-05-22 14:51:39 -040031#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050032#include "lpfc_hw.h"
33#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040035#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_disc.h"
37#include "lpfc_scsi.h"
38#include "lpfc.h"
39#include "lpfc_crtn.h"
40
41#define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
42#define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
43
James Smart2e0fef82007-06-17 19:56:36 -050044
James Smarte59058c2008-08-24 21:49:00 -040045/**
James Smart3621a712009-04-06 18:47:14 -040046 * lpfc_mem_alloc - create and allocate all PCI and memory pools
James Smarte59058c2008-08-24 21:49:00 -040047 * @phba: HBA to allocate pools for
48 *
49 * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -040050 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
James Smarte59058c2008-08-24 21:49:00 -040051 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
52 *
53 * Notes: Not interrupt-safe. Must be called with no locks held. If any
54 * allocation fails, frees all successfully allocated memory before returning.
55 *
56 * Returns:
57 * 0 on success
58 * -ENOMEM on failure (if any memory allocations fail)
59 **/
dea31012005-04-17 16:05:31 -050060int
James Smartda0436e2009-05-22 14:51:39 -040061lpfc_mem_alloc(struct lpfc_hba *phba, int align)
dea31012005-04-17 16:05:31 -050062{
63 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart92d7f7b2007-06-17 19:56:38 -050064 int longs;
dea31012005-04-17 16:05:31 -050065 int i;
66
James Smartda0436e2009-05-22 14:51:39 -040067 if (phba->sli_rev == LPFC_SLI_REV4)
68 phba->lpfc_scsi_dma_buf_pool =
69 pci_pool_create("lpfc_scsi_dma_buf_pool",
70 phba->pcidev,
71 phba->cfg_sg_dma_buf_size,
72 phba->cfg_sg_dma_buf_size,
73 0);
74 else
75 phba->lpfc_scsi_dma_buf_pool =
76 pci_pool_create("lpfc_scsi_dma_buf_pool",
77 phba->pcidev, phba->cfg_sg_dma_buf_size,
78 align, 0);
dea31012005-04-17 16:05:31 -050079 if (!phba->lpfc_scsi_dma_buf_pool)
80 goto fail;
81
82 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
James Smartda0436e2009-05-22 14:51:39 -040083 LPFC_BPL_SIZE,
84 align, 0);
dea31012005-04-17 16:05:31 -050085 if (!phba->lpfc_mbuf_pool)
86 goto fail_free_dma_buf_pool;
87
88 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
89 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +010090 if (!pool->elements)
91 goto fail_free_lpfc_mbuf_pool;
92
dea31012005-04-17 16:05:31 -050093 pool->max_count = 0;
94 pool->current_count = 0;
95 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
96 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
97 GFP_KERNEL, &pool->elements[i].phys);
98 if (!pool->elements[i].virt)
99 goto fail_free_mbuf_pool;
100 pool->max_count++;
101 pool->current_count++;
102 }
103
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800104 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
105 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -0500106 if (!phba->mbox_mem_pool)
107 goto fail_free_mbuf_pool;
108
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800109 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
110 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -0500111 if (!phba->nlp_mem_pool)
112 goto fail_free_mbox_pool;
James Smartda0436e2009-05-22 14:51:39 -0400113 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
114 phba->pcidev,
115 LPFC_HDR_BUF_SIZE, align, 0);
116 if (!phba->lpfc_hrb_pool)
James Smarted957682007-06-17 19:56:37 -0500117 goto fail_free_nlp_mem_pool;
James Smartda0436e2009-05-22 14:51:39 -0400118 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
119 phba->pcidev,
120 LPFC_DATA_BUF_SIZE, align, 0);
121 if (!phba->lpfc_drb_pool)
122 goto fail_free_hbq_pool;
James Smarted957682007-06-17 19:56:37 -0500123
James Smart858c9f62007-06-17 19:56:39 -0500124 /* vpi zero is reserved for the physical port so add 1 to max */
125 longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG;
James Smart92d7f7b2007-06-17 19:56:38 -0500126 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
127 if (!phba->vpi_bmask)
James Smartda0436e2009-05-22 14:51:39 -0400128 goto fail_free_dbq_pool;
James Smart92d7f7b2007-06-17 19:56:38 -0500129
dea31012005-04-17 16:05:31 -0500130 return 0;
131
James Smartda0436e2009-05-22 14:51:39 -0400132 fail_free_dbq_pool:
133 pci_pool_destroy(phba->lpfc_drb_pool);
134 phba->lpfc_drb_pool = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -0500135 fail_free_hbq_pool:
James Smartda0436e2009-05-22 14:51:39 -0400136 pci_pool_destroy(phba->lpfc_hrb_pool);
137 phba->lpfc_hrb_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500138 fail_free_nlp_mem_pool:
139 mempool_destroy(phba->nlp_mem_pool);
140 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500141 fail_free_mbox_pool:
142 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500143 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500144 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100145 while (i--)
dea31012005-04-17 16:05:31 -0500146 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
147 pool->elements[i].phys);
148 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100149 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500150 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500151 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500152 fail_free_dma_buf_pool:
153 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500154 phba->lpfc_scsi_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500155 fail:
156 return -ENOMEM;
157}
158
James Smarte59058c2008-08-24 21:49:00 -0400159/**
James Smartda0436e2009-05-22 14:51:39 -0400160 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400161 * @phba: HBA to free memory for
162 *
James Smartda0436e2009-05-22 14:51:39 -0400163 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
164 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400165 *
166 * Returns: None
167 **/
dea31012005-04-17 16:05:31 -0500168void
James Smartda0436e2009-05-22 14:51:39 -0400169lpfc_mem_free(struct lpfc_hba *phba)
170{
171 int i;
172 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
173
174 /* Free VPI bitmask memory */
175 kfree(phba->vpi_bmask);
176
177 /* Free HBQ pools */
178 lpfc_sli_hbqbuf_free_all(phba);
179 pci_pool_destroy(phba->lpfc_drb_pool);
180 phba->lpfc_drb_pool = NULL;
181 pci_pool_destroy(phba->lpfc_hrb_pool);
182 phba->lpfc_hrb_pool = NULL;
183
184 /* Free NLP memory pool */
185 mempool_destroy(phba->nlp_mem_pool);
186 phba->nlp_mem_pool = NULL;
187
188 /* Free mbox memory pool */
189 mempool_destroy(phba->mbox_mem_pool);
190 phba->mbox_mem_pool = NULL;
191
192 /* Free MBUF memory pool */
193 for (i = 0; i < pool->current_count; i++)
194 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
195 pool->elements[i].phys);
196 kfree(pool->elements);
197
198 pci_pool_destroy(phba->lpfc_mbuf_pool);
199 phba->lpfc_mbuf_pool = NULL;
200
201 /* Free DMA buffer memory pool */
202 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
203 phba->lpfc_scsi_dma_buf_pool = NULL;
204
205 return;
206}
207
208/**
209 * lpfc_mem_free_all - Frees all PCI and driver memory
210 * @phba: HBA to free memory for
211 *
212 * Description: Free memory from PCI and driver memory pools and also those
213 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
214 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
215 * the VPI bitmask.
216 *
217 * Returns: None
218 **/
219void
220lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500221{
222 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500223 LPFC_MBOXQ_t *mbox, *next_mbox;
224 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500225
James Smartda0436e2009-05-22 14:51:39 -0400226 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500227 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
228 mp = (struct lpfc_dmabuf *) (mbox->context1);
229 if (mp) {
230 lpfc_mbuf_free(phba, mp->virt, mp->phys);
231 kfree(mp);
232 }
233 list_del(&mbox->list);
234 mempool_free(mbox, phba->mbox_mem_pool);
235 }
James Smartda0436e2009-05-22 14:51:39 -0400236 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500237 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
238 mp = (struct lpfc_dmabuf *) (mbox->context1);
239 if (mp) {
240 lpfc_mbuf_free(phba, mp->virt, mp->phys);
241 kfree(mp);
242 }
243 list_del(&mbox->list);
244 mempool_free(mbox, phba->mbox_mem_pool);
245 }
James Smartda0436e2009-05-22 14:51:39 -0400246 /* Free the active mailbox command back to the mailbox memory pool */
247 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500248 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400249 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500250 if (psli->mbox_active) {
251 mbox = psli->mbox_active;
252 mp = (struct lpfc_dmabuf *) (mbox->context1);
253 if (mp) {
254 lpfc_mbuf_free(phba, mp->virt, mp->phys);
255 kfree(mp);
256 }
257 mempool_free(mbox, phba->mbox_mem_pool);
258 psli->mbox_active = NULL;
259 }
260
James Smartda0436e2009-05-22 14:51:39 -0400261 /* Free and destroy all the allocated memory pools */
262 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500263
James Smarte59058c2008-08-24 21:49:00 -0400264 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400265 kfree(psli->iocbq_lookup);
266 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400267
268 return;
dea31012005-04-17 16:05:31 -0500269}
270
James Smarte59058c2008-08-24 21:49:00 -0400271/**
James Smart3621a712009-04-06 18:47:14 -0400272 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400273 * @phba: HBA which owns the pool to allocate from
274 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
275 * @handle: used to return the DMA-mapped address of the mbuf
276 *
277 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
278 * Allocates from generic pci_pool_alloc function first and if that fails and
279 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
280 * HBA's pool.
281 *
282 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
283 * phba->hbalock.
284 *
285 * Returns:
286 * pointer to the allocated mbuf on success
287 * NULL on failure
288 **/
dea31012005-04-17 16:05:31 -0500289void *
290lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
291{
292 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500293 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500294 void *ret;
295
296 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
297
James Smart2e0fef82007-06-17 19:56:36 -0500298 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500299 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500300 pool->current_count--;
301 ret = pool->elements[pool->current_count].virt;
302 *handle = pool->elements[pool->current_count].phys;
303 }
James Smart2e0fef82007-06-17 19:56:36 -0500304 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500305 return ret;
306}
307
James Smarte59058c2008-08-24 21:49:00 -0400308/**
James Smart3621a712009-04-06 18:47:14 -0400309 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400310 * @phba: HBA which owns the pool to return to
311 * @virt: mbuf to free
312 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
313 *
314 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
315 * it is below its max_count, frees the mbuf otherwise.
316 *
317 * Notes: Must be called with phba->hbalock held to synchronize access to
318 * lpfc_mbuf_safety_pool.
319 *
320 * Returns: None
321 **/
dea31012005-04-17 16:05:31 -0500322void
James Smart2e0fef82007-06-17 19:56:36 -0500323__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500324{
325 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
326
327 if (pool->current_count < pool->max_count) {
328 pool->elements[pool->current_count].virt = virt;
329 pool->elements[pool->current_count].phys = dma;
330 pool->current_count++;
331 } else {
332 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
333 }
334 return;
335}
James Smart2e0fef82007-06-17 19:56:36 -0500336
James Smarte59058c2008-08-24 21:49:00 -0400337/**
James Smart3621a712009-04-06 18:47:14 -0400338 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400339 * @phba: HBA which owns the pool to return to
340 * @virt: mbuf to free
341 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
342 *
343 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
344 * it is below its max_count, frees the mbuf otherwise.
345 *
346 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
347 *
348 * Returns: None
349 **/
James Smart2e0fef82007-06-17 19:56:36 -0500350void
351lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
352{
353 unsigned long iflags;
354
355 spin_lock_irqsave(&phba->hbalock, iflags);
356 __lpfc_mbuf_free(phba, virt, dma);
357 spin_unlock_irqrestore(&phba->hbalock, iflags);
358 return;
359}
James Smarted957682007-06-17 19:56:37 -0500360
James Smarte59058c2008-08-24 21:49:00 -0400361/**
James Smart3621a712009-04-06 18:47:14 -0400362 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400363 * @phba: HBA to allocate HBQ buffer for
364 *
James Smartda0436e2009-05-22 14:51:39 -0400365 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400366 * pool along a non-DMA-mapped container for it.
367 *
368 * Notes: Not interrupt-safe. Must be called with no locks held.
369 *
370 * Returns:
371 * pointer to HBQ on success
372 * NULL on failure
373 **/
James Smart51ef4c22007-08-02 11:10:31 -0400374struct hbq_dmabuf *
375lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500376{
James Smart51ef4c22007-08-02 11:10:31 -0400377 struct hbq_dmabuf *hbqbp;
378
379 hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
380 if (!hbqbp)
381 return NULL;
382
James Smartda0436e2009-05-22 14:51:39 -0400383 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400384 &hbqbp->dbuf.phys);
385 if (!hbqbp->dbuf.virt) {
386 kfree(hbqbp);
387 return NULL;
388 }
389 hbqbp->size = LPFC_BPL_SIZE;
390 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500391}
392
James Smarte59058c2008-08-24 21:49:00 -0400393/**
James Smartda0436e2009-05-22 14:51:39 -0400394 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400395 * @phba: HBA buffer was allocated for
396 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
397 *
398 * Description: Frees both the container and the DMA-mapped buffer returned by
399 * lpfc_els_hbq_alloc.
400 *
401 * Notes: Can be called with or without locks held.
402 *
403 * Returns: None
404 **/
James Smarted957682007-06-17 19:56:37 -0500405void
James Smart51ef4c22007-08-02 11:10:31 -0400406lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500407{
James Smartda0436e2009-05-22 14:51:39 -0400408 pci_pool_free(phba->lpfc_hrb_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400409 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500410 return;
411}
412
James Smarte59058c2008-08-24 21:49:00 -0400413/**
James Smartda0436e2009-05-22 14:51:39 -0400414 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
415 * @phba: HBA to allocate a receive buffer for
416 *
417 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
418 * pool along a non-DMA-mapped container for it.
419 *
420 * Notes: Not interrupt-safe. Must be called with no locks held.
421 *
422 * Returns:
423 * pointer to HBQ on success
424 * NULL on failure
425 **/
426struct hbq_dmabuf *
427lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
428{
429 struct hbq_dmabuf *dma_buf;
430
431 dma_buf = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
432 if (!dma_buf)
433 return NULL;
434
435 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
436 &dma_buf->hbuf.phys);
437 if (!dma_buf->hbuf.virt) {
438 kfree(dma_buf);
439 return NULL;
440 }
441 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
442 &dma_buf->dbuf.phys);
443 if (!dma_buf->dbuf.virt) {
444 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
445 dma_buf->hbuf.phys);
446 kfree(dma_buf);
447 return NULL;
448 }
449 dma_buf->size = LPFC_BPL_SIZE;
450 return dma_buf;
451}
452
453/**
454 * lpfc_sli4_rb_free - Frees a receive buffer
455 * @phba: HBA buffer was allocated for
456 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
457 *
458 * Description: Frees both the container and the DMA-mapped buffers returned by
459 * lpfc_sli4_rb_alloc.
460 *
461 * Notes: Can be called with or without locks held.
462 *
463 * Returns: None
464 **/
465void
466lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
467{
468 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
469 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
470 kfree(dmab);
471 return;
472}
473
474/**
James Smart3621a712009-04-06 18:47:14 -0400475 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400476 * @phba: HBA buffer is associated with
477 * @mp: Buffer to free
478 *
479 * Description: Frees the given DMA buffer in the appropriate way given if the
480 * HBA is running in SLI3 mode with HBQs enabled.
481 *
482 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
483 *
484 * Returns: None
485 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500486void
487lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
488{
489 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500490 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500491
James Smart7f5f3d02008-02-08 18:50:14 -0500492 if (!mp)
493 return;
494
James Smart92d7f7b2007-06-17 19:56:38 -0500495 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart3163f722008-02-08 18:50:25 -0500496 /* Check whether HBQ is still in use */
497 spin_lock_irqsave(&phba->hbalock, flags);
498 if (!phba->hbq_in_use) {
499 spin_unlock_irqrestore(&phba->hbalock, flags);
500 return;
501 }
James Smart92d7f7b2007-06-17 19:56:38 -0500502 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500503 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500504 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400505 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
506 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500507 } else {
508 lpfc_sli_free_hbq(phba, hbq_entry);
509 }
James Smart3163f722008-02-08 18:50:25 -0500510 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500511 } else {
512 lpfc_mbuf_free(phba, mp->virt, mp->phys);
513 kfree(mp);
514 }
515 return;
516}