summaryrefslogtreecommitdiff
path: root/usr/src/lib/fm/topo/modules/i86pc/chip/chip_serial.c
blob: 87b152c237e7244c79b5d1be344c7e975578cbd2 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * Copyright 2019 Joyent, Inc.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <libnvpair.h>
#include <sys/types.h>
#include <libipmi.h>
#include <fm/topo_mod.h>
#include <ctype.h>
#include "chip.h"

#define	BUFSZ	128
#define	JEDEC_TBL_SZ	5

/*
 * The following table maps DIMM manufacturer names to a JEDEC ID as sourced
 * from JEDEC publication JEP106W.  This is (obviously) a sparse table which
 * only contains entries for manufacturers whose DIMM's have been qualified
 * for use on Sun platforms.
 */
static const char *jedec_tbl[JEDEC_TBL_SZ][2] =
{
	{ "HYUNDAI ELECTRONICS", "00AD" },
	{ "INFINEON", "00C1" },
	{ "MICRON TECHNOLOGY", "002C" },
	{ "QIMONDA", "7F51" },
	{ "SAMSUNG", "00CE" },
};

static int
ipmi_serial_lookup(topo_mod_t *mod, char *ipmi_tag, char *buf)
{
	char *fru_data;
	int i, found_id = 0, serial_len;
	ipmi_handle_t *hdl;
	ipmi_sdr_fru_locator_t *fru_loc;
	ipmi_fru_prod_info_t prod_info;

	topo_mod_dprintf(mod, "ipmi_serial_lookup() called\n");
	if ((hdl = topo_mod_ipmi_hold(mod)) == NULL) {
		topo_mod_dprintf(mod, "Failed to get IPMI handle\n");
		return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
	}

	topo_mod_dprintf(mod, "Looking up FRU data for %s ...\n", ipmi_tag);
	if ((fru_loc = ipmi_sdr_lookup_fru(hdl, (const char *)ipmi_tag))
	    == NULL) {
		topo_mod_dprintf(mod, "Failed to lookup %s (%s)\n", ipmi_tag,
		    ipmi_errmsg(hdl));
		topo_mod_ipmi_rele(mod);
		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
	}


	topo_mod_dprintf(mod, "Reading FRU data ...\n");
	if (ipmi_fru_read(hdl, fru_loc, &fru_data) < 0) {
		topo_mod_dprintf(mod, "Failed to read FRU data (%s)\n",
		    ipmi_errmsg(hdl));
		topo_mod_ipmi_rele(mod);
		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
	}

	topo_mod_dprintf(mod, "Parsing product info area ...\n");
	if (ipmi_fru_parse_product(hdl, fru_data, &prod_info) < 0) {
		topo_mod_dprintf(mod, "Failed to read FRU product info (%s)\n",
		    ipmi_errmsg(hdl));
		free(fru_data);
		topo_mod_ipmi_rele(mod);
		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
	}
	free(fru_data);
	topo_mod_ipmi_rele(mod);

	topo_mod_dprintf(mod, "FRU Product Serial: %s\n",
	    prod_info.ifpi_product_serial);
	topo_mod_dprintf(mod, "Manufacturer Name: \"%s\"\n",
	    prod_info.ifpi_manuf_name);

	serial_len = strnlen(prod_info.ifpi_product_serial, FRU_INFO_MAXLEN);

	/*
	 * Newer ILOM software that has the fix for CR 6607996 will have
	 * an 18-character serial number that has been synthesized using
	 * the recipe from the Sun SPD JEDEC DIMM specification.  If we
	 * find an 18-character then we'll simply use it, as-is, and
	 * return.
	 */
	if (serial_len == 18) {
		(void) memcpy(buf, prod_info.ifpi_product_serial, 18);
		*(buf+18) = '\0';
		return (0);
	}
	/*
	 * Older ILOM software that DOESN'T have the fix for CR 6607996 will
	 * only provide the 8 character manufacturer serial number.
	 *
	 * However, if for some reason the product info area doesn't have the
	 * serial information or if the serial isn't 8 characters (we may
	 * encounter SP's that don't populate the serial field or are buggy and
	 * populate it with garbage), then we'll stop right now and just set the
	 * buf to an empty string.
	 */
	if (serial_len != 8) {
		*buf = '\0';
		return (0);
	}

	/*
	 * What follows is a very crude adaptation of the recipe from the
	 * Sun SPD JEDEC DIMM specification for synthesizing globally unique
	 * serial numbers from the 8 character manufacturer serial number.
	 *
	 * The Sun serial number takes the following form:
	 *
	 * jjjjllyywwssssssss
	 *
	 * The components are:
	 *
	 * yyyy: JEDEC ID in hex (2 byte manufacture ID, 2 byte continuation
	 *	code).
	 *
	 * ll:   The memory module's manufacturing location.
	 *
	 * yyww: The module's manufacturing date (2-digit year/2-digit week)
	 *
	 * ssssssss: The 8 character maufacturer serial number
	 */
	/*
	 * First we need to normalize the manufacturer name we pulled out of
	 * the FRU product info area.  Our normalization algorithm is fairly
	 * simple:
	 *   - convert all alpha chars to uppercase
	 *   - convert non-alphanumeric characters to a single space
	 *
	 * We use the normalized name to lookup the JEDEC ID from a static
	 * table.  If the FRU area didn't have a manufacturer name or if the ID
	 * lookup fails we'll set jjjj to 0000.
	 */
	for (i = 0; prod_info.ifpi_manuf_name[i]; i++) {
		prod_info.ifpi_manuf_name[i] =
		    toupper(prod_info.ifpi_manuf_name[i]);
		if (!isalpha(prod_info.ifpi_manuf_name[i]) &&
		    !isdigit(prod_info.ifpi_manuf_name[i]))
			prod_info.ifpi_manuf_name[i] = (char)0x20;
	}
	topo_mod_dprintf(mod, "Normalized Manufacturer Name \"%s\"\n",
	    prod_info.ifpi_manuf_name);

	for (i = 0; i < JEDEC_TBL_SZ; i++)
		if (strcmp(prod_info.ifpi_manuf_name, jedec_tbl[i][0]) == 0) {
			found_id = 1;
			break;
		}

	if (found_id)
		(void) memcpy(buf, jedec_tbl[i][1], 4);
	else
		(void) memcpy(buf, (char *)("0000"), 4);

	/*
	 * The manufacturing location and date is not available via IPMI on
	 * Sun platforms, so we simply set these six digits to zeros.
	 */
	(void) memcpy((buf+4), (char *)("000000"), 6);

	/*
	 * Finally, we just copy the 8 character product serial straight over
	 * and then NULL terminate the string.
	 */
	(void) memcpy((buf+10), prod_info.ifpi_product_serial, 8);
	*(buf+18) = '\0';

	return (0);
}

