xref: /openbmc/linux/drivers/scsi/libfc/fc_fcp.c (revision e0883a3c)
1 /*
2  * Copyright(c) 2007 Intel Corporation. All rights reserved.
3  * Copyright(c) 2008 Red Hat, Inc.  All rights reserved.
4  * Copyright(c) 2008 Mike Christie
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Maintained at www.Open-FCoE.org
20  */
21 
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/spinlock.h>
27 #include <linux/scatterlist.h>
28 #include <linux/err.h>
29 #include <linux/crc32.h>
30 #include <linux/slab.h>
31 
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_cmnd.h>
37 
38 #include <scsi/fc/fc_fc2.h>
39 
40 #include <scsi/libfc.h>
41 #include <scsi/fc_encode.h>
42 
43 #include "fc_libfc.h"
44 
45 struct kmem_cache *scsi_pkt_cachep;
46 
47 /* SRB state definitions */
48 #define FC_SRB_FREE		0		/* cmd is free */
49 #define FC_SRB_CMD_SENT		(1 << 0)	/* cmd has been sent */
50 #define FC_SRB_RCV_STATUS	(1 << 1)	/* response has arrived */
51 #define FC_SRB_ABORT_PENDING	(1 << 2)	/* cmd abort sent to device */
52 #define FC_SRB_ABORTED		(1 << 3)	/* abort acknowledged */
53 #define FC_SRB_DISCONTIG	(1 << 4)	/* non-sequential data recvd */
54 #define FC_SRB_COMPL		(1 << 5)	/* fc_io_compl has been run */
55 #define FC_SRB_FCP_PROCESSING_TMO (1 << 6)	/* timer function processing */
56 
57 #define FC_SRB_READ		(1 << 1)
58 #define FC_SRB_WRITE		(1 << 0)
59 
60 /* constant added to e_d_tov timeout to get rec_tov value */
61 #define REC_TOV_CONST		1
62 
63 /*
64  * The SCp.ptr should be tested and set under the scsi_pkt_queue lock
65  */
66 #define CMD_SP(Cmnd)		    ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
67 #define CMD_ENTRY_STATUS(Cmnd)	    ((Cmnd)->SCp.have_data_in)
68 #define CMD_COMPL_STATUS(Cmnd)	    ((Cmnd)->SCp.this_residual)
69 #define CMD_SCSI_STATUS(Cmnd)	    ((Cmnd)->SCp.Status)
70 #define CMD_RESID_LEN(Cmnd)	    ((Cmnd)->SCp.buffers_residual)
71 
72 /**
73  * struct fc_fcp_internal - FCP layer internal data
74  * @scsi_pkt_pool: Memory pool to draw FCP packets from
75  * @scsi_queue_lock: Protects the scsi_pkt_queue
76  * @scsi_pkt_queue: Current FCP packets
77  * @last_can_queue_ramp_down_time: ramp down time
78  * @last_can_queue_ramp_up_time: ramp up time
79  * @max_can_queue: max can_queue size
80  */
81 struct fc_fcp_internal {
82 	mempool_t		*scsi_pkt_pool;
83 	spinlock_t		scsi_queue_lock;
84 	struct list_head	scsi_pkt_queue;
85 	unsigned long		last_can_queue_ramp_down_time;
86 	unsigned long		last_can_queue_ramp_up_time;
87 	int			max_can_queue;
88 };
89 
90 #define fc_get_scsi_internal(x)	((struct fc_fcp_internal *)(x)->scsi_priv)
91 
92 /*
93  * function prototypes
94  * FC scsi I/O related functions
95  */
96 static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
97 static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
98 static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
99 static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
100 static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
101 static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
102 static void fc_fcp_recovery(struct fc_fcp_pkt *, u8 code);
103 static void fc_fcp_timeout(unsigned long);
104 static void fc_fcp_rec(struct fc_fcp_pkt *);
105 static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
106 static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
107 static void fc_io_compl(struct fc_fcp_pkt *);
108 
109 static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
110 static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
111 static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
112 
113 /*
114  * command status codes
115  */
116 #define FC_COMPLETE		0
117 #define FC_CMD_ABORTED		1
118 #define FC_CMD_RESET		2
119 #define FC_CMD_PLOGO		3
120 #define FC_SNS_RCV		4
121 #define FC_TRANS_ERR		5
122 #define FC_DATA_OVRRUN		6
123 #define FC_DATA_UNDRUN		7
124 #define FC_ERROR		8
125 #define FC_HRD_ERROR		9
126 #define FC_CRC_ERROR		10
127 #define FC_TIMED_OUT		11
128 
129 /*
130  * Error recovery timeout values.
131  */
132 #define FC_SCSI_TM_TOV		(10 * HZ)
133 #define FC_HOST_RESET_TIMEOUT	(30 * HZ)
134 #define FC_CAN_QUEUE_PERIOD	(60 * HZ)
135 
136 #define FC_MAX_ERROR_CNT	5
137 #define FC_MAX_RECOV_RETRY	3
138 
139 #define FC_FCP_DFLT_QUEUE_DEPTH 32
140 
141 /**
142  * fc_fcp_pkt_alloc() - Allocate a fcp_pkt
143  * @lport: The local port that the FCP packet is for
144  * @gfp:   GFP flags for allocation
145  *
146  * Return value: fcp_pkt structure or null on allocation failure.
147  * Context:	 Can be called from process context, no lock is required.
148  */
149 static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
150 {
151 	struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
152 	struct fc_fcp_pkt *fsp;
153 
154 	fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
155 	if (fsp) {
156 		memset(fsp, 0, sizeof(*fsp));
157 		fsp->lp = lport;
158 		atomic_set(&fsp->ref_cnt, 1);
159 		init_timer(&fsp->timer);
160 		INIT_LIST_HEAD(&fsp->list);
161 		spin_lock_init(&fsp->scsi_pkt_lock);
162 	}
163 	return fsp;
164 }
165 
166 /**
167  * fc_fcp_pkt_release() - Release hold on a fcp_pkt
168  * @fsp: The FCP packet to be released
169  *
170  * Context: Can be called from process or interrupt context,
171  *	    no lock is required.
172  */
173 static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
174 {
175 	if (atomic_dec_and_test(&fsp->ref_cnt)) {
176 		struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
177 
178 		mempool_free(fsp, si->scsi_pkt_pool);
179 	}
180 }
181 
182 /**
183  * fc_fcp_pkt_hold() - Hold a fcp_pkt
184  * @fsp: The FCP packet to be held
185  */
186 static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
187 {
188 	atomic_inc(&fsp->ref_cnt);
189 }
190 
191 /**
192  * fc_fcp_pkt_destory() - Release hold on a fcp_pkt
193  * @seq: The sequence that the FCP packet is on (required by destructor API)
194  * @fsp: The FCP packet to be released
195  *
196  * This routine is called by a destructor callback in the exch_seq_send()
197  * routine of the libfc Transport Template. The 'struct fc_seq' is a required
198  * argument even though it is not used by this routine.
199  *
200  * Context: No locking required.
201  */
202 static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
203 {
204 	fc_fcp_pkt_release(fsp);
205 }
206 
207 /**
208  * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
209  * @fsp: The FCP packet to be locked and incremented
210  *
211  * We should only return error if we return a command to SCSI-ml before
212  * getting a response. This can happen in cases where we send a abort, but
213  * do not wait for the response and the abort and command can be passing
214  * each other on the wire/network-layer.
215  *
216  * Note: this function locks the packet and gets a reference to allow
217  * callers to call the completion function while the lock is held and
218  * not have to worry about the packets refcount.
219  *
220  * TODO: Maybe we should just have callers grab/release the lock and
221  * have a function that they call to verify the fsp and grab a ref if
222  * needed.
223  */
224 static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
225 {
226 	spin_lock_bh(&fsp->scsi_pkt_lock);
227 	if (fsp->state & FC_SRB_COMPL) {
228 		spin_unlock_bh(&fsp->scsi_pkt_lock);
229 		return -EPERM;
230 	}
231 
232 	fc_fcp_pkt_hold(fsp);
233 	return 0;
234 }
235 
236 /**
237  * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its
238  *			 reference count
239  * @fsp: The FCP packet to be unlocked and decremented
240  */
241 static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
242 {
243 	spin_unlock_bh(&fsp->scsi_pkt_lock);
244 	fc_fcp_pkt_release(fsp);
245 }
246 
247 /**
248  * fc_fcp_timer_set() - Start a timer for a fcp_pkt
249  * @fsp:   The FCP packet to start a timer for
250  * @delay: The timeout period for the timer
251  */
252 static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
253 {
254 	if (!(fsp->state & FC_SRB_COMPL))
255 		mod_timer(&fsp->timer, jiffies + delay);
256 }
257 
258 /**
259  * fc_fcp_send_abort() - Send an abort for exchanges associated with a
260  *			 fcp_pkt
261  * @fsp: The FCP packet to abort exchanges on
262  */
263 static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
264 {
265 	if (!fsp->seq_ptr)
266 		return -EINVAL;
267 
268 	fsp->state |= FC_SRB_ABORT_PENDING;
269 	return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
270 }
271 
272 /**
273  * fc_fcp_retry_cmd() - Retry a fcp_pkt
274  * @fsp: The FCP packet to be retried
275  *
276  * Sets the status code to be FC_ERROR and then calls
277  * fc_fcp_complete_locked() which in turn calls fc_io_compl().
278  * fc_io_compl() will notify the SCSI-ml that the I/O is done.
279  * The SCSI-ml will retry the command.
280  */
281 static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
282 {
283 	if (fsp->seq_ptr) {
284 		fsp->lp->tt.exch_done(fsp->seq_ptr);
285 		fsp->seq_ptr = NULL;
286 	}
287 
288 	fsp->state &= ~FC_SRB_ABORT_PENDING;
289 	fsp->io_status = 0;
290 	fsp->status_code = FC_ERROR;
291 	fc_fcp_complete_locked(fsp);
292 }
293 
294 /**
295  * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context
296  * @fsp: The FCP packet that will manage the DDP frames
297  * @xid: The XID that will be used for the DDP exchange
298  */
299 void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
300 {
301 	struct fc_lport *lport;
302 
303 	lport = fsp->lp;
304 	if ((fsp->req_flags & FC_SRB_READ) &&
305 	    (lport->lro_enabled) && (lport->tt.ddp_setup)) {
306 		if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd),
307 					scsi_sg_count(fsp->cmd)))
308 			fsp->xfer_ddp = xid;
309 	}
310 }
311 
312 /**
313  * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any
314  *		       DDP related resources for a fcp_pkt
315  * @fsp: The FCP packet that DDP had been used on
316  */
317 static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
318 {
319 	struct fc_lport *lport;
320 
321 	if (!fsp)
322 		return;
323 
324 	if (fsp->xfer_ddp == FC_XID_UNKNOWN)
325 		return;
326 
327 	lport = fsp->lp;
328 	if (lport->tt.ddp_done) {
329 		fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
330 		fsp->xfer_ddp = FC_XID_UNKNOWN;
331 	}
332 }
333 
334 /**
335  * fc_fcp_can_queue_ramp_up() - increases can_queue
336  * @lport: lport to ramp up can_queue
337  *
338  * Locking notes: Called with Scsi_Host lock held
339  */
340 static void fc_fcp_can_queue_ramp_up(struct fc_lport *lport)
341 {
342 	struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
343 	int can_queue;
344 
345 	if (si->last_can_queue_ramp_up_time &&
346 	    (time_before(jiffies, si->last_can_queue_ramp_up_time +
347 			 FC_CAN_QUEUE_PERIOD)))
348 		return;
349 
350 	if (time_before(jiffies, si->last_can_queue_ramp_down_time +
351 			FC_CAN_QUEUE_PERIOD))
352 		return;
353 
354 	si->last_can_queue_ramp_up_time = jiffies;
355 
356 	can_queue = lport->host->can_queue << 1;
357 	if (can_queue >= si->max_can_queue) {
358 		can_queue = si->max_can_queue;
359 		si->last_can_queue_ramp_down_time = 0;
360 	}
361 	lport->host->can_queue = can_queue;
362 	shost_printk(KERN_ERR, lport->host, "libfc: increased "
363 		     "can_queue to %d.\n", can_queue);
364 }
365 
366 /**
367  * fc_fcp_can_queue_ramp_down() - reduces can_queue
368  * @lport: lport to reduce can_queue
369  *
370  * If we are getting memory allocation failures, then we may
371  * be trying to execute too many commands. We let the running
372  * commands complete or timeout, then try again with a reduced
373  * can_queue. Eventually we will hit the point where we run
374  * on all reserved structs.
375  *
376  * Locking notes: Called with Scsi_Host lock held
377  */
378 static void fc_fcp_can_queue_ramp_down(struct fc_lport *lport)
379 {
380 	struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
381 	int can_queue;
382 
383 	if (si->last_can_queue_ramp_down_time &&
384 	    (time_before(jiffies, si->last_can_queue_ramp_down_time +
385 			 FC_CAN_QUEUE_PERIOD)))
386 		return;
387 
388 	si->last_can_queue_ramp_down_time = jiffies;
389 
390 	can_queue = lport->host->can_queue;
391 	can_queue >>= 1;
392 	if (!can_queue)
393 		can_queue = 1;
394 	lport->host->can_queue = can_queue;
395 	shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
396 		     "Reducing can_queue to %d.\n", can_queue);
397 }
398 
399 /*
400  * fc_fcp_frame_alloc() -  Allocates fc_frame structure and buffer.
401  * @lport:	fc lport struct
402  * @len:	payload length
403  *
404  * Allocates fc_frame structure and buffer but if fails to allocate
405  * then reduce can_queue.
406  */
407 static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport,
408 						  size_t len)
409 {
410 	struct fc_frame *fp;
411 	unsigned long flags;
412 
413 	fp = fc_frame_alloc(lport, len);
414 	if (likely(fp))
415 		return fp;
416 
417 	/* error case */
418 	spin_lock_irqsave(lport->host->host_lock, flags);
419 	fc_fcp_can_queue_ramp_down(lport);
420 	spin_unlock_irqrestore(lport->host->host_lock, flags);
421 	return NULL;
422 }
423 
424 /**
425  * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target
426  * @fsp: The FCP packet the data is on
427  * @fp:	 The data frame
428  */
429 static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
430 {
431 	struct scsi_cmnd *sc = fsp->cmd;
432 	struct fc_lport *lport = fsp->lp;
433 	struct fcoe_dev_stats *stats;
434 	struct fc_frame_header *fh;
435 	size_t start_offset;
436 	size_t offset;
437 	u32 crc;
438 	u32 copy_len = 0;
439 	size_t len;
440 	void *buf;
441 	struct scatterlist *sg;
442 	u32 nents;
443 	u8 host_bcode = FC_COMPLETE;
444 
445 	fh = fc_frame_header_get(fp);
446 	offset = ntohl(fh->fh_parm_offset);
447 	start_offset = offset;
448 	len = fr_len(fp) - sizeof(*fh);
449 	buf = fc_frame_payload_get(fp, 0);
450 
451 	/*
452 	 * if this I/O is ddped then clear it and initiate recovery since data
453 	 * frames are expected to be placed directly in that case.
454 	 *
455 	 * Indicate error to scsi-ml because something went wrong with the
456 	 * ddp handling to get us here.
457 	 */
458 	if (fsp->xfer_ddp != FC_XID_UNKNOWN) {
459 		fc_fcp_ddp_done(fsp);
460 		FC_FCP_DBG(fsp, "DDP I/O in fc_fcp_recv_data set ERROR\n");
461 		host_bcode = FC_ERROR;
462 		goto err;
463 	}
464 	if (offset + len > fsp->data_len) {
465 		/* this should never happen */
466 		if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
467 		    fc_frame_crc_check(fp))
468 			goto crc_err;
469 		FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
470 			   "data_len %x\n", len, offset, fsp->data_len);
471 
472 		/* Data is corrupted indicate scsi-ml should retry */
473 		host_bcode = FC_DATA_OVRRUN;
474 		goto err;
475 	}
476 	if (offset != fsp->xfer_len)
477 		fsp->state |= FC_SRB_DISCONTIG;
478 
479 	sg = scsi_sglist(sc);
480 	nents = scsi_sg_count(sc);
481 
482 	if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
483 		copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
484 						    &offset, KM_SOFTIRQ0, NULL);
485 	} else {
486 		crc = crc32(~0, (u8 *) fh, sizeof(*fh));
487 		copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
488 						    &offset, KM_SOFTIRQ0, &crc);
489 		buf = fc_frame_payload_get(fp, 0);
490 		if (len % 4)
491 			crc = crc32(crc, buf + len, 4 - (len % 4));
492 
493 		if (~crc != le32_to_cpu(fr_crc(fp))) {
494 crc_err:
495 			stats = per_cpu_ptr(lport->dev_stats, get_cpu());
496 			stats->ErrorFrames++;
497 			/* per cpu count, not total count, but OK for limit */
498 			if (stats->InvalidCRCCount++ < 5)
499 				printk(KERN_WARNING "libfc: CRC error on data "
500 				       "frame for port (%6.6x)\n",
501 				       lport->port_id);
502 			put_cpu();
503 			/*
504 			 * Assume the frame is total garbage.
505 			 * We may have copied it over the good part
506 			 * of the buffer.
507 			 * If so, we need to retry the entire operation.
508 			 * Otherwise, ignore it.
509 			 */
510 			if (fsp->state & FC_SRB_DISCONTIG) {
511 				host_bcode = FC_CRC_ERROR;
512 				goto err;
513 			}
514 			return;
515 		}
516 	}
517 
518 	if (fsp->xfer_contig_end == start_offset)
519 		fsp->xfer_contig_end += copy_len;
520 	fsp->xfer_len += copy_len;
521 
522 	/*
523 	 * In the very rare event that this data arrived after the response
524 	 * and completes the transfer, call the completion handler.
525 	 */
526 	if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
527 	    fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
528 		fc_fcp_complete_locked(fsp);
529 	return;
530 err:
531 	fc_fcp_recovery(fsp, host_bcode);
532 }
533 
534 /**
535  * fc_fcp_send_data() - Send SCSI data to a target
536  * @fsp:      The FCP packet the data is on
537  * @sp:	      The sequence the data is to be sent on
538  * @offset:   The starting offset for this data request
539  * @seq_blen: The burst length for this data request
540  *
541  * Called after receiving a Transfer Ready data descriptor.
542  * If the LLD is capable of sequence offload then send down the
543  * seq_blen amount of data in single frame, otherwise send
544  * multiple frames of the maximum frame payload supported by
545  * the target port.
546  */
547 static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
548 			    size_t offset, size_t seq_blen)
549 {
550 	struct fc_exch *ep;
551 	struct scsi_cmnd *sc;
552 	struct scatterlist *sg;
553 	struct fc_frame *fp = NULL;
554 	struct fc_lport *lport = fsp->lp;
555 	struct page *page;
556 	size_t remaining;
557 	size_t t_blen;
558 	size_t tlen;
559 	size_t sg_bytes;
560 	size_t frame_offset, fh_parm_offset;
561 	size_t off;
562 	int error;
563 	void *data = NULL;
564 	void *page_addr;
565 	int using_sg = lport->sg_supp;
566 	u32 f_ctl;
567 
568 	WARN_ON(seq_blen <= 0);
569 	if (unlikely(offset + seq_blen > fsp->data_len)) {
570 		/* this should never happen */
571 		FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
572 			   "offset %zx\n", seq_blen, offset);
573 		fc_fcp_send_abort(fsp);
574 		return 0;
575 	} else if (offset != fsp->xfer_len) {
576 		/* Out of Order Data Request - no problem, but unexpected. */
577 		FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
578 			   "seq_blen %zx offset %zx\n", seq_blen, offset);
579 	}
580 
581 	/*
582 	 * if LLD is capable of seq_offload then set transport
583 	 * burst length (t_blen) to seq_blen, otherwise set t_blen
584 	 * to max FC frame payload previously set in fsp->max_payload.
585 	 */
586 	t_blen = fsp->max_payload;
587 	if (lport->seq_offload) {
588 		t_blen = min(seq_blen, (size_t)lport->lso_max);
589 		FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
590 			   fsp, seq_blen, lport->lso_max, t_blen);
591 	}
592 
593 	if (t_blen > 512)
594 		t_blen &= ~(512 - 1);	/* round down to block size */
595 	sc = fsp->cmd;
596 
597 	remaining = seq_blen;
598 	fh_parm_offset = frame_offset = offset;
599 	tlen = 0;
600 	seq = lport->tt.seq_start_next(seq);
601 	f_ctl = FC_FC_REL_OFF;
602 	WARN_ON(!seq);
603 
604 	sg = scsi_sglist(sc);
605 
606 	while (remaining > 0 && sg) {
607 		if (offset >= sg->length) {
608 			offset -= sg->length;
609 			sg = sg_next(sg);
610 			continue;
611 		}
612 		if (!fp) {
613 			tlen = min(t_blen, remaining);
614 
615 			/*
616 			 * TODO.  Temporary workaround.	 fc_seq_send() can't
617 			 * handle odd lengths in non-linear skbs.
618 			 * This will be the final fragment only.
619 			 */
620 			if (tlen % 4)
621 				using_sg = 0;
622 			fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
623 			if (!fp)
624 				return -ENOMEM;
625 
626 			data = fc_frame_header_get(fp) + 1;
627 			fh_parm_offset = frame_offset;
628 			fr_max_payload(fp) = fsp->max_payload;
629 		}
630 
631 		off = offset + sg->offset;
632 		sg_bytes = min(tlen, sg->length - offset);
633 		sg_bytes = min(sg_bytes,
634 			       (size_t) (PAGE_SIZE - (off & ~PAGE_MASK)));
635 		page = sg_page(sg) + (off >> PAGE_SHIFT);
636 		if (using_sg) {
637 			get_page(page);
638 			skb_fill_page_desc(fp_skb(fp),
639 					   skb_shinfo(fp_skb(fp))->nr_frags,
640 					   page, off & ~PAGE_MASK, sg_bytes);
641 			fp_skb(fp)->data_len += sg_bytes;
642 			fr_len(fp) += sg_bytes;
643 			fp_skb(fp)->truesize += PAGE_SIZE;
644 		} else {
645 			/*
646 			 * The scatterlist item may be bigger than PAGE_SIZE,
647 			 * but we must not cross pages inside the kmap.
648 			 */
649 			page_addr = kmap_atomic(page, KM_SOFTIRQ0);
650 			memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
651 			       sg_bytes);
652 			kunmap_atomic(page_addr, KM_SOFTIRQ0);
653 			data += sg_bytes;
654 		}
655 		offset += sg_bytes;
656 		frame_offset += sg_bytes;
657 		tlen -= sg_bytes;
658 		remaining -= sg_bytes;
659 
660 		if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
661 		    (tlen))
662 			continue;
663 
664 		/*
665 		 * Send sequence with transfer sequence initiative in case
666 		 * this is last FCP frame of the sequence.
667 		 */
668 		if (remaining == 0)
669 			f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
670 
671 		ep = fc_seq_exch(seq);
672 		fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
673 			       FC_TYPE_FCP, f_ctl, fh_parm_offset);
674 
675 		/*
676 		 * send fragment using for a sequence.
677 		 */
678 		error = lport->tt.seq_send(lport, seq, fp);
679 		if (error) {
680 			WARN_ON(1);		/* send error should be rare */
681 			fc_fcp_retry_cmd(fsp);
682 			return 0;
683 		}
684 		fp = NULL;
685 	}
686 	fsp->xfer_len += seq_blen;	/* premature count? */
687 	return 0;
688 }
689 
690 /**
691  * fc_fcp_abts_resp() - Send an ABTS response
692  * @fsp: The FCP packet that is being aborted
693  * @fp:	 The response frame
694  */
695 static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
696 {
697 	int ba_done = 1;
698 	struct fc_ba_rjt *brp;
699 	struct fc_frame_header *fh;
700 
701 	fh = fc_frame_header_get(fp);
702 	switch (fh->fh_r_ctl) {
703 	case FC_RCTL_BA_ACC:
704 		break;
705 	case FC_RCTL_BA_RJT:
706 		brp = fc_frame_payload_get(fp, sizeof(*brp));
707 		if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
708 			break;
709 		/* fall thru */
710 	default:
711 		/*
712 		 * we will let the command timeout
713 		 * and scsi-ml recover in this case,
714 		 * therefore cleared the ba_done flag.
715 		 */
716 		ba_done = 0;
717 	}
718 
719 	if (ba_done) {
720 		fsp->state |= FC_SRB_ABORTED;
721 		fsp->state &= ~FC_SRB_ABORT_PENDING;
722 
723 		if (fsp->wait_for_comp)
724 			complete(&fsp->tm_done);
725 		else
726 			fc_fcp_complete_locked(fsp);
727 	}
728 }
729 
730 /**
731  * fc_fcp_recv() - Reveive an FCP frame
732  * @seq: The sequence the frame is on
733  * @fp:	 The received frame
734  * @arg: The related FCP packet
735  *
736  * Context: Called from Soft IRQ context. Can not be called
737  *	    holding the FCP packet list lock.
738  */
739 static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
740 {
741 	struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
742 	struct fc_lport *lport = fsp->lp;
743 	struct fc_frame_header *fh;
744 	struct fcp_txrdy *dd;
745 	u8 r_ctl;
746 	int rc = 0;
747 
748 	if (IS_ERR(fp)) {
749 		fc_fcp_error(fsp, fp);
750 		return;
751 	}
752 
753 	fh = fc_frame_header_get(fp);
754 	r_ctl = fh->fh_r_ctl;
755 
756 	if (lport->state != LPORT_ST_READY)
757 		goto out;
758 	if (fc_fcp_lock_pkt(fsp))
759 		goto out;
760 	fsp->last_pkt_time = jiffies;
761 
762 	if (fh->fh_type == FC_TYPE_BLS) {
763 		fc_fcp_abts_resp(fsp, fp);
764 		goto unlock;
765 	}
766 
767 	if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
768 		goto unlock;
769 
770 	if (r_ctl == FC_RCTL_DD_DATA_DESC) {
771 		/*
772 		 * received XFER RDY from the target
773 		 * need to send data to the target
774 		 */
775 		WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
776 		dd = fc_frame_payload_get(fp, sizeof(*dd));
777 		WARN_ON(!dd);
778 
779 		rc = fc_fcp_send_data(fsp, seq,
780 				      (size_t) ntohl(dd->ft_data_ro),
781 				      (size_t) ntohl(dd->ft_burst_len));
782 		if (!rc)
783 			seq->rec_data = fsp->xfer_len;
784 	} else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
785 		/*
786 		 * received a DATA frame
787 		 * next we will copy the data to the system buffer
788 		 */
789 		WARN_ON(fr_len(fp) < sizeof(*fh));	/* len may be 0 */
790 		fc_fcp_recv_data(fsp, fp);
791 		seq->rec_data = fsp->xfer_contig_end;
792 	} else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
793 		WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
794 
795 		fc_fcp_resp(fsp, fp);
796 	} else {
797 		FC_FCP_DBG(fsp, "unexpected frame.  r_ctl %x\n", r_ctl);
798 	}
799 unlock:
800 	fc_fcp_unlock_pkt(fsp);
801 out:
802 	fc_frame_free(fp);
803 }
804 
805 /**
806  * fc_fcp_resp() - Handler for FCP responses
807  * @fsp: The FCP packet the response is for
808  * @fp:	 The response frame
809  */
810 static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
811 {
812 	struct fc_frame_header *fh;
813 	struct fcp_resp *fc_rp;
814 	struct fcp_resp_ext *rp_ex;
815 	struct fcp_resp_rsp_info *fc_rp_info;
816 	u32 plen;
817 	u32 expected_len;
818 	u32 respl = 0;
819 	u32 snsl = 0;
820 	u8 flags = 0;
821 
822 	plen = fr_len(fp);
823 	fh = (struct fc_frame_header *)fr_hdr(fp);
824 	if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
825 		goto len_err;
826 	plen -= sizeof(*fh);
827 	fc_rp = (struct fcp_resp *)(fh + 1);
828 	fsp->cdb_status = fc_rp->fr_status;
829 	flags = fc_rp->fr_flags;
830 	fsp->scsi_comp_flags = flags;
831 	expected_len = fsp->data_len;
832 
833 	/* if ddp, update xfer len */
834 	fc_fcp_ddp_done(fsp);
835 
836 	if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
837 		rp_ex = (void *)(fc_rp + 1);
838 		if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
839 			if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
840 				goto len_err;
841 			fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
842 			if (flags & FCP_RSP_LEN_VAL) {
843 				respl = ntohl(rp_ex->fr_rsp_len);
844 				if (respl != sizeof(*fc_rp_info))
845 					goto len_err;
846 				if (fsp->wait_for_comp) {
847 					/* Abuse cdb_status for rsp code */
848 					fsp->cdb_status = fc_rp_info->rsp_code;
849 					complete(&fsp->tm_done);
850 					/*
851 					 * tmfs will not have any scsi cmd so
852 					 * exit here
853 					 */
854 					return;
855 				}
856 			}
857 			if (flags & FCP_SNS_LEN_VAL) {
858 				snsl = ntohl(rp_ex->fr_sns_len);
859 				if (snsl > SCSI_SENSE_BUFFERSIZE)
860 					snsl = SCSI_SENSE_BUFFERSIZE;
861 				memcpy(fsp->cmd->sense_buffer,
862 				       (char *)fc_rp_info + respl, snsl);
863 			}
864 		}
865 		if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
866 			if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
867 				goto len_err;
868 			if (flags & FCP_RESID_UNDER) {
869 				fsp->scsi_resid = ntohl(rp_ex->fr_resid);
870 				/*
871 				 * The cmnd->underflow is the minimum number of
872 				 * bytes that must be transfered for this
873 				 * command.  Provided a sense condition is not
874 				 * present, make sure the actual amount
875 				 * transferred is at least the underflow value
876 				 * or fail.
877 				 */
878 				if (!(flags & FCP_SNS_LEN_VAL) &&
879 				    (fc_rp->fr_status == 0) &&
880 				    (scsi_bufflen(fsp->cmd) -
881 				     fsp->scsi_resid) < fsp->cmd->underflow)
882 					goto err;
883 				expected_len -= fsp->scsi_resid;
884 			} else {
885 				fsp->status_code = FC_ERROR;
886 			}
887 		}
888 	}
889 	fsp->state |= FC_SRB_RCV_STATUS;
890 
891 	/*
892 	 * Check for missing or extra data frames.
893 	 */
894 	if (unlikely(fsp->xfer_len != expected_len)) {
895 		if (fsp->xfer_len < expected_len) {
896 			/*
897 			 * Some data may be queued locally,
898 			 * Wait a at least one jiffy to see if it is delivered.
899 			 * If this expires without data, we may do SRR.
900 			 */
901 			fc_fcp_timer_set(fsp, 2);
902 			return;
903 		}
904 		fsp->status_code = FC_DATA_OVRRUN;
905 		FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx greater than expected, "
906 			   "len %x, data len %x\n",
907 			   fsp->rport->port_id,
908 			   fsp->xfer_len, expected_len, fsp->data_len);
909 	}
910 	fc_fcp_complete_locked(fsp);
911 	return;
912 
913 len_err:
914 	FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
915 		   "snsl %u\n", flags, fr_len(fp), respl, snsl);
916 err:
917 	fsp->status_code = FC_ERROR;
918 	fc_fcp_complete_locked(fsp);
919 }
920 
921 /**
922  * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
923  *			      fcp_pkt lock held
924  * @fsp: The FCP packet to be completed
925  *
926  * This function may sleep if a timer is pending. The packet lock must be
927  * held, and the host lock must not be held.
928  */
929 static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
930 {
931 	struct fc_lport *lport = fsp->lp;
932 	struct fc_seq *seq;
933 	struct fc_exch *ep;
934 	u32 f_ctl;
935 
936 	if (fsp->state & FC_SRB_ABORT_PENDING)
937 		return;
938 
939 	if (fsp->state & FC_SRB_ABORTED) {
940 		if (!fsp->status_code)
941 			fsp->status_code = FC_CMD_ABORTED;
942 	} else {
943 		/*
944 		 * Test for transport underrun, independent of response
945 		 * underrun status.
946 		 */
947 		if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
948 		    (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
949 		     fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
950 			fsp->status_code = FC_DATA_UNDRUN;
951 			fsp->io_status = 0;
952 		}
953 	}
954 
955 	seq = fsp->seq_ptr;
956 	if (seq) {
957 		fsp->seq_ptr = NULL;
958 		if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
959 			struct fc_frame *conf_frame;
960 			struct fc_seq *csp;
961 
962 			csp = lport->tt.seq_start_next(seq);
963 			conf_frame = fc_fcp_frame_alloc(fsp->lp, 0);
964 			if (conf_frame) {
965 				f_ctl = FC_FC_SEQ_INIT;
966 				f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
967 				ep = fc_seq_exch(seq);
968 				fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
969 					       ep->did, ep->sid,
970 					       FC_TYPE_FCP, f_ctl, 0);
971 				lport->tt.seq_send(lport, csp, conf_frame);
972 			}
973 		}
974 		lport->tt.exch_done(seq);
975 	}
976 	fc_io_compl(fsp);
977 }
978 
979 /**
980  * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
981  * @fsp:   The FCP packet whose exchanges should be canceled
982  * @error: The reason for the cancellation
983  */
984 static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
985 {
986 	struct fc_lport *lport = fsp->lp;
987 
988 	if (fsp->seq_ptr) {
989 		lport->tt.exch_done(fsp->seq_ptr);
990 		fsp->seq_ptr = NULL;
991 	}
992 	fsp->status_code = error;
993 }
994 
995 /**
996  * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
997  * @lport: The local port whose exchanges should be canceled
998  * @id:	   The target's ID
999  * @lun:   The LUN
1000  * @error: The reason for cancellation
1001  *
1002  * If lun or id is -1, they are ignored.
1003  */
1004 static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
1005 				    unsigned int lun, int error)
1006 {
1007 	struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
1008 	struct fc_fcp_pkt *fsp;
1009 	struct scsi_cmnd *sc_cmd;
1010 	unsigned long flags;
1011 
1012 	spin_lock_irqsave(&si->scsi_queue_lock, flags);
1013 restart:
1014 	list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
1015 		sc_cmd = fsp->cmd;
1016 		if (id != -1 && scmd_id(sc_cmd) != id)
1017 			continue;
1018 
1019 		if (lun != -1 && sc_cmd->device->lun != lun)
1020 			continue;
1021 
1022 		fc_fcp_pkt_hold(fsp);
1023 		spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
1024 
1025 		if (!fc_fcp_lock_pkt(fsp)) {
1026 			fc_fcp_cleanup_cmd(fsp, error);
1027 			fc_io_compl(fsp);
1028 			fc_fcp_unlock_pkt(fsp);
1029 		}
1030 
1031 		fc_fcp_pkt_release(fsp);
1032 		spin_lock_irqsave(&si->scsi_queue_lock, flags);
1033 		/*
1034 		 * while we dropped the lock multiple pkts could
1035 		 * have been released, so we have to start over.
1036 		 */
1037 		goto restart;
1038 	}
1039 	spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
1040 }
1041 
1042 /**
1043  * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
1044  * @lport: The local port whose exchanges are to be aborted
1045  */
1046 static void fc_fcp_abort_io(struct fc_lport *lport)
1047 {
1048 	fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
1049 }
1050 
1051 /**
1052  * fc_fcp_pkt_send() - Send a fcp_pkt
1053  * @lport: The local port to send the FCP packet on
1054  * @fsp:   The FCP packet to send
1055  *
1056  * Return:  Zero for success and -1 for failure
1057  * Locks:   Called without locks held
1058  */
1059 static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
1060 {
1061 	struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
1062 	unsigned long flags;
1063 	int rc;
1064 
1065 	fsp->cmd->SCp.ptr = (char *)fsp;
1066 	fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1067 	fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
1068 
1069 	int_to_scsilun(fsp->cmd->device->lun,
1070 		       (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1071 	memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
1072 
1073 	spin_lock_irqsave(&si->scsi_queue_lock, flags);
1074 	list_add_tail(&fsp->list, &si->scsi_pkt_queue);
1075 	spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
1076 	rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
1077 	if (unlikely(rc)) {
1078 		spin_lock_irqsave(&si->scsi_queue_lock, flags);
1079 		list_del(&fsp->list);
1080 		spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
1081 	}
1082 
1083 	return rc;
1084 }
1085 
1086 /**
1087  * get_fsp_rec_tov() - Helper function to get REC_TOV
1088  * @fsp: the FCP packet
1089  */
1090 static inline unsigned int get_fsp_rec_tov(struct fc_fcp_pkt *fsp)
1091 {
1092 	struct fc_rport *rport;
1093 	struct fc_rport_libfc_priv *rpriv;
1094 
1095 	rport = fsp->rport;
1096 	rpriv = rport->dd_data;
1097 
1098 	return rpriv->e_d_tov + REC_TOV_CONST;
1099 }
1100 
1101 /**
1102  * fc_fcp_cmd_send() - Send a FCP command
1103  * @lport: The local port to send the command on
1104  * @fsp:   The FCP packet the command is on
1105  * @resp:  The handler for the response
1106  */
1107 static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
1108 			   void (*resp)(struct fc_seq *,
1109 					struct fc_frame *fp,
1110 					void *arg))
1111 {
1112 	struct fc_frame *fp;
1113 	struct fc_seq *seq;
1114 	struct fc_rport *rport;
1115 	struct fc_rport_libfc_priv *rpriv;
1116 	const size_t len = sizeof(fsp->cdb_cmd);
1117 	int rc = 0;
1118 	unsigned int rec_tov;
1119 
1120 	if (fc_fcp_lock_pkt(fsp))
1121 		return 0;
1122 
1123 	fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd));
1124 	if (!fp) {
1125 		rc = -1;
1126 		goto unlock;
1127 	}
1128 
1129 	memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
1130 	fr_fsp(fp) = fsp;
1131 	rport = fsp->rport;
1132 	fsp->max_payload = rport->maxframe_size;
1133 	rpriv = rport->dd_data;
1134 
1135 	fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
1136 		       rpriv->local_port->port_id, FC_TYPE_FCP,
1137 		       FC_FCTL_REQ, 0);
1138 
1139 	seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
1140 				      fsp, 0);
1141 	if (!seq) {
1142 		rc = -1;
1143 		goto unlock;
1144 	}
1145 	fsp->last_pkt_time = jiffies;
1146 	fsp->seq_ptr = seq;
1147 	fc_fcp_pkt_hold(fsp);	/* hold for fc_fcp_pkt_destroy */
1148 
1149 	rec_tov = get_fsp_rec_tov(fsp);
1150 
1151 	setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
1152 
1153 	if (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED)
1154 		fc_fcp_timer_set(fsp, rec_tov);
1155 unlock:
1156 	fc_fcp_unlock_pkt(fsp);
1157 	return rc;
1158 }
1159 
1160 /**
1161  * fc_fcp_error() - Handler for FCP layer errors
1162  * @fsp: The FCP packet the error is on
1163  * @fp:	 The frame that has errored
1164  */
1165 static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1166 {
1167 	int error = PTR_ERR(fp);
1168 
1169 	if (fc_fcp_lock_pkt(fsp))
1170 		return;
1171 
1172 	if (error == -FC_EX_CLOSED) {
1173 		fc_fcp_retry_cmd(fsp);
1174 		goto unlock;
1175 	}
1176 
1177 	/*
1178 	 * clear abort pending, because the lower layer
1179 	 * decided to force completion.
1180 	 */
1181 	fsp->state &= ~FC_SRB_ABORT_PENDING;
1182 	fsp->status_code = FC_CMD_PLOGO;
1183 	fc_fcp_complete_locked(fsp);
1184 unlock:
1185 	fc_fcp_unlock_pkt(fsp);
1186 }
1187 
1188 /**
1189  * fc_fcp_pkt_abort() - Abort a fcp_pkt
1190  * @fsp:   The FCP packet to abort on
1191  *
1192  * Called to send an abort and then wait for abort completion
1193  */
1194 static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
1195 {
1196 	int rc = FAILED;
1197 
1198 	if (fc_fcp_send_abort(fsp))
1199 		return FAILED;
1200 
1201 	init_completion(&fsp->tm_done);
1202 	fsp->wait_for_comp = 1;
1203 
1204 	spin_unlock_bh(&fsp->scsi_pkt_lock);
1205 	rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1206 	spin_lock_bh(&fsp->scsi_pkt_lock);
1207 	fsp->wait_for_comp = 0;
1208 
1209 	if (!rc) {
1210 		FC_FCP_DBG(fsp, "target abort cmd  failed\n");
1211 		rc = FAILED;
1212 	} else if (fsp->state & FC_SRB_ABORTED) {
1213 		FC_FCP_DBG(fsp, "target abort cmd  passed\n");
1214 		rc = SUCCESS;
1215 		fc_fcp_complete_locked(fsp);
1216 	}
1217 
1218 	return rc;
1219 }
1220 
1221 /**
1222  * fc_lun_reset_send() - Send LUN reset command
1223  * @data: The FCP packet that identifies the LUN to be reset
1224  */
1225 static void fc_lun_reset_send(unsigned long data)
1226 {
1227 	struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1228 	struct fc_lport *lport = fsp->lp;
1229 	unsigned int rec_tov;
1230 
1231 	if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
1232 		if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
1233 			return;
1234 		if (fc_fcp_lock_pkt(fsp))
1235 			return;
1236 		rec_tov = get_fsp_rec_tov(fsp);
1237 		setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
1238 		fc_fcp_timer_set(fsp, rec_tov);
1239 		fc_fcp_unlock_pkt(fsp);
1240 	}
1241 }
1242 
1243 /**
1244  * fc_lun_reset() - Send a LUN RESET command to a device
1245  *		    and wait for the reply
1246  * @lport: The local port to sent the comand on
1247  * @fsp:   The FCP packet that identifies the LUN to be reset
1248  * @id:	   The SCSI command ID
1249  * @lun:   The LUN ID to be reset
1250  */
1251 static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
1252 			unsigned int id, unsigned int lun)
1253 {
1254 	int rc;
1255 
1256 	fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1257 	fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
1258 	int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1259 
1260 	fsp->wait_for_comp = 1;
1261 	init_completion(&fsp->tm_done);
1262 
1263 	fc_lun_reset_send((unsigned long)fsp);
1264 
1265 	/*
1266 	 * wait for completion of reset
1267 	 * after that make sure all commands are terminated
1268 	 */
1269 	rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1270 
1271 	spin_lock_bh(&fsp->scsi_pkt_lock);
1272 	fsp->state |= FC_SRB_COMPL;
1273 	spin_unlock_bh(&fsp->scsi_pkt_lock);
1274 
1275 	del_timer_sync(&fsp->timer);
1276 
1277 	spin_lock_bh(&fsp->scsi_pkt_lock);
1278 	if (fsp->seq_ptr) {
1279 		lport->tt.exch_done(fsp->seq_ptr);
1280 		fsp->seq_ptr = NULL;
1281 	}
1282 	fsp->wait_for_comp = 0;
1283 	spin_unlock_bh(&fsp->scsi_pkt_lock);
1284 
1285 	if (!rc) {
1286 		FC_SCSI_DBG(lport, "lun reset failed\n");
1287 		return FAILED;
1288 	}
1289 
1290 	/* cdb_status holds the tmf's rsp code */
1291 	if (fsp->cdb_status != FCP_TMF_CMPL)
1292 		return FAILED;
1293 
1294 	FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
1295 	fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
1296 	return SUCCESS;
1297 }
1298 
1299 /**
1300  * fc_tm_done() - Task Managment response handler
1301  * @seq: The sequence that the response is on
1302  * @fp:	 The response frame
1303  * @arg: The FCP packet the response is for
1304  */
1305 static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1306 {
1307 	struct fc_fcp_pkt *fsp = arg;
1308 	struct fc_frame_header *fh;
1309 
1310 	if (IS_ERR(fp)) {
1311 		/*
1312 		 * If there is an error just let it timeout or wait
1313 		 * for TMF to be aborted if it timedout.
1314 		 *
1315 		 * scsi-eh will escalate for when either happens.
1316 		 */
1317 		return;
1318 	}
1319 
1320 	if (fc_fcp_lock_pkt(fsp))
1321 		return;
1322 
1323 	/*
1324 	 * raced with eh timeout handler.
1325 	 */
1326 	if (!fsp->seq_ptr || !fsp->wait_for_comp) {
1327 		spin_unlock_bh(&fsp->scsi_pkt_lock);
1328 		return;
1329 	}
1330 
1331 	fh = fc_frame_header_get(fp);
1332 	if (fh->fh_type != FC_TYPE_BLS)
1333 		fc_fcp_resp(fsp, fp);
1334 	fsp->seq_ptr = NULL;
1335 	fsp->lp->tt.exch_done(seq);
1336 	fc_frame_free(fp);
1337 	fc_fcp_unlock_pkt(fsp);
1338 }
1339 
1340 /**
1341  * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
1342  * @lport: The local port to be cleaned up
1343  */
1344 static void fc_fcp_cleanup(struct fc_lport *lport)
1345 {
1346 	fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
1347 }
1348 
1349 /**
1350  * fc_fcp_timeout() - Handler for fcp_pkt timeouts
1351  * @data: The FCP packet that has timed out
1352  *
1353  * If REC is supported then just issue it and return. The REC exchange will
1354  * complete or time out and recovery can continue at that point. Otherwise,
1355  * if the response has been received without all the data it has been
1356  * ER_TIMEOUT since the response was received. If the response has not been
1357  * received we see if data was received recently. If it has been then we
1358  * continue waiting, otherwise, we abort the command.
1359  */
1360 static void fc_fcp_timeout(unsigned long data)
1361 {
1362 	struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1363 	struct fc_rport *rport = fsp->rport;
1364 	struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1365 
1366 	if (fc_fcp_lock_pkt(fsp))
1367 		return;
1368 
1369 	if (fsp->cdb_cmd.fc_tm_flags)
1370 		goto unlock;
1371 
1372 	fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
1373 
1374 	if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
1375 		fc_fcp_rec(fsp);
1376 	else if (fsp->state & FC_SRB_RCV_STATUS)
1377 		fc_fcp_complete_locked(fsp);
1378 	else
1379 		fc_fcp_recovery(fsp, FC_TIMED_OUT);
1380 	fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
1381 unlock:
1382 	fc_fcp_unlock_pkt(fsp);
1383 }
1384 
1385 /**
1386  * fc_fcp_rec() - Send a REC ELS request
1387  * @fsp: The FCP packet to send the REC request on
1388  */
1389 static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
1390 {
1391 	struct fc_lport *lport;
1392 	struct fc_frame *fp;
1393 	struct fc_rport *rport;
1394 	struct fc_rport_libfc_priv *rpriv;
1395 	unsigned int rec_tov;
1396 
1397 	lport = fsp->lp;
1398 	rport = fsp->rport;
1399 	rpriv = rport->dd_data;
1400 	if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
1401 		fsp->status_code = FC_HRD_ERROR;
1402 		fsp->io_status = 0;
1403 		fc_fcp_complete_locked(fsp);
1404 		return;
1405 	}
1406 
1407 	rec_tov = get_fsp_rec_tov(fsp);
1408 
1409 	fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec));
1410 	if (!fp)
1411 		goto retry;
1412 
1413 	fr_seq(fp) = fsp->seq_ptr;
1414 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
1415 		       rpriv->local_port->port_id, FC_TYPE_ELS,
1416 		       FC_FCTL_REQ, 0);
1417 	if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
1418 				 fc_fcp_rec_resp, fsp,
1419 				 jiffies_to_msecs(rec_tov))) {
1420 		fc_fcp_pkt_hold(fsp);		/* hold while REC outstanding */
1421 		return;
1422 	}
1423 retry:
1424 	if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1425 		fc_fcp_timer_set(fsp, rec_tov);
1426 	else
1427 		fc_fcp_recovery(fsp, FC_TIMED_OUT);
1428 }
1429 
1430 /**
1431  * fc_fcp_rec_resp() - Handler for REC ELS responses
1432  * @seq: The sequence the response is on
1433  * @fp:	 The response frame
1434  * @arg: The FCP packet the response is on
1435  *
1436  * If the response is a reject then the scsi layer will handle
1437  * the timeout. If the response is a LS_ACC then if the I/O was not completed
1438  * set the timeout and return. If the I/O was completed then complete the
1439  * exchange and tell the SCSI layer.
1440  */
1441 static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1442 {
1443 	struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
1444 	struct fc_els_rec_acc *recp;
1445 	struct fc_els_ls_rjt *rjt;
1446 	u32 e_stat;
1447 	u8 opcode;
1448 	u32 offset;
1449 	enum dma_data_direction data_dir;
1450 	enum fc_rctl r_ctl;
1451 	struct fc_rport_libfc_priv *rpriv;
1452 
1453 	if (IS_ERR(fp)) {
1454 		fc_fcp_rec_error(fsp, fp);
1455 		return;
1456 	}
1457 
1458 	if (fc_fcp_lock_pkt(fsp))
1459 		goto out;
1460 
1461 	fsp->recov_retry = 0;
1462 	opcode = fc_frame_payload_op(fp);
1463 	if (opcode == ELS_LS_RJT) {
1464 		rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1465 		switch (rjt->er_reason) {
1466 		default:
1467 			FC_FCP_DBG(fsp, "device %x unexpected REC reject "
1468 				   "reason %d expl %d\n",
1469 				   fsp->rport->port_id, rjt->er_reason,
1470 				   rjt->er_explan);
1471 			/* fall through */
1472 		case ELS_RJT_UNSUP:
1473 			FC_FCP_DBG(fsp, "device does not support REC\n");
1474 			rpriv = fsp->rport->dd_data;
1475 			/*
1476 			 * if we do not spport RECs or got some bogus
1477 			 * reason then resetup timer so we check for
1478 			 * making progress.
1479 			 */
1480 			rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
1481 			break;
1482 		case ELS_RJT_LOGIC:
1483 		case ELS_RJT_UNAB:
1484 			/*
1485 			 * If no data transfer, the command frame got dropped
1486 			 * so we just retry.  If data was transferred, we
1487 			 * lost the response but the target has no record,
1488 			 * so we abort and retry.
1489 			 */
1490 			if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
1491 			    fsp->xfer_len == 0) {
1492 				fc_fcp_retry_cmd(fsp);
1493 				break;
1494 			}
1495 			fc_fcp_recovery(fsp, FC_ERROR);
1496 			break;
1497 		}
1498 	} else if (opcode == ELS_LS_ACC) {
1499 		if (fsp->state & FC_SRB_ABORTED)
1500 			goto unlock_out;
1501 
1502 		data_dir = fsp->cmd->sc_data_direction;
1503 		recp = fc_frame_payload_get(fp, sizeof(*recp));
1504 		offset = ntohl(recp->reca_fc4value);
1505 		e_stat = ntohl(recp->reca_e_stat);
1506 
1507 		if (e_stat & ESB_ST_COMPLETE) {
1508 
1509 			/*
1510 			 * The exchange is complete.
1511 			 *
1512 			 * For output, we must've lost the response.
1513 			 * For input, all data must've been sent.
1514 			 * We lost may have lost the response
1515 			 * (and a confirmation was requested) and maybe
1516 			 * some data.
1517 			 *
1518 			 * If all data received, send SRR
1519 			 * asking for response.	 If partial data received,
1520 			 * or gaps, SRR requests data at start of gap.
1521 			 * Recovery via SRR relies on in-order-delivery.
1522 			 */
1523 			if (data_dir == DMA_TO_DEVICE) {
1524 				r_ctl = FC_RCTL_DD_CMD_STATUS;
1525 			} else if (fsp->xfer_contig_end == offset) {
1526 				r_ctl = FC_RCTL_DD_CMD_STATUS;
1527 			} else {
1528 				offset = fsp->xfer_contig_end;
1529 				r_ctl = FC_RCTL_DD_SOL_DATA;
1530 			}
1531 			fc_fcp_srr(fsp, r_ctl, offset);
1532 		} else if (e_stat & ESB_ST_SEQ_INIT) {
1533 			unsigned int rec_tov = get_fsp_rec_tov(fsp);
1534 			/*
1535 			 * The remote port has the initiative, so just
1536 			 * keep waiting for it to complete.
1537 			 */
1538 			fc_fcp_timer_set(fsp, rec_tov);
1539 		} else {
1540 
1541 			/*
1542 			 * The exchange is incomplete, we have seq. initiative.
1543 			 * Lost response with requested confirmation,
1544 			 * lost confirmation, lost transfer ready or
1545 			 * lost write data.
1546 			 *
1547 			 * For output, if not all data was received, ask
1548 			 * for transfer ready to be repeated.
1549 			 *
1550 			 * If we received or sent all the data, send SRR to
1551 			 * request response.
1552 			 *
1553 			 * If we lost a response, we may have lost some read
1554 			 * data as well.
1555 			 */
1556 			r_ctl = FC_RCTL_DD_SOL_DATA;
1557 			if (data_dir == DMA_TO_DEVICE) {
1558 				r_ctl = FC_RCTL_DD_CMD_STATUS;
1559 				if (offset < fsp->data_len)
1560 					r_ctl = FC_RCTL_DD_DATA_DESC;
1561 			} else if (offset == fsp->xfer_contig_end) {
1562 				r_ctl = FC_RCTL_DD_CMD_STATUS;
1563 			} else if (fsp->xfer_contig_end < offset) {
1564 				offset = fsp->xfer_contig_end;
1565 			}
1566 			fc_fcp_srr(fsp, r_ctl, offset);
1567 		}
1568 	}
1569 unlock_out:
1570 	fc_fcp_unlock_pkt(fsp);
1571 out:
1572 	fc_fcp_pkt_release(fsp);	/* drop hold for outstanding REC */
1573 	fc_frame_free(fp);
1574 }
1575 
1576 /**
1577  * fc_fcp_rec_error() - Handler for REC errors
1578  * @fsp: The FCP packet the error is on
1579  * @fp:	 The REC frame
1580  */
1581 static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1582 {
1583 	int error = PTR_ERR(fp);
1584 
1585 	if (fc_fcp_lock_pkt(fsp))
1586 		goto out;
1587 
1588 	switch (error) {
1589 	case -FC_EX_CLOSED:
1590 		fc_fcp_retry_cmd(fsp);
1591 		break;
1592 
1593 	default:
1594 		FC_FCP_DBG(fsp, "REC %p fid %6.6x error unexpected error %d\n",
1595 			   fsp, fsp->rport->port_id, error);
1596 		fsp->status_code = FC_CMD_PLOGO;
1597 		/* fall through */
1598 
1599 	case -FC_EX_TIMEOUT:
1600 		/*
1601 		 * Assume REC or LS_ACC was lost.
1602 		 * The exchange manager will have aborted REC, so retry.
1603 		 */
1604 		FC_FCP_DBG(fsp, "REC fid %6.6x error error %d retry %d/%d\n",
1605 			   fsp->rport->port_id, error, fsp->recov_retry,
1606 			   FC_MAX_RECOV_RETRY);
1607 		if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1608 			fc_fcp_rec(fsp);
1609 		else
1610 			fc_fcp_recovery(fsp, FC_ERROR);
1611 		break;
1612 	}
1613 	fc_fcp_unlock_pkt(fsp);
1614 out:
1615 	fc_fcp_pkt_release(fsp);	/* drop hold for outstanding REC */
1616 }
1617 
1618 /**
1619  * fc_fcp_recovery() - Handler for fcp_pkt recovery
1620  * @fsp: The FCP pkt that needs to be aborted
1621  */
1622 static void fc_fcp_recovery(struct fc_fcp_pkt *fsp, u8 code)
1623 {
1624 	fsp->status_code = code;
1625 	fsp->cdb_status = 0;
1626 	fsp->io_status = 0;
1627 	/*
1628 	 * if this fails then we let the scsi command timer fire and
1629 	 * scsi-ml escalate.
1630 	 */
1631 	fc_fcp_send_abort(fsp);
1632 }
1633 
1634 /**
1635  * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
1636  * @fsp:   The FCP packet the SRR is to be sent on
1637  * @r_ctl: The R_CTL field for the SRR request
1638  * This is called after receiving status but insufficient data, or
1639  * when expecting status but the request has timed out.
1640  */
1641 static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1642 {
1643 	struct fc_lport *lport = fsp->lp;
1644 	struct fc_rport *rport;
1645 	struct fc_rport_libfc_priv *rpriv;
1646 	struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
1647 	struct fc_seq *seq;
1648 	struct fcp_srr *srr;
1649 	struct fc_frame *fp;
1650 	u8 cdb_op;
1651 	unsigned int rec_tov;
1652 
1653 	rport = fsp->rport;
1654 	rpriv = rport->dd_data;
1655 	cdb_op = fsp->cdb_cmd.fc_cdb[0];
1656 
1657 	if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
1658 	    rpriv->rp_state != RPORT_ST_READY)
1659 		goto retry;			/* shouldn't happen */
1660 	fp = fc_fcp_frame_alloc(lport, sizeof(*srr));
1661 	if (!fp)
1662 		goto retry;
1663 
1664 	srr = fc_frame_payload_get(fp, sizeof(*srr));
1665 	memset(srr, 0, sizeof(*srr));
1666 	srr->srr_op = ELS_SRR;
1667 	srr->srr_ox_id = htons(ep->oxid);
1668 	srr->srr_rx_id = htons(ep->rxid);
1669 	srr->srr_r_ctl = r_ctl;
1670 	srr->srr_rel_off = htonl(offset);
1671 
1672 	fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
1673 		       rpriv->local_port->port_id, FC_TYPE_FCP,
1674 		       FC_FCTL_REQ, 0);
1675 
1676 	rec_tov = get_fsp_rec_tov(fsp);
1677 	seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, NULL,
1678 				      fsp, jiffies_to_msecs(rec_tov));
1679 	if (!seq)
1680 		goto retry;
1681 
1682 	fsp->recov_seq = seq;
1683 	fsp->xfer_len = offset;
1684 	fsp->xfer_contig_end = offset;
1685 	fsp->state &= ~FC_SRB_RCV_STATUS;
1686 	fc_fcp_pkt_hold(fsp);		/* hold for outstanding SRR */
1687 	return;
1688 retry:
1689 	fc_fcp_retry_cmd(fsp);
1690 }
1691 
1692 /**
1693  * fc_fcp_srr_resp() - Handler for SRR response
1694  * @seq: The sequence the SRR is on
1695  * @fp:	 The SRR frame
1696  * @arg: The FCP packet the SRR is on
1697  */
1698 static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1699 {
1700 	struct fc_fcp_pkt *fsp = arg;
1701 	struct fc_frame_header *fh;
1702 	unsigned int rec_tov;
1703 
1704 	if (IS_ERR(fp)) {
1705 		fc_fcp_srr_error(fsp, fp);
1706 		return;
1707 	}
1708 
1709 	if (fc_fcp_lock_pkt(fsp))
1710 		goto out;
1711 
1712 	fh = fc_frame_header_get(fp);
1713 	/*
1714 	 * BUG? fc_fcp_srr_error calls exch_done which would release
1715 	 * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
1716 	 * then fc_exch_timeout would be sending an abort. The exch_done
1717 	 * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
1718 	 * an abort response though.
1719 	 */
1720 	if (fh->fh_type == FC_TYPE_BLS) {
1721 		fc_fcp_unlock_pkt(fsp);
1722 		return;
1723 	}
1724 
1725 	fsp->recov_seq = NULL;
1726 	switch (fc_frame_payload_op(fp)) {
1727 	case ELS_LS_ACC:
1728 		fsp->recov_retry = 0;
1729 		rec_tov = get_fsp_rec_tov(fsp);
1730 		fc_fcp_timer_set(fsp, rec_tov);
1731 		break;
1732 	case ELS_LS_RJT:
1733 	default:
1734 		fc_fcp_recovery(fsp, FC_ERROR);
1735 		break;
1736 	}
1737 	fc_fcp_unlock_pkt(fsp);
1738 	fsp->lp->tt.exch_done(seq);
1739 out:
1740 	fc_frame_free(fp);
1741 	fc_fcp_pkt_release(fsp);	/* drop hold for outstanding SRR */
1742 }
1743 
1744 /**
1745  * fc_fcp_srr_error() - Handler for SRR errors
1746  * @fsp: The FCP packet that the SRR error is on
1747  * @fp:	 The SRR frame
1748  */
1749 static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1750 {
1751 	if (fc_fcp_lock_pkt(fsp))
1752 		goto out;
1753 	fsp->lp->tt.exch_done(fsp->recov_seq);
1754 	fsp->recov_seq = NULL;
1755 	switch (PTR_ERR(fp)) {
1756 	case -FC_EX_TIMEOUT:
1757 		if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1758 			fc_fcp_rec(fsp);
1759 		else
1760 			fc_fcp_recovery(fsp, FC_TIMED_OUT);
1761 		break;
1762 	case -FC_EX_CLOSED:			/* e.g., link failure */
1763 		/* fall through */
1764 	default:
1765 		fc_fcp_retry_cmd(fsp);
1766 		break;
1767 	}
1768 	fc_fcp_unlock_pkt(fsp);
1769 out:
1770 	fc_fcp_pkt_release(fsp);	/* drop hold for outstanding SRR */
1771 }
1772 
1773 /**
1774  * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
1775  * @lport: The local port to be checked
1776  */
1777 static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
1778 {
1779 	/* lock ? */
1780 	return (lport->state == LPORT_ST_READY) &&
1781 		lport->link_up && !lport->qfull;
1782 }
1783 
1784 /**
1785  * fc_queuecommand() - The queuecommand function of the SCSI template
1786  * @cmd:   The scsi_cmnd to be executed
1787  * @done:  The callback function to be called when the scsi_cmnd is complete
1788  *
1789  * This is the i/o strategy routine, called by the SCSI layer. This routine
1790  * is called with the host_lock held.
1791  */
1792 static int fc_queuecommand_lck(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1793 {
1794 	struct fc_lport *lport;
1795 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1796 	struct fc_fcp_pkt *fsp;
1797 	struct fc_rport_libfc_priv *rpriv;
1798 	int rval;
1799 	int rc = 0;
1800 	struct fcoe_dev_stats *stats;
1801 
1802 	lport = shost_priv(sc_cmd->device->host);
1803 
1804 	rval = fc_remote_port_chkready(rport);
1805 	if (rval) {
1806 		sc_cmd->result = rval;
1807 		done(sc_cmd);
1808 		return 0;
1809 	}
1810 	spin_unlock_irq(lport->host->host_lock);
1811 
1812 	if (!*(struct fc_remote_port **)rport->dd_data) {
1813 		/*
1814 		 * rport is transitioning from blocked/deleted to
1815 		 * online
1816 		 */
1817 		sc_cmd->result = DID_IMM_RETRY << 16;
1818 		done(sc_cmd);
1819 		goto out;
1820 	}
1821 
1822 	rpriv = rport->dd_data;
1823 
1824 	if (!fc_fcp_lport_queue_ready(lport)) {
1825 		if (lport->qfull)
1826 			fc_fcp_can_queue_ramp_down(lport);
1827 		rc = SCSI_MLQUEUE_HOST_BUSY;
1828 		goto out;
1829 	}
1830 
1831 	fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
1832 	if (fsp == NULL) {
1833 		rc = SCSI_MLQUEUE_HOST_BUSY;
1834 		goto out;
1835 	}
1836 
1837 	/*
1838 	 * build the libfc request pkt
1839 	 */
1840 	fsp->cmd = sc_cmd;	/* save the cmd */
1841 	fsp->lp = lport;	/* save the softc ptr */
1842 	fsp->rport = rport;	/* set the remote port ptr */
1843 	fsp->xfer_ddp = FC_XID_UNKNOWN;
1844 	sc_cmd->scsi_done = done;
1845 
1846 	/*
1847 	 * set up the transfer length
1848 	 */
1849 	fsp->data_len = scsi_bufflen(sc_cmd);
1850 	fsp->xfer_len = 0;
1851 
1852 	/*
1853 	 * setup the data direction
1854 	 */
1855 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1856 	if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1857 		fsp->req_flags = FC_SRB_READ;
1858 		stats->InputRequests++;
1859 		stats->InputMegabytes += fsp->data_len;
1860 	} else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
1861 		fsp->req_flags = FC_SRB_WRITE;
1862 		stats->OutputRequests++;
1863 		stats->OutputMegabytes += fsp->data_len;
1864 	} else {
1865 		fsp->req_flags = 0;
1866 		stats->ControlRequests++;
1867 	}
1868 	put_cpu();
1869 
1870 	fsp->tgt_flags = rpriv->flags;
1871 
1872 	init_timer(&fsp->timer);
1873 	fsp->timer.data = (unsigned long)fsp;
1874 
1875 	/*
1876 	 * send it to the lower layer
1877 	 * if we get -1 return then put the request in the pending
1878 	 * queue.
1879 	 */
1880 	rval = fc_fcp_pkt_send(lport, fsp);
1881 	if (rval != 0) {
1882 		fsp->state = FC_SRB_FREE;
1883 		fc_fcp_pkt_release(fsp);
1884 		rc = SCSI_MLQUEUE_HOST_BUSY;
1885 	}
1886 out:
1887 	spin_lock_irq(lport->host->host_lock);
1888 	return rc;
1889 }
1890 
1891 DEF_SCSI_QCMD(fc_queuecommand)
1892 EXPORT_SYMBOL(fc_queuecommand);
1893 
1894 /**
1895  * fc_io_compl() - Handle responses for completed commands
1896  * @fsp: The FCP packet that is complete
1897  *
1898  * Translates fcp_pkt errors to a Linux SCSI errors.
1899  * The fcp packet lock must be held when calling.
1900  */
1901 static void fc_io_compl(struct fc_fcp_pkt *fsp)
1902 {
1903 	struct fc_fcp_internal *si;
1904 	struct scsi_cmnd *sc_cmd;
1905 	struct fc_lport *lport;
1906 	unsigned long flags;
1907 
1908 	/* release outstanding ddp context */
1909 	fc_fcp_ddp_done(fsp);
1910 
1911 	fsp->state |= FC_SRB_COMPL;
1912 	if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
1913 		spin_unlock_bh(&fsp->scsi_pkt_lock);
1914 		del_timer_sync(&fsp->timer);
1915 		spin_lock_bh(&fsp->scsi_pkt_lock);
1916 	}
1917 
1918 	lport = fsp->lp;
1919 	si = fc_get_scsi_internal(lport);
1920 
1921 	/*
1922 	 * if can_queue ramp down is done then try can_queue ramp up
1923 	 * since commands are completing now.
1924 	 */
1925 	if (si->last_can_queue_ramp_down_time)
1926 		fc_fcp_can_queue_ramp_up(lport);
1927 
1928 	sc_cmd = fsp->cmd;
1929 	CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
1930 	switch (fsp->status_code) {
1931 	case FC_COMPLETE:
1932 		if (fsp->cdb_status == 0) {
1933 			/*
1934 			 * good I/O status
1935 			 */
1936 			sc_cmd->result = DID_OK << 16;
1937 			if (fsp->scsi_resid)
1938 				CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1939 		} else {
1940 			/*
1941 			 * transport level I/O was ok but scsi
1942 			 * has non zero status
1943 			 */
1944 			sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
1945 		}
1946 		break;
1947 	case FC_ERROR:
1948 		FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
1949 			   "due to FC_ERROR\n");
1950 		sc_cmd->result = DID_ERROR << 16;
1951 		break;
1952 	case FC_DATA_UNDRUN:
1953 		if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
1954 			/*
1955 			 * scsi status is good but transport level
1956 			 * underrun.
1957 			 */
1958 			if (fsp->state & FC_SRB_RCV_STATUS) {
1959 				sc_cmd->result = DID_OK << 16;
1960 			} else {
1961 				FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml"
1962 					   " due to FC_DATA_UNDRUN (trans)\n");
1963 				sc_cmd->result = DID_ERROR << 16;
1964 			}
1965 		} else {
1966 			/*
1967 			 * scsi got underrun, this is an error
1968 			 */
1969 			FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
1970 				   "due to FC_DATA_UNDRUN (scsi)\n");
1971 			CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1972 			sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1973 		}
1974 		break;
1975 	case FC_DATA_OVRRUN:
1976 		/*
1977 		 * overrun is an error
1978 		 */
1979 		FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
1980 			   "due to FC_DATA_OVRRUN\n");
1981 		sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1982 		break;
1983 	case FC_CMD_ABORTED:
1984 		FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
1985 			  "due to FC_CMD_ABORTED\n");
1986 		sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
1987 		break;
1988 	case FC_CMD_RESET:
1989 		FC_FCP_DBG(fsp, "Returning DID_RESET to scsi-ml "
1990 			   "due to FC_CMD_RESET\n");
1991 		sc_cmd->result = (DID_RESET << 16);
1992 		break;
1993 	case FC_HRD_ERROR:
1994 		FC_FCP_DBG(fsp, "Returning DID_NO_CONNECT to scsi-ml "
1995 			   "due to FC_HRD_ERROR\n");
1996 		sc_cmd->result = (DID_NO_CONNECT << 16);
1997 		break;
1998 	case FC_CRC_ERROR:
1999 		FC_FCP_DBG(fsp, "Returning DID_PARITY to scsi-ml "
2000 			   "due to FC_CRC_ERROR\n");
2001 		sc_cmd->result = (DID_PARITY << 16);
2002 		break;
2003 	case FC_TIMED_OUT:
2004 		FC_FCP_DBG(fsp, "Returning DID_BUS_BUSY to scsi-ml "
2005 			   "due to FC_TIMED_OUT\n");
2006 		sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
2007 		break;
2008 	default:
2009 		FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
2010 			   "due to unknown error\n");
2011 		sc_cmd->result = (DID_ERROR << 16);
2012 		break;
2013 	}
2014 
2015 	if (lport->state != LPORT_ST_READY && fsp->status_code != FC_COMPLETE)
2016 		sc_cmd->result = (DID_TRANSPORT_DISRUPTED << 16);
2017 
2018 	spin_lock_irqsave(&si->scsi_queue_lock, flags);
2019 	list_del(&fsp->list);
2020 	sc_cmd->SCp.ptr = NULL;
2021 	spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
2022 	sc_cmd->scsi_done(sc_cmd);
2023 
2024 	/* release ref from initial allocation in queue command */
2025 	fc_fcp_pkt_release(fsp);
2026 }
2027 
2028 /**
2029  * fc_eh_abort() - Abort a command
2030  * @sc_cmd: The SCSI command to abort
2031  *
2032  * From SCSI host template.
2033  * Send an ABTS to the target device and wait for the response.
2034  */
2035 int fc_eh_abort(struct scsi_cmnd *sc_cmd)
2036 {
2037 	struct fc_fcp_pkt *fsp;
2038 	struct fc_lport *lport;
2039 	struct fc_fcp_internal *si;
2040 	int rc = FAILED;
2041 	unsigned long flags;
2042 
2043 	lport = shost_priv(sc_cmd->device->host);
2044 	if (lport->state != LPORT_ST_READY)
2045 		return rc;
2046 	else if (!lport->link_up)
2047 		return rc;
2048 
2049 	si = fc_get_scsi_internal(lport);
2050 	spin_lock_irqsave(&si->scsi_queue_lock, flags);
2051 	fsp = CMD_SP(sc_cmd);
2052 	if (!fsp) {
2053 		/* command completed while scsi eh was setting up */
2054 		spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
2055 		return SUCCESS;
2056 	}
2057 	/* grab a ref so the fsp and sc_cmd cannot be relased from under us */
2058 	fc_fcp_pkt_hold(fsp);
2059 	spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
2060 
2061 	if (fc_fcp_lock_pkt(fsp)) {
2062 		/* completed while we were waiting for timer to be deleted */
2063 		rc = SUCCESS;
2064 		goto release_pkt;
2065 	}
2066 
2067 	rc = fc_fcp_pkt_abort(fsp);
2068 	fc_fcp_unlock_pkt(fsp);
2069 
2070 release_pkt:
2071 	fc_fcp_pkt_release(fsp);
2072 	return rc;
2073 }
2074 EXPORT_SYMBOL(fc_eh_abort);
2075 
2076 /**
2077  * fc_eh_device_reset() - Reset a single LUN
2078  * @sc_cmd: The SCSI command which identifies the device whose
2079  *	    LUN is to be reset
2080  *
2081  * Set from SCSI host template.
2082  */
2083 int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
2084 {
2085 	struct fc_lport *lport;
2086 	struct fc_fcp_pkt *fsp;
2087 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
2088 	int rc = FAILED;
2089 	int rval;
2090 
2091 	rval = fc_remote_port_chkready(rport);
2092 	if (rval)
2093 		goto out;
2094 
2095 	lport = shost_priv(sc_cmd->device->host);
2096 
2097 	if (lport->state != LPORT_ST_READY)
2098 		return rc;
2099 
2100 	FC_SCSI_DBG(lport, "Resetting rport (%6.6x)\n", rport->port_id);
2101 
2102 	fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
2103 	if (fsp == NULL) {
2104 		printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
2105 		goto out;
2106 	}
2107 
2108 	/*
2109 	 * Build the libfc request pkt. Do not set the scsi cmnd, because
2110 	 * the sc passed in is not setup for execution like when sent
2111 	 * through the queuecommand callout.
2112 	 */
2113 	fsp->lp = lport;	/* save the softc ptr */
2114 	fsp->rport = rport;	/* set the remote port ptr */
2115 
2116 	/*
2117 	 * flush outstanding commands
2118 	 */
2119 	rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
2120 	fsp->state = FC_SRB_FREE;
2121 	fc_fcp_pkt_release(fsp);
2122 
2123 out:
2124 	return rc;
2125 }
2126 EXPORT_SYMBOL(fc_eh_device_reset);
2127 
2128 /**
2129  * fc_eh_host_reset() - Reset a Scsi_Host.
2130  * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
2131  */
2132 int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
2133 {
2134 	struct Scsi_Host *shost = sc_cmd->device->host;
2135 	struct fc_lport *lport = shost_priv(shost);
2136 	unsigned long wait_tmo;
2137 
2138 	FC_SCSI_DBG(lport, "Resetting host\n");
2139 
2140 	lport->tt.lport_reset(lport);
2141 	wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
2142 	while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
2143 							       wait_tmo))
2144 		msleep(1000);
2145 
2146 	if (fc_fcp_lport_queue_ready(lport)) {
2147 		shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
2148 			     "on port (%6.6x)\n", lport->port_id);
2149 		return SUCCESS;
2150 	} else {
2151 		shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
2152 			     "port (%6.6x) is not ready.\n",
2153 			     lport->port_id);
2154 		return FAILED;
2155 	}
2156 }
2157 EXPORT_SYMBOL(fc_eh_host_reset);
2158 
2159 /**
2160  * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
2161  * @sdev: The SCSI device that identifies the SCSI host
2162  *
2163  * Configures queue depth based on host's cmd_per_len. If not set
2164  * then we use the libfc default.
2165  */
2166 int fc_slave_alloc(struct scsi_device *sdev)
2167 {
2168 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2169 
2170 	if (!rport || fc_remote_port_chkready(rport))
2171 		return -ENXIO;
2172 
2173 	if (sdev->tagged_supported)
2174 		scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
2175 	else
2176 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2177 					FC_FCP_DFLT_QUEUE_DEPTH);
2178 
2179 	return 0;
2180 }
2181 EXPORT_SYMBOL(fc_slave_alloc);
2182 
2183 /**
2184  * fc_change_queue_depth() - Change a device's queue depth
2185  * @sdev:   The SCSI device whose queue depth is to change
2186  * @qdepth: The new queue depth
2187  * @reason: The resason for the change
2188  */
2189 int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
2190 {
2191 	switch (reason) {
2192 	case SCSI_QDEPTH_DEFAULT:
2193 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2194 		break;
2195 	case SCSI_QDEPTH_QFULL:
2196 		scsi_track_queue_full(sdev, qdepth);
2197 		break;
2198 	case SCSI_QDEPTH_RAMP_UP:
2199 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2200 		break;
2201 	default:
2202 		return -EOPNOTSUPP;
2203 	}
2204 	return sdev->queue_depth;
2205 }
2206 EXPORT_SYMBOL(fc_change_queue_depth);
2207 
2208 /**
2209  * fc_change_queue_type() - Change a device's queue type
2210  * @sdev:     The SCSI device whose queue depth is to change
2211  * @tag_type: Identifier for queue type
2212  */
2213 int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
2214 {
2215 	if (sdev->tagged_supported) {
2216 		scsi_set_tag_type(sdev, tag_type);
2217 		if (tag_type)
2218 			scsi_activate_tcq(sdev, sdev->queue_depth);
2219 		else
2220 			scsi_deactivate_tcq(sdev, sdev->queue_depth);
2221 	} else
2222 		tag_type = 0;
2223 
2224 	return tag_type;
2225 }
2226 EXPORT_SYMBOL(fc_change_queue_type);
2227 
2228 /**
2229  * fc_fcp_destory() - Tear down the FCP layer for a given local port
2230  * @lport: The local port that no longer needs the FCP layer
2231  */
2232 void fc_fcp_destroy(struct fc_lport *lport)
2233 {
2234 	struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
2235 
2236 	if (!list_empty(&si->scsi_pkt_queue))
2237 		printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
2238 		       "port (%6.6x)\n", lport->port_id);
2239 
2240 	mempool_destroy(si->scsi_pkt_pool);
2241 	kfree(si);
2242 	lport->scsi_priv = NULL;
2243 }
2244 EXPORT_SYMBOL(fc_fcp_destroy);
2245 
2246 int fc_setup_fcp()
2247 {
2248 	int rc = 0;
2249 
2250 	scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
2251 					    sizeof(struct fc_fcp_pkt),
2252 					    0, SLAB_HWCACHE_ALIGN, NULL);
2253 	if (!scsi_pkt_cachep) {
2254 		printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
2255 		       "module load failed!");
2256 		rc = -ENOMEM;
2257 	}
2258 
2259 	return rc;
2260 }
2261 
2262 void fc_destroy_fcp()
2263 {
2264 	if (scsi_pkt_cachep)
2265 		kmem_cache_destroy(scsi_pkt_cachep);
2266 }
2267 
2268 /**
2269  * fc_fcp_init() - Initialize the FCP layer for a local port
2270  * @lport: The local port to initialize the exchange layer for
2271  */
2272 int fc_fcp_init(struct fc_lport *lport)
2273 {
2274 	int rc;
2275 	struct fc_fcp_internal *si;
2276 
2277 	if (!lport->tt.fcp_cmd_send)
2278 		lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
2279 
2280 	if (!lport->tt.fcp_cleanup)
2281 		lport->tt.fcp_cleanup = fc_fcp_cleanup;
2282 
2283 	if (!lport->tt.fcp_abort_io)
2284 		lport->tt.fcp_abort_io = fc_fcp_abort_io;
2285 
2286 	si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
2287 	if (!si)
2288 		return -ENOMEM;
2289 	lport->scsi_priv = si;
2290 	si->max_can_queue = lport->host->can_queue;
2291 	INIT_LIST_HEAD(&si->scsi_pkt_queue);
2292 	spin_lock_init(&si->scsi_queue_lock);
2293 
2294 	si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
2295 	if (!si->scsi_pkt_pool) {
2296 		rc = -ENOMEM;
2297 		goto free_internal;
2298 	}
2299 	return 0;
2300 
2301 free_internal:
2302 	kfree(si);
2303 	return rc;
2304 }
2305 EXPORT_SYMBOL(fc_fcp_init);
2306