1dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>/*******************************************************************
2dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> * This file is part of the Emulex Linux Device Driver for         *
3c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * Fibre Channel Host Bus Adapters.                                *
4b857ff33d0a8be47220ebd0e816962c6037216bbJames Smart * Copyright (C) 2004-2014 Emulex.  All rights reserved.           *
5c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * EMULEX and SLI are trademarks of Emulex.                        *
6dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> * www.emulex.com                                                  *
7c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> *                                                                 *
9dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> * This program is free software; you can redistribute it and/or   *
10c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * modify it under the terms of version 2 of the GNU General       *
11c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * Public License as published by the Free Software Foundation.    *
12c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * This program is distributed in the hope that it will be useful. *
13c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * more details, a copy of which can be found in the file COPYING  *
19c44ce1737438d20ac58e808897e3f8eb015c66d3James.Smart@Emulex.Com * included with this package.                                     *
20dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> *******************************************************************/
21dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
22dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include <linux/mempool.h>
235a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
24dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include <linux/pci.h>
25dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include <linux/interrupt.h>
26dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
27f888ba3ce77c66bece3d804caf7d559838209a4aJames.Smart@Emulex.Com#include <scsi/scsi_device.h>
28f888ba3ce77c66bece3d804caf7d559838209a4aJames.Smart@Emulex.Com#include <scsi/scsi_transport_fc.h>
29f888ba3ce77c66bece3d804caf7d559838209a4aJames.Smart@Emulex.Com
30918865230e55b1fece2d8edec39d46c00626590bJames.Smart@Emulex.Com#include <scsi/scsi.h>
31918865230e55b1fece2d8edec39d46c00626590bJames.Smart@Emulex.Com
32da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart#include "lpfc_hw4.h"
33dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include "lpfc_hw.h"
34dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include "lpfc_sli.h"
35da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart#include "lpfc_sli4.h"
36ea2151b4e142fa2de0319d9dd80413a997bf435aJames Smart#include "lpfc_nl.h"
37dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include "lpfc_disc.h"
38dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include "lpfc_scsi.h"
39dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include "lpfc.h"
40dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#include "lpfc_crtn.h"
41cff261f6bd03612e792e4c8872c6ad049f743863James Smart#include "lpfc_logmsg.h"
42dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
43dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#define LPFC_MBUF_POOL_SIZE     64      /* max elements in MBUF safety pool */
44dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>#define LPFC_MEM_POOL_SIZE      64      /* max elem in non-DMA safety pool */
451ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart#define LPFC_DEVICE_DATA_POOL_SIZE 64   /* max elements in device data pool */
46dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
47cff261f6bd03612e792e4c8872c6ad049f743863James Smartint
48cff261f6bd03612e792e4c8872c6ad049f743863James Smartlpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
49cff261f6bd03612e792e4c8872c6ad049f743863James Smart	size_t bytes;
50cff261f6bd03612e792e4c8872c6ad049f743863James Smart	int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
51cff261f6bd03612e792e4c8872c6ad049f743863James Smart
52cff261f6bd03612e792e4c8872c6ad049f743863James Smart	if (max_xri <= 0)
53cff261f6bd03612e792e4c8872c6ad049f743863James Smart		return -ENOMEM;
54cff261f6bd03612e792e4c8872c6ad049f743863James Smart	bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) *
55cff261f6bd03612e792e4c8872c6ad049f743863James Smart		  sizeof(unsigned long);
56cff261f6bd03612e792e4c8872c6ad049f743863James Smart	phba->cfg_rrq_xri_bitmap_sz = bytes;
57cff261f6bd03612e792e4c8872c6ad049f743863James Smart	phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
58cff261f6bd03612e792e4c8872c6ad049f743863James Smart							    bytes);
59cff261f6bd03612e792e4c8872c6ad049f743863James Smart	if (!phba->active_rrq_pool)
60cff261f6bd03612e792e4c8872c6ad049f743863James Smart		return -ENOMEM;
61cff261f6bd03612e792e4c8872c6ad049f743863James Smart	else
62cff261f6bd03612e792e4c8872c6ad049f743863James Smart		return 0;
63cff261f6bd03612e792e4c8872c6ad049f743863James Smart}
642e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart
65e59058c44025d71c9b7f260076a932935d3bba95James Smart/**
663621a710a7dbb2d22a8e95d94bcf0c2d13ef57fcJames Smart * lpfc_mem_alloc - create and allocate all PCI and memory pools
67e59058c44025d71c9b7f260076a932935d3bba95James Smart * @phba: HBA to allocate pools for
68e59058c44025d71c9b7f260076a932935d3bba95James Smart *
69e59058c44025d71c9b7f260076a932935d3bba95James Smart * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
70da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * lpfc_mbuf_pool, lpfc_hrb_pool.  Creates and allocates kmalloc-backed mempools
71e59058c44025d71c9b7f260076a932935d3bba95James Smart * for LPFC_MBOXQ_t and lpfc_nodelist.  Also allocates the VPI bitmask.
72e59058c44025d71c9b7f260076a932935d3bba95James Smart *
73e59058c44025d71c9b7f260076a932935d3bba95James Smart * Notes: Not interrupt-safe.  Must be called with no locks held.  If any
74e59058c44025d71c9b7f260076a932935d3bba95James Smart * allocation fails, frees all successfully allocated memory before returning.
75e59058c44025d71c9b7f260076a932935d3bba95James Smart *
76e59058c44025d71c9b7f260076a932935d3bba95James Smart * Returns:
77e59058c44025d71c9b7f260076a932935d3bba95James Smart *   0 on success
78e59058c44025d71c9b7f260076a932935d3bba95James Smart *   -ENOMEM on failure (if any memory allocations fail)
79e59058c44025d71c9b7f260076a932935d3bba95James Smart **/
80dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>int
81da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smartlpfc_mem_alloc(struct lpfc_hba *phba, int align)
82dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>{
83dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
84dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	int i;
85dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
8696f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart	if (phba->sli_rev == LPFC_SLI_REV4) {
8796f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart		/* Calculate alignment */
8896f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart		if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
8996f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart			i = phba->cfg_sg_dma_buf_size;
9096f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart		else
9196f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart			i = SLI4_PAGE_SIZE;
9296f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart
93da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		phba->lpfc_scsi_dma_buf_pool =
94da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart			pci_pool_create("lpfc_scsi_dma_buf_pool",
95da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart				phba->pcidev,
96da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart				phba->cfg_sg_dma_buf_size,
9796f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart				i,
98da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart				0);
9996f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart	} else {
100da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		phba->lpfc_scsi_dma_buf_pool =
101da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart			pci_pool_create("lpfc_scsi_dma_buf_pool",
102da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart				phba->pcidev, phba->cfg_sg_dma_buf_size,
103da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart				align, 0);
10496f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart	}
10596f7077f671254e957a2815e54bb20e8d50f0bbcJames Smart
106dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	if (!phba->lpfc_scsi_dma_buf_pool)
107dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		goto fail;
108dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
109dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
110da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart							LPFC_BPL_SIZE,
111da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart							align, 0);
112dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	if (!phba->lpfc_mbuf_pool)
113dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		goto fail_free_dma_buf_pool;
114dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
115dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
116dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>					 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
117a96e0c7798057dd8055d0263c076fed975c10237Mariusz Kozlowski	if (!pool->elements)
118a96e0c7798057dd8055d0263c076fed975c10237Mariusz Kozlowski		goto fail_free_lpfc_mbuf_pool;
119a96e0c7798057dd8055d0263c076fed975c10237Mariusz Kozlowski
120dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	pool->max_count = 0;
121dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	pool->current_count = 0;
122dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
123dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
124dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>				       GFP_KERNEL, &pool->elements[i].phys);
125dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		if (!pool->elements[i].virt)
126dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>			goto fail_free_mbuf_pool;
127dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pool->max_count++;
128dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pool->current_count++;
129dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	}
130dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
1310eaae62abaa1ad1f231932b6cdd9fb1b91df6651Matthew Dobson	phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
1320eaae62abaa1ad1f231932b6cdd9fb1b91df6651Matthew Dobson							 sizeof(LPFC_MBOXQ_t));
133dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	if (!phba->mbox_mem_pool)
134dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		goto fail_free_mbuf_pool;
135dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
1360eaae62abaa1ad1f231932b6cdd9fb1b91df6651Matthew Dobson	phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
1370eaae62abaa1ad1f231932b6cdd9fb1b91df6651Matthew Dobson						sizeof(struct lpfc_nodelist));
138dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	if (!phba->nlp_mem_pool)
139dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		goto fail_free_mbox_pool;
1408568a4d2495ebcf5da38a2141c7633399143b1a5James Smart
1418568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	if (phba->sli_rev == LPFC_SLI_REV4) {
14219ca760979e4be41a3eb215fb8d0e96637161947James Smart		phba->rrq_pool =
14319ca760979e4be41a3eb215fb8d0e96637161947James Smart			mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
14419ca760979e4be41a3eb215fb8d0e96637161947James Smart						sizeof(struct lpfc_node_rrq));
14519ca760979e4be41a3eb215fb8d0e96637161947James Smart		if (!phba->rrq_pool)
14619ca760979e4be41a3eb215fb8d0e96637161947James Smart			goto fail_free_nlp_mem_pool;
1478568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
148da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart					      phba->pcidev,
149da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart					      LPFC_HDR_BUF_SIZE, align, 0);
1508568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		if (!phba->lpfc_hrb_pool)
15119ca760979e4be41a3eb215fb8d0e96637161947James Smart			goto fail_free_rrq_mem_pool;
1528568a4d2495ebcf5da38a2141c7633399143b1a5James Smart
1538568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
154da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart					      phba->pcidev,
155da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart					      LPFC_DATA_BUF_SIZE, align, 0);
1568568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		if (!phba->lpfc_drb_pool)
1578568a4d2495ebcf5da38a2141c7633399143b1a5James Smart			goto fail_free_hrb_pool;
1588568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		phba->lpfc_hbq_pool = NULL;
1598568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	} else {
1608568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
1618568a4d2495ebcf5da38a2141c7633399143b1a5James Smart			phba->pcidev, LPFC_BPL_SIZE, align, 0);
1628568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		if (!phba->lpfc_hbq_pool)
1638568a4d2495ebcf5da38a2141c7633399143b1a5James Smart			goto fail_free_nlp_mem_pool;
1648568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		phba->lpfc_hrb_pool = NULL;
1658568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		phba->lpfc_drb_pool = NULL;
1668568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	}
16792d7f7b0cde3ad2260e7462b40867b57efd49851James Smart
1681ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart	if (phba->cfg_EnableXLane) {
1691ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart		phba->device_data_mem_pool = mempool_create_kmalloc_pool(
1701ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart					LPFC_DEVICE_DATA_POOL_SIZE,
1711ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart					sizeof(struct lpfc_device_data));
1721ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart		if (!phba->device_data_mem_pool)
1731ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart			goto fail_free_hrb_pool;
1741ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart	} else {
1751ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart		phba->device_data_mem_pool = NULL;
1761ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart	}
1771ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart
178dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	return 0;
1798568a4d2495ebcf5da38a2141c7633399143b1a5James Smart fail_free_hrb_pool:
180da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	pci_pool_destroy(phba->lpfc_hrb_pool);
181da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	phba->lpfc_hrb_pool = NULL;
18219ca760979e4be41a3eb215fb8d0e96637161947James Smart fail_free_rrq_mem_pool:
18319ca760979e4be41a3eb215fb8d0e96637161947James Smart	mempool_destroy(phba->rrq_pool);
18419ca760979e4be41a3eb215fb8d0e96637161947James Smart	phba->rrq_pool = NULL;
185ed957684294618602b48f1950b0c9bbcb036583fJames Smart fail_free_nlp_mem_pool:
186ed957684294618602b48f1950b0c9bbcb036583fJames Smart	mempool_destroy(phba->nlp_mem_pool);
187ed957684294618602b48f1950b0c9bbcb036583fJames Smart	phba->nlp_mem_pool = NULL;
188dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> fail_free_mbox_pool:
189dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	mempool_destroy(phba->mbox_mem_pool);
1902e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	phba->mbox_mem_pool = NULL;
191dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> fail_free_mbuf_pool:
192a96e0c7798057dd8055d0263c076fed975c10237Mariusz Kozlowski	while (i--)
193dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
194dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>						 pool->elements[i].phys);
195dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	kfree(pool->elements);
196a96e0c7798057dd8055d0263c076fed975c10237Mariusz Kozlowski fail_free_lpfc_mbuf_pool:
197dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	pci_pool_destroy(phba->lpfc_mbuf_pool);
1982e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	phba->lpfc_mbuf_pool = NULL;
199dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> fail_free_dma_buf_pool:
200dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
2012e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	phba->lpfc_scsi_dma_buf_pool = NULL;
202dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com> fail:
203dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	return -ENOMEM;
204dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>}
205dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
206e59058c44025d71c9b7f260076a932935d3bba95James Smart/**
207da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
208e59058c44025d71c9b7f260076a932935d3bba95James Smart * @phba: HBA to free memory for
209e59058c44025d71c9b7f260076a932935d3bba95James Smart *
210da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Description: Free the memory allocated by lpfc_mem_alloc routine. This
211da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * routine is a the counterpart of lpfc_mem_alloc.
212e59058c44025d71c9b7f260076a932935d3bba95James Smart *
213e59058c44025d71c9b7f260076a932935d3bba95James Smart * Returns: None
214e59058c44025d71c9b7f260076a932935d3bba95James Smart **/
215dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>void
216da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smartlpfc_mem_free(struct lpfc_hba *phba)
217dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>{
218dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	int i;
219da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
2201ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart	struct lpfc_device_data *device_data;
221dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
222da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free HBQ pools */
223ed957684294618602b48f1950b0c9bbcb036583fJames Smart	lpfc_sli_hbqbuf_free_all(phba);
2248568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	if (phba->lpfc_drb_pool)
2258568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		pci_pool_destroy(phba->lpfc_drb_pool);
226da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	phba->lpfc_drb_pool = NULL;
2278568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	if (phba->lpfc_hrb_pool)
2288568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		pci_pool_destroy(phba->lpfc_hrb_pool);
229da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	phba->lpfc_hrb_pool = NULL;
230da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
2318568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	if (phba->lpfc_hbq_pool)
2328568a4d2495ebcf5da38a2141c7633399143b1a5James Smart		pci_pool_destroy(phba->lpfc_hbq_pool);
2338568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	phba->lpfc_hbq_pool = NULL;
2348568a4d2495ebcf5da38a2141c7633399143b1a5James Smart
23521a688cdfa2f48ca9132cf152b0cb7274cb37b98James Smart	if (phba->rrq_pool)
23621a688cdfa2f48ca9132cf152b0cb7274cb37b98James Smart		mempool_destroy(phba->rrq_pool);
23721a688cdfa2f48ca9132cf152b0cb7274cb37b98James Smart	phba->rrq_pool = NULL;
23821a688cdfa2f48ca9132cf152b0cb7274cb37b98James Smart
239da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free NLP memory pool */
240da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	mempool_destroy(phba->nlp_mem_pool);
241da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	phba->nlp_mem_pool = NULL;
242cff261f6bd03612e792e4c8872c6ad049f743863James Smart	if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
243cff261f6bd03612e792e4c8872c6ad049f743863James Smart		mempool_destroy(phba->active_rrq_pool);
244cff261f6bd03612e792e4c8872c6ad049f743863James Smart		phba->active_rrq_pool = NULL;
245cff261f6bd03612e792e4c8872c6ad049f743863James Smart	}
246da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
247da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free mbox memory pool */
248da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	mempool_destroy(phba->mbox_mem_pool);
249da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	phba->mbox_mem_pool = NULL;
250da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
251da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free MBUF memory pool */
252da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	for (i = 0; i < pool->current_count; i++)
253da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
254da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart			      pool->elements[i].phys);
255da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	kfree(pool->elements);
256da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
257da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	pci_pool_destroy(phba->lpfc_mbuf_pool);
258da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	phba->lpfc_mbuf_pool = NULL;
259ed957684294618602b48f1950b0c9bbcb036583fJames Smart
260da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free DMA buffer memory pool */
261da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
262da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	phba->lpfc_scsi_dma_buf_pool = NULL;
263da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
2641ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart	/* Free Device Data memory pool */
2651ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart	if (phba->device_data_mem_pool) {
2661ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart		/* Ensure all objects have been returned to the pool */
2671ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart		while (!list_empty(&phba->luns)) {
2681ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart			device_data = list_first_entry(&phba->luns,
2691ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart						       struct lpfc_device_data,
2701ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart						       listentry);
2711ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart			list_del(&device_data->listentry);
2721ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart			mempool_free(device_data, phba->device_data_mem_pool);
2731ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart		}
2741ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart		mempool_destroy(phba->device_data_mem_pool);
2751ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart	}
2761ba981fd3ad1f91b8bb205ce6aac6aad45f2fa7aJames Smart	phba->device_data_mem_pool = NULL;
277da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	return;
278da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart}
279da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
280da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart/**
281da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * lpfc_mem_free_all - Frees all PCI and driver memory
282da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * @phba: HBA to free memory for
283da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *
284da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Description: Free memory from PCI and driver memory pools and also those
285da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
286da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
287da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * the VPI bitmask.
288da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *
289da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Returns: None
290da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart **/
291da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smartvoid
292da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smartlpfc_mem_free_all(struct lpfc_hba *phba)
293da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart{
294da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	struct lpfc_sli *psli = &phba->sli;
295da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	LPFC_MBOXQ_t *mbox, *next_mbox;
296da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	struct lpfc_dmabuf   *mp;
297da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
298da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free memory used in mailbox queue back to mailbox memory pool */
299dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
300dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		mp = (struct lpfc_dmabuf *) (mbox->context1);
301dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		if (mp) {
302dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>			lpfc_mbuf_free(phba, mp->virt, mp->phys);
303dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>			kfree(mp);
304dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		}
305dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		list_del(&mbox->list);
306dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		mempool_free(mbox, phba->mbox_mem_pool);
307dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	}
308da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free memory used in mailbox cmpl list back to mailbox memory pool */
30992d7f7b0cde3ad2260e7462b40867b57efd49851James Smart	list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
31092d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		mp = (struct lpfc_dmabuf *) (mbox->context1);
31192d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		if (mp) {
31292d7f7b0cde3ad2260e7462b40867b57efd49851James Smart			lpfc_mbuf_free(phba, mp->virt, mp->phys);
31392d7f7b0cde3ad2260e7462b40867b57efd49851James Smart			kfree(mp);
31492d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		}
31592d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		list_del(&mbox->list);
31692d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		mempool_free(mbox, phba->mbox_mem_pool);
31792d7f7b0cde3ad2260e7462b40867b57efd49851James Smart	}
318da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free the active mailbox command back to the mailbox memory pool */
319da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	spin_lock_irq(&phba->hbalock);
320dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
321da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	spin_unlock_irq(&phba->hbalock);
322dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	if (psli->mbox_active) {
323dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		mbox = psli->mbox_active;
324dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		mp = (struct lpfc_dmabuf *) (mbox->context1);
325dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		if (mp) {
326dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>			lpfc_mbuf_free(phba, mp->virt, mp->phys);
327dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>			kfree(mp);
328dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		}
329dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		mempool_free(mbox, phba->mbox_mem_pool);
330dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		psli->mbox_active = NULL;
331dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	}
332dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
333da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	/* Free and destroy all the allocated memory pools */
334da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	lpfc_mem_free(phba);
3352e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart
336e59058c44025d71c9b7f260076a932935d3bba95James Smart	/* Free the iocb lookup array */
3379f49d3b05f092b89e774be20c654ceb0c64a7d19James Smart	kfree(psli->iocbq_lookup);
3389f49d3b05f092b89e774be20c654ceb0c64a7d19James Smart	psli->iocbq_lookup = NULL;
339da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
340da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	return;
341dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>}
342dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
343e59058c44025d71c9b7f260076a932935d3bba95James Smart/**
3443621a710a7dbb2d22a8e95d94bcf0c2d13ef57fcJames Smart * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
345e59058c44025d71c9b7f260076a932935d3bba95James Smart * @phba: HBA which owns the pool to allocate from
346e59058c44025d71c9b7f260076a932935d3bba95James Smart * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
347e59058c44025d71c9b7f260076a932935d3bba95James Smart * @handle: used to return the DMA-mapped address of the mbuf
348e59058c44025d71c9b7f260076a932935d3bba95James Smart *
349e59058c44025d71c9b7f260076a932935d3bba95James Smart * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
350e59058c44025d71c9b7f260076a932935d3bba95James Smart * Allocates from generic pci_pool_alloc function first and if that fails and
351e59058c44025d71c9b7f260076a932935d3bba95James Smart * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
352e59058c44025d71c9b7f260076a932935d3bba95James Smart * HBA's pool.
353e59058c44025d71c9b7f260076a932935d3bba95James Smart *
354e59058c44025d71c9b7f260076a932935d3bba95James Smart * Notes: Not interrupt-safe.  Must be called with no locks held.  Takes
355e59058c44025d71c9b7f260076a932935d3bba95James Smart * phba->hbalock.
356e59058c44025d71c9b7f260076a932935d3bba95James Smart *
357e59058c44025d71c9b7f260076a932935d3bba95James Smart * Returns:
358e59058c44025d71c9b7f260076a932935d3bba95James Smart *   pointer to the allocated mbuf on success
359e59058c44025d71c9b7f260076a932935d3bba95James Smart *   NULL on failure
360e59058c44025d71c9b7f260076a932935d3bba95James Smart **/
361dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>void *
362dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
363dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>{
364dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
3652e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	unsigned long iflags;
366dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	void *ret;
367dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
368dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
369dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
3702e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	spin_lock_irqsave(&phba->hbalock, iflags);
37192d7f7b0cde3ad2260e7462b40867b57efd49851James Smart	if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
372dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pool->current_count--;
373dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		ret = pool->elements[pool->current_count].virt;
374dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		*handle = pool->elements[pool->current_count].phys;
375dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	}
3762e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	spin_unlock_irqrestore(&phba->hbalock, iflags);
377dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	return ret;
378dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>}
379dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
380e59058c44025d71c9b7f260076a932935d3bba95James Smart/**
3813621a710a7dbb2d22a8e95d94bcf0c2d13ef57fcJames Smart * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
382e59058c44025d71c9b7f260076a932935d3bba95James Smart * @phba: HBA which owns the pool to return to
383e59058c44025d71c9b7f260076a932935d3bba95James Smart * @virt: mbuf to free
384e59058c44025d71c9b7f260076a932935d3bba95James Smart * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
385e59058c44025d71c9b7f260076a932935d3bba95James Smart *
386e59058c44025d71c9b7f260076a932935d3bba95James Smart * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
387e59058c44025d71c9b7f260076a932935d3bba95James Smart * it is below its max_count, frees the mbuf otherwise.
388e59058c44025d71c9b7f260076a932935d3bba95James Smart *
389e59058c44025d71c9b7f260076a932935d3bba95James Smart * Notes: Must be called with phba->hbalock held to synchronize access to
390e59058c44025d71c9b7f260076a932935d3bba95James Smart * lpfc_mbuf_safety_pool.
391e59058c44025d71c9b7f260076a932935d3bba95James Smart *
392e59058c44025d71c9b7f260076a932935d3bba95James Smart * Returns: None
393e59058c44025d71c9b7f260076a932935d3bba95James Smart **/
394dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>void
3952e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
396dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>{
397dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
398dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>
399dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	if (pool->current_count < pool->max_count) {
400dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pool->elements[pool->current_count].virt = virt;
401dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pool->elements[pool->current_count].phys = dma;
402dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pool->current_count++;
403dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	} else {
404dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>		pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
405dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	}
406dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>	return;
407dea3101e0a5c897d2c9351a7444e139db9f40247<jejb@titanic.il.steeleye.com>}
4082e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart
409e59058c44025d71c9b7f260076a932935d3bba95James Smart/**
4103621a710a7dbb2d22a8e95d94bcf0c2d13ef57fcJames Smart * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
411e59058c44025d71c9b7f260076a932935d3bba95James Smart * @phba: HBA which owns the pool to return to
412e59058c44025d71c9b7f260076a932935d3bba95James Smart * @virt: mbuf to free
413e59058c44025d71c9b7f260076a932935d3bba95James Smart * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
414e59058c44025d71c9b7f260076a932935d3bba95James Smart *
415e59058c44025d71c9b7f260076a932935d3bba95James Smart * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
416e59058c44025d71c9b7f260076a932935d3bba95James Smart * it is below its max_count, frees the mbuf otherwise.
417e59058c44025d71c9b7f260076a932935d3bba95James Smart *
418e59058c44025d71c9b7f260076a932935d3bba95James Smart * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
419e59058c44025d71c9b7f260076a932935d3bba95James Smart *
420e59058c44025d71c9b7f260076a932935d3bba95James Smart * Returns: None
421e59058c44025d71c9b7f260076a932935d3bba95James Smart **/
4222e0fef85e098f6794956b8b80b111179fbb4cbb7James Smartvoid
4232e0fef85e098f6794956b8b80b111179fbb4cbb7James Smartlpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
4242e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart{
4252e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	unsigned long iflags;
4262e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart
4272e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	spin_lock_irqsave(&phba->hbalock, iflags);
4282e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	__lpfc_mbuf_free(phba, virt, dma);
4292e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	spin_unlock_irqrestore(&phba->hbalock, iflags);
4302e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart	return;
4312e0fef85e098f6794956b8b80b111179fbb4cbb7James Smart}
432ed957684294618602b48f1950b0c9bbcb036583fJames Smart
433e59058c44025d71c9b7f260076a932935d3bba95James Smart/**
4343621a710a7dbb2d22a8e95d94bcf0c2d13ef57fcJames Smart * lpfc_els_hbq_alloc - Allocate an HBQ buffer
435e59058c44025d71c9b7f260076a932935d3bba95James Smart * @phba: HBA to allocate HBQ buffer for
436e59058c44025d71c9b7f260076a932935d3bba95James Smart *
437da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
438e59058c44025d71c9b7f260076a932935d3bba95James Smart * pool along a non-DMA-mapped container for it.
439e59058c44025d71c9b7f260076a932935d3bba95James Smart *
440e59058c44025d71c9b7f260076a932935d3bba95James Smart * Notes: Not interrupt-safe.  Must be called with no locks held.
441e59058c44025d71c9b7f260076a932935d3bba95James Smart *
442e59058c44025d71c9b7f260076a932935d3bba95James Smart * Returns:
443e59058c44025d71c9b7f260076a932935d3bba95James Smart *   pointer to HBQ on success
444e59058c44025d71c9b7f260076a932935d3bba95James Smart *   NULL on failure
445e59058c44025d71c9b7f260076a932935d3bba95James Smart **/
44651ef4c26891a734bc8416b639ad460a8162926bcJames Smartstruct hbq_dmabuf *
44751ef4c26891a734bc8416b639ad460a8162926bcJames Smartlpfc_els_hbq_alloc(struct lpfc_hba *phba)
448ed957684294618602b48f1950b0c9bbcb036583fJames Smart{
44951ef4c26891a734bc8416b639ad460a8162926bcJames Smart	struct hbq_dmabuf *hbqbp;
45051ef4c26891a734bc8416b639ad460a8162926bcJames Smart
4512e90f4b5a2a0ce5ab72c0c81c74269bd0a62522bJames Smart	hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
45251ef4c26891a734bc8416b639ad460a8162926bcJames Smart	if (!hbqbp)
45351ef4c26891a734bc8416b639ad460a8162926bcJames Smart		return NULL;
45451ef4c26891a734bc8416b639ad460a8162926bcJames Smart
4558568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
45651ef4c26891a734bc8416b639ad460a8162926bcJames Smart					  &hbqbp->dbuf.phys);
45751ef4c26891a734bc8416b639ad460a8162926bcJames Smart	if (!hbqbp->dbuf.virt) {
45851ef4c26891a734bc8416b639ad460a8162926bcJames Smart		kfree(hbqbp);
45951ef4c26891a734bc8416b639ad460a8162926bcJames Smart		return NULL;
46051ef4c26891a734bc8416b639ad460a8162926bcJames Smart	}
46151ef4c26891a734bc8416b639ad460a8162926bcJames Smart	hbqbp->size = LPFC_BPL_SIZE;
46251ef4c26891a734bc8416b639ad460a8162926bcJames Smart	return hbqbp;
463ed957684294618602b48f1950b0c9bbcb036583fJames Smart}
464ed957684294618602b48f1950b0c9bbcb036583fJames Smart
465e59058c44025d71c9b7f260076a932935d3bba95James Smart/**
466da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
467e59058c44025d71c9b7f260076a932935d3bba95James Smart * @phba: HBA buffer was allocated for
468e59058c44025d71c9b7f260076a932935d3bba95James Smart * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
469e59058c44025d71c9b7f260076a932935d3bba95James Smart *
470e59058c44025d71c9b7f260076a932935d3bba95James Smart * Description: Frees both the container and the DMA-mapped buffer returned by
471e59058c44025d71c9b7f260076a932935d3bba95James Smart * lpfc_els_hbq_alloc.
472e59058c44025d71c9b7f260076a932935d3bba95James Smart *
473e59058c44025d71c9b7f260076a932935d3bba95James Smart * Notes: Can be called with or without locks held.
474e59058c44025d71c9b7f260076a932935d3bba95James Smart *
475e59058c44025d71c9b7f260076a932935d3bba95James Smart * Returns: None
476e59058c44025d71c9b7f260076a932935d3bba95James Smart **/
477ed957684294618602b48f1950b0c9bbcb036583fJames Smartvoid
47851ef4c26891a734bc8416b639ad460a8162926bcJames Smartlpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
479ed957684294618602b48f1950b0c9bbcb036583fJames Smart{
4808568a4d2495ebcf5da38a2141c7633399143b1a5James Smart	pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
48151ef4c26891a734bc8416b639ad460a8162926bcJames Smart	kfree(hbqbp);
482ed957684294618602b48f1950b0c9bbcb036583fJames Smart	return;
483ed957684294618602b48f1950b0c9bbcb036583fJames Smart}
484ed957684294618602b48f1950b0c9bbcb036583fJames Smart
485e59058c44025d71c9b7f260076a932935d3bba95James Smart/**
486da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
487da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * @phba: HBA to allocate a receive buffer for
488da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *
489da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
490da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * pool along a non-DMA-mapped container for it.
491da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *
492da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Notes: Not interrupt-safe.  Must be called with no locks held.
493da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *
494da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Returns:
495da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *   pointer to HBQ on success
496da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *   NULL on failure
497da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart **/
498da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smartstruct hbq_dmabuf *
499da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smartlpfc_sli4_rb_alloc(struct lpfc_hba *phba)
500da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart{
501da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	struct hbq_dmabuf *dma_buf;
502da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
5032e90f4b5a2a0ce5ab72c0c81c74269bd0a62522bJames Smart	dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
504da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	if (!dma_buf)
505da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		return NULL;
506da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
507da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
508da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart					    &dma_buf->hbuf.phys);
509da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	if (!dma_buf->hbuf.virt) {
510da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		kfree(dma_buf);
511da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		return NULL;
512da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	}
513da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
514da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart					    &dma_buf->dbuf.phys);
515da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	if (!dma_buf->dbuf.virt) {
516da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
517da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart			      dma_buf->hbuf.phys);
518da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		kfree(dma_buf);
519da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart		return NULL;
520da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	}
521da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	dma_buf->size = LPFC_BPL_SIZE;
522da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	return dma_buf;
523da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart}
524da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
525da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart/**
526da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * lpfc_sli4_rb_free - Frees a receive buffer
527da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * @phba: HBA buffer was allocated for
528da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
529da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *
530da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Description: Frees both the container and the DMA-mapped buffers returned by
531da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * lpfc_sli4_rb_alloc.
532da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *
533da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Notes: Can be called with or without locks held.
534da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart *
535da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart * Returns: None
536da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart **/
537da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smartvoid
538da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smartlpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
539da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart{
540da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
541da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
542da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	kfree(dmab);
543da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart	return;
544da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart}
545da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart
546da0436e915a5c17ee79e72c1bf978a4ebb1cbf4dJames Smart/**
5473621a710a7dbb2d22a8e95d94bcf0c2d13ef57fcJames Smart * lpfc_in_buf_free - Free a DMA buffer
548e59058c44025d71c9b7f260076a932935d3bba95James Smart * @phba: HBA buffer is associated with
549e59058c44025d71c9b7f260076a932935d3bba95James Smart * @mp: Buffer to free
550e59058c44025d71c9b7f260076a932935d3bba95James Smart *
551e59058c44025d71c9b7f260076a932935d3bba95James Smart * Description: Frees the given DMA buffer in the appropriate way given if the
552e59058c44025d71c9b7f260076a932935d3bba95James Smart * HBA is running in SLI3 mode with HBQs enabled.
553e59058c44025d71c9b7f260076a932935d3bba95James Smart *
554e59058c44025d71c9b7f260076a932935d3bba95James Smart * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
555e59058c44025d71c9b7f260076a932935d3bba95James Smart *
556e59058c44025d71c9b7f260076a932935d3bba95James Smart * Returns: None
557e59058c44025d71c9b7f260076a932935d3bba95James Smart **/
55892d7f7b0cde3ad2260e7462b40867b57efd49851James Smartvoid
55992d7f7b0cde3ad2260e7462b40867b57efd49851James Smartlpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
56092d7f7b0cde3ad2260e7462b40867b57efd49851James Smart{
56192d7f7b0cde3ad2260e7462b40867b57efd49851James Smart	struct hbq_dmabuf *hbq_entry;
5623163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart	unsigned long flags;
56392d7f7b0cde3ad2260e7462b40867b57efd49851James Smart
5647f5f3d0d02aa2f124e764aee5c775589ce72fd42James Smart	if (!mp)
5657f5f3d0d02aa2f124e764aee5c775589ce72fd42James Smart		return;
5667f5f3d0d02aa2f124e764aee5c775589ce72fd42James Smart
56792d7f7b0cde3ad2260e7462b40867b57efd49851James Smart	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5683163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart		/* Check whether HBQ is still in use */
5693163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart		spin_lock_irqsave(&phba->hbalock, flags);
5703163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart		if (!phba->hbq_in_use) {
5713163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart			spin_unlock_irqrestore(&phba->hbalock, flags);
5723163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart			return;
5733163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart		}
57492d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
5753163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart		list_del(&hbq_entry->dbuf.list);
57692d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		if (hbq_entry->tag == -1) {
57751ef4c26891a734bc8416b639ad460a8162926bcJames Smart			(phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
57851ef4c26891a734bc8416b639ad460a8162926bcJames Smart				(phba, hbq_entry);
57992d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		} else {
58092d7f7b0cde3ad2260e7462b40867b57efd49851James Smart			lpfc_sli_free_hbq(phba, hbq_entry);
58192d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		}
5823163f725a5d071eea1830bbbfab78cfe3fc9baafJames Smart		spin_unlock_irqrestore(&phba->hbalock, flags);
58392d7f7b0cde3ad2260e7462b40867b57efd49851James Smart	} else {
58492d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		lpfc_mbuf_free(phba, mp->virt, mp->phys);
58592d7f7b0cde3ad2260e7462b40867b57efd49851James Smart		kfree(mp);
58692d7f7b0cde3ad2260e7462b40867b57efd49851James Smart	}
58792d7f7b0cde3ad2260e7462b40867b57efd49851James Smart	return;
58892d7f7b0cde3ad2260e7462b40867b57efd49851James Smart}
589