net/ngbe: optimize PHY initialization process
[dpdk.git] / drivers / net / ngbe / base / ngbe_hw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 Beijing WangXun Technology Co., Ltd.
3  * Copyright(c) 2010-2017 Intel Corporation
4  */
5
6 #include "ngbe_type.h"
7 #include "ngbe_mbx.h"
8 #include "ngbe_phy.h"
9 #include "ngbe_eeprom.h"
10 #include "ngbe_mng.h"
11 #include "ngbe_hw.h"
12
13 /**
14  *  ngbe_start_hw - Prepare hardware for Tx/Rx
15  *  @hw: pointer to hardware structure
16  *
17  *  Starts the hardware.
18  **/
19 s32 ngbe_start_hw(struct ngbe_hw *hw)
20 {
21         s32 err;
22
23         DEBUGFUNC("ngbe_start_hw");
24
25         /* Clear the VLAN filter table */
26         hw->mac.clear_vfta(hw);
27
28         /* Clear statistics registers */
29         hw->mac.clear_hw_cntrs(hw);
30
31         /* Setup flow control */
32         err = hw->mac.setup_fc(hw);
33         if (err != 0 && err != NGBE_NOT_IMPLEMENTED) {
34                 DEBUGOUT("Flow control setup failed, returning %d\n", err);
35                 return err;
36         }
37
38         /* Clear adapter stopped flag */
39         hw->adapter_stopped = false;
40
41         return 0;
42 }
43
44 /**
45  *  ngbe_init_hw - Generic hardware initialization
46  *  @hw: pointer to hardware structure
47  *
48  *  Initialize the hardware by resetting the hardware, filling the bus info
49  *  structure and media type, clears all on chip counters, initializes receive
50  *  address registers, multicast table, VLAN filter table, calls routine to set
51  *  up link and flow control settings, and leaves transmit and receive units
52  *  disabled and uninitialized
53  **/
54 s32 ngbe_init_hw(struct ngbe_hw *hw)
55 {
56         s32 status;
57
58         DEBUGFUNC("ngbe_init_hw");
59
60         ngbe_save_eeprom_version(hw);
61
62         /* Reset the hardware */
63         status = hw->mac.reset_hw(hw);
64         if (status == 0) {
65                 /* Start the HW */
66                 status = hw->mac.start_hw(hw);
67         }
68
69         if (status != 0)
70                 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
71
72         return status;
73 }
74
75 static void
76 ngbe_reset_misc_em(struct ngbe_hw *hw)
77 {
78         int i;
79
80         wr32(hw, NGBE_ISBADDRL, hw->isb_dma & 0xFFFFFFFF);
81         wr32(hw, NGBE_ISBADDRH, hw->isb_dma >> 32);
82
83         /* receive packets that size > 2048 */
84         wr32m(hw, NGBE_MACRXCFG,
85                 NGBE_MACRXCFG_JUMBO, NGBE_MACRXCFG_JUMBO);
86
87         wr32m(hw, NGBE_FRMSZ, NGBE_FRMSZ_MAX_MASK,
88                 NGBE_FRMSZ_MAX(NGBE_FRAME_SIZE_DFT));
89
90         /* clear counters on read */
91         wr32m(hw, NGBE_MACCNTCTL,
92                 NGBE_MACCNTCTL_RC, NGBE_MACCNTCTL_RC);
93
94         wr32m(hw, NGBE_RXFCCFG,
95                 NGBE_RXFCCFG_FC, NGBE_RXFCCFG_FC);
96         wr32m(hw, NGBE_TXFCCFG,
97                 NGBE_TXFCCFG_FC, NGBE_TXFCCFG_FC);
98
99         wr32m(hw, NGBE_MACRXFLT,
100                 NGBE_MACRXFLT_PROMISC, NGBE_MACRXFLT_PROMISC);
101
102         wr32m(hw, NGBE_RSTSTAT,
103                 NGBE_RSTSTAT_TMRINIT_MASK, NGBE_RSTSTAT_TMRINIT(30));
104
105         /* errata 4: initialize mng flex tbl and wakeup flex tbl*/
106         wr32(hw, NGBE_MNGFLEXSEL, 0);
107         for (i = 0; i < 16; i++) {
108                 wr32(hw, NGBE_MNGFLEXDWL(i), 0);
109                 wr32(hw, NGBE_MNGFLEXDWH(i), 0);
110                 wr32(hw, NGBE_MNGFLEXMSK(i), 0);
111         }
112         wr32(hw, NGBE_LANFLEXSEL, 0);
113         for (i = 0; i < 16; i++) {
114                 wr32(hw, NGBE_LANFLEXDWL(i), 0);
115                 wr32(hw, NGBE_LANFLEXDWH(i), 0);
116                 wr32(hw, NGBE_LANFLEXMSK(i), 0);
117         }
118
119         /* set pause frame dst mac addr */
120         wr32(hw, NGBE_RXPBPFCDMACL, 0xC2000001);
121         wr32(hw, NGBE_RXPBPFCDMACH, 0x0180);
122
123         wr32(hw, NGBE_MDIOMODE, 0xF);
124
125         wr32m(hw, NGBE_GPIE, NGBE_GPIE_MSIX, NGBE_GPIE_MSIX);
126
127         if ((hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_M88E1512_SFP ||
128                 (hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_YT8521S_SFP) {
129                 /* gpio0 is used to power on/off control*/
130                 wr32(hw, NGBE_GPIODIR, NGBE_GPIODIR_DDR(1));
131                 wr32(hw, NGBE_GPIODATA, NGBE_GPIOBIT_0);
132         }
133
134         hw->mac.init_thermal_sensor_thresh(hw);
135
136         /* enable mac transmitter */
137         wr32m(hw, NGBE_MACTXCFG, NGBE_MACTXCFG_TE, NGBE_MACTXCFG_TE);
138
139         /* sellect GMII */
140         wr32m(hw, NGBE_MACTXCFG,
141                 NGBE_MACTXCFG_SPEED_MASK, NGBE_MACTXCFG_SPEED_1G);
142
143         for (i = 0; i < 4; i++)
144                 wr32m(hw, NGBE_IVAR(i), 0x80808080, 0);
145 }
146
147 /**
148  *  ngbe_reset_hw_em - Perform hardware reset
149  *  @hw: pointer to hardware structure
150  *
151  *  Resets the hardware by resetting the transmit and receive units, masks
152  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
153  *  reset.
154  **/
155 s32 ngbe_reset_hw_em(struct ngbe_hw *hw)
156 {
157         s32 status;
158
159         DEBUGFUNC("ngbe_reset_hw_em");
160
161         /* Call adapter stop to disable tx/rx and clear interrupts */
162         status = hw->mac.stop_hw(hw);
163         if (status != 0)
164                 return status;
165
166         /* Identify PHY and related function pointers */
167         status = ngbe_init_phy(hw);
168         if (status)
169                 return status;
170
171         /* Reset PHY */
172         if (!hw->phy.reset_disable)
173                 hw->phy.reset_hw(hw);
174
175         wr32(hw, NGBE_RST, NGBE_RST_LAN(hw->bus.lan_id));
176         ngbe_flush(hw);
177         msec_delay(50);
178
179         ngbe_reset_misc_em(hw);
180         hw->mac.clear_hw_cntrs(hw);
181
182         msec_delay(50);
183
184         /* Store the permanent mac address */
185         hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
186
187         /*
188          * Store MAC address from RAR0, clear receive address registers, and
189          * clear the multicast table.
190          */
191         hw->mac.num_rar_entries = NGBE_EM_RAR_ENTRIES;
192         hw->mac.init_rx_addrs(hw);
193
194         return status;
195 }
196
197 /**
198  *  ngbe_clear_hw_cntrs - Generic clear hardware counters
199  *  @hw: pointer to hardware structure
200  *
201  *  Clears all hardware statistics counters by reading them from the hardware
202  *  Statistics counters are clear on read.
203  **/
204 s32 ngbe_clear_hw_cntrs(struct ngbe_hw *hw)
205 {
206         u16 i = 0;
207
208         DEBUGFUNC("ngbe_clear_hw_cntrs");
209
210         /* QP Stats */
211         /* don't write clear queue stats */
212         for (i = 0; i < NGBE_MAX_QP; i++) {
213                 hw->qp_last[i].rx_qp_packets = 0;
214                 hw->qp_last[i].tx_qp_packets = 0;
215                 hw->qp_last[i].rx_qp_bytes = 0;
216                 hw->qp_last[i].tx_qp_bytes = 0;
217                 hw->qp_last[i].rx_qp_mc_packets = 0;
218                 hw->qp_last[i].tx_qp_mc_packets = 0;
219                 hw->qp_last[i].rx_qp_bc_packets = 0;
220                 hw->qp_last[i].tx_qp_bc_packets = 0;
221         }
222
223         /* PB Stats */
224         rd32(hw, NGBE_PBRXLNKXON);
225         rd32(hw, NGBE_PBRXLNKXOFF);
226         rd32(hw, NGBE_PBTXLNKXON);
227         rd32(hw, NGBE_PBTXLNKXOFF);
228
229         /* DMA Stats */
230         rd32(hw, NGBE_DMARXPKT);
231         rd32(hw, NGBE_DMATXPKT);
232
233         rd64(hw, NGBE_DMARXOCTL);
234         rd64(hw, NGBE_DMATXOCTL);
235
236         /* MAC Stats */
237         rd64(hw, NGBE_MACRXERRCRCL);
238         rd64(hw, NGBE_MACRXMPKTL);
239         rd64(hw, NGBE_MACTXMPKTL);
240
241         rd64(hw, NGBE_MACRXPKTL);
242         rd64(hw, NGBE_MACTXPKTL);
243         rd64(hw, NGBE_MACRXGBOCTL);
244
245         rd64(hw, NGBE_MACRXOCTL);
246         rd32(hw, NGBE_MACTXOCTL);
247
248         rd64(hw, NGBE_MACRX1TO64L);
249         rd64(hw, NGBE_MACRX65TO127L);
250         rd64(hw, NGBE_MACRX128TO255L);
251         rd64(hw, NGBE_MACRX256TO511L);
252         rd64(hw, NGBE_MACRX512TO1023L);
253         rd64(hw, NGBE_MACRX1024TOMAXL);
254         rd64(hw, NGBE_MACTX1TO64L);
255         rd64(hw, NGBE_MACTX65TO127L);
256         rd64(hw, NGBE_MACTX128TO255L);
257         rd64(hw, NGBE_MACTX256TO511L);
258         rd64(hw, NGBE_MACTX512TO1023L);
259         rd64(hw, NGBE_MACTX1024TOMAXL);
260
261         rd64(hw, NGBE_MACRXERRLENL);
262         rd32(hw, NGBE_MACRXOVERSIZE);
263         rd32(hw, NGBE_MACRXJABBER);
264
265         /* MACsec Stats */
266         rd32(hw, NGBE_LSECTX_UTPKT);
267         rd32(hw, NGBE_LSECTX_ENCPKT);
268         rd32(hw, NGBE_LSECTX_PROTPKT);
269         rd32(hw, NGBE_LSECTX_ENCOCT);
270         rd32(hw, NGBE_LSECTX_PROTOCT);
271         rd32(hw, NGBE_LSECRX_UTPKT);
272         rd32(hw, NGBE_LSECRX_BTPKT);
273         rd32(hw, NGBE_LSECRX_NOSCIPKT);
274         rd32(hw, NGBE_LSECRX_UNSCIPKT);
275         rd32(hw, NGBE_LSECRX_DECOCT);
276         rd32(hw, NGBE_LSECRX_VLDOCT);
277         rd32(hw, NGBE_LSECRX_UNCHKPKT);
278         rd32(hw, NGBE_LSECRX_DLYPKT);
279         rd32(hw, NGBE_LSECRX_LATEPKT);
280         for (i = 0; i < 2; i++) {
281                 rd32(hw, NGBE_LSECRX_OKPKT(i));
282                 rd32(hw, NGBE_LSECRX_INVPKT(i));
283                 rd32(hw, NGBE_LSECRX_BADPKT(i));
284         }
285         for (i = 0; i < 4; i++) {
286                 rd32(hw, NGBE_LSECRX_INVSAPKT(i));
287                 rd32(hw, NGBE_LSECRX_BADSAPKT(i));
288         }
289
290         return 0;
291 }
292
293 /**
294  *  ngbe_get_mac_addr - Generic get MAC address
295  *  @hw: pointer to hardware structure
296  *  @mac_addr: Adapter MAC address
297  *
298  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
299  *  A reset of the adapter must be performed prior to calling this function
300  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
301  **/
302 s32 ngbe_get_mac_addr(struct ngbe_hw *hw, u8 *mac_addr)
303 {
304         u32 rar_high;
305         u32 rar_low;
306         u16 i;
307
308         DEBUGFUNC("ngbe_get_mac_addr");
309
310         wr32(hw, NGBE_ETHADDRIDX, 0);
311         rar_high = rd32(hw, NGBE_ETHADDRH);
312         rar_low = rd32(hw, NGBE_ETHADDRL);
313
314         for (i = 0; i < 2; i++)
315                 mac_addr[i] = (u8)(rar_high >> (1 - i) * 8);
316
317         for (i = 0; i < 4; i++)
318                 mac_addr[i + 2] = (u8)(rar_low >> (3 - i) * 8);
319
320         return 0;
321 }
322
323 /**
324  *  ngbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
325  *  @hw: pointer to the HW structure
326  *
327  *  Determines the LAN function id by reading memory-mapped registers and swaps
328  *  the port value if requested, and set MAC instance for devices.
329  **/
330 void ngbe_set_lan_id_multi_port(struct ngbe_hw *hw)
331 {
332         struct ngbe_bus_info *bus = &hw->bus;
333         u32 reg = 0;
334
335         DEBUGFUNC("ngbe_set_lan_id_multi_port");
336
337         reg = rd32(hw, NGBE_PORTSTAT);
338         bus->lan_id = NGBE_PORTSTAT_ID(reg);
339         bus->func = bus->lan_id;
340 }
341
342 /**
343  *  ngbe_stop_hw - Generic stop Tx/Rx units
344  *  @hw: pointer to hardware structure
345  *
346  *  Sets the adapter_stopped flag within ngbe_hw struct. Clears interrupts,
347  *  disables transmit and receive units. The adapter_stopped flag is used by
348  *  the shared code and drivers to determine if the adapter is in a stopped
349  *  state and should not touch the hardware.
350  **/
351 s32 ngbe_stop_hw(struct ngbe_hw *hw)
352 {
353         u16 i;
354         s32 status = 0;
355
356         DEBUGFUNC("ngbe_stop_hw");
357
358         /*
359          * Set the adapter_stopped flag so other driver functions stop touching
360          * the hardware
361          */
362         hw->adapter_stopped = true;
363
364         /* Disable the receive unit */
365         ngbe_disable_rx(hw);
366
367         /* Clear interrupt mask to stop interrupts from being generated */
368         wr32(hw, NGBE_IENMISC, 0);
369         wr32(hw, NGBE_IMS(0), NGBE_IMS_MASK);
370
371         /* Clear any pending interrupts, flush previous writes */
372         wr32(hw, NGBE_ICRMISC, NGBE_ICRMISC_MASK);
373         wr32(hw, NGBE_ICR(0), NGBE_ICR_MASK);
374
375         wr32(hw, NGBE_BMECTL, 0x3);
376
377         /* Disable the receive unit by stopping each queue */
378         for (i = 0; i < hw->mac.max_rx_queues; i++)
379                 wr32(hw, NGBE_RXCFG(i), 0);
380
381         /* flush all queues disables */
382         ngbe_flush(hw);
383         msec_delay(2);
384
385         /*
386          * Prevent the PCI-E bus from hanging by disabling PCI-E master
387          * access and verify no pending requests
388          */
389         status = ngbe_set_pcie_master(hw, false);
390         if (status)
391                 return status;
392
393         /* Disable the transmit unit.  Each queue must be disabled. */
394         for (i = 0; i < hw->mac.max_tx_queues; i++)
395                 wr32(hw, NGBE_TXCFG(i), 0);
396
397         /* flush all queues disables */
398         ngbe_flush(hw);
399         msec_delay(2);
400
401         return 0;
402 }
403
404 /**
405  *  ngbe_led_on - Turns on the software controllable LEDs.
406  *  @hw: pointer to hardware structure
407  *  @index: led number to turn on
408  **/
409 s32 ngbe_led_on(struct ngbe_hw *hw, u32 index)
410 {
411         u32 led_reg = rd32(hw, NGBE_LEDCTL);
412
413         DEBUGFUNC("ngbe_led_on");
414
415         if (index > 3)
416                 return NGBE_ERR_PARAM;
417
418         /* To turn on the LED, set mode to ON. */
419         led_reg |= NGBE_LEDCTL_100M;
420         wr32(hw, NGBE_LEDCTL, led_reg);
421         ngbe_flush(hw);
422
423         return 0;
424 }
425
426 /**
427  *  ngbe_led_off - Turns off the software controllable LEDs.
428  *  @hw: pointer to hardware structure
429  *  @index: led number to turn off
430  **/
431 s32 ngbe_led_off(struct ngbe_hw *hw, u32 index)
432 {
433         u32 led_reg = rd32(hw, NGBE_LEDCTL);
434
435         DEBUGFUNC("ngbe_led_off");
436
437         if (index > 3)
438                 return NGBE_ERR_PARAM;
439
440         /* To turn off the LED, set mode to OFF. */
441         led_reg &= ~NGBE_LEDCTL_100M;
442         wr32(hw, NGBE_LEDCTL, led_reg);
443         ngbe_flush(hw);
444
445         return 0;
446 }
447
448 /**
449  *  ngbe_validate_mac_addr - Validate MAC address
450  *  @mac_addr: pointer to MAC address.
451  *
452  *  Tests a MAC address to ensure it is a valid Individual Address.
453  **/
454 s32 ngbe_validate_mac_addr(u8 *mac_addr)
455 {
456         s32 status = 0;
457
458         DEBUGFUNC("ngbe_validate_mac_addr");
459
460         /* Make sure it is not a multicast address */
461         if (NGBE_IS_MULTICAST((struct rte_ether_addr *)mac_addr)) {
462                 status = NGBE_ERR_INVALID_MAC_ADDR;
463         /* Not a broadcast address */
464         } else if (NGBE_IS_BROADCAST((struct rte_ether_addr *)mac_addr)) {
465                 status = NGBE_ERR_INVALID_MAC_ADDR;
466         /* Reject the zero address */
467         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
468                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
469                 status = NGBE_ERR_INVALID_MAC_ADDR;
470         }
471         return status;
472 }
473
474 /**
475  *  ngbe_set_rar - Set Rx address register
476  *  @hw: pointer to hardware structure
477  *  @index: Receive address register to write
478  *  @addr: Address to put into receive address register
479  *  @vmdq: VMDq "set" or "pool" index
480  *  @enable_addr: set flag that address is active
481  *
482  *  Puts an ethernet address into a receive address register.
483  **/
484 s32 ngbe_set_rar(struct ngbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
485                           u32 enable_addr)
486 {
487         u32 rar_low, rar_high;
488         u32 rar_entries = hw->mac.num_rar_entries;
489
490         DEBUGFUNC("ngbe_set_rar");
491
492         /* Make sure we are using a valid rar index range */
493         if (index >= rar_entries) {
494                 DEBUGOUT("RAR index %d is out of range.\n", index);
495                 return NGBE_ERR_INVALID_ARGUMENT;
496         }
497
498         /* setup VMDq pool selection before this RAR gets enabled */
499         hw->mac.set_vmdq(hw, index, vmdq);
500
501         /*
502          * HW expects these in little endian so we reverse the byte
503          * order from network order (big endian) to little endian
504          */
505         rar_low = NGBE_ETHADDRL_AD0(addr[5]) |
506                   NGBE_ETHADDRL_AD1(addr[4]) |
507                   NGBE_ETHADDRL_AD2(addr[3]) |
508                   NGBE_ETHADDRL_AD3(addr[2]);
509         /*
510          * Some parts put the VMDq setting in the extra RAH bits,
511          * so save everything except the lower 16 bits that hold part
512          * of the address and the address valid bit.
513          */
514         rar_high = rd32(hw, NGBE_ETHADDRH);
515         rar_high &= ~NGBE_ETHADDRH_AD_MASK;
516         rar_high |= (NGBE_ETHADDRH_AD4(addr[1]) |
517                      NGBE_ETHADDRH_AD5(addr[0]));
518
519         rar_high &= ~NGBE_ETHADDRH_VLD;
520         if (enable_addr != 0)
521                 rar_high |= NGBE_ETHADDRH_VLD;
522
523         wr32(hw, NGBE_ETHADDRIDX, index);
524         wr32(hw, NGBE_ETHADDRL, rar_low);
525         wr32(hw, NGBE_ETHADDRH, rar_high);
526
527         return 0;
528 }
529
530 /**
531  *  ngbe_clear_rar - Remove Rx address register
532  *  @hw: pointer to hardware structure
533  *  @index: Receive address register to write
534  *
535  *  Clears an ethernet address from a receive address register.
536  **/
537 s32 ngbe_clear_rar(struct ngbe_hw *hw, u32 index)
538 {
539         u32 rar_high;
540         u32 rar_entries = hw->mac.num_rar_entries;
541
542         DEBUGFUNC("ngbe_clear_rar");
543
544         /* Make sure we are using a valid rar index range */
545         if (index >= rar_entries) {
546                 DEBUGOUT("RAR index %d is out of range.\n", index);
547                 return NGBE_ERR_INVALID_ARGUMENT;
548         }
549
550         /*
551          * Some parts put the VMDq setting in the extra RAH bits,
552          * so save everything except the lower 16 bits that hold part
553          * of the address and the address valid bit.
554          */
555         wr32(hw, NGBE_ETHADDRIDX, index);
556         rar_high = rd32(hw, NGBE_ETHADDRH);
557         rar_high &= ~(NGBE_ETHADDRH_AD_MASK | NGBE_ETHADDRH_VLD);
558
559         wr32(hw, NGBE_ETHADDRL, 0);
560         wr32(hw, NGBE_ETHADDRH, rar_high);
561
562         /* clear VMDq pool/queue selection for this RAR */
563         hw->mac.clear_vmdq(hw, index, BIT_MASK32);
564
565         return 0;
566 }
567
568 /**
569  *  ngbe_init_rx_addrs - Initializes receive address filters.
570  *  @hw: pointer to hardware structure
571  *
572  *  Places the MAC address in receive address register 0 and clears the rest
573  *  of the receive address registers. Clears the multicast table. Assumes
574  *  the receiver is in reset when the routine is called.
575  **/
576 s32 ngbe_init_rx_addrs(struct ngbe_hw *hw)
577 {
578         u32 i;
579         u32 psrctl;
580         u32 rar_entries = hw->mac.num_rar_entries;
581
582         DEBUGFUNC("ngbe_init_rx_addrs");
583
584         /*
585          * If the current mac address is valid, assume it is a software override
586          * to the permanent address.
587          * Otherwise, use the permanent address from the eeprom.
588          */
589         if (ngbe_validate_mac_addr(hw->mac.addr) ==
590             NGBE_ERR_INVALID_MAC_ADDR) {
591                 /* Get the MAC address from the RAR0 for later reference */
592                 hw->mac.get_mac_addr(hw, hw->mac.addr);
593
594                 DEBUGOUT(" Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
595                           hw->mac.addr[0], hw->mac.addr[1],
596                           hw->mac.addr[2]);
597                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
598                           hw->mac.addr[4], hw->mac.addr[5]);
599         } else {
600                 /* Setup the receive address. */
601                 DEBUGOUT("Overriding MAC Address in RAR[0]\n");
602                 DEBUGOUT(" New MAC Addr =%.2X %.2X %.2X ",
603                           hw->mac.addr[0], hw->mac.addr[1],
604                           hw->mac.addr[2]);
605                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
606                           hw->mac.addr[4], hw->mac.addr[5]);
607
608                 hw->mac.set_rar(hw, 0, hw->mac.addr, 0, true);
609         }
610
611         /* clear VMDq pool/queue selection for RAR 0 */
612         hw->mac.clear_vmdq(hw, 0, BIT_MASK32);
613
614         /* Zero out the other receive addresses. */
615         DEBUGOUT("Clearing RAR[1-%d]\n", rar_entries - 1);
616         for (i = 1; i < rar_entries; i++) {
617                 wr32(hw, NGBE_ETHADDRIDX, i);
618                 wr32(hw, NGBE_ETHADDRL, 0);
619                 wr32(hw, NGBE_ETHADDRH, 0);
620         }
621
622         /* Clear the MTA */
623         hw->addr_ctrl.mta_in_use = 0;
624         psrctl = rd32(hw, NGBE_PSRCTL);
625         psrctl &= ~(NGBE_PSRCTL_ADHF12_MASK | NGBE_PSRCTL_MCHFENA);
626         psrctl |= NGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
627         wr32(hw, NGBE_PSRCTL, psrctl);
628
629         DEBUGOUT(" Clearing MTA\n");
630         for (i = 0; i < hw->mac.mcft_size; i++)
631                 wr32(hw, NGBE_MCADDRTBL(i), 0);
632
633         ngbe_init_uta_tables(hw);
634
635         return 0;
636 }
637
638 /**
639  *  ngbe_mta_vector - Determines bit-vector in multicast table to set
640  *  @hw: pointer to hardware structure
641  *  @mc_addr: the multicast address
642  *
643  *  Extracts the 12 bits, from a multicast address, to determine which
644  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
645  *  incoming rx multicast addresses, to determine the bit-vector to check in
646  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
647  *  by the MO field of the PSRCTRL. The MO field is set during initialization
648  *  to mc_filter_type.
649  **/
650 static s32 ngbe_mta_vector(struct ngbe_hw *hw, u8 *mc_addr)
651 {
652         u32 vector = 0;
653
654         DEBUGFUNC("ngbe_mta_vector");
655
656         switch (hw->mac.mc_filter_type) {
657         case 0:   /* use bits [47:36] of the address */
658                 vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
659                 break;
660         case 1:   /* use bits [46:35] of the address */
661                 vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
662                 break;
663         case 2:   /* use bits [45:34] of the address */
664                 vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
665                 break;
666         case 3:   /* use bits [43:32] of the address */
667                 vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
668                 break;
669         default:  /* Invalid mc_filter_type */
670                 DEBUGOUT("MC filter type param set incorrectly\n");
671                 ASSERT(0);
672                 break;
673         }
674
675         /* vector can only be 12-bits or boundary will be exceeded */
676         vector &= 0xFFF;
677         return vector;
678 }
679
680 /**
681  *  ngbe_set_mta - Set bit-vector in multicast table
682  *  @hw: pointer to hardware structure
683  *  @mc_addr: Multicast address
684  *
685  *  Sets the bit-vector in the multicast table.
686  **/
687 void ngbe_set_mta(struct ngbe_hw *hw, u8 *mc_addr)
688 {
689         u32 vector;
690         u32 vector_bit;
691         u32 vector_reg;
692
693         DEBUGFUNC("ngbe_set_mta");
694
695         hw->addr_ctrl.mta_in_use++;
696
697         vector = ngbe_mta_vector(hw, mc_addr);
698         DEBUGOUT(" bit-vector = 0x%03X\n", vector);
699
700         /*
701          * The MTA is a register array of 128 32-bit registers. It is treated
702          * like an array of 4096 bits.  We want to set bit
703          * BitArray[vector_value]. So we figure out what register the bit is
704          * in, read it, OR in the new bit, then write back the new value.  The
705          * register is determined by the upper 7 bits of the vector value and
706          * the bit within that register are determined by the lower 5 bits of
707          * the value.
708          */
709         vector_reg = (vector >> 5) & 0x7F;
710         vector_bit = vector & 0x1F;
711         hw->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
712 }
713
714 /**
715  *  ngbe_update_mc_addr_list - Updates MAC list of multicast addresses
716  *  @hw: pointer to hardware structure
717  *  @mc_addr_list: the list of new multicast addresses
718  *  @mc_addr_count: number of addresses
719  *  @next: iterator function to walk the multicast address list
720  *  @clear: flag, when set clears the table beforehand
721  *
722  *  When the clear flag is set, the given list replaces any existing list.
723  *  Hashes the given addresses into the multicast table.
724  **/
725 s32 ngbe_update_mc_addr_list(struct ngbe_hw *hw, u8 *mc_addr_list,
726                                       u32 mc_addr_count, ngbe_mc_addr_itr next,
727                                       bool clear)
728 {
729         u32 i;
730         u32 vmdq;
731
732         DEBUGFUNC("ngbe_update_mc_addr_list");
733
734         /*
735          * Set the new number of MC addresses that we are being requested to
736          * use.
737          */
738         hw->addr_ctrl.num_mc_addrs = mc_addr_count;
739         hw->addr_ctrl.mta_in_use = 0;
740
741         /* Clear mta_shadow */
742         if (clear) {
743                 DEBUGOUT(" Clearing MTA\n");
744                 memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
745         }
746
747         /* Update mta_shadow */
748         for (i = 0; i < mc_addr_count; i++) {
749                 DEBUGOUT(" Adding the multicast addresses:\n");
750                 ngbe_set_mta(hw, next(hw, &mc_addr_list, &vmdq));
751         }
752
753         /* Enable mta */
754         for (i = 0; i < hw->mac.mcft_size; i++)
755                 wr32a(hw, NGBE_MCADDRTBL(0), i,
756                                       hw->mac.mta_shadow[i]);
757
758         if (hw->addr_ctrl.mta_in_use > 0) {
759                 u32 psrctl = rd32(hw, NGBE_PSRCTL);
760                 psrctl &= ~(NGBE_PSRCTL_ADHF12_MASK | NGBE_PSRCTL_MCHFENA);
761                 psrctl |= NGBE_PSRCTL_MCHFENA |
762                          NGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
763                 wr32(hw, NGBE_PSRCTL, psrctl);
764         }
765
766         DEBUGOUT("ngbe update mc addr list complete\n");
767         return 0;
768 }
769
770 /**
771  *  ngbe_setup_fc_em - Set up flow control
772  *  @hw: pointer to hardware structure
773  *
774  *  Called at init time to set up flow control.
775  **/
776 s32 ngbe_setup_fc_em(struct ngbe_hw *hw)
777 {
778         s32 err = 0;
779         u16 reg_cu = 0;
780
781         DEBUGFUNC("ngbe_setup_fc");
782
783         /* Validate the requested mode */
784         if (hw->fc.strict_ieee && hw->fc.requested_mode == ngbe_fc_rx_pause) {
785                 DEBUGOUT("ngbe_fc_rx_pause not valid in strict IEEE mode\n");
786                 err = NGBE_ERR_INVALID_LINK_SETTINGS;
787                 goto out;
788         }
789
790         /*
791          * 1gig parts do not have a word in the EEPROM to determine the
792          * default flow control setting, so we explicitly set it to full.
793          */
794         if (hw->fc.requested_mode == ngbe_fc_default)
795                 hw->fc.requested_mode = ngbe_fc_full;
796
797         /*
798          * The possible values of fc.requested_mode are:
799          * 0: Flow control is completely disabled
800          * 1: Rx flow control is enabled (we can receive pause frames,
801          *    but not send pause frames).
802          * 2: Tx flow control is enabled (we can send pause frames but
803          *    we do not support receiving pause frames).
804          * 3: Both Rx and Tx flow control (symmetric) are enabled.
805          * other: Invalid.
806          */
807         switch (hw->fc.requested_mode) {
808         case ngbe_fc_none:
809                 /* Flow control completely disabled by software override. */
810                 break;
811         case ngbe_fc_tx_pause:
812                 /*
813                  * Tx Flow control is enabled, and Rx Flow control is
814                  * disabled by software override.
815                  */
816                 if (hw->phy.type == ngbe_phy_mvl_sfi ||
817                         hw->phy.type == ngbe_phy_yt8521s_sfi)
818                         reg_cu |= MVL_FANA_ASM_PAUSE;
819                 else
820                         reg_cu |= 0x800; /*need to merge rtl and mvl on page 0*/
821                 break;
822         case ngbe_fc_rx_pause:
823                 /*
824                  * Rx Flow control is enabled and Tx Flow control is
825                  * disabled by software override. Since there really
826                  * isn't a way to advertise that we are capable of RX
827                  * Pause ONLY, we will advertise that we support both
828                  * symmetric and asymmetric Rx PAUSE, as such we fall
829                  * through to the fc_full statement.  Later, we will
830                  * disable the adapter's ability to send PAUSE frames.
831                  */
832         case ngbe_fc_full:
833                 /* Flow control (both Rx and Tx) is enabled by SW override. */
834                 if (hw->phy.type == ngbe_phy_mvl_sfi ||
835                         hw->phy.type == ngbe_phy_yt8521s_sfi)
836                         reg_cu |= MVL_FANA_SYM_PAUSE;
837                 else
838                         reg_cu |= 0xC00; /*need to merge rtl and mvl on page 0*/
839                 break;
840         default:
841                 DEBUGOUT("Flow control param set incorrectly\n");
842                 err = NGBE_ERR_CONFIG;
843                 goto out;
844         }
845
846         err = hw->phy.set_pause_adv(hw, reg_cu);
847
848 out:
849         return err;
850 }
851
852 /**
853  *  ngbe_fc_enable - Enable flow control
854  *  @hw: pointer to hardware structure
855  *
856  *  Enable flow control according to the current settings.
857  **/
858 s32 ngbe_fc_enable(struct ngbe_hw *hw)
859 {
860         s32 err = 0;
861         u32 mflcn_reg, fccfg_reg;
862         u32 pause_time;
863         u32 fcrtl, fcrth;
864
865         DEBUGFUNC("ngbe_fc_enable");
866
867         /* Validate the water mark configuration */
868         if (!hw->fc.pause_time) {
869                 err = NGBE_ERR_INVALID_LINK_SETTINGS;
870                 goto out;
871         }
872
873         /* Low water mark of zero causes XOFF floods */
874         if ((hw->fc.current_mode & ngbe_fc_tx_pause) && hw->fc.high_water) {
875                 if (!hw->fc.low_water ||
876                         hw->fc.low_water >= hw->fc.high_water) {
877                         DEBUGOUT("Invalid water mark configuration\n");
878                         err = NGBE_ERR_INVALID_LINK_SETTINGS;
879                         goto out;
880                 }
881         }
882
883         /* Negotiate the fc mode to use */
884         hw->mac.fc_autoneg(hw);
885
886         /* Disable any previous flow control settings */
887         mflcn_reg = rd32(hw, NGBE_RXFCCFG);
888         mflcn_reg &= ~NGBE_RXFCCFG_FC;
889
890         fccfg_reg = rd32(hw, NGBE_TXFCCFG);
891         fccfg_reg &= ~NGBE_TXFCCFG_FC;
892         /*
893          * The possible values of fc.current_mode are:
894          * 0: Flow control is completely disabled
895          * 1: Rx flow control is enabled (we can receive pause frames,
896          *    but not send pause frames).
897          * 2: Tx flow control is enabled (we can send pause frames but
898          *    we do not support receiving pause frames).
899          * 3: Both Rx and Tx flow control (symmetric) are enabled.
900          * other: Invalid.
901          */
902         switch (hw->fc.current_mode) {
903         case ngbe_fc_none:
904                 /*
905                  * Flow control is disabled by software override or autoneg.
906                  * The code below will actually disable it in the HW.
907                  */
908                 break;
909         case ngbe_fc_rx_pause:
910                 /*
911                  * Rx Flow control is enabled and Tx Flow control is
912                  * disabled by software override. Since there really
913                  * isn't a way to advertise that we are capable of RX
914                  * Pause ONLY, we will advertise that we support both
915                  * symmetric and asymmetric Rx PAUSE.  Later, we will
916                  * disable the adapter's ability to send PAUSE frames.
917                  */
918                 mflcn_reg |= NGBE_RXFCCFG_FC;
919                 break;
920         case ngbe_fc_tx_pause:
921                 /*
922                  * Tx Flow control is enabled, and Rx Flow control is
923                  * disabled by software override.
924                  */
925                 fccfg_reg |= NGBE_TXFCCFG_FC;
926                 break;
927         case ngbe_fc_full:
928                 /* Flow control (both Rx and Tx) is enabled by SW override. */
929                 mflcn_reg |= NGBE_RXFCCFG_FC;
930                 fccfg_reg |= NGBE_TXFCCFG_FC;
931                 break;
932         default:
933                 DEBUGOUT("Flow control param set incorrectly\n");
934                 err = NGBE_ERR_CONFIG;
935                 goto out;
936         }
937
938         /* Set 802.3x based flow control settings. */
939         wr32(hw, NGBE_RXFCCFG, mflcn_reg);
940         wr32(hw, NGBE_TXFCCFG, fccfg_reg);
941
942         /* Set up and enable Rx high/low water mark thresholds, enable XON. */
943         if ((hw->fc.current_mode & ngbe_fc_tx_pause) &&
944                 hw->fc.high_water) {
945                 fcrtl = NGBE_FCWTRLO_TH(hw->fc.low_water) |
946                         NGBE_FCWTRLO_XON;
947                 fcrth = NGBE_FCWTRHI_TH(hw->fc.high_water) |
948                         NGBE_FCWTRHI_XOFF;
949         } else {
950                 /*
951                  * In order to prevent Tx hangs when the internal Tx
952                  * switch is enabled we must set the high water mark
953                  * to the Rx packet buffer size - 24KB.  This allows
954                  * the Tx switch to function even under heavy Rx
955                  * workloads.
956                  */
957                 fcrtl = 0;
958                 fcrth = rd32(hw, NGBE_PBRXSIZE) - 24576;
959         }
960         wr32(hw, NGBE_FCWTRLO, fcrtl);
961         wr32(hw, NGBE_FCWTRHI, fcrth);
962
963         /* Configure pause time */
964         pause_time = NGBE_RXFCFSH_TIME(hw->fc.pause_time);
965         wr32(hw, NGBE_FCXOFFTM, pause_time * 0x00010000);
966
967         /* Configure flow control refresh threshold value */
968         wr32(hw, NGBE_RXFCRFSH, hw->fc.pause_time / 2);
969
970 out:
971         return err;
972 }
973
974 /**
975  *  ngbe_negotiate_fc - Negotiate flow control
976  *  @hw: pointer to hardware structure
977  *  @adv_reg: flow control advertised settings
978  *  @lp_reg: link partner's flow control settings
979  *  @adv_sym: symmetric pause bit in advertisement
980  *  @adv_asm: asymmetric pause bit in advertisement
981  *  @lp_sym: symmetric pause bit in link partner advertisement
982  *  @lp_asm: asymmetric pause bit in link partner advertisement
983  *
984  *  Find the intersection between advertised settings and link partner's
985  *  advertised settings
986  **/
987 s32 ngbe_negotiate_fc(struct ngbe_hw *hw, u32 adv_reg, u32 lp_reg,
988                        u32 adv_sym, u32 adv_asm, u32 lp_sym, u32 lp_asm)
989 {
990         if ((!(adv_reg)) ||  (!(lp_reg))) {
991                 DEBUGOUT("Local or link partner's advertised flow control "
992                          "settings are NULL. Local: %x, link partner: %x\n",
993                               adv_reg, lp_reg);
994                 return NGBE_ERR_FC_NOT_NEGOTIATED;
995         }
996
997         if ((adv_reg & adv_sym) && (lp_reg & lp_sym)) {
998                 /*
999                  * Now we need to check if the user selected Rx ONLY
1000                  * of pause frames.  In this case, we had to advertise
1001                  * FULL flow control because we could not advertise RX
1002                  * ONLY. Hence, we must now check to see if we need to
1003                  * turn OFF the TRANSMISSION of PAUSE frames.
1004                  */
1005                 if (hw->fc.requested_mode == ngbe_fc_full) {
1006                         hw->fc.current_mode = ngbe_fc_full;
1007                         DEBUGOUT("Flow Control = FULL.\n");
1008                 } else {
1009                         hw->fc.current_mode = ngbe_fc_rx_pause;
1010                         DEBUGOUT("Flow Control=RX PAUSE frames only\n");
1011                 }
1012         } else if (!(adv_reg & adv_sym) && (adv_reg & adv_asm) &&
1013                    (lp_reg & lp_sym) && (lp_reg & lp_asm)) {
1014                 hw->fc.current_mode = ngbe_fc_tx_pause;
1015                 DEBUGOUT("Flow Control = TX PAUSE frames only.\n");
1016         } else if ((adv_reg & adv_sym) && (adv_reg & adv_asm) &&
1017                    !(lp_reg & lp_sym) && (lp_reg & lp_asm)) {
1018                 hw->fc.current_mode = ngbe_fc_rx_pause;
1019                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
1020         } else {
1021                 hw->fc.current_mode = ngbe_fc_none;
1022                 DEBUGOUT("Flow Control = NONE.\n");
1023         }
1024         return 0;
1025 }
1026
1027 /**
1028  *  ngbe_fc_autoneg_em - Enable flow control IEEE clause 37
1029  *  @hw: pointer to hardware structure
1030  *
1031  *  Enable flow control according to IEEE clause 37.
1032  **/
1033 STATIC s32 ngbe_fc_autoneg_em(struct ngbe_hw *hw)
1034 {
1035         u8 technology_ability_reg = 0;
1036         u8 lp_technology_ability_reg = 0;
1037
1038         hw->phy.get_adv_pause(hw, &technology_ability_reg);
1039         hw->phy.get_lp_adv_pause(hw, &lp_technology_ability_reg);
1040
1041         return ngbe_negotiate_fc(hw, (u32)technology_ability_reg,
1042                                   (u32)lp_technology_ability_reg,
1043                                   NGBE_TAF_SYM_PAUSE, NGBE_TAF_ASM_PAUSE,
1044                                   NGBE_TAF_SYM_PAUSE, NGBE_TAF_ASM_PAUSE);
1045 }
1046
1047 /**
1048  *  ngbe_fc_autoneg - Configure flow control
1049  *  @hw: pointer to hardware structure
1050  *
1051  *  Compares our advertised flow control capabilities to those advertised by
1052  *  our link partner, and determines the proper flow control mode to use.
1053  **/
1054 void ngbe_fc_autoneg(struct ngbe_hw *hw)
1055 {
1056         s32 err = NGBE_ERR_FC_NOT_NEGOTIATED;
1057         u32 speed;
1058         bool link_up;
1059
1060         DEBUGFUNC("ngbe_fc_autoneg");
1061
1062         /*
1063          * AN should have completed when the cable was plugged in.
1064          * Look for reasons to bail out.  Bail out if:
1065          * - FC autoneg is disabled, or if
1066          * - link is not up.
1067          */
1068         if (hw->fc.disable_fc_autoneg) {
1069                 DEBUGOUT("Flow control autoneg is disabled");
1070                 goto out;
1071         }
1072
1073         hw->mac.check_link(hw, &speed, &link_up, false);
1074         if (!link_up) {
1075                 DEBUGOUT("The link is down");
1076                 goto out;
1077         }
1078
1079         err = ngbe_fc_autoneg_em(hw);
1080
1081 out:
1082         if (err == 0) {
1083                 hw->fc.fc_was_autonegged = true;
1084         } else {
1085                 hw->fc.fc_was_autonegged = false;
1086                 hw->fc.current_mode = hw->fc.requested_mode;
1087         }
1088 }
1089
1090 /**
1091  *  ngbe_set_pcie_master - Disable or Enable PCI-express master access
1092  *  @hw: pointer to hardware structure
1093  *
1094  *  Disables PCI-Express master access and verifies there are no pending
1095  *  requests. NGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
1096  *  bit hasn't caused the master requests to be disabled, else 0
1097  *  is returned signifying master requests disabled.
1098  **/
1099 s32 ngbe_set_pcie_master(struct ngbe_hw *hw, bool enable)
1100 {
1101         s32 status = 0;
1102         u16 addr = 0x04;
1103         u32 data, i;
1104
1105         DEBUGFUNC("ngbe_set_pcie_master");
1106
1107         ngbe_hic_pcie_read(hw, addr, &data, 4);
1108         if (enable)
1109                 data |= 0x04;
1110         else
1111                 data &= ~0x04;
1112
1113         ngbe_hic_pcie_write(hw, addr, &data, 4);
1114
1115         if (enable)
1116                 goto out;
1117
1118         /* Exit if master requests are blocked */
1119         if (!(rd32(hw, NGBE_BMEPEND)) ||
1120             NGBE_REMOVED(hw->hw_addr))
1121                 goto out;
1122
1123         /* Poll for master request bit to clear */
1124         for (i = 0; i < NGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
1125                 usec_delay(100);
1126                 if (!(rd32(hw, NGBE_BMEPEND)))
1127                         goto out;
1128         }
1129
1130         DEBUGOUT("PCIe transaction pending bit also did not clear.\n");
1131         status = NGBE_ERR_MASTER_REQUESTS_PENDING;
1132
1133 out:
1134         return status;
1135 }
1136
1137 /**
1138  *  ngbe_acquire_swfw_sync - Acquire SWFW semaphore
1139  *  @hw: pointer to hardware structure
1140  *  @mask: Mask to specify which semaphore to acquire
1141  *
1142  *  Acquires the SWFW semaphore through the MNGSEM register for the specified
1143  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
1144  **/
1145 s32 ngbe_acquire_swfw_sync(struct ngbe_hw *hw, u32 mask)
1146 {
1147         u32 mngsem = 0;
1148         u32 fwsm = 0;
1149         u32 swmask = NGBE_MNGSEM_SW(mask);
1150         u32 fwmask = NGBE_MNGSEM_FW(mask);
1151         u32 timeout = 200;
1152         u32 i;
1153
1154         DEBUGFUNC("ngbe_acquire_swfw_sync");
1155
1156         for (i = 0; i < timeout; i++) {
1157                 /*
1158                  * SW NVM semaphore bit is used for access to all
1159                  * SW_FW_SYNC bits (not just NVM)
1160                  */
1161                 if (ngbe_get_eeprom_semaphore(hw))
1162                         return NGBE_ERR_SWFW_SYNC;
1163
1164                 mngsem = rd32(hw, NGBE_MNGSEM);
1165                 if (mngsem & (fwmask | swmask)) {
1166                         /* Resource is currently in use by FW or SW */
1167                         ngbe_release_eeprom_semaphore(hw);
1168                         msec_delay(5);
1169                 } else {
1170                         mngsem |= swmask;
1171                         wr32(hw, NGBE_MNGSEM, mngsem);
1172                         ngbe_release_eeprom_semaphore(hw);
1173                         return 0;
1174                 }
1175         }
1176
1177         fwsm = rd32(hw, NGBE_MNGFWSYNC);
1178         DEBUGOUT("SWFW semaphore not granted: MNG_SWFW_SYNC = 0x%x, MNG_FW_SM = 0x%x\n",
1179                         mngsem, fwsm);
1180
1181         msec_delay(5);
1182         return NGBE_ERR_SWFW_SYNC;
1183 }
1184
1185 /**
1186  *  ngbe_release_swfw_sync - Release SWFW semaphore
1187  *  @hw: pointer to hardware structure
1188  *  @mask: Mask to specify which semaphore to release
1189  *
1190  *  Releases the SWFW semaphore through the MNGSEM register for the specified
1191  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
1192  **/
1193 void ngbe_release_swfw_sync(struct ngbe_hw *hw, u32 mask)
1194 {
1195         u32 mngsem;
1196         u32 swmask = mask;
1197
1198         DEBUGFUNC("ngbe_release_swfw_sync");
1199
1200         ngbe_get_eeprom_semaphore(hw);
1201
1202         mngsem = rd32(hw, NGBE_MNGSEM);
1203         mngsem &= ~swmask;
1204         wr32(hw, NGBE_MNGSEM, mngsem);
1205
1206         ngbe_release_eeprom_semaphore(hw);
1207 }
1208
1209 /**
1210  *  ngbe_disable_sec_rx_path - Stops the receive data path
1211  *  @hw: pointer to hardware structure
1212  *
1213  *  Stops the receive data path and waits for the HW to internally empty
1214  *  the Rx security block
1215  **/
1216 s32 ngbe_disable_sec_rx_path(struct ngbe_hw *hw)
1217 {
1218 #define NGBE_MAX_SECRX_POLL 4000
1219
1220         int i;
1221         u32 secrxreg;
1222
1223         DEBUGFUNC("ngbe_disable_sec_rx_path");
1224
1225
1226         secrxreg = rd32(hw, NGBE_SECRXCTL);
1227         secrxreg |= NGBE_SECRXCTL_XDSA;
1228         wr32(hw, NGBE_SECRXCTL, secrxreg);
1229         for (i = 0; i < NGBE_MAX_SECRX_POLL; i++) {
1230                 secrxreg = rd32(hw, NGBE_SECRXSTAT);
1231                 if (!(secrxreg & NGBE_SECRXSTAT_RDY))
1232                         /* Use interrupt-safe sleep just in case */
1233                         usec_delay(10);
1234                 else
1235                         break;
1236         }
1237
1238         /* For informational purposes only */
1239         if (i >= NGBE_MAX_SECRX_POLL)
1240                 DEBUGOUT("Rx unit being enabled before security "
1241                          "path fully disabled.  Continuing with init.\n");
1242
1243         return 0;
1244 }
1245
1246 /**
1247  *  ngbe_enable_sec_rx_path - Enables the receive data path
1248  *  @hw: pointer to hardware structure
1249  *
1250  *  Enables the receive data path.
1251  **/
1252 s32 ngbe_enable_sec_rx_path(struct ngbe_hw *hw)
1253 {
1254         u32 secrxreg;
1255
1256         DEBUGFUNC("ngbe_enable_sec_rx_path");
1257
1258         secrxreg = rd32(hw, NGBE_SECRXCTL);
1259         secrxreg &= ~NGBE_SECRXCTL_XDSA;
1260         wr32(hw, NGBE_SECRXCTL, secrxreg);
1261         ngbe_flush(hw);
1262
1263         return 0;
1264 }
1265
1266 /**
1267  *  ngbe_clear_vmdq - Disassociate a VMDq pool index from a rx address
1268  *  @hw: pointer to hardware struct
1269  *  @rar: receive address register index to disassociate
1270  *  @vmdq: VMDq pool index to remove from the rar
1271  **/
1272 s32 ngbe_clear_vmdq(struct ngbe_hw *hw, u32 rar, u32 vmdq)
1273 {
1274         u32 mpsar;
1275         u32 rar_entries = hw->mac.num_rar_entries;
1276
1277         DEBUGFUNC("ngbe_clear_vmdq");
1278
1279         /* Make sure we are using a valid rar index range */
1280         if (rar >= rar_entries) {
1281                 DEBUGOUT("RAR index %d is out of range.\n", rar);
1282                 return NGBE_ERR_INVALID_ARGUMENT;
1283         }
1284
1285         wr32(hw, NGBE_ETHADDRIDX, rar);
1286         mpsar = rd32(hw, NGBE_ETHADDRASS);
1287
1288         if (NGBE_REMOVED(hw->hw_addr))
1289                 goto done;
1290
1291         if (!mpsar)
1292                 goto done;
1293
1294         mpsar &= ~(1 << vmdq);
1295         wr32(hw, NGBE_ETHADDRASS, mpsar);
1296
1297         /* was that the last pool using this rar? */
1298         if (mpsar == 0 && rar != 0)
1299                 hw->mac.clear_rar(hw, rar);
1300 done:
1301         return 0;
1302 }
1303
1304 /**
1305  *  ngbe_set_vmdq - Associate a VMDq pool index with a rx address
1306  *  @hw: pointer to hardware struct
1307  *  @rar: receive address register index to associate with a VMDq index
1308  *  @vmdq: VMDq pool index
1309  **/
1310 s32 ngbe_set_vmdq(struct ngbe_hw *hw, u32 rar, u32 vmdq)
1311 {
1312         u32 mpsar;
1313         u32 rar_entries = hw->mac.num_rar_entries;
1314
1315         DEBUGFUNC("ngbe_set_vmdq");
1316
1317         /* Make sure we are using a valid rar index range */
1318         if (rar >= rar_entries) {
1319                 DEBUGOUT("RAR index %d is out of range.\n", rar);
1320                 return NGBE_ERR_INVALID_ARGUMENT;
1321         }
1322
1323         wr32(hw, NGBE_ETHADDRIDX, rar);
1324
1325         mpsar = rd32(hw, NGBE_ETHADDRASS);
1326         mpsar |= 1 << vmdq;
1327         wr32(hw, NGBE_ETHADDRASS, mpsar);
1328
1329         return 0;
1330 }
1331
1332 /**
1333  *  ngbe_init_uta_tables - Initialize the Unicast Table Array
1334  *  @hw: pointer to hardware structure
1335  **/
1336 s32 ngbe_init_uta_tables(struct ngbe_hw *hw)
1337 {
1338         int i;
1339
1340         DEBUGFUNC("ngbe_init_uta_tables");
1341         DEBUGOUT(" Clearing UTA\n");
1342
1343         for (i = 0; i < 128; i++)
1344                 wr32(hw, NGBE_UCADDRTBL(i), 0);
1345
1346         return 0;
1347 }
1348
1349 /**
1350  *  ngbe_find_vlvf_slot - find the vlanid or the first empty slot
1351  *  @hw: pointer to hardware structure
1352  *  @vlan: VLAN id to write to VLAN filter
1353  *  @vlvf_bypass: true to find vlanid only, false returns first empty slot if
1354  *                vlanid not found
1355  *
1356  *
1357  *  return the VLVF index where this VLAN id should be placed
1358  *
1359  **/
1360 s32 ngbe_find_vlvf_slot(struct ngbe_hw *hw, u32 vlan, bool vlvf_bypass)
1361 {
1362         s32 regindex, first_empty_slot;
1363         u32 bits;
1364
1365         /* short cut the special case */
1366         if (vlan == 0)
1367                 return 0;
1368
1369         /* if vlvf_bypass is set we don't want to use an empty slot, we
1370          * will simply bypass the VLVF if there are no entries present in the
1371          * VLVF that contain our VLAN
1372          */
1373         first_empty_slot = vlvf_bypass ? NGBE_ERR_NO_SPACE : 0;
1374
1375         /* add VLAN enable bit for comparison */
1376         vlan |= NGBE_PSRVLAN_EA;
1377
1378         /* Search for the vlan id in the VLVF entries. Save off the first empty
1379          * slot found along the way.
1380          *
1381          * pre-decrement loop covering (NGBE_NUM_POOL - 1) .. 1
1382          */
1383         for (regindex = NGBE_NUM_POOL; --regindex;) {
1384                 wr32(hw, NGBE_PSRVLANIDX, regindex);
1385                 bits = rd32(hw, NGBE_PSRVLAN);
1386                 if (bits == vlan)
1387                         return regindex;
1388                 if (!first_empty_slot && !bits)
1389                         first_empty_slot = regindex;
1390         }
1391
1392         /* If we are here then we didn't find the VLAN.  Return first empty
1393          * slot we found during our search, else error.
1394          */
1395         if (!first_empty_slot)
1396                 DEBUGOUT("No space in VLVF.\n");
1397
1398         return first_empty_slot ? first_empty_slot : NGBE_ERR_NO_SPACE;
1399 }
1400
1401 /**
1402  *  ngbe_set_vfta - Set VLAN filter table
1403  *  @hw: pointer to hardware structure
1404  *  @vlan: VLAN id to write to VLAN filter
1405  *  @vind: VMDq output index that maps queue to VLAN id in VLVFB
1406  *  @vlan_on: boolean flag to turn on/off VLAN
1407  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
1408  *
1409  *  Turn on/off specified VLAN in the VLAN filter table.
1410  **/
1411 s32 ngbe_set_vfta(struct ngbe_hw *hw, u32 vlan, u32 vind,
1412                            bool vlan_on, bool vlvf_bypass)
1413 {
1414         u32 regidx, vfta_delta, vfta;
1415         s32 err;
1416
1417         DEBUGFUNC("ngbe_set_vfta");
1418
1419         if (vlan > 4095 || vind > 63)
1420                 return NGBE_ERR_PARAM;
1421
1422         /*
1423          * this is a 2 part operation - first the VFTA, then the
1424          * VLVF and VLVFB if VT Mode is set
1425          * We don't write the VFTA until we know the VLVF part succeeded.
1426          */
1427
1428         /* Part 1
1429          * The VFTA is a bitstring made up of 128 32-bit registers
1430          * that enable the particular VLAN id, much like the MTA:
1431          *    bits[11-5]: which register
1432          *    bits[4-0]:  which bit in the register
1433          */
1434         regidx = vlan / 32;
1435         vfta_delta = 1 << (vlan % 32);
1436         vfta = rd32(hw, NGBE_VLANTBL(regidx));
1437
1438         /*
1439          * vfta_delta represents the difference between the current value
1440          * of vfta and the value we want in the register.  Since the diff
1441          * is an XOR mask we can just update the vfta using an XOR
1442          */
1443         vfta_delta &= vlan_on ? ~vfta : vfta;
1444         vfta ^= vfta_delta;
1445
1446         /* Part 2
1447          * Call ngbe_set_vlvf to set VLVFB and VLVF
1448          */
1449         err = ngbe_set_vlvf(hw, vlan, vind, vlan_on, &vfta_delta,
1450                                          vfta, vlvf_bypass);
1451         if (err != 0) {
1452                 if (vlvf_bypass)
1453                         goto vfta_update;
1454                 return err;
1455         }
1456
1457 vfta_update:
1458         /* Update VFTA now that we are ready for traffic */
1459         if (vfta_delta)
1460                 wr32(hw, NGBE_VLANTBL(regidx), vfta);
1461
1462         return 0;
1463 }
1464
1465 /**
1466  *  ngbe_set_vlvf - Set VLAN Pool Filter
1467  *  @hw: pointer to hardware structure
1468  *  @vlan: VLAN id to write to VLAN filter
1469  *  @vind: VMDq output index that maps queue to VLAN id in PSRVLANPLM
1470  *  @vlan_on: boolean flag to turn on/off VLAN in PSRVLAN
1471  *  @vfta_delta: pointer to the difference between the current value
1472  *               of PSRVLANPLM and the desired value
1473  *  @vfta: the desired value of the VFTA
1474  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
1475  *
1476  *  Turn on/off specified bit in VLVF table.
1477  **/
1478 s32 ngbe_set_vlvf(struct ngbe_hw *hw, u32 vlan, u32 vind,
1479                            bool vlan_on, u32 *vfta_delta, u32 vfta,
1480                            bool vlvf_bypass)
1481 {
1482         u32 bits;
1483         u32 portctl;
1484         s32 vlvf_index;
1485
1486         DEBUGFUNC("ngbe_set_vlvf");
1487
1488         if (vlan > 4095 || vind > 63)
1489                 return NGBE_ERR_PARAM;
1490
1491         /* If VT Mode is set
1492          *   Either vlan_on
1493          *     make sure the vlan is in PSRVLAN
1494          *     set the vind bit in the matching PSRVLANPLM
1495          *   Or !vlan_on
1496          *     clear the pool bit and possibly the vind
1497          */
1498         portctl = rd32(hw, NGBE_PORTCTL);
1499         if (!(portctl & NGBE_PORTCTL_NUMVT_MASK))
1500                 return 0;
1501
1502         vlvf_index = ngbe_find_vlvf_slot(hw, vlan, vlvf_bypass);
1503         if (vlvf_index < 0)
1504                 return vlvf_index;
1505
1506         wr32(hw, NGBE_PSRVLANIDX, vlvf_index);
1507         bits = rd32(hw, NGBE_PSRVLANPLM(vind / 32));
1508
1509         /* set the pool bit */
1510         bits |= 1 << (vind % 32);
1511         if (vlan_on)
1512                 goto vlvf_update;
1513
1514         /* clear the pool bit */
1515         bits ^= 1 << (vind % 32);
1516
1517         if (!bits &&
1518             !rd32(hw, NGBE_PSRVLANPLM(vind / 32))) {
1519                 /* Clear PSRVLANPLM first, then disable PSRVLAN. Otherwise
1520                  * we run the risk of stray packets leaking into
1521                  * the PF via the default pool
1522                  */
1523                 if (*vfta_delta)
1524                         wr32(hw, NGBE_PSRVLANPLM(vlan / 32), vfta);
1525
1526                 /* disable VLVF and clear remaining bit from pool */
1527                 wr32(hw, NGBE_PSRVLAN, 0);
1528                 wr32(hw, NGBE_PSRVLANPLM(vind / 32), 0);
1529
1530                 return 0;
1531         }
1532
1533         /* If there are still bits set in the PSRVLANPLM registers
1534          * for the VLAN ID indicated we need to see if the
1535          * caller is requesting that we clear the PSRVLANPLM entry bit.
1536          * If the caller has requested that we clear the PSRVLANPLM
1537          * entry bit but there are still pools/VFs using this VLAN
1538          * ID entry then ignore the request.  We're not worried
1539          * about the case where we're turning the PSRVLANPLM VLAN ID
1540          * entry bit on, only when requested to turn it off as
1541          * there may be multiple pools and/or VFs using the
1542          * VLAN ID entry.  In that case we cannot clear the
1543          * PSRVLANPLM bit until all pools/VFs using that VLAN ID have also
1544          * been cleared.  This will be indicated by "bits" being
1545          * zero.
1546          */
1547         *vfta_delta = 0;
1548
1549 vlvf_update:
1550         /* record pool change and enable VLAN ID if not already enabled */
1551         wr32(hw, NGBE_PSRVLANPLM(vind / 32), bits);
1552         wr32(hw, NGBE_PSRVLAN, NGBE_PSRVLAN_EA | vlan);
1553
1554         return 0;
1555 }
1556
1557 /**
1558  *  ngbe_clear_vfta - Clear VLAN filter table
1559  *  @hw: pointer to hardware structure
1560  *
1561  *  Clears the VLAN filer table, and the VMDq index associated with the filter
1562  **/
1563 s32 ngbe_clear_vfta(struct ngbe_hw *hw)
1564 {
1565         u32 offset;
1566
1567         DEBUGFUNC("ngbe_clear_vfta");
1568
1569         for (offset = 0; offset < hw->mac.vft_size; offset++)
1570                 wr32(hw, NGBE_VLANTBL(offset), 0);
1571
1572         for (offset = 0; offset < NGBE_NUM_POOL; offset++) {
1573                 wr32(hw, NGBE_PSRVLANIDX, offset);
1574                 wr32(hw, NGBE_PSRVLAN, 0);
1575                 wr32(hw, NGBE_PSRVLANPLM(0), 0);
1576         }
1577
1578         return 0;
1579 }
1580
1581 /**
1582  *  ngbe_check_mac_link_em - Determine link and speed status
1583  *  @hw: pointer to hardware structure
1584  *  @speed: pointer to link speed
1585  *  @link_up: true when link is up
1586  *  @link_up_wait_to_complete: bool used to wait for link up or not
1587  *
1588  *  Reads the links register to determine if link is up and the current speed
1589  **/
1590 s32 ngbe_check_mac_link_em(struct ngbe_hw *hw, u32 *speed,
1591                         bool *link_up, bool link_up_wait_to_complete)
1592 {
1593         u32 i, reg;
1594         s32 status = 0;
1595
1596         DEBUGFUNC("ngbe_check_mac_link_em");
1597
1598         reg = rd32(hw, NGBE_GPIOINTSTAT);
1599         wr32(hw, NGBE_GPIOEOI, reg);
1600
1601         if (link_up_wait_to_complete) {
1602                 for (i = 0; i < hw->mac.max_link_up_time; i++) {
1603                         status = hw->phy.check_link(hw, speed, link_up);
1604                         if (*link_up)
1605                                 break;
1606                         msec_delay(100);
1607                 }
1608         } else {
1609                 status = hw->phy.check_link(hw, speed, link_up);
1610         }
1611
1612         return status;
1613 }
1614
1615 s32 ngbe_get_link_capabilities_em(struct ngbe_hw *hw,
1616                                       u32 *speed,
1617                                       bool *autoneg)
1618 {
1619         s32 status = 0;
1620
1621         DEBUGFUNC("\n");
1622
1623         hw->mac.autoneg = *autoneg;
1624
1625         switch (hw->sub_device_id) {
1626         case NGBE_SUB_DEV_ID_EM_RTL_SGMII:
1627                 *speed = NGBE_LINK_SPEED_1GB_FULL |
1628                         NGBE_LINK_SPEED_100M_FULL |
1629                         NGBE_LINK_SPEED_10M_FULL;
1630                 break;
1631         default:
1632                 break;
1633         }
1634
1635         return status;
1636 }
1637
1638 s32 ngbe_setup_mac_link_em(struct ngbe_hw *hw,
1639                                u32 speed,
1640                                bool autoneg_wait_to_complete)
1641 {
1642         s32 status;
1643
1644         DEBUGFUNC("\n");
1645
1646         /* Setup the PHY according to input speed */
1647         status = hw->phy.setup_link(hw, speed, autoneg_wait_to_complete);
1648
1649         return status;
1650 }
1651
1652 /**
1653  *  ngbe_set_mac_anti_spoofing - Enable/Disable MAC anti-spoofing
1654  *  @hw: pointer to hardware structure
1655  *  @enable: enable or disable switch for MAC anti-spoofing
1656  *  @vf: Virtual Function pool - VF Pool to set for MAC anti-spoofing
1657  *
1658  **/
1659 void ngbe_set_mac_anti_spoofing(struct ngbe_hw *hw, bool enable, int vf)
1660 {
1661         u32 pfvfspoof;
1662
1663         pfvfspoof = rd32(hw, NGBE_POOLTXASMAC);
1664         if (enable)
1665                 pfvfspoof |= (1 << vf);
1666         else
1667                 pfvfspoof &= ~(1 << vf);
1668         wr32(hw, NGBE_POOLTXASMAC, pfvfspoof);
1669 }
1670
1671 /**
1672  * ngbe_set_pba - Initialize Rx packet buffer
1673  * @hw: pointer to hardware structure
1674  * @headroom: reserve n KB of headroom
1675  **/
1676 void ngbe_set_pba(struct ngbe_hw *hw)
1677 {
1678         u32 rxpktsize = hw->mac.rx_pb_size;
1679         u32 txpktsize, txpbthresh;
1680
1681         /* Reserve 256 KB of headroom */
1682         rxpktsize -= 256;
1683
1684         rxpktsize <<= 10;
1685         wr32(hw, NGBE_PBRXSIZE, rxpktsize);
1686
1687         /* Only support an equally distributed Tx packet buffer strategy. */
1688         txpktsize = NGBE_PBTXSIZE_MAX;
1689         txpbthresh = (txpktsize / 1024) - NGBE_TXPKT_SIZE_MAX;
1690
1691         wr32(hw, NGBE_PBTXSIZE, txpktsize);
1692         wr32(hw, NGBE_PBTXDMATH, txpbthresh);
1693 }
1694
1695 /**
1696  *  ngbe_set_vlan_anti_spoofing - Enable/Disable VLAN anti-spoofing
1697  *  @hw: pointer to hardware structure
1698  *  @enable: enable or disable switch for VLAN anti-spoofing
1699  *  @vf: Virtual Function pool - VF Pool to set for VLAN anti-spoofing
1700  *
1701  **/
1702 void ngbe_set_vlan_anti_spoofing(struct ngbe_hw *hw, bool enable, int vf)
1703 {
1704         u32 pfvfspoof;
1705
1706         pfvfspoof = rd32(hw, NGBE_POOLTXASVLAN);
1707         if (enable)
1708                 pfvfspoof |= (1 << vf);
1709         else
1710                 pfvfspoof &= ~(1 << vf);
1711         wr32(hw, NGBE_POOLTXASVLAN, pfvfspoof);
1712 }
1713
1714 /**
1715  *  ngbe_init_thermal_sensor_thresh - Inits thermal sensor thresholds
1716  *  @hw: pointer to hardware structure
1717  *
1718  *  Inits the thermal sensor thresholds according to the NVM map
1719  *  and save off the threshold and location values into mac.thermal_sensor_data
1720  **/
1721 s32 ngbe_init_thermal_sensor_thresh(struct ngbe_hw *hw)
1722 {
1723         struct ngbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
1724
1725         DEBUGFUNC("ngbe_init_thermal_sensor_thresh");
1726
1727         memset(data, 0, sizeof(struct ngbe_thermal_sensor_data));
1728
1729         if (hw->bus.lan_id != 0)
1730                 return NGBE_NOT_IMPLEMENTED;
1731
1732         wr32(hw, NGBE_TSINTR,
1733                 NGBE_TSINTR_AEN | NGBE_TSINTR_DEN);
1734         wr32(hw, NGBE_TSEN, NGBE_TSEN_ENA);
1735
1736
1737         data->sensor[0].alarm_thresh = 115;
1738         wr32(hw, NGBE_TSATHRE, 0x344);
1739         data->sensor[0].dalarm_thresh = 110;
1740         wr32(hw, NGBE_TSDTHRE, 0x330);
1741
1742         return 0;
1743 }
1744
1745 s32 ngbe_mac_check_overtemp(struct ngbe_hw *hw)
1746 {
1747         s32 status = 0;
1748         u32 ts_state;
1749
1750         DEBUGFUNC("ngbe_mac_check_overtemp");
1751
1752         /* Check that the LASI temp alarm status was triggered */
1753         ts_state = rd32(hw, NGBE_TSALM);
1754
1755         if (ts_state & NGBE_TSALM_HI)
1756                 status = NGBE_ERR_UNDERTEMP;
1757         else if (ts_state & NGBE_TSALM_LO)
1758                 status = NGBE_ERR_OVERTEMP;
1759
1760         return status;
1761 }
1762
1763 void ngbe_disable_rx(struct ngbe_hw *hw)
1764 {
1765         u32 pfdtxgswc;
1766
1767         pfdtxgswc = rd32(hw, NGBE_PSRCTL);
1768         if (pfdtxgswc & NGBE_PSRCTL_LBENA) {
1769                 pfdtxgswc &= ~NGBE_PSRCTL_LBENA;
1770                 wr32(hw, NGBE_PSRCTL, pfdtxgswc);
1771                 hw->mac.set_lben = true;
1772         } else {
1773                 hw->mac.set_lben = false;
1774         }
1775
1776         wr32m(hw, NGBE_PBRXCTL, NGBE_PBRXCTL_ENA, 0);
1777         wr32m(hw, NGBE_MACRXCFG, NGBE_MACRXCFG_ENA, 0);
1778 }
1779
1780 void ngbe_enable_rx(struct ngbe_hw *hw)
1781 {
1782         u32 pfdtxgswc;
1783
1784         wr32m(hw, NGBE_MACRXCFG, NGBE_MACRXCFG_ENA, NGBE_MACRXCFG_ENA);
1785         wr32m(hw, NGBE_PBRXCTL, NGBE_PBRXCTL_ENA, NGBE_PBRXCTL_ENA);
1786
1787         if (hw->mac.set_lben) {
1788                 pfdtxgswc = rd32(hw, NGBE_PSRCTL);
1789                 pfdtxgswc |= NGBE_PSRCTL_LBENA;
1790                 wr32(hw, NGBE_PSRCTL, pfdtxgswc);
1791                 hw->mac.set_lben = false;
1792         }
1793 }
1794
1795 /**
1796  *  ngbe_set_mac_type - Sets MAC type
1797  *  @hw: pointer to the HW structure
1798  *
1799  *  This function sets the mac type of the adapter based on the
1800  *  vendor ID and device ID stored in the hw structure.
1801  **/
1802 s32 ngbe_set_mac_type(struct ngbe_hw *hw)
1803 {
1804         s32 err = 0;
1805
1806         DEBUGFUNC("ngbe_set_mac_type");
1807
1808         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
1809                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
1810                 return NGBE_ERR_DEVICE_NOT_SUPPORTED;
1811         }
1812
1813         switch (hw->sub_device_id) {
1814         case NGBE_SUB_DEV_ID_EM_RTL_SGMII:
1815         case NGBE_SUB_DEV_ID_EM_MVL_RGMII:
1816                 hw->phy.media_type = ngbe_media_type_copper;
1817                 hw->mac.type = ngbe_mac_em;
1818                 break;
1819         case NGBE_SUB_DEV_ID_EM_MVL_SFP:
1820         case NGBE_SUB_DEV_ID_EM_YT8521S_SFP:
1821                 hw->phy.media_type = ngbe_media_type_fiber;
1822                 hw->mac.type = ngbe_mac_em;
1823                 break;
1824         case NGBE_SUB_DEV_ID_EM_VF:
1825                 hw->phy.media_type = ngbe_media_type_virtual;
1826                 hw->mac.type = ngbe_mac_em_vf;
1827                 break;
1828         default:
1829                 err = NGBE_ERR_DEVICE_NOT_SUPPORTED;
1830                 hw->phy.media_type = ngbe_media_type_unknown;
1831                 hw->mac.type = ngbe_mac_unknown;
1832                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
1833                 break;
1834         }
1835
1836         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
1837                   hw->mac.type, hw->phy.media_type, err);
1838         return err;
1839 }
1840
1841 /**
1842  *  ngbe_enable_rx_dma - Enable the Rx DMA unit
1843  *  @hw: pointer to hardware structure
1844  *  @regval: register value to write to RXCTRL
1845  *
1846  *  Enables the Rx DMA unit
1847  **/
1848 s32 ngbe_enable_rx_dma(struct ngbe_hw *hw, u32 regval)
1849 {
1850         DEBUGFUNC("ngbe_enable_rx_dma");
1851
1852         /*
1853          * Workaround silicon errata when enabling the Rx datapath.
1854          * If traffic is incoming before we enable the Rx unit, it could hang
1855          * the Rx DMA unit.  Therefore, make sure the security engine is
1856          * completely disabled prior to enabling the Rx unit.
1857          */
1858
1859         hw->mac.disable_sec_rx_path(hw);
1860
1861         if (regval & NGBE_PBRXCTL_ENA)
1862                 ngbe_enable_rx(hw);
1863         else
1864                 ngbe_disable_rx(hw);
1865
1866         hw->mac.enable_sec_rx_path(hw);
1867
1868         return 0;
1869 }
1870
1871 void ngbe_map_device_id(struct ngbe_hw *hw)
1872 {
1873         u16 oem = hw->sub_system_id & NGBE_OEM_MASK;
1874         u16 internal = hw->sub_system_id & NGBE_INTERNAL_MASK;
1875         hw->is_pf = true;
1876
1877         /* move subsystem_device_id to device_id */
1878         switch (hw->device_id) {
1879         case NGBE_DEV_ID_EM_WX1860AL_W_VF:
1880         case NGBE_DEV_ID_EM_WX1860A2_VF:
1881         case NGBE_DEV_ID_EM_WX1860A2S_VF:
1882         case NGBE_DEV_ID_EM_WX1860A4_VF:
1883         case NGBE_DEV_ID_EM_WX1860A4S_VF:
1884         case NGBE_DEV_ID_EM_WX1860AL2_VF:
1885         case NGBE_DEV_ID_EM_WX1860AL2S_VF:
1886         case NGBE_DEV_ID_EM_WX1860AL4_VF:
1887         case NGBE_DEV_ID_EM_WX1860AL4S_VF:
1888         case NGBE_DEV_ID_EM_WX1860NCSI_VF:
1889         case NGBE_DEV_ID_EM_WX1860A1_VF:
1890         case NGBE_DEV_ID_EM_WX1860A1L_VF:
1891                 hw->device_id = NGBE_DEV_ID_EM_VF;
1892                 hw->sub_device_id = NGBE_SUB_DEV_ID_EM_VF;
1893                 hw->is_pf = false;
1894                 break;
1895         case NGBE_DEV_ID_EM_WX1860AL_W:
1896         case NGBE_DEV_ID_EM_WX1860A2:
1897         case NGBE_DEV_ID_EM_WX1860A2S:
1898         case NGBE_DEV_ID_EM_WX1860A4:
1899         case NGBE_DEV_ID_EM_WX1860A4S:
1900         case NGBE_DEV_ID_EM_WX1860AL2:
1901         case NGBE_DEV_ID_EM_WX1860AL2S:
1902         case NGBE_DEV_ID_EM_WX1860AL4:
1903         case NGBE_DEV_ID_EM_WX1860AL4S:
1904         case NGBE_DEV_ID_EM_WX1860NCSI:
1905         case NGBE_DEV_ID_EM_WX1860A1:
1906         case NGBE_DEV_ID_EM_WX1860A1L:
1907                 hw->device_id = NGBE_DEV_ID_EM;
1908                 if (oem == NGBE_LY_M88E1512_SFP ||
1909                                 internal == NGBE_INTERNAL_SFP)
1910                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_MVL_SFP;
1911                 else if (hw->sub_system_id == NGBE_SUB_DEV_ID_EM_M88E1512_RJ45)
1912                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_MVL_RGMII;
1913                 else if (oem == NGBE_YT8521S_SFP ||
1914                                 oem == NGBE_LY_YT8521S_SFP)
1915                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_YT8521S_SFP;
1916                 else
1917                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_RTL_SGMII;
1918                 break;
1919         default:
1920                 break;
1921         }
1922 }
1923
1924 /**
1925  *  ngbe_init_ops_pf - Inits func ptrs and MAC type
1926  *  @hw: pointer to hardware structure
1927  *
1928  *  Initialize the function pointers and assign the MAC type.
1929  *  Does not touch the hardware.
1930  **/
1931 s32 ngbe_init_ops_pf(struct ngbe_hw *hw)
1932 {
1933         struct ngbe_bus_info *bus = &hw->bus;
1934         struct ngbe_mac_info *mac = &hw->mac;
1935         struct ngbe_phy_info *phy = &hw->phy;
1936         struct ngbe_rom_info *rom = &hw->rom;
1937         struct ngbe_mbx_info *mbx = &hw->mbx;
1938
1939         DEBUGFUNC("ngbe_init_ops_pf");
1940
1941         /* BUS */
1942         bus->set_lan_id = ngbe_set_lan_id_multi_port;
1943
1944         /* PHY */
1945         phy->identify = ngbe_identify_phy;
1946         phy->read_reg = ngbe_read_phy_reg;
1947         phy->write_reg = ngbe_write_phy_reg;
1948         phy->read_reg_unlocked = ngbe_read_phy_reg_mdi;
1949         phy->write_reg_unlocked = ngbe_write_phy_reg_mdi;
1950         phy->reset_hw = ngbe_reset_phy;
1951
1952         /* MAC */
1953         mac->init_hw = ngbe_init_hw;
1954         mac->reset_hw = ngbe_reset_hw_em;
1955         mac->start_hw = ngbe_start_hw;
1956         mac->clear_hw_cntrs = ngbe_clear_hw_cntrs;
1957         mac->enable_rx_dma = ngbe_enable_rx_dma;
1958         mac->get_mac_addr = ngbe_get_mac_addr;
1959         mac->stop_hw = ngbe_stop_hw;
1960         mac->acquire_swfw_sync = ngbe_acquire_swfw_sync;
1961         mac->release_swfw_sync = ngbe_release_swfw_sync;
1962
1963         mac->disable_sec_rx_path = ngbe_disable_sec_rx_path;
1964         mac->enable_sec_rx_path = ngbe_enable_sec_rx_path;
1965
1966         /* LEDs */
1967         mac->led_on = ngbe_led_on;
1968         mac->led_off = ngbe_led_off;
1969
1970         /* RAR, VLAN, Multicast */
1971         mac->set_rar = ngbe_set_rar;
1972         mac->clear_rar = ngbe_clear_rar;
1973         mac->init_rx_addrs = ngbe_init_rx_addrs;
1974         mac->update_mc_addr_list = ngbe_update_mc_addr_list;
1975         mac->set_vmdq = ngbe_set_vmdq;
1976         mac->clear_vmdq = ngbe_clear_vmdq;
1977         mac->set_vfta = ngbe_set_vfta;
1978         mac->set_vlvf = ngbe_set_vlvf;
1979         mac->clear_vfta = ngbe_clear_vfta;
1980         mac->set_mac_anti_spoofing = ngbe_set_mac_anti_spoofing;
1981         mac->set_vlan_anti_spoofing = ngbe_set_vlan_anti_spoofing;
1982
1983         /* Flow Control */
1984         mac->fc_enable = ngbe_fc_enable;
1985         mac->fc_autoneg = ngbe_fc_autoneg;
1986         mac->setup_fc = ngbe_setup_fc_em;
1987
1988         /* Link */
1989         mac->get_link_capabilities = ngbe_get_link_capabilities_em;
1990         mac->check_link = ngbe_check_mac_link_em;
1991         mac->setup_link = ngbe_setup_mac_link_em;
1992
1993         mac->setup_pba = ngbe_set_pba;
1994
1995         /* Manageability interface */
1996         mac->init_thermal_sensor_thresh = ngbe_init_thermal_sensor_thresh;
1997         mac->check_overtemp = ngbe_mac_check_overtemp;
1998
1999         mbx->init_params = ngbe_init_mbx_params_pf;
2000         mbx->read = ngbe_read_mbx_pf;
2001         mbx->write = ngbe_write_mbx_pf;
2002         mbx->check_for_msg = ngbe_check_for_msg_pf;
2003         mbx->check_for_ack = ngbe_check_for_ack_pf;
2004         mbx->check_for_rst = ngbe_check_for_rst_pf;
2005
2006         /* EEPROM */
2007         rom->init_params = ngbe_init_eeprom_params;
2008         rom->readw_buffer = ngbe_ee_readw_buffer;
2009         rom->read32 = ngbe_ee_read32;
2010         rom->writew_buffer = ngbe_ee_writew_buffer;
2011         rom->validate_checksum = ngbe_validate_eeprom_checksum_em;
2012
2013         mac->mcft_size          = NGBE_EM_MC_TBL_SIZE;
2014         mac->vft_size           = NGBE_EM_VFT_TBL_SIZE;
2015         mac->num_rar_entries    = NGBE_EM_RAR_ENTRIES;
2016         mac->rx_pb_size         = NGBE_EM_RX_PB_SIZE;
2017         mac->max_rx_queues      = NGBE_EM_MAX_RX_QUEUES;
2018         mac->max_tx_queues      = NGBE_EM_MAX_TX_QUEUES;
2019
2020         mac->default_speeds = NGBE_LINK_SPEED_10M_FULL |
2021                                 NGBE_LINK_SPEED_100M_FULL |
2022                                 NGBE_LINK_SPEED_1GB_FULL;
2023
2024         return 0;
2025 }
2026
2027 /**
2028  *  ngbe_init_shared_code - Initialize the shared code
2029  *  @hw: pointer to hardware structure
2030  *
2031  *  This will assign function pointers and assign the MAC type and PHY code.
2032  *  Does not touch the hardware. This function must be called prior to any
2033  *  other function in the shared code. The ngbe_hw structure should be
2034  *  memset to 0 prior to calling this function.  The following fields in
2035  *  hw structure should be filled in prior to calling this function:
2036  *  hw_addr, back, device_id, vendor_id, subsystem_device_id
2037  **/
2038 s32 ngbe_init_shared_code(struct ngbe_hw *hw)
2039 {
2040         s32 status = 0;
2041
2042         DEBUGFUNC("ngbe_init_shared_code");
2043
2044         /*
2045          * Set the mac type
2046          */
2047         ngbe_set_mac_type(hw);
2048
2049         ngbe_init_ops_dummy(hw);
2050         switch (hw->mac.type) {
2051         case ngbe_mac_em:
2052                 ngbe_init_ops_pf(hw);
2053                 break;
2054         default:
2055                 status = NGBE_ERR_DEVICE_NOT_SUPPORTED;
2056                 break;
2057         }
2058         hw->mac.max_link_up_time = NGBE_LINK_UP_TIME;
2059
2060         hw->bus.set_lan_id(hw);
2061
2062         return status;
2063 }
2064