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 __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 #include <sys/efi.h> 40 41 #include <vm/vm.h> 42 #include <vm/pmap.h> 43 #if defined(__amd64__) || defined(__i386__) 44 #include <machine/pc/bios.h> 45 #endif 46 #include <dev/smbios/smbios.h> 47 48 #ifdef LOCAL_MODULE 49 #include <ipmi.h> 50 #include <ipmivars.h> 51 #else 52 #include <sys/ipmi.h> 53 #include <dev/ipmi/ipmivars.h> 54 #endif 55 56 struct ipmi_entry { 57 uint8_t type; 58 uint8_t length; 59 uint16_t handle; 60 uint8_t interface_type; 61 uint8_t spec_revision; 62 uint8_t i2c_slave_address; 63 uint8_t NV_storage_device_address; 64 uint64_t base_address; 65 uint8_t base_address_modifier; 66 uint8_t interrupt_number; 67 }; 68 69 /* Fields in the base_address field of an IPMI entry. */ 70 #define IPMI_BAR_MODE(ba) ((ba) & 0x0000000000000001) 71 #define IPMI_BAR_ADDR(ba) ((ba) & 0xfffffffffffffffe) 72 73 /* Fields in the base_address_modifier field of an IPMI entry. */ 74 #define IPMI_BAM_IRQ_TRIGGER 0x01 75 #define IPMI_BAM_IRQ_POLARITY 0x02 76 #define IPMI_BAM_IRQ_VALID 0x08 77 #define IPMI_BAM_ADDR_LSB(bam) (((bam) & 0x10) >> 4) 78 #define IPMI_BAM_REG_SPACING(bam) (((bam) & 0xc0) >> 6) 79 #define SPACING_8 0x0 80 #define SPACING_32 0x1 81 #define SPACING_16 0x2 82 83 static struct ipmi_get_info ipmi_info; 84 static int ipmi_probed; 85 static struct mtx ipmi_info_mtx; 86 MTX_SYSINIT(ipmi_info, &ipmi_info_mtx, "ipmi info", MTX_DEF); 87 88 static void ipmi_smbios_probe(struct ipmi_get_info *); 89 static int smbios_cksum(struct smbios_eps *); 90 static void smbios_ipmi_info(struct smbios_structure_header *, void *); 91 92 static void 93 smbios_ipmi_info(struct smbios_structure_header *h, void *arg) 94 { 95 struct ipmi_get_info *info; 96 struct ipmi_entry *s; 97 98 if (h->type != 38 || h->length < 99 offsetof(struct ipmi_entry, interrupt_number)) 100 return; 101 s = (struct ipmi_entry *)h; 102 info = arg; 103 bzero(info, sizeof(struct ipmi_get_info)); 104 switch (s->interface_type) { 105 case KCS_MODE: 106 case SMIC_MODE: 107 case BT_MODE: 108 info->address = IPMI_BAR_ADDR(s->base_address) | 109 IPMI_BAM_ADDR_LSB(s->base_address_modifier); 110 info->io_mode = IPMI_BAR_MODE(s->base_address); 111 switch (IPMI_BAM_REG_SPACING(s->base_address_modifier)) { 112 case SPACING_8: 113 info->offset = 1; 114 break; 115 case SPACING_32: 116 info->offset = 4; 117 break; 118 case SPACING_16: 119 info->offset = 2; 120 break; 121 default: 122 printf("SMBIOS: Invalid register spacing\n"); 123 return; 124 } 125 break; 126 case SSIF_MODE: 127 if ((s->base_address & 0xffffffffffffff00) != 0) { 128 printf("SMBIOS: Invalid SSIF SMBus address, using BMC I2C slave address instead\n"); 129 info->address = s->i2c_slave_address; 130 break; 131 } 132 info->address = IPMI_BAR_ADDR(s->base_address); 133 break; 134 default: 135 return; 136 } 137 if (s->length > offsetof(struct ipmi_entry, interrupt_number)) { 138 if (s->interrupt_number > 15) 139 printf("SMBIOS: Non-ISA IRQ %d for IPMI\n", 140 s->interrupt_number); 141 else 142 info->irq = s->interrupt_number; 143 } 144 info->iface_type = s->interface_type; 145 } 146 147 /* 148 * Walk the SMBIOS table looking for an IPMI (type 38) entry. If we find 149 * one, return the parsed data in the passed in ipmi_get_info structure and 150 * return true. If we don't find one, return false. 151 */ 152 static void 153 ipmi_smbios_probe(struct ipmi_get_info *info) 154 { 155 #ifdef ARCH_MAY_USE_EFI 156 struct uuid efi_smbios; 157 void *addr_efi; 158 #endif 159 struct smbios_eps *header; 160 void *table; 161 u_int32_t addr; 162 163 addr = 0; 164 bzero(info, sizeof(struct ipmi_get_info)); 165 166 #ifdef ARCH_MAY_USE_EFI 167 efi_smbios = (struct uuid)EFI_TABLE_SMBIOS; 168 if (!efi_get_table(&efi_smbios, &addr_efi)) 169 addr = (vm_paddr_t)addr_efi; 170 #endif 171 172 if (addr == 0) 173 /* Find the SMBIOS table header. */ 174 addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN, 175 SMBIOS_STEP, SMBIOS_OFF); 176 if (addr == 0) 177 return; 178 179 /* 180 * Map the header. We first map a fixed size to get the actual 181 * length and then map it a second time with the actual length so 182 * we can verify the checksum. 183 */ 184 header = pmap_mapbios(addr, sizeof(struct smbios_eps)); 185 table = pmap_mapbios(addr, header->length); 186 pmap_unmapbios(header, sizeof(struct smbios_eps)); 187 header = table; 188 if (smbios_cksum(header) != 0) { 189 pmap_unmapbios(header, header->length); 190 return; 191 } 192 193 /* Now map the actual table and walk it looking for an IPMI entry. */ 194 table = pmap_mapbios(header->structure_table_address, 195 header->structure_table_length); 196 smbios_walk_table(table, header->number_structures, smbios_ipmi_info, 197 info); 198 199 /* Unmap everything. */ 200 pmap_unmapbios(table, header->structure_table_length); 201 pmap_unmapbios(header, header->length); 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 240 static int 241 smbios_cksum(struct smbios_eps *e) 242 { 243 u_int8_t *ptr; 244 u_int8_t cksum; 245 int i; 246 247 ptr = (u_int8_t *)e; 248 cksum = 0; 249 for (i = 0; i < e->length; i++) { 250 cksum += ptr[i]; 251 } 252 253 return (cksum); 254 } 255