]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/ipmi/ipmi_smbios.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / ipmi / ipmi_smbios.c
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 #if __FreeBSD_version < 602110
51 #define pmap_mapbios            pmap_mapdev
52 #define pmap_unmapbios          pmap_unmapdev
53 #endif
54
55 struct smbios_table {
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
72 struct structure_header {
73         uint8_t         type;
74         uint8_t         length;
75         uint16_t        handle;
76 };
77
78 struct 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
111 typedef void (*smbios_callback_t)(struct structure_header *, void *);
112
113 static struct ipmi_get_info ipmi_info;
114 static int ipmi_probed;
115 static struct mtx ipmi_info_mtx;
116 MTX_SYSINIT(ipmi_info, &ipmi_info_mtx, "ipmi info", MTX_DEF);
117
118 static void     ipmi_smbios_probe(struct ipmi_get_info *);
119 static int      smbios_cksum(struct smbios_table *);
120 static void     smbios_walk_table(uint8_t *, int, smbios_callback_t,
121                     void *);
122 static void     smbios_ipmi_info(struct structure_header *, void *);
123
124 static void
125 smbios_ipmi_info(struct structure_header *h, void *arg)
126 {
127         struct ipmi_get_info *info;
128         struct ipmi_entry *s;
129
130         if (h->type != 38 || h->length <
131             offsetof(struct ipmi_entry, interrupt_number))
132                 return;
133         s = (struct ipmi_entry *)h;
134         info = arg;
135         bzero(info, sizeof(struct ipmi_get_info));
136         switch (s->interface_type) {
137         case KCS_MODE:
138         case SMIC_MODE:
139                 info->address = IPMI_BAR_ADDR(s->base_address) |
140                     IPMI_BAM_ADDR_LSB(s->base_address_modifier);
141                 info->io_mode = IPMI_BAR_MODE(s->base_address);
142                 switch (IPMI_BAM_REG_SPACING(s->base_address_modifier)) {
143                 case SPACING_8:
144                         info->offset = 1;
145                         break;
146                 case SPACING_32:
147                         info->offset = 4;
148                         break;
149                 case SPACING_16:
150                         info->offset = 2;
151                         break;
152                 default:
153                         printf("SMBIOS: Invalid register spacing\n");
154                         return;
155                 }
156                 break;
157         case SSIF_MODE:
158                 if ((s->base_address & 0xffffffffffffff00) != 0) {
159                         printf("SMBIOS: Invalid SSIF SMBus address, using BMC I2C slave address instead\n");
160                         info->address = s->i2c_slave_address;
161                         break;
162                 }
163                 info->address = IPMI_BAR_ADDR(s->base_address);
164                 break;
165         default:
166                 return;
167         }
168         if (s->length > offsetof(struct ipmi_entry, interrupt_number)) {
169                 if (s->interrupt_number > 15)
170                         printf("SMBIOS: Non-ISA IRQ %d for IPMI\n",
171                             s->interrupt_number);
172                 else
173                         info->irq = s->interrupt_number;
174         }
175         info->iface_type = s->interface_type;
176 }
177
178 static void
179 smbios_walk_table(uint8_t *p, int entries, smbios_callback_t cb, void *arg)
180 {
181         struct structure_header *s;
182
183         while (entries--) {
184                 s = (struct structure_header *)p;
185                 cb(s, arg);
186
187                 /*
188                  * Look for a double-nul after the end of the
189                  * formatted area of this structure.
190                  */
191                 p += s->length;
192                 while (!(p[0] == 0 && p[1] == 0))
193                         p++;
194
195                 /*
196                  * Skip over the double-nul to the start of the next
197                  * structure.
198                  */
199                 p += 2;
200         }
201 }
202
203 /*
204  * Walk the SMBIOS table looking for an IPMI (type 38) entry.  If we find
205  * one, return the parsed data in the passed in ipmi_get_info structure and
206  * return true.  If we don't find one, return false.
207  */
208 static void
209 ipmi_smbios_probe(struct ipmi_get_info *info)
210 {
211         struct smbios_table *header;
212         void *table;
213         u_int32_t addr;
214
215         bzero(info, sizeof(struct ipmi_get_info));
216
217         /* Find the SMBIOS table header. */
218         addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN,
219                               SMBIOS_STEP, SMBIOS_OFF);
220         if (addr == 0)
221                 return;
222
223         /*
224          * Map the header.  We first map a fixed size to get the actual
225          * length and then map it a second time with the actual length so
226          * we can verify the checksum.
227          */
228         header = pmap_mapbios(addr, sizeof(struct smbios_table));
229         table = pmap_mapbios(addr, header->length);
230         pmap_unmapbios((vm_offset_t)header, sizeof(struct smbios_table));
231         header = table;
232         if (smbios_cksum(header) != 0) {
233                 pmap_unmapbios((vm_offset_t)header, header->length);
234                 return;
235         }
236
237         /* Now map the actual table and walk it looking for an IPMI entry. */
238         table = pmap_mapbios(header->structure_table_address,
239             header->structure_table_length);
240         smbios_walk_table(table, header->number_structures, smbios_ipmi_info,
241             info);
242
243         /* Unmap everything. */
244         pmap_unmapbios((vm_offset_t)table, header->structure_table_length);
245         pmap_unmapbios((vm_offset_t)header, header->length);
246 }
247
248 /*
249  * Return the SMBIOS IPMI table entry info to the caller.  If we haven't
250  * searched the IPMI table yet, search it.  Otherwise, return a cached
251  * copy of the data.
252  */
253 int
254 ipmi_smbios_identify(struct ipmi_get_info *info)
255 {
256
257         mtx_lock(&ipmi_info_mtx);
258         switch (ipmi_probed) {
259         case 0:
260                 /* Need to probe the SMBIOS table. */
261                 ipmi_probed++;
262                 mtx_unlock(&ipmi_info_mtx);
263                 ipmi_smbios_probe(&ipmi_info);
264                 mtx_lock(&ipmi_info_mtx);
265                 ipmi_probed++;
266                 wakeup(&ipmi_info);
267                 break;
268         case 1:
269                 /* Another thread is currently probing the table, so wait. */
270                 while (ipmi_probed == 1)
271                         msleep(&ipmi_info, &ipmi_info_mtx, 0, "ipmi info", 0);
272                 break;
273         default:
274                 /* The cached data is available. */
275                 break;
276         }
277
278         bcopy(&ipmi_info, info, sizeof(ipmi_info));
279         mtx_unlock(&ipmi_info_mtx);
280
281         return (info->iface_type != 0);
282 }
283
284 static int
285 smbios_cksum(struct smbios_table *e)
286 {
287         u_int8_t *ptr;
288         u_int8_t cksum;
289         int i;
290
291         ptr = (u_int8_t *)e;
292         cksum = 0;
293         for (i = 0; i < e->length; i++) {
294                 cksum += ptr[i];
295         }
296
297         return (cksum);
298 }