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