blob: 812d0cd7c86dcc8c9c3fe86f3ff6b8079ba850ab [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 Smart21a688c2012-08-03 12:34:15 -04004 * Copyright (C) 2004-2012 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;
65 int i;
66
James Smart96f70772013-04-17 20:16:15 -040067 if (phba->sli_rev == LPFC_SLI_REV4) {
68 /* Calculate alignment */
69 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
70 i = phba->cfg_sg_dma_buf_size;
71 else
72 i = SLI4_PAGE_SIZE;
73
James Smartda0436e2009-05-22 14:51:39 -040074 phba->lpfc_scsi_dma_buf_pool =
75 pci_pool_create("lpfc_scsi_dma_buf_pool",
76 phba->pcidev,
77 phba->cfg_sg_dma_buf_size,
James Smart96f70772013-04-17 20:16:15 -040078 i,
James Smartda0436e2009-05-22 14:51:39 -040079 0);
James Smart96f70772013-04-17 20:16:15 -040080 } else {
James Smartda0436e2009-05-22 14:51:39 -040081 phba->lpfc_scsi_dma_buf_pool =
82 pci_pool_create("lpfc_scsi_dma_buf_pool",
83 phba->pcidev, phba->cfg_sg_dma_buf_size,
84 align, 0);
James Smart96f70772013-04-17 20:16:15 -040085 }
86
dea31012005-04-17 16:05:31 -050087 if (!phba->lpfc_scsi_dma_buf_pool)
88 goto fail;
89
90 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
James Smartda0436e2009-05-22 14:51:39 -040091 LPFC_BPL_SIZE,
92 align, 0);
dea31012005-04-17 16:05:31 -050093 if (!phba->lpfc_mbuf_pool)
94 goto fail_free_dma_buf_pool;
95
96 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
97 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +010098 if (!pool->elements)
99 goto fail_free_lpfc_mbuf_pool;
100
dea31012005-04-17 16:05:31 -0500101 pool->max_count = 0;
102 pool->current_count = 0;
103 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
104 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
105 GFP_KERNEL, &pool->elements[i].phys);
106 if (!pool->elements[i].virt)
107 goto fail_free_mbuf_pool;
108 pool->max_count++;
109 pool->current_count++;
110 }
111
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800112 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
113 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -0500114 if (!phba->mbox_mem_pool)
115 goto fail_free_mbuf_pool;
116
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800117 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
118 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -0500119 if (!phba->nlp_mem_pool)
120 goto fail_free_mbox_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400121
122 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart19ca7602010-11-20 23:11:55 -0500123 phba->rrq_pool =
124 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
125 sizeof(struct lpfc_node_rrq));
126 if (!phba->rrq_pool)
127 goto fail_free_nlp_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400128 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400129 phba->pcidev,
130 LPFC_HDR_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400131 if (!phba->lpfc_hrb_pool)
James Smart19ca7602010-11-20 23:11:55 -0500132 goto fail_free_rrq_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400133
134 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400135 phba->pcidev,
136 LPFC_DATA_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400137 if (!phba->lpfc_drb_pool)
138 goto fail_free_hrb_pool;
139 phba->lpfc_hbq_pool = NULL;
140 } else {
141 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
142 phba->pcidev, LPFC_BPL_SIZE, align, 0);
143 if (!phba->lpfc_hbq_pool)
144 goto fail_free_nlp_mem_pool;
145 phba->lpfc_hrb_pool = NULL;
146 phba->lpfc_drb_pool = NULL;
147 }
James Smart92d7f7b2007-06-17 19:56:38 -0500148
dea31012005-04-17 16:05:31 -0500149 return 0;
James Smart8568a4d2009-07-19 10:01:16 -0400150 fail_free_hrb_pool:
James Smartda0436e2009-05-22 14:51:39 -0400151 pci_pool_destroy(phba->lpfc_hrb_pool);
152 phba->lpfc_hrb_pool = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500153 fail_free_rrq_mem_pool:
154 mempool_destroy(phba->rrq_pool);
155 phba->rrq_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500156 fail_free_nlp_mem_pool:
157 mempool_destroy(phba->nlp_mem_pool);
158 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500159 fail_free_mbox_pool:
160 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500161 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500162 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100163 while (i--)
dea31012005-04-17 16:05:31 -0500164 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
165 pool->elements[i].phys);
166 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100167 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500168 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500169 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500170 fail_free_dma_buf_pool:
171 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500172 phba->lpfc_scsi_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500173 fail:
174 return -ENOMEM;
175}
176
James Smarte59058c2008-08-24 21:49:00 -0400177/**
James Smartda0436e2009-05-22 14:51:39 -0400178 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400179 * @phba: HBA to free memory for
180 *
James Smartda0436e2009-05-22 14:51:39 -0400181 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
182 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400183 *
184 * Returns: None
185 **/
dea31012005-04-17 16:05:31 -0500186void
James Smartda0436e2009-05-22 14:51:39 -0400187lpfc_mem_free(struct lpfc_hba *phba)
188{
189 int i;
190 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
191
James Smartda0436e2009-05-22 14:51:39 -0400192 /* Free HBQ pools */
193 lpfc_sli_hbqbuf_free_all(phba);
James Smart8568a4d2009-07-19 10:01:16 -0400194 if (phba->lpfc_drb_pool)
195 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400196 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400197 if (phba->lpfc_hrb_pool)
198 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400199 phba->lpfc_hrb_pool = NULL;
200
James Smart8568a4d2009-07-19 10:01:16 -0400201 if (phba->lpfc_hbq_pool)
202 pci_pool_destroy(phba->lpfc_hbq_pool);
203 phba->lpfc_hbq_pool = NULL;
204
James Smart21a688c2012-08-03 12:34:15 -0400205 if (phba->rrq_pool)
206 mempool_destroy(phba->rrq_pool);
207 phba->rrq_pool = NULL;
208
James Smartda0436e2009-05-22 14:51:39 -0400209 /* Free NLP memory pool */
210 mempool_destroy(phba->nlp_mem_pool);
211 phba->nlp_mem_pool = NULL;
212
213 /* Free mbox memory pool */
214 mempool_destroy(phba->mbox_mem_pool);
215 phba->mbox_mem_pool = NULL;
216
217 /* Free MBUF memory pool */
218 for (i = 0; i < pool->current_count; i++)
219 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
220 pool->elements[i].phys);
221 kfree(pool->elements);
222
223 pci_pool_destroy(phba->lpfc_mbuf_pool);
224 phba->lpfc_mbuf_pool = NULL;
225
226 /* Free DMA buffer memory pool */
227 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
228 phba->lpfc_scsi_dma_buf_pool = NULL;
229
230 return;
231}
232
233/**
234 * lpfc_mem_free_all - Frees all PCI and driver memory
235 * @phba: HBA to free memory for
236 *
237 * Description: Free memory from PCI and driver memory pools and also those
238 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
239 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
240 * the VPI bitmask.
241 *
242 * Returns: None
243 **/
244void
245lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500246{
247 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500248 LPFC_MBOXQ_t *mbox, *next_mbox;
249 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500250
James Smartda0436e2009-05-22 14:51:39 -0400251 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500252 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
253 mp = (struct lpfc_dmabuf *) (mbox->context1);
254 if (mp) {
255 lpfc_mbuf_free(phba, mp->virt, mp->phys);
256 kfree(mp);
257 }
258 list_del(&mbox->list);
259 mempool_free(mbox, phba->mbox_mem_pool);
260 }
James Smartda0436e2009-05-22 14:51:39 -0400261 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500262 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
263 mp = (struct lpfc_dmabuf *) (mbox->context1);
264 if (mp) {
265 lpfc_mbuf_free(phba, mp->virt, mp->phys);
266 kfree(mp);
267 }
268 list_del(&mbox->list);
269 mempool_free(mbox, phba->mbox_mem_pool);
270 }
James Smartda0436e2009-05-22 14:51:39 -0400271 /* Free the active mailbox command back to the mailbox memory pool */
272 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500273 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400274 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500275 if (psli->mbox_active) {
276 mbox = psli->mbox_active;
277 mp = (struct lpfc_dmabuf *) (mbox->context1);
278 if (mp) {
279 lpfc_mbuf_free(phba, mp->virt, mp->phys);
280 kfree(mp);
281 }
282 mempool_free(mbox, phba->mbox_mem_pool);
283 psli->mbox_active = NULL;
284 }
285
James Smartda0436e2009-05-22 14:51:39 -0400286 /* Free and destroy all the allocated memory pools */
287 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500288
James Smarte59058c2008-08-24 21:49:00 -0400289 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400290 kfree(psli->iocbq_lookup);
291 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400292
293 return;
dea31012005-04-17 16:05:31 -0500294}
295
James Smarte59058c2008-08-24 21:49:00 -0400296/**
James Smart3621a712009-04-06 18:47:14 -0400297 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400298 * @phba: HBA which owns the pool to allocate from
299 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
300 * @handle: used to return the DMA-mapped address of the mbuf
301 *
302 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
303 * Allocates from generic pci_pool_alloc function first and if that fails and
304 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
305 * HBA's pool.
306 *
307 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
308 * phba->hbalock.
309 *
310 * Returns:
311 * pointer to the allocated mbuf on success
312 * NULL on failure
313 **/
dea31012005-04-17 16:05:31 -0500314void *
315lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
316{
317 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500318 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500319 void *ret;
320
321 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
322
James Smart2e0fef82007-06-17 19:56:36 -0500323 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500324 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500325 pool->current_count--;
326 ret = pool->elements[pool->current_count].virt;
327 *handle = pool->elements[pool->current_count].phys;
328 }
James Smart2e0fef82007-06-17 19:56:36 -0500329 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500330 return ret;
331}
332
James Smarte59058c2008-08-24 21:49:00 -0400333/**
James Smart3621a712009-04-06 18:47:14 -0400334 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400335 * @phba: HBA which owns the pool to return to
336 * @virt: mbuf to free
337 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
338 *
339 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
340 * it is below its max_count, frees the mbuf otherwise.
341 *
342 * Notes: Must be called with phba->hbalock held to synchronize access to
343 * lpfc_mbuf_safety_pool.
344 *
345 * Returns: None
346 **/
dea31012005-04-17 16:05:31 -0500347void
James Smart2e0fef82007-06-17 19:56:36 -0500348__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500349{
350 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
351
352 if (pool->current_count < pool->max_count) {
353 pool->elements[pool->current_count].virt = virt;
354 pool->elements[pool->current_count].phys = dma;
355 pool->current_count++;
356 } else {
357 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
358 }
359 return;
360}
James Smart2e0fef82007-06-17 19:56:36 -0500361
James Smarte59058c2008-08-24 21:49:00 -0400362/**
James Smart3621a712009-04-06 18:47:14 -0400363 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400364 * @phba: HBA which owns the pool to return to
365 * @virt: mbuf to free
366 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
367 *
368 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
369 * it is below its max_count, frees the mbuf otherwise.
370 *
371 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
372 *
373 * Returns: None
374 **/
James Smart2e0fef82007-06-17 19:56:36 -0500375void
376lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
377{
378 unsigned long iflags;
379
380 spin_lock_irqsave(&phba->hbalock, iflags);
381 __lpfc_mbuf_free(phba, virt, dma);
382 spin_unlock_irqrestore(&phba->hbalock, iflags);
383 return;
384}
James Smarted957682007-06-17 19:56:37 -0500385
James Smarte59058c2008-08-24 21:49:00 -0400386/**
James Smart3621a712009-04-06 18:47:14 -0400387 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400388 * @phba: HBA to allocate HBQ buffer for
389 *
James Smartda0436e2009-05-22 14:51:39 -0400390 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400391 * pool along a non-DMA-mapped container for it.
392 *
393 * Notes: Not interrupt-safe. Must be called with no locks held.
394 *
395 * Returns:
396 * pointer to HBQ on success
397 * NULL on failure
398 **/
James Smart51ef4c22007-08-02 11:10:31 -0400399struct hbq_dmabuf *
400lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500401{
James Smart51ef4c22007-08-02 11:10:31 -0400402 struct hbq_dmabuf *hbqbp;
403
James Smart2e90f4b2011-12-13 13:22:37 -0500404 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smart51ef4c22007-08-02 11:10:31 -0400405 if (!hbqbp)
406 return NULL;
407
James Smart8568a4d2009-07-19 10:01:16 -0400408 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400409 &hbqbp->dbuf.phys);
410 if (!hbqbp->dbuf.virt) {
411 kfree(hbqbp);
412 return NULL;
413 }
414 hbqbp->size = LPFC_BPL_SIZE;
415 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500416}
417
James Smarte59058c2008-08-24 21:49:00 -0400418/**
James Smartda0436e2009-05-22 14:51:39 -0400419 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400420 * @phba: HBA buffer was allocated for
421 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
422 *
423 * Description: Frees both the container and the DMA-mapped buffer returned by
424 * lpfc_els_hbq_alloc.
425 *
426 * Notes: Can be called with or without locks held.
427 *
428 * Returns: None
429 **/
James Smarted957682007-06-17 19:56:37 -0500430void
James Smart51ef4c22007-08-02 11:10:31 -0400431lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500432{
James Smart8568a4d2009-07-19 10:01:16 -0400433 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400434 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500435 return;
436}
437
James Smarte59058c2008-08-24 21:49:00 -0400438/**
James Smartda0436e2009-05-22 14:51:39 -0400439 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
440 * @phba: HBA to allocate a receive buffer for
441 *
442 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
443 * pool along a non-DMA-mapped container for it.
444 *
445 * Notes: Not interrupt-safe. Must be called with no locks held.
446 *
447 * Returns:
448 * pointer to HBQ on success
449 * NULL on failure
450 **/
451struct hbq_dmabuf *
452lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
453{
454 struct hbq_dmabuf *dma_buf;
455
James Smart2e90f4b2011-12-13 13:22:37 -0500456 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -0400457 if (!dma_buf)
458 return NULL;
459
460 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
461 &dma_buf->hbuf.phys);
462 if (!dma_buf->hbuf.virt) {
463 kfree(dma_buf);
464 return NULL;
465 }
466 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
467 &dma_buf->dbuf.phys);
468 if (!dma_buf->dbuf.virt) {
469 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
470 dma_buf->hbuf.phys);
471 kfree(dma_buf);
472 return NULL;
473 }
474 dma_buf->size = LPFC_BPL_SIZE;
475 return dma_buf;
476}
477
478/**
479 * lpfc_sli4_rb_free - Frees a receive buffer
480 * @phba: HBA buffer was allocated for
481 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
482 *
483 * Description: Frees both the container and the DMA-mapped buffers returned by
484 * lpfc_sli4_rb_alloc.
485 *
486 * Notes: Can be called with or without locks held.
487 *
488 * Returns: None
489 **/
490void
491lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
492{
493 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
494 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
495 kfree(dmab);
496 return;
497}
498
499/**
James Smart3621a712009-04-06 18:47:14 -0400500 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400501 * @phba: HBA buffer is associated with
502 * @mp: Buffer to free
503 *
504 * Description: Frees the given DMA buffer in the appropriate way given if the
505 * HBA is running in SLI3 mode with HBQs enabled.
506 *
507 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
508 *
509 * Returns: None
510 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500511void
512lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
513{
514 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500515 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500516
James Smart7f5f3d02008-02-08 18:50:14 -0500517 if (!mp)
518 return;
519
James Smart92d7f7b2007-06-17 19:56:38 -0500520 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart3163f722008-02-08 18:50:25 -0500521 /* Check whether HBQ is still in use */
522 spin_lock_irqsave(&phba->hbalock, flags);
523 if (!phba->hbq_in_use) {
524 spin_unlock_irqrestore(&phba->hbalock, flags);
525 return;
526 }
James Smart92d7f7b2007-06-17 19:56:38 -0500527 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500528 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500529 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400530 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
531 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500532 } else {
533 lpfc_sli_free_hbq(phba, hbq_entry);
534 }
James Smart3163f722008-02-08 18:50:25 -0500535 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500536 } else {
537 lpfc_mbuf_free(phba, mp->virt, mp->phys);
538 kfree(mp);
539 }
540 return;
541}