blob: a94f50442613e9f77cec6aff24fbf19a5a33756b [file] [log] [blame]
Shannon Nelsonc861ef82018-02-06 11:34:23 -08001// SPDX-License-Identifier: GPL-2.0
David S. Miller4c521e42007-07-09 22:23:51 -07002/* sunvnet.c: Sun LDOM Virtual Network Driver.
3 *
David S. Miller3d452e52008-09-01 01:48:52 -07004 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
Shannon Nelson0f512c82017-03-14 10:24:40 -07005 * Copyright (C) 2016-2017 Oracle. All rights reserved.
David S. Miller4c521e42007-07-09 22:23:51 -07006 */
7
Joe Perches4d5870e2010-08-17 07:55:05 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
David S. Miller4c521e42007-07-09 22:23:51 -070010#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/delay.h>
15#include <linux/init.h>
16#include <linux/netdevice.h>
17#include <linux/ethtool.h>
18#include <linux/etherdevice.h>
David S. Miller9184a042007-07-17 22:19:10 -070019#include <linux/mutex.h>
David L Stevensda38c562014-12-02 15:31:13 -050020#include <linux/highmem.h>
David L Stevense4defc72014-09-29 19:47:59 -040021#include <linux/if_vlan.h>
David S. Miller4c521e42007-07-09 22:23:51 -070022
David L Stevensa2b78e92014-09-29 19:48:24 -040023#if IS_ENABLED(CONFIG_IPV6)
24#include <linux/icmpv6.h>
25#endif
26
David L Stevens6d0ba912014-12-02 15:31:04 -050027#include <net/ip.h>
David L Stevensa2b78e92014-09-29 19:48:24 -040028#include <net/icmp.h>
29#include <net/route.h>
30
David S. Miller4c521e42007-07-09 22:23:51 -070031#include <asm/vio.h>
32#include <asm/ldc.h>
33
Aaron Young31762ea2016-03-15 11:35:37 -070034#include "sunvnet_common.h"
35
36/* length of time before we decide the hardware is borked,
37 * and dev->tx_timeout() should be called to fix the problem
38 */
39#define VNET_TX_TIMEOUT (5 * HZ)
David S. Miller4c521e42007-07-09 22:23:51 -070040
41#define DRV_MODULE_NAME "sunvnet"
Shannon Nelsonf2f3e212017-02-13 10:56:59 -080042#define DRV_MODULE_VERSION "2.0"
43#define DRV_MODULE_RELDATE "February 3, 2017"
David S. Miller4c521e42007-07-09 22:23:51 -070044
Bill Pembertonf73d12b2012-12-03 09:24:02 -050045static char version[] =
Shannon Nelsonf2f3e212017-02-13 10:56:59 -080046 DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
David S. Miller4c521e42007-07-09 22:23:51 -070047MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
48MODULE_DESCRIPTION("Sun LDOM virtual network driver");
49MODULE_LICENSE("GPL");
50MODULE_VERSION(DRV_MODULE_VERSION);
51
52/* Ordered from largest major to lowest */
53static struct vio_version vnet_versions[] = {
David L Stevens6d0ba912014-12-02 15:31:04 -050054 { .major = 1, .minor = 8 },
55 { .major = 1, .minor = 7 },
David L Stevense4defc72014-09-29 19:47:59 -040056 { .major = 1, .minor = 6 },
David S. Miller4c521e42007-07-09 22:23:51 -070057 { .major = 1, .minor = 0 },
58};
59
David S. Miller4c521e42007-07-09 22:23:51 -070060static void vnet_get_drvinfo(struct net_device *dev,
61 struct ethtool_drvinfo *info)
62{
Jiri Pirko7826d432013-01-06 00:44:26 +000063 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
64 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
David S. Miller4c521e42007-07-09 22:23:51 -070065}
66
67static u32 vnet_get_msglevel(struct net_device *dev)
68{
69 struct vnet *vp = netdev_priv(dev);
Aaron Youngdc153f82016-03-15 11:35:40 -070070
David S. Miller4c521e42007-07-09 22:23:51 -070071 return vp->msg_enable;
72}
73
74static void vnet_set_msglevel(struct net_device *dev, u32 value)
75{
76 struct vnet *vp = netdev_priv(dev);
Aaron Youngdc153f82016-03-15 11:35:40 -070077
David S. Miller4c521e42007-07-09 22:23:51 -070078 vp->msg_enable = value;
79}
80
Shannon Nelson0f512c82017-03-14 10:24:40 -070081static const struct {
82 const char string[ETH_GSTRING_LEN];
83} ethtool_stats_keys[] = {
84 { "rx_packets" },
85 { "tx_packets" },
86 { "rx_bytes" },
87 { "tx_bytes" },
88 { "rx_errors" },
89 { "tx_errors" },
90 { "rx_dropped" },
91 { "tx_dropped" },
92 { "multicast" },
93 { "rx_length_errors" },
94 { "rx_frame_errors" },
95 { "rx_missed_errors" },
96 { "tx_carrier_errors" },
97 { "nports" },
98};
99
100static int vnet_get_sset_count(struct net_device *dev, int sset)
101{
102 struct vnet *vp = (struct vnet *)netdev_priv(dev);
103
104 switch (sset) {
105 case ETH_SS_STATS:
106 return ARRAY_SIZE(ethtool_stats_keys)
107 + (NUM_VNET_PORT_STATS * vp->nports);
108 default:
109 return -EOPNOTSUPP;
110 }
111}
112
113static void vnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
114{
115 struct vnet *vp = (struct vnet *)netdev_priv(dev);
116 struct vnet_port *port;
117 char *p = (char *)buf;
118
119 switch (stringset) {
120 case ETH_SS_STATS:
121 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
122 p += sizeof(ethtool_stats_keys);
123
124 rcu_read_lock();
125 list_for_each_entry_rcu(port, &vp->port_list, list) {
126 snprintf(p, ETH_GSTRING_LEN, "p%u.%s-%pM",
127 port->q_index, port->switch_port ? "s" : "q",
128 port->raddr);
129 p += ETH_GSTRING_LEN;
130 snprintf(p, ETH_GSTRING_LEN, "p%u.rx_packets",
131 port->q_index);
132 p += ETH_GSTRING_LEN;
133 snprintf(p, ETH_GSTRING_LEN, "p%u.tx_packets",
134 port->q_index);
135 p += ETH_GSTRING_LEN;
136 snprintf(p, ETH_GSTRING_LEN, "p%u.rx_bytes",
137 port->q_index);
138 p += ETH_GSTRING_LEN;
139 snprintf(p, ETH_GSTRING_LEN, "p%u.tx_bytes",
140 port->q_index);
141 p += ETH_GSTRING_LEN;
142 snprintf(p, ETH_GSTRING_LEN, "p%u.event_up",
143 port->q_index);
144 p += ETH_GSTRING_LEN;
145 snprintf(p, ETH_GSTRING_LEN, "p%u.event_reset",
146 port->q_index);
147 p += ETH_GSTRING_LEN;
148 }
149 rcu_read_unlock();
150 break;
151 default:
152 WARN_ON(1);
153 break;
154 }
155}
156
157static void vnet_get_ethtool_stats(struct net_device *dev,
158 struct ethtool_stats *estats, u64 *data)
159{
160 struct vnet *vp = (struct vnet *)netdev_priv(dev);
161 struct vnet_port *port;
162 int i = 0;
163
164 data[i++] = dev->stats.rx_packets;
165 data[i++] = dev->stats.tx_packets;
166 data[i++] = dev->stats.rx_bytes;
167 data[i++] = dev->stats.tx_bytes;
168 data[i++] = dev->stats.rx_errors;
169 data[i++] = dev->stats.tx_errors;
170 data[i++] = dev->stats.rx_dropped;
171 data[i++] = dev->stats.tx_dropped;
172 data[i++] = dev->stats.multicast;
173 data[i++] = dev->stats.rx_length_errors;
174 data[i++] = dev->stats.rx_frame_errors;
175 data[i++] = dev->stats.rx_missed_errors;
176 data[i++] = dev->stats.tx_carrier_errors;
177 data[i++] = vp->nports;
178
179 rcu_read_lock();
180 list_for_each_entry_rcu(port, &vp->port_list, list) {
181 data[i++] = port->q_index;
182 data[i++] = port->stats.rx_packets;
183 data[i++] = port->stats.tx_packets;
184 data[i++] = port->stats.rx_bytes;
185 data[i++] = port->stats.tx_bytes;
186 data[i++] = port->stats.event_up;
187 data[i++] = port->stats.event_reset;
188 }
189 rcu_read_unlock();
190}
191
David S. Miller4c521e42007-07-09 22:23:51 -0700192static const struct ethtool_ops vnet_ethtool_ops = {
193 .get_drvinfo = vnet_get_drvinfo,
194 .get_msglevel = vnet_get_msglevel,
195 .set_msglevel = vnet_set_msglevel,
196 .get_link = ethtool_op_get_link,
Shannon Nelson0f512c82017-03-14 10:24:40 -0700197 .get_sset_count = vnet_get_sset_count,
198 .get_strings = vnet_get_strings,
199 .get_ethtool_stats = vnet_get_ethtool_stats,
David S. Miller4c521e42007-07-09 22:23:51 -0700200};
201
David S. Miller9184a042007-07-17 22:19:10 -0700202static LIST_HEAD(vnet_list);
203static DEFINE_MUTEX(vnet_list_mutex);
204
Aaron Young67d07192016-03-15 11:35:38 -0700205static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
206{
207 unsigned int hash = vnet_hashfn(skb->data);
208 struct hlist_head *hp = &vp->port_hash[hash];
209 struct vnet_port *port;
210
211 hlist_for_each_entry_rcu(port, hp, hash) {
212 if (!sunvnet_port_is_up_common(port))
213 continue;
214 if (ether_addr_equal(port->raddr, skb->data))
215 return port;
216 }
217 list_for_each_entry_rcu(port, &vp->port_list, list) {
218 if (!port->switch_port)
219 continue;
220 if (!sunvnet_port_is_up_common(port))
221 continue;
222 return port;
223 }
224 return NULL;
225}
226
227/* func arg to vnet_start_xmit_common() to get the proper tx port */
228static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb,
229 struct net_device *dev)
230{
231 struct vnet *vp = netdev_priv(dev);
232
233 return __tx_port_find(vp, skb);
234}
235
236static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
237 void *accel_priv, select_queue_fallback_t fallback)
238{
239 struct vnet *vp = netdev_priv(dev);
240 struct vnet_port *port = __tx_port_find(vp, skb);
241
242 if (!port)
243 return 0;
244
245 return port->q_index;
246}
247
248/* Wrappers to common functions */
249static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
250{
251 return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find);
252}
253
254static void vnet_set_rx_mode(struct net_device *dev)
255{
256 struct vnet *vp = netdev_priv(dev);
257
258 return sunvnet_set_rx_mode_common(dev, vp);
259}
260
261#ifdef CONFIG_NET_POLL_CONTROLLER
262static void vnet_poll_controller(struct net_device *dev)
263{
264 struct vnet *vp = netdev_priv(dev);
265
266 return sunvnet_poll_controller_common(dev, vp);
267}
268#endif
269
David S. Miller8fd17f22009-03-20 00:51:22 -0700270static const struct net_device_ops vnet_ops = {
Aaron Young31762ea2016-03-15 11:35:37 -0700271 .ndo_open = sunvnet_open_common,
272 .ndo_stop = sunvnet_close_common,
Aaron Young67d07192016-03-15 11:35:38 -0700273 .ndo_set_rx_mode = vnet_set_rx_mode,
Aaron Young31762ea2016-03-15 11:35:37 -0700274 .ndo_set_mac_address = sunvnet_set_mac_addr_common,
Ben Hutchings240c1022009-07-09 17:54:35 +0000275 .ndo_validate_addr = eth_validate_addr,
Aaron Young31762ea2016-03-15 11:35:37 -0700276 .ndo_tx_timeout = sunvnet_tx_timeout_common,
Aaron Young67d07192016-03-15 11:35:38 -0700277 .ndo_start_xmit = vnet_start_xmit,
278 .ndo_select_queue = vnet_select_queue,
Sowmini Varadhan69088822014-10-25 15:12:12 -0400279#ifdef CONFIG_NET_POLL_CONTROLLER
Aaron Young67d07192016-03-15 11:35:38 -0700280 .ndo_poll_controller = vnet_poll_controller,
Sowmini Varadhan69088822014-10-25 15:12:12 -0400281#endif
David S. Miller8fd17f22009-03-20 00:51:22 -0700282};
283
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400284static struct vnet *vnet_new(const u64 *local_mac,
285 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700286{
287 struct net_device *dev;
288 struct vnet *vp;
289 int err, i;
290
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400291 dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
Joe Perches41de8d42012-01-29 13:47:52 +0000292 if (!dev)
David S. Miller9184a042007-07-17 22:19:10 -0700293 return ERR_PTR(-ENOMEM);
David L Stevens8e845f4c2014-09-29 19:48:11 -0400294 dev->needed_headroom = VNET_PACKET_SKIP + 8;
295 dev->needed_tailroom = 8;
David S. Miller9184a042007-07-17 22:19:10 -0700296
297 for (i = 0; i < ETH_ALEN; i++)
298 dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
299
David S. Miller9184a042007-07-17 22:19:10 -0700300 vp = netdev_priv(dev);
301
302 spin_lock_init(&vp->lock);
303 vp->dev = dev;
304
305 INIT_LIST_HEAD(&vp->port_list);
306 for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
307 INIT_HLIST_HEAD(&vp->port_hash[i]);
308 INIT_LIST_HEAD(&vp->list);
309 vp->local_mac = *local_mac;
310
David S. Miller8fd17f22009-03-20 00:51:22 -0700311 dev->netdev_ops = &vnet_ops;
David S. Miller9184a042007-07-17 22:19:10 -0700312 dev->ethtool_ops = &vnet_ethtool_ops;
313 dev->watchdog_timeo = VNET_TX_TIMEOUT;
David S. Miller9184a042007-07-17 22:19:10 -0700314
Cathy Zhoucf556122018-03-14 10:56:07 -0700315 dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_ALL_TSO |
Shannon Nelson98524e02017-07-06 16:57:10 -0700316 NETIF_F_HW_CSUM | NETIF_F_SG;
David L Stevensda38c562014-12-02 15:31:13 -0500317 dev->features = dev->hw_features;
318
Jarod Wilson540bfe32016-10-17 15:54:10 -0400319 /* MTU range: 68 - 65535 */
320 dev->min_mtu = ETH_MIN_MTU;
321 dev->max_mtu = VNET_MAX_MTU;
322
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400323 SET_NETDEV_DEV(dev, &vdev->dev);
324
David S. Miller9184a042007-07-17 22:19:10 -0700325 err = register_netdev(dev);
326 if (err) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000327 pr_err("Cannot register net device, aborting\n");
David S. Miller9184a042007-07-17 22:19:10 -0700328 goto err_out_free_dev;
329 }
330
Joe Perches4d5870e2010-08-17 07:55:05 +0000331 netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
David S. Miller9184a042007-07-17 22:19:10 -0700332
333 list_add(&vp->list, &vnet_list);
334
335 return vp;
336
337err_out_free_dev:
338 free_netdev(dev);
339
340 return ERR_PTR(err);
341}
342
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400343static struct vnet *vnet_find_or_create(const u64 *local_mac,
344 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700345{
346 struct vnet *iter, *vp;
347
348 mutex_lock(&vnet_list_mutex);
349 vp = NULL;
350 list_for_each_entry(iter, &vnet_list, list) {
351 if (iter->local_mac == *local_mac) {
352 vp = iter;
353 break;
354 }
355 }
356 if (!vp)
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400357 vp = vnet_new(local_mac, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700358 mutex_unlock(&vnet_list_mutex);
359
360 return vp;
361}
362
Sowmini Varadhana4b70a02014-07-16 10:02:26 -0400363static void vnet_cleanup(void)
364{
365 struct vnet *vp;
366 struct net_device *dev;
367
368 mutex_lock(&vnet_list_mutex);
369 while (!list_empty(&vnet_list)) {
370 vp = list_first_entry(&vnet_list, struct vnet, list);
371 list_del(&vp->list);
372 dev = vp->dev;
373 /* vio_unregister_driver() should have cleaned up port_list */
374 BUG_ON(!list_empty(&vp->port_list));
375 unregister_netdev(dev);
376 free_netdev(dev);
377 }
378 mutex_unlock(&vnet_list_mutex);
379}
380
David S. Miller9184a042007-07-17 22:19:10 -0700381static const char *local_mac_prop = "local-mac-address";
382
Bill Pembertonf73d12b2012-12-03 09:24:02 -0500383static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400384 u64 port_node,
385 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700386{
387 const u64 *local_mac = NULL;
388 u64 a;
389
390 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
391 u64 target = mdesc_arc_target(hp, a);
392 const char *name;
393
394 name = mdesc_get_property(hp, target, "name", NULL);
395 if (!name || strcmp(name, "network"))
396 continue;
397
398 local_mac = mdesc_get_property(hp, target,
399 local_mac_prop, NULL);
400 if (local_mac)
401 break;
402 }
403 if (!local_mac)
404 return ERR_PTR(-ENODEV);
405
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400406 return vnet_find_or_create(local_mac, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700407}
408
David S. Miller4c521e42007-07-09 22:23:51 -0700409static struct ldc_channel_config vnet_ldc_cfg = {
Aaron Young31762ea2016-03-15 11:35:37 -0700410 .event = sunvnet_event_common,
David S. Miller4c521e42007-07-09 22:23:51 -0700411 .mtu = 64,
412 .mode = LDC_MODE_UNRELIABLE,
413};
414
415static struct vio_driver_ops vnet_vio_ops = {
Aaron Young31762ea2016-03-15 11:35:37 -0700416 .send_attr = sunvnet_send_attr_common,
417 .handle_attr = sunvnet_handle_attr_common,
418 .handshake_complete = sunvnet_handshake_complete_common,
David S. Miller4c521e42007-07-09 22:23:51 -0700419};
420
421const char *remote_macaddr_prop = "remote-mac-address";
422
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000423static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller4c521e42007-07-09 22:23:51 -0700424{
David S. Miller43fdf272007-07-12 13:47:50 -0700425 struct mdesc_handle *hp;
David S. Miller4c521e42007-07-09 22:23:51 -0700426 struct vnet_port *port;
427 unsigned long flags;
428 struct vnet *vp;
429 const u64 *rmac;
430 int len, i, err, switch_port;
431
David S. Miller43fdf272007-07-12 13:47:50 -0700432 hp = mdesc_grab();
433
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400434 vp = vnet_find_parent(hp, vdev->mp, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700435 if (IS_ERR(vp)) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000436 pr_err("Cannot find port parent vnet\n");
David S. Miller9184a042007-07-17 22:19:10 -0700437 err = PTR_ERR(vp);
438 goto err_out_put_mdesc;
439 }
440
David S. Miller43fdf272007-07-12 13:47:50 -0700441 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
442 err = -ENODEV;
David S. Miller4c521e42007-07-09 22:23:51 -0700443 if (!rmac) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000444 pr_err("Port lacks %s property\n", remote_macaddr_prop);
David S. Miller43fdf272007-07-12 13:47:50 -0700445 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -0700446 }
447
448 port = kzalloc(sizeof(*port), GFP_KERNEL);
David S. Miller43fdf272007-07-12 13:47:50 -0700449 err = -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +0000450 if (!port)
David S. Miller43fdf272007-07-12 13:47:50 -0700451 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -0700452
453 for (i = 0; i < ETH_ALEN; i++)
454 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
455
456 port->vp = vp;
457
David S. Miller43fdf272007-07-12 13:47:50 -0700458 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
David S. Miller4c521e42007-07-09 22:23:51 -0700459 vnet_versions, ARRAY_SIZE(vnet_versions),
460 &vnet_vio_ops, vp->dev->name);
461 if (err)
462 goto err_out_free_port;
463
464 err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
465 if (err)
466 goto err_out_free_port;
467
Aaron Young31762ea2016-03-15 11:35:37 -0700468 netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common,
469 NAPI_POLL_WEIGHT);
Sowmini Varadhan69088822014-10-25 15:12:12 -0400470
David S. Miller4c521e42007-07-09 22:23:51 -0700471 INIT_HLIST_NODE(&port->hash);
472 INIT_LIST_HEAD(&port->list);
473
474 switch_port = 0;
Aaron Youngdc153f82016-03-15 11:35:40 -0700475 if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL))
David S. Miller4c521e42007-07-09 22:23:51 -0700476 switch_port = 1;
David S. Miller028ebff2007-07-20 02:30:25 -0700477 port->switch_port = switch_port;
David L Stevens368e36e2014-12-02 15:31:38 -0500478 port->tso = true;
479 port->tsolen = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700480
481 spin_lock_irqsave(&vp->lock, flags);
482 if (switch_port)
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400483 list_add_rcu(&port->list, &vp->port_list);
David S. Miller4c521e42007-07-09 22:23:51 -0700484 else
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400485 list_add_tail_rcu(&port->list, &vp->port_list);
486 hlist_add_head_rcu(&port->hash,
487 &vp->port_hash[vnet_hashfn(port->raddr)]);
Aaron Young31762ea2016-03-15 11:35:37 -0700488 sunvnet_port_add_txq_common(port);
David S. Miller4c521e42007-07-09 22:23:51 -0700489 spin_unlock_irqrestore(&vp->lock, flags);
490
491 dev_set_drvdata(&vdev->dev, port);
492
Joe Perches4d5870e2010-08-17 07:55:05 +0000493 pr_info("%s: PORT ( remote-mac %pM%s )\n",
494 vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
David S. Miller4c521e42007-07-09 22:23:51 -0700495
Kees Cook0822c5d2017-10-16 17:29:28 -0700496 timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0);
David L Stevens8e845f4c2014-09-29 19:48:11 -0400497
Sowmini Varadhan69088822014-10-25 15:12:12 -0400498 napi_enable(&port->napi);
David S. Miller4c521e42007-07-09 22:23:51 -0700499 vio_port_up(&port->vio);
500
David S. Miller43fdf272007-07-12 13:47:50 -0700501 mdesc_release(hp);
502
David S. Miller4c521e42007-07-09 22:23:51 -0700503 return 0;
504
David S. Miller4c521e42007-07-09 22:23:51 -0700505err_out_free_port:
506 kfree(port);
507
David S. Miller43fdf272007-07-12 13:47:50 -0700508err_out_put_mdesc:
509 mdesc_release(hp);
David S. Miller4c521e42007-07-09 22:23:51 -0700510 return err;
511}
512
513static int vnet_port_remove(struct vio_dev *vdev)
514{
515 struct vnet_port *port = dev_get_drvdata(&vdev->dev);
516
517 if (port) {
David S. Miller4c521e42007-07-09 22:23:51 -0700518 del_timer_sync(&port->vio.timer);
519
Sowmini Varadhan69088822014-10-25 15:12:12 -0400520 napi_disable(&port->napi);
David S. Miller4c521e42007-07-09 22:23:51 -0700521
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400522 list_del_rcu(&port->list);
523 hlist_del_rcu(&port->hash);
524
525 synchronize_rcu();
526 del_timer_sync(&port->clean_timer);
Aaron Young31762ea2016-03-15 11:35:37 -0700527 sunvnet_port_rm_txq_common(port);
Sowmini Varadhan69088822014-10-25 15:12:12 -0400528 netif_napi_del(&port->napi);
Aaron Young31762ea2016-03-15 11:35:37 -0700529 sunvnet_port_free_tx_bufs_common(port);
David S. Miller4c521e42007-07-09 22:23:51 -0700530 vio_ldc_free(&port->vio);
531
532 dev_set_drvdata(&vdev->dev, NULL);
533
534 kfree(port);
535 }
536 return 0;
537}
538
David S. Miller3d452e52008-09-01 01:48:52 -0700539static const struct vio_device_id vnet_port_match[] = {
David S. Miller4c521e42007-07-09 22:23:51 -0700540 {
541 .type = "vnet-port",
542 },
543 {},
544};
Fabio Massimo Di Nittoda68e082007-07-18 14:35:23 -0700545MODULE_DEVICE_TABLE(vio, vnet_port_match);
David S. Miller4c521e42007-07-09 22:23:51 -0700546
547static struct vio_driver vnet_port_driver = {
548 .id_table = vnet_port_match,
549 .probe = vnet_port_probe,
550 .remove = vnet_port_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +0000551 .name = "vnet_port",
David S. Miller4c521e42007-07-09 22:23:51 -0700552};
553
David S. Miller4c521e42007-07-09 22:23:51 -0700554static int __init vnet_init(void)
555{
Shannon Nelsonf2f3e212017-02-13 10:56:59 -0800556 pr_info("%s\n", version);
David S. Miller9184a042007-07-17 22:19:10 -0700557 return vio_register_driver(&vnet_port_driver);
David S. Miller4c521e42007-07-09 22:23:51 -0700558}
559
560static void __exit vnet_exit(void)
561{
562 vio_unregister_driver(&vnet_port_driver);
Sowmini Varadhana4b70a02014-07-16 10:02:26 -0400563 vnet_cleanup();
David S. Miller4c521e42007-07-09 22:23:51 -0700564}
565
566module_init(vnet_init);
567module_exit(vnet_exit);