blob: 75b167e3fe98ad3fd6613d59a55d2f697014f4dc [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>
Shannon Nelson0f512c82017-03-14 10:24:40 -07004 * Copyright (C) 2016-2017 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"
Shannon Nelsonf2f3e212017-02-13 10:56:59 -080041#define DRV_MODULE_VERSION "2.0"
42#define DRV_MODULE_RELDATE "February 3, 2017"
David S. Miller4c521e42007-07-09 22:23:51 -070043
Bill Pembertonf73d12b2012-12-03 09:24:02 -050044static char version[] =
Shannon Nelsonf2f3e212017-02-13 10:56:59 -080045 DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
David S. Miller4c521e42007-07-09 22:23:51 -070046MODULE_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);
Aaron Youngdc153f82016-03-15 11:35:40 -070069
David S. Miller4c521e42007-07-09 22:23:51 -070070 return vp->msg_enable;
71}
72
73static void vnet_set_msglevel(struct net_device *dev, u32 value)
74{
75 struct vnet *vp = netdev_priv(dev);
Aaron Youngdc153f82016-03-15 11:35:40 -070076
David S. Miller4c521e42007-07-09 22:23:51 -070077 vp->msg_enable = value;
78}
79
Shannon Nelson0f512c82017-03-14 10:24:40 -070080static const struct {
81 const char string[ETH_GSTRING_LEN];
82} ethtool_stats_keys[] = {
83 { "rx_packets" },
84 { "tx_packets" },
85 { "rx_bytes" },
86 { "tx_bytes" },
87 { "rx_errors" },
88 { "tx_errors" },
89 { "rx_dropped" },
90 { "tx_dropped" },
91 { "multicast" },
92 { "rx_length_errors" },
93 { "rx_frame_errors" },
94 { "rx_missed_errors" },
95 { "tx_carrier_errors" },
96 { "nports" },
97};
98
99static int vnet_get_sset_count(struct net_device *dev, int sset)
100{
101 struct vnet *vp = (struct vnet *)netdev_priv(dev);
102
103 switch (sset) {
104 case ETH_SS_STATS:
105 return ARRAY_SIZE(ethtool_stats_keys)
106 + (NUM_VNET_PORT_STATS * vp->nports);
107 default:
108 return -EOPNOTSUPP;
109 }
110}
111
112static void vnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
113{
114 struct vnet *vp = (struct vnet *)netdev_priv(dev);
115 struct vnet_port *port;
116 char *p = (char *)buf;
117
118 switch (stringset) {
119 case ETH_SS_STATS:
120 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
121 p += sizeof(ethtool_stats_keys);
122
123 rcu_read_lock();
124 list_for_each_entry_rcu(port, &vp->port_list, list) {
125 snprintf(p, ETH_GSTRING_LEN, "p%u.%s-%pM",
126 port->q_index, port->switch_port ? "s" : "q",
127 port->raddr);
128 p += ETH_GSTRING_LEN;
129 snprintf(p, ETH_GSTRING_LEN, "p%u.rx_packets",
130 port->q_index);
131 p += ETH_GSTRING_LEN;
132 snprintf(p, ETH_GSTRING_LEN, "p%u.tx_packets",
133 port->q_index);
134 p += ETH_GSTRING_LEN;
135 snprintf(p, ETH_GSTRING_LEN, "p%u.rx_bytes",
136 port->q_index);
137 p += ETH_GSTRING_LEN;
138 snprintf(p, ETH_GSTRING_LEN, "p%u.tx_bytes",
139 port->q_index);
140 p += ETH_GSTRING_LEN;
141 snprintf(p, ETH_GSTRING_LEN, "p%u.event_up",
142 port->q_index);
143 p += ETH_GSTRING_LEN;
144 snprintf(p, ETH_GSTRING_LEN, "p%u.event_reset",
145 port->q_index);
146 p += ETH_GSTRING_LEN;
147 }
148 rcu_read_unlock();
149 break;
150 default:
151 WARN_ON(1);
152 break;
153 }
154}
155
156static void vnet_get_ethtool_stats(struct net_device *dev,
157 struct ethtool_stats *estats, u64 *data)
158{
159 struct vnet *vp = (struct vnet *)netdev_priv(dev);
160 struct vnet_port *port;
161 int i = 0;
162
163 data[i++] = dev->stats.rx_packets;
164 data[i++] = dev->stats.tx_packets;
165 data[i++] = dev->stats.rx_bytes;
166 data[i++] = dev->stats.tx_bytes;
167 data[i++] = dev->stats.rx_errors;
168 data[i++] = dev->stats.tx_errors;
169 data[i++] = dev->stats.rx_dropped;
170 data[i++] = dev->stats.tx_dropped;
171 data[i++] = dev->stats.multicast;
172 data[i++] = dev->stats.rx_length_errors;
173 data[i++] = dev->stats.rx_frame_errors;
174 data[i++] = dev->stats.rx_missed_errors;
175 data[i++] = dev->stats.tx_carrier_errors;
176 data[i++] = vp->nports;
177
178 rcu_read_lock();
179 list_for_each_entry_rcu(port, &vp->port_list, list) {
180 data[i++] = port->q_index;
181 data[i++] = port->stats.rx_packets;
182 data[i++] = port->stats.tx_packets;
183 data[i++] = port->stats.rx_bytes;
184 data[i++] = port->stats.tx_bytes;
185 data[i++] = port->stats.event_up;
186 data[i++] = port->stats.event_reset;
187 }
188 rcu_read_unlock();
189}
190
David S. Miller4c521e42007-07-09 22:23:51 -0700191static const struct ethtool_ops vnet_ethtool_ops = {
192 .get_drvinfo = vnet_get_drvinfo,
193 .get_msglevel = vnet_get_msglevel,
194 .set_msglevel = vnet_set_msglevel,
195 .get_link = ethtool_op_get_link,
Shannon Nelson0f512c82017-03-14 10:24:40 -0700196 .get_sset_count = vnet_get_sset_count,
197 .get_strings = vnet_get_strings,
198 .get_ethtool_stats = vnet_get_ethtool_stats,
David S. Miller4c521e42007-07-09 22:23:51 -0700199};
200
David S. Miller9184a042007-07-17 22:19:10 -0700201static LIST_HEAD(vnet_list);
202static DEFINE_MUTEX(vnet_list_mutex);
203
Aaron Young67d07192016-03-15 11:35:38 -0700204static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
205{
206 unsigned int hash = vnet_hashfn(skb->data);
207 struct hlist_head *hp = &vp->port_hash[hash];
208 struct vnet_port *port;
209
210 hlist_for_each_entry_rcu(port, hp, hash) {
211 if (!sunvnet_port_is_up_common(port))
212 continue;
213 if (ether_addr_equal(port->raddr, skb->data))
214 return port;
215 }
216 list_for_each_entry_rcu(port, &vp->port_list, list) {
217 if (!port->switch_port)
218 continue;
219 if (!sunvnet_port_is_up_common(port))
220 continue;
221 return port;
222 }
223 return NULL;
224}
225
226/* func arg to vnet_start_xmit_common() to get the proper tx port */
227static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb,
228 struct net_device *dev)
229{
230 struct vnet *vp = netdev_priv(dev);
231
232 return __tx_port_find(vp, skb);
233}
234
235static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
236 void *accel_priv, select_queue_fallback_t fallback)
237{
238 struct vnet *vp = netdev_priv(dev);
239 struct vnet_port *port = __tx_port_find(vp, skb);
240
241 if (!port)
242 return 0;
243
244 return port->q_index;
245}
246
247/* Wrappers to common functions */
248static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
249{
250 return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find);
251}
252
253static void vnet_set_rx_mode(struct net_device *dev)
254{
255 struct vnet *vp = netdev_priv(dev);
256
257 return sunvnet_set_rx_mode_common(dev, vp);
258}
259
260#ifdef CONFIG_NET_POLL_CONTROLLER
261static void vnet_poll_controller(struct net_device *dev)
262{
263 struct vnet *vp = netdev_priv(dev);
264
265 return sunvnet_poll_controller_common(dev, vp);
266}
267#endif
268
David S. Miller8fd17f22009-03-20 00:51:22 -0700269static const struct net_device_ops vnet_ops = {
Aaron Young31762ea2016-03-15 11:35:37 -0700270 .ndo_open = sunvnet_open_common,
271 .ndo_stop = sunvnet_close_common,
Aaron Young67d07192016-03-15 11:35:38 -0700272 .ndo_set_rx_mode = vnet_set_rx_mode,
Aaron Young31762ea2016-03-15 11:35:37 -0700273 .ndo_set_mac_address = sunvnet_set_mac_addr_common,
Ben Hutchings240c1022009-07-09 17:54:35 +0000274 .ndo_validate_addr = eth_validate_addr,
Aaron Young31762ea2016-03-15 11:35:37 -0700275 .ndo_tx_timeout = sunvnet_tx_timeout_common,
Aaron Young67d07192016-03-15 11:35:38 -0700276 .ndo_start_xmit = vnet_start_xmit,
277 .ndo_select_queue = vnet_select_queue,
Sowmini Varadhan69088822014-10-25 15:12:12 -0400278#ifdef CONFIG_NET_POLL_CONTROLLER
Aaron Young67d07192016-03-15 11:35:38 -0700279 .ndo_poll_controller = vnet_poll_controller,
Sowmini Varadhan69088822014-10-25 15:12:12 -0400280#endif
David S. Miller8fd17f22009-03-20 00:51:22 -0700281};
282
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400283static struct vnet *vnet_new(const u64 *local_mac,
284 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700285{
286 struct net_device *dev;
287 struct vnet *vp;
288 int err, i;
289
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400290 dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
Joe Perches41de8d42012-01-29 13:47:52 +0000291 if (!dev)
David S. Miller9184a042007-07-17 22:19:10 -0700292 return ERR_PTR(-ENOMEM);
David L Stevens8e845f4c2014-09-29 19:48:11 -0400293 dev->needed_headroom = VNET_PACKET_SKIP + 8;
294 dev->needed_tailroom = 8;
David S. Miller9184a042007-07-17 22:19:10 -0700295
296 for (i = 0; i < ETH_ALEN; i++)
297 dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
298
David S. Miller9184a042007-07-17 22:19:10 -0700299 vp = netdev_priv(dev);
300
301 spin_lock_init(&vp->lock);
302 vp->dev = dev;
303
304 INIT_LIST_HEAD(&vp->port_list);
305 for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
306 INIT_HLIST_HEAD(&vp->port_hash[i]);
307 INIT_LIST_HEAD(&vp->list);
308 vp->local_mac = *local_mac;
309
David S. Miller8fd17f22009-03-20 00:51:22 -0700310 dev->netdev_ops = &vnet_ops;
David S. Miller9184a042007-07-17 22:19:10 -0700311 dev->ethtool_ops = &vnet_ethtool_ops;
312 dev->watchdog_timeo = VNET_TX_TIMEOUT;
David S. Miller9184a042007-07-17 22:19:10 -0700313
David L Stevens368e36e2014-12-02 15:31:38 -0500314 dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_GSO_SOFTWARE |
Shannon Nelson7e9191c2017-06-14 15:43:37 -0700315 NETIF_F_IP_CSUM | NETIF_F_SG;
David L Stevensda38c562014-12-02 15:31:13 -0500316 dev->features = dev->hw_features;
317
Jarod Wilson540bfe32016-10-17 15:54:10 -0400318 /* MTU range: 68 - 65535 */
319 dev->min_mtu = ETH_MIN_MTU;
320 dev->max_mtu = VNET_MAX_MTU;
321
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400322 SET_NETDEV_DEV(dev, &vdev->dev);
323
David S. Miller9184a042007-07-17 22:19:10 -0700324 err = register_netdev(dev);
325 if (err) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000326 pr_err("Cannot register net device, aborting\n");
David S. Miller9184a042007-07-17 22:19:10 -0700327 goto err_out_free_dev;
328 }
329
Joe Perches4d5870e2010-08-17 07:55:05 +0000330 netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
David S. Miller9184a042007-07-17 22:19:10 -0700331
332 list_add(&vp->list, &vnet_list);
333
334 return vp;
335
336err_out_free_dev:
337 free_netdev(dev);
338
339 return ERR_PTR(err);
340}
341
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400342static struct vnet *vnet_find_or_create(const u64 *local_mac,
343 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700344{
345 struct vnet *iter, *vp;
346
347 mutex_lock(&vnet_list_mutex);
348 vp = NULL;
349 list_for_each_entry(iter, &vnet_list, list) {
350 if (iter->local_mac == *local_mac) {
351 vp = iter;
352 break;
353 }
354 }
355 if (!vp)
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400356 vp = vnet_new(local_mac, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700357 mutex_unlock(&vnet_list_mutex);
358
359 return vp;
360}
361
Sowmini Varadhana4b70a02014-07-16 10:02:26 -0400362static void vnet_cleanup(void)
363{
364 struct vnet *vp;
365 struct net_device *dev;
366
367 mutex_lock(&vnet_list_mutex);
368 while (!list_empty(&vnet_list)) {
369 vp = list_first_entry(&vnet_list, struct vnet, list);
370 list_del(&vp->list);
371 dev = vp->dev;
372 /* vio_unregister_driver() should have cleaned up port_list */
373 BUG_ON(!list_empty(&vp->port_list));
374 unregister_netdev(dev);
375 free_netdev(dev);
376 }
377 mutex_unlock(&vnet_list_mutex);
378}
379
David S. Miller9184a042007-07-17 22:19:10 -0700380static const char *local_mac_prop = "local-mac-address";
381
Bill Pembertonf73d12b2012-12-03 09:24:02 -0500382static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400383 u64 port_node,
384 struct vio_dev *vdev)
David S. Miller9184a042007-07-17 22:19:10 -0700385{
386 const u64 *local_mac = NULL;
387 u64 a;
388
389 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
390 u64 target = mdesc_arc_target(hp, a);
391 const char *name;
392
393 name = mdesc_get_property(hp, target, "name", NULL);
394 if (!name || strcmp(name, "network"))
395 continue;
396
397 local_mac = mdesc_get_property(hp, target,
398 local_mac_prop, NULL);
399 if (local_mac)
400 break;
401 }
402 if (!local_mac)
403 return ERR_PTR(-ENODEV);
404
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400405 return vnet_find_or_create(local_mac, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700406}
407
David S. Miller4c521e42007-07-09 22:23:51 -0700408static struct ldc_channel_config vnet_ldc_cfg = {
Aaron Young31762ea2016-03-15 11:35:37 -0700409 .event = sunvnet_event_common,
David S. Miller4c521e42007-07-09 22:23:51 -0700410 .mtu = 64,
411 .mode = LDC_MODE_UNRELIABLE,
412};
413
414static struct vio_driver_ops vnet_vio_ops = {
Aaron Young31762ea2016-03-15 11:35:37 -0700415 .send_attr = sunvnet_send_attr_common,
416 .handle_attr = sunvnet_handle_attr_common,
417 .handshake_complete = sunvnet_handshake_complete_common,
David S. Miller4c521e42007-07-09 22:23:51 -0700418};
419
420const char *remote_macaddr_prop = "remote-mac-address";
421
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000422static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller4c521e42007-07-09 22:23:51 -0700423{
David S. Miller43fdf272007-07-12 13:47:50 -0700424 struct mdesc_handle *hp;
David S. Miller4c521e42007-07-09 22:23:51 -0700425 struct vnet_port *port;
426 unsigned long flags;
427 struct vnet *vp;
428 const u64 *rmac;
429 int len, i, err, switch_port;
430
David S. Miller43fdf272007-07-12 13:47:50 -0700431 hp = mdesc_grab();
432
Sowmini Varadhan4c5d2832015-09-18 17:47:55 -0400433 vp = vnet_find_parent(hp, vdev->mp, vdev);
David S. Miller9184a042007-07-17 22:19:10 -0700434 if (IS_ERR(vp)) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000435 pr_err("Cannot find port parent vnet\n");
David S. Miller9184a042007-07-17 22:19:10 -0700436 err = PTR_ERR(vp);
437 goto err_out_put_mdesc;
438 }
439
David S. Miller43fdf272007-07-12 13:47:50 -0700440 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
441 err = -ENODEV;
David S. Miller4c521e42007-07-09 22:23:51 -0700442 if (!rmac) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000443 pr_err("Port lacks %s property\n", remote_macaddr_prop);
David S. Miller43fdf272007-07-12 13:47:50 -0700444 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -0700445 }
446
447 port = kzalloc(sizeof(*port), GFP_KERNEL);
David S. Miller43fdf272007-07-12 13:47:50 -0700448 err = -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +0000449 if (!port)
David S. Miller43fdf272007-07-12 13:47:50 -0700450 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -0700451
452 for (i = 0; i < ETH_ALEN; i++)
453 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
454
455 port->vp = vp;
456
David S. Miller43fdf272007-07-12 13:47:50 -0700457 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
David S. Miller4c521e42007-07-09 22:23:51 -0700458 vnet_versions, ARRAY_SIZE(vnet_versions),
459 &vnet_vio_ops, vp->dev->name);
460 if (err)
461 goto err_out_free_port;
462
463 err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
464 if (err)
465 goto err_out_free_port;
466
Aaron Young31762ea2016-03-15 11:35:37 -0700467 netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common,
468 NAPI_POLL_WEIGHT);
Sowmini Varadhan69088822014-10-25 15:12:12 -0400469
David S. Miller4c521e42007-07-09 22:23:51 -0700470 INIT_HLIST_NODE(&port->hash);
471 INIT_LIST_HEAD(&port->list);
472
473 switch_port = 0;
Aaron Youngdc153f82016-03-15 11:35:40 -0700474 if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL))
David S. Miller4c521e42007-07-09 22:23:51 -0700475 switch_port = 1;
David S. Miller028ebff2007-07-20 02:30:25 -0700476 port->switch_port = switch_port;
David L Stevens368e36e2014-12-02 15:31:38 -0500477 port->tso = true;
478 port->tsolen = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700479
480 spin_lock_irqsave(&vp->lock, flags);
481 if (switch_port)
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400482 list_add_rcu(&port->list, &vp->port_list);
David S. Miller4c521e42007-07-09 22:23:51 -0700483 else
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400484 list_add_tail_rcu(&port->list, &vp->port_list);
485 hlist_add_head_rcu(&port->hash,
486 &vp->port_hash[vnet_hashfn(port->raddr)]);
Aaron Young31762ea2016-03-15 11:35:37 -0700487 sunvnet_port_add_txq_common(port);
David S. Miller4c521e42007-07-09 22:23:51 -0700488 spin_unlock_irqrestore(&vp->lock, flags);
489
490 dev_set_drvdata(&vdev->dev, port);
491
Joe Perches4d5870e2010-08-17 07:55:05 +0000492 pr_info("%s: PORT ( remote-mac %pM%s )\n",
493 vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
David S. Miller4c521e42007-07-09 22:23:51 -0700494
Aaron Young31762ea2016-03-15 11:35:37 -0700495 setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common,
David L Stevens8e845f4c2014-09-29 19:48:11 -0400496 (unsigned long)port);
497
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);