]>
Commit | Line | Data |
---|---|---|
4c521e42 DM |
1 | /* sunvnet.c: Sun LDOM Virtual Network Driver. |
2 | * | |
3d452e55 | 3 | * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> |
67d0719f | 4 | * Copyright (C) 2016 Oracle. All rights reserved. |
4c521e42 DM |
5 | */ |
6 | ||
4d5870ec JP |
7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
8 | ||
4c521e42 DM |
9 | #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> | |
9184a046 | 18 | #include <linux/mutex.h> |
da38c564 | 19 | #include <linux/highmem.h> |
e4defc77 | 20 | #include <linux/if_vlan.h> |
4c521e42 | 21 | |
a2b78e9b DS |
22 | #if IS_ENABLED(CONFIG_IPV6) |
23 | #include <linux/icmpv6.h> | |
24 | #endif | |
25 | ||
6d0ba919 | 26 | #include <net/ip.h> |
a2b78e9b DS |
27 | #include <net/icmp.h> |
28 | #include <net/route.h> | |
29 | ||
4c521e42 DM |
30 | #include <asm/vio.h> |
31 | #include <asm/ldc.h> | |
32 | ||
31762eaa AY |
33 | #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) | |
4c521e42 DM |
39 | |
40 | #define DRV_MODULE_NAME "sunvnet" | |
4c521e42 DM |
41 | #define DRV_MODULE_VERSION "1.0" |
42 | #define DRV_MODULE_RELDATE "June 25, 2007" | |
43 | ||
f73d12bd | 44 | static char version[] = |
4c521e42 DM |
45 | DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n"; |
46 | MODULE_AUTHOR("David S. Miller (davem@davemloft.net)"); | |
47 | MODULE_DESCRIPTION("Sun LDOM virtual network driver"); | |
48 | MODULE_LICENSE("GPL"); | |
49 | MODULE_VERSION(DRV_MODULE_VERSION); | |
50 | ||
51 | /* Ordered from largest major to lowest */ | |
52 | static struct vio_version vnet_versions[] = { | |
6d0ba919 DS |
53 | { .major = 1, .minor = 8 }, |
54 | { .major = 1, .minor = 7 }, | |
e4defc77 | 55 | { .major = 1, .minor = 6 }, |
4c521e42 DM |
56 | { .major = 1, .minor = 0 }, |
57 | }; | |
58 | ||
4c521e42 DM |
59 | static void vnet_get_drvinfo(struct net_device *dev, |
60 | struct ethtool_drvinfo *info) | |
61 | { | |
7826d43f JP |
62 | strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); |
63 | strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version)); | |
4c521e42 DM |
64 | } |
65 | ||
66 | static u32 vnet_get_msglevel(struct net_device *dev) | |
67 | { | |
68 | struct vnet *vp = netdev_priv(dev); | |
dc153f85 | 69 | |
4c521e42 DM |
70 | return vp->msg_enable; |
71 | } | |
72 | ||
73 | static void vnet_set_msglevel(struct net_device *dev, u32 value) | |
74 | { | |
75 | struct vnet *vp = netdev_priv(dev); | |
dc153f85 | 76 | |
4c521e42 DM |
77 | vp->msg_enable = value; |
78 | } | |
79 | ||
80 | static const struct ethtool_ops vnet_ethtool_ops = { | |
81 | .get_drvinfo = vnet_get_drvinfo, | |
82 | .get_msglevel = vnet_get_msglevel, | |
83 | .set_msglevel = vnet_set_msglevel, | |
84 | .get_link = ethtool_op_get_link, | |
4c521e42 DM |
85 | }; |
86 | ||
9184a046 DM |
87 | static LIST_HEAD(vnet_list); |
88 | static DEFINE_MUTEX(vnet_list_mutex); | |
89 | ||
67d0719f AY |
90 | static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb) |
91 | { | |
92 | unsigned int hash = vnet_hashfn(skb->data); | |
93 | struct hlist_head *hp = &vp->port_hash[hash]; | |
94 | struct vnet_port *port; | |
95 | ||
96 | hlist_for_each_entry_rcu(port, hp, hash) { | |
97 | if (!sunvnet_port_is_up_common(port)) | |
98 | continue; | |
99 | if (ether_addr_equal(port->raddr, skb->data)) | |
100 | return port; | |
101 | } | |
102 | list_for_each_entry_rcu(port, &vp->port_list, list) { | |
103 | if (!port->switch_port) | |
104 | continue; | |
105 | if (!sunvnet_port_is_up_common(port)) | |
106 | continue; | |
107 | return port; | |
108 | } | |
109 | return NULL; | |
110 | } | |
111 | ||
112 | /* func arg to vnet_start_xmit_common() to get the proper tx port */ | |
113 | static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb, | |
114 | struct net_device *dev) | |
115 | { | |
116 | struct vnet *vp = netdev_priv(dev); | |
117 | ||
118 | return __tx_port_find(vp, skb); | |
119 | } | |
120 | ||
121 | static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb, | |
122 | void *accel_priv, select_queue_fallback_t fallback) | |
123 | { | |
124 | struct vnet *vp = netdev_priv(dev); | |
125 | struct vnet_port *port = __tx_port_find(vp, skb); | |
126 | ||
127 | if (!port) | |
128 | return 0; | |
129 | ||
130 | return port->q_index; | |
131 | } | |
132 | ||
133 | /* Wrappers to common functions */ | |
134 | static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev) | |
135 | { | |
136 | return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find); | |
137 | } | |
138 | ||
139 | static void vnet_set_rx_mode(struct net_device *dev) | |
140 | { | |
141 | struct vnet *vp = netdev_priv(dev); | |
142 | ||
143 | return sunvnet_set_rx_mode_common(dev, vp); | |
144 | } | |
145 | ||
146 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
147 | static void vnet_poll_controller(struct net_device *dev) | |
148 | { | |
149 | struct vnet *vp = netdev_priv(dev); | |
150 | ||
151 | return sunvnet_poll_controller_common(dev, vp); | |
152 | } | |
153 | #endif | |
154 | ||
8fd17f2e | 155 | static const struct net_device_ops vnet_ops = { |
31762eaa AY |
156 | .ndo_open = sunvnet_open_common, |
157 | .ndo_stop = sunvnet_close_common, | |
67d0719f | 158 | .ndo_set_rx_mode = vnet_set_rx_mode, |
31762eaa | 159 | .ndo_set_mac_address = sunvnet_set_mac_addr_common, |
240c102d | 160 | .ndo_validate_addr = eth_validate_addr, |
31762eaa | 161 | .ndo_tx_timeout = sunvnet_tx_timeout_common, |
67d0719f AY |
162 | .ndo_start_xmit = vnet_start_xmit, |
163 | .ndo_select_queue = vnet_select_queue, | |
69088822 | 164 | #ifdef CONFIG_NET_POLL_CONTROLLER |
67d0719f | 165 | .ndo_poll_controller = vnet_poll_controller, |
69088822 | 166 | #endif |
8fd17f2e DM |
167 | }; |
168 | ||
4c5d283a SV |
169 | static struct vnet *vnet_new(const u64 *local_mac, |
170 | struct vio_dev *vdev) | |
9184a046 DM |
171 | { |
172 | struct net_device *dev; | |
173 | struct vnet *vp; | |
174 | int err, i; | |
175 | ||
d51bffd1 | 176 | dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1); |
41de8d4c | 177 | if (!dev) |
9184a046 | 178 | return ERR_PTR(-ENOMEM); |
8e845f4c DS |
179 | dev->needed_headroom = VNET_PACKET_SKIP + 8; |
180 | dev->needed_tailroom = 8; | |
9184a046 DM |
181 | |
182 | for (i = 0; i < ETH_ALEN; i++) | |
183 | dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff; | |
184 | ||
9184a046 DM |
185 | vp = netdev_priv(dev); |
186 | ||
187 | spin_lock_init(&vp->lock); | |
188 | vp->dev = dev; | |
189 | ||
190 | INIT_LIST_HEAD(&vp->port_list); | |
191 | for (i = 0; i < VNET_PORT_HASH_SIZE; i++) | |
192 | INIT_HLIST_HEAD(&vp->port_hash[i]); | |
193 | INIT_LIST_HEAD(&vp->list); | |
194 | vp->local_mac = *local_mac; | |
195 | ||
8fd17f2e | 196 | dev->netdev_ops = &vnet_ops; |
9184a046 DM |
197 | dev->ethtool_ops = &vnet_ethtool_ops; |
198 | dev->watchdog_timeo = VNET_TX_TIMEOUT; | |
9184a046 | 199 | |
368e36ed | 200 | dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_GSO_SOFTWARE | |
9a72dd4d | 201 | NETIF_F_HW_CSUM | NETIF_F_SG; |
da38c564 DS |
202 | dev->features = dev->hw_features; |
203 | ||
540bfe30 JW |
204 | /* MTU range: 68 - 65535 */ |
205 | dev->min_mtu = ETH_MIN_MTU; | |
206 | dev->max_mtu = VNET_MAX_MTU; | |
207 | ||
4c5d283a SV |
208 | SET_NETDEV_DEV(dev, &vdev->dev); |
209 | ||
9184a046 DM |
210 | err = register_netdev(dev); |
211 | if (err) { | |
4d5870ec | 212 | pr_err("Cannot register net device, aborting\n"); |
9184a046 DM |
213 | goto err_out_free_dev; |
214 | } | |
215 | ||
4d5870ec | 216 | netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr); |
9184a046 DM |
217 | |
218 | list_add(&vp->list, &vnet_list); | |
219 | ||
220 | return vp; | |
221 | ||
222 | err_out_free_dev: | |
223 | free_netdev(dev); | |
224 | ||
225 | return ERR_PTR(err); | |
226 | } | |
227 | ||
4c5d283a SV |
228 | static struct vnet *vnet_find_or_create(const u64 *local_mac, |
229 | struct vio_dev *vdev) | |
9184a046 DM |
230 | { |
231 | struct vnet *iter, *vp; | |
232 | ||
233 | mutex_lock(&vnet_list_mutex); | |
234 | vp = NULL; | |
235 | list_for_each_entry(iter, &vnet_list, list) { | |
236 | if (iter->local_mac == *local_mac) { | |
237 | vp = iter; | |
238 | break; | |
239 | } | |
240 | } | |
241 | if (!vp) | |
4c5d283a | 242 | vp = vnet_new(local_mac, vdev); |
9184a046 DM |
243 | mutex_unlock(&vnet_list_mutex); |
244 | ||
245 | return vp; | |
246 | } | |
247 | ||
a4b70a07 SV |
248 | static void vnet_cleanup(void) |
249 | { | |
250 | struct vnet *vp; | |
251 | struct net_device *dev; | |
252 | ||
253 | mutex_lock(&vnet_list_mutex); | |
254 | while (!list_empty(&vnet_list)) { | |
255 | vp = list_first_entry(&vnet_list, struct vnet, list); | |
256 | list_del(&vp->list); | |
257 | dev = vp->dev; | |
258 | /* vio_unregister_driver() should have cleaned up port_list */ | |
259 | BUG_ON(!list_empty(&vp->port_list)); | |
260 | unregister_netdev(dev); | |
261 | free_netdev(dev); | |
262 | } | |
263 | mutex_unlock(&vnet_list_mutex); | |
264 | } | |
265 | ||
9184a046 DM |
266 | static const char *local_mac_prop = "local-mac-address"; |
267 | ||
f73d12bd | 268 | static struct vnet *vnet_find_parent(struct mdesc_handle *hp, |
4c5d283a SV |
269 | u64 port_node, |
270 | struct vio_dev *vdev) | |
9184a046 DM |
271 | { |
272 | const u64 *local_mac = NULL; | |
273 | u64 a; | |
274 | ||
275 | mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) { | |
276 | u64 target = mdesc_arc_target(hp, a); | |
277 | const char *name; | |
278 | ||
279 | name = mdesc_get_property(hp, target, "name", NULL); | |
280 | if (!name || strcmp(name, "network")) | |
281 | continue; | |
282 | ||
283 | local_mac = mdesc_get_property(hp, target, | |
284 | local_mac_prop, NULL); | |
285 | if (local_mac) | |
286 | break; | |
287 | } | |
288 | if (!local_mac) | |
289 | return ERR_PTR(-ENODEV); | |
290 | ||
4c5d283a | 291 | return vnet_find_or_create(local_mac, vdev); |
9184a046 DM |
292 | } |
293 | ||
4c521e42 | 294 | static struct ldc_channel_config vnet_ldc_cfg = { |
31762eaa | 295 | .event = sunvnet_event_common, |
4c521e42 DM |
296 | .mtu = 64, |
297 | .mode = LDC_MODE_UNRELIABLE, | |
298 | }; | |
299 | ||
300 | static struct vio_driver_ops vnet_vio_ops = { | |
31762eaa AY |
301 | .send_attr = sunvnet_send_attr_common, |
302 | .handle_attr = sunvnet_handle_attr_common, | |
303 | .handshake_complete = sunvnet_handshake_complete_common, | |
4c521e42 DM |
304 | }; |
305 | ||
f73d12bd | 306 | static void print_version(void) |
9184a046 | 307 | { |
4d5870ec | 308 | printk_once(KERN_INFO "%s", version); |
9184a046 DM |
309 | } |
310 | ||
4c521e42 DM |
311 | const char *remote_macaddr_prop = "remote-mac-address"; |
312 | ||
1dd06ae8 | 313 | static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id) |
4c521e42 | 314 | { |
43fdf274 | 315 | struct mdesc_handle *hp; |
4c521e42 DM |
316 | struct vnet_port *port; |
317 | unsigned long flags; | |
318 | struct vnet *vp; | |
319 | const u64 *rmac; | |
320 | int len, i, err, switch_port; | |
321 | ||
9184a046 | 322 | print_version(); |
4c521e42 | 323 | |
43fdf274 DM |
324 | hp = mdesc_grab(); |
325 | ||
4c5d283a | 326 | vp = vnet_find_parent(hp, vdev->mp, vdev); |
9184a046 | 327 | if (IS_ERR(vp)) { |
4d5870ec | 328 | pr_err("Cannot find port parent vnet\n"); |
9184a046 DM |
329 | err = PTR_ERR(vp); |
330 | goto err_out_put_mdesc; | |
331 | } | |
332 | ||
43fdf274 DM |
333 | rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len); |
334 | err = -ENODEV; | |
4c521e42 | 335 | if (!rmac) { |
4d5870ec | 336 | pr_err("Port lacks %s property\n", remote_macaddr_prop); |
43fdf274 | 337 | goto err_out_put_mdesc; |
4c521e42 DM |
338 | } |
339 | ||
340 | port = kzalloc(sizeof(*port), GFP_KERNEL); | |
43fdf274 | 341 | err = -ENOMEM; |
e404decb | 342 | if (!port) |
43fdf274 | 343 | goto err_out_put_mdesc; |
4c521e42 DM |
344 | |
345 | for (i = 0; i < ETH_ALEN; i++) | |
346 | port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff; | |
347 | ||
348 | port->vp = vp; | |
349 | ||
43fdf274 | 350 | err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK, |
4c521e42 DM |
351 | vnet_versions, ARRAY_SIZE(vnet_versions), |
352 | &vnet_vio_ops, vp->dev->name); | |
353 | if (err) | |
354 | goto err_out_free_port; | |
355 | ||
356 | err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port); | |
357 | if (err) | |
358 | goto err_out_free_port; | |
359 | ||
31762eaa AY |
360 | netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common, |
361 | NAPI_POLL_WEIGHT); | |
69088822 | 362 | |
4c521e42 DM |
363 | INIT_HLIST_NODE(&port->hash); |
364 | INIT_LIST_HEAD(&port->list); | |
365 | ||
366 | switch_port = 0; | |
dc153f85 | 367 | if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL)) |
4c521e42 | 368 | switch_port = 1; |
028ebff2 | 369 | port->switch_port = switch_port; |
368e36ed DS |
370 | port->tso = true; |
371 | port->tsolen = 0; | |
4c521e42 DM |
372 | |
373 | spin_lock_irqsave(&vp->lock, flags); | |
374 | if (switch_port) | |
2a968dd8 | 375 | list_add_rcu(&port->list, &vp->port_list); |
4c521e42 | 376 | else |
2a968dd8 SV |
377 | list_add_tail_rcu(&port->list, &vp->port_list); |
378 | hlist_add_head_rcu(&port->hash, | |
379 | &vp->port_hash[vnet_hashfn(port->raddr)]); | |
31762eaa | 380 | sunvnet_port_add_txq_common(port); |
4c521e42 DM |
381 | spin_unlock_irqrestore(&vp->lock, flags); |
382 | ||
383 | dev_set_drvdata(&vdev->dev, port); | |
384 | ||
4d5870ec JP |
385 | pr_info("%s: PORT ( remote-mac %pM%s )\n", |
386 | vp->dev->name, port->raddr, switch_port ? " switch-port" : ""); | |
4c521e42 | 387 | |
31762eaa | 388 | setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common, |
8e845f4c DS |
389 | (unsigned long)port); |
390 | ||
69088822 | 391 | napi_enable(&port->napi); |
4c521e42 DM |
392 | vio_port_up(&port->vio); |
393 | ||
43fdf274 DM |
394 | mdesc_release(hp); |
395 | ||
4c521e42 DM |
396 | return 0; |
397 | ||
4c521e42 DM |
398 | err_out_free_port: |
399 | kfree(port); | |
400 | ||
43fdf274 DM |
401 | err_out_put_mdesc: |
402 | mdesc_release(hp); | |
4c521e42 DM |
403 | return err; |
404 | } | |
405 | ||
406 | static int vnet_port_remove(struct vio_dev *vdev) | |
407 | { | |
408 | struct vnet_port *port = dev_get_drvdata(&vdev->dev); | |
409 | ||
410 | if (port) { | |
4c521e42 DM |
411 | del_timer_sync(&port->vio.timer); |
412 | ||
69088822 | 413 | napi_disable(&port->napi); |
4c521e42 | 414 | |
2a968dd8 SV |
415 | list_del_rcu(&port->list); |
416 | hlist_del_rcu(&port->hash); | |
417 | ||
418 | synchronize_rcu(); | |
419 | del_timer_sync(&port->clean_timer); | |
31762eaa | 420 | sunvnet_port_rm_txq_common(port); |
69088822 | 421 | netif_napi_del(&port->napi); |
31762eaa | 422 | sunvnet_port_free_tx_bufs_common(port); |
4c521e42 DM |
423 | vio_ldc_free(&port->vio); |
424 | ||
425 | dev_set_drvdata(&vdev->dev, NULL); | |
426 | ||
427 | kfree(port); | |
428 | } | |
429 | return 0; | |
430 | } | |
431 | ||
3d452e55 | 432 | static const struct vio_device_id vnet_port_match[] = { |
4c521e42 DM |
433 | { |
434 | .type = "vnet-port", | |
435 | }, | |
436 | {}, | |
437 | }; | |
da68e081 | 438 | MODULE_DEVICE_TABLE(vio, vnet_port_match); |
4c521e42 DM |
439 | |
440 | static struct vio_driver vnet_port_driver = { | |
441 | .id_table = vnet_port_match, | |
442 | .probe = vnet_port_probe, | |
443 | .remove = vnet_port_remove, | |
cb52d897 | 444 | .name = "vnet_port", |
4c521e42 DM |
445 | }; |
446 | ||
4c521e42 DM |
447 | static int __init vnet_init(void) |
448 | { | |
9184a046 | 449 | return vio_register_driver(&vnet_port_driver); |
4c521e42 DM |
450 | } |
451 | ||
452 | static void __exit vnet_exit(void) | |
453 | { | |
454 | vio_unregister_driver(&vnet_port_driver); | |
a4b70a07 | 455 | vnet_cleanup(); |
4c521e42 DM |
456 | } |
457 | ||
458 | module_init(vnet_init); | |
459 | module_exit(vnet_exit); |