| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 1 | /******************************************************************* | 
 | 2 |  * This file is part of the Emulex Linux Device Driver for         * | 
| James.Smart@Emulex.Com | c44ce17 | 2005-06-25 10:34:39 -0400 | [diff] [blame] | 3 |  * Fibre Channel Host Bus Adapters.                                * | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 4 |  * Copyright (C) 2004-2006 Emulex.  All rights reserved.           * | 
| James.Smart@Emulex.Com | c44ce17 | 2005-06-25 10:34:39 -0400 | [diff] [blame] | 5 |  * EMULEX and SLI are trademarks of Emulex.                        * | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 6 |  * www.emulex.com                                                  * | 
| James.Smart@Emulex.Com | c44ce17 | 2005-06-25 10:34:39 -0400 | [diff] [blame] | 7 |  * Portions Copyright (C) 2004-2005 Christoph Hellwig              * | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 8 |  *                                                                 * | 
 | 9 |  * This program is free software; you can redistribute it and/or   * | 
| James.Smart@Emulex.Com | c44ce17 | 2005-06-25 10:34:39 -0400 | [diff] [blame] | 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.                                     * | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 20 |  *******************************************************************/ | 
 | 21 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 22 | #include <linux/mempool.h> | 
 | 23 | #include <linux/pci.h> | 
 | 24 | #include <linux/interrupt.h> | 
 | 25 |  | 
| James.Smart@Emulex.Com | f888ba3 | 2005-08-10 15:03:01 -0400 | [diff] [blame] | 26 | #include <scsi/scsi_device.h> | 
 | 27 | #include <scsi/scsi_transport_fc.h> | 
 | 28 |  | 
| James.Smart@Emulex.Com | 9188652 | 2005-08-10 15:03:09 -0400 | [diff] [blame] | 29 | #include <scsi/scsi.h> | 
 | 30 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 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" | 
 | 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 Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 41 |  | 
 | 42 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 43 | int | 
 | 44 | lpfc_mem_alloc(struct lpfc_hba * phba) | 
 | 45 | { | 
 | 46 | 	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 47 | 	int longs; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 48 | 	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 Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 62 | 	if (!pool->elements) | 
 | 63 | 		goto fail_free_lpfc_mbuf_pool; | 
 | 64 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 65 | 	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 Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 76 | 	phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, | 
 | 77 | 							 sizeof(LPFC_MBOXQ_t)); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 78 | 	if (!phba->mbox_mem_pool) | 
 | 79 | 		goto fail_free_mbuf_pool; | 
 | 80 |  | 
| Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 81 | 	phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, | 
 | 82 | 						sizeof(struct lpfc_nodelist)); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 83 | 	if (!phba->nlp_mem_pool) | 
 | 84 | 		goto fail_free_mbox_pool; | 
 | 85 |  | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 86 | 	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 Smart | 858c9f6 | 2007-06-17 19:56:39 -0500 | [diff] [blame] | 91 | 	/* 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 Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 93 | 	phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL); | 
 | 94 | 	if (!phba->vpi_bmask) | 
 | 95 | 		goto fail_free_hbq_pool; | 
 | 96 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 97 | 	return 0; | 
 | 98 |  | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 99 |  fail_free_hbq_pool: | 
 | 100 | 	lpfc_sli_hbqbuf_free_all(phba); | 
| James Smart | 0937282 | 2008-01-11 01:52:54 -0500 | [diff] [blame] | 101 | 	pci_pool_destroy(phba->lpfc_hbq_pool); | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 102 |  fail_free_nlp_mem_pool: | 
 | 103 | 	mempool_destroy(phba->nlp_mem_pool); | 
 | 104 | 	phba->nlp_mem_pool = NULL; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 105 |  fail_free_mbox_pool: | 
 | 106 | 	mempool_destroy(phba->mbox_mem_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 107 | 	phba->mbox_mem_pool = NULL; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 108 |  fail_free_mbuf_pool: | 
| Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 109 | 	while (i--) | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 110 | 		pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, | 
 | 111 | 						 pool->elements[i].phys); | 
 | 112 | 	kfree(pool->elements); | 
| Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 113 |  fail_free_lpfc_mbuf_pool: | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 114 | 	pci_pool_destroy(phba->lpfc_mbuf_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 115 | 	phba->lpfc_mbuf_pool = NULL; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 116 |  fail_free_dma_buf_pool: | 
 | 117 | 	pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 118 | 	phba->lpfc_scsi_dma_buf_pool = NULL; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 119 |  fail: | 
 | 120 | 	return -ENOMEM; | 
 | 121 | } | 
 | 122 |  | 
 | 123 | void | 
 | 124 | lpfc_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 Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 132 | 	kfree(phba->vpi_bmask); | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 133 | 	lpfc_sli_hbqbuf_free_all(phba); | 
 | 134 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 135 | 	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 Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 144 | 	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 | 	} | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 153 |  | 
 | 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 Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 170 |  | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 171 | 	pci_pool_destroy(phba->lpfc_hbq_pool); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 172 | 	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 Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 177 |  | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 178 | 	phba->lpfc_hbq_pool = NULL; | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 179 | 	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 Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 184 | 				/* Free the iocb lookup array */ | 
| James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 185 | 	kfree(psli->iocbq_lookup); | 
 | 186 | 	psli->iocbq_lookup = NULL; | 
 | 187 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 188 | } | 
 | 189 |  | 
 | 190 | void * | 
 | 191 | lpfc_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 Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 194 | 	unsigned long iflags; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 195 | 	void *ret; | 
 | 196 |  | 
 | 197 | 	ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle); | 
 | 198 |  | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 199 | 	spin_lock_irqsave(&phba->hbalock, iflags); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 200 | 	if (!ret && (mem_flags & MEM_PRI) && pool->current_count) { | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 201 | 		pool->current_count--; | 
 | 202 | 		ret = pool->elements[pool->current_count].virt; | 
 | 203 | 		*handle = pool->elements[pool->current_count].phys; | 
 | 204 | 	} | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 205 | 	spin_unlock_irqrestore(&phba->hbalock, iflags); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 206 | 	return ret; | 
 | 207 | } | 
 | 208 |  | 
 | 209 | void | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 210 | __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 211 | { | 
 | 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 Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 223 |  | 
 | 224 | void | 
 | 225 | lpfc_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 Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 234 |  | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 235 | struct hbq_dmabuf * | 
 | 236 | lpfc_els_hbq_alloc(struct lpfc_hba *phba) | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 237 | { | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 238 | 	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 Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 252 | } | 
 | 253 |  | 
 | 254 | void | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 255 | lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp) | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 256 | { | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 257 | 	pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys); | 
 | 258 | 	kfree(hbqbp); | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 259 | 	return; | 
 | 260 | } | 
 | 261 |  | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 262 | /* This is ONLY called for the LPFC_ELS_HBQ */ | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 263 | void | 
 | 264 | lpfc_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 Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 271 | 			(phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer) | 
 | 272 | 				(phba, hbq_entry); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 273 | 		} 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 |  |