xref: /illumos-gate/usr/src/uts/common/sys/fct.h (revision c686756220120076a07be0dcce54be698101a3d1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
24  * Copyright 2020 RackTop Systems, Inc.
25  */
26 #ifndef	_FCT_H
27 #define	_FCT_H
28 
29 /*
30  * Definitions for common FC Target.
31  */
32 #include <sys/note.h>
33 #include <sys/stmf_defines.h>
34 #include <sys/fct_defines.h>
35 #include <sys/portif.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 typedef enum fct_struct_id {
42 	FCT_STRUCT_LOCAL_PORT = 1,
43 	FCT_STRUCT_REMOTE_PORT,
44 	FCT_STRUCT_CMD_RCVD_ELS,
45 	FCT_STRUCT_CMD_SOL_ELS,
46 	FCT_STRUCT_CMD_SOL_CT,
47 	FCT_STRUCT_CMD_RCVD_ABTS,
48 	FCT_STRUCT_CMD_FCP_XCHG,
49 	FCT_STRUCT_DBUF_STORE,
50 
51 	FCT_MAX_STRUCT_IDS
52 } fct_struct_id_t;
53 
54 typedef struct fct_remote_port {
55 	void		*rp_fct_private;
56 	void		*rp_fca_private;
57 
58 	struct fct_local_port *rp_port;
59 	char		rp_nwwn_str[FC_WWN_BUFLEN];
60 	char		rp_pwwn_str[FC_WWN_BUFLEN];
61 	uint8_t		rp_nwwn[FC_WWN_LEN];
62 	uint8_t		rp_pwwn[FC_WWN_LEN];
63 	uint32_t	rp_id;		/* 8 or 24 bit */
64 	uint32_t	rp_hard_address;
65 	uint16_t	rp_handle;
66 } fct_remote_port_t;
67 
68 #define	FCT_HANDLE_NONE	0xffff
69 
70 typedef struct fct_cmd {
71 	void		*cmd_fct_private;
72 	void		*cmd_fca_private;
73 	void		*cmd_specific;
74 
75 	struct fct_local_port	*cmd_port;
76 
77 	/* During cmd porting this can be set to NULL */
78 	struct fct_remote_port	*cmd_rp;
79 
80 	/* To link cmds together for handling things like ABTS. */
81 	struct fct_cmd	*cmd_link;
82 	uint8_t		cmd_type;
83 	uint8_t		cmd_rsvd1;
84 
85 	/* During cmd posting this can be set to FCT_HANDLE_NONE */
86 	uint16_t	cmd_rp_handle;
87 	uint32_t	cmd_handle;
88 	uint32_t	cmd_rportid;
89 	uint32_t	cmd_lportid;
90 	uint32_t	cmd_rsvd2;
91 	uint16_t	cmd_oxid;
92 	uint16_t	cmd_rxid;
93 	fct_status_t	cmd_comp_status;
94 } fct_cmd_t;
95 
96 /*
97  * fcmd_cmd_handle: Bit definitions.
98  *   31		  23	       15	    7	       0
99  *  +--------------+------------+------------+------------+
100  *  | V |uniq_cntr |fca specific|   cmd slot index	  |
101  *  +--------------+------------+------------+------------+
102  * V = handle valid.
103  */
104 #define	CMD_HANDLE_SLOT_INDEX(x)	((x) & 0xffff)
105 #define	CMD_HANDLE_VALID(x)		((x) & 0x80000000)
106 
107 enum fct_cmd_types {
108 	FCT_CMD_FCP_XCHG =	0x0001,
109 	FCT_CMD_RCVD_ELS =	0x0002,
110 	FCT_CMD_SOL_ELS =	0x0004,
111 	FCT_CMD_RCVD_ABTS =	0x0008,
112 	FCT_CMD_SOL_CT =	0x0010,
113 
114 	FCT_CMD_TYPE_ALL =	0xffff
115 };
116 
117 typedef struct fct_els {
118 	uint16_t	els_req_size;
119 	uint16_t	els_resp_size;
120 	uint16_t	els_req_alloc_size;
121 	uint16_t	els_resp_alloc_size;
122 	uint8_t		*els_req_payload;
123 	uint8_t		*els_resp_payload;
124 } fct_els_t;
125 
126 typedef struct fct_sol_ct {
127 	uint16_t	ct_req_size;
128 	uint16_t	ct_resp_size;
129 	uint16_t	ct_req_alloc_size;
130 	uint16_t	ct_resp_alloc_size;
131 	uint8_t		*ct_req_payload;
132 	uint8_t		*ct_resp_payload;
133 } fct_sol_ct_t;
134 
135 typedef struct fct_rcvd_abts {
136 	uint8_t		abts_resp_rctl;	/* Can be BA_ACC or BA_RJT */
137 	uint8_t		abts_state;
138 	uint16_t	rsvd;
139 	uint8_t		abts_resp_payload[12];
140 } fct_rcvd_abts_t;
141 
142 /*
143  * abts state
144  */
145 #define	ABTS_STATE_RECEIVED		0
146 #define	ABTS_STATE_RESPONDED		1
147 #define	ABTS_STATE_COMPLETED		2
148 #define	ABTS_STATE_ABORT_REQUESTED	3
149 #define	ABTS_STATE_ABORT_COMPLETED	4
150 
151 #define	FCHBA_MANUFACTURER_LEN		64
152 #define	FCHBA_SERIAL_NUMBER_LEN		64
153 #define	FCHBA_MODEL_LEN			256
154 #define	FCHBA_MODEL_DESCRIPTION_LEN	256
155 #define	FCHBA_HARDWARE_VERSION_LEN	256
156 #define	FCHBA_DRIVER_VERSION_LEN	256
157 #define	FCHBA_OPTION_ROM_VERSION_LEN	256
158 #define	FCHBA_FIRMWARE_VERSION_LEN	256
159 #define	FCHBA_DRIVER_NAME_LEN		256
160 #define	FCHBA_SYMB_NAME_LEN		255
161 
162 #define	FCT_INFO_LEN			160
163 #define	FCT_TASKQ_NAME_LEN		24
164 
165 #define	FC_TGT_PORT_INFO_CMD		(((uint32_t)'I') << 24)
166 #define	FC_TGT_PORT_RLS			FC_TGT_PORT_INFO_CMD + 0x1
167 
168 typedef struct fct_port_attrs {
169 	char		manufacturer[FCHBA_MANUFACTURER_LEN];
170 	char		serial_number[FCHBA_SERIAL_NUMBER_LEN];
171 	char		model[FCHBA_MODEL_LEN];
172 	char		model_description[FCHBA_MODEL_DESCRIPTION_LEN];
173 	char		hardware_version[FCHBA_HARDWARE_VERSION_LEN];
174 	char		driver_version[FCHBA_DRIVER_VERSION_LEN];
175 	char		option_rom_version[FCHBA_OPTION_ROM_VERSION_LEN];
176 	char		firmware_version[FCHBA_FIRMWARE_VERSION_LEN];
177 	char		driver_name[FCHBA_DRIVER_NAME_LEN];
178 	uint32_t	vendor_specific_id;
179 	uint32_t	supported_cos;
180 	uint32_t	supported_speed;
181 	uint32_t	max_frame_size;
182 } fct_port_attrs_t;
183 
184 typedef struct fct_port_link_status {
185 	uint32_t	LinkFailureCount;
186 	uint32_t	LossOfSyncCount;
187 	uint32_t	LossOfSignalsCount;
188 	uint32_t	PrimitiveSeqProtocolErrorCount;
189 	uint32_t	InvalidTransmissionWordCount;
190 	uint32_t	InvalidCRCCount;
191 } fct_port_link_status_t;
192 
193 typedef struct fct_dbuf_store {
194 	void			*fds_fct_private;
195 	void			*fds_fca_private;
196 	struct stmf_dbuf_store	*fds_ds;
197 
198 	stmf_data_buf_t *(*fds_alloc_data_buf)(struct fct_local_port *port,
199 			    uint32_t size, uint32_t *pminsize, uint32_t flags);
200 	void		(*fds_free_data_buf)(struct fct_dbuf_store *fds,
201 			    stmf_data_buf_t *dbuf);
202 	stmf_status_t	(*fds_setup_dbuf)(struct fct_local_port *port,
203 			    stmf_data_buf_t *dbuf, uint32_t flags);
204 	void		(*fds_teardown_dbuf)(struct fct_dbuf_store *fds,
205 			    stmf_data_buf_t *dbuf);
206 
207 	uint32_t		fds_max_sgl_xfer_len;
208 	uint32_t		fds_copy_threshold;
209 } fct_dbuf_store_t;
210 
211 #define	FCT_FCA_MODREV_1	1
212 
213 typedef struct fct_local_port {
214 	void			*port_fct_private;
215 	void			*port_fca_private;
216 	stmf_local_port_t	*port_lport;
217 
218 	char			port_nwwn_str[FC_WWN_BUFLEN];
219 	char			port_pwwn_str[FC_WWN_BUFLEN];
220 	uint8_t			port_nwwn[FC_WWN_LEN];
221 	uint8_t			port_pwwn[FC_WWN_LEN];
222 	char			*port_default_alias;
223 	char			*port_sym_node_name;
224 	char			*port_sym_port_name;
225 
226 	stmf_port_provider_t	*port_pp;
227 
228 	uint32_t		port_hard_address;
229 	uint16_t		port_max_logins;
230 	uint16_t		port_max_xchges;
231 	uint32_t		port_fca_fcp_cmd_size;
232 	uint32_t		port_fca_rp_private_size;
233 	uint32_t		port_fca_sol_els_private_size;
234 	uint32_t		port_fca_sol_ct_private_size;
235 
236 	/* in milliseconds */
237 	uint32_t		port_fca_abort_timeout;
238 
239 	fct_dbuf_store_t	*port_fds;
240 	fct_status_t		(*port_get_link_info)(
241 		struct fct_local_port *port, struct fct_link_info *li);
242 	fct_status_t		(*port_register_remote_port)(
243 		struct fct_local_port *port, struct fct_remote_port *rp,
244 		struct fct_cmd *login_els);
245 	fct_status_t		(*port_deregister_remote_port)(
246 		struct fct_local_port *port, struct fct_remote_port *rp);
247 	fct_status_t		(*port_send_cmd)(fct_cmd_t *cmd);
248 	fct_status_t		(*port_xfer_scsi_data)(fct_cmd_t *cmd,
249 			stmf_data_buf_t *dbuf, uint32_t flags);
250 	fct_status_t		(*port_send_cmd_response)(fct_cmd_t *cmd,
251 					uint32_t ioflags);
252 	fct_status_t		(*port_abort_cmd)(struct fct_local_port *port,
253 			fct_cmd_t *cmd, uint32_t flags);
254 	void			(*port_ctl)(struct fct_local_port *port,
255 						int cmd, void *arg);
256 	fct_status_t		(*port_flogi_xchg)(struct fct_local_port *port,
257 			struct fct_flogi_xchg *fx);
258 	void			(*port_populate_hba_details)(
259 		struct fct_local_port *port, struct fct_port_attrs *port_attrs);
260 	fct_status_t		(*port_info)(uint32_t cmd,
261 		struct fct_local_port *port, void *arg, uint8_t *buf,
262 		uint32_t *bufsizep);
263 	int		port_fca_version;
264 } fct_local_port_t;
265 
266 /*
267  * Common struct used during FLOGI exchange.
268  */
269 typedef struct fct_flogi_xchg {
270 	uint8_t		fx_op;		/* ELS_OP_FLOGI or ELS_OP_ACC/RJT */
271 	uint8_t		fx_rjt_reason;
272 	uint8_t		fx_rjt_expl;
273 	uint8_t		fx_sec_timeout;	/* Timeout in seconds */
274 	uint32_t	fx_fport:1,	/* 0=N_port, 1=F_port */
275 			rsvd2:31;
276 	uint32_t	fx_sid;		/* 24 bit SID to use */
277 	uint32_t	fx_did;		/* 24 bit DID to use */
278 	uint8_t		fx_pwwn[8];
279 	uint8_t		fx_nwwn[8];
280 } fct_flogi_xchg_t;
281 
282 typedef struct fct_link_info {
283 	uint32_t		portid;
284 	uint8_t			port_topology;
285 	uint8_t			port_speed;
286 
287 	uint8_t			rsvd:5,
288 
289 	/*
290 	 * FCA sets this bit to indicate that fct does not need to do FLOGI
291 	 * because either FCA did the FLOGI or it determined that its a private
292 	 * loop. Setting this bit by FCA is optional.
293 	 */
294 				port_no_fct_flogi:1,
295 
296 	/* FCA sets this bit to indicate that it did FLOGI */
297 				port_fca_flogi_done:1,
298 
299 	/* FCT sets this bit to indicate that it did FLOGI */
300 				port_fct_flogi_done:1;
301 
302 	uint8_t			rsvd1;
303 
304 	/* The fields below are only valid if someone did a successful flogi */
305 	uint8_t			port_rnwwn[8];
306 	uint8_t			port_rpwwn[8];
307 } fct_link_info_t;
308 
309 typedef struct fct_port_stat {
310 	kstat_named_t	link_failure_cnt;
311 	kstat_named_t	loss_of_sync_cnt;
312 	kstat_named_t	loss_of_signals_cnt;
313 	kstat_named_t	prim_seq_protocol_err_cnt;
314 	kstat_named_t	invalid_tx_word_cnt;
315 	kstat_named_t	invalid_crc_cnt;
316 } fct_port_stat_t;
317 
318 /*
319  * port topology
320  */
321 #define	PORT_TOPOLOGY_UNKNOWN		0
322 #define	PORT_TOPOLOGY_PT_TO_PT		1
323 #define	PORT_TOPOLOGY_PRIVATE_LOOP	2
324 #define	PORT_TOPOLOGY_PUBLIC_LOOP	6
325 #define	PORT_TOPOLOGY_FABRIC_PT_TO_PT	5
326 #define	PORT_TOPOLOGY_FABRIC_BIT	4
327 
328 #define	PORT_FLOGI_DONE(li)	(((li)->port_fca_flogi_done) || \
329 					((li)->port_fct_flogi_done))
330 
331 /*
332  * port speed
333  */
334 #define	PORT_SPEED_UNKNOWN		0
335 #define	PORT_SPEED_1G			1
336 #define	PORT_SPEED_2G			2
337 #define	PORT_SPEED_4G			4
338 #define	PORT_SPEED_8G			8
339 #define	PORT_SPEED_10G			16
340 #define	PORT_SPEED_16G			32
341 #define	PORT_SPEED_32G			64
342 
343 /*
344  * Abort commands
345  */
346 #define	FCT_TERMINATE_CMD		1
347 
348 /*
349  * FCT port states.
350  */
351 #define	FCT_STATE_OFFLINE	0
352 #define	FCT_STATE_ONLINING	1
353 #define	FCT_STATE_ONLINE	2
354 #define	FCT_STATE_OFFLINING	3
355 
356 /*
357  * fct ctl commands. These should not conflict with stmf ctl commands
358  */
359 #define	FCT_CMD_PORT_ONLINE		(STMF_LPORT_CTL_CMDS | 0x01)
360 #define	FCT_CMD_PORT_ONLINE_COMPLETE	(STMF_LPORT_CTL_CMDS | 0x02)
361 #define	FCT_CMD_PORT_OFFLINE		(STMF_LPORT_CTL_CMDS | 0x03)
362 #define	FCT_CMD_PORT_OFFLINE_COMPLETE	(STMF_LPORT_CTL_CMDS | 0x04)
363 #define	FCT_ACK_PORT_ONLINE_COMPLETE	(STMF_LPORT_CTL_CMDS | 0x05)
364 #define	FCT_ACK_PORT_OFFLINE_COMPLETE	(STMF_LPORT_CTL_CMDS | 0x06)
365 #define	FCT_CMD_FORCE_LIP		(STMF_LPORT_CTL_CMDS | 0x07)
366 
367 /*
368  * IO flags for cmd flow.
369  */
370 #define	FCT_IOF_FCA_DONE		0x10000
371 #define	FCT_IOF_FORCE_FCA_DONE		0x20000
372 
373 /*
374  * Fill CTIU preamble
375  */
376 #ifdef	lint
377 #define	FCT_FILL_CTIU_PREAMBLE(x_payload, x_ctop)	_NOTE(EMPTY)
378 #else
379 #define	FCT_FILL_CTIU_PREAMBLE(x_payload, x_ctop)	\
380 	do {						\
381 		x_payload[0] = 0x01;			\
382 		x_payload[4] = 0xFC;			\
383 		x_payload[5] = 0x02;			\
384 		x_payload[8] = 0xFF & (x_ctop >> 8);	\
385 		x_payload[9] = 0xFF & (x_ctop);		\
386 	} while (0)
387 #endif
388 
389 uint64_t fct_netbuf_to_value(uint8_t *buf, uint8_t nbytes);
390 void fct_value_to_netbuf(uint64_t value, uint8_t *buf, uint8_t nbytes);
391 void *fct_alloc(fct_struct_id_t struct_id, int additional_size, int flags);
392 void fct_free(void *ptr);
393 fct_cmd_t *fct_scsi_task_alloc(struct fct_local_port *port,
394     uint16_t rp_handle, uint32_t rportid, uint8_t *lun,
395     uint16_t cdb_length, uint16_t task_ext);
396 fct_status_t fct_register_local_port(fct_local_port_t *port);
397 fct_status_t fct_deregister_local_port(fct_local_port_t *port);
398 void fct_handle_event(fct_local_port_t *port, int event_id,
399     uint32_t event_flags, caddr_t arg);
400 void fct_post_rcvd_cmd(fct_cmd_t *cmd, stmf_data_buf_t *dbuf);
401 void fct_queue_cmd_for_termination(fct_cmd_t *cmd, fct_status_t s);
402 void fct_queue_scsi_task_for_termination(fct_cmd_t *cmd, fct_status_t s);
403 fct_cmd_t *fct_handle_to_cmd(fct_local_port_t *port, uint32_t fct_handle);
404 void fct_ctl(struct stmf_local_port *lport, int cmd, void *arg);
405 void fct_cmd_fca_aborted(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags);
406 uint16_t fct_get_rp_handle(fct_local_port_t *port, uint32_t rportid);
407 void fct_send_response_done(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags);
408 void fct_send_cmd_done(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags);
409 void fct_scsi_data_xfer_done(fct_cmd_t *cmd, stmf_data_buf_t *dbuf,
410     uint32_t ioflags);
411 fct_status_t fct_port_initialize(fct_local_port_t *port, uint32_t rflags,
412     char *additional_info);
413 fct_status_t fct_port_shutdown(fct_local_port_t *port, uint32_t rflags,
414     char *additional_info);
415 fct_status_t fct_handle_rcvd_flogi(fct_local_port_t *port,
416     fct_flogi_xchg_t *fx);
417 void fct_log_local_port_event(fct_local_port_t *port, char *subclass);
418 void fct_log_remote_port_event(fct_local_port_t *port, char *subclass,
419     uint8_t *rp_pwwn, uint32_t rp_id);
420 void fct_wwn_to_str(char *to_ptr, const uint8_t *from_ptr);
421 
422 #ifdef	__cplusplus
423 }
424 #endif
425 
426 #endif /* _FCT_H */
427