xref: /linux/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c (revision 85502b2214d50ba0ddf2a5fb454e4d28a160d175)
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_calc_eeprom_checksum - Calculates and returns the checksum
75  *  @wx: pointer to hardware structure
76  *  @checksum: pointer to cheksum
77  *
78  *  Returns a negative error code on error
79  **/
txgbe_calc_eeprom_checksum(struct wx * wx,u16 * checksum)80 static int txgbe_calc_eeprom_checksum(struct wx *wx, u16 *checksum)
81 {
82 	u16 *eeprom_ptrs = NULL;
83 	u16 *local_buffer;
84 	int status;
85 	u16 i;
86 
87 	wx_init_eeprom_params(wx);
88 
89 	eeprom_ptrs = kvmalloc_array(TXGBE_EEPROM_LAST_WORD, sizeof(u16),
90 				     GFP_KERNEL);
91 	if (!eeprom_ptrs)
92 		return -ENOMEM;
93 	/* Read pointer area */
94 	status = wx_read_ee_hostif_buffer(wx, 0, TXGBE_EEPROM_LAST_WORD, eeprom_ptrs);
95 	if (status != 0) {
96 		wx_err(wx, "Failed to read EEPROM image\n");
97 		kvfree(eeprom_ptrs);
98 		return status;
99 	}
100 	local_buffer = eeprom_ptrs;
101 
102 	for (i = 0; i < TXGBE_EEPROM_LAST_WORD; i++) {
103 		if (wx->mac.type == wx_mac_aml) {
104 			if (i >= TXGBE_EEPROM_I2C_SRART_PTR &&
105 			    i < TXGBE_EEPROM_I2C_END_PTR)
106 				local_buffer[i] = 0xffff;
107 		}
108 		if (i != wx->eeprom.sw_region_offset + TXGBE_EEPROM_CHECKSUM)
109 			*checksum += local_buffer[i];
110 	}
111 
112 	kvfree(eeprom_ptrs);
113 
114 	*checksum = TXGBE_EEPROM_SUM - *checksum;
115 
116 	return 0;
117 }
118 
119 /**
120  *  txgbe_validate_eeprom_checksum - Validate EEPROM checksum
121  *  @wx: pointer to hardware structure
122  *  @checksum_val: calculated checksum
123  *
124  *  Performs checksum calculation and validates the EEPROM checksum.  If the
125  *  caller does not need checksum_val, the value can be NULL.
126  **/
txgbe_validate_eeprom_checksum(struct wx * wx,u16 * checksum_val)127 int txgbe_validate_eeprom_checksum(struct wx *wx, u16 *checksum_val)
128 {
129 	u16 read_checksum = 0;
130 	u16 checksum;
131 	int status;
132 
133 	/* Read the first word from the EEPROM. If this times out or fails, do
134 	 * not continue or we could be in for a very long wait while every
135 	 * EEPROM read fails
136 	 */
137 	status = wx_read_ee_hostif(wx, 0, &checksum);
138 	if (status) {
139 		wx_err(wx, "EEPROM read failed\n");
140 		return status;
141 	}
142 
143 	checksum = 0;
144 	status = txgbe_calc_eeprom_checksum(wx, &checksum);
145 	if (status != 0)
146 		return status;
147 
148 	status = wx_read_ee_hostif(wx, wx->eeprom.sw_region_offset +
149 				   TXGBE_EEPROM_CHECKSUM, &read_checksum);
150 	if (status != 0)
151 		return status;
152 
153 	/* Verify read checksum from EEPROM is the same as
154 	 * calculated checksum
155 	 */
156 	if (read_checksum != checksum) {
157 		status = -EIO;
158 		wx_err(wx, "Invalid EEPROM checksum\n");
159 	}
160 
161 	/* If the user cares, return the calculated checksum */
162 	if (checksum_val)
163 		*checksum_val = checksum;
164 
165 	return status;
166 }
167 
txgbe_reset_misc(struct wx * wx)168 static void txgbe_reset_misc(struct wx *wx)
169 {
170 	wx_reset_misc(wx);
171 	txgbe_init_thermal_sensor_thresh(wx);
172 }
173 
174 /**
175  *  txgbe_reset_hw - Perform hardware reset
176  *  @wx: pointer to wx structure
177  *
178  *  Resets the hardware by resetting the transmit and receive units, masks
179  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
180  *  reset.
181  **/
txgbe_reset_hw(struct wx * wx)182 int txgbe_reset_hw(struct wx *wx)
183 {
184 	int status;
185 
186 	/* Call adapter stop to disable tx/rx and clear interrupts */
187 	status = wx_stop_adapter(wx);
188 	if (status != 0)
189 		return status;
190 
191 	if (wx->media_type != wx_media_copper) {
192 		u32 val;
193 
194 		val = WX_MIS_RST_LAN_RST(wx->bus.func);
195 		wr32(wx, WX_MIS_RST, val | rd32(wx, WX_MIS_RST));
196 		WX_WRITE_FLUSH(wx);
197 		usleep_range(10, 100);
198 	}
199 
200 	status = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_LAN_SW_RST(wx->bus.func));
201 	if (status != 0)
202 		return status;
203 
204 	txgbe_reset_misc(wx);
205 
206 	if (wx->mac.type != wx_mac_sp) {
207 		wr32(wx, TXGBE_PX_PF_BME, 0x1);
208 		wr32m(wx, TXGBE_RDM_RSC_CTL, TXGBE_RDM_RSC_CTL_FREE_CTL,
209 		      TXGBE_RDM_RSC_CTL_FREE_CTL);
210 	}
211 
212 	wx_clear_hw_cntrs(wx);
213 
214 	/* Store the permanent mac address */
215 	wx_get_mac_addr(wx, wx->mac.perm_addr);
216 
217 	/* Store MAC address from RAR0, clear receive address registers, and
218 	 * clear the multicast table.  Also reset num_rar_entries to 128,
219 	 * since we modify this value when programming the SAN MAC address.
220 	 */
221 	wx->mac.num_rar_entries = TXGBE_RAR_ENTRIES;
222 	wx_init_rx_addrs(wx);
223 
224 	pci_set_master(wx->pdev);
225 
226 	return 0;
227 }
228