net/ngbe: support flow control
[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         u32 reg_val;
354         u16 i;
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         /* Disable the transmit unit.  Each queue must be disabled. */
376         for (i = 0; i < hw->mac.max_tx_queues; i++)
377                 wr32(hw, NGBE_TXCFG(i), NGBE_TXCFG_FLUSH);
378
379         /* Disable the receive unit by stopping each queue */
380         for (i = 0; i < hw->mac.max_rx_queues; i++) {
381                 reg_val = rd32(hw, NGBE_RXCFG(i));
382                 reg_val &= ~NGBE_RXCFG_ENA;
383                 wr32(hw, NGBE_RXCFG(i), reg_val);
384         }
385
386         /* flush all queues disables */
387         ngbe_flush(hw);
388         msec_delay(2);
389
390         return 0;
391 }
392
393 /**
394  *  ngbe_validate_mac_addr - Validate MAC address
395  *  @mac_addr: pointer to MAC address.
396  *
397  *  Tests a MAC address to ensure it is a valid Individual Address.
398  **/
399 s32 ngbe_validate_mac_addr(u8 *mac_addr)
400 {
401         s32 status = 0;
402
403         DEBUGFUNC("ngbe_validate_mac_addr");
404
405         /* Make sure it is not a multicast address */
406         if (NGBE_IS_MULTICAST((struct rte_ether_addr *)mac_addr)) {
407                 status = NGBE_ERR_INVALID_MAC_ADDR;
408         /* Not a broadcast address */
409         } else if (NGBE_IS_BROADCAST((struct rte_ether_addr *)mac_addr)) {
410                 status = NGBE_ERR_INVALID_MAC_ADDR;
411         /* Reject the zero address */
412         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
413                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
414                 status = NGBE_ERR_INVALID_MAC_ADDR;
415         }
416         return status;
417 }
418
419 /**
420  *  ngbe_set_rar - Set Rx address register
421  *  @hw: pointer to hardware structure
422  *  @index: Receive address register to write
423  *  @addr: Address to put into receive address register
424  *  @vmdq: VMDq "set" or "pool" index
425  *  @enable_addr: set flag that address is active
426  *
427  *  Puts an ethernet address into a receive address register.
428  **/
429 s32 ngbe_set_rar(struct ngbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
430                           u32 enable_addr)
431 {
432         u32 rar_low, rar_high;
433         u32 rar_entries = hw->mac.num_rar_entries;
434
435         DEBUGFUNC("ngbe_set_rar");
436
437         /* Make sure we are using a valid rar index range */
438         if (index >= rar_entries) {
439                 DEBUGOUT("RAR index %d is out of range.\n", index);
440                 return NGBE_ERR_INVALID_ARGUMENT;
441         }
442
443         /* setup VMDq pool selection before this RAR gets enabled */
444         hw->mac.set_vmdq(hw, index, vmdq);
445
446         /*
447          * HW expects these in little endian so we reverse the byte
448          * order from network order (big endian) to little endian
449          */
450         rar_low = NGBE_ETHADDRL_AD0(addr[5]) |
451                   NGBE_ETHADDRL_AD1(addr[4]) |
452                   NGBE_ETHADDRL_AD2(addr[3]) |
453                   NGBE_ETHADDRL_AD3(addr[2]);
454         /*
455          * Some parts put the VMDq setting in the extra RAH bits,
456          * so save everything except the lower 16 bits that hold part
457          * of the address and the address valid bit.
458          */
459         rar_high = rd32(hw, NGBE_ETHADDRH);
460         rar_high &= ~NGBE_ETHADDRH_AD_MASK;
461         rar_high |= (NGBE_ETHADDRH_AD4(addr[1]) |
462                      NGBE_ETHADDRH_AD5(addr[0]));
463
464         rar_high &= ~NGBE_ETHADDRH_VLD;
465         if (enable_addr != 0)
466                 rar_high |= NGBE_ETHADDRH_VLD;
467
468         wr32(hw, NGBE_ETHADDRIDX, index);
469         wr32(hw, NGBE_ETHADDRL, rar_low);
470         wr32(hw, NGBE_ETHADDRH, rar_high);
471
472         return 0;
473 }
474
475 /**
476  *  ngbe_clear_rar - Remove Rx address register
477  *  @hw: pointer to hardware structure
478  *  @index: Receive address register to write
479  *
480  *  Clears an ethernet address from a receive address register.
481  **/
482 s32 ngbe_clear_rar(struct ngbe_hw *hw, u32 index)
483 {
484         u32 rar_high;
485         u32 rar_entries = hw->mac.num_rar_entries;
486
487         DEBUGFUNC("ngbe_clear_rar");
488
489         /* Make sure we are using a valid rar index range */
490         if (index >= rar_entries) {
491                 DEBUGOUT("RAR index %d is out of range.\n", index);
492                 return NGBE_ERR_INVALID_ARGUMENT;
493         }
494
495         /*
496          * Some parts put the VMDq setting in the extra RAH bits,
497          * so save everything except the lower 16 bits that hold part
498          * of the address and the address valid bit.
499          */
500         wr32(hw, NGBE_ETHADDRIDX, index);
501         rar_high = rd32(hw, NGBE_ETHADDRH);
502         rar_high &= ~(NGBE_ETHADDRH_AD_MASK | NGBE_ETHADDRH_VLD);
503
504         wr32(hw, NGBE_ETHADDRL, 0);
505         wr32(hw, NGBE_ETHADDRH, rar_high);
506
507         /* clear VMDq pool/queue selection for this RAR */
508         hw->mac.clear_vmdq(hw, index, BIT_MASK32);
509
510         return 0;
511 }
512
513 /**
514  *  ngbe_init_rx_addrs - Initializes receive address filters.
515  *  @hw: pointer to hardware structure
516  *
517  *  Places the MAC address in receive address register 0 and clears the rest
518  *  of the receive address registers. Clears the multicast table. Assumes
519  *  the receiver is in reset when the routine is called.
520  **/
521 s32 ngbe_init_rx_addrs(struct ngbe_hw *hw)
522 {
523         u32 i;
524         u32 psrctl;
525         u32 rar_entries = hw->mac.num_rar_entries;
526
527         DEBUGFUNC("ngbe_init_rx_addrs");
528
529         /*
530          * If the current mac address is valid, assume it is a software override
531          * to the permanent address.
532          * Otherwise, use the permanent address from the eeprom.
533          */
534         if (ngbe_validate_mac_addr(hw->mac.addr) ==
535             NGBE_ERR_INVALID_MAC_ADDR) {
536                 /* Get the MAC address from the RAR0 for later reference */
537                 hw->mac.get_mac_addr(hw, hw->mac.addr);
538
539                 DEBUGOUT(" Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
540                           hw->mac.addr[0], hw->mac.addr[1],
541                           hw->mac.addr[2]);
542                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
543                           hw->mac.addr[4], hw->mac.addr[5]);
544         } else {
545                 /* Setup the receive address. */
546                 DEBUGOUT("Overriding MAC Address in RAR[0]\n");
547                 DEBUGOUT(" New MAC Addr =%.2X %.2X %.2X ",
548                           hw->mac.addr[0], hw->mac.addr[1],
549                           hw->mac.addr[2]);
550                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
551                           hw->mac.addr[4], hw->mac.addr[5]);
552
553                 hw->mac.set_rar(hw, 0, hw->mac.addr, 0, true);
554         }
555
556         /* clear VMDq pool/queue selection for RAR 0 */
557         hw->mac.clear_vmdq(hw, 0, BIT_MASK32);
558
559         /* Zero out the other receive addresses. */
560         DEBUGOUT("Clearing RAR[1-%d]\n", rar_entries - 1);
561         for (i = 1; i < rar_entries; i++) {
562                 wr32(hw, NGBE_ETHADDRIDX, i);
563                 wr32(hw, NGBE_ETHADDRL, 0);
564                 wr32(hw, NGBE_ETHADDRH, 0);
565         }
566
567         /* Clear the MTA */
568         hw->addr_ctrl.mta_in_use = 0;
569         psrctl = rd32(hw, NGBE_PSRCTL);
570         psrctl &= ~(NGBE_PSRCTL_ADHF12_MASK | NGBE_PSRCTL_MCHFENA);
571         psrctl |= NGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
572         wr32(hw, NGBE_PSRCTL, psrctl);
573
574         DEBUGOUT(" Clearing MTA\n");
575         for (i = 0; i < hw->mac.mcft_size; i++)
576                 wr32(hw, NGBE_MCADDRTBL(i), 0);
577
578         ngbe_init_uta_tables(hw);
579
580         return 0;
581 }
582
583 /**
584  *  ngbe_mta_vector - Determines bit-vector in multicast table to set
585  *  @hw: pointer to hardware structure
586  *  @mc_addr: the multicast address
587  *
588  *  Extracts the 12 bits, from a multicast address, to determine which
589  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
590  *  incoming rx multicast addresses, to determine the bit-vector to check in
591  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
592  *  by the MO field of the PSRCTRL. The MO field is set during initialization
593  *  to mc_filter_type.
594  **/
595 static s32 ngbe_mta_vector(struct ngbe_hw *hw, u8 *mc_addr)
596 {
597         u32 vector = 0;
598
599         DEBUGFUNC("ngbe_mta_vector");
600
601         switch (hw->mac.mc_filter_type) {
602         case 0:   /* use bits [47:36] of the address */
603                 vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
604                 break;
605         case 1:   /* use bits [46:35] of the address */
606                 vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
607                 break;
608         case 2:   /* use bits [45:34] of the address */
609                 vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
610                 break;
611         case 3:   /* use bits [43:32] of the address */
612                 vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
613                 break;
614         default:  /* Invalid mc_filter_type */
615                 DEBUGOUT("MC filter type param set incorrectly\n");
616                 ASSERT(0);
617                 break;
618         }
619
620         /* vector can only be 12-bits or boundary will be exceeded */
621         vector &= 0xFFF;
622         return vector;
623 }
624
625 /**
626  *  ngbe_set_mta - Set bit-vector in multicast table
627  *  @hw: pointer to hardware structure
628  *  @mc_addr: Multicast address
629  *
630  *  Sets the bit-vector in the multicast table.
631  **/
632 void ngbe_set_mta(struct ngbe_hw *hw, u8 *mc_addr)
633 {
634         u32 vector;
635         u32 vector_bit;
636         u32 vector_reg;
637
638         DEBUGFUNC("ngbe_set_mta");
639
640         hw->addr_ctrl.mta_in_use++;
641
642         vector = ngbe_mta_vector(hw, mc_addr);
643         DEBUGOUT(" bit-vector = 0x%03X\n", vector);
644
645         /*
646          * The MTA is a register array of 128 32-bit registers. It is treated
647          * like an array of 4096 bits.  We want to set bit
648          * BitArray[vector_value]. So we figure out what register the bit is
649          * in, read it, OR in the new bit, then write back the new value.  The
650          * register is determined by the upper 7 bits of the vector value and
651          * the bit within that register are determined by the lower 5 bits of
652          * the value.
653          */
654         vector_reg = (vector >> 5) & 0x7F;
655         vector_bit = vector & 0x1F;
656         hw->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
657 }
658
659 /**
660  *  ngbe_update_mc_addr_list - Updates MAC list of multicast addresses
661  *  @hw: pointer to hardware structure
662  *  @mc_addr_list: the list of new multicast addresses
663  *  @mc_addr_count: number of addresses
664  *  @next: iterator function to walk the multicast address list
665  *  @clear: flag, when set clears the table beforehand
666  *
667  *  When the clear flag is set, the given list replaces any existing list.
668  *  Hashes the given addresses into the multicast table.
669  **/
670 s32 ngbe_update_mc_addr_list(struct ngbe_hw *hw, u8 *mc_addr_list,
671                                       u32 mc_addr_count, ngbe_mc_addr_itr next,
672                                       bool clear)
673 {
674         u32 i;
675         u32 vmdq;
676
677         DEBUGFUNC("ngbe_update_mc_addr_list");
678
679         /*
680          * Set the new number of MC addresses that we are being requested to
681          * use.
682          */
683         hw->addr_ctrl.num_mc_addrs = mc_addr_count;
684         hw->addr_ctrl.mta_in_use = 0;
685
686         /* Clear mta_shadow */
687         if (clear) {
688                 DEBUGOUT(" Clearing MTA\n");
689                 memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
690         }
691
692         /* Update mta_shadow */
693         for (i = 0; i < mc_addr_count; i++) {
694                 DEBUGOUT(" Adding the multicast addresses:\n");
695                 ngbe_set_mta(hw, next(hw, &mc_addr_list, &vmdq));
696         }
697
698         /* Enable mta */
699         for (i = 0; i < hw->mac.mcft_size; i++)
700                 wr32a(hw, NGBE_MCADDRTBL(0), i,
701                                       hw->mac.mta_shadow[i]);
702
703         if (hw->addr_ctrl.mta_in_use > 0) {
704                 u32 psrctl = rd32(hw, NGBE_PSRCTL);
705                 psrctl &= ~(NGBE_PSRCTL_ADHF12_MASK | NGBE_PSRCTL_MCHFENA);
706                 psrctl |= NGBE_PSRCTL_MCHFENA |
707                          NGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
708                 wr32(hw, NGBE_PSRCTL, psrctl);
709         }
710
711         DEBUGOUT("ngbe update mc addr list complete\n");
712         return 0;
713 }
714
715 /**
716  *  ngbe_setup_fc_em - Set up flow control
717  *  @hw: pointer to hardware structure
718  *
719  *  Called at init time to set up flow control.
720  **/
721 s32 ngbe_setup_fc_em(struct ngbe_hw *hw)
722 {
723         s32 err = 0;
724         u16 reg_cu = 0;
725
726         DEBUGFUNC("ngbe_setup_fc");
727
728         /* Validate the requested mode */
729         if (hw->fc.strict_ieee && hw->fc.requested_mode == ngbe_fc_rx_pause) {
730                 DEBUGOUT("ngbe_fc_rx_pause not valid in strict IEEE mode\n");
731                 err = NGBE_ERR_INVALID_LINK_SETTINGS;
732                 goto out;
733         }
734
735         /*
736          * 1gig parts do not have a word in the EEPROM to determine the
737          * default flow control setting, so we explicitly set it to full.
738          */
739         if (hw->fc.requested_mode == ngbe_fc_default)
740                 hw->fc.requested_mode = ngbe_fc_full;
741
742         /*
743          * The possible values of fc.requested_mode are:
744          * 0: Flow control is completely disabled
745          * 1: Rx flow control is enabled (we can receive pause frames,
746          *    but not send pause frames).
747          * 2: Tx flow control is enabled (we can send pause frames but
748          *    we do not support receiving pause frames).
749          * 3: Both Rx and Tx flow control (symmetric) are enabled.
750          * other: Invalid.
751          */
752         switch (hw->fc.requested_mode) {
753         case ngbe_fc_none:
754                 /* Flow control completely disabled by software override. */
755                 break;
756         case ngbe_fc_tx_pause:
757                 /*
758                  * Tx Flow control is enabled, and Rx Flow control is
759                  * disabled by software override.
760                  */
761                 if (hw->phy.type == ngbe_phy_mvl_sfi ||
762                         hw->phy.type == ngbe_phy_yt8521s_sfi)
763                         reg_cu |= MVL_FANA_ASM_PAUSE;
764                 else
765                         reg_cu |= 0x800; /*need to merge rtl and mvl on page 0*/
766                 break;
767         case ngbe_fc_rx_pause:
768                 /*
769                  * Rx Flow control is enabled and Tx Flow control is
770                  * disabled by software override. Since there really
771                  * isn't a way to advertise that we are capable of RX
772                  * Pause ONLY, we will advertise that we support both
773                  * symmetric and asymmetric Rx PAUSE, as such we fall
774                  * through to the fc_full statement.  Later, we will
775                  * disable the adapter's ability to send PAUSE frames.
776                  */
777         case ngbe_fc_full:
778                 /* Flow control (both Rx and Tx) is enabled by SW override. */
779                 if (hw->phy.type == ngbe_phy_mvl_sfi ||
780                         hw->phy.type == ngbe_phy_yt8521s_sfi)
781                         reg_cu |= MVL_FANA_SYM_PAUSE;
782                 else
783                         reg_cu |= 0xC00; /*need to merge rtl and mvl on page 0*/
784                 break;
785         default:
786                 DEBUGOUT("Flow control param set incorrectly\n");
787                 err = NGBE_ERR_CONFIG;
788                 goto out;
789         }
790
791         err = hw->phy.set_pause_adv(hw, reg_cu);
792
793 out:
794         return err;
795 }
796
797 /**
798  *  ngbe_fc_enable - Enable flow control
799  *  @hw: pointer to hardware structure
800  *
801  *  Enable flow control according to the current settings.
802  **/
803 s32 ngbe_fc_enable(struct ngbe_hw *hw)
804 {
805         s32 err = 0;
806         u32 mflcn_reg, fccfg_reg;
807         u32 pause_time;
808         u32 fcrtl, fcrth;
809
810         DEBUGFUNC("ngbe_fc_enable");
811
812         /* Validate the water mark configuration */
813         if (!hw->fc.pause_time) {
814                 err = NGBE_ERR_INVALID_LINK_SETTINGS;
815                 goto out;
816         }
817
818         /* Low water mark of zero causes XOFF floods */
819         if ((hw->fc.current_mode & ngbe_fc_tx_pause) && hw->fc.high_water) {
820                 if (!hw->fc.low_water ||
821                         hw->fc.low_water >= hw->fc.high_water) {
822                         DEBUGOUT("Invalid water mark configuration\n");
823                         err = NGBE_ERR_INVALID_LINK_SETTINGS;
824                         goto out;
825                 }
826         }
827
828         /* Negotiate the fc mode to use */
829         hw->mac.fc_autoneg(hw);
830
831         /* Disable any previous flow control settings */
832         mflcn_reg = rd32(hw, NGBE_RXFCCFG);
833         mflcn_reg &= ~NGBE_RXFCCFG_FC;
834
835         fccfg_reg = rd32(hw, NGBE_TXFCCFG);
836         fccfg_reg &= ~NGBE_TXFCCFG_FC;
837         /*
838          * The possible values of fc.current_mode are:
839          * 0: Flow control is completely disabled
840          * 1: Rx flow control is enabled (we can receive pause frames,
841          *    but not send pause frames).
842          * 2: Tx flow control is enabled (we can send pause frames but
843          *    we do not support receiving pause frames).
844          * 3: Both Rx and Tx flow control (symmetric) are enabled.
845          * other: Invalid.
846          */
847         switch (hw->fc.current_mode) {
848         case ngbe_fc_none:
849                 /*
850                  * Flow control is disabled by software override or autoneg.
851                  * The code below will actually disable it in the HW.
852                  */
853                 break;
854         case ngbe_fc_rx_pause:
855                 /*
856                  * Rx Flow control is enabled and Tx Flow control is
857                  * disabled by software override. Since there really
858                  * isn't a way to advertise that we are capable of RX
859                  * Pause ONLY, we will advertise that we support both
860                  * symmetric and asymmetric Rx PAUSE.  Later, we will
861                  * disable the adapter's ability to send PAUSE frames.
862                  */
863                 mflcn_reg |= NGBE_RXFCCFG_FC;
864                 break;
865         case ngbe_fc_tx_pause:
866                 /*
867                  * Tx Flow control is enabled, and Rx Flow control is
868                  * disabled by software override.
869                  */
870                 fccfg_reg |= NGBE_TXFCCFG_FC;
871                 break;
872         case ngbe_fc_full:
873                 /* Flow control (both Rx and Tx) is enabled by SW override. */
874                 mflcn_reg |= NGBE_RXFCCFG_FC;
875                 fccfg_reg |= NGBE_TXFCCFG_FC;
876                 break;
877         default:
878                 DEBUGOUT("Flow control param set incorrectly\n");
879                 err = NGBE_ERR_CONFIG;
880                 goto out;
881         }
882
883         /* Set 802.3x based flow control settings. */
884         wr32(hw, NGBE_RXFCCFG, mflcn_reg);
885         wr32(hw, NGBE_TXFCCFG, fccfg_reg);
886
887         /* Set up and enable Rx high/low water mark thresholds, enable XON. */
888         if ((hw->fc.current_mode & ngbe_fc_tx_pause) &&
889                 hw->fc.high_water) {
890                 fcrtl = NGBE_FCWTRLO_TH(hw->fc.low_water) |
891                         NGBE_FCWTRLO_XON;
892                 fcrth = NGBE_FCWTRHI_TH(hw->fc.high_water) |
893                         NGBE_FCWTRHI_XOFF;
894         } else {
895                 /*
896                  * In order to prevent Tx hangs when the internal Tx
897                  * switch is enabled we must set the high water mark
898                  * to the Rx packet buffer size - 24KB.  This allows
899                  * the Tx switch to function even under heavy Rx
900                  * workloads.
901                  */
902                 fcrtl = 0;
903                 fcrth = rd32(hw, NGBE_PBRXSIZE) - 24576;
904         }
905         wr32(hw, NGBE_FCWTRLO, fcrtl);
906         wr32(hw, NGBE_FCWTRHI, fcrth);
907
908         /* Configure pause time */
909         pause_time = NGBE_RXFCFSH_TIME(hw->fc.pause_time);
910         wr32(hw, NGBE_FCXOFFTM, pause_time * 0x00010000);
911
912         /* Configure flow control refresh threshold value */
913         wr32(hw, NGBE_RXFCRFSH, hw->fc.pause_time / 2);
914
915 out:
916         return err;
917 }
918
919 /**
920  *  ngbe_negotiate_fc - Negotiate flow control
921  *  @hw: pointer to hardware structure
922  *  @adv_reg: flow control advertised settings
923  *  @lp_reg: link partner's flow control settings
924  *  @adv_sym: symmetric pause bit in advertisement
925  *  @adv_asm: asymmetric pause bit in advertisement
926  *  @lp_sym: symmetric pause bit in link partner advertisement
927  *  @lp_asm: asymmetric pause bit in link partner advertisement
928  *
929  *  Find the intersection between advertised settings and link partner's
930  *  advertised settings
931  **/
932 s32 ngbe_negotiate_fc(struct ngbe_hw *hw, u32 adv_reg, u32 lp_reg,
933                        u32 adv_sym, u32 adv_asm, u32 lp_sym, u32 lp_asm)
934 {
935         if ((!(adv_reg)) ||  (!(lp_reg))) {
936                 DEBUGOUT("Local or link partner's advertised flow control "
937                          "settings are NULL. Local: %x, link partner: %x\n",
938                               adv_reg, lp_reg);
939                 return NGBE_ERR_FC_NOT_NEGOTIATED;
940         }
941
942         if ((adv_reg & adv_sym) && (lp_reg & lp_sym)) {
943                 /*
944                  * Now we need to check if the user selected Rx ONLY
945                  * of pause frames.  In this case, we had to advertise
946                  * FULL flow control because we could not advertise RX
947                  * ONLY. Hence, we must now check to see if we need to
948                  * turn OFF the TRANSMISSION of PAUSE frames.
949                  */
950                 if (hw->fc.requested_mode == ngbe_fc_full) {
951                         hw->fc.current_mode = ngbe_fc_full;
952                         DEBUGOUT("Flow Control = FULL.\n");
953                 } else {
954                         hw->fc.current_mode = ngbe_fc_rx_pause;
955                         DEBUGOUT("Flow Control=RX PAUSE frames only\n");
956                 }
957         } else if (!(adv_reg & adv_sym) && (adv_reg & adv_asm) &&
958                    (lp_reg & lp_sym) && (lp_reg & lp_asm)) {
959                 hw->fc.current_mode = ngbe_fc_tx_pause;
960                 DEBUGOUT("Flow Control = TX PAUSE frames only.\n");
961         } else if ((adv_reg & adv_sym) && (adv_reg & adv_asm) &&
962                    !(lp_reg & lp_sym) && (lp_reg & lp_asm)) {
963                 hw->fc.current_mode = ngbe_fc_rx_pause;
964                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
965         } else {
966                 hw->fc.current_mode = ngbe_fc_none;
967                 DEBUGOUT("Flow Control = NONE.\n");
968         }
969         return 0;
970 }
971
972 /**
973  *  ngbe_fc_autoneg_em - Enable flow control IEEE clause 37
974  *  @hw: pointer to hardware structure
975  *
976  *  Enable flow control according to IEEE clause 37.
977  **/
978 STATIC s32 ngbe_fc_autoneg_em(struct ngbe_hw *hw)
979 {
980         u8 technology_ability_reg = 0;
981         u8 lp_technology_ability_reg = 0;
982
983         hw->phy.get_adv_pause(hw, &technology_ability_reg);
984         hw->phy.get_lp_adv_pause(hw, &lp_technology_ability_reg);
985
986         return ngbe_negotiate_fc(hw, (u32)technology_ability_reg,
987                                   (u32)lp_technology_ability_reg,
988                                   NGBE_TAF_SYM_PAUSE, NGBE_TAF_ASM_PAUSE,
989                                   NGBE_TAF_SYM_PAUSE, NGBE_TAF_ASM_PAUSE);
990 }
991
992 /**
993  *  ngbe_fc_autoneg - Configure flow control
994  *  @hw: pointer to hardware structure
995  *
996  *  Compares our advertised flow control capabilities to those advertised by
997  *  our link partner, and determines the proper flow control mode to use.
998  **/
999 void ngbe_fc_autoneg(struct ngbe_hw *hw)
1000 {
1001         s32 err = NGBE_ERR_FC_NOT_NEGOTIATED;
1002         u32 speed;
1003         bool link_up;
1004
1005         DEBUGFUNC("ngbe_fc_autoneg");
1006
1007         /*
1008          * AN should have completed when the cable was plugged in.
1009          * Look for reasons to bail out.  Bail out if:
1010          * - FC autoneg is disabled, or if
1011          * - link is not up.
1012          */
1013         if (hw->fc.disable_fc_autoneg) {
1014                 DEBUGOUT("Flow control autoneg is disabled");
1015                 goto out;
1016         }
1017
1018         hw->mac.check_link(hw, &speed, &link_up, false);
1019         if (!link_up) {
1020                 DEBUGOUT("The link is down");
1021                 goto out;
1022         }
1023
1024         err = ngbe_fc_autoneg_em(hw);
1025
1026 out:
1027         if (err == 0) {
1028                 hw->fc.fc_was_autonegged = true;
1029         } else {
1030                 hw->fc.fc_was_autonegged = false;
1031                 hw->fc.current_mode = hw->fc.requested_mode;
1032         }
1033 }
1034
1035 /**
1036  *  ngbe_acquire_swfw_sync - Acquire SWFW semaphore
1037  *  @hw: pointer to hardware structure
1038  *  @mask: Mask to specify which semaphore to acquire
1039  *
1040  *  Acquires the SWFW semaphore through the MNGSEM register for the specified
1041  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
1042  **/
1043 s32 ngbe_acquire_swfw_sync(struct ngbe_hw *hw, u32 mask)
1044 {
1045         u32 mngsem = 0;
1046         u32 swmask = NGBE_MNGSEM_SW(mask);
1047         u32 fwmask = NGBE_MNGSEM_FW(mask);
1048         u32 timeout = 200;
1049         u32 i;
1050
1051         DEBUGFUNC("ngbe_acquire_swfw_sync");
1052
1053         for (i = 0; i < timeout; i++) {
1054                 /*
1055                  * SW NVM semaphore bit is used for access to all
1056                  * SW_FW_SYNC bits (not just NVM)
1057                  */
1058                 if (ngbe_get_eeprom_semaphore(hw))
1059                         return NGBE_ERR_SWFW_SYNC;
1060
1061                 mngsem = rd32(hw, NGBE_MNGSEM);
1062                 if (mngsem & (fwmask | swmask)) {
1063                         /* Resource is currently in use by FW or SW */
1064                         ngbe_release_eeprom_semaphore(hw);
1065                         msec_delay(5);
1066                 } else {
1067                         mngsem |= swmask;
1068                         wr32(hw, NGBE_MNGSEM, mngsem);
1069                         ngbe_release_eeprom_semaphore(hw);
1070                         return 0;
1071                 }
1072         }
1073
1074         /* If time expired clear the bits holding the lock and retry */
1075         if (mngsem & (fwmask | swmask))
1076                 ngbe_release_swfw_sync(hw, mngsem & (fwmask | swmask));
1077
1078         msec_delay(5);
1079         return NGBE_ERR_SWFW_SYNC;
1080 }
1081
1082 /**
1083  *  ngbe_release_swfw_sync - Release SWFW semaphore
1084  *  @hw: pointer to hardware structure
1085  *  @mask: Mask to specify which semaphore to release
1086  *
1087  *  Releases the SWFW semaphore through the MNGSEM register for the specified
1088  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
1089  **/
1090 void ngbe_release_swfw_sync(struct ngbe_hw *hw, u32 mask)
1091 {
1092         u32 mngsem;
1093         u32 swmask = mask;
1094
1095         DEBUGFUNC("ngbe_release_swfw_sync");
1096
1097         ngbe_get_eeprom_semaphore(hw);
1098
1099         mngsem = rd32(hw, NGBE_MNGSEM);
1100         mngsem &= ~swmask;
1101         wr32(hw, NGBE_MNGSEM, mngsem);
1102
1103         ngbe_release_eeprom_semaphore(hw);
1104 }
1105
1106 /**
1107  *  ngbe_disable_sec_rx_path - Stops the receive data path
1108  *  @hw: pointer to hardware structure
1109  *
1110  *  Stops the receive data path and waits for the HW to internally empty
1111  *  the Rx security block
1112  **/
1113 s32 ngbe_disable_sec_rx_path(struct ngbe_hw *hw)
1114 {
1115 #define NGBE_MAX_SECRX_POLL 4000
1116
1117         int i;
1118         u32 secrxreg;
1119
1120         DEBUGFUNC("ngbe_disable_sec_rx_path");
1121
1122
1123         secrxreg = rd32(hw, NGBE_SECRXCTL);
1124         secrxreg |= NGBE_SECRXCTL_XDSA;
1125         wr32(hw, NGBE_SECRXCTL, secrxreg);
1126         for (i = 0; i < NGBE_MAX_SECRX_POLL; i++) {
1127                 secrxreg = rd32(hw, NGBE_SECRXSTAT);
1128                 if (!(secrxreg & NGBE_SECRXSTAT_RDY))
1129                         /* Use interrupt-safe sleep just in case */
1130                         usec_delay(10);
1131                 else
1132                         break;
1133         }
1134
1135         /* For informational purposes only */
1136         if (i >= NGBE_MAX_SECRX_POLL)
1137                 DEBUGOUT("Rx unit being enabled before security "
1138                          "path fully disabled.  Continuing with init.\n");
1139
1140         return 0;
1141 }
1142
1143 /**
1144  *  ngbe_enable_sec_rx_path - Enables the receive data path
1145  *  @hw: pointer to hardware structure
1146  *
1147  *  Enables the receive data path.
1148  **/
1149 s32 ngbe_enable_sec_rx_path(struct ngbe_hw *hw)
1150 {
1151         u32 secrxreg;
1152
1153         DEBUGFUNC("ngbe_enable_sec_rx_path");
1154
1155         secrxreg = rd32(hw, NGBE_SECRXCTL);
1156         secrxreg &= ~NGBE_SECRXCTL_XDSA;
1157         wr32(hw, NGBE_SECRXCTL, secrxreg);
1158         ngbe_flush(hw);
1159
1160         return 0;
1161 }
1162
1163 /**
1164  *  ngbe_clear_vmdq - Disassociate a VMDq pool index from a rx address
1165  *  @hw: pointer to hardware struct
1166  *  @rar: receive address register index to disassociate
1167  *  @vmdq: VMDq pool index to remove from the rar
1168  **/
1169 s32 ngbe_clear_vmdq(struct ngbe_hw *hw, u32 rar, u32 vmdq)
1170 {
1171         u32 mpsar;
1172         u32 rar_entries = hw->mac.num_rar_entries;
1173
1174         DEBUGFUNC("ngbe_clear_vmdq");
1175
1176         /* Make sure we are using a valid rar index range */
1177         if (rar >= rar_entries) {
1178                 DEBUGOUT("RAR index %d is out of range.\n", rar);
1179                 return NGBE_ERR_INVALID_ARGUMENT;
1180         }
1181
1182         wr32(hw, NGBE_ETHADDRIDX, rar);
1183         mpsar = rd32(hw, NGBE_ETHADDRASS);
1184
1185         if (NGBE_REMOVED(hw->hw_addr))
1186                 goto done;
1187
1188         if (!mpsar)
1189                 goto done;
1190
1191         mpsar &= ~(1 << vmdq);
1192         wr32(hw, NGBE_ETHADDRASS, mpsar);
1193
1194         /* was that the last pool using this rar? */
1195         if (mpsar == 0 && rar != 0)
1196                 hw->mac.clear_rar(hw, rar);
1197 done:
1198         return 0;
1199 }
1200
1201 /**
1202  *  ngbe_set_vmdq - Associate a VMDq pool index with a rx address
1203  *  @hw: pointer to hardware struct
1204  *  @rar: receive address register index to associate with a VMDq index
1205  *  @vmdq: VMDq pool index
1206  **/
1207 s32 ngbe_set_vmdq(struct ngbe_hw *hw, u32 rar, u32 vmdq)
1208 {
1209         u32 mpsar;
1210         u32 rar_entries = hw->mac.num_rar_entries;
1211
1212         DEBUGFUNC("ngbe_set_vmdq");
1213
1214         /* Make sure we are using a valid rar index range */
1215         if (rar >= rar_entries) {
1216                 DEBUGOUT("RAR index %d is out of range.\n", rar);
1217                 return NGBE_ERR_INVALID_ARGUMENT;
1218         }
1219
1220         wr32(hw, NGBE_ETHADDRIDX, rar);
1221
1222         mpsar = rd32(hw, NGBE_ETHADDRASS);
1223         mpsar |= 1 << vmdq;
1224         wr32(hw, NGBE_ETHADDRASS, mpsar);
1225
1226         return 0;
1227 }
1228
1229 /**
1230  *  ngbe_init_uta_tables - Initialize the Unicast Table Array
1231  *  @hw: pointer to hardware structure
1232  **/
1233 s32 ngbe_init_uta_tables(struct ngbe_hw *hw)
1234 {
1235         int i;
1236
1237         DEBUGFUNC("ngbe_init_uta_tables");
1238         DEBUGOUT(" Clearing UTA\n");
1239
1240         for (i = 0; i < 128; i++)
1241                 wr32(hw, NGBE_UCADDRTBL(i), 0);
1242
1243         return 0;
1244 }
1245
1246 /**
1247  *  ngbe_find_vlvf_slot - find the vlanid or the first empty slot
1248  *  @hw: pointer to hardware structure
1249  *  @vlan: VLAN id to write to VLAN filter
1250  *  @vlvf_bypass: true to find vlanid only, false returns first empty slot if
1251  *                vlanid not found
1252  *
1253  *
1254  *  return the VLVF index where this VLAN id should be placed
1255  *
1256  **/
1257 s32 ngbe_find_vlvf_slot(struct ngbe_hw *hw, u32 vlan, bool vlvf_bypass)
1258 {
1259         s32 regindex, first_empty_slot;
1260         u32 bits;
1261
1262         /* short cut the special case */
1263         if (vlan == 0)
1264                 return 0;
1265
1266         /* if vlvf_bypass is set we don't want to use an empty slot, we
1267          * will simply bypass the VLVF if there are no entries present in the
1268          * VLVF that contain our VLAN
1269          */
1270         first_empty_slot = vlvf_bypass ? NGBE_ERR_NO_SPACE : 0;
1271
1272         /* add VLAN enable bit for comparison */
1273         vlan |= NGBE_PSRVLAN_EA;
1274
1275         /* Search for the vlan id in the VLVF entries. Save off the first empty
1276          * slot found along the way.
1277          *
1278          * pre-decrement loop covering (NGBE_NUM_POOL - 1) .. 1
1279          */
1280         for (regindex = NGBE_NUM_POOL; --regindex;) {
1281                 wr32(hw, NGBE_PSRVLANIDX, regindex);
1282                 bits = rd32(hw, NGBE_PSRVLAN);
1283                 if (bits == vlan)
1284                         return regindex;
1285                 if (!first_empty_slot && !bits)
1286                         first_empty_slot = regindex;
1287         }
1288
1289         /* If we are here then we didn't find the VLAN.  Return first empty
1290          * slot we found during our search, else error.
1291          */
1292         if (!first_empty_slot)
1293                 DEBUGOUT("No space in VLVF.\n");
1294
1295         return first_empty_slot ? first_empty_slot : NGBE_ERR_NO_SPACE;
1296 }
1297
1298 /**
1299  *  ngbe_set_vfta - Set VLAN filter table
1300  *  @hw: pointer to hardware structure
1301  *  @vlan: VLAN id to write to VLAN filter
1302  *  @vind: VMDq output index that maps queue to VLAN id in VLVFB
1303  *  @vlan_on: boolean flag to turn on/off VLAN
1304  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
1305  *
1306  *  Turn on/off specified VLAN in the VLAN filter table.
1307  **/
1308 s32 ngbe_set_vfta(struct ngbe_hw *hw, u32 vlan, u32 vind,
1309                            bool vlan_on, bool vlvf_bypass)
1310 {
1311         u32 regidx, vfta_delta, vfta;
1312         s32 err;
1313
1314         DEBUGFUNC("ngbe_set_vfta");
1315
1316         if (vlan > 4095 || vind > 63)
1317                 return NGBE_ERR_PARAM;
1318
1319         /*
1320          * this is a 2 part operation - first the VFTA, then the
1321          * VLVF and VLVFB if VT Mode is set
1322          * We don't write the VFTA until we know the VLVF part succeeded.
1323          */
1324
1325         /* Part 1
1326          * The VFTA is a bitstring made up of 128 32-bit registers
1327          * that enable the particular VLAN id, much like the MTA:
1328          *    bits[11-5]: which register
1329          *    bits[4-0]:  which bit in the register
1330          */
1331         regidx = vlan / 32;
1332         vfta_delta = 1 << (vlan % 32);
1333         vfta = rd32(hw, NGBE_VLANTBL(regidx));
1334
1335         /*
1336          * vfta_delta represents the difference between the current value
1337          * of vfta and the value we want in the register.  Since the diff
1338          * is an XOR mask we can just update the vfta using an XOR
1339          */
1340         vfta_delta &= vlan_on ? ~vfta : vfta;
1341         vfta ^= vfta_delta;
1342
1343         /* Part 2
1344          * Call ngbe_set_vlvf to set VLVFB and VLVF
1345          */
1346         err = ngbe_set_vlvf(hw, vlan, vind, vlan_on, &vfta_delta,
1347                                          vfta, vlvf_bypass);
1348         if (err != 0) {
1349                 if (vlvf_bypass)
1350                         goto vfta_update;
1351                 return err;
1352         }
1353
1354 vfta_update:
1355         /* Update VFTA now that we are ready for traffic */
1356         if (vfta_delta)
1357                 wr32(hw, NGBE_VLANTBL(regidx), vfta);
1358
1359         return 0;
1360 }
1361
1362 /**
1363  *  ngbe_set_vlvf - Set VLAN Pool Filter
1364  *  @hw: pointer to hardware structure
1365  *  @vlan: VLAN id to write to VLAN filter
1366  *  @vind: VMDq output index that maps queue to VLAN id in PSRVLANPLM
1367  *  @vlan_on: boolean flag to turn on/off VLAN in PSRVLAN
1368  *  @vfta_delta: pointer to the difference between the current value
1369  *               of PSRVLANPLM and the desired value
1370  *  @vfta: the desired value of the VFTA
1371  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
1372  *
1373  *  Turn on/off specified bit in VLVF table.
1374  **/
1375 s32 ngbe_set_vlvf(struct ngbe_hw *hw, u32 vlan, u32 vind,
1376                            bool vlan_on, u32 *vfta_delta, u32 vfta,
1377                            bool vlvf_bypass)
1378 {
1379         u32 bits;
1380         u32 portctl;
1381         s32 vlvf_index;
1382
1383         DEBUGFUNC("ngbe_set_vlvf");
1384
1385         if (vlan > 4095 || vind > 63)
1386                 return NGBE_ERR_PARAM;
1387
1388         /* If VT Mode is set
1389          *   Either vlan_on
1390          *     make sure the vlan is in PSRVLAN
1391          *     set the vind bit in the matching PSRVLANPLM
1392          *   Or !vlan_on
1393          *     clear the pool bit and possibly the vind
1394          */
1395         portctl = rd32(hw, NGBE_PORTCTL);
1396         if (!(portctl & NGBE_PORTCTL_NUMVT_MASK))
1397                 return 0;
1398
1399         vlvf_index = ngbe_find_vlvf_slot(hw, vlan, vlvf_bypass);
1400         if (vlvf_index < 0)
1401                 return vlvf_index;
1402
1403         wr32(hw, NGBE_PSRVLANIDX, vlvf_index);
1404         bits = rd32(hw, NGBE_PSRVLANPLM(vind / 32));
1405
1406         /* set the pool bit */
1407         bits |= 1 << (vind % 32);
1408         if (vlan_on)
1409                 goto vlvf_update;
1410
1411         /* clear the pool bit */
1412         bits ^= 1 << (vind % 32);
1413
1414         if (!bits &&
1415             !rd32(hw, NGBE_PSRVLANPLM(vind / 32))) {
1416                 /* Clear PSRVLANPLM first, then disable PSRVLAN. Otherwise
1417                  * we run the risk of stray packets leaking into
1418                  * the PF via the default pool
1419                  */
1420                 if (*vfta_delta)
1421                         wr32(hw, NGBE_PSRVLANPLM(vlan / 32), vfta);
1422
1423                 /* disable VLVF and clear remaining bit from pool */
1424                 wr32(hw, NGBE_PSRVLAN, 0);
1425                 wr32(hw, NGBE_PSRVLANPLM(vind / 32), 0);
1426
1427                 return 0;
1428         }
1429
1430         /* If there are still bits set in the PSRVLANPLM registers
1431          * for the VLAN ID indicated we need to see if the
1432          * caller is requesting that we clear the PSRVLANPLM entry bit.
1433          * If the caller has requested that we clear the PSRVLANPLM
1434          * entry bit but there are still pools/VFs using this VLAN
1435          * ID entry then ignore the request.  We're not worried
1436          * about the case where we're turning the PSRVLANPLM VLAN ID
1437          * entry bit on, only when requested to turn it off as
1438          * there may be multiple pools and/or VFs using the
1439          * VLAN ID entry.  In that case we cannot clear the
1440          * PSRVLANPLM bit until all pools/VFs using that VLAN ID have also
1441          * been cleared.  This will be indicated by "bits" being
1442          * zero.
1443          */
1444         *vfta_delta = 0;
1445
1446 vlvf_update:
1447         /* record pool change and enable VLAN ID if not already enabled */
1448         wr32(hw, NGBE_PSRVLANPLM(vind / 32), bits);
1449         wr32(hw, NGBE_PSRVLAN, NGBE_PSRVLAN_EA | vlan);
1450
1451         return 0;
1452 }
1453
1454 /**
1455  *  ngbe_clear_vfta - Clear VLAN filter table
1456  *  @hw: pointer to hardware structure
1457  *
1458  *  Clears the VLAN filer table, and the VMDq index associated with the filter
1459  **/
1460 s32 ngbe_clear_vfta(struct ngbe_hw *hw)
1461 {
1462         u32 offset;
1463
1464         DEBUGFUNC("ngbe_clear_vfta");
1465
1466         for (offset = 0; offset < hw->mac.vft_size; offset++)
1467                 wr32(hw, NGBE_VLANTBL(offset), 0);
1468
1469         for (offset = 0; offset < NGBE_NUM_POOL; offset++) {
1470                 wr32(hw, NGBE_PSRVLANIDX, offset);
1471                 wr32(hw, NGBE_PSRVLAN, 0);
1472                 wr32(hw, NGBE_PSRVLANPLM(0), 0);
1473         }
1474
1475         return 0;
1476 }
1477
1478 /**
1479  *  ngbe_check_mac_link_em - Determine link and speed status
1480  *  @hw: pointer to hardware structure
1481  *  @speed: pointer to link speed
1482  *  @link_up: true when link is up
1483  *  @link_up_wait_to_complete: bool used to wait for link up or not
1484  *
1485  *  Reads the links register to determine if link is up and the current speed
1486  **/
1487 s32 ngbe_check_mac_link_em(struct ngbe_hw *hw, u32 *speed,
1488                         bool *link_up, bool link_up_wait_to_complete)
1489 {
1490         u32 i, reg;
1491         s32 status = 0;
1492
1493         DEBUGFUNC("ngbe_check_mac_link_em");
1494
1495         reg = rd32(hw, NGBE_GPIOINTSTAT);
1496         wr32(hw, NGBE_GPIOEOI, reg);
1497
1498         if (link_up_wait_to_complete) {
1499                 for (i = 0; i < hw->mac.max_link_up_time; i++) {
1500                         status = hw->phy.check_link(hw, speed, link_up);
1501                         if (*link_up)
1502                                 break;
1503                         msec_delay(100);
1504                 }
1505         } else {
1506                 status = hw->phy.check_link(hw, speed, link_up);
1507         }
1508
1509         return status;
1510 }
1511
1512 s32 ngbe_get_link_capabilities_em(struct ngbe_hw *hw,
1513                                       u32 *speed,
1514                                       bool *autoneg)
1515 {
1516         s32 status = 0;
1517
1518         DEBUGFUNC("\n");
1519
1520         hw->mac.autoneg = *autoneg;
1521
1522         switch (hw->sub_device_id) {
1523         case NGBE_SUB_DEV_ID_EM_RTL_SGMII:
1524                 *speed = NGBE_LINK_SPEED_1GB_FULL |
1525                         NGBE_LINK_SPEED_100M_FULL |
1526                         NGBE_LINK_SPEED_10M_FULL;
1527                 break;
1528         default:
1529                 break;
1530         }
1531
1532         return status;
1533 }
1534
1535 s32 ngbe_setup_mac_link_em(struct ngbe_hw *hw,
1536                                u32 speed,
1537                                bool autoneg_wait_to_complete)
1538 {
1539         s32 status;
1540
1541         DEBUGFUNC("\n");
1542
1543         /* Setup the PHY according to input speed */
1544         status = hw->phy.setup_link(hw, speed, autoneg_wait_to_complete);
1545
1546         return status;
1547 }
1548
1549 /**
1550  *  ngbe_set_mac_anti_spoofing - Enable/Disable MAC anti-spoofing
1551  *  @hw: pointer to hardware structure
1552  *  @enable: enable or disable switch for MAC anti-spoofing
1553  *  @vf: Virtual Function pool - VF Pool to set for MAC anti-spoofing
1554  *
1555  **/
1556 void ngbe_set_mac_anti_spoofing(struct ngbe_hw *hw, bool enable, int vf)
1557 {
1558         u32 pfvfspoof;
1559
1560         pfvfspoof = rd32(hw, NGBE_POOLTXASMAC);
1561         if (enable)
1562                 pfvfspoof |= (1 << vf);
1563         else
1564                 pfvfspoof &= ~(1 << vf);
1565         wr32(hw, NGBE_POOLTXASMAC, pfvfspoof);
1566 }
1567
1568 /**
1569  *  ngbe_set_vlan_anti_spoofing - Enable/Disable VLAN anti-spoofing
1570  *  @hw: pointer to hardware structure
1571  *  @enable: enable or disable switch for VLAN anti-spoofing
1572  *  @vf: Virtual Function pool - VF Pool to set for VLAN anti-spoofing
1573  *
1574  **/
1575 void ngbe_set_vlan_anti_spoofing(struct ngbe_hw *hw, bool enable, int vf)
1576 {
1577         u32 pfvfspoof;
1578
1579         pfvfspoof = rd32(hw, NGBE_POOLTXASVLAN);
1580         if (enable)
1581                 pfvfspoof |= (1 << vf);
1582         else
1583                 pfvfspoof &= ~(1 << vf);
1584         wr32(hw, NGBE_POOLTXASVLAN, pfvfspoof);
1585 }
1586
1587 /**
1588  *  ngbe_init_thermal_sensor_thresh - Inits thermal sensor thresholds
1589  *  @hw: pointer to hardware structure
1590  *
1591  *  Inits the thermal sensor thresholds according to the NVM map
1592  *  and save off the threshold and location values into mac.thermal_sensor_data
1593  **/
1594 s32 ngbe_init_thermal_sensor_thresh(struct ngbe_hw *hw)
1595 {
1596         struct ngbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
1597
1598         DEBUGFUNC("ngbe_init_thermal_sensor_thresh");
1599
1600         memset(data, 0, sizeof(struct ngbe_thermal_sensor_data));
1601
1602         if (hw->bus.lan_id != 0)
1603                 return NGBE_NOT_IMPLEMENTED;
1604
1605         wr32(hw, NGBE_TSINTR,
1606                 NGBE_TSINTR_AEN | NGBE_TSINTR_DEN);
1607         wr32(hw, NGBE_TSEN, NGBE_TSEN_ENA);
1608
1609
1610         data->sensor[0].alarm_thresh = 115;
1611         wr32(hw, NGBE_TSATHRE, 0x344);
1612         data->sensor[0].dalarm_thresh = 110;
1613         wr32(hw, NGBE_TSDTHRE, 0x330);
1614
1615         return 0;
1616 }
1617
1618 s32 ngbe_mac_check_overtemp(struct ngbe_hw *hw)
1619 {
1620         s32 status = 0;
1621         u32 ts_state;
1622
1623         DEBUGFUNC("ngbe_mac_check_overtemp");
1624
1625         /* Check that the LASI temp alarm status was triggered */
1626         ts_state = rd32(hw, NGBE_TSALM);
1627
1628         if (ts_state & NGBE_TSALM_HI)
1629                 status = NGBE_ERR_UNDERTEMP;
1630         else if (ts_state & NGBE_TSALM_LO)
1631                 status = NGBE_ERR_OVERTEMP;
1632
1633         return status;
1634 }
1635
1636 void ngbe_disable_rx(struct ngbe_hw *hw)
1637 {
1638         u32 pfdtxgswc;
1639
1640         pfdtxgswc = rd32(hw, NGBE_PSRCTL);
1641         if (pfdtxgswc & NGBE_PSRCTL_LBENA) {
1642                 pfdtxgswc &= ~NGBE_PSRCTL_LBENA;
1643                 wr32(hw, NGBE_PSRCTL, pfdtxgswc);
1644                 hw->mac.set_lben = true;
1645         } else {
1646                 hw->mac.set_lben = false;
1647         }
1648
1649         wr32m(hw, NGBE_PBRXCTL, NGBE_PBRXCTL_ENA, 0);
1650         wr32m(hw, NGBE_MACRXCFG, NGBE_MACRXCFG_ENA, 0);
1651 }
1652
1653 void ngbe_enable_rx(struct ngbe_hw *hw)
1654 {
1655         u32 pfdtxgswc;
1656
1657         wr32m(hw, NGBE_MACRXCFG, NGBE_MACRXCFG_ENA, NGBE_MACRXCFG_ENA);
1658         wr32m(hw, NGBE_PBRXCTL, NGBE_PBRXCTL_ENA, NGBE_PBRXCTL_ENA);
1659
1660         if (hw->mac.set_lben) {
1661                 pfdtxgswc = rd32(hw, NGBE_PSRCTL);
1662                 pfdtxgswc |= NGBE_PSRCTL_LBENA;
1663                 wr32(hw, NGBE_PSRCTL, pfdtxgswc);
1664                 hw->mac.set_lben = false;
1665         }
1666 }
1667
1668 /**
1669  *  ngbe_set_mac_type - Sets MAC type
1670  *  @hw: pointer to the HW structure
1671  *
1672  *  This function sets the mac type of the adapter based on the
1673  *  vendor ID and device ID stored in the hw structure.
1674  **/
1675 s32 ngbe_set_mac_type(struct ngbe_hw *hw)
1676 {
1677         s32 err = 0;
1678
1679         DEBUGFUNC("ngbe_set_mac_type");
1680
1681         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
1682                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
1683                 return NGBE_ERR_DEVICE_NOT_SUPPORTED;
1684         }
1685
1686         switch (hw->sub_device_id) {
1687         case NGBE_SUB_DEV_ID_EM_RTL_SGMII:
1688         case NGBE_SUB_DEV_ID_EM_MVL_RGMII:
1689                 hw->phy.media_type = ngbe_media_type_copper;
1690                 hw->mac.type = ngbe_mac_em;
1691                 break;
1692         case NGBE_SUB_DEV_ID_EM_MVL_SFP:
1693         case NGBE_SUB_DEV_ID_EM_YT8521S_SFP:
1694                 hw->phy.media_type = ngbe_media_type_fiber;
1695                 hw->mac.type = ngbe_mac_em;
1696                 break;
1697         case NGBE_SUB_DEV_ID_EM_VF:
1698                 hw->phy.media_type = ngbe_media_type_virtual;
1699                 hw->mac.type = ngbe_mac_em_vf;
1700                 break;
1701         default:
1702                 err = NGBE_ERR_DEVICE_NOT_SUPPORTED;
1703                 hw->phy.media_type = ngbe_media_type_unknown;
1704                 hw->mac.type = ngbe_mac_unknown;
1705                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
1706                 break;
1707         }
1708
1709         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
1710                   hw->mac.type, hw->phy.media_type, err);
1711         return err;
1712 }
1713
1714 /**
1715  *  ngbe_enable_rx_dma - Enable the Rx DMA unit
1716  *  @hw: pointer to hardware structure
1717  *  @regval: register value to write to RXCTRL
1718  *
1719  *  Enables the Rx DMA unit
1720  **/
1721 s32 ngbe_enable_rx_dma(struct ngbe_hw *hw, u32 regval)
1722 {
1723         DEBUGFUNC("ngbe_enable_rx_dma");
1724
1725         /*
1726          * Workaround silicon errata when enabling the Rx datapath.
1727          * If traffic is incoming before we enable the Rx unit, it could hang
1728          * the Rx DMA unit.  Therefore, make sure the security engine is
1729          * completely disabled prior to enabling the Rx unit.
1730          */
1731
1732         hw->mac.disable_sec_rx_path(hw);
1733
1734         if (regval & NGBE_PBRXCTL_ENA)
1735                 ngbe_enable_rx(hw);
1736         else
1737                 ngbe_disable_rx(hw);
1738
1739         hw->mac.enable_sec_rx_path(hw);
1740
1741         return 0;
1742 }
1743
1744 void ngbe_map_device_id(struct ngbe_hw *hw)
1745 {
1746         u16 oem = hw->sub_system_id & NGBE_OEM_MASK;
1747         u16 internal = hw->sub_system_id & NGBE_INTERNAL_MASK;
1748         hw->is_pf = true;
1749
1750         /* move subsystem_device_id to device_id */
1751         switch (hw->device_id) {
1752         case NGBE_DEV_ID_EM_WX1860AL_W_VF:
1753         case NGBE_DEV_ID_EM_WX1860A2_VF:
1754         case NGBE_DEV_ID_EM_WX1860A2S_VF:
1755         case NGBE_DEV_ID_EM_WX1860A4_VF:
1756         case NGBE_DEV_ID_EM_WX1860A4S_VF:
1757         case NGBE_DEV_ID_EM_WX1860AL2_VF:
1758         case NGBE_DEV_ID_EM_WX1860AL2S_VF:
1759         case NGBE_DEV_ID_EM_WX1860AL4_VF:
1760         case NGBE_DEV_ID_EM_WX1860AL4S_VF:
1761         case NGBE_DEV_ID_EM_WX1860NCSI_VF:
1762         case NGBE_DEV_ID_EM_WX1860A1_VF:
1763         case NGBE_DEV_ID_EM_WX1860A1L_VF:
1764                 hw->device_id = NGBE_DEV_ID_EM_VF;
1765                 hw->sub_device_id = NGBE_SUB_DEV_ID_EM_VF;
1766                 hw->is_pf = false;
1767                 break;
1768         case NGBE_DEV_ID_EM_WX1860AL_W:
1769         case NGBE_DEV_ID_EM_WX1860A2:
1770         case NGBE_DEV_ID_EM_WX1860A2S:
1771         case NGBE_DEV_ID_EM_WX1860A4:
1772         case NGBE_DEV_ID_EM_WX1860A4S:
1773         case NGBE_DEV_ID_EM_WX1860AL2:
1774         case NGBE_DEV_ID_EM_WX1860AL2S:
1775         case NGBE_DEV_ID_EM_WX1860AL4:
1776         case NGBE_DEV_ID_EM_WX1860AL4S:
1777         case NGBE_DEV_ID_EM_WX1860NCSI:
1778         case NGBE_DEV_ID_EM_WX1860A1:
1779         case NGBE_DEV_ID_EM_WX1860A1L:
1780                 hw->device_id = NGBE_DEV_ID_EM;
1781                 if (oem == NGBE_LY_M88E1512_SFP ||
1782                                 internal == NGBE_INTERNAL_SFP)
1783                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_MVL_SFP;
1784                 else if (hw->sub_system_id == NGBE_SUB_DEV_ID_EM_M88E1512_RJ45)
1785                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_MVL_RGMII;
1786                 else if (oem == NGBE_YT8521S_SFP ||
1787                                 oem == NGBE_LY_YT8521S_SFP)
1788                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_YT8521S_SFP;
1789                 else
1790                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_RTL_SGMII;
1791                 break;
1792         default:
1793                 break;
1794         }
1795 }
1796
1797 /**
1798  *  ngbe_init_ops_pf - Inits func ptrs and MAC type
1799  *  @hw: pointer to hardware structure
1800  *
1801  *  Initialize the function pointers and assign the MAC type.
1802  *  Does not touch the hardware.
1803  **/
1804 s32 ngbe_init_ops_pf(struct ngbe_hw *hw)
1805 {
1806         struct ngbe_bus_info *bus = &hw->bus;
1807         struct ngbe_mac_info *mac = &hw->mac;
1808         struct ngbe_phy_info *phy = &hw->phy;
1809         struct ngbe_rom_info *rom = &hw->rom;
1810         struct ngbe_mbx_info *mbx = &hw->mbx;
1811
1812         DEBUGFUNC("ngbe_init_ops_pf");
1813
1814         /* BUS */
1815         bus->set_lan_id = ngbe_set_lan_id_multi_port;
1816
1817         /* PHY */
1818         phy->identify = ngbe_identify_phy;
1819         phy->read_reg = ngbe_read_phy_reg;
1820         phy->write_reg = ngbe_write_phy_reg;
1821         phy->read_reg_unlocked = ngbe_read_phy_reg_mdi;
1822         phy->write_reg_unlocked = ngbe_write_phy_reg_mdi;
1823         phy->reset_hw = ngbe_reset_phy;
1824
1825         /* MAC */
1826         mac->init_hw = ngbe_init_hw;
1827         mac->reset_hw = ngbe_reset_hw_em;
1828         mac->start_hw = ngbe_start_hw;
1829         mac->clear_hw_cntrs = ngbe_clear_hw_cntrs;
1830         mac->enable_rx_dma = ngbe_enable_rx_dma;
1831         mac->get_mac_addr = ngbe_get_mac_addr;
1832         mac->stop_hw = ngbe_stop_hw;
1833         mac->acquire_swfw_sync = ngbe_acquire_swfw_sync;
1834         mac->release_swfw_sync = ngbe_release_swfw_sync;
1835
1836         mac->disable_sec_rx_path = ngbe_disable_sec_rx_path;
1837         mac->enable_sec_rx_path = ngbe_enable_sec_rx_path;
1838
1839         /* RAR, VLAN, Multicast */
1840         mac->set_rar = ngbe_set_rar;
1841         mac->clear_rar = ngbe_clear_rar;
1842         mac->init_rx_addrs = ngbe_init_rx_addrs;
1843         mac->update_mc_addr_list = ngbe_update_mc_addr_list;
1844         mac->set_vmdq = ngbe_set_vmdq;
1845         mac->clear_vmdq = ngbe_clear_vmdq;
1846         mac->set_vfta = ngbe_set_vfta;
1847         mac->set_vlvf = ngbe_set_vlvf;
1848         mac->clear_vfta = ngbe_clear_vfta;
1849         mac->set_mac_anti_spoofing = ngbe_set_mac_anti_spoofing;
1850         mac->set_vlan_anti_spoofing = ngbe_set_vlan_anti_spoofing;
1851
1852         /* Flow Control */
1853         mac->fc_enable = ngbe_fc_enable;
1854         mac->fc_autoneg = ngbe_fc_autoneg;
1855         mac->setup_fc = ngbe_setup_fc_em;
1856
1857         /* Link */
1858         mac->get_link_capabilities = ngbe_get_link_capabilities_em;
1859         mac->check_link = ngbe_check_mac_link_em;
1860         mac->setup_link = ngbe_setup_mac_link_em;
1861
1862         /* Manageability interface */
1863         mac->init_thermal_sensor_thresh = ngbe_init_thermal_sensor_thresh;
1864         mac->check_overtemp = ngbe_mac_check_overtemp;
1865
1866         mbx->init_params = ngbe_init_mbx_params_pf;
1867         mbx->read = ngbe_read_mbx_pf;
1868         mbx->write = ngbe_write_mbx_pf;
1869         mbx->check_for_msg = ngbe_check_for_msg_pf;
1870         mbx->check_for_ack = ngbe_check_for_ack_pf;
1871         mbx->check_for_rst = ngbe_check_for_rst_pf;
1872
1873         /* EEPROM */
1874         rom->init_params = ngbe_init_eeprom_params;
1875         rom->read32 = ngbe_ee_read32;
1876         rom->validate_checksum = ngbe_validate_eeprom_checksum_em;
1877
1878         mac->mcft_size          = NGBE_EM_MC_TBL_SIZE;
1879         mac->vft_size           = NGBE_EM_VFT_TBL_SIZE;
1880         mac->num_rar_entries    = NGBE_EM_RAR_ENTRIES;
1881         mac->max_rx_queues      = NGBE_EM_MAX_RX_QUEUES;
1882         mac->max_tx_queues      = NGBE_EM_MAX_TX_QUEUES;
1883
1884         mac->default_speeds = NGBE_LINK_SPEED_10M_FULL |
1885                                 NGBE_LINK_SPEED_100M_FULL |
1886                                 NGBE_LINK_SPEED_1GB_FULL;
1887
1888         return 0;
1889 }
1890
1891 /**
1892  *  ngbe_init_shared_code - Initialize the shared code
1893  *  @hw: pointer to hardware structure
1894  *
1895  *  This will assign function pointers and assign the MAC type and PHY code.
1896  *  Does not touch the hardware. This function must be called prior to any
1897  *  other function in the shared code. The ngbe_hw structure should be
1898  *  memset to 0 prior to calling this function.  The following fields in
1899  *  hw structure should be filled in prior to calling this function:
1900  *  hw_addr, back, device_id, vendor_id, subsystem_device_id
1901  **/
1902 s32 ngbe_init_shared_code(struct ngbe_hw *hw)
1903 {
1904         s32 status = 0;
1905
1906         DEBUGFUNC("ngbe_init_shared_code");
1907
1908         /*
1909          * Set the mac type
1910          */
1911         ngbe_set_mac_type(hw);
1912
1913         ngbe_init_ops_dummy(hw);
1914         switch (hw->mac.type) {
1915         case ngbe_mac_em:
1916                 ngbe_init_ops_pf(hw);
1917                 break;
1918         default:
1919                 status = NGBE_ERR_DEVICE_NOT_SUPPORTED;
1920                 break;
1921         }
1922         hw->mac.max_link_up_time = NGBE_LINK_UP_TIME;
1923
1924         hw->bus.set_lan_id(hw);
1925
1926         return status;
1927 }
1928