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