net/ngbe: support device start/stop
[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_phy.h"
8 #include "ngbe_eeprom.h"
9 #include "ngbe_mng.h"
10 #include "ngbe_hw.h"
11
12 /**
13  *  ngbe_start_hw - Prepare hardware for Tx/Rx
14  *  @hw: pointer to hardware structure
15  *
16  *  Starts the hardware.
17  **/
18 s32 ngbe_start_hw(struct ngbe_hw *hw)
19 {
20         DEBUGFUNC("ngbe_start_hw");
21
22         /* Clear adapter stopped flag */
23         hw->adapter_stopped = false;
24
25         return 0;
26 }
27
28 /**
29  *  ngbe_init_hw - Generic hardware initialization
30  *  @hw: pointer to hardware structure
31  *
32  *  Initialize the hardware by resetting the hardware, filling the bus info
33  *  structure and media type, clears all on chip counters, initializes receive
34  *  address registers, multicast table, VLAN filter table, calls routine to set
35  *  up link and flow control settings, and leaves transmit and receive units
36  *  disabled and uninitialized
37  **/
38 s32 ngbe_init_hw(struct ngbe_hw *hw)
39 {
40         s32 status;
41
42         DEBUGFUNC("ngbe_init_hw");
43
44         /* Reset the hardware */
45         status = hw->mac.reset_hw(hw);
46         if (status == 0) {
47                 /* Start the HW */
48                 status = hw->mac.start_hw(hw);
49         }
50
51         if (status != 0)
52                 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
53
54         return status;
55 }
56
57 static void
58 ngbe_reset_misc_em(struct ngbe_hw *hw)
59 {
60         int i;
61
62         wr32(hw, NGBE_ISBADDRL, hw->isb_dma & 0xFFFFFFFF);
63         wr32(hw, NGBE_ISBADDRH, hw->isb_dma >> 32);
64
65         /* receive packets that size > 2048 */
66         wr32m(hw, NGBE_MACRXCFG,
67                 NGBE_MACRXCFG_JUMBO, NGBE_MACRXCFG_JUMBO);
68
69         wr32m(hw, NGBE_FRMSZ, NGBE_FRMSZ_MAX_MASK,
70                 NGBE_FRMSZ_MAX(NGBE_FRAME_SIZE_DFT));
71
72         /* clear counters on read */
73         wr32m(hw, NGBE_MACCNTCTL,
74                 NGBE_MACCNTCTL_RC, NGBE_MACCNTCTL_RC);
75
76         wr32m(hw, NGBE_RXFCCFG,
77                 NGBE_RXFCCFG_FC, NGBE_RXFCCFG_FC);
78         wr32m(hw, NGBE_TXFCCFG,
79                 NGBE_TXFCCFG_FC, NGBE_TXFCCFG_FC);
80
81         wr32m(hw, NGBE_MACRXFLT,
82                 NGBE_MACRXFLT_PROMISC, NGBE_MACRXFLT_PROMISC);
83
84         wr32m(hw, NGBE_RSTSTAT,
85                 NGBE_RSTSTAT_TMRINIT_MASK, NGBE_RSTSTAT_TMRINIT(30));
86
87         /* errata 4: initialize mng flex tbl and wakeup flex tbl*/
88         wr32(hw, NGBE_MNGFLEXSEL, 0);
89         for (i = 0; i < 16; i++) {
90                 wr32(hw, NGBE_MNGFLEXDWL(i), 0);
91                 wr32(hw, NGBE_MNGFLEXDWH(i), 0);
92                 wr32(hw, NGBE_MNGFLEXMSK(i), 0);
93         }
94         wr32(hw, NGBE_LANFLEXSEL, 0);
95         for (i = 0; i < 16; i++) {
96                 wr32(hw, NGBE_LANFLEXDWL(i), 0);
97                 wr32(hw, NGBE_LANFLEXDWH(i), 0);
98                 wr32(hw, NGBE_LANFLEXMSK(i), 0);
99         }
100
101         /* set pause frame dst mac addr */
102         wr32(hw, NGBE_RXPBPFCDMACL, 0xC2000001);
103         wr32(hw, NGBE_RXPBPFCDMACH, 0x0180);
104
105         wr32(hw, NGBE_MDIOMODE, 0xF);
106
107         wr32m(hw, NGBE_GPIE, NGBE_GPIE_MSIX, NGBE_GPIE_MSIX);
108
109         if ((hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_M88E1512_SFP ||
110                 (hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_YT8521S_SFP) {
111                 /* gpio0 is used to power on/off control*/
112                 wr32(hw, NGBE_GPIODIR, NGBE_GPIODIR_DDR(1));
113                 wr32(hw, NGBE_GPIODATA, NGBE_GPIOBIT_0);
114         }
115
116         hw->mac.init_thermal_sensor_thresh(hw);
117
118         /* enable mac transmitter */
119         wr32m(hw, NGBE_MACTXCFG, NGBE_MACTXCFG_TE, NGBE_MACTXCFG_TE);
120
121         /* sellect GMII */
122         wr32m(hw, NGBE_MACTXCFG,
123                 NGBE_MACTXCFG_SPEED_MASK, NGBE_MACTXCFG_SPEED_1G);
124
125         for (i = 0; i < 4; i++)
126                 wr32m(hw, NGBE_IVAR(i), 0x80808080, 0);
127 }
128
129 /**
130  *  ngbe_reset_hw_em - Perform hardware reset
131  *  @hw: pointer to hardware structure
132  *
133  *  Resets the hardware by resetting the transmit and receive units, masks
134  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
135  *  reset.
136  **/
137 s32 ngbe_reset_hw_em(struct ngbe_hw *hw)
138 {
139         s32 status;
140
141         DEBUGFUNC("ngbe_reset_hw_em");
142
143         /* Call adapter stop to disable tx/rx and clear interrupts */
144         status = hw->mac.stop_hw(hw);
145         if (status != 0)
146                 return status;
147
148         /* Identify PHY and related function pointers */
149         status = ngbe_init_phy(hw);
150         if (status)
151                 return status;
152
153         /* Reset PHY */
154         if (!hw->phy.reset_disable)
155                 hw->phy.reset_hw(hw);
156
157         wr32(hw, NGBE_RST, NGBE_RST_LAN(hw->bus.lan_id));
158         ngbe_flush(hw);
159         msec_delay(50);
160
161         ngbe_reset_misc_em(hw);
162
163         msec_delay(50);
164
165         /* Store the permanent mac address */
166         hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
167
168         /*
169          * Store MAC address from RAR0, clear receive address registers, and
170          * clear the multicast table.
171          */
172         hw->mac.num_rar_entries = NGBE_EM_RAR_ENTRIES;
173         hw->mac.init_rx_addrs(hw);
174
175         return status;
176 }
177
178 /**
179  *  ngbe_get_mac_addr - Generic get MAC address
180  *  @hw: pointer to hardware structure
181  *  @mac_addr: Adapter MAC address
182  *
183  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
184  *  A reset of the adapter must be performed prior to calling this function
185  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
186  **/
187 s32 ngbe_get_mac_addr(struct ngbe_hw *hw, u8 *mac_addr)
188 {
189         u32 rar_high;
190         u32 rar_low;
191         u16 i;
192
193         DEBUGFUNC("ngbe_get_mac_addr");
194
195         wr32(hw, NGBE_ETHADDRIDX, 0);
196         rar_high = rd32(hw, NGBE_ETHADDRH);
197         rar_low = rd32(hw, NGBE_ETHADDRL);
198
199         for (i = 0; i < 2; i++)
200                 mac_addr[i] = (u8)(rar_high >> (1 - i) * 8);
201
202         for (i = 0; i < 4; i++)
203                 mac_addr[i + 2] = (u8)(rar_low >> (3 - i) * 8);
204
205         return 0;
206 }
207
208 /**
209  *  ngbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
210  *  @hw: pointer to the HW structure
211  *
212  *  Determines the LAN function id by reading memory-mapped registers and swaps
213  *  the port value if requested, and set MAC instance for devices.
214  **/
215 void ngbe_set_lan_id_multi_port(struct ngbe_hw *hw)
216 {
217         struct ngbe_bus_info *bus = &hw->bus;
218         u32 reg = 0;
219
220         DEBUGFUNC("ngbe_set_lan_id_multi_port");
221
222         reg = rd32(hw, NGBE_PORTSTAT);
223         bus->lan_id = NGBE_PORTSTAT_ID(reg);
224         bus->func = bus->lan_id;
225 }
226
227 /**
228  *  ngbe_stop_hw - Generic stop Tx/Rx units
229  *  @hw: pointer to hardware structure
230  *
231  *  Sets the adapter_stopped flag within ngbe_hw struct. Clears interrupts,
232  *  disables transmit and receive units. The adapter_stopped flag is used by
233  *  the shared code and drivers to determine if the adapter is in a stopped
234  *  state and should not touch the hardware.
235  **/
236 s32 ngbe_stop_hw(struct ngbe_hw *hw)
237 {
238         u32 reg_val;
239         u16 i;
240
241         DEBUGFUNC("ngbe_stop_hw");
242
243         /*
244          * Set the adapter_stopped flag so other driver functions stop touching
245          * the hardware
246          */
247         hw->adapter_stopped = true;
248
249         /* Disable the receive unit */
250         ngbe_disable_rx(hw);
251
252         /* Clear interrupt mask to stop interrupts from being generated */
253         wr32(hw, NGBE_IENMISC, 0);
254         wr32(hw, NGBE_IMS(0), NGBE_IMS_MASK);
255
256         /* Clear any pending interrupts, flush previous writes */
257         wr32(hw, NGBE_ICRMISC, NGBE_ICRMISC_MASK);
258         wr32(hw, NGBE_ICR(0), NGBE_ICR_MASK);
259
260         /* Disable the transmit unit.  Each queue must be disabled. */
261         for (i = 0; i < hw->mac.max_tx_queues; i++)
262                 wr32(hw, NGBE_TXCFG(i), NGBE_TXCFG_FLUSH);
263
264         /* Disable the receive unit by stopping each queue */
265         for (i = 0; i < hw->mac.max_rx_queues; i++) {
266                 reg_val = rd32(hw, NGBE_RXCFG(i));
267                 reg_val &= ~NGBE_RXCFG_ENA;
268                 wr32(hw, NGBE_RXCFG(i), reg_val);
269         }
270
271         /* flush all queues disables */
272         ngbe_flush(hw);
273         msec_delay(2);
274
275         return 0;
276 }
277
278 /**
279  *  ngbe_validate_mac_addr - Validate MAC address
280  *  @mac_addr: pointer to MAC address.
281  *
282  *  Tests a MAC address to ensure it is a valid Individual Address.
283  **/
284 s32 ngbe_validate_mac_addr(u8 *mac_addr)
285 {
286         s32 status = 0;
287
288         DEBUGFUNC("ngbe_validate_mac_addr");
289
290         /* Make sure it is not a multicast address */
291         if (NGBE_IS_MULTICAST((struct rte_ether_addr *)mac_addr)) {
292                 status = NGBE_ERR_INVALID_MAC_ADDR;
293         /* Not a broadcast address */
294         } else if (NGBE_IS_BROADCAST((struct rte_ether_addr *)mac_addr)) {
295                 status = NGBE_ERR_INVALID_MAC_ADDR;
296         /* Reject the zero address */
297         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
298                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
299                 status = NGBE_ERR_INVALID_MAC_ADDR;
300         }
301         return status;
302 }
303
304 /**
305  *  ngbe_set_rar - Set Rx address register
306  *  @hw: pointer to hardware structure
307  *  @index: Receive address register to write
308  *  @addr: Address to put into receive address register
309  *  @vmdq: VMDq "set" or "pool" index
310  *  @enable_addr: set flag that address is active
311  *
312  *  Puts an ethernet address into a receive address register.
313  **/
314 s32 ngbe_set_rar(struct ngbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
315                           u32 enable_addr)
316 {
317         u32 rar_low, rar_high;
318         u32 rar_entries = hw->mac.num_rar_entries;
319
320         DEBUGFUNC("ngbe_set_rar");
321
322         /* Make sure we are using a valid rar index range */
323         if (index >= rar_entries) {
324                 DEBUGOUT("RAR index %d is out of range.\n", index);
325                 return NGBE_ERR_INVALID_ARGUMENT;
326         }
327
328         /* setup VMDq pool selection before this RAR gets enabled */
329         hw->mac.set_vmdq(hw, index, vmdq);
330
331         /*
332          * HW expects these in little endian so we reverse the byte
333          * order from network order (big endian) to little endian
334          */
335         rar_low = NGBE_ETHADDRL_AD0(addr[5]) |
336                   NGBE_ETHADDRL_AD1(addr[4]) |
337                   NGBE_ETHADDRL_AD2(addr[3]) |
338                   NGBE_ETHADDRL_AD3(addr[2]);
339         /*
340          * Some parts put the VMDq setting in the extra RAH bits,
341          * so save everything except the lower 16 bits that hold part
342          * of the address and the address valid bit.
343          */
344         rar_high = rd32(hw, NGBE_ETHADDRH);
345         rar_high &= ~NGBE_ETHADDRH_AD_MASK;
346         rar_high |= (NGBE_ETHADDRH_AD4(addr[1]) |
347                      NGBE_ETHADDRH_AD5(addr[0]));
348
349         rar_high &= ~NGBE_ETHADDRH_VLD;
350         if (enable_addr != 0)
351                 rar_high |= NGBE_ETHADDRH_VLD;
352
353         wr32(hw, NGBE_ETHADDRIDX, index);
354         wr32(hw, NGBE_ETHADDRL, rar_low);
355         wr32(hw, NGBE_ETHADDRH, rar_high);
356
357         return 0;
358 }
359
360 /**
361  *  ngbe_clear_rar - Remove Rx address register
362  *  @hw: pointer to hardware structure
363  *  @index: Receive address register to write
364  *
365  *  Clears an ethernet address from a receive address register.
366  **/
367 s32 ngbe_clear_rar(struct ngbe_hw *hw, u32 index)
368 {
369         u32 rar_high;
370         u32 rar_entries = hw->mac.num_rar_entries;
371
372         DEBUGFUNC("ngbe_clear_rar");
373
374         /* Make sure we are using a valid rar index range */
375         if (index >= rar_entries) {
376                 DEBUGOUT("RAR index %d is out of range.\n", index);
377                 return NGBE_ERR_INVALID_ARGUMENT;
378         }
379
380         /*
381          * Some parts put the VMDq setting in the extra RAH bits,
382          * so save everything except the lower 16 bits that hold part
383          * of the address and the address valid bit.
384          */
385         wr32(hw, NGBE_ETHADDRIDX, index);
386         rar_high = rd32(hw, NGBE_ETHADDRH);
387         rar_high &= ~(NGBE_ETHADDRH_AD_MASK | NGBE_ETHADDRH_VLD);
388
389         wr32(hw, NGBE_ETHADDRL, 0);
390         wr32(hw, NGBE_ETHADDRH, rar_high);
391
392         /* clear VMDq pool/queue selection for this RAR */
393         hw->mac.clear_vmdq(hw, index, BIT_MASK32);
394
395         return 0;
396 }
397
398 /**
399  *  ngbe_init_rx_addrs - Initializes receive address filters.
400  *  @hw: pointer to hardware structure
401  *
402  *  Places the MAC address in receive address register 0 and clears the rest
403  *  of the receive address registers. Clears the multicast table. Assumes
404  *  the receiver is in reset when the routine is called.
405  **/
406 s32 ngbe_init_rx_addrs(struct ngbe_hw *hw)
407 {
408         u32 i;
409         u32 psrctl;
410         u32 rar_entries = hw->mac.num_rar_entries;
411
412         DEBUGFUNC("ngbe_init_rx_addrs");
413
414         /*
415          * If the current mac address is valid, assume it is a software override
416          * to the permanent address.
417          * Otherwise, use the permanent address from the eeprom.
418          */
419         if (ngbe_validate_mac_addr(hw->mac.addr) ==
420             NGBE_ERR_INVALID_MAC_ADDR) {
421                 /* Get the MAC address from the RAR0 for later reference */
422                 hw->mac.get_mac_addr(hw, hw->mac.addr);
423
424                 DEBUGOUT(" Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
425                           hw->mac.addr[0], hw->mac.addr[1],
426                           hw->mac.addr[2]);
427                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
428                           hw->mac.addr[4], hw->mac.addr[5]);
429         } else {
430                 /* Setup the receive address. */
431                 DEBUGOUT("Overriding MAC Address in RAR[0]\n");
432                 DEBUGOUT(" New MAC Addr =%.2X %.2X %.2X ",
433                           hw->mac.addr[0], hw->mac.addr[1],
434                           hw->mac.addr[2]);
435                 DEBUGOUT("%.2X %.2X %.2X\n", hw->mac.addr[3],
436                           hw->mac.addr[4], hw->mac.addr[5]);
437
438                 hw->mac.set_rar(hw, 0, hw->mac.addr, 0, true);
439         }
440
441         /* clear VMDq pool/queue selection for RAR 0 */
442         hw->mac.clear_vmdq(hw, 0, BIT_MASK32);
443
444         /* Zero out the other receive addresses. */
445         DEBUGOUT("Clearing RAR[1-%d]\n", rar_entries - 1);
446         for (i = 1; i < rar_entries; i++) {
447                 wr32(hw, NGBE_ETHADDRIDX, i);
448                 wr32(hw, NGBE_ETHADDRL, 0);
449                 wr32(hw, NGBE_ETHADDRH, 0);
450         }
451
452         /* Clear the MTA */
453         hw->addr_ctrl.mta_in_use = 0;
454         psrctl = rd32(hw, NGBE_PSRCTL);
455         psrctl &= ~(NGBE_PSRCTL_ADHF12_MASK | NGBE_PSRCTL_MCHFENA);
456         psrctl |= NGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
457         wr32(hw, NGBE_PSRCTL, psrctl);
458
459         DEBUGOUT(" Clearing MTA\n");
460         for (i = 0; i < hw->mac.mcft_size; i++)
461                 wr32(hw, NGBE_MCADDRTBL(i), 0);
462
463         ngbe_init_uta_tables(hw);
464
465         return 0;
466 }
467
468 /**
469  *  ngbe_acquire_swfw_sync - Acquire SWFW semaphore
470  *  @hw: pointer to hardware structure
471  *  @mask: Mask to specify which semaphore to acquire
472  *
473  *  Acquires the SWFW semaphore through the MNGSEM register for the specified
474  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
475  **/
476 s32 ngbe_acquire_swfw_sync(struct ngbe_hw *hw, u32 mask)
477 {
478         u32 mngsem = 0;
479         u32 swmask = NGBE_MNGSEM_SW(mask);
480         u32 fwmask = NGBE_MNGSEM_FW(mask);
481         u32 timeout = 200;
482         u32 i;
483
484         DEBUGFUNC("ngbe_acquire_swfw_sync");
485
486         for (i = 0; i < timeout; i++) {
487                 /*
488                  * SW NVM semaphore bit is used for access to all
489                  * SW_FW_SYNC bits (not just NVM)
490                  */
491                 if (ngbe_get_eeprom_semaphore(hw))
492                         return NGBE_ERR_SWFW_SYNC;
493
494                 mngsem = rd32(hw, NGBE_MNGSEM);
495                 if (mngsem & (fwmask | swmask)) {
496                         /* Resource is currently in use by FW or SW */
497                         ngbe_release_eeprom_semaphore(hw);
498                         msec_delay(5);
499                 } else {
500                         mngsem |= swmask;
501                         wr32(hw, NGBE_MNGSEM, mngsem);
502                         ngbe_release_eeprom_semaphore(hw);
503                         return 0;
504                 }
505         }
506
507         /* If time expired clear the bits holding the lock and retry */
508         if (mngsem & (fwmask | swmask))
509                 ngbe_release_swfw_sync(hw, mngsem & (fwmask | swmask));
510
511         msec_delay(5);
512         return NGBE_ERR_SWFW_SYNC;
513 }
514
515 /**
516  *  ngbe_release_swfw_sync - Release SWFW semaphore
517  *  @hw: pointer to hardware structure
518  *  @mask: Mask to specify which semaphore to release
519  *
520  *  Releases the SWFW semaphore through the MNGSEM register for the specified
521  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
522  **/
523 void ngbe_release_swfw_sync(struct ngbe_hw *hw, u32 mask)
524 {
525         u32 mngsem;
526         u32 swmask = mask;
527
528         DEBUGFUNC("ngbe_release_swfw_sync");
529
530         ngbe_get_eeprom_semaphore(hw);
531
532         mngsem = rd32(hw, NGBE_MNGSEM);
533         mngsem &= ~swmask;
534         wr32(hw, NGBE_MNGSEM, mngsem);
535
536         ngbe_release_eeprom_semaphore(hw);
537 }
538
539 /**
540  *  ngbe_clear_vmdq - Disassociate a VMDq pool index from a rx address
541  *  @hw: pointer to hardware struct
542  *  @rar: receive address register index to disassociate
543  *  @vmdq: VMDq pool index to remove from the rar
544  **/
545 s32 ngbe_clear_vmdq(struct ngbe_hw *hw, u32 rar, u32 vmdq)
546 {
547         u32 mpsar;
548         u32 rar_entries = hw->mac.num_rar_entries;
549
550         DEBUGFUNC("ngbe_clear_vmdq");
551
552         /* Make sure we are using a valid rar index range */
553         if (rar >= rar_entries) {
554                 DEBUGOUT("RAR index %d is out of range.\n", rar);
555                 return NGBE_ERR_INVALID_ARGUMENT;
556         }
557
558         wr32(hw, NGBE_ETHADDRIDX, rar);
559         mpsar = rd32(hw, NGBE_ETHADDRASS);
560
561         if (NGBE_REMOVED(hw->hw_addr))
562                 goto done;
563
564         if (!mpsar)
565                 goto done;
566
567         mpsar &= ~(1 << vmdq);
568         wr32(hw, NGBE_ETHADDRASS, mpsar);
569
570         /* was that the last pool using this rar? */
571         if (mpsar == 0 && rar != 0)
572                 hw->mac.clear_rar(hw, rar);
573 done:
574         return 0;
575 }
576
577 /**
578  *  ngbe_set_vmdq - Associate a VMDq pool index with a rx address
579  *  @hw: pointer to hardware struct
580  *  @rar: receive address register index to associate with a VMDq index
581  *  @vmdq: VMDq pool index
582  **/
583 s32 ngbe_set_vmdq(struct ngbe_hw *hw, u32 rar, u32 vmdq)
584 {
585         u32 mpsar;
586         u32 rar_entries = hw->mac.num_rar_entries;
587
588         DEBUGFUNC("ngbe_set_vmdq");
589
590         /* Make sure we are using a valid rar index range */
591         if (rar >= rar_entries) {
592                 DEBUGOUT("RAR index %d is out of range.\n", rar);
593                 return NGBE_ERR_INVALID_ARGUMENT;
594         }
595
596         wr32(hw, NGBE_ETHADDRIDX, rar);
597
598         mpsar = rd32(hw, NGBE_ETHADDRASS);
599         mpsar |= 1 << vmdq;
600         wr32(hw, NGBE_ETHADDRASS, mpsar);
601
602         return 0;
603 }
604
605 /**
606  *  ngbe_init_uta_tables - Initialize the Unicast Table Array
607  *  @hw: pointer to hardware structure
608  **/
609 s32 ngbe_init_uta_tables(struct ngbe_hw *hw)
610 {
611         int i;
612
613         DEBUGFUNC("ngbe_init_uta_tables");
614         DEBUGOUT(" Clearing UTA\n");
615
616         for (i = 0; i < 128; i++)
617                 wr32(hw, NGBE_UCADDRTBL(i), 0);
618
619         return 0;
620 }
621
622 /**
623  *  ngbe_check_mac_link_em - Determine link and speed status
624  *  @hw: pointer to hardware structure
625  *  @speed: pointer to link speed
626  *  @link_up: true when link is up
627  *  @link_up_wait_to_complete: bool used to wait for link up or not
628  *
629  *  Reads the links register to determine if link is up and the current speed
630  **/
631 s32 ngbe_check_mac_link_em(struct ngbe_hw *hw, u32 *speed,
632                         bool *link_up, bool link_up_wait_to_complete)
633 {
634         u32 i, reg;
635         s32 status = 0;
636
637         DEBUGFUNC("ngbe_check_mac_link_em");
638
639         reg = rd32(hw, NGBE_GPIOINTSTAT);
640         wr32(hw, NGBE_GPIOEOI, reg);
641
642         if (link_up_wait_to_complete) {
643                 for (i = 0; i < hw->mac.max_link_up_time; i++) {
644                         status = hw->phy.check_link(hw, speed, link_up);
645                         if (*link_up)
646                                 break;
647                         msec_delay(100);
648                 }
649         } else {
650                 status = hw->phy.check_link(hw, speed, link_up);
651         }
652
653         return status;
654 }
655
656 s32 ngbe_get_link_capabilities_em(struct ngbe_hw *hw,
657                                       u32 *speed,
658                                       bool *autoneg)
659 {
660         s32 status = 0;
661
662         DEBUGFUNC("\n");
663
664         hw->mac.autoneg = *autoneg;
665
666         switch (hw->sub_device_id) {
667         case NGBE_SUB_DEV_ID_EM_RTL_SGMII:
668                 *speed = NGBE_LINK_SPEED_1GB_FULL |
669                         NGBE_LINK_SPEED_100M_FULL |
670                         NGBE_LINK_SPEED_10M_FULL;
671                 break;
672         default:
673                 break;
674         }
675
676         return status;
677 }
678
679 s32 ngbe_setup_mac_link_em(struct ngbe_hw *hw,
680                                u32 speed,
681                                bool autoneg_wait_to_complete)
682 {
683         s32 status;
684
685         DEBUGFUNC("\n");
686
687         /* Setup the PHY according to input speed */
688         status = hw->phy.setup_link(hw, speed, autoneg_wait_to_complete);
689
690         return status;
691 }
692
693 /**
694  *  ngbe_init_thermal_sensor_thresh - Inits thermal sensor thresholds
695  *  @hw: pointer to hardware structure
696  *
697  *  Inits the thermal sensor thresholds according to the NVM map
698  *  and save off the threshold and location values into mac.thermal_sensor_data
699  **/
700 s32 ngbe_init_thermal_sensor_thresh(struct ngbe_hw *hw)
701 {
702         struct ngbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
703
704         DEBUGFUNC("ngbe_init_thermal_sensor_thresh");
705
706         memset(data, 0, sizeof(struct ngbe_thermal_sensor_data));
707
708         if (hw->bus.lan_id != 0)
709                 return NGBE_NOT_IMPLEMENTED;
710
711         wr32(hw, NGBE_TSINTR,
712                 NGBE_TSINTR_AEN | NGBE_TSINTR_DEN);
713         wr32(hw, NGBE_TSEN, NGBE_TSEN_ENA);
714
715
716         data->sensor[0].alarm_thresh = 115;
717         wr32(hw, NGBE_TSATHRE, 0x344);
718         data->sensor[0].dalarm_thresh = 110;
719         wr32(hw, NGBE_TSDTHRE, 0x330);
720
721         return 0;
722 }
723
724 s32 ngbe_mac_check_overtemp(struct ngbe_hw *hw)
725 {
726         s32 status = 0;
727         u32 ts_state;
728
729         DEBUGFUNC("ngbe_mac_check_overtemp");
730
731         /* Check that the LASI temp alarm status was triggered */
732         ts_state = rd32(hw, NGBE_TSALM);
733
734         if (ts_state & NGBE_TSALM_HI)
735                 status = NGBE_ERR_UNDERTEMP;
736         else if (ts_state & NGBE_TSALM_LO)
737                 status = NGBE_ERR_OVERTEMP;
738
739         return status;
740 }
741
742 void ngbe_disable_rx(struct ngbe_hw *hw)
743 {
744         u32 pfdtxgswc;
745
746         pfdtxgswc = rd32(hw, NGBE_PSRCTL);
747         if (pfdtxgswc & NGBE_PSRCTL_LBENA) {
748                 pfdtxgswc &= ~NGBE_PSRCTL_LBENA;
749                 wr32(hw, NGBE_PSRCTL, pfdtxgswc);
750                 hw->mac.set_lben = true;
751         } else {
752                 hw->mac.set_lben = false;
753         }
754
755         wr32m(hw, NGBE_PBRXCTL, NGBE_PBRXCTL_ENA, 0);
756         wr32m(hw, NGBE_MACRXCFG, NGBE_MACRXCFG_ENA, 0);
757 }
758
759 /**
760  *  ngbe_set_mac_type - Sets MAC type
761  *  @hw: pointer to the HW structure
762  *
763  *  This function sets the mac type of the adapter based on the
764  *  vendor ID and device ID stored in the hw structure.
765  **/
766 s32 ngbe_set_mac_type(struct ngbe_hw *hw)
767 {
768         s32 err = 0;
769
770         DEBUGFUNC("ngbe_set_mac_type");
771
772         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
773                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
774                 return NGBE_ERR_DEVICE_NOT_SUPPORTED;
775         }
776
777         switch (hw->sub_device_id) {
778         case NGBE_SUB_DEV_ID_EM_RTL_SGMII:
779         case NGBE_SUB_DEV_ID_EM_MVL_RGMII:
780                 hw->phy.media_type = ngbe_media_type_copper;
781                 hw->mac.type = ngbe_mac_em;
782                 break;
783         case NGBE_SUB_DEV_ID_EM_MVL_SFP:
784         case NGBE_SUB_DEV_ID_EM_YT8521S_SFP:
785                 hw->phy.media_type = ngbe_media_type_fiber;
786                 hw->mac.type = ngbe_mac_em;
787                 break;
788         case NGBE_SUB_DEV_ID_EM_VF:
789                 hw->phy.media_type = ngbe_media_type_virtual;
790                 hw->mac.type = ngbe_mac_em_vf;
791                 break;
792         default:
793                 err = NGBE_ERR_DEVICE_NOT_SUPPORTED;
794                 hw->phy.media_type = ngbe_media_type_unknown;
795                 hw->mac.type = ngbe_mac_unknown;
796                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
797                 break;
798         }
799
800         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
801                   hw->mac.type, hw->phy.media_type, err);
802         return err;
803 }
804
805 void ngbe_map_device_id(struct ngbe_hw *hw)
806 {
807         u16 oem = hw->sub_system_id & NGBE_OEM_MASK;
808         u16 internal = hw->sub_system_id & NGBE_INTERNAL_MASK;
809         hw->is_pf = true;
810
811         /* move subsystem_device_id to device_id */
812         switch (hw->device_id) {
813         case NGBE_DEV_ID_EM_WX1860AL_W_VF:
814         case NGBE_DEV_ID_EM_WX1860A2_VF:
815         case NGBE_DEV_ID_EM_WX1860A2S_VF:
816         case NGBE_DEV_ID_EM_WX1860A4_VF:
817         case NGBE_DEV_ID_EM_WX1860A4S_VF:
818         case NGBE_DEV_ID_EM_WX1860AL2_VF:
819         case NGBE_DEV_ID_EM_WX1860AL2S_VF:
820         case NGBE_DEV_ID_EM_WX1860AL4_VF:
821         case NGBE_DEV_ID_EM_WX1860AL4S_VF:
822         case NGBE_DEV_ID_EM_WX1860NCSI_VF:
823         case NGBE_DEV_ID_EM_WX1860A1_VF:
824         case NGBE_DEV_ID_EM_WX1860A1L_VF:
825                 hw->device_id = NGBE_DEV_ID_EM_VF;
826                 hw->sub_device_id = NGBE_SUB_DEV_ID_EM_VF;
827                 hw->is_pf = false;
828                 break;
829         case NGBE_DEV_ID_EM_WX1860AL_W:
830         case NGBE_DEV_ID_EM_WX1860A2:
831         case NGBE_DEV_ID_EM_WX1860A2S:
832         case NGBE_DEV_ID_EM_WX1860A4:
833         case NGBE_DEV_ID_EM_WX1860A4S:
834         case NGBE_DEV_ID_EM_WX1860AL2:
835         case NGBE_DEV_ID_EM_WX1860AL2S:
836         case NGBE_DEV_ID_EM_WX1860AL4:
837         case NGBE_DEV_ID_EM_WX1860AL4S:
838         case NGBE_DEV_ID_EM_WX1860NCSI:
839         case NGBE_DEV_ID_EM_WX1860A1:
840         case NGBE_DEV_ID_EM_WX1860A1L:
841                 hw->device_id = NGBE_DEV_ID_EM;
842                 if (oem == NGBE_LY_M88E1512_SFP ||
843                                 internal == NGBE_INTERNAL_SFP)
844                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_MVL_SFP;
845                 else if (hw->sub_system_id == NGBE_SUB_DEV_ID_EM_M88E1512_RJ45)
846                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_MVL_RGMII;
847                 else if (oem == NGBE_YT8521S_SFP ||
848                                 oem == NGBE_LY_YT8521S_SFP)
849                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_YT8521S_SFP;
850                 else
851                         hw->sub_device_id = NGBE_SUB_DEV_ID_EM_RTL_SGMII;
852                 break;
853         default:
854                 break;
855         }
856 }
857
858 /**
859  *  ngbe_init_ops_pf - Inits func ptrs and MAC type
860  *  @hw: pointer to hardware structure
861  *
862  *  Initialize the function pointers and assign the MAC type.
863  *  Does not touch the hardware.
864  **/
865 s32 ngbe_init_ops_pf(struct ngbe_hw *hw)
866 {
867         struct ngbe_bus_info *bus = &hw->bus;
868         struct ngbe_mac_info *mac = &hw->mac;
869         struct ngbe_phy_info *phy = &hw->phy;
870         struct ngbe_rom_info *rom = &hw->rom;
871
872         DEBUGFUNC("ngbe_init_ops_pf");
873
874         /* BUS */
875         bus->set_lan_id = ngbe_set_lan_id_multi_port;
876
877         /* PHY */
878         phy->identify = ngbe_identify_phy;
879         phy->read_reg = ngbe_read_phy_reg;
880         phy->write_reg = ngbe_write_phy_reg;
881         phy->read_reg_unlocked = ngbe_read_phy_reg_mdi;
882         phy->write_reg_unlocked = ngbe_write_phy_reg_mdi;
883         phy->reset_hw = ngbe_reset_phy;
884
885         /* MAC */
886         mac->init_hw = ngbe_init_hw;
887         mac->reset_hw = ngbe_reset_hw_em;
888         mac->start_hw = ngbe_start_hw;
889         mac->get_mac_addr = ngbe_get_mac_addr;
890         mac->stop_hw = ngbe_stop_hw;
891         mac->acquire_swfw_sync = ngbe_acquire_swfw_sync;
892         mac->release_swfw_sync = ngbe_release_swfw_sync;
893
894         /* RAR */
895         mac->set_rar = ngbe_set_rar;
896         mac->clear_rar = ngbe_clear_rar;
897         mac->init_rx_addrs = ngbe_init_rx_addrs;
898         mac->set_vmdq = ngbe_set_vmdq;
899         mac->clear_vmdq = ngbe_clear_vmdq;
900
901         /* Link */
902         mac->get_link_capabilities = ngbe_get_link_capabilities_em;
903         mac->check_link = ngbe_check_mac_link_em;
904         mac->setup_link = ngbe_setup_mac_link_em;
905
906         /* Manageability interface */
907         mac->init_thermal_sensor_thresh = ngbe_init_thermal_sensor_thresh;
908         mac->check_overtemp = ngbe_mac_check_overtemp;
909
910         /* EEPROM */
911         rom->init_params = ngbe_init_eeprom_params;
912         rom->validate_checksum = ngbe_validate_eeprom_checksum_em;
913
914         mac->mcft_size          = NGBE_EM_MC_TBL_SIZE;
915         mac->num_rar_entries    = NGBE_EM_RAR_ENTRIES;
916         mac->max_rx_queues      = NGBE_EM_MAX_RX_QUEUES;
917         mac->max_tx_queues      = NGBE_EM_MAX_TX_QUEUES;
918
919         mac->default_speeds = NGBE_LINK_SPEED_10M_FULL |
920                                 NGBE_LINK_SPEED_100M_FULL |
921                                 NGBE_LINK_SPEED_1GB_FULL;
922
923         return 0;
924 }
925
926 /**
927  *  ngbe_init_shared_code - Initialize the shared code
928  *  @hw: pointer to hardware structure
929  *
930  *  This will assign function pointers and assign the MAC type and PHY code.
931  *  Does not touch the hardware. This function must be called prior to any
932  *  other function in the shared code. The ngbe_hw structure should be
933  *  memset to 0 prior to calling this function.  The following fields in
934  *  hw structure should be filled in prior to calling this function:
935  *  hw_addr, back, device_id, vendor_id, subsystem_device_id
936  **/
937 s32 ngbe_init_shared_code(struct ngbe_hw *hw)
938 {
939         s32 status = 0;
940
941         DEBUGFUNC("ngbe_init_shared_code");
942
943         /*
944          * Set the mac type
945          */
946         ngbe_set_mac_type(hw);
947
948         ngbe_init_ops_dummy(hw);
949         switch (hw->mac.type) {
950         case ngbe_mac_em:
951                 ngbe_init_ops_pf(hw);
952                 break;
953         default:
954                 status = NGBE_ERR_DEVICE_NOT_SUPPORTED;
955                 break;
956         }
957         hw->mac.max_link_up_time = NGBE_LINK_UP_TIME;
958
959         hw->bus.set_lan_id(hw);
960
961         return status;
962 }
963