xref: /openbmc/linux/drivers/scsi/libfc/fc_encode.h (revision 0bf49ffb)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright(c) 2008 Intel Corporation. All rights reserved.
4  *
5  * Maintained at www.Open-FCoE.org
6  */
7 
8 #ifndef _FC_ENCODE_H_
9 #define _FC_ENCODE_H_
10 #include <asm/unaligned.h>
11 #include <linux/utsname.h>
12 #include <scsi/fc/fc_ms.h>
13 
14 /*
15  * F_CTL values for simple requests and responses.
16  */
17 #define FC_FCTL_REQ	(FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)
18 #define FC_FCTL_RESP	(FC_FC_EX_CTX | FC_FC_LAST_SEQ | \
19 			FC_FC_END_SEQ | FC_FC_SEQ_INIT)
20 
21 struct fc_ns_rft {
22 	struct fc_ns_fid fid;	/* port ID object */
23 	struct fc_ns_fts fts;	/* FC4-types object */
24 };
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_rff_id rff;
33 		struct fc_ns_fid fid;
34 		struct fc_ns_rsnn snn;
35 		struct fc_ns_rspn spn;
36 		struct fc_fdmi_rhba rhba;
37 		struct fc_fdmi_rpa  rpa;
38 		struct fc_fdmi_dprt dprt;
39 		struct fc_fdmi_dhba dhba;
40 	} payload;
41 };
42 
43 /**
44  * fc_adisc_fill() - Fill in adisc request frame
45  * @lport: local port.
46  * @fp: fc frame where payload will be placed.
47  */
48 static inline void fc_adisc_fill(struct fc_lport *lport, struct fc_frame *fp)
49 {
50 	struct fc_els_adisc *adisc;
51 
52 	adisc = fc_frame_payload_get(fp, sizeof(*adisc));
53 	memset(adisc, 0, sizeof(*adisc));
54 	adisc->adisc_cmd = ELS_ADISC;
55 	put_unaligned_be64(lport->wwpn, &adisc->adisc_wwpn);
56 	put_unaligned_be64(lport->wwnn, &adisc->adisc_wwnn);
57 	hton24(adisc->adisc_port_id, lport->port_id);
58 }
59 
60 /**
61  * fc_ct_hdr_fill- fills ct header and reset ct payload
62  * returns pointer to ct request.
63  */
64 static inline struct fc_ct_req *fc_ct_hdr_fill(const struct fc_frame *fp,
65 					       unsigned int op, size_t req_size,
66 					       enum fc_ct_fs_type fs_type,
67 					       u8 subtype)
68 {
69 	struct fc_ct_req *ct;
70 	size_t ct_plen;
71 
72 	ct_plen  = sizeof(struct fc_ct_hdr) + req_size;
73 	ct = fc_frame_payload_get(fp, ct_plen);
74 	memset(ct, 0, ct_plen);
75 	ct->hdr.ct_rev = FC_CT_REV;
76 	ct->hdr.ct_fs_type = fs_type;
77 	ct->hdr.ct_fs_subtype = subtype;
78 	ct->hdr.ct_cmd = htons((u16) op);
79 	return ct;
80 }
81 
82 /**
83  * fc_ct_ns_fill() - Fill in a name service request frame
84  * @lport: local port.
85  * @fc_id: FC_ID of non-destination rport for GPN_ID and similar inquiries.
86  * @fp: frame to contain payload.
87  * @op: CT opcode.
88  * @r_ctl: pointer to FC header R_CTL.
89  * @fh_type: pointer to FC-4 type.
90  */
91 static inline int fc_ct_ns_fill(struct fc_lport *lport,
92 		      u32 fc_id, struct fc_frame *fp,
93 		      unsigned int op, enum fc_rctl *r_ctl,
94 		      enum fc_fh_type *fh_type)
95 {
96 	struct fc_ct_req *ct;
97 	size_t len;
98 
99 	switch (op) {
100 	case FC_NS_GPN_FT:
101 		ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_gid_ft),
102 				    FC_FST_DIR, FC_NS_SUBTYPE);
103 		ct->payload.gid.fn_fc4_type = FC_TYPE_FCP;
104 		break;
105 
106 	case FC_NS_GPN_ID:
107 		ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_fid),
108 				    FC_FST_DIR, FC_NS_SUBTYPE);
109 		ct->payload.gid.fn_fc4_type = FC_TYPE_FCP;
110 		hton24(ct->payload.fid.fp_fid, fc_id);
111 		break;
112 
113 	case FC_NS_RFT_ID:
114 		ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rft),
115 				    FC_FST_DIR, FC_NS_SUBTYPE);
116 		hton24(ct->payload.rft.fid.fp_fid, lport->port_id);
117 		ct->payload.rft.fts = lport->fcts;
118 		break;
119 
120 	case FC_NS_RFF_ID:
121 		ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rff_id),
122 				    FC_FST_DIR, FC_NS_SUBTYPE);
123 		hton24(ct->payload.rff.fr_fid.fp_fid, lport->port_id);
124 		ct->payload.rff.fr_type = FC_TYPE_FCP;
125 		if (lport->service_params & FCP_SPPF_INIT_FCN)
126 			ct->payload.rff.fr_feat = FCP_FEAT_INIT;
127 		if (lport->service_params & FCP_SPPF_TARG_FCN)
128 			ct->payload.rff.fr_feat |= FCP_FEAT_TARG;
129 		break;
130 
131 	case FC_NS_RNN_ID:
132 		ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rn_id),
133 				    FC_FST_DIR, FC_NS_SUBTYPE);
134 		hton24(ct->payload.rn.fr_fid.fp_fid, lport->port_id);
135 		put_unaligned_be64(lport->wwnn, &ct->payload.rn.fr_wwn);
136 		break;
137 
138 	case FC_NS_RSPN_ID:
139 		len = strnlen(fc_host_symbolic_name(lport->host), 255);
140 		ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len,
141 				    FC_FST_DIR, FC_NS_SUBTYPE);
142 		hton24(ct->payload.spn.fr_fid.fp_fid, lport->port_id);
143 		strncpy(ct->payload.spn.fr_name,
144 			fc_host_symbolic_name(lport->host), len);
145 		ct->payload.spn.fr_name_len = len;
146 		break;
147 
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 				    FC_FST_DIR, FC_NS_SUBTYPE);
152 		put_unaligned_be64(lport->wwnn, &ct->payload.snn.fr_wwn);
153 		strncpy(ct->payload.snn.fr_name,
154 			fc_host_symbolic_name(lport->host), len);
155 		ct->payload.snn.fr_name_len = len;
156 		break;
157 
158 	default:
159 		return -EINVAL;
160 	}
161 	*r_ctl = FC_RCTL_DD_UNSOL_CTL;
162 	*fh_type = FC_TYPE_CT;
163 	return 0;
164 }
165 
166 static inline void fc_ct_ms_fill_attr(struct fc_fdmi_attr_entry *entry,
167 				    const char *in, size_t len)
168 {
169 	int copied = strscpy(entry->value, in, len);
170 	if (copied > 0)
171 		memset(entry->value, copied, len - copied);
172 }
173 
174 /**
175  * fc_ct_ms_fill() - Fill in a mgmt service request frame
176  * @lport: local port.
177  * @fc_id: FC_ID of non-destination rport for GPN_ID and similar inquiries.
178  * @fp: frame to contain payload.
179  * @op: CT opcode.
180  * @r_ctl: pointer to FC header R_CTL.
181  * @fh_type: pointer to FC-4 type.
182  */
183 static inline int fc_ct_ms_fill(struct fc_lport *lport,
184 		      u32 fc_id, struct fc_frame *fp,
185 		      unsigned int op, enum fc_rctl *r_ctl,
186 		      enum fc_fh_type *fh_type)
187 {
188 	struct fc_ct_req *ct;
189 	size_t len;
190 	struct fc_fdmi_attr_entry *entry;
191 	struct fs_fdmi_attrs *hba_attrs;
192 	int numattrs = 0;
193 
194 	switch (op) {
195 	case FC_FDMI_RHBA:
196 		numattrs = 10;
197 		len = sizeof(struct fc_fdmi_rhba);
198 		len -= sizeof(struct fc_fdmi_attr_entry);
199 		len += (numattrs * FC_FDMI_ATTR_ENTRY_HEADER_LEN);
200 		len += FC_FDMI_HBA_ATTR_NODENAME_LEN;
201 		len += FC_FDMI_HBA_ATTR_MANUFACTURER_LEN;
202 		len += FC_FDMI_HBA_ATTR_SERIALNUMBER_LEN;
203 		len += FC_FDMI_HBA_ATTR_MODEL_LEN;
204 		len += FC_FDMI_HBA_ATTR_MODELDESCR_LEN;
205 		len += FC_FDMI_HBA_ATTR_HARDWAREVERSION_LEN;
206 		len += FC_FDMI_HBA_ATTR_DRIVERVERSION_LEN;
207 		len += FC_FDMI_HBA_ATTR_OPTIONROMVERSION_LEN;
208 		len += FC_FDMI_HBA_ATTR_FIRMWAREVERSION_LEN;
209 		len += FC_FDMI_HBA_ATTR_OSNAMEVERSION_LEN;
210 		ct = fc_ct_hdr_fill(fp, op, len, FC_FST_MGMT,
211 				    FC_FDMI_SUBTYPE);
212 
213 		/* HBA Identifier */
214 		put_unaligned_be64(lport->wwpn, &ct->payload.rhba.hbaid.id);
215 		/* Number of Ports - always 1 */
216 		put_unaligned_be32(1, &ct->payload.rhba.port.numport);
217 		/* Port Name */
218 		put_unaligned_be64(lport->wwpn,
219 				   &ct->payload.rhba.port.port[0].portname);
220 
221 		/* HBA Attributes */
222 		put_unaligned_be32(numattrs,
223 				   &ct->payload.rhba.hba_attrs.numattrs);
224 		hba_attrs = &ct->payload.rhba.hba_attrs;
225 		entry = (struct fc_fdmi_attr_entry *)hba_attrs->attr;
226 		/* NodeName*/
227 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
228 		len += FC_FDMI_HBA_ATTR_NODENAME_LEN;
229 		put_unaligned_be16(FC_FDMI_HBA_ATTR_NODENAME,
230 				   &entry->type);
231 		put_unaligned_be16(len, &entry->len);
232 		put_unaligned_be64(lport->wwnn,
233 				   (__be64 *)&entry->value[0]);
234 
235 		/* Manufacturer */
236 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
237 					FC_FDMI_HBA_ATTR_NODENAME_LEN);
238 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
239 		len += FC_FDMI_HBA_ATTR_MANUFACTURER_LEN;
240 		put_unaligned_be16(FC_FDMI_HBA_ATTR_MANUFACTURER,
241 				   &entry->type);
242 		put_unaligned_be16(len, &entry->len);
243 		fc_ct_ms_fill_attr(entry,
244 			fc_host_manufacturer(lport->host),
245 			FC_FDMI_HBA_ATTR_MANUFACTURER_LEN);
246 
247 		/* SerialNumber */
248 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
249 					FC_FDMI_HBA_ATTR_MANUFACTURER_LEN);
250 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
251 		len += FC_FDMI_HBA_ATTR_SERIALNUMBER_LEN;
252 		put_unaligned_be16(FC_FDMI_HBA_ATTR_SERIALNUMBER,
253 				   &entry->type);
254 		put_unaligned_be16(len, &entry->len);
255 		fc_ct_ms_fill_attr(entry,
256 			fc_host_serial_number(lport->host),
257 			FC_FDMI_HBA_ATTR_SERIALNUMBER_LEN);
258 
259 		/* Model */
260 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
261 					FC_FDMI_HBA_ATTR_SERIALNUMBER_LEN);
262 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
263 		len += FC_FDMI_HBA_ATTR_MODEL_LEN;
264 		put_unaligned_be16(FC_FDMI_HBA_ATTR_MODEL,
265 				   &entry->type);
266 		put_unaligned_be16(len, &entry->len);
267 		fc_ct_ms_fill_attr(entry,
268 			fc_host_model(lport->host),
269 			FC_FDMI_HBA_ATTR_MODEL_LEN);
270 
271 		/* Model Description */
272 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
273 					FC_FDMI_HBA_ATTR_MODEL_LEN);
274 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
275 		len += FC_FDMI_HBA_ATTR_MODELDESCR_LEN;
276 		put_unaligned_be16(FC_FDMI_HBA_ATTR_MODELDESCRIPTION,
277 				   &entry->type);
278 		put_unaligned_be16(len, &entry->len);
279 		fc_ct_ms_fill_attr(entry,
280 			fc_host_model_description(lport->host),
281 			FC_FDMI_HBA_ATTR_MODELDESCR_LEN);
282 
283 		/* Hardware Version */
284 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
285 					FC_FDMI_HBA_ATTR_MODELDESCR_LEN);
286 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
287 		len += FC_FDMI_HBA_ATTR_HARDWAREVERSION_LEN;
288 		put_unaligned_be16(FC_FDMI_HBA_ATTR_HARDWAREVERSION,
289 				   &entry->type);
290 		put_unaligned_be16(len, &entry->len);
291 		fc_ct_ms_fill_attr(entry,
292 			fc_host_hardware_version(lport->host),
293 			FC_FDMI_HBA_ATTR_HARDWAREVERSION_LEN);
294 
295 		/* Driver Version */
296 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
297 					FC_FDMI_HBA_ATTR_HARDWAREVERSION_LEN);
298 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
299 		len += FC_FDMI_HBA_ATTR_DRIVERVERSION_LEN;
300 		put_unaligned_be16(FC_FDMI_HBA_ATTR_DRIVERVERSION,
301 				   &entry->type);
302 		put_unaligned_be16(len, &entry->len);
303 		fc_ct_ms_fill_attr(entry,
304 			fc_host_driver_version(lport->host),
305 			FC_FDMI_HBA_ATTR_DRIVERVERSION_LEN);
306 
307 		/* OptionROM Version */
308 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
309 					FC_FDMI_HBA_ATTR_DRIVERVERSION_LEN);
310 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
311 		len += FC_FDMI_HBA_ATTR_OPTIONROMVERSION_LEN;
312 		put_unaligned_be16(FC_FDMI_HBA_ATTR_OPTIONROMVERSION,
313 				   &entry->type);
314 		put_unaligned_be16(len, &entry->len);
315 		fc_ct_ms_fill_attr(entry,
316 			fc_host_optionrom_version(lport->host),
317 			FC_FDMI_HBA_ATTR_OPTIONROMVERSION_LEN);
318 
319 		/* Firmware Version */
320 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
321 					FC_FDMI_HBA_ATTR_OPTIONROMVERSION_LEN);
322 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
323 		len += FC_FDMI_HBA_ATTR_FIRMWAREVERSION_LEN;
324 		put_unaligned_be16(FC_FDMI_HBA_ATTR_FIRMWAREVERSION,
325 				   &entry->type);
326 		put_unaligned_be16(len, &entry->len);
327 		fc_ct_ms_fill_attr(entry,
328 			fc_host_firmware_version(lport->host),
329 			FC_FDMI_HBA_ATTR_FIRMWAREVERSION_LEN);
330 
331 		/* OS Name and Version */
332 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
333 					FC_FDMI_HBA_ATTR_FIRMWAREVERSION_LEN);
334 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
335 		len += FC_FDMI_HBA_ATTR_OSNAMEVERSION_LEN;
336 		put_unaligned_be16(FC_FDMI_HBA_ATTR_OSNAMEVERSION,
337 				   &entry->type);
338 		put_unaligned_be16(len, &entry->len);
339 		snprintf((char *)&entry->value,
340 			FC_FDMI_HBA_ATTR_OSNAMEVERSION_LEN,
341 			"%s v%s",
342 			init_utsname()->sysname,
343 			init_utsname()->release);
344 		break;
345 	case FC_FDMI_RPA:
346 		numattrs = 6;
347 		len = sizeof(struct fc_fdmi_rpa);
348 		len -= sizeof(struct fc_fdmi_attr_entry);
349 		len += (numattrs * FC_FDMI_ATTR_ENTRY_HEADER_LEN);
350 		len += FC_FDMI_PORT_ATTR_FC4TYPES_LEN;
351 		len += FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN;
352 		len += FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN;
353 		len += FC_FDMI_PORT_ATTR_MAXFRAMESIZE_LEN;
354 		len += FC_FDMI_PORT_ATTR_OSDEVICENAME_LEN;
355 		len += FC_FDMI_PORT_ATTR_HOSTNAME_LEN;
356 		ct = fc_ct_hdr_fill(fp, op, len, FC_FST_MGMT,
357 				    FC_FDMI_SUBTYPE);
358 
359 		/* Port Name */
360 		put_unaligned_be64(lport->wwpn,
361 				   &ct->payload.rpa.port.portname);
362 
363 		/* Port Attributes */
364 		put_unaligned_be32(numattrs,
365 				   &ct->payload.rpa.hba_attrs.numattrs);
366 
367 		hba_attrs = &ct->payload.rpa.hba_attrs;
368 		entry = (struct fc_fdmi_attr_entry *)hba_attrs->attr;
369 
370 		/* FC4 types */
371 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
372 		len += FC_FDMI_PORT_ATTR_FC4TYPES_LEN;
373 		put_unaligned_be16(FC_FDMI_PORT_ATTR_FC4TYPES,
374 				   &entry->type);
375 		put_unaligned_be16(len, &entry->len);
376 		memcpy(&entry->value, fc_host_supported_fc4s(lport->host),
377 		       FC_FDMI_PORT_ATTR_FC4TYPES_LEN);
378 
379 		/* Supported Speed */
380 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
381 					FC_FDMI_PORT_ATTR_FC4TYPES_LEN);
382 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
383 		len += FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN;
384 		put_unaligned_be16(FC_FDMI_PORT_ATTR_SUPPORTEDSPEED,
385 				   &entry->type);
386 		put_unaligned_be16(len, &entry->len);
387 
388 		put_unaligned_be32(fc_host_supported_speeds(lport->host),
389 				   &entry->value);
390 
391 		/* Current Port Speed */
392 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
393 					FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN);
394 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
395 		len += FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN;
396 		put_unaligned_be16(FC_FDMI_PORT_ATTR_CURRENTPORTSPEED,
397 				   &entry->type);
398 		put_unaligned_be16(len, &entry->len);
399 		put_unaligned_be32(lport->link_speed,
400 				   &entry->value);
401 
402 		/* Max Frame Size */
403 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
404 					FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN);
405 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
406 		len += FC_FDMI_PORT_ATTR_MAXFRAMESIZE_LEN;
407 		put_unaligned_be16(FC_FDMI_PORT_ATTR_MAXFRAMESIZE,
408 				   &entry->type);
409 		put_unaligned_be16(len, &entry->len);
410 		put_unaligned_be32(fc_host_maxframe_size(lport->host),
411 				   &entry->value);
412 
413 		/* OS Device Name */
414 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
415 					FC_FDMI_PORT_ATTR_MAXFRAMESIZE_LEN);
416 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
417 		len += FC_FDMI_PORT_ATTR_OSDEVICENAME_LEN;
418 		put_unaligned_be16(FC_FDMI_PORT_ATTR_OSDEVICENAME,
419 				   &entry->type);
420 		put_unaligned_be16(len, &entry->len);
421 		/* Use the sysfs device name */
422 		fc_ct_ms_fill_attr(entry,
423 			dev_name(&lport->host->shost_gendev),
424 			strnlen(dev_name(&lport->host->shost_gendev),
425 				FC_FDMI_PORT_ATTR_HOSTNAME_LEN));
426 
427 		/* Host Name */
428 		entry = (struct fc_fdmi_attr_entry *)((char *)entry->value +
429 					FC_FDMI_PORT_ATTR_OSDEVICENAME_LEN);
430 		len = FC_FDMI_ATTR_ENTRY_HEADER_LEN;
431 		len += FC_FDMI_PORT_ATTR_HOSTNAME_LEN;
432 		put_unaligned_be16(FC_FDMI_PORT_ATTR_HOSTNAME,
433 				   &entry->type);
434 		put_unaligned_be16(len, &entry->len);
435 		if (strlen(fc_host_system_hostname(lport->host)))
436 			fc_ct_ms_fill_attr(entry,
437 				fc_host_system_hostname(lport->host),
438 				strnlen(fc_host_system_hostname(lport->host),
439 					FC_FDMI_PORT_ATTR_HOSTNAME_LEN));
440 		else
441 			fc_ct_ms_fill_attr(entry,
442 				init_utsname()->nodename,
443 				FC_FDMI_PORT_ATTR_HOSTNAME_LEN);
444 		break;
445 	case FC_FDMI_DPRT:
446 		len = sizeof(struct fc_fdmi_dprt);
447 		ct = fc_ct_hdr_fill(fp, op, len, FC_FST_MGMT,
448 				    FC_FDMI_SUBTYPE);
449 		/* Port Name */
450 		put_unaligned_be64(lport->wwpn,
451 				   &ct->payload.dprt.port.portname);
452 		break;
453 	case FC_FDMI_DHBA:
454 		len = sizeof(struct fc_fdmi_dhba);
455 		ct = fc_ct_hdr_fill(fp, op, len, FC_FST_MGMT,
456 				    FC_FDMI_SUBTYPE);
457 		/* HBA Identifier */
458 		put_unaligned_be64(lport->wwpn, &ct->payload.dhba.hbaid.id);
459 		break;
460 	default:
461 		return -EINVAL;
462 	}
463 	*r_ctl = FC_RCTL_DD_UNSOL_CTL;
464 	*fh_type = FC_TYPE_CT;
465 	return 0;
466 }
467 
468 /**
469  * fc_ct_fill() - Fill in a common transport service request frame
470  * @lport: local port.
471  * @fc_id: FC_ID of non-destination rport for GPN_ID and similar inquiries.
472  * @fp: frame to contain payload.
473  * @op: CT opcode.
474  * @r_ctl: pointer to FC header R_CTL.
475  * @fh_type: pointer to FC-4 type.
476  */
477 static inline int fc_ct_fill(struct fc_lport *lport,
478 		      u32 fc_id, struct fc_frame *fp,
479 		      unsigned int op, enum fc_rctl *r_ctl,
480 		      enum fc_fh_type *fh_type, u32 *did)
481 {
482 	int rc = -EINVAL;
483 
484 	switch (fc_id) {
485 	case FC_FID_MGMT_SERV:
486 		rc = fc_ct_ms_fill(lport, fc_id, fp, op, r_ctl, fh_type);
487 		*did = FC_FID_MGMT_SERV;
488 		break;
489 	case FC_FID_DIR_SERV:
490 	default:
491 		rc = fc_ct_ns_fill(lport, fc_id, fp, op, r_ctl, fh_type);
492 		*did = FC_FID_DIR_SERV;
493 		break;
494 	}
495 
496 	return rc;
497 }
498 /**
499  * fc_plogi_fill - Fill in plogi request frame
500  */
501 static inline void fc_plogi_fill(struct fc_lport *lport, struct fc_frame *fp,
502 				 unsigned int op)
503 {
504 	struct fc_els_flogi *plogi;
505 	struct fc_els_csp *csp;
506 	struct fc_els_cssp *cp;
507 
508 	plogi = fc_frame_payload_get(fp, sizeof(*plogi));
509 	memset(plogi, 0, sizeof(*plogi));
510 	plogi->fl_cmd = (u8) op;
511 	put_unaligned_be64(lport->wwpn, &plogi->fl_wwpn);
512 	put_unaligned_be64(lport->wwnn, &plogi->fl_wwnn);
513 
514 	csp = &plogi->fl_csp;
515 	csp->sp_hi_ver = 0x20;
516 	csp->sp_lo_ver = 0x20;
517 	csp->sp_bb_cred = htons(10);	/* this gets set by gateway */
518 	csp->sp_bb_data = htons((u16) lport->mfs);
519 	cp = &plogi->fl_cssp[3 - 1];	/* class 3 parameters */
520 	cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
521 	csp->sp_features = htons(FC_SP_FT_CIRO);
522 	csp->sp_tot_seq = htons(255);	/* seq. we accept */
523 	csp->sp_rel_off = htons(0x1f);
524 	csp->sp_e_d_tov = htonl(lport->e_d_tov);
525 
526 	cp->cp_rdfs = htons((u16) lport->mfs);
527 	cp->cp_con_seq = htons(255);
528 	cp->cp_open_seq = 1;
529 }
530 
531 /**
532  * fc_flogi_fill - Fill in a flogi request frame.
533  */
534 static inline void fc_flogi_fill(struct fc_lport *lport, struct fc_frame *fp)
535 {
536 	struct fc_els_csp *sp;
537 	struct fc_els_cssp *cp;
538 	struct fc_els_flogi *flogi;
539 
540 	flogi = fc_frame_payload_get(fp, sizeof(*flogi));
541 	memset(flogi, 0, sizeof(*flogi));
542 	flogi->fl_cmd = (u8) ELS_FLOGI;
543 	put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
544 	put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
545 	sp = &flogi->fl_csp;
546 	sp->sp_hi_ver = 0x20;
547 	sp->sp_lo_ver = 0x20;
548 	sp->sp_bb_cred = htons(10);	/* this gets set by gateway */
549 	sp->sp_bb_data = htons((u16) lport->mfs);
550 	cp = &flogi->fl_cssp[3 - 1];	/* class 3 parameters */
551 	cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
552 	if (lport->does_npiv)
553 		sp->sp_features = htons(FC_SP_FT_NPIV);
554 }
555 
556 /**
557  * fc_fdisc_fill - Fill in a fdisc request frame.
558  */
559 static inline void fc_fdisc_fill(struct fc_lport *lport, struct fc_frame *fp)
560 {
561 	struct fc_els_csp *sp;
562 	struct fc_els_cssp *cp;
563 	struct fc_els_flogi *fdisc;
564 
565 	fdisc = fc_frame_payload_get(fp, sizeof(*fdisc));
566 	memset(fdisc, 0, sizeof(*fdisc));
567 	fdisc->fl_cmd = (u8) ELS_FDISC;
568 	put_unaligned_be64(lport->wwpn, &fdisc->fl_wwpn);
569 	put_unaligned_be64(lport->wwnn, &fdisc->fl_wwnn);
570 	sp = &fdisc->fl_csp;
571 	sp->sp_hi_ver = 0x20;
572 	sp->sp_lo_ver = 0x20;
573 	sp->sp_bb_cred = htons(10);	/* this gets set by gateway */
574 	sp->sp_bb_data = htons((u16) lport->mfs);
575 	cp = &fdisc->fl_cssp[3 - 1];	/* class 3 parameters */
576 	cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
577 }
578 
579 /**
580  * fc_logo_fill - Fill in a logo request frame.
581  */
582 static inline void fc_logo_fill(struct fc_lport *lport, struct fc_frame *fp)
583 {
584 	struct fc_els_logo *logo;
585 
586 	logo = fc_frame_payload_get(fp, sizeof(*logo));
587 	memset(logo, 0, sizeof(*logo));
588 	logo->fl_cmd = ELS_LOGO;
589 	hton24(logo->fl_n_port_id, lport->port_id);
590 	logo->fl_n_port_wwn = htonll(lport->wwpn);
591 }
592 
593 /**
594  * fc_rtv_fill - Fill in RTV (read timeout value) request frame.
595  */
596 static inline void fc_rtv_fill(struct fc_lport *lport, struct fc_frame *fp)
597 {
598 	struct fc_els_rtv *rtv;
599 
600 	rtv = fc_frame_payload_get(fp, sizeof(*rtv));
601 	memset(rtv, 0, sizeof(*rtv));
602 	rtv->rtv_cmd = ELS_RTV;
603 }
604 
605 /**
606  * fc_rec_fill - Fill in rec request frame
607  */
608 static inline void fc_rec_fill(struct fc_lport *lport, struct fc_frame *fp)
609 {
610 	struct fc_els_rec *rec;
611 	struct fc_exch *ep = fc_seq_exch(fr_seq(fp));
612 
613 	rec = fc_frame_payload_get(fp, sizeof(*rec));
614 	memset(rec, 0, sizeof(*rec));
615 	rec->rec_cmd = ELS_REC;
616 	hton24(rec->rec_s_id, lport->port_id);
617 	rec->rec_ox_id = htons(ep->oxid);
618 	rec->rec_rx_id = htons(ep->rxid);
619 }
620 
621 /**
622  * fc_prli_fill - Fill in prli request frame
623  */
624 static inline void fc_prli_fill(struct fc_lport *lport, struct fc_frame *fp)
625 {
626 	struct {
627 		struct fc_els_prli prli;
628 		struct fc_els_spp spp;
629 	} *pp;
630 
631 	pp = fc_frame_payload_get(fp, sizeof(*pp));
632 	memset(pp, 0, sizeof(*pp));
633 	pp->prli.prli_cmd = ELS_PRLI;
634 	pp->prli.prli_spp_len = sizeof(struct fc_els_spp);
635 	pp->prli.prli_len = htons(sizeof(*pp));
636 	pp->spp.spp_type = FC_TYPE_FCP;
637 	pp->spp.spp_flags = FC_SPP_EST_IMG_PAIR;
638 	pp->spp.spp_params = htonl(lport->service_params);
639 }
640 
641 /**
642  * fc_scr_fill - Fill in a scr request frame.
643  */
644 static inline void fc_scr_fill(struct fc_lport *lport, struct fc_frame *fp)
645 {
646 	struct fc_els_scr *scr;
647 
648 	scr = fc_frame_payload_get(fp, sizeof(*scr));
649 	memset(scr, 0, sizeof(*scr));
650 	scr->scr_cmd = ELS_SCR;
651 	scr->scr_reg_func = ELS_SCRF_FULL;
652 }
653 
654 /**
655  * fc_els_fill - Fill in an ELS  request frame
656  */
657 static inline int fc_els_fill(struct fc_lport *lport,
658 		       u32 did,
659 		       struct fc_frame *fp, unsigned int op,
660 		       enum fc_rctl *r_ctl, enum fc_fh_type *fh_type)
661 {
662 	switch (op) {
663 	case ELS_ADISC:
664 		fc_adisc_fill(lport, fp);
665 		break;
666 
667 	case ELS_PLOGI:
668 		fc_plogi_fill(lport, fp, ELS_PLOGI);
669 		break;
670 
671 	case ELS_FLOGI:
672 		fc_flogi_fill(lport, fp);
673 		break;
674 
675 	case ELS_FDISC:
676 		fc_fdisc_fill(lport, fp);
677 		break;
678 
679 	case ELS_LOGO:
680 		fc_logo_fill(lport, fp);
681 		break;
682 
683 	case ELS_RTV:
684 		fc_rtv_fill(lport, fp);
685 		break;
686 
687 	case ELS_REC:
688 		fc_rec_fill(lport, fp);
689 		break;
690 
691 	case ELS_PRLI:
692 		fc_prli_fill(lport, fp);
693 		break;
694 
695 	case ELS_SCR:
696 		fc_scr_fill(lport, fp);
697 		break;
698 
699 	default:
700 		return -EINVAL;
701 	}
702 
703 	*r_ctl = FC_RCTL_ELS_REQ;
704 	*fh_type = FC_TYPE_ELS;
705 	return 0;
706 }
707 #endif /* _FC_ENCODE_H_ */
708