1 // SPDX-License-Identifier: GPL-2.0-only
3 * QLogic FCoE Offload Driver
4 * Copyright (c) 2016-2018 Cavium Inc.
6 #include <linux/if_ether.h>
7 #include <linux/if_vlan.h>
10 extern const struct qed_fcoe_ops
*qed_ops
;
12 * FIP VLAN functions that will eventually move to libfcoe.
15 void qedf_fcoe_send_vlan_req(struct qedf_ctx
*qedf
)
19 struct fip_vlan
*vlan
;
20 #define MY_FIP_ALL_FCF_MACS ((__u8[6]) { 1, 0x10, 0x18, 1, 0, 2 })
21 static u8 my_fcoe_all_fcfs
[ETH_ALEN
] = MY_FIP_ALL_FCF_MACS
;
22 unsigned long flags
= 0;
25 skb
= dev_alloc_skb(sizeof(struct fip_vlan
));
27 QEDF_ERR(&qedf
->dbg_ctx
,
28 "Failed to allocate skb.\n");
32 eth_fr
= (char *)skb
->data
;
33 vlan
= (struct fip_vlan
*)eth_fr
;
35 memset(vlan
, 0, sizeof(*vlan
));
36 ether_addr_copy(vlan
->eth
.h_source
, qedf
->mac
);
37 ether_addr_copy(vlan
->eth
.h_dest
, my_fcoe_all_fcfs
);
38 vlan
->eth
.h_proto
= htons(ETH_P_FIP
);
40 vlan
->fip
.fip_ver
= FIP_VER_ENCAPS(FIP_VER
);
41 vlan
->fip
.fip_op
= htons(FIP_OP_VLAN
);
42 vlan
->fip
.fip_subcode
= FIP_SC_VL_REQ
;
43 vlan
->fip
.fip_dl_len
= htons(sizeof(vlan
->desc
) / FIP_BPW
);
45 vlan
->desc
.mac
.fd_desc
.fip_dtype
= FIP_DT_MAC
;
46 vlan
->desc
.mac
.fd_desc
.fip_dlen
= sizeof(vlan
->desc
.mac
) / FIP_BPW
;
47 ether_addr_copy(vlan
->desc
.mac
.fd_mac
, qedf
->mac
);
49 vlan
->desc
.wwnn
.fd_desc
.fip_dtype
= FIP_DT_NAME
;
50 vlan
->desc
.wwnn
.fd_desc
.fip_dlen
= sizeof(vlan
->desc
.wwnn
) / FIP_BPW
;
51 put_unaligned_be64(qedf
->lport
->wwnn
, &vlan
->desc
.wwnn
.fd_wwn
);
53 skb_put(skb
, sizeof(*vlan
));
54 skb
->protocol
= htons(ETH_P_FIP
);
55 skb_reset_mac_header(skb
);
56 skb_reset_network_header(skb
);
58 QEDF_INFO(&(qedf
->dbg_ctx
), QEDF_LOG_DISC
, "Sending FIP VLAN "
61 if (atomic_read(&qedf
->link_state
) != QEDF_LINK_UP
) {
62 QEDF_WARN(&(qedf
->dbg_ctx
), "Cannot send vlan request "
63 "because link is not up.\n");
69 set_bit(QED_LL2_XMIT_FLAGS_FIP_DISCOVERY
, &flags
);
70 rc
= qed_ops
->ll2
->start_xmit(qedf
->cdev
, skb
, flags
);
72 QEDF_ERR(&qedf
->dbg_ctx
, "start_xmit failed rc = %d.\n", rc
);
79 static void qedf_fcoe_process_vlan_resp(struct qedf_ctx
*qedf
,
82 struct fip_header
*fiph
;
83 struct fip_desc
*desc
;
88 fiph
= (struct fip_header
*)(((void *)skb
->data
) + 2 * ETH_ALEN
+ 2);
90 rlen
= ntohs(fiph
->fip_dl_len
) * 4;
91 desc
= (struct fip_desc
*)(fiph
+ 1);
93 dlen
= desc
->fip_dlen
* FIP_BPW
;
94 switch (desc
->fip_dtype
) {
96 vid
= ntohs(((struct fip_vlan_desc
*)desc
)->fd_vlan
);
99 desc
= (struct fip_desc
*)((char *)desc
+ dlen
);
103 if (atomic_read(&qedf
->link_state
) == QEDF_LINK_DOWN
) {
104 QEDF_INFO(&qedf
->dbg_ctx
, QEDF_LOG_DISC
,
105 "Dropping VLAN response as link is down.\n");
109 QEDF_INFO(&(qedf
->dbg_ctx
), QEDF_LOG_DISC
, "VLAN response, "
112 if (vid
> 0 && qedf
->vlan_id
!= vid
) {
113 qedf_set_vlan_id(qedf
, vid
);
115 /* Inform waiter that it's ok to call fcoe_ctlr_link up() */
116 if (!completion_done(&qedf
->fipvlan_compl
))
117 complete(&qedf
->fipvlan_compl
);
121 void qedf_fip_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
123 struct qedf_ctx
*qedf
= container_of(fip
, struct qedf_ctx
, ctlr
);
124 struct ethhdr
*eth_hdr
;
125 struct fip_header
*fiph
;
126 u16 op
, vlan_tci
= 0;
130 if (!test_bit(QEDF_LL2_STARTED
, &qedf
->flags
)) {
131 QEDF_WARN(&(qedf
->dbg_ctx
), "LL2 not started\n");
136 fiph
= (struct fip_header
*) ((void *)skb
->data
+ 2 * ETH_ALEN
+ 2);
137 eth_hdr
= (struct ethhdr
*)skb_mac_header(skb
);
138 op
= ntohs(fiph
->fip_op
);
139 sub
= fiph
->fip_subcode
;
142 * Add VLAN tag to non-offload FIP frame based on current stored VLAN
143 * for FIP/FCoE traffic.
145 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), qedf
->vlan_id
);
147 /* Get VLAN ID from skb for printing purposes */
148 __vlan_hwaccel_get_tag(skb
, &vlan_tci
);
150 QEDF_INFO(&(qedf
->dbg_ctx
), QEDF_LOG_LL2
, "FIP frame send: "
151 "dest=%pM op=%x sub=%x vlan=%04x.", eth_hdr
->h_dest
, op
, sub
,
153 if (qedf_dump_frames
)
154 print_hex_dump(KERN_WARNING
, "fip ", DUMP_PREFIX_OFFSET
, 16, 1,
155 skb
->data
, skb
->len
, false);
157 rc
= qed_ops
->ll2
->start_xmit(qedf
->cdev
, skb
, 0);
159 QEDF_ERR(&qedf
->dbg_ctx
, "start_xmit failed rc = %d.\n", rc
);
165 static u8 fcoe_all_enode
[ETH_ALEN
] = FIP_ALL_ENODE_MACS
;
167 /* Process incoming FIP frames. */
168 void qedf_fip_recv(struct qedf_ctx
*qedf
, struct sk_buff
*skb
)
170 struct ethhdr
*eth_hdr
;
171 struct fip_header
*fiph
;
172 struct fip_desc
*desc
;
173 struct fip_mac_desc
*mp
;
174 struct fip_wwn_desc
*wp
;
175 struct fip_vn_desc
*vp
;
179 bool fcf_valid
= false;
180 /* Default is to handle CVL regardless of fabric id descriptor */
181 bool fabric_id_valid
= true;
182 bool fc_wwpn_valid
= false;
186 eth_hdr
= (struct ethhdr
*)skb_mac_header(skb
);
187 fiph
= (struct fip_header
*) ((void *)skb
->data
+ 2 * ETH_ALEN
+ 2);
188 op
= ntohs(fiph
->fip_op
);
189 sub
= fiph
->fip_subcode
;
191 QEDF_INFO(&qedf
->dbg_ctx
, QEDF_LOG_LL2
,
192 "FIP frame received: skb=%p fiph=%p source=%pM destn=%pM op=%x sub=%x vlan=%04x",
193 skb
, fiph
, eth_hdr
->h_source
, eth_hdr
->h_dest
, op
,
195 if (qedf_dump_frames
)
196 print_hex_dump(KERN_WARNING
, "fip ", DUMP_PREFIX_OFFSET
, 16, 1,
197 skb
->data
, skb
->len
, false);
199 if (!ether_addr_equal(eth_hdr
->h_dest
, qedf
->mac
) &&
200 !ether_addr_equal(eth_hdr
->h_dest
, fcoe_all_enode
) &&
201 !ether_addr_equal(eth_hdr
->h_dest
, qedf
->data_src_addr
)) {
202 QEDF_INFO(&qedf
->dbg_ctx
, QEDF_LOG_LL2
,
203 "Dropping FIP type 0x%x pkt due to destination MAC mismatch dest_mac=%pM ctlr.dest_addr=%pM data_src_addr=%pM.\n",
204 op
, eth_hdr
->h_dest
, qedf
->mac
,
205 qedf
->data_src_addr
);
210 /* Handle FIP VLAN resp in the driver */
211 if (op
== FIP_OP_VLAN
&& sub
== FIP_SC_VL_NOTE
) {
212 qedf_fcoe_process_vlan_resp(qedf
, skb
);
214 } else if (op
== FIP_OP_CTRL
&& sub
== FIP_SC_CLR_VLINK
) {
215 QEDF_INFO(&(qedf
->dbg_ctx
), QEDF_LOG_DISC
, "Clear virtual "
218 /* Check that an FCF has been selected by fcoe */
219 if (qedf
->ctlr
.sel_fcf
== NULL
) {
220 QEDF_INFO(&(qedf
->dbg_ctx
), QEDF_LOG_DISC
,
221 "Dropping CVL since FCF has not been selected "
228 * We need to loop through the CVL descriptors to determine
229 * if we want to reset the fcoe link
231 rlen
= ntohs(fiph
->fip_dl_len
) * FIP_BPW
;
232 desc
= (struct fip_desc
*)(fiph
+ 1);
233 while (rlen
>= sizeof(*desc
)) {
234 dlen
= desc
->fip_dlen
* FIP_BPW
;
235 switch (desc
->fip_dtype
) {
237 mp
= (struct fip_mac_desc
*)desc
;
238 QEDF_INFO(&qedf
->dbg_ctx
, QEDF_LOG_DISC
,
239 "Switch fd_mac=%pM.\n", mp
->fd_mac
);
240 if (ether_addr_equal(mp
->fd_mac
,
241 qedf
->ctlr
.sel_fcf
->fcf_mac
))
245 wp
= (struct fip_wwn_desc
*)desc
;
246 switch_name
= get_unaligned_be64(&wp
->fd_wwn
);
247 QEDF_INFO(&qedf
->dbg_ctx
, QEDF_LOG_DISC
,
248 "Switch fd_wwn=%016llx fcf_switch_name=%016llx.\n",
250 qedf
->ctlr
.sel_fcf
->switch_name
);
252 qedf
->ctlr
.sel_fcf
->switch_name
)
253 fc_wwpn_valid
= true;
256 fabric_id_valid
= false;
257 vp
= (struct fip_vn_desc
*)desc
;
259 QEDF_ERR(&qedf
->dbg_ctx
,
260 "CVL vx_port fd_fc_id=0x%x fd_mac=%pM fd_wwpn=%016llx.\n",
261 ntoh24(vp
->fd_fc_id
), vp
->fd_mac
,
262 get_unaligned_be64(&vp
->fd_wwpn
));
263 /* Check for vx_port wwpn OR Check vx_port
264 * fabric ID OR Check vx_port MAC
266 if ((get_unaligned_be64(&vp
->fd_wwpn
) ==
268 (ntoh24(vp
->fd_fc_id
) ==
269 qedf
->lport
->port_id
) ||
270 (ether_addr_equal(vp
->fd_mac
,
271 qedf
->data_src_addr
))) {
272 fabric_id_valid
= true;
276 /* Ignore anything else */
279 desc
= (struct fip_desc
*)((char *)desc
+ dlen
);
283 QEDF_INFO(&qedf
->dbg_ctx
, QEDF_LOG_DISC
,
284 "fcf_valid=%d fabric_id_valid=%d fc_wwpn_valid=%d.\n",
285 fcf_valid
, fabric_id_valid
, fc_wwpn_valid
);
286 if (fcf_valid
&& fabric_id_valid
&& fc_wwpn_valid
)
287 qedf_ctx_soft_reset(qedf
->lport
);
290 /* Everything else is handled by libfcoe */
291 __skb_pull(skb
, ETH_HLEN
);
292 fcoe_ctlr_recv(&qedf
->ctlr
, skb
);
296 u8
*qedf_get_src_mac(struct fc_lport
*lport
)
298 struct qedf_ctx
*qedf
= lport_priv(lport
);
300 return qedf
->data_src_addr
;