Skip to content

Commit 79625f4

Browse files
Jiawen Wukuba-moo
authored andcommitted
net: wangxun: Move MAC address handling to libwx
For setting MAC address, both txgbe and ngbe drivers have the same handling flow with different parameters. Move the same codes to libwx. Signed-off-by: Jiawen Wu <jiawenwu@trustnetic.com> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 92710fe commit 79625f4

File tree

7 files changed

+151
-212
lines changed

7 files changed

+151
-212
lines changed

drivers/net/ethernet/wangxun/libwx/wx_hw.c

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (c) 2015 - 2022 Beijing WangXun Technology Co., Ltd. */
33

44
#include <linux/etherdevice.h>
5+
#include <linux/netdevice.h>
56
#include <linux/if_ether.h>
67
#include <linux/iopoll.h>
78
#include <linux/pci.h>
@@ -536,8 +537,8 @@ EXPORT_SYMBOL(wx_get_mac_addr);
536537
*
537538
* Puts an ethernet address into a receive address register.
538539
**/
539-
int wx_set_rar(struct wx_hw *wxhw, u32 index, u8 *addr, u64 pools,
540-
u32 enable_addr)
540+
static int wx_set_rar(struct wx_hw *wxhw, u32 index, u8 *addr, u64 pools,
541+
u32 enable_addr)
541542
{
542543
u32 rar_entries = wxhw->mac.num_rar_entries;
543544
u32 rar_low, rar_high;
@@ -581,7 +582,6 @@ int wx_set_rar(struct wx_hw *wxhw, u32 index, u8 *addr, u64 pools,
581582

582583
return 0;
583584
}
584-
EXPORT_SYMBOL(wx_set_rar);
585585

586586
/**
587587
* wx_clear_rar - Remove Rx address register
@@ -590,7 +590,7 @@ EXPORT_SYMBOL(wx_set_rar);
590590
*
591591
* Clears an ethernet address from a receive address register.
592592
**/
593-
int wx_clear_rar(struct wx_hw *wxhw, u32 index)
593+
static int wx_clear_rar(struct wx_hw *wxhw, u32 index)
594594
{
595595
u32 rar_entries = wxhw->mac.num_rar_entries;
596596

@@ -618,7 +618,6 @@ int wx_clear_rar(struct wx_hw *wxhw, u32 index)
618618

619619
return 0;
620620
}
621-
EXPORT_SYMBOL(wx_clear_rar);
622621

623622
/**
624623
* wx_clear_vmdq - Disassociate a VMDq pool index from a rx address
@@ -722,6 +721,105 @@ void wx_init_rx_addrs(struct wx_hw *wxhw)
722721
}
723722
EXPORT_SYMBOL(wx_init_rx_addrs);
724723

724+
static void wx_sync_mac_table(struct wx_hw *wxhw)
725+
{
726+
int i;
727+
728+
for (i = 0; i < wxhw->mac.num_rar_entries; i++) {
729+
if (wxhw->mac_table[i].state & WX_MAC_STATE_MODIFIED) {
730+
if (wxhw->mac_table[i].state & WX_MAC_STATE_IN_USE) {
731+
wx_set_rar(wxhw, i,
732+
wxhw->mac_table[i].addr,
733+
wxhw->mac_table[i].pools,
734+
WX_PSR_MAC_SWC_AD_H_AV);
735+
} else {
736+
wx_clear_rar(wxhw, i);
737+
}
738+
wxhw->mac_table[i].state &= ~(WX_MAC_STATE_MODIFIED);
739+
}
740+
}
741+
}
742+
743+
/* this function destroys the first RAR entry */
744+
void wx_mac_set_default_filter(struct wx_hw *wxhw, u8 *addr)
745+
{
746+
memcpy(&wxhw->mac_table[0].addr, addr, ETH_ALEN);
747+
wxhw->mac_table[0].pools = 1ULL;
748+
wxhw->mac_table[0].state = (WX_MAC_STATE_DEFAULT | WX_MAC_STATE_IN_USE);
749+
wx_set_rar(wxhw, 0, wxhw->mac_table[0].addr,
750+
wxhw->mac_table[0].pools,
751+
WX_PSR_MAC_SWC_AD_H_AV);
752+
}
753+
EXPORT_SYMBOL(wx_mac_set_default_filter);
754+
755+
void wx_flush_sw_mac_table(struct wx_hw *wxhw)
756+
{
757+
u32 i;
758+
759+
for (i = 0; i < wxhw->mac.num_rar_entries; i++) {
760+
if (!(wxhw->mac_table[i].state & WX_MAC_STATE_IN_USE))
761+
continue;
762+
763+
wxhw->mac_table[i].state |= WX_MAC_STATE_MODIFIED;
764+
wxhw->mac_table[i].state &= ~WX_MAC_STATE_IN_USE;
765+
memset(wxhw->mac_table[i].addr, 0, ETH_ALEN);
766+
wxhw->mac_table[i].pools = 0;
767+
}
768+
wx_sync_mac_table(wxhw);
769+
}
770+
EXPORT_SYMBOL(wx_flush_sw_mac_table);
771+
772+
static int wx_del_mac_filter(struct wx_hw *wxhw, u8 *addr, u16 pool)
773+
{
774+
u32 i;
775+
776+
if (is_zero_ether_addr(addr))
777+
return -EINVAL;
778+
779+
/* search table for addr, if found, set to 0 and sync */
780+
for (i = 0; i < wxhw->mac.num_rar_entries; i++) {
781+
if (!ether_addr_equal(addr, wxhw->mac_table[i].addr))
782+
continue;
783+
784+
wxhw->mac_table[i].state |= WX_MAC_STATE_MODIFIED;
785+
wxhw->mac_table[i].pools &= ~(1ULL << pool);
786+
if (!wxhw->mac_table[i].pools) {
787+
wxhw->mac_table[i].state &= ~WX_MAC_STATE_IN_USE;
788+
memset(wxhw->mac_table[i].addr, 0, ETH_ALEN);
789+
}
790+
wx_sync_mac_table(wxhw);
791+
return 0;
792+
}
793+
return -ENOMEM;
794+
}
795+
796+
/**
797+
* wx_set_mac - Change the Ethernet Address of the NIC
798+
* @netdev: network interface device structure
799+
* @p: pointer to an address structure
800+
*
801+
* Returns 0 on success, negative on failure
802+
**/
803+
int wx_set_mac(struct net_device *netdev, void *p)
804+
{
805+
struct wx_hw *wxhw = container_of(&netdev, struct wx_hw, netdev);
806+
struct sockaddr *addr = p;
807+
int retval;
808+
809+
retval = eth_prepare_mac_addr_change(netdev, addr);
810+
if (retval)
811+
return retval;
812+
813+
wx_del_mac_filter(wxhw, wxhw->mac.addr, 0);
814+
eth_hw_addr_set(netdev, addr->sa_data);
815+
memcpy(wxhw->mac.addr, addr->sa_data, netdev->addr_len);
816+
817+
wx_mac_set_default_filter(wxhw, wxhw->mac.addr);
818+
819+
return 0;
820+
}
821+
EXPORT_SYMBOL(wx_set_mac);
822+
725823
void wx_disable_rx(struct wx_hw *wxhw)
726824
{
727825
u32 pfdtxgswc;
@@ -929,6 +1027,14 @@ int wx_sw_init(struct wx_hw *wxhw)
9291027
return err;
9301028
}
9311029

1030+
wxhw->mac_table = kcalloc(wxhw->mac.num_rar_entries,
1031+
sizeof(struct wx_mac_addr),
1032+
GFP_KERNEL);
1033+
if (!wxhw->mac_table) {
1034+
wx_err(wxhw, "mac_table allocation failed\n");
1035+
return -ENOMEM;
1036+
}
1037+
9321038
return 0;
9331039
}
9341040
EXPORT_SYMBOL(wx_sw_init);

