blob: b1db23c70781d2f5ec0b5c6cf39e5e8196d4a838 [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"
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 */
45
James Smartcff261f2013-12-17 20:29:47 -050046int
47lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
48 size_t bytes;
49 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
50
51 if (max_xri <= 0)
52 return -ENOMEM;
53 bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) *
54 sizeof(unsigned long);
55 phba->cfg_rrq_xri_bitmap_sz = bytes;
56 phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
57 bytes);
58 if (!phba->active_rrq_pool)
59 return -ENOMEM;
60 else
61 return 0;
62}
James Smart2e0fef82007-06-17 19:56:36 -050063
James Smarte59058c2008-08-24 21:49:00 -040064/**
James Smart3621a712009-04-06 18:47:14 -040065 * lpfc_mem_alloc - create and allocate all PCI and memory pools
James Smarte59058c2008-08-24 21:49:00 -040066 * @phba: HBA to allocate pools for
67 *
68 * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -040069 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
James Smarte59058c2008-08-24 21:49:00 -040070 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
71 *
72 * Notes: Not interrupt-safe. Must be called with no locks held. If any
73 * allocation fails, frees all successfully allocated memory before returning.
74 *
75 * Returns:
76 * 0 on success
77 * -ENOMEM on failure (if any memory allocations fail)
78 **/
dea31012005-04-17 16:05:31 -050079int
James Smartda0436e2009-05-22 14:51:39 -040080lpfc_mem_alloc(struct lpfc_hba *phba, int align)
dea31012005-04-17 16:05:31 -050081{
82 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
83 int i;
84
James Smart96f70772013-04-17 20:16:15 -040085 if (phba->sli_rev == LPFC_SLI_REV4) {
86 /* Calculate alignment */
87 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
88 i = phba->cfg_sg_dma_buf_size;
89 else
90 i = SLI4_PAGE_SIZE;
91
James Smartda0436e2009-05-22 14:51:39 -040092 phba->lpfc_scsi_dma_buf_pool =
93 pci_pool_create("lpfc_scsi_dma_buf_pool",
94 phba->pcidev,
95 phba->cfg_sg_dma_buf_size,
James Smart96f70772013-04-17 20:16:15 -040096 i,
James Smartda0436e2009-05-22 14:51:39 -040097 0);
James Smart96f70772013-04-17 20:16:15 -040098 } else {
James Smartda0436e2009-05-22 14:51:39 -040099 phba->lpfc_scsi_dma_buf_pool =
100 pci_pool_create("lpfc_scsi_dma_buf_pool",
101 phba->pcidev, phba->cfg_sg_dma_buf_size,
102 align, 0);
James Smart96f70772013-04-17 20:16:15 -0400103 }
104
dea31012005-04-17 16:05:31 -0500105 if (!phba->lpfc_scsi_dma_buf_pool)
106 goto fail;
107
108 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
James Smartda0436e2009-05-22 14:51:39 -0400109 LPFC_BPL_SIZE,
110 align, 0);
dea31012005-04-17 16:05:31 -0500111 if (!phba->lpfc_mbuf_pool)
112 goto fail_free_dma_buf_pool;
113
114 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
115 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100116 if (!pool->elements)
117 goto fail_free_lpfc_mbuf_pool;
118
dea31012005-04-17 16:05:31 -0500119 pool->max_count = 0;
120 pool->current_count = 0;
121 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
122 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
123 GFP_KERNEL, &pool->elements[i].phys);
124 if (!pool->elements[i].virt)
125 goto fail_free_mbuf_pool;
126 pool->max_count++;
127 pool->current_count++;
128 }
129
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800130 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
131 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -0500132 if (!phba->mbox_mem_pool)
133 goto fail_free_mbuf_pool;
134
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800135 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
136 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -0500137 if (!phba->nlp_mem_pool)
138 goto fail_free_mbox_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400139
140 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart19ca7602010-11-20 23:11:55 -0500141 phba->rrq_pool =
142 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
143 sizeof(struct lpfc_node_rrq));
144 if (!phba->rrq_pool)
145 goto fail_free_nlp_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400146 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400147 phba->pcidev,
148 LPFC_HDR_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400149 if (!phba->lpfc_hrb_pool)
James Smart19ca7602010-11-20 23:11:55 -0500150 goto fail_free_rrq_mem_pool;
James Smart8568a4d2009-07-19 10:01:16 -0400151
152 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
James Smartda0436e2009-05-22 14:51:39 -0400153 phba->pcidev,
154 LPFC_DATA_BUF_SIZE, align, 0);
James Smart8568a4d2009-07-19 10:01:16 -0400155 if (!phba->lpfc_drb_pool)
156 goto fail_free_hrb_pool;
157 phba->lpfc_hbq_pool = NULL;
158 } else {
159 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
160 phba->pcidev, LPFC_BPL_SIZE, align, 0);
161 if (!phba->lpfc_hbq_pool)
162 goto fail_free_nlp_mem_pool;
163 phba->lpfc_hrb_pool = NULL;
164 phba->lpfc_drb_pool = NULL;
165 }
James Smart92d7f7b2007-06-17 19:56:38 -0500166
dea31012005-04-17 16:05:31 -0500167 return 0;
James Smart8568a4d2009-07-19 10:01:16 -0400168 fail_free_hrb_pool:
James Smartda0436e2009-05-22 14:51:39 -0400169 pci_pool_destroy(phba->lpfc_hrb_pool);
170 phba->lpfc_hrb_pool = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500171 fail_free_rrq_mem_pool:
172 mempool_destroy(phba->rrq_pool);
173 phba->rrq_pool = NULL;
James Smarted957682007-06-17 19:56:37 -0500174 fail_free_nlp_mem_pool:
175 mempool_destroy(phba->nlp_mem_pool);
176 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500177 fail_free_mbox_pool:
178 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500179 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500180 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100181 while (i--)
dea31012005-04-17 16:05:31 -0500182 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
183 pool->elements[i].phys);
184 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100185 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500186 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500187 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500188 fail_free_dma_buf_pool:
189 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500190 phba->lpfc_scsi_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500191 fail:
192 return -ENOMEM;
193}
194
James Smarte59058c2008-08-24 21:49:00 -0400195/**
James Smartda0436e2009-05-22 14:51:39 -0400196 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
James Smarte59058c2008-08-24 21:49:00 -0400197 * @phba: HBA to free memory for
198 *
James Smartda0436e2009-05-22 14:51:39 -0400199 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
200 * routine is a the counterpart of lpfc_mem_alloc.
James Smarte59058c2008-08-24 21:49:00 -0400201 *
202 * Returns: None
203 **/
dea31012005-04-17 16:05:31 -0500204void
James Smartda0436e2009-05-22 14:51:39 -0400205lpfc_mem_free(struct lpfc_hba *phba)
206{
207 int i;
208 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
209
James Smartda0436e2009-05-22 14:51:39 -0400210 /* Free HBQ pools */
211 lpfc_sli_hbqbuf_free_all(phba);
James Smart8568a4d2009-07-19 10:01:16 -0400212 if (phba->lpfc_drb_pool)
213 pci_pool_destroy(phba->lpfc_drb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400214 phba->lpfc_drb_pool = NULL;
James Smart8568a4d2009-07-19 10:01:16 -0400215 if (phba->lpfc_hrb_pool)
216 pci_pool_destroy(phba->lpfc_hrb_pool);
James Smartda0436e2009-05-22 14:51:39 -0400217 phba->lpfc_hrb_pool = NULL;
218
James Smart8568a4d2009-07-19 10:01:16 -0400219 if (phba->lpfc_hbq_pool)
220 pci_pool_destroy(phba->lpfc_hbq_pool);
221 phba->lpfc_hbq_pool = NULL;
222
James Smart21a688c2012-08-03 12:34:15 -0400223 if (phba->rrq_pool)
224 mempool_destroy(phba->rrq_pool);
225 phba->rrq_pool = NULL;
226
James Smartda0436e2009-05-22 14:51:39 -0400227 /* Free NLP memory pool */
228 mempool_destroy(phba->nlp_mem_pool);
229 phba->nlp_mem_pool = NULL;
James Smartcff261f2013-12-17 20:29:47 -0500230 if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
231 mempool_destroy(phba->active_rrq_pool);
232 phba->active_rrq_pool = NULL;
233 }
James Smartda0436e2009-05-22 14:51:39 -0400234
235 /* Free mbox memory pool */
236 mempool_destroy(phba->mbox_mem_pool);
237 phba->mbox_mem_pool = NULL;
238
239 /* Free MBUF memory pool */
240 for (i = 0; i < pool->current_count; i++)
241 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
242 pool->elements[i].phys);
243 kfree(pool->elements);
244
245 pci_pool_destroy(phba->lpfc_mbuf_pool);
246 phba->lpfc_mbuf_pool = NULL;
247
248 /* Free DMA buffer memory pool */
249 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
250 phba->lpfc_scsi_dma_buf_pool = NULL;
251
252 return;
253}
254
255/**
256 * lpfc_mem_free_all - Frees all PCI and driver memory
257 * @phba: HBA to free memory for
258 *
259 * Description: Free memory from PCI and driver memory pools and also those
260 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
261 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
262 * the VPI bitmask.
263 *
264 * Returns: None
265 **/
266void
267lpfc_mem_free_all(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500268{
269 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500270 LPFC_MBOXQ_t *mbox, *next_mbox;
271 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -0500272
James Smartda0436e2009-05-22 14:51:39 -0400273 /* Free memory used in mailbox queue back to mailbox memory pool */
dea31012005-04-17 16:05:31 -0500274 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
275 mp = (struct lpfc_dmabuf *) (mbox->context1);
276 if (mp) {
277 lpfc_mbuf_free(phba, mp->virt, mp->phys);
278 kfree(mp);
279 }
280 list_del(&mbox->list);
281 mempool_free(mbox, phba->mbox_mem_pool);
282 }
James Smartda0436e2009-05-22 14:51:39 -0400283 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
James Smart92d7f7b2007-06-17 19:56:38 -0500284 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
285 mp = (struct lpfc_dmabuf *) (mbox->context1);
286 if (mp) {
287 lpfc_mbuf_free(phba, mp->virt, mp->phys);
288 kfree(mp);
289 }
290 list_del(&mbox->list);
291 mempool_free(mbox, phba->mbox_mem_pool);
292 }
James Smartda0436e2009-05-22 14:51:39 -0400293 /* Free the active mailbox command back to the mailbox memory pool */
294 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500295 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smartda0436e2009-05-22 14:51:39 -0400296 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500297 if (psli->mbox_active) {
298 mbox = psli->mbox_active;
299 mp = (struct lpfc_dmabuf *) (mbox->context1);
300 if (mp) {
301 lpfc_mbuf_free(phba, mp->virt, mp->phys);
302 kfree(mp);
303 }
304 mempool_free(mbox, phba->mbox_mem_pool);
305 psli->mbox_active = NULL;
306 }
307
James Smartda0436e2009-05-22 14:51:39 -0400308 /* Free and destroy all the allocated memory pools */
309 lpfc_mem_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500310
James Smarte59058c2008-08-24 21:49:00 -0400311 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400312 kfree(psli->iocbq_lookup);
313 psli->iocbq_lookup = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400314
315 return;
dea31012005-04-17 16:05:31 -0500316}
317
James Smarte59058c2008-08-24 21:49:00 -0400318/**
James Smart3621a712009-04-06 18:47:14 -0400319 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
James Smarte59058c2008-08-24 21:49:00 -0400320 * @phba: HBA which owns the pool to allocate from
321 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
322 * @handle: used to return the DMA-mapped address of the mbuf
323 *
324 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
325 * Allocates from generic pci_pool_alloc function first and if that fails and
326 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
327 * HBA's pool.
328 *
329 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
330 * phba->hbalock.
331 *
332 * Returns:
333 * pointer to the allocated mbuf on success
334 * NULL on failure
335 **/
dea31012005-04-17 16:05:31 -0500336void *
337lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
338{
339 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500340 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500341 void *ret;
342
343 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
344
James Smart2e0fef82007-06-17 19:56:36 -0500345 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500346 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500347 pool->current_count--;
348 ret = pool->elements[pool->current_count].virt;
349 *handle = pool->elements[pool->current_count].phys;
350 }
James Smart2e0fef82007-06-17 19:56:36 -0500351 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500352 return ret;
353}
354
James Smarte59058c2008-08-24 21:49:00 -0400355/**
James Smart3621a712009-04-06 18:47:14 -0400356 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
James Smarte59058c2008-08-24 21:49:00 -0400357 * @phba: HBA which owns the pool to return to
358 * @virt: mbuf to free
359 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
360 *
361 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
362 * it is below its max_count, frees the mbuf otherwise.
363 *
364 * Notes: Must be called with phba->hbalock held to synchronize access to
365 * lpfc_mbuf_safety_pool.
366 *
367 * Returns: None
368 **/
dea31012005-04-17 16:05:31 -0500369void
James Smart2e0fef82007-06-17 19:56:36 -0500370__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500371{
372 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
373
374 if (pool->current_count < pool->max_count) {
375 pool->elements[pool->current_count].virt = virt;
376 pool->elements[pool->current_count].phys = dma;
377 pool->current_count++;
378 } else {
379 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
380 }
381 return;
382}
James Smart2e0fef82007-06-17 19:56:36 -0500383
James Smarte59058c2008-08-24 21:49:00 -0400384/**
James Smart3621a712009-04-06 18:47:14 -0400385 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
James Smarte59058c2008-08-24 21:49:00 -0400386 * @phba: HBA which owns the pool to return to
387 * @virt: mbuf to free
388 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
389 *
390 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
391 * it is below its max_count, frees the mbuf otherwise.
392 *
393 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
394 *
395 * Returns: None
396 **/
James Smart2e0fef82007-06-17 19:56:36 -0500397void
398lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
399{
400 unsigned long iflags;
401
402 spin_lock_irqsave(&phba->hbalock, iflags);
403 __lpfc_mbuf_free(phba, virt, dma);
404 spin_unlock_irqrestore(&phba->hbalock, iflags);
405 return;
406}
James Smarted957682007-06-17 19:56:37 -0500407
James Smarte59058c2008-08-24 21:49:00 -0400408/**
James Smart3621a712009-04-06 18:47:14 -0400409 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
James Smarte59058c2008-08-24 21:49:00 -0400410 * @phba: HBA to allocate HBQ buffer for
411 *
James Smartda0436e2009-05-22 14:51:39 -0400412 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
James Smarte59058c2008-08-24 21:49:00 -0400413 * pool along a non-DMA-mapped container for it.
414 *
415 * Notes: Not interrupt-safe. Must be called with no locks held.
416 *
417 * Returns:
418 * pointer to HBQ on success
419 * NULL on failure
420 **/
James Smart51ef4c22007-08-02 11:10:31 -0400421struct hbq_dmabuf *
422lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500423{
James Smart51ef4c22007-08-02 11:10:31 -0400424 struct hbq_dmabuf *hbqbp;
425
James Smart2e90f4b2011-12-13 13:22:37 -0500426 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smart51ef4c22007-08-02 11:10:31 -0400427 if (!hbqbp)
428 return NULL;
429
James Smart8568a4d2009-07-19 10:01:16 -0400430 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
James Smart51ef4c22007-08-02 11:10:31 -0400431 &hbqbp->dbuf.phys);
432 if (!hbqbp->dbuf.virt) {
433 kfree(hbqbp);
434 return NULL;
435 }
436 hbqbp->size = LPFC_BPL_SIZE;
437 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500438}
439
James Smarte59058c2008-08-24 21:49:00 -0400440/**
James Smartda0436e2009-05-22 14:51:39 -0400441 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
James Smarte59058c2008-08-24 21:49:00 -0400442 * @phba: HBA buffer was allocated for
443 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
444 *
445 * Description: Frees both the container and the DMA-mapped buffer returned by
446 * lpfc_els_hbq_alloc.
447 *
448 * Notes: Can be called with or without locks held.
449 *
450 * Returns: None
451 **/
James Smarted957682007-06-17 19:56:37 -0500452void
James Smart51ef4c22007-08-02 11:10:31 -0400453lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500454{
James Smart8568a4d2009-07-19 10:01:16 -0400455 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
James Smart51ef4c22007-08-02 11:10:31 -0400456 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500457 return;
458}
459
James Smarte59058c2008-08-24 21:49:00 -0400460/**
James Smartda0436e2009-05-22 14:51:39 -0400461 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
462 * @phba: HBA to allocate a receive buffer for
463 *
464 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
465 * pool along a non-DMA-mapped container for it.
466 *
467 * Notes: Not interrupt-safe. Must be called with no locks held.
468 *
469 * Returns:
470 * pointer to HBQ on success
471 * NULL on failure
472 **/
473struct hbq_dmabuf *
474lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
475{
476 struct hbq_dmabuf *dma_buf;
477
James Smart2e90f4b2011-12-13 13:22:37 -0500478 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -0400479 if (!dma_buf)
480 return NULL;
481
482 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
483 &dma_buf->hbuf.phys);
484 if (!dma_buf->hbuf.virt) {
485 kfree(dma_buf);
486 return NULL;
487 }
488 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
489 &dma_buf->dbuf.phys);
490 if (!dma_buf->dbuf.virt) {
491 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
492 dma_buf->hbuf.phys);
493 kfree(dma_buf);
494 return NULL;
495 }
496 dma_buf->size = LPFC_BPL_SIZE;
497 return dma_buf;
498}
499
500/**
501 * lpfc_sli4_rb_free - Frees a receive buffer
502 * @phba: HBA buffer was allocated for
503 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
504 *
505 * Description: Frees both the container and the DMA-mapped buffers returned by
506 * lpfc_sli4_rb_alloc.
507 *
508 * Notes: Can be called with or without locks held.
509 *
510 * Returns: None
511 **/
512void
513lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
514{
515 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
516 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
517 kfree(dmab);
518 return;
519}
520
521/**
James Smart3621a712009-04-06 18:47:14 -0400522 * lpfc_in_buf_free - Free a DMA buffer
James Smarte59058c2008-08-24 21:49:00 -0400523 * @phba: HBA buffer is associated with
524 * @mp: Buffer to free
525 *
526 * Description: Frees the given DMA buffer in the appropriate way given if the
527 * HBA is running in SLI3 mode with HBQs enabled.
528 *
529 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
530 *
531 * Returns: None
532 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500533void
534lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
535{
536 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500537 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500538
James Smart7f5f3d02008-02-08 18:50:14 -0500539 if (!mp)
540 return;
541
James Smart92d7f7b2007-06-17 19:56:38 -0500542 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart3163f722008-02-08 18:50:25 -0500543 /* Check whether HBQ is still in use */
544 spin_lock_irqsave(&phba->hbalock, flags);
545 if (!phba->hbq_in_use) {
546 spin_unlock_irqrestore(&phba->hbalock, flags);
547 return;
548 }
James Smart92d7f7b2007-06-17 19:56:38 -0500549 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500550 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500551 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400552 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
553 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500554 } else {
555 lpfc_sli_free_hbq(phba, hbq_entry);
556 }
James Smart3163f722008-02-08 18:50:25 -0500557 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500558 } else {
559 lpfc_mbuf_free(phba, mp->virt, mp->phys);
560 kfree(mp);
561 }
562 return;
563}