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. *
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. *
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.69 2004/09/28 07:54:24EDT sf_support Exp $
25 #include <linux/mempool.h>
26 #include <linux/pci.h>
27 #include <linux/slab.h>
28 #include <scsi/scsi_device.h>
31 #include "lpfc_disc.h"
32 #include "lpfc_scsi.h"
34 #include "lpfc_crtn.h"
38 lpfc_pool_kmalloc(int gfp_flags, void *data)
40 return kmalloc((unsigned long)data, gfp_flags);
44 lpfc_pool_kfree(void *obj, void *data)
50 lpfc_mem_alloc(struct lpfc_hba * phba)
52 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
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)
60 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
62 if (!phba->lpfc_mbuf_pool)
63 goto fail_free_dma_ext_pool;
65 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
66 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
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;
75 pool->current_count++;
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;
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;
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;
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;
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;
111 mempool_destroy(phba->nlp_mem_pool);
113 mempool_destroy(phba->mbox_mem_pool);
114 fail_free_scsibuf_pool:
115 mempool_destroy(phba->scsibuf_mem_pool);
117 mempool_destroy(phba->iocb_mem_pool);
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);
131 lpfc_mem_free(struct lpfc_hba * phba)
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;
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);
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;
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);
159 pci_pool_destroy(phba->lpfc_scsi_dma_ext_pool);
160 pci_pool_destroy(phba->lpfc_mbuf_pool);
164 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
166 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
169 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_ATOMIC, handle);
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;
180 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
182 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
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++;
189 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);