xref: /illumos-gate/usr/src/uts/common/sys/idm/idm.h (revision 7a088f03b431bdffa96c3b2175964d4d38420caa)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _IDM_H
28 #define	_IDM_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 typedef enum {
35 	IDM_STATUS_SUCCESS = 0,
36 	IDM_STATUS_FAIL,
37 	IDM_STATUS_NORESOURCES,
38 	IDM_STATUS_REJECT,
39 	IDM_STATUS_IO,
40 	IDM_STATUS_ABORTED,
41 	IDM_STATUS_SUSPENDED,
42 	IDM_STATUS_HEADER_DIGEST,
43 	IDM_STATUS_DATA_DIGEST,
44 	IDM_STATUS_PROTOCOL_ERROR,
45 	IDM_STATUS_LOGIN_FAIL
46 } idm_status_t;
47 
48 
49 typedef enum {
50 	CN_CONNECT_ACCEPT = 1,	/* Target only */
51 	CN_LOGIN_FAIL,
52 	CN_READY_FOR_LOGIN,	/* Initiator only */
53 	CN_FFP_ENABLED,
54 	CN_FFP_DISABLED,
55 	CN_CONNECT_LOST,
56 	CN_CONNECT_DESTROY,
57 	CN_CONNECT_FAIL,
58 	CN_MAX
59 } idm_client_notify_t;
60 
61 #ifdef IDM_CN_NOTIFY_STRINGS
62 static const char *idm_cn_strings[CN_MAX + 1] = {
63 	"CN_UNDEFINED",
64 	"CN_CONNECT_ACCEPT",
65 	"CN_LOGIN_FAIL",
66 	"CN_READY_FOR_LOGIN",
67 	"CN_FFP_ENABLED",
68 	"CN_FFP_DISABLED",
69 	"CN_CONNECT_LOST",
70 	"CN_CONNECT_DESTROY",
71 	"CN_CONNECT_FAIL",
72 	"CN_MAX"
73 };
74 #endif
75 
76 typedef enum {
77 	FD_CONN_FAIL,
78 	FD_CONN_LOGOUT,
79 	FD_SESS_LOGOUT
80 } idm_ffp_disable_t;
81 
82 typedef enum {
83 	AT_INTERNAL_SUSPEND,
84 	AT_INTERNAL_ABORT,
85 	AT_TASK_MGMT_ABORT
86 } idm_abort_type_t;
87 
88 typedef enum {
89 	TASK_IDLE,
90 	TASK_ACTIVE,
91 	TASK_SUSPENDING,
92 	TASK_SUSPENDED,
93 	TASK_ABORTING,
94 	TASK_ABORTED,
95 	TASK_COMPLETE,
96 	TASK_MAX_STATE
97 } idm_task_state_t;
98 
99 #ifdef IDM_TASK_SM_STRINGS
100 static const char *idm_ts_name[TASK_MAX_STATE+1] = {
101 	"TASK_IDLE",
102 	"TASK_ACTIVE",
103 	"TASK_SUSPENDING",
104 	"TASK_SUSPENDED",
105 	"TASK_ABORTING",
106 	"TASK_ABORTED",
107 	"TASK_COMPLETE",
108 	"TASK_MAX_STATE"
109 };
110 #endif
111 
112 typedef enum {
113 	KV_HANDLED = 0,
114 	KV_HANDLED_NO_TRANSIT,
115 	KV_UNHANDLED,
116 	KV_TARGET_ONLY,
117 	KV_NO_RESOURCES,
118 	KV_INTERNAL_ERROR,
119 	KV_VALUE_ERROR,
120 	KV_MISSING_FIELDS,
121 	KV_AUTH_FAILED
122 } kv_status_t;
123 
124 /*
125  * Request structures
126  */
127 
128 /* Defined in idm_impl.h */
129 struct idm_conn_s;
130 struct idm_svc_s;
131 struct idm_buf_s;
132 struct idm_pdu_s;
133 struct idm_task_s;
134 
135 typedef idm_status_t (idm_client_notify_cb_t)(
136     struct idm_conn_s *ic, idm_client_notify_t cn, uintptr_t data);
137 
138 typedef void (idm_rx_pdu_cb_t)(struct idm_conn_s *ic, struct idm_pdu_s *pdu);
139 
140 typedef void (idm_rx_pdu_error_cb_t)(struct idm_conn_s *ic,
141     struct idm_pdu_s *pdu, idm_status_t status);
142 
143 typedef void (idm_buf_cb_t)(struct idm_buf_s *idb, idm_status_t status);
144 
145 typedef void (idm_pdu_cb_t)(struct idm_pdu_s *pdu, idm_status_t status);
146 
147 typedef void (idm_task_cb_t)(struct idm_task_s *task, idm_status_t status);
148 
149 typedef void (idm_build_hdr_cb_t)(
150     struct idm_task_s *task, struct idm_pdu_s *pdu, uint8_t opcode);
151 
152 typedef union idm_sockaddr {
153 	struct sockaddr		sin;
154 	struct sockaddr_in	sin4;
155 	struct sockaddr_in6	sin6;
156 } idm_sockaddr_t;
157 
158 #define	SIZEOF_SOCKADDR(so)		\
159 	((so)->sa_family == AF_INET ?	\
160 	sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6))
161 
162 typedef struct {
163 	idm_rx_pdu_cb_t		*icb_rx_scsi_cmd;
164 	idm_rx_pdu_cb_t		*icb_rx_scsi_rsp;
165 	idm_rx_pdu_cb_t		*icb_rx_misc;
166 	idm_rx_pdu_error_cb_t	*icb_rx_error;
167 	idm_task_cb_t		*icb_task_aborted;
168 	idm_client_notify_cb_t	*icb_client_notify;
169 	idm_build_hdr_cb_t	*icb_build_hdr;
170 } idm_conn_ops_t;
171 
172 typedef struct {
173 	int			cr_domain;
174 	int			cr_type;
175 	int			cr_protocol;
176 	boolean_t		cr_bound;
177 	idm_sockaddr_t		cr_bound_addr;
178 	idm_sockaddr_t		cr_ini_dst_addr;
179 	ldi_ident_t		cr_li;
180 	idm_conn_ops_t		icr_conn_ops;
181 } idm_conn_req_t;
182 
183 typedef struct {
184 	uint16_t		sr_port;
185 	ldi_ident_t		sr_li;
186 	idm_conn_ops_t		sr_conn_ops;
187 } idm_svc_req_t;
188 
189 
190 /* This is not how other networking code handles this */
191 typedef struct {
192 	union {
193 		struct in_addr	in4;
194 		struct in6_addr	in6;
195 	} i_addr;
196 	/* i_insize determines which is valid in the union above */
197 	int			i_insize;
198 } idm_ipaddr_t;
199 
200 typedef struct {
201 	idm_ipaddr_t		a_addr;
202 	uint32_t		a_port,
203 				a_oid;
204 } idm_addr_t;
205 
206 typedef struct {
207 	uint32_t		al_vers,			/* In */
208 				al_oid;				/* In */
209 	uint32_t		al_in_cnt;			/* In */
210 	uint32_t		al_out_cnt;			/* Out */
211 	uint32_t		al_tpgt;			/* Out */
212 	idm_addr_t		al_addrs[1];			/* Out */
213 } idm_addr_list_t;
214 
215 /*
216  * State machine auditing
217  */
218 
219 #define	SM_AUDIT_BUF_MAX_REC	32
220 
221 typedef enum {
222 	SAR_UNDEFINED = 0,
223 	SAR_STATE_EVENT,
224 	SAR_STATE_CHANGE
225 } sm_audit_record_type_t;
226 
227 typedef enum {
228 	SAS_UNDEFINED = 0,
229 	SAS_IDM_CONN,
230 	SAS_IDM_TASK,
231 	SAS_ISCSIT_TGT,
232 	SAS_ISCSIT_SESS,
233 	SAS_ISCSIT_LOGIN,
234 	SAS_ISCSI_CMD,
235 	SAS_ISCSI_SESS,
236 	SAS_ISCSI_CONN,
237 	SAS_ISCSI_LOGIN
238 } sm_audit_sm_type_t;
239 
240 typedef struct {
241 	timespec_t		sar_timestamp;
242 	sm_audit_sm_type_t	sar_sm_type;
243 	sm_audit_record_type_t	sar_type;
244 	int			sar_state;
245 	int			sar_new_state;	/* Only for SAR_STATE_CHANGE */
246 	int			sar_event;	/* Only for SAR_STATE_EVENT */
247 	uintptr_t		sar_event_info;	/* Only for SAR_STATE_EVENT */
248 } sm_audit_record_t;
249 
250 typedef struct {
251 	int			sab_index;
252 	int			sab_max_index;
253 	sm_audit_record_t	sab_records[SM_AUDIT_BUF_MAX_REC];
254 } sm_audit_buf_t;
255 
256 extern boolean_t idm_sm_logging;
257 extern boolean_t idm_conn_logging;
258 extern boolean_t idm_svc_logging;
259 
260 #define	IDM_SM_LOG if (idm_sm_logging) cmn_err
261 #define	IDM_CONN_LOG if (idm_conn_logging) cmn_err
262 #define	IDM_SVC_LOG if (idm_svc_logging) cmn_err
263 
264 void idm_sm_audit_init(sm_audit_buf_t *audit_buf);
265 
266 void idm_sm_audit_event(sm_audit_buf_t *audit_buf,
267     sm_audit_sm_type_t sm_type,
268     int state, int event, uintptr_t event_info);
269 
270 void idm_sm_audit_state_change(sm_audit_buf_t *audit_buf,
271     sm_audit_sm_type_t sm_type, int state, int new_state);
272 
273 
274 #include <sys/iscsi_protocol.h>
275 #include <sys/idm/idm_conn_sm.h>
276 #include <sys/idm/idm_transport.h>
277 #include <sys/idm/idm_impl.h>
278 #include <sys/idm/idm_text.h>
279 #include <sys/idm/idm_so.h>
280 
281 /*
282  * iSCSI Initiator Services
283  */
284 
285 idm_status_t
286 idm_ini_conn_create(idm_conn_req_t *cr, idm_conn_t **new_con);
287 
288 idm_status_t
289 idm_ini_conn_connect(idm_conn_t *ic);
290 
291 void
292 idm_ini_conn_disconnect(idm_conn_t *ic);
293 
294 void
295 idm_ini_conn_disconnect_sync(idm_conn_t *ic);
296 
297 void
298 idm_ini_conn_destroy(idm_conn_t *ic);
299 
300 /*
301  * iSCSI Target Services
302  */
303 
304 idm_status_t
305 idm_tgt_svc_create(idm_svc_req_t *sr, idm_svc_t **new_svc);
306 
307 idm_status_t
308 idm_tgt_svc_online(idm_svc_t *is);
309 
310 void
311 idm_tgt_svc_offline(idm_svc_t *is);
312 
313 void
314 idm_tgt_svc_destroy(idm_svc_t *is);
315 
316 void
317 idm_tgt_svc_destroy_if_unref(idm_svc_t *is);
318 
319 idm_svc_t *
320 idm_tgt_svc_lookup(uint16_t port);
321 
322 void
323 idm_tgt_svc_hold(idm_svc_t *is);
324 
325 void
326 idm_tgt_svc_rele_and_destroy(idm_svc_t *is);
327 
328 idm_status_t
329 idm_tgt_conn_accept(idm_conn_t *ic);
330 
331 void
332 idm_tgt_conn_reject(idm_conn_t *ic);
333 
334 void
335 idm_conn_hold(idm_conn_t *ic);
336 
337 void
338 idm_conn_rele(idm_conn_t *ic);
339 
340 /*
341  * Target data transfer services
342  */
343 idm_status_t
344 idm_buf_tx_to_ini(idm_task_t *idt, idm_buf_t *idb,
345     uint32_t offset, uint32_t xfer_length,
346     idm_buf_cb_t idb_buf_cb, void *cb_arg);
347 
348 idm_status_t
349 idm_buf_rx_from_ini(idm_task_t *idt, idm_buf_t *idb,
350     uint32_t offset, uint32_t xfer_length,
351     idm_buf_cb_t idb_buf_cb, void *cb_arg);
352 
353 void
354 idm_buf_tx_to_ini_done(idm_task_t *idt, idm_buf_t *idb, idm_status_t status);
355 
356 void
357 idm_buf_rx_from_ini_done(idm_task_t *idt, idm_buf_t *idb, idm_status_t status);
358 
359 /*
360  * Shared Initiator/Target Services
361  */
362 kv_status_t
363 idm_negotiate_key_values(idm_conn_t *ic, nvlist_t *request_nvl,
364     nvlist_t *response_nvl, nvlist_t *negotiated_nvl);
365 
366 void
367 idm_notice_key_values(idm_conn_t *ic, nvlist_t *negotiated_nvl);
368 
369 /*
370  * Buffer services
371  */
372 
373 idm_buf_t *
374 idm_buf_alloc(idm_conn_t *ic, void *bufptr, uint64_t buflen);
375 
376 void
377 idm_buf_free(idm_buf_t *idb);
378 
379 void
380 idm_buf_bind_in(idm_task_t *idt, idm_buf_t *buf);
381 
382 void
383 idm_buf_bind_out(idm_task_t *idt, idm_buf_t *buf);
384 
385 void
386 idm_buf_unbind_in(idm_task_t *idt, idm_buf_t *buf);
387 
388 void
389 idm_buf_unbind_out(idm_task_t *idt, idm_buf_t *buf);
390 
391 idm_buf_t *
392 idm_buf_find(void *lbuf, size_t data_offset);
393 
394 void
395 idm_bufpat_set(idm_buf_t *idb);
396 
397 boolean_t
398 idm_bufpat_check(idm_buf_t *idb, int check_len, idm_bufpat_check_type_t type);
399 
400 extern boolean_t idm_pattern_checking;
401 
402 #define	IDM_BUFPAT_SET(CHK_BUF) 				\
403 	if (idm_pattern_checking && (CHK_BUF)->idb_bufalloc) {	\
404 		idm_bufpat_set(CHK_BUF);			\
405 	}
406 
407 #define	IDM_BUFPAT_CHECK(CHK_BUF, CHK_LEN, CHK_TYPE) 		\
408 	if (idm_pattern_checking) {				\
409 		(void) idm_bufpat_check(CHK_BUF, CHK_LEN, CHK_TYPE);	\
410 	}
411 
412 /*
413  * Task services
414  */
415 idm_task_t *
416 idm_task_alloc(idm_conn_t *ic);
417 
418 void
419 idm_task_start(idm_task_t *idt, uintptr_t handle);
420 
421 void
422 idm_task_abort(idm_conn_t *ic, idm_task_t *idt, idm_abort_type_t abort_type);
423 
424 void
425 idm_task_cleanup(idm_task_t *idt);
426 
427 void
428 idm_task_done(idm_task_t *idt);
429 
430 void
431 idm_task_free(idm_task_t *idt);
432 
433 idm_task_t *
434 idm_task_find(idm_conn_t *ic, uint32_t itt, uint32_t ttt);
435 
436 idm_task_t *
437 idm_task_find_and_complete(idm_conn_t *ic, uint32_t itt, uint32_t ttt);
438 
439 void *
440 idm_task_find_by_handle(idm_conn_t *ic, uintptr_t handle);
441 
442 void
443 idm_task_hold(idm_task_t *idt);
444 
445 void
446 idm_task_rele(idm_task_t *idt);
447 
448 /*
449  * PDU Services
450  */
451 
452 idm_pdu_t *
453 idm_pdu_alloc(uint_t hdrlen, uint_t datalen);
454 
455 idm_pdu_t *
456 idm_pdu_alloc_nosleep(uint_t hdrlen, uint_t datalen);
457 
458 void
459 idm_pdu_free(idm_pdu_t *pdu);
460 
461 void
462 idm_pdu_init(idm_pdu_t *pdu, idm_conn_t *ic, void *private, idm_pdu_cb_t *cb);
463 
464 void
465 idm_pdu_init_hdr(idm_pdu_t *pdu, uint8_t *hdr, uint_t hdrlen);
466 
467 void
468 idm_pdu_init_data(idm_pdu_t *pdu, uint8_t *data, uint_t datalen);
469 
470 void
471 idm_pdu_complete(idm_pdu_t *pdu, idm_status_t status);
472 
473 void
474 idm_pdu_tx(idm_pdu_t *pdu);
475 
476 /*
477  * Object reference tracking
478  */
479 
480 void
481 idm_refcnt_init(idm_refcnt_t *refcnt, void *referenced_obj);
482 
483 void
484 idm_refcnt_destroy(idm_refcnt_t *refcnt);
485 
486 void
487 idm_refcnt_reset(idm_refcnt_t *refcnt);
488 
489 void
490 idm_refcnt_hold(idm_refcnt_t *refcnt);
491 
492 void
493 idm_refcnt_rele(idm_refcnt_t *refcnt);
494 
495 void
496 idm_refcnt_rele_and_destroy(idm_refcnt_t *refcnt, idm_refcnt_cb_t *cb_func);
497 
498 void
499 idm_refcnt_wait_ref(idm_refcnt_t *refcnt);
500 
501 void
502 idm_refcnt_async_wait_ref(idm_refcnt_t *refcnt, idm_refcnt_cb_t *cb_func);
503 
504 
505 #ifdef	__cplusplus
506 }
507 #endif
508 
509 #endif /* _IDM_H */
510