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> |
stephen hemminger | 27f5aa9 | 2017-07-24 10:57:29 -0700 | [diff] [blame] | 36 | #include <linux/rtnetlink.h> |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 37 | #include <linux/netpoll.h> |
stephen hemminger | 27f5aa9 | 2017-07-24 10:57:29 -0700 | [diff] [blame] | 38 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 39 | #include <net/arp.h> |
| 40 | #include <net/route.h> |
| 41 | #include <net/sock.h> |
| 42 | #include <net/pkt_sched.h> |
Michael Kelley | 8eb1b3c | 2017-05-30 11:36:56 -0700 | [diff] [blame] | 43 | #include <net/checksum.h> |
| 44 | #include <net/ip6_checksum.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 45 | |
K. Y. Srinivasan | 5ca7252 | 2011-05-12 19:34:37 -0700 | [diff] [blame] | 46 | #include "hyperv_net.h" |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 47 | |
Haiyang Zhang | fa85a6c | 2012-07-25 08:08:41 +0000 | [diff] [blame] | 48 | #define RING_SIZE_MIN 64 |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 49 | #define LINKCHANGE_INT (2 * HZ) |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 50 | #define VF_TAKEOVER_INT (HZ / 10) |
stephen hemminger | a50af86 | 2016-12-06 13:43:54 -0800 | [diff] [blame] | 51 | |
Hank Janssen | 99c8da0 | 2010-10-12 10:45:23 -0700 | [diff] [blame] | 52 | static int ring_size = 128; |
Stephen Hemminger | 450d7a4 | 2010-05-04 09:58:53 -0700 | [diff] [blame] | 53 | module_param(ring_size, int, S_IRUGO); |
| 54 | MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 55 | |
Simon Xiao | 3f300ff | 2015-04-28 01:05:17 -0700 | [diff] [blame] | 56 | static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | |
| 57 | NETIF_MSG_LINK | NETIF_MSG_IFUP | |
| 58 | NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | |
| 59 | NETIF_MSG_TX_ERR; |
| 60 | |
| 61 | static int debug = -1; |
| 62 | module_param(debug, int, S_IRUGO); |
| 63 | MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); |
| 64 | |
Greg Kroah-Hartman | 4e9bfef | 2009-07-15 12:45:20 -0700 | [diff] [blame] | 65 | static void netvsc_set_multicast_list(struct net_device *net) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 66 | { |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 67 | struct net_device_context *net_device_ctx = netdev_priv(net); |
stephen hemminger | 4f19c0d | 2017-06-07 15:53:49 -0700 | [diff] [blame] | 68 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 69 | |
stephen hemminger | 4f19c0d | 2017-06-07 15:53:49 -0700 | [diff] [blame] | 70 | rndis_filter_update(nvdev); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 73 | static int netvsc_open(struct net_device *net) |
| 74 | { |
Haiyang Zhang | 53fa1a6 | 2017-06-21 16:40:47 -0700 | [diff] [blame] | 75 | struct net_device_context *ndev_ctx = netdev_priv(net); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 76 | struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); |
stephen hemminger | 79e8cbe | 2017-07-19 11:53:13 -0700 | [diff] [blame] | 77 | struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev); |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 78 | struct rndis_device *rdev; |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 79 | int ret = 0; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 80 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 81 | netif_carrier_off(net); |
| 82 | |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 83 | /* Open up the device */ |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 84 | ret = rndis_filter_open(nvdev); |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 85 | if (ret != 0) { |
| 86 | netdev_err(net, "unable to open device (ret %d).\n", ret); |
| 87 | return ret; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 90 | netif_tx_wake_all_queues(net); |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 91 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 92 | rdev = nvdev->extension; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 93 | |
| 94 | if (!rdev->link_state) |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 95 | netif_carrier_on(net); |
| 96 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 97 | if (vf_netdev) { |
| 98 | /* Setting synthetic device up transparently sets |
| 99 | * slave as up. If open fails, then slave will be |
| 100 | * still be offline (and not used). |
| 101 | */ |
| 102 | ret = dev_open(vf_netdev); |
| 103 | if (ret) |
| 104 | netdev_warn(net, |
| 105 | "unable to open slave: %s: %d\n", |
| 106 | vf_netdev->name, ret); |
| 107 | } |
| 108 | return 0; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 111 | static int netvsc_close(struct net_device *net) |
| 112 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 113 | struct net_device_context *net_device_ctx = netdev_priv(net); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 114 | struct net_device *vf_netdev |
| 115 | = rtnl_dereference(net_device_ctx->vf_netdev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 116 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 117 | int ret; |
stephen hemminger | 4097596 | 2017-06-08 16:21:19 -0700 | [diff] [blame] | 118 | u32 aread, i, msec = 10, retry = 0, retry_max = 20; |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 119 | struct vmbus_channel *chn; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 120 | |
Haiyang Zhang | 0a28253 | 2012-02-02 07:17:59 +0000 | [diff] [blame] | 121 | netif_tx_disable(net); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 122 | |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 123 | ret = rndis_filter_close(nvdev); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 124 | if (ret != 0) { |
Hank Janssen | eb335bc | 2011-03-29 13:58:48 -0700 | [diff] [blame] | 125 | netdev_err(net, "unable to close device (ret %d).\n", ret); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | /* Ensure pending bytes in ring are read */ |
| 130 | while (true) { |
| 131 | aread = 0; |
| 132 | for (i = 0; i < nvdev->num_chn; i++) { |
stephen hemminger | b8b835a | 2017-01-24 13:06:07 -0800 | [diff] [blame] | 133 | chn = nvdev->chan_table[i].channel; |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 134 | if (!chn) |
| 135 | continue; |
| 136 | |
stephen hemminger | 4097596 | 2017-06-08 16:21:19 -0700 | [diff] [blame] | 137 | aread = hv_get_bytes_to_read(&chn->inbound); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 138 | if (aread) |
| 139 | break; |
| 140 | |
stephen hemminger | 4097596 | 2017-06-08 16:21:19 -0700 | [diff] [blame] | 141 | aread = hv_get_bytes_to_read(&chn->outbound); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 142 | if (aread) |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | retry++; |
| 147 | if (retry > retry_max || aread == 0) |
| 148 | break; |
| 149 | |
| 150 | msleep(msec); |
| 151 | |
| 152 | if (msec < 1000) |
| 153 | msec *= 2; |
| 154 | } |
| 155 | |
| 156 | if (aread) { |
| 157 | netdev_err(net, "Ring buffer not empty after closing rndis\n"); |
| 158 | ret = -ETIMEDOUT; |
| 159 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 160 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 161 | if (vf_netdev) |
| 162 | dev_close(vf_netdev); |
| 163 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 164 | return ret; |
| 165 | } |
| 166 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 167 | static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size, |
| 168 | int pkt_type) |
| 169 | { |
| 170 | struct rndis_packet *rndis_pkt; |
| 171 | struct rndis_per_packet_info *ppi; |
| 172 | |
| 173 | rndis_pkt = &msg->msg.pkt; |
| 174 | rndis_pkt->data_offset += ppi_size; |
| 175 | |
| 176 | ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt + |
| 177 | rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len); |
| 178 | |
| 179 | ppi->size = ppi_size; |
| 180 | ppi->type = pkt_type; |
| 181 | ppi->ppi_offset = sizeof(struct rndis_per_packet_info); |
| 182 | |
| 183 | rndis_pkt->per_pkt_info_len += ppi_size; |
| 184 | |
| 185 | return ppi; |
| 186 | } |
| 187 | |
Haiyang Zhang | f72860a | 2017-04-12 11:45:18 -0700 | [diff] [blame] | 188 | /* Azure hosts don't support non-TCP port numbers in hashing yet. We compute |
| 189 | * hash for non-TCP traffic with only IP numbers. |
| 190 | */ |
| 191 | static inline u32 netvsc_get_hash(struct sk_buff *skb, struct sock *sk) |
| 192 | { |
| 193 | struct flow_keys flow; |
| 194 | u32 hash; |
| 195 | static u32 hashrnd __read_mostly; |
| 196 | |
| 197 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 198 | |
| 199 | if (!skb_flow_dissect_flow_keys(skb, &flow, 0)) |
| 200 | return 0; |
| 201 | |
| 202 | if (flow.basic.ip_proto == IPPROTO_TCP) { |
| 203 | return skb_get_hash(skb); |
| 204 | } else { |
| 205 | if (flow.basic.n_proto == htons(ETH_P_IP)) |
| 206 | hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd); |
| 207 | else if (flow.basic.n_proto == htons(ETH_P_IPV6)) |
| 208 | hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd); |
| 209 | else |
| 210 | hash = 0; |
| 211 | |
| 212 | skb_set_hash(skb, hash, PKT_HASH_TYPE_L3); |
| 213 | } |
| 214 | |
| 215 | return hash; |
| 216 | } |
| 217 | |
Haiyang Zhang | 8db91f6 | 2017-04-12 11:35:05 -0700 | [diff] [blame] | 218 | static inline int netvsc_get_tx_queue(struct net_device *ndev, |
| 219 | struct sk_buff *skb, int old_idx) |
| 220 | { |
| 221 | const struct net_device_context *ndc = netdev_priv(ndev); |
| 222 | struct sock *sk = skb->sk; |
| 223 | int q_idx; |
| 224 | |
Haiyang Zhang | f72860a | 2017-04-12 11:45:18 -0700 | [diff] [blame] | 225 | q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) & |
Haiyang Zhang | 8db91f6 | 2017-04-12 11:35:05 -0700 | [diff] [blame] | 226 | (VRSS_SEND_TAB_SIZE - 1)]; |
| 227 | |
| 228 | /* If queue index changed record the new value */ |
| 229 | if (q_idx != old_idx && |
| 230 | sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache)) |
| 231 | sk_tx_queue_set(sk, q_idx); |
| 232 | |
| 233 | return q_idx; |
| 234 | } |
| 235 | |
stephen hemminger | d8e18ee | 2017-01-24 13:06:05 -0800 | [diff] [blame] | 236 | /* |
| 237 | * Select queue for transmit. |
| 238 | * |
| 239 | * If a valid queue has already been assigned, then use that. |
| 240 | * Otherwise compute tx queue based on hash and the send table. |
| 241 | * |
| 242 | * This is basically similar to default (__netdev_pick_tx) with the added step |
| 243 | * of using the host send_table when no other queue has been assigned. |
| 244 | * |
| 245 | * TODO support XPS - but get_xps_queue not exported |
| 246 | */ |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 247 | static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb) |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 248 | { |
Haiyang Zhang | 8db91f6 | 2017-04-12 11:35:05 -0700 | [diff] [blame] | 249 | int q_idx = sk_tx_queue_get(skb->sk); |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 250 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 251 | if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) { |
Haiyang Zhang | 8db91f6 | 2017-04-12 11:35:05 -0700 | [diff] [blame] | 252 | /* If forwarding a packet, we use the recorded queue when |
| 253 | * available for better cache locality. |
| 254 | */ |
| 255 | if (skb_rx_queue_recorded(skb)) |
| 256 | q_idx = skb_get_rx_queue(skb); |
| 257 | else |
| 258 | q_idx = netvsc_get_tx_queue(ndev, skb, q_idx); |
stephen hemminger | d8e18ee | 2017-01-24 13:06:05 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 261 | return q_idx; |
| 262 | } |
| 263 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 264 | static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb, |
| 265 | void *accel_priv, |
| 266 | select_queue_fallback_t fallback) |
| 267 | { |
| 268 | struct net_device_context *ndc = netdev_priv(ndev); |
| 269 | struct net_device *vf_netdev; |
| 270 | u16 txq; |
| 271 | |
| 272 | rcu_read_lock(); |
| 273 | vf_netdev = rcu_dereference(ndc->vf_netdev); |
| 274 | if (vf_netdev) { |
| 275 | txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; |
| 276 | qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; |
| 277 | } else { |
| 278 | txq = netvsc_pick_tx(ndev, skb); |
| 279 | } |
| 280 | rcu_read_unlock(); |
| 281 | |
| 282 | while (unlikely(txq >= ndev->real_num_tx_queues)) |
| 283 | txq -= ndev->real_num_tx_queues; |
| 284 | |
| 285 | return txq; |
| 286 | } |
| 287 | |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 288 | static u32 fill_pg_buf(struct page *page, u32 offset, u32 len, |
| 289 | struct hv_page_buffer *pb) |
| 290 | { |
| 291 | int j = 0; |
| 292 | |
| 293 | /* Deal with compund pages by ignoring unused part |
| 294 | * of the page. |
| 295 | */ |
| 296 | page += (offset >> PAGE_SHIFT); |
| 297 | offset &= ~PAGE_MASK; |
| 298 | |
| 299 | while (len > 0) { |
| 300 | unsigned long bytes; |
| 301 | |
| 302 | bytes = PAGE_SIZE - offset; |
| 303 | if (bytes > len) |
| 304 | bytes = len; |
| 305 | pb[j].pfn = page_to_pfn(page); |
| 306 | pb[j].offset = offset; |
| 307 | pb[j].len = bytes; |
| 308 | |
| 309 | offset += bytes; |
| 310 | len -= bytes; |
| 311 | |
| 312 | if (offset == PAGE_SIZE && len) { |
| 313 | page++; |
| 314 | offset = 0; |
| 315 | j++; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return j + 1; |
| 320 | } |
| 321 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 322 | 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] | 323 | struct hv_netvsc_packet *packet, |
stephen hemminger | 02b6de0 | 2017-07-28 08:59:44 -0700 | [diff] [blame] | 324 | struct hv_page_buffer *pb) |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 325 | { |
| 326 | u32 slots_used = 0; |
| 327 | char *data = skb->data; |
| 328 | int frags = skb_shinfo(skb)->nr_frags; |
| 329 | int i; |
| 330 | |
| 331 | /* The packet is laid out thus: |
Haiyang Zhang | aa0a34b | 2015-04-13 16:34:35 -0700 | [diff] [blame] | 332 | * 1. hdr: RNDIS header and PPI |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 333 | * 2. skb linear data |
| 334 | * 3. skb fragment data |
| 335 | */ |
| 336 | if (hdr != NULL) |
| 337 | slots_used += fill_pg_buf(virt_to_page(hdr), |
| 338 | offset_in_page(hdr), |
| 339 | len, &pb[slots_used]); |
| 340 | |
Haiyang Zhang | aa0a34b | 2015-04-13 16:34:35 -0700 | [diff] [blame] | 341 | packet->rmsg_size = len; |
| 342 | packet->rmsg_pgcnt = slots_used; |
| 343 | |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 344 | slots_used += fill_pg_buf(virt_to_page(data), |
| 345 | offset_in_page(data), |
| 346 | skb_headlen(skb), &pb[slots_used]); |
| 347 | |
| 348 | for (i = 0; i < frags; i++) { |
| 349 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; |
| 350 | |
| 351 | slots_used += fill_pg_buf(skb_frag_page(frag), |
| 352 | frag->page_offset, |
| 353 | skb_frag_size(frag), &pb[slots_used]); |
| 354 | } |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 355 | return slots_used; |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 356 | } |
| 357 | |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 358 | static int count_skb_frag_slots(struct sk_buff *skb) |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 359 | { |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 360 | int i, frags = skb_shinfo(skb)->nr_frags; |
| 361 | int pages = 0; |
| 362 | |
| 363 | for (i = 0; i < frags; i++) { |
| 364 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; |
| 365 | unsigned long size = skb_frag_size(frag); |
| 366 | unsigned long offset = frag->page_offset; |
| 367 | |
| 368 | /* Skip unused frames from start of page */ |
| 369 | offset &= ~PAGE_MASK; |
| 370 | pages += PFN_UP(offset + size); |
| 371 | } |
| 372 | return pages; |
| 373 | } |
| 374 | |
| 375 | static int netvsc_get_slots(struct sk_buff *skb) |
| 376 | { |
| 377 | char *data = skb->data; |
| 378 | unsigned int offset = offset_in_page(data); |
| 379 | unsigned int len = skb_headlen(skb); |
| 380 | int slots; |
| 381 | int frag_slots; |
| 382 | |
| 383 | slots = DIV_ROUND_UP(offset + len, PAGE_SIZE); |
| 384 | frag_slots = count_skb_frag_slots(skb); |
| 385 | return slots + frag_slots; |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 386 | } |
| 387 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 388 | static u32 net_checksum_info(struct sk_buff *skb) |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 389 | { |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 390 | if (skb->protocol == htons(ETH_P_IP)) { |
| 391 | struct iphdr *ip = ip_hdr(skb); |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 392 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 393 | if (ip->protocol == IPPROTO_TCP) |
| 394 | return TRANSPORT_INFO_IPV4_TCP; |
| 395 | else if (ip->protocol == IPPROTO_UDP) |
| 396 | return TRANSPORT_INFO_IPV4_UDP; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 397 | } else { |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 398 | struct ipv6hdr *ip6 = ipv6_hdr(skb); |
| 399 | |
| 400 | if (ip6->nexthdr == IPPROTO_TCP) |
| 401 | return TRANSPORT_INFO_IPV6_TCP; |
Mohammed Gamal | 37b9dfa | 2017-07-24 10:57:26 -0700 | [diff] [blame] | 402 | else if (ip6->nexthdr == IPPROTO_UDP) |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 403 | return TRANSPORT_INFO_IPV6_UDP; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 404 | } |
| 405 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 406 | return TRANSPORT_INFO_NOT_IP; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 407 | } |
| 408 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 409 | /* Send skb on the slave VF device. */ |
| 410 | static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev, |
| 411 | struct sk_buff *skb) |
| 412 | { |
| 413 | struct net_device_context *ndev_ctx = netdev_priv(net); |
| 414 | unsigned int len = skb->len; |
| 415 | int rc; |
| 416 | |
| 417 | skb->dev = vf_netdev; |
| 418 | skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping; |
| 419 | |
| 420 | rc = dev_queue_xmit(skb); |
| 421 | if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) { |
| 422 | struct netvsc_vf_pcpu_stats *pcpu_stats |
| 423 | = this_cpu_ptr(ndev_ctx->vf_stats); |
| 424 | |
| 425 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 426 | pcpu_stats->tx_packets++; |
| 427 | pcpu_stats->tx_bytes += len; |
| 428 | u64_stats_update_end(&pcpu_stats->syncp); |
| 429 | } else { |
| 430 | this_cpu_inc(ndev_ctx->vf_stats->tx_dropped); |
| 431 | } |
| 432 | |
| 433 | return rc; |
| 434 | } |
| 435 | |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 436 | 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] | 437 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 438 | struct net_device_context *net_device_ctx = netdev_priv(net); |
Vitaly Kuznetsov | 981a1bd | 2015-04-08 17:54:05 +0200 | [diff] [blame] | 439 | struct hv_netvsc_packet *packet = NULL; |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 440 | int ret; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 441 | unsigned int num_data_pgs; |
| 442 | struct rndis_message *rndis_msg; |
| 443 | struct rndis_packet *rndis_pkt; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 444 | struct net_device *vf_netdev; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 445 | u32 rndis_msg_size; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 446 | struct rndis_per_packet_info *ppi; |
Haiyang Zhang | 307f099 | 2014-05-21 12:55:39 -0700 | [diff] [blame] | 447 | u32 hash; |
stephen hemminger | 02b6de0 | 2017-07-28 08:59:44 -0700 | [diff] [blame] | 448 | struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT]; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 449 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 450 | /* if VF is present and up then redirect packets |
| 451 | * already called with rcu_read_lock_bh |
| 452 | */ |
| 453 | vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev); |
| 454 | if (vf_netdev && netif_running(vf_netdev) && |
| 455 | !netpoll_tx_running(net)) |
| 456 | return netvsc_vf_xmit(net, vf_netdev, skb); |
| 457 | |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 458 | /* We will atmost need two pages to describe the rndis |
| 459 | * header. We can only transmit MAX_PAGE_BUFFER_COUNT number |
Vitaly Kuznetsov | e88f7e0 | 2015-04-08 17:54:06 +0200 | [diff] [blame] | 460 | * of pages in a single packet. If skb is scattered around |
| 461 | * more pages we try linearizing it. |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 462 | */ |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 463 | |
| 464 | num_data_pgs = netvsc_get_slots(skb) + 2; |
| 465 | |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 466 | if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) { |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 467 | ++net_device_ctx->eth_stats.tx_scattered; |
| 468 | |
| 469 | if (skb_linearize(skb)) |
| 470 | goto no_memory; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 471 | |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 472 | num_data_pgs = netvsc_get_slots(skb) + 2; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 473 | if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) { |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 474 | ++net_device_ctx->eth_stats.tx_too_big; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 475 | goto drop; |
| 476 | } |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 477 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 478 | |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 479 | /* |
| 480 | * Place the rndis header in the skb head room and |
| 481 | * the skb->cb will be used for hv_netvsc_packet |
| 482 | * structure. |
| 483 | */ |
| 484 | ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE); |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 485 | if (ret) |
| 486 | goto no_memory; |
| 487 | |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 488 | /* Use the skb control buffer for building up the packet */ |
| 489 | BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) > |
| 490 | FIELD_SIZEOF(struct sk_buff, cb)); |
| 491 | packet = (struct hv_netvsc_packet *)skb->cb; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 492 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 493 | packet->q_idx = skb_get_queue_mapping(skb); |
| 494 | |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 495 | packet->total_data_buflen = skb->len; |
stephen hemminger | 793e395 | 2017-01-24 13:06:12 -0800 | [diff] [blame] | 496 | packet->total_bytes = skb->len; |
| 497 | packet->total_packets = 1; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 498 | |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 499 | rndis_msg = (struct rndis_message *)skb->head; |
KY Srinivasan | b08cc79 | 2015-03-29 21:08:42 -0700 | [diff] [blame] | 500 | |
KY Srinivasan | 2447676 | 2015-12-01 16:43:06 -0800 | [diff] [blame] | 501 | memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 502 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 503 | /* Add the rndis header */ |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 504 | rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET; |
| 505 | rndis_msg->msg_len = packet->total_data_buflen; |
| 506 | rndis_pkt = &rndis_msg->msg.pkt; |
| 507 | rndis_pkt->data_offset = sizeof(struct rndis_packet); |
| 508 | rndis_pkt->data_len = packet->total_data_buflen; |
| 509 | rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet); |
| 510 | |
| 511 | rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet); |
| 512 | |
Haiyang Zhang | 307f099 | 2014-05-21 12:55:39 -0700 | [diff] [blame] | 513 | hash = skb_get_hash_raw(skb); |
| 514 | if (hash != 0 && net->real_num_tx_queues > 1) { |
| 515 | rndis_msg_size += NDIS_HASH_PPI_SIZE; |
| 516 | ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE, |
| 517 | NBL_HASH_VALUE); |
| 518 | *(u32 *)((void *)ppi + ppi->ppi_offset) = hash; |
| 519 | } |
| 520 | |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 521 | if (skb_vlan_tag_present(skb)) { |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 522 | struct ndis_pkt_8021q_info *vlan; |
| 523 | |
| 524 | rndis_msg_size += NDIS_VLAN_PPI_SIZE; |
| 525 | ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE, |
| 526 | IEEE_8021Q_INFO); |
| 527 | vlan = (struct ndis_pkt_8021q_info *)((void *)ppi + |
| 528 | ppi->ppi_offset); |
KY Srinivasan | 760d1e3 | 2015-12-01 16:43:19 -0800 | [diff] [blame] | 529 | vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK; |
| 530 | vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >> |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 531 | VLAN_PRIO_SHIFT; |
| 532 | } |
| 533 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 534 | if (skb_is_gso(skb)) { |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 535 | struct ndis_tcp_lso_info *lso_info; |
| 536 | |
| 537 | rndis_msg_size += NDIS_LSO_PPI_SIZE; |
| 538 | ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE, |
| 539 | TCP_LARGESEND_PKTINFO); |
| 540 | |
| 541 | lso_info = (struct ndis_tcp_lso_info *)((void *)ppi + |
| 542 | ppi->ppi_offset); |
| 543 | |
| 544 | 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] | 545 | if (skb->protocol == htons(ETH_P_IP)) { |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 546 | lso_info->lso_v2_transmit.ip_version = |
| 547 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4; |
| 548 | ip_hdr(skb)->tot_len = 0; |
| 549 | ip_hdr(skb)->check = 0; |
| 550 | tcp_hdr(skb)->check = |
| 551 | ~csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 552 | ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); |
| 553 | } else { |
| 554 | lso_info->lso_v2_transmit.ip_version = |
| 555 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6; |
| 556 | ipv6_hdr(skb)->payload_len = 0; |
| 557 | tcp_hdr(skb)->check = |
| 558 | ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 559 | &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); |
| 560 | } |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 561 | lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb); |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 562 | lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size; |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 563 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 564 | if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) { |
| 565 | struct ndis_tcp_ip_checksum_info *csum_info; |
| 566 | |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 567 | rndis_msg_size += NDIS_CSUM_PPI_SIZE; |
| 568 | ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE, |
| 569 | TCPIP_CHKSUM_PKTINFO); |
| 570 | |
| 571 | csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi + |
| 572 | ppi->ppi_offset); |
| 573 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 574 | csum_info->transmit.tcp_header_offset = skb_transport_offset(skb); |
| 575 | |
| 576 | if (skb->protocol == htons(ETH_P_IP)) { |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 577 | csum_info->transmit.is_ipv4 = 1; |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 578 | |
| 579 | if (ip_hdr(skb)->protocol == IPPROTO_TCP) |
| 580 | csum_info->transmit.tcp_checksum = 1; |
| 581 | else |
| 582 | csum_info->transmit.udp_checksum = 1; |
| 583 | } else { |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 584 | csum_info->transmit.is_ipv6 = 1; |
| 585 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 586 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) |
| 587 | csum_info->transmit.tcp_checksum = 1; |
| 588 | else |
| 589 | csum_info->transmit.udp_checksum = 1; |
| 590 | } |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 591 | } else { |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 592 | /* Can't do offload of this type of checksum */ |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 593 | if (skb_checksum_help(skb)) |
| 594 | goto drop; |
| 595 | } |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 596 | } |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 597 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 598 | /* Start filling in the page buffers with the rndis hdr */ |
| 599 | rndis_msg->msg_len += rndis_msg_size; |
Haiyang Zhang | 942396b | 2014-10-22 13:47:18 -0700 | [diff] [blame] | 600 | packet->total_data_buflen = rndis_msg->msg_len; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 601 | packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size, |
stephen hemminger | 02b6de0 | 2017-07-28 08:59:44 -0700 | [diff] [blame] | 602 | skb, packet, pb); |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 603 | |
sixiao@microsoft.com | 76d13b5 | 2016-02-17 16:43:59 -0800 | [diff] [blame] | 604 | /* timestamp packet in software */ |
| 605 | skb_tx_timestamp(skb); |
stephen hemminger | 2a926f7 | 2017-07-19 11:53:17 -0700 | [diff] [blame] | 606 | |
stephen hemminger | 02b6de0 | 2017-07-28 08:59:44 -0700 | [diff] [blame] | 607 | ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb); |
stephen hemminger | 793e395 | 2017-01-24 13:06:12 -0800 | [diff] [blame] | 608 | if (likely(ret == 0)) |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 609 | return NETDEV_TX_OK; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 610 | |
| 611 | if (ret == -EAGAIN) { |
| 612 | ++net_device_ctx->eth_stats.tx_busy; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 613 | return NETDEV_TX_BUSY; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | if (ret == -ENOSPC) |
| 617 | ++net_device_ctx->eth_stats.tx_no_space; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 618 | |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 619 | drop: |
| 620 | dev_kfree_skb_any(skb); |
| 621 | net->stats.tx_dropped++; |
| 622 | |
| 623 | return NETDEV_TX_OK; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 624 | |
| 625 | no_memory: |
| 626 | ++net_device_ctx->eth_stats.tx_no_memory; |
| 627 | goto drop; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 628 | } |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 629 | /* |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 630 | * netvsc_linkstatus_callback - Link up/down notification |
| 631 | */ |
K. Y. Srinivasan | 90ef117 | 2011-05-12 19:34:50 -0700 | [diff] [blame] | 632 | void netvsc_linkstatus_callback(struct hv_device *device_obj, |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 633 | struct rndis_message *resp) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 634 | { |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 635 | struct rndis_indicate_status *indicate = &resp->msg.indicate_status; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 636 | struct net_device *net; |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 637 | struct net_device_context *ndev_ctx; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 638 | struct netvsc_reconfig *event; |
| 639 | unsigned long flags; |
| 640 | |
Haiyang Zhang | 7f5d5af | 2016-08-04 10:42:15 -0700 | [diff] [blame] | 641 | net = hv_get_drvdata(device_obj); |
| 642 | |
| 643 | if (!net) |
| 644 | return; |
| 645 | |
| 646 | ndev_ctx = netdev_priv(net); |
| 647 | |
| 648 | /* Update the physical link speed when changing to another vSwitch */ |
| 649 | if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) { |
| 650 | u32 speed; |
| 651 | |
| 652 | speed = *(u32 *)((void *)indicate + indicate-> |
| 653 | status_buf_offset) / 10000; |
| 654 | ndev_ctx->speed = speed; |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | /* Handle these link change statuses below */ |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 659 | if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE && |
| 660 | indicate->status != RNDIS_STATUS_MEDIA_CONNECT && |
| 661 | indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT) |
| 662 | return; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 663 | |
Haiyang Zhang | 7f5d5af | 2016-08-04 10:42:15 -0700 | [diff] [blame] | 664 | if (net->reg_state != NETREG_REGISTERED) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 665 | return; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 666 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 667 | event = kzalloc(sizeof(*event), GFP_ATOMIC); |
| 668 | if (!event) |
| 669 | return; |
| 670 | event->event = indicate->status; |
| 671 | |
| 672 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
| 673 | list_add_tail(&event->list, &ndev_ctx->reconfig_events); |
| 674 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 675 | |
| 676 | schedule_delayed_work(&ndev_ctx->dwork, 0); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 677 | } |
| 678 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 679 | static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net, |
stephen hemminger | e91e7dd | 2017-02-27 10:26:51 -0800 | [diff] [blame] | 680 | struct napi_struct *napi, |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 681 | const struct ndis_tcp_ip_checksum_info *csum_info, |
| 682 | const struct ndis_pkt_8021q_info *vlan, |
| 683 | void *data, u32 buflen) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 684 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 685 | struct sk_buff *skb; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 686 | |
stephen hemminger | e91e7dd | 2017-02-27 10:26:51 -0800 | [diff] [blame] | 687 | skb = napi_alloc_skb(napi, buflen); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 688 | if (!skb) |
| 689 | return skb; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 690 | |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 691 | /* |
| 692 | * Copy to skb. This copy is needed here since the memory pointed by |
| 693 | * hv_netvsc_packet cannot be deallocated |
| 694 | */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 695 | skb_put_data(skb, data, buflen); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 696 | |
| 697 | skb->protocol = eth_type_trans(skb, net); |
Stephen Hemminger | e52fed7 | 2016-10-23 21:32:47 -0700 | [diff] [blame] | 698 | |
| 699 | /* skb is already created with CHECKSUM_NONE */ |
| 700 | skb_checksum_none_assert(skb); |
| 701 | |
| 702 | /* |
| 703 | * In Linux, the IP checksum is always checked. |
| 704 | * Do L4 checksum offload if enabled and present. |
| 705 | */ |
| 706 | if (csum_info && (net->features & NETIF_F_RXCSUM)) { |
| 707 | if (csum_info->receive.tcp_checksum_succeeded || |
| 708 | csum_info->receive.udp_checksum_succeeded) |
KY Srinivasan | e3d605e | 2014-03-08 19:23:16 -0800 | [diff] [blame] | 709 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
KY Srinivasan | e3d605e | 2014-03-08 19:23:16 -0800 | [diff] [blame] | 710 | } |
| 711 | |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 712 | if (vlan) { |
| 713 | u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT); |
| 714 | |
Haiyang Zhang | 93725cb | 2013-06-17 15:36:49 -0700 | [diff] [blame] | 715 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
KY Srinivasan | 760d1e3 | 2015-12-01 16:43:19 -0800 | [diff] [blame] | 716 | vlan_tci); |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 717 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 718 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 719 | return skb; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * netvsc_recv_callback - Callback when we receive a packet from the |
| 724 | * "wire" on the specified device. |
| 725 | */ |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 726 | int netvsc_recv_callback(struct net_device *net, |
| 727 | struct vmbus_channel *channel, |
| 728 | void *data, u32 len, |
| 729 | const struct ndis_tcp_ip_checksum_info *csum_info, |
| 730 | const struct ndis_pkt_8021q_info *vlan) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 731 | { |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 732 | struct net_device_context *net_device_ctx = netdev_priv(net); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 733 | struct netvsc_device *net_device; |
stephen hemminger | 742fe54 | 2017-02-27 10:26:50 -0800 | [diff] [blame] | 734 | u16 q_idx = channel->offermsg.offer.sub_channel_index; |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 735 | struct netvsc_channel *nvchan; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 736 | struct sk_buff *skb; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 737 | struct netvsc_stats *rx_stats; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 738 | |
Stephen Hemminger | 9cbcc42 | 2016-09-22 16:56:34 -0700 | [diff] [blame] | 739 | if (net->reg_state != NETREG_REGISTERED) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 740 | return NVSP_STAT_FAIL; |
| 741 | |
stephen hemminger | 0719e72 | 2017-01-11 09:16:32 -0800 | [diff] [blame] | 742 | rcu_read_lock(); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 743 | net_device = rcu_dereference(net_device_ctx->nvdev); |
| 744 | if (unlikely(!net_device)) |
| 745 | goto drop; |
| 746 | |
| 747 | nvchan = &net_device->chan_table[q_idx]; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 748 | |
| 749 | /* Allocate a skb - TODO direct I/O to pages? */ |
stephen hemminger | e91e7dd | 2017-02-27 10:26:51 -0800 | [diff] [blame] | 750 | skb = netvsc_alloc_recv_skb(net, &nvchan->napi, |
| 751 | csum_info, vlan, data, len); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 752 | if (unlikely(!skb)) { |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 753 | drop: |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 754 | ++net->stats.rx_dropped; |
stephen hemminger | 0719e72 | 2017-01-11 09:16:32 -0800 | [diff] [blame] | 755 | rcu_read_unlock(); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 756 | return NVSP_STAT_FAIL; |
| 757 | } |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 758 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 759 | skb_record_rx_queue(skb, q_idx); |
Stephen Hemminger | 9cbcc42 | 2016-09-22 16:56:34 -0700 | [diff] [blame] | 760 | |
| 761 | /* |
| 762 | * Even if injecting the packet, record the statistics |
| 763 | * on the synthetic device because modifying the VF device |
| 764 | * statistics will not work correctly. |
| 765 | */ |
stephen hemminger | 742fe54 | 2017-02-27 10:26:50 -0800 | [diff] [blame] | 766 | rx_stats = &nvchan->rx_stats; |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 767 | u64_stats_update_begin(&rx_stats->syncp); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 768 | rx_stats->packets++; |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 769 | rx_stats->bytes += len; |
Stephen Hemminger | f7ad75b | 2016-09-22 16:56:35 -0700 | [diff] [blame] | 770 | |
| 771 | if (skb->pkt_type == PACKET_BROADCAST) |
| 772 | ++rx_stats->broadcast; |
| 773 | else if (skb->pkt_type == PACKET_MULTICAST) |
| 774 | ++rx_stats->multicast; |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 775 | u64_stats_update_end(&rx_stats->syncp); |
Stephen Hemminger | 9495c28 | 2010-03-09 17:42:17 -0800 | [diff] [blame] | 776 | |
stephen hemminger | 742fe54 | 2017-02-27 10:26:50 -0800 | [diff] [blame] | 777 | napi_gro_receive(&nvchan->napi, skb); |
stephen hemminger | 0719e72 | 2017-01-11 09:16:32 -0800 | [diff] [blame] | 778 | rcu_read_unlock(); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 779 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 783 | static void netvsc_get_drvinfo(struct net_device *net, |
| 784 | struct ethtool_drvinfo *info) |
| 785 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 786 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 787 | strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 790 | static void netvsc_get_channels(struct net_device *net, |
| 791 | struct ethtool_channels *channel) |
| 792 | { |
| 793 | struct net_device_context *net_device_ctx = netdev_priv(net); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 794 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 795 | |
| 796 | if (nvdev) { |
| 797 | channel->max_combined = nvdev->max_chn; |
| 798 | channel->combined_count = nvdev->num_chn; |
| 799 | } |
| 800 | } |
| 801 | |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 802 | static int netvsc_set_channels(struct net_device *net, |
| 803 | struct ethtool_channels *channels) |
| 804 | { |
| 805 | struct net_device_context *net_device_ctx = netdev_priv(net); |
| 806 | struct hv_device *dev = net_device_ctx->device_ctx; |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 807 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 808 | unsigned int orig, count = channels->combined_count; |
| 809 | struct netvsc_device_info device_info; |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 810 | bool was_opened; |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 811 | int ret = 0; |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 812 | |
| 813 | /* We do not support separate count for rx, tx, or other */ |
| 814 | if (count == 0 || |
| 815 | channels->rx_count || channels->tx_count || channels->other_count) |
| 816 | return -EINVAL; |
| 817 | |
Arnd Bergmann | b92b7d3 | 2017-06-22 00:16:37 +0200 | [diff] [blame] | 818 | if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX) |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 819 | return -EINVAL; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 820 | |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 821 | if (!nvdev || nvdev->destroy) |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 822 | return -ENODEV; |
| 823 | |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 824 | if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 825 | return -EINVAL; |
| 826 | |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 827 | if (count > nvdev->max_chn) |
| 828 | return -EINVAL; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 829 | |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 830 | orig = nvdev->num_chn; |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 831 | was_opened = rndis_filter_opened(nvdev); |
| 832 | if (was_opened) |
| 833 | rndis_filter_close(nvdev); |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 834 | |
stephen hemminger | 2289f0a | 2017-01-24 13:06:10 -0800 | [diff] [blame] | 835 | rndis_filter_device_remove(dev, nvdev); |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 836 | |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 837 | memset(&device_info, 0, sizeof(device_info)); |
| 838 | device_info.num_chn = count; |
| 839 | device_info.ring_size = ring_size; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 840 | |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 841 | nvdev = rndis_filter_device_add(dev, &device_info); |
| 842 | if (!IS_ERR(nvdev)) { |
| 843 | netif_set_real_num_tx_queues(net, nvdev->num_chn); |
| 844 | netif_set_real_num_rx_queues(net, nvdev->num_chn); |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 845 | } else { |
stephen hemminger | d6aac1f | 2017-07-28 08:59:41 -0700 | [diff] [blame] | 846 | ret = PTR_ERR(nvdev); |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 847 | device_info.num_chn = orig; |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 848 | rndis_filter_device_add(dev, &device_info); |
| 849 | } |
| 850 | |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 851 | if (was_opened) |
| 852 | rndis_filter_open(nvdev); |
stephen hemminger | 163891d | 2017-03-22 14:50:58 -0700 | [diff] [blame] | 853 | |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 854 | /* We may have missed link change notifications */ |
stephen hemminger | 1b01994 | 2017-07-19 11:53:12 -0700 | [diff] [blame] | 855 | net_device_ctx->last_reconfig = 0; |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 856 | schedule_delayed_work(&net_device_ctx->dwork, 0); |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 857 | |
| 858 | return ret; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 861 | static bool |
| 862 | netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd) |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 863 | { |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 864 | struct ethtool_link_ksettings diff1 = *cmd; |
| 865 | struct ethtool_link_ksettings diff2 = {}; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 866 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 867 | diff1.base.speed = 0; |
| 868 | diff1.base.duplex = 0; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 869 | /* advertising and cmd are usually set */ |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 870 | ethtool_link_ksettings_zero_link_mode(&diff1, advertising); |
| 871 | diff1.base.cmd = 0; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 872 | /* We set port to PORT_OTHER */ |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 873 | diff2.base.port = PORT_OTHER; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 874 | |
| 875 | return !memcmp(&diff1, &diff2, sizeof(diff1)); |
| 876 | } |
| 877 | |
| 878 | static void netvsc_init_settings(struct net_device *dev) |
| 879 | { |
| 880 | struct net_device_context *ndc = netdev_priv(dev); |
| 881 | |
| 882 | ndc->speed = SPEED_UNKNOWN; |
Simon Xiao | f3c9d40e | 2017-04-14 14:42:58 -0700 | [diff] [blame] | 883 | ndc->duplex = DUPLEX_FULL; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 886 | static int netvsc_get_link_ksettings(struct net_device *dev, |
| 887 | struct ethtool_link_ksettings *cmd) |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 888 | { |
| 889 | struct net_device_context *ndc = netdev_priv(dev); |
| 890 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 891 | cmd->base.speed = ndc->speed; |
| 892 | cmd->base.duplex = ndc->duplex; |
| 893 | cmd->base.port = PORT_OTHER; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 894 | |
| 895 | return 0; |
| 896 | } |
| 897 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 898 | static int netvsc_set_link_ksettings(struct net_device *dev, |
| 899 | const struct ethtool_link_ksettings *cmd) |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 900 | { |
| 901 | struct net_device_context *ndc = netdev_priv(dev); |
| 902 | u32 speed; |
| 903 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 904 | speed = cmd->base.speed; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 905 | if (!ethtool_validate_speed(speed) || |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 906 | !ethtool_validate_duplex(cmd->base.duplex) || |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 907 | !netvsc_validate_ethtool_ss_cmd(cmd)) |
| 908 | return -EINVAL; |
| 909 | |
| 910 | ndc->speed = speed; |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 911 | ndc->duplex = cmd->base.duplex; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 916 | static int netvsc_change_mtu(struct net_device *ndev, int mtu) |
| 917 | { |
| 918 | struct net_device_context *ndevctx = netdev_priv(ndev); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 919 | struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 920 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 921 | struct hv_device *hdev = ndevctx->device_ctx; |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 922 | int orig_mtu = ndev->mtu; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 923 | struct netvsc_device_info device_info; |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 924 | bool was_opened; |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 925 | int ret = 0; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 926 | |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 927 | if (!nvdev || nvdev->destroy) |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 928 | return -ENODEV; |
| 929 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 930 | /* Change MTU of underlying VF netdev first. */ |
| 931 | if (vf_netdev) { |
| 932 | ret = dev_set_mtu(vf_netdev, mtu); |
| 933 | if (ret) |
| 934 | return ret; |
| 935 | } |
| 936 | |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 937 | netif_device_detach(ndev); |
| 938 | was_opened = rndis_filter_opened(nvdev); |
| 939 | if (was_opened) |
| 940 | rndis_filter_close(nvdev); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 941 | |
Andrew Schwartzmeyer | 8ebdcc5 | 2015-08-11 17:14:31 -0700 | [diff] [blame] | 942 | memset(&device_info, 0, sizeof(device_info)); |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 943 | device_info.ring_size = ring_size; |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 944 | device_info.num_chn = nvdev->num_chn; |
Dexuan Cui | 152669b | 2017-03-02 13:00:53 +0000 | [diff] [blame] | 945 | |
Dexuan Cui | 152669b | 2017-03-02 13:00:53 +0000 | [diff] [blame] | 946 | rndis_filter_device_remove(hdev, nvdev); |
| 947 | |
Dexuan Cui | 152669b | 2017-03-02 13:00:53 +0000 | [diff] [blame] | 948 | ndev->mtu = mtu; |
| 949 | |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 950 | nvdev = rndis_filter_device_add(hdev, &device_info); |
| 951 | if (IS_ERR(nvdev)) { |
| 952 | ret = PTR_ERR(nvdev); |
| 953 | |
| 954 | /* Attempt rollback to original MTU */ |
| 955 | ndev->mtu = orig_mtu; |
| 956 | rndis_filter_device_add(hdev, &device_info); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 957 | |
| 958 | if (vf_netdev) |
| 959 | dev_set_mtu(vf_netdev, orig_mtu); |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 960 | } |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 961 | |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 962 | if (was_opened) |
| 963 | rndis_filter_open(nvdev); |
| 964 | |
| 965 | netif_device_attach(ndev); |
stephen hemminger | 163891d | 2017-03-22 14:50:58 -0700 | [diff] [blame] | 966 | |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 967 | /* We may have missed link change notifications */ |
| 968 | schedule_delayed_work(&ndevctx->dwork, 0); |
| 969 | |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 970 | return ret; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 971 | } |
| 972 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 973 | static void netvsc_get_vf_stats(struct net_device *net, |
| 974 | struct netvsc_vf_pcpu_stats *tot) |
| 975 | { |
| 976 | struct net_device_context *ndev_ctx = netdev_priv(net); |
| 977 | int i; |
| 978 | |
| 979 | memset(tot, 0, sizeof(*tot)); |
| 980 | |
| 981 | for_each_possible_cpu(i) { |
| 982 | const struct netvsc_vf_pcpu_stats *stats |
| 983 | = per_cpu_ptr(ndev_ctx->vf_stats, i); |
| 984 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
| 985 | unsigned int start; |
| 986 | |
| 987 | do { |
| 988 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
| 989 | rx_packets = stats->rx_packets; |
| 990 | tx_packets = stats->tx_packets; |
| 991 | rx_bytes = stats->rx_bytes; |
| 992 | tx_bytes = stats->tx_bytes; |
| 993 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
| 994 | |
| 995 | tot->rx_packets += rx_packets; |
| 996 | tot->tx_packets += tx_packets; |
| 997 | tot->rx_bytes += rx_bytes; |
| 998 | tot->tx_bytes += tx_bytes; |
| 999 | tot->tx_dropped += stats->tx_dropped; |
| 1000 | } |
| 1001 | } |
| 1002 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 1003 | static void netvsc_get_stats64(struct net_device *net, |
| 1004 | struct rtnl_link_stats64 *t) |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1005 | { |
| 1006 | struct net_device_context *ndev_ctx = netdev_priv(net); |
stephen hemminger | 776e726 | 2017-04-14 14:42:57 -0700 | [diff] [blame] | 1007 | struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1008 | struct netvsc_vf_pcpu_stats vf_tot; |
| 1009 | int i; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1010 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1011 | if (!nvdev) |
| 1012 | return; |
| 1013 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1014 | netdev_stats_to_stats64(t, &net->stats); |
| 1015 | |
| 1016 | netvsc_get_vf_stats(net, &vf_tot); |
| 1017 | t->rx_packets += vf_tot.rx_packets; |
| 1018 | t->tx_packets += vf_tot.tx_packets; |
| 1019 | t->rx_bytes += vf_tot.rx_bytes; |
| 1020 | t->tx_bytes += vf_tot.tx_bytes; |
| 1021 | t->tx_dropped += vf_tot.tx_dropped; |
| 1022 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1023 | for (i = 0; i < nvdev->num_chn; i++) { |
| 1024 | const struct netvsc_channel *nvchan = &nvdev->chan_table[i]; |
| 1025 | const struct netvsc_stats *stats; |
| 1026 | u64 packets, bytes, multicast; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1027 | unsigned int start; |
| 1028 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1029 | stats = &nvchan->tx_stats; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1030 | do { |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1031 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
| 1032 | packets = stats->packets; |
| 1033 | bytes = stats->bytes; |
| 1034 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1035 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1036 | t->tx_bytes += bytes; |
| 1037 | t->tx_packets += packets; |
| 1038 | |
| 1039 | stats = &nvchan->rx_stats; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1040 | do { |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1041 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
| 1042 | packets = stats->packets; |
| 1043 | bytes = stats->bytes; |
| 1044 | multicast = stats->multicast + stats->broadcast; |
| 1045 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1046 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1047 | t->rx_bytes += bytes; |
| 1048 | t->rx_packets += packets; |
| 1049 | t->multicast += multicast; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1050 | } |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1051 | } |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1052 | |
| 1053 | static int netvsc_set_mac_addr(struct net_device *ndev, void *p) |
| 1054 | { |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1055 | struct net_device_context *ndc = netdev_priv(ndev); |
stephen hemminger | 16ba326 | 2017-08-09 17:46:05 -0700 | [diff] [blame^] | 1056 | struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1057 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1058 | struct sockaddr *addr = p; |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1059 | int err; |
| 1060 | |
stephen hemminger | 16ba326 | 2017-08-09 17:46:05 -0700 | [diff] [blame^] | 1061 | err = eth_prepare_mac_addr_change(ndev, p); |
| 1062 | if (err) |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1063 | return err; |
| 1064 | |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1065 | if (!nvdev) |
| 1066 | return -ENODEV; |
| 1067 | |
stephen hemminger | 16ba326 | 2017-08-09 17:46:05 -0700 | [diff] [blame^] | 1068 | if (vf_netdev) { |
| 1069 | err = dev_set_mac_address(vf_netdev, addr); |
| 1070 | if (err) |
| 1071 | return err; |
| 1072 | } |
| 1073 | |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1074 | err = rndis_filter_set_device_mac(nvdev, addr->sa_data); |
stephen hemminger | 16ba326 | 2017-08-09 17:46:05 -0700 | [diff] [blame^] | 1075 | if (!err) { |
| 1076 | eth_commit_mac_addr_change(ndev, p); |
| 1077 | } else if (vf_netdev) { |
| 1078 | /* rollback change on VF */ |
| 1079 | memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN); |
| 1080 | dev_set_mac_address(vf_netdev, addr); |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | return err; |
| 1084 | } |
| 1085 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1086 | static const struct { |
| 1087 | char name[ETH_GSTRING_LEN]; |
| 1088 | u16 offset; |
| 1089 | } netvsc_stats[] = { |
| 1090 | { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) }, |
| 1091 | { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) }, |
| 1092 | { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) }, |
| 1093 | { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) }, |
| 1094 | { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) }, |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1095 | }, vf_stats[] = { |
| 1096 | { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) }, |
| 1097 | { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) }, |
| 1098 | { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) }, |
| 1099 | { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) }, |
| 1100 | { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) }, |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1101 | }; |
| 1102 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1103 | #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats) |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1104 | #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats) |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1105 | |
| 1106 | /* 4 statistics per queue (rx/tx packets/bytes) */ |
| 1107 | #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4) |
| 1108 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1109 | static int netvsc_get_sset_count(struct net_device *dev, int string_set) |
| 1110 | { |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1111 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | fbd4c7e | 2017-06-07 15:53:47 -0700 | [diff] [blame] | 1112 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1113 | |
| 1114 | if (!nvdev) |
| 1115 | return -ENODEV; |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1116 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1117 | switch (string_set) { |
| 1118 | case ETH_SS_STATS: |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1119 | return NETVSC_GLOBAL_STATS_LEN |
| 1120 | + NETVSC_VF_STATS_LEN |
| 1121 | + NETVSC_QUEUE_STATS_LEN(nvdev); |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1122 | default: |
| 1123 | return -EINVAL; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | static void netvsc_get_ethtool_stats(struct net_device *dev, |
| 1128 | struct ethtool_stats *stats, u64 *data) |
| 1129 | { |
| 1130 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1131 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1132 | const void *nds = &ndc->eth_stats; |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1133 | const struct netvsc_stats *qstats; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1134 | struct netvsc_vf_pcpu_stats sum; |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1135 | unsigned int start; |
| 1136 | u64 packets, bytes; |
| 1137 | int i, j; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1138 | |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1139 | if (!nvdev) |
| 1140 | return; |
| 1141 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1142 | for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++) |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1143 | data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset); |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1144 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1145 | netvsc_get_vf_stats(dev, &sum); |
| 1146 | for (j = 0; j < NETVSC_VF_STATS_LEN; j++) |
| 1147 | data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset); |
| 1148 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1149 | for (j = 0; j < nvdev->num_chn; j++) { |
| 1150 | qstats = &nvdev->chan_table[j].tx_stats; |
| 1151 | |
| 1152 | do { |
| 1153 | start = u64_stats_fetch_begin_irq(&qstats->syncp); |
| 1154 | packets = qstats->packets; |
| 1155 | bytes = qstats->bytes; |
| 1156 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); |
| 1157 | data[i++] = packets; |
| 1158 | data[i++] = bytes; |
| 1159 | |
| 1160 | qstats = &nvdev->chan_table[j].rx_stats; |
| 1161 | do { |
| 1162 | start = u64_stats_fetch_begin_irq(&qstats->syncp); |
| 1163 | packets = qstats->packets; |
| 1164 | bytes = qstats->bytes; |
| 1165 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); |
| 1166 | data[i++] = packets; |
| 1167 | data[i++] = bytes; |
| 1168 | } |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data) |
| 1172 | { |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1173 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1174 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1175 | u8 *p = data; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1176 | int i; |
| 1177 | |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1178 | if (!nvdev) |
| 1179 | return; |
| 1180 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1181 | switch (stringset) { |
| 1182 | case ETH_SS_STATS: |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1183 | for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) { |
| 1184 | memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN); |
| 1185 | p += ETH_GSTRING_LEN; |
| 1186 | } |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1187 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1188 | for (i = 0; i < ARRAY_SIZE(vf_stats); i++) { |
| 1189 | memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN); |
| 1190 | p += ETH_GSTRING_LEN; |
| 1191 | } |
| 1192 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1193 | for (i = 0; i < nvdev->num_chn; i++) { |
| 1194 | sprintf(p, "tx_queue_%u_packets", i); |
| 1195 | p += ETH_GSTRING_LEN; |
| 1196 | sprintf(p, "tx_queue_%u_bytes", i); |
| 1197 | p += ETH_GSTRING_LEN; |
| 1198 | sprintf(p, "rx_queue_%u_packets", i); |
| 1199 | p += ETH_GSTRING_LEN; |
| 1200 | sprintf(p, "rx_queue_%u_bytes", i); |
| 1201 | p += ETH_GSTRING_LEN; |
| 1202 | } |
| 1203 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1204 | break; |
| 1205 | } |
| 1206 | } |
| 1207 | |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1208 | static int |
stephen hemminger | b5a5dc8 | 2017-01-24 13:06:01 -0800 | [diff] [blame] | 1209 | netvsc_get_rss_hash_opts(struct netvsc_device *nvdev, |
| 1210 | struct ethtool_rxnfc *info) |
| 1211 | { |
| 1212 | info->data = RXH_IP_SRC | RXH_IP_DST; |
| 1213 | |
| 1214 | switch (info->flow_type) { |
| 1215 | case TCP_V4_FLOW: |
| 1216 | case TCP_V6_FLOW: |
| 1217 | info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; |
| 1218 | /* fallthrough */ |
| 1219 | case UDP_V4_FLOW: |
| 1220 | case UDP_V6_FLOW: |
| 1221 | case IPV4_FLOW: |
| 1222 | case IPV6_FLOW: |
| 1223 | break; |
| 1224 | default: |
| 1225 | info->data = 0; |
| 1226 | break; |
| 1227 | } |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static int |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1233 | netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, |
| 1234 | u32 *rules) |
| 1235 | { |
| 1236 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1237 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1238 | |
| 1239 | if (!nvdev) |
| 1240 | return -ENODEV; |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1241 | |
| 1242 | switch (info->cmd) { |
| 1243 | case ETHTOOL_GRXRINGS: |
| 1244 | info->data = nvdev->num_chn; |
| 1245 | return 0; |
stephen hemminger | b5a5dc8 | 2017-01-24 13:06:01 -0800 | [diff] [blame] | 1246 | |
| 1247 | case ETHTOOL_GRXFH: |
| 1248 | return netvsc_get_rss_hash_opts(nvdev, info); |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1249 | } |
| 1250 | return -EOPNOTSUPP; |
| 1251 | } |
| 1252 | |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1253 | #ifdef CONFIG_NET_POLL_CONTROLLER |
stephen hemminger | a5ecd43 | 2017-06-07 15:53:48 -0700 | [diff] [blame] | 1254 | static void netvsc_poll_controller(struct net_device *dev) |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1255 | { |
stephen hemminger | a5ecd43 | 2017-06-07 15:53:48 -0700 | [diff] [blame] | 1256 | struct net_device_context *ndc = netdev_priv(dev); |
| 1257 | struct netvsc_device *ndev; |
| 1258 | int i; |
| 1259 | |
| 1260 | rcu_read_lock(); |
| 1261 | ndev = rcu_dereference(ndc->nvdev); |
| 1262 | if (ndev) { |
| 1263 | for (i = 0; i < ndev->num_chn; i++) { |
| 1264 | struct netvsc_channel *nvchan = &ndev->chan_table[i]; |
| 1265 | |
| 1266 | napi_schedule(&nvchan->napi); |
| 1267 | } |
| 1268 | } |
| 1269 | rcu_read_unlock(); |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1270 | } |
| 1271 | #endif |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1272 | |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1273 | static u32 netvsc_get_rxfh_key_size(struct net_device *dev) |
| 1274 | { |
| 1275 | return NETVSC_HASH_KEYLEN; |
| 1276 | } |
| 1277 | |
| 1278 | static u32 netvsc_rss_indir_size(struct net_device *dev) |
| 1279 | { |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1280 | return ITAB_NUM; |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, |
| 1284 | u8 *hfunc) |
| 1285 | { |
| 1286 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1287 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
Colin Ian King | eb996ed | 2017-03-25 14:26:39 +0000 | [diff] [blame] | 1288 | struct rndis_device *rndis_dev; |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1289 | int i; |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1290 | |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1291 | if (!ndev) |
| 1292 | return -ENODEV; |
| 1293 | |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1294 | if (hfunc) |
| 1295 | *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ |
| 1296 | |
Colin Ian King | eb996ed | 2017-03-25 14:26:39 +0000 | [diff] [blame] | 1297 | rndis_dev = ndev->extension; |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1298 | if (indir) { |
| 1299 | for (i = 0; i < ITAB_NUM; i++) |
| 1300 | indir[i] = rndis_dev->ind_table[i]; |
| 1301 | } |
| 1302 | |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1303 | if (key) |
| 1304 | memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN); |
| 1305 | |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, |
| 1310 | const u8 *key, const u8 hfunc) |
| 1311 | { |
| 1312 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1313 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
Colin Ian King | eb996ed | 2017-03-25 14:26:39 +0000 | [diff] [blame] | 1314 | struct rndis_device *rndis_dev; |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1315 | int i; |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1316 | |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1317 | if (!ndev) |
| 1318 | return -ENODEV; |
| 1319 | |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1320 | if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) |
| 1321 | return -EOPNOTSUPP; |
| 1322 | |
Colin Ian King | eb996ed | 2017-03-25 14:26:39 +0000 | [diff] [blame] | 1323 | rndis_dev = ndev->extension; |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1324 | if (indir) { |
| 1325 | for (i = 0; i < ITAB_NUM; i++) |
Arnd Bergmann | b92b7d3 | 2017-06-22 00:16:37 +0200 | [diff] [blame] | 1326 | if (indir[i] >= VRSS_CHANNEL_MAX) |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1327 | return -EINVAL; |
| 1328 | |
| 1329 | for (i = 0; i < ITAB_NUM; i++) |
| 1330 | rndis_dev->ind_table[i] = indir[i]; |
| 1331 | } |
| 1332 | |
| 1333 | if (!key) { |
| 1334 | if (!indir) |
| 1335 | return 0; |
| 1336 | |
| 1337 | key = rndis_dev->rss_key; |
| 1338 | } |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1339 | |
| 1340 | return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn); |
| 1341 | } |
| 1342 | |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1343 | static const struct ethtool_ops ethtool_ops = { |
| 1344 | .get_drvinfo = netvsc_get_drvinfo, |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1345 | .get_link = ethtool_op_get_link, |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1346 | .get_ethtool_stats = netvsc_get_ethtool_stats, |
| 1347 | .get_sset_count = netvsc_get_sset_count, |
| 1348 | .get_strings = netvsc_get_strings, |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 1349 | .get_channels = netvsc_get_channels, |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 1350 | .set_channels = netvsc_set_channels, |
sixiao@microsoft.com | 76d13b5 | 2016-02-17 16:43:59 -0800 | [diff] [blame] | 1351 | .get_ts_info = ethtool_op_get_ts_info, |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1352 | .get_rxnfc = netvsc_get_rxnfc, |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1353 | .get_rxfh_key_size = netvsc_get_rxfh_key_size, |
| 1354 | .get_rxfh_indir_size = netvsc_rss_indir_size, |
| 1355 | .get_rxfh = netvsc_get_rxfh, |
| 1356 | .set_rxfh = netvsc_set_rxfh, |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 1357 | .get_link_ksettings = netvsc_get_link_ksettings, |
| 1358 | .set_link_ksettings = netvsc_set_link_ksettings, |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1359 | }; |
| 1360 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1361 | static const struct net_device_ops device_ops = { |
| 1362 | .ndo_open = netvsc_open, |
| 1363 | .ndo_stop = netvsc_close, |
| 1364 | .ndo_start_xmit = netvsc_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1365 | .ndo_set_rx_mode = netvsc_set_multicast_list, |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 1366 | .ndo_change_mtu = netvsc_change_mtu, |
Haiyang Zhang | b681b58 | 2010-08-03 19:15:31 +0000 | [diff] [blame] | 1367 | .ndo_validate_addr = eth_validate_addr, |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1368 | .ndo_set_mac_address = netvsc_set_mac_addr, |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1369 | .ndo_select_queue = netvsc_select_queue, |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1370 | .ndo_get_stats64 = netvsc_get_stats64, |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1371 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1372 | .ndo_poll_controller = netvsc_poll_controller, |
| 1373 | #endif |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1374 | }; |
| 1375 | |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1376 | /* |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1377 | * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link |
| 1378 | * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is |
| 1379 | * present send GARP packet to network peers with netif_notify_peers(). |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1380 | */ |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1381 | static void netvsc_link_change(struct work_struct *w) |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1382 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1383 | struct net_device_context *ndev_ctx = |
| 1384 | container_of(w, struct net_device_context, dwork.work); |
| 1385 | struct hv_device *device_obj = ndev_ctx->device_ctx; |
| 1386 | struct net_device *net = hv_get_drvdata(device_obj); |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1387 | struct netvsc_device *net_device; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1388 | struct rndis_device *rdev; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1389 | struct netvsc_reconfig *event = NULL; |
| 1390 | bool notify = false, reschedule = false; |
| 1391 | unsigned long flags, next_reconfig, delay; |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1392 | |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1393 | rtnl_lock(); |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 1394 | net_device = rtnl_dereference(ndev_ctx->nvdev); |
| 1395 | if (!net_device) |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1396 | goto out_unlock; |
| 1397 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1398 | rdev = net_device->extension; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1399 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1400 | next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT; |
| 1401 | if (time_is_after_jiffies(next_reconfig)) { |
| 1402 | /* link_watch only sends one notification with current state |
| 1403 | * per second, avoid doing reconfig more frequently. Handle |
| 1404 | * wrap around. |
| 1405 | */ |
| 1406 | delay = next_reconfig - jiffies; |
| 1407 | delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT; |
| 1408 | schedule_delayed_work(&ndev_ctx->dwork, delay); |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1409 | goto out_unlock; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1410 | } |
| 1411 | ndev_ctx->last_reconfig = jiffies; |
| 1412 | |
| 1413 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
| 1414 | if (!list_empty(&ndev_ctx->reconfig_events)) { |
| 1415 | event = list_first_entry(&ndev_ctx->reconfig_events, |
| 1416 | struct netvsc_reconfig, list); |
| 1417 | list_del(&event->list); |
| 1418 | reschedule = !list_empty(&ndev_ctx->reconfig_events); |
| 1419 | } |
| 1420 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 1421 | |
| 1422 | if (!event) |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1423 | goto out_unlock; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1424 | |
| 1425 | switch (event->event) { |
| 1426 | /* Only the following events are possible due to the check in |
| 1427 | * netvsc_linkstatus_callback() |
| 1428 | */ |
| 1429 | case RNDIS_STATUS_MEDIA_CONNECT: |
| 1430 | if (rdev->link_state) { |
| 1431 | rdev->link_state = false; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1432 | netif_carrier_on(net); |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1433 | netif_tx_wake_all_queues(net); |
| 1434 | } else { |
| 1435 | notify = true; |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 1436 | } |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1437 | kfree(event); |
| 1438 | break; |
| 1439 | case RNDIS_STATUS_MEDIA_DISCONNECT: |
| 1440 | if (!rdev->link_state) { |
| 1441 | rdev->link_state = true; |
| 1442 | netif_carrier_off(net); |
| 1443 | netif_tx_stop_all_queues(net); |
| 1444 | } |
| 1445 | kfree(event); |
| 1446 | break; |
| 1447 | case RNDIS_STATUS_NETWORK_CHANGE: |
| 1448 | /* Only makes sense if carrier is present */ |
| 1449 | if (!rdev->link_state) { |
| 1450 | rdev->link_state = true; |
| 1451 | netif_carrier_off(net); |
| 1452 | netif_tx_stop_all_queues(net); |
| 1453 | event->event = RNDIS_STATUS_MEDIA_CONNECT; |
| 1454 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
Haiyang Zhang | 15cfd40 | 2016-04-21 16:13:01 -0700 | [diff] [blame] | 1455 | list_add(&event->list, &ndev_ctx->reconfig_events); |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1456 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 1457 | reschedule = true; |
| 1458 | } |
| 1459 | break; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | rtnl_unlock(); |
| 1463 | |
| 1464 | if (notify) |
| 1465 | netdev_notify_peers(net); |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1466 | |
| 1467 | /* link_watch only sends one notification with current state per |
| 1468 | * second, handle next reconfig event in 2 seconds. |
| 1469 | */ |
| 1470 | if (reschedule) |
| 1471 | schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1472 | |
| 1473 | return; |
| 1474 | |
| 1475 | out_unlock: |
| 1476 | rtnl_unlock(); |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1479 | static struct net_device *get_netvsc_bymac(const u8 *mac) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1480 | { |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1481 | struct net_device *dev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1482 | |
Stephen Hemminger | 8737caa | 2016-08-23 12:17:44 -0700 | [diff] [blame] | 1483 | ASSERT_RTNL(); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1484 | |
| 1485 | for_each_netdev(&init_net, dev) { |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1486 | if (dev->netdev_ops != &device_ops) |
| 1487 | continue; /* not a netvsc device */ |
| 1488 | |
| 1489 | if (ether_addr_equal(mac, dev->perm_addr)) |
| 1490 | return dev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1491 | } |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1492 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1493 | return NULL; |
| 1494 | } |
| 1495 | |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1496 | static struct net_device *get_netvsc_byref(struct net_device *vf_netdev) |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1497 | { |
| 1498 | struct net_device *dev; |
| 1499 | |
| 1500 | ASSERT_RTNL(); |
| 1501 | |
| 1502 | for_each_netdev(&init_net, dev) { |
| 1503 | struct net_device_context *net_device_ctx; |
| 1504 | |
| 1505 | if (dev->netdev_ops != &device_ops) |
| 1506 | continue; /* not a netvsc device */ |
| 1507 | |
| 1508 | net_device_ctx = netdev_priv(dev); |
stephen hemminger | 79e8cbe | 2017-07-19 11:53:13 -0700 | [diff] [blame] | 1509 | if (!rtnl_dereference(net_device_ctx->nvdev)) |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1510 | continue; /* device is removed */ |
| 1511 | |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1512 | if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev) |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1513 | return dev; /* a match */ |
| 1514 | } |
| 1515 | |
| 1516 | return NULL; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1519 | /* Called when VF is injecting data into network stack. |
| 1520 | * Change the associated network device from VF to netvsc. |
| 1521 | * note: already called with rcu_read_lock |
| 1522 | */ |
| 1523 | static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb) |
| 1524 | { |
| 1525 | struct sk_buff *skb = *pskb; |
| 1526 | struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data); |
| 1527 | struct net_device_context *ndev_ctx = netdev_priv(ndev); |
| 1528 | struct netvsc_vf_pcpu_stats *pcpu_stats |
| 1529 | = this_cpu_ptr(ndev_ctx->vf_stats); |
| 1530 | |
| 1531 | skb->dev = ndev; |
| 1532 | |
| 1533 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 1534 | pcpu_stats->rx_packets++; |
| 1535 | pcpu_stats->rx_bytes += skb->len; |
| 1536 | u64_stats_update_end(&pcpu_stats->syncp); |
| 1537 | |
| 1538 | return RX_HANDLER_ANOTHER; |
| 1539 | } |
| 1540 | |
| 1541 | static int netvsc_vf_join(struct net_device *vf_netdev, |
| 1542 | struct net_device *ndev) |
| 1543 | { |
| 1544 | struct net_device_context *ndev_ctx = netdev_priv(ndev); |
| 1545 | int ret; |
| 1546 | |
| 1547 | ret = netdev_rx_handler_register(vf_netdev, |
| 1548 | netvsc_vf_handle_frame, ndev); |
| 1549 | if (ret != 0) { |
| 1550 | netdev_err(vf_netdev, |
| 1551 | "can not register netvsc VF receive handler (err = %d)\n", |
| 1552 | ret); |
| 1553 | goto rx_handler_failed; |
| 1554 | } |
| 1555 | |
| 1556 | ret = netdev_upper_dev_link(vf_netdev, ndev); |
| 1557 | if (ret != 0) { |
| 1558 | netdev_err(vf_netdev, |
| 1559 | "can not set master device %s (err = %d)\n", |
| 1560 | ndev->name, ret); |
| 1561 | goto upper_link_failed; |
| 1562 | } |
| 1563 | |
| 1564 | /* set slave flag before open to prevent IPv6 addrconf */ |
| 1565 | vf_netdev->flags |= IFF_SLAVE; |
| 1566 | |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1567 | schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT); |
| 1568 | |
| 1569 | call_netdevice_notifiers(NETDEV_JOIN, vf_netdev); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1570 | |
| 1571 | netdev_info(vf_netdev, "joined to %s\n", ndev->name); |
| 1572 | return 0; |
| 1573 | |
| 1574 | upper_link_failed: |
| 1575 | netdev_rx_handler_unregister(vf_netdev); |
| 1576 | rx_handler_failed: |
| 1577 | return ret; |
| 1578 | } |
| 1579 | |
| 1580 | static void __netvsc_vf_setup(struct net_device *ndev, |
| 1581 | struct net_device *vf_netdev) |
| 1582 | { |
| 1583 | int ret; |
| 1584 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1585 | /* Align MTU of VF with master */ |
| 1586 | ret = dev_set_mtu(vf_netdev, ndev->mtu); |
| 1587 | if (ret) |
| 1588 | netdev_warn(vf_netdev, |
| 1589 | "unable to change mtu to %u\n", ndev->mtu); |
| 1590 | |
| 1591 | if (netif_running(ndev)) { |
| 1592 | ret = dev_open(vf_netdev); |
| 1593 | if (ret) |
| 1594 | netdev_warn(vf_netdev, |
| 1595 | "unable to open: %d\n", ret); |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | /* Setup VF as slave of the synthetic device. |
| 1600 | * Runs in workqueue to avoid recursion in netlink callbacks. |
| 1601 | */ |
| 1602 | static void netvsc_vf_setup(struct work_struct *w) |
| 1603 | { |
| 1604 | struct net_device_context *ndev_ctx |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1605 | = container_of(w, struct net_device_context, vf_takeover.work); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1606 | struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx); |
| 1607 | struct net_device *vf_netdev; |
| 1608 | |
stephen hemminger | fb84af8 | 2017-08-04 12:14:00 -0700 | [diff] [blame] | 1609 | if (!rtnl_trylock()) { |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1610 | schedule_delayed_work(&ndev_ctx->vf_takeover, 0); |
stephen hemminger | fb84af8 | 2017-08-04 12:14:00 -0700 | [diff] [blame] | 1611 | return; |
| 1612 | } |
| 1613 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1614 | vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); |
| 1615 | if (vf_netdev) |
| 1616 | __netvsc_vf_setup(ndev, vf_netdev); |
| 1617 | |
| 1618 | rtnl_unlock(); |
| 1619 | } |
| 1620 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1621 | static int netvsc_register_vf(struct net_device *vf_netdev) |
| 1622 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1623 | struct net_device *ndev; |
| 1624 | struct net_device_context *net_device_ctx; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1625 | struct netvsc_device *netvsc_dev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1626 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1627 | if (vf_netdev->addr_len != ETH_ALEN) |
| 1628 | return NOTIFY_DONE; |
| 1629 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1630 | /* |
| 1631 | * We will use the MAC address to locate the synthetic interface to |
| 1632 | * associate with the VF interface. If we don't find a matching |
| 1633 | * synthetic interface, move on. |
| 1634 | */ |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1635 | ndev = get_netvsc_bymac(vf_netdev->perm_addr); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1636 | if (!ndev) |
| 1637 | return NOTIFY_DONE; |
| 1638 | |
| 1639 | net_device_ctx = netdev_priv(ndev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1640 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1641 | if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev)) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1642 | return NOTIFY_DONE; |
| 1643 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1644 | if (netvsc_vf_join(vf_netdev, ndev) != 0) |
| 1645 | return NOTIFY_DONE; |
| 1646 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1647 | netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1648 | |
| 1649 | /* Prevent this module from being unloaded while VF is registered */ |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1650 | try_module_get(THIS_MODULE); |
Stephen Hemminger | 07d0f00 | 2016-09-22 16:56:30 -0700 | [diff] [blame] | 1651 | |
| 1652 | dev_hold(vf_netdev); |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1653 | rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1654 | return NOTIFY_OK; |
| 1655 | } |
| 1656 | |
stephen hemminger | 7b83f52 | 2017-08-07 11:30:00 -0700 | [diff] [blame] | 1657 | static int netvsc_vf_up(struct net_device *vf_netdev) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1658 | { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1659 | struct net_device_context *net_device_ctx; |
stephen hemminger | 7b83f52 | 2017-08-07 11:30:00 -0700 | [diff] [blame] | 1660 | struct netvsc_device *netvsc_dev; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1661 | struct net_device *ndev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1662 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1663 | ndev = get_netvsc_byref(vf_netdev); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1664 | if (!ndev) |
| 1665 | return NOTIFY_DONE; |
| 1666 | |
| 1667 | net_device_ctx = netdev_priv(ndev); |
stephen hemminger | 7b83f52 | 2017-08-07 11:30:00 -0700 | [diff] [blame] | 1668 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
| 1669 | if (!netvsc_dev) |
| 1670 | return NOTIFY_DONE; |
| 1671 | |
| 1672 | /* Bump refcount when datapath is acvive - Why? */ |
| 1673 | rndis_filter_open(netvsc_dev); |
| 1674 | |
| 1675 | /* notify the host to switch the data path. */ |
| 1676 | netvsc_switch_datapath(ndev, true); |
| 1677 | netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name); |
| 1678 | |
| 1679 | return NOTIFY_OK; |
| 1680 | } |
| 1681 | |
| 1682 | static int netvsc_vf_down(struct net_device *vf_netdev) |
| 1683 | { |
| 1684 | struct net_device_context *net_device_ctx; |
| 1685 | struct netvsc_device *netvsc_dev; |
| 1686 | struct net_device *ndev; |
| 1687 | |
| 1688 | ndev = get_netvsc_byref(vf_netdev); |
| 1689 | if (!ndev) |
| 1690 | return NOTIFY_DONE; |
| 1691 | |
| 1692 | net_device_ctx = netdev_priv(ndev); |
| 1693 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
| 1694 | if (!netvsc_dev) |
| 1695 | return NOTIFY_DONE; |
| 1696 | |
| 1697 | netvsc_switch_datapath(ndev, false); |
| 1698 | netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name); |
| 1699 | rndis_filter_close(netvsc_dev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1700 | |
| 1701 | return NOTIFY_OK; |
| 1702 | } |
| 1703 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1704 | static int netvsc_unregister_vf(struct net_device *vf_netdev) |
| 1705 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1706 | struct net_device *ndev; |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1707 | struct net_device_context *net_device_ctx; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1708 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1709 | ndev = get_netvsc_byref(vf_netdev); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1710 | if (!ndev) |
| 1711 | return NOTIFY_DONE; |
| 1712 | |
| 1713 | net_device_ctx = netdev_priv(ndev); |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1714 | cancel_delayed_work_sync(&net_device_ctx->vf_takeover); |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1715 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1716 | netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name); |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1717 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1718 | netdev_upper_dev_unlink(vf_netdev, ndev); |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1719 | RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL); |
Stephen Hemminger | 07d0f00 | 2016-09-22 16:56:30 -0700 | [diff] [blame] | 1720 | dev_put(vf_netdev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1721 | module_put(THIS_MODULE); |
| 1722 | return NOTIFY_OK; |
| 1723 | } |
| 1724 | |
K. Y. Srinivasan | 8494689 | 2011-09-13 10:59:38 -0700 | [diff] [blame] | 1725 | static int netvsc_probe(struct hv_device *dev, |
| 1726 | const struct hv_vmbus_device_id *dev_id) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1727 | { |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1728 | struct net_device *net = NULL; |
| 1729 | struct net_device_context *net_device_ctx; |
| 1730 | struct netvsc_device_info device_info; |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1731 | struct netvsc_device *nvdev; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1732 | int ret = -ENOMEM; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1733 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1734 | net = alloc_etherdev_mq(sizeof(struct net_device_context), |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 1735 | VRSS_CHANNEL_MAX); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1736 | if (!net) |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1737 | goto no_net; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1738 | |
Haiyang Zhang | 1b07da5 | 2014-03-04 14:11:06 -0800 | [diff] [blame] | 1739 | netif_carrier_off(net); |
| 1740 | |
Haiyang Zhang | b37879e | 2016-08-04 10:42:14 -0700 | [diff] [blame] | 1741 | netvsc_init_settings(net); |
| 1742 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1743 | net_device_ctx = netdev_priv(net); |
K. Y. Srinivasan | 9efd21e | 2011-04-29 13:45:10 -0700 | [diff] [blame] | 1744 | net_device_ctx->device_ctx = dev; |
Simon Xiao | 3f300ff | 2015-04-28 01:05:17 -0700 | [diff] [blame] | 1745 | net_device_ctx->msg_enable = netif_msg_init(debug, default_msg); |
| 1746 | if (netif_msg_probe(net_device_ctx)) |
| 1747 | netdev_dbg(net, "netvsc msg_enable: %d\n", |
| 1748 | net_device_ctx->msg_enable); |
| 1749 | |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1750 | hv_set_drvdata(dev, net); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 1751 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1752 | INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1753 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1754 | spin_lock_init(&net_device_ctx->lock); |
| 1755 | INIT_LIST_HEAD(&net_device_ctx->reconfig_events); |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1756 | INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1757 | |
| 1758 | net_device_ctx->vf_stats |
| 1759 | = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats); |
| 1760 | if (!net_device_ctx->vf_stats) |
| 1761 | goto no_stats; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1762 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1763 | net->netdev_ops = &device_ops; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1764 | net->ethtool_ops = ðtool_ops; |
K. Y. Srinivasan | 9efd21e | 2011-04-29 13:45:10 -0700 | [diff] [blame] | 1765 | SET_NETDEV_DEV(net, &dev->device); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1766 | |
Vitaly Kuznetsov | 14a03cf | 2016-02-05 17:29:08 +0100 | [diff] [blame] | 1767 | /* We always need headroom for rndis header */ |
| 1768 | net->needed_headroom = RNDIS_AND_PPI_SIZE; |
| 1769 | |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1770 | /* Notify the netvsc driver of the new device */ |
Andrew Schwartzmeyer | 8ebdcc5 | 2015-08-11 17:14:31 -0700 | [diff] [blame] | 1771 | memset(&device_info, 0, sizeof(device_info)); |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1772 | device_info.ring_size = ring_size; |
stephen hemminger | 3071ada | 2017-03-22 14:50:59 -0700 | [diff] [blame] | 1773 | device_info.num_chn = VRSS_CHANNEL_DEFAULT; |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 1774 | |
| 1775 | nvdev = rndis_filter_device_add(dev, &device_info); |
| 1776 | if (IS_ERR(nvdev)) { |
| 1777 | ret = PTR_ERR(nvdev); |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1778 | netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1779 | goto rndis_failed; |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1780 | } |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1781 | |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1782 | memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN); |
| 1783 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 1784 | /* hw_features computed in rndis_filter_device_add */ |
| 1785 | net->features = net->hw_features | |
| 1786 | NETIF_F_HIGHDMA | NETIF_F_SG | |
| 1787 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; |
| 1788 | net->vlan_features = net->features; |
| 1789 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1790 | netif_set_real_num_tx_queues(net, nvdev->num_chn); |
| 1791 | netif_set_real_num_rx_queues(net, nvdev->num_chn); |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1792 | |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 1793 | netdev_lockdep_set_classes(net); |
| 1794 | |
Jarod Wilson | d0c2c99 | 2016-10-20 13:55:21 -0400 | [diff] [blame] | 1795 | /* MTU range: 68 - 1500 or 65521 */ |
| 1796 | net->min_mtu = NETVSC_MTU_MIN; |
| 1797 | if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) |
| 1798 | net->max_mtu = NETVSC_MTU - ETH_HLEN; |
| 1799 | else |
| 1800 | net->max_mtu = ETH_DATA_LEN; |
| 1801 | |
Haiyang Zhang | a68f961 | 2013-12-20 16:52:31 -0800 | [diff] [blame] | 1802 | ret = register_netdev(net); |
| 1803 | if (ret != 0) { |
| 1804 | pr_err("Unable to register netdev.\n"); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1805 | goto register_failed; |
Haiyang Zhang | a68f961 | 2013-12-20 16:52:31 -0800 | [diff] [blame] | 1806 | } |
| 1807 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1808 | return ret; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1809 | |
| 1810 | register_failed: |
| 1811 | rndis_filter_device_remove(dev, nvdev); |
| 1812 | rndis_failed: |
| 1813 | free_percpu(net_device_ctx->vf_stats); |
| 1814 | no_stats: |
| 1815 | hv_set_drvdata(dev, NULL); |
| 1816 | free_netdev(net); |
| 1817 | no_net: |
| 1818 | return ret; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
K. Y. Srinivasan | 415b023 | 2011-04-29 13:45:12 -0700 | [diff] [blame] | 1821 | static int netvsc_remove(struct hv_device *dev) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1822 | { |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1823 | struct net_device *net; |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 1824 | struct net_device_context *ndev_ctx; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1825 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 1826 | net = hv_get_drvdata(dev); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1827 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1828 | if (net == NULL) { |
K. Y. Srinivasan | 415b023 | 2011-04-29 13:45:12 -0700 | [diff] [blame] | 1829 | dev_err(&dev->device, "No net device to remove\n"); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1830 | return 0; |
| 1831 | } |
| 1832 | |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 1833 | ndev_ctx = netdev_priv(net); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 1834 | |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 1835 | netif_device_detach(net); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 1836 | |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 1837 | cancel_delayed_work_sync(&ndev_ctx->dwork); |
| 1838 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1839 | /* |
| 1840 | * Call to the vsc driver to let it know that the device is being |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 1841 | * removed. Also blocks mtu and channel changes. |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1842 | */ |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 1843 | rtnl_lock(); |
stephen hemminger | 79e8cbe | 2017-07-19 11:53:13 -0700 | [diff] [blame] | 1844 | rndis_filter_device_remove(dev, |
| 1845 | rtnl_dereference(ndev_ctx->nvdev)); |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 1846 | rtnl_unlock(); |
| 1847 | |
| 1848 | unregister_netdev(net); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1849 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 1850 | hv_set_drvdata(dev, NULL); |
| 1851 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1852 | free_percpu(ndev_ctx->vf_stats); |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1853 | free_netdev(net); |
Haiyang Zhang | df06bcf | 2011-05-23 09:03:47 -0700 | [diff] [blame] | 1854 | return 0; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 1857 | static const struct hv_vmbus_device_id id_table[] = { |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1858 | /* Network guid */ |
K. Y. Srinivasan | 8f50594 | 2013-01-23 17:42:42 -0800 | [diff] [blame] | 1859 | { HV_NIC_GUID, }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1860 | { }, |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 1861 | }; |
| 1862 | |
| 1863 | MODULE_DEVICE_TABLE(vmbus, id_table); |
| 1864 | |
K. Y. Srinivasan | f1542a6 | 2011-05-10 07:55:16 -0700 | [diff] [blame] | 1865 | /* The one and only one */ |
K. Y. Srinivasan | fde0ef9 | 2011-05-12 19:35:08 -0700 | [diff] [blame] | 1866 | static struct hv_driver netvsc_drv = { |
Haiyang Zhang | d31b20f | 2012-03-07 10:02:00 +0000 | [diff] [blame] | 1867 | .name = KBUILD_MODNAME, |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 1868 | .id_table = id_table, |
K. Y. Srinivasan | fde0ef9 | 2011-05-12 19:35:08 -0700 | [diff] [blame] | 1869 | .probe = netvsc_probe, |
| 1870 | .remove = netvsc_remove, |
K. Y. Srinivasan | d489097 | 2011-05-10 07:55:17 -0700 | [diff] [blame] | 1871 | }; |
K. Y. Srinivasan | f1542a6 | 2011-05-10 07:55:16 -0700 | [diff] [blame] | 1872 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1873 | /* |
| 1874 | * On Hyper-V, every VF interface is matched with a corresponding |
| 1875 | * synthetic interface. The synthetic interface is presented first |
| 1876 | * to the guest. When the corresponding VF instance is registered, |
| 1877 | * we will take care of switching the data path. |
| 1878 | */ |
| 1879 | static int netvsc_netdev_event(struct notifier_block *this, |
| 1880 | unsigned long event, void *ptr) |
| 1881 | { |
| 1882 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); |
| 1883 | |
Stephen Hemminger | ee837a1 | 2016-09-22 16:56:31 -0700 | [diff] [blame] | 1884 | /* Skip our own events */ |
| 1885 | if (event_dev->netdev_ops == &device_ops) |
| 1886 | return NOTIFY_DONE; |
| 1887 | |
| 1888 | /* Avoid non-Ethernet type devices */ |
| 1889 | if (event_dev->type != ARPHRD_ETHER) |
| 1890 | return NOTIFY_DONE; |
| 1891 | |
Vitaly Kuznetsov | 0dbff14 | 2016-08-15 17:48:43 +0200 | [diff] [blame] | 1892 | /* Avoid Vlan dev with same MAC registering as VF */ |
Parav Pandit | d0d7b10 | 2017-02-04 11:00:49 -0600 | [diff] [blame] | 1893 | if (is_vlan_dev(event_dev)) |
Vitaly Kuznetsov | 0dbff14 | 2016-08-15 17:48:43 +0200 | [diff] [blame] | 1894 | return NOTIFY_DONE; |
| 1895 | |
| 1896 | /* Avoid Bonding master dev with same MAC registering as VF */ |
Stephen Hemminger | ee837a1 | 2016-09-22 16:56:31 -0700 | [diff] [blame] | 1897 | if ((event_dev->priv_flags & IFF_BONDING) && |
| 1898 | (event_dev->flags & IFF_MASTER)) |
Haiyang Zhang | cb2911f | 2016-06-02 12:02:04 -0700 | [diff] [blame] | 1899 | return NOTIFY_DONE; |
| 1900 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1901 | switch (event) { |
| 1902 | case NETDEV_REGISTER: |
| 1903 | return netvsc_register_vf(event_dev); |
| 1904 | case NETDEV_UNREGISTER: |
| 1905 | return netvsc_unregister_vf(event_dev); |
| 1906 | case NETDEV_UP: |
stephen hemminger | 7b83f52 | 2017-08-07 11:30:00 -0700 | [diff] [blame] | 1907 | return netvsc_vf_up(event_dev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1908 | case NETDEV_DOWN: |
stephen hemminger | 7b83f52 | 2017-08-07 11:30:00 -0700 | [diff] [blame] | 1909 | return netvsc_vf_down(event_dev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1910 | default: |
| 1911 | return NOTIFY_DONE; |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | static struct notifier_block netvsc_netdev_notifier = { |
| 1916 | .notifier_call = netvsc_netdev_event, |
| 1917 | }; |
| 1918 | |
K. Y. Srinivasan | a9869c9 | 2011-05-12 19:35:17 -0700 | [diff] [blame] | 1919 | static void __exit netvsc_drv_exit(void) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1920 | { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1921 | unregister_netdevice_notifier(&netvsc_netdev_notifier); |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1922 | vmbus_driver_unregister(&netvsc_drv); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
K. Y. Srinivasan | 1fde28c | 2011-05-12 19:35:16 -0700 | [diff] [blame] | 1925 | static int __init netvsc_drv_init(void) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1926 | { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1927 | int ret; |
| 1928 | |
Haiyang Zhang | fa85a6c | 2012-07-25 08:08:41 +0000 | [diff] [blame] | 1929 | if (ring_size < RING_SIZE_MIN) { |
| 1930 | ring_size = RING_SIZE_MIN; |
| 1931 | pr_info("Increased ring_size to %d (min allowed)\n", |
| 1932 | ring_size); |
| 1933 | } |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1934 | ret = vmbus_driver_register(&netvsc_drv); |
| 1935 | |
| 1936 | if (ret) |
| 1937 | return ret; |
| 1938 | |
| 1939 | register_netdevice_notifier(&netvsc_netdev_notifier); |
| 1940 | return 0; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 1943 | MODULE_LICENSE("GPL"); |
Stephen Hemminger | 7880fc5 | 2010-05-04 09:58:52 -0700 | [diff] [blame] | 1944 | MODULE_DESCRIPTION("Microsoft Hyper-V network driver"); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1945 | |
K. Y. Srinivasan | 1fde28c | 2011-05-12 19:35:16 -0700 | [diff] [blame] | 1946 | module_init(netvsc_drv_init); |
K. Y. Srinivasan | a9869c9 | 2011-05-12 19:35:17 -0700 | [diff] [blame] | 1947 | module_exit(netvsc_drv_exit); |