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