1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 /* 25 * Copyright 2020 Joyent, Inc. 26 */ 27 28 #include <strings.h> 29 #include <devid.h> 30 #include <pthread.h> 31 #include <inttypes.h> 32 #include <sys/dkio.h> 33 #include <sys/scsi/scsi_types.h> 34 #include <fm/topo_mod.h> 35 #include <fm/topo_list.h> 36 #include <fm/libdiskstatus.h> 37 #include <sys/fm/protocol.h> 38 #include "disk.h" 39 #include "disk_drivers.h" 40 41 static int disk_enum(topo_mod_t *, tnode_t *, const char *, 42 topo_instance_t, topo_instance_t, void *, void *); 43 44 static const topo_modops_t disk_ops = 45 { disk_enum, NULL }; 46 47 static const topo_modinfo_t disk_info = 48 {DISK, FM_FMRI_SCHEME_HC, DISK_VERSION, &disk_ops}; 49 50 static int 51 disk_declare_driver(topo_mod_t *mod, tnode_t *baynode, topo_list_t *dlistp, 52 char *driver) 53 { 54 int err; 55 56 if (strcmp(MPTSAS_DRV, driver) == 0) { 57 char *sas_address = NULL; 58 tnode_t *child = NULL; 59 60 if ((err = disk_mptsas_find_disk(mod, baynode, 61 &sas_address)) != 0) 62 return (err); 63 64 err = disk_declare_addr(mod, baynode, dlistp, 65 sas_address, &child); 66 topo_mod_strfree(mod, sas_address); 67 68 return (err); 69 } else if (strcmp(NVME_DRV, driver) == 0) { 70 if (disk_nvme_enum_disk(mod, baynode) != 0) 71 return (-1); 72 73 return (0); 74 } 75 76 topo_mod_dprintf(mod, "unknown disk driver '%s'\n", driver); 77 return (-1); 78 } 79 80 /*ARGSUSED*/ 81 static int 82 disk_enum(topo_mod_t *mod, tnode_t *baynode, 83 const char *name, topo_instance_t min, topo_instance_t max, 84 void *arg, void *notused) 85 { 86 char *device, *driver, *pname; 87 int err; 88 topo_list_t *dlistp = topo_mod_getspecific(mod); 89 90 if (strcmp(name, DISK) != 0 && strcmp(name, NVME) != 0) { 91 topo_mod_dprintf(mod, "disk_enum: can't enumerate %s nodes - " 92 "only know how to enumerate %s and %s nodes.", name, 93 DISK, NVME); 94 return (-1); 95 } 96 97 /* 98 * Historically we've always set the parent FRU on nodes; however, it's 99 * not clear why. Certain node types like USB don't want this, so we 100 * only do this if the parent is actually a bay. 101 */ 102 pname = topo_node_name(baynode); 103 if (strcmp(pname, BAY) == 0) { 104 nvlist_t *fmri; 105 if (topo_node_resource(baynode, &fmri, &err) != 0) { 106 topo_mod_dprintf(mod, "disk_enum: " 107 "topo_node_resource error %s\n", 108 topo_strerror(err)); 109 return (-1); 110 } 111 /* 112 * If the disk enumerator module has been run from an XML map 113 * and the parent bay node was already created by an enumerator 114 * module (e.g. ses), then the FRU will already be set. 115 */ 116 if (topo_node_fru_set(baynode, fmri, 0, &err) != 0 && 117 err != ETOPO_PROP_DEFD) { 118 topo_mod_dprintf(mod, "disk_enum: " 119 "topo_node_fru error %s\n", topo_strerror(err)); 120 nvlist_free(fmri); 121 return (-1); 122 } 123 nvlist_free(fmri); 124 } 125 126 /* 127 * For internal storage, first check to see if we need to 128 * request more detail from an HBA driver. 129 */ 130 if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING, 131 TOPO_BINDING_DRIVER, &driver, &err) == 0) { 132 err = disk_declare_driver(mod, baynode, dlistp, driver); 133 134 topo_mod_strfree(mod, driver); 135 return (err); 136 } else if (err != ETOPO_PROP_NOENT) { 137 topo_mod_dprintf(mod, "disk_enum: " 138 "binding error %s\n", topo_strerror(err)); 139 return (-1); 140 } 141 142 /* 143 * For internal storage, get the path to the occupant from the 144 * binding group of the bay node 145 */ 146 if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING, 147 TOPO_BINDING_OCCUPANT, &device, &err) != 0) { 148 topo_mod_dprintf(mod, "disk_enum: " 149 "failed to lookup prop %s/%s: %s\n", TOPO_PGROUP_BINDING, 150 TOPO_BINDING_OCCUPANT, topo_strerror(err)); 151 return (-1); 152 } 153 154 155 /* locate and topo enumerate the disk with that path */ 156 err = disk_declare_path(mod, baynode, dlistp, device); 157 158 topo_mod_strfree(mod, device); 159 return (err); 160 } 161 162 /*ARGSUSED*/ 163 int 164 _topo_init(topo_mod_t *mod, topo_version_t version) 165 { 166 topo_list_t *dlistp; 167 168 /* 169 * Turn on module debugging output 170 */ 171 if (getenv("TOPODISKDEBUG") != NULL) 172 topo_mod_setdebug(mod); 173 topo_mod_dprintf(mod, "_topo_init: " 174 "initializing %s enumerator\n", DISK); 175 176 if (topo_mod_register(mod, &disk_info, TOPO_VERSION) != 0) { 177 topo_mod_dprintf(mod, "_topo_init: " 178 "%s registration failed: %s\n", DISK, topo_mod_errmsg(mod)); 179 return (-1); /* mod errno already set */ 180 } 181 182 if ((dlistp = topo_mod_zalloc(mod, sizeof (topo_list_t))) == NULL) { 183 topo_mod_dprintf(mod, "_topo_inti: failed to allocate " 184 "disk list"); 185 return (-1); 186 } 187 188 if (dev_list_gather(mod, dlistp) != 0) { 189 topo_mod_unregister(mod); 190 topo_mod_free(mod, dlistp, sizeof (topo_list_t)); 191 topo_mod_dprintf(mod, "_topo_init: " 192 "failed to locate disks"); 193 return (-1); 194 } 195 196 topo_mod_dprintf(mod, "_topo_init: " 197 "%s enumerator initialized\n", DISK); 198 199 topo_mod_setspecific(mod, dlistp); 200 201 return (0); 202 } 203 204 void 205 _topo_fini(topo_mod_t *mod) 206 { 207 topo_list_t *dlistp = topo_mod_getspecific(mod); 208 dev_list_free(mod, dlistp); 209 topo_mod_free(mod, dlistp, sizeof (topo_list_t)); 210 topo_mod_unregister(mod); 211 topo_mod_dprintf(mod, "_topo_fini: " 212 "%s enumerator uninitialized\n", DISK); 213 } 214