xref: /illumos-gate/usr/src/cmd/devfsadm/disk_link.c (revision ca0df52a)
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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * 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  */
21c73799ddSYuri Pankov 
227c478bd9Sstevel@tonic-gate /*
23406fc510SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
24*ca0df52aSHans Rosenfeld  * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
25d88e498aSjiang wu - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <devfsadm.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <limits.h>
3497869ac5Sjhd #include <ctype.h>
35406fc510SToomas Soome #include <unistd.h>
364c06356bSdh #include <sys/int_fmtio.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
3845916cd2Sjpk #include <bsm/devalloc.h>
394c06356bSdh #include <sys/scsi/scsi_address.h>
404c06356bSdh #include <sys/libdevid.h>
41406fc510SToomas Soome #include <sys/lofi.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	DISK_SUBPATH_MAX 100
447c478bd9Sstevel@tonic-gate #define	RM_STALE 0x01
457c478bd9Sstevel@tonic-gate #define	DISK_LINK_RE	"^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$"
467c478bd9Sstevel@tonic-gate #define	DISK_LINK_TO_UPPER(ch)\
477c478bd9Sstevel@tonic-gate 	(((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch)
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	SLICE_SMI	"s7"
507c478bd9Sstevel@tonic-gate #define	SLICE_EFI	""
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	MN_SMI		"h"
537c478bd9Sstevel@tonic-gate #define	MN_EFI		"wd"
547c478bd9Sstevel@tonic-gate #define	ASCIIWWNSIZE	255
55aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
56aa1b14e7SSheshadri Vasudevan /*
57aa1b14e7SSheshadri Vasudevan  * The number of minor nodes per LUN is defined by the disk drivers.
58aa1b14e7SSheshadri Vasudevan  * Currently it is set to 64. Refer CMLBUNIT_SHIFT (cmlb_impl.h)
59aa1b14e7SSheshadri Vasudevan  */
60aa1b14e7SSheshadri Vasudevan #define	NUM_MINORS_PER_INSTANCE	64
61aa1b14e7SSheshadri Vasudevan #endif
62aa1b14e7SSheshadri Vasudevan 
637c478bd9Sstevel@tonic-gate 
6445916cd2Sjpk extern int system_labeled;
6545916cd2Sjpk 
667c478bd9Sstevel@tonic-gate static int disk_callback_chan(di_minor_t minor, di_node_t node);
677c478bd9Sstevel@tonic-gate static int disk_callback_nchan(di_minor_t minor, di_node_t node);
68df8c2287SHans Rosenfeld static int disk_callback_blkdev(di_minor_t minor, di_node_t node);
697c478bd9Sstevel@tonic-gate static int disk_callback_wwn(di_minor_t minor, di_node_t node);
7097869ac5Sjhd static int disk_callback_xvmd(di_minor_t minor, di_node_t node);
717c478bd9Sstevel@tonic-gate static int disk_callback_fabric(di_minor_t minor, di_node_t node);
72d88e498aSjiang wu - Sun Microsystems - Beijing China static int disk_callback_sas(di_minor_t minor, di_node_t node);
737c478bd9Sstevel@tonic-gate static void disk_common(di_minor_t minor, di_node_t node, char *disk,
747c478bd9Sstevel@tonic-gate 				int flags);
757c478bd9Sstevel@tonic-gate static char *diskctrl(di_node_t node, di_minor_t minor);
768d483882Smlf static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags);
77406fc510SToomas Soome static void disk_rm_lofi_all(char *file);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static devfsadm_create_t disk_cbt[] = {
814c06356bSdh 	{ "disk", DDI_NT_BLOCK, NULL,
827c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
837c478bd9Sstevel@tonic-gate 	},
844c06356bSdh 	{ "disk", DDI_NT_BLOCK_CHAN, NULL,
857c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
867c478bd9Sstevel@tonic-gate 	},
87df8c2287SHans Rosenfeld 	{ "disk", DDI_NT_BLOCK_BLKDEV, NULL,
88df8c2287SHans Rosenfeld 	    TYPE_EXACT, ILEVEL_0, disk_callback_blkdev
89df8c2287SHans Rosenfeld 	},
904c06356bSdh 	{ "disk", DDI_NT_BLOCK_FABRIC, NULL,
917c478bd9Sstevel@tonic-gate 		TYPE_EXACT, ILEVEL_0, disk_callback_fabric
927c478bd9Sstevel@tonic-gate 	},
934c06356bSdh 	{ "disk", DDI_NT_BLOCK_WWN, NULL,
947c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_wwn
957c478bd9Sstevel@tonic-gate 	},
964c06356bSdh 	{ "disk", DDI_NT_BLOCK_SAS, NULL,
97d88e498aSjiang wu - Sun Microsystems - Beijing China 	    TYPE_EXACT, ILEVEL_0, disk_callback_sas
98d88e498aSjiang wu - Sun Microsystems - Beijing China 	},
994c06356bSdh 	{ "disk", DDI_NT_CD, NULL,
1007c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
1017c478bd9Sstevel@tonic-gate 	},
1024c06356bSdh 	{ "disk", DDI_NT_CD_CHAN, NULL,
1037c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
1047c478bd9Sstevel@tonic-gate 	},
1054c06356bSdh 	{ "disk", DDI_NT_BLOCK_XVMD, NULL,
10697869ac5Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
10797869ac5Sjhd 	},
1084c06356bSdh 	{ "disk", DDI_NT_CD_XVMD, NULL,
10997869ac5Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
11097869ac5Sjhd 	},
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(disk_cbt);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
116406fc510SToomas Soome  * HOT auto cleanup of disks is done for lofi devices only.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static devfsadm_remove_t disk_remove_cbt[] = {
119406fc510SToomas Soome 	{ "disk", DISK_LINK_RE, RM_HOT | RM_POST | RM_ALWAYS,
120406fc510SToomas Soome 		ILEVEL_0, disk_rm_lofi_all
121406fc510SToomas Soome 	},
1227c478bd9Sstevel@tonic-gate 	{ "disk", DISK_LINK_RE, RM_POST,
1237c478bd9Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate };
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
1287c478bd9Sstevel@tonic-gate 
1298d483882Smlf static devlink_re_t disks_re_array[] = {
1308d483882Smlf 	{"^r?dsk/c([0-9]+)", 1},
1318d483882Smlf 	{"^cfg/c([0-9]+)$", 1},
1328d483882Smlf 	{"^scsi/.+/c([0-9]+)", 1},
1338d483882Smlf 	{NULL}
1348d483882Smlf };
1358d483882Smlf 
1368d483882Smlf static char *disk_mid = "disk_mid";
1378d483882Smlf static char *modname = "disk_link";
1388d483882Smlf 
139406fc510SToomas Soome /*
140406fc510SToomas Soome  * Check if link is from lofi by checking path from readlink().
141406fc510SToomas Soome  */
142406fc510SToomas Soome static int
is_lofi_disk(char * file)143406fc510SToomas Soome is_lofi_disk(char *file)
144406fc510SToomas Soome {
145406fc510SToomas Soome 	char buf[PATH_MAX + 1];
146406fc510SToomas Soome 	char filepath[PATH_MAX];
147406fc510SToomas Soome 	char *ptr;
148406fc510SToomas Soome 	ssize_t size;
149406fc510SToomas Soome 
150406fc510SToomas Soome 	size = snprintf(filepath, sizeof (filepath), "%s/dev/%s",
151406fc510SToomas Soome 	    devfsadm_root_path(), file);
152406fc510SToomas Soome 	if (size > sizeof (filepath))
153406fc510SToomas Soome 		return (0);
154406fc510SToomas Soome 
155406fc510SToomas Soome 	size = readlink(filepath, buf, sizeof (buf) - 1);
156406fc510SToomas Soome 	if (size == -1)
157406fc510SToomas Soome 		return (0);
158406fc510SToomas Soome 	buf[size] = '\0';
159406fc510SToomas Soome 	ptr = strchr(buf, '@');
160406fc510SToomas Soome 	if (ptr == NULL)
161406fc510SToomas Soome 		return (0);
162406fc510SToomas Soome 	ptr[1] = '\0';
163406fc510SToomas Soome 	if (strcmp(buf, "../../devices/pseudo/lofi@") != 0)
164406fc510SToomas Soome 		return (0);
165406fc510SToomas Soome 	return (1);
166406fc510SToomas Soome }
167406fc510SToomas Soome 
168406fc510SToomas Soome /*
169406fc510SToomas Soome  * Wrapper around devfsadm_rm_link() for lofi devices.
170406fc510SToomas Soome  */
disk_rm_lofi_all(char * file)171406fc510SToomas Soome static void disk_rm_lofi_all(char *file)
172406fc510SToomas Soome {
173406fc510SToomas Soome 	if (is_lofi_disk(file))
174406fc510SToomas Soome 		devfsadm_rm_link(file);
175406fc510SToomas Soome }
176406fc510SToomas Soome 
1778d483882Smlf int
minor_init()1788d483882Smlf minor_init()
1798d483882Smlf {
1808d483882Smlf 	devfsadm_print(disk_mid,
1818d483882Smlf 	    "%s: minor_init(): Creating disks reserved ID cache\n",
1828d483882Smlf 	    modname);
1838d483882Smlf 	return (devfsadm_reserve_id_cache(disks_re_array, NULL));
1848d483882Smlf }
1858d483882Smlf 
1867c478bd9Sstevel@tonic-gate static int
disk_callback_chan(di_minor_t minor,di_node_t node)1877c478bd9Sstevel@tonic-gate disk_callback_chan(di_minor_t minor, di_node_t node)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	char *addr;
190406fc510SToomas Soome 	char disk[23];
191406fc510SToomas Soome 	char *driver;
192406fc510SToomas Soome 	uint_t targ = 0;
193406fc510SToomas Soome 	uint_t lun = 0;
194406fc510SToomas Soome 
195406fc510SToomas Soome 	driver = di_driver_name(node);
196406fc510SToomas Soome 	if (strcmp(driver, LOFI_DRIVER_NAME) != 0) {
197406fc510SToomas Soome 		addr = di_bus_addr(node);
198406fc510SToomas Soome 		(void) sscanf(addr, "%X,%X", &targ, &lun);
199406fc510SToomas Soome 	} else {
200406fc510SToomas Soome 		targ = di_instance(node);
201406fc510SToomas Soome 	}
2027c478bd9Sstevel@tonic-gate 
203406fc510SToomas Soome 	(void) snprintf(disk, sizeof (disk), "t%dd%d", targ, lun);
2047c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
2057c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate static int
disk_callback_nchan(di_minor_t minor,di_node_t node)2107c478bd9Sstevel@tonic-gate disk_callback_nchan(di_minor_t minor, di_node_t node)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	char *addr;
2137c478bd9Sstevel@tonic-gate 	char disk[10];
2147c478bd9Sstevel@tonic-gate 	uint_t lun;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	addr = di_bus_addr(node);
2177c478bd9Sstevel@tonic-gate 	(void) sscanf(addr, "%X", &lun);
2187c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "d%d", lun);
2197c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
2207c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
224df8c2287SHans Rosenfeld static int
disk_callback_blkdev(di_minor_t minor,di_node_t node)225df8c2287SHans Rosenfeld disk_callback_blkdev(di_minor_t minor, di_node_t node)
226df8c2287SHans Rosenfeld {
227df8c2287SHans Rosenfeld 	char *addr;
228df8c2287SHans Rosenfeld 	char disk[DISK_SUBPATH_MAX];
229*ca0df52aSHans Rosenfeld 	char guid[50];
230df8c2287SHans Rosenfeld 	uint_t lun = 0;
231df8c2287SHans Rosenfeld 
232df8c2287SHans Rosenfeld 	addr = di_bus_addr(node);
233*ca0df52aSHans Rosenfeld 	(void) sscanf(addr, "w%49[0-9A-F],%X", &guid, &lun);
234*ca0df52aSHans Rosenfeld 	(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", guid, lun);
235df8c2287SHans Rosenfeld 	disk_common(minor, node, disk, RM_STALE);
236df8c2287SHans Rosenfeld 	return (DEVFSADM_CONTINUE);
237df8c2287SHans Rosenfeld }
238df8c2287SHans Rosenfeld 
2397c478bd9Sstevel@tonic-gate static int
disk_callback_wwn(di_minor_t minor,di_node_t node)2407c478bd9Sstevel@tonic-gate disk_callback_wwn(di_minor_t minor, di_node_t node)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	char disk[10];
2437c478bd9Sstevel@tonic-gate 	int lun;
2447c478bd9Sstevel@tonic-gate 	int targ;
2457c478bd9Sstevel@tonic-gate 	int *intp;
2467c478bd9Sstevel@tonic-gate 
2474c06356bSdh 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_TARGET,
2484c06356bSdh 	    &intp) <= 0) {
2497c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	targ = *intp;
2524c06356bSdh 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_LUN,
2534c06356bSdh 	    &intp) <= 0) {
254e37c6c37Scth 		lun = 0;
2557c478bd9Sstevel@tonic-gate 	} else {
256e37c6c37Scth 		lun = *intp;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static int
disk_callback_fabric(di_minor_t minor,di_node_t node)2667c478bd9Sstevel@tonic-gate disk_callback_fabric(di_minor_t minor, di_node_t node)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	char disk[DISK_SUBPATH_MAX];
2697c478bd9Sstevel@tonic-gate 	int lun;
2707c478bd9Sstevel@tonic-gate 	int count;
2717c478bd9Sstevel@tonic-gate 	int *intp;
2727c478bd9Sstevel@tonic-gate 	uchar_t *str;
2737c478bd9Sstevel@tonic-gate 	uchar_t *wwn;
2747c478bd9Sstevel@tonic-gate 	uchar_t ascii_wwn[ASCIIWWNSIZE];
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2777c478bd9Sstevel@tonic-gate 	    "client-guid", (char **)&wwn) > 0) {
278e37c6c37Scth 		if (strlcpy((char *)ascii_wwn, (char *)wwn,
279e37c6c37Scth 		    sizeof (ascii_wwn)) >= sizeof (ascii_wwn)) {
2807c478bd9Sstevel@tonic-gate 			devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
281e37c6c37Scth 			    strlen((char *)wwn));
2827c478bd9Sstevel@tonic-gate 			return (DEVFSADM_CONTINUE);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		lun = 0;
2857c478bd9Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
2867c478bd9Sstevel@tonic-gate 	    "port-wwn", &wwn) > 0) {
2877c478bd9Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2884c06356bSdh 		    SCSI_ADDR_PROP_LUN, &intp) > 0) {
2897c478bd9Sstevel@tonic-gate 			lun = *intp;
2907c478bd9Sstevel@tonic-gate 		} else {
2917c478bd9Sstevel@tonic-gate 			lun = 0;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
2957c478bd9Sstevel@tonic-gate 			(void) sprintf((caddr_t)str, "%02x", wwn[count]);
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 		*str = '\0';
2987c478bd9Sstevel@tonic-gate 	} else {
2997c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	for (str = ascii_wwn; *str != '\0'; str++) {
3037c478bd9Sstevel@tonic-gate 		*str = DISK_LINK_TO_UPPER(*str);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
313d88e498aSjiang wu - Sun Microsystems - Beijing China static int
disk_callback_sas(di_minor_t minor,di_node_t node)314d88e498aSjiang wu - Sun Microsystems - Beijing China disk_callback_sas(di_minor_t minor, di_node_t node)
315d88e498aSjiang wu - Sun Microsystems - Beijing China {
316d88e498aSjiang wu - Sun Microsystems - Beijing China 	char disk[DISK_SUBPATH_MAX];
3174c06356bSdh 	int lun64_found = 0;
3184c06356bSdh 	scsi_lun64_t lun64, sl;
3194c06356bSdh 	scsi_lun_t lun;
3204c06356bSdh 	int64_t *lun64p;
3214c06356bSdh 	uint64_t wwn;
322d88e498aSjiang wu - Sun Microsystems - Beijing China 	int *intp;
3234c06356bSdh 	char *tgt_port;
3244c06356bSdh 	uchar_t addr_method;
3254c06356bSdh 
3264c06356bSdh 	/* Get lun property */
3274c06356bSdh 	if (di_prop_lookup_int64(DDI_DEV_T_ANY, node,
3284c06356bSdh 	    SCSI_ADDR_PROP_LUN64, &lun64p) > 0) {
3294c06356bSdh 		if (*lun64p != SCSI_LUN64_ILLEGAL) {
3304c06356bSdh 			lun64_found = 1;
3314c06356bSdh 			lun64 = (uint64_t)*lun64p;
3324c06356bSdh 		}
3334c06356bSdh 	}
3344c06356bSdh 	if ((!lun64_found) && (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
3354c06356bSdh 	    SCSI_ADDR_PROP_LUN, &intp) > 0)) {
3364c06356bSdh 		lun64 = (uint64_t)*intp;
337d88e498aSjiang wu - Sun Microsystems - Beijing China 	}
3384c06356bSdh 
3394c06356bSdh 	lun = scsi_lun64_to_lun(lun64);
3404c06356bSdh 
3414c06356bSdh 	addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
3424c06356bSdh 
343d88e498aSjiang wu - Sun Microsystems - Beijing China 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
3444c06356bSdh 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
3454c06356bSdh 		(void) scsi_wwnstr_to_wwn(tgt_port, &wwn);
3464c06356bSdh 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
3474c06356bSdh 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
3484c06356bSdh 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
3494c06356bSdh 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
3504c06356bSdh 			(void) snprintf(disk, DISK_SUBPATH_MAX,
3514c06356bSdh 			    "t%"PRIX64"d%"PRId64, wwn, lun64);
3524c06356bSdh 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
3534c06356bSdh 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
3544c06356bSdh 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
3554c06356bSdh 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
3564c06356bSdh 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
3574c06356bSdh 			(void) snprintf(disk, DISK_SUBPATH_MAX,
3584c06356bSdh 			    "t%"PRIX64"d%"PRIX16, wwn, sl);
3594c06356bSdh 		} else {
3604c06356bSdh 			(void) snprintf(disk, DISK_SUBPATH_MAX,
3614c06356bSdh 			    "t%"PRIX64"d%"PRIX64, wwn, lun64);
362d88e498aSjiang wu - Sun Microsystems - Beijing China 		}
363d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
3644c06356bSdh 	    SCSI_ADDR_PROP_SATA_PHY, &intp) > 0) {
3654c06356bSdh 		/* Use phy format naming, for SATA devices without wwn */
3664c06356bSdh 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
3674c06356bSdh 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
3684c06356bSdh 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
3694c06356bSdh 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
3704c06356bSdh 			(void) snprintf(disk, DISK_SUBPATH_MAX,
37182bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRId64, *intp, lun64);
3724c06356bSdh 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
3734c06356bSdh 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
3744c06356bSdh 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
3754c06356bSdh 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
3764c06356bSdh 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
3774c06356bSdh 			(void) snprintf(disk, DISK_SUBPATH_MAX,
37882bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRIX16, *intp, sl);
3794c06356bSdh 		} else {
3804c06356bSdh 			(void) snprintf(disk, DISK_SUBPATH_MAX,
38182bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRIX64, *intp, lun64);
3824c06356bSdh 		}
383d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else {
384d88e498aSjiang wu - Sun Microsystems - Beijing China 		return (DEVFSADM_CONTINUE);
385d88e498aSjiang wu - Sun Microsystems - Beijing China 	}
386d88e498aSjiang wu - Sun Microsystems - Beijing China 
387d88e498aSjiang wu - Sun Microsystems - Beijing China 	disk_common(minor, node, disk, RM_STALE);
388d88e498aSjiang wu - Sun Microsystems - Beijing China 
389d88e498aSjiang wu - Sun Microsystems - Beijing China 	return (DEVFSADM_CONTINUE);
390d88e498aSjiang wu - Sun Microsystems - Beijing China }
391d88e498aSjiang wu - Sun Microsystems - Beijing China 
39297869ac5Sjhd /*
39397869ac5Sjhd  * xVM virtual block device
39497869ac5Sjhd  *
395c73799ddSYuri Pankov  * Xen passes device number in the following format:
39697869ac5Sjhd  *
397efc44fd7SVitaliy Gusev  *    1 << 28 | disk << 8 | partition      xvd, disks or partitions 16 onwards
398efc44fd7SVitaliy Gusev  *  202 <<  8 | disk << 4 | partition      xvd, disks and partitions up to 15
399efc44fd7SVitaliy Gusev  *    8 <<  8 | disk << 4 | partition      sd, disks and partitions up to 15
400efc44fd7SVitaliy Gusev  *    3 <<  8 | disk << 6 | partition      hd, disks 0..1, partitions 0..63
401efc44fd7SVitaliy Gusev  *   22 <<  8 | (disk-2) << 6 | partition  hd, disks 2..3, partitions 0..63
402efc44fd7SVitaliy Gusev  *    2 << 28 onwards                      reserved for future use
403efc44fd7SVitaliy Gusev  *   other values less than 1 << 28        deprecated / reserved
404efc44fd7SVitaliy Gusev  *
405c73799ddSYuri Pankov  * The corresponding /dev/dsk name can be:
406efc44fd7SVitaliy Gusev  *
407c73799ddSYuri Pankov  *          c0tYdX
408efc44fd7SVitaliy Gusev  *
409efc44fd7SVitaliy Gusev  * where Y,X >= 0.
41097869ac5Sjhd  *
41197869ac5Sjhd  * For PV guests using the legacy naming (0, 1, 2, ...)
412c73799ddSYuri Pankov  * the disk names created will be c0d[0..767].
413efc44fd7SVitaliy Gusev  */
414efc44fd7SVitaliy Gusev 
415efc44fd7SVitaliy Gusev #define	HD_BASE		(3 << 8)
416efc44fd7SVitaliy Gusev #define	XEN_EXT_SHIFT	(28)
417efc44fd7SVitaliy Gusev 
418efc44fd7SVitaliy Gusev /*
419efc44fd7SVitaliy Gusev  * Return: Number of parsed and written parameters
42097869ac5Sjhd  */
42197869ac5Sjhd static int
decode_xen_device(uint_t device,uint_t * disk,uint_t * plun)422efc44fd7SVitaliy Gusev decode_xen_device(uint_t device, uint_t *disk, uint_t *plun)
42397869ac5Sjhd {
424efc44fd7SVitaliy Gusev 	uint_t dsk, lun = 0;
425efc44fd7SVitaliy Gusev 	int ret = 1;
426efc44fd7SVitaliy Gusev 
427efc44fd7SVitaliy Gusev 	if ((device >> XEN_EXT_SHIFT) > 1)
428efc44fd7SVitaliy Gusev 		return (0);
42997869ac5Sjhd 
430efc44fd7SVitaliy Gusev 	if (device < HD_BASE) {
431efc44fd7SVitaliy Gusev 		/* legacy device address */
432efc44fd7SVitaliy Gusev 		dsk = device;
433efc44fd7SVitaliy Gusev 		goto end;
434efc44fd7SVitaliy Gusev 	}
435efc44fd7SVitaliy Gusev 
436efc44fd7SVitaliy Gusev 	ret = 2;
437efc44fd7SVitaliy Gusev 	if (device & (1 << XEN_EXT_SHIFT)) {
438efc44fd7SVitaliy Gusev 		/* extended */
439efc44fd7SVitaliy Gusev 		dsk = device & (~0xff);
440efc44fd7SVitaliy Gusev 		lun = device & 0xff;
441efc44fd7SVitaliy Gusev 		goto end;
442efc44fd7SVitaliy Gusev 	}
443efc44fd7SVitaliy Gusev 
444efc44fd7SVitaliy Gusev 	switch (device >> 8) {
445efc44fd7SVitaliy Gusev 	case 202:				/* xvd */
446efc44fd7SVitaliy Gusev 		dsk = (device >> 4) & 0xf;
447efc44fd7SVitaliy Gusev 		lun =  device & 0xf;
448efc44fd7SVitaliy Gusev 		break;
449efc44fd7SVitaliy Gusev 	case 8:					/* sd */
450efc44fd7SVitaliy Gusev 		dsk = device & (~0xf);
451efc44fd7SVitaliy Gusev 		lun = device & 0xf;
452efc44fd7SVitaliy Gusev 		break;
453efc44fd7SVitaliy Gusev 	case 3:					/* hd, disk 0..1 */
454c73799ddSYuri Pankov 		dsk = (device >> 6) & 0x1;
455efc44fd7SVitaliy Gusev 		lun = device & 0x3f;
456efc44fd7SVitaliy Gusev 		break;
457efc44fd7SVitaliy Gusev 	case 22:				/* hd, disk 2..3 */
458c73799ddSYuri Pankov 		dsk = ((device >> 6) & 0x1) + 2;
459efc44fd7SVitaliy Gusev 		lun = device & 0x3f;
460efc44fd7SVitaliy Gusev 		break;
461efc44fd7SVitaliy Gusev 	default:
462efc44fd7SVitaliy Gusev 		return (0);
463efc44fd7SVitaliy Gusev 	}
464efc44fd7SVitaliy Gusev end:
465efc44fd7SVitaliy Gusev 	*disk = dsk;
466efc44fd7SVitaliy Gusev 	*plun = lun;
467efc44fd7SVitaliy Gusev 	return (ret);
468efc44fd7SVitaliy Gusev }
469efc44fd7SVitaliy Gusev 
470efc44fd7SVitaliy Gusev static int
disk_callback_xvmd(di_minor_t minor,di_node_t node)471efc44fd7SVitaliy Gusev disk_callback_xvmd(di_minor_t minor, di_node_t node)
472efc44fd7SVitaliy Gusev {
47397869ac5Sjhd 	char *addr;
47497869ac5Sjhd 	char disk[16];
47597869ac5Sjhd 	uint_t targ;
476efc44fd7SVitaliy Gusev 	uint_t dsk, lun;
477efc44fd7SVitaliy Gusev 	int res;
47897869ac5Sjhd 
47997869ac5Sjhd 	addr = di_bus_addr(node);
48097869ac5Sjhd 	targ = strtol(addr, (char **)NULL, 10);
481efc44fd7SVitaliy Gusev 
482efc44fd7SVitaliy Gusev 	res = decode_xen_device(targ, &dsk, &lun);
483efc44fd7SVitaliy Gusev 
48497869ac5Sjhd 	/* HVM device names are generated using the standard generator */
485efc44fd7SVitaliy Gusev 
486efc44fd7SVitaliy Gusev 	if (res == 1)
487efc44fd7SVitaliy Gusev 		(void) snprintf(disk, sizeof (disk),  "d%d", dsk);
488efc44fd7SVitaliy Gusev 	else if (res == 2)
489efc44fd7SVitaliy Gusev 		(void) snprintf(disk, sizeof (disk), "t%dd%d", dsk, lun);
490efc44fd7SVitaliy Gusev 	else {
49197869ac5Sjhd 		devfsadm_errprint("%s: invalid disk device number (%s)\n",
49297869ac5Sjhd 		    modname, addr);
49397869ac5Sjhd 		return (DEVFSADM_CONTINUE);
49497869ac5Sjhd 	}
49597869ac5Sjhd 	disk_common(minor, node, disk, 0);
49697869ac5Sjhd 	return (DEVFSADM_CONTINUE);
49797869ac5Sjhd }
49897869ac5Sjhd 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * This function is called for every disk minor node.
5017c478bd9Sstevel@tonic-gate  * Calls enumerate to assign a logical controller number, and
5027c478bd9Sstevel@tonic-gate  * then devfsadm_mklink to make the link.
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate static void
disk_common(di_minor_t minor,di_node_t node,char * disk,int flags)5057c478bd9Sstevel@tonic-gate disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	char l_path[PATH_MAX + 1];
50818c2aff7Sartem 	char sec_path[PATH_MAX + 1];
5097c478bd9Sstevel@tonic-gate 	char stale_re[DISK_SUBPATH_MAX];
5107c478bd9Sstevel@tonic-gate 	char *dir;
5117c478bd9Sstevel@tonic-gate 	char slice[4];
5127c478bd9Sstevel@tonic-gate 	char *mn;
5137c478bd9Sstevel@tonic-gate 	char *ctrl;
51445916cd2Sjpk 	char *nt = NULL;
51518c2aff7Sartem 	int *int_prop;
51645916cd2Sjpk 	int  nflags = 0;
517aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
518aa1b14e7SSheshadri Vasudevan 	char mn_copy[4];
519aa1b14e7SSheshadri Vasudevan 	char *part;
520aa1b14e7SSheshadri Vasudevan 	int part_num;
521aa1b14e7SSheshadri Vasudevan #endif
522aa1b14e7SSheshadri Vasudevan 
523aa1b14e7SSheshadri Vasudevan 	mn = di_minor_name(minor);
524aa1b14e7SSheshadri Vasudevan 	if (strstr(mn, ",raw")) {
5257c478bd9Sstevel@tonic-gate 		dir = "rdsk";
526aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
527aa1b14e7SSheshadri Vasudevan 		(void) strncpy(mn_copy, mn, 4);
528aa1b14e7SSheshadri Vasudevan 		part = strtok(mn_copy, ",");
529aa1b14e7SSheshadri Vasudevan #endif
5307c478bd9Sstevel@tonic-gate 	} else {
5317c478bd9Sstevel@tonic-gate 		dir = "dsk";
532aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
533aa1b14e7SSheshadri Vasudevan 		part = mn;
534aa1b14e7SSheshadri Vasudevan #endif
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
537aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
538aa1b14e7SSheshadri Vasudevan 	/*
539aa1b14e7SSheshadri Vasudevan 	 * The following is a table describing the allocation of
540aa1b14e7SSheshadri Vasudevan 	 * minor numbers, minor names and /dev/dsk names for partitions
541aa1b14e7SSheshadri Vasudevan 	 * and slices on x86 systems.
542aa1b14e7SSheshadri Vasudevan 	 *
543aa1b14e7SSheshadri Vasudevan 	 *	Minor Number	Minor Name	/dev/dsk name
544aa1b14e7SSheshadri Vasudevan 	 *	---------------------------------------------
545aa1b14e7SSheshadri Vasudevan 	 *	0 to 15		"a" to "p"	s0 to s15
546aa1b14e7SSheshadri Vasudevan 	 *	16		"q"		p0
547aa1b14e7SSheshadri Vasudevan 	 *	17 to 20	"r" to "u"	p1 to p4
548aa1b14e7SSheshadri Vasudevan 	 *	21 to 52	"p5" to "p36"	p5 to p36
549aa1b14e7SSheshadri Vasudevan 	 *
550aa1b14e7SSheshadri Vasudevan 	 */
551aa1b14e7SSheshadri Vasudevan 	part_num = atoi(part + 1);
552aa1b14e7SSheshadri Vasudevan 
553aa1b14e7SSheshadri Vasudevan 	if ((mn[0] == 'p') && (part_num >= 5)) {
554aa1b14e7SSheshadri Vasudevan 		/* logical drive */
555aa1b14e7SSheshadri Vasudevan 		(void) snprintf(slice, 4, "%s", part);
556aa1b14e7SSheshadri Vasudevan 	} else {
557aa1b14e7SSheshadri Vasudevan #endif
558aa1b14e7SSheshadri Vasudevan 	if (mn[0] < 'q') {
5597c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, "s%d", mn[0] - 'a');
5607c478bd9Sstevel@tonic-gate 	} else if (strncmp(mn, MN_EFI, 2) != 0) {
5617c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, "p%d", mn[0] - 'q');
5627c478bd9Sstevel@tonic-gate 	} else {
5637c478bd9Sstevel@tonic-gate 		/* For EFI label */
5647c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, SLICE_EFI);
5657c478bd9Sstevel@tonic-gate 	}
566aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
567aa1b14e7SSheshadri Vasudevan 	}
568aa1b14e7SSheshadri Vasudevan #endif
5697c478bd9Sstevel@tonic-gate 
5708d483882Smlf 	nflags = 0;
5718d483882Smlf 	if (system_labeled) {
5728d483882Smlf 		nt = di_minor_nodetype(minor);
5738d483882Smlf 		if ((nt != NULL) &&
5748d483882Smlf 		    ((strcmp(nt, DDI_NT_CD) == 0) ||
5758d483882Smlf 		    (strcmp(nt, DDI_NT_CD_CHAN) == 0) ||
5768d483882Smlf 		    (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) {
5778d483882Smlf 			nflags = DA_ADD|DA_CD;
5788d483882Smlf 		}
5798d483882Smlf 	}
5808d483882Smlf 
5818d483882Smlf 	if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) {
5828d483882Smlf 		devfsadm_print(disk_mid, "Reserved link exists. Not "
5838d483882Smlf 		    "creating links for slice %s\n", slice);
5848d483882Smlf 		return;
5858d483882Smlf 	}
5868d483882Smlf 
5877c478bd9Sstevel@tonic-gate 	if (NULL == (ctrl = diskctrl(node, minor)))
5887c478bd9Sstevel@tonic-gate 		return;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	(void) strcpy(l_path, dir);
5917c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, "/c");
5927c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, ctrl);
5937c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, disk);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * If switching between SMI and EFI label or vice versa
5977c478bd9Sstevel@tonic-gate 	 * cleanup the previous label's devlinks.
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
6007c478bd9Sstevel@tonic-gate 		char *s, tpath[PATH_MAX + 1];
6017c478bd9Sstevel@tonic-gate 		struct stat sb;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		s = l_path + strlen(l_path);
6047c478bd9Sstevel@tonic-gate 		(void) strcat(l_path, (*mn == *(MN_SMI))
6057c478bd9Sstevel@tonic-gate 		    ? SLICE_EFI : SLICE_SMI);
6067c478bd9Sstevel@tonic-gate 		/*
6077c478bd9Sstevel@tonic-gate 		 * Attempt the remove only if the stale link exists
6087c478bd9Sstevel@tonic-gate 		 */
6097c478bd9Sstevel@tonic-gate 		(void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
6107c478bd9Sstevel@tonic-gate 		    devfsadm_root_path(), l_path);
6117c478bd9Sstevel@tonic-gate 		if (lstat(tpath, &sb) != -1)
6127c478bd9Sstevel@tonic-gate 			devfsadm_rm_all(l_path);
6137c478bd9Sstevel@tonic-gate 		*s = '\0';
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, slice);
6167c478bd9Sstevel@tonic-gate 
61745916cd2Sjpk 	(void) devfsadm_mklink(l_path, node, minor, nflags);
6187c478bd9Sstevel@tonic-gate 
61918c2aff7Sartem 	/* secondary links for removable and hotpluggable devices */
62018c2aff7Sartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
62118c2aff7Sartem 	    &int_prop) >= 0) {
62218c2aff7Sartem 		(void) strcpy(sec_path, "removable-media/");
62318c2aff7Sartem 		(void) strcat(sec_path, l_path);
62418c2aff7Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
6257544909dSartem 	}
6267544909dSartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
62718c2aff7Sartem 	    &int_prop) >= 0) {
62818c2aff7Sartem 		(void) strcpy(sec_path, "hotpluggable/");
62918c2aff7Sartem 		(void) strcat(sec_path, l_path);
630