xref: /linux/drivers/scsi/lpfc/lpfc_mem.c (revision 776cfebb430c7b22c208b1b17add97f354d97cab)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Enterprise Fibre Channel Host Bus Adapters.                     *
4  * Refer to the README file included with this package for         *
5  * driver version and adapter support.                             *
6  * Copyright (C) 2004 Emulex Corporation.                          *
7  * www.emulex.com                                                  *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of the GNU General Public License     *
11  * as published by the Free Software Foundation; either version 2  *
12  * of the License, or (at your option) any later version.          *
13  *                                                                 *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of  *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
17  * GNU General Public License for more details, a copy of which    *
18  * can be found in the file COPYING included with this package.    *
19  *******************************************************************/
20 
21 /*
22  * $Id: lpfc_mem.c 1.79 2005/04/13 14:25:50EDT sf_support Exp  $
23  */
24 
25 #include <linux/mempool.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 
29 #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 
39 static void *
40 lpfc_pool_kmalloc(unsigned int gfp_flags, void *data)
41 {
42 	return kmalloc((unsigned long)data, gfp_flags);
43 }
44 
45 static void
46 lpfc_pool_kfree(void *obj, void *data)
47 {
48 	kfree(obj);
49 }
50 
51 int
52 lpfc_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 
108 void
109 lpfc_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 
150 void *
151 lpfc_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 
166 void
167 lpfc_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 }
180