xref: /onnv-gate/usr/src/cmd/devfsadm/sgen_link.c (revision 10696:cd0f390dd9e2)
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
5*10696SDavid.Hollister@Sun.COM  * Common Development and Distribution License (the "License").
6*10696SDavid.Hollister@Sun.COM  * 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 /*
22*10696SDavid.Hollister@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <devfsadm.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <limits.h>
310Sstevel@tonic-gate #include <ctype.h>
32*10696SDavid.Hollister@Sun.COM #include <sys/int_fmtio.h>
33*10696SDavid.Hollister@Sun.COM #include <sys/scsi/scsi_address.h>
34*10696SDavid.Hollister@Sun.COM #include <sys/libdevid.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #define	SGEN_LINK_RE	"^scsi/.+/c[0-9]+t[0-9A-F]+d[0-9]+$"
370Sstevel@tonic-gate #define	SGEN_DIR	"scsi"
380Sstevel@tonic-gate #define	SGEN_CLASS	"generic-scsi"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static int sgen_callback(di_minor_t minor, di_node_t node);
410Sstevel@tonic-gate static char *find_ctrlr(di_node_t node, di_minor_t minor);
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static devfsadm_create_t sgen_create_cbt[] = {
450Sstevel@tonic-gate 	{ SGEN_CLASS, "ddi_generic:scsi", NULL,
460Sstevel@tonic-gate 	    TYPE_EXACT | CREATE_DEFER, ILEVEL_0, sgen_callback
470Sstevel@tonic-gate 	}
480Sstevel@tonic-gate };
490Sstevel@tonic-gate 
500Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(sgen_create_cbt);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * HOT auto cleanup of sgen links not desired.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate static devfsadm_remove_t sgen_remove_cbt[] = {
560Sstevel@tonic-gate 	{ SGEN_CLASS, SGEN_LINK_RE, RM_POST,
570Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate };
600Sstevel@tonic-gate 
610Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(sgen_remove_cbt);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static int
sgen_callback(di_minor_t minor,di_node_t node)640Sstevel@tonic-gate sgen_callback(di_minor_t minor, di_node_t node)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	char *baddr, *cnum, *tstr;
670Sstevel@tonic-gate 	char lpath[PATH_MAX], buf[PATH_MAX];
68*10696SDavid.Hollister@Sun.COM 	uchar_t *wwnstr;
69*10696SDavid.Hollister@Sun.COM 	char *tgt_port;
70*10696SDavid.Hollister@Sun.COM 
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if ((cnum = find_ctrlr(node, minor)) == NULL)
730Sstevel@tonic-gate 		goto done;
740Sstevel@tonic-gate 
75*10696SDavid.Hollister@Sun.COM 	/*
76*10696SDavid.Hollister@Sun.COM 	 * SCSAv3 attached devices.
77*10696SDavid.Hollister@Sun.COM 	 */
780Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
79*10696SDavid.Hollister@Sun.COM 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
80*10696SDavid.Hollister@Sun.COM 		uint64_t wwn;
81*10696SDavid.Hollister@Sun.COM 		scsi_lun64_t sl;
82*10696SDavid.Hollister@Sun.COM 		scsi_lun_t lun;
83*10696SDavid.Hollister@Sun.COM 		int64_t lun64;
84*10696SDavid.Hollister@Sun.COM 		int64_t *lun64p;
85*10696SDavid.Hollister@Sun.COM 		int *intp;
86*10696SDavid.Hollister@Sun.COM 		uchar_t addr_method;
87*10696SDavid.Hollister@Sun.COM 
88*10696SDavid.Hollister@Sun.COM 		/* Get lun property */
89*10696SDavid.Hollister@Sun.COM 		if ((di_prop_lookup_int64(DDI_DEV_T_ANY, node,
90*10696SDavid.Hollister@Sun.COM 		    SCSI_ADDR_PROP_LUN64, &lun64p) > 0) &&
91*10696SDavid.Hollister@Sun.COM 		    (*lun64p != SCSI_LUN64_ILLEGAL)) {
92*10696SDavid.Hollister@Sun.COM 			lun64 = *lun64p;
93*10696SDavid.Hollister@Sun.COM 		} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
94*10696SDavid.Hollister@Sun.COM 		    SCSI_ADDR_PROP_LUN, &intp) > 0) {
95*10696SDavid.Hollister@Sun.COM 			lun64 = (uint64_t)*intp;
96*10696SDavid.Hollister@Sun.COM 		}
97*10696SDavid.Hollister@Sun.COM 
98*10696SDavid.Hollister@Sun.COM 		lun = scsi_lun64_to_lun(lun64);
99*10696SDavid.Hollister@Sun.COM 
100*10696SDavid.Hollister@Sun.COM 		addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
101*10696SDavid.Hollister@Sun.COM 
102*10696SDavid.Hollister@Sun.COM 		(void) scsi_wwnstr_to_wwn(tgt_port, &wwn);
103*10696SDavid.Hollister@Sun.COM 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
104*10696SDavid.Hollister@Sun.COM 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
105*10696SDavid.Hollister@Sun.COM 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
106*10696SDavid.Hollister@Sun.COM 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
107*10696SDavid.Hollister@Sun.COM 			(void) snprintf(lpath, PATH_MAX,
108*10696SDavid.Hollister@Sun.COM 			    "%s/%s/c%st%"PRIX64"d%"PRId64, SGEN_DIR,
109*10696SDavid.Hollister@Sun.COM 			    di_minor_name(minor), cnum, wwn, lun64);
110*10696SDavid.Hollister@Sun.COM 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
111*10696SDavid.Hollister@Sun.COM 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
112*10696SDavid.Hollister@Sun.COM 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
113*10696SDavid.Hollister@Sun.COM 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
114*10696SDavid.Hollister@Sun.COM 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
115*10696SDavid.Hollister@Sun.COM 			(void) snprintf(lpath, PATH_MAX,
116*10696SDavid.Hollister@Sun.COM 			    "%s/%s/c%st%"PRIX64"d%"PRIX16, SGEN_DIR,
117*10696SDavid.Hollister@Sun.COM 			    di_minor_name(minor), cnum, wwn, sl);
118*10696SDavid.Hollister@Sun.COM 		} else {
119*10696SDavid.Hollister@Sun.COM 			(void) snprintf(lpath, PATH_MAX,
120*10696SDavid.Hollister@Sun.COM 			    "%s/%s/c%st%"PRIX64"d%"PRIX64, SGEN_DIR,
121*10696SDavid.Hollister@Sun.COM 			    di_minor_name(minor), cnum, wwn, lun64);
122*10696SDavid.Hollister@Sun.COM 		}
123*10696SDavid.Hollister@Sun.COM 	} else if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
124*10696SDavid.Hollister@Sun.COM 	    "client-guid", (char **)&wwnstr) > 0) {
1250Sstevel@tonic-gate 		/*
1260Sstevel@tonic-gate 		 * MPXIO-enabled devices; lun is always 0.
1270Sstevel@tonic-gate 		 */
128*10696SDavid.Hollister@Sun.COM 		if (strlcpy((char *)buf, (char *)wwnstr, sizeof (buf)) >=
1290Sstevel@tonic-gate 		    sizeof (buf))
1300Sstevel@tonic-gate 			goto done;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		for (tstr = buf; *tstr != '\0'; tstr++) {
1330Sstevel@tonic-gate 			*tstr = toupper(*tstr);
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%sd0", SGEN_DIR,
1360Sstevel@tonic-gate 		    di_minor_name(minor), cnum, buf) >= sizeof (lpath))
1370Sstevel@tonic-gate 			goto done;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
140*10696SDavid.Hollister@Sun.COM 	    "port-wwn", &wwnstr) == 8) {
1410Sstevel@tonic-gate 		/*
1420Sstevel@tonic-gate 		 * "normal" fibre channel devices
1430Sstevel@tonic-gate 		 */
1440Sstevel@tonic-gate 		int lun, *lunp, count;
1450Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "lun", &lunp) > 0)
1460Sstevel@tonic-gate 			lun = *lunp;
1470Sstevel@tonic-gate 		else
1480Sstevel@tonic-gate 			lun = 0;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 		for (count = 0, tstr = buf; count < 8; count++, tstr += 2)
151*10696SDavid.Hollister@Sun.COM 			(void) sprintf(tstr, "%02X", wwnstr[count]);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 		*tstr = '\0';
1540Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%sd%d", SGEN_DIR,
1550Sstevel@tonic-gate 		    di_minor_name(minor), cnum, buf, lun) >= sizeof (lpath))
1560Sstevel@tonic-gate 			goto done;
1570Sstevel@tonic-gate 	} else {
1580Sstevel@tonic-gate 		/*
1590Sstevel@tonic-gate 		 * Parallel SCSI devices
1600Sstevel@tonic-gate 		 */
1610Sstevel@tonic-gate 		uint_t targ, lun;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		if ((baddr = di_bus_addr(node)) == NULL)
1640Sstevel@tonic-gate 			goto done;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 		if (sscanf(baddr, "%X,%X", &targ, &lun) != 2)
1670Sstevel@tonic-gate 			goto done;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%dd%d", SGEN_DIR,
1700Sstevel@tonic-gate 		    di_minor_name(minor), cnum, targ, lun) >= sizeof (lpath))
1710Sstevel@tonic-gate 			goto done;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	(void) devfsadm_mklink(lpath, node, minor, 0);
1750Sstevel@tonic-gate done:
1760Sstevel@tonic-gate 	free(cnum);
1770Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
1810Sstevel@tonic-gate #define	RULE_INDEX	2
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static char *
find_ctrlr(di_node_t node,di_minor_t minor)1840Sstevel@tonic-gate find_ctrlr(di_node_t node, di_minor_t minor)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	char path[PATH_MAX + 1];
1870Sstevel@tonic-gate 	char *devfspath;
1880Sstevel@tonic-gate 	char *buf, *mn;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	devfsadm_enumerate_t rules[3] = {
1910Sstevel@tonic-gate 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
1920Sstevel@tonic-gate 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
1930Sstevel@tonic-gate 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
1940Sstevel@tonic-gate 	};
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	mn = di_minor_name(minor);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
1990Sstevel@tonic-gate 		return (NULL);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
2020Sstevel@tonic-gate 	(void) strcat(path, ":");
2030Sstevel@tonic-gate 	(void) strcat(path, mn);
2040Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/*
2070Sstevel@tonic-gate 	 * Use controller (parent) component of device path
2080Sstevel@tonic-gate 	 */
2090Sstevel@tonic-gate 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
2100Sstevel@tonic-gate 	    DEVFSADM_MULTIPLE) {
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		/*
2130Sstevel@tonic-gate 		 * We failed because there are multiple logical controller
2140Sstevel@tonic-gate 		 * numbers for a single physical controller.  If we use node
2150Sstevel@tonic-gate 		 * name also for DEVICE paths in the match it should fix this
2160Sstevel@tonic-gate 		 * and only find one logical controller. (See 4045879).
2170Sstevel@tonic-gate 		 * NOTE: Rules for controllers are not changed, as there is
2180Sstevel@tonic-gate 		 * no unique controller number for them in this case.
2190Sstevel@tonic-gate 		 *
2200Sstevel@tonic-gate 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
2210Sstevel@tonic-gate 		 * modules. NOT to be used by other modules.
2220Sstevel@tonic-gate 		 */
2230Sstevel@tonic-gate 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
2240Sstevel@tonic-gate 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
2250Sstevel@tonic-gate 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
2260Sstevel@tonic-gate 			return (NULL);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	return (buf);
2310Sstevel@tonic-gate }
232