1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
9 * This program is free software; you can redistribute it and/or *
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. *
20 *******************************************************************/
22 #include <linux/mempool.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <linux/interrupt.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_transport_fc.h>
30 #include <scsi/scsi.h>
35 #include "lpfc_sli4.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
40 #include "lpfc_crtn.h"
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 */
47 * lpfc_mem_alloc - create and allocate all PCI and memory pools
48 * @phba: HBA to allocate pools for
50 * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
51 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
52 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
54 * Notes: Not interrupt-safe. Must be called with no locks held. If any
55 * allocation fails, frees all successfully allocated memory before returning.
59 * -ENOMEM on failure (if any memory allocations fail)
62 lpfc_mem_alloc(struct lpfc_hba *phba, int align)
64 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
67 if (phba->sli_rev == LPFC_SLI_REV4) {
68 /* Calculate alignment */
69 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
70 i = phba->cfg_sg_dma_buf_size;
74 phba->lpfc_scsi_dma_buf_pool =
75 pci_pool_create("lpfc_scsi_dma_buf_pool",
77 phba->cfg_sg_dma_buf_size,
81 phba->lpfc_scsi_dma_buf_pool =
82 pci_pool_create("lpfc_scsi_dma_buf_pool",
83 phba->pcidev, phba->cfg_sg_dma_buf_size,
87 if (!phba->lpfc_scsi_dma_buf_pool)
90 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
93 if (!phba->lpfc_mbuf_pool)
94 goto fail_free_dma_buf_pool;
96 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
97 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
99 goto fail_free_lpfc_mbuf_pool;
102 pool->current_count = 0;
103 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
104 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
105 GFP_KERNEL, &pool->elements[i].phys);
106 if (!pool->elements[i].virt)
107 goto fail_free_mbuf_pool;
109 pool->current_count++;
112 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
113 sizeof(LPFC_MBOXQ_t));
114 if (!phba->mbox_mem_pool)
115 goto fail_free_mbuf_pool;
117 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
118 sizeof(struct lpfc_nodelist));
119 if (!phba->nlp_mem_pool)
120 goto fail_free_mbox_pool;
122 if (phba->sli_rev == LPFC_SLI_REV4) {
124 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
125 sizeof(struct lpfc_node_rrq));
127 goto fail_free_nlp_mem_pool;
128 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
130 LPFC_HDR_BUF_SIZE, align, 0);
131 if (!phba->lpfc_hrb_pool)
132 goto fail_free_rrq_mem_pool;
134 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
136 LPFC_DATA_BUF_SIZE, align, 0);
137 if (!phba->lpfc_drb_pool)
138 goto fail_free_hrb_pool;
139 phba->lpfc_hbq_pool = NULL;
141 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
142 phba->pcidev, LPFC_BPL_SIZE, align, 0);
143 if (!phba->lpfc_hbq_pool)
144 goto fail_free_nlp_mem_pool;
145 phba->lpfc_hrb_pool = NULL;
146 phba->lpfc_drb_pool = NULL;
151 pci_pool_destroy(phba->lpfc_hrb_pool);
152 phba->lpfc_hrb_pool = NULL;
153 fail_free_rrq_mem_pool:
154 mempool_destroy(phba->rrq_pool);
155 phba->rrq_pool = NULL;
156 fail_free_nlp_mem_pool:
157 mempool_destroy(phba->nlp_mem_pool);
158 phba->nlp_mem_pool = NULL;
160 mempool_destroy(phba->mbox_mem_pool);
161 phba->mbox_mem_pool = NULL;
164 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
165 pool->elements[i].phys);
166 kfree(pool->elements);
167 fail_free_lpfc_mbuf_pool:
168 pci_pool_destroy(phba->lpfc_mbuf_pool);
169 phba->lpfc_mbuf_pool = NULL;
170 fail_free_dma_buf_pool:
171 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
172 phba->lpfc_scsi_dma_buf_pool = NULL;
178 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
179 * @phba: HBA to free memory for
181 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
182 * routine is a the counterpart of lpfc_mem_alloc.
187 lpfc_mem_free(struct lpfc_hba *phba)
190 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
193 lpfc_sli_hbqbuf_free_all(phba);
194 if (phba->lpfc_drb_pool)
195 pci_pool_destroy(phba->lpfc_drb_pool);
196 phba->lpfc_drb_pool = NULL;
197 if (phba->lpfc_hrb_pool)
198 pci_pool_destroy(phba->lpfc_hrb_pool);
199 phba->lpfc_hrb_pool = NULL;
201 if (phba->lpfc_hbq_pool)
202 pci_pool_destroy(phba->lpfc_hbq_pool);
203 phba->lpfc_hbq_pool = NULL;
206 mempool_destroy(phba->rrq_pool);
207 phba->rrq_pool = NULL;
209 /* Free NLP memory pool */
210 mempool_destroy(phba->nlp_mem_pool);
211 phba->nlp_mem_pool = NULL;
213 /* Free mbox memory pool */
214 mempool_destroy(phba->mbox_mem_pool);
215 phba->mbox_mem_pool = NULL;
217 /* Free MBUF memory pool */
218 for (i = 0; i < pool->current_count; i++)
219 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
220 pool->elements[i].phys);
221 kfree(pool->elements);
223 pci_pool_destroy(phba->lpfc_mbuf_pool);
224 phba->lpfc_mbuf_pool = NULL;
226 /* Free DMA buffer memory pool */
227 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
228 phba->lpfc_scsi_dma_buf_pool = NULL;
234 * lpfc_mem_free_all - Frees all PCI and driver memory
235 * @phba: HBA to free memory for
237 * Description: Free memory from PCI and driver memory pools and also those
238 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
239 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
245 lpfc_mem_free_all(struct lpfc_hba *phba)
247 struct lpfc_sli *psli = &phba->sli;
248 LPFC_MBOXQ_t *mbox, *next_mbox;
249 struct lpfc_dmabuf *mp;
251 /* Free memory used in mailbox queue back to mailbox memory pool */
252 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
253 mp = (struct lpfc_dmabuf *) (mbox->context1);
255 lpfc_mbuf_free(phba, mp->virt, mp->phys);
258 list_del(&mbox->list);
259 mempool_free(mbox, phba->mbox_mem_pool);
261 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
262 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
263 mp = (struct lpfc_dmabuf *) (mbox->context1);
265 lpfc_mbuf_free(phba, mp->virt, mp->phys);
268 list_del(&mbox->list);
269 mempool_free(mbox, phba->mbox_mem_pool);
271 /* Free the active mailbox command back to the mailbox memory pool */
272 spin_lock_irq(&phba->hbalock);
273 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
274 spin_unlock_irq(&phba->hbalock);
275 if (psli->mbox_active) {
276 mbox = psli->mbox_active;
277 mp = (struct lpfc_dmabuf *) (mbox->context1);
279 lpfc_mbuf_free(phba, mp->virt, mp->phys);
282 mempool_free(mbox, phba->mbox_mem_pool);
283 psli->mbox_active = NULL;
286 /* Free and destroy all the allocated memory pools */
289 /* Free the iocb lookup array */
290 kfree(psli->iocbq_lookup);
291 psli->iocbq_lookup = NULL;
297 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
298 * @phba: HBA which owns the pool to allocate from
299 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
300 * @handle: used to return the DMA-mapped address of the mbuf
302 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
303 * Allocates from generic pci_pool_alloc function first and if that fails and
304 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
307 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
311 * pointer to the allocated mbuf on success
315 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
317 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
318 unsigned long iflags;
321 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
323 spin_lock_irqsave(&phba->hbalock, iflags);
324 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
325 pool->current_count--;
326 ret = pool->elements[pool->current_count].virt;
327 *handle = pool->elements[pool->current_count].phys;
329 spin_unlock_irqrestore(&phba->hbalock, iflags);
334 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
335 * @phba: HBA which owns the pool to return to
336 * @virt: mbuf to free
337 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
339 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
340 * it is below its max_count, frees the mbuf otherwise.
342 * Notes: Must be called with phba->hbalock held to synchronize access to
343 * lpfc_mbuf_safety_pool.
348 __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
350 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
352 if (pool->current_count < pool->max_count) {
353 pool->elements[pool->current_count].virt = virt;
354 pool->elements[pool->current_count].phys = dma;
355 pool->current_count++;
357 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
363 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
364 * @phba: HBA which owns the pool to return to
365 * @virt: mbuf to free
366 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
368 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
369 * it is below its max_count, frees the mbuf otherwise.
371 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
376 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
378 unsigned long iflags;
380 spin_lock_irqsave(&phba->hbalock, iflags);
381 __lpfc_mbuf_free(phba, virt, dma);
382 spin_unlock_irqrestore(&phba->hbalock, iflags);
387 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
388 * @phba: HBA to allocate HBQ buffer for
390 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
391 * pool along a non-DMA-mapped container for it.
393 * Notes: Not interrupt-safe. Must be called with no locks held.
396 * pointer to HBQ on success
400 lpfc_els_hbq_alloc(struct lpfc_hba *phba)
402 struct hbq_dmabuf *hbqbp;
404 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
408 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
410 if (!hbqbp->dbuf.virt) {
414 hbqbp->size = LPFC_BPL_SIZE;
419 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
420 * @phba: HBA buffer was allocated for
421 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
423 * Description: Frees both the container and the DMA-mapped buffer returned by
424 * lpfc_els_hbq_alloc.
426 * Notes: Can be called with or without locks held.
431 lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
433 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
439 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
440 * @phba: HBA to allocate a receive buffer for
442 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
443 * pool along a non-DMA-mapped container for it.
445 * Notes: Not interrupt-safe. Must be called with no locks held.
448 * pointer to HBQ on success
452 lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
454 struct hbq_dmabuf *dma_buf;
456 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
460 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
461 &dma_buf->hbuf.phys);
462 if (!dma_buf->hbuf.virt) {
466 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
467 &dma_buf->dbuf.phys);
468 if (!dma_buf->dbuf.virt) {
469 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
474 dma_buf->size = LPFC_BPL_SIZE;
479 * lpfc_sli4_rb_free - Frees a receive buffer
480 * @phba: HBA buffer was allocated for
481 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
483 * Description: Frees both the container and the DMA-mapped buffers returned by
484 * lpfc_sli4_rb_alloc.
486 * Notes: Can be called with or without locks held.
491 lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
493 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
494 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
500 * lpfc_in_buf_free - Free a DMA buffer
501 * @phba: HBA buffer is associated with
502 * @mp: Buffer to free
504 * Description: Frees the given DMA buffer in the appropriate way given if the
505 * HBA is running in SLI3 mode with HBQs enabled.
507 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
512 lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
514 struct hbq_dmabuf *hbq_entry;
520 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
521 /* Check whether HBQ is still in use */
522 spin_lock_irqsave(&phba->hbalock, flags);
523 if (!phba->hbq_in_use) {
524 spin_unlock_irqrestore(&phba->hbalock, flags);
527 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
528 list_del(&hbq_entry->dbuf.list);
529 if (hbq_entry->tag == -1) {
530 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
533 lpfc_sli_free_hbq(phba, hbq_entry);
535 spin_unlock_irqrestore(&phba->hbalock, flags);
537 lpfc_mbuf_free(phba, mp->virt, mp->phys);