1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2015 - 2022 Beijing WangXun Technology Co., Ltd. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/if_ether.h>
6 #include <linux/string.h>
7 #include <linux/iopoll.h>
8 #include <linux/types.h>
9 #include <linux/pci.h>
10 
11 #include "../libwx/wx_type.h"
12 #include "../libwx/wx_hw.h"
13 #include "txgbe_type.h"
14 #include "txgbe_hw.h"
15 
16 /**
17  *  txgbe_init_thermal_sensor_thresh - Inits thermal sensor thresholds
18  *  @wx: pointer to hardware structure
19  *
20  *  Inits the thermal sensor thresholds according to the NVM map
21  *  and save off the threshold and location values into mac.thermal_sensor_data
22  **/
23 static void txgbe_init_thermal_sensor_thresh(struct wx *wx)
24 {
25 	struct wx_thermal_sensor_data *data = &wx->mac.sensor;
26 
27 	memset(data, 0, sizeof(struct wx_thermal_sensor_data));
28 
29 	/* Only support thermal sensors attached to SP physical port 0 */
30 	if (wx->bus.func)
31 		return;
32 
33 	wr32(wx, TXGBE_TS_CTL, TXGBE_TS_CTL_EVAL_MD);
34 
35 	wr32(wx, WX_TS_INT_EN,
36 	     WX_TS_INT_EN_ALARM_INT_EN | WX_TS_INT_EN_DALARM_INT_EN);
37 	wr32(wx, WX_TS_EN, WX_TS_EN_ENA);
38 
39 	data->alarm_thresh = 100;
40 	wr32(wx, WX_TS_ALARM_THRE, 677);
41 	data->dalarm_thresh = 90;
42 	wr32(wx, WX_TS_DALARM_THRE, 614);
43 }
44 
45 /**
46  *  txgbe_read_pba_string - Reads part number string from EEPROM
47  *  @wx: pointer to hardware structure
48  *  @pba_num: stores the part number string from the EEPROM
49  *  @pba_num_size: part number string buffer length
50  *
51  *  Reads the part number string from the EEPROM.
52  **/
53 int txgbe_read_pba_string(struct wx *wx, u8 *pba_num, u32 pba_num_size)
54 {
55 	u16 pba_ptr, offset, length, data;
56 	int ret_val;
57 
58 	if (!pba_num) {
59 		wx_err(wx, "PBA string buffer was null\n");
60 		return -EINVAL;
61 	}
62 
63 	ret_val = wx_read_ee_hostif(wx,
64 				    wx->eeprom.sw_region_offset + TXGBE_PBANUM0_PTR,
65 				    &data);
66 	if (ret_val != 0) {
67 		wx_err(wx, "NVM Read Error\n");
68 		return ret_val;
69 	}
70 
71 	ret_val = wx_read_ee_hostif(wx,
72 				    wx->eeprom.sw_region_offset + TXGBE_PBANUM1_PTR,
73 				    &pba_ptr);
74 	if (ret_val != 0) {
75 		wx_err(wx, "NVM Read Error\n");
76 		return ret_val;
77 	}
78 
79 	/* if data is not ptr guard the PBA must be in legacy format which
80 	 * means pba_ptr is actually our second data word for the PBA number
81 	 * and we can decode it into an ascii string
82 	 */
83 	if (data != TXGBE_PBANUM_PTR_GUARD) {
84 		wx_err(wx, "NVM PBA number is not stored as string\n");
85 
86 		/* we will need 11 characters to store the PBA */
87 		if (pba_num_size < 11) {
88 			wx_err(wx, "PBA string buffer too small\n");
89 			return -ENOMEM;
90 		}
91 
92 		/* extract hex string from data and pba_ptr */
93 		pba_num[0] = (data >> 12) & 0xF;
94 		pba_num[1] = (data >> 8) & 0xF;
95 		pba_num[2] = (data >> 4) & 0xF;
96 		pba_num[3] = data & 0xF;
97 		pba_num[4] = (pba_ptr >> 12) & 0xF;
98 		pba_num[5] = (pba_ptr >> 8) & 0xF;
99 		pba_num[6] = '-';
100 		pba_num[7] = 0;
101 		pba_num[8] = (pba_ptr >> 4) & 0xF;
102 		pba_num[9] = pba_ptr & 0xF;
103 
104 		/* put a null character on the end of our string */
105 		pba_num[10] = '\0';
106 
107 		/* switch all the data but the '-' to hex char */
108 		for (offset = 0; offset < 10; offset++) {
109 			if (pba_num[offset] < 0xA)
110 				pba_num[offset] += '0';
111 			else if (pba_num[offset] < 0x10)
112 				pba_num[offset] += 'A' - 0xA;
113 		}
114 
115 		return 0;
116 	}
117 
118 	ret_val = wx_read_ee_hostif(wx, pba_ptr, &length);
119 	if (ret_val != 0) {
120 		wx_err(wx, "NVM Read Error\n");
121 		return ret_val;
122 	}
123 
124 	if (length == 0xFFFF || length == 0) {
125 		wx_err(wx, "NVM PBA number section invalid length\n");
126 		return -EINVAL;
127 	}
128 
129 	/* check if pba_num buffer is big enough */
130 	if (pba_num_size  < (((u32)length * 2) - 1)) {
131 		wx_err(wx, "PBA string buffer too small\n");
132 		return -ENOMEM;
133 	}
134 
135 	/* trim pba length from start of string */
136 	pba_ptr++;
137 	length--;
138 
139 	for (offset = 0; offset < length; offset++) {
140 		ret_val = wx_read_ee_hostif(wx, pba_ptr + offset, &data);
141 		if (ret_val != 0) {
142 			wx_err(wx, "NVM Read Error\n");
143 			return ret_val;
144 		}
145 		pba_num[offset * 2] = (u8)(data >> 8);
146 		pba_num[(offset * 2) + 1] = (u8)(data & 0xFF);
147 	}
148 	pba_num[offset * 2] = '\0';
149 
150 	return 0;
151 }
152 
153 /**
154  *  txgbe_calc_eeprom_checksum - Calculates and returns the checksum
155  *  @wx: pointer to hardware structure
156  *  @checksum: pointer to cheksum
157  *
158  *  Returns a negative error code on error
159  **/
160 static int txgbe_calc_eeprom_checksum(struct wx *wx, u16 *checksum)
161 {
162 	u16 *eeprom_ptrs = NULL;
163 	u16 *local_buffer;
164 	int status;
165 	u16 i;
166 
167 	wx_init_eeprom_params(wx);
168 
169 	eeprom_ptrs = kvmalloc_array(TXGBE_EEPROM_LAST_WORD, sizeof(u16),
170 				     GFP_KERNEL);
171 	if (!eeprom_ptrs)
172 		return -ENOMEM;
173 	/* Read pointer area */
174 	status = wx_read_ee_hostif_buffer(wx, 0, TXGBE_EEPROM_LAST_WORD, eeprom_ptrs);
175 	if (status != 0) {
176 		wx_err(wx, "Failed to read EEPROM image\n");
177 		kvfree(eeprom_ptrs);
178 		return status;
179 	}
180 	local_buffer = eeprom_ptrs;
181 
182 	for (i = 0; i < TXGBE_EEPROM_LAST_WORD; i++)
183 		if (i != wx->eeprom.sw_region_offset + TXGBE_EEPROM_CHECKSUM)
184 			*checksum += local_buffer[i];
185 
186 	if (eeprom_ptrs)
187 		kvfree(eeprom_ptrs);
188 
189 	if (*checksum > TXGBE_EEPROM_SUM)
190 		return -EINVAL;
191 
192 	*checksum = TXGBE_EEPROM_SUM - *checksum;
193 
194 	return 0;
195 }
196 
197 /**
198  *  txgbe_validate_eeprom_checksum - Validate EEPROM checksum
199  *  @wx: pointer to hardware structure
200  *  @checksum_val: calculated checksum
201  *
202  *  Performs checksum calculation and validates the EEPROM checksum.  If the
203  *  caller does not need checksum_val, the value can be NULL.
204  **/
205 int txgbe_validate_eeprom_checksum(struct wx *wx, u16 *checksum_val)
206 {
207 	u16 read_checksum = 0;
208 	u16 checksum;
209 	int status;
210 
211 	/* Read the first word from the EEPROM. If this times out or fails, do
212 	 * not continue or we could be in for a very long wait while every
213 	 * EEPROM read fails
214 	 */
215 	status = wx_read_ee_hostif(wx, 0, &checksum);
216 	if (status) {
217 		wx_err(wx, "EEPROM read failed\n");
218 		return status;
219 	}
220 
221 	checksum = 0;
222 	status = txgbe_calc_eeprom_checksum(wx, &checksum);
223 	if (status != 0)
224 		return status;
225 
226 	status = wx_read_ee_hostif(wx, wx->eeprom.sw_region_offset +
227 				   TXGBE_EEPROM_CHECKSUM, &read_checksum);
228 	if (status != 0)
229 		return status;
230 
231 	/* Verify read checksum from EEPROM is the same as
232 	 * calculated checksum
233 	 */
234 	if (read_checksum != checksum) {
235 		status = -EIO;
236 		wx_err(wx, "Invalid EEPROM checksum\n");
237 	}
238 
239 	/* If the user cares, return the calculated checksum */
240 	if (checksum_val)
241 		*checksum_val = checksum;
242 
243 	return status;
244 }
245 
246 static void txgbe_reset_misc(struct wx *wx)
247 {
248 	wx_reset_misc(wx);
249 	txgbe_init_thermal_sensor_thresh(wx);
250 }
251 
252 /**
253  *  txgbe_reset_hw - Perform hardware reset
254  *  @wx: pointer to wx structure
255  *
256  *  Resets the hardware by resetting the transmit and receive units, masks
257  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
258  *  reset.
259  **/
260 int txgbe_reset_hw(struct wx *wx)
261 {
262 	int status;
263 
264 	/* Call adapter stop to disable tx/rx and clear interrupts */
265 	status = wx_stop_adapter(wx);
266 	if (status != 0)
267 		return status;
268 
269 	if (!(((wx->subsystem_device_id & WX_NCSI_MASK) == WX_NCSI_SUP) ||
270 	      ((wx->subsystem_device_id & WX_WOL_MASK) == WX_WOL_SUP)))
271 		wx_reset_hostif(wx);
272 
273 	usleep_range(10, 100);
274 
275 	status = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_LAN_SW_RST(wx->bus.func));
276 	if (status != 0)
277 		return status;
278 
279 	txgbe_reset_misc(wx);
280 
281 	/* Store the permanent mac address */
282 	wx_get_mac_addr(wx, wx->mac.perm_addr);
283 
284 	/* Store MAC address from RAR0, clear receive address registers, and
285 	 * clear the multicast table.  Also reset num_rar_entries to 128,
286 	 * since we modify this value when programming the SAN MAC address.
287 	 */
288 	wx->mac.num_rar_entries = TXGBE_SP_RAR_ENTRIES;
289 	wx_init_rx_addrs(wx);
290 
291 	pci_set_master(wx->pdev);
292 
293 	return 0;
294 }
295