47 #include <linux/kernel.h>
50 #include <linux/slab.h>
54 #include <linux/export.h>
55 #include <asm/unaligned.h>
81 static const char *fc_rport_state_names[] = {
105 list_for_each_entry_rcu(rdata, &lport->
disc.rports,
peers)
106 if (rdata->
ids.port_id == port_id)
125 rdata = lport->tt.rport_lookup(lport, port_id);
129 rdata = kzalloc(
sizeof(*rdata) + lport->rport_priv_size,
GFP_KERNEL);
133 rdata->
ids.node_name = -1;
134 rdata->
ids.port_name = -1;
138 kref_init(&rdata->
kref);
144 rdata->
e_d_tov = lport->e_d_tov;
145 rdata->
r_a_tov = lport->r_a_tov;
151 list_add_rcu(&rdata->
peers, &lport->disc.rports);
160 static void fc_rport_destroy(
struct kref *
kref)
176 cp = fc_rport_state_names[rdata->
rp_state];
203 static unsigned int fc_plogi_get_maxframe(
struct fc_els_flogi *flp,
228 static void fc_rport_state_enter(
struct fc_rport_priv *rdata,
255 event = rdata->
event;
256 rport_ops = rdata->
ops;
257 rport = rdata->
rport;
266 kref_get(&rdata->
kref);
273 lport->
tt.rport_logoff(rdata);
274 kref_put(&rdata->
kref, lport->
tt.rport_destroy);
300 kref_put(&rdata->
kref, lport->
tt.rport_destroy);
310 if (prov && prov->
prlo)
315 port_id = rdata->
ids.port_id;
331 lport->
tt.exch_mgr_reset(lport, 0, port_id);
332 lport->
tt.exch_mgr_reset(lport, port_id, 0);
349 kref_put(&rdata->
kref, lport->
tt.rport_destroy);
356 fc_rport_enter_flogi(rdata);
360 list_del_rcu(&rdata->
peers);
362 kref_put(&rdata->
kref, lport->
tt.rport_destroy);
370 fc_rport_enter_ready(rdata);
402 fc_rport_enter_adisc(rdata);
409 fc_rport_enter_flogi(rdata);
431 static void fc_rport_enter_delete(
struct fc_rport_priv *rdata,
462 FC_RPORT_DBG(rdata,
"Port in Delete state, not removing\n");
465 fc_rport_enter_logo(rdata);
484 static void fc_rport_enter_ready(
struct fc_rport_priv *rdata)
503 static void fc_rport_timeout(
struct work_struct *work)
512 fc_rport_enter_flogi(rdata);
515 fc_rport_enter_plogi(rdata);
518 fc_rport_enter_prli(rdata);
521 fc_rport_enter_rtv(rdata);
524 fc_rport_enter_adisc(rdata);
546 FC_RPORT_DBG(rdata,
"Error %ld in state %s, retries %d\n",
547 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
557 fc_rport_enter_ready(rdata);
561 fc_rport_enter_logo(rdata);
582 static void fc_rport_error_retry(
struct fc_rport_priv *rdata,
592 FC_RPORT_DBG(rdata,
"Error %ld in state %s, retrying\n",
603 fc_rport_error(rdata, fp);
616 static int fc_rport_login_complete(
struct fc_rport_priv *rdata,
621 unsigned int e_d_tov;
624 flogi = fc_frame_payload_get(fp,
sizeof(*flogi));
630 if (fc_frame_payload_op(fp) ==
ELS_FLOGI) {
662 unsigned int r_a_tov;
672 FC_RPORT_DBG(rdata,
"Received a FLOGI response, but in state "
680 fc_rport_error(rdata, fp);
686 if (fc_rport_login_complete(rdata, fp))
689 flogi = fc_frame_payload_get(fp,
sizeof(*flogi));
696 if (rdata->
ids.port_name < lport->
wwpn)
697 fc_rport_enter_plogi(rdata);
709 fc_rport_error_retry(rdata, fp);
720 static void fc_rport_enter_flogi(
struct fc_rport_priv *rdata)
726 return fc_rport_enter_plogi(rdata);
728 FC_RPORT_DBG(rdata,
"Entered FLOGI state from %s state\n",
733 fp = fc_frame_alloc(lport,
sizeof(
struct fc_els_flogi));
735 return fc_rport_error_retry(rdata, fp);
737 if (!lport->
tt.elsct_send(lport, rdata->
ids.port_id, fp,
ELS_FLOGI,
738 fc_rport_flogi_resp, rdata,
740 fc_rport_error_retry(rdata, NULL);
742 kref_get(&rdata->
kref);
750 static void fc_rport_recv_flogi_req(
struct fc_lport *lport,
760 sid = fc_frame_sid(fp);
773 flp = fc_frame_payload_get(fp,
sizeof(*flp));
780 rdata = lport->
tt.rport_lookup(lport, sid);
830 if (fc_rport_login_complete(rdata, fp)) {
837 fp = fc_frame_alloc(lport,
sizeof(*flp));
841 fc_flogi_fill(lport, fp);
842 flp = fc_frame_payload_get(fp,
sizeof(*flp));
846 lport->
tt.frame_send(lport, fp);
848 if (rdata->
ids.port_name < lport->
wwpn)
849 fc_rport_enter_plogi(rdata);
855 fc_frame_free(rx_fp);
860 lport->
tt.seq_els_rsp_send(rx_fp,
ELS_LS_RJT, &rjt_data);
861 fc_frame_free(rx_fp);
874 static void fc_rport_plogi_resp(
struct fc_seq *sp,
struct fc_frame *fp,
889 FC_RPORT_DBG(rdata,
"Received a PLOGI response, but in state "
897 fc_rport_error_retry(rdata, fp);
901 op = fc_frame_payload_op(fp);
903 (plp = fc_frame_payload_get(fp,
sizeof(*plp))) != NULL) {
911 fc_rport_login_complete(rdata, fp);
914 if (cssp_seq < csp_seq)
918 fc_rport_enter_prli(rdata);
920 fc_rport_error_retry(rdata, fp);
936 static void fc_rport_enter_plogi(
struct fc_rport_priv *rdata)
941 FC_RPORT_DBG(rdata,
"Port entered PLOGI state from %s state\n",
947 fp = fc_frame_alloc(lport,
sizeof(
struct fc_els_flogi));
949 FC_RPORT_DBG(rdata,
"%s frame alloc failed\n", __func__);
950 fc_rport_error_retry(rdata, fp);
955 if (!lport->
tt.elsct_send(lport, rdata->
ids.port_id, fp,
ELS_PLOGI,
956 fc_rport_plogi_resp, rdata,
958 fc_rport_error_retry(rdata, NULL);
960 kref_get(&rdata->
kref);
973 static void fc_rport_prli_resp(
struct fc_seq *sp,
struct fc_frame *fp,
993 FC_RPORT_DBG(rdata,
"Received a PRLI response, but in state "
1001 fc_rport_error_retry(rdata, fp);
1008 op = fc_frame_payload_op(fp);
1010 pp = fc_frame_payload_get(fp,
sizeof(*
pp));
1020 fc_rport_error(rdata, fp);
1022 fc_rport_error_retry(rdata, fp);
1025 if (
pp->prli.prli_spp_len <
sizeof(
pp->spp))
1028 fcp_parm =
ntohl(
pp->spp.spp_params);
1036 memset(&temp_spp, 0,
sizeof(temp_spp));
1037 prov->
prli(rdata,
pp->prli.prli_spp_len,
1038 &
pp->spp, &temp_spp);
1048 fc_rport_enter_rtv(rdata);
1051 FC_RPORT_DBG(rdata,
"Bad ELS response for PRLI command\n");
1052 fc_rport_error_retry(rdata, fp);
1069 static void fc_rport_enter_prli(
struct fc_rport_priv *rdata)
1084 fc_rport_enter_ready(rdata);
1088 FC_RPORT_DBG(rdata,
"Port entered PRLI state from %s state\n",
1093 fp = fc_frame_alloc(lport,
sizeof(*
pp));
1095 fc_rport_error_retry(rdata, fp);
1099 fc_prli_fill(lport, fp);
1103 pp = fc_frame_payload_get(fp,
sizeof(*
pp));
1104 prov->
prli(rdata,
sizeof(
pp->spp), NULL, &
pp->spp);
1111 if (!lport->
tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
1112 NULL, rdata, 2 * lport->
r_a_tov))
1113 fc_rport_error_retry(rdata, NULL);
1115 kref_get(&rdata->
kref);
1130 static void fc_rport_rtv_resp(
struct fc_seq *sp,
struct fc_frame *fp,
1141 FC_RPORT_DBG(rdata,
"Received a RTV response, but in state "
1149 fc_rport_error(rdata, fp);
1153 op = fc_frame_payload_op(fp);
1159 rtv = fc_frame_payload_get(fp,
sizeof(*rtv));
1175 fc_rport_enter_ready(rdata);
1196 FC_RPORT_DBG(rdata,
"Port entered RTV state from %s state\n",
1201 fp = fc_frame_alloc(lport,
sizeof(
struct fc_els_rtv));
1203 fc_rport_error_retry(rdata, fp);
1207 if (!lport->
tt.elsct_send(lport, rdata->
ids.port_id, fp,
ELS_RTV,
1208 fc_rport_rtv_resp, rdata,
1210 fc_rport_error_retry(rdata, NULL);
1212 kref_get(&rdata->
kref);
1221 static void fc_rport_logo_resp(
struct fc_seq *sp,
struct fc_frame *fp,
1224 struct fc_lport *lport = lport_arg;
1240 static void fc_rport_enter_logo(
struct fc_rport_priv *rdata)
1245 FC_RPORT_DBG(rdata,
"Port sending LOGO from %s state\n",
1248 fp = fc_frame_alloc(lport,
sizeof(
struct fc_els_logo));
1252 fc_rport_logo_resp, lport, 0);
1265 static void fc_rport_adisc_resp(
struct fc_seq *sp,
struct fc_frame *fp,
1277 FC_RPORT_DBG(rdata,
"Received a ADISC resp but in state %s\n",
1285 fc_rport_error(rdata, fp);
1294 op = fc_frame_payload_op(fp);
1295 adisc = fc_frame_payload_get(fp,
sizeof(*adisc));
1301 fc_rport_enter_flogi(rdata);
1304 fc_rport_enter_ready(rdata);
1320 static void fc_rport_enter_adisc(
struct fc_rport_priv *rdata)
1330 fp = fc_frame_alloc(lport,
sizeof(
struct fc_els_adisc));
1332 fc_rport_error_retry(rdata, fp);
1335 if (!lport->
tt.elsct_send(lport, rdata->
ids.port_id, fp,
ELS_ADISC,
1336 fc_rport_adisc_resp, rdata,
1338 fc_rport_error_retry(rdata, NULL);
1340 kref_get(&rdata->
kref);
1350 static void fc_rport_recv_adisc_req(
struct fc_rport_priv *rdata,
1360 adisc = fc_frame_payload_get(in_fp,
sizeof(*adisc));
1364 lport->
tt.seq_els_rsp_send(in_fp,
ELS_LS_RJT, &rjt_data);
1368 fp = fc_frame_alloc(lport,
sizeof(*adisc));
1371 fc_adisc_fill(lport, fp);
1372 adisc = fc_frame_payload_get(fp,
sizeof(*adisc));
1375 lport->
tt.frame_send(lport, fp);
1377 fc_frame_free(in_fp);
1388 static void fc_rport_recv_rls_req(
struct fc_rport_priv *rdata,
1400 FC_RPORT_DBG(rdata,
"Received RLS request while in state %s\n",
1403 rls = fc_frame_payload_get(rx_fp,
sizeof(*rls));
1410 fp = fc_frame_alloc(lport,
sizeof(*rsp));
1417 rsp = fc_frame_payload_get(fp,
sizeof(*rsp));
1418 memset(rsp, 0,
sizeof(*rsp));
1421 if (lport->
tt.get_lesb) {
1423 lport->
tt.get_lesb(lport, lesb);
1436 lport->
tt.frame_send(lport, fp);
1440 lport->
tt.seq_els_rsp_send(rx_fp,
ELS_LS_RJT, &rjt_data);
1442 fc_frame_free(rx_fp);
1455 static void fc_rport_recv_els_req(
struct fc_lport *lport,
struct fc_frame *fp)
1461 rdata = lport->
tt.rport_lookup(lport, fc_frame_sid(fp));
1480 switch (fc_frame_payload_op(fp)) {
1482 fc_rport_recv_prli_req(rdata, fp);
1485 fc_rport_recv_prlo_req(rdata, fp);
1488 fc_rport_recv_adisc_req(rdata, fp);
1491 lport->
tt.seq_els_rsp_send(fp,
ELS_RRQ, NULL);
1495 lport->
tt.seq_els_rsp_send(fp,
ELS_REC, NULL);
1499 fc_rport_recv_rls_req(rdata, fp);
1533 switch (fc_frame_payload_op(fp)) {
1535 fc_rport_recv_flogi_req(lport, fp);
1538 fc_rport_recv_plogi_req(lport, fp);
1541 fc_rport_recv_logo_req(lport, fp);
1549 fc_rport_recv_els_req(lport, fp);
1567 static void fc_rport_recv_plogi_req(
struct fc_lport *lport,
1577 sid = fc_frame_sid(fp);
1581 pl = fc_frame_payload_get(fp,
sizeof(*pl));
1589 disc = &lport->
disc;
1591 rdata = lport->
tt.rport_create(lport, sid);
1621 FC_RPORT_DBG(rdata,
"Received PLOGI in PLOGI_WAIT state\n");
1624 FC_RPORT_DBG(rdata,
"Received PLOGI in PLOGI state\n");
1625 if (rdata->
ids.port_name < lport->
wwpn) {
1636 FC_RPORT_DBG(rdata,
"Received PLOGI in logged-in state %d "
1637 "- ignored for now\n", rdata->
rp_state);
1642 FC_RPORT_DBG(rdata,
"Received PLOGI in state %s - send busy\n",
1658 fp = fc_frame_alloc(lport,
sizeof(*pl));
1664 lport->
tt.frame_send(lport, fp);
1665 fc_rport_enter_prli(rdata);
1668 fc_frame_free(rx_fp);
1684 static void fc_rport_recv_prli_req(
struct fc_rport_priv *rdata,
1702 FC_RPORT_DBG(rdata,
"Received PRLI request while in state %s\n",
1706 pp = fc_frame_payload_get(rx_fp,
sizeof(*
pp));
1709 plen =
ntohs(
pp->prli.prli_len);
1710 if ((plen % 4) != 0 || plen > len || plen < 16)
1714 plen =
pp->prli.prli_spp_len;
1715 if ((plen % 4) != 0 || plen <
sizeof(*spp) ||
1716 plen > len || len <
sizeof(*
pp) || plen < 12)
1720 fp = fc_frame_alloc(lport, len);
1726 pp = fc_frame_payload_get(fp, len);
1730 pp->prli.prli_spp_len =
plen;
1731 pp->prli.prli_len =
htons(len);
1741 while (len >= plen) {
1747 if (rspp->
spp_type < FC_FC4_PROV_SIZE) {
1750 resp = prov->
prli(rdata, plen, rspp, spp);
1753 passive = prov->
prli(rdata, plen, rspp, spp);
1775 lport->
tt.frame_send(lport, fp);
1779 fc_rport_enter_ready(rdata);
1790 lport->
tt.seq_els_rsp_send(rx_fp,
ELS_LS_RJT, &rjt_data);
1792 fc_frame_free(rx_fp);
1803 static void fc_rport_recv_prlo_req(
struct fc_rport_priv *rdata,
1818 FC_RPORT_DBG(rdata,
"Received PRLO request while in state %s\n",
1822 pp = fc_frame_payload_get(rx_fp,
sizeof(*
pp));
1825 plen =
ntohs(
pp->prlo.prlo_len);
1833 fp = fc_frame_alloc(lport, len);
1840 pp = fc_frame_payload_get(fp, len);
1844 pp->prlo.prlo_obs = 0x10;
1845 pp->prlo.prlo_len =
htons(len);
1854 lport->
tt.frame_send(lport, fp);
1861 lport->
tt.seq_els_rsp_send(rx_fp,
ELS_LS_RJT, &rjt_data);
1863 fc_frame_free(rx_fp);
1874 static void fc_rport_recv_logo_req(
struct fc_lport *lport,
struct fc_frame *fp)
1881 sid = fc_frame_sid(fp);
1884 rdata = lport->
tt.rport_lookup(lport, sid);
1887 FC_RPORT_DBG(rdata,
"Received LOGO request while in state %s\n",
1894 "Received LOGO from non-logged-in port\n");
1902 static void fc_rport_flush_queue(
void)
1913 if (!lport->
tt.rport_lookup)
1914 lport->
tt.rport_lookup = fc_rport_lookup;
1916 if (!lport->
tt.rport_create)
1917 lport->
tt.rport_create = fc_rport_create;
1919 if (!lport->
tt.rport_login)
1920 lport->
tt.rport_login = fc_rport_login;
1922 if (!lport->
tt.rport_logoff)
1923 lport->
tt.rport_logoff = fc_rport_logoff;
1925 if (!lport->
tt.rport_recv_req)
1926 lport->
tt.rport_recv_req = fc_rport_recv_req;
1928 if (!lport->
tt.rport_flush_queue)
1929 lport->
tt.rport_flush_queue = fc_rport_flush_queue;
1931 if (!lport->
tt.rport_destroy)
1932 lport->
tt.rport_destroy = fc_rport_destroy;
1961 if (fcp_parm & FCP_SPPF_RETRY)
1982 .prli = fc_rport_fcp_prli,
2008 .prli = fc_rport_t0_prli,
2017 if (!rport_event_queue)
2039 lport->
tt.exch_mgr_reset(lport, 0, rport->
port_id);
2040 lport->
tt.exch_mgr_reset(lport, rport->
port_id, 0);