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