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 518c2aff7Sartem * Common Development and Distribution License (the "License"). 618c2aff7Sartem * 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*42f64fdbSSarah Jelinek * Copyright 2010 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 <fcntl.h> 277c478bd9Sstevel@tonic-gate #include <libdevinfo.h> 287c478bd9Sstevel@tonic-gate #include <stdio.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <sys/stat.h> 327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 34681d9761SEric Taylor #include <sys/mkdev.h> 357c478bd9Sstevel@tonic-gate #include <ctype.h> 367c478bd9Sstevel@tonic-gate #include <libgen.h> 377c478bd9Sstevel@tonic-gate #include <unistd.h> 387c478bd9Sstevel@tonic-gate #include <devid.h> 39e7cbe64fSgw #include <sys/fs/zfs.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include "libdiskmgt.h" 427c478bd9Sstevel@tonic-gate #include "disks_private.h" 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #define CLUSTER_DEV "did" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* specify which disk links to use in the /dev directory */ 477c478bd9Sstevel@tonic-gate #define DEVLINK_REGEX "rdsk/.*" 487c478bd9Sstevel@tonic-gate #define DEVLINK_FLOPPY_REGEX "rdiskette[0-9]" 497c478bd9Sstevel@tonic-gate #define DEVLINK_DID_REGEX "did/rdsk/.*" 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define FLOPPY_NAME "rdiskette" 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #define MAXPROPLEN 1024 547c478bd9Sstevel@tonic-gate #define DEVICE_ID_PROP "devid" 557c478bd9Sstevel@tonic-gate #define PROD_ID_PROP "inquiry-product-id" 567c478bd9Sstevel@tonic-gate #define PROD_ID_USB_PROP "usb-product-name" 577c478bd9Sstevel@tonic-gate #define REMOVABLE_PROP "removable-media" 580167b58cScg #define HOTPLUGGABLE_PROP "hotpluggable" 597c478bd9Sstevel@tonic-gate #define SCSI_OPTIONS_PROP "scsi-options" 607c478bd9Sstevel@tonic-gate #define VENDOR_ID_PROP "inquiry-vendor-id" 617c478bd9Sstevel@tonic-gate #define VENDOR_ID_USB_PROP "usb-vendor-name" 627c478bd9Sstevel@tonic-gate #define WWN_PROP "node-wwn" 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static char *ctrltypes[] = { 657c478bd9Sstevel@tonic-gate DDI_NT_SCSI_NEXUS, 667c478bd9Sstevel@tonic-gate DDI_NT_SCSI_ATTACHMENT_POINT, 677c478bd9Sstevel@tonic-gate DDI_NT_FC_ATTACHMENT_POINT, 687c478bd9Sstevel@tonic-gate NULL 697c478bd9Sstevel@tonic-gate }; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate static char *bustypes[] = { 727c478bd9Sstevel@tonic-gate "sbus", 737c478bd9Sstevel@tonic-gate "pci", 747c478bd9Sstevel@tonic-gate "usb", 757c478bd9Sstevel@tonic-gate NULL 767c478bd9Sstevel@tonic-gate }; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate static bus_t *add_bus(struct search_args *args, di_node_t node, 797c478bd9Sstevel@tonic-gate di_minor_t minor, controller_t *cp); 807c478bd9Sstevel@tonic-gate static int add_cluster_devs(di_node_t node, di_minor_t minor, 817c478bd9Sstevel@tonic-gate void *arg); 827c478bd9Sstevel@tonic-gate static controller_t *add_controller(struct search_args *args, 837c478bd9Sstevel@tonic-gate di_node_t node, di_minor_t minor); 847c478bd9Sstevel@tonic-gate static int add_devpath(di_devlink_t devlink, void *arg); 857c478bd9Sstevel@tonic-gate static int add_devs(di_node_t node, di_minor_t minor, void *arg); 867c478bd9Sstevel@tonic-gate static int add_disk2controller(disk_t *diskp, 877c478bd9Sstevel@tonic-gate struct search_args *args); 887c478bd9Sstevel@tonic-gate static int add_disk2path(disk_t *dp, path_t *pp, 897c478bd9Sstevel@tonic-gate di_path_state_t st, char *wwn); 907c478bd9Sstevel@tonic-gate static int add_int2array(int p, int **parray); 917c478bd9Sstevel@tonic-gate static int add_ptr2array(void *p, void ***parray); 927c478bd9Sstevel@tonic-gate static char *bus_type(di_node_t node, di_minor_t minor, 937c478bd9Sstevel@tonic-gate di_prom_handle_t ph); 94*42f64fdbSSarah Jelinek static void remove_controller(controller_t *cp, 957c478bd9Sstevel@tonic-gate controller_t *currp); 967c478bd9Sstevel@tonic-gate static void clean_paths(struct search_args *args); 977c478bd9Sstevel@tonic-gate static disk_t *create_disk(char *deviceid, char *kernel_name, 987c478bd9Sstevel@tonic-gate struct search_args *args); 997c478bd9Sstevel@tonic-gate static char *ctype(di_node_t node, di_minor_t minor); 10097ab9a43SJohn Levon static boolean_t disk_is_cdrom(const char *type); 1017c478bd9Sstevel@tonic-gate static alias_t *find_alias(disk_t *diskp, char *kernel_name); 1027c478bd9Sstevel@tonic-gate static bus_t *find_bus(struct search_args *args, char *name); 1037c478bd9Sstevel@tonic-gate static controller_t *find_controller(struct search_args *args, char *name); 1047c478bd9Sstevel@tonic-gate static int fix_cluster_devpath(di_devlink_t devlink, void *arg); 1057c478bd9Sstevel@tonic-gate static disk_t *get_disk_by_deviceid(disk_t *listp, char *devid); 1067c478bd9Sstevel@tonic-gate static void get_disk_name_from_path(char *path, char *name, 1077c478bd9Sstevel@tonic-gate int size); 1087c478bd9Sstevel@tonic-gate static char *get_byte_prop(char *prop_name, di_node_t node); 1097c478bd9Sstevel@tonic-gate static di_node_t get_parent_bus(di_node_t node, 1107c478bd9Sstevel@tonic-gate struct search_args *args); 1117c478bd9Sstevel@tonic-gate static int get_prom_int(char *prop_name, di_node_t node, 1127c478bd9Sstevel@tonic-gate di_prom_handle_t ph); 1137c478bd9Sstevel@tonic-gate static char *get_prom_str(char *prop_name, di_node_t node, 1147c478bd9Sstevel@tonic-gate di_prom_handle_t ph); 1157c478bd9Sstevel@tonic-gate static int get_prop(char *prop_name, di_node_t node); 1167c478bd9Sstevel@tonic-gate static char *get_str_prop(char *prop_name, di_node_t node); 1177c478bd9Sstevel@tonic-gate static int have_disk(struct search_args *args, char *devid, 1187c478bd9Sstevel@tonic-gate char *kernel_name, disk_t **diskp); 1197c478bd9Sstevel@tonic-gate static int is_cluster_disk(di_node_t node, di_minor_t minor); 1207c478bd9Sstevel@tonic-gate static int is_ctds(char *name); 1217c478bd9Sstevel@tonic-gate static int is_drive(di_minor_t minor); 122e7cbe64fSgw static int is_zvol(di_node_t node, di_minor_t minor); 1237c478bd9Sstevel@tonic-gate static int is_HBA(di_node_t node, di_minor_t minor); 1247c478bd9Sstevel@tonic-gate static int new_alias(disk_t *diskp, char *kernel_path, 1257c478bd9Sstevel@tonic-gate char *devlink_path, struct search_args *args); 1267c478bd9Sstevel@tonic-gate static int new_devpath(alias_t *ap, char *devpath); 1277c478bd9Sstevel@tonic-gate static path_t *new_path(controller_t *cp, disk_t *diskp, 1287c478bd9Sstevel@tonic-gate di_node_t node, di_path_state_t st, char *wwn); 1297c478bd9Sstevel@tonic-gate static void remove_invalid_controller(char *name, 1307c478bd9Sstevel@tonic-gate controller_t *currp, struct search_args *args); 1317c478bd9Sstevel@tonic-gate static char *str_case_index(register char *s1, register char *s2); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate /* 1347c478bd9Sstevel@tonic-gate * The functions in this file do a dev tree walk to build up a model of the 1357c478bd9Sstevel@tonic-gate * disks, controllers and paths on the system. This model is returned in the 1367c478bd9Sstevel@tonic-gate * args->disk_listp and args->controller_listp members of the args param. 1377c478bd9Sstevel@tonic-gate * There is no global data for this file so it is thread safe. It is up to 1387c478bd9Sstevel@tonic-gate * the caller to merge the resulting model with any existing model that is 1397c478bd9Sstevel@tonic-gate * cached. The caller must also free the memory for this model when it is 1407c478bd9Sstevel@tonic-gate * no longer needed. 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate void 1437c478bd9Sstevel@tonic-gate findevs(struct search_args *args) 1447c478bd9Sstevel@tonic-gate { 1457c478bd9Sstevel@tonic-gate uint_t flags; 1467c478bd9Sstevel@tonic-gate di_node_t di_root; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate args->dev_walk_status = 0; 1497c478bd9Sstevel@tonic-gate args->disk_listp = NULL; 1507c478bd9Sstevel@tonic-gate args->controller_listp = NULL; 1517c478bd9Sstevel@tonic-gate args->bus_listp = NULL; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate args->handle = di_devlink_init(NULL, 0); 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate /* 1567c478bd9Sstevel@tonic-gate * Have to make several passes at this with the new devfs caching. 1577c478bd9Sstevel@tonic-gate * First, we find non-mpxio devices. Then we find mpxio/multipath 1587c478bd9Sstevel@tonic-gate * devices. Finally, we get cluster devices. 1597c478bd9Sstevel@tonic-gate */ 1607c478bd9Sstevel@tonic-gate flags = DINFOCACHE; 1617c478bd9Sstevel@tonic-gate di_root = di_init("/", flags); 1627c478bd9Sstevel@tonic-gate args->ph = di_prom_init(); 1637c478bd9Sstevel@tonic-gate (void) di_walk_minor(di_root, NULL, 0, args, add_devs); 1647c478bd9Sstevel@tonic-gate di_fini(di_root); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate flags = DINFOCPYALL | DINFOPATH; 1677c478bd9Sstevel@tonic-gate di_root = di_init("/", flags); 1687c478bd9Sstevel@tonic-gate (void) di_walk_minor(di_root, NULL, 0, args, add_devs); 1697c478bd9Sstevel@tonic-gate di_fini(di_root); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* do another pass to clean up cluster devpaths */ 1727c478bd9Sstevel@tonic-gate flags = DINFOCACHE; 1737c478bd9Sstevel@tonic-gate di_root = di_init("/", flags); 1747c478bd9Sstevel@tonic-gate (void) di_walk_minor(di_root, DDI_PSEUDO, 0, args, add_cluster_devs); 1757c478bd9Sstevel@tonic-gate if (args->ph != DI_PROM_HANDLE_NIL) { 176*42f64fdbSSarah Jelinek (void) di_prom_fini(args->ph); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate di_fini(di_root); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate (void) di_devlink_fini(&(args->handle)); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate clean_paths(args); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate /* 1867c478bd9Sstevel@tonic-gate * Definitions of private functions 1877c478bd9Sstevel@tonic-gate */ 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate static bus_t * 1907c478bd9Sstevel@tonic-gate add_bus(struct search_args *args, di_node_t node, di_minor_t minor, 1917c478bd9Sstevel@tonic-gate controller_t *cp) 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate char *btype; 1947c478bd9Sstevel@tonic-gate char *devpath; 1957c478bd9Sstevel@tonic-gate bus_t *bp; 1967c478bd9Sstevel@tonic-gate char kstat_name[MAXPATHLEN]; 1977c478bd9Sstevel@tonic-gate di_node_t pnode; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate if (node == DI_NODE_NIL) { 200*42f64fdbSSarah Jelinek return (NULL); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate if ((btype = bus_type(node, minor, args->ph)) == NULL) { 204*42f64fdbSSarah Jelinek return (add_bus(args, di_parent_node(node), 205*42f64fdbSSarah Jelinek di_minor_next(di_parent_node(node), NULL), cp)); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate devpath = di_devfs_path(node); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if ((bp = find_bus(args, devpath)) != NULL) { 211*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 2127c478bd9Sstevel@tonic-gate 213*42f64fdbSSarah Jelinek if (cp != NULL) { 214*42f64fdbSSarah Jelinek if (add_ptr2array(cp, 215*42f64fdbSSarah Jelinek (void ***)&bp->controllers) != 0) { 216*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 217*42f64fdbSSarah Jelinek return (NULL); 218*42f64fdbSSarah Jelinek } 2197c478bd9Sstevel@tonic-gate } 220*42f64fdbSSarah Jelinek return (bp); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* Special handling for root node. */ 2247c478bd9Sstevel@tonic-gate if (strcmp(devpath, "/") == 0) { 225*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 226*42f64fdbSSarah Jelinek return (NULL); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate if (dm_debug) { 230*42f64fdbSSarah Jelinek (void) fprintf(stderr, "INFO: add_bus %s\n", devpath); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate bp = (bus_t *)calloc(1, sizeof (bus_t)); 2347c478bd9Sstevel@tonic-gate if (bp == NULL) { 235*42f64fdbSSarah Jelinek return (NULL); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate bp->name = strdup(devpath); 2397c478bd9Sstevel@tonic-gate di_devfs_path_free((void *) devpath); 2407c478bd9Sstevel@tonic-gate if (bp->name == NULL) { 241*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 242*42f64fdbSSarah Jelinek cache_free_bus(bp); 243*42f64fdbSSarah Jelinek return (NULL); 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate bp->btype = strdup(btype); 2477c478bd9Sstevel@tonic-gate if (bp->btype == NULL) { 248*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 249*42f64fdbSSarah Jelinek cache_free_bus(bp); 250*42f64fdbSSarah Jelinek return (NULL); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate (void) snprintf(kstat_name, sizeof (kstat_name), "%s%d", 2547c478bd9Sstevel@tonic-gate di_node_name(node), di_instance(node)); 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if ((bp->kstat_name = strdup(kstat_name)) == NULL) { 257*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 258*42f64fdbSSarah Jelinek cache_free_bus(bp); 259*42f64fdbSSarah Jelinek return (NULL); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate /* if parent node is a bus, get its name */ 2637c478bd9Sstevel@tonic-gate if ((pnode = get_parent_bus(node, args)) != NULL) { 264*42f64fdbSSarah Jelinek devpath = di_devfs_path(pnode); 265*42f64fdbSSarah Jelinek bp->pname = strdup(devpath); 266*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 267*42f64fdbSSarah Jelinek if (bp->pname == NULL) { 268*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 269*42f64fdbSSarah Jelinek cache_free_bus(bp); 270*42f64fdbSSarah Jelinek return (NULL); 271*42f64fdbSSarah Jelinek } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate } else { 274*42f64fdbSSarah Jelinek bp->pname = NULL; 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate bp->freq = get_prom_int("clock-frequency", node, args->ph); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate bp->controllers = (controller_t **)calloc(1, sizeof (controller_t *)); 2807c478bd9Sstevel@tonic-gate if (bp->controllers == NULL) { 281*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 282*42f64fdbSSarah Jelinek cache_free_bus(bp); 283*42f64fdbSSarah Jelinek return (NULL); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate bp->controllers[0] = NULL; 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate if (cp != NULL) { 288*42f64fdbSSarah Jelinek if (add_ptr2array(cp, (void ***)&bp->controllers) != 0) { 289*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 290*42f64fdbSSarah Jelinek return (NULL); 291*42f64fdbSSarah Jelinek } 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate bp->next = args->bus_listp; 2957c478bd9Sstevel@tonic-gate args->bus_listp = bp; 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate return (bp); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate static int 3017c478bd9Sstevel@tonic-gate add_cluster_devs(di_node_t node, di_minor_t minor, void *arg) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate struct search_args *args; 3047c478bd9Sstevel@tonic-gate char *devpath; 3057c478bd9Sstevel@tonic-gate char slice_path[MAXPATHLEN]; 3067c478bd9Sstevel@tonic-gate int result = DI_WALK_CONTINUE; 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate if (!is_cluster_disk(node, minor)) { 309*42f64fdbSSarah Jelinek return (DI_WALK_CONTINUE); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate args = (struct search_args *)arg; 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate if (dm_debug > 1) { 315*42f64fdbSSarah Jelinek /* This is all just debugging code */ 316*42f64fdbSSarah Jelinek char *devpath; 317*42f64fdbSSarah Jelinek char dev_name[MAXPATHLEN]; 3187c478bd9Sstevel@tonic-gate 319*42f64fdbSSarah Jelinek devpath = di_devfs_path(node); 320*42f64fdbSSarah Jelinek (void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath, 321*42f64fdbSSarah Jelinek di_minor_name(minor)); 322*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 3237c478bd9Sstevel@tonic-gate 324*42f64fdbSSarah Jelinek (void) fprintf(stderr, "INFO: cluster dev: %s\n", dev_name); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate args->node = node; 3287c478bd9Sstevel@tonic-gate args->minor = minor; 3297c478bd9Sstevel@tonic-gate args->dev_walk_status = 0; 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate /* 3327c478bd9Sstevel@tonic-gate * Fix the devpaths for the cluster drive. 3337c478bd9Sstevel@tonic-gate * 3347c478bd9Sstevel@tonic-gate * We will come through here once for each raw slice device name. 3357c478bd9Sstevel@tonic-gate */ 3367c478bd9Sstevel@tonic-gate devpath = di_devfs_path(node); 3377c478bd9Sstevel@tonic-gate (void) snprintf(slice_path, sizeof (slice_path), "%s:%s", devpath, 3387c478bd9Sstevel@tonic-gate di_minor_name(minor)); 3397c478bd9Sstevel@tonic-gate di_devfs_path_free((void *) devpath); 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate /* Walk the /dev tree to get the cluster devlinks. */ 3427c478bd9Sstevel@tonic-gate (void) di_devlink_walk(args->handle, DEVLINK_DID_REGEX, slice_path, 3437c478bd9Sstevel@tonic-gate DI_PRIMARY_LINK, arg, fix_cluster_devpath); 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate if (args->dev_walk_status != 0) { 346*42f64fdbSSarah Jelinek result = DI_WALK_TERMINATE; 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate return (result); 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate static controller_t * 3537c478bd9Sstevel@tonic-gate add_controller(struct search_args *args, di_node_t node, di_minor_t minor) 3547c478bd9Sstevel@tonic-gate { 3557c478bd9Sstevel@tonic-gate char *devpath; 3567c478bd9Sstevel@tonic-gate controller_t *cp; 3577c478bd9Sstevel@tonic-gate char kstat_name[MAXPATHLEN]; 3587c478bd9Sstevel@tonic-gate char *c_type = DM_CTYPE_UNKNOWN; 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate devpath = di_devfs_path(node); 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate if ((cp = find_controller(args, devpath)) != NULL) { 363*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 364*42f64fdbSSarah Jelinek return (cp); 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate /* Special handling for fp attachment node. */ 3687c478bd9Sstevel@tonic-gate if (strcmp(di_node_name(node), "fp") == 0) { 369*42f64fdbSSarah Jelinek di_node_t pnode; 3707c478bd9Sstevel@tonic-gate 371*42f64fdbSSarah Jelinek pnode = di_parent_node(node); 372*42f64fdbSSarah Jelinek if (pnode != DI_NODE_NIL) { 373*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 374*42f64fdbSSarah Jelinek devpath = di_devfs_path(pnode); 3757c478bd9Sstevel@tonic-gate 376*42f64fdbSSarah Jelinek if ((cp = find_controller(args, devpath)) != NULL) { 377*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 378*42f64fdbSSarah Jelinek return (cp); 379*42f64fdbSSarah Jelinek } 3807c478bd9Sstevel@tonic-gate 381*42f64fdbSSarah Jelinek /* not in the list, create it */ 382*42f64fdbSSarah Jelinek node = pnode; 383*42f64fdbSSarah Jelinek c_type = DM_CTYPE_FIBRE; 384*42f64fdbSSarah Jelinek } 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate if (dm_debug) { 388*42f64fdbSSarah Jelinek (void) fprintf(stderr, "INFO: add_controller %s\n", devpath); 3897c478bd9Sstevel@tonic-gate } 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate cp = (controller_t *)calloc(1, sizeof (controller_t)); 3927c478bd9Sstevel@tonic-gate if (cp == NULL) { 393*42f64fdbSSarah Jelinek return (NULL); 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate cp->name = strdup(devpath); 3977c478bd9Sstevel@tonic-gate di_devfs_path_free((void *) devpath); 3987c478bd9Sstevel@tonic-gate if (cp->name == NULL) { 399*42f64fdbSSarah Jelinek cache_free_controller(cp); 400*42f64fdbSSarah Jelinek return (NULL); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate if (strcmp(c_type, DM_CTYPE_UNKNOWN) == 0) { 404*42f64fdbSSarah Jelinek c_type = ctype(node, minor); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate cp->ctype = c_type; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate (void) snprintf(kstat_name, sizeof (kstat_name), "%s%d", 4097c478bd9Sstevel@tonic-gate di_node_name(node), di_instance(node)); 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate if ((cp->kstat_name = strdup(kstat_name)) == NULL) { 412*42f64fdbSSarah Jelinek cache_free_controller(cp); 413*42f64fdbSSarah Jelinek return (NULL); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(cp->ctype, "scsi")) { 417*42f64fdbSSarah Jelinek cp->scsi_options = get_prop(SCSI_OPTIONS_PROP, node); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(di_node_name(node), "scsi_vhci")) { 421*42f64fdbSSarah Jelinek cp->multiplex = 1; 4227c478bd9Sstevel@tonic-gate } else { 423*42f64fdbSSarah Jelinek cp->multiplex = 0; 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate cp->freq = get_prom_int("clock-frequency", node, args->ph); 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate cp->disks = (disk_t **)calloc(1, sizeof (disk_t *)); 4297c478bd9Sstevel@tonic-gate if (cp->disks == NULL) { 430*42f64fdbSSarah Jelinek cache_free_controller(cp); 431*42f64fdbSSarah Jelinek return (NULL); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate cp->disks[0] = NULL; 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate cp->next = args->controller_listp; 4367c478bd9Sstevel@tonic-gate args->controller_listp = cp; 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate cp->bus = add_bus(args, di_parent_node(node), 4397c478bd9Sstevel@tonic-gate di_minor_next(di_parent_node(node), NULL), cp); 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate return (cp); 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate static int 4457c478bd9Sstevel@tonic-gate add_devpath(di_devlink_t devlink, void *arg) 4467c478bd9Sstevel@tonic-gate { 4477c478bd9Sstevel@tonic-gate struct search_args *args; 4487c478bd9Sstevel@tonic-gate char *devidstr; 4497c478bd9Sstevel@tonic-gate disk_t *diskp; 4507c478bd9Sstevel@tonic-gate char kernel_name[MAXPATHLEN]; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate args = (struct search_args *)arg; 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate /* 4557c478bd9Sstevel@tonic-gate * Get the diskp value from calling have_disk. Can either be found 4567c478bd9Sstevel@tonic-gate * by kernel name or devid. 4577c478bd9Sstevel@tonic-gate */ 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate diskp = NULL; 4607c478bd9Sstevel@tonic-gate devidstr = get_str_prop(DEVICE_ID_PROP, args->node); 4617c478bd9Sstevel@tonic-gate (void) snprintf(kernel_name, sizeof (kernel_name), "%s%d", 4627c478bd9Sstevel@tonic-gate di_node_name(args->node), di_instance(args->node)); 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate (void) have_disk(args, devidstr, kernel_name, &diskp); 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate /* 4677c478bd9Sstevel@tonic-gate * The devlink_path is usually of the form /dev/rdsk/c0t0d0s0. 4687c478bd9Sstevel@tonic-gate * For diskettes it is /dev/rdiskette*. 4697c478bd9Sstevel@tonic-gate * On Intel we would also get each fdisk partition as well 4707c478bd9Sstevel@tonic-gate * (e.g. /dev/rdsk/c0t0d0p0). 4717c478bd9Sstevel@tonic-gate */ 4727c478bd9Sstevel@tonic-gate if (diskp != NULL) { 473*42f64fdbSSarah Jelinek alias_t *ap; 474*42f64fdbSSarah Jelinek char *devlink_path; 4757c478bd9Sstevel@tonic-gate 476*42f64fdbSSarah Jelinek if (diskp->drv_type != DM_DT_FLOPPY) { 477*42f64fdbSSarah Jelinek /* 478*42f64fdbSSarah Jelinek * Add other controllers for multipath disks. 479*42f64fdbSSarah Jelinek * This will have no effect if the controller 480*42f64fdbSSarah Jelinek * relationship is already set up. 481*42f64fdbSSarah Jelinek */ 482*42f64fdbSSarah Jelinek if (add_disk2controller(diskp, args) != 0) { 483*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 484*42f64fdbSSarah Jelinek } 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate 487*42f64fdbSSarah Jelinek (void) snprintf(kernel_name, sizeof (kernel_name), "%s%d", 488*42f64fdbSSarah Jelinek di_node_name(args->node), di_instance(args->node)); 489*42f64fdbSSarah Jelinek devlink_path = (char *)di_devlink_path(devlink); 4907c478bd9Sstevel@tonic-gate 491*42f64fdbSSarah Jelinek if (dm_debug > 1) { 492*42f64fdbSSarah Jelinek (void) fprintf(stderr, 493*42f64fdbSSarah Jelinek "INFO: devpath %s\n", devlink_path); 4947c478bd9Sstevel@tonic-gate } 495*42f64fdbSSarah Jelinek 496*42f64fdbSSarah Jelinek if ((ap = find_alias(diskp, kernel_name)) == NULL) { 497*42f64fdbSSarah Jelinek if (new_alias(diskp, kernel_name, devlink_path, 498*42f64fdbSSarah Jelinek args) != 0) { 499*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 500*42f64fdbSSarah Jelinek } 501*42f64fdbSSarah Jelinek } else { 502*42f64fdbSSarah Jelinek /* 503*42f64fdbSSarah Jelinek * It is possible that we have already added this 504*42f64fdbSSarah Jelinek * devpath. Do not add it again. new_devpath will 505*42f64fdbSSarah Jelinek * return a 0 if found, and not add the path. 506*42f64fdbSSarah Jelinek */ 507*42f64fdbSSarah Jelinek if (new_devpath(ap, devlink_path) != 0) { 508*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 509*42f64fdbSSarah Jelinek } 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate static int 5177c478bd9Sstevel@tonic-gate add_devs(di_node_t node, di_minor_t minor, void *arg) 5187c478bd9Sstevel@tonic-gate { 5197c478bd9Sstevel@tonic-gate struct search_args *args; 5207c478bd9Sstevel@tonic-gate int result = DI_WALK_CONTINUE; 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate args = (struct search_args *)arg; 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate if (dm_debug > 1) { 525*42f64fdbSSarah Jelinek /* This is all just debugging code */ 526*42f64fdbSSarah Jelinek char *devpath; 527*42f64fdbSSarah Jelinek char dev_name[MAXPATHLEN]; 528*42f64fdbSSarah Jelinek 529*42f64fdbSSarah Jelinek devpath = di_devfs_path(node); 530*42f64fdbSSarah Jelinek (void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath, 531*42f64fdbSSarah Jelinek di_minor_name(minor)); 532*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 533*42f64fdbSSarah Jelinek 534*42f64fdbSSarah Jelinek (void) fprintf(stderr, 535*42f64fdbSSarah Jelinek "INFO: dev: %s, node: %s%d, minor: 0x%x, type: %s\n", 536*42f64fdbSSarah Jelinek dev_name, di_node_name(node), di_instance(node), 537*42f64fdbSSarah Jelinek di_minor_spectype(minor), 538*42f64fdbSSarah Jelinek (di_minor_nodetype(minor) != NULL ? 5397c478bd9Sstevel@tonic-gate di_minor_nodetype(minor) : "NULL")); 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate if (bus_type(node, minor, args->ph) != NULL) { 543*42f64fdbSSarah Jelinek if (add_bus(args, node, minor, NULL) == NULL) { 544*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 545*42f64fdbSSarah Jelinek result = DI_WALK_TERMINATE; 546*42f64fdbSSarah Jelinek } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate } else if (is_HBA(node, minor)) { 549*42f64fdbSSarah Jelinek if (add_controller(args, node, minor) == NULL) { 550*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 551*42f64fdbSSarah Jelinek result = DI_WALK_TERMINATE; 552*42f64fdbSSarah Jelinek } 5537c478bd9Sstevel@tonic-gate 554e7cbe64fSgw } else if (di_minor_spectype(minor) == S_IFCHR && 555*42f64fdbSSarah Jelinek (is_drive(minor) || is_zvol(node, minor))) { 556*42f64fdbSSarah Jelinek char *devidstr; 557*42f64fdbSSarah Jelinek char kernel_name[MAXPATHLEN]; 558*42f64fdbSSarah Jelinek disk_t *diskp; 5597c478bd9Sstevel@tonic-gate 560*42f64fdbSSarah Jelinek (void) snprintf(kernel_name, sizeof (kernel_name), "%s%d", 561*42f64fdbSSarah Jelinek di_node_name(node), di_instance(node)); 562*42f64fdbSSarah Jelinek devidstr = get_str_prop(DEVICE_ID_PROP, node); 563*42f64fdbSSarah Jelinek 564*42f64fdbSSarah Jelinek args->node = node; 565*42f64fdbSSarah Jelinek args->minor = minor; 566*42f64fdbSSarah Jelinek /* 567*42f64fdbSSarah Jelinek * Check if we already got this disk and 568*42f64fdbSSarah Jelinek * this is another slice. 569*42f64fdbSSarah Jelinek */ 570*42f64fdbSSarah Jelinek if (!have_disk(args, devidstr, kernel_name, &diskp)) { 571*42f64fdbSSarah Jelinek args->dev_walk_status = 0; 572*42f64fdbSSarah Jelinek /* 573*42f64fdbSSarah Jelinek * This is a newly found disk, create the 574*42f64fdbSSarah Jelinek * disk structure. 575*42f64fdbSSarah Jelinek */ 576*42f64fdbSSarah Jelinek diskp = create_disk(devidstr, kernel_name, args); 577*42f64fdbSSarah Jelinek if (diskp == NULL) { 578*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 579*42f64fdbSSarah Jelinek } 580*42f64fdbSSarah Jelinek 581*42f64fdbSSarah Jelinek if (diskp->drv_type != DM_DT_FLOPPY) { 582*42f64fdbSSarah Jelinek /* add the controller relationship */ 583*42f64fdbSSarah Jelinek if (args->dev_walk_status == 0) { 584*42f64fdbSSarah Jelinek if (add_disk2controller(diskp, 585*42f64fdbSSarah Jelinek args) != 0) { 586*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 587*42f64fdbSSarah Jelinek } 588*42f64fdbSSarah Jelinek } 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate } 591*42f64fdbSSarah Jelinek if (is_zvol(node, minor)) { 592*42f64fdbSSarah Jelinek char zvdsk[MAXNAMELEN]; 593*42f64fdbSSarah Jelinek char *str; 594*42f64fdbSSarah Jelinek alias_t *ap; 595*42f64fdbSSarah Jelinek 596*42f64fdbSSarah Jelinek if (di_prop_lookup_strings(di_minor_devt(minor), 597*42f64fdbSSarah Jelinek node, "name", &str) == -1) 598*42f64fdbSSarah Jelinek return (DI_WALK_CONTINUE); 599*42f64fdbSSarah Jelinek (void) snprintf(zvdsk, MAXNAMELEN, "/dev/zvol/rdsk/%s", 600*42f64fdbSSarah Jelinek str); 601*42f64fdbSSarah Jelinek if ((ap = find_alias(diskp, kernel_name)) == NULL) { 602*42f64fdbSSarah Jelinek if (new_alias(diskp, kernel_name, 603*42f64fdbSSarah Jelinek zvdsk, args) != 0) { 604*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 605*42f64fdbSSarah Jelinek } 606*42f64fdbSSarah Jelinek } else { 607*42f64fdbSSarah Jelinek /* 608*42f64fdbSSarah Jelinek * It is possible that we have already added 609*42f64fdbSSarah Jelinek * this devpath. 610*42f64fdbSSarah Jelinek * Do not add it again. new_devpath will 611*42f64fdbSSarah Jelinek * return a 0 if found, and not add the path. 612*42f64fdbSSarah Jelinek */ 613*42f64fdbSSarah Jelinek if (new_devpath(ap, zvdsk) != 0) { 614*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 615*42f64fdbSSarah Jelinek } 616*42f64fdbSSarah Jelinek } 617681d9761SEric Taylor } 6187c478bd9Sstevel@tonic-gate 619*42f64fdbSSarah Jelinek /* Add the devpaths for the drive. */ 620*42f64fdbSSarah Jelinek if (args->dev_walk_status == 0) { 621*42f64fdbSSarah Jelinek char *devpath; 622*42f64fdbSSarah Jelinek char slice_path[MAXPATHLEN]; 623*42f64fdbSSarah Jelinek char *pattern; 6247c478bd9Sstevel@tonic-gate 625*42f64fdbSSarah Jelinek /* 626*42f64fdbSSarah Jelinek * We will come through here once for each of 627*42f64fdbSSarah Jelinek * the raw slice device names. 628*42f64fdbSSarah Jelinek */ 629*42f64fdbSSarah Jelinek devpath = di_devfs_path(node); 630*42f64fdbSSarah Jelinek (void) snprintf(slice_path, 631*42f64fdbSSarah Jelinek sizeof (slice_path), "%s:%s", 632*42f64fdbSSarah Jelinek devpath, di_minor_name(minor)); 633*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 634*42f64fdbSSarah Jelinek 635*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(di_minor_nodetype(minor), 636*42f64fdbSSarah Jelinek DDI_NT_FD)) { 637*42f64fdbSSarah Jelinek pattern = DEVLINK_FLOPPY_REGEX; 638*42f64fdbSSarah Jelinek } else { 639*42f64fdbSSarah Jelinek pattern = DEVLINK_REGEX; 640*42f64fdbSSarah Jelinek } 6417c478bd9Sstevel@tonic-gate 642*42f64fdbSSarah Jelinek /* Walk the /dev tree to get the devlinks. */ 643*42f64fdbSSarah Jelinek (void) di_devlink_walk(args->handle, pattern, 644*42f64fdbSSarah Jelinek slice_path, DI_PRIMARY_LINK, arg, add_devpath); 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate 647*42f64fdbSSarah Jelinek if (args->dev_walk_status != 0) { 648*42f64fdbSSarah Jelinek result = DI_WALK_TERMINATE; 649*42f64fdbSSarah Jelinek } 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate return (result); 6537c478bd9Sstevel@tonic-gate } 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate static int 6567c478bd9Sstevel@tonic-gate add_disk2controller(disk_t *diskp, struct search_args *args) 6577c478bd9Sstevel@tonic-gate { 6587c478bd9Sstevel@tonic-gate di_node_t pnode; 6597c478bd9Sstevel@tonic-gate controller_t *cp; 6607c478bd9Sstevel@tonic-gate di_minor_t minor; 6617c478bd9Sstevel@tonic-gate di_node_t node; 6627c478bd9Sstevel@tonic-gate int i; 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate node = args->node; 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate pnode = di_parent_node(node); 6677c478bd9Sstevel@tonic-gate if (pnode == DI_NODE_NIL) { 668*42f64fdbSSarah Jelinek return (0); 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate minor = di_minor_next(pnode, NULL); 6727c478bd9Sstevel@tonic-gate if (minor == NULL) { 673*42f64fdbSSarah Jelinek return (0); 6747c478bd9Sstevel@tonic-gate } 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate if ((cp = add_controller(args, pnode, minor)) == NULL) { 677*42f64fdbSSarah Jelinek return (ENOMEM); 6787c478bd9Sstevel@tonic-gate } 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate /* check if the disk <-> ctrl assoc is already there */ 6817c478bd9Sstevel@tonic-gate for (i = 0; diskp->controllers[i]; i++) { 682*42f64fdbSSarah Jelinek if (cp == diskp->controllers[i]) { 683*42f64fdbSSarah Jelinek return (0); 684*42f64fdbSSarah Jelinek } 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate /* this is a new controller for this disk */ 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate /* add the disk to the controlller */ 6907c478bd9Sstevel@tonic-gate if (add_ptr2array(diskp, (void ***)&cp->disks) != 0) { 691*42f64fdbSSarah Jelinek return (ENOMEM); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate /* add the controlller to the disk */ 6957c478bd9Sstevel@tonic-gate if (add_ptr2array(cp, (void ***)&diskp->controllers) != 0) { 696*42f64fdbSSarah Jelinek return (ENOMEM); 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate /* 7007c478bd9Sstevel@tonic-gate * Set up paths for mpxio controlled drives. 7017c478bd9Sstevel@tonic-gate */ 7027c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(di_node_name(pnode), "scsi_vhci")) { 703*42f64fdbSSarah Jelinek /* note: mpxio di_path stuff is all consolidation private */ 704*42f64fdbSSarah Jelinek di_path_t pi = DI_PATH_NIL; 705*42f64fdbSSarah Jelinek 706*42f64fdbSSarah Jelinek while ( 707*42f64fdbSSarah Jelinek (pi = di_path_client_next_path(node, pi)) != DI_PATH_NIL) { 708*42f64fdbSSarah Jelinek int cnt; 709*42f64fdbSSarah Jelinek uchar_t *bytes; 710*42f64fdbSSarah Jelinek char str[MAXPATHLEN]; 711*42f64fdbSSarah Jelinek char *wwn; 712*42f64fdbSSarah Jelinek 713*42f64fdbSSarah Jelinek di_node_t phci_node = di_path_phci_node(pi); 714*42f64fdbSSarah Jelinek 715*42f64fdbSSarah Jelinek /* get the node wwn */ 716*42f64fdbSSarah Jelinek cnt = di_path_prop_lookup_bytes(pi, WWN_PROP, &bytes); 717*42f64fdbSSarah Jelinek wwn = NULL; 718*42f64fdbSSarah Jelinek if (cnt > 0) { 719*42f64fdbSSarah Jelinek int i; 720*42f64fdbSSarah Jelinek str[0] = 0; 721*42f64fdbSSarah Jelinek 722*42f64fdbSSarah Jelinek for (i = 0; i < cnt; i++) { 723*42f64fdbSSarah Jelinek /* 724*42f64fdbSSarah Jelinek * A byte is only 2 hex chars + null. 725*42f64fdbSSarah Jelinek */ 726*42f64fdbSSarah Jelinek char bstr[8]; 727*42f64fdbSSarah Jelinek 728*42f64fdbSSarah Jelinek (void) snprintf(bstr, 729*42f64fdbSSarah Jelinek sizeof (bstr), "%.2x", bytes[i]); 730*42f64fdbSSarah Jelinek (void) strlcat(str, bstr, sizeof (str)); 731*42f64fdbSSarah Jelinek } 732*42f64fdbSSarah Jelinek wwn = str; 733*42f64fdbSSarah Jelinek } 7347c478bd9Sstevel@tonic-gate 735*42f64fdbSSarah Jelinek if (new_path(cp, diskp, phci_node, 736*42f64fdbSSarah Jelinek di_path_state(pi), wwn) == NULL) { 737*42f64fdbSSarah Jelinek return (ENOMEM); 738*42f64fdbSSarah Jelinek } 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate } 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate return (0); 7437c478bd9Sstevel@tonic-gate } 7447c478bd9Sstevel@tonic-gate 7457c478bd9Sstevel@tonic-gate static int 7467c478bd9Sstevel@tonic-gate add_disk2path(disk_t *dp, path_t *pp, di_path_state_t st, char *wwn) 7477c478bd9Sstevel@tonic-gate { 7487c478bd9Sstevel@tonic-gate /* add the disk to the path */ 7497c478bd9Sstevel@tonic-gate if (add_ptr2array(dp, (void ***)&pp->disks) != 0) { 750*42f64fdbSSarah Jelinek cache_free_path(pp); 751*42f64fdbSSarah Jelinek return (0); 7527c478bd9Sstevel@tonic-gate } 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate /* add the path to the disk */ 7557c478bd9Sstevel@tonic-gate if (add_ptr2array(pp, (void ***)&dp->paths) != 0) { 756*42f64fdbSSarah Jelinek cache_free_path(pp); 757*42f64fdbSSarah Jelinek return (0); 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate 7607c478bd9Sstevel@tonic-gate /* add the path state for this disk */ 7617c478bd9Sstevel@tonic-gate if (add_int2array(st, &pp->states) != 0) { 762*42f64fdbSSarah Jelinek cache_free_path(pp); 763*42f64fdbSSarah Jelinek return (0); 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate /* add the path state for this disk */ 7677c478bd9Sstevel@tonic-gate if (wwn != NULL) { 768*42f64fdbSSarah Jelinek char *wp; 7697c478bd9Sstevel@tonic-gate 770*42f64fdbSSarah Jelinek if ((wp = strdup(wwn)) != NULL) { 771*42f64fdbSSarah Jelinek if (add_ptr2array(wp, (void ***)(&pp->wwns)) != 0) { 772*42f64fdbSSarah Jelinek cache_free_path(pp); 773*42f64fdbSSarah Jelinek return (0); 774*42f64fdbSSarah Jelinek } 7757c478bd9Sstevel@tonic-gate } 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate return (1); 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate 7817c478bd9Sstevel@tonic-gate static int 7827c478bd9Sstevel@tonic-gate add_int2array(int p, int **parray) 7837c478bd9Sstevel@tonic-gate { 7847c478bd9Sstevel@tonic-gate int i; 7857c478bd9Sstevel@tonic-gate int cnt; 7867c478bd9Sstevel@tonic-gate int *pa; 7877c478bd9Sstevel@tonic-gate int *new_array; 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate pa = *parray; 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate cnt = 0; 7927c478bd9Sstevel@tonic-gate if (pa != NULL) { 793*42f64fdbSSarah Jelinek for (; pa[cnt] != -1; cnt++) 794*42f64fdbSSarah Jelinek ; 7957c478bd9Sstevel@tonic-gate } 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate new_array = (int *)calloc(cnt + 2, sizeof (int *)); 7987c478bd9Sstevel@tonic-gate if (new_array == NULL) { 799*42f64fdbSSarah Jelinek return (ENOMEM); 8007c478bd9Sstevel@tonic-gate } 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate /* copy the existing array */ 8037c478bd9Sstevel@tonic-gate for (i = 0; i < cnt; i++) { 804*42f64fdbSSarah Jelinek new_array[i] = pa[i]; 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate new_array[i] = p; 8087c478bd9Sstevel@tonic-gate new_array[i + 1] = -1; 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate free(pa); 8117c478bd9Sstevel@tonic-gate *parray = new_array; 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate return (0); 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate static int 8177c478bd9Sstevel@tonic-gate add_ptr2array(void *p, void ***parray) 8187c478bd9Sstevel@tonic-gate { 8197c478bd9Sstevel@tonic-gate int i; 8207c478bd9Sstevel@tonic-gate int cnt; 8217c478bd9Sstevel@tonic-gate void **pa; 8227c478bd9Sstevel@tonic-gate void **new_array; 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate pa = *parray; 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate cnt = 0; 8277c478bd9Sstevel@tonic-gate if (pa != NULL) { 828*42f64fdbSSarah Jelinek for (; pa[cnt]; cnt++) 829*42f64fdbSSarah Jelinek ; 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate new_array = (void **)calloc(cnt + 2, sizeof (void *)); 8337c478bd9Sstevel@tonic-gate if (new_array == NULL) { 834*42f64fdbSSarah Jelinek return (ENOMEM); 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate /* copy the existing array */ 8387c478bd9Sstevel@tonic-gate for (i = 0; i < cnt; i++) { 839*42f64fdbSSarah Jelinek new_array[i] = pa[i]; 8407c478bd9Sstevel@tonic-gate } 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate new_array[i] = p; 8437c478bd9Sstevel@tonic-gate new_array[i + 1] = NULL; 8447c478bd9Sstevel@tonic-gate 8457c478bd9Sstevel@tonic-gate free(pa); 8467c478bd9Sstevel@tonic-gate *parray = new_array; 8477c478bd9Sstevel@tonic-gate 8487c478bd9Sstevel@tonic-gate return (0); 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate /* 852*42f64fdbSSarah Jelinek * This function checks to see if a controller has other associations 853*42f64fdbSSarah Jelinek * that may be valid. If we are calling this function, we have found that 854*42f64fdbSSarah Jelinek * a controller for an mpxio device is showing up independently of the 855*42f64fdbSSarah Jelinek * mpxio controller, noted as /scsi_vhci. This can happen with some FC 856*42f64fdbSSarah Jelinek * cards that have inbound management devices that show up as well, with 857*42f64fdbSSarah Jelinek * the real controller data associated. We do not want to display these 858*42f64fdbSSarah Jelinek * 'devices' as real devices in libdiskmgt. 8597c478bd9Sstevel@tonic-gate */ 860*42f64fdbSSarah Jelinek static void 861*42f64fdbSSarah Jelinek remove_controller(controller_t *cp, controller_t *currp) 8627c478bd9Sstevel@tonic-gate { 863*42f64fdbSSarah Jelinek disk_t *dp; 864*42f64fdbSSarah Jelinek int i; 8657c478bd9Sstevel@tonic-gate 866*42f64fdbSSarah Jelinek if (cp == currp) { 867*42f64fdbSSarah Jelinek if (dm_debug) { 868*42f64fdbSSarah Jelinek (void) fprintf(stderr, "ERROR: removing current" 869*42f64fdbSSarah Jelinek " controller\n"); 870*42f64fdbSSarah Jelinek } 871*42f64fdbSSarah Jelinek return; 872*42f64fdbSSarah Jelinek } 8737c478bd9Sstevel@tonic-gate 874*42f64fdbSSarah Jelinek if (cp->disks != NULL && cp->disks[0] != NULL) { 875*42f64fdbSSarah Jelinek if (dm_debug) { 876*42f64fdbSSarah Jelinek (void) fprintf(stderr, 877*42f64fdbSSarah Jelinek "INFO: removing inbound management controller" 878*42f64fdbSSarah Jelinek " with disk ptrs.\n"); 879*42f64fdbSSarah Jelinek } 880*42f64fdbSSarah Jelinek /* 881*42f64fdbSSarah Jelinek * loop through the disks and remove the reference to the 882*42f64fdbSSarah Jelinek * controller for this disk structure. The disk itself 883*42f64fdbSSarah Jelinek * is still a valid device, the controller being removed 884*42f64fdbSSarah Jelinek * is a 'path' so any disk that has a reference to it 885*42f64fdbSSarah Jelinek * as a controller needs to have this reference removed. 886*42f64fdbSSarah Jelinek */ 887*42f64fdbSSarah Jelinek dp = cp->disks[0]; 888*42f64fdbSSarah Jelinek while (dp != NULL) { 889*42f64fdbSSarah Jelinek for (i = 0; dp->controllers[i]; i++) { 890*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(dp->controllers[i]->name, 891*42f64fdbSSarah Jelinek cp->name)) { 892*42f64fdbSSarah Jelinek int j; 893*42f64fdbSSarah Jelinek 894*42f64fdbSSarah Jelinek for (j = i; dp->controllers[j]; j++) { 895*42f64fdbSSarah Jelinek dp->controllers[j] = 896*42f64fdbSSarah Jelinek dp->controllers[j + 1]; 897*42f64fdbSSarah Jelinek } 898*42f64fdbSSarah Jelinek } 899*42f64fdbSSarah Jelinek } 900*42f64fdbSSarah Jelinek dp = dp->next; 901*42f64fdbSSarah Jelinek } 9027c478bd9Sstevel@tonic-gate } 903*42f64fdbSSarah Jelinek /* 904*42f64fdbSSarah Jelinek * Paths are removed with the call to cache_free_controller() 905*42f64fdbSSarah Jelinek * below. 906*42f64fdbSSarah Jelinek */ 9077c478bd9Sstevel@tonic-gate 908*42f64fdbSSarah Jelinek if (cp->paths != NULL && cp->paths[0] != NULL) { 909*42f64fdbSSarah Jelinek if (dm_debug) { 910*42f64fdbSSarah Jelinek (void) fprintf(stderr, 911*42f64fdbSSarah Jelinek "INFO: removing inbound management controller" 912*42f64fdbSSarah Jelinek " with path ptrs. \n"); 913*42f64fdbSSarah Jelinek } 914*42f64fdbSSarah Jelinek } 915*42f64fdbSSarah Jelinek cache_free_controller(cp); 9167c478bd9Sstevel@tonic-gate } 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate /* 9197c478bd9Sstevel@tonic-gate * If we have a controller in the list that is really a path then we need to 9207c478bd9Sstevel@tonic-gate * take that controller out of the list since nodes that are paths are not 9217c478bd9Sstevel@tonic-gate * considered to be controllers. 9227c478bd9Sstevel@tonic-gate */ 9237c478bd9Sstevel@tonic-gate static void 9247c478bd9Sstevel@tonic-gate clean_paths(struct search_args *args) 9257c478bd9Sstevel@tonic-gate { 9267c478bd9Sstevel@tonic-gate controller_t *cp; 9277c478bd9Sstevel@tonic-gate 9287c478bd9Sstevel@tonic-gate cp = args->controller_listp; 9297c478bd9Sstevel@tonic-gate while (cp != NULL) { 930*42f64fdbSSarah Jelinek path_t **pp; 9317c478bd9Sstevel@tonic-gate 932*42f64fdbSSarah Jelinek pp = cp->paths; 933*42f64fdbSSarah Jelinek if (pp != NULL) { 934*42f64fdbSSarah Jelinek int i; 9357c478bd9Sstevel@tonic-gate 936*42f64fdbSSarah Jelinek for (i = 0; pp[i]; i++) { 937*42f64fdbSSarah Jelinek remove_invalid_controller(pp[i]->name, cp, 938*42f64fdbSSarah Jelinek args); 939*42f64fdbSSarah Jelinek } 9407c478bd9Sstevel@tonic-gate } 941*42f64fdbSSarah Jelinek cp = cp->next; 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate static disk_t * 9467c478bd9Sstevel@tonic-gate create_disk(char *deviceid, char *kernel_name, struct search_args *args) 9477c478bd9Sstevel@tonic-gate { 9487c478bd9Sstevel@tonic-gate disk_t *diskp; 9497c478bd9Sstevel@tonic-gate char *type; 9507c478bd9Sstevel@tonic-gate char *prod_id; 9517c478bd9Sstevel@tonic-gate char *vendor_id; 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate if (dm_debug) { 954*42f64fdbSSarah Jelinek (void) fprintf(stderr, "INFO: create_disk %s\n", kernel_name); 9557c478bd9Sstevel@tonic-gate } 9567c478bd9Sstevel@tonic-gate 9577c478bd9Sstevel@tonic-gate diskp = calloc(1, sizeof (disk_t)); 9587c478bd9Sstevel@tonic-gate if (diskp == NULL) { 959*42f64fdbSSarah Jelinek return (NULL); 9607c478bd9Sstevel@tonic-gate } 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate diskp->controllers = (controller_t **) 9637c478bd9Sstevel@tonic-gate calloc(1, sizeof (controller_t *)); 9647c478bd9Sstevel@tonic-gate if (diskp->controllers == NULL) { 965*42f64fdbSSarah Jelinek cache_free_disk(diskp); 966*42f64fdbSSarah Jelinek return (NULL); 9677c478bd9Sstevel@tonic-gate } 9687c478bd9Sstevel@tonic-gate diskp->controllers[0] = NULL; 9697c478bd9Sstevel@tonic-gate 9707c478bd9Sstevel@tonic-gate diskp->devid = NULL; 9717c478bd9Sstevel@tonic-gate if (deviceid != NULL) { 972*42f64fdbSSarah Jelinek if ((diskp->device_id = strdup(deviceid)) == NULL) { 973*42f64fdbSSarah Jelinek cache_free_disk(diskp); 974*42f64fdbSSarah Jelinek return (NULL); 975*42f64fdbSSarah Jelinek } 976*42f64fdbSSarah Jelinek (void) devid_str_decode(deviceid, &(diskp->devid), NULL); 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate if (kernel_name != NULL) { 980*42f64fdbSSarah Jelinek diskp->kernel_name = strdup(kernel_name); 981*42f64fdbSSarah Jelinek if (diskp->kernel_name == NULL) { 982*42f64fdbSSarah Jelinek cache_free_disk(diskp); 983*42f64fdbSSarah Jelinek return (NULL); 984*42f64fdbSSarah Jelinek } 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate diskp->paths = NULL; 9887c478bd9Sstevel@tonic-gate diskp->aliases = NULL; 9897c478bd9Sstevel@tonic-gate 9907c478bd9Sstevel@tonic-gate diskp->cd_rom = 0; 9917c478bd9Sstevel@tonic-gate diskp->rpm = 0; 9927c478bd9Sstevel@tonic-gate type = di_minor_nodetype(args->minor); 9937c478bd9Sstevel@tonic-gate 9947c478bd9Sstevel@tonic-gate prod_id = get_str_prop(PROD_ID_PROP, args->node); 9957c478bd9Sstevel@tonic-gate if (prod_id != NULL) { 9967c478bd9Sstevel@tonic-gate if ((diskp->product_id = strdup(prod_id)) == NULL) { 997*42f64fdbSSarah Jelinek cache_free_disk(diskp); 998*42f64fdbSSarah Jelinek return (NULL); 999*42f64fdbSSarah Jelinek } 1000*42f64fdbSSarah Jelinek } else { 1001*42f64fdbSSarah Jelinek prod_id = get_str_prop(PROD_ID_USB_PROP, args->node); 1002*42f64fdbSSarah Jelinek if (prod_id != NULL) { 1003*42f64fdbSSarah Jelinek if ((diskp->product_id = strdup(prod_id)) == NULL) { 1004*42f64fdbSSarah Jelinek cache_free_disk(diskp); 1005*42f64fdbSSarah Jelinek return (NULL); 1006*42f64fdbSSarah Jelinek } 10077c478bd9Sstevel@tonic-gate } 10087c478bd9Sstevel@tonic-gate } 10097c478bd9Sstevel@tonic-gate 10107c478bd9Sstevel@tonic-gate vendor_id = get_str_prop(VENDOR_ID_PROP, args->node); 10117c478bd9Sstevel@tonic-gate if (vendor_id != NULL) { 10127c478bd9Sstevel@tonic-gate if ((diskp->vendor_id = strdup(vendor_id)) == NULL) { 1013*42f64fdbSSarah Jelinek cache_free_disk(diskp); 1014*42f64fdbSSarah Jelinek return (NULL); 1015*42f64fdbSSarah Jelinek } 1016*42f64fdbSSarah Jelinek } else { 1017*42f64fdbSSarah Jelinek vendor_id = get_str_prop(VENDOR_ID_PROP, args->node); 1018*42f64fdbSSarah Jelinek if (vendor_id != NULL) { 1019*42f64fdbSSarah Jelinek if ((diskp->vendor_id = strdup(vendor_id)) == NULL) { 1020*42f64fdbSSarah Jelinek cache_free_disk(diskp); 1021*42f64fdbSSarah Jelinek return (NULL); 1022*42f64fdbSSarah Jelinek } 10237c478bd9Sstevel@tonic-gate } 10247c478bd9Sstevel@tonic-gate } 10257c478bd9Sstevel@tonic-gate 10267c478bd9Sstevel@tonic-gate /* 10277c478bd9Sstevel@tonic-gate * DVD, CD-ROM, CD-RW, MO, etc. are all reported as CD-ROMS. 10287c478bd9Sstevel@tonic-gate * We try to use uscsi later to determine the real type. 10297c478bd9Sstevel@tonic-gate * The cd_rom flag tells us that the kernel categorized the drive 10307c478bd9Sstevel@tonic-gate * as a CD-ROM. We leave the drv_type as UKNOWN for now. 10317c478bd9Sstevel@tonic-gate * The combination of the cd_rom flag being set with the drv_type of 10327c478bd9Sstevel@tonic-gate * unknown is what triggers the uscsi probe in drive.c. 10337c478bd9Sstevel@tonic-gate */ 10347c478bd9Sstevel@tonic-gate if (disk_is_cdrom(type)) { 1035*42f64fdbSSarah Jelinek diskp->drv_type = DM_DT_UNKNOWN; 1036*42f64fdbSSarah Jelinek diskp->cd_rom = 1; 1037*42f64fdbSSarah Jelinek diskp->removable = 1; 10387c478bd9Sstevel@tonic-gate } else if (libdiskmgt_str_eq(type, DDI_NT_FD)) { 1039*42f64fdbSSarah Jelinek diskp->drv_type = DM_DT_FLOPPY; 1040*42f64fdbSSarah Jelinek diskp->removable = 1; 10417c478bd9Sstevel@tonic-gate } else { 10420167b58cScg /* not a "CD-ROM" or Floppy */ 1043*42f64fdbSSarah Jelinek diskp->removable = get_prop(REMOVABLE_PROP, args->node); 10447c478bd9Sstevel@tonic-gate 1045*42f64fdbSSarah Jelinek if (diskp->removable == -1) { 1046*42f64fdbSSarah Jelinek diskp->removable = 0; 10473e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64) 1048*42f64fdbSSarah Jelinek /* 1049*42f64fdbSSarah Jelinek * x86 does not have removable property. 1050*42f64fdbSSarah Jelinek * Check for common removable drives, zip & jaz, 1051*42f64fdbSSarah Jelinek * and mark those correctly. 1052*42f64fdbSSarah Jelinek */ 1053*42f64fdbSSarah Jelinek if (vendor_id != NULL && prod_id != NULL) { 1054*42f64fdbSSarah Jelinek if (str_case_index(vendor_id, 1055*42f64fdbSSarah Jelinek "iomega") != NULL) { 1056*42f64fdbSSarah Jelinek if (str_case_index(prod_id, 1057*42f64fdbSSarah Jelinek "jaz") != NULL) { 1058*42f64fdbSSarah Jelinek diskp->removable = 1; 1059*42f64fdbSSarah Jelinek } else if (str_case_index(prod_id, 1060*42f64fdbSSarah Jelinek "zip") != NULL) { 1061*42f64fdbSSarah Jelinek diskp->removable = 1; 1062*42f64fdbSSarah Jelinek } 1063*42f64fdbSSarah Jelinek } 10647c478bd9Sstevel@tonic-gate } 10657c478bd9Sstevel@tonic-gate #endif 1066*42f64fdbSSarah Jelinek } 10677c478bd9Sstevel@tonic-gate 1068*42f64fdbSSarah Jelinek if (diskp->removable) { 1069*42f64fdbSSarah Jelinek /* 1070*42f64fdbSSarah Jelinek * For removable jaz or zip drives there is no way 1071*42f64fdbSSarah Jelinek * to get the drive type unless media is inserted,so 1072*42f64fdbSSarah Jelinek * we look at the product-id for a hint. 1073*42f64fdbSSarah Jelinek */ 1074*42f64fdbSSarah Jelinek diskp->drv_type = DM_DT_UNKNOWN; 1075*42f64fdbSSarah Jelinek 1076*42f64fdbSSarah Jelinek if (prod_id != NULL) { 1077*42f64fdbSSarah Jelinek if (str_case_index(prod_id, "jaz") != NULL) { 1078*42f64fdbSSarah Jelinek diskp->drv_type = DM_DT_JAZ; 1079*42f64fdbSSarah Jelinek } else if (str_case_index(prod_id, 1080*42f64fdbSSarah Jelinek "zip") != NULL) { 1081*42f64fdbSSarah Jelinek diskp->drv_type = DM_DT_ZIP; 1082*42f64fdbSSarah Jelinek } 1083*42f64fdbSSarah Jelinek } 1084*42f64fdbSSarah Jelinek } else { 1085*42f64fdbSSarah Jelinek diskp->drv_type = DM_DT_FIXED; 10867c478bd9Sstevel@tonic-gate } 10877c478bd9Sstevel@tonic-gate } 10887c478bd9Sstevel@tonic-gate 10897c478bd9Sstevel@tonic-gate diskp->next = args->disk_listp; 10907c478bd9Sstevel@tonic-gate args->disk_listp = diskp; 10917c478bd9Sstevel@tonic-gate 10927c478bd9Sstevel@tonic-gate return (diskp); 10937c478bd9Sstevel@tonic-gate } 10947c478bd9Sstevel@tonic-gate 10957c478bd9Sstevel@tonic-gate static char * 10967c478bd9Sstevel@tonic-gate ctype(di_node_t node, di_minor_t minor) 10977c478bd9Sstevel@tonic-gate { 10987c478bd9Sstevel@tonic-gate char *type; 10997c478bd9Sstevel@tonic-gate char *name; 11007c478bd9Sstevel@tonic-gate 11017c478bd9Sstevel@tonic-gate type = di_minor_nodetype(minor); 11027c478bd9Sstevel@tonic-gate name = di_node_name(node); 11037c478bd9Sstevel@tonic-gate 11047c478bd9Sstevel@tonic-gate /* IDE disks use SCSI nexus as the type, so handle this special case */ 11057c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(name, "ide")) { 1106*42f64fdbSSarah Jelinek return (DM_CTYPE_ATA); 11077c478bd9Sstevel@tonic-gate } 11087c478bd9Sstevel@tonic-gate 11097c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(di_minor_name(minor), "scsa2usb")) { 1110*42f64fdbSSarah Jelinek return (DM_CTYPE_USB); 11117c478bd9Sstevel@tonic-gate } 11127c478bd9Sstevel@tonic-gate 11137c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) || 11147c478bd9Sstevel@tonic-gate libdiskmgt_str_eq(type, DDI_NT_SCSI_ATTACHMENT_POINT)) { 1115*42f64fdbSSarah Jelinek return (DM_CTYPE_SCSI); 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(type, DDI_NT_FC_ATTACHMENT_POINT)) { 1119*42f64fdbSSarah Jelinek return (DM_CTYPE_FIBRE); 11207c478bd9Sstevel@tonic-gate } 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(type, DDI_NT_NEXUS) && 11237c478bd9Sstevel@tonic-gate libdiskmgt_str_eq(name, "fp")) { 1124*42f64fdbSSarah Jelinek return (DM_CTYPE_FIBRE); 11257c478bd9Sstevel@tonic-gate } 11267c478bd9Sstevel@tonic-gate 11277c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(type, DDI_PSEUDO) && 11287c478bd9Sstevel@tonic-gate libdiskmgt_str_eq(name, "ide")) { 1129*42f64fdbSSarah Jelinek return (DM_CTYPE_ATA); 11307c478bd9Sstevel@tonic-gate } 11317c478bd9Sstevel@tonic-gate 11327c478bd9Sstevel@tonic-gate return (DM_CTYPE_UNKNOWN); 11337c478bd9Sstevel@tonic-gate } 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate static boolean_t 113697ab9a43SJohn Levon disk_is_cdrom(const char *type) 11377c478bd9Sstevel@tonic-gate { 113897ab9a43SJohn Levon return (strncmp(type, DDI_NT_CD, strlen(DDI_NT_CD)) == 0); 11397c478bd9Sstevel@tonic-gate } 11407c478bd9Sstevel@tonic-gate 11417c478bd9Sstevel@tonic-gate static alias_t * 11427c478bd9Sstevel@tonic-gate find_alias(disk_t *diskp, char *kernel_name) 11437c478bd9Sstevel@tonic-gate { 11447c478bd9Sstevel@tonic-gate alias_t *ap; 11457c478bd9Sstevel@tonic-gate 11467c478bd9Sstevel@tonic-gate ap = diskp->aliases; 11477c478bd9Sstevel@tonic-gate while (ap != NULL) { 1148*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(ap->kstat_name, kernel_name)) { 1149*42f64fdbSSarah Jelinek return (ap); 1150*42f64fdbSSarah Jelinek } 1151*42f64fdbSSarah Jelinek ap = ap->next; 11527c478bd9Sstevel@tonic-gate } 11537c478bd9Sstevel@tonic-gate 11547c478bd9Sstevel@tonic-gate return (NULL); 11557c478bd9Sstevel@tonic-gate } 11567c478bd9Sstevel@tonic-gate 11577c478bd9Sstevel@tonic-gate static bus_t * 11587c478bd9Sstevel@tonic-gate find_bus(struct search_args *args, char *name) 11597c478bd9Sstevel@tonic-gate { 11607c478bd9Sstevel@tonic-gate bus_t *listp; 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate listp = args->bus_listp; 11637c478bd9Sstevel@tonic-gate while (listp != NULL) { 1164*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(listp->name, name)) { 1165*42f64fdbSSarah Jelinek return (listp); 1166*42f64fdbSSarah Jelinek } 1167*42f64fdbSSarah Jelinek listp = listp->next; 11687c478bd9Sstevel@tonic-gate } 11697c478bd9Sstevel@tonic-gate 11707c478bd9Sstevel@tonic-gate return (NULL); 11717c478bd9Sstevel@tonic-gate } 11727c478bd9Sstevel@tonic-gate 11737c478bd9Sstevel@tonic-gate static controller_t * 11747c478bd9Sstevel@tonic-gate find_controller(struct search_args *args, char *name) 11757c478bd9Sstevel@tonic-gate { 11767c478bd9Sstevel@tonic-gate controller_t *listp; 11777c478bd9Sstevel@tonic-gate 11787c478bd9Sstevel@tonic-gate listp = args->controller_listp; 11797c478bd9Sstevel@tonic-gate while (listp != NULL) { 1180*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(listp->name, name)) { 1181*42f64fdbSSarah Jelinek return (listp); 1182*42f64fdbSSarah Jelinek } 1183*42f64fdbSSarah Jelinek listp = listp->next; 11847c478bd9Sstevel@tonic-gate } 11857c478bd9Sstevel@tonic-gate 11867c478bd9Sstevel@tonic-gate return (NULL); 11877c478bd9Sstevel@tonic-gate } 11887c478bd9Sstevel@tonic-gate 11897c478bd9Sstevel@tonic-gate static int 11907c478bd9Sstevel@tonic-gate fix_cluster_devpath(di_devlink_t devlink, void *arg) 11917c478bd9Sstevel@tonic-gate { 11927c478bd9Sstevel@tonic-gate int fd; 11937c478bd9Sstevel@tonic-gate struct search_args *args; 11947c478bd9Sstevel@tonic-gate char *devlink_path; 11957c478bd9Sstevel@tonic-gate disk_t *diskp = NULL; 11967c478bd9Sstevel@tonic-gate alias_t *ap = NULL; 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate /* 11997c478bd9Sstevel@tonic-gate * The devlink_path is of the form /dev/did/rdsk/d1s0. 12007c478bd9Sstevel@tonic-gate */ 12017c478bd9Sstevel@tonic-gate 12027c478bd9Sstevel@tonic-gate args = (struct search_args *)arg; 12037c478bd9Sstevel@tonic-gate 12047c478bd9Sstevel@tonic-gate /* Find the disk by the deviceid we read from the cluster disk. */ 12057c478bd9Sstevel@tonic-gate devlink_path = (char *)di_devlink_path(devlink); 12067c478bd9Sstevel@tonic-gate if (devlink_path == NULL) { 1207*42f64fdbSSarah Jelinek return (DI_WALK_CONTINUE); 12087c478bd9Sstevel@tonic-gate } 12097c478bd9Sstevel@tonic-gate 12107c478bd9Sstevel@tonic-gate if ((fd = open(devlink_path, O_RDONLY|O_NDELAY)) >= 0) { 1211*42f64fdbSSarah Jelinek ddi_devid_t devid; 12127c478bd9Sstevel@tonic-gate 1213*42f64fdbSSarah Jelinek if (dm_debug > 1) { 1214*42f64fdbSSarah Jelinek (void) fprintf(stderr, "INFO: cluster devpath %s\n", 1215*42f64fdbSSarah Jelinek devlink_path); 1216*42f64fdbSSarah Jelinek } 12177c478bd9Sstevel@tonic-gate 1218*42f64fdbSSarah Jelinek if (devid_get(fd, &devid) == 0) { 1219*42f64fdbSSarah Jelinek char *minor; 1220*42f64fdbSSarah Jelinek char *devidstr; 1221*42f64fdbSSarah Jelinek 1222*42f64fdbSSarah Jelinek minor = di_minor_name(args->minor); 1223*42f64fdbSSarah Jelinek 1224*42f64fdbSSarah Jelinek if ((devidstr = 1225*42f64fdbSSarah Jelinek devid_str_encode(devid, minor)) != NULL) { 1226*42f64fdbSSarah Jelinek diskp = get_disk_by_deviceid(args->disk_listp, 1227*42f64fdbSSarah Jelinek devidstr); 1228*42f64fdbSSarah Jelinek /* 1229*42f64fdbSSarah Jelinek * This really shouldn't happen, since 1230*42f64fdbSSarah Jelinek * we should have found all of the disks 1231*42f64fdbSSarah Jelinek * during our first pass through 1232*42f64fdbSSarah Jelinek * the dev tree, but just in case... 1233*42f64fdbSSarah Jelinek */ 1234*42f64fdbSSarah Jelinek if (diskp == NULL) { 1235*42f64fdbSSarah Jelinek if (dm_debug > 1) { 1236*42f64fdbSSarah Jelinek (void) fprintf(stderr, 1237*42f64fdbSSarah Jelinek "INFO: cluster create" 1238*42f64fdbSSarah Jelinek " disk\n"); 1239*42f64fdbSSarah Jelinek } 1240*42f64fdbSSarah Jelinek 1241*42f64fdbSSarah Jelinek diskp = create_disk(devidstr, 1242*42f64fdbSSarah Jelinek NULL, args); 1243*42f64fdbSSarah Jelinek if (diskp == NULL) { 1244*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 1245*42f64fdbSSarah Jelinek } 1246*42f64fdbSSarah Jelinek 1247*42f64fdbSSarah Jelinek /* add the controller relationship */ 1248*42f64fdbSSarah Jelinek if (args->dev_walk_status == 0) { 1249*42f64fdbSSarah Jelinek if (add_disk2controller(diskp, 1250*42f64fdbSSarah Jelinek args) != 0) { 1251*42f64fdbSSarah Jelinek args->dev_walk_status 1252*42f64fdbSSarah Jelinek = ENOMEM; 1253*42f64fdbSSarah Jelinek } 1254*42f64fdbSSarah Jelinek } 1255*42f64fdbSSarah Jelinek 1256*42f64fdbSSarah Jelinek if (new_alias(diskp, NULL, 1257*42f64fdbSSarah Jelinek devlink_path, args) != 0) { 1258*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 1259*42f64fdbSSarah Jelinek } 12607c478bd9Sstevel@tonic-gate } 1261*42f64fdbSSarah Jelinek devid_str_free(devidstr); 12627c478bd9Sstevel@tonic-gate } 1263*42f64fdbSSarah Jelinek devid_free(devid); 12647c478bd9Sstevel@tonic-gate } 1265*42f64fdbSSarah Jelinek (void) close(fd); 12667c478bd9Sstevel@tonic-gate } 12677c478bd9Sstevel@tonic-gate 12687c478bd9Sstevel@tonic-gate 12697c478bd9Sstevel@tonic-gate if (diskp != NULL) { 1270*42f64fdbSSarah Jelinek if (dm_debug > 1) { 1271*42f64fdbSSarah Jelinek (void) fprintf(stderr, "INFO: cluster found" 1272*42f64fdbSSarah Jelinek " disk\n"); 1273*42f64fdbSSarah Jelinek } 1274*42f64fdbSSarah Jelinek ap = diskp->aliases; 12757c478bd9Sstevel@tonic-gate } 12767c478bd9Sstevel@tonic-gate 12777c478bd9Sstevel@tonic-gate if (ap != NULL) { 12787c478bd9Sstevel@tonic-gate /* 1279*42f64fdbSSarah Jelinek * NOTE: if ap->next != NULL have cluster 1280*42f64fdbSSarah Jelinek * disks w/ multiple paths. 12817c478bd9Sstevel@tonic-gate */ 12827c478bd9Sstevel@tonic-gate 1283*42f64fdbSSarah Jelinek if (!ap->cluster) { 1284*42f64fdbSSarah Jelinek char *basep; 1285*42f64fdbSSarah Jelinek char *namep; 1286*42f64fdbSSarah Jelinek int cnt = 0; 1287*42f64fdbSSarah Jelinek int size; 1288*42f64fdbSSarah Jelinek char alias[MAXPATHLEN]; 12897c478bd9Sstevel@tonic-gate 1290*42f64fdbSSarah Jelinek /* 1291*42f64fdbSSarah Jelinek * First time; save the /dev/rdsk devpaths and 1292*42f64fdbSSarah Jelinek * update the alias info with the new alias name. 1293*42f64fdbSSarah Jelinek */ 1294*42f64fdbSSarah Jelinek ap->orig_paths = ap->devpaths; 1295*42f64fdbSSarah Jelinek ap->devpaths = NULL; 12967c478bd9Sstevel@tonic-gate 1297*42f64fdbSSarah Jelinek free(ap->alias); 12987c478bd9Sstevel@tonic-gate 1299*42f64fdbSSarah Jelinek /* get the new cluster alias name */ 1300*42f64fdbSSarah Jelinek basep = strrchr(devlink_path, '/'); 1301*42f64fdbSSarah Jelinek if (basep == NULL) { 1302*42f64fdbSSarah Jelinek basep = devlink_path; 1303*42f64fdbSSarah Jelinek } else { 1304*42f64fdbSSarah Jelinek basep++; 1305*42f64fdbSSarah Jelinek } 1306*42f64fdbSSarah Jelinek size = sizeof (alias) - 1; 1307*42f64fdbSSarah Jelinek namep = alias; 13087c478bd9Sstevel@tonic-gate 1309*42f64fdbSSarah Jelinek while (*basep != 0 && *basep != 's' && cnt < size) { 1310*42f64fdbSSarah Jelinek *namep++ = *basep++; 1311*42f64fdbSSarah Jelinek cnt++; 1312*42f64fdbSSarah Jelinek } 1313*42f64fdbSSarah Jelinek *namep = 0; 13147c478bd9Sstevel@tonic-gate 1315*42f64fdbSSarah Jelinek if ((ap->alias = strdup(alias)) == NULL) { 1316*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 1317*42f64fdbSSarah Jelinek } 1318*42f64fdbSSarah Jelinek 1319*42f64fdbSSarah Jelinek ap->cluster = 1; 1320*42f64fdbSSarah Jelinek } 1321*42f64fdbSSarah Jelinek 1322*42f64fdbSSarah Jelinek if (new_devpath(ap, devlink_path) != 0) { 1323*42f64fdbSSarah Jelinek args->dev_walk_status = ENOMEM; 1324*42f64fdbSSarah Jelinek } 13257c478bd9Sstevel@tonic-gate } 13267c478bd9Sstevel@tonic-gate 13277c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 13287c478bd9Sstevel@tonic-gate } 13297c478bd9Sstevel@tonic-gate 13307c478bd9Sstevel@tonic-gate /* 13317c478bd9Sstevel@tonic-gate * Check if we have the drive in our list, based upon the device id. 13327c478bd9Sstevel@tonic-gate * We got the device id from the dev tree walk. This is encoded 13337c478bd9Sstevel@tonic-gate * using devid_str_encode(3DEVID). In order to check the device ids we need 13347c478bd9Sstevel@tonic-gate * to use the devid_compare(3DEVID) function, so we need to decode the 13357c478bd9Sstevel@tonic-gate * string representation of the device id. 13367c478bd9Sstevel@tonic-gate */ 13377c478bd9Sstevel@tonic-gate static disk_t * 13387c478bd9Sstevel@tonic-gate get_disk_by_deviceid(disk_t *listp, char *devidstr) 13397c478bd9Sstevel@tonic-gate { 13407c478bd9Sstevel@tonic-gate ddi_devid_t devid; 13417c478bd9Sstevel@tonic-gate 13427c478bd9Sstevel@tonic-gate if (devidstr == NULL || devid_str_decode(devidstr, &devid, NULL) != 0) { 1343*42f64fdbSSarah Jelinek return (NULL); 13447c478bd9Sstevel@tonic-gate } 13457c478bd9Sstevel@tonic-gate 13467c478bd9Sstevel@tonic-gate while (listp != NULL) { 1347*42f64fdbSSarah Jelinek if (listp->devid != NULL && 1348*42f64fdbSSarah Jelinek devid_compare(listp->devid, devid) == 0) { 1349*42f64fdbSSarah Jelinek break; 1350*42f64fdbSSarah Jelinek } 1351*42f64fdbSSarah Jelinek listp = listp->next; 13527c478bd9Sstevel@tonic-gate } 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate devid_free(devid); 13557c478bd9Sstevel@tonic-gate return (listp); 13567c478bd9Sstevel@tonic-gate } 13577c478bd9Sstevel@tonic-gate 13587c478bd9Sstevel@tonic-gate /* 13597c478bd9Sstevel@tonic-gate * Get the base disk name with no path prefix and no slice (if there is one). 13607c478bd9Sstevel@tonic-gate * The name parameter should be big enough to hold the name. 13617c478bd9Sstevel@tonic-gate * This handles diskette names ok (/dev/rdiskette0) since there is no slice, 13627c478bd9Sstevel@tonic-gate * and converts the raw diskette name. 13637c478bd9Sstevel@tonic-gate * But, we don't know how to strip off the slice from third party drive 13647c478bd9Sstevel@tonic-gate * names. That just means that their drive name will include a slice on 13657c478bd9Sstevel@tonic-gate * it. 13667c478bd9Sstevel@tonic-gate */ 13677c478bd9Sstevel@tonic-gate static void 13687c478bd9Sstevel@tonic-gate get_disk_name_from_path(char *path, char *name, int size) 13697c478bd9Sstevel@tonic-gate { 13707c478bd9Sstevel@tonic-gate char *basep; 13717c478bd9Sstevel@tonic-gate int cnt = 0; 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate basep = strrchr(path, '/'); 13747c478bd9Sstevel@tonic-gate if (basep == NULL) { 1375*42f64fdbSSarah Jelinek basep = path; 13767c478bd9Sstevel@tonic-gate } else { 1377*42f64fdbSSarah Jelinek basep++; 13787c478bd9Sstevel@tonic-gate } 13797c478bd9Sstevel@tonic-gate 13807c478bd9Sstevel@tonic-gate size = size - 1; /* leave room for terminating 0 */ 13817c478bd9Sstevel@tonic-gate 13827c478bd9Sstevel@tonic-gate if (is_ctds(basep)) { 1383*42f64fdbSSarah Jelinek while (*basep != 0 && *basep != 's' && cnt < size) { 1384*42f64fdbSSarah Jelinek *name++ = *basep++; 1385*42f64fdbSSarah Jelinek cnt++; 1386*42f64fdbSSarah Jelinek } 1387*42f64fdbSSarah Jelinek *name = 0; 13887c478bd9Sstevel@tonic-gate } else { 1389*42f64fdbSSarah Jelinek if (strncmp(basep, FLOPPY_NAME, 1390*42f64fdbSSarah Jelinek sizeof (FLOPPY_NAME) - 1) == 0) { 1391*42f64fdbSSarah Jelinek /* 1392*42f64fdbSSarah Jelinek * a floppy, convert rdiskette name to diskette name, 1393*42f64fdbSSarah Jelinek * by skipping over the 'r' for raw diskette 1394*42f64fdbSSarah Jelinek */ 1395*42f64fdbSSarah Jelinek basep++; 1396*42f64fdbSSarah Jelinek } 13977c478bd9Sstevel@tonic-gate 1398*42f64fdbSSarah Jelinek /* not a ctds name, just copy it */ 1399*42f64fdbSSarah Jelinek (void) strlcpy(name, basep, size); 14007c478bd9Sstevel@tonic-gate } 14017c478bd9Sstevel@tonic-gate } 14027c478bd9Sstevel@tonic-gate 14037c478bd9Sstevel@tonic-gate static char * 14047c478bd9Sstevel@tonic-gate get_byte_prop(char *prop_name, di_node_t node) 14057c478bd9Sstevel@tonic-gate { 14067c478bd9Sstevel@tonic-gate int cnt; 14077c478bd9Sstevel@tonic-gate uchar_t *bytes; 14087c478bd9Sstevel@tonic-gate int i; 14097c478bd9Sstevel@tonic-gate char str[MAXPATHLEN]; 14107c478bd9Sstevel@tonic-gate 14117c478bd9Sstevel@tonic-gate cnt = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, prop_name, &bytes); 14127c478bd9Sstevel@tonic-gate if (cnt < 1) { 1413*42f64fdbSSarah Jelinek return (NULL); 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate 14167c478bd9Sstevel@tonic-gate str[0] = 0; 14177c478bd9Sstevel@tonic-gate for (i = 0; i < cnt; i++) { 1418*42f64fdbSSarah Jelinek char bstr[8]; /* a byte is only 2 hex chars + null */ 14197c478bd9Sstevel@tonic-gate 1420*42f64fdbSSarah Jelinek (void) snprintf(bstr, sizeof (bstr), "%.2x", bytes[i]); 1421*42f64fdbSSarah Jelinek (void) strlcat(str, bstr, sizeof (str)); 14227c478bd9Sstevel@tonic-gate } 14237c478bd9Sstevel@tonic-gate return (strdup(str)); 14247c478bd9Sstevel@tonic-gate } 14257c478bd9Sstevel@tonic-gate 14267c478bd9Sstevel@tonic-gate static di_node_t 14277c478bd9Sstevel@tonic-gate get_parent_bus(di_node_t node, struct search_args *args) 14287c478bd9Sstevel@tonic-gate { 14297c478bd9Sstevel@tonic-gate di_node_t pnode; 14307c478bd9Sstevel@tonic-gate 14317c478bd9Sstevel@tonic-gate pnode = di_parent_node(node); 14327c478bd9Sstevel@tonic-gate if (pnode == DI_NODE_NIL) { 1433*42f64fdbSSarah Jelinek return (NULL); 14347c478bd9Sstevel@tonic-gate } 14357c478bd9Sstevel@tonic-gate 14367c478bd9Sstevel@tonic-gate if (bus_type(pnode, di_minor_next(pnode, NULL), args->ph) != NULL) { 1437*42f64fdbSSarah Jelinek return (pnode); 14387c478bd9Sstevel@tonic-gate } 14397c478bd9Sstevel@tonic-gate 14407c478bd9Sstevel@tonic-gate return (get_parent_bus(pnode, args)); 14417c478bd9Sstevel@tonic-gate } 14427c478bd9Sstevel@tonic-gate 14437c478bd9Sstevel@tonic-gate static int 14447c478bd9Sstevel@tonic-gate get_prom_int(char *prop_name, di_node_t node, di_prom_handle_t ph) 14457c478bd9Sstevel@tonic-gate { 14467c478bd9Sstevel@tonic-gate int *n; 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate if (di_prom_prop_lookup_ints(ph, node, prop_name, &n) == 1) { 1449*42f64fdbSSarah Jelinek return (*n); 14507c478bd9Sstevel@tonic-gate } 14517c478bd9Sstevel@tonic-gate 14527c478bd9Sstevel@tonic-gate return (0); 14537c478bd9Sstevel@tonic-gate } 14547c478bd9Sstevel@tonic-gate 14557c478bd9Sstevel@tonic-gate static char * 14567c478bd9Sstevel@tonic-gate get_prom_str(char *prop_name, di_node_t node, di_prom_handle_t ph) 14577c478bd9Sstevel@tonic-gate { 14587c478bd9Sstevel@tonic-gate char *str; 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate if (di_prom_prop_lookup_strings(ph, node, prop_name, &str) == 1) { 1461*42f64fdbSSarah Jelinek return (str); 14627c478bd9Sstevel@tonic-gate } 14637c478bd9Sstevel@tonic-gate 14647c478bd9Sstevel@tonic-gate return (NULL); 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate /* 14687c478bd9Sstevel@tonic-gate * Get one of the positive int or boolean properties. 14697c478bd9Sstevel@tonic-gate */ 14707c478bd9Sstevel@tonic-gate static int 14717c478bd9Sstevel@tonic-gate get_prop(char *prop_name, di_node_t node) 14727c478bd9Sstevel@tonic-gate { 14737c478bd9Sstevel@tonic-gate int num; 14747c478bd9Sstevel@tonic-gate int *ip; 14757c478bd9Sstevel@tonic-gate 14767c478bd9Sstevel@tonic-gate if ((num = di_prop_lookup_ints(DDI_DEV_T_ANY, node, prop_name, &ip)) 14777c478bd9Sstevel@tonic-gate >= 0) { 1478*42f64fdbSSarah Jelinek if (num == 0) { 1479*42f64fdbSSarah Jelinek /* boolean */ 1480*42f64fdbSSarah Jelinek return (1); 1481*42f64fdbSSarah Jelinek } else if (num == 1) { 1482*42f64fdbSSarah Jelinek /* single int */ 1483*42f64fdbSSarah Jelinek return (*ip); 1484*42f64fdbSSarah Jelinek } 14857c478bd9Sstevel@tonic-gate } 14867c478bd9Sstevel@tonic-gate return (-1); 14877c478bd9Sstevel@tonic-gate } 14887c478bd9Sstevel@tonic-gate 14897c478bd9Sstevel@tonic-gate static char * 14907c478bd9Sstevel@tonic-gate get_str_prop(char *prop_name, di_node_t node) 14917c478bd9Sstevel@tonic-gate { 14927c478bd9Sstevel@tonic-gate char *str; 14937c478bd9Sstevel@tonic-gate 14947c478bd9Sstevel@tonic-gate if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, prop_name, &str) == 1) { 1495*42f64fdbSSarah Jelinek return (str); 14967c478bd9Sstevel@tonic-gate } 14977c478bd9Sstevel@tonic-gate 14987c478bd9Sstevel@tonic-gate return (NULL); 14997c478bd9Sstevel@tonic-gate } 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate /* 15027c478bd9Sstevel@tonic-gate * Check if we have the drive in our list, based upon the device id, if the 15037c478bd9Sstevel@tonic-gate * drive has a device id, or the kernel name, if it doesn't have a device id. 15047c478bd9Sstevel@tonic-gate */ 15057c478bd9Sstevel@tonic-gate static int 15067c478bd9Sstevel@tonic-gate have_disk(struct search_args *args, char *devidstr, char *kernel_name, 15077c478bd9Sstevel@tonic-gate disk_t **diskp) 15087c478bd9Sstevel@tonic-gate { 15097c478bd9Sstevel@tonic-gate disk_t *listp; 15107c478bd9Sstevel@tonic-gate 15117c478bd9Sstevel@tonic-gate *diskp = NULL; 15127c478bd9Sstevel@tonic-gate listp = args->disk_listp; 15137c478bd9Sstevel@tonic-gate if (devidstr != NULL) { 1514*42f64fdbSSarah Jelinek if ((*diskp = get_disk_by_deviceid(listp, devidstr)) != NULL) { 1515*42f64fdbSSarah Jelinek return (1); 1516*42f64fdbSSarah Jelinek } 15177c478bd9Sstevel@tonic-gate 15187c478bd9Sstevel@tonic-gate } else { 1519*42f64fdbSSarah Jelinek /* no devid, try matching the kernel names on the drives */ 1520*42f64fdbSSarah Jelinek while (listp != NULL) { 1521*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(kernel_name, 1522*42f64fdbSSarah Jelinek listp->kernel_name)) { 1523*42f64fdbSSarah Jelinek *diskp = listp; 1524*42f64fdbSSarah Jelinek return (1); 1525*42f64fdbSSarah Jelinek } 1526*42f64fdbSSarah Jelinek listp = listp->next; 15277c478bd9Sstevel@tonic-gate } 15287c478bd9Sstevel@tonic-gate } 15297c478bd9Sstevel@tonic-gate return (0); 15307c478bd9Sstevel@tonic-gate } 15317c478bd9Sstevel@tonic-gate 15327c478bd9Sstevel@tonic-gate static char * 15337c478bd9Sstevel@tonic-gate bus_type(di_node_t node, di_minor_t minor, di_prom_handle_t ph) 15347c478bd9Sstevel@tonic-gate { 15357c478bd9Sstevel@tonic-gate char *type; 15367c478bd9Sstevel@tonic-gate int i; 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate type = get_prom_str("device_type", node, ph); 15397c478bd9Sstevel@tonic-gate if (type == NULL) { 1540*42f64fdbSSarah Jelinek type = di_node_name(node); 15417c478bd9Sstevel@tonic-gate } 15427c478bd9Sstevel@tonic-gate 15437c478bd9Sstevel@tonic-gate for (i = 0; bustypes[i]; i++) { 1544*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(type, bustypes[i])) { 1545*42f64fdbSSarah Jelinek return (type); 1546*42f64fdbSSarah Jelinek } 15477c478bd9Sstevel@tonic-gate } 15487c478bd9Sstevel@tonic-gate 15497c478bd9Sstevel@tonic-gate if (minor != NULL && strcmp(di_minor_nodetype(minor), 15507c478bd9Sstevel@tonic-gate DDI_NT_USB_ATTACHMENT_POINT) == 0) { 1551*42f64fdbSSarah Jelinek return ("usb"); 15527c478bd9Sstevel@tonic-gate } 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate return (NULL); 15557c478bd9Sstevel@tonic-gate } 15567c478bd9Sstevel@tonic-gate 15577c478bd9Sstevel@tonic-gate static int 15587c478bd9Sstevel@tonic-gate is_cluster_disk(di_node_t node, di_minor_t minor) 15597c478bd9Sstevel@tonic-gate { 15607c478bd9Sstevel@tonic-gate if (di_minor_spectype(minor) == S_IFCHR && 15617c478bd9Sstevel@tonic-gate libdiskmgt_str_eq(di_minor_nodetype(minor), DDI_PSEUDO) && 15627c478bd9Sstevel@tonic-gate libdiskmgt_str_eq(di_node_name(node), CLUSTER_DEV)) { 1563*42f64fdbSSarah Jelinek return (1); 15647c478bd9Sstevel@tonic-gate } 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate return (0); 15677c478bd9Sstevel@tonic-gate } 15687c478bd9Sstevel@tonic-gate 15697c478bd9Sstevel@tonic-gate /* 15707c478bd9Sstevel@tonic-gate * If the input name is in c[t]ds format then return 1, otherwise return 0. 15717c478bd9Sstevel@tonic-gate */ 15727c478bd9Sstevel@tonic-gate static int 15737c478bd9Sstevel@tonic-gate is_ctds(char *name) 15747c478bd9Sstevel@tonic-gate { 15757c478bd9Sstevel@tonic-gate char *p; 15767c478bd9Sstevel@tonic-gate 15777c478bd9Sstevel@tonic-gate p = name; 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate if (*p++ != 'c') { 1580*42f64fdbSSarah Jelinek return (0); 15817c478bd9Sstevel@tonic-gate } 15827c478bd9Sstevel@tonic-gate /* skip controller digits */ 15837c478bd9Sstevel@tonic-gate while (isdigit(*p)) { 1584*42f64fdbSSarah Jelinek p++; 15857c478bd9Sstevel@tonic-gate } 15867c478bd9Sstevel@tonic-gate 15877c478bd9Sstevel@tonic-gate /* handle optional target */ 15887c478bd9Sstevel@tonic-gate if (*p == 't') { 15897c478bd9Sstevel@tonic-gate p++; 1590*42f64fdbSSarah Jelinek /* skip over target */ 1591*42f64fdbSSarah Jelinek while (isdigit(*p) || isupper(*p)) { 1592*42f64fdbSSarah Jelinek p++; 1593*42f64fdbSSarah Jelinek } 15947c478bd9Sstevel@tonic-gate } 15957c478bd9Sstevel@tonic-gate 15967c478bd9Sstevel@tonic-gate if (*p++ != 'd') { 1597*42f64fdbSSarah Jelinek return (0); 15987c478bd9Sstevel@tonic-gate } 15997c478bd9Sstevel@tonic-gate while (isdigit(*p)) { 1600*42f64fdbSSarah Jelinek p++; 16017c478bd9Sstevel@tonic-gate } 16027c478bd9Sstevel@tonic-gate 16037c478bd9Sstevel@tonic-gate if (*p++ != 's') { 1604*42f64fdbSSarah Jelinek return (0); 16057c478bd9Sstevel@tonic-gate } 16067c478bd9Sstevel@tonic-gate 16077c478bd9Sstevel@tonic-gate /* check the slice number */ 16087c478bd9Sstevel@tonic-gate while (isdigit(*p)) { 1609*42f64fdbSSarah Jelinek p++; 16107c478bd9Sstevel@tonic-gate } 16117c478bd9Sstevel@tonic-gate 16127c478bd9Sstevel@tonic-gate if (*p != 0) { 1613*42f64fdbSSarah Jelinek return (0); 16147c478bd9Sstevel@tonic-gate } 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate return (1); 16177c478bd9Sstevel@tonic-gate } 16187c478bd9Sstevel@tonic-gate 16197c478bd9Sstevel@tonic-gate static int 16207c478bd9Sstevel@tonic-gate is_drive(di_minor_t minor) 16217c478bd9Sstevel@tonic-gate { 162297ab9a43SJohn Levon return (strncmp(di_minor_nodetype(minor), DDI_NT_BLOCK, 162397ab9a43SJohn Levon strlen(DDI_NT_BLOCK)) == 0); 16247c478bd9Sstevel@tonic-gate } 16257c478bd9Sstevel@tonic-gate 1626e7cbe64fSgw static int 1627e7cbe64fSgw is_zvol(di_node_t node, di_minor_t minor) 1628e7cbe64fSgw { 1629e7cbe64fSgw if ((strncmp(di_node_name(node), ZFS_DRIVER, 3) == 0) && 1630681d9761SEric Taylor minor(di_minor_devt(minor))) 1631e7cbe64fSgw return (1); 1632e7cbe64fSgw return (0); 1633e7cbe64fSgw } 1634e7cbe64fSgw 16357c478bd9Sstevel@tonic-gate static int 16367c478bd9Sstevel@tonic-gate is_HBA(di_node_t node, di_minor_t minor) 16377c478bd9Sstevel@tonic-gate { 16387c478bd9Sstevel@tonic-gate char *type; 16397c478bd9Sstevel@tonic-gate char *name; 16407c478bd9Sstevel@tonic-gate int type_index; 16417c478bd9Sstevel@tonic-gate 16427c478bd9Sstevel@tonic-gate type = di_minor_nodetype(minor); 16437c478bd9Sstevel@tonic-gate type_index = 0; 16447c478bd9Sstevel@tonic-gate 16457c478bd9Sstevel@tonic-gate while (ctrltypes[type_index] != NULL) { 1646*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(type, ctrltypes[type_index])) { 1647*42f64fdbSSarah Jelinek return (1); 1648*42f64fdbSSarah Jelinek } 1649*42f64fdbSSarah Jelinek type_index++; 16507c478bd9Sstevel@tonic-gate } 16517c478bd9Sstevel@tonic-gate 16527c478bd9Sstevel@tonic-gate name = di_node_name(node); 16537c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(type, DDI_PSEUDO) && 16547c478bd9Sstevel@tonic-gate libdiskmgt_str_eq(name, "ide")) { 1655*42f64fdbSSarah Jelinek return (1); 16567c478bd9Sstevel@tonic-gate } 16577c478bd9Sstevel@tonic-gate 16587c478bd9Sstevel@tonic-gate return (0); 16597c478bd9Sstevel@tonic-gate } 16607c478bd9Sstevel@tonic-gate 16617c478bd9Sstevel@tonic-gate static int 16627c478bd9Sstevel@tonic-gate new_alias(disk_t *diskp, char *kernel_name, char *devlink_path, 16637c478bd9Sstevel@tonic-gate struct search_args *args) 16647c478bd9Sstevel@tonic-gate { 16657c478bd9Sstevel@tonic-gate alias_t *aliasp; 16667c478bd9Sstevel@tonic-gate char alias[MAXPATHLEN]; 16677c478bd9Sstevel@tonic-gate di_node_t pnode; 16687c478bd9Sstevel@tonic-gate 16697c478bd9Sstevel@tonic-gate aliasp = malloc(sizeof (alias_t)); 16707c478bd9Sstevel@tonic-gate if (aliasp == NULL) { 1671*42f64fdbSSarah Jelinek return (ENOMEM); 16727c478bd9Sstevel@tonic-gate } 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate aliasp->alias = NULL; 16757c478bd9Sstevel@tonic-gate aliasp->kstat_name = NULL; 16767c478bd9Sstevel@tonic-gate aliasp->wwn = NULL; 16777c478bd9Sstevel@tonic-gate aliasp->devpaths = NULL; 16787c478bd9Sstevel@tonic-gate aliasp->orig_paths = NULL; 16797c478bd9Sstevel@tonic-gate 16807c478bd9Sstevel@tonic-gate get_disk_name_from_path(devlink_path, alias, sizeof (alias)); 16817c478bd9Sstevel@tonic-gate 16827c478bd9Sstevel@tonic-gate aliasp->alias = strdup(alias); 16837c478bd9Sstevel@tonic-gate if (aliasp->alias == NULL) { 1684*42f64fdbSSarah Jelinek cache_free_alias(aliasp); 1685*42f64fdbSSarah Jelinek return (ENOMEM); 16867c478bd9Sstevel@tonic-gate } 16877c478bd9Sstevel@tonic-gate 16887c478bd9Sstevel@tonic-gate if (kernel_name != NULL) { 1689*42f64fdbSSarah Jelinek aliasp->kstat_name = strdup(kernel_name); 1690*42f64fdbSSarah Jelinek if (aliasp->kstat_name == NULL) { 1691*42f64fdbSSarah Jelinek cache_free_alias(aliasp); 1692*42f64fdbSSarah Jelinek return (ENOMEM); 1693*42f64fdbSSarah Jelinek } 16947c478bd9Sstevel@tonic-gate } else { 1695*42f64fdbSSarah Jelinek aliasp->kstat_name = NULL; 16967c478bd9Sstevel@tonic-gate } 16977c478bd9Sstevel@tonic-gate 16987c478bd9Sstevel@tonic-gate aliasp->cluster = 0; 16997c478bd9Sstevel@tonic-gate aliasp->lun = get_prop(DM_LUN, args->node); 17007c478bd9Sstevel@tonic-gate aliasp->target = get_prop(DM_TARGET, args->node); 17017c478bd9Sstevel@tonic-gate aliasp->wwn = get_byte_prop(WWN_PROP, args->node); 17027c478bd9Sstevel@tonic-gate 17037c478bd9Sstevel@tonic-gate pnode = di_parent_node(args->node); 17047c478bd9Sstevel@tonic-gate if (pnode != DI_NODE_NIL) { 1705*42f64fdbSSarah Jelinek char prop_name[MAXPROPLEN]; 17067c478bd9Sstevel@tonic-gate 1707*42f64fdbSSarah Jelinek (void) snprintf(prop_name, sizeof (prop_name), 1708*42f64fdbSSarah Jelinek "target%d-sync-speed", aliasp->target); 1709*42f64fdbSSarah Jelinek diskp->sync_speed = get_prop(prop_name, pnode); 1710*42f64fdbSSarah Jelinek (void) snprintf(prop_name, sizeof (prop_name), "target%d-wide", 1711*42f64fdbSSarah Jelinek aliasp->target); 1712*42f64fdbSSarah Jelinek diskp->wide = get_prop(prop_name, pnode); 17137c478bd9Sstevel@tonic-gate } 17147c478bd9Sstevel@tonic-gate 17157c478bd9Sstevel@tonic-gate if (new_devpath(aliasp, devlink_path) != 0) { 1716*42f64fdbSSarah Jelinek cache_free_alias(aliasp); 1717*42f64fdbSSarah Jelinek return (ENOMEM); 17187c478bd9Sstevel@tonic-gate } 17197c478bd9Sstevel@tonic-gate 17207c478bd9Sstevel@tonic-gate aliasp->next = diskp->aliases; 17217c478bd9Sstevel@tonic-gate diskp->aliases = aliasp; 17227c478bd9Sstevel@tonic-gate 17237c478bd9Sstevel@tonic-gate return (0); 17247c478bd9Sstevel@tonic-gate } 17257c478bd9Sstevel@tonic-gate 17267c478bd9Sstevel@tonic-gate /* 17277c478bd9Sstevel@tonic-gate * Append the new devpath to the end of the devpath list. This is important 17287c478bd9Sstevel@tonic-gate * since we may want to use the order of the devpaths to match up the vtoc 17297c478bd9Sstevel@tonic-gate * entries. 17307c478bd9Sstevel@tonic-gate */ 17317c478bd9Sstevel@tonic-gate static int 17327c478bd9Sstevel@tonic-gate new_devpath(alias_t *ap, char *devpath) 17337c478bd9Sstevel@tonic-gate { 17347c478bd9Sstevel@tonic-gate slice_t *newdp; 17357c478bd9Sstevel@tonic-gate slice_t *alistp; 17367c478bd9Sstevel@tonic-gate 17377c478bd9Sstevel@tonic-gate /* 17387c478bd9Sstevel@tonic-gate * First, search the alias list to be sure that this devpath is 17397c478bd9Sstevel@tonic-gate * not already there. 17407c478bd9Sstevel@tonic-gate */ 17417c478bd9Sstevel@tonic-gate 17427c478bd9Sstevel@tonic-gate for (alistp = ap->devpaths; alistp != NULL; alistp = alistp->next) { 1743*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(alistp->devpath, devpath)) { 1744*42f64fdbSSarah Jelinek return (0); 1745*42f64fdbSSarah Jelinek } 17467c478bd9Sstevel@tonic-gate } 17477c478bd9Sstevel@tonic-gate 17487c478bd9Sstevel@tonic-gate /* 17497c478bd9Sstevel@tonic-gate * Otherwise, not found so add this new devpath to the list. 17507c478bd9Sstevel@tonic-gate */ 17517c478bd9Sstevel@tonic-gate 17527c478bd9Sstevel@tonic-gate newdp = malloc(sizeof (slice_t)); 17537c478bd9Sstevel@tonic-gate if (newdp == NULL) { 1754*42f64fdbSSarah Jelinek return (ENOMEM); 17557c478bd9Sstevel@tonic-gate } 17567c478bd9Sstevel@tonic-gate 17577c478bd9Sstevel@tonic-gate newdp->devpath = strdup(devpath); 17587c478bd9Sstevel@tonic-gate if (newdp->devpath == NULL) { 1759*42f64fdbSSarah Jelinek free(newdp); 1760*42f64fdbSSarah Jelinek return (ENOMEM); 17617c478bd9Sstevel@tonic-gate } 17627c478bd9Sstevel@tonic-gate newdp->slice_num = -1; 17637c478bd9Sstevel@tonic-gate newdp->next = NULL; 17647c478bd9Sstevel@tonic-gate 17657c478bd9Sstevel@tonic-gate if (ap->devpaths == NULL) { 1766*42f64fdbSSarah Jelinek ap->devpaths = newdp; 17677c478bd9Sstevel@tonic-gate } else { 1768*42f64fdbSSarah Jelinek /* append the devpath to the end of the list */ 1769*42f64fdbSSarah Jelinek slice_t *dp; 17707c478bd9Sstevel@tonic-gate 1771*42f64fdbSSarah Jelinek dp = ap->devpaths; 1772*42f64fdbSSarah Jelinek while (dp->next != NULL) { 1773*42f64fdbSSarah Jelinek dp = dp->next; 1774*42f64fdbSSarah Jelinek } 17757c478bd9Sstevel@tonic-gate 1776*42f64fdbSSarah Jelinek dp->next = newdp; 17777c478bd9Sstevel@tonic-gate } 17787c478bd9Sstevel@tonic-gate 17797c478bd9Sstevel@tonic-gate return (0); 17807c478bd9Sstevel@tonic-gate } 17817c478bd9Sstevel@tonic-gate 17827c478bd9Sstevel@tonic-gate static path_t * 17837c478bd9Sstevel@tonic-gate new_path(controller_t *cp, disk_t *dp, di_node_t node, di_path_state_t st, 17847c478bd9Sstevel@tonic-gate char *wwn) 17857c478bd9Sstevel@tonic-gate { 17867c478bd9Sstevel@tonic-gate char *devpath; 17877c478bd9Sstevel@tonic-gate path_t *pp; 17887c478bd9Sstevel@tonic-gate di_minor_t minor; 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate /* Special handling for fp attachment node. */ 17917c478bd9Sstevel@tonic-gate if (strcmp(di_node_name(node), "fp") == 0) { 1792*42f64fdbSSarah Jelinek di_node_t pnode; 17937c478bd9Sstevel@tonic-gate 1794*42f64fdbSSarah Jelinek pnode = di_parent_node(node); 1795*42f64fdbSSarah Jelinek if (pnode != DI_NODE_NIL) { 1796*42f64fdbSSarah Jelinek node = pnode; 1797*42f64fdbSSarah Jelinek } 17987c478bd9Sstevel@tonic-gate } 17997c478bd9Sstevel@tonic-gate 18007c478bd9Sstevel@tonic-gate devpath = di_devfs_path(node); 18017c478bd9Sstevel@tonic-gate 18027c478bd9Sstevel@tonic-gate /* check if the path is already there */ 18037c478bd9Sstevel@tonic-gate pp = NULL; 18047c478bd9Sstevel@tonic-gate if (cp->paths != NULL) { 1805*42f64fdbSSarah Jelinek int i; 18067c478bd9Sstevel@tonic-gate 1807*42f64fdbSSarah Jelinek for (i = 0; cp->paths[i]; i++) { 1808*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(devpath, cp->paths[i]->name)) { 1809*42f64fdbSSarah Jelinek pp = cp->paths[i]; 1810*42f64fdbSSarah Jelinek break; 1811*42f64fdbSSarah Jelinek } 18127c478bd9Sstevel@tonic-gate } 18137c478bd9Sstevel@tonic-gate } 18147c478bd9Sstevel@tonic-gate 18157c478bd9Sstevel@tonic-gate if (pp != NULL) { 1816*42f64fdbSSarah Jelinek /* the path exists, add this disk to it */ 18177c478bd9Sstevel@tonic-gate 1818*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 1819*42f64fdbSSarah Jelinek if (!add_disk2path(dp, pp, st, wwn)) { 1820*42f64fdbSSarah Jelinek return (NULL); 1821*42f64fdbSSarah Jelinek } 1822*42f64fdbSSarah Jelinek return (pp); 18237c478bd9Sstevel@tonic-gate } 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate /* create a new path */ 18267c478bd9Sstevel@tonic-gate 18277c478bd9Sstevel@tonic-gate pp = calloc(1, sizeof (path_t)); 18287c478bd9Sstevel@tonic-gate if (pp == NULL) { 1829*42f64fdbSSarah Jelinek di_devfs_path_free((void *) devpath); 1830*42f64fdbSSarah Jelinek return (NULL); 18317c478bd9Sstevel@tonic-gate } 18327c478bd9Sstevel@tonic-gate 18337c478bd9Sstevel@tonic-gate pp->name = strdup(devpath); 18347c478bd9Sstevel@tonic-gate di_devfs_path_free((void *) devpath); 18357c478bd9Sstevel@tonic-gate if (pp->name == NULL) { 1836*42f64fdbSSarah Jelinek cache_free_path(pp); 1837*42f64fdbSSarah Jelinek return (NULL); 18387c478bd9Sstevel@tonic-gate } 18397c478bd9Sstevel@tonic-gate 18407c478bd9Sstevel@tonic-gate /* add the disk to the path */ 18417c478bd9Sstevel@tonic-gate if (!add_disk2path(dp, pp, st, wwn)) { 1842*42f64fdbSSarah Jelinek return (NULL); 18437c478bd9Sstevel@tonic-gate } 18447c478bd9Sstevel@tonic-gate 18457c478bd9Sstevel@tonic-gate /* add the path to the controller */ 18467c478bd9Sstevel@tonic-gate if (add_ptr2array(pp, (void ***)&cp->paths) != 0) { 1847*42f64fdbSSarah Jelinek cache_free_path(pp); 1848*42f64fdbSSarah Jelinek return (NULL); 18497c478bd9Sstevel@tonic-gate } 18507c478bd9Sstevel@tonic-gate 18517c478bd9Sstevel@tonic-gate /* add the controller to the path */ 18527c478bd9Sstevel@tonic-gate pp->controller = cp; 18537c478bd9Sstevel@tonic-gate 18547c478bd9Sstevel@tonic-gate minor = di_minor_next(node, NULL); 18557c478bd9Sstevel@tonic-gate if (minor != NULL) { 1856*42f64fdbSSarah Jelinek pp->ctype = ctype(node, minor); 18577c478bd9Sstevel@tonic-gate } else { 1858*42f64fdbSSarah Jelinek pp->ctype = DM_CTYPE_UNKNOWN; 18597c478bd9Sstevel@tonic-gate } 18607c478bd9Sstevel@tonic-gate 18617c478bd9Sstevel@tonic-gate return (pp); 18627c478bd9Sstevel@tonic-gate } 18637c478bd9Sstevel@tonic-gate 18647c478bd9Sstevel@tonic-gate /* 18657c478bd9Sstevel@tonic-gate * We pass in the current controller pointer (currp) so we can double check 18667c478bd9Sstevel@tonic-gate * that we aren't corrupting the list by removing the element we are on. This 18677c478bd9Sstevel@tonic-gate * should never happen, but it doesn't hurt to double check. 18687c478bd9Sstevel@tonic-gate */ 18697c478bd9Sstevel@tonic-gate static void 18707c478bd9Sstevel@tonic-gate remove_invalid_controller(char *name, controller_t *currp, 18717c478bd9Sstevel@tonic-gate struct search_args *args) 18727c478bd9Sstevel@tonic-gate { 18737c478bd9Sstevel@tonic-gate controller_t *cp; 18747c478bd9Sstevel@tonic-gate bus_t *bp; 18757c478bd9Sstevel@tonic-gate controller_t *prevp; 18767c478bd9Sstevel@tonic-gate 18777c478bd9Sstevel@tonic-gate bp = args->bus_listp; 18787c478bd9Sstevel@tonic-gate while (bp != NULL) { 1879*42f64fdbSSarah Jelinek int i; 18807c478bd9Sstevel@tonic-gate 1881*42f64fdbSSarah Jelinek for (i = 0; bp->controllers[i]; i++) { 1882*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(bp->controllers[i]->name, name)) { 1883*42f64fdbSSarah Jelinek int j; 1884*42f64fdbSSarah Jelinek /* 1885*42f64fdbSSarah Jelinek * remove pointer to invalid controller. 1886*42f64fdbSSarah Jelinek * (it is a path) 1887*42f64fdbSSarah Jelinek */ 1888*42f64fdbSSarah Jelinek for (j = i; bp->controllers[j]; j++) { 1889*42f64fdbSSarah Jelinek bp->controllers[j] = 1890*42f64fdbSSarah Jelinek bp->controllers[j + 1]; 1891*42f64fdbSSarah Jelinek } 1892*42f64fdbSSarah Jelinek } 18937c478bd9Sstevel@tonic-gate } 1894*42f64fdbSSarah Jelinek bp = bp->next; 18957c478bd9Sstevel@tonic-gate } 18967c478bd9Sstevel@tonic-gate 18977c478bd9Sstevel@tonic-gate if (args->controller_listp == NULL) { 1898*42f64fdbSSarah Jelinek return; 18997c478bd9Sstevel@tonic-gate } 19007c478bd9Sstevel@tonic-gate 19017c478bd9Sstevel@tonic-gate cp = args->controller_listp; 19027c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(cp->name, name)) { 19037c478bd9Sstevel@tonic-gate args->controller_listp = cp->next; 1904*42f64fdbSSarah Jelinek if (dm_debug) { 1905*42f64fdbSSarah Jelinek (void) fprintf(stderr, 1906*42f64fdbSSarah Jelinek "INFO: Removed controller %s from list\n", 1907*42f64fdbSSarah Jelinek cp->name); 1908*42f64fdbSSarah Jelinek } 1909*42f64fdbSSarah Jelinek remove_controller(cp, currp); 1910*42f64fdbSSarah Jelinek return; 19117c478bd9Sstevel@tonic-gate } 19127c478bd9Sstevel@tonic-gate 19137c478bd9Sstevel@tonic-gate prevp = cp; 19147c478bd9Sstevel@tonic-gate cp = cp->next; 19157c478bd9Sstevel@tonic-gate while (cp != NULL) { 1916*42f64fdbSSarah Jelinek if (libdiskmgt_str_eq(cp->name, name)) { 1917*42f64fdbSSarah Jelinek if (dm_debug) { 1918*42f64fdbSSarah Jelinek (void) fprintf(stderr, 1919*42f64fdbSSarah Jelinek "INFO: Removed controller %s from list\n", 1920*42f64fdbSSarah Jelinek cp->name); 1921*42f64fdbSSarah Jelinek } 1922*42f64fdbSSarah Jelinek prevp->next = cp->next; 1923*42f64fdbSSarah Jelinek remove_controller(cp, currp); 1924*42f64fdbSSarah Jelinek return; 19257c478bd9Sstevel@tonic-gate } 1926*42f64fdbSSarah Jelinek prevp = cp; 1927*42f64fdbSSarah Jelinek cp = cp->next; 19287c478bd9Sstevel@tonic-gate } 19297c478bd9Sstevel@tonic-gate } 19307c478bd9Sstevel@tonic-gate 19317c478bd9Sstevel@tonic-gate /* 19327c478bd9Sstevel@tonic-gate * This is the standard strstr code modified for case independence. 19337c478bd9Sstevel@tonic-gate */ 19347c478bd9Sstevel@tonic-gate static char * 19357c478bd9Sstevel@tonic-gate str_case_index(register char *s1, register char *s2) 19367c478bd9Sstevel@tonic-gate { 19377c478bd9Sstevel@tonic-gate uint_t s2len = strlen(s2); /* length of the second string */ 19387c478bd9Sstevel@tonic-gate 19397c478bd9Sstevel@tonic-gate /* If the length of the second string is 0, return the first arg. */ 19407c478bd9Sstevel@tonic-gate if (s2len == 0) { 1941*42f64fdbSSarah Jelinek return (s1); 19427c478bd9Sstevel@tonic-gate } 19437c478bd9Sstevel@tonic-gate 19447c478bd9Sstevel@tonic-gate while (strlen(s1) >= s2len) { 1945*42f64fdbSSarah Jelinek if (strncasecmp(s1, s2, s2len) == 0) { 1946*42f64fdbSSarah Jelinek return (s1); 1947*42f64fdbSSarah Jelinek } 1948*42f64fdbSSarah Jelinek s1++; 19497c478bd9Sstevel@tonic-gate } 19507c478bd9Sstevel@tonic-gate return (NULL); 19517c478bd9Sstevel@tonic-gate } 1952