22 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
30 #include <linux/slab.h>
33 #include <scsi/scsi.h>
49 #define FC_SRB_CMD_SENT (1 << 0)
50 #define FC_SRB_RCV_STATUS (1 << 1)
51 #define FC_SRB_ABORT_PENDING (1 << 2)
52 #define FC_SRB_ABORTED (1 << 3)
53 #define FC_SRB_DISCONTIG (1 << 4)
54 #define FC_SRB_COMPL (1 << 5)
55 #define FC_SRB_FCP_PROCESSING_TMO (1 << 6)
57 #define FC_SRB_READ (1 << 1)
58 #define FC_SRB_WRITE (1 << 0)
63 #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
64 #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
65 #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
66 #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
67 #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
87 #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
94 static void fc_fcp_recv(
struct fc_seq *,
struct fc_frame *,
void *);
96 static void fc_fcp_complete_locked(
struct fc_fcp_pkt *);
97 static void fc_tm_done(
struct fc_seq *,
struct fc_frame *,
void *);
100 static void fc_fcp_timeout(
unsigned long);
103 static void fc_fcp_rec_resp(
struct fc_seq *,
struct fc_frame *,
void *);
107 static void fc_fcp_srr_resp(
struct fc_seq *,
struct fc_frame *,
void *);
113 #define FC_COMPLETE 0
114 #define FC_CMD_ABORTED 1
115 #define FC_CMD_RESET 2
116 #define FC_CMD_PLOGO 3
118 #define FC_TRANS_ERR 5
119 #define FC_DATA_OVRRUN 6
120 #define FC_DATA_UNDRUN 7
122 #define FC_HRD_ERROR 9
123 #define FC_CRC_ERROR 10
124 #define FC_TIMED_OUT 11
129 #define FC_SCSI_TM_TOV (10 * HZ)
130 #define FC_HOST_RESET_TIMEOUT (30 * HZ)
131 #define FC_CAN_QUEUE_PERIOD (60 * HZ)
133 #define FC_MAX_ERROR_CNT 5
134 #define FC_MAX_RECOV_RETRY 3
136 #define FC_FCP_DFLT_QUEUE_DEPTH 32
153 memset(fsp, 0,
sizeof(*fsp));
159 INIT_LIST_HEAD(&fsp->
list);
175 static void fc_fcp_pkt_release(
struct fc_fcp_pkt *fsp)
188 static void fc_fcp_pkt_hold(
struct fc_fcp_pkt *fsp)
204 static void fc_fcp_pkt_destroy(
struct fc_seq *seq,
void *fsp)
206 fc_fcp_pkt_release(fsp);
226 static inline int fc_fcp_lock_pkt(
struct fc_fcp_pkt *fsp)
234 fc_fcp_pkt_hold(fsp);
243 static inline void fc_fcp_unlock_pkt(
struct fc_fcp_pkt *fsp)
246 fc_fcp_pkt_release(fsp);
254 static void fc_fcp_timer_set(
struct fc_fcp_pkt *fsp,
unsigned long delay)
265 static int fc_fcp_send_abort(
struct fc_fcp_pkt *fsp)
274 return fsp->
lp->tt.seq_exch_abort(fsp->
seq_ptr, 0);
286 static void fc_fcp_retry_cmd(
struct fc_fcp_pkt *fsp)
296 fc_fcp_complete_locked(fsp);
311 if (lport->
tt.ddp_setup(lport, xid, scsi_sglist(fsp->
cmd),
312 scsi_sg_count(fsp->
cmd)))
333 if (lport->
tt.ddp_done) {
362 can_queue = lport->
host->can_queue << 1;
369 "can_queue to %d.\n", can_queue);
372 spin_unlock_irqrestore(lport->
host->host_lock, flags);
385 static void fc_fcp_can_queue_ramp_down(
struct fc_lport *lport)
400 can_queue = lport->
host->can_queue;
406 "Reducing can_queue to %d.\n", can_queue);
409 spin_unlock_irqrestore(lport->
host->host_lock, flags);
425 fp = fc_frame_alloc(lport, len);
432 fc_fcp_can_queue_ramp_down(lport);
457 fh = fc_frame_header_get(fp);
460 len =
fr_len(fp) -
sizeof(*fh);
461 buf = fc_frame_payload_get(fp, 0);
472 FC_FCP_DBG(fsp,
"DDP I/O in fc_fcp_recv_data set ERROR\n");
481 FC_FCP_DBG(fsp,
"data received past end. len %zx offset %zx "
482 "data_len %x\n", len, offset, fsp->
data_len);
491 sg = scsi_sglist(sc);
492 nents = scsi_sg_count(sc);
494 if (!(
fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
498 crc =
crc32(~0, (
u8 *) fh,
sizeof(*fh));
501 buf = fc_frame_payload_get(fp, 0);
503 crc =
crc32(crc, buf + len, 4 - (len % 4));
512 "frame for port (%6.6x)\n",
540 fc_fcp_complete_locked(fsp);
543 fc_fcp_recovery(fsp, host_bcode);
560 size_t offset,
size_t seq_blen)
583 FC_FCP_DBG(fsp,
"xfer-ready past end. seq_blen %zx "
584 "offset %zx\n", seq_blen, offset);
585 fc_fcp_send_abort(fsp);
587 }
else if (offset != fsp->
xfer_len) {
590 "seq_blen %zx offset %zx\n", seq_blen, offset);
600 t_blen =
min(seq_blen, (
size_t)lport->
lso_max);
601 FC_FCP_DBG(fsp,
"fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
602 fsp, seq_blen, lport->
lso_max, t_blen);
606 t_blen &= ~(512 - 1);
609 remaining = seq_blen;
610 fh_parm_offset = frame_offset =
offset;
612 seq = lport->
tt.seq_start_next(seq);
616 sg = scsi_sglist(sc);
618 while (remaining > 0 && sg) {
619 if (offset >= sg->
length) {
625 tlen =
min(t_blen, remaining);
634 fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
638 data = fc_frame_header_get(fp) + 1;
643 off = offset + sg->
offset;
644 sg_bytes =
min(tlen, sg->
length - offset);
645 sg_bytes =
min(sg_bytes,
650 skb_fill_page_desc(
fp_skb(fp),
651 skb_shinfo(
fp_skb(fp))->nr_frags,
653 fp_skb(fp)->data_len += sg_bytes;
668 frame_offset += sg_bytes;
670 remaining -= sg_bytes;
690 error = lport->
tt.seq_send(lport, seq, fp);
712 fh = fc_frame_header_get(fp);
717 brp = fc_frame_payload_get(fp,
sizeof(*brp));
737 fc_fcp_complete_locked(fsp);
760 fc_fcp_error(fsp, fp);
764 fh = fc_frame_header_get(fp);
769 if (fc_fcp_lock_pkt(fsp))
773 fc_fcp_abts_resp(fsp, fp);
786 dd = fc_frame_payload_get(fp,
sizeof(*dd));
789 rc = fc_fcp_send_data(fsp, seq,
800 fc_fcp_recv_data(fsp, fp);
805 fc_fcp_resp(fsp, fp);
807 FC_FCP_DBG(fsp,
"unexpected frame. r_ctl %x\n", r_ctl);
810 fc_fcp_unlock_pkt(fsp);
834 if (
unlikely(plen <
sizeof(*fh) +
sizeof(*fc_rp)))
837 fc_rp = (
struct fcp_resp *)(fh + 1);
847 rp_ex = (
void *)(fc_rp + 1);
849 if (plen <
sizeof(*fc_rp) +
sizeof(*rp_ex))
873 (
char *)fc_rp_info + respl, snsl);
877 if (plen <
sizeof(*fc_rp) +
sizeof(rp_ex->
fr_resid))
889 if (!(flags & FCP_SNS_LEN_VAL) &&
891 (scsi_bufflen(fsp->
cmd) -
912 fc_fcp_timer_set(fsp, 2);
916 FC_FCP_DBG(fsp,
"tgt %6.6x xfer len %zx greater than expected, "
917 "len %x, data len %x\n",
921 fc_fcp_complete_locked(fsp);
925 FC_FCP_DBG(fsp,
"short FCP response. flags 0x%x len %u respl %u "
926 "snsl %u\n", flags,
fr_len(fp), respl, snsl);
929 fc_fcp_complete_locked(fsp);
940 static void fc_fcp_complete_locked(
struct fc_fcp_pkt *fsp)
973 csp = lport->
tt.seq_start_next(seq);
974 conf_frame = fc_fcp_frame_alloc(fsp->
lp, 0);
982 lport->
tt.seq_send(lport, csp, conf_frame);
985 lport->
tt.exch_done(seq);
1001 static void fc_fcp_cleanup_cmd(
struct fc_fcp_pkt *fsp,
int error)
1021 static void fc_fcp_cleanup_each_cmd(
struct fc_lport *lport,
unsigned int id,
1022 unsigned int lun,
int error)
1027 unsigned long flags;
1033 if (
id != -1 &&
scmd_id(sc_cmd) !=
id)
1036 if (lun != -1 && sc_cmd->
device->lun != lun)
1039 fc_fcp_pkt_hold(fsp);
1042 if (!fc_fcp_lock_pkt(fsp)) {
1043 fc_fcp_cleanup_cmd(fsp, error);
1045 fc_fcp_unlock_pkt(fsp);
1048 fc_fcp_pkt_release(fsp);
1063 static void fc_fcp_abort_io(
struct fc_lport *lport)
1079 unsigned long flags;
1082 fsp->
cmd->SCp.ptr = (
char *)fsp;
1092 rc = lport->
tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
1109 static inline unsigned int get_fsp_rec_tov(
struct fc_fcp_pkt *fsp)
1131 const size_t len =
sizeof(fsp->
cdb_cmd);
1134 if (fc_fcp_lock_pkt(fsp))
1137 fp = fc_fcp_frame_alloc(lport,
sizeof(fsp->
cdb_cmd));
1153 seq = lport->
tt.exch_seq_send(lport, fp,
resp, fc_fcp_pkt_destroy,
1160 fc_fcp_pkt_hold(fsp);
1164 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
1167 fc_fcp_unlock_pkt(fsp);
1178 int error = PTR_ERR(fp);
1180 if (fc_fcp_lock_pkt(fsp))
1184 fc_fcp_retry_cmd(fsp);
1194 fc_fcp_complete_locked(fsp);
1196 fc_fcp_unlock_pkt(fsp);
1205 static int fc_fcp_pkt_abort(
struct fc_fcp_pkt *fsp)
1208 unsigned long ticks_left;
1210 if (fc_fcp_send_abort(fsp))
1213 init_completion(&fsp->
tm_done);
1223 FC_FCP_DBG(fsp,
"target abort cmd failed\n");
1225 FC_FCP_DBG(fsp,
"target abort cmd passed\n");
1227 fc_fcp_complete_locked(fsp);
1237 static void fc_lun_reset_send(
unsigned long data)
1242 if (lport->
tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
1245 if (fc_fcp_lock_pkt(fsp))
1248 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
1249 fc_fcp_unlock_pkt(fsp);
1262 unsigned int id,
unsigned int lun)
1271 init_completion(&fsp->
tm_done);
1273 fc_lun_reset_send((
unsigned long)fsp);
1304 FC_SCSI_DBG(lport,
"lun reset to lun %u completed\n", lun);
1315 static void fc_tm_done(
struct fc_seq *seq,
struct fc_frame *fp,
void *arg)
1330 if (fc_fcp_lock_pkt(fsp))
1339 fh = fc_frame_header_get(fp);
1341 fc_fcp_resp(fsp, fp);
1343 fsp->
lp->tt.exch_done(seq);
1345 fc_fcp_unlock_pkt(fsp);
1354 static void fc_fcp_cleanup(
struct fc_lport *lport)
1356 fc_fcp_cleanup_each_cmd(lport, -1, -1,
FC_ERROR);
1370 static void fc_fcp_timeout(
unsigned long data)
1376 if (fc_fcp_lock_pkt(fsp))
1385 fc_fcp_complete_locked(fsp);
1392 fc_fcp_unlock_pkt(fsp);
1399 static void fc_fcp_rec(
struct fc_fcp_pkt *fsp)
1412 fc_fcp_complete_locked(fsp);
1416 fp = fc_fcp_frame_alloc(lport,
sizeof(
struct fc_els_rec));
1425 fc_fcp_rec_resp, fsp,
1427 fc_fcp_pkt_hold(fsp);
1432 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
1448 static void fc_fcp_rec_resp(
struct fc_seq *seq,
struct fc_frame *fp,
void *arg)
1461 fc_fcp_rec_error(fsp, fp);
1465 if (fc_fcp_lock_pkt(fsp))
1469 opcode = fc_frame_payload_op(fp);
1471 rjt = fc_frame_payload_get(fp,
sizeof(*rjt));
1474 FC_FCP_DBG(fsp,
"device %x unexpected REC reject "
1475 "reason %d expl %d\n",
1480 FC_FCP_DBG(fsp,
"device does not support REC\n");
1481 rpriv = fsp->
rport->dd_data;
1499 fc_fcp_retry_cmd(fsp);
1509 data_dir = fsp->
cmd->sc_data_direction;
1510 recp = fc_frame_payload_get(fp,
sizeof(*recp));
1538 fc_fcp_srr(fsp, r_ctl, offset);
1544 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
1572 fc_fcp_srr(fsp, r_ctl, offset);
1576 fc_fcp_unlock_pkt(fsp);
1578 fc_fcp_pkt_release(fsp);
1589 int error = PTR_ERR(fp);
1591 if (fc_fcp_lock_pkt(fsp))
1596 fc_fcp_retry_cmd(fsp);
1600 FC_FCP_DBG(fsp,
"REC %p fid %6.6x error unexpected error %d\n",
1601 fsp, fsp->
rport->port_id, error);
1610 FC_FCP_DBG(fsp,
"REC fid %6.6x error error %d retry %d/%d\n",
1619 fc_fcp_unlock_pkt(fsp);
1621 fc_fcp_pkt_release(fsp);
1637 fc_fcp_send_abort(fsp);
1656 unsigned int rec_tov;
1664 fp = fc_fcp_frame_alloc(lport,
sizeof(*srr));
1668 srr = fc_frame_payload_get(fp,
sizeof(*srr));
1669 memset(srr, 0,
sizeof(*srr));
1680 rec_tov = get_fsp_rec_tov(fsp);
1681 seq = lport->
tt.exch_seq_send(lport, fp, fc_fcp_srr_resp,
1691 fc_fcp_pkt_hold(fsp);
1694 fc_fcp_retry_cmd(fsp);
1703 static void fc_fcp_srr_resp(
struct fc_seq *seq,
struct fc_frame *fp,
void *arg)
1709 fc_fcp_srr_error(fsp, fp);
1713 if (fc_fcp_lock_pkt(fsp))
1716 fh = fc_frame_header_get(fp);
1725 fc_fcp_unlock_pkt(fsp);
1729 switch (fc_frame_payload_op(fp)) {
1732 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
1739 fc_fcp_unlock_pkt(fsp);
1741 fsp->
lp->tt.exch_done(seq);
1752 if (fc_fcp_lock_pkt(fsp))
1754 switch (PTR_ERR(fp)) {
1764 fc_fcp_retry_cmd(fsp);
1767 fc_fcp_unlock_pkt(fsp);
1776 static inline int fc_fcp_lport_queue_ready(
struct fc_lport *lport)
1792 struct fc_lport *lport = shost_priv(shost);
1800 rval = fc_remote_port_chkready(rport);
1807 if (!*(
struct fc_remote_port **)rport->
dd_data) {
1819 if (!fc_fcp_lport_queue_ready(lport)) {
1821 fc_fcp_can_queue_ramp_down(lport);
1841 fsp->
data_len = scsi_bufflen(sc_cmd);
1867 rval = fc_fcp_pkt_send(lport, fsp);
1870 fc_fcp_pkt_release(fsp);
1885 static void fc_io_compl(
struct fc_fcp_pkt *fsp)
1890 unsigned long flags;
1910 fc_fcp_can_queue_ramp_up(lport);
1932 FC_FCP_DBG(fsp,
"Returning DID_ERROR to scsi-ml "
1933 "due to FC_ERROR\n");
1945 FC_FCP_DBG(fsp,
"Returning DID_ERROR to scsi-ml"
1946 " due to FC_DATA_UNDRUN (trans)\n");
1953 FC_FCP_DBG(fsp,
"Returning DID_ERROR to scsi-ml "
1954 "due to FC_DATA_UNDRUN (scsi)\n");
1963 FC_FCP_DBG(fsp,
"Returning DID_ERROR to scsi-ml "
1964 "due to FC_DATA_OVRRUN\n");
1968 FC_FCP_DBG(fsp,
"Returning DID_ERROR to scsi-ml "
1969 "due to FC_CMD_ABORTED\n");
1973 FC_FCP_DBG(fsp,
"Returning DID_RESET to scsi-ml "
1974 "due to FC_CMD_RESET\n");
1978 FC_FCP_DBG(fsp,
"Returning DID_NO_CONNECT to scsi-ml "
1979 "due to FC_HRD_ERROR\n");
1983 FC_FCP_DBG(fsp,
"Returning DID_PARITY to scsi-ml "
1984 "due to FC_CRC_ERROR\n");
1988 FC_FCP_DBG(fsp,
"Returning DID_BUS_BUSY to scsi-ml "
1989 "due to FC_TIMED_OUT\n");
1993 FC_FCP_DBG(fsp,
"Returning DID_ERROR to scsi-ml "
1994 "due to unknown error\n");
2009 fc_fcp_pkt_release(fsp);
2025 unsigned long flags;
2032 lport = shost_priv(sc_cmd->
device->host);
2047 fc_fcp_pkt_hold(fsp);
2050 if (fc_fcp_lock_pkt(fsp)) {
2056 rc = fc_fcp_pkt_abort(fsp);
2057 fc_fcp_unlock_pkt(fsp);
2060 fc_fcp_pkt_release(fsp);
2084 lport = shost_priv(sc_cmd->
device->host);
2091 fsp = fc_fcp_pkt_alloc(lport,
GFP_NOIO);
2107 rc = fc_lun_reset(lport, fsp,
scmd_id(sc_cmd), sc_cmd->
device->lun);
2109 fc_fcp_pkt_release(fsp);
2123 struct fc_lport *lport = shost_priv(shost);
2124 unsigned long wait_tmo;
2130 lport->
tt.lport_reset(lport);
2136 if (fc_fcp_lport_queue_ready(lport)) {
2138 "on port (%6.6x)\n", lport->
port_id);
2142 "port (%6.6x) is not ready.\n",
2160 if (!rport || fc_remote_port_chkready(rport))
2206 scsi_set_tag_type(sdev, tag_type);
2228 "port (%6.6x)\n", lport->
port_id);
2243 if (!scsi_pkt_cachep) {
2245 "module load failed!");
2254 if (scsi_pkt_cachep)
2267 if (!lport->
tt.fcp_cmd_send)
2268 lport->
tt.fcp_cmd_send = fc_fcp_cmd_send;
2270 if (!lport->
tt.fcp_cleanup)
2271 lport->
tt.fcp_cleanup = fc_fcp_cleanup;
2273 if (!lport->
tt.fcp_abort_io)
2274 lport->
tt.fcp_abort_io = fc_fcp_abort_io;
2284 si->
scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);