David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1 | /* sunvnet.c: Sun LDOM Virtual Network Driver. |
| 2 | * |
David S. Miller | 3d452e5 | 2008-09-01 01:48:52 -0700 | [diff] [blame] | 3 | * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> |
Aaron Young | 67d0719 | 2016-03-15 11:35:38 -0700 | [diff] [blame] | 4 | * Copyright (C) 2016 Oracle. All rights reserved. |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 8 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 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> |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 18 | #include <linux/mutex.h> |
David L Stevens | da38c56 | 2014-12-02 15:31:13 -0500 | [diff] [blame] | 19 | #include <linux/highmem.h> |
David L Stevens | e4defc7 | 2014-09-29 19:47:59 -0400 | [diff] [blame] | 20 | #include <linux/if_vlan.h> |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 21 | |
David L Stevens | a2b78e9 | 2014-09-29 19:48:24 -0400 | [diff] [blame] | 22 | #if IS_ENABLED(CONFIG_IPV6) |
| 23 | #include <linux/icmpv6.h> |
| 24 | #endif |
| 25 | |
David L Stevens | 6d0ba91 | 2014-12-02 15:31:04 -0500 | [diff] [blame] | 26 | #include <net/ip.h> |
David L Stevens | a2b78e9 | 2014-09-29 19:48:24 -0400 | [diff] [blame] | 27 | #include <net/icmp.h> |
| 28 | #include <net/route.h> |
| 29 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 30 | #include <asm/vio.h> |
| 31 | #include <asm/ldc.h> |
| 32 | |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 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) |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 39 | |
| 40 | #define DRV_MODULE_NAME "sunvnet" |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 41 | #define DRV_MODULE_VERSION "1.0" |
| 42 | #define DRV_MODULE_RELDATE "June 25, 2007" |
| 43 | |
Bill Pemberton | f73d12b | 2012-12-03 09:24:02 -0500 | [diff] [blame] | 44 | static char version[] = |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 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[] = { |
David L Stevens | 6d0ba91 | 2014-12-02 15:31:04 -0500 | [diff] [blame] | 53 | { .major = 1, .minor = 8 }, |
| 54 | { .major = 1, .minor = 7 }, |
David L Stevens | e4defc7 | 2014-09-29 19:47:59 -0400 | [diff] [blame] | 55 | { .major = 1, .minor = 6 }, |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 56 | { .major = 1, .minor = 0 }, |
| 57 | }; |
| 58 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 59 | static void vnet_get_drvinfo(struct net_device *dev, |
| 60 | struct ethtool_drvinfo *info) |
| 61 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 62 | strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); |
| 63 | strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version)); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static u32 vnet_get_msglevel(struct net_device *dev) |
| 67 | { |
| 68 | struct vnet *vp = netdev_priv(dev); |
Aaron Young | dc153f8 | 2016-03-15 11:35:40 -0700 | [diff] [blame] | 69 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 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); |
Aaron Young | dc153f8 | 2016-03-15 11:35:40 -0700 | [diff] [blame] | 76 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 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, |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 87 | static LIST_HEAD(vnet_list); |
| 88 | static DEFINE_MUTEX(vnet_list_mutex); |
| 89 | |
Aaron Young | 67d0719 | 2016-03-15 11:35:38 -0700 | [diff] [blame] | 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 | |
David S. Miller | 8fd17f2 | 2009-03-20 00:51:22 -0700 | [diff] [blame] | 155 | static const struct net_device_ops vnet_ops = { |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 156 | .ndo_open = sunvnet_open_common, |
| 157 | .ndo_stop = sunvnet_close_common, |
Aaron Young | 67d0719 | 2016-03-15 11:35:38 -0700 | [diff] [blame] | 158 | .ndo_set_rx_mode = vnet_set_rx_mode, |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 159 | .ndo_set_mac_address = sunvnet_set_mac_addr_common, |
Ben Hutchings | 240c102 | 2009-07-09 17:54:35 +0000 | [diff] [blame] | 160 | .ndo_validate_addr = eth_validate_addr, |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 161 | .ndo_tx_timeout = sunvnet_tx_timeout_common, |
| 162 | .ndo_change_mtu = sunvnet_change_mtu_common, |
Aaron Young | 67d0719 | 2016-03-15 11:35:38 -0700 | [diff] [blame] | 163 | .ndo_start_xmit = vnet_start_xmit, |
| 164 | .ndo_select_queue = vnet_select_queue, |
Sowmini Varadhan | 6908882 | 2014-10-25 15:12:12 -0400 | [diff] [blame] | 165 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Aaron Young | 67d0719 | 2016-03-15 11:35:38 -0700 | [diff] [blame] | 166 | .ndo_poll_controller = vnet_poll_controller, |
Sowmini Varadhan | 6908882 | 2014-10-25 15:12:12 -0400 | [diff] [blame] | 167 | #endif |
David S. Miller | 8fd17f2 | 2009-03-20 00:51:22 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Sowmini Varadhan | 4c5d283 | 2015-09-18 17:47:55 -0400 | [diff] [blame] | 170 | static struct vnet *vnet_new(const u64 *local_mac, |
| 171 | struct vio_dev *vdev) |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 172 | { |
| 173 | struct net_device *dev; |
| 174 | struct vnet *vp; |
| 175 | int err, i; |
| 176 | |
Sowmini Varadhan | d51bffd | 2014-10-30 12:46:09 -0400 | [diff] [blame] | 177 | dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1); |
Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 178 | if (!dev) |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 179 | return ERR_PTR(-ENOMEM); |
David L Stevens | 8e845f4c | 2014-09-29 19:48:11 -0400 | [diff] [blame] | 180 | dev->needed_headroom = VNET_PACKET_SKIP + 8; |
| 181 | dev->needed_tailroom = 8; |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 182 | |
| 183 | for (i = 0; i < ETH_ALEN; i++) |
| 184 | dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff; |
| 185 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 186 | vp = netdev_priv(dev); |
| 187 | |
| 188 | spin_lock_init(&vp->lock); |
| 189 | vp->dev = dev; |
| 190 | |
| 191 | INIT_LIST_HEAD(&vp->port_list); |
| 192 | for (i = 0; i < VNET_PORT_HASH_SIZE; i++) |
| 193 | INIT_HLIST_HEAD(&vp->port_hash[i]); |
| 194 | INIT_LIST_HEAD(&vp->list); |
| 195 | vp->local_mac = *local_mac; |
| 196 | |
David S. Miller | 8fd17f2 | 2009-03-20 00:51:22 -0700 | [diff] [blame] | 197 | dev->netdev_ops = &vnet_ops; |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 198 | dev->ethtool_ops = &vnet_ethtool_ops; |
| 199 | dev->watchdog_timeo = VNET_TX_TIMEOUT; |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 200 | |
David L Stevens | 368e36e | 2014-12-02 15:31:38 -0500 | [diff] [blame] | 201 | dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_GSO_SOFTWARE | |
David L Stevens | 9a72dd4 | 2014-12-02 15:31:30 -0500 | [diff] [blame] | 202 | NETIF_F_HW_CSUM | NETIF_F_SG; |
David L Stevens | da38c56 | 2014-12-02 15:31:13 -0500 | [diff] [blame] | 203 | dev->features = dev->hw_features; |
| 204 | |
Sowmini Varadhan | 4c5d283 | 2015-09-18 17:47:55 -0400 | [diff] [blame] | 205 | SET_NETDEV_DEV(dev, &vdev->dev); |
| 206 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 207 | err = register_netdev(dev); |
| 208 | if (err) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 209 | pr_err("Cannot register net device, aborting\n"); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 210 | goto err_out_free_dev; |
| 211 | } |
| 212 | |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 213 | netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 214 | |
| 215 | list_add(&vp->list, &vnet_list); |
| 216 | |
| 217 | return vp; |
| 218 | |
| 219 | err_out_free_dev: |
| 220 | free_netdev(dev); |
| 221 | |
| 222 | return ERR_PTR(err); |
| 223 | } |
| 224 | |
Sowmini Varadhan | 4c5d283 | 2015-09-18 17:47:55 -0400 | [diff] [blame] | 225 | static struct vnet *vnet_find_or_create(const u64 *local_mac, |
| 226 | struct vio_dev *vdev) |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 227 | { |
| 228 | struct vnet *iter, *vp; |
| 229 | |
| 230 | mutex_lock(&vnet_list_mutex); |
| 231 | vp = NULL; |
| 232 | list_for_each_entry(iter, &vnet_list, list) { |
| 233 | if (iter->local_mac == *local_mac) { |
| 234 | vp = iter; |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | if (!vp) |
Sowmini Varadhan | 4c5d283 | 2015-09-18 17:47:55 -0400 | [diff] [blame] | 239 | vp = vnet_new(local_mac, vdev); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 240 | mutex_unlock(&vnet_list_mutex); |
| 241 | |
| 242 | return vp; |
| 243 | } |
| 244 | |
Sowmini Varadhan | a4b70a0 | 2014-07-16 10:02:26 -0400 | [diff] [blame] | 245 | static void vnet_cleanup(void) |
| 246 | { |
| 247 | struct vnet *vp; |
| 248 | struct net_device *dev; |
| 249 | |
| 250 | mutex_lock(&vnet_list_mutex); |
| 251 | while (!list_empty(&vnet_list)) { |
| 252 | vp = list_first_entry(&vnet_list, struct vnet, list); |
| 253 | list_del(&vp->list); |
| 254 | dev = vp->dev; |
| 255 | /* vio_unregister_driver() should have cleaned up port_list */ |
| 256 | BUG_ON(!list_empty(&vp->port_list)); |
| 257 | unregister_netdev(dev); |
| 258 | free_netdev(dev); |
| 259 | } |
| 260 | mutex_unlock(&vnet_list_mutex); |
| 261 | } |
| 262 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 263 | static const char *local_mac_prop = "local-mac-address"; |
| 264 | |
Bill Pemberton | f73d12b | 2012-12-03 09:24:02 -0500 | [diff] [blame] | 265 | static struct vnet *vnet_find_parent(struct mdesc_handle *hp, |
Sowmini Varadhan | 4c5d283 | 2015-09-18 17:47:55 -0400 | [diff] [blame] | 266 | u64 port_node, |
| 267 | struct vio_dev *vdev) |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 268 | { |
| 269 | const u64 *local_mac = NULL; |
| 270 | u64 a; |
| 271 | |
| 272 | mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) { |
| 273 | u64 target = mdesc_arc_target(hp, a); |
| 274 | const char *name; |
| 275 | |
| 276 | name = mdesc_get_property(hp, target, "name", NULL); |
| 277 | if (!name || strcmp(name, "network")) |
| 278 | continue; |
| 279 | |
| 280 | local_mac = mdesc_get_property(hp, target, |
| 281 | local_mac_prop, NULL); |
| 282 | if (local_mac) |
| 283 | break; |
| 284 | } |
| 285 | if (!local_mac) |
| 286 | return ERR_PTR(-ENODEV); |
| 287 | |
Sowmini Varadhan | 4c5d283 | 2015-09-18 17:47:55 -0400 | [diff] [blame] | 288 | return vnet_find_or_create(local_mac, vdev); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 289 | } |
| 290 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 291 | static struct ldc_channel_config vnet_ldc_cfg = { |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 292 | .event = sunvnet_event_common, |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 293 | .mtu = 64, |
| 294 | .mode = LDC_MODE_UNRELIABLE, |
| 295 | }; |
| 296 | |
| 297 | static struct vio_driver_ops vnet_vio_ops = { |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 298 | .send_attr = sunvnet_send_attr_common, |
| 299 | .handle_attr = sunvnet_handle_attr_common, |
| 300 | .handshake_complete = sunvnet_handshake_complete_common, |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 301 | }; |
| 302 | |
Bill Pemberton | f73d12b | 2012-12-03 09:24:02 -0500 | [diff] [blame] | 303 | static void print_version(void) |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 304 | { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 305 | printk_once(KERN_INFO "%s", version); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 306 | } |
| 307 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 308 | const char *remote_macaddr_prop = "remote-mac-address"; |
| 309 | |
Greg Kroah-Hartman | 1dd06ae | 2012-12-06 14:30:56 +0000 | [diff] [blame] | 310 | static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id) |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 311 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 312 | struct mdesc_handle *hp; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 313 | struct vnet_port *port; |
| 314 | unsigned long flags; |
| 315 | struct vnet *vp; |
| 316 | const u64 *rmac; |
| 317 | int len, i, err, switch_port; |
| 318 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 319 | print_version(); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 320 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 321 | hp = mdesc_grab(); |
| 322 | |
Sowmini Varadhan | 4c5d283 | 2015-09-18 17:47:55 -0400 | [diff] [blame] | 323 | vp = vnet_find_parent(hp, vdev->mp, vdev); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 324 | if (IS_ERR(vp)) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 325 | pr_err("Cannot find port parent vnet\n"); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 326 | err = PTR_ERR(vp); |
| 327 | goto err_out_put_mdesc; |
| 328 | } |
| 329 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 330 | rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len); |
| 331 | err = -ENODEV; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 332 | if (!rmac) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 333 | pr_err("Port lacks %s property\n", remote_macaddr_prop); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 334 | goto err_out_put_mdesc; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 338 | err = -ENOMEM; |
Joe Perches | e404dec | 2012-01-29 12:56:23 +0000 | [diff] [blame] | 339 | if (!port) |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 340 | goto err_out_put_mdesc; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 341 | |
| 342 | for (i = 0; i < ETH_ALEN; i++) |
| 343 | port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff; |
| 344 | |
| 345 | port->vp = vp; |
| 346 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 347 | err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK, |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 348 | vnet_versions, ARRAY_SIZE(vnet_versions), |
| 349 | &vnet_vio_ops, vp->dev->name); |
| 350 | if (err) |
| 351 | goto err_out_free_port; |
| 352 | |
| 353 | err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port); |
| 354 | if (err) |
| 355 | goto err_out_free_port; |
| 356 | |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 357 | netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common, |
| 358 | NAPI_POLL_WEIGHT); |
Sowmini Varadhan | 6908882 | 2014-10-25 15:12:12 -0400 | [diff] [blame] | 359 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 360 | INIT_HLIST_NODE(&port->hash); |
| 361 | INIT_LIST_HEAD(&port->list); |
| 362 | |
| 363 | switch_port = 0; |
Aaron Young | dc153f8 | 2016-03-15 11:35:40 -0700 | [diff] [blame] | 364 | if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL)) |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 365 | switch_port = 1; |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 366 | port->switch_port = switch_port; |
David L Stevens | 368e36e | 2014-12-02 15:31:38 -0500 | [diff] [blame] | 367 | port->tso = true; |
| 368 | port->tsolen = 0; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 369 | |
| 370 | spin_lock_irqsave(&vp->lock, flags); |
| 371 | if (switch_port) |
Sowmini Varadhan | 2a968dd | 2014-10-25 15:12:20 -0400 | [diff] [blame] | 372 | list_add_rcu(&port->list, &vp->port_list); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 373 | else |
Sowmini Varadhan | 2a968dd | 2014-10-25 15:12:20 -0400 | [diff] [blame] | 374 | list_add_tail_rcu(&port->list, &vp->port_list); |
| 375 | hlist_add_head_rcu(&port->hash, |
| 376 | &vp->port_hash[vnet_hashfn(port->raddr)]); |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 377 | sunvnet_port_add_txq_common(port); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 378 | spin_unlock_irqrestore(&vp->lock, flags); |
| 379 | |
| 380 | dev_set_drvdata(&vdev->dev, port); |
| 381 | |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 382 | pr_info("%s: PORT ( remote-mac %pM%s )\n", |
| 383 | vp->dev->name, port->raddr, switch_port ? " switch-port" : ""); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 384 | |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 385 | setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common, |
David L Stevens | 8e845f4c | 2014-09-29 19:48:11 -0400 | [diff] [blame] | 386 | (unsigned long)port); |
| 387 | |
Sowmini Varadhan | 6908882 | 2014-10-25 15:12:12 -0400 | [diff] [blame] | 388 | napi_enable(&port->napi); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 389 | vio_port_up(&port->vio); |
| 390 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 391 | mdesc_release(hp); |
| 392 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 393 | return 0; |
| 394 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 395 | err_out_free_port: |
| 396 | kfree(port); |
| 397 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 398 | err_out_put_mdesc: |
| 399 | mdesc_release(hp); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 400 | return err; |
| 401 | } |
| 402 | |
| 403 | static int vnet_port_remove(struct vio_dev *vdev) |
| 404 | { |
| 405 | struct vnet_port *port = dev_get_drvdata(&vdev->dev); |
| 406 | |
| 407 | if (port) { |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 408 | del_timer_sync(&port->vio.timer); |
| 409 | |
Sowmini Varadhan | 6908882 | 2014-10-25 15:12:12 -0400 | [diff] [blame] | 410 | napi_disable(&port->napi); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 411 | |
Sowmini Varadhan | 2a968dd | 2014-10-25 15:12:20 -0400 | [diff] [blame] | 412 | list_del_rcu(&port->list); |
| 413 | hlist_del_rcu(&port->hash); |
| 414 | |
| 415 | synchronize_rcu(); |
| 416 | del_timer_sync(&port->clean_timer); |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 417 | sunvnet_port_rm_txq_common(port); |
Sowmini Varadhan | 6908882 | 2014-10-25 15:12:12 -0400 | [diff] [blame] | 418 | netif_napi_del(&port->napi); |
Aaron Young | 31762ea | 2016-03-15 11:35:37 -0700 | [diff] [blame] | 419 | sunvnet_port_free_tx_bufs_common(port); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 420 | vio_ldc_free(&port->vio); |
| 421 | |
| 422 | dev_set_drvdata(&vdev->dev, NULL); |
| 423 | |
| 424 | kfree(port); |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
David S. Miller | 3d452e5 | 2008-09-01 01:48:52 -0700 | [diff] [blame] | 429 | static const struct vio_device_id vnet_port_match[] = { |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 430 | { |
| 431 | .type = "vnet-port", |
| 432 | }, |
| 433 | {}, |
| 434 | }; |
Fabio Massimo Di Nitto | da68e08 | 2007-07-18 14:35:23 -0700 | [diff] [blame] | 435 | MODULE_DEVICE_TABLE(vio, vnet_port_match); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 436 | |
| 437 | static struct vio_driver vnet_port_driver = { |
| 438 | .id_table = vnet_port_match, |
| 439 | .probe = vnet_port_probe, |
| 440 | .remove = vnet_port_remove, |
Benjamin Herrenschmidt | cb52d89 | 2012-03-26 19:06:30 +0000 | [diff] [blame] | 441 | .name = "vnet_port", |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 444 | static int __init vnet_init(void) |
| 445 | { |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 446 | return vio_register_driver(&vnet_port_driver); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static void __exit vnet_exit(void) |
| 450 | { |
| 451 | vio_unregister_driver(&vnet_port_driver); |
Sowmini Varadhan | a4b70a0 | 2014-07-16 10:02:26 -0400 | [diff] [blame] | 452 | vnet_cleanup(); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | module_init(vnet_init); |
| 456 | module_exit(vnet_exit); |