]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/lpfc/lpfc_mem.c
Merge tag 'efi-urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi into...
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / lpfc / lpfc_mem.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
d080abe0
JS
4 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
b857ff33 6 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
c44ce173 7 * EMULEX and SLI are trademarks of Emulex. *
d080abe0 8 * www.broadcom.com *
c44ce173 9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
10 * *
11 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
12 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea3101e
JB
22 *******************************************************************/
23
dea3101e 24#include <linux/mempool.h>
5a0e3ad6 25#include <linux/slab.h>
dea3101e
JB
26#include <linux/pci.h>
27#include <linux/interrupt.h>
28
895427bd 29#include <scsi/scsi.h>
f888ba3c
JSEC
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_transport_fc.h>
895427bd 32#include <scsi/fc/fc_fs.h>
f888ba3c 33
895427bd 34#include <linux/nvme-fc-driver.h>
91886523 35
da0436e9 36#include "lpfc_hw4.h"
dea3101e
JB
37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
da0436e9 39#include "lpfc_sli4.h"
ea2151b4 40#include "lpfc_nl.h"
dea3101e 41#include "lpfc_disc.h"
dea3101e 42#include "lpfc.h"
895427bd
JS
43#include "lpfc_scsi.h"
44#include "lpfc_nvme.h"
f358dd0c 45#include "lpfc_nvmet.h"
dea3101e 46#include "lpfc_crtn.h"
cff261f6 47#include "lpfc_logmsg.h"
dea3101e
JB
48
49#define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
50#define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
1ba981fd 51#define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */
dea3101e 52
cff261f6
JS
53int
54lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
55 size_t bytes;
56 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
57
58 if (max_xri <= 0)
59 return -ENOMEM;
60 bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) *
61 sizeof(unsigned long);
62 phba->cfg_rrq_xri_bitmap_sz = bytes;
63 phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
64 bytes);
65 if (!phba->active_rrq_pool)
66 return -ENOMEM;
67 else
68 return 0;
69}
2e0fef85 70
e59058c4 71/**
3621a710 72 * lpfc_mem_alloc - create and allocate all PCI and memory pools
e59058c4
JS
73 * @phba: HBA to allocate pools for
74 *
895427bd 75 * Description: Creates and allocates PCI pools lpfc_sg_dma_buf_pool,
da0436e9 76 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
e59058c4
JS
77 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
78 *
79 * Notes: Not interrupt-safe. Must be called with no locks held. If any
80 * allocation fails, frees all successfully allocated memory before returning.
81 *
82 * Returns:
83 * 0 on success
84 * -ENOMEM on failure (if any memory allocations fail)
85 **/
dea3101e 86int
da0436e9 87lpfc_mem_alloc(struct lpfc_hba *phba, int align)
dea3101e
JB
88{
89 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
90 int i;
91
96f7077f
JS
92 if (phba->sli_rev == LPFC_SLI_REV4) {
93 /* Calculate alignment */
94 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
95 i = phba->cfg_sg_dma_buf_size;
96 else
97 i = SLI4_PAGE_SIZE;
98
895427bd
JS
99 phba->lpfc_sg_dma_buf_pool =
100 pci_pool_create("lpfc_sg_dma_buf_pool",
101 phba->pcidev,
102 phba->cfg_sg_dma_buf_size,
103 i, 0);
104 if (!phba->lpfc_sg_dma_buf_pool)
105 goto fail;
106
96f7077f 107 } else {
895427bd
JS
108 phba->lpfc_sg_dma_buf_pool =
109 pci_pool_create("lpfc_sg_dma_buf_pool",
110 phba->pcidev, phba->cfg_sg_dma_buf_size,
111 align, 0);
96f7077f 112
895427bd
JS
113 if (!phba->lpfc_sg_dma_buf_pool)
114 goto fail;
115 }
dea3101e
JB
116
117 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
da0436e9
JS
118 LPFC_BPL_SIZE,
119 align, 0);
dea3101e
JB
120 if (!phba->lpfc_mbuf_pool)
121 goto fail_free_dma_buf_pool;
122
123 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
124 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
a96e0c77
MK
125 if (!pool->elements)
126 goto fail_free_lpfc_mbuf_pool;
127
dea3101e
JB
128 pool->max_count = 0;
129 pool->current_count = 0;
130 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
131 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
132 GFP_KERNEL, &pool->elements[i].phys);
133 if (!pool->elements[i].virt)
134 goto fail_free_mbuf_pool;
135 pool->max_count++;
136 pool->current_count++;
137 }
138
0eaae62a
MD
139 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
140 sizeof(LPFC_MBOXQ_t));
dea3101e
JB
141 if (!phba->mbox_mem_pool)
142 goto fail_free_mbuf_pool;
143
0eaae62a
MD
144 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
145 sizeof(struct lpfc_nodelist));
dea3101e
JB
146 if (!phba->nlp_mem_pool)
147 goto fail_free_mbox_pool;
8568a4d2
JS
148
149 if (phba->sli_rev == LPFC_SLI_REV4) {
19ca7609
JS
150 phba->rrq_pool =
151 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
152 sizeof(struct lpfc_node_rrq));
153 if (!phba->rrq_pool)
154 goto fail_free_nlp_mem_pool;
8568a4d2 155 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
da0436e9
JS
156 phba->pcidev,
157 LPFC_HDR_BUF_SIZE, align, 0);
8568a4d2 158 if (!phba->lpfc_hrb_pool)
19ca7609 159 goto fail_free_rrq_mem_pool;
8568a4d2
JS
160
161 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
da0436e9
JS
162 phba->pcidev,
163 LPFC_DATA_BUF_SIZE, align, 0);
8568a4d2
JS
164 if (!phba->lpfc_drb_pool)
165 goto fail_free_hrb_pool;
166 phba->lpfc_hbq_pool = NULL;
167 } else {
168 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
169 phba->pcidev, LPFC_BPL_SIZE, align, 0);
170 if (!phba->lpfc_hbq_pool)
171 goto fail_free_nlp_mem_pool;
172 phba->lpfc_hrb_pool = NULL;
173 phba->lpfc_drb_pool = NULL;
174 }
92d7f7b0 175
1ba981fd
JS
176 if (phba->cfg_EnableXLane) {
177 phba->device_data_mem_pool = mempool_create_kmalloc_pool(
178 LPFC_DEVICE_DATA_POOL_SIZE,
179 sizeof(struct lpfc_device_data));
180 if (!phba->device_data_mem_pool)
895427bd 181 goto fail_free_drb_pool;
1ba981fd
JS
182 } else {
183 phba->device_data_mem_pool = NULL;
184 }
185
dea3101e 186 return 0;
895427bd
JS
187fail_free_drb_pool:
188 pci_pool_destroy(phba->lpfc_drb_pool);
189 phba->lpfc_drb_pool = NULL;
8568a4d2 190 fail_free_hrb_pool:
da0436e9
JS
191 pci_pool_destroy(phba->lpfc_hrb_pool);
192 phba->lpfc_hrb_pool = NULL;
19ca7609
JS
193 fail_free_rrq_mem_pool:
194 mempool_destroy(phba->rrq_pool);
195 phba->rrq_pool = NULL;
ed957684
JS
196 fail_free_nlp_mem_pool:
197 mempool_destroy(phba->nlp_mem_pool);
198 phba->nlp_mem_pool = NULL;
dea3101e
JB
199 fail_free_mbox_pool:
200 mempool_destroy(phba->mbox_mem_pool);
2e0fef85 201 phba->mbox_mem_pool = NULL;
dea3101e 202 fail_free_mbuf_pool:
a96e0c77 203 while (i--)
dea3101e
JB
204 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
205 pool->elements[i].phys);
206 kfree(pool->elements);
a96e0c77 207 fail_free_lpfc_mbuf_pool:
dea3101e 208 pci_pool_destroy(phba->lpfc_mbuf_pool);
2e0fef85 209 phba->lpfc_mbuf_pool = NULL;
dea3101e 210 fail_free_dma_buf_pool:
895427bd
JS
211 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
212 phba->lpfc_sg_dma_buf_pool = NULL;
dea3101e
JB
213 fail:
214 return -ENOMEM;
215}
216
e59058c4 217/**
da0436e9 218 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
e59058c4
JS
219 * @phba: HBA to free memory for
220 *
da0436e9
JS
221 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
222 * routine is a the counterpart of lpfc_mem_alloc.
e59058c4
JS
223 *
224 * Returns: None
225 **/
dea3101e 226void
da0436e9 227lpfc_mem_free(struct lpfc_hba *phba)
dea3101e 228{
dea3101e 229 int i;
da0436e9 230 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
1ba981fd 231 struct lpfc_device_data *device_data;
dea3101e 232
da0436e9 233 /* Free HBQ pools */
ed957684 234 lpfc_sli_hbqbuf_free_all(phba);
8568a4d2
JS
235 if (phba->lpfc_drb_pool)
236 pci_pool_destroy(phba->lpfc_drb_pool);
da0436e9 237 phba->lpfc_drb_pool = NULL;
8568a4d2
JS
238 if (phba->lpfc_hrb_pool)
239 pci_pool_destroy(phba->lpfc_hrb_pool);
da0436e9 240 phba->lpfc_hrb_pool = NULL;
895427bd
JS
241 if (phba->txrdy_payload_pool)
242 pci_pool_destroy(phba->txrdy_payload_pool);
243 phba->txrdy_payload_pool = NULL;
da0436e9 244
8568a4d2
JS
245 if (phba->lpfc_hbq_pool)
246 pci_pool_destroy(phba->lpfc_hbq_pool);
247 phba->lpfc_hbq_pool = NULL;
d65c8fff
MP
248
249 if (phba->rrq_pool)
250 mempool_destroy(phba->rrq_pool);
21a688cd
JS
251 phba->rrq_pool = NULL;
252
da0436e9
JS
253 /* Free NLP memory pool */
254 mempool_destroy(phba->nlp_mem_pool);
255 phba->nlp_mem_pool = NULL;
d65c8fff 256 if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
cff261f6
JS
257 mempool_destroy(phba->active_rrq_pool);
258 phba->active_rrq_pool = NULL;
259 }
da0436e9
JS
260
261 /* Free mbox memory pool */
262 mempool_destroy(phba->mbox_mem_pool);
263 phba->mbox_mem_pool = NULL;
264
265 /* Free MBUF memory pool */
266 for (i = 0; i < pool->current_count; i++)
267 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
268 pool->elements[i].phys);
269 kfree(pool->elements);
270
271 pci_pool_destroy(phba->lpfc_mbuf_pool);
272 phba->lpfc_mbuf_pool = NULL;
ed957684 273
da0436e9 274 /* Free DMA buffer memory pool */
895427bd
JS
275 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
276 phba->lpfc_sg_dma_buf_pool = NULL;
da0436e9 277
1ba981fd
JS
278 /* Free Device Data memory pool */
279 if (phba->device_data_mem_pool) {
280 /* Ensure all objects have been returned to the pool */
281 while (!list_empty(&phba->luns)) {
282 device_data = list_first_entry(&phba->luns,
283 struct lpfc_device_data,
284 listentry);
285 list_del(&device_data->listentry);
286 mempool_free(device_data, phba->device_data_mem_pool);
287 }
288 mempool_destroy(phba->device_data_mem_pool);
289 }
290 phba->device_data_mem_pool = NULL;
da0436e9
JS
291 return;
292}
293
294/**
295 * lpfc_mem_free_all - Frees all PCI and driver memory
296 * @phba: HBA to free memory for
297 *
298 * Description: Free memory from PCI and driver memory pools and also those
895427bd 299 * used : lpfc_sg_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
da0436e9
JS
300 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
301 * the VPI bitmask.
302 *
303 * Returns: None
304 **/
305void
306lpfc_mem_free_all(struct lpfc_hba *phba)
307{
308 struct lpfc_sli *psli = &phba->sli;
309 LPFC_MBOXQ_t *mbox, *next_mbox;
310 struct lpfc_dmabuf *mp;
311
312 /* Free memory used in mailbox queue back to mailbox memory pool */
dea3101e
JB
313 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
314 mp = (struct lpfc_dmabuf *) (mbox->context1);
315 if (mp) {
316 lpfc_mbuf_free(phba, mp->virt, mp->phys);
317 kfree(mp);
318 }
319 list_del(&mbox->list);
320 mempool_free(mbox, phba->mbox_mem_pool);
321 }
da0436e9 322 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
92d7f7b0
JS
323 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
324 mp = (struct lpfc_dmabuf *) (mbox->context1);
325 if (mp) {
326 lpfc_mbuf_free(phba, mp->virt, mp->phys);
327 kfree(mp);
328 }
329 list_del(&mbox->list);
330 mempool_free(mbox, phba->mbox_mem_pool);
331 }
da0436e9
JS
332 /* Free the active mailbox command back to the mailbox memory pool */
333 spin_lock_irq(&phba->hbalock);
dea3101e 334 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
da0436e9 335 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
336 if (psli->mbox_active) {
337 mbox = psli->mbox_active;
338 mp = (struct lpfc_dmabuf *) (mbox->context1);
339 if (mp) {
340 lpfc_mbuf_free(phba, mp->virt, mp->phys);
341 kfree(mp);
342 }
343 mempool_free(mbox, phba->mbox_mem_pool);
344 psli->mbox_active = NULL;
345 }
346
da0436e9
JS
347 /* Free and destroy all the allocated memory pools */
348 lpfc_mem_free(phba);
2e0fef85 349
e59058c4 350 /* Free the iocb lookup array */
9f49d3b0
JS
351 kfree(psli->iocbq_lookup);
352 psli->iocbq_lookup = NULL;
da0436e9
JS
353
354 return;
dea3101e
JB
355}
356
e59058c4 357/**
3621a710 358 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
e59058c4
JS
359 * @phba: HBA which owns the pool to allocate from
360 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
361 * @handle: used to return the DMA-mapped address of the mbuf
362 *
363 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
364 * Allocates from generic pci_pool_alloc function first and if that fails and
365 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
366 * HBA's pool.
367 *
368 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
369 * phba->hbalock.
370 *
371 * Returns:
372 * pointer to the allocated mbuf on success
373 * NULL on failure
374 **/
dea3101e
JB
375void *
376lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
377{
378 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
2e0fef85 379 unsigned long iflags;
dea3101e
JB
380 void *ret;
381
382 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
383
2e0fef85 384 spin_lock_irqsave(&phba->hbalock, iflags);
92d7f7b0 385 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
dea3101e
JB
386 pool->current_count--;
387 ret = pool->elements[pool->current_count].virt;
388 *handle = pool->elements[pool->current_count].phys;
389 }
2e0fef85 390 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
391 return ret;
392}
393
e59058c4 394/**
3621a710 395 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
e59058c4
JS
396 * @phba: HBA which owns the pool to return to
397 * @virt: mbuf to free
398 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
399 *
400 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
401 * it is below its max_count, frees the mbuf otherwise.
402 *
403 * Notes: Must be called with phba->hbalock held to synchronize access to
404 * lpfc_mbuf_safety_pool.
405 *
406 * Returns: None
407 **/
dea3101e 408void
2e0fef85 409__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
dea3101e
JB
410{
411 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
412
413 if (pool->current_count < pool->max_count) {
414 pool->elements[pool->current_count].virt = virt;
415 pool->elements[pool->current_count].phys = dma;
416 pool->current_count++;
417 } else {
418 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
419 }
420 return;
421}
2e0fef85 422
e59058c4 423/**
3621a710 424 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
e59058c4
JS
425 * @phba: HBA which owns the pool to return to
426 * @virt: mbuf to free
427 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
428 *
429 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
430 * it is below its max_count, frees the mbuf otherwise.
431 *
432 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
433 *
434 * Returns: None
435 **/
2e0fef85
JS
436void
437lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
438{
439 unsigned long iflags;
440
441 spin_lock_irqsave(&phba->hbalock, iflags);
442 __lpfc_mbuf_free(phba, virt, dma);
443 spin_unlock_irqrestore(&phba->hbalock, iflags);
444 return;
445}
ed957684 446
f358dd0c
JS
447/**
448 * lpfc_nvmet_buf_alloc - Allocate an nvmet_buf from the
449 * lpfc_sg_dma_buf_pool PCI pool
450 * @phba: HBA which owns the pool to allocate from
451 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
452 * @handle: used to return the DMA-mapped address of the nvmet_buf
453 *
454 * Description: Allocates a DMA-mapped buffer from the lpfc_sg_dma_buf_pool
455 * PCI pool. Allocates from generic pci_pool_alloc function.
456 *
457 * Returns:
458 * pointer to the allocated nvmet_buf on success
459 * NULL on failure
460 **/
461void *
462lpfc_nvmet_buf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
463{
464 void *ret;
465
466 ret = pci_pool_alloc(phba->lpfc_sg_dma_buf_pool, GFP_KERNEL, handle);
467 return ret;
468}
469
470/**
471 * lpfc_nvmet_buf_free - Free an nvmet_buf from the lpfc_sg_dma_buf_pool
472 * PCI pool
473 * @phba: HBA which owns the pool to return to
474 * @virt: nvmet_buf to free
475 * @dma: the DMA-mapped address of the lpfc_sg_dma_buf_pool to be freed
476 *
477 * Returns: None
478 **/
479void
480lpfc_nvmet_buf_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma)
481{
482 pci_pool_free(phba->lpfc_sg_dma_buf_pool, virt, dma);
483}
484
e59058c4 485/**
3621a710 486 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
e59058c4
JS
487 * @phba: HBA to allocate HBQ buffer for
488 *
da0436e9 489 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
e59058c4
JS
490 * pool along a non-DMA-mapped container for it.
491 *
492 * Notes: Not interrupt-safe. Must be called with no locks held.
493 *
494 * Returns:
495 * pointer to HBQ on success
496 * NULL on failure
497 **/
51ef4c26
JS
498struct hbq_dmabuf *
499lpfc_els_hbq_alloc(struct lpfc_hba *phba)
ed957684 500{
51ef4c26
JS
501 struct hbq_dmabuf *hbqbp;
502
2e90f4b5 503 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
51ef4c26
JS
504 if (!hbqbp)
505 return NULL;
506
8568a4d2 507 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
51ef4c26
JS
508 &hbqbp->dbuf.phys);
509 if (!hbqbp->dbuf.virt) {
510 kfree(hbqbp);
511 return NULL;
512 }
895427bd 513 hbqbp->total_size = LPFC_BPL_SIZE;
51ef4c26 514 return hbqbp;
ed957684
JS
515}
516
e59058c4 517/**
da0436e9 518 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
e59058c4
JS
519 * @phba: HBA buffer was allocated for
520 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
521 *
522 * Description: Frees both the container and the DMA-mapped buffer returned by
523 * lpfc_els_hbq_alloc.
524 *
525 * Notes: Can be called with or without locks held.
526 *
527 * Returns: None
528 **/
ed957684 529void
51ef4c26 530lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
ed957684 531{
8568a4d2 532 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
51ef4c26 533 kfree(hbqbp);
ed957684
JS
534 return;
535}
536
da0436e9
JS
537/**
538 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
539 * @phba: HBA to allocate a receive buffer for
540 *
541 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
542 * pool along a non-DMA-mapped container for it.
543 *
544 * Notes: Not interrupt-safe. Must be called with no locks held.
545 *
546 * Returns:
547 * pointer to HBQ on success
548 * NULL on failure
549 **/
550struct hbq_dmabuf *
551lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
552{
553 struct hbq_dmabuf *dma_buf;
554
2e90f4b5 555 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
da0436e9
JS
556 if (!dma_buf)
557 return NULL;
558
559 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
560 &dma_buf->hbuf.phys);
561 if (!dma_buf->hbuf.virt) {
562 kfree(dma_buf);
563 return NULL;
564 }
565 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
566 &dma_buf->dbuf.phys);
567 if (!dma_buf->dbuf.virt) {
568 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
569 dma_buf->hbuf.phys);
570 kfree(dma_buf);
571 return NULL;
572 }
895427bd 573 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
da0436e9
JS
574 return dma_buf;
575}
576
577/**
578 * lpfc_sli4_rb_free - Frees a receive buffer
579 * @phba: HBA buffer was allocated for
580 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
581 *
582 * Description: Frees both the container and the DMA-mapped buffers returned by
583 * lpfc_sli4_rb_alloc.
584 *
585 * Notes: Can be called with or without locks held.
586 *
587 * Returns: None
588 **/
589void
590lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
591{
592 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
593 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
594 kfree(dmab);
da0436e9
JS
595}
596
f358dd0c
JS
597/**
598 * lpfc_sli4_nvmet_alloc - Allocate an SLI4 Receive buffer
599 * @phba: HBA to allocate a receive buffer for
600 *
601 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
602 * pool along a non-DMA-mapped container for it.
603 *
604 * Notes: Not interrupt-safe. Must be called with no locks held.
605 *
606 * Returns:
607 * pointer to HBQ on success
608 * NULL on failure
609 **/
610struct rqb_dmabuf *
611lpfc_sli4_nvmet_alloc(struct lpfc_hba *phba)
612{
613 struct rqb_dmabuf *dma_buf;
614 struct lpfc_iocbq *nvmewqe;
615 union lpfc_wqe128 *wqe;
616
617 dma_buf = kzalloc(sizeof(struct rqb_dmabuf), GFP_KERNEL);
618 if (!dma_buf)
619 return NULL;
620
621 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
622 &dma_buf->hbuf.phys);
623 if (!dma_buf->hbuf.virt) {
624 kfree(dma_buf);
625 return NULL;
626 }
627 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
628 &dma_buf->dbuf.phys);
629 if (!dma_buf->dbuf.virt) {
630 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
631 dma_buf->hbuf.phys);
632 kfree(dma_buf);
633 return NULL;
634 }
635 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
636
637 dma_buf->context = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx),
638 GFP_KERNEL);
639 if (!dma_buf->context) {
640 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
641 dma_buf->dbuf.phys);
642 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
643 dma_buf->hbuf.phys);
644 kfree(dma_buf);
645 return NULL;
646 }
647
648 dma_buf->iocbq = lpfc_sli_get_iocbq(phba);
649 dma_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
650 if (!dma_buf->iocbq) {
651 kfree(dma_buf->context);
652 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
653 dma_buf->dbuf.phys);
654 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
655 dma_buf->hbuf.phys);
656 kfree(dma_buf);
657 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
658 "2621 Ran out of nvmet iocb/WQEs\n");
659 return NULL;
660 }
661 nvmewqe = dma_buf->iocbq;
662 wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
663 /* Initialize WQE */
664 memset(wqe, 0, sizeof(union lpfc_wqe));
665 /* Word 7 */
666 bf_set(wqe_ct, &wqe->generic.wqe_com, SLI4_CT_RPI);
667 bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
668 bf_set(wqe_pu, &wqe->generic.wqe_com, 1);
669 /* Word 10 */
670 bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
671 bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
672 bf_set(wqe_qosd, &wqe->generic.wqe_com, 0);
673
674 dma_buf->iocbq->context1 = NULL;
675 spin_lock(&phba->sli4_hba.sgl_list_lock);
676 dma_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, dma_buf->iocbq);
677 spin_unlock(&phba->sli4_hba.sgl_list_lock);
678 if (!dma_buf->sglq) {
679 lpfc_sli_release_iocbq(phba, dma_buf->iocbq);
680 kfree(dma_buf->context);
681 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
682 dma_buf->dbuf.phys);
683 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
684 dma_buf->hbuf.phys);
685 kfree(dma_buf);
686 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
687 "6132 Ran out of nvmet XRIs\n");
688 return NULL;
689 }
690 return dma_buf;
691}
692
693/**
694 * lpfc_sli4_nvmet_free - Frees a receive buffer
695 * @phba: HBA buffer was allocated for
696 * @dmab: DMA Buffer container returned by lpfc_sli4_rbq_alloc
697 *
698 * Description: Frees both the container and the DMA-mapped buffers returned by
699 * lpfc_sli4_nvmet_alloc.
700 *
701 * Notes: Can be called with or without locks held.
702 *
703 * Returns: None
704 **/
705void
706lpfc_sli4_nvmet_free(struct lpfc_hba *phba, struct rqb_dmabuf *dmab)
707{
708 unsigned long flags;
709
710 __lpfc_clear_active_sglq(phba, dmab->sglq->sli4_lxritag);
711 dmab->sglq->state = SGL_FREED;
712 dmab->sglq->ndlp = NULL;
713
714 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, flags);
715 list_add_tail(&dmab->sglq->list, &phba->sli4_hba.lpfc_nvmet_sgl_list);
716 spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, flags);
717
718 lpfc_sli_release_iocbq(phba, dmab->iocbq);
719 kfree(dmab->context);
720 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
721 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
722 kfree(dmab);
723}
724
e59058c4 725/**
3621a710 726 * lpfc_in_buf_free - Free a DMA buffer
e59058c4
JS
727 * @phba: HBA buffer is associated with
728 * @mp: Buffer to free
729 *
730 * Description: Frees the given DMA buffer in the appropriate way given if the
731 * HBA is running in SLI3 mode with HBQs enabled.
732 *
733 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
734 *
735 * Returns: None
736 **/
92d7f7b0
JS
737void
738lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
739{
740 struct hbq_dmabuf *hbq_entry;
3163f725 741 unsigned long flags;
92d7f7b0 742
7f5f3d0d
JS
743 if (!mp)
744 return;
745
92d7f7b0 746 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
895427bd 747 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
3163f725
JS
748 /* Check whether HBQ is still in use */
749 spin_lock_irqsave(&phba->hbalock, flags);
750 if (!phba->hbq_in_use) {
751 spin_unlock_irqrestore(&phba->hbalock, flags);
752 return;
753 }
3163f725 754 list_del(&hbq_entry->dbuf.list);
92d7f7b0 755 if (hbq_entry->tag == -1) {
51ef4c26
JS
756 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
757 (phba, hbq_entry);
92d7f7b0
JS
758 } else {
759 lpfc_sli_free_hbq(phba, hbq_entry);
760 }
3163f725 761 spin_unlock_irqrestore(&phba->hbalock, flags);
92d7f7b0
JS
762 } else {
763 lpfc_mbuf_free(phba, mp->virt, mp->phys);
764 kfree(mp);
765 }
766 return;
767}
d613b6a7
JS
768
769/**
770 * lpfc_rq_buf_free - Free a RQ DMA buffer
771 * @phba: HBA buffer is associated with
772 * @mp: Buffer to free
773 *
774 * Description: Frees the given DMA buffer in the appropriate way given by
775 * reposting it to its associated RQ so it can be reused.
776 *
777 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
778 *
779 * Returns: None
780 **/
781void
782lpfc_rq_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
783{
784 struct lpfc_rqb *rqbp;
785 struct lpfc_rqe hrqe;
786 struct lpfc_rqe drqe;
787 struct rqb_dmabuf *rqb_entry;
788 unsigned long flags;
789 int rc;
790
791 if (!mp)
792 return;
793
794 rqb_entry = container_of(mp, struct rqb_dmabuf, hbuf);
795 rqbp = rqb_entry->hrq->rqbp;
796
797 spin_lock_irqsave(&phba->hbalock, flags);
798 list_del(&rqb_entry->hbuf.list);
799 hrqe.address_lo = putPaddrLow(rqb_entry->hbuf.phys);
800 hrqe.address_hi = putPaddrHigh(rqb_entry->hbuf.phys);
801 drqe.address_lo = putPaddrLow(rqb_entry->dbuf.phys);
802 drqe.address_hi = putPaddrHigh(rqb_entry->dbuf.phys);
803 rc = lpfc_sli4_rq_put(rqb_entry->hrq, rqb_entry->drq, &hrqe, &drqe);
804 if (rc < 0) {
805 (rqbp->rqb_free_buffer)(phba, rqb_entry);
806 } else {
807 list_add_tail(&rqb_entry->hbuf.list, &rqbp->rqb_buffer_list);
808 rqbp->buffer_count++;
809 }
810
811 spin_unlock_irqrestore(&phba->hbalock, flags);
812}