xref: /illumos-gate/usr/src/uts/intel/io/devfm_machdep.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright (c) 2018, Joyent, Inc.
25  */
26 
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 
31 #include <sys/fm/protocol.h>
32 #include <sys/fm/smb/fmsmb.h>
33 #include <sys/devfm.h>
34 
35 #include <sys/cpu_module.h>
36 
37 #define	ANY_ID		(uint_t)-1
38 
39 /*
40  * INIT_HDLS is the initial size of cmi_hdl_t array.  We fill the array
41  * during cmi_hdl_walk, if the array overflows, we will reallocate
42  * a new array twice the size of the old one.
43  */
44 #define	INIT_HDLS	16
45 
46 typedef struct fm_cmi_walk_t
47 {
48 	uint_t	chipid;		/* chipid to match during walk */
49 	uint_t	coreid;		/* coreid to match */
50 	uint_t	strandid;	/* strandid to match */
51 	int	(*cbfunc)(cmi_hdl_t, void *, void *);  	/* callback function */
52 	cmi_hdl_t *hdls;	/* allocated array to save the handles */
53 	int	nhdl_max;	/* allocated array size */
54 	int	nhdl;		/* handles saved */
55 } fm_cmi_walk_t;
56 
57 extern int x86gentopo_legacy;
58 
59 int
60 fm_get_paddr(nvlist_t *nvl, uint64_t *paddr)
61 {
62 	uint8_t version;
63 	uint64_t pa;
64 	char *scheme;
65 	int err;
66 
67 	/* Verify FMRI scheme name and version number */
68 	if ((nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &scheme) != 0) ||
69 	    (strcmp(scheme, FM_FMRI_SCHEME_HC) != 0) ||
70 	    (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0) ||
71 	    version > FM_HC_SCHEME_VERSION) {
72 		return (EINVAL);
73 	}
74 
75 	if ((err = cmi_mc_unumtopa(NULL, nvl, &pa)) != CMI_SUCCESS &&
76 	    err != CMIERR_MC_PARTIALUNUMTOPA)
77 		return (EINVAL);
78 
79 	*paddr = pa;
80 	return (0);
81 }
82 
83 /*
84  * Routines for cmi handles walk.
85  */
86 
87 static void
88 walk_init(fm_cmi_walk_t *wp, uint_t chipid, uint_t coreid, uint_t strandid,
89     int (*cbfunc)(cmi_hdl_t, void *, void *))
90 {
91 	wp->chipid = chipid;
92 	wp->coreid = coreid;
93 	wp->strandid = strandid;
94 	/*
95 	 * If callback is not set, we allocate an array to save the
96 	 * cmi handles.
97 	 */
98 	if ((wp->cbfunc = cbfunc) == NULL) {
99 		wp->hdls = kmem_alloc(sizeof (cmi_hdl_t) * INIT_HDLS, KM_SLEEP);
100 		wp->nhdl_max = INIT_HDLS;
101 		wp->nhdl = 0;
102 	}
103 }
104 
105 static void
106 walk_fini(fm_cmi_walk_t *wp)
107 {
108 	if (wp->cbfunc == NULL)
109 		kmem_free(wp->hdls, sizeof (cmi_hdl_t) * wp->nhdl_max);
110 }
111 
112 static int
113 select_cmi_hdl(cmi_hdl_t hdl, void *arg1, void *arg2, void *arg3)
114 {
115 	fm_cmi_walk_t *wp = (fm_cmi_walk_t *)arg1;
116 
117 	if (wp->chipid != ANY_ID && wp->chipid != cmi_hdl_chipid(hdl))
118 		return (CMI_HDL_WALK_NEXT);
119 	if (wp->coreid != ANY_ID && wp->coreid != cmi_hdl_coreid(hdl))
120 		return (CMI_HDL_WALK_NEXT);
121 	if (wp->strandid != ANY_ID && wp->strandid != cmi_hdl_strandid(hdl))
122 		return (CMI_HDL_WALK_NEXT);
123 
124 	/*
125 	 * Call the callback function if any exists, otherwise we hold a
126 	 * reference of the handle and push it to preallocated array.
127 	 * If the allocated array is going to overflow, reallocate a
128 	 * bigger one to replace it.
129 	 */
130 	if (wp->cbfunc != NULL)
131 		return (wp->cbfunc(hdl, arg2, arg3));
132 
133 	if (wp->nhdl == wp->nhdl_max) {
134 		size_t sz = sizeof (cmi_hdl_t) * wp->nhdl_max;
135 		cmi_hdl_t *newarray = kmem_alloc(sz << 1, KM_SLEEP);
136 
137 		bcopy(wp->hdls, newarray, sz);
138 		kmem_free(wp->hdls, sz);
139 		wp->hdls = newarray;
140 		wp->nhdl_max <<= 1;
141 	}
142 
143 	cmi_hdl_hold(hdl);
144 	wp->hdls[wp->nhdl++] = hdl;
145 
146 	return (CMI_HDL_WALK_NEXT);
147 }
148 
149 static void
150 populate_cpu(nvlist_t **nvlp, cmi_hdl_t hdl)
151 {
152 	uint_t	fm_chipid;
153 	uint16_t smbios_id;
154 	const char *idstr;
155 
156 	(void) nvlist_alloc(nvlp, NV_UNIQUE_NAME, KM_SLEEP);
157 
158 	/*
159 	 * If SMBIOS satisfies FMA Topology needs, gather
160 	 * more information on the chip's physical roots
161 	 * like /chassis=x/motherboard=y/cpuboard=z and
162 	 * set the chip_id to match the SMBIOS' Type 4
163 	 * ordering & this has to match the ereport's chip
164 	 * resource instance derived off of SMBIOS.
165 	 * Multi-Chip-Module support should set the chipid
166 	 * in terms of the processor package rather than
167 	 * the die/node in the processor package, for FM.
168 	 */
169 
170 	if (!x86gentopo_legacy) {
171 		smbios_id = cmi_hdl_smbiosid(hdl);
172 		fm_chipid = cmi_hdl_smb_chipid(hdl);
173 		(void) nvlist_add_nvlist(*nvlp, FM_PHYSCPU_INFO_CHIP_ROOTS,
174 		    cmi_hdl_smb_bboard(hdl));
175 		(void) nvlist_add_uint16(*nvlp, FM_PHYSCPU_INFO_SMBIOS_ID,
176 		    (uint16_t)smbios_id);
177 	} else
178 		fm_chipid = cmi_hdl_chipid(hdl);
179 
180 	fm_payload_set(*nvlp,
181 	    FM_PHYSCPU_INFO_VENDOR_ID, DATA_TYPE_STRING,
182 	    cmi_hdl_vendorstr(hdl),
183 	    FM_PHYSCPU_INFO_FAMILY, DATA_TYPE_INT32,
184 	    (int32_t)cmi_hdl_family(hdl),
185 	    FM_PHYSCPU_INFO_MODEL, DATA_TYPE_INT32,
186 	    (int32_t)cmi_hdl_model(hdl),
187 	    FM_PHYSCPU_INFO_STEPPING, DATA_TYPE_INT32,
188 	    (int32_t)cmi_hdl_stepping(hdl),
189 	    FM_PHYSCPU_INFO_CHIP_ID, DATA_TYPE_INT32,
190 	    (int32_t)fm_chipid,
191 	    FM_PHYSCPU_INFO_NPROCNODES, DATA_TYPE_INT32,
192 	    (int32_t)cmi_hdl_procnodes_per_pkg(hdl),
193 	    FM_PHYSCPU_INFO_PROCNODE_ID, DATA_TYPE_INT32,
194 	    (int32_t)cmi_hdl_procnodeid(hdl),
195 	    FM_PHYSCPU_INFO_CORE_ID, DATA_TYPE_INT32,
196 	    (int32_t)cmi_hdl_coreid(hdl),
197 	    FM_PHYSCPU_INFO_STRAND_ID, DATA_TYPE_INT32,
198 	    (int32_t)cmi_hdl_strandid(hdl),
199 	    FM_PHYSCPU_INFO_STRAND_APICID, DATA_TYPE_INT32,
200 	    (int32_t)cmi_hdl_strand_apicid(hdl),
201 	    FM_PHYSCPU_INFO_CHIP_REV, DATA_TYPE_STRING,
202 	    cmi_hdl_chiprevstr(hdl),
203 	    FM_PHYSCPU_INFO_SOCKET_TYPE, DATA_TYPE_UINT32,
204 	    (uint32_t)cmi_hdl_getsockettype(hdl),
205 	    FM_PHYSCPU_INFO_CPU_ID, DATA_TYPE_INT32,
206 	    (int32_t)cmi_hdl_logical_id(hdl),
207 	    NULL);
208 
209 	/*
210 	 * Do this separately so that way if there is no ident string we do not
211 	 * trigger an error.
212 	 */
213 	if ((idstr = cmi_hdl_chipident(hdl)) != NULL) {
214 		fm_payload_set(*nvlp,
215 		    FM_PHYSCPU_INFO_CHIP_IDENTSTR, DATA_TYPE_STRING, idstr,
216 		    NULL);
217 	}
218 }
219 
220 /*ARGSUSED*/
221 int
222 fm_ioctl_physcpu_info(int cmd, nvlist_t *invl, nvlist_t **onvlp)
223 {
224 	nvlist_t **cpus, *nvl;
225 	int i, err;
226 	fm_cmi_walk_t wk;
227 
228 	/*
229 	 * Do a walk to save all the cmi handles in the array.
230 	 */
231 	walk_init(&wk, ANY_ID, ANY_ID, ANY_ID, NULL);
232 	cmi_hdl_walk(select_cmi_hdl, &wk, NULL, NULL);
233 
234 	if (wk.nhdl == 0) {
235 		walk_fini(&wk);
236 		return (ENOENT);
237 	}
238 
239 	cpus = kmem_alloc(sizeof (nvlist_t *) * wk.nhdl, KM_SLEEP);
240 	for (i = 0; i < wk.nhdl; i++) {
241 		populate_cpu(cpus + i, wk.hdls[i]);
242 		cmi_hdl_rele(wk.hdls[i]);
243 	}
244 
245 	walk_fini(&wk);
246 
247 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
248 	err = nvlist_add_nvlist_array(nvl, FM_PHYSCPU_INFO_CPUS,
249 	    cpus, wk.nhdl);
250 
251 	for (i = 0; i < wk.nhdl; i++)
252 		nvlist_free(cpus[i]);
253 	kmem_free(cpus, sizeof (nvlist_t *) * wk.nhdl);
254 
255 	if (err != 0) {
256 		nvlist_free(nvl);
257 		return (err);
258 	}
259 
260 	*onvlp = nvl;
261 	return (0);
262 }
263 
264 int
265 fm_ioctl_cpu_retire(int cmd, nvlist_t *invl, nvlist_t **onvlp)
266 {
267 	int32_t chipid, coreid, strandid;
268 	int rc, new_status, old_status;
269 	cmi_hdl_t hdl;
270 	nvlist_t *nvl;
271 
272 	switch (cmd) {
273 	case FM_IOC_CPU_RETIRE:
274 		new_status = P_FAULTED;
275 		break;
276 	case FM_IOC_CPU_STATUS:
277 		new_status = P_STATUS;
278 		break;
279 	case FM_IOC_CPU_UNRETIRE:
280 		new_status = P_ONLINE;
281 		break;
282 	default:
283 		return (ENOTTY);
284 	}
285 
286 	if (nvlist_lookup_int32(invl, FM_CPU_RETIRE_CHIP_ID, &chipid) != 0 ||
287 	    nvlist_lookup_int32(invl, FM_CPU_RETIRE_CORE_ID, &coreid) != 0 ||
288 	    nvlist_lookup_int32(invl, FM_CPU_RETIRE_STRAND_ID, &strandid) != 0)
289 		return (EINVAL);
290 
291 	hdl = cmi_hdl_lookup(CMI_HDL_NEUTRAL, chipid, coreid, strandid);
292 	if (hdl == NULL)
293 		return (EINVAL);
294 
295 	rc = cmi_hdl_online(hdl, new_status, &old_status);
296 	cmi_hdl_rele(hdl);
297 
298 	if (rc == 0) {
299 		(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
300 		(void) nvlist_add_int32(nvl, FM_CPU_RETIRE_OLDSTATUS,
301 		    old_status);
302 		*onvlp = nvl;
303 	}
304 
305 	return (rc);
306 }
307 
308 /*
309  * Retrun the value of x86gentopo_legacy variable as an nvpair.
310  *
311  * The caller is responsible for freeing the nvlist.
312  */
313 /* ARGSUSED */
314 int
315 fm_ioctl_gentopo_legacy(int cmd, nvlist_t *invl, nvlist_t **onvlp)
316 {
317 	nvlist_t *nvl;
318 
319 	if (cmd != FM_IOC_GENTOPO_LEGACY) {
320 		return (ENOTTY);
321 	}
322 
323 	/*
324 	 * Inform the caller of the intentions of the ereport generators to
325 	 * generate either a "generic" or "legacy" x86 topology.
326 	 */
327 
328 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
329 	(void) nvlist_add_int32(nvl, FM_GENTOPO_LEGACY, x86gentopo_legacy);
330 	*onvlp = nvl;
331 
332 	return (0);
333 }
334