net/ngbe: set MAC type and LAN ID with initialization
[dpdk.git] / drivers / net / ngbe / base / ngbe_hw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 Beijing WangXun Technology Co., Ltd.
3  * Copyright(c) 2010-2017 Intel Corporation
4  */
5
6 #include "ngbe_type.h"
7 #include "ngbe_hw.h"
8
9 /**
10  *  ngbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
11  *  @hw: pointer to the HW structure
12  *
13  *  Determines the LAN function id by reading memory-mapped registers and swaps
14  *  the port value if requested, and set MAC instance for devices.
15  **/
16 void ngbe_set_lan_id_multi_port(struct ngbe_hw *hw)
17 {
18         struct ngbe_bus_info *bus = &hw->bus;
19         u32 reg = 0;
20
21         DEBUGFUNC("ngbe_set_lan_id_multi_port");
22
23         reg = rd32(hw, NGBE_PORTSTAT);
24         bus->lan_id = NGBE_PORTSTAT_ID(reg);
25         bus->func = bus->lan_id;
26 }
27
28 /**
29  *  ngbe_set_mac_type - Sets MAC type
30  *  @hw: pointer to the HW structure
31  *
32  *  This function sets the mac type of the adapter based on the
33  *  vendor ID and device ID stored in the hw structure.
34  **/
35 s32 ngbe_set_mac_type(struct ngbe_hw *hw)
36 {
37         s32 err = 0;
38
39         DEBUGFUNC("ngbe_set_mac_type");
40
41         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
42                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
43                 return NGBE_ERR_DEVICE_NOT_SUPPORTED;
44         }
45
46         switch (hw->sub_device_id) {
47         case NGBE_SUB_DEV_ID_EM_RTL_SGMII:
48         case NGBE_SUB_DEV_ID_EM_MVL_RGMII:
49                 hw->phy.media_type = ngbe_media_type_copper;
50                 hw->mac.type = ngbe_mac_em;
51                 break;
52         case NGBE_SUB_DEV_ID_EM_MVL_SFP:
53         case NGBE_SUB_DEV_ID_EM_YT8521S_SFP:
54                 hw->phy.media_type = ngbe_media_type_fiber;
55                 hw->mac.type = ngbe_mac_em;
56                 break;
57         case NGBE_SUB_DEV_ID_EM_VF:
58                 hw->phy.media_type = ngbe_media_type_virtual;
59                 hw->mac.type = ngbe_mac_em_vf;
60                 break;
61         default:
62                 err = NGBE_ERR_DEVICE_NOT_SUPPORTED;
63                 hw->phy.media_type = ngbe_media_type_unknown;
64                 hw->mac.type = ngbe_mac_unknown;
65                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
66                 break;
67         }
68
69         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
70                   hw->mac.type, hw->phy.media_type, err);
71         return err;
72 }
73
74 void ngbe_map_device_id(struct ngbe_hw *hw)
75 {
76         u16 oem = hw->sub_system_id & NGBE_OEM_MASK;
77         u16 internal = hw->sub_system_id & NGBE_INTERNAL_MASK;
78         hw->is_pf = true;
79
80         /* move subsystem_device_id to device_id */
81         switch (hw->device_id) {
82         case NGBE_DEV_ID_EM_WX1860AL_W_VF:
83         case NGBE_DEV_ID_EM_WX1860A2_VF:
84         case NGBE_DEV_ID_EM_WX1860A2S_VF:
85         case NGBE_DEV_ID_EM_WX1860A4_VF:
86         case NGBE_DEV_ID_EM_WX1860A4S_VF:
87         case NGBE_DEV_ID_EM_WX1860AL2_VF:
88         case NGBE_DEV_ID_EM_WX1860AL2S_VF:
89         case NGBE_DEV_ID_EM_WX1860AL4_VF:
90         case NGBE_DEV_ID_EM_WX1860AL4S_VF:
91         case NGBE_DEV_ID_EM_WX1860NCSI_VF:
92         case NGBE_DEV_ID_EM_WX1860A1_VF:
93         case NGBE_DEV_ID_EM_WX1860A1L_VF:
94                 hw->device_id = NGBE_DEV_ID_EM_VF;
95                 hw->sub_device_id = NGBE_SUB_DEV_ID_EM_VF;
96                 hw->is_pf = false;
97                 break;
98         case NGBE_DEV_ID_EM_WX1860AL_W:
99         case NGBE_DEV_ID_EM_WX1860A2:
100         case NGBE_DEV_ID_EM_WX1860A2S:
101         case NGBE_DEV_ID_EM_WX1860A4:
102         case NGBE_DEV_ID_EM_WX1860A4S:
103         case NGBE_DEV_ID_EM_WX1860AL2:
104         case NGBE_DEV_ID_EM_WX1860AL2S:
105         case NGBE_DEV_ID_EM_WX1860AL4:
106         case NGBE_DEV_ID_EM_WX1860AL4S:
107         case NGBE_DEV_ID_EM_WX1860NCSI:
108         case NGBE_DEV_ID_EM_WX1860A1:
109         case NGBE_DEV_ID_EM_WX1860A1L:
110                 hw->device_id = NGBE_DEV_ID_EM;
111                 if (oem == NGBE_LY_M88E1512_SFP ||
112                                 internal == NGBE_INTERNAL_SFP)
113                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_MVL_SFP;
114                 else if (hw->sub_system_id == NGBE_SUB_DEV_ID_EM_M88E1512_RJ45)
115                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_MVL_RGMII;
116                 else if (oem == NGBE_YT8521S_SFP ||
117                                 oem == NGBE_LY_YT8521S_SFP)
118                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_YT8521S_SFP;
119                 else
120                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_RTL_SGMII;
121                 break;
122         default:
123                 break;
124         }
125 }
126
127 /**
128  *  ngbe_init_ops_pf - Inits func ptrs and MAC type
129  *  @hw: pointer to hardware structure
130  *
131  *  Initialize the function pointers and assign the MAC type.
132  *  Does not touch the hardware.
133  **/
134 s32 ngbe_init_ops_pf(struct ngbe_hw *hw)
135 {
136         struct ngbe_bus_info *bus = &hw->bus;
137
138         DEBUGFUNC("ngbe_init_ops_pf");
139
140         /* BUS */
141         bus->set_lan_id = ngbe_set_lan_id_multi_port;
142
143         return 0;
144 }
145
146 /**
147  *  ngbe_init_shared_code - Initialize the shared code
148  *  @hw: pointer to hardware structure
149  *
150  *  This will assign function pointers and assign the MAC type and PHY code.
151  *  Does not touch the hardware. This function must be called prior to any
152  *  other function in the shared code. The ngbe_hw structure should be
153  *  memset to 0 prior to calling this function.  The following fields in
154  *  hw structure should be filled in prior to calling this function:
155  *  hw_addr, back, device_id, vendor_id, subsystem_device_id
156  **/
157 s32 ngbe_init_shared_code(struct ngbe_hw *hw)
158 {
159         s32 status = 0;
160
161         DEBUGFUNC("ngbe_init_shared_code");
162
163         /*
164          * Set the mac type
165          */
166         ngbe_set_mac_type(hw);
167
168         ngbe_init_ops_dummy(hw);
169         switch (hw->mac.type) {
170         case ngbe_mac_em:
171                 ngbe_init_ops_pf(hw);
172                 break;
173         default:
174                 status = NGBE_ERR_DEVICE_NOT_SUPPORTED;
175                 break;
176         }
177
178         hw->bus.set_lan_id(hw);
179
180         return status;
181 }
182