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        0x60000
33 
34 static bool is_fru_eeprom_supported(struct amdgpu_device *adev)
35 {
36 	/* Only server cards have the FRU EEPROM
37 	 * TODO: See if we can figure this out dynamically instead of
38 	 * having to parse VBIOS versions.
39 	 */
40 	struct atom_context *atom_ctx = adev->mode_info.atom_context;
41 
42 	/* The i2c access is blocked on VF
43 	 * TODO: Need other way to get the info
44 	 */
45 	if (amdgpu_sriov_vf(adev))
46 		return false;
47 
48 	/* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
49 	 * we can use just the "DXXX" portion. If there were more models, we
50 	 * could convert the 3 characters to a hex integer and use a switch
51 	 * for ease/speed/readability. For now, 2 string comparisons are
52 	 * reasonable and not too expensive
53 	 */
54 	switch (adev->asic_type) {
55 	case CHIP_VEGA20:
56 #ifdef notyet
57 		/* D161 and D163 are the VG20 server SKUs */
58 		if (strnstr(atom_ctx->vbios_version, "D161",
59 			    sizeof(atom_ctx->vbios_version)) ||
60 		    strnstr(atom_ctx->vbios_version, "D163",
61 			    sizeof(atom_ctx->vbios_version)))
62 			return true;
63 		else
64 #endif
65 			return false;
66 	case CHIP_ALDEBARAN:
67 		/* All Aldebaran SKUs have the FRU */
68 		return true;
69 	case CHIP_SIENNA_CICHLID:
70 #ifdef notyet
71 		if (strnstr(atom_ctx->vbios_version, "D603",
72 		    sizeof(atom_ctx->vbios_version))) {
73 			if (strnstr(atom_ctx->vbios_version, "D603GLXE",
74 			    sizeof(atom_ctx->vbios_version)))
75 				return false;
76 			else
77 				return true;
78 		} else {
79 			return false;
80 		}
81 #else
82 		return false;
83 #endif
84 	default:
85 		return false;
86 	}
87 }
88 
89 static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
90 				  unsigned char *buf, size_t buf_size)
91 {
92 	int ret;
93 	u8 size;
94 
95 	ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr, buf, 1);
96 	if (ret < 1) {
97 		DRM_WARN("FRU: Failed to get size field");
98 		return ret;
99 	}
100 
101 	/* The size returned by the i2c requires subtraction of 0xC0 since the
102 	 * size apparently always reports as 0xC0+actual size.
103 	 */
104 	size = buf[0] & 0x3F;
105 	size = min_t(size_t, size, buf_size);
106 
107 	ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr + 1,
108 				 buf, size);
109 	if (ret < 1) {
110 		DRM_WARN("FRU: Failed to get data field");
111 		return ret;
112 	}
113 
114 	return size;
115 }
116 
117 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
118 {
119 	unsigned char buf[AMDGPU_PRODUCT_NAME_LEN];
120 	u32 addrptr;
121 	int size, len;
122 
123 	if (!is_fru_eeprom_supported(adev))
124 		return 0;
125 
126 	/* If algo exists, it means that the i2c_adapter's initialized */
127 	if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
128 		DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
129 		return -ENODEV;
130 	}
131 
132 	/* There's a lot of repetition here. This is due to the FRU having
133 	 * variable-length fields. To get the information, we have to find the
134 	 * size of each field, and then keep reading along and reading along
135 	 * until we get all of the data that we want. We use addrptr to track
136 	 * the address as we go
137 	 */
138 
139 	/* The first fields are all of size 1-byte, from 0-7 are offsets that
140 	 * contain information that isn't useful to us.
141 	 * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
142 	 * and the language field, so just start from 0xb, manufacturer size
143 	 */
144 	addrptr = FRU_EEPROM_MADDR + 0xb;
145 	size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
146 	if (size < 1) {
147 		DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
148 		return -EINVAL;
149 	}
150 
151 	/* Increment the addrptr by the size of the field, and 1 due to the
152 	 * size field being 1 byte. This pattern continues below.
153 	 */
154 	addrptr += size + 1;
155 	size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
156 	if (size < 1) {
157 		DRM_ERROR("Failed to read FRU product name, ret:%d", size);
158 		return -EINVAL;
159 	}
160 
161 	len = size;
162 	if (len >= AMDGPU_PRODUCT_NAME_LEN) {
163 		DRM_WARN("FRU Product Name is larger than %d characters. This is likely a mistake",
164 				AMDGPU_PRODUCT_NAME_LEN);
165 		len = AMDGPU_PRODUCT_NAME_LEN - 1;
166 	}
167 	memcpy(adev->product_name, buf, len);
168 	adev->product_name[len] = '\0';
169 
170 	addrptr += size + 1;
171 	size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
172 	if (size < 1) {
173 		DRM_ERROR("Failed to read FRU product number, ret:%d", size);
174 		return -EINVAL;
175 	}
176 
177 	len = size;
178 	/* Product number should only be 16 characters. Any more,
179 	 * and something could be wrong. Cap it at 16 to be safe
180 	 */
181 	if (len >= sizeof(adev->product_number)) {
182 		DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
183 		len = sizeof(adev->product_number) - 1;
184 	}
185 	memcpy(adev->product_number, buf, len);
186 	adev->product_number[len] = '\0';
187 
188 	addrptr += size + 1;
189 	size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
190 
191 	if (size < 1) {
192 		DRM_ERROR("Failed to read FRU product version, ret:%d", size);
193 		return -EINVAL;
194 	}
195 
196 	addrptr += size + 1;
197 	size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
198 
199 	if (size < 1) {
200 		DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
201 		return -EINVAL;
202 	}
203 
204 	len = size;
205 	/* Serial number should only be 16 characters. Any more,
206 	 * and something could be wrong. Cap it at 16 to be safe
207 	 */
208 	if (len >= sizeof(adev->serial)) {
209 		DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
210 		len = sizeof(adev->serial) - 1;
211 	}
212 	memcpy(adev->serial, buf, len);
213 	adev->serial[len] = '\0';
214 
215 	return 0;
216 }
217