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