fc_encode.h 9.7 KB

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