xref: /freebsd/sys/dev/ipmi/ipmi_smbios.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/condvar.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/selinfo.h>
36 #include <sys/efi.h>
37 
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 #if defined(__amd64__) || defined(__i386__)
41 #include <machine/pc/bios.h>
42 #endif
43 #include <dev/smbios/smbios.h>
44 
45 #ifdef LOCAL_MODULE
46 #include <ipmi.h>
47 #include <ipmivars.h>
48 #else
49 #include <sys/ipmi.h>
50 #include <dev/ipmi/ipmivars.h>
51 #endif
52 
53 struct ipmi_entry {
54 	uint8_t		type;
55 	uint8_t		length;
56 	uint16_t	handle;
57 	uint8_t		interface_type;
58 	uint8_t		spec_revision;
59 	uint8_t		i2c_slave_address;
60 	uint8_t		NV_storage_device_address;
61 	uint64_t	base_address;
62 	uint8_t		base_address_modifier;
63 	uint8_t		interrupt_number;
64 };
65 
66 /* Fields in the base_address field of an IPMI entry. */
67 #define	IPMI_BAR_MODE(ba)	((ba) & 0x0000000000000001)
68 #define	IPMI_BAR_ADDR(ba)	((ba) & 0xfffffffffffffffe)
69 
70 /* Fields in the base_address_modifier field of an IPMI entry. */
71 #define	IPMI_BAM_IRQ_TRIGGER	0x01
72 #define	IPMI_BAM_IRQ_POLARITY	0x02
73 #define	IPMI_BAM_IRQ_VALID	0x08
74 #define	IPMI_BAM_ADDR_LSB(bam)	(((bam) & 0x10) >> 4)
75 #define	IPMI_BAM_REG_SPACING(bam) (((bam) & 0xc0) >> 6)
76 #define	SPACING_8		0x0
77 #define	SPACING_32		0x1
78 #define	SPACING_16		0x2
79 
80 static struct ipmi_get_info ipmi_info;
81 static int ipmi_probed;
82 static struct mtx ipmi_info_mtx;
83 MTX_SYSINIT(ipmi_info, &ipmi_info_mtx, "ipmi info", MTX_DEF);
84 
85 static void	ipmi_smbios_probe(struct ipmi_get_info *);
86 static int	smbios_cksum(struct smbios_eps *);
87 static void	smbios_ipmi_info(struct smbios_structure_header *, void *);
88 
89 static void
90 smbios_ipmi_info(struct smbios_structure_header *h, void *arg)
91 {
92 	struct ipmi_get_info *info;
93 	struct ipmi_entry *s;
94 
95 	if (h->type != 38 || h->length <
96 	    offsetof(struct ipmi_entry, interrupt_number))
97 		return;
98 	s = (struct ipmi_entry *)h;
99 	info = arg;
100 	bzero(info, sizeof(struct ipmi_get_info));
101 	switch (s->interface_type) {
102 	case KCS_MODE:
103 	case SMIC_MODE:
104 	case BT_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 /*
145  * Walk the SMBIOS table looking for an IPMI (type 38) entry.  If we find
146  * one, return the parsed data in the passed in ipmi_get_info structure and
147  * return true.  If we don't find one, return false.
148  */
149 static void
150 ipmi_smbios_probe(struct ipmi_get_info *info)
151 {
152 #ifdef ARCH_MAY_USE_EFI
153 	struct uuid efi_smbios;
154 	void *addr_efi;
155 #endif
156 	struct smbios_eps *header;
157 	void *table;
158 	u_int32_t addr;
159 
160 	addr = 0;
161 	bzero(info, sizeof(struct ipmi_get_info));
162 
163 #ifdef ARCH_MAY_USE_EFI
164 	efi_smbios = (struct uuid)EFI_TABLE_SMBIOS;
165 	if (!efi_get_table(&efi_smbios, &addr_efi))
166 		addr = (vm_paddr_t)addr_efi;
167 #endif
168 
169 	if (addr == 0)
170 		/* Find the SMBIOS table header. */
171 		addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN,
172 			SMBIOS_STEP, SMBIOS_OFF);
173 	if (addr == 0)
174 		return;
175 
176 	/*
177 	 * Map the header.  We first map a fixed size to get the actual
178 	 * length and then map it a second time with the actual length so
179 	 * we can verify the checksum.
180 	 */
181 	header = pmap_mapbios(addr, sizeof(struct smbios_eps));
182 	table = pmap_mapbios(addr, header->length);
183 	pmap_unmapbios(header, sizeof(struct smbios_eps));
184 	header = table;
185 	if (smbios_cksum(header) != 0) {
186 		pmap_unmapbios(header, header->length);
187 		return;
188 	}
189 
190 	/* Now map the actual table and walk it looking for an IPMI entry. */
191 	table = pmap_mapbios(header->structure_table_address,
192 	    header->structure_table_length);
193 	smbios_walk_table(table, header->number_structures, smbios_ipmi_info,
194 	    info);
195 
196 	/* Unmap everything. */
197 	pmap_unmapbios(table, header->structure_table_length);
198 	pmap_unmapbios(header, header->length);
199 }
200 
201 /*
202  * Return the SMBIOS IPMI table entry info to the caller.  If we haven't
203  * searched the IPMI table yet, search it.  Otherwise, return a cached
204  * copy of the data.
205  */
206 int
207 ipmi_smbios_identify(struct ipmi_get_info *info)
208 {
209 
210 	mtx_lock(&ipmi_info_mtx);
211 	switch (ipmi_probed) {
212 	case 0:
213 		/* Need to probe the SMBIOS table. */
214 		ipmi_probed++;
215 		mtx_unlock(&ipmi_info_mtx);
216 		ipmi_smbios_probe(&ipmi_info);
217 		mtx_lock(&ipmi_info_mtx);
218 		ipmi_probed++;
219 		wakeup(&ipmi_info);
220 		break;
221 	case 1:
222 		/* Another thread is currently probing the table, so wait. */
223 		while (ipmi_probed == 1)
224 			msleep(&ipmi_info, &ipmi_info_mtx, 0, "ipmi info", 0);
225 		break;
226 	default:
227 		/* The cached data is available. */
228 		break;
229 	}
230 
231 	bcopy(&ipmi_info, info, sizeof(ipmi_info));
232 	mtx_unlock(&ipmi_info_mtx);
233 
234 	return (info->iface_type != 0);
235 }
236 
237 static int
238 smbios_cksum(struct smbios_eps *e)
239 {
240 	u_int8_t *ptr;
241 	u_int8_t cksum;
242 	int i;
243 
244 	ptr = (u_int8_t *)e;
245 	cksum = 0;
246 	for (i = 0; i < e->length; i++) {
247 		cksum += ptr[i];
248 	}
249 
250 	return (cksum);
251 }
252