GNU Linux-libre 6.8.9-gnu
[releases.git] / drivers / net / ethernet / wangxun / txgbe / txgbe_hw.c
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  **/
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  **/
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  **/
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  **/
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 (i != wx->eeprom.sw_region_offset + TXGBE_EEPROM_CHECKSUM)
104                         *checksum += local_buffer[i];
105
106         if (eeprom_ptrs)
107                 kvfree(eeprom_ptrs);
108
109         *checksum = TXGBE_EEPROM_SUM - *checksum;
110
111         return 0;
112 }
113
114 /**
115  *  txgbe_validate_eeprom_checksum - Validate EEPROM checksum
116  *  @wx: pointer to hardware structure
117  *  @checksum_val: calculated checksum
118  *
119  *  Performs checksum calculation and validates the EEPROM checksum.  If the
120  *  caller does not need checksum_val, the value can be NULL.
121  **/
122 int txgbe_validate_eeprom_checksum(struct wx *wx, u16 *checksum_val)
123 {
124         u16 read_checksum = 0;
125         u16 checksum;
126         int status;
127
128         /* Read the first word from the EEPROM. If this times out or fails, do
129          * not continue or we could be in for a very long wait while every
130          * EEPROM read fails
131          */
132         status = wx_read_ee_hostif(wx, 0, &checksum);
133         if (status) {
134                 wx_err(wx, "EEPROM read failed\n");
135                 return status;
136         }
137
138         checksum = 0;
139         status = txgbe_calc_eeprom_checksum(wx, &checksum);
140         if (status != 0)
141                 return status;
142
143         status = wx_read_ee_hostif(wx, wx->eeprom.sw_region_offset +
144                                    TXGBE_EEPROM_CHECKSUM, &read_checksum);
145         if (status != 0)
146                 return status;
147
148         /* Verify read checksum from EEPROM is the same as
149          * calculated checksum
150          */
151         if (read_checksum != checksum) {
152                 status = -EIO;
153                 wx_err(wx, "Invalid EEPROM checksum\n");
154         }
155
156         /* If the user cares, return the calculated checksum */
157         if (checksum_val)
158                 *checksum_val = checksum;
159
160         return status;
161 }
162
163 static void txgbe_reset_misc(struct wx *wx)
164 {
165         wx_reset_misc(wx);
166         txgbe_init_thermal_sensor_thresh(wx);
167 }
168
169 /**
170  *  txgbe_reset_hw - Perform hardware reset
171  *  @wx: pointer to wx structure
172  *
173  *  Resets the hardware by resetting the transmit and receive units, masks
174  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
175  *  reset.
176  **/
177 int txgbe_reset_hw(struct wx *wx)
178 {
179         int status;
180
181         /* Call adapter stop to disable tx/rx and clear interrupts */
182         status = wx_stop_adapter(wx);
183         if (status != 0)
184                 return status;
185
186         if (wx->media_type != sp_media_copper) {
187                 u32 val;
188
189                 val = WX_MIS_RST_LAN_RST(wx->bus.func);
190                 wr32(wx, WX_MIS_RST, val | rd32(wx, WX_MIS_RST));
191                 WX_WRITE_FLUSH(wx);
192                 usleep_range(10, 100);
193         }
194
195         status = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_LAN_SW_RST(wx->bus.func));
196         if (status != 0)
197                 return status;
198
199         txgbe_reset_misc(wx);
200
201         wx_clear_hw_cntrs(wx);
202
203         /* Store the permanent mac address */
204         wx_get_mac_addr(wx, wx->mac.perm_addr);
205
206         /* Store MAC address from RAR0, clear receive address registers, and
207          * clear the multicast table.  Also reset num_rar_entries to 128,
208          * since we modify this value when programming the SAN MAC address.
209          */
210         wx->mac.num_rar_entries = TXGBE_SP_RAR_ENTRIES;
211         wx_init_rx_addrs(wx);
212
213         pci_set_master(wx->pdev);
214
215         return 0;
216 }