blob: 07017658ac56770e601659727b641957d9c0a6c4 [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
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
dea31012005-04-17 16:05:31 -050041int
42lpfc_mem_alloc(struct lpfc_hba * phba)
43{
44 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
45 int i;
46
47 phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool",
48 phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0);
49 if (!phba->lpfc_scsi_dma_buf_pool)
50 goto fail;
51
52 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
53 LPFC_BPL_SIZE, 8,0);
54 if (!phba->lpfc_mbuf_pool)
55 goto fail_free_dma_buf_pool;
56
57 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
58 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
59 pool->max_count = 0;
60 pool->current_count = 0;
61 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
62 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
63 GFP_KERNEL, &pool->elements[i].phys);
64 if (!pool->elements[i].virt)
65 goto fail_free_mbuf_pool;
66 pool->max_count++;
67 pool->current_count++;
68 }
69
Matthew Dobson0eaae62a2006-03-26 01:37:47 -080070 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
71 sizeof(LPFC_MBOXQ_t));
dea31012005-04-17 16:05:31 -050072 if (!phba->mbox_mem_pool)
73 goto fail_free_mbuf_pool;
74
Matthew Dobson0eaae62a2006-03-26 01:37:47 -080075 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
76 sizeof(struct lpfc_nodelist));
dea31012005-04-17 16:05:31 -050077 if (!phba->nlp_mem_pool)
78 goto fail_free_mbox_pool;
79
80 return 0;
81
82 fail_free_mbox_pool:
83 mempool_destroy(phba->mbox_mem_pool);
84 fail_free_mbuf_pool:
85 while (--i)
86 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
87 pool->elements[i].phys);
88 kfree(pool->elements);
89 pci_pool_destroy(phba->lpfc_mbuf_pool);
90 fail_free_dma_buf_pool:
91 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
92 fail:
93 return -ENOMEM;
94}
95
96void
97lpfc_mem_free(struct lpfc_hba * phba)
98{
99 struct lpfc_sli *psli = &phba->sli;
100 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
101 LPFC_MBOXQ_t *mbox, *next_mbox;
102 struct lpfc_dmabuf *mp;
103 int i;
104
105 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
106 mp = (struct lpfc_dmabuf *) (mbox->context1);
107 if (mp) {
108 lpfc_mbuf_free(phba, mp->virt, mp->phys);
109 kfree(mp);
110 }
111 list_del(&mbox->list);
112 mempool_free(mbox, phba->mbox_mem_pool);
113 }
114
115 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
116 if (psli->mbox_active) {
117 mbox = psli->mbox_active;
118 mp = (struct lpfc_dmabuf *) (mbox->context1);
119 if (mp) {
120 lpfc_mbuf_free(phba, mp->virt, mp->phys);
121 kfree(mp);
122 }
123 mempool_free(mbox, phba->mbox_mem_pool);
124 psli->mbox_active = NULL;
125 }
126
127 for (i = 0; i < pool->current_count; i++)
128 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
129 pool->elements[i].phys);
130 kfree(pool->elements);
131 mempool_destroy(phba->nlp_mem_pool);
132 mempool_destroy(phba->mbox_mem_pool);
133
134 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
135 pci_pool_destroy(phba->lpfc_mbuf_pool);
136}
137
138void *
139lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
140{
141 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
142 void *ret;
143
144 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
145
146 if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) {
147 pool->current_count--;
148 ret = pool->elements[pool->current_count].virt;
149 *handle = pool->elements[pool->current_count].phys;
150 }
151 return ret;
152}
153
154void
155lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
156{
157 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
158
159 if (pool->current_count < pool->max_count) {
160 pool->elements[pool->current_count].virt = virt;
161 pool->elements[pool->current_count].phys = dma;
162 pool->current_count++;
163 } else {
164 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
165 }
166 return;
167}