blob: 20e09174ff6240bb5b1ba2eb4f13f0a4fd0d5bf3 [file] [log] [blame]
Hank Janssenfceaf242009-07-13 15:34:54 -07001/*
Hank Janssenfceaf242009-07-13 15:34:54 -07002 * 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 Kirsheradf8d3f2013-12-06 06:28:47 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Hank Janssenfceaf242009-07-13 15:34:54 -070015 *
16 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +000017 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070018 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 */
Hank Jansseneb335bc2011-03-29 13:58:48 -070020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070022#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080023#include <linux/sched.h>
24#include <linux/wait.h>
Greg Kroah-Hartman0ffa63b2009-07-15 11:06:01 -070025#include <linux/mm.h>
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -070026#include <linux/delay.h>
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070027#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Haiyang Zhangd9871152011-09-01 12:19:41 -070029#include <linux/netdevice.h>
Haiyang Zhangf157e782011-12-15 13:45:16 -080030#include <linux/if_ether.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100031#include <linux/vmalloc.h>
KY Srinivasanc25aaf82014-04-30 10:14:31 -070032#include <asm/sync_bitops.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070033
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070034#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070035
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070036/*
37 * Switch the data path from the synthetic interface to the VF
38 * interface.
39 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020040void netvsc_switch_datapath(struct net_device *ndev, bool vf)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070041{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +020042 struct net_device_context *net_device_ctx = netdev_priv(ndev);
43 struct hv_device *dev = net_device_ctx->device_ctx;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020044 struct netvsc_device *nv_dev = net_device_ctx->nvdev;
45 struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070046
47 memset(init_pkt, 0, sizeof(struct nvsp_message));
48 init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
49 if (vf)
50 init_pkt->msg.v4_msg.active_dp.active_datapath =
51 NVSP_DATAPATH_VF;
52 else
53 init_pkt->msg.v4_msg.active_dp.active_datapath =
54 NVSP_DATAPATH_SYNTHETIC;
55
56 vmbus_sendpacket(dev->channel, init_pkt,
57 sizeof(struct nvsp_message),
58 (unsigned long)init_pkt,
59 VM_PKT_DATA_INBAND, 0);
60}
61
Hank Janssenfceaf242009-07-13 15:34:54 -070062
Vitaly Kuznetsov88098832016-05-13 13:55:25 +020063static struct netvsc_device *alloc_net_device(void)
Hank Janssenfceaf242009-07-13 15:34:54 -070064{
Haiyang Zhang85799a32010-12-10 12:03:54 -080065 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070066
Haiyang Zhang85799a32010-12-10 12:03:54 -080067 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
68 if (!net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -070069 return NULL;
70
Haiyang Zhangf90251c2014-08-15 19:18:19 +000071 net_device->cb_buffer = kzalloc(NETVSC_PACKET_SIZE, GFP_KERNEL);
72 if (!net_device->cb_buffer) {
73 kfree(net_device);
74 return NULL;
75 }
76
Haiyang Zhangdc5cd892012-06-04 06:42:38 +000077 init_waitqueue_head(&net_device->wait_drain);
K. Y. Srinivasanc38b9c72011-08-27 11:31:12 -070078 net_device->destroy = false;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070079 atomic_set(&net_device->open_cnt, 0);
80 atomic_set(&net_device->vf_use_cnt, 0);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -070081 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
82 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
83
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070084 net_device->vf_netdev = NULL;
85 net_device->vf_inject = false;
86
Haiyang Zhang85799a32010-12-10 12:03:54 -080087 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070088}
89
Haiyang Zhangf90251c2014-08-15 19:18:19 +000090static void free_netvsc_device(struct netvsc_device *nvdev)
91{
92 kfree(nvdev->cb_buffer);
93 kfree(nvdev);
94}
95
Haiyang Zhang5a71ae32010-12-10 12:03:55 -080096static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -070097{
Vitaly Kuznetsov26254662016-06-03 17:50:59 +020098 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
Hank Janssenfceaf242009-07-13 15:34:54 -070099
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700100 if (net_device && net_device->destroy)
Haiyang Zhang85799a32010-12-10 12:03:54 -0800101 net_device = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700102
Haiyang Zhang85799a32010-12-10 12:03:54 -0800103 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700104}
105
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800106static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700107{
Vitaly Kuznetsov26254662016-06-03 17:50:59 +0200108 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700109
110 if (!net_device)
111 goto get_in_err;
112
113 if (net_device->destroy &&
114 atomic_read(&net_device->num_outstanding_sends) == 0)
Haiyang Zhang85799a32010-12-10 12:03:54 -0800115 net_device = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700116
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700117get_in_err:
Haiyang Zhang85799a32010-12-10 12:03:54 -0800118 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700119}
120
Hank Janssenfceaf242009-07-13 15:34:54 -0700121
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200122static int netvsc_destroy_buf(struct hv_device *device)
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700123{
124 struct nvsp_message *revoke_packet;
125 int ret = 0;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200126 struct net_device *ndev = hv_get_drvdata(device);
Vitaly Kuznetsov26254662016-06-03 17:50:59 +0200127 struct netvsc_device *net_device = net_device_to_netvsc_device(ndev);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700128
129 /*
130 * If we got a section count, it means we received a
131 * SendReceiveBufferComplete msg (ie sent
132 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
133 * to send a revoke msg here
134 */
135 if (net_device->recv_section_cnt) {
136 /* Send the revoke receive buffer */
137 revoke_packet = &net_device->revoke_packet;
138 memset(revoke_packet, 0, sizeof(struct nvsp_message));
139
140 revoke_packet->hdr.msg_type =
141 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
142 revoke_packet->msg.v1_msg.
143 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
144
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200145 ret = vmbus_sendpacket(device->channel,
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700146 revoke_packet,
147 sizeof(struct nvsp_message),
148 (unsigned long)revoke_packet,
149 VM_PKT_DATA_INBAND, 0);
150 /*
151 * If we failed here, we might as well return and
152 * have a leak rather than continue and a bugchk
153 */
154 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700155 netdev_err(ndev, "unable to send "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700156 "revoke receive buffer to netvsp\n");
K. Y. Srinivasana3e00532011-08-25 09:49:12 -0700157 return ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700158 }
159 }
160
161 /* Teardown the gpadl on the vsp end */
162 if (net_device->recv_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200163 ret = vmbus_teardown_gpadl(device->channel,
164 net_device->recv_buf_gpadl_handle);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700165
166 /* If we failed here, we might as well return and have a leak
167 * rather than continue and a bugchk
168 */
169 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700170 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700171 "unable to teardown receive buffer's gpadl\n");
Dan Carpenter7f9615e2011-08-27 14:06:07 +0300172 return ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700173 }
174 net_device->recv_buf_gpadl_handle = 0;
175 }
176
177 if (net_device->recv_buf) {
178 /* Free up the receive buffer */
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800179 vfree(net_device->recv_buf);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700180 net_device->recv_buf = NULL;
181 }
182
183 if (net_device->recv_section) {
184 net_device->recv_section_cnt = 0;
185 kfree(net_device->recv_section);
186 net_device->recv_section = NULL;
187 }
188
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700189 /* Deal with the send buffer we may have setup.
190 * If we got a send section size, it means we received a
Haiyang Zhangc51ed18252014-12-19 18:25:18 -0800191 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
192 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700193 * to send a revoke msg here
194 */
195 if (net_device->send_section_size) {
196 /* Send the revoke receive buffer */
197 revoke_packet = &net_device->revoke_packet;
198 memset(revoke_packet, 0, sizeof(struct nvsp_message));
199
200 revoke_packet->hdr.msg_type =
201 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
Haiyang Zhangc51ed18252014-12-19 18:25:18 -0800202 revoke_packet->msg.v1_msg.revoke_send_buf.id =
203 NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700204
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200205 ret = vmbus_sendpacket(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700206 revoke_packet,
207 sizeof(struct nvsp_message),
208 (unsigned long)revoke_packet,
209 VM_PKT_DATA_INBAND, 0);
210 /* If we failed here, we might as well return and
211 * have a leak rather than continue and a bugchk
212 */
213 if (ret != 0) {
214 netdev_err(ndev, "unable to send "
215 "revoke send buffer to netvsp\n");
216 return ret;
217 }
218 }
219 /* Teardown the gpadl on the vsp end */
220 if (net_device->send_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200221 ret = vmbus_teardown_gpadl(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700222 net_device->send_buf_gpadl_handle);
223
224 /* If we failed here, we might as well return and have a leak
225 * rather than continue and a bugchk
226 */
227 if (ret != 0) {
228 netdev_err(ndev,
229 "unable to teardown send buffer's gpadl\n");
230 return ret;
231 }
Dave Jones2f184232014-06-16 16:59:02 -0400232 net_device->send_buf_gpadl_handle = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700233 }
234 if (net_device->send_buf) {
Haiyang Zhangc51ed18252014-12-19 18:25:18 -0800235 /* Free up the send buffer */
KY Srinivasan06b47aa2014-08-02 10:42:02 -0700236 vfree(net_device->send_buf);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700237 net_device->send_buf = NULL;
238 }
239 kfree(net_device->send_section_map);
240
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700241 return ret;
242}
243
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700244static int netvsc_init_buf(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700245{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700246 int ret = 0;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800247 struct netvsc_device *net_device;
248 struct nvsp_message *init_packet;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700249 struct net_device *ndev;
K. Y. Srinivasan0a726c22015-05-28 17:08:06 -0700250 int node;
Hank Janssenfceaf242009-07-13 15:34:54 -0700251
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800252 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700253 if (!net_device)
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700254 return -ENODEV;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200255 ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700256
K. Y. Srinivasan0a726c22015-05-28 17:08:06 -0700257 node = cpu_to_node(device->channel->target_cpu);
258 net_device->recv_buf = vzalloc_node(net_device->recv_buf_size, node);
259 if (!net_device->recv_buf)
260 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
261
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800262 if (!net_device->recv_buf) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700263 netdev_err(ndev, "unable to allocate receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700264 "buffer of size %d\n", net_device->recv_buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700265 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800266 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700267 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700268
Bill Pemberton454f18a2009-07-27 16:47:24 -0400269 /*
270 * Establish the gpadl handle for this buffer on this
271 * channel. Note: This call uses the vmbus connection rather
272 * than the channel to establish the gpadl handle.
273 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800274 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
275 net_device->recv_buf_size,
276 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700277 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700278 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700279 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800280 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700281 }
282
Hank Janssenfceaf242009-07-13 15:34:54 -0700283
Bill Pemberton454f18a2009-07-27 16:47:24 -0400284 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800285 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700286
Haiyang Zhang85799a32010-12-10 12:03:54 -0800287 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700288
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800289 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
290 init_packet->msg.v1_msg.send_recv_buf.
291 gpadl_handle = net_device->recv_buf_gpadl_handle;
292 init_packet->msg.v1_msg.
293 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700294
Bill Pemberton454f18a2009-07-27 16:47:24 -0400295 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800296 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700297 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800298 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800299 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700300 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700301 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700302 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700303 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800304 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700305 }
306
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200307 wait_for_completion(&net_device->channel_init_wait);
Hank Janssenfceaf242009-07-13 15:34:54 -0700308
Bill Pemberton454f18a2009-07-27 16:47:24 -0400309 /* Check the response */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800310 if (init_packet->msg.v1_msg.
311 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700312 netdev_err(ndev, "Unable to complete receive buffer "
Haiyang Zhang8bff33a2011-09-01 12:19:48 -0700313 "initialization with NetVsp - status %d\n",
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800314 init_packet->msg.v1_msg.
315 send_recv_buf_complete.status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700316 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800317 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700318 }
319
Bill Pemberton454f18a2009-07-27 16:47:24 -0400320 /* Parse the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700321
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800322 net_device->recv_section_cnt = init_packet->msg.
323 v1_msg.send_recv_buf_complete.num_sections;
Hank Janssenfceaf242009-07-13 15:34:54 -0700324
Haiyang Zhangc1813202011-11-30 07:19:07 -0800325 net_device->recv_section = kmemdup(
326 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
327 net_device->recv_section_cnt *
328 sizeof(struct nvsp_1_receive_buffer_section),
329 GFP_KERNEL);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800330 if (net_device->recv_section == NULL) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700331 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800332 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700333 }
334
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700335 /*
336 * For 1st release, there should only be 1 section that represents the
337 * entire receive buffer
338 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800339 if (net_device->recv_section_cnt != 1 ||
340 net_device->recv_section->offset != 0) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700341 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800342 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700343 }
344
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700345 /* Now setup the send buffer.
346 */
K. Y. Srinivasan5defde52015-05-28 17:08:07 -0700347 net_device->send_buf = vzalloc_node(net_device->send_buf_size, node);
348 if (!net_device->send_buf)
349 net_device->send_buf = vzalloc(net_device->send_buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700350 if (!net_device->send_buf) {
351 netdev_err(ndev, "unable to allocate send "
352 "buffer of size %d\n", net_device->send_buf_size);
353 ret = -ENOMEM;
354 goto cleanup;
355 }
356
357 /* Establish the gpadl handle for this buffer on this
358 * channel. Note: This call uses the vmbus connection rather
359 * than the channel to establish the gpadl handle.
360 */
361 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
362 net_device->send_buf_size,
363 &net_device->send_buf_gpadl_handle);
364 if (ret != 0) {
365 netdev_err(ndev,
366 "unable to establish send buffer's gpadl\n");
367 goto cleanup;
368 }
369
370 /* Notify the NetVsp of the gpadl handle */
371 init_packet = &net_device->channel_init_pkt;
372 memset(init_packet, 0, sizeof(struct nvsp_message));
373 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
Haiyang Zhangc51ed18252014-12-19 18:25:18 -0800374 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700375 net_device->send_buf_gpadl_handle;
Haiyang Zhangc51ed18252014-12-19 18:25:18 -0800376 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700377
378 /* Send the gpadl notification request */
379 ret = vmbus_sendpacket(device->channel, init_packet,
380 sizeof(struct nvsp_message),
381 (unsigned long)init_packet,
382 VM_PKT_DATA_INBAND,
383 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
384 if (ret != 0) {
385 netdev_err(ndev,
386 "unable to send send buffer's gpadl to netvsp\n");
387 goto cleanup;
388 }
389
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200390 wait_for_completion(&net_device->channel_init_wait);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700391
392 /* Check the response */
393 if (init_packet->msg.v1_msg.
394 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
395 netdev_err(ndev, "Unable to complete send buffer "
396 "initialization with NetVsp - status %d\n",
397 init_packet->msg.v1_msg.
Haiyang Zhangc51ed18252014-12-19 18:25:18 -0800398 send_send_buf_complete.status);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700399 ret = -EINVAL;
400 goto cleanup;
401 }
402
403 /* Parse the response */
404 net_device->send_section_size = init_packet->msg.
405 v1_msg.send_send_buf_complete.section_size;
406
407 /* Section count is simply the size divided by the section size.
408 */
409 net_device->send_section_cnt =
410 net_device->send_buf_size/net_device->send_section_size;
411
412 dev_info(&device->device, "Send section size: %d, Section count:%d\n",
413 net_device->send_section_size, net_device->send_section_cnt);
414
415 /* Setup state for managing the send buffer. */
416 net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
417 BITS_PER_LONG);
418
419 net_device->send_section_map =
420 kzalloc(net_device->map_words * sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800421 if (net_device->send_section_map == NULL) {
422 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700423 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800424 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700425
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800426 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700427
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800428cleanup:
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200429 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700430
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800431exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700432 return ret;
433}
434
Hank Janssenfceaf242009-07-13 15:34:54 -0700435
Haiyang Zhangf157e782011-12-15 13:45:16 -0800436/* Negotiate NVSP protocol version */
437static int negotiate_nvsp_ver(struct hv_device *device,
438 struct netvsc_device *net_device,
439 struct nvsp_message *init_packet,
440 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700441{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200442 struct net_device *ndev = hv_get_drvdata(device);
Nicholas Mc Guire7390fe92015-01-25 15:46:31 +0100443 int ret;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800444
445 memset(init_packet, 0, sizeof(struct nvsp_message));
446 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
447 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
448 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
449
450 /* Send the init request */
451 ret = vmbus_sendpacket(device->channel, init_packet,
452 sizeof(struct nvsp_message),
453 (unsigned long)init_packet,
454 VM_PKT_DATA_INBAND,
455 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
456
457 if (ret != 0)
458 return ret;
459
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200460 wait_for_completion(&net_device->channel_init_wait);
Haiyang Zhangf157e782011-12-15 13:45:16 -0800461
462 if (init_packet->msg.init_msg.init_complete.status !=
463 NVSP_STAT_SUCCESS)
464 return -EINVAL;
465
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800466 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800467 return 0;
468
Haiyang Zhang71790a22015-07-24 10:08:40 -0700469 /* NVSPv2 or later: Send NDIS config */
Haiyang Zhangf157e782011-12-15 13:45:16 -0800470 memset(init_packet, 0, sizeof(struct nvsp_message));
471 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200472 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000473 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800474
Haiyang Zhang71790a22015-07-24 10:08:40 -0700475 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5)
476 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
477
Haiyang Zhangf157e782011-12-15 13:45:16 -0800478 ret = vmbus_sendpacket(device->channel, init_packet,
479 sizeof(struct nvsp_message),
480 (unsigned long)init_packet,
481 VM_PKT_DATA_INBAND, 0);
482
483 return ret;
484}
485
486static int netvsc_connect_vsp(struct hv_device *device)
487{
488 int ret;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800489 struct netvsc_device *net_device;
490 struct nvsp_message *init_packet;
491 int ndis_version;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800492 u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
493 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
494 int i, num_ver = 4; /* number of different NVSP versions */
Hank Janssenfceaf242009-07-13 15:34:54 -0700495
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800496 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700497 if (!net_device)
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700498 return -ENODEV;
Hank Janssenfceaf242009-07-13 15:34:54 -0700499
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800500 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700501
Haiyang Zhangf157e782011-12-15 13:45:16 -0800502 /* Negotiate the latest NVSP protocol supported */
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800503 for (i = num_ver - 1; i >= 0; i--)
504 if (negotiate_nvsp_ver(device, net_device, init_packet,
505 ver_list[i]) == 0) {
506 net_device->nvsp_version = ver_list[i];
507 break;
508 }
509
510 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700511 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800512 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700513 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800514
515 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
516
Bill Pemberton454f18a2009-07-27 16:47:24 -0400517 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800518 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700519
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800520 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700521 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800522 else
523 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700524
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800525 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
526 init_packet->msg.v1_msg.
527 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800528 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800529 init_packet->msg.v1_msg.
530 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800531 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700532
Bill Pemberton454f18a2009-07-27 16:47:24 -0400533 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800534 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800535 sizeof(struct nvsp_message),
536 (unsigned long)init_packet,
537 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700538 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800539 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700540
Bill Pemberton454f18a2009-07-27 16:47:24 -0400541 /* Post the big receive buffer to NetVSP */
Haiyang Zhang99d30162014-03-09 16:10:59 -0700542 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
543 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
544 else
545 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700546 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
Haiyang Zhang99d30162014-03-09 16:10:59 -0700547
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700548 ret = netvsc_init_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700549
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800550cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700551 return ret;
552}
553
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200554static void netvsc_disconnect_vsp(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700555{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200556 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700557}
558
Hank Janssen3e189512010-03-04 22:11:00 +0000559/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800560 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700561 */
K. Y. Srinivasan905620d2011-05-10 07:54:54 -0700562int netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700563{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200564 struct net_device *ndev = hv_get_drvdata(device);
565 struct net_device_context *net_device_ctx = netdev_priv(ndev);
566 struct netvsc_device *net_device = net_device_ctx->nvdev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700567
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200568 netvsc_disconnect_vsp(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700569
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200570 net_device_ctx->nvdev = NULL;
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700571
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700572 /*
573 * At this point, no one should be accessing net_device
574 * except in here
575 */
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700576 dev_notice(&device->device, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700577
Bill Pemberton454f18a2009-07-27 16:47:24 -0400578 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800579 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700580
Bill Pemberton454f18a2009-07-27 16:47:24 -0400581 /* Release all resources */
Markus Elfringaa99c472014-11-25 22:33:45 +0100582 vfree(net_device->sub_cb_buf);
Haiyang Zhangf90251c2014-08-15 19:18:19 +0000583 free_netvsc_device(net_device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700584 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700585}
586
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000587
588#define RING_AVAIL_PERCENT_HIWATER 20
589#define RING_AVAIL_PERCENT_LOWATER 10
590
591/*
592 * Get the percentage of available bytes to write in the ring.
593 * The return value is in range from 0 to 100.
594 */
595static inline u32 hv_ringbuf_avail_percent(
596 struct hv_ring_buffer_info *ring_info)
597{
598 u32 avail_read, avail_write;
599
600 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
601
602 return avail_write * 100 / ring_info->ring_datasize;
603}
604
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700605static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
606 u32 index)
607{
608 sync_change_bit(index, net_device->send_section_map);
609}
610
KY Srinivasan97c17232014-02-16 16:38:44 -0800611static void netvsc_send_completion(struct netvsc_device *net_device,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800612 struct vmbus_channel *incoming_channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800613 struct hv_device *device,
Haiyang Zhang85799a32010-12-10 12:03:54 -0800614 struct vmpacket_descriptor *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700615{
Haiyang Zhang85799a32010-12-10 12:03:54 -0800616 struct nvsp_message *nvsp_packet;
617 struct hv_netvsc_packet *nvsc_packet;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200618 struct net_device *ndev = hv_get_drvdata(device);
619 struct net_device_context *net_device_ctx = netdev_priv(ndev);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700620 u32 send_index;
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800621 struct sk_buff *skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700622
Haiyang Zhang85799a32010-12-10 12:03:54 -0800623 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
Haiyang Zhang415f2282011-01-26 12:12:13 -0800624 (packet->offset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -0700625
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800626 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
627 (nvsp_packet->hdr.msg_type ==
628 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
629 (nvsp_packet->hdr.msg_type ==
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700630 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
631 (nvsp_packet->hdr.msg_type ==
632 NVSP_MSG5_TYPE_SUBCHANNEL)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400633 /* Copy the response back */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800634 memcpy(&net_device->channel_init_pkt, nvsp_packet,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700635 sizeof(struct nvsp_message));
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700636 complete(&net_device->channel_init_wait);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800637 } else if (nvsp_packet->hdr.msg_type ==
638 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000639 int num_outstanding_sends;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700640 u16 q_idx = 0;
641 struct vmbus_channel *channel = device->channel;
642 int queue_sends;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000643
Bill Pemberton454f18a2009-07-27 16:47:24 -0400644 /* Get the send context */
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800645 skb = (struct sk_buff *)(unsigned long)packet->trans_id;
Hank Janssenfceaf242009-07-13 15:34:54 -0700646
Bill Pemberton454f18a2009-07-27 16:47:24 -0400647 /* Notify the layer above us */
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800648 if (skb) {
649 nvsc_packet = (struct hv_netvsc_packet *) skb->cb;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700650 send_index = nvsc_packet->send_buf_index;
651 if (send_index != NETVSC_INVALID_INDEX)
652 netvsc_free_send_slot(net_device, send_index);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700653 q_idx = nvsc_packet->q_idx;
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800654 channel = incoming_channel;
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800655 dev_kfree_skb_any(skb);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700656 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700657
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000658 num_outstanding_sends =
659 atomic_dec_return(&net_device->num_outstanding_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700660 queue_sends = atomic_dec_return(&net_device->
661 queue_sends[q_idx]);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800662
Haiyang Zhangdc5cd892012-06-04 06:42:38 +0000663 if (net_device->destroy && num_outstanding_sends == 0)
664 wake_up(&net_device->wait_drain);
665
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700666 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200667 !net_device_ctx->start_remove &&
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700668 (hv_ringbuf_avail_percent(&channel->outbound) >
669 RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
670 netif_tx_wake_queue(netdev_get_tx_queue(
671 ndev, q_idx));
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700672 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700673 netdev_err(ndev, "Unknown send completion packet type- "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700674 "%d received!!\n", nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700675 }
676
Hank Janssenfceaf242009-07-13 15:34:54 -0700677}
678
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700679static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
680{
681 unsigned long index;
682 u32 max_words = net_device->map_words;
683 unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
684 u32 section_cnt = net_device->send_section_cnt;
685 int ret_val = NETVSC_INVALID_INDEX;
686 int i;
687 int prev_val;
688
689 for (i = 0; i < max_words; i++) {
690 if (!~(map_addr[i]))
691 continue;
692 index = ffz(map_addr[i]);
693 prev_val = sync_test_and_set_bit(index, &map_addr[i]);
694 if (prev_val)
695 continue;
696 if ((index + (i * BITS_PER_LONG)) >= section_cnt)
697 break;
698 ret_val = (index + (i * BITS_PER_LONG));
699 break;
700 }
701 return ret_val;
702}
703
Lad, Prabhakarda19fcd2015-02-05 15:06:33 +0000704static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
705 unsigned int section_index,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700706 u32 pend_size,
KY Srinivasan24476762015-12-01 16:43:06 -0800707 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800708 struct rndis_message *rndis_msg,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800709 struct hv_page_buffer **pb,
710 struct sk_buff *skb)
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700711{
712 char *start = net_device->send_buf;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700713 char *dest = start + (section_index * net_device->send_section_size)
714 + pend_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700715 int i;
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800716 bool is_data_pkt = (skb != NULL) ? true : false;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800717 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700718 u32 msg_size = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700719 u32 padding = 0;
720 u32 remain = packet->total_data_buflen % net_device->pkt_align;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700721 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
722 packet->page_buf_cnt;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700723
724 /* Add padding */
KY Srinivasanbde79be2015-12-01 16:43:17 -0800725 if (is_data_pkt && xmit_more && remain &&
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700726 !packet->cp_partial) {
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700727 padding = net_device->pkt_align - remain;
KY Srinivasan24476762015-12-01 16:43:06 -0800728 rndis_msg->msg_len += padding;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700729 packet->total_data_buflen += padding;
730 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700731
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700732 for (i = 0; i < page_count; i++) {
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800733 char *src = phys_to_virt((*pb)[i].pfn << PAGE_SHIFT);
734 u32 offset = (*pb)[i].offset;
735 u32 len = (*pb)[i].len;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700736
737 memcpy(dest, (src + offset), len);
738 msg_size += len;
739 dest += len;
740 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700741
742 if (padding) {
743 memset(dest, 0, padding);
744 msg_size += padding;
745 }
746
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700747 return msg_size;
748}
749
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700750static inline int netvsc_send_pkt(
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200751 struct hv_device *device,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700752 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800753 struct netvsc_device *net_device,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800754 struct hv_page_buffer **pb,
755 struct sk_buff *skb)
Hank Janssenfceaf242009-07-13 15:34:54 -0700756{
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700757 struct nvsp_message nvmsg;
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700758 u16 q_idx = packet->q_idx;
Vitaly Kuznetsov8b9fbe12015-12-01 16:43:11 -0800759 struct vmbus_channel *out_channel = net_device->chn_table[q_idx];
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200760 struct net_device *ndev = hv_get_drvdata(device);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700761 u64 req_id;
762 int ret;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700763 struct hv_page_buffer *pgbuf;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700764 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
KY Srinivasanbde79be2015-12-01 16:43:17 -0800765 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700766
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700767 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800768 if (skb != NULL) {
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700769 /* 0 is RMC_DATA; */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700770 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 0;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700771 } else {
772 /* 1 is RMC_CONTROL; */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700773 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 1;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700774 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700775
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700776 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
777 packet->send_buf_index;
778 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
779 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
780 else
781 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size =
782 packet->total_data_buflen;
Hank Janssenfceaf242009-07-13 15:34:54 -0700783
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800784 req_id = (ulong)skb;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000785
Haiyang Zhangc3582a22014-12-01 13:28:39 -0800786 if (out_channel->rescind)
787 return -ENODEV;
788
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700789 /*
790 * It is possible that once we successfully place this packet
791 * on the ringbuffer, we may stop the queue. In that case, we want
792 * to notify the host independent of the xmit_more flag. We don't
793 * need to be precise here; in the worst case we may signal the host
794 * unnecessarily.
795 */
796 if (ring_avail < (RING_AVAIL_PERCENT_LOWATER + 1))
KY Srinivasanbde79be2015-12-01 16:43:17 -0800797 xmit_more = false;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700798
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800799 if (packet->page_buf_cnt) {
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800800 pgbuf = packet->cp_partial ? (*pb) +
801 packet->rmsg_pgcnt : (*pb);
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700802 ret = vmbus_sendpacket_pagebuffer_ctl(out_channel,
803 pgbuf,
804 packet->page_buf_cnt,
805 &nvmsg,
806 sizeof(struct nvsp_message),
807 req_id,
808 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
KY Srinivasanbde79be2015-12-01 16:43:17 -0800809 !xmit_more);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700810 } else {
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700811 ret = vmbus_sendpacket_ctl(out_channel, &nvmsg,
812 sizeof(struct nvsp_message),
813 req_id,
814 VM_PKT_DATA_INBAND,
815 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
KY Srinivasanbde79be2015-12-01 16:43:17 -0800816 !xmit_more);
Hank Janssenfceaf242009-07-13 15:34:54 -0700817 }
818
Haiyang Zhang1d068252011-12-02 11:56:25 -0800819 if (ret == 0) {
820 atomic_inc(&net_device->num_outstanding_sends);
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700821 atomic_inc(&net_device->queue_sends[q_idx]);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700822
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700823 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
824 netif_tx_stop_queue(netdev_get_tx_queue(ndev, q_idx));
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700825
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000826 if (atomic_read(&net_device->
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700827 queue_sends[q_idx]) < 1)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700828 netif_tx_wake_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700829 ndev, q_idx));
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000830 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800831 } else if (ret == -EAGAIN) {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700832 netif_tx_stop_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700833 ndev, q_idx));
834 if (atomic_read(&net_device->queue_sends[q_idx]) < 1) {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700835 netif_tx_wake_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700836 ndev, q_idx));
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000837 ret = -ENOSPC;
838 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800839 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700840 netdev_err(ndev, "Unable to send packet %p ret %d\n",
Haiyang Zhang85799a32010-12-10 12:03:54 -0800841 packet, ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800842 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700843
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700844 return ret;
845}
846
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800847/* Move packet out of multi send data (msd), and clear msd */
848static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
849 struct sk_buff **msd_skb,
850 struct multi_send_data *msdp)
851{
852 *msd_skb = msdp->skb;
853 *msd_send = msdp->pkt;
854 msdp->skb = NULL;
855 msdp->pkt = NULL;
856 msdp->count = 0;
857}
858
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700859int netvsc_send(struct hv_device *device,
KY Srinivasan24476762015-12-01 16:43:06 -0800860 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800861 struct rndis_message *rndis_msg,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800862 struct hv_page_buffer **pb,
863 struct sk_buff *skb)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700864{
865 struct netvsc_device *net_device;
866 int ret = 0, m_ret = 0;
867 struct vmbus_channel *out_channel;
868 u16 q_idx = packet->q_idx;
869 u32 pktlen = packet->total_data_buflen, msd_len = 0;
870 unsigned int section_index = NETVSC_INVALID_INDEX;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700871 struct multi_send_data *msdp;
872 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800873 struct sk_buff *msd_skb = NULL;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700874 bool try_batch;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800875 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700876
877 net_device = get_outbound_net_device(device);
878 if (!net_device)
879 return -ENODEV;
880
Vitaly Kuznetsov8b9fbe12015-12-01 16:43:11 -0800881 out_channel = net_device->chn_table[q_idx];
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800882
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700883 packet->send_buf_index = NETVSC_INVALID_INDEX;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700884 packet->cp_partial = false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700885
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800886 /* Send control message directly without accessing msd (Multi-Send
887 * Data) field which may be changed during data packet processing.
888 */
889 if (!skb) {
890 cur_send = packet;
891 goto send_now;
892 }
893
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700894 msdp = &net_device->msd[q_idx];
895
896 /* batch packets in send buffer if possible */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700897 if (msdp->pkt)
898 msd_len = msdp->pkt->total_data_buflen;
899
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800900 try_batch = (skb != NULL) && msd_len > 0 && msdp->count <
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700901 net_device->max_pkt;
902
903 if (try_batch && msd_len + pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700904 net_device->send_section_size) {
905 section_index = msdp->pkt->send_buf_index;
906
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700907 } else if (try_batch && msd_len + packet->rmsg_size <
908 net_device->send_section_size) {
909 section_index = msdp->pkt->send_buf_index;
910 packet->cp_partial = true;
911
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800912 } else if ((skb != NULL) && pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700913 net_device->send_section_size) {
914 section_index = netvsc_get_next_send_section(net_device);
915 if (section_index != NETVSC_INVALID_INDEX) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800916 move_pkt_msd(&msd_send, &msd_skb, msdp);
917 msd_len = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700918 }
919 }
920
921 if (section_index != NETVSC_INVALID_INDEX) {
922 netvsc_copy_to_send_buf(net_device,
923 section_index, msd_len,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800924 packet, rndis_msg, pb, skb);
KY Srinivasanb08cc792015-03-29 21:08:42 -0700925
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700926 packet->send_buf_index = section_index;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700927
928 if (packet->cp_partial) {
929 packet->page_buf_cnt -= packet->rmsg_pgcnt;
930 packet->total_data_buflen = msd_len + packet->rmsg_size;
931 } else {
932 packet->page_buf_cnt = 0;
933 packet->total_data_buflen += msd_len;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700934 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700935
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800936 if (msdp->skb)
937 dev_kfree_skb_any(msdp->skb);
Haiyang Zhangee90b812015-04-06 15:22:54 -0700938
KY Srinivasanbde79be2015-12-01 16:43:17 -0800939 if (xmit_more && !packet->cp_partial) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800940 msdp->skb = skb;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700941 msdp->pkt = packet;
942 msdp->count++;
943 } else {
944 cur_send = packet;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800945 msdp->skb = NULL;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700946 msdp->pkt = NULL;
947 msdp->count = 0;
948 }
949 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800950 move_pkt_msd(&msd_send, &msd_skb, msdp);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700951 cur_send = packet;
952 }
953
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700954 if (msd_send) {
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200955 m_ret = netvsc_send_pkt(device, msd_send, net_device,
956 NULL, msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700957
958 if (m_ret != 0) {
959 netvsc_free_send_slot(net_device,
960 msd_send->send_buf_index);
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800961 dev_kfree_skb_any(msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700962 }
963 }
964
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800965send_now:
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700966 if (cur_send)
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200967 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700968
Jerry Snitselaar7aab5152015-05-04 10:57:16 -0700969 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
970 netvsc_free_send_slot(net_device, section_index);
Haiyang Zhangd953ca42015-01-29 12:34:49 -0800971
Hank Janssenfceaf242009-07-13 15:34:54 -0700972 return ret;
973}
974
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700975static void netvsc_send_recv_completion(struct hv_device *device,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700976 struct vmbus_channel *channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800977 struct netvsc_device *net_device,
Haiyang Zhang63f69212012-10-02 05:30:23 +0000978 u64 transaction_id, u32 status)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700979{
980 struct nvsp_message recvcompMessage;
981 int retries = 0;
982 int ret;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200983 struct net_device *ndev = hv_get_drvdata(device);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700984
985 recvcompMessage.hdr.msg_type =
986 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
987
Haiyang Zhang63f69212012-10-02 05:30:23 +0000988 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700989
990retry_send_cmplt:
991 /* Send the completion */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700992 ret = vmbus_sendpacket(channel, &recvcompMessage,
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700993 sizeof(struct nvsp_message), transaction_id,
994 VM_PKT_COMP, 0);
995 if (ret == 0) {
996 /* success */
997 /* no-op */
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700998 } else if (ret == -EAGAIN) {
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700999 /* no more room...wait a bit and attempt to retry 3 times */
1000 retries++;
Haiyang Zhangd9871152011-09-01 12:19:41 -07001001 netdev_err(ndev, "unable to send receive completion pkt"
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001002 " (tid %llx)...retrying %d\n", transaction_id, retries);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001003
1004 if (retries < 4) {
1005 udelay(100);
1006 goto retry_send_cmplt;
1007 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001008 netdev_err(ndev, "unable to send receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001009 "completion pkt (tid %llx)...give up retrying\n",
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001010 transaction_id);
1011 }
1012 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001013 netdev_err(ndev, "unable to send receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001014 "completion pkt - %llx\n", transaction_id);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001015 }
1016}
1017
KY Srinivasan97c17232014-02-16 16:38:44 -08001018static void netvsc_receive(struct netvsc_device *net_device,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001019 struct vmbus_channel *channel,
KY Srinivasan97c17232014-02-16 16:38:44 -08001020 struct hv_device *device,
1021 struct vmpacket_descriptor *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -07001022{
Haiyang Zhang85799a32010-12-10 12:03:54 -08001023 struct vmtransfer_page_packet_header *vmxferpage_packet;
1024 struct nvsp_message *nvsp_packet;
Haiyang Zhang4baab262014-04-21 14:54:43 -07001025 struct hv_netvsc_packet nv_pkt;
1026 struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
1027 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -08001028 int i;
1029 int count = 0;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001030 struct net_device *ndev = hv_get_drvdata(device);
KY Srinivasanc4b20c62015-12-01 16:43:07 -08001031 void *data;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -07001032
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001033 /*
1034 * All inbound packets other than send completion should be xfer page
1035 * packet
1036 */
Haiyang Zhang415f2282011-01-26 12:12:13 -08001037 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001038 netdev_err(ndev, "Unknown packet type received - %d\n",
Haiyang Zhang415f2282011-01-26 12:12:13 -08001039 packet->type);
Hank Janssenfceaf242009-07-13 15:34:54 -07001040 return;
1041 }
1042
Haiyang Zhang85799a32010-12-10 12:03:54 -08001043 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
Haiyang Zhang415f2282011-01-26 12:12:13 -08001044 (packet->offset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -07001045
Bill Pemberton454f18a2009-07-27 16:47:24 -04001046 /* Make sure this is a valid nvsp packet */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -08001047 if (nvsp_packet->hdr.msg_type !=
1048 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001049 netdev_err(ndev, "Unknown nvsp packet type received-"
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001050 " %d\n", nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -07001051 return;
1052 }
1053
Haiyang Zhang85799a32010-12-10 12:03:54 -08001054 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
Hank Janssenfceaf242009-07-13 15:34:54 -07001055
Haiyang Zhang415f2282011-01-26 12:12:13 -08001056 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001057 netdev_err(ndev, "Invalid xfer page set id - "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001058 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
Haiyang Zhang415f2282011-01-26 12:12:13 -08001059 vmxferpage_packet->xfer_pageset_id);
Hank Janssenfceaf242009-07-13 15:34:54 -07001060 return;
1061 }
1062
Haiyang Zhang4baab262014-04-21 14:54:43 -07001063 count = vmxferpage_packet->range_cnt;
Hank Janssenfceaf242009-07-13 15:34:54 -07001064
Bill Pemberton454f18a2009-07-27 16:47:24 -04001065 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -07001066 for (i = 0; i < count; i++) {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001067 /* Initialize the netvsc packet */
KY Srinivasanc4b20c62015-12-01 16:43:07 -08001068 data = (void *)((unsigned long)net_device->
Haiyang Zhang45326342011-12-15 13:45:15 -08001069 recv_buf + vmxferpage_packet->ranges[i].byte_offset);
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -08001070 netvsc_packet->total_data_buflen =
Haiyang Zhang415f2282011-01-26 12:12:13 -08001071 vmxferpage_packet->ranges[i].byte_count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001072
Bill Pemberton454f18a2009-07-27 16:47:24 -04001073 /* Pass it to the upper layer */
KY Srinivasan10082f92015-12-01 16:43:18 -08001074 status = rndis_filter_receive(device, netvsc_packet, &data,
1075 channel);
Hank Janssenfceaf242009-07-13 15:34:54 -07001076
Hank Janssenfceaf242009-07-13 15:34:54 -07001077 }
1078
Haiyang Zhang4baab262014-04-21 14:54:43 -07001079 netvsc_send_recv_completion(device, channel, net_device,
1080 vmxferpage_packet->d.trans_id, status);
Hank Janssenfceaf242009-07-13 15:34:54 -07001081}
1082
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001083
1084static void netvsc_send_table(struct hv_device *hdev,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001085 struct nvsp_message *nvmsg)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001086{
1087 struct netvsc_device *nvscdev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001088 struct net_device *ndev = hv_get_drvdata(hdev);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001089 int i;
1090 u32 count, *tab;
1091
1092 nvscdev = get_outbound_net_device(hdev);
1093 if (!nvscdev)
1094 return;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001095
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001096 count = nvmsg->msg.v5_msg.send_table.count;
1097 if (count != VRSS_SEND_TAB_SIZE) {
1098 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1099 return;
1100 }
1101
1102 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1103 nvmsg->msg.v5_msg.send_table.offset);
1104
1105 for (i = 0; i < count; i++)
1106 nvscdev->send_table[i] = tab[i];
1107}
1108
Haiyang Zhang71790a22015-07-24 10:08:40 -07001109static void netvsc_send_vf(struct netvsc_device *nvdev,
1110 struct nvsp_message *nvmsg)
1111{
1112 nvdev->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1113 nvdev->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
1114}
1115
1116static inline void netvsc_receive_inband(struct hv_device *hdev,
1117 struct netvsc_device *nvdev,
1118 struct nvsp_message *nvmsg)
1119{
1120 switch (nvmsg->hdr.msg_type) {
1121 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1122 netvsc_send_table(hdev, nvmsg);
1123 break;
1124
1125 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
1126 netvsc_send_vf(nvdev, nvmsg);
1127 break;
1128 }
1129}
1130
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001131static void netvsc_process_raw_pkt(struct hv_device *device,
1132 struct vmbus_channel *channel,
1133 struct netvsc_device *net_device,
1134 struct net_device *ndev,
1135 u64 request_id,
1136 struct vmpacket_descriptor *desc)
1137{
1138 struct nvsp_message *nvmsg;
1139
1140 nvmsg = (struct nvsp_message *)((unsigned long)
1141 desc + (desc->offset8 << 3));
1142
1143 switch (desc->type) {
1144 case VM_PKT_COMP:
1145 netvsc_send_completion(net_device, channel, device, desc);
1146 break;
1147
1148 case VM_PKT_DATA_USING_XFER_PAGES:
1149 netvsc_receive(net_device, channel, device, desc);
1150 break;
1151
1152 case VM_PKT_DATA_INBAND:
1153 netvsc_receive_inband(device, net_device, nvmsg);
1154 break;
1155
1156 default:
1157 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
1158 desc->type, request_id);
1159 break;
1160 }
1161}
1162
1163
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001164void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001165{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001166 int ret;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001167 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1168 struct hv_device *device;
Haiyang Zhang85799a32010-12-10 12:03:54 -08001169 struct netvsc_device *net_device;
1170 u32 bytes_recvd;
1171 u64 request_id;
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -07001172 struct vmpacket_descriptor *desc;
Bill Pembertonc6fcf0b2010-04-27 16:23:47 -04001173 unsigned char *buffer;
1174 int bufferlen = NETVSC_PACKET_SIZE;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001175 struct net_device *ndev;
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001176 bool need_to_commit = false;
Hank Janssenfceaf242009-07-13 15:34:54 -07001177
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001178 if (channel->primary_channel != NULL)
1179 device = channel->primary_channel->device_obj;
1180 else
1181 device = channel->device_obj;
1182
Haiyang Zhang5a71ae32010-12-10 12:03:55 -08001183 net_device = get_inbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001184 if (!net_device)
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001185 return;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001186 ndev = hv_get_drvdata(device);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001187 buffer = get_per_channel_state(channel);
Hank Janssenfceaf242009-07-13 15:34:54 -07001188
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001189 do {
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001190 desc = get_next_pkt_raw(channel);
1191 if (desc != NULL) {
1192 netvsc_process_raw_pkt(device,
1193 channel,
1194 net_device,
1195 ndev,
1196 desc->trans_id,
1197 desc);
1198
1199 put_pkt_raw(channel, desc);
1200 need_to_commit = true;
1201 continue;
1202 }
1203 if (need_to_commit) {
1204 need_to_commit = false;
1205 commit_rd_index(channel);
1206 }
1207
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001208 ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
Haiyang Zhang85799a32010-12-10 12:03:54 -08001209 &bytes_recvd, &request_id);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001210 if (ret == 0) {
Haiyang Zhang85799a32010-12-10 12:03:54 -08001211 if (bytes_recvd > 0) {
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001212 desc = (struct vmpacket_descriptor *)buffer;
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001213 netvsc_process_raw_pkt(device,
1214 channel,
1215 net_device,
1216 ndev,
1217 request_id,
1218 desc);
Hank Janssenfceaf242009-07-13 15:34:54 -07001219
Hank Janssenfceaf242009-07-13 15:34:54 -07001220
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001221 } else {
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001222 /*
1223 * We are done for this pass.
1224 */
Hank Janssenfceaf242009-07-13 15:34:54 -07001225 break;
1226 }
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001227
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -07001228 } else if (ret == -ENOBUFS) {
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001229 if (bufferlen > NETVSC_PACKET_SIZE)
1230 kfree(buffer);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001231 /* Handle large packet */
Haiyang Zhang85799a32010-12-10 12:03:54 -08001232 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001233 if (buffer == NULL) {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001234 /* Try again next time around */
Haiyang Zhangd9871152011-09-01 12:19:41 -07001235 netdev_err(ndev,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001236 "unable to allocate buffer of size "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001237 "(%d)!!\n", bytes_recvd);
Hank Janssenfceaf242009-07-13 15:34:54 -07001238 break;
1239 }
1240
Haiyang Zhang85799a32010-12-10 12:03:54 -08001241 bufferlen = bytes_recvd;
Hank Janssenfceaf242009-07-13 15:34:54 -07001242 }
1243 } while (1);
1244
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001245 if (bufferlen > NETVSC_PACKET_SIZE)
1246 kfree(buffer);
Hank Janssenfceaf242009-07-13 15:34:54 -07001247 return;
1248}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001249
1250/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001251 * netvsc_device_add - Callback when the device belonging to this
1252 * driver is added
1253 */
K. Y. Srinivasan7bd23a42011-05-10 07:54:53 -07001254int netvsc_device_add(struct hv_device *device, void *additional_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001255{
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001256 int i, ret = 0;
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001257 int ring_size =
1258 ((struct netvsc_device_info *)additional_info)->ring_size;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001259 struct netvsc_device *net_device;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001260 struct net_device *ndev = hv_get_drvdata(device);
1261 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001262
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001263 net_device = alloc_net_device();
Dan Carpenterb1c84922014-09-04 14:11:23 +03001264 if (!net_device)
1265 return -ENOMEM;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001266
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001267 net_device->ring_size = ring_size;
1268
Haiyang Zhangb637e022011-04-21 12:30:45 -07001269 /* Initialize the NetVSC channel extension */
K. Y. Srinivasan35abb212011-05-10 07:55:41 -07001270 init_completion(&net_device->channel_init_wait);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001271
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001272 set_per_channel_state(device->channel, net_device->cb_buffer);
1273
Haiyang Zhangb637e022011-04-21 12:30:45 -07001274 /* Open the channel */
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001275 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1276 ring_size * PAGE_SIZE, NULL, 0,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001277 netvsc_channel_cb, device->channel);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001278
1279 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001280 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001281 goto cleanup;
1282 }
1283
1284 /* Channel is opened */
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001285 pr_info("hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001286
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001287 /* If we're reopening the device we may have multiple queues, fill the
1288 * chn_table with the default channel to use it before subchannels are
1289 * opened.
1290 */
1291 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
1292 net_device->chn_table[i] = device->channel;
1293
1294 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1295 * populated.
1296 */
1297 wmb();
1298
1299 net_device_ctx->nvdev = net_device;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001300
Haiyang Zhangb637e022011-04-21 12:30:45 -07001301 /* Connect with the NetVsp */
1302 ret = netvsc_connect_vsp(device);
1303 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001304 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001305 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001306 goto close;
1307 }
1308
1309 return ret;
1310
1311close:
1312 /* Now, we can close the channel safely */
1313 vmbus_close(device->channel);
1314
1315cleanup:
Haiyang Zhangf90251c2014-08-15 19:18:19 +00001316 free_netvsc_device(net_device);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001317
1318 return ret;
1319}