1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2009 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
;
68 if (phba
->sli_rev
== LPFC_SLI_REV4
)
69 phba
->lpfc_scsi_dma_buf_pool
=
70 pci_pool_create("lpfc_scsi_dma_buf_pool",
72 phba
->cfg_sg_dma_buf_size
,
73 phba
->cfg_sg_dma_buf_size
,
76 phba
->lpfc_scsi_dma_buf_pool
=
77 pci_pool_create("lpfc_scsi_dma_buf_pool",
78 phba
->pcidev
, phba
->cfg_sg_dma_buf_size
,
80 if (!phba
->lpfc_scsi_dma_buf_pool
)
83 phba
->lpfc_mbuf_pool
= pci_pool_create("lpfc_mbuf_pool", phba
->pcidev
,
86 if (!phba
->lpfc_mbuf_pool
)
87 goto fail_free_dma_buf_pool
;
89 pool
->elements
= kmalloc(sizeof(struct lpfc_dmabuf
) *
90 LPFC_MBUF_POOL_SIZE
, GFP_KERNEL
);
92 goto fail_free_lpfc_mbuf_pool
;
95 pool
->current_count
= 0;
96 for ( i
= 0; i
< LPFC_MBUF_POOL_SIZE
; i
++) {
97 pool
->elements
[i
].virt
= pci_pool_alloc(phba
->lpfc_mbuf_pool
,
98 GFP_KERNEL
, &pool
->elements
[i
].phys
);
99 if (!pool
->elements
[i
].virt
)
100 goto fail_free_mbuf_pool
;
102 pool
->current_count
++;
105 phba
->mbox_mem_pool
= mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE
,
106 sizeof(LPFC_MBOXQ_t
));
107 if (!phba
->mbox_mem_pool
)
108 goto fail_free_mbuf_pool
;
110 phba
->nlp_mem_pool
= mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE
,
111 sizeof(struct lpfc_nodelist
));
112 if (!phba
->nlp_mem_pool
)
113 goto fail_free_mbox_pool
;
115 if (phba
->sli_rev
== LPFC_SLI_REV4
) {
117 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE
,
118 sizeof(struct lpfc_node_rrq
));
120 goto fail_free_nlp_mem_pool
;
121 phba
->lpfc_hrb_pool
= pci_pool_create("lpfc_hrb_pool",
123 LPFC_HDR_BUF_SIZE
, align
, 0);
124 if (!phba
->lpfc_hrb_pool
)
125 goto fail_free_rrq_mem_pool
;
127 phba
->lpfc_drb_pool
= pci_pool_create("lpfc_drb_pool",
129 LPFC_DATA_BUF_SIZE
, align
, 0);
130 if (!phba
->lpfc_drb_pool
)
131 goto fail_free_hrb_pool
;
132 phba
->lpfc_hbq_pool
= NULL
;
134 phba
->lpfc_hbq_pool
= pci_pool_create("lpfc_hbq_pool",
135 phba
->pcidev
, LPFC_BPL_SIZE
, align
, 0);
136 if (!phba
->lpfc_hbq_pool
)
137 goto fail_free_nlp_mem_pool
;
138 phba
->lpfc_hrb_pool
= NULL
;
139 phba
->lpfc_drb_pool
= NULL
;
141 /* vpi zero is reserved for the physical port so add 1 to max */
142 longs
= ((phba
->max_vpi
+ 1) + BITS_PER_LONG
- 1) / BITS_PER_LONG
;
143 phba
->vpi_bmask
= kzalloc(longs
* sizeof(unsigned long), GFP_KERNEL
);
144 if (!phba
->vpi_bmask
)
145 goto fail_free_dbq_pool
;
150 pci_pool_destroy(phba
->lpfc_drb_pool
);
151 phba
->lpfc_drb_pool
= NULL
;
153 pci_pool_destroy(phba
->lpfc_hrb_pool
);
154 phba
->lpfc_hrb_pool
= NULL
;
155 fail_free_rrq_mem_pool
:
156 mempool_destroy(phba
->rrq_pool
);
157 phba
->rrq_pool
= NULL
;
158 fail_free_nlp_mem_pool
:
159 mempool_destroy(phba
->nlp_mem_pool
);
160 phba
->nlp_mem_pool
= NULL
;
162 mempool_destroy(phba
->mbox_mem_pool
);
163 phba
->mbox_mem_pool
= NULL
;
166 pci_pool_free(phba
->lpfc_mbuf_pool
, pool
->elements
[i
].virt
,
167 pool
->elements
[i
].phys
);
168 kfree(pool
->elements
);
169 fail_free_lpfc_mbuf_pool
:
170 pci_pool_destroy(phba
->lpfc_mbuf_pool
);
171 phba
->lpfc_mbuf_pool
= NULL
;
172 fail_free_dma_buf_pool
:
173 pci_pool_destroy(phba
->lpfc_scsi_dma_buf_pool
);
174 phba
->lpfc_scsi_dma_buf_pool
= NULL
;
180 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
181 * @phba: HBA to free memory for
183 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
184 * routine is a the counterpart of lpfc_mem_alloc.
189 lpfc_mem_free(struct lpfc_hba
*phba
)
192 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
194 /* Free VPI bitmask memory */
195 kfree(phba
->vpi_bmask
);
198 lpfc_sli_hbqbuf_free_all(phba
);
199 if (phba
->lpfc_drb_pool
)
200 pci_pool_destroy(phba
->lpfc_drb_pool
);
201 phba
->lpfc_drb_pool
= NULL
;
202 if (phba
->lpfc_hrb_pool
)
203 pci_pool_destroy(phba
->lpfc_hrb_pool
);
204 phba
->lpfc_hrb_pool
= NULL
;
206 if (phba
->lpfc_hbq_pool
)
207 pci_pool_destroy(phba
->lpfc_hbq_pool
);
208 phba
->lpfc_hbq_pool
= NULL
;
210 /* Free NLP memory pool */
211 mempool_destroy(phba
->nlp_mem_pool
);
212 phba
->nlp_mem_pool
= NULL
;
214 /* Free mbox memory pool */
215 mempool_destroy(phba
->mbox_mem_pool
);
216 phba
->mbox_mem_pool
= NULL
;
218 /* Free MBUF memory pool */
219 for (i
= 0; i
< pool
->current_count
; i
++)
220 pci_pool_free(phba
->lpfc_mbuf_pool
, pool
->elements
[i
].virt
,
221 pool
->elements
[i
].phys
);
222 kfree(pool
->elements
);
224 pci_pool_destroy(phba
->lpfc_mbuf_pool
);
225 phba
->lpfc_mbuf_pool
= NULL
;
227 /* Free DMA buffer memory pool */
228 pci_pool_destroy(phba
->lpfc_scsi_dma_buf_pool
);
229 phba
->lpfc_scsi_dma_buf_pool
= NULL
;
235 * lpfc_mem_free_all - Frees all PCI and driver memory
236 * @phba: HBA to free memory for
238 * Description: Free memory from PCI and driver memory pools and also those
239 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
240 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
246 lpfc_mem_free_all(struct lpfc_hba
*phba
)
248 struct lpfc_sli
*psli
= &phba
->sli
;
249 LPFC_MBOXQ_t
*mbox
, *next_mbox
;
250 struct lpfc_dmabuf
*mp
;
252 /* Free memory used in mailbox queue back to mailbox memory pool */
253 list_for_each_entry_safe(mbox
, next_mbox
, &psli
->mboxq
, list
) {
254 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
256 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
259 list_del(&mbox
->list
);
260 mempool_free(mbox
, phba
->mbox_mem_pool
);
262 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
263 list_for_each_entry_safe(mbox
, next_mbox
, &psli
->mboxq_cmpl
, list
) {
264 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
266 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
269 list_del(&mbox
->list
);
270 mempool_free(mbox
, phba
->mbox_mem_pool
);
272 /* Free the active mailbox command back to the mailbox memory pool */
273 spin_lock_irq(&phba
->hbalock
);
274 psli
->sli_flag
&= ~LPFC_SLI_MBOX_ACTIVE
;
275 spin_unlock_irq(&phba
->hbalock
);
276 if (psli
->mbox_active
) {
277 mbox
= psli
->mbox_active
;
278 mp
= (struct lpfc_dmabuf
*) (mbox
->context1
);
280 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
283 mempool_free(mbox
, phba
->mbox_mem_pool
);
284 psli
->mbox_active
= NULL
;
287 /* Free and destroy all the allocated memory pools */
290 /* Free the iocb lookup array */
291 kfree(psli
->iocbq_lookup
);
292 psli
->iocbq_lookup
= NULL
;
298 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
299 * @phba: HBA which owns the pool to allocate from
300 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
301 * @handle: used to return the DMA-mapped address of the mbuf
303 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
304 * Allocates from generic pci_pool_alloc function first and if that fails and
305 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
308 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
312 * pointer to the allocated mbuf on success
316 lpfc_mbuf_alloc(struct lpfc_hba
*phba
, int mem_flags
, dma_addr_t
*handle
)
318 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
319 unsigned long iflags
;
322 ret
= pci_pool_alloc(phba
->lpfc_mbuf_pool
, GFP_KERNEL
, handle
);
324 spin_lock_irqsave(&phba
->hbalock
, iflags
);
325 if (!ret
&& (mem_flags
& MEM_PRI
) && pool
->current_count
) {
326 pool
->current_count
--;
327 ret
= pool
->elements
[pool
->current_count
].virt
;
328 *handle
= pool
->elements
[pool
->current_count
].phys
;
330 spin_unlock_irqrestore(&phba
->hbalock
, iflags
);
335 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
336 * @phba: HBA which owns the pool to return to
337 * @virt: mbuf to free
338 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
340 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
341 * it is below its max_count, frees the mbuf otherwise.
343 * Notes: Must be called with phba->hbalock held to synchronize access to
344 * lpfc_mbuf_safety_pool.
349 __lpfc_mbuf_free(struct lpfc_hba
* phba
, void *virt
, dma_addr_t dma
)
351 struct lpfc_dma_pool
*pool
= &phba
->lpfc_mbuf_safety_pool
;
353 if (pool
->current_count
< pool
->max_count
) {
354 pool
->elements
[pool
->current_count
].virt
= virt
;
355 pool
->elements
[pool
->current_count
].phys
= dma
;
356 pool
->current_count
++;
358 pci_pool_free(phba
->lpfc_mbuf_pool
, virt
, dma
);
364 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
365 * @phba: HBA which owns the pool to return to
366 * @virt: mbuf to free
367 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
369 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
370 * it is below its max_count, frees the mbuf otherwise.
372 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
377 lpfc_mbuf_free(struct lpfc_hba
* phba
, void *virt
, dma_addr_t dma
)
379 unsigned long iflags
;
381 spin_lock_irqsave(&phba
->hbalock
, iflags
);
382 __lpfc_mbuf_free(phba
, virt
, dma
);
383 spin_unlock_irqrestore(&phba
->hbalock
, iflags
);
388 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
389 * @phba: HBA to allocate HBQ buffer for
391 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
392 * pool along a non-DMA-mapped container for it.
394 * Notes: Not interrupt-safe. Must be called with no locks held.
397 * pointer to HBQ on success
401 lpfc_els_hbq_alloc(struct lpfc_hba
*phba
)
403 struct hbq_dmabuf
*hbqbp
;
405 hbqbp
= kmalloc(sizeof(struct hbq_dmabuf
), GFP_KERNEL
);
409 hbqbp
->dbuf
.virt
= pci_pool_alloc(phba
->lpfc_hbq_pool
, GFP_KERNEL
,
411 if (!hbqbp
->dbuf
.virt
) {
415 hbqbp
->size
= LPFC_BPL_SIZE
;
420 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
421 * @phba: HBA buffer was allocated for
422 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
424 * Description: Frees both the container and the DMA-mapped buffer returned by
425 * lpfc_els_hbq_alloc.
427 * Notes: Can be called with or without locks held.
432 lpfc_els_hbq_free(struct lpfc_hba
*phba
, struct hbq_dmabuf
*hbqbp
)
434 pci_pool_free(phba
->lpfc_hbq_pool
, hbqbp
->dbuf
.virt
, hbqbp
->dbuf
.phys
);
440 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
441 * @phba: HBA to allocate a receive buffer for
443 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
444 * pool along a non-DMA-mapped container for it.
446 * Notes: Not interrupt-safe. Must be called with no locks held.
449 * pointer to HBQ on success
453 lpfc_sli4_rb_alloc(struct lpfc_hba
*phba
)
455 struct hbq_dmabuf
*dma_buf
;
457 dma_buf
= kmalloc(sizeof(struct hbq_dmabuf
), GFP_KERNEL
);
461 dma_buf
->hbuf
.virt
= pci_pool_alloc(phba
->lpfc_hrb_pool
, GFP_KERNEL
,
462 &dma_buf
->hbuf
.phys
);
463 if (!dma_buf
->hbuf
.virt
) {
467 dma_buf
->dbuf
.virt
= pci_pool_alloc(phba
->lpfc_drb_pool
, GFP_KERNEL
,
468 &dma_buf
->dbuf
.phys
);
469 if (!dma_buf
->dbuf
.virt
) {
470 pci_pool_free(phba
->lpfc_hrb_pool
, dma_buf
->hbuf
.virt
,
475 dma_buf
->size
= LPFC_BPL_SIZE
;
480 * lpfc_sli4_rb_free - Frees a receive buffer
481 * @phba: HBA buffer was allocated for
482 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
484 * Description: Frees both the container and the DMA-mapped buffers returned by
485 * lpfc_sli4_rb_alloc.
487 * Notes: Can be called with or without locks held.
492 lpfc_sli4_rb_free(struct lpfc_hba
*phba
, struct hbq_dmabuf
*dmab
)
494 pci_pool_free(phba
->lpfc_hrb_pool
, dmab
->hbuf
.virt
, dmab
->hbuf
.phys
);
495 pci_pool_free(phba
->lpfc_drb_pool
, dmab
->dbuf
.virt
, dmab
->dbuf
.phys
);
501 * lpfc_in_buf_free - Free a DMA buffer
502 * @phba: HBA buffer is associated with
503 * @mp: Buffer to free
505 * Description: Frees the given DMA buffer in the appropriate way given if the
506 * HBA is running in SLI3 mode with HBQs enabled.
508 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
513 lpfc_in_buf_free(struct lpfc_hba
*phba
, struct lpfc_dmabuf
*mp
)
515 struct hbq_dmabuf
*hbq_entry
;
521 if (phba
->sli3_options
& LPFC_SLI3_HBQ_ENABLED
) {
522 /* Check whether HBQ is still in use */
523 spin_lock_irqsave(&phba
->hbalock
, flags
);
524 if (!phba
->hbq_in_use
) {
525 spin_unlock_irqrestore(&phba
->hbalock
, flags
);
528 hbq_entry
= container_of(mp
, struct hbq_dmabuf
, dbuf
);
529 list_del(&hbq_entry
->dbuf
.list
);
530 if (hbq_entry
->tag
== -1) {
531 (phba
->hbqs
[LPFC_ELS_HBQ
].hbq_free_buffer
)
534 lpfc_sli_free_hbq(phba
, hbq_entry
);
536 spin_unlock_irqrestore(&phba
->hbalock
, flags
);
538 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);