drivers/net/ethernet/wangxun/libwx/wx_hw.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ int wx_read_ee_hostif_buffer(struct wx_hw *wxhw,
1515
int wx_reset_hostif(struct wx_hw *wxhw);
1616
void wx_init_eeprom_params(struct wx_hw *wxhw);
1717
void wx_get_mac_addr(struct wx_hw *wxhw, u8 *mac_addr);
18-
int wx_set_rar(struct wx_hw *wxhw, u32 index, u8 *addr, u64 pools, u32 enable_addr);
19-
int wx_clear_rar(struct wx_hw *wxhw, u32 index);
2018
void wx_init_rx_addrs(struct wx_hw *wxhw);
19+
void wx_mac_set_default_filter(struct wx_hw *wxhw, u8 *addr);
20+
void wx_flush_sw_mac_table(struct wx_hw *wxhw);
21+
int wx_set_mac(struct net_device *netdev, void *p);
2122
void wx_disable_rx(struct wx_hw *wxhw);
2223
int wx_disable_pcie_master(struct wx_hw *wxhw);
2324
int wx_stop_adapter(struct wx_hw *wxhw);

drivers/net/ethernet/wangxun/libwx/wx_type.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@
185185

186186
#define WX_SW_REGION_PTR 0x1C
187187

188+
#define WX_MAC_STATE_DEFAULT 0x1
189+
#define WX_MAC_STATE_MODIFIED 0x2
190+
#define WX_MAC_STATE_IN_USE 0x4
191+
188192
/* Host Interface Command Structures */
189193
struct wx_hic_hdr {
190194
u8 cmd;
@@ -284,6 +288,12 @@ struct wx_addr_filter_info {
284288
bool user_set_promisc;
285289
};
286290

291+
struct wx_mac_addr {
292+
u8 addr[ETH_ALEN];
293+
u16 state; /* bitmask */
294+
u64 pools;
295+
};
296+
287297
enum wx_reset_type {
288298
WX_LAN_RESET = 0,
289299
WX_SW_RESET,
@@ -293,10 +303,12 @@ enum wx_reset_type {
293303
struct wx_hw {
294304
u8 __iomem *hw_addr;
295305
struct pci_dev *pdev;
306+
struct net_device *netdev;
296307
struct wx_bus_info bus;
297308
struct wx_mac_info mac;
298309
struct wx_eeprom_info eeprom;
299310
struct wx_addr_filter_info addr_ctrl;
311+
struct wx_mac_addr *mac_table;
300312
u16 device_id;
301313
u16 vendor_id;
302314
u16 subsystem_device_id;

drivers/net/ethernet/wangxun/ngbe/ngbe_main.c

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ static const struct pci_device_id ngbe_pci_tbl[] = {
3939
{ .device = 0 }
4040
};
4141

42-
static void ngbe_mac_set_default_filter(struct ngbe_adapter *adapter, u8 *addr)
43-
{
44-
memcpy(&adapter->mac_table[0].addr, addr, ETH_ALEN);
45-
adapter->mac_table[0].pools = 1ULL;
46-
adapter->mac_table[0].state = (NGBE_MAC_STATE_DEFAULT |
47-
NGBE_MAC_STATE_IN_USE);
48-
wx_set_rar(&adapter->wxhw, 0, adapter->mac_table[0].addr,
49-
adapter->mac_table[0].pools,
50-
WX_PSR_MAC_SWC_AD_H_AV);
51-
}
52-
5342
/**
5443
* ngbe_init_type_code - Initialize the shared code
5544
* @adapter: pointer to hardware structure
@@ -152,6 +141,10 @@ static int ngbe_sw_init(struct ngbe_adapter *adapter)
152141
wxhw->hw_addr = adapter->io_addr;
153142
wxhw->pdev = pdev;
154143

144+
wxhw->mac.num_rar_entries = NGBE_RAR_ENTRIES;
145+
wxhw->mac.max_rx_queues = NGBE_MAX_RX_QUEUES;
146+
wxhw->mac.max_tx_queues = NGBE_MAX_TX_QUEUES;
147+
155148
/* PCI config space info */
156149
err = wx_sw_init(wxhw);
157150
if (err < 0) {
@@ -163,25 +156,13 @@ static int ngbe_sw_init(struct ngbe_adapter *adapter)
163156
/* mac type, phy type , oem type */
164157
ngbe_init_type_code(adapter);
165158

166-
wxhw->mac.max_rx_queues = NGBE_MAX_RX_QUEUES;
167-
wxhw->mac.max_tx_queues = NGBE_MAX_TX_QUEUES;
168-
wxhw->mac.num_rar_entries = NGBE_RAR_ENTRIES;
169159
/* Set common capability flags and settings */
170160
adapter->max_q_vectors = NGBE_MAX_MSIX_VECTORS;
171-
172161
err = wx_get_pcie_msix_counts(wxhw, &msix_count, NGBE_MAX_MSIX_VECTORS);
173162
if (err)
174163
dev_err(&pdev->dev, "Do not support MSI-X\n");
175164
wxhw->mac.max_msix_vectors = msix_count;
176165

177-
adapter->mac_table = kcalloc(wxhw->mac.num_rar_entries,
178-
sizeof(struct ngbe_mac_addr),
179-
GFP_KERNEL);
180-
if (!adapter->mac_table) {
181-
dev_err(&pdev->dev, "mac_table allocation failed: %d\n", err);
182-
return -ENOMEM;
183-
}
184-
185166
if (ngbe_init_rss_key(adapter))
186167
return -ENOMEM;
187168

@@ -252,30 +233,6 @@ static netdev_tx_t ngbe_xmit_frame(struct sk_buff *skb,
252233
return NETDEV_TX_OK;
253234
}
254235

255-
/**
256-
* ngbe_set_mac - Change the Ethernet Address of the NIC
257-
* @netdev: network interface device structure
258-
* @p: pointer to an address structure
259-
*
260-
* Returns 0 on success, negative on failure
261-
**/
262-
static int ngbe_set_mac(struct net_device *netdev, void *p)
263-
{
264-
struct ngbe_adapter *adapter = netdev_priv(netdev);
265-
struct wx_hw *wxhw = &adapter->wxhw;
266-
struct sockaddr *addr = p;
267-
268-
if (!is_valid_ether_addr(addr->sa_data))
269-
return -EADDRNOTAVAIL;
270-
271-
eth_hw_addr_set(netdev, addr->sa_data);
272-
memcpy(wxhw->mac.addr, addr->sa_data, netdev->addr_len);
273-
274-
ngbe_mac_set_default_filter(adapter, wxhw->mac.addr);
275-
276-
return 0;
277-
}
278-
279236
static void ngbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake)
280237
{
281238
struct ngbe_adapter *adapter = pci_get_drvdata(pdev);
@@ -312,7 +269,7 @@ static const struct net_device_ops ngbe_netdev_ops = {
312269
.ndo_stop = ngbe_close,
313270
.ndo_start_xmit = ngbe_xmit_frame,
314271
.ndo_validate_addr = eth_validate_addr,
315-
.ndo_set_mac_address = ngbe_set_mac,
272+
.ndo_set_mac_address = wx_set_mac,
316273
};
317274

318275
/**
@@ -377,6 +334,7 @@ static int ngbe_probe(struct pci_dev *pdev,
377334
adapter->netdev = netdev;
378335
adapter->pdev = pdev;
379336
wxhw = &adapter->wxhw;
337+
wxhw->netdev = netdev;
380338
adapter->msg_enable = BIT(3) - 1;
381339

382340
adapter->io_addr = devm_ioremap(&pdev->dev,
@@ -463,7 +421,7 @@ static int ngbe_probe(struct pci_dev *pdev,
463421
}
464422

465423
eth_hw_addr_set(netdev, wxhw->mac.perm_addr);
466-
ngbe_mac_set_default_filter(adapter, wxhw->mac.perm_addr);
424+
wx_mac_set_default_filter(wxhw, wxhw->mac.perm_addr);
467425

468426
err = register_netdev(netdev);
469427
if (err)
@@ -481,7 +439,7 @@ static int ngbe_probe(struct pci_dev *pdev,
481439
err_register:
482440
wx_control_hw(wxhw, false);
483441
err_free_mac_table:
484-
kfree(adapter->mac_table);
442+
kfree(wxhw->mac_table);
485443
err_pci_release_regions:
486444
pci_disable_pcie_error_reporting(pdev);
487445
pci_release_selected_regions(pdev,
@@ -503,14 +461,15 @@ static int ngbe_probe(struct pci_dev *pdev,
503461
static void ngbe_remove(struct pci_dev *pdev)
504462
{
505463
struct ngbe_adapter *adapter = pci_get_drvdata(pdev);
464+
struct wx_hw *wxhw = &adapter->wxhw;
506465
struct net_device *netdev;
507466

508467
netdev = adapter->netdev;
509468
unregister_netdev(netdev);
510469
pci_release_selected_regions(pdev,
511470
pci_select_bars(pdev, IORESOURCE_MEM));
512471

513-
kfree(adapter->mac_table);
472+
kfree(wxhw->mac_table);
514473
pci_disable_pcie_error_reporting(pdev);
515474

516475
pci_disable_device(pdev);

drivers/net/ethernet/wangxun/ngbe/ngbe_type.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@
110110
#define NGBE_MAX_RXD 8192
111111
#define NGBE_MIN_RXD 128
112112

113-
#define NGBE_MAC_STATE_DEFAULT 0x1
114-
#define NGBE_MAC_STATE_MODIFIED 0x2
115-
#define NGBE_MAC_STATE_IN_USE 0x4
116-
117113
enum ngbe_phy_type {
118114
ngbe_phy_unknown = 0,
119115
ngbe_phy_none,
@@ -151,12 +147,6 @@ struct ngbe_phy_info {
151147

152148
};
153149

154-
struct ngbe_mac_addr {
155-
u8 addr[ETH_ALEN];
156-
u16 state; /* bitmask */
157-
u64 pools;
158-
};
159-
160150
/* board specific private data structure */
161151
struct ngbe_adapter {
162152
u8 __iomem *io_addr; /* Mainly for iounmap use */
@@ -172,7 +162,6 @@ struct ngbe_adapter {
172162
bool ncsi_enabled;
173163
bool gpio_ctrl;
174164

175-
struct ngbe_mac_addr *mac_table;
176165
u16 msg_enable;
177166

178167
/* Tx fast path data */

0 commit comments

Comments
 (0)