1 /* 2 * Copyright 2019 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 */ 23 #include <linux/pci.h> 24 25 #include "amdgpu.h" 26 #include "amdgpu_i2c.h" 27 #include "smu_v11_0_i2c.h" 28 #include "atom.h" 29 #include "amdgpu_fru_eeprom.h" 30 #include "amdgpu_eeprom.h" 31 32 #define FRU_EEPROM_MADDR_6 0x60000 33 #define FRU_EEPROM_MADDR_8 0x80000 34 #define FRU_EEPROM_MADDR_INV 0xFFFFF 35 36 static bool is_fru_eeprom_supported(struct amdgpu_device *adev, u32 *fru_addr) 37 { 38 /* Only server cards have the FRU EEPROM 39 * TODO: See if we can figure this out dynamically instead of 40 * having to parse VBIOS versions. 41 */ 42 struct atom_context *atom_ctx = adev->mode_info.atom_context; 43 44 /* The i2c access is blocked on VF 45 * TODO: Need other way to get the info 46 * Also, FRU not valid for APU devices. 47 */ 48 if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU)) 49 return false; 50 51 /* The default I2C EEPROM address of the FRU. 52 */ 53 if (fru_addr) 54 *fru_addr = FRU_EEPROM_MADDR_8; 55 56 /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification, 57 * we can use just the "DXXX" portion. If there were more models, we 58 * could convert the 3 characters to a hex integer and use a switch 59 * for ease/speed/readability. For now, 2 string comparisons are 60 * reasonable and not too expensive 61 */ 62 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) { 63 case IP_VERSION(11, 0, 2): 64 switch (adev->asic_type) { 65 case CHIP_VEGA20: 66 /* D161 and D163 are the VG20 server SKUs */ 67 if (atom_ctx && (strnstr(atom_ctx->vbios_pn, "D161", 68 sizeof(atom_ctx->vbios_pn)) || 69 strnstr(atom_ctx->vbios_pn, "D163", 70 sizeof(atom_ctx->vbios_pn)))) { 71 if (fru_addr) 72 *fru_addr = FRU_EEPROM_MADDR_6; 73 return true; 74 } else { 75 return false; 76 } 77 case CHIP_ARCTURUS: 78 default: 79 return false; 80 } 81 case IP_VERSION(11, 0, 7): 82 if (atom_ctx && strnstr(atom_ctx->vbios_pn, "D603", 83 sizeof(atom_ctx->vbios_pn))) { 84 if (strnstr(atom_ctx->vbios_pn, "D603GLXE", 85 sizeof(atom_ctx->vbios_pn))) { 86 return false; 87 } 88 89 if (fru_addr) 90 *fru_addr = FRU_EEPROM_MADDR_6; 91 return true; 92 93 } else { 94 return false; 95 } 96 case IP_VERSION(13, 0, 2): 97 /* All Aldebaran SKUs have an FRU */ 98 if (atom_ctx && !strnstr(atom_ctx->vbios_pn, "D673", 99 sizeof(atom_ctx->vbios_pn))) 100 if (fru_addr) 101 *fru_addr = FRU_EEPROM_MADDR_6; 102 return true; 103 case IP_VERSION(13, 0, 6): 104 case IP_VERSION(13, 0, 14): 105 if (fru_addr) 106 *fru_addr = FRU_EEPROM_MADDR_8; 107 return true; 108 case IP_VERSION(13, 0, 12): 109 if (fru_addr) 110 *fru_addr = FRU_EEPROM_MADDR_INV; 111 return true; 112 default: 113 return false; 114 } 115 } 116 117 int amdgpu_fru_get_product_info(struct amdgpu_device *adev) 118 { 119 struct amdgpu_fru_info *fru_info; 120 unsigned char buf[8], *pia; 121 u32 addr, fru_addr; 122 int size, len; 123 u8 csum; 124 125 if (!is_fru_eeprom_supported(adev, &fru_addr)) 126 return 0; 127 128 /* FRU data avaialble, but no direct EEPROM access */ 129 if (fru_addr == FRU_EEPROM_MADDR_INV) 130 return 0; 131 132 if (!adev->fru_info) { 133 adev->fru_info = kzalloc(sizeof(*adev->fru_info), GFP_KERNEL); 134 if (!adev->fru_info) 135 return -ENOMEM; 136 } 137 138 fru_info = adev->fru_info; 139 /* For Arcturus-and-later, default value of serial_number is unique_id 140 * so convert it to a 16-digit HEX string for convenience and 141 * backwards-compatibility. 142 */ 143 sprintf(fru_info->serial, "%llx", adev->unique_id); 144 145 /* If algo exists, it means that the i2c_adapter's initialized */ 146 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) { 147 dev_warn(adev->dev, 148 "Cannot access FRU, EEPROM accessor not initialized"); 149 return -ENODEV; 150 } 151 152 /* Read the IPMI Common header */ 153 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf, 154 sizeof(buf)); 155 if (len != 8) { 156 dev_err(adev->dev, "Couldn't read the IPMI Common Header: %d", 157 len); 158 return len < 0 ? len : -EIO; 159 } 160 161 if (buf[0] != 1) { 162 dev_err(adev->dev, "Bad IPMI Common Header version: 0x%02x", 163 buf[0]); 164 return -EIO; 165 } 166 167 for (csum = 0; len > 0; len--) 168 csum += buf[len - 1]; 169 if (csum) { 170 dev_err(adev->dev, "Bad IPMI Common Header checksum: 0x%02x", 171 csum); 172 return -EIO; 173 } 174 175 /* Get the offset to the Product Info Area (PIA). */ 176 addr = buf[4] * 8; 177 if (!addr) 178 return 0; 179 180 /* Get the absolute address to the PIA. */ 181 addr += fru_addr; 182 183 /* Read the header of the PIA. */ 184 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3); 185 if (len != 3) { 186 dev_err(adev->dev, 187 "Couldn't read the Product Info Area header: %d", len); 188 return len < 0 ? len : -EIO; 189 } 190 191 if (buf[0] != 1) { 192 dev_err(adev->dev, "Bad IPMI Product Info Area version: 0x%02x", 193 buf[0]); 194 return -EIO; 195 } 196 197 size = buf[1] * 8; 198 pia = kzalloc(size, GFP_KERNEL); 199 if (!pia) 200 return -ENOMEM; 201 202 /* Read the whole PIA. */ 203 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size); 204 if (len != size) { 205 kfree(pia); 206 dev_err(adev->dev, "Couldn't read the Product Info Area: %d", 207 len); 208 return len < 0 ? len : -EIO; 209 } 210 211 for (csum = 0; size > 0; size--) 212 csum += pia[size - 1]; 213 if (csum) { 214 dev_err(adev->dev, "Bad Product Info Area checksum: 0x%02x", 215 csum); 216 kfree(pia); 217 return -EIO; 218 } 219 220 /* Now extract useful information from the PIA. 221 * 222 * Read Manufacturer Name field whose length is [3]. 223 */ 224 addr = 3; 225 if (addr + 1 >= len) 226 goto Out; 227 memcpy(fru_info->manufacturer_name, pia + addr + 1, 228 min_t(size_t, sizeof(fru_info->manufacturer_name), 229 pia[addr] & 0x3F)); 230 fru_info->manufacturer_name[sizeof(fru_info->manufacturer_name) - 1] = 231 '\0'; 232 233 /* Read Product Name field. */ 234 addr += 1 + (pia[addr] & 0x3F); 235 if (addr + 1 >= len) 236 goto Out; 237 memcpy(fru_info->product_name, pia + addr + 1, 238 min_t(size_t, sizeof(fru_info->product_name), pia[addr] & 0x3F)); 239 fru_info->product_name[sizeof(fru_info->product_name) - 1] = '\0'; 240 241 /* Go to the Product Part/Model Number field. */ 242 addr += 1 + (pia[addr] & 0x3F); 243 if (addr + 1 >= len) 244 goto Out; 245 memcpy(fru_info->product_number, pia + addr + 1, 246 min_t(size_t, sizeof(fru_info->product_number), 247 pia[addr] & 0x3F)); 248 fru_info->product_number[sizeof(fru_info->product_number) - 1] = '\0'; 249 250 /* Go to the Product Version field. */ 251 addr += 1 + (pia[addr] & 0x3F); 252 253 /* Go to the Product Serial Number field. */ 254 addr += 1 + (pia[addr] & 0x3F); 255 if (addr + 1 >= len) 256 goto Out; 257 memcpy(fru_info->serial, pia + addr + 1, 258 min_t(size_t, sizeof(fru_info->serial), pia[addr] & 0x3F)); 259 fru_info->serial[sizeof(fru_info->serial) - 1] = '\0'; 260 261 /* Asset Tag field */ 262 addr += 1 + (pia[addr] & 0x3F); 263 264 /* FRU File Id field. This could be 'null'. */ 265 addr += 1 + (pia[addr] & 0x3F); 266 if ((addr + 1 >= len) || !(pia[addr] & 0x3F)) 267 goto Out; 268 memcpy(fru_info->fru_id, pia + addr + 1, 269 min_t(size_t, sizeof(fru_info->fru_id), pia[addr] & 0x3F)); 270 fru_info->fru_id[sizeof(fru_info->fru_id) - 1] = '\0'; 271 272 Out: 273 kfree(pia); 274 return 0; 275 } 276 277 /** 278 * DOC: product_name 279 * 280 * The amdgpu driver provides a sysfs API for reporting the product name 281 * for the device 282 * The file product_name is used for this and returns the product name 283 * as returned from the FRU. 284 * NOTE: This is only available for certain server cards 285 */ 286 287 static ssize_t amdgpu_fru_product_name_show(struct device *dev, 288 struct device_attribute *attr, 289 char *buf) 290 { 291 struct drm_device *ddev = dev_get_drvdata(dev); 292 struct amdgpu_device *adev = drm_to_adev(ddev); 293 294 return sysfs_emit(buf, "%s\n", adev->fru_info->product_name); 295 } 296 297 static DEVICE_ATTR(product_name, 0444, amdgpu_fru_product_name_show, NULL); 298 299 /** 300 * DOC: product_number 301 * 302 * The amdgpu driver provides a sysfs API for reporting the part number 303 * for the device 304 * The file product_number is used for this and returns the part number 305 * as returned from the FRU. 306 * NOTE: This is only available for certain server cards 307 */ 308 309 static ssize_t amdgpu_fru_product_number_show(struct device *dev, 310 struct device_attribute *attr, 311 char *buf) 312 { 313 struct drm_device *ddev = dev_get_drvdata(dev); 314 struct amdgpu_device *adev = drm_to_adev(ddev); 315 316 return sysfs_emit(buf, "%s\n", adev->fru_info->product_number); 317 } 318 319 static DEVICE_ATTR(product_number, 0444, amdgpu_fru_product_number_show, NULL); 320 321 /** 322 * DOC: serial_number 323 * 324 * The amdgpu driver provides a sysfs API for reporting the serial number 325 * for the device 326 * The file serial_number is used for this and returns the serial number 327 * as returned from the FRU. 328 * NOTE: This is only available for certain server cards 329 */ 330 331 static ssize_t amdgpu_fru_serial_number_show(struct device *dev, 332 struct device_attribute *attr, 333 char *buf) 334 { 335 struct drm_device *ddev = dev_get_drvdata(dev); 336 struct amdgpu_device *adev = drm_to_adev(ddev); 337 338 return sysfs_emit(buf, "%s\n", adev->fru_info->serial); 339 } 340 341 static DEVICE_ATTR(serial_number, 0444, amdgpu_fru_serial_number_show, NULL); 342 343 /** 344 * DOC: fru_id 345 * 346 * The amdgpu driver provides a sysfs API for reporting FRU File Id 347 * for the device. 348 * The file fru_id is used for this and returns the File Id value 349 * as returned from the FRU. 350 * NOTE: This is only available for certain server cards 351 */ 352 353 static ssize_t amdgpu_fru_id_show(struct device *dev, 354 struct device_attribute *attr, char *buf) 355 { 356 struct drm_device *ddev = dev_get_drvdata(dev); 357 struct amdgpu_device *adev = drm_to_adev(ddev); 358 359 return sysfs_emit(buf, "%s\n", adev->fru_info->fru_id); 360 } 361 362 static DEVICE_ATTR(fru_id, 0444, amdgpu_fru_id_show, NULL); 363 364 /** 365 * DOC: manufacturer 366 * 367 * The amdgpu driver provides a sysfs API for reporting manufacturer name from 368 * FRU information. 369 * The file manufacturer returns the value as returned from the FRU. 370 * NOTE: This is only available for certain server cards 371 */ 372 373 static ssize_t amdgpu_fru_manufacturer_name_show(struct device *dev, 374 struct device_attribute *attr, 375 char *buf) 376 { 377 struct drm_device *ddev = dev_get_drvdata(dev); 378 struct amdgpu_device *adev = drm_to_adev(ddev); 379 380 return sysfs_emit(buf, "%s\n", adev->fru_info->manufacturer_name); 381 } 382 383 static DEVICE_ATTR(manufacturer, 0444, amdgpu_fru_manufacturer_name_show, NULL); 384 385 static const struct attribute *amdgpu_fru_attributes[] = { 386 &dev_attr_product_name.attr, 387 &dev_attr_product_number.attr, 388 &dev_attr_serial_number.attr, 389 &dev_attr_fru_id.attr, 390 &dev_attr_manufacturer.attr, 391 NULL 392 }; 393 394 int amdgpu_fru_sysfs_init(struct amdgpu_device *adev) 395 { 396 if (!is_fru_eeprom_supported(adev, NULL) || !adev->fru_info) 397 return 0; 398 399 return sysfs_create_files(&adev->dev->kobj, amdgpu_fru_attributes); 400 } 401 402 void amdgpu_fru_sysfs_fini(struct amdgpu_device *adev) 403 { 404 if (!adev->fru_info) 405 return; 406 407 sysfs_remove_files(&adev->dev->kobj, amdgpu_fru_attributes); 408 } 409