| 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 | d8e93df | 2009-05-22 14:53:05 -0400 | [diff] [blame] | 4 |  * Copyright (C) 2004-2009 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> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 24 | #include <linux/pci.h> | 
 | 25 | #include <linux/interrupt.h> | 
 | 26 |  | 
| James.Smart@Emulex.Com | f888ba3 | 2005-08-10 15:03:01 -0400 | [diff] [blame] | 27 | #include <scsi/scsi_device.h> | 
 | 28 | #include <scsi/scsi_transport_fc.h> | 
 | 29 |  | 
| James.Smart@Emulex.Com | 9188652 | 2005-08-10 15:03:09 -0400 | [diff] [blame] | 30 | #include <scsi/scsi.h> | 
 | 31 |  | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 32 | #include "lpfc_hw4.h" | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 33 | #include "lpfc_hw.h" | 
 | 34 | #include "lpfc_sli.h" | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 35 | #include "lpfc_sli4.h" | 
| James Smart | ea2151b | 2008-09-07 11:52:10 -0400 | [diff] [blame] | 36 | #include "lpfc_nl.h" | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 37 | #include "lpfc_disc.h" | 
 | 38 | #include "lpfc_scsi.h" | 
 | 39 | #include "lpfc.h" | 
 | 40 | #include "lpfc_crtn.h" | 
 | 41 |  | 
 | 42 | #define LPFC_MBUF_POOL_SIZE     64      /* max elements in MBUF safety pool */ | 
 | 43 | #define LPFC_MEM_POOL_SIZE      64      /* max elem in non-DMA safety pool */ | 
 | 44 |  | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 45 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 46 | /** | 
| James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 47 |  * lpfc_mem_alloc - create and allocate all PCI and memory pools | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 48 |  * @phba: HBA to allocate pools for | 
 | 49 |  * | 
 | 50 |  * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool, | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 51 |  * lpfc_mbuf_pool, lpfc_hrb_pool.  Creates and allocates kmalloc-backed mempools | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 52 |  * for LPFC_MBOXQ_t and lpfc_nodelist.  Also allocates the VPI bitmask. | 
 | 53 |  * | 
 | 54 |  * Notes: Not interrupt-safe.  Must be called with no locks held.  If any | 
 | 55 |  * allocation fails, frees all successfully allocated memory before returning. | 
 | 56 |  * | 
 | 57 |  * Returns: | 
 | 58 |  *   0 on success | 
 | 59 |  *   -ENOMEM on failure (if any memory allocations fail) | 
 | 60 |  **/ | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 61 | int | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 62 | lpfc_mem_alloc(struct lpfc_hba *phba, int align) | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 63 | { | 
 | 64 | 	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 65 | 	int longs; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 66 | 	int i; | 
 | 67 |  | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 68 | 	if (phba->sli_rev == LPFC_SLI_REV4) | 
 | 69 | 		phba->lpfc_scsi_dma_buf_pool = | 
 | 70 | 			pci_pool_create("lpfc_scsi_dma_buf_pool", | 
 | 71 | 				phba->pcidev, | 
 | 72 | 				phba->cfg_sg_dma_buf_size, | 
 | 73 | 				phba->cfg_sg_dma_buf_size, | 
 | 74 | 				0); | 
 | 75 | 	else | 
 | 76 | 		phba->lpfc_scsi_dma_buf_pool = | 
 | 77 | 			pci_pool_create("lpfc_scsi_dma_buf_pool", | 
 | 78 | 				phba->pcidev, phba->cfg_sg_dma_buf_size, | 
 | 79 | 				align, 0); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 80 | 	if (!phba->lpfc_scsi_dma_buf_pool) | 
 | 81 | 		goto fail; | 
 | 82 |  | 
 | 83 | 	phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev, | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 84 | 							LPFC_BPL_SIZE, | 
 | 85 | 							align, 0); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 86 | 	if (!phba->lpfc_mbuf_pool) | 
 | 87 | 		goto fail_free_dma_buf_pool; | 
 | 88 |  | 
 | 89 | 	pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) * | 
 | 90 | 					 LPFC_MBUF_POOL_SIZE, GFP_KERNEL); | 
| Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 91 | 	if (!pool->elements) | 
 | 92 | 		goto fail_free_lpfc_mbuf_pool; | 
 | 93 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 94 | 	pool->max_count = 0; | 
 | 95 | 	pool->current_count = 0; | 
 | 96 | 	for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) { | 
 | 97 | 		pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool, | 
 | 98 | 				       GFP_KERNEL, &pool->elements[i].phys); | 
 | 99 | 		if (!pool->elements[i].virt) | 
 | 100 | 			goto fail_free_mbuf_pool; | 
 | 101 | 		pool->max_count++; | 
 | 102 | 		pool->current_count++; | 
 | 103 | 	} | 
 | 104 |  | 
| Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 105 | 	phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, | 
 | 106 | 							 sizeof(LPFC_MBOXQ_t)); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 107 | 	if (!phba->mbox_mem_pool) | 
 | 108 | 		goto fail_free_mbuf_pool; | 
 | 109 |  | 
| Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 110 | 	phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, | 
 | 111 | 						sizeof(struct lpfc_nodelist)); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 112 | 	if (!phba->nlp_mem_pool) | 
 | 113 | 		goto fail_free_mbox_pool; | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 114 |  | 
 | 115 | 	if (phba->sli_rev == LPFC_SLI_REV4) { | 
 | 116 | 		phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool", | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 117 | 					      phba->pcidev, | 
 | 118 | 					      LPFC_HDR_BUF_SIZE, align, 0); | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 119 | 		if (!phba->lpfc_hrb_pool) | 
 | 120 | 			goto fail_free_nlp_mem_pool; | 
 | 121 |  | 
 | 122 | 		phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool", | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 123 | 					      phba->pcidev, | 
 | 124 | 					      LPFC_DATA_BUF_SIZE, align, 0); | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 125 | 		if (!phba->lpfc_drb_pool) | 
 | 126 | 			goto fail_free_hrb_pool; | 
 | 127 | 		phba->lpfc_hbq_pool = NULL; | 
 | 128 | 	} else { | 
 | 129 | 		phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool", | 
 | 130 | 			phba->pcidev, LPFC_BPL_SIZE, align, 0); | 
 | 131 | 		if (!phba->lpfc_hbq_pool) | 
 | 132 | 			goto fail_free_nlp_mem_pool; | 
 | 133 | 		phba->lpfc_hrb_pool = NULL; | 
 | 134 | 		phba->lpfc_drb_pool = NULL; | 
 | 135 | 	} | 
| James Smart | 858c9f6 | 2007-06-17 19:56:39 -0500 | [diff] [blame] | 136 | 	/* vpi zero is reserved for the physical port so add 1 to max */ | 
 | 137 | 	longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG; | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 138 | 	phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL); | 
 | 139 | 	if (!phba->vpi_bmask) | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 140 | 		goto fail_free_dbq_pool; | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 141 |  | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 142 | 	return 0; | 
 | 143 |  | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 144 |  fail_free_dbq_pool: | 
 | 145 | 	pci_pool_destroy(phba->lpfc_drb_pool); | 
 | 146 | 	phba->lpfc_drb_pool = NULL; | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 147 |  fail_free_hrb_pool: | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 148 | 	pci_pool_destroy(phba->lpfc_hrb_pool); | 
 | 149 | 	phba->lpfc_hrb_pool = NULL; | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 150 |  fail_free_nlp_mem_pool: | 
 | 151 | 	mempool_destroy(phba->nlp_mem_pool); | 
 | 152 | 	phba->nlp_mem_pool = NULL; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 153 |  fail_free_mbox_pool: | 
 | 154 | 	mempool_destroy(phba->mbox_mem_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 155 | 	phba->mbox_mem_pool = NULL; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 156 |  fail_free_mbuf_pool: | 
| Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 157 | 	while (i--) | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 158 | 		pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, | 
 | 159 | 						 pool->elements[i].phys); | 
 | 160 | 	kfree(pool->elements); | 
| Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 161 |  fail_free_lpfc_mbuf_pool: | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 162 | 	pci_pool_destroy(phba->lpfc_mbuf_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 163 | 	phba->lpfc_mbuf_pool = NULL; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 164 |  fail_free_dma_buf_pool: | 
 | 165 | 	pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 166 | 	phba->lpfc_scsi_dma_buf_pool = NULL; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 167 |  fail: | 
 | 168 | 	return -ENOMEM; | 
 | 169 | } | 
 | 170 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 171 | /** | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 172 |  * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 173 |  * @phba: HBA to free memory for | 
 | 174 |  * | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 175 |  * Description: Free the memory allocated by lpfc_mem_alloc routine. This | 
 | 176 |  * routine is a the counterpart of lpfc_mem_alloc. | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 177 |  * | 
 | 178 |  * Returns: None | 
 | 179 |  **/ | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 180 | void | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 181 | lpfc_mem_free(struct lpfc_hba *phba) | 
 | 182 | { | 
 | 183 | 	int i; | 
 | 184 | 	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
 | 185 |  | 
 | 186 | 	/* Free VPI bitmask memory */ | 
 | 187 | 	kfree(phba->vpi_bmask); | 
 | 188 |  | 
 | 189 | 	/* Free HBQ pools */ | 
 | 190 | 	lpfc_sli_hbqbuf_free_all(phba); | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 191 | 	if (phba->lpfc_drb_pool) | 
 | 192 | 		pci_pool_destroy(phba->lpfc_drb_pool); | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 193 | 	phba->lpfc_drb_pool = NULL; | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 194 | 	if (phba->lpfc_hrb_pool) | 
 | 195 | 		pci_pool_destroy(phba->lpfc_hrb_pool); | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 196 | 	phba->lpfc_hrb_pool = NULL; | 
 | 197 |  | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 198 | 	if (phba->lpfc_hbq_pool) | 
 | 199 | 		pci_pool_destroy(phba->lpfc_hbq_pool); | 
 | 200 | 	phba->lpfc_hbq_pool = NULL; | 
 | 201 |  | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 202 | 	/* Free NLP memory pool */ | 
 | 203 | 	mempool_destroy(phba->nlp_mem_pool); | 
 | 204 | 	phba->nlp_mem_pool = NULL; | 
 | 205 |  | 
 | 206 | 	/* Free mbox memory pool */ | 
 | 207 | 	mempool_destroy(phba->mbox_mem_pool); | 
 | 208 | 	phba->mbox_mem_pool = NULL; | 
 | 209 |  | 
 | 210 | 	/* Free MBUF memory pool */ | 
 | 211 | 	for (i = 0; i < pool->current_count; i++) | 
 | 212 | 		pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, | 
 | 213 | 			      pool->elements[i].phys); | 
 | 214 | 	kfree(pool->elements); | 
 | 215 |  | 
 | 216 | 	pci_pool_destroy(phba->lpfc_mbuf_pool); | 
 | 217 | 	phba->lpfc_mbuf_pool = NULL; | 
 | 218 |  | 
 | 219 | 	/* Free DMA buffer memory pool */ | 
 | 220 | 	pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); | 
 | 221 | 	phba->lpfc_scsi_dma_buf_pool = NULL; | 
 | 222 |  | 
 | 223 | 	return; | 
 | 224 | } | 
 | 225 |  | 
 | 226 | /** | 
 | 227 |  * lpfc_mem_free_all - Frees all PCI and driver memory | 
 | 228 |  * @phba: HBA to free memory for | 
 | 229 |  * | 
 | 230 |  * Description: Free memory from PCI and driver memory pools and also those | 
 | 231 |  * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees | 
 | 232 |  * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees | 
 | 233 |  * the VPI bitmask. | 
 | 234 |  * | 
 | 235 |  * Returns: None | 
 | 236 |  **/ | 
 | 237 | void | 
 | 238 | lpfc_mem_free_all(struct lpfc_hba *phba) | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 239 | { | 
 | 240 | 	struct lpfc_sli *psli = &phba->sli; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 241 | 	LPFC_MBOXQ_t *mbox, *next_mbox; | 
 | 242 | 	struct lpfc_dmabuf   *mp; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 243 |  | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 244 | 	/* Free memory used in mailbox queue back to mailbox memory pool */ | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 245 | 	list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) { | 
 | 246 | 		mp = (struct lpfc_dmabuf *) (mbox->context1); | 
 | 247 | 		if (mp) { | 
 | 248 | 			lpfc_mbuf_free(phba, mp->virt, mp->phys); | 
 | 249 | 			kfree(mp); | 
 | 250 | 		} | 
 | 251 | 		list_del(&mbox->list); | 
 | 252 | 		mempool_free(mbox, phba->mbox_mem_pool); | 
 | 253 | 	} | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 254 | 	/* Free memory used in mailbox cmpl list back to mailbox memory pool */ | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 255 | 	list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) { | 
 | 256 | 		mp = (struct lpfc_dmabuf *) (mbox->context1); | 
 | 257 | 		if (mp) { | 
 | 258 | 			lpfc_mbuf_free(phba, mp->virt, mp->phys); | 
 | 259 | 			kfree(mp); | 
 | 260 | 		} | 
 | 261 | 		list_del(&mbox->list); | 
 | 262 | 		mempool_free(mbox, phba->mbox_mem_pool); | 
 | 263 | 	} | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 264 | 	/* Free the active mailbox command back to the mailbox memory pool */ | 
 | 265 | 	spin_lock_irq(&phba->hbalock); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 266 | 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE; | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 267 | 	spin_unlock_irq(&phba->hbalock); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 268 | 	if (psli->mbox_active) { | 
 | 269 | 		mbox = psli->mbox_active; | 
 | 270 | 		mp = (struct lpfc_dmabuf *) (mbox->context1); | 
 | 271 | 		if (mp) { | 
 | 272 | 			lpfc_mbuf_free(phba, mp->virt, mp->phys); | 
 | 273 | 			kfree(mp); | 
 | 274 | 		} | 
 | 275 | 		mempool_free(mbox, phba->mbox_mem_pool); | 
 | 276 | 		psli->mbox_active = NULL; | 
 | 277 | 	} | 
 | 278 |  | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 279 | 	/* Free and destroy all the allocated memory pools */ | 
 | 280 | 	lpfc_mem_free(phba); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 281 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 282 | 	/* Free the iocb lookup array */ | 
