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 return status; 187 } 188 local_buffer = eeprom_ptrs; 189 } else { 190 if (buffer_size < TXGBE_EEPROM_LAST_WORD) 191 return -EFAULT; 192 local_buffer = buffer; 193 } 194 195 for (i = 0; i < TXGBE_EEPROM_LAST_WORD; i++) 196 if (i != wxhw->eeprom.sw_region_offset + TXGBE_EEPROM_CHECKSUM) 197 *checksum += local_buffer[i]; 198 199 *checksum = TXGBE_EEPROM_SUM - *checksum; 200 if (*checksum < 0) 201 return -EINVAL; 202 203 if (eeprom_ptrs) 204 kvfree(eeprom_ptrs); 205 206 return 0; 207 } 208 209 /** 210 * txgbe_validate_eeprom_checksum - Validate EEPROM checksum 211 * @hw: pointer to hardware structure 212 * @checksum_val: calculated checksum 213 * 214 * Performs checksum calculation and validates the EEPROM checksum. If the 215 * caller does not need checksum_val, the value can be NULL. 216 **/ 217 int txgbe_validate_eeprom_checksum(struct txgbe_hw *hw, u16 *checksum_val) 218 { 219 struct wx_hw *wxhw = &hw->wxhw; 220 u16 read_checksum = 0; 221 u16 checksum; 222 int status; 223 224 /* Read the first word from the EEPROM. If this times out or fails, do 225 * not continue or we could be in for a very long wait while every 226 * EEPROM read fails 227 */ 228 status = wx_read_ee_hostif(wxhw, 0, &checksum); 229 if (status) { 230 wx_err(wxhw, "EEPROM read failed\n"); 231 return status; 232 } 233 234 checksum = 0; 235 status = txgbe_calc_eeprom_checksum(hw, &checksum); 236 if (status != 0) 237 return status; 238 239 status = wx_read_ee_hostif(wxhw, wxhw->eeprom.sw_region_offset + 240 TXGBE_EEPROM_CHECKSUM, &read_checksum); 241 if (status != 0) 242 return status; 243 244 /* Verify read checksum from EEPROM is the same as 245 * calculated checksum 246 */ 247 if (read_checksum != checksum) { 248 status = -EIO; 249 wx_err(wxhw, "Invalid EEPROM checksum\n"); 250 } 251 252 /* If the user cares, return the calculated checksum */ 253 if (checksum_val) 254 *checksum_val = checksum; 255 256 return status; 257 } 258 259 static void txgbe_reset_misc(struct txgbe_hw *hw) 260 { 261 struct wx_hw *wxhw = &hw->wxhw; 262 263 wx_reset_misc(wxhw); 264 txgbe_init_thermal_sensor_thresh(hw); 265 } 266 267 /** 268 * txgbe_reset_hw - Perform hardware reset 269 * @hw: pointer to hardware structure 270 * 271 * Resets the hardware by resetting the transmit and receive units, masks 272 * and clears all interrupts, perform a PHY reset, and perform a link (MAC) 273 * reset. 274 **/ 275 int txgbe_reset_hw(struct txgbe_hw *hw) 276 { 277 struct wx_hw *wxhw = &hw->wxhw; 278 int status; 279 280 /* Call adapter stop to disable tx/rx and clear interrupts */ 281 status = wx_stop_adapter(wxhw); 282 if (status != 0) 283 return status; 284 285 if (!(((wxhw->subsystem_device_id & WX_NCSI_MASK) == WX_NCSI_SUP) || 286 ((wxhw->subsystem_device_id & WX_WOL_MASK) == WX_WOL_SUP))) 287 wx_reset_hostif(wxhw); 288 289 usleep_range(10, 100); 290 291 status = wx_check_flash_load(wxhw, TXGBE_SPI_ILDR_STATUS_LAN_SW_RST(wxhw->bus.func)); 292 if (status != 0) 293 return status; 294 295 txgbe_reset_misc(hw); 296 297 /* Store the permanent mac address */ 298 wx_get_mac_addr(wxhw, wxhw->mac.perm_addr); 299 300 /* Store MAC address from RAR0, clear receive address registers, and 301 * clear the multicast table. Also reset num_rar_entries to 128, 302 * since we modify this value when programming the SAN MAC address. 303 */ 304 wxhw->mac.num_rar_entries = TXGBE_SP_RAR_ENTRIES; 305 wx_init_rx_addrs(wxhw); 306 307 pci_set_master(wxhw->pdev); 308 309 return 0; 310 } 311