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 case IP_VERSION(15, 0, 8): 110 if (fru_addr) 111 *fru_addr = FRU_EEPROM_MADDR_INV; 112 return true; 113 default: 114 return false; 115 } 116 } 117 118 int amdgpu_fru_get_product_info(struct amdgpu_device *adev) 119 { 120 struct amdgpu_fru_info *fru_info; 121 unsigned char buf[8], *pia; 122 u32 addr, fru_addr; 123 int size, len; 124 u8 csum; 125 126 if (!is_fru_eeprom_supported(adev, &fru_addr)) 127 return 0; 128 129 /* FRU data avaialble, but no direct EEPROM access */ 130 if (fru_addr == FRU_EEPROM_MADDR_INV) 131 return 0; 132 133 if (!adev->fru_info) { 134 adev->fru_info = kzalloc_obj(*adev->fru_info); 135 if (!adev->fru_info) 136 return -ENOMEM; 137 } 138 139 fru_info = adev->fru_info; 140 /* For Arcturus-and-later, default value of serial_number is unique_id 141 * so convert it to a 16-digit HEX string for convenience and 142 * backwards-compatibility. 143 */ 144 sprintf(fru_info->serial, "%llx", adev->unique_id); 145 146 /* If algo exists, it means that the i2c_adapter's initialized */ 147 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) { 148 dev_warn(adev->dev, 149 "Cannot access FRU, EEPROM accessor not initialized"); 150 return -ENODEV; 151 } 152 153 /* Read the IPMI Common header */ 154 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf, 155 sizeof(buf)); 156 if (len != 8) { 157 dev_err(adev->dev, "Couldn't read the IPMI Common Header: %d", 158 len); 159 return len < 0 ? len : -EIO; 160 } 161 162 if (buf[0] != 1) { 163 dev_err(adev->dev, "Bad IPMI Common Header version: 0x%02x", 164 buf[0]); 165 return -EIO; 166 } 167 168 for (csum = 0; len > 0; len--) 169 csum += buf[len - 1]; 170 if (csum) { 171 dev_err(adev->dev, "Bad IPMI Common Header checksum: 0x%02x", 172 csum); 173 return -EIO; 174 } 175 176 /* Get the offset to the Product Info Area (PIA). */ 177 addr = buf[4] * 8; 178 if (!addr) 179 return 0; 180 181 /* Get the absolute address to the PIA. */ 182 addr += fru_addr; 183 184 /* Read the header of the PIA. */ 185 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3); 186 if (len != 3) { 187 dev_err(adev->dev, 188 "Couldn't read the Product Info Area header: %d", len); 189 return len < 0 ? len : -EIO; 190 } 191 192 if (buf[0] != 1) { 193 dev_err(adev->dev, "Bad IPMI Product Info Area version: 0x%02x", 194 buf[0]); 195 return -EIO; 196 } 197 198 size = buf[1] * 8; 199 pia = kzalloc(size, GFP_KERNEL); 200 if (!pia) 201 return -ENOMEM; 202 203 /* Read the whole PIA. */ 204 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size); 205 if (len != size) { 206 kfree(pia); 207 dev_err(adev->dev, "Couldn't read the Product Info Area: %d", 208 len); 209 return len < 0 ? len : -EIO; 210 } 211 212 for (csum = 0; size > 0; size--) 213 csum += pia[size - 1]; 214 if (csum) { 215 dev_err(adev->dev, "Bad Product Info Area checksum: 0x%02x", 216 csum); 217 kfree(pia); 218 return -EIO; 219 } 220 221 /* Now extract useful information from the PIA. 222 * 223 * Read Manufacturer Name field whose length is [3]. 224 */ 225 addr = 3; 226 if (addr + 1 >= len) 227 goto Out; 228 memcpy(fru_info->manufacturer_name, pia + addr + 1, 229 min_t(size_t, sizeof(fru_info->manufacturer_name), 230 pia[addr] & 0x3F)); 231 fru_info->manufacturer_name[sizeof(fru_info->manufacturer_name) - 1] = 232 '\0'; 233 234 /* Read Product Name field. */ 235 addr += 1 + (pia[addr] & 0x3F); 236 if (addr + 1 >= len) 237 goto Out; 238 memcpy(fru_info->product_name, pia + addr + 1, 239 min_t(size_t, sizeof(fru_info->product_name), pia[addr] & 0x3F)); 240 fru_info->product_name[sizeof(fru_info->product_name) - 1] = '\0'; 241 242 /* Go to the Product Part/Model Number field. */ 243 addr += 1 + (pia[addr] & 0x3F); 244 if (addr + 1 >= len) 245 goto Out; 246 memcpy(fru_info->product_number, pia + addr + 1, 247 min_t(size_t, sizeof(fru_info->product_number), 248 pia[addr] & 0x3F)); 249 fru_info->product_number[sizeof(fru_info->product_number) - 1] = '\0'; 250 251 /* Go to the Product Version field. */ 252 addr += 1 + (pia[addr] & 0x3F); 253 254 /* Go to the Product Serial Number field. */ 255 addr += 1 + (pia[addr] & 0x3F); 256 if (addr + 1 >= len) 257 goto Out; 258 memcpy(fru_info->serial, pia + addr + 1, 259 min_t(size_t, sizeof(fru_info->serial), pia[addr] & 0x3F)); 260 fru_info->serial[sizeof(fru_info->serial) - 1] = '\0'; 261 262 /* Asset Tag field */ 263 addr += 1 + (pia[addr] & 0x3F); 264 265 /* FRU File Id field. This could be 'null'. */ 266 addr += 1 + (pia[addr] & 0x3F); 267 if ((addr + 1 >= len) || !(pia[addr] & 0x3F)) 268 goto Out; 269 memcpy(fru_info->fru_id, pia + addr + 1, 270 min_t(size_t, sizeof(fru_info->fru_id), pia[addr] & 0x3F)); 271 fru_info->fru_id[sizeof(fru_info->fru_id) - 1] = '\0'; 272 273 Out: 274 kfree(pia); 275 return 0; 276 } 277 278 /** 279 * DOC: product_name 280 * 281 * The amdgpu driver provides a sysfs API for reporting the product name 282 * for the device 283 * The file product_name is used for this and returns the product name 284 * as returned from the FRU. 285 * NOTE: This is only available for certain server cards 286 */ 287 288 static ssize_t amdgpu_fru_product_name_show(struct device *dev, 289 struct device_attribute *attr, 290 char *buf) 291 { 292 struct drm_device *ddev = dev_get_drvdata(dev); 293 struct amdgpu_device *adev = drm_to_adev(ddev); 294 295 return sysfs_emit(buf, "%s\n", adev->fru_info->product_name); 296 } 297 298 static DEVICE_ATTR(product_name, 0444, amdgpu_fru_product_name_show, NULL); 299 300 /** 301 * DOC: product_number 302 * 303 * The amdgpu driver provides a sysfs API for reporting the part number 304 * for the device 305 * The file product_number is used for this and returns the part number 306 * as returned from the FRU. 307 * NOTE: This is only available for certain server cards 308 */ 309 310 static ssize_t amdgpu_fru_product_number_show(struct device *dev, 311 struct device_attribute *attr, 312 char *buf) 313 { 314 struct drm_device *ddev = dev_get_drvdata(dev); 315 struct amdgpu_device *adev = drm_to_adev(ddev); 316 317 return sysfs_emit(buf, "%s\n", adev->fru_info->product_number); 318 } 319 320 static DEVICE_ATTR(product_number, 0444, amdgpu_fru_product_number_show, NULL); 321 322 /** 323 * DOC: serial_number 324 * 325 * The amdgpu driver provides a sysfs API for reporting the serial number 326 * for the device 327 * The file serial_number is used for this and returns the serial number 328 * as returned from the FRU. 329 * NOTE: This is only available for certain server cards 330 */ 331 332 static ssize_t amdgpu_fru_serial_number_show(struct device *dev, 333 struct device_attribute *attr, 334 char *buf) 335 { 336 struct drm_device *ddev = dev_get_drvdata(dev); 337 struct amdgpu_device *adev = drm_to_adev(ddev); 338 339 return sysfs_emit(buf, "%s\n", adev->fru_info->serial); 340 } 341 342 static DEVICE_ATTR(serial_number, 0444, amdgpu_fru_serial_number_show, NULL); 343 344 /** 345 * DOC: fru_id 346 * 347 * The amdgpu driver provides a sysfs API for reporting FRU File Id 348 * for the device. 349 * The file fru_id is used for this and returns the File Id value 350 * as returned from the FRU. 351 * NOTE: This is only available for certain server cards 352 */ 353 354 static ssize_t amdgpu_fru_id_show(struct device *dev, 355 struct device_attribute *attr, char *buf) 356 { 357 struct drm_device *ddev = dev_get_drvdata(dev); 358 struct amdgpu_device *adev = drm_to_adev(ddev); 359 360 return sysfs_emit(buf, "%s\n", adev->fru_info->fru_id); 361 } 362 363 static DEVICE_ATTR(fru_id, 0444, amdgpu_fru_id_show, NULL); 364 365 /** 366 * DOC: manufacturer 367 * 368 * The amdgpu driver provides a sysfs API for reporting manufacturer name from 369 * FRU information. 370 * The file manufacturer returns the value as returned from the FRU. 371 * NOTE: This is only available for certain server cards 372 */ 373 374 static ssize_t amdgpu_fru_manufacturer_name_show(struct device *dev, 375 struct device_attribute *attr, 376 char *buf) 377 { 378 struct drm_device *ddev = dev_get_drvdata(dev); 379 struct amdgpu_device *adev = drm_to_adev(ddev); 380 381 return sysfs_emit(buf, "%s\n", adev->fru_info->manufacturer_name); 382 } 383 384 static DEVICE_ATTR(manufacturer, 0444, amdgpu_fru_manufacturer_name_show, NULL); 385 386 static const struct attribute *amdgpu_fru_attributes[] = { 387 &dev_attr_product_name.attr, 388 &dev_attr_product_number.attr, 389 &dev_attr_serial_number.attr, 390 &dev_attr_fru_id.attr, 391 &dev_attr_manufacturer.attr, 392 NULL 393 }; 394 395 int amdgpu_fru_sysfs_init(struct amdgpu_device *adev) 396 { 397 if (!is_fru_eeprom_supported(adev, NULL) || !adev->fru_info) 398 return 0; 399 400 return sysfs_create_files(&adev->dev->kobj, amdgpu_fru_attributes); 401 } 402 403 void amdgpu_fru_sysfs_fini(struct amdgpu_device *adev) 404 { 405 if (!adev->fru_info) 406 return; 407 408 sysfs_remove_files(&adev->dev->kobj, amdgpu_fru_attributes); 409 } 410