xref: /openbmc/linux/drivers/nvme/target/fc.c (revision 32c2e6dd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 Avago Technologies.  All rights reserved.
4  */
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>
13 
14 #include "nvmet.h"
15 #include <linux/nvme-fc-driver.h>
16 #include <linux/nvme-fc.h>
17 #include "../host/fc.h"
18 
19 
20 /* *************************** Data Structures/Defines ****************** */
21 
22 
23 #define NVMET_LS_CTX_COUNT		256
24 
25 struct nvmet_fc_tgtport;
26 struct nvmet_fc_tgt_assoc;
27 
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 */
31 
32 	struct list_head		ls_rcv_list; /* tgtport->ls_rcv_list */
33 
34 	struct nvmet_fc_tgtport		*tgtport;
35 	struct nvmet_fc_tgt_assoc	*assoc;
36 	void				*hosthandle;
37 
38 	union nvmefc_ls_requests	*rqstbuf;
39 	union nvmefc_ls_responses	*rspbuf;
40 	u16				rqstdatalen;
41 	dma_addr_t			rspdma;
42 
43 	struct scatterlist		sg[2];
44 
45 	struct work_struct		work;
46 } __aligned(sizeof(unsigned long long));
47 
48 struct nvmet_fc_ls_req_op {		/* for an LS RQST XMT */
49 	struct nvmefc_ls_req		ls_req;
50 
51 	struct nvmet_fc_tgtport		*tgtport;
52 	void				*hosthandle;
53 
54 	int				ls_error;
55 	struct list_head		lsreq_list; /* tgtport->ls_req_list */
56 	bool				req_queued;
57 };
58 
59 
60 /* desired maximum for a single sequence - if sg list allows it */
61 #define NVMET_FC_MAX_SEQ_LENGTH		(256 * 1024)
62 
63 enum nvmet_fcp_datadir {
64 	NVMET_FCP_NODATA,
65 	NVMET_FCP_WRITE,
66 	NVMET_FCP_READ,
67 	NVMET_FCP_ABORTED,
68 };
69 
70 struct nvmet_fc_fcp_iod {
71 	struct nvmefc_tgt_fcp_req	*fcpreq;
72 
73 	struct nvme_fc_cmd_iu		cmdiubuf;
74 	struct nvme_fc_ersp_iu		rspiubuf;
75 	dma_addr_t			rspdma;
76 	struct scatterlist		*next_sg;
77 	struct scatterlist		*data_sg;
78 	int				data_sg_cnt;
79 	u32				offset;
80 	enum nvmet_fcp_datadir		io_dir;
81 	bool				active;
82 	bool				abort;
83 	bool				aborted;
84 	bool				writedataactive;
85 	spinlock_t			flock;
86 
87 	struct nvmet_req		req;
88 	struct work_struct		defer_work;
89 
90 	struct nvmet_fc_tgtport		*tgtport;
91 	struct nvmet_fc_tgt_queue	*queue;
92 
93 	struct list_head		fcp_list;	/* tgtport->fcp_list */
94 };
95 
96 struct nvmet_fc_tgtport {
97 	struct nvmet_fc_target_port	fc_target_port;
98 
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;
102 
103 	struct nvmet_fc_ls_iod		*iod;
104 	spinlock_t			lock;
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;
112 	struct kref			ref;
113 	u32				max_sg_cnt;
114 };
115 
116 struct nvmet_fc_port_entry {
117 	struct nvmet_fc_tgtport		*tgtport;
118 	struct nvmet_port		*port;
119 	u64				node_name;
120 	u64				port_name;
121 	struct list_head		pe_list;
122 };
123 
124 struct nvmet_fc_defer_fcp_req {
125 	struct list_head		req_list;
126 	struct nvmefc_tgt_fcp_req	*fcp_req;
127 };
128 
129 struct nvmet_fc_tgt_queue {
130 	bool				ninetypercent;
131 	u16				qid;
132 	u16				sqsize;
133 	u16				ersp_ratio;
134 	__le16				sqhd;
135 	atomic_t			connected;
136 	atomic_t			sqtail;
137 	atomic_t			zrspcnt;
138 	atomic_t			rsn;
139 	spinlock_t			qlock;
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;
147 	struct kref			ref;
148 	struct rcu_head			rcu;
149 	struct nvmet_fc_fcp_iod		fod[];		/* array of fcp_iods */
150 } __aligned(sizeof(unsigned long long));
151 
152 struct nvmet_fc_hostport {
153 	struct nvmet_fc_tgtport		*tgtport;
154 	void				*hosthandle;
155 	struct list_head		host_list;
156 	struct kref			ref;
157 	u8				invalid;
158 };
159 
160 struct nvmet_fc_tgt_assoc {
161 	u64				association_id;
162 	u32				a_id;
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];
169 	struct kref			ref;
170 	struct work_struct		del_work;
171 	struct rcu_head			rcu;
172 };
173 
174 
175 static inline int
176 nvmet_fc_iodnum(struct nvmet_fc_ls_iod *iodptr)
177 {
178 	return (iodptr - iodptr->tgtport->iod);
179 }
180 
181 static inline int
182 nvmet_fc_fodnum(struct nvmet_fc_fcp_iod *fodptr)
183 {
184 	return (fodptr - fodptr->queue->fod);
185 }
186 
187 
188 /*
189  * Association and Connection IDs:
190  *
191  * Association ID will have random number in upper 6 bytes and zero
192  *   in lower 2 bytes
193  *
194  * Connection IDs will be Association ID with QID or'd in lower 2 bytes
195  *
196  * note: Association ID = Connection ID for queue 0
197  */
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))
201 
202 static inline u64
203 nvmet_fc_makeconnid(struct nvmet_fc_tgt_assoc *assoc, u16 qid)
204 {
205 	return (assoc->association_id | qid);
206 }
207 
208 static inline u64
209 nvmet_fc_getassociationid(u64 connectionid)
210 {
211 	return connectionid & ~NVMET_FC_QUEUEID_MASK;
212 }
213 
214 static inline u16
215 nvmet_fc_getqueueid(u64 connectionid)
216 {
217 	return (u16)(connectionid & NVMET_FC_QUEUEID_MASK);
218 }
219 
220 static inline struct nvmet_fc_tgtport *
221 targetport_to_tgtport(struct nvmet_fc_target_port *targetport)
222 {
223 	return container_of(targetport, struct nvmet_fc_tgtport,
224 				 fc_target_port);
225 }
226 
227 static inline struct nvmet_fc_fcp_iod *
228 nvmet_req_to_fod(struct nvmet_req *nvme_req)
229 {
230 	return container_of(nvme_req, struct nvmet_fc_fcp_iod, req);
231 }
232 
233 
234 /* *************************** Globals **************************** */
235 
236 
237 static DEFINE_SPINLOCK(nvmet_fc_tgtlock);
238 
239 static LIST_HEAD(nvmet_fc_target_list);
240 static DEFINE_IDA(nvmet_fc_tgtport_cnt);
241 static LIST_HEAD(nvmet_fc_portentry_list);
242 
243 
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);
257 
258 
259 /* *********************** FC-NVME DMA Handling **************************** */
260 
261 /*
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
265  * may crash.
266  *
267  * As such:
268  * Wrapper all the dma routines and check the dev pointer.
269  *
270  * If simple mappings (return just a dma address, we'll noop them,
271  * returning a dma address of 0.
272  *
273  * On more complex mappings (dma_map_sg), a pseudo routine fills
274  * in the scatter list, setting all dma addresses to 0.
275  */
276 
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)
280 {
281 	return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
282 }
283 
284 static inline int
285 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
286 {
287 	return dev ? dma_mapping_error(dev, dma_addr) : 0;
288 }
289 
290 static inline void
291 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
292 	enum dma_data_direction dir)
293 {
294 	if (dev)
295 		dma_unmap_single(dev, addr, size, dir);
296 }
297 
298 static inline void
299 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
300 		enum dma_data_direction dir)
301 {
302 	if (dev)
303 		dma_sync_single_for_cpu(dev, addr, size, dir);
304 }
305 
306 static inline void
307 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
308 		enum dma_data_direction dir)
309 {
310 	if (dev)
311 		dma_sync_single_for_device(dev, addr, size, dir);
312 }
313 
314 /* pseudo dma_map_sg call */
315 static int
316 fc_map_sg(struct scatterlist *sg, int nents)
317 {
318 	struct scatterlist *s;
319 	int i;
320 
321 	WARN_ON(nents == 0 || sg[0].length == 0);
322 
323 	for_each_sg(sg, s, nents, i) {
324 		s->dma_address = 0L;
325 #ifdef CONFIG_NEED_SG_DMA_LENGTH
326 		s->dma_length = s->length;
327 #endif
328 	}
329 	return nents;
330 }
331 
332 static inline int
333 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
334 		enum dma_data_direction dir)
335 {
336 	return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
337 }
338 
339 static inline void
340 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
341 		enum dma_data_direction dir)
342 {
343 	if (dev)
344 		dma_unmap_sg(dev, sg, nents, dir);
345 }
346 
347 
348 /* ********************** FC-NVME LS XMT Handling ************************* */
349 
350 
351 static void
352 __nvmet_fc_finish_ls_req(struct nvmet_fc_ls_req_op *lsop)
353 {
354 	struct nvmet_fc_tgtport *tgtport = lsop->tgtport;
355 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
356 	unsigned long flags;
357 
358 	spin_lock_irqsave(&tgtport->lock, flags);
359 
360 	if (!lsop->req_queued) {
361 		spin_unlock_irqrestore(&tgtport->lock, flags);
362 		return;
363 	}
364 
365 	list_del(&lsop->lsreq_list);
366 
367 	lsop->req_queued = false;
368 
369 	spin_unlock_irqrestore(&tgtport->lock, flags);
370 
371 	fc_dma_unmap_single(tgtport->dev, lsreq->rqstdma,
372 				  (lsreq->rqstlen + lsreq->rsplen),
373 				  DMA_BIDIRECTIONAL);
374 
375 	nvmet_fc_tgtport_put(tgtport);
376 }
377 
378 static int
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))
382 {
383 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
384 	unsigned long flags;
385 	int ret = 0;
386 
387 	if (!tgtport->ops->ls_req)
388 		return -EOPNOTSUPP;
389 
390 	if (!nvmet_fc_tgtport_get(tgtport))
391 		return -ESHUTDOWN;
392 
393 	lsreq->done = done;
394 	lsop->req_queued = false;
395 	INIT_LIST_HEAD(&lsop->lsreq_list);
396 
397 	lsreq->rqstdma = fc_dma_map_single(tgtport->dev, lsreq->rqstaddr,
398 				  lsreq->rqstlen + lsreq->rsplen,
399 				  DMA_BIDIRECTIONAL);
400 	if (fc_dma_mapping_error(tgtport->dev, lsreq->rqstdma)) {
401 		ret = -EFAULT;
402 		goto out_puttgtport;
403 	}
404 	lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
405 
406 	spin_lock_irqsave(&tgtport->lock, flags);
407 
408 	list_add_tail(&lsop->lsreq_list, &tgtport->ls_req_list);
409 
410 	lsop->req_queued = true;
411 
412 	spin_unlock_irqrestore(&tgtport->lock, flags);
413 
414 	ret = tgtport->ops->ls_req(&tgtport->fc_target_port, lsop->hosthandle,
415 				   lsreq);
416 	if (ret)
417 		goto out_unlink;
418 
419 	return 0;
420 
421 out_unlink:
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),
429 				  DMA_BIDIRECTIONAL);
430 out_puttgtport:
431 	nvmet_fc_tgtport_put(tgtport);
432 
433 	return ret;
434 }
435 
436 static int
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))
440 {
441 	/* don't wait for completion */
442 
443 	return __nvmet_fc_send_ls_req(tgtport, lsop, done);
444 }
445 
446 static void
447 nvmet_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
448 {
449 	struct nvmet_fc_ls_req_op *lsop =
450 		container_of(lsreq, struct nvmet_fc_ls_req_op, ls_req);
451 
452 	__nvmet_fc_finish_ls_req(lsop);
453 
454 	/* fc-nvme target doesn't care about success or failure of cmd */
455 
456 	kfree(lsop);
457 }
458 
459 /*
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
465  * become invalid.
466  *
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.
475  */
476 static void
477 nvmet_fc_xmt_disconnect_assoc(struct nvmet_fc_tgt_assoc *assoc)
478 {
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;
484 	int ret;
485 
486 	/*
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.
490 	 */
491 	if (!tgtport->ops->ls_req || !assoc->hostport ||
492 	    assoc->hostport->invalid)
493 		return;
494 
495 	lsop = kzalloc((sizeof(*lsop) +
496 			sizeof(*discon_rqst) + sizeof(*discon_acc) +
497 			tgtport->ops->lsrqst_priv_sz), GFP_KERNEL);
498 	if (!lsop) {
499 		dev_info(tgtport->dev,
500 			"{%d:%d} send Disconnect Association failed: ENOMEM\n",
501 			tgtport->fc_target_port.port_num, assoc->a_id);
502 		return;
503 	}
504 
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];
510 	else
511 		lsreq->private = NULL;
512 
513 	lsop->tgtport = tgtport;
514 	lsop->hosthandle = assoc->hostport->hosthandle;
515 
516 	nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
517 				assoc->association_id);
518 
519 	ret = nvmet_fc_send_ls_req_async(tgtport, lsop,
520 				nvmet_fc_disconnect_assoc_done);
521 	if (ret) {
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);
525 		kfree(lsop);
526 	}
527 }
528 
529 
530 /* *********************** FC-NVME Port Management ************************ */
531 
532 
533 static int
534 nvmet_fc_alloc_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
535 {
536 	struct nvmet_fc_ls_iod *iod;
537 	int i;
538 
539 	iod = kcalloc(NVMET_LS_CTX_COUNT, sizeof(struct nvmet_fc_ls_iod),
540 			GFP_KERNEL);
541 	if (!iod)
542 		return -ENOMEM;
543 
544 	tgtport->iod = iod;
545 
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);
550 
551 		iod->rqstbuf = kzalloc(sizeof(union nvmefc_ls_requests) +
552 				       sizeof(union nvmefc_ls_responses),
553 				       GFP_KERNEL);
554 		if (!iod->rqstbuf)
555 			goto out_fail;
556 
557 		iod->rspbuf = (union nvmefc_ls_responses *)&iod->rqstbuf[1];
558 
559 		iod->rspdma = fc_dma_map_single(tgtport->dev, iod->rspbuf,
560 						sizeof(*iod->rspbuf),
561 						DMA_TO_DEVICE);
562 		if (fc_dma_mapping_error(tgtport->dev, iod->rspdma))
563 			goto out_fail;
564 	}
565 
566 	return 0;
567 
568 out_fail:
569 	kfree(iod->rqstbuf);
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);
574 		kfree(iod->rqstbuf);
575 		list_del(&iod->ls_rcv_list);
576 	}
577 
578 	kfree(iod);
579 
580 	return -EFAULT;
581 }
582 
583 static void
584 nvmet_fc_free_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
585 {
586 	struct nvmet_fc_ls_iod *iod = tgtport->iod;
587 	int i;
588 
589 	for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
590 		fc_dma_unmap_single(tgtport->dev,
591 				iod->rspdma, sizeof(*iod->rspbuf),
592 				DMA_TO_DEVICE);
593 		kfree(iod->rqstbuf);
594 		list_del(&iod->ls_rcv_list);
595 	}
596 	kfree(tgtport->iod);
597 }
598 
599 static struct nvmet_fc_ls_iod *
600 nvmet_fc_alloc_ls_iod(struct nvmet_fc_tgtport *tgtport)
601 {
602 	struct nvmet_fc_ls_iod *iod;
603 	unsigned long flags;
604 
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);
608 	if (iod)
609 		list_move_tail(&iod->ls_rcv_list, &tgtport->ls_busylist);
610 	spin_unlock_irqrestore(&tgtport->lock, flags);
611 	return iod;
612 }
613 
614 
615 static void
616 nvmet_fc_free_ls_iod(struct nvmet_fc_tgtport *tgtport,
617 			struct nvmet_fc_ls_iod *iod)
618 {
619 	unsigned long flags;
620 
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);
624 }
625 
626 static void
627 nvmet_fc_prep_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
628 				struct nvmet_fc_tgt_queue *queue)
629 {
630 	struct nvmet_fc_fcp_iod *fod = queue->fod;
631 	int i;
632 
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;
636 		fod->queue = queue;
637 		fod->active = false;
638 		fod->abort = false;
639 		fod->aborted = false;
640 		fod->fcpreq = NULL;
641 		list_add_tail(&fod->fcp_list, &queue->fod_list);
642 		spin_lock_init(&fod->flock);
643 
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),
651 						DMA_TO_DEVICE);
652 				fod->rspdma = 0L;
653 				list_del(&fod->fcp_list);
654 			}
655 
656 			return;
657 		}
658 	}
659 }
660 
661 static void
662 nvmet_fc_destroy_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
663 				struct nvmet_fc_tgt_queue *queue)
664 {
665 	struct nvmet_fc_fcp_iod *fod = queue->fod;
666 	int i;
667 
668 	for (i = 0; i < queue->sqsize; fod++, i++) {
669 		if (fod->rspdma)
670 			fc_dma_unmap_single(tgtport->dev, fod->rspdma,
671 				sizeof(fod->rspiubuf), DMA_TO_DEVICE);
672 	}
673 }
674 
675 static struct nvmet_fc_fcp_iod *
676 nvmet_fc_alloc_fcp_iod(struct nvmet_fc_tgt_queue *queue)
677 {
678 	struct nvmet_fc_fcp_iod *fod;
679 
680 	lockdep_assert_held(&queue->qlock);
681 
682 	fod = list_first_entry_or_null(&queue->fod_list,
683 					struct nvmet_fc_fcp_iod, fcp_list);
684 	if (fod) {
685 		list_del(&fod->fcp_list);
686 		fod->active = true;
687 		/*
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.
691 		 */
692 	}
693 	return fod;
694 }
695 
696 
697 static void
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)
701 {
702 	struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
703 
704 	/*
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
707 	 */
708 	fcpreq->hwqid = queue->qid ?
709 			((queue->qid - 1) % tgtport->ops->max_hw_queues) : 0;
710 
711 	nvmet_fc_handle_fcp_rqst(tgtport, fod);
712 }
713 
714 static void
715 nvmet_fc_fcp_rqst_op_defer_work(struct work_struct *work)
716 {
717 	struct nvmet_fc_fcp_iod *fod =
718 		container_of(work, struct nvmet_fc_fcp_iod, defer_work);
719 
720 	/* Submit deferred IO for processing */
721 	nvmet_fc_queue_fcp_req(fod->tgtport, fod->queue, fod->fcpreq);
722 
723 }
724 
725 static void
726 nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
727 			struct nvmet_fc_fcp_iod *fod)
728 {
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;
732 	unsigned long flags;
733 
734 	fc_dma_sync_single_for_cpu(tgtport->dev, fod->rspdma,
735 				sizeof(fod->rspiubuf), DMA_TO_DEVICE);
736 
737 	fcpreq->nvmet_fc_private = NULL;
738 
739 	fod->active = false;
740 	fod->abort = false;
741 	fod->aborted = false;
742 	fod->writedataactive = false;
743 	fod->fcpreq = NULL;
744 
745 	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
746 
747 	/* release the queue lookup reference on the completed IO */
748 	nvmet_fc_tgt_q_put(queue);
749 
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);
753 	if (!deferfcp) {
754 		list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
755 		spin_unlock_irqrestore(&queue->qlock, flags);
756 		return;
757 	}
758 
759 	/* Re-use the fod for the next pending cmd that was deferred */
760 	list_del(&deferfcp->req_list);
761 
762 	fcpreq = deferfcp->fcp_req;
763 
764 	/* deferfcp can be reused for another IO at a later date */
765 	list_add_tail(&deferfcp->req_list, &queue->avail_defer_list);
766 
767 	spin_unlock_irqrestore(&queue->qlock, flags);
768 
769 	/* Save NVME CMD IO in fod */
770 	memcpy(&fod->cmdiubuf, fcpreq->rspaddr, fcpreq->rsplen);
771 
772 	/* Setup new fcpreq to be processed */
773 	fcpreq->rspaddr = NULL;
774 	fcpreq->rsplen  = 0;
775 	fcpreq->nvmet_fc_private = fod;
776 	fod->fcpreq = fcpreq;
777 	fod->active = true;
778 
779 	/* inform LLDD IO is now being processed */
780 	tgtport->ops->defer_rcv(&tgtport->fc_target_port, fcpreq);
781 
782 	/*
783 	 * Leave the queue lookup get reference taken when
784 	 * fod was originally allocated.
785 	 */
786 
787 	queue_work(queue->work_q, &fod->defer_work);
788 }
789 
790 static struct nvmet_fc_tgt_queue *
791 nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
792 			u16 qid, u16 sqsize)
793 {
794 	struct nvmet_fc_tgt_queue *queue;
795 	int ret;
796 
797 	if (qid > NVMET_NR_QUEUES)
798 		return NULL;
799 
800 	queue = kzalloc(struct_size(queue, fod, sqsize), GFP_KERNEL);
801 	if (!queue)
802 		return NULL;
803 
804 	if (!nvmet_fc_tgt_a_get(assoc))
805 		goto out_free_queue;
806 
807 	queue->work_q = alloc_workqueue("ntfc%d.%d.%d", 0, 0,
808 				assoc->tgtport->fc_target_port.port_num,
809 				assoc->a_id, qid);
810 	if (!queue->work_q)
811 		goto out_a_put;
812 
813 	queue->qid = qid;
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);
825 
826 	nvmet_fc_prep_fcp_iodlist(assoc->tgtport, queue);
827 
828 	ret = nvmet_sq_init(&queue->nvme_sq);
829 	if (ret)
830 		goto out_fail_iodlist;
831 
832 	WARN_ON(assoc->queues[qid]);
833 	rcu_assign_pointer(assoc->queues[qid], queue);
834 
835 	return queue;
836 
837 out_fail_iodlist:
838 	nvmet_fc_destroy_fcp_iodlist(assoc->tgtport, queue);
839 	destroy_workqueue(queue->work_q);
840 out_a_put:
841 	nvmet_fc_tgt_a_put(assoc);
842 out_free_queue:
843 	kfree(queue);
844 	return NULL;
845 }
846 
847 
848 static void
849 nvmet_fc_tgt_queue_free(struct kref *ref)
850 {
851 	struct nvmet_fc_tgt_queue *queue =
852 		container_of(ref, struct nvmet_fc_tgt_queue, ref);
853 
854 	rcu_assign_pointer(queue->assoc->queues[queue->qid], NULL);
855 
856 	nvmet_fc_destroy_fcp_iodlist(queue->assoc->tgtport, queue);
857 
858 	nvmet_fc_tgt_a_put(queue->assoc);
859 
860 	destroy_workqueue(queue->work_q);
861 
862 	kfree_rcu(queue, rcu);
863 }
864 
865 static void
866 nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue)
867 {
868 	kref_put(&queue->ref, nvmet_fc_tgt_queue_free);
869 }
870 
871 static int
872 nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue)
873 {
874 	return kref_get_unless_zero(&queue->ref);
875 }
876 
877 
878 static void
879 nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
880 {
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;
884 	unsigned long flags;
885 	int i;
886 	bool disconnect;
887 
888 	disconnect = atomic_xchg(&queue->connected, 0);
889 
890 	/* if not connected, nothing to do */
891 	if (!disconnect)
892 		return;
893 
894 	spin_lock_irqsave(&queue->qlock, flags);
895 	/* abort outstanding io's */
896 	for (i = 0; i < queue->sqsize; fod++, i++) {
897 		if (fod->active) {
898 			spin_lock(&fod->flock);
899 			fod->abort = true;
900 			/*
901 			 * only call lldd abort routine if waiting for
902 			 * writedata. other outstanding ops should finish
903 			 * on their own.
904 			 */
905 			if (fod->writedataactive) {
906 				fod->aborted = true;
907 				spin_unlock(&fod->flock);
908 				tgtport->ops->fcp_abort(
909 					&tgtport->fc_target_port, fod->fcpreq);
910 			} else
911 				spin_unlock(&fod->flock);
912 		}
913 	}
914 
915 	/* Cleanup defer'ed IOs in queue */
916 	list_for_each_entry_safe(deferfcp, tempptr, &queue->avail_defer_list,
917 				req_list) {
918 		list_del(&deferfcp->req_list);
919 		kfree(deferfcp);
920 	}
921 
922 	for (;;) {
923 		deferfcp = list_first_entry_or_null(&queue->pending_cmd_list,
924 				struct nvmet_fc_defer_fcp_req, req_list);
925 		if (!deferfcp)
926 			break;
927 
928 		list_del(&deferfcp->req_list);
929 		spin_unlock_irqrestore(&queue->qlock, flags);
930 
931 		tgtport->ops->defer_rcv(&tgtport->fc_target_port,
932 				deferfcp->fcp_req);
933 
934 		tgtport->ops->fcp_abort(&tgtport->fc_target_port,
935 				deferfcp->fcp_req);
936 
937 		tgtport->ops->fcp_req_release(&tgtport->fc_target_port,
938 				deferfcp->fcp_req);
939 
940 		/* release the queue lookup reference */
941 		nvmet_fc_tgt_q_put(queue);
942 
943 		kfree(deferfcp);
944 
945 		spin_lock_irqsave(&queue->qlock, flags);
946 	}
947 	spin_unlock_irqrestore(&queue->qlock, flags);
948 
949 	flush_workqueue(queue->work_q);
950 
951 	nvmet_sq_destroy(&queue->nvme_sq);
952 
953 	nvmet_fc_tgt_q_put(queue);
954 }
955 
956 static struct nvmet_fc_tgt_queue *
957 nvmet_fc_find_target_queue(struct nvmet_fc_tgtport *tgtport,
958 				u64 connection_id)
959 {
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);
964 
965 	if (qid > NVMET_NR_QUEUES)
966 		return NULL;
967 
968 	rcu_read_lock();
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]);
972 			if (queue &&
973 			    (!atomic_read(&queue->connected) ||
974 			     !nvmet_fc_tgt_q_get(queue)))
975 				queue = NULL;
976 			rcu_read_unlock();
977 			return queue;
978 		}
979 	}
980 	rcu_read_unlock();
981 	return NULL;
982 }
983 
984 static void
985 nvmet_fc_hostport_free(struct kref *ref)
986 {
987 	struct nvmet_fc_hostport *hostport =
988 		container_of(ref, struct nvmet_fc_hostport, ref);
989 	struct nvmet_fc_tgtport *tgtport = hostport->tgtport;
990 	unsigned long flags;
991 
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);
997 	kfree(hostport);
998 	nvmet_fc_tgtport_put(tgtport);
999 }
1000 
1001 static void
1002 nvmet_fc_hostport_put(struct nvmet_fc_hostport *hostport)
1003 {
1004 	kref_put(&hostport->ref, nvmet_fc_hostport_free);
1005 }
1006 
1007 static int
1008 nvmet_fc_hostport_get(struct nvmet_fc_hostport *hostport)
1009 {
1010 	return kref_get_unless_zero(&hostport->ref);
1011 }
1012 
1013 static void
1014 nvmet_fc_free_hostport(struct nvmet_fc_hostport *hostport)
1015 {
1016 	/* if LLDD not implemented, leave as NULL */
1017 	if (!hostport || !hostport->hosthandle)
1018 		return;
1019 
1020 	nvmet_fc_hostport_put(hostport);
1021 }
1022 
1023 static struct nvmet_fc_hostport *
1024 nvmet_fc_alloc_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
1025 {
1026 	struct nvmet_fc_hostport *newhost, *host, *match = NULL;
1027 	unsigned long flags;
1028 
1029 	/* if LLDD not implemented, leave as NULL */
1030 	if (!hosthandle)
1031 		return NULL;
1032 
1033 	/* take reference for what will be the newly allocated hostport */
1034 	if (!nvmet_fc_tgtport_get(tgtport))
1035 		return ERR_PTR(-EINVAL);
1036 
1037 	newhost = kzalloc(sizeof(*newhost), GFP_KERNEL);
1038 	if (!newhost) {
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)) {
1043 					match = host;
1044 					break;
1045 				}
1046 			}
1047 		}
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);
1052 	}
1053 
1054 	newhost->tgtport = tgtport;
1055 	newhost->hosthandle = hosthandle;
1056 	INIT_LIST_HEAD(&newhost->host_list);
1057 	kref_init(&newhost->ref);
1058 
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)) {
1063 				match = host;
1064 				break;
1065 			}
1066 		}
1067 	}
1068 	if (match) {
1069 		kfree(newhost);
1070 		newhost = NULL;
1071 		/* releasing allocation - release reference */
1072 		nvmet_fc_tgtport_put(tgtport);
1073 	} else
1074 		list_add_tail(&newhost->host_list, &tgtport->host_list);
1075 	spin_unlock_irqrestore(&tgtport->lock, flags);
1076 
1077 	return (match) ? match : newhost;
1078 }
1079 
1080 static void
1081 nvmet_fc_delete_assoc(struct work_struct *work)
1082 {
1083 	struct nvmet_fc_tgt_assoc *assoc =
1084 		container_of(work, struct nvmet_fc_tgt_assoc, del_work);
1085 
1086 	nvmet_fc_delete_target_assoc(assoc);
1087 	nvmet_fc_tgt_a_put(assoc);
1088 }
1089 
1090 static struct nvmet_fc_tgt_assoc *
1091 nvmet_fc_alloc_target_assoc(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
1092 {
1093 	struct nvmet_fc_tgt_assoc *assoc, *tmpassoc;
1094 	unsigned long flags;
1095 	u64 ran;
1096 	int idx;
1097 	bool needrandom = true;
1098 
1099 	assoc = kzalloc(sizeof(*assoc), GFP_KERNEL);
1100 	if (!assoc)
1101 		return NULL;
1102 
1103 	idx = ida_simple_get(&tgtport->assoc_cnt, 0, 0, GFP_KERNEL);
1104 	if (idx < 0)
1105 		goto out_free_assoc;
1106 
1107 	if (!nvmet_fc_tgtport_get(tgtport))
1108 		goto out_ida;
1109 
1110 	assoc->hostport = nvmet_fc_alloc_hostport(tgtport, hosthandle);
1111 	if (IS_ERR(assoc->hostport))
1112 		goto out_put;
1113 
1114 	assoc->tgtport = tgtport;
1115 	assoc->a_id = idx;
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);
1120 
1121 	while (needrandom) {
1122 		get_random_bytes(&ran, sizeof(ran) - BYTES_FOR_QID);
1123 		ran = ran << BYTES_FOR_QID_SHIFT;
1124 
1125 		spin_lock_irqsave(&tgtport->lock, flags);
1126 		needrandom = false;
1127 		list_for_each_entry(tmpassoc, &tgtport->assoc_list, a_list) {
1128 			if (ran == tmpassoc->association_id) {
1129 				needrandom = true;
1130 				break;
1131 			}
1132 		}
1133 		if (!needrandom) {
1134 			assoc->association_id = ran;
1135 			list_add_tail_rcu(&assoc->a_list, &tgtport->assoc_list);
1136 		}
1137 		spin_unlock_irqrestore(&tgtport->lock, flags);
1138 	}
1139 
1140 	return assoc;
1141 
1142 out_put:
1143 	nvmet_fc_tgtport_put(tgtport);
1144 out_ida:
1145 	ida_simple_remove(&tgtport->assoc_cnt, idx);
1146 out_free_assoc:
1147 	kfree(assoc);
1148 	return NULL;
1149 }
1150 
1151 static void
1152 nvmet_fc_target_assoc_free(struct kref *ref)
1153 {
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;
1159 
1160 	/* Send Disconnect now that all i/o has completed */
1161 	nvmet_fc_xmt_disconnect_assoc(assoc);
1162 
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 */
1169 	if (oldls)
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);
1177 }
1178 
1179 static void
1180 nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc)
1181 {
1182 	kref_put(&assoc->ref, nvmet_fc_target_assoc_free);
1183 }
1184 
1185 static int
1186 nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc)
1187 {
1188 	return kref_get_unless_zero(&assoc->ref);
1189 }
1190 
1191 static void
1192 nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc *assoc)
1193 {
1194 	struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
1195 	struct nvmet_fc_tgt_queue *queue;
1196 	int i, terminating;
1197 
1198 	terminating = atomic_xchg(&assoc->terminating, 1);
1199 
1200 	/* if already terminating, do nothing */
1201 	if (terminating)
1202 		return;
1203 
1204 
1205 	for (i = NVMET_NR_QUEUES; i >= 0; i--) {
1206 		rcu_read_lock();
1207 		queue = rcu_dereference(assoc->queues[i]);
1208 		if (!queue) {
1209 			rcu_read_unlock();
1210 			continue;
1211 		}
1212 
1213 		if (!nvmet_fc_tgt_q_get(queue)) {
1214 			rcu_read_unlock();
1215 			continue;
1216 		}
1217 		rcu_read_unlock();
1218 		nvmet_fc_delete_target_queue(queue);
1219 		nvmet_fc_tgt_q_put(queue);
1220 	}
1221 
1222 	dev_info(tgtport->dev,
1223 		"{%d:%d} Association deleted\n",
1224 		tgtport->fc_target_port.port_num, assoc->a_id);
1225 
1226 	nvmet_fc_tgt_a_put(assoc);
1227 }
1228 
1229 static struct nvmet_fc_tgt_assoc *
1230 nvmet_fc_find_target_assoc(struct nvmet_fc_tgtport *tgtport,
1231 				u64 association_id)
1232 {
1233 	struct nvmet_fc_tgt_assoc *assoc;
1234 	struct nvmet_fc_tgt_assoc *ret = NULL;
1235 
1236 	rcu_read_lock();
1237 	list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
1238 		if (association_id == assoc->association_id) {
1239 			ret = assoc;
1240 			if (!nvmet_fc_tgt_a_get(assoc))
1241 				ret = NULL;
1242 			break;
1243 		}
1244 	}
1245 	rcu_read_unlock();
1246 
1247 	return ret;
1248 }
1249 
1250 static void
1251 nvmet_fc_portentry_bind(struct nvmet_fc_tgtport *tgtport,
1252 			struct nvmet_fc_port_entry *pe,
1253 			struct nvmet_port *port)
1254 {
1255 	lockdep_assert_held(&nvmet_fc_tgtlock);
1256 
1257 	pe->tgtport = tgtport;
1258 	tgtport->pe = pe;
1259 
1260 	pe->port = port;
1261 	port->priv = pe;
1262 
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);
1266 
1267 	list_add_tail(&pe->pe_list, &nvmet_fc_portentry_list);
1268 }
1269 
1270 static void
1271 nvmet_fc_portentry_unbind(struct nvmet_fc_port_entry *pe)
1272 {
1273 	unsigned long flags;
1274 
1275 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1276 	if (pe->tgtport)
1277 		pe->tgtport->pe = NULL;
1278 	list_del(&pe->pe_list);
1279 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1280 }
1281 
1282 /*
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.
1286  */
1287 static void
1288 nvmet_fc_portentry_unbind_tgt(struct nvmet_fc_tgtport *tgtport)
1289 {
1290 	struct nvmet_fc_port_entry *pe;
1291 	unsigned long flags;
1292 
1293 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1294 	pe = tgtport->pe;
1295 	if (pe)
1296 		pe->tgtport = NULL;
1297 	tgtport->pe = NULL;
1298 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1299 }
1300 
1301 /*
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.
1308  */
1309 static void
1310 nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
1311 {
1312 	struct nvmet_fc_port_entry *pe;
1313 	unsigned long flags;
1314 
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);
1320 			tgtport->pe = pe;
1321 			pe->tgtport = tgtport;
1322 			break;
1323 		}
1324 	}
1325 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1326 }
1327 
1328 /**
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.
1340  *
1341  * Returns:
1342  * a completion status. Must be 0 upon success; a negative errno
1343  * (ex: -ENXIO) upon failure.
1344  */
1345 int
1346 nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
1347 			struct nvmet_fc_target_template *template,
1348 			struct device *dev,
1349 			struct nvmet_fc_target_port **portptr)
1350 {
1351 	struct nvmet_fc_tgtport *newrec;
1352 	unsigned long flags;
1353 	int ret, idx;
1354 
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) {
1360 		ret = -EINVAL;
1361 		goto out_regtgt_failed;
1362 	}
1363 
1364 	newrec = kzalloc((sizeof(*newrec) + template->target_priv_sz),
1365 			 GFP_KERNEL);
1366 	if (!newrec) {
1367 		ret = -ENOMEM;
1368 		goto out_regtgt_failed;
1369 	}
1370 
1371 	idx = ida_simple_get(&nvmet_fc_tgtport_cnt, 0, 0, GFP_KERNEL);
1372 	if (idx < 0) {
1373 		ret = -ENOSPC;
1374 		goto out_fail_kfree;
1375 	}
1376 
1377 	if (!get_device(dev) && dev) {
1378 		ret = -ENODEV;
1379 		goto out_ida_put;
1380 	}
1381 
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];
1386 	else
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);
1391 	newrec->dev = dev;
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;
1402 
1403 	ret = nvmet_fc_alloc_ls_iodlist(newrec);
1404 	if (ret) {
1405 		ret = -ENOMEM;
1406 		goto out_free_newrec;
1407 	}
1408 
1409 	nvmet_fc_portentry_rebind_tgt(newrec);
1410 
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);
1414 
1415 	*portptr = &newrec->fc_target_port;
1416 	return 0;
1417 
1418 out_free_newrec:
1419 	put_device(dev);
1420 out_ida_put:
1421 	ida_simple_remove(&nvmet_fc_tgtport_cnt, idx);
1422 out_fail_kfree:
1423 	kfree(newrec);
1424 out_regtgt_failed:
1425 	*portptr = NULL;
1426 	return ret;
1427 }
1428 EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
1429 
1430 
1431 static void
1432 nvmet_fc_free_tgtport(struct kref *ref)
1433 {
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;
1438 
1439 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1440 	list_del(&tgtport->tgt_list);
1441 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1442 
1443 	nvmet_fc_free_ls_iodlist(tgtport);
1444 
1445 	/* let the LLDD know we've finished tearing it down */
1446 	tgtport->ops->targetport_delete(&tgtport->fc_target_port);
1447 
1448 	ida_simple_remove(&nvmet_fc_tgtport_cnt,
1449 			tgtport->fc_target_port.port_num);
1450 
1451 	ida_destroy(&tgtport->assoc_cnt);
1452 
1453 	kfree(tgtport);
1454 
1455 	put_device(dev);
1456 }
1457 
1458 static void
1459 nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport)
1460 {
1461 	kref_put(&tgtport->ref, nvmet_fc_free_tgtport);
1462 }
1463 
1464 static int
1465 nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport)
1466 {
1467 	return kref_get_unless_zero(&tgtport->ref);
1468 }
1469 
1470 static void
1471 __nvmet_fc_free_assocs(struct nvmet_fc_tgtport *tgtport)
1472 {
1473 	struct nvmet_fc_tgt_assoc *assoc;
1474 
1475 	rcu_read_lock();
1476 	list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
1477 		if (!nvmet_fc_tgt_a_get(assoc))
1478 			continue;
1479 		if (!schedule_work(&assoc->del_work))
1480 			/* already deleting - release local reference */
1481 			nvmet_fc_tgt_a_put(assoc);
1482 	}
1483 	rcu_read_unlock();
1484 }
1485 
1486 /**
1487  * nvmet_fc_invalidate_host - transport entry point called by an LLDD
1488  *                       to remove references to a hosthandle for LS's.
1489  *
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.
1494  *
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.
1507  *
1508  * @target_port: pointer to the (registered) target port that a prior
1509  *              LS was received on and which supplied the transport the
1510  *              hosthandle.
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.
1514  */
1515 void
1516 nvmet_fc_invalidate_host(struct nvmet_fc_target_port *target_port,
1517 			void *hosthandle)
1518 {
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;
1523 
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)
1529 			continue;
1530 		if (!nvmet_fc_tgt_a_get(assoc))
1531 			continue;
1532 		assoc->hostport->invalid = 1;
1533 		noassoc = false;
1534 		if (!schedule_work(&assoc->del_work))
1535 			/* already deleting - release local reference */
1536 			nvmet_fc_tgt_a_put(assoc);
1537 	}
1538 	spin_unlock_irqrestore(&tgtport->lock, flags);
1539 
1540 	/* if there's nothing to wait for - call the callback */
1541 	if (noassoc && tgtport->ops->host_release)
1542 		tgtport->ops->host_release(hosthandle);
1543 }
1544 EXPORT_SYMBOL_GPL(nvmet_fc_invalidate_host);
1545 
1546 /*
1547  * nvmet layer has called to terminate an association
1548  */
1549 static void
1550 nvmet_fc_delete_ctrl(struct nvmet_ctrl *ctrl)
1551 {
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;
1557 
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,
1561 			tgt_list) {
1562 		if (!nvmet_fc_tgtport_get(tgtport))
1563 			continue;
1564 		spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1565 
1566 		rcu_read_lock();
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))
1571 					found_ctrl = true;
1572 				break;
1573 			}
1574 		}
1575 		rcu_read_unlock();
1576 
1577 		nvmet_fc_tgtport_put(tgtport);
1578 
1579 		if (found_ctrl) {
1580 			if (!schedule_work(&assoc->del_work))
1581 				/* already deleting - release local reference */
1582 				nvmet_fc_tgt_a_put(assoc);
1583 			return;
1584 		}
1585 
1586 		spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1587 	}
1588 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1589 }
1590 
1591 /**
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
1596  *               deregistered.
1597  *
1598  * Returns:
1599  * a completion status. Must be 0 upon success; a negative errno
1600  * (ex: -ENXIO) upon failure.
1601  */
1602 int
1603 nvmet_fc_unregister_targetport(struct nvmet_fc_target_port *target_port)
1604 {
1605 	struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
1606 
1607 	nvmet_fc_portentry_unbind_tgt(tgtport);
1608 
1609 	/* terminate any outstanding associations */
1610 	__nvmet_fc_free_assocs(tgtport);
1611 
1612 	/*
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.
1617 	 */
1618 
1619 	nvmet_fc_tgtport_put(tgtport);
1620 
1621 	return 0;
1622 }
1623 EXPORT_SYMBOL_GPL(nvmet_fc_unregister_targetport);
1624 
1625 
1626 /* ********************** FC-NVME LS RCV Handling ************************* */
1627 
1628 
1629 static void
1630 nvmet_fc_ls_create_association(struct nvmet_fc_tgtport *tgtport,
1631 			struct nvmet_fc_ls_iod *iod)
1632 {
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;
1636 	int ret = 0;
1637 
1638 	memset(acc, 0, sizeof(*acc));
1639 
1640 	/*
1641 	 * FC-NVME spec changes. There are initiators sending different
1642 	 * lengths as padding sizes for Create Association Cmd descriptor
1643 	 * was incorrect.
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.
1647 	 */
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;
1663 
1664 	else {
1665 		/* new association w/ admin queue */
1666 		iod->assoc = nvmet_fc_alloc_target_assoc(
1667 						tgtport, iod->hosthandle);
1668 		if (!iod->assoc)
1669 			ret = VERR_ASSOC_ALLOC_FAIL;
1670 		else {
1671 			queue = nvmet_fc_alloc_target_queue(iod->assoc, 0,
1672 					be16_to_cpu(rqst->assoc_cmd.sqsize));
1673 			if (!queue)
1674 				ret = VERR_QUEUE_ALLOC_FAIL;
1675 		}
1676 	}
1677 
1678 	if (ret) {
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);
1686 		return;
1687 	}
1688 
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 */
1692 
1693 	dev_info(tgtport->dev,
1694 		"{%d:%d} Association created\n",
1695 		tgtport->fc_target_port.port_num, iod->assoc->a_id);
1696 
1697 	/* format a response */
1698 
1699 	iod->lsrsp->rsplen = sizeof(*acc);
1700 
1701 	nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1702 			fcnvme_lsdesc_len(
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 =
1707 			fcnvme_lsdesc_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 =
1713 			fcnvme_lsdesc_len(
1714 				sizeof(struct fcnvme_lsdesc_conn_id));
1715 	acc->connectid.connection_id = acc->associd.association_id;
1716 }
1717 
1718 static void
1719 nvmet_fc_ls_create_connection(struct nvmet_fc_tgtport *tgtport,
1720 			struct nvmet_fc_ls_iod *iod)
1721 {
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;
1725 	int ret = 0;
1726 
1727 	memset(acc, 0, sizeof(*acc));
1728 
1729 	if (iod->rqstdatalen < sizeof(struct fcnvme_ls_cr_conn_rqst))
1730 		ret = VERR_CR_CONN_LEN;
1731 	else if (rqst->desc_list_len !=
1732 			fcnvme_lsdesc_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 !=
1738 			fcnvme_lsdesc_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 !=
1745 			fcnvme_lsdesc_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;
1752 
1753 	else {
1754 		/* new io queue */
1755 		iod->assoc = nvmet_fc_find_target_assoc(tgtport,
1756 				be64_to_cpu(rqst->associd.association_id));
1757 		if (!iod->assoc)
1758 			ret = VERR_NO_ASSOC;
1759 		else {
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));
1763 			if (!queue)
1764 				ret = VERR_QUEUE_ALLOC_FAIL;
1765 
1766 			/* release get taken in nvmet_fc_find_target_assoc */
1767 			nvmet_fc_tgt_a_put(iod->assoc);
1768 		}
1769 	}
1770 
1771 	if (ret) {
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);
1781 		return;
1782 	}
1783 
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 */
1787 
1788 	/* format a response */
1789 
1790 	iod->lsrsp->rsplen = sizeof(*acc);
1791 
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 =
1797 			fcnvme_lsdesc_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)));
1802 }
1803 
1804 /*
1805  * Returns true if the LS response is to be transmit
1806  * Returns false if the LS response is to be delayed
1807  */
1808 static int
1809 nvmet_fc_ls_disconnect(struct nvmet_fc_tgtport *tgtport,
1810 			struct nvmet_fc_ls_iod *iod)
1811 {
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;
1819 	int ret = 0;
1820 
1821 	memset(acc, 0, sizeof(*acc));
1822 
1823 	ret = nvmefc_vldt_lsreq_discon_assoc(iod->rqstdatalen, rqst);
1824 	if (!ret) {
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));
1828 		iod->assoc = assoc;
1829 		if (!assoc)
1830 			ret = VERR_NO_ASSOC;
1831 	}
1832 
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);
1843 		return true;
1844 	}
1845 
1846 	/* format a response */
1847 
1848 	iod->lsrsp->rsplen = sizeof(*acc);
1849 
1850 	nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1851 			fcnvme_lsdesc_len(
1852 				sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
1853 			FCNVME_LS_DISCONNECT_ASSOC);
1854 
1855 	/* release get taken in nvmet_fc_find_target_assoc */
1856 	nvmet_fc_tgt_a_put(assoc);
1857 
1858 	/*
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
1865 	 * on the older one.
1866 	 */
1867 	spin_lock_irqsave(&tgtport->lock, flags);
1868 	oldls = assoc->rcv_disconn;
1869 	assoc->rcv_disconn = iod;
1870 	spin_unlock_irqrestore(&tgtport->lock, flags);
1871 
1872 	nvmet_fc_delete_target_assoc(assoc);
1873 
1874 	if (oldls) {
1875 		dev_info(tgtport->dev,
1876 			"{%d:%d} Multiple Disconnect Association LS's "
1877 			"received\n",
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 */
1883 						rqst->w0.ls_cmd,
1884 						FCNVME_RJT_RC_UNAB,
1885 						FCNVME_RJT_EXP_NONE, 0);
1886 		nvmet_fc_xmt_ls_rsp(tgtport, oldls);
1887 	}
1888 
1889 	return false;
1890 }
1891 
1892 
1893 /* *********************** NVME Ctrl Routines **************************** */
1894 
1895 
1896 static void nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req);
1897 
1898 static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops;
1899 
1900 static void
1901 nvmet_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
1902 {
1903 	struct nvmet_fc_ls_iod *iod = lsrsp->nvme_fc_private;
1904 	struct nvmet_fc_tgtport *tgtport = iod->tgtport;
1905 
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);
1910 }
1911 
1912 static void
1913 nvmet_fc_xmt_ls_rsp(struct nvmet_fc_tgtport *tgtport,
1914 				struct nvmet_fc_ls_iod *iod)
1915 {
1916 	int ret;
1917 
1918 	fc_dma_sync_single_for_device(tgtport->dev, iod->rspdma,
1919 				  sizeof(*iod->rspbuf), DMA_TO_DEVICE);
1920 
1921 	ret = tgtport->ops->xmt_ls_rsp(&tgtport->fc_target_port, iod->lsrsp);
1922 	if (ret)
1923 		nvmet_fc_xmt_ls_rsp_done(iod->lsrsp);
1924 }
1925 
1926 /*
1927  * Actual processing routine for received FC-NVME LS Requests from the LLD
1928  */
1929 static void
1930 nvmet_fc_handle_ls_rqst(struct nvmet_fc_tgtport *tgtport,
1931 			struct nvmet_fc_ls_iod *iod)
1932 {
1933 	struct fcnvme_ls_rqst_w0 *w0 = &iod->rqstbuf->rq_cr_assoc.w0;
1934 	bool sendrsp = true;
1935 
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;
1942 
1943 	iod->assoc = NULL;
1944 
1945 	/*
1946 	 * handlers:
1947 	 *   parse request input, execute the request, and format the
1948 	 *   LS response
1949 	 */
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);
1954 		break;
1955 	case FCNVME_LS_CREATE_CONNECTION:
1956 		/* Creates an IO Queue/Connection */
1957 		nvmet_fc_ls_create_connection(tgtport, iod);
1958 		break;
1959 	case FCNVME_LS_DISCONNECT_ASSOC:
1960 		/* Terminate a Queue/Connection or the Association */
1961 		sendrsp = nvmet_fc_ls_disconnect(tgtport, iod);
1962 		break;
1963 	default:
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);
1967 	}
1968 
1969 	if (sendrsp)
1970 		nvmet_fc_xmt_ls_rsp(tgtport, iod);
1971 }
1972 
1973 /*
1974  * Actual processing routine for received FC-NVME LS Requests from the LLD
1975  */
1976 static void
1977 nvmet_fc_handle_ls_rqst_work(struct work_struct *work)
1978 {
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;
1982 
1983 	nvmet_fc_handle_ls_rqst(tgtport, iod);
1984 }
1985 
1986 
1987 /**
1988  * nvmet_fc_rcv_ls_req - transport entry point called by an LLDD
1989  *                       upon the reception of a NVME LS request.
1990  *
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.
1994  *
1995  * If this routine returns error, the LLDD should abort the exchange.
1996  *
1997  * @target_port: pointer to the (registered) target port the LS was
1998  *              received on.
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
2003  */
2004 int
2005 nvmet_fc_rcv_ls_req(struct nvmet_fc_target_port *target_port,
2006 			void *hosthandle,
2007 			struct nvmefc_ls_rsp *lsrsp,
2008 			void *lsreqbuf, u32 lsreqbuf_len)
2009 {
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;
2013 
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] : "",
2019 			lsreqbuf_len);
2020 		return -E2BIG;
2021 	}
2022 
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] : "");
2028 		return -ESHUTDOWN;
2029 	}
2030 
2031 	iod = nvmet_fc_alloc_ls_iod(tgtport);
2032 	if (!iod) {
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);
2038 		return -ENOENT;
2039 	}
2040 
2041 	iod->lsrsp = lsrsp;
2042 	iod->fcpreq = NULL;
2043 	memcpy(iod->rqstbuf, lsreqbuf, lsreqbuf_len);
2044 	iod->rqstdatalen = lsreqbuf_len;
2045 	iod->hosthandle = hosthandle;
2046 
2047 	schedule_work(&iod->work);
2048 
2049 	return 0;
2050 }
2051 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_ls_req);
2052 
2053 
2054 /*
2055  * **********************
2056  * Start of FCP handling
2057  * **********************
2058  */
2059 
2060 static int
2061 nvmet_fc_alloc_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
2062 {
2063 	struct scatterlist *sg;
2064 	unsigned int nent;
2065 
2066 	sg = sgl_alloc(fod->req.transfer_len, GFP_KERNEL, &nent);
2067 	if (!sg)
2068 		goto out;
2069 
2070 	fod->data_sg = sg;
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;
2077 
2078 	return 0;
2079 
2080 out:
2081 	return NVME_SC_INTERNAL;
2082 }
2083 
2084 static void
2085 nvmet_fc_free_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
2086 {
2087 	if (!fod->data_sg || !fod->data_sg_cnt)
2088 		return;
2089 
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;
2096 }
2097 
2098 
2099 static bool
2100 queue_90percent_full(struct nvmet_fc_tgt_queue *q, u32 sqhd)
2101 {
2102 	u32 sqtail, used;
2103 
2104 	/* egad, this is ugly. And sqtail is just a best guess */
2105 	sqtail = atomic_read(&q->sqtail) % q->sqsize;
2106 
2107 	used = (sqtail < sqhd) ? (sqtail + q->sqsize - sqhd) : (sqtail - sqhd);
2108 	return ((used * 10) >= (((u32)(q->sqsize - 1) * 9)));
2109 }
2110 
2111 /*
2112  * Prep RSP payload.
2113  * May be a NVMET_FCOP_RSP or NVMET_FCOP_READDATA_RSP op
2114  */
2115 static void
2116 nvmet_fc_prep_fcp_rsp(struct nvmet_fc_tgtport *tgtport,
2117 				struct nvmet_fc_fcp_iod *fod)
2118 {
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;
2125 
2126 	if (fod->fcpreq->op == NVMET_FCOP_READDATA_RSP)
2127 		xfr_length = fod->req.transfer_len;
2128 	else
2129 		xfr_length = fod->offset;
2130 
2131 	/*
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
2138 	 *   this.
2139 	 *
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
2143 	 *    practice)
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
2149 	 */
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)))
2157 		send_ersp = true;
2158 
2159 	/* re-set the fields */
2160 	fod->fcpreq->rspaddr = ersp;
2161 	fod->fcpreq->rspdma = fod->rspdma;
2162 
2163 	if (!send_ersp) {
2164 		memset(ersp, 0, NVME_FC_SIZEOF_ZEROS_RSP);
2165 		fod->fcpreq->rsplen = NVME_FC_SIZEOF_ZEROS_RSP;
2166 	} else {
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);
2172 	}
2173 
2174 	fc_dma_sync_single_for_device(tgtport->dev, fod->rspdma,
2175 				  sizeof(fod->rspiubuf), DMA_TO_DEVICE);
2176 }
2177 
2178 static void nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req *fcpreq);
2179 
2180 static void
2181 nvmet_fc_abort_op(struct nvmet_fc_tgtport *tgtport,
2182 				struct nvmet_fc_fcp_iod *fod)
2183 {
2184 	struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
2185 
2186 	/* data no longer needed */
2187 	nvmet_fc_free_tgt_pgs(fod);
2188 
2189 	/*
2190 	 * if an ABTS was received or we issued the fcp_abort early
2191 	 * don't call abort routine again.
2192 	 */
2193 	/* no need to take lock - lock was taken earlier to get here */
2194 	if (!fod->aborted)
2195 		tgtport->ops->fcp_abort(&tgtport->fc_target_port, fcpreq);
2196 
2197 	nvmet_fc_free_fcp_iod(fod->queue, fod);
2198 }
2199 
2200 static void
2201 nvmet_fc_xmt_fcp_rsp(struct nvmet_fc_tgtport *tgtport,
2202 				struct nvmet_fc_fcp_iod *fod)
2203 {
2204 	int ret;
2205 
2206 	fod->fcpreq->op = NVMET_FCOP_RSP;
2207 	fod->fcpreq->timeout = 0;
2208 
2209 	nvmet_fc_prep_fcp_rsp(tgtport, fod);
2210 
2211 	ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fod->fcpreq);
2212 	if (ret)
2213 		nvmet_fc_abort_op(tgtport, fod);
2214 }
2215 
2216 static void
2217 nvmet_fc_transfer_fcp_data(struct nvmet_fc_tgtport *tgtport,
2218 				struct nvmet_fc_fcp_iod *fod, u8 op)
2219 {
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;
2224 	u32 tlen = 0;
2225 	int ret;
2226 
2227 	fcpreq->op = op;
2228 	fcpreq->offset = fod->offset;
2229 	fcpreq->timeout = NVME_FC_TGTOP_TIMEOUT_SEC;
2230 
2231 	/*
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.
2239 	 */
2240 	fcpreq->sg = sg;
2241 	fcpreq->sg_cnt = 0;
2242 	while (tlen < remaininglen &&
2243 	       fcpreq->sg_cnt < tgtport->max_sg_cnt &&
2244 	       tlen + sg_dma_len(sg) < NVMET_FC_MAX_SEQ_LENGTH) {
2245 		fcpreq->sg_cnt++;
2246 		tlen += sg_dma_len(sg);
2247 		sg = sg_next(sg);
2248 	}
2249 	if (tlen < remaininglen && fcpreq->sg_cnt == 0) {
2250 		fcpreq->sg_cnt++;
2251 		tlen += min_t(u32, sg_dma_len(sg), remaininglen);
2252 		sg = sg_next(sg);
2253 	}
2254 	if (tlen < remaininglen)
2255 		fod->next_sg = sg;
2256 	else
2257 		fod->next_sg = NULL;
2258 
2259 	fcpreq->transfer_length = tlen;
2260 	fcpreq->transferred_length = 0;
2261 	fcpreq->fcp_error = 0;
2262 	fcpreq->rsplen = 0;
2263 
2264 	/*
2265 	 * If the last READDATA request: check if LLDD supports
2266 	 * combined xfr with response.
2267 	 */
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);
2273 	}
2274 
2275 	ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fod->fcpreq);
2276 	if (ret) {
2277 		/*
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
2281 		 */
2282 		fod->abort = true;
2283 
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);
2293 		}
2294 	}
2295 }
2296 
2297 static inline bool
2298 __nvmet_fc_fod_op_abort(struct nvmet_fc_fcp_iod *fod, bool abort)
2299 {
2300 	struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
2301 	struct nvmet_fc_tgtport *tgtport = fod->tgtport;
2302 
2303 	/* if in the middle of an io and we need to tear down */
2304 	if (abort) {
2305 		if (fcpreq->op == NVMET_FCOP_WRITEDATA) {
2306 			nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
2307 			return true;
2308 		}
2309 
2310 		nvmet_fc_abort_op(tgtport, fod);
2311 		return true;
2312 	}
2313 
2314 	return false;
2315 }
2316 
2317 /*
2318  * actual done handler for FCP operations when completed by the lldd
2319  */
2320 static void
2321 nvmet_fc_fod_op_done(struct nvmet_fc_fcp_iod *fod)
2322 {
2323 	struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
2324 	struct nvmet_fc_tgtport *tgtport = fod->tgtport;
2325 	unsigned long flags;
2326 	bool abort;
2327 
2328 	spin_lock_irqsave(&fod->flock, flags);
2329 	abort = fod->abort;
2330 	fod->writedataactive = false;
2331 	spin_unlock_irqrestore(&fod->flock, flags);
2332 
2333 	switch (fcpreq->op) {
2334 
2335 	case NVMET_FCOP_WRITEDATA:
2336 		if (__nvmet_fc_fod_op_abort(fod, abort))
2337 			return;
2338 		if (fcpreq->fcp_error ||
2339 		    fcpreq->transferred_length != fcpreq->transfer_length) {
2340 			spin_lock_irqsave(&fod->flock, flags);
2341 			fod->abort = true;
2342 			spin_unlock_irqrestore(&fod->flock, flags);
2343 
2344 			nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
2345 			return;
2346 		}
2347 
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);
2353 
2354 			/* transfer the next chunk */
2355 			nvmet_fc_transfer_fcp_data(tgtport, fod,
2356 						NVMET_FCOP_WRITEDATA);
2357 			return;
2358 		}
2359 
2360 		/* data transfer complete, resume with nvmet layer */
2361 		fod->req.execute(&fod->req);
2362 		break;
2363 
2364 	case NVMET_FCOP_READDATA:
2365 	case NVMET_FCOP_READDATA_RSP:
2366 		if (__nvmet_fc_fod_op_abort(fod, abort))
2367 			return;
2368 		if (fcpreq->fcp_error ||
2369 		    fcpreq->transferred_length != fcpreq->transfer_length) {
2370 			nvmet_fc_abort_op(tgtport, fod);
2371 			return;
2372 		}
2373 
2374 		/* success */
2375 
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);
2380 			return;
2381 		}
2382 
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);
2388 			return;
2389 		}
2390 
2391 		/* data transfer complete, send response */
2392 
2393 		/* data no longer needed */
2394 		nvmet_fc_free_tgt_pgs(fod);
2395 
2396 		nvmet_fc_xmt_fcp_rsp(tgtport, fod);
2397 
2398 		break;
2399 
2400 	case NVMET_FCOP_RSP:
2401 		if (__nvmet_fc_fod_op_abort(fod, abort))
2402 			return;
2403 		nvmet_fc_free_fcp_iod(fod->queue, fod);
2404 		break;
2405 
2406 	default:
2407 		break;
2408 	}
2409 }
2410 
2411 static void
2412 nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req *fcpreq)
2413 {
2414 	struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
2415 
2416 	nvmet_fc_fod_op_done(fod);
2417 }
2418 
2419 /*
2420  * actual completion handler after execution by the nvmet layer
2421  */
2422 static void
2423 __nvmet_fc_fcp_nvme_cmd_done(struct nvmet_fc_tgtport *tgtport,
2424 			struct nvmet_fc_fcp_iod *fod, int status)
2425 {
2426 	struct nvme_common_command *sqe = &fod->cmdiubuf.sqe.common;
2427 	struct nvme_completion *cqe = &fod->rspiubuf.cqe;
2428 	unsigned long flags;
2429 	bool abort;
2430 
2431 	spin_lock_irqsave(&fod->flock, flags);
2432 	abort = fod->abort;
2433 	spin_unlock_irqrestore(&fod->flock, flags);
2434 
2435 	/* if we have a CQE, snoop the last sq_head value */
2436 	if (!status)
2437 		fod->queue->sqhd = cqe->sq_head;
2438 
2439 	if (abort) {
2440 		nvmet_fc_abort_op(tgtport, fod);
2441 		return;
2442 	}
2443 
2444 	/* if an error handling the cmd post initial parsing */
2445 	if (status) {
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);
2452 	} else {
2453 
2454 		/*
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
2457 		 * be moved
2458 		 */
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);
2463 			return;
2464 		}
2465 
2466 		/* writes & no data - fall thru */
2467 	}
2468 
2469 	/* data no longer needed */
2470 	nvmet_fc_free_tgt_pgs(fod);
2471 
2472 	nvmet_fc_xmt_fcp_rsp(tgtport, fod);
2473 }
2474 
2475 
2476 static void
2477 nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req)
2478 {
2479 	struct nvmet_fc_fcp_iod *fod = nvmet_req_to_fod(nvme_req);
2480 	struct nvmet_fc_tgtport *tgtport = fod->tgtport;
2481 
2482 	__nvmet_fc_fcp_nvme_cmd_done(tgtport, fod, 0);
2483 }
2484 
2485 
2486 /*
2487  * Actual processing routine for received FC-NVME I/O Requests from the LLD
2488  */
2489 static void
2490 nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
2491 			struct nvmet_fc_fcp_iod *fod)
2492 {
2493 	struct nvme_fc_cmd_iu *cmdiu = &fod->cmdiubuf;
2494 	u32 xfrlen = be32_to_cpu(cmdiu->data_len);
2495 	int ret;
2496 
2497 	/*
2498 	 * if there is no nvmet mapping to the targetport there
2499 	 * shouldn't be requests. just terminate them.
2500 	 */
2501 	if (!tgtport->pe)
2502 		goto transport_error;
2503 
2504 	/*
2505 	 * Fused commands are currently not supported in the linux
2506 	 * implementation.
2507 	 *
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.
2511 	 */
2512 
2513 	fod->fcpreq->done = nvmet_fc_xmt_fcp_op_done;
2514 
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;
2523 	} else {
2524 		fod->io_dir = NVMET_FCP_NODATA;
2525 		if (xfrlen)
2526 			goto transport_error;
2527 	}
2528 
2529 	fod->req.cmd = &fod->cmdiubuf.sqe;
2530 	fod->req.cqe = &fod->rspiubuf.cqe;
2531 	fod->req.port = tgtport->pe->port;
2532 
2533 	/* clear any response payload */
2534 	memset(&fod->rspiubuf, 0, sizeof(fod->rspiubuf));
2535 
2536 	fod->data_sg = NULL;
2537 	fod->data_sg_cnt = 0;
2538 
2539 	ret = nvmet_req_init(&fod->req,
2540 				&fod->queue->nvme_cq,
2541 				&fod->queue->nvme_sq,
2542 				&nvmet_fc_tgt_fcp_ops);
2543 	if (!ret) {
2544 		/* bad SQE content or invalid ctrl state */
2545 		/* nvmet layer has already called op done to send rsp. */
2546 		return;
2547 	}
2548 
2549 	fod->req.transfer_len = xfrlen;
2550 
2551 	/* keep a running counter of tail position */
2552 	atomic_inc(&fod->queue->sqtail);
2553 
2554 	if (fod->req.transfer_len) {
2555 		ret = nvmet_fc_alloc_tgt_pgs(fod);
2556 		if (ret) {
2557 			nvmet_req_complete(&fod->req, ret);
2558 			return;
2559 		}
2560 	}
2561 	fod->req.sg = fod->data_sg;
2562 	fod->req.sg_cnt = fod->data_sg_cnt;
2563 	fod->offset = 0;
2564 
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);
2568 		return;
2569 	}
2570 
2571 	/*
2572 	 * Reads or no data:
2573 	 *
2574 	 * can invoke the nvmet_layer now. If read data, cmd completion will
2575 	 * push the data
2576 	 */
2577 	fod->req.execute(&fod->req);
2578 	return;
2579 
2580 transport_error:
2581 	nvmet_fc_abort_op(tgtport, fod);
2582 }
2583 
2584 /**
2585  * nvmet_fc_rcv_fcp_req - transport entry point called by an LLDD
2586  *                       upon the reception of a NVME FCP CMD IU.
2587  *
2588  * Pass a FC-NVME FCP CMD IU received from the FC link to the nvmet-fc
2589  * layer for processing.
2590  *
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.
2596  *
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.
2613  *
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.
2618  *
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.
2623  *
2624  * @target_port: pointer to the (registered) target port the FCP CMD IU
2625  *              was received on.
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
2630  */
2631 int
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)
2635 {
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;
2642 
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)))
2648 		return -EIO;
2649 
2650 	queue = nvmet_fc_find_target_queue(tgtport,
2651 				be64_to_cpu(cmdiu->connection_id));
2652 	if (!queue)
2653 		return -ENOTCONN;
2654 
2655 	/*
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.
2660 	 */
2661 
2662 	spin_lock_irqsave(&queue->qlock, flags);
2663 
2664 	fod = nvmet_fc_alloc_fcp_iod(queue);
2665 	if (fod) {
2666 		spin_unlock_irqrestore(&queue->qlock, flags);
2667 
2668 		fcpreq->nvmet_fc_private = fod;
2669 		fod->fcpreq = fcpreq;
2670 
2671 		memcpy(&fod->cmdiubuf, cmdiubuf, cmdiubuf_len);
2672 
2673 		nvmet_fc_queue_fcp_req(tgtport, queue, fcpreq);
2674 
2675 		return 0;
2676 	}
2677 
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);
2682 		return -ENOENT;
2683 	}
2684 
2685 	deferfcp = list_first_entry_or_null(&queue->avail_defer_list,
2686 			struct nvmet_fc_defer_fcp_req, req_list);
2687 	if (deferfcp) {
2688 		/* Just re-use one that was previously allocated */
2689 		list_del(&deferfcp->req_list);
2690 	} else {
2691 		spin_unlock_irqrestore(&queue->qlock, flags);
2692 
2693 		/* Now we need to dynamically allocate one */
2694 		deferfcp = kmalloc(sizeof(*deferfcp), GFP_KERNEL);
2695 		if (!deferfcp) {
2696 			/* release the queue lookup reference */
2697 			nvmet_fc_tgt_q_put(queue);
2698 			return -ENOMEM;
2699 		}
2700 		spin_lock_irqsave(&queue->qlock, flags);
2701 	}
2702 
2703 	/* For now, use rspaddr / rsplen to save payload information */
2704 	fcpreq->rspaddr = cmdiubuf;
2705 	fcpreq->rsplen  = cmdiubuf_len;
2706 	deferfcp->fcp_req = fcpreq;
2707 
2708 	/* defer processing till a fod becomes available */
2709 	list_add_tail(&deferfcp->req_list, &queue->pending_cmd_list);
2710 
2711 	/* NOTE: the queue lookup reference is still valid */
2712 
2713 	spin_unlock_irqrestore(&queue->qlock, flags);
2714 
2715 	return -EOVERFLOW;
2716 }
2717 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_fcp_req);
2718 
2719 /**
2720  * nvmet_fc_rcv_fcp_abort - transport entry point called by an LLDD
2721  *                       upon the reception of an ABTS for a FCP command
2722  *
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).
2727  *
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
2735  * called.
2736  *
2737  * @target_port: pointer to the (registered) target port the FCP CMD IU
2738  *              was received on.
2739  * @fcpreq:     pointer to the fcpreq request structure that corresponds
2740  *              to the exchange that received the ABTS.
2741  */
2742 void
2743 nvmet_fc_rcv_fcp_abort(struct nvmet_fc_target_port *target_port,
2744 			struct nvmefc_tgt_fcp_req *fcpreq)
2745 {
2746 	struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
2747 	struct nvmet_fc_tgt_queue *queue;
2748 	unsigned long flags;
2749 
2750 	if (!fod || fod->fcpreq != fcpreq)
2751 		/* job appears to have already completed, ignore abort */
2752 		return;
2753 
2754 	queue = fod->queue;
2755 
2756 	spin_lock_irqsave(&queue->qlock, flags);
2757 	if (fod->active) {
2758 		/*
2759 		 * mark as abort. The abort handler, invoked upon completion
2760 		 * of any work, will detect the aborted status and do the
2761 		 * callback.
2762 		 */
2763 		spin_lock(&fod->flock);
2764 		fod->abort = true;
2765 		fod->aborted = true;
2766 		spin_unlock(&fod->flock);
2767 	}
2768 	spin_unlock_irqrestore(&queue->qlock, flags);
2769 }
2770 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_fcp_abort);
2771 
2772 
2773 struct nvmet_fc_traddr {
2774 	u64	nn;
2775 	u64	pn;
2776 };
2777 
2778 static int
2779 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
2780 {
2781 	u64 token64;
2782 
2783 	if (match_u64(sstr, &token64))
2784 		return -EINVAL;
2785 	*val = token64;
2786 
2787 	return 0;
2788 }
2789 
2790 /*
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.
2794  */
2795 static int
2796 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
2797 {
2798 	char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
2799 	substring_t wwn = { name, &name[sizeof(name)-1] };
2800 	int nnoffset, pnoffset;
2801 
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;
2816 	} else
2817 		goto out_einval;
2818 
2819 	name[0] = '0';
2820 	name[1] = 'x';
2821 	name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
2822 
2823 	memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
2824 	if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
2825 		goto out_einval;
2826 
2827 	memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
2828 	if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
2829 		goto out_einval;
2830 
2831 	return 0;
2832 
2833 out_einval:
2834 	pr_warn("%s: bad traddr string\n", __func__);
2835 	return -EINVAL;
2836 }
2837 
2838 static int
2839 nvmet_fc_add_port(struct nvmet_port *port)
2840 {
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;
2845 	int ret;
2846 
2847 	/* validate the address info */
2848 	if ((port->disc_addr.trtype != NVMF_TRTYPE_FC) ||
2849 	    (port->disc_addr.adrfam != NVMF_ADDR_FAMILY_FC))
2850 		return -EINVAL;
2851 
2852 	/* map the traddr address info to a target port */
2853 
2854 	ret = nvme_fc_parse_traddr(&traddr, port->disc_addr.traddr,
2855 			sizeof(port->disc_addr.traddr));
2856 	if (ret)
2857 		return ret;
2858 
2859 	pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2860 	if (!pe)
2861 		return -ENOMEM;
2862 
2863 	ret = -ENXIO;
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 */
2869 			if (!tgtport->pe) {
2870 				nvmet_fc_portentry_bind(tgtport, pe, port);
2871 				ret = 0;
2872 			} else
2873 				ret = -EALREADY;
2874 			break;
2875 		}
2876 	}
2877 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
2878 
2879 	if (ret)
2880 		kfree(pe);
2881 
2882 	return ret;
2883 }
2884 
2885 static void
2886 nvmet_fc_remove_port(struct nvmet_port *port)
2887 {
2888 	struct nvmet_fc_port_entry *pe = port->priv;
2889 
2890 	nvmet_fc_portentry_unbind(pe);
2891 
2892 	kfree(pe);
2893 }
2894 
2895 static void
2896 nvmet_fc_discovery_chg(struct nvmet_port *port)
2897 {
2898 	struct nvmet_fc_port_entry *pe = port->priv;
2899 	struct nvmet_fc_tgtport *tgtport = pe->tgtport;
2900 
2901 	if (tgtport && tgtport->ops->discovery_event)
2902 		tgtport->ops->discovery_event(&tgtport->fc_target_port);
2903 }
2904 
2905 static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = {
2906 	.owner			= THIS_MODULE,
2907 	.type			= NVMF_TRTYPE_FC,
2908 	.msdbd			= 1,
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,
2914 };
2915 
2916 static int __init nvmet_fc_init_module(void)
2917 {
2918 	return nvmet_register_transport(&nvmet_fc_tgt_fcp_ops);
2919 }
2920 
2921 static void __exit nvmet_fc_exit_module(void)
2922 {
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__);
2926 
2927 	nvmet_unregister_transport(&nvmet_fc_tgt_fcp_ops);
2928 
2929 	ida_destroy(&nvmet_fc_tgtport_cnt);
2930 }
2931 
2932 module_init(nvmet_fc_init_module);
2933 module_exit(nvmet_fc_exit_module);
2934 
2935 MODULE_LICENSE("GPL v2");
2936