17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d91393a8SChris Horne  * Common Development and Distribution License (the "License").
6d91393a8SChris Horne  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2255e592a2SRandall Ralphs  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23f3f5a4ddSjongkis  * Use is subject to license terms.
24*8b95ebbdSRobert Mustacchi  * Copyright (c) 2017, Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef	_SYS_SCSI_SCSI_ADDRESS_H
287c478bd9Sstevel@tonic-gate #define	_SYS_SCSI_SCSI_ADDRESS_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/scsi/scsi_types.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * SCSI address definition.
387c478bd9Sstevel@tonic-gate  *
3953a7b6b6SChris Horne  * A scsi_address(9S) structure stores the host adapter routing and
4053a7b6b6SChris Horne  * scsi_device(9S) unit-address routing information necessary to reference
4153a7b6b6SChris Horne  * a specific SCSI target device logical unit function.
427c478bd9Sstevel@tonic-gate  *
4353a7b6b6SChris Horne  * Host adapter routing information is stored in the scsi_hba_tran(9S)
4453a7b6b6SChris Horne  * structure, pointed to by the scsi_address(9S) 'a_hba_tran' field.
457c478bd9Sstevel@tonic-gate  *
4653a7b6b6SChris Horne  * The scsi_device(9S) unit-address routing information (i.e. SCSA's
4753a7b6b6SChris Horne  * representation of leaf disk/tape driver's "@unit-address" portion of
4853a7b6b6SChris Horne  * a /devices path) is maintained in three different forms:
49f3f5a4ddSjongkis  *
5053a7b6b6SChris Horne  * SCSI_HBA_ADDR_SPI: In SCSI_HBA_ADDR_SPI mode (default), the SCSA
5153a7b6b6SChris Horne  *	framework, during initialization, places unit-address property
5253a7b6b6SChris Horne  *	information, converted to numeric form, directly into the
5353a7b6b6SChris Horne  *	'a_target' and 'a_lun' fields of the scsi_address(9S) structure
5453a7b6b6SChris Horne  *	(embedded in the scsi_device(9S) structure). To maintain
5553a7b6b6SChris Horne  *	per-scsi_device(9S) state, host adapter drivers often use
5653a7b6b6SChris Horne  *	'a_target' and 'a_lun' to index into a large fixed array
5753a7b6b6SChris Horne  *	(limited by the drivers idea of maximum supported target and
5853a7b6b6SChris Horne  *	lun).
59f3f5a4ddSjongkis  *
6053a7b6b6SChris Horne  *	NOTE: a_sublun is reserved for internal use only and has never
6153a7b6b6SChris Horne  *	been part of DDI scsi_address(9S).
6253a7b6b6SChris Horne  *
6353a7b6b6SChris Horne  * SCSI_HBA_ADDR_COMPLEX: The host adapter driver will maintain
6453a7b6b6SChris Horne  *	per-unit-address/per-scsi_device(9S) HBA private state by using
6553a7b6b6SChris Horne  *	scsi_device_hba_private_set(9F) during tran_tgt_init(9E) (using
6653a7b6b6SChris Horne  *	property interfaces to obtain/convert unit-address information into
6753a7b6b6SChris Horne  *	a host adapter private form).  In SCSI_HBA_ADDR_COMPLEX mode, the SCSA
6853a7b6b6SChris Horne  *	framework, prior to tran_tgt_init(9E), places a pointer to the
6953a7b6b6SChris Horne  *	scsi_device(9S) in the 'a.a_sd' scsi_address(9S) field, and uses
7053a7b6b6SChris Horne  *	'sd_hba_private' to store per-scsi_device hba private data.
7153a7b6b6SChris Horne  *
7253a7b6b6SChris Horne  * SCSI_HBA_TRAN_CLONE: SCSI_HBA_TRAN_CLONE is an older method for
7353a7b6b6SChris Horne  *	supporting devices with non-SPI unit-address. It is still
7453a7b6b6SChris Horne  *	supported, but its use is discouraged. From a unit-address
7553a7b6b6SChris Horne  *	perspective, operation is similar to SCSI_HBA_ADDR_COMPLEX, but
7653a7b6b6SChris Horne  *	per-scsi_device(9S) state is supported via 'cloning' of the
7753a7b6b6SChris Horne  *	scsi_hba_tran(9S) structure (to provide a per-scsi_device(9S)
7853a7b6b6SChris Horne  *	version of 'tran_tgt_private'/'tran_sd' accessible via
7953a7b6b6SChris Horne  *	'a_hba_tran').
8053a7b6b6SChris Horne  *
8153a7b6b6SChris Horne  * NOTE: Compatible evolution of SCSA is constrained by the fact that the
8253a7b6b6SChris Horne  * scsi_address(9S) structure is embedded at the base of the scsi_device(9S)
8353a7b6b6SChris Horne  * structure, and is structure copied into the base of each allocated
8453a7b6b6SChris Horne  * scsi_pkt(9S) structure.
8553a7b6b6SChris Horne  *
8653a7b6b6SChris Horne  * In general, device unit-address information is used exclusively by
8753a7b6b6SChris Horne  * the host adapter driver (the exception being target drivers
8853a7b6b6SChris Horne  * communicating with SCSI Parallel Interconnect (SPI) SCSI-1 devices
8953a7b6b6SChris Horne  * that embed SCSI logical unit addressing in the CDB). Target drivers
9053a7b6b6SChris Horne  * which need to communicate with SPI SCSI-1 devices that embed logical
9153a7b6b6SChris Horne  * unit addresses in the CDB must obtain target and logical unit
9253a7b6b6SChris Horne  * addresses from the device's properties (SCSI_ADDR_PROP_TARGET and
9353a7b6b6SChris Horne  * SCSI_ADDR_PROP_LUN).
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate struct scsi_address {
9653a7b6b6SChris Horne 	struct scsi_hba_tran	*a_hba_tran;	/* Transport vector */
9753a7b6b6SChris Horne 	union {
9853a7b6b6SChris Horne 		struct {			/* SPI: */
9953a7b6b6SChris Horne 			ushort_t a_target;	/* ua target */
10053a7b6b6SChris Horne 			uchar_t	 a_lun;		/* ua lun on target */
10153a7b6b6SChris Horne 			uchar_t	 _a_sublun;	/* (private) */
10253a7b6b6SChris Horne 		} spi;
10353a7b6b6SChris Horne 		struct scsi_device *a_sd;	/* COMPLEX: (private) */
10453a7b6b6SChris Horne 	} a;					/* device unit-adddress info */
1057c478bd9Sstevel@tonic-gate };
10653a7b6b6SChris Horne #define	a_target	a.spi.a_target
10753a7b6b6SChris Horne #define	a_lun		a.spi.a_lun
10853a7b6b6SChris Horne #define	a_sublun	a.spi._a_sublun
1097c478bd9Sstevel@tonic-gate 
110d91393a8SChris Horne /* Device unit-address property names */
11155e592a2SRandall Ralphs #define	SCSI_ADDR_PROP_TARGET		"target"	/* int */
11255e592a2SRandall Ralphs #define	SCSI_ADDR_PROP_LUN		"lun"		/* int */
11355e592a2SRandall Ralphs 
11455e592a2SRandall Ralphs #define	SCSI_ADDR_PROP_TARGET_PORT	"target-port"	/* string */
11555e592a2SRandall Ralphs #define	SCSI_ADDR_PROP_LUN64		"lun64"		/* int64 */
11655e592a2SRandall Ralphs #define	SCSI_ADDR_PROP_SFUNC		"sfunc"		/* int */
11755e592a2SRandall Ralphs 
1184c06356bSdh #define	SCSI_ADDR_PROP_IPORTUA		"scsi-iport"	/* string */
1194c06356bSdh 
1204c06356bSdh #define	SCSI_ADDR_PROP_SATA_PHY		"sata-phy"	/* int */
1214c06356bSdh 
12255e592a2SRandall Ralphs /*
12355e592a2SRandall Ralphs  * Addressing property names, values are in string form compatible
12455e592a2SRandall Ralphs  * with the SCSI_ADDR_PROP_TARGET_PORT part of the related
12555e592a2SRandall Ralphs  * IEEE-1275 OpenFirmware binding unit-address string.
12655e592a2SRandall Ralphs  */
12755e592a2SRandall Ralphs #define	SCSI_ADDR_PROP_INITIATOR_PORT	"initiator-port"
12855e592a2SRandall Ralphs #define	SCSI_ADDR_PROP_ATTACHED_PORT	"attached-port"
12955e592a2SRandall Ralphs #define	SCSI_ADDR_PROP_BRIDGE_PORT	"bridge-port"
130f3f5a4ddSjongkis 
131d91393a8SChris Horne /*
132d91393a8SChris Horne  * Normalized representation of a scsi_lun (with SCSI-2 lun positioned
133d91393a8SChris Horne  * for compatibility).
134d91393a8SChris Horne  */
135d91393a8SChris Horne typedef uint64_t	scsi_lun64_t;
136d91393a8SChris Horne #define	PRIlun64	PRIx64
137d91393a8SChris Horne #ifdef	_LP64
138d91393a8SChris Horne #define	SCSI_LUN64_ILLEGAL	(-1L)
139d91393a8SChris Horne #else	/* _LP64 */
140d91393a8SChris Horne #define	SCSI_LUN64_ILLEGAL	(-1LL)
141d91393a8SChris Horne #endif	/* _LP64 */
142d91393a8SChris Horne 
143d91393a8SChris Horne /* Structure of a 64-bit SCSI LUN per SCSI standard */
144d91393a8SChris Horne typedef	struct scsi_lun {
145d91393a8SChris Horne 	uchar_t	sl_lun1_msb;	/* format */
146d91393a8SChris Horne 	uchar_t	sl_lun1_lsb;	/* first level */
147d91393a8SChris Horne 	uchar_t	sl_lun2_msb;
148d91393a8SChris Horne 	uchar_t	sl_lun2_lsb;	/* second level */
149d91393a8SChris Horne 	uchar_t	sl_lun3_msb;
150d91393a8SChris Horne 	uchar_t	sl_lun3_lsb;	/* third level */
151d91393a8SChris Horne 	uchar_t	sl_lun4_msb;
152d91393a8SChris Horne 	uchar_t	sl_lun4_lsb;	/* fourth level */
153d91393a8SChris Horne } scsi_lun_t;
154d91393a8SChris Horne 
155ed141cfcSsrivijitha dugganapalli /* SCSI standard defined lun addressing methods (in sl_lunX_msb) */
156ed141cfcSsrivijitha dugganapalli #define	SCSI_LUN_AM_MASK	0xC0		/* Address Method Mask */
157ed141cfcSsrivijitha dugganapalli #define	SCSI_LUN_AM_PDEV	0x00		/* Peripheral device AM */
158ed141cfcSsrivijitha dugganapalli #define	SCSI_LUN_AM_FLAT	0x40		/* Flat space AM */
159ed141cfcSsrivijitha dugganapalli #define	SCSI_LUN_AM_LUN		0x80		/* Logical unit AM */
160ed141cfcSsrivijitha dugganapalli #define	SCSI_LUN_AM_EFLAT	0xC0		/* Extended flat space AM */
161ed141cfcSsrivijitha dugganapalli #define	SCSI_LUN_AM_ELUN	0xC0		/* Extended logical unit AM */
162ed141cfcSsrivijitha dugganapalli 
163ed141cfcSsrivijitha dugganapalli #ifdef	_KERNEL
164ed141cfcSsrivijitha dugganapalli /* SCSI LUN conversion between SCSI_ADDR_PROP_LUN64 and SCSI standard forms */
165ed141cfcSsrivijitha dugganapalli scsi_lun64_t	scsi_lun_to_lun64(scsi_lun_t lun);
166ed141cfcSsrivijitha dugganapalli scsi_lun_t	scsi_lun64_to_lun(scsi_lun64_t lun64);
167ed141cfcSsrivijitha dugganapalli 
168ed141cfcSsrivijitha dugganapalli /* SCSI WWN conversion (property values should be in unit_address form) */
169ed141cfcSsrivijitha dugganapalli int		scsi_wwnstr_to_wwn(const char *wwnstr, uint64_t *wwnp);
170ed141cfcSsrivijitha dugganapalli char		*scsi_wwn_to_wwnstr(uint64_t wwn,
171ed141cfcSsrivijitha dugganapalli 		    int unit_address_form, char *wwnstr);
172ed141cfcSsrivijitha dugganapalli void		scsi_wwnstr_hexcase(char *wwnstr, int lower_case);
1734c06356bSdh const char	*scsi_wwnstr_skip_ua_prefix(const char *wwnstr);
174ed141cfcSsrivijitha dugganapalli void		scsi_free_wwnstr(char *wwnstr);
175*8b95ebbdSRobert Mustacchi 
176*8b95ebbdSRobert Mustacchi /*
177*8b95ebbdSRobert Mustacchi  * Buffer lengths for SCSI strings. SCSI_WWN_STRLEN is the length of a WWN
178*8b95ebbdSRobert Mustacchi  * that's not in unit-address form. SCSI_WWN_UA_STRLEN includes the
179*8b95ebbdSRobert Mustacchi  * unit-address. SCSI_WWN_BUFLEN provides a buffer that's large enough for all
180*8b95ebbdSRobert Mustacchi  * of these.
181*8b95ebbdSRobert Mustacchi  */
182*8b95ebbdSRobert Mustacchi #define	SCSI_WWN_STRLEN	16
183*8b95ebbdSRobert Mustacchi #define	SCSI_WWN_UA_STRLEN	17
184*8b95ebbdSRobert Mustacchi #define	SCSI_WWN_BUFLEN	SCSI_MAXNAMELEN
185*8b95ebbdSRobert Mustacchi 
186ed141cfcSsrivijitha dugganapalli #endif	/* _KERNEL */
187ed141cfcSsrivijitha dugganapalli 
1887c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate #endif
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #endif	/* _SYS_SCSI_SCSI_ADDRESS_H */
193