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 static int fc_fcp_debug
;
48 #define FC_DEBUG_FCP(fmt...) \
54 static struct kmem_cache
*scsi_pkt_cachep
;
56 /* SRB state definitions */
57 #define FC_SRB_FREE 0 /* cmd is free */
58 #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
59 #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
60 #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
61 #define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */
62 #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
63 #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
64 #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
65 #define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
67 #define FC_SRB_READ (1 << 1)
68 #define FC_SRB_WRITE (1 << 0)
71 * The SCp.ptr should be tested and set under the host lock. NULL indicates
72 * that the command has been retruned to the scsi layer.
74 #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
75 #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
76 #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
77 #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
78 #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
80 struct fc_fcp_internal
{
81 mempool_t
*scsi_pkt_pool
;
82 struct list_head scsi_pkt_queue
;
86 #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
90 * FC scsi I/O related functions
92 static void fc_fcp_recv_data(struct fc_fcp_pkt
*, struct fc_frame
*);
93 static void fc_fcp_recv(struct fc_seq
*, struct fc_frame
*, void *);
94 static void fc_fcp_resp(struct fc_fcp_pkt
*, struct fc_frame
*);
95 static void fc_fcp_complete_locked(struct fc_fcp_pkt
*);
96 static void fc_tm_done(struct fc_seq
*, struct fc_frame
*, void *);
97 static void fc_fcp_error(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
);
98 static void fc_timeout_error(struct fc_fcp_pkt
*);
99 static void fc_fcp_timeout(unsigned long data
);
100 static void fc_fcp_rec(struct fc_fcp_pkt
*);
101 static void fc_fcp_rec_error(struct fc_fcp_pkt
*, struct fc_frame
*);
102 static void fc_fcp_rec_resp(struct fc_seq
*, struct fc_frame
*, void *);
103 static void fc_io_compl(struct fc_fcp_pkt
*);
105 static void fc_fcp_srr(struct fc_fcp_pkt
*, enum fc_rctl
, u32
);
106 static void fc_fcp_srr_resp(struct fc_seq
*, struct fc_frame
*, void *);
107 static void fc_fcp_srr_error(struct fc_fcp_pkt
*, struct fc_frame
*);
110 * command status codes
112 #define FC_COMPLETE 0
113 #define FC_CMD_ABORTED 1
114 #define FC_CMD_RESET 2
115 #define FC_CMD_PLOGO 3
117 #define FC_TRANS_ERR 5
118 #define FC_DATA_OVRRUN 6
119 #define FC_DATA_UNDRUN 7
121 #define FC_HRD_ERROR 9
122 #define FC_CMD_TIME_OUT 10
125 * Error recovery timeout values.
127 #define FC_SCSI_ER_TIMEOUT (10 * HZ)
128 #define FC_SCSI_TM_TOV (10 * HZ)
129 #define FC_SCSI_REC_TOV (2 * HZ)
130 #define FC_HOST_RESET_TIMEOUT (30 * HZ)
132 #define FC_MAX_ERROR_CNT 5
133 #define FC_MAX_RECOV_RETRY 3
135 #define FC_FCP_DFLT_QUEUE_DEPTH 32
138 * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet
139 * @lp: fc lport struct
140 * @gfp: gfp flags for allocation
142 * This is used by upper layer scsi driver.
143 * Return Value : scsi_pkt structure or null on allocation failure.
144 * Context : call from process context. no locking required.
146 static struct fc_fcp_pkt
*fc_fcp_pkt_alloc(struct fc_lport
*lp
, gfp_t gfp
)
148 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
149 struct fc_fcp_pkt
*fsp
;
151 fsp
= mempool_alloc(si
->scsi_pkt_pool
, gfp
);
153 memset(fsp
, 0, sizeof(*fsp
));
155 atomic_set(&fsp
->ref_cnt
, 1);
156 init_timer(&fsp
->timer
);
157 INIT_LIST_HEAD(&fsp
->list
);
158 spin_lock_init(&fsp
->scsi_pkt_lock
);
164 * fc_fcp_pkt_release() - release hold on scsi_pkt packet
165 * @fsp: fcp packet struct
167 * This is used by upper layer scsi driver.
168 * Context : call from process and interrupt context.
169 * no locking required
171 static void fc_fcp_pkt_release(struct fc_fcp_pkt
*fsp
)
173 if (atomic_dec_and_test(&fsp
->ref_cnt
)) {
174 struct fc_fcp_internal
*si
= fc_get_scsi_internal(fsp
->lp
);
176 mempool_free(fsp
, si
->scsi_pkt_pool
);
180 static void fc_fcp_pkt_hold(struct fc_fcp_pkt
*fsp
)
182 atomic_inc(&fsp
->ref_cnt
);
186 * fc_fcp_pkt_destory() - release hold on scsi_pkt packet
187 * @seq: exchange sequence
188 * @fsp: fcp packet struct
190 * Release hold on scsi_pkt packet set to keep scsi_pkt
191 * till EM layer exch resource is not freed.
192 * Context : called from from EM layer.
193 * no locking required
195 static void fc_fcp_pkt_destroy(struct fc_seq
*seq
, void *fsp
)
197 fc_fcp_pkt_release(fsp
);
201 * fc_fcp_lock_pkt() - lock a packet and get a ref to it.
204 * We should only return error if we return a command to scsi-ml before
205 * getting a response. This can happen in cases where we send a abort, but
206 * do not wait for the response and the abort and command can be passing
207 * each other on the wire/network-layer.
209 * Note: this function locks the packet and gets a reference to allow
210 * callers to call the completion function while the lock is held and
211 * not have to worry about the packets refcount.
213 * TODO: Maybe we should just have callers grab/release the lock and
214 * have a function that they call to verify the fsp and grab a ref if
217 static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt
*fsp
)
219 spin_lock_bh(&fsp
->scsi_pkt_lock
);
220 if (fsp
->state
& FC_SRB_COMPL
) {
221 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
225 fc_fcp_pkt_hold(fsp
);
229 static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt
*fsp
)
231 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
232 fc_fcp_pkt_release(fsp
);
235 static void fc_fcp_timer_set(struct fc_fcp_pkt
*fsp
, unsigned long delay
)
237 if (!(fsp
->state
& FC_SRB_COMPL
))
238 mod_timer(&fsp
->timer
, jiffies
+ delay
);
241 static int fc_fcp_send_abort(struct fc_fcp_pkt
*fsp
)
246 fsp
->state
|= FC_SRB_ABORT_PENDING
;
247 return fsp
->lp
->tt
.seq_exch_abort(fsp
->seq_ptr
, 0);
252 * An abort isn't needed.
254 static void fc_fcp_retry_cmd(struct fc_fcp_pkt
*fsp
)
257 fsp
->lp
->tt
.exch_done(fsp
->seq_ptr
);
261 fsp
->state
&= ~FC_SRB_ABORT_PENDING
;
263 fsp
->status_code
= FC_ERROR
;
264 fc_fcp_complete_locked(fsp
);
268 * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP
269 * transfer for a read I/O indicated by the fc_fcp_pkt.
270 * @fsp: ptr to the fc_fcp_pkt
272 * This is called in exch_seq_send() when we have a newly allocated
273 * exchange with a valid exchange id to setup ddp.
277 void fc_fcp_ddp_setup(struct fc_fcp_pkt
*fsp
, u16 xid
)
285 if ((fsp
->req_flags
& FC_SRB_READ
) &&
286 (lp
->lro_enabled
) && (lp
->tt
.ddp_setup
)) {
287 if (lp
->tt
.ddp_setup(lp
, xid
, scsi_sglist(fsp
->cmd
),
288 scsi_sg_count(fsp
->cmd
)))
292 EXPORT_SYMBOL(fc_fcp_ddp_setup
);
295 * fc_fcp_ddp_done - calls to LLD's ddp_done to release any
296 * DDP related resources for this I/O if it is initialized
298 * @fsp: ptr to the fc_fcp_pkt
302 static void fc_fcp_ddp_done(struct fc_fcp_pkt
*fsp
)
310 if (fsp
->xfer_ddp
&& lp
->tt
.ddp_done
) {
311 fsp
->xfer_len
= lp
->tt
.ddp_done(lp
, fsp
->xfer_ddp
);
318 * Receive SCSI data from target.
319 * Called after receiving solicited data.
321 static void fc_fcp_recv_data(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
323 struct scsi_cmnd
*sc
= fsp
->cmd
;
324 struct fc_lport
*lp
= fsp
->lp
;
325 struct fcoe_dev_stats
*stats
;
326 struct fc_frame_header
*fh
;
333 struct scatterlist
*sg
;
336 fh
= fc_frame_header_get(fp
);
337 offset
= ntohl(fh
->fh_parm_offset
);
338 start_offset
= offset
;
339 len
= fr_len(fp
) - sizeof(*fh
);
340 buf
= fc_frame_payload_get(fp
, 0);
342 /* if this I/O is ddped, update xfer len */
343 fc_fcp_ddp_done(fsp
);
345 if (offset
+ len
> fsp
->data_len
) {
346 /* this should never happen */
347 if ((fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) &&
348 fc_frame_crc_check(fp
))
350 FC_DEBUG_FCP("data received past end. len %zx offset %zx "
351 "data_len %x\n", len
, offset
, fsp
->data_len
);
352 fc_fcp_retry_cmd(fsp
);
355 if (offset
!= fsp
->xfer_len
)
356 fsp
->state
|= FC_SRB_DISCONTIG
;
359 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
)
360 crc
= crc32(~0, (u8
*) fh
, sizeof(*fh
));
362 sg
= scsi_sglist(sc
);
365 while (remaining
> 0 && sg
) {
370 if (offset
>= sg
->length
) {
371 offset
-= sg
->length
;
375 sg_bytes
= min(remaining
, sg
->length
- offset
);
378 * The scatterlist item may be bigger than PAGE_SIZE,
379 * but we are limited to mapping PAGE_SIZE at a time.
381 off
= offset
+ sg
->offset
;
382 sg_bytes
= min(sg_bytes
, (size_t)
383 (PAGE_SIZE
- (off
& ~PAGE_MASK
)));
384 page_addr
= kmap_atomic(sg_page(sg
) + (off
>> PAGE_SHIFT
),
387 break; /* XXX panic? */
389 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
)
390 crc
= crc32(crc
, buf
, sg_bytes
);
391 memcpy((char *)page_addr
+ (off
& ~PAGE_MASK
), buf
,
394 kunmap_atomic(page_addr
, KM_SOFTIRQ0
);
397 remaining
-= sg_bytes
;
398 copy_len
+= sg_bytes
;
401 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
402 buf
= fc_frame_payload_get(fp
, 0);
404 crc
= crc32(crc
, buf
+ len
, 4 - (len
% 4));
405 len
+= 4 - (len
% 4);
408 if (~crc
!= le32_to_cpu(fr_crc(fp
))) {
410 stats
= fc_lport_get_stats(lp
);
411 stats
->ErrorFrames
++;
412 /* FIXME - per cpu count, not total count! */
413 if (stats
->InvalidCRCCount
++ < 5)
414 printk(KERN_WARNING
"CRC error on data 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_DEBUG_FCP("xfer-ready past end. seq_blen %zx offset %zx\n",
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_DEBUG_FCP("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_DEBUG_FCP("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
;
514 * If a get_page()/put_page() will fail, don't use sg lists
515 * in the fc_frame structure.
517 * The put_page() may be long after the I/O has completed
518 * in the case of FCoE, since the network driver does it
519 * via free_skb(). See the test in free_pages_check().
521 * Test this case with 'dd </dev/zero >/dev/st0 bs=64k'.
524 for (sg
= scsi_sglist(sc
); sg
; sg
= sg_next(sg
)) {
525 if (page_count(sg_page(sg
)) == 0 ||
526 (sg_page(sg
)->flags
& (1 << PG_lru
|
540 sg
= scsi_sglist(sc
);
542 while (remaining
> 0 && sg
) {
543 if (offset
>= sg
->length
) {
544 offset
-= sg
->length
;
549 tlen
= min(t_blen
, remaining
);
552 * TODO. Temporary workaround. fc_seq_send() can't
553 * handle odd lengths in non-linear skbs.
554 * This will be the final fragment only.
559 fp
= _fc_frame_alloc(lp
, 0);
563 fp
= fc_frame_alloc(lp
, tlen
);
567 data
= (void *)(fr_hdr(fp
)) +
568 sizeof(struct fc_frame_header
);
570 fh_parm_offset
= frame_offset
;
571 fr_max_payload(fp
) = fsp
->max_payload
;
573 sg_bytes
= min(tlen
, sg
->length
- offset
);
575 WARN_ON(skb_shinfo(fp_skb(fp
))->nr_frags
>
577 get_page(sg_page(sg
));
578 skb_fill_page_desc(fp_skb(fp
),
579 skb_shinfo(fp_skb(fp
))->nr_frags
,
580 sg_page(sg
), sg
->offset
+ offset
,
582 fp_skb(fp
)->data_len
+= sg_bytes
;
583 fr_len(fp
) += sg_bytes
;
584 fp_skb(fp
)->truesize
+= PAGE_SIZE
;
586 size_t off
= offset
+ sg
->offset
;
589 * The scatterlist item may be bigger than PAGE_SIZE,
590 * but we must not cross pages inside the kmap.
592 sg_bytes
= min(sg_bytes
, (size_t) (PAGE_SIZE
-
593 (off
& ~PAGE_MASK
)));
594 page_addr
= kmap_atomic(sg_page(sg
) +
597 memcpy(data
, (char *)page_addr
+ (off
& ~PAGE_MASK
),
599 kunmap_atomic(page_addr
, KM_SOFTIRQ0
);
603 frame_offset
+= sg_bytes
;
605 remaining
-= sg_bytes
;
611 * Send sequence with transfer sequence initiative in case
612 * this is last FCP frame of the sequence.
615 f_ctl
|= FC_FC_SEQ_INIT
| FC_FC_END_SEQ
;
617 ep
= fc_seq_exch(seq
);
618 fc_fill_fc_hdr(fp
, FC_RCTL_DD_SOL_DATA
, ep
->did
, ep
->sid
,
619 FC_TYPE_FCP
, f_ctl
, fh_parm_offset
);
622 * send fragment using for a sequence.
624 error
= lp
->tt
.seq_send(lp
, seq
, fp
);
626 WARN_ON(1); /* send error should be rare */
627 fc_fcp_retry_cmd(fsp
);
632 fsp
->xfer_len
+= seq_blen
; /* premature count? */
636 static void fc_fcp_abts_resp(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
639 struct fc_ba_rjt
*brp
;
640 struct fc_frame_header
*fh
;
642 fh
= fc_frame_header_get(fp
);
643 switch (fh
->fh_r_ctl
) {
647 brp
= fc_frame_payload_get(fp
, sizeof(*brp
));
648 if (brp
&& brp
->br_reason
== FC_BA_RJT_LOG_ERR
)
653 * we will let the command timeout
654 * and scsi-ml recover in this case,
655 * therefore cleared the ba_done flag.
661 fsp
->state
|= FC_SRB_ABORTED
;
662 fsp
->state
&= ~FC_SRB_ABORT_PENDING
;
664 if (fsp
->wait_for_comp
)
665 complete(&fsp
->tm_done
);
667 fc_fcp_complete_locked(fsp
);
672 * fc_fcp_reduce_can_queue() - drop can_queue
673 * @lp: lport to drop queueing for
675 * If we are getting memory allocation failures, then we may
676 * be trying to execute too many commands. We let the running
677 * commands complete or timeout, then try again with a reduced
678 * can_queue. Eventually we will hit the point where we run
679 * on all reserved structs.
681 static void fc_fcp_reduce_can_queue(struct fc_lport
*lp
)
683 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
687 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
692 can_queue
= lp
->host
->can_queue
;
696 lp
->host
->can_queue
= can_queue
;
697 shost_printk(KERN_ERR
, lp
->host
, "Could not allocate frame.\n"
698 "Reducing can_queue to %d.\n", can_queue
);
700 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
704 * fc_fcp_recv() - Reveive FCP frames
705 * @seq: The sequence the frame is on
707 * @arg: The related FCP packet
710 * Context : called from Soft IRQ context
711 * can not called holding list lock
713 static void fc_fcp_recv(struct fc_seq
*seq
, struct fc_frame
*fp
, void *arg
)
715 struct fc_fcp_pkt
*fsp
= (struct fc_fcp_pkt
*)arg
;
716 struct fc_lport
*lport
= fsp
->lp
;
717 struct fc_frame_header
*fh
;
718 struct fcp_txrdy
*dd
;
725 fh
= fc_frame_header_get(fp
);
726 r_ctl
= fh
->fh_r_ctl
;
728 if (!(lport
->state
& LPORT_ST_READY
))
730 if (fc_fcp_lock_pkt(fsp
))
732 fsp
->last_pkt_time
= jiffies
;
734 if (fh
->fh_type
== FC_TYPE_BLS
) {
735 fc_fcp_abts_resp(fsp
, fp
);
739 if (fsp
->state
& (FC_SRB_ABORTED
| FC_SRB_ABORT_PENDING
))
742 if (r_ctl
== FC_RCTL_DD_DATA_DESC
) {
744 * received XFER RDY from the target
745 * need to send data to the target
747 WARN_ON(fr_flags(fp
) & FCPHF_CRC_UNCHECKED
);
748 dd
= fc_frame_payload_get(fp
, sizeof(*dd
));
751 rc
= fc_fcp_send_data(fsp
, seq
,
752 (size_t) ntohl(dd
->ft_data_ro
),
753 (size_t) ntohl(dd
->ft_burst_len
));
755 seq
->rec_data
= fsp
->xfer_len
;
756 else if (rc
== -ENOMEM
)
757 fsp
->state
|= FC_SRB_NOMEM
;
758 } else if (r_ctl
== FC_RCTL_DD_SOL_DATA
) {
760 * received a DATA frame
761 * next we will copy the data to the system buffer
763 WARN_ON(fr_len(fp
) < sizeof(*fh
)); /* len may be 0 */
764 fc_fcp_recv_data(fsp
, fp
);
765 seq
->rec_data
= fsp
->xfer_contig_end
;
766 } else if (r_ctl
== FC_RCTL_DD_CMD_STATUS
) {
767 WARN_ON(fr_flags(fp
) & FCPHF_CRC_UNCHECKED
);
769 fc_fcp_resp(fsp
, fp
);
771 FC_DBG("unexpected frame. r_ctl %x\n", r_ctl
);
774 fc_fcp_unlock_pkt(fsp
);
779 fc_fcp_error(fsp
, fp
);
780 else if (rc
== -ENOMEM
)
781 fc_fcp_reduce_can_queue(lport
);
784 static void fc_fcp_resp(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
786 struct fc_frame_header
*fh
;
787 struct fcp_resp
*fc_rp
;
788 struct fcp_resp_ext
*rp_ex
;
789 struct fcp_resp_rsp_info
*fc_rp_info
;
797 fh
= (struct fc_frame_header
*)fr_hdr(fp
);
798 if (unlikely(plen
< sizeof(*fh
) + sizeof(*fc_rp
)))
801 fc_rp
= (struct fcp_resp
*)(fh
+ 1);
802 fsp
->cdb_status
= fc_rp
->fr_status
;
803 flags
= fc_rp
->fr_flags
;
804 fsp
->scsi_comp_flags
= flags
;
805 expected_len
= fsp
->data_len
;
807 /* if ddp, update xfer len */
808 fc_fcp_ddp_done(fsp
);
810 if (unlikely((flags
& ~FCP_CONF_REQ
) || fc_rp
->fr_status
)) {
811 rp_ex
= (void *)(fc_rp
+ 1);
812 if (flags
& (FCP_RSP_LEN_VAL
| FCP_SNS_LEN_VAL
)) {
813 if (plen
< sizeof(*fc_rp
) + sizeof(*rp_ex
))
815 fc_rp_info
= (struct fcp_resp_rsp_info
*)(rp_ex
+ 1);
816 if (flags
& FCP_RSP_LEN_VAL
) {
817 respl
= ntohl(rp_ex
->fr_rsp_len
);
818 if (respl
!= sizeof(*fc_rp_info
))
820 if (fsp
->wait_for_comp
) {
821 /* Abuse cdb_status for rsp code */
822 fsp
->cdb_status
= fc_rp_info
->rsp_code
;
823 complete(&fsp
->tm_done
);
825 * tmfs will not have any scsi cmd so
832 if (flags
& FCP_SNS_LEN_VAL
) {
833 snsl
= ntohl(rp_ex
->fr_sns_len
);
834 if (snsl
> SCSI_SENSE_BUFFERSIZE
)
835 snsl
= SCSI_SENSE_BUFFERSIZE
;
836 memcpy(fsp
->cmd
->sense_buffer
,
837 (char *)fc_rp_info
+ respl
, snsl
);
840 if (flags
& (FCP_RESID_UNDER
| FCP_RESID_OVER
)) {
841 if (plen
< sizeof(*fc_rp
) + sizeof(rp_ex
->fr_resid
))
843 if (flags
& FCP_RESID_UNDER
) {
844 fsp
->scsi_resid
= ntohl(rp_ex
->fr_resid
);
846 * The cmnd->underflow is the minimum number of
847 * bytes that must be transfered for this
848 * command. Provided a sense condition is not
849 * present, make sure the actual amount
850 * transferred is at least the underflow value
853 if (!(flags
& FCP_SNS_LEN_VAL
) &&
854 (fc_rp
->fr_status
== 0) &&
855 (scsi_bufflen(fsp
->cmd
) -
856 fsp
->scsi_resid
) < fsp
->cmd
->underflow
)
858 expected_len
-= fsp
->scsi_resid
;
860 fsp
->status_code
= FC_ERROR
;
864 fsp
->state
|= FC_SRB_RCV_STATUS
;
867 * Check for missing or extra data frames.
869 if (unlikely(fsp
->xfer_len
!= expected_len
)) {
870 if (fsp
->xfer_len
< expected_len
) {
872 * Some data may be queued locally,
873 * Wait a at least one jiffy to see if it is delivered.
874 * If this expires without data, we may do SRR.
876 fc_fcp_timer_set(fsp
, 2);
879 fsp
->status_code
= FC_DATA_OVRRUN
;
880 FC_DBG("tgt %6x xfer len %zx greater than expected len %x. "
883 fsp
->xfer_len
, expected_len
, fsp
->data_len
);
885 fc_fcp_complete_locked(fsp
);
889 FC_DBG("short FCP response. flags 0x%x len %u respl %u snsl %u\n",
890 flags
, fr_len(fp
), respl
, snsl
);
892 fsp
->status_code
= FC_ERROR
;
893 fc_fcp_complete_locked(fsp
);
897 * fc_fcp_complete_locked() - complete processing of a fcp packet
900 * This function may sleep if a timer is pending. The packet lock must be
901 * held, and the host lock must not be held.
903 static void fc_fcp_complete_locked(struct fc_fcp_pkt
*fsp
)
905 struct fc_lport
*lp
= fsp
->lp
;
910 if (fsp
->state
& FC_SRB_ABORT_PENDING
)
913 if (fsp
->state
& FC_SRB_ABORTED
) {
914 if (!fsp
->status_code
)
915 fsp
->status_code
= FC_CMD_ABORTED
;
918 * Test for transport underrun, independent of response
921 if (fsp
->xfer_len
< fsp
->data_len
&& !fsp
->io_status
&&
922 (!(fsp
->scsi_comp_flags
& FCP_RESID_UNDER
) ||
923 fsp
->xfer_len
< fsp
->data_len
- fsp
->scsi_resid
)) {
924 fsp
->status_code
= FC_DATA_UNDRUN
;
932 if (unlikely(fsp
->scsi_comp_flags
& FCP_CONF_REQ
)) {
933 struct fc_frame
*conf_frame
;
936 csp
= lp
->tt
.seq_start_next(seq
);
937 conf_frame
= fc_frame_alloc(fsp
->lp
, 0);
939 f_ctl
= FC_FC_SEQ_INIT
;
940 f_ctl
|= FC_FC_LAST_SEQ
| FC_FC_END_SEQ
;
941 ep
= fc_seq_exch(seq
);
942 fc_fill_fc_hdr(conf_frame
, FC_RCTL_DD_SOL_CTL
,
944 FC_TYPE_FCP
, f_ctl
, 0);
945 lp
->tt
.seq_send(lp
, csp
, conf_frame
);
948 lp
->tt
.exch_done(seq
);
953 static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt
*fsp
, int error
)
955 struct fc_lport
*lp
= fsp
->lp
;
958 lp
->tt
.exch_done(fsp
->seq_ptr
);
961 fsp
->status_code
= error
;
965 * fc_fcp_cleanup_each_cmd() - Cleanup active commads
969 * @error: fsp status code
971 * If lun or id is -1, they are ignored.
973 static void fc_fcp_cleanup_each_cmd(struct fc_lport
*lp
, unsigned int id
,
974 unsigned int lun
, int error
)
976 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
977 struct fc_fcp_pkt
*fsp
;
978 struct scsi_cmnd
*sc_cmd
;
981 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
983 list_for_each_entry(fsp
, &si
->scsi_pkt_queue
, list
) {
985 if (id
!= -1 && scmd_id(sc_cmd
) != id
)
988 if (lun
!= -1 && sc_cmd
->device
->lun
!= lun
)
991 fc_fcp_pkt_hold(fsp
);
992 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
994 if (!fc_fcp_lock_pkt(fsp
)) {
995 fc_fcp_cleanup_cmd(fsp
, error
);
997 fc_fcp_unlock_pkt(fsp
);
1000 fc_fcp_pkt_release(fsp
);
1001 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
1003 * while we dropped the lock multiple pkts could
1004 * have been released, so we have to start over.
1008 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1011 static void fc_fcp_abort_io(struct fc_lport
*lp
)
1013 fc_fcp_cleanup_each_cmd(lp
, -1, -1, FC_HRD_ERROR
);
1017 * fc_fcp_pkt_send() - send a fcp packet to the lower level.
1021 * This is called by upper layer protocol.
1022 * Return : zero for success and -1 for failure
1023 * Context : called from queuecommand which can be called from process
1025 * Locks : called with the host lock and irqs disabled.
1027 static int fc_fcp_pkt_send(struct fc_lport
*lp
, struct fc_fcp_pkt
*fsp
)
1029 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
1032 fsp
->cmd
->SCp
.ptr
= (char *)fsp
;
1033 fsp
->cdb_cmd
.fc_dl
= htonl(fsp
->data_len
);
1034 fsp
->cdb_cmd
.fc_flags
= fsp
->req_flags
& ~FCP_CFL_LEN_MASK
;
1036 int_to_scsilun(fsp
->cmd
->device
->lun
,
1037 (struct scsi_lun
*)fsp
->cdb_cmd
.fc_lun
);
1038 memcpy(fsp
->cdb_cmd
.fc_cdb
, fsp
->cmd
->cmnd
, fsp
->cmd
->cmd_len
);
1039 list_add_tail(&fsp
->list
, &si
->scsi_pkt_queue
);
1041 spin_unlock_irq(lp
->host
->host_lock
);
1042 rc
= lp
->tt
.fcp_cmd_send(lp
, fsp
, fc_fcp_recv
);
1043 spin_lock_irq(lp
->host
->host_lock
);
1045 list_del(&fsp
->list
);
1050 static int fc_fcp_cmd_send(struct fc_lport
*lp
, struct fc_fcp_pkt
*fsp
,
1051 void (*resp
)(struct fc_seq
*,
1052 struct fc_frame
*fp
,
1055 struct fc_frame
*fp
;
1057 struct fc_rport
*rport
;
1058 struct fc_rport_libfc_priv
*rp
;
1059 const size_t len
= sizeof(fsp
->cdb_cmd
);
1062 if (fc_fcp_lock_pkt(fsp
))
1065 fp
= fc_frame_alloc(lp
, sizeof(fsp
->cdb_cmd
));
1071 memcpy(fc_frame_payload_get(fp
, len
), &fsp
->cdb_cmd
, len
);
1074 fsp
->max_payload
= rport
->maxframe_size
;
1075 rp
= rport
->dd_data
;
1077 fc_fill_fc_hdr(fp
, FC_RCTL_DD_UNSOL_CMD
, rport
->port_id
,
1078 fc_host_port_id(rp
->local_port
->host
), FC_TYPE_FCP
,
1079 FC_FC_FIRST_SEQ
| FC_FC_END_SEQ
| FC_FC_SEQ_INIT
, 0);
1081 seq
= lp
->tt
.exch_seq_send(lp
, fp
, resp
, fc_fcp_pkt_destroy
, fsp
, 0);
1087 fsp
->last_pkt_time
= jiffies
;
1089 fc_fcp_pkt_hold(fsp
); /* hold for fc_fcp_pkt_destroy */
1091 setup_timer(&fsp
->timer
, fc_fcp_timeout
, (unsigned long)fsp
);
1092 fc_fcp_timer_set(fsp
,
1093 (fsp
->tgt_flags
& FC_RP_FLAGS_REC_SUPPORTED
) ?
1094 FC_SCSI_REC_TOV
: FC_SCSI_ER_TIMEOUT
);
1096 fc_fcp_unlock_pkt(fsp
);
1101 * transport error handler
1103 static void fc_fcp_error(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
1105 int error
= PTR_ERR(fp
);
1107 if (fc_fcp_lock_pkt(fsp
))
1112 fc_fcp_retry_cmd(fsp
);
1115 FC_DBG("unknown error %ld\n", PTR_ERR(fp
));
1118 * clear abort pending, because the lower layer
1119 * decided to force completion.
1121 fsp
->state
&= ~FC_SRB_ABORT_PENDING
;
1122 fsp
->status_code
= FC_CMD_PLOGO
;
1123 fc_fcp_complete_locked(fsp
);
1125 fc_fcp_unlock_pkt(fsp
);
1129 * Scsi abort handler- calls to send an abort
1130 * and then wait for abort completion
1132 static int fc_fcp_pkt_abort(struct fc_lport
*lp
, struct fc_fcp_pkt
*fsp
)
1136 if (fc_fcp_send_abort(fsp
))
1139 init_completion(&fsp
->tm_done
);
1140 fsp
->wait_for_comp
= 1;
1142 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1143 rc
= wait_for_completion_timeout(&fsp
->tm_done
, FC_SCSI_TM_TOV
);
1144 spin_lock_bh(&fsp
->scsi_pkt_lock
);
1145 fsp
->wait_for_comp
= 0;
1148 FC_DBG("target abort cmd failed\n");
1150 } else if (fsp
->state
& FC_SRB_ABORTED
) {
1151 FC_DBG("target abort cmd passed\n");
1153 fc_fcp_complete_locked(fsp
);
1160 * Retry LUN reset after resource allocation failed.
1162 static void fc_lun_reset_send(unsigned long data
)
1164 struct fc_fcp_pkt
*fsp
= (struct fc_fcp_pkt
*)data
;
1165 struct fc_lport
*lp
= fsp
->lp
;
1166 if (lp
->tt
.fcp_cmd_send(lp
, fsp
, fc_tm_done
)) {
1167 if (fsp
->recov_retry
++ >= FC_MAX_RECOV_RETRY
)
1169 if (fc_fcp_lock_pkt(fsp
))
1171 setup_timer(&fsp
->timer
, fc_lun_reset_send
, (unsigned long)fsp
);
1172 fc_fcp_timer_set(fsp
, FC_SCSI_REC_TOV
);
1173 fc_fcp_unlock_pkt(fsp
);
1178 * Scsi device reset handler- send a LUN RESET to the device
1179 * and wait for reset reply
1181 static int fc_lun_reset(struct fc_lport
*lp
, struct fc_fcp_pkt
*fsp
,
1182 unsigned int id
, unsigned int lun
)
1186 fsp
->cdb_cmd
.fc_dl
= htonl(fsp
->data_len
);
1187 fsp
->cdb_cmd
.fc_tm_flags
= FCP_TMF_LUN_RESET
;
1188 int_to_scsilun(lun
, (struct scsi_lun
*)fsp
->cdb_cmd
.fc_lun
);
1190 fsp
->wait_for_comp
= 1;
1191 init_completion(&fsp
->tm_done
);
1193 fc_lun_reset_send((unsigned long)fsp
);
1196 * wait for completion of reset
1197 * after that make sure all commands are terminated
1199 rc
= wait_for_completion_timeout(&fsp
->tm_done
, FC_SCSI_TM_TOV
);
1201 spin_lock_bh(&fsp
->scsi_pkt_lock
);
1202 fsp
->state
|= FC_SRB_COMPL
;
1203 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1205 del_timer_sync(&fsp
->timer
);
1207 spin_lock_bh(&fsp
->scsi_pkt_lock
);
1209 lp
->tt
.exch_done(fsp
->seq_ptr
);
1210 fsp
->seq_ptr
= NULL
;
1212 fsp
->wait_for_comp
= 0;
1213 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1216 FC_DBG("lun reset failed\n");
1220 /* cdb_status holds the tmf's rsp code */
1221 if (fsp
->cdb_status
!= FCP_TMF_CMPL
)
1224 FC_DBG("lun reset to lun %u completed\n", lun
);
1225 fc_fcp_cleanup_each_cmd(lp
, id
, lun
, FC_CMD_ABORTED
);
1230 * Task Managment response handler
1232 static void fc_tm_done(struct fc_seq
*seq
, struct fc_frame
*fp
, void *arg
)
1234 struct fc_fcp_pkt
*fsp
= arg
;
1235 struct fc_frame_header
*fh
;
1239 * If there is an error just let it timeout or wait
1240 * for TMF to be aborted if it timedout.
1242 * scsi-eh will escalate for when either happens.
1247 if (fc_fcp_lock_pkt(fsp
))
1251 * raced with eh timeout handler.
1253 if (!fsp
->seq_ptr
|| !fsp
->wait_for_comp
) {
1254 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1258 fh
= fc_frame_header_get(fp
);
1259 if (fh
->fh_type
!= FC_TYPE_BLS
)
1260 fc_fcp_resp(fsp
, fp
);
1261 fsp
->seq_ptr
= NULL
;
1262 fsp
->lp
->tt
.exch_done(seq
);
1264 fc_fcp_unlock_pkt(fsp
);
1267 static void fc_fcp_cleanup(struct fc_lport
*lp
)
1269 fc_fcp_cleanup_each_cmd(lp
, -1, -1, FC_ERROR
);
1273 * fc_fcp_timeout: called by OS timer function.
1275 * The timer has been inactivated and must be reactivated if desired
1276 * using fc_fcp_timer_set().
1280 * If REC is supported, just issue it, and return. The REC exchange will
1281 * complete or time out, and recovery can continue at that point.
1283 * Otherwise, if the response has been received without all the data,
1284 * it has been ER_TIMEOUT since the response was received.
1286 * If the response has not been received,
1287 * we see if data was received recently. If it has been, we continue waiting,
1288 * otherwise, we abort the command.
1290 static void fc_fcp_timeout(unsigned long data
)
1292 struct fc_fcp_pkt
*fsp
= (struct fc_fcp_pkt
*)data
;
1293 struct fc_rport
*rport
= fsp
->rport
;
1294 struct fc_rport_libfc_priv
*rp
= rport
->dd_data
;
1296 if (fc_fcp_lock_pkt(fsp
))
1299 if (fsp
->cdb_cmd
.fc_tm_flags
)
1302 fsp
->state
|= FC_SRB_FCP_PROCESSING_TMO
;
1304 if (rp
->flags
& FC_RP_FLAGS_REC_SUPPORTED
)
1306 else if (time_after_eq(fsp
->last_pkt_time
+ (FC_SCSI_ER_TIMEOUT
/ 2),
1308 fc_fcp_timer_set(fsp
, FC_SCSI_ER_TIMEOUT
);
1309 else if (fsp
->state
& FC_SRB_RCV_STATUS
)
1310 fc_fcp_complete_locked(fsp
);
1312 fc_timeout_error(fsp
);
1313 fsp
->state
&= ~FC_SRB_FCP_PROCESSING_TMO
;
1315 fc_fcp_unlock_pkt(fsp
);
1319 * Send a REC ELS request
1321 static void fc_fcp_rec(struct fc_fcp_pkt
*fsp
)
1323 struct fc_lport
*lp
;
1324 struct fc_frame
*fp
;
1325 struct fc_rport
*rport
;
1326 struct fc_rport_libfc_priv
*rp
;
1330 rp
= rport
->dd_data
;
1331 if (!fsp
->seq_ptr
|| rp
->rp_state
!= RPORT_ST_READY
) {
1332 fsp
->status_code
= FC_HRD_ERROR
;
1334 fc_fcp_complete_locked(fsp
);
1337 fp
= fc_frame_alloc(lp
, sizeof(struct fc_els_rec
));
1341 fr_seq(fp
) = fsp
->seq_ptr
;
1342 fc_fill_fc_hdr(fp
, FC_RCTL_ELS_REQ
, rport
->port_id
,
1343 fc_host_port_id(rp
->local_port
->host
), FC_TYPE_ELS
,
1344 FC_FC_FIRST_SEQ
| FC_FC_END_SEQ
| FC_FC_SEQ_INIT
, 0);
1345 if (lp
->tt
.elsct_send(lp
, rport
, fp
, ELS_REC
, fc_fcp_rec_resp
,
1346 fsp
, jiffies_to_msecs(FC_SCSI_REC_TOV
))) {
1347 fc_fcp_pkt_hold(fsp
); /* hold while REC outstanding */
1352 if (fsp
->recov_retry
++ < FC_MAX_RECOV_RETRY
)
1353 fc_fcp_timer_set(fsp
, FC_SCSI_REC_TOV
);
1355 fc_timeout_error(fsp
);
1359 * Receive handler for REC ELS frame
1360 * if it is a reject then let the scsi layer to handle
1361 * the timeout. if it is a LS_ACC then if the io was not completed
1362 * then set the timeout and return otherwise complete the exchange
1363 * and tell the scsi layer to restart the I/O.
1365 static void fc_fcp_rec_resp(struct fc_seq
*seq
, struct fc_frame
*fp
, void *arg
)
1367 struct fc_fcp_pkt
*fsp
= (struct fc_fcp_pkt
*)arg
;
1368 struct fc_els_rec_acc
*recp
;
1369 struct fc_els_ls_rjt
*rjt
;
1373 enum dma_data_direction data_dir
;
1375 struct fc_rport_libfc_priv
*rp
;
1378 fc_fcp_rec_error(fsp
, fp
);
1382 if (fc_fcp_lock_pkt(fsp
))
1385 fsp
->recov_retry
= 0;
1386 opcode
= fc_frame_payload_op(fp
);
1387 if (opcode
== ELS_LS_RJT
) {
1388 rjt
= fc_frame_payload_get(fp
, sizeof(*rjt
));
1389 switch (rjt
->er_reason
) {
1391 FC_DEBUG_FCP("device %x unexpected REC reject "
1392 "reason %d expl %d\n",
1393 fsp
->rport
->port_id
, rjt
->er_reason
,
1397 FC_DEBUG_FCP("device does not support REC\n");
1398 rp
= fsp
->rport
->dd_data
;
1400 * if we do not spport RECs or got some bogus
1401 * reason then resetup timer so we check for
1404 rp
->flags
&= ~FC_RP_FLAGS_REC_SUPPORTED
;
1405 fc_fcp_timer_set(fsp
, FC_SCSI_ER_TIMEOUT
);
1410 * If no data transfer, the command frame got dropped
1411 * so we just retry. If data was transferred, we
1412 * lost the response but the target has no record,
1413 * so we abort and retry.
1415 if (rjt
->er_explan
== ELS_EXPL_OXID_RXID
&&
1416 fsp
->xfer_len
== 0) {
1417 fc_fcp_retry_cmd(fsp
);
1420 fc_timeout_error(fsp
);
1423 } else if (opcode
== ELS_LS_ACC
) {
1424 if (fsp
->state
& FC_SRB_ABORTED
)
1427 data_dir
= fsp
->cmd
->sc_data_direction
;
1428 recp
= fc_frame_payload_get(fp
, sizeof(*recp
));
1429 offset
= ntohl(recp
->reca_fc4value
);
1430 e_stat
= ntohl(recp
->reca_e_stat
);
1432 if (e_stat
& ESB_ST_COMPLETE
) {
1435 * The exchange is complete.
1437 * For output, we must've lost the response.
1438 * For input, all data must've been sent.
1439 * We lost may have lost the response
1440 * (and a confirmation was requested) and maybe
1443 * If all data received, send SRR
1444 * asking for response. If partial data received,
1445 * or gaps, SRR requests data at start of gap.
1446 * Recovery via SRR relies on in-order-delivery.
1448 if (data_dir
== DMA_TO_DEVICE
) {
1449 r_ctl
= FC_RCTL_DD_CMD_STATUS
;
1450 } else if (fsp
->xfer_contig_end
== offset
) {
1451 r_ctl
= FC_RCTL_DD_CMD_STATUS
;
1453 offset
= fsp
->xfer_contig_end
;
1454 r_ctl
= FC_RCTL_DD_SOL_DATA
;
1456 fc_fcp_srr(fsp
, r_ctl
, offset
);
1457 } else if (e_stat
& ESB_ST_SEQ_INIT
) {
1460 * The remote port has the initiative, so just
1461 * keep waiting for it to complete.
1463 fc_fcp_timer_set(fsp
, FC_SCSI_REC_TOV
);
1467 * The exchange is incomplete, we have seq. initiative.
1468 * Lost response with requested confirmation,
1469 * lost confirmation, lost transfer ready or
1472 * For output, if not all data was received, ask
1473 * for transfer ready to be repeated.
1475 * If we received or sent all the data, send SRR to
1478 * If we lost a response, we may have lost some read
1481 r_ctl
= FC_RCTL_DD_SOL_DATA
;
1482 if (data_dir
== DMA_TO_DEVICE
) {
1483 r_ctl
= FC_RCTL_DD_CMD_STATUS
;
1484 if (offset
< fsp
->data_len
)
1485 r_ctl
= FC_RCTL_DD_DATA_DESC
;
1486 } else if (offset
== fsp
->xfer_contig_end
) {
1487 r_ctl
= FC_RCTL_DD_CMD_STATUS
;
1488 } else if (fsp
->xfer_contig_end
< offset
) {
1489 offset
= fsp
->xfer_contig_end
;
1491 fc_fcp_srr(fsp
, r_ctl
, offset
);
1495 fc_fcp_unlock_pkt(fsp
);
1497 fc_fcp_pkt_release(fsp
); /* drop hold for outstanding REC */
1502 * Handle error response or timeout for REC exchange.
1504 static void fc_fcp_rec_error(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
1506 int error
= PTR_ERR(fp
);
1508 if (fc_fcp_lock_pkt(fsp
))
1513 fc_fcp_retry_cmd(fsp
);
1517 FC_DBG("REC %p fid %x error unexpected error %d\n",
1518 fsp
, fsp
->rport
->port_id
, error
);
1519 fsp
->status_code
= FC_CMD_PLOGO
;
1522 case -FC_EX_TIMEOUT
:
1524 * Assume REC or LS_ACC was lost.
1525 * The exchange manager will have aborted REC, so retry.
1527 FC_DBG("REC fid %x error error %d retry %d/%d\n",
1528 fsp
->rport
->port_id
, error
, fsp
->recov_retry
,
1529 FC_MAX_RECOV_RETRY
);
1530 if (fsp
->recov_retry
++ < FC_MAX_RECOV_RETRY
)
1533 fc_timeout_error(fsp
);
1536 fc_fcp_unlock_pkt(fsp
);
1538 fc_fcp_pkt_release(fsp
); /* drop hold for outstanding REC */
1542 * Time out error routine:
1543 * abort's the I/O close the exchange and
1544 * send completion notification to scsi layer
1546 static void fc_timeout_error(struct fc_fcp_pkt
*fsp
)
1548 fsp
->status_code
= FC_CMD_TIME_OUT
;
1549 fsp
->cdb_status
= 0;
1552 * if this fails then we let the scsi command timer fire and
1555 fc_fcp_send_abort(fsp
);
1559 * Sequence retransmission request.
1560 * This is called after receiving status but insufficient data, or
1561 * when expecting status but the request has timed out.
1563 static void fc_fcp_srr(struct fc_fcp_pkt
*fsp
, enum fc_rctl r_ctl
, u32 offset
)
1565 struct fc_lport
*lp
= fsp
->lp
;
1566 struct fc_rport
*rport
;
1567 struct fc_rport_libfc_priv
*rp
;
1568 struct fc_exch
*ep
= fc_seq_exch(fsp
->seq_ptr
);
1570 struct fcp_srr
*srr
;
1571 struct fc_frame
*fp
;
1575 rp
= rport
->dd_data
;
1576 cdb_op
= fsp
->cdb_cmd
.fc_cdb
[0];
1578 if (!(rp
->flags
& FC_RP_FLAGS_RETRY
) || rp
->rp_state
!= RPORT_ST_READY
)
1579 goto retry
; /* shouldn't happen */
1580 fp
= fc_frame_alloc(lp
, sizeof(*srr
));
1584 srr
= fc_frame_payload_get(fp
, sizeof(*srr
));
1585 memset(srr
, 0, sizeof(*srr
));
1586 srr
->srr_op
= ELS_SRR
;
1587 srr
->srr_ox_id
= htons(ep
->oxid
);
1588 srr
->srr_rx_id
= htons(ep
->rxid
);
1589 srr
->srr_r_ctl
= r_ctl
;
1590 srr
->srr_rel_off
= htonl(offset
);
1592 fc_fill_fc_hdr(fp
, FC_RCTL_ELS4_REQ
, rport
->port_id
,
1593 fc_host_port_id(rp
->local_port
->host
), FC_TYPE_FCP
,
1594 FC_FC_FIRST_SEQ
| FC_FC_END_SEQ
| FC_FC_SEQ_INIT
, 0);
1596 seq
= lp
->tt
.exch_seq_send(lp
, fp
, fc_fcp_srr_resp
, NULL
,
1597 fsp
, jiffies_to_msecs(FC_SCSI_REC_TOV
));
1602 fsp
->recov_seq
= seq
;
1603 fsp
->xfer_len
= offset
;
1604 fsp
->xfer_contig_end
= offset
;
1605 fsp
->state
&= ~FC_SRB_RCV_STATUS
;
1606 fc_fcp_pkt_hold(fsp
); /* hold for outstanding SRR */
1609 fc_fcp_retry_cmd(fsp
);
1613 * Handle response from SRR.
1615 static void fc_fcp_srr_resp(struct fc_seq
*seq
, struct fc_frame
*fp
, void *arg
)
1617 struct fc_fcp_pkt
*fsp
= arg
;
1618 struct fc_frame_header
*fh
;
1621 fc_fcp_srr_error(fsp
, fp
);
1625 if (fc_fcp_lock_pkt(fsp
))
1628 fh
= fc_frame_header_get(fp
);
1630 * BUG? fc_fcp_srr_error calls exch_done which would release
1631 * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
1632 * then fc_exch_timeout would be sending an abort. The exch_done
1633 * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
1634 * an abort response though.
1636 if (fh
->fh_type
== FC_TYPE_BLS
) {
1637 fc_fcp_unlock_pkt(fsp
);
1641 fsp
->recov_seq
= NULL
;
1642 switch (fc_frame_payload_op(fp
)) {
1644 fsp
->recov_retry
= 0;
1645 fc_fcp_timer_set(fsp
, FC_SCSI_REC_TOV
);
1649 fc_timeout_error(fsp
);
1652 fc_fcp_unlock_pkt(fsp
);
1653 fsp
->lp
->tt
.exch_done(seq
);
1656 fc_fcp_pkt_release(fsp
); /* drop hold for outstanding SRR */
1659 static void fc_fcp_srr_error(struct fc_fcp_pkt
*fsp
, struct fc_frame
*fp
)
1661 if (fc_fcp_lock_pkt(fsp
))
1663 fsp
->lp
->tt
.exch_done(fsp
->recov_seq
);
1664 fsp
->recov_seq
= NULL
;
1665 switch (PTR_ERR(fp
)) {
1666 case -FC_EX_TIMEOUT
:
1667 if (fsp
->recov_retry
++ < FC_MAX_RECOV_RETRY
)
1670 fc_timeout_error(fsp
);
1672 case -FC_EX_CLOSED
: /* e.g., link failure */
1675 fc_fcp_retry_cmd(fsp
);
1678 fc_fcp_unlock_pkt(fsp
);
1680 fc_fcp_pkt_release(fsp
); /* drop hold for outstanding SRR */
1683 static inline int fc_fcp_lport_queue_ready(struct fc_lport
*lp
)
1686 return (lp
->state
== LPORT_ST_READY
) && lp
->link_up
&& !lp
->qfull
;
1690 * fc_queuecommand - The queuecommand function of the scsi template
1691 * @cmd: struct scsi_cmnd to be executed
1692 * @done: Callback function to be called when cmd is completed
1694 * this is the i/o strategy routine, called by the scsi layer
1695 * this routine is called with holding the host_lock.
1697 int fc_queuecommand(struct scsi_cmnd
*sc_cmd
, void (*done
)(struct scsi_cmnd
*))
1699 struct fc_lport
*lp
;
1700 struct fc_rport
*rport
= starget_to_rport(scsi_target(sc_cmd
->device
));
1701 struct fc_fcp_pkt
*fsp
;
1702 struct fc_rport_libfc_priv
*rp
;
1705 struct fcoe_dev_stats
*stats
;
1707 lp
= shost_priv(sc_cmd
->device
->host
);
1709 rval
= fc_remote_port_chkready(rport
);
1711 sc_cmd
->result
= rval
;
1716 if (!*(struct fc_remote_port
**)rport
->dd_data
) {
1718 * rport is transitioning from blocked/deleted to
1721 sc_cmd
->result
= DID_IMM_RETRY
<< 16;
1726 rp
= rport
->dd_data
;
1728 if (!fc_fcp_lport_queue_ready(lp
)) {
1729 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1733 fsp
= fc_fcp_pkt_alloc(lp
, GFP_ATOMIC
);
1735 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1740 * build the libfc request pkt
1742 fsp
->cmd
= sc_cmd
; /* save the cmd */
1743 fsp
->lp
= lp
; /* save the softc ptr */
1744 fsp
->rport
= rport
; /* set the remote port ptr */
1745 sc_cmd
->scsi_done
= done
;
1748 * set up the transfer length
1750 fsp
->data_len
= scsi_bufflen(sc_cmd
);
1754 * setup the data direction
1756 stats
= fc_lport_get_stats(lp
);
1757 if (sc_cmd
->sc_data_direction
== DMA_FROM_DEVICE
) {
1758 fsp
->req_flags
= FC_SRB_READ
;
1759 stats
->InputRequests
++;
1760 stats
->InputMegabytes
= fsp
->data_len
;
1761 } else if (sc_cmd
->sc_data_direction
== DMA_TO_DEVICE
) {
1762 fsp
->req_flags
= FC_SRB_WRITE
;
1763 stats
->OutputRequests
++;
1764 stats
->OutputMegabytes
= fsp
->data_len
;
1767 stats
->ControlRequests
++;
1770 fsp
->tgt_flags
= rp
->flags
;
1772 init_timer(&fsp
->timer
);
1773 fsp
->timer
.data
= (unsigned long)fsp
;
1776 * send it to the lower layer
1777 * if we get -1 return then put the request in the pending
1780 rval
= fc_fcp_pkt_send(lp
, fsp
);
1782 fsp
->state
= FC_SRB_FREE
;
1783 fc_fcp_pkt_release(fsp
);
1784 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1789 EXPORT_SYMBOL(fc_queuecommand
);
1792 * fc_io_compl() - Handle responses for completed commands
1795 * Translates a error to a Linux SCSI error.
1797 * The fcp packet lock must be held when calling.
1799 static void fc_io_compl(struct fc_fcp_pkt
*fsp
)
1801 struct fc_fcp_internal
*si
;
1802 struct scsi_cmnd
*sc_cmd
;
1803 struct fc_lport
*lp
;
1804 unsigned long flags
;
1806 /* release outstanding ddp context */
1807 fc_fcp_ddp_done(fsp
);
1809 fsp
->state
|= FC_SRB_COMPL
;
1810 if (!(fsp
->state
& FC_SRB_FCP_PROCESSING_TMO
)) {
1811 spin_unlock_bh(&fsp
->scsi_pkt_lock
);
1812 del_timer_sync(&fsp
->timer
);
1813 spin_lock_bh(&fsp
->scsi_pkt_lock
);
1817 si
= fc_get_scsi_internal(lp
);
1818 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
1820 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1825 * if a command timed out while we had to try and throttle IO
1826 * and it is now getting cleaned up, then we are about to
1827 * try again so clear the throttled flag incase we get more
1830 if (si
->throttled
&& fsp
->state
& FC_SRB_NOMEM
)
1836 if (!sc_cmd
->SCp
.ptr
) {
1837 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1841 CMD_SCSI_STATUS(sc_cmd
) = fsp
->cdb_status
;
1842 switch (fsp
->status_code
) {
1844 if (fsp
->cdb_status
== 0) {
1848 sc_cmd
->result
= DID_OK
<< 16;
1849 if (fsp
->scsi_resid
)
1850 CMD_RESID_LEN(sc_cmd
) = fsp
->scsi_resid
;
1851 } else if (fsp
->cdb_status
== QUEUE_FULL
) {
1852 struct scsi_device
*tmp_sdev
;
1853 struct scsi_device
*sdev
= sc_cmd
->device
;
1855 shost_for_each_device(tmp_sdev
, sdev
->host
) {
1856 if (tmp_sdev
->id
!= sdev
->id
)
1859 if (tmp_sdev
->queue_depth
> 1) {
1860 scsi_track_queue_full(tmp_sdev
,
1865 sc_cmd
->result
= (DID_OK
<< 16) | fsp
->cdb_status
;
1868 * transport level I/O was ok but scsi
1869 * has non zero status
1871 sc_cmd
->result
= (DID_OK
<< 16) | fsp
->cdb_status
;
1875 sc_cmd
->result
= DID_ERROR
<< 16;
1877 case FC_DATA_UNDRUN
:
1878 if ((fsp
->cdb_status
== 0) && !(fsp
->req_flags
& FC_SRB_READ
)) {
1880 * scsi status is good but transport level
1883 sc_cmd
->result
= DID_OK
<< 16;
1886 * scsi got underrun, this is an error
1888 CMD_RESID_LEN(sc_cmd
) = fsp
->scsi_resid
;
1889 sc_cmd
->result
= (DID_ERROR
<< 16) | fsp
->cdb_status
;
1892 case FC_DATA_OVRRUN
:
1894 * overrun is an error
1896 sc_cmd
->result
= (DID_ERROR
<< 16) | fsp
->cdb_status
;
1898 case FC_CMD_ABORTED
:
1899 sc_cmd
->result
= (DID_ABORT
<< 16) | fsp
->io_status
;
1901 case FC_CMD_TIME_OUT
:
1902 sc_cmd
->result
= (DID_BUS_BUSY
<< 16) | fsp
->io_status
;
1905 sc_cmd
->result
= (DID_RESET
<< 16);
1908 sc_cmd
->result
= (DID_NO_CONNECT
<< 16);
1911 sc_cmd
->result
= (DID_ERROR
<< 16);
1915 list_del(&fsp
->list
);
1916 sc_cmd
->SCp
.ptr
= NULL
;
1917 sc_cmd
->scsi_done(sc_cmd
);
1918 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1920 /* release ref from initial allocation in queue command */
1921 fc_fcp_pkt_release(fsp
);
1925 * fc_fcp_complete() - complete processing of a fcp packet
1928 * This function may sleep if a fsp timer is pending.
1929 * The host lock must not be held by caller.
1931 void fc_fcp_complete(struct fc_fcp_pkt
*fsp
)
1933 if (fc_fcp_lock_pkt(fsp
))
1936 fc_fcp_complete_locked(fsp
);
1937 fc_fcp_unlock_pkt(fsp
);
1939 EXPORT_SYMBOL(fc_fcp_complete
);
1942 * fc_eh_abort() - Abort a command
1943 * @sc_cmd: scsi command to abort
1945 * From scsi host template.
1946 * send ABTS to the target device and wait for the response
1947 * sc_cmd is the pointer to the command to be aborted.
1949 int fc_eh_abort(struct scsi_cmnd
*sc_cmd
)
1951 struct fc_fcp_pkt
*fsp
;
1952 struct fc_lport
*lp
;
1954 unsigned long flags
;
1956 lp
= shost_priv(sc_cmd
->device
->host
);
1957 if (lp
->state
!= LPORT_ST_READY
)
1959 else if (!lp
->link_up
)
1962 spin_lock_irqsave(lp
->host
->host_lock
, flags
);
1963 fsp
= CMD_SP(sc_cmd
);
1965 /* command completed while scsi eh was setting up */
1966 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1969 /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
1970 fc_fcp_pkt_hold(fsp
);
1971 spin_unlock_irqrestore(lp
->host
->host_lock
, flags
);
1973 if (fc_fcp_lock_pkt(fsp
)) {
1974 /* completed while we were waiting for timer to be deleted */
1979 rc
= fc_fcp_pkt_abort(lp
, fsp
);
1980 fc_fcp_unlock_pkt(fsp
);
1983 fc_fcp_pkt_release(fsp
);
1986 EXPORT_SYMBOL(fc_eh_abort
);
1989 * fc_eh_device_reset() Reset a single LUN
1990 * @sc_cmd: scsi command
1992 * Set from scsi host template to send tm cmd to the target and wait for the
1995 int fc_eh_device_reset(struct scsi_cmnd
*sc_cmd
)
1997 struct fc_lport
*lp
;
1998 struct fc_fcp_pkt
*fsp
;
1999 struct fc_rport
*rport
= starget_to_rport(scsi_target(sc_cmd
->device
));
2001 struct fc_rport_libfc_priv
*rp
;
2004 rval
= fc_remote_port_chkready(rport
);
2008 rp
= rport
->dd_data
;
2009 lp
= shost_priv(sc_cmd
->device
->host
);
2011 if (lp
->state
!= LPORT_ST_READY
)
2014 fsp
= fc_fcp_pkt_alloc(lp
, GFP_NOIO
);
2016 FC_DBG("could not allocate scsi_pkt\n");
2017 sc_cmd
->result
= DID_NO_CONNECT
<< 16;
2022 * Build the libfc request pkt. Do not set the scsi cmnd, because
2023 * the sc passed in is not setup for execution like when sent
2024 * through the queuecommand callout.
2026 fsp
->lp
= lp
; /* save the softc ptr */
2027 fsp
->rport
= rport
; /* set the remote port ptr */
2030 * flush outstanding commands
2032 rc
= fc_lun_reset(lp
, fsp
, scmd_id(sc_cmd
), sc_cmd
->device
->lun
);
2033 fsp
->state
= FC_SRB_FREE
;
2034 fc_fcp_pkt_release(fsp
);
2039 EXPORT_SYMBOL(fc_eh_device_reset
);
2042 * fc_eh_host_reset() - The reset function will reset the ports on the host.
2043 * @sc_cmd: scsi command
2045 int fc_eh_host_reset(struct scsi_cmnd
*sc_cmd
)
2047 struct Scsi_Host
*shost
= sc_cmd
->device
->host
;
2048 struct fc_lport
*lp
= shost_priv(shost
);
2049 unsigned long wait_tmo
;
2051 lp
->tt
.lport_reset(lp
);
2052 wait_tmo
= jiffies
+ FC_HOST_RESET_TIMEOUT
;
2053 while (!fc_fcp_lport_queue_ready(lp
) && time_before(jiffies
, wait_tmo
))
2056 if (fc_fcp_lport_queue_ready(lp
)) {
2057 shost_printk(KERN_INFO
, shost
, "Host reset succeeded.\n");
2060 shost_printk(KERN_INFO
, shost
, "Host reset failed. "
2061 "lport not ready.\n");
2065 EXPORT_SYMBOL(fc_eh_host_reset
);
2068 * fc_slave_alloc() - configure queue depth
2069 * @sdev: scsi device
2071 * Configures queue depth based on host's cmd_per_len. If not set
2072 * then we use the libfc default.
2074 int fc_slave_alloc(struct scsi_device
*sdev
)
2076 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
2079 if (!rport
|| fc_remote_port_chkready(rport
))
2082 if (sdev
->tagged_supported
) {
2083 if (sdev
->host
->hostt
->cmd_per_lun
)
2084 queue_depth
= sdev
->host
->hostt
->cmd_per_lun
;
2086 queue_depth
= FC_FCP_DFLT_QUEUE_DEPTH
;
2087 scsi_activate_tcq(sdev
, queue_depth
);
2091 EXPORT_SYMBOL(fc_slave_alloc
);
2093 int fc_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
2095 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), qdepth
);
2096 return sdev
->queue_depth
;
2098 EXPORT_SYMBOL(fc_change_queue_depth
);
2100 int fc_change_queue_type(struct scsi_device
*sdev
, int tag_type
)
2102 if (sdev
->tagged_supported
) {
2103 scsi_set_tag_type(sdev
, tag_type
);
2105 scsi_activate_tcq(sdev
, sdev
->queue_depth
);
2107 scsi_deactivate_tcq(sdev
, sdev
->queue_depth
);
2113 EXPORT_SYMBOL(fc_change_queue_type
);
2115 void fc_fcp_destroy(struct fc_lport
*lp
)
2117 struct fc_fcp_internal
*si
= fc_get_scsi_internal(lp
);
2119 if (!list_empty(&si
->scsi_pkt_queue
))
2120 printk(KERN_ERR
"Leaked scsi packets.\n");
2122 mempool_destroy(si
->scsi_pkt_pool
);
2124 lp
->scsi_priv
= NULL
;
2126 EXPORT_SYMBOL(fc_fcp_destroy
);
2128 int fc_fcp_init(struct fc_lport
*lp
)
2131 struct fc_fcp_internal
*si
;
2133 if (!lp
->tt
.fcp_cmd_send
)
2134 lp
->tt
.fcp_cmd_send
= fc_fcp_cmd_send
;
2136 if (!lp
->tt
.fcp_cleanup
)
2137 lp
->tt
.fcp_cleanup
= fc_fcp_cleanup
;
2139 if (!lp
->tt
.fcp_abort_io
)
2140 lp
->tt
.fcp_abort_io
= fc_fcp_abort_io
;
2142 si
= kzalloc(sizeof(struct fc_fcp_internal
), GFP_KERNEL
);
2146 INIT_LIST_HEAD(&si
->scsi_pkt_queue
);
2148 si
->scsi_pkt_pool
= mempool_create_slab_pool(2, scsi_pkt_cachep
);
2149 if (!si
->scsi_pkt_pool
) {
2159 EXPORT_SYMBOL(fc_fcp_init
);
2161 static int __init
libfc_init(void)
2165 scsi_pkt_cachep
= kmem_cache_create("libfc_fcp_pkt",
2166 sizeof(struct fc_fcp_pkt
),
2167 0, SLAB_HWCACHE_ALIGN
, NULL
);
2168 if (scsi_pkt_cachep
== NULL
) {
2169 FC_DBG("Unable to allocate SRB cache...module load failed!");
2173 rc
= fc_setup_exch_mgr();
2175 goto destroy_pkt_cache
;
2177 rc
= fc_setup_rport();
2183 fc_destroy_exch_mgr();
2185 kmem_cache_destroy(scsi_pkt_cachep
);
2189 static void __exit
libfc_exit(void)
2191 kmem_cache_destroy(scsi_pkt_cachep
);
2192 fc_destroy_exch_mgr();
2196 module_init(libfc_init
);
2197 module_exit(libfc_exit
);