/* ARGSUSED */
int
get_dimm_serial(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
    nvlist_t *in, nvlist_t **out)
{
	char **entity_refs, fru_serial[FRU_INFO_MAXLEN];
	int err, rv = 0, i;
	uint_t nelems;
	boolean_t found_serial = B_FALSE;

	if (topo_prop_get_string_array(node, TOPO_PGROUP_IPMI, "entity_ref",
	    &entity_refs, &nelems, &err) != 0) {
		topo_mod_dprintf(mod, "%s: Failed to lookup entity_ref property"
		    " (%s)", __func__, topo_strerror(err));
		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
	}

	for (i = 0; i < nelems; i++) {
		if (ipmi_serial_lookup(mod, entity_refs[i], fru_serial) == 0) {
			found_serial = B_TRUE;
			break;
		} else
			topo_mod_dprintf(mod, "Failed to lookup serial for "
			    "%s\n", entity_refs[i]);
	}
	if (! found_serial)
		(void) strcpy(fru_serial, "");

	if (store_prop_val(mod, fru_serial, "serial", out) != 0) {
		topo_mod_dprintf(mod, "Failed to set serial\n");
		/* topo errno already set */
		rv = -1;
	}
	topo_mod_strfreev(mod, entity_refs, nelems);

	return (rv);
}