blob: 50d9136a6e047b3c3264e2e8b7f38dd002fb6516 [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 Smarte59058c2008-08-24 21:49:00 -04004 * Copyright (C) 2004-2008 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
dea31012005-04-17 16:05:31 -050031#include "lpfc_hw.h"
32#include "lpfc_sli.h"
33#include "lpfc_disc.h"
34#include "lpfc_scsi.h"
35#include "lpfc.h"
36#include "lpfc_crtn.h"
37
38#define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
39#define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
40
James Smart2e0fef82007-06-17 19:56:36 -050041
James Smarte59058c2008-08-24 21:49:00 -040042/**
43 * lpfc_mem_alloc: create and allocate all PCI and memory pools
44 * @phba: HBA to allocate pools for
45 *
46 * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
47 * lpfc_mbuf_pool, lpfc_hbq_pool. Creates and allocates kmalloc-backed mempools
48 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
49 *
50 * Notes: Not interrupt-safe. Must be called with no locks held. If any
51 * allocation fails, frees all successfully allocated memory before returning.
52 *
53 * Returns:
54 * 0 on success
55 * -ENOMEM on failure (if any memory allocations fail)
56 **/
dea31012005-04-17 16:05:31 -050057int
58lpfc_mem_alloc(struct lpfc_hba * phba)
59{
60 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart92d7f7b2007-06-17 19:56:38 -050061 int longs;
dea31012005-04-17 16:05:31 -050062 int i;
63
64 phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
65 phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
66 if (!phba->lpfc_scsi_dma_buf_pool)
67 goto fail;
68
69 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
70 LPFC_BPL_SIZE, 8,0);
71 if (!phba->lpfc_mbuf_pool)
72 goto fail_free_dma_buf_pool;
73
74 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
75 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +010076 if (!pool->elements)
77 goto fail_free_lpfc_mbuf_pool;
78
dea31012005-04-17 16:05:31 -050079 pool->max_count = 0;
80 pool->current_count = 0;
81 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
82 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
83 GFP_KERNEL, &pool->elements[i].phys);
84 if (!pool->elements[i].virt)
85 goto fail_free_mbuf_pool;
86 pool->max_count++;
87 pool->current_count++;
88 }
89
Matthew Dobson0eaae62a2006-03-26 01:37:47 -080090 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
91 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -050092 if (!phba->mbox_mem_pool)
93 goto fail_free_mbuf_pool;
94
Matthew Dobson0eaae62a2006-03-26 01:37:47 -080095 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
96 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -050097 if (!phba->nlp_mem_pool)
98 goto fail_free_mbox_pool;
99
James Smarted957682007-06-17 19:56:37 -0500100 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev,
101 LPFC_BPL_SIZE, 8, 0);
102 if (!phba->lpfc_hbq_pool)
103 goto fail_free_nlp_mem_pool;
104
James Smart858c9f62007-06-17 19:56:39 -0500105 /* vpi zero is reserved for the physical port so add 1 to max */
106 longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG;
James Smart92d7f7b2007-06-17 19:56:38 -0500107 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
108 if (!phba->vpi_bmask)
109 goto fail_free_hbq_pool;
110
dea31012005-04-17 16:05:31 -0500111 return 0;
112
James Smart92d7f7b2007-06-17 19:56:38 -0500113 fail_free_hbq_pool:
114 lpfc_sli_hbqbuf_free_all(phba);
James Smart09372822008-01-11 01:52:54 -0500115 pci_pool_destroy(phba->lpfc_hbq_pool);
James Smarted957682007-06-17 19:56:37 -0500116 fail_free_nlp_mem_pool:
117 mempool_destroy(phba->nlp_mem_pool);
118 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500119 fail_free_mbox_pool:
120 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500121 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500122 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100123 while (i--)
dea31012005-04-17 16:05:31 -0500124 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
125 pool->elements[i].phys);
126 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100127 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500128 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500129 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500130 fail_free_dma_buf_pool:
131 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500132 phba->lpfc_scsi_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500133 fail:
134 return -ENOMEM;
135}
136
James Smarte59058c2008-08-24 21:49:00 -0400137/**
138 * lpfc_mem_free: Frees all PCI and memory allocated by lpfc_mem_alloc
139 * @phba: HBA to free memory for
140 *
141 * Description: Frees PCI pools lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool,
142 * lpfc_hbq_pool. Frees kmalloc-backed mempools for LPFC_MBOXQ_t and
143 * lpfc_nodelist. Also frees the VPI bitmask.
144 *
145 * Returns: None
146 **/
dea31012005-04-17 16:05:31 -0500147void
148lpfc_mem_free(struct lpfc_hba * phba)
149{
150 struct lpfc_sli *psli = &phba->sli;
151 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
152 LPFC_MBOXQ_t *mbox, *next_mbox;
153 struct lpfc_dmabuf *mp;
154 int i;
155
James Smart92d7f7b2007-06-17 19:56:38 -0500156 kfree(phba->vpi_bmask);
James Smarted957682007-06-17 19:56:37 -0500157 lpfc_sli_hbqbuf_free_all(phba);
158
dea31012005-04-17 16:05:31 -0500159 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
160 mp = (struct lpfc_dmabuf *) (mbox->context1);
161 if (mp) {
162 lpfc_mbuf_free(phba, mp->virt, mp->phys);
163 kfree(mp);
164 }
165 list_del(&mbox->list);
166 mempool_free(mbox, phba->mbox_mem_pool);
167 }
James Smart92d7f7b2007-06-17 19:56:38 -0500168 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
169 mp = (struct lpfc_dmabuf *) (mbox->context1);
170 if (mp) {
171 lpfc_mbuf_free(phba, mp->virt, mp->phys);
172 kfree(mp);
173 }
174 list_del(&mbox->list);
175 mempool_free(mbox, phba->mbox_mem_pool);
176 }
dea31012005-04-17 16:05:31 -0500177
178 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
179 if (psli->mbox_active) {
180 mbox = psli->mbox_active;
181 mp = (struct lpfc_dmabuf *) (mbox->context1);
182 if (mp) {
183 lpfc_mbuf_free(phba, mp->virt, mp->phys);
184 kfree(mp);
185 }
186 mempool_free(mbox, phba->mbox_mem_pool);
187 psli->mbox_active = NULL;
188 }
189
190 for (i = 0; i < pool->current_count; i++)
191 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
192 pool->elements[i].phys);
193 kfree(pool->elements);
James Smart2e0fef82007-06-17 19:56:36 -0500194
James Smarted957682007-06-17 19:56:37 -0500195 pci_pool_destroy(phba->lpfc_hbq_pool);
dea31012005-04-17 16:05:31 -0500196 mempool_destroy(phba->nlp_mem_pool);
197 mempool_destroy(phba->mbox_mem_pool);
198
199 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
200 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart9f49d3b2006-07-06 15:49:34 -0400201
James Smarted957682007-06-17 19:56:37 -0500202 phba->lpfc_hbq_pool = NULL;
James Smart2e0fef82007-06-17 19:56:36 -0500203 phba->nlp_mem_pool = NULL;
204 phba->mbox_mem_pool = NULL;
205 phba->lpfc_scsi_dma_buf_pool = NULL;
206 phba->lpfc_mbuf_pool = NULL;
207
James Smarte59058c2008-08-24 21:49:00 -0400208 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400209 kfree(psli->iocbq_lookup);
210 psli->iocbq_lookup = NULL;
dea31012005-04-17 16:05:31 -0500211}
212
James Smarte59058c2008-08-24 21:49:00 -0400213/**
214 * lpfc_mbuf_alloc: Allocate an mbuf from the lpfc_mbuf_pool PCI pool
215 * @phba: HBA which owns the pool to allocate from
216 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
217 * @handle: used to return the DMA-mapped address of the mbuf
218 *
219 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
220 * Allocates from generic pci_pool_alloc function first and if that fails and
221 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
222 * HBA's pool.
223 *
224 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
225 * phba->hbalock.
226 *
227 * Returns:
228 * pointer to the allocated mbuf on success
229 * NULL on failure
230 **/
dea31012005-04-17 16:05:31 -0500231void *
232lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
233{
234 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500235 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500236 void *ret;
237
238 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
239
James Smart2e0fef82007-06-17 19:56:36 -0500240 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500241 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500242 pool->current_count--;
243 ret = pool->elements[pool->current_count].virt;
244 *handle = pool->elements[pool->current_count].phys;
245 }
James Smart2e0fef82007-06-17 19:56:36 -0500246 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500247 return ret;
248}
249
James Smarte59058c2008-08-24 21:49:00 -0400250/**
251 * __lpfc_mem_free: Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
252 * @phba: HBA which owns the pool to return to
253 * @virt: mbuf to free
254 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
255 *
256 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
257 * it is below its max_count, frees the mbuf otherwise.
258 *
259 * Notes: Must be called with phba->hbalock held to synchronize access to
260 * lpfc_mbuf_safety_pool.
261 *
262 * Returns: None
263 **/
dea31012005-04-17 16:05:31 -0500264void
James Smart2e0fef82007-06-17 19:56:36 -0500265__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500266{
267 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
268
269 if (pool->current_count < pool->max_count) {
270 pool->elements[pool->current_count].virt = virt;
271 pool->elements[pool->current_count].phys = dma;
272 pool->current_count++;
273 } else {
274 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
275 }
276 return;
277}
James Smart2e0fef82007-06-17 19:56:36 -0500278
James Smarte59058c2008-08-24 21:49:00 -0400279/**
280 * lpfc_mem_free: Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
281 * @phba: HBA which owns the pool to return to
282 * @virt: mbuf to free
283 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
284 *
285 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
286 * it is below its max_count, frees the mbuf otherwise.
287 *
288 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
289 *
290 * Returns: None
291 **/
James Smart2e0fef82007-06-17 19:56:36 -0500292void
James Smarte59058c2008-08-24 21:49:00 -0400293
James Smart2e0fef82007-06-17 19:56:36 -0500294lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
295{
296 unsigned long iflags;
297
298 spin_lock_irqsave(&phba->hbalock, iflags);
299 __lpfc_mbuf_free(phba, virt, dma);
300 spin_unlock_irqrestore(&phba->hbalock, iflags);
301 return;
302}
James Smarted957682007-06-17 19:56:37 -0500303
James Smarte59058c2008-08-24 21:49:00 -0400304/**
305 * lpfc_els_hbq_alloc: Allocate an HBQ buffer
306 * @phba: HBA to allocate HBQ buffer for
307 *
308 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hbq_pool PCI
309 * pool along a non-DMA-mapped container for it.
310 *
311 * Notes: Not interrupt-safe. Must be called with no locks held.
312 *
313 * Returns:
314 * pointer to HBQ on success
315 * NULL on failure
316 **/
James Smart51ef4c22007-08-02 11:10:31 -0400317struct hbq_dmabuf *
318lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500319{
James Smart51ef4c22007-08-02 11:10:31 -0400320 struct hbq_dmabuf *hbqbp;
321
322 hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
323 if (!hbqbp)
324 return NULL;
325
326 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
327 &hbqbp->dbuf.phys);
328 if (!hbqbp->dbuf.virt) {
329 kfree(hbqbp);
330 return NULL;
331 }
332 hbqbp->size = LPFC_BPL_SIZE;
333 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500334}
335
James Smarte59058c2008-08-24 21:49:00 -0400336/**
337 * lpfc_mem_hbq_free: Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
338 * @phba: HBA buffer was allocated for
339 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
340 *
341 * Description: Frees both the container and the DMA-mapped buffer returned by
342 * lpfc_els_hbq_alloc.
343 *
344 * Notes: Can be called with or without locks held.
345 *
346 * Returns: None
347 **/
James Smarted957682007-06-17 19:56:37 -0500348void
James Smart51ef4c22007-08-02 11:10:31 -0400349lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500350{
James Smart51ef4c22007-08-02 11:10:31 -0400351 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
352 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500353 return;
354}
355
James Smarte59058c2008-08-24 21:49:00 -0400356/**
357 * lpfc_in_buf_free: Free a DMA buffer
358 * @phba: HBA buffer is associated with
359 * @mp: Buffer to free
360 *
361 * Description: Frees the given DMA buffer in the appropriate way given if the
362 * HBA is running in SLI3 mode with HBQs enabled.
363 *
364 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
365 *
366 * Returns: None
367 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500368void
369lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
370{
371 struct hbq_dmabuf *hbq_entry;
James Smart3163f722008-02-08 18:50:25 -0500372 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500373
James Smart7f5f3d02008-02-08 18:50:14 -0500374 if (!mp)
375 return;
376
James Smart92d7f7b2007-06-17 19:56:38 -0500377 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart3163f722008-02-08 18:50:25 -0500378 /* Check whether HBQ is still in use */
379 spin_lock_irqsave(&phba->hbalock, flags);
380 if (!phba->hbq_in_use) {
381 spin_unlock_irqrestore(&phba->hbalock, flags);
382 return;
383 }
James Smart92d7f7b2007-06-17 19:56:38 -0500384 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
James Smart3163f722008-02-08 18:50:25 -0500385 list_del(&hbq_entry->dbuf.list);
James Smart92d7f7b2007-06-17 19:56:38 -0500386 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400387 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
388 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500389 } else {
390 lpfc_sli_free_hbq(phba, hbq_entry);
391 }
James Smart3163f722008-02-08 18:50:25 -0500392 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500393 } else {
394 lpfc_mbuf_free(phba, mp->virt, mp->phys);
395 kfree(mp);
396 }
397 return;
398}