xref: /openbmc/linux/drivers/net/ethernet/sun/ldmvsw.c (revision 82003e04)
1 /* ldmvsw.c: Sun4v LDOM Virtual Switch Driver.
2  *
3  * Copyright (C) 2016 Oracle. All rights reserved.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/delay.h>
9 #include <linux/etherdevice.h>
10 #include <linux/ethtool.h>
11 #include <linux/highmem.h>
12 #include <linux/if_vlan.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/netdevice.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 
21 #if defined(CONFIG_IPV6)
22 #include <linux/icmpv6.h>
23 #endif
24 
25 #include <net/ip.h>
26 #include <net/icmp.h>
27 #include <net/route.h>
28 
29 #include <asm/vio.h>
30 #include <asm/ldc.h>
31 
32 /* This driver makes use of the common code in sunvnet_common.c */
33 #include "sunvnet_common.h"
34 
35 /* Length of time before we decide the hardware is hung,
36  * and dev->tx_timeout() should be called to fix the problem.
37  */
38 #define VSW_TX_TIMEOUT			(10 * HZ)
39 
40 /* Static HW Addr used for the network interfaces representing vsw ports */
41 static u8 vsw_port_hwaddr[ETH_ALEN] = {0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
42 
43 #define DRV_MODULE_NAME		"ldmvsw"
44 #define DRV_MODULE_VERSION	"1.0"
45 #define DRV_MODULE_RELDATE	"Jan 15, 2016"
46 
47 static char version[] =
48 	DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
49 MODULE_AUTHOR("Oracle");
50 MODULE_DESCRIPTION("Sun4v LDOM Virtual Switch Driver");
51 MODULE_LICENSE("GPL");
52 MODULE_VERSION(DRV_MODULE_VERSION);
53 
54 /* Ordered from largest major to lowest */
55 static struct vio_version vsw_versions[] = {
56 	{ .major = 1, .minor = 8 },
57 	{ .major = 1, .minor = 7 },
58 	{ .major = 1, .minor = 6 },
59 	{ .major = 1, .minor = 0 },
60 };
61 
62 static void vsw_get_drvinfo(struct net_device *dev,
63 			    struct ethtool_drvinfo *info)
64 {
65 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
66 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
67 }
68 
69 static u32 vsw_get_msglevel(struct net_device *dev)
70 {
71 	struct vnet_port *port = netdev_priv(dev);
72 
73 	return port->vp->msg_enable;
74 }
75 
76 static void vsw_set_msglevel(struct net_device *dev, u32 value)
77 {
78 	struct vnet_port *port = netdev_priv(dev);
79 
80 	port->vp->msg_enable = value;
81 }
82 
83 static const struct ethtool_ops vsw_ethtool_ops = {
84 	.get_drvinfo		= vsw_get_drvinfo,
85 	.get_msglevel		= vsw_get_msglevel,
86 	.set_msglevel		= vsw_set_msglevel,
87 	.get_link		= ethtool_op_get_link,
88 };
89 
90 static LIST_HEAD(vnet_list);
91 static DEFINE_MUTEX(vnet_list_mutex);
92 
93 /* func arg to vnet_start_xmit_common() to get the proper tx port */
94 static struct vnet_port *vsw_tx_port_find(struct sk_buff *skb,
95 					  struct net_device *dev)
96 {
97 	struct vnet_port *port = netdev_priv(dev);
98 
99 	return port;
100 }
101 
102 static u16 vsw_select_queue(struct net_device *dev, struct sk_buff *skb,
103 			    void *accel_priv, select_queue_fallback_t fallback)
104 {
105 	struct vnet_port *port = netdev_priv(dev);
106 
107 	if (!port)
108 		return 0;
109 
110 	return port->q_index;
111 }
112 
113 /* Wrappers to common functions */
114 static int vsw_start_xmit(struct sk_buff *skb, struct net_device *dev)
115 {
116 	return sunvnet_start_xmit_common(skb, dev, vsw_tx_port_find);
117 }
118 
119 static void vsw_set_rx_mode(struct net_device *dev)
120 {
121 	struct vnet_port *port = netdev_priv(dev);
122 
123 	return sunvnet_set_rx_mode_common(dev, port->vp);
124 }
125 
126 #ifdef CONFIG_NET_POLL_CONTROLLER
127 static void vsw_poll_controller(struct net_device *dev)
128 {
129 	struct vnet_port *port = netdev_priv(dev);
130 
131 	return sunvnet_poll_controller_common(dev, port->vp);
132 }
133 #endif
134 
135 static const struct net_device_ops vsw_ops = {
136 	.ndo_open		= sunvnet_open_common,
137 	.ndo_stop		= sunvnet_close_common,
138 	.ndo_set_rx_mode	= vsw_set_rx_mode,
139 	.ndo_set_mac_address	= sunvnet_set_mac_addr_common,
140 	.ndo_validate_addr	= eth_validate_addr,
141 	.ndo_tx_timeout		= sunvnet_tx_timeout_common,
142 	.ndo_change_mtu		= sunvnet_change_mtu_common,
143 	.ndo_start_xmit		= vsw_start_xmit,
144 	.ndo_select_queue	= vsw_select_queue,
145 #ifdef CONFIG_NET_POLL_CONTROLLER
146 	.ndo_poll_controller    = vsw_poll_controller,
147 #endif
148 };
149 
150 static const char *local_mac_prop = "local-mac-address";
151 static const char *cfg_handle_prop = "cfg-handle";
152 
153 static struct vnet *vsw_get_vnet(struct mdesc_handle *hp,
154 				 u64 port_node,
155 				 u64 *handle)
156 {
157 	struct vnet *vp;
158 	struct vnet *iter;
159 	const u64 *local_mac = NULL;
160 	const u64 *cfghandle = NULL;
161 	u64 a;
162 
163 	/* Get the parent virtual-network-switch macaddr and cfghandle */
164 	mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
165 		u64 target = mdesc_arc_target(hp, a);
166 		const char *name;
167 
168 		name = mdesc_get_property(hp, target, "name", NULL);
169 		if (!name || strcmp(name, "virtual-network-switch"))
170 			continue;
171 
172 		local_mac = mdesc_get_property(hp, target,
173 					       local_mac_prop, NULL);
174 		cfghandle = mdesc_get_property(hp, target,
175 					       cfg_handle_prop, NULL);
176 		break;
177 	}
178 	if (!local_mac || !cfghandle)
179 		return ERR_PTR(-ENODEV);
180 
181 	/* find or create associated vnet */
182 	vp = NULL;
183 	mutex_lock(&vnet_list_mutex);
184 	list_for_each_entry(iter, &vnet_list, list) {
185 		if (iter->local_mac == *local_mac) {
186 			vp = iter;
187 			break;
188 		}
189 	}
190 
191 	if (!vp) {
192 		vp = kzalloc(sizeof(*vp), GFP_KERNEL);
193 		if (unlikely(!vp)) {
194 			mutex_unlock(&vnet_list_mutex);
195 			return ERR_PTR(-ENOMEM);
196 		}
197 
198 		spin_lock_init(&vp->lock);
199 		INIT_LIST_HEAD(&vp->port_list);
200 		INIT_LIST_HEAD(&vp->list);
201 		vp->local_mac = *local_mac;
202 		list_add(&vp->list, &vnet_list);
203 	}
204 
205 	mutex_unlock(&vnet_list_mutex);
206 
207 	*handle = (u64)*cfghandle;
208 
209 	return vp;
210 }
211 
212 static struct net_device *vsw_alloc_netdev(u8 hwaddr[],
213 					   struct vio_dev *vdev,
214 					   u64 handle,
215 					   u64 port_id)
216 {
217 	struct net_device *dev;
218 	struct vnet_port *port;
219 	int i;
220 
221 	dev = alloc_etherdev_mqs(sizeof(*port), VNET_MAX_TXQS, 1);
222 	if (!dev)
223 		return ERR_PTR(-ENOMEM);
224 	dev->needed_headroom = VNET_PACKET_SKIP + 8;
225 	dev->needed_tailroom = 8;
226 
227 	for (i = 0; i < ETH_ALEN; i++) {
228 		dev->dev_addr[i] = hwaddr[i];
229 		dev->perm_addr[i] = dev->dev_addr[i];
230 	}
231 
232 	sprintf(dev->name, "vif%d.%d", (int)handle, (int)port_id);
233 
234 	dev->netdev_ops = &vsw_ops;
235 	dev->ethtool_ops = &vsw_ethtool_ops;
236 	dev->watchdog_timeo = VSW_TX_TIMEOUT;
237 
238 	dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_GSO_SOFTWARE |
239 			   NETIF_F_HW_CSUM | NETIF_F_SG;
240 	dev->features = dev->hw_features;
241 
242 	SET_NETDEV_DEV(dev, &vdev->dev);
243 
244 	return dev;
245 }
246 
247 static struct ldc_channel_config vsw_ldc_cfg = {
248 	.event		= sunvnet_event_common,
249 	.mtu		= 64,
250 	.mode		= LDC_MODE_UNRELIABLE,
251 };
252 
253 static struct vio_driver_ops vsw_vio_ops = {
254 	.send_attr		= sunvnet_send_attr_common,
255 	.handle_attr		= sunvnet_handle_attr_common,
256 	.handshake_complete	= sunvnet_handshake_complete_common,
257 };
258 
259 static void print_version(void)
260 {
261 	printk_once(KERN_INFO "%s", version);
262 }
263 
264 static const char *remote_macaddr_prop = "remote-mac-address";
265 static const char *id_prop = "id";
266 
267 static int vsw_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
268 {
269 	struct mdesc_handle *hp;
270 	struct vnet_port *port;
271 	unsigned long flags;
272 	struct vnet *vp;
273 	struct net_device *dev;
274 	const u64 *rmac;
275 	int len, i, err;
276 	const u64 *port_id;
277 	u64 handle;
278 
279 	print_version();
280 
281 	hp = mdesc_grab();
282 
283 	rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
284 	err = -ENODEV;
285 	if (!rmac) {
286 		pr_err("Port lacks %s property\n", remote_macaddr_prop);
287 		mdesc_release(hp);
288 		return err;
289 	}
290 
291 	port_id = mdesc_get_property(hp, vdev->mp, id_prop, NULL);
292 	err = -ENODEV;
293 	if (!port_id) {
294 		pr_err("Port lacks %s property\n", id_prop);
295 		mdesc_release(hp);
296 		return err;
297 	}
298 
299 	/* Get (or create) the vnet associated with this port */
300 	vp = vsw_get_vnet(hp, vdev->mp, &handle);
301 	if (unlikely(IS_ERR(vp))) {
302 		err = PTR_ERR(vp);
303 		pr_err("Failed to get vnet for vsw-port\n");
304 		mdesc_release(hp);
305 		return err;
306 	}
307 
308 	mdesc_release(hp);
309 
310 	dev = vsw_alloc_netdev(vsw_port_hwaddr, vdev, handle, *port_id);
311 	if (IS_ERR(dev)) {
312 		err = PTR_ERR(dev);
313 		pr_err("Failed to alloc netdev for vsw-port\n");
314 		return err;
315 	}
316 
317 	port = netdev_priv(dev);
318 
319 	INIT_LIST_HEAD(&port->list);
320 
321 	for (i = 0; i < ETH_ALEN; i++)
322 		port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
323 
324 	port->vp = vp;
325 	port->dev = dev;
326 	port->switch_port = 1;
327 	port->tso = true;
328 	port->tsolen = 0;
329 
330 	/* Mark the port as belonging to ldmvsw which directs the
331 	 * the common code to use the net_device in the vnet_port
332 	 * rather than the net_device in the vnet (which is used
333 	 * by sunvnet). This bit is used by the VNET_PORT_TO_NET_DEVICE
334 	 * macro.
335 	 */
336 	port->vsw = 1;
337 
338 	err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
339 			      vsw_versions, ARRAY_SIZE(vsw_versions),
340 			      &vsw_vio_ops, dev->name);
341 	if (err)
342 		goto err_out_free_dev;
343 
344 	err = vio_ldc_alloc(&port->vio, &vsw_ldc_cfg, port);
345 	if (err)
346 		goto err_out_free_dev;
347 
348 	dev_set_drvdata(&vdev->dev, port);
349 
350 	netif_napi_add(dev, &port->napi, sunvnet_poll_common,
351 		       NAPI_POLL_WEIGHT);
352 
353 	spin_lock_irqsave(&vp->lock, flags);
354 	list_add_rcu(&port->list, &vp->port_list);
355 	spin_unlock_irqrestore(&vp->lock, flags);
356 
357 	setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common,
358 		    (unsigned long)port);
359 
360 	err = register_netdev(dev);
361 	if (err) {
362 		pr_err("Cannot register net device, aborting\n");
363 		goto err_out_del_timer;
364 	}
365 
366 	spin_lock_irqsave(&vp->lock, flags);
367 	sunvnet_port_add_txq_common(port);
368 	spin_unlock_irqrestore(&vp->lock, flags);
369 
370 	napi_enable(&port->napi);
371 	vio_port_up(&port->vio);
372 
373 	netdev_info(dev, "LDOM vsw-port %pM\n", dev->dev_addr);
374 
375 	pr_info("%s: PORT ( remote-mac %pM%s )\n", dev->name,
376 		port->raddr, " switch-port");
377 
378 	return 0;
379 
380 err_out_del_timer:
381 	del_timer_sync(&port->clean_timer);
382 	list_del_rcu(&port->list);
383 	synchronize_rcu();
384 	netif_napi_del(&port->napi);
385 	dev_set_drvdata(&vdev->dev, NULL);
386 	vio_ldc_free(&port->vio);
387 
388 err_out_free_dev:
389 	free_netdev(dev);
390 	return err;
391 }
392 
393 static int vsw_port_remove(struct vio_dev *vdev)
394 {
395 	struct vnet_port *port = dev_get_drvdata(&vdev->dev);
396 	unsigned long flags;
397 
398 	if (port) {
399 		del_timer_sync(&port->vio.timer);
400 
401 		napi_disable(&port->napi);
402 
403 		list_del_rcu(&port->list);
404 
405 		synchronize_rcu();
406 		del_timer_sync(&port->clean_timer);
407 		spin_lock_irqsave(&port->vp->lock, flags);
408 		sunvnet_port_rm_txq_common(port);
409 		spin_unlock_irqrestore(&port->vp->lock, flags);
410 		netif_napi_del(&port->napi);
411 		sunvnet_port_free_tx_bufs_common(port);
412 		vio_ldc_free(&port->vio);
413 
414 		dev_set_drvdata(&vdev->dev, NULL);
415 
416 		unregister_netdev(port->dev);
417 		free_netdev(port->dev);
418 	}
419 
420 	return 0;
421 }
422 
423 static void vsw_cleanup(void)
424 {
425 	struct vnet *vp;
426 
427 	/* just need to free up the vnet list */
428 	mutex_lock(&vnet_list_mutex);
429 	while (!list_empty(&vnet_list)) {
430 		vp = list_first_entry(&vnet_list, struct vnet, list);
431 		list_del(&vp->list);
432 		/* vio_unregister_driver() should have cleaned up port_list */
433 		if (!list_empty(&vp->port_list))
434 			pr_err("Ports not removed by VIO subsystem!\n");
435 		kfree(vp);
436 	}
437 	mutex_unlock(&vnet_list_mutex);
438 }
439 
440 static const struct vio_device_id vsw_port_match[] = {
441 	{
442 		.type = "vsw-port",
443 	},
444 	{},
445 };
446 MODULE_DEVICE_TABLE(vio, vsw_port_match);
447 
448 static struct vio_driver vsw_port_driver = {
449 	.id_table	= vsw_port_match,
450 	.probe		= vsw_port_probe,
451 	.remove		= vsw_port_remove,
452 	.name		= "vsw_port",
453 };
454 
455 static int __init vsw_init(void)
456 {
457 	return vio_register_driver(&vsw_port_driver);
458 }
459 
460 static void __exit vsw_exit(void)
461 {
462 	vio_unregister_driver(&vsw_port_driver);
463 	vsw_cleanup();
464 }
465 
466 module_init(vsw_init);
467 module_exit(vsw_exit);
468