| 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 | 128bdda | 2018-01-30 15:59:03 -0800 | [diff] [blame] | 4 | * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term * |
James Smart | 4ae2ebd | 2018-06-26 08:24:31 -0700 | [diff] [blame] | 5 | * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. * |
James Smart | b857ff3 | 2014-04-04 13:52:50 -0400 | [diff] [blame] | 6 | * Copyright (C) 2004-2014 Emulex. All rights reserved. * |
James.Smart@Emulex.Com | c44ce17 | 2005-06-25 10:34:39 -0400 | [diff] [blame] | 7 | * EMULEX and SLI are trademarks of Emulex. * |
James Smart | d080abe | 2017-02-12 13:52:39 -0800 | [diff] [blame] | 8 | * www.broadcom.com * |
James.Smart@Emulex.Com | c44ce17 | 2005-06-25 10:34:39 -0400 | [diff] [blame] | 9 | * Portions Copyright (C) 2004-2005 Christoph Hellwig * |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 10 | * * |
| 11 | * 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] | 12 | * modify it under the terms of version 2 of the GNU General * |
| 13 | * Public License as published by the Free Software Foundation. * |
| 14 | * This program is distributed in the hope that it will be useful. * |
| 15 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * |
| 16 | * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE * |
| 18 | * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD * |
| 19 | * TO BE LEGALLY INVALID. See the GNU General Public License for * |
| 20 | * more details, a copy of which can be found in the file COPYING * |
| 21 | * included with this package. * |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 22 | *******************************************************************/ |
| 23 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 24 | #include <linux/mempool.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 26 | #include <linux/pci.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 29 | #include <scsi/scsi.h> |
James.Smart@Emulex.Com | f888ba3 | 2005-08-10 15:03:01 -0400 | [diff] [blame] | 30 | #include <scsi/scsi_device.h> |
| 31 | #include <scsi/scsi_transport_fc.h> |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 32 | #include <scsi/fc/fc_fs.h> |
James.Smart@Emulex.Com | f888ba3 | 2005-08-10 15:03:01 -0400 | [diff] [blame] | 33 | |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 34 | #include <linux/nvme-fc-driver.h> |
James.Smart@Emulex.Com | 9188652 | 2005-08-10 15:03:09 -0400 | [diff] [blame] | 35 | |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 36 | #include "lpfc_hw4.h" |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 37 | #include "lpfc_hw.h" |
| 38 | #include "lpfc_sli.h" |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 39 | #include "lpfc_sli4.h" |
James Smart | ea2151b | 2008-09-07 11:52:10 -0400 | [diff] [blame] | 40 | #include "lpfc_nl.h" |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 41 | #include "lpfc_disc.h" |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 42 | #include "lpfc.h" |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 43 | #include "lpfc_scsi.h" |
| 44 | #include "lpfc_nvme.h" |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 45 | #include "lpfc_nvmet.h" |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 46 | #include "lpfc_crtn.h" |
James Smart | cff261f | 2013-12-17 20:29:47 -0500 | [diff] [blame] | 47 | #include "lpfc_logmsg.h" |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 48 | |
| 49 | #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */ |
| 50 | #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] | 51 | #define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */ |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 52 | |
James Smart | cff261f | 2013-12-17 20:29:47 -0500 | [diff] [blame] | 53 | int |
| 54 | lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) { |
| 55 | size_t bytes; |
| 56 | int max_xri = phba->sli4_hba.max_cfg_param.max_xri; |
| 57 | |
| 58 | if (max_xri <= 0) |
| 59 | return -ENOMEM; |
| 60 | bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) * |
| 61 | sizeof(unsigned long); |
| 62 | phba->cfg_rrq_xri_bitmap_sz = bytes; |
| 63 | phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, |
| 64 | bytes); |
| 65 | if (!phba->active_rrq_pool) |
| 66 | return -ENOMEM; |
| 67 | else |
| 68 | return 0; |
| 69 | } |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 70 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 71 | /** |
James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 72 | * lpfc_mem_alloc - create and allocate all PCI and memory pools |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 73 | * @phba: HBA to allocate pools for |
| 74 | * |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 75 | * Description: Creates and allocates PCI pools lpfc_sg_dma_buf_pool, |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 76 | * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 77 | * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask. |
| 78 | * |
| 79 | * Notes: Not interrupt-safe. Must be called with no locks held. If any |
| 80 | * allocation fails, frees all successfully allocated memory before returning. |
| 81 | * |
| 82 | * Returns: |
| 83 | * 0 on success |
| 84 | * -ENOMEM on failure (if any memory allocations fail) |
| 85 | **/ |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 86 | int |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 87 | lpfc_mem_alloc(struct lpfc_hba *phba, int align) |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 88 | { |
| 89 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 90 | int i; |
| 91 | |
James Smart | 96f7077 | 2013-04-17 20:16:15 -0400 | [diff] [blame] | 92 | if (phba->sli_rev == LPFC_SLI_REV4) { |
| 93 | /* Calculate alignment */ |
| 94 | if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE) |
| 95 | i = phba->cfg_sg_dma_buf_size; |
| 96 | else |
| 97 | i = SLI4_PAGE_SIZE; |
| 98 | |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 99 | phba->lpfc_sg_dma_buf_pool = |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 100 | dma_pool_create("lpfc_sg_dma_buf_pool", |
| 101 | &phba->pcidev->dev, |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 102 | phba->cfg_sg_dma_buf_size, |
| 103 | i, 0); |
| 104 | if (!phba->lpfc_sg_dma_buf_pool) |
| 105 | goto fail; |
James Smart | 96f7077 | 2013-04-17 20:16:15 -0400 | [diff] [blame] | 106 | |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 107 | } else { |
| 108 | phba->lpfc_sg_dma_buf_pool = |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 109 | dma_pool_create("lpfc_sg_dma_buf_pool", |
| 110 | &phba->pcidev->dev, phba->cfg_sg_dma_buf_size, |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 111 | align, 0); |
| 112 | |
| 113 | if (!phba->lpfc_sg_dma_buf_pool) |
| 114 | goto fail; |
| 115 | } |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 116 | |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 117 | phba->lpfc_mbuf_pool = dma_pool_create("lpfc_mbuf_pool", &phba->pcidev->dev, |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 118 | LPFC_BPL_SIZE, |
| 119 | align, 0); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 120 | if (!phba->lpfc_mbuf_pool) |
| 121 | goto fail_free_dma_buf_pool; |
| 122 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 123 | pool->elements = kmalloc_array(LPFC_MBUF_POOL_SIZE, |
| 124 | sizeof(struct lpfc_dmabuf), |
| 125 | GFP_KERNEL); |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 126 | if (!pool->elements) |
| 127 | goto fail_free_lpfc_mbuf_pool; |
| 128 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 129 | pool->max_count = 0; |
| 130 | pool->current_count = 0; |
| 131 | for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 132 | pool->elements[i].virt = dma_pool_alloc(phba->lpfc_mbuf_pool, |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 133 | GFP_KERNEL, &pool->elements[i].phys); |
| 134 | if (!pool->elements[i].virt) |
| 135 | goto fail_free_mbuf_pool; |
| 136 | pool->max_count++; |
| 137 | pool->current_count++; |
| 138 | } |
| 139 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 140 | phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, |
| 141 | sizeof(LPFC_MBOXQ_t)); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 142 | if (!phba->mbox_mem_pool) |
| 143 | goto fail_free_mbuf_pool; |
| 144 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 145 | phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, |
| 146 | sizeof(struct lpfc_nodelist)); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 147 | if (!phba->nlp_mem_pool) |
| 148 | goto fail_free_mbox_pool; |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 149 | |
| 150 | if (phba->sli_rev == LPFC_SLI_REV4) { |
James Smart | 19ca760 | 2010-11-20 23:11:55 -0500 | [diff] [blame] | 151 | phba->rrq_pool = |
| 152 | mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, |
| 153 | sizeof(struct lpfc_node_rrq)); |
| 154 | if (!phba->rrq_pool) |
| 155 | goto fail_free_nlp_mem_pool; |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 156 | phba->lpfc_hrb_pool = dma_pool_create("lpfc_hrb_pool", |
| 157 | &phba->pcidev->dev, |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 158 | LPFC_HDR_BUF_SIZE, align, 0); |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 159 | if (!phba->lpfc_hrb_pool) |
James Smart | 19ca760 | 2010-11-20 23:11:55 -0500 | [diff] [blame] | 160 | goto fail_free_rrq_mem_pool; |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 161 | |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 162 | phba->lpfc_drb_pool = dma_pool_create("lpfc_drb_pool", |
| 163 | &phba->pcidev->dev, |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 164 | LPFC_DATA_BUF_SIZE, align, 0); |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 165 | if (!phba->lpfc_drb_pool) |
| 166 | goto fail_free_hrb_pool; |
| 167 | phba->lpfc_hbq_pool = NULL; |
| 168 | } else { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 169 | phba->lpfc_hbq_pool = dma_pool_create("lpfc_hbq_pool", |
| 170 | &phba->pcidev->dev, LPFC_BPL_SIZE, align, 0); |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 171 | if (!phba->lpfc_hbq_pool) |
| 172 | goto fail_free_nlp_mem_pool; |
| 173 | phba->lpfc_hrb_pool = NULL; |
| 174 | phba->lpfc_drb_pool = NULL; |
| 175 | } |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 176 | |
James Smart | 1ba981f | 2014-02-20 09:56:45 -0500 | [diff] [blame] | 177 | if (phba->cfg_EnableXLane) { |
| 178 | phba->device_data_mem_pool = mempool_create_kmalloc_pool( |
| 179 | LPFC_DEVICE_DATA_POOL_SIZE, |
| 180 | sizeof(struct lpfc_device_data)); |
| 181 | if (!phba->device_data_mem_pool) |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 182 | goto fail_free_drb_pool; |
James Smart | 1ba981f | 2014-02-20 09:56:45 -0500 | [diff] [blame] | 183 | } else { |
| 184 | phba->device_data_mem_pool = NULL; |
| 185 | } |
| 186 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 187 | return 0; |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 188 | fail_free_drb_pool: |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 189 | dma_pool_destroy(phba->lpfc_drb_pool); |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 190 | phba->lpfc_drb_pool = NULL; |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 191 | fail_free_hrb_pool: |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 192 | dma_pool_destroy(phba->lpfc_hrb_pool); |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 193 | phba->lpfc_hrb_pool = NULL; |
James Smart | 19ca760 | 2010-11-20 23:11:55 -0500 | [diff] [blame] | 194 | fail_free_rrq_mem_pool: |
| 195 | mempool_destroy(phba->rrq_pool); |
| 196 | phba->rrq_pool = NULL; |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 197 | fail_free_nlp_mem_pool: |
| 198 | mempool_destroy(phba->nlp_mem_pool); |
| 199 | phba->nlp_mem_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 200 | fail_free_mbox_pool: |
| 201 | mempool_destroy(phba->mbox_mem_pool); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 202 | phba->mbox_mem_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 203 | fail_free_mbuf_pool: |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 204 | while (i--) |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 205 | dma_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 206 | pool->elements[i].phys); |
| 207 | kfree(pool->elements); |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 208 | fail_free_lpfc_mbuf_pool: |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 209 | dma_pool_destroy(phba->lpfc_mbuf_pool); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 210 | phba->lpfc_mbuf_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 211 | fail_free_dma_buf_pool: |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 212 | dma_pool_destroy(phba->lpfc_sg_dma_buf_pool); |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 213 | phba->lpfc_sg_dma_buf_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 214 | fail: |
| 215 | return -ENOMEM; |
| 216 | } |
| 217 | |
James Smart | 3c603be | 2017-05-15 15:20:44 -0700 | [diff] [blame] | 218 | int |
| 219 | lpfc_nvmet_mem_alloc(struct lpfc_hba *phba) |
| 220 | { |
| 221 | phba->lpfc_nvmet_drb_pool = |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 222 | dma_pool_create("lpfc_nvmet_drb_pool", |
| 223 | &phba->pcidev->dev, LPFC_NVMET_DATA_BUF_SIZE, |
James Smart | 3c603be | 2017-05-15 15:20:44 -0700 | [diff] [blame] | 224 | SGL_ALIGN_SZ, 0); |
| 225 | if (!phba->lpfc_nvmet_drb_pool) { |
| 226 | lpfc_printf_log(phba, KERN_ERR, LOG_INIT, |
| 227 | "6024 Can't enable NVME Target - no memory\n"); |
| 228 | return -ENOMEM; |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 233 | /** |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 234 | * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 235 | * @phba: HBA to free memory for |
| 236 | * |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 237 | * Description: Free the memory allocated by lpfc_mem_alloc routine. This |
| 238 | * routine is a the counterpart of lpfc_mem_alloc. |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 239 | * |
| 240 | * Returns: None |
| 241 | **/ |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 242 | void |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 243 | lpfc_mem_free(struct lpfc_hba *phba) |
| 244 | { |
| 245 | int i; |
| 246 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
James Smart | 1ba981f | 2014-02-20 09:56:45 -0500 | [diff] [blame] | 247 | struct lpfc_device_data *device_data; |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 248 | |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 249 | /* Free HBQ pools */ |
| 250 | lpfc_sli_hbqbuf_free_all(phba); |
James Smart | 3c603be | 2017-05-15 15:20:44 -0700 | [diff] [blame] | 251 | if (phba->lpfc_nvmet_drb_pool) |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 252 | dma_pool_destroy(phba->lpfc_nvmet_drb_pool); |
James Smart | 3c603be | 2017-05-15 15:20:44 -0700 | [diff] [blame] | 253 | phba->lpfc_nvmet_drb_pool = NULL; |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 254 | if (phba->lpfc_drb_pool) |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 255 | dma_pool_destroy(phba->lpfc_drb_pool); |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 256 | phba->lpfc_drb_pool = NULL; |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 257 | if (phba->lpfc_hrb_pool) |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 258 | dma_pool_destroy(phba->lpfc_hrb_pool); |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 259 | phba->lpfc_hrb_pool = NULL; |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 260 | if (phba->txrdy_payload_pool) |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 261 | dma_pool_destroy(phba->txrdy_payload_pool); |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 262 | phba->txrdy_payload_pool = NULL; |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 263 | |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 264 | if (phba->lpfc_hbq_pool) |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 265 | dma_pool_destroy(phba->lpfc_hbq_pool); |
James Smart | 8568a4d | 2009-07-19 10:01:16 -0400 | [diff] [blame] | 266 | phba->lpfc_hbq_pool = NULL; |
Martin K. Petersen | d65c8ff | 2016-05-09 21:39:43 -0400 | [diff] [blame] | 267 | |
| 268 | if (phba->rrq_pool) |
| 269 | mempool_destroy(phba->rrq_pool); |
James Smart | 21a688c | 2012-08-03 12:34:15 -0400 | [diff] [blame] | 270 | phba->rrq_pool = NULL; |
| 271 | |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 272 | /* Free NLP memory pool */ |
| 273 | mempool_destroy(phba->nlp_mem_pool); |
| 274 | phba->nlp_mem_pool = NULL; |
Martin K. Petersen | d65c8ff | 2016-05-09 21:39:43 -0400 | [diff] [blame] | 275 | if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) { |
James Smart | cff261f | 2013-12-17 20:29:47 -0500 | [diff] [blame] | 276 | mempool_destroy(phba->active_rrq_pool); |
| 277 | phba->active_rrq_pool = NULL; |
| 278 | } |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 279 | |
| 280 | /* Free mbox memory pool */ |
| 281 | mempool_destroy(phba->mbox_mem_pool); |
| 282 | phba->mbox_mem_pool = NULL; |
| 283 | |
| 284 | /* Free MBUF memory pool */ |
| 285 | for (i = 0; i < pool->current_count; i++) |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 286 | dma_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 287 | pool->elements[i].phys); |
| 288 | kfree(pool->elements); |
| 289 | |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 290 | dma_pool_destroy(phba->lpfc_mbuf_pool); |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 291 | phba->lpfc_mbuf_pool = NULL; |
| 292 | |
| 293 | /* Free DMA buffer memory pool */ |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 294 | dma_pool_destroy(phba->lpfc_sg_dma_buf_pool); |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 295 | phba->lpfc_sg_dma_buf_pool = NULL; |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 296 | |
James Smart | 1ba981f | 2014-02-20 09:56:45 -0500 | [diff] [blame] | 297 | /* Free Device Data memory pool */ |
| 298 | if (phba->device_data_mem_pool) { |
| 299 | /* Ensure all objects have been returned to the pool */ |
| 300 | while (!list_empty(&phba->luns)) { |
| 301 | device_data = list_first_entry(&phba->luns, |
| 302 | struct lpfc_device_data, |
| 303 | listentry); |
| 304 | list_del(&device_data->listentry); |
| 305 | mempool_free(device_data, phba->device_data_mem_pool); |
| 306 | } |
| 307 | mempool_destroy(phba->device_data_mem_pool); |
| 308 | } |
| 309 | phba->device_data_mem_pool = NULL; |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * lpfc_mem_free_all - Frees all PCI and driver memory |
| 315 | * @phba: HBA to free memory for |
| 316 | * |
| 317 | * Description: Free memory from PCI and driver memory pools and also those |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 318 | * 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] | 319 | * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees |
| 320 | * the VPI bitmask. |
| 321 | * |
| 322 | * Returns: None |
| 323 | **/ |
| 324 | void |
| 325 | lpfc_mem_free_all(struct lpfc_hba *phba) |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 326 | { |
| 327 | struct lpfc_sli *psli = &phba->sli; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 328 | LPFC_MBOXQ_t *mbox, *next_mbox; |
| 329 | struct lpfc_dmabuf *mp; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 330 | |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 331 | /* Free memory used in mailbox queue back to mailbox memory pool */ |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 332 | list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) { |
James Smart | 3e1f071 | 2018-11-29 16:09:29 -0800 | [diff] [blame] | 333 | mp = (struct lpfc_dmabuf *)(mbox->ctx_buf); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 334 | if (mp) { |
| 335 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 336 | kfree(mp); |
| 337 | } |
| 338 | list_del(&mbox->list); |
| 339 | mempool_free(mbox, phba->mbox_mem_pool); |
| 340 | } |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 341 | /* Free memory used in mailbox cmpl list back to mailbox memory pool */ |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 342 | list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) { |
James Smart | 3e1f071 | 2018-11-29 16:09:29 -0800 | [diff] [blame] | 343 | mp = (struct lpfc_dmabuf *)(mbox->ctx_buf); |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 344 | if (mp) { |
| 345 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 346 | kfree(mp); |
| 347 | } |
| 348 | list_del(&mbox->list); |
| 349 | mempool_free(mbox, phba->mbox_mem_pool); |
| 350 | } |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 351 | /* Free the active mailbox command back to the mailbox memory pool */ |
| 352 | spin_lock_irq(&phba->hbalock); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 353 | psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE; |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 354 | spin_unlock_irq(&phba->hbalock); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 355 | if (psli->mbox_active) { |
| 356 | mbox = psli->mbox_active; |
James Smart | 3e1f071 | 2018-11-29 16:09:29 -0800 | [diff] [blame] | 357 | mp = (struct lpfc_dmabuf *)(mbox->ctx_buf); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 358 | if (mp) { |
| 359 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 360 | kfree(mp); |
| 361 | } |
| 362 | mempool_free(mbox, phba->mbox_mem_pool); |
| 363 | psli->mbox_active = NULL; |
| 364 | } |
| 365 | |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 366 | /* Free and destroy all the allocated memory pools */ |
| 367 | lpfc_mem_free(phba); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 368 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 369 | /* Free the iocb lookup array */ |
James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 370 | kfree(psli->iocbq_lookup); |
| 371 | psli->iocbq_lookup = NULL; |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 372 | |
| 373 | return; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 374 | } |
| 375 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 376 | /** |
James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 377 | * 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] | 378 | * @phba: HBA which owns the pool to allocate from |
| 379 | * @mem_flags: indicates if this is a priority (MEM_PRI) allocation |
| 380 | * @handle: used to return the DMA-mapped address of the mbuf |
| 381 | * |
| 382 | * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool. |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 383 | * Allocates from generic dma_pool_alloc function first and if that fails and |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 384 | * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the |
| 385 | * HBA's pool. |
| 386 | * |
| 387 | * Notes: Not interrupt-safe. Must be called with no locks held. Takes |
| 388 | * phba->hbalock. |
| 389 | * |
| 390 | * Returns: |
| 391 | * pointer to the allocated mbuf on success |
| 392 | * NULL on failure |
| 393 | **/ |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 394 | void * |
| 395 | lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle) |
| 396 | { |
| 397 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 398 | unsigned long iflags; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 399 | void *ret; |
| 400 | |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 401 | ret = dma_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 402 | |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 403 | spin_lock_irqsave(&phba->hbalock, iflags); |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 404 | if (!ret && (mem_flags & MEM_PRI) && pool->current_count) { |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 405 | pool->current_count--; |
| 406 | ret = pool->elements[pool->current_count].virt; |
| 407 | *handle = pool->elements[pool->current_count].phys; |
| 408 | } |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 409 | spin_unlock_irqrestore(&phba->hbalock, iflags); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 410 | return ret; |
| 411 | } |
| 412 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 413 | /** |
James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 414 | * __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] | 415 | * @phba: HBA which owns the pool to return to |
| 416 | * @virt: mbuf to free |
| 417 | * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed |
| 418 | * |
| 419 | * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if |
| 420 | * it is below its max_count, frees the mbuf otherwise. |
| 421 | * |
| 422 | * Notes: Must be called with phba->hbalock held to synchronize access to |
| 423 | * lpfc_mbuf_safety_pool. |
| 424 | * |
| 425 | * Returns: None |
| 426 | **/ |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 427 | void |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 428 | __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 429 | { |
| 430 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 431 | |
| 432 | if (pool->current_count < pool->max_count) { |
| 433 | pool->elements[pool->current_count].virt = virt; |
| 434 | pool->elements[pool->current_count].phys = dma; |
| 435 | pool->current_count++; |
| 436 | } else { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 437 | dma_pool_free(phba->lpfc_mbuf_pool, virt, dma); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 438 | } |
| 439 | return; |
| 440 | } |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 441 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 442 | /** |
James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 443 | * 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] | 444 | * @phba: HBA which owns the pool to return to |
| 445 | * @virt: mbuf to free |
| 446 | * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed |
| 447 | * |
| 448 | * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if |
| 449 | * it is below its max_count, frees the mbuf otherwise. |
| 450 | * |
| 451 | * Notes: Takes phba->hbalock. Can be called with or without other locks held. |
| 452 | * |
| 453 | * Returns: None |
| 454 | **/ |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 455 | void |
| 456 | lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) |
| 457 | { |
| 458 | unsigned long iflags; |
| 459 | |
| 460 | spin_lock_irqsave(&phba->hbalock, iflags); |
| 461 | __lpfc_mbuf_free(phba, virt, dma); |
| 462 | spin_unlock_irqrestore(&phba->hbalock, iflags); |
| 463 | return; |
| 464 | } |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 465 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 466 | /** |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 467 | * lpfc_nvmet_buf_alloc - Allocate an nvmet_buf from the |
| 468 | * lpfc_sg_dma_buf_pool PCI pool |
| 469 | * @phba: HBA which owns the pool to allocate from |
| 470 | * @mem_flags: indicates if this is a priority (MEM_PRI) allocation |
| 471 | * @handle: used to return the DMA-mapped address of the nvmet_buf |
| 472 | * |
| 473 | * Description: Allocates a DMA-mapped buffer from the lpfc_sg_dma_buf_pool |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 474 | * PCI pool. Allocates from generic dma_pool_alloc function. |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 475 | * |
| 476 | * Returns: |
| 477 | * pointer to the allocated nvmet_buf on success |
| 478 | * NULL on failure |
| 479 | **/ |
| 480 | void * |
| 481 | lpfc_nvmet_buf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle) |
| 482 | { |
| 483 | void *ret; |
| 484 | |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 485 | ret = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool, GFP_KERNEL, handle); |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * lpfc_nvmet_buf_free - Free an nvmet_buf from the lpfc_sg_dma_buf_pool |
| 491 | * PCI pool |
| 492 | * @phba: HBA which owns the pool to return to |
| 493 | * @virt: nvmet_buf to free |
| 494 | * @dma: the DMA-mapped address of the lpfc_sg_dma_buf_pool to be freed |
| 495 | * |
| 496 | * Returns: None |
| 497 | **/ |
| 498 | void |
| 499 | lpfc_nvmet_buf_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma) |
| 500 | { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 501 | dma_pool_free(phba->lpfc_sg_dma_buf_pool, virt, dma); |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /** |
James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 505 | * lpfc_els_hbq_alloc - Allocate an HBQ buffer |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 506 | * @phba: HBA to allocate HBQ buffer for |
| 507 | * |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 508 | * 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] | 509 | * pool along a non-DMA-mapped container for it. |
| 510 | * |
| 511 | * Notes: Not interrupt-safe. Must be called with no locks held. |
| 512 | * |
| 513 | * Returns: |
| 514 | * pointer to HBQ on success |
| 515 | * NULL on failure |
| 516 | **/ |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 517 | struct hbq_dmabuf * |
| 518 | lpfc_els_hbq_alloc(struct lpfc_hba *phba) |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 519 | { |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 520 | struct hbq_dmabuf *hbqbp; |
| 521 | |
James Smart | 2e90f4b | 2011-12-13 13:22:37 -0500 | [diff] [blame] | 522 | hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL); |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 523 | if (!hbqbp) |
| 524 | return NULL; |
| 525 | |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 526 | hbqbp->dbuf.virt = dma_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL, |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 527 | &hbqbp->dbuf.phys); |
| 528 | if (!hbqbp->dbuf.virt) { |
| 529 | kfree(hbqbp); |
| 530 | return NULL; |
| 531 | } |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 532 | hbqbp->total_size = LPFC_BPL_SIZE; |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 533 | return hbqbp; |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 534 | } |
| 535 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 536 | /** |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 537 | * 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] | 538 | * @phba: HBA buffer was allocated for |
| 539 | * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc |
| 540 | * |
| 541 | * Description: Frees both the container and the DMA-mapped buffer returned by |
| 542 | * lpfc_els_hbq_alloc. |
| 543 | * |
| 544 | * Notes: Can be called with or without locks held. |
| 545 | * |
| 546 | * Returns: None |
| 547 | **/ |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 548 | void |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 549 | lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp) |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 550 | { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 551 | dma_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys); |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 552 | kfree(hbqbp); |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 556 | /** |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 557 | * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer |
| 558 | * @phba: HBA to allocate a receive buffer for |
| 559 | * |
| 560 | * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI |
| 561 | * pool along a non-DMA-mapped container for it. |
| 562 | * |
| 563 | * Notes: Not interrupt-safe. Must be called with no locks held. |
| 564 | * |
| 565 | * Returns: |
| 566 | * pointer to HBQ on success |
| 567 | * NULL on failure |
| 568 | **/ |
| 569 | struct hbq_dmabuf * |
| 570 | lpfc_sli4_rb_alloc(struct lpfc_hba *phba) |
| 571 | { |
| 572 | struct hbq_dmabuf *dma_buf; |
| 573 | |
James Smart | 2e90f4b | 2011-12-13 13:22:37 -0500 | [diff] [blame] | 574 | dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL); |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 575 | if (!dma_buf) |
| 576 | return NULL; |
| 577 | |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 578 | dma_buf->hbuf.virt = dma_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL, |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 579 | &dma_buf->hbuf.phys); |
| 580 | if (!dma_buf->hbuf.virt) { |
| 581 | kfree(dma_buf); |
| 582 | return NULL; |
| 583 | } |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 584 | dma_buf->dbuf.virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL, |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 585 | &dma_buf->dbuf.phys); |
| 586 | if (!dma_buf->dbuf.virt) { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 587 | dma_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt, |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 588 | dma_buf->hbuf.phys); |
| 589 | kfree(dma_buf); |
| 590 | return NULL; |
| 591 | } |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 592 | dma_buf->total_size = LPFC_DATA_BUF_SIZE; |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 593 | return dma_buf; |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * lpfc_sli4_rb_free - Frees a receive buffer |
| 598 | * @phba: HBA buffer was allocated for |
| 599 | * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc |
| 600 | * |
| 601 | * Description: Frees both the container and the DMA-mapped buffers returned by |
| 602 | * lpfc_sli4_rb_alloc. |
| 603 | * |
| 604 | * Notes: Can be called with or without locks held. |
| 605 | * |
| 606 | * Returns: None |
| 607 | **/ |
| 608 | void |
| 609 | lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab) |
| 610 | { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 611 | dma_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys); |
| 612 | dma_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys); |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 613 | kfree(dmab); |
James Smart | da0436e | 2009-05-22 14:51:39 -0400 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 617 | * lpfc_sli4_nvmet_alloc - Allocate an SLI4 Receive buffer |
| 618 | * @phba: HBA to allocate a receive buffer for |
| 619 | * |
| 620 | * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI |
| 621 | * pool along a non-DMA-mapped container for it. |
| 622 | * |
| 623 | * Notes: Not interrupt-safe. Must be called with no locks held. |
| 624 | * |
| 625 | * Returns: |
| 626 | * pointer to HBQ on success |
| 627 | * NULL on failure |
| 628 | **/ |
| 629 | struct rqb_dmabuf * |
| 630 | lpfc_sli4_nvmet_alloc(struct lpfc_hba *phba) |
| 631 | { |
| 632 | struct rqb_dmabuf *dma_buf; |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 633 | |
| 634 | dma_buf = kzalloc(sizeof(struct rqb_dmabuf), GFP_KERNEL); |
| 635 | if (!dma_buf) |
| 636 | return NULL; |
| 637 | |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 638 | dma_buf->hbuf.virt = dma_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL, |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 639 | &dma_buf->hbuf.phys); |
| 640 | if (!dma_buf->hbuf.virt) { |
| 641 | kfree(dma_buf); |
| 642 | return NULL; |
| 643 | } |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 644 | dma_buf->dbuf.virt = dma_pool_alloc(phba->lpfc_nvmet_drb_pool, |
James Smart | 3c603be | 2017-05-15 15:20:44 -0700 | [diff] [blame] | 645 | GFP_KERNEL, &dma_buf->dbuf.phys); |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 646 | if (!dma_buf->dbuf.virt) { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 647 | dma_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt, |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 648 | dma_buf->hbuf.phys); |
| 649 | kfree(dma_buf); |
| 650 | return NULL; |
| 651 | } |
James Smart | 3c603be | 2017-05-15 15:20:44 -0700 | [diff] [blame] | 652 | dma_buf->total_size = LPFC_NVMET_DATA_BUF_SIZE; |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 653 | return dma_buf; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * lpfc_sli4_nvmet_free - Frees a receive buffer |
| 658 | * @phba: HBA buffer was allocated for |
| 659 | * @dmab: DMA Buffer container returned by lpfc_sli4_rbq_alloc |
| 660 | * |
| 661 | * Description: Frees both the container and the DMA-mapped buffers returned by |
| 662 | * lpfc_sli4_nvmet_alloc. |
| 663 | * |
| 664 | * Notes: Can be called with or without locks held. |
| 665 | * |
| 666 | * Returns: None |
| 667 | **/ |
| 668 | void |
| 669 | lpfc_sli4_nvmet_free(struct lpfc_hba *phba, struct rqb_dmabuf *dmab) |
| 670 | { |
Romain Perier | 771db5c | 2017-07-06 10:13:05 +0200 | [diff] [blame] | 671 | dma_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys); |
| 672 | dma_pool_free(phba->lpfc_nvmet_drb_pool, |
James Smart | 3c603be | 2017-05-15 15:20:44 -0700 | [diff] [blame] | 673 | dmab->dbuf.virt, dmab->dbuf.phys); |
James Smart | f358dd0 | 2017-02-12 13:52:34 -0800 | [diff] [blame] | 674 | kfree(dmab); |
| 675 | } |
| 676 | |
| 677 | /** |
James Smart | 3621a71 | 2009-04-06 18:47:14 -0400 | [diff] [blame] | 678 | * lpfc_in_buf_free - Free a DMA buffer |
James Smart | e59058c | 2008-08-24 21:49:00 -0400 | [diff] [blame] | 679 | * @phba: HBA buffer is associated with |
| 680 | * @mp: Buffer to free |
| 681 | * |
| 682 | * Description: Frees the given DMA buffer in the appropriate way given if the |
| 683 | * HBA is running in SLI3 mode with HBQs enabled. |
| 684 | * |
| 685 | * Notes: Takes phba->hbalock. Can be called with or without other locks held. |
| 686 | * |
| 687 | * Returns: None |
| 688 | **/ |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 689 | void |
| 690 | lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp) |
| 691 | { |
| 692 | struct hbq_dmabuf *hbq_entry; |
James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 693 | unsigned long flags; |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 694 | |
James Smart | 7f5f3d0 | 2008-02-08 18:50:14 -0500 | [diff] [blame] | 695 | if (!mp) |
| 696 | return; |
| 697 | |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 698 | if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) { |
James Smart | 895427b | 2017-02-12 13:52:30 -0800 | [diff] [blame] | 699 | hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf); |
James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 700 | /* Check whether HBQ is still in use */ |
| 701 | spin_lock_irqsave(&phba->hbalock, flags); |
| 702 | if (!phba->hbq_in_use) { |
| 703 | spin_unlock_irqrestore(&phba->hbalock, flags); |
| 704 | return; |
| 705 | } |
James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 706 | list_del(&hbq_entry->dbuf.list); |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 707 | if (hbq_entry->tag == -1) { |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame] | 708 | (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer) |
| 709 | (phba, hbq_entry); |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 710 | } else { |
| 711 | lpfc_sli_free_hbq(phba, hbq_entry); |
| 712 | } |
James Smart | 3163f72 | 2008-02-08 18:50:25 -0500 | [diff] [blame] | 713 | spin_unlock_irqrestore(&phba->hbalock, flags); |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 714 | } else { |
| 715 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 716 | kfree(mp); |
| 717 | } |
| 718 | return; |
| 719 | } |
James Smart | d613b6a | 2017-02-12 13:52:37 -0800 | [diff] [blame] | 720 | |
| 721 | /** |
| 722 | * lpfc_rq_buf_free - Free a RQ DMA buffer |
| 723 | * @phba: HBA buffer is associated with |
| 724 | * @mp: Buffer to free |
| 725 | * |
| 726 | * Description: Frees the given DMA buffer in the appropriate way given by |
| 727 | * reposting it to its associated RQ so it can be reused. |
| 728 | * |
| 729 | * Notes: Takes phba->hbalock. Can be called with or without other locks held. |
| 730 | * |
| 731 | * Returns: None |
| 732 | **/ |
| 733 | void |
| 734 | lpfc_rq_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp) |
| 735 | { |
| 736 | struct lpfc_rqb *rqbp; |
| 737 | struct lpfc_rqe hrqe; |
| 738 | struct lpfc_rqe drqe; |
| 739 | struct rqb_dmabuf *rqb_entry; |
| 740 | unsigned long flags; |
| 741 | int rc; |
| 742 | |
| 743 | if (!mp) |
| 744 | return; |
| 745 | |
| 746 | rqb_entry = container_of(mp, struct rqb_dmabuf, hbuf); |
| 747 | rqbp = rqb_entry->hrq->rqbp; |
| 748 | |
| 749 | spin_lock_irqsave(&phba->hbalock, flags); |
| 750 | list_del(&rqb_entry->hbuf.list); |
| 751 | hrqe.address_lo = putPaddrLow(rqb_entry->hbuf.phys); |
| 752 | hrqe.address_hi = putPaddrHigh(rqb_entry->hbuf.phys); |
| 753 | drqe.address_lo = putPaddrLow(rqb_entry->dbuf.phys); |
| 754 | drqe.address_hi = putPaddrHigh(rqb_entry->dbuf.phys); |
| 755 | rc = lpfc_sli4_rq_put(rqb_entry->hrq, rqb_entry->drq, &hrqe, &drqe); |
| 756 | if (rc < 0) { |
James Smart | 411de51 | 2018-01-30 15:58:52 -0800 | [diff] [blame] | 757 | (rqbp->rqb_free_buffer)(phba, rqb_entry); |
James Smart | 6c621a2 | 2017-05-15 15:20:45 -0700 | [diff] [blame] | 758 | lpfc_printf_log(phba, KERN_ERR, LOG_INIT, |
James Smart | 411de51 | 2018-01-30 15:58:52 -0800 | [diff] [blame] | 759 | "6409 Cannot post to HRQ %d: %x %x %x " |
| 760 | "DRQ %x %x\n", |
James Smart | 6c621a2 | 2017-05-15 15:20:45 -0700 | [diff] [blame] | 761 | rqb_entry->hrq->queue_id, |
| 762 | rqb_entry->hrq->host_index, |
James Smart | 411de51 | 2018-01-30 15:58:52 -0800 | [diff] [blame] | 763 | rqb_entry->hrq->hba_index, |
| 764 | rqb_entry->hrq->entry_count, |
| 765 | rqb_entry->drq->host_index, |
| 766 | rqb_entry->drq->hba_index); |
James Smart | d613b6a | 2017-02-12 13:52:37 -0800 | [diff] [blame] | 767 | } else { |
| 768 | list_add_tail(&rqb_entry->hbuf.list, &rqbp->rqb_buffer_list); |
| 769 | rqbp->buffer_count++; |
| 770 | } |
| 771 | |
| 772 | spin_unlock_irqrestore(&phba->hbalock, flags); |
| 773 | } |