xref: /illumos-gate/usr/src/uts/common/io/comstar/port/srpt/srpt_impl.h (revision 861a91627796c35220e75654dac61e5707536dcd)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SRPT_IMPL_H_
28 #define	_SRPT_IMPL_H_
29 
30 /*
31  * Prototypes and data structures for the SRP Target Port Provider.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/ddi.h>
36 #include <sys/ib/ibtl/ibti.h>
37 #include <sys/modctl.h>
38 
39 #include <stmf.h>
40 #include <stmf_ioctl.h>
41 #include <portif.h>
42 
43 #include <sys/ib/mgt/ibdma/ibdma.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 /* Format the session identifier */
50 #define	ALIAS_STR(s, a, b)						\
51 	((void) snprintf((s), sizeof ((s)), "%016llx:%016llx",		\
52 	    (u_longlong_t)(a), (u_longlong_t)(b)))
53 
54 /* Format the EUI name */
55 #define	EUI_STR(s, a)							\
56 	((void) snprintf((s), sizeof ((s)), "eui.%016llX", (u_longlong_t)(a)))
57 
58 /*
59  * We should/could consider making some of these values tunables.
60  * Specifically, SEND_MSG_SIZE and SEND_MSG_DEPTH.
61  */
62 enum {
63 	SRPT_DEFAULT_IOC_SRQ_SIZE = 4096,
64 	SRPT_DEFAULT_SEND_MSG_DEPTH = 128,
65 	/*
66 	 * SEND_MSG_SIZE must be a multiple of 64 as it is registered
67 	 * as memory regions with IB.  To support a scatter/gather table
68 	 * size of 32, the size must be at not less than 960.  To support
69 	 * the maximum scatter/gather table size of 255, the IU must
70 	 * be at least 4160 bytes.
71 	 */
72 	SRPT_DEFAULT_SEND_MSG_SIZE = 4160,
73 	SRPT_DEFAULT_MAX_RDMA_SIZE = 65536,
74 	SRPT_MIN_T_I_IU_LEN = 52,
75 	SRPT_EUI_ID_LEN = 20,
76 	SRPT_RECV_WC_POLL_SIZE = 16,
77 	SRPT_SEND_WC_POLL_SIZE = 16,
78 	SRPT_MAX_OUT_IO_PER_CMD = 16,
79 	SRPT_FENCE_SEND = 1,
80 	SRPT_NO_FENCE_SEND = 0
81 };
82 
83 struct srpt_target_port_s;
84 
85 #define	SRPT_ALIAS_LEN	(SRP_PORT_ID_LEN * 2 + 2)
86 
87 /*
88  * SRP Session - represents a SCSI I_T_Nexus.
89  *
90  * Sessions map 1 or more initiator logins to a specific I/O
91  * Controller SCSI Target Port.  Targets create sessions
92  * at initiator login and release when no longer referenced
93  * by a login.
94  */
95 typedef struct srpt_session_s {
96 	krwlock_t 			ss_rwlock;
97 	list_node_t			ss_node;
98 
99 	/*
100 	 * ADVANCED FEATURE, NOT YET SUPPORTED.
101 	 * In multi-channel mode, multiple RDMA communication
102 	 * channels may reference the same SCSI session.  When
103 	 * a channel releases its reference to the SCSI session,
104 	 * it should have no tasks associated with the session.
105 	 *
106 	 * If multi-channel is implemented, add a channel list
107 	 * to this object instead of tracking it on the target.
108 	 *
109 	 * Will also need a session state & mode.  Mode is to
110 	 * track if the session is MULTI or SINGLE channel.
111 	 */
112 
113 	stmf_scsi_session_t		*ss_ss;
114 	struct srpt_target_port_s	*ss_tgt;
115 	list_t				ss_task_list;
116 
117 	/*
118 	 * SRP Initiator and target identifiers are 128-bit.
119 	 *
120 	 * The specification defines the initiator to be 64-bits of
121 	 * ID extension and 64 bits of GUID, but these are really
122 	 * just a recommendation.  Generally the extension is used
123 	 * to create unique I_T_Nexus from the same initiator and
124 	 * target.  Initiators are inconsistent on the GUID they
125 	 * use, some use the HCA Node, some the HCA port.
126 	 *
127 	 * The specification defines the target to be 64-bits of
128 	 * service ID followed by 64-bits of I/O Controller GUID.
129 	 * In the case where there is a single default target
130 	 * service, they will be the same (our default).
131 	 */
132 	uint8_t				ss_i_id[SRP_PORT_ID_LEN];
133 	uint8_t				ss_t_id[SRP_PORT_ID_LEN];
134 
135 	/* So we can see the full 128-bit initiator login from stmfadm */
136 	char				ss_i_alias[SRPT_ALIAS_LEN];
137 	uint8_t				ss_hw_port;
138 
139 	char				ss_t_alias[SRPT_ALIAS_LEN];
140 	char				ss_i_name[SRPT_EUI_ID_LEN + 1];
141 	char				ss_t_name[SRPT_EUI_ID_LEN + 1];
142 	char				ss_i_gid[SRPT_ALIAS_LEN];
143 	char				ss_t_gid[SRPT_ALIAS_LEN];
144 } srpt_session_t;
145 
146 /*
147  * Send work request types.
148  */
149 typedef enum srpt_swqe_type_e {
150 	SRPT_SWQE_TYPE_DATA = 1,
151 	SRPT_SWQE_TYPE_RESP
152 } srpt_swqe_type_t;
153 
154 typedef struct srpt_swqe_s {
155 	srpt_swqe_type_t	sw_type;
156 	void			*sw_addr;
157 	ibt_wrid_t		sw_next;
158 } srpt_swqe_t;
159 
160 /*
161  * SRP Channel - the RDMA communications channel associated with
162  * a specific SRP login.
163  */
164 typedef enum srpt_channel_state_e {
165 	SRPT_CHANNEL_CONNECTING = 0,
166 	SRPT_CHANNEL_CONNECTED,
167 	SRPT_CHANNEL_DISCONNECTING
168 } srpt_channel_state_t;
169 
170 typedef struct srpt_channel_s {
171 	krwlock_t 			ch_rwlock;
172 
173 	kmutex_t			ch_reflock;
174 	uint_t				ch_refcnt;
175 	kcondvar_t			ch_cv_complete;
176 	uint_t				ch_cv_waiters;
177 
178 	list_node_t			ch_stp_node;
179 	srpt_channel_state_t		ch_state;
180 	ibt_cq_hdl_t			ch_scq_hdl;
181 	ibt_cq_hdl_t			ch_rcq_hdl;
182 	ibt_channel_hdl_t		ch_chan_hdl;
183 	ibt_chan_sizes_t		ch_sizes;
184 
185 	uint32_t			ch_req_lim_delta;
186 	uint32_t			ch_ti_iu_len;
187 	struct srpt_target_port_s	*ch_tgt;
188 	srpt_session_t			*ch_session;
189 
190 	/*
191 	 * Map IB send WQE request IDs to the
192 	 * apporpriate operation type (for errors).
193 	 */
194 	kmutex_t			ch_swqe_lock;
195 	srpt_swqe_t			*ch_swqe;
196 	uint32_t			ch_num_swqe;
197 	uint32_t			ch_head;
198 	uint32_t			ch_tail;
199 	uint32_t			ch_swqe_posted;
200 } srpt_channel_t;
201 
202 /*
203  * SRP Information Unit (IU).  Each IU structure contains
204  * the buffer for the IU itself (received over the RC
205  * channel), and all of the context required by the target
206  * to process this request represented by the IU.
207  * Available IU structures are managed on the I/O Controller
208  * shared receive queue.
209  */
210 enum {
211 	SRPT_IU_STMF_ABORTING	= 1 << 0,	/* STMF called abort */
212 	SRPT_IU_SRP_ABORTING	= 1 << 1,	/* SRP initiator aborting */
213 	SRPT_IU_ABORTED		= 1 << 2,	/* Task has been aborted */
214 	SRPT_IU_RESP_SENT	= 1 << 3	/* Response queued */
215 };
216 
217 typedef struct srpt_iu_s {
218 	/*
219 	 * The buffer for the IU itself.  When unused (a
220 	 * reference count of zero), this buffer is posted
221 	 * on the I/O Controllers SRPT SRQ.
222 	 */
223 	void			*iu_buf;
224 	ibt_wr_ds_t		iu_sge;
225 	struct srpt_ioc_s	*iu_ioc;
226 	uint_t			iu_pool_ndx;
227 	kmutex_t		iu_lock;
228 
229 	/*
230 	 * The following are reset for each IU request
231 	 * processed by this buffer.
232 	 */
233 	list_node_t		iu_ss_task_node;
234 	srpt_channel_t		*iu_ch;
235 
236 	uint_t			iu_num_rdescs;
237 	srp_direct_desc_t	*iu_rdescs;
238 	uint_t			iu_tot_xfer_len;
239 
240 	uint64_t		iu_tag;
241 	uint_t			iu_flags;
242 	uint32_t		iu_sq_posted_cnt;
243 	scsi_task_t		*iu_stmf_task;
244 } srpt_iu_t;
245 
246 /*
247  * SRP SCSI Target Port.  By default each HCA creates a single
248  * SCSI Target Port based on the associated I/O Controller
249  * (HCA) node GUID and made available through each physical
250  * hardware port of the I/O Controller.
251  */
252 typedef enum srpt_target_state_e {
253 	SRPT_TGT_STATE_OFFLINE = 0,
254 	SRPT_TGT_STATE_ONLINING,
255 	SRPT_TGT_STATE_ONLINE,
256 	SRPT_TGT_STATE_OFFLINING
257 } srpt_target_state_t;
258 
259 typedef struct srpt_hw_port_s {
260 	ibt_sbind_hdl_t		hwp_bind_hdl;
261 	ib_gid_t		hwp_gid;
262 } srpt_hw_port_t;
263 
264 typedef struct srpt_target_port_s {
265 	stmf_local_port_t	*tp_lport;
266 	struct srpt_ioc_s	*tp_ioc;
267 
268 	kmutex_t		tp_lock;
269 	srpt_target_state_t	tp_state;
270 	kcondvar_t		tp_offline_complete;
271 	uint_t			tp_drv_disabled;
272 
273 	/*
274 	 * We are using a simple list for channels right now, we
275 	 * probably should  switch this over to the AVL
276 	 * implementation eventually (but lookups are not done
277 	 * in the data path so this is not urgent).
278 	 */
279 	kmutex_t		tp_ch_list_lock;
280 	list_t			tp_ch_list;
281 
282 	/*
283 	 * A list of active sessions.  Session lifetime is
284 	 * determined by having active channels, but track
285 	 * them here for easier determination to when a
286 	 * target can truly be offlined, and as a step toward
287 	 * being session-focused rather than channel-focused.
288 	 * If we ever truly support multi-channel, move the
289 	 * channels to be part of the session object.
290 	 *
291 	 * Sessions should remain on this list until they
292 	 * are deregistered from STMF.  This allows the target
293 	 * to properly track when it can consider itself 'offline'.
294 	 */
295 	kmutex_t		tp_sess_list_lock;
296 	kcondvar_t		tp_sess_complete;
297 	list_t			tp_sess_list;
298 
299 	uint_t			tp_srp_enabled;
300 	ibt_srv_hdl_t		tp_ibt_svc_hdl;
301 	ibt_srv_desc_t		tp_ibt_svc_desc;
302 	ib_svc_id_t		tp_ibt_svc_id;
303 	scsi_devid_desc_t	*tp_scsi_devid;
304 	uint8_t			tp_srp_port_id[SRP_PORT_ID_LEN];
305 
306 	uint_t			tp_nports;
307 	srpt_hw_port_t		*tp_hw_port;
308 	/*
309 	 * track the number of active ports so we can offline the target if
310 	 * none
311 	 */
312 	uint32_t		tp_num_active_ports;
313 	/* state STMF wants the target in.  We may be offline due to no ports */
314 	srpt_target_state_t	tp_requested_state;
315 } srpt_target_port_t;
316 
317 /*
318  * SRP Target hardware device.  A SRP Target hardware device
319  * is an IB HCA.  All ports of the HCA comprise a single
320  * I/O Controller that is registered with the IB Device
321  * Managment Agent.
322  */
323 typedef struct srpt_ioc_s {
324 	list_node_t			ioc_node;
325 
326 	krwlock_t 			ioc_rwlock;
327 	ibt_hca_hdl_t			ioc_ibt_hdl;
328 	ibt_hca_attr_t			ioc_attr;
329 	ib_guid_t			ioc_guid;
330 
331 	/*
332 	 * By default each HCA is a single SRP.T10 service based on
333 	 * the HCA GUID.  We have implemented the target here as a
334 	 * pointer to facilitate moving to a list of targets if
335 	 * appropriate down the road.
336 	 */
337 	srpt_target_port_t		*ioc_tgt_port;
338 
339 
340 	/*
341 	 * Each HCA registers a single I/O Controller with the
342 	 * IB Device Management Agent.
343 	 */
344 	ibdma_hdl_t			ioc_ibdma_hdl;
345 	ib_dm_ioc_ctrl_profile_t	ioc_profile;
346 	ib_dm_srv_t			ioc_svc;
347 
348 	ibt_pd_hdl_t			ioc_pd_hdl;
349 	ibt_srq_sizes_t			ioc_srq_attr;
350 	ibt_srq_hdl_t			ioc_srq_hdl;
351 
352 	/*
353 	 * The I/O Controller pool of IU resources allocated
354 	 * at controller creation.
355 	 */
356 	uint32_t			ioc_num_iu_entries;
357 	srpt_iu_t			*ioc_iu_pool;
358 	ibt_mr_hdl_t			ioc_iu_mr_hdl;
359 	void				*ioc_iu_bufs;  /* iu buffer space */
360 
361 	/*
362 	 * Each I/O Controller has it's own data buffer
363 	 * vmem arena.  Pool is created at controller creation,
364 	 * and expanded as required.  This keeps IB memory
365 	 * registrations to a minimum in the data path.
366 	 */
367 	struct srpt_vmem_pool_s		*ioc_dbuf_pool;
368 	stmf_dbuf_store_t		*ioc_stmf_ds;
369 } srpt_ioc_t;
370 
371 /*
372  * Memory regions
373  */
374 typedef struct srpt_mr_s {
375 	ibt_mr_hdl_t			mr_hdl;
376 	ib_vaddr_t			mr_va;
377 	ib_memlen_t			mr_len;
378 	ibt_lkey_t			mr_lkey;
379 	ibt_rkey_t			mr_rkey;
380 	avl_node_t			mr_avl;
381 } srpt_mr_t;
382 
383 /*
384  * SRP Target vmem arena definition
385  */
386 typedef struct srpt_vmem_pool_s {
387 	srpt_ioc_t		*svp_ioc;
388 	ib_memlen_t		svp_chunksize;
389 	vmem_t			*svp_vmem;
390 	uint64_t		svp_total_size;
391 	uint64_t		svp_max_size;
392 	avl_tree_t		svp_mr_list;
393 	krwlock_t		svp_lock;
394 	ibt_mr_flags_t		svp_flags;
395 } srpt_vmem_pool_t;
396 
397 /*
398  * SRP port provider data buffer, allocated and freed
399  * via calls to the IOC datastore.
400  */
401 typedef struct srpt_ds_dbuf_s {
402 	stmf_data_buf_t			*db_stmf_buf;
403 	srpt_ioc_t			*db_ioc;
404 	ibt_mr_hdl_t			db_mr_hdl;
405 	ibt_wr_ds_t			db_sge;
406 	srpt_iu_t			*db_iu;
407 } srpt_ds_dbuf_t;
408 
409 /*
410  * SRP Target service state
411  */
412 typedef enum {
413 	SRPT_SVC_DISABLED,
414 	SRPT_SVC_ENABLED
415 } srpt_svc_state_t;
416 
417 typedef struct {
418 	ddi_modhandle_t		ibdmah;
419 	ibdma_hdl_t		(*ibdma_register)(ib_guid_t,
420 				    ib_dm_ioc_ctrl_profile_t *, ib_dm_srv_t *);
421 	ibdma_status_t		(*ibdma_unregister)(ibdma_hdl_t);
422 	ibdma_status_t		(*ibdma_update)(ibdma_hdl_t,
423 				    ib_dm_ioc_ctrl_profile_t *, ib_dm_srv_t *);
424 } srpt_ibdma_ops_t;
425 
426 /*
427  * SRP Target protocol driver context data structure, maintaining
428  * the global state of the protocol.
429  */
430 typedef struct srpt_ctxt_s {
431 	dev_info_t			*sc_dip;
432 	krwlock_t			sc_rwlock;
433 	srpt_svc_state_t		sc_svc_state;
434 
435 	ibt_clnt_hdl_t			sc_ibt_hdl;
436 
437 	/*
438 	 * SRP Target I/O Controllers. Each IBT HCA represents an
439 	 * I/O Controller.  Must hold rwlock as a writer to update.
440 	 */
441 	list_t				sc_ioc_list;
442 	uint_t				sc_num_iocs;
443 
444 	/* Back-end COMSTAR port provider interface. */
445 	stmf_port_provider_t		*sc_pp;
446 
447 	/* IBDMA entry points */
448 	srpt_ibdma_ops_t		sc_ibdma_ops;
449 } srpt_ctxt_t;
450 
451 typedef struct srpt_iu_data_s {
452 	union {
453 		uint8_t			srp_op;
454 		srp_cmd_req_t		srp_cmd;
455 		srp_tsk_mgmt_t		srp_tsk_mgmt;
456 		srp_i_logout_t		srp_i_logout;
457 		srp_rsp_t		srp_rsp;
458 	} rx_iu;
459 } srpt_iu_data_t;
460 
461 extern srpt_ctxt_t *srpt_ctxt;
462 
463 /*
464  * For Non recoverable or Major Errors
465  */
466 #define	SRPT_LOG_L0	0
467 
468 /*
469  * For additional information on Non recoverable errors and
470  * warnings/informational message for sys-admin types.
471  */
472 #define	SRPT_LOG_L1	1
473 
474 /*
475  * debug only
476  * for more verbose trace than L1, for e.g. recoverable errors,
477  * or intersting trace
478  */
479 #define	SRPT_LOG_L2	2
480 
481 /*
482  * debug only
483  * for more verbose trace than L2, for e.g. printing function entries....
484  */
485 #define	SRPT_LOG_L3	3
486 
487 /*
488  * debug only
489  * for more verbose trace than L3, for e.g. printing minor function entries...
490  */
491 #define	SRPT_LOG_L4	4
492 
493 /*
494  * srpt_errlevel can be set in the debugger to enable additional logging.
495  * You can also add set srpt:srpt_errlevel={0,1,2,3,4} in /etc/system.
496  * The default log level is L1.
497  */
498 #define	SRPT_LOG_DEFAULT_LEVEL SRPT_LOG_L1
499 
500 extern uint_t srpt_errlevel;
501 
502 
503 #define	SRPT_DPRINTF_L0(...) cmn_err(CE_WARN, __VA_ARGS__)
504 #define	SRPT_DPRINTF_L1(...) cmn_err(CE_NOTE, __VA_ARGS__)
505 #define	SRPT_DPRINTF_L2(...)	if (srpt_errlevel >= SRPT_LOG_L2) { \
506 					cmn_err(CE_NOTE, __VA_ARGS__);\
507 				}
508 #ifdef	DEBUG
509 #define	SRPT_DPRINTF_L3(...)	if (srpt_errlevel >= SRPT_LOG_L3) { \
510 					cmn_err(CE_NOTE, __VA_ARGS__);\
511 				}
512 #define	SRPT_DPRINTF_L4(...)	if (srpt_errlevel >= SRPT_LOG_L4) { \
513 					cmn_err(CE_NOTE, __VA_ARGS__);\
514 				}
515 #else
516 #define	SRPT_DPRINTF_L3		0 &&
517 #define	SRPT_DPRINTF_L4		0 &&
518 #endif
519 
520 #ifdef __cplusplus
521 }
522 #endif
523 
524 #endif /* _SRPT_IMPL_H_ */
525