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