[Fix bug# 771] Too many (8) bytes recieved when using AES/hwcrypto
[linux-2.6/sactl.git] / drivers / scsi / lpfc / lpfc_mem.c
blob352df47bcaca1026d7090e3cd5d1b4d305ca3eb5
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. *
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 *******************************************************************/
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>
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"
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 */
41 static void *
42 lpfc_pool_kmalloc(gfp_t gfp_flags, void *data)
44 return kmalloc((unsigned long)data, gfp_flags);
47 static void
48 lpfc_pool_kfree(void *obj, void *data)
50 kfree(obj);
53 int
54 lpfc_mem_alloc(struct lpfc_hba * phba)
56 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
57 int i;
59 phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
60 phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
61 if (!phba->lpfc_scsi_dma_buf_pool)
62 goto fail;
64 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
65 LPFC_BPL_SIZE, 8,0);
66 if (!phba->lpfc_mbuf_pool)
67 goto fail_free_dma_buf_pool;
69 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
70 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
71 pool->max_count = 0;
72 pool->current_count = 0;
73 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
74 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
75 GFP_KERNEL, &pool->elements[i].phys);
76 if (!pool->elements[i].virt)
77 goto fail_free_mbuf_pool;
78 pool->max_count++;
79 pool->current_count++;
82 phba->mbox_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
83 lpfc_pool_kmalloc, lpfc_pool_kfree,
84 (void *)(unsigned long)sizeof(LPFC_MBOXQ_t));
85 if (!phba->mbox_mem_pool)
86 goto fail_free_mbuf_pool;
88 phba->nlp_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE,
89 lpfc_pool_kmalloc, lpfc_pool_kfree,
90 (void *)(unsigned long)sizeof(struct lpfc_nodelist));
91 if (!phba->nlp_mem_pool)
92 goto fail_free_mbox_pool;
94 return 0;
96 fail_free_mbox_pool:
97 mempool_destroy(phba->mbox_mem_pool);
98 fail_free_mbuf_pool:
99 while (--i)
100 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
101 pool->elements[i].phys);
102 kfree(pool->elements);
103 pci_pool_destroy(phba->lpfc_mbuf_pool);
104 fail_free_dma_buf_pool:
105 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
106 fail:
107 return -ENOMEM;
110 void
111 lpfc_mem_free(struct lpfc_hba * phba)
113 struct lpfc_sli *psli = &phba->sli;
114 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
115 LPFC_MBOXQ_t *mbox, *next_mbox;
116 struct lpfc_dmabuf *mp;
117 int i;
119 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
120 mp = (struct lpfc_dmabuf *) (mbox->context1);
121 if (mp) {
122 lpfc_mbuf_free(phba, mp->virt, mp->phys);
123 kfree(mp);
125 list_del(&mbox->list);
126 mempool_free(mbox, phba->mbox_mem_pool);
129 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
130 if (psli->mbox_active) {
131 mbox = psli->mbox_active;
132 mp = (struct lpfc_dmabuf *) (mbox->context1);
133 if (mp) {
134 lpfc_mbuf_free(phba, mp->virt, mp->phys);
135 kfree(mp);
137 mempool_free(mbox, phba->mbox_mem_pool);
138 psli->mbox_active = NULL;
141 for (i = 0; i < pool->current_count; i++)
142 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
143 pool->elements[i].phys);
144 kfree(pool->elements);
145 mempool_destroy(phba->nlp_mem_pool);
146 mempool_destroy(phba->mbox_mem_pool);
148 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
149 pci_pool_destroy(phba->lpfc_mbuf_pool);
152 void *
153 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
155 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
156 void *ret;
158 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
160 if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) {
161 pool->current_count--;
162 ret = pool->elements[pool->current_count].virt;
163 *handle = pool->elements[pool->current_count].phys;
165 return ret;
168 void
169 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
171 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
173 if (pool->current_count < pool->max_count) {
174 pool->elements[pool->current_count].virt = virt;
175 pool->elements[pool->current_count].phys = dma;
176 pool->current_count++;
177 } else {
178 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
180 return;