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>
44 static struct kmem_cache *scsi_pkt_cachep;
46 /* SRB state definitions */
47 #define FC_SRB_FREE 0 /* cmd is free */
48 #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
49 #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
50 #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
51 #define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */
52 #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
53 #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
54 #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
55 #define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
57 #define FC_SRB_READ (1 << 1)
58 #define FC_SRB_WRITE (1 << 0)
61 * The SCp.ptr should be tested and set under the host lock. NULL indicates
62 * that the command has been retruned to the scsi layer.
64 #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
65 #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
66 #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
67 #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
68 #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
70 struct fc_fcp_internal {
71 mempool_t *scsi_pkt_pool;
72 struct list_head scsi_pkt_queue;
76 #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
80 * FC scsi I/O related functions
82 static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
83 static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
84 static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
85 static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
86 static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
87 static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp);
88 static void fc_timeout_error(struct fc_fcp_pkt *);
89 static void fc_fcp_timeout(unsigned long data);
90 static void fc_fcp_rec(struct fc_fcp_pkt *);
91 static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
92 static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
93 static void fc_io_compl(struct fc_fcp_pkt *);
95 static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
96 static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
97 static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
100 * command status codes
102 #define FC_COMPLETE 0
103 #define FC_CMD_ABORTED 1
104 #define FC_CMD_RESET 2
105 #define FC_CMD_PLOGO 3
107 #define FC_TRANS_ERR 5
108 #define FC_DATA_OVRRUN 6
109 #define FC_DATA_UNDRUN 7
111 #define FC_HRD_ERROR 9
112 #define FC_CMD_TIME_OUT 10
115 * Error recovery timeout values.
117 #define FC_SCSI_ER_TIMEOUT (10 * HZ)
118 #define FC_SCSI_TM_TOV (10 * HZ)
119 #define FC_SCSI_REC_TOV (2 * HZ)
120 #define FC_HOST_RESET_TIMEOUT (30 * HZ)
122 #define FC_MAX_ERROR_CNT 5
123 #define FC_MAX_RECOV_RETRY 3
125 #define FC_FCP_DFLT_QUEUE_DEPTH 32
128 * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet
129 * @lp: fc lport struct
130 * @gfp: gfp flags for allocation
132 * This is used by upper layer scsi driver.
133 * Return Value : scsi_pkt structure or null on allocation failure.
134 * Context : call from process context. no locking required.
136 static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lp, gfp_t gfp)
138 struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
139 struct fc_fcp_pkt *fsp;
141 fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
143 memset(fsp, 0, sizeof(*fsp));
145 atomic_set(&fsp->ref_cnt, 1);
146 init_timer(&fsp->timer);
147 INIT_LIST_HEAD(&fsp->list);
148 spin_lock_init(&fsp->scsi_pkt_lock);
154 * fc_fcp_pkt_release() - release hold on scsi_pkt packet
155 * @fsp: fcp packet struct
157 * This is used by upper layer scsi driver.
158 * Context : call from process and interrupt context.
159 * no locking required
161 static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
163 if (atomic_dec_and_test(&fsp->ref_cnt)) {
164 struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
166 mempool_free(fsp, si->scsi_pkt_pool);
170 static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
172 atomic_inc(&fsp->ref_cnt);
176 * fc_fcp_pkt_destory() - release hold on scsi_pkt packet
177 * @seq: exchange sequence
178 * @fsp: fcp packet struct
180 * Release hold on scsi_pkt packet set to keep scsi_pkt
181 * till EM layer exch resource is not freed.
182 * Context : called from from EM layer.
183 * no locking required
185 static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
187 fc_fcp_pkt_release(fsp);
191 * fc_fcp_lock_pkt() - lock a packet and get a ref to it.
194 * We should only return error if we return a command to scsi-ml before
195 * getting a response. This can happen in cases where we send a abort, but
196 * do not wait for the response and the abort and command can be passing
197 * each other on the wire/network-layer.
199 * Note: this function locks the packet and gets a reference to allow
200 * callers to call the completion function while the lock is held and
201 * not have to worry about the packets refcount.
203 * TODO: Maybe we should just have callers grab/release the lock and
204 * have a function that they call to verify the fsp and grab a ref if
207 static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
209 spin_lock_bh(&fsp->scsi_pkt_lock);
210 if (fsp->state & FC_SRB_COMPL) {
211 spin_unlock_bh(&fsp->scsi_pkt_lock);
215 fc_fcp_pkt_hold(fsp);
219 static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
221 spin_unlock_bh(&fsp->scsi_pkt_lock);
222 fc_fcp_pkt_release(fsp);
225 static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
227 if (!(fsp->state & FC_SRB_COMPL))
228 mod_timer(&fsp->timer, jiffies + delay);
231 static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
236 fsp->state |= FC_SRB_ABORT_PENDING;
237 return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
242 * An abort isn't needed.
244 static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
247 fsp->lp->tt.exch_done(fsp->seq_ptr);
251 fsp->state &= ~FC_SRB_ABORT_PENDING;
253 fsp->status_code = FC_ERROR;
254 fc_fcp_complete_locked(fsp);
258 * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP
259 * transfer for a read I/O indicated by the fc_fcp_pkt.
260 * @fsp: ptr to the fc_fcp_pkt
262 * This is called in exch_seq_send() when we have a newly allocated
263 * exchange with a valid exchange id to setup ddp.
267 void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
275 if ((fsp->req_flags & FC_SRB_READ) &&
276 (lp->lro_enabled) && (lp->tt.ddp_setup)) {
277 if (lp->tt.ddp_setup(lp, xid, scsi_sglist(fsp->cmd),
278 scsi_sg_count(fsp->cmd)))
284 * fc_fcp_ddp_done - calls to LLD's ddp_done to release any
285 * DDP related resources for this I/O if it is initialized
287 * @fsp: ptr to the fc_fcp_pkt
291 static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
298 if (fsp->xfer_ddp == FC_XID_UNKNOWN)
302 if (lp->tt.ddp_done) {
303 fsp->xfer_len = lp->tt.ddp_done(lp, fsp->xfer_ddp);
304 fsp->xfer_ddp = FC_XID_UNKNOWN;
310 * Receive SCSI data from target.
311 * Called after receiving solicited data.
313 static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
315 struct scsi_cmnd *sc = fsp->cmd;
316 struct fc_lport *lp = fsp->lp;
317 struct fcoe_dev_stats *stats;
318 struct fc_frame_header *fh;
325 struct scatterlist *sg;
328 fh = fc_frame_header_get(fp);
329 offset = ntohl(fh->fh_parm_offset);
330 start_offset = offset;
331 len = fr_len(fp) - sizeof(*fh);
332 buf = fc_frame_payload_get(fp, 0);
334 /* if this I/O is ddped, update xfer len */
335 fc_fcp_ddp_done(fsp);
337 if (offset + len > fsp->data_len) {
338 /* this should never happen */
339 if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
340 fc_frame_crc_check(fp))
342 FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
343 "data_len %x\n", len, offset, fsp->data_len);
344 fc_fcp_retry_cmd(fsp);
347 if (offset != fsp->xfer_len)
348 fsp->state |= FC_SRB_DISCONTIG;
351 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
352 crc = crc32(~0, (u8 *) fh, sizeof(*fh));
354 sg = scsi_sglist(sc);
357 while (remaining > 0 && sg) {
362 if (offset >= sg->length) {
363 offset -= sg->length;
367 sg_bytes = min(remaining, sg->length - offset);
370 * The scatterlist item may be bigger than PAGE_SIZE,
371 * but we are limited to mapping PAGE_SIZE at a time.
373 off = offset + sg->offset;
374 sg_bytes = min(sg_bytes, (size_t)
375 (PAGE_SIZE - (off & ~PAGE_MASK)));
376 page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
379 break; /* XXX panic? */
381 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
382 crc = crc32(crc, buf, sg_bytes);
383 memcpy((char *)page_addr + (off & ~PAGE_MASK), buf,
386 kunmap_atomic(page_addr, KM_SOFTIRQ0);
389 remaining -= sg_bytes;
390 copy_len += sg_bytes;
393 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
394 buf = fc_frame_payload_get(fp, 0);
396 crc = crc32(crc, buf + len, 4 - (len % 4));
397 len += 4 - (len % 4);
400 if (~crc != le32_to_cpu(fr_crc(fp))) {
402 stats = fc_lport_get_stats(lp);
403 stats->ErrorFrames++;
404 /* FIXME - per cpu count, not total count! */
405 if (stats->InvalidCRCCount++ < 5)
406 printk(KERN_WARNING "libfc: CRC error on data "
407 "frame for port (%6x)\n",
408 fc_host_port_id(lp->host));
410 * Assume the frame is total garbage.
411 * We may have copied it over the good part
413 * If so, we need to retry the entire operation.
414 * Otherwise, ignore it.
416 if (fsp->state & FC_SRB_DISCONTIG)
417 fc_fcp_retry_cmd(fsp);
422 if (fsp->xfer_contig_end == start_offset)
423 fsp->xfer_contig_end += copy_len;
424 fsp->xfer_len += copy_len;
427 * In the very rare event that this data arrived after the response
428 * and completes the transfer, call the completion handler.
430 if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
431 fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
432 fc_fcp_complete_locked(fsp);
436 * fc_fcp_send_data() - Send SCSI data to target.
437 * @fsp: ptr to fc_fcp_pkt
438 * @sp: ptr to this sequence
439 * @offset: starting offset for this data request
440 * @seq_blen: the burst length for this data request
442 * Called after receiving a Transfer Ready data descriptor.
443 * if LLD is capable of seq offload then send down seq_blen
444 * size of data in single frame, otherwise send multiple FC
445 * frames of max FC frame payload supported by target port.
447 * Returns : 0 for success.
449 static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
450 size_t offset, size_t seq_blen)
453 struct scsi_cmnd *sc;
454 struct scatterlist *sg;
455 struct fc_frame *fp = NULL;
456 struct fc_lport *lp = fsp->lp;
461 size_t frame_offset, fh_parm_offset;
465 int using_sg = lp->sg_supp;
468 WARN_ON(seq_blen <= 0);
469 if (unlikely(offset + seq_blen > fsp->data_len)) {
470 /* this should never happen */
471 FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
472 "offset %zx\n", seq_blen, offset);
473 fc_fcp_send_abort(fsp);
475 } else if (offset != fsp->xfer_len) {
476 /* Out of Order Data Request - no problem, but unexpected. */
477 FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
478 "seq_blen %zx offset %zx\n", seq_blen, offset);
482 * if LLD is capable of seq_offload then set transport
483 * burst length (t_blen) to seq_blen, otherwise set t_blen
484 * to max FC frame payload previously set in fsp->max_payload.
486 t_blen = fsp->max_payload;
487 if (lp->seq_offload) {
488 t_blen = min(seq_blen, (size_t)lp->lso_max);
489 FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
490 fsp, seq_blen, lp->lso_max, t_blen);
493 WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
495 t_blen &= ~(512 - 1); /* round down to block size */
496 WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); /* won't go below 256 */
499 remaining = seq_blen;
500 fh_parm_offset = frame_offset = offset;
502 seq = lp->tt.seq_start_next(seq);
503 f_ctl = FC_FC_REL_OFF;
506 sg = scsi_sglist(sc);
508 while (remaining > 0 && sg) {
509 if (offset >= sg->length) {
510 offset -= sg->length;
515 tlen = min(t_blen, remaining);
518 * TODO. Temporary workaround. fc_seq_send() can't
519 * handle odd lengths in non-linear skbs.
520 * This will be the final fragment only.
525 fp = _fc_frame_alloc(lp, 0);
529 fp = fc_frame_alloc(lp, tlen);
533 data = (void *)(fr_hdr(fp)) +
534 sizeof(struct fc_frame_header);
536 fh_parm_offset = frame_offset;
537 fr_max_payload(fp) = fsp->max_payload;
539 sg_bytes = min(tlen, sg->length - offset);
541 get_page(sg_page(sg));
542 skb_fill_page_desc(fp_skb(fp),
543 skb_shinfo(fp_skb(fp))->nr_frags,
544 sg_page(sg), sg->offset + offset,
546 fp_skb(fp)->data_len += sg_bytes;
547 fr_len(fp) += sg_bytes;
548 fp_skb(fp)->truesize += PAGE_SIZE;
550 size_t off = offset + sg->offset;
553 * The scatterlist item may be bigger than PAGE_SIZE,
554 * but we must not cross pages inside the kmap.
556 sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
557 (off & ~PAGE_MASK)));
558 page_addr = kmap_atomic(sg_page(sg) +
561 memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
563 kunmap_atomic(page_addr, KM_SOFTIRQ0);
567 frame_offset += sg_bytes;
569 remaining -= sg_bytes;
571 if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
576 * Send sequence with transfer sequence initiative in case
577 * this is last FCP frame of the sequence.
580 f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
582 ep = fc_seq_exch(seq);
583 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
584 FC_TYPE_FCP, f_ctl, fh_parm_offset);
587 * send fragment using for a sequence.
589 error = lp->tt.seq_send(lp, seq, fp);
591 WARN_ON(1); /* send error should be rare */
592 fc_fcp_retry_cmd(fsp);
597 fsp->xfer_len += seq_blen; /* premature count? */
601 static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
604 struct fc_ba_rjt *brp;
605 struct fc_frame_header *fh;
607 fh = fc_frame_header_get(fp);
608 switch (fh->fh_r_ctl) {
612 brp = fc_frame_payload_get(fp, sizeof(*brp));
613 if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
618 * we will let the command timeout
619 * and scsi-ml recover in this case,
620 * therefore cleared the ba_done flag.
626 fsp->state |= FC_SRB_ABORTED;
627 fsp->state &= ~FC_SRB_ABORT_PENDING;
629 if (fsp->wait_for_comp)
630 complete(&fsp->tm_done);
632 fc_fcp_complete_locked(fsp);
637 * fc_fcp_reduce_can_queue() - drop can_queue
638 * @lp: lport to drop queueing for
640 * If we are getting memory allocation failures, then we may
641 * be trying to execute too many commands. We let the running
642 * commands complete or timeout, then try again with a reduced
643 * can_queue. Eventually we will hit the point where we run
644 * on all reserved structs.
646 static void fc_fcp_reduce_can_queue(struct fc_lport *lp)
648 struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
652 spin_lock_irqsave(lp->host->host_lock, flags);
657 can_queue = lp->host->can_queue;
661 lp->host->can_queue = can_queue;
662 shost_printk(KERN_ERR, lp->host, "libfc: Could not allocate frame.\n"
663 "Reducing can_queue to %d.\n", can_queue);
665 spin_unlock_irqrestore(lp->host->host_lock, flags);
669 * fc_fcp_recv() - Reveive FCP frames
670 * @seq: The sequence the frame is on
672 * @arg: The related FCP packet
675 * Context : called from Soft IRQ context
676 * can not called holding list lock
678 static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
680 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
681 struct fc_lport *lport = fsp->lp;
682 struct fc_frame_header *fh;
683 struct fcp_txrdy *dd;
690 fh = fc_frame_header_get(fp);
691 r_ctl = fh->fh_r_ctl;
693 if (!(lport->state & LPORT_ST_READY))
695 if (fc_fcp_lock_pkt(fsp))
697 fsp->last_pkt_time = jiffies;
699 if (fh->fh_type == FC_TYPE_BLS) {
700 fc_fcp_abts_resp(fsp, fp);
704 if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
707 if (r_ctl == FC_RCTL_DD_DATA_DESC) {
709 * received XFER RDY from the target
710 * need to send data to the target
712 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
713 dd = fc_frame_payload_get(fp, sizeof(*dd));
716 rc = fc_fcp_send_data(fsp, seq,
717 (size_t) ntohl(dd->ft_data_ro),
718 (size_t) ntohl(dd->ft_burst_len));
720 seq->rec_data = fsp->xfer_len;
721 else if (rc == -ENOMEM)
722 fsp->state |= FC_SRB_NOMEM;
723 } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
725 * received a DATA frame
726 * next we will copy the data to the system buffer
728 WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
729 fc_fcp_recv_data(fsp, fp);
730 seq->rec_data = fsp->xfer_contig_end;
731 } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
732 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
734 fc_fcp_resp(fsp, fp);
736 FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
739 fc_fcp_unlock_pkt(fsp);
744 fc_fcp_error(fsp, fp);
745 else if (rc == -ENOMEM)
746 fc_fcp_reduce_can_queue(lport);
749 static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
751 struct fc_frame_header *fh;
752 struct fcp_resp *fc_rp;
753 struct fcp_resp_ext *rp_ex;
754 struct fcp_resp_rsp_info *fc_rp_info;
762 fh = (struct fc_frame_header *)fr_hdr(fp);
763 if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
766 fc_rp = (struct fcp_resp *)(fh + 1);
767 fsp->cdb_status = fc_rp->fr_status;
768 flags = fc_rp->fr_flags;
769 fsp->scsi_comp_flags = flags;
770 expected_len = fsp->data_len;
772 /* if ddp, update xfer len */
773 fc_fcp_ddp_done(fsp);
775 if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
776 rp_ex = (void *)(fc_rp + 1);
777 if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
778 if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
780 fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
781 if (flags & FCP_RSP_LEN_VAL) {
782 respl = ntohl(rp_ex->fr_rsp_len);
783 if (respl != sizeof(*fc_rp_info))
785 if (fsp->wait_for_comp) {
786 /* Abuse cdb_status for rsp code */
787 fsp->cdb_status = fc_rp_info->rsp_code;
788 complete(&fsp->tm_done);
790 * tmfs will not have any scsi cmd so
797 if (flags & FCP_SNS_LEN_VAL) {
798 snsl = ntohl(rp_ex->fr_sns_len);
799 if (snsl > SCSI_SENSE_BUFFERSIZE)
800 snsl = SCSI_SENSE_BUFFERSIZE;
801 memcpy(fsp->cmd->sense_buffer,
802 (char *)fc_rp_info + respl, snsl);
805 if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
806 if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
808 if (flags & FCP_RESID_UNDER) {
809 fsp->scsi_resid = ntohl(rp_ex->fr_resid);
811 * The cmnd->underflow is the minimum number of
812 * bytes that must be transfered for this
813 * command. Provided a sense condition is not
814 * present, make sure the actual amount
815 * transferred is at least the underflow value
818 if (!(flags & FCP_SNS_LEN_VAL) &&
819 (fc_rp->fr_status == 0) &&
820 (scsi_bufflen(fsp->cmd) -
821 fsp->scsi_resid) < fsp->cmd->underflow)
823 expected_len -= fsp->scsi_resid;
825 fsp->status_code = FC_ERROR;
829 fsp->state |= FC_SRB_RCV_STATUS;
832 * Check for missing or extra data frames.
834 if (unlikely(fsp->xfer_len != expected_len)) {
835 if (fsp->xfer_len < expected_len) {
837 * Some data may be queued locally,
838 * Wait a at least one jiffy to see if it is delivered.
839 * If this expires without data, we may do SRR.
841 fc_fcp_timer_set(fsp, 2);
844 fsp->status_code = FC_DATA_OVRRUN;
845 FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
846 "len %x, data len %x\n",
848 fsp->xfer_len, expected_len, fsp->data_len);
850 fc_fcp_complete_locked(fsp);
854 FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
855 "snsl %u\n", flags, fr_len(fp), respl, snsl);
857 fsp->status_code = FC_ERROR;
858 fc_fcp_complete_locked(fsp);
862 * fc_fcp_complete_locked() - complete processing of a fcp packet
865 * This function may sleep if a timer is pending. The packet lock must be
866 * held, and the host lock must not be held.
868 static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
870 struct fc_lport *lp = fsp->lp;
875 if (fsp->state & FC_SRB_ABORT_PENDING)
878 if (fsp->state & FC_SRB_ABORTED) {
879 if (!fsp->status_code)
880 fsp->status_code = FC_CMD_ABORTED;
883 * Test for transport underrun, independent of response
886 if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
887 (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
888 fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
889 fsp->status_code = FC_DATA_UNDRUN;
897 if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
898 struct fc_frame *conf_frame;
901 csp = lp->tt.seq_start_next(seq);
902 conf_frame = fc_frame_alloc(fsp->lp, 0);
904 f_ctl = FC_FC_SEQ_INIT;
905 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
906 ep = fc_seq_exch(seq);
907 fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
909 FC_TYPE_FCP, f_ctl, 0);
910 lp->tt.seq_send(lp, csp, conf_frame);
913 lp->tt.exch_done(seq);
918 static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
920 struct fc_lport *lp = fsp->lp;
923 lp->tt.exch_done(fsp->seq_ptr);
926 fsp->status_code = error;
930 * fc_fcp_cleanup_each_cmd() - Cleanup active commads
934 * @error: fsp status code
936 * If lun or id is -1, they are ignored.
938 static void fc_fcp_cleanup_each_cmd(struct fc_lport *lp, unsigned int id,
939 unsigned int lun, int error)
941 struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
942 struct fc_fcp_pkt *fsp;
943 struct scsi_cmnd *sc_cmd;
946 spin_lock_irqsave(lp->host->host_lock, flags);
948 list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
950 if (id != -1 && scmd_id(sc_cmd) != id)
953 if (lun != -1 && sc_cmd->device->lun != lun)
956 fc_fcp_pkt_hold(fsp);
957 spin_unlock_irqrestore(lp->host->host_lock, flags);
959 if (!fc_fcp_lock_pkt(fsp)) {
960 fc_fcp_cleanup_cmd(fsp, error);
962 fc_fcp_unlock_pkt(fsp);
965 fc_fcp_pkt_release(fsp);
966 spin_lock_irqsave(lp->host->host_lock, flags);
968 * while we dropped the lock multiple pkts could
969 * have been released, so we have to start over.
973 spin_unlock_irqrestore(lp->host->host_lock, flags);
976 static void fc_fcp_abort_io(struct fc_lport *lp)
978 fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_HRD_ERROR);
982 * fc_fcp_pkt_send() - send a fcp packet to the lower level.
986 * This is called by upper layer protocol.
987 * Return : zero for success and -1 for failure
988 * Context : called from queuecommand which can be called from process
990 * Locks : called with the host lock and irqs disabled.
992 static int fc_fcp_pkt_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
994 struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
997 fsp->cmd->SCp.ptr = (char *)fsp;
998 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
999 fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
1001 int_to_scsilun(fsp->cmd->device->lun,
1002 (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1003 memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
1004 list_add_tail(&fsp->list, &si->scsi_pkt_queue);
1006 spin_unlock_irq(lp->host->host_lock);
1007 rc = lp->tt.fcp_cmd_send(lp, fsp, fc_fcp_recv);
1008 spin_lock_irq(lp->host->host_lock);
1010 list_del(&fsp->list);
1015 static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
1016 void (*resp)(struct fc_seq *,
1017 struct fc_frame *fp,
1020 struct fc_frame *fp;
1022 struct fc_rport *rport;
1023 struct fc_rport_libfc_priv *rp;
1024 const size_t len = sizeof(fsp->cdb_cmd);
1027 if (fc_fcp_lock_pkt(fsp))
1030 fp = fc_frame_alloc(lp, sizeof(fsp->cdb_cmd));
1036 memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
1039 fsp->max_payload = rport->maxframe_size;
1040 rp = rport->dd_data;
1042 fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
1043 fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
1044 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1046 seq = lp->tt.exch_seq_send(lp, fp, resp, fc_fcp_pkt_destroy, fsp, 0);
1051 fsp->last_pkt_time = jiffies;
1053 fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
1055 setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
1056 fc_fcp_timer_set(fsp,
1057 (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
1058 FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
1060 fc_fcp_unlock_pkt(fsp);
1065 * transport error handler
1067 static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1069 int error = PTR_ERR(fp);
1071 if (fc_fcp_lock_pkt(fsp))
1074 if (error == -FC_EX_CLOSED) {
1075 fc_fcp_retry_cmd(fsp);
1080 * clear abort pending, because the lower layer
1081 * decided to force completion.
1083 fsp->state &= ~FC_SRB_ABORT_PENDING;
1084 fsp->status_code = FC_CMD_PLOGO;
1085 fc_fcp_complete_locked(fsp);
1087 fc_fcp_unlock_pkt(fsp);
1091 * Scsi abort handler- calls to send an abort
1092 * and then wait for abort completion
1094 static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
1098 if (fc_fcp_send_abort(fsp))
1101 init_completion(&fsp->tm_done);
1102 fsp->wait_for_comp = 1;
1104 spin_unlock_bh(&fsp->scsi_pkt_lock);
1105 rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1106 spin_lock_bh(&fsp->scsi_pkt_lock);
1107 fsp->wait_for_comp = 0;
1110 FC_FCP_DBG(fsp, "target abort cmd failed\n");
1112 } else if (fsp->state & FC_SRB_ABORTED) {
1113 FC_FCP_DBG(fsp, "target abort cmd passed\n");
1115 fc_fcp_complete_locked(fsp);
1122 * Retry LUN reset after resource allocation failed.
1124 static void fc_lun_reset_send(unsigned long data)
1126 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1127 struct fc_lport *lp = fsp->lp;
1128 if (lp->tt.fcp_cmd_send(lp, fsp, fc_tm_done)) {
1129 if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
1131 if (fc_fcp_lock_pkt(fsp))
1133 setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
1134 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1135 fc_fcp_unlock_pkt(fsp);
1140 * Scsi device reset handler- send a LUN RESET to the device
1141 * and wait for reset reply
1143 static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
1144 unsigned int id, unsigned int lun)
1148 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1149 fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
1150 int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1152 fsp->wait_for_comp = 1;
1153 init_completion(&fsp->tm_done);
1155 fc_lun_reset_send((unsigned long)fsp);
1158 * wait for completion of reset
1159 * after that make sure all commands are terminated
1161 rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1163 spin_lock_bh(&fsp->scsi_pkt_lock);
1164 fsp->state |= FC_SRB_COMPL;
1165 spin_unlock_bh(&fsp->scsi_pkt_lock);
1167 del_timer_sync(&fsp->timer);
1169 spin_lock_bh(&fsp->scsi_pkt_lock);
1171 lp->tt.exch_done(fsp->seq_ptr);
1172 fsp->seq_ptr = NULL;
1174 fsp->wait_for_comp = 0;
1175 spin_unlock_bh(&fsp->scsi_pkt_lock);
1178 FC_SCSI_DBG(lp, "lun reset failed\n");
1182 /* cdb_status holds the tmf's rsp code */
1183 if (fsp->cdb_status != FCP_TMF_CMPL)
1186 FC_SCSI_DBG(lp, "lun reset to lun %u completed\n", lun);
1187 fc_fcp_cleanup_each_cmd(lp, id, lun, FC_CMD_ABORTED);
1192 * Task Managment response handler
1194 static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1196 struct fc_fcp_pkt *fsp = arg;
1197 struct fc_frame_header *fh;
1201 * If there is an error just let it timeout or wait
1202 * for TMF to be aborted if it timedout.
1204 * scsi-eh will escalate for when either happens.
1209 if (fc_fcp_lock_pkt(fsp))
1213 * raced with eh timeout handler.
1215 if (!fsp->seq_ptr || !fsp->wait_for_comp) {
1216 spin_unlock_bh(&fsp->scsi_pkt_lock);
1220 fh = fc_frame_header_get(fp);
1221 if (fh->fh_type != FC_TYPE_BLS)
1222 fc_fcp_resp(fsp, fp);
1223 fsp->seq_ptr = NULL;
1224 fsp->lp->tt.exch_done(seq);
1226 fc_fcp_unlock_pkt(fsp);
1229 static void fc_fcp_cleanup(struct fc_lport *lp)
1231 fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_ERROR);
1235 * fc_fcp_timeout: called by OS timer function.
1237 * The timer has been inactivated and must be reactivated if desired
1238 * using fc_fcp_timer_set().
1242 * If REC is supported, just issue it, and return. The REC exchange will
1243 * complete or time out, and recovery can continue at that point.
1245 * Otherwise, if the response has been received without all the data,
1246 * it has been ER_TIMEOUT since the response was received.
1248 * If the response has not been received,
1249 * we see if data was received recently. If it has been, we continue waiting,
1250 * otherwise, we abort the command.
1252 static void fc_fcp_timeout(unsigned long data)
1254 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1255 struct fc_rport *rport = fsp->rport;
1256 struct fc_rport_libfc_priv *rp = rport->dd_data;
1258 if (fc_fcp_lock_pkt(fsp))
1261 if (fsp->cdb_cmd.fc_tm_flags)
1264 fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
1266 if (rp->flags & FC_RP_FLAGS_REC_SUPPORTED)
1268 else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
1270 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1271 else if (fsp->state & FC_SRB_RCV_STATUS)
1272 fc_fcp_complete_locked(fsp);
1274 fc_timeout_error(fsp);
1275 fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
1277 fc_fcp_unlock_pkt(fsp);
1281 * Send a REC ELS request
1283 static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
1285 struct fc_lport *lp;
1286 struct fc_frame *fp;
1287 struct fc_rport *rport;
1288 struct fc_rport_libfc_priv *rp;
1292 rp = rport->dd_data;
1293 if (!fsp->seq_ptr || rp->rp_state != RPORT_ST_READY) {
1294 fsp->status_code = FC_HRD_ERROR;
1296 fc_fcp_complete_locked(fsp);
1299 fp = fc_frame_alloc(lp, sizeof(struct fc_els_rec));
1303 fr_seq(fp) = fsp->seq_ptr;
1304 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
1305 fc_host_port_id(rp->local_port->host), FC_TYPE_ELS,
1306 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1307 if (lp->tt.elsct_send(lp, rport->port_id, fp, ELS_REC, fc_fcp_rec_resp,
1308 fsp, jiffies_to_msecs(FC_SCSI_REC_TOV))) {
1309 fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
1313 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1314 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1316 fc_timeout_error(fsp);
1320 * Receive handler for REC ELS frame
1321 * if it is a reject then let the scsi layer to handle
1322 * the timeout. if it is a LS_ACC then if the io was not completed
1323 * then set the timeout and return otherwise complete the exchange
1324 * and tell the scsi layer to restart the I/O.
1326 static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1328 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
1329 struct fc_els_rec_acc *recp;
1330 struct fc_els_ls_rjt *rjt;
1334 enum dma_data_direction data_dir;
1336 struct fc_rport_libfc_priv *rp;
1339 fc_fcp_rec_error(fsp, fp);
1343 if (fc_fcp_lock_pkt(fsp))
1346 fsp->recov_retry = 0;
1347 opcode = fc_frame_payload_op(fp);
1348 if (opcode == ELS_LS_RJT) {
1349 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1350 switch (rjt->er_reason) {
1352 FC_FCP_DBG(fsp, "device %x unexpected REC reject "
1353 "reason %d expl %d\n",
1354 fsp->rport->port_id, rjt->er_reason,
1358 FC_FCP_DBG(fsp, "device does not support REC\n");
1359 rp = fsp->rport->dd_data;
1361 * if we do not spport RECs or got some bogus
1362 * reason then resetup timer so we check for
1365 rp->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
1366 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1371 * If no data transfer, the command frame got dropped
1372 * so we just retry. If data was transferred, we
1373 * lost the response but the target has no record,
1374 * so we abort and retry.
1376 if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
1377 fsp->xfer_len == 0) {
1378 fc_fcp_retry_cmd(fsp);
1381 fc_timeout_error(fsp);
1384 } else if (opcode == ELS_LS_ACC) {
1385 if (fsp->state & FC_SRB_ABORTED)
1388 data_dir = fsp->cmd->sc_data_direction;
1389 recp = fc_frame_payload_get(fp, sizeof(*recp));
1390 offset = ntohl(recp->reca_fc4value);
1391 e_stat = ntohl(recp->reca_e_stat);
1393 if (e_stat & ESB_ST_COMPLETE) {
1396 * The exchange is complete.
1398 * For output, we must've lost the response.
1399 * For input, all data must've been sent.
1400 * We lost may have lost the response
1401 * (and a confirmation was requested) and maybe
1404 * If all data received, send SRR
1405 * asking for response. If partial data received,
1406 * or gaps, SRR requests data at start of gap.
1407 * Recovery via SRR relies on in-order-delivery.
1409 if (data_dir == DMA_TO_DEVICE) {
1410 r_ctl = FC_RCTL_DD_CMD_STATUS;
1411 } else if (fsp->xfer_contig_end == offset) {
1412 r_ctl = FC_RCTL_DD_CMD_STATUS;
1414 offset = fsp->xfer_contig_end;
1415 r_ctl = FC_RCTL_DD_SOL_DATA;
1417 fc_fcp_srr(fsp, r_ctl, offset);
1418 } else if (e_stat & ESB_ST_SEQ_INIT) {
1421 * The remote port has the initiative, so just
1422 * keep waiting for it to complete.
1424 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1428 * The exchange is incomplete, we have seq. initiative.
1429 * Lost response with requested confirmation,
1430 * lost confirmation, lost transfer ready or
1433 * For output, if not all data was received, ask
1434 * for transfer ready to be repeated.
1436 * If we received or sent all the data, send SRR to
1439 * If we lost a response, we may have lost some read
1442 r_ctl = FC_RCTL_DD_SOL_DATA;
1443 if (data_dir == DMA_TO_DEVICE) {
1444 r_ctl = FC_RCTL_DD_CMD_STATUS;
1445 if (offset < fsp->data_len)
1446 r_ctl = FC_RCTL_DD_DATA_DESC;
1447 } else if (offset == fsp->xfer_contig_end) {
1448 r_ctl = FC_RCTL_DD_CMD_STATUS;
1449 } else if (fsp->xfer_contig_end < offset) {
1450 offset = fsp->xfer_contig_end;
1452 fc_fcp_srr(fsp, r_ctl, offset);
1456 fc_fcp_unlock_pkt(fsp);
1458 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1463 * Handle error response or timeout for REC exchange.
1465 static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1467 int error = PTR_ERR(fp);
1469 if (fc_fcp_lock_pkt(fsp))
1474 fc_fcp_retry_cmd(fsp);
1478 FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
1479 fsp, fsp->rport->port_id, error);
1480 fsp->status_code = FC_CMD_PLOGO;
1483 case -FC_EX_TIMEOUT:
1485 * Assume REC or LS_ACC was lost.
1486 * The exchange manager will have aborted REC, so retry.
1488 FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
1489 fsp->rport->port_id, error, fsp->recov_retry,
1490 FC_MAX_RECOV_RETRY);
1491 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1494 fc_timeout_error(fsp);
1497 fc_fcp_unlock_pkt(fsp);
1499 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1503 * Time out error routine:
1504 * abort's the I/O close the exchange and
1505 * send completion notification to scsi layer
1507 static void fc_timeout_error(struct fc_fcp_pkt *fsp)
1509 fsp->status_code = FC_CMD_TIME_OUT;
1510 fsp->cdb_status = 0;
1513 * if this fails then we let the scsi command timer fire and
1516 fc_fcp_send_abort(fsp);
1520 * Sequence retransmission request.
1521 * This is called after receiving status but insufficient data, or
1522 * when expecting status but the request has timed out.
1524 static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1526 struct fc_lport *lp = fsp->lp;
1527 struct fc_rport *rport;
1528 struct fc_rport_libfc_priv *rp;
1529 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
1531 struct fcp_srr *srr;
1532 struct fc_frame *fp;
1536 rp = rport->dd_data;
1537 cdb_op = fsp->cdb_cmd.fc_cdb[0];
1539 if (!(rp->flags & FC_RP_FLAGS_RETRY) || rp->rp_state != RPORT_ST_READY)
1540 goto retry; /* shouldn't happen */
1541 fp = fc_frame_alloc(lp, sizeof(*srr));
1545 srr = fc_frame_payload_get(fp, sizeof(*srr));
1546 memset(srr, 0, sizeof(*srr));
1547 srr->srr_op = ELS_SRR;
1548 srr->srr_ox_id = htons(ep->oxid);
1549 srr->srr_rx_id = htons(ep->rxid);
1550 srr->srr_r_ctl = r_ctl;
1551 srr->srr_rel_off = htonl(offset);
1553 fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
1554 fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
1555 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1557 seq = lp->tt.exch_seq_send(lp, fp, fc_fcp_srr_resp, NULL,
1558 fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
1562 fsp->recov_seq = seq;
1563 fsp->xfer_len = offset;
1564 fsp->xfer_contig_end = offset;
1565 fsp->state &= ~FC_SRB_RCV_STATUS;
1566 fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
1569 fc_fcp_retry_cmd(fsp);
1573 * Handle response from SRR.
1575 static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1577 struct fc_fcp_pkt *fsp = arg;
1578 struct fc_frame_header *fh;
1581 fc_fcp_srr_error(fsp, fp);
1585 if (fc_fcp_lock_pkt(fsp))
1588 fh = fc_frame_header_get(fp);
1590 * BUG? fc_fcp_srr_error calls exch_done which would release
1591 * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
1592 * then fc_exch_timeout would be sending an abort. The exch_done
1593 * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
1594 * an abort response though.
1596 if (fh->fh_type == FC_TYPE_BLS) {
1597 fc_fcp_unlock_pkt(fsp);
1601 fsp->recov_seq = NULL;
1602 switch (fc_frame_payload_op(fp)) {
1604 fsp->recov_retry = 0;
1605 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1609 fc_timeout_error(fsp);
1612 fc_fcp_unlock_pkt(fsp);
1613 fsp->lp->tt.exch_done(seq);
1616 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
1619 static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1621 if (fc_fcp_lock_pkt(fsp))
1623 fsp->lp->tt.exch_done(fsp->recov_seq);
1624 fsp->recov_seq = NULL;
1625 switch (PTR_ERR(fp)) {
1626 case -FC_EX_TIMEOUT:
1627 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1630 fc_timeout_error(fsp);
1632 case -FC_EX_CLOSED: /* e.g., link failure */
1635 fc_fcp_retry_cmd(fsp);
1638 fc_fcp_unlock_pkt(fsp);
1640 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
1643 static inline int fc_fcp_lport_queue_ready(struct fc_lport *lp)
1646 return (lp->state == LPORT_ST_READY) && lp->link_up && !lp->qfull;
1650 * fc_queuecommand - The queuecommand function of the scsi template
1651 * @cmd: struct scsi_cmnd to be executed
1652 * @done: Callback function to be called when cmd is completed
1654 * this is the i/o strategy routine, called by the scsi layer
1655 * this routine is called with holding the host_lock.
1657 int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1659 struct fc_lport *lp;
1660 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1661 struct fc_fcp_pkt *fsp;
1662 struct fc_rport_libfc_priv *rp;
1665 struct fcoe_dev_stats *stats;
1667 lp = shost_priv(sc_cmd->device->host);
1669 rval = fc_remote_port_chkready(rport);
1671 sc_cmd->result = rval;
1676 if (!*(struct fc_remote_port **)rport->dd_data) {
1678 * rport is transitioning from blocked/deleted to
1681 sc_cmd->result = DID_IMM_RETRY << 16;
1686 rp = rport->dd_data;
1688 if (!fc_fcp_lport_queue_ready(lp)) {
1689 rc = SCSI_MLQUEUE_HOST_BUSY;
1693 fsp = fc_fcp_pkt_alloc(lp, GFP_ATOMIC);
1695 rc = SCSI_MLQUEUE_HOST_BUSY;
1700 * build the libfc request pkt
1702 fsp->cmd = sc_cmd; /* save the cmd */
1703 fsp->lp = lp; /* save the softc ptr */
1704 fsp->rport = rport; /* set the remote port ptr */
1705 fsp->xfer_ddp = FC_XID_UNKNOWN;
1706 sc_cmd->scsi_done = done;
1709 * set up the transfer length
1711 fsp->data_len = scsi_bufflen(sc_cmd);
1715 * setup the data direction
1717 stats = fc_lport_get_stats(lp);
1718 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1719 fsp->req_flags = FC_SRB_READ;
1720 stats->InputRequests++;
1721 stats->InputMegabytes = fsp->data_len;
1722 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
1723 fsp->req_flags = FC_SRB_WRITE;
1724 stats->OutputRequests++;
1725 stats->OutputMegabytes = fsp->data_len;
1728 stats->ControlRequests++;
1731 fsp->tgt_flags = rp->flags;
1733 init_timer(&fsp->timer);
1734 fsp->timer.data = (unsigned long)fsp;
1737 * send it to the lower layer
1738 * if we get -1 return then put the request in the pending
1741 rval = fc_fcp_pkt_send(lp, fsp);
1743 fsp->state = FC_SRB_FREE;
1744 fc_fcp_pkt_release(fsp);
1745 rc = SCSI_MLQUEUE_HOST_BUSY;
1750 EXPORT_SYMBOL(fc_queuecommand);
1753 * fc_io_compl() - Handle responses for completed commands
1756 * Translates a error to a Linux SCSI error.
1758 * The fcp packet lock must be held when calling.
1760 static void fc_io_compl(struct fc_fcp_pkt *fsp)
1762 struct fc_fcp_internal *si;
1763 struct scsi_cmnd *sc_cmd;
1764 struct fc_lport *lp;
1765 unsigned long flags;
1767 /* release outstanding ddp context */
1768 fc_fcp_ddp_done(fsp);
1770 fsp->state |= FC_SRB_COMPL;
1771 if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
1772 spin_unlock_bh(&fsp->scsi_pkt_lock);
1773 del_timer_sync(&fsp->timer);
1774 spin_lock_bh(&fsp->scsi_pkt_lock);
1778 si = fc_get_scsi_internal(lp);
1779 spin_lock_irqsave(lp->host->host_lock, flags);
1781 spin_unlock_irqrestore(lp->host->host_lock, flags);
1786 * if a command timed out while we had to try and throttle IO
1787 * and it is now getting cleaned up, then we are about to
1788 * try again so clear the throttled flag incase we get more
1791 if (si->throttled && fsp->state & FC_SRB_NOMEM)
1797 if (!sc_cmd->SCp.ptr) {
1798 spin_unlock_irqrestore(lp->host->host_lock, flags);
1802 CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
1803 switch (fsp->status_code) {
1805 if (fsp->cdb_status == 0) {
1809 sc_cmd->result = DID_OK << 16;
1810 if (fsp->scsi_resid)
1811 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1814 * transport level I/O was ok but scsi
1815 * has non zero status
1817 sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
1821 sc_cmd->result = DID_ERROR << 16;
1823 case FC_DATA_UNDRUN:
1824 if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
1826 * scsi status is good but transport level
1829 sc_cmd->result = (fsp->state & FC_SRB_RCV_STATUS ?
1830 DID_OK : DID_ERROR) << 16;
1833 * scsi got underrun, this is an error
1835 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1836 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1839 case FC_DATA_OVRRUN:
1841 * overrun is an error
1843 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1845 case FC_CMD_ABORTED:
1846 sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
1848 case FC_CMD_TIME_OUT:
1849 sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
1852 sc_cmd->result = (DID_RESET << 16);
1855 sc_cmd->result = (DID_NO_CONNECT << 16);
1858 sc_cmd->result = (DID_ERROR << 16);
1862 list_del(&fsp->list);
1863 sc_cmd->SCp.ptr = NULL;
1864 sc_cmd->scsi_done(sc_cmd);
1865 spin_unlock_irqrestore(lp->host->host_lock, flags);
1867 /* release ref from initial allocation in queue command */
1868 fc_fcp_pkt_release(fsp);
1872 * fc_eh_abort() - Abort a command
1873 * @sc_cmd: scsi command to abort
1875 * From scsi host template.
1876 * send ABTS to the target device and wait for the response
1877 * sc_cmd is the pointer to the command to be aborted.
1879 int fc_eh_abort(struct scsi_cmnd *sc_cmd)
1881 struct fc_fcp_pkt *fsp;
1882 struct fc_lport *lp;
1884 unsigned long flags;
1886 lp = shost_priv(sc_cmd->device->host);
1887 if (lp->state != LPORT_ST_READY)
1889 else if (!lp->link_up)
1892 spin_lock_irqsave(lp->host->host_lock, flags);
1893 fsp = CMD_SP(sc_cmd);
1895 /* command completed while scsi eh was setting up */
1896 spin_unlock_irqrestore(lp->host->host_lock, flags);
1899 /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
1900 fc_fcp_pkt_hold(fsp);
1901 spin_unlock_irqrestore(lp->host->host_lock, flags);
1903 if (fc_fcp_lock_pkt(fsp)) {
1904 /* completed while we were waiting for timer to be deleted */
1909 rc = fc_fcp_pkt_abort(fsp);
1910 fc_fcp_unlock_pkt(fsp);
1913 fc_fcp_pkt_release(fsp);
1916 EXPORT_SYMBOL(fc_eh_abort);
1919 * fc_eh_device_reset() Reset a single LUN
1920 * @sc_cmd: scsi command
1922 * Set from scsi host template to send tm cmd to the target and wait for the
1925 int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1927 struct fc_lport *lp;
1928 struct fc_fcp_pkt *fsp;
1929 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1931 struct fc_rport_libfc_priv *rp;
1934 rval = fc_remote_port_chkready(rport);
1938 rp = rport->dd_data;
1939 lp = shost_priv(sc_cmd->device->host);
1941 if (lp->state != LPORT_ST_READY)
1944 FC_SCSI_DBG(lp, "Resetting rport (%6x)\n", rport->port_id);
1946 fsp = fc_fcp_pkt_alloc(lp, GFP_NOIO);
1948 printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
1949 sc_cmd->result = DID_NO_CONNECT << 16;
1954 * Build the libfc request pkt. Do not set the scsi cmnd, because
1955 * the sc passed in is not setup for execution like when sent
1956 * through the queuecommand callout.
1958 fsp->lp = lp; /* save the softc ptr */
1959 fsp->rport = rport; /* set the remote port ptr */
1962 * flush outstanding commands
1964 rc = fc_lun_reset(lp, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
1965 fsp->state = FC_SRB_FREE;
1966 fc_fcp_pkt_release(fsp);
1971 EXPORT_SYMBOL(fc_eh_device_reset);
1974 * fc_eh_host_reset() - The reset function will reset the ports on the host.
1975 * @sc_cmd: scsi command
1977 int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
1979 struct Scsi_Host *shost = sc_cmd->device->host;
1980 struct fc_lport *lp = shost_priv(shost);
1981 unsigned long wait_tmo;
1983 FC_SCSI_DBG(lp, "Resetting host\n");
1985 lp->tt.lport_reset(lp);
1986 wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
1987 while (!fc_fcp_lport_queue_ready(lp) && time_before(jiffies, wait_tmo))
1990 if (fc_fcp_lport_queue_ready(lp)) {
1991 shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
1992 "on port (%6x)\n", fc_host_port_id(lp->host));
1995 shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
1996 "port (%6x) is not ready.\n",
1997 fc_host_port_id(lp->host));
2001 EXPORT_SYMBOL(fc_eh_host_reset);
2004 * fc_slave_alloc() - configure queue depth
2005 * @sdev: scsi device
2007 * Configures queue depth based on host's cmd_per_len. If not set
2008 * then we use the libfc default.
2010 int fc_slave_alloc(struct scsi_device *sdev)
2012 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2014 if (!rport || fc_remote_port_chkready(rport))
2017 if (sdev->tagged_supported)
2018 scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
2020 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2021 FC_FCP_DFLT_QUEUE_DEPTH);
2025 EXPORT_SYMBOL(fc_slave_alloc);
2027 int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
2030 case SCSI_QDEPTH_DEFAULT:
2031 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2033 case SCSI_QDEPTH_QFULL:
2034 scsi_track_queue_full(sdev, qdepth);
2036 case SCSI_QDEPTH_RAMP_UP:
2037 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2042 return sdev->queue_depth;
2044 EXPORT_SYMBOL(fc_change_queue_depth);
2046 int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
2048 if (sdev->tagged_supported) {
2049 scsi_set_tag_type(sdev, tag_type);
2051 scsi_activate_tcq(sdev, sdev->queue_depth);
2053 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2059 EXPORT_SYMBOL(fc_change_queue_type);
2061 void fc_fcp_destroy(struct fc_lport *lp)
2063 struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
2065 if (!list_empty(&si->scsi_pkt_queue))
2066 printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
2067 "port (%6x)\n", fc_host_port_id(lp->host));
2069 mempool_destroy(si->scsi_pkt_pool);
2071 lp->scsi_priv = NULL;
2073 EXPORT_SYMBOL(fc_fcp_destroy);
2075 int fc_fcp_init(struct fc_lport *lp)
2078 struct fc_fcp_internal *si;
2080 if (!lp->tt.fcp_cmd_send)
2081 lp->tt.fcp_cmd_send = fc_fcp_cmd_send;
2083 if (!lp->tt.fcp_cleanup)
2084 lp->tt.fcp_cleanup = fc_fcp_cleanup;
2086 if (!lp->tt.fcp_abort_io)
2087 lp->tt.fcp_abort_io = fc_fcp_abort_io;
2089 si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
2093 INIT_LIST_HEAD(&si->scsi_pkt_queue);
2095 si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
2096 if (!si->scsi_pkt_pool) {
2106 EXPORT_SYMBOL(fc_fcp_init);
2108 static int __init libfc_init(void)
2112 scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
2113 sizeof(struct fc_fcp_pkt),
2114 0, SLAB_HWCACHE_ALIGN, NULL);
2115 if (scsi_pkt_cachep == NULL) {
2116 printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
2117 "module load failed!");
2121 rc = fc_setup_exch_mgr();
2123 goto destroy_pkt_cache;
2125 rc = fc_setup_rport();
2131 fc_destroy_exch_mgr();
2133 kmem_cache_destroy(scsi_pkt_cachep);
2137 static void __exit libfc_exit(void)
2139 kmem_cache_destroy(scsi_pkt_cachep);
2140 fc_destroy_exch_mgr();
2144 module_init(libfc_init);
2145 module_exit(libfc_exit);