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 	u32 buffer_size = 0;
164 	u16 *buffer = NULL;
165 	u16 *local_buffer;
166 	int status;
167 	u16 i;
168 
169 	wx_init_eeprom_params(wx);
170 
171 	if (!buffer) {
172 		eeprom_ptrs = kvmalloc_array(TXGBE_EEPROM_LAST_WORD, sizeof(u16),
173 					     GFP_KERNEL);
174 		if (!eeprom_ptrs)
175 			return -ENOMEM;
176 		/* Read pointer area */
177 		status = wx_read_ee_hostif_buffer(wx, 0,
178 						  TXGBE_EEPROM_LAST_WORD,
179 						  eeprom_ptrs);
180 		if (status != 0) {
181 			wx_err(wx, "Failed to read EEPROM image\n");
182 			kvfree(eeprom_ptrs);
183 			return status;
184 		}
185 		local_buffer = eeprom_ptrs;
186 	} else {
187 		if (buffer_size < TXGBE_EEPROM_LAST_WORD)
188 			return -EFAULT;
189 		local_buffer = buffer;
190 	}
191 
192 	for (i = 0; i < TXGBE_EEPROM_LAST_WORD; i++)
193 		if (i != wx->eeprom.sw_region_offset + TXGBE_EEPROM_CHECKSUM)
194 			*checksum += local_buffer[i];
195 
196 	if (eeprom_ptrs)
197 		kvfree(eeprom_ptrs);
198 
199 	if (*checksum > TXGBE_EEPROM_SUM)
200 		return -EINVAL;
201 
202 	*checksum = TXGBE_EEPROM_SUM - *checksum;
203 
204 	return 0;
205 }
206 
207 /**
208  *  txgbe_validate_eeprom_checksum - Validate EEPROM checksum
209  *  @wx: pointer to hardware structure
210  *  @checksum_val: calculated checksum
211  *
212  *  Performs checksum calculation and validates the EEPROM checksum.  If the
213  *  caller does not need checksum_val, the value can be NULL.
214  **/
215 int txgbe_validate_eeprom_checksum(struct wx *wx, u16 *checksum_val)
216 {
217 	u16 read_checksum = 0;
218 	u16 checksum;
219 	int status;
220 
221 	/* Read the first word from the EEPROM. If this times out or fails, do
222 	 * not continue or we could be in for a very long wait while every
223 	 * EEPROM read fails
224 	 */
225 	status = wx_read_ee_hostif(wx, 0, &checksum);
226 	if (status) {
227 		wx_err(wx, "EEPROM read failed\n");
228 		return status;
229 	}
230 
231 	checksum = 0;
232 	status = txgbe_calc_eeprom_checksum(wx, &checksum);
233 	if (status != 0)
234 		return status;
235 
236 	status = wx_read_ee_hostif(wx, wx->eeprom.sw_region_offset +
237 				   TXGBE_EEPROM_CHECKSUM, &read_checksum);
238 	if (status != 0)
239 		return status;
240 
241 	/* Verify read checksum from EEPROM is the same as
242 	 * calculated checksum
243 	 */
244 	if (read_checksum != checksum) {
245 		status = -EIO;
246 		wx_err(wx, "Invalid EEPROM checksum\n");
247 	}
248 
249 	/* If the user cares, return the calculated checksum */
250 	if (checksum_val)
251 		*checksum_val = checksum;
252 
253 	return status;
254 }
255 
256 static void txgbe_reset_misc(struct wx *wx)
257 {
258 	wx_reset_misc(wx);
259 	txgbe_init_thermal_sensor_thresh(wx);
260 }
261 
262 /**
263  *  txgbe_reset_hw - Perform hardware reset
264  *  @wx: pointer to wx structure
265  *
266  *  Resets the hardware by resetting the transmit and receive units, masks
267  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
268  *  reset.
269  **/
270 int txgbe_reset_hw(struct wx *wx)
271 {
272 	int status;
273 
274 	/* Call adapter stop to disable tx/rx and clear interrupts */
275 	status = wx_stop_adapter(wx);
276 	if (status != 0)
277 		return status;
278 
279 	if (!(((wx->subsystem_device_id & WX_NCSI_MASK) == WX_NCSI_SUP) ||
280 	      ((wx->subsystem_device_id & WX_WOL_MASK) == WX_WOL_SUP)))
281 		wx_reset_hostif(wx);
282 
283 	usleep_range(10, 100);
284 
285 	status = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_LAN_SW_RST(wx->bus.func));
286 	if (status != 0)
287 		return status;
288 
289 	txgbe_reset_misc(wx);
290 
291 	/* Store the permanent mac address */
292 	wx_get_mac_addr(wx, wx->mac.perm_addr);
293 
294 	/* Store MAC address from RAR0, clear receive address registers, and
295 	 * clear the multicast table.  Also reset num_rar_entries to 128,
296 	 * since we modify this value when programming the SAN MAC address.
297 	 */
298 	wx->mac.num_rar_entries = TXGBE_SP_RAR_ENTRIES;
299 	wx_init_rx_addrs(wx);
300 
301 	pci_set_master(wx->pdev);
302 
303 	return 0;
304 }
305