blob: be418d8448a565ebfcf02f1675164447bb32fd1c [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
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
Joe Eykholt24f089e2010-07-20 15:21:01 -070024/*
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
Robert Love42e9a922008-12-09 15:10:17 -080031struct 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;
Joe Eykholtab593b12009-11-03 11:49:27 -080042 struct fc_ns_rff_id rff;
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -070043 struct fc_ns_fid fid;
Chris Leech5baa17c2009-11-03 11:46:56 -080044 struct fc_ns_rsnn snn;
Chris Leechc9866a52009-11-03 11:47:01 -080045 struct fc_ns_rspn spn;
Robert Love42e9a922008-12-09 15:10:17 -080046 } payload;
47};
48
Bhanu Prakash Gollapudi059f04d2011-02-25 15:03:07 -080049static 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)
53{
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
Robert Love42e9a922008-12-09 15:10:17 -080065/**
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);
Bhanu Prakash Gollapudi059f04d2011-02-25 15:03:07 -080075 __fc_fill_fc_hdr(fh, r_ctl, did, sid, type, f_ctl, parm_offset);
Robert Love42e9a922008-12-09 15:10:17 -080076}
77
78/**
Joe Eykholt370c3bd2009-08-25 14:03:47 -070079 * 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);
Robert Love7b2787e2010-05-07 15:18:41 -070092 hton24(adisc->adisc_port_id, lport->port_id);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070093}
94
95/**
Robert Love42e9a922008-12-09 15:10:17 -080096 * 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,
100 unsigned int op, size_t req_size)
101{
102 struct fc_ct_req *ct;
103 size_t ct_plen;
104
105 ct_plen = sizeof(struct fc_ct_hdr) + req_size;
106 ct = fc_frame_payload_get(fp, ct_plen);
107 memset(ct, 0, ct_plen);
108 ct->hdr.ct_rev = FC_CT_REV;
109 ct->hdr.ct_fs_type = FC_FST_DIR;
110 ct->hdr.ct_fs_subtype = FC_NS_SUBTYPE;
111 ct->hdr.ct_cmd = htons((u16) op);
112 return ct;
113}
114
115/**
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700116 * fc_ct_fill() - Fill in a name service request frame
117 * @lport: local port.
118 * @fc_id: FC_ID of non-destination rport for GPN_ID and similar inquiries.
119 * @fp: frame to contain payload.
120 * @op: CT opcode.
121 * @r_ctl: pointer to FC header R_CTL.
122 * @fh_type: pointer to FC-4 type.
Robert Love42e9a922008-12-09 15:10:17 -0800123 */
Joe Eykholta46f3272009-08-25 14:00:55 -0700124static inline int fc_ct_fill(struct fc_lport *lport,
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700125 u32 fc_id, struct fc_frame *fp,
Joe Eykholta46f3272009-08-25 14:00:55 -0700126 unsigned int op, enum fc_rctl *r_ctl,
Robert Love42e9a922008-12-09 15:10:17 -0800127 enum fc_fh_type *fh_type)
128{
129 struct fc_ct_req *ct;
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800130 size_t len;
Robert Love42e9a922008-12-09 15:10:17 -0800131
132 switch (op) {
133 case FC_NS_GPN_FT:
134 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_gid_ft));
135 ct->payload.gid.fn_fc4_type = FC_TYPE_FCP;
136 break;
137
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700138 case FC_NS_GPN_ID:
139 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_fid));
140 hton24(ct->payload.fid.fp_fid, fc_id);
141 break;
142
Robert Love42e9a922008-12-09 15:10:17 -0800143 case FC_NS_RFT_ID:
144 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rft));
Robert Love7b2787e2010-05-07 15:18:41 -0700145 hton24(ct->payload.rft.fid.fp_fid, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800146 ct->payload.rft.fts = lport->fcts;
147 break;
148
Joe Eykholtab593b12009-11-03 11:49:27 -0800149 case FC_NS_RFF_ID:
150 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rff_id));
Robert Love7b2787e2010-05-07 15:18:41 -0700151 hton24(ct->payload.rff.fr_fid.fp_fid, lport->port_id);
Joe Eykholtab593b12009-11-03 11:49:27 -0800152 ct->payload.rff.fr_type = FC_TYPE_FCP;
153 if (lport->service_params & FCP_SPPF_INIT_FCN)
154 ct->payload.rff.fr_feat = FCP_FEAT_INIT;
155 if (lport->service_params & FCP_SPPF_TARG_FCN)
156 ct->payload.rff.fr_feat |= FCP_FEAT_TARG;
157 break;
158
Chris Leechc9c7bd72009-11-03 11:46:51 -0800159 case FC_NS_RNN_ID:
Robert Love42e9a922008-12-09 15:10:17 -0800160 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rn_id));
Robert Love7b2787e2010-05-07 15:18:41 -0700161 hton24(ct->payload.rn.fr_fid.fp_fid, lport->port_id);
Chris Leechc9c7bd72009-11-03 11:46:51 -0800162 put_unaligned_be64(lport->wwnn, &ct->payload.rn.fr_wwn);
Robert Love42e9a922008-12-09 15:10:17 -0800163 break;
164
Chris Leechc9866a52009-11-03 11:47:01 -0800165 case FC_NS_RSPN_ID:
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800166 len = strnlen(fc_host_symbolic_name(lport->host), 255);
167 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len);
Robert Love7b2787e2010-05-07 15:18:41 -0700168 hton24(ct->payload.spn.fr_fid.fp_fid, lport->port_id);
Chris Leechc9866a52009-11-03 11:47:01 -0800169 strncpy(ct->payload.spn.fr_name,
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800170 fc_host_symbolic_name(lport->host), len);
171 ct->payload.spn.fr_name_len = len;
Chris Leechc9866a52009-11-03 11:47:01 -0800172 break;
173
Chris Leech5baa17c2009-11-03 11:46:56 -0800174 case FC_NS_RSNN_NN:
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800175 len = strnlen(fc_host_symbolic_name(lport->host), 255);
176 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rsnn) + len);
Chris Leech5baa17c2009-11-03 11:46:56 -0800177 put_unaligned_be64(lport->wwnn, &ct->payload.snn.fr_wwn);
178 strncpy(ct->payload.snn.fr_name,
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800179 fc_host_symbolic_name(lport->host), len);
180 ct->payload.snn.fr_name_len = len;
Chris Leech5baa17c2009-11-03 11:46:56 -0800181 break;
182
Robert Love42e9a922008-12-09 15:10:17 -0800183 default:
Robert Love42e9a922008-12-09 15:10:17 -0800184 return -EINVAL;
185 }
186 *r_ctl = FC_RCTL_DD_UNSOL_CTL;
Robert Love42e9a922008-12-09 15:10:17 -0800187 *fh_type = FC_TYPE_CT;
188 return 0;
189}
190
191/**
192 * fc_plogi_fill - Fill in plogi request frame
193 */
194static inline void fc_plogi_fill(struct fc_lport *lport, struct fc_frame *fp,
195 unsigned int op)
196{
197 struct fc_els_flogi *plogi;
198 struct fc_els_csp *csp;
199 struct fc_els_cssp *cp;
200
201 plogi = fc_frame_payload_get(fp, sizeof(*plogi));
202 memset(plogi, 0, sizeof(*plogi));
203 plogi->fl_cmd = (u8) op;
204 put_unaligned_be64(lport->wwpn, &plogi->fl_wwpn);
205 put_unaligned_be64(lport->wwnn, &plogi->fl_wwnn);
206
207 csp = &plogi->fl_csp;
208 csp->sp_hi_ver = 0x20;
209 csp->sp_lo_ver = 0x20;
210 csp->sp_bb_cred = htons(10); /* this gets set by gateway */
211 csp->sp_bb_data = htons((u16) lport->mfs);
212 cp = &plogi->fl_cssp[3 - 1]; /* class 3 parameters */
213 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
214 csp->sp_features = htons(FC_SP_FT_CIRO);
215 csp->sp_tot_seq = htons(255); /* seq. we accept */
216 csp->sp_rel_off = htons(0x1f);
217 csp->sp_e_d_tov = htonl(lport->e_d_tov);
218
219 cp->cp_rdfs = htons((u16) lport->mfs);
220 cp->cp_con_seq = htons(255);
221 cp->cp_open_seq = 1;
222}
223
224/**
225 * fc_flogi_fill - Fill in a flogi request frame.
226 */
227static inline void fc_flogi_fill(struct fc_lport *lport, struct fc_frame *fp)
228{
229 struct fc_els_csp *sp;
230 struct fc_els_cssp *cp;
231 struct fc_els_flogi *flogi;
232
233 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
234 memset(flogi, 0, sizeof(*flogi));
235 flogi->fl_cmd = (u8) ELS_FLOGI;
236 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
237 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
238 sp = &flogi->fl_csp;
239 sp->sp_hi_ver = 0x20;
240 sp->sp_lo_ver = 0x20;
241 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
242 sp->sp_bb_data = htons((u16) lport->mfs);
243 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
244 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
Chris Leechdb36c062009-11-03 11:46:24 -0800245 if (lport->does_npiv)
246 sp->sp_features = htons(FC_SP_FT_NPIV);
247}
248
249/**
250 * fc_fdisc_fill - Fill in a fdisc request frame.
251 */
252static inline void fc_fdisc_fill(struct fc_lport *lport, struct fc_frame *fp)
253{
254 struct fc_els_csp *sp;
255 struct fc_els_cssp *cp;
256 struct fc_els_flogi *fdisc;
257
258 fdisc = fc_frame_payload_get(fp, sizeof(*fdisc));
259 memset(fdisc, 0, sizeof(*fdisc));
260 fdisc->fl_cmd = (u8) ELS_FDISC;
261 put_unaligned_be64(lport->wwpn, &fdisc->fl_wwpn);
262 put_unaligned_be64(lport->wwnn, &fdisc->fl_wwnn);
263 sp = &fdisc->fl_csp;
264 sp->sp_hi_ver = 0x20;
265 sp->sp_lo_ver = 0x20;
266 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
267 sp->sp_bb_data = htons((u16) lport->mfs);
268 cp = &fdisc->fl_cssp[3 - 1]; /* class 3 parameters */
269 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
Robert Love42e9a922008-12-09 15:10:17 -0800270}
271
272/**
273 * fc_logo_fill - Fill in a logo request frame.
274 */
275static inline void fc_logo_fill(struct fc_lport *lport, struct fc_frame *fp)
276{
277 struct fc_els_logo *logo;
278
279 logo = fc_frame_payload_get(fp, sizeof(*logo));
280 memset(logo, 0, sizeof(*logo));
281 logo->fl_cmd = ELS_LOGO;
Robert Love7b2787e2010-05-07 15:18:41 -0700282 hton24(logo->fl_n_port_id, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800283 logo->fl_n_port_wwn = htonll(lport->wwpn);
284}
285
286/**
287 * fc_rtv_fill - Fill in RTV (read timeout value) request frame.
288 */
289static inline void fc_rtv_fill(struct fc_lport *lport, struct fc_frame *fp)
290{
291 struct fc_els_rtv *rtv;
292
293 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
294 memset(rtv, 0, sizeof(*rtv));
295 rtv->rtv_cmd = ELS_RTV;
296}
297
298/**
299 * fc_rec_fill - Fill in rec request frame
300 */
301static inline void fc_rec_fill(struct fc_lport *lport, struct fc_frame *fp)
302{
303 struct fc_els_rec *rec;
304 struct fc_exch *ep = fc_seq_exch(fr_seq(fp));
305
306 rec = fc_frame_payload_get(fp, sizeof(*rec));
307 memset(rec, 0, sizeof(*rec));
308 rec->rec_cmd = ELS_REC;
Robert Love7b2787e2010-05-07 15:18:41 -0700309 hton24(rec->rec_s_id, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800310 rec->rec_ox_id = htons(ep->oxid);
311 rec->rec_rx_id = htons(ep->rxid);
312}
313
314/**
315 * fc_prli_fill - Fill in prli request frame
316 */
317static inline void fc_prli_fill(struct fc_lport *lport, struct fc_frame *fp)
318{
319 struct {
320 struct fc_els_prli prli;
321 struct fc_els_spp spp;
322 } *pp;
323
324 pp = fc_frame_payload_get(fp, sizeof(*pp));
325 memset(pp, 0, sizeof(*pp));
326 pp->prli.prli_cmd = ELS_PRLI;
327 pp->prli.prli_spp_len = sizeof(struct fc_els_spp);
328 pp->prli.prli_len = htons(sizeof(*pp));
329 pp->spp.spp_type = FC_TYPE_FCP;
330 pp->spp.spp_flags = FC_SPP_EST_IMG_PAIR;
331 pp->spp.spp_params = htonl(lport->service_params);
332}
333
334/**
335 * fc_scr_fill - Fill in a scr request frame.
336 */
337static inline void fc_scr_fill(struct fc_lport *lport, struct fc_frame *fp)
338{
339 struct fc_els_scr *scr;
340
341 scr = fc_frame_payload_get(fp, sizeof(*scr));
342 memset(scr, 0, sizeof(*scr));
343 scr->scr_cmd = ELS_SCR;
344 scr->scr_reg_func = ELS_SCRF_FULL;
345}
346
347/**
348 * fc_els_fill - Fill in an ELS request frame
349 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700350static inline int fc_els_fill(struct fc_lport *lport,
Joe Eykholta46f3272009-08-25 14:00:55 -0700351 u32 did,
Robert Love42e9a922008-12-09 15:10:17 -0800352 struct fc_frame *fp, unsigned int op,
Joe Eykholta46f3272009-08-25 14:00:55 -0700353 enum fc_rctl *r_ctl, enum fc_fh_type *fh_type)
Robert Love42e9a922008-12-09 15:10:17 -0800354{
355 switch (op) {
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700356 case ELS_ADISC:
357 fc_adisc_fill(lport, fp);
358 break;
359
Robert Love42e9a922008-12-09 15:10:17 -0800360 case ELS_PLOGI:
361 fc_plogi_fill(lport, fp, ELS_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800362 break;
363
364 case ELS_FLOGI:
365 fc_flogi_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800366 break;
367
Chris Leechdb36c062009-11-03 11:46:24 -0800368 case ELS_FDISC:
369 fc_fdisc_fill(lport, fp);
370 break;
371
Robert Love42e9a922008-12-09 15:10:17 -0800372 case ELS_LOGO:
373 fc_logo_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800374 break;
375
376 case ELS_RTV:
377 fc_rtv_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800378 break;
379
380 case ELS_REC:
381 fc_rec_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800382 break;
383
384 case ELS_PRLI:
385 fc_prli_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800386 break;
387
388 case ELS_SCR:
389 fc_scr_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800390 break;
391
392 default:
Robert Love42e9a922008-12-09 15:10:17 -0800393 return -EINVAL;
394 }
395
396 *r_ctl = FC_RCTL_ELS_REQ;
397 *fh_type = FC_TYPE_ELS;
398 return 0;
399}
400#endif /* _FC_ENCODE_H_ */