[SCSI] libfc: Make the libfc Common Transport(CT) code generic
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / include / scsi / fc_encode.h
CommitLineData
42e9a92f
RL
1/*
2 * Copyright(c) 2008 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#ifndef _FC_ENCODE_H_
21#define _FC_ENCODE_H_
22#include <asm/unaligned.h>
23
24f089e2
JE
24/*
25 * F_CTL values for simple requests and responses.
26 */
27#define FC_FCTL_REQ (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)
28#define FC_FCTL_RESP (FC_FC_EX_CTX | FC_FC_LAST_SEQ | \
29 FC_FC_END_SEQ | FC_FC_SEQ_INIT)
30
42e9a92f
RL
31struct fc_ns_rft {
32 struct fc_ns_fid fid; /* port ID object */
33 struct fc_ns_fts fts; /* FC4-types object */
34};
35
36struct fc_ct_req {
37 struct fc_ct_hdr hdr;
38 union {
39 struct fc_ns_gid_ft gid;
40 struct fc_ns_rn_id rn;
41 struct fc_ns_rft rft;
ab593b18 42 struct fc_ns_rff_id rff;
2ab7e1ec 43 struct fc_ns_fid fid;
5baa17c3 44 struct fc_ns_rsnn snn;
c9866a54 45 struct fc_ns_rspn spn;
42e9a92f
RL
46 } payload;
47};
48
059f04d4
BPG
49static inline void __fc_fill_fc_hdr(struct fc_frame_header *fh,
50 enum fc_rctl r_ctl,
51 u32 did, u32 sid, enum fc_fh_type type,
52 u32 f_ctl, u32 parm_offset)
42e9a92f 53{
42e9a92f
RL
54 WARN_ON(r_ctl == 0);
55 fh->fh_r_ctl = r_ctl;
56 hton24(fh->fh_d_id, did);
57 hton24(fh->fh_s_id, sid);
58 fh->fh_type = type;
59 hton24(fh->fh_f_ctl, f_ctl);
60 fh->fh_cs_ctl = 0;
61 fh->fh_df_ctl = 0;
62 fh->fh_parm_offset = htonl(parm_offset);
63}
64
059f04d4
BPG
65/**
66 * fill FC header fields in specified fc_frame
67 */
68static inline void fc_fill_fc_hdr(struct fc_frame *fp, enum fc_rctl r_ctl,
69 u32 did, u32 sid, enum fc_fh_type type,
70 u32 f_ctl, u32 parm_offset)
71{
72 struct fc_frame_header *fh;
73
74 fh = fc_frame_header_get(fp);
75 __fc_fill_fc_hdr(fh, r_ctl, did, sid, type, f_ctl, parm_offset);
76}
77
370c3bd0
JE
78/**
79 * fc_adisc_fill() - Fill in adisc request frame
80 * @lport: local port.
81 * @fp: fc frame where payload will be placed.
82 */
83static inline void fc_adisc_fill(struct fc_lport *lport, struct fc_frame *fp)
84{
85 struct fc_els_adisc *adisc;
86
87 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
88 memset(adisc, 0, sizeof(*adisc));
89 adisc->adisc_cmd = ELS_ADISC;
90 put_unaligned_be64(lport->wwpn, &adisc->adisc_wwpn);
91 put_unaligned_be64(lport->wwnn, &adisc->adisc_wwnn);
7b2787ec 92 hton24(adisc->adisc_port_id, lport->port_id);
370c3bd0
JE
93}
94
42e9a92f
RL
95/**
96 * fc_ct_hdr_fill- fills ct header and reset ct payload
97 * returns pointer to ct request.
98 */
99static inline struct fc_ct_req *fc_ct_hdr_fill(const struct fc_frame *fp,
1ea2c1da
NP
100 unsigned int op, size_t req_size,
101 enum fc_ct_fs_type fs_type,
102 u8 subtype)
42e9a92f
RL
103{
104 struct fc_ct_req *ct;
105 size_t ct_plen;
106
107 ct_plen = sizeof(struct fc_ct_hdr) + req_size;
108 ct = fc_frame_payload_get(fp, ct_plen);
109 memset(ct, 0, ct_plen);
110 ct->hdr.ct_rev = FC_CT_REV;
1ea2c1da
NP
111 ct->hdr.ct_fs_type = fs_type;
112 ct->hdr.ct_fs_subtype = subtype;
42e9a92f
RL
113 ct->hdr.ct_cmd = htons((u16) op);
114 return ct;
115}
116
117/**
1ea2c1da 118 * fc_ct_ns_fill() - Fill in a name service request frame
2ab7e1ec
JE
119 * @lport: local port.
120 * @fc_id: FC_ID of non-destination rport for GPN_ID and similar inquiries.
121 * @fp: frame to contain payload.
122 * @op: CT opcode.
123 * @r_ctl: pointer to FC header R_CTL.
124 * @fh_type: pointer to FC-4 type.
42e9a92f 125 */
1ea2c1da 126static inline int fc_ct_ns_fill(struct fc_lport *lport,
2ab7e1ec 127 u32 fc_id, struct fc_frame *fp,
a46f327a 128 unsigned int op, enum fc_rctl *r_ctl,
42e9a92f
RL
129 enum fc_fh_type *fh_type)
130{
131 struct fc_ct_req *ct;
5f9a056d 132 size_t len;
42e9a92f
RL
133
134 switch (op) {
135 case FC_NS_GPN_FT:
1ea2c1da
NP
136 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_gid_ft),
137 FC_FST_DIR, FC_NS_SUBTYPE);
42e9a92f
RL
138 ct->payload.gid.fn_fc4_type = FC_TYPE_FCP;
139 break;
140
2ab7e1ec 141 case FC_NS_GPN_ID:
1ea2c1da
NP
142 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_fid),
143 FC_FST_DIR, FC_NS_SUBTYPE);
144 ct->payload.gid.fn_fc4_type = FC_TYPE_FCP;
2ab7e1ec
JE
145 hton24(ct->payload.fid.fp_fid, fc_id);
146 break;
147
42e9a92f 148 case FC_NS_RFT_ID:
1ea2c1da
NP
149 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rft),
150 FC_FST_DIR, FC_NS_SUBTYPE);
7b2787ec 151 hton24(ct->payload.rft.fid.fp_fid, lport->port_id);
42e9a92f
RL
152 ct->payload.rft.fts = lport->fcts;
153 break;
154
ab593b18 155 case FC_NS_RFF_ID:
1ea2c1da
NP
156 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rff_id),
157 FC_FST_DIR, FC_NS_SUBTYPE);
7b2787ec 158 hton24(ct->payload.rff.fr_fid.fp_fid, lport->port_id);
ab593b18
JE
159 ct->payload.rff.fr_type = FC_TYPE_FCP;
160 if (lport->service_params & FCP_SPPF_INIT_FCN)
161 ct->payload.rff.fr_feat = FCP_FEAT_INIT;
162 if (lport->service_params & FCP_SPPF_TARG_FCN)
163 ct->payload.rff.fr_feat |= FCP_FEAT_TARG;
164 break;
165
c9c7bd7a 166 case FC_NS_RNN_ID:
1ea2c1da
NP
167 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rn_id),
168 FC_FST_DIR, FC_NS_SUBTYPE);
7b2787ec 169 hton24(ct->payload.rn.fr_fid.fp_fid, lport->port_id);
c9c7bd7a 170 put_unaligned_be64(lport->wwnn, &ct->payload.rn.fr_wwn);
42e9a92f
RL
171 break;
172
c9866a54 173 case FC_NS_RSPN_ID:
5f9a056d 174 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1ea2c1da
NP
175 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len,
176 FC_FST_DIR, FC_NS_SUBTYPE);
7b2787ec 177 hton24(ct->payload.spn.fr_fid.fp_fid, lport->port_id);
c9866a54 178 strncpy(ct->payload.spn.fr_name,
5f9a056d
JE
179 fc_host_symbolic_name(lport->host), len);
180 ct->payload.spn.fr_name_len = len;
c9866a54
CL
181 break;
182
5baa17c3 183 case FC_NS_RSNN_NN:
5f9a056d 184 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1ea2c1da
NP
185 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rsnn) + len,
186 FC_FST_DIR, FC_NS_SUBTYPE);
5baa17c3
CL
187 put_unaligned_be64(lport->wwnn, &ct->payload.snn.fr_wwn);
188 strncpy(ct->payload.snn.fr_name,
5f9a056d
JE
189 fc_host_symbolic_name(lport->host), len);
190 ct->payload.snn.fr_name_len = len;
5baa17c3
CL
191 break;
192
42e9a92f 193 default:
42e9a92f
RL
194 return -EINVAL;
195 }
196 *r_ctl = FC_RCTL_DD_UNSOL_CTL;
42e9a92f
RL
197 *fh_type = FC_TYPE_CT;
198 return 0;
199}
200
1ea2c1da
NP
201/**
202 * fc_ct_fill() - Fill in a common transport service request frame
203 * @lport: local port.
204 * @fc_id: FC_ID of non-destination rport for GPN_ID and similar inquiries.
205 * @fp: frame to contain payload.
206 * @op: CT opcode.
207 * @r_ctl: pointer to FC header R_CTL.
208 * @fh_type: pointer to FC-4 type.
209 */
210static inline int fc_ct_fill(struct fc_lport *lport,
211 u32 fc_id, struct fc_frame *fp,
212 unsigned int op, enum fc_rctl *r_ctl,
213 enum fc_fh_type *fh_type, u32 *did)
214{
215 int rc = -EINVAL;
216
217 switch (fc_id) {
218 case FC_FID_DIR_SERV:
219 default:
220 rc = fc_ct_ns_fill(lport, fc_id, fp, op, r_ctl, fh_type);
221 *did = FC_FID_DIR_SERV;
222 break;
223 }
224
225 return rc;
226}
42e9a92f
RL
227/**
228 * fc_plogi_fill - Fill in plogi request frame
229 */
230static inline void fc_plogi_fill(struct fc_lport *lport, struct fc_frame *fp,
231 unsigned int op)
232{
233 struct fc_els_flogi *plogi;
234 struct fc_els_csp *csp;
235 struct fc_els_cssp *cp;
236
237 plogi = fc_frame_payload_get(fp, sizeof(*plogi));
238 memset(plogi, 0, sizeof(*plogi));
239 plogi->fl_cmd = (u8) op;
240 put_unaligned_be64(lport->wwpn, &plogi->fl_wwpn);
241 put_unaligned_be64(lport->wwnn, &plogi->fl_wwnn);
242
243 csp = &plogi->fl_csp;
244 csp->sp_hi_ver = 0x20;
245 csp->sp_lo_ver = 0x20;
246 csp->sp_bb_cred = htons(10); /* this gets set by gateway */
247 csp->sp_bb_data = htons((u16) lport->mfs);
248 cp = &plogi->fl_cssp[3 - 1]; /* class 3 parameters */
249 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
250 csp->sp_features = htons(FC_SP_FT_CIRO);
251 csp->sp_tot_seq = htons(255); /* seq. we accept */
252 csp->sp_rel_off = htons(0x1f);
253 csp->sp_e_d_tov = htonl(lport->e_d_tov);
254
255 cp->cp_rdfs = htons((u16) lport->mfs);
256 cp->cp_con_seq = htons(255);
257 cp->cp_open_seq = 1;
258}
259
260/**
261 * fc_flogi_fill - Fill in a flogi request frame.
262 */
263static inline void fc_flogi_fill(struct fc_lport *lport, struct fc_frame *fp)
264{
265 struct fc_els_csp *sp;
266 struct fc_els_cssp *cp;
267 struct fc_els_flogi *flogi;
268
269 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
270 memset(flogi, 0, sizeof(*flogi));
271 flogi->fl_cmd = (u8) ELS_FLOGI;
272 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
273 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
274 sp = &flogi->fl_csp;
275 sp->sp_hi_ver = 0x20;
276 sp->sp_lo_ver = 0x20;
277 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
278 sp->sp_bb_data = htons((u16) lport->mfs);
279 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
280 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
db36c06c
CL
281 if (lport->does_npiv)
282 sp->sp_features = htons(FC_SP_FT_NPIV);
283}
284
285/**
286 * fc_fdisc_fill - Fill in a fdisc request frame.
287 */
288static inline void fc_fdisc_fill(struct fc_lport *lport, struct fc_frame *fp)
289{
290 struct fc_els_csp *sp;
291 struct fc_els_cssp *cp;
292 struct fc_els_flogi *fdisc;
293
294 fdisc = fc_frame_payload_get(fp, sizeof(*fdisc));
295 memset(fdisc, 0, sizeof(*fdisc));
296 fdisc->fl_cmd = (u8) ELS_FDISC;
297 put_unaligned_be64(lport->wwpn, &fdisc->fl_wwpn);
298 put_unaligned_be64(lport->wwnn, &fdisc->fl_wwnn);
299 sp = &fdisc->fl_csp;
300 sp->sp_hi_ver = 0x20;
301 sp->sp_lo_ver = 0x20;
302 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
303 sp->sp_bb_data = htons((u16) lport->mfs);
304 cp = &fdisc->fl_cssp[3 - 1]; /* class 3 parameters */
305 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
42e9a92f
RL
306}
307
308/**
309 * fc_logo_fill - Fill in a logo request frame.
310 */
311static inline void fc_logo_fill(struct fc_lport *lport, struct fc_frame *fp)
312{
313 struct fc_els_logo *logo;
314
315 logo = fc_frame_payload_get(fp, sizeof(*logo));
316 memset(logo, 0, sizeof(*logo));
317 logo->fl_cmd = ELS_LOGO;
7b2787ec 318 hton24(logo->fl_n_port_id, lport->port_id);
42e9a92f
RL
319 logo->fl_n_port_wwn = htonll(lport->wwpn);
320}
321
322/**
323 * fc_rtv_fill - Fill in RTV (read timeout value) request frame.
324 */
325static inline void fc_rtv_fill(struct fc_lport *lport, struct fc_frame *fp)
326{
327 struct fc_els_rtv *rtv;
328
329 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
330 memset(rtv, 0, sizeof(*rtv));
331 rtv->rtv_cmd = ELS_RTV;
332}
333
334/**
335 * fc_rec_fill - Fill in rec request frame
336 */
337static inline void fc_rec_fill(struct fc_lport *lport, struct fc_frame *fp)
338{
339 struct fc_els_rec *rec;
340 struct fc_exch *ep = fc_seq_exch(fr_seq(fp));
341
342 rec = fc_frame_payload_get(fp, sizeof(*rec));
343 memset(rec, 0, sizeof(*rec));
344 rec->rec_cmd = ELS_REC;
7b2787ec 345 hton24(rec->rec_s_id, lport->port_id);
42e9a92f
RL
346 rec->rec_ox_id = htons(ep->oxid);
347 rec->rec_rx_id = htons(ep->rxid);
348}
349
350/**
351 * fc_prli_fill - Fill in prli request frame
352 */
353static inline void fc_prli_fill(struct fc_lport *lport, struct fc_frame *fp)
354{
355 struct {
356 struct fc_els_prli prli;
357 struct fc_els_spp spp;
358 } *pp;
359
360 pp = fc_frame_payload_get(fp, sizeof(*pp));
361 memset(pp, 0, sizeof(*pp));
362 pp->prli.prli_cmd = ELS_PRLI;
363 pp->prli.prli_spp_len = sizeof(struct fc_els_spp);
364 pp->prli.prli_len = htons(sizeof(*pp));
365 pp->spp.spp_type = FC_TYPE_FCP;
366 pp->spp.spp_flags = FC_SPP_EST_IMG_PAIR;
367 pp->spp.spp_params = htonl(lport->service_params);
368}
369
370/**
371 * fc_scr_fill - Fill in a scr request frame.
372 */
373static inline void fc_scr_fill(struct fc_lport *lport, struct fc_frame *fp)
374{
375 struct fc_els_scr *scr;
376
377 scr = fc_frame_payload_get(fp, sizeof(*scr));
378 memset(scr, 0, sizeof(*scr));
379 scr->scr_cmd = ELS_SCR;
380 scr->scr_reg_func = ELS_SCRF_FULL;
381}
382
383/**
384 * fc_els_fill - Fill in an ELS request frame
385 */
9fb9d328 386static inline int fc_els_fill(struct fc_lport *lport,
a46f327a 387 u32 did,
42e9a92f 388 struct fc_frame *fp, unsigned int op,
a46f327a 389 enum fc_rctl *r_ctl, enum fc_fh_type *fh_type)
42e9a92f
RL
390{
391 switch (op) {
370c3bd0
JE
392 case ELS_ADISC:
393 fc_adisc_fill(lport, fp);
394 break;
395
42e9a92f
RL
396 case ELS_PLOGI:
397 fc_plogi_fill(lport, fp, ELS_PLOGI);
42e9a92f
RL
398 break;
399
400 case ELS_FLOGI:
401 fc_flogi_fill(lport, fp);
42e9a92f
RL
402 break;
403
db36c06c
CL
404 case ELS_FDISC:
405 fc_fdisc_fill(lport, fp);
406 break;
407
42e9a92f
RL
408 case ELS_LOGO:
409 fc_logo_fill(lport, fp);
42e9a92f
RL
410 break;
411
412 case ELS_RTV:
413 fc_rtv_fill(lport, fp);
42e9a92f
RL
414 break;
415
416 case ELS_REC:
417 fc_rec_fill(lport, fp);
42e9a92f
RL
418 break;
419
420 case ELS_PRLI:
421 fc_prli_fill(lport, fp);
42e9a92f
RL
422 break;
423
424 case ELS_SCR:
425 fc_scr_fill(lport, fp);
42e9a92f
RL
426 break;
427
428 default:
42e9a92f
RL
429 return -EINVAL;
430 }
431
432 *r_ctl = FC_RCTL_ELS_REQ;
433 *fh_type = FC_TYPE_ELS;
434 return 0;
435}
436#endif /* _FC_ENCODE_H_ */