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. *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
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>
33 #include "lpfc_disc.h"
34 #include "lpfc_scsi.h"
36 #include "lpfc_crtn.h"
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 */
44 lpfc_mem_alloc(struct lpfc_hba
* phba
)
46 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
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
)
55 phba
->lpfc_mbuf_pool
= pci_pool_create("lpfc_mbuf_pool", phba
->pcidev
,
57 if (!phba
->lpfc_mbuf_pool
)
58 goto fail_free_dma_buf_pool
;
60 pool
->elements
= kmalloc(sizeof(struct lpfc_dmabuf
) *
61 LPFC_MBUF_POOL_SIZE
, GFP_KERNEL
);
63 goto fail_free_lpfc_mbuf_pool
;
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
;
73 pool
->current_count
++;
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
;
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
;
86 phba
->lpfc_hbq_pool
= pci_pool_create("lpfc_hbq_pool",phba
->pcidev
,
88 if (!phba
->lpfc_hbq_pool
)
89 goto fail_free_nlp_mem_pool
;
91 /* vpi zero is reserved for the physical port so add 1 to max */
92 longs
= ((phba
->max_vpi
+ 1) + BITS_PER_LONG
- 1) / BITS_PER_LONG
;
93 phba
->vpi_bmask
= kzalloc(longs
* sizeof(unsigned long), GFP_KERNEL
);
95 goto fail_free_hbq_pool
;
100 lpfc_sli_hbqbuf_free_all(phba
);
101 pci_pool_destroy(phba
->lpfc_hbq_pool
);
102 fail_free_nlp_mem_pool
:
103 mempool_destroy(phba
->nlp_mem_pool
);
104 phba
->nlp_mem_pool
= NULL
;
106 mempool_destroy(phba
->mbox_mem_pool
);
107 phba
->mbox_mem_pool
= NULL
;
110 pci_pool_free(phba
->lpfc_mbuf_pool
, pool
->elements
[i
].virt
,
111 pool
->elements
[i
].phys
);
112 kfree(pool
->elements
);
113 fail_free_lpfc_mbuf_pool
:
114 pci_pool_destroy(phba
->lpfc_mbuf_pool
);
115 phba
->lpfc_mbuf_pool
= NULL
;
116 fail_free_dma_buf_pool
:
117 pci_pool_destroy(phba
->lpfc_scsi_dma_buf_pool
);
118 phba
->lpfc_scsi_dma_buf_pool
= NULL
;
124 lpfc_mem_free(struct lpfc_hba
* phba
)
126 struct lpfc_sli
*psli
= &phba
->sli
;
127 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
128 LPFC_MBOXQ_t
*mbox
, *next_mbox
;
129 struct lpfc_dmabuf
*mp
;
132 kfree(phba
->vpi_bmask
);
133 lpfc_sli_hbqbuf_free_all(phba
);
135 list_for_each_entry_safe(mbox
, next_mbox
, &psli
->mboxq
, list
) {
136 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
138 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
141 list_del(&mbox
->list
);
142 mempool_free(mbox
, phba
->mbox_mem_pool
);
144 list_for_each_entry_safe(mbox
, next_mbox
, &psli
->mboxq_cmpl
, list
) {
145 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
147 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
150 list_del(&mbox
->list
);
151 mempool_free(mbox
, phba
->mbox_mem_pool
);
154 psli
->sli_flag
&= ~LPFC_SLI_MBOX_ACTIVE
;
155 if (psli
->mbox_active
) {
156 mbox
= psli
->mbox_active
;
157 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
159 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
162 mempool_free(mbox
, phba
->mbox_mem_pool
);
163 psli
->mbox_active
= NULL
;
166 for (i
= 0; i
< pool
->current_count
; i
++)
167 pci_pool_free(phba
->lpfc_mbuf_pool
, pool
->elements
[i
].virt
,
168 pool
->elements
[i
].phys
);
169 kfree(pool
->elements
);
171 pci_pool_destroy(phba
->lpfc_hbq_pool
);
172 mempool_destroy(phba
->nlp_mem_pool
);
173 mempool_destroy(phba
->mbox_mem_pool
);
175 pci_pool_destroy(phba
->lpfc_scsi_dma_buf_pool
);
176 pci_pool_destroy(phba
->lpfc_mbuf_pool
);
178 phba
->lpfc_hbq_pool
= NULL
;
179 phba
->nlp_mem_pool
= NULL
;
180 phba
->mbox_mem_pool
= NULL
;
181 phba
->lpfc_scsi_dma_buf_pool
= NULL
;
182 phba
->lpfc_mbuf_pool
= NULL
;
184 /* Free the iocb lookup array */
185 kfree(psli
->iocbq_lookup
);
186 psli
->iocbq_lookup
= NULL
;
191 lpfc_mbuf_alloc(struct lpfc_hba
*phba
, int mem_flags
, dma_addr_t
*handle
)
193 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
194 unsigned long iflags
;
197 ret
= pci_pool_alloc(phba
->lpfc_mbuf_pool
, GFP_KERNEL
, handle
);
199 spin_lock_irqsave(&phba
->hbalock
, iflags
);
200 if (!ret
&& (mem_flags
& MEM_PRI
) && pool
->current_count
) {
201 pool
->current_count
--;
202 ret
= pool
->elements
[pool
->current_count
].virt
;
203 *handle
= pool
->elements
[pool
->current_count
].phys
;
205 spin_unlock_irqrestore(&phba
->hbalock
, iflags
);
210 __lpfc_mbuf_free(struct lpfc_hba
* phba
, void *virt
, dma_addr_t dma
)
212 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
214 if (pool
->current_count
< pool
->max_count
) {
215 pool
->elements
[pool
->current_count
].virt
= virt
;
216 pool
->elements
[pool
->current_count
].phys
= dma
;
217 pool
->current_count
++;
219 pci_pool_free(phba
->lpfc_mbuf_pool
, virt
, dma
);
225 lpfc_mbuf_free(struct lpfc_hba
* phba
, void *virt
, dma_addr_t dma
)
227 unsigned long iflags
;
229 spin_lock_irqsave(&phba
->hbalock
, iflags
);
230 __lpfc_mbuf_free(phba
, virt
, dma
);
231 spin_unlock_irqrestore(&phba
->hbalock
, iflags
);
236 lpfc_els_hbq_alloc(struct lpfc_hba
*phba
)
238 struct hbq_dmabuf
*hbqbp
;
240 hbqbp
= kmalloc(sizeof(struct hbq_dmabuf
), GFP_KERNEL
);
244 hbqbp
->dbuf
.virt
= pci_pool_alloc(phba
->lpfc_hbq_pool
, GFP_KERNEL
,
246 if (!hbqbp
->dbuf
.virt
) {
250 hbqbp
->size
= LPFC_BPL_SIZE
;
255 lpfc_els_hbq_free(struct lpfc_hba
*phba
, struct hbq_dmabuf
*hbqbp
)
257 pci_pool_free(phba
->lpfc_hbq_pool
, hbqbp
->dbuf
.virt
, hbqbp
->dbuf
.phys
);
262 /* This is ONLY called for the LPFC_ELS_HBQ */
264 lpfc_in_buf_free(struct lpfc_hba
*phba
, struct lpfc_dmabuf
*mp
)
266 struct hbq_dmabuf
*hbq_entry
;
272 if (phba
->sli3_options
& LPFC_SLI3_HBQ_ENABLED
) {
273 /* Check whether HBQ is still in use */
274 spin_lock_irqsave(&phba
->hbalock
, flags
);
275 if (!phba
->hbq_in_use
) {
276 spin_unlock_irqrestore(&phba
->hbalock
, flags
);
279 hbq_entry
= container_of(mp
, struct hbq_dmabuf
, dbuf
);
280 list_del(&hbq_entry
->dbuf
.list
);
281 if (hbq_entry
->tag
== -1) {
282 (phba
->hbqs
[LPFC_ELS_HBQ
].hbq_free_buffer
)
285 lpfc_sli_free_hbq(phba
, hbq_entry
);
287 spin_unlock_irqrestore(&phba
->hbalock
, flags
);
289 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);