xref: /titanic_50/usr/src/cmd/biosdev/biosdev.c (revision d155f3fe5b86a343798922aecec4a88ccb9c569b)
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*d155f3feSshidokht  * Common Development and Distribution License (the "License").
6*d155f3feSshidokht  * 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 /*
2222d01144Sshidokht  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <dirent.h>
367c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/pci.h>
417c478bd9Sstevel@tonic-gate #include <sys/biosdisk.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * structure used for searching device tree for a node matching
467c478bd9Sstevel@tonic-gate  * pci bus/dev/fn
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate typedef struct pcibdf {
497c478bd9Sstevel@tonic-gate 	int busnum;
507c478bd9Sstevel@tonic-gate 	int devnum;
517c478bd9Sstevel@tonic-gate 	int funcnum;
527c478bd9Sstevel@tonic-gate 	di_node_t	di_node;
537c478bd9Sstevel@tonic-gate } pcibdf_t;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * structure used for searching device tree for a node matching
577c478bd9Sstevel@tonic-gate  * USB serial number.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate typedef struct {
607c478bd9Sstevel@tonic-gate 	uint64_t serialno;
617c478bd9Sstevel@tonic-gate 	di_node_t	node;
627c478bd9Sstevel@tonic-gate } usbser_t;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * structure for holding the mapping info
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate typedef struct {
687c478bd9Sstevel@tonic-gate 	int disklist_index;	/* index to disk_list of the mapped path */
697c478bd9Sstevel@tonic-gate 	int matchcount;		/* number of matches per this device number */
707c478bd9Sstevel@tonic-gate } mapinfo_t;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	DEVFS_PREFIX "/devices"
737c478bd9Sstevel@tonic-gate #define	DISKS_LIST_INCR		20	/* increment for resizing disk_list */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	BIOSPROPNAME_TMPL	"biosdev-0x%x"
767c478bd9Sstevel@tonic-gate #define	BIOSPROPNAME_TMPL_LEN	13
777c478bd9Sstevel@tonic-gate #define	BIOSDEV_NUM		8
787c478bd9Sstevel@tonic-gate #define	STARTING_DRVNUM		0x80
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * array to hold mappings. Element at index X corresponds to BIOS device
827c478bd9Sstevel@tonic-gate  * number 0x80 + X
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate static mapinfo_t mapinfo[BIOSDEV_NUM];
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Cache copy of kernel device tree snapshot root handle, includes devices
887c478bd9Sstevel@tonic-gate  * that are detached
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate static di_node_t root_node = DI_NODE_NIL;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * kernel device tree snapshot with currently attached devices. Detached
947c478bd9Sstevel@tonic-gate  * devices are not included.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate static di_node_t root_allnode = DI_NODE_NIL;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * handle to retrieve prom properties
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static di_prom_handle_t prom_hdl = DI_PROM_HANDLE_NIL;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static char **disk_list = NULL;	/* array of physical device pathnames */
1057c478bd9Sstevel@tonic-gate static int disk_list_len = 0;		/* length of disk_list */
1067c478bd9Sstevel@tonic-gate static int disk_list_valid = 0;	/* number of valid entries in disk_list */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static int debug = 0;			/* used for enabling debug output */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* Local function prototypes */
1127c478bd9Sstevel@tonic-gate static void new_disk_list_entry(di_node_t node);
113*d155f3feSshidokht static int i_disktype(di_node_t node, di_minor_t minor, void *arg);
1147c478bd9Sstevel@tonic-gate static void build_disk_list();
115*d155f3feSshidokht static int search_disklist_match_path(char *path);
1167c478bd9Sstevel@tonic-gate static void free_disks();
117*d155f3feSshidokht static void cleanup_and_exit(int);
118*d155f3feSshidokht 
1197c478bd9Sstevel@tonic-gate static int match_edd(biosdev_data_t *bd);
1207c478bd9Sstevel@tonic-gate static int match_first_block(biosdev_data_t *bd);
121*d155f3feSshidokht 
122*d155f3feSshidokht static di_node_t search_tree_match_pcibdf(di_node_t node, int bus, int dev,
123*d155f3feSshidokht     int fn);
124*d155f3feSshidokht static int i_match_pcibdf(di_node_t node, void *arg);
125*d155f3feSshidokht 
126*d155f3feSshidokht static di_node_t search_tree_match_usbserialno(di_node_t node,
127*d155f3feSshidokht     uint64_t serialno);
128*d155f3feSshidokht static int i_match_usbserialno(di_node_t node, void *arg);
129*d155f3feSshidokht 
130*d155f3feSshidokht static di_node_t search_children_match_busaddr(di_node_t node,
131*d155f3feSshidokht     char *matchbusaddr);
132*d155f3feSshidokht 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static void
1367c478bd9Sstevel@tonic-gate new_disk_list_entry(di_node_t node)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	size_t	newsize;
1397c478bd9Sstevel@tonic-gate 	char **newlist;
1407c478bd9Sstevel@tonic-gate 	int newlen;
1417c478bd9Sstevel@tonic-gate 	char *devfspath;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (disk_list_valid >= disk_list_len)	{
1447c478bd9Sstevel@tonic-gate 		/* valid should never really be larger than len */
1457c478bd9Sstevel@tonic-gate 		/* if they are equal we need to init or realloc */
1467c478bd9Sstevel@tonic-gate 		newlen = disk_list_len + DISKS_LIST_INCR;
1477c478bd9Sstevel@tonic-gate 		newsize = newlen * sizeof (*disk_list);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		newlist = (char **)realloc(disk_list, newsize);
1507c478bd9Sstevel@tonic-gate 		if (newlist == NULL) {
1517c478bd9Sstevel@tonic-gate 			(void) printf("realloc failed to resize disk table\n");
1527c478bd9Sstevel@tonic-gate 			cleanup_and_exit(1);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		disk_list = newlist;
1557c478bd9Sstevel@tonic-gate 		disk_list_len = newlen;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	devfspath = di_devfs_path(node);
1597c478bd9Sstevel@tonic-gate 	disk_list[disk_list_valid] = devfspath;
1607c478bd9Sstevel@tonic-gate 	if (debug)
1617c478bd9Sstevel@tonic-gate 		(void) printf("adding %s\n", devfspath);
1627c478bd9Sstevel@tonic-gate 	disk_list_valid++;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /* ARGSUSED */
1667c478bd9Sstevel@tonic-gate static int
167*d155f3feSshidokht i_disktype(di_node_t node, di_minor_t minor, void *arg)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	char *minortype;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (di_minor_spectype(minor) == S_IFCHR) {
1727c478bd9Sstevel@tonic-gate 		minortype = di_minor_nodetype(minor);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		/* exclude CD's */
1757c478bd9Sstevel@tonic-gate 		if (strncmp(minortype, DDI_NT_CD, sizeof (DDI_NT_CD) - 1) != 0)
1767c478bd9Sstevel@tonic-gate 			/* only take p0 raw device */
1777c478bd9Sstevel@tonic-gate 			if (strcmp(di_minor_name(minor), "q,raw") == 0)
1787c478bd9Sstevel@tonic-gate 				new_disk_list_entry(node);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static void
1847c478bd9Sstevel@tonic-gate build_disk_list()
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	int ret;
1877c478bd9Sstevel@tonic-gate 	ret = di_walk_minor(root_node, DDI_NT_BLOCK, 0, NULL,
188*d155f3feSshidokht 	    i_disktype);
1897c478bd9Sstevel@tonic-gate 	if (ret != 0) {
1907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "di_walk_minor failed errno %d\n",
1917c478bd9Sstevel@tonic-gate 		    errno);
1927c478bd9Sstevel@tonic-gate 		cleanup_and_exit(1);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static void
1977c478bd9Sstevel@tonic-gate free_disks()
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	int i;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (disk_list) {
2027c478bd9Sstevel@tonic-gate 		for (i = 0; i < disk_list_valid; i++)
2037c478bd9Sstevel@tonic-gate 			di_devfs_path_free(disk_list[i]);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		free(disk_list);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate static int
210*d155f3feSshidokht i_match_pcibdf(di_node_t node, void *arg)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	pcibdf_t *pbp;
2137c478bd9Sstevel@tonic-gate 	int len;
2147c478bd9Sstevel@tonic-gate 	uint32_t	regval;
2157c478bd9Sstevel@tonic-gate 	uint32_t	busnum, funcnum, devicenum;
2167c478bd9Sstevel@tonic-gate 	char *devtype;
2177c478bd9Sstevel@tonic-gate 	uint32_t *regbuf = NULL;
2187c478bd9Sstevel@tonic-gate 	di_node_t	parentnode;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	pbp = (pcibdf_t *)arg;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	parentnode = di_parent_node(node);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	len = di_prop_lookup_strings(DDI_DEV_T_ANY, parentnode,
2257c478bd9Sstevel@tonic-gate 	    "device_type", (char **)&devtype);
2267c478bd9Sstevel@tonic-gate 
22770025d76Sjohnny 	if ((len <= 0) ||
22870025d76Sjohnny 	    ((strcmp(devtype, "pci") != 0) && (strcmp(devtype, "pciex") != 0)))
2297c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	len = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg",
2327c478bd9Sstevel@tonic-gate 	    (int **)&regbuf);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (len <= 0) {
2357c478bd9Sstevel@tonic-gate 		/* Try PROM property */
2367c478bd9Sstevel@tonic-gate 		len = di_prom_prop_lookup_ints(prom_hdl, node, "reg",
2377c478bd9Sstevel@tonic-gate 		    (int **)&regbuf);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (len > 0) {
2427c478bd9Sstevel@tonic-gate 		regval = regbuf[0];
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		busnum = PCI_REG_BUS_G(regval);
2457c478bd9Sstevel@tonic-gate 		devicenum = PCI_REG_DEV_G(regval);
2467c478bd9Sstevel@tonic-gate 		funcnum = PCI_REG_FUNC_G(regval);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		if ((busnum == pbp->busnum) &&
2497c478bd9Sstevel@tonic-gate 		    (devicenum == pbp->devnum) &&
2507c478bd9Sstevel@tonic-gate 		    (funcnum == pbp->funcnum)) {
2517c478bd9Sstevel@tonic-gate 			/* found it */
2527c478bd9Sstevel@tonic-gate 			pbp->di_node = node;
2537c478bd9Sstevel@tonic-gate 			return (DI_WALK_TERMINATE);
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static di_node_t
261*d155f3feSshidokht search_tree_match_pcibdf(di_node_t node, int bus, int dev, int fn)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	pcibdf_t pb;
2647c478bd9Sstevel@tonic-gate 	pb.busnum = bus;
2657c478bd9Sstevel@tonic-gate 	pb.devnum = dev;
2667c478bd9Sstevel@tonic-gate 	pb.funcnum = fn;
2677c478bd9Sstevel@tonic-gate 	pb.di_node = DI_NODE_NIL;
2687c478bd9Sstevel@tonic-gate 
269*d155f3feSshidokht 	(void) di_walk_node(node, DI_WALK_CLDFIRST, &pb, i_match_pcibdf);
2707c478bd9Sstevel@tonic-gate 	return (pb.di_node);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate static int
275*d155f3feSshidokht i_match_usbserialno(di_node_t node, void *arg)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	int len;
2787c478bd9Sstevel@tonic-gate 	char *serialp;
2797c478bd9Sstevel@tonic-gate 	usbser_t *usbsp;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	usbsp = (usbser_t *)arg;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	len = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, "usb-serialno",
2847c478bd9Sstevel@tonic-gate 	    (uchar_t **)&serialp);
2857c478bd9Sstevel@tonic-gate 
286*d155f3feSshidokht 	if ((len > 0) && (strncmp((char *)&usbsp->serialno, serialp,
287*d155f3feSshidokht 	    sizeof (uint64_t)) == 0)) {
2887c478bd9Sstevel@tonic-gate 		usbsp->node = node;
2897c478bd9Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate static di_node_t
295*d155f3feSshidokht search_tree_match_usbserialno(di_node_t node, uint64_t serialno)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	usbser_t usbs;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	usbs.serialno = serialno;
3017c478bd9Sstevel@tonic-gate 	usbs.node = DI_NODE_NIL;
3027c478bd9Sstevel@tonic-gate 
303*d155f3feSshidokht 	(void) di_walk_node(node, DI_WALK_CLDFIRST, &usbs, i_match_usbserialno);
3047c478bd9Sstevel@tonic-gate 	return (usbs.node);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
307*d155f3feSshidokht /*
308*d155f3feSshidokht  * returns the index to the disklist to the disk with matching path
309*d155f3feSshidokht  */
3107c478bd9Sstevel@tonic-gate static int
311*d155f3feSshidokht search_disklist_match_path(char *path)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	int i;
3147c478bd9Sstevel@tonic-gate 	for (i = 0; i < disk_list_valid; i++)
3157c478bd9Sstevel@tonic-gate 		if (strcmp(disk_list[i], path) == 0) {
3167c478bd9Sstevel@tonic-gate 			return (i);
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 	return (-1);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
321*d155f3feSshidokht /*
322*d155f3feSshidokht  * Find first child of 'node' whose unit address is 'matchbusaddr'
323*d155f3feSshidokht  */
324*d155f3feSshidokht static di_node_t
325*d155f3feSshidokht search_children_match_busaddr(di_node_t node, char *matchbusaddr)
326*d155f3feSshidokht {
327*d155f3feSshidokht 	di_node_t cnode;
328*d155f3feSshidokht 	char *busaddr;
329*d155f3feSshidokht 
330*d155f3feSshidokht 	if (matchbusaddr == NULL)
331*d155f3feSshidokht 		return (DI_NODE_NIL);
332*d155f3feSshidokht 
333*d155f3feSshidokht 
334*d155f3feSshidokht 	for (cnode = di_child_node(node); cnode != DI_NODE_NIL;
335*d155f3feSshidokht 	    cnode = di_sibling_node(cnode)) {
336*d155f3feSshidokht 		busaddr = di_bus_addr(cnode);
337*d155f3feSshidokht 		if (busaddr == NULL)
338*d155f3feSshidokht 			continue;
339*d155f3feSshidokht 		if (strncmp(busaddr, matchbusaddr, MAXNAMELEN) == 0)
340*d155f3feSshidokht 			break;
341*d155f3feSshidokht 	}
342*d155f3feSshidokht 	return (cnode);
343*d155f3feSshidokht }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * Construct a physical device pathname from EDD and verify the
3477c478bd9Sstevel@tonic-gate  * path exists. Return the index of in disk_list for the mapped
3487c478bd9Sstevel@tonic-gate  * path on success, -1 on failure.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate static int
3517c478bd9Sstevel@tonic-gate match_edd(biosdev_data_t *bdata)
3527c478bd9Sstevel@tonic-gate {
353*d155f3feSshidokht 	di_node_t node, cnode = DI_NODE_NIL;
354*d155f3feSshidokht 	char *devfspath = NULL;
3557c478bd9Sstevel@tonic-gate 	fn48_t *bd;
3567c478bd9Sstevel@tonic-gate 	int index;
357*d155f3feSshidokht 	char busaddrbuf[MAXNAMELEN];
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (!bdata->edd_valid) {
3607c478bd9Sstevel@tonic-gate 		if (debug)
3617c478bd9Sstevel@tonic-gate 			(void) printf("edd not valid\n");
3627c478bd9Sstevel@tonic-gate 		return (-1);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	bd = &bdata->fn48_dev_params;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if (bd->magic != 0xBEDD || bd->pathinfo_len == 0) {
3687c478bd9Sstevel@tonic-gate 		/* EDD extensions for devicepath not present */
3697c478bd9Sstevel@tonic-gate 		if (debug)
3707c478bd9Sstevel@tonic-gate 			(void) printf("magic not valid %x pathinfolen %d\n",
3717c478bd9Sstevel@tonic-gate 			    bd->magic, bd->pathinfo_len);
3727c478bd9Sstevel@tonic-gate 		return (-1);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/* we handle only PCI scsi, ata or sata for now */
3767c478bd9Sstevel@tonic-gate 	if (strncmp(bd->bustype, "PCI", 3) != 0) {
3777c478bd9Sstevel@tonic-gate 		if (debug)
3787c478bd9Sstevel@tonic-gate 			(void) printf("was not pci %s\n", bd->bustype);
3797c478bd9Sstevel@tonic-gate 		return (-1);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	if (debug)
3827c478bd9Sstevel@tonic-gate 		(void) printf("match_edd bdf %d %d %d\n",
3837c478bd9Sstevel@tonic-gate 		    bd->interfacepath.pci.bus,
3847c478bd9Sstevel@tonic-gate 		    bd->interfacepath.pci.device,
3857c478bd9Sstevel@tonic-gate 		    bd->interfacepath.pci.function);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* look into devinfo tree and find a node with matching pci b/d/f */
388*d155f3feSshidokht 	node = search_tree_match_pcibdf(root_node, bd->interfacepath.pci.bus,
389*d155f3feSshidokht 	    bd->interfacepath.pci.device, bd->interfacepath.pci.function);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
3927c478bd9Sstevel@tonic-gate 		if (debug)
3937c478bd9Sstevel@tonic-gate 			(void) printf(" could not find a node in tree "
3947c478bd9Sstevel@tonic-gate 			    "matching bdf\n");
3957c478bd9Sstevel@tonic-gate 		return (-1);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (debug)
399*d155f3feSshidokht 		(void) printf("interface type %s pci channel %x target %x\n",
400*d155f3feSshidokht 		    bd->interface_type, bd->interfacepath.pci.channel,
401*d155f3feSshidokht 		    bd->devicepath.scsi.target);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (strncmp(bd->interface_type, "SCSI", 4) == 0) {
4047c478bd9Sstevel@tonic-gate 
405*d155f3feSshidokht 		(void) snprintf(busaddrbuf, MAXNAMELEN, "%x,%x",
4067c478bd9Sstevel@tonic-gate 		    bd->devicepath.scsi.target, bd->devicepath.scsi.lun_lo);
4077c478bd9Sstevel@tonic-gate 
408*d155f3feSshidokht 		cnode = search_children_match_busaddr(node, busaddrbuf);
4097c478bd9Sstevel@tonic-gate 
410*d155f3feSshidokht 	} else if ((strncmp(bd->interface_type, "ATAPI", 5) == 0) ||
411*d155f3feSshidokht 	    (strncmp(bd->interface_type, "ATA", 3) == 0) ||
412*d155f3feSshidokht 	    (strncmp(bd->interface_type, "SATA", 4) == 0)) {
413*d155f3feSshidokht 
414*d155f3feSshidokht 		if (strncmp(di_node_name(node), "pci-ide", 7) == 0) {
415*d155f3feSshidokht 			/*
416*d155f3feSshidokht 			 * Legacy using pci-ide
417*d155f3feSshidokht 			 * the child should be ide@<x>, where x is
418*d155f3feSshidokht 			 * the channel number
419*d155f3feSshidokht 			 */
420*d155f3feSshidokht 			(void) snprintf(busaddrbuf, MAXNAMELEN, "%d",
421*d155f3feSshidokht 			    bd->interfacepath.pci.channel);
422*d155f3feSshidokht 
423*d155f3feSshidokht 			if ((cnode = search_children_match_busaddr(node,
424*d155f3feSshidokht 			    busaddrbuf)) != DI_NODE_NIL) {
425*d155f3feSshidokht 
426*d155f3feSshidokht 				(void) snprintf(busaddrbuf, MAXNAMELEN, "%x,0",
427*d155f3feSshidokht 				    bd->devicepath.ata.chan);
428*d155f3feSshidokht 				cnode = search_children_match_busaddr(cnode,
429*d155f3feSshidokht 				    busaddrbuf);
430*d155f3feSshidokht 
431*d155f3feSshidokht 				if (cnode == DI_NODE_NIL)
432*d155f3feSshidokht 					if (debug)
433*d155f3feSshidokht 						(void) printf("Interface %s "
434*d155f3feSshidokht 						    "using pci-ide no "
435*d155f3feSshidokht 						    "grandchild at %s\n",
436*d155f3feSshidokht 						    bd->interface_type,
437*d155f3feSshidokht 						    busaddrbuf);
438*d155f3feSshidokht 			} else {
439*d155f3feSshidokht 				if (debug)
440*d155f3feSshidokht 					(void) printf("Interface %s using "
441*d155f3feSshidokht 					    "pci-ide, with no child at %s\n",
442*d155f3feSshidokht 					    bd->interface_type, busaddrbuf);
443*d155f3feSshidokht 			}
444*d155f3feSshidokht 		} else {
445*d155f3feSshidokht 			if (strncmp(bd->interface_type, "SATA", 4) == 0) {
446*d155f3feSshidokht 				/*
447*d155f3feSshidokht 				 * The current EDD (EDD-2) spec does not
448*d155f3feSshidokht 				 * address port number. This is work in
449*d155f3feSshidokht 				 * progress.
450*d155f3feSshidokht 				 * Interprete the first field of device path
451*d155f3feSshidokht 				 * as port number. Needs to be revisited
452*d155f3feSshidokht 				 * with port multiplier support.
453*d155f3feSshidokht 				 */
454*d155f3feSshidokht 				(void) snprintf(busaddrbuf, MAXNAMELEN, "%x,0",
4557c478bd9Sstevel@tonic-gate 				    bd->devicepath.ata.chan);
4567c478bd9Sstevel@tonic-gate 
457*d155f3feSshidokht 				cnode = search_children_match_busaddr(node,
458*d155f3feSshidokht 				    busaddrbuf);
459*d155f3feSshidokht 			} else {
460*d155f3feSshidokht 				if (debug)
461*d155f3feSshidokht 					(void) printf("Interface %s, not using"
462*d155f3feSshidokht 					    " pci-ide\n", bd->interface_type);
463*d155f3feSshidokht 			}
464*d155f3feSshidokht 		}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	} else if (strncmp(bd->interface_type, "USB", 3) == 0) {
467*d155f3feSshidokht 		cnode = search_tree_match_usbserialno(node,
468*d155f3feSshidokht 		    bd->devicepath.usb.usb_serial_id);
4697c478bd9Sstevel@tonic-gate 	} else {
4707c478bd9Sstevel@tonic-gate 		if (debug)
4717c478bd9Sstevel@tonic-gate 			(void) printf("sorry not supported interface %s\n",
4727c478bd9Sstevel@tonic-gate 			    bd->interface_type);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
475*d155f3feSshidokht 	if (cnode != DI_NODE_NIL) {
476*d155f3feSshidokht 		devfspath = di_devfs_path(cnode);
477*d155f3feSshidokht 		index = search_disklist_match_path(devfspath);
478*d155f3feSshidokht 		di_devfs_path_free(devfspath);
479*d155f3feSshidokht 		if (index >= 0)
4807c478bd9Sstevel@tonic-gate 			return (index);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	return (-1);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * For each disk in list of disks, compare the first block with the
4887c478bd9Sstevel@tonic-gate  * one from bdd. On the first match, return the index of path in
4897c478bd9Sstevel@tonic-gate  * disk_list. If none matched return -1.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate static int
4927c478bd9Sstevel@tonic-gate match_first_block(biosdev_data_t *bd)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	char diskpath[MAXPATHLEN];
4967c478bd9Sstevel@tonic-gate 	int fd;
4977c478bd9Sstevel@tonic-gate 	char buf[512];
4987c478bd9Sstevel@tonic-gate 	ssize_t	num_read;
4997c478bd9Sstevel@tonic-gate 	int i;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (!bd->first_block_valid)
5027c478bd9Sstevel@tonic-gate 		return (-1);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	for (i = 0; i < disk_list_valid; i++) {
5057c478bd9Sstevel@tonic-gate 		(void) snprintf(diskpath, MAXPATHLEN, "%s/%s:q,raw",
5067c478bd9Sstevel@tonic-gate 		    DEVFS_PREFIX, disk_list[i]);
5077c478bd9Sstevel@tonic-gate 		fd = open(diskpath, O_RDONLY);
5087c478bd9Sstevel@tonic-gate 		if (fd  < 0) {
5097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "opening %s failed errno %d\n",
5107c478bd9Sstevel@tonic-gate 			    diskpath, errno);
5117c478bd9Sstevel@tonic-gate 			continue;
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		num_read = read(fd, buf, 512);
5147c478bd9Sstevel@tonic-gate 		if (num_read != 512) {
5157c478bd9Sstevel@tonic-gate 			(void) printf("read only %d bytes from %s\n", num_read,
5167c478bd9Sstevel@tonic-gate 			    diskpath);
5177c478bd9Sstevel@tonic-gate 			continue;
5187c478bd9Sstevel@tonic-gate 		}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		if (memcmp(buf, bd->first_block, 512) == 0)	 {
5217c478bd9Sstevel@tonic-gate 			/* found it */
5227c478bd9Sstevel@tonic-gate 			return (i);
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 	return (-1);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate static void
5307c478bd9Sstevel@tonic-gate cleanup_and_exit(int exitcode)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	free_disks();
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (root_node != DI_NODE_NIL)
5367c478bd9Sstevel@tonic-gate 		di_fini(root_node);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (root_allnode != DI_NODE_NIL)
5397c478bd9Sstevel@tonic-gate 		di_fini(root_allnode);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (prom_hdl != DI_PROM_HANDLE_NIL)
5427c478bd9Sstevel@tonic-gate 		di_prom_fini(prom_hdl);
5437c478bd9Sstevel@tonic-gate 	exit(exitcode);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate int
5497c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	biosdev_data_t		*biosdata;
5527c478bd9Sstevel@tonic-gate 	int len, i, c, j;
5537c478bd9Sstevel@tonic-gate 	int matchedindex = -1;
5547c478bd9Sstevel@tonic-gate 	char biospropname[BIOSPROPNAME_TMPL_LEN];
5557c478bd9Sstevel@tonic-gate 	int totalmatches = 0;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != -1)  {
5597c478bd9Sstevel@tonic-gate 		switch (c) {
5607c478bd9Sstevel@tonic-gate 		case 'd':
5617c478bd9Sstevel@tonic-gate 			debug = 1;
5627c478bd9Sstevel@tonic-gate 			break;
5637c478bd9Sstevel@tonic-gate 		default:
5647c478bd9Sstevel@tonic-gate 			(void) printf("unknown option %c\n", c);
5657c478bd9Sstevel@tonic-gate 			exit(1);
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if ((prom_hdl = di_prom_init()) == DI_PROM_HANDLE_NIL) {
5707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "di_prom_init failed\n");
5717c478bd9Sstevel@tonic-gate 		cleanup_and_exit(1);
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if ((root_node = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
5757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "di_init failed\n");
5767c478bd9Sstevel@tonic-gate 		cleanup_and_exit(1);
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if ((root_allnode = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
5807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "di_init failed\n");
5817c478bd9Sstevel@tonic-gate 		cleanup_and_exit(1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	(void) memset(mapinfo, 0, sizeof (mapinfo));
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* get a list of all disks in the system */
5877c478bd9Sstevel@tonic-gate 	build_disk_list();
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/* for each property try to match with a disk */
5907c478bd9Sstevel@tonic-gate 	for (i = 0; i < BIOSDEV_NUM; i++) {
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		(void) snprintf((char *)biospropname, BIOSPROPNAME_TMPL_LEN,
5937c478bd9Sstevel@tonic-gate 		    BIOSPROPNAME_TMPL, i + STARTING_DRVNUM);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		len = di_prop_lookup_bytes(DDI_DEV_T_ANY, root_allnode,
5967c478bd9Sstevel@tonic-gate 		    biospropname, (uchar_t **)&biosdata);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		if (len <= 0) {
5997c478bd9Sstevel@tonic-gate 			continue;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		if (debug)
6037c478bd9Sstevel@tonic-gate 			(void) printf("matching %s\n", biospropname);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		matchedindex = match_edd(biosdata);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		if (matchedindex == -1) {
6087c478bd9Sstevel@tonic-gate 			matchedindex = match_first_block(biosdata);
6097c478bd9Sstevel@tonic-gate 			if (debug && matchedindex != -1)
6107c478bd9Sstevel@tonic-gate 				(void) printf("matched first block\n");
6117c478bd9Sstevel@tonic-gate 		} else if (debug)
6127c478bd9Sstevel@tonic-gate 			(void) printf("matched thru edd\n");
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		if (matchedindex != -1) {
6157c478bd9Sstevel@tonic-gate 			mapinfo[i].disklist_index = matchedindex;
6167c478bd9Sstevel@tonic-gate 			mapinfo[i].matchcount++;
6177c478bd9Sstevel@tonic-gate 			if (debug)
6187c478bd9Sstevel@tonic-gate 				(void) printf("0x%x %s\n", i + STARTING_DRVNUM,
6197c478bd9Sstevel@tonic-gate 				    disk_list[matchedindex]);
6207c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; j++) {
6217c478bd9Sstevel@tonic-gate 				if (mapinfo[j].matchcount > 0 &&
6227c478bd9Sstevel@tonic-gate 				    mapinfo[j].disklist_index == matchedindex) {
6237c478bd9Sstevel@tonic-gate 					mapinfo[j].matchcount++;
6247c478bd9Sstevel@tonic-gate 					mapinfo[i].matchcount++;
6257c478bd9Sstevel@tonic-gate 				}
6267c478bd9Sstevel@tonic-gate 			}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		} else if (debug)
6297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Could not match %s\n",
6307c478bd9Sstevel@tonic-gate 			    biospropname);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	for (i = 0; i < BIOSDEV_NUM; i++) {
6347c478bd9Sstevel@tonic-gate 		if (mapinfo[i].matchcount == 1) {
6357c478bd9Sstevel@tonic-gate 			(void) printf("0x%x %s\n", i + STARTING_DRVNUM,
6367c478bd9Sstevel@tonic-gate 			    disk_list[mapinfo[i].disklist_index]);
6377c478bd9Sstevel@tonic-gate 			totalmatches++;
6387c478bd9Sstevel@tonic-gate 		} else if (debug && mapinfo[i].matchcount > 1) {
6397c478bd9Sstevel@tonic-gate 			(void) printf("0x%x %s matchcount %d\n",
6407c478bd9Sstevel@tonic-gate 			    i + STARTING_DRVNUM,
6417c478bd9Sstevel@tonic-gate 			    disk_list[mapinfo[i].disklist_index],
6427c478bd9Sstevel@tonic-gate 			    mapinfo[i].matchcount);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if (totalmatches == 0) {
6477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "biosdev: Could not match any!!\n");
6487c478bd9Sstevel@tonic-gate 		cleanup_and_exit(1);
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	cleanup_and_exit(0);
6527c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
6537c478bd9Sstevel@tonic-gate 	return (0);
6547c478bd9Sstevel@tonic-gate }
655