6d293c846a46d0f1a58f1388f3b871d205262803
[pandora-kernel.git] / include / scsi / fc_encode.h
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
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
31 struct fc_ns_rft {
32         struct fc_ns_fid fid;   /* port ID object */
33         struct fc_ns_fts fts;   /* FC4-types object */
34 };
35
36 struct 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;
42                 struct fc_ns_rff_id rff;
43                 struct fc_ns_fid fid;
44                 struct fc_ns_rsnn snn;
45                 struct fc_ns_rspn spn;
46         } payload;
47 };
48
49 /**
50  * fill FC header fields in specified fc_frame
51  */
52 static inline void fc_fill_fc_hdr(struct fc_frame *fp, enum fc_rctl r_ctl,
53                                   u32 did, u32 sid, enum fc_fh_type type,
54                                   u32 f_ctl, u32 parm_offset)
55 {
56         struct fc_frame_header *fh;
57
58         fh = fc_frame_header_get(fp);
59         WARN_ON(r_ctl == 0);
60         fh->fh_r_ctl = r_ctl;
61         hton24(fh->fh_d_id, did);
62         hton24(fh->fh_s_id, sid);
63         fh->fh_type = type;
64         hton24(fh->fh_f_ctl, f_ctl);
65         fh->fh_cs_ctl = 0;
66         fh->fh_df_ctl = 0;
67         fh->fh_parm_offset = htonl(parm_offset);
68 }
69
70 /**
71  * fc_adisc_fill() - Fill in adisc request frame
72  * @lport: local port.
73  * @fp: fc frame where payload will be placed.
74  */
75 static inline void fc_adisc_fill(struct fc_lport *lport, struct fc_frame *fp)
76 {
77         struct fc_els_adisc *adisc;
78
79         adisc = fc_frame_payload_get(fp, sizeof(*adisc));
80         memset(adisc, 0, sizeof(*adisc));
81         adisc->adisc_cmd = ELS_ADISC;
82         put_unaligned_be64(lport->wwpn, &adisc->adisc_wwpn);
83         put_unaligned_be64(lport->wwnn, &adisc->adisc_wwnn);
84         hton24(adisc->adisc_port_id, lport->port_id);
85 }
86
87 /**
88  * fc_ct_hdr_fill- fills ct header and reset ct payload
89  * returns pointer to ct request.
90  */
91 static inline struct fc_ct_req *fc_ct_hdr_fill(const struct fc_frame *fp,
92                                                unsigned int op, size_t req_size)
93 {
94         struct fc_ct_req *ct;
95         size_t ct_plen;
96
97         ct_plen  = sizeof(struct fc_ct_hdr) + req_size;
98         ct = fc_frame_payload_get(fp, ct_plen);
99         memset(ct, 0, ct_plen);
100         ct->hdr.ct_rev = FC_CT_REV;
101         ct->hdr.ct_fs_type = FC_FST_DIR;
102         ct->hdr.ct_fs_subtype = FC_NS_SUBTYPE;
103         ct->hdr.ct_cmd = htons((u16) op);
104         return ct;
105 }
106
107 /**
108  * fc_ct_fill() - Fill in a name service request frame
109  * @lport: local port.
110  * @fc_id: FC_ID of non-destination rport for GPN_ID and similar inquiries.
111  * @fp: frame to contain payload.
112  * @op: CT opcode.
113  * @r_ctl: pointer to FC header R_CTL.
114  * @fh_type: pointer to FC-4 type.
115  */
116 static inline int fc_ct_fill(struct fc_lport *lport,
117                       u32 fc_id, struct fc_frame *fp,
118                       unsigned int op, enum fc_rctl *r_ctl,
119                       enum fc_fh_type *fh_type)
120 {
121         struct fc_ct_req *ct;
122         size_t len;
123
124         switch (op) {
125         case FC_NS_GPN_FT:
126                 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_gid_ft));
127                 ct->payload.gid.fn_fc4_type = FC_TYPE_FCP;
128                 break;
129
130         case FC_NS_GPN_ID:
131                 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_fid));
132                 hton24(ct->payload.fid.fp_fid, fc_id);
133                 break;
134
135         case FC_NS_RFT_ID:
136                 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rft));
137                 hton24(ct->payload.rft.fid.fp_fid, lport->port_id);
138                 ct->payload.rft.fts = lport->fcts;
139                 break;
140
141         case FC_NS_RFF_ID:
142                 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rff_id));
143                 hton24(ct->payload.rff.fr_fid.fp_fid, lport->port_id);
144                 ct->payload.rff.fr_type = FC_TYPE_FCP;
145                 if (lport->service_params & FCP_SPPF_INIT_FCN)
146                         ct->payload.rff.fr_feat = FCP_FEAT_INIT;
147                 if (lport->service_params & FCP_SPPF_TARG_FCN)
148                         ct->payload.rff.fr_feat |= FCP_FEAT_TARG;
149                 break;
150
151         case FC_NS_RNN_ID:
152                 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rn_id));
153                 hton24(ct->payload.rn.fr_fid.fp_fid, lport->port_id);
154                 put_unaligned_be64(lport->wwnn, &ct->payload.rn.fr_wwn);
155                 break;
156
157         case FC_NS_RSPN_ID:
158                 len = strnlen(fc_host_symbolic_name(lport->host), 255);
159                 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len);
160                 hton24(ct->payload.spn.fr_fid.fp_fid, lport->port_id);
161                 strncpy(ct->payload.spn.fr_name,
162                         fc_host_symbolic_name(lport->host), len);
163                 ct->payload.spn.fr_name_len = len;
164                 break;
165
166         case FC_NS_RSNN_NN:
167                 len = strnlen(fc_host_symbolic_name(lport->host), 255);
168                 ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rsnn) + len);
169                 put_unaligned_be64(lport->wwnn, &ct->payload.snn.fr_wwn);
170                 strncpy(ct->payload.snn.fr_name,
171                         fc_host_symbolic_name(lport->host), len);
172                 ct->payload.snn.fr_name_len = len;
173                 break;
174
175         default:
176                 return -EINVAL;
177         }
178         *r_ctl = FC_RCTL_DD_UNSOL_CTL;
179         *fh_type = FC_TYPE_CT;
180         return 0;
181 }
182
183 /**
184  * fc_plogi_fill - Fill in plogi request frame
185  */
186 static inline void fc_plogi_fill(struct fc_lport *lport, struct fc_frame *fp,
187                                  unsigned int op)
188 {
189         struct fc_els_flogi *plogi;
190         struct fc_els_csp *csp;
191         struct fc_els_cssp *cp;
192
193         plogi = fc_frame_payload_get(fp, sizeof(*plogi));
194         memset(plogi, 0, sizeof(*plogi));
195         plogi->fl_cmd = (u8) op;
196         put_unaligned_be64(lport->wwpn, &plogi->fl_wwpn);
197         put_unaligned_be64(lport->wwnn, &plogi->fl_wwnn);
198
199         csp = &plogi->fl_csp;
200         csp->sp_hi_ver = 0x20;
201         csp->sp_lo_ver = 0x20;
202         csp->sp_bb_cred = htons(10);    /* this gets set by gateway */
203         csp->sp_bb_data = htons((u16) lport->mfs);
204         cp = &plogi->fl_cssp[3 - 1];    /* class 3 parameters */
205         cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
206         csp->sp_features = htons(FC_SP_FT_CIRO);
207         csp->sp_tot_seq = htons(255);   /* seq. we accept */
208         csp->sp_rel_off = htons(0x1f);
209         csp->sp_e_d_tov = htonl(lport->e_d_tov);
210
211         cp->cp_rdfs = htons((u16) lport->mfs);
212         cp->cp_con_seq = htons(255);
213         cp->cp_open_seq = 1;
214 }
215
216 /**
217  * fc_flogi_fill - Fill in a flogi request frame.
218  */
219 static inline void fc_flogi_fill(struct fc_lport *lport, struct fc_frame *fp)
220 {
221         struct fc_els_csp *sp;
222         struct fc_els_cssp *cp;
223         struct fc_els_flogi *flogi;
224
225         flogi = fc_frame_payload_get(fp, sizeof(*flogi));
226         memset(flogi, 0, sizeof(*flogi));
227         flogi->fl_cmd = (u8) ELS_FLOGI;
228         put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
229         put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
230         sp = &flogi->fl_csp;
231         sp->sp_hi_ver = 0x20;
232         sp->sp_lo_ver = 0x20;
233         sp->sp_bb_cred = htons(10);     /* this gets set by gateway */
234         sp->sp_bb_data = htons((u16) lport->mfs);
235         cp = &flogi->fl_cssp[3 - 1];    /* class 3 parameters */
236         cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
237         if (lport->does_npiv)
238                 sp->sp_features = htons(FC_SP_FT_NPIV);
239 }
240
241 /**
242  * fc_fdisc_fill - Fill in a fdisc request frame.
243  */
244 static inline void fc_fdisc_fill(struct fc_lport *lport, struct fc_frame *fp)
245 {
246         struct fc_els_csp *sp;
247         struct fc_els_cssp *cp;
248         struct fc_els_flogi *fdisc;
249
250         fdisc = fc_frame_payload_get(fp, sizeof(*fdisc));
251         memset(fdisc, 0, sizeof(*fdisc));
252         fdisc->fl_cmd = (u8) ELS_FDISC;
253         put_unaligned_be64(lport->wwpn, &fdisc->fl_wwpn);
254         put_unaligned_be64(lport->wwnn, &fdisc->fl_wwnn);
255         sp = &fdisc->fl_csp;
256         sp->sp_hi_ver = 0x20;
257         sp->sp_lo_ver = 0x20;
258         sp->sp_bb_cred = htons(10);     /* this gets set by gateway */
259         sp->sp_bb_data = htons((u16) lport->mfs);
260         cp = &fdisc->fl_cssp[3 - 1];    /* class 3 parameters */
261         cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
262 }
263
264 /**
265  * fc_logo_fill - Fill in a logo request frame.
266  */
267 static inline void fc_logo_fill(struct fc_lport *lport, struct fc_frame *fp)
268 {
269         struct fc_els_logo *logo;
270
271         logo = fc_frame_payload_get(fp, sizeof(*logo));
272         memset(logo, 0, sizeof(*logo));
273         logo->fl_cmd = ELS_LOGO;
274         hton24(logo->fl_n_port_id, lport->port_id);
275         logo->fl_n_port_wwn = htonll(lport->wwpn);
276 }
277
278 /**
279  * fc_rtv_fill - Fill in RTV (read timeout value) request frame.
280  */
281 static inline void fc_rtv_fill(struct fc_lport *lport, struct fc_frame *fp)
282 {
283         struct fc_els_rtv *rtv;
284
285         rtv = fc_frame_payload_get(fp, sizeof(*rtv));
286         memset(rtv, 0, sizeof(*rtv));
287         rtv->rtv_cmd = ELS_RTV;
288 }
289
290 /**
291  * fc_rec_fill - Fill in rec request frame
292  */
293 static inline void fc_rec_fill(struct fc_lport *lport, struct fc_frame *fp)
294 {
295         struct fc_els_rec *rec;
296         struct fc_exch *ep = fc_seq_exch(fr_seq(fp));
297
298         rec = fc_frame_payload_get(fp, sizeof(*rec));
299         memset(rec, 0, sizeof(*rec));
300         rec->rec_cmd = ELS_REC;
301         hton24(rec->rec_s_id, lport->port_id);
302         rec->rec_ox_id = htons(ep->oxid);
303         rec->rec_rx_id = htons(ep->rxid);
304 }
305
306 /**
307  * fc_prli_fill - Fill in prli request frame
308  */
309 static inline void fc_prli_fill(struct fc_lport *lport, struct fc_frame *fp)
310 {
311         struct {
312                 struct fc_els_prli prli;
313                 struct fc_els_spp spp;
314         } *pp;
315
316         pp = fc_frame_payload_get(fp, sizeof(*pp));
317         memset(pp, 0, sizeof(*pp));
318         pp->prli.prli_cmd = ELS_PRLI;
319         pp->prli.prli_spp_len = sizeof(struct fc_els_spp);
320         pp->prli.prli_len = htons(sizeof(*pp));
321         pp->spp.spp_type = FC_TYPE_FCP;
322         pp->spp.spp_flags = FC_SPP_EST_IMG_PAIR;
323         pp->spp.spp_params = htonl(lport->service_params);
324 }
325
326 /**
327  * fc_scr_fill - Fill in a scr request frame.
328  */
329 static inline void fc_scr_fill(struct fc_lport *lport, struct fc_frame *fp)
330 {
331         struct fc_els_scr *scr;
332
333         scr = fc_frame_payload_get(fp, sizeof(*scr));
334         memset(scr, 0, sizeof(*scr));
335         scr->scr_cmd = ELS_SCR;
336         scr->scr_reg_func = ELS_SCRF_FULL;
337 }
338
339 /**
340  * fc_els_fill - Fill in an ELS  request frame
341  */
342 static inline int fc_els_fill(struct fc_lport *lport,
343                        u32 did,
344                        struct fc_frame *fp, unsigned int op,
345                        enum fc_rctl *r_ctl, enum fc_fh_type *fh_type)
346 {
347         switch (op) {
348         case ELS_ADISC:
349                 fc_adisc_fill(lport, fp);
350                 break;
351
352         case ELS_PLOGI:
353                 fc_plogi_fill(lport, fp, ELS_PLOGI);
354                 break;
355
356         case ELS_FLOGI:
357                 fc_flogi_fill(lport, fp);
358                 break;
359
360         case ELS_FDISC:
361                 fc_fdisc_fill(lport, fp);
362                 break;
363
364         case ELS_LOGO:
365                 fc_logo_fill(lport, fp);
366                 break;
367
368         case ELS_RTV:
369                 fc_rtv_fill(lport, fp);
370                 break;
371
372         case ELS_REC:
373                 fc_rec_fill(lport, fp);
374                 break;
375
376         case ELS_PRLI:
377                 fc_prli_fill(lport, fp);
378                 break;
379
380         case ELS_SCR:
381                 fc_scr_fill(lport, fp);
382                 break;
383
384         default:
385                 return -EINVAL;
386         }
387
388         *r_ctl = FC_RCTL_ELS_REQ;
389         *fh_type = FC_TYPE_ELS;
390         return 0;
391 }
392 #endif /* _FC_ENCODE_H_ */