summaryrefslogtreecommitdiff
path: root/usr/src/lib/fm/topo/modules/common/disk/disk.c
blob: 7663ffc8fbbaf834906c4bdd34c40af29f5f8efe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 */
/*
 * Copyright 2020 Joyent, Inc.
 */

#include <strings.h>
#include <devid.h>
#include <pthread.h>
#include <inttypes.h>
#include <sys/dkio.h>
#include <sys/scsi/scsi_types.h>
#include <fm/topo_mod.h>
#include <fm/topo_list.h>
#include <fm/libdiskstatus.h>
#include <sys/fm/protocol.h>
#include "disk.h"
#include "disk_drivers.h"

static int disk_enum(topo_mod_t *, tnode_t *, const char *,
	topo_instance_t, topo_instance_t, void *, void *);

static const topo_modops_t disk_ops =
	{ disk_enum, NULL };

static const topo_modinfo_t disk_info =
	{DISK, FM_FMRI_SCHEME_HC, DISK_VERSION, &disk_ops};

static int
disk_declare_driver(topo_mod_t *mod, tnode_t *baynode, topo_list_t *dlistp,
    char *driver)
{
	int err;

	if (strcmp(MPTSAS_DRV, driver) == 0) {
		char *sas_address = NULL;
		tnode_t *child = NULL;

		if ((err = disk_mptsas_find_disk(mod, baynode,
		    &sas_address)) != 0)
			return (err);

		err = disk_declare_addr(mod, baynode, dlistp,
		    sas_address, &child);
		topo_mod_strfree(mod, sas_address);

		return (err);
	} else if (strcmp(NVME_DRV, driver) == 0) {
		if (disk_nvme_enum_disk(mod, baynode) != 0)
			return (-1);

		return (0);
	}

	topo_mod_dprintf(mod, "unknown disk driver '%s'\n", driver);
	return (-1);
}

/*ARGSUSED*/
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)
{
	char		*device, *driver, *pname;
	int		err;
	topo_list_t	*dlistp = topo_mod_getspecific(mod);

	if (strcmp(name, DISK) != 0 && strcmp(name, NVME) != 0) {
		topo_mod_dprintf(mod, "disk_enum: can't enumerate %s nodes - "
		    "only know how to enumerate %s and %s nodes.", name,
		    DISK, NVME);
		return (-1);
	}

	/*
	 * Historically we've always set the parent FRU on nodes; however, it's
	 * not clear why. Certain node types like USB don't want this, so we
	 * only do this if the parent is actually a bay.
	 */
	pname = topo_node_name(baynode);
	if (strcmp(pname, BAY) == 0) {
		nvlist_t	*fmri;
		if (topo_node_resource(baynode, &fmri, &err) != 0) {
			topo_mod_dprintf(mod, "disk_enum: "
			    "topo_node_resource error %s\n",
			    topo_strerror(err));
			return (-1);
		}
		/*
		 * If the disk enumerator module has been run from an XML map
		 * and the parent bay node was already created by an enumerator
		 * module (e.g. ses), then the FRU will already be set.
		 */
		if (topo_node_fru_set(baynode, fmri, 0, &err) != 0 &&
		    err != ETOPO_PROP_DEFD) {
			topo_mod_dprintf(mod, "disk_enum: "
			    "topo_node_fru error %s\n", topo_strerror(err));
			nvlist_free(fmri);
			return (-1);
		}
		nvlist_free(fmri);
	}

	/*
	 * For internal storage, first check to see if we need to
	 * request more detail from an HBA driver.
	 */
	if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
	    TOPO_BINDING_DRIVER, &driver, &err) == 0) {
		err = disk_declare_driver(mod, baynode, dlistp, driver);

		topo_mod_strfree(mod, driver);
		return (err);
	} else if (err != ETOPO_PROP_NOENT) {
		topo_mod_dprintf(mod, "disk_enum: "
		    "binding error %s\n", topo_strerror(err));
		return (-1);
	}

	/*
	 * For internal storage, get the path to the occupant from the
	 * binding group of the bay node
	 */
	if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
	    TOPO_BINDING_OCCUPANT, &device, &err) != 0) {
		topo_mod_dprintf(mod, "disk_enum: "
		    "failed to lookup prop %s/%s: %s\n", TOPO_PGROUP_BINDING,
		    TOPO_BINDING_OCCUPANT, topo_strerror(err));
		return (-1);
	}


	/* locate and topo enumerate the disk with that path */
	err = disk_declare_path(mod, baynode, dlistp, device);

	topo_mod_strfree(mod, device);
	return (err);
}

/*ARGSUSED*/
int
_topo_init(topo_mod_t *mod, topo_version_t version)
{
	topo_list_t *dlistp;

	/*
	 * Turn on module debugging output
	 */
	if (getenv("TOPODISKDEBUG") != NULL)
		topo_mod_setdebug(mod);
	topo_mod_dprintf(mod, "_topo_init: "
	    "initializing %s enumerator\n", DISK);

	if (topo_mod_register(mod, &disk_info, TOPO_VERSION) != 0) {
		topo_mod_dprintf(mod, "_topo_init: "
		    "%s registration failed: %s\n", DISK, topo_mod_errmsg(mod));
		return (-1);		/* mod errno already set */
	}

	if ((dlistp = topo_mod_zalloc(mod, sizeof (topo_list_t))) == NULL) {
		topo_mod_dprintf(mod, "_topo_inti: failed to allocate "
		    "disk list");
		return (-1);
	}

	if (dev_list_gather(mod, dlistp) != 0) {
		topo_mod_unregister(mod);
		topo_mod_free(mod, dlistp, sizeof (topo_list_t));
		topo_mod_dprintf(mod, "_topo_init: "
		    "failed to locate disks");
		return (-1);
	}

	topo_mod_dprintf(mod, "_topo_init: "
	    "%s enumerator initialized\n", DISK);

	topo_mod_setspecific(mod, dlistp);

	return (0);
}

void
_topo_fini(topo_mod_t *mod)
{
	topo_list_t *dlistp = topo_mod_getspecific(mod);
	dev_list_free(mod, dlistp);
	topo_mod_free(mod, dlistp, sizeof (topo_list_t));
	topo_mod_unregister(mod);
	topo_mod_dprintf(mod, "_topo_fini: "
	    "%s enumerator uninitialized\n", DISK);
}