1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2005 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>
31 #include "lpfc_disc.h"
32 #include "lpfc_scsi.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 */
40 lpfc_pool_kmalloc(unsigned int gfp_flags, void *data)
42 return kmalloc((unsigned long)data, gfp_flags);
46 lpfc_pool_kfree(void *obj, void *data)
52 lpfc_mem_alloc(struct lpfc_hba * phba)
54 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
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)
62 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
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);
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;
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;
95 mempool_destroy(phba->mbox_mem_pool);
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);
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;
117 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
118 mp = (struct lpfc_dmabuf *) (mbox->context1);
120 lpfc_mbuf_free(phba, mp->virt, mp->phys);
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);
132 lpfc_mbuf_free(phba, mp->virt, mp->phys);
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);
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;
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;
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++;
176 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);