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_MAX_TX_QUEUES 128
12 #define TXGBE_RAPTOR_MAX_RX_QUEUES 128
13 #define TXGBE_RAPTOR_RAR_ENTRIES 128
16 * txgbe_init_hw - Generic hardware initialization
17 * @hw: pointer to hardware structure
19 * Initialize the hardware by resetting the hardware, filling the bus info
20 * structure and media type, clears all on chip counters, initializes receive
21 * address registers, multicast table, VLAN filter table, calls routine to set
22 * up link and flow control settings, and leaves transmit and receive units
23 * disabled and uninitialized
25 s32 txgbe_init_hw(struct txgbe_hw *hw)
29 DEBUGFUNC("txgbe_init_hw");
31 /* Reset the hardware */
32 status = hw->mac.reset_hw(hw);
33 if (status == 0 || status == TXGBE_ERR_SFP_NOT_PRESENT) {
35 status = hw->mac.start_hw(hw);
39 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
46 * txgbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
47 * @hw: pointer to the HW structure
49 * Determines the LAN function id by reading memory-mapped registers and swaps
50 * the port value if requested, and set MAC instance for devices.
52 void txgbe_set_lan_id_multi_port(struct txgbe_hw *hw)
54 struct txgbe_bus_info *bus = &hw->bus;
57 DEBUGFUNC("txgbe_set_lan_id_multi_port_pcie");
59 reg = rd32(hw, TXGBE_PORTSTAT);
60 bus->lan_id = TXGBE_PORTSTAT_ID(reg);
62 /* check for single port */
63 reg = rd32(hw, TXGBE_PWR);
64 if (TXGBE_PWR_LANID(reg) == TXGBE_PWR_LANID_SWAP)
67 bus->func = bus->lan_id;
71 * txgbe_validate_mac_addr - Validate MAC address
72 * @mac_addr: pointer to MAC address.
74 * Tests a MAC address to ensure it is a valid Individual Address.
76 s32 txgbe_validate_mac_addr(u8 *mac_addr)
80 DEBUGFUNC("txgbe_validate_mac_addr");
82 /* Make sure it is not a multicast address */
83 if (TXGBE_IS_MULTICAST(mac_addr)) {
84 status = TXGBE_ERR_INVALID_MAC_ADDR;
85 /* Not a broadcast address */
86 } else if (TXGBE_IS_BROADCAST(mac_addr)) {
87 status = TXGBE_ERR_INVALID_MAC_ADDR;
88 /* Reject the zero address */
89 } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
90 mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
91 status = TXGBE_ERR_INVALID_MAC_ADDR;
97 * txgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
98 * @hw: pointer to the hardware structure
100 * The MACs can experience issues if TX work is still pending
101 * when a reset occurs. This function prevents this by flushing the PCIe
102 * buffers on the system.
104 void txgbe_clear_tx_pending(struct txgbe_hw *hw)
109 * If double reset is not requested then all transactions should
110 * already be clear and as such there is no work to do
112 if (!(hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
115 hlreg0 = rd32(hw, TXGBE_PSRCTL);
116 wr32(hw, TXGBE_PSRCTL, hlreg0 | TXGBE_PSRCTL_LBENA);
118 /* Wait for a last completion before clearing buffers */
123 * Before proceeding, make sure that the PCIe block does not have
124 * transactions pending.
126 poll = (800 * 11) / 10;
127 for (i = 0; i < poll; i++)
130 /* Flush all writes and allow 20usec for all transactions to clear */
134 /* restore previous register values */
135 wr32(hw, TXGBE_PSRCTL, hlreg0);
139 * txgbe_init_shared_code - Initialize the shared code
140 * @hw: pointer to hardware structure
142 * This will assign function pointers and assign the MAC type and PHY code.
143 * Does not touch the hardware. This function must be called prior to any
144 * other function in the shared code. The txgbe_hw structure should be
145 * memset to 0 prior to calling this function. The following fields in
146 * hw structure should be filled in prior to calling this function:
147 * hw_addr, back, device_id, vendor_id, subsystem_device_id,
148 * subsystem_vendor_id, and revision_id
150 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
154 DEBUGFUNC("txgbe_init_shared_code");
159 txgbe_set_mac_type(hw);
161 txgbe_init_ops_dummy(hw);
162 switch (hw->mac.type) {
163 case txgbe_mac_raptor:
164 status = txgbe_init_ops_pf(hw);
167 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
170 hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
172 hw->bus.set_lan_id(hw);
178 * txgbe_set_mac_type - Sets MAC type
179 * @hw: pointer to the HW structure
181 * This function sets the mac type of the adapter based on the
182 * vendor ID and device ID stored in the hw structure.
184 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
188 DEBUGFUNC("txgbe_set_mac_type");
190 if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
191 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
192 return TXGBE_ERR_DEVICE_NOT_SUPPORTED;
195 switch (hw->device_id) {
196 case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
197 hw->phy.media_type = txgbe_media_type_backplane;
198 hw->mac.type = txgbe_mac_raptor;
200 case TXGBE_DEV_ID_RAPTOR_XAUI:
201 case TXGBE_DEV_ID_RAPTOR_SGMII:
202 hw->phy.media_type = txgbe_media_type_copper;
203 hw->mac.type = txgbe_mac_raptor;
205 case TXGBE_DEV_ID_RAPTOR_SFP:
206 case TXGBE_DEV_ID_WX1820_SFP:
207 hw->phy.media_type = txgbe_media_type_fiber;
208 hw->mac.type = txgbe_mac_raptor;
210 case TXGBE_DEV_ID_RAPTOR_QSFP:
211 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
212 hw->mac.type = txgbe_mac_raptor;
214 case TXGBE_DEV_ID_RAPTOR_VF:
215 case TXGBE_DEV_ID_RAPTOR_VF_HV:
216 hw->phy.media_type = txgbe_media_type_virtual;
217 hw->mac.type = txgbe_mac_raptor_vf;
220 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
221 DEBUGOUT("Unsupported device id: %x", hw->device_id);
225 DEBUGOUT("found mac: %d media: %d, returns: %d\n",
226 hw->mac.type, hw->phy.media_type, err);
230 void txgbe_init_mac_link_ops(struct txgbe_hw *hw)
232 struct txgbe_mac_info *mac = &hw->mac;
234 DEBUGFUNC("txgbe_init_mac_link_ops");
239 * txgbe_init_phy_raptor - PHY/SFP specific init
240 * @hw: pointer to hardware structure
242 * Initialize any function pointers that were not able to be
243 * set during init_shared_code because the PHY/SFP type was
244 * not known. Perform the SFP init if necessary.
247 s32 txgbe_init_phy_raptor(struct txgbe_hw *hw)
249 struct txgbe_phy_info *phy = &hw->phy;
252 DEBUGFUNC("txgbe_init_phy_raptor");
254 if (hw->device_id == TXGBE_DEV_ID_RAPTOR_QSFP) {
255 /* Store flag indicating I2C bus access control unit. */
256 hw->phy.qsfp_shared_i2c_bus = TRUE;
258 /* Initialize access to QSFP+ I2C bus */
262 /* Identify the PHY or SFP module */
263 err = phy->identify(hw);
264 if (err == TXGBE_ERR_SFP_NOT_SUPPORTED)
265 goto init_phy_ops_out;
267 /* Setup function pointers based on detected SFP module and speeds */
268 txgbe_init_mac_link_ops(hw);
275 * txgbe_init_ops_pf - Inits func ptrs and MAC type
276 * @hw: pointer to hardware structure
278 * Initialize the function pointers and assign the MAC type.
279 * Does not touch the hardware.
281 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
283 struct txgbe_bus_info *bus = &hw->bus;
284 struct txgbe_mac_info *mac = &hw->mac;
285 struct txgbe_phy_info *phy = &hw->phy;
286 struct txgbe_rom_info *rom = &hw->rom;
288 DEBUGFUNC("txgbe_init_ops_pf");
291 bus->set_lan_id = txgbe_set_lan_id_multi_port;
294 phy->identify = txgbe_identify_phy;
295 phy->init = txgbe_init_phy_raptor;
296 phy->read_reg = txgbe_read_phy_reg;
297 phy->write_reg = txgbe_write_phy_reg;
298 phy->read_reg_mdi = txgbe_read_phy_reg_mdi;
299 phy->write_reg_mdi = txgbe_write_phy_reg_mdi;
300 phy->read_i2c_byte = txgbe_read_i2c_byte;
301 phy->write_i2c_byte = txgbe_write_i2c_byte;
302 phy->read_i2c_eeprom = txgbe_read_i2c_eeprom;
303 phy->write_i2c_eeprom = txgbe_write_i2c_eeprom;
304 phy->reset = txgbe_reset_phy;
307 mac->init_hw = txgbe_init_hw;
308 mac->reset_hw = txgbe_reset_hw;
311 rom->init_params = txgbe_init_eeprom_params;
312 rom->read16 = txgbe_ee_read16;
313 rom->readw_buffer = txgbe_ee_readw_buffer;
314 rom->readw_sw = txgbe_ee_readw_sw;
315 rom->read32 = txgbe_ee_read32;
316 rom->write16 = txgbe_ee_write16;
317 rom->writew_buffer = txgbe_ee_writew_buffer;
318 rom->writew_sw = txgbe_ee_writew_sw;
319 rom->write32 = txgbe_ee_write32;
320 rom->validate_checksum = txgbe_validate_eeprom_checksum;
321 rom->update_checksum = txgbe_update_eeprom_checksum;
322 rom->calc_checksum = txgbe_calc_eeprom_checksum;
324 mac->num_rar_entries = TXGBE_RAPTOR_RAR_ENTRIES;
325 mac->max_rx_queues = TXGBE_RAPTOR_MAX_RX_QUEUES;
326 mac->max_tx_queues = TXGBE_RAPTOR_MAX_TX_QUEUES;
332 txgbe_check_flash_load(struct txgbe_hw *hw, u32 check_bit)
337 /* if there's flash existing */
338 if (!(rd32(hw, TXGBE_SPISTAT) & TXGBE_SPISTAT_BPFLASH)) {
339 /* wait hw load flash done */
340 for (i = 0; i < 10; i++) {
341 reg = rd32(hw, TXGBE_ILDRSTAT);
342 if (!(reg & check_bit)) {
349 err = TXGBE_ERR_FLASH_LOADING_FAILED;
355 * txgbe_reset_hw - Perform hardware reset
356 * @hw: pointer to hardware structure
358 * Resets the hardware by resetting the transmit and receive units, masks
359 * and clears all interrupts, perform a PHY reset, and perform a link (MAC)
362 s32 txgbe_reset_hw(struct txgbe_hw *hw)
367 DEBUGFUNC("txgbe_reset_hw");
369 /* Call adapter stop to disable tx/rx and clear interrupts */
370 status = hw->mac.stop_hw(hw);
374 /* flush pending Tx transactions */
375 txgbe_clear_tx_pending(hw);
377 /* Identify PHY and related function pointers */
378 status = hw->phy.init(hw);
379 if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
382 /* Setup SFP module if there is one present. */
383 if (hw->phy.sfp_setup_needed) {
384 status = hw->mac.setup_sfp(hw);
385 hw->phy.sfp_setup_needed = false;
387 if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
391 if (!hw->phy.reset_disable)
394 /* remember AUTOC from before we reset */
395 autoc = hw->mac.autoc_read(hw);
399 * Issue global reset to the MAC. Needs to be SW reset if link is up.
400 * If link reset is used when link is up, it might reset the PHY when
401 * mng is using it. If link is down or the flag to force full link
402 * reset is set, then perform link reset.
404 if (txgbe_mng_present(hw)) {
407 wr32(hw, TXGBE_RST, TXGBE_RST_LAN(hw->bus.lan_id));
412 if (hw->bus.lan_id == 0) {
413 status = txgbe_check_flash_load(hw,
414 TXGBE_ILDRSTAT_SWRST_LAN0);
416 status = txgbe_check_flash_load(hw,
417 TXGBE_ILDRSTAT_SWRST_LAN1);
425 * Double resets are required for recovery from certain error
426 * conditions. Between resets, it is necessary to stall to
427 * allow time for any pending HW events to complete.
429 if (hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
430 hw->mac.flags &= ~TXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
435 * Store the original AUTOC/AUTOC2 values if they have not been
436 * stored off yet. Otherwise restore the stored original
437 * values since the reset operation sets back to defaults.
439 if (!hw->mac.orig_link_settings_stored) {
440 hw->mac.orig_autoc = hw->mac.autoc_read(hw);
441 hw->mac.autoc_write(hw, hw->mac.orig_autoc);
442 hw->mac.orig_link_settings_stored = true;
444 hw->mac.orig_autoc = autoc;
447 /* Store the permanent mac address */
448 hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
451 * Store MAC address from RAR0, clear receive address registers, and
452 * clear the multicast table. Also reset num_rar_entries to 128,
453 * since we modify this value when programming the SAN MAC address.
455 hw->mac.num_rar_entries = 128;
456 hw->mac.init_rx_addrs(hw);
458 /* Store the permanent SAN mac address */
459 hw->mac.get_san_mac_addr(hw, hw->mac.san_addr);
461 /* Add the SAN MAC address to the RAR only if it's a valid address */
462 if (txgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
463 /* Save the SAN MAC RAR index */
464 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
466 hw->mac.set_rar(hw, hw->mac.san_mac_rar_index,
467 hw->mac.san_addr, 0, true);
469 /* clear VMDq pool/queue selection for this RAR */
470 hw->mac.clear_vmdq(hw, hw->mac.san_mac_rar_index,
473 /* Reserve the last RAR for the SAN MAC address */
474 hw->mac.num_rar_entries--;
477 /* Store the alternative WWNN/WWPN prefix */
478 hw->mac.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
479 &hw->mac.wwpn_prefix);