net/txgbe: add interrupt operation
[dpdk.git] / drivers / net / txgbe / base / txgbe_hw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include "txgbe_type.h"
6 #include "txgbe_phy.h"
7 #include "txgbe_eeprom.h"
8 #include "txgbe_mng.h"
9 #include "txgbe_hw.h"
10
11 #define TXGBE_RAPTOR_MAX_TX_QUEUES 128
12 #define TXGBE_RAPTOR_MAX_RX_QUEUES 128
13 #define TXGBE_RAPTOR_RAR_ENTRIES   128
14
15 /**
16  *  txgbe_init_hw - Generic hardware initialization
17  *  @hw: pointer to hardware structure
18  *
19  *  Initialize the hardware by resetting the hardware, filling the bus info
20  *  structure and media type, clears all on chip counters, initializes receive
21  *  address registers, multicast table, VLAN filter table, calls routine to set
22  *  up link and flow control settings, and leaves transmit and receive units
23  *  disabled and uninitialized
24  **/
25 s32 txgbe_init_hw(struct txgbe_hw *hw)
26 {
27         s32 status;
28
29         DEBUGFUNC("txgbe_init_hw");
30
31         /* Reset the hardware */
32         status = hw->mac.reset_hw(hw);
33         if (status == 0 || status == TXGBE_ERR_SFP_NOT_PRESENT) {
34                 /* Start the HW */
35                 status = hw->mac.start_hw(hw);
36         }
37
38         if (status != 0)
39                 DEBUGOUT("Failed to initialize HW, STATUS = %d\n", status);
40
41         return status;
42 }
43
44
45 /**
46  *  txgbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
47  *  @hw: pointer to the HW structure
48  *
49  *  Determines the LAN function id by reading memory-mapped registers and swaps
50  *  the port value if requested, and set MAC instance for devices.
51  **/
52 void txgbe_set_lan_id_multi_port(struct txgbe_hw *hw)
53 {
54         struct txgbe_bus_info *bus = &hw->bus;
55         u32 reg;
56
57         DEBUGFUNC("txgbe_set_lan_id_multi_port_pcie");
58
59         reg = rd32(hw, TXGBE_PORTSTAT);
60         bus->lan_id = TXGBE_PORTSTAT_ID(reg);
61
62         /* check for single port */
63         reg = rd32(hw, TXGBE_PWR);
64         if (TXGBE_PWR_LANID(reg) == TXGBE_PWR_LANID_SWAP)
65                 bus->func = 0;
66         else
67                 bus->func = bus->lan_id;
68 }
69
70 /**
71  *  txgbe_validate_mac_addr - Validate MAC address
72  *  @mac_addr: pointer to MAC address.
73  *
74  *  Tests a MAC address to ensure it is a valid Individual Address.
75  **/
76 s32 txgbe_validate_mac_addr(u8 *mac_addr)
77 {
78         s32 status = 0;
79
80         DEBUGFUNC("txgbe_validate_mac_addr");
81
82         /* Make sure it is not a multicast address */
83         if (TXGBE_IS_MULTICAST(mac_addr)) {
84                 status = TXGBE_ERR_INVALID_MAC_ADDR;
85         /* Not a broadcast address */
86         } else if (TXGBE_IS_BROADCAST(mac_addr)) {
87                 status = TXGBE_ERR_INVALID_MAC_ADDR;
88         /* Reject the zero address */
89         } else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
90                    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
91                 status = TXGBE_ERR_INVALID_MAC_ADDR;
92         }
93         return status;
94 }
95
96 /**
97  * txgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
98  * @hw: pointer to the hardware structure
99  *
100  * The MACs can experience issues if TX work is still pending
101  * when a reset occurs.  This function prevents this by flushing the PCIe
102  * buffers on the system.
103  **/
104 void txgbe_clear_tx_pending(struct txgbe_hw *hw)
105 {
106         u32 hlreg0, i, poll;
107
108         /*
109          * If double reset is not requested then all transactions should
110          * already be clear and as such there is no work to do
111          */
112         if (!(hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
113                 return;
114
115         hlreg0 = rd32(hw, TXGBE_PSRCTL);
116         wr32(hw, TXGBE_PSRCTL, hlreg0 | TXGBE_PSRCTL_LBENA);
117
118         /* Wait for a last completion before clearing buffers */
119         txgbe_flush(hw);
120         msec_delay(3);
121
122         /*
123          * Before proceeding, make sure that the PCIe block does not have
124          * transactions pending.
125          */
126         poll = (800 * 11) / 10;
127         for (i = 0; i < poll; i++)
128                 usec_delay(100);
129
130         /* Flush all writes and allow 20usec for all transactions to clear */
131         txgbe_flush(hw);
132         usec_delay(20);
133
134         /* restore previous register values */
135         wr32(hw, TXGBE_PSRCTL, hlreg0);
136 }
137
138 /**
139  *  txgbe_init_shared_code - Initialize the shared code
140  *  @hw: pointer to hardware structure
141  *
142  *  This will assign function pointers and assign the MAC type and PHY code.
143  *  Does not touch the hardware. This function must be called prior to any
144  *  other function in the shared code. The txgbe_hw structure should be
145  *  memset to 0 prior to calling this function.  The following fields in
146  *  hw structure should be filled in prior to calling this function:
147  *  hw_addr, back, device_id, vendor_id, subsystem_device_id,
148  *  subsystem_vendor_id, and revision_id
149  **/
150 s32 txgbe_init_shared_code(struct txgbe_hw *hw)
151 {
152         s32 status;
153
154         DEBUGFUNC("txgbe_init_shared_code");
155
156         /*
157          * Set the mac type
158          */
159         txgbe_set_mac_type(hw);
160
161         txgbe_init_ops_dummy(hw);
162         switch (hw->mac.type) {
163         case txgbe_mac_raptor:
164                 status = txgbe_init_ops_pf(hw);
165                 break;
166         default:
167                 status = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
168                 break;
169         }
170         hw->mac.max_link_up_time = TXGBE_LINK_UP_TIME;
171
172         hw->bus.set_lan_id(hw);
173
174         return status;
175 }
176
177 /**
178  *  txgbe_set_mac_type - Sets MAC type
179  *  @hw: pointer to the HW structure
180  *
181  *  This function sets the mac type of the adapter based on the
182  *  vendor ID and device ID stored in the hw structure.
183  **/
184 s32 txgbe_set_mac_type(struct txgbe_hw *hw)
185 {
186         s32 err = 0;
187
188         DEBUGFUNC("txgbe_set_mac_type");
189
190         if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
191                 DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
192                 return TXGBE_ERR_DEVICE_NOT_SUPPORTED;
193         }
194
195         switch (hw->device_id) {
196         case TXGBE_DEV_ID_RAPTOR_KR_KX_KX4:
197                 hw->phy.media_type = txgbe_media_type_backplane;
198                 hw->mac.type = txgbe_mac_raptor;
199                 break;
200         case TXGBE_DEV_ID_RAPTOR_XAUI:
201         case TXGBE_DEV_ID_RAPTOR_SGMII:
202                 hw->phy.media_type = txgbe_media_type_copper;
203                 hw->mac.type = txgbe_mac_raptor;
204                 break;
205         case TXGBE_DEV_ID_RAPTOR_SFP:
206         case TXGBE_DEV_ID_WX1820_SFP:
207                 hw->phy.media_type = txgbe_media_type_fiber;
208                 hw->mac.type = txgbe_mac_raptor;
209                 break;
210         case TXGBE_DEV_ID_RAPTOR_QSFP:
211                 hw->phy.media_type = txgbe_media_type_fiber_qsfp;
212                 hw->mac.type = txgbe_mac_raptor;
213                 break;
214         case TXGBE_DEV_ID_RAPTOR_VF:
215         case TXGBE_DEV_ID_RAPTOR_VF_HV:
216                 hw->phy.media_type = txgbe_media_type_virtual;
217                 hw->mac.type = txgbe_mac_raptor_vf;
218                 break;
219         default:
220                 err = TXGBE_ERR_DEVICE_NOT_SUPPORTED;
221                 DEBUGOUT("Unsupported device id: %x", hw->device_id);
222                 break;
223         }
224
225         DEBUGOUT("found mac: %d media: %d, returns: %d\n",
226                   hw->mac.type, hw->phy.media_type, err);
227         return err;
228 }
229
230 void txgbe_init_mac_link_ops(struct txgbe_hw *hw)
231 {
232         struct txgbe_mac_info *mac = &hw->mac;
233
234         DEBUGFUNC("txgbe_init_mac_link_ops");
235         RTE_SET_USED(mac);
236 }
237
238 /**
239  *  txgbe_init_phy_raptor - PHY/SFP specific init
240  *  @hw: pointer to hardware structure
241  *
242  *  Initialize any function pointers that were not able to be
243  *  set during init_shared_code because the PHY/SFP type was
244  *  not known.  Perform the SFP init if necessary.
245  *
246  **/
247 s32 txgbe_init_phy_raptor(struct txgbe_hw *hw)
248 {
249         struct txgbe_phy_info *phy = &hw->phy;
250         s32 err = 0;
251
252         DEBUGFUNC("txgbe_init_phy_raptor");
253
254         if (hw->device_id == TXGBE_DEV_ID_RAPTOR_QSFP) {
255                 /* Store flag indicating I2C bus access control unit. */
256                 hw->phy.qsfp_shared_i2c_bus = TRUE;
257
258                 /* Initialize access to QSFP+ I2C bus */
259                 txgbe_flush(hw);
260         }
261
262         /* Identify the PHY or SFP module */
263         err = phy->identify(hw);
264         if (err == TXGBE_ERR_SFP_NOT_SUPPORTED)
265                 goto init_phy_ops_out;
266
267         /* Setup function pointers based on detected SFP module and speeds */
268         txgbe_init_mac_link_ops(hw);
269
270 init_phy_ops_out:
271         return err;
272 }
273
274 /**
275  *  txgbe_init_ops_pf - Inits func ptrs and MAC type
276  *  @hw: pointer to hardware structure
277  *
278  *  Initialize the function pointers and assign the MAC type.
279  *  Does not touch the hardware.
280  **/
281 s32 txgbe_init_ops_pf(struct txgbe_hw *hw)
282 {
283         struct txgbe_bus_info *bus = &hw->bus;
284         struct txgbe_mac_info *mac = &hw->mac;
285         struct txgbe_phy_info *phy = &hw->phy;
286         struct txgbe_rom_info *rom = &hw->rom;
287
288         DEBUGFUNC("txgbe_init_ops_pf");
289
290         /* BUS */
291         bus->set_lan_id = txgbe_set_lan_id_multi_port;
292
293         /* PHY */
294         phy->identify = txgbe_identify_phy;
295         phy->init = txgbe_init_phy_raptor;
296         phy->read_reg = txgbe_read_phy_reg;
297         phy->write_reg = txgbe_write_phy_reg;
298         phy->read_reg_mdi = txgbe_read_phy_reg_mdi;
299         phy->write_reg_mdi = txgbe_write_phy_reg_mdi;
300         phy->read_i2c_byte = txgbe_read_i2c_byte;
301         phy->write_i2c_byte = txgbe_write_i2c_byte;
302         phy->read_i2c_eeprom = txgbe_read_i2c_eeprom;
303         phy->write_i2c_eeprom = txgbe_write_i2c_eeprom;
304         phy->reset = txgbe_reset_phy;
305
306         /* MAC */
307         mac->init_hw = txgbe_init_hw;
308         mac->reset_hw = txgbe_reset_hw;
309
310         /* EEPROM */
311         rom->init_params = txgbe_init_eeprom_params;
312         rom->read16 = txgbe_ee_read16;
313         rom->readw_buffer = txgbe_ee_readw_buffer;
314         rom->readw_sw = txgbe_ee_readw_sw;
315         rom->read32 = txgbe_ee_read32;
316         rom->write16 = txgbe_ee_write16;
317         rom->writew_buffer = txgbe_ee_writew_buffer;
318         rom->writew_sw = txgbe_ee_writew_sw;
319         rom->write32 = txgbe_ee_write32;
320         rom->validate_checksum = txgbe_validate_eeprom_checksum;
321         rom->update_checksum = txgbe_update_eeprom_checksum;
322         rom->calc_checksum = txgbe_calc_eeprom_checksum;
323
324         mac->num_rar_entries    = TXGBE_RAPTOR_RAR_ENTRIES;
325         mac->max_rx_queues      = TXGBE_RAPTOR_MAX_RX_QUEUES;
326         mac->max_tx_queues      = TXGBE_RAPTOR_MAX_TX_QUEUES;
327
328         return 0;
329 }
330
331 static int
332 txgbe_check_flash_load(struct txgbe_hw *hw, u32 check_bit)
333 {
334         u32 reg = 0;
335         u32 i;
336         int err = 0;
337         /* if there's flash existing */
338         if (!(rd32(hw, TXGBE_SPISTAT) & TXGBE_SPISTAT_BPFLASH)) {
339                 /* wait hw load flash done */
340                 for (i = 0; i < 10; i++) {
341                         reg = rd32(hw, TXGBE_ILDRSTAT);
342                         if (!(reg & check_bit)) {
343                                 /* done */
344                                 break;
345                         }
346                         msleep(100);
347                 }
348                 if (i == 10)
349                         err = TXGBE_ERR_FLASH_LOADING_FAILED;
350         }
351         return err;
352 }
353
354 /**
355  *  txgbe_reset_hw - Perform hardware reset
356  *  @hw: pointer to hardware structure
357  *
358  *  Resets the hardware by resetting the transmit and receive units, masks
359  *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
360  *  reset.
361  **/
362 s32 txgbe_reset_hw(struct txgbe_hw *hw)
363 {
364         s32 status;
365         u32 autoc;
366
367         DEBUGFUNC("txgbe_reset_hw");
368
369         /* Call adapter stop to disable tx/rx and clear interrupts */
370         status = hw->mac.stop_hw(hw);
371         if (status != 0)
372                 return status;
373
374         /* flush pending Tx transactions */
375         txgbe_clear_tx_pending(hw);
376
377         /* Identify PHY and related function pointers */
378         status = hw->phy.init(hw);
379         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
380                 return status;
381
382         /* Setup SFP module if there is one present. */
383         if (hw->phy.sfp_setup_needed) {
384                 status = hw->mac.setup_sfp(hw);
385                 hw->phy.sfp_setup_needed = false;
386         }
387         if (status == TXGBE_ERR_SFP_NOT_SUPPORTED)
388                 return status;
389
390         /* Reset PHY */
391         if (!hw->phy.reset_disable)
392                 hw->phy.reset(hw);
393
394         /* remember AUTOC from before we reset */
395         autoc = hw->mac.autoc_read(hw);
396
397 mac_reset_top:
398         /*
399          * Issue global reset to the MAC.  Needs to be SW reset if link is up.
400          * If link reset is used when link is up, it might reset the PHY when
401          * mng is using it.  If link is down or the flag to force full link
402          * reset is set, then perform link reset.
403          */
404         if (txgbe_mng_present(hw)) {
405                 txgbe_hic_reset(hw);
406         } else {
407                 wr32(hw, TXGBE_RST, TXGBE_RST_LAN(hw->bus.lan_id));
408                 txgbe_flush(hw);
409         }
410         usec_delay(10);
411
412         if (hw->bus.lan_id == 0) {
413                 status = txgbe_check_flash_load(hw,
414                                 TXGBE_ILDRSTAT_SWRST_LAN0);
415         } else {
416                 status = txgbe_check_flash_load(hw,
417                                 TXGBE_ILDRSTAT_SWRST_LAN1);
418         }
419         if (status != 0)
420                 return status;
421
422         msec_delay(50);
423
424         /*
425          * Double resets are required for recovery from certain error
426          * conditions.  Between resets, it is necessary to stall to
427          * allow time for any pending HW events to complete.
428          */
429         if (hw->mac.flags & TXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
430                 hw->mac.flags &= ~TXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
431                 goto mac_reset_top;
432         }
433
434         /*
435          * Store the original AUTOC/AUTOC2 values if they have not been
436          * stored off yet.  Otherwise restore the stored original
437          * values since the reset operation sets back to defaults.
438          */
439         if (!hw->mac.orig_link_settings_stored) {
440                 hw->mac.orig_autoc = hw->mac.autoc_read(hw);
441                 hw->mac.autoc_write(hw, hw->mac.orig_autoc);
442                 hw->mac.orig_link_settings_stored = true;
443         } else {
444                 hw->mac.orig_autoc = autoc;
445         }
446
447         /* Store the permanent mac address */
448         hw->mac.get_mac_addr(hw, hw->mac.perm_addr);
449
450         /*
451          * Store MAC address from RAR0, clear receive address registers, and
452          * clear the multicast table.  Also reset num_rar_entries to 128,
453          * since we modify this value when programming the SAN MAC address.
454          */
455         hw->mac.num_rar_entries = 128;
456         hw->mac.init_rx_addrs(hw);
457
458         /* Store the permanent SAN mac address */
459         hw->mac.get_san_mac_addr(hw, hw->mac.san_addr);
460
461         /* Add the SAN MAC address to the RAR only if it's a valid address */
462         if (txgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
463                 /* Save the SAN MAC RAR index */
464                 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
465
466                 hw->mac.set_rar(hw, hw->mac.san_mac_rar_index,
467                                     hw->mac.san_addr, 0, true);
468
469                 /* clear VMDq pool/queue selection for this RAR */
470                 hw->mac.clear_vmdq(hw, hw->mac.san_mac_rar_index,
471                                        BIT_MASK32);
472
473                 /* Reserve the last RAR for the SAN MAC address */
474                 hw->mac.num_rar_entries--;
475         }
476
477         /* Store the alternative WWNN/WWPN prefix */
478         hw->mac.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
479                                    &hw->mac.wwpn_prefix);
480
481         return status;
482 }
483