5b346746efb2459d12a73e3bf8fbb0affeaa106a
[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_MAX_TX_QUEUES 128
12 #define TXGBE_RAPTOR_MAX_RX_QUEUES 128
13 #define TXGBE_RAPTOR_RAR_ENTRIES   128
14
15 static s32 txgbe_setup_copper_link_raptor(struct txgbe_hw *hw,
16                                          u32 speed,
17                                          bool autoneg_wait_to_complete);
18
19 /**
20  *  txgbe_init_hw - Generic hardware initialization
21  *  @hw: pointer to hardware structure
22  *
23  *  Initialize the hardware by resetting the hardware, filling the bus info
24  *  structure and media type, clears all on chip counters, initializes receive
25  *  address registers, multicast table, VLAN filter table, calls routine to set
26  *  up link and flow control settings, and leaves transmit and receive units
27  *  disabled and uninitialized
28  **/
29 s32 txgbe_init_hw(struct txgbe_hw *hw)
30 {
31         s32 status;
32
33         DEBUGFUNC("txgbe_init_hw");
34
35         /* Reset the hardware */
36         status = hw->mac.reset_hw(hw);
37         if (status == 0 || status == TXGBE_ERR_SFP_NOT_PRESENT) {
38                 /* Start the HW */
39                 status = hw->mac.start_hw(hw);
40         }
41
42         if (status != 0)
43                 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
44
45         return status;
46 }
47
48
49 /**
50  *  txgbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
51  *  @hw: pointer to the HW structure
52  *
53  *  Determines the LAN function id by reading memory-mapped registers and swaps
54  *  the port value if requested, and set MAC instance for devices.
55  **/
56 void txgbe_set_lan_id_multi_port(struct txgbe_hw *hw)
57 {
58         struct txgbe_bus_info *bus = &hw->bus;
59         u32 reg;
60
61         DEBUGFUNC("txgbe_set_lan_id_multi_port_pcie");
62
63         reg = rd32(hw, TXGBE_PORTSTAT);
64         bus->lan_id = TXGBE_PORTSTAT_ID(reg);
65
66         /* check for single port */
67         reg = rd32(hw, TXGBE_PWR);
68         if (TXGBE_PWR_LANID(reg) == TXGBE_PWR_LANID_SWAP)
69                 bus->func = 0;
70         else
71                 bus->func = bus->lan_id;
72 }
73
74 /**
75  *  txgbe_validate_mac_addr - Validate MAC address
76  *  @mac_addr: pointer to MAC address.
77  *
78  *  Tests a MAC address to ensure it is a valid Individual Address.
79  **/
80 s32 txgbe_validate_mac_addr(u8 *mac_addr)
81 {
82         s32 status = 0;
83
84         DEBUGFUNC("txgbe_validate_mac_addr");
85
86         /* Make sure it is not a multicast address */
87         if (TXGBE_IS_MULTICAST(mac_addr)) {
88                 status = TXGBE_ERR_INVALID_MAC_ADDR;
89         /* Not a broadcast address */
90         } else if (TXGBE_IS_BROADCAST(mac_addr)) {
91                 status = TXGBE_ERR_INVALID_MAC_ADDR;
92         /* Reject the zero address */
93         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
94                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
95                 status = TXGBE_ERR_INVALID_MAC_ADDR;
96         }
97         return status;
98 }
99
100 /**
101  *  txgbe_need_crosstalk_fix - Determine if we need to do cross talk fix
102  *  @hw: pointer to hardware structure
103  *
104  *  Contains the logic to identify if we need to verify link for the
105  *  crosstalk fix
106  **/
107 static bool txgbe_need_crosstalk_fix(struct txgbe_hw *hw)
108 {
109         /* Does FW say we need the fix */
110         if (!hw->need_crosstalk_fix)
111                 return false;
112
113         /* Only consider SFP+ PHYs i.e. media type fiber */
114         switch (hw->phy.media_type) {
115         case txgbe_media_type_fiber:
116         case txgbe_media_type_fiber_qsfp:
117                 break;
118         default:
119                 return false;
120         }
121
122         return true;
123 }
124
125 /**
126  *  txgbe_check_mac_link - Determine link and speed status
127  *  @hw: pointer to hardware structure
128  *  @speed: pointer to link speed
129  *  @link_up: true when link is up
130  *  @link_up_wait_to_complete: bool used to wait for link up or not
131  *
132  *  Reads the links register to determine if link is up and the current speed
133  **/
134 s32 txgbe_check_mac_link(struct txgbe_hw *hw, u32 *speed,
135                                  bool *link_up, bool link_up_wait_to_complete)
136 {
137         u32 links_reg, links_orig;
138         u32 i;
139
140         DEBUGFUNC("txgbe_check_mac_link");
141
142         /* If Crosstalk fix enabled do the sanity check of making sure
143          * the SFP+ cage is full.
144          */
145         if (txgbe_need_crosstalk_fix(hw)) {
146                 u32 sfp_cage_full;
147
148                 switch (hw->mac.type) {
149                 case txgbe_mac_raptor:
150                         sfp_cage_full = !rd32m(hw, TXGBE_GPIODATA,
151                                         TXGBE_GPIOBIT_2);
152                         break;
153                 default:
154                         /* sanity check - No SFP+ devices here */
155                         sfp_cage_full = false;
156                         break;
157                 }
158
159                 if (!sfp_cage_full) {
160                         *link_up = false;
161                         *speed = TXGBE_LINK_SPEED_UNKNOWN;
162                         return 0;
163                 }
164         }
165
166         /* clear the old state */
167         links_orig = rd32(hw, TXGBE_PORTSTAT);
168
169         links_reg = rd32(hw, TXGBE_PORTSTAT);
170
171         if (links_orig != links_reg) {
172                 DEBUGOUT("LINKS changed from %08X to %08X\n",
173                           links_orig, links_reg);
174         }
175
176         if (link_up_wait_to_complete) {
177                 for (i = 0; i < hw->mac.max_link_up_time; i++) {
178                         if (!(links_reg & TXGBE_PORTSTAT_UP)) {
179                                 *link_up = false;
180                         } else {
181                                 *link_up = true;
182                                 break;
183                         }
184                         msec_delay(100);
185                         links_reg = rd32(hw, TXGBE_PORTSTAT);
186                 }
187         } else {
188                 if (links_reg & TXGBE_PORTSTAT_UP)
189                         *link_up = true;
190                 else
191                         *link_up = false;
192         }
193
194         switch (links_reg & TXGBE_PORTSTAT_BW_MASK) {
195         case TXGBE_PORTSTAT_BW_10G:
196                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
197                 break;
198         case TXGBE_PORTSTAT_BW_1G:
199                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
200                 break;
201         case TXGBE_PORTSTAT_BW_100M:
202                 *speed = TXGBE_LINK_SPEED_100M_FULL;
203                 break;
204         default:
205                 *speed = TXGBE_LINK_SPEED_UNKNOWN;
206         }
207
208         return 0;
209 }
210
211 /**
212  * txgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
213  * @hw: pointer to the hardware structure
214  *
215  * The MACs can experience issues if TX work is still pending
216  * when a reset occurs.  This function prevents this by flushing the PCIe
217  * buffers on the system.
218  **/
219 void txgbe_clear_tx_pending(struct txgbe_hw *hw)
220 {
221         u32 hlreg0, i, poll;
222
223         /*
224          * If double reset is not requested then all transactions should
225          * already be clear and as such there is no work to do
226          */
227         if (!(hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
228                 return;
229
230         hlreg0 = rd32(hw, TXGBE_PSRCTL);
231         wr32(hw, TXGBE_PSRCTL, hlreg0 | TXGBE_PSRCTL_LBENA);
232
233         /* Wait for a last completion before clearing buffers */
234         txgbe_flush(hw);
235         msec_delay(3);
236
237         /*
238          * Before proceeding, make sure that the PCIe block does not have
239          * transactions pending.
240          */
241         poll = (800 * 11) / 10;
242         for (i = 0; i < poll; i++)
243                 usec_delay(100);
244
245         /* Flush all writes and allow 20usec for all transactions to clear */
246         txgbe_flush(hw);
247         usec_delay(20);
248
249         /* restore previous register values */
250         wr32(hw, TXGBE_PSRCTL, hlreg0);
251 }
252
253 /**
254  *  txgbe_init_shared_code - Initialize the shared code
255  *  @hw: pointer to hardware structure
256  *
257  *  This will assign function pointers and assign the MAC type and PHY code.
258  *  Does not touch the hardware. This function must be called prior to any
259  *  other function in the shared code. The txgbe_hw structure should be
260  *  memset to 0 prior to calling this function.  The following fields in
261  *  hw structure should be filled in prior to calling this function:
262  *  hw_addr, back, device_id, vendor_id, subsystem_device_id,
263  *  subsystem_vendor_id, and revision_id
264  **/
265 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
266 {
267         s32 status;
268
269         DEBUGFUNC("txgbe_init_shared_code");
270
271         /*
272          * Set the mac type
273          */
274         txgbe_set_mac_type(hw);
275
276         txgbe_init_ops_dummy(hw);
277         switch (hw->mac.type) {
278         case txgbe_mac_raptor:
279                 status = txgbe_init_ops_pf(hw);
280                 break;
281         default:
282                 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
283                 break;
284         }
285         hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
286
287         hw->bus.set_lan_id(hw);
288
289         return status;
290 }
291
292 /**
293  *  txgbe_set_mac_type - Sets MAC type
294  *  @hw: pointer to the HW structure
295  *
296  *  This function sets the mac type of the adapter based on the
297  *  vendor ID and device ID stored in the hw structure.
298  **/
299 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
300 {
301         s32 err = 0;
302
303         DEBUGFUNC("txgbe_set_mac_type");
304
305         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
306                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
307                 return TXGBE_ERR_DEVICE_NOT_SUPPORTED;
308         }
309
310         switch (hw->device_id) {
311         case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
312                 hw->phy.media_type = txgbe_media_type_backplane;
313                 hw->mac.type = txgbe_mac_raptor;
314                 break;
315         case TXGBE_DEV_ID_RAPTOR_XAUI:
316         case TXGBE_DEV_ID_RAPTOR_SGMII:
317                 hw->phy.media_type = txgbe_media_type_copper;
318                 hw->mac.type = txgbe_mac_raptor;
319                 break;
320         case TXGBE_DEV_ID_RAPTOR_SFP:
321         case TXGBE_DEV_ID_WX1820_SFP:
322                 hw->phy.media_type = txgbe_media_type_fiber;
323                 hw->mac.type = txgbe_mac_raptor;
324                 break;
325         case TXGBE_DEV_ID_RAPTOR_QSFP:
326                 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
327                 hw->mac.type = txgbe_mac_raptor;
328                 break;
329         case TXGBE_DEV_ID_RAPTOR_VF:
330         case TXGBE_DEV_ID_RAPTOR_VF_HV:
331                 hw->phy.media_type = txgbe_media_type_virtual;
332                 hw->mac.type = txgbe_mac_raptor_vf;
333                 break;
334         default:
335                 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
336                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
337                 break;
338         }
339
340         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
341                   hw->mac.type, hw->phy.media_type, err);
342         return err;
343 }
344
345 void txgbe_init_mac_link_ops(struct txgbe_hw *hw)
346 {
347         struct txgbe_mac_info *mac = &hw->mac;
348
349         DEBUGFUNC("txgbe_init_mac_link_ops");
350
351         mac->setup_link = txgbe_setup_mac_link;
352 }
353
354 /**
355  *  txgbe_init_phy_raptor - PHY/SFP specific init
356  *  @hw: pointer to hardware structure
357  *
358  *  Initialize any function pointers that were not able to be
359  *  set during init_shared_code because the PHY/SFP type was
360  *  not known.  Perform the SFP init if necessary.
361  *
362  **/
363 s32 txgbe_init_phy_raptor(struct txgbe_hw *hw)
364 {
365         struct txgbe_mac_info *mac = &hw->mac;
366         struct txgbe_phy_info *phy = &hw->phy;
367         s32 err = 0;
368
369         DEBUGFUNC("txgbe_init_phy_raptor");
370
371         if (hw->device_id == TXGBE_DEV_ID_RAPTOR_QSFP) {
372                 /* Store flag indicating I2C bus access control unit. */
373                 hw->phy.qsfp_shared_i2c_bus = TRUE;
374
375                 /* Initialize access to QSFP+ I2C bus */
376                 txgbe_flush(hw);
377         }
378
379         /* Identify the PHY or SFP module */
380         err = phy->identify(hw);
381         if (err == TXGBE_ERR_SFP_NOT_SUPPORTED)
382                 goto init_phy_ops_out;
383
384         /* Setup function pointers based on detected SFP module and speeds */
385         txgbe_init_mac_link_ops(hw);
386
387         /* If copper media, overwrite with copper function pointers */
388         if (phy->media_type == txgbe_media_type_copper) {
389                 mac->setup_link = txgbe_setup_copper_link_raptor;
390                 mac->get_link_capabilities =
391                                   txgbe_get_copper_link_capabilities;
392         }
393
394         /* Set necessary function pointers based on PHY type */
395         switch (hw->phy.type) {
396         case txgbe_phy_tn:
397                 phy->setup_link = txgbe_setup_phy_link_tnx;
398                 phy->check_link = txgbe_check_phy_link_tnx;
399                 break;
400         default:
401                 break;
402         }
403
404 init_phy_ops_out:
405         return err;
406 }
407
408 /**
409  *  txgbe_init_ops_pf - Inits func ptrs and MAC type
410  *  @hw: pointer to hardware structure
411  *
412  *  Initialize the function pointers and assign the MAC type.
413  *  Does not touch the hardware.
414  **/
415 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
416 {
417         struct txgbe_bus_info *bus = &hw->bus;
418         struct txgbe_mac_info *mac = &hw->mac;
419         struct txgbe_phy_info *phy = &hw->phy;
420         struct txgbe_rom_info *rom = &hw->rom;
421
422         DEBUGFUNC("txgbe_init_ops_pf");
423
424         /* BUS */
425         bus->set_lan_id = txgbe_set_lan_id_multi_port;
426
427         /* PHY */
428         phy->identify = txgbe_identify_phy;
429         phy->init = txgbe_init_phy_raptor;
430         phy->read_reg = txgbe_read_phy_reg;
431         phy->write_reg = txgbe_write_phy_reg;
432         phy->read_reg_mdi = txgbe_read_phy_reg_mdi;
433         phy->write_reg_mdi = txgbe_write_phy_reg_mdi;
434         phy->setup_link = txgbe_setup_phy_link;
435         phy->setup_link_speed = txgbe_setup_phy_link_speed;
436         phy->read_i2c_byte = txgbe_read_i2c_byte;
437         phy->write_i2c_byte = txgbe_write_i2c_byte;
438         phy->read_i2c_eeprom = txgbe_read_i2c_eeprom;
439         phy->write_i2c_eeprom = txgbe_write_i2c_eeprom;
440         phy->reset = txgbe_reset_phy;
441
442         /* MAC */
443         mac->init_hw = txgbe_init_hw;
444         mac->reset_hw = txgbe_reset_hw;
445
446         /* Link */
447         mac->get_link_capabilities = txgbe_get_link_capabilities_raptor;
448         mac->check_link = txgbe_check_mac_link;
449
450         /* EEPROM */
451         rom->init_params = txgbe_init_eeprom_params;
452         rom->read16 = txgbe_ee_read16;
453         rom->readw_buffer = txgbe_ee_readw_buffer;
454         rom->readw_sw = txgbe_ee_readw_sw;
455         rom->read32 = txgbe_ee_read32;
456         rom->write16 = txgbe_ee_write16;
457         rom->writew_buffer = txgbe_ee_writew_buffer;
458         rom->writew_sw = txgbe_ee_writew_sw;
459         rom->write32 = txgbe_ee_write32;
460         rom->validate_checksum = txgbe_validate_eeprom_checksum;
461         rom->update_checksum = txgbe_update_eeprom_checksum;
462         rom->calc_checksum = txgbe_calc_eeprom_checksum;
463
464         mac->num_rar_entries    = TXGBE_RAPTOR_RAR_ENTRIES;
465         mac->max_rx_queues      = TXGBE_RAPTOR_MAX_RX_QUEUES;
466         mac->max_tx_queues      = TXGBE_RAPTOR_MAX_TX_QUEUES;
467
468         return 0;
469 }
470
471 /**
472  *  txgbe_get_link_capabilities_raptor - Determines link capabilities
473  *  @hw: pointer to hardware structure
474  *  @speed: pointer to link speed
475  *  @autoneg: true when autoneg or autotry is enabled
476  *
477  *  Determines the link capabilities by reading the AUTOC register.
478  **/
479 s32 txgbe_get_link_capabilities_raptor(struct txgbe_hw *hw,
480                                       u32 *speed,
481                                       bool *autoneg)
482 {
483         s32 status = 0;
484         u32 autoc = 0;
485
486         DEBUGFUNC("txgbe_get_link_capabilities_raptor");
487
488         /* Check if 1G SFP module. */
489         if (hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core0 ||
490             hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core1 ||
491             hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core0 ||
492             hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core1 ||
493             hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core0 ||
494             hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core1) {
495                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
496                 *autoneg = true;
497                 return 0;
498         }
499
500         /*
501          * Determine link capabilities based on the stored value of AUTOC,
502          * which represents EEPROM defaults.  If AUTOC value has not
503          * been stored, use the current register values.
504          */
505         if (hw->mac.orig_link_settings_stored)
506                 autoc = hw->mac.orig_autoc;
507         else
508                 autoc = hw->mac.autoc_read(hw);
509
510         switch (autoc & TXGBE_AUTOC_LMS_MASK) {
511         case TXGBE_AUTOC_LMS_1G_LINK_NO_AN:
512                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
513                 *autoneg = false;
514                 break;
515
516         case TXGBE_AUTOC_LMS_10G_LINK_NO_AN:
517                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
518                 *autoneg = false;
519                 break;
520
521         case TXGBE_AUTOC_LMS_1G_AN:
522                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
523                 *autoneg = true;
524                 break;
525
526         case TXGBE_AUTOC_LMS_10G:
527                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
528                 *autoneg = false;
529                 break;
530
531         case TXGBE_AUTOC_LMS_KX4_KX_KR:
532         case TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN:
533                 *speed = TXGBE_LINK_SPEED_UNKNOWN;
534                 if (autoc & TXGBE_AUTOC_KR_SUPP)
535                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
536                 if (autoc & TXGBE_AUTOC_KX4_SUPP)
537                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
538                 if (autoc & TXGBE_AUTOC_KX_SUPP)
539                         *speed |= TXGBE_LINK_SPEED_1GB_FULL;
540                 *autoneg = true;
541                 break;
542
543         case TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII:
544                 *speed = TXGBE_LINK_SPEED_100M_FULL;
545                 if (autoc & TXGBE_AUTOC_KR_SUPP)
546                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
547                 if (autoc & TXGBE_AUTOC_KX4_SUPP)
548                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
549                 if (autoc & TXGBE_AUTOC_KX_SUPP)
550                         *speed |= TXGBE_LINK_SPEED_1GB_FULL;
551                 *autoneg = true;
552                 break;
553
554         case TXGBE_AUTOC_LMS_SGMII_1G_100M:
555                 *speed = TXGBE_LINK_SPEED_1GB_FULL |
556                          TXGBE_LINK_SPEED_100M_FULL |
557                          TXGBE_LINK_SPEED_10M_FULL;
558                 *autoneg = false;
559                 break;
560
561         default:
562                 return TXGBE_ERR_LINK_SETUP;
563         }
564
565         return status;
566 }
567
568 /**
569  *  txgbe_start_mac_link_raptor - Setup MAC link settings
570  *  @hw: pointer to hardware structure
571  *  @autoneg_wait_to_complete: true when waiting for completion is needed
572  *
573  *  Configures link settings based on values in the txgbe_hw struct.
574  *  Restarts the link.  Performs autonegotiation if needed.
575  **/
576 s32 txgbe_start_mac_link_raptor(struct txgbe_hw *hw,
577                                bool autoneg_wait_to_complete)
578 {
579         s32 status = 0;
580         bool got_lock = false;
581
582         DEBUGFUNC("txgbe_start_mac_link_raptor");
583
584         UNREFERENCED_PARAMETER(autoneg_wait_to_complete);
585
586         /*  reset_pipeline requires us to hold this lock as it writes to
587          *  AUTOC.
588          */
589         if (txgbe_verify_lesm_fw_enabled_raptor(hw)) {
590                 status = hw->mac.acquire_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
591                 if (status != 0)
592                         goto out;
593
594                 got_lock = true;
595         }
596
597         /* Restart link */
598         txgbe_reset_pipeline_raptor(hw);
599
600         if (got_lock)
601                 hw->mac.release_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
602
603         /* Add delay to filter out noises during initial link setup */
604         msec_delay(50);
605
606 out:
607         return status;
608 }
609
610 /**
611  *  txgbe_setup_mac_link - Set MAC link speed
612  *  @hw: pointer to hardware structure
613  *  @speed: new link speed
614  *  @autoneg_wait_to_complete: true when waiting for completion is needed
615  *
616  *  Set the link speed in the AUTOC register and restarts link.
617  **/
618 s32 txgbe_setup_mac_link(struct txgbe_hw *hw,
619                                u32 speed,
620                                bool autoneg_wait_to_complete)
621 {
622         bool autoneg = false;
623         s32 status = 0;
624
625         u64 autoc = hw->mac.autoc_read(hw);
626         u64 pma_pmd_10gs = autoc & TXGBE_AUTOC_10GS_PMA_PMD_MASK;
627         u64 pma_pmd_1g = autoc & TXGBE_AUTOC_1G_PMA_PMD_MASK;
628         u64 link_mode = autoc & TXGBE_AUTOC_LMS_MASK;
629         u64 current_autoc = autoc;
630         u64 orig_autoc = 0;
631         u32 links_reg;
632         u32 i;
633         u32 link_capabilities = TXGBE_LINK_SPEED_UNKNOWN;
634
635         DEBUGFUNC("txgbe_setup_mac_link");
636
637         /* Check to see if speed passed in is supported. */
638         status = hw->mac.get_link_capabilities(hw,
639                         &link_capabilities, &autoneg);
640         if (status)
641                 return status;
642
643         speed &= link_capabilities;
644         if (speed == TXGBE_LINK_SPEED_UNKNOWN)
645                 return TXGBE_ERR_LINK_SETUP;
646
647         /* Use stored value (EEPROM defaults) of AUTOC to find KR/KX4 support*/
648         if (hw->mac.orig_link_settings_stored)
649                 orig_autoc = hw->mac.orig_autoc;
650         else
651                 orig_autoc = autoc;
652
653         link_mode = autoc & TXGBE_AUTOC_LMS_MASK;
654         pma_pmd_1g = autoc & TXGBE_AUTOC_1G_PMA_PMD_MASK;
655
656         if (link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR ||
657             link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
658             link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
659                 /* Set KX4/KX/KR support according to speed requested */
660                 autoc &= ~(TXGBE_AUTOC_KX_SUPP |
661                            TXGBE_AUTOC_KX4_SUPP |
662                            TXGBE_AUTOC_KR_SUPP);
663                 if (speed & TXGBE_LINK_SPEED_10GB_FULL) {
664                         if (orig_autoc & TXGBE_AUTOC_KX4_SUPP)
665                                 autoc |= TXGBE_AUTOC_KX4_SUPP;
666                         if ((orig_autoc & TXGBE_AUTOC_KR_SUPP) &&
667                             !hw->phy.smart_speed_active)
668                                 autoc |= TXGBE_AUTOC_KR_SUPP;
669                 }
670                 if (speed & TXGBE_LINK_SPEED_1GB_FULL)
671                         autoc |= TXGBE_AUTOC_KX_SUPP;
672         } else if ((pma_pmd_1g == TXGBE_AUTOC_1G_SFI) &&
673                    (link_mode == TXGBE_AUTOC_LMS_1G_LINK_NO_AN ||
674                     link_mode == TXGBE_AUTOC_LMS_1G_AN)) {
675                 /* Switch from 1G SFI to 10G SFI if requested */
676                 if (speed == TXGBE_LINK_SPEED_10GB_FULL &&
677                     pma_pmd_10gs == TXGBE_AUTOC_10GS_SFI) {
678                         autoc &= ~TXGBE_AUTOC_LMS_MASK;
679                         autoc |= TXGBE_AUTOC_LMS_10G;
680                 }
681         } else if ((pma_pmd_10gs == TXGBE_AUTOC_10GS_SFI) &&
682                    (link_mode == TXGBE_AUTOC_LMS_10G)) {
683                 /* Switch from 10G SFI to 1G SFI if requested */
684                 if (speed == TXGBE_LINK_SPEED_1GB_FULL &&
685                     pma_pmd_1g == TXGBE_AUTOC_1G_SFI) {
686                         autoc &= ~TXGBE_AUTOC_LMS_MASK;
687                         if (autoneg || hw->phy.type == txgbe_phy_qsfp_intel)
688                                 autoc |= TXGBE_AUTOC_LMS_1G_AN;
689                         else
690                                 autoc |= TXGBE_AUTOC_LMS_1G_LINK_NO_AN;
691                 }
692         }
693
694         if (autoc == current_autoc)
695                 return status;
696
697         autoc &= ~TXGBE_AUTOC_SPEED_MASK;
698         autoc |= TXGBE_AUTOC_SPEED(speed);
699         autoc |= (autoneg ? TXGBE_AUTOC_AUTONEG : 0);
700
701         /* Restart link */
702         hw->mac.autoc_write(hw, autoc);
703
704         /* Only poll for autoneg to complete if specified to do so */
705         if (autoneg_wait_to_complete) {
706                 if (link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR ||
707                     link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
708                     link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
709                         links_reg = 0; /*Just in case Autoneg time=0*/
710                         for (i = 0; i < TXGBE_AUTO_NEG_TIME; i++) {
711                                 links_reg = rd32(hw, TXGBE_PORTSTAT);
712                                 if (links_reg & TXGBE_PORTSTAT_UP)
713                                         break;
714                                 msec_delay(100);
715                         }
716                         if (!(links_reg & TXGBE_PORTSTAT_UP)) {
717                                 status = TXGBE_ERR_AUTONEG_NOT_COMPLETE;
718                                 DEBUGOUT("Autoneg did not complete.\n");
719                         }
720                 }
721         }
722
723         /* Add delay to filter out noises during initial link setup */
724         msec_delay(50);
725
726         return status;
727 }
728
729 /**
730  *  txgbe_setup_copper_link_raptor - Set the PHY autoneg advertised field
731  *  @hw: pointer to hardware structure
732  *  @speed: new link speed
733  *  @autoneg_wait_to_complete: true if waiting is needed to complete
734  *
735  *  Restarts link on PHY and MAC based on settings passed in.
736  **/
737 static s32 txgbe_setup_copper_link_raptor(struct txgbe_hw *hw,
738                                          u32 speed,
739                                          bool autoneg_wait_to_complete)
740 {
741         s32 status;
742
743         DEBUGFUNC("txgbe_setup_copper_link_raptor");
744
745         /* Setup the PHY according to input speed */
746         status = hw->phy.setup_link_speed(hw, speed,
747                                               autoneg_wait_to_complete);
748         /* Set up MAC */
749         txgbe_start_mac_link_raptor(hw, autoneg_wait_to_complete);
750
751         return status;
752 }
753
754 static int
755 txgbe_check_flash_load(struct txgbe_hw *hw, u32 check_bit)
756 {
757         u32 reg = 0;
758         u32 i;
759         int err = 0;
760         /* if there's flash existing */
761         if (!(rd32(hw, TXGBE_SPISTAT) & TXGBE_SPISTAT_BPFLASH)) {
762                 /* wait hw load flash done */
763                 for (i = 0; i < 10; i++) {
764                         reg = rd32(hw, TXGBE_ILDRSTAT);
765                         if (!(reg & check_bit)) {
766                                 /* done */
767                                 break;
768                         }
769                         msleep(100);
770                 }
771                 if (i == 10)
772                         err = TXGBE_ERR_FLASH_LOADING_FAILED;
773         }
774         return err;
775 }
776
777 /**
778  *  txgbe_reset_hw - Perform hardware reset
779  *  @hw: pointer to hardware structure
780  *
781  *  Resets the hardware by resetting the transmit and receive units, masks
782  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
783  *  reset.
784  **/
785 s32 txgbe_reset_hw(struct txgbe_hw *hw)
786 {
787         s32 status;
788         u32 autoc;
789
790         DEBUGFUNC("txgbe_reset_hw");
791
792         /* Call adapter stop to disable tx/rx and clear interrupts */
793         status = hw->mac.stop_hw(hw);
794         if (status != 0)
795                 return status;
796
797         /* flush pending Tx transactions */
798         txgbe_clear_tx_pending(hw);
799
800         /* Identify PHY and related function pointers */
801         status = hw->phy.init(hw);
802         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
803                 return status;
804
805         /* Setup SFP module if there is one present. */
806         if (hw->phy.sfp_setup_needed) {
807                 status = hw->mac.setup_sfp(hw);
808                 hw->phy.sfp_setup_needed = false;
809         }
810         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
811                 return status;
812
813         /* Reset PHY */
814         if (!hw->phy.reset_disable)
815                 hw->phy.reset(hw);
816
817         /* remember AUTOC from before we reset */
818         autoc = hw->mac.autoc_read(hw);
819
820 mac_reset_top:
821         /*
822          * Issue global reset to the MAC.  Needs to be SW reset if link is up.
823          * If link reset is used when link is up, it might reset the PHY when
824          * mng is using it.  If link is down or the flag to force full link
825          * reset is set, then perform link reset.
826          */
827         if (txgbe_mng_present(hw)) {
828                 txgbe_hic_reset(hw);
829         } else {
830                 wr32(hw, TXGBE_RST, TXGBE_RST_LAN(hw->bus.lan_id));
831                 txgbe_flush(hw);
832         }
833         usec_delay(10);
834
835         if (hw->bus.lan_id == 0) {
836                 status = txgbe_check_flash_load(hw,
837                                 TXGBE_ILDRSTAT_SWRST_LAN0);
838         } else {
839                 status = txgbe_check_flash_load(hw,
840                                 TXGBE_ILDRSTAT_SWRST_LAN1);
841         }
842         if (status != 0)
843                 return status;
844
845         msec_delay(50);
846
847         /*
848          * Double resets are required for recovery from certain error
849          * conditions.  Between resets, it is necessary to stall to
850          * allow time for any pending HW events to complete.
851          */
852         if (hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
853                 hw->mac.flags &= ~TXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
854                 goto mac_reset_top;
855         }
856
857         /*
858          * Store the original AUTOC/AUTOC2 values if they have not been
859          * stored off yet.  Otherwise restore the stored original
860          * values since the reset operation sets back to defaults.
861          */
862         if (!hw->mac.orig_link_settings_stored) {
863                 hw->mac.orig_autoc = hw->mac.autoc_read(hw);
864                 hw->mac.autoc_write(hw, hw->mac.orig_autoc);
865                 hw->mac.orig_link_settings_stored = true;
866         } else {
867                 hw->mac.orig_autoc = autoc;
868         }
869
870         /* Store the permanent mac address */
871         hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
872
873         /*
874          * Store MAC address from RAR0, clear receive address registers, and
875          * clear the multicast table.  Also reset num_rar_entries to 128,
876          * since we modify this value when programming the SAN MAC address.
877          */
878         hw->mac.num_rar_entries = 128;
879         hw->mac.init_rx_addrs(hw);
880
881         /* Store the permanent SAN mac address */
882         hw->mac.get_san_mac_addr(hw, hw->mac.san_addr);
883
884         /* Add the SAN MAC address to the RAR only if it's a valid address */
885         if (txgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
886                 /* Save the SAN MAC RAR index */
887                 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
888
889                 hw->mac.set_rar(hw, hw->mac.san_mac_rar_index,
890                                     hw->mac.san_addr, 0, true);
891
892                 /* clear VMDq pool/queue selection for this RAR */
893                 hw->mac.clear_vmdq(hw, hw->mac.san_mac_rar_index,
894                                        BIT_MASK32);
895
896                 /* Reserve the last RAR for the SAN MAC address */
897                 hw->mac.num_rar_entries--;
898         }
899
900         /* Store the alternative WWNN/WWPN prefix */
901         hw->mac.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
902                                    &hw->mac.wwpn_prefix);
903
904         return status;
905 }
906
907 /**
908  *  txgbe_verify_lesm_fw_enabled_raptor - Checks LESM FW module state.
909  *  @hw: pointer to hardware structure
910  *
911  *  Returns true if the LESM FW module is present and enabled. Otherwise
912  *  returns false. Smart Speed must be disabled if LESM FW module is enabled.
913  **/
914 bool txgbe_verify_lesm_fw_enabled_raptor(struct txgbe_hw *hw)
915 {
916         bool lesm_enabled = false;
917         u16 fw_offset, fw_lesm_param_offset, fw_lesm_state;
918         s32 status;
919
920         DEBUGFUNC("txgbe_verify_lesm_fw_enabled_raptor");
921
922         /* get the offset to the Firmware Module block */
923         status = hw->rom.read16(hw, TXGBE_FW_PTR, &fw_offset);
924
925         if (status != 0 || fw_offset == 0 || fw_offset == 0xFFFF)
926                 goto out;
927
928         /* get the offset to the LESM Parameters block */
929         status = hw->rom.read16(hw, (fw_offset +
930                                      TXGBE_FW_LESM_PARAMETERS_PTR),
931                                      &fw_lesm_param_offset);
932
933         if (status != 0 ||
934             fw_lesm_param_offset == 0 || fw_lesm_param_offset == 0xFFFF)
935                 goto out;
936
937         /* get the LESM state word */
938         status = hw->rom.read16(hw, (fw_lesm_param_offset +
939                                      TXGBE_FW_LESM_STATE_1),
940                                      &fw_lesm_state);
941
942         if (status == 0 && (fw_lesm_state & TXGBE_FW_LESM_STATE_ENABLED))
943                 lesm_enabled = true;
944
945 out:
946         lesm_enabled = false;
947         return lesm_enabled;
948 }
949
950 /**
951  * txgbe_reset_pipeline_raptor - perform pipeline reset
952  *
953  *  @hw: pointer to hardware structure
954  *
955  * Reset pipeline by asserting Restart_AN together with LMS change to ensure
956  * full pipeline reset.  This function assumes the SW/FW lock is held.
957  **/
958 s32 txgbe_reset_pipeline_raptor(struct txgbe_hw *hw)
959 {
960         s32 err = 0;
961         u64 autoc;
962
963         autoc = hw->mac.autoc_read(hw);
964
965         /* Enable link if disabled in NVM */
966         if (autoc & TXGBE_AUTOC_LINK_DIA_MASK)
967                 autoc &= ~TXGBE_AUTOC_LINK_DIA_MASK;
968
969         autoc |= TXGBE_AUTOC_AN_RESTART;
970         /* Write AUTOC register with toggled LMS[2] bit and Restart_AN */
971         hw->mac.autoc_write(hw, autoc ^ TXGBE_AUTOC_LMS_AN);
972
973         /* Write AUTOC register with original LMS field and Restart_AN */
974         hw->mac.autoc_write(hw, autoc);
975         txgbe_flush(hw);
976
977         return err;
978 }
979