scsi: lpfc: NVME Target: bind to nvmet_fc api
[linux-2.6/btrfs-unstable.git] / drivers / scsi / lpfc / lpfc_mem.c
blob2437ec5f48631d28bea20c3c96e66ae851949e73
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
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.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_transport_fc.h>
30 #include <scsi/fc/fc_fs.h>
32 #include <linux/nvme-fc-driver.h>
34 #include "lpfc_hw4.h"
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_nl.h"
39 #include "lpfc_disc.h"
40 #include "lpfc.h"
41 #include "lpfc_scsi.h"
42 #include "lpfc_nvme.h"
43 #include "lpfc_nvmet.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
47 #define LPFC_MBUF_POOL_SIZE 64 /* max elements in MBUF safety pool */
48 #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
49 #define LPFC_DEVICE_DATA_POOL_SIZE 64 /* max elements in device data pool */
51 int
52 lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
53 size_t bytes;
54 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
56 if (max_xri <= 0)
57 return -ENOMEM;
58 bytes = ((BITS_PER_LONG - 1 + max_xri) / BITS_PER_LONG) *
59 sizeof(unsigned long);
60 phba->cfg_rrq_xri_bitmap_sz = bytes;
61 phba->active_rrq_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
62 bytes);
63 if (!phba->active_rrq_pool)
64 return -ENOMEM;
65 else
66 return 0;
69 /**
70 * lpfc_mem_alloc - create and allocate all PCI and memory pools
71 * @phba: HBA to allocate pools for
73 * Description: Creates and allocates PCI pools lpfc_sg_dma_buf_pool,
74 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
75 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
77 * Notes: Not interrupt-safe. Must be called with no locks held. If any
78 * allocation fails, frees all successfully allocated memory before returning.
80 * Returns:
81 * 0 on success
82 * -ENOMEM on failure (if any memory allocations fail)
83 **/
84 int
85 lpfc_mem_alloc(struct lpfc_hba *phba, int align)
87 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
88 int i;
90 if (phba->sli_rev == LPFC_SLI_REV4) {
91 /* Calculate alignment */
92 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
93 i = phba->cfg_sg_dma_buf_size;
94 else
95 i = SLI4_PAGE_SIZE;
97 phba->lpfc_sg_dma_buf_pool =
98 pci_pool_create("lpfc_sg_dma_buf_pool",
99 phba->pcidev,
100 phba->cfg_sg_dma_buf_size,
101 i, 0);
102 if (!phba->lpfc_sg_dma_buf_pool)
103 goto fail;
105 } else {
106 phba->lpfc_sg_dma_buf_pool =
107 pci_pool_create("lpfc_sg_dma_buf_pool",
108 phba->pcidev, phba->cfg_sg_dma_buf_size,
109 align, 0);
111 if (!phba->lpfc_sg_dma_buf_pool)
112 goto fail;
115 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
116 LPFC_BPL_SIZE,
117 align, 0);
118 if (!phba->lpfc_mbuf_pool)
119 goto fail_free_dma_buf_pool;
121 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
122 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
123 if (!pool->elements)
124 goto fail_free_lpfc_mbuf_pool;
126 pool->max_count = 0;
127 pool->current_count = 0;
128 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
129 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
130 GFP_KERNEL, &pool->elements[i].phys);
131 if (!pool->elements[i].virt)
132 goto fail_free_mbuf_pool;
133 pool->max_count++;
134 pool->current_count++;
137 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
138 sizeof(LPFC_MBOXQ_t));
139 if (!phba->mbox_mem_pool)
140 goto fail_free_mbuf_pool;
142 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
143 sizeof(struct lpfc_nodelist));
144 if (!phba->nlp_mem_pool)
145 goto fail_free_mbox_pool;
147 if (phba->sli_rev == LPFC_SLI_REV4) {
148 phba->rrq_pool =
149 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
150 sizeof(struct lpfc_node_rrq));
151 if (!phba->rrq_pool)
152 goto fail_free_nlp_mem_pool;
153 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
154 phba->pcidev,
155 LPFC_HDR_BUF_SIZE, align, 0);
156 if (!phba->lpfc_hrb_pool)
157 goto fail_free_rrq_mem_pool;
159 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
160 phba->pcidev,
161 LPFC_DATA_BUF_SIZE, align, 0);
162 if (!phba->lpfc_drb_pool)
163 goto fail_free_hrb_pool;
164 phba->lpfc_hbq_pool = NULL;
165 } else {
166 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
167 phba->pcidev, LPFC_BPL_SIZE, align, 0);
168 if (!phba->lpfc_hbq_pool)
169 goto fail_free_nlp_mem_pool;
170 phba->lpfc_hrb_pool = NULL;
171 phba->lpfc_drb_pool = NULL;
174 if (phba->cfg_EnableXLane) {
175 phba->device_data_mem_pool = mempool_create_kmalloc_pool(
176 LPFC_DEVICE_DATA_POOL_SIZE,
177 sizeof(struct lpfc_device_data));
178 if (!phba->device_data_mem_pool)
179 goto fail_free_drb_pool;
180 } else {
181 phba->device_data_mem_pool = NULL;
184 return 0;
185 fail_free_drb_pool:
186 pci_pool_destroy(phba->lpfc_drb_pool);
187 phba->lpfc_drb_pool = NULL;
188 fail_free_hrb_pool:
189 pci_pool_destroy(phba->lpfc_hrb_pool);
190 phba->lpfc_hrb_pool = NULL;
191 fail_free_rrq_mem_pool:
192 mempool_destroy(phba->rrq_pool);
193 phba->rrq_pool = NULL;
194 fail_free_nlp_mem_pool:
195 mempool_destroy(phba->nlp_mem_pool);
196 phba->nlp_mem_pool = NULL;
197 fail_free_mbox_pool:
198 mempool_destroy(phba->mbox_mem_pool);
199 phba->mbox_mem_pool = NULL;
200 fail_free_mbuf_pool:
201 while (i--)
202 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
203 pool->elements[i].phys);
204 kfree(pool->elements);
205 fail_free_lpfc_mbuf_pool:
206 pci_pool_destroy(phba->lpfc_mbuf_pool);
207 phba->lpfc_mbuf_pool = NULL;
208 fail_free_dma_buf_pool:
209 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
210 phba->lpfc_sg_dma_buf_pool = NULL;
211 fail:
212 return -ENOMEM;
216 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
217 * @phba: HBA to free memory for
219 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
220 * routine is a the counterpart of lpfc_mem_alloc.
222 * Returns: None
224 void
225 lpfc_mem_free(struct lpfc_hba *phba)
227 int i;
228 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
229 struct lpfc_device_data *device_data;
231 /* Free HBQ pools */
232 lpfc_sli_hbqbuf_free_all(phba);
233 if (phba->lpfc_drb_pool)
234 pci_pool_destroy(phba->lpfc_drb_pool);
235 phba->lpfc_drb_pool = NULL;
236 if (phba->lpfc_hrb_pool)
237 pci_pool_destroy(phba->lpfc_hrb_pool);
238 phba->lpfc_hrb_pool = NULL;
239 if (phba->txrdy_payload_pool)
240 pci_pool_destroy(phba->txrdy_payload_pool);
241 phba->txrdy_payload_pool = NULL;
243 if (phba->lpfc_hbq_pool)
244 pci_pool_destroy(phba->lpfc_hbq_pool);
245 phba->lpfc_hbq_pool = NULL;
247 if (phba->rrq_pool)
248 mempool_destroy(phba->rrq_pool);
249 phba->rrq_pool = NULL;
251 /* Free NLP memory pool */
252 mempool_destroy(phba->nlp_mem_pool);
253 phba->nlp_mem_pool = NULL;
254 if (phba->sli_rev == LPFC_SLI_REV4 && phba->active_rrq_pool) {
255 mempool_destroy(phba->active_rrq_pool);
256 phba->active_rrq_pool = NULL;
259 /* Free mbox memory pool */
260 mempool_destroy(phba->mbox_mem_pool);
261 phba->mbox_mem_pool = NULL;
263 /* Free MBUF memory pool */
264 for (i = 0; i < pool->current_count; i++)
265 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
266 pool->elements[i].phys);
267 kfree(pool->elements);
269 pci_pool_destroy(phba->lpfc_mbuf_pool);
270 phba->lpfc_mbuf_pool = NULL;
272 /* Free DMA buffer memory pool */
273 pci_pool_destroy(phba->lpfc_sg_dma_buf_pool);
274 phba->lpfc_sg_dma_buf_pool = NULL;
276 /* Free Device Data memory pool */
277 if (phba->device_data_mem_pool) {
278 /* Ensure all objects have been returned to the pool */
279 while (!list_empty(&phba->luns)) {
280 device_data = list_first_entry(&phba->luns,
281 struct lpfc_device_data,
282 listentry);
283 list_del(&device_data->listentry);
284 mempool_free(device_data, phba->device_data_mem_pool);
286 mempool_destroy(phba->device_data_mem_pool);
288 phba->device_data_mem_pool = NULL;
289 return;
293 * lpfc_mem_free_all - Frees all PCI and driver memory
294 * @phba: HBA to free memory for
296 * Description: Free memory from PCI and driver memory pools and also those
297 * used : lpfc_sg_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
298 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
299 * the VPI bitmask.
301 * Returns: None
303 void
304 lpfc_mem_free_all(struct lpfc_hba *phba)
306 struct lpfc_sli *psli = &phba->sli;
307 LPFC_MBOXQ_t *mbox, *next_mbox;
308 struct lpfc_dmabuf *mp;
310 /* Free memory used in mailbox queue back to mailbox memory pool */
311 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
312 mp = (struct lpfc_dmabuf *) (mbox->context1);
313 if (mp) {
314 lpfc_mbuf_free(phba, mp->virt, mp->phys);
315 kfree(mp);
317 list_del(&mbox->list);
318 mempool_free(mbox, phba->mbox_mem_pool);
320 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
321 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
322 mp = (struct lpfc_dmabuf *) (mbox->context1);
323 if (mp) {
324 lpfc_mbuf_free(phba, mp->virt, mp->phys);
325 kfree(mp);
327 list_del(&mbox->list);
328 mempool_free(mbox, phba->mbox_mem_pool);
330 /* Free the active mailbox command back to the mailbox memory pool */
331 spin_lock_irq(&phba->hbalock);
332 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
333 spin_unlock_irq(&phba->hbalock);
334 if (psli->mbox_active) {
335 mbox = psli->mbox_active;
336 mp = (struct lpfc_dmabuf *) (mbox->context1);
337 if (mp) {
338 lpfc_mbuf_free(phba, mp->virt, mp->phys);
339 kfree(mp);
341 mempool_free(mbox, phba->mbox_mem_pool);
342 psli->mbox_active = NULL;
345 /* Free and destroy all the allocated memory pools */
346 lpfc_mem_free(phba);
348 /* Free the iocb lookup array */
349 kfree(psli->iocbq_lookup);
350 psli->iocbq_lookup = NULL;
352 return;
356 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
357 * @phba: HBA which owns the pool to allocate from
358 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
359 * @handle: used to return the DMA-mapped address of the mbuf
361 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
362 * Allocates from generic pci_pool_alloc function first and if that fails and
363 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
364 * HBA's pool.
366 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
367 * phba->hbalock.
369 * Returns:
370 * pointer to the allocated mbuf on success
371 * NULL on failure
373 void *
374 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
376 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
377 unsigned long iflags;
378 void *ret;
380 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
382 spin_lock_irqsave(&phba->hbalock, iflags);
383 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
384 pool->current_count--;
385 ret = pool->elements[pool->current_count].virt;
386 *handle = pool->elements[pool->current_count].phys;
388 spin_unlock_irqrestore(&phba->hbalock, iflags);
389 return ret;
393 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
394 * @phba: HBA which owns the pool to return to
395 * @virt: mbuf to free
396 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
398 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
399 * it is below its max_count, frees the mbuf otherwise.
401 * Notes: Must be called with phba->hbalock held to synchronize access to
402 * lpfc_mbuf_safety_pool.
404 * Returns: None
406 void
407 __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
409 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
411 if (pool->current_count < pool->max_count) {
412 pool->elements[pool->current_count].virt = virt;
413 pool->elements[pool->current_count].phys = dma;
414 pool->current_count++;
415 } else {
416 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
418 return;
422 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
423 * @phba: HBA which owns the pool to return to
424 * @virt: mbuf to free
425 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
427 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
428 * it is below its max_count, frees the mbuf otherwise.
430 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
432 * Returns: None
434 void
435 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
437 unsigned long iflags;
439 spin_lock_irqsave(&phba->hbalock, iflags);
440 __lpfc_mbuf_free(phba, virt, dma);
441 spin_unlock_irqrestore(&phba->hbalock, iflags);
442 return;
446 * lpfc_nvmet_buf_alloc - Allocate an nvmet_buf from the
447 * lpfc_sg_dma_buf_pool PCI pool
448 * @phba: HBA which owns the pool to allocate from
449 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
450 * @handle: used to return the DMA-mapped address of the nvmet_buf
452 * Description: Allocates a DMA-mapped buffer from the lpfc_sg_dma_buf_pool
453 * PCI pool. Allocates from generic pci_pool_alloc function.
455 * Returns:
456 * pointer to the allocated nvmet_buf on success
457 * NULL on failure
459 void *
460 lpfc_nvmet_buf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
462 void *ret;
464 ret = pci_pool_alloc(phba->lpfc_sg_dma_buf_pool, GFP_KERNEL, handle);
465 return ret;
469 * lpfc_nvmet_buf_free - Free an nvmet_buf from the lpfc_sg_dma_buf_pool
470 * PCI pool
471 * @phba: HBA which owns the pool to return to
472 * @virt: nvmet_buf to free
473 * @dma: the DMA-mapped address of the lpfc_sg_dma_buf_pool to be freed
475 * Returns: None
477 void
478 lpfc_nvmet_buf_free(struct lpfc_hba *phba, void *virt, dma_addr_t dma)
480 pci_pool_free(phba->lpfc_sg_dma_buf_pool, virt, dma);
484 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
485 * @phba: HBA to allocate HBQ buffer for
487 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
488 * pool along a non-DMA-mapped container for it.
490 * Notes: Not interrupt-safe. Must be called with no locks held.
492 * Returns:
493 * pointer to HBQ on success
494 * NULL on failure
496 struct hbq_dmabuf *
497 lpfc_els_hbq_alloc(struct lpfc_hba *phba)
499 struct hbq_dmabuf *hbqbp;
501 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
502 if (!hbqbp)
503 return NULL;
505 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
506 &hbqbp->dbuf.phys);
507 if (!hbqbp->dbuf.virt) {
508 kfree(hbqbp);
509 return NULL;
511 hbqbp->total_size = LPFC_BPL_SIZE;
512 return hbqbp;
516 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
517 * @phba: HBA buffer was allocated for
518 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
520 * Description: Frees both the container and the DMA-mapped buffer returned by
521 * lpfc_els_hbq_alloc.
523 * Notes: Can be called with or without locks held.
525 * Returns: None
527 void
528 lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
530 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
531 kfree(hbqbp);
532 return;
536 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
537 * @phba: HBA to allocate a receive buffer for
539 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
540 * pool along a non-DMA-mapped container for it.
542 * Notes: Not interrupt-safe. Must be called with no locks held.
544 * Returns:
545 * pointer to HBQ on success
546 * NULL on failure
548 struct hbq_dmabuf *
549 lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
551 struct hbq_dmabuf *dma_buf;
553 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
554 if (!dma_buf)
555 return NULL;
557 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
558 &dma_buf->hbuf.phys);
559 if (!dma_buf->hbuf.virt) {
560 kfree(dma_buf);
561 return NULL;
563 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
564 &dma_buf->dbuf.phys);
565 if (!dma_buf->dbuf.virt) {
566 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
567 dma_buf->hbuf.phys);
568 kfree(dma_buf);
569 return NULL;
571 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
572 return dma_buf;
576 * lpfc_sli4_rb_free - Frees a receive buffer
577 * @phba: HBA buffer was allocated for
578 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
580 * Description: Frees both the container and the DMA-mapped buffers returned by
581 * lpfc_sli4_rb_alloc.
583 * Notes: Can be called with or without locks held.
585 * Returns: None
587 void
588 lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
590 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
591 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
592 kfree(dmab);
596 * lpfc_sli4_nvmet_alloc - Allocate an SLI4 Receive buffer
597 * @phba: HBA to allocate a receive buffer for
599 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
600 * pool along a non-DMA-mapped container for it.
602 * Notes: Not interrupt-safe. Must be called with no locks held.
604 * Returns:
605 * pointer to HBQ on success
606 * NULL on failure
608 struct rqb_dmabuf *
609 lpfc_sli4_nvmet_alloc(struct lpfc_hba *phba)
611 struct rqb_dmabuf *dma_buf;
612 struct lpfc_iocbq *nvmewqe;
613 union lpfc_wqe128 *wqe;
615 dma_buf = kzalloc(sizeof(struct rqb_dmabuf), GFP_KERNEL);
616 if (!dma_buf)
617 return NULL;
619 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
620 &dma_buf->hbuf.phys);
621 if (!dma_buf->hbuf.virt) {
622 kfree(dma_buf);
623 return NULL;
625 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
626 &dma_buf->dbuf.phys);
627 if (!dma_buf->dbuf.virt) {
628 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
629 dma_buf->hbuf.phys);
630 kfree(dma_buf);
631 return NULL;
633 dma_buf->total_size = LPFC_DATA_BUF_SIZE;
635 dma_buf->context = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx),
636 GFP_KERNEL);
637 if (!dma_buf->context) {
638 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
639 dma_buf->dbuf.phys);
640 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
641 dma_buf->hbuf.phys);
642 kfree(dma_buf);
643 return NULL;
646 dma_buf->iocbq = lpfc_sli_get_iocbq(phba);
647 dma_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
648 if (!dma_buf->iocbq) {
649 kfree(dma_buf->context);
650 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
651 dma_buf->dbuf.phys);
652 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
653 dma_buf->hbuf.phys);
654 kfree(dma_buf);
655 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
656 "2621 Ran out of nvmet iocb/WQEs\n");
657 return NULL;
659 nvmewqe = dma_buf->iocbq;
660 wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
661 /* Initialize WQE */
662 memset(wqe, 0, sizeof(union lpfc_wqe));
663 /* Word 7 */
664 bf_set(wqe_ct, &wqe->generic.wqe_com, SLI4_CT_RPI);
665 bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
666 bf_set(wqe_pu, &wqe->generic.wqe_com, 1);
667 /* Word 10 */
668 bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
669 bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
670 bf_set(wqe_qosd, &wqe->generic.wqe_com, 0);
672 dma_buf->iocbq->context1 = NULL;
673 spin_lock(&phba->sli4_hba.sgl_list_lock);
674 dma_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, dma_buf->iocbq);
675 spin_unlock(&phba->sli4_hba.sgl_list_lock);
676 if (!dma_buf->sglq) {
677 lpfc_sli_release_iocbq(phba, dma_buf->iocbq);
678 kfree(dma_buf->context);
679 pci_pool_free(phba->lpfc_drb_pool, dma_buf->dbuf.virt,
680 dma_buf->dbuf.phys);
681 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
682 dma_buf->hbuf.phys);
683 kfree(dma_buf);
684 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
685 "6132 Ran out of nvmet XRIs\n");
686 return NULL;
688 return dma_buf;
692 * lpfc_sli4_nvmet_free - Frees a receive buffer
693 * @phba: HBA buffer was allocated for
694 * @dmab: DMA Buffer container returned by lpfc_sli4_rbq_alloc
696 * Description: Frees both the container and the DMA-mapped buffers returned by
697 * lpfc_sli4_nvmet_alloc.
699 * Notes: Can be called with or without locks held.
701 * Returns: None
703 void
704 lpfc_sli4_nvmet_free(struct lpfc_hba *phba, struct rqb_dmabuf *dmab)
706 unsigned long flags;
708 __lpfc_clear_active_sglq(phba, dmab->sglq->sli4_lxritag);
709 dmab->sglq->state = SGL_FREED;
710 dmab->sglq->ndlp = NULL;
712 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, flags);
713 list_add_tail(&dmab->sglq->list, &phba->sli4_hba.lpfc_nvmet_sgl_list);
714 spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, flags);
716 lpfc_sli_release_iocbq(phba, dmab->iocbq);
717 kfree(dmab->context);
718 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
719 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
720 kfree(dmab);
724 * lpfc_in_buf_free - Free a DMA buffer
725 * @phba: HBA buffer is associated with
726 * @mp: Buffer to free
728 * Description: Frees the given DMA buffer in the appropriate way given if the
729 * HBA is running in SLI3 mode with HBQs enabled.
731 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
733 * Returns: None
735 void
736 lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
738 struct hbq_dmabuf *hbq_entry;
739 unsigned long flags;
741 if (!mp)
742 return;
744 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
745 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
746 /* Check whether HBQ is still in use */
747 spin_lock_irqsave(&phba->hbalock, flags);
748 if (!phba->hbq_in_use) {
749 spin_unlock_irqrestore(&phba->hbalock, flags);
750 return;
752 list_del(&hbq_entry->dbuf.list);
753 if (hbq_entry->tag == -1) {
754 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
755 (phba, hbq_entry);
756 } else {
757 lpfc_sli_free_hbq(phba, hbq_entry);
759 spin_unlock_irqrestore(&phba->hbalock, flags);
760 } else {
761 lpfc_mbuf_free(phba, mp->virt, mp->phys);
762 kfree(mp);
764 return;
768 * lpfc_rq_buf_free - Free a RQ DMA buffer
769 * @phba: HBA buffer is associated with
770 * @mp: Buffer to free
772 * Description: Frees the given DMA buffer in the appropriate way given by
773 * reposting it to its associated RQ so it can be reused.
775 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
777 * Returns: None
779 void
780 lpfc_rq_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
782 struct lpfc_rqb *rqbp;
783 struct lpfc_rqe hrqe;
784 struct lpfc_rqe drqe;
785 struct rqb_dmabuf *rqb_entry;
786 unsigned long flags;
787 int rc;
789 if (!mp)
790 return;
792 rqb_entry = container_of(mp, struct rqb_dmabuf, hbuf);
793 rqbp = rqb_entry->hrq->rqbp;
795 spin_lock_irqsave(&phba->hbalock, flags);
796 list_del(&rqb_entry->hbuf.list);
797 hrqe.address_lo = putPaddrLow(rqb_entry->hbuf.phys);
798 hrqe.address_hi = putPaddrHigh(rqb_entry->hbuf.phys);
799 drqe.address_lo = putPaddrLow(rqb_entry->dbuf.phys);
800 drqe.address_hi = putPaddrHigh(rqb_entry->dbuf.phys);
801 rc = lpfc_sli4_rq_put(rqb_entry->hrq, rqb_entry->drq, &hrqe, &drqe);
802 if (rc < 0) {
803 (rqbp->rqb_free_buffer)(phba, rqb_entry);
804 } else {
805 list_add_tail(&rqb_entry->hbuf.list, &rqbp->rqb_buffer_list);
806 rqbp->buffer_count++;
809 spin_unlock_irqrestore(&phba->hbalock, flags);