[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / drivers / scsi / lpfc / lpfc_mem.c
blob4397e1160712d7da8a5f4eb7c980ffe98f002815
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Enterprise Fibre Channel Host Bus Adapters. *
4 * Refer to the README file included with this package for *
5 * driver version and adapter support. *
6 * Copyright (C) 2004 Emulex Corporation. *
7 * www.emulex.com *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of the GNU General Public License *
11 * as published by the Free Software Foundation; either version 2 *
12 * of the License, or (at your option) any later version. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details, a copy of which *
18 * can be found in the file COPYING included with this package. *
19 *******************************************************************/
22 * $Id: lpfc_mem.c 1.79 2005/04/13 14:25:50EDT sf_support Exp $
25 #include <linux/mempool.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
29 #include "lpfc_hw.h"
30 #include "lpfc_sli.h"
31 #include "lpfc_disc.h"
32 #include "lpfc_scsi.h"
33 #include "lpfc.h"
34 #include "lpfc_crtn.h"
36 #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
37 #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
39 static void *
40 lpfc_pool_kmalloc(unsigned int gfp_flags, void *data)
42 return kmalloc((unsigned long)data, gfp_flags);
45 static void
46 lpfc_pool_kfree(void *obj, void *data)
48 kfree(obj);
51 int
52 lpfc_mem_alloc(struct lpfc_hba * phba)
54 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
55 int i;
57 phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
58 phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
59 if (!phba->lpfc_scsi_dma_buf_pool)
60 goto fail;
62 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
63 LPFC_BPL_SIZE, 8,0);
64 if (!phba->lpfc_mbuf_pool)
65 goto fail_free_dma_buf_pool;
67 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
68 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
69 pool->max_count = 0;
70 pool->current_count = 0;
71 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
72 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
73 GFP_KERNEL, &pool->elements[i].phys);
74 if (!pool->elements[i].virt)
75 goto fail_free_mbuf_pool;
76 pool->max_count++;
77 pool->current_count++;
80 phba->mbox_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
81 lpfc_pool_kmalloc, lpfc_pool_kfree,
82 (void *)(unsigned long)sizeof(LPFC_MBOXQ_t));
83 if (!phba->mbox_mem_pool)
84 goto fail_free_mbuf_pool;
86 phba->nlp_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
87 lpfc_pool_kmalloc, lpfc_pool_kfree,
88 (void *)(unsigned long)sizeof(struct lpfc_nodelist));
89 if (!phba->nlp_mem_pool)
90 goto fail_free_mbox_pool;
92 return 0;
94 fail_free_mbox_pool:
95 mempool_destroy(phba->mbox_mem_pool);
96 fail_free_mbuf_pool:
97 while (--i)
98 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
99 pool->elements[i].phys);
100 kfree(pool->elements);
101 pci_pool_destroy(phba->lpfc_mbuf_pool);
102 fail_free_dma_buf_pool:
103 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
104 fail:
105 return -ENOMEM;
108 void
109 lpfc_mem_free(struct lpfc_hba * phba)
111 struct lpfc_sli *psli = &phba->sli;
112 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
113 LPFC_MBOXQ_t *mbox, *next_mbox;
114 struct lpfc_dmabuf *mp;
115 int i;
117 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
118 mp = (struct lpfc_dmabuf *) (mbox->context1);
119 if (mp) {
120 lpfc_mbuf_free(phba, mp->virt, mp->phys);
121 kfree(mp);
123 list_del(&mbox->list);
124 mempool_free(mbox, phba->mbox_mem_pool);
127 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
128 if (psli->mbox_active) {
129 mbox = psli->mbox_active;
130 mp = (struct lpfc_dmabuf *) (mbox->context1);
131 if (mp) {
132 lpfc_mbuf_free(phba, mp->virt, mp->phys);
133 kfree(mp);
135 mempool_free(mbox, phba->mbox_mem_pool);
136 psli->mbox_active = NULL;
139 for (i = 0; i < pool->current_count; i++)
140 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
141 pool->elements[i].phys);
142 kfree(pool->elements);
143 mempool_destroy(phba->nlp_mem_pool);
144 mempool_destroy(phba->mbox_mem_pool);
146 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
147 pci_pool_destroy(phba->lpfc_mbuf_pool);
150 void *
151 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
153 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
154 void *ret;
156 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
158 if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) {
159 pool->current_count--;
160 ret = pool->elements[pool->current_count].virt;
161 *handle = pool->elements[pool->current_count].phys;
163 return ret;
166 void
167 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
169 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
171 if (pool->current_count < pool->max_count) {
172 pool->elements[pool->current_count].virt = virt;
173 pool->elements[pool->current_count].phys = dma;
174 pool->current_count++;
175 } else {
176 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
178 return;