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