net/txgbe: add queue stats mapping
[dpdk.git] / drivers / net / txgbe / base / txgbe_hw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include "txgbe_type.h"
6 #include "txgbe_phy.h"
7 #include "txgbe_eeprom.h"
8 #include "txgbe_mng.h"
9 #include "txgbe_hw.h"
10
11 #define TXGBE_RAPTOR_MAX_TX_QUEUES 128
12 #define TXGBE_RAPTOR_MAX_RX_QUEUES 128
13 #define TXGBE_RAPTOR_RAR_ENTRIES   128
14 #define TXGBE_RAPTOR_MC_TBL_SIZE   128
15
16 static s32 txgbe_setup_copper_link_raptor(struct txgbe_hw *hw,
17                                          u32 speed,
18                                          bool autoneg_wait_to_complete);
19
20 static s32 txgbe_mta_vector(struct txgbe_hw *hw, u8 *mc_addr);
21 static s32 txgbe_get_san_mac_addr_offset(struct txgbe_hw *hw,
22                                          u16 *san_mac_offset);
23
24 /**
25  *  txgbe_start_hw - Prepare hardware for Tx/Rx
26  *  @hw: pointer to hardware structure
27  *
28  *  Starts the hardware by filling the bus info structure and media type, clears
29  *  all on chip counters, initializes receive address registers, multicast
30  *  table, VLAN filter table, calls routine to set up link and flow control
31  *  settings, and leaves transmit and receive units disabled and uninitialized
32  **/
33 s32 txgbe_start_hw(struct txgbe_hw *hw)
34 {
35         u16 device_caps;
36
37         DEBUGFUNC("txgbe_start_hw");
38
39         /* Set the media type */
40         hw->phy.media_type = hw->phy.get_media_type(hw);
41
42         /* Clear statistics registers */
43         hw->mac.clear_hw_cntrs(hw);
44
45         /* Cache bit indicating need for crosstalk fix */
46         switch (hw->mac.type) {
47         case txgbe_mac_raptor:
48                 hw->mac.get_device_caps(hw, &device_caps);
49                 if (device_caps & TXGBE_DEVICE_CAPS_NO_CROSSTALK_WR)
50                         hw->need_crosstalk_fix = false;
51                 else
52                         hw->need_crosstalk_fix = true;
53                 break;
54         default:
55                 hw->need_crosstalk_fix = false;
56                 break;
57         }
58
59         /* Clear adapter stopped flag */
60         hw->adapter_stopped = false;
61
62         return 0;
63 }
64
65 /**
66  *  txgbe_start_hw_gen2 - Init sequence for common device family
67  *  @hw: pointer to hw structure
68  *
69  * Performs the init sequence common to the second generation
70  * of 10 GbE devices.
71  **/
72 s32 txgbe_start_hw_gen2(struct txgbe_hw *hw)
73 {
74         u32 i;
75
76         /* Clear the rate limiters */
77         for (i = 0; i < hw->mac.max_tx_queues; i++) {
78                 wr32(hw, TXGBE_ARBPOOLIDX, i);
79                 wr32(hw, TXGBE_ARBTXRATE, 0);
80         }
81         txgbe_flush(hw);
82
83         /* We need to run link autotry after the driver loads */
84         hw->mac.autotry_restart = true;
85
86         return 0;
87 }
88
89 /**
90  *  txgbe_init_hw - Generic hardware initialization
91  *  @hw: pointer to hardware structure
92  *
93  *  Initialize the hardware by resetting the hardware, filling the bus info
94  *  structure and media type, clears all on chip counters, initializes receive
95  *  address registers, multicast table, VLAN filter table, calls routine to set
96  *  up link and flow control settings, and leaves transmit and receive units
97  *  disabled and uninitialized
98  **/
99 s32 txgbe_init_hw(struct txgbe_hw *hw)
100 {
101         s32 status;
102
103         DEBUGFUNC("txgbe_init_hw");
104
105         /* Reset the hardware */
106         status = hw->mac.reset_hw(hw);
107         if (status == 0 || status == TXGBE_ERR_SFP_NOT_PRESENT) {
108                 /* Start the HW */
109                 status = hw->mac.start_hw(hw);
110         }
111
112         if (status != 0)
113                 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
114
115         return status;
116 }
117
118 /**
119  *  txgbe_clear_hw_cntrs - Generic clear hardware counters
120  *  @hw: pointer to hardware structure
121  *
122  *  Clears all hardware statistics counters by reading them from the hardware
123  *  Statistics counters are clear on read.
124  **/
125 s32 txgbe_clear_hw_cntrs(struct txgbe_hw *hw)
126 {
127         u16 i = 0;
128
129         DEBUGFUNC("txgbe_clear_hw_cntrs");
130
131         /* QP Stats */
132         /* don't write clear queue stats */
133         for (i = 0; i < TXGBE_MAX_QP; i++) {
134                 hw->qp_last[i].rx_qp_packets = 0;
135                 hw->qp_last[i].tx_qp_packets = 0;
136                 hw->qp_last[i].rx_qp_bytes = 0;
137                 hw->qp_last[i].tx_qp_bytes = 0;
138                 hw->qp_last[i].rx_qp_mc_packets = 0;
139         }
140
141         /* PB Stats */
142         for (i = 0; i < TXGBE_MAX_UP; i++) {
143                 rd32(hw, TXGBE_PBRXUPXON(i));
144                 rd32(hw, TXGBE_PBRXUPXOFF(i));
145                 rd32(hw, TXGBE_PBTXUPXON(i));
146                 rd32(hw, TXGBE_PBTXUPXOFF(i));
147                 rd32(hw, TXGBE_PBTXUPOFF(i));
148
149                 rd32(hw, TXGBE_PBRXMISS(i));
150         }
151         rd32(hw, TXGBE_PBRXLNKXON);
152         rd32(hw, TXGBE_PBRXLNKXOFF);
153         rd32(hw, TXGBE_PBTXLNKXON);
154         rd32(hw, TXGBE_PBTXLNKXOFF);
155
156         /* DMA Stats */
157         rd32(hw, TXGBE_DMARXPKT);
158         rd32(hw, TXGBE_DMATXPKT);
159
160         rd64(hw, TXGBE_DMARXOCTL);
161         rd64(hw, TXGBE_DMATXOCTL);
162
163         /* MAC Stats */
164         rd64(hw, TXGBE_MACRXERRCRCL);
165         rd64(hw, TXGBE_MACRXMPKTL);
166         rd64(hw, TXGBE_MACTXMPKTL);
167
168         rd64(hw, TXGBE_MACRXPKTL);
169         rd64(hw, TXGBE_MACTXPKTL);
170         rd64(hw, TXGBE_MACRXGBOCTL);
171
172         rd64(hw, TXGBE_MACRXOCTL);
173         rd32(hw, TXGBE_MACTXOCTL);
174
175         rd64(hw, TXGBE_MACRX1TO64L);
176         rd64(hw, TXGBE_MACRX65TO127L);
177         rd64(hw, TXGBE_MACRX128TO255L);
178         rd64(hw, TXGBE_MACRX256TO511L);
179         rd64(hw, TXGBE_MACRX512TO1023L);
180         rd64(hw, TXGBE_MACRX1024TOMAXL);
181         rd64(hw, TXGBE_MACTX1TO64L);
182         rd64(hw, TXGBE_MACTX65TO127L);
183         rd64(hw, TXGBE_MACTX128TO255L);
184         rd64(hw, TXGBE_MACTX256TO511L);
185         rd64(hw, TXGBE_MACTX512TO1023L);
186         rd64(hw, TXGBE_MACTX1024TOMAXL);
187
188         rd64(hw, TXGBE_MACRXERRLENL);
189         rd32(hw, TXGBE_MACRXOVERSIZE);
190         rd32(hw, TXGBE_MACRXJABBER);
191
192         /* FCoE Stats */
193         rd32(hw, TXGBE_FCOECRC);
194         rd32(hw, TXGBE_FCOELAST);
195         rd32(hw, TXGBE_FCOERPDC);
196         rd32(hw, TXGBE_FCOEPRC);
197         rd32(hw, TXGBE_FCOEPTC);
198         rd32(hw, TXGBE_FCOEDWRC);
199         rd32(hw, TXGBE_FCOEDWTC);
200
201         /* Flow Director Stats */
202         rd32(hw, TXGBE_FDIRMATCH);
203         rd32(hw, TXGBE_FDIRMISS);
204         rd32(hw, TXGBE_FDIRUSED);
205         rd32(hw, TXGBE_FDIRUSED);
206         rd32(hw, TXGBE_FDIRFAIL);
207         rd32(hw, TXGBE_FDIRFAIL);
208
209         /* MACsec Stats */
210         rd32(hw, TXGBE_LSECTX_UTPKT);
211         rd32(hw, TXGBE_LSECTX_ENCPKT);
212         rd32(hw, TXGBE_LSECTX_PROTPKT);
213         rd32(hw, TXGBE_LSECTX_ENCOCT);
214         rd32(hw, TXGBE_LSECTX_PROTOCT);
215         rd32(hw, TXGBE_LSECRX_UTPKT);
216         rd32(hw, TXGBE_LSECRX_BTPKT);
217         rd32(hw, TXGBE_LSECRX_NOSCIPKT);
218         rd32(hw, TXGBE_LSECRX_UNSCIPKT);
219         rd32(hw, TXGBE_LSECRX_DECOCT);
220         rd32(hw, TXGBE_LSECRX_VLDOCT);
221         rd32(hw, TXGBE_LSECRX_UNCHKPKT);
222         rd32(hw, TXGBE_LSECRX_DLYPKT);
223         rd32(hw, TXGBE_LSECRX_LATEPKT);
224         for (i = 0; i < 2; i++) {
225                 rd32(hw, TXGBE_LSECRX_OKPKT(i));
226                 rd32(hw, TXGBE_LSECRX_INVPKT(i));
227                 rd32(hw, TXGBE_LSECRX_BADPKT(i));
228         }
229         rd32(hw, TXGBE_LSECRX_INVSAPKT);
230         rd32(hw, TXGBE_LSECRX_BADSAPKT);
231
232         return 0;
233 }
234
235 /**
236  *  txgbe_get_mac_addr - Generic get MAC address
237  *  @hw: pointer to hardware structure
238  *  @mac_addr: Adapter MAC address
239  *
240  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
241  *  A reset of the adapter must be performed prior to calling this function
242  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
243  **/
244 s32 txgbe_get_mac_addr(struct txgbe_hw *hw, u8 *mac_addr)
245 {
246         u32 rar_high;
247         u32 rar_low;
248         u16 i;
249
250         DEBUGFUNC("txgbe_get_mac_addr");
251
252         wr32(hw, TXGBE_ETHADDRIDX, 0);
253         rar_high = rd32(hw, TXGBE_ETHADDRH);
254         rar_low = rd32(hw, TXGBE_ETHADDRL);
255
256         for (i = 0; i < 2; i++)
257                 mac_addr[i] = (u8)(rar_high >> (1 - i) * 8);
258
259         for (i = 0; i < 4; i++)
260                 mac_addr[i + 2] = (u8)(rar_low >> (3 - i) * 8);
261
262         return 0;
263 }
264
265 /**
266  *  txgbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
267  *  @hw: pointer to the HW structure
268  *
269  *  Determines the LAN function id by reading memory-mapped registers and swaps
270  *  the port value if requested, and set MAC instance for devices.
271  **/
272 void txgbe_set_lan_id_multi_port(struct txgbe_hw *hw)
273 {
274         struct txgbe_bus_info *bus = &hw->bus;
275         u32 reg;
276
277         DEBUGFUNC("txgbe_set_lan_id_multi_port_pcie");
278
279         reg = rd32(hw, TXGBE_PORTSTAT);
280         bus->lan_id = TXGBE_PORTSTAT_ID(reg);
281
282         /* check for single port */
283         reg = rd32(hw, TXGBE_PWR);
284         if (TXGBE_PWR_LANID(reg) == TXGBE_PWR_LANID_SWAP)
285                 bus->func = 0;
286         else
287                 bus->func = bus->lan_id;
288 }
289
290 /**
291  *  txgbe_stop_hw - Generic stop Tx/Rx units
292  *  @hw: pointer to hardware structure
293  *
294  *  Sets the adapter_stopped flag within txgbe_hw struct. Clears interrupts,
295  *  disables transmit and receive units. The adapter_stopped flag is used by
296  *  the shared code and drivers to determine if the adapter is in a stopped
297  *  state and should not touch the hardware.
298  **/
299 s32 txgbe_stop_hw(struct txgbe_hw *hw)
300 {
301         u32 reg_val;
302         u16 i;
303
304         DEBUGFUNC("txgbe_stop_hw");
305
306         /*
307          * Set the adapter_stopped flag so other driver functions stop touching
308          * the hardware
309          */
310         hw->adapter_stopped = true;
311
312         /* Disable the receive unit */
313         txgbe_disable_rx(hw);
314
315         /* Clear interrupt mask to stop interrupts from being generated */
316         wr32(hw, TXGBE_IENMISC, 0);
317         wr32(hw, TXGBE_IMS(0), TXGBE_IMS_MASK);
318         wr32(hw, TXGBE_IMS(1), TXGBE_IMS_MASK);
319
320         /* Clear any pending interrupts, flush previous writes */
321         wr32(hw, TXGBE_ICRMISC, TXGBE_ICRMISC_MASK);
322         wr32(hw, TXGBE_ICR(0), TXGBE_ICR_MASK);
323         wr32(hw, TXGBE_ICR(1), TXGBE_ICR_MASK);
324
325         /* Disable the transmit unit.  Each queue must be disabled. */
326         for (i = 0; i < hw->mac.max_tx_queues; i++)
327                 wr32(hw, TXGBE_TXCFG(i), TXGBE_TXCFG_FLUSH);
328
329         /* Disable the receive unit by stopping each queue */
330         for (i = 0; i < hw->mac.max_rx_queues; i++) {
331                 reg_val = rd32(hw, TXGBE_RXCFG(i));
332                 reg_val &= ~TXGBE_RXCFG_ENA;
333                 wr32(hw, TXGBE_RXCFG(i), reg_val);
334         }
335
336         /* flush all queues disables */
337         txgbe_flush(hw);
338         msec_delay(2);
339
340         return 0;
341 }
342
343 /**
344  *  txgbe_validate_mac_addr - Validate MAC address
345  *  @mac_addr: pointer to MAC address.
346  *
347  *  Tests a MAC address to ensure it is a valid Individual Address.
348  **/
349 s32 txgbe_validate_mac_addr(u8 *mac_addr)
350 {
351         s32 status = 0;
352
353         DEBUGFUNC("txgbe_validate_mac_addr");
354
355         /* Make sure it is not a multicast address */
356         if (TXGBE_IS_MULTICAST(mac_addr)) {
357                 status = TXGBE_ERR_INVALID_MAC_ADDR;
358         /* Not a broadcast address */
359         } else if (TXGBE_IS_BROADCAST(mac_addr)) {
360                 status = TXGBE_ERR_INVALID_MAC_ADDR;
361         /* Reject the zero address */
362         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
363                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
364                 status = TXGBE_ERR_INVALID_MAC_ADDR;
365         }
366         return status;
367 }
368
369 /**
370  *  txgbe_set_rar - Set Rx address register
371  *  @hw: pointer to hardware structure
372  *  @index: Receive address register to write
373  *  @addr: Address to put into receive address register
374  *  @vmdq: VMDq "set" or "pool" index
375  *  @enable_addr: set flag that address is active
376  *
377  *  Puts an ethernet address into a receive address register.
378  **/
379 s32 txgbe_set_rar(struct txgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
380                           u32 enable_addr)
381 {
382         u32 rar_low, rar_high;
383         u32 rar_entries = hw->mac.num_rar_entries;
384
385         DEBUGFUNC("txgbe_set_rar");
386
387         /* Make sure we are using a valid rar index range */
388         if (index >= rar_entries) {
389                 DEBUGOUT("RAR index %d is out of range.\n", index);
390                 return TXGBE_ERR_INVALID_ARGUMENT;
391         }
392
393         /* setup VMDq pool selection before this RAR gets enabled */
394         hw->mac.set_vmdq(hw, index, vmdq);
395
396         /*
397          * HW expects these in little endian so we reverse the byte
398          * order from network order (big endian) to little endian
399          */
400         rar_low = TXGBE_ETHADDRL_AD0(addr[5]) |
401                   TXGBE_ETHADDRL_AD1(addr[4]) |
402                   TXGBE_ETHADDRL_AD2(addr[3]) |
403                   TXGBE_ETHADDRL_AD3(addr[2]);
404         /*
405          * Some parts put the VMDq setting in the extra RAH bits,
406          * so save everything except the lower 16 bits that hold part
407          * of the address and the address valid bit.
408          */
409         rar_high = rd32(hw, TXGBE_ETHADDRH);
410         rar_high &= ~TXGBE_ETHADDRH_AD_MASK;
411         rar_high |= (TXGBE_ETHADDRH_AD4(addr[1]) |
412                      TXGBE_ETHADDRH_AD5(addr[0]));
413
414         rar_high &= ~TXGBE_ETHADDRH_VLD;
415         if (enable_addr != 0)
416                 rar_high |= TXGBE_ETHADDRH_VLD;
417
418         wr32(hw, TXGBE_ETHADDRIDX, index);
419         wr32(hw, TXGBE_ETHADDRL, rar_low);
420         wr32(hw, TXGBE_ETHADDRH, rar_high);
421
422         return 0;
423 }
424
425 /**
426  *  txgbe_clear_rar - Remove Rx address register
427  *  @hw: pointer to hardware structure
428  *  @index: Receive address register to write
429  *
430  *  Clears an ethernet address from a receive address register.
431  **/
432 s32 txgbe_clear_rar(struct txgbe_hw *hw, u32 index)
433 {
434         u32 rar_high;
435         u32 rar_entries = hw->mac.num_rar_entries;
436
437         DEBUGFUNC("txgbe_clear_rar");
438
439         /* Make sure we are using a valid rar index range */
440         if (index >= rar_entries) {
441                 DEBUGOUT("RAR index %d is out of range.\n", index);
442                 return TXGBE_ERR_INVALID_ARGUMENT;
443         }
444
445         /*
446          * Some parts put the VMDq setting in the extra RAH bits,
447          * so save everything except the lower 16 bits that hold part
448          * of the address and the address valid bit.
449          */
450         wr32(hw, TXGBE_ETHADDRIDX, index);
451         rar_high = rd32(hw, TXGBE_ETHADDRH);
452         rar_high &= ~(TXGBE_ETHADDRH_AD_MASK | TXGBE_ETHADDRH_VLD);
453
454         wr32(hw, TXGBE_ETHADDRL, 0);
455         wr32(hw, TXGBE_ETHADDRH, rar_high);
456
457         /* clear VMDq pool/queue selection for this RAR */
458         hw->mac.clear_vmdq(hw, index, BIT_MASK32);
459
460         return 0;
461 }
462
463 /**
464  *  txgbe_init_rx_addrs - Initializes receive address filters.
465  *  @hw: pointer to hardware structure
466  *
467  *  Places the MAC address in receive address register 0 and clears the rest
468  *  of the receive address registers. Clears the multicast table. Assumes
469  *  the receiver is in reset when the routine is called.
470  **/
471 s32 txgbe_init_rx_addrs(struct txgbe_hw *hw)
472 {
473         u32 i;
474         u32 psrctl;
475         u32 rar_entries = hw->mac.num_rar_entries;
476
477         DEBUGFUNC("txgbe_init_rx_addrs");
478
479         /*
480          * If the current mac address is valid, assume it is a software override
481          * to the permanent address.
482          * Otherwise, use the permanent address from the eeprom.
483          */
484         if (txgbe_validate_mac_addr(hw->mac.addr) ==
485             TXGBE_ERR_INVALID_MAC_ADDR) {
486                 /* Get the MAC address from the RAR0 for later reference */
487                 hw->mac.get_mac_addr(hw, hw->mac.addr);
488
489                 DEBUGOUT(" Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
490                           hw->mac.addr[0], hw->mac.addr[1],
491                           hw->mac.addr[2]);
492                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
493                           hw->mac.addr[4], hw->mac.addr[5]);
494         } else {
495                 /* Setup the receive address. */
496                 DEBUGOUT("Overriding MAC Address in RAR[0]\n");
497                 DEBUGOUT(" New MAC Addr =%.2X %.2X %.2X ",
498                           hw->mac.addr[0], hw->mac.addr[1],
499                           hw->mac.addr[2]);
500                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
501                           hw->mac.addr[4], hw->mac.addr[5]);
502
503                 hw->mac.set_rar(hw, 0, hw->mac.addr, 0, true);
504         }
505
506         /* clear VMDq pool/queue selection for RAR 0 */
507         hw->mac.clear_vmdq(hw, 0, BIT_MASK32);
508
509         hw->addr_ctrl.overflow_promisc = 0;
510
511         hw->addr_ctrl.rar_used_count = 1;
512
513         /* Zero out the other receive addresses. */
514         DEBUGOUT("Clearing RAR[1-%d]\n", rar_entries - 1);
515         for (i = 1; i < rar_entries; i++) {
516                 wr32(hw, TXGBE_ETHADDRIDX, i);
517                 wr32(hw, TXGBE_ETHADDRL, 0);
518                 wr32(hw, TXGBE_ETHADDRH, 0);
519         }
520
521         /* Clear the MTA */
522         hw->addr_ctrl.mta_in_use = 0;
523         psrctl = rd32(hw, TXGBE_PSRCTL);
524         psrctl &= ~(TXGBE_PSRCTL_ADHF12_MASK | TXGBE_PSRCTL_MCHFENA);
525         psrctl |= TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
526         wr32(hw, TXGBE_PSRCTL, psrctl);
527
528         DEBUGOUT(" Clearing MTA\n");
529         for (i = 0; i < hw->mac.mcft_size; i++)
530                 wr32(hw, TXGBE_MCADDRTBL(i), 0);
531
532         txgbe_init_uta_tables(hw);
533
534         return 0;
535 }
536
537 /**
538  *  txgbe_mta_vector - Determines bit-vector in multicast table to set
539  *  @hw: pointer to hardware structure
540  *  @mc_addr: the multicast address
541  *
542  *  Extracts the 12 bits, from a multicast address, to determine which
543  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
544  *  incoming rx multicast addresses, to determine the bit-vector to check in
545  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
546  *  by the MO field of the PSRCTRL. The MO field is set during initialization
547  *  to mc_filter_type.
548  **/
549 static s32 txgbe_mta_vector(struct txgbe_hw *hw, u8 *mc_addr)
550 {
551         u32 vector = 0;
552
553         DEBUGFUNC("txgbe_mta_vector");
554
555         switch (hw->mac.mc_filter_type) {
556         case 0:   /* use bits [47:36] of the address */
557                 vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
558                 break;
559         case 1:   /* use bits [46:35] of the address */
560                 vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
561                 break;
562         case 2:   /* use bits [45:34] of the address */
563                 vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
564                 break;
565         case 3:   /* use bits [43:32] of the address */
566                 vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
567                 break;
568         default:  /* Invalid mc_filter_type */
569                 DEBUGOUT("MC filter type param set incorrectly\n");
570                 ASSERT(0);
571                 break;
572         }
573
574         /* vector can only be 12-bits or boundary will be exceeded */
575         vector &= 0xFFF;
576         return vector;
577 }
578
579 /**
580  *  txgbe_set_mta - Set bit-vector in multicast table
581  *  @hw: pointer to hardware structure
582  *  @mc_addr: Multicast address
583  *
584  *  Sets the bit-vector in the multicast table.
585  **/
586 void txgbe_set_mta(struct txgbe_hw *hw, u8 *mc_addr)
587 {
588         u32 vector;
589         u32 vector_bit;
590         u32 vector_reg;
591
592         DEBUGFUNC("txgbe_set_mta");
593
594         hw->addr_ctrl.mta_in_use++;
595
596         vector = txgbe_mta_vector(hw, mc_addr);
597         DEBUGOUT(" bit-vector = 0x%03X\n", vector);
598
599         /*
600          * The MTA is a register array of 128 32-bit registers. It is treated
601          * like an array of 4096 bits.  We want to set bit
602          * BitArray[vector_value]. So we figure out what register the bit is
603          * in, read it, OR in the new bit, then write back the new value.  The
604          * register is determined by the upper 7 bits of the vector value and
605          * the bit within that register are determined by the lower 5 bits of
606          * the value.
607          */
608         vector_reg = (vector >> 5) & 0x7F;
609         vector_bit = vector & 0x1F;
610         hw->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
611 }
612
613 /**
614  *  txgbe_update_mc_addr_list - Updates MAC list of multicast addresses
615  *  @hw: pointer to hardware structure
616  *  @mc_addr_list: the list of new multicast addresses
617  *  @mc_addr_count: number of addresses
618  *  @next: iterator function to walk the multicast address list
619  *  @clear: flag, when set clears the table beforehand
620  *
621  *  When the clear flag is set, the given list replaces any existing list.
622  *  Hashes the given addresses into the multicast table.
623  **/
624 s32 txgbe_update_mc_addr_list(struct txgbe_hw *hw, u8 *mc_addr_list,
625                                       u32 mc_addr_count, txgbe_mc_addr_itr next,
626                                       bool clear)
627 {
628         u32 i;
629         u32 vmdq;
630
631         DEBUGFUNC("txgbe_update_mc_addr_list");
632
633         /*
634          * Set the new number of MC addresses that we are being requested to
635          * use.
636          */
637         hw->addr_ctrl.num_mc_addrs = mc_addr_count;
638         hw->addr_ctrl.mta_in_use = 0;
639
640         /* Clear mta_shadow */
641         if (clear) {
642                 DEBUGOUT(" Clearing MTA\n");
643                 memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
644         }
645
646         /* Update mta_shadow */
647         for (i = 0; i < mc_addr_count; i++) {
648                 DEBUGOUT(" Adding the multicast addresses:\n");
649                 txgbe_set_mta(hw, next(hw, &mc_addr_list, &vmdq));
650         }
651
652         /* Enable mta */
653         for (i = 0; i < hw->mac.mcft_size; i++)
654                 wr32a(hw, TXGBE_MCADDRTBL(0), i,
655                                       hw->mac.mta_shadow[i]);
656
657         if (hw->addr_ctrl.mta_in_use > 0) {
658                 u32 psrctl = rd32(hw, TXGBE_PSRCTL);
659                 psrctl &= ~(TXGBE_PSRCTL_ADHF12_MASK | TXGBE_PSRCTL_MCHFENA);
660                 psrctl |= TXGBE_PSRCTL_MCHFENA |
661                          TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
662                 wr32(hw, TXGBE_PSRCTL, psrctl);
663         }
664
665         DEBUGOUT("txgbe update mc addr list complete\n");
666         return 0;
667 }
668
669 /**
670  *  txgbe_disable_sec_rx_path - Stops the receive data path
671  *  @hw: pointer to hardware structure
672  *
673  *  Stops the receive data path and waits for the HW to internally empty
674  *  the Rx security block
675  **/
676 s32 txgbe_disable_sec_rx_path(struct txgbe_hw *hw)
677 {
678 #define TXGBE_MAX_SECRX_POLL 4000
679
680         int i;
681         u32 secrxreg;
682
683         DEBUGFUNC("txgbe_disable_sec_rx_path");
684
685         secrxreg = rd32(hw, TXGBE_SECRXCTL);
686         secrxreg |= TXGBE_SECRXCTL_XDSA;
687         wr32(hw, TXGBE_SECRXCTL, secrxreg);
688         for (i = 0; i < TXGBE_MAX_SECRX_POLL; i++) {
689                 secrxreg = rd32(hw, TXGBE_SECRXSTAT);
690                 if (!(secrxreg & TXGBE_SECRXSTAT_RDY))
691                         /* Use interrupt-safe sleep just in case */
692                         usec_delay(10);
693                 else
694                         break;
695         }
696
697         /* For informational purposes only */
698         if (i >= TXGBE_MAX_SECRX_POLL)
699                 DEBUGOUT("Rx unit being enabled before security "
700                          "path fully disabled.  Continuing with init.\n");
701
702         return 0;
703 }
704
705 /**
706  *  txgbe_enable_sec_rx_path - Enables the receive data path
707  *  @hw: pointer to hardware structure
708  *
709  *  Enables the receive data path.
710  **/
711 s32 txgbe_enable_sec_rx_path(struct txgbe_hw *hw)
712 {
713         u32 secrxreg;
714
715         DEBUGFUNC("txgbe_enable_sec_rx_path");
716
717         secrxreg = rd32(hw, TXGBE_SECRXCTL);
718         secrxreg &= ~TXGBE_SECRXCTL_XDSA;
719         wr32(hw, TXGBE_SECRXCTL, secrxreg);
720         txgbe_flush(hw);
721
722         return 0;
723 }
724
725 /**
726  *  txgbe_disable_sec_tx_path - Stops the transmit data path
727  *  @hw: pointer to hardware structure
728  *
729  *  Stops the transmit data path and waits for the HW to internally empty
730  *  the Tx security block
731  **/
732 int txgbe_disable_sec_tx_path(struct txgbe_hw *hw)
733 {
734 #define TXGBE_MAX_SECTX_POLL 40
735
736         int i;
737         u32 sectxreg;
738
739         sectxreg = rd32(hw, TXGBE_SECTXCTL);
740         sectxreg |= TXGBE_SECTXCTL_XDSA;
741         wr32(hw, TXGBE_SECTXCTL, sectxreg);
742         for (i = 0; i < TXGBE_MAX_SECTX_POLL; i++) {
743                 sectxreg = rd32(hw, TXGBE_SECTXSTAT);
744                 if (sectxreg & TXGBE_SECTXSTAT_RDY)
745                         break;
746                 /* Use interrupt-safe sleep just in case */
747                 usec_delay(1000);
748         }
749
750         /* For informational purposes only */
751         if (i >= TXGBE_MAX_SECTX_POLL)
752                 PMD_DRV_LOG(DEBUG, "Tx unit being enabled before security "
753                          "path fully disabled.  Continuing with init.");
754
755         return 0;
756 }
757
758 /**
759  *  txgbe_enable_sec_tx_path - Enables the transmit data path
760  *  @hw: pointer to hardware structure
761  *
762  *  Enables the transmit data path.
763  **/
764 int txgbe_enable_sec_tx_path(struct txgbe_hw *hw)
765 {
766         uint32_t sectxreg;
767
768         sectxreg = rd32(hw, TXGBE_SECTXCTL);
769         sectxreg &= ~TXGBE_SECTXCTL_XDSA;
770         wr32(hw, TXGBE_SECTXCTL, sectxreg);
771         txgbe_flush(hw);
772
773         return 0;
774 }
775
776 /**
777  *  txgbe_get_san_mac_addr_offset - Get SAN MAC address offset from the EEPROM
778  *  @hw: pointer to hardware structure
779  *  @san_mac_offset: SAN MAC address offset
780  *
781  *  This function will read the EEPROM location for the SAN MAC address
782  *  pointer, and returns the value at that location.  This is used in both
783  *  get and set mac_addr routines.
784  **/
785 static s32 txgbe_get_san_mac_addr_offset(struct txgbe_hw *hw,
786                                          u16 *san_mac_offset)
787 {
788         s32 err;
789
790         DEBUGFUNC("txgbe_get_san_mac_addr_offset");
791
792         /*
793          * First read the EEPROM pointer to see if the MAC addresses are
794          * available.
795          */
796         err = hw->rom.readw_sw(hw, TXGBE_SAN_MAC_ADDR_PTR,
797                                       san_mac_offset);
798         if (err) {
799                 DEBUGOUT("eeprom at offset %d failed",
800                          TXGBE_SAN_MAC_ADDR_PTR);
801         }
802
803         return err;
804 }
805
806 /**
807  *  txgbe_get_san_mac_addr - SAN MAC address retrieval from the EEPROM
808  *  @hw: pointer to hardware structure
809  *  @san_mac_addr: SAN MAC address
810  *
811  *  Reads the SAN MAC address from the EEPROM, if it's available.  This is
812  *  per-port, so set_lan_id() must be called before reading the addresses.
813  *  set_lan_id() is called by identify_sfp(), but this cannot be relied
814  *  upon for non-SFP connections, so we must call it here.
815  **/
816 s32 txgbe_get_san_mac_addr(struct txgbe_hw *hw, u8 *san_mac_addr)
817 {
818         u16 san_mac_data, san_mac_offset;
819         u8 i;
820         s32 err;
821
822         DEBUGFUNC("txgbe_get_san_mac_addr");
823
824         /*
825          * First read the EEPROM pointer to see if the MAC addresses are
826          * available. If they're not, no point in calling set_lan_id() here.
827          */
828         err = txgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
829         if (err || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
830                 goto san_mac_addr_out;
831
832         /* apply the port offset to the address offset */
833         (hw->bus.func) ? (san_mac_offset += TXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
834                          (san_mac_offset += TXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
835         for (i = 0; i < 3; i++) {
836                 err = hw->rom.read16(hw, san_mac_offset,
837                                               &san_mac_data);
838                 if (err) {
839                         DEBUGOUT("eeprom read at offset %d failed",
840                                  san_mac_offset);
841                         goto san_mac_addr_out;
842                 }
843                 san_mac_addr[i * 2] = (u8)(san_mac_data);
844                 san_mac_addr[i * 2 + 1] = (u8)(san_mac_data >> 8);
845                 san_mac_offset++;
846         }
847         return 0;
848
849 san_mac_addr_out:
850         /*
851          * No addresses available in this EEPROM.  It's not an
852          * error though, so just wipe the local address and return.
853          */
854         for (i = 0; i < 6; i++)
855                 san_mac_addr[i] = 0xFF;
856         return 0;
857 }
858
859 /**
860  *  txgbe_set_san_mac_addr - Write the SAN MAC address to the EEPROM
861  *  @hw: pointer to hardware structure
862  *  @san_mac_addr: SAN MAC address
863  *
864  *  Write a SAN MAC address to the EEPROM.
865  **/
866 s32 txgbe_set_san_mac_addr(struct txgbe_hw *hw, u8 *san_mac_addr)
867 {
868         s32 err;
869         u16 san_mac_data, san_mac_offset;
870         u8 i;
871
872         DEBUGFUNC("txgbe_set_san_mac_addr");
873
874         /* Look for SAN mac address pointer.  If not defined, return */
875         err = txgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
876         if (err || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
877                 return TXGBE_ERR_NO_SAN_ADDR_PTR;
878
879         /* Apply the port offset to the address offset */
880         (hw->bus.func) ? (san_mac_offset += TXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
881                          (san_mac_offset += TXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
882
883         for (i = 0; i < 3; i++) {
884                 san_mac_data = (u16)((u16)(san_mac_addr[i * 2 + 1]) << 8);
885                 san_mac_data |= (u16)(san_mac_addr[i * 2]);
886                 hw->rom.write16(hw, san_mac_offset, san_mac_data);
887                 san_mac_offset++;
888         }
889
890         return 0;
891 }
892
893 /**
894  *  txgbe_init_uta_tables - Initialize the Unicast Table Array
895  *  @hw: pointer to hardware structure
896  **/
897 s32 txgbe_init_uta_tables(struct txgbe_hw *hw)
898 {
899         int i;
900
901         DEBUGFUNC("txgbe_init_uta_tables");
902         DEBUGOUT(" Clearing UTA\n");
903
904         for (i = 0; i < 128; i++)
905                 wr32(hw, TXGBE_UCADDRTBL(i), 0);
906
907         return 0;
908 }
909
910 /**
911  *  txgbe_need_crosstalk_fix - Determine if we need to do cross talk fix
912  *  @hw: pointer to hardware structure
913  *
914  *  Contains the logic to identify if we need to verify link for the
915  *  crosstalk fix
916  **/
917 static bool txgbe_need_crosstalk_fix(struct txgbe_hw *hw)
918 {
919         /* Does FW say we need the fix */
920         if (!hw->need_crosstalk_fix)
921                 return false;
922
923         /* Only consider SFP+ PHYs i.e. media type fiber */
924         switch (hw->phy.media_type) {
925         case txgbe_media_type_fiber:
926         case txgbe_media_type_fiber_qsfp:
927                 break;
928         default:
929                 return false;
930         }
931
932         return true;
933 }
934
935 /**
936  *  txgbe_check_mac_link - Determine link and speed status
937  *  @hw: pointer to hardware structure
938  *  @speed: pointer to link speed
939  *  @link_up: true when link is up
940  *  @link_up_wait_to_complete: bool used to wait for link up or not
941  *
942  *  Reads the links register to determine if link is up and the current speed
943  **/
944 s32 txgbe_check_mac_link(struct txgbe_hw *hw, u32 *speed,
945                                  bool *link_up, bool link_up_wait_to_complete)
946 {
947         u32 links_reg, links_orig;
948         u32 i;
949
950         DEBUGFUNC("txgbe_check_mac_link");
951
952         /* If Crosstalk fix enabled do the sanity check of making sure
953          * the SFP+ cage is full.
954          */
955         if (txgbe_need_crosstalk_fix(hw)) {
956                 u32 sfp_cage_full;
957
958                 switch (hw->mac.type) {
959                 case txgbe_mac_raptor:
960                         sfp_cage_full = !rd32m(hw, TXGBE_GPIODATA,
961                                         TXGBE_GPIOBIT_2);
962                         break;
963                 default:
964                         /* sanity check - No SFP+ devices here */
965                         sfp_cage_full = false;
966                         break;
967                 }
968
969                 if (!sfp_cage_full) {
970                         *link_up = false;
971                         *speed = TXGBE_LINK_SPEED_UNKNOWN;
972                         return 0;
973                 }
974         }
975
976         /* clear the old state */
977         links_orig = rd32(hw, TXGBE_PORTSTAT);
978
979         links_reg = rd32(hw, TXGBE_PORTSTAT);
980
981         if (links_orig != links_reg) {
982                 DEBUGOUT("LINKS changed from %08X to %08X\n",
983                           links_orig, links_reg);
984         }
985
986         if (link_up_wait_to_complete) {
987                 for (i = 0; i < hw->mac.max_link_up_time; i++) {
988                         if (!(links_reg & TXGBE_PORTSTAT_UP)) {
989                                 *link_up = false;
990                         } else {
991                                 *link_up = true;
992                                 break;
993                         }
994                         msec_delay(100);
995                         links_reg = rd32(hw, TXGBE_PORTSTAT);
996                 }
997         } else {
998                 if (links_reg & TXGBE_PORTSTAT_UP)
999                         *link_up = true;
1000                 else
1001                         *link_up = false;
1002         }
1003
1004         switch (links_reg & TXGBE_PORTSTAT_BW_MASK) {
1005         case TXGBE_PORTSTAT_BW_10G:
1006                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
1007                 break;
1008         case TXGBE_PORTSTAT_BW_1G:
1009                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
1010                 break;
1011         case TXGBE_PORTSTAT_BW_100M:
1012                 *speed = TXGBE_LINK_SPEED_100M_FULL;
1013                 break;
1014         default:
1015                 *speed = TXGBE_LINK_SPEED_UNKNOWN;
1016         }
1017
1018         return 0;
1019 }
1020
1021 /**
1022  *  txgbe_get_device_caps - Get additional device capabilities
1023  *  @hw: pointer to hardware structure
1024  *  @device_caps: the EEPROM word with the extra device capabilities
1025  *
1026  *  This function will read the EEPROM location for the device capabilities,
1027  *  and return the word through device_caps.
1028  **/
1029 s32 txgbe_get_device_caps(struct txgbe_hw *hw, u16 *device_caps)
1030 {
1031         DEBUGFUNC("txgbe_get_device_caps");
1032
1033         hw->rom.readw_sw(hw, TXGBE_DEVICE_CAPS, device_caps);
1034
1035         return 0;
1036 }
1037
1038 /**
1039  * txgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
1040  * @hw: pointer to the hardware structure
1041  *
1042  * The MACs can experience issues if TX work is still pending
1043  * when a reset occurs.  This function prevents this by flushing the PCIe
1044  * buffers on the system.
1045  **/
1046 void txgbe_clear_tx_pending(struct txgbe_hw *hw)
1047 {
1048         u32 hlreg0, i, poll;
1049
1050         /*
1051          * If double reset is not requested then all transactions should
1052          * already be clear and as such there is no work to do
1053          */
1054         if (!(hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
1055                 return;
1056
1057         hlreg0 = rd32(hw, TXGBE_PSRCTL);
1058         wr32(hw, TXGBE_PSRCTL, hlreg0 | TXGBE_PSRCTL_LBENA);
1059
1060         /* Wait for a last completion before clearing buffers */
1061         txgbe_flush(hw);
1062         msec_delay(3);
1063
1064         /*
1065          * Before proceeding, make sure that the PCIe block does not have
1066          * transactions pending.
1067          */
1068         poll = (800 * 11) / 10;
1069         for (i = 0; i < poll; i++)
1070                 usec_delay(100);
1071
1072         /* Flush all writes and allow 20usec for all transactions to clear */
1073         txgbe_flush(hw);
1074         usec_delay(20);
1075
1076         /* restore previous register values */
1077         wr32(hw, TXGBE_PSRCTL, hlreg0);
1078 }
1079
1080 /**
1081  *  txgbe_get_thermal_sensor_data - Gathers thermal sensor data
1082  *  @hw: pointer to hardware structure
1083  *
1084  *  Returns the thermal sensor data structure
1085  **/
1086 s32 txgbe_get_thermal_sensor_data(struct txgbe_hw *hw)
1087 {
1088         struct txgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
1089         s64 tsv;
1090         u32 ts_stat;
1091
1092         DEBUGFUNC("txgbe_get_thermal_sensor_data");
1093
1094         /* Only support thermal sensors attached to physical port 0 */
1095         if (hw->bus.lan_id != 0)
1096                 return TXGBE_NOT_IMPLEMENTED;
1097
1098         ts_stat = rd32(hw, TXGBE_TSSTAT);
1099         tsv = (s64)TXGBE_TSSTAT_DATA(ts_stat);
1100         tsv = tsv > 1200 ? tsv : 1200;
1101         tsv = -(48380 << 8) / 1000
1102                 + tsv * (31020 << 8) / 100000
1103                 - tsv * tsv * (18201 << 8) / 100000000
1104                 + tsv * tsv * tsv * (81542 << 8) / 1000000000000
1105                 - tsv * tsv * tsv * tsv * (16743 << 8) / 1000000000000000;
1106         tsv >>= 8;
1107
1108         data->sensor[0].temp = (s16)tsv;
1109
1110         return 0;
1111 }
1112
1113 /**
1114  *  txgbe_init_thermal_sensor_thresh - Inits thermal sensor thresholds
1115  *  @hw: pointer to hardware structure
1116  *
1117  *  Inits the thermal sensor thresholds according to the NVM map
1118  *  and save off the threshold and location values into mac.thermal_sensor_data
1119  **/
1120 s32 txgbe_init_thermal_sensor_thresh(struct txgbe_hw *hw)
1121 {
1122         struct txgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
1123
1124         DEBUGFUNC("txgbe_init_thermal_sensor_thresh");
1125
1126         memset(data, 0, sizeof(struct txgbe_thermal_sensor_data));
1127
1128         if (hw->bus.lan_id != 0)
1129                 return TXGBE_NOT_IMPLEMENTED;
1130
1131         wr32(hw, TXGBE_TSCTRL, TXGBE_TSCTRL_EVALMD);
1132         wr32(hw, TXGBE_TSINTR,
1133                 TXGBE_TSINTR_AEN | TXGBE_TSINTR_DEN);
1134         wr32(hw, TXGBE_TSEN, TXGBE_TSEN_ENA);
1135
1136
1137         data->sensor[0].alarm_thresh = 100;
1138         wr32(hw, TXGBE_TSATHRE, 677);
1139         data->sensor[0].dalarm_thresh = 90;
1140         wr32(hw, TXGBE_TSDTHRE, 614);
1141
1142         return 0;
1143 }
1144
1145 void txgbe_disable_rx(struct txgbe_hw *hw)
1146 {
1147         u32 pfdtxgswc;
1148
1149         pfdtxgswc = rd32(hw, TXGBE_PSRCTL);
1150         if (pfdtxgswc & TXGBE_PSRCTL_LBENA) {
1151                 pfdtxgswc &= ~TXGBE_PSRCTL_LBENA;
1152                 wr32(hw, TXGBE_PSRCTL, pfdtxgswc);
1153                 hw->mac.set_lben = true;
1154         } else {
1155                 hw->mac.set_lben = false;
1156         }
1157
1158         wr32m(hw, TXGBE_PBRXCTL, TXGBE_PBRXCTL_ENA, 0);
1159         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_ENA, 0);
1160 }
1161
1162 void txgbe_enable_rx(struct txgbe_hw *hw)
1163 {
1164         u32 pfdtxgswc;
1165
1166         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_ENA, TXGBE_MACRXCFG_ENA);
1167         wr32m(hw, TXGBE_PBRXCTL, TXGBE_PBRXCTL_ENA, TXGBE_PBRXCTL_ENA);
1168
1169         if (hw->mac.set_lben) {
1170                 pfdtxgswc = rd32(hw, TXGBE_PSRCTL);
1171                 pfdtxgswc |= TXGBE_PSRCTL_LBENA;
1172                 wr32(hw, TXGBE_PSRCTL, pfdtxgswc);
1173                 hw->mac.set_lben = false;
1174         }
1175 }
1176
1177 /**
1178  *  txgbe_setup_mac_link_multispeed_fiber - Set MAC link speed
1179  *  @hw: pointer to hardware structure
1180  *  @speed: new link speed
1181  *  @autoneg_wait_to_complete: true when waiting for completion is needed
1182  *
1183  *  Set the link speed in the MAC and/or PHY register and restarts link.
1184  **/
1185 s32 txgbe_setup_mac_link_multispeed_fiber(struct txgbe_hw *hw,
1186                                           u32 speed,
1187                                           bool autoneg_wait_to_complete)
1188 {
1189         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
1190         u32 highest_link_speed = TXGBE_LINK_SPEED_UNKNOWN;
1191         s32 status = 0;
1192         u32 speedcnt = 0;
1193         u32 i = 0;
1194         bool autoneg, link_up = false;
1195
1196         DEBUGFUNC("txgbe_setup_mac_link_multispeed_fiber");
1197
1198         /* Mask off requested but non-supported speeds */
1199         status = hw->mac.get_link_capabilities(hw, &link_speed, &autoneg);
1200         if (status != 0)
1201                 return status;
1202
1203         speed &= link_speed;
1204
1205         /* Try each speed one by one, highest priority first.  We do this in
1206          * software because 10Gb fiber doesn't support speed autonegotiation.
1207          */
1208         if (speed & TXGBE_LINK_SPEED_10GB_FULL) {
1209                 speedcnt++;
1210                 highest_link_speed = TXGBE_LINK_SPEED_10GB_FULL;
1211
1212                 /* Set the module link speed */
1213                 switch (hw->phy.media_type) {
1214                 case txgbe_media_type_fiber:
1215                         hw->mac.set_rate_select_speed(hw,
1216                                 TXGBE_LINK_SPEED_10GB_FULL);
1217                         break;
1218                 case txgbe_media_type_fiber_qsfp:
1219                         /* QSFP module automatically detects MAC link speed */
1220                         break;
1221                 default:
1222                         DEBUGOUT("Unexpected media type.\n");
1223                         break;
1224                 }
1225
1226                 /* Allow module to change analog characteristics (1G->10G) */
1227                 msec_delay(40);
1228
1229                 status = hw->mac.setup_mac_link(hw,
1230                                 TXGBE_LINK_SPEED_10GB_FULL,
1231                                 autoneg_wait_to_complete);
1232                 if (status != 0)
1233                         return status;
1234
1235                 /* Flap the Tx laser if it has not already been done */
1236                 hw->mac.flap_tx_laser(hw);
1237
1238                 /* Wait for the controller to acquire link.  Per IEEE 802.3ap,
1239                  * Section 73.10.2, we may have to wait up to 500ms if KR is
1240                  * attempted.  uses the same timing for 10g SFI.
1241                  */
1242                 for (i = 0; i < 5; i++) {
1243                         /* Wait for the link partner to also set speed */
1244                         msec_delay(100);
1245
1246                         /* If we have link, just jump out */
1247                         status = hw->mac.check_link(hw, &link_speed,
1248                                 &link_up, false);
1249                         if (status != 0)
1250                                 return status;
1251
1252                         if (link_up)
1253                                 goto out;
1254                 }
1255         }
1256
1257         if (speed & TXGBE_LINK_SPEED_1GB_FULL) {
1258                 speedcnt++;
1259                 if (highest_link_speed == TXGBE_LINK_SPEED_UNKNOWN)
1260                         highest_link_speed = TXGBE_LINK_SPEED_1GB_FULL;
1261
1262                 /* Set the module link speed */
1263                 switch (hw->phy.media_type) {
1264                 case txgbe_media_type_fiber:
1265                         hw->mac.set_rate_select_speed(hw,
1266                                 TXGBE_LINK_SPEED_1GB_FULL);
1267                         break;
1268                 case txgbe_media_type_fiber_qsfp:
1269                         /* QSFP module automatically detects link speed */
1270                         break;
1271                 default:
1272                         DEBUGOUT("Unexpected media type.\n");
1273                         break;
1274                 }
1275
1276                 /* Allow module to change analog characteristics (10G->1G) */
1277                 msec_delay(40);
1278
1279                 status = hw->mac.setup_mac_link(hw,
1280                                 TXGBE_LINK_SPEED_1GB_FULL,
1281                                 autoneg_wait_to_complete);
1282                 if (status != 0)
1283                         return status;
1284
1285                 /* Flap the Tx laser if it has not already been done */
1286                 hw->mac.flap_tx_laser(hw);
1287
1288                 /* Wait for the link partner to also set speed */
1289                 msec_delay(100);
1290
1291                 /* If we have link, just jump out */
1292                 status = hw->mac.check_link(hw, &link_speed, &link_up, false);
1293                 if (status != 0)
1294                         return status;
1295
1296                 if (link_up)
1297                         goto out;
1298         }
1299
1300         /* We didn't get link.  Configure back to the highest speed we tried,
1301          * (if there was more than one).  We call ourselves back with just the
1302          * single highest speed that the user requested.
1303          */
1304         if (speedcnt > 1)
1305                 status = txgbe_setup_mac_link_multispeed_fiber(hw,
1306                                                       highest_link_speed,
1307                                                       autoneg_wait_to_complete);
1308
1309 out:
1310         /* Set autoneg_advertised value based on input link speed */
1311         hw->phy.autoneg_advertised = 0;
1312
1313         if (speed & TXGBE_LINK_SPEED_10GB_FULL)
1314                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_10GB_FULL;
1315
1316         if (speed & TXGBE_LINK_SPEED_1GB_FULL)
1317                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_1GB_FULL;
1318
1319         return status;
1320 }
1321
1322 /**
1323  *  txgbe_init_shared_code - Initialize the shared code
1324  *  @hw: pointer to hardware structure
1325  *
1326  *  This will assign function pointers and assign the MAC type and PHY code.
1327  *  Does not touch the hardware. This function must be called prior to any
1328  *  other function in the shared code. The txgbe_hw structure should be
1329  *  memset to 0 prior to calling this function.  The following fields in
1330  *  hw structure should be filled in prior to calling this function:
1331  *  hw_addr, back, device_id, vendor_id, subsystem_device_id,
1332  *  subsystem_vendor_id, and revision_id
1333  **/
1334 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
1335 {
1336         s32 status;
1337
1338         DEBUGFUNC("txgbe_init_shared_code");
1339
1340         /*
1341          * Set the mac type
1342          */
1343         txgbe_set_mac_type(hw);
1344
1345         txgbe_init_ops_dummy(hw);
1346         switch (hw->mac.type) {
1347         case txgbe_mac_raptor:
1348                 status = txgbe_init_ops_pf(hw);
1349                 break;
1350         default:
1351                 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
1352                 break;
1353         }
1354         hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
1355
1356         hw->bus.set_lan_id(hw);
1357
1358         return status;
1359 }
1360
1361 /**
1362  *  txgbe_set_mac_type - Sets MAC type
1363  *  @hw: pointer to the HW structure
1364  *
1365  *  This function sets the mac type of the adapter based on the
1366  *  vendor ID and device ID stored in the hw structure.
1367  **/
1368 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
1369 {
1370         s32 err = 0;
1371
1372         DEBUGFUNC("txgbe_set_mac_type");
1373
1374         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
1375                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
1376                 return TXGBE_ERR_DEVICE_NOT_SUPPORTED;
1377         }
1378
1379         switch (hw->device_id) {
1380         case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
1381                 hw->phy.media_type = txgbe_media_type_backplane;
1382                 hw->mac.type = txgbe_mac_raptor;
1383                 break;
1384         case TXGBE_DEV_ID_RAPTOR_XAUI:
1385         case TXGBE_DEV_ID_RAPTOR_SGMII:
1386                 hw->phy.media_type = txgbe_media_type_copper;
1387                 hw->mac.type = txgbe_mac_raptor;
1388                 break;
1389         case TXGBE_DEV_ID_RAPTOR_SFP:
1390         case TXGBE_DEV_ID_WX1820_SFP:
1391                 hw->phy.media_type = txgbe_media_type_fiber;
1392                 hw->mac.type = txgbe_mac_raptor;
1393                 break;
1394         case TXGBE_DEV_ID_RAPTOR_QSFP:
1395                 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
1396                 hw->mac.type = txgbe_mac_raptor;
1397                 break;
1398         case TXGBE_DEV_ID_RAPTOR_VF:
1399         case TXGBE_DEV_ID_RAPTOR_VF_HV:
1400                 hw->phy.media_type = txgbe_media_type_virtual;
1401                 hw->mac.type = txgbe_mac_raptor_vf;
1402                 break;
1403         default:
1404                 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
1405                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
1406                 break;
1407         }
1408
1409         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
1410                   hw->mac.type, hw->phy.media_type, err);
1411         return err;
1412 }
1413
1414 void txgbe_init_mac_link_ops(struct txgbe_hw *hw)
1415 {
1416         struct txgbe_mac_info *mac = &hw->mac;
1417
1418         DEBUGFUNC("txgbe_init_mac_link_ops");
1419
1420         /*
1421          * enable the laser control functions for SFP+ fiber
1422          * and MNG not enabled
1423          */
1424         if (hw->phy.media_type == txgbe_media_type_fiber &&
1425             !txgbe_mng_enabled(hw)) {
1426                 mac->disable_tx_laser =
1427                         txgbe_disable_tx_laser_multispeed_fiber;
1428                 mac->enable_tx_laser =
1429                         txgbe_enable_tx_laser_multispeed_fiber;
1430                 mac->flap_tx_laser =
1431                         txgbe_flap_tx_laser_multispeed_fiber;
1432         }
1433
1434         if ((hw->phy.media_type == txgbe_media_type_fiber ||
1435              hw->phy.media_type == txgbe_media_type_fiber_qsfp) &&
1436             hw->phy.multispeed_fiber) {
1437                 /* Set up dual speed SFP+ support */
1438                 mac->setup_link = txgbe_setup_mac_link_multispeed_fiber;
1439                 mac->setup_mac_link = txgbe_setup_mac_link;
1440                 mac->set_rate_select_speed = txgbe_set_hard_rate_select_speed;
1441         } else if ((hw->phy.media_type == txgbe_media_type_backplane) &&
1442                     (hw->phy.smart_speed == txgbe_smart_speed_auto ||
1443                      hw->phy.smart_speed == txgbe_smart_speed_on) &&
1444                      !txgbe_verify_lesm_fw_enabled_raptor(hw)) {
1445                 mac->setup_link = txgbe_setup_mac_link_smartspeed;
1446         } else {
1447                 mac->setup_link = txgbe_setup_mac_link;
1448         }
1449 }
1450
1451 /**
1452  *  txgbe_init_phy_raptor - PHY/SFP specific init
1453  *  @hw: pointer to hardware structure
1454  *
1455  *  Initialize any function pointers that were not able to be
1456  *  set during init_shared_code because the PHY/SFP type was
1457  *  not known.  Perform the SFP init if necessary.
1458  *
1459  **/
1460 s32 txgbe_init_phy_raptor(struct txgbe_hw *hw)
1461 {
1462         struct txgbe_mac_info *mac = &hw->mac;
1463         struct txgbe_phy_info *phy = &hw->phy;
1464         s32 err = 0;
1465
1466         DEBUGFUNC("txgbe_init_phy_raptor");
1467
1468         if (hw->device_id == TXGBE_DEV_ID_RAPTOR_QSFP) {
1469                 /* Store flag indicating I2C bus access control unit. */
1470                 hw->phy.qsfp_shared_i2c_bus = TRUE;
1471
1472                 /* Initialize access to QSFP+ I2C bus */
1473                 txgbe_flush(hw);
1474         }
1475
1476         /* Identify the PHY or SFP module */
1477         err = phy->identify(hw);
1478         if (err == TXGBE_ERR_SFP_NOT_SUPPORTED)
1479                 goto init_phy_ops_out;
1480
1481         /* Setup function pointers based on detected SFP module and speeds */
1482         txgbe_init_mac_link_ops(hw);
1483
1484         /* If copper media, overwrite with copper function pointers */
1485         if (phy->media_type == txgbe_media_type_copper) {
1486                 mac->setup_link = txgbe_setup_copper_link_raptor;
1487                 mac->get_link_capabilities =
1488                                   txgbe_get_copper_link_capabilities;
1489         }
1490
1491         /* Set necessary function pointers based on PHY type */
1492         switch (hw->phy.type) {
1493         case txgbe_phy_tn:
1494                 phy->setup_link = txgbe_setup_phy_link_tnx;
1495                 phy->check_link = txgbe_check_phy_link_tnx;
1496                 break;
1497         default:
1498                 break;
1499         }
1500
1501 init_phy_ops_out:
1502         return err;
1503 }
1504
1505 s32 txgbe_setup_sfp_modules(struct txgbe_hw *hw)
1506 {
1507         s32 err = 0;
1508
1509         DEBUGFUNC("txgbe_setup_sfp_modules");
1510
1511         if (hw->phy.sfp_type == txgbe_sfp_type_unknown)
1512                 return 0;
1513
1514         txgbe_init_mac_link_ops(hw);
1515
1516         /* PHY config will finish before releasing the semaphore */
1517         err = hw->mac.acquire_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
1518         if (err != 0)
1519                 return TXGBE_ERR_SWFW_SYNC;
1520
1521         /* Release the semaphore */
1522         hw->mac.release_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
1523
1524         /* Delay obtaining semaphore again to allow FW access
1525          * prot_autoc_write uses the semaphore too.
1526          */
1527         msec_delay(hw->rom.semaphore_delay);
1528
1529         if (err) {
1530                 DEBUGOUT("sfp module setup not complete\n");
1531                 return TXGBE_ERR_SFP_SETUP_NOT_COMPLETE;
1532         }
1533
1534         return err;
1535 }
1536
1537 /**
1538  *  txgbe_init_ops_pf - Inits func ptrs and MAC type
1539  *  @hw: pointer to hardware structure
1540  *
1541  *  Initialize the function pointers and assign the MAC type.
1542  *  Does not touch the hardware.
1543  **/
1544 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
1545 {
1546         struct txgbe_bus_info *bus = &hw->bus;
1547         struct txgbe_mac_info *mac = &hw->mac;
1548         struct txgbe_phy_info *phy = &hw->phy;
1549         struct txgbe_rom_info *rom = &hw->rom;
1550
1551         DEBUGFUNC("txgbe_init_ops_pf");
1552
1553         /* BUS */
1554         bus->set_lan_id = txgbe_set_lan_id_multi_port;
1555
1556         /* PHY */
1557         phy->get_media_type = txgbe_get_media_type_raptor;
1558         phy->identify = txgbe_identify_phy;
1559         phy->init = txgbe_init_phy_raptor;
1560         phy->read_reg = txgbe_read_phy_reg;
1561         phy->write_reg = txgbe_write_phy_reg;
1562         phy->read_reg_mdi = txgbe_read_phy_reg_mdi;
1563         phy->write_reg_mdi = txgbe_write_phy_reg_mdi;
1564         phy->setup_link = txgbe_setup_phy_link;
1565         phy->setup_link_speed = txgbe_setup_phy_link_speed;
1566         phy->read_i2c_byte = txgbe_read_i2c_byte;
1567         phy->write_i2c_byte = txgbe_write_i2c_byte;
1568         phy->read_i2c_eeprom = txgbe_read_i2c_eeprom;
1569         phy->write_i2c_eeprom = txgbe_write_i2c_eeprom;
1570         phy->reset = txgbe_reset_phy;
1571
1572         /* MAC */
1573         mac->init_hw = txgbe_init_hw;
1574         mac->start_hw = txgbe_start_hw_raptor;
1575         mac->clear_hw_cntrs = txgbe_clear_hw_cntrs;
1576         mac->enable_rx_dma = txgbe_enable_rx_dma_raptor;
1577         mac->get_mac_addr = txgbe_get_mac_addr;
1578         mac->stop_hw = txgbe_stop_hw;
1579         mac->reset_hw = txgbe_reset_hw;
1580
1581         mac->disable_sec_rx_path = txgbe_disable_sec_rx_path;
1582         mac->enable_sec_rx_path = txgbe_enable_sec_rx_path;
1583         mac->disable_sec_tx_path = txgbe_disable_sec_tx_path;
1584         mac->enable_sec_tx_path = txgbe_enable_sec_tx_path;
1585         mac->get_san_mac_addr = txgbe_get_san_mac_addr;
1586         mac->set_san_mac_addr = txgbe_set_san_mac_addr;
1587         mac->get_device_caps = txgbe_get_device_caps;
1588         mac->autoc_read = txgbe_autoc_read;
1589         mac->autoc_write = txgbe_autoc_write;
1590
1591         mac->set_rar = txgbe_set_rar;
1592         mac->clear_rar = txgbe_clear_rar;
1593         mac->init_rx_addrs = txgbe_init_rx_addrs;
1594         mac->enable_rx = txgbe_enable_rx;
1595         mac->disable_rx = txgbe_disable_rx;
1596         mac->init_uta_tables = txgbe_init_uta_tables;
1597         mac->setup_sfp = txgbe_setup_sfp_modules;
1598         /* Link */
1599         mac->get_link_capabilities = txgbe_get_link_capabilities_raptor;
1600         mac->check_link = txgbe_check_mac_link;
1601
1602         /* Manageability interface */
1603         mac->get_thermal_sensor_data = txgbe_get_thermal_sensor_data;
1604         mac->init_thermal_sensor_thresh = txgbe_init_thermal_sensor_thresh;
1605
1606         /* EEPROM */
1607         rom->init_params = txgbe_init_eeprom_params;
1608         rom->read16 = txgbe_ee_read16;
1609         rom->readw_buffer = txgbe_ee_readw_buffer;
1610         rom->readw_sw = txgbe_ee_readw_sw;
1611         rom->read32 = txgbe_ee_read32;
1612         rom->write16 = txgbe_ee_write16;
1613         rom->writew_buffer = txgbe_ee_writew_buffer;
1614         rom->writew_sw = txgbe_ee_writew_sw;
1615         rom->write32 = txgbe_ee_write32;
1616         rom->validate_checksum = txgbe_validate_eeprom_checksum;
1617         rom->update_checksum = txgbe_update_eeprom_checksum;
1618         rom->calc_checksum = txgbe_calc_eeprom_checksum;
1619
1620         mac->mcft_size          = TXGBE_RAPTOR_MC_TBL_SIZE;
1621         mac->num_rar_entries    = TXGBE_RAPTOR_RAR_ENTRIES;
1622         mac->max_rx_queues      = TXGBE_RAPTOR_MAX_RX_QUEUES;
1623         mac->max_tx_queues      = TXGBE_RAPTOR_MAX_TX_QUEUES;
1624
1625         return 0;
1626 }
1627
1628 /**
1629  *  txgbe_get_link_capabilities_raptor - Determines link capabilities
1630  *  @hw: pointer to hardware structure
1631  *  @speed: pointer to link speed
1632  *  @autoneg: true when autoneg or autotry is enabled
1633  *
1634  *  Determines the link capabilities by reading the AUTOC register.
1635  **/
1636 s32 txgbe_get_link_capabilities_raptor(struct txgbe_hw *hw,
1637                                       u32 *speed,
1638                                       bool *autoneg)
1639 {
1640         s32 status = 0;
1641         u32 autoc = 0;
1642
1643         DEBUGFUNC("txgbe_get_link_capabilities_raptor");
1644
1645         /* Check if 1G SFP module. */
1646         if (hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core0 ||
1647             hw->phy.sfp_type == txgbe_sfp_type_1g_cu_core1 ||
1648             hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core0 ||
1649             hw->phy.sfp_type == txgbe_sfp_type_1g_lx_core1 ||
1650             hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core0 ||
1651             hw->phy.sfp_type == txgbe_sfp_type_1g_sx_core1) {
1652                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
1653                 *autoneg = true;
1654                 return 0;
1655         }
1656
1657         /*
1658          * Determine link capabilities based on the stored value of AUTOC,
1659          * which represents EEPROM defaults.  If AUTOC value has not
1660          * been stored, use the current register values.
1661          */
1662         if (hw->mac.orig_link_settings_stored)
1663                 autoc = hw->mac.orig_autoc;
1664         else
1665                 autoc = hw->mac.autoc_read(hw);
1666
1667         switch (autoc & TXGBE_AUTOC_LMS_MASK) {
1668         case TXGBE_AUTOC_LMS_1G_LINK_NO_AN:
1669                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
1670                 *autoneg = false;
1671                 break;
1672
1673         case TXGBE_AUTOC_LMS_10G_LINK_NO_AN:
1674                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
1675                 *autoneg = false;
1676                 break;
1677
1678         case TXGBE_AUTOC_LMS_1G_AN:
1679                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
1680                 *autoneg = true;
1681                 break;
1682
1683         case TXGBE_AUTOC_LMS_10G:
1684                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
1685                 *autoneg = false;
1686                 break;
1687
1688         case TXGBE_AUTOC_LMS_KX4_KX_KR:
1689         case TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN:
1690                 *speed = TXGBE_LINK_SPEED_UNKNOWN;
1691                 if (autoc & TXGBE_AUTOC_KR_SUPP)
1692                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
1693                 if (autoc & TXGBE_AUTOC_KX4_SUPP)
1694                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
1695                 if (autoc & TXGBE_AUTOC_KX_SUPP)
1696                         *speed |= TXGBE_LINK_SPEED_1GB_FULL;
1697                 *autoneg = true;
1698                 break;
1699
1700         case TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII:
1701                 *speed = TXGBE_LINK_SPEED_100M_FULL;
1702                 if (autoc & TXGBE_AUTOC_KR_SUPP)
1703                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
1704                 if (autoc & TXGBE_AUTOC_KX4_SUPP)
1705                         *speed |= TXGBE_LINK_SPEED_10GB_FULL;
1706                 if (autoc & TXGBE_AUTOC_KX_SUPP)
1707                         *speed |= TXGBE_LINK_SPEED_1GB_FULL;
1708                 *autoneg = true;
1709                 break;
1710
1711         case TXGBE_AUTOC_LMS_SGMII_1G_100M:
1712                 *speed = TXGBE_LINK_SPEED_1GB_FULL |
1713                          TXGBE_LINK_SPEED_100M_FULL |
1714                          TXGBE_LINK_SPEED_10M_FULL;
1715                 *autoneg = false;
1716                 break;
1717
1718         default:
1719                 return TXGBE_ERR_LINK_SETUP;
1720         }
1721
1722         if (hw->phy.multispeed_fiber) {
1723                 *speed |= TXGBE_LINK_SPEED_10GB_FULL |
1724                           TXGBE_LINK_SPEED_1GB_FULL;
1725
1726                 /* QSFP must not enable full auto-negotiation
1727                  * Limited autoneg is enabled at 1G
1728                  */
1729                 if (hw->phy.media_type == txgbe_media_type_fiber_qsfp)
1730                         *autoneg = false;
1731                 else
1732                         *autoneg = true;
1733         }
1734
1735         return status;
1736 }
1737
1738 /**
1739  *  txgbe_get_media_type_raptor - Get media type
1740  *  @hw: pointer to hardware structure
1741  *
1742  *  Returns the media type (fiber, copper, backplane)
1743  **/
1744 u32 txgbe_get_media_type_raptor(struct txgbe_hw *hw)
1745 {
1746         u32 media_type;
1747
1748         DEBUGFUNC("txgbe_get_media_type_raptor");
1749
1750         /* Detect if there is a copper PHY attached. */
1751         switch (hw->phy.type) {
1752         case txgbe_phy_cu_unknown:
1753         case txgbe_phy_tn:
1754                 media_type = txgbe_media_type_copper;
1755                 return media_type;
1756         default:
1757                 break;
1758         }
1759
1760         switch (hw->device_id) {
1761         case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
1762                 /* Default device ID is mezzanine card KX/KX4 */
1763                 media_type = txgbe_media_type_backplane;
1764                 break;
1765         case TXGBE_DEV_ID_RAPTOR_SFP:
1766         case TXGBE_DEV_ID_WX1820_SFP:
1767                 media_type = txgbe_media_type_fiber;
1768                 break;
1769         case TXGBE_DEV_ID_RAPTOR_QSFP:
1770                 media_type = txgbe_media_type_fiber_qsfp;
1771                 break;
1772         case TXGBE_DEV_ID_RAPTOR_XAUI:
1773         case TXGBE_DEV_ID_RAPTOR_SGMII:
1774                 media_type = txgbe_media_type_copper;
1775                 break;
1776         default:
1777                 media_type = txgbe_media_type_unknown;
1778                 break;
1779         }
1780
1781         return media_type;
1782 }
1783
1784 /**
1785  *  txgbe_start_mac_link_raptor - Setup MAC link settings
1786  *  @hw: pointer to hardware structure
1787  *  @autoneg_wait_to_complete: true when waiting for completion is needed
1788  *
1789  *  Configures link settings based on values in the txgbe_hw struct.
1790  *  Restarts the link.  Performs autonegotiation if needed.
1791  **/
1792 s32 txgbe_start_mac_link_raptor(struct txgbe_hw *hw,
1793                                bool autoneg_wait_to_complete)
1794 {
1795         s32 status = 0;
1796         bool got_lock = false;
1797
1798         DEBUGFUNC("txgbe_start_mac_link_raptor");
1799
1800         UNREFERENCED_PARAMETER(autoneg_wait_to_complete);
1801
1802         /*  reset_pipeline requires us to hold this lock as it writes to
1803          *  AUTOC.
1804          */
1805         if (txgbe_verify_lesm_fw_enabled_raptor(hw)) {
1806                 status = hw->mac.acquire_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
1807                 if (status != 0)
1808                         goto out;
1809
1810                 got_lock = true;
1811         }
1812
1813         /* Restart link */
1814         txgbe_reset_pipeline_raptor(hw);
1815
1816         if (got_lock)
1817                 hw->mac.release_swfw_sync(hw, TXGBE_MNGSEM_SWPHY);
1818
1819         /* Add delay to filter out noises during initial link setup */
1820         msec_delay(50);
1821
1822 out:
1823         return status;
1824 }
1825
1826 /**
1827  *  txgbe_disable_tx_laser_multispeed_fiber - Disable Tx laser
1828  *  @hw: pointer to hardware structure
1829  *
1830  *  The base drivers may require better control over SFP+ module
1831  *  PHY states.  This includes selectively shutting down the Tx
1832  *  laser on the PHY, effectively halting physical link.
1833  **/
1834 void txgbe_disable_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
1835 {
1836         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
1837
1838         /* Blocked by MNG FW so bail */
1839         if (txgbe_check_reset_blocked(hw))
1840                 return;
1841
1842         /* Disable Tx laser; allow 100us to go dark per spec */
1843         esdp_reg |= (TXGBE_GPIOBIT_0 | TXGBE_GPIOBIT_1);
1844         wr32(hw, TXGBE_GPIODATA, esdp_reg);
1845         txgbe_flush(hw);
1846         usec_delay(100);
1847 }
1848
1849 /**
1850  *  txgbe_enable_tx_laser_multispeed_fiber - Enable Tx laser
1851  *  @hw: pointer to hardware structure
1852  *
1853  *  The base drivers may require better control over SFP+ module
1854  *  PHY states.  This includes selectively turning on the Tx
1855  *  laser on the PHY, effectively starting physical link.
1856  **/
1857 void txgbe_enable_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
1858 {
1859         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
1860
1861         /* Enable Tx laser; allow 100ms to light up */
1862         esdp_reg &= ~(TXGBE_GPIOBIT_0 | TXGBE_GPIOBIT_1);
1863         wr32(hw, TXGBE_GPIODATA, esdp_reg);
1864         txgbe_flush(hw);
1865         msec_delay(100);
1866 }
1867
1868 /**
1869  *  txgbe_flap_tx_laser_multispeed_fiber - Flap Tx laser
1870  *  @hw: pointer to hardware structure
1871  *
1872  *  When the driver changes the link speeds that it can support,
1873  *  it sets autotry_restart to true to indicate that we need to
1874  *  initiate a new autotry session with the link partner.  To do
1875  *  so, we set the speed then disable and re-enable the Tx laser, to
1876  *  alert the link partner that it also needs to restart autotry on its
1877  *  end.  This is consistent with true clause 37 autoneg, which also
1878  *  involves a loss of signal.
1879  **/
1880 void txgbe_flap_tx_laser_multispeed_fiber(struct txgbe_hw *hw)
1881 {
1882         DEBUGFUNC("txgbe_flap_tx_laser_multispeed_fiber");
1883
1884         /* Blocked by MNG FW so bail */
1885         if (txgbe_check_reset_blocked(hw))
1886                 return;
1887
1888         if (hw->mac.autotry_restart) {
1889                 txgbe_disable_tx_laser_multispeed_fiber(hw);
1890                 txgbe_enable_tx_laser_multispeed_fiber(hw);
1891                 hw->mac.autotry_restart = false;
1892         }
1893 }
1894
1895 /**
1896  *  txgbe_set_hard_rate_select_speed - Set module link speed
1897  *  @hw: pointer to hardware structure
1898  *  @speed: link speed to set
1899  *
1900  *  Set module link speed via RS0/RS1 rate select pins.
1901  */
1902 void txgbe_set_hard_rate_select_speed(struct txgbe_hw *hw,
1903                                         u32 speed)
1904 {
1905         u32 esdp_reg = rd32(hw, TXGBE_GPIODATA);
1906
1907         switch (speed) {
1908         case TXGBE_LINK_SPEED_10GB_FULL:
1909                 esdp_reg |= (TXGBE_GPIOBIT_4 | TXGBE_GPIOBIT_5);
1910                 break;
1911         case TXGBE_LINK_SPEED_1GB_FULL:
1912                 esdp_reg &= ~(TXGBE_GPIOBIT_4 | TXGBE_GPIOBIT_5);
1913                 break;
1914         default:
1915                 DEBUGOUT("Invalid fixed module speed\n");
1916                 return;
1917         }
1918
1919         wr32(hw, TXGBE_GPIODATA, esdp_reg);
1920         txgbe_flush(hw);
1921 }
1922
1923 /**
1924  *  txgbe_setup_mac_link_smartspeed - Set MAC link speed using SmartSpeed
1925  *  @hw: pointer to hardware structure
1926  *  @speed: new link speed
1927  *  @autoneg_wait_to_complete: true when waiting for completion is needed
1928  *
1929  *  Implements the Intel SmartSpeed algorithm.
1930  **/
1931 s32 txgbe_setup_mac_link_smartspeed(struct txgbe_hw *hw,
1932                                     u32 speed,
1933                                     bool autoneg_wait_to_complete)
1934 {
1935         s32 status = 0;
1936         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
1937         s32 i, j;
1938         bool link_up = false;
1939         u32 autoc_reg = rd32_epcs(hw, SR_AN_MMD_ADV_REG1);
1940
1941         DEBUGFUNC("txgbe_setup_mac_link_smartspeed");
1942
1943          /* Set autoneg_advertised value based on input link speed */
1944         hw->phy.autoneg_advertised = 0;
1945
1946         if (speed & TXGBE_LINK_SPEED_10GB_FULL)
1947                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_10GB_FULL;
1948
1949         if (speed & TXGBE_LINK_SPEED_1GB_FULL)
1950                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_1GB_FULL;
1951
1952         if (speed & TXGBE_LINK_SPEED_100M_FULL)
1953                 hw->phy.autoneg_advertised |= TXGBE_LINK_SPEED_100M_FULL;
1954
1955         /*
1956          * Implement Intel SmartSpeed algorithm.  SmartSpeed will reduce the
1957          * autoneg advertisement if link is unable to be established at the
1958          * highest negotiated rate.  This can sometimes happen due to integrity
1959          * issues with the physical media connection.
1960          */
1961
1962         /* First, try to get link with full advertisement */
1963         hw->phy.smart_speed_active = false;
1964         for (j = 0; j < TXGBE_SMARTSPEED_MAX_RETRIES; j++) {
1965                 status = txgbe_setup_mac_link(hw, speed,
1966                                                     autoneg_wait_to_complete);
1967                 if (status != 0)
1968                         goto out;
1969
1970                 /*
1971                  * Wait for the controller to acquire link.  Per IEEE 802.3ap,
1972                  * Section 73.10.2, we may have to wait up to 500ms if KR is
1973                  * attempted, or 200ms if KX/KX4/BX/BX4 is attempted, per
1974                  * Table 9 in the AN MAS.
1975                  */
1976                 for (i = 0; i < 5; i++) {
1977                         msec_delay(100);
1978
1979                         /* If we have link, just jump out */
1980                         status = hw->mac.check_link(hw, &link_speed, &link_up,
1981                                                   false);
1982                         if (status != 0)
1983                                 goto out;
1984
1985                         if (link_up)
1986                                 goto out;
1987                 }
1988         }
1989
1990         /*
1991          * We didn't get link.  If we advertised KR plus one of KX4/KX
1992          * (or BX4/BX), then disable KR and try again.
1993          */
1994         if (((autoc_reg & TXGBE_AUTOC_KR_SUPP) == 0) ||
1995             ((autoc_reg & TXGBE_AUTOC_KX_SUPP) == 0 &&
1996              (autoc_reg & TXGBE_AUTOC_KX4_SUPP) == 0))
1997                 goto out;
1998
1999         /* Turn SmartSpeed on to disable KR support */
2000         hw->phy.smart_speed_active = true;
2001         status = txgbe_setup_mac_link(hw, speed,
2002                                             autoneg_wait_to_complete);
2003         if (status != 0)
2004                 goto out;
2005
2006         /*
2007          * Wait for the controller to acquire link.  600ms will allow for
2008          * the AN link_fail_inhibit_timer as well for multiple cycles of
2009          * parallel detect, both 10g and 1g. This allows for the maximum
2010          * connect attempts as defined in the AN MAS table 73-7.
2011          */
2012         for (i = 0; i < 6; i++) {
2013                 msec_delay(100);
2014
2015                 /* If we have link, just jump out */
2016                 status = hw->mac.check_link(hw, &link_speed, &link_up, false);
2017                 if (status != 0)
2018                         goto out;
2019
2020                 if (link_up)
2021                         goto out;
2022         }
2023
2024         /* We didn't get link.  Turn SmartSpeed back off. */
2025         hw->phy.smart_speed_active = false;
2026         status = txgbe_setup_mac_link(hw, speed,
2027                                             autoneg_wait_to_complete);
2028
2029 out:
2030         if (link_up && link_speed == TXGBE_LINK_SPEED_1GB_FULL)
2031                 DEBUGOUT("Smartspeed has downgraded the link speed "
2032                 "from the maximum advertised\n");
2033         return status;
2034 }
2035
2036 /**
2037  *  txgbe_setup_mac_link - Set MAC link speed
2038  *  @hw: pointer to hardware structure
2039  *  @speed: new link speed
2040  *  @autoneg_wait_to_complete: true when waiting for completion is needed
2041  *
2042  *  Set the link speed in the AUTOC register and restarts link.
2043  **/
2044 s32 txgbe_setup_mac_link(struct txgbe_hw *hw,
2045                                u32 speed,
2046                                bool autoneg_wait_to_complete)
2047 {
2048         bool autoneg = false;
2049         s32 status = 0;
2050
2051         u64 autoc = hw->mac.autoc_read(hw);
2052         u64 pma_pmd_10gs = autoc & TXGBE_AUTOC_10GS_PMA_PMD_MASK;
2053         u64 pma_pmd_1g = autoc & TXGBE_AUTOC_1G_PMA_PMD_MASK;
2054         u64 link_mode = autoc & TXGBE_AUTOC_LMS_MASK;
2055         u64 current_autoc = autoc;
2056         u64 orig_autoc = 0;
2057         u32 links_reg;
2058         u32 i;
2059         u32 link_capabilities = TXGBE_LINK_SPEED_UNKNOWN;
2060
2061         DEBUGFUNC("txgbe_setup_mac_link");
2062
2063         /* Check to see if speed passed in is supported. */
2064         status = hw->mac.get_link_capabilities(hw,
2065                         &link_capabilities, &autoneg);
2066         if (status)
2067                 return status;
2068
2069         speed &= link_capabilities;
2070         if (speed == TXGBE_LINK_SPEED_UNKNOWN)
2071                 return TXGBE_ERR_LINK_SETUP;
2072
2073         /* Use stored value (EEPROM defaults) of AUTOC to find KR/KX4 support*/
2074         if (hw->mac.orig_link_settings_stored)
2075                 orig_autoc = hw->mac.orig_autoc;
2076         else
2077                 orig_autoc = autoc;
2078
2079         link_mode = autoc & TXGBE_AUTOC_LMS_MASK;
2080         pma_pmd_1g = autoc & TXGBE_AUTOC_1G_PMA_PMD_MASK;
2081
2082         if (link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR ||
2083             link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
2084             link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
2085                 /* Set KX4/KX/KR support according to speed requested */
2086                 autoc &= ~(TXGBE_AUTOC_KX_SUPP |
2087                            TXGBE_AUTOC_KX4_SUPP |
2088                            TXGBE_AUTOC_KR_SUPP);
2089                 if (speed & TXGBE_LINK_SPEED_10GB_FULL) {
2090                         if (orig_autoc & TXGBE_AUTOC_KX4_SUPP)
2091                                 autoc |= TXGBE_AUTOC_KX4_SUPP;
2092                         if ((orig_autoc & TXGBE_AUTOC_KR_SUPP) &&
2093                             !hw->phy.smart_speed_active)
2094                                 autoc |= TXGBE_AUTOC_KR_SUPP;
2095                 }
2096                 if (speed & TXGBE_LINK_SPEED_1GB_FULL)
2097                         autoc |= TXGBE_AUTOC_KX_SUPP;
2098         } else if ((pma_pmd_1g == TXGBE_AUTOC_1G_SFI) &&
2099                    (link_mode == TXGBE_AUTOC_LMS_1G_LINK_NO_AN ||
2100                     link_mode == TXGBE_AUTOC_LMS_1G_AN)) {
2101                 /* Switch from 1G SFI to 10G SFI if requested */
2102                 if (speed == TXGBE_LINK_SPEED_10GB_FULL &&
2103                     pma_pmd_10gs == TXGBE_AUTOC_10GS_SFI) {
2104                         autoc &= ~TXGBE_AUTOC_LMS_MASK;
2105                         autoc |= TXGBE_AUTOC_LMS_10G;
2106                 }
2107         } else if ((pma_pmd_10gs == TXGBE_AUTOC_10GS_SFI) &&
2108                    (link_mode == TXGBE_AUTOC_LMS_10G)) {
2109                 /* Switch from 10G SFI to 1G SFI if requested */
2110                 if (speed == TXGBE_LINK_SPEED_1GB_FULL &&
2111                     pma_pmd_1g == TXGBE_AUTOC_1G_SFI) {
2112                         autoc &= ~TXGBE_AUTOC_LMS_MASK;
2113                         if (autoneg || hw->phy.type == txgbe_phy_qsfp_intel)
2114                                 autoc |= TXGBE_AUTOC_LMS_1G_AN;
2115                         else
2116                                 autoc |= TXGBE_AUTOC_LMS_1G_LINK_NO_AN;
2117                 }
2118         }
2119
2120         if (autoc == current_autoc)
2121                 return status;
2122
2123         autoc &= ~TXGBE_AUTOC_SPEED_MASK;
2124         autoc |= TXGBE_AUTOC_SPEED(speed);
2125         autoc |= (autoneg ? TXGBE_AUTOC_AUTONEG : 0);
2126
2127         /* Restart link */
2128         hw->mac.autoc_write(hw, autoc);
2129
2130         /* Only poll for autoneg to complete if specified to do so */
2131         if (autoneg_wait_to_complete) {
2132                 if (link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR ||
2133                     link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_1G_AN ||
2134                     link_mode == TXGBE_AUTOC_LMS_KX4_KX_KR_SGMII) {
2135                         links_reg = 0; /*Just in case Autoneg time=0*/
2136                         for (i = 0; i < TXGBE_AUTO_NEG_TIME; i++) {
2137                                 links_reg = rd32(hw, TXGBE_PORTSTAT);
2138                                 if (links_reg & TXGBE_PORTSTAT_UP)
2139                                         break;
2140                                 msec_delay(100);
2141                         }
2142                         if (!(links_reg & TXGBE_PORTSTAT_UP)) {
2143                                 status = TXGBE_ERR_AUTONEG_NOT_COMPLETE;
2144                                 DEBUGOUT("Autoneg did not complete.\n");
2145                         }
2146                 }
2147         }
2148
2149         /* Add delay to filter out noises during initial link setup */
2150         msec_delay(50);
2151
2152         return status;
2153 }
2154
2155 /**
2156  *  txgbe_setup_copper_link_raptor - Set the PHY autoneg advertised field
2157  *  @hw: pointer to hardware structure
2158  *  @speed: new link speed
2159  *  @autoneg_wait_to_complete: true if waiting is needed to complete
2160  *
2161  *  Restarts link on PHY and MAC based on settings passed in.
2162  **/
2163 static s32 txgbe_setup_copper_link_raptor(struct txgbe_hw *hw,
2164                                          u32 speed,
2165                                          bool autoneg_wait_to_complete)
2166 {
2167         s32 status;
2168
2169         DEBUGFUNC("txgbe_setup_copper_link_raptor");
2170
2171         /* Setup the PHY according to input speed */
2172         status = hw->phy.setup_link_speed(hw, speed,
2173                                               autoneg_wait_to_complete);
2174         /* Set up MAC */
2175         txgbe_start_mac_link_raptor(hw, autoneg_wait_to_complete);
2176
2177         return status;
2178 }
2179
2180 static int
2181 txgbe_check_flash_load(struct txgbe_hw *hw, u32 check_bit)
2182 {
2183         u32 reg = 0;
2184         u32 i;
2185         int err = 0;
2186         /* if there's flash existing */
2187         if (!(rd32(hw, TXGBE_SPISTAT) & TXGBE_SPISTAT_BPFLASH)) {
2188                 /* wait hw load flash done */
2189                 for (i = 0; i < 10; i++) {
2190                         reg = rd32(hw, TXGBE_ILDRSTAT);
2191                         if (!(reg & check_bit)) {
2192                                 /* done */
2193                                 break;
2194                         }
2195                         msleep(100);
2196                 }
2197                 if (i == 10)
2198                         err = TXGBE_ERR_FLASH_LOADING_FAILED;
2199         }
2200         return err;
2201 }
2202
2203 static void
2204 txgbe_reset_misc(struct txgbe_hw *hw)
2205 {
2206         int i;
2207         u32 value;
2208
2209         wr32(hw, TXGBE_ISBADDRL, hw->isb_dma & 0x00000000FFFFFFFF);
2210         wr32(hw, TXGBE_ISBADDRH, hw->isb_dma >> 32);
2211
2212         value = rd32_epcs(hw, SR_XS_PCS_CTRL2);
2213         if ((value & 0x3) != SR_PCS_CTRL2_TYPE_SEL_X)
2214                 hw->link_status = TXGBE_LINK_STATUS_NONE;
2215
2216         /* receive packets that size > 2048 */
2217         wr32m(hw, TXGBE_MACRXCFG,
2218                 TXGBE_MACRXCFG_JUMBO, TXGBE_MACRXCFG_JUMBO);
2219
2220         wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
2221                 TXGBE_FRMSZ_MAX(TXGBE_FRAME_SIZE_DFT));
2222
2223         /* clear counters on read */
2224         wr32m(hw, TXGBE_MACCNTCTL,
2225                 TXGBE_MACCNTCTL_RC, TXGBE_MACCNTCTL_RC);
2226
2227         wr32m(hw, TXGBE_RXFCCFG,
2228                 TXGBE_RXFCCFG_FC, TXGBE_RXFCCFG_FC);
2229         wr32m(hw, TXGBE_TXFCCFG,
2230                 TXGBE_TXFCCFG_FC, TXGBE_TXFCCFG_FC);
2231
2232         wr32m(hw, TXGBE_MACRXFLT,
2233                 TXGBE_MACRXFLT_PROMISC, TXGBE_MACRXFLT_PROMISC);
2234
2235         wr32m(hw, TXGBE_RSTSTAT,
2236                 TXGBE_RSTSTAT_TMRINIT_MASK, TXGBE_RSTSTAT_TMRINIT(30));
2237
2238         /* errata 4: initialize mng flex tbl and wakeup flex tbl*/
2239         wr32(hw, TXGBE_MNGFLEXSEL, 0);
2240         for (i = 0; i < 16; i++) {
2241                 wr32(hw, TXGBE_MNGFLEXDWL(i), 0);
2242                 wr32(hw, TXGBE_MNGFLEXDWH(i), 0);
2243                 wr32(hw, TXGBE_MNGFLEXMSK(i), 0);
2244         }
2245         wr32(hw, TXGBE_LANFLEXSEL, 0);
2246         for (i = 0; i < 16; i++) {
2247                 wr32(hw, TXGBE_LANFLEXDWL(i), 0);
2248                 wr32(hw, TXGBE_LANFLEXDWH(i), 0);
2249                 wr32(hw, TXGBE_LANFLEXMSK(i), 0);
2250         }
2251
2252         /* set pause frame dst mac addr */
2253         wr32(hw, TXGBE_RXPBPFCDMACL, 0xC2000001);
2254         wr32(hw, TXGBE_RXPBPFCDMACH, 0x0180);
2255
2256         hw->mac.init_thermal_sensor_thresh(hw);
2257
2258         /* enable mac transmitter */
2259         wr32m(hw, TXGBE_MACTXCFG, TXGBE_MACTXCFG_TXE, TXGBE_MACTXCFG_TXE);
2260
2261         for (i = 0; i < 4; i++)
2262                 wr32m(hw, TXGBE_IVAR(i), 0x80808080, 0);
2263 }
2264
2265 /**
2266  *  txgbe_reset_hw - Perform hardware reset
2267  *  @hw: pointer to hardware structure
2268  *
2269  *  Resets the hardware by resetting the transmit and receive units, masks
2270  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
2271  *  reset.
2272  **/
2273 s32 txgbe_reset_hw(struct txgbe_hw *hw)
2274 {
2275         s32 status;
2276         u32 autoc;
2277
2278         DEBUGFUNC("txgbe_reset_hw");
2279
2280         /* Call adapter stop to disable tx/rx and clear interrupts */
2281         status = hw->mac.stop_hw(hw);
2282         if (status != 0)
2283                 return status;
2284
2285         /* flush pending Tx transactions */
2286         txgbe_clear_tx_pending(hw);
2287
2288         /* Identify PHY and related function pointers */
2289         status = hw->phy.init(hw);
2290         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
2291                 return status;
2292
2293         /* Setup SFP module if there is one present. */
2294         if (hw->phy.sfp_setup_needed) {
2295                 status = hw->mac.setup_sfp(hw);
2296                 hw->phy.sfp_setup_needed = false;
2297         }
2298         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
2299                 return status;
2300
2301         /* Reset PHY */
2302         if (!hw->phy.reset_disable)
2303                 hw->phy.reset(hw);
2304
2305         /* remember AUTOC from before we reset */
2306         autoc = hw->mac.autoc_read(hw);
2307
2308 mac_reset_top:
2309         /*
2310          * Issue global reset to the MAC.  Needs to be SW reset if link is up.
2311          * If link reset is used when link is up, it might reset the PHY when
2312          * mng is using it.  If link is down or the flag to force full link
2313          * reset is set, then perform link reset.
2314          */
2315         if (txgbe_mng_present(hw)) {
2316                 txgbe_hic_reset(hw);
2317         } else {
2318                 wr32(hw, TXGBE_RST, TXGBE_RST_LAN(hw->bus.lan_id));
2319                 txgbe_flush(hw);
2320         }
2321         usec_delay(10);
2322
2323         txgbe_reset_misc(hw);
2324
2325         if (hw->bus.lan_id == 0) {
2326                 status = txgbe_check_flash_load(hw,
2327                                 TXGBE_ILDRSTAT_SWRST_LAN0);
2328         } else {
2329                 status = txgbe_check_flash_load(hw,
2330                                 TXGBE_ILDRSTAT_SWRST_LAN1);
2331         }
2332         if (status != 0)
2333                 return status;
2334
2335         msec_delay(50);
2336
2337         /*
2338          * Double resets are required for recovery from certain error
2339          * conditions.  Between resets, it is necessary to stall to
2340          * allow time for any pending HW events to complete.
2341          */
2342         if (hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
2343                 hw->mac.flags &= ~TXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
2344                 goto mac_reset_top;
2345         }
2346
2347         /*
2348          * Store the original AUTOC/AUTOC2 values if they have not been
2349          * stored off yet.  Otherwise restore the stored original
2350          * values since the reset operation sets back to defaults.
2351          */
2352         if (!hw->mac.orig_link_settings_stored) {
2353                 hw->mac.orig_autoc = hw->mac.autoc_read(hw);
2354                 hw->mac.autoc_write(hw, hw->mac.orig_autoc);
2355                 hw->mac.orig_link_settings_stored = true;
2356         } else {
2357                 hw->mac.orig_autoc = autoc;
2358         }
2359
2360         /* Store the permanent mac address */
2361         hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
2362
2363         /*
2364          * Store MAC address from RAR0, clear receive address registers, and
2365          * clear the multicast table.  Also reset num_rar_entries to 128,
2366          * since we modify this value when programming the SAN MAC address.
2367          */
2368         hw->mac.num_rar_entries = 128;
2369         hw->mac.init_rx_addrs(hw);
2370
2371         /* Store the permanent SAN mac address */
2372         hw->mac.get_san_mac_addr(hw, hw->mac.san_addr);
2373
2374         /* Add the SAN MAC address to the RAR only if it's a valid address */
2375         if (txgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
2376                 /* Save the SAN MAC RAR index */
2377                 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
2378
2379                 hw->mac.set_rar(hw, hw->mac.san_mac_rar_index,
2380                                     hw->mac.san_addr, 0, true);
2381
2382                 /* clear VMDq pool/queue selection for this RAR */
2383                 hw->mac.clear_vmdq(hw, hw->mac.san_mac_rar_index,
2384                                        BIT_MASK32);
2385
2386                 /* Reserve the last RAR for the SAN MAC address */
2387                 hw->mac.num_rar_entries--;
2388         }
2389
2390         /* Store the alternative WWNN/WWPN prefix */
2391         hw->mac.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
2392                                    &hw->mac.wwpn_prefix);
2393
2394         return status;
2395 }
2396
2397 /**
2398  *  txgbe_start_hw_raptor - Prepare hardware for Tx/Rx
2399  *  @hw: pointer to hardware structure
2400  *
2401  *  Starts the hardware using the generic start_hw function
2402  *  and the generation start_hw function.
2403  *  Then performs revision-specific operations, if any.
2404  **/
2405 s32 txgbe_start_hw_raptor(struct txgbe_hw *hw)
2406 {
2407         s32 err = 0;
2408
2409         DEBUGFUNC("txgbe_start_hw_raptor");
2410
2411         err = txgbe_start_hw(hw);
2412         if (err != 0)
2413                 goto out;
2414
2415         err = txgbe_start_hw_gen2(hw);
2416         if (err != 0)
2417                 goto out;
2418
2419         /* We need to run link autotry after the driver loads */
2420         hw->mac.autotry_restart = true;
2421
2422 out:
2423         return err;
2424 }
2425
2426 /**
2427  *  txgbe_enable_rx_dma_raptor - Enable the Rx DMA unit
2428  *  @hw: pointer to hardware structure
2429  *  @regval: register value to write to RXCTRL
2430  *
2431  *  Enables the Rx DMA unit
2432  **/
2433 s32 txgbe_enable_rx_dma_raptor(struct txgbe_hw *hw, u32 regval)
2434 {
2435         DEBUGFUNC("txgbe_enable_rx_dma_raptor");
2436
2437         /*
2438          * Workaround silicon errata when enabling the Rx datapath.
2439          * If traffic is incoming before we enable the Rx unit, it could hang
2440          * the Rx DMA unit.  Therefore, make sure the security engine is
2441          * completely disabled prior to enabling the Rx unit.
2442          */
2443
2444         hw->mac.disable_sec_rx_path(hw);
2445
2446         if (regval & TXGBE_PBRXCTL_ENA)
2447                 txgbe_enable_rx(hw);
2448         else
2449                 txgbe_disable_rx(hw);
2450
2451         hw->mac.enable_sec_rx_path(hw);
2452
2453         return 0;
2454 }
2455
2456 /**
2457  *  txgbe_verify_lesm_fw_enabled_raptor - Checks LESM FW module state.
2458  *  @hw: pointer to hardware structure
2459  *
2460  *  Returns true if the LESM FW module is present and enabled. Otherwise
2461  *  returns false. Smart Speed must be disabled if LESM FW module is enabled.
2462  **/
2463 bool txgbe_verify_lesm_fw_enabled_raptor(struct txgbe_hw *hw)
2464 {
2465         bool lesm_enabled = false;
2466         u16 fw_offset, fw_lesm_param_offset, fw_lesm_state;
2467         s32 status;
2468
2469         DEBUGFUNC("txgbe_verify_lesm_fw_enabled_raptor");
2470
2471         /* get the offset to the Firmware Module block */
2472         status = hw->rom.read16(hw, TXGBE_FW_PTR, &fw_offset);
2473
2474         if (status != 0 || fw_offset == 0 || fw_offset == 0xFFFF)
2475                 goto out;
2476
2477         /* get the offset to the LESM Parameters block */
2478         status = hw->rom.read16(hw, (fw_offset +
2479                                      TXGBE_FW_LESM_PARAMETERS_PTR),
2480                                      &fw_lesm_param_offset);
2481
2482         if (status != 0 ||
2483             fw_lesm_param_offset == 0 || fw_lesm_param_offset == 0xFFFF)
2484                 goto out;
2485
2486         /* get the LESM state word */
2487         status = hw->rom.read16(hw, (fw_lesm_param_offset +
2488                                      TXGBE_FW_LESM_STATE_1),
2489                                      &fw_lesm_state);
2490
2491         if (status == 0 && (fw_lesm_state & TXGBE_FW_LESM_STATE_ENABLED))
2492                 lesm_enabled = true;
2493
2494 out:
2495         lesm_enabled = false;
2496         return lesm_enabled;
2497 }
2498
2499 /**
2500  * txgbe_reset_pipeline_raptor - perform pipeline reset
2501  *
2502  *  @hw: pointer to hardware structure
2503  *
2504  * Reset pipeline by asserting Restart_AN together with LMS change to ensure
2505  * full pipeline reset.  This function assumes the SW/FW lock is held.
2506  **/
2507 s32 txgbe_reset_pipeline_raptor(struct txgbe_hw *hw)
2508 {
2509         s32 err = 0;
2510         u64 autoc;
2511
2512         autoc = hw->mac.autoc_read(hw);
2513
2514         /* Enable link if disabled in NVM */
2515         if (autoc & TXGBE_AUTOC_LINK_DIA_MASK)
2516                 autoc &= ~TXGBE_AUTOC_LINK_DIA_MASK;
2517
2518         autoc |= TXGBE_AUTOC_AN_RESTART;
2519         /* Write AUTOC register with toggled LMS[2] bit and Restart_AN */
2520         hw->mac.autoc_write(hw, autoc ^ TXGBE_AUTOC_LMS_AN);
2521
2522         /* Write AUTOC register with original LMS field and Restart_AN */
2523         hw->mac.autoc_write(hw, autoc);
2524         txgbe_flush(hw);
2525
2526         return err;
2527 }
2528