10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <devfsadm.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <limits.h>
330Sstevel@tonic-gate #include <sys/stat.h>
341676Sjpk #include <bsm/devalloc.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #define	DISK_SUBPATH_MAX 100
370Sstevel@tonic-gate #define	RM_STALE 0x01
380Sstevel@tonic-gate #define	DISK_LINK_RE	"^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$"
390Sstevel@tonic-gate #define	DISK_LINK_TO_UPPER(ch)\
400Sstevel@tonic-gate 	(((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch)
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define	SLICE_SMI	"s7"
430Sstevel@tonic-gate #define	SLICE_EFI	""
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	MN_SMI		"h"
460Sstevel@tonic-gate #define	MN_EFI		"wd"
470Sstevel@tonic-gate #define	ASCIIWWNSIZE	255
480Sstevel@tonic-gate 
491676Sjpk extern int system_labeled;
501676Sjpk 
510Sstevel@tonic-gate static int disk_callback_chan(di_minor_t minor, di_node_t node);
520Sstevel@tonic-gate static int disk_callback_nchan(di_minor_t minor, di_node_t node);
530Sstevel@tonic-gate static int disk_callback_wwn(di_minor_t minor, di_node_t node);
540Sstevel@tonic-gate static int disk_callback_fabric(di_minor_t minor, di_node_t node);
550Sstevel@tonic-gate static void disk_common(di_minor_t minor, di_node_t node, char *disk,
560Sstevel@tonic-gate 				int flags);
570Sstevel@tonic-gate static char *diskctrl(di_node_t node, di_minor_t minor);
580Sstevel@tonic-gate extern void rm_link_from_cache(char *devlink, char *physpath);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static devfsadm_create_t disk_cbt[] = {
620Sstevel@tonic-gate 	{ "disk", "ddi_block", NULL,
630Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
640Sstevel@tonic-gate 	},
650Sstevel@tonic-gate 	{ "disk", "ddi_block:channel", NULL,
660Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
670Sstevel@tonic-gate 	},
680Sstevel@tonic-gate 	{ "disk", "ddi_block:fabric", NULL,
690Sstevel@tonic-gate 		TYPE_EXACT, ILEVEL_0, disk_callback_fabric
700Sstevel@tonic-gate 	},
710Sstevel@tonic-gate 	{ "disk", "ddi_block:wwn", NULL,
720Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_wwn
730Sstevel@tonic-gate 	},
740Sstevel@tonic-gate 	{ "disk", "ddi_block:cdrom", NULL,
750Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
760Sstevel@tonic-gate 	},
770Sstevel@tonic-gate 	{ "disk", "ddi_block:cdrom:channel", NULL,
780Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
790Sstevel@tonic-gate 	},
800Sstevel@tonic-gate };
810Sstevel@tonic-gate 
820Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(disk_cbt);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * HOT auto cleanup of disks not desired.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate static devfsadm_remove_t disk_remove_cbt[] = {
880Sstevel@tonic-gate 	{ "disk", DISK_LINK_RE, RM_POST,
890Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static int
960Sstevel@tonic-gate disk_callback_chan(di_minor_t minor, di_node_t node)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	char *addr;
990Sstevel@tonic-gate 	char disk[20];
1000Sstevel@tonic-gate 	uint_t targ;
1010Sstevel@tonic-gate 	uint_t lun;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	addr = di_bus_addr(node);
1040Sstevel@tonic-gate 	(void) sscanf(addr, "%X,%X", &targ, &lun);
1050Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1060Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1070Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static int
1120Sstevel@tonic-gate disk_callback_nchan(di_minor_t minor, di_node_t node)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	char *addr;
1150Sstevel@tonic-gate 	char disk[10];
1160Sstevel@tonic-gate 	uint_t lun;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	addr = di_bus_addr(node);
1190Sstevel@tonic-gate 	(void) sscanf(addr, "%X", &lun);
1200Sstevel@tonic-gate 	(void) sprintf(disk, "d%d", lun);
1210Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1220Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate static int
1270Sstevel@tonic-gate disk_callback_wwn(di_minor_t minor, di_node_t node)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	char disk[10];
1300Sstevel@tonic-gate 	int lun;
1310Sstevel@tonic-gate 	int targ;
1320Sstevel@tonic-gate 	int *intp;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
1350Sstevel@tonic-gate 		"target", &intp) <= 0) {
1360Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	targ = *intp;
1390Sstevel@tonic-gate 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
1400Sstevel@tonic-gate 		    "lun", &intp) <= 0) {
1410Sstevel@tonic-gate 		    lun = 0;
1420Sstevel@tonic-gate 	} else {
1430Sstevel@tonic-gate 		    lun = *intp;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate static int
1530Sstevel@tonic-gate disk_callback_fabric(di_minor_t minor, di_node_t node)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	char disk[DISK_SUBPATH_MAX];
1560Sstevel@tonic-gate 	int lun;
1570Sstevel@tonic-gate 	int count;
1580Sstevel@tonic-gate 	int *intp;
1590Sstevel@tonic-gate 	uchar_t *str;
1600Sstevel@tonic-gate 	uchar_t *wwn;
1610Sstevel@tonic-gate 	uchar_t ascii_wwn[ASCIIWWNSIZE];
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
1640Sstevel@tonic-gate 	    "client-guid", (char **)&wwn) > 0) {
1650Sstevel@tonic-gate 		if (strlcpy((char *)ascii_wwn, (char *)wwn, sizeof (ascii_wwn))
1660Sstevel@tonic-gate 			>= sizeof (ascii_wwn)) {
1670Sstevel@tonic-gate 			devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
1680Sstevel@tonic-gate 				strlen((char *)wwn));
1690Sstevel@tonic-gate 			return (DEVFSADM_CONTINUE);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 		lun = 0;
1720Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
1730Sstevel@tonic-gate 	    "port-wwn", &wwn) > 0) {
1740Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
1750Sstevel@tonic-gate 		    "lun", &intp) > 0) {
1760Sstevel@tonic-gate 			lun = *intp;
1770Sstevel@tonic-gate 		} else {
1780Sstevel@tonic-gate 			lun = 0;
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
1820Sstevel@tonic-gate 			(void) sprintf((caddr_t)str, "%02x", wwn[count]);
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 		*str = '\0';
1850Sstevel@tonic-gate 	} else {
1860Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	for (str = ascii_wwn; *str != '\0'; str++) {
1900Sstevel@tonic-gate 		*str = DISK_LINK_TO_UPPER(*str);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate  * This function is called for every disk minor node.
2020Sstevel@tonic-gate  * Calls enumerate to assign a logical controller number, and
2030Sstevel@tonic-gate  * then devfsadm_mklink to make the link.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static void
2060Sstevel@tonic-gate disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	char l_path[PATH_MAX + 1];
209*2912Sartem 	char sec_path[PATH_MAX + 1];
2100Sstevel@tonic-gate 	char stale_re[DISK_SUBPATH_MAX];
2110Sstevel@tonic-gate 	char *dir;
2120Sstevel@tonic-gate 	char slice[4];
2130Sstevel@tonic-gate 	char *mn;
2140Sstevel@tonic-gate 	char *ctrl;
2151676Sjpk 	char *nt = NULL;
216*2912Sartem 	int *int_prop;
2171676Sjpk 	int  nflags = 0;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (strstr(mn = di_minor_name(minor), ",raw")) {
2200Sstevel@tonic-gate 		dir = "rdsk";
2210Sstevel@tonic-gate 	} else {
2220Sstevel@tonic-gate 		dir = "dsk";
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (mn[0] < 113) {
2260Sstevel@tonic-gate 		(void) sprintf(slice, "s%d", mn[0] - 'a');
2270Sstevel@tonic-gate 	} else if (strncmp(mn, MN_EFI, 2) != 0) {
2280Sstevel@tonic-gate 		(void) sprintf(slice, "p%d", mn[0] - 'q');
2290Sstevel@tonic-gate 	} else {
2300Sstevel@tonic-gate 		/* For EFI label */
2310Sstevel@tonic-gate 		(void) sprintf(slice, SLICE_EFI);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (NULL == (ctrl = diskctrl(node, minor)))
2350Sstevel@tonic-gate 		return;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	(void) strcpy(l_path, dir);
2380Sstevel@tonic-gate 	(void) strcat(l_path, "/c");
2390Sstevel@tonic-gate 	(void) strcat(l_path, ctrl);
2400Sstevel@tonic-gate 	(void) strcat(l_path, disk);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * If switching between SMI and EFI label or vice versa
2440Sstevel@tonic-gate 	 * cleanup the previous label's devlinks.
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
2470Sstevel@tonic-gate 		char *s, tpath[PATH_MAX + 1];
2480Sstevel@tonic-gate 		struct stat sb;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		s = l_path + strlen(l_path);
2510Sstevel@tonic-gate 		(void) strcat(l_path, (*mn == *(MN_SMI))
2520Sstevel@tonic-gate 		    ? SLICE_EFI : SLICE_SMI);
2530Sstevel@tonic-gate 		/*
2540Sstevel@tonic-gate 		 * Attempt the remove only if the stale link exists
2550Sstevel@tonic-gate 		 */
2560Sstevel@tonic-gate 		(void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
2570Sstevel@tonic-gate 		    devfsadm_root_path(), l_path);
2580Sstevel@tonic-gate 		if (lstat(tpath, &sb) != -1)
2590Sstevel@tonic-gate 			devfsadm_rm_all(l_path);
2600Sstevel@tonic-gate 		*s = '\0';
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	(void) strcat(l_path, slice);
2630Sstevel@tonic-gate 
2641676Sjpk 	if (system_labeled) {
2651676Sjpk 		nt = di_minor_nodetype(minor);
2661676Sjpk 		if ((nt != NULL) &&
2671676Sjpk 		    ((strcmp(nt, DDI_NT_CD) == 0) ||
2681676Sjpk 		    (strcmp(nt, DDI_NT_CD_CHAN) == 0) ||
2691676Sjpk 		    (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) {
2701676Sjpk 			nflags = DA_ADD|DA_CD;
2711676Sjpk 		}
2721676Sjpk 	}
2731676Sjpk 
2741676Sjpk 	(void) devfsadm_mklink(l_path, node, minor, nflags);
2750Sstevel@tonic-gate 
276*2912Sartem 	/* secondary links for removable and hotpluggable devices */
277*2912Sartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
278*2912Sartem 	    &int_prop) >= 0) {
279*2912Sartem 		(void) strcpy(sec_path, "removable-media/");
280*2912Sartem 		(void) strcat(sec_path, l_path);
281*2912Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
282*2912Sartem 	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
283*2912Sartem 	    &int_prop) >= 0) {
284*2912Sartem 		(void) strcpy(sec_path, "hotpluggable/");
285*2912Sartem 		(void) strcat(sec_path, l_path);
286*2912Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
287*2912Sartem 	}
288*2912Sartem 
2890Sstevel@tonic-gate 	if ((flags & RM_STALE) == RM_STALE) {
2900Sstevel@tonic-gate 		(void) strcpy(stale_re, "^");
2910Sstevel@tonic-gate 		(void) strcat(stale_re, dir);
2920Sstevel@tonic-gate 		(void) strcat(stale_re, "/c");
2930Sstevel@tonic-gate 		(void) strcat(stale_re, ctrl);
2940Sstevel@tonic-gate 		(void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$");
2950Sstevel@tonic-gate 		/*
2960Sstevel@tonic-gate 		 * optimizations are made inside of devfsadm_rm_stale_links
2970Sstevel@tonic-gate 		 * instead of before calling the function, as it always
2980Sstevel@tonic-gate 		 * needs to add the valid link to the cache.
2990Sstevel@tonic-gate 		 */
3000Sstevel@tonic-gate 		devfsadm_rm_stale_links(stale_re, l_path, node, minor);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	free(ctrl);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
3080Sstevel@tonic-gate #define	RULE_INDEX	0
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static char *
3110Sstevel@tonic-gate diskctrl(di_node_t node, di_minor_t minor)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	char path[PATH_MAX + 1];
3140Sstevel@tonic-gate 	char *devfspath;
3150Sstevel@tonic-gate 	char *buf, *mn;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	devfsadm_enumerate_t rules[3] = {
3180Sstevel@tonic-gate 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
3190Sstevel@tonic-gate 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
3200Sstevel@tonic-gate 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
3210Sstevel@tonic-gate 	};
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	mn = di_minor_name(minor);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
3260Sstevel@tonic-gate 		return (NULL);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
3290Sstevel@tonic-gate 	(void) strcat(path, ":");
3300Sstevel@tonic-gate 	(void) strcat(path, mn);
3310Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * Use controller component of disk path
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
3370Sstevel@tonic-gate 	    DEVFSADM_MULTIPLE) {
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 		/*
3400Sstevel@tonic-gate 		 * We failed because there are multiple logical controller
3410Sstevel@tonic-gate 		 * numbers for a single physical controller.  If we use node
3420Sstevel@tonic-gate 		 * name also in the match it should fix this and only find one
3430Sstevel@tonic-gate 		 * logical controller. (See 4045879).
3440Sstevel@tonic-gate 		 * NOTE: Rules for controllers are not changed, as there is
3450Sstevel@tonic-gate 		 * no unique controller number for them in this case.
3460Sstevel@tonic-gate 		 *
3470Sstevel@tonic-gate 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
3480Sstevel@tonic-gate 		 * modules. NOT to be used by other modules.
3490Sstevel@tonic-gate 		 */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
3520Sstevel@tonic-gate 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
3530Sstevel@tonic-gate 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
3540Sstevel@tonic-gate 			return (NULL);
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	return (buf);
3590Sstevel@tonic-gate }
360