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