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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <unistd.h> 337c478bd9Sstevel@tonic-gate #include <sys/param.h> 347c478bd9Sstevel@tonic-gate #include <errno.h> 357c478bd9Sstevel@tonic-gate #include <sys/types.h> 367c478bd9Sstevel@tonic-gate #include <dirent.h> 377c478bd9Sstevel@tonic-gate #include <libdevinfo.h> 387c478bd9Sstevel@tonic-gate #include <fcntl.h> 397c478bd9Sstevel@tonic-gate #include <sys/types.h> 407c478bd9Sstevel@tonic-gate #include <sys/stat.h> 417c478bd9Sstevel@tonic-gate #include <sys/pci.h> 427c478bd9Sstevel@tonic-gate #include <sys/biosdisk.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * structure used for searching device tree for a node matching 477c478bd9Sstevel@tonic-gate * pci bus/dev/fn 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate typedef struct pcibdf { 507c478bd9Sstevel@tonic-gate int busnum; 517c478bd9Sstevel@tonic-gate int devnum; 527c478bd9Sstevel@tonic-gate int funcnum; 537c478bd9Sstevel@tonic-gate di_node_t di_node; 547c478bd9Sstevel@tonic-gate } pcibdf_t; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * structure used for searching device tree for a node matching 587c478bd9Sstevel@tonic-gate * USB serial number. 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate typedef struct { 617c478bd9Sstevel@tonic-gate uint64_t serialno; 627c478bd9Sstevel@tonic-gate di_node_t node; 637c478bd9Sstevel@tonic-gate } usbser_t; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * structure for holding the mapping info 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate typedef struct { 697c478bd9Sstevel@tonic-gate int disklist_index; /* index to disk_list of the mapped path */ 707c478bd9Sstevel@tonic-gate int matchcount; /* number of matches per this device number */ 717c478bd9Sstevel@tonic-gate } mapinfo_t; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate #define DEVFS_PREFIX "/devices" 747c478bd9Sstevel@tonic-gate #define DISKS_LIST_INCR 20 /* increment for resizing disk_list */ 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate #define BIOSPROPNAME_TMPL "biosdev-0x%x" 777c478bd9Sstevel@tonic-gate #define BIOSPROPNAME_TMPL_LEN 13 787c478bd9Sstevel@tonic-gate #define BIOSDEV_NUM 8 797c478bd9Sstevel@tonic-gate #define STARTING_DRVNUM 0x80 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * array to hold mappings. Element at index X corresponds to BIOS device 837c478bd9Sstevel@tonic-gate * number 0x80 + X 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate static mapinfo_t mapinfo[BIOSDEV_NUM]; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * Cache copy of kernel device tree snapshot root handle, includes devices 897c478bd9Sstevel@tonic-gate * that are detached 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate static di_node_t root_node = DI_NODE_NIL; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * kernel device tree snapshot with currently attached devices. Detached 957c478bd9Sstevel@tonic-gate * devices are not included. 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate static di_node_t root_allnode = DI_NODE_NIL; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * handle to retrieve prom properties 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate static di_prom_handle_t prom_hdl = DI_PROM_HANDLE_NIL; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate static char **disk_list = NULL; /* array of physical device pathnames */ 1067c478bd9Sstevel@tonic-gate static int disk_list_len = 0; /* length of disk_list */ 1077c478bd9Sstevel@tonic-gate static int disk_list_valid = 0; /* number of valid entries in disk_list */ 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate static int debug = 0; /* used for enabling debug output */ 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* Local function prototypes */ 1137c478bd9Sstevel@tonic-gate static void new_disk_list_entry(di_node_t node); 1147c478bd9Sstevel@tonic-gate static int find_disks_callback(di_node_t node, di_minor_t minor, void *arg); 1157c478bd9Sstevel@tonic-gate static void build_disk_list(); 1167c478bd9Sstevel@tonic-gate static void free_disks(); 1177c478bd9Sstevel@tonic-gate static int matchpcibdf(di_node_t node, void *arg); 1187c478bd9Sstevel@tonic-gate static int matchusbserial(di_node_t node, void *arg); 1197c478bd9Sstevel@tonic-gate static di_node_t search_tree_forpcibdf(int bus, int dev, int fn); 1207c478bd9Sstevel@tonic-gate static int match_edd(biosdev_data_t *bd); 1217c478bd9Sstevel@tonic-gate static int match_first_block(biosdev_data_t *bd); 1227c478bd9Sstevel@tonic-gate static void cleanup_and_exit(int); 1237c478bd9Sstevel@tonic-gate static int find_path_index_in_disk_list(char *path); 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate static void 1277c478bd9Sstevel@tonic-gate new_disk_list_entry(di_node_t node) 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate size_t newsize; 1307c478bd9Sstevel@tonic-gate char **newlist; 1317c478bd9Sstevel@tonic-gate int newlen; 1327c478bd9Sstevel@tonic-gate char *devfspath; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate if (disk_list_valid >= disk_list_len) { 1357c478bd9Sstevel@tonic-gate /* valid should never really be larger than len */ 1367c478bd9Sstevel@tonic-gate /* if they are equal we need to init or realloc */ 1377c478bd9Sstevel@tonic-gate newlen = disk_list_len + DISKS_LIST_INCR; 1387c478bd9Sstevel@tonic-gate newsize = newlen * sizeof (*disk_list); 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate newlist = (char **)realloc(disk_list, newsize); 1417c478bd9Sstevel@tonic-gate if (newlist == NULL) { 1427c478bd9Sstevel@tonic-gate (void) printf("realloc failed to resize disk table\n"); 1437c478bd9Sstevel@tonic-gate cleanup_and_exit(1); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate disk_list = newlist; 1467c478bd9Sstevel@tonic-gate disk_list_len = newlen; 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate devfspath = di_devfs_path(node); 1507c478bd9Sstevel@tonic-gate disk_list[disk_list_valid] = devfspath; 1517c478bd9Sstevel@tonic-gate if (debug) 1527c478bd9Sstevel@tonic-gate (void) printf("adding %s\n", devfspath); 1537c478bd9Sstevel@tonic-gate disk_list_valid++; 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1577c478bd9Sstevel@tonic-gate static int 1587c478bd9Sstevel@tonic-gate find_disks_callback(di_node_t node, di_minor_t minor, void *arg) 1597c478bd9Sstevel@tonic-gate { 1607c478bd9Sstevel@tonic-gate char *minortype; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate if (di_minor_spectype(minor) == S_IFCHR) { 1637c478bd9Sstevel@tonic-gate minortype = di_minor_nodetype(minor); 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* exclude CD's */ 1667c478bd9Sstevel@tonic-gate if (strncmp(minortype, DDI_NT_CD, sizeof (DDI_NT_CD) - 1) != 0) 1677c478bd9Sstevel@tonic-gate /* only take p0 raw device */ 1687c478bd9Sstevel@tonic-gate if (strcmp(di_minor_name(minor), "q,raw") == 0) 1697c478bd9Sstevel@tonic-gate new_disk_list_entry(node); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate static void 1757c478bd9Sstevel@tonic-gate build_disk_list() 1767c478bd9Sstevel@tonic-gate { 1777c478bd9Sstevel@tonic-gate int ret; 1787c478bd9Sstevel@tonic-gate ret = di_walk_minor(root_node, DDI_NT_BLOCK, 0, NULL, 1797c478bd9Sstevel@tonic-gate find_disks_callback); 1807c478bd9Sstevel@tonic-gate if (ret != 0) { 1817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "di_walk_minor failed errno %d\n", 1827c478bd9Sstevel@tonic-gate errno); 1837c478bd9Sstevel@tonic-gate cleanup_and_exit(1); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate static void 1887c478bd9Sstevel@tonic-gate free_disks() 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate int i; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (disk_list) { 1937c478bd9Sstevel@tonic-gate for (i = 0; i < disk_list_valid; i++) 1947c478bd9Sstevel@tonic-gate di_devfs_path_free(disk_list[i]); 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate free(disk_list); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate static int 2017c478bd9Sstevel@tonic-gate matchpcibdf(di_node_t node, void *arg) 2027c478bd9Sstevel@tonic-gate { 2037c478bd9Sstevel@tonic-gate pcibdf_t *pbp; 2047c478bd9Sstevel@tonic-gate int len; 2057c478bd9Sstevel@tonic-gate uint32_t regval; 2067c478bd9Sstevel@tonic-gate uint32_t busnum, funcnum, devicenum; 2077c478bd9Sstevel@tonic-gate char *devtype; 2087c478bd9Sstevel@tonic-gate uint32_t *regbuf = NULL; 2097c478bd9Sstevel@tonic-gate di_node_t parentnode; 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate pbp = (pcibdf_t *)arg; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate parentnode = di_parent_node(node); 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate len = di_prop_lookup_strings(DDI_DEV_T_ANY, parentnode, 2167c478bd9Sstevel@tonic-gate "device_type", (char **)&devtype); 2177c478bd9Sstevel@tonic-gate 218*70025d76Sjohnny if ((len <= 0) || 219*70025d76Sjohnny ((strcmp(devtype, "pci") != 0) && (strcmp(devtype, "pciex") != 0))) 2207c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate len = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", 2237c478bd9Sstevel@tonic-gate (int **)®buf); 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate if (len <= 0) { 2267c478bd9Sstevel@tonic-gate /* Try PROM property */ 2277c478bd9Sstevel@tonic-gate len = di_prom_prop_lookup_ints(prom_hdl, node, "reg", 2287c478bd9Sstevel@tonic-gate (int **)®buf); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate if (len > 0) { 2337c478bd9Sstevel@tonic-gate regval = regbuf[0]; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate busnum = PCI_REG_BUS_G(regval); 2367c478bd9Sstevel@tonic-gate devicenum = PCI_REG_DEV_G(regval); 2377c478bd9Sstevel@tonic-gate funcnum = PCI_REG_FUNC_G(regval); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if ((busnum == pbp->busnum) && 2407c478bd9Sstevel@tonic-gate (devicenum == pbp->devnum) && 2417c478bd9Sstevel@tonic-gate (funcnum == pbp->funcnum)) { 2427c478bd9Sstevel@tonic-gate /* found it */ 2437c478bd9Sstevel@tonic-gate pbp->di_node = node; 2447c478bd9Sstevel@tonic-gate return (DI_WALK_TERMINATE); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate static di_node_t 2527c478bd9Sstevel@tonic-gate search_tree_forpcibdf(int bus, int dev, int fn) 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate pcibdf_t pb; 2557c478bd9Sstevel@tonic-gate pb.busnum = bus; 2567c478bd9Sstevel@tonic-gate pb.devnum = dev; 2577c478bd9Sstevel@tonic-gate pb.funcnum = fn; 2587c478bd9Sstevel@tonic-gate pb.di_node = DI_NODE_NIL; 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate (void) di_walk_node(root_node, DI_WALK_CLDFIRST, &pb, matchpcibdf); 2617c478bd9Sstevel@tonic-gate return (pb.di_node); 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate static int 2667c478bd9Sstevel@tonic-gate matchusbserial(di_node_t node, void *arg) 2677c478bd9Sstevel@tonic-gate { 2687c478bd9Sstevel@tonic-gate int len; 2697c478bd9Sstevel@tonic-gate char *serialp; 2707c478bd9Sstevel@tonic-gate usbser_t *usbsp; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate usbsp = (usbser_t *)arg; 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate len = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, "usb-serialno", 2757c478bd9Sstevel@tonic-gate (uchar_t **)&serialp); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if ((len > 0) && (strcmp((char *)&usbsp->serialno, serialp) == 0)) { 2787c478bd9Sstevel@tonic-gate usbsp->node = node; 2797c478bd9Sstevel@tonic-gate return (DI_WALK_TERMINATE); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate static di_node_t 2857c478bd9Sstevel@tonic-gate match_usb(di_node_t node, uint64_t serialno) 2867c478bd9Sstevel@tonic-gate { 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate usbser_t usbs; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate usbs.serialno = serialno; 2917c478bd9Sstevel@tonic-gate usbs.node = DI_NODE_NIL; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate (void) di_walk_node(node, DI_WALK_CLDFIRST, &usbs, matchusbserial); 2947c478bd9Sstevel@tonic-gate return (usbs.node); 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate static int 2997c478bd9Sstevel@tonic-gate find_path_index_in_disk_list(char *path) 3007c478bd9Sstevel@tonic-gate { 3017c478bd9Sstevel@tonic-gate int i; 3027c478bd9Sstevel@tonic-gate for (i = 0; i < disk_list_valid; i++) 3037c478bd9Sstevel@tonic-gate if (strcmp(disk_list[i], path) == 0) { 3047c478bd9Sstevel@tonic-gate return (i); 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate return (-1); 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate /* 3117c478bd9Sstevel@tonic-gate * Construct a physical device pathname from EDD and verify the 3127c478bd9Sstevel@tonic-gate * path exists. Return the index of in disk_list for the mapped 3137c478bd9Sstevel@tonic-gate * path on success, -1 on failure. 3147c478bd9Sstevel@tonic-gate */ 3157c478bd9Sstevel@tonic-gate static int 3167c478bd9Sstevel@tonic-gate match_edd(biosdev_data_t *bdata) 3177c478bd9Sstevel@tonic-gate { 3187c478bd9Sstevel@tonic-gate di_node_t node; 3197c478bd9Sstevel@tonic-gate char *devfspath; 3207c478bd9Sstevel@tonic-gate fn48_t *bd; 3217c478bd9Sstevel@tonic-gate int index; 3227c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate if (!bdata->edd_valid) { 3257c478bd9Sstevel@tonic-gate if (debug) 3267c478bd9Sstevel@tonic-gate (void) printf("edd not valid\n"); 3277c478bd9Sstevel@tonic-gate return (-1); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate bd = &bdata->fn48_dev_params; 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate if (bd->magic != 0xBEDD || bd->pathinfo_len == 0) { 3337c478bd9Sstevel@tonic-gate /* EDD extensions for devicepath not present */ 3347c478bd9Sstevel@tonic-gate if (debug) 3357c478bd9Sstevel@tonic-gate (void) printf("magic not valid %x pathinfolen %d\n", 3367c478bd9Sstevel@tonic-gate bd->magic, bd->pathinfo_len); 3377c478bd9Sstevel@tonic-gate return (-1); 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* we handle only PCI scsi, ata or sata for now */ 3417c478bd9Sstevel@tonic-gate if (strncmp(bd->bustype, "PCI", 3) != 0) { 3427c478bd9Sstevel@tonic-gate if (debug) 3437c478bd9Sstevel@tonic-gate (void) printf("was not pci %s\n", bd->bustype); 3447c478bd9Sstevel@tonic-gate return (-1); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate if (debug) 3477c478bd9Sstevel@tonic-gate (void) printf("match_edd bdf %d %d %d\n", 3487c478bd9Sstevel@tonic-gate bd->interfacepath.pci.bus, 3497c478bd9Sstevel@tonic-gate bd->interfacepath.pci.device, 3507c478bd9Sstevel@tonic-gate bd->interfacepath.pci.function); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate /* look into devinfo tree and find a node with matching pci b/d/f */ 3537c478bd9Sstevel@tonic-gate node = search_tree_forpcibdf(bd->interfacepath.pci.bus, 3547c478bd9Sstevel@tonic-gate bd->interfacepath.pci.device, 3557c478bd9Sstevel@tonic-gate bd->interfacepath.pci.function); 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate if (node == DI_NODE_NIL) { 3587c478bd9Sstevel@tonic-gate if (debug) 3597c478bd9Sstevel@tonic-gate (void) printf(" could not find a node in tree " 3607c478bd9Sstevel@tonic-gate "matching bdf\n"); 3617c478bd9Sstevel@tonic-gate return (-1); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate /* construct a path */ 3657c478bd9Sstevel@tonic-gate devfspath = di_devfs_path(node); 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate if (debug) 3687c478bd9Sstevel@tonic-gate (void) printf("interface type %s\n", bd->interface_type); 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate if (strncmp(bd->interface_type, "SCSI", 4) == 0) { 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate /* at this time sd doesnot support luns greater than uchar_t */ 3737c478bd9Sstevel@tonic-gate (void) snprintf(path, MAXPATHLEN, "%s/sd@%d,%d", devfspath, 3747c478bd9Sstevel@tonic-gate bd->devicepath.scsi.target, bd->devicepath.scsi.lun_lo); 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate } else if (strncmp(bd->interface_type, "ATAPI", 5) == 0) { 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate (void) snprintf(path, MAXPATHLEN, "%s/ide@%d/sd@%d,0", 3797c478bd9Sstevel@tonic-gate devfspath, bd->interfacepath.pci.channel, 3807c478bd9Sstevel@tonic-gate bd->devicepath.ata.chan); 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate } else if (strncmp(bd->interface_type, "ATA", 3) == 0) { 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate (void) snprintf(path, MAXPATHLEN, "%s/ide@%d/cmdk@%d,0", 3857c478bd9Sstevel@tonic-gate devfspath, bd->interfacepath.pci.channel, 3867c478bd9Sstevel@tonic-gate bd->devicepath.ata.chan); 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate } else if (strncmp(bd->interface_type, "SATA", 4) == 0) { 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate (void) snprintf(path, MAXPATHLEN, "%s/ide@%d/cmdk@%d,0", 3917c478bd9Sstevel@tonic-gate devfspath, bd->interfacepath.pci.channel, 3927c478bd9Sstevel@tonic-gate bd->devicepath.ata.chan); 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate } else if (strncmp(bd->interface_type, "USB", 3) == 0) { 3957c478bd9Sstevel@tonic-gate (void) printf("USB\n"); 3967c478bd9Sstevel@tonic-gate node = match_usb(node, bd->devicepath.usb.usb_serial_id); 3977c478bd9Sstevel@tonic-gate if (node != DI_NODE_NIL) { 3987c478bd9Sstevel@tonic-gate if (debug) 3997c478bd9Sstevel@tonic-gate (void) printf("usb path %s\n", 4007c478bd9Sstevel@tonic-gate di_devfs_path(node)); 4017c478bd9Sstevel@tonic-gate (void) snprintf(path, MAXPATHLEN, "%s", devfspath); 4027c478bd9Sstevel@tonic-gate } else 4037c478bd9Sstevel@tonic-gate return (-1); 4047c478bd9Sstevel@tonic-gate } else { 4057c478bd9Sstevel@tonic-gate if (debug) 4067c478bd9Sstevel@tonic-gate (void) printf("sorry not supported interface %s\n", 4077c478bd9Sstevel@tonic-gate bd->interface_type); 4087c478bd9Sstevel@tonic-gate return (-1); 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate index = find_path_index_in_disk_list(path); 4127c478bd9Sstevel@tonic-gate if (index >= 0) { 4137c478bd9Sstevel@tonic-gate return (index); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate return (-1); 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate /* 4207c478bd9Sstevel@tonic-gate * For each disk in list of disks, compare the first block with the 4217c478bd9Sstevel@tonic-gate * one from bdd. On the first match, return the index of path in 4227c478bd9Sstevel@tonic-gate * disk_list. If none matched return -1. 4237c478bd9Sstevel@tonic-gate */ 4247c478bd9Sstevel@tonic-gate static int 4257c478bd9Sstevel@tonic-gate match_first_block(biosdev_data_t *bd) 4267c478bd9Sstevel@tonic-gate { 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate char diskpath[MAXPATHLEN]; 4297c478bd9Sstevel@tonic-gate int fd; 4307c478bd9Sstevel@tonic-gate char buf[512]; 4317c478bd9Sstevel@tonic-gate ssize_t num_read; 4327c478bd9Sstevel@tonic-gate int i; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate if (!bd->first_block_valid) 4357c478bd9Sstevel@tonic-gate return (-1); 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate for (i = 0; i < disk_list_valid; i++) { 4387c478bd9Sstevel@tonic-gate (void) snprintf(diskpath, MAXPATHLEN, "%s/%s:q,raw", 4397c478bd9Sstevel@tonic-gate DEVFS_PREFIX, disk_list[i]); 4407c478bd9Sstevel@tonic-gate fd = open(diskpath, O_RDONLY); 4417c478bd9Sstevel@tonic-gate if (fd < 0) { 4427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "opening %s failed errno %d\n", 4437c478bd9Sstevel@tonic-gate diskpath, errno); 4447c478bd9Sstevel@tonic-gate continue; 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate num_read = read(fd, buf, 512); 4477c478bd9Sstevel@tonic-gate if (num_read != 512) { 4487c478bd9Sstevel@tonic-gate (void) printf("read only %d bytes from %s\n", num_read, 4497c478bd9Sstevel@tonic-gate diskpath); 4507c478bd9Sstevel@tonic-gate continue; 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate if (memcmp(buf, bd->first_block, 512) == 0) { 4547c478bd9Sstevel@tonic-gate /* found it */ 4557c478bd9Sstevel@tonic-gate return (i); 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate return (-1); 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate static void 4637c478bd9Sstevel@tonic-gate cleanup_and_exit(int exitcode) 4647c478bd9Sstevel@tonic-gate { 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate free_disks(); 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate if (root_node != DI_NODE_NIL) 4697c478bd9Sstevel@tonic-gate di_fini(root_node); 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate if (root_allnode != DI_NODE_NIL) 4727c478bd9Sstevel@tonic-gate di_fini(root_allnode); 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate if (prom_hdl != DI_PROM_HANDLE_NIL) 4757c478bd9Sstevel@tonic-gate di_prom_fini(prom_hdl); 4767c478bd9Sstevel@tonic-gate exit(exitcode); 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate int 4827c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 4837c478bd9Sstevel@tonic-gate { 4847c478bd9Sstevel@tonic-gate biosdev_data_t *biosdata; 4857c478bd9Sstevel@tonic-gate int len, i, c, j; 4867c478bd9Sstevel@tonic-gate int matchedindex = -1; 4877c478bd9Sstevel@tonic-gate char biospropname[BIOSPROPNAME_TMPL_LEN]; 4887c478bd9Sstevel@tonic-gate int totalmatches = 0; 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "d")) != -1) { 4927c478bd9Sstevel@tonic-gate switch (c) { 4937c478bd9Sstevel@tonic-gate case 'd': 4947c478bd9Sstevel@tonic-gate debug = 1; 4957c478bd9Sstevel@tonic-gate break; 4967c478bd9Sstevel@tonic-gate default: 4977c478bd9Sstevel@tonic-gate (void) printf("unknown option %c\n", c); 4987c478bd9Sstevel@tonic-gate exit(1); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate if ((prom_hdl = di_prom_init()) == DI_PROM_HANDLE_NIL) { 5037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "di_prom_init failed\n"); 5047c478bd9Sstevel@tonic-gate cleanup_and_exit(1); 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate if ((root_node = di_init("/", DINFOCACHE)) == DI_NODE_NIL) { 5087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "di_init failed\n"); 5097c478bd9Sstevel@tonic-gate cleanup_and_exit(1); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate if ((root_allnode = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) { 5137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "di_init failed\n"); 5147c478bd9Sstevel@tonic-gate cleanup_and_exit(1); 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate (void) memset(mapinfo, 0, sizeof (mapinfo)); 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate /* get a list of all disks in the system */ 5207c478bd9Sstevel@tonic-gate build_disk_list(); 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate /* for each property try to match with a disk */ 5237c478bd9Sstevel@tonic-gate for (i = 0; i < BIOSDEV_NUM; i++) { 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate (void) snprintf((char *)biospropname, BIOSPROPNAME_TMPL_LEN, 5267c478bd9Sstevel@tonic-gate BIOSPROPNAME_TMPL, i + STARTING_DRVNUM); 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate len = di_prop_lookup_bytes(DDI_DEV_T_ANY, root_allnode, 5297c478bd9Sstevel@tonic-gate biospropname, (uchar_t **)&biosdata); 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate if (len <= 0) { 5327c478bd9Sstevel@tonic-gate continue; 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate if (debug) 5367c478bd9Sstevel@tonic-gate (void) printf("matching %s\n", biospropname); 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate matchedindex = match_edd(biosdata); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate if (matchedindex == -1) { 5417c478bd9Sstevel@tonic-gate matchedindex = match_first_block(biosdata); 5427c478bd9Sstevel@tonic-gate if (debug && matchedindex != -1) 5437c478bd9Sstevel@tonic-gate (void) printf("matched first block\n"); 5447c478bd9Sstevel@tonic-gate } else if (debug) 5457c478bd9Sstevel@tonic-gate (void) printf("matched thru edd\n"); 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate if (matchedindex != -1) { 5487c478bd9Sstevel@tonic-gate mapinfo[i].disklist_index = matchedindex; 5497c478bd9Sstevel@tonic-gate mapinfo[i].matchcount++; 5507c478bd9Sstevel@tonic-gate if (debug) 5517c478bd9Sstevel@tonic-gate (void) printf("0x%x %s\n", i + STARTING_DRVNUM, 5527c478bd9Sstevel@tonic-gate disk_list[matchedindex]); 5537c478bd9Sstevel@tonic-gate for (j = 0; j < i; j++) { 5547c478bd9Sstevel@tonic-gate if (mapinfo[j].matchcount > 0 && 5557c478bd9Sstevel@tonic-gate mapinfo[j].disklist_index == matchedindex) { 5567c478bd9Sstevel@tonic-gate mapinfo[j].matchcount++; 5577c478bd9Sstevel@tonic-gate mapinfo[i].matchcount++; 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate } 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate } else if (debug) 5627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Could not match %s\n", 5637c478bd9Sstevel@tonic-gate biospropname); 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate for (i = 0; i < BIOSDEV_NUM; i++) { 5677c478bd9Sstevel@tonic-gate if (mapinfo[i].matchcount == 1) { 5687c478bd9Sstevel@tonic-gate (void) printf("0x%x %s\n", i + STARTING_DRVNUM, 5697c478bd9Sstevel@tonic-gate disk_list[mapinfo[i].disklist_index]); 5707c478bd9Sstevel@tonic-gate totalmatches++; 5717c478bd9Sstevel@tonic-gate } else if (debug && mapinfo[i].matchcount > 1) { 5727c478bd9Sstevel@tonic-gate (void) printf("0x%x %s matchcount %d\n", 5737c478bd9Sstevel@tonic-gate i + STARTING_DRVNUM, 5747c478bd9Sstevel@tonic-gate disk_list[mapinfo[i].disklist_index], 5757c478bd9Sstevel@tonic-gate mapinfo[i].matchcount); 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate if (totalmatches == 0) { 5807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "biosdev: Could not match any!!\n"); 5817c478bd9Sstevel@tonic-gate cleanup_and_exit(1); 5827c478bd9Sstevel@tonic-gate } 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate cleanup_and_exit(0); 5857c478bd9Sstevel@tonic-gate /* NOTREACHED */ 5867c478bd9Sstevel@tonic-gate return (0); 5877c478bd9Sstevel@tonic-gate } 588