1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020
5 #include "txgbe_type.h"
7 #include "txgbe_eeprom.h"
11 #define TXGBE_RAPTOR_RAR_ENTRIES 128
14 * txgbe_init_hw - Generic hardware initialization
15 * @hw: pointer to hardware structure
17 * Initialize the hardware by resetting the hardware, filling the bus info
18 * structure and media type, clears all on chip counters, initializes receive
19 * address registers, multicast table, VLAN filter table, calls routine to set
20 * up link and flow control settings, and leaves transmit and receive units
21 * disabled and uninitialized
23 s32 txgbe_init_hw(struct txgbe_hw *hw)
27 DEBUGFUNC("txgbe_init_hw");
29 /* Reset the hardware */
30 status = hw->mac.reset_hw(hw);
31 if (status == 0 || status == TXGBE_ERR_SFP_NOT_PRESENT) {
33 status = hw->mac.start_hw(hw);
37 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
44 * txgbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
45 * @hw: pointer to the HW structure
47 * Determines the LAN function id by reading memory-mapped registers and swaps
48 * the port value if requested, and set MAC instance for devices.
50 void txgbe_set_lan_id_multi_port(struct txgbe_hw *hw)
52 struct txgbe_bus_info *bus = &hw->bus;
55 DEBUGFUNC("txgbe_set_lan_id_multi_port_pcie");
57 reg = rd32(hw, TXGBE_PORTSTAT);
58 bus->lan_id = TXGBE_PORTSTAT_ID(reg);
60 /* check for single port */
61 reg = rd32(hw, TXGBE_PWR);
62 if (TXGBE_PWR_LANID(reg) == TXGBE_PWR_LANID_SWAP)
65 bus->func = bus->lan_id;
69 * txgbe_validate_mac_addr - Validate MAC address
70 * @mac_addr: pointer to MAC address.
72 * Tests a MAC address to ensure it is a valid Individual Address.
74 s32 txgbe_validate_mac_addr(u8 *mac_addr)
78 DEBUGFUNC("txgbe_validate_mac_addr");
80 /* Make sure it is not a multicast address */
81 if (TXGBE_IS_MULTICAST(mac_addr)) {
82 status = TXGBE_ERR_INVALID_MAC_ADDR;
83 /* Not a broadcast address */
84 } else if (TXGBE_IS_BROADCAST(mac_addr)) {
85 status = TXGBE_ERR_INVALID_MAC_ADDR;
86 /* Reject the zero address */
87 } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
88 mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
89 status = TXGBE_ERR_INVALID_MAC_ADDR;
95 * txgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
96 * @hw: pointer to the hardware structure
98 * The MACs can experience issues if TX work is still pending
99 * when a reset occurs. This function prevents this by flushing the PCIe
100 * buffers on the system.
102 void txgbe_clear_tx_pending(struct txgbe_hw *hw)
107 * If double reset is not requested then all transactions should
108 * already be clear and as such there is no work to do
110 if (!(hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
113 hlreg0 = rd32(hw, TXGBE_PSRCTL);
114 wr32(hw, TXGBE_PSRCTL, hlreg0 | TXGBE_PSRCTL_LBENA);
116 /* Wait for a last completion before clearing buffers */
121 * Before proceeding, make sure that the PCIe block does not have
122 * transactions pending.
124 poll = (800 * 11) / 10;
125 for (i = 0; i < poll; i++)
128 /* Flush all writes and allow 20usec for all transactions to clear */
132 /* restore previous register values */
133 wr32(hw, TXGBE_PSRCTL, hlreg0);
137 * txgbe_init_shared_code - Initialize the shared code
138 * @hw: pointer to hardware structure
140 * This will assign function pointers and assign the MAC type and PHY code.
141 * Does not touch the hardware. This function must be called prior to any
142 * other function in the shared code. The txgbe_hw structure should be
143 * memset to 0 prior to calling this function. The following fields in
144 * hw structure should be filled in prior to calling this function:
145 * hw_addr, back, device_id, vendor_id, subsystem_device_id,
146 * subsystem_vendor_id, and revision_id
148 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
152 DEBUGFUNC("txgbe_init_shared_code");
157 txgbe_set_mac_type(hw);
159 txgbe_init_ops_dummy(hw);
160 switch (hw->mac.type) {
161 case txgbe_mac_raptor:
162 status = txgbe_init_ops_pf(hw);
165 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
168 hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
170 hw->bus.set_lan_id(hw);
176 * txgbe_set_mac_type - Sets MAC type
177 * @hw: pointer to the HW structure
179 * This function sets the mac type of the adapter based on the
180 * vendor ID and device ID stored in the hw structure.
182 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
186 DEBUGFUNC("txgbe_set_mac_type");
188 if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
189 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
190 return TXGBE_ERR_DEVICE_NOT_SUPPORTED;
193 switch (hw->device_id) {
194 case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
195 hw->phy.media_type = txgbe_media_type_backplane;
196 hw->mac.type = txgbe_mac_raptor;
198 case TXGBE_DEV_ID_RAPTOR_XAUI:
199 case TXGBE_DEV_ID_RAPTOR_SGMII:
200 hw->phy.media_type = txgbe_media_type_copper;
201 hw->mac.type = txgbe_mac_raptor;
203 case TXGBE_DEV_ID_RAPTOR_SFP:
204 case TXGBE_DEV_ID_WX1820_SFP:
205 hw->phy.media_type = txgbe_media_type_fiber;
206 hw->mac.type = txgbe_mac_raptor;
208 case TXGBE_DEV_ID_RAPTOR_QSFP:
209 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
210 hw->mac.type = txgbe_mac_raptor;
212 case TXGBE_DEV_ID_RAPTOR_VF:
213 case TXGBE_DEV_ID_RAPTOR_VF_HV:
214 hw->phy.media_type = txgbe_media_type_virtual;
215 hw->mac.type = txgbe_mac_raptor_vf;
218 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
219 DEBUGOUT("Unsupported device id: %x", hw->device_id);
223 DEBUGOUT("found mac: %d media: %d, returns: %d\n",
224 hw->mac.type, hw->phy.media_type, err);
228 void txgbe_init_mac_link_ops(struct txgbe_hw *hw)
230 struct txgbe_mac_info *mac = &hw->mac;
232 DEBUGFUNC("txgbe_init_mac_link_ops");
237 * txgbe_init_phy_raptor - PHY/SFP specific init
238 * @hw: pointer to hardware structure
240 * Initialize any function pointers that were not able to be
241 * set during init_shared_code because the PHY/SFP type was
242 * not known. Perform the SFP init if necessary.
245 s32 txgbe_init_phy_raptor(struct txgbe_hw *hw)
247 struct txgbe_phy_info *phy = &hw->phy;
250 DEBUGFUNC("txgbe_init_phy_raptor");
252 if (hw->device_id == TXGBE_DEV_ID_RAPTOR_QSFP) {
253 /* Store flag indicating I2C bus access control unit. */
254 hw->phy.qsfp_shared_i2c_bus = TRUE;
256 /* Initialize access to QSFP+ I2C bus */
260 /* Identify the PHY or SFP module */
261 err = phy->identify(hw);
262 if (err == TXGBE_ERR_SFP_NOT_SUPPORTED)
263 goto init_phy_ops_out;
265 /* Setup function pointers based on detected SFP module and speeds */
266 txgbe_init_mac_link_ops(hw);
273 * txgbe_init_ops_pf - Inits func ptrs and MAC type
274 * @hw: pointer to hardware structure
276 * Initialize the function pointers and assign the MAC type.
277 * Does not touch the hardware.
279 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
281 struct txgbe_bus_info *bus = &hw->bus;
282 struct txgbe_mac_info *mac = &hw->mac;
283 struct txgbe_phy_info *phy = &hw->phy;
284 struct txgbe_rom_info *rom = &hw->rom;
286 DEBUGFUNC("txgbe_init_ops_pf");
289 bus->set_lan_id = txgbe_set_lan_id_multi_port;
292 phy->identify = txgbe_identify_phy;
293 phy->init = txgbe_init_phy_raptor;
294 phy->read_reg = txgbe_read_phy_reg;
295 phy->write_reg = txgbe_write_phy_reg;
296 phy->read_reg_mdi = txgbe_read_phy_reg_mdi;
297 phy->write_reg_mdi = txgbe_write_phy_reg_mdi;
298 phy->read_i2c_byte = txgbe_read_i2c_byte;
299 phy->write_i2c_byte = txgbe_write_i2c_byte;
300 phy->read_i2c_eeprom = txgbe_read_i2c_eeprom;
301 phy->write_i2c_eeprom = txgbe_write_i2c_eeprom;
302 phy->reset = txgbe_reset_phy;
305 mac->init_hw = txgbe_init_hw;
306 mac->reset_hw = txgbe_reset_hw;
307 mac->num_rar_entries = TXGBE_RAPTOR_RAR_ENTRIES;
310 rom->init_params = txgbe_init_eeprom_params;
311 rom->read16 = txgbe_ee_read16;
312 rom->readw_buffer = txgbe_ee_readw_buffer;
313 rom->readw_sw = txgbe_ee_readw_sw;
314 rom->read32 = txgbe_ee_read32;
315 rom->write16 = txgbe_ee_write16;
316 rom->writew_buffer = txgbe_ee_writew_buffer;
317 rom->writew_sw = txgbe_ee_writew_sw;
318 rom->write32 = txgbe_ee_write32;
319 rom->validate_checksum = txgbe_validate_eeprom_checksum;
320 rom->update_checksum = txgbe_update_eeprom_checksum;
321 rom->calc_checksum = txgbe_calc_eeprom_checksum;
327 txgbe_check_flash_load(struct txgbe_hw *hw, u32 check_bit)
332 /* if there's flash existing */
333 if (!(rd32(hw, TXGBE_SPISTAT) & TXGBE_SPISTAT_BPFLASH)) {
334 /* wait hw load flash done */
335 for (i = 0; i < 10; i++) {
336 reg = rd32(hw, TXGBE_ILDRSTAT);
337 if (!(reg & check_bit)) {
344 err = TXGBE_ERR_FLASH_LOADING_FAILED;
350 * txgbe_reset_hw - Perform hardware reset
351 * @hw: pointer to hardware structure
353 * Resets the hardware by resetting the transmit and receive units, masks
354 * and clears all interrupts, perform a PHY reset, and perform a link (MAC)
357 s32 txgbe_reset_hw(struct txgbe_hw *hw)
362 DEBUGFUNC("txgbe_reset_hw");
364 /* Call adapter stop to disable tx/rx and clear interrupts */
365 status = hw->mac.stop_hw(hw);
369 /* flush pending Tx transactions */
370 txgbe_clear_tx_pending(hw);
372 /* Identify PHY and related function pointers */
373 status = hw->phy.init(hw);
374 if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
377 /* Setup SFP module if there is one present. */
378 if (hw->phy.sfp_setup_needed) {
379 status = hw->mac.setup_sfp(hw);
380 hw->phy.sfp_setup_needed = false;
382 if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
386 if (!hw->phy.reset_disable)
389 /* remember AUTOC from before we reset */
390 autoc = hw->mac.autoc_read(hw);
394 * Issue global reset to the MAC. Needs to be SW reset if link is up.
395 * If link reset is used when link is up, it might reset the PHY when
396 * mng is using it. If link is down or the flag to force full link
397 * reset is set, then perform link reset.
399 if (txgbe_mng_present(hw)) {
402 wr32(hw, TXGBE_RST, TXGBE_RST_LAN(hw->bus.lan_id));
407 if (hw->bus.lan_id == 0) {
408 status = txgbe_check_flash_load(hw,
409 TXGBE_ILDRSTAT_SWRST_LAN0);
411 status = txgbe_check_flash_load(hw,
412 TXGBE_ILDRSTAT_SWRST_LAN1);
420 * Double resets are required for recovery from certain error
421 * conditions. Between resets, it is necessary to stall to
422 * allow time for any pending HW events to complete.
424 if (hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
425 hw->mac.flags &= ~TXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
430 * Store the original AUTOC/AUTOC2 values if they have not been
431 * stored off yet. Otherwise restore the stored original
432 * values since the reset operation sets back to defaults.
434 if (!hw->mac.orig_link_settings_stored) {
435 hw->mac.orig_autoc = hw->mac.autoc_read(hw);
436 hw->mac.autoc_write(hw, hw->mac.orig_autoc);
437 hw->mac.orig_link_settings_stored = true;
439 hw->mac.orig_autoc = autoc;
442 /* Store the permanent mac address */
443 hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
446 * Store MAC address from RAR0, clear receive address registers, and
447 * clear the multicast table. Also reset num_rar_entries to 128,
448 * since we modify this value when programming the SAN MAC address.
450 hw->mac.num_rar_entries = 128;
451 hw->mac.init_rx_addrs(hw);
453 /* Store the permanent SAN mac address */
454 hw->mac.get_san_mac_addr(hw, hw->mac.san_addr);
456 /* Add the SAN MAC address to the RAR only if it's a valid address */
457 if (txgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
458 /* Save the SAN MAC RAR index */
459 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
461 hw->mac.set_rar(hw, hw->mac.san_mac_rar_index,
462 hw->mac.san_addr, 0, true);
464 /* clear VMDq pool/queue selection for this RAR */
465 hw->mac.clear_vmdq(hw, hw->mac.san_mac_rar_index,
468 /* Reserve the last RAR for the SAN MAC address */
469 hw->mac.num_rar_entries--;
472 /* Store the alternative WWNN/WWPN prefix */
473 hw->mac.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
474 &hw->mac.wwpn_prefix);