d2d12a929648729021b5ccaa1801f65a56cb9350
[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_phy.h"
7 #include "txgbe_eeprom.h"
8 #include "txgbe_mng.h"
9 #include "txgbe_hw.h"
10
11 #define TXGBE_RAPTOR_RAR_ENTRIES   128
12
13 /**
14  *  txgbe_init_hw - Generic hardware initialization
15  *  @hw: pointer to hardware structure
16  *
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
22  **/
23 s32 txgbe_init_hw(struct txgbe_hw *hw)
24 {
25         s32 status;
26
27         DEBUGFUNC("txgbe_init_hw");
28
29         /* Reset the hardware */
30         status = hw->mac.reset_hw(hw);
31         if (status == 0 || status == TXGBE_ERR_SFP_NOT_PRESENT) {
32                 /* Start the HW */
33                 status = hw->mac.start_hw(hw);
34         }
35
36         if (status != 0)
37                 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
38
39         return status;
40 }
41
42
43 /**
44  *  txgbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
45  *  @hw: pointer to the HW structure
46  *
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.
49  **/
50 void txgbe_set_lan_id_multi_port(struct txgbe_hw *hw)
51 {
52         struct txgbe_bus_info *bus = &hw->bus;
53         u32 reg;
54
55         DEBUGFUNC("txgbe_set_lan_id_multi_port_pcie");
56
57         reg = rd32(hw, TXGBE_PORTSTAT);
58         bus->lan_id = TXGBE_PORTSTAT_ID(reg);
59
60         /* check for single port */
61         reg = rd32(hw, TXGBE_PWR);
62         if (TXGBE_PWR_LANID(reg) == TXGBE_PWR_LANID_SWAP)
63                 bus->func = 0;
64         else
65                 bus->func = bus->lan_id;
66 }
67
68 /**
69  *  txgbe_validate_mac_addr - Validate MAC address
70  *  @mac_addr: pointer to MAC address.
71  *
72  *  Tests a MAC address to ensure it is a valid Individual Address.
73  **/
74 s32 txgbe_validate_mac_addr(u8 *mac_addr)
75 {
76         s32 status = 0;
77
78         DEBUGFUNC("txgbe_validate_mac_addr");
79
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;
90         }
91         return status;
92 }
93
94 /**
95  * txgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
96  * @hw: pointer to the hardware structure
97  *
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.
101  **/
102 void txgbe_clear_tx_pending(struct txgbe_hw *hw)
103 {
104         u32 hlreg0, i, poll;
105
106         /*
107          * If double reset is not requested then all transactions should
108          * already be clear and as such there is no work to do
109          */
110         if (!(hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
111                 return;
112
113         hlreg0 = rd32(hw, TXGBE_PSRCTL);
114         wr32(hw, TXGBE_PSRCTL, hlreg0 | TXGBE_PSRCTL_LBENA);
115
116         /* Wait for a last completion before clearing buffers */
117         txgbe_flush(hw);
118         msec_delay(3);
119
120         /*
121          * Before proceeding, make sure that the PCIe block does not have
122          * transactions pending.
123          */
124         poll = (800 * 11) / 10;
125         for (i = 0; i < poll; i++)
126                 usec_delay(100);
127
128         /* Flush all writes and allow 20usec for all transactions to clear */
129         txgbe_flush(hw);
130         usec_delay(20);
131
132         /* restore previous register values */
133         wr32(hw, TXGBE_PSRCTL, hlreg0);
134 }
135
136 /**
137  *  txgbe_init_shared_code - Initialize the shared code
138  *  @hw: pointer to hardware structure
139  *
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
147  **/
148 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
149 {
150         s32 status;
151
152         DEBUGFUNC("txgbe_init_shared_code");
153
154         /*
155          * Set the mac type
156          */
157         txgbe_set_mac_type(hw);
158
159         txgbe_init_ops_dummy(hw);
160         switch (hw->mac.type) {
161         case txgbe_mac_raptor:
162                 status = txgbe_init_ops_pf(hw);
163                 break;
164         default:
165                 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
166                 break;
167         }
168         hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
169
170         hw->bus.set_lan_id(hw);
171
172         return status;
173 }
174
175 /**
176  *  txgbe_set_mac_type - Sets MAC type
177  *  @hw: pointer to the HW structure
178  *
179  *  This function sets the mac type of the adapter based on the
180  *  vendor ID and device ID stored in the hw structure.
181  **/
182 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
183 {
184         s32 err = 0;
185
186         DEBUGFUNC("txgbe_set_mac_type");
187
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;
191         }
192
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;
197                 break;
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;
202                 break;
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;
207                 break;
208         case TXGBE_DEV_ID_RAPTOR_QSFP:
209                 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
210                 hw->mac.type = txgbe_mac_raptor;
211                 break;
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;
216                 break;
217         default:
218                 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
219                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
220                 break;
221         }
222
223         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
224                   hw->mac.type, hw->phy.media_type, err);
225         return err;
226 }
227
228 void txgbe_init_mac_link_ops(struct txgbe_hw *hw)
229 {
230         struct txgbe_mac_info *mac = &hw->mac;
231
232         DEBUGFUNC("txgbe_init_mac_link_ops");
233         RTE_SET_USED(mac);
234 }
235
236 /**
237  *  txgbe_init_phy_raptor - PHY/SFP specific init
238  *  @hw: pointer to hardware structure
239  *
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.
243  *
244  **/
245 s32 txgbe_init_phy_raptor(struct txgbe_hw *hw)
246 {
247         struct txgbe_phy_info *phy = &hw->phy;
248         s32 err = 0;
249
250         DEBUGFUNC("txgbe_init_phy_raptor");
251
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;
255
256                 /* Initialize access to QSFP+ I2C bus */
257                 txgbe_flush(hw);
258         }
259
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;
264
265         /* Setup function pointers based on detected SFP module and speeds */
266         txgbe_init_mac_link_ops(hw);
267
268 init_phy_ops_out:
269         return err;
270 }
271
272 /**
273  *  txgbe_init_ops_pf - Inits func ptrs and MAC type
274  *  @hw: pointer to hardware structure
275  *
276  *  Initialize the function pointers and assign the MAC type.
277  *  Does not touch the hardware.
278  **/
279 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
280 {
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;
285
286         DEBUGFUNC("txgbe_init_ops_pf");
287
288         /* BUS */
289         bus->set_lan_id = txgbe_set_lan_id_multi_port;
290
291         /* PHY */
292         phy->identify = txgbe_identify_phy;
293         phy->init = txgbe_init_phy_raptor;
294         phy->read_i2c_byte = txgbe_read_i2c_byte;
295         phy->write_i2c_byte = txgbe_write_i2c_byte;
296         phy->read_i2c_eeprom = txgbe_read_i2c_eeprom;
297         phy->write_i2c_eeprom = txgbe_write_i2c_eeprom;
298
299         /* MAC */
300         mac->init_hw = txgbe_init_hw;
301         mac->reset_hw = txgbe_reset_hw;
302         mac->num_rar_entries    = TXGBE_RAPTOR_RAR_ENTRIES;
303
304         /* EEPROM */
305         rom->init_params = txgbe_init_eeprom_params;
306         rom->read16 = txgbe_ee_read16;
307         rom->readw_buffer = txgbe_ee_readw_buffer;
308         rom->readw_sw = txgbe_ee_readw_sw;
309         rom->read32 = txgbe_ee_read32;
310         rom->write16 = txgbe_ee_write16;
311         rom->writew_buffer = txgbe_ee_writew_buffer;
312         rom->writew_sw = txgbe_ee_writew_sw;
313         rom->write32 = txgbe_ee_write32;
314         rom->validate_checksum = txgbe_validate_eeprom_checksum;
315         rom->update_checksum = txgbe_update_eeprom_checksum;
316         rom->calc_checksum = txgbe_calc_eeprom_checksum;
317
318         return 0;
319 }
320
321 static int
322 txgbe_check_flash_load(struct txgbe_hw *hw, u32 check_bit)
323 {
324         u32 reg = 0;
325         u32 i;
326         int err = 0;
327         /* if there's flash existing */
328         if (!(rd32(hw, TXGBE_SPISTAT) & TXGBE_SPISTAT_BPFLASH)) {
329                 /* wait hw load flash done */
330                 for (i = 0; i < 10; i++) {
331                         reg = rd32(hw, TXGBE_ILDRSTAT);
332                         if (!(reg & check_bit)) {
333                                 /* done */
334                                 break;
335                         }
336                         msleep(100);
337                 }
338                 if (i == 10)
339                         err = TXGBE_ERR_FLASH_LOADING_FAILED;
340         }
341         return err;
342 }
343
344 /**
345  *  txgbe_reset_hw - Perform hardware reset
346  *  @hw: pointer to hardware structure
347  *
348  *  Resets the hardware by resetting the transmit and receive units, masks
349  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
350  *  reset.
351  **/
352 s32 txgbe_reset_hw(struct txgbe_hw *hw)
353 {
354         s32 status;
355         u32 autoc;
356
357         DEBUGFUNC("txgbe_reset_hw");
358
359         /* Call adapter stop to disable tx/rx and clear interrupts */
360         status = hw->mac.stop_hw(hw);
361         if (status != 0)
362                 return status;
363
364         /* flush pending Tx transactions */
365         txgbe_clear_tx_pending(hw);
366
367         /* Identify PHY and related function pointers */
368         status = hw->phy.init(hw);
369         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
370                 return status;
371
372         /* Setup SFP module if there is one present. */
373         if (hw->phy.sfp_setup_needed) {
374                 status = hw->mac.setup_sfp(hw);
375                 hw->phy.sfp_setup_needed = false;
376         }
377         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
378                 return status;
379
380         /* Reset PHY */
381         if (!hw->phy.reset_disable)
382                 hw->phy.reset(hw);
383
384         /* remember AUTOC from before we reset */
385         autoc = hw->mac.autoc_read(hw);
386
387 mac_reset_top:
388         /*
389          * Issue global reset to the MAC.  Needs to be SW reset if link is up.
390          * If link reset is used when link is up, it might reset the PHY when
391          * mng is using it.  If link is down or the flag to force full link
392          * reset is set, then perform link reset.
393          */
394         if (txgbe_mng_present(hw)) {
395                 txgbe_hic_reset(hw);
396         } else {
397                 wr32(hw, TXGBE_RST, TXGBE_RST_LAN(hw->bus.lan_id));
398                 txgbe_flush(hw);
399         }
400         usec_delay(10);
401
402         if (hw->bus.lan_id == 0) {
403                 status = txgbe_check_flash_load(hw,
404                                 TXGBE_ILDRSTAT_SWRST_LAN0);
405         } else {
406                 status = txgbe_check_flash_load(hw,
407                                 TXGBE_ILDRSTAT_SWRST_LAN1);
408         }
409         if (status != 0)
410                 return status;
411
412         msec_delay(50);
413
414         /*
415          * Double resets are required for recovery from certain error
416          * conditions.  Between resets, it is necessary to stall to
417          * allow time for any pending HW events to complete.
418          */
419         if (hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
420                 hw->mac.flags &= ~TXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
421                 goto mac_reset_top;
422         }
423
424         /*
425          * Store the original AUTOC/AUTOC2 values if they have not been
426          * stored off yet.  Otherwise restore the stored original
427          * values since the reset operation sets back to defaults.
428          */
429         if (!hw->mac.orig_link_settings_stored) {
430                 hw->mac.orig_autoc = hw->mac.autoc_read(hw);
431                 hw->mac.autoc_write(hw, hw->mac.orig_autoc);
432                 hw->mac.orig_link_settings_stored = true;
433         } else {
434                 hw->mac.orig_autoc = autoc;
435         }
436
437         /* Store the permanent mac address */
438         hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
439
440         /*
441          * Store MAC address from RAR0, clear receive address registers, and
442          * clear the multicast table.  Also reset num_rar_entries to 128,
443          * since we modify this value when programming the SAN MAC address.
444          */
445         hw->mac.num_rar_entries = 128;
446         hw->mac.init_rx_addrs(hw);
447
448         /* Store the permanent SAN mac address */
449         hw->mac.get_san_mac_addr(hw, hw->mac.san_addr);
450
451         /* Add the SAN MAC address to the RAR only if it's a valid address */
452         if (txgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
453                 /* Save the SAN MAC RAR index */
454                 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
455
456                 hw->mac.set_rar(hw, hw->mac.san_mac_rar_index,
457                                     hw->mac.san_addr, 0, true);
458
459                 /* clear VMDq pool/queue selection for this RAR */
460                 hw->mac.clear_vmdq(hw, hw->mac.san_mac_rar_index,
461                                        BIT_MASK32);
462
463                 /* Reserve the last RAR for the SAN MAC address */
464                 hw->mac.num_rar_entries--;
465         }
466
467         /* Store the alternative WWNN/WWPN prefix */
468         hw->mac.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
469                                    &hw->mac.wwpn_prefix);
470
471         return status;
472 }
473