fc_encode.h 10 KB

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