xref: /freebsd/sys/dev/ipmi/ipmi_smbios.c (revision bbacb7ce72956a41c0daeefe875e5209d87c11ba)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/condvar.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 #include <sys/selinfo.h>
39 
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 #if defined(__amd64__) || defined(__i386__)
43 #include <machine/pc/bios.h>
44 #endif
45 #include <dev/smbios/smbios.h>
46 
47 #ifdef LOCAL_MODULE
48 #include <ipmi.h>
49 #include <ipmivars.h>
50 #else
51 #include <sys/ipmi.h>
52 #include <dev/ipmi/ipmivars.h>
53 #endif
54 
55 struct ipmi_entry {
56 	uint8_t		type;
57 	uint8_t		length;
58 	uint16_t	handle;
59 	uint8_t		interface_type;
60 	uint8_t		spec_revision;
61 	uint8_t		i2c_slave_address;
62 	uint8_t		NV_storage_device_address;
63 	uint64_t	base_address;
64 	uint8_t		base_address_modifier;
65 	uint8_t		interrupt_number;
66 };
67 
68 /* Fields in the base_address field of an IPMI entry. */
69 #define	IPMI_BAR_MODE(ba)	((ba) & 0x0000000000000001)
70 #define	IPMI_BAR_ADDR(ba)	((ba) & 0xfffffffffffffffe)
71 
72 /* Fields in the base_address_modifier field of an IPMI entry. */
73 #define	IPMI_BAM_IRQ_TRIGGER	0x01
74 #define	IPMI_BAM_IRQ_POLARITY	0x02
75 #define	IPMI_BAM_IRQ_VALID	0x08
76 #define	IPMI_BAM_ADDR_LSB(bam)	(((bam) & 0x10) >> 4)
77 #define	IPMI_BAM_REG_SPACING(bam) (((bam) & 0xc0) >> 6)
78 #define	SPACING_8		0x0
79 #define	SPACING_32		0x1
80 #define	SPACING_16		0x2
81 
82 typedef void (*smbios_callback_t)(struct smbios_structure_header *, void *);
83 
84 static struct ipmi_get_info ipmi_info;
85 static int ipmi_probed;
86 static struct mtx ipmi_info_mtx;
87 MTX_SYSINIT(ipmi_info, &ipmi_info_mtx, "ipmi info", MTX_DEF);
88 
89 static void	ipmi_smbios_probe(struct ipmi_get_info *);
90 static void	smbios_walk_table(uint8_t *, vm_size_t, smbios_callback_t,
91 		    void *);
92 static void	smbios_ipmi_info(struct smbios_structure_header *, void *);
93 
94 static void
95 smbios_ipmi_info(struct smbios_structure_header *h, void *arg)
96 {
97 	struct ipmi_get_info *info;
98 	struct ipmi_entry *s;
99 
100 	if (h->type != 38 || h->length <
101 	    offsetof(struct ipmi_entry, interrupt_number))
102 		return;
103 	s = (struct ipmi_entry *)h;
104 	info = arg;
105 	bzero(info, sizeof(struct ipmi_get_info));
106 	switch (s->interface_type) {
107 	case KCS_MODE:
108 	case SMIC_MODE:
109 		info->address = IPMI_BAR_ADDR(s->base_address) |
110 		    IPMI_BAM_ADDR_LSB(s->base_address_modifier);
111 		info->io_mode = IPMI_BAR_MODE(s->base_address);
112 		switch (IPMI_BAM_REG_SPACING(s->base_address_modifier)) {
113 		case SPACING_8:
114 			info->offset = 1;
115 			break;
116 		case SPACING_32:
117 			info->offset = 4;
118 			break;
119 		case SPACING_16:
120 			info->offset = 2;
121 			break;
122 		default:
123 			printf("SMBIOS: Invalid register spacing\n");
124 			return;
125 		}
126 		break;
127 	case SSIF_MODE:
128 		if ((s->base_address & 0xffffffffffffff00) != 0) {
129 			printf("SMBIOS: Invalid SSIF SMBus address, using BMC I2C slave address instead\n");
130 			info->address = s->i2c_slave_address;
131 			break;
132 		}
133 		info->address = IPMI_BAR_ADDR(s->base_address);
134 		break;
135 	default:
136 		return;
137 	}
138 	if (s->length > offsetof(struct ipmi_entry, interrupt_number)) {
139 		if (s->interrupt_number > 15)
140 			printf("SMBIOS: Non-ISA IRQ %d for IPMI\n",
141 			    s->interrupt_number);
142 		else
143 			info->irq = s->interrupt_number;
144 	}
145 	info->iface_type = s->interface_type;
146 }
147 
148 static void
149 smbios_walk_table(uint8_t *table, vm_size_t size, smbios_callback_t cb, void *arg)
150 {
151 	struct smbios_structure_header *s;
152 	uint8_t *p;
153 
154 	for (p = table; p < table + size;) {
155 		s = (struct smbios_structure_header *)p;
156 		cb(s, arg);
157 
158 		/*
159 		 * Look for a double-nul after the end of the
160 		 * formatted area of this structure.
161 		 */
162 		p += s->length;
163 		while (!(p[0] == 0 && p[1] == 0)) {
164 			p++;
165 			if (p >= table + size)
166 				return;
167 		}
168 
169 		/*
170 		 * Skip over the double-nul to the start of the next
171 		 * structure.
172 		 */
173 		p += 2;
174 	}
175 }
176 
177 /*
178  * Walk the SMBIOS table looking for an IPMI (type 38) entry.  If we find
179  * one, return the parsed data in the passed in ipmi_get_info structure and
180  * return true.  If we don't find one, return false.
181  */
182 static void
183 ipmi_smbios_probe(struct ipmi_get_info *info)
184 {
185 	void *table;
186 	vm_paddr_t table_paddr;
187 	vm_size_t table_size;
188 	int err;
189 
190 	bzero(info, sizeof(struct ipmi_get_info));
191 
192 	err = smbios_get_structure_table(&table_paddr, &table_size);
193 	if (err != 0)
194 		return;
195 
196 	table = pmap_mapbios(table_paddr, table_size);
197 
198 	smbios_walk_table(table, table_size, smbios_ipmi_info, info);
199 
200 	/* Unmap everything. */
201 	pmap_unmapbios((vm_offset_t)table, table_size);
202 }
203 
204 /*
205  * Return the SMBIOS IPMI table entry info to the caller.  If we haven't
206  * searched the IPMI table yet, search it.  Otherwise, return a cached
207  * copy of the data.
208  */
209 int
210 ipmi_smbios_identify(struct ipmi_get_info *info)
211 {
212 
213 	mtx_lock(&ipmi_info_mtx);
214 	switch (ipmi_probed) {
215 	case 0:
216 		/* Need to probe the SMBIOS table. */
217 		ipmi_probed++;
218 		mtx_unlock(&ipmi_info_mtx);
219 		ipmi_smbios_probe(&ipmi_info);
220 		mtx_lock(&ipmi_info_mtx);
221 		ipmi_probed++;
222 		wakeup(&ipmi_info);
223 		break;
224 	case 1:
225 		/* Another thread is currently probing the table, so wait. */
226 		while (ipmi_probed == 1)
227 			msleep(&ipmi_info, &ipmi_info_mtx, 0, "ipmi info", 0);
228 		break;
229 	default:
230 		/* The cached data is available. */
231 		break;
232 	}
233 
234 	bcopy(&ipmi_info, info, sizeof(ipmi_info));
235 	mtx_unlock(&ipmi_info_mtx);
236 
237 	return (info->iface_type != 0);
238 }
239