xref: /titanic_41/usr/src/cmd/devfsadm/sgen_link.c (revision 4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6)
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
5*4c06356bSdh142964  * Common Development and Distribution License (the "License").
6*4c06356bSdh142964  * 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 /*
22*4c06356bSdh142964  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <devfsadm.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
32*4c06356bSdh142964 #include <sys/int_fmtio.h>
33*4c06356bSdh142964 #include <sys/scsi/scsi_address.h>
34*4c06356bSdh142964 #include <sys/libdevid.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define	SGEN_LINK_RE	"^scsi/.+/c[0-9]+t[0-9A-F]+d[0-9]+$"
377c478bd9Sstevel@tonic-gate #define	SGEN_DIR	"scsi"
387c478bd9Sstevel@tonic-gate #define	SGEN_CLASS	"generic-scsi"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static int sgen_callback(di_minor_t minor, di_node_t node);
417c478bd9Sstevel@tonic-gate static char *find_ctrlr(di_node_t node, di_minor_t minor);
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static devfsadm_create_t sgen_create_cbt[] = {
457c478bd9Sstevel@tonic-gate 	{ SGEN_CLASS, "ddi_generic:scsi", NULL,
467c478bd9Sstevel@tonic-gate 	    TYPE_EXACT | CREATE_DEFER, ILEVEL_0, sgen_callback
477c478bd9Sstevel@tonic-gate 	}
487c478bd9Sstevel@tonic-gate };
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(sgen_create_cbt);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * HOT auto cleanup of sgen links not desired.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate static devfsadm_remove_t sgen_remove_cbt[] = {
567c478bd9Sstevel@tonic-gate 	{ SGEN_CLASS, SGEN_LINK_RE, RM_POST,
577c478bd9Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(sgen_remove_cbt);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static int
sgen_callback(di_minor_t minor,di_node_t node)647c478bd9Sstevel@tonic-gate sgen_callback(di_minor_t minor, di_node_t node)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	char *baddr, *cnum, *tstr;
677c478bd9Sstevel@tonic-gate 	char lpath[PATH_MAX], buf[PATH_MAX];
68*4c06356bSdh142964 	uchar_t *wwnstr;
69*4c06356bSdh142964 	char *tgt_port;
70*4c06356bSdh142964 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if ((cnum = find_ctrlr(node, minor)) == NULL)
737c478bd9Sstevel@tonic-gate 		goto done;
747c478bd9Sstevel@tonic-gate 
75*4c06356bSdh142964 	/*
76*4c06356bSdh142964 	 * SCSAv3 attached devices.
77*4c06356bSdh142964 	 */
787c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
79*4c06356bSdh142964 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
80*4c06356bSdh142964 		uint64_t wwn;
81*4c06356bSdh142964 		scsi_lun64_t sl;
82*4c06356bSdh142964 		scsi_lun_t lun;
83*4c06356bSdh142964 		int64_t lun64;
84*4c06356bSdh142964 		int64_t *lun64p;
85*4c06356bSdh142964 		int *intp;
86*4c06356bSdh142964 		uchar_t addr_method;
87*4c06356bSdh142964 
88*4c06356bSdh142964 		/* Get lun property */
89*4c06356bSdh142964 		if ((di_prop_lookup_int64(DDI_DEV_T_ANY, node,
90*4c06356bSdh142964 		    SCSI_ADDR_PROP_LUN64, &lun64p) > 0) &&
91*4c06356bSdh142964 		    (*lun64p != SCSI_LUN64_ILLEGAL)) {
92*4c06356bSdh142964 			lun64 = *lun64p;
93*4c06356bSdh142964 		} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
94*4c06356bSdh142964 		    SCSI_ADDR_PROP_LUN, &intp) > 0) {
95*4c06356bSdh142964 			lun64 = (uint64_t)*intp;
96*4c06356bSdh142964 		}
97*4c06356bSdh142964 
98*4c06356bSdh142964 		lun = scsi_lun64_to_lun(lun64);
99*4c06356bSdh142964 
100*4c06356bSdh142964 		addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
101*4c06356bSdh142964 
102*4c06356bSdh142964 		(void) scsi_wwnstr_to_wwn(tgt_port, &wwn);
103*4c06356bSdh142964 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
104*4c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
105*4c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
106*4c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
107*4c06356bSdh142964 			(void) snprintf(lpath, PATH_MAX,
108*4c06356bSdh142964 			    "%s/%s/c%st%"PRIX64"d%"PRId64, SGEN_DIR,
109*4c06356bSdh142964 			    di_minor_name(minor), cnum, wwn, lun64);
110*4c06356bSdh142964 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
111*4c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
112*4c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
113*4c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
114*4c06356bSdh142964 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
115*4c06356bSdh142964 			(void) snprintf(lpath, PATH_MAX,
116*4c06356bSdh142964 			    "%s/%s/c%st%"PRIX64"d%"PRIX16, SGEN_DIR,
117*4c06356bSdh142964 			    di_minor_name(minor), cnum, wwn, sl);
118*4c06356bSdh142964 		} else {
119*4c06356bSdh142964 			(void) snprintf(lpath, PATH_MAX,
120*4c06356bSdh142964 			    "%s/%s/c%st%"PRIX64"d%"PRIX64, SGEN_DIR,
121*4c06356bSdh142964 			    di_minor_name(minor), cnum, wwn, lun64);
122*4c06356bSdh142964 		}
123*4c06356bSdh142964 	} else if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
124*4c06356bSdh142964 	    "client-guid", (char **)&wwnstr) > 0) {
1257c478bd9Sstevel@tonic-gate 		/*
1267c478bd9Sstevel@tonic-gate 		 * MPXIO-enabled devices; lun is always 0.
1277c478bd9Sstevel@tonic-gate 		 */
128*4c06356bSdh142964 		if (strlcpy((char *)buf, (char *)wwnstr, sizeof (buf)) >=
1297c478bd9Sstevel@tonic-gate 		    sizeof (buf))
1307c478bd9Sstevel@tonic-gate 			goto done;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		for (tstr = buf; *tstr != '\0'; tstr++) {
1337c478bd9Sstevel@tonic-gate 			*tstr = toupper(*tstr);
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%sd0", SGEN_DIR,
1367c478bd9Sstevel@tonic-gate 		    di_minor_name(minor), cnum, buf) >= sizeof (lpath))
1377c478bd9Sstevel@tonic-gate 			goto done;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
140*4c06356bSdh142964 	    "port-wwn", &wwnstr) == 8) {
1417c478bd9Sstevel@tonic-gate 		/*
1427c478bd9Sstevel@tonic-gate 		 * "normal" fibre channel devices
1437c478bd9Sstevel@tonic-gate 		 */
1447c478bd9Sstevel@tonic-gate 		int lun, *lunp, count;
1457c478bd9Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "lun", &lunp) > 0)
1467c478bd9Sstevel@tonic-gate 			lun = *lunp;
1477c478bd9Sstevel@tonic-gate 		else
1487c478bd9Sstevel@tonic-gate 			lun = 0;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		for (count = 0, tstr = buf; count < 8; count++, tstr += 2)
151*4c06356bSdh142964 			(void) sprintf(tstr, "%02X", wwnstr[count]);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		*tstr = '\0';
1547c478bd9Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%sd%d", SGEN_DIR,
1557c478bd9Sstevel@tonic-gate 		    di_minor_name(minor), cnum, buf, lun) >= sizeof (lpath))
1567c478bd9Sstevel@tonic-gate 			goto done;
1577c478bd9Sstevel@tonic-gate 	} else {
1587c478bd9Sstevel@tonic-gate 		/*
1597c478bd9Sstevel@tonic-gate 		 * Parallel SCSI devices
1607c478bd9Sstevel@tonic-gate 		 */
1617c478bd9Sstevel@tonic-gate 		uint_t targ, lun;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		if ((baddr = di_bus_addr(node)) == NULL)
1647c478bd9Sstevel@tonic-gate 			goto done;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		if (sscanf(baddr, "%X,%X", &targ, &lun) != 2)
1677c478bd9Sstevel@tonic-gate 			goto done;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%dd%d", SGEN_DIR,
1707c478bd9Sstevel@tonic-gate 		    di_minor_name(minor), cnum, targ, lun) >= sizeof (lpath))
1717c478bd9Sstevel@tonic-gate 			goto done;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	(void) devfsadm_mklink(lpath, node, minor, 0);
1757c478bd9Sstevel@tonic-gate done:
1767c478bd9Sstevel@tonic-gate 	free(cnum);
1777c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
1817c478bd9Sstevel@tonic-gate #define	RULE_INDEX	2
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static char *
find_ctrlr(di_node_t node,di_minor_t minor)1847c478bd9Sstevel@tonic-gate find_ctrlr(di_node_t node, di_minor_t minor)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
1877c478bd9Sstevel@tonic-gate 	char *devfspath;
1887c478bd9Sstevel@tonic-gate 	char *buf, *mn;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[3] = {
1917c478bd9Sstevel@tonic-gate 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
1927c478bd9Sstevel@tonic-gate 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
1937c478bd9Sstevel@tonic-gate 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
1947c478bd9Sstevel@tonic-gate 	};
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
1997c478bd9Sstevel@tonic-gate 		return (NULL);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
2027c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
2037c478bd9Sstevel@tonic-gate 	(void) strcat(path, mn);
2047c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * Use controller (parent) component of device path
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
2107c478bd9Sstevel@tonic-gate 	    DEVFSADM_MULTIPLE) {
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		/*
2137c478bd9Sstevel@tonic-gate 		 * We failed because there are multiple logical controller
2147c478bd9Sstevel@tonic-gate 		 * numbers for a single physical controller.  If we use node
2157c478bd9Sstevel@tonic-gate 		 * name also for DEVICE paths in the match it should fix this
2167c478bd9Sstevel@tonic-gate 		 * and only find one logical controller. (See 4045879).
2177c478bd9Sstevel@tonic-gate 		 * NOTE: Rules for controllers are not changed, as there is
2187c478bd9Sstevel@tonic-gate 		 * no unique controller number for them in this case.
2197c478bd9Sstevel@tonic-gate 		 *
2207c478bd9Sstevel@tonic-gate 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
2217c478bd9Sstevel@tonic-gate 		 * modules. NOT to be used by other modules.
2227c478bd9Sstevel@tonic-gate 		 */
2237c478bd9Sstevel@tonic-gate 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
2247c478bd9Sstevel@tonic-gate 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
2257c478bd9Sstevel@tonic-gate 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
2267c478bd9Sstevel@tonic-gate 			return (NULL);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	return (buf);
2317c478bd9Sstevel@tonic-gate }
232