xref: /freebsd/sys/dev/ipmi/ipmi_smbios.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
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$");
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 struct ipmi_entry {
51 	uint8_t		type;
52 	uint8_t		length;
53 	uint16_t	handle;
54 	uint8_t		interface_type;
55 	uint8_t		spec_revision;
56 	uint8_t		i2c_slave_address;
57 	uint8_t		NV_storage_device_address;
58 	uint64_t	base_address;
59 	uint8_t		base_address_modifier;
60 	uint8_t		interrupt_number;
61 };
62 
63 /* Fields in the base_address field of an IPMI entry. */
64 #define	IPMI_BAR_MODE(ba)	((ba) & 0x0000000000000001)
65 #define	IPMI_BAR_ADDR(ba)	((ba) & 0xfffffffffffffffe)
66 
67 /* Fields in the base_address_modifier field of an IPMI entry. */
68 #define	IPMI_BAM_IRQ_TRIGGER	0x01
69 #define	IPMI_BAM_IRQ_POLARITY	0x02
70 #define	IPMI_BAM_IRQ_VALID	0x08
71 #define	IPMI_BAM_ADDR_LSB(bam)	(((bam) & 0x10) >> 4)
72 #define	IPMI_BAM_REG_SPACING(bam) (((bam) & 0xc0) >> 6)
73 #define	SPACING_8		0x0
74 #define	SPACING_32		0x1
75 #define	SPACING_16		0x2
76 
77 typedef void (*smbios_callback_t)(struct smbios_structure_header *, void *);
78 
79 static struct ipmi_get_info ipmi_info;
80 static int ipmi_probed;
81 static struct mtx ipmi_info_mtx;
82 MTX_SYSINIT(ipmi_info, &ipmi_info_mtx, "ipmi info", MTX_DEF);
83 
84 static void	ipmi_smbios_probe(struct ipmi_get_info *);
85 static int	smbios_cksum(struct smbios_eps *);
86 static void	smbios_walk_table(uint8_t *, int, smbios_callback_t,
87 		    void *);
88 static void	smbios_ipmi_info(struct smbios_structure_header *, void *);
89 
90 static void
91 smbios_ipmi_info(struct smbios_structure_header *h, void *arg)
92 {
93 	struct ipmi_get_info *info;
94 	struct ipmi_entry *s;
95 
96 	if (h->type != 38 || h->length <
97 	    offsetof(struct ipmi_entry, interrupt_number))
98 		return;
99 	s = (struct ipmi_entry *)h;
100 	info = arg;
101 	bzero(info, sizeof(struct ipmi_get_info));
102 	switch (s->interface_type) {
103 	case KCS_MODE:
104 	case SMIC_MODE:
105 		info->address = IPMI_BAR_ADDR(s->base_address) |
106 		    IPMI_BAM_ADDR_LSB(s->base_address_modifier);
107 		info->io_mode = IPMI_BAR_MODE(s->base_address);
108 		switch (IPMI_BAM_REG_SPACING(s->base_address_modifier)) {
109 		case SPACING_8:
110 			info->offset = 1;
111 			break;
112 		case SPACING_32:
113 			info->offset = 4;
114 			break;
115 		case SPACING_16:
116 			info->offset = 2;
117 			break;
118 		default:
119 			printf("SMBIOS: Invalid register spacing\n");
120 			return;
121 		}
122 		break;
123 	case SSIF_MODE:
124 		if ((s->base_address & 0xffffffffffffff00) != 0) {
125 			printf("SMBIOS: Invalid SSIF SMBus address, using BMC I2C slave address instead\n");
126 			info->address = s->i2c_slave_address;
127 			break;
128 		}
129 		info->address = IPMI_BAR_ADDR(s->base_address);
130 		break;
131 	default:
132 		return;
133 	}
134 	if (s->length > offsetof(struct ipmi_entry, interrupt_number)) {
135 		if (s->interrupt_number > 15)
136 			printf("SMBIOS: Non-ISA IRQ %d for IPMI\n",
137 			    s->interrupt_number);
138 		else
139 			info->irq = s->interrupt_number;
140 	}
141 	info->iface_type = s->interface_type;
142 }
143 
144 static void
145 smbios_walk_table(uint8_t *p, int entries, smbios_callback_t cb, void *arg)
146 {
147 	struct smbios_structure_header *s;
148 
149 	while (entries--) {
150 		s = (struct smbios_structure_header *)p;
151 		cb(s, arg);
152 
153 		/*
154 		 * Look for a double-nul after the end of the
155 		 * formatted area of this structure.
156 		 */
157 		p += s->length;
158 		while (!(p[0] == 0 && p[1] == 0))
159 			p++;
160 
161 		/*
162 		 * Skip over the double-nul to the start of the next
163 		 * structure.
164 		 */
165 		p += 2;
166 	}
167 }
168 
169 /*
170  * Walk the SMBIOS table looking for an IPMI (type 38) entry.  If we find
171  * one, return the parsed data in the passed in ipmi_get_info structure and
172  * return true.  If we don't find one, return false.
173  */
174 static void
175 ipmi_smbios_probe(struct ipmi_get_info *info)
176 {
177 	struct smbios_eps *header;
178 	void *table;
179 	u_int32_t addr;
180 
181 	bzero(info, sizeof(struct ipmi_get_info));
182 
183 	/* Find the SMBIOS table header. */
184 	addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN,
185 			      SMBIOS_STEP, SMBIOS_OFF);
186 	if (addr == 0)
187 		return;
188 
189 	/*
190 	 * Map the header.  We first map a fixed size to get the actual
191 	 * length and then map it a second time with the actual length so
192 	 * we can verify the checksum.
193 	 */
194 	header = pmap_mapbios(addr, sizeof(struct smbios_eps));
195 	table = pmap_mapbios(addr, header->length);
196 	pmap_unmapbios((vm_offset_t)header, sizeof(struct smbios_eps));
197 	header = table;
198 	if (smbios_cksum(header) != 0) {
199 		pmap_unmapbios((vm_offset_t)header, header->length);
200 		return;
201 	}
202 
203 	/* Now map the actual table and walk it looking for an IPMI entry. */
204 	table = pmap_mapbios(header->structure_table_address,
205 	    header->structure_table_length);
206 	smbios_walk_table(table, header->number_structures, smbios_ipmi_info,
207 	    info);
208 
209 	/* Unmap everything. */
210 	pmap_unmapbios((vm_offset_t)table, header->structure_table_length);
211 	pmap_unmapbios((vm_offset_t)header, header->length);
212 }
213 
214 /*
215  * Return the SMBIOS IPMI table entry info to the caller.  If we haven't
216  * searched the IPMI table yet, search it.  Otherwise, return a cached
217  * copy of the data.
218  */
219 int
220 ipmi_smbios_identify(struct ipmi_get_info *info)
221 {
222 
223 	mtx_lock(&ipmi_info_mtx);
224 	switch (ipmi_probed) {
225 	case 0:
226 		/* Need to probe the SMBIOS table. */
227 		ipmi_probed++;
228 		mtx_unlock(&ipmi_info_mtx);
229 		ipmi_smbios_probe(&ipmi_info);
230 		mtx_lock(&ipmi_info_mtx);
231 		ipmi_probed++;
232 		wakeup(&ipmi_info);
233 		break;
234 	case 1:
235 		/* Another thread is currently probing the table, so wait. */
236 		while (ipmi_probed == 1)
237 			msleep(&ipmi_info, &ipmi_info_mtx, 0, "ipmi info", 0);
238 		break;
239 	default:
240 		/* The cached data is available. */
241 		break;
242 	}
243 
244 	bcopy(&ipmi_info, info, sizeof(ipmi_info));
245 	mtx_unlock(&ipmi_info_mtx);
246 
247 	return (info->iface_type != 0);
248 }
249 
250 static int
251 smbios_cksum(struct smbios_eps *e)
252 {
253 	u_int8_t *ptr;
254 	u_int8_t cksum;
255 	int i;
256 
257 	ptr = (u_int8_t *)e;
258 	cksum = 0;
259 	for (i = 0; i < e->length; i++) {
260 		cksum += ptr[i];
261 	}
262 
263 	return (cksum);
264 }
265