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