xref: /openbmc/linux/drivers/scsi/qedf/qedf_fip.c (revision f66501dc)
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 = -1;
24 
25 	skb = dev_alloc_skb(sizeof(struct fip_vlan));
26 	if (!skb)
27 		return;
28 
29 	eth_fr = (char *)skb->data;
30 	vlan = (struct fip_vlan *)eth_fr;
31 
32 	memset(vlan, 0, sizeof(*vlan));
33 	ether_addr_copy(vlan->eth.h_source, qedf->mac);
34 	ether_addr_copy(vlan->eth.h_dest, my_fcoe_all_fcfs);
35 	vlan->eth.h_proto = htons(ETH_P_FIP);
36 
37 	vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
38 	vlan->fip.fip_op = htons(FIP_OP_VLAN);
39 	vlan->fip.fip_subcode = FIP_SC_VL_REQ;
40 	vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
41 
42 	vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
43 	vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
44 	ether_addr_copy(vlan->desc.mac.fd_mac, qedf->mac);
45 
46 	vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
47 	vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
48 	put_unaligned_be64(qedf->lport->wwnn, &vlan->desc.wwnn.fd_wwn);
49 
50 	skb_put(skb, sizeof(*vlan));
51 	skb->protocol = htons(ETH_P_FIP);
52 	skb_reset_mac_header(skb);
53 	skb_reset_network_header(skb);
54 
55 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Sending FIP VLAN "
56 		   "request.");
57 
58 	if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
59 		QEDF_WARN(&(qedf->dbg_ctx), "Cannot send vlan request "
60 		    "because link is not up.\n");
61 
62 		kfree_skb(skb);
63 		return;
64 	}
65 
66 	set_bit(QED_LL2_XMIT_FLAGS_FIP_DISCOVERY, &flags);
67 	rc = qed_ops->ll2->start_xmit(qedf->cdev, skb, flags);
68 	if (rc) {
69 		QEDF_ERR(&qedf->dbg_ctx, "start_xmit failed rc = %d.\n", rc);
70 		kfree_skb(skb);
71 		return;
72 	}
73 
74 }
75 
76 static void qedf_fcoe_process_vlan_resp(struct qedf_ctx *qedf,
77 	struct sk_buff *skb)
78 {
79 	struct fip_header *fiph;
80 	struct fip_desc *desc;
81 	u16 vid = 0;
82 	ssize_t rlen;
83 	size_t dlen;
84 
85 	fiph = (struct fip_header *)(((void *)skb->data) + 2 * ETH_ALEN + 2);
86 
87 	rlen = ntohs(fiph->fip_dl_len) * 4;
88 	desc = (struct fip_desc *)(fiph + 1);
89 	while (rlen > 0) {
90 		dlen = desc->fip_dlen * FIP_BPW;
91 		switch (desc->fip_dtype) {
92 		case FIP_DT_VLAN:
93 			vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
94 			break;
95 		}
96 		desc = (struct fip_desc *)((char *)desc + dlen);
97 		rlen -= dlen;
98 	}
99 
100 	if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
101 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
102 			  "Dropping VLAN response as link is down.\n");
103 		return;
104 	}
105 
106 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "VLAN response, "
107 		   "vid=0x%x.\n", vid);
108 
109 	if (vid > 0 && qedf->vlan_id != vid) {
110 		qedf_set_vlan_id(qedf, vid);
111 
112 		/* Inform waiter that it's ok to call fcoe_ctlr_link up() */
113 		if (!completion_done(&qedf->fipvlan_compl))
114 			complete(&qedf->fipvlan_compl);
115 	}
116 }
117 
118 void qedf_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
119 {
120 	struct qedf_ctx *qedf = container_of(fip, struct qedf_ctx, ctlr);
121 	struct ethhdr *eth_hdr;
122 	struct fip_header *fiph;
123 	u16 op, vlan_tci = 0;
124 	u8 sub;
125 	int rc = -1;
126 
127 	if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
128 		QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
129 		kfree_skb(skb);
130 		return;
131 	}
132 
133 	fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
134 	eth_hdr = (struct ethhdr *)skb_mac_header(skb);
135 	op = ntohs(fiph->fip_op);
136 	sub = fiph->fip_subcode;
137 
138 	/*
139 	 * Add VLAN tag to non-offload FIP frame based on current stored VLAN
140 	 * for FIP/FCoE traffic.
141 	 */
142 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
143 
144 	/* Get VLAN ID from skb for printing purposes */
145 	__vlan_hwaccel_get_tag(skb, &vlan_tci);
146 
147 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FIP frame send: "
148 	    "dest=%pM op=%x sub=%x vlan=%04x.", eth_hdr->h_dest, op, sub,
149 	    vlan_tci);
150 	if (qedf_dump_frames)
151 		print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
152 		    skb->data, skb->len, false);
153 
154 	rc = qed_ops->ll2->start_xmit(qedf->cdev, skb, 0);
155 	if (rc) {
156 		QEDF_ERR(&qedf->dbg_ctx, "start_xmit failed rc = %d.\n", rc);
157 		kfree_skb(skb);
158 		return;
159 	}
160 }
161 
162 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
163 
164 /* Process incoming FIP frames. */
165 void qedf_fip_recv(struct qedf_ctx *qedf, struct sk_buff *skb)
166 {
167 	struct ethhdr *eth_hdr;
168 	struct fip_header *fiph;
169 	struct fip_desc *desc;
170 	struct fip_mac_desc *mp;
171 	struct fip_wwn_desc *wp;
172 	struct fip_vn_desc *vp;
173 	size_t rlen, dlen;
174 	u16 op;
175 	u8 sub;
176 	bool fcf_valid = false;
177 	/* Default is to handle CVL regardless of fabric id descriptor */
178 	bool fabric_id_valid = true;
179 	bool fc_wwpn_valid = false;
180 	u64 switch_name;
181 	u16 vlan = 0;
182 
183 	eth_hdr = (struct ethhdr *)skb_mac_header(skb);
184 	fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
185 	op = ntohs(fiph->fip_op);
186 	sub = fiph->fip_subcode;
187 
188 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
189 		  "FIP frame received: skb=%p fiph=%p source=%pM destn=%pM op=%x sub=%x vlan=%04x",
190 		  skb, fiph, eth_hdr->h_source, eth_hdr->h_dest, op,
191 		  sub, vlan);
192 	if (qedf_dump_frames)
193 		print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
194 		    skb->data, skb->len, false);
195 
196 	if (!ether_addr_equal(eth_hdr->h_dest, qedf->mac) &&
197 	    !ether_addr_equal(eth_hdr->h_dest, fcoe_all_enode) &&
198 		!ether_addr_equal(eth_hdr->h_dest, qedf->data_src_addr)) {
199 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
200 			  "Dropping FIP type 0x%x pkt due to destination MAC mismatch dest_mac=%pM ctlr.dest_addr=%pM data_src_addr=%pM.\n",
201 			  op, eth_hdr->h_dest, qedf->mac,
202 			  qedf->data_src_addr);
203 		kfree_skb(skb);
204 		return;
205 	}
206 
207 	/* Handle FIP VLAN resp in the driver */
208 	if (op == FIP_OP_VLAN && sub == FIP_SC_VL_NOTE) {
209 		qedf_fcoe_process_vlan_resp(qedf, skb);
210 		kfree_skb(skb);
211 	} else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
212 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Clear virtual "
213 			   "link received.\n");
214 
215 		/* Check that an FCF has been selected by fcoe */
216 		if (qedf->ctlr.sel_fcf == NULL) {
217 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
218 			    "Dropping CVL since FCF has not been selected "
219 			    "yet.");
220 			kfree_skb(skb);
221 			return;
222 		}
223 
224 		/*
225 		 * We need to loop through the CVL descriptors to determine
226 		 * if we want to reset the fcoe link
227 		 */
228 		rlen = ntohs(fiph->fip_dl_len) * FIP_BPW;
229 		desc = (struct fip_desc *)(fiph + 1);
230 		while (rlen >= sizeof(*desc)) {
231 			dlen = desc->fip_dlen * FIP_BPW;
232 			switch (desc->fip_dtype) {
233 			case FIP_DT_MAC:
234 				mp = (struct fip_mac_desc *)desc;
235 				QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
236 					  "Switch fd_mac=%pM.\n", mp->fd_mac);
237 				if (ether_addr_equal(mp->fd_mac,
238 				    qedf->ctlr.sel_fcf->fcf_mac))
239 					fcf_valid = true;
240 				break;
241 			case FIP_DT_NAME:
242 				wp = (struct fip_wwn_desc *)desc;
243 				switch_name = get_unaligned_be64(&wp->fd_wwn);
244 				QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
245 					  "Switch fd_wwn=%016llx fcf_switch_name=%016llx.\n",
246 					  switch_name,
247 					  qedf->ctlr.sel_fcf->switch_name);
248 				if (switch_name ==
249 				    qedf->ctlr.sel_fcf->switch_name)
250 					fc_wwpn_valid = true;
251 				break;
252 			case FIP_DT_VN_ID:
253 				vp = (struct fip_vn_desc *)desc;
254 				QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
255 					  "vx_port fd_fc_id=%x fd_mac=%pM.\n",
256 					  ntoh24(vp->fd_fc_id), vp->fd_mac);
257 				/* Check vx_port fabric ID */
258 				if (ntoh24(vp->fd_fc_id) !=
259 				    qedf->lport->port_id)
260 					fabric_id_valid = false;
261 				/* Check vx_port MAC */
262 				if (!ether_addr_equal(vp->fd_mac,
263 						      qedf->data_src_addr))
264 					fabric_id_valid = false;
265 				break;
266 			default:
267 				/* Ignore anything else */
268 				break;
269 			}
270 			desc = (struct fip_desc *)((char *)desc + dlen);
271 			rlen -= dlen;
272 		}
273 
274 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
275 			  "fcf_valid=%d fabric_id_valid=%d fc_wwpn_valid=%d.\n",
276 			  fcf_valid, fabric_id_valid, fc_wwpn_valid);
277 		if (fcf_valid && fabric_id_valid && fc_wwpn_valid)
278 			qedf_ctx_soft_reset(qedf->lport);
279 		kfree_skb(skb);
280 	} else {
281 		/* Everything else is handled by libfcoe */
282 		__skb_pull(skb, ETH_HLEN);
283 		fcoe_ctlr_recv(&qedf->ctlr, skb);
284 	}
285 }
286 
287 u8 *qedf_get_src_mac(struct fc_lport *lport)
288 {
289 	struct qedf_ctx *qedf = lport_priv(lport);
290 
291 	return qedf->data_src_addr;
292 }
293