xref: /titanic_44/usr/src/uts/common/sys/idm/idm_impl.h (revision 3c112a2b34403220c06c3e2fcac403358cfba168)
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  */
24 #ifndef	_IDM_IMPL_H_
25 #define	_IDM_IMPL_H_
26 
27 #ifdef	__cplusplus
28 extern "C" {
29 #endif
30 
31 #include <sys/avl.h>
32 #include <sys/socket_impl.h>
33 
34 /*
35  * IDM lock order:
36  *
37  * idm_taskid_table_lock, idm_task_t.idt_mutex
38  */
39 
40 #define	CF_LOGIN_READY		0x00000001
41 #define	CF_INITIAL_LOGIN	0x00000002
42 #define	CF_ERROR		0x80000000
43 
44 typedef enum {
45 	CONN_TYPE_INI = 1,
46 	CONN_TYPE_TGT
47 } idm_conn_type_t;
48 
49 /*
50  * Watchdog interval in seconds
51  */
52 #define	IDM_WD_INTERVAL			5
53 
54 /*
55  * Timeout period before the client "keepalive" callback is invoked in
56  * seconds if the connection is idle.
57  */
58 #define	IDM_TRANSPORT_KEEPALIVE_IDLE_TIMEOUT	20
59 
60 /*
61  * Timeout period before a TRANSPORT_FAIL event is generated in seconds
62  * if the connection is idle.
63  */
64 #define	IDM_TRANSPORT_FAIL_IDLE_TIMEOUT	30
65 
66 /*
67  * IDM reference count structure.  Audit code is shamelessly adapted
68  * from CIFS server.
69  */
70 
71 #define	REFCNT_AUDIT_STACK_DEPTH	16
72 #define	REFCNT_AUDIT_BUF_MAX_REC	16
73 
74 typedef struct {
75 	uint32_t		anr_refcnt;
76 	int			anr_depth;
77 	pc_t			anr_stack[REFCNT_AUDIT_STACK_DEPTH];
78 } refcnt_audit_record_t;
79 
80 typedef struct {
81 	int			anb_index;
82 	int			anb_max_index;
83 	refcnt_audit_record_t	anb_records[REFCNT_AUDIT_BUF_MAX_REC];
84 } refcnt_audit_buf_t;
85 
86 #define	REFCNT_AUDIT(_rf_) {				\
87 	refcnt_audit_record_t	*anr;			\
88 							\
89 	anr = (_rf_)->ir_audit_buf.anb_records;		\
90 	anr += (_rf_)->ir_audit_buf.anb_index;		\
91 	(_rf_)->ir_audit_buf.anb_index++;		\
92 	(_rf_)->ir_audit_buf.anb_index &=		\
93 	    (_rf_)->ir_audit_buf.anb_max_index;		\
94 	anr->anr_refcnt = (_rf_)->ir_refcnt;		\
95 	anr->anr_depth = getpcstack(anr->anr_stack,	\
96 	    REFCNT_AUDIT_STACK_DEPTH);			\
97 }
98 
99 struct idm_refcnt_s;
100 
101 typedef void (idm_refcnt_cb_t)(void *ref_obj);
102 
103 typedef enum {
104 	REF_NOWAIT,
105 	REF_WAIT_SYNC,
106 	REF_WAIT_ASYNC
107 } idm_refcnt_wait_t;
108 
109 typedef struct idm_refcnt_s {
110 	int			ir_refcnt;
111 	void			*ir_referenced_obj;
112 	idm_refcnt_wait_t	ir_waiting;
113 	kmutex_t		ir_mutex;
114 	kcondvar_t		ir_cv;
115 	idm_refcnt_cb_t		*ir_cb;
116 	refcnt_audit_buf_t	ir_audit_buf;
117 } idm_refcnt_t;
118 
119 /*
120  * connection parameters - These parameters would be populated at
121  * connection create, or during key-value negotiation at login
122  */
123 typedef struct idm_conn_params_s {
124 	uint32_t		max_recv_dataseglen;
125 	uint32_t		max_xmit_dataseglen;
126 	uint32_t		conn_login_max;
127 	uint32_t		conn_login_interval;
128 	boolean_t		nonblock_socket;
129 } idm_conn_param_t;
130 
131 typedef struct idm_svc_s {
132 	list_node_t		is_list_node;
133 	kmutex_t		is_mutex;
134 	kcondvar_t		is_cv;
135 	kmutex_t		is_count_mutex;
136 	kcondvar_t		is_count_cv;
137 	idm_refcnt_t		is_refcnt;
138 	int			is_online;
139 	/* transport-specific service components */
140 	void			*is_so_svc;
141 	void			*is_iser_svc;
142 	idm_svc_req_t		is_svc_req;
143 } idm_svc_t;
144 
145 #define	ISCSI_MAX_TSIH_LEN	6	/* 0x%04x */
146 #define	ISCSI_MAX_ISID_LEN	ISCSI_ISID_LEN * 2
147 
148 typedef struct idm_conn_s {
149 	list_node_t		ic_list_node;
150 	void			*ic_handle;
151 	idm_refcnt_t		ic_refcnt;
152 	idm_svc_t		*ic_svc_binding; /* Target conn. only */
153 	idm_sockaddr_t 		ic_ini_dst_addr;
154 	struct sockaddr_storage	ic_laddr;	/* conn local address */
155 	struct sockaddr_storage	ic_raddr;	/* conn remote address */
156 
157 	/*
158 	 * the target_name, initiator_name, initiator session
159 	 * identifier and target session identifying handle
160 	 * are only used for target connections.
161 	 */
162 	char			ic_target_name[ISCSI_MAX_NAME_LEN + 1];
163 	char			ic_initiator_name[ISCSI_MAX_NAME_LEN + 1];
164 	char			ic_tsih[ISCSI_MAX_TSIH_LEN + 1];
165 	char			ic_isid[ISCSI_MAX_ISID_LEN + 1];
166 	idm_conn_state_t	ic_state;
167 	idm_conn_state_t	ic_last_state;
168 	sm_audit_buf_t		ic_state_audit;
169 	kmutex_t		ic_state_mutex;
170 	kcondvar_t		ic_state_cv;
171 	uint32_t		ic_state_flags;
172 	timeout_id_t		ic_state_timeout;
173 	struct idm_conn_s	*ic_reinstate_conn; /* For conn reinst. */
174 	struct idm_conn_s	*ic_logout_conn; /* For other conn logout */
175 	taskq_t			*ic_state_taskq;
176 	int			ic_pdu_events;
177 	boolean_t		ic_login_info_valid;
178 	boolean_t		ic_rdma_extensions;
179 	uint16_t		ic_login_cid;
180 
181 	kmutex_t		ic_mutex;
182 	kcondvar_t		ic_cv;
183 	idm_status_t		ic_conn_sm_status;
184 
185 	boolean_t		ic_ffp;
186 	boolean_t		ic_keepalive;
187 	uint32_t		ic_internal_cid;
188 
189 	uint32_t		ic_conn_flags;
190 	idm_conn_type_t		ic_conn_type;
191 	idm_conn_ops_t		ic_conn_ops;
192 	idm_transport_ops_t	*ic_transport_ops;
193 	idm_transport_type_t	ic_transport_type;
194 	int			ic_transport_hdrlen;
195 	void			*ic_transport_private;
196 	idm_conn_param_t	ic_conn_params;
197 	/*
198 	 * Save client callback to interpose idm callback
199 	 */
200 	idm_pdu_cb_t		*ic_client_callback;
201 	clock_t			ic_timestamp;
202 } idm_conn_t;
203 
204 #define	IDM_CONN_HEADER_DIGEST	0x00000001
205 #define	IDM_CONN_DATA_DIGEST	0x00000002
206 #define	IDM_CONN_USE_SCOREBOARD	0x00000004
207 
208 #define	IDM_CONN_ISINI(ICI_IC)	((ICI_IC)->ic_conn_type == CONN_TYPE_INI)
209 #define	IDM_CONN_ISTGT(ICI_IC)	((ICI_IC)->ic_conn_type == CONN_TYPE_TGT)
210 
211 /*
212  * An IDM target task can transfer data using multiple buffers. The task
213  * will maintain a list of buffers, and each buffer will contain the relative
214  * offset of the transfer and a pointer to the next buffer in the list.
215  *
216  * Note on client private data:
217  * idt_private is intended to be a pointer to some sort of client-
218  * specific state.
219  *
220  * idt_client_handle is a more generic client-private piece of data that can
221  * be used by the client for the express purpose of task lookup.  The driving
222  * use case for this is for the client to store the initiator task tag for
223  * a given task so that it may be more easily retrieved for task management.
224  *
225  * The key take away here is that clients should never call
226  * idm_task_find_by_handle in the performance path.
227  *
228  * An initiator will require only one buffer per task, the offset will be 0.
229  */
230 
231 typedef struct idm_task_s {
232 	idm_conn_t		*idt_ic;	/* Associated connection */
233 	/* connection type is in idt_ic->ic_conn_type */
234 	kmutex_t		idt_mutex;
235 	void			*idt_private;	/* Client private data */
236 	uintptr_t		idt_client_handle;	/* Client private */
237 	uint32_t		idt_tt;		/* Task tag */
238 	uint32_t		idt_r2t_ttt;	/* R2T Target Task tag */
239 	idm_task_state_t	idt_state;
240 	idm_refcnt_t		idt_refcnt;
241 
242 	/*
243 	 * Statistics
244 	 */
245 	int			idt_tx_to_ini_start;
246 	int			idt_tx_to_ini_done;
247 	int			idt_rx_from_ini_start;
248 	int			idt_rx_from_ini_done;
249 	int			idt_tx_bytes;	/* IDM_CONN_USE_SCOREBOARD */
250 	int			idt_rx_bytes;	/* IDM_CONN_USE_SCOREBOARD */
251 
252 	uint32_t		idt_exp_datasn;	/* expected datasn */
253 	uint32_t		idt_exp_rttsn;	/* expected rttsn */
254 	list_t			idt_inbufv;	/* chunks of IN buffers */
255 	list_t			idt_outbufv;	/* chunks of OUT buffers */
256 
257 	/*
258 	 * Transport header, which describes this tasks remote tagged buffer
259 	 */
260 	int			idt_transport_hdrlen;
261 	void			*idt_transport_hdr;
262 	uint32_t		idt_flags;	/* phase collapse */
263 } idm_task_t;
264 
265 int idm_task_constructor(void *task_void, void *arg, int flags);
266 void idm_task_destructor(void *task_void, void *arg);
267 
268 #define	IDM_TASKIDS_MAX		16384
269 #define	IDM_BUF_MAGIC		0x49425546	/* "IBUF" */
270 
271 #define	IDM_TASK_PHASECOLLAPSE_REQ	0x00000001 /* request phase collapse */
272 #define	IDM_TASK_PHASECOLLAPSE_SUCCESS	0x00000002 /* phase collapse success */
273 
274 /* Protect with task mutex */
275 typedef struct idm_buf_s {
276 	uint32_t	idb_magic;	/* "IBUF" */
277 
278 	/*
279 	 * Note: idm_tx_link *must* be the second element in the list for
280 	 * proper TX PDU ordering.
281 	 */
282 	list_node_t	idm_tx_link;	/* link in a list of TX objects */
283 
284 	list_node_t	idb_buflink;	/* link in a multi-buffer data xfer */
285 	idm_conn_t	*idb_ic;	/* Associated connection */
286 	void		*idb_buf;	/* data */
287 	uint64_t	idb_buflen;	/* length of buffer */
288 	size_t		idb_bufoffset;	/* offset in a multi-buffer xfer */
289 	boolean_t	idb_bufalloc;  /* true if alloc'd in idm_buf_alloc */
290 	/*
291 	 * DataPDUInOrder=Yes, so to track that the PDUs in a sequence are sent
292 	 * in continuously increasing address order, check that offsets for a
293 	 * single buffer xfer are in order.
294 	 */
295 	uint32_t	idb_exp_offset;
296 	size_t		idb_xfer_len;	/* Current requested xfer len */
297 	void		*idb_buf_private; /* transport-specific buf handle */
298 	void		*idb_reg_private; /* transport-specific reg handle */
299 	void		*idb_bufptr; /* transport-specific bcopy pointer */
300 	boolean_t	idb_bufbcopy;	/* true if bcopy required */
301 
302 	idm_buf_cb_t	*idb_buf_cb;	/* Data Completion Notify, tgt only */
303 	void		*idb_cb_arg;	/* Client private data */
304 	idm_task_t	*idb_task_binding;
305 	timespec_t	idb_xfer_start;
306 	timespec_t	idb_xfer_done;
307 	boolean_t	idb_in_transport;
308 	boolean_t	idb_tx_thread;		/* Sockets only */
309 	iscsi_hdr_t	idb_data_hdr_tmpl;	/* Sockets only */
310 	idm_status_t	idb_status;
311 } idm_buf_t;
312 
313 typedef enum {
314 	BP_CHECK_QUICK,
315 	BP_CHECK_THOROUGH,
316 	BP_CHECK_ASSERT
317 } idm_bufpat_check_type_t;
318 
319 #define	BUFPAT_MATCH(bc_bufpat, bc_idb) 		\
320 	((bufpat->bufpat_idb == bc_idb) &&		\
321 	    (bufpat->bufpat_bufmagic == IDM_BUF_MAGIC))
322 
323 typedef struct idm_bufpat_s {
324 	void		*bufpat_idb;
325 	uint32_t	bufpat_bufmagic;
326 	uint32_t	bufpat_offset;
327 } idm_bufpat_t;
328 
329 #define	PDU_MAX_IOVLEN	12
330 #define	IDM_PDU_MAGIC	0x49504455	/* "IPDU" */
331 
332 typedef struct idm_pdu_s {
333 	uint32_t	isp_magic;	/* "IPDU" */
334 
335 	/*
336 	 * Internal - Order is vital.  idm_tx_link *must* be the second
337 	 * element in this structure for proper TX PDU ordering.
338 	 */
339 	list_node_t	idm_tx_link;
340 
341 	list_node_t	isp_client_lnd;
342 
343 	idm_conn_t	*isp_ic;	/* Must be set */
344 	iscsi_hdr_t	*isp_hdr;
345 	uint_t		isp_hdrlen;
346 	uint8_t		*isp_data;
347 	uint_t		isp_datalen;
348 
349 	/* Transport header */
350 	void		*isp_transport_hdr;
351 	uint32_t	isp_transport_hdrlen;
352 	void		*isp_transport_private;
353 
354 	/*
355 	 * isp_data is used for sending SCSI status, NOP, text, scsi and
356 	 * non-scsi data. Data is received using isp_iov and isp_iovlen
357 	 * to support data over multiple buffers.
358 	 */
359 	void		*isp_private;
360 	idm_pdu_cb_t	*isp_callback;
361 	idm_status_t	isp_status;
362 
363 	/*
364 	 * The following four elements are only used in
365 	 * idm_sorecv_scsidata() currently.
366 	 */
367 	struct iovec	isp_iov[PDU_MAX_IOVLEN];
368 	int		isp_iovlen;
369 	idm_buf_t	*isp_sorx_buf;
370 
371 	/* Implementation data for idm_pdu_alloc and sorx PDU cache */
372 	uint32_t	isp_flags;
373 	uint_t		isp_hdrbuflen;
374 	uint_t		isp_databuflen;
375 	time_t		isp_queue_time;
376 } idm_pdu_t;
377 
378 /*
379  * This "generic" object is used when removing an item from the ic_tx_list
380  * in order to determine whether it's an idm_pdu_t or an idm_buf_t
381  */
382 
383 typedef struct {
384 	uint32_t	idm_tx_obj_magic;
385 	/*
386 	 * idm_tx_link *must* be the second element in this structure.
387 	 */
388 	list_node_t	idm_tx_link;
389 } idm_tx_obj_t;
390 
391 
392 #define	IDM_PDU_OPCODE(PDU) \
393 	((PDU)->isp_hdr->opcode & ISCSI_OPCODE_MASK)
394 
395 #define	IDM_PDU_ALLOC		0x00000001
396 #define	IDM_PDU_ADDL_HDR	0x00000002
397 #define	IDM_PDU_ADDL_DATA	0x00000004
398 #define	IDM_PDU_LOGIN_TX	0x00000008
399 #define	IDM_PDU_SET_STATSN	0x00000010
400 #define	IDM_PDU_ADVANCE_STATSN	0x00000020
401 
402 #define	OSD_EXT_CDB_AHSLEN	(200 - 15)
403 #define	BIDI_AHS_LENGTH		5
404 #define	IDM_SORX_CACHE_AHSLEN \
405 	(((OSD_EXT_CDB_AHSLEN + 3) + \
406 	    (BIDI_AHS_LENGTH + 3)) / sizeof (uint32_t))
407 #define	IDM_SORX_CACHE_HDRLEN	(sizeof (iscsi_hdr_t) + IDM_SORX_CACHE_AHSLEN)
408 
409 /*
410  * ID pool
411  */
412 
413 #define	IDM_IDPOOL_MAGIC	0x4944504C	/* IDPL */
414 #define	IDM_IDPOOL_MIN_SIZE	64	/* Number of IDs to begin with */
415 #define	IDM_IDPOOL_MAX_SIZE	64 * 1024
416 
417 typedef struct idm_idpool {
418 	uint32_t	id_magic;
419 	kmutex_t	id_mutex;
420 	uint8_t		*id_pool;
421 	uint32_t	id_size;
422 	uint8_t		id_bit;
423 	uint8_t		id_bit_idx;
424 	uint32_t	id_idx;
425 	uint32_t	id_idx_msk;
426 	uint32_t	id_free_counter;
427 	uint32_t	id_max_free_counter;
428 } idm_idpool_t;
429 
430 /*
431  * Global IDM state structure
432  */
433 typedef struct {
434 	kmutex_t	idm_global_mutex;
435 	taskq_t		*idm_global_taskq;
436 	kthread_t	*idm_wd_thread;
437 	kt_did_t	idm_wd_thread_did;
438 	boolean_t	idm_wd_thread_running;
439 	kcondvar_t	idm_wd_cv;
440 	list_t		idm_tgt_svc_list;
441 	kcondvar_t	idm_tgt_svc_cv;
442 	list_t		idm_tgt_conn_list;
443 	int		idm_tgt_conn_count;
444 	list_t		idm_ini_conn_list;
445 	kmem_cache_t	*idm_buf_cache;
446 	kmem_cache_t	*idm_task_cache;
447 	krwlock_t	idm_taskid_table_lock;
448 	idm_task_t	**idm_taskid_table;
449 	uint32_t	idm_taskid_next;
450 	uint32_t	idm_taskid_max;
451 	idm_idpool_t	idm_conn_id_pool;
452 	kmem_cache_t	*idm_sotx_pdu_cache;
453 	kmem_cache_t	*idm_sorx_pdu_cache;
454 	kmem_cache_t	*idm_so_128k_buf_cache;
455 } idm_global_t;
456 
457 idm_global_t	idm; /* Global state */
458 
459 int
460 idm_idpool_create(idm_idpool_t	*pool);
461 
462 void
463 idm_idpool_destroy(idm_idpool_t *pool);
464 
465 int
466 idm_idpool_alloc(idm_idpool_t *pool, uint16_t *id);
467 
468 void
469 idm_idpool_free(idm_idpool_t *pool, uint16_t id);
470 
471 void
472 idm_pdu_rx(idm_conn_t *ic, idm_pdu_t *pdu);
473 
474 void
475 idm_pdu_tx_forward(idm_conn_t *ic, idm_pdu_t *pdu);
476 
477 boolean_t
478 idm_pdu_rx_forward_ffp(idm_conn_t *ic, idm_pdu_t *pdu);
479 
480 void
481 idm_pdu_rx_forward(idm_conn_t *ic, idm_pdu_t *pdu);
482 
483 void
484 idm_pdu_tx_protocol_error(idm_conn_t *ic, idm_pdu_t *pdu);
485 
486 void
487 idm_pdu_rx_protocol_error(idm_conn_t *ic, idm_pdu_t *pdu);
488 
489 void idm_parse_login_rsp(idm_conn_t *ic, idm_pdu_t *logout_req_pdu,
490     boolean_t rx);
491 
492 void idm_parse_logout_req(idm_conn_t *ic, idm_pdu_t *logout_req_pdu,
493     boolean_t rx);
494 
495 void idm_parse_logout_rsp(idm_conn_t *ic, idm_pdu_t *login_rsp_pdu,
496     boolean_t rx);
497 
498 idm_status_t idm_svc_conn_create(idm_svc_t *is, idm_transport_type_t type,
499     idm_conn_t **ic_result);
500 
501 void idm_svc_conn_destroy(idm_conn_t *ic);
502 
503 idm_status_t idm_ini_conn_finish(idm_conn_t *ic);
504 
505 idm_status_t idm_tgt_conn_finish(idm_conn_t *ic);
506 
507 idm_conn_t *idm_conn_create_common(idm_conn_type_t conn_type,
508     idm_transport_type_t tt, idm_conn_ops_t *conn_ops);
509 
510 void idm_conn_destroy_common(idm_conn_t *ic);
511 
512 void idm_conn_close(idm_conn_t *ic);
513 
514 uint32_t idm_cid_alloc(void);
515 
516 void idm_cid_free(uint32_t cid);
517 
518 uint32_t idm_crc32c(void *address, unsigned long length);
519 
520 uint32_t idm_crc32c_continued(void *address, unsigned long length,
521     uint32_t crc);
522 
523 void idm_listbuf_insert(list_t *lst, idm_buf_t *buf);
524 
525 idm_conn_t *idm_lookup_conn(uint8_t *isid, uint16_t tsih, uint16_t cid);
526 
527 #ifdef	__cplusplus
528 }
529 #endif
530 
531 #endif /* _IDM_IMPL_H_ */
532