1 // SPDX-License-Identifier: GPL-2.0
2 /* sunvnet.c: Sun LDOM Virtual Network Driver.
4 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
5 * Copyright (C) 2016-2017 Oracle. All rights reserved.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/netdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/etherdevice.h>
19 #include <linux/mutex.h>
20 #include <linux/highmem.h>
21 #include <linux/if_vlan.h>
23 #if IS_ENABLED(CONFIG_IPV6)
24 #include <linux/icmpv6.h>
29 #include <net/route.h>
34 #include "sunvnet_common.h"
36 /* length of time before we decide the hardware is borked,
37 * and dev->tx_timeout() should be called to fix the problem
39 #define VNET_TX_TIMEOUT (5 * HZ)
41 #define DRV_MODULE_NAME "sunvnet"
42 #define DRV_MODULE_VERSION "2.0"
43 #define DRV_MODULE_RELDATE "February 3, 2017"
45 static char version
[] =
46 DRV_MODULE_NAME
" " DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")";
47 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
48 MODULE_DESCRIPTION("Sun LDOM virtual network driver");
49 MODULE_LICENSE("GPL");
50 MODULE_VERSION(DRV_MODULE_VERSION
);
52 /* Ordered from largest major to lowest */
53 static struct vio_version vnet_versions
[] = {
54 { .major
= 1, .minor
= 8 },
55 { .major
= 1, .minor
= 7 },
56 { .major
= 1, .minor
= 6 },
57 { .major
= 1, .minor
= 0 },
60 static void vnet_get_drvinfo(struct net_device
*dev
,
61 struct ethtool_drvinfo
*info
)
63 strlcpy(info
->driver
, DRV_MODULE_NAME
, sizeof(info
->driver
));
64 strlcpy(info
->version
, DRV_MODULE_VERSION
, sizeof(info
->version
));
67 static u32
vnet_get_msglevel(struct net_device
*dev
)
69 struct vnet
*vp
= netdev_priv(dev
);
71 return vp
->msg_enable
;
74 static void vnet_set_msglevel(struct net_device
*dev
, u32 value
)
76 struct vnet
*vp
= netdev_priv(dev
);
78 vp
->msg_enable
= value
;
82 const char string
[ETH_GSTRING_LEN
];
83 } ethtool_stats_keys
[] = {
93 { "rx_length_errors" },
94 { "rx_frame_errors" },
95 { "rx_missed_errors" },
96 { "tx_carrier_errors" },
100 static int vnet_get_sset_count(struct net_device
*dev
, int sset
)
102 struct vnet
*vp
= (struct vnet
*)netdev_priv(dev
);
106 return ARRAY_SIZE(ethtool_stats_keys
)
107 + (NUM_VNET_PORT_STATS
* vp
->nports
);
113 static void vnet_get_strings(struct net_device
*dev
, u32 stringset
, u8
*buf
)
115 struct vnet
*vp
= (struct vnet
*)netdev_priv(dev
);
116 struct vnet_port
*port
;
117 char *p
= (char *)buf
;
121 memcpy(buf
, ðtool_stats_keys
, sizeof(ethtool_stats_keys
));
122 p
+= sizeof(ethtool_stats_keys
);
125 list_for_each_entry_rcu(port
, &vp
->port_list
, list
) {
126 snprintf(p
, ETH_GSTRING_LEN
, "p%u.%s-%pM",
127 port
->q_index
, port
->switch_port
? "s" : "q",
129 p
+= ETH_GSTRING_LEN
;
130 snprintf(p
, ETH_GSTRING_LEN
, "p%u.rx_packets",
132 p
+= ETH_GSTRING_LEN
;
133 snprintf(p
, ETH_GSTRING_LEN
, "p%u.tx_packets",
135 p
+= ETH_GSTRING_LEN
;
136 snprintf(p
, ETH_GSTRING_LEN
, "p%u.rx_bytes",
138 p
+= ETH_GSTRING_LEN
;
139 snprintf(p
, ETH_GSTRING_LEN
, "p%u.tx_bytes",
141 p
+= ETH_GSTRING_LEN
;
142 snprintf(p
, ETH_GSTRING_LEN
, "p%u.event_up",
144 p
+= ETH_GSTRING_LEN
;
145 snprintf(p
, ETH_GSTRING_LEN
, "p%u.event_reset",
147 p
+= ETH_GSTRING_LEN
;
157 static void vnet_get_ethtool_stats(struct net_device
*dev
,
158 struct ethtool_stats
*estats
, u64
*data
)
160 struct vnet
*vp
= (struct vnet
*)netdev_priv(dev
);
161 struct vnet_port
*port
;
164 data
[i
++] = dev
->stats
.rx_packets
;
165 data
[i
++] = dev
->stats
.tx_packets
;
166 data
[i
++] = dev
->stats
.rx_bytes
;
167 data
[i
++] = dev
->stats
.tx_bytes
;
168 data
[i
++] = dev
->stats
.rx_errors
;
169 data
[i
++] = dev
->stats
.tx_errors
;
170 data
[i
++] = dev
->stats
.rx_dropped
;
171 data
[i
++] = dev
->stats
.tx_dropped
;
172 data
[i
++] = dev
->stats
.multicast
;
173 data
[i
++] = dev
->stats
.rx_length_errors
;
174 data
[i
++] = dev
->stats
.rx_frame_errors
;
175 data
[i
++] = dev
->stats
.rx_missed_errors
;
176 data
[i
++] = dev
->stats
.tx_carrier_errors
;
177 data
[i
++] = vp
->nports
;
180 list_for_each_entry_rcu(port
, &vp
->port_list
, list
) {
181 data
[i
++] = port
->q_index
;
182 data
[i
++] = port
->stats
.rx_packets
;
183 data
[i
++] = port
->stats
.tx_packets
;
184 data
[i
++] = port
->stats
.rx_bytes
;
185 data
[i
++] = port
->stats
.tx_bytes
;
186 data
[i
++] = port
->stats
.event_up
;
187 data
[i
++] = port
->stats
.event_reset
;
192 static const struct ethtool_ops vnet_ethtool_ops
= {
193 .get_drvinfo
= vnet_get_drvinfo
,
194 .get_msglevel
= vnet_get_msglevel
,
195 .set_msglevel
= vnet_set_msglevel
,
196 .get_link
= ethtool_op_get_link
,
197 .get_sset_count
= vnet_get_sset_count
,
198 .get_strings
= vnet_get_strings
,
199 .get_ethtool_stats
= vnet_get_ethtool_stats
,
202 static LIST_HEAD(vnet_list
);
203 static DEFINE_MUTEX(vnet_list_mutex
);
205 static struct vnet_port
*__tx_port_find(struct vnet
*vp
, struct sk_buff
*skb
)
207 unsigned int hash
= vnet_hashfn(skb
->data
);
208 struct hlist_head
*hp
= &vp
->port_hash
[hash
];
209 struct vnet_port
*port
;
211 hlist_for_each_entry_rcu(port
, hp
, hash
) {
212 if (!sunvnet_port_is_up_common(port
))
214 if (ether_addr_equal(port
->raddr
, skb
->data
))
217 list_for_each_entry_rcu(port
, &vp
->port_list
, list
) {
218 if (!port
->switch_port
)
220 if (!sunvnet_port_is_up_common(port
))
227 /* func arg to vnet_start_xmit_common() to get the proper tx port */
228 static struct vnet_port
*vnet_tx_port_find(struct sk_buff
*skb
,
229 struct net_device
*dev
)
231 struct vnet
*vp
= netdev_priv(dev
);
233 return __tx_port_find(vp
, skb
);
236 static u16
vnet_select_queue(struct net_device
*dev
, struct sk_buff
*skb
,
237 struct net_device
*sb_dev
)
239 struct vnet
*vp
= netdev_priv(dev
);
240 struct vnet_port
*port
= __tx_port_find(vp
, skb
);
245 return port
->q_index
;
248 /* Wrappers to common functions */
249 static netdev_tx_t
vnet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
251 return sunvnet_start_xmit_common(skb
, dev
, vnet_tx_port_find
);
254 static void vnet_set_rx_mode(struct net_device
*dev
)
256 struct vnet
*vp
= netdev_priv(dev
);
258 return sunvnet_set_rx_mode_common(dev
, vp
);
261 #ifdef CONFIG_NET_POLL_CONTROLLER
262 static void vnet_poll_controller(struct net_device
*dev
)
264 struct vnet
*vp
= netdev_priv(dev
);
266 return sunvnet_poll_controller_common(dev
, vp
);
270 static const struct net_device_ops vnet_ops
= {
271 .ndo_open
= sunvnet_open_common
,
272 .ndo_stop
= sunvnet_close_common
,
273 .ndo_set_rx_mode
= vnet_set_rx_mode
,
274 .ndo_set_mac_address
= sunvnet_set_mac_addr_common
,
275 .ndo_validate_addr
= eth_validate_addr
,
276 .ndo_tx_timeout
= sunvnet_tx_timeout_common
,
277 .ndo_start_xmit
= vnet_start_xmit
,
278 .ndo_select_queue
= vnet_select_queue
,
279 #ifdef CONFIG_NET_POLL_CONTROLLER
280 .ndo_poll_controller
= vnet_poll_controller
,
284 static struct vnet
*vnet_new(const u64
*local_mac
,
285 struct vio_dev
*vdev
)
287 struct net_device
*dev
;
291 dev
= alloc_etherdev_mqs(sizeof(*vp
), VNET_MAX_TXQS
, 1);
293 return ERR_PTR(-ENOMEM
);
294 dev
->needed_headroom
= VNET_PACKET_SKIP
+ 8;
295 dev
->needed_tailroom
= 8;
297 for (i
= 0; i
< ETH_ALEN
; i
++)
298 dev
->dev_addr
[i
] = (*local_mac
>> (5 - i
) * 8) & 0xff;
300 vp
= netdev_priv(dev
);
302 spin_lock_init(&vp
->lock
);
305 INIT_LIST_HEAD(&vp
->port_list
);
306 for (i
= 0; i
< VNET_PORT_HASH_SIZE
; i
++)
307 INIT_HLIST_HEAD(&vp
->port_hash
[i
]);
308 INIT_LIST_HEAD(&vp
->list
);
309 vp
->local_mac
= *local_mac
;
311 dev
->netdev_ops
= &vnet_ops
;
312 dev
->ethtool_ops
= &vnet_ethtool_ops
;
313 dev
->watchdog_timeo
= VNET_TX_TIMEOUT
;
315 dev
->hw_features
= NETIF_F_TSO
| NETIF_F_GSO
| NETIF_F_ALL_TSO
|
316 NETIF_F_HW_CSUM
| NETIF_F_SG
;
317 dev
->features
= dev
->hw_features
;
319 /* MTU range: 68 - 65535 */
320 dev
->min_mtu
= ETH_MIN_MTU
;
321 dev
->max_mtu
= VNET_MAX_MTU
;
323 SET_NETDEV_DEV(dev
, &vdev
->dev
);
325 err
= register_netdev(dev
);
327 pr_err("Cannot register net device, aborting\n");
328 goto err_out_free_dev
;
331 netdev_info(dev
, "Sun LDOM vnet %pM\n", dev
->dev_addr
);
333 list_add(&vp
->list
, &vnet_list
);
343 static struct vnet
*vnet_find_or_create(const u64
*local_mac
,
344 struct vio_dev
*vdev
)
346 struct vnet
*iter
, *vp
;
348 mutex_lock(&vnet_list_mutex
);
350 list_for_each_entry(iter
, &vnet_list
, list
) {
351 if (iter
->local_mac
== *local_mac
) {
357 vp
= vnet_new(local_mac
, vdev
);
358 mutex_unlock(&vnet_list_mutex
);
363 static void vnet_cleanup(void)
366 struct net_device
*dev
;
368 mutex_lock(&vnet_list_mutex
);
369 while (!list_empty(&vnet_list
)) {
370 vp
= list_first_entry(&vnet_list
, struct vnet
, list
);
373 /* vio_unregister_driver() should have cleaned up port_list */
374 BUG_ON(!list_empty(&vp
->port_list
));
375 unregister_netdev(dev
);
378 mutex_unlock(&vnet_list_mutex
);
381 static const char *local_mac_prop
= "local-mac-address";
383 static struct vnet
*vnet_find_parent(struct mdesc_handle
*hp
,
385 struct vio_dev
*vdev
)
387 const u64
*local_mac
= NULL
;
390 mdesc_for_each_arc(a
, hp
, port_node
, MDESC_ARC_TYPE_BACK
) {
391 u64 target
= mdesc_arc_target(hp
, a
);
394 name
= mdesc_get_property(hp
, target
, "name", NULL
);
395 if (!name
|| strcmp(name
, "network"))
398 local_mac
= mdesc_get_property(hp
, target
,
399 local_mac_prop
, NULL
);
404 return ERR_PTR(-ENODEV
);
406 return vnet_find_or_create(local_mac
, vdev
);
409 static struct ldc_channel_config vnet_ldc_cfg
= {
410 .event
= sunvnet_event_common
,
412 .mode
= LDC_MODE_UNRELIABLE
,
415 static struct vio_driver_ops vnet_vio_ops
= {
416 .send_attr
= sunvnet_send_attr_common
,
417 .handle_attr
= sunvnet_handle_attr_common
,
418 .handshake_complete
= sunvnet_handshake_complete_common
,
421 const char *remote_macaddr_prop
= "remote-mac-address";
423 static int vnet_port_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
425 struct mdesc_handle
*hp
;
426 struct vnet_port
*port
;
430 int len
, i
, err
, switch_port
;
434 vp
= vnet_find_parent(hp
, vdev
->mp
, vdev
);
436 pr_err("Cannot find port parent vnet\n");
438 goto err_out_put_mdesc
;
441 rmac
= mdesc_get_property(hp
, vdev
->mp
, remote_macaddr_prop
, &len
);
444 pr_err("Port lacks %s property\n", remote_macaddr_prop
);
445 goto err_out_put_mdesc
;
448 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
451 goto err_out_put_mdesc
;
453 for (i
= 0; i
< ETH_ALEN
; i
++)
454 port
->raddr
[i
] = (*rmac
>> (5 - i
) * 8) & 0xff;
458 err
= vio_driver_init(&port
->vio
, vdev
, VDEV_NETWORK
,
459 vnet_versions
, ARRAY_SIZE(vnet_versions
),
460 &vnet_vio_ops
, vp
->dev
->name
);
462 goto err_out_free_port
;
464 err
= vio_ldc_alloc(&port
->vio
, &vnet_ldc_cfg
, port
);
466 goto err_out_free_port
;
468 netif_napi_add(port
->vp
->dev
, &port
->napi
, sunvnet_poll_common
,
471 INIT_HLIST_NODE(&port
->hash
);
472 INIT_LIST_HEAD(&port
->list
);
475 if (mdesc_get_property(hp
, vdev
->mp
, "switch-port", NULL
))
477 port
->switch_port
= switch_port
;
481 spin_lock_irqsave(&vp
->lock
, flags
);
483 list_add_rcu(&port
->list
, &vp
->port_list
);
485 list_add_tail_rcu(&port
->list
, &vp
->port_list
);
486 hlist_add_head_rcu(&port
->hash
,
487 &vp
->port_hash
[vnet_hashfn(port
->raddr
)]);
488 sunvnet_port_add_txq_common(port
);
489 spin_unlock_irqrestore(&vp
->lock
, flags
);
491 dev_set_drvdata(&vdev
->dev
, port
);
493 pr_info("%s: PORT ( remote-mac %pM%s )\n",
494 vp
->dev
->name
, port
->raddr
, switch_port
? " switch-port" : "");
496 timer_setup(&port
->clean_timer
, sunvnet_clean_timer_expire_common
, 0);
498 napi_enable(&port
->napi
);
499 vio_port_up(&port
->vio
);
513 static int vnet_port_remove(struct vio_dev
*vdev
)
515 struct vnet_port
*port
= dev_get_drvdata(&vdev
->dev
);
518 del_timer_sync(&port
->vio
.timer
);
520 napi_disable(&port
->napi
);
522 list_del_rcu(&port
->list
);
523 hlist_del_rcu(&port
->hash
);
526 del_timer_sync(&port
->clean_timer
);
527 sunvnet_port_rm_txq_common(port
);
528 netif_napi_del(&port
->napi
);
529 sunvnet_port_free_tx_bufs_common(port
);
530 vio_ldc_free(&port
->vio
);
532 dev_set_drvdata(&vdev
->dev
, NULL
);
539 static const struct vio_device_id vnet_port_match
[] = {
545 MODULE_DEVICE_TABLE(vio
, vnet_port_match
);
547 static struct vio_driver vnet_port_driver
= {
548 .id_table
= vnet_port_match
,
549 .probe
= vnet_port_probe
,
550 .remove
= vnet_port_remove
,
554 static int __init
vnet_init(void)
556 pr_info("%s\n", version
);
557 return vio_register_driver(&vnet_port_driver
);
560 static void __exit
vnet_exit(void)
562 vio_unregister_driver(&vnet_port_driver
);
566 module_init(vnet_init
);
567 module_exit(vnet_exit
);