2 * Copyright(c) 2007 Intel Corporation. All rights reserved.
3 * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
4 * Copyright(c) 2008 Mike Christie
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 * Maintained at www.Open-FCoE.org
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/spinlock.h>
27 #include <linux/scatterlist.h>
28 #include <linux/err.h>
29 #include <linux/crc32.h>
31 #include <scsi/scsi_tcq.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_cmnd.h>
37 #include <scsi/fc/fc_fc2.h>
39 #include <scsi/libfc.h>
40 #include <scsi/fc_encode.h>
42 MODULE_AUTHOR("Open-FCoE.org");
43 MODULE_DESCRIPTION("libfc");
44 MODULE_LICENSE("GPL v2");
46 unsigned int fc_debug_logging
;
47 module_param_named(debug_logging
, fc_debug_logging
, int, S_IRUGO
|S_IWUSR
);
48 MODULE_PARM_DESC(debug_logging
, "a bit mask of logging levels");
50 static struct kmem_cache
*scsi_pkt_cachep
;
52 /* SRB state definitions */
53 #define FC_SRB_FREE 0 /* cmd is free */
54 #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
55 #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
56 #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
57 #define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */
58 #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
59 #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
60 #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
61 #define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
63 #define FC_SRB_READ (1 << 1)
64 #define FC_SRB_WRITE (1 << 0)
67 * The SCp.ptr should be tested and set under the host lock. NULL indicates
68 * that the command has been retruned to the scsi layer.
70 #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
71 #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
72 #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
73 #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
74 #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
76 struct fc_fcp_internal
{
77 mempool_t
*scsi_pkt_pool
;
78 struct list_head scsi_pkt_queue
;
82 #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
86 * FC scsi I/O related functions
88 static void fc_fcp_recv_data(struct fc_fcp_pkt
*, struct fc_frame
*);
89 static void fc_fcp_recv(struct fc_seq
*, struct fc_frame
*, void *);
90 static void fc_fcp_resp(struct fc_fcp_pkt
*, struct fc_frame
*);
91 static void fc_fcp_complete_locked(struct fc_fcp_pkt
*);
92 static void fc_tm_done(struct fc_seq
*, struct fc_frame
*, void *);
93 static void fc_fcp_error(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
);
94 static void fc_timeout_error(struct fc_fcp_pkt
*);
95 static void fc_fcp_timeout(unsigned long data
);
96 static void fc_fcp_rec(struct fc_fcp_pkt
*);
97 static void fc_fcp_rec_error(struct fc_fcp_pkt
*, struct fc_frame
*);
98 static void fc_fcp_rec_resp(struct fc_seq
*, struct fc_frame
*, void *);
99 static void fc_io_compl(struct fc_fcp_pkt
*);
101 static void fc_fcp_srr(struct fc_fcp_pkt
*, enum fc_rctl
, u32
);
102 static void fc_fcp_srr_resp(struct fc_seq
*, struct fc_frame
*, void *);
103 static void fc_fcp_srr_error(struct fc_fcp_pkt
*, struct fc_frame
*);
106 * command status codes
108 #define FC_COMPLETE 0
109 #define FC_CMD_ABORTED 1
110 #define FC_CMD_RESET 2
111 #define FC_CMD_PLOGO 3
113 #define FC_TRANS_ERR 5
114 #define FC_DATA_OVRRUN 6
115 #define FC_DATA_UNDRUN 7
117 #define FC_HRD_ERROR 9
118 #define FC_CMD_TIME_OUT 10
121 * Error recovery timeout values.
123 #define FC_SCSI_ER_TIMEOUT (10 * HZ)
124 #define FC_SCSI_TM_TOV (10 * HZ)
125 #define FC_SCSI_REC_TOV (2 * HZ)
126 #define FC_HOST_RESET_TIMEOUT (30 * HZ)
128 #define FC_MAX_ERROR_CNT 5
129 #define FC_MAX_RECOV_RETRY 3
131 #define FC_FCP_DFLT_QUEUE_DEPTH 32
134 * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet
135 * @lp: fc lport struct
136 * @gfp: gfp flags for allocation
138 * This is used by upper layer scsi driver.
139 * Return Value : scsi_pkt structure or null on allocation failure.
140 * Context : call from process context. no locking required.
142 static struct fc_fcp_pkt
*fc_fcp_pkt_alloc(struct fc_lport
*lp
, gfp_t gfp
)
144 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
145 struct fc_fcp_pkt
*fsp
;
147 fsp
= mempool_alloc(si
->scsi_pkt_pool
, gfp
);
149 memset(fsp
, 0, sizeof(*fsp
));
151 atomic_set(&fsp
->ref_cnt
, 1);
152 init_timer(&fsp
->timer
);
153 INIT_LIST_HEAD(&fsp
->list
);
154 spin_lock_init(&fsp
->scsi_pkt_lock
);
160 * fc_fcp_pkt_release() - release hold on scsi_pkt packet
161 * @fsp: fcp packet struct
163 * This is used by upper layer scsi driver.
164 * Context : call from process and interrupt context.
165 * no locking required
167 static void fc_fcp_pkt_release(struct fc_fcp_pkt
*fsp
)
169 if (atomic_dec_and_test(&fsp
->ref_cnt
)) {
170 struct fc_fcp_internal
*si
= fc_get_scsi_internal(fsp
->lp
);
172 mempool_free(fsp
, si
->scsi_pkt_pool
);
176 static void fc_fcp_pkt_hold(struct fc_fcp_pkt
*fsp
)
178 atomic_inc(&fsp
->ref_cnt
);
182 * fc_fcp_pkt_destory() - release hold on scsi_pkt packet
183 * @seq: exchange sequence
184 * @fsp: fcp packet struct
186 * Release hold on scsi_pkt packet set to keep scsi_pkt
187 * till EM layer exch resource is not freed.
188 * Context : called from from EM layer.
189 * no locking required
191 static void fc_fcp_pkt_destroy(struct fc_seq
*seq
, void *fsp
)
193 fc_fcp_pkt_release(fsp
);
197 * fc_fcp_lock_pkt() - lock a packet and get a ref to it.
200 * We should only return error if we return a command to scsi-ml before
201 * getting a response. This can happen in cases where we send a abort, but
202 * do not wait for the response and the abort and command can be passing
203 * each other on the wire/network-layer.
205 * Note: this function locks the packet and gets a reference to allow
206 * callers to call the completion function while the lock is held and
207 * not have to worry about the packets refcount.
209 * TODO: Maybe we should just have callers grab/release the lock and
210 * have a function that they call to verify the fsp and grab a ref if
213 static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt
*fsp
)
215 spin_lock_bh(&fsp
->scsi_pkt_lock
);
216 if (fsp
->state
& FC_SRB_COMPL
) {
217 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
221 fc_fcp_pkt_hold(fsp
);
225 static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt
*fsp
)
227 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
228 fc_fcp_pkt_release(fsp
);
231 static void fc_fcp_timer_set(struct fc_fcp_pkt
*fsp
, unsigned long delay
)
233 if (!(fsp
->state
& FC_SRB_COMPL
))
234 mod_timer(&fsp
->timer
, jiffies
+ delay
);
237 static int fc_fcp_send_abort(struct fc_fcp_pkt
*fsp
)
242 fsp
->state
|= FC_SRB_ABORT_PENDING
;
243 return fsp
->lp
->tt
.seq_exch_abort(fsp
->seq_ptr
, 0);
248 * An abort isn't needed.
250 static void fc_fcp_retry_cmd(struct fc_fcp_pkt
*fsp
)
253 fsp
->lp
->tt
.exch_done(fsp
->seq_ptr
);
257 fsp
->state
&= ~FC_SRB_ABORT_PENDING
;
259 fsp
->status_code
= FC_ERROR
;
260 fc_fcp_complete_locked(fsp
);
264 * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP
265 * transfer for a read I/O indicated by the fc_fcp_pkt.
266 * @fsp: ptr to the fc_fcp_pkt
268 * This is called in exch_seq_send() when we have a newly allocated
269 * exchange with a valid exchange id to setup ddp.
273 void fc_fcp_ddp_setup(struct fc_fcp_pkt
*fsp
, u16 xid
)
281 if ((fsp
->req_flags
& FC_SRB_READ
) &&
282 (lp
->lro_enabled
) && (lp
->tt
.ddp_setup
)) {
283 if (lp
->tt
.ddp_setup(lp
, xid
, scsi_sglist(fsp
->cmd
),
284 scsi_sg_count(fsp
->cmd
)))
288 EXPORT_SYMBOL(fc_fcp_ddp_setup
);
291 * fc_fcp_ddp_done - calls to LLD's ddp_done to release any
292 * DDP related resources for this I/O if it is initialized
294 * @fsp: ptr to the fc_fcp_pkt
298 static void fc_fcp_ddp_done(struct fc_fcp_pkt
*fsp
)
305 if (fsp
->xfer_ddp
== FC_XID_UNKNOWN
)
309 if (lp
->tt
.ddp_done
) {
310 fsp
->xfer_len
= lp
->tt
.ddp_done(lp
, fsp
->xfer_ddp
);
311 fsp
->xfer_ddp
= FC_XID_UNKNOWN
;
317 * Receive SCSI data from target.
318 * Called after receiving solicited data.
320 static void fc_fcp_recv_data(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
322 struct scsi_cmnd
*sc
= fsp
->cmd
;
323 struct fc_lport
*lp
= fsp
->lp
;
324 struct fcoe_dev_stats
*stats
;
325 struct fc_frame_header
*fh
;
332 struct scatterlist
*sg
;
335 fh
= fc_frame_header_get(fp
);
336 offset
= ntohl(fh
->fh_parm_offset
);
337 start_offset
= offset
;
338 len
= fr_len(fp
) - sizeof(*fh
);
339 buf
= fc_frame_payload_get(fp
, 0);
341 /* if this I/O is ddped, update xfer len */
342 fc_fcp_ddp_done(fsp
);
344 if (offset
+ len
> fsp
->data_len
) {
345 /* this should never happen */
346 if ((fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) &&
347 fc_frame_crc_check(fp
))
349 FC_FCP_DBG(fsp
, "data received past end. len %zx offset %zx "
350 "data_len %x\n", len
, offset
, fsp
->data_len
);
351 fc_fcp_retry_cmd(fsp
);
354 if (offset
!= fsp
->xfer_len
)
355 fsp
->state
|= FC_SRB_DISCONTIG
;
358 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
)
359 crc
= crc32(~0, (u8
*) fh
, sizeof(*fh
));
361 sg
= scsi_sglist(sc
);
364 while (remaining
> 0 && sg
) {
369 if (offset
>= sg
->length
) {
370 offset
-= sg
->length
;
374 sg_bytes
= min(remaining
, sg
->length
- offset
);
377 * The scatterlist item may be bigger than PAGE_SIZE,
378 * but we are limited to mapping PAGE_SIZE at a time.
380 off
= offset
+ sg
->offset
;
381 sg_bytes
= min(sg_bytes
, (size_t)
382 (PAGE_SIZE
- (off
& ~PAGE_MASK
)));
383 page_addr
= kmap_atomic(sg_page(sg
) + (off
>> PAGE_SHIFT
),
386 break; /* XXX panic? */
388 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
)
389 crc
= crc32(crc
, buf
, sg_bytes
);
390 memcpy((char *)page_addr
+ (off
& ~PAGE_MASK
), buf
,
393 kunmap_atomic(page_addr
, KM_SOFTIRQ0
);
396 remaining
-= sg_bytes
;
397 copy_len
+= sg_bytes
;
400 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
401 buf
= fc_frame_payload_get(fp
, 0);
403 crc
= crc32(crc
, buf
+ len
, 4 - (len
% 4));
404 len
+= 4 - (len
% 4);
407 if (~crc
!= le32_to_cpu(fr_crc(fp
))) {
409 stats
= fc_lport_get_stats(lp
);
410 stats
->ErrorFrames
++;
411 /* FIXME - per cpu count, not total count! */
412 if (stats
->InvalidCRCCount
++ < 5)
413 printk(KERN_WARNING
"libfc: CRC error on data "
414 "frame for port (%6x)\n",
415 fc_host_port_id(lp
->host
));
417 * Assume the frame is total garbage.
418 * We may have copied it over the good part
420 * If so, we need to retry the entire operation.
421 * Otherwise, ignore it.
423 if (fsp
->state
& FC_SRB_DISCONTIG
)
424 fc_fcp_retry_cmd(fsp
);
429 if (fsp
->xfer_contig_end
== start_offset
)
430 fsp
->xfer_contig_end
+= copy_len
;
431 fsp
->xfer_len
+= copy_len
;
434 * In the very rare event that this data arrived after the response
435 * and completes the transfer, call the completion handler.
437 if (unlikely(fsp
->state
& FC_SRB_RCV_STATUS
) &&
438 fsp
->xfer_len
== fsp
->data_len
- fsp
->scsi_resid
)
439 fc_fcp_complete_locked(fsp
);
443 * fc_fcp_send_data() - Send SCSI data to target.
444 * @fsp: ptr to fc_fcp_pkt
445 * @sp: ptr to this sequence
446 * @offset: starting offset for this data request
447 * @seq_blen: the burst length for this data request
449 * Called after receiving a Transfer Ready data descriptor.
450 * if LLD is capable of seq offload then send down seq_blen
451 * size of data in single frame, otherwise send multiple FC
452 * frames of max FC frame payload supported by target port.
454 * Returns : 0 for success.
456 static int fc_fcp_send_data(struct fc_fcp_pkt
*fsp
, struct fc_seq
*seq
,
457 size_t offset
, size_t seq_blen
)
460 struct scsi_cmnd
*sc
;
461 struct scatterlist
*sg
;
462 struct fc_frame
*fp
= NULL
;
463 struct fc_lport
*lp
= fsp
->lp
;
468 size_t frame_offset
, fh_parm_offset
;
472 int using_sg
= lp
->sg_supp
;
475 WARN_ON(seq_blen
<= 0);
476 if (unlikely(offset
+ seq_blen
> fsp
->data_len
)) {
477 /* this should never happen */
478 FC_FCP_DBG(fsp
, "xfer-ready past end. seq_blen %zx "
479 "offset %zx\n", seq_blen
, offset
);
480 fc_fcp_send_abort(fsp
);
482 } else if (offset
!= fsp
->xfer_len
) {
483 /* Out of Order Data Request - no problem, but unexpected. */
484 FC_FCP_DBG(fsp
, "xfer-ready non-contiguous. "
485 "seq_blen %zx offset %zx\n", seq_blen
, offset
);
489 * if LLD is capable of seq_offload then set transport
490 * burst length (t_blen) to seq_blen, otherwise set t_blen
491 * to max FC frame payload previously set in fsp->max_payload.
493 t_blen
= fsp
->max_payload
;
494 if (lp
->seq_offload
) {
495 t_blen
= min(seq_blen
, (size_t)lp
->lso_max
);
496 FC_FCP_DBG(fsp
, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
497 fsp
, seq_blen
, lp
->lso_max
, t_blen
);
500 WARN_ON(t_blen
< FC_MIN_MAX_PAYLOAD
);
502 t_blen
&= ~(512 - 1); /* round down to block size */
503 WARN_ON(t_blen
< FC_MIN_MAX_PAYLOAD
); /* won't go below 256 */
506 remaining
= seq_blen
;
507 fh_parm_offset
= frame_offset
= offset
;
509 seq
= lp
->tt
.seq_start_next(seq
);
510 f_ctl
= FC_FC_REL_OFF
;
513 sg
= scsi_sglist(sc
);
515 while (remaining
> 0 && sg
) {
516 if (offset
>= sg
->length
) {
517 offset
-= sg
->length
;
522 tlen
= min(t_blen
, remaining
);
525 * TODO. Temporary workaround. fc_seq_send() can't
526 * handle odd lengths in non-linear skbs.
527 * This will be the final fragment only.
532 fp
= _fc_frame_alloc(lp
, 0);
536 fp
= fc_frame_alloc(lp
, tlen
);
540 data
= (void *)(fr_hdr(fp
)) +
541 sizeof(struct fc_frame_header
);
543 fh_parm_offset
= frame_offset
;
544 fr_max_payload(fp
) = fsp
->max_payload
;
546 sg_bytes
= min(tlen
, sg
->length
- offset
);
548 get_page(sg_page(sg
));
549 skb_fill_page_desc(fp_skb(fp
),
550 skb_shinfo(fp_skb(fp
))->nr_frags
,
551 sg_page(sg
), sg
->offset
+ offset
,
553 fp_skb(fp
)->data_len
+= sg_bytes
;
554 fr_len(fp
) += sg_bytes
;
555 fp_skb(fp
)->truesize
+= PAGE_SIZE
;
557 size_t off
= offset
+ sg
->offset
;
560 * The scatterlist item may be bigger than PAGE_SIZE,
561 * but we must not cross pages inside the kmap.
563 sg_bytes
= min(sg_bytes
, (size_t) (PAGE_SIZE
-
564 (off
& ~PAGE_MASK
)));
565 page_addr
= kmap_atomic(sg_page(sg
) +
568 memcpy(data
, (char *)page_addr
+ (off
& ~PAGE_MASK
),
570 kunmap_atomic(page_addr
, KM_SOFTIRQ0
);
574 frame_offset
+= sg_bytes
;
576 remaining
-= sg_bytes
;
578 if ((skb_shinfo(fp_skb(fp
))->nr_frags
< FC_FRAME_SG_LEN
) &&
583 * Send sequence with transfer sequence initiative in case
584 * this is last FCP frame of the sequence.
587 f_ctl
|= FC_FC_SEQ_INIT
| FC_FC_END_SEQ
;
589 ep
= fc_seq_exch(seq
);
590 fc_fill_fc_hdr(fp
, FC_RCTL_DD_SOL_DATA
, ep
->did
, ep
->sid
,
591 FC_TYPE_FCP
, f_ctl
, fh_parm_offset
);
594 * send fragment using for a sequence.
596 error
= lp
->tt
.seq_send(lp
, seq
, fp
);
598 WARN_ON(1); /* send error should be rare */
599 fc_fcp_retry_cmd(fsp
);
604 fsp
->xfer_len
+= seq_blen
; /* premature count? */
608 static void fc_fcp_abts_resp(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
611 struct fc_ba_rjt
*brp
;
612 struct fc_frame_header
*fh
;
614 fh
= fc_frame_header_get(fp
);
615 switch (fh
->fh_r_ctl
) {
619 brp
= fc_frame_payload_get(fp
, sizeof(*brp
));
620 if (brp
&& brp
->br_reason
== FC_BA_RJT_LOG_ERR
)
625 * we will let the command timeout
626 * and scsi-ml recover in this case,
627 * therefore cleared the ba_done flag.
633 fsp
->state
|= FC_SRB_ABORTED
;
634 fsp
->state
&= ~FC_SRB_ABORT_PENDING
;
636 if (fsp
->wait_for_comp
)
637 complete(&fsp
->tm_done
);
639 fc_fcp_complete_locked(fsp
);
644 * fc_fcp_reduce_can_queue() - drop can_queue
645 * @lp: lport to drop queueing for
647 * If we are getting memory allocation failures, then we may
648 * be trying to execute too many commands. We let the running
649 * commands complete or timeout, then try again with a reduced
650 * can_queue. Eventually we will hit the point where we run
651 * on all reserved structs.
653 static void fc_fcp_reduce_can_queue(struct fc_lport
*lp
)
655 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
659 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
664 can_queue
= lp
->host
->can_queue
;
668 lp
->host
->can_queue
= can_queue
;
669 shost_printk(KERN_ERR
, lp
->host
, "libfc: Could not allocate frame.\n"
670 "Reducing can_queue to %d.\n", can_queue
);
672 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
676 * fc_fcp_recv() - Reveive FCP frames
677 * @seq: The sequence the frame is on
679 * @arg: The related FCP packet
682 * Context : called from Soft IRQ context
683 * can not called holding list lock
685 static void fc_fcp_recv(struct fc_seq
*seq
, struct fc_frame
*fp
, void *arg
)
687 struct fc_fcp_pkt
*fsp
= (struct fc_fcp_pkt
*)arg
;
688 struct fc_lport
*lport
= fsp
->lp
;
689 struct fc_frame_header
*fh
;
690 struct fcp_txrdy
*dd
;
697 fh
= fc_frame_header_get(fp
);
698 r_ctl
= fh
->fh_r_ctl
;
700 if (!(lport
->state
& LPORT_ST_READY
))
702 if (fc_fcp_lock_pkt(fsp
))
704 fsp
->last_pkt_time
= jiffies
;
706 if (fh
->fh_type
== FC_TYPE_BLS
) {
707 fc_fcp_abts_resp(fsp
, fp
);
711 if (fsp
->state
& (FC_SRB_ABORTED
| FC_SRB_ABORT_PENDING
))
714 if (r_ctl
== FC_RCTL_DD_DATA_DESC
) {
716 * received XFER RDY from the target
717 * need to send data to the target
719 WARN_ON(fr_flags(fp
) & FCPHF_CRC_UNCHECKED
);
720 dd
= fc_frame_payload_get(fp
, sizeof(*dd
));
723 rc
= fc_fcp_send_data(fsp
, seq
,
724 (size_t) ntohl(dd
->ft_data_ro
),
725 (size_t) ntohl(dd
->ft_burst_len
));
727 seq
->rec_data
= fsp
->xfer_len
;
728 else if (rc
== -ENOMEM
)
729 fsp
->state
|= FC_SRB_NOMEM
;
730 } else if (r_ctl
== FC_RCTL_DD_SOL_DATA
) {
732 * received a DATA frame
733 * next we will copy the data to the system buffer
735 WARN_ON(fr_len(fp
) < sizeof(*fh
)); /* len may be 0 */
736 fc_fcp_recv_data(fsp
, fp
);
737 seq
->rec_data
= fsp
->xfer_contig_end
;
738 } else if (r_ctl
== FC_RCTL_DD_CMD_STATUS
) {
739 WARN_ON(fr_flags(fp
) & FCPHF_CRC_UNCHECKED
);
741 fc_fcp_resp(fsp
, fp
);
743 FC_FCP_DBG(fsp
, "unexpected frame. r_ctl %x\n", r_ctl
);
746 fc_fcp_unlock_pkt(fsp
);
751 fc_fcp_error(fsp
, fp
);
752 else if (rc
== -ENOMEM
)
753 fc_fcp_reduce_can_queue(lport
);
756 static void fc_fcp_resp(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
758 struct fc_frame_header
*fh
;
759 struct fcp_resp
*fc_rp
;
760 struct fcp_resp_ext
*rp_ex
;
761 struct fcp_resp_rsp_info
*fc_rp_info
;
769 fh
= (struct fc_frame_header
*)fr_hdr(fp
);
770 if (unlikely(plen
< sizeof(*fh
) + sizeof(*fc_rp
)))
773 fc_rp
= (struct fcp_resp
*)(fh
+ 1);
774 fsp
->cdb_status
= fc_rp
->fr_status
;
775 flags
= fc_rp
->fr_flags
;
776 fsp
->scsi_comp_flags
= flags
;
777 expected_len
= fsp
->data_len
;
779 /* if ddp, update xfer len */
780 fc_fcp_ddp_done(fsp
);
782 if (unlikely((flags
& ~FCP_CONF_REQ
) || fc_rp
->fr_status
)) {
783 rp_ex
= (void *)(fc_rp
+ 1);
784 if (flags
& (FCP_RSP_LEN_VAL
| FCP_SNS_LEN_VAL
)) {
785 if (plen
< sizeof(*fc_rp
) + sizeof(*rp_ex
))
787 fc_rp_info
= (struct fcp_resp_rsp_info
*)(rp_ex
+ 1);
788 if (flags
& FCP_RSP_LEN_VAL
) {
789 respl
= ntohl(rp_ex
->fr_rsp_len
);
790 if (respl
!= sizeof(*fc_rp_info
))
792 if (fsp
->wait_for_comp
) {
793 /* Abuse cdb_status for rsp code */
794 fsp
->cdb_status
= fc_rp_info
->rsp_code
;
795 complete(&fsp
->tm_done
);
797 * tmfs will not have any scsi cmd so
804 if (flags
& FCP_SNS_LEN_VAL
) {
805 snsl
= ntohl(rp_ex
->fr_sns_len
);
806 if (snsl
> SCSI_SENSE_BUFFERSIZE
)
807 snsl
= SCSI_SENSE_BUFFERSIZE
;
808 memcpy(fsp
->cmd
->sense_buffer
,
809 (char *)fc_rp_info
+ respl
, snsl
);
812 if (flags
& (FCP_RESID_UNDER
| FCP_RESID_OVER
)) {
813 if (plen
< sizeof(*fc_rp
) + sizeof(rp_ex
->fr_resid
))
815 if (flags
& FCP_RESID_UNDER
) {
816 fsp
->scsi_resid
= ntohl(rp_ex
->fr_resid
);
818 * The cmnd->underflow is the minimum number of
819 * bytes that must be transfered for this
820 * command. Provided a sense condition is not
821 * present, make sure the actual amount
822 * transferred is at least the underflow value
825 if (!(flags
& FCP_SNS_LEN_VAL
) &&
826 (fc_rp
->fr_status
== 0) &&
827 (scsi_bufflen(fsp
->cmd
) -
828 fsp
->scsi_resid
) < fsp
->cmd
->underflow
)
830 expected_len
-= fsp
->scsi_resid
;
832 fsp
->status_code
= FC_ERROR
;
836 fsp
->state
|= FC_SRB_RCV_STATUS
;
839 * Check for missing or extra data frames.
841 if (unlikely(fsp
->xfer_len
!= expected_len
)) {
842 if (fsp
->xfer_len
< expected_len
) {
844 * Some data may be queued locally,
845 * Wait a at least one jiffy to see if it is delivered.
846 * If this expires without data, we may do SRR.
848 fc_fcp_timer_set(fsp
, 2);
851 fsp
->status_code
= FC_DATA_OVRRUN
;
852 FC_FCP_DBG(fsp
, "tgt %6x xfer len %zx greater than expected, "
853 "len %x, data len %x\n",
855 fsp
->xfer_len
, expected_len
, fsp
->data_len
);
857 fc_fcp_complete_locked(fsp
);
861 FC_FCP_DBG(fsp
, "short FCP response. flags 0x%x len %u respl %u "
862 "snsl %u\n", flags
, fr_len(fp
), respl
, snsl
);
864 fsp
->status_code
= FC_ERROR
;
865 fc_fcp_complete_locked(fsp
);
869 * fc_fcp_complete_locked() - complete processing of a fcp packet
872 * This function may sleep if a timer is pending. The packet lock must be
873 * held, and the host lock must not be held.
875 static void fc_fcp_complete_locked(struct fc_fcp_pkt
*fsp
)
877 struct fc_lport
*lp
= fsp
->lp
;
882 if (fsp
->state
& FC_SRB_ABORT_PENDING
)
885 if (fsp
->state
& FC_SRB_ABORTED
) {
886 if (!fsp
->status_code
)
887 fsp
->status_code
= FC_CMD_ABORTED
;
890 * Test for transport underrun, independent of response
893 if (fsp
->xfer_len
< fsp
->data_len
&& !fsp
->io_status
&&
894 (!(fsp
->scsi_comp_flags
& FCP_RESID_UNDER
) ||
895 fsp
->xfer_len
< fsp
->data_len
- fsp
->scsi_resid
)) {
896 fsp
->status_code
= FC_DATA_UNDRUN
;
904 if (unlikely(fsp
->scsi_comp_flags
& FCP_CONF_REQ
)) {
905 struct fc_frame
*conf_frame
;
908 csp
= lp
->tt
.seq_start_next(seq
);
909 conf_frame
= fc_frame_alloc(fsp
->lp
, 0);
911 f_ctl
= FC_FC_SEQ_INIT
;
912 f_ctl
|= FC_FC_LAST_SEQ
| FC_FC_END_SEQ
;
913 ep
= fc_seq_exch(seq
);
914 fc_fill_fc_hdr(conf_frame
, FC_RCTL_DD_SOL_CTL
,
916 FC_TYPE_FCP
, f_ctl
, 0);
917 lp
->tt
.seq_send(lp
, csp
, conf_frame
);
920 lp
->tt
.exch_done(seq
);
925 static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt
*fsp
, int error
)
927 struct fc_lport
*lp
= fsp
->lp
;
930 lp
->tt
.exch_done(fsp
->seq_ptr
);
933 fsp
->status_code
= error
;
937 * fc_fcp_cleanup_each_cmd() - Cleanup active commads
941 * @error: fsp status code
943 * If lun or id is -1, they are ignored.
945 static void fc_fcp_cleanup_each_cmd(struct fc_lport
*lp
, unsigned int id
,
946 unsigned int lun
, int error
)
948 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
949 struct fc_fcp_pkt
*fsp
;
950 struct scsi_cmnd
*sc_cmd
;
953 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
955 list_for_each_entry(fsp
, &si
->scsi_pkt_queue
, list
) {
957 if (id
!= -1 && scmd_id(sc_cmd
) != id
)
960 if (lun
!= -1 && sc_cmd
->device
->lun
!= lun
)
963 fc_fcp_pkt_hold(fsp
);
964 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
966 if (!fc_fcp_lock_pkt(fsp
)) {
967 fc_fcp_cleanup_cmd(fsp
, error
);
969 fc_fcp_unlock_pkt(fsp
);
972 fc_fcp_pkt_release(fsp
);
973 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
975 * while we dropped the lock multiple pkts could
976 * have been released, so we have to start over.
980 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
983 static void fc_fcp_abort_io(struct fc_lport
*lp
)
985 fc_fcp_cleanup_each_cmd(lp
, -1, -1, FC_HRD_ERROR
);
989 * fc_fcp_pkt_send() - send a fcp packet to the lower level.
993 * This is called by upper layer protocol.
994 * Return : zero for success and -1 for failure
995 * Context : called from queuecommand which can be called from process
997 * Locks : called with the host lock and irqs disabled.
999 static int fc_fcp_pkt_send(struct fc_lport
*lp
, struct fc_fcp_pkt
*fsp
)
1001 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
1004 fsp
->cmd
->SCp
.ptr
= (char *)fsp
;
1005 fsp
->cdb_cmd
.fc_dl
= htonl(fsp
->data_len
);
1006 fsp
->cdb_cmd
.fc_flags
= fsp
->req_flags
& ~FCP_CFL_LEN_MASK
;
1008 int_to_scsilun(fsp
->cmd
->device
->lun
,
1009 (struct scsi_lun
*)fsp
->cdb_cmd
.fc_lun
);
1010 memcpy(fsp
->cdb_cmd
.fc_cdb
, fsp
->cmd
->cmnd
, fsp
->cmd
->cmd_len
);
1011 list_add_tail(&fsp
->list
, &si
->scsi_pkt_queue
);
1013 spin_unlock_irq(lp
->host
->host_lock
);
1014 rc
= lp
->tt
.fcp_cmd_send(lp
, fsp
, fc_fcp_recv
);
1015 spin_lock_irq(lp
->host
->host_lock
);
1017 list_del(&fsp
->list
);
1022 static int fc_fcp_cmd_send(struct fc_lport
*lp
, struct fc_fcp_pkt
*fsp
,
1023 void (*resp
)(struct fc_seq
*,
1024 struct fc_frame
*fp
,
1027 struct fc_frame
*fp
;
1029 struct fc_rport
*rport
;
1030 struct fc_rport_libfc_priv
*rp
;
1031 const size_t len
= sizeof(fsp
->cdb_cmd
);
1034 if (fc_fcp_lock_pkt(fsp
))
1037 fp
= fc_frame_alloc(lp
, sizeof(fsp
->cdb_cmd
));
1043 memcpy(fc_frame_payload_get(fp
, len
), &fsp
->cdb_cmd
, len
);
1046 fsp
->max_payload
= rport
->maxframe_size
;
1047 rp
= rport
->dd_data
;
1049 fc_fill_fc_hdr(fp
, FC_RCTL_DD_UNSOL_CMD
, rport
->port_id
,
1050 fc_host_port_id(rp
->local_port
->host
), FC_TYPE_FCP
,
1051 FC_FC_FIRST_SEQ
| FC_FC_END_SEQ
| FC_FC_SEQ_INIT
, 0);
1053 seq
= lp
->tt
.exch_seq_send(lp
, fp
, resp
, fc_fcp_pkt_destroy
, fsp
, 0);
1058 fsp
->last_pkt_time
= jiffies
;
1060 fc_fcp_pkt_hold(fsp
); /* hold for fc_fcp_pkt_destroy */
1062 setup_timer(&fsp
->timer
, fc_fcp_timeout
, (unsigned long)fsp
);
1063 fc_fcp_timer_set(fsp
,
1064 (fsp
->tgt_flags
& FC_RP_FLAGS_REC_SUPPORTED
) ?
1065 FC_SCSI_REC_TOV
: FC_SCSI_ER_TIMEOUT
);
1067 fc_fcp_unlock_pkt(fsp
);
1072 * transport error handler
1074 static void fc_fcp_error(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
1076 int error
= PTR_ERR(fp
);
1078 if (fc_fcp_lock_pkt(fsp
))
1081 if (error
== -FC_EX_CLOSED
) {
1082 fc_fcp_retry_cmd(fsp
);
1087 * clear abort pending, because the lower layer
1088 * decided to force completion.
1090 fsp
->state
&= ~FC_SRB_ABORT_PENDING
;
1091 fsp
->status_code
= FC_CMD_PLOGO
;
1092 fc_fcp_complete_locked(fsp
);
1094 fc_fcp_unlock_pkt(fsp
);
1098 * Scsi abort handler- calls to send an abort
1099 * and then wait for abort completion
1101 static int fc_fcp_pkt_abort(struct fc_lport
*lp
, struct fc_fcp_pkt
*fsp
)
1105 if (fc_fcp_send_abort(fsp
))
1108 init_completion(&fsp
->tm_done
);
1109 fsp
->wait_for_comp
= 1;
1111 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1112 rc
= wait_for_completion_timeout(&fsp
->tm_done
, FC_SCSI_TM_TOV
);
1113 spin_lock_bh(&fsp
->scsi_pkt_lock
);
1114 fsp
->wait_for_comp
= 0;
1117 FC_FCP_DBG(fsp
, "target abort cmd failed\n");
1119 } else if (fsp
->state
& FC_SRB_ABORTED
) {
1120 FC_FCP_DBG(fsp
, "target abort cmd passed\n");
1122 fc_fcp_complete_locked(fsp
);
1129 * Retry LUN reset after resource allocation failed.
1131 static void fc_lun_reset_send(unsigned long data
)
1133 struct fc_fcp_pkt
*fsp
= (struct fc_fcp_pkt
*)data
;
1134 struct fc_lport
*lp
= fsp
->lp
;
1135 if (lp
->tt
.fcp_cmd_send(lp
, fsp
, fc_tm_done
)) {
1136 if (fsp
->recov_retry
++ >= FC_MAX_RECOV_RETRY
)
1138 if (fc_fcp_lock_pkt(fsp
))
1140 setup_timer(&fsp
->timer
, fc_lun_reset_send
, (unsigned long)fsp
);
1141 fc_fcp_timer_set(fsp
, FC_SCSI_REC_TOV
);
1142 fc_fcp_unlock_pkt(fsp
);
1147 * Scsi device reset handler- send a LUN RESET to the device
1148 * and wait for reset reply
1150 static int fc_lun_reset(struct fc_lport
*lp
, struct fc_fcp_pkt
*fsp
,
1151 unsigned int id
, unsigned int lun
)
1155 fsp
->cdb_cmd
.fc_dl
= htonl(fsp
->data_len
);
1156 fsp
->cdb_cmd
.fc_tm_flags
= FCP_TMF_LUN_RESET
;
1157 int_to_scsilun(lun
, (struct scsi_lun
*)fsp
->cdb_cmd
.fc_lun
);
1159 fsp
->wait_for_comp
= 1;
1160 init_completion(&fsp
->tm_done
);
1162 fc_lun_reset_send((unsigned long)fsp
);
1165 * wait for completion of reset
1166 * after that make sure all commands are terminated
1168 rc
= wait_for_completion_timeout(&fsp
->tm_done
, FC_SCSI_TM_TOV
);
1170 spin_lock_bh(&fsp
->scsi_pkt_lock
);
1171 fsp
->state
|= FC_SRB_COMPL
;
1172 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1174 del_timer_sync(&fsp
->timer
);
1176 spin_lock_bh(&fsp
->scsi_pkt_lock
);
1178 lp
->tt
.exch_done(fsp
->seq_ptr
);
1179 fsp
->seq_ptr
= NULL
;
1181 fsp
->wait_for_comp
= 0;
1182 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1185 FC_SCSI_DBG(lp
, "lun reset failed\n");
1189 /* cdb_status holds the tmf's rsp code */
1190 if (fsp
->cdb_status
!= FCP_TMF_CMPL
)
1193 FC_SCSI_DBG(lp
, "lun reset to lun %u completed\n", lun
);
1194 fc_fcp_cleanup_each_cmd(lp
, id
, lun
, FC_CMD_ABORTED
);
1199 * Task Managment response handler
1201 static void fc_tm_done(struct fc_seq
*seq
, struct fc_frame
*fp
, void *arg
)
1203 struct fc_fcp_pkt
*fsp
= arg
;
1204 struct fc_frame_header
*fh
;
1208 * If there is an error just let it timeout or wait
1209 * for TMF to be aborted if it timedout.
1211 * scsi-eh will escalate for when either happens.
1216 if (fc_fcp_lock_pkt(fsp
))
1220 * raced with eh timeout handler.
1222 if (!fsp
->seq_ptr
|| !fsp
->wait_for_comp
) {
1223 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1227 fh
= fc_frame_header_get(fp
);
1228 if (fh
->fh_type
!= FC_TYPE_BLS
)
1229 fc_fcp_resp(fsp
, fp
);
1230 fsp
->seq_ptr
= NULL
;
1231 fsp
->lp
->tt
.exch_done(seq
);
1233 fc_fcp_unlock_pkt(fsp
);
1236 static void fc_fcp_cleanup(struct fc_lport
*lp
)
1238 fc_fcp_cleanup_each_cmd(lp
, -1, -1, FC_ERROR
);
1242 * fc_fcp_timeout: called by OS timer function.
1244 * The timer has been inactivated and must be reactivated if desired
1245 * using fc_fcp_timer_set().
1249 * If REC is supported, just issue it, and return. The REC exchange will
1250 * complete or time out, and recovery can continue at that point.
1252 * Otherwise, if the response has been received without all the data,
1253 * it has been ER_TIMEOUT since the response was received.
1255 * If the response has not been received,
1256 * we see if data was received recently. If it has been, we continue waiting,
1257 * otherwise, we abort the command.
1259 static void fc_fcp_timeout(unsigned long data
)
1261 struct fc_fcp_pkt
*fsp
= (struct fc_fcp_pkt
*)data
;
1262 struct fc_rport
*rport
= fsp
->rport
;
1263 struct fc_rport_libfc_priv
*rp
= rport
->dd_data
;
1265 if (fc_fcp_lock_pkt(fsp
))
1268 if (fsp
->cdb_cmd
.fc_tm_flags
)
1271 fsp
->state
|= FC_SRB_FCP_PROCESSING_TMO
;
1273 if (rp
->flags
& FC_RP_FLAGS_REC_SUPPORTED
)
1275 else if (time_after_eq(fsp
->last_pkt_time
+ (FC_SCSI_ER_TIMEOUT
/ 2),
1277 fc_fcp_timer_set(fsp
, FC_SCSI_ER_TIMEOUT
);
1278 else if (fsp
->state
& FC_SRB_RCV_STATUS
)
1279 fc_fcp_complete_locked(fsp
);
1281 fc_timeout_error(fsp
);
1282 fsp
->state
&= ~FC_SRB_FCP_PROCESSING_TMO
;
1284 fc_fcp_unlock_pkt(fsp
);
1288 * Send a REC ELS request
1290 static void fc_fcp_rec(struct fc_fcp_pkt
*fsp
)
1292 struct fc_lport
*lp
;
1293 struct fc_frame
*fp
;
1294 struct fc_rport
*rport
;
1295 struct fc_rport_libfc_priv
*rp
;
1299 rp
= rport
->dd_data
;
1300 if (!fsp
->seq_ptr
|| rp
->rp_state
!= RPORT_ST_READY
) {
1301 fsp
->status_code
= FC_HRD_ERROR
;
1303 fc_fcp_complete_locked(fsp
);
1306 fp
= fc_frame_alloc(lp
, sizeof(struct fc_els_rec
));
1310 fr_seq(fp
) = fsp
->seq_ptr
;
1311 fc_fill_fc_hdr(fp
, FC_RCTL_ELS_REQ
, rport
->port_id
,
1312 fc_host_port_id(rp
->local_port
->host
), FC_TYPE_ELS
,
1313 FC_FC_FIRST_SEQ
| FC_FC_END_SEQ
| FC_FC_SEQ_INIT
, 0);
1314 if (lp
->tt
.elsct_send(lp
, rport
->port_id
, fp
, ELS_REC
, fc_fcp_rec_resp
,
1315 fsp
, jiffies_to_msecs(FC_SCSI_REC_TOV
))) {
1316 fc_fcp_pkt_hold(fsp
); /* hold while REC outstanding */
1320 if (fsp
->recov_retry
++ < FC_MAX_RECOV_RETRY
)
1321 fc_fcp_timer_set(fsp
, FC_SCSI_REC_TOV
);
1323 fc_timeout_error(fsp
);
1327 * Receive handler for REC ELS frame
1328 * if it is a reject then let the scsi layer to handle
1329 * the timeout. if it is a LS_ACC then if the io was not completed
1330 * then set the timeout and return otherwise complete the exchange
1331 * and tell the scsi layer to restart the I/O.
1333 static void fc_fcp_rec_resp(struct fc_seq
*seq
, struct fc_frame
*fp
, void *arg
)
1335 struct fc_fcp_pkt
*fsp
= (struct fc_fcp_pkt
*)arg
;
1336 struct fc_els_rec_acc
*recp
;
1337 struct fc_els_ls_rjt
*rjt
;
1341 enum dma_data_direction data_dir
;
1343 struct fc_rport_libfc_priv
*rp
;
1346 fc_fcp_rec_error(fsp
, fp
);
1350 if (fc_fcp_lock_pkt(fsp
))
1353 fsp
->recov_retry
= 0;
1354 opcode
= fc_frame_payload_op(fp
);
1355 if (opcode
== ELS_LS_RJT
) {
1356 rjt
= fc_frame_payload_get(fp
, sizeof(*rjt
));
1357 switch (rjt
->er_reason
) {
1359 FC_FCP_DBG(fsp
, "device %x unexpected REC reject "
1360 "reason %d expl %d\n",
1361 fsp
->rport
->port_id
, rjt
->er_reason
,
1365 FC_FCP_DBG(fsp
, "device does not support REC\n");
1366 rp
= fsp
->rport
->dd_data
;
1368 * if we do not spport RECs or got some bogus
1369 * reason then resetup timer so we check for
1372 rp
->flags
&= ~FC_RP_FLAGS_REC_SUPPORTED
;
1373 fc_fcp_timer_set(fsp
, FC_SCSI_ER_TIMEOUT
);
1378 * If no data transfer, the command frame got dropped
1379 * so we just retry. If data was transferred, we
1380 * lost the response but the target has no record,
1381 * so we abort and retry.
1383 if (rjt
->er_explan
== ELS_EXPL_OXID_RXID
&&
1384 fsp
->xfer_len
== 0) {
1385 fc_fcp_retry_cmd(fsp
);
1388 fc_timeout_error(fsp
);
1391 } else if (opcode
== ELS_LS_ACC
) {
1392 if (fsp
->state
& FC_SRB_ABORTED
)
1395 data_dir
= fsp
->cmd
->sc_data_direction
;
1396 recp
= fc_frame_payload_get(fp
, sizeof(*recp
));
1397 offset
= ntohl(recp
->reca_fc4value
);
1398 e_stat
= ntohl(recp
->reca_e_stat
);
1400 if (e_stat
& ESB_ST_COMPLETE
) {
1403 * The exchange is complete.
1405 * For output, we must've lost the response.
1406 * For input, all data must've been sent.
1407 * We lost may have lost the response
1408 * (and a confirmation was requested) and maybe
1411 * If all data received, send SRR
1412 * asking for response. If partial data received,
1413 * or gaps, SRR requests data at start of gap.
1414 * Recovery via SRR relies on in-order-delivery.
1416 if (data_dir
== DMA_TO_DEVICE
) {
1417 r_ctl
= FC_RCTL_DD_CMD_STATUS
;
1418 } else if (fsp
->xfer_contig_end
== offset
) {
1419 r_ctl
= FC_RCTL_DD_CMD_STATUS
;
1421 offset
= fsp
->xfer_contig_end
;
1422 r_ctl
= FC_RCTL_DD_SOL_DATA
;
1424 fc_fcp_srr(fsp
, r_ctl
, offset
);
1425 } else if (e_stat
& ESB_ST_SEQ_INIT
) {
1428 * The remote port has the initiative, so just
1429 * keep waiting for it to complete.
1431 fc_fcp_timer_set(fsp
, FC_SCSI_REC_TOV
);
1435 * The exchange is incomplete, we have seq. initiative.
1436 * Lost response with requested confirmation,
1437 * lost confirmation, lost transfer ready or
1440 * For output, if not all data was received, ask
1441 * for transfer ready to be repeated.
1443 * If we received or sent all the data, send SRR to
1446 * If we lost a response, we may have lost some read
1449 r_ctl
= FC_RCTL_DD_SOL_DATA
;
1450 if (data_dir
== DMA_TO_DEVICE
) {
1451 r_ctl
= FC_RCTL_DD_CMD_STATUS
;
1452 if (offset
< fsp
->data_len
)
1453 r_ctl
= FC_RCTL_DD_DATA_DESC
;
1454 } else if (offset
== fsp
->xfer_contig_end
) {
1455 r_ctl
= FC_RCTL_DD_CMD_STATUS
;
1456 } else if (fsp
->xfer_contig_end
< offset
) {
1457 offset
= fsp
->xfer_contig_end
;
1459 fc_fcp_srr(fsp
, r_ctl
, offset
);
1463 fc_fcp_unlock_pkt(fsp
);
1465 fc_fcp_pkt_release(fsp
); /* drop hold for outstanding REC */
1470 * Handle error response or timeout for REC exchange.
1472 static void fc_fcp_rec_error(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
1474 int error
= PTR_ERR(fp
);
1476 if (fc_fcp_lock_pkt(fsp
))
1481 fc_fcp_retry_cmd(fsp
);
1485 FC_FCP_DBG(fsp
, "REC %p fid %x error unexpected error %d\n",
1486 fsp
, fsp
->rport
->port_id
, error
);
1487 fsp
->status_code
= FC_CMD_PLOGO
;
1490 case -FC_EX_TIMEOUT
:
1492 * Assume REC or LS_ACC was lost.
1493 * The exchange manager will have aborted REC, so retry.
1495 FC_FCP_DBG(fsp
, "REC fid %x error error %d retry %d/%d\n",
1496 fsp
->rport
->port_id
, error
, fsp
->recov_retry
,
1497 FC_MAX_RECOV_RETRY
);
1498 if (fsp
->recov_retry
++ < FC_MAX_RECOV_RETRY
)
1501 fc_timeout_error(fsp
);
1504 fc_fcp_unlock_pkt(fsp
);
1506 fc_fcp_pkt_release(fsp
); /* drop hold for outstanding REC */
1510 * Time out error routine:
1511 * abort's the I/O close the exchange and
1512 * send completion notification to scsi layer
1514 static void fc_timeout_error(struct fc_fcp_pkt
*fsp
)
1516 fsp
->status_code
= FC_CMD_TIME_OUT
;
1517 fsp
->cdb_status
= 0;
1520 * if this fails then we let the scsi command timer fire and
1523 fc_fcp_send_abort(fsp
);
1527 * Sequence retransmission request.
1528 * This is called after receiving status but insufficient data, or
1529 * when expecting status but the request has timed out.
1531 static void fc_fcp_srr(struct fc_fcp_pkt
*fsp
, enum fc_rctl r_ctl
, u32 offset
)
1533 struct fc_lport
*lp
= fsp
->lp
;
1534 struct fc_rport
*rport
;
1535 struct fc_rport_libfc_priv
*rp
;
1536 struct fc_exch
*ep
= fc_seq_exch(fsp
->seq_ptr
);
1538 struct fcp_srr
*srr
;
1539 struct fc_frame
*fp
;
1543 rp
= rport
->dd_data
;
1544 cdb_op
= fsp
->cdb_cmd
.fc_cdb
[0];
1546 if (!(rp
->flags
& FC_RP_FLAGS_RETRY
) || rp
->rp_state
!= RPORT_ST_READY
)
1547 goto retry
; /* shouldn't happen */
1548 fp
= fc_frame_alloc(lp
, sizeof(*srr
));
1552 srr
= fc_frame_payload_get(fp
, sizeof(*srr
));
1553 memset(srr
, 0, sizeof(*srr
));
1554 srr
->srr_op
= ELS_SRR
;
1555 srr
->srr_ox_id
= htons(ep
->oxid
);
1556 srr
->srr_rx_id
= htons(ep
->rxid
);
1557 srr
->srr_r_ctl
= r_ctl
;
1558 srr
->srr_rel_off
= htonl(offset
);
1560 fc_fill_fc_hdr(fp
, FC_RCTL_ELS4_REQ
, rport
->port_id
,
1561 fc_host_port_id(rp
->local_port
->host
), FC_TYPE_FCP
,
1562 FC_FC_FIRST_SEQ
| FC_FC_END_SEQ
| FC_FC_SEQ_INIT
, 0);
1564 seq
= lp
->tt
.exch_seq_send(lp
, fp
, fc_fcp_srr_resp
, NULL
,
1565 fsp
, jiffies_to_msecs(FC_SCSI_REC_TOV
));
1569 fsp
->recov_seq
= seq
;
1570 fsp
->xfer_len
= offset
;
1571 fsp
->xfer_contig_end
= offset
;
1572 fsp
->state
&= ~FC_SRB_RCV_STATUS
;
1573 fc_fcp_pkt_hold(fsp
); /* hold for outstanding SRR */
1576 fc_fcp_retry_cmd(fsp
);
1580 * Handle response from SRR.
1582 static void fc_fcp_srr_resp(struct fc_seq
*seq
, struct fc_frame
*fp
, void *arg
)
1584 struct fc_fcp_pkt
*fsp
= arg
;
1585 struct fc_frame_header
*fh
;
1588 fc_fcp_srr_error(fsp
, fp
);
1592 if (fc_fcp_lock_pkt(fsp
))
1595 fh
= fc_frame_header_get(fp
);
1597 * BUG? fc_fcp_srr_error calls exch_done which would release
1598 * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
1599 * then fc_exch_timeout would be sending an abort. The exch_done
1600 * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
1601 * an abort response though.
1603 if (fh
->fh_type
== FC_TYPE_BLS
) {
1604 fc_fcp_unlock_pkt(fsp
);
1608 fsp
->recov_seq
= NULL
;
1609 switch (fc_frame_payload_op(fp
)) {
1611 fsp
->recov_retry
= 0;
1612 fc_fcp_timer_set(fsp
, FC_SCSI_REC_TOV
);
1616 fc_timeout_error(fsp
);
1619 fc_fcp_unlock_pkt(fsp
);
1620 fsp
->lp
->tt
.exch_done(seq
);
1623 fc_fcp_pkt_release(fsp
); /* drop hold for outstanding SRR */
1626 static void fc_fcp_srr_error(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
1628 if (fc_fcp_lock_pkt(fsp
))
1630 fsp
->lp
->tt
.exch_done(fsp
->recov_seq
);
1631 fsp
->recov_seq
= NULL
;
1632 switch (PTR_ERR(fp
)) {
1633 case -FC_EX_TIMEOUT
:
1634 if (fsp
->recov_retry
++ < FC_MAX_RECOV_RETRY
)
1637 fc_timeout_error(fsp
);
1639 case -FC_EX_CLOSED
: /* e.g., link failure */
1642 fc_fcp_retry_cmd(fsp
);
1645 fc_fcp_unlock_pkt(fsp
);
1647 fc_fcp_pkt_release(fsp
); /* drop hold for outstanding SRR */
1650 static inline int fc_fcp_lport_queue_ready(struct fc_lport
*lp
)
1653 return (lp
->state
== LPORT_ST_READY
) && lp
->link_up
&& !lp
->qfull
;
1657 * fc_queuecommand - The queuecommand function of the scsi template
1658 * @cmd: struct scsi_cmnd to be executed
1659 * @done: Callback function to be called when cmd is completed
1661 * this is the i/o strategy routine, called by the scsi layer
1662 * this routine is called with holding the host_lock.
1664 int fc_queuecommand(struct scsi_cmnd
*sc_cmd
, void (*done
)(struct scsi_cmnd
*))
1666 struct fc_lport
*lp
;
1667 struct fc_rport
*rport
= starget_to_rport(scsi_target(sc_cmd
->device
));
1668 struct fc_fcp_pkt
*fsp
;
1669 struct fc_rport_libfc_priv
*rp
;
1672 struct fcoe_dev_stats
*stats
;
1674 lp
= shost_priv(sc_cmd
->device
->host
);
1676 rval
= fc_remote_port_chkready(rport
);
1678 sc_cmd
->result
= rval
;
1683 if (!*(struct fc_remote_port
**)rport
->dd_data
) {
1685 * rport is transitioning from blocked/deleted to
1688 sc_cmd
->result
= DID_IMM_RETRY
<< 16;
1693 rp
= rport
->dd_data
;
1695 if (!fc_fcp_lport_queue_ready(lp
)) {
1696 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1700 fsp
= fc_fcp_pkt_alloc(lp
, GFP_ATOMIC
);
1702 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1707 * build the libfc request pkt
1709 fsp
->cmd
= sc_cmd
; /* save the cmd */
1710 fsp
->lp
= lp
; /* save the softc ptr */
1711 fsp
->rport
= rport
; /* set the remote port ptr */
1712 fsp
->xfer_ddp
= FC_XID_UNKNOWN
;
1713 sc_cmd
->scsi_done
= done
;
1716 * set up the transfer length
1718 fsp
->data_len
= scsi_bufflen(sc_cmd
);
1722 * setup the data direction
1724 stats
= fc_lport_get_stats(lp
);
1725 if (sc_cmd
->sc_data_direction
== DMA_FROM_DEVICE
) {
1726 fsp
->req_flags
= FC_SRB_READ
;
1727 stats
->InputRequests
++;
1728 stats
->InputMegabytes
= fsp
->data_len
;
1729 } else if (sc_cmd
->sc_data_direction
== DMA_TO_DEVICE
) {
1730 fsp
->req_flags
= FC_SRB_WRITE
;
1731 stats
->OutputRequests
++;
1732 stats
->OutputMegabytes
= fsp
->data_len
;
1735 stats
->ControlRequests
++;
1738 fsp
->tgt_flags
= rp
->flags
;
1740 init_timer(&fsp
->timer
);
1741 fsp
->timer
.data
= (unsigned long)fsp
;
1744 * send it to the lower layer
1745 * if we get -1 return then put the request in the pending
1748 rval
= fc_fcp_pkt_send(lp
, fsp
);
1750 fsp
->state
= FC_SRB_FREE
;
1751 fc_fcp_pkt_release(fsp
);
1752 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1757 EXPORT_SYMBOL(fc_queuecommand
);
1760 * fc_io_compl() - Handle responses for completed commands
1763 * Translates a error to a Linux SCSI error.
1765 * The fcp packet lock must be held when calling.
1767 static void fc_io_compl(struct fc_fcp_pkt
*fsp
)
1769 struct fc_fcp_internal
*si
;
1770 struct scsi_cmnd
*sc_cmd
;
1771 struct fc_lport
*lp
;
1772 unsigned long flags
;
1774 /* release outstanding ddp context */
1775 fc_fcp_ddp_done(fsp
);
1777 fsp
->state
|= FC_SRB_COMPL
;
1778 if (!(fsp
->state
& FC_SRB_FCP_PROCESSING_TMO
)) {
1779 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1780 del_timer_sync(&fsp
->timer
);
1781 spin_lock_bh(&fsp
->scsi_pkt_lock
);
1785 si
= fc_get_scsi_internal(lp
);
1786 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
1788 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1793 * if a command timed out while we had to try and throttle IO
1794 * and it is now getting cleaned up, then we are about to
1795 * try again so clear the throttled flag incase we get more
1798 if (si
->throttled
&& fsp
->state
& FC_SRB_NOMEM
)
1804 if (!sc_cmd
->SCp
.ptr
) {
1805 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1809 CMD_SCSI_STATUS(sc_cmd
) = fsp
->cdb_status
;
1810 switch (fsp
->status_code
) {
1812 if (fsp
->cdb_status
== 0) {
1816 sc_cmd
->result
= DID_OK
<< 16;
1817 if (fsp
->scsi_resid
)
1818 CMD_RESID_LEN(sc_cmd
) = fsp
->scsi_resid
;
1819 } else if (fsp
->cdb_status
== QUEUE_FULL
) {
1820 struct scsi_device
*tmp_sdev
;
1821 struct scsi_device
*sdev
= sc_cmd
->device
;
1823 shost_for_each_device(tmp_sdev
, sdev
->host
) {
1824 if (tmp_sdev
->id
!= sdev
->id
)
1827 if (tmp_sdev
->queue_depth
> 1) {
1828 scsi_track_queue_full(tmp_sdev
,
1833 sc_cmd
->result
= (DID_OK
<< 16) | fsp
->cdb_status
;
1836 * transport level I/O was ok but scsi
1837 * has non zero status
1839 sc_cmd
->result
= (DID_OK
<< 16) | fsp
->cdb_status
;
1843 sc_cmd
->result
= DID_ERROR
<< 16;
1845 case FC_DATA_UNDRUN
:
1846 if ((fsp
->cdb_status
== 0) && !(fsp
->req_flags
& FC_SRB_READ
)) {
1848 * scsi status is good but transport level
1851 sc_cmd
->result
= (fsp
->state
& FC_SRB_RCV_STATUS
?
1852 DID_OK
: DID_ERROR
) << 16;
1855 * scsi got underrun, this is an error
1857 CMD_RESID_LEN(sc_cmd
) = fsp
->scsi_resid
;
1858 sc_cmd
->result
= (DID_ERROR
<< 16) | fsp
->cdb_status
;
1861 case FC_DATA_OVRRUN
:
1863 * overrun is an error
1865 sc_cmd
->result
= (DID_ERROR
<< 16) | fsp
->cdb_status
;
1867 case FC_CMD_ABORTED
:
1868 sc_cmd
->result
= (DID_ERROR
<< 16) | fsp
->io_status
;
1870 case FC_CMD_TIME_OUT
:
1871 sc_cmd
->result
= (DID_BUS_BUSY
<< 16) | fsp
->io_status
;
1874 sc_cmd
->result
= (DID_RESET
<< 16);
1877 sc_cmd
->result
= (DID_NO_CONNECT
<< 16);
1880 sc_cmd
->result
= (DID_ERROR
<< 16);
1884 list_del(&fsp
->list
);
1885 sc_cmd
->SCp
.ptr
= NULL
;
1886 sc_cmd
->scsi_done(sc_cmd
);
1887 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1889 /* release ref from initial allocation in queue command */
1890 fc_fcp_pkt_release(fsp
);
1894 * fc_fcp_complete() - complete processing of a fcp packet
1897 * This function may sleep if a fsp timer is pending.
1898 * The host lock must not be held by caller.
1900 void fc_fcp_complete(struct fc_fcp_pkt
*fsp
)
1902 if (fc_fcp_lock_pkt(fsp
))
1905 fc_fcp_complete_locked(fsp
);
1906 fc_fcp_unlock_pkt(fsp
);
1908 EXPORT_SYMBOL(fc_fcp_complete
);
1911 * fc_eh_abort() - Abort a command
1912 * @sc_cmd: scsi command to abort
1914 * From scsi host template.
1915 * send ABTS to the target device and wait for the response
1916 * sc_cmd is the pointer to the command to be aborted.
1918 int fc_eh_abort(struct scsi_cmnd
*sc_cmd
)
1920 struct fc_fcp_pkt
*fsp
;
1921 struct fc_lport
*lp
;
1923 unsigned long flags
;
1925 lp
= shost_priv(sc_cmd
->device
->host
);
1926 if (lp
->state
!= LPORT_ST_READY
)
1928 else if (!lp
->link_up
)
1931 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
1932 fsp
= CMD_SP(sc_cmd
);
1934 /* command completed while scsi eh was setting up */
1935 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1938 /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
1939 fc_fcp_pkt_hold(fsp
);
1940 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1942 if (fc_fcp_lock_pkt(fsp
)) {
1943 /* completed while we were waiting for timer to be deleted */
1948 rc
= fc_fcp_pkt_abort(lp
, fsp
);
1949 fc_fcp_unlock_pkt(fsp
);
1952 fc_fcp_pkt_release(fsp
);
1955 EXPORT_SYMBOL(fc_eh_abort
);
1958 * fc_eh_device_reset() Reset a single LUN
1959 * @sc_cmd: scsi command
1961 * Set from scsi host template to send tm cmd to the target and wait for the
1964 int fc_eh_device_reset(struct scsi_cmnd
*sc_cmd
)
1966 struct fc_lport
*lp
;
1967 struct fc_fcp_pkt
*fsp
;
1968 struct fc_rport
*rport
= starget_to_rport(scsi_target(sc_cmd
->device
));
1970 struct fc_rport_libfc_priv
*rp
;
1973 rval
= fc_remote_port_chkready(rport
);
1977 rp
= rport
->dd_data
;
1978 lp
= shost_priv(sc_cmd
->device
->host
);
1980 if (lp
->state
!= LPORT_ST_READY
)
1983 FC_SCSI_DBG(lp
, "Resetting rport (%6x)\n", rport
->port_id
);
1985 fsp
= fc_fcp_pkt_alloc(lp
, GFP_NOIO
);
1987 printk(KERN_WARNING
"libfc: could not allocate scsi_pkt\n");
1988 sc_cmd
->result
= DID_NO_CONNECT
<< 16;
1993 * Build the libfc request pkt. Do not set the scsi cmnd, because
1994 * the sc passed in is not setup for execution like when sent
1995 * through the queuecommand callout.
1997 fsp
->lp
= lp
; /* save the softc ptr */
1998 fsp
->rport
= rport
; /* set the remote port ptr */
2001 * flush outstanding commands
2003 rc
= fc_lun_reset(lp
, fsp
, scmd_id(sc_cmd
), sc_cmd
->device
->lun
);
2004 fsp
->state
= FC_SRB_FREE
;
2005 fc_fcp_pkt_release(fsp
);
2010 EXPORT_SYMBOL(fc_eh_device_reset
);
2013 * fc_eh_host_reset() - The reset function will reset the ports on the host.
2014 * @sc_cmd: scsi command
2016 int fc_eh_host_reset(struct scsi_cmnd
*sc_cmd
)
2018 struct Scsi_Host
*shost
= sc_cmd
->device
->host
;
2019 struct fc_lport
*lp
= shost_priv(shost
);
2020 unsigned long wait_tmo
;
2022 FC_SCSI_DBG(lp
, "Resetting host\n");
2024 lp
->tt
.lport_reset(lp
);
2025 wait_tmo
= jiffies
+ FC_HOST_RESET_TIMEOUT
;
2026 while (!fc_fcp_lport_queue_ready(lp
) && time_before(jiffies
, wait_tmo
))
2029 if (fc_fcp_lport_queue_ready(lp
)) {
2030 shost_printk(KERN_INFO
, shost
, "libfc: Host reset succeeded "
2031 "on port (%6x)\n", fc_host_port_id(lp
->host
));
2034 shost_printk(KERN_INFO
, shost
, "libfc: Host reset failed, "
2035 "port (%6x) is not ready.\n",
2036 fc_host_port_id(lp
->host
));
2040 EXPORT_SYMBOL(fc_eh_host_reset
);
2043 * fc_slave_alloc() - configure queue depth
2044 * @sdev: scsi device
2046 * Configures queue depth based on host's cmd_per_len. If not set
2047 * then we use the libfc default.
2049 int fc_slave_alloc(struct scsi_device
*sdev
)
2051 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
2053 if (!rport
|| fc_remote_port_chkready(rport
))
2056 if (sdev
->tagged_supported
)
2057 scsi_activate_tcq(sdev
, FC_FCP_DFLT_QUEUE_DEPTH
);
2059 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
),
2060 FC_FCP_DFLT_QUEUE_DEPTH
);
2064 EXPORT_SYMBOL(fc_slave_alloc
);
2066 int fc_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
2068 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), qdepth
);
2069 return sdev
->queue_depth
;
2071 EXPORT_SYMBOL(fc_change_queue_depth
);
2073 int fc_change_queue_type(struct scsi_device
*sdev
, int tag_type
)
2075 if (sdev
->tagged_supported
) {
2076 scsi_set_tag_type(sdev
, tag_type
);
2078 scsi_activate_tcq(sdev
, sdev
->queue_depth
);
2080 scsi_deactivate_tcq(sdev
, sdev
->queue_depth
);
2086 EXPORT_SYMBOL(fc_change_queue_type
);
2088 void fc_fcp_destroy(struct fc_lport
*lp
)
2090 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
2092 if (!list_empty(&si
->scsi_pkt_queue
))
2093 printk(KERN_ERR
"libfc: Leaked SCSI packets when destroying "
2094 "port (%6x)\n", fc_host_port_id(lp
->host
));
2096 mempool_destroy(si
->scsi_pkt_pool
);
2098 lp
->scsi_priv
= NULL
;
2100 EXPORT_SYMBOL(fc_fcp_destroy
);
2102 int fc_fcp_init(struct fc_lport
*lp
)
2105 struct fc_fcp_internal
*si
;
2107 if (!lp
->tt
.fcp_cmd_send
)
2108 lp
->tt
.fcp_cmd_send
= fc_fcp_cmd_send
;
2110 if (!lp
->tt
.fcp_cleanup
)
2111 lp
->tt
.fcp_cleanup
= fc_fcp_cleanup
;
2113 if (!lp
->tt
.fcp_abort_io
)
2114 lp
->tt
.fcp_abort_io
= fc_fcp_abort_io
;
2116 si
= kzalloc(sizeof(struct fc_fcp_internal
), GFP_KERNEL
);
2120 INIT_LIST_HEAD(&si
->scsi_pkt_queue
);
2122 si
->scsi_pkt_pool
= mempool_create_slab_pool(2, scsi_pkt_cachep
);
2123 if (!si
->scsi_pkt_pool
) {
2133 EXPORT_SYMBOL(fc_fcp_init
);
2135 static int __init
libfc_init(void)
2139 scsi_pkt_cachep
= kmem_cache_create("libfc_fcp_pkt",
2140 sizeof(struct fc_fcp_pkt
),
2141 0, SLAB_HWCACHE_ALIGN
, NULL
);
2142 if (scsi_pkt_cachep
== NULL
) {
2143 printk(KERN_ERR
"libfc: Unable to allocate SRB cache, "
2144 "module load failed!");
2148 rc
= fc_setup_exch_mgr();
2150 goto destroy_pkt_cache
;
2152 rc
= fc_setup_rport();
2158 fc_destroy_exch_mgr();
2160 kmem_cache_destroy(scsi_pkt_cachep
);
2164 static void __exit
libfc_exit(void)
2166 kmem_cache_destroy(scsi_pkt_cachep
);
2167 fc_destroy_exch_mgr();
2171 module_init(libfc_init
);
2172 module_exit(libfc_exit
);