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