xref: /linux/drivers/scsi/qedf/qedf_fip.c (revision ca853314e78b0a65c20b6a889a23c31f918d4aa2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  QLogic FCoE Offload Driver
4  *  Copyright (c) 2016-2018 Cavium Inc.
5  */
6 #include <linux/if_ether.h>
7 #include <linux/if_vlan.h>
8 #include "qedf.h"
9 
10 extern const struct qed_fcoe_ops *qed_ops;
11 /*
12  * FIP VLAN functions that will eventually move to libfcoe.
13  */
14 
15 void qedf_fcoe_send_vlan_req(struct qedf_ctx *qedf)
16 {
17 	struct sk_buff *skb;
18 	char *eth_fr;
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;
23 	int rc;
24 
25 	skb = dev_alloc_skb(sizeof(struct fip_vlan));
26 	if (!skb) {
27 		QEDF_ERR(&qedf->dbg_ctx,
28 			 "Failed to allocate skb.\n");
29 		return;
30 	}
31 
32 	eth_fr = (char *)skb->data;
33 	vlan = (struct fip_vlan *)eth_fr;
34 
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);
39 
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);
44 
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);
48 
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);
52 
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);
57 
58 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Sending FIP VLAN "
59 		   "request.");
60 
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");
64 
65 		kfree_skb(skb);
66 		return;
67 	}
68 
69 	set_bit(QED_LL2_XMIT_FLAGS_FIP_DISCOVERY, &flags);
70 	rc = qed_ops->ll2->start_xmit(qedf->cdev, skb, flags);
71 	if (rc) {
72 		QEDF_ERR(&qedf->dbg_ctx, "start_xmit failed rc = %d.\n", rc);
73 		kfree_skb(skb);
74 		return;
75 	}
76 
77 }
78 
79 static void qedf_fcoe_process_vlan_resp(struct qedf_ctx *qedf,
80 	struct sk_buff *skb)
81 {
82 	struct fip_header *fiph;
83 	struct fip_desc *desc;
84 	u16 vid = 0;
85 	ssize_t rlen;
86 	size_t dlen;
87 
88 	fiph = (struct fip_header *)(((void *)skb->data) + 2 * ETH_ALEN + 2);
89 
90 	rlen = ntohs(fiph->fip_dl_len) * 4;
91 	desc = (struct fip_desc *)(fiph + 1);
92 	while (rlen > 0) {
93 		dlen = desc->fip_dlen * FIP_BPW;
94 		switch (desc->fip_dtype) {
95 		case FIP_DT_VLAN:
96 			vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
97 			break;
98 		}
99 		desc = (struct fip_desc *)((char *)desc + dlen);
100 		rlen -= dlen;
101 	}
102 
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");
106 		return;
107 	}
108 
109 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "VLAN response, "
110 		   "vid=0x%x.\n", vid);
111 
112 	if (vid > 0 && qedf->vlan_id != vid) {
113 		qedf_set_vlan_id(qedf, vid);
114 
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);
118 	}
119 }
120 
121 void qedf_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
122 {
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;
127 	u8 sub;
128 	int rc = -1;
129 
130 	if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
131 		QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
132 		kfree_skb(skb);
133 		return;
134 	}
135 
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;
140 
141 	/*
142 	 * Add VLAN tag to non-offload FIP frame based on current stored VLAN
143 	 * for FIP/FCoE traffic.
144 	 */
145 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
146 
147 	/* Get VLAN ID from skb for printing purposes */
148 	__vlan_hwaccel_get_tag(skb, &vlan_tci);
149 
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,
152 	    vlan_tci);
153 	if (qedf_dump_frames)
154 		print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
155 		    skb->data, skb->len, false);
156 
157 	rc = qed_ops->ll2->start_xmit(qedf->cdev, skb, 0);
158 	if (rc) {
159 		QEDF_ERR(&qedf->dbg_ctx, "start_xmit failed rc = %d.\n", rc);
160 		kfree_skb(skb);
161 		return;
162 	}
163 }
164 
165 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
166 
167 /* Process incoming FIP frames. */
168 void qedf_fip_recv(struct qedf_ctx *qedf, struct sk_buff *skb)
169 {
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;
176 	size_t rlen, dlen;
177 	u16 op;
178 	u8 sub;
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;
183 	u64 switch_name;
184 	u16 vlan = 0;
185 
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;
190 
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,
194 		  sub, vlan);
195 	if (qedf_dump_frames)
196 		print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
197 		    skb->data, skb->len, false);
198 
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);
206 		kfree_skb(skb);
207 		return;
208 	}
209 
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);
213 		kfree_skb(skb);
214 	} else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
215 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Clear virtual "
216 			   "link received.\n");
217 
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 "
222 			    "yet.");
223 			kfree_skb(skb);
224 			return;
225 		}
226 
227 		/*
228 		 * We need to loop through the CVL descriptors to determine
229 		 * if we want to reset the fcoe link
230 		 */
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) {
236 			case FIP_DT_MAC:
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))
242 					fcf_valid = true;
243 				break;
244 			case FIP_DT_NAME:
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",
249 					  switch_name,
250 					  qedf->ctlr.sel_fcf->switch_name);
251 				if (switch_name ==
252 				    qedf->ctlr.sel_fcf->switch_name)
253 					fc_wwpn_valid = true;
254 				break;
255 			case FIP_DT_VN_ID:
256 				fabric_id_valid = false;
257 				vp = (struct fip_vn_desc *)desc;
258 
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
265 				 */
266 				if ((get_unaligned_be64(&vp->fd_wwpn) ==
267 					qedf->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;
273 				}
274 				break;
275 			default:
276 				/* Ignore anything else */
277 				break;
278 			}
279 			desc = (struct fip_desc *)((char *)desc + dlen);
280 			rlen -= dlen;
281 		}
282 
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);
288 		kfree_skb(skb);
289 	} else {
290 		/* Everything else is handled by libfcoe */
291 		__skb_pull(skb, ETH_HLEN);
292 		fcoe_ctlr_recv(&qedf->ctlr, skb);
293 	}
294 }
295 
296 u8 *qedf_get_src_mac(struct fc_lport *lport)
297 {
298 	struct qedf_ctx *qedf = lport_priv(lport);
299 
300 	return qedf->data_src_addr;
301 }
302