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