• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Avago Technologies.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful.
9  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
10  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
11  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
12  * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
13  * See the GNU General Public License for more details, a copy of which
14  * can be found in the file COPYING included with this package
15  *
16  */
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/blk-mq.h>
21 #include <linux/parser.h>
22 #include <linux/random.h>
23 #include <uapi/scsi/fc/fc_fs.h>
24 #include <uapi/scsi/fc/fc_els.h>
25 
26 #include "nvmet.h"
27 #include <linux/nvme-fc-driver.h>
28 #include <linux/nvme-fc.h>
29 
30 
31 /* *************************** Data Structures/Defines ****************** */
32 
33 
34 #define NVMET_LS_CTX_COUNT		4
35 
36 /* for this implementation, assume small single frame rqst/rsp */
37 #define NVME_FC_MAX_LS_BUFFER_SIZE		2048
38 
39 struct nvmet_fc_tgtport;
40 struct nvmet_fc_tgt_assoc;
41 
42 struct nvmet_fc_ls_iod {
43 	struct nvmefc_tgt_ls_req	*lsreq;
44 	struct nvmefc_tgt_fcp_req	*fcpreq;	/* only if RS */
45 
46 	struct list_head		ls_list;	/* tgtport->ls_list */
47 
48 	struct nvmet_fc_tgtport		*tgtport;
49 	struct nvmet_fc_tgt_assoc	*assoc;
50 
51 	u8				*rqstbuf;
52 	u8				*rspbuf;
53 	u16				rqstdatalen;
54 	dma_addr_t			rspdma;
55 
56 	struct scatterlist		sg[2];
57 
58 	struct work_struct		work;
59 } __aligned(sizeof(unsigned long long));
60 
61 /* desired maximum for a single sequence - if sg list allows it */
62 #define NVMET_FC_MAX_SEQ_LENGTH		(256 * 1024)
63 
64 enum nvmet_fcp_datadir {
65 	NVMET_FCP_NODATA,
66 	NVMET_FCP_WRITE,
67 	NVMET_FCP_READ,
68 	NVMET_FCP_ABORTED,
69 };
70 
71 struct nvmet_fc_fcp_iod {
72 	struct nvmefc_tgt_fcp_req	*fcpreq;
73 
74 	struct nvme_fc_cmd_iu		cmdiubuf;
75 	struct nvme_fc_ersp_iu		rspiubuf;
76 	dma_addr_t			rspdma;
77 	struct scatterlist		*next_sg;
78 	struct scatterlist		*data_sg;
79 	int				data_sg_cnt;
80 	u32				total_length;
81 	u32				offset;
82 	enum nvmet_fcp_datadir		io_dir;
83 	bool				active;
84 	bool				abort;
85 	bool				aborted;
86 	bool				writedataactive;
87 	spinlock_t			flock;
88 
89 	struct nvmet_req		req;
90 	struct work_struct		work;
91 	struct work_struct		done_work;
92 
93 	struct nvmet_fc_tgtport		*tgtport;
94 	struct nvmet_fc_tgt_queue	*queue;
95 
96 	struct list_head		fcp_list;	/* tgtport->fcp_list */
97 };
98 
99 struct nvmet_fc_tgtport {
100 
101 	struct nvmet_fc_target_port	fc_target_port;
102 
103 	struct list_head		tgt_list; /* nvmet_fc_target_list */
104 	struct device			*dev;	/* dev for dma mapping */
105 	struct nvmet_fc_target_template	*ops;
106 
107 	struct nvmet_fc_ls_iod		*iod;
108 	spinlock_t			lock;
109 	struct list_head		ls_list;
110 	struct list_head		ls_busylist;
111 	struct list_head		assoc_list;
112 	struct ida			assoc_cnt;
113 	struct nvmet_port		*port;
114 	struct kref			ref;
115 	u32				max_sg_cnt;
116 };
117 
118 struct nvmet_fc_defer_fcp_req {
119 	struct list_head		req_list;
120 	struct nvmefc_tgt_fcp_req	*fcp_req;
121 };
122 
123 struct nvmet_fc_tgt_queue {
124 	bool				ninetypercent;
125 	u16				qid;
126 	u16				sqsize;
127 	u16				ersp_ratio;
128 	__le16				sqhd;
129 	int				cpu;
130 	atomic_t			connected;
131 	atomic_t			sqtail;
132 	atomic_t			zrspcnt;
133 	atomic_t			rsn;
134 	spinlock_t			qlock;
135 	struct nvmet_port		*port;
136 	struct nvmet_cq			nvme_cq;
137 	struct nvmet_sq			nvme_sq;
138 	struct nvmet_fc_tgt_assoc	*assoc;
139 	struct nvmet_fc_fcp_iod		*fod;		/* array of fcp_iods */
140 	struct list_head		fod_list;
141 	struct list_head		pending_cmd_list;
142 	struct list_head		avail_defer_list;
143 	struct workqueue_struct		*work_q;
144 	struct kref			ref;
145 } __aligned(sizeof(unsigned long long));
146 
147 struct nvmet_fc_tgt_assoc {
148 	u64				association_id;
149 	u32				a_id;
150 	struct nvmet_fc_tgtport		*tgtport;
151 	struct list_head		a_list;
152 	struct nvmet_fc_tgt_queue	*queues[NVMET_NR_QUEUES + 1];
153 	struct kref			ref;
154 };
155 
156 
157 static inline int
nvmet_fc_iodnum(struct nvmet_fc_ls_iod * iodptr)158 nvmet_fc_iodnum(struct nvmet_fc_ls_iod *iodptr)
159 {
160 	return (iodptr - iodptr->tgtport->iod);
161 }
162 
163 static inline int
nvmet_fc_fodnum(struct nvmet_fc_fcp_iod * fodptr)164 nvmet_fc_fodnum(struct nvmet_fc_fcp_iod *fodptr)
165 {
166 	return (fodptr - fodptr->queue->fod);
167 }
168 
169 
170 /*
171  * Association and Connection IDs:
172  *
173  * Association ID will have random number in upper 6 bytes and zero
174  *   in lower 2 bytes
175  *
176  * Connection IDs will be Association ID with QID or'd in lower 2 bytes
177  *
178  * note: Association ID = Connection ID for queue 0
179  */
180 #define BYTES_FOR_QID			sizeof(u16)
181 #define BYTES_FOR_QID_SHIFT		(BYTES_FOR_QID * 8)
182 #define NVMET_FC_QUEUEID_MASK		((u64)((1 << BYTES_FOR_QID_SHIFT) - 1))
183 
184 static inline u64
nvmet_fc_makeconnid(struct nvmet_fc_tgt_assoc * assoc,u16 qid)185 nvmet_fc_makeconnid(struct nvmet_fc_tgt_assoc *assoc, u16 qid)
186 {
187 	return (assoc->association_id | qid);
188 }
189 
190 static inline u64
nvmet_fc_getassociationid(u64 connectionid)191 nvmet_fc_getassociationid(u64 connectionid)
192 {
193 	return connectionid & ~NVMET_FC_QUEUEID_MASK;
194 }
195 
196 static inline u16
nvmet_fc_getqueueid(u64 connectionid)197 nvmet_fc_getqueueid(u64 connectionid)
198 {
199 	return (u16)(connectionid & NVMET_FC_QUEUEID_MASK);
200 }
201 
202 static inline struct nvmet_fc_tgtport *
targetport_to_tgtport(struct nvmet_fc_target_port * targetport)203 targetport_to_tgtport(struct nvmet_fc_target_port *targetport)
204 {
205 	return container_of(targetport, struct nvmet_fc_tgtport,
206 				 fc_target_port);
207 }
208 
209 static inline struct nvmet_fc_fcp_iod *
nvmet_req_to_fod(struct nvmet_req * nvme_req)210 nvmet_req_to_fod(struct nvmet_req *nvme_req)
211 {
212 	return container_of(nvme_req, struct nvmet_fc_fcp_iod, req);
213 }
214 
215 
216 /* *************************** Globals **************************** */
217 
218 
219 static DEFINE_SPINLOCK(nvmet_fc_tgtlock);
220 
221 static LIST_HEAD(nvmet_fc_target_list);
222 static DEFINE_IDA(nvmet_fc_tgtport_cnt);
223 
224 
225 static void nvmet_fc_handle_ls_rqst_work(struct work_struct *work);
226 static void nvmet_fc_handle_fcp_rqst_work(struct work_struct *work);
227 static void nvmet_fc_fcp_rqst_op_done_work(struct work_struct *work);
228 static void nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc);
229 static int nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc);
230 static void nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue);
231 static int nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue);
232 static void nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport);
233 static int nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport);
234 static void nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
235 					struct nvmet_fc_fcp_iod *fod);
236 
237 
238 /* *********************** FC-NVME DMA Handling **************************** */
239 
240 /*
241  * The fcloop device passes in a NULL device pointer. Real LLD's will
242  * pass in a valid device pointer. If NULL is passed to the dma mapping
243  * routines, depending on the platform, it may or may not succeed, and
244  * may crash.
245  *
246  * As such:
247  * Wrapper all the dma routines and check the dev pointer.
248  *
249  * If simple mappings (return just a dma address, we'll noop them,
250  * returning a dma address of 0.
251  *
252  * On more complex mappings (dma_map_sg), a pseudo routine fills
253  * in the scatter list, setting all dma addresses to 0.
254  */
255 
256 static inline dma_addr_t
fc_dma_map_single(struct device * dev,void * ptr,size_t size,enum dma_data_direction dir)257 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
258 		enum dma_data_direction dir)
259 {
260 	return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
261 }
262 
263 static inline int
fc_dma_mapping_error(struct device * dev,dma_addr_t dma_addr)264 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
265 {
266 	return dev ? dma_mapping_error(dev, dma_addr) : 0;
267 }
268 
269 static inline void
fc_dma_unmap_single(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)270 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
271 	enum dma_data_direction dir)
272 {
273 	if (dev)
274 		dma_unmap_single(dev, addr, size, dir);
275 }
276 
277 static inline void
fc_dma_sync_single_for_cpu(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)278 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
279 		enum dma_data_direction dir)
280 {
281 	if (dev)
282 		dma_sync_single_for_cpu(dev, addr, size, dir);
283 }
284 
285 static inline void
fc_dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)286 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
287 		enum dma_data_direction dir)
288 {
289 	if (dev)
290 		dma_sync_single_for_device(dev, addr, size, dir);
291 }
292 
293 /* pseudo dma_map_sg call */
294 static int
fc_map_sg(struct scatterlist * sg,int nents)295 fc_map_sg(struct scatterlist *sg, int nents)
296 {
297 	struct scatterlist *s;
298 	int i;
299 
300 	WARN_ON(nents == 0 || sg[0].length == 0);
301 
302 	for_each_sg(sg, s, nents, i) {
303 		s->dma_address = 0L;
304 #ifdef CONFIG_NEED_SG_DMA_LENGTH
305 		s->dma_length = s->length;
306 #endif
307 	}
308 	return nents;
309 }
310 
311 static inline int
fc_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)312 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
313 		enum dma_data_direction dir)
314 {
315 	return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
316 }
317 
318 static inline void
fc_dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)319 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
320 		enum dma_data_direction dir)
321 {
322 	if (dev)
323 		dma_unmap_sg(dev, sg, nents, dir);
324 }
325 
326 
327 /* *********************** FC-NVME Port Management ************************ */
328 
329 
330 static int
nvmet_fc_alloc_ls_iodlist(struct nvmet_fc_tgtport * tgtport)331 nvmet_fc_alloc_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
332 {
333 	struct nvmet_fc_ls_iod *iod;
334 	int i;
335 
336 	iod = kcalloc(NVMET_LS_CTX_COUNT, sizeof(struct nvmet_fc_ls_iod),
337 			GFP_KERNEL);
338 	if (!iod)
339 		return -ENOMEM;
340 
341 	tgtport->iod = iod;
342 
343 	for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
344 		INIT_WORK(&iod->work, nvmet_fc_handle_ls_rqst_work);
345 		iod->tgtport = tgtport;
346 		list_add_tail(&iod->ls_list, &tgtport->ls_list);
347 
348 		iod->rqstbuf = kcalloc(2, NVME_FC_MAX_LS_BUFFER_SIZE,
349 			GFP_KERNEL);
350 		if (!iod->rqstbuf)
351 			goto out_fail;
352 
353 		iod->rspbuf = iod->rqstbuf + NVME_FC_MAX_LS_BUFFER_SIZE;
354 
355 		iod->rspdma = fc_dma_map_single(tgtport->dev, iod->rspbuf,
356 						NVME_FC_MAX_LS_BUFFER_SIZE,
357 						DMA_TO_DEVICE);
358 		if (fc_dma_mapping_error(tgtport->dev, iod->rspdma))
359 			goto out_fail;
360 	}
361 
362 	return 0;
363 
364 out_fail:
365 	kfree(iod->rqstbuf);
366 	list_del(&iod->ls_list);
367 	for (iod--, i--; i >= 0; iod--, i--) {
368 		fc_dma_unmap_single(tgtport->dev, iod->rspdma,
369 				NVME_FC_MAX_LS_BUFFER_SIZE, DMA_TO_DEVICE);
370 		kfree(iod->rqstbuf);
371 		list_del(&iod->ls_list);
372 	}
373 
374 	kfree(iod);
375 
376 	return -EFAULT;
377 }
378 
379 static void
nvmet_fc_free_ls_iodlist(struct nvmet_fc_tgtport * tgtport)380 nvmet_fc_free_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
381 {
382 	struct nvmet_fc_ls_iod *iod = tgtport->iod;
383 	int i;
384 
385 	for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
386 		fc_dma_unmap_single(tgtport->dev,
387 				iod->rspdma, NVME_FC_MAX_LS_BUFFER_SIZE,
388 				DMA_TO_DEVICE);
389 		kfree(iod->rqstbuf);
390 		list_del(&iod->ls_list);
391 	}
392 	kfree(tgtport->iod);
393 }
394 
395 static struct nvmet_fc_ls_iod *
nvmet_fc_alloc_ls_iod(struct nvmet_fc_tgtport * tgtport)396 nvmet_fc_alloc_ls_iod(struct nvmet_fc_tgtport *tgtport)
397 {
398 	struct nvmet_fc_ls_iod *iod;
399 	unsigned long flags;
400 
401 	spin_lock_irqsave(&tgtport->lock, flags);
402 	iod = list_first_entry_or_null(&tgtport->ls_list,
403 					struct nvmet_fc_ls_iod, ls_list);
404 	if (iod)
405 		list_move_tail(&iod->ls_list, &tgtport->ls_busylist);
406 	spin_unlock_irqrestore(&tgtport->lock, flags);
407 	return iod;
408 }
409 
410 
411 static void
nvmet_fc_free_ls_iod(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_ls_iod * iod)412 nvmet_fc_free_ls_iod(struct nvmet_fc_tgtport *tgtport,
413 			struct nvmet_fc_ls_iod *iod)
414 {
415 	unsigned long flags;
416 
417 	spin_lock_irqsave(&tgtport->lock, flags);
418 	list_move(&iod->ls_list, &tgtport->ls_list);
419 	spin_unlock_irqrestore(&tgtport->lock, flags);
420 }
421 
422 static void
nvmet_fc_prep_fcp_iodlist(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_tgt_queue * queue)423 nvmet_fc_prep_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
424 				struct nvmet_fc_tgt_queue *queue)
425 {
426 	struct nvmet_fc_fcp_iod *fod = queue->fod;
427 	int i;
428 
429 	for (i = 0; i < queue->sqsize; fod++, i++) {
430 		INIT_WORK(&fod->work, nvmet_fc_handle_fcp_rqst_work);
431 		INIT_WORK(&fod->done_work, nvmet_fc_fcp_rqst_op_done_work);
432 		fod->tgtport = tgtport;
433 		fod->queue = queue;
434 		fod->active = false;
435 		fod->abort = false;
436 		fod->aborted = false;
437 		fod->fcpreq = NULL;
438 		list_add_tail(&fod->fcp_list, &queue->fod_list);
439 		spin_lock_init(&fod->flock);
440 
441 		fod->rspdma = fc_dma_map_single(tgtport->dev, &fod->rspiubuf,
442 					sizeof(fod->rspiubuf), DMA_TO_DEVICE);
443 		if (fc_dma_mapping_error(tgtport->dev, fod->rspdma)) {
444 			list_del(&fod->fcp_list);
445 			for (fod--, i--; i >= 0; fod--, i--) {
446 				fc_dma_unmap_single(tgtport->dev, fod->rspdma,
447 						sizeof(fod->rspiubuf),
448 						DMA_TO_DEVICE);
449 				fod->rspdma = 0L;
450 				list_del(&fod->fcp_list);
451 			}
452 
453 			return;
454 		}
455 	}
456 }
457 
458 static void
nvmet_fc_destroy_fcp_iodlist(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_tgt_queue * queue)459 nvmet_fc_destroy_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
460 				struct nvmet_fc_tgt_queue *queue)
461 {
462 	struct nvmet_fc_fcp_iod *fod = queue->fod;
463 	int i;
464 
465 	for (i = 0; i < queue->sqsize; fod++, i++) {
466 		if (fod->rspdma)
467 			fc_dma_unmap_single(tgtport->dev, fod->rspdma,
468 				sizeof(fod->rspiubuf), DMA_TO_DEVICE);
469 	}
470 }
471 
472 static struct nvmet_fc_fcp_iod *
nvmet_fc_alloc_fcp_iod(struct nvmet_fc_tgt_queue * queue)473 nvmet_fc_alloc_fcp_iod(struct nvmet_fc_tgt_queue *queue)
474 {
475 	struct nvmet_fc_fcp_iod *fod;
476 
477 	lockdep_assert_held(&queue->qlock);
478 
479 	fod = list_first_entry_or_null(&queue->fod_list,
480 					struct nvmet_fc_fcp_iod, fcp_list);
481 	if (fod) {
482 		list_del(&fod->fcp_list);
483 		fod->active = true;
484 		/*
485 		 * no queue reference is taken, as it was taken by the
486 		 * queue lookup just prior to the allocation. The iod
487 		 * will "inherit" that reference.
488 		 */
489 	}
490 	return fod;
491 }
492 
493 
494 static void
nvmet_fc_queue_fcp_req(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_tgt_queue * queue,struct nvmefc_tgt_fcp_req * fcpreq)495 nvmet_fc_queue_fcp_req(struct nvmet_fc_tgtport *tgtport,
496 		       struct nvmet_fc_tgt_queue *queue,
497 		       struct nvmefc_tgt_fcp_req *fcpreq)
498 {
499 	struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
500 
501 	/*
502 	 * put all admin cmds on hw queue id 0. All io commands go to
503 	 * the respective hw queue based on a modulo basis
504 	 */
505 	fcpreq->hwqid = queue->qid ?
506 			((queue->qid - 1) % tgtport->ops->max_hw_queues) : 0;
507 
508 	if (tgtport->ops->target_features & NVMET_FCTGTFEAT_CMD_IN_ISR)
509 		queue_work_on(queue->cpu, queue->work_q, &fod->work);
510 	else
511 		nvmet_fc_handle_fcp_rqst(tgtport, fod);
512 }
513 
514 static void
nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue * queue,struct nvmet_fc_fcp_iod * fod)515 nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
516 			struct nvmet_fc_fcp_iod *fod)
517 {
518 	struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
519 	struct nvmet_fc_tgtport *tgtport = fod->tgtport;
520 	struct nvmet_fc_defer_fcp_req *deferfcp;
521 	unsigned long flags;
522 
523 	fc_dma_sync_single_for_cpu(tgtport->dev, fod->rspdma,
524 				sizeof(fod->rspiubuf), DMA_TO_DEVICE);
525 
526 	fcpreq->nvmet_fc_private = NULL;
527 
528 	fod->active = false;
529 	fod->abort = false;
530 	fod->aborted = false;
531 	fod->writedataactive = false;
532 	fod->fcpreq = NULL;
533 
534 	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
535 
536 	/* release the queue lookup reference on the completed IO */
537 	nvmet_fc_tgt_q_put(queue);
538 
539 	spin_lock_irqsave(&queue->qlock, flags);
540 	deferfcp = list_first_entry_or_null(&queue->pending_cmd_list,
541 				struct nvmet_fc_defer_fcp_req, req_list);
542 	if (!deferfcp) {
543 		list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
544 		spin_unlock_irqrestore(&queue->qlock, flags);
545 		return;
546 	}
547 
548 	/* Re-use the fod for the next pending cmd that was deferred */
549 	list_del(&deferfcp->req_list);
550 
551 	fcpreq = deferfcp->fcp_req;
552 
553 	/* deferfcp can be reused for another IO at a later date */
554 	list_add_tail(&deferfcp->req_list, &queue->avail_defer_list);
555 
556 	spin_unlock_irqrestore(&queue->qlock, flags);
557 
558 	/* Save NVME CMD IO in fod */
559 	memcpy(&fod->cmdiubuf, fcpreq->rspaddr, fcpreq->rsplen);
560 
561 	/* Setup new fcpreq to be processed */
562 	fcpreq->rspaddr = NULL;
563 	fcpreq->rsplen  = 0;
564 	fcpreq->nvmet_fc_private = fod;
565 	fod->fcpreq = fcpreq;
566 	fod->active = true;
567 
568 	/* inform LLDD IO is now being processed */
569 	tgtport->ops->defer_rcv(&tgtport->fc_target_port, fcpreq);
570 
571 	/* Submit deferred IO for processing */
572 	nvmet_fc_queue_fcp_req(tgtport, queue, fcpreq);
573 
574 	/*
575 	 * Leave the queue lookup get reference taken when
576 	 * fod was originally allocated.
577 	 */
578 }
579 
580 static int
nvmet_fc_queue_to_cpu(struct nvmet_fc_tgtport * tgtport,int qid)581 nvmet_fc_queue_to_cpu(struct nvmet_fc_tgtport *tgtport, int qid)
582 {
583 	int cpu, idx, cnt;
584 
585 	if (tgtport->ops->max_hw_queues == 1)
586 		return WORK_CPU_UNBOUND;
587 
588 	/* Simple cpu selection based on qid modulo active cpu count */
589 	idx = !qid ? 0 : (qid - 1) % num_active_cpus();
590 
591 	/* find the n'th active cpu */
592 	for (cpu = 0, cnt = 0; ; ) {
593 		if (cpu_active(cpu)) {
594 			if (cnt == idx)
595 				break;
596 			cnt++;
597 		}
598 		cpu = (cpu + 1) % num_possible_cpus();
599 	}
600 
601 	return cpu;
602 }
603 
604 static struct nvmet_fc_tgt_queue *
nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc * assoc,u16 qid,u16 sqsize)605 nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
606 			u16 qid, u16 sqsize)
607 {
608 	struct nvmet_fc_tgt_queue *queue;
609 	unsigned long flags;
610 	int ret;
611 
612 	if (qid > NVMET_NR_QUEUES)
613 		return NULL;
614 
615 	queue = kzalloc((sizeof(*queue) +
616 				(sizeof(struct nvmet_fc_fcp_iod) * sqsize)),
617 				GFP_KERNEL);
618 	if (!queue)
619 		return NULL;
620 
621 	if (!nvmet_fc_tgt_a_get(assoc))
622 		goto out_free_queue;
623 
624 	queue->work_q = alloc_workqueue("ntfc%d.%d.%d", 0, 0,
625 				assoc->tgtport->fc_target_port.port_num,
626 				assoc->a_id, qid);
627 	if (!queue->work_q)
628 		goto out_a_put;
629 
630 	queue->fod = (struct nvmet_fc_fcp_iod *)&queue[1];
631 	queue->qid = qid;
632 	queue->sqsize = sqsize;
633 	queue->assoc = assoc;
634 	queue->port = assoc->tgtport->port;
635 	queue->cpu = nvmet_fc_queue_to_cpu(assoc->tgtport, qid);
636 	INIT_LIST_HEAD(&queue->fod_list);
637 	INIT_LIST_HEAD(&queue->avail_defer_list);
638 	INIT_LIST_HEAD(&queue->pending_cmd_list);
639 	atomic_set(&queue->connected, 0);
640 	atomic_set(&queue->sqtail, 0);
641 	atomic_set(&queue->rsn, 1);
642 	atomic_set(&queue->zrspcnt, 0);
643 	spin_lock_init(&queue->qlock);
644 	kref_init(&queue->ref);
645 
646 	nvmet_fc_prep_fcp_iodlist(assoc->tgtport, queue);
647 
648 	ret = nvmet_sq_init(&queue->nvme_sq);
649 	if (ret)
650 		goto out_fail_iodlist;
651 
652 	WARN_ON(assoc->queues[qid]);
653 	spin_lock_irqsave(&assoc->tgtport->lock, flags);
654 	assoc->queues[qid] = queue;
655 	spin_unlock_irqrestore(&assoc->tgtport->lock, flags);
656 
657 	return queue;
658 
659 out_fail_iodlist:
660 	nvmet_fc_destroy_fcp_iodlist(assoc->tgtport, queue);
661 	destroy_workqueue(queue->work_q);
662 out_a_put:
663 	nvmet_fc_tgt_a_put(assoc);
664 out_free_queue:
665 	kfree(queue);
666 	return NULL;
667 }
668 
669 
670 static void
nvmet_fc_tgt_queue_free(struct kref * ref)671 nvmet_fc_tgt_queue_free(struct kref *ref)
672 {
673 	struct nvmet_fc_tgt_queue *queue =
674 		container_of(ref, struct nvmet_fc_tgt_queue, ref);
675 	unsigned long flags;
676 
677 	spin_lock_irqsave(&queue->assoc->tgtport->lock, flags);
678 	queue->assoc->queues[queue->qid] = NULL;
679 	spin_unlock_irqrestore(&queue->assoc->tgtport->lock, flags);
680 
681 	nvmet_fc_destroy_fcp_iodlist(queue->assoc->tgtport, queue);
682 
683 	nvmet_fc_tgt_a_put(queue->assoc);
684 
685 	destroy_workqueue(queue->work_q);
686 
687 	kfree(queue);
688 }
689 
690 static void
nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue * queue)691 nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue)
692 {
693 	kref_put(&queue->ref, nvmet_fc_tgt_queue_free);
694 }
695 
696 static int
nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue * queue)697 nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue)
698 {
699 	return kref_get_unless_zero(&queue->ref);
700 }
701 
702 
703 static void
nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue * queue)704 nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
705 {
706 	struct nvmet_fc_tgtport *tgtport = queue->assoc->tgtport;
707 	struct nvmet_fc_fcp_iod *fod = queue->fod;
708 	struct nvmet_fc_defer_fcp_req *deferfcp, *tempptr;
709 	unsigned long flags;
710 	int i, writedataactive;
711 	bool disconnect;
712 
713 	disconnect = atomic_xchg(&queue->connected, 0);
714 
715 	spin_lock_irqsave(&queue->qlock, flags);
716 	/* about outstanding io's */
717 	for (i = 0; i < queue->sqsize; fod++, i++) {
718 		if (fod->active) {
719 			spin_lock(&fod->flock);
720 			fod->abort = true;
721 			writedataactive = fod->writedataactive;
722 			spin_unlock(&fod->flock);
723 			/*
724 			 * only call lldd abort routine if waiting for
725 			 * writedata. other outstanding ops should finish
726 			 * on their own.
727 			 */
728 			if (writedataactive) {
729 				spin_lock(&fod->flock);
730 				fod->aborted = true;
731 				spin_unlock(&fod->flock);
732 				tgtport->ops->fcp_abort(
733 					&tgtport->fc_target_port, fod->fcpreq);
734 			}
735 		}
736 	}
737 
738 	/* Cleanup defer'ed IOs in queue */
739 	list_for_each_entry_safe(deferfcp, tempptr, &queue->avail_defer_list,
740 				req_list) {
741 		list_del(&deferfcp->req_list);
742 		kfree(deferfcp);
743 	}
744 
745 	for (;;) {
746 		deferfcp = list_first_entry_or_null(&queue->pending_cmd_list,
747 				struct nvmet_fc_defer_fcp_req, req_list);
748 		if (!deferfcp)
749 			break;
750 
751 		list_del(&deferfcp->req_list);
752 		spin_unlock_irqrestore(&queue->qlock, flags);
753 
754 		tgtport->ops->defer_rcv(&tgtport->fc_target_port,
755 				deferfcp->fcp_req);
756 
757 		tgtport->ops->fcp_abort(&tgtport->fc_target_port,
758 				deferfcp->fcp_req);
759 
760 		tgtport->ops->fcp_req_release(&tgtport->fc_target_port,
761 				deferfcp->fcp_req);
762 
763 		/* release the queue lookup reference */
764 		nvmet_fc_tgt_q_put(queue);
765 
766 		kfree(deferfcp);
767 
768 		spin_lock_irqsave(&queue->qlock, flags);
769 	}
770 	spin_unlock_irqrestore(&queue->qlock, flags);
771 
772 	flush_workqueue(queue->work_q);
773 
774 	if (disconnect)
775 		nvmet_sq_destroy(&queue->nvme_sq);
776 
777 	nvmet_fc_tgt_q_put(queue);
778 }
779 
780 static struct nvmet_fc_tgt_queue *
nvmet_fc_find_target_queue(struct nvmet_fc_tgtport * tgtport,u64 connection_id)781 nvmet_fc_find_target_queue(struct nvmet_fc_tgtport *tgtport,
782 				u64 connection_id)
783 {
784 	struct nvmet_fc_tgt_assoc *assoc;
785 	struct nvmet_fc_tgt_queue *queue;
786 	u64 association_id = nvmet_fc_getassociationid(connection_id);
787 	u16 qid = nvmet_fc_getqueueid(connection_id);
788 	unsigned long flags;
789 
790 	if (qid > NVMET_NR_QUEUES)
791 		return NULL;
792 
793 	spin_lock_irqsave(&tgtport->lock, flags);
794 	list_for_each_entry(assoc, &tgtport->assoc_list, a_list) {
795 		if (association_id == assoc->association_id) {
796 			queue = assoc->queues[qid];
797 			if (queue &&
798 			    (!atomic_read(&queue->connected) ||
799 			     !nvmet_fc_tgt_q_get(queue)))
800 				queue = NULL;
801 			spin_unlock_irqrestore(&tgtport->lock, flags);
802 			return queue;
803 		}
804 	}
805 	spin_unlock_irqrestore(&tgtport->lock, flags);
806 	return NULL;
807 }
808 
809 static struct nvmet_fc_tgt_assoc *
nvmet_fc_alloc_target_assoc(struct nvmet_fc_tgtport * tgtport)810 nvmet_fc_alloc_target_assoc(struct nvmet_fc_tgtport *tgtport)
811 {
812 	struct nvmet_fc_tgt_assoc *assoc, *tmpassoc;
813 	unsigned long flags;
814 	u64 ran;
815 	int idx;
816 	bool needrandom = true;
817 
818 	assoc = kzalloc(sizeof(*assoc), GFP_KERNEL);
819 	if (!assoc)
820 		return NULL;
821 
822 	idx = ida_simple_get(&tgtport->assoc_cnt, 0, 0, GFP_KERNEL);
823 	if (idx < 0)
824 		goto out_free_assoc;
825 
826 	if (!nvmet_fc_tgtport_get(tgtport))
827 		goto out_ida_put;
828 
829 	assoc->tgtport = tgtport;
830 	assoc->a_id = idx;
831 	INIT_LIST_HEAD(&assoc->a_list);
832 	kref_init(&assoc->ref);
833 
834 	while (needrandom) {
835 		get_random_bytes(&ran, sizeof(ran) - BYTES_FOR_QID);
836 		ran = ran << BYTES_FOR_QID_SHIFT;
837 
838 		spin_lock_irqsave(&tgtport->lock, flags);
839 		needrandom = false;
840 		list_for_each_entry(tmpassoc, &tgtport->assoc_list, a_list)
841 			if (ran == tmpassoc->association_id) {
842 				needrandom = true;
843 				break;
844 			}
845 		if (!needrandom) {
846 			assoc->association_id = ran;
847 			list_add_tail(&assoc->a_list, &tgtport->assoc_list);
848 		}
849 		spin_unlock_irqrestore(&tgtport->lock, flags);
850 	}
851 
852 	return assoc;
853 
854 out_ida_put:
855 	ida_simple_remove(&tgtport->assoc_cnt, idx);
856 out_free_assoc:
857 	kfree(assoc);
858 	return NULL;
859 }
860 
861 static void
nvmet_fc_target_assoc_free(struct kref * ref)862 nvmet_fc_target_assoc_free(struct kref *ref)
863 {
864 	struct nvmet_fc_tgt_assoc *assoc =
865 		container_of(ref, struct nvmet_fc_tgt_assoc, ref);
866 	struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
867 	unsigned long flags;
868 
869 	spin_lock_irqsave(&tgtport->lock, flags);
870 	list_del(&assoc->a_list);
871 	spin_unlock_irqrestore(&tgtport->lock, flags);
872 	ida_simple_remove(&tgtport->assoc_cnt, assoc->a_id);
873 	kfree(assoc);
874 	nvmet_fc_tgtport_put(tgtport);
875 }
876 
877 static void
nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc * assoc)878 nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc)
879 {
880 	kref_put(&assoc->ref, nvmet_fc_target_assoc_free);
881 }
882 
883 static int
nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc * assoc)884 nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc)
885 {
886 	return kref_get_unless_zero(&assoc->ref);
887 }
888 
889 static void
nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc * assoc)890 nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc *assoc)
891 {
892 	struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
893 	struct nvmet_fc_tgt_queue *queue;
894 	unsigned long flags;
895 	int i;
896 
897 	spin_lock_irqsave(&tgtport->lock, flags);
898 	for (i = NVMET_NR_QUEUES; i >= 0; i--) {
899 		queue = assoc->queues[i];
900 		if (queue) {
901 			if (!nvmet_fc_tgt_q_get(queue))
902 				continue;
903 			spin_unlock_irqrestore(&tgtport->lock, flags);
904 			nvmet_fc_delete_target_queue(queue);
905 			nvmet_fc_tgt_q_put(queue);
906 			spin_lock_irqsave(&tgtport->lock, flags);
907 		}
908 	}
909 	spin_unlock_irqrestore(&tgtport->lock, flags);
910 
911 	nvmet_fc_tgt_a_put(assoc);
912 }
913 
914 static struct nvmet_fc_tgt_assoc *
nvmet_fc_find_target_assoc(struct nvmet_fc_tgtport * tgtport,u64 association_id)915 nvmet_fc_find_target_assoc(struct nvmet_fc_tgtport *tgtport,
916 				u64 association_id)
917 {
918 	struct nvmet_fc_tgt_assoc *assoc;
919 	struct nvmet_fc_tgt_assoc *ret = NULL;
920 	unsigned long flags;
921 
922 	spin_lock_irqsave(&tgtport->lock, flags);
923 	list_for_each_entry(assoc, &tgtport->assoc_list, a_list) {
924 		if (association_id == assoc->association_id) {
925 			ret = assoc;
926 			nvmet_fc_tgt_a_get(assoc);
927 			break;
928 		}
929 	}
930 	spin_unlock_irqrestore(&tgtport->lock, flags);
931 
932 	return ret;
933 }
934 
935 
936 /**
937  * nvme_fc_register_targetport - transport entry point called by an
938  *                              LLDD to register the existence of a local
939  *                              NVME subystem FC port.
940  * @pinfo:     pointer to information about the port to be registered
941  * @template:  LLDD entrypoints and operational parameters for the port
942  * @dev:       physical hardware device node port corresponds to. Will be
943  *             used for DMA mappings
944  * @portptr:   pointer to a local port pointer. Upon success, the routine
945  *             will allocate a nvme_fc_local_port structure and place its
946  *             address in the local port pointer. Upon failure, local port
947  *             pointer will be set to NULL.
948  *
949  * Returns:
950  * a completion status. Must be 0 upon success; a negative errno
951  * (ex: -ENXIO) upon failure.
952  */
953 int
nvmet_fc_register_targetport(struct nvmet_fc_port_info * pinfo,struct nvmet_fc_target_template * template,struct device * dev,struct nvmet_fc_target_port ** portptr)954 nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
955 			struct nvmet_fc_target_template *template,
956 			struct device *dev,
957 			struct nvmet_fc_target_port **portptr)
958 {
959 	struct nvmet_fc_tgtport *newrec;
960 	unsigned long flags;
961 	int ret, idx;
962 
963 	if (!template->xmt_ls_rsp || !template->fcp_op ||
964 	    !template->fcp_abort ||
965 	    !template->fcp_req_release || !template->targetport_delete ||
966 	    !template->max_hw_queues || !template->max_sgl_segments ||
967 	    !template->max_dif_sgl_segments || !template->dma_boundary) {
968 		ret = -EINVAL;
969 		goto out_regtgt_failed;
970 	}
971 
972 	newrec = kzalloc((sizeof(*newrec) + template->target_priv_sz),
973 			 GFP_KERNEL);
974 	if (!newrec) {
975 		ret = -ENOMEM;
976 		goto out_regtgt_failed;
977 	}
978 
979 	idx = ida_simple_get(&nvmet_fc_tgtport_cnt, 0, 0, GFP_KERNEL);
980 	if (idx < 0) {
981 		ret = -ENOSPC;
982 		goto out_fail_kfree;
983 	}
984 
985 	if (!get_device(dev) && dev) {
986 		ret = -ENODEV;
987 		goto out_ida_put;
988 	}
989 
990 	newrec->fc_target_port.node_name = pinfo->node_name;
991 	newrec->fc_target_port.port_name = pinfo->port_name;
992 	newrec->fc_target_port.private = &newrec[1];
993 	newrec->fc_target_port.port_id = pinfo->port_id;
994 	newrec->fc_target_port.port_num = idx;
995 	INIT_LIST_HEAD(&newrec->tgt_list);
996 	newrec->dev = dev;
997 	newrec->ops = template;
998 	spin_lock_init(&newrec->lock);
999 	INIT_LIST_HEAD(&newrec->ls_list);
1000 	INIT_LIST_HEAD(&newrec->ls_busylist);
1001 	INIT_LIST_HEAD(&newrec->assoc_list);
1002 	kref_init(&newrec->ref);
1003 	ida_init(&newrec->assoc_cnt);
1004 	newrec->max_sg_cnt = template->max_sgl_segments;
1005 
1006 	ret = nvmet_fc_alloc_ls_iodlist(newrec);
1007 	if (ret) {
1008 		ret = -ENOMEM;
1009 		goto out_free_newrec;
1010 	}
1011 
1012 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1013 	list_add_tail(&newrec->tgt_list, &nvmet_fc_target_list);
1014 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1015 
1016 	*portptr = &newrec->fc_target_port;
1017 	return 0;
1018 
1019 out_free_newrec:
1020 	put_device(dev);
1021 out_ida_put:
1022 	ida_simple_remove(&nvmet_fc_tgtport_cnt, idx);
1023 out_fail_kfree:
1024 	kfree(newrec);
1025 out_regtgt_failed:
1026 	*portptr = NULL;
1027 	return ret;
1028 }
1029 EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
1030 
1031 
1032 static void
nvmet_fc_free_tgtport(struct kref * ref)1033 nvmet_fc_free_tgtport(struct kref *ref)
1034 {
1035 	struct nvmet_fc_tgtport *tgtport =
1036 		container_of(ref, struct nvmet_fc_tgtport, ref);
1037 	struct device *dev = tgtport->dev;
1038 	unsigned long flags;
1039 
1040 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1041 	list_del(&tgtport->tgt_list);
1042 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1043 
1044 	nvmet_fc_free_ls_iodlist(tgtport);
1045 
1046 	/* let the LLDD know we've finished tearing it down */
1047 	tgtport->ops->targetport_delete(&tgtport->fc_target_port);
1048 
1049 	ida_simple_remove(&nvmet_fc_tgtport_cnt,
1050 			tgtport->fc_target_port.port_num);
1051 
1052 	ida_destroy(&tgtport->assoc_cnt);
1053 
1054 	kfree(tgtport);
1055 
1056 	put_device(dev);
1057 }
1058 
1059 static void
nvmet_fc_tgtport_put(struct nvmet_fc_tgtport * tgtport)1060 nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport)
1061 {
1062 	kref_put(&tgtport->ref, nvmet_fc_free_tgtport);
1063 }
1064 
1065 static int
nvmet_fc_tgtport_get(struct nvmet_fc_tgtport * tgtport)1066 nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport)
1067 {
1068 	return kref_get_unless_zero(&tgtport->ref);
1069 }
1070 
1071 static void
__nvmet_fc_free_assocs(struct nvmet_fc_tgtport * tgtport)1072 __nvmet_fc_free_assocs(struct nvmet_fc_tgtport *tgtport)
1073 {
1074 	struct nvmet_fc_tgt_assoc *assoc, *next;
1075 	unsigned long flags;
1076 
1077 	spin_lock_irqsave(&tgtport->lock, flags);
1078 	list_for_each_entry_safe(assoc, next,
1079 				&tgtport->assoc_list, a_list) {
1080 		if (!nvmet_fc_tgt_a_get(assoc))
1081 			continue;
1082 		spin_unlock_irqrestore(&tgtport->lock, flags);
1083 		nvmet_fc_delete_target_assoc(assoc);
1084 		nvmet_fc_tgt_a_put(assoc);
1085 		spin_lock_irqsave(&tgtport->lock, flags);
1086 	}
1087 	spin_unlock_irqrestore(&tgtport->lock, flags);
1088 }
1089 
1090 /*
1091  * nvmet layer has called to terminate an association
1092  */
1093 static void
nvmet_fc_delete_ctrl(struct nvmet_ctrl * ctrl)1094 nvmet_fc_delete_ctrl(struct nvmet_ctrl *ctrl)
1095 {
1096 	struct nvmet_fc_tgtport *tgtport, *next;
1097 	struct nvmet_fc_tgt_assoc *assoc;
1098 	struct nvmet_fc_tgt_queue *queue;
1099 	unsigned long flags;
1100 	bool found_ctrl = false;
1101 
1102 	/* this is a bit ugly, but don't want to make locks layered */
1103 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1104 	list_for_each_entry_safe(tgtport, next, &nvmet_fc_target_list,
1105 			tgt_list) {
1106 		if (!nvmet_fc_tgtport_get(tgtport))
1107 			continue;
1108 		spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1109 
1110 		spin_lock_irqsave(&tgtport->lock, flags);
1111 		list_for_each_entry(assoc, &tgtport->assoc_list, a_list) {
1112 			queue = assoc->queues[0];
1113 			if (queue && queue->nvme_sq.ctrl == ctrl) {
1114 				if (nvmet_fc_tgt_a_get(assoc))
1115 					found_ctrl = true;
1116 				break;
1117 			}
1118 		}
1119 		spin_unlock_irqrestore(&tgtport->lock, flags);
1120 
1121 		nvmet_fc_tgtport_put(tgtport);
1122 
1123 		if (found_ctrl) {
1124 			nvmet_fc_delete_target_assoc(assoc);
1125 			nvmet_fc_tgt_a_put(assoc);
1126 			return;
1127 		}
1128 
1129 		spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1130 	}
1131 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1132 }
1133 
1134 /**
1135  * nvme_fc_unregister_targetport - transport entry point called by an
1136  *                              LLDD to deregister/remove a previously
1137  *                              registered a local NVME subsystem FC port.
1138  * @tgtport: pointer to the (registered) target port that is to be
1139  *           deregistered.
1140  *
1141  * Returns:
1142  * a completion status. Must be 0 upon success; a negative errno
1143  * (ex: -ENXIO) upon failure.
1144  */
1145 int
nvmet_fc_unregister_targetport(struct nvmet_fc_target_port * target_port)1146 nvmet_fc_unregister_targetport(struct nvmet_fc_target_port *target_port)
1147 {
1148 	struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
1149 
1150 	/* terminate any outstanding associations */
1151 	__nvmet_fc_free_assocs(tgtport);
1152 
1153 	nvmet_fc_tgtport_put(tgtport);
1154 
1155 	return 0;
1156 }
1157 EXPORT_SYMBOL_GPL(nvmet_fc_unregister_targetport);
1158 
1159 
1160 /* *********************** FC-NVME LS Handling **************************** */
1161 
1162 
1163 static void
nvmet_fc_format_rsp_hdr(void * buf,u8 ls_cmd,__be32 desc_len,u8 rqst_ls_cmd)1164 nvmet_fc_format_rsp_hdr(void *buf, u8 ls_cmd, __be32 desc_len, u8 rqst_ls_cmd)
1165 {
1166 	struct fcnvme_ls_acc_hdr *acc = buf;
1167 
1168 	acc->w0.ls_cmd = ls_cmd;
1169 	acc->desc_list_len = desc_len;
1170 	acc->rqst.desc_tag = cpu_to_be32(FCNVME_LSDESC_RQST);
1171 	acc->rqst.desc_len =
1172 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst));
1173 	acc->rqst.w0.ls_cmd = rqst_ls_cmd;
1174 }
1175 
1176 static int
nvmet_fc_format_rjt(void * buf,u16 buflen,u8 ls_cmd,u8 reason,u8 explanation,u8 vendor)1177 nvmet_fc_format_rjt(void *buf, u16 buflen, u8 ls_cmd,
1178 			u8 reason, u8 explanation, u8 vendor)
1179 {
1180 	struct fcnvme_ls_rjt *rjt = buf;
1181 
1182 	nvmet_fc_format_rsp_hdr(buf, FCNVME_LSDESC_RQST,
1183 			fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_rjt)),
1184 			ls_cmd);
1185 	rjt->rjt.desc_tag = cpu_to_be32(FCNVME_LSDESC_RJT);
1186 	rjt->rjt.desc_len = fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rjt));
1187 	rjt->rjt.reason_code = reason;
1188 	rjt->rjt.reason_explanation = explanation;
1189 	rjt->rjt.vendor = vendor;
1190 
1191 	return sizeof(struct fcnvme_ls_rjt);
1192 }
1193 
1194 /* Validation Error indexes into the string table below */
1195 enum {
1196 	VERR_NO_ERROR		= 0,
1197 	VERR_CR_ASSOC_LEN	= 1,
1198 	VERR_CR_ASSOC_RQST_LEN	= 2,
1199 	VERR_CR_ASSOC_CMD	= 3,
1200 	VERR_CR_ASSOC_CMD_LEN	= 4,
1201 	VERR_ERSP_RATIO		= 5,
1202 	VERR_ASSOC_ALLOC_FAIL	= 6,
1203 	VERR_QUEUE_ALLOC_FAIL	= 7,
1204 	VERR_CR_CONN_LEN	= 8,
1205 	VERR_CR_CONN_RQST_LEN	= 9,
1206 	VERR_ASSOC_ID		= 10,
1207 	VERR_ASSOC_ID_LEN	= 11,
1208 	VERR_NO_ASSOC		= 12,
1209 	VERR_CONN_ID		= 13,
1210 	VERR_CONN_ID_LEN	= 14,
1211 	VERR_NO_CONN		= 15,
1212 	VERR_CR_CONN_CMD	= 16,
1213 	VERR_CR_CONN_CMD_LEN	= 17,
1214 	VERR_DISCONN_LEN	= 18,
1215 	VERR_DISCONN_RQST_LEN	= 19,
1216 	VERR_DISCONN_CMD	= 20,
1217 	VERR_DISCONN_CMD_LEN	= 21,
1218 	VERR_DISCONN_SCOPE	= 22,
1219 	VERR_RS_LEN		= 23,
1220 	VERR_RS_RQST_LEN	= 24,
1221 	VERR_RS_CMD		= 25,
1222 	VERR_RS_CMD_LEN		= 26,
1223 	VERR_RS_RCTL		= 27,
1224 	VERR_RS_RO		= 28,
1225 };
1226 
1227 static char *validation_errors[] = {
1228 	"OK",
1229 	"Bad CR_ASSOC Length",
1230 	"Bad CR_ASSOC Rqst Length",
1231 	"Not CR_ASSOC Cmd",
1232 	"Bad CR_ASSOC Cmd Length",
1233 	"Bad Ersp Ratio",
1234 	"Association Allocation Failed",
1235 	"Queue Allocation Failed",
1236 	"Bad CR_CONN Length",
1237 	"Bad CR_CONN Rqst Length",
1238 	"Not Association ID",
1239 	"Bad Association ID Length",
1240 	"No Association",
1241 	"Not Connection ID",
1242 	"Bad Connection ID Length",
1243 	"No Connection",
1244 	"Not CR_CONN Cmd",
1245 	"Bad CR_CONN Cmd Length",
1246 	"Bad DISCONN Length",
1247 	"Bad DISCONN Rqst Length",
1248 	"Not DISCONN Cmd",
1249 	"Bad DISCONN Cmd Length",
1250 	"Bad Disconnect Scope",
1251 	"Bad RS Length",
1252 	"Bad RS Rqst Length",
1253 	"Not RS Cmd",
1254 	"Bad RS Cmd Length",
1255 	"Bad RS R_CTL",
1256 	"Bad RS Relative Offset",
1257 };
1258 
1259 static void
nvmet_fc_ls_create_association(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_ls_iod * iod)1260 nvmet_fc_ls_create_association(struct nvmet_fc_tgtport *tgtport,
1261 			struct nvmet_fc_ls_iod *iod)
1262 {
1263 	struct fcnvme_ls_cr_assoc_rqst *rqst =
1264 				(struct fcnvme_ls_cr_assoc_rqst *)iod->rqstbuf;
1265 	struct fcnvme_ls_cr_assoc_acc *acc =
1266 				(struct fcnvme_ls_cr_assoc_acc *)iod->rspbuf;
1267 	struct nvmet_fc_tgt_queue *queue;
1268 	int ret = 0;
1269 
1270 	memset(acc, 0, sizeof(*acc));
1271 
1272 	/*
1273 	 * FC-NVME spec changes. There are initiators sending different
1274 	 * lengths as padding sizes for Create Association Cmd descriptor
1275 	 * was incorrect.
1276 	 * Accept anything of "minimum" length. Assume format per 1.15
1277 	 * spec (with HOSTID reduced to 16 bytes), ignore how long the
1278 	 * trailing pad length is.
1279 	 */
1280 	if (iod->rqstdatalen < FCNVME_LSDESC_CRA_RQST_MINLEN)
1281 		ret = VERR_CR_ASSOC_LEN;
1282 	else if (be32_to_cpu(rqst->desc_list_len) <
1283 			FCNVME_LSDESC_CRA_RQST_MIN_LISTLEN)
1284 		ret = VERR_CR_ASSOC_RQST_LEN;
1285 	else if (rqst->assoc_cmd.desc_tag !=
1286 			cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD))
1287 		ret = VERR_CR_ASSOC_CMD;
1288 	else if (be32_to_cpu(rqst->assoc_cmd.desc_len) <
1289 			FCNVME_LSDESC_CRA_CMD_DESC_MIN_DESCLEN)
1290 		ret = VERR_CR_ASSOC_CMD_LEN;
1291 	else if (!rqst->assoc_cmd.ersp_ratio ||
1292 		 (be16_to_cpu(rqst->assoc_cmd.ersp_ratio) >=
1293 				be16_to_cpu(rqst->assoc_cmd.sqsize)))
1294 		ret = VERR_ERSP_RATIO;
1295 
1296 	else {
1297 		/* new association w/ admin queue */
1298 		iod->assoc = nvmet_fc_alloc_target_assoc(tgtport);
1299 		if (!iod->assoc)
1300 			ret = VERR_ASSOC_ALLOC_FAIL;
1301 		else {
1302 			queue = nvmet_fc_alloc_target_queue(iod->assoc, 0,
1303 					be16_to_cpu(rqst->assoc_cmd.sqsize));
1304 			if (!queue)
1305 				ret = VERR_QUEUE_ALLOC_FAIL;
1306 		}
1307 	}
1308 
1309 	if (ret) {
1310 		dev_err(tgtport->dev,
1311 			"Create Association LS failed: %s\n",
1312 			validation_errors[ret]);
1313 		iod->lsreq->rsplen = nvmet_fc_format_rjt(acc,
1314 				NVME_FC_MAX_LS_BUFFER_SIZE, rqst->w0.ls_cmd,
1315 				FCNVME_RJT_RC_LOGIC,
1316 				FCNVME_RJT_EXP_NONE, 0);
1317 		return;
1318 	}
1319 
1320 	queue->ersp_ratio = be16_to_cpu(rqst->assoc_cmd.ersp_ratio);
1321 	atomic_set(&queue->connected, 1);
1322 	queue->sqhd = 0;	/* best place to init value */
1323 
1324 	/* format a response */
1325 
1326 	iod->lsreq->rsplen = sizeof(*acc);
1327 
1328 	nvmet_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1329 			fcnvme_lsdesc_len(
1330 				sizeof(struct fcnvme_ls_cr_assoc_acc)),
1331 			FCNVME_LS_CREATE_ASSOCIATION);
1332 	acc->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1333 	acc->associd.desc_len =
1334 			fcnvme_lsdesc_len(
1335 				sizeof(struct fcnvme_lsdesc_assoc_id));
1336 	acc->associd.association_id =
1337 			cpu_to_be64(nvmet_fc_makeconnid(iod->assoc, 0));
1338 	acc->connectid.desc_tag = cpu_to_be32(FCNVME_LSDESC_CONN_ID);
1339 	acc->connectid.desc_len =
1340 			fcnvme_lsdesc_len(
1341 				sizeof(struct fcnvme_lsdesc_conn_id));
1342 	acc->connectid.connection_id = acc->associd.association_id;
1343 }
1344 
1345 static void
nvmet_fc_ls_create_connection(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_ls_iod * iod)1346 nvmet_fc_ls_create_connection(struct nvmet_fc_tgtport *tgtport,
1347 			struct nvmet_fc_ls_iod *iod)
1348 {
1349 	struct fcnvme_ls_cr_conn_rqst *rqst =
1350 				(struct fcnvme_ls_cr_conn_rqst *)iod->rqstbuf;
1351 	struct fcnvme_ls_cr_conn_acc *acc =
1352 				(struct fcnvme_ls_cr_conn_acc *)iod->rspbuf;
1353 	struct nvmet_fc_tgt_queue *queue;
1354 	int ret = 0;
1355 
1356 	memset(acc, 0, sizeof(*acc));
1357 
1358 	if (iod->rqstdatalen < sizeof(struct fcnvme_ls_cr_conn_rqst))
1359 		ret = VERR_CR_CONN_LEN;
1360 	else if (rqst->desc_list_len !=
1361 			fcnvme_lsdesc_len(
1362 				sizeof(struct fcnvme_ls_cr_conn_rqst)))
1363 		ret = VERR_CR_CONN_RQST_LEN;
1364 	else if (rqst->associd.desc_tag != cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1365 		ret = VERR_ASSOC_ID;
1366 	else if (rqst->associd.desc_len !=
1367 			fcnvme_lsdesc_len(
1368 				sizeof(struct fcnvme_lsdesc_assoc_id)))
1369 		ret = VERR_ASSOC_ID_LEN;
1370 	else if (rqst->connect_cmd.desc_tag !=
1371 			cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD))
1372 		ret = VERR_CR_CONN_CMD;
1373 	else if (rqst->connect_cmd.desc_len !=
1374 			fcnvme_lsdesc_len(
1375 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd)))
1376 		ret = VERR_CR_CONN_CMD_LEN;
1377 	else if (!rqst->connect_cmd.ersp_ratio ||
1378 		 (be16_to_cpu(rqst->connect_cmd.ersp_ratio) >=
1379 				be16_to_cpu(rqst->connect_cmd.sqsize)))
1380 		ret = VERR_ERSP_RATIO;
1381 
1382 	else {
1383 		/* new io queue */
1384 		iod->assoc = nvmet_fc_find_target_assoc(tgtport,
1385 				be64_to_cpu(rqst->associd.association_id));
1386 		if (!iod->assoc)
1387 			ret = VERR_NO_ASSOC;
1388 		else {
1389 			queue = nvmet_fc_alloc_target_queue(iod->assoc,
1390 					be16_to_cpu(rqst->connect_cmd.qid),
1391 					be16_to_cpu(rqst->connect_cmd.sqsize));
1392 			if (!queue)
1393 				ret = VERR_QUEUE_ALLOC_FAIL;
1394 
1395 			/* release get taken in nvmet_fc_find_target_assoc */
1396 			nvmet_fc_tgt_a_put(iod->assoc);
1397 		}
1398 	}
1399 
1400 	if (ret) {
1401 		dev_err(tgtport->dev,
1402 			"Create Connection LS failed: %s\n",
1403 			validation_errors[ret]);
1404 		iod->lsreq->rsplen = nvmet_fc_format_rjt(acc,
1405 				NVME_FC_MAX_LS_BUFFER_SIZE, rqst->w0.ls_cmd,
1406 				(ret == VERR_NO_ASSOC) ?
1407 					FCNVME_RJT_RC_INV_ASSOC :
1408 					FCNVME_RJT_RC_LOGIC,
1409 				FCNVME_RJT_EXP_NONE, 0);
1410 		return;
1411 	}
1412 
1413 	queue->ersp_ratio = be16_to_cpu(rqst->connect_cmd.ersp_ratio);
1414 	atomic_set(&queue->connected, 1);
1415 	queue->sqhd = 0;	/* best place to init value */
1416 
1417 	/* format a response */
1418 
1419 	iod->lsreq->rsplen = sizeof(*acc);
1420 
1421 	nvmet_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1422 			fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)),
1423 			FCNVME_LS_CREATE_CONNECTION);
1424 	acc->connectid.desc_tag = cpu_to_be32(FCNVME_LSDESC_CONN_ID);
1425 	acc->connectid.desc_len =
1426 			fcnvme_lsdesc_len(
1427 				sizeof(struct fcnvme_lsdesc_conn_id));
1428 	acc->connectid.connection_id =
1429 			cpu_to_be64(nvmet_fc_makeconnid(iod->assoc,
1430 				be16_to_cpu(rqst->connect_cmd.qid)));
1431 }
1432 
1433 static void
nvmet_fc_ls_disconnect(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_ls_iod * iod)1434 nvmet_fc_ls_disconnect(struct nvmet_fc_tgtport *tgtport,
1435 			struct nvmet_fc_ls_iod *iod)
1436 {
1437 	struct fcnvme_ls_disconnect_rqst *rqst =
1438 			(struct fcnvme_ls_disconnect_rqst *)iod->rqstbuf;
1439 	struct fcnvme_ls_disconnect_acc *acc =
1440 			(struct fcnvme_ls_disconnect_acc *)iod->rspbuf;
1441 	struct nvmet_fc_tgt_queue *queue = NULL;
1442 	struct nvmet_fc_tgt_assoc *assoc;
1443 	int ret = 0;
1444 	bool del_assoc = false;
1445 
1446 	memset(acc, 0, sizeof(*acc));
1447 
1448 	if (iod->rqstdatalen < sizeof(struct fcnvme_ls_disconnect_rqst))
1449 		ret = VERR_DISCONN_LEN;
1450 	else if (rqst->desc_list_len !=
1451 			fcnvme_lsdesc_len(
1452 				sizeof(struct fcnvme_ls_disconnect_rqst)))
1453 		ret = VERR_DISCONN_RQST_LEN;
1454 	else if (rqst->associd.desc_tag != cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1455 		ret = VERR_ASSOC_ID;
1456 	else if (rqst->associd.desc_len !=
1457 			fcnvme_lsdesc_len(
1458 				sizeof(struct fcnvme_lsdesc_assoc_id)))
1459 		ret = VERR_ASSOC_ID_LEN;
1460 	else if (rqst->discon_cmd.desc_tag !=
1461 			cpu_to_be32(FCNVME_LSDESC_DISCONN_CMD))
1462 		ret = VERR_DISCONN_CMD;
1463 	else if (rqst->discon_cmd.desc_len !=
1464 			fcnvme_lsdesc_len(
1465 				sizeof(struct fcnvme_lsdesc_disconn_cmd)))
1466 		ret = VERR_DISCONN_CMD_LEN;
1467 	else if ((rqst->discon_cmd.scope != FCNVME_DISCONN_ASSOCIATION) &&
1468 			(rqst->discon_cmd.scope != FCNVME_DISCONN_CONNECTION))
1469 		ret = VERR_DISCONN_SCOPE;
1470 	else {
1471 		/* match an active association */
1472 		assoc = nvmet_fc_find_target_assoc(tgtport,
1473 				be64_to_cpu(rqst->associd.association_id));
1474 		iod->assoc = assoc;
1475 		if (assoc) {
1476 			if (rqst->discon_cmd.scope ==
1477 					FCNVME_DISCONN_CONNECTION) {
1478 				queue = nvmet_fc_find_target_queue(tgtport,
1479 						be64_to_cpu(
1480 							rqst->discon_cmd.id));
1481 				if (!queue) {
1482 					nvmet_fc_tgt_a_put(assoc);
1483 					ret = VERR_NO_CONN;
1484 				}
1485 			}
1486 		} else
1487 			ret = VERR_NO_ASSOC;
1488 	}
1489 
1490 	if (ret) {
1491 		dev_err(tgtport->dev,
1492 			"Disconnect LS failed: %s\n",
1493 			validation_errors[ret]);
1494 		iod->lsreq->rsplen = nvmet_fc_format_rjt(acc,
1495 				NVME_FC_MAX_LS_BUFFER_SIZE, rqst->w0.ls_cmd,
1496 				(ret == VERR_NO_ASSOC) ?
1497 					FCNVME_RJT_RC_INV_ASSOC :
1498 					(ret == VERR_NO_CONN) ?
1499 						FCNVME_RJT_RC_INV_CONN :
1500 						FCNVME_RJT_RC_LOGIC,
1501 				FCNVME_RJT_EXP_NONE, 0);
1502 		return;
1503 	}
1504 
1505 	/* format a response */
1506 
1507 	iod->lsreq->rsplen = sizeof(*acc);
1508 
1509 	nvmet_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1510 			fcnvme_lsdesc_len(
1511 				sizeof(struct fcnvme_ls_disconnect_acc)),
1512 			FCNVME_LS_DISCONNECT);
1513 
1514 
1515 	/* are we to delete a Connection ID (queue) */
1516 	if (queue) {
1517 		int qid = queue->qid;
1518 
1519 		nvmet_fc_delete_target_queue(queue);
1520 
1521 		/* release the get taken by find_target_queue */
1522 		nvmet_fc_tgt_q_put(queue);
1523 
1524 		/* tear association down if io queue terminated */
1525 		if (!qid)
1526 			del_assoc = true;
1527 	}
1528 
1529 	/* release get taken in nvmet_fc_find_target_assoc */
1530 	nvmet_fc_tgt_a_put(iod->assoc);
1531 
1532 	if (del_assoc)
1533 		nvmet_fc_delete_target_assoc(iod->assoc);
1534 }
1535 
1536 
1537 /* *********************** NVME Ctrl Routines **************************** */
1538 
1539 
1540 static void nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req);
1541 
1542 static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops;
1543 
1544 static void
nvmet_fc_xmt_ls_rsp_done(struct nvmefc_tgt_ls_req * lsreq)1545 nvmet_fc_xmt_ls_rsp_done(struct nvmefc_tgt_ls_req *lsreq)
1546 {
1547 	struct nvmet_fc_ls_iod *iod = lsreq->nvmet_fc_private;
1548 	struct nvmet_fc_tgtport *tgtport = iod->tgtport;
1549 
1550 	fc_dma_sync_single_for_cpu(tgtport->dev, iod->rspdma,
1551 				NVME_FC_MAX_LS_BUFFER_SIZE, DMA_TO_DEVICE);
1552 	nvmet_fc_free_ls_iod(tgtport, iod);
1553 	nvmet_fc_tgtport_put(tgtport);
1554 }
1555 
1556 static void
nvmet_fc_xmt_ls_rsp(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_ls_iod * iod)1557 nvmet_fc_xmt_ls_rsp(struct nvmet_fc_tgtport *tgtport,
1558 				struct nvmet_fc_ls_iod *iod)
1559 {
1560 	int ret;
1561 
1562 	fc_dma_sync_single_for_device(tgtport->dev, iod->rspdma,
1563 				  NVME_FC_MAX_LS_BUFFER_SIZE, DMA_TO_DEVICE);
1564 
1565 	ret = tgtport->ops->xmt_ls_rsp(&tgtport->fc_target_port, iod->lsreq);
1566 	if (ret)
1567 		nvmet_fc_xmt_ls_rsp_done(iod->lsreq);
1568 }
1569 
1570 /*
1571  * Actual processing routine for received FC-NVME LS Requests from the LLD
1572  */
1573 static void
nvmet_fc_handle_ls_rqst(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_ls_iod * iod)1574 nvmet_fc_handle_ls_rqst(struct nvmet_fc_tgtport *tgtport,
1575 			struct nvmet_fc_ls_iod *iod)
1576 {
1577 	struct fcnvme_ls_rqst_w0 *w0 =
1578 			(struct fcnvme_ls_rqst_w0 *)iod->rqstbuf;
1579 
1580 	iod->lsreq->nvmet_fc_private = iod;
1581 	iod->lsreq->rspbuf = iod->rspbuf;
1582 	iod->lsreq->rspdma = iod->rspdma;
1583 	iod->lsreq->done = nvmet_fc_xmt_ls_rsp_done;
1584 	/* Be preventative. handlers will later set to valid length */
1585 	iod->lsreq->rsplen = 0;
1586 
1587 	iod->assoc = NULL;
1588 
1589 	/*
1590 	 * handlers:
1591 	 *   parse request input, execute the request, and format the
1592 	 *   LS response
1593 	 */
1594 	switch (w0->ls_cmd) {
1595 	case FCNVME_LS_CREATE_ASSOCIATION:
1596 		/* Creates Association and initial Admin Queue/Connection */
1597 		nvmet_fc_ls_create_association(tgtport, iod);
1598 		break;
1599 	case FCNVME_LS_CREATE_CONNECTION:
1600 		/* Creates an IO Queue/Connection */
1601 		nvmet_fc_ls_create_connection(tgtport, iod);
1602 		break;
1603 	case FCNVME_LS_DISCONNECT:
1604 		/* Terminate a Queue/Connection or the Association */
1605 		nvmet_fc_ls_disconnect(tgtport, iod);
1606 		break;
1607 	default:
1608 		iod->lsreq->rsplen = nvmet_fc_format_rjt(iod->rspbuf,
1609 				NVME_FC_MAX_LS_BUFFER_SIZE, w0->ls_cmd,
1610 				FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
1611 	}
1612 
1613 	nvmet_fc_xmt_ls_rsp(tgtport, iod);
1614 }
1615 
1616 /*
1617  * Actual processing routine for received FC-NVME LS Requests from the LLD
1618  */
1619 static void
nvmet_fc_handle_ls_rqst_work(struct work_struct * work)1620 nvmet_fc_handle_ls_rqst_work(struct work_struct *work)
1621 {
1622 	struct nvmet_fc_ls_iod *iod =
1623 		container_of(work, struct nvmet_fc_ls_iod, work);
1624 	struct nvmet_fc_tgtport *tgtport = iod->tgtport;
1625 
1626 	nvmet_fc_handle_ls_rqst(tgtport, iod);
1627 }
1628 
1629 
1630 /**
1631  * nvmet_fc_rcv_ls_req - transport entry point called by an LLDD
1632  *                       upon the reception of a NVME LS request.
1633  *
1634  * The nvmet-fc layer will copy payload to an internal structure for
1635  * processing.  As such, upon completion of the routine, the LLDD may
1636  * immediately free/reuse the LS request buffer passed in the call.
1637  *
1638  * If this routine returns error, the LLDD should abort the exchange.
1639  *
1640  * @tgtport:    pointer to the (registered) target port the LS was
1641  *              received on.
1642  * @lsreq:      pointer to a lsreq request structure to be used to reference
1643  *              the exchange corresponding to the LS.
1644  * @lsreqbuf:   pointer to the buffer containing the LS Request
1645  * @lsreqbuf_len: length, in bytes, of the received LS request
1646  */
1647 int
nvmet_fc_rcv_ls_req(struct nvmet_fc_target_port * target_port,struct nvmefc_tgt_ls_req * lsreq,void * lsreqbuf,u32 lsreqbuf_len)1648 nvmet_fc_rcv_ls_req(struct nvmet_fc_target_port *target_port,
1649 			struct nvmefc_tgt_ls_req *lsreq,
1650 			void *lsreqbuf, u32 lsreqbuf_len)
1651 {
1652 	struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
1653 	struct nvmet_fc_ls_iod *iod;
1654 
1655 	if (lsreqbuf_len > NVME_FC_MAX_LS_BUFFER_SIZE)
1656 		return -E2BIG;
1657 
1658 	if (!nvmet_fc_tgtport_get(tgtport))
1659 		return -ESHUTDOWN;
1660 
1661 	iod = nvmet_fc_alloc_ls_iod(tgtport);
1662 	if (!iod) {
1663 		nvmet_fc_tgtport_put(tgtport);
1664 		return -ENOENT;
1665 	}
1666 
1667 	iod->lsreq = lsreq;
1668 	iod->fcpreq = NULL;
1669 	memcpy(iod->rqstbuf, lsreqbuf, lsreqbuf_len);
1670 	iod->rqstdatalen = lsreqbuf_len;
1671 
1672 	schedule_work(&iod->work);
1673 
1674 	return 0;
1675 }
1676 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_ls_req);
1677 
1678 
1679 /*
1680  * **********************
1681  * Start of FCP handling
1682  * **********************
1683  */
1684 
1685 static int
nvmet_fc_alloc_tgt_pgs(struct nvmet_fc_fcp_iod * fod)1686 nvmet_fc_alloc_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
1687 {
1688 	struct scatterlist *sg;
1689 	struct page *page;
1690 	unsigned int nent;
1691 	u32 page_len, length;
1692 	int i = 0;
1693 
1694 	length = fod->total_length;
1695 	nent = DIV_ROUND_UP(length, PAGE_SIZE);
1696 	sg = kmalloc_array(nent, sizeof(struct scatterlist), GFP_KERNEL);
1697 	if (!sg)
1698 		goto out;
1699 
1700 	sg_init_table(sg, nent);
1701 
1702 	while (length) {
1703 		page_len = min_t(u32, length, PAGE_SIZE);
1704 
1705 		page = alloc_page(GFP_KERNEL);
1706 		if (!page)
1707 			goto out_free_pages;
1708 
1709 		sg_set_page(&sg[i], page, page_len, 0);
1710 		length -= page_len;
1711 		i++;
1712 	}
1713 
1714 	fod->data_sg = sg;
1715 	fod->data_sg_cnt = nent;
1716 	fod->data_sg_cnt = fc_dma_map_sg(fod->tgtport->dev, sg, nent,
1717 				((fod->io_dir == NVMET_FCP_WRITE) ?
1718 					DMA_FROM_DEVICE : DMA_TO_DEVICE));
1719 				/* note: write from initiator perspective */
1720 	fod->next_sg = fod->data_sg;
1721 
1722 	return 0;
1723 
1724 out_free_pages:
1725 	while (i > 0) {
1726 		i--;
1727 		__free_page(sg_page(&sg[i]));
1728 	}
1729 	kfree(sg);
1730 	fod->data_sg = NULL;
1731 	fod->data_sg_cnt = 0;
1732 out:
1733 	return NVME_SC_INTERNAL;
1734 }
1735 
1736 static void
nvmet_fc_free_tgt_pgs(struct nvmet_fc_fcp_iod * fod)1737 nvmet_fc_free_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
1738 {
1739 	struct scatterlist *sg;
1740 	int count;
1741 
1742 	if (!fod->data_sg || !fod->data_sg_cnt)
1743 		return;
1744 
1745 	fc_dma_unmap_sg(fod->tgtport->dev, fod->data_sg, fod->data_sg_cnt,
1746 				((fod->io_dir == NVMET_FCP_WRITE) ?
1747 					DMA_FROM_DEVICE : DMA_TO_DEVICE));
1748 	for_each_sg(fod->data_sg, sg, fod->data_sg_cnt, count)
1749 		__free_page(sg_page(sg));
1750 	kfree(fod->data_sg);
1751 	fod->data_sg = NULL;
1752 	fod->data_sg_cnt = 0;
1753 }
1754 
1755 
1756 static bool
queue_90percent_full(struct nvmet_fc_tgt_queue * q,u32 sqhd)1757 queue_90percent_full(struct nvmet_fc_tgt_queue *q, u32 sqhd)
1758 {
1759 	u32 sqtail, used;
1760 
1761 	/* egad, this is ugly. And sqtail is just a best guess */
1762 	sqtail = atomic_read(&q->sqtail) % q->sqsize;
1763 
1764 	used = (sqtail < sqhd) ? (sqtail + q->sqsize - sqhd) : (sqtail - sqhd);
1765 	return ((used * 10) >= (((u32)(q->sqsize - 1) * 9)));
1766 }
1767 
1768 /*
1769  * Prep RSP payload.
1770  * May be a NVMET_FCOP_RSP or NVMET_FCOP_READDATA_RSP op
1771  */
1772 static void
nvmet_fc_prep_fcp_rsp(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_fcp_iod * fod)1773 nvmet_fc_prep_fcp_rsp(struct nvmet_fc_tgtport *tgtport,
1774 				struct nvmet_fc_fcp_iod *fod)
1775 {
1776 	struct nvme_fc_ersp_iu *ersp = &fod->rspiubuf;
1777 	struct nvme_common_command *sqe = &fod->cmdiubuf.sqe.common;
1778 	struct nvme_completion *cqe = &ersp->cqe;
1779 	u32 *cqewd = (u32 *)cqe;
1780 	bool send_ersp = false;
1781 	u32 rsn, rspcnt, xfr_length;
1782 
1783 	if (fod->fcpreq->op == NVMET_FCOP_READDATA_RSP)
1784 		xfr_length = fod->total_length;
1785 	else
1786 		xfr_length = fod->offset;
1787 
1788 	/*
1789 	 * check to see if we can send a 0's rsp.
1790 	 *   Note: to send a 0's response, the NVME-FC host transport will
1791 	 *   recreate the CQE. The host transport knows: sq id, SQHD (last
1792 	 *   seen in an ersp), and command_id. Thus it will create a
1793 	 *   zero-filled CQE with those known fields filled in. Transport
1794 	 *   must send an ersp for any condition where the cqe won't match
1795 	 *   this.
1796 	 *
1797 	 * Here are the FC-NVME mandated cases where we must send an ersp:
1798 	 *  every N responses, where N=ersp_ratio
1799 	 *  force fabric commands to send ersp's (not in FC-NVME but good
1800 	 *    practice)
1801 	 *  normal cmds: any time status is non-zero, or status is zero
1802 	 *     but words 0 or 1 are non-zero.
1803 	 *  the SQ is 90% or more full
1804 	 *  the cmd is a fused command
1805 	 *  transferred data length not equal to cmd iu length
1806 	 */
1807 	rspcnt = atomic_inc_return(&fod->queue->zrspcnt);
1808 	if (!(rspcnt % fod->queue->ersp_ratio) ||
1809 	    sqe->opcode == nvme_fabrics_command ||
1810 	    xfr_length != fod->total_length ||
1811 	    (le16_to_cpu(cqe->status) & 0xFFFE) || cqewd[0] || cqewd[1] ||
1812 	    (sqe->flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND)) ||
1813 	    queue_90percent_full(fod->queue, le16_to_cpu(cqe->sq_head)))
1814 		send_ersp = true;
1815 
1816 	/* re-set the fields */
1817 	fod->fcpreq->rspaddr = ersp;
1818 	fod->fcpreq->rspdma = fod->rspdma;
1819 
1820 	if (!send_ersp) {
1821 		memset(ersp, 0, NVME_FC_SIZEOF_ZEROS_RSP);
1822 		fod->fcpreq->rsplen = NVME_FC_SIZEOF_ZEROS_RSP;
1823 	} else {
1824 		ersp->iu_len = cpu_to_be16(sizeof(*ersp)/sizeof(u32));
1825 		rsn = atomic_inc_return(&fod->queue->rsn);
1826 		ersp->rsn = cpu_to_be32(rsn);
1827 		ersp->xfrd_len = cpu_to_be32(xfr_length);
1828 		fod->fcpreq->rsplen = sizeof(*ersp);
1829 	}
1830 
1831 	fc_dma_sync_single_for_device(tgtport->dev, fod->rspdma,
1832 				  sizeof(fod->rspiubuf), DMA_TO_DEVICE);
1833 }
1834 
1835 static void nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req *fcpreq);
1836 
1837 static void
nvmet_fc_abort_op(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_fcp_iod * fod)1838 nvmet_fc_abort_op(struct nvmet_fc_tgtport *tgtport,
1839 				struct nvmet_fc_fcp_iod *fod)
1840 {
1841 	struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
1842 
1843 	/* data no longer needed */
1844 	nvmet_fc_free_tgt_pgs(fod);
1845 
1846 	/*
1847 	 * if an ABTS was received or we issued the fcp_abort early
1848 	 * don't call abort routine again.
1849 	 */
1850 	/* no need to take lock - lock was taken earlier to get here */
1851 	if (!fod->aborted)
1852 		tgtport->ops->fcp_abort(&tgtport->fc_target_port, fcpreq);
1853 
1854 	nvmet_fc_free_fcp_iod(fod->queue, fod);
1855 }
1856 
1857 static void
nvmet_fc_xmt_fcp_rsp(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_fcp_iod * fod)1858 nvmet_fc_xmt_fcp_rsp(struct nvmet_fc_tgtport *tgtport,
1859 				struct nvmet_fc_fcp_iod *fod)
1860 {
1861 	int ret;
1862 
1863 	fod->fcpreq->op = NVMET_FCOP_RSP;
1864 	fod->fcpreq->timeout = 0;
1865 
1866 	nvmet_fc_prep_fcp_rsp(tgtport, fod);
1867 
1868 	ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fod->fcpreq);
1869 	if (ret)
1870 		nvmet_fc_abort_op(tgtport, fod);
1871 }
1872 
1873 static void
nvmet_fc_transfer_fcp_data(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_fcp_iod * fod,u8 op)1874 nvmet_fc_transfer_fcp_data(struct nvmet_fc_tgtport *tgtport,
1875 				struct nvmet_fc_fcp_iod *fod, u8 op)
1876 {
1877 	struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
1878 	struct scatterlist *sg = fod->next_sg;
1879 	unsigned long flags;
1880 	u32 remaininglen = fod->total_length - fod->offset;
1881 	u32 tlen = 0;
1882 	int ret;
1883 
1884 	fcpreq->op = op;
1885 	fcpreq->offset = fod->offset;
1886 	fcpreq->timeout = NVME_FC_TGTOP_TIMEOUT_SEC;
1887 
1888 	/*
1889 	 * for next sequence:
1890 	 *  break at a sg element boundary
1891 	 *  attempt to keep sequence length capped at
1892 	 *    NVMET_FC_MAX_SEQ_LENGTH but allow sequence to
1893 	 *    be longer if a single sg element is larger
1894 	 *    than that amount. This is done to avoid creating
1895 	 *    a new sg list to use for the tgtport api.
1896 	 */
1897 	fcpreq->sg = sg;
1898 	fcpreq->sg_cnt = 0;
1899 	while (tlen < remaininglen &&
1900 	       fcpreq->sg_cnt < tgtport->max_sg_cnt &&
1901 	       tlen + sg_dma_len(sg) < NVMET_FC_MAX_SEQ_LENGTH) {
1902 		fcpreq->sg_cnt++;
1903 		tlen += sg_dma_len(sg);
1904 		sg = sg_next(sg);
1905 	}
1906 	if (tlen < remaininglen && fcpreq->sg_cnt == 0) {
1907 		fcpreq->sg_cnt++;
1908 		tlen += min_t(u32, sg_dma_len(sg), remaininglen);
1909 		sg = sg_next(sg);
1910 	}
1911 	if (tlen < remaininglen)
1912 		fod->next_sg = sg;
1913 	else
1914 		fod->next_sg = NULL;
1915 
1916 	fcpreq->transfer_length = tlen;
1917 	fcpreq->transferred_length = 0;
1918 	fcpreq->fcp_error = 0;
1919 	fcpreq->rsplen = 0;
1920 
1921 	/*
1922 	 * If the last READDATA request: check if LLDD supports
1923 	 * combined xfr with response.
1924 	 */
1925 	if ((op == NVMET_FCOP_READDATA) &&
1926 	    ((fod->offset + fcpreq->transfer_length) == fod->total_length) &&
1927 	    (tgtport->ops->target_features & NVMET_FCTGTFEAT_READDATA_RSP)) {
1928 		fcpreq->op = NVMET_FCOP_READDATA_RSP;
1929 		nvmet_fc_prep_fcp_rsp(tgtport, fod);
1930 	}
1931 
1932 	ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fod->fcpreq);
1933 	if (ret) {
1934 		/*
1935 		 * should be ok to set w/o lock as its in the thread of
1936 		 * execution (not an async timer routine) and doesn't
1937 		 * contend with any clearing action
1938 		 */
1939 		fod->abort = true;
1940 
1941 		if (op == NVMET_FCOP_WRITEDATA) {
1942 			spin_lock_irqsave(&fod->flock, flags);
1943 			fod->writedataactive = false;
1944 			spin_unlock_irqrestore(&fod->flock, flags);
1945 			nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
1946 		} else /* NVMET_FCOP_READDATA or NVMET_FCOP_READDATA_RSP */ {
1947 			fcpreq->fcp_error = ret;
1948 			fcpreq->transferred_length = 0;
1949 			nvmet_fc_xmt_fcp_op_done(fod->fcpreq);
1950 		}
1951 	}
1952 }
1953 
1954 static inline bool
__nvmet_fc_fod_op_abort(struct nvmet_fc_fcp_iod * fod,bool abort)1955 __nvmet_fc_fod_op_abort(struct nvmet_fc_fcp_iod *fod, bool abort)
1956 {
1957 	struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
1958 	struct nvmet_fc_tgtport *tgtport = fod->tgtport;
1959 
1960 	/* if in the middle of an io and we need to tear down */
1961 	if (abort) {
1962 		if (fcpreq->op == NVMET_FCOP_WRITEDATA) {
1963 			nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
1964 			return true;
1965 		}
1966 
1967 		nvmet_fc_abort_op(tgtport, fod);
1968 		return true;
1969 	}
1970 
1971 	return false;
1972 }
1973 
1974 /*
1975  * actual done handler for FCP operations when completed by the lldd
1976  */
1977 static void
nvmet_fc_fod_op_done(struct nvmet_fc_fcp_iod * fod)1978 nvmet_fc_fod_op_done(struct nvmet_fc_fcp_iod *fod)
1979 {
1980 	struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
1981 	struct nvmet_fc_tgtport *tgtport = fod->tgtport;
1982 	unsigned long flags;
1983 	bool abort;
1984 
1985 	spin_lock_irqsave(&fod->flock, flags);
1986 	abort = fod->abort;
1987 	fod->writedataactive = false;
1988 	spin_unlock_irqrestore(&fod->flock, flags);
1989 
1990 	switch (fcpreq->op) {
1991 
1992 	case NVMET_FCOP_WRITEDATA:
1993 		if (__nvmet_fc_fod_op_abort(fod, abort))
1994 			return;
1995 		if (fcpreq->fcp_error ||
1996 		    fcpreq->transferred_length != fcpreq->transfer_length) {
1997 			spin_lock(&fod->flock);
1998 			fod->abort = true;
1999 			spin_unlock(&fod->flock);
2000 
2001 			nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
2002 			return;
2003 		}
2004 
2005 		fod->offset += fcpreq->transferred_length;
2006 		if (fod->offset != fod->total_length) {
2007 			spin_lock_irqsave(&fod->flock, flags);
2008 			fod->writedataactive = true;
2009 			spin_unlock_irqrestore(&fod->flock, flags);
2010 
2011 			/* transfer the next chunk */
2012 			nvmet_fc_transfer_fcp_data(tgtport, fod,
2013 						NVMET_FCOP_WRITEDATA);
2014 			return;
2015 		}
2016 
2017 		/* data transfer complete, resume with nvmet layer */
2018 
2019 		fod->req.execute(&fod->req);
2020 
2021 		break;
2022 
2023 	case NVMET_FCOP_READDATA:
2024 	case NVMET_FCOP_READDATA_RSP:
2025 		if (__nvmet_fc_fod_op_abort(fod, abort))
2026 			return;
2027 		if (fcpreq->fcp_error ||
2028 		    fcpreq->transferred_length != fcpreq->transfer_length) {
2029 			nvmet_fc_abort_op(tgtport, fod);
2030 			return;
2031 		}
2032 
2033 		/* success */
2034 
2035 		if (fcpreq->op == NVMET_FCOP_READDATA_RSP) {
2036 			/* data no longer needed */
2037 			nvmet_fc_free_tgt_pgs(fod);
2038 			nvmet_fc_free_fcp_iod(fod->queue, fod);
2039 			return;
2040 		}
2041 
2042 		fod->offset += fcpreq->transferred_length;
2043 		if (fod->offset != fod->total_length) {
2044 			/* transfer the next chunk */
2045 			nvmet_fc_transfer_fcp_data(tgtport, fod,
2046 						NVMET_FCOP_READDATA);
2047 			return;
2048 		}
2049 
2050 		/* data transfer complete, send response */
2051 
2052 		/* data no longer needed */
2053 		nvmet_fc_free_tgt_pgs(fod);
2054 
2055 		nvmet_fc_xmt_fcp_rsp(tgtport, fod);
2056 
2057 		break;
2058 
2059 	case NVMET_FCOP_RSP:
2060 		if (__nvmet_fc_fod_op_abort(fod, abort))
2061 			return;
2062 		nvmet_fc_free_fcp_iod(fod->queue, fod);
2063 		break;
2064 
2065 	default:
2066 		break;
2067 	}
2068 }
2069 
2070 static void
nvmet_fc_fcp_rqst_op_done_work(struct work_struct * work)2071 nvmet_fc_fcp_rqst_op_done_work(struct work_struct *work)
2072 {
2073 	struct nvmet_fc_fcp_iod *fod =
2074 		container_of(work, struct nvmet_fc_fcp_iod, done_work);
2075 
2076 	nvmet_fc_fod_op_done(fod);
2077 }
2078 
2079 static void
nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req * fcpreq)2080 nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req *fcpreq)
2081 {
2082 	struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
2083 	struct nvmet_fc_tgt_queue *queue = fod->queue;
2084 
2085 	if (fod->tgtport->ops->target_features & NVMET_FCTGTFEAT_OPDONE_IN_ISR)
2086 		/* context switch so completion is not in ISR context */
2087 		queue_work_on(queue->cpu, queue->work_q, &fod->done_work);
2088 	else
2089 		nvmet_fc_fod_op_done(fod);
2090 }
2091 
2092 /*
2093  * actual completion handler after execution by the nvmet layer
2094  */
2095 static void
__nvmet_fc_fcp_nvme_cmd_done(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_fcp_iod * fod,int status)2096 __nvmet_fc_fcp_nvme_cmd_done(struct nvmet_fc_tgtport *tgtport,
2097 			struct nvmet_fc_fcp_iod *fod, int status)
2098 {
2099 	struct nvme_common_command *sqe = &fod->cmdiubuf.sqe.common;
2100 	struct nvme_completion *cqe = &fod->rspiubuf.cqe;
2101 	unsigned long flags;
2102 	bool abort;
2103 
2104 	spin_lock_irqsave(&fod->flock, flags);
2105 	abort = fod->abort;
2106 	spin_unlock_irqrestore(&fod->flock, flags);
2107 
2108 	/* if we have a CQE, snoop the last sq_head value */
2109 	if (!status)
2110 		fod->queue->sqhd = cqe->sq_head;
2111 
2112 	if (abort) {
2113 		nvmet_fc_abort_op(tgtport, fod);
2114 		return;
2115 	}
2116 
2117 	/* if an error handling the cmd post initial parsing */
2118 	if (status) {
2119 		/* fudge up a failed CQE status for our transport error */
2120 		memset(cqe, 0, sizeof(*cqe));
2121 		cqe->sq_head = fod->queue->sqhd;	/* echo last cqe sqhd */
2122 		cqe->sq_id = cpu_to_le16(fod->queue->qid);
2123 		cqe->command_id = sqe->command_id;
2124 		cqe->status = cpu_to_le16(status);
2125 	} else {
2126 
2127 		/*
2128 		 * try to push the data even if the SQE status is non-zero.
2129 		 * There may be a status where data still was intended to
2130 		 * be moved
2131 		 */
2132 		if ((fod->io_dir == NVMET_FCP_READ) && (fod->data_sg_cnt)) {
2133 			/* push the data over before sending rsp */
2134 			nvmet_fc_transfer_fcp_data(tgtport, fod,
2135 						NVMET_FCOP_READDATA);
2136 			return;
2137 		}
2138 
2139 		/* writes & no data - fall thru */
2140 	}
2141 
2142 	/* data no longer needed */
2143 	nvmet_fc_free_tgt_pgs(fod);
2144 
2145 	nvmet_fc_xmt_fcp_rsp(tgtport, fod);
2146 }
2147 
2148 
2149 static void
nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req * nvme_req)2150 nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req)
2151 {
2152 	struct nvmet_fc_fcp_iod *fod = nvmet_req_to_fod(nvme_req);
2153 	struct nvmet_fc_tgtport *tgtport = fod->tgtport;
2154 
2155 	__nvmet_fc_fcp_nvme_cmd_done(tgtport, fod, 0);
2156 }
2157 
2158 
2159 /*
2160  * Actual processing routine for received FC-NVME LS Requests from the LLD
2161  */
2162 static void
nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport * tgtport,struct nvmet_fc_fcp_iod * fod)2163 nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
2164 			struct nvmet_fc_fcp_iod *fod)
2165 {
2166 	struct nvme_fc_cmd_iu *cmdiu = &fod->cmdiubuf;
2167 	int ret;
2168 
2169 	/*
2170 	 * Fused commands are currently not supported in the linux
2171 	 * implementation.
2172 	 *
2173 	 * As such, the implementation of the FC transport does not
2174 	 * look at the fused commands and order delivery to the upper
2175 	 * layer until we have both based on csn.
2176 	 */
2177 
2178 	fod->fcpreq->done = nvmet_fc_xmt_fcp_op_done;
2179 
2180 	fod->total_length = be32_to_cpu(cmdiu->data_len);
2181 	if (cmdiu->flags & FCNVME_CMD_FLAGS_WRITE) {
2182 		fod->io_dir = NVMET_FCP_WRITE;
2183 		if (!nvme_is_write(&cmdiu->sqe))
2184 			goto transport_error;
2185 	} else if (cmdiu->flags & FCNVME_CMD_FLAGS_READ) {
2186 		fod->io_dir = NVMET_FCP_READ;
2187 		if (nvme_is_write(&cmdiu->sqe))
2188 			goto transport_error;
2189 	} else {
2190 		fod->io_dir = NVMET_FCP_NODATA;
2191 		if (fod->total_length)
2192 			goto transport_error;
2193 	}
2194 
2195 	fod->req.cmd = &fod->cmdiubuf.sqe;
2196 	fod->req.rsp = &fod->rspiubuf.cqe;
2197 	fod->req.port = fod->queue->port;
2198 
2199 	/* ensure nvmet handlers will set cmd handler callback */
2200 	fod->req.execute = NULL;
2201 
2202 	/* clear any response payload */
2203 	memset(&fod->rspiubuf, 0, sizeof(fod->rspiubuf));
2204 
2205 	fod->data_sg = NULL;
2206 	fod->data_sg_cnt = 0;
2207 
2208 	ret = nvmet_req_init(&fod->req,
2209 				&fod->queue->nvme_cq,
2210 				&fod->queue->nvme_sq,
2211 				&nvmet_fc_tgt_fcp_ops);
2212 	if (!ret) {
2213 		/* bad SQE content or invalid ctrl state */
2214 		/* nvmet layer has already called op done to send rsp. */
2215 		return;
2216 	}
2217 
2218 	/* keep a running counter of tail position */
2219 	atomic_inc(&fod->queue->sqtail);
2220 
2221 	if (fod->total_length) {
2222 		ret = nvmet_fc_alloc_tgt_pgs(fod);
2223 		if (ret) {
2224 			nvmet_req_complete(&fod->req, ret);
2225 			return;
2226 		}
2227 	}
2228 	fod->req.sg = fod->data_sg;
2229 	fod->req.sg_cnt = fod->data_sg_cnt;
2230 	fod->offset = 0;
2231 
2232 	if (fod->io_dir == NVMET_FCP_WRITE) {
2233 		/* pull the data over before invoking nvmet layer */
2234 		nvmet_fc_transfer_fcp_data(tgtport, fod, NVMET_FCOP_WRITEDATA);
2235 		return;
2236 	}
2237 
2238 	/*
2239 	 * Reads or no data:
2240 	 *
2241 	 * can invoke the nvmet_layer now. If read data, cmd completion will
2242 	 * push the data
2243 	 */
2244 
2245 	fod->req.execute(&fod->req);
2246 
2247 	return;
2248 
2249 transport_error:
2250 	nvmet_fc_abort_op(tgtport, fod);
2251 }
2252 
2253 /*
2254  * Actual processing routine for received FC-NVME LS Requests from the LLD
2255  */
2256 static void
nvmet_fc_handle_fcp_rqst_work(struct work_struct * work)2257 nvmet_fc_handle_fcp_rqst_work(struct work_struct *work)
2258 {
2259 	struct nvmet_fc_fcp_iod *fod =
2260 		container_of(work, struct nvmet_fc_fcp_iod, work);
2261 	struct nvmet_fc_tgtport *tgtport = fod->tgtport;
2262 
2263 	nvmet_fc_handle_fcp_rqst(tgtport, fod);
2264 }
2265 
2266 /**
2267  * nvmet_fc_rcv_fcp_req - transport entry point called by an LLDD
2268  *                       upon the reception of a NVME FCP CMD IU.
2269  *
2270  * Pass a FC-NVME FCP CMD IU received from the FC link to the nvmet-fc
2271  * layer for processing.
2272  *
2273  * The nvmet_fc layer allocates a local job structure (struct
2274  * nvmet_fc_fcp_iod) from the queue for the io and copies the
2275  * CMD IU buffer to the job structure. As such, on a successful
2276  * completion (returns 0), the LLDD may immediately free/reuse
2277  * the CMD IU buffer passed in the call.
2278  *
2279  * However, in some circumstances, due to the packetized nature of FC
2280  * and the api of the FC LLDD which may issue a hw command to send the
2281  * response, but the LLDD may not get the hw completion for that command
2282  * and upcall the nvmet_fc layer before a new command may be
2283  * asynchronously received - its possible for a command to be received
2284  * before the LLDD and nvmet_fc have recycled the job structure. It gives
2285  * the appearance of more commands received than fits in the sq.
2286  * To alleviate this scenario, a temporary queue is maintained in the
2287  * transport for pending LLDD requests waiting for a queue job structure.
2288  * In these "overrun" cases, a temporary queue element is allocated
2289  * the LLDD request and CMD iu buffer information remembered, and the
2290  * routine returns a -EOVERFLOW status. Subsequently, when a queue job
2291  * structure is freed, it is immediately reallocated for anything on the
2292  * pending request list. The LLDDs defer_rcv() callback is called,
2293  * informing the LLDD that it may reuse the CMD IU buffer, and the io
2294  * is then started normally with the transport.
2295  *
2296  * The LLDD, when receiving an -EOVERFLOW completion status, is to treat
2297  * the completion as successful but must not reuse the CMD IU buffer
2298  * until the LLDD's defer_rcv() callback has been called for the
2299  * corresponding struct nvmefc_tgt_fcp_req pointer.
2300  *
2301  * If there is any other condition in which an error occurs, the
2302  * transport will return a non-zero status indicating the error.
2303  * In all cases other than -EOVERFLOW, the transport has not accepted the
2304  * request and the LLDD should abort the exchange.
2305  *
2306  * @target_port: pointer to the (registered) target port the FCP CMD IU
2307  *              was received on.
2308  * @fcpreq:     pointer to a fcpreq request structure to be used to reference
2309  *              the exchange corresponding to the FCP Exchange.
2310  * @cmdiubuf:   pointer to the buffer containing the FCP CMD IU
2311  * @cmdiubuf_len: length, in bytes, of the received FCP CMD IU
2312  */
2313 int
nvmet_fc_rcv_fcp_req(struct nvmet_fc_target_port * target_port,struct nvmefc_tgt_fcp_req * fcpreq,void * cmdiubuf,u32 cmdiubuf_len)2314 nvmet_fc_rcv_fcp_req(struct nvmet_fc_target_port *target_port,
2315 			struct nvmefc_tgt_fcp_req *fcpreq,
2316 			void *cmdiubuf, u32 cmdiubuf_len)
2317 {
2318 	struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
2319 	struct nvme_fc_cmd_iu *cmdiu = cmdiubuf;
2320 	struct nvmet_fc_tgt_queue *queue;
2321 	struct nvmet_fc_fcp_iod *fod;
2322 	struct nvmet_fc_defer_fcp_req *deferfcp;
2323 	unsigned long flags;
2324 
2325 	/* validate iu, so the connection id can be used to find the queue */
2326 	if ((cmdiubuf_len != sizeof(*cmdiu)) ||
2327 			(cmdiu->scsi_id != NVME_CMD_SCSI_ID) ||
2328 			(cmdiu->fc_id != NVME_CMD_FC_ID) ||
2329 			(be16_to_cpu(cmdiu->iu_len) != (sizeof(*cmdiu)/4)))
2330 		return -EIO;
2331 
2332 	queue = nvmet_fc_find_target_queue(tgtport,
2333 				be64_to_cpu(cmdiu->connection_id));
2334 	if (!queue)
2335 		return -ENOTCONN;
2336 
2337 	/*
2338 	 * note: reference taken by find_target_queue
2339 	 * After successful fod allocation, the fod will inherit the
2340 	 * ownership of that reference and will remove the reference
2341 	 * when the fod is freed.
2342 	 */
2343 
2344 	spin_lock_irqsave(&queue->qlock, flags);
2345 
2346 	fod = nvmet_fc_alloc_fcp_iod(queue);
2347 	if (fod) {
2348 		spin_unlock_irqrestore(&queue->qlock, flags);
2349 
2350 		fcpreq->nvmet_fc_private = fod;
2351 		fod->fcpreq = fcpreq;
2352 
2353 		memcpy(&fod->cmdiubuf, cmdiubuf, cmdiubuf_len);
2354 
2355 		nvmet_fc_queue_fcp_req(tgtport, queue, fcpreq);
2356 
2357 		return 0;
2358 	}
2359 
2360 	if (!tgtport->ops->defer_rcv) {
2361 		spin_unlock_irqrestore(&queue->qlock, flags);
2362 		/* release the queue lookup reference */
2363 		nvmet_fc_tgt_q_put(queue);
2364 		return -ENOENT;
2365 	}
2366 
2367 	deferfcp = list_first_entry_or_null(&queue->avail_defer_list,
2368 			struct nvmet_fc_defer_fcp_req, req_list);
2369 	if (deferfcp) {
2370 		/* Just re-use one that was previously allocated */
2371 		list_del(&deferfcp->req_list);
2372 	} else {
2373 		spin_unlock_irqrestore(&queue->qlock, flags);
2374 
2375 		/* Now we need to dynamically allocate one */
2376 		deferfcp = kmalloc(sizeof(*deferfcp), GFP_KERNEL);
2377 		if (!deferfcp) {
2378 			/* release the queue lookup reference */
2379 			nvmet_fc_tgt_q_put(queue);
2380 			return -ENOMEM;
2381 		}
2382 		spin_lock_irqsave(&queue->qlock, flags);
2383 	}
2384 
2385 	/* For now, use rspaddr / rsplen to save payload information */
2386 	fcpreq->rspaddr = cmdiubuf;
2387 	fcpreq->rsplen  = cmdiubuf_len;
2388 	deferfcp->fcp_req = fcpreq;
2389 
2390 	/* defer processing till a fod becomes available */
2391 	list_add_tail(&deferfcp->req_list, &queue->pending_cmd_list);
2392 
2393 	/* NOTE: the queue lookup reference is still valid */
2394 
2395 	spin_unlock_irqrestore(&queue->qlock, flags);
2396 
2397 	return -EOVERFLOW;
2398 }
2399 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_fcp_req);
2400 
2401 /**
2402  * nvmet_fc_rcv_fcp_abort - transport entry point called by an LLDD
2403  *                       upon the reception of an ABTS for a FCP command
2404  *
2405  * Notify the transport that an ABTS has been received for a FCP command
2406  * that had been given to the transport via nvmet_fc_rcv_fcp_req(). The
2407  * LLDD believes the command is still being worked on
2408  * (template_ops->fcp_req_release() has not been called).
2409  *
2410  * The transport will wait for any outstanding work (an op to the LLDD,
2411  * which the lldd should complete with error due to the ABTS; or the
2412  * completion from the nvmet layer of the nvme command), then will
2413  * stop processing and call the nvmet_fc_rcv_fcp_req() callback to
2414  * return the i/o context to the LLDD.  The LLDD may send the BA_ACC
2415  * to the ABTS either after return from this function (assuming any
2416  * outstanding op work has been terminated) or upon the callback being
2417  * called.
2418  *
2419  * @target_port: pointer to the (registered) target port the FCP CMD IU
2420  *              was received on.
2421  * @fcpreq:     pointer to the fcpreq request structure that corresponds
2422  *              to the exchange that received the ABTS.
2423  */
2424 void
nvmet_fc_rcv_fcp_abort(struct nvmet_fc_target_port * target_port,struct nvmefc_tgt_fcp_req * fcpreq)2425 nvmet_fc_rcv_fcp_abort(struct nvmet_fc_target_port *target_port,
2426 			struct nvmefc_tgt_fcp_req *fcpreq)
2427 {
2428 	struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
2429 	struct nvmet_fc_tgt_queue *queue;
2430 	unsigned long flags;
2431 
2432 	if (!fod || fod->fcpreq != fcpreq)
2433 		/* job appears to have already completed, ignore abort */
2434 		return;
2435 
2436 	queue = fod->queue;
2437 
2438 	spin_lock_irqsave(&queue->qlock, flags);
2439 	if (fod->active) {
2440 		/*
2441 		 * mark as abort. The abort handler, invoked upon completion
2442 		 * of any work, will detect the aborted status and do the
2443 		 * callback.
2444 		 */
2445 		spin_lock(&fod->flock);
2446 		fod->abort = true;
2447 		fod->aborted = true;
2448 		spin_unlock(&fod->flock);
2449 	}
2450 	spin_unlock_irqrestore(&queue->qlock, flags);
2451 }
2452 EXPORT_SYMBOL_GPL(nvmet_fc_rcv_fcp_abort);
2453 
2454 
2455 struct nvmet_fc_traddr {
2456 	u64	nn;
2457 	u64	pn;
2458 };
2459 
2460 static int
__nvme_fc_parse_u64(substring_t * sstr,u64 * val)2461 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
2462 {
2463 	u64 token64;
2464 
2465 	if (match_u64(sstr, &token64))
2466 		return -EINVAL;
2467 	*val = token64;
2468 
2469 	return 0;
2470 }
2471 
2472 /*
2473  * This routine validates and extracts the WWN's from the TRADDR string.
2474  * As kernel parsers need the 0x to determine number base, universally
2475  * build string to parse with 0x prefix before parsing name strings.
2476  */
2477 static int
nvme_fc_parse_traddr(struct nvmet_fc_traddr * traddr,char * buf,size_t blen)2478 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
2479 {
2480 	char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
2481 	substring_t wwn = { name, &name[sizeof(name)-1] };
2482 	int nnoffset, pnoffset;
2483 
2484 	/* validate it string one of the 2 allowed formats */
2485 	if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
2486 			!strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
2487 			!strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
2488 				"pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
2489 		nnoffset = NVME_FC_TRADDR_OXNNLEN;
2490 		pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
2491 						NVME_FC_TRADDR_OXNNLEN;
2492 	} else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
2493 			!strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
2494 			!strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
2495 				"pn-", NVME_FC_TRADDR_NNLEN))) {
2496 		nnoffset = NVME_FC_TRADDR_NNLEN;
2497 		pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
2498 	} else
2499 		goto out_einval;
2500 
2501 	name[0] = '0';
2502 	name[1] = 'x';
2503 	name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
2504 
2505 	memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
2506 	if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
2507 		goto out_einval;
2508 
2509 	memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
2510 	if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
2511 		goto out_einval;
2512 
2513 	return 0;
2514 
2515 out_einval:
2516 	pr_warn("%s: bad traddr string\n", __func__);
2517 	return -EINVAL;
2518 }
2519 
2520 static int
nvmet_fc_add_port(struct nvmet_port * port)2521 nvmet_fc_add_port(struct nvmet_port *port)
2522 {
2523 	struct nvmet_fc_tgtport *tgtport;
2524 	struct nvmet_fc_traddr traddr = { 0L, 0L };
2525 	unsigned long flags;
2526 	int ret;
2527 
2528 	/* validate the address info */
2529 	if ((port->disc_addr.trtype != NVMF_TRTYPE_FC) ||
2530 	    (port->disc_addr.adrfam != NVMF_ADDR_FAMILY_FC))
2531 		return -EINVAL;
2532 
2533 	/* map the traddr address info to a target port */
2534 
2535 	ret = nvme_fc_parse_traddr(&traddr, port->disc_addr.traddr,
2536 			sizeof(port->disc_addr.traddr));
2537 	if (ret)
2538 		return ret;
2539 
2540 	ret = -ENXIO;
2541 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
2542 	list_for_each_entry(tgtport, &nvmet_fc_target_list, tgt_list) {
2543 		if ((tgtport->fc_target_port.node_name == traddr.nn) &&
2544 		    (tgtport->fc_target_port.port_name == traddr.pn)) {
2545 			/* a FC port can only be 1 nvmet port id */
2546 			if (!tgtport->port) {
2547 				tgtport->port = port;
2548 				port->priv = tgtport;
2549 				nvmet_fc_tgtport_get(tgtport);
2550 				ret = 0;
2551 			} else
2552 				ret = -EALREADY;
2553 			break;
2554 		}
2555 	}
2556 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
2557 	return ret;
2558 }
2559 
2560 static void
nvmet_fc_remove_port(struct nvmet_port * port)2561 nvmet_fc_remove_port(struct nvmet_port *port)
2562 {
2563 	struct nvmet_fc_tgtport *tgtport = port->priv;
2564 	unsigned long flags;
2565 	bool matched = false;
2566 
2567 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
2568 	if (tgtport->port == port) {
2569 		matched = true;
2570 		tgtport->port = NULL;
2571 	}
2572 	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
2573 
2574 	if (matched)
2575 		nvmet_fc_tgtport_put(tgtport);
2576 }
2577 
2578 static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = {
2579 	.owner			= THIS_MODULE,
2580 	.type			= NVMF_TRTYPE_FC,
2581 	.msdbd			= 1,
2582 	.add_port		= nvmet_fc_add_port,
2583 	.remove_port		= nvmet_fc_remove_port,
2584 	.queue_response		= nvmet_fc_fcp_nvme_cmd_done,
2585 	.delete_ctrl		= nvmet_fc_delete_ctrl,
2586 };
2587 
nvmet_fc_init_module(void)2588 static int __init nvmet_fc_init_module(void)
2589 {
2590 	return nvmet_register_transport(&nvmet_fc_tgt_fcp_ops);
2591 }
2592 
nvmet_fc_exit_module(void)2593 static void __exit nvmet_fc_exit_module(void)
2594 {
2595 	/* sanity check - all lports should be removed */
2596 	if (!list_empty(&nvmet_fc_target_list))
2597 		pr_warn("%s: targetport list not empty\n", __func__);
2598 
2599 	nvmet_unregister_transport(&nvmet_fc_tgt_fcp_ops);
2600 
2601 	ida_destroy(&nvmet_fc_tgtport_cnt);
2602 }
2603 
2604 module_init(nvmet_fc_init_module);
2605 module_exit(nvmet_fc_exit_module);
2606 
2607 MODULE_LICENSE("GPL v2");
2608