1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2024 Hisilicon Limited.
4 #include <linux/etherdevice.h>
5 #include <linux/if_vlan.h>
6 #include <linux/netdevice.h>
8 #include "hbg_common.h"
9 #include "hbg_ethtool.h"
15 static void hbg_change_mtu(struct hbg_priv
*priv
, int new_mtu
);
17 static void hbg_all_irq_enable(struct hbg_priv
*priv
, bool enabled
)
19 struct hbg_irq_info
*info
;
22 for (i
= 0; i
< priv
->vectors
.info_array_len
; i
++) {
23 info
= &priv
->vectors
.info_array
[i
];
24 hbg_hw_irq_enable(priv
, info
->mask
, enabled
);
28 static int hbg_net_open(struct net_device
*netdev
)
30 struct hbg_priv
*priv
= netdev_priv(netdev
);
33 ret
= hbg_txrx_init(priv
);
37 hbg_all_irq_enable(priv
, true);
38 hbg_hw_mac_enable(priv
, HBG_STATUS_ENABLE
);
39 netif_start_queue(netdev
);
45 /* This function only can be called after hbg_txrx_uninit() */
46 static int hbg_hw_txrx_clear(struct hbg_priv
*priv
)
50 /* After ring buffers have been released,
51 * do a reset to release hw fifo rx ring buffer
53 ret
= hbg_hw_event_notify(priv
, HBG_HW_EVENT_RESET
);
57 /* After reset, regs need to be reconfigured */
59 hbg_hw_set_uc_addr(priv
, ether_addr_to_u64(priv
->netdev
->dev_addr
));
60 hbg_change_mtu(priv
, priv
->netdev
->mtu
);
65 static int hbg_net_stop(struct net_device
*netdev
)
67 struct hbg_priv
*priv
= netdev_priv(netdev
);
70 netif_stop_queue(netdev
);
71 hbg_hw_mac_enable(priv
, HBG_STATUS_DISABLE
);
72 hbg_all_irq_enable(priv
, false);
73 hbg_txrx_uninit(priv
);
74 return hbg_hw_txrx_clear(priv
);
77 static int hbg_net_set_mac_address(struct net_device
*netdev
, void *addr
)
79 struct hbg_priv
*priv
= netdev_priv(netdev
);
82 mac_addr
= ((struct sockaddr
*)addr
)->sa_data
;
84 if (!is_valid_ether_addr(mac_addr
))
85 return -EADDRNOTAVAIL
;
87 hbg_hw_set_uc_addr(priv
, ether_addr_to_u64(mac_addr
));
88 dev_addr_set(netdev
, mac_addr
);
93 static void hbg_change_mtu(struct hbg_priv
*priv
, int new_mtu
)
97 frame_len
= new_mtu
+ VLAN_HLEN
* priv
->dev_specs
.vlan_layers
+
98 ETH_HLEN
+ ETH_FCS_LEN
;
99 hbg_hw_set_mtu(priv
, frame_len
);
102 static int hbg_net_change_mtu(struct net_device
*netdev
, int new_mtu
)
104 struct hbg_priv
*priv
= netdev_priv(netdev
);
106 if (netif_running(netdev
))
109 hbg_change_mtu(priv
, new_mtu
);
110 WRITE_ONCE(netdev
->mtu
, new_mtu
);
112 dev_dbg(&priv
->pdev
->dev
,
113 "change mtu from %u to %u\n", netdev
->mtu
, new_mtu
);
118 static void hbg_net_tx_timeout(struct net_device
*netdev
, unsigned int txqueue
)
120 struct hbg_priv
*priv
= netdev_priv(netdev
);
121 struct hbg_ring
*ring
= &priv
->tx_ring
;
122 char *buf
= ring
->tout_log_buf
;
125 pos
+= scnprintf(buf
+ pos
, HBG_TX_TIMEOUT_BUF_LEN
- pos
,
126 "ring used num: %u, fifo used num: %u\n",
127 hbg_get_queue_used_num(ring
),
128 hbg_hw_get_fifo_used_num(priv
, HBG_DIR_TX
));
129 pos
+= scnprintf(buf
+ pos
, HBG_TX_TIMEOUT_BUF_LEN
- pos
,
130 "ntc: %u, ntu: %u, irq enabled: %u\n",
131 ring
->ntc
, ring
->ntu
,
132 hbg_hw_irq_is_enabled(priv
, HBG_INT_MSK_TX_B
));
134 netdev_info(netdev
, "%s", buf
);
137 static const struct net_device_ops hbg_netdev_ops
= {
138 .ndo_open
= hbg_net_open
,
139 .ndo_stop
= hbg_net_stop
,
140 .ndo_start_xmit
= hbg_net_start_xmit
,
141 .ndo_validate_addr
= eth_validate_addr
,
142 .ndo_set_mac_address
= hbg_net_set_mac_address
,
143 .ndo_change_mtu
= hbg_net_change_mtu
,
144 .ndo_tx_timeout
= hbg_net_tx_timeout
,
147 static int hbg_init(struct hbg_priv
*priv
)
151 ret
= hbg_hw_event_notify(priv
, HBG_HW_EVENT_INIT
);
155 ret
= hbg_hw_init(priv
);
159 ret
= hbg_irq_init(priv
);
163 return hbg_mdio_init(priv
);
166 static int hbg_pci_init(struct pci_dev
*pdev
)
168 struct net_device
*netdev
= pci_get_drvdata(pdev
);
169 struct hbg_priv
*priv
= netdev_priv(netdev
);
170 struct device
*dev
= &pdev
->dev
;
173 ret
= pcim_enable_device(pdev
);
175 return dev_err_probe(dev
, ret
, "failed to enable PCI device\n");
177 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32));
179 return dev_err_probe(dev
, ret
, "failed to set PCI DMA mask\n");
181 ret
= pcim_iomap_regions(pdev
, BIT(0), dev_driver_string(dev
));
183 return dev_err_probe(dev
, ret
, "failed to map PCI bar space\n");
185 priv
->io_base
= pcim_iomap_table(pdev
)[0];
187 return dev_err_probe(dev
, -ENOMEM
, "failed to get io base\n");
189 pci_set_master(pdev
);
193 static int hbg_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
195 struct device
*dev
= &pdev
->dev
;
196 struct net_device
*netdev
;
197 struct hbg_priv
*priv
;
200 netdev
= devm_alloc_etherdev(dev
, sizeof(struct hbg_priv
));
204 pci_set_drvdata(pdev
, netdev
);
205 SET_NETDEV_DEV(netdev
, dev
);
207 priv
= netdev_priv(netdev
);
208 priv
->netdev
= netdev
;
211 ret
= hbg_pci_init(pdev
);
215 ret
= hbg_init(priv
);
219 netdev
->pcpu_stat_type
= NETDEV_PCPU_STAT_TSTATS
;
220 netdev
->max_mtu
= priv
->dev_specs
.max_mtu
;
221 netdev
->min_mtu
= priv
->dev_specs
.min_mtu
;
222 netdev
->netdev_ops
= &hbg_netdev_ops
;
223 netdev
->watchdog_timeo
= 5 * HZ
;
225 hbg_change_mtu(priv
, ETH_DATA_LEN
);
226 hbg_net_set_mac_address(priv
->netdev
, &priv
->dev_specs
.mac_addr
);
227 hbg_ethtool_set_ops(netdev
);
229 ret
= devm_register_netdev(dev
, netdev
);
231 return dev_err_probe(dev
, ret
, "failed to register netdev\n");
233 netif_carrier_off(netdev
);
237 static const struct pci_device_id hbg_pci_tbl
[] = {
238 {PCI_VDEVICE(HUAWEI
, 0x3730), 0},
241 MODULE_DEVICE_TABLE(pci
, hbg_pci_tbl
);
243 static struct pci_driver hbg_driver
= {
245 .id_table
= hbg_pci_tbl
,
248 module_pci_driver(hbg_driver
);
250 MODULE_LICENSE("GPL");
251 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
252 MODULE_DESCRIPTION("hibmcge driver");
253 MODULE_VERSION("1.0");