xref: /illumos-gate/usr/src/lib/fm/topo/modules/i86pc/chip/chip_serial.c (revision d67944fbe3fa0b31893a7116a09b0718eecf6078)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <libnvpair.h>
33 #include <sys/types.h>
34 #include <libipmi.h>
35 #include <fm/topo_mod.h>
36 #include <ctype.h>
37 #include "chip.h"
38 
39 #define	BUFSZ	128
40 #define	JEDEC_TBL_SZ	4
41 
42 /*
43  * The following table maps DIMM manufacturer names to a JEDEC ID as sourced
44  * from JEDEC publication JEP106W.  This is (obviously) a sparse table which
45  * only contains entries for manufacturers whose DIMM's have been qualified
46  * for use on Sun platforms.
47  */
48 static const char *jedec_tbl[JEDEC_TBL_SZ][2] =
49 {
50 	{ "INFINEON", "00C1" },
51 	{ "MICRON TECHNOLOGY", "002C" },
52 	{ "QIMONDA", "7F51" },
53 	{ "SAMSUNG", "00CE" },
54 };
55 
56 static int
57 ipmi_serial_lookup(topo_mod_t *mod, char *ipmi_tag, char *buf)
58 {
59 	char *fru_data;
60 	int i, found_id = 0, serial_len;
61 	ipmi_handle_t *hdl;
62 	ipmi_sdr_fru_locator_t *fru_loc;
63 	ipmi_fru_prod_info_t prod_info;
64 
65 	topo_mod_dprintf(mod, "ipmi_serial_lookup() called\n");
66 	if ((hdl = topo_mod_ipmi_hold(mod)) == NULL) {
67 		topo_mod_dprintf(mod, "Failed to get IPMI handle\n");
68 		return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
69 	}
70 
71 	topo_mod_dprintf(mod, "Looking up FRU data for %s ...\n", ipmi_tag);
72 	if ((fru_loc = ipmi_sdr_lookup_fru(hdl, (const char *)ipmi_tag))
73 	    == NULL) {
74 		topo_mod_dprintf(mod, "Failed to lookup %s (%s)\n", ipmi_tag,
75 		    ipmi_errmsg(hdl));
76 		topo_mod_ipmi_rele(mod);
77 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
78 	}
79 
80 
81 	topo_mod_dprintf(mod, "Reading FRU data ...\n");
82 	if (ipmi_fru_read(hdl, fru_loc, &fru_data) < 0) {
83 		topo_mod_dprintf(mod, "Failed to read FRU data (%s)\n",
84 		    ipmi_errmsg(hdl));
85 		topo_mod_ipmi_rele(mod);
86 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
87 	}
88 
89 	topo_mod_dprintf(mod, "Parsing product info area ...\n");
90 	if (ipmi_fru_parse_product(hdl, fru_data, &prod_info) < 0) {
91 		topo_mod_dprintf(mod, "Failed to read FRU product info (%s)\n",
92 		    ipmi_errmsg(hdl));
93 		free(fru_data);
94 		topo_mod_ipmi_rele(mod);
95 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
96 	}
97 	free(fru_data);
98 	topo_mod_ipmi_rele(mod);
99 
100 	topo_mod_dprintf(mod, "FRU Product Serial: %s\n",
101 	    prod_info.ifpi_product_serial);
102 	topo_mod_dprintf(mod, "Manufacturer Name: \"%s\"\n",
103 	    prod_info.ifpi_manuf_name);
104 
105 	serial_len = strnlen(prod_info.ifpi_product_serial, FRU_INFO_MAXLEN);
106 
107 	/*
108 	 * Newer ILOM software that has the fix for CR 6607996 will have
109 	 * an 18-character serial number that has been synthesized using
110 	 * the recipe from the Sun SPD JEDEC DIMM specification.  If we
111 	 * find an 18-character then we'll simply use it, as-is, and
112 	 * return.
113 	 */
114 	if (serial_len == 18) {
115 		(void) memcpy(buf, prod_info.ifpi_product_serial, 18);
116 		*(buf+18) = '\0';
117 		return (0);
118 	}
119 	/*
120 	 * Older ILOM software that DOESN'T have the fix for CR 6607996 will
121 	 * only provide the 8 character manufacturer serial number.
122 	 *
123 	 * However, if for some reason the product info area doesn't have the
124 	 * serial information or if the serial isn't 8 characters (we may
125 	 * encounter SP's that don't populate the serial field or are buggy and
126 	 * populate it with garbage), then we'll stop right now and just set the
127 	 * buf to an empty string.
128 	 */
129 	if (serial_len != 8) {
130 		*buf = '\0';
131 		return (0);
132 	}
133 
134 	/*
135 	 * What follows is a very crude adaptation of the recipe from the
136 	 * Sun SPD JEDEC DIMM specification for synthesizing globally unique
137 	 * serial numbers from the 8 character manufacturer serial number.
138 	 *
139 	 * The Sun serial number takes the following form:
140 	 *
141 	 * jjjjllyywwssssssss
142 	 *
143 	 * The components are:
144 	 *
145 	 * yyyy: JEDEC ID in hex (2 byte manufacture ID, 2 byte continuation
146 	 * 		code).
147 	 *
148 	 * ll:   The memory module's manufacturing location.
149 	 *
150 	 * yyww: The module's manufacturing date (2-digit year/2-digit week)
151 	 *
152 	 * ssssssss: The 8 character maufacturer serial number
153 	 */
154 	/*
155 	 * First we need to normalize the manufacturer name we pulled out of
156 	 * the FRU product info area.  Our normalization algorithm is fairly
157 	 * simple:
158 	 *   - convert all alpha chars to uppercase
159 	 *   - convert non-alphanumeric characters to a single space
160 	 *
161 	 * We use the normalized name to lookup the JEDEC ID from a static
162 	 * table.  If the FRU area didn't have a manufacturer name or if the ID
163 	 * lookup fails we'll set jjjj to 0000.
164 	 */
165 	for (i = 0; prod_info.ifpi_manuf_name[i]; i++) {
166 		prod_info.ifpi_manuf_name[i] =
167 		    toupper(prod_info.ifpi_manuf_name[i]);
168 		if (!isalpha(prod_info.ifpi_manuf_name[i]) &&
169 		    !isdigit(prod_info.ifpi_manuf_name[i]))
170 			prod_info.ifpi_manuf_name[i] = (char)0x20;
171 	}
172 	topo_mod_dprintf(mod, "Normalized Manufacturer Name \"%s\"\n",
173 	    prod_info.ifpi_manuf_name);
174 
175 	for (i = 0; i < JEDEC_TBL_SZ; i++)
176 		if (strcmp(prod_info.ifpi_manuf_name, jedec_tbl[i][0]) == 0) {
177 			found_id = 1;
178 			break;
179 		}
180 
181 	if (found_id)
182 		(void) memcpy(buf, jedec_tbl[i][1], 4);
183 	else
184 		(void) memcpy(buf, (char *)("0000"), 4);
185 
186 	/*
187 	 * The manufacturing location and date is not available via IPMI on
188 	 * Sun platforms, so we simply set these six digits to zeros.
189 	 */
190 	(void) memcpy((buf+4), (char *)("000000"), 6);
191 
192 	/*
193 	 * Finally, we just copy the 8 character product serial straight over
194 	 * and then NULL terminate the string.
195 	 */
196 	(void) memcpy((buf+10), prod_info.ifpi_product_serial, 8);
197 	*(buf+18) = '\0';
198 
199 	return (0);
200 }
201 
202 /* ARGSUSED */
203 int
204 get_dimm_serial(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
205     nvlist_t *in, nvlist_t **out)
206 {
207 	char *fmtstr, ipmi_tag[BUFSZ], fru_serial[FRU_INFO_MAXLEN];
208 	tnode_t *chip;
209 	int ret;
210 	uint32_t offset;
211 	nvlist_t *args;
212 
213 	topo_mod_dprintf(mod, "get_dimm_serial() called\n");
214 	if ((ret = nvlist_lookup_nvlist(in, "args", &args)) != 0) {
215 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
216 		    strerror(ret));
217 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
218 	}
219 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
220 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
221 		    strerror(ret));
222 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
223 	}
224 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
225 		/* topo errno set */
226 		topo_mod_dprintf(mod, "Failed to retrieve format arg\n");
227 		return (-1);
228 	}
229 
230 	chip = topo_node_parent(topo_node_parent(node));
231 
232 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
233 	(void) snprintf(ipmi_tag, BUFSZ, fmtstr, topo_node_instance(chip),
234 	    (topo_node_instance(node) + offset));
235 
236 	if (ipmi_serial_lookup(mod, ipmi_tag, fru_serial) != 0) {
237 		topo_mod_dprintf(mod, "Failed to lookup serial for %s\n",
238 		    ipmi_tag);
239 		(void) strcpy(fru_serial, "");
240 	}
241 
242 	if (store_prop_val(mod, fru_serial, "serial", out) != 0) {
243 		topo_mod_dprintf(mod, "Failed to set serial\n");
244 		/* topo errno already set */
245 		return (-1);
246 	}
247 	return (0);
248 }
249 
250 /* ARGSUSED */
251 int
252 get_cs_serial(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
253     nvlist_t *in, nvlist_t **out)
254 {
255 	char *fmtstr, ipmi_tag[BUFSZ], fru_serial[FRU_INFO_MAXLEN];
256 	tnode_t *chip, *chan;
257 	int ret, dimm_num;
258 	uint32_t offset;
259 	nvlist_t *args;
260 
261 	topo_mod_dprintf(mod, "get_cs_serial() called\n");
262 	if ((ret = nvlist_lookup_nvlist(in, "args", &args)) != 0) {
263 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
264 		    strerror(ret));
265 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
266 	}
267 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
268 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
269 		    strerror(ret));
270 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
271 	}
272 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
273 		/* topo errno set */
274 		topo_mod_dprintf(mod, "Failed to retrieve format arg\n");
275 		return (-1);
276 	}
277 
278 	chip = topo_node_parent(topo_node_parent(topo_node_parent(node)));
279 	chan = topo_node_parent(node);
280 
281 	dimm_num = topo_node_instance(node) - (topo_node_instance(node) % 2)
282 	    + topo_node_instance(chan) + offset;
283 
284 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
285 	(void) snprintf(ipmi_tag, BUFSZ, fmtstr, topo_node_instance(chip),
286 	    dimm_num);
287 
288 	if (ipmi_serial_lookup(mod, ipmi_tag, fru_serial) != 0) {
289 		topo_mod_dprintf(mod, "Failed to lookup serial for %s\n",
290 		    ipmi_tag);
291 		(void) strcpy(fru_serial, "");
292 	}
293 
294 	if (store_prop_val(mod, fru_serial, "serial", out) != 0) {
295 		topo_mod_dprintf(mod, "Failed to set serial\n");
296 		/* topo errno already set */
297 		return (-1);
298 	}
299 	return (0);
300 }
301