| James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 283 | 	kfree(psli->iocbq_lookup); | 
 | 284 | 	psli->iocbq_lookup = NULL; | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 285 |  | 
 | 286 | 	return; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 287 | } | 
 | 288 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 289 | /** | 
| James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 290 |  * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 291 |  * @phba: HBA which owns the pool to allocate from | 
 | 292 |  * @mem_flags: indicates if this is a priority (MEM_PRI) allocation | 
 | 293 |  * @handle: used to return the DMA-mapped address of the mbuf | 
 | 294 |  * | 
 | 295 |  * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool. | 
 | 296 |  * Allocates from generic pci_pool_alloc function first and if that fails and | 
 | 297 |  * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the | 
 | 298 |  * HBA's pool. | 
 | 299 |  * | 
 | 300 |  * Notes: Not interrupt-safe.  Must be called with no locks held.  Takes | 
 | 301 |  * phba->hbalock. | 
 | 302 |  * | 
 | 303 |  * Returns: | 
 | 304 |  *   pointer to the allocated mbuf on success | 
 | 305 |  *   NULL on failure | 
 | 306 |  **/ | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 307 | void * | 
 | 308 | lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle) | 
 | 309 | { | 
 | 310 | 	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 311 | 	unsigned long iflags; | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 312 | 	void *ret; | 
 | 313 |  | 
 | 314 | 	ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle); | 
 | 315 |  | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 316 | 	spin_lock_irqsave(&phba->hbalock, iflags); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 317 | 	if (!ret && (mem_flags & MEM_PRI) && pool->current_count) { | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 318 | 		pool->current_count--; | 
 | 319 | 		ret = pool->elements[pool->current_count].virt; | 
 | 320 | 		*handle = pool->elements[pool->current_count].phys; | 
 | 321 | 	} | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 322 | 	spin_unlock_irqrestore(&phba->hbalock, iflags); | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 323 | 	return ret; | 
 | 324 | } | 
 | 325 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 326 | /** | 
| James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 327 |  * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked) | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 328 |  * @phba: HBA which owns the pool to return to | 
 | 329 |  * @virt: mbuf to free | 
 | 330 |  * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed | 
 | 331 |  * | 
 | 332 |  * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if | 
 | 333 |  * it is below its max_count, frees the mbuf otherwise. | 
 | 334 |  * | 
 | 335 |  * Notes: Must be called with phba->hbalock held to synchronize access to | 
 | 336 |  * lpfc_mbuf_safety_pool. | 
 | 337 |  * | 
 | 338 |  * Returns: None | 
 | 339 |  **/ | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 340 | void | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 341 | __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) | 
 | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 342 | { | 
 | 343 | 	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
 | 344 |  | 
 | 345 | 	if (pool->current_count < pool->max_count) { | 
 | 346 | 		pool->elements[pool->current_count].virt = virt; | 
 | 347 | 		pool->elements[pool->current_count].phys = dma; | 
 | 348 | 		pool->current_count++; | 
 | 349 | 	} else { | 
 | 350 | 		pci_pool_free(phba->lpfc_mbuf_pool, virt, dma); | 
 | 351 | 	} | 
 | 352 | 	return; | 
 | 353 | } | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 354 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 355 | /** | 
| James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 356 |  * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked) | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 357 |  * @phba: HBA which owns the pool to return to | 
 | 358 |  * @virt: mbuf to free | 
 | 359 |  * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed | 
 | 360 |  * | 
 | 361 |  * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if | 
 | 362 |  * it is below its max_count, frees the mbuf otherwise. | 
 | 363 |  * | 
 | 364 |  * Notes: Takes phba->hbalock.  Can be called with or without other locks held. | 
 | 365 |  * | 
 | 366 |  * Returns: None | 
 | 367 |  **/ | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 368 | void | 
 | 369 | lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) | 
 | 370 | { | 
 | 371 | 	unsigned long iflags; | 
 | 372 |  | 
 | 373 | 	spin_lock_irqsave(&phba->hbalock, iflags); | 
 | 374 | 	__lpfc_mbuf_free(phba, virt, dma); | 
 | 375 | 	spin_unlock_irqrestore(&phba->hbalock, iflags); | 
 | 376 | 	return; | 
 | 377 | } | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 378 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 379 | /** | 
| James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 380 |  * lpfc_els_hbq_alloc - Allocate an HBQ buffer | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 381 |  * @phba: HBA to allocate HBQ buffer for | 
 | 382 |  * | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 383 |  * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 384 |  * pool along a non-DMA-mapped container for it. | 
 | 385 |  * | 
 | 386 |  * Notes: Not interrupt-safe.  Must be called with no locks held. | 
 | 387 |  * | 
 | 388 |  * Returns: | 
 | 389 |  *   pointer to HBQ on success | 
 | 390 |  *   NULL on failure | 
 | 391 |  **/ | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 392 | struct hbq_dmabuf * | 
 | 393 | lpfc_els_hbq_alloc(struct lpfc_hba *phba) | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 394 | { | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 395 | 	struct hbq_dmabuf *hbqbp; | 
 | 396 |  | 
 | 397 | 	hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL); | 
 | 398 | 	if (!hbqbp) | 
 | 399 | 		return NULL; | 
 | 400 |  | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 401 | 	hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL, | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 402 | 					  &hbqbp->dbuf.phys); | 
 | 403 | 	if (!hbqbp->dbuf.virt) { | 
 | 404 | 		kfree(hbqbp); | 
 | 405 | 		return NULL; | 
 | 406 | 	} | 
 | 407 | 	hbqbp->size = LPFC_BPL_SIZE; | 
 | 408 | 	return hbqbp; | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 409 | } | 
 | 410 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 411 | /** | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 412 |  * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 413 |  * @phba: HBA buffer was allocated for | 
 | 414 |  * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc | 
 | 415 |  * | 
 | 416 |  * Description: Frees both the container and the DMA-mapped buffer returned by | 
 | 417 |  * lpfc_els_hbq_alloc. | 
 | 418 |  * | 
 | 419 |  * Notes: Can be called with or without locks held. | 
 | 420 |  * | 
 | 421 |  * Returns: None | 
 | 422 |  **/ | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 423 | void | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 424 | lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp) | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 425 | { | 
| James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 426 | 	pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys); | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 427 | 	kfree(hbqbp); | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 428 | 	return; | 
 | 429 | } | 
 | 430 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 431 | /** | 
| James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 432 |  * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer | 
 | 433 |  * @phba: HBA to allocate a receive buffer for | 
 | 434 |  * | 
 | 435 |  * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI | 
 | 436 |  * pool along a non-DMA-mapped container for it. | 
 | 437 |  * | 
 | 438 |  * Notes: Not interrupt-safe.  Must be called with no locks held. | 
 | 439 |  * | 
 | 440 |  * Returns: | 
 | 441 |  *   pointer to HBQ on success | 
 | 442 |  *   NULL on failure | 
 | 443 |  **/ | 
 | 444 | struct hbq_dmabuf * | 
 | 445 | lpfc_sli4_rb_alloc(struct lpfc_hba *phba) | 
 | 446 | { | 
 | 447 | 	struct hbq_dmabuf *dma_buf; | 
 | 448 |  | 
 | 449 | 	dma_buf = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL); | 
 | 450 | 	if (!dma_buf) | 
 | 451 | 		return NULL; | 
 | 452 |  | 
 | 453 | 	dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL, | 
 | 454 | 					    &dma_buf->hbuf.phys); | 
 | 455 | 	if (!dma_buf->hbuf.virt) { | 
 | 456 | 		kfree(dma_buf); | 
 | 457 | 		return NULL; | 
 | 458 | 	} | 
 | 459 | 	dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL, | 
 | 460 | 					    &dma_buf->dbuf.phys); | 
 | 461 | 	if (!dma_buf->dbuf.virt) { | 
 | 462 | 		pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt, | 
 | 463 | 			      dma_buf->hbuf.phys); | 
 | 464 | 		kfree(dma_buf); | 
 | 465 | 		return NULL; | 
 | 466 | 	} | 
 | 467 | 	dma_buf->size = LPFC_BPL_SIZE; | 
 | 468 | 	return dma_buf; | 
 | 469 | } | 
 | 470 |  | 
 | 471 | /** | 
 | 472 |  * lpfc_sli4_rb_free - Frees a receive buffer | 
 | 473 |  * @phba: HBA buffer was allocated for | 
 | 474 |  * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc | 
 | 475 |  * | 
 | 476 |  * Description: Frees both the container and the DMA-mapped buffers returned by | 
 | 477 |  * lpfc_sli4_rb_alloc. | 
 | 478 |  * | 
 | 479 |  * Notes: Can be called with or without locks held. | 
 | 480 |  * | 
 | 481 |  * Returns: None | 
 | 482 |  **/ | 
 | 483 | void | 
 | 484 | lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab) | 
 | 485 | { | 
 | 486 | 	pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys); | 
 | 487 | 	pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys); | 
 | 488 | 	kfree(dmab); | 
 | 489 | 	return; | 
 | 490 | } | 
 | 491 |  | 
 | 492 | /** | 
| James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 493 |  * lpfc_in_buf_free - Free a DMA buffer | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 494 |  * @phba: HBA buffer is associated with | 
 | 495 |  * @mp: Buffer to free | 
 | 496 |  * | 
 | 497 |  * Description: Frees the given DMA buffer in the appropriate way given if the | 
 | 498 |  * HBA is running in SLI3 mode with HBQs enabled. | 
 | 499 |  * | 
 | 500 |  * Notes: Takes phba->hbalock.  Can be called with or without other locks held. | 
 | 501 |  * | 
 | 502 |  * Returns: None | 
 | 503 |  **/ | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 504 | void | 
 | 505 | lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp) | 
 | 506 | { | 
 | 507 | 	struct hbq_dmabuf *hbq_entry; | 
| James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 508 | 	unsigned long flags; | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 509 |  | 
| James Smart | 7f5f3d0 | 2008-02-08 18:50:14 -0500 | [diff] [blame] | 510 | 	if (!mp) | 
 | 511 | 		return; | 
 | 512 |  | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 513 | 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) { | 
| James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 514 | 		/* Check whether HBQ is still in use */ | 
 | 515 | 		spin_lock_irqsave(&phba->hbalock, flags); | 
 | 516 | 		if (!phba->hbq_in_use) { | 
 | 517 | 			spin_unlock_irqrestore(&phba->hbalock, flags); | 
 | 518 | 			return; | 
 | 519 | 		} | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 520 | 		hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf); | 
| James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 521 | 		list_del(&hbq_entry->dbuf.list); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 522 | 		if (hbq_entry->tag == -1) { | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 523 | 			(phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer) | 
 | 524 | 				(phba, hbq_entry); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 525 | 		} else { | 
 | 526 | 			lpfc_sli_free_hbq(phba, hbq_entry); | 
 | 527 | 		} | 
| James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 528 | 		spin_unlock_irqrestore(&phba->hbalock, flags); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 529 | 	} else { | 
 | 530 | 		lpfc_mbuf_free(phba, mp->virt, mp->phys); | 
 | 531 | 		kfree(mp); | 
 | 532 | 	} | 
 | 533 | 	return; | 
 | 534 | } |