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