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 * $FreeBSD: head/sys/dev/ipmi/ipmi_smbios.c 241027 2012-09-28 11:59:32Z jhb $ 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/conf.h> 36 37 #include <vm/vm.h> 38 #include <vm/pmap.h> 39 #include <machine/pc/bios.h> 40 41 #ifdef LOCAL_MODULE 42 #include <ipmi.h> 43 #include <ipmivars.h> 44 #else 45 #include <sys/ipmi.h> 46 #include <dev/misc/ipmi/ipmivars.h> 47 #endif 48 49 #if __FreeBSD_version < 602110 50 #define pmap_mapbios pmap_mapdev 51 #define pmap_unmapbios pmap_unmapdev 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 typedef void (*smbios_callback_t)(struct smbios_structure_header *, void *); 82 83 static struct ipmi_get_info ipmi_info; 84 static int ipmi_probed; 85 static struct lock ipmi_info_lock; 86 LOCK_SYSINIT(ipmi_info, &ipmi_info_lock, "ipmi info", LK_CANRECURSE); 87 88 static void ipmi_smbios_probe(struct ipmi_get_info *); 89 static int smbios_cksum(struct smbios_eps *); 90 static void smbios_walk_table(uint8_t *, int, 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 kprintf("SMBIOS: Invalid register spacing\n"); 124 return; 125 } 126 break; 127 case SSIF_MODE: 128 if ((s->base_address & 0xffffffffffffff00) != 0) { 129 kprintf("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 kprintf("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 *p, int entries, smbios_callback_t cb, void *arg) 150 { 151 struct smbios_structure_header *s; 152 153 while (entries--) { 154 s = (struct smbios_structure_header *)p; 155 cb(s, arg); 156 157 /* 158 * Look for a double-nul after the end of the 159 * formatted area of this structure. 160 */ 161 p += s->length; 162 while (!(p[0] == 0 && p[1] == 0)) 163 p++; 164 165 /* 166 * Skip over the double-nul to the start of the next 167 * structure. 168 */ 169 p += 2; 170 } 171 } 172 173 /* 174 * Walk the SMBIOS table looking for an IPMI (type 38) entry. If we find 175 * one, return the parsed data in the passed in ipmi_get_info structure and 176 * return true. If we don't find one, return false. 177 */ 178 static void 179 ipmi_smbios_probe(struct ipmi_get_info *info) 180 { 181 struct smbios_eps *header; 182 void *table; 183 u_int32_t addr; 184 185 bzero(info, sizeof(struct ipmi_get_info)); 186 187 /* Find the SMBIOS table header. */ 188 addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN, 189 SMBIOS_STEP, SMBIOS_OFF); 190 if (addr == 0) 191 return; 192 193 /* 194 * Map the header. We first map a fixed size to get the actual 195 * length and then map it a second time with the actual length so 196 * we can verify the checksum. 197 */ 198 header = pmap_mapbios(addr, sizeof(struct smbios_eps)); 199 table = pmap_mapbios(addr, header->length); 200 pmap_unmapbios((vm_offset_t)header, sizeof(struct smbios_eps)); 201 header = table; 202 if (smbios_cksum(header) != 0) { 203 pmap_unmapbios((vm_offset_t)header, header->length); 204 return; 205 } 206 207 /* Now map the actual table and walk it looking for an IPMI entry. */ 208 table = pmap_mapbios(header->structure_table_address, 209 header->structure_table_length); 210 smbios_walk_table(table, header->number_structures, smbios_ipmi_info, 211 info); 212 213 /* Unmap everything. */ 214 pmap_unmapbios((vm_offset_t)table, header->structure_table_length); 215 pmap_unmapbios((vm_offset_t)header, header->length); 216 } 217 218 /* 219 * Return the SMBIOS IPMI table entry info to the caller. If we haven't 220 * searched the IPMI table yet, search it. Otherwise, return a cached 221 * copy of the data. 222 */ 223 int 224 ipmi_smbios_identify(struct ipmi_get_info *info) 225 { 226 227 lockmgr(&ipmi_info_lock, LK_EXCLUSIVE); 228 switch (ipmi_probed) { 229 case 0: 230 /* Need to probe the SMBIOS table. */ 231 ipmi_probed++; 232 lockmgr(&ipmi_info_lock, LK_RELEASE); 233 ipmi_smbios_probe(&ipmi_info); 234 lockmgr(&ipmi_info_lock, LK_EXCLUSIVE); 235 ipmi_probed++; 236 wakeup(&ipmi_info); 237 break; 238 case 1: 239 /* Another thread is currently probing the table, so wait. */ 240 while (ipmi_probed == 1) 241 lksleep(&ipmi_info, &ipmi_info_lock, 0, "ipmi info", 0); 242 break; 243 default: 244 /* The cached data is available. */ 245 break; 246 } 247 248 bcopy(&ipmi_info, info, sizeof(ipmi_info)); 249 lockmgr(&ipmi_info_lock, LK_RELEASE); 250 251 return (info->iface_type != 0); 252 } 253 254 static int 255 smbios_cksum(struct smbios_eps *e) 256 { 257 u_int8_t *ptr; 258 u_int8_t cksum; 259 int i; 260 261 ptr = (u_int8_t *)e; 262 cksum = 0; 263 for (i = 0; i < e->length; i++) { 264 cksum += ptr[i]; 265 } 266 267 return (cksum); 268 } 269