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 
35 static 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 	 */
46 	if (amdgpu_sriov_vf(adev))
47 		return false;
48 
49 	/* The default I2C EEPROM address of the FRU.
50 	 */
51 	if (fru_addr)
52 		*fru_addr = FRU_EEPROM_MADDR_8;
53 
54 	/* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
55 	 * we can use just the "DXXX" portion. If there were more models, we
56 	 * could convert the 3 characters to a hex integer and use a switch
57 	 * for ease/speed/readability. For now, 2 string comparisons are
58 	 * reasonable and not too expensive
59 	 */
60 	switch (adev->asic_type) {
61 	case CHIP_VEGA20:
62 		/* D161 and D163 are the VG20 server SKUs */
63 		if (strnstr(atom_ctx->vbios_version, "D161",
64 			    sizeof(atom_ctx->vbios_version)) ||
65 		    strnstr(atom_ctx->vbios_version, "D163",
66 			    sizeof(atom_ctx->vbios_version))) {
67 			*fru_addr = FRU_EEPROM_MADDR_6;
68 			return true;
69 		} else {
70 			return false;
71 		}
72 	case CHIP_ALDEBARAN:
73 		/* All Aldebaran SKUs have an FRU */
74 		if (!strnstr(atom_ctx->vbios_version, "D673",
75 			     sizeof(atom_ctx->vbios_version)))
76 			if (fru_addr)
77 				*fru_addr = FRU_EEPROM_MADDR_6;
78 		return true;
79 	case CHIP_SIENNA_CICHLID:
80 		if (strnstr(atom_ctx->vbios_version, "D603",
81 			    sizeof(atom_ctx->vbios_version))) {
82 			if (strnstr(atom_ctx->vbios_version, "D603GLXE",
83 				    sizeof(atom_ctx->vbios_version))) {
84 				return false;
85 			} else {
86 				*fru_addr = FRU_EEPROM_MADDR_6;
87 				return true;
88 			}
89 		} else {
90 			return false;
91 		}
92 	default:
93 		return false;
94 	}
95 }
96 
97 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
98 {
99 	unsigned char buf[8], *pia;
100 	u32 addr, fru_addr;
101 	int size, len;
102 	u8 csum;
103 
104 	if (!is_fru_eeprom_supported(adev, &fru_addr))
105 		return 0;
106 
107 	/* If algo exists, it means that the i2c_adapter's initialized */
108 	if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
109 		DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
110 		return -ENODEV;
111 	}
112 
113 	/* Read the IPMI Common header */
114 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
115 				 sizeof(buf));
116 	if (len != 8) {
117 		DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
118 		return len < 0 ? len : -EIO;
119 	}
120 
121 	if (buf[0] != 1) {
122 		DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
123 		return -EIO;
124 	}
125 
126 	for (csum = 0; len > 0; len--)
127 		csum += buf[len - 1];
128 	if (csum) {
129 		DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
130 		return -EIO;
131 	}
132 
133 	/* Get the offset to the Product Info Area (PIA). */
134 	addr = buf[4] * 8;
135 	if (!addr)
136 		return 0;
137 
138 	/* Get the absolute address to the PIA. */
139 	addr += fru_addr;
140 
141 	/* Read the header of the PIA. */
142 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
143 	if (len != 3) {
144 		DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
145 		return len < 0 ? len : -EIO;
146 	}
147 
148 	if (buf[0] != 1) {
149 		DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
150 		return -EIO;
151 	}
152 
153 	size = buf[1] * 8;
154 	pia = kzalloc(size, GFP_KERNEL);
155 	if (!pia)
156 		return -ENOMEM;
157 
158 	/* Read the whole PIA. */
159 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
160 	if (len != size) {
161 		kfree(pia);
162 		DRM_ERROR("Couldn't read the Product Info Area: %d", len);
163 		return len < 0 ? len : -EIO;
164 	}
165 
166 	for (csum = 0; size > 0; size--)
167 		csum += pia[size - 1];
168 	if (csum) {
169 		DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
170 		return -EIO;
171 	}
172 
173 	/* Now extract useful information from the PIA.
174 	 *
175 	 * Skip the Manufacturer Name at [3] and go directly to
176 	 * the Product Name field.
177 	 */
178 	addr = 3 + 1 + (pia[3] & 0x3F);
179 	if (addr + 1 >= len)
180 		goto Out;
181 	memcpy(adev->product_name, pia + addr + 1,
182 	       min_t(size_t,
183 		     sizeof(adev->product_name),
184 		     pia[addr] & 0x3F));
185 	adev->product_name[sizeof(adev->product_name) - 1] = '\0';
186 
187 	/* Go to the Product Part/Model Number field. */
188 	addr += 1 + (pia[addr] & 0x3F);
189 	if (addr + 1 >= len)
190 		goto Out;
191 	memcpy(adev->product_number, pia + addr + 1,
192 	       min_t(size_t,
193 		     sizeof(adev->product_number),
194 		     pia[addr] & 0x3F));
195 	adev->product_number[sizeof(adev->product_number) - 1] = '\0';
196 
197 	/* Go to the Product Version field. */
198 	addr += 1 + (pia[addr] & 0x3F);
199 
200 	/* Go to the Product Serial Number field. */
201 	addr += 1 + (pia[addr] & 0x3F);
202 	if (addr + 1 >= len)
203 		goto Out;
204 	memcpy(adev->serial, pia + addr + 1, min_t(size_t,
205 						   sizeof(adev->serial),
206 						   pia[addr] & 0x3F));
207 	adev->serial[sizeof(adev->serial) - 1] = '\0';
208 Out:
209 	kfree(pia);
210 	return 0;
211 }
212