fc_encode.h 11 KB

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