13287e96aSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
261d8658bSDupuis, Chad /*
361d8658bSDupuis, Chad * QLogic FCoE Offload Driver
45d1c8b5bSChad Dupuis * Copyright (c) 2016-2018 Cavium Inc.
561d8658bSDupuis, Chad */
661d8658bSDupuis, Chad #include <linux/if_ether.h>
761d8658bSDupuis, Chad #include <linux/if_vlan.h>
861d8658bSDupuis, Chad #include "qedf.h"
961d8658bSDupuis, Chad
1061d8658bSDupuis, Chad extern const struct qed_fcoe_ops *qed_ops;
1161d8658bSDupuis, Chad /*
1261d8658bSDupuis, Chad * FIP VLAN functions that will eventually move to libfcoe.
1361d8658bSDupuis, Chad */
1461d8658bSDupuis, Chad
qedf_fcoe_send_vlan_req(struct qedf_ctx * qedf)1561d8658bSDupuis, Chad void qedf_fcoe_send_vlan_req(struct qedf_ctx *qedf)
1661d8658bSDupuis, Chad {
1761d8658bSDupuis, Chad struct sk_buff *skb;
1861d8658bSDupuis, Chad char *eth_fr;
1961d8658bSDupuis, Chad struct fip_vlan *vlan;
2061d8658bSDupuis, Chad #define MY_FIP_ALL_FCF_MACS ((__u8[6]) { 1, 0x10, 0x18, 1, 0, 2 })
2161d8658bSDupuis, Chad static u8 my_fcoe_all_fcfs[ETH_ALEN] = MY_FIP_ALL_FCF_MACS;
22cac6f691SSudarsana Reddy Kalluru unsigned long flags = 0;
23*89523cb8SColin Ian King int rc;
2461d8658bSDupuis, Chad
2561d8658bSDupuis, Chad skb = dev_alloc_skb(sizeof(struct fip_vlan));
26e82e6ff7SSaurav Kashyap if (!skb) {
27e82e6ff7SSaurav Kashyap QEDF_ERR(&qedf->dbg_ctx,
28e82e6ff7SSaurav Kashyap "Failed to allocate skb.\n");
2961d8658bSDupuis, Chad return;
30e82e6ff7SSaurav Kashyap }
3161d8658bSDupuis, Chad
3261d8658bSDupuis, Chad eth_fr = (char *)skb->data;
3361d8658bSDupuis, Chad vlan = (struct fip_vlan *)eth_fr;
3461d8658bSDupuis, Chad
3561d8658bSDupuis, Chad memset(vlan, 0, sizeof(*vlan));
3661d8658bSDupuis, Chad ether_addr_copy(vlan->eth.h_source, qedf->mac);
3761d8658bSDupuis, Chad ether_addr_copy(vlan->eth.h_dest, my_fcoe_all_fcfs);
3861d8658bSDupuis, Chad vlan->eth.h_proto = htons(ETH_P_FIP);
3961d8658bSDupuis, Chad
4061d8658bSDupuis, Chad vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
4161d8658bSDupuis, Chad vlan->fip.fip_op = htons(FIP_OP_VLAN);
4261d8658bSDupuis, Chad vlan->fip.fip_subcode = FIP_SC_VL_REQ;
4361d8658bSDupuis, Chad vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
4461d8658bSDupuis, Chad
4561d8658bSDupuis, Chad vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
4661d8658bSDupuis, Chad vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
4761d8658bSDupuis, Chad ether_addr_copy(vlan->desc.mac.fd_mac, qedf->mac);
4861d8658bSDupuis, Chad
4961d8658bSDupuis, Chad vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
5061d8658bSDupuis, Chad vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
5161d8658bSDupuis, Chad put_unaligned_be64(qedf->lport->wwnn, &vlan->desc.wwnn.fd_wwn);
5261d8658bSDupuis, Chad
5361d8658bSDupuis, Chad skb_put(skb, sizeof(*vlan));
5461d8658bSDupuis, Chad skb->protocol = htons(ETH_P_FIP);
5561d8658bSDupuis, Chad skb_reset_mac_header(skb);
5661d8658bSDupuis, Chad skb_reset_network_header(skb);
5761d8658bSDupuis, Chad
5861d8658bSDupuis, Chad QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Sending FIP VLAN "
5961d8658bSDupuis, Chad "request.");
6061d8658bSDupuis, Chad
6161d8658bSDupuis, Chad if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
6261d8658bSDupuis, Chad QEDF_WARN(&(qedf->dbg_ctx), "Cannot send vlan request "
6361d8658bSDupuis, Chad "because link is not up.\n");
6461d8658bSDupuis, Chad
6561d8658bSDupuis, Chad kfree_skb(skb);
6661d8658bSDupuis, Chad return;
6761d8658bSDupuis, Chad }
68cac6f691SSudarsana Reddy Kalluru
69cac6f691SSudarsana Reddy Kalluru set_bit(QED_LL2_XMIT_FLAGS_FIP_DISCOVERY, &flags);
7019c2b6d8SSaurav Kashyap rc = qed_ops->ll2->start_xmit(qedf->cdev, skb, flags);
7119c2b6d8SSaurav Kashyap if (rc) {
7219c2b6d8SSaurav Kashyap QEDF_ERR(&qedf->dbg_ctx, "start_xmit failed rc = %d.\n", rc);
7319c2b6d8SSaurav Kashyap kfree_skb(skb);
7419c2b6d8SSaurav Kashyap return;
7519c2b6d8SSaurav Kashyap }
7619c2b6d8SSaurav Kashyap
7761d8658bSDupuis, Chad }
7861d8658bSDupuis, Chad
qedf_fcoe_process_vlan_resp(struct qedf_ctx * qedf,struct sk_buff * skb)7961d8658bSDupuis, Chad static void qedf_fcoe_process_vlan_resp(struct qedf_ctx *qedf,
8061d8658bSDupuis, Chad struct sk_buff *skb)
8161d8658bSDupuis, Chad {
8261d8658bSDupuis, Chad struct fip_header *fiph;
8361d8658bSDupuis, Chad struct fip_desc *desc;
8461d8658bSDupuis, Chad u16 vid = 0;
8561d8658bSDupuis, Chad ssize_t rlen;
8661d8658bSDupuis, Chad size_t dlen;
8761d8658bSDupuis, Chad
8861d8658bSDupuis, Chad fiph = (struct fip_header *)(((void *)skb->data) + 2 * ETH_ALEN + 2);
8961d8658bSDupuis, Chad
9061d8658bSDupuis, Chad rlen = ntohs(fiph->fip_dl_len) * 4;
9161d8658bSDupuis, Chad desc = (struct fip_desc *)(fiph + 1);
9261d8658bSDupuis, Chad while (rlen > 0) {
9361d8658bSDupuis, Chad dlen = desc->fip_dlen * FIP_BPW;
9461d8658bSDupuis, Chad switch (desc->fip_dtype) {
9561d8658bSDupuis, Chad case FIP_DT_VLAN:
9661d8658bSDupuis, Chad vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
9761d8658bSDupuis, Chad break;
9861d8658bSDupuis, Chad }
9961d8658bSDupuis, Chad desc = (struct fip_desc *)((char *)desc + dlen);
10061d8658bSDupuis, Chad rlen -= dlen;
10161d8658bSDupuis, Chad }
10261d8658bSDupuis, Chad
10376dbf4ffSSaurav Kashyap if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
10476dbf4ffSSaurav Kashyap QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
10576dbf4ffSSaurav Kashyap "Dropping VLAN response as link is down.\n");
10676dbf4ffSSaurav Kashyap return;
10776dbf4ffSSaurav Kashyap }
10876dbf4ffSSaurav Kashyap
10961d8658bSDupuis, Chad QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "VLAN response, "
11061d8658bSDupuis, Chad "vid=0x%x.\n", vid);
11161d8658bSDupuis, Chad
11261d8658bSDupuis, Chad if (vid > 0 && qedf->vlan_id != vid) {
11361d8658bSDupuis, Chad qedf_set_vlan_id(qedf, vid);
11461d8658bSDupuis, Chad
11561d8658bSDupuis, Chad /* Inform waiter that it's ok to call fcoe_ctlr_link up() */
1168eaf7dfcSChad Dupuis if (!completion_done(&qedf->fipvlan_compl))
11761d8658bSDupuis, Chad complete(&qedf->fipvlan_compl);
11861d8658bSDupuis, Chad }
11961d8658bSDupuis, Chad }
12061d8658bSDupuis, Chad
qedf_fip_send(struct fcoe_ctlr * fip,struct sk_buff * skb)12161d8658bSDupuis, Chad void qedf_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
12261d8658bSDupuis, Chad {
12361d8658bSDupuis, Chad struct qedf_ctx *qedf = container_of(fip, struct qedf_ctx, ctlr);
12461d8658bSDupuis, Chad struct ethhdr *eth_hdr;
12561d8658bSDupuis, Chad struct fip_header *fiph;
12661d8658bSDupuis, Chad u16 op, vlan_tci = 0;
12761d8658bSDupuis, Chad u8 sub;
12819c2b6d8SSaurav Kashyap int rc = -1;
12961d8658bSDupuis, Chad
13061d8658bSDupuis, Chad if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
13161d8658bSDupuis, Chad QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
13261d8658bSDupuis, Chad kfree_skb(skb);
13361d8658bSDupuis, Chad return;
13461d8658bSDupuis, Chad }
13561d8658bSDupuis, Chad
13661d8658bSDupuis, Chad fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
13761d8658bSDupuis, Chad eth_hdr = (struct ethhdr *)skb_mac_header(skb);
13861d8658bSDupuis, Chad op = ntohs(fiph->fip_op);
13961d8658bSDupuis, Chad sub = fiph->fip_subcode;
14061d8658bSDupuis, Chad
141cf291163SChad Dupuis /*
142cf291163SChad Dupuis * Add VLAN tag to non-offload FIP frame based on current stored VLAN
143cf291163SChad Dupuis * for FIP/FCoE traffic.
144cf291163SChad Dupuis */
145cf291163SChad Dupuis __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
14661d8658bSDupuis, Chad
147cf291163SChad Dupuis /* Get VLAN ID from skb for printing purposes */
148cf291163SChad Dupuis __vlan_hwaccel_get_tag(skb, &vlan_tci);
14961d8658bSDupuis, Chad
15061d8658bSDupuis, Chad QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FIP frame send: "
15161d8658bSDupuis, Chad "dest=%pM op=%x sub=%x vlan=%04x.", eth_hdr->h_dest, op, sub,
15215a93de7SChad Dupuis vlan_tci);
15361d8658bSDupuis, Chad if (qedf_dump_frames)
15461d8658bSDupuis, Chad print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
15561d8658bSDupuis, Chad skb->data, skb->len, false);
15661d8658bSDupuis, Chad
15719c2b6d8SSaurav Kashyap rc = qed_ops->ll2->start_xmit(qedf->cdev, skb, 0);
15819c2b6d8SSaurav Kashyap if (rc) {
15919c2b6d8SSaurav Kashyap QEDF_ERR(&qedf->dbg_ctx, "start_xmit failed rc = %d.\n", rc);
16019c2b6d8SSaurav Kashyap kfree_skb(skb);
16119c2b6d8SSaurav Kashyap return;
16219c2b6d8SSaurav Kashyap }
16361d8658bSDupuis, Chad }
16461d8658bSDupuis, Chad
165fc37ddceSChad Dupuis static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
166fc37ddceSChad Dupuis
16761d8658bSDupuis, Chad /* Process incoming FIP frames. */
qedf_fip_recv(struct qedf_ctx * qedf,struct sk_buff * skb)16861d8658bSDupuis, Chad void qedf_fip_recv(struct qedf_ctx *qedf, struct sk_buff *skb)
16961d8658bSDupuis, Chad {
17061d8658bSDupuis, Chad struct ethhdr *eth_hdr;
17161d8658bSDupuis, Chad struct fip_header *fiph;
17261d8658bSDupuis, Chad struct fip_desc *desc;
17361d8658bSDupuis, Chad struct fip_mac_desc *mp;
17461d8658bSDupuis, Chad struct fip_wwn_desc *wp;
17561d8658bSDupuis, Chad struct fip_vn_desc *vp;
17661d8658bSDupuis, Chad size_t rlen, dlen;
17761d8658bSDupuis, Chad u16 op;
17861d8658bSDupuis, Chad u8 sub;
179fc37ddceSChad Dupuis bool fcf_valid = false;
180fc37ddceSChad Dupuis /* Default is to handle CVL regardless of fabric id descriptor */
181fc37ddceSChad Dupuis bool fabric_id_valid = true;
182fc37ddceSChad Dupuis bool fc_wwpn_valid = false;
183fc37ddceSChad Dupuis u64 switch_name;
184fc37ddceSChad Dupuis u16 vlan = 0;
18561d8658bSDupuis, Chad
18661d8658bSDupuis, Chad eth_hdr = (struct ethhdr *)skb_mac_header(skb);
18761d8658bSDupuis, Chad fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
18861d8658bSDupuis, Chad op = ntohs(fiph->fip_op);
18961d8658bSDupuis, Chad sub = fiph->fip_subcode;
19061d8658bSDupuis, Chad
191fc37ddceSChad Dupuis QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
192fc37ddceSChad Dupuis "FIP frame received: skb=%p fiph=%p source=%pM destn=%pM op=%x sub=%x vlan=%04x",
193fc37ddceSChad Dupuis skb, fiph, eth_hdr->h_source, eth_hdr->h_dest, op,
194fc37ddceSChad Dupuis sub, vlan);
19561d8658bSDupuis, Chad if (qedf_dump_frames)
19661d8658bSDupuis, Chad print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
19761d8658bSDupuis, Chad skb->data, skb->len, false);
19861d8658bSDupuis, Chad
199fc37ddceSChad Dupuis if (!ether_addr_equal(eth_hdr->h_dest, qedf->mac) &&
200fc37ddceSChad Dupuis !ether_addr_equal(eth_hdr->h_dest, fcoe_all_enode) &&
201fc37ddceSChad Dupuis !ether_addr_equal(eth_hdr->h_dest, qedf->data_src_addr)) {
202fc37ddceSChad Dupuis QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
203fc37ddceSChad Dupuis "Dropping FIP type 0x%x pkt due to destination MAC mismatch dest_mac=%pM ctlr.dest_addr=%pM data_src_addr=%pM.\n",
204fc37ddceSChad Dupuis op, eth_hdr->h_dest, qedf->mac,
205fc37ddceSChad Dupuis qedf->data_src_addr);
206fc37ddceSChad Dupuis kfree_skb(skb);
207fc37ddceSChad Dupuis return;
208fc37ddceSChad Dupuis }
209fc37ddceSChad Dupuis
21061d8658bSDupuis, Chad /* Handle FIP VLAN resp in the driver */
21161d8658bSDupuis, Chad if (op == FIP_OP_VLAN && sub == FIP_SC_VL_NOTE) {
21261d8658bSDupuis, Chad qedf_fcoe_process_vlan_resp(qedf, skb);
21361d8658bSDupuis, Chad kfree_skb(skb);
21461d8658bSDupuis, Chad } else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
21561d8658bSDupuis, Chad QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Clear virtual "
21661d8658bSDupuis, Chad "link received.\n");
21761d8658bSDupuis, Chad
21861d8658bSDupuis, Chad /* Check that an FCF has been selected by fcoe */
21961d8658bSDupuis, Chad if (qedf->ctlr.sel_fcf == NULL) {
22061d8658bSDupuis, Chad QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
22161d8658bSDupuis, Chad "Dropping CVL since FCF has not been selected "
22261d8658bSDupuis, Chad "yet.");
223f32803bbSChad Dupuis kfree_skb(skb);
22461d8658bSDupuis, Chad return;
22561d8658bSDupuis, Chad }
22661d8658bSDupuis, Chad
22761d8658bSDupuis, Chad /*
22861d8658bSDupuis, Chad * We need to loop through the CVL descriptors to determine
22961d8658bSDupuis, Chad * if we want to reset the fcoe link
23061d8658bSDupuis, Chad */
23161d8658bSDupuis, Chad rlen = ntohs(fiph->fip_dl_len) * FIP_BPW;
23261d8658bSDupuis, Chad desc = (struct fip_desc *)(fiph + 1);
23361d8658bSDupuis, Chad while (rlen >= sizeof(*desc)) {
23461d8658bSDupuis, Chad dlen = desc->fip_dlen * FIP_BPW;
23561d8658bSDupuis, Chad switch (desc->fip_dtype) {
23661d8658bSDupuis, Chad case FIP_DT_MAC:
23761d8658bSDupuis, Chad mp = (struct fip_mac_desc *)desc;
238fc37ddceSChad Dupuis QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
239fc37ddceSChad Dupuis "Switch fd_mac=%pM.\n", mp->fd_mac);
24053c51adbSChad Dupuis if (ether_addr_equal(mp->fd_mac,
24153c51adbSChad Dupuis qedf->ctlr.sel_fcf->fcf_mac))
242fc37ddceSChad Dupuis fcf_valid = true;
24361d8658bSDupuis, Chad break;
24461d8658bSDupuis, Chad case FIP_DT_NAME:
24561d8658bSDupuis, Chad wp = (struct fip_wwn_desc *)desc;
246fc37ddceSChad Dupuis switch_name = get_unaligned_be64(&wp->fd_wwn);
247fc37ddceSChad Dupuis QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
248fc37ddceSChad Dupuis "Switch fd_wwn=%016llx fcf_switch_name=%016llx.\n",
249fc37ddceSChad Dupuis switch_name,
250fc37ddceSChad Dupuis qedf->ctlr.sel_fcf->switch_name);
251fc37ddceSChad Dupuis if (switch_name ==
252fc37ddceSChad Dupuis qedf->ctlr.sel_fcf->switch_name)
253fc37ddceSChad Dupuis fc_wwpn_valid = true;
25461d8658bSDupuis, Chad break;
25561d8658bSDupuis, Chad case FIP_DT_VN_ID:
256b29a907fSSaurav Kashyap fabric_id_valid = false;
25761d8658bSDupuis, Chad vp = (struct fip_vn_desc *)desc;
258b29a907fSSaurav Kashyap
259b29a907fSSaurav Kashyap QEDF_ERR(&qedf->dbg_ctx,
260b29a907fSSaurav Kashyap "CVL vx_port fd_fc_id=0x%x fd_mac=%pM fd_wwpn=%016llx.\n",
261b29a907fSSaurav Kashyap ntoh24(vp->fd_fc_id), vp->fd_mac,
262b29a907fSSaurav Kashyap get_unaligned_be64(&vp->fd_wwpn));
263b29a907fSSaurav Kashyap /* Check for vx_port wwpn OR Check vx_port
264b29a907fSSaurav Kashyap * fabric ID OR Check vx_port MAC
265b29a907fSSaurav Kashyap */
266b29a907fSSaurav Kashyap if ((get_unaligned_be64(&vp->fd_wwpn) ==
267b29a907fSSaurav Kashyap qedf->wwpn) ||
268b29a907fSSaurav Kashyap (ntoh24(vp->fd_fc_id) ==
269b29a907fSSaurav Kashyap qedf->lport->port_id) ||
270b29a907fSSaurav Kashyap (ether_addr_equal(vp->fd_mac,
271b29a907fSSaurav Kashyap qedf->data_src_addr))) {
272b29a907fSSaurav Kashyap fabric_id_valid = true;
273b29a907fSSaurav Kashyap }
27461d8658bSDupuis, Chad break;
27561d8658bSDupuis, Chad default:
27661d8658bSDupuis, Chad /* Ignore anything else */
27761d8658bSDupuis, Chad break;
27861d8658bSDupuis, Chad }
27961d8658bSDupuis, Chad desc = (struct fip_desc *)((char *)desc + dlen);
28061d8658bSDupuis, Chad rlen -= dlen;
28161d8658bSDupuis, Chad }
28261d8658bSDupuis, Chad
283fc37ddceSChad Dupuis QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
284fc37ddceSChad Dupuis "fcf_valid=%d fabric_id_valid=%d fc_wwpn_valid=%d.\n",
285fc37ddceSChad Dupuis fcf_valid, fabric_id_valid, fc_wwpn_valid);
286fc37ddceSChad Dupuis if (fcf_valid && fabric_id_valid && fc_wwpn_valid)
287a66c6cd2SChad Dupuis qedf_ctx_soft_reset(qedf->lport);
28861d8658bSDupuis, Chad kfree_skb(skb);
28961d8658bSDupuis, Chad } else {
29061d8658bSDupuis, Chad /* Everything else is handled by libfcoe */
29161d8658bSDupuis, Chad __skb_pull(skb, ETH_HLEN);
29261d8658bSDupuis, Chad fcoe_ctlr_recv(&qedf->ctlr, skb);
29361d8658bSDupuis, Chad }
29461d8658bSDupuis, Chad }
29561d8658bSDupuis, Chad
qedf_get_src_mac(struct fc_lport * lport)29661d8658bSDupuis, Chad u8 *qedf_get_src_mac(struct fc_lport *lport)
29761d8658bSDupuis, Chad {
29861d8658bSDupuis, Chad struct qedf_ctx *qedf = lport_priv(lport);
29961d8658bSDupuis, Chad
30061d8658bSDupuis, Chad return qedf->data_src_addr;
30161d8658bSDupuis, Chad }
302