This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_mem.c
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  *******************************************************************/
20
21 /*
22  * $Id: lpfc_mem.c 1.69 2004/09/28 07:54:24EDT sf_support Exp  $
23  */
24
25 #include <linux/mempool.h>
26 #include <linux/pci.h>
27 #include <linux/slab.h>
28 #include <scsi/scsi_device.h>
29
30 #include "lpfc_sli.h"
31 #include "lpfc_disc.h"
32 #include "lpfc_scsi.h"
33 #include "lpfc.h"
34 #include "lpfc_crtn.h"
35 #include "lpfc_mem.h"
36
37 static void *
38 lpfc_pool_kmalloc(int gfp_flags, void *data)
39 {
40         return kmalloc((unsigned long)data, gfp_flags);
41 }
42
43 static void
44 lpfc_pool_kfree(void *obj, void *data)
45 {
46         kfree(obj);
47 }
48
49 int
50 lpfc_mem_alloc(struct lpfc_hba * phba)
51 {
52         struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
53         int i;
54
55         phba->lpfc_scsi_dma_ext_pool = pci_pool_create("lpfc_scsi_dma_ext_pool",
56                                 phba->pcidev, LPFC_SCSI_DMA_EXT_SIZE, 8, 0);
57         if (!phba->lpfc_scsi_dma_ext_pool)
58                 goto fail;
59
60         phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
61                                                         LPFC_BPL_SIZE, 8,0);
62         if (!phba->lpfc_mbuf_pool)
63                 goto fail_free_dma_ext_pool;
64
65         pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
66                                          LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
67         pool->max_count = 0;
68         pool->current_count = 0;
69         for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
70                 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
71                                        GFP_KERNEL, &pool->elements[i].phys);
72                 if (!pool->elements[i].virt)
73                         goto fail_free_mbuf_pool;
74                 pool->max_count++;
75                 pool->current_count++;
76         }
77
78         phba->iocb_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
79                         lpfc_pool_kmalloc, lpfc_pool_kfree,
80                         (void *)(unsigned long)sizeof(struct lpfc_iocbq));
81         if (!phba->iocb_mem_pool)
82                 goto fail_free_mbuf_pool;
83
84         phba->scsibuf_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
85                         lpfc_pool_kmalloc, lpfc_pool_kfree,
86                         (void *)(unsigned long)sizeof(struct lpfc_scsi_buf));
87         if (!phba->scsibuf_mem_pool)
88                 goto fail_free_iocb_pool;
89
90         phba->mbox_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
91                                 lpfc_pool_kmalloc, lpfc_pool_kfree,
92                                 (void *)(unsigned long)sizeof(LPFC_MBOXQ_t));
93         if (!phba->mbox_mem_pool)
94                 goto fail_free_scsibuf_pool;
95
96         phba->nlp_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
97                         lpfc_pool_kmalloc, lpfc_pool_kfree,
98                         (void *)(unsigned long)sizeof(struct lpfc_nodelist));
99         if (!phba->nlp_mem_pool)
100                 goto fail_free_mbox_pool;
101
102         phba->bind_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
103                         lpfc_pool_kmalloc, lpfc_pool_kfree,
104                         (void *)(unsigned long)sizeof(struct lpfc_bindlist));
105         if (!phba->bind_mem_pool)
106                 goto fail_free_nlp_pool;
107
108         return 0;
109
110  fail_free_nlp_pool:
111         mempool_destroy(phba->nlp_mem_pool);
112  fail_free_mbox_pool:
113         mempool_destroy(phba->mbox_mem_pool);
114  fail_free_scsibuf_pool:
115         mempool_destroy(phba->scsibuf_mem_pool);
116  fail_free_iocb_pool:
117         mempool_destroy(phba->iocb_mem_pool);
118  fail_free_mbuf_pool:
119         while (--i)
120                 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
121                                                  pool->elements[i].phys);
122         kfree(pool->elements);
123         pci_pool_destroy(phba->lpfc_mbuf_pool);
124  fail_free_dma_ext_pool:
125         pci_pool_destroy(phba->lpfc_scsi_dma_ext_pool);
126  fail:
127         return -ENOMEM;
128 }
129
130 void
131 lpfc_mem_free(struct lpfc_hba * phba)
132 {
133         struct lpfc_sli *psli = &phba->sli;
134         struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
135         LPFC_MBOXQ_t *mbox, *next_mbox;
136         int i;
137
138         list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
139                 list_del(&mbox->list);
140                 mempool_free(mbox, phba->mbox_mem_pool);
141         }
142
143         psli->sliinit.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
144         if (psli->mbox_active) {
145                 mempool_free(psli->mbox_active, phba->mbox_mem_pool);
146                 psli->mbox_active = NULL;
147         }
148
149         for (i = 0; i < pool->current_count; i++)
150                 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
151                                                  pool->elements[i].phys);
152         kfree(pool->elements);
153         mempool_destroy(phba->bind_mem_pool);
154         mempool_destroy(phba->nlp_mem_pool);
155         mempool_destroy(phba->mbox_mem_pool);
156         mempool_destroy(phba->scsibuf_mem_pool);
157         mempool_destroy(phba->iocb_mem_pool);
158
159         pci_pool_destroy(phba->lpfc_scsi_dma_ext_pool);
160         pci_pool_destroy(phba->lpfc_mbuf_pool);
161 }
162
163 void *
164 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
165 {
166         struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
167         void *ret;
168
169         ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_ATOMIC, handle);
170
171         if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) {
172                 pool->current_count--;
173                 ret = pool->elements[pool->current_count].virt;
174                 *handle = pool->elements[pool->current_count].phys;
175         }
176         return ret;
177 }
178
179 void
180 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
181 {
182         struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
183
184         if (pool->current_count < pool->max_count) {
185                 pool->elements[pool->current_count].virt = virt;
186                 pool->elements[pool->current_count].phys = dma;
187                 pool->current_count++;
188         } else {
189                 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
190         }
191         return;
192 }