21 #include <linux/types.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
27 #include <linux/netdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/errno.h>
33 #include <linux/bitops.h>
34 #include <linux/slab.h>
49 #define FCOE_CTLR_MIN_FKA 500
50 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA
52 static void fcoe_ctlr_timeout(
unsigned long);
53 static void fcoe_ctlr_timer_work(
struct work_struct *);
54 static void fcoe_ctlr_recv_work(
struct work_struct *);
55 static int fcoe_ctlr_flogi_retry(
struct fcoe_ctlr *);
57 static void fcoe_ctlr_vn_start(
struct fcoe_ctlr *);
59 static void fcoe_ctlr_vn_timeout(
struct fcoe_ctlr *);
67 static const char *
const fcoe_ctlr_states[] = {
82 const char *
cp =
"unknown";
85 cp = fcoe_ctlr_states[
state];
98 if (state == fip->
state)
102 fcoe_ctlr_state(fip->
state), fcoe_ctlr_state(state));
112 static inline int fcoe_ctlr_mtu_valid(
const struct fcoe_fcf *fcf)
123 static inline int fcoe_ctlr_fcf_usable(
struct fcoe_fcf *fcf)
134 static void fcoe_ctlr_map_dest(
struct fcoe_ctlr *fip)
152 INIT_LIST_HEAD(&fip->
fcfs);
157 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
158 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
159 skb_queue_head_init(&fip->fip_recv_list);
163 static int fcoe_sysfs_fcf_add(
struct fcoe_fcf *
new)
171 new->fabric_name, new->fcf_mac);
175 temp.fabric_name =
new->fabric_name;
176 temp.switch_name =
new->switch_name;
177 temp.fc_map =
new->fc_map;
178 temp.vfid =
new->vfid;
180 temp.priority =
new->pri;
181 temp.fka_period =
new->fka_period;
203 new->fcf_dev = fcf_dev;
205 list_add(&new->list, &fip->
fcfs);
213 static void fcoe_sysfs_fcf_del(
struct fcoe_fcf *
new)
239 static void fcoe_ctlr_reset_fcfs(
struct fcoe_ctlr *fip)
246 fcoe_sysfs_fcf_del(fcf);
272 fcoe_ctlr_reset_fcfs(fip);
287 static void fcoe_ctlr_announce(
struct fcoe_ctlr *fip)
300 spin_unlock_bh(&fip->ctlr_lock);
307 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
313 "Fibre-Channel Forwarder MAC %pM\n",
329 static inline u32 fcoe_ctlr_fcoe_size(
struct fcoe_ctlr *fip)
359 skb = dev_alloc_skb(
sizeof(*sol));
363 sol = (
struct fip_sol *)skb->
data;
365 memset(sol, 0,
sizeof(*sol));
373 sol->fip.fip_dl_len =
htons(
sizeof(sol->desc) /
FIP_BPW);
379 sol->desc.mac.fd_desc.fip_dlen =
sizeof(sol->desc.mac) /
FIP_BPW;
383 sol->desc.wwnn.fd_desc.fip_dlen =
sizeof(sol->desc.wwnn) /
FIP_BPW;
386 fcoe_size = fcoe_ctlr_fcoe_size(fip);
388 sol->desc.size.fd_desc.fip_dlen =
sizeof(sol->desc.size) /
FIP_BPW;
389 sol->desc.size.fd_size =
htons(fcoe_size);
394 skb_reset_mac_header(skb);
395 skb_reset_network_header(skb);
415 fcoe_ctlr_set_state(fip, fip->
mode);
427 fcoe_ctlr_solicit(fip,
NULL);
430 fcoe_ctlr_vn_start(fip);
444 static void fcoe_ctlr_reset(
struct fcoe_ctlr *fip)
446 fcoe_ctlr_reset_fcfs(fip);
452 fcoe_ctlr_map_dest(fip);
470 fcoe_ctlr_reset(fip);
495 static void fcoe_ctlr_send_keep_alive(
struct fcoe_ctlr *fip,
512 if (!fcf || (ports && !lp->
port_id))
515 len =
sizeof(*kal) + ports *
sizeof(*vn);
516 skb = dev_alloc_skb(len);
520 kal = (
struct fip_kal *)skb->
data;
529 kal->fip.fip_dl_len =
htons((
sizeof(kal->mac) +
530 ports *
sizeof(*vn)) /
FIP_BPW);
536 kal->mac.fd_desc.fip_dlen =
sizeof(kal->mac) /
FIP_BPW;
541 vn->fd_desc.fip_dlen =
sizeof(*vn) /
FIP_BPW;
549 skb_reset_mac_header(skb);
550 skb_reset_network_header(skb);
572 struct fip_encaps_head {
585 op = *(
u8 *)(fh + 1);
587 cap = (
struct fip_encaps_head *)
skb_push(skb,
sizeof(*cap));
588 memset(cap, 0,
sizeof(*cap));
591 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
598 fip_flags = fcf->
flags;
614 cap->fip.fip_flags =
htons(fip_flags);
616 cap->encaps.fd_desc.fip_dtype =
dtype;
617 cap->encaps.fd_desc.fip_dlen = dlen /
FIP_BPW;
620 dlen +=
sizeof(*mac);
622 memset(mac, 0,
sizeof(*mac));
642 skb_reset_mac_header(skb);
643 skb_reset_network_header(skb);
674 op = *(
u8 *)(fh + 1);
685 fcoe_ctlr_map_dest(fip);
689 fcoe_ctlr_map_dest(fip);
739 fc_fcoe_set_mac(mac, fh->
fh_d_id);
756 if (fcoe_ctlr_encaps(fip, lport, op, skb,
ntoh24(fh->
fh_d_id)))
783 static unsigned long fcoe_ctlr_age_fcfs(
struct fcoe_ctlr *fip)
788 unsigned long deadline;
789 unsigned long sel_time = 0;
793 INIT_LIST_HEAD(&del_list);
803 "Missing Discovery Advertisement "
804 "for fab %16.16llx count %lld\n",
808 next_timer = deadline;
821 list_add(&fcf->
list, &del_list);
825 next_timer = deadline;
826 if (fcoe_ctlr_mtu_valid(fcf) &&
828 sel_time = fcf->
time;
835 fcoe_sysfs_fcf_del(fcf);
855 static int fcoe_ctlr_parse_adv(
struct fcoe_ctlr *fip,
868 memset(fcf, 0,
sizeof(*fcf));
881 if (rlen +
sizeof(*fiph) > skb->
len)
884 desc = (
struct fip_desc *)(fiph + 1);
887 if (dlen <
sizeof(*desc) || dlen > rlen)
893 "Descriptors in FIP adv\n");
910 if (!is_valid_ether_addr(fcf->
fcf_mac)) {
912 "Invalid MAC addr %pM in FIP adv\n",
974 LIBFCOE_FIP_DBG(fip,
"FIP length error in descriptor type %x len %zu\n",
994 if (fcoe_ctlr_parse_adv(fip, skb, &
new))
998 first = list_empty(&fip->
fcfs);
1002 fcf->
fc_map ==
new.fc_map &&
1003 compare_ether_addr(fcf->
fcf_mac,
new.fcf_mac) == 0) {
1016 memcpy(fcf, &
new,
sizeof(
new));
1018 rc = fcoe_sysfs_fcf_add(fcf);
1021 "for FCF, fab %16.16llx mac %pM\n",
1034 if (!fcoe_ctlr_fcf_usable(fcf))
1035 fcf->
flags =
new.flags;
1047 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1058 fcoe_ctlr_solicit(fip, fcf);
1067 fcoe_ctlr_solicit(fip,
NULL);
1082 if (mtu_valid && !fip->
sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
1085 if (!timer_pending(&fip->
timer) ||
1125 if (rlen +
sizeof(*fiph) > skb->
len)
1128 desc = (
struct fip_desc *)(fiph + 1);
1132 if (dlen <
sizeof(*desc) || dlen > rlen)
1139 "Descriptors in FIP ELS\n");
1147 if (desc_cnt == 1) {
1149 "received out of order\n");
1168 if ((desc_cnt == 3) && (sel))
1177 if (desc_cnt != 1) {
1179 "received out of order\n");
1184 if (dlen <
sizeof(*els) +
sizeof(*fh) + 1)
1186 els_len = dlen -
sizeof(*els);
1197 if (desc_cnt <= 2) {
1199 "received out of order\n");
1210 els_op = *(
u8 *)(fh + 1);
1215 if (!is_valid_ether_addr(granted_mac)) {
1217 "Invalid MAC address %pM in FIP ELS\n",
1226 fcoe_ctlr_announce(fip);
1229 !fcoe_ctlr_flogi_retry(fip))
1233 if ((desc_cnt == 0) || ((els_op !=
ELS_LS_RJT) &&
1261 LIBFCOE_FIP_DBG(fip,
"FIP length error in descriptor type %x len %zu\n",
1275 static void fcoe_ctlr_recv_clr_vlink(
struct fcoe_ctlr *fip,
1289 int reset_phys_port = 0;
1303 desc = (
struct fip_desc *)(fh + 1);
1310 num_vlink_desc = rlen /
sizeof(*vp);
1312 vlink_desc_arr =
kmalloc(
sizeof(vp) * num_vlink_desc,
1314 if (!vlink_desc_arr)
1318 while (rlen >=
sizeof(*desc)) {
1327 "Descriptors in FIP CVL\n");
1333 if (dlen <
sizeof(*mp))
1341 if (dlen <
sizeof(*wp))
1349 if (dlen <
sizeof(*vp))
1351 vlink_desc_arr[num_vlink_desc++] = vp;
1354 if (vn_port && (vn_port == lport)) {
1357 get_cpu())->VLinkFailureCount++;
1359 fcoe_ctlr_reset(fip);
1379 else if (!num_vlink_desc) {
1388 fcoe_ctlr_reset(fip);
1397 fcoe_ctlr_solicit(fip,
NULL);
1402 for (i = 0; i < num_vlink_desc; i++) {
1403 vp = vlink_desc_arr[
i];
1413 if (compare_ether_addr(fip->get_src_addr(vn_port),
1419 if (vn_port == lport)
1424 reset_phys_port = 1;
1429 if (reset_phys_port) {
1431 fcoe_ctlr_solicit(fip, NULL);
1436 kfree(vlink_desc_arr);
1460 static int fcoe_ctlr_recv_handler(
struct fcoe_ctlr *fip,
struct sk_buff *skb)
1468 if (skb_linearize(skb))
1470 if (skb->
len <
sizeof(*fiph))
1475 compare_ether_addr(eh->
h_dest, fcoe_all_vn2vn) &&
1476 compare_ether_addr(eh->
h_dest, fcoe_all_p2p))
1479 compare_ether_addr(eh->
h_dest, fcoe_all_enode))
1501 return fcoe_ctlr_vn_recv(fip, skb);
1508 fcoe_ctlr_recv_els(fip, skb);
1516 fcoe_ctlr_recv_adv(fip, skb);
1518 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1549 "VFID %d mac %pM map %x val %d "
1552 fcf->
fc_map, fcoe_ctlr_mtu_valid(fcf),
1563 if (!fcoe_ctlr_fcf_usable(fcf)) {
1565 "map %x %svalid %savailable\n",
1596 static int fcoe_ctlr_flogi_send_locked(
struct fcoe_ctlr *fip)
1622 fip->
send(fip, skb);
1634 static int fcoe_ctlr_flogi_retry(
struct fcoe_ctlr *fip)
1642 fcf = fcoe_ctlr_select(fip);
1648 fcoe_ctlr_solicit(fip, NULL);
1649 error = fcoe_ctlr_flogi_send_locked(fip);
1665 static void fcoe_ctlr_flogi_send(
struct fcoe_ctlr *fip)
1682 fcf = fcoe_ctlr_select(fip);
1687 fcf = fcoe_ctlr_select(fip);
1691 fcoe_ctlr_flogi_send_locked(fip);
1692 fip->flogi_req_send = 0;
1703 static void fcoe_ctlr_timeout(
unsigned long arg)
1723 u8 send_ctlr_ka = 0;
1724 u8 send_port_ka = 0;
1727 unsigned long next_timer;
1731 return fcoe_ctlr_vn_timeout(fip);
1739 next_timer = fcoe_ctlr_age_fcfs(fip);
1744 sel = fcoe_ctlr_select(fip);
1751 fcoe_ctlr_flogi_send(fip);
1752 else if (!sel && fcf)
1771 if (!list_empty(&fip->
fcfs))
1778 fcoe_ctlr_solicit(fip, NULL);
1782 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->
ctl_src_addr);
1787 fcoe_ctlr_send_keep_alive(fip, fip->
lp, 1, mac);
1790 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1800 static void fcoe_ctlr_recv_work(
struct work_struct *recv_work)
1807 fcoe_ctlr_recv_handler(fip, skb);
1832 sa = eth_hdr(&fp->
skb)->h_source;
1833 fh = fc_frame_header_get(fp);
1837 op = fc_frame_payload_op(fp);
1848 "received FLOGI LS_ACC using non-FIP mode\n");
1857 fcoe_ctlr_map_dest(fip);
1875 "Setting non-FIP mode\n");
1893 unsigned int scheme,
unsigned int port)
1899 host_mac = ((
u64) mac[0] << 40) |
1900 ((
u64) mac[1] << 32) |
1901 ((
u64) mac[2] << 24) |
1902 ((
u64) mac[3] << 16) |
1903 ((
u64) mac[4] << 8) |
1906 WARN_ON(host_mac >= (1ULL << 48));
1907 wwn = host_mac | ((
u64) scheme << 60);
1914 wwn |= (
u64) port << 48;
1941 static void fcoe_ctlr_vn_send(
struct fcoe_ctlr *fip,
1943 const u8 *
dest,
size_t min_len)
1959 len =
sizeof(*frame);
1966 dlen +=
sizeof(frame->mac) +
sizeof(frame->wwnn) +
sizeof(frame->vn);
1967 len =
max(len, min_len +
sizeof(
struct ethhdr));
1969 skb = dev_alloc_skb(len);
1973 frame = (
struct fip_frame *)skb->
data;
1987 frame->fip.fip_subcode = sub;
1991 frame->mac.fd_desc.fip_dlen =
sizeof(frame->mac) /
FIP_BPW;
1995 frame->wwnn.fd_desc.fip_dlen =
sizeof(frame->wwnn) /
FIP_BPW;
1999 frame->vn.fd_desc.fip_dlen =
sizeof(frame->vn) /
FIP_BPW;
2032 skb_reset_mac_header(skb);
2033 skb_reset_network_header(skb);
2035 fip->
send(fip, skb);
2046 static void fcoe_ctlr_vn_rport_callback(
struct fc_lport *lport,
2051 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2054 rdata->
ids.port_id, event);
2067 "rport FLOGI limited port_id %6.6x\n",
2068 rdata->
ids.port_id);
2069 lport->
tt.rport_logoff(rdata);
2079 .event_callback = fcoe_ctlr_vn_rport_callback,
2088 static void fcoe_ctlr_disc_stop_locked(
struct fc_lport *lport)
2102 static void fcoe_ctlr_disc_stop(
struct fc_lport *lport)
2107 fcoe_ctlr_disc_stop_locked(lport);
2118 static void fcoe_ctlr_disc_stop_final(
struct fc_lport *lport)
2120 fcoe_ctlr_disc_stop(lport);
2121 lport->
tt.rport_flush_queue();
2131 static void fcoe_ctlr_vn_restart(
struct fcoe_ctlr *fip)
2136 fcoe_ctlr_disc_stop_locked(fip->
lp);
2149 port_id = fip->
lp->wwpn & 0xffff;
2150 if (!port_id || port_id == 0xffff)
2169 static void fcoe_ctlr_vn_start(
struct fcoe_ctlr *fip)
2173 fcoe_ctlr_vn_restart(fip);
2185 static int fcoe_ctlr_vn_parse(
struct fcoe_ctlr *fip,
2202 memset(rdata, 0,
sizeof(*rdata) +
sizeof(*frport));
2203 frport = fcoe_ctlr_rport(rdata);
2228 if (rlen +
sizeof(*fiph) > skb->
len)
2231 desc = (
struct fip_desc *)(fiph + 1);
2234 if (dlen <
sizeof(*desc) || dlen > rlen)
2239 if (!(desc_mask &
BIT(dtype))) {
2241 "unexpected or duplicated desc "
2243 "FIP VN2VN subtype %u\n",
2247 desc_mask &= ~
BIT(dtype);
2255 if (!is_valid_ether_addr(macd->
fd_mac)) {
2257 "Invalid MAC addr %pM in FIP VN2VN\n",
2289 "in FIP probe\n", dtype);
2301 LIBFCOE_FIP_DBG(fip,
"FIP length error in descriptor type %x len %zu\n",
2312 static void fcoe_ctlr_vn_send_claim(
struct fcoe_ctlr *fip)
2325 static void fcoe_ctlr_vn_probe_req(
struct fcoe_ctlr *fip,
2328 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2333 switch (fip->
state) {
2348 if (fip->
lp->wwpn > rdata->
ids.port_name &&
2356 fcoe_ctlr_vn_restart(fip);
2370 static void fcoe_ctlr_vn_probe_reply(
struct fcoe_ctlr *fip,
2375 switch (fip->
state) {
2380 fcoe_ctlr_vn_restart(fip);
2383 fcoe_ctlr_vn_send_claim(fip);
2405 port_id =
new->ids.port_id;
2410 rdata = lport->
tt.rport_create(lport, port_id);
2416 rdata->
ops = &fcoe_ctlr_vn_rport_ops;
2422 lport->
tt.rport_logoff(rdata);
2427 frport = fcoe_ctlr_rport(rdata);
2429 port_id, frport->
fcoe_len ?
"old" :
"new");
2430 *frport = *fcoe_ctlr_rport(
new);
2442 static int fcoe_ctlr_vn_lookup(
struct fcoe_ctlr *fip,
u32 port_id,
u8 *mac)
2450 rdata = lport->
tt.rport_lookup(lport, port_id);
2452 frport = fcoe_ctlr_rport(rdata);
2467 static void fcoe_ctlr_vn_claim_notify(
struct fcoe_ctlr *fip,
2470 struct fcoe_rport *frport = fcoe_ctlr_rport(
new);
2476 switch (fip->
state) {
2480 if (new->ids.port_id == fip->
port_id)
2481 fcoe_ctlr_vn_restart(fip);
2485 if (new->ids.port_id == fip->
port_id) {
2486 if (new->ids.port_name > fip->
lp->wwpn) {
2487 fcoe_ctlr_vn_restart(fip);
2490 fcoe_ctlr_vn_send_claim(fip);
2495 fcoe_ctlr_fcoe_size(fip)));
2496 fcoe_ctlr_vn_add(fip,
new);
2510 static void fcoe_ctlr_vn_claim_resp(
struct fcoe_ctlr *fip,
2516 fcoe_ctlr_vn_add(fip,
new);
2526 static void fcoe_ctlr_vn_beacon(
struct fcoe_ctlr *fip,
2533 frport = fcoe_ctlr_rport(
new);
2539 rdata = lport->
tt.rport_lookup(lport, new->ids.
port_id);
2541 kref_get(&rdata->
kref);
2544 if (rdata->
ids.node_name == new->ids.node_name &&
2545 rdata->
ids.port_name == new->ids.port_name) {
2546 frport = fcoe_ctlr_rport(rdata);
2548 lport->
tt.rport_login(rdata);
2551 kref_put(&rdata->
kref, lport->
tt.rport_destroy);
2562 LIBFCOE_FIP_DBG(fip,
"beacon from new rport %x. sending claim notify\n",
2566 fcoe_ctlr_vn_send_claim(fip);
2577 static unsigned long fcoe_ctlr_vn_age(
struct fcoe_ctlr *fip)
2582 unsigned long next_time;
2583 unsigned long deadline;
2587 list_for_each_entry_rcu(rdata, &lport->
disc.rports,
peers) {
2588 frport = fcoe_ctlr_rport(rdata);
2591 deadline = frport->
time +
2596 "port %16.16llx fc_id %6.6x beacon expired\n",
2597 rdata->
ids.port_name, rdata->
ids.port_id);
2598 lport->
tt.rport_logoff(rdata);
2600 next_time = deadline;
2627 rc = fcoe_ctlr_vn_parse(fip, skb, &
buf.rdata);
2636 fcoe_ctlr_vn_probe_req(fip, &
buf.rdata);
2639 fcoe_ctlr_vn_probe_reply(fip, &
buf.rdata);
2642 fcoe_ctlr_vn_claim_notify(fip, &
buf.rdata);
2645 fcoe_ctlr_vn_claim_resp(fip, &
buf.rdata);
2648 fcoe_ctlr_vn_beacon(fip, &
buf.rdata);
2669 static void fcoe_ctlr_disc_recv(
struct fc_lport *lport,
struct fc_frame *fp)
2714 static void fcoe_ctlr_vn_disc(
struct fcoe_ctlr *fip)
2725 list_for_each_entry_rcu(rdata, &disc->
rports,
peers) {
2726 frport = fcoe_ctlr_rport(rdata);
2728 lport->
tt.rport_login(rdata);
2739 static void fcoe_ctlr_vn_timeout(
struct fcoe_ctlr *fip)
2741 unsigned long next_time;
2743 u32 new_port_id = 0;
2746 switch (fip->
state) {
2761 hton24(mac + 3, new_port_id);
2762 fcoe_ctlr_map_dest(fip);
2764 fcoe_ctlr_vn_send_claim(fip);
2780 fcoe_ctlr_vn_disc(fip);
2783 next_time = fcoe_ctlr_vn_age(fip);
2797 WARN(1,
"unexpected state %d\n", fip->
state);
2822 memcpy(&lport->
tt, tt,
sizeof(*tt));
2833 lport->
tt.disc_recv_req = fcoe_ctlr_disc_recv;
2834 lport->
tt.disc_start = fcoe_ctlr_disc_start;
2835 lport->
tt.disc_stop = fcoe_ctlr_disc_stop;
2836 lport->
tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2838 INIT_LIST_HEAD(&lport->
disc.rports);
2850 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
2869 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2872 switch (ctlr->
mode) {