blob: a1b6db6016da0aee42bf9753833012d26bad85e9 [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 Smart8568a4d2009-07-19 10:01:16 -0400113
114 if (phba->sli_rev == LPFC_SLI_REV4) {
115 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400116 phba->pcidev,
117 LPFC_HDR_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400118 if (!phba->lpfc_hrb_pool)
119 goto fail_free_nlp_mem_pool;
120
121 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400122 phba->pcidev,
123 LPFC_DATA_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400124 if (!phba->lpfc_drb_pool)
125 goto fail_free_hrb_pool;
126 phba->lpfc_hbq_pool = NULL;
127 } else {
128 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
129 phba->pcidev, LPFC_BPL_SIZE, align, 0);
130 if (!phba->lpfc_hbq_pool)
131 goto fail_free_nlp_mem_pool;
132 phba->lpfc_hrb_pool = NULL;
133 phba->lpfc_drb_pool = NULL;
134 }
James Smart858c9f62007-06-17 19:56:39 -0500135 /* vpi zero is reserved for the physical port so add 1 to max */
136 longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG;
James Smart92d7f7b2007-06-17 19:56:38 -0500137 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
138 if (!phba->vpi_bmask)
James Smartda0436e2009-05-22 14:51:39 -0400139 goto fail_free_dbq_pool;
James Smart92d7f7b2007-06-17 19:56:38 -0500140
dea31012005-04-17 16:05:31 -0500141 return 0;
142
James Smartda0436e2009-05-22 14:51:39 -0400143 fail_free_dbq_pool:
144 pci_pool_destroy(phba->lpfc_drb_pool);
145 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400146 fail_free_hrb_pool:
James Smartda0436e2009-05-22 14:51:39 -0400147 pci_pool_destroy(phba->lpfc_hrb_pool);
148 phba->lpfc_hrb_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500149 fail_free_nlp_mem_pool:
150 mempool_destroy(phba->nlp_mem_pool);
151 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500152 fail_free_mbox_pool:
153 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500154 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500155 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100156 while (i--)
dea31012005-04-17 16:05:31 -0500157 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
158 pool->elements[i].phys);
159 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100160 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500161 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500162 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500163 fail_free_dma_buf_pool:
164 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500165 phba->lpfc_scsi_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500166 fail:
167 return -ENOMEM;
168}
169
James Smarte59058c2008-08-24 21:49:00 -0400170/**
James Smartda0436e2009-05-22 14:51:39 -0400171 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400172 * @phba: HBA to free memory for
173 *
James Smartda0436e2009-05-22 14:51:39 -0400174 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
175 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400176 *
177 * Returns: None
178 **/
dea31012005-04-17 16:05:31 -0500179void
James Smartda0436e2009-05-22 14:51:39 -0400180lpfc_mem_free(struct lpfc_hba *phba)
181{
182 int i;
183 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
184
185 /* Free VPI bitmask memory */
186 kfree(phba->vpi_bmask);
187
188 /* Free HBQ pools */
189 lpfc_sli_hbqbuf_free_all(phba);
James Smart8568a4d2009-07-19 10:01:16 -0400190 if (phba->lpfc_drb_pool)
191 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400192 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400193 if (phba->lpfc_hrb_pool)
194 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400195 phba->lpfc_hrb_pool = NULL;
196
James Smart8568a4d2009-07-19 10:01:16 -0400197 if (phba->lpfc_hbq_pool)
198 pci_pool_destroy(phba->lpfc_hbq_pool);
199 phba->lpfc_hbq_pool = NULL;
200
James Smartda0436e2009-05-22 14:51:39 -0400201 /* Free NLP memory pool */
202 mempool_destroy(phba->nlp_mem_pool);
203 phba->nlp_mem_pool = NULL;
204
205 /* Free mbox memory pool */
206 mempool_destroy(phba->mbox_mem_pool);
207 phba->mbox_mem_pool = NULL;
208
209 /* Free MBUF memory pool */
210 for (i = 0; i < pool->current_count; i++)
211 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
212 pool->elements[i].phys);
213 kfree(pool->elements);
214
215 pci_pool_destroy(phba->lpfc_mbuf_pool);
216 phba->lpfc_mbuf_pool = NULL;
217
218 /* Free DMA buffer memory pool */
219 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
220 phba->lpfc_scsi_dma_buf_pool = NULL;
221
222 return;
223}
224
225/**
226 * lpfc_mem_free_all - Frees all PCI and driver memory
227 * @phba: HBA to free memory for
228 *
229 * Description: Free memory from PCI and driver memory pools and also those
230 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
231 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
232 * the VPI bitmask.
233 *
234 * Returns: None
235 **/
236void
237lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500238{
239 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500240 LPFC_MBOXQ_t *mbox, *next_mbox;
241 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500242
James Smartda0436e2009-05-22 14:51:39 -0400243 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500244 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
245 mp = (struct lpfc_dmabuf *) (mbox->context1);
246 if (mp) {
247 lpfc_mbuf_free(phba, mp->virt, mp->phys);
248 kfree(mp);
249 }
250 list_del(&mbox->list);
251 mempool_free(mbox, phba->mbox_mem_pool);
252 }
James Smartda0436e2009-05-22 14:51:39 -0400253 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500254 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
255 mp = (struct lpfc_dmabuf *) (mbox->context1);
256 if (mp) {
257 lpfc_mbuf_free(phba, mp->virt, mp->phys);
258 kfree(mp);
259 }
260 list_del(&mbox->list);
261 mempool_free(mbox, phba->mbox_mem_pool);
262 }
James Smartda0436e2009-05-22 14:51:39 -0400263 /* Free the active mailbox command back to the mailbox memory pool */
264 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500265 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400266 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500267 if (psli->mbox_active) {
268 mbox = psli->mbox_active;
269 mp = (struct lpfc_dmabuf *) (mbox->context1);
270 if (mp) {
271 lpfc_mbuf_free(phba, mp->virt, mp->phys);
272 kfree(mp);
273 }
274 mempool_free(mbox, phba->mbox_mem_pool);
275 psli->mbox_active = NULL;
276 }
277
James Smartda0436e2009-05-22 14:51:39 -0400278 /* Free and destroy all the allocated memory pools */
279 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500280
James Smarte59058c2008-08-24 21:49:00 -0400281 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400282 kfree(psli->iocbq_lookup);
283 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400284
285 return;
dea31012005-04-17 16:05:31 -0500286}
287
James Smarte59058c2008-08-24 21:49:00 -0400288/**
James Smart3621a712009-04-06 18:47:14 -0400289 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400290 * @phba: HBA which owns the pool to allocate from
291 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
292 * @handle: used to return the DMA-mapped address of the mbuf
293 *
294 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
295 * Allocates from generic pci_pool_alloc function first and if that fails and
296 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
297 * HBA's pool.
298 *
299 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
300 * phba->hbalock.
301 *
302 * Returns:
303 * pointer to the allocated mbuf on success
304 * NULL on failure
305 **/
dea31012005-04-17 16:05:31 -0500306void *
307lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
308{
309 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500310 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500311 void *ret;
312
313 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
314
James Smart2e0fef82007-06-17 19:56:36 -0500315 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500316 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500317 pool->current_count--;
318 ret = pool->elements[pool->current_count].virt;
319 *handle = pool->elements[pool->current_count].phys;
320 }
James Smart2e0fef82007-06-17 19:56:36 -0500321 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500322 return ret;
323}
324
James Smarte59058c2008-08-24 21:49:00 -0400325/**
James Smart3621a712009-04-06 18:47:14 -0400326 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400327 * @phba: HBA which owns the pool to return to
328 * @virt: mbuf to free
329 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
330 *
331 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
332 * it is below its max_count, frees the mbuf otherwise.
333 *
334 * Notes: Must be called with phba->hbalock held to synchronize access to
335 * lpfc_mbuf_safety_pool.
336 *
337 * Returns: None
338 **/
dea31012005-04-17 16:05:31 -0500339void
James Smart2e0fef82007-06-17 19:56:36 -0500340__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500341{
342 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
343
344 if (pool->current_count < pool->max_count) {
345 pool->elements[pool->current_count].virt = virt;
346 pool->elements[pool->current_count].phys = dma;
347 pool->current_count++;
348 } else {
349 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
350 }
351 return;
352}
James Smart2e0fef82007-06-17 19:56:36 -0500353
James Smarte59058c2008-08-24 21:49:00 -0400354/**
James Smart3621a712009-04-06 18:47:14 -0400355 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400356 * @phba: HBA which owns the pool to return to
357 * @virt: mbuf to free
358 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
359 *
360 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
361 * it is below its max_count, frees the mbuf otherwise.
362 *
363 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
364 *
365 * Returns: None
366 **/
James Smart2e0fef82007-06-17 19:56:36 -0500367void
368lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
369{
370 unsigned long iflags;
371
372 spin_lock_irqsave(&phba->hbalock, iflags);
373 __lpfc_mbuf_free(phba, virt, dma);
374 spin_unlock_irqrestore(&phba->hbalock, iflags);
375 return;
376}
James Smarted957682007-06-17 19:56:37 -0500377
James Smarte59058c2008-08-24 21:49:00 -0400378/**
James Smart3621a712009-04-06 18:47:14 -0400379 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400380 * @phba: HBA to allocate HBQ buffer for
381 *
James Smartda0436e2009-05-22 14:51:39 -0400382 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400383 * pool along a non-DMA-mapped container for it.
384 *
385 * Notes: Not interrupt-safe. Must be called with no locks held.
386 *
387 * Returns:
388 * pointer to HBQ on success
389 * NULL on failure
390 **/
James Smart51ef4c22007-08-02 11:10:31 -0400391struct hbq_dmabuf *
392lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500393{
James Smart51ef4c22007-08-02 11:10:31 -0400394 struct hbq_dmabuf *hbqbp;
395
396 hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
397 if (!hbqbp)
398 return NULL;
399
James Smart8568a4d2009-07-19 10:01:16 -0400400 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400401 &hbqbp->dbuf.phys);
402 if (!hbqbp->dbuf.virt) {
403 kfree(hbqbp);
404 return NULL;
405 }
406 hbqbp->size = LPFC_BPL_SIZE;
407 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500408}
409
James Smarte59058c2008-08-24 21:49:00 -0400410/**
James Smartda0436e2009-05-22 14:51:39 -0400411 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400412 * @phba: HBA buffer was allocated for
413 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
414 *
415 * Description: Frees both the container and the DMA-mapped buffer returned by
416 * lpfc_els_hbq_alloc.
417 *
418 * Notes: Can be called with or without locks held.
419 *
420 * Returns: None
421 **/
James Smarted957682007-06-17 19:56:37 -0500422void
James Smart51ef4c22007-08-02 11:10:31 -0400423lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500424{
James Smart8568a4d2009-07-19 10:01:16 -0400425 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400426 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500427 return;
428}
429
James Smarte59058c2008-08-24 21:49:00 -0400430/**
James Smartda0436e2009-05-22 14:51:39 -0400431 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
432 * @phba: HBA to allocate a receive buffer for
433 *
434 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
435 * pool along a non-DMA-mapped container for it.
436 *
437 * Notes: Not interrupt-safe. Must be called with no locks held.
438 *
439 * Returns:
440 * pointer to HBQ on success
441 * NULL on failure
442 **/
443struct hbq_dmabuf *
444lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
445{
446 struct hbq_dmabuf *dma_buf;
447
448 dma_buf = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
449 if (!dma_buf)
450 return NULL;
451
452 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
453 &dma_buf->hbuf.phys);
454 if (!dma_buf->hbuf.virt) {
455 kfree(dma_buf);
456 return NULL;
457 }
458 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
459 &dma_buf->dbuf.phys);
460 if (!dma_buf->dbuf.virt) {
461 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
462 dma_buf->hbuf.phys);
463 kfree(dma_buf);
464 return NULL;
465 }
466 dma_buf->size = LPFC_BPL_SIZE;
467 return dma_buf;
468}
469
470/**
471 * lpfc_sli4_rb_free - Frees a receive buffer
472 * @phba: HBA buffer was allocated for
473 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
474 *
475 * Description: Frees both the container and the DMA-mapped buffers returned by
476 * lpfc_sli4_rb_alloc.
477 *
478 * Notes: Can be called with or without locks held.
479 *
480 * Returns: None
481 **/
482void
483lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
484{
485 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
486 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
487 kfree(dmab);
488 return;
489}
490
491/**
James Smart3621a712009-04-06 18:47:14 -0400492 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400493 * @phba: HBA buffer is associated with
494 * @mp: Buffer to free
495 *
496 * Description: Frees the given DMA buffer in the appropriate way given if the
497 * HBA is running in SLI3 mode with HBQs enabled.
498 *
499 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
500 *
501 * Returns: None
502 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500503void
504lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
505{
506 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500507 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500508
James Smart7f5f3d02008-02-08 18:50:14 -0500509 if (!mp)
510 return;
511
James Smart92d7f7b2007-06-17 19:56:38 -0500512 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart3163f722008-02-08 18:50:25 -0500513 /* Check whether HBQ is still in use */
514 spin_lock_irqsave(&phba->hbalock, flags);
515 if (!phba->hbq_in_use) {
516 spin_unlock_irqrestore(&phba->hbalock, flags);
517 return;
518 }
James Smart92d7f7b2007-06-17 19:56:38 -0500519 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500520 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500521 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400522 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
523 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500524 } else {
525 lpfc_sli_free_hbq(phba, hbq_entry);
526 }
James Smart3163f722008-02-08 18:50:25 -0500527 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500528 } else {
529 lpfc_mbuf_free(phba, mp->virt, mp->phys);
530 kfree(mp);
531 }
532 return;
533}