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