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