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