1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
// SPDX-License-Identifier: GPL-2.0+
// Copyright (c) 2024 Hisilicon Limited.
#include <linux/etherdevice.h>
#include <linux/if_vlan.h>
#include <linux/netdevice.h>
#include <linux/pci.h>
#include "hbg_common.h"
#include "hbg_err.h"
#include "hbg_ethtool.h"
#include "hbg_hw.h"
#include "hbg_irq.h"
#include "hbg_mdio.h"
#include "hbg_txrx.h"
#include "hbg_debugfs.h"
static void hbg_all_irq_enable(struct hbg_priv *priv, bool enabled)
{
struct hbg_irq_info *info;
u32 i;
for (i = 0; i < priv->vectors.info_array_len; i++) {
info = &priv->vectors.info_array[i];
hbg_hw_irq_enable(priv, info->mask, enabled);
}
}
static int hbg_net_open(struct net_device *netdev)
{
struct hbg_priv *priv = netdev_priv(netdev);
int ret;
ret = hbg_txrx_init(priv);
if (ret)
return ret;
hbg_all_irq_enable(priv, true);
hbg_hw_mac_enable(priv, HBG_STATUS_ENABLE);
netif_start_queue(netdev);
hbg_phy_start(priv);
return 0;
}
/* This function only can be called after hbg_txrx_uninit() */
static int hbg_hw_txrx_clear(struct hbg_priv *priv)
{
int ret;
/* After ring buffers have been released,
* do a reset to release hw fifo rx ring buffer
*/
ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_RESET);
if (ret)
return ret;
/* After reset, regs need to be reconfigured */
return hbg_rebuild(priv);
}
static int hbg_net_stop(struct net_device *netdev)
{
struct hbg_priv *priv = netdev_priv(netdev);
hbg_phy_stop(priv);
netif_stop_queue(netdev);
hbg_hw_mac_enable(priv, HBG_STATUS_DISABLE);
hbg_all_irq_enable(priv, false);
hbg_txrx_uninit(priv);
return hbg_hw_txrx_clear(priv);
}
static void hbg_update_promisc_mode(struct net_device *netdev, bool overflow)
{
struct hbg_priv *priv = netdev_priv(netdev);
/* Only when not table_overflow, and netdev->flags not set IFF_PROMISC,
* The MAC filter will be enabled.
* Otherwise the filter will be disabled.
*/
priv->filter.enabled = !(overflow || (netdev->flags & IFF_PROMISC));
hbg_hw_set_mac_filter_enable(priv, priv->filter.enabled);
}
static void hbg_set_mac_to_mac_table(struct hbg_priv *priv,
u32 index, const u8 *addr)
{
if (addr) {
ether_addr_copy(priv->filter.mac_table[index].addr, addr);
hbg_hw_set_uc_addr(priv, ether_addr_to_u64(addr), index);
} else {
eth_zero_addr(priv->filter.mac_table[index].addr);
hbg_hw_set_uc_addr(priv, 0, index);
}
}
static int hbg_get_index_from_mac_table(struct hbg_priv *priv,
const u8 *addr, u32 *index)
{
u32 i;
for (i = 0; i < priv->filter.table_max_len; i++)
if (ether_addr_equal(priv->filter.mac_table[i].addr, addr)) {
*index = i;
return 0;
}
return -EINVAL;
}
static int hbg_add_mac_to_filter(struct hbg_priv *priv, const u8 *addr)
{
u32 index;
/* already exists */
if (!hbg_get_index_from_mac_table(priv, addr, &index))
return 0;
for (index = 0; index < priv->filter.table_max_len; index++)
if (is_zero_ether_addr(priv->filter.mac_table[index].addr)) {
hbg_set_mac_to_mac_table(priv, index, addr);
return 0;
}
return -ENOSPC;
}
static void hbg_del_mac_from_filter(struct hbg_priv *priv, const u8 *addr)
{
u32 index;
/* not exists */
if (hbg_get_index_from_mac_table(priv, addr, &index))
return;
hbg_set_mac_to_mac_table(priv, index, NULL);
}
static int hbg_uc_sync(struct net_device *netdev, const unsigned char *addr)
{
struct hbg_priv *priv = netdev_priv(netdev);
return hbg_add_mac_to_filter(priv, addr);
}
static int hbg_uc_unsync(struct net_device *netdev, const unsigned char *addr)
{
struct hbg_priv *priv = netdev_priv(netdev);
if (ether_addr_equal(netdev->dev_addr, (u8 *)addr))
return 0;
hbg_del_mac_from_filter(priv, addr);
return 0;
}
static void hbg_net_set_rx_mode(struct net_device *netdev)
{
int ret;
ret = __dev_uc_sync(netdev, hbg_uc_sync, hbg_uc_unsync);
/* If ret != 0, overflow has occurred */
hbg_update_promisc_mode(netdev, !!ret);
}
static int hbg_net_set_mac_address(struct net_device *netdev, void *addr)
{
struct hbg_priv *priv = netdev_priv(netdev);
u8 *mac_addr;
bool exists;
u32 index;
mac_addr = ((struct sockaddr *)addr)->sa_data;
if (!is_valid_ether_addr(mac_addr))
return -EADDRNOTAVAIL;
/* The index of host mac is always 0.
* If new mac address already exists,
* delete the existing mac address and
* add it to the position with index 0.
*/
exists = !hbg_get_index_from_mac_table(priv, mac_addr, &index);
hbg_set_mac_to_mac_table(priv, 0, mac_addr);
if (exists)
hbg_set_mac_to_mac_table(priv, index, NULL);
hbg_hw_set_rx_pause_mac_addr(priv, ether_addr_to_u64(mac_addr));
dev_addr_set(netdev, mac_addr);
return 0;
}
static int hbg_net_change_mtu(struct net_device *netdev, int new_mtu)
{
struct hbg_priv *priv = netdev_priv(netdev);
if (netif_running(netdev))
return -EBUSY;
hbg_hw_set_mtu(priv, new_mtu);
WRITE_ONCE(netdev->mtu, new_mtu);
dev_dbg(&priv->pdev->dev,
"change mtu from %u to %u\n", netdev->mtu, new_mtu);
return 0;
}
static void hbg_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
{
struct hbg_priv *priv = netdev_priv(netdev);
struct hbg_ring *ring = &priv->tx_ring;
char *buf = ring->tout_log_buf;
u32 pos = 0;
pos += scnprintf(buf + pos, HBG_TX_TIMEOUT_BUF_LEN - pos,
"ring used num: %u, fifo used num: %u\n",
hbg_get_queue_used_num(ring),
hbg_hw_get_fifo_used_num(priv, HBG_DIR_TX));
pos += scnprintf(buf + pos, HBG_TX_TIMEOUT_BUF_LEN - pos,
"ntc: %u, ntu: %u, irq enabled: %u\n",
ring->ntc, ring->ntu,
hbg_hw_irq_is_enabled(priv, HBG_INT_MSK_TX_B));
netdev_info(netdev, "%s", buf);
}
static const struct net_device_ops hbg_netdev_ops = {
.ndo_open = hbg_net_open,
.ndo_stop = hbg_net_stop,
.ndo_start_xmit = hbg_net_start_xmit,
.ndo_validate_addr = eth_validate_addr,
.ndo_set_mac_address = hbg_net_set_mac_address,
.ndo_change_mtu = hbg_net_change_mtu,
.ndo_tx_timeout = hbg_net_tx_timeout,
.ndo_set_rx_mode = hbg_net_set_rx_mode,
};
static int hbg_mac_filter_init(struct hbg_priv *priv)
{
struct hbg_dev_specs *dev_specs = &priv->dev_specs;
struct hbg_mac_filter *filter = &priv->filter;
struct hbg_mac_table_entry *tmp_table;
tmp_table = devm_kcalloc(&priv->pdev->dev, dev_specs->uc_mac_num,
sizeof(*tmp_table), GFP_KERNEL);
if (!tmp_table)
return -ENOMEM;
filter->mac_table = tmp_table;
filter->table_max_len = dev_specs->uc_mac_num;
filter->enabled = true;
hbg_hw_set_mac_filter_enable(priv, filter->enabled);
return 0;
}
static void hbg_init_user_def(struct hbg_priv *priv)
{
struct ethtool_pauseparam *pause_param = &priv->user_def.pause_param;
priv->mac.pause_autoneg = HBG_STATUS_ENABLE;
pause_param->autoneg = priv->mac.pause_autoneg;
hbg_hw_get_pause_enable(priv, &pause_param->tx_pause,
&pause_param->rx_pause);
}
static int hbg_init(struct hbg_priv *priv)
{
int ret;
ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_INIT);
if (ret)
return ret;
ret = hbg_hw_init(priv);
if (ret)
return ret;
ret = hbg_irq_init(priv);
if (ret)
return ret;
ret = hbg_mdio_init(priv);
if (ret)
return ret;
ret = hbg_mac_filter_init(priv);
if (ret)
return ret;
hbg_debugfs_init(priv);
hbg_init_user_def(priv);
return 0;
}
static int hbg_pci_init(struct pci_dev *pdev)
{
struct net_device *netdev = pci_get_drvdata(pdev);
struct hbg_priv *priv = netdev_priv(netdev);
struct device *dev = &pdev->dev;
int ret;
ret = pcim_enable_device(pdev);
if (ret)
return dev_err_probe(dev, ret, "failed to enable PCI device\n");
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
if (ret)
return dev_err_probe(dev, ret, "failed to set PCI DMA mask\n");
ret = pcim_iomap_regions(pdev, BIT(0), dev_driver_string(dev));
if (ret)
return dev_err_probe(dev, ret, "failed to map PCI bar space\n");
priv->io_base = pcim_iomap_table(pdev)[0];
if (!priv->io_base)
return dev_err_probe(dev, -ENOMEM, "failed to get io base\n");
pci_set_master(pdev);
return 0;
}
static int hbg_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
struct device *dev = &pdev->dev;
struct net_device *netdev;
struct hbg_priv *priv;
int ret;
netdev = devm_alloc_etherdev(dev, sizeof(struct hbg_priv));
if (!netdev)
return -ENOMEM;
pci_set_drvdata(pdev, netdev);
SET_NETDEV_DEV(netdev, dev);
priv = netdev_priv(netdev);
priv->netdev = netdev;
priv->pdev = pdev;
ret = hbg_pci_init(pdev);
if (ret)
return ret;
ret = hbg_init(priv);
if (ret)
return ret;
netdev->priv_flags |= IFF_UNICAST_FLT;
netdev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
netdev->max_mtu = priv->dev_specs.max_mtu;
netdev->min_mtu = priv->dev_specs.min_mtu;
netdev->netdev_ops = &hbg_netdev_ops;
netdev->watchdog_timeo = 5 * HZ;
hbg_hw_set_mtu(priv, ETH_DATA_LEN);
hbg_net_set_mac_address(priv->netdev, &priv->dev_specs.mac_addr);
hbg_ethtool_set_ops(netdev);
ret = devm_register_netdev(dev, netdev);
if (ret)
return dev_err_probe(dev, ret, "failed to register netdev\n");
netif_carrier_off(netdev);
return 0;
}
static const struct pci_device_id hbg_pci_tbl[] = {
{PCI_VDEVICE(HUAWEI, 0x3730), 0},
{ }
};
MODULE_DEVICE_TABLE(pci, hbg_pci_tbl);
static struct pci_driver hbg_driver = {
.name = "hibmcge",
.id_table = hbg_pci_tbl,
.probe = hbg_probe,
};
static int __init hbg_module_init(void)
{
int ret;
hbg_debugfs_register();
hbg_set_pci_err_handler(&hbg_driver);
ret = pci_register_driver(&hbg_driver);
if (ret)
hbg_debugfs_unregister();
return ret;
}
module_init(hbg_module_init);
static void __exit hbg_module_exit(void)
{
pci_unregister_driver(&hbg_driver);
hbg_debugfs_unregister();
}
module_exit(hbg_module_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
MODULE_DESCRIPTION("hibmcge driver");
MODULE_VERSION("1.0");
|