| 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. * |
| 4 | * Copyright (C) 2004-2005 Emulex. All rights reserved. * |
| 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 | |
| 26 | #include "lpfc_hw.h" |
| 27 | #include "lpfc_sli.h" |
| 28 | #include "lpfc_disc.h" |
| 29 | #include "lpfc_scsi.h" |
| 30 | #include "lpfc.h" |
| 31 | #include "lpfc_crtn.h" |
| 32 | |
| 33 | #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */ |
| 34 | #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */ |
| 35 | |
| 36 | static void * |
| 37 | lpfc_pool_kmalloc(unsigned int gfp_flags, void *data) |
| 38 | { |
| 39 | return kmalloc((unsigned long)data, gfp_flags); |
| 40 | } |
| 41 | |
| 42 | static void |
| 43 | lpfc_pool_kfree(void *obj, void *data) |
| 44 | { |
| 45 | kfree(obj); |
| 46 | } |
| 47 | |
| 48 | int |
| 49 | lpfc_mem_alloc(struct lpfc_hba * phba) |
| 50 | { |
| 51 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 52 | int i; |
| 53 | |
| 54 | phba->lpfc_scsi_dma_buf_pool = pci_pool_create("lpfc_scsi_dma_buf_pool", |
| 55 | phba->pcidev, phba->cfg_sg_dma_buf_size, 8, 0); |
| 56 | if (!phba->lpfc_scsi_dma_buf_pool) |
| 57 | goto fail; |
| 58 | |
| 59 | phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev, |
| 60 | LPFC_BPL_SIZE, 8,0); |
| 61 | if (!phba->lpfc_mbuf_pool) |
| 62 | goto fail_free_dma_buf_pool; |
| 63 | |
| 64 | pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) * |
| 65 | LPFC_MBUF_POOL_SIZE, GFP_KERNEL); |
| 66 | pool->max_count = 0; |
| 67 | pool->current_count = 0; |
| 68 | for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) { |
| 69 | pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool, |
| 70 | GFP_KERNEL, &pool->elements[i].phys); |
| 71 | if (!pool->elements[i].virt) |
| 72 | goto fail_free_mbuf_pool; |
| 73 | pool->max_count++; |
| 74 | pool->current_count++; |
| 75 | } |
| 76 | |
| 77 | phba->mbox_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE, |
| 78 | lpfc_pool_kmalloc, lpfc_pool_kfree, |
| 79 | (void *)(unsigned long)sizeof(LPFC_MBOXQ_t)); |
| 80 | if (!phba->mbox_mem_pool) |
| 81 | goto fail_free_mbuf_pool; |
| 82 | |
| 83 | phba->nlp_mem_pool = mempool_create(LPFC_MEM_POOL_SIZE, |
| 84 | lpfc_pool_kmalloc, lpfc_pool_kfree, |
| 85 | (void *)(unsigned long)sizeof(struct lpfc_nodelist)); |
| 86 | if (!phba->nlp_mem_pool) |
| 87 | goto fail_free_mbox_pool; |
| 88 | |
| 89 | return 0; |
| 90 | |
| 91 | fail_free_mbox_pool: |
| 92 | mempool_destroy(phba->mbox_mem_pool); |
| 93 | fail_free_mbuf_pool: |
| 94 | while (--i) |
| 95 | pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, |
| 96 | pool->elements[i].phys); |
| 97 | kfree(pool->elements); |
| 98 | pci_pool_destroy(phba->lpfc_mbuf_pool); |
| 99 | fail_free_dma_buf_pool: |
| 100 | pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); |
| 101 | fail: |
| 102 | return -ENOMEM; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | lpfc_mem_free(struct lpfc_hba * phba) |
| 107 | { |
| 108 | struct lpfc_sli *psli = &phba->sli; |
| 109 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 110 | LPFC_MBOXQ_t *mbox, *next_mbox; |
| 111 | struct lpfc_dmabuf *mp; |
| 112 | int i; |
| 113 | |
| 114 | list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) { |
| 115 | mp = (struct lpfc_dmabuf *) (mbox->context1); |
| 116 | if (mp) { |
| 117 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 118 | kfree(mp); |
| 119 | } |
| 120 | list_del(&mbox->list); |
| 121 | mempool_free(mbox, phba->mbox_mem_pool); |
| 122 | } |
| 123 | |
| 124 | psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE; |
| 125 | if (psli->mbox_active) { |
| 126 | mbox = psli->mbox_active; |
| 127 | mp = (struct lpfc_dmabuf *) (mbox->context1); |
| 128 | if (mp) { |
| 129 | lpfc_mbuf_free(phba, mp->virt, mp->phys); |
| 130 | kfree(mp); |
| 131 | } |
| 132 | mempool_free(mbox, phba->mbox_mem_pool); |
| 133 | psli->mbox_active = NULL; |
| 134 | } |
| 135 | |
| 136 | for (i = 0; i < pool->current_count; i++) |
| 137 | pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt, |
| 138 | pool->elements[i].phys); |
| 139 | kfree(pool->elements); |
| 140 | mempool_destroy(phba->nlp_mem_pool); |
| 141 | mempool_destroy(phba->mbox_mem_pool); |
| 142 | |
| 143 | pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool); |
| 144 | pci_pool_destroy(phba->lpfc_mbuf_pool); |
| 145 | } |
| 146 | |
| 147 | void * |
| 148 | lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle) |
| 149 | { |
| 150 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 151 | void *ret; |
| 152 | |
| 153 | ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle); |
| 154 | |
| 155 | if (!ret && ( mem_flags & MEM_PRI) && pool->current_count) { |
| 156 | pool->current_count--; |
| 157 | ret = pool->elements[pool->current_count].virt; |
| 158 | *handle = pool->elements[pool->current_count].phys; |
| 159 | } |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma) |
| 165 | { |
| 166 | struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool; |
| 167 | |
| 168 | if (pool->current_count < pool->max_count) { |
| 169 | pool->elements[pool->current_count].virt = virt; |
| 170 | pool->elements[pool->current_count].phys = dma; |
| 171 | pool->current_count++; |
| 172 | } else { |
| 173 | pci_pool_free(phba->lpfc_mbuf_pool, virt, dma); |
| 174 | } |
| 175 | return; |
| 176 | } |