| 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 | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 4 | * Copyright (C) 2004-2006 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> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | |
James.Smart@Emulex.Com | f888ba3 | 2005-08-10 15:03:01 -0400 | [diff] [blame] | 26 | #include <scsi/scsi_device.h> |
| 27 | #include <scsi/scsi_transport_fc.h> |
| 28 | |
James.Smart@Emulex.Com | 9188652 | 2005-08-10 15:03:09 -0400 | [diff] [blame] | 29 | #include <scsi/scsi.h> |
| 30 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 31 | #include "lpfc_hw.h" |
| 32 | #include "lpfc_sli.h" |
| 33 | #include "lpfc_disc.h" |
| 34 | #include "lpfc_scsi.h" |
| 35 | #include "lpfc.h" |
| 36 | #include "lpfc_crtn.h" |
| 37 | |
| 38 | #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */ |
| 39 | #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */ |
| 40 | |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 41 | |
| 42 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 43 | int |
| 44 | lpfc_mem_alloc(struct lpfc_hba * phba) |
| 45 | { |
| 46 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 47 | int longs; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 48 | int i; |
| 49 | |
| 50 | phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool", |
| 51 | phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0); |
| 52 | if (!phba->lpfc_scsi_dma_buf_pool) |
| 53 | goto fail; |
| 54 | |
| 55 | phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev, |
| 56 | LPFC_BPL_SIZE, 8,0); |
| 57 | if (!phba->lpfc_mbuf_pool) |
| 58 | goto fail_free_dma_buf_pool; |
| 59 | |
| 60 | pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) * |
| 61 | LPFC_MBUF_POOL_SIZE, GFP_KERNEL); |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 62 | if (!pool->elements) |
| 63 | goto fail_free_lpfc_mbuf_pool; |
| 64 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 65 | pool->max_count = 0; |
| 66 | pool->current_count = 0; |
| 67 | for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) { |
| 68 | pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool, |
| 69 | GFP_KERNEL, &pool->elements[i].phys); |
| 70 | if (!pool->elements[i].virt) |
| 71 | goto fail_free_mbuf_pool; |
| 72 | pool->max_count++; |
| 73 | pool->current_count++; |
| 74 | } |
| 75 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 76 | phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, |
| 77 | sizeof(LPFC_MBOXQ_t)); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 78 | if (!phba->mbox_mem_pool) |
| 79 | goto fail_free_mbuf_pool; |
| 80 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 81 | phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, |
| 82 | sizeof(struct lpfc_nodelist)); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 83 | if (!phba->nlp_mem_pool) |
| 84 | goto fail_free_mbox_pool; |
| 85 | |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 86 | phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev, |
| 87 | LPFC_BPL_SIZE, 8, 0); |
| 88 | if (!phba->lpfc_hbq_pool) |
| 89 | goto fail_free_nlp_mem_pool; |
| 90 | |
James Smart | 858c9f6 | 2007-06-17 19:56:39 -0500 | [diff] [blame] | 91 | /* vpi zero is reserved for the physical port so add 1 to max */ |
| 92 | longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG; |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 93 | phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL); |
| 94 | if (!phba->vpi_bmask) |
| 95 | goto fail_free_hbq_pool; |
| 96 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 97 | return 0; |
| 98 | |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 99 | fail_free_hbq_pool: |
| 100 | lpfc_sli_hbqbuf_free_all(phba); |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 101 | fail_free_nlp_mem_pool: |
| 102 | mempool_destroy(phba->nlp_mem_pool); |
| 103 | phba->nlp_mem_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 104 | fail_free_mbox_pool: |
| 105 | mempool_destroy(phba->mbox_mem_pool); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 106 | phba->mbox_mem_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 107 | fail_free_mbuf_pool: |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 108 | while (i--) |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 109 | pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, |
| 110 | pool->elements[i].phys); |
| 111 | kfree(pool->elements); |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 112 | fail_free_lpfc_mbuf_pool: |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 113 | pci_pool_destroy(phba->lpfc_mbuf_pool); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 114 | phba->lpfc_mbuf_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 115 | fail_free_dma_buf_pool: |
| 116 | pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 117 | phba->lpfc_scsi_dma_buf_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 118 | fail: |
| 119 | return -ENOMEM; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | lpfc_mem_free(struct lpfc_hba * phba) |
| 124 | { |
| 125 | struct lpfc_sli *psli = &phba->sli; |
| 126 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 127 | LPFC_MBOXQ_t *mbox, *next_mbox; |
| 128 | struct lpfc_dmabuf *mp; |
| 129 | int i; |
| 130 | |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 131 | kfree(phba->vpi_bmask); |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 132 | lpfc_sli_hbqbuf_free_all(phba); |
| 133 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 134 | list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) { |
| 135 | mp = (struct lpfc_dmabuf *) (mbox->context1); |
| 136 | if (mp) { |
| 137 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 138 | kfree(mp); |
| 139 | } |
| 140 | list_del(&mbox->list); |
| 141 | mempool_free(mbox, phba->mbox_mem_pool); |
| 142 | } |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 143 | list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) { |
| 144 | mp = (struct lpfc_dmabuf *) (mbox->context1); |
| 145 | if (mp) { |
| 146 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 147 | kfree(mp); |
| 148 | } |
| 149 | list_del(&mbox->list); |
| 150 | mempool_free(mbox, phba->mbox_mem_pool); |
| 151 | } |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 152 | |
| 153 | psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE; |
| 154 | if (psli->mbox_active) { |
| 155 | mbox = psli->mbox_active; |
| 156 | mp = (struct lpfc_dmabuf *) (mbox->context1); |
| 157 | if (mp) { |
| 158 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 159 | kfree(mp); |
| 160 | } |
| 161 | mempool_free(mbox, phba->mbox_mem_pool); |
| 162 | psli->mbox_active = NULL; |
| 163 | } |
| 164 | |
| 165 | for (i = 0; i < pool->current_count; i++) |
| 166 | pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, |
| 167 | pool->elements[i].phys); |
| 168 | kfree(pool->elements); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 169 | |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 170 | pci_pool_destroy(phba->lpfc_hbq_pool); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 171 | mempool_destroy(phba->nlp_mem_pool); |
| 172 | mempool_destroy(phba->mbox_mem_pool); |
| 173 | |
| 174 | pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); |
| 175 | pci_pool_destroy(phba->lpfc_mbuf_pool); |
James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 176 | |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 177 | phba->lpfc_hbq_pool = NULL; |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 178 | phba->nlp_mem_pool = NULL; |
| 179 | phba->mbox_mem_pool = NULL; |
| 180 | phba->lpfc_scsi_dma_buf_pool = NULL; |
| 181 | phba->lpfc_mbuf_pool = NULL; |
| 182 | |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 183 | /* Free the iocb lookup array */ |
James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 184 | kfree(psli->iocbq_lookup); |
| 185 | psli->iocbq_lookup = NULL; |
| 186 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void * |
| 190 | lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle) |
| 191 | { |
| 192 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 193 | unsigned long iflags; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 194 | void *ret; |
| 195 | |
| 196 | ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle); |
| 197 | |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 198 | spin_lock_irqsave(&phba->hbalock, iflags); |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 199 | if (!ret && (mem_flags & MEM_PRI) && pool->current_count) { |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 200 | pool->current_count--; |
| 201 | ret = pool->elements[pool->current_count].virt; |
| 202 | *handle = pool->elements[pool->current_count].phys; |
| 203 | } |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 204 | spin_unlock_irqrestore(&phba->hbalock, iflags); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | void |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 209 | __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 210 | { |
| 211 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 212 | |
| 213 | if (pool->current_count < pool->max_count) { |
| 214 | pool->elements[pool->current_count].virt = virt; |
| 215 | pool->elements[pool->current_count].phys = dma; |
| 216 | pool->current_count++; |
| 217 | } else { |
| 218 | pci_pool_free(phba->lpfc_mbuf_pool, virt, dma); |
| 219 | } |
| 220 | return; |
| 221 | } |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 222 | |
| 223 | void |
| 224 | lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) |
| 225 | { |
| 226 | unsigned long iflags; |
| 227 | |
| 228 | spin_lock_irqsave(&phba->hbalock, iflags); |
| 229 | __lpfc_mbuf_free(phba, virt, dma); |
| 230 | spin_unlock_irqrestore(&phba->hbalock, iflags); |
| 231 | return; |
| 232 | } |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 233 | |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame^] | 234 | struct hbq_dmabuf * |
| 235 | lpfc_els_hbq_alloc(struct lpfc_hba *phba) |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 236 | { |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame^] | 237 | struct hbq_dmabuf *hbqbp; |
| 238 | |
| 239 | hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL); |
| 240 | if (!hbqbp) |
| 241 | return NULL; |
| 242 | |
| 243 | hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL, |
| 244 | &hbqbp->dbuf.phys); |
| 245 | if (!hbqbp->dbuf.virt) { |
| 246 | kfree(hbqbp); |
| 247 | return NULL; |
| 248 | } |
| 249 | hbqbp->size = LPFC_BPL_SIZE; |
| 250 | return hbqbp; |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame^] | 254 | lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp) |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 255 | { |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame^] | 256 | pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys); |
| 257 | kfree(hbqbp); |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame^] | 261 | /* This is ONLY called for the LPFC_ELS_HBQ */ |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 262 | void |
| 263 | lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp) |
| 264 | { |
| 265 | struct hbq_dmabuf *hbq_entry; |
| 266 | |
| 267 | if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) { |
| 268 | hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf); |
| 269 | if (hbq_entry->tag == -1) { |
James Smart | 51ef4c2 | 2007-08-02 11:10:31 -0400 | [diff] [blame^] | 270 | (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer) |
| 271 | (phba, hbq_entry); |
James Smart | 92d7f7b | 2007-06-17 19:56:38 -0500 | [diff] [blame] | 272 | } else { |
| 273 | lpfc_sli_free_hbq(phba, hbq_entry); |
| 274 | } |
| 275 | } else { |
| 276 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 277 | kfree(mp); |
| 278 | } |
| 279 | return; |
| 280 | } |
| 281 | |