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