]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/ipmi/ipmi_smbios.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 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 int      smbios_cksum(struct smbios_eps *);
91 static void     smbios_walk_table(uint8_t *, int, smbios_callback_t,
92                     void *);
93 static void     smbios_ipmi_info(struct smbios_structure_header *, void *);
94
95 static void
96 smbios_ipmi_info(struct smbios_structure_header *h, void *arg)
97 {
98         struct ipmi_get_info *info;
99         struct ipmi_entry *s;
100
101         if (h->type != 38 || h->length <
102             offsetof(struct ipmi_entry, interrupt_number))
103                 return;
104         s = (struct ipmi_entry *)h;
105         info = arg;
106         bzero(info, sizeof(struct ipmi_get_info));
107         switch (s->interface_type) {
108         case KCS_MODE:
109         case SMIC_MODE:
110                 info->address = IPMI_BAR_ADDR(s->base_address) |
111                     IPMI_BAM_ADDR_LSB(s->base_address_modifier);
112                 info->io_mode = IPMI_BAR_MODE(s->base_address);
113                 switch (IPMI_BAM_REG_SPACING(s->base_address_modifier)) {
114                 case SPACING_8:
115                         info->offset = 1;
116                         break;
117                 case SPACING_32:
118                         info->offset = 4;
119                         break;
120                 case SPACING_16:
121                         info->offset = 2;
122                         break;
123                 default:
124                         printf("SMBIOS: Invalid register spacing\n");
125                         return;
126                 }
127                 break;
128         case SSIF_MODE:
129                 if ((s->base_address & 0xffffffffffffff00) != 0) {
130                         printf("SMBIOS: Invalid SSIF SMBus address, using BMC I2C slave address instead\n");
131                         info->address = s->i2c_slave_address;
132                         break;
133                 }
134                 info->address = IPMI_BAR_ADDR(s->base_address);
135                 break;
136         default:
137                 return;
138         }
139         if (s->length > offsetof(struct ipmi_entry, interrupt_number)) {
140                 if (s->interrupt_number > 15)
141                         printf("SMBIOS: Non-ISA IRQ %d for IPMI\n",
142                             s->interrupt_number);
143                 else
144                         info->irq = s->interrupt_number;
145         }
146         info->iface_type = s->interface_type;
147 }
148
149 static void
150 smbios_walk_table(uint8_t *p, int entries, smbios_callback_t cb, void *arg)
151 {
152         struct smbios_structure_header *s;
153
154         while (entries--) {
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
166                 /*
167                  * Skip over the double-nul to the start of the next
168                  * structure.
169                  */
170                 p += 2;
171         }
172 }
173
174 /*
175  * Walk the SMBIOS table looking for an IPMI (type 38) entry.  If we find
176  * one, return the parsed data in the passed in ipmi_get_info structure and
177  * return true.  If we don't find one, return false.
178  */
179 static void
180 ipmi_smbios_probe(struct ipmi_get_info *info)
181 {
182         struct smbios_eps *header;
183         void *table;
184         u_int32_t addr;
185
186         bzero(info, sizeof(struct ipmi_get_info));
187
188         /* Find the SMBIOS table header. */
189         addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN,
190                               SMBIOS_STEP, SMBIOS_OFF);
191         if (addr == 0)
192                 return;
193
194         /*
195          * Map the header.  We first map a fixed size to get the actual
196          * length and then map it a second time with the actual length so
197          * we can verify the checksum.
198          */
199         header = pmap_mapbios(addr, sizeof(struct smbios_eps));
200         table = pmap_mapbios(addr, header->length);
201         pmap_unmapbios((vm_offset_t)header, sizeof(struct smbios_eps));
202         header = table;
203         if (smbios_cksum(header) != 0) {
204                 pmap_unmapbios((vm_offset_t)header, header->length);
205                 return;
206         }
207
208         /* Now map the actual table and walk it looking for an IPMI entry. */
209         table = pmap_mapbios(header->structure_table_address,
210             header->structure_table_length);
211         smbios_walk_table(table, header->number_structures, smbios_ipmi_info,
212             info);
213
214         /* Unmap everything. */
215         pmap_unmapbios((vm_offset_t)table, header->structure_table_length);
216         pmap_unmapbios((vm_offset_t)header, header->length);
217 }
218
219 /*
220  * Return the SMBIOS IPMI table entry info to the caller.  If we haven't
221  * searched the IPMI table yet, search it.  Otherwise, return a cached
222  * copy of the data.
223  */
224 int
225 ipmi_smbios_identify(struct ipmi_get_info *info)
226 {
227
228         mtx_lock(&ipmi_info_mtx);
229         switch (ipmi_probed) {
230         case 0:
231                 /* Need to probe the SMBIOS table. */
232                 ipmi_probed++;
233                 mtx_unlock(&ipmi_info_mtx);
234                 ipmi_smbios_probe(&ipmi_info);
235                 mtx_lock(&ipmi_info_mtx);
236                 ipmi_probed++;
237                 wakeup(&ipmi_info);
238                 break;
239         case 1:
240                 /* Another thread is currently probing the table, so wait. */
241                 while (ipmi_probed == 1)
242                         msleep(&ipmi_info, &ipmi_info_mtx, 0, "ipmi info", 0);
243                 break;
244         default:
245                 /* The cached data is available. */
246                 break;
247         }
248
249         bcopy(&ipmi_info, info, sizeof(ipmi_info));
250         mtx_unlock(&ipmi_info_mtx);
251
252         return (info->iface_type != 0);
253 }
254
255 static int
256 smbios_cksum(struct smbios_eps *e)
257 {
258         u_int8_t *ptr;
259         u_int8_t cksum;
260         int i;
261
262         ptr = (u_int8_t *)e;
263         cksum = 0;
264         for (i = 0; i < e->length; i++) {
265                 cksum += ptr[i];
266         }
267
268         return (cksum);
269 }