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