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