Commit 0dbf2c56 authored by Luben Tuikov's avatar Luben Tuikov Committed by Alex Deucher

drm/amdgpu: Interpret IPMI data for product information (v2)

Don't assume FRU MCU memory locations for the FRU data fields, or their sizes,
instead read and interpret the IPMI data, as stipulated in the IPMI spec
version 1.0 rev 1.2.

Extract the Product Name, Product Part/Model Number, and the Product Serial
Number by interpreting the IPMI data.

Check the checksums of the stored IPMI data to make sure we don't read and
give corrupted data back the the user.

Eliminate small I2C reads, and instead read the whole Product Info Area in one
go, and then extract the information we're seeking from it.

Eliminates a whole function, making this file smaller.

v2: Clarify changes in the commit message.

Cc: Alex Deucher <Alexander.Deucher@amd.com>
Cc: Kent Russell <kent.russell@amd.com>
Signed-off-by: default avatarLuben Tuikov <luben.tuikov@amd.com>
Tested-by: default avatarKent Russell <kent.russell@amd.com>
Reviewed-by: default avatarKent Russell <kent.russell@amd.com>
Reviewed-by: default avatarAlex Deucher <Alexander.Deucher@amd.com>
Signed-off-by: default avatarAlex Deucher <alexander.deucher@amd.com>
parent afbe5d1e
...@@ -94,39 +94,12 @@ static bool is_fru_eeprom_supported(struct amdgpu_device *adev, u32 *fru_addr) ...@@ -94,39 +94,12 @@ static bool is_fru_eeprom_supported(struct amdgpu_device *adev, u32 *fru_addr)
} }
} }
static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
unsigned char *buf, size_t buf_size)
{
int ret;
u8 size;
ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr, buf, 1);
if (ret < 1) {
DRM_WARN("FRU: Failed to get size field");
return ret;
}
/* The size returned by the i2c requires subtraction of 0xC0 since the
* size apparently always reports as 0xC0+actual size.
*/
size = buf[0] & 0x3F;
size = min_t(size_t, size, buf_size);
ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr + 1,
buf, size);
if (ret < 1) {
DRM_WARN("FRU: Failed to get data field");
return ret;
}
return size;
}
int amdgpu_fru_get_product_info(struct amdgpu_device *adev) int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
{ {
unsigned char buf[AMDGPU_PRODUCT_NAME_LEN]; unsigned char buf[8], *pia;
u32 addrptr, fru_addr; u32 addr, fru_addr;
int size, len; int size, len;
u8 csum;
if (!is_fru_eeprom_supported(adev, &fru_addr)) if (!is_fru_eeprom_supported(adev, &fru_addr))
return 0; return 0;
...@@ -137,88 +110,102 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev) ...@@ -137,88 +110,102 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
return -ENODEV; return -ENODEV;
} }
/* There's a lot of repetition here. This is due to the FRU having /* Read the IPMI Common header */
* variable-length fields. To get the information, we have to find the len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
* size of each field, and then keep reading along and reading along sizeof(buf));
* until we get all of the data that we want. We use addrptr to track if (len != 8) {
* the address as we go DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
*/ return len < 0 ? len : -EIO;
/* The first fields are all of size 1-byte, from 0-7 are offsets that
* contain information that isn't useful to us.
* Bytes 8-a are all 1-byte and refer to the size of the entire struct,
* and the language field, so just start from 0xb, manufacturer size
*/
addrptr = fru_addr + 0xb;
size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
if (size < 1) {
DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
return -EINVAL;
} }
/* Increment the addrptr by the size of the field, and 1 due to the if (buf[0] != 1) {
* size field being 1 byte. This pattern continues below. DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
*/ return -EIO;
addrptr += size + 1;
size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
if (size < 1) {
DRM_ERROR("Failed to read FRU product name, ret:%d", size);
return -EINVAL;
} }
len = size; for (csum = 0; len > 0; len--)
if (len >= AMDGPU_PRODUCT_NAME_LEN) { csum += buf[len - 1];
DRM_WARN("FRU Product Name is larger than %d characters. This is likely a mistake", if (csum) {
AMDGPU_PRODUCT_NAME_LEN); DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
len = AMDGPU_PRODUCT_NAME_LEN - 1; return -EIO;
}
memcpy(adev->product_name, buf, len);
adev->product_name[len] = '\0';
addrptr += size + 1;
size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
if (size < 1) {
DRM_ERROR("Failed to read FRU product number, ret:%d", size);
return -EINVAL;
} }
len = size; /* Get the offset to the Product Info Area (PIA). */
/* Product number should only be 16 characters. Any more, addr = buf[4] * 8;
* and something could be wrong. Cap it at 16 to be safe if (!addr)
*/ return 0;
if (len >= sizeof(adev->product_number)) {
DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
len = sizeof(adev->product_number) - 1;
}
memcpy(adev->product_number, buf, len);
adev->product_number[len] = '\0';
addrptr += size + 1; /* Get the absolute address to the PIA. */
size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf)); addr += fru_addr;
if (size < 1) { /* Read the header of the PIA. */
DRM_ERROR("Failed to read FRU product version, ret:%d", size); len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
return -EINVAL; if (len != 3) {
DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
return len < 0 ? len : -EIO;
} }
addrptr += size + 1; if (buf[0] != 1) {
size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf)); DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
return -EIO;
}
if (size < 1) { size = buf[1] * 8;
DRM_ERROR("Failed to read FRU serial number, ret:%d", size); pia = kzalloc(size, GFP_KERNEL);
return -EINVAL; if (!pia)
return -ENOMEM;
/* Read the whole PIA. */
len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
if (len != size) {
kfree(pia);
DRM_ERROR("Couldn't read the Product Info Area: %d", len);
return len < 0 ? len : -EIO;
} }
len = size; for (csum = 0; size > 0; size--)
/* Serial number should only be 16 characters. Any more, csum += pia[size - 1];
* and something could be wrong. Cap it at 16 to be safe if (csum) {
*/ DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
if (len >= sizeof(adev->serial)) { return -EIO;
DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
len = sizeof(adev->serial) - 1;
} }
memcpy(adev->serial, buf, len);
adev->serial[len] = '\0';
/* Now extract useful information from the PIA.
*
* Skip the Manufacturer Name at [3] and go directly to
* the Product Name field.
*/
addr = 3 + 1 + (pia[3] & 0x3F);
if (addr + 1 >= len)
goto Out;
memcpy(adev->product_name, pia + addr + 1,
min_t(size_t,
sizeof(adev->product_name),
pia[addr] & 0x3F));
adev->product_name[sizeof(adev->product_name) - 1] = '\0';
/* Go to the Product Part/Model Number field. */
addr += 1 + (pia[addr] & 0x3F);
if (addr + 1 >= len)
goto Out;
memcpy(adev->product_number, pia + addr + 1,
min_t(size_t,
sizeof(adev->product_number),
pia[addr] & 0x3F));
adev->product_number[sizeof(adev->product_number) - 1] = '\0';
/* Go to the Product Version field. */
addr += 1 + (pia[addr] & 0x3F);
/* Go to the Product Serial Number field. */
addr += 1 + (pia[addr] & 0x3F);
if (addr + 1 >= len)
goto Out;
memcpy(adev->serial, pia + addr + 1, min_t(size_t,
sizeof(adev->serial),
pia[addr] & 0x3F));
adev->serial[sizeof(adev->serial) - 1] = '\0';
Out:
kfree(pia);
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment