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