[SCSI] lpfc: NPIV: add NPIV support on top of SLI-3
[linux-2.6] / drivers / scsi / lpfc / lpfc_mem.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2006 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  *******************************************************************/
21
22 #include <linux/mempool.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_transport_fc.h>
28
29 #include <scsi/scsi.h>
30
31 #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
41
42
43 int
44 lpfc_mem_alloc(struct lpfc_hba * phba)
45 {
46         struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
47         int longs;
48         int i;
49
50         phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
51                                 phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
52         if (!phba->lpfc_scsi_dma_buf_pool)
53                 goto fail;
54
55         phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
56                                                         LPFC_BPL_SIZE, 8,0);
57         if (!phba->lpfc_mbuf_pool)
58                 goto fail_free_dma_buf_pool;
59
60         pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
61                                          LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
62         if (!pool->elements)
63                 goto fail_free_lpfc_mbuf_pool;
64
65         pool->max_count = 0;
66         pool->current_count = 0;
67         for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
68                 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
69                                        GFP_KERNEL, &pool->elements[i].phys);
70                 if (!pool->elements[i].virt)
71                         goto fail_free_mbuf_pool;
72                 pool->max_count++;
73                 pool->current_count++;
74         }
75
76         phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
77                                                          sizeof(LPFC_MBOXQ_t));
78         if (!phba->mbox_mem_pool)
79                 goto fail_free_mbuf_pool;
80
81         phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
82                                                 sizeof(struct lpfc_nodelist));
83         if (!phba->nlp_mem_pool)
84                 goto fail_free_mbox_pool;
85
86         phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev,
87                                               LPFC_BPL_SIZE, 8, 0);
88         if (!phba->lpfc_hbq_pool)
89                 goto fail_free_nlp_mem_pool;
90
91         longs = (phba->max_vpi + BITS_PER_LONG - 1) / BITS_PER_LONG;
92         phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
93         if (!phba->vpi_bmask)
94                 goto fail_free_hbq_pool;
95
96         return 0;
97
98  fail_free_hbq_pool:
99         lpfc_sli_hbqbuf_free_all(phba);
100  fail_free_nlp_mem_pool:
101         mempool_destroy(phba->nlp_mem_pool);
102         phba->nlp_mem_pool = NULL;
103  fail_free_mbox_pool:
104         mempool_destroy(phba->mbox_mem_pool);
105         phba->mbox_mem_pool = NULL;
106  fail_free_mbuf_pool:
107         while (i--)
108                 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
109                                                  pool->elements[i].phys);
110         kfree(pool->elements);
111  fail_free_lpfc_mbuf_pool:
112         pci_pool_destroy(phba->lpfc_mbuf_pool);
113         phba->lpfc_mbuf_pool = NULL;
114  fail_free_dma_buf_pool:
115         pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
116         phba->lpfc_scsi_dma_buf_pool = NULL;
117  fail:
118         return -ENOMEM;
119 }
120
121 void
122 lpfc_mem_free(struct lpfc_hba * phba)
123 {
124         struct lpfc_sli *psli = &phba->sli;
125         struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
126         LPFC_MBOXQ_t *mbox, *next_mbox;
127         struct lpfc_dmabuf   *mp;
128         int i;
129
130         kfree(phba->vpi_bmask);
131         lpfc_sli_hbqbuf_free_all(phba);
132
133         list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
134                 mp = (struct lpfc_dmabuf *) (mbox->context1);
135                 if (mp) {
136                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
137                         kfree(mp);
138                 }
139                 list_del(&mbox->list);
140                 mempool_free(mbox, phba->mbox_mem_pool);
141         }
142         list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
143                 mp = (struct lpfc_dmabuf *) (mbox->context1);
144                 if (mp) {
145                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
146                         kfree(mp);
147                 }
148                 list_del(&mbox->list);
149                 mempool_free(mbox, phba->mbox_mem_pool);
150         }
151
152         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
153         if (psli->mbox_active) {
154                 mbox = psli->mbox_active;
155                 mp = (struct lpfc_dmabuf *) (mbox->context1);
156                 if (mp) {
157                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
158                         kfree(mp);
159                 }
160                 mempool_free(mbox, phba->mbox_mem_pool);
161                 psli->mbox_active = NULL;
162         }
163
164         for (i = 0; i < pool->current_count; i++)
165                 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
166                                                  pool->elements[i].phys);
167         kfree(pool->elements);
168
169         pci_pool_destroy(phba->lpfc_hbq_pool);
170         mempool_destroy(phba->nlp_mem_pool);
171         mempool_destroy(phba->mbox_mem_pool);
172
173         pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
174         pci_pool_destroy(phba->lpfc_mbuf_pool);
175
176         phba->lpfc_hbq_pool = NULL;
177         phba->nlp_mem_pool = NULL;
178         phba->mbox_mem_pool = NULL;
179         phba->lpfc_scsi_dma_buf_pool = NULL;
180         phba->lpfc_mbuf_pool = NULL;
181
182                                 /* Free the iocb lookup array */
183         kfree(psli->iocbq_lookup);
184         psli->iocbq_lookup = NULL;
185
186 }
187
188 void *
189 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
190 {
191         struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
192         unsigned long iflags;
193         void *ret;
194
195         ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
196
197         spin_lock_irqsave(&phba->hbalock, iflags);
198         if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
199                 pool->current_count--;
200                 ret = pool->elements[pool->current_count].virt;
201                 *handle = pool->elements[pool->current_count].phys;
202         }
203         spin_unlock_irqrestore(&phba->hbalock, iflags);
204         return ret;
205 }
206
207 void
208 __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
209 {
210         struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
211
212         if (pool->current_count < pool->max_count) {
213                 pool->elements[pool->current_count].virt = virt;
214                 pool->elements[pool->current_count].phys = dma;
215                 pool->current_count++;
216         } else {
217                 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
218         }
219         return;
220 }
221
222 void
223 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
224 {
225         unsigned long iflags;
226
227         spin_lock_irqsave(&phba->hbalock, iflags);
228         __lpfc_mbuf_free(phba, virt, dma);
229         spin_unlock_irqrestore(&phba->hbalock, iflags);
230         return;
231 }
232
233 void *
234 lpfc_hbq_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
235 {
236         void *ret;
237         ret = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_ATOMIC, handle);
238         return ret;
239 }
240
241 void
242 lpfc_hbq_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma)
243 {
244         pci_pool_free(phba->lpfc_hbq_pool, virt, dma);
245         return;
246 }
247
248 void
249 lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
250 {
251         struct hbq_dmabuf *hbq_entry;
252
253         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
254                 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
255                 if (hbq_entry->tag == -1) {
256                         lpfc_hbq_free(phba, hbq_entry->dbuf.virt,
257                                       hbq_entry->dbuf.phys);
258                         kfree(hbq_entry);
259                 } else {
260                         lpfc_sli_free_hbq(phba, hbq_entry);
261                 }
262         } else {
263                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
264                 kfree(mp);
265         }
266         return;
267 }
268