thinkpad-acpi: drop HKEY event 0x5010
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / lpfc / lpfc_mem.c
blobe198c917c13ecc55d91839d8ca64a8051ee7f097
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * 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. *
20 *******************************************************************/
22 #include <linux/mempool.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_transport_fc.h>
29 #include <scsi/scsi.h>
31 #include "lpfc_hw4.h"
32 #include "lpfc_hw.h"
33 #include "lpfc_sli.h"
34 #include "lpfc_sli4.h"
35 #include "lpfc_nl.h"
36 #include "lpfc_disc.h"
37 #include "lpfc_scsi.h"
38 #include "lpfc.h"
39 #include "lpfc_crtn.h"
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 */
45 /**
46 * lpfc_mem_alloc - create and allocate all PCI and memory pools
47 * @phba: HBA to allocate pools for
49 * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
50 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
51 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
53 * Notes: Not interrupt-safe. Must be called with no locks held. If any
54 * allocation fails, frees all successfully allocated memory before returning.
56 * Returns:
57 * 0 on success
58 * -ENOMEM on failure (if any memory allocations fail)
59 **/
60 int
61 lpfc_mem_alloc(struct lpfc_hba *phba, int align)
63 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
64 int longs;
65 int i;
67 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);
79 if (!phba->lpfc_scsi_dma_buf_pool)
80 goto fail;
82 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
83 LPFC_BPL_SIZE,
84 align, 0);
85 if (!phba->lpfc_mbuf_pool)
86 goto fail_free_dma_buf_pool;
88 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
89 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
90 if (!pool->elements)
91 goto fail_free_lpfc_mbuf_pool;
93 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++;
104 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
105 sizeof(LPFC_MBOXQ_t));
106 if (!phba->mbox_mem_pool)
107 goto fail_free_mbuf_pool;
109 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
110 sizeof(struct lpfc_nodelist));
111 if (!phba->nlp_mem_pool)
112 goto fail_free_mbox_pool;
113 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)
117 goto fail_free_nlp_mem_pool;
118 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;
124 /* 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;
126 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
127 if (!phba->vpi_bmask)
128 goto fail_free_dbq_pool;
130 return 0;
132 fail_free_dbq_pool:
133 pci_pool_destroy(phba->lpfc_drb_pool);
134 phba->lpfc_drb_pool = NULL;
135 fail_free_hbq_pool:
136 pci_pool_destroy(phba->lpfc_hrb_pool);
137 phba->lpfc_hrb_pool = NULL;
138 fail_free_nlp_mem_pool:
139 mempool_destroy(phba->nlp_mem_pool);
140 phba->nlp_mem_pool = NULL;
141 fail_free_mbox_pool:
142 mempool_destroy(phba->mbox_mem_pool);
143 phba->mbox_mem_pool = NULL;
144 fail_free_mbuf_pool:
145 while (i--)
146 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
147 pool->elements[i].phys);
148 kfree(pool->elements);
149 fail_free_lpfc_mbuf_pool:
150 pci_pool_destroy(phba->lpfc_mbuf_pool);
151 phba->lpfc_mbuf_pool = NULL;
152 fail_free_dma_buf_pool:
153 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
154 phba->lpfc_scsi_dma_buf_pool = NULL;
155 fail:
156 return -ENOMEM;
160 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
161 * @phba: HBA to free memory for
163 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
164 * routine is a the counterpart of lpfc_mem_alloc.
166 * Returns: None
168 void
169 lpfc_mem_free(struct lpfc_hba *phba)
171 int i;
172 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
174 /* Free VPI bitmask memory */
175 kfree(phba->vpi_bmask);
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;
184 /* Free NLP memory pool */
185 mempool_destroy(phba->nlp_mem_pool);
186 phba->nlp_mem_pool = NULL;
188 /* Free mbox memory pool */
189 mempool_destroy(phba->mbox_mem_pool);
190 phba->mbox_mem_pool = NULL;
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);
198 pci_pool_destroy(phba->lpfc_mbuf_pool);
199 phba->lpfc_mbuf_pool = NULL;
201 /* Free DMA buffer memory pool */
202 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
203 phba->lpfc_scsi_dma_buf_pool = NULL;
205 return;
209 * lpfc_mem_free_all - Frees all PCI and driver memory
210 * @phba: HBA to free memory for
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.
217 * Returns: None
219 void
220 lpfc_mem_free_all(struct lpfc_hba *phba)
222 struct lpfc_sli *psli = &phba->sli;
223 LPFC_MBOXQ_t *mbox, *next_mbox;
224 struct lpfc_dmabuf *mp;
226 /* Free memory used in mailbox queue back to mailbox memory pool */
227 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);
233 list_del(&mbox->list);
234 mempool_free(mbox, phba->mbox_mem_pool);
236 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
237 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);
243 list_del(&mbox->list);
244 mempool_free(mbox, phba->mbox_mem_pool);
246 /* Free the active mailbox command back to the mailbox memory pool */
247 spin_lock_irq(&phba->hbalock);
248 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
249 spin_unlock_irq(&phba->hbalock);
250 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);
257 mempool_free(mbox, phba->mbox_mem_pool);
258 psli->mbox_active = NULL;
261 /* Free and destroy all the allocated memory pools */
262 lpfc_mem_free(phba);
264 /* Free the iocb lookup array */
265 kfree(psli->iocbq_lookup);
266 psli->iocbq_lookup = NULL;
268 return;
272 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
273 * @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
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.
282 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
283 * phba->hbalock.
285 * Returns:
286 * pointer to the allocated mbuf on success
287 * NULL on failure
289 void *
290 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
292 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
293 unsigned long iflags;
294 void *ret;
296 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
298 spin_lock_irqsave(&phba->hbalock, iflags);
299 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
300 pool->current_count--;
301 ret = pool->elements[pool->current_count].virt;
302 *handle = pool->elements[pool->current_count].phys;
304 spin_unlock_irqrestore(&phba->hbalock, iflags);
305 return ret;
309 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
310 * @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
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.
317 * Notes: Must be called with phba->hbalock held to synchronize access to
318 * lpfc_mbuf_safety_pool.
320 * Returns: None
322 void
323 __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
325 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
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);
334 return;
338 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
339 * @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
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.
346 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
348 * Returns: None
350 void
351 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
353 unsigned long iflags;
355 spin_lock_irqsave(&phba->hbalock, iflags);
356 __lpfc_mbuf_free(phba, virt, dma);
357 spin_unlock_irqrestore(&phba->hbalock, iflags);
358 return;
362 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
363 * @phba: HBA to allocate HBQ buffer for
365 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
366 * pool along a non-DMA-mapped container for it.
368 * Notes: Not interrupt-safe. Must be called with no locks held.
370 * Returns:
371 * pointer to HBQ on success
372 * NULL on failure
374 struct hbq_dmabuf *
375 lpfc_els_hbq_alloc(struct lpfc_hba *phba)
377 struct hbq_dmabuf *hbqbp;
379 hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
380 if (!hbqbp)
381 return NULL;
383 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
384 &hbqbp->dbuf.phys);
385 if (!hbqbp->dbuf.virt) {
386 kfree(hbqbp);
387 return NULL;
389 hbqbp->size = LPFC_BPL_SIZE;
390 return hbqbp;
394 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
395 * @phba: HBA buffer was allocated for
396 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
398 * Description: Frees both the container and the DMA-mapped buffer returned by
399 * lpfc_els_hbq_alloc.
401 * Notes: Can be called with or without locks held.
403 * Returns: None
405 void
406 lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
408 pci_pool_free(phba->lpfc_hrb_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
409 kfree(hbqbp);
410 return;
414 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
415 * @phba: HBA to allocate a receive buffer for
417 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
418 * pool along a non-DMA-mapped container for it.
420 * Notes: Not interrupt-safe. Must be called with no locks held.
422 * Returns:
423 * pointer to HBQ on success
424 * NULL on failure
426 struct hbq_dmabuf *
427 lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
429 struct hbq_dmabuf *dma_buf;
431 dma_buf = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
432 if (!dma_buf)
433 return NULL;
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;
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;
449 dma_buf->size = LPFC_BPL_SIZE;
450 return dma_buf;
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
458 * Description: Frees both the container and the DMA-mapped buffers returned by
459 * lpfc_sli4_rb_alloc.
461 * Notes: Can be called with or without locks held.
463 * Returns: None
465 void
466 lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
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;
475 * lpfc_in_buf_free - Free a DMA buffer
476 * @phba: HBA buffer is associated with
477 * @mp: Buffer to free
479 * Description: Frees the given DMA buffer in the appropriate way given if the
480 * HBA is running in SLI3 mode with HBQs enabled.
482 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
484 * Returns: None
486 void
487 lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
489 struct hbq_dmabuf *hbq_entry;
490 unsigned long flags;
492 if (!mp)
493 return;
495 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
496 /* 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;
502 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
503 list_del(&hbq_entry->dbuf.list);
504 if (hbq_entry->tag == -1) {
505 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
506 (phba, hbq_entry);
507 } else {
508 lpfc_sli_free_hbq(phba, hbq_entry);
510 spin_unlock_irqrestore(&phba->hbalock, flags);
511 } else {
512 lpfc_mbuf_free(phba, mp->virt, mp->phys);
513 kfree(mp);
515 return;