1bd607166SKent Russell /*
2bd607166SKent Russell  * Copyright 2019 Advanced Micro Devices, Inc.
3bd607166SKent Russell  *
4bd607166SKent Russell  * Permission is hereby granted, free of charge, to any person obtaining a
5bd607166SKent Russell  * copy of this software and associated documentation files (the "Software"),
6bd607166SKent Russell  * to deal in the Software without restriction, including without limitation
7bd607166SKent Russell  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bd607166SKent Russell  * and/or sell copies of the Software, and to permit persons to whom the
9bd607166SKent Russell  * Software is furnished to do so, subject to the following conditions:
10bd607166SKent Russell  *
11bd607166SKent Russell  * The above copyright notice and this permission notice shall be included in
12bd607166SKent Russell  * all copies or substantial portions of the Software.
13bd607166SKent Russell  *
14bd607166SKent Russell  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bd607166SKent Russell  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bd607166SKent Russell  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bd607166SKent Russell  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18bd607166SKent Russell  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19bd607166SKent Russell  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20bd607166SKent Russell  * OTHER DEALINGS IN THE SOFTWARE.
21bd607166SKent Russell  *
22bd607166SKent Russell  */
231ea2b260SKent Russell #include <linux/pci.h>
241ea2b260SKent Russell 
25bd607166SKent Russell #include "amdgpu.h"
26bd607166SKent Russell #include "amdgpu_i2c.h"
27bd607166SKent Russell #include "smu_v11_0_i2c.h"
28bd607166SKent Russell #include "atom.h"
29d43f7ff6SAlex Deucher #include "amdgpu_fru_eeprom.h"
3025e5c09fSAlex Deucher #include "amdgpu_eeprom.h"
31bd607166SKent Russell 
32afbe5d1eSLuben Tuikov #define FRU_EEPROM_MADDR_6      0x60000
33afbe5d1eSLuben Tuikov #define FRU_EEPROM_MADDR_8      0x80000
34*25907304SAsad Kamal #define FRU_EEPROM_MADDR_INV    0xFFFFF
35bd607166SKent Russell 
is_fru_eeprom_supported(struct amdgpu_device * adev,u32 * fru_addr)36afbe5d1eSLuben Tuikov static bool is_fru_eeprom_supported(struct amdgpu_device *adev, u32 *fru_addr)
37fabe01d7SJohn Clements {
38f94582e4SKent Russell 	/* Only server cards have the FRU EEPROM
39f94582e4SKent Russell 	 * TODO: See if we can figure this out dynamically instead of
40f94582e4SKent Russell 	 * having to parse VBIOS versions.
411ea2b260SKent Russell 	 */
42f94582e4SKent Russell 	struct atom_context *atom_ctx = adev->mode_info.atom_context;
43f94582e4SKent Russell 
44901abf36Sshaoyunl 	/* The i2c access is blocked on VF
45901abf36Sshaoyunl 	 * TODO: Need other way to get the info
46a8558fceSLijo Lazar 	 * Also, FRU not valid for APU devices.
47901abf36Sshaoyunl 	 */
48a8558fceSLijo Lazar 	if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU))
49901abf36Sshaoyunl 		return false;
50901abf36Sshaoyunl 
51afbe5d1eSLuben Tuikov 	/* The default I2C EEPROM address of the FRU.
52afbe5d1eSLuben Tuikov 	 */
53afbe5d1eSLuben Tuikov 	if (fru_addr)
54afbe5d1eSLuben Tuikov 		*fru_addr = FRU_EEPROM_MADDR_8;
55afbe5d1eSLuben Tuikov 
563ed89339SLuben Tuikov 	/* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
57f94582e4SKent Russell 	 * we can use just the "DXXX" portion. If there were more models, we
58f94582e4SKent Russell 	 * could convert the 3 characters to a hex integer and use a switch
59f94582e4SKent Russell 	 * for ease/speed/readability. For now, 2 string comparisons are
60f94582e4SKent Russell 	 * reasonable and not too expensive
61f94582e4SKent Russell 	 */
62be2e8acaSYang Wang 	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
63be2e8acaSYang Wang 	case IP_VERSION(11, 0, 2):
64f94582e4SKent Russell 		switch (adev->asic_type) {
65f94582e4SKent Russell 		case CHIP_VEGA20:
66f94582e4SKent Russell 			/* D161 and D163 are the VG20 server SKUs */
676e8ca38eSLijo Lazar 			if (atom_ctx && (strnstr(atom_ctx->vbios_pn, "D161",
68adf64e21SMario Limonciello 						 sizeof(atom_ctx->vbios_pn)) ||
69adf64e21SMario Limonciello 					 strnstr(atom_ctx->vbios_pn, "D163",
706e8ca38eSLijo Lazar 						 sizeof(atom_ctx->vbios_pn)))) {
7128afcb0aSLuben Tuikov 				if (fru_addr)
72afbe5d1eSLuben Tuikov 					*fru_addr = FRU_EEPROM_MADDR_6;
731ea2b260SKent Russell 				return true;
74afbe5d1eSLuben Tuikov 			} else {
75fabe01d7SJohn Clements 				return false;
76afbe5d1eSLuben Tuikov 			}
77be2e8acaSYang Wang 		case CHIP_ARCTURUS:
78be2e8acaSYang Wang 		default:
79be2e8acaSYang Wang 			return false;
80be2e8acaSYang Wang 		}
81be2e8acaSYang Wang 	case IP_VERSION(11, 0, 7):
826e8ca38eSLijo Lazar 		if (atom_ctx && strnstr(atom_ctx->vbios_pn, "D603",
83adf64e21SMario Limonciello 					sizeof(atom_ctx->vbios_pn))) {
84adf64e21SMario Limonciello 			if (strnstr(atom_ctx->vbios_pn, "D603GLXE",
85adf64e21SMario Limonciello 				    sizeof(atom_ctx->vbios_pn))) {
863ed89339SLuben Tuikov 				return false;
87ce83aa7bSSrinivasan Shanmugam 			}
88ce83aa7bSSrinivasan Shanmugam 
8928afcb0aSLuben Tuikov 			if (fru_addr)
90afbe5d1eSLuben Tuikov 				*fru_addr = FRU_EEPROM_MADDR_6;
91c8fea927SGuchun Chen 			return true;
92ce83aa7bSSrinivasan Shanmugam 
93c8fea927SGuchun Chen 		} else {
94c8fea927SGuchun Chen 			return false;
95c8fea927SGuchun Chen 		}
96be2e8acaSYang Wang 	case IP_VERSION(13, 0, 2):
97be2e8acaSYang Wang 		/* All Aldebaran SKUs have an FRU */
986e8ca38eSLijo Lazar 		if (atom_ctx && !strnstr(atom_ctx->vbios_pn, "D673",
99be2e8acaSYang Wang 					 sizeof(atom_ctx->vbios_pn)))
100be2e8acaSYang Wang 			if (fru_addr)
101be2e8acaSYang Wang 				*fru_addr = FRU_EEPROM_MADDR_6;
102be2e8acaSYang Wang 		return true;
103be2e8acaSYang Wang 	case IP_VERSION(13, 0, 6):
104a6bcffa5SHawking Zhang 	case IP_VERSION(13, 0, 14):
105be2e8acaSYang Wang 			if (fru_addr)
106be2e8acaSYang Wang 				*fru_addr = FRU_EEPROM_MADDR_8;
107be2e8acaSYang Wang 			return true;
108*25907304SAsad Kamal 	case IP_VERSION(13, 0, 12):
109*25907304SAsad Kamal 			if (fru_addr)
110*25907304SAsad Kamal 				*fru_addr = FRU_EEPROM_MADDR_INV;
111*25907304SAsad Kamal 			return true;
112f94582e4SKent Russell 	default:
113f94582e4SKent Russell 		return false;
114f94582e4SKent Russell 	}
115fabe01d7SJohn Clements }
116fabe01d7SJohn Clements 
amdgpu_fru_get_product_info(struct amdgpu_device * adev)117bd607166SKent Russell int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
118bd607166SKent Russell {
1198a2b5139SLijo Lazar 	struct amdgpu_fru_info *fru_info;
1200dbf2c56SLuben Tuikov 	unsigned char buf[8], *pia;
1210dbf2c56SLuben Tuikov 	u32 addr, fru_addr;
122ccdfbfecSLuben Tuikov 	int size, len;
1230dbf2c56SLuben Tuikov 	u8 csum;
124bd607166SKent Russell 
125afbe5d1eSLuben Tuikov 	if (!is_fru_eeprom_supported(adev, &fru_addr))
126fabe01d7SJohn Clements 		return 0;
127fabe01d7SJohn Clements 
128*25907304SAsad Kamal 	/* FRU data avaialble, but no direct EEPROM access */
129*25907304SAsad Kamal 	if (fru_addr == FRU_EEPROM_MADDR_INV)
130*25907304SAsad Kamal 		return 0;
131*25907304SAsad Kamal 
1328a2b5139SLijo Lazar 	if (!adev->fru_info) {
1338a2b5139SLijo Lazar 		adev->fru_info = kzalloc(sizeof(*adev->fru_info), GFP_KERNEL);
1348a2b5139SLijo Lazar 		if (!adev->fru_info)
1358a2b5139SLijo Lazar 			return -ENOMEM;
1368a2b5139SLijo Lazar 	}
1378a2b5139SLijo Lazar 
1388a2b5139SLijo Lazar 	fru_info = adev->fru_info;
1398a2b5139SLijo Lazar 	/* For Arcturus-and-later, default value of serial_number is unique_id
1408a2b5139SLijo Lazar 	 * so convert it to a 16-digit HEX string for convenience and
1418a2b5139SLijo Lazar 	 * backwards-compatibility.
1428a2b5139SLijo Lazar 	 */
1438a2b5139SLijo Lazar 	sprintf(fru_info->serial, "%llx", adev->unique_id);
1448a2b5139SLijo Lazar 
145bd607166SKent Russell 	/* If algo exists, it means that the i2c_adapter's initialized */
1462f60dd50SLuben Tuikov 	if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
147bd607166SKent Russell 		DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
14802b865f8SJiansong Chen 		return -ENODEV;
149bd607166SKent Russell 	}
150bd607166SKent Russell 
1510dbf2c56SLuben Tuikov 	/* Read the IPMI Common header */
1520dbf2c56SLuben Tuikov 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
1530dbf2c56SLuben Tuikov 				 sizeof(buf));
1540dbf2c56SLuben Tuikov 	if (len != 8) {
1550dbf2c56SLuben Tuikov 		DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
1560dbf2c56SLuben Tuikov 		return len < 0 ? len : -EIO;
1570dbf2c56SLuben Tuikov 	}
1580dbf2c56SLuben Tuikov 
1590dbf2c56SLuben Tuikov 	if (buf[0] != 1) {
1600dbf2c56SLuben Tuikov 		DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
1610dbf2c56SLuben Tuikov 		return -EIO;
1620dbf2c56SLuben Tuikov 	}
1630dbf2c56SLuben Tuikov 
1640dbf2c56SLuben Tuikov 	for (csum = 0; len > 0; len--)
1650dbf2c56SLuben Tuikov 		csum += buf[len - 1];
1660dbf2c56SLuben Tuikov 	if (csum) {
1670dbf2c56SLuben Tuikov 		DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
1680dbf2c56SLuben Tuikov 		return -EIO;
1690dbf2c56SLuben Tuikov 	}
1700dbf2c56SLuben Tuikov 
1710dbf2c56SLuben Tuikov 	/* Get the offset to the Product Info Area (PIA). */
1720dbf2c56SLuben Tuikov 	addr = buf[4] * 8;
1730dbf2c56SLuben Tuikov 	if (!addr)
1740dbf2c56SLuben Tuikov 		return 0;
1750dbf2c56SLuben Tuikov 
1760dbf2c56SLuben Tuikov 	/* Get the absolute address to the PIA. */
1770dbf2c56SLuben Tuikov 	addr += fru_addr;
1780dbf2c56SLuben Tuikov 
1790dbf2c56SLuben Tuikov 	/* Read the header of the PIA. */
1800dbf2c56SLuben Tuikov 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
1810dbf2c56SLuben Tuikov 	if (len != 3) {
1820dbf2c56SLuben Tuikov 		DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
1830dbf2c56SLuben Tuikov 		return len < 0 ? len : -EIO;
1840dbf2c56SLuben Tuikov 	}
1850dbf2c56SLuben Tuikov 
1860dbf2c56SLuben Tuikov 	if (buf[0] != 1) {
1870dbf2c56SLuben Tuikov 		DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
1880dbf2c56SLuben Tuikov 		return -EIO;
1890dbf2c56SLuben Tuikov 	}
1900dbf2c56SLuben Tuikov 
1910dbf2c56SLuben Tuikov 	size = buf[1] * 8;
1920dbf2c56SLuben Tuikov 	pia = kzalloc(size, GFP_KERNEL);
1930dbf2c56SLuben Tuikov 	if (!pia)
1940dbf2c56SLuben Tuikov 		return -ENOMEM;
1950dbf2c56SLuben Tuikov 
1960dbf2c56SLuben Tuikov 	/* Read the whole PIA. */
1970dbf2c56SLuben Tuikov 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
1980dbf2c56SLuben Tuikov 	if (len != size) {
1990dbf2c56SLuben Tuikov 		kfree(pia);
2000dbf2c56SLuben Tuikov 		DRM_ERROR("Couldn't read the Product Info Area: %d", len);
2010dbf2c56SLuben Tuikov 		return len < 0 ? len : -EIO;
2020dbf2c56SLuben Tuikov 	}
2030dbf2c56SLuben Tuikov 
2040dbf2c56SLuben Tuikov 	for (csum = 0; size > 0; size--)
2050dbf2c56SLuben Tuikov 		csum += pia[size - 1];
2060dbf2c56SLuben Tuikov 	if (csum) {
2070dbf2c56SLuben Tuikov 		DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
2089ed630c5SLuben Tuikov 		kfree(pia);
2090dbf2c56SLuben Tuikov 		return -EIO;
2100dbf2c56SLuben Tuikov 	}
2110dbf2c56SLuben Tuikov 
2120dbf2c56SLuben Tuikov 	/* Now extract useful information from the PIA.
2130dbf2c56SLuben Tuikov 	 *
214ac6b1f27SLijo Lazar 	 * Read Manufacturer Name field whose length is [3].
215bd607166SKent Russell 	 */
216ac6b1f27SLijo Lazar 	addr = 3;
217ac6b1f27SLijo Lazar 	if (addr + 1 >= len)
218ac6b1f27SLijo Lazar 		goto Out;
219ac6b1f27SLijo Lazar 	memcpy(fru_info->manufacturer_name, pia + addr + 1,
220ac6b1f27SLijo Lazar 	       min_t(size_t, sizeof(fru_info->manufacturer_name),
221ac6b1f27SLijo Lazar 		     pia[addr] & 0x3F));
222ac6b1f27SLijo Lazar 	fru_info->manufacturer_name[sizeof(fru_info->manufacturer_name) - 1] =
223ac6b1f27SLijo Lazar 		'\0';
224ac6b1f27SLijo Lazar 
225ac6b1f27SLijo Lazar 	/* Read Product Name field. */
226ac6b1f27SLijo Lazar 	addr += 1 + (pia[addr] & 0x3F);
2270dbf2c56SLuben Tuikov 	if (addr + 1 >= len)
2280dbf2c56SLuben Tuikov 		goto Out;
2298a2b5139SLijo Lazar 	memcpy(fru_info->product_name, pia + addr + 1,
2308a2b5139SLijo Lazar 	       min_t(size_t, sizeof(fru_info->product_name), pia[addr] & 0x3F));
2318a2b5139SLijo Lazar 	fru_info->product_name[sizeof(fru_info->product_name) - 1] = '\0';
232bd607166SKent Russell 
2330dbf2c56SLuben Tuikov 	/* Go to the Product Part/Model Number field. */
2340dbf2c56SLuben Tuikov 	addr += 1 + (pia[addr] & 0x3F);
2350dbf2c56SLuben Tuikov 	if (addr + 1 >= len)
2360dbf2c56SLuben Tuikov 		goto Out;
2378a2b5139SLijo Lazar 	memcpy(fru_info->product_number, pia + addr + 1,
2388a2b5139SLijo Lazar 	       min_t(size_t, sizeof(fru_info->product_number),
2390dbf2c56SLuben Tuikov 		     pia[addr] & 0x3F));
2408a2b5139SLijo Lazar 	fru_info->product_number[sizeof(fru_info->product_number) - 1] = '\0';
241bd607166SKent Russell 
2420dbf2c56SLuben Tuikov 	/* Go to the Product Version field. */
2430dbf2c56SLuben Tuikov 	addr += 1 + (pia[addr] & 0x3F);
244bd607166SKent Russell 
2450dbf2c56SLuben Tuikov 	/* Go to the Product Serial Number field. */
2460dbf2c56SLuben Tuikov 	addr += 1 + (pia[addr] & 0x3F);
2470dbf2c56SLuben Tuikov 	if (addr + 1 >= len)
2480dbf2c56SLuben Tuikov 		goto Out;
2498a2b5139SLijo Lazar 	memcpy(fru_info->serial, pia + addr + 1,
2508a2b5139SLijo Lazar 	       min_t(size_t, sizeof(fru_info->serial), pia[addr] & 0x3F));
2518a2b5139SLijo Lazar 	fru_info->serial[sizeof(fru_info->serial) - 1] = '\0';
252ac6b1f27SLijo Lazar 
253ac6b1f27SLijo Lazar 	/* Asset Tag field */
254ac6b1f27SLijo Lazar 	addr += 1 + (pia[addr] & 0x3F);
255ac6b1f27SLijo Lazar 
256ac6b1f27SLijo Lazar 	/* FRU File Id field. This could be 'null'. */
257ac6b1f27SLijo Lazar 	addr += 1 + (pia[addr] & 0x3F);
258ac6b1f27SLijo Lazar 	if ((addr + 1 >= len) || !(pia[addr] & 0x3F))
259ac6b1f27SLijo Lazar 		goto Out;
260ac6b1f27SLijo Lazar 	memcpy(fru_info->fru_id, pia + addr + 1,
261ac6b1f27SLijo Lazar 	       min_t(size_t, sizeof(fru_info->fru_id), pia[addr] & 0x3F));
262ac6b1f27SLijo Lazar 	fru_info->fru_id[sizeof(fru_info->fru_id) - 1] = '\0';
263ac6b1f27SLijo Lazar 
2640dbf2c56SLuben Tuikov Out:
2650dbf2c56SLuben Tuikov 	kfree(pia);
266bd607166SKent Russell 	return 0;
267bd607166SKent Russell }
2687957ec80SLijo Lazar 
2697957ec80SLijo Lazar /**
2707957ec80SLijo Lazar  * DOC: product_name
2717957ec80SLijo Lazar  *
2727957ec80SLijo Lazar  * The amdgpu driver provides a sysfs API for reporting the product name
2737957ec80SLijo Lazar  * for the device
2747957ec80SLijo Lazar  * The file product_name is used for this and returns the product name
2757957ec80SLijo Lazar  * as returned from the FRU.
2767957ec80SLijo Lazar  * NOTE: This is only available for certain server cards
2777957ec80SLijo Lazar  */
2787957ec80SLijo Lazar 
amdgpu_fru_product_name_show(struct device * dev,struct device_attribute * attr,char * buf)2797957ec80SLijo Lazar static ssize_t amdgpu_fru_product_name_show(struct device *dev,
2807957ec80SLijo Lazar 					    struct device_attribute *attr,
2817957ec80SLijo Lazar 					    char *buf)
2827957ec80SLijo Lazar {
2837957ec80SLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
2847957ec80SLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
2857957ec80SLijo Lazar 
2868a2b5139SLijo Lazar 	return sysfs_emit(buf, "%s\n", adev->fru_info->product_name);
2877957ec80SLijo Lazar }
2887957ec80SLijo Lazar 
2897957ec80SLijo Lazar static DEVICE_ATTR(product_name, 0444, amdgpu_fru_product_name_show, NULL);
2907957ec80SLijo Lazar 
2917957ec80SLijo Lazar /**
2927957ec80SLijo Lazar  * DOC: product_number
2937957ec80SLijo Lazar  *
2947957ec80SLijo Lazar  * The amdgpu driver provides a sysfs API for reporting the part number
2957957ec80SLijo Lazar  * for the device
2967957ec80SLijo Lazar  * The file product_number is used for this and returns the part number
2977957ec80SLijo Lazar  * as returned from the FRU.
2987957ec80SLijo Lazar  * NOTE: This is only available for certain server cards
2997957ec80SLijo Lazar  */
3007957ec80SLijo Lazar 
amdgpu_fru_product_number_show(struct device * dev,struct device_attribute * attr,char * buf)3017957ec80SLijo Lazar static ssize_t amdgpu_fru_product_number_show(struct device *dev,
3027957ec80SLijo Lazar 					      struct device_attribute *attr,
3037957ec80SLijo Lazar 					      char *buf)
3047957ec80SLijo Lazar {
3057957ec80SLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
3067957ec80SLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
3077957ec80SLijo Lazar 
3088a2b5139SLijo Lazar 	return sysfs_emit(buf, "%s\n", adev->fru_info->product_number);
3097957ec80SLijo Lazar }
3107957ec80SLijo Lazar 
3117957ec80SLijo Lazar static DEVICE_ATTR(product_number, 0444, amdgpu_fru_product_number_show, NULL);
3127957ec80SLijo Lazar 
3137957ec80SLijo Lazar /**
3147957ec80SLijo Lazar  * DOC: serial_number
3157957ec80SLijo Lazar  *
3167957ec80SLijo Lazar  * The amdgpu driver provides a sysfs API for reporting the serial number
3177957ec80SLijo Lazar  * for the device
3187957ec80SLijo Lazar  * The file serial_number is used for this and returns the serial number
3197957ec80SLijo Lazar  * as returned from the FRU.
3207957ec80SLijo Lazar  * NOTE: This is only available for certain server cards
3217957ec80SLijo Lazar  */
3227957ec80SLijo Lazar 
amdgpu_fru_serial_number_show(struct device * dev,struct device_attribute * attr,char * buf)3237957ec80SLijo Lazar static ssize_t amdgpu_fru_serial_number_show(struct device *dev,
3247957ec80SLijo Lazar 					     struct device_attribute *attr,
3257957ec80SLijo Lazar 					     char *buf)
3267957ec80SLijo Lazar {
3277957ec80SLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
3287957ec80SLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
3297957ec80SLijo Lazar 
3308a2b5139SLijo Lazar 	return sysfs_emit(buf, "%s\n", adev->fru_info->serial);
3317957ec80SLijo Lazar }
3327957ec80SLijo Lazar 
3337957ec80SLijo Lazar static DEVICE_ATTR(serial_number, 0444, amdgpu_fru_serial_number_show, NULL);
3347957ec80SLijo Lazar 
335b3e73b5aSLijo Lazar /**
336b3e73b5aSLijo Lazar  * DOC: fru_id
337b3e73b5aSLijo Lazar  *
338b3e73b5aSLijo Lazar  * The amdgpu driver provides a sysfs API for reporting FRU File Id
339b3e73b5aSLijo Lazar  * for the device.
340b3e73b5aSLijo Lazar  * The file fru_id is used for this and returns the File Id value
341b3e73b5aSLijo Lazar  * as returned from the FRU.
342b3e73b5aSLijo Lazar  * NOTE: This is only available for certain server cards
343b3e73b5aSLijo Lazar  */
344b3e73b5aSLijo Lazar 
amdgpu_fru_id_show(struct device * dev,struct device_attribute * attr,char * buf)345ac6b1f27SLijo Lazar static ssize_t amdgpu_fru_id_show(struct device *dev,
346ac6b1f27SLijo Lazar 				  struct device_attribute *attr, char *buf)
347ac6b1f27SLijo Lazar {
348ac6b1f27SLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
349ac6b1f27SLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
350ac6b1f27SLijo Lazar 
351ac6b1f27SLijo Lazar 	return sysfs_emit(buf, "%s\n", adev->fru_info->fru_id);
352ac6b1f27SLijo Lazar }
353ac6b1f27SLijo Lazar 
354ac6b1f27SLijo Lazar static DEVICE_ATTR(fru_id, 0444, amdgpu_fru_id_show, NULL);
355ac6b1f27SLijo Lazar 
356b3e73b5aSLijo Lazar /**
357b3e73b5aSLijo Lazar  * DOC: manufacturer
358b3e73b5aSLijo Lazar  *
359b3e73b5aSLijo Lazar  * The amdgpu driver provides a sysfs API for reporting manufacturer name from
360b3e73b5aSLijo Lazar  * FRU information.
361b3e73b5aSLijo Lazar  * The file manufacturer returns the value as returned from the FRU.
362b3e73b5aSLijo Lazar  * NOTE: This is only available for certain server cards
363b3e73b5aSLijo Lazar  */
364b3e73b5aSLijo Lazar 
amdgpu_fru_manufacturer_name_show(struct device * dev,struct device_attribute * attr,char * buf)365ac6b1f27SLijo Lazar static ssize_t amdgpu_fru_manufacturer_name_show(struct device *dev,
366ac6b1f27SLijo Lazar 						 struct device_attribute *attr,
367ac6b1f27SLijo Lazar 						 char *buf)
368ac6b1f27SLijo Lazar {
369ac6b1f27SLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
370ac6b1f27SLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
371ac6b1f27SLijo Lazar 
372ac6b1f27SLijo Lazar 	return sysfs_emit(buf, "%s\n", adev->fru_info->manufacturer_name);
373ac6b1f27SLijo Lazar }
374ac6b1f27SLijo Lazar 
375ac6b1f27SLijo Lazar static DEVICE_ATTR(manufacturer, 0444, amdgpu_fru_manufacturer_name_show, NULL);
376ac6b1f27SLijo Lazar 
3777957ec80SLijo Lazar static const struct attribute *amdgpu_fru_attributes[] = {
3787957ec80SLijo Lazar 	&dev_attr_product_name.attr,
3797957ec80SLijo Lazar 	&dev_attr_product_number.attr,
3807957ec80SLijo Lazar 	&dev_attr_serial_number.attr,
381ac6b1f27SLijo Lazar 	&dev_attr_fru_id.attr,
382ac6b1f27SLijo Lazar 	&dev_attr_manufacturer.attr,
3837957ec80SLijo Lazar 	NULL
3847957ec80SLijo Lazar };
3857957ec80SLijo Lazar 
amdgpu_fru_sysfs_init(struct amdgpu_device * adev)3867957ec80SLijo Lazar int amdgpu_fru_sysfs_init(struct amdgpu_device *adev)
3877957ec80SLijo Lazar {
3888a2b5139SLijo Lazar 	if (!is_fru_eeprom_supported(adev, NULL) || !adev->fru_info)
3897957ec80SLijo Lazar 		return 0;
3907957ec80SLijo Lazar 
3917957ec80SLijo Lazar 	return sysfs_create_files(&adev->dev->kobj, amdgpu_fru_attributes);
3927957ec80SLijo Lazar }
3937957ec80SLijo Lazar 
amdgpu_fru_sysfs_fini(struct amdgpu_device * adev)3947957ec80SLijo Lazar void amdgpu_fru_sysfs_fini(struct amdgpu_device *adev)
3957957ec80SLijo Lazar {
396edd628adSLijo Lazar 	if (!adev->fru_info)
3977957ec80SLijo Lazar 		return;
3987957ec80SLijo Lazar 
3997957ec80SLijo Lazar 	sysfs_remove_files(&adev->dev->kobj, amdgpu_fru_attributes);
4007957ec80SLijo Lazar }
401