xref: /linux/drivers/net/ethernet/sun/sunvnet.c (revision 9410645520e9b820069761f3450ef6661418e279)
1 // SPDX-License-Identifier: GPL-2.0
2 /* sunvnet.c: Sun LDOM Virtual Network Driver.
3  *
4  * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
5  * Copyright (C) 2016-2017 Oracle. All rights reserved.
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
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>
22 
23 #if IS_ENABLED(CONFIG_IPV6)
24 #include <linux/icmpv6.h>
25 #endif
26 
27 #include <net/ip.h>
28 #include <net/icmp.h>
29 #include <net/route.h>
30 
31 #include <asm/vio.h>
32 #include <asm/ldc.h>
33 
34 #include "sunvnet_common.h"
35 
36 /* length of time before we decide the hardware is borked,
37  * and dev->tx_timeout() should be called to fix the problem
38  */
39 #define VNET_TX_TIMEOUT			(5 * HZ)
40 
41 #define DRV_MODULE_NAME		"sunvnet"
42 #define DRV_MODULE_VERSION	"2.0"
43 #define DRV_MODULE_RELDATE	"February 3, 2017"
44 
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);
51 
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 },
58 };
59 
vnet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)60 static void vnet_get_drvinfo(struct net_device *dev,
61 			     struct ethtool_drvinfo *info)
62 {
63 	strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
64 	strscpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
65 }
66 
vnet_get_msglevel(struct net_device * dev)67 static u32 vnet_get_msglevel(struct net_device *dev)
68 {
69 	struct vnet *vp = netdev_priv(dev);
70 
71 	return vp->msg_enable;
72 }
73 
vnet_set_msglevel(struct net_device * dev,u32 value)74 static void vnet_set_msglevel(struct net_device *dev, u32 value)
75 {
76 	struct vnet *vp = netdev_priv(dev);
77 
78 	vp->msg_enable = value;
79 }
80 
81 static const struct {
82 	const char string[ETH_GSTRING_LEN];
83 } ethtool_stats_keys[] = {
84 	{ "rx_packets" },
85 	{ "tx_packets" },
86 	{ "rx_bytes" },
87 	{ "tx_bytes" },
88 	{ "rx_errors" },
89 	{ "tx_errors" },
90 	{ "rx_dropped" },
91 	{ "tx_dropped" },
92 	{ "multicast" },
93 	{ "rx_length_errors" },
94 	{ "rx_frame_errors" },
95 	{ "rx_missed_errors" },
96 	{ "tx_carrier_errors" },
97 	{ "nports" },
98 };
99 
vnet_get_sset_count(struct net_device * dev,int sset)100 static int vnet_get_sset_count(struct net_device *dev, int sset)
101 {
102 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
103 
104 	switch (sset) {
105 	case ETH_SS_STATS:
106 		return ARRAY_SIZE(ethtool_stats_keys)
107 			+ (NUM_VNET_PORT_STATS * vp->nports);
108 	default:
109 		return -EOPNOTSUPP;
110 	}
111 }
112 
vnet_get_strings(struct net_device * dev,u32 stringset,u8 * buf)113 static void vnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
114 {
115 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
116 	struct vnet_port *port;
117 
118 	switch (stringset) {
119 	case ETH_SS_STATS:
120 		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
121 		buf += sizeof(ethtool_stats_keys);
122 
123 		rcu_read_lock();
124 		list_for_each_entry_rcu(port, &vp->port_list, list) {
125 			ethtool_sprintf(&buf, "p%u.%s-%pM", port->q_index,
126 					port->switch_port ? "s" : "q",
127 					port->raddr);
128 			ethtool_sprintf(&buf, "p%u.rx_packets", port->q_index);
129 			ethtool_sprintf(&buf, "p%u.tx_packets", port->q_index);
130 			ethtool_sprintf(&buf, "p%u.rx_bytes", port->q_index);
131 			ethtool_sprintf(&buf, "p%u.tx_bytes", port->q_index);
132 			ethtool_sprintf(&buf, "p%u.event_up", port->q_index);
133 			ethtool_sprintf(&buf, "p%u.event_reset", port->q_index);
134 		}
135 		rcu_read_unlock();
136 		break;
137 	default:
138 		WARN_ON(1);
139 		break;
140 	}
141 }
142 
vnet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * estats,u64 * data)143 static void vnet_get_ethtool_stats(struct net_device *dev,
144 				   struct ethtool_stats *estats, u64 *data)
145 {
146 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
147 	struct vnet_port *port;
148 	int i = 0;
149 
150 	data[i++] = dev->stats.rx_packets;
151 	data[i++] = dev->stats.tx_packets;
152 	data[i++] = dev->stats.rx_bytes;
153 	data[i++] = dev->stats.tx_bytes;
154 	data[i++] = dev->stats.rx_errors;
155 	data[i++] = dev->stats.tx_errors;
156 	data[i++] = dev->stats.rx_dropped;
157 	data[i++] = dev->stats.tx_dropped;
158 	data[i++] = dev->stats.multicast;
159 	data[i++] = dev->stats.rx_length_errors;
160 	data[i++] = dev->stats.rx_frame_errors;
161 	data[i++] = dev->stats.rx_missed_errors;
162 	data[i++] = dev->stats.tx_carrier_errors;
163 	data[i++] = vp->nports;
164 
165 	rcu_read_lock();
166 	list_for_each_entry_rcu(port, &vp->port_list, list) {
167 		data[i++] = port->q_index;
168 		data[i++] = port->stats.rx_packets;
169 		data[i++] = port->stats.tx_packets;
170 		data[i++] = port->stats.rx_bytes;
171 		data[i++] = port->stats.tx_bytes;
172 		data[i++] = port->stats.event_up;
173 		data[i++] = port->stats.event_reset;
174 	}
175 	rcu_read_unlock();
176 }
177 
178 static const struct ethtool_ops vnet_ethtool_ops = {
179 	.get_drvinfo		= vnet_get_drvinfo,
180 	.get_msglevel		= vnet_get_msglevel,
181 	.set_msglevel		= vnet_set_msglevel,
182 	.get_link		= ethtool_op_get_link,
183 	.get_sset_count		= vnet_get_sset_count,
184 	.get_strings		= vnet_get_strings,
185 	.get_ethtool_stats	= vnet_get_ethtool_stats,
186 };
187 
188 static LIST_HEAD(vnet_list);
189 static DEFINE_MUTEX(vnet_list_mutex);
190 
__tx_port_find(struct vnet * vp,struct sk_buff * skb)191 static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
192 {
193 	unsigned int hash = vnet_hashfn(skb->data);
194 	struct hlist_head *hp = &vp->port_hash[hash];
195 	struct vnet_port *port;
196 
197 	hlist_for_each_entry_rcu(port, hp, hash) {
198 		if (!sunvnet_port_is_up_common(port))
199 			continue;
200 		if (ether_addr_equal(port->raddr, skb->data))
201 			return port;
202 	}
203 	list_for_each_entry_rcu(port, &vp->port_list, list) {
204 		if (!port->switch_port)
205 			continue;
206 		if (!sunvnet_port_is_up_common(port))
207 			continue;
208 		return port;
209 	}
210 	return NULL;
211 }
212 
213 /* func arg to vnet_start_xmit_common() to get the proper tx port */
vnet_tx_port_find(struct sk_buff * skb,struct net_device * dev)214 static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb,
215 					   struct net_device *dev)
216 {
217 	struct vnet *vp = netdev_priv(dev);
218 
219 	return __tx_port_find(vp, skb);
220 }
221 
vnet_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)222 static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
223 			     struct net_device *sb_dev)
224 {
225 	struct vnet *vp = netdev_priv(dev);
226 	struct vnet_port *port = __tx_port_find(vp, skb);
227 
228 	if (!port)
229 		return 0;
230 
231 	return port->q_index;
232 }
233 
234 /* Wrappers to common functions */
vnet_start_xmit(struct sk_buff * skb,struct net_device * dev)235 static netdev_tx_t vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
236 {
237 	return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find);
238 }
239 
vnet_set_rx_mode(struct net_device * dev)240 static void vnet_set_rx_mode(struct net_device *dev)
241 {
242 	struct vnet *vp = netdev_priv(dev);
243 
244 	return sunvnet_set_rx_mode_common(dev, vp);
245 }
246 
247 #ifdef CONFIG_NET_POLL_CONTROLLER
vnet_poll_controller(struct net_device * dev)248 static void vnet_poll_controller(struct net_device *dev)
249 {
250 	struct vnet *vp = netdev_priv(dev);
251 
252 	return sunvnet_poll_controller_common(dev, vp);
253 }
254 #endif
255 
256 static const struct net_device_ops vnet_ops = {
257 	.ndo_open		= sunvnet_open_common,
258 	.ndo_stop		= sunvnet_close_common,
259 	.ndo_set_rx_mode	= vnet_set_rx_mode,
260 	.ndo_set_mac_address	= sunvnet_set_mac_addr_common,
261 	.ndo_validate_addr	= eth_validate_addr,
262 	.ndo_tx_timeout		= sunvnet_tx_timeout_common,
263 	.ndo_start_xmit		= vnet_start_xmit,
264 	.ndo_select_queue	= vnet_select_queue,
265 #ifdef CONFIG_NET_POLL_CONTROLLER
266 	.ndo_poll_controller	= vnet_poll_controller,
267 #endif
268 };
269 
vnet_new(const u64 * local_mac,struct vio_dev * vdev)270 static struct vnet *vnet_new(const u64 *local_mac,
271 			     struct vio_dev *vdev)
272 {
273 	struct net_device *dev;
274 	u8 addr[ETH_ALEN];
275 	struct vnet *vp;
276 	int err, i;
277 
278 	dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
279 	if (!dev)
280 		return ERR_PTR(-ENOMEM);
281 	dev->needed_headroom = VNET_PACKET_SKIP + 8;
282 	dev->needed_tailroom = 8;
283 
284 	for (i = 0; i < ETH_ALEN; i++)
285 		addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
286 	eth_hw_addr_set(dev, addr);
287 
288 	vp = netdev_priv(dev);
289 
290 	spin_lock_init(&vp->lock);
291 	vp->dev = dev;
292 
293 	INIT_LIST_HEAD(&vp->port_list);
294 	for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
295 		INIT_HLIST_HEAD(&vp->port_hash[i]);
296 	INIT_LIST_HEAD(&vp->list);
297 	vp->local_mac = *local_mac;
298 
299 	dev->netdev_ops = &vnet_ops;
300 	dev->ethtool_ops = &vnet_ethtool_ops;
301 	dev->watchdog_timeo = VNET_TX_TIMEOUT;
302 
303 	dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_ALL_TSO |
304 			   NETIF_F_HW_CSUM | NETIF_F_SG;
305 	dev->features = dev->hw_features;
306 
307 	/* MTU range: 68 - 65535 */
308 	dev->min_mtu = ETH_MIN_MTU;
309 	dev->max_mtu = VNET_MAX_MTU;
310 
311 	SET_NETDEV_DEV(dev, &vdev->dev);
312 
313 	err = register_netdev(dev);
314 	if (err) {
315 		pr_err("Cannot register net device, aborting\n");
316 		goto err_out_free_dev;
317 	}
318 
319 	netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
320 
321 	list_add(&vp->list, &vnet_list);
322 
323 	return vp;
324 
325 err_out_free_dev:
326 	free_netdev(dev);
327 
328 	return ERR_PTR(err);
329 }
330 
vnet_find_or_create(const u64 * local_mac,struct vio_dev * vdev)331 static struct vnet *vnet_find_or_create(const u64 *local_mac,
332 					struct vio_dev *vdev)
333 {
334 	struct vnet *iter, *vp;
335 
336 	mutex_lock(&vnet_list_mutex);
337 	vp = NULL;
338 	list_for_each_entry(iter, &vnet_list, list) {
339 		if (iter->local_mac == *local_mac) {
340 			vp = iter;
341 			break;
342 		}
343 	}
344 	if (!vp)
345 		vp = vnet_new(local_mac, vdev);
346 	mutex_unlock(&vnet_list_mutex);
347 
348 	return vp;
349 }
350 
vnet_cleanup(void)351 static void vnet_cleanup(void)
352 {
353 	struct vnet *vp;
354 	struct net_device *dev;
355 
356 	mutex_lock(&vnet_list_mutex);
357 	while (!list_empty(&vnet_list)) {
358 		vp = list_first_entry(&vnet_list, struct vnet, list);
359 		list_del(&vp->list);
360 		dev = vp->dev;
361 		/* vio_unregister_driver() should have cleaned up port_list */
362 		BUG_ON(!list_empty(&vp->port_list));
363 		unregister_netdev(dev);
364 		free_netdev(dev);
365 	}
366 	mutex_unlock(&vnet_list_mutex);
367 }
368 
369 static const char *local_mac_prop = "local-mac-address";
370 
vnet_find_parent(struct mdesc_handle * hp,u64 port_node,struct vio_dev * vdev)371 static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
372 				     u64 port_node,
373 				     struct vio_dev *vdev)
374 {
375 	const u64 *local_mac = NULL;
376 	u64 a;
377 
378 	mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
379 		u64 target = mdesc_arc_target(hp, a);
380 		const char *name;
381 
382 		name = mdesc_get_property(hp, target, "name", NULL);
383 		if (!name || strcmp(name, "network"))
384 			continue;
385 
386 		local_mac = mdesc_get_property(hp, target,
387 					       local_mac_prop, NULL);
388 		if (local_mac)
389 			break;
390 	}
391 	if (!local_mac)
392 		return ERR_PTR(-ENODEV);
393 
394 	return vnet_find_or_create(local_mac, vdev);
395 }
396 
397 static struct ldc_channel_config vnet_ldc_cfg = {
398 	.event		= sunvnet_event_common,
399 	.mtu		= 64,
400 	.mode		= LDC_MODE_UNRELIABLE,
401 };
402 
403 static struct vio_driver_ops vnet_vio_ops = {
404 	.send_attr		= sunvnet_send_attr_common,
405 	.handle_attr		= sunvnet_handle_attr_common,
406 	.handshake_complete	= sunvnet_handshake_complete_common,
407 };
408 
409 const char *remote_macaddr_prop = "remote-mac-address";
410 
vnet_port_probe(struct vio_dev * vdev,const struct vio_device_id * id)411 static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
412 {
413 	struct mdesc_handle *hp;
414 	struct vnet_port *port;
415 	unsigned long flags;
416 	struct vnet *vp;
417 	const u64 *rmac;
418 	int len, i, err, switch_port;
419 
420 	hp = mdesc_grab();
421 
422 	if (!hp)
423 		return -ENODEV;
424 
425 	vp = vnet_find_parent(hp, vdev->mp, vdev);
426 	if (IS_ERR(vp)) {
427 		pr_err("Cannot find port parent vnet\n");
428 		err = PTR_ERR(vp);
429 		goto err_out_put_mdesc;
430 	}
431 
432 	rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
433 	err = -ENODEV;
434 	if (!rmac) {
435 		pr_err("Port lacks %s property\n", remote_macaddr_prop);
436 		goto err_out_put_mdesc;
437 	}
438 
439 	port = kzalloc(sizeof(*port), GFP_KERNEL);
440 	err = -ENOMEM;
441 	if (!port)
442 		goto err_out_put_mdesc;
443 
444 	for (i = 0; i < ETH_ALEN; i++)
445 		port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
446 
447 	port->vp = vp;
448 
449 	err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
450 			      vnet_versions, ARRAY_SIZE(vnet_versions),
451 			      &vnet_vio_ops, vp->dev->name);
452 	if (err)
453 		goto err_out_free_port;
454 
455 	err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
456 	if (err)
457 		goto err_out_free_port;
458 
459 	netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common);
460 
461 	INIT_HLIST_NODE(&port->hash);
462 	INIT_LIST_HEAD(&port->list);
463 
464 	switch_port = 0;
465 	if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL))
466 		switch_port = 1;
467 	port->switch_port = switch_port;
468 	port->tso = true;
469 	port->tsolen = 0;
470 
471 	spin_lock_irqsave(&vp->lock, flags);
472 	if (switch_port)
473 		list_add_rcu(&port->list, &vp->port_list);
474 	else
475 		list_add_tail_rcu(&port->list, &vp->port_list);
476 	hlist_add_head_rcu(&port->hash,
477 			   &vp->port_hash[vnet_hashfn(port->raddr)]);
478 	sunvnet_port_add_txq_common(port);
479 	spin_unlock_irqrestore(&vp->lock, flags);
480 
481 	dev_set_drvdata(&vdev->dev, port);
482 
483 	pr_info("%s: PORT ( remote-mac %pM%s )\n",
484 		vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
485 
486 	timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0);
487 
488 	napi_enable(&port->napi);
489 	vio_port_up(&port->vio);
490 
491 	mdesc_release(hp);
492 
493 	return 0;
494 
495 err_out_free_port:
496 	kfree(port);
497 
498 err_out_put_mdesc:
499 	mdesc_release(hp);
500 	return err;
501 }
502 
vnet_port_remove(struct vio_dev * vdev)503 static void vnet_port_remove(struct vio_dev *vdev)
504 {
505 	struct vnet_port *port = dev_get_drvdata(&vdev->dev);
506 
507 	if (port) {
508 		del_timer_sync(&port->vio.timer);
509 
510 		napi_disable(&port->napi);
511 
512 		list_del_rcu(&port->list);
513 		hlist_del_rcu(&port->hash);
514 
515 		synchronize_rcu();
516 		timer_shutdown_sync(&port->clean_timer);
517 		sunvnet_port_rm_txq_common(port);
518 		netif_napi_del(&port->napi);
519 		sunvnet_port_free_tx_bufs_common(port);
520 		vio_ldc_free(&port->vio);
521 
522 		dev_set_drvdata(&vdev->dev, NULL);
523 
524 		kfree(port);
525 	}
526 }
527 
528 static const struct vio_device_id vnet_port_match[] = {
529 	{
530 		.type = "vnet-port",
531 	},
532 	{},
533 };
534 MODULE_DEVICE_TABLE(vio, vnet_port_match);
535 
536 static struct vio_driver vnet_port_driver = {
537 	.id_table	= vnet_port_match,
538 	.probe		= vnet_port_probe,
539 	.remove		= vnet_port_remove,
540 	.name		= "vnet_port",
541 };
542 
vnet_init(void)543 static int __init vnet_init(void)
544 {
545 	pr_info("%s\n", version);
546 	return vio_register_driver(&vnet_port_driver);
547 }
548 
vnet_exit(void)549 static void __exit vnet_exit(void)
550 {
551 	vio_unregister_driver(&vnet_port_driver);
552 	vnet_cleanup();
553 }
554 
555 module_init(vnet_init);
556 module_exit(vnet_exit);
557