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