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