vpd.c revision 112553
1/*-
2 * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/i386/bios/vpd.c 112553 2003-03-24 19:32:57Z mdodd $
27 */
28
29/*
30 * VPD decoder for IBM systems (Thinkpads)
31 * http://www-1.ibm.com/support/docview.wss?uid=psg1MIGR-45120
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/socket.h>
38#include <sys/sysctl.h>
39
40#include <sys/module.h>
41#include <sys/bus.h>
42
43#include <machine/bus.h>
44#include <machine/resource.h>
45#include <sys/rman.h>
46
47#include <vm/vm.h>
48#include <vm/pmap.h>
49#include <machine/md_var.h>
50#include <machine/pc/bios.h>
51
52/*
53 * Vital Product Data
54 */
55struct vpd {
56	u_int16_t	Header;			/* 0x55AA */
57	u_int8_t	Signature[3];		/* Always 'VPD' */
58	u_int8_t	Length;			/* Sructure Length */
59
60	u_int8_t	Reserved[7];		/* Reserved */
61
62	u_int8_t	BuildID[9];		/* BIOS Build ID */
63	u_int8_t	BoxSerial[7];		/* Box Serial Number */
64	u_int8_t	PlanarSerial[11];	/* Motherboard Serial Number */
65	u_int8_t	MachType[7];		/* Machine Type/Model */
66	u_int8_t	Checksum;		/* Checksum */
67} __packed;
68
69struct vpd_softc {
70	device_t		dev;
71	struct resource *	res;
72	int			rid;
73
74	struct vpd *		vpd;
75
76	struct sysctl_ctx_list  ctx;
77
78	char		BuildID[10];
79	char		BoxSerial[8];
80	char		PlanarSerial[12];
81	char		MachineType[5];
82	char		MachineModel[4];
83};
84
85#define	VPD_START	0xf0000
86#define	VPD_STEP	0x10
87#define	VPD_OFF		2
88#define	VPD_LEN		3
89#define	VPD_SIG		"VPD"
90
91#define	RES2VPD(res)	((struct vpd *)rman_get_virtual(res))
92#define	ADDR2VPD(addr)	((struct vpd *)BIOS_PADDRTOVADDR(addr))
93
94static devclass_t vpd_devclass;
95
96static void	vpd_identify	(driver_t *, device_t);
97static int	vpd_probe	(device_t);
98static int	vpd_attach	(device_t);
99static int	vpd_detach	(device_t);
100static int	vpd_modevent	(module_t, int, void *);
101
102static int	vpd_cksum	(struct vpd *);
103
104SYSCTL_NODE(_hw, OID_AUTO, vpd, CTLFLAG_RD, NULL, NULL);
105SYSCTL_NODE(_hw_vpd, OID_AUTO, machine, CTLFLAG_RD, NULL, NULL);
106SYSCTL_NODE(_hw_vpd_machine, OID_AUTO, type, CTLFLAG_RD, NULL, NULL);
107SYSCTL_NODE(_hw_vpd_machine, OID_AUTO, model, CTLFLAG_RD, NULL, NULL);
108SYSCTL_NODE(_hw_vpd, OID_AUTO, build_id, CTLFLAG_RD, NULL, NULL);
109SYSCTL_NODE(_hw_vpd, OID_AUTO, serial, CTLFLAG_RD, NULL, NULL);
110SYSCTL_NODE(_hw_vpd_serial, OID_AUTO, box, CTLFLAG_RD, NULL, NULL);
111SYSCTL_NODE(_hw_vpd_serial, OID_AUTO, planar, CTLFLAG_RD, NULL, NULL);
112
113static void
114vpd_identify (driver_t *driver, device_t parent)
115{
116	device_t child;
117	u_int32_t addr;
118	int length;
119	int rid;
120
121	if (!device_is_alive(parent))
122		return;
123
124	addr = bios_sigsearch(VPD_START, VPD_SIG, VPD_LEN, VPD_STEP, VPD_OFF);
125	if (addr != 0) {
126		rid = 0;
127		length = ADDR2VPD(addr)->Length;
128
129		child = BUS_ADD_CHILD(parent, 0, "vpd", -1);
130		device_set_driver(child, driver);
131		bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
132		device_set_desc(child, "Vital Product Data Area");
133	}
134
135	return;
136}
137
138static int
139vpd_probe (device_t dev)
140{
141	struct resource *res;
142	int rid;
143	int error;
144
145	error = 0;
146	rid = 0;
147	res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
148		0ul, ~0ul, 1, RF_ACTIVE);
149	if (res == NULL) {
150		device_printf(dev, "Unable to allocate memory resource.\n");
151		error = ENOMEM;
152		goto bad;
153	}
154
155	if (vpd_cksum(RES2VPD(res))) {
156		device_printf(dev, "VPD checksum failed.\n");
157		error = ENXIO;
158		goto bad;
159	}
160
161bad:
162	if (res)
163		bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
164	return (error);
165}
166
167static int
168vpd_attach (device_t dev)
169{
170	struct vpd_softc *sc;
171	char unit[4];
172	int error;
173
174	sc = device_get_softc(dev);
175	error = 0;
176
177	sc->dev = dev;
178	sc->rid = 0;
179	sc->res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->rid,
180		0ul, ~0ul, 1, RF_ACTIVE);
181	if (sc->res == NULL) {
182		device_printf(dev, "Unable to allocate memory resource.\n");
183		error = ENOMEM;
184		goto bad;
185	}
186	sc->vpd = RES2VPD(sc->res);
187
188	snprintf(unit, sizeof(unit), "%d", device_get_unit(sc->dev));
189	snprintf(sc->MachineType, 5, "%.4s", sc->vpd->MachType);
190	snprintf(sc->MachineModel, 4, "%.3s", sc->vpd->MachType+4);
191	snprintf(sc->BuildID, 10, "%.9s", sc->vpd->BuildID);
192	snprintf(sc->BoxSerial, 8, "%.7s", sc->vpd->BoxSerial);
193	snprintf(sc->PlanarSerial, 12, "%.11s", sc->vpd->PlanarSerial);
194
195	sysctl_ctx_init(&sc->ctx);
196	SYSCTL_ADD_STRING(&sc->ctx,
197		SYSCTL_STATIC_CHILDREN(_hw_vpd_machine_type), OID_AUTO,
198		unit, CTLFLAG_RD|CTLFLAG_DYN, sc->MachineType, 0, NULL);
199	SYSCTL_ADD_STRING(&sc->ctx,
200		SYSCTL_STATIC_CHILDREN(_hw_vpd_machine_model), OID_AUTO,
201		unit, CTLFLAG_RD|CTLFLAG_DYN, sc->MachineModel, 0, NULL);
202	SYSCTL_ADD_STRING(&sc->ctx,
203		SYSCTL_STATIC_CHILDREN(_hw_vpd_build_id), OID_AUTO,
204		unit, CTLFLAG_RD|CTLFLAG_DYN, sc->BuildID, 0, NULL);
205	SYSCTL_ADD_STRING(&sc->ctx,
206		SYSCTL_STATIC_CHILDREN(_hw_vpd_serial_box), OID_AUTO,
207		unit, CTLFLAG_RD|CTLFLAG_DYN, sc->BoxSerial, 0, NULL);
208	SYSCTL_ADD_STRING(&sc->ctx,
209		SYSCTL_STATIC_CHILDREN(_hw_vpd_serial_planar), OID_AUTO,
210		unit, CTLFLAG_RD|CTLFLAG_DYN, sc->PlanarSerial, 0, NULL);
211
212	device_printf(dev, "Machine Type: %.4s, Model: %.3s, Build ID: %.9s\n",
213		sc->MachineType, sc->MachineModel, sc->BuildID);
214	device_printf(dev, "Box Serial: %.7s, Planar Serial: %.11s\n",
215		sc->BoxSerial, sc->PlanarSerial);
216
217	return (0);
218bad:
219	if (sc->res)
220		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
221	return (error);
222}
223
224static int
225vpd_detach (device_t dev)
226{
227	struct vpd_softc *sc;
228
229	sc = device_get_softc(dev);
230
231	if (sc->res)
232		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
233
234	sysctl_ctx_free(&sc->ctx);
235
236	return (0);
237}
238
239static int
240vpd_modevent (mod, what, arg)
241        module_t        mod;
242        int             what;
243        void *          arg;
244{
245	device_t *	devs;
246	int		count;
247	int		i;
248
249	switch (what) {
250	case MOD_LOAD:
251		break;
252	case MOD_UNLOAD:
253		devclass_get_devices(vpd_devclass, &devs, &count);
254		for (i = 0; i < count; i++) {
255			device_delete_child(device_get_parent(devs[i]), devs[i]);
256		}
257		break;
258	default:
259		break;
260	}
261
262	return (0);
263}
264
265static device_method_t vpd_methods[] = {
266	/* Device interface */
267	DEVMETHOD(device_identify,      vpd_identify),
268	DEVMETHOD(device_probe,         vpd_probe),
269	DEVMETHOD(device_attach,        vpd_attach),
270	DEVMETHOD(device_detach,        vpd_detach),
271	{ 0, 0 }
272};
273
274static driver_t vpd_driver = {
275	"vpd",
276	vpd_methods,
277	sizeof(struct vpd_softc),
278};
279
280DRIVER_MODULE(vpd, nexus, vpd_driver, vpd_devclass, vpd_modevent, 0);
281MODULE_VERSION(vpd, 1);
282
283static int
284vpd_cksum (struct vpd *v)
285{
286	u_int8_t *ptr;
287	u_int8_t cksum;
288	int i;
289
290	ptr = (u_int8_t *)v;
291	cksum = 0;
292	for (i = 0; i < v->Length ; i++) {
293		cksum += ptr[i];
294	}
295
296	/* XXX: For some reason this isn't right. */
297	printf("VPD cksum: 0x%02x\n", cksum);
298	cksum = 0;
299
300	return (cksum);
301}
302