blob: 89952deae47fc813b66c6d856f16dd648a803a0c [file] [log] [blame]
Aaron Young5d01fa02016-03-15 11:35:39 -07001/* 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>
Aaron Young5d01fa02016-03-15 11:35:39 -070014#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 */
41static u8 vsw_port_hwaddr[ETH_ALEN] = {0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
42
43#define DRV_MODULE_NAME "ldmvsw"
Shannon Nelson76020112017-02-13 10:57:03 -080044#define DRV_MODULE_VERSION "1.1"
45#define DRV_MODULE_RELDATE "February 3, 2017"
Aaron Young5d01fa02016-03-15 11:35:39 -070046
47static char version[] =
Shannon Nelson76020112017-02-13 10:57:03 -080048 DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Aaron Young5d01fa02016-03-15 11:35:39 -070049MODULE_AUTHOR("Oracle");
50MODULE_DESCRIPTION("Sun4v LDOM Virtual Switch Driver");
51MODULE_LICENSE("GPL");
52MODULE_VERSION(DRV_MODULE_VERSION);
53
54/* Ordered from largest major to lowest */
55static 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
62static 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
69static 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
76static 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
83static 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
90static LIST_HEAD(vnet_list);
91static DEFINE_MUTEX(vnet_list_mutex);
92
93/* func arg to vnet_start_xmit_common() to get the proper tx port */
94static 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
102static 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 */
114static 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
119static 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
127static 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
135static 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,
Aaron Young5d01fa02016-03-15 11:35:39 -0700142 .ndo_start_xmit = vsw_start_xmit,
143 .ndo_select_queue = vsw_select_queue,
144#ifdef CONFIG_NET_POLL_CONTROLLER
145 .ndo_poll_controller = vsw_poll_controller,
146#endif
147};
148
149static const char *local_mac_prop = "local-mac-address";
150static const char *cfg_handle_prop = "cfg-handle";
151
152static struct vnet *vsw_get_vnet(struct mdesc_handle *hp,
153 u64 port_node,
154 u64 *handle)
155{
156 struct vnet *vp;
157 struct vnet *iter;
158 const u64 *local_mac = NULL;
159 const u64 *cfghandle = NULL;
160 u64 a;
161
162 /* Get the parent virtual-network-switch macaddr and cfghandle */
163 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
164 u64 target = mdesc_arc_target(hp, a);
165 const char *name;
166
167 name = mdesc_get_property(hp, target, "name", NULL);
168 if (!name || strcmp(name, "virtual-network-switch"))
169 continue;
170
171 local_mac = mdesc_get_property(hp, target,
172 local_mac_prop, NULL);
173 cfghandle = mdesc_get_property(hp, target,
174 cfg_handle_prop, NULL);
175 break;
176 }
177 if (!local_mac || !cfghandle)
178 return ERR_PTR(-ENODEV);
179
180 /* find or create associated vnet */
181 vp = NULL;
182 mutex_lock(&vnet_list_mutex);
183 list_for_each_entry(iter, &vnet_list, list) {
184 if (iter->local_mac == *local_mac) {
185 vp = iter;
186 break;
187 }
188 }
189
190 if (!vp) {
191 vp = kzalloc(sizeof(*vp), GFP_KERNEL);
192 if (unlikely(!vp)) {
193 mutex_unlock(&vnet_list_mutex);
194 return ERR_PTR(-ENOMEM);
195 }
196
197 spin_lock_init(&vp->lock);
198 INIT_LIST_HEAD(&vp->port_list);
199 INIT_LIST_HEAD(&vp->list);
200 vp->local_mac = *local_mac;
201 list_add(&vp->list, &vnet_list);
202 }
203
204 mutex_unlock(&vnet_list_mutex);
205
206 *handle = (u64)*cfghandle;
207
208 return vp;
209}
210
211static struct net_device *vsw_alloc_netdev(u8 hwaddr[],
212 struct vio_dev *vdev,
213 u64 handle,
214 u64 port_id)
215{
216 struct net_device *dev;
217 struct vnet_port *port;
218 int i;
219
220 dev = alloc_etherdev_mqs(sizeof(*port), VNET_MAX_TXQS, 1);
221 if (!dev)
222 return ERR_PTR(-ENOMEM);
223 dev->needed_headroom = VNET_PACKET_SKIP + 8;
224 dev->needed_tailroom = 8;
225
226 for (i = 0; i < ETH_ALEN; i++) {
227 dev->dev_addr[i] = hwaddr[i];
228 dev->perm_addr[i] = dev->dev_addr[i];
229 }
230
231 sprintf(dev->name, "vif%d.%d", (int)handle, (int)port_id);
232
233 dev->netdev_ops = &vsw_ops;
234 dev->ethtool_ops = &vsw_ethtool_ops;
235 dev->watchdog_timeo = VSW_TX_TIMEOUT;
236
Shannon Nelsonbc221a32017-02-13 10:57:04 -0800237 dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG;
Aaron Young5d01fa02016-03-15 11:35:39 -0700238 dev->features = dev->hw_features;
239
Jarod Wilson540bfe32016-10-17 15:54:10 -0400240 /* MTU range: 68 - 65535 */
241 dev->min_mtu = ETH_MIN_MTU;
242 dev->max_mtu = VNET_MAX_MTU;
243
Aaron Young5d01fa02016-03-15 11:35:39 -0700244 SET_NETDEV_DEV(dev, &vdev->dev);
245
246 return dev;
247}
248
249static struct ldc_channel_config vsw_ldc_cfg = {
250 .event = sunvnet_event_common,
251 .mtu = 64,
252 .mode = LDC_MODE_UNRELIABLE,
253};
254
255static struct vio_driver_ops vsw_vio_ops = {
256 .send_attr = sunvnet_send_attr_common,
257 .handle_attr = sunvnet_handle_attr_common,
258 .handshake_complete = sunvnet_handshake_complete_common,
259};
260
Aaron Young5d01fa02016-03-15 11:35:39 -0700261static const char *remote_macaddr_prop = "remote-mac-address";
262static const char *id_prop = "id";
263
264static int vsw_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
265{
266 struct mdesc_handle *hp;
267 struct vnet_port *port;
268 unsigned long flags;
269 struct vnet *vp;
270 struct net_device *dev;
271 const u64 *rmac;
272 int len, i, err;
273 const u64 *port_id;
274 u64 handle;
275
Aaron Young5d01fa02016-03-15 11:35:39 -0700276 hp = mdesc_grab();
277
278 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
279 err = -ENODEV;
280 if (!rmac) {
281 pr_err("Port lacks %s property\n", remote_macaddr_prop);
282 mdesc_release(hp);
283 return err;
284 }
285
286 port_id = mdesc_get_property(hp, vdev->mp, id_prop, NULL);
287 err = -ENODEV;
288 if (!port_id) {
289 pr_err("Port lacks %s property\n", id_prop);
290 mdesc_release(hp);
291 return err;
292 }
293
294 /* Get (or create) the vnet associated with this port */
295 vp = vsw_get_vnet(hp, vdev->mp, &handle);
296 if (unlikely(IS_ERR(vp))) {
297 err = PTR_ERR(vp);
298 pr_err("Failed to get vnet for vsw-port\n");
299 mdesc_release(hp);
300 return err;
301 }
302
303 mdesc_release(hp);
304
305 dev = vsw_alloc_netdev(vsw_port_hwaddr, vdev, handle, *port_id);
306 if (IS_ERR(dev)) {
307 err = PTR_ERR(dev);
308 pr_err("Failed to alloc netdev for vsw-port\n");
309 return err;
310 }
311
312 port = netdev_priv(dev);
313
314 INIT_LIST_HEAD(&port->list);
315
316 for (i = 0; i < ETH_ALEN; i++)
317 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
318
319 port->vp = vp;
320 port->dev = dev;
321 port->switch_port = 1;
Shannon Nelsonbc221a32017-02-13 10:57:04 -0800322 port->tso = false; /* no tso in vsw, misbehaves in bridge */
Aaron Young5d01fa02016-03-15 11:35:39 -0700323 port->tsolen = 0;
324
325 /* Mark the port as belonging to ldmvsw which directs the
326 * the common code to use the net_device in the vnet_port
327 * rather than the net_device in the vnet (which is used
328 * by sunvnet). This bit is used by the VNET_PORT_TO_NET_DEVICE
329 * macro.
330 */
331 port->vsw = 1;
332
333 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
334 vsw_versions, ARRAY_SIZE(vsw_versions),
335 &vsw_vio_ops, dev->name);
336 if (err)
337 goto err_out_free_dev;
338
339 err = vio_ldc_alloc(&port->vio, &vsw_ldc_cfg, port);
340 if (err)
341 goto err_out_free_dev;
342
343 dev_set_drvdata(&vdev->dev, port);
344
345 netif_napi_add(dev, &port->napi, sunvnet_poll_common,
346 NAPI_POLL_WEIGHT);
347
348 spin_lock_irqsave(&vp->lock, flags);
349 list_add_rcu(&port->list, &vp->port_list);
350 spin_unlock_irqrestore(&vp->lock, flags);
351
352 setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common,
353 (unsigned long)port);
354
355 err = register_netdev(dev);
356 if (err) {
357 pr_err("Cannot register net device, aborting\n");
358 goto err_out_del_timer;
359 }
360
361 spin_lock_irqsave(&vp->lock, flags);
362 sunvnet_port_add_txq_common(port);
363 spin_unlock_irqrestore(&vp->lock, flags);
364
365 napi_enable(&port->napi);
366 vio_port_up(&port->vio);
367
368 netdev_info(dev, "LDOM vsw-port %pM\n", dev->dev_addr);
369
370 pr_info("%s: PORT ( remote-mac %pM%s )\n", dev->name,
371 port->raddr, " switch-port");
372
373 return 0;
374
375err_out_del_timer:
376 del_timer_sync(&port->clean_timer);
377 list_del_rcu(&port->list);
378 synchronize_rcu();
379 netif_napi_del(&port->napi);
380 dev_set_drvdata(&vdev->dev, NULL);
381 vio_ldc_free(&port->vio);
382
383err_out_free_dev:
384 free_netdev(dev);
385 return err;
386}
387
388static int vsw_port_remove(struct vio_dev *vdev)
389{
390 struct vnet_port *port = dev_get_drvdata(&vdev->dev);
391 unsigned long flags;
392
393 if (port) {
394 del_timer_sync(&port->vio.timer);
395
396 napi_disable(&port->napi);
397
398 list_del_rcu(&port->list);
399
400 synchronize_rcu();
401 del_timer_sync(&port->clean_timer);
402 spin_lock_irqsave(&port->vp->lock, flags);
403 sunvnet_port_rm_txq_common(port);
404 spin_unlock_irqrestore(&port->vp->lock, flags);
405 netif_napi_del(&port->napi);
406 sunvnet_port_free_tx_bufs_common(port);
407 vio_ldc_free(&port->vio);
408
409 dev_set_drvdata(&vdev->dev, NULL);
410
411 unregister_netdev(port->dev);
412 free_netdev(port->dev);
413 }
414
415 return 0;
416}
417
418static void vsw_cleanup(void)
419{
420 struct vnet *vp;
421
422 /* just need to free up the vnet list */
423 mutex_lock(&vnet_list_mutex);
424 while (!list_empty(&vnet_list)) {
425 vp = list_first_entry(&vnet_list, struct vnet, list);
426 list_del(&vp->list);
427 /* vio_unregister_driver() should have cleaned up port_list */
428 if (!list_empty(&vp->port_list))
429 pr_err("Ports not removed by VIO subsystem!\n");
430 kfree(vp);
431 }
432 mutex_unlock(&vnet_list_mutex);
433}
434
435static const struct vio_device_id vsw_port_match[] = {
436 {
437 .type = "vsw-port",
438 },
439 {},
440};
441MODULE_DEVICE_TABLE(vio, vsw_port_match);
442
443static struct vio_driver vsw_port_driver = {
444 .id_table = vsw_port_match,
445 .probe = vsw_port_probe,
446 .remove = vsw_port_remove,
447 .name = "vsw_port",
448};
449
450static int __init vsw_init(void)
451{
Shannon Nelson76020112017-02-13 10:57:03 -0800452 pr_info("%s\n", version);
Aaron Young5d01fa02016-03-15 11:35:39 -0700453 return vio_register_driver(&vsw_port_driver);
454}
455
456static void __exit vsw_exit(void)
457{
458 vio_unregister_driver(&vsw_port_driver);
459 vsw_cleanup();
460}
461
462module_init(vsw_init);
463module_exit(vsw_exit);