xref: /openbmc/linux/drivers/scsi/qedf/qedf_fip.c (revision fc37ddce937bb245cc3490fbaa98e64f31bb6751)
161d8658bSDupuis, Chad /*
261d8658bSDupuis, Chad  *  QLogic FCoE Offload Driver
35d1c8b5bSChad Dupuis  *  Copyright (c) 2016-2018 Cavium Inc.
461d8658bSDupuis, Chad  *
561d8658bSDupuis, Chad  *  This software is available under the terms of the GNU General Public License
661d8658bSDupuis, Chad  *  (GPL) Version 2, available from the file COPYING in the main directory of
761d8658bSDupuis, Chad  *  this source tree.
861d8658bSDupuis, Chad  */
961d8658bSDupuis, Chad #include <linux/if_ether.h>
1061d8658bSDupuis, Chad #include <linux/if_vlan.h>
1161d8658bSDupuis, Chad #include "qedf.h"
1261d8658bSDupuis, Chad 
1361d8658bSDupuis, Chad extern const struct qed_fcoe_ops *qed_ops;
1461d8658bSDupuis, Chad /*
1561d8658bSDupuis, Chad  * FIP VLAN functions that will eventually move to libfcoe.
1661d8658bSDupuis, Chad  */
1761d8658bSDupuis, Chad 
1861d8658bSDupuis, Chad void qedf_fcoe_send_vlan_req(struct qedf_ctx *qedf)
1961d8658bSDupuis, Chad {
2061d8658bSDupuis, Chad 	struct sk_buff *skb;
2161d8658bSDupuis, Chad 	char *eth_fr;
2261d8658bSDupuis, Chad 	int fr_len;
2361d8658bSDupuis, Chad 	struct fip_vlan *vlan;
2461d8658bSDupuis, Chad #define MY_FIP_ALL_FCF_MACS        ((__u8[6]) { 1, 0x10, 0x18, 1, 0, 2 })
2561d8658bSDupuis, Chad 	static u8 my_fcoe_all_fcfs[ETH_ALEN] = MY_FIP_ALL_FCF_MACS;
26cac6f691SSudarsana Reddy Kalluru 	unsigned long flags = 0;
2761d8658bSDupuis, Chad 
2861d8658bSDupuis, Chad 	skb = dev_alloc_skb(sizeof(struct fip_vlan));
2961d8658bSDupuis, Chad 	if (!skb)
3061d8658bSDupuis, Chad 		return;
3161d8658bSDupuis, Chad 
3261d8658bSDupuis, Chad 	fr_len = sizeof(*vlan);
3361d8658bSDupuis, Chad 	eth_fr = (char *)skb->data;
3461d8658bSDupuis, Chad 	vlan = (struct fip_vlan *)eth_fr;
3561d8658bSDupuis, Chad 
3661d8658bSDupuis, Chad 	memset(vlan, 0, sizeof(*vlan));
3761d8658bSDupuis, Chad 	ether_addr_copy(vlan->eth.h_source, qedf->mac);
3861d8658bSDupuis, Chad 	ether_addr_copy(vlan->eth.h_dest, my_fcoe_all_fcfs);
3961d8658bSDupuis, Chad 	vlan->eth.h_proto = htons(ETH_P_FIP);
4061d8658bSDupuis, Chad 
4161d8658bSDupuis, Chad 	vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
4261d8658bSDupuis, Chad 	vlan->fip.fip_op = htons(FIP_OP_VLAN);
4361d8658bSDupuis, Chad 	vlan->fip.fip_subcode = FIP_SC_VL_REQ;
4461d8658bSDupuis, Chad 	vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
4561d8658bSDupuis, Chad 
4661d8658bSDupuis, Chad 	vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
4761d8658bSDupuis, Chad 	vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
4861d8658bSDupuis, Chad 	ether_addr_copy(vlan->desc.mac.fd_mac, qedf->mac);
4961d8658bSDupuis, Chad 
5061d8658bSDupuis, Chad 	vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
5161d8658bSDupuis, Chad 	vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
5261d8658bSDupuis, Chad 	put_unaligned_be64(qedf->lport->wwnn, &vlan->desc.wwnn.fd_wwn);
5361d8658bSDupuis, Chad 
5461d8658bSDupuis, Chad 	skb_put(skb, sizeof(*vlan));
5561d8658bSDupuis, Chad 	skb->protocol = htons(ETH_P_FIP);
5661d8658bSDupuis, Chad 	skb_reset_mac_header(skb);
5761d8658bSDupuis, Chad 	skb_reset_network_header(skb);
5861d8658bSDupuis, Chad 
5961d8658bSDupuis, Chad 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Sending FIP VLAN "
6061d8658bSDupuis, Chad 		   "request.");
6161d8658bSDupuis, Chad 
6261d8658bSDupuis, Chad 	if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
6361d8658bSDupuis, Chad 		QEDF_WARN(&(qedf->dbg_ctx), "Cannot send vlan request "
6461d8658bSDupuis, Chad 		    "because link is not up.\n");
6561d8658bSDupuis, Chad 
6661d8658bSDupuis, Chad 		kfree_skb(skb);
6761d8658bSDupuis, Chad 		return;
6861d8658bSDupuis, Chad 	}
69cac6f691SSudarsana Reddy Kalluru 
70cac6f691SSudarsana Reddy Kalluru 	set_bit(QED_LL2_XMIT_FLAGS_FIP_DISCOVERY, &flags);
71cac6f691SSudarsana Reddy Kalluru 	qed_ops->ll2->start_xmit(qedf->cdev, skb, flags);
7261d8658bSDupuis, Chad }
7361d8658bSDupuis, Chad 
7461d8658bSDupuis, Chad static void qedf_fcoe_process_vlan_resp(struct qedf_ctx *qedf,
7561d8658bSDupuis, Chad 	struct sk_buff *skb)
7661d8658bSDupuis, Chad {
7761d8658bSDupuis, Chad 	struct fip_header *fiph;
7861d8658bSDupuis, Chad 	struct fip_desc *desc;
7961d8658bSDupuis, Chad 	u16 vid = 0;
8061d8658bSDupuis, Chad 	ssize_t rlen;
8161d8658bSDupuis, Chad 	size_t dlen;
8261d8658bSDupuis, Chad 
8361d8658bSDupuis, Chad 	fiph = (struct fip_header *)(((void *)skb->data) + 2 * ETH_ALEN + 2);
8461d8658bSDupuis, Chad 
8561d8658bSDupuis, Chad 	rlen = ntohs(fiph->fip_dl_len) * 4;
8661d8658bSDupuis, Chad 	desc = (struct fip_desc *)(fiph + 1);
8761d8658bSDupuis, Chad 	while (rlen > 0) {
8861d8658bSDupuis, Chad 		dlen = desc->fip_dlen * FIP_BPW;
8961d8658bSDupuis, Chad 		switch (desc->fip_dtype) {
9061d8658bSDupuis, Chad 		case FIP_DT_VLAN:
9161d8658bSDupuis, Chad 			vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
9261d8658bSDupuis, Chad 			break;
9361d8658bSDupuis, Chad 		}
9461d8658bSDupuis, Chad 		desc = (struct fip_desc *)((char *)desc + dlen);
9561d8658bSDupuis, Chad 		rlen -= dlen;
9661d8658bSDupuis, Chad 	}
9761d8658bSDupuis, Chad 
9876dbf4ffSSaurav Kashyap 	if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
9976dbf4ffSSaurav Kashyap 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
10076dbf4ffSSaurav Kashyap 			  "Dropping VLAN response as link is down.\n");
10176dbf4ffSSaurav Kashyap 		return;
10276dbf4ffSSaurav Kashyap 	}
10376dbf4ffSSaurav Kashyap 
10461d8658bSDupuis, Chad 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "VLAN response, "
10561d8658bSDupuis, Chad 		   "vid=0x%x.\n", vid);
10661d8658bSDupuis, Chad 
10761d8658bSDupuis, Chad 	if (vid > 0 && qedf->vlan_id != vid) {
10861d8658bSDupuis, Chad 		qedf_set_vlan_id(qedf, vid);
10961d8658bSDupuis, Chad 
11061d8658bSDupuis, Chad 		/* Inform waiter that it's ok to call fcoe_ctlr_link up() */
1118eaf7dfcSChad Dupuis 		if (!completion_done(&qedf->fipvlan_compl))
11261d8658bSDupuis, Chad 			complete(&qedf->fipvlan_compl);
11361d8658bSDupuis, Chad 	}
11461d8658bSDupuis, Chad }
11561d8658bSDupuis, Chad 
11661d8658bSDupuis, Chad void qedf_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
11761d8658bSDupuis, Chad {
11861d8658bSDupuis, Chad 	struct qedf_ctx *qedf = container_of(fip, struct qedf_ctx, ctlr);
11961d8658bSDupuis, Chad 	struct ethhdr *eth_hdr;
12061d8658bSDupuis, Chad 	struct fip_header *fiph;
12161d8658bSDupuis, Chad 	u16 op, vlan_tci = 0;
12261d8658bSDupuis, Chad 	u8 sub;
12361d8658bSDupuis, Chad 
12461d8658bSDupuis, Chad 	if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
12561d8658bSDupuis, Chad 		QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
12661d8658bSDupuis, Chad 		kfree_skb(skb);
12761d8658bSDupuis, Chad 		return;
12861d8658bSDupuis, Chad 	}
12961d8658bSDupuis, Chad 
13061d8658bSDupuis, Chad 	fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
13161d8658bSDupuis, Chad 	eth_hdr = (struct ethhdr *)skb_mac_header(skb);
13261d8658bSDupuis, Chad 	op = ntohs(fiph->fip_op);
13361d8658bSDupuis, Chad 	sub = fiph->fip_subcode;
13461d8658bSDupuis, Chad 
135cf291163SChad Dupuis 	/*
136cf291163SChad Dupuis 	 * Add VLAN tag to non-offload FIP frame based on current stored VLAN
137cf291163SChad Dupuis 	 * for FIP/FCoE traffic.
138cf291163SChad Dupuis 	 */
139cf291163SChad Dupuis 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
14061d8658bSDupuis, Chad 
141cf291163SChad Dupuis 	/* Get VLAN ID from skb for printing purposes */
142cf291163SChad Dupuis 	__vlan_hwaccel_get_tag(skb, &vlan_tci);
14361d8658bSDupuis, Chad 
14461d8658bSDupuis, Chad 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FIP frame send: "
14561d8658bSDupuis, Chad 	    "dest=%pM op=%x sub=%x vlan=%04x.", eth_hdr->h_dest, op, sub,
14615a93de7SChad Dupuis 	    vlan_tci);
14761d8658bSDupuis, Chad 	if (qedf_dump_frames)
14861d8658bSDupuis, Chad 		print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
14961d8658bSDupuis, Chad 		    skb->data, skb->len, false);
15061d8658bSDupuis, Chad 
151cac6f691SSudarsana Reddy Kalluru 	qed_ops->ll2->start_xmit(qedf->cdev, skb, 0);
15261d8658bSDupuis, Chad }
15361d8658bSDupuis, Chad 
154*fc37ddceSChad Dupuis static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
155*fc37ddceSChad Dupuis 
15661d8658bSDupuis, Chad /* Process incoming FIP frames. */
15761d8658bSDupuis, Chad void qedf_fip_recv(struct qedf_ctx *qedf, struct sk_buff *skb)
15861d8658bSDupuis, Chad {
15961d8658bSDupuis, Chad 	struct ethhdr *eth_hdr;
16061d8658bSDupuis, Chad 	struct fip_header *fiph;
16161d8658bSDupuis, Chad 	struct fip_desc *desc;
16261d8658bSDupuis, Chad 	struct fip_mac_desc *mp;
16361d8658bSDupuis, Chad 	struct fip_wwn_desc *wp;
16461d8658bSDupuis, Chad 	struct fip_vn_desc *vp;
16561d8658bSDupuis, Chad 	size_t rlen, dlen;
16661d8658bSDupuis, Chad 	u16 op;
16761d8658bSDupuis, Chad 	u8 sub;
168*fc37ddceSChad Dupuis 	bool fcf_valid = false;
169*fc37ddceSChad Dupuis 	/* Default is to handle CVL regardless of fabric id descriptor */
170*fc37ddceSChad Dupuis 	bool fabric_id_valid = true;
171*fc37ddceSChad Dupuis 	bool fc_wwpn_valid = false;
172*fc37ddceSChad Dupuis 	u64 switch_name;
173*fc37ddceSChad Dupuis 	u16 vlan = 0;
17461d8658bSDupuis, Chad 
17561d8658bSDupuis, Chad 	eth_hdr = (struct ethhdr *)skb_mac_header(skb);
17661d8658bSDupuis, Chad 	fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
17761d8658bSDupuis, Chad 	op = ntohs(fiph->fip_op);
17861d8658bSDupuis, Chad 	sub = fiph->fip_subcode;
17961d8658bSDupuis, Chad 
180*fc37ddceSChad Dupuis 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
181*fc37ddceSChad Dupuis 		  "FIP frame received: skb=%p fiph=%p source=%pM destn=%pM op=%x sub=%x vlan=%04x",
182*fc37ddceSChad Dupuis 		  skb, fiph, eth_hdr->h_source, eth_hdr->h_dest, op,
183*fc37ddceSChad Dupuis 		  sub, vlan);
18461d8658bSDupuis, Chad 	if (qedf_dump_frames)
18561d8658bSDupuis, Chad 		print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
18661d8658bSDupuis, Chad 		    skb->data, skb->len, false);
18761d8658bSDupuis, Chad 
188*fc37ddceSChad Dupuis 	if (!ether_addr_equal(eth_hdr->h_dest, qedf->mac) &&
189*fc37ddceSChad Dupuis 	    !ether_addr_equal(eth_hdr->h_dest, fcoe_all_enode) &&
190*fc37ddceSChad Dupuis 		!ether_addr_equal(eth_hdr->h_dest, qedf->data_src_addr)) {
191*fc37ddceSChad Dupuis 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
192*fc37ddceSChad Dupuis 			  "Dropping FIP type 0x%x pkt due to destination MAC mismatch dest_mac=%pM ctlr.dest_addr=%pM data_src_addr=%pM.\n",
193*fc37ddceSChad Dupuis 			  op, eth_hdr->h_dest, qedf->mac,
194*fc37ddceSChad Dupuis 			  qedf->data_src_addr);
195*fc37ddceSChad Dupuis 		kfree_skb(skb);
196*fc37ddceSChad Dupuis 		return;
197*fc37ddceSChad Dupuis 	}
198*fc37ddceSChad Dupuis 
19961d8658bSDupuis, Chad 	/* Handle FIP VLAN resp in the driver */
20061d8658bSDupuis, Chad 	if (op == FIP_OP_VLAN && sub == FIP_SC_VL_NOTE) {
20161d8658bSDupuis, Chad 		qedf_fcoe_process_vlan_resp(qedf, skb);
20261d8658bSDupuis, Chad 		kfree_skb(skb);
20361d8658bSDupuis, Chad 	} else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
20461d8658bSDupuis, Chad 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Clear virtual "
20561d8658bSDupuis, Chad 			   "link received.\n");
20661d8658bSDupuis, Chad 
20761d8658bSDupuis, Chad 		/* Check that an FCF has been selected by fcoe */
20861d8658bSDupuis, Chad 		if (qedf->ctlr.sel_fcf == NULL) {
20961d8658bSDupuis, Chad 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
21061d8658bSDupuis, Chad 			    "Dropping CVL since FCF has not been selected "
21161d8658bSDupuis, Chad 			    "yet.");
212f32803bbSChad Dupuis 			kfree_skb(skb);
21361d8658bSDupuis, Chad 			return;
21461d8658bSDupuis, Chad 		}
21561d8658bSDupuis, Chad 
21661d8658bSDupuis, Chad 		/*
21761d8658bSDupuis, Chad 		 * We need to loop through the CVL descriptors to determine
21861d8658bSDupuis, Chad 		 * if we want to reset the fcoe link
21961d8658bSDupuis, Chad 		 */
22061d8658bSDupuis, Chad 		rlen = ntohs(fiph->fip_dl_len) * FIP_BPW;
22161d8658bSDupuis, Chad 		desc = (struct fip_desc *)(fiph + 1);
22261d8658bSDupuis, Chad 		while (rlen >= sizeof(*desc)) {
22361d8658bSDupuis, Chad 			dlen = desc->fip_dlen * FIP_BPW;
22461d8658bSDupuis, Chad 			switch (desc->fip_dtype) {
22561d8658bSDupuis, Chad 			case FIP_DT_MAC:
22661d8658bSDupuis, Chad 				mp = (struct fip_mac_desc *)desc;
227*fc37ddceSChad Dupuis 				QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
228*fc37ddceSChad Dupuis 					  "Switch fd_mac=%pM.\n", mp->fd_mac);
22953c51adbSChad Dupuis 				if (ether_addr_equal(mp->fd_mac,
23053c51adbSChad Dupuis 				    qedf->ctlr.sel_fcf->fcf_mac))
231*fc37ddceSChad Dupuis 					fcf_valid = true;
23261d8658bSDupuis, Chad 				break;
23361d8658bSDupuis, Chad 			case FIP_DT_NAME:
23461d8658bSDupuis, Chad 				wp = (struct fip_wwn_desc *)desc;
235*fc37ddceSChad Dupuis 				switch_name = get_unaligned_be64(&wp->fd_wwn);
236*fc37ddceSChad Dupuis 				QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
237*fc37ddceSChad Dupuis 					  "Switch fd_wwn=%016llx fcf_switch_name=%016llx.\n",
238*fc37ddceSChad Dupuis 					  switch_name,
239*fc37ddceSChad Dupuis 					  qedf->ctlr.sel_fcf->switch_name);
240*fc37ddceSChad Dupuis 				if (switch_name ==
241*fc37ddceSChad Dupuis 				    qedf->ctlr.sel_fcf->switch_name)
242*fc37ddceSChad Dupuis 					fc_wwpn_valid = true;
24361d8658bSDupuis, Chad 				break;
24461d8658bSDupuis, Chad 			case FIP_DT_VN_ID:
24561d8658bSDupuis, Chad 				vp = (struct fip_vn_desc *)desc;
246*fc37ddceSChad Dupuis 				QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
247*fc37ddceSChad Dupuis 					  "vx_port fd_fc_id=%x fd_mac=%pM.\n",
248*fc37ddceSChad Dupuis 					  ntoh24(vp->fd_fc_id), vp->fd_mac);
249*fc37ddceSChad Dupuis 				/* Check vx_port fabric ID */
250*fc37ddceSChad Dupuis 				if (ntoh24(vp->fd_fc_id) !=
25153c51adbSChad Dupuis 				    qedf->lport->port_id)
252*fc37ddceSChad Dupuis 					fabric_id_valid = false;
253*fc37ddceSChad Dupuis 				/* Check vx_port MAC */
254*fc37ddceSChad Dupuis 				if (!ether_addr_equal(vp->fd_mac,
255*fc37ddceSChad Dupuis 						      qedf->data_src_addr))
256*fc37ddceSChad Dupuis 					fabric_id_valid = false;
25761d8658bSDupuis, Chad 				break;
25861d8658bSDupuis, Chad 			default:
25961d8658bSDupuis, Chad 				/* Ignore anything else */
26061d8658bSDupuis, Chad 				break;
26161d8658bSDupuis, Chad 			}
26261d8658bSDupuis, Chad 			desc = (struct fip_desc *)((char *)desc + dlen);
26361d8658bSDupuis, Chad 			rlen -= dlen;
26461d8658bSDupuis, Chad 		}
26561d8658bSDupuis, Chad 
266*fc37ddceSChad Dupuis 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
267*fc37ddceSChad Dupuis 			  "fcf_valid=%d fabric_id_valid=%d fc_wwpn_valid=%d.\n",
268*fc37ddceSChad Dupuis 			  fcf_valid, fabric_id_valid, fc_wwpn_valid);
269*fc37ddceSChad Dupuis 		if (fcf_valid && fabric_id_valid && fc_wwpn_valid)
270a66c6cd2SChad Dupuis 			qedf_ctx_soft_reset(qedf->lport);
27161d8658bSDupuis, Chad 		kfree_skb(skb);
27261d8658bSDupuis, Chad 	} else {
27361d8658bSDupuis, Chad 		/* Everything else is handled by libfcoe */
27461d8658bSDupuis, Chad 		__skb_pull(skb, ETH_HLEN);
27561d8658bSDupuis, Chad 		fcoe_ctlr_recv(&qedf->ctlr, skb);
27661d8658bSDupuis, Chad 	}
27761d8658bSDupuis, Chad }
27861d8658bSDupuis, Chad 
27961d8658bSDupuis, Chad u8 *qedf_get_src_mac(struct fc_lport *lport)
28061d8658bSDupuis, Chad {
28161d8658bSDupuis, Chad 	struct qedf_ctx *qedf = lport_priv(lport);
28261d8658bSDupuis, Chad 
28361d8658bSDupuis, Chad 	return qedf->data_src_addr;
28461d8658bSDupuis, Chad }
285