blob: 98c5f1612681c071f82990898d51327c625c1c07 [file] [log] [blame]
David S. Miller4c521e42007-07-09 22:23:51 -07001/* sunvnet.c: Sun LDOM Virtual Network Driver.
2 *
David S. Miller3d452e52008-09-01 01:48:52 -07003 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
Aaron Young67d07192016-03-15 11:35:38 -07004 * Copyright (C) 2016 Oracle. All rights reserved.
David S. Miller4c521e42007-07-09 22:23:51 -07005 */
6
Joe Perches4d5870e2010-08-17 07:55:05 +00007#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
David S. Miller4c521e42007-07-09 22:23:51 -07009#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/types.h>
12#include <linux/slab.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/netdevice.h>
16#include <linux/ethtool.h>
17#include <linux/etherdevice.h>
David S. Miller9184a042007-07-17 22:19:10 -070018#include <linux/mutex.h>
David L Stevensda38c562014-12-02 15:31:13 -050019#include <linux/highmem.h>
David L Stevense4defc72014-09-29 19:47:59 -040020#include <linux/if_vlan.h>
David S. Miller4c521e42007-07-09 22:23:51 -070021
David L Stevensa2b78e92014-09-29 19:48:24 -040022#if IS_ENABLED(CONFIG_IPV6)
23#include <linux/icmpv6.h>
24#endif
25
David L Stevens6d0ba912014-12-02 15:31:04 -050026#include <net/ip.h>
David L Stevensa2b78e92014-09-29 19:48:24 -040027#include <net/icmp.h>
28#include <net/route.h>
29
David S. Miller4c521e42007-07-09 22:23:51 -070030#include <asm/vio.h>
31#include <asm/ldc.h>
32
Aaron Young31762ea2016-03-15 11:35:37 -070033#include "sunvnet_common.h"
34
35/* length of time before we decide the hardware is borked,
36 * and dev->tx_timeout() should be called to fix the problem
37 */
38#define VNET_TX_TIMEOUT (5 * HZ)
David S. Miller4c521e42007-07-09 22:23:51 -070039
40#define DRV_MODULE_NAME "sunvnet"
David S. Miller4c521e42007-07-09 22:23:51 -070041#define DRV_MODULE_VERSION "1.0"
42#define DRV_MODULE_RELDATE "June 25, 2007"
43
Bill Pembertonf73d12b2012-12-03 09:24:02 -050044static char version[] =
David S. Miller4c521e42007-07-09 22:23:51 -070045 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
46MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
47MODULE_DESCRIPTION("Sun LDOM virtual network driver");
48MODULE_LICENSE("GPL");
49MODULE_VERSION(DRV_MODULE_VERSION);
50
51/* Ordered from largest major to lowest */
52static struct vio_version vnet_versions[] = {
David L Stevens6d0ba912014-12-02 15:31:04 -050053 { .major = 1, .minor = 8 },
54 { .major = 1, .minor = 7 },
David L Stevense4defc72014-09-29 19:47:59 -040055 { .major = 1, .minor = 6 },
David S. Miller4c521e42007-07-09 22:23:51 -070056 { .major = 1, .minor = 0 },
57};
58
David S. Miller4c521e42007-07-09 22:23:51 -070059static void vnet_get_drvinfo(struct net_device *dev,
60 struct ethtool_drvinfo *info)
61{
Jiri Pirko7826d432013-01-06 00:44:26 +000062 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
63 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
David S. Miller4c521e42007-07-09 22:23:51 -070064}
65
66static u32 vnet_get_msglevel(struct net_device *dev)
67{
68 struct vnet *vp = netdev_priv(dev);
69 return vp->msg_enable;
70}
71
72static void vnet_set_msglevel(struct net_device *dev, u32 value)
73{
74 struct vnet *vp = netdev_priv(dev);
75 vp->msg_enable = value;
76}
77
78static const struct ethtool_ops vnet_ethtool_ops = {
79 .get_drvinfo = vnet_get_drvinfo,
80 .get_msglevel = vnet_get_msglevel,
81 .set_msglevel = vnet_set_msglevel,
82 .get_link = ethtool_op_get_link,
David S. Miller4c521e42007-07-09 22:23:51 -070083};
84
David S. Miller9184a042007-07-17 22:19:10 -070085static LIST_HEAD(vnet_list);
86static DEFINE_MUTEX(vnet_list_mutex);
87
Aaron Young67d07192016-03-15 11:35:38 -070088static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
89{
90 unsigned int hash = vnet_hashfn(skb->data);
91 struct hlist_head *hp = &vp->port_hash[hash];
92 struct vnet_port *port;
93
94 hlist_for_each_entry_rcu(port, hp, hash) {
95 if (!sunvnet_port_is_up_common(port))
96 continue;
97 if (ether_addr_equal(port->raddr, skb->data))
98 return port;
99 }
100 list_for_each_entry_rcu(port, &vp->port_list, list) {
101 if (!port->switch_port)
102 continue;
103 if (!sunvnet_port_is_up_common(port))
104 continue;
105 return port;
106 }
107 return NULL;
108}
109
110/* func arg to vnet_start_xmit_common() to get the proper tx port */
111static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb,
112 struct net_device *dev)
113{
114 struct vnet *vp = netdev_priv(dev);
115
116 return __tx_port_find(vp, skb);
117}
118
119static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
120 void *accel_priv, select_queue_fallback_t fallback)
121{
122 struct vnet *vp = netdev_priv(dev);
123 struct vnet_port *port = __tx_port_find(vp, skb);
124
125 if (!port)
126 return 0;
127
128 return port->q_index;
129}
130
131/* Wrappers to common functions */
132static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
133{
134 return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find);
135}
136
137static void vnet_set_rx_mode(struct net_device *dev)
138{
139 struct vnet *vp = netdev_priv(dev);
140
141 return sunvnet_set_rx_mode_common(dev, vp);
142}
143
144#ifdef CONFIG_NET_POLL_CONTROLLER
145static void vnet_poll_controller(struct net_device *dev)
146{
147 struct vnet *vp = netdev_priv(dev);
148
149 return sunvnet_poll_controller_common(dev, vp);
150}
151#endif
152
David S. Miller8fd17f22009-03-20 00:51:22 -0700153static const struct net_device_ops vnet_ops = {
Aaron Young31762ea2016-03-15 11:35:37 -0700154 .ndo_open = sunvnet_open_common,
155 .ndo_stop = sunvnet_close_common,
Aaron Young67d07192016-03-15 11:35:38 -0700156 .ndo_set_rx_mode = vnet_set_rx_mode,
Aaron Young31762ea2016-03-15 11:35:37 -0700157 .ndo_set_mac_address = sunvnet_set_mac_addr_common,
Ben Hutchings240c1022009-07-09 17:54:35 +0000158 .ndo_validate_addr = eth_validate_addr,
Aaron Young31762ea2016-03-15 11:35:37 -0700159 .ndo_tx_timeout = sunvnet_tx_timeout_common,
160 .ndo_change_mtu = sunvnet_change_mtu_common,
Aaron Young67d07192016-03-15 11:35:38 -0700161 .ndo_start_xmit = vnet_start_xmit,
162 .ndo_select_queue = vnet_select_queue,
Sowmini Varadhan69088822014-10-25 15:12:12 -0400163#ifdef CONFIG_NET_POLL_CONTROLLER
Aaron Young67d07192016-03-15 11:35:38 -0700164 .ndo_poll_controller = vnet_poll_controller,
Sowmini Varadhan69088822014-10-25 15:12:12 -0400165#endif
David S. Miller8fd17f22009-03-20 00:51:22 -0700166};
167
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400168static struct vnet *vnet_new(const u64 *local_mac,
169 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700170{
171 struct net_device *dev;
172 struct vnet *vp;
173 int err, i;
174
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400175 dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
Joe Perches41de8d42012-01-29 13:47:52 +0000176 if (!dev)
David S. Miller9184a042007-07-17 22:19:10 -0700177 return ERR_PTR(-ENOMEM);
David L Stevens8e845f4c2014-09-29 19:48:11 -0400178 dev->needed_headroom = VNET_PACKET_SKIP + 8;
179 dev->needed_tailroom = 8;
David S. Miller9184a042007-07-17 22:19:10 -0700180
181 for (i = 0; i < ETH_ALEN; i++)
182 dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
183
David S. Miller9184a042007-07-17 22:19:10 -0700184 vp = netdev_priv(dev);
185
186 spin_lock_init(&vp->lock);
187 vp->dev = dev;
188
189 INIT_LIST_HEAD(&vp->port_list);
190 for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
191 INIT_HLIST_HEAD(&vp->port_hash[i]);
192 INIT_LIST_HEAD(&vp->list);
193 vp->local_mac = *local_mac;
194
David S. Miller8fd17f22009-03-20 00:51:22 -0700195 dev->netdev_ops = &vnet_ops;
David S. Miller9184a042007-07-17 22:19:10 -0700196 dev->ethtool_ops = &vnet_ethtool_ops;
197 dev->watchdog_timeo = VNET_TX_TIMEOUT;
David S. Miller9184a042007-07-17 22:19:10 -0700198
David L Stevens368e36e2014-12-02 15:31:38 -0500199 dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_GSO_SOFTWARE |
David L Stevens9a72dd42014-12-02 15:31:30 -0500200 NETIF_F_HW_CSUM | NETIF_F_SG;
David L Stevensda38c562014-12-02 15:31:13 -0500201 dev->features = dev->hw_features;
202
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400203 SET_NETDEV_DEV(dev, &vdev->dev);
204
David S. Miller9184a042007-07-17 22:19:10 -0700205 err = register_netdev(dev);
206 if (err) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000207 pr_err("Cannot register net device, aborting\n");
David S. Miller9184a042007-07-17 22:19:10 -0700208 goto err_out_free_dev;
209 }
210
Joe Perches4d5870e2010-08-17 07:55:05 +0000211 netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
David S. Miller9184a042007-07-17 22:19:10 -0700212
213 list_add(&vp->list, &vnet_list);
214
215 return vp;
216
217err_out_free_dev:
218 free_netdev(dev);
219
220 return ERR_PTR(err);
221}
222
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400223static struct vnet *vnet_find_or_create(const u64 *local_mac,
224 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700225{
226 struct vnet *iter, *vp;
227
228 mutex_lock(&vnet_list_mutex);
229 vp = NULL;
230 list_for_each_entry(iter, &vnet_list, list) {
231 if (iter->local_mac == *local_mac) {
232 vp = iter;
233 break;
234 }
235 }
236 if (!vp)
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400237 vp = vnet_new(local_mac, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700238 mutex_unlock(&vnet_list_mutex);
239
240 return vp;
241}
242
Sowmini Varadhana4b70a02014-07-16 10:02:26 -0400243static void vnet_cleanup(void)
244{
245 struct vnet *vp;
246 struct net_device *dev;
247
248 mutex_lock(&vnet_list_mutex);
249 while (!list_empty(&vnet_list)) {
250 vp = list_first_entry(&vnet_list, struct vnet, list);
251 list_del(&vp->list);
252 dev = vp->dev;
253 /* vio_unregister_driver() should have cleaned up port_list */
254 BUG_ON(!list_empty(&vp->port_list));
255 unregister_netdev(dev);
256 free_netdev(dev);
257 }
258 mutex_unlock(&vnet_list_mutex);
259}
260
David S. Miller9184a042007-07-17 22:19:10 -0700261static const char *local_mac_prop = "local-mac-address";
262
Bill Pembertonf73d12b2012-12-03 09:24:02 -0500263static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400264 u64 port_node,
265 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700266{
267 const u64 *local_mac = NULL;
268 u64 a;
269
270 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
271 u64 target = mdesc_arc_target(hp, a);
272 const char *name;
273
274 name = mdesc_get_property(hp, target, "name", NULL);
275 if (!name || strcmp(name, "network"))
276 continue;
277
278 local_mac = mdesc_get_property(hp, target,
279 local_mac_prop, NULL);
280 if (local_mac)
281 break;
282 }
283 if (!local_mac)
284 return ERR_PTR(-ENODEV);
285
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400286 return vnet_find_or_create(local_mac, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700287}
288
David S. Miller4c521e42007-07-09 22:23:51 -0700289static struct ldc_channel_config vnet_ldc_cfg = {
Aaron Young31762ea2016-03-15 11:35:37 -0700290 .event = sunvnet_event_common,
David S. Miller4c521e42007-07-09 22:23:51 -0700291 .mtu = 64,
292 .mode = LDC_MODE_UNRELIABLE,
293};
294
295static struct vio_driver_ops vnet_vio_ops = {
Aaron Young31762ea2016-03-15 11:35:37 -0700296 .send_attr = sunvnet_send_attr_common,
297 .handle_attr = sunvnet_handle_attr_common,
298 .handshake_complete = sunvnet_handshake_complete_common,
David S. Miller4c521e42007-07-09 22:23:51 -0700299};
300
Bill Pembertonf73d12b2012-12-03 09:24:02 -0500301static void print_version(void)
David S. Miller9184a042007-07-17 22:19:10 -0700302{
Joe Perches4d5870e2010-08-17 07:55:05 +0000303 printk_once(KERN_INFO "%s", version);
David S. Miller9184a042007-07-17 22:19:10 -0700304}
305
David S. Miller4c521e42007-07-09 22:23:51 -0700306const char *remote_macaddr_prop = "remote-mac-address";
307
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000308static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller4c521e42007-07-09 22:23:51 -0700309{
David S. Miller43fdf272007-07-12 13:47:50 -0700310 struct mdesc_handle *hp;
David S. Miller4c521e42007-07-09 22:23:51 -0700311 struct vnet_port *port;
312 unsigned long flags;
313 struct vnet *vp;
314 const u64 *rmac;
315 int len, i, err, switch_port;
316
David S. Miller9184a042007-07-17 22:19:10 -0700317 print_version();
David S. Miller4c521e42007-07-09 22:23:51 -0700318
David S. Miller43fdf272007-07-12 13:47:50 -0700319 hp = mdesc_grab();
320
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400321 vp = vnet_find_parent(hp, vdev->mp, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700322 if (IS_ERR(vp)) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000323 pr_err("Cannot find port parent vnet\n");
David S. Miller9184a042007-07-17 22:19:10 -0700324 err = PTR_ERR(vp);
325 goto err_out_put_mdesc;
326 }
327
David S. Miller43fdf272007-07-12 13:47:50 -0700328 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
329 err = -ENODEV;
David S. Miller4c521e42007-07-09 22:23:51 -0700330 if (!rmac) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000331 pr_err("Port lacks %s property\n", remote_macaddr_prop);
David S. Miller43fdf272007-07-12 13:47:50 -0700332 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -0700333 }
334
335 port = kzalloc(sizeof(*port), GFP_KERNEL);
David S. Miller43fdf272007-07-12 13:47:50 -0700336 err = -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +0000337 if (!port)
David S. Miller43fdf272007-07-12 13:47:50 -0700338 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -0700339
340 for (i = 0; i < ETH_ALEN; i++)
341 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
342
343 port->vp = vp;
344
David S. Miller43fdf272007-07-12 13:47:50 -0700345 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
David S. Miller4c521e42007-07-09 22:23:51 -0700346 vnet_versions, ARRAY_SIZE(vnet_versions),
347 &vnet_vio_ops, vp->dev->name);
348 if (err)
349 goto err_out_free_port;
350
351 err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
352 if (err)
353 goto err_out_free_port;
354
Aaron Young31762ea2016-03-15 11:35:37 -0700355 netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common,
356 NAPI_POLL_WEIGHT);
Sowmini Varadhan69088822014-10-25 15:12:12 -0400357
David S. Miller4c521e42007-07-09 22:23:51 -0700358 INIT_HLIST_NODE(&port->hash);
359 INIT_LIST_HEAD(&port->list);
360
361 switch_port = 0;
David S. Miller43fdf272007-07-12 13:47:50 -0700362 if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL) != NULL)
David S. Miller4c521e42007-07-09 22:23:51 -0700363 switch_port = 1;
David S. Miller028ebff2007-07-20 02:30:25 -0700364 port->switch_port = switch_port;
David L Stevens368e36e2014-12-02 15:31:38 -0500365 port->tso = true;
366 port->tsolen = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700367
368 spin_lock_irqsave(&vp->lock, flags);
369 if (switch_port)
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400370 list_add_rcu(&port->list, &vp->port_list);
David S. Miller4c521e42007-07-09 22:23:51 -0700371 else
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400372 list_add_tail_rcu(&port->list, &vp->port_list);
373 hlist_add_head_rcu(&port->hash,
374 &vp->port_hash[vnet_hashfn(port->raddr)]);
Aaron Young31762ea2016-03-15 11:35:37 -0700375 sunvnet_port_add_txq_common(port);
David S. Miller4c521e42007-07-09 22:23:51 -0700376 spin_unlock_irqrestore(&vp->lock, flags);
377
378 dev_set_drvdata(&vdev->dev, port);
379
Joe Perches4d5870e2010-08-17 07:55:05 +0000380 pr_info("%s: PORT ( remote-mac %pM%s )\n",
381 vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
David S. Miller4c521e42007-07-09 22:23:51 -0700382
Aaron Young31762ea2016-03-15 11:35:37 -0700383 setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common,
David L Stevens8e845f4c2014-09-29 19:48:11 -0400384 (unsigned long)port);
385
Sowmini Varadhan69088822014-10-25 15:12:12 -0400386 napi_enable(&port->napi);
David S. Miller4c521e42007-07-09 22:23:51 -0700387 vio_port_up(&port->vio);
388
David S. Miller43fdf272007-07-12 13:47:50 -0700389 mdesc_release(hp);
390
David S. Miller4c521e42007-07-09 22:23:51 -0700391 return 0;
392
David S. Miller4c521e42007-07-09 22:23:51 -0700393err_out_free_port:
394 kfree(port);
395
David S. Miller43fdf272007-07-12 13:47:50 -0700396err_out_put_mdesc:
397 mdesc_release(hp);
David S. Miller4c521e42007-07-09 22:23:51 -0700398 return err;
399}
400
401static int vnet_port_remove(struct vio_dev *vdev)
402{
403 struct vnet_port *port = dev_get_drvdata(&vdev->dev);
404
405 if (port) {
David S. Miller4c521e42007-07-09 22:23:51 -0700406
407 del_timer_sync(&port->vio.timer);
408
Sowmini Varadhan69088822014-10-25 15:12:12 -0400409 napi_disable(&port->napi);
David S. Miller4c521e42007-07-09 22:23:51 -0700410
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400411 list_del_rcu(&port->list);
412 hlist_del_rcu(&port->hash);
413
414 synchronize_rcu();
415 del_timer_sync(&port->clean_timer);
Aaron Young31762ea2016-03-15 11:35:37 -0700416 sunvnet_port_rm_txq_common(port);
Sowmini Varadhan69088822014-10-25 15:12:12 -0400417 netif_napi_del(&port->napi);
Aaron Young31762ea2016-03-15 11:35:37 -0700418 sunvnet_port_free_tx_bufs_common(port);
David S. Miller4c521e42007-07-09 22:23:51 -0700419 vio_ldc_free(&port->vio);
420
421 dev_set_drvdata(&vdev->dev, NULL);
422
423 kfree(port);
Dave Kleikampaabb9872013-07-01 16:49:22 -0500424
David S. Miller4c521e42007-07-09 22:23:51 -0700425 }
426 return 0;
427}
428
David S. Miller3d452e52008-09-01 01:48:52 -0700429static const struct vio_device_id vnet_port_match[] = {
David S. Miller4c521e42007-07-09 22:23:51 -0700430 {
431 .type = "vnet-port",
432 },
433 {},
434};
Fabio Massimo Di Nittoda68e082007-07-18 14:35:23 -0700435MODULE_DEVICE_TABLE(vio, vnet_port_match);
David S. Miller4c521e42007-07-09 22:23:51 -0700436
437static struct vio_driver vnet_port_driver = {
438 .id_table = vnet_port_match,
439 .probe = vnet_port_probe,
440 .remove = vnet_port_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +0000441 .name = "vnet_port",
David S. Miller4c521e42007-07-09 22:23:51 -0700442};
443
David S. Miller4c521e42007-07-09 22:23:51 -0700444static int __init vnet_init(void)
445{
David S. Miller9184a042007-07-17 22:19:10 -0700446 return vio_register_driver(&vnet_port_driver);
David S. Miller4c521e42007-07-09 22:23:51 -0700447}
448
449static void __exit vnet_exit(void)
450{
451 vio_unregister_driver(&vnet_port_driver);
Sowmini Varadhana4b70a02014-07-16 10:02:26 -0400452 vnet_cleanup();
David S. Miller4c521e42007-07-09 22:23:51 -0700453}
454
455module_init(vnet_init);
456module_exit(vnet_exit);