Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009, Microsoft Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
Jeff Kirsher | adf8d3f | 2013-12-06 06:28:47 -0800 | [diff] [blame] | 14 | * this program; if not, see <http://www.gnu.org/licenses/>. |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 15 | * |
| 16 | * Authors: |
Haiyang Zhang | d0e94d1 | 2009-11-23 17:00:22 +0000 | [diff] [blame] | 17 | * Haiyang Zhang <haiyangz@microsoft.com> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 18 | * Hank Janssen <hjanssen@microsoft.com> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 19 | */ |
Hank Janssen | eb335bc | 2011-03-29 13:58:48 -0700 | [diff] [blame] | 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 21 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
K. Y. Srinivasan | 9079ce6 | 2011-06-16 13:16:37 -0700 | [diff] [blame] | 23 | #include <linux/atomic.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/device.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 27 | #include <linux/io.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 28 | #include <linux/delay.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/inetdevice.h> |
| 31 | #include <linux/etherdevice.h> |
| 32 | #include <linux/skbuff.h> |
Haiyang Zhang | c802db1 | 2013-05-28 06:15:56 +0000 | [diff] [blame] | 33 | #include <linux/if_vlan.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 34 | #include <linux/in.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 36 | #include <net/arp.h> |
| 37 | #include <net/route.h> |
| 38 | #include <net/sock.h> |
| 39 | #include <net/pkt_sched.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 40 | |
K. Y. Srinivasan | 5ca7252 | 2011-05-12 19:34:37 -0700 | [diff] [blame] | 41 | #include "hyperv_net.h" |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 42 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 43 | |
Haiyang Zhang | fa85a6c | 2012-07-25 08:08:41 +0000 | [diff] [blame] | 44 | #define RING_SIZE_MIN 64 |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 45 | #define LINKCHANGE_INT (2 * HZ) |
sixiao@microsoft.com | a060679 | 2016-02-04 15:49:34 -0800 | [diff] [blame] | 46 | #define NETVSC_HW_FEATURES (NETIF_F_RXCSUM | \ |
| 47 | NETIF_F_SG | \ |
| 48 | NETIF_F_TSO | \ |
| 49 | NETIF_F_TSO6 | \ |
| 50 | NETIF_F_HW_CSUM) |
Hank Janssen | 99c8da0 | 2010-10-12 10:45:23 -0700 | [diff] [blame] | 51 | static int ring_size = 128; |
Stephen Hemminger | 450d7a4 | 2010-05-04 09:58:53 -0700 | [diff] [blame] | 52 | module_param(ring_size, int, S_IRUGO); |
| 53 | MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 54 | |
KY Srinivasan | e01ec21 | 2015-05-27 13:16:57 -0700 | [diff] [blame] | 55 | static int max_num_vrss_chns = 8; |
| 56 | |
Simon Xiao | 3f300ff | 2015-04-28 01:05:17 -0700 | [diff] [blame] | 57 | static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | |
| 58 | NETIF_MSG_LINK | NETIF_MSG_IFUP | |
| 59 | NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | |
| 60 | NETIF_MSG_TX_ERR; |
| 61 | |
| 62 | static int debug = -1; |
| 63 | module_param(debug, int, S_IRUGO); |
| 64 | MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); |
| 65 | |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 66 | static void do_set_multicast(struct work_struct *w) |
| 67 | { |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 68 | struct net_device_context *ndevctx = |
| 69 | container_of(w, struct net_device_context, work); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 70 | struct hv_device *device_obj = ndevctx->device_ctx; |
| 71 | struct net_device *ndev = hv_get_drvdata(device_obj); |
| 72 | struct netvsc_device *nvdev = ndevctx->nvdev; |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 73 | struct rndis_device *rdev; |
| 74 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 75 | if (!nvdev) |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 76 | return; |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 77 | |
| 78 | rdev = nvdev->extension; |
| 79 | if (rdev == NULL) |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 80 | return; |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 81 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 82 | if (ndev->flags & IFF_PROMISC) |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 83 | rndis_filter_set_packet_filter(rdev, |
| 84 | NDIS_PACKET_TYPE_PROMISCUOUS); |
| 85 | else |
| 86 | rndis_filter_set_packet_filter(rdev, |
| 87 | NDIS_PACKET_TYPE_BROADCAST | |
| 88 | NDIS_PACKET_TYPE_ALL_MULTICAST | |
| 89 | NDIS_PACKET_TYPE_DIRECTED); |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Greg Kroah-Hartman | 4e9bfef | 2009-07-15 12:45:20 -0700 | [diff] [blame] | 92 | static void netvsc_set_multicast_list(struct net_device *net) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 93 | { |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 94 | struct net_device_context *net_device_ctx = netdev_priv(net); |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 95 | |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 96 | schedule_work(&net_device_ctx->work); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 99 | static int netvsc_open(struct net_device *net) |
| 100 | { |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 101 | struct netvsc_device *nvdev = net_device_to_netvsc_device(net); |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 102 | struct rndis_device *rdev; |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 103 | int ret = 0; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 104 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 105 | netif_carrier_off(net); |
| 106 | |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 107 | /* Open up the device */ |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 108 | ret = rndis_filter_open(nvdev); |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 109 | if (ret != 0) { |
| 110 | netdev_err(net, "unable to open device (ret %d).\n", ret); |
| 111 | return ret; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 114 | netif_tx_wake_all_queues(net); |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 115 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 116 | rdev = nvdev->extension; |
| 117 | if (!rdev->link_state) |
| 118 | netif_carrier_on(net); |
| 119 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 120 | return ret; |
| 121 | } |
| 122 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 123 | static int netvsc_close(struct net_device *net) |
| 124 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 125 | struct net_device_context *net_device_ctx = netdev_priv(net); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 126 | struct netvsc_device *nvdev = net_device_ctx->nvdev; |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 127 | int ret; |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 128 | u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20; |
| 129 | struct vmbus_channel *chn; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 130 | |
Haiyang Zhang | 0a28253 | 2012-02-02 07:17:59 +0000 | [diff] [blame] | 131 | netif_tx_disable(net); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 132 | |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 133 | /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */ |
| 134 | cancel_work_sync(&net_device_ctx->work); |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 135 | ret = rndis_filter_close(nvdev); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 136 | if (ret != 0) { |
Hank Janssen | eb335bc | 2011-03-29 13:58:48 -0700 | [diff] [blame] | 137 | netdev_err(net, "unable to close device (ret %d).\n", ret); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | /* Ensure pending bytes in ring are read */ |
| 142 | while (true) { |
| 143 | aread = 0; |
| 144 | for (i = 0; i < nvdev->num_chn; i++) { |
| 145 | chn = nvdev->chn_table[i]; |
| 146 | if (!chn) |
| 147 | continue; |
| 148 | |
| 149 | hv_get_ringbuffer_availbytes(&chn->inbound, &aread, |
| 150 | &awrite); |
| 151 | |
| 152 | if (aread) |
| 153 | break; |
| 154 | |
| 155 | hv_get_ringbuffer_availbytes(&chn->outbound, &aread, |
| 156 | &awrite); |
| 157 | |
| 158 | if (aread) |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | retry++; |
| 163 | if (retry > retry_max || aread == 0) |
| 164 | break; |
| 165 | |
| 166 | msleep(msec); |
| 167 | |
| 168 | if (msec < 1000) |
| 169 | msec *= 2; |
| 170 | } |
| 171 | |
| 172 | if (aread) { |
| 173 | netdev_err(net, "Ring buffer not empty after closing rndis\n"); |
| 174 | ret = -ETIMEDOUT; |
| 175 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 176 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 177 | return ret; |
| 178 | } |
| 179 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 180 | static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size, |
| 181 | int pkt_type) |
| 182 | { |
| 183 | struct rndis_packet *rndis_pkt; |
| 184 | struct rndis_per_packet_info *ppi; |
| 185 | |
| 186 | rndis_pkt = &msg->msg.pkt; |
| 187 | rndis_pkt->data_offset += ppi_size; |
| 188 | |
| 189 | ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt + |
| 190 | rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len); |
| 191 | |
| 192 | ppi->size = ppi_size; |
| 193 | ppi->type = pkt_type; |
| 194 | ppi->ppi_offset = sizeof(struct rndis_per_packet_info); |
| 195 | |
| 196 | rndis_pkt->per_pkt_info_len += ppi_size; |
| 197 | |
| 198 | return ppi; |
| 199 | } |
| 200 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 201 | static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb, |
| 202 | void *accel_priv, select_queue_fallback_t fallback) |
| 203 | { |
| 204 | struct net_device_context *net_device_ctx = netdev_priv(ndev); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 205 | struct netvsc_device *nvsc_dev = net_device_ctx->nvdev; |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 206 | u32 hash; |
| 207 | u16 q_idx = 0; |
| 208 | |
| 209 | if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1) |
| 210 | return 0; |
| 211 | |
Vitaly Kuznetsov | 757647e | 2016-01-25 16:00:41 +0100 | [diff] [blame] | 212 | hash = skb_get_hash(skb); |
| 213 | q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] % |
| 214 | ndev->real_num_tx_queues; |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 215 | |
Vitaly Kuznetsov | 8b9fbe1 | 2015-12-01 16:43:11 -0800 | [diff] [blame] | 216 | if (!nvsc_dev->chn_table[q_idx]) |
| 217 | q_idx = 0; |
| 218 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 219 | return q_idx; |
| 220 | } |
| 221 | |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 222 | static u32 fill_pg_buf(struct page *page, u32 offset, u32 len, |
| 223 | struct hv_page_buffer *pb) |
| 224 | { |
| 225 | int j = 0; |
| 226 | |
| 227 | /* Deal with compund pages by ignoring unused part |
| 228 | * of the page. |
| 229 | */ |
| 230 | page += (offset >> PAGE_SHIFT); |
| 231 | offset &= ~PAGE_MASK; |
| 232 | |
| 233 | while (len > 0) { |
| 234 | unsigned long bytes; |
| 235 | |
| 236 | bytes = PAGE_SIZE - offset; |
| 237 | if (bytes > len) |
| 238 | bytes = len; |
| 239 | pb[j].pfn = page_to_pfn(page); |
| 240 | pb[j].offset = offset; |
| 241 | pb[j].len = bytes; |
| 242 | |
| 243 | offset += bytes; |
| 244 | len -= bytes; |
| 245 | |
| 246 | if (offset == PAGE_SIZE && len) { |
| 247 | page++; |
| 248 | offset = 0; |
| 249 | j++; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return j + 1; |
| 254 | } |
| 255 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 256 | static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb, |
KY Srinivasan | a9f2e2d | 2015-12-01 16:43:13 -0800 | [diff] [blame] | 257 | struct hv_netvsc_packet *packet, |
| 258 | struct hv_page_buffer **page_buf) |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 259 | { |
KY Srinivasan | a9f2e2d | 2015-12-01 16:43:13 -0800 | [diff] [blame] | 260 | struct hv_page_buffer *pb = *page_buf; |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 261 | u32 slots_used = 0; |
| 262 | char *data = skb->data; |
| 263 | int frags = skb_shinfo(skb)->nr_frags; |
| 264 | int i; |
| 265 | |
| 266 | /* The packet is laid out thus: |
Haiyang Zhang | aa0a34b | 2015-04-13 16:34:35 -0700 | [diff] [blame] | 267 | * 1. hdr: RNDIS header and PPI |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 268 | * 2. skb linear data |
| 269 | * 3. skb fragment data |
| 270 | */ |
| 271 | if (hdr != NULL) |
| 272 | slots_used += fill_pg_buf(virt_to_page(hdr), |
| 273 | offset_in_page(hdr), |
| 274 | len, &pb[slots_used]); |
| 275 | |
Haiyang Zhang | aa0a34b | 2015-04-13 16:34:35 -0700 | [diff] [blame] | 276 | packet->rmsg_size = len; |
| 277 | packet->rmsg_pgcnt = slots_used; |
| 278 | |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 279 | slots_used += fill_pg_buf(virt_to_page(data), |
| 280 | offset_in_page(data), |
| 281 | skb_headlen(skb), &pb[slots_used]); |
| 282 | |
| 283 | for (i = 0; i < frags; i++) { |
| 284 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; |
| 285 | |
| 286 | slots_used += fill_pg_buf(skb_frag_page(frag), |
| 287 | frag->page_offset, |
| 288 | skb_frag_size(frag), &pb[slots_used]); |
| 289 | } |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 290 | return slots_used; |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static int count_skb_frag_slots(struct sk_buff *skb) |
| 294 | { |
| 295 | int i, frags = skb_shinfo(skb)->nr_frags; |
| 296 | int pages = 0; |
| 297 | |
| 298 | for (i = 0; i < frags; i++) { |
| 299 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; |
| 300 | unsigned long size = skb_frag_size(frag); |
| 301 | unsigned long offset = frag->page_offset; |
| 302 | |
| 303 | /* Skip unused frames from start of page */ |
| 304 | offset &= ~PAGE_MASK; |
| 305 | pages += PFN_UP(offset + size); |
| 306 | } |
| 307 | return pages; |
| 308 | } |
| 309 | |
| 310 | static int netvsc_get_slots(struct sk_buff *skb) |
| 311 | { |
| 312 | char *data = skb->data; |
| 313 | unsigned int offset = offset_in_page(data); |
| 314 | unsigned int len = skb_headlen(skb); |
| 315 | int slots; |
| 316 | int frag_slots; |
| 317 | |
| 318 | slots = DIV_ROUND_UP(offset + len, PAGE_SIZE); |
| 319 | frag_slots = count_skb_frag_slots(skb); |
| 320 | return slots + frag_slots; |
| 321 | } |
| 322 | |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 323 | static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off) |
| 324 | { |
| 325 | u32 ret_val = TRANSPORT_INFO_NOT_IP; |
| 326 | |
| 327 | if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) && |
| 328 | (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) { |
| 329 | goto not_ip; |
| 330 | } |
| 331 | |
| 332 | *trans_off = skb_transport_offset(skb); |
| 333 | |
| 334 | if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) { |
| 335 | struct iphdr *iphdr = ip_hdr(skb); |
| 336 | |
| 337 | if (iphdr->protocol == IPPROTO_TCP) |
| 338 | ret_val = TRANSPORT_INFO_IPV4_TCP; |
| 339 | else if (iphdr->protocol == IPPROTO_UDP) |
| 340 | ret_val = TRANSPORT_INFO_IPV4_UDP; |
| 341 | } else { |
| 342 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) |
| 343 | ret_val = TRANSPORT_INFO_IPV6_TCP; |
| 344 | else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP) |
| 345 | ret_val = TRANSPORT_INFO_IPV6_UDP; |
| 346 | } |
| 347 | |
| 348 | not_ip: |
| 349 | return ret_val; |
| 350 | } |
| 351 | |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 352 | static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 353 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 354 | struct net_device_context *net_device_ctx = netdev_priv(net); |
Vitaly Kuznetsov | 981a1bd | 2015-04-08 17:54:05 +0200 | [diff] [blame] | 355 | struct hv_netvsc_packet *packet = NULL; |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 356 | int ret; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 357 | unsigned int num_data_pgs; |
| 358 | struct rndis_message *rndis_msg; |
| 359 | struct rndis_packet *rndis_pkt; |
| 360 | u32 rndis_msg_size; |
| 361 | bool isvlan; |
Vitaly Kuznetsov | e88f7e0 | 2015-04-08 17:54:06 +0200 | [diff] [blame] | 362 | bool linear = false; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 363 | struct rndis_per_packet_info *ppi; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 364 | struct ndis_tcp_ip_checksum_info *csum_info; |
KY Srinivasan | 77bf548 | 2014-03-08 19:23:18 -0800 | [diff] [blame] | 365 | struct ndis_tcp_lso_info *lso_info; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 366 | int hdr_offset; |
| 367 | u32 net_trans_info; |
Haiyang Zhang | 307f099 | 2014-05-21 12:55:39 -0700 | [diff] [blame] | 368 | u32 hash; |
Vitaly Kuznetsov | e88f7e0 | 2015-04-08 17:54:06 +0200 | [diff] [blame] | 369 | u32 skb_length; |
KY Srinivasan | b08cc79 | 2015-03-29 21:08:42 -0700 | [diff] [blame] | 370 | struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT]; |
KY Srinivasan | a9f2e2d | 2015-12-01 16:43:13 -0800 | [diff] [blame] | 371 | struct hv_page_buffer *pb = page_buf; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 372 | struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 373 | |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 374 | /* We will atmost need two pages to describe the rndis |
| 375 | * header. We can only transmit MAX_PAGE_BUFFER_COUNT number |
Vitaly Kuznetsov | e88f7e0 | 2015-04-08 17:54:06 +0200 | [diff] [blame] | 376 | * of pages in a single packet. If skb is scattered around |
| 377 | * more pages we try linearizing it. |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 378 | */ |
Vitaly Kuznetsov | e88f7e0 | 2015-04-08 17:54:06 +0200 | [diff] [blame] | 379 | |
| 380 | check_size: |
| 381 | skb_length = skb->len; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 382 | num_data_pgs = netvsc_get_slots(skb) + 2; |
Vitaly Kuznetsov | e88f7e0 | 2015-04-08 17:54:06 +0200 | [diff] [blame] | 383 | if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) { |
| 384 | net_alert_ratelimited("packet too big: %u pages (%u bytes)\n", |
| 385 | num_data_pgs, skb->len); |
Vitaly Kuznetsov | 981a1bd | 2015-04-08 17:54:05 +0200 | [diff] [blame] | 386 | ret = -EFAULT; |
| 387 | goto drop; |
Vitaly Kuznetsov | e88f7e0 | 2015-04-08 17:54:06 +0200 | [diff] [blame] | 388 | } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) { |
| 389 | if (skb_linearize(skb)) { |
| 390 | net_alert_ratelimited("failed to linearize skb\n"); |
| 391 | ret = -ENOMEM; |
| 392 | goto drop; |
| 393 | } |
| 394 | linear = true; |
| 395 | goto check_size; |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 396 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 397 | |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 398 | /* |
| 399 | * Place the rndis header in the skb head room and |
| 400 | * the skb->cb will be used for hv_netvsc_packet |
| 401 | * structure. |
| 402 | */ |
| 403 | ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE); |
KY Srinivasan | b56fc3c | 2015-04-28 17:59:48 -0700 | [diff] [blame] | 404 | if (ret) { |
| 405 | netdev_err(net, "unable to alloc hv_netvsc_packet\n"); |
| 406 | ret = -ENOMEM; |
| 407 | goto drop; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 408 | } |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 409 | /* Use the skb control buffer for building up the packet */ |
| 410 | BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) > |
| 411 | FIELD_SIZEOF(struct sk_buff, cb)); |
| 412 | packet = (struct hv_netvsc_packet *)skb->cb; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 413 | |
Haiyang Zhang | 1f5f3a7 | 2012-03-12 10:20:50 +0000 | [diff] [blame] | 414 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 415 | packet->q_idx = skb_get_queue_mapping(skb); |
| 416 | |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 417 | packet->total_data_buflen = skb->len; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 418 | |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 419 | rndis_msg = (struct rndis_message *)skb->head; |
KY Srinivasan | b08cc79 | 2015-03-29 21:08:42 -0700 | [diff] [blame] | 420 | |
KY Srinivasan | 2447676 | 2015-12-01 16:43:06 -0800 | [diff] [blame] | 421 | memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 422 | |
KY Srinivasan | 760d1e3 | 2015-12-01 16:43:19 -0800 | [diff] [blame] | 423 | isvlan = skb->vlan_tci & VLAN_TAG_PRESENT; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 424 | |
| 425 | /* Add the rndis header */ |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 426 | rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET; |
| 427 | rndis_msg->msg_len = packet->total_data_buflen; |
| 428 | rndis_pkt = &rndis_msg->msg.pkt; |
| 429 | rndis_pkt->data_offset = sizeof(struct rndis_packet); |
| 430 | rndis_pkt->data_len = packet->total_data_buflen; |
| 431 | rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet); |
| 432 | |
| 433 | rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet); |
| 434 | |
Haiyang Zhang | 307f099 | 2014-05-21 12:55:39 -0700 | [diff] [blame] | 435 | hash = skb_get_hash_raw(skb); |
| 436 | if (hash != 0 && net->real_num_tx_queues > 1) { |
| 437 | rndis_msg_size += NDIS_HASH_PPI_SIZE; |
| 438 | ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE, |
| 439 | NBL_HASH_VALUE); |
| 440 | *(u32 *)((void *)ppi + ppi->ppi_offset) = hash; |
| 441 | } |
| 442 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 443 | if (isvlan) { |
| 444 | struct ndis_pkt_8021q_info *vlan; |
| 445 | |
| 446 | rndis_msg_size += NDIS_VLAN_PPI_SIZE; |
| 447 | ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE, |
| 448 | IEEE_8021Q_INFO); |
| 449 | vlan = (struct ndis_pkt_8021q_info *)((void *)ppi + |
| 450 | ppi->ppi_offset); |
KY Srinivasan | 760d1e3 | 2015-12-01 16:43:19 -0800 | [diff] [blame] | 451 | vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK; |
| 452 | vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >> |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 453 | VLAN_PRIO_SHIFT; |
| 454 | } |
| 455 | |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 456 | net_trans_info = get_net_transport_info(skb, &hdr_offset); |
| 457 | if (net_trans_info == TRANSPORT_INFO_NOT_IP) |
| 458 | goto do_send; |
| 459 | |
| 460 | /* |
| 461 | * Setup the sendside checksum offload only if this is not a |
| 462 | * GSO packet. |
| 463 | */ |
| 464 | if (skb_is_gso(skb)) |
KY Srinivasan | 77bf548 | 2014-03-08 19:23:18 -0800 | [diff] [blame] | 465 | goto do_lso; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 466 | |
KY Srinivasan | 22041fb | 2014-04-30 11:58:25 -0700 | [diff] [blame] | 467 | if ((skb->ip_summed == CHECKSUM_NONE) || |
| 468 | (skb->ip_summed == CHECKSUM_UNNECESSARY)) |
| 469 | goto do_send; |
| 470 | |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 471 | rndis_msg_size += NDIS_CSUM_PPI_SIZE; |
| 472 | ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE, |
| 473 | TCPIP_CHKSUM_PKTINFO); |
| 474 | |
| 475 | csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi + |
| 476 | ppi->ppi_offset); |
| 477 | |
| 478 | if (net_trans_info & (INFO_IPV4 << 16)) |
| 479 | csum_info->transmit.is_ipv4 = 1; |
| 480 | else |
| 481 | csum_info->transmit.is_ipv6 = 1; |
| 482 | |
| 483 | if (net_trans_info & INFO_TCP) { |
| 484 | csum_info->transmit.tcp_checksum = 1; |
| 485 | csum_info->transmit.tcp_header_offset = hdr_offset; |
| 486 | } else if (net_trans_info & INFO_UDP) { |
KY Srinivasan | af9893a | 2014-04-09 15:00:47 -0700 | [diff] [blame] | 487 | /* UDP checksum offload is not supported on ws2008r2. |
| 488 | * Furthermore, on ws2012 and ws2012r2, there are some |
| 489 | * issues with udp checksum offload from Linux guests. |
| 490 | * (these are host issues). |
| 491 | * For now compute the checksum here. |
| 492 | */ |
| 493 | struct udphdr *uh; |
| 494 | u16 udp_len; |
| 495 | |
| 496 | ret = skb_cow_head(skb, 0); |
| 497 | if (ret) |
| 498 | goto drop; |
| 499 | |
| 500 | uh = udp_hdr(skb); |
| 501 | udp_len = ntohs(uh->len); |
| 502 | uh->check = 0; |
| 503 | uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 504 | ip_hdr(skb)->daddr, |
| 505 | udp_len, IPPROTO_UDP, |
| 506 | csum_partial(uh, udp_len, 0)); |
| 507 | if (uh->check == 0) |
| 508 | uh->check = CSUM_MANGLED_0; |
| 509 | |
| 510 | csum_info->transmit.udp_checksum = 0; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 511 | } |
KY Srinivasan | 77bf548 | 2014-03-08 19:23:18 -0800 | [diff] [blame] | 512 | goto do_send; |
| 513 | |
| 514 | do_lso: |
| 515 | rndis_msg_size += NDIS_LSO_PPI_SIZE; |
| 516 | ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE, |
| 517 | TCP_LARGESEND_PKTINFO); |
| 518 | |
| 519 | lso_info = (struct ndis_tcp_lso_info *)((void *)ppi + |
| 520 | ppi->ppi_offset); |
| 521 | |
| 522 | lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE; |
| 523 | if (net_trans_info & (INFO_IPV4 << 16)) { |
| 524 | lso_info->lso_v2_transmit.ip_version = |
| 525 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4; |
| 526 | ip_hdr(skb)->tot_len = 0; |
| 527 | ip_hdr(skb)->check = 0; |
| 528 | tcp_hdr(skb)->check = |
| 529 | ~csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 530 | ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); |
| 531 | } else { |
| 532 | lso_info->lso_v2_transmit.ip_version = |
| 533 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6; |
| 534 | ipv6_hdr(skb)->payload_len = 0; |
| 535 | tcp_hdr(skb)->check = |
| 536 | ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 537 | &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); |
| 538 | } |
| 539 | lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset; |
| 540 | lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 541 | |
| 542 | do_send: |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 543 | /* Start filling in the page buffers with the rndis hdr */ |
| 544 | rndis_msg->msg_len += rndis_msg_size; |
Haiyang Zhang | 942396b | 2014-10-22 13:47:18 -0700 | [diff] [blame] | 545 | packet->total_data_buflen = rndis_msg->msg_len; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 546 | packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size, |
KY Srinivasan | a9f2e2d | 2015-12-01 16:43:13 -0800 | [diff] [blame] | 547 | skb, packet, &pb); |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 548 | |
sixiao@microsoft.com | 76d13b5 | 2016-02-17 16:43:59 -0800 | [diff] [blame] | 549 | /* timestamp packet in software */ |
| 550 | skb_tx_timestamp(skb); |
KY Srinivasan | 3a3d9a0 | 2015-12-01 16:43:14 -0800 | [diff] [blame] | 551 | ret = netvsc_send(net_device_ctx->device_ctx, packet, |
| 552 | rndis_msg, &pb, skb); |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 553 | |
KY Srinivasan | af9893a | 2014-04-09 15:00:47 -0700 | [diff] [blame] | 554 | drop: |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 555 | if (ret == 0) { |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 556 | u64_stats_update_begin(&tx_stats->syncp); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 557 | tx_stats->packets++; |
| 558 | tx_stats->bytes += skb_length; |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 559 | u64_stats_update_end(&tx_stats->syncp); |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 560 | } else { |
Haiyang Zhang | 33be96e | 2012-03-27 13:20:45 +0000 | [diff] [blame] | 561 | if (ret != -EAGAIN) { |
| 562 | dev_kfree_skb_any(skb); |
| 563 | net->stats.tx_dropped++; |
| 564 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Haiyang Zhang | 33be96e | 2012-03-27 13:20:45 +0000 | [diff] [blame] | 567 | return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 570 | /* |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 571 | * netvsc_linkstatus_callback - Link up/down notification |
| 572 | */ |
K. Y. Srinivasan | 90ef117 | 2011-05-12 19:34:50 -0700 | [diff] [blame] | 573 | void netvsc_linkstatus_callback(struct hv_device *device_obj, |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 574 | struct rndis_message *resp) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 575 | { |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 576 | struct rndis_indicate_status *indicate = &resp->msg.indicate_status; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 577 | struct net_device *net; |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 578 | struct net_device_context *ndev_ctx; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 579 | struct netvsc_reconfig *event; |
| 580 | unsigned long flags; |
| 581 | |
Haiyang Zhang | 7f5d5af | 2016-08-04 10:42:15 -0700 | [diff] [blame] | 582 | net = hv_get_drvdata(device_obj); |
| 583 | |
| 584 | if (!net) |
| 585 | return; |
| 586 | |
| 587 | ndev_ctx = netdev_priv(net); |
| 588 | |
| 589 | /* Update the physical link speed when changing to another vSwitch */ |
| 590 | if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) { |
| 591 | u32 speed; |
| 592 | |
| 593 | speed = *(u32 *)((void *)indicate + indicate-> |
| 594 | status_buf_offset) / 10000; |
| 595 | ndev_ctx->speed = speed; |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | /* Handle these link change statuses below */ |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 600 | if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE && |
| 601 | indicate->status != RNDIS_STATUS_MEDIA_CONNECT && |
| 602 | indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT) |
| 603 | return; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 604 | |
Haiyang Zhang | 7f5d5af | 2016-08-04 10:42:15 -0700 | [diff] [blame] | 605 | if (net->reg_state != NETREG_REGISTERED) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 606 | return; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 607 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 608 | event = kzalloc(sizeof(*event), GFP_ATOMIC); |
| 609 | if (!event) |
| 610 | return; |
| 611 | event->event = indicate->status; |
| 612 | |
| 613 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
| 614 | list_add_tail(&event->list, &ndev_ctx->reconfig_events); |
| 615 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 616 | |
| 617 | schedule_delayed_work(&ndev_ctx->dwork, 0); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 618 | } |
| 619 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 620 | |
| 621 | static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net, |
KY Srinivasan | e3d605e | 2014-03-08 19:23:16 -0800 | [diff] [blame] | 622 | struct hv_netvsc_packet *packet, |
KY Srinivasan | 25b85ee | 2015-12-01 16:43:05 -0800 | [diff] [blame] | 623 | struct ndis_tcp_ip_checksum_info *csum_info, |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 624 | void *data, u16 vlan_tci) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 625 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 626 | struct sk_buff *skb; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 627 | |
Haiyang Zhang | 72a2f5b | 2010-12-10 12:03:58 -0800 | [diff] [blame] | 628 | skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 629 | if (!skb) |
| 630 | return skb; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 631 | |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 632 | /* |
| 633 | * Copy to skb. This copy is needed here since the memory pointed by |
| 634 | * hv_netvsc_packet cannot be deallocated |
| 635 | */ |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 636 | memcpy(skb_put(skb, packet->total_data_buflen), data, |
| 637 | packet->total_data_buflen); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 638 | |
| 639 | skb->protocol = eth_type_trans(skb, net); |
KY Srinivasan | e3d605e | 2014-03-08 19:23:16 -0800 | [diff] [blame] | 640 | if (csum_info) { |
| 641 | /* We only look at the IP checksum here. |
| 642 | * Should we be dropping the packet if checksum |
| 643 | * failed? How do we deal with other checksums - TCP/UDP? |
| 644 | */ |
| 645 | if (csum_info->receive.ip_checksum_succeeded) |
| 646 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 647 | else |
| 648 | skb->ip_summed = CHECKSUM_NONE; |
| 649 | } |
| 650 | |
KY Srinivasan | 760d1e3 | 2015-12-01 16:43:19 -0800 | [diff] [blame] | 651 | if (vlan_tci & VLAN_TAG_PRESENT) |
Haiyang Zhang | 93725cb | 2013-06-17 15:36:49 -0700 | [diff] [blame] | 652 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
KY Srinivasan | 760d1e3 | 2015-12-01 16:43:19 -0800 | [diff] [blame] | 653 | vlan_tci); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 654 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 655 | return skb; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * netvsc_recv_callback - Callback when we receive a packet from the |
| 660 | * "wire" on the specified device. |
| 661 | */ |
| 662 | int netvsc_recv_callback(struct hv_device *device_obj, |
| 663 | struct hv_netvsc_packet *packet, |
| 664 | void **data, |
| 665 | struct ndis_tcp_ip_checksum_info *csum_info, |
| 666 | struct vmbus_channel *channel, |
| 667 | u16 vlan_tci) |
| 668 | { |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 669 | struct net_device *net = hv_get_drvdata(device_obj); |
| 670 | struct net_device_context *net_device_ctx = netdev_priv(net); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 671 | struct sk_buff *skb; |
| 672 | struct sk_buff *vf_skb; |
| 673 | struct netvsc_stats *rx_stats; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 674 | u32 bytes_recvd = packet->total_data_buflen; |
| 675 | int ret = 0; |
| 676 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 677 | if (!net || net->reg_state != NETREG_REGISTERED) |
| 678 | return NVSP_STAT_FAIL; |
| 679 | |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 680 | if (READ_ONCE(net_device_ctx->vf_inject)) { |
| 681 | atomic_inc(&net_device_ctx->vf_use_cnt); |
| 682 | if (!READ_ONCE(net_device_ctx->vf_inject)) { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 683 | /* |
| 684 | * We raced; just move on. |
| 685 | */ |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 686 | atomic_dec(&net_device_ctx->vf_use_cnt); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 687 | goto vf_injection_done; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Inject this packet into the VF inerface. |
| 692 | * On Hyper-V, multicast and brodcast packets |
| 693 | * are only delivered on the synthetic interface |
| 694 | * (after subjecting these to policy filters on |
| 695 | * the host). Deliver these via the VF interface |
| 696 | * in the guest. |
| 697 | */ |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 698 | vf_skb = netvsc_alloc_recv_skb(net_device_ctx->vf_netdev, |
| 699 | packet, csum_info, *data, |
| 700 | vlan_tci); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 701 | if (vf_skb != NULL) { |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 702 | ++net_device_ctx->vf_netdev->stats.rx_packets; |
| 703 | net_device_ctx->vf_netdev->stats.rx_bytes += |
| 704 | bytes_recvd; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 705 | netif_receive_skb(vf_skb); |
| 706 | } else { |
| 707 | ++net->stats.rx_dropped; |
| 708 | ret = NVSP_STAT_FAIL; |
| 709 | } |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 710 | atomic_dec(&net_device_ctx->vf_use_cnt); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 711 | return ret; |
| 712 | } |
| 713 | |
| 714 | vf_injection_done: |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 715 | rx_stats = this_cpu_ptr(net_device_ctx->rx_stats); |
| 716 | |
| 717 | /* Allocate a skb - TODO direct I/O to pages? */ |
| 718 | skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci); |
| 719 | if (unlikely(!skb)) { |
| 720 | ++net->stats.rx_dropped; |
| 721 | return NVSP_STAT_FAIL; |
| 722 | } |
KY Srinivasan | 25b85ee | 2015-12-01 16:43:05 -0800 | [diff] [blame] | 723 | skb_record_rx_queue(skb, channel-> |
Haiyang Zhang | e565e80 | 2014-05-08 15:14:10 -0700 | [diff] [blame] | 724 | offermsg.offer.sub_channel_index); |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 725 | |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 726 | u64_stats_update_begin(&rx_stats->syncp); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 727 | rx_stats->packets++; |
| 728 | rx_stats->bytes += packet->total_data_buflen; |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 729 | u64_stats_update_end(&rx_stats->syncp); |
Stephen Hemminger | 9495c28 | 2010-03-09 17:42:17 -0800 | [diff] [blame] | 730 | |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 731 | /* |
| 732 | * Pass the skb back up. Network stack will deallocate the skb when it |
Stephen Hemminger | 9495c28 | 2010-03-09 17:42:17 -0800 | [diff] [blame] | 733 | * is done. |
| 734 | * TODO - use NAPI? |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 735 | */ |
Stephen Hemminger | 9495c28 | 2010-03-09 17:42:17 -0800 | [diff] [blame] | 736 | netif_rx(skb); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 737 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 738 | return 0; |
| 739 | } |
| 740 | |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 741 | static void netvsc_get_drvinfo(struct net_device *net, |
| 742 | struct ethtool_drvinfo *info) |
| 743 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 744 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 745 | strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 748 | static void netvsc_get_channels(struct net_device *net, |
| 749 | struct ethtool_channels *channel) |
| 750 | { |
| 751 | struct net_device_context *net_device_ctx = netdev_priv(net); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 752 | struct netvsc_device *nvdev = net_device_ctx->nvdev; |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 753 | |
| 754 | if (nvdev) { |
| 755 | channel->max_combined = nvdev->max_chn; |
| 756 | channel->combined_count = nvdev->num_chn; |
| 757 | } |
| 758 | } |
| 759 | |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 760 | static int netvsc_set_channels(struct net_device *net, |
| 761 | struct ethtool_channels *channels) |
| 762 | { |
| 763 | struct net_device_context *net_device_ctx = netdev_priv(net); |
| 764 | struct hv_device *dev = net_device_ctx->device_ctx; |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 765 | struct netvsc_device *nvdev = net_device_ctx->nvdev; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 766 | struct netvsc_device_info device_info; |
Andrew Schwartzmeyer | 954591b | 2015-08-18 20:06:32 -0700 | [diff] [blame] | 767 | u32 num_chn; |
| 768 | u32 max_chn; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 769 | int ret = 0; |
| 770 | bool recovering = false; |
| 771 | |
Vitaly Kuznetsov | 6da7225 | 2016-05-13 13:55:24 +0200 | [diff] [blame] | 772 | if (net_device_ctx->start_remove || !nvdev || nvdev->destroy) |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 773 | return -ENODEV; |
| 774 | |
Andrew Schwartzmeyer | 954591b | 2015-08-18 20:06:32 -0700 | [diff] [blame] | 775 | num_chn = nvdev->num_chn; |
| 776 | max_chn = min_t(u32, nvdev->max_chn, num_online_cpus()); |
| 777 | |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 778 | if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) { |
| 779 | pr_info("vRSS unsupported before NVSP Version 5\n"); |
| 780 | return -EINVAL; |
| 781 | } |
| 782 | |
| 783 | /* We do not support rx, tx, or other */ |
| 784 | if (!channels || |
| 785 | channels->rx_count || |
| 786 | channels->tx_count || |
| 787 | channels->other_count || |
| 788 | (channels->combined_count < 1)) |
| 789 | return -EINVAL; |
| 790 | |
| 791 | if (channels->combined_count > max_chn) { |
| 792 | pr_info("combined channels too high, using %d\n", max_chn); |
| 793 | channels->combined_count = max_chn; |
| 794 | } |
| 795 | |
| 796 | ret = netvsc_close(net); |
| 797 | if (ret) |
| 798 | goto out; |
| 799 | |
| 800 | do_set: |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 801 | net_device_ctx->start_remove = true; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 802 | rndis_filter_device_remove(dev); |
| 803 | |
| 804 | nvdev->num_chn = channels->combined_count; |
| 805 | |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 806 | memset(&device_info, 0, sizeof(device_info)); |
| 807 | device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */ |
| 808 | device_info.ring_size = ring_size; |
| 809 | device_info.max_num_vrss_chns = max_num_vrss_chns; |
| 810 | |
| 811 | ret = rndis_filter_device_add(dev, &device_info); |
| 812 | if (ret) { |
| 813 | if (recovering) { |
| 814 | netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); |
| 815 | return ret; |
| 816 | } |
| 817 | goto recover; |
| 818 | } |
| 819 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 820 | nvdev = net_device_ctx->nvdev; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 821 | |
| 822 | ret = netif_set_real_num_tx_queues(net, nvdev->num_chn); |
| 823 | if (ret) { |
| 824 | if (recovering) { |
| 825 | netdev_err(net, "could not set tx queue count (ret %d)\n", ret); |
| 826 | return ret; |
| 827 | } |
| 828 | goto recover; |
| 829 | } |
| 830 | |
| 831 | ret = netif_set_real_num_rx_queues(net, nvdev->num_chn); |
| 832 | if (ret) { |
| 833 | if (recovering) { |
| 834 | netdev_err(net, "could not set rx queue count (ret %d)\n", ret); |
| 835 | return ret; |
| 836 | } |
| 837 | goto recover; |
| 838 | } |
| 839 | |
| 840 | out: |
| 841 | netvsc_open(net); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 842 | net_device_ctx->start_remove = false; |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 843 | /* We may have missed link change notifications */ |
| 844 | schedule_delayed_work(&net_device_ctx->dwork, 0); |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 845 | |
| 846 | return ret; |
| 847 | |
| 848 | recover: |
| 849 | /* If the above failed, we attempt to recover through the same |
| 850 | * process but with the original number of channels. |
| 851 | */ |
| 852 | netdev_err(net, "could not set channels, recovering\n"); |
| 853 | recovering = true; |
| 854 | channels->combined_count = num_chn; |
| 855 | goto do_set; |
| 856 | } |
| 857 | |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 858 | static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd) |
| 859 | { |
| 860 | struct ethtool_cmd diff1 = *cmd; |
| 861 | struct ethtool_cmd diff2 = {}; |
| 862 | |
| 863 | ethtool_cmd_speed_set(&diff1, 0); |
| 864 | diff1.duplex = 0; |
| 865 | /* advertising and cmd are usually set */ |
| 866 | diff1.advertising = 0; |
| 867 | diff1.cmd = 0; |
| 868 | /* We set port to PORT_OTHER */ |
| 869 | diff2.port = PORT_OTHER; |
| 870 | |
| 871 | return !memcmp(&diff1, &diff2, sizeof(diff1)); |
| 872 | } |
| 873 | |
| 874 | static void netvsc_init_settings(struct net_device *dev) |
| 875 | { |
| 876 | struct net_device_context *ndc = netdev_priv(dev); |
| 877 | |
| 878 | ndc->speed = SPEED_UNKNOWN; |
| 879 | ndc->duplex = DUPLEX_UNKNOWN; |
| 880 | } |
| 881 | |
| 882 | static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 883 | { |
| 884 | struct net_device_context *ndc = netdev_priv(dev); |
| 885 | |
| 886 | ethtool_cmd_speed_set(cmd, ndc->speed); |
| 887 | cmd->duplex = ndc->duplex; |
| 888 | cmd->port = PORT_OTHER; |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 894 | { |
| 895 | struct net_device_context *ndc = netdev_priv(dev); |
| 896 | u32 speed; |
| 897 | |
| 898 | speed = ethtool_cmd_speed(cmd); |
| 899 | if (!ethtool_validate_speed(speed) || |
| 900 | !ethtool_validate_duplex(cmd->duplex) || |
| 901 | !netvsc_validate_ethtool_ss_cmd(cmd)) |
| 902 | return -EINVAL; |
| 903 | |
| 904 | ndc->speed = speed; |
| 905 | ndc->duplex = cmd->duplex; |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 910 | static int netvsc_change_mtu(struct net_device *ndev, int mtu) |
| 911 | { |
| 912 | struct net_device_context *ndevctx = netdev_priv(ndev); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 913 | struct netvsc_device *nvdev = ndevctx->nvdev; |
| 914 | struct hv_device *hdev = ndevctx->device_ctx; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 915 | struct netvsc_device_info device_info; |
| 916 | int limit = ETH_DATA_LEN; |
Haiyang Zhang | d212b46 | 2016-03-23 09:43:09 -0700 | [diff] [blame] | 917 | u32 num_chn; |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 918 | int ret = 0; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 919 | |
Vitaly Kuznetsov | 6da7225 | 2016-05-13 13:55:24 +0200 | [diff] [blame] | 920 | if (ndevctx->start_remove || !nvdev || nvdev->destroy) |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 921 | return -ENODEV; |
| 922 | |
Haiyang Zhang | a1eabb0 | 2014-02-19 15:49:45 -0800 | [diff] [blame] | 923 | if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) |
Haiyang Zhang | 4d3c9d3 | 2014-11-12 14:07:44 -0800 | [diff] [blame] | 924 | limit = NETVSC_MTU - ETH_HLEN; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 925 | |
Haiyang Zhang | f9cbce3 | 2015-07-06 14:11:37 -0700 | [diff] [blame] | 926 | if (mtu < NETVSC_MTU_MIN || mtu > limit) |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 927 | return -EINVAL; |
| 928 | |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 929 | ret = netvsc_close(ndev); |
| 930 | if (ret) |
| 931 | goto out; |
| 932 | |
Haiyang Zhang | d212b46 | 2016-03-23 09:43:09 -0700 | [diff] [blame] | 933 | num_chn = nvdev->num_chn; |
| 934 | |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 935 | ndevctx->start_remove = true; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 936 | rndis_filter_device_remove(hdev); |
| 937 | |
| 938 | ndev->mtu = mtu; |
| 939 | |
Andrew Schwartzmeyer | 8ebdcc5 | 2015-08-11 17:14:31 -0700 | [diff] [blame] | 940 | memset(&device_info, 0, sizeof(device_info)); |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 941 | device_info.ring_size = ring_size; |
Haiyang Zhang | d212b46 | 2016-03-23 09:43:09 -0700 | [diff] [blame] | 942 | device_info.num_chn = num_chn; |
KY Srinivasan | e01ec21 | 2015-05-27 13:16:57 -0700 | [diff] [blame] | 943 | device_info.max_num_vrss_chns = max_num_vrss_chns; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 944 | rndis_filter_device_add(hdev, &device_info); |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 945 | |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 946 | out: |
| 947 | netvsc_open(ndev); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 948 | ndevctx->start_remove = false; |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 949 | |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 950 | /* We may have missed link change notifications */ |
| 951 | schedule_delayed_work(&ndevctx->dwork, 0); |
| 952 | |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 953 | return ret; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 954 | } |
| 955 | |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 956 | static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net, |
| 957 | struct rtnl_link_stats64 *t) |
| 958 | { |
| 959 | struct net_device_context *ndev_ctx = netdev_priv(net); |
| 960 | int cpu; |
| 961 | |
| 962 | for_each_possible_cpu(cpu) { |
| 963 | struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats, |
| 964 | cpu); |
| 965 | struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats, |
| 966 | cpu); |
| 967 | u64 tx_packets, tx_bytes, rx_packets, rx_bytes; |
| 968 | unsigned int start; |
| 969 | |
| 970 | do { |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 971 | start = u64_stats_fetch_begin_irq(&tx_stats->syncp); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 972 | tx_packets = tx_stats->packets; |
| 973 | tx_bytes = tx_stats->bytes; |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 974 | } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start)); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 975 | |
| 976 | do { |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 977 | start = u64_stats_fetch_begin_irq(&rx_stats->syncp); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 978 | rx_packets = rx_stats->packets; |
| 979 | rx_bytes = rx_stats->bytes; |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 980 | } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start)); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 981 | |
| 982 | t->tx_bytes += tx_bytes; |
| 983 | t->tx_packets += tx_packets; |
| 984 | t->rx_bytes += rx_bytes; |
| 985 | t->rx_packets += rx_packets; |
| 986 | } |
| 987 | |
| 988 | t->tx_dropped = net->stats.tx_dropped; |
| 989 | t->tx_errors = net->stats.tx_dropped; |
| 990 | |
| 991 | t->rx_dropped = net->stats.rx_dropped; |
| 992 | t->rx_errors = net->stats.rx_errors; |
| 993 | |
| 994 | return t; |
| 995 | } |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 996 | |
| 997 | static int netvsc_set_mac_addr(struct net_device *ndev, void *p) |
| 998 | { |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 999 | struct sockaddr *addr = p; |
Jianjun Kong | 9a4c831 | 2013-01-18 16:52:09 +0000 | [diff] [blame] | 1000 | char save_adr[ETH_ALEN]; |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1001 | unsigned char save_aatype; |
| 1002 | int err; |
| 1003 | |
| 1004 | memcpy(save_adr, ndev->dev_addr, ETH_ALEN); |
| 1005 | save_aatype = ndev->addr_assign_type; |
| 1006 | |
| 1007 | err = eth_mac_addr(ndev, p); |
| 1008 | if (err != 0) |
| 1009 | return err; |
| 1010 | |
Vitaly Kuznetsov | e834da9a | 2016-06-03 17:51:01 +0200 | [diff] [blame] | 1011 | err = rndis_filter_set_device_mac(ndev, addr->sa_data); |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1012 | if (err != 0) { |
| 1013 | /* roll back to saved MAC */ |
| 1014 | memcpy(ndev->dev_addr, save_adr, ETH_ALEN); |
| 1015 | ndev->addr_assign_type = save_aatype; |
| 1016 | } |
| 1017 | |
| 1018 | return err; |
| 1019 | } |
| 1020 | |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1021 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1022 | static void netvsc_poll_controller(struct net_device *net) |
| 1023 | { |
| 1024 | /* As netvsc_start_xmit() works synchronous we don't have to |
| 1025 | * trigger anything here. |
| 1026 | */ |
| 1027 | } |
| 1028 | #endif |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1029 | |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1030 | static const struct ethtool_ops ethtool_ops = { |
| 1031 | .get_drvinfo = netvsc_get_drvinfo, |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1032 | .get_link = ethtool_op_get_link, |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 1033 | .get_channels = netvsc_get_channels, |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 1034 | .set_channels = netvsc_set_channels, |
sixiao@microsoft.com | 76d13b5 | 2016-02-17 16:43:59 -0800 | [diff] [blame] | 1035 | .get_ts_info = ethtool_op_get_ts_info, |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 1036 | .get_settings = netvsc_get_settings, |
| 1037 | .set_settings = netvsc_set_settings, |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1038 | }; |
| 1039 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1040 | static const struct net_device_ops device_ops = { |
| 1041 | .ndo_open = netvsc_open, |
| 1042 | .ndo_stop = netvsc_close, |
| 1043 | .ndo_start_xmit = netvsc_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1044 | .ndo_set_rx_mode = netvsc_set_multicast_list, |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 1045 | .ndo_change_mtu = netvsc_change_mtu, |
Haiyang Zhang | b681b58 | 2010-08-03 19:15:31 +0000 | [diff] [blame] | 1046 | .ndo_validate_addr = eth_validate_addr, |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1047 | .ndo_set_mac_address = netvsc_set_mac_addr, |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1048 | .ndo_select_queue = netvsc_select_queue, |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1049 | .ndo_get_stats64 = netvsc_get_stats64, |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1050 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1051 | .ndo_poll_controller = netvsc_poll_controller, |
| 1052 | #endif |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1053 | }; |
| 1054 | |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1055 | /* |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1056 | * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link |
| 1057 | * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is |
| 1058 | * present send GARP packet to network peers with netif_notify_peers(). |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1059 | */ |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1060 | static void netvsc_link_change(struct work_struct *w) |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1061 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1062 | struct net_device_context *ndev_ctx = |
| 1063 | container_of(w, struct net_device_context, dwork.work); |
| 1064 | struct hv_device *device_obj = ndev_ctx->device_ctx; |
| 1065 | struct net_device *net = hv_get_drvdata(device_obj); |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1066 | struct netvsc_device *net_device; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1067 | struct rndis_device *rdev; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1068 | struct netvsc_reconfig *event = NULL; |
| 1069 | bool notify = false, reschedule = false; |
| 1070 | unsigned long flags, next_reconfig, delay; |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1071 | |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1072 | rtnl_lock(); |
| 1073 | if (ndev_ctx->start_remove) |
| 1074 | goto out_unlock; |
| 1075 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 1076 | net_device = ndev_ctx->nvdev; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1077 | rdev = net_device->extension; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1078 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1079 | next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT; |
| 1080 | if (time_is_after_jiffies(next_reconfig)) { |
| 1081 | /* link_watch only sends one notification with current state |
| 1082 | * per second, avoid doing reconfig more frequently. Handle |
| 1083 | * wrap around. |
| 1084 | */ |
| 1085 | delay = next_reconfig - jiffies; |
| 1086 | delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT; |
| 1087 | schedule_delayed_work(&ndev_ctx->dwork, delay); |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1088 | goto out_unlock; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1089 | } |
| 1090 | ndev_ctx->last_reconfig = jiffies; |
| 1091 | |
| 1092 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
| 1093 | if (!list_empty(&ndev_ctx->reconfig_events)) { |
| 1094 | event = list_first_entry(&ndev_ctx->reconfig_events, |
| 1095 | struct netvsc_reconfig, list); |
| 1096 | list_del(&event->list); |
| 1097 | reschedule = !list_empty(&ndev_ctx->reconfig_events); |
| 1098 | } |
| 1099 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 1100 | |
| 1101 | if (!event) |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1102 | goto out_unlock; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1103 | |
| 1104 | switch (event->event) { |
| 1105 | /* Only the following events are possible due to the check in |
| 1106 | * netvsc_linkstatus_callback() |
| 1107 | */ |
| 1108 | case RNDIS_STATUS_MEDIA_CONNECT: |
| 1109 | if (rdev->link_state) { |
| 1110 | rdev->link_state = false; |
| 1111 | netif_carrier_on(net); |
| 1112 | netif_tx_wake_all_queues(net); |
| 1113 | } else { |
| 1114 | notify = true; |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 1115 | } |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1116 | kfree(event); |
| 1117 | break; |
| 1118 | case RNDIS_STATUS_MEDIA_DISCONNECT: |
| 1119 | if (!rdev->link_state) { |
| 1120 | rdev->link_state = true; |
| 1121 | netif_carrier_off(net); |
| 1122 | netif_tx_stop_all_queues(net); |
| 1123 | } |
| 1124 | kfree(event); |
| 1125 | break; |
| 1126 | case RNDIS_STATUS_NETWORK_CHANGE: |
| 1127 | /* Only makes sense if carrier is present */ |
| 1128 | if (!rdev->link_state) { |
| 1129 | rdev->link_state = true; |
| 1130 | netif_carrier_off(net); |
| 1131 | netif_tx_stop_all_queues(net); |
| 1132 | event->event = RNDIS_STATUS_MEDIA_CONNECT; |
| 1133 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
Haiyang Zhang | 15cfd40 | 2016-04-21 16:13:01 -0700 | [diff] [blame] | 1134 | list_add(&event->list, &ndev_ctx->reconfig_events); |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1135 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 1136 | reschedule = true; |
| 1137 | } |
| 1138 | break; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | rtnl_unlock(); |
| 1142 | |
| 1143 | if (notify) |
| 1144 | netdev_notify_peers(net); |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1145 | |
| 1146 | /* link_watch only sends one notification with current state per |
| 1147 | * second, handle next reconfig event in 2 seconds. |
| 1148 | */ |
| 1149 | if (reschedule) |
| 1150 | schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1151 | |
| 1152 | return; |
| 1153 | |
| 1154 | out_unlock: |
| 1155 | rtnl_unlock(); |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1158 | static void netvsc_free_netdev(struct net_device *netdev) |
| 1159 | { |
| 1160 | struct net_device_context *net_device_ctx = netdev_priv(netdev); |
| 1161 | |
| 1162 | free_percpu(net_device_ctx->tx_stats); |
| 1163 | free_percpu(net_device_ctx->rx_stats); |
| 1164 | free_netdev(netdev); |
| 1165 | } |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1166 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1167 | static struct net_device *get_netvsc_net_device(char *mac) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1168 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1169 | struct net_device *dev, *found = NULL; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1170 | |
Stephen Hemminger | 8737caa | 2016-08-23 12:17:44 -0700 | [diff] [blame^] | 1171 | ASSERT_RTNL(); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1172 | |
| 1173 | for_each_netdev(&init_net, dev) { |
| 1174 | if (memcmp(dev->dev_addr, mac, ETH_ALEN) == 0) { |
| 1175 | if (dev->netdev_ops != &device_ops) |
| 1176 | continue; |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1177 | found = dev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1178 | break; |
| 1179 | } |
| 1180 | } |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1181 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1182 | return found; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | static int netvsc_register_vf(struct net_device *vf_netdev) |
| 1186 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1187 | struct net_device *ndev; |
| 1188 | struct net_device_context *net_device_ctx; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1189 | struct netvsc_device *netvsc_dev; |
| 1190 | const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops; |
| 1191 | |
| 1192 | if (eth_ops == NULL || eth_ops == ðtool_ops) |
| 1193 | return NOTIFY_DONE; |
| 1194 | |
| 1195 | /* |
| 1196 | * We will use the MAC address to locate the synthetic interface to |
| 1197 | * associate with the VF interface. If we don't find a matching |
| 1198 | * synthetic interface, move on. |
| 1199 | */ |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1200 | ndev = get_netvsc_net_device(vf_netdev->dev_addr); |
| 1201 | if (!ndev) |
| 1202 | return NOTIFY_DONE; |
| 1203 | |
| 1204 | net_device_ctx = netdev_priv(ndev); |
| 1205 | netvsc_dev = net_device_ctx->nvdev; |
Vitaly Kuznetsov | 0f20d79 | 2016-08-15 17:48:42 +0200 | [diff] [blame] | 1206 | if (!netvsc_dev || net_device_ctx->vf_netdev) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1207 | return NOTIFY_DONE; |
| 1208 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1209 | netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1210 | /* |
| 1211 | * Take a reference on the module. |
| 1212 | */ |
| 1213 | try_module_get(THIS_MODULE); |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 1214 | net_device_ctx->vf_netdev = vf_netdev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1215 | return NOTIFY_OK; |
| 1216 | } |
| 1217 | |
Vitaly Kuznetsov | 57c1826b99 | 2016-08-15 17:48:41 +0200 | [diff] [blame] | 1218 | static void netvsc_inject_enable(struct net_device_context *net_device_ctx) |
| 1219 | { |
| 1220 | net_device_ctx->vf_inject = true; |
| 1221 | } |
| 1222 | |
| 1223 | static void netvsc_inject_disable(struct net_device_context *net_device_ctx) |
| 1224 | { |
| 1225 | net_device_ctx->vf_inject = false; |
| 1226 | |
| 1227 | /* Wait for currently active users to drain out. */ |
| 1228 | while (atomic_read(&net_device_ctx->vf_use_cnt) != 0) |
| 1229 | udelay(50); |
| 1230 | } |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1231 | |
| 1232 | static int netvsc_vf_up(struct net_device *vf_netdev) |
| 1233 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1234 | struct net_device *ndev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1235 | struct netvsc_device *netvsc_dev; |
| 1236 | const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops; |
| 1237 | struct net_device_context *net_device_ctx; |
| 1238 | |
| 1239 | if (eth_ops == ðtool_ops) |
| 1240 | return NOTIFY_DONE; |
| 1241 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1242 | ndev = get_netvsc_net_device(vf_netdev->dev_addr); |
| 1243 | if (!ndev) |
| 1244 | return NOTIFY_DONE; |
| 1245 | |
| 1246 | net_device_ctx = netdev_priv(ndev); |
| 1247 | netvsc_dev = net_device_ctx->nvdev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1248 | |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 1249 | if (!netvsc_dev || !net_device_ctx->vf_netdev) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1250 | return NOTIFY_DONE; |
| 1251 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1252 | netdev_info(ndev, "VF up: %s\n", vf_netdev->name); |
Vitaly Kuznetsov | 57c1826b99 | 2016-08-15 17:48:41 +0200 | [diff] [blame] | 1253 | netvsc_inject_enable(net_device_ctx); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1254 | |
| 1255 | /* |
| 1256 | * Open the device before switching data path. |
| 1257 | */ |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 1258 | rndis_filter_open(netvsc_dev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1259 | |
| 1260 | /* |
| 1261 | * notify the host to switch the data path. |
| 1262 | */ |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1263 | netvsc_switch_datapath(ndev, true); |
| 1264 | netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1265 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1266 | netif_carrier_off(ndev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1267 | |
Vitaly Kuznetsov | d072218 | 2016-08-15 17:48:40 +0200 | [diff] [blame] | 1268 | /* Now notify peers through VF device. */ |
| 1269 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1270 | |
| 1271 | return NOTIFY_OK; |
| 1272 | } |
| 1273 | |
| 1274 | |
| 1275 | static int netvsc_vf_down(struct net_device *vf_netdev) |
| 1276 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1277 | struct net_device *ndev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1278 | struct netvsc_device *netvsc_dev; |
| 1279 | struct net_device_context *net_device_ctx; |
| 1280 | const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops; |
| 1281 | |
| 1282 | if (eth_ops == ðtool_ops) |
| 1283 | return NOTIFY_DONE; |
| 1284 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1285 | ndev = get_netvsc_net_device(vf_netdev->dev_addr); |
| 1286 | if (!ndev) |
| 1287 | return NOTIFY_DONE; |
| 1288 | |
| 1289 | net_device_ctx = netdev_priv(ndev); |
| 1290 | netvsc_dev = net_device_ctx->nvdev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1291 | |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 1292 | if (!netvsc_dev || !net_device_ctx->vf_netdev) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1293 | return NOTIFY_DONE; |
| 1294 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1295 | netdev_info(ndev, "VF down: %s\n", vf_netdev->name); |
Vitaly Kuznetsov | 57c1826b99 | 2016-08-15 17:48:41 +0200 | [diff] [blame] | 1296 | netvsc_inject_disable(net_device_ctx); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1297 | netvsc_switch_datapath(ndev, false); |
| 1298 | netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name); |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 1299 | rndis_filter_close(netvsc_dev); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1300 | netif_carrier_on(ndev); |
Vitaly Kuznetsov | d072218 | 2016-08-15 17:48:40 +0200 | [diff] [blame] | 1301 | |
| 1302 | /* Now notify peers through netvsc device. */ |
| 1303 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1304 | |
| 1305 | return NOTIFY_OK; |
| 1306 | } |
| 1307 | |
| 1308 | |
| 1309 | static int netvsc_unregister_vf(struct net_device *vf_netdev) |
| 1310 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1311 | struct net_device *ndev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1312 | struct netvsc_device *netvsc_dev; |
| 1313 | const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops; |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1314 | struct net_device_context *net_device_ctx; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1315 | |
| 1316 | if (eth_ops == ðtool_ops) |
| 1317 | return NOTIFY_DONE; |
| 1318 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1319 | ndev = get_netvsc_net_device(vf_netdev->dev_addr); |
| 1320 | if (!ndev) |
| 1321 | return NOTIFY_DONE; |
| 1322 | |
| 1323 | net_device_ctx = netdev_priv(ndev); |
| 1324 | netvsc_dev = net_device_ctx->nvdev; |
Vitaly Kuznetsov | 0f20d79 | 2016-08-15 17:48:42 +0200 | [diff] [blame] | 1325 | if (!netvsc_dev || !net_device_ctx->vf_netdev) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1326 | return NOTIFY_DONE; |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1327 | netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name); |
Vitaly Kuznetsov | 57c1826b99 | 2016-08-15 17:48:41 +0200 | [diff] [blame] | 1328 | netvsc_inject_disable(net_device_ctx); |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 1329 | net_device_ctx->vf_netdev = NULL; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1330 | module_put(THIS_MODULE); |
| 1331 | return NOTIFY_OK; |
| 1332 | } |
| 1333 | |
K. Y. Srinivasan | 8494689 | 2011-09-13 10:59:38 -0700 | [diff] [blame] | 1334 | static int netvsc_probe(struct hv_device *dev, |
| 1335 | const struct hv_vmbus_device_id *dev_id) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1336 | { |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1337 | struct net_device *net = NULL; |
| 1338 | struct net_device_context *net_device_ctx; |
| 1339 | struct netvsc_device_info device_info; |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1340 | struct netvsc_device *nvdev; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1341 | int ret; |
| 1342 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1343 | net = alloc_etherdev_mq(sizeof(struct net_device_context), |
| 1344 | num_online_cpus()); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1345 | if (!net) |
K. Y. Srinivasan | 51a805d | 2011-08-25 09:49:11 -0700 | [diff] [blame] | 1346 | return -ENOMEM; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1347 | |
Haiyang Zhang | 1b07da5 | 2014-03-04 14:11:06 -0800 | [diff] [blame] | 1348 | netif_carrier_off(net); |
| 1349 | |
Haiyang Zhang | b37879e | 2016-08-04 10:42:14 -0700 | [diff] [blame] | 1350 | netvsc_init_settings(net); |
| 1351 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1352 | net_device_ctx = netdev_priv(net); |
K. Y. Srinivasan | 9efd21e | 2011-04-29 13:45:10 -0700 | [diff] [blame] | 1353 | net_device_ctx->device_ctx = dev; |
Simon Xiao | 3f300ff | 2015-04-28 01:05:17 -0700 | [diff] [blame] | 1354 | net_device_ctx->msg_enable = netif_msg_init(debug, default_msg); |
| 1355 | if (netif_msg_probe(net_device_ctx)) |
| 1356 | netdev_dbg(net, "netvsc msg_enable: %d\n", |
| 1357 | net_device_ctx->msg_enable); |
| 1358 | |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1359 | net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats); |
| 1360 | if (!net_device_ctx->tx_stats) { |
| 1361 | free_netdev(net); |
| 1362 | return -ENOMEM; |
| 1363 | } |
| 1364 | net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats); |
| 1365 | if (!net_device_ctx->rx_stats) { |
| 1366 | free_percpu(net_device_ctx->tx_stats); |
| 1367 | free_netdev(net); |
| 1368 | return -ENOMEM; |
| 1369 | } |
| 1370 | |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1371 | hv_set_drvdata(dev, net); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 1372 | |
| 1373 | net_device_ctx->start_remove = false; |
| 1374 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1375 | INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change); |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 1376 | INIT_WORK(&net_device_ctx->work, do_set_multicast); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1377 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1378 | spin_lock_init(&net_device_ctx->lock); |
| 1379 | INIT_LIST_HEAD(&net_device_ctx->reconfig_events); |
| 1380 | |
Vitaly Kuznetsov | f9a7da9 | 2016-08-15 17:48:39 +0200 | [diff] [blame] | 1381 | atomic_set(&net_device_ctx->vf_use_cnt, 0); |
| 1382 | net_device_ctx->vf_netdev = NULL; |
| 1383 | net_device_ctx->vf_inject = false; |
| 1384 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1385 | net->netdev_ops = &device_ops; |
| 1386 | |
sixiao@microsoft.com | a060679 | 2016-02-04 15:49:34 -0800 | [diff] [blame] | 1387 | net->hw_features = NETVSC_HW_FEATURES; |
| 1388 | net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX; |
Stephen Hemminger | 6048718 | 2010-05-04 09:58:55 -0700 | [diff] [blame] | 1389 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1390 | net->ethtool_ops = ðtool_ops; |
K. Y. Srinivasan | 9efd21e | 2011-04-29 13:45:10 -0700 | [diff] [blame] | 1391 | SET_NETDEV_DEV(net, &dev->device); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1392 | |
Vitaly Kuznetsov | 14a03cf | 2016-02-05 17:29:08 +0100 | [diff] [blame] | 1393 | /* We always need headroom for rndis header */ |
| 1394 | net->needed_headroom = RNDIS_AND_PPI_SIZE; |
| 1395 | |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1396 | /* Notify the netvsc driver of the new device */ |
Andrew Schwartzmeyer | 8ebdcc5 | 2015-08-11 17:14:31 -0700 | [diff] [blame] | 1397 | memset(&device_info, 0, sizeof(device_info)); |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1398 | device_info.ring_size = ring_size; |
KY Srinivasan | e01ec21 | 2015-05-27 13:16:57 -0700 | [diff] [blame] | 1399 | device_info.max_num_vrss_chns = max_num_vrss_chns; |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1400 | ret = rndis_filter_device_add(dev, &device_info); |
| 1401 | if (ret != 0) { |
| 1402 | netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1403 | netvsc_free_netdev(net); |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1404 | hv_set_drvdata(dev, NULL); |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1405 | return ret; |
| 1406 | } |
| 1407 | memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN); |
| 1408 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 1409 | nvdev = net_device_ctx->nvdev; |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1410 | netif_set_real_num_tx_queues(net, nvdev->num_chn); |
| 1411 | netif_set_real_num_rx_queues(net, nvdev->num_chn); |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1412 | |
Haiyang Zhang | a68f961 | 2013-12-20 16:52:31 -0800 | [diff] [blame] | 1413 | ret = register_netdev(net); |
| 1414 | if (ret != 0) { |
| 1415 | pr_err("Unable to register netdev.\n"); |
| 1416 | rndis_filter_device_remove(dev); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1417 | netvsc_free_netdev(net); |
Haiyang Zhang | a68f961 | 2013-12-20 16:52:31 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1420 | return ret; |
| 1421 | } |
| 1422 | |
K. Y. Srinivasan | 415b023 | 2011-04-29 13:45:12 -0700 | [diff] [blame] | 1423 | static int netvsc_remove(struct hv_device *dev) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1424 | { |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1425 | struct net_device *net; |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 1426 | struct net_device_context *ndev_ctx; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1427 | struct netvsc_device *net_device; |
| 1428 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 1429 | net = hv_get_drvdata(dev); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1430 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1431 | if (net == NULL) { |
K. Y. Srinivasan | 415b023 | 2011-04-29 13:45:12 -0700 | [diff] [blame] | 1432 | dev_err(&dev->device, "No net device to remove\n"); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
| 1435 | |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 1436 | |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 1437 | ndev_ctx = netdev_priv(net); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 1438 | net_device = ndev_ctx->nvdev; |
| 1439 | |
Vitaly Kuznetsov | 6da7225 | 2016-05-13 13:55:24 +0200 | [diff] [blame] | 1440 | /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels() |
| 1441 | * removing the device. |
| 1442 | */ |
| 1443 | rtnl_lock(); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 1444 | ndev_ctx->start_remove = true; |
Vitaly Kuznetsov | 6da7225 | 2016-05-13 13:55:24 +0200 | [diff] [blame] | 1445 | rtnl_unlock(); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 1446 | |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 1447 | cancel_delayed_work_sync(&ndev_ctx->dwork); |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 1448 | cancel_work_sync(&ndev_ctx->work); |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 1449 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1450 | /* Stop outbound asap */ |
Haiyang Zhang | 0a28253 | 2012-02-02 07:17:59 +0000 | [diff] [blame] | 1451 | netif_tx_disable(net); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1452 | |
| 1453 | unregister_netdev(net); |
| 1454 | |
| 1455 | /* |
| 1456 | * Call to the vsc driver to let it know that the device is being |
| 1457 | * removed |
| 1458 | */ |
Haiyang Zhang | df06bcf | 2011-05-23 09:03:47 -0700 | [diff] [blame] | 1459 | rndis_filter_device_remove(dev); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1460 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 1461 | hv_set_drvdata(dev, NULL); |
| 1462 | |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1463 | netvsc_free_netdev(net); |
Haiyang Zhang | df06bcf | 2011-05-23 09:03:47 -0700 | [diff] [blame] | 1464 | return 0; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 1467 | static const struct hv_vmbus_device_id id_table[] = { |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1468 | /* Network guid */ |
K. Y. Srinivasan | 8f50594 | 2013-01-23 17:42:42 -0800 | [diff] [blame] | 1469 | { HV_NIC_GUID, }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1470 | { }, |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 1471 | }; |
| 1472 | |
| 1473 | MODULE_DEVICE_TABLE(vmbus, id_table); |
| 1474 | |
K. Y. Srinivasan | f1542a6 | 2011-05-10 07:55:16 -0700 | [diff] [blame] | 1475 | /* The one and only one */ |
K. Y. Srinivasan | fde0ef9 | 2011-05-12 19:35:08 -0700 | [diff] [blame] | 1476 | static struct hv_driver netvsc_drv = { |
Haiyang Zhang | d31b20f | 2012-03-07 10:02:00 +0000 | [diff] [blame] | 1477 | .name = KBUILD_MODNAME, |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 1478 | .id_table = id_table, |
K. Y. Srinivasan | fde0ef9 | 2011-05-12 19:35:08 -0700 | [diff] [blame] | 1479 | .probe = netvsc_probe, |
| 1480 | .remove = netvsc_remove, |
K. Y. Srinivasan | d489097 | 2011-05-10 07:55:17 -0700 | [diff] [blame] | 1481 | }; |
K. Y. Srinivasan | f1542a6 | 2011-05-10 07:55:16 -0700 | [diff] [blame] | 1482 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1483 | |
| 1484 | /* |
| 1485 | * On Hyper-V, every VF interface is matched with a corresponding |
| 1486 | * synthetic interface. The synthetic interface is presented first |
| 1487 | * to the guest. When the corresponding VF instance is registered, |
| 1488 | * we will take care of switching the data path. |
| 1489 | */ |
| 1490 | static int netvsc_netdev_event(struct notifier_block *this, |
| 1491 | unsigned long event, void *ptr) |
| 1492 | { |
| 1493 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); |
| 1494 | |
Vitaly Kuznetsov | 0dbff14 | 2016-08-15 17:48:43 +0200 | [diff] [blame] | 1495 | /* Avoid Vlan dev with same MAC registering as VF */ |
| 1496 | if (event_dev->priv_flags & IFF_802_1Q_VLAN) |
| 1497 | return NOTIFY_DONE; |
| 1498 | |
| 1499 | /* Avoid Bonding master dev with same MAC registering as VF */ |
| 1500 | if (event_dev->priv_flags & IFF_BONDING && |
| 1501 | event_dev->flags & IFF_MASTER) |
Haiyang Zhang | cb2911f | 2016-06-02 12:02:04 -0700 | [diff] [blame] | 1502 | return NOTIFY_DONE; |
| 1503 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1504 | switch (event) { |
| 1505 | case NETDEV_REGISTER: |
| 1506 | return netvsc_register_vf(event_dev); |
| 1507 | case NETDEV_UNREGISTER: |
| 1508 | return netvsc_unregister_vf(event_dev); |
| 1509 | case NETDEV_UP: |
| 1510 | return netvsc_vf_up(event_dev); |
| 1511 | case NETDEV_DOWN: |
| 1512 | return netvsc_vf_down(event_dev); |
| 1513 | default: |
| 1514 | return NOTIFY_DONE; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | static struct notifier_block netvsc_netdev_notifier = { |
| 1519 | .notifier_call = netvsc_netdev_event, |
| 1520 | }; |
| 1521 | |
K. Y. Srinivasan | a9869c9 | 2011-05-12 19:35:17 -0700 | [diff] [blame] | 1522 | static void __exit netvsc_drv_exit(void) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1523 | { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1524 | unregister_netdevice_notifier(&netvsc_netdev_notifier); |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1525 | vmbus_driver_unregister(&netvsc_drv); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
K. Y. Srinivasan | 1fde28c | 2011-05-12 19:35:16 -0700 | [diff] [blame] | 1528 | static int __init netvsc_drv_init(void) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1529 | { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1530 | int ret; |
| 1531 | |
Haiyang Zhang | fa85a6c | 2012-07-25 08:08:41 +0000 | [diff] [blame] | 1532 | if (ring_size < RING_SIZE_MIN) { |
| 1533 | ring_size = RING_SIZE_MIN; |
| 1534 | pr_info("Increased ring_size to %d (min allowed)\n", |
| 1535 | ring_size); |
| 1536 | } |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1537 | ret = vmbus_driver_register(&netvsc_drv); |
| 1538 | |
| 1539 | if (ret) |
| 1540 | return ret; |
| 1541 | |
| 1542 | register_netdevice_notifier(&netvsc_netdev_notifier); |
| 1543 | return 0; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 1546 | MODULE_LICENSE("GPL"); |
Stephen Hemminger | 7880fc5 | 2010-05-04 09:58:52 -0700 | [diff] [blame] | 1547 | MODULE_DESCRIPTION("Microsoft Hyper-V network driver"); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1548 | |
K. Y. Srinivasan | 1fde28c | 2011-05-12 19:35:16 -0700 | [diff] [blame] | 1549 | module_init(netvsc_drv_init); |
K. Y. Srinivasan | a9869c9 | 2011-05-12 19:35:17 -0700 | [diff] [blame] | 1550 | module_exit(netvsc_drv_exit); |