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 #define I2C_PRODUCT_INFO_OFFSET 0xC0
34 
35 static 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 	case CHIP_ALDEBARAN:
60 		/* All Aldebaran SKUs have the FRU */
61 		return true;
62 	default:
63 		return false;
64 	}
65 }
66 
67 static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
68 				  unsigned char *buff)
69 {
70 	int ret, size;
71 
72 	ret = amdgpu_eeprom_read(&adev->pm.smu_i2c, addrptr, buff, 1);
73 	if (ret < 1) {
74 		DRM_WARN("FRU: Failed to get size field");
75 		return ret;
76 	}
77 
78 	/* The size returned by the i2c requires subtraction of 0xC0 since the
79 	 * size apparently always reports as 0xC0+actual size.
80 	 */
81 	size = buff[0] - I2C_PRODUCT_INFO_OFFSET;
82 
83 	ret = amdgpu_eeprom_read(&adev->pm.smu_i2c, addrptr + 1, buff, size);
84 	if (ret < 1) {
85 		DRM_WARN("FRU: Failed to get data field");
86 		return ret;
87 	}
88 
89 	return size;
90 }
91 
92 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
93 {
94 	unsigned char buff[AMDGPU_PRODUCT_NAME_LEN+2];
95 	u32 addrptr;
96 	int size, len;
97 	int offset = 2;
98 
99 	if (!is_fru_eeprom_supported(adev))
100 		return 0;
101 
102 	if (adev->asic_type == CHIP_ALDEBARAN)
103 		offset = 0;
104 
105 	/* If algo exists, it means that the i2c_adapter's initialized */
106 	if (!adev->pm.smu_i2c.algo) {
107 		DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
108 		return -ENODEV;
109 	}
110 
111 	/* There's a lot of repetition here. This is due to the FRU having
112 	 * variable-length fields. To get the information, we have to find the
113 	 * size of each field, and then keep reading along and reading along
114 	 * until we get all of the data that we want. We use addrptr to track
115 	 * the address as we go
116 	 */
117 
118 	/* The first fields are all of size 1-byte, from 0-7 are offsets that
119 	 * contain information that isn't useful to us.
120 	 * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
121 	 * and the language field, so just start from 0xb, manufacturer size
122 	 */
123 	addrptr = FRU_EEPROM_MADDR + 0xb;
124 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
125 	if (size < 1) {
126 		DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
127 		return -EINVAL;
128 	}
129 
130 	/* Increment the addrptr by the size of the field, and 1 due to the
131 	 * size field being 1 byte. This pattern continues below.
132 	 */
133 	addrptr += size + 1;
134 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
135 	if (size < 1) {
136 		DRM_ERROR("Failed to read FRU product name, ret:%d", size);
137 		return -EINVAL;
138 	}
139 
140 	len = size;
141 	if (len >= AMDGPU_PRODUCT_NAME_LEN) {
142 		DRM_WARN("FRU Product Name is larger than %d characters. This is likely a mistake",
143 				AMDGPU_PRODUCT_NAME_LEN);
144 		len = AMDGPU_PRODUCT_NAME_LEN - 1;
145 	}
146 	/* Start at 2 due to buff using fields 0 and 1 for the address */
147 	memcpy(adev->product_name, &buff[offset], len);
148 	adev->product_name[len] = '\0';
149 
150 	addrptr += size + 1;
151 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
152 	if (size < 1) {
153 		DRM_ERROR("Failed to read FRU product number, ret:%d", size);
154 		return -EINVAL;
155 	}
156 
157 	len = size;
158 	/* Product number should only be 16 characters. Any more,
159 	 * and something could be wrong. Cap it at 16 to be safe
160 	 */
161 	if (len >= sizeof(adev->product_number)) {
162 		DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
163 		len = sizeof(adev->product_number) - 1;
164 	}
165 	memcpy(adev->product_number, &buff[offset], len);
166 	adev->product_number[len] = '\0';
167 
168 	addrptr += size + 1;
169 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
170 
171 	if (size < 1) {
172 		DRM_ERROR("Failed to read FRU product version, ret:%d", size);
173 		return -EINVAL;
174 	}
175 
176 	addrptr += size + 1;
177 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
178 
179 	if (size < 1) {
180 		DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
181 		return -EINVAL;
182 	}
183 
184 	len = size;
185 	/* Serial number should only be 16 characters. Any more,
186 	 * and something could be wrong. Cap it at 16 to be safe
187 	 */
188 	if (len >= sizeof(adev->serial)) {
189 		DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
190 		len = sizeof(adev->serial) - 1;
191 	}
192 	memcpy(adev->serial, &buff[offset], len);
193 	adev->serial[len] = '\0';
194 
195 	return 0;
196 }
197