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