Linux-libre 4.14.82-gnu
[librecmc/linux-libre.git] / drivers / scsi / qedf / qedf_fip.c
1 /*
2  *  QLogic FCoE Offload Driver
3  *  Copyright (c) 2016-2017 Cavium Inc.
4  *
5  *  This software is available under the terms of the GNU General Public License
6  *  (GPL) Version 2, available from the file COPYING in the main directory of
7  *  this source tree.
8  */
9 #include <linux/if_ether.h>
10 #include <linux/if_vlan.h>
11 #include "qedf.h"
12
13 extern const struct qed_fcoe_ops *qed_ops;
14 /*
15  * FIP VLAN functions that will eventually move to libfcoe.
16  */
17
18 void qedf_fcoe_send_vlan_req(struct qedf_ctx *qedf)
19 {
20         struct sk_buff *skb;
21         char *eth_fr;
22         int fr_len;
23         struct fip_vlan *vlan;
24 #define MY_FIP_ALL_FCF_MACS        ((__u8[6]) { 1, 0x10, 0x18, 1, 0, 2 })
25         static u8 my_fcoe_all_fcfs[ETH_ALEN] = MY_FIP_ALL_FCF_MACS;
26
27         skb = dev_alloc_skb(sizeof(struct fip_vlan));
28         if (!skb)
29                 return;
30
31         fr_len = sizeof(*vlan);
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         qed_ops->ll2->start_xmit(qedf->cdev, skb);
69 }
70
71 static void qedf_fcoe_process_vlan_resp(struct qedf_ctx *qedf,
72         struct sk_buff *skb)
73 {
74         struct fip_header *fiph;
75         struct fip_desc *desc;
76         u16 vid = 0;
77         ssize_t rlen;
78         size_t dlen;
79
80         fiph = (struct fip_header *)(((void *)skb->data) + 2 * ETH_ALEN + 2);
81
82         rlen = ntohs(fiph->fip_dl_len) * 4;
83         desc = (struct fip_desc *)(fiph + 1);
84         while (rlen > 0) {
85                 dlen = desc->fip_dlen * FIP_BPW;
86                 switch (desc->fip_dtype) {
87                 case FIP_DT_VLAN:
88                         vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
89                         break;
90                 }
91                 desc = (struct fip_desc *)((char *)desc + dlen);
92                 rlen -= dlen;
93         }
94
95         QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "VLAN response, "
96                    "vid=0x%x.\n", vid);
97
98         if (vid > 0 && qedf->vlan_id != vid) {
99                 qedf_set_vlan_id(qedf, vid);
100
101                 /* Inform waiter that it's ok to call fcoe_ctlr_link up() */
102                 if (!completion_done(&qedf->fipvlan_compl))
103                         complete(&qedf->fipvlan_compl);
104         }
105 }
106
107 void qedf_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
108 {
109         struct qedf_ctx *qedf = container_of(fip, struct qedf_ctx, ctlr);
110         struct ethhdr *eth_hdr;
111         struct fip_header *fiph;
112         u16 op, vlan_tci = 0;
113         u8 sub;
114
115         if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
116                 QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
117                 kfree_skb(skb);
118                 return;
119         }
120
121         fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
122         eth_hdr = (struct ethhdr *)skb_mac_header(skb);
123         op = ntohs(fiph->fip_op);
124         sub = fiph->fip_subcode;
125
126         /*
127          * Add VLAN tag to non-offload FIP frame based on current stored VLAN
128          * for FIP/FCoE traffic.
129          */
130         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
131
132         /* Get VLAN ID from skb for printing purposes */
133         __vlan_hwaccel_get_tag(skb, &vlan_tci);
134
135         QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FIP frame send: "
136             "dest=%pM op=%x sub=%x vlan=%04x.", eth_hdr->h_dest, op, sub,
137             ntohs(vlan_tci));
138         if (qedf_dump_frames)
139                 print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
140                     skb->data, skb->len, false);
141
142         qed_ops->ll2->start_xmit(qedf->cdev, skb);
143 }
144
145 /* Process incoming FIP frames. */
146 void qedf_fip_recv(struct qedf_ctx *qedf, struct sk_buff *skb)
147 {
148         struct ethhdr *eth_hdr;
149         struct fip_header *fiph;
150         struct fip_desc *desc;
151         struct fip_mac_desc *mp;
152         struct fip_wwn_desc *wp;
153         struct fip_vn_desc *vp;
154         size_t rlen, dlen;
155         u16 op;
156         u8 sub;
157         bool do_reset = false;
158
159         eth_hdr = (struct ethhdr *)skb_mac_header(skb);
160         fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
161         op = ntohs(fiph->fip_op);
162         sub = fiph->fip_subcode;
163
164         QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FIP frame received: "
165             "skb=%p fiph=%p source=%pM op=%x sub=%x", skb, fiph,
166             eth_hdr->h_source, op, sub);
167         if (qedf_dump_frames)
168                 print_hex_dump(KERN_WARNING, "fip ", DUMP_PREFIX_OFFSET, 16, 1,
169                     skb->data, skb->len, false);
170
171         /* Handle FIP VLAN resp in the driver */
172         if (op == FIP_OP_VLAN && sub == FIP_SC_VL_NOTE) {
173                 qedf_fcoe_process_vlan_resp(qedf, skb);
174                 kfree_skb(skb);
175         } else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
176                 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Clear virtual "
177                            "link received.\n");
178
179                 /* Check that an FCF has been selected by fcoe */
180                 if (qedf->ctlr.sel_fcf == NULL) {
181                         QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
182                             "Dropping CVL since FCF has not been selected "
183                             "yet.");
184                         return;
185                 }
186
187                 /*
188                  * We need to loop through the CVL descriptors to determine
189                  * if we want to reset the fcoe link
190                  */
191                 rlen = ntohs(fiph->fip_dl_len) * FIP_BPW;
192                 desc = (struct fip_desc *)(fiph + 1);
193                 while (rlen >= sizeof(*desc)) {
194                         dlen = desc->fip_dlen * FIP_BPW;
195                         switch (desc->fip_dtype) {
196                         case FIP_DT_MAC:
197                                 mp = (struct fip_mac_desc *)desc;
198                                 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
199                                     "fd_mac=%pM\n", mp->fd_mac);
200                                 if (ether_addr_equal(mp->fd_mac,
201                                     qedf->ctlr.sel_fcf->fcf_mac))
202                                         do_reset = true;
203                                 break;
204                         case FIP_DT_NAME:
205                                 wp = (struct fip_wwn_desc *)desc;
206                                 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
207                                     "fc_wwpn=%016llx.\n",
208                                     get_unaligned_be64(&wp->fd_wwn));
209                                 break;
210                         case FIP_DT_VN_ID:
211                                 vp = (struct fip_vn_desc *)desc;
212                                 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
213                                     "fd_fc_id=%x.\n", ntoh24(vp->fd_fc_id));
214                                 if (ntoh24(vp->fd_fc_id) ==
215                                     qedf->lport->port_id)
216                                         do_reset = true;
217                                 break;
218                         default:
219                                 /* Ignore anything else */
220                                 break;
221                         }
222                         desc = (struct fip_desc *)((char *)desc + dlen);
223                         rlen -= dlen;
224                 }
225
226                 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
227                     "do_reset=%d.\n", do_reset);
228                 if (do_reset) {
229                         fcoe_ctlr_link_down(&qedf->ctlr);
230                         qedf_wait_for_upload(qedf);
231                         fcoe_ctlr_link_up(&qedf->ctlr);
232                 }
233                 kfree_skb(skb);
234         } else {
235                 /* Everything else is handled by libfcoe */
236                 __skb_pull(skb, ETH_HLEN);
237                 fcoe_ctlr_recv(&qedf->ctlr, skb);
238         }
239 }
240
241 u8 *qedf_get_src_mac(struct fc_lport *lport)
242 {
243         struct qedf_ctx *qedf = lport_priv(lport);
244
245         return qedf->data_src_addr;
246 }