blob: 034a8bfa9ac8d3a11271b7e1c784edf60cbfdb67 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2005 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/mempool.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040026#include <scsi/scsi_device.h>
27#include <scsi/scsi_transport_fc.h>
28
dea31012005-04-17 16:05:31 -050029#include "lpfc_hw.h"
30#include "lpfc_sli.h"
31#include "lpfc_disc.h"
32#include "lpfc_scsi.h"
33#include "lpfc.h"
34#include "lpfc_crtn.h"
35
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 */
38
39static void *
40lpfc_pool_kmalloc(unsigned int gfp_flags, void *data)
41{
42 return kmalloc((unsigned long)data, gfp_flags);
43}
44
45static void
46lpfc_pool_kfree(void *obj, void *data)
47{
48 kfree(obj);
49}
50
51int
52lpfc_mem_alloc(struct lpfc_hba * phba)
53{
54 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
55 int i;
56
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)
60 goto fail;
61
62 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
63 LPFC_BPL_SIZE, 8,0);
64 if (!phba->lpfc_mbuf_pool)
65 goto fail_free_dma_buf_pool;
66
67 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
68 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
69 pool->max_count = 0;
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;
76 pool->max_count++;
77 pool->current_count++;
78 }
79
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;
85
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;
91
92 return 0;
93
94 fail_free_mbox_pool:
95 mempool_destroy(phba->mbox_mem_pool);
96 fail_free_mbuf_pool:
97 while (--i)
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);
104 fail:
105 return -ENOMEM;
106}
107
108void
109lpfc_mem_free(struct lpfc_hba * phba)
110{
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;
115 int i;
116
117 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
118 mp = (struct lpfc_dmabuf *) (mbox->context1);
119 if (mp) {
120 lpfc_mbuf_free(phba, mp->virt, mp->phys);
121 kfree(mp);
122 }
123 list_del(&mbox->list);
124 mempool_free(mbox, phba->mbox_mem_pool);
125 }
126
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);
131 if (mp) {
132 lpfc_mbuf_free(phba, mp->virt, mp->phys);
133 kfree(mp);
134 }
135 mempool_free(mbox, phba->mbox_mem_pool);
136 psli->mbox_active = NULL;
137 }
138
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);
145
146 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
147 pci_pool_destroy(phba->lpfc_mbuf_pool);
148}
149
150void *
151lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
152{
153 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
154 void *ret;
155
156 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
157
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;
162 }
163 return ret;
164}
165
166void
167lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
168{
169 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
170
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++;
175 } else {
176 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
177 }
178 return;
179}