|  | 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 | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 4 | * Copyright (C) 2004-2008 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" | 
| James Smart | ea2151b | 2008-09-07 11:52:10 -0400 | [diff] [blame] | 33 | #include "lpfc_nl.h" | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 34 | #include "lpfc_disc.h" | 
|  | 35 | #include "lpfc_scsi.h" | 
|  | 36 | #include "lpfc.h" | 
|  | 37 | #include "lpfc_crtn.h" | 
|  | 38 |  | 
|  | 39 | #define LPFC_MBUF_POOL_SIZE     64      /* max elements in MBUF safety pool */ | 
|  | 40 | #define LPFC_MEM_POOL_SIZE      64      /* max elem in non-DMA safety pool */ | 
|  | 41 |  | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 42 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 43 | /** | 
|  | 44 | * lpfc_mem_alloc: create and allocate all PCI and memory pools | 
|  | 45 | * @phba: HBA to allocate pools for | 
|  | 46 | * | 
|  | 47 | * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool, | 
|  | 48 | * lpfc_mbuf_pool, lpfc_hbq_pool.  Creates and allocates kmalloc-backed mempools | 
|  | 49 | * for LPFC_MBOXQ_t and lpfc_nodelist.  Also allocates the VPI bitmask. | 
|  | 50 | * | 
|  | 51 | * Notes: Not interrupt-safe.  Must be called with no locks held.  If any | 
|  | 52 | * allocation fails, frees all successfully allocated memory before returning. | 
|  | 53 | * | 
|  | 54 | * Returns: | 
|  | 55 | *   0 on success | 
|  | 56 | *   -ENOMEM on failure (if any memory allocations fail) | 
|  | 57 | **/ | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 58 | int | 
|  | 59 | lpfc_mem_alloc(struct lpfc_hba * phba) | 
|  | 60 | { | 
|  | 61 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 62 | int longs; | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 63 | int i; | 
|  | 64 |  | 
|  | 65 | phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool", | 
|  | 66 | phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0); | 
|  | 67 | if (!phba->lpfc_scsi_dma_buf_pool) | 
|  | 68 | goto fail; | 
|  | 69 |  | 
|  | 70 | phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev, | 
|  | 71 | LPFC_BPL_SIZE, 8,0); | 
|  | 72 | if (!phba->lpfc_mbuf_pool) | 
|  | 73 | goto fail_free_dma_buf_pool; | 
|  | 74 |  | 
|  | 75 | pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) * | 
|  | 76 | LPFC_MBUF_POOL_SIZE, GFP_KERNEL); | 
| Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 77 | if (!pool->elements) | 
|  | 78 | goto fail_free_lpfc_mbuf_pool; | 
|  | 79 |  | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 80 | pool->max_count = 0; | 
|  | 81 | pool->current_count = 0; | 
|  | 82 | for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) { | 
|  | 83 | pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool, | 
|  | 84 | GFP_KERNEL, &pool->elements[i].phys); | 
|  | 85 | if (!pool->elements[i].virt) | 
|  | 86 | goto fail_free_mbuf_pool; | 
|  | 87 | pool->max_count++; | 
|  | 88 | pool->current_count++; | 
|  | 89 | } | 
|  | 90 |  | 
| Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 91 | phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, | 
|  | 92 | sizeof(LPFC_MBOXQ_t)); | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 93 | if (!phba->mbox_mem_pool) | 
|  | 94 | goto fail_free_mbuf_pool; | 
|  | 95 |  | 
| Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 96 | phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, | 
|  | 97 | sizeof(struct lpfc_nodelist)); | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 98 | if (!phba->nlp_mem_pool) | 
|  | 99 | goto fail_free_mbox_pool; | 
|  | 100 |  | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 101 | phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev, | 
|  | 102 | LPFC_BPL_SIZE, 8, 0); | 
|  | 103 | if (!phba->lpfc_hbq_pool) | 
|  | 104 | goto fail_free_nlp_mem_pool; | 
|  | 105 |  | 
| James Smart | 858c9f6 | 2007-06-17 19:56:39 -0500 | [diff] [blame] | 106 | /* vpi zero is reserved for the physical port so add 1 to max */ | 
|  | 107 | longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG; | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 108 | phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL); | 
|  | 109 | if (!phba->vpi_bmask) | 
|  | 110 | goto fail_free_hbq_pool; | 
|  | 111 |  | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 112 | return 0; | 
|  | 113 |  | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 114 | fail_free_hbq_pool: | 
|  | 115 | lpfc_sli_hbqbuf_free_all(phba); | 
| James Smart | 0937282 | 2008-01-11 01:52:54 -0500 | [diff] [blame] | 116 | pci_pool_destroy(phba->lpfc_hbq_pool); | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 117 | fail_free_nlp_mem_pool: | 
|  | 118 | mempool_destroy(phba->nlp_mem_pool); | 
|  | 119 | phba->nlp_mem_pool = NULL; | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 120 | fail_free_mbox_pool: | 
|  | 121 | mempool_destroy(phba->mbox_mem_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 122 | phba->mbox_mem_pool = NULL; | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 123 | fail_free_mbuf_pool: | 
| Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 124 | while (i--) | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 125 | pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, | 
|  | 126 | pool->elements[i].phys); | 
|  | 127 | kfree(pool->elements); | 
| Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 128 | fail_free_lpfc_mbuf_pool: | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 129 | pci_pool_destroy(phba->lpfc_mbuf_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 130 | phba->lpfc_mbuf_pool = NULL; | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 131 | fail_free_dma_buf_pool: | 
|  | 132 | pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 133 | phba->lpfc_scsi_dma_buf_pool = NULL; | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 134 | fail: | 
|  | 135 | return -ENOMEM; | 
|  | 136 | } | 
|  | 137 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 138 | /** | 
|  | 139 | * lpfc_mem_free: Frees all PCI and memory allocated by lpfc_mem_alloc | 
|  | 140 | * @phba: HBA to free memory for | 
|  | 141 | * | 
|  | 142 | * Description: Frees PCI pools lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, | 
|  | 143 | * lpfc_hbq_pool.  Frees kmalloc-backed mempools for LPFC_MBOXQ_t and | 
|  | 144 | * lpfc_nodelist.  Also frees the VPI bitmask. | 
|  | 145 | * | 
|  | 146 | * Returns: None | 
|  | 147 | **/ | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 148 | void | 
|  | 149 | lpfc_mem_free(struct lpfc_hba * phba) | 
|  | 150 | { | 
|  | 151 | struct lpfc_sli *psli = &phba->sli; | 
|  | 152 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
|  | 153 | LPFC_MBOXQ_t *mbox, *next_mbox; | 
|  | 154 | struct lpfc_dmabuf   *mp; | 
|  | 155 | int i; | 
|  | 156 |  | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 157 | kfree(phba->vpi_bmask); | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 158 | lpfc_sli_hbqbuf_free_all(phba); | 
|  | 159 |  | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 160 | list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) { | 
|  | 161 | mp = (struct lpfc_dmabuf *) (mbox->context1); | 
|  | 162 | if (mp) { | 
|  | 163 | lpfc_mbuf_free(phba, mp->virt, mp->phys); | 
|  | 164 | kfree(mp); | 
|  | 165 | } | 
|  | 166 | list_del(&mbox->list); | 
|  | 167 | mempool_free(mbox, phba->mbox_mem_pool); | 
|  | 168 | } | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 169 | list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) { | 
|  | 170 | mp = (struct lpfc_dmabuf *) (mbox->context1); | 
|  | 171 | if (mp) { | 
|  | 172 | lpfc_mbuf_free(phba, mp->virt, mp->phys); | 
|  | 173 | kfree(mp); | 
|  | 174 | } | 
|  | 175 | list_del(&mbox->list); | 
|  | 176 | mempool_free(mbox, phba->mbox_mem_pool); | 
|  | 177 | } | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 178 |  | 
|  | 179 | psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE; | 
|  | 180 | if (psli->mbox_active) { | 
|  | 181 | mbox = psli->mbox_active; | 
|  | 182 | mp = (struct lpfc_dmabuf *) (mbox->context1); | 
|  | 183 | if (mp) { | 
|  | 184 | lpfc_mbuf_free(phba, mp->virt, mp->phys); | 
|  | 185 | kfree(mp); | 
|  | 186 | } | 
|  | 187 | mempool_free(mbox, phba->mbox_mem_pool); | 
|  | 188 | psli->mbox_active = NULL; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | for (i = 0; i < pool->current_count; i++) | 
|  | 192 | pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, | 
|  | 193 | pool->elements[i].phys); | 
|  | 194 | kfree(pool->elements); | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 195 |  | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 196 | pci_pool_destroy(phba->lpfc_hbq_pool); | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 197 | mempool_destroy(phba->nlp_mem_pool); | 
|  | 198 | mempool_destroy(phba->mbox_mem_pool); | 
|  | 199 |  | 
|  | 200 | pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); | 
|  | 201 | pci_pool_destroy(phba->lpfc_mbuf_pool); | 
| James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 202 |  | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 203 | phba->lpfc_hbq_pool = NULL; | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 204 | phba->nlp_mem_pool = NULL; | 
|  | 205 | phba->mbox_mem_pool = NULL; | 
|  | 206 | phba->lpfc_scsi_dma_buf_pool = NULL; | 
|  | 207 | phba->lpfc_mbuf_pool = NULL; | 
|  | 208 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 209 | /* Free the iocb lookup array */ | 
| James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 210 | kfree(psli->iocbq_lookup); | 
|  | 211 | psli->iocbq_lookup = NULL; | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 214 | /** | 
|  | 215 | * lpfc_mbuf_alloc: Allocate an mbuf from the lpfc_mbuf_pool PCI pool | 
|  | 216 | * @phba: HBA which owns the pool to allocate from | 
|  | 217 | * @mem_flags: indicates if this is a priority (MEM_PRI) allocation | 
|  | 218 | * @handle: used to return the DMA-mapped address of the mbuf | 
|  | 219 | * | 
|  | 220 | * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool. | 
|  | 221 | * Allocates from generic pci_pool_alloc function first and if that fails and | 
|  | 222 | * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the | 
|  | 223 | * HBA's pool. | 
|  | 224 | * | 
|  | 225 | * Notes: Not interrupt-safe.  Must be called with no locks held.  Takes | 
|  | 226 | * phba->hbalock. | 
|  | 227 | * | 
|  | 228 | * Returns: | 
|  | 229 | *   pointer to the allocated mbuf on success | 
|  | 230 | *   NULL on failure | 
|  | 231 | **/ | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 232 | void * | 
|  | 233 | lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle) | 
|  | 234 | { | 
|  | 235 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 236 | unsigned long iflags; | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 237 | void *ret; | 
|  | 238 |  | 
|  | 239 | ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle); | 
|  | 240 |  | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 241 | spin_lock_irqsave(&phba->hbalock, iflags); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 242 | if (!ret && (mem_flags & MEM_PRI) && pool->current_count) { | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 243 | pool->current_count--; | 
|  | 244 | ret = pool->elements[pool->current_count].virt; | 
|  | 245 | *handle = pool->elements[pool->current_count].phys; | 
|  | 246 | } | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 247 | spin_unlock_irqrestore(&phba->hbalock, iflags); | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 248 | return ret; | 
|  | 249 | } | 
|  | 250 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 251 | /** | 
|  | 252 | * __lpfc_mem_free: Free an mbuf from the lpfc_mbuf_pool PCI pool (locked) | 
|  | 253 | * @phba: HBA which owns the pool to return to | 
|  | 254 | * @virt: mbuf to free | 
|  | 255 | * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed | 
|  | 256 | * | 
|  | 257 | * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if | 
|  | 258 | * it is below its max_count, frees the mbuf otherwise. | 
|  | 259 | * | 
|  | 260 | * Notes: Must be called with phba->hbalock held to synchronize access to | 
|  | 261 | * lpfc_mbuf_safety_pool. | 
|  | 262 | * | 
|  | 263 | * Returns: None | 
|  | 264 | **/ | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 265 | void | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 266 | __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) | 
|  | dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 267 | { | 
|  | 268 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; | 
|  | 269 |  | 
|  | 270 | if (pool->current_count < pool->max_count) { | 
|  | 271 | pool->elements[pool->current_count].virt = virt; | 
|  | 272 | pool->elements[pool->current_count].phys = dma; | 
|  | 273 | pool->current_count++; | 
|  | 274 | } else { | 
|  | 275 | pci_pool_free(phba->lpfc_mbuf_pool, virt, dma); | 
|  | 276 | } | 
|  | 277 | return; | 
|  | 278 | } | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 279 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 280 | /** | 
|  | 281 | * lpfc_mem_free: Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked) | 
|  | 282 | * @phba: HBA which owns the pool to return to | 
|  | 283 | * @virt: mbuf to free | 
|  | 284 | * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed | 
|  | 285 | * | 
|  | 286 | * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if | 
|  | 287 | * it is below its max_count, frees the mbuf otherwise. | 
|  | 288 | * | 
|  | 289 | * Notes: Takes phba->hbalock.  Can be called with or without other locks held. | 
|  | 290 | * | 
|  | 291 | * Returns: None | 
|  | 292 | **/ | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 293 | void | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 294 |  | 
| James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 295 | lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) | 
|  | 296 | { | 
|  | 297 | unsigned long iflags; | 
|  | 298 |  | 
|  | 299 | spin_lock_irqsave(&phba->hbalock, iflags); | 
|  | 300 | __lpfc_mbuf_free(phba, virt, dma); | 
|  | 301 | spin_unlock_irqrestore(&phba->hbalock, iflags); | 
|  | 302 | return; | 
|  | 303 | } | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 304 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 305 | /** | 
|  | 306 | * lpfc_els_hbq_alloc: Allocate an HBQ buffer | 
|  | 307 | * @phba: HBA to allocate HBQ buffer for | 
|  | 308 | * | 
|  | 309 | * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hbq_pool PCI | 
|  | 310 | * pool along a non-DMA-mapped container for it. | 
|  | 311 | * | 
|  | 312 | * Notes: Not interrupt-safe.  Must be called with no locks held. | 
|  | 313 | * | 
|  | 314 | * Returns: | 
|  | 315 | *   pointer to HBQ on success | 
|  | 316 | *   NULL on failure | 
|  | 317 | **/ | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 318 | struct hbq_dmabuf * | 
|  | 319 | lpfc_els_hbq_alloc(struct lpfc_hba *phba) | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 320 | { | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 321 | struct hbq_dmabuf *hbqbp; | 
|  | 322 |  | 
|  | 323 | hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL); | 
|  | 324 | if (!hbqbp) | 
|  | 325 | return NULL; | 
|  | 326 |  | 
|  | 327 | hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL, | 
|  | 328 | &hbqbp->dbuf.phys); | 
|  | 329 | if (!hbqbp->dbuf.virt) { | 
|  | 330 | kfree(hbqbp); | 
|  | 331 | return NULL; | 
|  | 332 | } | 
|  | 333 | hbqbp->size = LPFC_BPL_SIZE; | 
|  | 334 | return hbqbp; | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 337 | /** | 
|  | 338 | * lpfc_mem_hbq_free: Frees an HBQ buffer allocated with lpfc_els_hbq_alloc | 
|  | 339 | * @phba: HBA buffer was allocated for | 
|  | 340 | * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc | 
|  | 341 | * | 
|  | 342 | * Description: Frees both the container and the DMA-mapped buffer returned by | 
|  | 343 | * lpfc_els_hbq_alloc. | 
|  | 344 | * | 
|  | 345 | * Notes: Can be called with or without locks held. | 
|  | 346 | * | 
|  | 347 | * Returns: None | 
|  | 348 | **/ | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 349 | void | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 350 | lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp) | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 351 | { | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 352 | pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys); | 
|  | 353 | kfree(hbqbp); | 
| James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 354 | return; | 
|  | 355 | } | 
|  | 356 |  | 
| James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 357 | /** | 
|  | 358 | * lpfc_in_buf_free: Free a DMA buffer | 
|  | 359 | * @phba: HBA buffer is associated with | 
|  | 360 | * @mp: Buffer to free | 
|  | 361 | * | 
|  | 362 | * Description: Frees the given DMA buffer in the appropriate way given if the | 
|  | 363 | * HBA is running in SLI3 mode with HBQs enabled. | 
|  | 364 | * | 
|  | 365 | * Notes: Takes phba->hbalock.  Can be called with or without other locks held. | 
|  | 366 | * | 
|  | 367 | * Returns: None | 
|  | 368 | **/ | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 369 | void | 
|  | 370 | lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp) | 
|  | 371 | { | 
|  | 372 | struct hbq_dmabuf *hbq_entry; | 
| James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 373 | unsigned long flags; | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 374 |  | 
| James Smart | 7f5f3d0 | 2008-02-08 18:50:14 -0500 | [diff] [blame] | 375 | if (!mp) | 
|  | 376 | return; | 
|  | 377 |  | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 378 | if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) { | 
| James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 379 | /* Check whether HBQ is still in use */ | 
|  | 380 | spin_lock_irqsave(&phba->hbalock, flags); | 
|  | 381 | if (!phba->hbq_in_use) { | 
|  | 382 | spin_unlock_irqrestore(&phba->hbalock, flags); | 
|  | 383 | return; | 
|  | 384 | } | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 385 | hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf); | 
| James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 386 | list_del(&hbq_entry->dbuf.list); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 387 | if (hbq_entry->tag == -1) { | 
| James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 388 | (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer) | 
|  | 389 | (phba, hbq_entry); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 390 | } else { | 
|  | 391 | lpfc_sli_free_hbq(phba, hbq_entry); | 
|  | 392 | } | 
| James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 393 | spin_unlock_irqrestore(&phba->hbalock, flags); | 
| James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 394 | } else { | 
|  | 395 | lpfc_mbuf_free(phba, mp->virt, mp->phys); | 
|  | 396 | kfree(mp); | 
|  | 397 | } | 
|  | 398 | return; | 
|  | 399 | } |