blob: 6dc5ab8d6716b3f8fd13e69dd3309afbe1cf8190 [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. *
James Smart2e0fef82007-06-17 19:56:36 -05004 * Copyright (C) 2004-2006 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * 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
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
30
dea31012005-04-17 16:05:31 -050031#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"
37
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 */
40
James Smart2e0fef82007-06-17 19:56:36 -050041
42
dea31012005-04-17 16:05:31 -050043int
44lpfc_mem_alloc(struct lpfc_hba * phba)
45{
46 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart92d7f7b2007-06-17 19:56:38 -050047 int longs;
dea31012005-04-17 16:05:31 -050048 int i;
49
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)
53 goto fail;
54
55 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
56 LPFC_BPL_SIZE, 8,0);
57 if (!phba->lpfc_mbuf_pool)
58 goto fail_free_dma_buf_pool;
59
60 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
61 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +010062 if (!pool->elements)
63 goto fail_free_lpfc_mbuf_pool;
64
dea31012005-04-17 16:05:31 -050065 pool->max_count = 0;
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;
72 pool->max_count++;
73 pool->current_count++;
74 }
75
Matthew Dobson0eaae62a2006-03-26 01:37:47 -080076 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
77 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -050078 if (!phba->mbox_mem_pool)
79 goto fail_free_mbuf_pool;
80
Matthew Dobson0eaae62a2006-03-26 01:37:47 -080081 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
82 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -050083 if (!phba->nlp_mem_pool)
84 goto fail_free_mbox_pool;
85
James Smarted957682007-06-17 19:56:37 -050086 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev,
87 LPFC_BPL_SIZE, 8, 0);
88 if (!phba->lpfc_hbq_pool)
89 goto fail_free_nlp_mem_pool;
90
James Smart858c9f62007-06-17 19:56:39 -050091 /* 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;
James Smart92d7f7b2007-06-17 19:56:38 -050093 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
94 if (!phba->vpi_bmask)
95 goto fail_free_hbq_pool;
96
dea31012005-04-17 16:05:31 -050097 return 0;
98
James Smart92d7f7b2007-06-17 19:56:38 -050099 fail_free_hbq_pool:
100 lpfc_sli_hbqbuf_free_all(phba);
James Smart09372822008-01-11 01:52:54 -0500101 pci_pool_destroy(phba->lpfc_hbq_pool);
James Smarted957682007-06-17 19:56:37 -0500102 fail_free_nlp_mem_pool:
103 mempool_destroy(phba->nlp_mem_pool);
104 phba->nlp_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500105 fail_free_mbox_pool:
106 mempool_destroy(phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500107 phba->mbox_mem_pool = NULL;
dea31012005-04-17 16:05:31 -0500108 fail_free_mbuf_pool:
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100109 while (i--)
dea31012005-04-17 16:05:31 -0500110 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
111 pool->elements[i].phys);
112 kfree(pool->elements);
Mariusz Kozlowskia96e0c72007-01-02 01:07:32 +0100113 fail_free_lpfc_mbuf_pool:
dea31012005-04-17 16:05:31 -0500114 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500115 phba->lpfc_mbuf_pool = NULL;
dea31012005-04-17 16:05:31 -0500116 fail_free_dma_buf_pool:
117 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
James Smart2e0fef82007-06-17 19:56:36 -0500118 phba->lpfc_scsi_dma_buf_pool = NULL;
dea31012005-04-17 16:05:31 -0500119 fail:
120 return -ENOMEM;
121}
122
123void
124lpfc_mem_free(struct lpfc_hba * phba)
125{
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;
130 int i;
131
James Smart92d7f7b2007-06-17 19:56:38 -0500132 kfree(phba->vpi_bmask);
James Smarted957682007-06-17 19:56:37 -0500133 lpfc_sli_hbqbuf_free_all(phba);
134
dea31012005-04-17 16:05:31 -0500135 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
136 mp = (struct lpfc_dmabuf *) (mbox->context1);
137 if (mp) {
138 lpfc_mbuf_free(phba, mp->virt, mp->phys);
139 kfree(mp);
140 }
141 list_del(&mbox->list);
142 mempool_free(mbox, phba->mbox_mem_pool);
143 }
James Smart92d7f7b2007-06-17 19:56:38 -0500144 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
145 mp = (struct lpfc_dmabuf *) (mbox->context1);
146 if (mp) {
147 lpfc_mbuf_free(phba, mp->virt, mp->phys);
148 kfree(mp);
149 }
150 list_del(&mbox->list);
151 mempool_free(mbox, phba->mbox_mem_pool);
152 }
dea31012005-04-17 16:05:31 -0500153
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);
158 if (mp) {
159 lpfc_mbuf_free(phba, mp->virt, mp->phys);
160 kfree(mp);
161 }
162 mempool_free(mbox, phba->mbox_mem_pool);
163 psli->mbox_active = NULL;
164 }
165
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);
James Smart2e0fef82007-06-17 19:56:36 -0500170
James Smarted957682007-06-17 19:56:37 -0500171 pci_pool_destroy(phba->lpfc_hbq_pool);
dea31012005-04-17 16:05:31 -0500172 mempool_destroy(phba->nlp_mem_pool);
173 mempool_destroy(phba->mbox_mem_pool);
174
175 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
176 pci_pool_destroy(phba->lpfc_mbuf_pool);
James Smart9f49d3b2006-07-06 15:49:34 -0400177
James Smarted957682007-06-17 19:56:37 -0500178 phba->lpfc_hbq_pool = NULL;
James Smart2e0fef82007-06-17 19:56:36 -0500179 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;
183
James Smart92d7f7b2007-06-17 19:56:38 -0500184 /* Free the iocb lookup array */
James Smart9f49d3b2006-07-06 15:49:34 -0400185 kfree(psli->iocbq_lookup);
186 psli->iocbq_lookup = NULL;
187
dea31012005-04-17 16:05:31 -0500188}
189
190void *
191lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
192{
193 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
James Smart2e0fef82007-06-17 19:56:36 -0500194 unsigned long iflags;
dea31012005-04-17 16:05:31 -0500195 void *ret;
196
197 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
198
James Smart2e0fef82007-06-17 19:56:36 -0500199 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart92d7f7b2007-06-17 19:56:38 -0500200 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea31012005-04-17 16:05:31 -0500201 pool->current_count--;
202 ret = pool->elements[pool->current_count].virt;
203 *handle = pool->elements[pool->current_count].phys;
204 }
James Smart2e0fef82007-06-17 19:56:36 -0500205 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -0500206 return ret;
207}
208
209void
James Smart2e0fef82007-06-17 19:56:36 -0500210__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea31012005-04-17 16:05:31 -0500211{
212 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
213
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++;
218 } else {
219 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
220 }
221 return;
222}
James Smart2e0fef82007-06-17 19:56:36 -0500223
224void
225lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
226{
227 unsigned long iflags;
228
229 spin_lock_irqsave(&phba->hbalock, iflags);
230 __lpfc_mbuf_free(phba, virt, dma);
231 spin_unlock_irqrestore(&phba->hbalock, iflags);
232 return;
233}
James Smarted957682007-06-17 19:56:37 -0500234
James Smart51ef4c22007-08-02 11:10:31 -0400235struct hbq_dmabuf *
236lpfc_els_hbq_alloc(struct lpfc_hba *phba)
James Smarted957682007-06-17 19:56:37 -0500237{
James Smart51ef4c22007-08-02 11:10:31 -0400238 struct hbq_dmabuf *hbqbp;
239
240 hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
241 if (!hbqbp)
242 return NULL;
243
244 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
245 &hbqbp->dbuf.phys);
246 if (!hbqbp->dbuf.virt) {
247 kfree(hbqbp);
248 return NULL;
249 }
250 hbqbp->size = LPFC_BPL_SIZE;
251 return hbqbp;
James Smarted957682007-06-17 19:56:37 -0500252}
253
254void
James Smart51ef4c22007-08-02 11:10:31 -0400255lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
James Smarted957682007-06-17 19:56:37 -0500256{
James Smart51ef4c22007-08-02 11:10:31 -0400257 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
258 kfree(hbqbp);
James Smarted957682007-06-17 19:56:37 -0500259 return;
260}
261
James Smart51ef4c22007-08-02 11:10:31 -0400262/* This is ONLY called for the LPFC_ELS_HBQ */
James Smart92d7f7b2007-06-17 19:56:38 -0500263void
264lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
265{
266 struct hbq_dmabuf *hbq_entry;
267
268 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
269 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
270 if (hbq_entry->tag == -1) {
James Smart51ef4c22007-08-02 11:10:31 -0400271 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
272 (phba, hbq_entry);
James Smart92d7f7b2007-06-17 19:56:38 -0500273 } else {
274 lpfc_sli_free_hbq(phba, hbq_entry);
275 }
276 } else {
277 lpfc_mbuf_free(phba, mp->virt, mp->phys);
278 kfree(mp);
279 }
280 return;
281}
282