e942c5631d577df725e891dd84fcabcd9c21b406
[dpdk.git] / drivers / net / txgbe / base / txgbe_hw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include "txgbe_type.h"
6 #include "txgbe_eeprom.h"
7 #include "txgbe_hw.h"
8
9 #define TXGBE_RAPTOR_RAR_ENTRIES   128
10
11 /**
12  *  txgbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
13  *  @hw: pointer to the HW structure
14  *
15  *  Determines the LAN function id by reading memory-mapped registers and swaps
16  *  the port value if requested, and set MAC instance for devices.
17  **/
18 void txgbe_set_lan_id_multi_port(struct txgbe_hw *hw)
19 {
20         struct txgbe_bus_info *bus = &hw->bus;
21         u32 reg;
22
23         DEBUGFUNC("txgbe_set_lan_id_multi_port_pcie");
24
25         reg = rd32(hw, TXGBE_PORTSTAT);
26         bus->lan_id = TXGBE_PORTSTAT_ID(reg);
27
28         /* check for single port */
29         reg = rd32(hw, TXGBE_PWR);
30         if (TXGBE_PWR_LANID(reg) == TXGBE_PWR_LANID_SWAP)
31                 bus->func = 0;
32         else
33                 bus->func = bus->lan_id;
34 }
35
36 /**
37  *  txgbe_init_shared_code - Initialize the shared code
38  *  @hw: pointer to hardware structure
39  *
40  *  This will assign function pointers and assign the MAC type and PHY code.
41  *  Does not touch the hardware. This function must be called prior to any
42  *  other function in the shared code. The txgbe_hw structure should be
43  *  memset to 0 prior to calling this function.  The following fields in
44  *  hw structure should be filled in prior to calling this function:
45  *  hw_addr, back, device_id, vendor_id, subsystem_device_id,
46  *  subsystem_vendor_id, and revision_id
47  **/
48 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
49 {
50         s32 status;
51
52         DEBUGFUNC("txgbe_init_shared_code");
53
54         /*
55          * Set the mac type
56          */
57         txgbe_set_mac_type(hw);
58
59         txgbe_init_ops_dummy(hw);
60         switch (hw->mac.type) {
61         case txgbe_mac_raptor:
62                 status = txgbe_init_ops_pf(hw);
63                 break;
64         default:
65                 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
66                 break;
67         }
68         hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
69
70         hw->bus.set_lan_id(hw);
71
72         return status;
73 }
74
75 /**
76  *  txgbe_set_mac_type - Sets MAC type
77  *  @hw: pointer to the HW structure
78  *
79  *  This function sets the mac type of the adapter based on the
80  *  vendor ID and device ID stored in the hw structure.
81  **/
82 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
83 {
84         s32 err = 0;
85
86         DEBUGFUNC("txgbe_set_mac_type");
87
88         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
89                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
90                 return TXGBE_ERR_DEVICE_NOT_SUPPORTED;
91         }
92
93         switch (hw->device_id) {
94         case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
95                 hw->phy.media_type = txgbe_media_type_backplane;
96                 hw->mac.type = txgbe_mac_raptor;
97                 break;
98         case TXGBE_DEV_ID_RAPTOR_XAUI:
99         case TXGBE_DEV_ID_RAPTOR_SGMII:
100                 hw->phy.media_type = txgbe_media_type_copper;
101                 hw->mac.type = txgbe_mac_raptor;
102                 break;
103         case TXGBE_DEV_ID_RAPTOR_SFP:
104         case TXGBE_DEV_ID_WX1820_SFP:
105                 hw->phy.media_type = txgbe_media_type_fiber;
106                 hw->mac.type = txgbe_mac_raptor;
107                 break;
108         case TXGBE_DEV_ID_RAPTOR_QSFP:
109                 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
110                 hw->mac.type = txgbe_mac_raptor;
111                 break;
112         case TXGBE_DEV_ID_RAPTOR_VF:
113         case TXGBE_DEV_ID_RAPTOR_VF_HV:
114                 hw->phy.media_type = txgbe_media_type_virtual;
115                 hw->mac.type = txgbe_mac_raptor_vf;
116                 break;
117         default:
118                 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
119                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
120                 break;
121         }
122
123         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
124                   hw->mac.type, hw->phy.media_type, err);
125         return err;
126 }
127
128 /**
129  *  txgbe_init_ops_pf - Inits func ptrs and MAC type
130  *  @hw: pointer to hardware structure
131  *
132  *  Initialize the function pointers and assign the MAC type.
133  *  Does not touch the hardware.
134  **/
135 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
136 {
137         struct txgbe_bus_info *bus = &hw->bus;
138         struct txgbe_mac_info *mac = &hw->mac;
139         struct txgbe_rom_info *rom = &hw->rom;
140
141         DEBUGFUNC("txgbe_init_ops_pf");
142
143         /* BUS */
144         bus->set_lan_id = txgbe_set_lan_id_multi_port;
145
146         /* MAC */
147         mac->num_rar_entries    = TXGBE_RAPTOR_RAR_ENTRIES;
148
149         /* EEPROM */
150         rom->init_params = txgbe_init_eeprom_params;
151         rom->read16 = txgbe_ee_read16;
152         rom->readw_buffer = txgbe_ee_readw_buffer;
153         rom->readw_sw = txgbe_ee_readw_sw;
154         rom->read32 = txgbe_ee_read32;
155         rom->write16 = txgbe_ee_write16;
156         rom->writew_buffer = txgbe_ee_writew_buffer;
157         rom->writew_sw = txgbe_ee_writew_sw;
158         rom->write32 = txgbe_ee_write32;
159         rom->validate_checksum = txgbe_validate_eeprom_checksum;
160         rom->update_checksum = txgbe_update_eeprom_checksum;
161         rom->calc_checksum = txgbe_calc_eeprom_checksum;
162
163         return 0;
164 }