ipmi_smbios.c revision 162564
1/*-
2 * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ipmi/ipmi_smbios.c 162564 2006-09-22 22:16:20Z jhb $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/condvar.h>
34#include <sys/eventhandler.h>
35#include <sys/kernel.h>
36#include <sys/selinfo.h>
37
38#include <vm/vm.h>
39#include <vm/pmap.h>
40#include <machine/pc/bios.h>
41
42#ifdef LOCAL_MODULE
43#include <ipmi.h>
44#include <ipmivars.h>
45#else
46#include <sys/ipmi.h>
47#include <dev/ipmi/ipmivars.h>
48#endif
49
50#if __FreeBSD_version < 700020
51#define	pmap_mapbios		pmap_mapdev
52#define	pmap_unmapbios		pmap_unmapdev
53#endif
54
55struct smbios_table_entry {
56	uint8_t		anchor_string[4];
57	uint8_t		checksum;
58	uint8_t		length;
59	uint8_t		major_version;
60	uint8_t		minor_version;
61	uint16_t	maximum_structure_size;
62	uint8_t		entry_point_revision;
63	uint8_t		formatted_area[5];
64	uint8_t		DMI_anchor_string[5];
65	uint8_t		intermediate_checksum;
66	uint16_t	structure_table_length;
67	uint32_t	structure_table_address;
68	uint16_t	number_structures;
69	uint8_t		BCD_revision;
70};
71
72struct structure_header {
73	uint8_t		type;
74	uint8_t		length;
75	uint16_t	handle;
76};
77
78struct ipmi_entry {
79	uint8_t		type;
80	uint8_t		length;
81	uint16_t	handle;
82	uint8_t		interface_type;
83	uint8_t		spec_revision;
84	uint8_t		i2c_slave_address;
85	uint8_t		NV_storage_device_address;
86	uint64_t	base_address;
87	uint8_t		base_address_modifier;
88	uint8_t		interrupt_number;
89};
90
91/* Fields in the base_address field of an IPMI entry. */
92#define	IPMI_BAR_MODE(ba)	((ba) & 0x0000000000000001)
93#define	IPMI_BAR_ADDR(ba)	((ba) & 0xfffffffffffffffe)
94
95/* Fields in the base_address_modifier field of an IPMI entry. */
96#define	IPMI_BAM_IRQ_TRIGGER	0x01
97#define	IPMI_BAM_IRQ_POLARITY	0x02
98#define	IPMI_BAM_IRQ_VALID	0x08
99#define	IPMI_BAM_ADDR_LSB(bam)	(((bam) & 0x10) >> 4)
100#define	IPMI_BAM_REG_SPACING(bam) (((bam) & 0xc0) >> 6)
101#define	SPACING_8		0x0
102#define	SPACING_32		0x1
103#define	SPACING_16		0x2
104
105#define	SMBIOS_START		0xf0000
106#define	SMBIOS_STEP		0x10
107#define	SMBIOS_OFF		0
108#define	SMBIOS_LEN		4
109#define	SMBIOS_SIG		"_SM_"
110
111typedef void (*dispatchproc_t)(uint8_t *p, char **table,
112    struct ipmi_get_info *info);
113
114static struct ipmi_get_info ipmi_info;
115static int ipmi_probed;
116static struct mtx ipmi_info_mtx;
117MTX_SYSINIT(ipmi_info, &ipmi_info_mtx, "ipmi info", MTX_DEF);
118
119static char	*get_strings(char *, char **);
120static void	ipmi_smbios_probe(struct ipmi_get_info *);
121static int	smbios_cksum	(struct smbios_table_entry *);
122static void	smbios_run_table(uint8_t *, int, dispatchproc_t *,
123		    struct ipmi_get_info *);
124static void	smbios_t38_proc_info(uint8_t *, char **,
125		    struct ipmi_get_info *);
126
127static void
128smbios_t38_proc_info(uint8_t *p, char **table, struct ipmi_get_info *info)
129{
130	struct ipmi_entry *s = (struct ipmi_entry *)p;
131
132	bzero(info, sizeof(struct ipmi_get_info));
133	switch (s->interface_type) {
134	case KCS_MODE:
135	case SMIC_MODE:
136		info->address = IPMI_BAR_ADDR(s->base_address) |
137		    IPMI_BAM_ADDR_LSB(s->base_address_modifier);
138		info->io_mode = IPMI_BAR_MODE(s->base_address);
139		switch (IPMI_BAM_REG_SPACING(s->base_address_modifier)) {
140		case SPACING_8:
141			info->offset = 1;
142			break;
143		case SPACING_32:
144			info->offset = 4;
145			break;
146		case SPACING_16:
147			info->offset = 2;
148			break;
149		default:
150			printf("SMBIOS: Invalid register spacing\n");
151			return;
152		}
153		break;
154	case SSIF_MODE:
155		if ((s->base_address & 0xffffffffffffff00) != 0) {
156			printf("SMBIOS: Invalid SSIF SMBus address, using BMC I2C slave address instead\n");
157			info->address = s->i2c_slave_address >> 1;
158			break;
159		}
160		info->address = IPMI_BAR_ADDR(s->base_address) >> 1;
161		break;
162	default:
163		return;
164	}
165	if (s->length > offsetof(struct ipmi_entry, interrupt_number)) {
166		if (s->interrupt_number > 15)
167			printf("SMBIOS: Non-ISA IRQ %d for IPMI\n",
168			    s->interrupt_number);
169		else
170			info->irq = s->interrupt_number;
171	}
172	info->iface_type = s->interface_type;
173}
174
175static char *
176get_strings(char *p, char **table)
177{
178	/* Scan for strings, stoping at a single null byte */
179	while (*p != 0) {
180		*table++ = p;
181		p += strlen(p) + 1;
182	}
183	*table = 0;
184
185	/* Skip past terminating null byte */
186	return (p + 1);
187}
188
189
190static void
191smbios_run_table(uint8_t *p, int entries, dispatchproc_t *dispatchstatus,
192    struct ipmi_get_info *info)
193{
194	struct structure_header *s;
195	char *table[20];
196	uint8_t *nextp;
197
198	while(entries--) {
199		s = (struct structure_header *) p;
200		nextp = get_strings(p + s->length, table);
201
202		/*
203		 * No strings still has a double-null at the end,
204		 * skip over it
205		 */
206		if (table[0] == 0)
207			nextp++;
208
209		if (dispatchstatus[*p]) {
210			(dispatchstatus[*p])(p, table, info);
211		}
212		p = nextp;
213	}
214}
215
216/*
217 * Walk the SMBIOS table looking for an IPMI (type 38) entry.  If we find
218 * one, return the parsed data in the passed in ipmi_get_info structure and
219 * return true.  If we don't find one, return false.
220 */
221static void
222ipmi_smbios_probe(struct ipmi_get_info *info)
223{
224	dispatchproc_t dispatch_smbios_ipmi[256];
225	struct smbios_table_entry *header;
226	void *table;
227	u_int32_t addr;
228
229	bzero(info, sizeof(struct ipmi_get_info));
230
231	/* Find the SMBIOS table header. */
232	addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN,
233			      SMBIOS_STEP, SMBIOS_OFF);
234	if (addr == 0)
235		return;
236
237	/*
238	 * Map the header.  We first map a fixed size to get the actual
239	 * length and then map it a second time with the actual length so
240	 * we can verify the checksum.
241	 */
242	header = pmap_mapbios(addr, sizeof(struct smbios_table_entry));
243	table = pmap_mapbios(addr, header->length);
244	pmap_unmapbios((vm_offset_t)header, sizeof(struct smbios_table_entry));
245	header = table;
246	if (smbios_cksum(header) != 0) {
247		pmap_unmapbios((vm_offset_t)header, header->length);
248		return;
249	}
250
251	/* Now map the actual table and walk it looking for an IPMI entry. */
252	table = pmap_mapbios(header->structure_table_address,
253	    header->structure_table_length);
254	bzero((void *)dispatch_smbios_ipmi, sizeof(dispatch_smbios_ipmi));
255	dispatch_smbios_ipmi[38] = (void *)smbios_t38_proc_info;
256	smbios_run_table(table, header->number_structures, dispatch_smbios_ipmi,
257	    info);
258
259	/* Unmap everything. */
260	pmap_unmapbios((vm_offset_t)table, header->structure_table_length);
261	pmap_unmapbios((vm_offset_t)header, header->length);
262}
263
264/*
265 * Return the SMBIOS IPMI table entry info to the caller.  If we haven't
266 * searched the IPMI table yet, search it.  Otherwise, return a cached
267 * copy of the data.
268 */
269int
270ipmi_smbios_identify(struct ipmi_get_info *info)
271{
272
273	mtx_lock(&ipmi_info_mtx);
274	switch (ipmi_probed) {
275	case 0:
276		/* Need to probe the SMBIOS table. */
277		ipmi_probed++;
278		mtx_unlock(&ipmi_info_mtx);
279		ipmi_smbios_probe(&ipmi_info);
280		mtx_lock(&ipmi_info_mtx);
281		ipmi_probed++;
282		wakeup(&ipmi_info);
283		break;
284	case 1:
285		/* Another thread is currently probing the table, so wait. */
286		while (ipmi_probed == 1)
287			msleep(&ipmi_info, &ipmi_info_mtx, 0, "ipmi info", 0);
288		break;
289	default:
290		/* The cached data is available. */
291		break;
292	}
293
294	bcopy(&ipmi_info, info, sizeof(ipmi_info));
295	mtx_unlock(&ipmi_info_mtx);
296
297	return (info->iface_type != 0);
298}
299
300static int
301smbios_cksum (struct smbios_table_entry *e)
302{
303	u_int8_t *ptr;
304	u_int8_t cksum;
305	int i;
306
307	ptr = (u_int8_t *)e;
308	cksum = 0;
309	for (i = 0; i < e->length; i++) {
310		cksum += ptr[i];
311	}
312
313	return (cksum);
314}
315