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