blob: 5a90fed0626065613ba59fa7c5f8ca3c2dff6ef3 [file] [log] [blame]
Aaron Young5d01fa02016-03-15 11:35:39 -07001/* ldmvsw.c: Sun4v LDOM Virtual Switch Driver.
2 *
Shannon Nelson867fa152017-03-14 10:24:39 -07003 * Copyright (C) 2016-2017 Oracle. All rights reserved.
Aaron Young5d01fa02016-03-15 11:35:39 -07004 */
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 Nelson867fa152017-03-14 10:24:39 -070044#define DRV_MODULE_VERSION "1.2"
45#define DRV_MODULE_RELDATE "March 4, 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
Shannon Nelson867fa152017-03-14 10:24:39 -0700126int ldmvsw_open(struct net_device *dev)
127{
128 struct vnet_port *port = netdev_priv(dev);
129 struct vio_driver_state *vio = &port->vio;
130
131 /* reset the channel */
132 vio_link_state_change(vio, LDC_EVENT_RESET);
133 vnet_port_reset(port);
134 vio_port_up(vio);
135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(ldmvsw_open);
139
Aaron Young5d01fa02016-03-15 11:35:39 -0700140#ifdef CONFIG_NET_POLL_CONTROLLER
141static void vsw_poll_controller(struct net_device *dev)
142{
143 struct vnet_port *port = netdev_priv(dev);
144
145 return sunvnet_poll_controller_common(dev, port->vp);
146}
147#endif
148
149static const struct net_device_ops vsw_ops = {
Shannon Nelson867fa152017-03-14 10:24:39 -0700150 .ndo_open = ldmvsw_open,
Aaron Young5d01fa02016-03-15 11:35:39 -0700151 .ndo_stop = sunvnet_close_common,
152 .ndo_set_rx_mode = vsw_set_rx_mode,
153 .ndo_set_mac_address = sunvnet_set_mac_addr_common,
154 .ndo_validate_addr = eth_validate_addr,
155 .ndo_tx_timeout = sunvnet_tx_timeout_common,
Aaron Young5d01fa02016-03-15 11:35:39 -0700156 .ndo_start_xmit = vsw_start_xmit,
157 .ndo_select_queue = vsw_select_queue,
158#ifdef CONFIG_NET_POLL_CONTROLLER
159 .ndo_poll_controller = vsw_poll_controller,
160#endif
161};
162
163static const char *local_mac_prop = "local-mac-address";
164static const char *cfg_handle_prop = "cfg-handle";
165
166static struct vnet *vsw_get_vnet(struct mdesc_handle *hp,
167 u64 port_node,
168 u64 *handle)
169{
170 struct vnet *vp;
171 struct vnet *iter;
172 const u64 *local_mac = NULL;
173 const u64 *cfghandle = NULL;
174 u64 a;
175
176 /* Get the parent virtual-network-switch macaddr and cfghandle */
177 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
178 u64 target = mdesc_arc_target(hp, a);
179 const char *name;
180
181 name = mdesc_get_property(hp, target, "name", NULL);
182 if (!name || strcmp(name, "virtual-network-switch"))
183 continue;
184
185 local_mac = mdesc_get_property(hp, target,
186 local_mac_prop, NULL);
187 cfghandle = mdesc_get_property(hp, target,
188 cfg_handle_prop, NULL);
189 break;
190 }
191 if (!local_mac || !cfghandle)
192 return ERR_PTR(-ENODEV);
193
194 /* find or create associated vnet */
195 vp = NULL;
196 mutex_lock(&vnet_list_mutex);
197 list_for_each_entry(iter, &vnet_list, list) {
198 if (iter->local_mac == *local_mac) {
199 vp = iter;
200 break;
201 }
202 }
203
204 if (!vp) {
205 vp = kzalloc(sizeof(*vp), GFP_KERNEL);
206 if (unlikely(!vp)) {
207 mutex_unlock(&vnet_list_mutex);
208 return ERR_PTR(-ENOMEM);
209 }
210
211 spin_lock_init(&vp->lock);
212 INIT_LIST_HEAD(&vp->port_list);
213 INIT_LIST_HEAD(&vp->list);
214 vp->local_mac = *local_mac;
215 list_add(&vp->list, &vnet_list);
216 }
217
218 mutex_unlock(&vnet_list_mutex);
219
220 *handle = (u64)*cfghandle;
221
222 return vp;
223}
224
225static struct net_device *vsw_alloc_netdev(u8 hwaddr[],
226 struct vio_dev *vdev,
227 u64 handle,
228 u64 port_id)
229{
230 struct net_device *dev;
231 struct vnet_port *port;
232 int i;
233
234 dev = alloc_etherdev_mqs(sizeof(*port), VNET_MAX_TXQS, 1);
235 if (!dev)
236 return ERR_PTR(-ENOMEM);
237 dev->needed_headroom = VNET_PACKET_SKIP + 8;
238 dev->needed_tailroom = 8;
239
240 for (i = 0; i < ETH_ALEN; i++) {
241 dev->dev_addr[i] = hwaddr[i];
242 dev->perm_addr[i] = dev->dev_addr[i];
243 }
244
245 sprintf(dev->name, "vif%d.%d", (int)handle, (int)port_id);
246
247 dev->netdev_ops = &vsw_ops;
248 dev->ethtool_ops = &vsw_ethtool_ops;
249 dev->watchdog_timeo = VSW_TX_TIMEOUT;
250
Shannon Nelsonbc221a32017-02-13 10:57:04 -0800251 dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG;
Aaron Young5d01fa02016-03-15 11:35:39 -0700252 dev->features = dev->hw_features;
253
Jarod Wilson540bfe32016-10-17 15:54:10 -0400254 /* MTU range: 68 - 65535 */
255 dev->min_mtu = ETH_MIN_MTU;
256 dev->max_mtu = VNET_MAX_MTU;
257
Aaron Young5d01fa02016-03-15 11:35:39 -0700258 SET_NETDEV_DEV(dev, &vdev->dev);
259
260 return dev;
261}
262
263static struct ldc_channel_config vsw_ldc_cfg = {
264 .event = sunvnet_event_common,
265 .mtu = 64,
266 .mode = LDC_MODE_UNRELIABLE,
267};
268
269static struct vio_driver_ops vsw_vio_ops = {
270 .send_attr = sunvnet_send_attr_common,
271 .handle_attr = sunvnet_handle_attr_common,
272 .handshake_complete = sunvnet_handshake_complete_common,
273};
274
Aaron Young5d01fa02016-03-15 11:35:39 -0700275static const char *remote_macaddr_prop = "remote-mac-address";
276static const char *id_prop = "id";
277
278static int vsw_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
279{
280 struct mdesc_handle *hp;
281 struct vnet_port *port;
282 unsigned long flags;
283 struct vnet *vp;
284 struct net_device *dev;
285 const u64 *rmac;
286 int len, i, err;
287 const u64 *port_id;
288 u64 handle;
289
Aaron Young5d01fa02016-03-15 11:35:39 -0700290 hp = mdesc_grab();
291
292 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
293 err = -ENODEV;
294 if (!rmac) {
295 pr_err("Port lacks %s property\n", remote_macaddr_prop);
296 mdesc_release(hp);
297 return err;
298 }
299
300 port_id = mdesc_get_property(hp, vdev->mp, id_prop, NULL);
301 err = -ENODEV;
302 if (!port_id) {
303 pr_err("Port lacks %s property\n", id_prop);
304 mdesc_release(hp);
305 return err;
306 }
307
308 /* Get (or create) the vnet associated with this port */
309 vp = vsw_get_vnet(hp, vdev->mp, &handle);
310 if (unlikely(IS_ERR(vp))) {
311 err = PTR_ERR(vp);
312 pr_err("Failed to get vnet for vsw-port\n");
313 mdesc_release(hp);
314 return err;
315 }
316
317 mdesc_release(hp);
318
319 dev = vsw_alloc_netdev(vsw_port_hwaddr, vdev, handle, *port_id);
320 if (IS_ERR(dev)) {
321 err = PTR_ERR(dev);
322 pr_err("Failed to alloc netdev for vsw-port\n");
323 return err;
324 }
325
326 port = netdev_priv(dev);
327
328 INIT_LIST_HEAD(&port->list);
329
330 for (i = 0; i < ETH_ALEN; i++)
331 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
332
333 port->vp = vp;
334 port->dev = dev;
335 port->switch_port = 1;
Shannon Nelsonbc221a32017-02-13 10:57:04 -0800336 port->tso = false; /* no tso in vsw, misbehaves in bridge */
Aaron Young5d01fa02016-03-15 11:35:39 -0700337 port->tsolen = 0;
338
339 /* Mark the port as belonging to ldmvsw which directs the
340 * the common code to use the net_device in the vnet_port
341 * rather than the net_device in the vnet (which is used
342 * by sunvnet). This bit is used by the VNET_PORT_TO_NET_DEVICE
343 * macro.
344 */
345 port->vsw = 1;
346
347 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
348 vsw_versions, ARRAY_SIZE(vsw_versions),
349 &vsw_vio_ops, dev->name);
350 if (err)
351 goto err_out_free_dev;
352
353 err = vio_ldc_alloc(&port->vio, &vsw_ldc_cfg, port);
354 if (err)
355 goto err_out_free_dev;
356
357 dev_set_drvdata(&vdev->dev, port);
358
359 netif_napi_add(dev, &port->napi, sunvnet_poll_common,
360 NAPI_POLL_WEIGHT);
361
362 spin_lock_irqsave(&vp->lock, flags);
363 list_add_rcu(&port->list, &vp->port_list);
364 spin_unlock_irqrestore(&vp->lock, flags);
365
366 setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common,
367 (unsigned long)port);
368
369 err = register_netdev(dev);
370 if (err) {
371 pr_err("Cannot register net device, aborting\n");
372 goto err_out_del_timer;
373 }
374
375 spin_lock_irqsave(&vp->lock, flags);
376 sunvnet_port_add_txq_common(port);
377 spin_unlock_irqrestore(&vp->lock, flags);
378
379 napi_enable(&port->napi);
380 vio_port_up(&port->vio);
381
Shannon Nelson867fa152017-03-14 10:24:39 -0700382 /* assure no carrier until we receive an LDC_EVENT_UP,
383 * even if the vsw config script tries to force us up
384 */
385 netif_carrier_off(dev);
386
Aaron Young5d01fa02016-03-15 11:35:39 -0700387 netdev_info(dev, "LDOM vsw-port %pM\n", dev->dev_addr);
388
389 pr_info("%s: PORT ( remote-mac %pM%s )\n", dev->name,
390 port->raddr, " switch-port");
391
392 return 0;
393
394err_out_del_timer:
395 del_timer_sync(&port->clean_timer);
396 list_del_rcu(&port->list);
397 synchronize_rcu();
398 netif_napi_del(&port->napi);
399 dev_set_drvdata(&vdev->dev, NULL);
400 vio_ldc_free(&port->vio);
401
402err_out_free_dev:
403 free_netdev(dev);
404 return err;
405}
406
407static int vsw_port_remove(struct vio_dev *vdev)
408{
409 struct vnet_port *port = dev_get_drvdata(&vdev->dev);
410 unsigned long flags;
411
412 if (port) {
413 del_timer_sync(&port->vio.timer);
414
415 napi_disable(&port->napi);
416
417 list_del_rcu(&port->list);
418
419 synchronize_rcu();
420 del_timer_sync(&port->clean_timer);
421 spin_lock_irqsave(&port->vp->lock, flags);
422 sunvnet_port_rm_txq_common(port);
423 spin_unlock_irqrestore(&port->vp->lock, flags);
424 netif_napi_del(&port->napi);
425 sunvnet_port_free_tx_bufs_common(port);
426 vio_ldc_free(&port->vio);
427
428 dev_set_drvdata(&vdev->dev, NULL);
429
430 unregister_netdev(port->dev);
431 free_netdev(port->dev);
432 }
433
434 return 0;
435}
436
437static void vsw_cleanup(void)
438{
439 struct vnet *vp;
440
441 /* just need to free up the vnet list */
442 mutex_lock(&vnet_list_mutex);
443 while (!list_empty(&vnet_list)) {
444 vp = list_first_entry(&vnet_list, struct vnet, list);
445 list_del(&vp->list);
446 /* vio_unregister_driver() should have cleaned up port_list */
447 if (!list_empty(&vp->port_list))
448 pr_err("Ports not removed by VIO subsystem!\n");
449 kfree(vp);
450 }
451 mutex_unlock(&vnet_list_mutex);
452}
453
454static const struct vio_device_id vsw_port_match[] = {
455 {
456 .type = "vsw-port",
457 },
458 {},
459};
460MODULE_DEVICE_TABLE(vio, vsw_port_match);
461
462static struct vio_driver vsw_port_driver = {
463 .id_table = vsw_port_match,
464 .probe = vsw_port_probe,
465 .remove = vsw_port_remove,
466 .name = "vsw_port",
467};
468
469static int __init vsw_init(void)
470{
Shannon Nelson76020112017-02-13 10:57:03 -0800471 pr_info("%s\n", version);
Aaron Young5d01fa02016-03-15 11:35:39 -0700472 return vio_register_driver(&vsw_port_driver);
473}
474
475static void __exit vsw_exit(void)
476{
477 vio_unregister_driver(&vsw_port_driver);
478 vsw_cleanup();
479}
480
481module_init(vsw_init);
482module_exit(vsw_exit);