blob: 9b4867c9c2d2e6cf8c433ef903b2d4d168b1848e [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
24struct fc_ns_rft {
25 struct fc_ns_fid fid; /* port ID object */
26 struct fc_ns_fts fts; /* FC4-types object */
27};
28
29struct fc_ct_req {
30 struct fc_ct_hdr hdr;
31 union {
32 struct fc_ns_gid_ft gid;
33 struct fc_ns_rn_id rn;
34 struct fc_ns_rft rft;
Joe Eykholtab593b12009-11-03 11:49:27 -080035 struct fc_ns_rff_id rff;
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -070036 struct fc_ns_fid fid;
Chris Leech5baa17c2009-11-03 11:46:56 -080037 struct fc_ns_rsnn snn;
Chris Leechc9866a52009-11-03 11:47:01 -080038 struct fc_ns_rspn spn;
Robert Love42e9a922008-12-09 15:10:17 -080039 } payload;
40};
41
42/**
43 * fill FC header fields in specified fc_frame
44 */
45static inline void fc_fill_fc_hdr(struct fc_frame *fp, enum fc_rctl r_ctl,
46 u32 did, u32 sid, enum fc_fh_type type,
47 u32 f_ctl, u32 parm_offset)
48{
49 struct fc_frame_header *fh;
50
51 fh = fc_frame_header_get(fp);
52 WARN_ON(r_ctl == 0);
53 fh->fh_r_ctl = r_ctl;
54 hton24(fh->fh_d_id, did);
55 hton24(fh->fh_s_id, sid);
56 fh->fh_type = type;
57 hton24(fh->fh_f_ctl, f_ctl);
58 fh->fh_cs_ctl = 0;
59 fh->fh_df_ctl = 0;
60 fh->fh_parm_offset = htonl(parm_offset);
61}
62
63/**
Joe Eykholt370c3bd2009-08-25 14:03:47 -070064 * fc_adisc_fill() - Fill in adisc request frame
65 * @lport: local port.
66 * @fp: fc frame where payload will be placed.
67 */
68static inline void fc_adisc_fill(struct fc_lport *lport, struct fc_frame *fp)
69{
70 struct fc_els_adisc *adisc;
71
72 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
73 memset(adisc, 0, sizeof(*adisc));
74 adisc->adisc_cmd = ELS_ADISC;
75 put_unaligned_be64(lport->wwpn, &adisc->adisc_wwpn);
76 put_unaligned_be64(lport->wwnn, &adisc->adisc_wwnn);
Robert Love7b2787e2010-05-07 15:18:41 -070077 hton24(adisc->adisc_port_id, lport->port_id);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070078}
79
80/**
Robert Love42e9a922008-12-09 15:10:17 -080081 * fc_ct_hdr_fill- fills ct header and reset ct payload
82 * returns pointer to ct request.
83 */
84static inline struct fc_ct_req *fc_ct_hdr_fill(const struct fc_frame *fp,
85 unsigned int op, size_t req_size)
86{
87 struct fc_ct_req *ct;
88 size_t ct_plen;
89
90 ct_plen = sizeof(struct fc_ct_hdr) + req_size;
91 ct = fc_frame_payload_get(fp, ct_plen);
92 memset(ct, 0, ct_plen);
93 ct->hdr.ct_rev = FC_CT_REV;
94 ct->hdr.ct_fs_type = FC_FST_DIR;
95 ct->hdr.ct_fs_subtype = FC_NS_SUBTYPE;
96 ct->hdr.ct_cmd = htons((u16) op);
97 return ct;
98}
99
100/**
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700101 * fc_ct_fill() - Fill in a name service request frame
102 * @lport: local port.
103 * @fc_id: FC_ID of non-destination rport for GPN_ID and similar inquiries.
104 * @fp: frame to contain payload.
105 * @op: CT opcode.
106 * @r_ctl: pointer to FC header R_CTL.
107 * @fh_type: pointer to FC-4 type.
Robert Love42e9a922008-12-09 15:10:17 -0800108 */
Joe Eykholta46f3272009-08-25 14:00:55 -0700109static inline int fc_ct_fill(struct fc_lport *lport,
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700110 u32 fc_id, struct fc_frame *fp,
Joe Eykholta46f3272009-08-25 14:00:55 -0700111 unsigned int op, enum fc_rctl *r_ctl,
Robert Love42e9a922008-12-09 15:10:17 -0800112 enum fc_fh_type *fh_type)
113{
114 struct fc_ct_req *ct;
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800115 size_t len;
Robert Love42e9a922008-12-09 15:10:17 -0800116
117 switch (op) {
118 case FC_NS_GPN_FT:
119 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_gid_ft));
120 ct->payload.gid.fn_fc4_type = FC_TYPE_FCP;
121 break;
122
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700123 case FC_NS_GPN_ID:
124 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_fid));
125 hton24(ct->payload.fid.fp_fid, fc_id);
126 break;
127
Robert Love42e9a922008-12-09 15:10:17 -0800128 case FC_NS_RFT_ID:
129 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rft));
Robert Love7b2787e2010-05-07 15:18:41 -0700130 hton24(ct->payload.rft.fid.fp_fid, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800131 ct->payload.rft.fts = lport->fcts;
132 break;
133
Joe Eykholtab593b12009-11-03 11:49:27 -0800134 case FC_NS_RFF_ID:
135 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rff_id));
Robert Love7b2787e2010-05-07 15:18:41 -0700136 hton24(ct->payload.rff.fr_fid.fp_fid, lport->port_id);
Joe Eykholtab593b12009-11-03 11:49:27 -0800137 ct->payload.rff.fr_type = FC_TYPE_FCP;
138 if (lport->service_params & FCP_SPPF_INIT_FCN)
139 ct->payload.rff.fr_feat = FCP_FEAT_INIT;
140 if (lport->service_params & FCP_SPPF_TARG_FCN)
141 ct->payload.rff.fr_feat |= FCP_FEAT_TARG;
142 break;
143
Chris Leechc9c7bd72009-11-03 11:46:51 -0800144 case FC_NS_RNN_ID:
Robert Love42e9a922008-12-09 15:10:17 -0800145 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rn_id));
Robert Love7b2787e2010-05-07 15:18:41 -0700146 hton24(ct->payload.rn.fr_fid.fp_fid, lport->port_id);
Chris Leechc9c7bd72009-11-03 11:46:51 -0800147 put_unaligned_be64(lport->wwnn, &ct->payload.rn.fr_wwn);
Robert Love42e9a922008-12-09 15:10:17 -0800148 break;
149
Chris Leechc9866a52009-11-03 11:47:01 -0800150 case FC_NS_RSPN_ID:
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800151 len = strnlen(fc_host_symbolic_name(lport->host), 255);
152 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len);
Robert Love7b2787e2010-05-07 15:18:41 -0700153 hton24(ct->payload.spn.fr_fid.fp_fid, lport->port_id);
Chris Leechc9866a52009-11-03 11:47:01 -0800154 strncpy(ct->payload.spn.fr_name,
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800155 fc_host_symbolic_name(lport->host), len);
156 ct->payload.spn.fr_name_len = len;
Chris Leechc9866a52009-11-03 11:47:01 -0800157 break;
158
Chris Leech5baa17c2009-11-03 11:46:56 -0800159 case FC_NS_RSNN_NN:
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800160 len = strnlen(fc_host_symbolic_name(lport->host), 255);
161 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rsnn) + len);
Chris Leech5baa17c2009-11-03 11:46:56 -0800162 put_unaligned_be64(lport->wwnn, &ct->payload.snn.fr_wwn);
163 strncpy(ct->payload.snn.fr_name,
Joe Eykholt5f9a0562009-11-03 11:48:55 -0800164 fc_host_symbolic_name(lport->host), len);
165 ct->payload.snn.fr_name_len = len;
Chris Leech5baa17c2009-11-03 11:46:56 -0800166 break;
167
Robert Love42e9a922008-12-09 15:10:17 -0800168 default:
Robert Love42e9a922008-12-09 15:10:17 -0800169 return -EINVAL;
170 }
171 *r_ctl = FC_RCTL_DD_UNSOL_CTL;
Robert Love42e9a922008-12-09 15:10:17 -0800172 *fh_type = FC_TYPE_CT;
173 return 0;
174}
175
176/**
177 * fc_plogi_fill - Fill in plogi request frame
178 */
179static inline void fc_plogi_fill(struct fc_lport *lport, struct fc_frame *fp,
180 unsigned int op)
181{
182 struct fc_els_flogi *plogi;
183 struct fc_els_csp *csp;
184 struct fc_els_cssp *cp;
185
186 plogi = fc_frame_payload_get(fp, sizeof(*plogi));
187 memset(plogi, 0, sizeof(*plogi));
188 plogi->fl_cmd = (u8) op;
189 put_unaligned_be64(lport->wwpn, &plogi->fl_wwpn);
190 put_unaligned_be64(lport->wwnn, &plogi->fl_wwnn);
191
192 csp = &plogi->fl_csp;
193 csp->sp_hi_ver = 0x20;
194 csp->sp_lo_ver = 0x20;
195 csp->sp_bb_cred = htons(10); /* this gets set by gateway */
196 csp->sp_bb_data = htons((u16) lport->mfs);
197 cp = &plogi->fl_cssp[3 - 1]; /* class 3 parameters */
198 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
199 csp->sp_features = htons(FC_SP_FT_CIRO);
200 csp->sp_tot_seq = htons(255); /* seq. we accept */
201 csp->sp_rel_off = htons(0x1f);
202 csp->sp_e_d_tov = htonl(lport->e_d_tov);
203
204 cp->cp_rdfs = htons((u16) lport->mfs);
205 cp->cp_con_seq = htons(255);
206 cp->cp_open_seq = 1;
207}
208
209/**
210 * fc_flogi_fill - Fill in a flogi request frame.
211 */
212static inline void fc_flogi_fill(struct fc_lport *lport, struct fc_frame *fp)
213{
214 struct fc_els_csp *sp;
215 struct fc_els_cssp *cp;
216 struct fc_els_flogi *flogi;
217
218 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
219 memset(flogi, 0, sizeof(*flogi));
220 flogi->fl_cmd = (u8) ELS_FLOGI;
221 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
222 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
223 sp = &flogi->fl_csp;
224 sp->sp_hi_ver = 0x20;
225 sp->sp_lo_ver = 0x20;
226 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
227 sp->sp_bb_data = htons((u16) lport->mfs);
228 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
229 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
Chris Leechdb36c062009-11-03 11:46:24 -0800230 if (lport->does_npiv)
231 sp->sp_features = htons(FC_SP_FT_NPIV);
232}
233
234/**
235 * fc_fdisc_fill - Fill in a fdisc request frame.
236 */
237static inline void fc_fdisc_fill(struct fc_lport *lport, struct fc_frame *fp)
238{
239 struct fc_els_csp *sp;
240 struct fc_els_cssp *cp;
241 struct fc_els_flogi *fdisc;
242
243 fdisc = fc_frame_payload_get(fp, sizeof(*fdisc));
244 memset(fdisc, 0, sizeof(*fdisc));
245 fdisc->fl_cmd = (u8) ELS_FDISC;
246 put_unaligned_be64(lport->wwpn, &fdisc->fl_wwpn);
247 put_unaligned_be64(lport->wwnn, &fdisc->fl_wwnn);
248 sp = &fdisc->fl_csp;
249 sp->sp_hi_ver = 0x20;
250 sp->sp_lo_ver = 0x20;
251 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
252 sp->sp_bb_data = htons((u16) lport->mfs);
253 cp = &fdisc->fl_cssp[3 - 1]; /* class 3 parameters */
254 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
Robert Love42e9a922008-12-09 15:10:17 -0800255}
256
257/**
258 * fc_logo_fill - Fill in a logo request frame.
259 */
260static inline void fc_logo_fill(struct fc_lport *lport, struct fc_frame *fp)
261{
262 struct fc_els_logo *logo;
263
264 logo = fc_frame_payload_get(fp, sizeof(*logo));
265 memset(logo, 0, sizeof(*logo));
266 logo->fl_cmd = ELS_LOGO;
Robert Love7b2787e2010-05-07 15:18:41 -0700267 hton24(logo->fl_n_port_id, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800268 logo->fl_n_port_wwn = htonll(lport->wwpn);
269}
270
271/**
272 * fc_rtv_fill - Fill in RTV (read timeout value) request frame.
273 */
274static inline void fc_rtv_fill(struct fc_lport *lport, struct fc_frame *fp)
275{
276 struct fc_els_rtv *rtv;
277
278 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
279 memset(rtv, 0, sizeof(*rtv));
280 rtv->rtv_cmd = ELS_RTV;
281}
282
283/**
284 * fc_rec_fill - Fill in rec request frame
285 */
286static inline void fc_rec_fill(struct fc_lport *lport, struct fc_frame *fp)
287{
288 struct fc_els_rec *rec;
289 struct fc_exch *ep = fc_seq_exch(fr_seq(fp));
290
291 rec = fc_frame_payload_get(fp, sizeof(*rec));
292 memset(rec, 0, sizeof(*rec));
293 rec->rec_cmd = ELS_REC;
Robert Love7b2787e2010-05-07 15:18:41 -0700294 hton24(rec->rec_s_id, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800295 rec->rec_ox_id = htons(ep->oxid);
296 rec->rec_rx_id = htons(ep->rxid);
297}
298
299/**
300 * fc_prli_fill - Fill in prli request frame
301 */
302static inline void fc_prli_fill(struct fc_lport *lport, struct fc_frame *fp)
303{
304 struct {
305 struct fc_els_prli prli;
306 struct fc_els_spp spp;
307 } *pp;
308
309 pp = fc_frame_payload_get(fp, sizeof(*pp));
310 memset(pp, 0, sizeof(*pp));
311 pp->prli.prli_cmd = ELS_PRLI;
312 pp->prli.prli_spp_len = sizeof(struct fc_els_spp);
313 pp->prli.prli_len = htons(sizeof(*pp));
314 pp->spp.spp_type = FC_TYPE_FCP;
315 pp->spp.spp_flags = FC_SPP_EST_IMG_PAIR;
316 pp->spp.spp_params = htonl(lport->service_params);
317}
318
319/**
320 * fc_scr_fill - Fill in a scr request frame.
321 */
322static inline void fc_scr_fill(struct fc_lport *lport, struct fc_frame *fp)
323{
324 struct fc_els_scr *scr;
325
326 scr = fc_frame_payload_get(fp, sizeof(*scr));
327 memset(scr, 0, sizeof(*scr));
328 scr->scr_cmd = ELS_SCR;
329 scr->scr_reg_func = ELS_SCRF_FULL;
330}
331
332/**
333 * fc_els_fill - Fill in an ELS request frame
334 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700335static inline int fc_els_fill(struct fc_lport *lport,
Joe Eykholta46f3272009-08-25 14:00:55 -0700336 u32 did,
Robert Love42e9a922008-12-09 15:10:17 -0800337 struct fc_frame *fp, unsigned int op,
Joe Eykholta46f3272009-08-25 14:00:55 -0700338 enum fc_rctl *r_ctl, enum fc_fh_type *fh_type)
Robert Love42e9a922008-12-09 15:10:17 -0800339{
340 switch (op) {
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700341 case ELS_ADISC:
342 fc_adisc_fill(lport, fp);
343 break;
344
Robert Love42e9a922008-12-09 15:10:17 -0800345 case ELS_PLOGI:
346 fc_plogi_fill(lport, fp, ELS_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800347 break;
348
349 case ELS_FLOGI:
350 fc_flogi_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800351 break;
352
Chris Leechdb36c062009-11-03 11:46:24 -0800353 case ELS_FDISC:
354 fc_fdisc_fill(lport, fp);
355 break;
356
Robert Love42e9a922008-12-09 15:10:17 -0800357 case ELS_LOGO:
358 fc_logo_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800359 break;
360
361 case ELS_RTV:
362 fc_rtv_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800363 break;
364
365 case ELS_REC:
366 fc_rec_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800367 break;
368
369 case ELS_PRLI:
370 fc_prli_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800371 break;
372
373 case ELS_SCR:
374 fc_scr_fill(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800375 break;
376
377 default:
Robert Love42e9a922008-12-09 15:10:17 -0800378 return -EINVAL;
379 }
380
381 *r_ctl = FC_RCTL_ELS_REQ;
382 *fh_type = FC_TYPE_ELS;
383 return 0;
384}
385#endif /* _FC_ENCODE_H_ */