Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: Luben Tuikov <luben.tuikov@amd.com>
Tested-by: Kent Russell <kent.russell@amd.com>
Reviewed-by: Kent Russell <kent.russell@amd.com>
Reviewed-by: Alex Deucher <Alexander.Deucher@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

authored by

Luben Tuikov and committed by
Alex Deucher
0dbf2c56 afbe5d1e

+96 -109
+96 -109
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c
··· 94 94 } 95 95 } 96 96 97 - static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr, 98 - unsigned char *buf, size_t buf_size) 99 - { 100 - int ret; 101 - u8 size; 102 - 103 - ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr, buf, 1); 104 - if (ret < 1) { 105 - DRM_WARN("FRU: Failed to get size field"); 106 - return ret; 107 - } 108 - 109 - /* The size returned by the i2c requires subtraction of 0xC0 since the 110 - * size apparently always reports as 0xC0+actual size. 111 - */ 112 - size = buf[0] & 0x3F; 113 - size = min_t(size_t, size, buf_size); 114 - 115 - ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr + 1, 116 - buf, size); 117 - if (ret < 1) { 118 - DRM_WARN("FRU: Failed to get data field"); 119 - return ret; 120 - } 121 - 122 - return size; 123 - } 124 - 125 97 int amdgpu_fru_get_product_info(struct amdgpu_device *adev) 126 98 { 127 - unsigned char buf[AMDGPU_PRODUCT_NAME_LEN]; 128 - u32 addrptr, fru_addr; 99 + unsigned char buf[8], *pia; 100 + u32 addr, fru_addr; 129 101 int size, len; 102 + u8 csum; 130 103 131 104 if (!is_fru_eeprom_supported(adev, &fru_addr)) 132 105 return 0; ··· 110 137 return -ENODEV; 111 138 } 112 139 113 - /* There's a lot of repetition here. This is due to the FRU having 114 - * variable-length fields. To get the information, we have to find the 115 - * size of each field, and then keep reading along and reading along 116 - * until we get all of the data that we want. We use addrptr to track 117 - * the address as we go 140 + /* Read the IPMI Common header */ 141 + len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf, 142 + sizeof(buf)); 143 + if (len != 8) { 144 + DRM_ERROR("Couldn't read the IPMI Common Header: %d", len); 145 + return len < 0 ? len : -EIO; 146 + } 147 + 148 + if (buf[0] != 1) { 149 + DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]); 150 + return -EIO; 151 + } 152 + 153 + for (csum = 0; len > 0; len--) 154 + csum += buf[len - 1]; 155 + if (csum) { 156 + DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum); 157 + return -EIO; 158 + } 159 + 160 + /* Get the offset to the Product Info Area (PIA). */ 161 + addr = buf[4] * 8; 162 + if (!addr) 163 + return 0; 164 + 165 + /* Get the absolute address to the PIA. */ 166 + addr += fru_addr; 167 + 168 + /* Read the header of the PIA. */ 169 + len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3); 170 + if (len != 3) { 171 + DRM_ERROR("Couldn't read the Product Info Area header: %d", len); 172 + return len < 0 ? len : -EIO; 173 + } 174 + 175 + if (buf[0] != 1) { 176 + DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]); 177 + return -EIO; 178 + } 179 + 180 + size = buf[1] * 8; 181 + pia = kzalloc(size, GFP_KERNEL); 182 + if (!pia) 183 + return -ENOMEM; 184 + 185 + /* Read the whole PIA. */ 186 + len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size); 187 + if (len != size) { 188 + kfree(pia); 189 + DRM_ERROR("Couldn't read the Product Info Area: %d", len); 190 + return len < 0 ? len : -EIO; 191 + } 192 + 193 + for (csum = 0; size > 0; size--) 194 + csum += pia[size - 1]; 195 + if (csum) { 196 + DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum); 197 + return -EIO; 198 + } 199 + 200 + /* Now extract useful information from the PIA. 201 + * 202 + * Skip the Manufacturer Name at [3] and go directly to 203 + * the Product Name field. 118 204 */ 205 + addr = 3 + 1 + (pia[3] & 0x3F); 206 + if (addr + 1 >= len) 207 + goto Out; 208 + memcpy(adev->product_name, pia + addr + 1, 209 + min_t(size_t, 210 + sizeof(adev->product_name), 211 + pia[addr] & 0x3F)); 212 + adev->product_name[sizeof(adev->product_name) - 1] = '\0'; 119 213 120 - /* The first fields are all of size 1-byte, from 0-7 are offsets that 121 - * contain information that isn't useful to us. 122 - * Bytes 8-a are all 1-byte and refer to the size of the entire struct, 123 - * and the language field, so just start from 0xb, manufacturer size 124 - */ 125 - addrptr = fru_addr + 0xb; 126 - size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf)); 127 - if (size < 1) { 128 - DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size); 129 - return -EINVAL; 130 - } 214 + /* Go to the Product Part/Model Number field. */ 215 + addr += 1 + (pia[addr] & 0x3F); 216 + if (addr + 1 >= len) 217 + goto Out; 218 + memcpy(adev->product_number, pia + addr + 1, 219 + min_t(size_t, 220 + sizeof(adev->product_number), 221 + pia[addr] & 0x3F)); 222 + adev->product_number[sizeof(adev->product_number) - 1] = '\0'; 131 223 132 - /* Increment the addrptr by the size of the field, and 1 due to the 133 - * size field being 1 byte. This pattern continues below. 134 - */ 135 - addrptr += size + 1; 136 - size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf)); 137 - if (size < 1) { 138 - DRM_ERROR("Failed to read FRU product name, ret:%d", size); 139 - return -EINVAL; 140 - } 224 + /* Go to the Product Version field. */ 225 + addr += 1 + (pia[addr] & 0x3F); 141 226 142 - len = size; 143 - if (len >= AMDGPU_PRODUCT_NAME_LEN) { 144 - DRM_WARN("FRU Product Name is larger than %d characters. This is likely a mistake", 145 - AMDGPU_PRODUCT_NAME_LEN); 146 - len = AMDGPU_PRODUCT_NAME_LEN - 1; 147 - } 148 - memcpy(adev->product_name, buf, len); 149 - adev->product_name[len] = '\0'; 150 - 151 - addrptr += size + 1; 152 - size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf)); 153 - if (size < 1) { 154 - DRM_ERROR("Failed to read FRU product number, ret:%d", size); 155 - return -EINVAL; 156 - } 157 - 158 - len = size; 159 - /* Product number should only be 16 characters. Any more, 160 - * and something could be wrong. Cap it at 16 to be safe 161 - */ 162 - if (len >= sizeof(adev->product_number)) { 163 - DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake"); 164 - len = sizeof(adev->product_number) - 1; 165 - } 166 - memcpy(adev->product_number, buf, len); 167 - adev->product_number[len] = '\0'; 168 - 169 - addrptr += size + 1; 170 - size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf)); 171 - 172 - if (size < 1) { 173 - DRM_ERROR("Failed to read FRU product version, ret:%d", size); 174 - return -EINVAL; 175 - } 176 - 177 - addrptr += size + 1; 178 - size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf)); 179 - 180 - if (size < 1) { 181 - DRM_ERROR("Failed to read FRU serial number, ret:%d", size); 182 - return -EINVAL; 183 - } 184 - 185 - len = size; 186 - /* Serial number should only be 16 characters. Any more, 187 - * and something could be wrong. Cap it at 16 to be safe 188 - */ 189 - if (len >= sizeof(adev->serial)) { 190 - DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake"); 191 - len = sizeof(adev->serial) - 1; 192 - } 193 - memcpy(adev->serial, buf, len); 194 - adev->serial[len] = '\0'; 195 - 227 + /* Go to the Product Serial Number field. */ 228 + addr += 1 + (pia[addr] & 0x3F); 229 + if (addr + 1 >= len) 230 + goto Out; 231 + memcpy(adev->serial, pia + addr + 1, min_t(size_t, 232 + sizeof(adev->serial), 233 + pia[addr] & 0x3F)); 234 + adev->serial[sizeof(adev->serial) - 1] = '\0'; 235 + Out: 236 + kfree(pia); 196 237 return 0; 197 238 }