Loading...
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
35static bool is_fru_eeprom_supported(struct amdgpu_device *adev, u32 *fru_addr)
36{
37 /* Only server cards have the FRU EEPROM
38 * TODO: See if we can figure this out dynamically instead of
39 * having to parse VBIOS versions.
40 */
41 struct atom_context *atom_ctx = adev->mode_info.atom_context;
42
43 /* The i2c access is blocked on VF
44 * TODO: Need other way to get the info
45 * Also, FRU not valid for APU devices.
46 */
47 if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU))
48 return false;
49
50 /* The default I2C EEPROM address of the FRU.
51 */
52 if (fru_addr)
53 *fru_addr = FRU_EEPROM_MADDR_8;
54
55 /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
56 * we can use just the "DXXX" portion. If there were more models, we
57 * could convert the 3 characters to a hex integer and use a switch
58 * for ease/speed/readability. For now, 2 string comparisons are
59 * reasonable and not too expensive
60 */
61 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
62 case IP_VERSION(11, 0, 2):
63 switch (adev->asic_type) {
64 case CHIP_VEGA20:
65 /* D161 and D163 are the VG20 server SKUs */
66 if (strnstr(atom_ctx->vbios_pn, "D161",
67 sizeof(atom_ctx->vbios_pn)) ||
68 strnstr(atom_ctx->vbios_pn, "D163",
69 sizeof(atom_ctx->vbios_pn))) {
70 if (fru_addr)
71 *fru_addr = FRU_EEPROM_MADDR_6;
72 return true;
73 } else {
74 return false;
75 }
76 case CHIP_ARCTURUS:
77 default:
78 return false;
79 }
80 case IP_VERSION(11, 0, 7):
81 if (strnstr(atom_ctx->vbios_pn, "D603",
82 sizeof(atom_ctx->vbios_pn))) {
83 if (strnstr(atom_ctx->vbios_pn, "D603GLXE",
84 sizeof(atom_ctx->vbios_pn))) {
85 return false;
86 }
87
88 if (fru_addr)
89 *fru_addr = FRU_EEPROM_MADDR_6;
90 return true;
91
92 } else {
93 return false;
94 }
95 case IP_VERSION(13, 0, 2):
96 /* All Aldebaran SKUs have an FRU */
97 if (!strnstr(atom_ctx->vbios_pn, "D673",
98 sizeof(atom_ctx->vbios_pn)))
99 if (fru_addr)
100 *fru_addr = FRU_EEPROM_MADDR_6;
101 return true;
102 case IP_VERSION(13, 0, 6):
103 if (fru_addr)
104 *fru_addr = FRU_EEPROM_MADDR_8;
105 return true;
106 default:
107 return false;
108 }
109}
110
111int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
112{
113 struct amdgpu_fru_info *fru_info;
114 unsigned char buf[8], *pia;
115 u32 addr, fru_addr;
116 int size, len;
117 u8 csum;
118
119 if (!is_fru_eeprom_supported(adev, &fru_addr))
120 return 0;
121
122 if (!adev->fru_info) {
123 adev->fru_info = kzalloc(sizeof(*adev->fru_info), GFP_KERNEL);
124 if (!adev->fru_info)
125 return -ENOMEM;
126 }
127
128 fru_info = adev->fru_info;
129 /* For Arcturus-and-later, default value of serial_number is unique_id
130 * so convert it to a 16-digit HEX string for convenience and
131 * backwards-compatibility.
132 */
133 sprintf(fru_info->serial, "%llx", adev->unique_id);
134
135 /* If algo exists, it means that the i2c_adapter's initialized */
136 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
137 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
138 return -ENODEV;
139 }
140
141 /* Read the IPMI Common header */
142 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
143 sizeof(buf));
144 if (len != 8) {
145 DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
146 return len < 0 ? len : -EIO;
147 }
148
149 if (buf[0] != 1) {
150 DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
151 return -EIO;
152 }
153
154 for (csum = 0; len > 0; len--)
155 csum += buf[len - 1];
156 if (csum) {
157 DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
158 return -EIO;
159 }
160
161 /* Get the offset to the Product Info Area (PIA). */
162 addr = buf[4] * 8;
163 if (!addr)
164 return 0;
165
166 /* Get the absolute address to the PIA. */
167 addr += fru_addr;
168
169 /* Read the header of the PIA. */
170 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
171 if (len != 3) {
172 DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
173 return len < 0 ? len : -EIO;
174 }
175
176 if (buf[0] != 1) {
177 DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
178 return -EIO;
179 }
180
181 size = buf[1] * 8;
182 pia = kzalloc(size, GFP_KERNEL);
183 if (!pia)
184 return -ENOMEM;
185
186 /* Read the whole PIA. */
187 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
188 if (len != size) {
189 kfree(pia);
190 DRM_ERROR("Couldn't read the Product Info Area: %d", len);
191 return len < 0 ? len : -EIO;
192 }
193
194 for (csum = 0; size > 0; size--)
195 csum += pia[size - 1];
196 if (csum) {
197 DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
198 kfree(pia);
199 return -EIO;
200 }
201
202 /* Now extract useful information from the PIA.
203 *
204 * Read Manufacturer Name field whose length is [3].
205 */
206 addr = 3;
207 if (addr + 1 >= len)
208 goto Out;
209 memcpy(fru_info->manufacturer_name, pia + addr + 1,
210 min_t(size_t, sizeof(fru_info->manufacturer_name),
211 pia[addr] & 0x3F));
212 fru_info->manufacturer_name[sizeof(fru_info->manufacturer_name) - 1] =
213 '\0';
214
215 /* Read Product Name field. */
216 addr += 1 + (pia[addr] & 0x3F);
217 if (addr + 1 >= len)
218 goto Out;
219 memcpy(fru_info->product_name, pia + addr + 1,
220 min_t(size_t, sizeof(fru_info->product_name), pia[addr] & 0x3F));
221 fru_info->product_name[sizeof(fru_info->product_name) - 1] = '\0';
222
223 /* Go to the Product Part/Model Number field. */
224 addr += 1 + (pia[addr] & 0x3F);
225 if (addr + 1 >= len)
226 goto Out;
227 memcpy(fru_info->product_number, pia + addr + 1,
228 min_t(size_t, sizeof(fru_info->product_number),
229 pia[addr] & 0x3F));
230 fru_info->product_number[sizeof(fru_info->product_number) - 1] = '\0';
231
232 /* Go to the Product Version field. */
233 addr += 1 + (pia[addr] & 0x3F);
234
235 /* Go to the Product Serial Number field. */
236 addr += 1 + (pia[addr] & 0x3F);
237 if (addr + 1 >= len)
238 goto Out;
239 memcpy(fru_info->serial, pia + addr + 1,
240 min_t(size_t, sizeof(fru_info->serial), pia[addr] & 0x3F));
241 fru_info->serial[sizeof(fru_info->serial) - 1] = '\0';
242
243 /* Asset Tag field */
244 addr += 1 + (pia[addr] & 0x3F);
245
246 /* FRU File Id field. This could be 'null'. */
247 addr += 1 + (pia[addr] & 0x3F);
248 if ((addr + 1 >= len) || !(pia[addr] & 0x3F))
249 goto Out;
250 memcpy(fru_info->fru_id, pia + addr + 1,
251 min_t(size_t, sizeof(fru_info->fru_id), pia[addr] & 0x3F));
252 fru_info->fru_id[sizeof(fru_info->fru_id) - 1] = '\0';
253
254Out:
255 kfree(pia);
256 return 0;
257}
258
259/**
260 * DOC: product_name
261 *
262 * The amdgpu driver provides a sysfs API for reporting the product name
263 * for the device
264 * The file product_name is used for this and returns the product name
265 * as returned from the FRU.
266 * NOTE: This is only available for certain server cards
267 */
268
269static ssize_t amdgpu_fru_product_name_show(struct device *dev,
270 struct device_attribute *attr,
271 char *buf)
272{
273 struct drm_device *ddev = dev_get_drvdata(dev);
274 struct amdgpu_device *adev = drm_to_adev(ddev);
275
276 return sysfs_emit(buf, "%s\n", adev->fru_info->product_name);
277}
278
279static DEVICE_ATTR(product_name, 0444, amdgpu_fru_product_name_show, NULL);
280
281/**
282 * DOC: product_number
283 *
284 * The amdgpu driver provides a sysfs API for reporting the part number
285 * for the device
286 * The file product_number is used for this and returns the part number
287 * as returned from the FRU.
288 * NOTE: This is only available for certain server cards
289 */
290
291static ssize_t amdgpu_fru_product_number_show(struct device *dev,
292 struct device_attribute *attr,
293 char *buf)
294{
295 struct drm_device *ddev = dev_get_drvdata(dev);
296 struct amdgpu_device *adev = drm_to_adev(ddev);
297
298 return sysfs_emit(buf, "%s\n", adev->fru_info->product_number);
299}
300
301static DEVICE_ATTR(product_number, 0444, amdgpu_fru_product_number_show, NULL);
302
303/**
304 * DOC: serial_number
305 *
306 * The amdgpu driver provides a sysfs API for reporting the serial number
307 * for the device
308 * The file serial_number is used for this and returns the serial number
309 * as returned from the FRU.
310 * NOTE: This is only available for certain server cards
311 */
312
313static ssize_t amdgpu_fru_serial_number_show(struct device *dev,
314 struct device_attribute *attr,
315 char *buf)
316{
317 struct drm_device *ddev = dev_get_drvdata(dev);
318 struct amdgpu_device *adev = drm_to_adev(ddev);
319
320 return sysfs_emit(buf, "%s\n", adev->fru_info->serial);
321}
322
323static DEVICE_ATTR(serial_number, 0444, amdgpu_fru_serial_number_show, NULL);
324
325/**
326 * DOC: fru_id
327 *
328 * The amdgpu driver provides a sysfs API for reporting FRU File Id
329 * for the device.
330 * The file fru_id is used for this and returns the File Id value
331 * as returned from the FRU.
332 * NOTE: This is only available for certain server cards
333 */
334
335static ssize_t amdgpu_fru_id_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
337{
338 struct drm_device *ddev = dev_get_drvdata(dev);
339 struct amdgpu_device *adev = drm_to_adev(ddev);
340
341 return sysfs_emit(buf, "%s\n", adev->fru_info->fru_id);
342}
343
344static DEVICE_ATTR(fru_id, 0444, amdgpu_fru_id_show, NULL);
345
346/**
347 * DOC: manufacturer
348 *
349 * The amdgpu driver provides a sysfs API for reporting manufacturer name from
350 * FRU information.
351 * The file manufacturer returns the value as returned from the FRU.
352 * NOTE: This is only available for certain server cards
353 */
354
355static ssize_t amdgpu_fru_manufacturer_name_show(struct device *dev,
356 struct device_attribute *attr,
357 char *buf)
358{
359 struct drm_device *ddev = dev_get_drvdata(dev);
360 struct amdgpu_device *adev = drm_to_adev(ddev);
361
362 return sysfs_emit(buf, "%s\n", adev->fru_info->manufacturer_name);
363}
364
365static DEVICE_ATTR(manufacturer, 0444, amdgpu_fru_manufacturer_name_show, NULL);
366
367static const struct attribute *amdgpu_fru_attributes[] = {
368 &dev_attr_product_name.attr,
369 &dev_attr_product_number.attr,
370 &dev_attr_serial_number.attr,
371 &dev_attr_fru_id.attr,
372 &dev_attr_manufacturer.attr,
373 NULL
374};
375
376int amdgpu_fru_sysfs_init(struct amdgpu_device *adev)
377{
378 if (!is_fru_eeprom_supported(adev, NULL) || !adev->fru_info)
379 return 0;
380
381 return sysfs_create_files(&adev->dev->kobj, amdgpu_fru_attributes);
382}
383
384void amdgpu_fru_sysfs_fini(struct amdgpu_device *adev)
385{
386 if (!is_fru_eeprom_supported(adev, NULL) || !adev->fru_info)
387 return;
388
389 sysfs_remove_files(&adev->dev->kobj, amdgpu_fru_attributes);
390}
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
31#define I2C_PRODUCT_INFO_ADDR 0xAC
32#define I2C_PRODUCT_INFO_ADDR_SIZE 0x2
33#define I2C_PRODUCT_INFO_OFFSET 0xC0
34
35static bool is_fru_eeprom_supported(struct amdgpu_device *adev)
36{
37 /* Only server cards have the FRU EEPROM
38 * TODO: See if we can figure this out dynamically instead of
39 * having to parse VBIOS versions.
40 */
41 struct atom_context *atom_ctx = adev->mode_info.atom_context;
42
43 /* VBIOS is of the format ###-DXXXYY-##. For SKU identification,
44 * we can use just the "DXXX" portion. If there were more models, we
45 * could convert the 3 characters to a hex integer and use a switch
46 * for ease/speed/readability. For now, 2 string comparisons are
47 * reasonable and not too expensive
48 */
49 switch (adev->asic_type) {
50 case CHIP_VEGA20:
51 /* D161 and D163 are the VG20 server SKUs */
52 if (strnstr(atom_ctx->vbios_version, "D161",
53 sizeof(atom_ctx->vbios_version)) ||
54 strnstr(atom_ctx->vbios_version, "D163",
55 sizeof(atom_ctx->vbios_version)))
56 return true;
57 else
58 return false;
59 default:
60 return false;
61 }
62}
63
64static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
65 unsigned char *buff)
66{
67 int ret, size;
68 struct i2c_msg msg = {
69 .addr = I2C_PRODUCT_INFO_ADDR,
70 .flags = I2C_M_RD,
71 .buf = buff,
72 };
73 buff[0] = 0;
74 buff[1] = addrptr;
75 msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + 1;
76 ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
77
78 if (ret < 1) {
79 DRM_WARN("FRU: Failed to get size field");
80 return ret;
81 }
82
83 /* The size returned by the i2c requires subtraction of 0xC0 since the
84 * size apparently always reports as 0xC0+actual size.
85 */
86 size = buff[2] - I2C_PRODUCT_INFO_OFFSET;
87 /* Add 1 since address field was 1 byte */
88 buff[1] = addrptr + 1;
89
90 msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + size;
91 ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
92
93 if (ret < 1) {
94 DRM_WARN("FRU: Failed to get data field");
95 return ret;
96 }
97
98 return size;
99}
100
101int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
102{
103 unsigned char buff[34];
104 int addrptr, size;
105 int len;
106
107 if (!is_fru_eeprom_supported(adev))
108 return 0;
109
110 /* If algo exists, it means that the i2c_adapter's initialized */
111 if (!adev->pm.smu_i2c.algo) {
112 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
113 return -ENODEV;
114 }
115
116 /* There's a lot of repetition here. This is due to the FRU having
117 * variable-length fields. To get the information, we have to find the
118 * size of each field, and then keep reading along and reading along
119 * until we get all of the data that we want. We use addrptr to track
120 * the address as we go
121 */
122
123 /* The first fields are all of size 1-byte, from 0-7 are offsets that
124 * contain information that isn't useful to us.
125 * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
126 * and the language field, so just start from 0xb, manufacturer size
127 */
128 addrptr = 0xb;
129 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
130 if (size < 1) {
131 DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
132 return -EINVAL;
133 }
134
135 /* Increment the addrptr by the size of the field, and 1 due to the
136 * size field being 1 byte. This pattern continues below.
137 */
138 addrptr += size + 1;
139 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
140 if (size < 1) {
141 DRM_ERROR("Failed to read FRU product name, ret:%d", size);
142 return -EINVAL;
143 }
144
145 len = size;
146 /* Product name should only be 32 characters. Any more,
147 * and something could be wrong. Cap it at 32 to be safe
148 */
149 if (len >= sizeof(adev->product_name)) {
150 DRM_WARN("FRU Product Number is larger than 32 characters. This is likely a mistake");
151 len = sizeof(adev->product_name) - 1;
152 }
153 /* Start at 2 due to buff using fields 0 and 1 for the address */
154 memcpy(adev->product_name, &buff[2], len);
155 adev->product_name[len] = '\0';
156
157 addrptr += size + 1;
158 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
159 if (size < 1) {
160 DRM_ERROR("Failed to read FRU product number, ret:%d", size);
161 return -EINVAL;
162 }
163
164 len = size;
165 /* Product number should only be 16 characters. Any more,
166 * and something could be wrong. Cap it at 16 to be safe
167 */
168 if (len >= sizeof(adev->product_number)) {
169 DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
170 len = sizeof(adev->product_number) - 1;
171 }
172 memcpy(adev->product_number, &buff[2], len);
173 adev->product_number[len] = '\0';
174
175 addrptr += size + 1;
176 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
177
178 if (size < 1) {
179 DRM_ERROR("Failed to read FRU product version, ret:%d", size);
180 return -EINVAL;
181 }
182
183 addrptr += size + 1;
184 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
185
186 if (size < 1) {
187 DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
188 return -EINVAL;
189 }
190
191 len = size;
192 /* Serial number should only be 16 characters. Any more,
193 * and something could be wrong. Cap it at 16 to be safe
194 */
195 if (len >= sizeof(adev->serial)) {
196 DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
197 len = sizeof(adev->serial) - 1;
198 }
199 memcpy(adev->serial, &buff[2], len);
200 adev->serial[len] = '\0';
201
202 return 0;
203}