net/txgbe: add multi-speed link setup
[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_setup_mac_link_multispeed_fiber - Set MAC link speed
255  *  @hw: pointer to hardware structure
256  *  @speed: new link speed
257  *  @autoneg_wait_to_complete: true when waiting for completion is needed
258  *
259  *  Set the link speed in the MAC and/or PHY register and restarts link.
260  **/
261 s32 txgbe_setup_mac_link_multispeed_fiber(struct txgbe_hw *hw,
262                                           u32 speed,
263                                           bool autoneg_wait_to_complete)
264 {
265         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
266         u32 highest_link_speed = TXGBE_LINK_SPEED_UNKNOWN;
267         s32 status = 0;
268         u32 speedcnt = 0;
269         u32 i = 0;
270         bool autoneg, link_up = false;
271
272         DEBUGFUNC("txgbe_setup_mac_link_multispeed_fiber");
273
274         /* Mask off requested but non-supported speeds */
275         status = hw->mac.get_link_capabilities(hw, &link_speed, &autoneg);
276         if (status != 0)
277                 return status;
278
279         speed &= link_speed;
280
281         /* Try each speed one by one, highest priority first.  We do this in
282          * software because 10Gb fiber doesn't support speed autonegotiation.
283          */
284         if (speed & TXGBE_LINK_SPEED_10GB_FULL) {
285                 speedcnt++;
286                 highest_link_speed = TXGBE_LINK_SPEED_10GB_FULL;
287
288                 /* Set the module link speed */
289                 switch (hw->phy.media_type) {
290                 case txgbe_media_type_fiber:
291                         hw->mac.set_rate_select_speed(hw,
292                                 TXGBE_LINK_SPEED_10GB_FULL);
293                         break;
294                 case txgbe_media_type_fiber_qsfp:
295                         /* QSFP module automatically detects MAC link speed */
296                         break;
297                 default:
298                         DEBUGOUT("Unexpected media type.\n");
299                         break;
300                 }
301
302                 /* Allow module to change analog characteristics (1G->10G) */
303                 msec_delay(40);
304
305                 status = hw->mac.setup_mac_link(hw,
306                                 TXGBE_LINK_SPEED_10GB_FULL,
307                                 autoneg_wait_to_complete);
308                 if (status != 0)
309                         return status;
310
311                 /* Flap the Tx laser if it has not already been done */
312                 hw->mac.flap_tx_laser(hw);
313
314                 /* Wait for the controller to acquire link.  Per IEEE 802.3ap,
315                  * Section 73.10.2, we may have to wait up to 500ms if KR is
316                  * attempted.  uses the same timing for 10g SFI.
317                  */
318                 for (i = 0; i < 5; i++) {
319                         /* Wait for the link partner to also set speed */
320                         msec_delay(100);
321
322                         /* If we have link, just jump out */
323                         status = hw->mac.check_link(hw, &link_speed,
324                                 &link_up, false);
325                         if (status != 0)
326                                 return status;
327
328                         if (link_up)
329                                 goto out;
330                 }
331         }
332
333         if (speed & TXGBE_LINK_SPEED_1GB_FULL) {
334                 speedcnt++;
335                 if (highest_link_speed == TXGBE_LINK_SPEED_UNKNOWN)
336                         highest_link_speed = TXGBE_LINK_SPEED_1GB_FULL;
337
338                 /* Set the module link speed */
339                 switch (hw->phy.media_type) {
340                 case txgbe_media_type_fiber:
341                         hw->mac.set_rate_select_speed(hw,
342                                 TXGBE_LINK_SPEED_1GB_FULL);
343                         break;
344                 case txgbe_media_type_fiber_qsfp:
345                         /* QSFP module automatically detects link speed */
346                         break;
347                 default:
348                         DEBUGOUT("Unexpected media type.\n");
349                         break;
350                 }
351
352                 /* Allow module to change analog characteristics (10G->1G) */
353                 msec_delay(40);
354
355                 status = hw->mac.setup_mac_link(hw,
356                                 TXGBE_LINK_SPEED_1GB_FULL,
357                                 autoneg_wait_to_complete);
358                 if (status != 0)
359                         return status;
360
361                 /* Flap the Tx laser if it has not already been done */
362                 hw->mac.flap_tx_laser(hw);
363
364                 /* Wait for the link partner to also set speed */
365                 msec_delay(100);
366
367                 /* If we have link, just jump out */
368                 status = hw->mac.check_link(hw, &link_speed, &link_up, false);
369                 if (status != 0)
370                         return status;
371
372                 if (link_up)
373                         goto out;
374         }
375
376         /* We didn't get link.  Configure back to the highest speed we tried,
377          * (if there was more than one).  We call ourselves back with just the
378          * single highest speed that the user requested.
379          */
380         if (speedcnt > 1)
381                 status = txgbe_setup_mac_link_multispeed_fiber(hw,
382                                                       highest_link_speed,
383                                                       autoneg_wait_to_complete);
384
385 out:
386         /* Set autoneg_advertised value based on input link speed */
387         hw->phy.autoneg_advertised = 0;
388
389         if (speed & TXGBE_LINK_SPEED_10GB_FULL)
390                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_10GB_FULL;
391
392         if (speed & TXGBE_LINK_SPEED_1GB_FULL)
393                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_1GB_FULL;
394
395         return status;
396 }
397
398 /**
399  *  txgbe_init_shared_code - Initialize the shared code
400  *  @hw: pointer to hardware structure
401  *
402  *  This will assign function pointers and assign the MAC type and PHY code.
403  *  Does not touch the hardware. This function must be called prior to any
404  *  other function in the shared code. The txgbe_hw structure should be
405  *  memset to 0 prior to calling this function.  The following fields in
406  *  hw structure should be filled in prior to calling this function:
407  *  hw_addr, back, device_id, vendor_id, subsystem_device_id,
408  *  subsystem_vendor_id, and revision_id
409  **/
410 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
411 {
412         s32 status;
413
414         DEBUGFUNC("txgbe_init_shared_code");
415
416         /*
417          * Set the mac type
418          */
419         txgbe_set_mac_type(hw);
420
421         txgbe_init_ops_dummy(hw);
422         switch (hw->mac.type) {
423         case txgbe_mac_raptor:
424                 status = txgbe_init_ops_pf(hw);
425                 break;
426         default:
427                 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
428                 break;
429         }
430         hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
431
432         hw->bus.set_lan_id(hw);
433
434         return status;
435 }
436
437 /**
438  *  txgbe_set_mac_type - Sets MAC type
439  *  @hw: pointer to the HW structure
440  *
441  *  This function sets the mac type of the adapter based on the
442  *  vendor ID and device ID stored in the hw structure.
443  **/
444 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
445 {
446         s32 err = 0;
447
448         DEBUGFUNC("txgbe_set_mac_type");
449
450         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
451                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
452                 return TXGBE_ERR_DEVICE_NOT_SUPPORTED;
453         }
454
455         switch (hw->device_id) {
456         case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
457                 hw->phy.media_type = txgbe_media_type_backplane;
458                 hw->mac.type = txgbe_mac_raptor;
459                 break;
460         case TXGBE_DEV_ID_RAPTOR_XAUI:
461         case TXGBE_DEV_ID_RAPTOR_SGMII:
462                 hw->phy.media_type = txgbe_media_type_copper;
463                 hw->mac.type = txgbe_mac_raptor;
464                 break;
465         case TXGBE_DEV_ID_RAPTOR_SFP:
466         case TXGBE_DEV_ID_WX1820_SFP:
467                 hw->phy.media_type = txgbe_media_type_fiber;
468                 hw->mac.type = txgbe_mac_raptor;
469                 break;
470         case TXGBE_DEV_ID_RAPTOR_QSFP:
471                 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
472                 hw->mac.type = txgbe_mac_raptor;
473                 break;
474         case TXGBE_DEV_ID_RAPTOR_VF:
475         case TXGBE_DEV_ID_RAPTOR_VF_HV:
476                 hw->phy.media_type = txgbe_media_type_virtual;
477                 hw->mac.type = txgbe_mac_raptor_vf;
478                 break;
479         default:
480                 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
481                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
482                 break;
483         }
484
485         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
486                   hw->mac.type, hw->phy.media_type, err);
487         return err;
488 }
489
490 void txgbe_init_mac_link_ops(struct txgbe_hw *hw)
491 {
492         struct txgbe_mac_info *mac = &hw->mac;
493
494         DEBUGFUNC("txgbe_init_mac_link_ops");
495
496         /*
497          * enable the laser control functions for SFP+ fiber
498          * and MNG not enabled
499          */
500         if (hw->phy.media_type == txgbe_media_type_fiber &&
501             !txgbe_mng_enabled(hw)) {
502                 mac->disable_tx_laser =
503                         txgbe_disable_tx_laser_multispeed_fiber;
504                 mac->enable_tx_laser =
505                         txgbe_enable_tx_laser_multispeed_fiber;
506                 mac->flap_tx_laser =
507                         txgbe_flap_tx_laser_multispeed_fiber;
508         }
509
510         if ((hw->phy.media_type == txgbe_media_type_fiber ||
511              hw->phy.media_type == txgbe_media_type_fiber_qsfp) &&
512             hw->phy.multispeed_fiber) {
513                 /* Set up dual speed SFP+ support */
514                 mac->setup_link = txgbe_setup_mac_link_multispeed_fiber;
515                 mac->setup_mac_link = txgbe_setup_mac_link;
516                 mac->set_rate_select_speed = txgbe_set_hard_rate_select_speed;
517         } else if ((hw->phy.media_type == txgbe_media_type_backplane) &&
518                     (hw->phy.smart_speed == txgbe_smart_speed_auto ||
519                      hw->phy.smart_speed == txgbe_smart_speed_on) &&
520                      !txgbe_verify_lesm_fw_enabled_raptor(hw)) {
521                 mac->setup_link = txgbe_setup_mac_link_smartspeed;
522         } else {
523                 mac->setup_link = txgbe_setup_mac_link;
524         }
525 }
526
527 /**
528  *  txgbe_init_phy_raptor - PHY/SFP specific init
529  *  @hw: pointer to hardware structure
530  *
531  *  Initialize any function pointers that were not able to be
532  *  set during init_shared_code because the PHY/SFP type was
533  *  not known.  Perform the SFP init if necessary.
534  *
535  **/
536 s32 txgbe_init_phy_raptor(struct txgbe_hw *hw)
537 {
538         struct txgbe_mac_info *mac = &hw->mac;
539         struct txgbe_phy_info *phy = &hw->phy;
540         s32 err = 0;
541
542         DEBUGFUNC("txgbe_init_phy_raptor");
543
544         if (hw->device_id == TXGBE_DEV_ID_RAPTOR_QSFP) {
545                 /* Store flag indicating I2C bus access control unit. */
546                 hw->phy.qsfp_shared_i2c_bus = TRUE;
547
548                 /* Initialize access to QSFP+ I2C bus */
549                 txgbe_flush(hw);
550         }
551
552         /* Identify the PHY or SFP module */
553         err = phy->identify(hw);
554         if (err == TXGBE_ERR_SFP_NOT_SUPPORTED)
555                 goto init_phy_ops_out;
556
557         /* Setup function pointers based on detected SFP module and speeds */
558         txgbe_init_mac_link_ops(hw);
559
560         /* If copper media, overwrite with copper function pointers */
561         if (phy->media_type == txgbe_media_type_copper) {
562                 mac->setup_link = txgbe_setup_copper_link_raptor;
563                 mac->get_link_capabilities =
564                                   txgbe_get_copper_link_capabilities;
565         }
566
567         /* Set necessary function pointers based on PHY type */
568         switch (hw->phy.type) {
569         case txgbe_phy_tn:
570                 phy->setup_link = txgbe_setup_phy_link_tnx;
571                 phy->check_link = txgbe_check_phy_link_tnx;
572                 break;
573         default:
574                 break;
575         }
576
577 init_phy_ops_out:
578         return err;
579 }
580
581 /**
582  *  txgbe_init_ops_pf - Inits func ptrs and MAC type
583  *  @hw: pointer to hardware structure
584  *
585  *  Initialize the function pointers and assign the MAC type.
586  *  Does not touch the hardware.
587  **/
588 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
589 {
590         struct txgbe_bus_info *bus = &hw->bus;
591         struct txgbe_mac_info *mac = &hw->mac;
592         struct txgbe_phy_info *phy = &hw->phy;
593         struct txgbe_rom_info *rom = &hw->rom;
594
595         DEBUGFUNC("txgbe_init_ops_pf");
596
597         /* BUS */
598         bus->set_lan_id = txgbe_set_lan_id_multi_port;
599
600         /* PHY */
601         phy->identify = txgbe_identify_phy;
602         phy->init = txgbe_init_phy_raptor;
603         phy->read_reg = txgbe_read_phy_reg;
604         phy->write_reg = txgbe_write_phy_reg;
605         phy->read_reg_mdi = txgbe_read_phy_reg_mdi;
606         phy->write_reg_mdi = txgbe_write_phy_reg_mdi;
607         phy->setup_link = txgbe_setup_phy_link;
608         phy->setup_link_speed = txgbe_setup_phy_link_speed;
609         phy->read_i2c_byte = txgbe_read_i2c_byte;
610         phy->write_i2c_byte = txgbe_write_i2c_byte;
611         phy->read_i2c_eeprom = txgbe_read_i2c_eeprom;
612         phy->write_i2c_eeprom = txgbe_write_i2c_eeprom;
613         phy->reset = txgbe_reset_phy;
614
615         /* MAC */
616         mac->init_hw = txgbe_init_hw;
617         mac->reset_hw = txgbe_reset_hw;
618
619         /* Link */
620         mac->get_link_capabilities = txgbe_get_link_capabilities_raptor;
621         mac->check_link = txgbe_check_mac_link;
622
623         /* EEPROM */
624         rom->init_params = txgbe_init_eeprom_params;
625         rom->read16 = txgbe_ee_read16;
626         rom->readw_buffer = txgbe_ee_readw_buffer;
627         rom->readw_sw = txgbe_ee_readw_sw;
628         rom->read32 = txgbe_ee_read32;
629         rom->write16 = txgbe_ee_write16;
630         rom->writew_buffer = txgbe_ee_writew_buffer;
631         rom->writew_sw = txgbe_ee_writew_sw;
632         rom->write32 = txgbe_ee_write32;
633         rom->validate_checksum = txgbe_validate_eeprom_checksum;
634         rom->update_checksum = txgbe_update_eeprom_checksum;
635         rom->calc_checksum = txgbe_calc_eeprom_checksum;
636
637         mac->num_rar_entries    = TXGBE_RAPTOR_RAR_ENTRIES;
638         mac->max_rx_queues      = TXGBE_RAPTOR_MAX_RX_QUEUES;
639         mac->max_tx_queues      = TXGBE_RAPTOR_MAX_TX_QUEUES;
640
641         return 0;
642 }
643
644 /**
645  *  txgbe_get_link_capabilities_raptor - Determines link capabilities
646  *  @hw: pointer to hardware structure
647  *  @speed: pointer to link speed
648  *  @autoneg: true when autoneg or autotry is enabled
649  *
650  *  Determines the link capabilities by reading the AUTOC register.
651  **/
652 s32 txgbe_get_link_capabilities_raptor(struct txgbe_hw *hw,
653                                       u32 *speed,
654                                       bool *autoneg)
655 {
656         s32 status = 0;
657         u32 autoc = 0;
658
659         DEBUGFUNC("txgbe_get_link_capabilities_raptor");
660
661         /* Check if 1G SFP module. */
662         if (hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core0 ||
663             hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core1 ||
664             hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core0 ||
665             hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core1 ||
666             hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core0 ||
667             hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core1) {
668                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
669                 *autoneg = true;
670                 return 0;
671         }
672
673         /*
674          * Determine link capabilities based on the stored value of AUTOC,
675          * which represents EEPROM defaults.  If AUTOC value has not
676          * been stored, use the current register values.
677          */
678         if (hw->mac.orig_link_settings_stored)
679                 autoc = hw->mac.orig_autoc;
680         else
681                 autoc = hw->mac.autoc_read(hw);
682
683         switch (autoc & TXGBE_AUTOC_LMS_MASK) {
684         case TXGBE_AUTOC_LMS_1G_LINK_NO_AN:
685                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
686                 *autoneg = false;
687                 break;
688
689         case TXGBE_AUTOC_LMS_10G_LINK_NO_AN:
690                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
691                 *autoneg = false;
692                 break;
693
694         case TXGBE_AUTOC_LMS_1G_AN:
695                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
696                 *autoneg = true;
697                 break;
698
699         case TXGBE_AUTOC_LMS_10G:
700                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
701                 *autoneg = false;
702                 break;
703
704         case TXGBE_AUTOC_LMS_KX4_KX_KR:
705         case TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN:
706                 *speed = TXGBE_LINK_SPEED_UNKNOWN;
707                 if (autoc & TXGBE_AUTOC_KR_SUPP)
708                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
709                 if (autoc & TXGBE_AUTOC_KX4_SUPP)
710                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
711                 if (autoc & TXGBE_AUTOC_KX_SUPP)
712                         *speed |= TXGBE_LINK_SPEED_1GB_FULL;
713                 *autoneg = true;
714                 break;
715
716         case TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII:
717                 *speed = TXGBE_LINK_SPEED_100M_FULL;
718                 if (autoc & TXGBE_AUTOC_KR_SUPP)
719                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
720                 if (autoc & TXGBE_AUTOC_KX4_SUPP)
721                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
722                 if (autoc & TXGBE_AUTOC_KX_SUPP)
723                         *speed |= TXGBE_LINK_SPEED_1GB_FULL;
724                 *autoneg = true;
725                 break;
726
727         case TXGBE_AUTOC_LMS_SGMII_1G_100M:
728                 *speed = TXGBE_LINK_SPEED_1GB_FULL |
729                          TXGBE_LINK_SPEED_100M_FULL |
730                          TXGBE_LINK_SPEED_10M_FULL;
731                 *autoneg = false;
732                 break;
733
734         default:
735                 return TXGBE_ERR_LINK_SETUP;
736         }
737
738         if (hw->phy.multispeed_fiber) {
739                 *speed |= TXGBE_LINK_SPEED_10GB_FULL |
740                           TXGBE_LINK_SPEED_1GB_FULL;
741
742                 /* QSFP must not enable full auto-negotiation
743                  * Limited autoneg is enabled at 1G
744                  */
745                 if (hw->phy.media_type == txgbe_media_type_fiber_qsfp)
746                         *autoneg = false;
747                 else
748                         *autoneg = true;
749         }
750
751         return status;
752 }
753
754 /**
755  *  txgbe_start_mac_link_raptor - Setup MAC link settings
756  *  @hw: pointer to hardware structure
757  *  @autoneg_wait_to_complete: true when waiting for completion is needed
758  *
759  *  Configures link settings based on values in the txgbe_hw struct.
760  *  Restarts the link.  Performs autonegotiation if needed.
761  **/
762 s32 txgbe_start_mac_link_raptor(struct txgbe_hw *hw,
763                                bool autoneg_wait_to_complete)
764 {
765         s32 status = 0;
766         bool got_lock = false;
767
768         DEBUGFUNC("txgbe_start_mac_link_raptor");
769
770         UNREFERENCED_PARAMETER(autoneg_wait_to_complete);
771
772         /*  reset_pipeline requires us to hold this lock as it writes to
773          *  AUTOC.
774          */
775         if (txgbe_verify_lesm_fw_enabled_raptor(hw)) {
776                 status = hw->mac.acquire_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
777                 if (status != 0)
778                         goto out;
779
780                 got_lock = true;
781         }
782
783         /* Restart link */
784         txgbe_reset_pipeline_raptor(hw);
785
786         if (got_lock)
787                 hw->mac.release_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
788
789         /* Add delay to filter out noises during initial link setup */
790         msec_delay(50);
791
792 out:
793         return status;
794 }
795
796 /**
797  *  txgbe_disable_tx_laser_multispeed_fiber - Disable Tx laser
798  *  @hw: pointer to hardware structure
799  *
800  *  The base drivers may require better control over SFP+ module
801  *  PHY states.  This includes selectively shutting down the Tx
802  *  laser on the PHY, effectively halting physical link.
803  **/
804 void txgbe_disable_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
805 {
806         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
807
808         /* Blocked by MNG FW so bail */
809         if (txgbe_check_reset_blocked(hw))
810                 return;
811
812         /* Disable Tx laser; allow 100us to go dark per spec */
813         esdp_reg |= (TXGBE_GPIOBIT_0 | TXGBE_GPIOBIT_1);
814         wr32(hw, TXGBE_GPIODATA, esdp_reg);
815         txgbe_flush(hw);
816         usec_delay(100);
817 }
818
819 /**
820  *  txgbe_enable_tx_laser_multispeed_fiber - Enable Tx laser
821  *  @hw: pointer to hardware structure
822  *
823  *  The base drivers may require better control over SFP+ module
824  *  PHY states.  This includes selectively turning on the Tx
825  *  laser on the PHY, effectively starting physical link.
826  **/
827 void txgbe_enable_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
828 {
829         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
830
831         /* Enable Tx laser; allow 100ms to light up */
832         esdp_reg &= ~(TXGBE_GPIOBIT_0 | TXGBE_GPIOBIT_1);
833         wr32(hw, TXGBE_GPIODATA, esdp_reg);
834         txgbe_flush(hw);
835         msec_delay(100);
836 }
837
838 /**
839  *  txgbe_flap_tx_laser_multispeed_fiber - Flap Tx laser
840  *  @hw: pointer to hardware structure
841  *
842  *  When the driver changes the link speeds that it can support,
843  *  it sets autotry_restart to true to indicate that we need to
844  *  initiate a new autotry session with the link partner.  To do
845  *  so, we set the speed then disable and re-enable the Tx laser, to
846  *  alert the link partner that it also needs to restart autotry on its
847  *  end.  This is consistent with true clause 37 autoneg, which also
848  *  involves a loss of signal.
849  **/
850 void txgbe_flap_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
851 {
852         DEBUGFUNC("txgbe_flap_tx_laser_multispeed_fiber");
853
854         /* Blocked by MNG FW so bail */
855         if (txgbe_check_reset_blocked(hw))
856                 return;
857
858         if (hw->mac.autotry_restart) {
859                 txgbe_disable_tx_laser_multispeed_fiber(hw);
860                 txgbe_enable_tx_laser_multispeed_fiber(hw);
861                 hw->mac.autotry_restart = false;
862         }
863 }
864
865 /**
866  *  txgbe_set_hard_rate_select_speed - Set module link speed
867  *  @hw: pointer to hardware structure
868  *  @speed: link speed to set
869  *
870  *  Set module link speed via RS0/RS1 rate select pins.
871  */
872 void txgbe_set_hard_rate_select_speed(struct txgbe_hw *hw,
873                                         u32 speed)
874 {
875         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
876
877         switch (speed) {
878         case TXGBE_LINK_SPEED_10GB_FULL:
879                 esdp_reg |= (TXGBE_GPIOBIT_4 | TXGBE_GPIOBIT_5);
880                 break;
881         case TXGBE_LINK_SPEED_1GB_FULL:
882                 esdp_reg &= ~(TXGBE_GPIOBIT_4 | TXGBE_GPIOBIT_5);
883                 break;
884         default:
885                 DEBUGOUT("Invalid fixed module speed\n");
886                 return;
887         }
888
889         wr32(hw, TXGBE_GPIODATA, esdp_reg);
890         txgbe_flush(hw);
891 }
892
893 /**
894  *  txgbe_setup_mac_link_smartspeed - Set MAC link speed using SmartSpeed
895  *  @hw: pointer to hardware structure
896  *  @speed: new link speed
897  *  @autoneg_wait_to_complete: true when waiting for completion is needed
898  *
899  *  Implements the Intel SmartSpeed algorithm.
900  **/
901 s32 txgbe_setup_mac_link_smartspeed(struct txgbe_hw *hw,
902                                     u32 speed,
903                                     bool autoneg_wait_to_complete)
904 {
905         s32 status = 0;
906         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
907         s32 i, j;
908         bool link_up = false;
909         u32 autoc_reg = rd32_epcs(hw, SR_AN_MMD_ADV_REG1);
910
911         DEBUGFUNC("txgbe_setup_mac_link_smartspeed");
912
913          /* Set autoneg_advertised value based on input link speed */
914         hw->phy.autoneg_advertised = 0;
915
916         if (speed & TXGBE_LINK_SPEED_10GB_FULL)
917                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_10GB_FULL;
918
919         if (speed & TXGBE_LINK_SPEED_1GB_FULL)
920                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_1GB_FULL;
921
922         if (speed & TXGBE_LINK_SPEED_100M_FULL)
923                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_100M_FULL;
924
925         /*
926          * Implement Intel SmartSpeed algorithm.  SmartSpeed will reduce the
927          * autoneg advertisement if link is unable to be established at the
928          * highest negotiated rate.  This can sometimes happen due to integrity
929          * issues with the physical media connection.
930          */
931
932         /* First, try to get link with full advertisement */
933         hw->phy.smart_speed_active = false;
934         for (j = 0; j < TXGBE_SMARTSPEED_MAX_RETRIES; j++) {
935                 status = txgbe_setup_mac_link(hw, speed,
936                                                     autoneg_wait_to_complete);
937                 if (status != 0)
938                         goto out;
939
940                 /*
941                  * Wait for the controller to acquire link.  Per IEEE 802.3ap,
942                  * Section 73.10.2, we may have to wait up to 500ms if KR is
943                  * attempted, or 200ms if KX/KX4/BX/BX4 is attempted, per
944                  * Table 9 in the AN MAS.
945                  */
946                 for (i = 0; i < 5; i++) {
947                         msec_delay(100);
948
949                         /* If we have link, just jump out */
950                         status = hw->mac.check_link(hw, &link_speed, &link_up,
951                                                   false);
952                         if (status != 0)
953                                 goto out;
954
955                         if (link_up)
956                                 goto out;
957                 }
958         }
959
960         /*
961          * We didn't get link.  If we advertised KR plus one of KX4/KX
962          * (or BX4/BX), then disable KR and try again.
963          */
964         if (((autoc_reg & TXGBE_AUTOC_KR_SUPP) == 0) ||
965             ((autoc_reg & TXGBE_AUTOC_KX_SUPP) == 0 &&
966              (autoc_reg & TXGBE_AUTOC_KX4_SUPP) == 0))
967                 goto out;
968
969         /* Turn SmartSpeed on to disable KR support */
970         hw->phy.smart_speed_active = true;
971         status = txgbe_setup_mac_link(hw, speed,
972                                             autoneg_wait_to_complete);
973         if (status != 0)
974                 goto out;
975
976         /*
977          * Wait for the controller to acquire link.  600ms will allow for
978          * the AN link_fail_inhibit_timer as well for multiple cycles of
979          * parallel detect, both 10g and 1g. This allows for the maximum
980          * connect attempts as defined in the AN MAS table 73-7.
981          */
982         for (i = 0; i < 6; i++) {
983                 msec_delay(100);
984
985                 /* If we have link, just jump out */
986                 status = hw->mac.check_link(hw, &link_speed, &link_up, false);
987                 if (status != 0)
988                         goto out;
989
990                 if (link_up)
991                         goto out;
992         }
993
994         /* We didn't get link.  Turn SmartSpeed back off. */
995         hw->phy.smart_speed_active = false;
996         status = txgbe_setup_mac_link(hw, speed,
997                                             autoneg_wait_to_complete);
998
999 out:
1000         if (link_up && link_speed == TXGBE_LINK_SPEED_1GB_FULL)
1001                 DEBUGOUT("Smartspeed has downgraded the link speed "
1002                 "from the maximum advertised\n");
1003         return status;
1004 }
1005
1006 /**
1007  *  txgbe_setup_mac_link - Set MAC link speed
1008  *  @hw: pointer to hardware structure
1009  *  @speed: new link speed
1010  *  @autoneg_wait_to_complete: true when waiting for completion is needed
1011  *
1012  *  Set the link speed in the AUTOC register and restarts link.
1013  **/
1014 s32 txgbe_setup_mac_link(struct txgbe_hw *hw,
1015                                u32 speed,
1016                                bool autoneg_wait_to_complete)
1017 {
1018         bool autoneg = false;
1019         s32 status = 0;
1020
1021         u64 autoc = hw->mac.autoc_read(hw);
1022         u64 pma_pmd_10gs = autoc & TXGBE_AUTOC_10GS_PMA_PMD_MASK;
1023         u64 pma_pmd_1g = autoc & TXGBE_AUTOC_1G_PMA_PMD_MASK;
1024         u64 link_mode = autoc & TXGBE_AUTOC_LMS_MASK;
1025         u64 current_autoc = autoc;
1026         u64 orig_autoc = 0;
1027         u32 links_reg;
1028         u32 i;
1029         u32 link_capabilities = TXGBE_LINK_SPEED_UNKNOWN;
1030
1031         DEBUGFUNC("txgbe_setup_mac_link");
1032
1033         /* Check to see if speed passed in is supported. */
1034         status = hw->mac.get_link_capabilities(hw,
1035                         &link_capabilities, &autoneg);
1036         if (status)
1037                 return status;
1038
1039         speed &= link_capabilities;
1040         if (speed == TXGBE_LINK_SPEED_UNKNOWN)
1041                 return TXGBE_ERR_LINK_SETUP;
1042
1043         /* Use stored value (EEPROM defaults) of AUTOC to find KR/KX4 support*/
1044         if (hw->mac.orig_link_settings_stored)
1045                 orig_autoc = hw->mac.orig_autoc;
1046         else
1047                 orig_autoc = autoc;
1048
1049         link_mode = autoc & TXGBE_AUTOC_LMS_MASK;
1050         pma_pmd_1g = autoc & TXGBE_AUTOC_1G_PMA_PMD_MASK;
1051
1052         if (link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR ||
1053             link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
1054             link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
1055                 /* Set KX4/KX/KR support according to speed requested */
1056                 autoc &= ~(TXGBE_AUTOC_KX_SUPP |
1057                            TXGBE_AUTOC_KX4_SUPP |
1058                            TXGBE_AUTOC_KR_SUPP);
1059                 if (speed & TXGBE_LINK_SPEED_10GB_FULL) {
1060                         if (orig_autoc & TXGBE_AUTOC_KX4_SUPP)
1061                                 autoc |= TXGBE_AUTOC_KX4_SUPP;
1062                         if ((orig_autoc & TXGBE_AUTOC_KR_SUPP) &&
1063                             !hw->phy.smart_speed_active)
1064                                 autoc |= TXGBE_AUTOC_KR_SUPP;
1065                 }
1066                 if (speed & TXGBE_LINK_SPEED_1GB_FULL)
1067                         autoc |= TXGBE_AUTOC_KX_SUPP;
1068         } else if ((pma_pmd_1g == TXGBE_AUTOC_1G_SFI) &&
1069                    (link_mode == TXGBE_AUTOC_LMS_1G_LINK_NO_AN ||
1070                     link_mode == TXGBE_AUTOC_LMS_1G_AN)) {
1071                 /* Switch from 1G SFI to 10G SFI if requested */
1072                 if (speed == TXGBE_LINK_SPEED_10GB_FULL &&
1073                     pma_pmd_10gs == TXGBE_AUTOC_10GS_SFI) {
1074                         autoc &= ~TXGBE_AUTOC_LMS_MASK;
1075                         autoc |= TXGBE_AUTOC_LMS_10G;
1076                 }
1077         } else if ((pma_pmd_10gs == TXGBE_AUTOC_10GS_SFI) &&
1078                    (link_mode == TXGBE_AUTOC_LMS_10G)) {
1079                 /* Switch from 10G SFI to 1G SFI if requested */
1080                 if (speed == TXGBE_LINK_SPEED_1GB_FULL &&
1081                     pma_pmd_1g == TXGBE_AUTOC_1G_SFI) {
1082                         autoc &= ~TXGBE_AUTOC_LMS_MASK;
1083                         if (autoneg || hw->phy.type == txgbe_phy_qsfp_intel)
1084                                 autoc |= TXGBE_AUTOC_LMS_1G_AN;
1085                         else
1086                                 autoc |= TXGBE_AUTOC_LMS_1G_LINK_NO_AN;
1087                 }
1088         }
1089
1090         if (autoc == current_autoc)
1091                 return status;
1092
1093         autoc &= ~TXGBE_AUTOC_SPEED_MASK;
1094         autoc |= TXGBE_AUTOC_SPEED(speed);
1095         autoc |= (autoneg ? TXGBE_AUTOC_AUTONEG : 0);
1096
1097         /* Restart link */
1098         hw->mac.autoc_write(hw, autoc);
1099
1100         /* Only poll for autoneg to complete if specified to do so */
1101         if (autoneg_wait_to_complete) {
1102                 if (link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR ||
1103                     link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
1104                     link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
1105                         links_reg = 0; /*Just in case Autoneg time=0*/
1106                         for (i = 0; i < TXGBE_AUTO_NEG_TIME; i++) {
1107                                 links_reg = rd32(hw, TXGBE_PORTSTAT);
1108                                 if (links_reg & TXGBE_PORTSTAT_UP)
1109                                         break;
1110                                 msec_delay(100);
1111                         }
1112                         if (!(links_reg & TXGBE_PORTSTAT_UP)) {
1113                                 status = TXGBE_ERR_AUTONEG_NOT_COMPLETE;
1114                                 DEBUGOUT("Autoneg did not complete.\n");
1115                         }
1116                 }
1117         }
1118
1119         /* Add delay to filter out noises during initial link setup */
1120         msec_delay(50);
1121
1122         return status;
1123 }
1124
1125 /**
1126  *  txgbe_setup_copper_link_raptor - Set the PHY autoneg advertised field
1127  *  @hw: pointer to hardware structure
1128  *  @speed: new link speed
1129  *  @autoneg_wait_to_complete: true if waiting is needed to complete
1130  *
1131  *  Restarts link on PHY and MAC based on settings passed in.
1132  **/
1133 static s32 txgbe_setup_copper_link_raptor(struct txgbe_hw *hw,
1134                                          u32 speed,
1135                                          bool autoneg_wait_to_complete)
1136 {
1137         s32 status;
1138
1139         DEBUGFUNC("txgbe_setup_copper_link_raptor");
1140
1141         /* Setup the PHY according to input speed */
1142         status = hw->phy.setup_link_speed(hw, speed,
1143                                               autoneg_wait_to_complete);
1144         /* Set up MAC */
1145         txgbe_start_mac_link_raptor(hw, autoneg_wait_to_complete);
1146
1147         return status;
1148 }
1149
1150 static int
1151 txgbe_check_flash_load(struct txgbe_hw *hw, u32 check_bit)
1152 {
1153         u32 reg = 0;
1154         u32 i;
1155         int err = 0;
1156         /* if there's flash existing */
1157         if (!(rd32(hw, TXGBE_SPISTAT) & TXGBE_SPISTAT_BPFLASH)) {
1158                 /* wait hw load flash done */
1159                 for (i = 0; i < 10; i++) {
1160                         reg = rd32(hw, TXGBE_ILDRSTAT);
1161                         if (!(reg & check_bit)) {
1162                                 /* done */
1163                                 break;
1164                         }
1165                         msleep(100);
1166                 }
1167                 if (i == 10)
1168                         err = TXGBE_ERR_FLASH_LOADING_FAILED;
1169         }
1170         return err;
1171 }
1172
1173 /**
1174  *  txgbe_reset_hw - Perform hardware reset
1175  *  @hw: pointer to hardware structure
1176  *
1177  *  Resets the hardware by resetting the transmit and receive units, masks
1178  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
1179  *  reset.
1180  **/
1181 s32 txgbe_reset_hw(struct txgbe_hw *hw)
1182 {
1183         s32 status;
1184         u32 autoc;
1185
1186         DEBUGFUNC("txgbe_reset_hw");
1187
1188         /* Call adapter stop to disable tx/rx and clear interrupts */
1189         status = hw->mac.stop_hw(hw);
1190         if (status != 0)
1191                 return status;
1192
1193         /* flush pending Tx transactions */
1194         txgbe_clear_tx_pending(hw);
1195
1196         /* Identify PHY and related function pointers */
1197         status = hw->phy.init(hw);
1198         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
1199                 return status;
1200
1201         /* Setup SFP module if there is one present. */
1202         if (hw->phy.sfp_setup_needed) {
1203                 status = hw->mac.setup_sfp(hw);
1204                 hw->phy.sfp_setup_needed = false;
1205         }
1206         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
1207                 return status;
1208
1209         /* Reset PHY */
1210         if (!hw->phy.reset_disable)
1211                 hw->phy.reset(hw);
1212
1213         /* remember AUTOC from before we reset */
1214         autoc = hw->mac.autoc_read(hw);
1215
1216 mac_reset_top:
1217         /*
1218          * Issue global reset to the MAC.  Needs to be SW reset if link is up.
1219          * If link reset is used when link is up, it might reset the PHY when
1220          * mng is using it.  If link is down or the flag to force full link
1221          * reset is set, then perform link reset.
1222          */
1223         if (txgbe_mng_present(hw)) {
1224                 txgbe_hic_reset(hw);
1225         } else {
1226                 wr32(hw, TXGBE_RST, TXGBE_RST_LAN(hw->bus.lan_id));
1227                 txgbe_flush(hw);
1228         }
1229         usec_delay(10);
1230
1231         if (hw->bus.lan_id == 0) {
1232                 status = txgbe_check_flash_load(hw,
1233                                 TXGBE_ILDRSTAT_SWRST_LAN0);
1234         } else {
1235                 status = txgbe_check_flash_load(hw,
1236                                 TXGBE_ILDRSTAT_SWRST_LAN1);
1237         }
1238         if (status != 0)
1239                 return status;
1240
1241         msec_delay(50);
1242
1243         /*
1244          * Double resets are required for recovery from certain error
1245          * conditions.  Between resets, it is necessary to stall to
1246          * allow time for any pending HW events to complete.
1247          */
1248         if (hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
1249                 hw->mac.flags &= ~TXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
1250                 goto mac_reset_top;
1251         }
1252
1253         /*
1254          * Store the original AUTOC/AUTOC2 values if they have not been
1255          * stored off yet.  Otherwise restore the stored original
1256          * values since the reset operation sets back to defaults.
1257          */
1258         if (!hw->mac.orig_link_settings_stored) {
1259                 hw->mac.orig_autoc = hw->mac.autoc_read(hw);
1260                 hw->mac.autoc_write(hw, hw->mac.orig_autoc);
1261                 hw->mac.orig_link_settings_stored = true;
1262         } else {
1263                 hw->mac.orig_autoc = autoc;
1264         }
1265
1266         /* Store the permanent mac address */
1267         hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
1268
1269         /*
1270          * Store MAC address from RAR0, clear receive address registers, and
1271          * clear the multicast table.  Also reset num_rar_entries to 128,
1272          * since we modify this value when programming the SAN MAC address.
1273          */
1274         hw->mac.num_rar_entries = 128;
1275         hw->mac.init_rx_addrs(hw);
1276
1277         /* Store the permanent SAN mac address */
1278         hw->mac.get_san_mac_addr(hw, hw->mac.san_addr);
1279
1280         /* Add the SAN MAC address to the RAR only if it's a valid address */
1281         if (txgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
1282                 /* Save the SAN MAC RAR index */
1283                 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
1284
1285                 hw->mac.set_rar(hw, hw->mac.san_mac_rar_index,
1286                                     hw->mac.san_addr, 0, true);
1287
1288                 /* clear VMDq pool/queue selection for this RAR */
1289                 hw->mac.clear_vmdq(hw, hw->mac.san_mac_rar_index,
1290                                        BIT_MASK32);
1291
1292                 /* Reserve the last RAR for the SAN MAC address */
1293                 hw->mac.num_rar_entries--;
1294         }
1295
1296         /* Store the alternative WWNN/WWPN prefix */
1297         hw->mac.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
1298                                    &hw->mac.wwpn_prefix);
1299
1300         return status;
1301 }
1302
1303 /**
1304  *  txgbe_verify_lesm_fw_enabled_raptor - Checks LESM FW module state.
1305  *  @hw: pointer to hardware structure
1306  *
1307  *  Returns true if the LESM FW module is present and enabled. Otherwise
1308  *  returns false. Smart Speed must be disabled if LESM FW module is enabled.
1309  **/
1310 bool txgbe_verify_lesm_fw_enabled_raptor(struct txgbe_hw *hw)
1311 {
1312         bool lesm_enabled = false;
1313         u16 fw_offset, fw_lesm_param_offset, fw_lesm_state;
1314         s32 status;
1315
1316         DEBUGFUNC("txgbe_verify_lesm_fw_enabled_raptor");
1317
1318         /* get the offset to the Firmware Module block */
1319         status = hw->rom.read16(hw, TXGBE_FW_PTR, &fw_offset);
1320
1321         if (status != 0 || fw_offset == 0 || fw_offset == 0xFFFF)
1322                 goto out;
1323
1324         /* get the offset to the LESM Parameters block */
1325         status = hw->rom.read16(hw, (fw_offset +
1326                                      TXGBE_FW_LESM_PARAMETERS_PTR),
1327                                      &fw_lesm_param_offset);
1328
1329         if (status != 0 ||
1330             fw_lesm_param_offset == 0 || fw_lesm_param_offset == 0xFFFF)
1331                 goto out;
1332
1333         /* get the LESM state word */
1334         status = hw->rom.read16(hw, (fw_lesm_param_offset +
1335                                      TXGBE_FW_LESM_STATE_1),
1336                                      &fw_lesm_state);
1337
1338         if (status == 0 && (fw_lesm_state & TXGBE_FW_LESM_STATE_ENABLED))
1339                 lesm_enabled = true;
1340
1341 out:
1342         lesm_enabled = false;
1343         return lesm_enabled;
1344 }
1345
1346 /**
1347  * txgbe_reset_pipeline_raptor - perform pipeline reset
1348  *
1349  *  @hw: pointer to hardware structure
1350  *
1351  * Reset pipeline by asserting Restart_AN together with LMS change to ensure
1352  * full pipeline reset.  This function assumes the SW/FW lock is held.
1353  **/
1354 s32 txgbe_reset_pipeline_raptor(struct txgbe_hw *hw)
1355 {
1356         s32 err = 0;
1357         u64 autoc;
1358
1359         autoc = hw->mac.autoc_read(hw);
1360
1361         /* Enable link if disabled in NVM */
1362         if (autoc & TXGBE_AUTOC_LINK_DIA_MASK)
1363                 autoc &= ~TXGBE_AUTOC_LINK_DIA_MASK;
1364
1365         autoc |= TXGBE_AUTOC_AN_RESTART;
1366         /* Write AUTOC register with toggled LMS[2] bit and Restart_AN */
1367         hw->mac.autoc_write(hw, autoc ^ TXGBE_AUTOC_LMS_AN);
1368
1369         /* Write AUTOC register with original LMS field and Restart_AN */
1370         hw->mac.autoc_write(hw, autoc);
1371         txgbe_flush(hw);
1372
1373         return err;
1374 }
1375