1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Avago Technologies. All rights reserved.
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/blk-mq.h>
9 #include <linux/parser.h>
10 #include <linux/random.h>
11 #include <uapi/scsi/fc/fc_fs.h>
12 #include <uapi/scsi/fc/fc_els.h>
15 #include <linux/nvme-fc-driver.h>
16 #include <linux/nvme-fc.h>
17 #include "../host/fc.h"
20 /* *************************** Data Structures/Defines ****************** */
23 #define NVMET_LS_CTX_COUNT 256
25 struct nvmet_fc_tgtport;
26 struct nvmet_fc_tgt_assoc;
28 struct nvmet_fc_ls_iod { /* for an LS RQST RCV */
29 struct nvmefc_ls_rsp *lsrsp;
30 struct nvmefc_tgt_fcp_req *fcpreq; /* only if RS */
32 struct list_head ls_rcv_list; /* tgtport->ls_rcv_list */
34 struct nvmet_fc_tgtport *tgtport;
35 struct nvmet_fc_tgt_assoc *assoc;
38 union nvmefc_ls_requests *rqstbuf;
39 union nvmefc_ls_responses *rspbuf;
43 struct scatterlist sg[2];
45 struct work_struct work;
46 } __aligned(sizeof(unsigned long long));
48 struct nvmet_fc_ls_req_op { /* for an LS RQST XMT */
49 struct nvmefc_ls_req ls_req;
51 struct nvmet_fc_tgtport *tgtport;
55 struct list_head lsreq_list; /* tgtport->ls_req_list */
60 /* desired maximum for a single sequence - if sg list allows it */
61 #define NVMET_FC_MAX_SEQ_LENGTH (256 * 1024)
63 enum nvmet_fcp_datadir {
70 struct nvmet_fc_fcp_iod {
71 struct nvmefc_tgt_fcp_req *fcpreq;
73 struct nvme_fc_cmd_iu cmdiubuf;
74 struct nvme_fc_ersp_iu rspiubuf;
76 struct scatterlist *next_sg;
77 struct scatterlist *data_sg;
80 enum nvmet_fcp_datadir io_dir;
88 struct work_struct defer_work;
90 struct nvmet_fc_tgtport *tgtport;
91 struct nvmet_fc_tgt_queue *queue;
93 struct list_head fcp_list; /* tgtport->fcp_list */
96 struct nvmet_fc_tgtport {
97 struct nvmet_fc_target_port fc_target_port;
99 struct list_head tgt_list; /* nvmet_fc_target_list */
100 struct device *dev; /* dev for dma mapping */
101 struct nvmet_fc_target_template *ops;
103 struct nvmet_fc_ls_iod *iod;
105 struct list_head ls_rcv_list;
106 struct list_head ls_req_list;
107 struct list_head ls_busylist;
108 struct list_head assoc_list;
109 struct list_head host_list;
110 struct ida assoc_cnt;
111 struct nvmet_fc_port_entry *pe;
116 struct nvmet_fc_port_entry {
117 struct nvmet_fc_tgtport *tgtport;
118 struct nvmet_port *port;
121 struct list_head pe_list;
124 struct nvmet_fc_defer_fcp_req {
125 struct list_head req_list;
126 struct nvmefc_tgt_fcp_req *fcp_req;
129 struct nvmet_fc_tgt_queue {
140 struct nvmet_cq nvme_cq;
141 struct nvmet_sq nvme_sq;
142 struct nvmet_fc_tgt_assoc *assoc;
143 struct list_head fod_list;
144 struct list_head pending_cmd_list;
145 struct list_head avail_defer_list;
146 struct workqueue_struct *work_q;
149 struct nvmet_fc_fcp_iod fod[]; /* array of fcp_iods */
150 } __aligned(sizeof(unsigned long long));
152 struct nvmet_fc_hostport {
153 struct nvmet_fc_tgtport *tgtport;
155 struct list_head host_list;
160 struct nvmet_fc_tgt_assoc {
163 atomic_t terminating;
164 struct nvmet_fc_tgtport *tgtport;
165 struct nvmet_fc_hostport *hostport;
166 struct nvmet_fc_ls_iod *rcv_disconn;
167 struct list_head a_list;
168 struct nvmet_fc_tgt_queue __rcu *queues[NVMET_NR_QUEUES + 1];
170 struct work_struct del_work;
176 nvmet_fc_iodnum(struct nvmet_fc_ls_iod *iodptr)
178 return (iodptr - iodptr->tgtport->iod);
182 nvmet_fc_fodnum(struct nvmet_fc_fcp_iod *fodptr)
184 return (fodptr - fodptr->queue->fod);
189 * Association and Connection IDs:
191 * Association ID will have random number in upper 6 bytes and zero
194 * Connection IDs will be Association ID with QID or'd in lower 2 bytes
196 * note: Association ID = Connection ID for queue 0
198 #define BYTES_FOR_QID sizeof(u16)
199 #define BYTES_FOR_QID_SHIFT (BYTES_FOR_QID * 8)
200 #define NVMET_FC_QUEUEID_MASK ((u64)((1 << BYTES_FOR_QID_SHIFT) - 1))
203 nvmet_fc_makeconnid(struct nvmet_fc_tgt_assoc *assoc, u16 qid)
205 return (assoc->association_id | qid);
209 nvmet_fc_getassociationid(u64 connectionid)
211 return connectionid & ~NVMET_FC_QUEUEID_MASK;
215 nvmet_fc_getqueueid(u64 connectionid)
217 return (u16)(connectionid & NVMET_FC_QUEUEID_MASK);
220 static inline struct nvmet_fc_tgtport *
221 targetport_to_tgtport(struct nvmet_fc_target_port *targetport)
223 return container_of(targetport, struct nvmet_fc_tgtport,
227 static inline struct nvmet_fc_fcp_iod *
228 nvmet_req_to_fod(struct nvmet_req *nvme_req)
230 return container_of(nvme_req, struct nvmet_fc_fcp_iod, req);
234 /* *************************** Globals **************************** */
237 static DEFINE_SPINLOCK(nvmet_fc_tgtlock);
239 static LIST_HEAD(nvmet_fc_target_list);
240 static DEFINE_IDA(nvmet_fc_tgtport_cnt);
241 static LIST_HEAD(nvmet_fc_portentry_list);
244 static void nvmet_fc_handle_ls_rqst_work(struct work_struct *work);
245 static void nvmet_fc_fcp_rqst_op_defer_work(struct work_struct *work);
246 static void nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc);
247 static int nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc);
248 static void nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue);
249 static int nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue);
250 static void nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport);
251 static int nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport);
252 static void nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
253 struct nvmet_fc_fcp_iod *fod);
254 static void nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc *assoc);
255 static void nvmet_fc_xmt_ls_rsp(struct nvmet_fc_tgtport *tgtport,
256 struct nvmet_fc_ls_iod *iod);
259 /* *********************** FC-NVME DMA Handling **************************** */
262 * The fcloop device passes in a NULL device pointer. Real LLD's will
263 * pass in a valid device pointer. If NULL is passed to the dma mapping
264 * routines, depending on the platform, it may or may not succeed, and
268 * Wrapper all the dma routines and check the dev pointer.
270 * If simple mappings (return just a dma address, we'll noop them,
271 * returning a dma address of 0.
273 * On more complex mappings (dma_map_sg), a pseudo routine fills
274 * in the scatter list, setting all dma addresses to 0.
277 static inline dma_addr_t
278 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
279 enum dma_data_direction dir)
281 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
285 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
287 return dev ? dma_mapping_error(dev, dma_addr) : 0;
291 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
292 enum dma_data_direction dir)
295 dma_unmap_single(dev, addr, size, dir);
299 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
300 enum dma_data_direction dir)
303 dma_sync_single_for_cpu(dev, addr, size, dir);
307 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
308 enum dma_data_direction dir)
311 dma_sync_single_for_device(dev, addr, size, dir);
314 /* pseudo dma_map_sg call */
316 fc_map_sg(struct scatterlist *sg, int nents)
318 struct scatterlist *s;
321 WARN_ON(nents == 0 || sg[0].length == 0);
323 for_each_sg(sg, s, nents, i) {
325 #ifdef CONFIG_NEED_SG_DMA_LENGTH
326 s->dma_length = s->length;
333 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
334 enum dma_data_direction dir)
336 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
340 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
341 enum dma_data_direction dir)
344 dma_unmap_sg(dev, sg, nents, dir);
348 /* ********************** FC-NVME LS XMT Handling ************************* */
352 __nvmet_fc_finish_ls_req(struct nvmet_fc_ls_req_op *lsop)
354 struct nvmet_fc_tgtport *tgtport = lsop->tgtport;
355 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
358 spin_lock_irqsave(&tgtport->lock, flags);
360 if (!lsop->req_queued) {
361 spin_unlock_irqrestore(&tgtport->lock, flags);
365 list_del(&lsop->lsreq_list);
367 lsop->req_queued = false;
369 spin_unlock_irqrestore(&tgtport->lock, flags);
371 fc_dma_unmap_single(tgtport->dev, lsreq->rqstdma,
372 (lsreq->rqstlen + lsreq->rsplen),
375 nvmet_fc_tgtport_put(tgtport);
379 __nvmet_fc_send_ls_req(struct nvmet_fc_tgtport *tgtport,
380 struct nvmet_fc_ls_req_op *lsop,
381 void (*done)(struct nvmefc_ls_req *req, int status))
383 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
387 if (!tgtport->ops->ls_req)
390 if (!nvmet_fc_tgtport_get(tgtport))
394 lsop->req_queued = false;
395 INIT_LIST_HEAD(&lsop->lsreq_list);
397 lsreq->rqstdma = fc_dma_map_single(tgtport->dev, lsreq->rqstaddr,
398 lsreq->rqstlen + lsreq->rsplen,
400 if (fc_dma_mapping_error(tgtport->dev, lsreq->rqstdma)) {
404 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
406 spin_lock_irqsave(&tgtport->lock, flags);
408 list_add_tail(&lsop->lsreq_list, &tgtport->ls_req_list);
410 lsop->req_queued = true;
412 spin_unlock_irqrestore(&tgtport->lock, flags);
414 ret = tgtport->ops->ls_req(&tgtport->fc_target_port, lsop->hosthandle,
422 lsop->ls_error = ret;
423 spin_lock_irqsave(&tgtport->lock, flags);
424 lsop->req_queued = false;
425 list_del(&lsop->lsreq_list);
426 spin_unlock_irqrestore(&tgtport->lock, flags);
427 fc_dma_unmap_single(tgtport->dev, lsreq->rqstdma,
428 (lsreq->rqstlen + lsreq->rsplen),
431 nvmet_fc_tgtport_put(tgtport);
437 nvmet_fc_send_ls_req_async(struct nvmet_fc_tgtport *tgtport,
438 struct nvmet_fc_ls_req_op *lsop,
439 void (*done)(struct nvmefc_ls_req *req, int status))
441 /* don't wait for completion */
443 return __nvmet_fc_send_ls_req(tgtport, lsop, done);
447 nvmet_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
449 struct nvmet_fc_ls_req_op *lsop =
450 container_of(lsreq, struct nvmet_fc_ls_req_op, ls_req);
452 __nvmet_fc_finish_ls_req(lsop);
454 /* fc-nvme target doesn't care about success or failure of cmd */
460 * This routine sends a FC-NVME LS to disconnect (aka terminate)
461 * the FC-NVME Association. Terminating the association also
462 * terminates the FC-NVME connections (per queue, both admin and io
463 * queues) that are part of the association. E.g. things are torn
464 * down, and the related FC-NVME Association ID and Connection IDs
467 * The behavior of the fc-nvme target is such that it's
468 * understanding of the association and connections will implicitly
469 * be torn down. The action is implicit as it may be due to a loss of
470 * connectivity with the fc-nvme host, so the target may never get a
471 * response even if it tried. As such, the action of this routine
472 * is to asynchronously send the LS, ignore any results of the LS, and
473 * continue on with terminating the association. If the fc-nvme host
474 * is present and receives the LS, it too can tear down.
477 nvmet_fc_xmt_disconnect_assoc(struct nvmet_fc_tgt_assoc *assoc)
479 struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
480 struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
481 struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
482 struct nvmet_fc_ls_req_op *lsop;
483 struct nvmefc_ls_req *lsreq;
487 * If ls_req is NULL or no hosthandle, it's an older lldd and no
488 * message is normal. Otherwise, send unless the hostport has
489 * already been invalidated by the lldd.
491 if (!tgtport->ops->ls_req || !assoc->hostport ||
492 assoc->hostport->invalid)
495 lsop = kzalloc((sizeof(*lsop) +
496 sizeof(*discon_rqst) + sizeof(*discon_acc) +
497 tgtport->ops->lsrqst_priv_sz), GFP_KERNEL);
499 dev_info(tgtport->dev,
500 "{%d:%d} send Disconnect Association failed: ENOMEM\n",
501 tgtport->fc_target_port.port_num, assoc->a_id);
505 discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
506 discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
507 lsreq = &lsop->ls_req;
508 if (tgtport->ops->lsrqst_priv_sz)
509 lsreq->private = (void *)&discon_acc[1];
511 lsreq->private = NULL;
513 lsop->tgtport = tgtport;
514 lsop->hosthandle = assoc->hostport->hosthandle;
516 nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
517 assoc->association_id);
519 ret = nvmet_fc_send_ls_req_async(tgtport, lsop,
520 nvmet_fc_disconnect_assoc_done);
522 dev_info(tgtport->dev,
523 "{%d:%d} XMT Disconnect Association failed: %d\n",
524 tgtport->fc_target_port.port_num, assoc->a_id, ret);
530 /* *********************** FC-NVME Port Management ************************ */
534 nvmet_fc_alloc_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
536 struct nvmet_fc_ls_iod *iod;
539 iod = kcalloc(NVMET_LS_CTX_COUNT, sizeof(struct nvmet_fc_ls_iod),
546 for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
547 INIT_WORK(&iod->work, nvmet_fc_handle_ls_rqst_work);
548 iod->tgtport = tgtport;
549 list_add_tail(&iod->ls_rcv_list, &tgtport->ls_rcv_list);
551 iod->rqstbuf = kzalloc(sizeof(union nvmefc_ls_requests) +
552 sizeof(union nvmefc_ls_responses),
557 iod->rspbuf = (union nvmefc_ls_responses *)&iod->rqstbuf[1];
559 iod->rspdma = fc_dma_map_single(tgtport->dev, iod->rspbuf,
560 sizeof(*iod->rspbuf),
562 if (fc_dma_mapping_error(tgtport->dev, iod->rspdma))
570 list_del(&iod->ls_rcv_list);
571 for (iod--, i--; i >= 0; iod--, i--) {
572 fc_dma_unmap_single(tgtport->dev, iod->rspdma,
573 sizeof(*iod->rspbuf), DMA_TO_DEVICE);
575 list_del(&iod->ls_rcv_list);
584 nvmet_fc_free_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
586 struct nvmet_fc_ls_iod *iod = tgtport->iod;
589 for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
590 fc_dma_unmap_single(tgtport->dev,
591 iod->rspdma, sizeof(*iod->rspbuf),
594 list_del(&iod->ls_rcv_list);
599 static struct nvmet_fc_ls_iod *
600 nvmet_fc_alloc_ls_iod(struct nvmet_fc_tgtport *tgtport)
602 struct nvmet_fc_ls_iod *iod;
605 spin_lock_irqsave(&tgtport->lock, flags);
606 iod = list_first_entry_or_null(&tgtport->ls_rcv_list,
607 struct nvmet_fc_ls_iod, ls_rcv_list);
609 list_move_tail(&iod->ls_rcv_list, &tgtport->ls_busylist);
610 spin_unlock_irqrestore(&tgtport->lock, flags);
616 nvmet_fc_free_ls_iod(struct nvmet_fc_tgtport *tgtport,
617 struct nvmet_fc_ls_iod *iod)
621 spin_lock_irqsave(&tgtport->lock, flags);
622 list_move(&iod->ls_rcv_list, &tgtport->ls_rcv_list);
623 spin_unlock_irqrestore(&tgtport->lock, flags);
627 nvmet_fc_prep_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
628 struct nvmet_fc_tgt_queue *queue)
630 struct nvmet_fc_fcp_iod *fod = queue->fod;
633 for (i = 0; i < queue->sqsize; fod++, i++) {
634 INIT_WORK(&fod->defer_work, nvmet_fc_fcp_rqst_op_defer_work);
635 fod->tgtport = tgtport;
639 fod->aborted = false;
641 list_add_tail(&fod->fcp_list, &queue->fod_list);
642 spin_lock_init(&fod->flock);
644 fod->rspdma = fc_dma_map_single(tgtport->dev, &fod->rspiubuf,
645 sizeof(fod->rspiubuf), DMA_TO_DEVICE);
646 if (fc_dma_mapping_error(tgtport->dev, fod->rspdma)) {
647 list_del(&fod->fcp_list);
648 for (fod--, i--; i >= 0; fod--, i--) {
649 fc_dma_unmap_single(tgtport->dev, fod->rspdma,
650 sizeof(fod->rspiubuf),
653 list_del(&fod->fcp_list);
662 nvmet_fc_destroy_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
663 struct nvmet_fc_tgt_queue *queue)
665 struct nvmet_fc_fcp_iod *fod = queue->fod;
668 for (i = 0; i < queue->sqsize; fod++, i++) {
670 fc_dma_unmap_single(tgtport->dev, fod->rspdma,
671 sizeof(fod->rspiubuf), DMA_TO_DEVICE);
675 static struct nvmet_fc_fcp_iod *
676 nvmet_fc_alloc_fcp_iod(struct nvmet_fc_tgt_queue *queue)
678 struct nvmet_fc_fcp_iod *fod;
680 lockdep_assert_held(&queue->qlock);
682 fod = list_first_entry_or_null(&queue->fod_list,
683 struct nvmet_fc_fcp_iod, fcp_list);
685 list_del(&fod->fcp_list);
688 * no queue reference is taken, as it was taken by the
689 * queue lookup just prior to the allocation. The iod
690 * will "inherit" that reference.
698 nvmet_fc_queue_fcp_req(struct nvmet_fc_tgtport *tgtport,
699 struct nvmet_fc_tgt_queue *queue,
700 struct nvmefc_tgt_fcp_req *fcpreq)
702 struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
705 * put all admin cmds on hw queue id 0. All io commands go to
706 * the respective hw queue based on a modulo basis
708 fcpreq->hwqid = queue->qid ?
709 ((queue->qid - 1) % tgtport->ops->max_hw_queues) : 0;
711 nvmet_fc_handle_fcp_rqst(tgtport, fod);
715 nvmet_fc_fcp_rqst_op_defer_work(struct work_struct *work)
717 struct nvmet_fc_fcp_iod *fod =
718 container_of(work, struct nvmet_fc_fcp_iod, defer_work);
720 /* Submit deferred IO for processing */
721 nvmet_fc_queue_fcp_req(fod->tgtport, fod->queue, fod->fcpreq);
726 nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
727 struct nvmet_fc_fcp_iod *fod)
729 struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
730 struct nvmet_fc_tgtport *tgtport = fod->tgtport;
731 struct nvmet_fc_defer_fcp_req *deferfcp;
734 fc_dma_sync_single_for_cpu(tgtport->dev, fod->rspdma,
735 sizeof(fod->rspiubuf), DMA_TO_DEVICE);
737 fcpreq->nvmet_fc_private = NULL;
741 fod->aborted = false;
742 fod->writedataactive = false;
745 tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
747 /* release the queue lookup reference on the completed IO */
748 nvmet_fc_tgt_q_put(queue);
750 spin_lock_irqsave(&queue->qlock, flags);
751 deferfcp = list_first_entry_or_null(&queue->pending_cmd_list,
752 struct nvmet_fc_defer_fcp_req, req_list);
754 list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
755 spin_unlock_irqrestore(&queue->qlock, flags);
759 /* Re-use the fod for the next pending cmd that was deferred */
760 list_del(&deferfcp->req_list);
762 fcpreq = deferfcp->fcp_req;
764 /* deferfcp can be reused for another IO at a later date */
765 list_add_tail(&deferfcp->req_list, &queue->avail_defer_list);
767 spin_unlock_irqrestore(&queue->qlock, flags);
769 /* Save NVME CMD IO in fod */
770 memcpy(&fod->cmdiubuf, fcpreq->rspaddr, fcpreq->rsplen);
772 /* Setup new fcpreq to be processed */
773 fcpreq->rspaddr = NULL;
775 fcpreq->nvmet_fc_private = fod;
776 fod->fcpreq = fcpreq;
779 /* inform LLDD IO is now being processed */
780 tgtport->ops->defer_rcv(&tgtport->fc_target_port, fcpreq);
783 * Leave the queue lookup get reference taken when
784 * fod was originally allocated.
787 queue_work(queue->work_q, &fod->defer_work);
790 static struct nvmet_fc_tgt_queue *
791 nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
794 struct nvmet_fc_tgt_queue *queue;
797 if (qid > NVMET_NR_QUEUES)
800 queue = kzalloc(struct_size(queue, fod, sqsize), GFP_KERNEL);
804 if (!nvmet_fc_tgt_a_get(assoc))
807 queue->work_q = alloc_workqueue("ntfc%d.%d.%d", 0, 0,
808 assoc->tgtport->fc_target_port.port_num,
814 queue->sqsize = sqsize;
815 queue->assoc = assoc;
816 INIT_LIST_HEAD(&queue->fod_list);
817 INIT_LIST_HEAD(&queue->avail_defer_list);
818 INIT_LIST_HEAD(&queue->pending_cmd_list);
819 atomic_set(&queue->connected, 0);
820 atomic_set(&queue->sqtail, 0);
821 atomic_set(&queue->rsn, 1);
822 atomic_set(&queue->zrspcnt, 0);
823 spin_lock_init(&queue->qlock);
824 kref_init(&queue->ref);
826 nvmet_fc_prep_fcp_iodlist(assoc->tgtport, queue);
828 ret = nvmet_sq_init(&queue->nvme_sq);
830 goto out_fail_iodlist;
832 WARN_ON(assoc->queues[qid]);
833 rcu_assign_pointer(assoc->queues[qid], queue);
838 nvmet_fc_destroy_fcp_iodlist(assoc->tgtport, queue);
839 destroy_workqueue(queue->work_q);
841 nvmet_fc_tgt_a_put(assoc);
849 nvmet_fc_tgt_queue_free(struct kref *ref)
851 struct nvmet_fc_tgt_queue *queue =
852 container_of(ref, struct nvmet_fc_tgt_queue, ref);
854 rcu_assign_pointer(queue->assoc->queues[queue->qid], NULL);
856 nvmet_fc_destroy_fcp_iodlist(queue->assoc->tgtport, queue);
858 nvmet_fc_tgt_a_put(queue->assoc);
860 destroy_workqueue(queue->work_q);
862 kfree_rcu(queue, rcu);
866 nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue)
868 kref_put(&queue->ref, nvmet_fc_tgt_queue_free);
872 nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue)
874 return kref_get_unless_zero(&queue->ref);
879 nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
881 struct nvmet_fc_tgtport *tgtport = queue->assoc->tgtport;
882 struct nvmet_fc_fcp_iod *fod = queue->fod;
883 struct nvmet_fc_defer_fcp_req *deferfcp, *tempptr;
888 disconnect = atomic_xchg(&queue->connected, 0);
890 /* if not connected, nothing to do */
894 spin_lock_irqsave(&queue->qlock, flags);
895 /* abort outstanding io's */
896 for (i = 0; i < queue->sqsize; fod++, i++) {
898 spin_lock(&fod->flock);
901 * only call lldd abort routine if waiting for
902 * writedata. other outstanding ops should finish
905 if (fod->writedataactive) {
907 spin_unlock(&fod->flock);
908 tgtport->ops->fcp_abort(
909 &tgtport->fc_target_port, fod->fcpreq);
911 spin_unlock(&fod->flock);
915 /* Cleanup defer'ed IOs in queue */
916 list_for_each_entry_safe(deferfcp, tempptr, &queue->avail_defer_list,
918 list_del(&deferfcp->req_list);
923 deferfcp = list_first_entry_or_null(&queue->pending_cmd_list,
924 struct nvmet_fc_defer_fcp_req, req_list);
928 list_del(&deferfcp->req_list);
929 spin_unlock_irqrestore(&queue->qlock, flags);
931 tgtport->ops->defer_rcv(&tgtport->fc_target_port,
934 tgtport->ops->fcp_abort(&tgtport->fc_target_port,
937 tgtport->ops->fcp_req_release(&tgtport->fc_target_port,
940 /* release the queue lookup reference */
941 nvmet_fc_tgt_q_put(queue);
945 spin_lock_irqsave(&queue->qlock, flags);
947 spin_unlock_irqrestore(&queue->qlock, flags);
949 flush_workqueue(queue->work_q);
951 nvmet_sq_destroy(&queue->nvme_sq);
953 nvmet_fc_tgt_q_put(queue);
956 static struct nvmet_fc_tgt_queue *
957 nvmet_fc_find_target_queue(struct nvmet_fc_tgtport *tgtport,
960 struct nvmet_fc_tgt_assoc *assoc;
961 struct nvmet_fc_tgt_queue *queue;
962 u64 association_id = nvmet_fc_getassociationid(connection_id);
963 u16 qid = nvmet_fc_getqueueid(connection_id);
965 if (qid > NVMET_NR_QUEUES)
969 list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
970 if (association_id == assoc->association_id) {
971 queue = rcu_dereference(assoc->queues[qid]);
973 (!atomic_read(&queue->connected) ||
974 !nvmet_fc_tgt_q_get(queue)))
985 nvmet_fc_hostport_free(struct kref *ref)
987 struct nvmet_fc_hostport *hostport =
988 container_of(ref, struct nvmet_fc_hostport, ref);
989 struct nvmet_fc_tgtport *tgtport = hostport->tgtport;
992 spin_lock_irqsave(&tgtport->lock, flags);
993 list_del(&hostport->host_list);
994 spin_unlock_irqrestore(&tgtport->lock, flags);
995 if (tgtport->ops->host_release && hostport->invalid)
996 tgtport->ops->host_release(hostport->hosthandle);
998 nvmet_fc_tgtport_put(tgtport);
1002 nvmet_fc_hostport_put(struct nvmet_fc_hostport *hostport)
1004 kref_put(&hostport->ref, nvmet_fc_hostport_free);
1008 nvmet_fc_hostport_get(struct nvmet_fc_hostport *hostport)
1010 return kref_get_unless_zero(&hostport->ref);
1014 nvmet_fc_free_hostport(struct nvmet_fc_hostport *hostport)
1016 /* if LLDD not implemented, leave as NULL */
1017 if (!hostport || !hostport->hosthandle)
1020 nvmet_fc_hostport_put(hostport);
1023 static struct nvmet_fc_hostport *
1024 nvmet_fc_alloc_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
1026 struct nvmet_fc_hostport *newhost, *host, *match = NULL;
1027 unsigned long flags;
1029 /* if LLDD not implemented, leave as NULL */
1033 /* take reference for what will be the newly allocated hostport */
1034 if (!nvmet_fc_tgtport_get(tgtport))
1035 return ERR_PTR(-EINVAL);
1037 newhost = kzalloc(sizeof(*newhost), GFP_KERNEL);
1039 spin_lock_irqsave(&tgtport->lock, flags);
1040 list_for_each_entry(host, &tgtport->host_list, host_list) {
1041 if (host->hosthandle == hosthandle && !host->invalid) {
1042 if (nvmet_fc_hostport_get(host)) {
1048 spin_unlock_irqrestore(&tgtport->lock, flags);
1049 /* no allocation - release reference */
1050 nvmet_fc_tgtport_put(tgtport);
1051 return (match) ? match : ERR_PTR(-ENOMEM);
1054 newhost->tgtport = tgtport;
1055 newhost->hosthandle = hosthandle;
1056 INIT_LIST_HEAD(&newhost->host_list);
1057 kref_init(&newhost->ref);
1059 spin_lock_irqsave(&tgtport->lock, flags);
1060 list_for_each_entry(host, &tgtport->host_list, host_list) {
1061 if (host->hosthandle == hosthandle && !host->invalid) {
1062 if (nvmet_fc_hostport_get(host)) {
1071 /* releasing allocation - release reference */
1072 nvmet_fc_tgtport_put(tgtport);
1074 list_add_tail(&newhost->host_list, &tgtport->host_list);
1075 spin_unlock_irqrestore(&tgtport->lock, flags);
1077 return (match) ? match : newhost;
1081 nvmet_fc_delete_assoc(struct work_struct *work)
1083 struct nvmet_fc_tgt_assoc *assoc =
1084 container_of(work, struct nvmet_fc_tgt_assoc, del_work);
1086 nvmet_fc_delete_target_assoc(assoc);
1087 nvmet_fc_tgt_a_put(assoc);
1090 static struct nvmet_fc_tgt_assoc *
1091 nvmet_fc_alloc_target_assoc(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
1093 struct nvmet_fc_tgt_assoc *assoc, *tmpassoc;
1094 unsigned long flags;
1097 bool needrandom = true;
1099 assoc = kzalloc(sizeof(*assoc), GFP_KERNEL);
1103 idx = ida_simple_get(&tgtport->assoc_cnt, 0, 0, GFP_KERNEL);
1105 goto out_free_assoc;
1107 if (!nvmet_fc_tgtport_get(tgtport))
1110 assoc->hostport = nvmet_fc_alloc_hostport(tgtport, hosthandle);
1111 if (IS_ERR(assoc->hostport))
1114 assoc->tgtport = tgtport;
1116 INIT_LIST_HEAD(&assoc->a_list);
1117 kref_init(&assoc->ref);
1118 INIT_WORK(&assoc->del_work, nvmet_fc_delete_assoc);
1119 atomic_set(&assoc->terminating, 0);
1121 while (needrandom) {
1122 get_random_bytes(&ran, sizeof(ran) - BYTES_FOR_QID);
1123 ran = ran << BYTES_FOR_QID_SHIFT;
1125 spin_lock_irqsave(&tgtport->lock, flags);
1127 list_for_each_entry(tmpassoc, &tgtport->assoc_list, a_list) {
1128 if (ran == tmpassoc->association_id) {
1134 assoc->association_id = ran;
1135 list_add_tail_rcu(&assoc->a_list, &tgtport->assoc_list);
1137 spin_unlock_irqrestore(&tgtport->lock, flags);
1143 nvmet_fc_tgtport_put(tgtport);
1145 ida_simple_remove(&tgtport->assoc_cnt, idx);
1152 nvmet_fc_target_assoc_free(struct kref *ref)
1154 struct nvmet_fc_tgt_assoc *assoc =
1155 container_of(ref, struct nvmet_fc_tgt_assoc, ref);
1156 struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
1157 struct nvmet_fc_ls_iod *oldls;
1158 unsigned long flags;
1160 /* Send Disconnect now that all i/o has completed */
1161 nvmet_fc_xmt_disconnect_assoc(assoc);
1163 nvmet_fc_free_hostport(assoc->hostport);
1164 spin_lock_irqsave(&tgtport->lock, flags);
1165 list_del_rcu(&assoc->a_list);
1166 oldls = assoc->rcv_disconn;
1167 spin_unlock_irqrestore(&tgtport->lock, flags);
1168 /* if pending Rcv Disconnect Association LS, send rsp now */
1170 nvmet_fc_xmt_ls_rsp(tgtport, oldls);
1171 ida_simple_remove(&tgtport->assoc_cnt, assoc->a_id);
1172 dev_info(tgtport->dev,
1173 "{%d:%d} Association freed\n",
1174 tgtport->fc_target_port.port_num, assoc->a_id);
1175 kfree_rcu(assoc, rcu);
1176 nvmet_fc_tgtport_put(tgtport);
1180 nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc)
1182 kref_put(&assoc->ref, nvmet_fc_target_assoc_free);
1186 nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc)
1188 return kref_get_unless_zero(&assoc->ref);
1192 nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc *assoc)
1194 struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
1195 struct nvmet_fc_tgt_queue *queue;
1198 terminating = atomic_xchg(&assoc->terminating, 1);
1200 /* if already terminating, do nothing */
1205 for (i = NVMET_NR_QUEUES; i >= 0; i--) {
1207 queue = rcu_dereference(assoc->queues[i]);
1213 if (!nvmet_fc_tgt_q_get(queue)) {
1218 nvmet_fc_delete_target_queue(queue);
1219 nvmet_fc_tgt_q_put(queue);
1222 dev_info(tgtport->dev,
1223 "{%d:%d} Association deleted\n",
1224 tgtport->fc_target_port.port_num, assoc->a_id);
1226 nvmet_fc_tgt_a_put(assoc);
1229 static struct nvmet_fc_tgt_assoc *
1230 nvmet_fc_find_target_assoc(struct nvmet_fc_tgtport *tgtport,
1233 struct nvmet_fc_tgt_assoc *assoc;
1234 struct nvmet_fc_tgt_assoc *ret = NULL;
1237 list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
1238 if (association_id == assoc->association_id) {
1240 if (!nvmet_fc_tgt_a_get(assoc))
1251 nvmet_fc_portentry_bind(struct nvmet_fc_tgtport *tgtport,
1252 struct nvmet_fc_port_entry *pe,
1253 struct nvmet_port *port)
1255 lockdep_assert_held(&nvmet_fc_tgtlock);
1257 pe->tgtport = tgtport;
1263 pe->node_name = tgtport->fc_target_port.node_name;
1264 pe->port_name = tgtport->fc_target_port.port_name;
1265 INIT_LIST_HEAD(&pe->pe_list);
1267 list_add_tail(&pe->pe_list, &nvmet_fc_portentry_list);
1271 nvmet_fc_portentry_unbind(struct nvmet_fc_port_entry *pe)
1273 unsigned long flags;
1275 spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1277 pe->tgtport->pe = NULL;
1278 list_del(&pe->pe_list);
1279 spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1283 * called when a targetport deregisters. Breaks the relationship
1284 * with the nvmet port, but leaves the port_entry in place so that
1285 * re-registration can resume operation.
1288 nvmet_fc_portentry_unbind_tgt(struct nvmet_fc_tgtport *tgtport)
1290 struct nvmet_fc_port_entry *pe;
1291 unsigned long flags;
1293 spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1298 spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1302 * called when a new targetport is registered. Looks in the
1303 * existing nvmet port_entries to see if the nvmet layer is
1304 * configured for the targetport's wwn's. (the targetport existed,
1305 * nvmet configured, the lldd unregistered the tgtport, and is now
1306 * reregistering the same targetport). If so, set the nvmet port
1307 * port entry on the targetport.
1310 nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
1312 struct nvmet_fc_port_entry *pe;
1313 unsigned long flags;
1315 spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1316 list_for_each_entry(pe, &nvmet_fc_portentry_list, pe_list) {
1317 if (tgtport->fc_target_port.node_name == pe->node_name &&
1318 tgtport->fc_target_port.port_name == pe->port_name) {
1319 WARN_ON(pe->tgtport);
1321 pe->tgtport = tgtport;
1325 spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1329 * nvme_fc_register_targetport - transport entry point called by an
1330 * LLDD to register the existence of a local
1331 * NVME subystem FC port.
1332 * @pinfo: pointer to information about the port to be registered
1333 * @template: LLDD entrypoints and operational parameters for the port
1334 * @dev: physical hardware device node port corresponds to. Will be
1335 * used for DMA mappings
1336 * @portptr: pointer to a local port pointer. Upon success, the routine
1337 * will allocate a nvme_fc_local_port structure and place its
1338 * address in the local port pointer. Upon failure, local port
1339 * pointer will be set to NULL.
1342 * a completion status. Must be 0 upon success; a negative errno
1343 * (ex: -ENXIO) upon failure.
1346 nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
1347 struct nvmet_fc_target_template *template,
1349 struct nvmet_fc_target_port **portptr)
1351 struct nvmet_fc_tgtport *newrec;
1352 unsigned long flags;
1355 if (!template->xmt_ls_rsp || !template->fcp_op ||
1356 !template->fcp_abort ||
1357 !template->fcp_req_release || !template->targetport_delete ||
1358 !template->max_hw_queues || !template->max_sgl_segments ||
1359 !template->max_dif_sgl_segments || !template->dma_boundary) {
1361 goto out_regtgt_failed;
1364 newrec = kzalloc((sizeof(*newrec) + template->target_priv_sz),
1368 goto out_regtgt_failed;
1371 idx = ida_simple_get(&nvmet_fc_tgtport_cnt, 0, 0, GFP_KERNEL);
1374 goto out_fail_kfree;
1377 if (!get_device(dev) && dev) {
1382 newrec->fc_target_port.node_name = pinfo->node_name;
1383 newrec->fc_target_port.port_name = pinfo->port_name;
1384 if (template->target_priv_sz)
1385 newrec->fc_target_port.private = &newrec[1];
1387 newrec->fc_target_port.private = NULL;
1388 newrec->fc_target_port.port_id = pinfo->port_id;
1389 newrec->fc_target_port.port_num = idx;
1390 INIT_LIST_HEAD(&newrec->tgt_list);
1392 newrec->ops = template;
1393 spin_lock_init(&newrec->lock);
1394 INIT_LIST_HEAD(&newrec->ls_rcv_list);
1395 INIT_LIST_HEAD(&newrec->ls_req_list);
1396 INIT_LIST_HEAD(&newrec->ls_busylist);
1397 INIT_LIST_HEAD(&newrec->assoc_list);
1398 INIT_LIST_HEAD(&newrec->host_list);
1399 kref_init(&newrec->ref);
1400 ida_init(&newrec->assoc_cnt);
1401 newrec->max_sg_cnt = template->max_sgl_segments;
1403 ret = nvmet_fc_alloc_ls_iodlist(newrec);
1406 goto out_free_newrec;
1409 nvmet_fc_portentry_rebind_tgt(newrec);
1411 spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1412 list_add_tail(&newrec->tgt_list, &nvmet_fc_target_list);
1413 spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1415 *portptr = &newrec->fc_target_port;
1421 ida_simple_remove(&nvmet_fc_tgtport_cnt, idx);
1428 EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
1432 nvmet_fc_free_tgtport(struct kref *ref)
1434 struct nvmet_fc_tgtport *tgtport =
1435 container_of(ref, struct nvmet_fc_tgtport, ref);
1436 struct device *dev = tgtport->dev;
1437 unsigned long flags;
1439 spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1440 list_del(&tgtport->tgt_list);
1441 spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1443 nvmet_fc_free_ls_iodlist(tgtport);
1445 /* let the LLDD know we've finished tearing it down */
1446 tgtport->ops->targetport_delete(&tgtport->fc_target_port);
1448 ida_simple_remove(&nvmet_fc_tgtport_cnt,
1449 tgtport->fc_target_port.port_num);
1451 ida_destroy(&tgtport->assoc_cnt);
1459 nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport)
1461 kref_put(&tgtport->ref, nvmet_fc_free_tgtport);
1465 nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport)
1467 return kref_get_unless_zero(&tgtport->ref);
1471 __nvmet_fc_free_assocs(struct nvmet_fc_tgtport *tgtport)
1473 struct nvmet_fc_tgt_assoc *assoc;
1476 list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
1477 if (!nvmet_fc_tgt_a_get(assoc))
1479 if (!schedule_work(&assoc->del_work))
1480 /* already deleting - release local reference */
1481 nvmet_fc_tgt_a_put(assoc);
1487 * nvmet_fc_invalidate_host - transport entry point called by an LLDD
1488 * to remove references to a hosthandle for LS's.
1490 * The nvmet-fc layer ensures that any references to the hosthandle
1491 * on the targetport are forgotten (set to NULL). The LLDD will
1492 * typically call this when a login with a remote host port has been
1493 * lost, thus LS's for the remote host port are no longer possible.
1495 * If an LS request is outstanding to the targetport/hosthandle (or
1496 * issued concurrently with the call to invalidate the host), the
1497 * LLDD is responsible for terminating/aborting the LS and completing
1498 * the LS request. It is recommended that these terminations/aborts
1499 * occur after calling to invalidate the host handle to avoid additional
1500 * retries by the nvmet-fc transport. The nvmet-fc transport may
1501 * continue to reference host handle while it cleans up outstanding
1502 * NVME associations. The nvmet-fc transport will call the
1503 * ops->host_release() callback to notify the LLDD that all references
1504 * are complete and the related host handle can be recovered.
1505 * Note: if there are no references, the callback may be called before
1506 * the invalidate host call returns.
1508 * @target_port: pointer to the (registered) target port that a prior
1509 * LS was received on and which supplied the transport the
1511 * @hosthandle: the handle (pointer) that represents the host port
1512 * that no longer has connectivity and that LS's should
1513 * no longer be directed to.
1516 nvmet_fc_invalidate_host(struct nvmet_fc_target_port *target_port,
1519 struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
1520 struct nvmet_fc_tgt_assoc *assoc, *next;
1521 unsigned long flags;
1522 bool noassoc = true;
1524 spin_lock_irqsave(&tgtport->lock, flags);
1525 list_for_each_entry_safe(assoc, next,
1526 &tgtport->assoc_list, a_list) {
1527 if (!assoc->hostport ||
1528 assoc->hostport->hosthandle != hosthandle)
1530 if (!nvmet_fc_tgt_a_get(assoc))
1532 assoc->hostport->invalid = 1;
1534 if (!schedule_work(&assoc->del_work))
1535 /* already deleting - release local reference */
1536 nvmet_fc_tgt_a_put(assoc);
1538 spin_unlock_irqrestore(&tgtport->lock, flags);
1540 /* if there's nothing to wait for - call the callback */
1541 if (noassoc && tgtport->ops->host_release)
1542 tgtport->ops->host_release(hosthandle);
1544 EXPORT_SYMBOL_GPL(nvmet_fc_invalidate_host);
1547 * nvmet layer has called to terminate an association
1550 nvmet_fc_delete_ctrl(struct nvmet_ctrl *ctrl)
1552 struct nvmet_fc_tgtport *tgtport, *next;
1553 struct nvmet_fc_tgt_assoc *assoc;
1554 struct nvmet_fc_tgt_queue *queue;
1555 unsigned long flags;
1556 bool found_ctrl = false;
1558 /* this is a bit ugly, but don't want to make locks layered */
1559 spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1560 list_for_each_entry_safe(tgtport, next, &nvmet_fc_target_list,
1562 if (!nvmet_fc_tgtport_get(tgtport))
1564 spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1567 list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
1568 queue = rcu_dereference(assoc->queues[0]);
1569 if (queue && queue->nvme_sq.ctrl == ctrl) {
1570 if (nvmet_fc_tgt_a_get(assoc))
1577 nvmet_fc_tgtport_put(tgtport);
1580 if (!schedule_work(&assoc->del_work))
1581 /* already deleting - release local reference */
1582 nvmet_fc_tgt_a_put(assoc);
1586 spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1588 spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1592 * nvme_fc_unregister_targetport - transport entry point called by an
1593 * LLDD to deregister/remove a previously
1594 * registered a local NVME subsystem FC port.
1595 * @target_port: pointer to the (registered) target port that is to be
1599 * a completion status. Must be 0 upon success; a negative errno
1600 * (ex: -ENXIO) upon failure.
1603 nvmet_fc_unregister_targetport(struct nvmet_fc_target_port *target_port)
1605 struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
1607 nvmet_fc_portentry_unbind_tgt(tgtport);
1609 /* terminate any outstanding associations */
1610 __nvmet_fc_free_assocs(tgtport);
1613 * should terminate LS's as well. However, LS's will be generated
1614 * at the tail end of association termination, so they likely don't
1615 * exist yet. And even if they did, it's worthwhile to just let
1616 * them finish and targetport ref counting will clean things up.
1619 nvmet_fc_tgtport_put(tgtport);
1623 EXPORT_SYMBOL_GPL(nvmet_fc_unregister_targetport);
1626 /* ********************** FC-NVME LS RCV Handling ************************* */
1630 nvmet_fc_ls_create_association(struct nvmet_fc_tgtport *tgtport,
1631 struct nvmet_fc_ls_iod *iod)
1633 struct fcnvme_ls_cr_assoc_rqst *rqst = &iod->rqstbuf->rq_cr_assoc;
1634 struct fcnvme_ls_cr_assoc_acc *acc = &iod->rspbuf->rsp_cr_assoc;
1635 struct nvmet_fc_tgt_queue *queue;
1638 memset(acc, 0, sizeof(*acc));
1641 * FC-NVME spec changes. There are initiators sending different
1642 * lengths as padding sizes for Create Association Cmd descriptor
1644 * Accept anything of "minimum" length. Assume format per 1.15
1645 * spec (with HOSTID reduced to 16 bytes), ignore how long the
1646 * trailing pad length is.
1648 if (iod->rqstdatalen < FCNVME_LSDESC_CRA_RQST_MINLEN)
1649 ret = VERR_CR_ASSOC_LEN;
1650 else if (be32_to_cpu(rqst->desc_list_len) <
1651 FCNVME_LSDESC_CRA_RQST_MIN_LISTLEN)
1652 ret = VERR_CR_ASSOC_RQST_LEN;
1653 else if (rqst->assoc_cmd.desc_tag !=
1654 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD))
1655 ret = VERR_CR_ASSOC_CMD;
1656 else if (be32_to_cpu(rqst->assoc_cmd.desc_len) <
1657 FCNVME_LSDESC_CRA_CMD_DESC_MIN_DESCLEN)
1658 ret = VERR_CR_ASSOC_CMD_LEN;
1659 else if (!rqst->assoc_cmd.ersp_ratio ||
1660 (be16_to_cpu(rqst->assoc_cmd.ersp_ratio) >=
1661 be16_to_cpu(rqst->assoc_cmd.sqsize)))
1662 ret = VERR_ERSP_RATIO;
1665 /* new association w/ admin queue */
1666 iod->assoc = nvmet_fc_alloc_target_assoc(
1667 tgtport, iod->hosthandle);
1669 ret = VERR_ASSOC_ALLOC_FAIL;
1671 queue = nvmet_fc_alloc_target_queue(iod->assoc, 0,
1672 be16_to_cpu(rqst->assoc_cmd.sqsize));
1674 ret = VERR_QUEUE_ALLOC_FAIL;
1679 dev_err(tgtport->dev,
1680 "Create Association LS failed: %s\n",
1681 validation_errors[ret]);
1682 iod->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1683 sizeof(*acc), rqst->w0.ls_cmd,
1684 FCNVME_RJT_RC_LOGIC,
1685 FCNVME_RJT_EXP_NONE, 0);
1689 queue->ersp_ratio = be16_to_cpu(rqst->assoc_cmd.ersp_ratio);
1690 atomic_set(&queue->connected, 1);
1691 queue->sqhd = 0; /* best place to init value */
1693 dev_info(tgtport->dev,
1694 "{%d:%d} Association created\n",
1695 tgtport->fc_target_port.port_num, iod->assoc->a_id);
1697 /* format a response */
1699 iod->lsrsp->rsplen = sizeof(*acc);
1701 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1703 sizeof(struct fcnvme_ls_cr_assoc_acc)),
1704 FCNVME_LS_CREATE_ASSOCIATION);
1705 acc->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1706 acc->associd.desc_len =
1708 sizeof(struct fcnvme_lsdesc_assoc_id));
1709 acc->associd.association_id =
1710 cpu_to_be64(nvmet_fc_makeconnid(iod->assoc, 0));
1711 acc->connectid.desc_tag = cpu_to_be32(FCNVME_LSDESC_CONN_ID);
1712 acc->connectid.desc_len =
1714 sizeof(struct fcnvme_lsdesc_conn_id));
1715 acc->connectid.connection_id = acc->associd.association_id;
1719 nvmet_fc_ls_create_connection(struct nvmet_fc_tgtport *tgtport,
1720 struct nvmet_fc_ls_iod *iod)
1722 struct fcnvme_ls_cr_conn_rqst *rqst = &iod->rqstbuf->rq_cr_conn;
1723 struct fcnvme_ls_cr_conn_acc *acc = &iod->rspbuf->rsp_cr_conn;
1724 struct nvmet_fc_tgt_queue *queue;
1727 memset(acc, 0, sizeof(*acc));
1729 if (iod->rqstdatalen < sizeof(struct fcnvme_ls_cr_conn_rqst))
1730 ret = VERR_CR_CONN_LEN;
1731 else if (rqst->desc_list_len !=
1733 sizeof(struct fcnvme_ls_cr_conn_rqst)))
1734 ret = VERR_CR_CONN_RQST_LEN;
1735 else if (rqst->associd.desc_tag != cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1736 ret = VERR_ASSOC_ID;
1737 else if (rqst->associd.desc_len !=
1739 sizeof(struct fcnvme_lsdesc_assoc_id)))
1740 ret = VERR_ASSOC_ID_LEN;
1741 else if (rqst->connect_cmd.desc_tag !=
1742 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD))
1743 ret = VERR_CR_CONN_CMD;
1744 else if (rqst->connect_cmd.desc_len !=
1746 sizeof(struct fcnvme_lsdesc_cr_conn_cmd)))
1747 ret = VERR_CR_CONN_CMD_LEN;
1748 else if (!rqst->connect_cmd.ersp_ratio ||
1749 (be16_to_cpu(rqst->connect_cmd.ersp_ratio) >=
1750 be16_to_cpu(rqst->connect_cmd.sqsize)))
1751 ret = VERR_ERSP_RATIO;
1755 iod->assoc = nvmet_fc_find_target_assoc(tgtport,
1756 be64_to_cpu(rqst->associd.association_id));
1758 ret = VERR_NO_ASSOC;
1760 queue = nvmet_fc_alloc_target_queue(iod->assoc,
1761 be16_to_cpu(rqst->connect_cmd.qid),
1762 be16_to_cpu(rqst->connect_cmd.sqsize));
1764 ret = VERR_QUEUE_ALLOC_FAIL;
1766 /* release get taken in nvmet_fc_find_target_assoc */
1767 nvmet_fc_tgt_a_put(iod->assoc);
1772 dev_err(tgtport->dev,
1773 "Create Connection LS failed: %s\n",
1774 validation_errors[ret]);
1775 iod->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1776 sizeof(*acc), rqst->w0.ls_cmd,
1777 (ret == VERR_NO_ASSOC) ?
1778 FCNVME_RJT_RC_INV_ASSOC :
1779 FCNVME_RJT_RC_LOGIC,
1780 FCNVME_RJT_EXP_NONE, 0);
1784 queue->ersp_ratio = be16_to_cpu(rqst->connect_cmd.ersp_ratio);
1785 atomic_set(&queue->connected, 1);
1786 queue->sqhd = 0; /* best place to init value */
1788 /* format a response */
1790 iod->lsrsp->rsplen = sizeof(*acc);
1792 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1793 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)),
1794 FCNVME_LS_CREATE_CONNECTION);
1795 acc->connectid.desc_tag = cpu_to_be32(FCNVME_LSDESC_CONN_ID);
1796 acc->connectid.desc_len =
1798 sizeof(struct fcnvme_lsdesc_conn_id));
1799 acc->connectid.connection_id =
1800 cpu_to_be64(nvmet_fc_makeconnid(iod->assoc,
1801 be16_to_cpu(rqst->connect_cmd.qid)));
1805 * Returns true if the LS response is to be transmit
1806 * Returns false if the LS response is to be delayed
1809 nvmet_fc_ls_disconnect(struct nvmet_fc_tgtport *tgtport,
1810 struct nvmet_fc_ls_iod *iod)
1812 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1813 &iod->rqstbuf->rq_dis_assoc;
1814 struct fcnvme_ls_disconnect_assoc_acc *acc =
1815 &iod->rspbuf->rsp_dis_assoc;
1816 struct nvmet_fc_tgt_assoc *assoc = NULL;
1817 struct nvmet_fc_ls_iod *oldls = NULL;
1818 unsigned long flags;
1821 memset(acc, 0, sizeof(*acc));
1823 ret = nvmefc_vldt_lsreq_discon_assoc(iod->rqstdatalen, rqst);
1825 /* match an active association - takes an assoc ref if !NULL */
1826 assoc = nvmet_fc_find_target_assoc(tgtport,
1827 be64_to_cpu(rqst->associd.association_id));
1830 ret = VERR_NO_ASSOC;
1833 if (ret || !assoc) {
1834 dev_err(tgtport->dev,
1835 "Disconnect LS failed: %s\n",
1836 validation_errors[ret]);
1837 iod->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1838 sizeof(*acc), rqst->w0.ls_cmd,
1839 (ret == VERR_NO_ASSOC) ?
1840 FCNVME_RJT_RC_INV_ASSOC :
1841 FCNVME_RJT_RC_LOGIC,
1842 FCNVME_RJT_EXP_NONE, 0);
1846 /* format a response */
1848 iod->lsrsp->rsplen = sizeof(*acc);
1850 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1852 sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
1853 FCNVME_LS_DISCONNECT_ASSOC);
1855 /* release get taken in nvmet_fc_find_target_assoc */
1856 nvmet_fc_tgt_a_put(assoc);
1859 * The rules for LS response says the response cannot
1860 * go back until ABTS's have been sent for all outstanding
1861 * I/O and a Disconnect Association LS has been sent.
1862 * So... save off the Disconnect LS to send the response
1863 * later. If there was a prior LS already saved, replace
1864 * it with the newer one and send a can't perform reject
1867 spin_lock_irqsave(&tgtport->lock, flags);
1868 oldls = assoc->rcv_disconn;
1869 assoc->rcv_disconn = iod;
1870 spin_unlock_irqrestore(&tgtport->lock, flags);
1872 nvmet_fc_delete_target_assoc(assoc);
1875 dev_info(tgtport->dev,
1876 "{%d:%d} Multiple Disconnect Association LS's "
1878 tgtport->fc_target_port.port_num, assoc->a_id);
1879 /* overwrite good response with bogus failure */
1880 oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
1881 sizeof(*iod->rspbuf),
1882 /* ok to use rqst, LS is same */
1885 FCNVME_RJT_EXP_NONE, 0);
1886 nvmet_fc_xmt_ls_rsp(tgtport, oldls);
1893 /* *********************** NVME Ctrl Routines **************************** */
1896 static void nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req);
1898 static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops;
1901 nvmet_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
1903 struct nvmet_fc_ls_iod *iod = lsrsp->nvme_fc_private;
1904 struct nvmet_fc_tgtport *tgtport = iod->tgtport;
1906 fc_dma_sync_single_for_cpu(tgtport->dev, iod->rspdma,
1907 sizeof(*iod->rspbuf), DMA_TO_DEVICE);
1908 nvmet_fc_free_ls_iod(tgtport, iod);
1909 nvmet_fc_tgtport_put(tgtport);
1913 nvmet_fc_xmt_ls_rsp(struct nvmet_fc_tgtport *tgtport,
1914 struct nvmet_fc_ls_iod *iod)
1918 fc_dma_sync_single_for_device(tgtport->dev, iod->rspdma,
1919 sizeof(*iod->rspbuf), DMA_TO_DEVICE);
1921 ret = tgtport->ops->xmt_ls_rsp(&tgtport->fc_target_port, iod->lsrsp);
1923 nvmet_fc_xmt_ls_rsp_done(iod->lsrsp);
1927 * Actual processing routine for received FC-NVME LS Requests from the LLD
1930 nvmet_fc_handle_ls_rqst(struct nvmet_fc_tgtport *tgtport,
1931 struct nvmet_fc_ls_iod *iod)
1933 struct fcnvme_ls_rqst_w0 *w0 = &iod->rqstbuf->rq_cr_assoc.w0;
1934 bool sendrsp = true;
1936 iod->lsrsp->nvme_fc_private = iod;
1937 iod->lsrsp->rspbuf = iod->rspbuf;
1938 iod->lsrsp->rspdma = iod->rspdma;
1939 iod->lsrsp->done = nvmet_fc_xmt_ls_rsp_done;
1940 /* Be preventative. handlers will later set to valid length */
1941 iod->lsrsp->rsplen = 0;
1947 * parse request input, execute the request, and format the
1950 switch (w0->ls_cmd) {
1951 case FCNVME_LS_CREATE_ASSOCIATION:
1952 /* Creates Association and initial Admin Queue/Connection */
1953 nvmet_fc_ls_create_association(tgtport, iod);
1955 case FCNVME_LS_CREATE_CONNECTION:
1956 /* Creates an IO Queue/Connection */
1957 nvmet_fc_ls_create_connection(tgtport, iod);
1959 case FCNVME_LS_DISCONNECT_ASSOC:
1960 /* Terminate a Queue/Connection or the Association */
1961 sendrsp = nvmet_fc_ls_disconnect(tgtport, iod);
1964 iod->lsrsp->rsplen = nvme_fc_format_rjt(iod->rspbuf,
1965 sizeof(*iod->rspbuf), w0->ls_cmd,
1966 FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
1970 nvmet_fc_xmt_ls_rsp(tgtport, iod);
1974 * Actual processing routine for received FC-NVME LS Requests from the LLD
1977 nvmet_fc_handle_ls_rqst_work(struct work_struct *work)
1979 struct nvmet_fc_ls_iod *iod =
1980 container_of(work, struct nvmet_fc_ls_iod, work);
1981 struct nvmet_fc_tgtport *tgtport = iod->tgtport;
1983 nvmet_fc_handle_ls_rqst(tgtport, iod);
1988 * nvmet_fc_rcv_ls_req - transport entry point called by an LLDD
1989 * upon the reception of a NVME LS request.
1991 * The nvmet-fc layer will copy payload to an internal structure for
1992 * processing. As such, upon completion of the routine, the LLDD may
1993 * immediately free/reuse the LS request buffer passed in the call.
1995 * If this routine returns error, the LLDD should abort the exchange.
1997 * @target_port: pointer to the (registered) target port the LS was
1999 * @lsrsp: pointer to a lsrsp structure to be used to reference
2000 * the exchange corresponding to the LS.
2001 * @lsreqbuf: pointer to the buffer containing the LS Request
2002 * @lsreqbuf_len: length, in bytes, of the received LS request
2005 nvmet_fc_rcv_ls_req(struct nvmet_fc_target_port *target_port,
2007 struct nvmefc_ls_rsp *lsrsp,
2008 void *lsreqbuf, u32 lsreqbuf_len)
2010 struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
2011 struct nvmet_fc_ls_iod *iod;
2012 struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
2014 if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
2015 dev_info(tgtport->dev,
2016 "RCV %s LS failed: payload too large (%d)\n",
2017 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
2018 nvmefc_ls_names[w0->ls_cmd] : "",
2023 if (!nvmet_fc_tgtport_get(tgtport)) {
2024 dev_info(tgtport->dev,
2025 "RCV %s LS failed: target deleting\n",
2026 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
2027 nvmefc_ls_names[w0->ls_cmd] : "");
2031 iod = nvmet_fc_alloc_ls_iod(tgtport);
2033 dev_info(tgtport->dev,
2034 "RCV %s LS failed: context allocation failed\n",
2035 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
2036 nvmefc_ls_names[w0->ls_cmd] : "");
2037 nvmet_fc_tgtport_put(tgtport);
2043 memcpy(iod->rqstbuf, lsreqbuf, lsreqbuf_len);
2044 iod->rqstdatalen = lsreqbuf_len;
2045 iod->hosthandle = hosthandle;
2047 schedule_work(&iod->work);
2051 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_ls_req);
2055 * **********************
2056 * Start of FCP handling
2057 * **********************
2061 nvmet_fc_alloc_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
2063 struct scatterlist *sg;
2066 sg = sgl_alloc(fod->req.transfer_len, GFP_KERNEL, &nent);
2071 fod->data_sg_cnt = nent;
2072 fod->data_sg_cnt = fc_dma_map_sg(fod->tgtport->dev, sg, nent,
2073 ((fod->io_dir == NVMET_FCP_WRITE) ?
2074 DMA_FROM_DEVICE : DMA_TO_DEVICE));
2075 /* note: write from initiator perspective */
2076 fod->next_sg = fod->data_sg;
2081 return NVME_SC_INTERNAL;
2085 nvmet_fc_free_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
2087 if (!fod->data_sg || !fod->data_sg_cnt)
2090 fc_dma_unmap_sg(fod->tgtport->dev, fod->data_sg, fod->data_sg_cnt,
2091 ((fod->io_dir == NVMET_FCP_WRITE) ?
2092 DMA_FROM_DEVICE : DMA_TO_DEVICE));
2093 sgl_free(fod->data_sg);
2094 fod->data_sg = NULL;
2095 fod->data_sg_cnt = 0;
2100 queue_90percent_full(struct nvmet_fc_tgt_queue *q, u32 sqhd)
2104 /* egad, this is ugly. And sqtail is just a best guess */
2105 sqtail = atomic_read(&q->sqtail) % q->sqsize;
2107 used = (sqtail < sqhd) ? (sqtail + q->sqsize - sqhd) : (sqtail - sqhd);
2108 return ((used * 10) >= (((u32)(q->sqsize - 1) * 9)));
2113 * May be a NVMET_FCOP_RSP or NVMET_FCOP_READDATA_RSP op
2116 nvmet_fc_prep_fcp_rsp(struct nvmet_fc_tgtport *tgtport,
2117 struct nvmet_fc_fcp_iod *fod)
2119 struct nvme_fc_ersp_iu *ersp = &fod->rspiubuf;
2120 struct nvme_common_command *sqe = &fod->cmdiubuf.sqe.common;
2121 struct nvme_completion *cqe = &ersp->cqe;
2122 u32 *cqewd = (u32 *)cqe;
2123 bool send_ersp = false;
2124 u32 rsn, rspcnt, xfr_length;
2126 if (fod->fcpreq->op == NVMET_FCOP_READDATA_RSP)
2127 xfr_length = fod->req.transfer_len;
2129 xfr_length = fod->offset;
2132 * check to see if we can send a 0's rsp.
2133 * Note: to send a 0's response, the NVME-FC host transport will
2134 * recreate the CQE. The host transport knows: sq id, SQHD (last
2135 * seen in an ersp), and command_id. Thus it will create a
2136 * zero-filled CQE with those known fields filled in. Transport
2137 * must send an ersp for any condition where the cqe won't match
2140 * Here are the FC-NVME mandated cases where we must send an ersp:
2141 * every N responses, where N=ersp_ratio
2142 * force fabric commands to send ersp's (not in FC-NVME but good
2144 * normal cmds: any time status is non-zero, or status is zero
2145 * but words 0 or 1 are non-zero.
2146 * the SQ is 90% or more full
2147 * the cmd is a fused command
2148 * transferred data length not equal to cmd iu length
2150 rspcnt = atomic_inc_return(&fod->queue->zrspcnt);
2151 if (!(rspcnt % fod->queue->ersp_ratio) ||
2152 nvme_is_fabrics((struct nvme_command *) sqe) ||
2153 xfr_length != fod->req.transfer_len ||
2154 (le16_to_cpu(cqe->status) & 0xFFFE) || cqewd[0] || cqewd[1] ||
2155 (sqe->flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND)) ||
2156 queue_90percent_full(fod->queue, le16_to_cpu(cqe->sq_head)))
2159 /* re-set the fields */
2160 fod->fcpreq->rspaddr = ersp;
2161 fod->fcpreq->rspdma = fod->rspdma;
2164 memset(ersp, 0, NVME_FC_SIZEOF_ZEROS_RSP);
2165 fod->fcpreq->rsplen = NVME_FC_SIZEOF_ZEROS_RSP;
2167 ersp->iu_len = cpu_to_be16(sizeof(*ersp)/sizeof(u32));
2168 rsn = atomic_inc_return(&fod->queue->rsn);
2169 ersp->rsn = cpu_to_be32(rsn);
2170 ersp->xfrd_len = cpu_to_be32(xfr_length);
2171 fod->fcpreq->rsplen = sizeof(*ersp);
2174 fc_dma_sync_single_for_device(tgtport->dev, fod->rspdma,
2175 sizeof(fod->rspiubuf), DMA_TO_DEVICE);
2178 static void nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req *fcpreq);
2181 nvmet_fc_abort_op(struct nvmet_fc_tgtport *tgtport,
2182 struct nvmet_fc_fcp_iod *fod)
2184 struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
2186 /* data no longer needed */
2187 nvmet_fc_free_tgt_pgs(fod);
2190 * if an ABTS was received or we issued the fcp_abort early
2191 * don't call abort routine again.
2193 /* no need to take lock - lock was taken earlier to get here */
2195 tgtport->ops->fcp_abort(&tgtport->fc_target_port, fcpreq);
2197 nvmet_fc_free_fcp_iod(fod->queue, fod);
2201 nvmet_fc_xmt_fcp_rsp(struct nvmet_fc_tgtport *tgtport,
2202 struct nvmet_fc_fcp_iod *fod)
2206 fod->fcpreq->op = NVMET_FCOP_RSP;
2207 fod->fcpreq->timeout = 0;
2209 nvmet_fc_prep_fcp_rsp(tgtport, fod);
2211 ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fod->fcpreq);
2213 nvmet_fc_abort_op(tgtport, fod);
2217 nvmet_fc_transfer_fcp_data(struct nvmet_fc_tgtport *tgtport,
2218 struct nvmet_fc_fcp_iod *fod, u8 op)
2220 struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
2221 struct scatterlist *sg = fod->next_sg;
2222 unsigned long flags;
2223 u32 remaininglen = fod->req.transfer_len - fod->offset;
2228 fcpreq->offset = fod->offset;
2229 fcpreq->timeout = NVME_FC_TGTOP_TIMEOUT_SEC;
2232 * for next sequence:
2233 * break at a sg element boundary
2234 * attempt to keep sequence length capped at
2235 * NVMET_FC_MAX_SEQ_LENGTH but allow sequence to
2236 * be longer if a single sg element is larger
2237 * than that amount. This is done to avoid creating
2238 * a new sg list to use for the tgtport api.
2242 while (tlen < remaininglen &&
2243 fcpreq->sg_cnt < tgtport->max_sg_cnt &&
2244 tlen + sg_dma_len(sg) < NVMET_FC_MAX_SEQ_LENGTH) {
2246 tlen += sg_dma_len(sg);
2249 if (tlen < remaininglen && fcpreq->sg_cnt == 0) {
2251 tlen += min_t(u32, sg_dma_len(sg), remaininglen);
2254 if (tlen < remaininglen)
2257 fod->next_sg = NULL;
2259 fcpreq->transfer_length = tlen;
2260 fcpreq->transferred_length = 0;
2261 fcpreq->fcp_error = 0;
2265 * If the last READDATA request: check if LLDD supports
2266 * combined xfr with response.
2268 if ((op == NVMET_FCOP_READDATA) &&
2269 ((fod->offset + fcpreq->transfer_length) == fod->req.transfer_len) &&
2270 (tgtport->ops->target_features & NVMET_FCTGTFEAT_READDATA_RSP)) {
2271 fcpreq->op = NVMET_FCOP_READDATA_RSP;
2272 nvmet_fc_prep_fcp_rsp(tgtport, fod);
2275 ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fod->fcpreq);
2278 * should be ok to set w/o lock as its in the thread of
2279 * execution (not an async timer routine) and doesn't
2280 * contend with any clearing action
2284 if (op == NVMET_FCOP_WRITEDATA) {
2285 spin_lock_irqsave(&fod->flock, flags);
2286 fod->writedataactive = false;
2287 spin_unlock_irqrestore(&fod->flock, flags);
2288 nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
2289 } else /* NVMET_FCOP_READDATA or NVMET_FCOP_READDATA_RSP */ {
2290 fcpreq->fcp_error = ret;
2291 fcpreq->transferred_length = 0;
2292 nvmet_fc_xmt_fcp_op_done(fod->fcpreq);
2298 __nvmet_fc_fod_op_abort(struct nvmet_fc_fcp_iod *fod, bool abort)
2300 struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
2301 struct nvmet_fc_tgtport *tgtport = fod->tgtport;
2303 /* if in the middle of an io and we need to tear down */
2305 if (fcpreq->op == NVMET_FCOP_WRITEDATA) {
2306 nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
2310 nvmet_fc_abort_op(tgtport, fod);
2318 * actual done handler for FCP operations when completed by the lldd
2321 nvmet_fc_fod_op_done(struct nvmet_fc_fcp_iod *fod)
2323 struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
2324 struct nvmet_fc_tgtport *tgtport = fod->tgtport;
2325 unsigned long flags;
2328 spin_lock_irqsave(&fod->flock, flags);
2330 fod->writedataactive = false;
2331 spin_unlock_irqrestore(&fod->flock, flags);
2333 switch (fcpreq->op) {
2335 case NVMET_FCOP_WRITEDATA:
2336 if (__nvmet_fc_fod_op_abort(fod, abort))
2338 if (fcpreq->fcp_error ||
2339 fcpreq->transferred_length != fcpreq->transfer_length) {
2340 spin_lock_irqsave(&fod->flock, flags);
2342 spin_unlock_irqrestore(&fod->flock, flags);
2344 nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
2348 fod->offset += fcpreq->transferred_length;
2349 if (fod->offset != fod->req.transfer_len) {
2350 spin_lock_irqsave(&fod->flock, flags);
2351 fod->writedataactive = true;
2352 spin_unlock_irqrestore(&fod->flock, flags);
2354 /* transfer the next chunk */
2355 nvmet_fc_transfer_fcp_data(tgtport, fod,
2356 NVMET_FCOP_WRITEDATA);
2360 /* data transfer complete, resume with nvmet layer */
2361 fod->req.execute(&fod->req);
2364 case NVMET_FCOP_READDATA:
2365 case NVMET_FCOP_READDATA_RSP:
2366 if (__nvmet_fc_fod_op_abort(fod, abort))
2368 if (fcpreq->fcp_error ||
2369 fcpreq->transferred_length != fcpreq->transfer_length) {
2370 nvmet_fc_abort_op(tgtport, fod);
2376 if (fcpreq->op == NVMET_FCOP_READDATA_RSP) {
2377 /* data no longer needed */
2378 nvmet_fc_free_tgt_pgs(fod);
2379 nvmet_fc_free_fcp_iod(fod->queue, fod);
2383 fod->offset += fcpreq->transferred_length;
2384 if (fod->offset != fod->req.transfer_len) {
2385 /* transfer the next chunk */
2386 nvmet_fc_transfer_fcp_data(tgtport, fod,
2387 NVMET_FCOP_READDATA);
2391 /* data transfer complete, send response */
2393 /* data no longer needed */
2394 nvmet_fc_free_tgt_pgs(fod);
2396 nvmet_fc_xmt_fcp_rsp(tgtport, fod);
2400 case NVMET_FCOP_RSP:
2401 if (__nvmet_fc_fod_op_abort(fod, abort))
2403 nvmet_fc_free_fcp_iod(fod->queue, fod);
2412 nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req *fcpreq)
2414 struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
2416 nvmet_fc_fod_op_done(fod);
2420 * actual completion handler after execution by the nvmet layer
2423 __nvmet_fc_fcp_nvme_cmd_done(struct nvmet_fc_tgtport *tgtport,
2424 struct nvmet_fc_fcp_iod *fod, int status)
2426 struct nvme_common_command *sqe = &fod->cmdiubuf.sqe.common;
2427 struct nvme_completion *cqe = &fod->rspiubuf.cqe;
2428 unsigned long flags;
2431 spin_lock_irqsave(&fod->flock, flags);
2433 spin_unlock_irqrestore(&fod->flock, flags);
2435 /* if we have a CQE, snoop the last sq_head value */
2437 fod->queue->sqhd = cqe->sq_head;
2440 nvmet_fc_abort_op(tgtport, fod);
2444 /* if an error handling the cmd post initial parsing */
2446 /* fudge up a failed CQE status for our transport error */
2447 memset(cqe, 0, sizeof(*cqe));
2448 cqe->sq_head = fod->queue->sqhd; /* echo last cqe sqhd */
2449 cqe->sq_id = cpu_to_le16(fod->queue->qid);
2450 cqe->command_id = sqe->command_id;
2451 cqe->status = cpu_to_le16(status);
2455 * try to push the data even if the SQE status is non-zero.
2456 * There may be a status where data still was intended to
2459 if ((fod->io_dir == NVMET_FCP_READ) && (fod->data_sg_cnt)) {
2460 /* push the data over before sending rsp */
2461 nvmet_fc_transfer_fcp_data(tgtport, fod,
2462 NVMET_FCOP_READDATA);
2466 /* writes & no data - fall thru */
2469 /* data no longer needed */
2470 nvmet_fc_free_tgt_pgs(fod);
2472 nvmet_fc_xmt_fcp_rsp(tgtport, fod);
2477 nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req)
2479 struct nvmet_fc_fcp_iod *fod = nvmet_req_to_fod(nvme_req);
2480 struct nvmet_fc_tgtport *tgtport = fod->tgtport;
2482 __nvmet_fc_fcp_nvme_cmd_done(tgtport, fod, 0);
2487 * Actual processing routine for received FC-NVME I/O Requests from the LLD
2490 nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
2491 struct nvmet_fc_fcp_iod *fod)
2493 struct nvme_fc_cmd_iu *cmdiu = &fod->cmdiubuf;
2494 u32 xfrlen = be32_to_cpu(cmdiu->data_len);
2498 * if there is no nvmet mapping to the targetport there
2499 * shouldn't be requests. just terminate them.
2502 goto transport_error;
2505 * Fused commands are currently not supported in the linux
2508 * As such, the implementation of the FC transport does not
2509 * look at the fused commands and order delivery to the upper
2510 * layer until we have both based on csn.
2513 fod->fcpreq->done = nvmet_fc_xmt_fcp_op_done;
2515 if (cmdiu->flags & FCNVME_CMD_FLAGS_WRITE) {
2516 fod->io_dir = NVMET_FCP_WRITE;
2517 if (!nvme_is_write(&cmdiu->sqe))
2518 goto transport_error;
2519 } else if (cmdiu->flags & FCNVME_CMD_FLAGS_READ) {
2520 fod->io_dir = NVMET_FCP_READ;
2521 if (nvme_is_write(&cmdiu->sqe))
2522 goto transport_error;
2524 fod->io_dir = NVMET_FCP_NODATA;
2526 goto transport_error;
2529 fod->req.cmd = &fod->cmdiubuf.sqe;
2530 fod->req.cqe = &fod->rspiubuf.cqe;
2531 fod->req.port = tgtport->pe->port;
2533 /* clear any response payload */
2534 memset(&fod->rspiubuf, 0, sizeof(fod->rspiubuf));
2536 fod->data_sg = NULL;
2537 fod->data_sg_cnt = 0;
2539 ret = nvmet_req_init(&fod->req,
2540 &fod->queue->nvme_cq,
2541 &fod->queue->nvme_sq,
2542 &nvmet_fc_tgt_fcp_ops);
2544 /* bad SQE content or invalid ctrl state */
2545 /* nvmet layer has already called op done to send rsp. */
2549 fod->req.transfer_len = xfrlen;
2551 /* keep a running counter of tail position */
2552 atomic_inc(&fod->queue->sqtail);
2554 if (fod->req.transfer_len) {
2555 ret = nvmet_fc_alloc_tgt_pgs(fod);
2557 nvmet_req_complete(&fod->req, ret);
2561 fod->req.sg = fod->data_sg;
2562 fod->req.sg_cnt = fod->data_sg_cnt;
2565 if (fod->io_dir == NVMET_FCP_WRITE) {
2566 /* pull the data over before invoking nvmet layer */
2567 nvmet_fc_transfer_fcp_data(tgtport, fod, NVMET_FCOP_WRITEDATA);
2574 * can invoke the nvmet_layer now. If read data, cmd completion will
2577 fod->req.execute(&fod->req);
2581 nvmet_fc_abort_op(tgtport, fod);
2585 * nvmet_fc_rcv_fcp_req - transport entry point called by an LLDD
2586 * upon the reception of a NVME FCP CMD IU.
2588 * Pass a FC-NVME FCP CMD IU received from the FC link to the nvmet-fc
2589 * layer for processing.
2591 * The nvmet_fc layer allocates a local job structure (struct
2592 * nvmet_fc_fcp_iod) from the queue for the io and copies the
2593 * CMD IU buffer to the job structure. As such, on a successful
2594 * completion (returns 0), the LLDD may immediately free/reuse
2595 * the CMD IU buffer passed in the call.
2597 * However, in some circumstances, due to the packetized nature of FC
2598 * and the api of the FC LLDD which may issue a hw command to send the
2599 * response, but the LLDD may not get the hw completion for that command
2600 * and upcall the nvmet_fc layer before a new command may be
2601 * asynchronously received - its possible for a command to be received
2602 * before the LLDD and nvmet_fc have recycled the job structure. It gives
2603 * the appearance of more commands received than fits in the sq.
2604 * To alleviate this scenario, a temporary queue is maintained in the
2605 * transport for pending LLDD requests waiting for a queue job structure.
2606 * In these "overrun" cases, a temporary queue element is allocated
2607 * the LLDD request and CMD iu buffer information remembered, and the
2608 * routine returns a -EOVERFLOW status. Subsequently, when a queue job
2609 * structure is freed, it is immediately reallocated for anything on the
2610 * pending request list. The LLDDs defer_rcv() callback is called,
2611 * informing the LLDD that it may reuse the CMD IU buffer, and the io
2612 * is then started normally with the transport.
2614 * The LLDD, when receiving an -EOVERFLOW completion status, is to treat
2615 * the completion as successful but must not reuse the CMD IU buffer
2616 * until the LLDD's defer_rcv() callback has been called for the
2617 * corresponding struct nvmefc_tgt_fcp_req pointer.
2619 * If there is any other condition in which an error occurs, the
2620 * transport will return a non-zero status indicating the error.
2621 * In all cases other than -EOVERFLOW, the transport has not accepted the
2622 * request and the LLDD should abort the exchange.
2624 * @target_port: pointer to the (registered) target port the FCP CMD IU
2626 * @fcpreq: pointer to a fcpreq request structure to be used to reference
2627 * the exchange corresponding to the FCP Exchange.
2628 * @cmdiubuf: pointer to the buffer containing the FCP CMD IU
2629 * @cmdiubuf_len: length, in bytes, of the received FCP CMD IU
2632 nvmet_fc_rcv_fcp_req(struct nvmet_fc_target_port *target_port,
2633 struct nvmefc_tgt_fcp_req *fcpreq,
2634 void *cmdiubuf, u32 cmdiubuf_len)
2636 struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
2637 struct nvme_fc_cmd_iu *cmdiu = cmdiubuf;
2638 struct nvmet_fc_tgt_queue *queue;
2639 struct nvmet_fc_fcp_iod *fod;
2640 struct nvmet_fc_defer_fcp_req *deferfcp;
2641 unsigned long flags;
2643 /* validate iu, so the connection id can be used to find the queue */
2644 if ((cmdiubuf_len != sizeof(*cmdiu)) ||
2645 (cmdiu->format_id != NVME_CMD_FORMAT_ID) ||
2646 (cmdiu->fc_id != NVME_CMD_FC_ID) ||
2647 (be16_to_cpu(cmdiu->iu_len) != (sizeof(*cmdiu)/4)))
2650 queue = nvmet_fc_find_target_queue(tgtport,
2651 be64_to_cpu(cmdiu->connection_id));
2656 * note: reference taken by find_target_queue
2657 * After successful fod allocation, the fod will inherit the
2658 * ownership of that reference and will remove the reference
2659 * when the fod is freed.
2662 spin_lock_irqsave(&queue->qlock, flags);
2664 fod = nvmet_fc_alloc_fcp_iod(queue);
2666 spin_unlock_irqrestore(&queue->qlock, flags);
2668 fcpreq->nvmet_fc_private = fod;
2669 fod->fcpreq = fcpreq;
2671 memcpy(&fod->cmdiubuf, cmdiubuf, cmdiubuf_len);
2673 nvmet_fc_queue_fcp_req(tgtport, queue, fcpreq);
2678 if (!tgtport->ops->defer_rcv) {
2679 spin_unlock_irqrestore(&queue->qlock, flags);
2680 /* release the queue lookup reference */
2681 nvmet_fc_tgt_q_put(queue);
2685 deferfcp = list_first_entry_or_null(&queue->avail_defer_list,
2686 struct nvmet_fc_defer_fcp_req, req_list);
2688 /* Just re-use one that was previously allocated */
2689 list_del(&deferfcp->req_list);
2691 spin_unlock_irqrestore(&queue->qlock, flags);
2693 /* Now we need to dynamically allocate one */
2694 deferfcp = kmalloc(sizeof(*deferfcp), GFP_KERNEL);
2696 /* release the queue lookup reference */
2697 nvmet_fc_tgt_q_put(queue);
2700 spin_lock_irqsave(&queue->qlock, flags);
2703 /* For now, use rspaddr / rsplen to save payload information */
2704 fcpreq->rspaddr = cmdiubuf;
2705 fcpreq->rsplen = cmdiubuf_len;
2706 deferfcp->fcp_req = fcpreq;
2708 /* defer processing till a fod becomes available */
2709 list_add_tail(&deferfcp->req_list, &queue->pending_cmd_list);
2711 /* NOTE: the queue lookup reference is still valid */
2713 spin_unlock_irqrestore(&queue->qlock, flags);
2717 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_fcp_req);
2720 * nvmet_fc_rcv_fcp_abort - transport entry point called by an LLDD
2721 * upon the reception of an ABTS for a FCP command
2723 * Notify the transport that an ABTS has been received for a FCP command
2724 * that had been given to the transport via nvmet_fc_rcv_fcp_req(). The
2725 * LLDD believes the command is still being worked on
2726 * (template_ops->fcp_req_release() has not been called).
2728 * The transport will wait for any outstanding work (an op to the LLDD,
2729 * which the lldd should complete with error due to the ABTS; or the
2730 * completion from the nvmet layer of the nvme command), then will
2731 * stop processing and call the nvmet_fc_rcv_fcp_req() callback to
2732 * return the i/o context to the LLDD. The LLDD may send the BA_ACC
2733 * to the ABTS either after return from this function (assuming any
2734 * outstanding op work has been terminated) or upon the callback being
2737 * @target_port: pointer to the (registered) target port the FCP CMD IU
2739 * @fcpreq: pointer to the fcpreq request structure that corresponds
2740 * to the exchange that received the ABTS.
2743 nvmet_fc_rcv_fcp_abort(struct nvmet_fc_target_port *target_port,
2744 struct nvmefc_tgt_fcp_req *fcpreq)
2746 struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
2747 struct nvmet_fc_tgt_queue *queue;
2748 unsigned long flags;
2750 if (!fod || fod->fcpreq != fcpreq)
2751 /* job appears to have already completed, ignore abort */
2756 spin_lock_irqsave(&queue->qlock, flags);
2759 * mark as abort. The abort handler, invoked upon completion
2760 * of any work, will detect the aborted status and do the
2763 spin_lock(&fod->flock);
2765 fod->aborted = true;
2766 spin_unlock(&fod->flock);
2768 spin_unlock_irqrestore(&queue->qlock, flags);
2770 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_fcp_abort);
2773 struct nvmet_fc_traddr {
2779 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
2783 if (match_u64(sstr, &token64))
2791 * This routine validates and extracts the WWN's from the TRADDR string.
2792 * As kernel parsers need the 0x to determine number base, universally
2793 * build string to parse with 0x prefix before parsing name strings.
2796 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
2798 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
2799 substring_t wwn = { name, &name[sizeof(name)-1] };
2800 int nnoffset, pnoffset;
2802 /* validate if string is one of the 2 allowed formats */
2803 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
2804 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
2805 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
2806 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
2807 nnoffset = NVME_FC_TRADDR_OXNNLEN;
2808 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
2809 NVME_FC_TRADDR_OXNNLEN;
2810 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
2811 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
2812 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
2813 "pn-", NVME_FC_TRADDR_NNLEN))) {
2814 nnoffset = NVME_FC_TRADDR_NNLEN;
2815 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
2821 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
2823 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
2824 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
2827 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
2828 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
2834 pr_warn("%s: bad traddr string\n", __func__);
2839 nvmet_fc_add_port(struct nvmet_port *port)
2841 struct nvmet_fc_tgtport *tgtport;
2842 struct nvmet_fc_port_entry *pe;
2843 struct nvmet_fc_traddr traddr = { 0L, 0L };
2844 unsigned long flags;
2847 /* validate the address info */
2848 if ((port->disc_addr.trtype != NVMF_TRTYPE_FC) ||
2849 (port->disc_addr.adrfam != NVMF_ADDR_FAMILY_FC))
2852 /* map the traddr address info to a target port */
2854 ret = nvme_fc_parse_traddr(&traddr, port->disc_addr.traddr,
2855 sizeof(port->disc_addr.traddr));
2859 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2864 spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
2865 list_for_each_entry(tgtport, &nvmet_fc_target_list, tgt_list) {
2866 if ((tgtport->fc_target_port.node_name == traddr.nn) &&
2867 (tgtport->fc_target_port.port_name == traddr.pn)) {
2868 /* a FC port can only be 1 nvmet port id */
2870 nvmet_fc_portentry_bind(tgtport, pe, port);
2877 spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
2886 nvmet_fc_remove_port(struct nvmet_port *port)
2888 struct nvmet_fc_port_entry *pe = port->priv;
2890 nvmet_fc_portentry_unbind(pe);
2896 nvmet_fc_discovery_chg(struct nvmet_port *port)
2898 struct nvmet_fc_port_entry *pe = port->priv;
2899 struct nvmet_fc_tgtport *tgtport = pe->tgtport;
2901 if (tgtport && tgtport->ops->discovery_event)
2902 tgtport->ops->discovery_event(&tgtport->fc_target_port);
2905 static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = {
2906 .owner = THIS_MODULE,
2907 .type = NVMF_TRTYPE_FC,
2909 .add_port = nvmet_fc_add_port,
2910 .remove_port = nvmet_fc_remove_port,
2911 .queue_response = nvmet_fc_fcp_nvme_cmd_done,
2912 .delete_ctrl = nvmet_fc_delete_ctrl,
2913 .discovery_chg = nvmet_fc_discovery_chg,
2916 static int __init nvmet_fc_init_module(void)
2918 return nvmet_register_transport(&nvmet_fc_tgt_fcp_ops);
2921 static void __exit nvmet_fc_exit_module(void)
2923 /* sanity check - all lports should be removed */
2924 if (!list_empty(&nvmet_fc_target_list))
2925 pr_warn("%s: targetport list not empty\n", __func__);
2927 nvmet_unregister_transport(&nvmet_fc_tgt_fcp_ops);
2929 ida_destroy(&nvmet_fc_tgtport_cnt);
2932 module_init(nvmet_fc_init_module);
2933 module_exit(nvmet_fc_exit_module);
2935 MODULE_LICENSE("GPL v2");