| 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; |
| 47 | int i; |
| 48 | |
| 49 | phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool", |
| 50 | phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0); |
| 51 | if (!phba->lpfc_scsi_dma_buf_pool) |
| 52 | goto fail; |
| 53 | |
| 54 | phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev, |
| 55 | LPFC_BPL_SIZE, 8,0); |
| 56 | if (!phba->lpfc_mbuf_pool) |
| 57 | goto fail_free_dma_buf_pool; |
| 58 | |
| 59 | pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) * |
| 60 | LPFC_MBUF_POOL_SIZE, GFP_KERNEL); |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 61 | if (!pool->elements) |
| 62 | goto fail_free_lpfc_mbuf_pool; |
| 63 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 64 | pool->max_count = 0; |
| 65 | pool->current_count = 0; |
| 66 | for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) { |
| 67 | pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool, |
| 68 | GFP_KERNEL, &pool->elements[i].phys); |
| 69 | if (!pool->elements[i].virt) |
| 70 | goto fail_free_mbuf_pool; |
| 71 | pool->max_count++; |
| 72 | pool->current_count++; |
| 73 | } |
| 74 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 75 | phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, |
| 76 | sizeof(LPFC_MBOXQ_t)); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 77 | if (!phba->mbox_mem_pool) |
| 78 | goto fail_free_mbuf_pool; |
| 79 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 80 | phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE, |
| 81 | sizeof(struct lpfc_nodelist)); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 82 | if (!phba->nlp_mem_pool) |
| 83 | goto fail_free_mbox_pool; |
| 84 | |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame^] | 85 | phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",phba->pcidev, |
| 86 | LPFC_BPL_SIZE, 8, 0); |
| 87 | if (!phba->lpfc_hbq_pool) |
| 88 | goto fail_free_nlp_mem_pool; |
| 89 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 90 | return 0; |
| 91 | |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame^] | 92 | fail_free_nlp_mem_pool: |
| 93 | mempool_destroy(phba->nlp_mem_pool); |
| 94 | phba->nlp_mem_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 95 | fail_free_mbox_pool: |
| 96 | mempool_destroy(phba->mbox_mem_pool); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 97 | phba->mbox_mem_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 98 | fail_free_mbuf_pool: |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 99 | while (i--) |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 100 | pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, |
| 101 | pool->elements[i].phys); |
| 102 | kfree(pool->elements); |
Mariusz Kozlowski | a96e0c7 | 2007-01-02 01:07:32 +0100 | [diff] [blame] | 103 | fail_free_lpfc_mbuf_pool: |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 104 | pci_pool_destroy(phba->lpfc_mbuf_pool); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 105 | phba->lpfc_mbuf_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 106 | fail_free_dma_buf_pool: |
| 107 | pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 108 | phba->lpfc_scsi_dma_buf_pool = NULL; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 109 | fail: |
| 110 | return -ENOMEM; |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | lpfc_mem_free(struct lpfc_hba * phba) |
| 115 | { |
| 116 | struct lpfc_sli *psli = &phba->sli; |
| 117 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 118 | LPFC_MBOXQ_t *mbox, *next_mbox; |
| 119 | struct lpfc_dmabuf *mp; |
| 120 | int i; |
| 121 | |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame^] | 122 | lpfc_sli_hbqbuf_free_all(phba); |
| 123 | |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 124 | spin_lock_irq(&phba->hbalock); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 125 | list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) { |
| 126 | mp = (struct lpfc_dmabuf *) (mbox->context1); |
| 127 | if (mp) { |
| 128 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 129 | kfree(mp); |
| 130 | } |
| 131 | list_del(&mbox->list); |
| 132 | mempool_free(mbox, phba->mbox_mem_pool); |
| 133 | } |
| 134 | |
| 135 | psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE; |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 136 | spin_unlock_irq(&phba->hbalock); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 137 | if (psli->mbox_active) { |
| 138 | mbox = psli->mbox_active; |
| 139 | mp = (struct lpfc_dmabuf *) (mbox->context1); |
| 140 | if (mp) { |
| 141 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 142 | kfree(mp); |
| 143 | } |
| 144 | mempool_free(mbox, phba->mbox_mem_pool); |
| 145 | psli->mbox_active = NULL; |
| 146 | } |
| 147 | |
| 148 | for (i = 0; i < pool->current_count; i++) |
| 149 | pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, |
| 150 | pool->elements[i].phys); |
| 151 | kfree(pool->elements); |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 152 | |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame^] | 153 | pci_pool_destroy(phba->lpfc_hbq_pool); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 154 | mempool_destroy(phba->nlp_mem_pool); |
| 155 | mempool_destroy(phba->mbox_mem_pool); |
| 156 | |
| 157 | pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); |
| 158 | pci_pool_destroy(phba->lpfc_mbuf_pool); |
James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 159 | |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame^] | 160 | phba->lpfc_hbq_pool = NULL; |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 161 | phba->nlp_mem_pool = NULL; |
| 162 | phba->mbox_mem_pool = NULL; |
| 163 | phba->lpfc_scsi_dma_buf_pool = NULL; |
| 164 | phba->lpfc_mbuf_pool = NULL; |
| 165 | |
James Smart | 9f49d3b | 2006-07-06 15:49:34 -0400 | [diff] [blame] | 166 | /* Free the iocb lookup array */ |
| 167 | kfree(psli->iocbq_lookup); |
| 168 | psli->iocbq_lookup = NULL; |
| 169 | |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void * |
| 173 | lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle) |
| 174 | { |
| 175 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 176 | unsigned long iflags; |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 177 | void *ret; |
| 178 | |
| 179 | ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle); |
| 180 | |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 181 | spin_lock_irqsave(&phba->hbalock, iflags); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 182 | if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) { |
| 183 | pool->current_count--; |
| 184 | ret = pool->elements[pool->current_count].virt; |
| 185 | *handle = pool->elements[pool->current_count].phys; |
| 186 | } |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 187 | spin_unlock_irqrestore(&phba->hbalock, iflags); |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | void |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 192 | __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) |
| dea3101 | 2005-04-17 16:05:31 -0500 | [diff] [blame] | 193 | { |
| 194 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 195 | |
| 196 | if (pool->current_count < pool->max_count) { |
| 197 | pool->elements[pool->current_count].virt = virt; |
| 198 | pool->elements[pool->current_count].phys = dma; |
| 199 | pool->current_count++; |
| 200 | } else { |
| 201 | pci_pool_free(phba->lpfc_mbuf_pool, virt, dma); |
| 202 | } |
| 203 | return; |
| 204 | } |
James Smart | 2e0fef8 | 2007-06-17 19:56:36 -0500 | [diff] [blame] | 205 | |
| 206 | void |
| 207 | lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) |
| 208 | { |
| 209 | unsigned long iflags; |
| 210 | |
| 211 | spin_lock_irqsave(&phba->hbalock, iflags); |
| 212 | __lpfc_mbuf_free(phba, virt, dma); |
| 213 | spin_unlock_irqrestore(&phba->hbalock, iflags); |
| 214 | return; |
| 215 | } |
James Smart | ed95768 | 2007-06-17 19:56:37 -0500 | [diff] [blame^] | 216 | |
| 217 | |
| 218 | void * |
| 219 | lpfc_hbq_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle) |
| 220 | { |
| 221 | void *ret; |
| 222 | ret = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_ATOMIC, handle); |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | void |
| 227 | lpfc_hbq_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma) |
| 228 | { |
| 229 | pci_pool_free(phba->lpfc_hbq_pool, virt, dma); |
| 230 | return; |
| 231 | } |
| 232 | |