1184cd04cScth /*
2184cd04cScth * CDDL HEADER START
3184cd04cScth *
4184cd04cScth * The contents of this file are subject to the terms of the
5184cd04cScth * Common Development and Distribution License (the "License").
6184cd04cScth * You may not use this file except in compliance with the License.
7184cd04cScth *
8184cd04cScth * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9184cd04cScth * or http://www.opensolaris.org/os/licensing.
10184cd04cScth * See the License for the specific language governing permissions
11184cd04cScth * and limitations under the License.
12184cd04cScth *
13184cd04cScth * When distributing Covered Code, include this CDDL HEADER in each
14184cd04cScth * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15184cd04cScth * If applicable, add the following below this CDDL HEADER, with the
16184cd04cScth * fields enclosed by brackets "[]" replaced with your own identifying
17184cd04cScth * information: Portions Copyright [yyyy] [name of copyright owner]
18184cd04cScth *
19184cd04cScth * CDDL HEADER END
20184cd04cScth */
21184cd04cScth /*
22ac88567aSHyon Kim * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23184cd04cScth */
24aed5247fSJoshua M. Clulow /*
253c6ffbabSRob Johnston * Copyright 2020 Joyent, Inc.
26*533affcbSRobert Mustacchi * Copyright 2024 Oxide Computer Company
27aed5247fSJoshua M. Clulow */
28184cd04cScth
29184cd04cScth #include <strings.h>
30184cd04cScth #include <devid.h>
31184cd04cScth #include <pthread.h>
32184cd04cScth #include <inttypes.h>
33184cd04cScth #include <sys/dkio.h>
34184cd04cScth #include <sys/scsi/scsi_types.h>
35184cd04cScth #include <fm/topo_mod.h>
36184cd04cScth #include <fm/topo_list.h>
37184cd04cScth #include <fm/libdiskstatus.h>
38184cd04cScth #include <sys/fm/protocol.h>
39184cd04cScth #include "disk.h"
40aed5247fSJoshua M. Clulow #include "disk_drivers.h"
41184cd04cScth
42184cd04cScth static int disk_enum(topo_mod_t *, tnode_t *, const char *,
43184cd04cScth topo_instance_t, topo_instance_t, void *, void *);
44184cd04cScth
45184cd04cScth static const topo_modops_t disk_ops =
46184cd04cScth { disk_enum, NULL };
47184cd04cScth
48602ca9eaScth static const topo_modinfo_t disk_info =
49184cd04cScth {DISK, FM_FMRI_SCHEME_HC, DISK_VERSION, &disk_ops};
50184cd04cScth
51aed5247fSJoshua M. Clulow static int
disk_declare_driver(topo_mod_t * mod,tnode_t * baynode,topo_list_t * dlistp,char * driver)52aed5247fSJoshua M. Clulow disk_declare_driver(topo_mod_t *mod, tnode_t *baynode, topo_list_t *dlistp,
53aed5247fSJoshua M. Clulow char *driver)
54aed5247fSJoshua M. Clulow {
55aed5247fSJoshua M. Clulow int err;
56aed5247fSJoshua M. Clulow
573c6ffbabSRob Johnston if (strcmp(MPTSAS_DRV, driver) == 0) {
58aed5247fSJoshua M. Clulow char *sas_address = NULL;
59aed5247fSJoshua M. Clulow tnode_t *child = NULL;
60aed5247fSJoshua M. Clulow
61aed5247fSJoshua M. Clulow if ((err = disk_mptsas_find_disk(mod, baynode,
62aed5247fSJoshua M. Clulow &sas_address)) != 0)
63aed5247fSJoshua M. Clulow return (err);
64aed5247fSJoshua M. Clulow
65aed5247fSJoshua M. Clulow err = disk_declare_addr(mod, baynode, dlistp,
66aed5247fSJoshua M. Clulow sas_address, &child);
67aed5247fSJoshua M. Clulow topo_mod_strfree(mod, sas_address);
68aed5247fSJoshua M. Clulow
69aed5247fSJoshua M. Clulow return (err);
703c6ffbabSRob Johnston } else if (strcmp(NVME_DRV, driver) == 0) {
713c6ffbabSRob Johnston if (disk_nvme_enum_disk(mod, baynode) != 0)
723c6ffbabSRob Johnston return (-1);
733c6ffbabSRob Johnston
743c6ffbabSRob Johnston return (0);
75aed5247fSJoshua M. Clulow }
76aed5247fSJoshua M. Clulow
77aed5247fSJoshua M. Clulow topo_mod_dprintf(mod, "unknown disk driver '%s'\n", driver);
78aed5247fSJoshua M. Clulow return (-1);
79aed5247fSJoshua M. Clulow }
80aed5247fSJoshua M. Clulow
81602ca9eaScth /*ARGSUSED*/
82602ca9eaScth static int
disk_enum(topo_mod_t * mod,tnode_t * baynode,const char * name,topo_instance_t min,topo_instance_t max,void * arg,void * notused)83602ca9eaScth disk_enum(topo_mod_t *mod, tnode_t *baynode,
84602ca9eaScth const char *name, topo_instance_t min, topo_instance_t max,
85602ca9eaScth void *arg, void *notused)
86184cd04cScth {
87672fc84aSRobert Mustacchi char *device, *driver, *pname;
88184cd04cScth int err;
89*533affcbSRobert Mustacchi topo_disk_t *disk = topo_mod_getspecific(mod);
90*533affcbSRobert Mustacchi topo_list_t *dlistp = &disk->td_dlist;
91184cd04cScth
923c6ffbabSRob Johnston if (strcmp(name, DISK) != 0 && strcmp(name, NVME) != 0) {
933c6ffbabSRob Johnston topo_mod_dprintf(mod, "disk_enum: can't enumerate %s nodes - "
943c6ffbabSRob Johnston "only know how to enumerate %s and %s nodes.", name,
953c6ffbabSRob Johnston DISK, NVME);
96602ca9eaScth return (-1);
97184cd04cScth }
98184cd04cScth
99672fc84aSRobert Mustacchi /*
100672fc84aSRobert Mustacchi * Historically we've always set the parent FRU on nodes; however, it's
101672fc84aSRobert Mustacchi * not clear why. Certain node types like USB don't want this, so we
102672fc84aSRobert Mustacchi * only do this if the parent is actually a bay.
103672fc84aSRobert Mustacchi */
104672fc84aSRobert Mustacchi pname = topo_node_name(baynode);
105672fc84aSRobert Mustacchi if (strcmp(pname, BAY) == 0) {
106672fc84aSRobert Mustacchi nvlist_t *fmri;
107602ca9eaScth if (topo_node_resource(baynode, &fmri, &err) != 0) {
108602ca9eaScth topo_mod_dprintf(mod, "disk_enum: "
109672fc84aSRobert Mustacchi "topo_node_resource error %s\n",
110672fc84aSRobert Mustacchi topo_strerror(err));
111602ca9eaScth return (-1);
112184cd04cScth }
1133c6ffbabSRob Johnston /*
1143c6ffbabSRob Johnston * If the disk enumerator module has been run from an XML map
1153c6ffbabSRob Johnston * and the parent bay node was already created by an enumerator
1163c6ffbabSRob Johnston * module (e.g. ses), then the FRU will already be set.
1173c6ffbabSRob Johnston */
1183c6ffbabSRob Johnston if (topo_node_fru_set(baynode, fmri, 0, &err) != 0 &&
1193c6ffbabSRob Johnston err != ETOPO_PROP_DEFD) {
120602ca9eaScth topo_mod_dprintf(mod, "disk_enum: "
121602ca9eaScth "topo_node_fru error %s\n", topo_strerror(err));
122184cd04cScth nvlist_free(fmri);
123602ca9eaScth return (-1);
124184cd04cScth }
125184cd04cScth nvlist_free(fmri);
126672fc84aSRobert Mustacchi }
127184cd04cScth
128602ca9eaScth /*
129aed5247fSJoshua M. Clulow * For internal storage, first check to see if we need to
130aed5247fSJoshua M. Clulow * request more detail from an HBA driver.
131aed5247fSJoshua M. Clulow */
132aed5247fSJoshua M. Clulow if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
133aed5247fSJoshua M. Clulow TOPO_BINDING_DRIVER, &driver, &err) == 0) {
134aed5247fSJoshua M. Clulow err = disk_declare_driver(mod, baynode, dlistp, driver);
135aed5247fSJoshua M. Clulow
136aed5247fSJoshua M. Clulow topo_mod_strfree(mod, driver);
137aed5247fSJoshua M. Clulow return (err);
138aed5247fSJoshua M. Clulow } else if (err != ETOPO_PROP_NOENT) {
139aed5247fSJoshua M. Clulow topo_mod_dprintf(mod, "disk_enum: "
140aed5247fSJoshua M. Clulow "binding error %s\n", topo_strerror(err));
141aed5247fSJoshua M. Clulow return (-1);
142aed5247fSJoshua M. Clulow }
143aed5247fSJoshua M. Clulow
144aed5247fSJoshua M. Clulow /*
145602ca9eaScth * For internal storage, get the path to the occupant from the
146602ca9eaScth * binding group of the bay node
147602ca9eaScth */
148602ca9eaScth if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
149602ca9eaScth TOPO_BINDING_OCCUPANT, &device, &err) != 0) {
150602ca9eaScth topo_mod_dprintf(mod, "disk_enum: "
1513c6ffbabSRob Johnston "failed to lookup prop %s/%s: %s\n", TOPO_PGROUP_BINDING,
1523c6ffbabSRob Johnston TOPO_BINDING_OCCUPANT, topo_strerror(err));
153184cd04cScth return (-1);
154184cd04cScth }
155184cd04cScth
156184cd04cScth
157602ca9eaScth /* locate and topo enumerate the disk with that path */
158602ca9eaScth err = disk_declare_path(mod, baynode, dlistp, device);
159602ca9eaScth
160184cd04cScth topo_mod_strfree(mod, device);
161602ca9eaScth return (err);
162184cd04cScth }
163184cd04cScth
164184cd04cScth /*ARGSUSED*/
165184cd04cScth int
_topo_init(topo_mod_t * mod,topo_version_t version)166184cd04cScth _topo_init(topo_mod_t *mod, topo_version_t version)
167184cd04cScth {
168*533affcbSRobert Mustacchi topo_disk_t *disk;
169184cd04cScth
170184cd04cScth /*
171184cd04cScth * Turn on module debugging output
172184cd04cScth */
173184cd04cScth if (getenv("TOPODISKDEBUG") != NULL)
174184cd04cScth topo_mod_setdebug(mod);
175602ca9eaScth topo_mod_dprintf(mod, "_topo_init: "
176602ca9eaScth "initializing %s enumerator\n", DISK);
177184cd04cScth
178184cd04cScth if (topo_mod_register(mod, &disk_info, TOPO_VERSION) != 0) {
179602ca9eaScth topo_mod_dprintf(mod, "_topo_init: "
180602ca9eaScth "%s registration failed: %s\n", DISK, topo_mod_errmsg(mod));
181184cd04cScth return (-1); /* mod errno already set */
182184cd04cScth }
183184cd04cScth
184*533affcbSRobert Mustacchi if ((disk = topo_mod_zalloc(mod, sizeof (topo_disk_t))) == NULL) {
185*533affcbSRobert Mustacchi topo_mod_dprintf(mod, "_topo_init: failed to allocate "
186*533affcbSRobert Mustacchi "module data");
187184cd04cScth return (-1);
188184cd04cScth }
189602ca9eaScth
190*533affcbSRobert Mustacchi if ((disk->td_nvme = nvme_init()) == NULL) {
191*533affcbSRobert Mustacchi topo_mod_dprintf(mod, "_topo_init: failed to create libnvme "
192*533affcbSRobert Mustacchi "handle: %s", strerror(errno));
193*533affcbSRobert Mustacchi topo_mod_free(mod, disk, sizeof (topo_disk_t));
194184cd04cScth topo_mod_unregister(mod);
195*533affcbSRobert Mustacchi return (-1);
196*533affcbSRobert Mustacchi }
197*533affcbSRobert Mustacchi
198*533affcbSRobert Mustacchi if (dev_list_gather(mod, &disk->td_dlist) != 0) {
199*533affcbSRobert Mustacchi nvme_fini(disk->td_nvme);
200*533affcbSRobert Mustacchi topo_mod_free(mod, disk, sizeof (topo_disk_t));
201*533affcbSRobert Mustacchi topo_mod_unregister(mod);
202602ca9eaScth topo_mod_dprintf(mod, "_topo_init: "
203602ca9eaScth "failed to locate disks");
204184cd04cScth return (-1);
205184cd04cScth }
206184cd04cScth
207602ca9eaScth topo_mod_dprintf(mod, "_topo_init: "
208602ca9eaScth "%s enumerator initialized\n", DISK);
209184cd04cScth
210*533affcbSRobert Mustacchi topo_mod_setspecific(mod, disk);
211184cd04cScth
212184cd04cScth return (0);
213184cd04cScth }
214184cd04cScth
215184cd04cScth void
_topo_fini(topo_mod_t * mod)216184cd04cScth _topo_fini(topo_mod_t *mod)
217184cd04cScth {
218*533affcbSRobert Mustacchi topo_disk_t *disk = topo_mod_getspecific(mod);
219*533affcbSRobert Mustacchi dev_list_free(mod, &disk->td_dlist);
220*533affcbSRobert Mustacchi nvme_fini(disk->td_nvme);
221*533affcbSRobert Mustacchi topo_mod_free(mod, disk, sizeof (topo_disk_t));
222184cd04cScth topo_mod_unregister(mod);
223602ca9eaScth topo_mod_dprintf(mod, "_topo_fini: "
224602ca9eaScth "%s enumerator uninitialized\n", DISK);
225184cd04cScth }
226