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