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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_SCSI_IMPL_TRANSPORT_H
27 #define	_SYS_SCSI_IMPL_TRANSPORT_H
28 
29 /*
30  * Include the loadable module wrapper.
31  */
32 #include <sys/modctl.h>
33 #include <sys/note.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #ifdef	_KERNEL
40 
41 /*
42  * SCSI transport structures
43  *
44  *	As each Host Adapter makes itself known to the system,
45  *	it will create and register with the library the structure
46  *	described below. This is so that the library knows how to route
47  *	packets, resource control requests, and capability requests
48  *	for any particular host adapter. The 'a_hba_tran' field of a
49  *	scsi_address structure made known to a Target driver will
50  *	point to one of these transport structures.
51  */
52 
53 typedef struct scsi_hba_tran	scsi_hba_tran_t;
54 
55 struct scsi_hba_tran {
56 	/*
57 	 * Ptr to the device info structure for this particular HBA.
58 	 */
59 	dev_info_t	*tran_hba_dip;
60 
61 	/*
62 	 * Private fields for use by the HBA itself.
63 	 */
64 	void		*tran_hba_private;	/* HBA softstate */
65 
66 	/*
67 	 * The following two fields are only used in the SCSI_HBA_TRAN_CLONE
68 	 * case. Consider using SCSI_HBA_ADDR_COMPLEX instead.
69 	 */
70 	void			*tran_tgt_private;
71 	struct scsi_device	*tran_sd;
72 
73 	/*
74 	 * Vectors to point to specific HBA entry points
75 	 */
76 	int		(*tran_tgt_init)(
77 				dev_info_t		*hba_dip,
78 				dev_info_t		*tgt_dip,
79 				scsi_hba_tran_t		*tran,
80 				struct scsi_device	*sd);
81 
82 	int		(*tran_tgt_probe)(
83 				struct scsi_device	*sd,
84 				int			(*callback)(
85 								void));
86 	void		(*tran_tgt_free)(
87 				dev_info_t		*hba_dip,
88 				dev_info_t		*tgt_dip,
89 				scsi_hba_tran_t		*tran,
90 				struct scsi_device	*sd);
91 
92 	int		(*tran_start)(
93 				struct scsi_address	*ap,
94 				struct scsi_pkt		*pkt);
95 
96 	int		(*tran_reset)(
97 				struct scsi_address	*ap,
98 				int			level);
99 
100 	int		(*tran_abort)(
101 				struct scsi_address	*ap,
102 				struct scsi_pkt		*pkt);
103 
104 	int		(*tran_getcap)(
105 				struct scsi_address	*ap,
106 				char			*cap,
107 				int			whom);
108 
109 	int		(*tran_setcap)(
110 				struct scsi_address	*ap,
111 				char			*cap,
112 				int			value,
113 				int			whom);
114 
115 	struct scsi_pkt	*(*tran_init_pkt)(
116 				struct scsi_address	*ap,
117 				struct scsi_pkt		*pkt,
118 				struct buf		*bp,
119 				int			cmdlen,
120 				int			statuslen,
121 				int			tgtlen,
122 				int			flags,
123 				int			(*callback)(
124 								caddr_t	arg),
125 				caddr_t			callback_arg);
126 
127 	void		(*tran_destroy_pkt)(
128 				struct scsi_address	*ap,
129 				struct scsi_pkt		*pkt);
130 
131 	void		(*tran_dmafree)(
132 				struct scsi_address	*ap,
133 				struct scsi_pkt		*pkt);
134 
135 	void		(*tran_sync_pkt)(
136 				struct scsi_address	*ap,
137 				struct scsi_pkt		*pkt);
138 
139 	int		(*tran_reset_notify)(
140 				struct scsi_address	*ap,
141 				int			flag,
142 				void			(*callback)(caddr_t),
143 				caddr_t			arg);
144 
145 	int		(*tran_get_bus_addr)(
146 				struct scsi_device	*sd,
147 				char			*name,
148 				int			len);
149 
150 	int		(*tran_get_name)(
151 				struct scsi_device	*sd,
152 				char			*name,
153 				int			len);
154 
155 	int		(*tran_clear_aca)(
156 				struct scsi_address	*ap);
157 
158 	int		(*tran_clear_task_set)(
159 				struct scsi_address	*ap);
160 
161 	int		(*tran_terminate_task)(
162 				struct scsi_address	*ap,
163 				struct scsi_pkt		*pkt);
164 
165 	int		(*tran_get_eventcookie)(
166 				dev_info_t		*hba_dip,
167 				dev_info_t		*tgt_dip,
168 				char			*name,
169 				ddi_eventcookie_t	*eventp);
170 
171 	int		(*tran_add_eventcall)(
172 				dev_info_t		*hba_dip,
173 				dev_info_t		*tgt_dip,
174 				ddi_eventcookie_t	event,
175 				void			(*callback)(
176 						dev_info_t *tgt_dip,
177 						ddi_eventcookie_t event,
178 						void *arg,
179 						void *bus_impldata),
180 				void			*arg,
181 				ddi_callback_id_t *cb_id);
182 
183 	int		(*tran_remove_eventcall)(dev_info_t *devi,
184 			ddi_callback_id_t cb_id);
185 
186 	int		(*tran_post_event)(
187 				dev_info_t		*hba_dip,
188 				dev_info_t		*tgt_dip,
189 				ddi_eventcookie_t	event,
190 				void			*bus_impldata);
191 
192 	int		(*tran_quiesce)(
193 				dev_info_t		*hba_dip);
194 
195 	int		(*tran_unquiesce)(
196 				dev_info_t		*hba_dip);
197 
198 	int		(*tran_bus_reset)(
199 				dev_info_t		*hba_dip,
200 				int			level);
201 
202 	/*
203 	 * Implementation-private specifics.
204 	 * No HBA should refer to any of the fields below.
205 	 * This information can and will change.
206 	 */
207 	int			tran_hba_flags;		/* flag options */
208 
209 	uint_t			tran_obs1;
210 	uchar_t			tran_obs2;
211 	uchar_t			tran_obs3;
212 
213 	/*
214 	 * open_lock: protect tran_minor_isopen
215 	 * open_flag: bit field indicating which minor nodes are open.
216 	 *	0 = closed, 1 = shared open, all bits 1 = excl open.
217 	 *
218 	 * XXX Unused if hba driver chooses to implement own
219 	 *	xxopen(9e) entry point
220 	 */
221 	kmutex_t		tran_open_lock;
222 	uint64_t		tran_open_flag;
223 
224 	/*
225 	 * bus_config vectors - ON Consolidation Private
226 	 * These interfaces are subject to change.
227 	 */
228 	int		(*tran_bus_config)(
229 				dev_info_t		*hba_dip,
230 				uint_t			flag,
231 				ddi_bus_config_op_t	op,
232 				void			*arg,
233 				dev_info_t		**tgt_dipp);
234 
235 	int		(*tran_bus_unconfig)(
236 				dev_info_t		*hba_dip,
237 				uint_t			flag,
238 				ddi_bus_config_op_t	op,
239 				void			*arg);
240 
241 	int		(*tran_bus_power)(
242 				dev_info_t		*dip,
243 				void			*impl_arg,
244 				pm_bus_power_op_t	op,
245 				void			*arg,
246 				void			*result);
247 
248 	/*
249 	 * Inter-Connect type of trasnport as defined in
250 	 * usr/src/uts/common/sys/scsi/impl/services.h
251 	 */
252 	int		tran_interconnect_type;
253 
254 	/* tran_setup_pkt(9E) related scsi_pkt fields */
255 	int		(*tran_pkt_constructor)(
256 				struct scsi_pkt		*pkt,
257 				scsi_hba_tran_t		*tran,
258 				int			kmflag);
259 	void		(*tran_pkt_destructor)(
260 				struct scsi_pkt		*pkt,
261 				scsi_hba_tran_t		*tran);
262 	kmem_cache_t	*tran_pkt_cache_ptr;
263 	uint_t		tran_hba_len;
264 	int		(*tran_setup_pkt)(
265 				struct scsi_pkt		*pkt,
266 				int			(*callback)(
267 								caddr_t	arg),
268 				caddr_t			callback_arg);
269 	void		(*tran_teardown_pkt)(
270 				struct scsi_pkt		*pkt);
271 	ddi_dma_attr_t	tran_dma_attr;
272 
273 	void		*tran_extension;
274 
275 	/*
276 	 * An fm_capable HBA driver can set tran_fm_capable prior to
277 	 * scsi_hba_attach_setup(). If not set, SCSA provides a default
278 	 * implementation.
279 	 */
280 	int		tran_fm_capable;
281 
282 #ifdef	SCSI_SIZE_CLEAN_VERIFY
283 	/*
284 	 * Must be last: Building a driver with-and-without
285 	 * -DSCSI_SIZE_CLEAN_VERIFY, and checking driver modules for
286 	 * differences with a tools like 'wsdiff' allows a developer to verify
287 	 * that their driver has no dependencies on scsi*(9S) size.
288 	 */
289 	int		_pad[8];
290 #endif	/* SCSI_SIZE_CLEAN_VERIFY */
291 };
292 size_t	scsi_hba_tran_size();			/* private */
293 
294 #ifdef __lock_lint
295 _NOTE(SCHEME_PROTECTS_DATA("stable data",
296 	scsi_hba_tran::tran_sd
297 	scsi_hba_tran::tran_hba_dip
298 	scsi_hba_tran::tran_hba_flags
299 	scsi_hba_tran::tran_open_flag
300 	scsi_hba_tran::tran_pkt_cache_ptr))
301 /*
302  * we only modify the dma atributes (like dma_attr_granular) upon
303  * attach and in response to a setcap. It is also up to the target
304  * driver to not have any outstanding I/Os when it is changing the
305  * capabilities of the transport.
306  */
307 _NOTE(SCHEME_PROTECTS_DATA("serialized by target driver", \
308 	scsi_hba_tran::tran_dma_attr.dma_attr_granular))
309 #endif
310 
311 /*
312  * Prototypes for SCSI HBA interface functions
313  *
314  * All these functions are public interfaces, with the
315  * exception of:
316  *	interface				called by
317  *	scsi_initialize_hba_interface()		_init() of scsi module
318  *	scsi_uninitialize_hba_interface()	_fini() of scsi module
319  */
320 
321 void		scsi_initialize_hba_interface(void);
322 
323 #ifdef	NO_SCSI_FINI_YET
324 void		scsi_uninitialize_hba_interface(void);
325 #endif	/* NO_SCSI_FINI_YET */
326 
327 int		scsi_hba_init(
328 				struct modlinkage	*modlp);
329 
330 void		scsi_hba_fini(
331 				struct modlinkage	*modlp);
332 
333 int		scsi_hba_attach(
334 				dev_info_t		*hba_dip,
335 				ddi_dma_lim_t		*hba_lim,
336 				scsi_hba_tran_t		*tran,
337 				int			flags,
338 				void			*hba_options);
339 
340 int		scsi_hba_attach_setup(
341 				dev_info_t		*hba_dip,
342 				ddi_dma_attr_t		*hba_dma_attr,
343 				scsi_hba_tran_t		*tran,
344 				int			flags);
345 
346 int		scsi_hba_detach(
347 				dev_info_t		*hba_dip);
348 
349 scsi_hba_tran_t	*scsi_hba_tran_alloc(
350 				dev_info_t		*hba_dip,
351 				int			flags);
352 
353 int		scsi_tran_ext_alloc(
354 				scsi_hba_tran_t		*tran,
355 				size_t			length,
356 				int			flags);
357 
358 void		scsi_tran_ext_free(
359 				scsi_hba_tran_t		*tran,
360 				size_t			length);
361 
362 void		scsi_hba_tran_free(
363 				scsi_hba_tran_t		*tran);
364 
365 int		scsi_hba_probe(
366 				struct scsi_device	*sd,
367 				int			(*callback)(void));
368 
369 char			*scsi_get_device_type_string(
370 				char			*prop_name,
371 				dev_info_t		*hba_dip,
372 				struct scsi_device	*sd);
373 
374 int		scsi_get_scsi_maxluns(
375 				struct scsi_device	*sd);
376 
377 int		scsi_get_scsi_options(
378 				struct scsi_device	*sd,
379 				int			default_scsi_options);
380 
381 int		scsi_get_device_type_scsi_options(
382 				dev_info_t		*hba_dip,
383 				struct scsi_device	*sd,
384 				int			default_scsi_options);
385 
386 struct scsi_pkt	*scsi_hba_pkt_alloc(
387 				dev_info_t		*hba_dip,
388 				struct scsi_address	*ap,
389 				int			cmdlen,
390 				int			statuslen,
391 				int			tgtlen,
392 				int			hbalen,
393 				int			(*callback)(caddr_t),
394 				caddr_t			arg);
395 
396 void		scsi_hba_pkt_free(
397 				struct scsi_address	*ap,
398 				struct scsi_pkt		*pkt);
399 
400 
401 int		scsi_hba_lookup_capstr(
402 				char			*capstr);
403 
404 int		scsi_hba_in_panic(void);
405 
406 int		scsi_hba_open(
407 				dev_t			*devp,
408 				int			flags,
409 				int			otyp,
410 				cred_t			*credp);
411 
412 int		scsi_hba_close(
413 				dev_t			dev,
414 				int			flag,
415 				int			otyp,
416 				cred_t			*credp);
417 
418 int		scsi_hba_ioctl(
419 				dev_t			dev,
420 				int			cmd,
421 				intptr_t		arg,
422 				int			mode,
423 				cred_t			*credp,
424 				int			*rvalp);
425 
426 void		scsi_hba_nodename_compatible_get(
427 				struct scsi_inquiry	*inq,
428 				char			*binding_set,
429 				int			dtype_node,
430 				char			*compat0,
431 				char			**nodenamep,
432 				char			***compatiblep,
433 				int			*ncompatiblep);
434 
435 void		scsi_hba_nodename_compatible_free(
436 				char			*nodename,
437 				char			**compatible);
438 
439 
440 int		scsi_hba_prop_update_inqstring(
441 				struct scsi_device	*sd,
442 				char			*name,
443 				char			*data,
444 				size_t			len);
445 
446 void		scsi_hba_pkt_comp(
447 				struct scsi_pkt		*pkt);
448 /*
449  * Flags for scsi_hba_attach
450  *
451  * SCSI_HBA_ADDR_SPI		The host adapter driver wants the
452  *				scsi_address(9S) structure to be maintained
453  *				in legacy SPI 'a_target'/'a_lun' form (default).
454  *
455  * SCSI_HBA_ADDR_COMPLEX	The host adapter has a complex unit-address
456  *				space, and the HBA driver wants to maintain
457  *				per-scsi_device(9S) HBA private data using
458  *				scsi_address_device(9F) and
459  *				scsi_device_hba_private_[gs]et(9F).  The HBA
460  *				driver must maintain a private representation
461  *				of the scsi_device(9S) unit-address - typically
462  *				established during tran_tgt_init(9F) based on
463  *				property values.
464  *
465  * SCSI_HBA_TRAN_CLONE		Consider using SCSI_HBA_ADDR_COMPLEX instead.
466  *				SCSI_HBA_TRAN_CLONE is a KLUDGE to address
467  *				limitations of the scsi_address(9S) structure
468  *				via duplication of scsi_hba_tran(9S) and
469  *				use of tran_tgt_private.
470  */
471 #define	SCSI_HBA_TRAN_CLONE	0x01		/* clone scsi_hba_tran(9S) */
472 						/* structure per scsi_device */
473 #define	SCSI_HBA_TRAN_ALLOC	0x02		/* set if scsi_hba_tran_alloc */
474 						/* is called */
475 #define	SCSI_HBA_TRAN_CDB	0x04		/* allocate cdb */
476 #define	SCSI_HBA_TRAN_SCB	0x08		/* allocate sense */
477 #define	SCSI_HBA_TRAN_FMSCSA	0x10		/* using common ddi_fm_* */
478 
479 #define	SCSI_HBA_ADDR_SPI	0x20		/* scsi_address in SPI form */
480 #define	SCSI_HBA_ADDR_COMPLEX	0x40		/* scsi_address is COMPLEX */
481 
482 /*
483  * Flags for scsi_hba allocation functions
484  */
485 #define	SCSI_HBA_CANSLEEP	0x01		/* can sleep */
486 
487 /*
488  * Support extra flavors for SCSA children
489  */
490 #define	SCSA_FLAVOR_SCSI_DEVICE	NDI_FLAVOR_VANILLA
491 #define	SCSA_FLAVOR_IPORT	1
492 #define	SCSA_FLAVOR_SMP		2
493 #define	SCSA_NFLAVORS		3
494 
495 /*
496  * For minor nodes created by the SCSA framework, minor numbers are
497  * formed by left-shifting instance by INST_MINOR_SHIFT and OR in a
498  * number less than 64.
499  *
500  * - Numbers 0 - 31 are reserved by the framework, part of the range are
501  *	in use, as defined below.
502  *
503  * - Numbers 32 - 63 are available for HBA driver use.
504  */
505 #define	INST_MINOR_SHIFT	6
506 #define	TRAN_MINOR_MASK		((1 << INST_MINOR_SHIFT) - 1)
507 #define	TRAN_OPEN_EXCL		(uint64_t)-1
508 
509 #define	DEVCTL_MINOR		0
510 #define	SCSI_MINOR		1
511 
512 #define	INST2DEVCTL(x)		(((x) << INST_MINOR_SHIFT) | DEVCTL_MINOR)
513 #define	INST2SCSI(x)		(((x) << INST_MINOR_SHIFT) | SCSI_MINOR)
514 #define	MINOR2INST(x)		((x) >> INST_MINOR_SHIFT)
515 
516 #endif	/* _KERNEL */
517 
518 #ifdef	__cplusplus
519 }
520 #endif
521 
522 #endif	/* _SYS_SCSI_IMPL_TRANSPORT_H */
523