net/txgbe: support flow control
[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_mbx.h"
7 #include "txgbe_phy.h"
8 #include "txgbe_dcb.h"
9 #include "txgbe_eeprom.h"
10 #include "txgbe_mng.h"
11 #include "txgbe_hw.h"
12
13 #define TXGBE_RAPTOR_MAX_TX_QUEUES 128
14 #define TXGBE_RAPTOR_MAX_RX_QUEUES 128
15 #define TXGBE_RAPTOR_RAR_ENTRIES   128
16 #define TXGBE_RAPTOR_MC_TBL_SIZE   128
17 #define TXGBE_RAPTOR_VFT_TBL_SIZE  128
18 #define TXGBE_RAPTOR_RX_PB_SIZE   512 /*KB*/
19
20 static s32 txgbe_setup_copper_link_raptor(struct txgbe_hw *hw,
21                                          u32 speed,
22                                          bool autoneg_wait_to_complete);
23
24 static s32 txgbe_mta_vector(struct txgbe_hw *hw, u8 *mc_addr);
25 static s32 txgbe_get_san_mac_addr_offset(struct txgbe_hw *hw,
26                                          u16 *san_mac_offset);
27
28 /**
29  * txgbe_device_supports_autoneg_fc - Check if device supports autonegotiation
30  * of flow control
31  * @hw: pointer to hardware structure
32  *
33  * This function returns true if the device supports flow control
34  * autonegotiation, and false if it does not.
35  *
36  **/
37 bool txgbe_device_supports_autoneg_fc(struct txgbe_hw *hw)
38 {
39         bool supported = false;
40         u32 speed;
41         bool link_up;
42
43         DEBUGFUNC("txgbe_device_supports_autoneg_fc");
44
45         switch (hw->phy.media_type) {
46         case txgbe_media_type_fiber_qsfp:
47         case txgbe_media_type_fiber:
48                 hw->mac.check_link(hw, &speed, &link_up, false);
49                 /* if link is down, assume supported */
50                 if (link_up)
51                         supported = speed == TXGBE_LINK_SPEED_1GB_FULL ?
52                         true : false;
53                 else
54                         supported = true;
55
56                 break;
57         case txgbe_media_type_backplane:
58                 supported = true;
59                 break;
60         case txgbe_media_type_copper:
61                 /* only some copper devices support flow control autoneg */
62                 switch (hw->device_id) {
63                 case TXGBE_DEV_ID_RAPTOR_XAUI:
64                 case TXGBE_DEV_ID_RAPTOR_SGMII:
65                         supported = true;
66                         break;
67                 default:
68                         supported = false;
69                 }
70         default:
71                 break;
72         }
73
74         if (!supported)
75                 DEBUGOUT("Device %x does not support flow control autoneg",
76                               hw->device_id);
77         return supported;
78 }
79
80 /**
81  *  txgbe_setup_fc - Set up flow control
82  *  @hw: pointer to hardware structure
83  *
84  *  Called at init time to set up flow control.
85  **/
86 s32 txgbe_setup_fc(struct txgbe_hw *hw)
87 {
88         s32 err = 0;
89         u32 reg = 0;
90         u16 reg_cu = 0;
91         u32 value = 0;
92         u64 reg_bp = 0;
93         bool locked = false;
94
95         DEBUGFUNC("txgbe_setup_fc");
96
97         /* Validate the requested mode */
98         if (hw->fc.strict_ieee && hw->fc.requested_mode == txgbe_fc_rx_pause) {
99                 DEBUGOUT("txgbe_fc_rx_pause not valid in strict IEEE mode\n");
100                 err = TXGBE_ERR_INVALID_LINK_SETTINGS;
101                 goto out;
102         }
103
104         /*
105          * 10gig parts do not have a word in the EEPROM to determine the
106          * default flow control setting, so we explicitly set it to full.
107          */
108         if (hw->fc.requested_mode == txgbe_fc_default)
109                 hw->fc.requested_mode = txgbe_fc_full;
110
111         /*
112          * Set up the 1G and 10G flow control advertisement registers so the
113          * HW will be able to do fc autoneg once the cable is plugged in.  If
114          * we link at 10G, the 1G advertisement is harmless and vice versa.
115          */
116         switch (hw->phy.media_type) {
117         case txgbe_media_type_backplane:
118                 /* some MAC's need RMW protection on AUTOC */
119                 err = hw->mac.prot_autoc_read(hw, &locked, &reg_bp);
120                 if (err != 0)
121                         goto out;
122
123                 /* fall through - only backplane uses autoc */
124         case txgbe_media_type_fiber_qsfp:
125         case txgbe_media_type_fiber:
126         case txgbe_media_type_copper:
127                 hw->phy.read_reg(hw, TXGBE_MD_AUTO_NEG_ADVT,
128                                      TXGBE_MD_DEV_AUTO_NEG, &reg_cu);
129                 break;
130         default:
131                 break;
132         }
133
134         /*
135          * The possible values of fc.requested_mode are:
136          * 0: Flow control is completely disabled
137          * 1: Rx flow control is enabled (we can receive pause frames,
138          *    but not send pause frames).
139          * 2: Tx flow control is enabled (we can send pause frames but
140          *    we do not support receiving pause frames).
141          * 3: Both Rx and Tx flow control (symmetric) are enabled.
142          * other: Invalid.
143          */
144         switch (hw->fc.requested_mode) {
145         case txgbe_fc_none:
146                 /* Flow control completely disabled by software override. */
147                 reg &= ~(SR_MII_MMD_AN_ADV_PAUSE_SYM |
148                         SR_MII_MMD_AN_ADV_PAUSE_ASM);
149                 if (hw->phy.media_type == txgbe_media_type_backplane)
150                         reg_bp &= ~(TXGBE_AUTOC_SYM_PAUSE |
151                                     TXGBE_AUTOC_ASM_PAUSE);
152                 else if (hw->phy.media_type == txgbe_media_type_copper)
153                         reg_cu &= ~(TXGBE_TAF_SYM_PAUSE | TXGBE_TAF_ASM_PAUSE);
154                 break;
155         case txgbe_fc_tx_pause:
156                 /*
157                  * Tx Flow control is enabled, and Rx Flow control is
158                  * disabled by software override.
159                  */
160                 reg |= SR_MII_MMD_AN_ADV_PAUSE_ASM;
161                 reg &= ~SR_MII_MMD_AN_ADV_PAUSE_SYM;
162                 if (hw->phy.media_type == txgbe_media_type_backplane) {
163                         reg_bp |= TXGBE_AUTOC_ASM_PAUSE;
164                         reg_bp &= ~TXGBE_AUTOC_SYM_PAUSE;
165                 } else if (hw->phy.media_type == txgbe_media_type_copper) {
166                         reg_cu |= TXGBE_TAF_ASM_PAUSE;
167                         reg_cu &= ~TXGBE_TAF_SYM_PAUSE;
168                 }
169                 reg |= SR_MII_MMD_AN_ADV_PAUSE_ASM;
170                 reg_bp |= SR_AN_MMD_ADV_REG1_PAUSE_ASM;
171                 break;
172         case txgbe_fc_rx_pause:
173                 /*
174                  * Rx Flow control is enabled and Tx Flow control is
175                  * disabled by software override. Since there really
176                  * isn't a way to advertise that we are capable of RX
177                  * Pause ONLY, we will advertise that we support both
178                  * symmetric and asymmetric Rx PAUSE, as such we fall
179                  * through to the fc_full statement.  Later, we will
180                  * disable the adapter's ability to send PAUSE frames.
181                  */
182         case txgbe_fc_full:
183                 /* Flow control (both Rx and Tx) is enabled by SW override. */
184                 reg |= SR_MII_MMD_AN_ADV_PAUSE_SYM |
185                         SR_MII_MMD_AN_ADV_PAUSE_ASM;
186                 if (hw->phy.media_type == txgbe_media_type_backplane)
187                         reg_bp |= TXGBE_AUTOC_SYM_PAUSE |
188                                   TXGBE_AUTOC_ASM_PAUSE;
189                 else if (hw->phy.media_type == txgbe_media_type_copper)
190                         reg_cu |= TXGBE_TAF_SYM_PAUSE | TXGBE_TAF_ASM_PAUSE;
191                 reg |= SR_MII_MMD_AN_ADV_PAUSE_SYM |
192                         SR_MII_MMD_AN_ADV_PAUSE_ASM;
193                 reg_bp |= SR_AN_MMD_ADV_REG1_PAUSE_SYM |
194                         SR_AN_MMD_ADV_REG1_PAUSE_ASM;
195                 break;
196         default:
197                 DEBUGOUT("Flow control param set incorrectly\n");
198                 err = TXGBE_ERR_CONFIG;
199                 goto out;
200         }
201
202         /*
203          * Enable auto-negotiation between the MAC & PHY;
204          * the MAC will advertise clause 37 flow control.
205          */
206         value = rd32_epcs(hw, SR_MII_MMD_AN_ADV);
207         value = (value & ~(SR_MII_MMD_AN_ADV_PAUSE_ASM |
208                 SR_MII_MMD_AN_ADV_PAUSE_SYM)) | reg;
209         wr32_epcs(hw, SR_MII_MMD_AN_ADV, value);
210
211         /*
212          * AUTOC restart handles negotiation of 1G and 10G on backplane
213          * and copper. There is no need to set the PCS1GCTL register.
214          *
215          */
216         if (hw->phy.media_type == txgbe_media_type_backplane) {
217                 value = rd32_epcs(hw, SR_AN_MMD_ADV_REG1);
218                 value = (value & ~(SR_AN_MMD_ADV_REG1_PAUSE_ASM |
219                         SR_AN_MMD_ADV_REG1_PAUSE_SYM)) |
220                         reg_bp;
221                 wr32_epcs(hw, SR_AN_MMD_ADV_REG1, value);
222         } else if ((hw->phy.media_type == txgbe_media_type_copper) &&
223                     (txgbe_device_supports_autoneg_fc(hw))) {
224                 hw->phy.write_reg(hw, TXGBE_MD_AUTO_NEG_ADVT,
225                                       TXGBE_MD_DEV_AUTO_NEG, reg_cu);
226         }
227
228         DEBUGOUT("Set up FC; reg = 0x%08X\n", reg);
229 out:
230         return err;
231 }
232
233 /**
234  *  txgbe_start_hw - Prepare hardware for Tx/Rx
235  *  @hw: pointer to hardware structure
236  *
237  *  Starts the hardware by filling the bus info structure and media type, clears
238  *  all on chip counters, initializes receive address registers, multicast
239  *  table, VLAN filter table, calls routine to set up link and flow control
240  *  settings, and leaves transmit and receive units disabled and uninitialized
241  **/
242 s32 txgbe_start_hw(struct txgbe_hw *hw)
243 {
244         s32 err;
245         u16 device_caps;
246
247         DEBUGFUNC("txgbe_start_hw");
248
249         /* Set the media type */
250         hw->phy.media_type = hw->phy.get_media_type(hw);
251
252         /* Clear the VLAN filter table */
253         hw->mac.clear_vfta(hw);
254
255         /* Clear statistics registers */
256         hw->mac.clear_hw_cntrs(hw);
257
258         /* Setup flow control */
259         err = txgbe_setup_fc(hw);
260         if (err != 0 && err != TXGBE_NOT_IMPLEMENTED) {
261                 DEBUGOUT("Flow control setup failed, returning %d\n", err);
262                 return err;
263         }
264
265         /* Cache bit indicating need for crosstalk fix */
266         switch (hw->mac.type) {
267         case txgbe_mac_raptor:
268                 hw->mac.get_device_caps(hw, &device_caps);
269                 if (device_caps & TXGBE_DEVICE_CAPS_NO_CROSSTALK_WR)
270                         hw->need_crosstalk_fix = false;
271                 else
272                         hw->need_crosstalk_fix = true;
273                 break;
274         default:
275                 hw->need_crosstalk_fix = false;
276                 break;
277         }
278
279         /* Clear adapter stopped flag */
280         hw->adapter_stopped = false;
281
282         return 0;
283 }
284
285 /**
286  *  txgbe_start_hw_gen2 - Init sequence for common device family
287  *  @hw: pointer to hw structure
288  *
289  * Performs the init sequence common to the second generation
290  * of 10 GbE devices.
291  **/
292 s32 txgbe_start_hw_gen2(struct txgbe_hw *hw)
293 {
294         u32 i;
295
296         /* Clear the rate limiters */
297         for (i = 0; i < hw->mac.max_tx_queues; i++) {
298                 wr32(hw, TXGBE_ARBPOOLIDX, i);
299                 wr32(hw, TXGBE_ARBTXRATE, 0);
300         }
301         txgbe_flush(hw);
302
303         /* We need to run link autotry after the driver loads */
304         hw->mac.autotry_restart = true;
305
306         return 0;
307 }
308
309 /**
310  *  txgbe_init_hw - Generic hardware initialization
311  *  @hw: pointer to hardware structure
312  *
313  *  Initialize the hardware by resetting the hardware, filling the bus info
314  *  structure and media type, clears all on chip counters, initializes receive
315  *  address registers, multicast table, VLAN filter table, calls routine to set
316  *  up link and flow control settings, and leaves transmit and receive units
317  *  disabled and uninitialized
318  **/
319 s32 txgbe_init_hw(struct txgbe_hw *hw)
320 {
321         s32 status;
322
323         DEBUGFUNC("txgbe_init_hw");
324
325         /* Reset the hardware */
326         status = hw->mac.reset_hw(hw);
327         if (status == 0 || status == TXGBE_ERR_SFP_NOT_PRESENT) {
328                 /* Start the HW */
329                 status = hw->mac.start_hw(hw);
330         }
331
332         if (status != 0)
333                 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
334
335         return status;
336 }
337
338 /**
339  *  txgbe_clear_hw_cntrs - Generic clear hardware counters
340  *  @hw: pointer to hardware structure
341  *
342  *  Clears all hardware statistics counters by reading them from the hardware
343  *  Statistics counters are clear on read.
344  **/
345 s32 txgbe_clear_hw_cntrs(struct txgbe_hw *hw)
346 {
347         u16 i = 0;
348
349         DEBUGFUNC("txgbe_clear_hw_cntrs");
350
351         /* QP Stats */
352         /* don't write clear queue stats */
353         for (i = 0; i < TXGBE_MAX_QP; i++) {
354                 hw->qp_last[i].rx_qp_packets = 0;
355                 hw->qp_last[i].tx_qp_packets = 0;
356                 hw->qp_last[i].rx_qp_bytes = 0;
357                 hw->qp_last[i].tx_qp_bytes = 0;
358                 hw->qp_last[i].rx_qp_mc_packets = 0;
359         }
360
361         /* PB Stats */
362         for (i = 0; i < TXGBE_MAX_UP; i++) {
363                 rd32(hw, TXGBE_PBRXUPXON(i));
364                 rd32(hw, TXGBE_PBRXUPXOFF(i));
365                 rd32(hw, TXGBE_PBTXUPXON(i));
366                 rd32(hw, TXGBE_PBTXUPXOFF(i));
367                 rd32(hw, TXGBE_PBTXUPOFF(i));
368
369                 rd32(hw, TXGBE_PBRXMISS(i));
370         }
371         rd32(hw, TXGBE_PBRXLNKXON);
372         rd32(hw, TXGBE_PBRXLNKXOFF);
373         rd32(hw, TXGBE_PBTXLNKXON);
374         rd32(hw, TXGBE_PBTXLNKXOFF);
375
376         /* DMA Stats */
377         rd32(hw, TXGBE_DMARXPKT);
378         rd32(hw, TXGBE_DMATXPKT);
379
380         rd64(hw, TXGBE_DMARXOCTL);
381         rd64(hw, TXGBE_DMATXOCTL);
382
383         /* MAC Stats */
384         rd64(hw, TXGBE_MACRXERRCRCL);
385         rd64(hw, TXGBE_MACRXMPKTL);
386         rd64(hw, TXGBE_MACTXMPKTL);
387
388         rd64(hw, TXGBE_MACRXPKTL);
389         rd64(hw, TXGBE_MACTXPKTL);
390         rd64(hw, TXGBE_MACRXGBOCTL);
391
392         rd64(hw, TXGBE_MACRXOCTL);
393         rd32(hw, TXGBE_MACTXOCTL);
394
395         rd64(hw, TXGBE_MACRX1TO64L);
396         rd64(hw, TXGBE_MACRX65TO127L);
397         rd64(hw, TXGBE_MACRX128TO255L);
398         rd64(hw, TXGBE_MACRX256TO511L);
399         rd64(hw, TXGBE_MACRX512TO1023L);
400         rd64(hw, TXGBE_MACRX1024TOMAXL);
401         rd64(hw, TXGBE_MACTX1TO64L);
402         rd64(hw, TXGBE_MACTX65TO127L);
403         rd64(hw, TXGBE_MACTX128TO255L);
404         rd64(hw, TXGBE_MACTX256TO511L);
405         rd64(hw, TXGBE_MACTX512TO1023L);
406         rd64(hw, TXGBE_MACTX1024TOMAXL);
407
408         rd64(hw, TXGBE_MACRXERRLENL);
409         rd32(hw, TXGBE_MACRXOVERSIZE);
410         rd32(hw, TXGBE_MACRXJABBER);
411
412         /* FCoE Stats */
413         rd32(hw, TXGBE_FCOECRC);
414         rd32(hw, TXGBE_FCOELAST);
415         rd32(hw, TXGBE_FCOERPDC);
416         rd32(hw, TXGBE_FCOEPRC);
417         rd32(hw, TXGBE_FCOEPTC);
418         rd32(hw, TXGBE_FCOEDWRC);
419         rd32(hw, TXGBE_FCOEDWTC);
420
421         /* Flow Director Stats */
422         rd32(hw, TXGBE_FDIRMATCH);
423         rd32(hw, TXGBE_FDIRMISS);
424         rd32(hw, TXGBE_FDIRUSED);
425         rd32(hw, TXGBE_FDIRUSED);
426         rd32(hw, TXGBE_FDIRFAIL);
427         rd32(hw, TXGBE_FDIRFAIL);
428
429         /* MACsec Stats */
430         rd32(hw, TXGBE_LSECTX_UTPKT);
431         rd32(hw, TXGBE_LSECTX_ENCPKT);
432         rd32(hw, TXGBE_LSECTX_PROTPKT);
433         rd32(hw, TXGBE_LSECTX_ENCOCT);
434         rd32(hw, TXGBE_LSECTX_PROTOCT);
435         rd32(hw, TXGBE_LSECRX_UTPKT);
436         rd32(hw, TXGBE_LSECRX_BTPKT);
437         rd32(hw, TXGBE_LSECRX_NOSCIPKT);
438         rd32(hw, TXGBE_LSECRX_UNSCIPKT);
439         rd32(hw, TXGBE_LSECRX_DECOCT);
440         rd32(hw, TXGBE_LSECRX_VLDOCT);
441         rd32(hw, TXGBE_LSECRX_UNCHKPKT);
442         rd32(hw, TXGBE_LSECRX_DLYPKT);
443         rd32(hw, TXGBE_LSECRX_LATEPKT);
444         for (i = 0; i < 2; i++) {
445                 rd32(hw, TXGBE_LSECRX_OKPKT(i));
446                 rd32(hw, TXGBE_LSECRX_INVPKT(i));
447                 rd32(hw, TXGBE_LSECRX_BADPKT(i));
448         }
449         rd32(hw, TXGBE_LSECRX_INVSAPKT);
450         rd32(hw, TXGBE_LSECRX_BADSAPKT);
451
452         return 0;
453 }
454
455 /**
456  *  txgbe_get_mac_addr - Generic get MAC address
457  *  @hw: pointer to hardware structure
458  *  @mac_addr: Adapter MAC address
459  *
460  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
461  *  A reset of the adapter must be performed prior to calling this function
462  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
463  **/
464 s32 txgbe_get_mac_addr(struct txgbe_hw *hw, u8 *mac_addr)
465 {
466         u32 rar_high;
467         u32 rar_low;
468         u16 i;
469
470         DEBUGFUNC("txgbe_get_mac_addr");
471
472         wr32(hw, TXGBE_ETHADDRIDX, 0);
473         rar_high = rd32(hw, TXGBE_ETHADDRH);
474         rar_low = rd32(hw, TXGBE_ETHADDRL);
475
476         for (i = 0; i < 2; i++)
477                 mac_addr[i] = (u8)(rar_high >> (1 - i) * 8);
478
479         for (i = 0; i < 4; i++)
480                 mac_addr[i + 2] = (u8)(rar_low >> (3 - i) * 8);
481
482         return 0;
483 }
484
485 /**
486  *  txgbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
487  *  @hw: pointer to the HW structure
488  *
489  *  Determines the LAN function id by reading memory-mapped registers and swaps
490  *  the port value if requested, and set MAC instance for devices.
491  **/
492 void txgbe_set_lan_id_multi_port(struct txgbe_hw *hw)
493 {
494         struct txgbe_bus_info *bus = &hw->bus;
495         u32 reg;
496
497         DEBUGFUNC("txgbe_set_lan_id_multi_port_pcie");
498
499         reg = rd32(hw, TXGBE_PORTSTAT);
500         bus->lan_id = TXGBE_PORTSTAT_ID(reg);
501
502         /* check for single port */
503         reg = rd32(hw, TXGBE_PWR);
504         if (TXGBE_PWR_LANID(reg) == TXGBE_PWR_LANID_SWAP)
505                 bus->func = 0;
506         else
507                 bus->func = bus->lan_id;
508 }
509
510 /**
511  *  txgbe_stop_hw - Generic stop Tx/Rx units
512  *  @hw: pointer to hardware structure
513  *
514  *  Sets the adapter_stopped flag within txgbe_hw struct. Clears interrupts,
515  *  disables transmit and receive units. The adapter_stopped flag is used by
516  *  the shared code and drivers to determine if the adapter is in a stopped
517  *  state and should not touch the hardware.
518  **/
519 s32 txgbe_stop_hw(struct txgbe_hw *hw)
520 {
521         u32 reg_val;
522         u16 i;
523
524         DEBUGFUNC("txgbe_stop_hw");
525
526         /*
527          * Set the adapter_stopped flag so other driver functions stop touching
528          * the hardware
529          */
530         hw->adapter_stopped = true;
531
532         /* Disable the receive unit */
533         txgbe_disable_rx(hw);
534
535         /* Clear interrupt mask to stop interrupts from being generated */
536         wr32(hw, TXGBE_IENMISC, 0);
537         wr32(hw, TXGBE_IMS(0), TXGBE_IMS_MASK);
538         wr32(hw, TXGBE_IMS(1), TXGBE_IMS_MASK);
539
540         /* Clear any pending interrupts, flush previous writes */
541         wr32(hw, TXGBE_ICRMISC, TXGBE_ICRMISC_MASK);
542         wr32(hw, TXGBE_ICR(0), TXGBE_ICR_MASK);
543         wr32(hw, TXGBE_ICR(1), TXGBE_ICR_MASK);
544
545         /* Disable the transmit unit.  Each queue must be disabled. */
546         for (i = 0; i < hw->mac.max_tx_queues; i++)
547                 wr32(hw, TXGBE_TXCFG(i), TXGBE_TXCFG_FLUSH);
548
549         /* Disable the receive unit by stopping each queue */
550         for (i = 0; i < hw->mac.max_rx_queues; i++) {
551                 reg_val = rd32(hw, TXGBE_RXCFG(i));
552                 reg_val &= ~TXGBE_RXCFG_ENA;
553                 wr32(hw, TXGBE_RXCFG(i), reg_val);
554         }
555
556         /* flush all queues disables */
557         txgbe_flush(hw);
558         msec_delay(2);
559
560         return 0;
561 }
562
563 /**
564  *  txgbe_validate_mac_addr - Validate MAC address
565  *  @mac_addr: pointer to MAC address.
566  *
567  *  Tests a MAC address to ensure it is a valid Individual Address.
568  **/
569 s32 txgbe_validate_mac_addr(u8 *mac_addr)
570 {
571         s32 status = 0;
572
573         DEBUGFUNC("txgbe_validate_mac_addr");
574
575         /* Make sure it is not a multicast address */
576         if (TXGBE_IS_MULTICAST(mac_addr)) {
577                 status = TXGBE_ERR_INVALID_MAC_ADDR;
578         /* Not a broadcast address */
579         } else if (TXGBE_IS_BROADCAST(mac_addr)) {
580                 status = TXGBE_ERR_INVALID_MAC_ADDR;
581         /* Reject the zero address */
582         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
583                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
584                 status = TXGBE_ERR_INVALID_MAC_ADDR;
585         }
586         return status;
587 }
588
589 /**
590  *  txgbe_set_rar - Set Rx address register
591  *  @hw: pointer to hardware structure
592  *  @index: Receive address register to write
593  *  @addr: Address to put into receive address register
594  *  @vmdq: VMDq "set" or "pool" index
595  *  @enable_addr: set flag that address is active
596  *
597  *  Puts an ethernet address into a receive address register.
598  **/
599 s32 txgbe_set_rar(struct txgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
600                           u32 enable_addr)
601 {
602         u32 rar_low, rar_high;
603         u32 rar_entries = hw->mac.num_rar_entries;
604
605         DEBUGFUNC("txgbe_set_rar");
606
607         /* Make sure we are using a valid rar index range */
608         if (index >= rar_entries) {
609                 DEBUGOUT("RAR index %d is out of range.\n", index);
610                 return TXGBE_ERR_INVALID_ARGUMENT;
611         }
612
613         /* setup VMDq pool selection before this RAR gets enabled */
614         hw->mac.set_vmdq(hw, index, vmdq);
615
616         /*
617          * HW expects these in little endian so we reverse the byte
618          * order from network order (big endian) to little endian
619          */
620         rar_low = TXGBE_ETHADDRL_AD0(addr[5]) |
621                   TXGBE_ETHADDRL_AD1(addr[4]) |
622                   TXGBE_ETHADDRL_AD2(addr[3]) |
623                   TXGBE_ETHADDRL_AD3(addr[2]);
624         /*
625          * Some parts put the VMDq setting in the extra RAH bits,
626          * so save everything except the lower 16 bits that hold part
627          * of the address and the address valid bit.
628          */
629         rar_high = rd32(hw, TXGBE_ETHADDRH);
630         rar_high &= ~TXGBE_ETHADDRH_AD_MASK;
631         rar_high |= (TXGBE_ETHADDRH_AD4(addr[1]) |
632                      TXGBE_ETHADDRH_AD5(addr[0]));
633
634         rar_high &= ~TXGBE_ETHADDRH_VLD;
635         if (enable_addr != 0)
636                 rar_high |= TXGBE_ETHADDRH_VLD;
637
638         wr32(hw, TXGBE_ETHADDRIDX, index);
639         wr32(hw, TXGBE_ETHADDRL, rar_low);
640         wr32(hw, TXGBE_ETHADDRH, rar_high);
641
642         return 0;
643 }
644
645 /**
646  *  txgbe_clear_rar - Remove Rx address register
647  *  @hw: pointer to hardware structure
648  *  @index: Receive address register to write
649  *
650  *  Clears an ethernet address from a receive address register.
651  **/
652 s32 txgbe_clear_rar(struct txgbe_hw *hw, u32 index)
653 {
654         u32 rar_high;
655         u32 rar_entries = hw->mac.num_rar_entries;
656
657         DEBUGFUNC("txgbe_clear_rar");
658
659         /* Make sure we are using a valid rar index range */
660         if (index >= rar_entries) {
661                 DEBUGOUT("RAR index %d is out of range.\n", index);
662                 return TXGBE_ERR_INVALID_ARGUMENT;
663         }
664
665         /*
666          * Some parts put the VMDq setting in the extra RAH bits,
667          * so save everything except the lower 16 bits that hold part
668          * of the address and the address valid bit.
669          */
670         wr32(hw, TXGBE_ETHADDRIDX, index);
671         rar_high = rd32(hw, TXGBE_ETHADDRH);
672         rar_high &= ~(TXGBE_ETHADDRH_AD_MASK | TXGBE_ETHADDRH_VLD);
673
674         wr32(hw, TXGBE_ETHADDRL, 0);
675         wr32(hw, TXGBE_ETHADDRH, rar_high);
676
677         /* clear VMDq pool/queue selection for this RAR */
678         hw->mac.clear_vmdq(hw, index, BIT_MASK32);
679
680         return 0;
681 }
682
683 /**
684  *  txgbe_init_rx_addrs - Initializes receive address filters.
685  *  @hw: pointer to hardware structure
686  *
687  *  Places the MAC address in receive address register 0 and clears the rest
688  *  of the receive address registers. Clears the multicast table. Assumes
689  *  the receiver is in reset when the routine is called.
690  **/
691 s32 txgbe_init_rx_addrs(struct txgbe_hw *hw)
692 {
693         u32 i;
694         u32 psrctl;
695         u32 rar_entries = hw->mac.num_rar_entries;
696
697         DEBUGFUNC("txgbe_init_rx_addrs");
698
699         /*
700          * If the current mac address is valid, assume it is a software override
701          * to the permanent address.
702          * Otherwise, use the permanent address from the eeprom.
703          */
704         if (txgbe_validate_mac_addr(hw->mac.addr) ==
705             TXGBE_ERR_INVALID_MAC_ADDR) {
706                 /* Get the MAC address from the RAR0 for later reference */
707                 hw->mac.get_mac_addr(hw, hw->mac.addr);
708
709                 DEBUGOUT(" Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
710                           hw->mac.addr[0], hw->mac.addr[1],
711                           hw->mac.addr[2]);
712                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
713                           hw->mac.addr[4], hw->mac.addr[5]);
714         } else {
715                 /* Setup the receive address. */
716                 DEBUGOUT("Overriding MAC Address in RAR[0]\n");
717                 DEBUGOUT(" New MAC Addr =%.2X %.2X %.2X ",
718                           hw->mac.addr[0], hw->mac.addr[1],
719                           hw->mac.addr[2]);
720                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
721                           hw->mac.addr[4], hw->mac.addr[5]);
722
723                 hw->mac.set_rar(hw, 0, hw->mac.addr, 0, true);
724         }
725
726         /* clear VMDq pool/queue selection for RAR 0 */
727         hw->mac.clear_vmdq(hw, 0, BIT_MASK32);
728
729         hw->addr_ctrl.overflow_promisc = 0;
730
731         hw->addr_ctrl.rar_used_count = 1;
732
733         /* Zero out the other receive addresses. */
734         DEBUGOUT("Clearing RAR[1-%d]\n", rar_entries - 1);
735         for (i = 1; i < rar_entries; i++) {
736                 wr32(hw, TXGBE_ETHADDRIDX, i);
737                 wr32(hw, TXGBE_ETHADDRL, 0);
738                 wr32(hw, TXGBE_ETHADDRH, 0);
739         }
740
741         /* Clear the MTA */
742         hw->addr_ctrl.mta_in_use = 0;
743         psrctl = rd32(hw, TXGBE_PSRCTL);
744         psrctl &= ~(TXGBE_PSRCTL_ADHF12_MASK | TXGBE_PSRCTL_MCHFENA);
745         psrctl |= TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
746         wr32(hw, TXGBE_PSRCTL, psrctl);
747
748         DEBUGOUT(" Clearing MTA\n");
749         for (i = 0; i < hw->mac.mcft_size; i++)
750                 wr32(hw, TXGBE_MCADDRTBL(i), 0);
751
752         txgbe_init_uta_tables(hw);
753
754         return 0;
755 }
756
757 /**
758  *  txgbe_mta_vector - Determines bit-vector in multicast table to set
759  *  @hw: pointer to hardware structure
760  *  @mc_addr: the multicast address
761  *
762  *  Extracts the 12 bits, from a multicast address, to determine which
763  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
764  *  incoming rx multicast addresses, to determine the bit-vector to check in
765  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
766  *  by the MO field of the PSRCTRL. The MO field is set during initialization
767  *  to mc_filter_type.
768  **/
769 static s32 txgbe_mta_vector(struct txgbe_hw *hw, u8 *mc_addr)
770 {
771         u32 vector = 0;
772
773         DEBUGFUNC("txgbe_mta_vector");
774
775         switch (hw->mac.mc_filter_type) {
776         case 0:   /* use bits [47:36] of the address */
777                 vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
778                 break;
779         case 1:   /* use bits [46:35] of the address */
780                 vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
781                 break;
782         case 2:   /* use bits [45:34] of the address */
783                 vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
784                 break;
785         case 3:   /* use bits [43:32] of the address */
786                 vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
787                 break;
788         default:  /* Invalid mc_filter_type */
789                 DEBUGOUT("MC filter type param set incorrectly\n");
790                 ASSERT(0);
791                 break;
792         }
793
794         /* vector can only be 12-bits or boundary will be exceeded */
795         vector &= 0xFFF;
796         return vector;
797 }
798
799 /**
800  *  txgbe_set_mta - Set bit-vector in multicast table
801  *  @hw: pointer to hardware structure
802  *  @mc_addr: Multicast address
803  *
804  *  Sets the bit-vector in the multicast table.
805  **/
806 void txgbe_set_mta(struct txgbe_hw *hw, u8 *mc_addr)
807 {
808         u32 vector;
809         u32 vector_bit;
810         u32 vector_reg;
811
812         DEBUGFUNC("txgbe_set_mta");
813
814         hw->addr_ctrl.mta_in_use++;
815
816         vector = txgbe_mta_vector(hw, mc_addr);
817         DEBUGOUT(" bit-vector = 0x%03X\n", vector);
818
819         /*
820          * The MTA is a register array of 128 32-bit registers. It is treated
821          * like an array of 4096 bits.  We want to set bit
822          * BitArray[vector_value]. So we figure out what register the bit is
823          * in, read it, OR in the new bit, then write back the new value.  The
824          * register is determined by the upper 7 bits of the vector value and
825          * the bit within that register are determined by the lower 5 bits of
826          * the value.
827          */
828         vector_reg = (vector >> 5) & 0x7F;
829         vector_bit = vector & 0x1F;
830         hw->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
831 }
832
833 /**
834  *  txgbe_update_mc_addr_list - Updates MAC list of multicast addresses
835  *  @hw: pointer to hardware structure
836  *  @mc_addr_list: the list of new multicast addresses
837  *  @mc_addr_count: number of addresses
838  *  @next: iterator function to walk the multicast address list
839  *  @clear: flag, when set clears the table beforehand
840  *
841  *  When the clear flag is set, the given list replaces any existing list.
842  *  Hashes the given addresses into the multicast table.
843  **/
844 s32 txgbe_update_mc_addr_list(struct txgbe_hw *hw, u8 *mc_addr_list,
845                                       u32 mc_addr_count, txgbe_mc_addr_itr next,
846                                       bool clear)
847 {
848         u32 i;
849         u32 vmdq;
850
851         DEBUGFUNC("txgbe_update_mc_addr_list");
852
853         /*
854          * Set the new number of MC addresses that we are being requested to
855          * use.
856          */
857         hw->addr_ctrl.num_mc_addrs = mc_addr_count;
858         hw->addr_ctrl.mta_in_use = 0;
859
860         /* Clear mta_shadow */
861         if (clear) {
862                 DEBUGOUT(" Clearing MTA\n");
863                 memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
864         }
865
866         /* Update mta_shadow */
867         for (i = 0; i < mc_addr_count; i++) {
868                 DEBUGOUT(" Adding the multicast addresses:\n");
869                 txgbe_set_mta(hw, next(hw, &mc_addr_list, &vmdq));
870         }
871
872         /* Enable mta */
873         for (i = 0; i < hw->mac.mcft_size; i++)
874                 wr32a(hw, TXGBE_MCADDRTBL(0), i,
875                                       hw->mac.mta_shadow[i]);
876
877         if (hw->addr_ctrl.mta_in_use > 0) {
878                 u32 psrctl = rd32(hw, TXGBE_PSRCTL);
879                 psrctl &= ~(TXGBE_PSRCTL_ADHF12_MASK | TXGBE_PSRCTL_MCHFENA);
880                 psrctl |= TXGBE_PSRCTL_MCHFENA |
881                          TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
882                 wr32(hw, TXGBE_PSRCTL, psrctl);
883         }
884
885         DEBUGOUT("txgbe update mc addr list complete\n");
886         return 0;
887 }
888
889 /**
890  *  txgbe_fc_enable - Enable flow control
891  *  @hw: pointer to hardware structure
892  *
893  *  Enable flow control according to the current settings.
894  **/
895 s32 txgbe_fc_enable(struct txgbe_hw *hw)
896 {
897         s32 err = 0;
898         u32 mflcn_reg, fccfg_reg;
899         u32 pause_time;
900         u32 fcrtl, fcrth;
901         int i;
902
903         DEBUGFUNC("txgbe_fc_enable");
904
905         /* Validate the water mark configuration */
906         if (!hw->fc.pause_time) {
907                 err = TXGBE_ERR_INVALID_LINK_SETTINGS;
908                 goto out;
909         }
910
911         /* Low water mark of zero causes XOFF floods */
912         for (i = 0; i < TXGBE_DCB_TC_MAX; i++) {
913                 if ((hw->fc.current_mode & txgbe_fc_tx_pause) &&
914                     hw->fc.high_water[i]) {
915                         if (!hw->fc.low_water[i] ||
916                             hw->fc.low_water[i] >= hw->fc.high_water[i]) {
917                                 DEBUGOUT("Invalid water mark configuration\n");
918                                 err = TXGBE_ERR_INVALID_LINK_SETTINGS;
919                                 goto out;
920                         }
921                 }
922         }
923
924         /* Negotiate the fc mode to use */
925         hw->mac.fc_autoneg(hw);
926
927         /* Disable any previous flow control settings */
928         mflcn_reg = rd32(hw, TXGBE_RXFCCFG);
929         mflcn_reg &= ~(TXGBE_RXFCCFG_FC | TXGBE_RXFCCFG_PFC);
930
931         fccfg_reg = rd32(hw, TXGBE_TXFCCFG);
932         fccfg_reg &= ~(TXGBE_TXFCCFG_FC | TXGBE_TXFCCFG_PFC);
933
934         /*
935          * The possible values of fc.current_mode are:
936          * 0: Flow control is completely disabled
937          * 1: Rx flow control is enabled (we can receive pause frames,
938          *    but not send pause frames).
939          * 2: Tx flow control is enabled (we can send pause frames but
940          *    we do not support receiving pause frames).
941          * 3: Both Rx and Tx flow control (symmetric) are enabled.
942          * other: Invalid.
943          */
944         switch (hw->fc.current_mode) {
945         case txgbe_fc_none:
946                 /*
947                  * Flow control is disabled by software override or autoneg.
948                  * The code below will actually disable it in the HW.
949                  */
950                 break;
951         case txgbe_fc_rx_pause:
952                 /*
953                  * Rx Flow control is enabled and Tx Flow control is
954                  * disabled by software override. Since there really
955                  * isn't a way to advertise that we are capable of RX
956                  * Pause ONLY, we will advertise that we support both
957                  * symmetric and asymmetric Rx PAUSE.  Later, we will
958                  * disable the adapter's ability to send PAUSE frames.
959                  */
960                 mflcn_reg |= TXGBE_RXFCCFG_FC;
961                 break;
962         case txgbe_fc_tx_pause:
963                 /*
964                  * Tx Flow control is enabled, and Rx Flow control is
965                  * disabled by software override.
966                  */
967                 fccfg_reg |= TXGBE_TXFCCFG_FC;
968                 break;
969         case txgbe_fc_full:
970                 /* Flow control (both Rx and Tx) is enabled by SW override. */
971                 mflcn_reg |= TXGBE_RXFCCFG_FC;
972                 fccfg_reg |= TXGBE_TXFCCFG_FC;
973                 break;
974         default:
975                 DEBUGOUT("Flow control param set incorrectly\n");
976                 err = TXGBE_ERR_CONFIG;
977                 goto out;
978         }
979
980         /* Set 802.3x based flow control settings. */
981         wr32(hw, TXGBE_RXFCCFG, mflcn_reg);
982         wr32(hw, TXGBE_TXFCCFG, fccfg_reg);
983
984         /* Set up and enable Rx high/low water mark thresholds, enable XON. */
985         for (i = 0; i < TXGBE_DCB_TC_MAX; i++) {
986                 if ((hw->fc.current_mode & txgbe_fc_tx_pause) &&
987                     hw->fc.high_water[i]) {
988                         fcrtl = TXGBE_FCWTRLO_TH(hw->fc.low_water[i]) |
989                                 TXGBE_FCWTRLO_XON;
990                         fcrth = TXGBE_FCWTRHI_TH(hw->fc.high_water[i]) |
991                                 TXGBE_FCWTRHI_XOFF;
992                 } else {
993                         /*
994                          * In order to prevent Tx hangs when the internal Tx
995                          * switch is enabled we must set the high water mark
996                          * to the Rx packet buffer size - 24KB.  This allows
997                          * the Tx switch to function even under heavy Rx
998                          * workloads.
999                          */
1000                         fcrtl = 0;
1001                         fcrth = rd32(hw, TXGBE_PBRXSIZE(i)) - 24576;
1002                 }
1003                 wr32(hw, TXGBE_FCWTRLO(i), fcrtl);
1004                 wr32(hw, TXGBE_FCWTRHI(i), fcrth);
1005         }
1006
1007         /* Configure pause time (2 TCs per register) */
1008         pause_time = TXGBE_RXFCFSH_TIME(hw->fc.pause_time);
1009         for (i = 0; i < (TXGBE_DCB_TC_MAX / 2); i++)
1010                 wr32(hw, TXGBE_FCXOFFTM(i), pause_time * 0x00010001);
1011
1012         /* Configure flow control refresh threshold value */
1013         wr32(hw, TXGBE_RXFCRFSH, hw->fc.pause_time / 2);
1014
1015 out:
1016         return err;
1017 }
1018
1019 /**
1020  *  txgbe_acquire_swfw_sync - Acquire SWFW semaphore
1021  *  @hw: pointer to hardware structure
1022  *  @mask: Mask to specify which semaphore to acquire
1023  *
1024  *  Acquires the SWFW semaphore through the MNGSEM register for the specified
1025  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
1026  **/
1027 s32 txgbe_acquire_swfw_sync(struct txgbe_hw *hw, u32 mask)
1028 {
1029         u32 mngsem = 0;
1030         u32 swmask = TXGBE_MNGSEM_SW(mask);
1031         u32 fwmask = TXGBE_MNGSEM_FW(mask);
1032         u32 timeout = 200;
1033         u32 i;
1034
1035         DEBUGFUNC("txgbe_acquire_swfw_sync");
1036
1037         for (i = 0; i < timeout; i++) {
1038                 /*
1039                  * SW NVM semaphore bit is used for access to all
1040                  * SW_FW_SYNC bits (not just NVM)
1041                  */
1042                 if (txgbe_get_eeprom_semaphore(hw))
1043                         return TXGBE_ERR_SWFW_SYNC;
1044
1045                 mngsem = rd32(hw, TXGBE_MNGSEM);
1046                 if (mngsem & (fwmask | swmask)) {
1047                         /* Resource is currently in use by FW or SW */
1048                         txgbe_release_eeprom_semaphore(hw);
1049                         msec_delay(5);
1050                 } else {
1051                         mngsem |= swmask;
1052                         wr32(hw, TXGBE_MNGSEM, mngsem);
1053                         txgbe_release_eeprom_semaphore(hw);
1054                         return 0;
1055                 }
1056         }
1057
1058         /* If time expired clear the bits holding the lock and retry */
1059         if (mngsem & (fwmask | swmask))
1060                 txgbe_release_swfw_sync(hw, mngsem & (fwmask | swmask));
1061
1062         msec_delay(5);
1063         return TXGBE_ERR_SWFW_SYNC;
1064 }
1065
1066 /**
1067  *  txgbe_release_swfw_sync - Release SWFW semaphore
1068  *  @hw: pointer to hardware structure
1069  *  @mask: Mask to specify which semaphore to release
1070  *
1071  *  Releases the SWFW semaphore through the MNGSEM register for the specified
1072  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
1073  **/
1074 void txgbe_release_swfw_sync(struct txgbe_hw *hw, u32 mask)
1075 {
1076         u32 mngsem;
1077         u32 swmask = mask;
1078
1079         DEBUGFUNC("txgbe_release_swfw_sync");
1080
1081         txgbe_get_eeprom_semaphore(hw);
1082
1083         mngsem = rd32(hw, TXGBE_MNGSEM);
1084         mngsem &= ~swmask;
1085         wr32(hw, TXGBE_MNGSEM, mngsem);
1086
1087         txgbe_release_eeprom_semaphore(hw);
1088 }
1089
1090 /**
1091  *  txgbe_disable_sec_rx_path - Stops the receive data path
1092  *  @hw: pointer to hardware structure
1093  *
1094  *  Stops the receive data path and waits for the HW to internally empty
1095  *  the Rx security block
1096  **/
1097 s32 txgbe_disable_sec_rx_path(struct txgbe_hw *hw)
1098 {
1099 #define TXGBE_MAX_SECRX_POLL 4000
1100
1101         int i;
1102         u32 secrxreg;
1103
1104         DEBUGFUNC("txgbe_disable_sec_rx_path");
1105
1106         secrxreg = rd32(hw, TXGBE_SECRXCTL);
1107         secrxreg |= TXGBE_SECRXCTL_XDSA;
1108         wr32(hw, TXGBE_SECRXCTL, secrxreg);
1109         for (i = 0; i < TXGBE_MAX_SECRX_POLL; i++) {
1110                 secrxreg = rd32(hw, TXGBE_SECRXSTAT);
1111                 if (!(secrxreg & TXGBE_SECRXSTAT_RDY))
1112                         /* Use interrupt-safe sleep just in case */
1113                         usec_delay(10);
1114                 else
1115                         break;
1116         }
1117
1118         /* For informational purposes only */
1119         if (i >= TXGBE_MAX_SECRX_POLL)
1120                 DEBUGOUT("Rx unit being enabled before security "
1121                          "path fully disabled.  Continuing with init.\n");
1122
1123         return 0;
1124 }
1125
1126 /**
1127  *  txgbe_enable_sec_rx_path - Enables the receive data path
1128  *  @hw: pointer to hardware structure
1129  *
1130  *  Enables the receive data path.
1131  **/
1132 s32 txgbe_enable_sec_rx_path(struct txgbe_hw *hw)
1133 {
1134         u32 secrxreg;
1135
1136         DEBUGFUNC("txgbe_enable_sec_rx_path");
1137
1138         secrxreg = rd32(hw, TXGBE_SECRXCTL);
1139         secrxreg &= ~TXGBE_SECRXCTL_XDSA;
1140         wr32(hw, TXGBE_SECRXCTL, secrxreg);
1141         txgbe_flush(hw);
1142
1143         return 0;
1144 }
1145
1146 /**
1147  *  txgbe_disable_sec_tx_path - Stops the transmit data path
1148  *  @hw: pointer to hardware structure
1149  *
1150  *  Stops the transmit data path and waits for the HW to internally empty
1151  *  the Tx security block
1152  **/
1153 int txgbe_disable_sec_tx_path(struct txgbe_hw *hw)
1154 {
1155 #define TXGBE_MAX_SECTX_POLL 40
1156
1157         int i;
1158         u32 sectxreg;
1159
1160         sectxreg = rd32(hw, TXGBE_SECTXCTL);
1161         sectxreg |= TXGBE_SECTXCTL_XDSA;
1162         wr32(hw, TXGBE_SECTXCTL, sectxreg);
1163         for (i = 0; i < TXGBE_MAX_SECTX_POLL; i++) {
1164                 sectxreg = rd32(hw, TXGBE_SECTXSTAT);
1165                 if (sectxreg & TXGBE_SECTXSTAT_RDY)
1166                         break;
1167                 /* Use interrupt-safe sleep just in case */
1168                 usec_delay(1000);
1169         }
1170
1171         /* For informational purposes only */
1172         if (i >= TXGBE_MAX_SECTX_POLL)
1173                 PMD_DRV_LOG(DEBUG, "Tx unit being enabled before security "
1174                          "path fully disabled.  Continuing with init.");
1175
1176         return 0;
1177 }
1178
1179 /**
1180  *  txgbe_enable_sec_tx_path - Enables the transmit data path
1181  *  @hw: pointer to hardware structure
1182  *
1183  *  Enables the transmit data path.
1184  **/
1185 int txgbe_enable_sec_tx_path(struct txgbe_hw *hw)
1186 {
1187         uint32_t sectxreg;
1188
1189         sectxreg = rd32(hw, TXGBE_SECTXCTL);
1190         sectxreg &= ~TXGBE_SECTXCTL_XDSA;
1191         wr32(hw, TXGBE_SECTXCTL, sectxreg);
1192         txgbe_flush(hw);
1193
1194         return 0;
1195 }
1196
1197 /**
1198  *  txgbe_get_san_mac_addr_offset - Get SAN MAC address offset from the EEPROM
1199  *  @hw: pointer to hardware structure
1200  *  @san_mac_offset: SAN MAC address offset
1201  *
1202  *  This function will read the EEPROM location for the SAN MAC address
1203  *  pointer, and returns the value at that location.  This is used in both
1204  *  get and set mac_addr routines.
1205  **/
1206 static s32 txgbe_get_san_mac_addr_offset(struct txgbe_hw *hw,
1207                                          u16 *san_mac_offset)
1208 {
1209         s32 err;
1210
1211         DEBUGFUNC("txgbe_get_san_mac_addr_offset");
1212
1213         /*
1214          * First read the EEPROM pointer to see if the MAC addresses are
1215          * available.
1216          */
1217         err = hw->rom.readw_sw(hw, TXGBE_SAN_MAC_ADDR_PTR,
1218                                       san_mac_offset);
1219         if (err) {
1220                 DEBUGOUT("eeprom at offset %d failed",
1221                          TXGBE_SAN_MAC_ADDR_PTR);
1222         }
1223
1224         return err;
1225 }
1226
1227 /**
1228  *  txgbe_get_san_mac_addr - SAN MAC address retrieval from the EEPROM
1229  *  @hw: pointer to hardware structure
1230  *  @san_mac_addr: SAN MAC address
1231  *
1232  *  Reads the SAN MAC address from the EEPROM, if it's available.  This is
1233  *  per-port, so set_lan_id() must be called before reading the addresses.
1234  *  set_lan_id() is called by identify_sfp(), but this cannot be relied
1235  *  upon for non-SFP connections, so we must call it here.
1236  **/
1237 s32 txgbe_get_san_mac_addr(struct txgbe_hw *hw, u8 *san_mac_addr)
1238 {
1239         u16 san_mac_data, san_mac_offset;
1240         u8 i;
1241         s32 err;
1242
1243         DEBUGFUNC("txgbe_get_san_mac_addr");
1244
1245         /*
1246          * First read the EEPROM pointer to see if the MAC addresses are
1247          * available. If they're not, no point in calling set_lan_id() here.
1248          */
1249         err = txgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
1250         if (err || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
1251                 goto san_mac_addr_out;
1252
1253         /* apply the port offset to the address offset */
1254         (hw->bus.func) ? (san_mac_offset += TXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
1255                          (san_mac_offset += TXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
1256         for (i = 0; i < 3; i++) {
1257                 err = hw->rom.read16(hw, san_mac_offset,
1258                                               &san_mac_data);
1259                 if (err) {
1260                         DEBUGOUT("eeprom read at offset %d failed",
1261                                  san_mac_offset);
1262                         goto san_mac_addr_out;
1263                 }
1264                 san_mac_addr[i * 2] = (u8)(san_mac_data);
1265                 san_mac_addr[i * 2 + 1] = (u8)(san_mac_data >> 8);
1266                 san_mac_offset++;
1267         }
1268         return 0;
1269
1270 san_mac_addr_out:
1271         /*
1272          * No addresses available in this EEPROM.  It's not an
1273          * error though, so just wipe the local address and return.
1274          */
1275         for (i = 0; i < 6; i++)
1276                 san_mac_addr[i] = 0xFF;
1277         return 0;
1278 }
1279
1280 /**
1281  *  txgbe_set_san_mac_addr - Write the SAN MAC address to the EEPROM
1282  *  @hw: pointer to hardware structure
1283  *  @san_mac_addr: SAN MAC address
1284  *
1285  *  Write a SAN MAC address to the EEPROM.
1286  **/
1287 s32 txgbe_set_san_mac_addr(struct txgbe_hw *hw, u8 *san_mac_addr)
1288 {
1289         s32 err;
1290         u16 san_mac_data, san_mac_offset;
1291         u8 i;
1292
1293         DEBUGFUNC("txgbe_set_san_mac_addr");
1294
1295         /* Look for SAN mac address pointer.  If not defined, return */
1296         err = txgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
1297         if (err || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
1298                 return TXGBE_ERR_NO_SAN_ADDR_PTR;
1299
1300         /* Apply the port offset to the address offset */
1301         (hw->bus.func) ? (san_mac_offset += TXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
1302                          (san_mac_offset += TXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
1303
1304         for (i = 0; i < 3; i++) {
1305                 san_mac_data = (u16)((u16)(san_mac_addr[i * 2 + 1]) << 8);
1306                 san_mac_data |= (u16)(san_mac_addr[i * 2]);
1307                 hw->rom.write16(hw, san_mac_offset, san_mac_data);
1308                 san_mac_offset++;
1309         }
1310
1311         return 0;
1312 }
1313
1314 /**
1315  *  txgbe_clear_vmdq - Disassociate a VMDq pool index from a rx address
1316  *  @hw: pointer to hardware struct
1317  *  @rar: receive address register index to disassociate
1318  *  @vmdq: VMDq pool index to remove from the rar
1319  **/
1320 s32 txgbe_clear_vmdq(struct txgbe_hw *hw, u32 rar, u32 vmdq)
1321 {
1322         u32 mpsar_lo, mpsar_hi;
1323         u32 rar_entries = hw->mac.num_rar_entries;
1324
1325         DEBUGFUNC("txgbe_clear_vmdq");
1326
1327         /* Make sure we are using a valid rar index range */
1328         if (rar >= rar_entries) {
1329                 DEBUGOUT("RAR index %d is out of range.\n", rar);
1330                 return TXGBE_ERR_INVALID_ARGUMENT;
1331         }
1332
1333         wr32(hw, TXGBE_ETHADDRIDX, rar);
1334         mpsar_lo = rd32(hw, TXGBE_ETHADDRASSL);
1335         mpsar_hi = rd32(hw, TXGBE_ETHADDRASSH);
1336
1337         if (TXGBE_REMOVED(hw->hw_addr))
1338                 goto done;
1339
1340         if (!mpsar_lo && !mpsar_hi)
1341                 goto done;
1342
1343         if (vmdq == BIT_MASK32) {
1344                 if (mpsar_lo) {
1345                         wr32(hw, TXGBE_ETHADDRASSL, 0);
1346                         mpsar_lo = 0;
1347                 }
1348                 if (mpsar_hi) {
1349                         wr32(hw, TXGBE_ETHADDRASSH, 0);
1350                         mpsar_hi = 0;
1351                 }
1352         } else if (vmdq < 32) {
1353                 mpsar_lo &= ~(1 << vmdq);
1354                 wr32(hw, TXGBE_ETHADDRASSL, mpsar_lo);
1355         } else {
1356                 mpsar_hi &= ~(1 << (vmdq - 32));
1357                 wr32(hw, TXGBE_ETHADDRASSH, mpsar_hi);
1358         }
1359
1360         /* was that the last pool using this rar? */
1361         if (mpsar_lo == 0 && mpsar_hi == 0 &&
1362             rar != 0 && rar != hw->mac.san_mac_rar_index)
1363                 hw->mac.clear_rar(hw, rar);
1364 done:
1365         return 0;
1366 }
1367
1368 /**
1369  *  txgbe_set_vmdq - Associate a VMDq pool index with a rx address
1370  *  @hw: pointer to hardware struct
1371  *  @rar: receive address register index to associate with a VMDq index
1372  *  @vmdq: VMDq pool index
1373  **/
1374 s32 txgbe_set_vmdq(struct txgbe_hw *hw, u32 rar, u32 vmdq)
1375 {
1376         u32 mpsar;
1377         u32 rar_entries = hw->mac.num_rar_entries;
1378
1379         DEBUGFUNC("txgbe_set_vmdq");
1380
1381         /* Make sure we are using a valid rar index range */
1382         if (rar >= rar_entries) {
1383                 DEBUGOUT("RAR index %d is out of range.\n", rar);
1384                 return TXGBE_ERR_INVALID_ARGUMENT;
1385         }
1386
1387         wr32(hw, TXGBE_ETHADDRIDX, rar);
1388         if (vmdq < 32) {
1389                 mpsar = rd32(hw, TXGBE_ETHADDRASSL);
1390                 mpsar |= 1 << vmdq;
1391                 wr32(hw, TXGBE_ETHADDRASSL, mpsar);
1392         } else {
1393                 mpsar = rd32(hw, TXGBE_ETHADDRASSH);
1394                 mpsar |= 1 << (vmdq - 32);
1395                 wr32(hw, TXGBE_ETHADDRASSH, mpsar);
1396         }
1397         return 0;
1398 }
1399
1400 /**
1401  *  txgbe_init_uta_tables - Initialize the Unicast Table Array
1402  *  @hw: pointer to hardware structure
1403  **/
1404 s32 txgbe_init_uta_tables(struct txgbe_hw *hw)
1405 {
1406         int i;
1407
1408         DEBUGFUNC("txgbe_init_uta_tables");
1409         DEBUGOUT(" Clearing UTA\n");
1410
1411         for (i = 0; i < 128; i++)
1412                 wr32(hw, TXGBE_UCADDRTBL(i), 0);
1413
1414         return 0;
1415 }
1416
1417 /**
1418  *  txgbe_find_vlvf_slot - find the vlanid or the first empty slot
1419  *  @hw: pointer to hardware structure
1420  *  @vlan: VLAN id to write to VLAN filter
1421  *  @vlvf_bypass: true to find vlanid only, false returns first empty slot if
1422  *                vlanid not found
1423  *
1424  *
1425  *  return the VLVF index where this VLAN id should be placed
1426  *
1427  **/
1428 s32 txgbe_find_vlvf_slot(struct txgbe_hw *hw, u32 vlan, bool vlvf_bypass)
1429 {
1430         s32 regindex, first_empty_slot;
1431         u32 bits;
1432
1433         /* short cut the special case */
1434         if (vlan == 0)
1435                 return 0;
1436
1437         /* if vlvf_bypass is set we don't want to use an empty slot, we
1438          * will simply bypass the VLVF if there are no entries present in the
1439          * VLVF that contain our VLAN
1440          */
1441         first_empty_slot = vlvf_bypass ? TXGBE_ERR_NO_SPACE : 0;
1442
1443         /* add VLAN enable bit for comparison */
1444         vlan |= TXGBE_PSRVLAN_EA;
1445
1446         /* Search for the vlan id in the VLVF entries. Save off the first empty
1447          * slot found along the way.
1448          *
1449          * pre-decrement loop covering (TXGBE_NUM_POOL - 1) .. 1
1450          */
1451         for (regindex = TXGBE_NUM_POOL; --regindex;) {
1452                 wr32(hw, TXGBE_PSRVLANIDX, regindex);
1453                 bits = rd32(hw, TXGBE_PSRVLAN);
1454                 if (bits == vlan)
1455                         return regindex;
1456                 if (!first_empty_slot && !bits)
1457                         first_empty_slot = regindex;
1458         }
1459
1460         /* If we are here then we didn't find the VLAN.  Return first empty
1461          * slot we found during our search, else error.
1462          */
1463         if (!first_empty_slot)
1464                 DEBUGOUT("No space in VLVF.\n");
1465
1466         return first_empty_slot ? first_empty_slot : TXGBE_ERR_NO_SPACE;
1467 }
1468
1469 /**
1470  *  txgbe_set_vfta - Set VLAN filter table
1471  *  @hw: pointer to hardware structure
1472  *  @vlan: VLAN id to write to VLAN filter
1473  *  @vind: VMDq output index that maps queue to VLAN id in VLVFB
1474  *  @vlan_on: boolean flag to turn on/off VLAN
1475  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
1476  *
1477  *  Turn on/off specified VLAN in the VLAN filter table.
1478  **/
1479 s32 txgbe_set_vfta(struct txgbe_hw *hw, u32 vlan, u32 vind,
1480                            bool vlan_on, bool vlvf_bypass)
1481 {
1482         u32 regidx, vfta_delta, vfta;
1483         s32 err;
1484
1485         DEBUGFUNC("txgbe_set_vfta");
1486
1487         if (vlan > 4095 || vind > 63)
1488                 return TXGBE_ERR_PARAM;
1489
1490         /*
1491          * this is a 2 part operation - first the VFTA, then the
1492          * VLVF and VLVFB if VT Mode is set
1493          * We don't write the VFTA until we know the VLVF part succeeded.
1494          */
1495
1496         /* Part 1
1497          * The VFTA is a bitstring made up of 128 32-bit registers
1498          * that enable the particular VLAN id, much like the MTA:
1499          *    bits[11-5]: which register
1500          *    bits[4-0]:  which bit in the register
1501          */
1502         regidx = vlan / 32;
1503         vfta_delta = 1 << (vlan % 32);
1504         vfta = rd32(hw, TXGBE_VLANTBL(regidx));
1505
1506         /*
1507          * vfta_delta represents the difference between the current value
1508          * of vfta and the value we want in the register.  Since the diff
1509          * is an XOR mask we can just update the vfta using an XOR
1510          */
1511         vfta_delta &= vlan_on ? ~vfta : vfta;
1512         vfta ^= vfta_delta;
1513
1514         /* Part 2
1515          * Call txgbe_set_vlvf to set VLVFB and VLVF
1516          */
1517         err = txgbe_set_vlvf(hw, vlan, vind, vlan_on, &vfta_delta,
1518                                          vfta, vlvf_bypass);
1519         if (err != 0) {
1520                 if (vlvf_bypass)
1521                         goto vfta_update;
1522                 return err;
1523         }
1524
1525 vfta_update:
1526         /* Update VFTA now that we are ready for traffic */
1527         if (vfta_delta)
1528                 wr32(hw, TXGBE_VLANTBL(regidx), vfta);
1529
1530         return 0;
1531 }
1532
1533 /**
1534  *  txgbe_set_vlvf - Set VLAN Pool Filter
1535  *  @hw: pointer to hardware structure
1536  *  @vlan: VLAN id to write to VLAN filter
1537  *  @vind: VMDq output index that maps queue to VLAN id in PSRVLANPLM
1538  *  @vlan_on: boolean flag to turn on/off VLAN in PSRVLAN
1539  *  @vfta_delta: pointer to the difference between the current value
1540  *               of PSRVLANPLM and the desired value
1541  *  @vfta: the desired value of the VFTA
1542  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
1543  *
1544  *  Turn on/off specified bit in VLVF table.
1545  **/
1546 s32 txgbe_set_vlvf(struct txgbe_hw *hw, u32 vlan, u32 vind,
1547                            bool vlan_on, u32 *vfta_delta, u32 vfta,
1548                            bool vlvf_bypass)
1549 {
1550         u32 bits;
1551         u32 portctl;
1552         s32 vlvf_index;
1553
1554         DEBUGFUNC("txgbe_set_vlvf");
1555
1556         if (vlan > 4095 || vind > 63)
1557                 return TXGBE_ERR_PARAM;
1558
1559         /* If VT Mode is set
1560          *   Either vlan_on
1561          *     make sure the vlan is in PSRVLAN
1562          *     set the vind bit in the matching PSRVLANPLM
1563          *   Or !vlan_on
1564          *     clear the pool bit and possibly the vind
1565          */
1566         portctl = rd32(hw, TXGBE_PORTCTL);
1567         if (!(portctl & TXGBE_PORTCTL_NUMVT_MASK))
1568                 return 0;
1569
1570         vlvf_index = txgbe_find_vlvf_slot(hw, vlan, vlvf_bypass);
1571         if (vlvf_index < 0)
1572                 return vlvf_index;
1573
1574         wr32(hw, TXGBE_PSRVLANIDX, vlvf_index);
1575         bits = rd32(hw, TXGBE_PSRVLANPLM(vind / 32));
1576
1577         /* set the pool bit */
1578         bits |= 1 << (vind % 32);
1579         if (vlan_on)
1580                 goto vlvf_update;
1581
1582         /* clear the pool bit */
1583         bits ^= 1 << (vind % 32);
1584
1585         if (!bits &&
1586             !rd32(hw, TXGBE_PSRVLANPLM(vind / 32))) {
1587                 /* Clear PSRVLANPLM first, then disable PSRVLAN. Otherwise
1588                  * we run the risk of stray packets leaking into
1589                  * the PF via the default pool
1590                  */
1591                 if (*vfta_delta)
1592                         wr32(hw, TXGBE_PSRVLANPLM(vlan / 32), vfta);
1593
1594                 /* disable VLVF and clear remaining bit from pool */
1595                 wr32(hw, TXGBE_PSRVLAN, 0);
1596                 wr32(hw, TXGBE_PSRVLANPLM(vind / 32), 0);
1597
1598                 return 0;
1599         }
1600
1601         /* If there are still bits set in the PSRVLANPLM registers
1602          * for the VLAN ID indicated we need to see if the
1603          * caller is requesting that we clear the PSRVLANPLM entry bit.
1604          * If the caller has requested that we clear the PSRVLANPLM
1605          * entry bit but there are still pools/VFs using this VLAN
1606          * ID entry then ignore the request.  We're not worried
1607          * about the case where we're turning the PSRVLANPLM VLAN ID
1608          * entry bit on, only when requested to turn it off as
1609          * there may be multiple pools and/or VFs using the
1610          * VLAN ID entry.  In that case we cannot clear the
1611          * PSRVLANPLM bit until all pools/VFs using that VLAN ID have also
1612          * been cleared.  This will be indicated by "bits" being
1613          * zero.
1614          */
1615         *vfta_delta = 0;
1616
1617 vlvf_update:
1618         /* record pool change and enable VLAN ID if not already enabled */
1619         wr32(hw, TXGBE_PSRVLANPLM(vind / 32), bits);
1620         wr32(hw, TXGBE_PSRVLAN, TXGBE_PSRVLAN_EA | vlan);
1621
1622         return 0;
1623 }
1624
1625 /**
1626  *  txgbe_clear_vfta - Clear VLAN filter table
1627  *  @hw: pointer to hardware structure
1628  *
1629  *  Clears the VLAN filer table, and the VMDq index associated with the filter
1630  **/
1631 s32 txgbe_clear_vfta(struct txgbe_hw *hw)
1632 {
1633         u32 offset;
1634
1635         DEBUGFUNC("txgbe_clear_vfta");
1636
1637         for (offset = 0; offset < hw->mac.vft_size; offset++)
1638                 wr32(hw, TXGBE_VLANTBL(offset), 0);
1639
1640         for (offset = 0; offset < TXGBE_NUM_POOL; offset++) {
1641                 wr32(hw, TXGBE_PSRVLANIDX, offset);
1642                 wr32(hw, TXGBE_PSRVLAN, 0);
1643                 wr32(hw, TXGBE_PSRVLANPLM(0), 0);
1644                 wr32(hw, TXGBE_PSRVLANPLM(1), 0);
1645         }
1646
1647         return 0;
1648 }
1649
1650 /**
1651  *  txgbe_need_crosstalk_fix - Determine if we need to do cross talk fix
1652  *  @hw: pointer to hardware structure
1653  *
1654  *  Contains the logic to identify if we need to verify link for the
1655  *  crosstalk fix
1656  **/
1657 static bool txgbe_need_crosstalk_fix(struct txgbe_hw *hw)
1658 {
1659         /* Does FW say we need the fix */
1660         if (!hw->need_crosstalk_fix)
1661                 return false;
1662
1663         /* Only consider SFP+ PHYs i.e. media type fiber */
1664         switch (hw->phy.media_type) {
1665         case txgbe_media_type_fiber:
1666         case txgbe_media_type_fiber_qsfp:
1667                 break;
1668         default:
1669                 return false;
1670         }
1671
1672         return true;
1673 }
1674
1675 /**
1676  *  txgbe_check_mac_link - Determine link and speed status
1677  *  @hw: pointer to hardware structure
1678  *  @speed: pointer to link speed
1679  *  @link_up: true when link is up
1680  *  @link_up_wait_to_complete: bool used to wait for link up or not
1681  *
1682  *  Reads the links register to determine if link is up and the current speed
1683  **/
1684 s32 txgbe_check_mac_link(struct txgbe_hw *hw, u32 *speed,
1685                                  bool *link_up, bool link_up_wait_to_complete)
1686 {
1687         u32 links_reg, links_orig;
1688         u32 i;
1689
1690         DEBUGFUNC("txgbe_check_mac_link");
1691
1692         /* If Crosstalk fix enabled do the sanity check of making sure
1693          * the SFP+ cage is full.
1694          */
1695         if (txgbe_need_crosstalk_fix(hw)) {
1696                 u32 sfp_cage_full;
1697
1698                 switch (hw->mac.type) {
1699                 case txgbe_mac_raptor:
1700                         sfp_cage_full = !rd32m(hw, TXGBE_GPIODATA,
1701                                         TXGBE_GPIOBIT_2);
1702                         break;
1703                 default:
1704                         /* sanity check - No SFP+ devices here */
1705                         sfp_cage_full = false;
1706                         break;
1707                 }
1708
1709                 if (!sfp_cage_full) {
1710                         *link_up = false;
1711                         *speed = TXGBE_LINK_SPEED_UNKNOWN;
1712                         return 0;
1713                 }
1714         }
1715
1716         /* clear the old state */
1717         links_orig = rd32(hw, TXGBE_PORTSTAT);
1718
1719         links_reg = rd32(hw, TXGBE_PORTSTAT);
1720
1721         if (links_orig != links_reg) {
1722                 DEBUGOUT("LINKS changed from %08X to %08X\n",
1723                           links_orig, links_reg);
1724         }
1725
1726         if (link_up_wait_to_complete) {
1727                 for (i = 0; i < hw->mac.max_link_up_time; i++) {
1728                         if (!(links_reg & TXGBE_PORTSTAT_UP)) {
1729                                 *link_up = false;
1730                         } else {
1731                                 *link_up = true;
1732                                 break;
1733                         }
1734                         msec_delay(100);
1735                         links_reg = rd32(hw, TXGBE_PORTSTAT);
1736                 }
1737         } else {
1738                 if (links_reg & TXGBE_PORTSTAT_UP)
1739                         *link_up = true;
1740                 else
1741                         *link_up = false;
1742         }
1743
1744         switch (links_reg & TXGBE_PORTSTAT_BW_MASK) {
1745         case TXGBE_PORTSTAT_BW_10G:
1746                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
1747                 break;
1748         case TXGBE_PORTSTAT_BW_1G:
1749                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
1750                 break;
1751         case TXGBE_PORTSTAT_BW_100M:
1752                 *speed = TXGBE_LINK_SPEED_100M_FULL;
1753                 break;
1754         default:
1755                 *speed = TXGBE_LINK_SPEED_UNKNOWN;
1756         }
1757
1758         return 0;
1759 }
1760
1761 /**
1762  *  txgbe_get_wwn_prefix - Get alternative WWNN/WWPN prefix from
1763  *  the EEPROM
1764  *  @hw: pointer to hardware structure
1765  *  @wwnn_prefix: the alternative WWNN prefix
1766  *  @wwpn_prefix: the alternative WWPN prefix
1767  *
1768  *  This function will read the EEPROM from the alternative SAN MAC address
1769  *  block to check the support for the alternative WWNN/WWPN prefix support.
1770  **/
1771 s32 txgbe_get_wwn_prefix(struct txgbe_hw *hw, u16 *wwnn_prefix,
1772                                  u16 *wwpn_prefix)
1773 {
1774         u16 offset, caps;
1775         u16 alt_san_mac_blk_offset;
1776
1777         DEBUGFUNC("txgbe_get_wwn_prefix");
1778
1779         /* clear output first */
1780         *wwnn_prefix = 0xFFFF;
1781         *wwpn_prefix = 0xFFFF;
1782
1783         /* check if alternative SAN MAC is supported */
1784         offset = TXGBE_ALT_SAN_MAC_ADDR_BLK_PTR;
1785         if (hw->rom.readw_sw(hw, offset, &alt_san_mac_blk_offset))
1786                 goto wwn_prefix_err;
1787
1788         if (alt_san_mac_blk_offset == 0 || alt_san_mac_blk_offset == 0xFFFF)
1789                 goto wwn_prefix_out;
1790
1791         /* check capability in alternative san mac address block */
1792         offset = alt_san_mac_blk_offset + TXGBE_ALT_SAN_MAC_ADDR_CAPS_OFFSET;
1793         if (hw->rom.read16(hw, offset, &caps))
1794                 goto wwn_prefix_err;
1795         if (!(caps & TXGBE_ALT_SAN_MAC_ADDR_CAPS_ALTWWN))
1796                 goto wwn_prefix_out;
1797
1798         /* get the corresponding prefix for WWNN/WWPN */
1799         offset = alt_san_mac_blk_offset + TXGBE_ALT_SAN_MAC_ADDR_WWNN_OFFSET;
1800         if (hw->rom.read16(hw, offset, wwnn_prefix))
1801                 DEBUGOUT("eeprom read at offset %d failed", offset);
1802
1803         offset = alt_san_mac_blk_offset + TXGBE_ALT_SAN_MAC_ADDR_WWPN_OFFSET;
1804         if (hw->rom.read16(hw, offset, wwpn_prefix))
1805                 goto wwn_prefix_err;
1806
1807 wwn_prefix_out:
1808         return 0;
1809
1810 wwn_prefix_err:
1811         DEBUGOUT("eeprom read at offset %d failed", offset);
1812         return 0;
1813 }
1814
1815 /**
1816  *  txgbe_set_mac_anti_spoofing - Enable/Disable MAC anti-spoofing
1817  *  @hw: pointer to hardware structure
1818  *  @enable: enable or disable switch for MAC anti-spoofing
1819  *  @vf: Virtual Function pool - VF Pool to set for MAC anti-spoofing
1820  *
1821  **/
1822 void txgbe_set_mac_anti_spoofing(struct txgbe_hw *hw, bool enable, int vf)
1823 {
1824         int vf_target_reg = vf >> 3;
1825         int vf_target_shift = vf % 8;
1826         u32 pfvfspoof;
1827
1828         pfvfspoof = rd32(hw, TXGBE_POOLTXASMAC(vf_target_reg));
1829         if (enable)
1830                 pfvfspoof |= (1 << vf_target_shift);
1831         else
1832                 pfvfspoof &= ~(1 << vf_target_shift);
1833         wr32(hw, TXGBE_POOLTXASMAC(vf_target_reg), pfvfspoof);
1834 }
1835
1836 /**
1837  * txgbe_set_ethertype_anti_spoofing - Configure Ethertype anti-spoofing
1838  * @hw: pointer to hardware structure
1839  * @enable: enable or disable switch for Ethertype anti-spoofing
1840  * @vf: Virtual Function pool - VF Pool to set for Ethertype anti-spoofing
1841  *
1842  **/
1843 void txgbe_set_ethertype_anti_spoofing(struct txgbe_hw *hw,
1844                 bool enable, int vf)
1845 {
1846         int vf_target_reg = vf >> 3;
1847         int vf_target_shift = vf % 8;
1848         u32 pfvfspoof;
1849
1850         pfvfspoof = rd32(hw, TXGBE_POOLTXASET(vf_target_reg));
1851         if (enable)
1852                 pfvfspoof |= (1 << vf_target_shift);
1853         else
1854                 pfvfspoof &= ~(1 << vf_target_shift);
1855         wr32(hw, TXGBE_POOLTXASET(vf_target_reg), pfvfspoof);
1856 }
1857
1858 /**
1859  *  txgbe_get_device_caps - Get additional device capabilities
1860  *  @hw: pointer to hardware structure
1861  *  @device_caps: the EEPROM word with the extra device capabilities
1862  *
1863  *  This function will read the EEPROM location for the device capabilities,
1864  *  and return the word through device_caps.
1865  **/
1866 s32 txgbe_get_device_caps(struct txgbe_hw *hw, u16 *device_caps)
1867 {
1868         DEBUGFUNC("txgbe_get_device_caps");
1869
1870         hw->rom.readw_sw(hw, TXGBE_DEVICE_CAPS, device_caps);
1871
1872         return 0;
1873 }
1874
1875 /**
1876  * txgbe_set_pba - Initialize Rx packet buffer
1877  * @hw: pointer to hardware structure
1878  * @num_pb: number of packet buffers to allocate
1879  * @headroom: reserve n KB of headroom
1880  * @strategy: packet buffer allocation strategy
1881  **/
1882 void txgbe_set_pba(struct txgbe_hw *hw, int num_pb, u32 headroom,
1883                              int strategy)
1884 {
1885         u32 pbsize = hw->mac.rx_pb_size;
1886         int i = 0;
1887         u32 rxpktsize, txpktsize, txpbthresh;
1888
1889         UNREFERENCED_PARAMETER(hw);
1890
1891         /* Reserve headroom */
1892         pbsize -= headroom;
1893
1894         if (!num_pb)
1895                 num_pb = 1;
1896
1897         /* Divide remaining packet buffer space amongst the number of packet
1898          * buffers requested using supplied strategy.
1899          */
1900         switch (strategy) {
1901         case PBA_STRATEGY_WEIGHTED:
1902                 /* txgbe_dcb_pba_80_48 strategy weight first half of packet
1903                  * buffer with 5/8 of the packet buffer space.
1904                  */
1905                 rxpktsize = (pbsize * 5) / (num_pb * 4);
1906                 pbsize -= rxpktsize * (num_pb / 2);
1907                 rxpktsize <<= 10;
1908                 for (; i < (num_pb / 2); i++)
1909                         wr32(hw, TXGBE_PBRXSIZE(i), rxpktsize);
1910                 /* fall through - configure remaining packet buffers */
1911         case PBA_STRATEGY_EQUAL:
1912                 rxpktsize = (pbsize / (num_pb - i));
1913                 rxpktsize <<= 10;
1914                 for (; i < num_pb; i++)
1915                         wr32(hw, TXGBE_PBRXSIZE(i), rxpktsize);
1916                 break;
1917         default:
1918                 break;
1919         }
1920
1921         /* Only support an equally distributed Tx packet buffer strategy. */
1922         txpktsize = TXGBE_PBTXSIZE_MAX / num_pb;
1923         txpbthresh = (txpktsize / 1024) - TXGBE_TXPKT_SIZE_MAX;
1924         for (i = 0; i < num_pb; i++) {
1925                 wr32(hw, TXGBE_PBTXSIZE(i), txpktsize);
1926                 wr32(hw, TXGBE_PBTXDMATH(i), txpbthresh);
1927         }
1928
1929         /* Clear unused TCs, if any, to zero buffer size*/
1930         for (; i < TXGBE_MAX_UP; i++) {
1931                 wr32(hw, TXGBE_PBRXSIZE(i), 0);
1932                 wr32(hw, TXGBE_PBTXSIZE(i), 0);
1933                 wr32(hw, TXGBE_PBTXDMATH(i), 0);
1934         }
1935 }
1936
1937 /**
1938  * txgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
1939  * @hw: pointer to the hardware structure
1940  *
1941  * The MACs can experience issues if TX work is still pending
1942  * when a reset occurs.  This function prevents this by flushing the PCIe
1943  * buffers on the system.
1944  **/
1945 void txgbe_clear_tx_pending(struct txgbe_hw *hw)
1946 {
1947         u32 hlreg0, i, poll;
1948
1949         /*
1950          * If double reset is not requested then all transactions should
1951          * already be clear and as such there is no work to do
1952          */
1953         if (!(hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
1954                 return;
1955
1956         hlreg0 = rd32(hw, TXGBE_PSRCTL);
1957         wr32(hw, TXGBE_PSRCTL, hlreg0 | TXGBE_PSRCTL_LBENA);
1958
1959         /* Wait for a last completion before clearing buffers */
1960         txgbe_flush(hw);
1961         msec_delay(3);
1962
1963         /*
1964          * Before proceeding, make sure that the PCIe block does not have
1965          * transactions pending.
1966          */
1967         poll = (800 * 11) / 10;
1968         for (i = 0; i < poll; i++)
1969                 usec_delay(100);
1970
1971         /* Flush all writes and allow 20usec for all transactions to clear */
1972         txgbe_flush(hw);
1973         usec_delay(20);
1974
1975         /* restore previous register values */
1976         wr32(hw, TXGBE_PSRCTL, hlreg0);
1977 }
1978
1979 /**
1980  *  txgbe_get_thermal_sensor_data - Gathers thermal sensor data
1981  *  @hw: pointer to hardware structure
1982  *
1983  *  Returns the thermal sensor data structure
1984  **/
1985 s32 txgbe_get_thermal_sensor_data(struct txgbe_hw *hw)
1986 {
1987         struct txgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
1988         s64 tsv;
1989         u32 ts_stat;
1990
1991         DEBUGFUNC("txgbe_get_thermal_sensor_data");
1992
1993         /* Only support thermal sensors attached to physical port 0 */
1994         if (hw->bus.lan_id != 0)
1995                 return TXGBE_NOT_IMPLEMENTED;
1996
1997         ts_stat = rd32(hw, TXGBE_TSSTAT);
1998         tsv = (s64)TXGBE_TSSTAT_DATA(ts_stat);
1999         tsv = tsv > 1200 ? tsv : 1200;
2000         tsv = -(48380 << 8) / 1000
2001                 + tsv * (31020 << 8) / 100000
2002                 - tsv * tsv * (18201 << 8) / 100000000
2003                 + tsv * tsv * tsv * (81542 << 8) / 1000000000000
2004                 - tsv * tsv * tsv * tsv * (16743 << 8) / 1000000000000000;
2005         tsv >>= 8;
2006
2007         data->sensor[0].temp = (s16)tsv;
2008
2009         return 0;
2010 }
2011
2012 /**
2013  *  txgbe_init_thermal_sensor_thresh - Inits thermal sensor thresholds
2014  *  @hw: pointer to hardware structure
2015  *
2016  *  Inits the thermal sensor thresholds according to the NVM map
2017  *  and save off the threshold and location values into mac.thermal_sensor_data
2018  **/
2019 s32 txgbe_init_thermal_sensor_thresh(struct txgbe_hw *hw)
2020 {
2021         struct txgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
2022
2023         DEBUGFUNC("txgbe_init_thermal_sensor_thresh");
2024
2025         memset(data, 0, sizeof(struct txgbe_thermal_sensor_data));
2026
2027         if (hw->bus.lan_id != 0)
2028                 return TXGBE_NOT_IMPLEMENTED;
2029
2030         wr32(hw, TXGBE_TSCTRL, TXGBE_TSCTRL_EVALMD);
2031         wr32(hw, TXGBE_TSINTR,
2032                 TXGBE_TSINTR_AEN | TXGBE_TSINTR_DEN);
2033         wr32(hw, TXGBE_TSEN, TXGBE_TSEN_ENA);
2034
2035
2036         data->sensor[0].alarm_thresh = 100;
2037         wr32(hw, TXGBE_TSATHRE, 677);
2038         data->sensor[0].dalarm_thresh = 90;
2039         wr32(hw, TXGBE_TSDTHRE, 614);
2040
2041         return 0;
2042 }
2043
2044 void txgbe_disable_rx(struct txgbe_hw *hw)
2045 {
2046         u32 pfdtxgswc;
2047
2048         pfdtxgswc = rd32(hw, TXGBE_PSRCTL);
2049         if (pfdtxgswc & TXGBE_PSRCTL_LBENA) {
2050                 pfdtxgswc &= ~TXGBE_PSRCTL_LBENA;
2051                 wr32(hw, TXGBE_PSRCTL, pfdtxgswc);
2052                 hw->mac.set_lben = true;
2053         } else {
2054                 hw->mac.set_lben = false;
2055         }
2056
2057         wr32m(hw, TXGBE_PBRXCTL, TXGBE_PBRXCTL_ENA, 0);
2058         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_ENA, 0);
2059 }
2060
2061 void txgbe_enable_rx(struct txgbe_hw *hw)
2062 {
2063         u32 pfdtxgswc;
2064
2065         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_ENA, TXGBE_MACRXCFG_ENA);
2066         wr32m(hw, TXGBE_PBRXCTL, TXGBE_PBRXCTL_ENA, TXGBE_PBRXCTL_ENA);
2067
2068         if (hw->mac.set_lben) {
2069                 pfdtxgswc = rd32(hw, TXGBE_PSRCTL);
2070                 pfdtxgswc |= TXGBE_PSRCTL_LBENA;
2071                 wr32(hw, TXGBE_PSRCTL, pfdtxgswc);
2072                 hw->mac.set_lben = false;
2073         }
2074 }
2075
2076 /**
2077  *  txgbe_setup_mac_link_multispeed_fiber - Set MAC link speed
2078  *  @hw: pointer to hardware structure
2079  *  @speed: new link speed
2080  *  @autoneg_wait_to_complete: true when waiting for completion is needed
2081  *
2082  *  Set the link speed in the MAC and/or PHY register and restarts link.
2083  **/
2084 s32 txgbe_setup_mac_link_multispeed_fiber(struct txgbe_hw *hw,
2085                                           u32 speed,
2086                                           bool autoneg_wait_to_complete)
2087 {
2088         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
2089         u32 highest_link_speed = TXGBE_LINK_SPEED_UNKNOWN;
2090         s32 status = 0;
2091         u32 speedcnt = 0;
2092         u32 i = 0;
2093         bool autoneg, link_up = false;
2094
2095         DEBUGFUNC("txgbe_setup_mac_link_multispeed_fiber");
2096
2097         /* Mask off requested but non-supported speeds */
2098         status = hw->mac.get_link_capabilities(hw, &link_speed, &autoneg);
2099         if (status != 0)
2100                 return status;
2101
2102         speed &= link_speed;
2103
2104         /* Try each speed one by one, highest priority first.  We do this in
2105          * software because 10Gb fiber doesn't support speed autonegotiation.
2106          */
2107         if (speed & TXGBE_LINK_SPEED_10GB_FULL) {
2108                 speedcnt++;
2109                 highest_link_speed = TXGBE_LINK_SPEED_10GB_FULL;
2110
2111                 /* Set the module link speed */
2112                 switch (hw->phy.media_type) {
2113                 case txgbe_media_type_fiber:
2114                         hw->mac.set_rate_select_speed(hw,
2115                                 TXGBE_LINK_SPEED_10GB_FULL);
2116                         break;
2117                 case txgbe_media_type_fiber_qsfp:
2118                         /* QSFP module automatically detects MAC link speed */
2119                         break;
2120                 default:
2121                         DEBUGOUT("Unexpected media type.\n");
2122                         break;
2123                 }
2124
2125                 /* Allow module to change analog characteristics (1G->10G) */
2126                 msec_delay(40);
2127
2128                 status = hw->mac.setup_mac_link(hw,
2129                                 TXGBE_LINK_SPEED_10GB_FULL,
2130                                 autoneg_wait_to_complete);
2131                 if (status != 0)
2132                         return status;
2133
2134                 /* Flap the Tx laser if it has not already been done */
2135                 hw->mac.flap_tx_laser(hw);
2136
2137                 /* Wait for the controller to acquire link.  Per IEEE 802.3ap,
2138                  * Section 73.10.2, we may have to wait up to 500ms if KR is
2139                  * attempted.  uses the same timing for 10g SFI.
2140                  */
2141                 for (i = 0; i < 5; i++) {
2142                         /* Wait for the link partner to also set speed */
2143                         msec_delay(100);
2144
2145                         /* If we have link, just jump out */
2146                         status = hw->mac.check_link(hw, &link_speed,
2147                                 &link_up, false);
2148                         if (status != 0)
2149                                 return status;
2150
2151                         if (link_up)
2152                                 goto out;
2153                 }
2154         }
2155
2156         if (speed & TXGBE_LINK_SPEED_1GB_FULL) {
2157                 speedcnt++;
2158                 if (highest_link_speed == TXGBE_LINK_SPEED_UNKNOWN)
2159                         highest_link_speed = TXGBE_LINK_SPEED_1GB_FULL;
2160
2161                 /* Set the module link speed */
2162                 switch (hw->phy.media_type) {
2163                 case txgbe_media_type_fiber:
2164                         hw->mac.set_rate_select_speed(hw,
2165                                 TXGBE_LINK_SPEED_1GB_FULL);
2166                         break;
2167                 case txgbe_media_type_fiber_qsfp:
2168                         /* QSFP module automatically detects link speed */
2169                         break;
2170                 default:
2171                         DEBUGOUT("Unexpected media type.\n");
2172                         break;
2173                 }
2174
2175                 /* Allow module to change analog characteristics (10G->1G) */
2176                 msec_delay(40);
2177
2178                 status = hw->mac.setup_mac_link(hw,
2179                                 TXGBE_LINK_SPEED_1GB_FULL,
2180                                 autoneg_wait_to_complete);
2181                 if (status != 0)
2182                         return status;
2183
2184                 /* Flap the Tx laser if it has not already been done */
2185                 hw->mac.flap_tx_laser(hw);
2186
2187                 /* Wait for the link partner to also set speed */
2188                 msec_delay(100);
2189
2190                 /* If we have link, just jump out */
2191                 status = hw->mac.check_link(hw, &link_speed, &link_up, false);
2192                 if (status != 0)
2193                         return status;
2194
2195                 if (link_up)
2196                         goto out;
2197         }
2198
2199         /* We didn't get link.  Configure back to the highest speed we tried,
2200          * (if there was more than one).  We call ourselves back with just the
2201          * single highest speed that the user requested.
2202          */
2203         if (speedcnt > 1)
2204                 status = txgbe_setup_mac_link_multispeed_fiber(hw,
2205                                                       highest_link_speed,
2206                                                       autoneg_wait_to_complete);
2207
2208 out:
2209         /* Set autoneg_advertised value based on input link speed */
2210         hw->phy.autoneg_advertised = 0;
2211
2212         if (speed & TXGBE_LINK_SPEED_10GB_FULL)
2213                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_10GB_FULL;
2214
2215         if (speed & TXGBE_LINK_SPEED_1GB_FULL)
2216                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_1GB_FULL;
2217
2218         return status;
2219 }
2220
2221 /**
2222  *  txgbe_init_shared_code - Initialize the shared code
2223  *  @hw: pointer to hardware structure
2224  *
2225  *  This will assign function pointers and assign the MAC type and PHY code.
2226  *  Does not touch the hardware. This function must be called prior to any
2227  *  other function in the shared code. The txgbe_hw structure should be
2228  *  memset to 0 prior to calling this function.  The following fields in
2229  *  hw structure should be filled in prior to calling this function:
2230  *  hw_addr, back, device_id, vendor_id, subsystem_device_id,
2231  *  subsystem_vendor_id, and revision_id
2232  **/
2233 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
2234 {
2235         s32 status;
2236
2237         DEBUGFUNC("txgbe_init_shared_code");
2238
2239         /*
2240          * Set the mac type
2241          */
2242         txgbe_set_mac_type(hw);
2243
2244         txgbe_init_ops_dummy(hw);
2245         switch (hw->mac.type) {
2246         case txgbe_mac_raptor:
2247                 status = txgbe_init_ops_pf(hw);
2248                 break;
2249         default:
2250                 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
2251                 break;
2252         }
2253         hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
2254
2255         hw->bus.set_lan_id(hw);
2256
2257         return status;
2258 }
2259
2260 /**
2261  *  txgbe_set_mac_type - Sets MAC type
2262  *  @hw: pointer to the HW structure
2263  *
2264  *  This function sets the mac type of the adapter based on the
2265  *  vendor ID and device ID stored in the hw structure.
2266  **/
2267 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
2268 {
2269         s32 err = 0;
2270
2271         DEBUGFUNC("txgbe_set_mac_type");
2272
2273         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
2274                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
2275                 return TXGBE_ERR_DEVICE_NOT_SUPPORTED;
2276         }
2277
2278         switch (hw->device_id) {
2279         case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
2280                 hw->phy.media_type = txgbe_media_type_backplane;
2281                 hw->mac.type = txgbe_mac_raptor;
2282                 break;
2283         case TXGBE_DEV_ID_RAPTOR_XAUI:
2284         case TXGBE_DEV_ID_RAPTOR_SGMII:
2285                 hw->phy.media_type = txgbe_media_type_copper;
2286                 hw->mac.type = txgbe_mac_raptor;
2287                 break;
2288         case TXGBE_DEV_ID_RAPTOR_SFP:
2289         case TXGBE_DEV_ID_WX1820_SFP:
2290                 hw->phy.media_type = txgbe_media_type_fiber;
2291                 hw->mac.type = txgbe_mac_raptor;
2292                 break;
2293         case TXGBE_DEV_ID_RAPTOR_QSFP:
2294                 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
2295                 hw->mac.type = txgbe_mac_raptor;
2296                 break;
2297         case TXGBE_DEV_ID_RAPTOR_VF:
2298         case TXGBE_DEV_ID_RAPTOR_VF_HV:
2299                 hw->phy.media_type = txgbe_media_type_virtual;
2300                 hw->mac.type = txgbe_mac_raptor_vf;
2301                 break;
2302         default:
2303                 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
2304                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
2305                 break;
2306         }
2307
2308         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
2309                   hw->mac.type, hw->phy.media_type, err);
2310         return err;
2311 }
2312
2313 void txgbe_init_mac_link_ops(struct txgbe_hw *hw)
2314 {
2315         struct txgbe_mac_info *mac = &hw->mac;
2316
2317         DEBUGFUNC("txgbe_init_mac_link_ops");
2318
2319         /*
2320          * enable the laser control functions for SFP+ fiber
2321          * and MNG not enabled
2322          */
2323         if (hw->phy.media_type == txgbe_media_type_fiber &&
2324             !txgbe_mng_enabled(hw)) {
2325                 mac->disable_tx_laser =
2326                         txgbe_disable_tx_laser_multispeed_fiber;
2327                 mac->enable_tx_laser =
2328                         txgbe_enable_tx_laser_multispeed_fiber;
2329                 mac->flap_tx_laser =
2330                         txgbe_flap_tx_laser_multispeed_fiber;
2331         }
2332
2333         if ((hw->phy.media_type == txgbe_media_type_fiber ||
2334              hw->phy.media_type == txgbe_media_type_fiber_qsfp) &&
2335             hw->phy.multispeed_fiber) {
2336                 /* Set up dual speed SFP+ support */
2337                 mac->setup_link = txgbe_setup_mac_link_multispeed_fiber;
2338                 mac->setup_mac_link = txgbe_setup_mac_link;
2339                 mac->set_rate_select_speed = txgbe_set_hard_rate_select_speed;
2340         } else if ((hw->phy.media_type == txgbe_media_type_backplane) &&
2341                     (hw->phy.smart_speed == txgbe_smart_speed_auto ||
2342                      hw->phy.smart_speed == txgbe_smart_speed_on) &&
2343                      !txgbe_verify_lesm_fw_enabled_raptor(hw)) {
2344                 mac->setup_link = txgbe_setup_mac_link_smartspeed;
2345         } else {
2346                 mac->setup_link = txgbe_setup_mac_link;
2347         }
2348 }
2349
2350 /**
2351  *  txgbe_init_phy_raptor - PHY/SFP specific init
2352  *  @hw: pointer to hardware structure
2353  *
2354  *  Initialize any function pointers that were not able to be
2355  *  set during init_shared_code because the PHY/SFP type was
2356  *  not known.  Perform the SFP init if necessary.
2357  *
2358  **/
2359 s32 txgbe_init_phy_raptor(struct txgbe_hw *hw)
2360 {
2361         struct txgbe_mac_info *mac = &hw->mac;
2362         struct txgbe_phy_info *phy = &hw->phy;
2363         s32 err = 0;
2364
2365         DEBUGFUNC("txgbe_init_phy_raptor");
2366
2367         if (hw->device_id == TXGBE_DEV_ID_RAPTOR_QSFP) {
2368                 /* Store flag indicating I2C bus access control unit. */
2369                 hw->phy.qsfp_shared_i2c_bus = TRUE;
2370
2371                 /* Initialize access to QSFP+ I2C bus */
2372                 txgbe_flush(hw);
2373         }
2374
2375         /* Identify the PHY or SFP module */
2376         err = phy->identify(hw);
2377         if (err == TXGBE_ERR_SFP_NOT_SUPPORTED)
2378                 goto init_phy_ops_out;
2379
2380         /* Setup function pointers based on detected SFP module and speeds */
2381         txgbe_init_mac_link_ops(hw);
2382
2383         /* If copper media, overwrite with copper function pointers */
2384         if (phy->media_type == txgbe_media_type_copper) {
2385                 mac->setup_link = txgbe_setup_copper_link_raptor;
2386                 mac->get_link_capabilities =
2387                                   txgbe_get_copper_link_capabilities;
2388         }
2389
2390         /* Set necessary function pointers based on PHY type */
2391         switch (hw->phy.type) {
2392         case txgbe_phy_tn:
2393                 phy->setup_link = txgbe_setup_phy_link_tnx;
2394                 phy->check_link = txgbe_check_phy_link_tnx;
2395                 break;
2396         default:
2397                 break;
2398         }
2399
2400 init_phy_ops_out:
2401         return err;
2402 }
2403
2404 s32 txgbe_setup_sfp_modules(struct txgbe_hw *hw)
2405 {
2406         s32 err = 0;
2407
2408         DEBUGFUNC("txgbe_setup_sfp_modules");
2409
2410         if (hw->phy.sfp_type == txgbe_sfp_type_unknown)
2411                 return 0;
2412
2413         txgbe_init_mac_link_ops(hw);
2414
2415         /* PHY config will finish before releasing the semaphore */
2416         err = hw->mac.acquire_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
2417         if (err != 0)
2418                 return TXGBE_ERR_SWFW_SYNC;
2419
2420         /* Release the semaphore */
2421         hw->mac.release_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
2422
2423         /* Delay obtaining semaphore again to allow FW access
2424          * prot_autoc_write uses the semaphore too.
2425          */
2426         msec_delay(hw->rom.semaphore_delay);
2427
2428         if (err) {
2429                 DEBUGOUT("sfp module setup not complete\n");
2430                 return TXGBE_ERR_SFP_SETUP_NOT_COMPLETE;
2431         }
2432
2433         return err;
2434 }
2435
2436 /**
2437  *  txgbe_prot_autoc_read_raptor - Hides MAC differences needed for AUTOC read
2438  *  @hw: pointer to hardware structure
2439  *  @locked: Return the if we locked for this read.
2440  *  @value: Value we read from AUTOC
2441  *
2442  *  For this part we need to wrap read-modify-writes with a possible
2443  *  FW/SW lock.  It is assumed this lock will be freed with the next
2444  *  prot_autoc_write_raptor().
2445  */
2446 s32 txgbe_prot_autoc_read_raptor(struct txgbe_hw *hw, bool *locked, u64 *value)
2447 {
2448         s32 err;
2449         bool lock_state = false;
2450
2451          /* If LESM is on then we need to hold the SW/FW semaphore. */
2452         if (txgbe_verify_lesm_fw_enabled_raptor(hw)) {
2453                 err = hw->mac.acquire_swfw_sync(hw,
2454                                         TXGBE_MNGSEM_SWPHY);
2455                 if (err != 0)
2456                         return TXGBE_ERR_SWFW_SYNC;
2457
2458                 lock_state = true;
2459         }
2460
2461         if (locked)
2462                 *locked = lock_state;
2463
2464         *value = txgbe_autoc_read(hw);
2465         return 0;
2466 }
2467
2468 /**
2469  * txgbe_prot_autoc_write_raptor - Hides MAC differences needed for AUTOC write
2470  * @hw: pointer to hardware structure
2471  * @autoc: value to write to AUTOC
2472  * @locked: bool to indicate whether the SW/FW lock was already taken by
2473  *           previous prot_autoc_read_raptor.
2474  *
2475  * This part may need to hold the SW/FW lock around all writes to
2476  * AUTOC. Likewise after a write we need to do a pipeline reset.
2477  */
2478 s32 txgbe_prot_autoc_write_raptor(struct txgbe_hw *hw, bool locked, u64 autoc)
2479 {
2480         int err = 0;
2481
2482         /* Blocked by MNG FW so bail */
2483         if (txgbe_check_reset_blocked(hw))
2484                 goto out;
2485
2486         /* We only need to get the lock if:
2487          *  - We didn't do it already (in the read part of a read-modify-write)
2488          *  - LESM is enabled.
2489          */
2490         if (!locked && txgbe_verify_lesm_fw_enabled_raptor(hw)) {
2491                 err = hw->mac.acquire_swfw_sync(hw,
2492                                         TXGBE_MNGSEM_SWPHY);
2493                 if (err != 0)
2494                         return TXGBE_ERR_SWFW_SYNC;
2495
2496                 locked = true;
2497         }
2498
2499         txgbe_autoc_write(hw, autoc);
2500         err = txgbe_reset_pipeline_raptor(hw);
2501
2502 out:
2503         /* Free the SW/FW semaphore as we either grabbed it here or
2504          * already had it when this function was called.
2505          */
2506         if (locked)
2507                 hw->mac.release_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
2508
2509         return err;
2510 }
2511
2512 /**
2513  *  txgbe_init_ops_pf - Inits func ptrs and MAC type
2514  *  @hw: pointer to hardware structure
2515  *
2516  *  Initialize the function pointers and assign the MAC type.
2517  *  Does not touch the hardware.
2518  **/
2519 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
2520 {
2521         struct txgbe_bus_info *bus = &hw->bus;
2522         struct txgbe_mac_info *mac = &hw->mac;
2523         struct txgbe_phy_info *phy = &hw->phy;
2524         struct txgbe_rom_info *rom = &hw->rom;
2525         struct txgbe_mbx_info *mbx = &hw->mbx;
2526
2527         DEBUGFUNC("txgbe_init_ops_pf");
2528
2529         /* BUS */
2530         bus->set_lan_id = txgbe_set_lan_id_multi_port;
2531
2532         /* PHY */
2533         phy->get_media_type = txgbe_get_media_type_raptor;
2534         phy->identify = txgbe_identify_phy;
2535         phy->init = txgbe_init_phy_raptor;
2536         phy->read_reg = txgbe_read_phy_reg;
2537         phy->write_reg = txgbe_write_phy_reg;
2538         phy->read_reg_mdi = txgbe_read_phy_reg_mdi;
2539         phy->write_reg_mdi = txgbe_write_phy_reg_mdi;
2540         phy->setup_link = txgbe_setup_phy_link;
2541         phy->setup_link_speed = txgbe_setup_phy_link_speed;
2542         phy->read_i2c_byte = txgbe_read_i2c_byte;
2543         phy->write_i2c_byte = txgbe_write_i2c_byte;
2544         phy->read_i2c_eeprom = txgbe_read_i2c_eeprom;
2545         phy->write_i2c_eeprom = txgbe_write_i2c_eeprom;
2546         phy->reset = txgbe_reset_phy;
2547
2548         /* MAC */
2549         mac->init_hw = txgbe_init_hw;
2550         mac->start_hw = txgbe_start_hw_raptor;
2551         mac->clear_hw_cntrs = txgbe_clear_hw_cntrs;
2552         mac->enable_rx_dma = txgbe_enable_rx_dma_raptor;
2553         mac->get_mac_addr = txgbe_get_mac_addr;
2554         mac->stop_hw = txgbe_stop_hw;
2555         mac->acquire_swfw_sync = txgbe_acquire_swfw_sync;
2556         mac->release_swfw_sync = txgbe_release_swfw_sync;
2557         mac->reset_hw = txgbe_reset_hw;
2558
2559         mac->disable_sec_rx_path = txgbe_disable_sec_rx_path;
2560         mac->enable_sec_rx_path = txgbe_enable_sec_rx_path;
2561         mac->disable_sec_tx_path = txgbe_disable_sec_tx_path;
2562         mac->enable_sec_tx_path = txgbe_enable_sec_tx_path;
2563         mac->get_san_mac_addr = txgbe_get_san_mac_addr;
2564         mac->set_san_mac_addr = txgbe_set_san_mac_addr;
2565         mac->get_device_caps = txgbe_get_device_caps;
2566         mac->get_wwn_prefix = txgbe_get_wwn_prefix;
2567         mac->autoc_read = txgbe_autoc_read;
2568         mac->autoc_write = txgbe_autoc_write;
2569         mac->prot_autoc_read = txgbe_prot_autoc_read_raptor;
2570         mac->prot_autoc_write = txgbe_prot_autoc_write_raptor;
2571
2572         /* RAR, Multicast, VLAN */
2573         mac->set_rar = txgbe_set_rar;
2574         mac->clear_rar = txgbe_clear_rar;
2575         mac->init_rx_addrs = txgbe_init_rx_addrs;
2576         mac->enable_rx = txgbe_enable_rx;
2577         mac->disable_rx = txgbe_disable_rx;
2578         mac->set_vmdq = txgbe_set_vmdq;
2579         mac->clear_vmdq = txgbe_clear_vmdq;
2580         mac->set_vfta = txgbe_set_vfta;
2581         mac->set_vlvf = txgbe_set_vlvf;
2582         mac->clear_vfta = txgbe_clear_vfta;
2583         mac->init_uta_tables = txgbe_init_uta_tables;
2584         mac->setup_sfp = txgbe_setup_sfp_modules;
2585         mac->set_mac_anti_spoofing = txgbe_set_mac_anti_spoofing;
2586         mac->set_ethertype_anti_spoofing = txgbe_set_ethertype_anti_spoofing;
2587
2588         /* Flow Control */
2589         mac->fc_enable = txgbe_fc_enable;
2590         mac->setup_fc = txgbe_setup_fc;
2591
2592         /* Link */
2593         mac->get_link_capabilities = txgbe_get_link_capabilities_raptor;
2594         mac->check_link = txgbe_check_mac_link;
2595         mac->setup_pba = txgbe_set_pba;
2596
2597         /* Manageability interface */
2598         mac->set_fw_drv_ver = txgbe_hic_set_drv_ver;
2599         mac->get_thermal_sensor_data = txgbe_get_thermal_sensor_data;
2600         mac->init_thermal_sensor_thresh = txgbe_init_thermal_sensor_thresh;
2601
2602         mbx->init_params = txgbe_init_mbx_params_pf;
2603         mbx->read = txgbe_read_mbx_pf;
2604         mbx->write = txgbe_write_mbx_pf;
2605         mbx->check_for_msg = txgbe_check_for_msg_pf;
2606         mbx->check_for_ack = txgbe_check_for_ack_pf;
2607         mbx->check_for_rst = txgbe_check_for_rst_pf;
2608
2609         /* EEPROM */
2610         rom->init_params = txgbe_init_eeprom_params;
2611         rom->read16 = txgbe_ee_read16;
2612         rom->readw_buffer = txgbe_ee_readw_buffer;
2613         rom->readw_sw = txgbe_ee_readw_sw;
2614         rom->read32 = txgbe_ee_read32;
2615         rom->write16 = txgbe_ee_write16;
2616         rom->writew_buffer = txgbe_ee_writew_buffer;
2617         rom->writew_sw = txgbe_ee_writew_sw;
2618         rom->write32 = txgbe_ee_write32;
2619         rom->validate_checksum = txgbe_validate_eeprom_checksum;
2620         rom->update_checksum = txgbe_update_eeprom_checksum;
2621         rom->calc_checksum = txgbe_calc_eeprom_checksum;
2622
2623         mac->mcft_size          = TXGBE_RAPTOR_MC_TBL_SIZE;
2624         mac->vft_size           = TXGBE_RAPTOR_VFT_TBL_SIZE;
2625         mac->num_rar_entries    = TXGBE_RAPTOR_RAR_ENTRIES;
2626         mac->rx_pb_size         = TXGBE_RAPTOR_RX_PB_SIZE;
2627         mac->max_rx_queues      = TXGBE_RAPTOR_MAX_RX_QUEUES;
2628         mac->max_tx_queues      = TXGBE_RAPTOR_MAX_TX_QUEUES;
2629
2630         return 0;
2631 }
2632
2633 /**
2634  *  txgbe_get_link_capabilities_raptor - Determines link capabilities
2635  *  @hw: pointer to hardware structure
2636  *  @speed: pointer to link speed
2637  *  @autoneg: true when autoneg or autotry is enabled
2638  *
2639  *  Determines the link capabilities by reading the AUTOC register.
2640  **/
2641 s32 txgbe_get_link_capabilities_raptor(struct txgbe_hw *hw,
2642                                       u32 *speed,
2643                                       bool *autoneg)
2644 {
2645         s32 status = 0;
2646         u32 autoc = 0;
2647
2648         DEBUGFUNC("txgbe_get_link_capabilities_raptor");
2649
2650         /* Check if 1G SFP module. */
2651         if (hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core0 ||
2652             hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core1 ||
2653             hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core0 ||
2654             hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core1 ||
2655             hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core0 ||
2656             hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core1) {
2657                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
2658                 *autoneg = true;
2659                 return 0;
2660         }
2661
2662         /*
2663          * Determine link capabilities based on the stored value of AUTOC,
2664          * which represents EEPROM defaults.  If AUTOC value has not
2665          * been stored, use the current register values.
2666          */
2667         if (hw->mac.orig_link_settings_stored)
2668                 autoc = hw->mac.orig_autoc;
2669         else
2670                 autoc = hw->mac.autoc_read(hw);
2671
2672         switch (autoc & TXGBE_AUTOC_LMS_MASK) {
2673         case TXGBE_AUTOC_LMS_1G_LINK_NO_AN:
2674                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
2675                 *autoneg = false;
2676                 break;
2677
2678         case TXGBE_AUTOC_LMS_10G_LINK_NO_AN:
2679                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
2680                 *autoneg = false;
2681                 break;
2682
2683         case TXGBE_AUTOC_LMS_1G_AN:
2684                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
2685                 *autoneg = true;
2686                 break;
2687
2688         case TXGBE_AUTOC_LMS_10G:
2689                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
2690                 *autoneg = false;
2691                 break;
2692
2693         case TXGBE_AUTOC_LMS_KX4_KX_KR:
2694         case TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN:
2695                 *speed = TXGBE_LINK_SPEED_UNKNOWN;
2696                 if (autoc & TXGBE_AUTOC_KR_SUPP)
2697                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
2698                 if (autoc & TXGBE_AUTOC_KX4_SUPP)
2699                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
2700                 if (autoc & TXGBE_AUTOC_KX_SUPP)
2701                         *speed |= TXGBE_LINK_SPEED_1GB_FULL;
2702                 *autoneg = true;
2703                 break;
2704
2705         case TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII:
2706                 *speed = TXGBE_LINK_SPEED_100M_FULL;
2707                 if (autoc & TXGBE_AUTOC_KR_SUPP)
2708                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
2709                 if (autoc & TXGBE_AUTOC_KX4_SUPP)
2710                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
2711                 if (autoc & TXGBE_AUTOC_KX_SUPP)
2712                         *speed |= TXGBE_LINK_SPEED_1GB_FULL;
2713                 *autoneg = true;
2714                 break;
2715
2716         case TXGBE_AUTOC_LMS_SGMII_1G_100M:
2717                 *speed = TXGBE_LINK_SPEED_1GB_FULL |
2718                          TXGBE_LINK_SPEED_100M_FULL |
2719                          TXGBE_LINK_SPEED_10M_FULL;
2720                 *autoneg = false;
2721                 break;
2722
2723         default:
2724                 return TXGBE_ERR_LINK_SETUP;
2725         }
2726
2727         if (hw->phy.multispeed_fiber) {
2728                 *speed |= TXGBE_LINK_SPEED_10GB_FULL |
2729                           TXGBE_LINK_SPEED_1GB_FULL;
2730
2731                 /* QSFP must not enable full auto-negotiation
2732                  * Limited autoneg is enabled at 1G
2733                  */
2734                 if (hw->phy.media_type == txgbe_media_type_fiber_qsfp)
2735                         *autoneg = false;
2736                 else
2737                         *autoneg = true;
2738         }
2739
2740         return status;
2741 }
2742
2743 /**
2744  *  txgbe_get_media_type_raptor - Get media type
2745  *  @hw: pointer to hardware structure
2746  *
2747  *  Returns the media type (fiber, copper, backplane)
2748  **/
2749 u32 txgbe_get_media_type_raptor(struct txgbe_hw *hw)
2750 {
2751         u32 media_type;
2752
2753         DEBUGFUNC("txgbe_get_media_type_raptor");
2754
2755         /* Detect if there is a copper PHY attached. */
2756         switch (hw->phy.type) {
2757         case txgbe_phy_cu_unknown:
2758         case txgbe_phy_tn:
2759                 media_type = txgbe_media_type_copper;
2760                 return media_type;
2761         default:
2762                 break;
2763         }
2764
2765         switch (hw->device_id) {
2766         case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
2767                 /* Default device ID is mezzanine card KX/KX4 */
2768                 media_type = txgbe_media_type_backplane;
2769                 break;
2770         case TXGBE_DEV_ID_RAPTOR_SFP:
2771         case TXGBE_DEV_ID_WX1820_SFP:
2772                 media_type = txgbe_media_type_fiber;
2773                 break;
2774         case TXGBE_DEV_ID_RAPTOR_QSFP:
2775                 media_type = txgbe_media_type_fiber_qsfp;
2776                 break;
2777         case TXGBE_DEV_ID_RAPTOR_XAUI:
2778         case TXGBE_DEV_ID_RAPTOR_SGMII:
2779                 media_type = txgbe_media_type_copper;
2780                 break;
2781         default:
2782                 media_type = txgbe_media_type_unknown;
2783                 break;
2784         }
2785
2786         return media_type;
2787 }
2788
2789 /**
2790  *  txgbe_start_mac_link_raptor - Setup MAC link settings
2791  *  @hw: pointer to hardware structure
2792  *  @autoneg_wait_to_complete: true when waiting for completion is needed
2793  *
2794  *  Configures link settings based on values in the txgbe_hw struct.
2795  *  Restarts the link.  Performs autonegotiation if needed.
2796  **/
2797 s32 txgbe_start_mac_link_raptor(struct txgbe_hw *hw,
2798                                bool autoneg_wait_to_complete)
2799 {
2800         s32 status = 0;
2801         bool got_lock = false;
2802
2803         DEBUGFUNC("txgbe_start_mac_link_raptor");
2804
2805         UNREFERENCED_PARAMETER(autoneg_wait_to_complete);
2806
2807         /*  reset_pipeline requires us to hold this lock as it writes to
2808          *  AUTOC.
2809          */
2810         if (txgbe_verify_lesm_fw_enabled_raptor(hw)) {
2811                 status = hw->mac.acquire_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
2812                 if (status != 0)
2813                         goto out;
2814
2815                 got_lock = true;
2816         }
2817
2818         /* Restart link */
2819         txgbe_reset_pipeline_raptor(hw);
2820
2821         if (got_lock)
2822                 hw->mac.release_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
2823
2824         /* Add delay to filter out noises during initial link setup */
2825         msec_delay(50);
2826
2827 out:
2828         return status;
2829 }
2830
2831 /**
2832  *  txgbe_disable_tx_laser_multispeed_fiber - Disable Tx laser
2833  *  @hw: pointer to hardware structure
2834  *
2835  *  The base drivers may require better control over SFP+ module
2836  *  PHY states.  This includes selectively shutting down the Tx
2837  *  laser on the PHY, effectively halting physical link.
2838  **/
2839 void txgbe_disable_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
2840 {
2841         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
2842
2843         /* Blocked by MNG FW so bail */
2844         if (txgbe_check_reset_blocked(hw))
2845                 return;
2846
2847         /* Disable Tx laser; allow 100us to go dark per spec */
2848         esdp_reg |= (TXGBE_GPIOBIT_0 | TXGBE_GPIOBIT_1);
2849         wr32(hw, TXGBE_GPIODATA, esdp_reg);
2850         txgbe_flush(hw);
2851         usec_delay(100);
2852 }
2853
2854 /**
2855  *  txgbe_enable_tx_laser_multispeed_fiber - Enable Tx laser
2856  *  @hw: pointer to hardware structure
2857  *
2858  *  The base drivers may require better control over SFP+ module
2859  *  PHY states.  This includes selectively turning on the Tx
2860  *  laser on the PHY, effectively starting physical link.
2861  **/
2862 void txgbe_enable_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
2863 {
2864         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
2865
2866         /* Enable Tx laser; allow 100ms to light up */
2867         esdp_reg &= ~(TXGBE_GPIOBIT_0 | TXGBE_GPIOBIT_1);
2868         wr32(hw, TXGBE_GPIODATA, esdp_reg);
2869         txgbe_flush(hw);
2870         msec_delay(100);
2871 }
2872
2873 /**
2874  *  txgbe_flap_tx_laser_multispeed_fiber - Flap Tx laser
2875  *  @hw: pointer to hardware structure
2876  *
2877  *  When the driver changes the link speeds that it can support,
2878  *  it sets autotry_restart to true to indicate that we need to
2879  *  initiate a new autotry session with the link partner.  To do
2880  *  so, we set the speed then disable and re-enable the Tx laser, to
2881  *  alert the link partner that it also needs to restart autotry on its
2882  *  end.  This is consistent with true clause 37 autoneg, which also
2883  *  involves a loss of signal.
2884  **/
2885 void txgbe_flap_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
2886 {
2887         DEBUGFUNC("txgbe_flap_tx_laser_multispeed_fiber");
2888
2889         /* Blocked by MNG FW so bail */
2890         if (txgbe_check_reset_blocked(hw))
2891                 return;
2892
2893         if (hw->mac.autotry_restart) {
2894                 txgbe_disable_tx_laser_multispeed_fiber(hw);
2895                 txgbe_enable_tx_laser_multispeed_fiber(hw);
2896                 hw->mac.autotry_restart = false;
2897         }
2898 }
2899
2900 /**
2901  *  txgbe_set_hard_rate_select_speed - Set module link speed
2902  *  @hw: pointer to hardware structure
2903  *  @speed: link speed to set
2904  *
2905  *  Set module link speed via RS0/RS1 rate select pins.
2906  */
2907 void txgbe_set_hard_rate_select_speed(struct txgbe_hw *hw,
2908                                         u32 speed)
2909 {
2910         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
2911
2912         switch (speed) {
2913         case TXGBE_LINK_SPEED_10GB_FULL:
2914                 esdp_reg |= (TXGBE_GPIOBIT_4 | TXGBE_GPIOBIT_5);
2915                 break;
2916         case TXGBE_LINK_SPEED_1GB_FULL:
2917                 esdp_reg &= ~(TXGBE_GPIOBIT_4 | TXGBE_GPIOBIT_5);
2918                 break;
2919         default:
2920                 DEBUGOUT("Invalid fixed module speed\n");
2921                 return;
2922         }
2923
2924         wr32(hw, TXGBE_GPIODATA, esdp_reg);
2925         txgbe_flush(hw);
2926 }
2927
2928 /**
2929  *  txgbe_setup_mac_link_smartspeed - Set MAC link speed using SmartSpeed
2930  *  @hw: pointer to hardware structure
2931  *  @speed: new link speed
2932  *  @autoneg_wait_to_complete: true when waiting for completion is needed
2933  *
2934  *  Implements the Intel SmartSpeed algorithm.
2935  **/
2936 s32 txgbe_setup_mac_link_smartspeed(struct txgbe_hw *hw,
2937                                     u32 speed,
2938                                     bool autoneg_wait_to_complete)
2939 {
2940         s32 status = 0;
2941         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
2942         s32 i, j;
2943         bool link_up = false;
2944         u32 autoc_reg = rd32_epcs(hw, SR_AN_MMD_ADV_REG1);
2945
2946         DEBUGFUNC("txgbe_setup_mac_link_smartspeed");
2947
2948          /* Set autoneg_advertised value based on input link speed */
2949         hw->phy.autoneg_advertised = 0;
2950
2951         if (speed & TXGBE_LINK_SPEED_10GB_FULL)
2952                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_10GB_FULL;
2953
2954         if (speed & TXGBE_LINK_SPEED_1GB_FULL)
2955                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_1GB_FULL;
2956
2957         if (speed & TXGBE_LINK_SPEED_100M_FULL)
2958                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_100M_FULL;
2959
2960         /*
2961          * Implement Intel SmartSpeed algorithm.  SmartSpeed will reduce the
2962          * autoneg advertisement if link is unable to be established at the
2963          * highest negotiated rate.  This can sometimes happen due to integrity
2964          * issues with the physical media connection.
2965          */
2966
2967         /* First, try to get link with full advertisement */
2968         hw->phy.smart_speed_active = false;
2969         for (j = 0; j < TXGBE_SMARTSPEED_MAX_RETRIES; j++) {
2970                 status = txgbe_setup_mac_link(hw, speed,
2971                                                     autoneg_wait_to_complete);
2972                 if (status != 0)
2973                         goto out;
2974
2975                 /*
2976                  * Wait for the controller to acquire link.  Per IEEE 802.3ap,
2977                  * Section 73.10.2, we may have to wait up to 500ms if KR is
2978                  * attempted, or 200ms if KX/KX4/BX/BX4 is attempted, per
2979                  * Table 9 in the AN MAS.
2980                  */
2981                 for (i = 0; i < 5; i++) {
2982                         msec_delay(100);
2983
2984                         /* If we have link, just jump out */
2985                         status = hw->mac.check_link(hw, &link_speed, &link_up,
2986                                                   false);
2987                         if (status != 0)
2988                                 goto out;
2989
2990                         if (link_up)
2991                                 goto out;
2992                 }
2993         }
2994
2995         /*
2996          * We didn't get link.  If we advertised KR plus one of KX4/KX
2997          * (or BX4/BX), then disable KR and try again.
2998          */
2999         if (((autoc_reg & TXGBE_AUTOC_KR_SUPP) == 0) ||
3000             ((autoc_reg & TXGBE_AUTOC_KX_SUPP) == 0 &&
3001              (autoc_reg & TXGBE_AUTOC_KX4_SUPP) == 0))
3002                 goto out;
3003
3004         /* Turn SmartSpeed on to disable KR support */
3005         hw->phy.smart_speed_active = true;
3006         status = txgbe_setup_mac_link(hw, speed,
3007                                             autoneg_wait_to_complete);
3008         if (status != 0)
3009                 goto out;
3010
3011         /*
3012          * Wait for the controller to acquire link.  600ms will allow for
3013          * the AN link_fail_inhibit_timer as well for multiple cycles of
3014          * parallel detect, both 10g and 1g. This allows for the maximum
3015          * connect attempts as defined in the AN MAS table 73-7.
3016          */
3017         for (i = 0; i < 6; i++) {
3018                 msec_delay(100);
3019
3020                 /* If we have link, just jump out */
3021                 status = hw->mac.check_link(hw, &link_speed, &link_up, false);
3022                 if (status != 0)
3023                         goto out;
3024
3025                 if (link_up)
3026                         goto out;
3027         }
3028
3029         /* We didn't get link.  Turn SmartSpeed back off. */
3030         hw->phy.smart_speed_active = false;
3031         status = txgbe_setup_mac_link(hw, speed,
3032                                             autoneg_wait_to_complete);
3033
3034 out:
3035         if (link_up && link_speed == TXGBE_LINK_SPEED_1GB_FULL)
3036                 DEBUGOUT("Smartspeed has downgraded the link speed "
3037                 "from the maximum advertised\n");
3038         return status;
3039 }
3040
3041 /**
3042  *  txgbe_setup_mac_link - Set MAC link speed
3043  *  @hw: pointer to hardware structure
3044  *  @speed: new link speed
3045  *  @autoneg_wait_to_complete: true when waiting for completion is needed
3046  *
3047  *  Set the link speed in the AUTOC register and restarts link.
3048  **/
3049 s32 txgbe_setup_mac_link(struct txgbe_hw *hw,
3050                                u32 speed,
3051                                bool autoneg_wait_to_complete)
3052 {
3053         bool autoneg = false;
3054         s32 status = 0;
3055
3056         u64 autoc = hw->mac.autoc_read(hw);
3057         u64 pma_pmd_10gs = autoc & TXGBE_AUTOC_10GS_PMA_PMD_MASK;
3058         u64 pma_pmd_1g = autoc & TXGBE_AUTOC_1G_PMA_PMD_MASK;
3059         u64 link_mode = autoc & TXGBE_AUTOC_LMS_MASK;
3060         u64 current_autoc = autoc;
3061         u64 orig_autoc = 0;
3062         u32 links_reg;
3063         u32 i;
3064         u32 link_capabilities = TXGBE_LINK_SPEED_UNKNOWN;
3065
3066         DEBUGFUNC("txgbe_setup_mac_link");
3067
3068         /* Check to see if speed passed in is supported. */
3069         status = hw->mac.get_link_capabilities(hw,
3070                         &link_capabilities, &autoneg);
3071         if (status)
3072                 return status;
3073
3074         speed &= link_capabilities;
3075         if (speed == TXGBE_LINK_SPEED_UNKNOWN)
3076                 return TXGBE_ERR_LINK_SETUP;
3077
3078         /* Use stored value (EEPROM defaults) of AUTOC to find KR/KX4 support*/
3079         if (hw->mac.orig_link_settings_stored)
3080                 orig_autoc = hw->mac.orig_autoc;
3081         else
3082                 orig_autoc = autoc;
3083
3084         link_mode = autoc & TXGBE_AUTOC_LMS_MASK;
3085         pma_pmd_1g = autoc & TXGBE_AUTOC_1G_PMA_PMD_MASK;
3086
3087         if (link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR ||
3088             link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
3089             link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
3090                 /* Set KX4/KX/KR support according to speed requested */
3091                 autoc &= ~(TXGBE_AUTOC_KX_SUPP |
3092                            TXGBE_AUTOC_KX4_SUPP |
3093                            TXGBE_AUTOC_KR_SUPP);
3094                 if (speed & TXGBE_LINK_SPEED_10GB_FULL) {
3095                         if (orig_autoc & TXGBE_AUTOC_KX4_SUPP)
3096                                 autoc |= TXGBE_AUTOC_KX4_SUPP;
3097                         if ((orig_autoc & TXGBE_AUTOC_KR_SUPP) &&
3098                             !hw->phy.smart_speed_active)
3099                                 autoc |= TXGBE_AUTOC_KR_SUPP;
3100                 }
3101                 if (speed & TXGBE_LINK_SPEED_1GB_FULL)
3102                         autoc |= TXGBE_AUTOC_KX_SUPP;
3103         } else if ((pma_pmd_1g == TXGBE_AUTOC_1G_SFI) &&
3104                    (link_mode == TXGBE_AUTOC_LMS_1G_LINK_NO_AN ||
3105                     link_mode == TXGBE_AUTOC_LMS_1G_AN)) {
3106                 /* Switch from 1G SFI to 10G SFI if requested */
3107                 if (speed == TXGBE_LINK_SPEED_10GB_FULL &&
3108                     pma_pmd_10gs == TXGBE_AUTOC_10GS_SFI) {
3109                         autoc &= ~TXGBE_AUTOC_LMS_MASK;
3110                         autoc |= TXGBE_AUTOC_LMS_10G;
3111                 }
3112         } else if ((pma_pmd_10gs == TXGBE_AUTOC_10GS_SFI) &&
3113                    (link_mode == TXGBE_AUTOC_LMS_10G)) {
3114                 /* Switch from 10G SFI to 1G SFI if requested */
3115                 if (speed == TXGBE_LINK_SPEED_1GB_FULL &&
3116                     pma_pmd_1g == TXGBE_AUTOC_1G_SFI) {
3117                         autoc &= ~TXGBE_AUTOC_LMS_MASK;
3118                         if (autoneg || hw->phy.type == txgbe_phy_qsfp_intel)
3119                                 autoc |= TXGBE_AUTOC_LMS_1G_AN;
3120                         else
3121                                 autoc |= TXGBE_AUTOC_LMS_1G_LINK_NO_AN;
3122                 }
3123         }
3124
3125         if (autoc == current_autoc)
3126                 return status;
3127
3128         autoc &= ~TXGBE_AUTOC_SPEED_MASK;
3129         autoc |= TXGBE_AUTOC_SPEED(speed);
3130         autoc |= (autoneg ? TXGBE_AUTOC_AUTONEG : 0);
3131
3132         /* Restart link */
3133         hw->mac.autoc_write(hw, autoc);
3134
3135         /* Only poll for autoneg to complete if specified to do so */
3136         if (autoneg_wait_to_complete) {
3137                 if (link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR ||
3138                     link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
3139                     link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
3140                         links_reg = 0; /*Just in case Autoneg time=0*/
3141                         for (i = 0; i < TXGBE_AUTO_NEG_TIME; i++) {
3142                                 links_reg = rd32(hw, TXGBE_PORTSTAT);
3143                                 if (links_reg & TXGBE_PORTSTAT_UP)
3144                                         break;
3145                                 msec_delay(100);
3146                         }
3147                         if (!(links_reg & TXGBE_PORTSTAT_UP)) {
3148                                 status = TXGBE_ERR_AUTONEG_NOT_COMPLETE;
3149                                 DEBUGOUT("Autoneg did not complete.\n");
3150                         }
3151                 }
3152         }
3153
3154         /* Add delay to filter out noises during initial link setup */
3155         msec_delay(50);
3156
3157         return status;
3158 }
3159
3160 /**
3161  *  txgbe_setup_copper_link_raptor - Set the PHY autoneg advertised field
3162  *  @hw: pointer to hardware structure
3163  *  @speed: new link speed
3164  *  @autoneg_wait_to_complete: true if waiting is needed to complete
3165  *
3166  *  Restarts link on PHY and MAC based on settings passed in.
3167  **/
3168 static s32 txgbe_setup_copper_link_raptor(struct txgbe_hw *hw,
3169                                          u32 speed,
3170                                          bool autoneg_wait_to_complete)
3171 {
3172         s32 status;
3173
3174         DEBUGFUNC("txgbe_setup_copper_link_raptor");
3175
3176         /* Setup the PHY according to input speed */
3177         status = hw->phy.setup_link_speed(hw, speed,
3178                                               autoneg_wait_to_complete);
3179         /* Set up MAC */
3180         txgbe_start_mac_link_raptor(hw, autoneg_wait_to_complete);
3181
3182         return status;
3183 }
3184
3185 static int
3186 txgbe_check_flash_load(struct txgbe_hw *hw, u32 check_bit)
3187 {
3188         u32 reg = 0;
3189         u32 i;
3190         int err = 0;
3191         /* if there's flash existing */
3192         if (!(rd32(hw, TXGBE_SPISTAT) & TXGBE_SPISTAT_BPFLASH)) {
3193                 /* wait hw load flash done */
3194                 for (i = 0; i < 10; i++) {
3195                         reg = rd32(hw, TXGBE_ILDRSTAT);
3196                         if (!(reg & check_bit)) {
3197                                 /* done */
3198                                 break;
3199                         }
3200                         msleep(100);
3201                 }
3202                 if (i == 10)
3203                         err = TXGBE_ERR_FLASH_LOADING_FAILED;
3204         }
3205         return err;
3206 }
3207
3208 static void
3209 txgbe_reset_misc(struct txgbe_hw *hw)
3210 {
3211         int i;
3212         u32 value;
3213
3214         wr32(hw, TXGBE_ISBADDRL, hw->isb_dma & 0x00000000FFFFFFFF);
3215         wr32(hw, TXGBE_ISBADDRH, hw->isb_dma >> 32);
3216
3217         value = rd32_epcs(hw, SR_XS_PCS_CTRL2);
3218         if ((value & 0x3) != SR_PCS_CTRL2_TYPE_SEL_X)
3219                 hw->link_status = TXGBE_LINK_STATUS_NONE;
3220
3221         /* receive packets that size > 2048 */
3222         wr32m(hw, TXGBE_MACRXCFG,
3223                 TXGBE_MACRXCFG_JUMBO, TXGBE_MACRXCFG_JUMBO);
3224
3225         wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
3226                 TXGBE_FRMSZ_MAX(TXGBE_FRAME_SIZE_DFT));
3227
3228         /* clear counters on read */
3229         wr32m(hw, TXGBE_MACCNTCTL,
3230                 TXGBE_MACCNTCTL_RC, TXGBE_MACCNTCTL_RC);
3231
3232         wr32m(hw, TXGBE_RXFCCFG,
3233                 TXGBE_RXFCCFG_FC, TXGBE_RXFCCFG_FC);
3234         wr32m(hw, TXGBE_TXFCCFG,
3235                 TXGBE_TXFCCFG_FC, TXGBE_TXFCCFG_FC);
3236
3237         wr32m(hw, TXGBE_MACRXFLT,
3238                 TXGBE_MACRXFLT_PROMISC, TXGBE_MACRXFLT_PROMISC);
3239
3240         wr32m(hw, TXGBE_RSTSTAT,
3241                 TXGBE_RSTSTAT_TMRINIT_MASK, TXGBE_RSTSTAT_TMRINIT(30));
3242
3243         /* errata 4: initialize mng flex tbl and wakeup flex tbl*/
3244         wr32(hw, TXGBE_MNGFLEXSEL, 0);
3245         for (i = 0; i < 16; i++) {
3246                 wr32(hw, TXGBE_MNGFLEXDWL(i), 0);
3247                 wr32(hw, TXGBE_MNGFLEXDWH(i), 0);
3248                 wr32(hw, TXGBE_MNGFLEXMSK(i), 0);
3249         }
3250         wr32(hw, TXGBE_LANFLEXSEL, 0);
3251         for (i = 0; i < 16; i++) {
3252                 wr32(hw, TXGBE_LANFLEXDWL(i), 0);
3253                 wr32(hw, TXGBE_LANFLEXDWH(i), 0);
3254                 wr32(hw, TXGBE_LANFLEXMSK(i), 0);
3255         }
3256
3257         /* set pause frame dst mac addr */
3258         wr32(hw, TXGBE_RXPBPFCDMACL, 0xC2000001);
3259         wr32(hw, TXGBE_RXPBPFCDMACH, 0x0180);
3260
3261         hw->mac.init_thermal_sensor_thresh(hw);
3262
3263         /* enable mac transmitter */
3264         wr32m(hw, TXGBE_MACTXCFG, TXGBE_MACTXCFG_TXE, TXGBE_MACTXCFG_TXE);
3265
3266         for (i = 0; i < 4; i++)
3267                 wr32m(hw, TXGBE_IVAR(i), 0x80808080, 0);
3268 }
3269
3270 /**
3271  *  txgbe_reset_hw - Perform hardware reset
3272  *  @hw: pointer to hardware structure
3273  *
3274  *  Resets the hardware by resetting the transmit and receive units, masks
3275  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
3276  *  reset.
3277  **/
3278 s32 txgbe_reset_hw(struct txgbe_hw *hw)
3279 {
3280         s32 status;
3281         u32 autoc;
3282
3283         DEBUGFUNC("txgbe_reset_hw");
3284
3285         /* Call adapter stop to disable tx/rx and clear interrupts */
3286         status = hw->mac.stop_hw(hw);
3287         if (status != 0)
3288                 return status;
3289
3290         /* flush pending Tx transactions */
3291         txgbe_clear_tx_pending(hw);
3292
3293         /* Identify PHY and related function pointers */
3294         status = hw->phy.init(hw);
3295         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
3296                 return status;
3297
3298         /* Setup SFP module if there is one present. */
3299         if (hw->phy.sfp_setup_needed) {
3300                 status = hw->mac.setup_sfp(hw);
3301                 hw->phy.sfp_setup_needed = false;
3302         }
3303         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
3304                 return status;
3305
3306         /* Reset PHY */
3307         if (!hw->phy.reset_disable)
3308                 hw->phy.reset(hw);
3309
3310         /* remember AUTOC from before we reset */
3311         autoc = hw->mac.autoc_read(hw);
3312
3313 mac_reset_top:
3314         /*
3315          * Issue global reset to the MAC.  Needs to be SW reset if link is up.
3316          * If link reset is used when link is up, it might reset the PHY when
3317          * mng is using it.  If link is down or the flag to force full link
3318          * reset is set, then perform link reset.
3319          */
3320         if (txgbe_mng_present(hw)) {
3321                 txgbe_hic_reset(hw);
3322         } else {
3323                 wr32(hw, TXGBE_RST, TXGBE_RST_LAN(hw->bus.lan_id));
3324                 txgbe_flush(hw);
3325         }
3326         usec_delay(10);
3327
3328         txgbe_reset_misc(hw);
3329
3330         if (hw->bus.lan_id == 0) {
3331                 status = txgbe_check_flash_load(hw,
3332                                 TXGBE_ILDRSTAT_SWRST_LAN0);
3333         } else {
3334                 status = txgbe_check_flash_load(hw,
3335                                 TXGBE_ILDRSTAT_SWRST_LAN1);
3336         }
3337         if (status != 0)
3338                 return status;
3339
3340         msec_delay(50);
3341
3342         /*
3343          * Double resets are required for recovery from certain error
3344          * conditions.  Between resets, it is necessary to stall to
3345          * allow time for any pending HW events to complete.
3346          */
3347         if (hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
3348                 hw->mac.flags &= ~TXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
3349                 goto mac_reset_top;
3350         }
3351
3352         /*
3353          * Store the original AUTOC/AUTOC2 values if they have not been
3354          * stored off yet.  Otherwise restore the stored original
3355          * values since the reset operation sets back to defaults.
3356          */
3357         if (!hw->mac.orig_link_settings_stored) {
3358                 hw->mac.orig_autoc = hw->mac.autoc_read(hw);
3359                 hw->mac.autoc_write(hw, hw->mac.orig_autoc);
3360                 hw->mac.orig_link_settings_stored = true;
3361         } else {
3362                 hw->mac.orig_autoc = autoc;
3363         }
3364
3365         /* Store the permanent mac address */
3366         hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
3367
3368         /*
3369          * Store MAC address from RAR0, clear receive address registers, and
3370          * clear the multicast table.  Also reset num_rar_entries to 128,
3371          * since we modify this value when programming the SAN MAC address.
3372          */
3373         hw->mac.num_rar_entries = 128;
3374         hw->mac.init_rx_addrs(hw);
3375
3376         /* Store the permanent SAN mac address */
3377         hw->mac.get_san_mac_addr(hw, hw->mac.san_addr);
3378
3379         /* Add the SAN MAC address to the RAR only if it's a valid address */
3380         if (txgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
3381                 /* Save the SAN MAC RAR index */
3382                 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
3383
3384                 hw->mac.set_rar(hw, hw->mac.san_mac_rar_index,
3385                                     hw->mac.san_addr, 0, true);
3386
3387                 /* clear VMDq pool/queue selection for this RAR */
3388                 hw->mac.clear_vmdq(hw, hw->mac.san_mac_rar_index,
3389                                        BIT_MASK32);
3390
3391                 /* Reserve the last RAR for the SAN MAC address */
3392                 hw->mac.num_rar_entries--;
3393         }
3394
3395         /* Store the alternative WWNN/WWPN prefix */
3396         hw->mac.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
3397                                    &hw->mac.wwpn_prefix);
3398
3399         return status;
3400 }
3401
3402 /**
3403  *  txgbe_start_hw_raptor - Prepare hardware for Tx/Rx
3404  *  @hw: pointer to hardware structure
3405  *
3406  *  Starts the hardware using the generic start_hw function
3407  *  and the generation start_hw function.
3408  *  Then performs revision-specific operations, if any.
3409  **/
3410 s32 txgbe_start_hw_raptor(struct txgbe_hw *hw)
3411 {
3412         s32 err = 0;
3413
3414         DEBUGFUNC("txgbe_start_hw_raptor");
3415
3416         err = txgbe_start_hw(hw);
3417         if (err != 0)
3418                 goto out;
3419
3420         err = txgbe_start_hw_gen2(hw);
3421         if (err != 0)
3422                 goto out;
3423
3424         /* We need to run link autotry after the driver loads */
3425         hw->mac.autotry_restart = true;
3426
3427 out:
3428         return err;
3429 }
3430
3431 /**
3432  *  txgbe_enable_rx_dma_raptor - Enable the Rx DMA unit
3433  *  @hw: pointer to hardware structure
3434  *  @regval: register value to write to RXCTRL
3435  *
3436  *  Enables the Rx DMA unit
3437  **/
3438 s32 txgbe_enable_rx_dma_raptor(struct txgbe_hw *hw, u32 regval)
3439 {
3440         DEBUGFUNC("txgbe_enable_rx_dma_raptor");
3441
3442         /*
3443          * Workaround silicon errata when enabling the Rx datapath.
3444          * If traffic is incoming before we enable the Rx unit, it could hang
3445          * the Rx DMA unit.  Therefore, make sure the security engine is
3446          * completely disabled prior to enabling the Rx unit.
3447          */
3448
3449         hw->mac.disable_sec_rx_path(hw);
3450
3451         if (regval & TXGBE_PBRXCTL_ENA)
3452                 txgbe_enable_rx(hw);
3453         else
3454                 txgbe_disable_rx(hw);
3455
3456         hw->mac.enable_sec_rx_path(hw);
3457
3458         return 0;
3459 }
3460
3461 /**
3462  *  txgbe_verify_lesm_fw_enabled_raptor - Checks LESM FW module state.
3463  *  @hw: pointer to hardware structure
3464  *
3465  *  Returns true if the LESM FW module is present and enabled. Otherwise
3466  *  returns false. Smart Speed must be disabled if LESM FW module is enabled.
3467  **/
3468 bool txgbe_verify_lesm_fw_enabled_raptor(struct txgbe_hw *hw)
3469 {
3470         bool lesm_enabled = false;
3471         u16 fw_offset, fw_lesm_param_offset, fw_lesm_state;
3472         s32 status;
3473
3474         DEBUGFUNC("txgbe_verify_lesm_fw_enabled_raptor");
3475
3476         /* get the offset to the Firmware Module block */
3477         status = hw->rom.read16(hw, TXGBE_FW_PTR, &fw_offset);
3478
3479         if (status != 0 || fw_offset == 0 || fw_offset == 0xFFFF)
3480                 goto out;
3481
3482         /* get the offset to the LESM Parameters block */
3483         status = hw->rom.read16(hw, (fw_offset +
3484                                      TXGBE_FW_LESM_PARAMETERS_PTR),
3485                                      &fw_lesm_param_offset);
3486
3487         if (status != 0 ||
3488             fw_lesm_param_offset == 0 || fw_lesm_param_offset == 0xFFFF)
3489                 goto out;
3490
3491         /* get the LESM state word */
3492         status = hw->rom.read16(hw, (fw_lesm_param_offset +
3493                                      TXGBE_FW_LESM_STATE_1),
3494                                      &fw_lesm_state);
3495
3496         if (status == 0 && (fw_lesm_state & TXGBE_FW_LESM_STATE_ENABLED))
3497                 lesm_enabled = true;
3498
3499 out:
3500         lesm_enabled = false;
3501         return lesm_enabled;
3502 }
3503
3504 /**
3505  * txgbe_reset_pipeline_raptor - perform pipeline reset
3506  *
3507  *  @hw: pointer to hardware structure
3508  *
3509  * Reset pipeline by asserting Restart_AN together with LMS change to ensure
3510  * full pipeline reset.  This function assumes the SW/FW lock is held.
3511  **/
3512 s32 txgbe_reset_pipeline_raptor(struct txgbe_hw *hw)
3513 {
3514         s32 err = 0;
3515         u64 autoc;
3516
3517         autoc = hw->mac.autoc_read(hw);
3518
3519         /* Enable link if disabled in NVM */
3520         if (autoc & TXGBE_AUTOC_LINK_DIA_MASK)
3521                 autoc &= ~TXGBE_AUTOC_LINK_DIA_MASK;
3522
3523         autoc |= TXGBE_AUTOC_AN_RESTART;
3524         /* Write AUTOC register with toggled LMS[2] bit and Restart_AN */
3525         hw->mac.autoc_write(hw, autoc ^ TXGBE_AUTOC_LMS_AN);
3526
3527         /* Write AUTOC register with original LMS field and Restart_AN */
3528         hw->mac.autoc_write(hw, autoc);
3529         txgbe_flush(hw);
3530
3531         return err;
3532 }
3533