blob: 4dd2a1f2da115d9470f3cff87283f35f8e3750db [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
Vitaly Kuznetsov88098832016-05-13 13:55:25 +020062static struct netvsc_device *alloc_net_device(void)
Hank Janssenfceaf242009-07-13 15:34:54 -070063{
Haiyang Zhang85799a32010-12-10 12:03:54 -080064 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070065
Haiyang Zhang85799a32010-12-10 12:03:54 -080066 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
67 if (!net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -070068 return NULL;
69
stephen hemmingerb8b835a2017-01-24 13:06:07 -080070 net_device->chan_table[0].mrc.buf
71 = vzalloc(NETVSC_RECVSLOT_MAX * sizeof(struct recv_comp_data));
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070072
Haiyang Zhangdc5cd892012-06-04 06:42:38 +000073 init_waitqueue_head(&net_device->wait_drain);
K. Y. Srinivasanc38b9c72011-08-27 11:31:12 -070074 net_device->destroy = false;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070075 atomic_set(&net_device->open_cnt, 0);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -070076 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
77 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
Stephen Hemmingerfd612602016-08-23 12:17:51 -070078 init_completion(&net_device->channel_init_wait);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -070079
Haiyang Zhang85799a32010-12-10 12:03:54 -080080 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070081}
82
Haiyang Zhangf90251c2014-08-15 19:18:19 +000083static void free_netvsc_device(struct netvsc_device *nvdev)
84{
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070085 int i;
86
87 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
stephen hemmingerb8b835a2017-01-24 13:06:07 -080088 vfree(nvdev->chan_table[i].mrc.buf);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070089
Haiyang Zhangf90251c2014-08-15 19:18:19 +000090 kfree(nvdev);
91}
92
Haiyang Zhang5a71ae32010-12-10 12:03:55 -080093static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -070094{
Vitaly Kuznetsov26254662016-06-03 17:50:59 +020095 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
Hank Janssenfceaf242009-07-13 15:34:54 -070096
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -070097 if (net_device && net_device->destroy)
Haiyang Zhang85799a32010-12-10 12:03:54 -080098 net_device = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -070099
Haiyang Zhang85799a32010-12-10 12:03:54 -0800100 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700101}
102
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800103static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700104{
Vitaly Kuznetsov26254662016-06-03 17:50:59 +0200105 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700106
107 if (!net_device)
108 goto get_in_err;
109
110 if (net_device->destroy &&
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700111 atomic_read(&net_device->num_outstanding_sends) == 0 &&
112 atomic_read(&net_device->num_outstanding_recvs) == 0)
Haiyang Zhang85799a32010-12-10 12:03:54 -0800113 net_device = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700114
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700115get_in_err:
Haiyang Zhang85799a32010-12-10 12:03:54 -0800116 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700117}
118
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700119static void netvsc_destroy_buf(struct hv_device *device)
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700120{
121 struct nvsp_message *revoke_packet;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200122 struct net_device *ndev = hv_get_drvdata(device);
Vitaly Kuznetsov26254662016-06-03 17:50:59 +0200123 struct netvsc_device *net_device = net_device_to_netvsc_device(ndev);
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700124 int ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700125
126 /*
127 * If we got a section count, it means we received a
128 * SendReceiveBufferComplete msg (ie sent
129 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
130 * to send a revoke msg here
131 */
132 if (net_device->recv_section_cnt) {
133 /* Send the revoke receive buffer */
134 revoke_packet = &net_device->revoke_packet;
135 memset(revoke_packet, 0, sizeof(struct nvsp_message));
136
137 revoke_packet->hdr.msg_type =
138 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
139 revoke_packet->msg.v1_msg.
140 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
141
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200142 ret = vmbus_sendpacket(device->channel,
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700143 revoke_packet,
144 sizeof(struct nvsp_message),
145 (unsigned long)revoke_packet,
146 VM_PKT_DATA_INBAND, 0);
147 /*
148 * If we failed here, we might as well return and
149 * have a leak rather than continue and a bugchk
150 */
151 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700152 netdev_err(ndev, "unable to send "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700153 "revoke receive buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700154 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700155 }
156 }
157
158 /* Teardown the gpadl on the vsp end */
159 if (net_device->recv_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200160 ret = vmbus_teardown_gpadl(device->channel,
161 net_device->recv_buf_gpadl_handle);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700162
163 /* If we failed here, we might as well return and have a leak
164 * rather than continue and a bugchk
165 */
166 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700167 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700168 "unable to teardown receive buffer's gpadl\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700169 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700170 }
171 net_device->recv_buf_gpadl_handle = 0;
172 }
173
174 if (net_device->recv_buf) {
175 /* Free up the receive buffer */
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800176 vfree(net_device->recv_buf);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700177 net_device->recv_buf = NULL;
178 }
179
180 if (net_device->recv_section) {
181 net_device->recv_section_cnt = 0;
182 kfree(net_device->recv_section);
183 net_device->recv_section = NULL;
184 }
185
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700186 /* Deal with the send buffer we may have setup.
187 * If we got a send section size, it means we received a
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800188 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
189 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700190 * to send a revoke msg here
191 */
192 if (net_device->send_section_size) {
193 /* Send the revoke receive buffer */
194 revoke_packet = &net_device->revoke_packet;
195 memset(revoke_packet, 0, sizeof(struct nvsp_message));
196
197 revoke_packet->hdr.msg_type =
198 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800199 revoke_packet->msg.v1_msg.revoke_send_buf.id =
200 NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700201
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200202 ret = vmbus_sendpacket(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700203 revoke_packet,
204 sizeof(struct nvsp_message),
205 (unsigned long)revoke_packet,
206 VM_PKT_DATA_INBAND, 0);
207 /* If we failed here, we might as well return and
208 * have a leak rather than continue and a bugchk
209 */
210 if (ret != 0) {
211 netdev_err(ndev, "unable to send "
212 "revoke send buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700213 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700214 }
215 }
216 /* Teardown the gpadl on the vsp end */
217 if (net_device->send_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200218 ret = vmbus_teardown_gpadl(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700219 net_device->send_buf_gpadl_handle);
220
221 /* If we failed here, we might as well return and have a leak
222 * rather than continue and a bugchk
223 */
224 if (ret != 0) {
225 netdev_err(ndev,
226 "unable to teardown send buffer's gpadl\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700227 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700228 }
Dave Jones2f184232014-06-16 16:59:02 -0400229 net_device->send_buf_gpadl_handle = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700230 }
231 if (net_device->send_buf) {
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800232 /* Free up the send buffer */
KY Srinivasan06b47aa2014-08-02 10:42:02 -0700233 vfree(net_device->send_buf);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700234 net_device->send_buf = NULL;
235 }
236 kfree(net_device->send_section_map);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700237}
238
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700239static int netvsc_init_buf(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700240{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700241 int ret = 0;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800242 struct netvsc_device *net_device;
243 struct nvsp_message *init_packet;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700244 struct net_device *ndev;
K. Y. Srinivasan0a726c22015-05-28 17:08:06 -0700245 int node;
Hank Janssenfceaf242009-07-13 15:34:54 -0700246
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800247 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700248 if (!net_device)
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700249 return -ENODEV;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200250 ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700251
K. Y. Srinivasan0a726c22015-05-28 17:08:06 -0700252 node = cpu_to_node(device->channel->target_cpu);
253 net_device->recv_buf = vzalloc_node(net_device->recv_buf_size, node);
254 if (!net_device->recv_buf)
255 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
256
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800257 if (!net_device->recv_buf) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700258 netdev_err(ndev, "unable to allocate receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700259 "buffer of size %d\n", net_device->recv_buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700260 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800261 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700262 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700263
Bill Pemberton454f18a2009-07-27 16:47:24 -0400264 /*
265 * Establish the gpadl handle for this buffer on this
266 * channel. Note: This call uses the vmbus connection rather
267 * than the channel to establish the gpadl handle.
268 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800269 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
270 net_device->recv_buf_size,
271 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700272 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700273 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700274 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800275 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700276 }
277
Bill Pemberton454f18a2009-07-27 16:47:24 -0400278 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800279 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700280
Haiyang Zhang85799a32010-12-10 12:03:54 -0800281 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700282
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800283 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
284 init_packet->msg.v1_msg.send_recv_buf.
285 gpadl_handle = net_device->recv_buf_gpadl_handle;
286 init_packet->msg.v1_msg.
287 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700288
Bill Pemberton454f18a2009-07-27 16:47:24 -0400289 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800290 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700291 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800292 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800293 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700294 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700295 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700296 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700297 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800298 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700299 }
300
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200301 wait_for_completion(&net_device->channel_init_wait);
Hank Janssenfceaf242009-07-13 15:34:54 -0700302
Bill Pemberton454f18a2009-07-27 16:47:24 -0400303 /* Check the response */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800304 if (init_packet->msg.v1_msg.
305 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700306 netdev_err(ndev, "Unable to complete receive buffer "
Haiyang Zhang8bff33a2011-09-01 12:19:48 -0700307 "initialization with NetVsp - status %d\n",
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800308 init_packet->msg.v1_msg.
309 send_recv_buf_complete.status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700310 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800311 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700312 }
313
Bill Pemberton454f18a2009-07-27 16:47:24 -0400314 /* Parse the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700315
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800316 net_device->recv_section_cnt = init_packet->msg.
317 v1_msg.send_recv_buf_complete.num_sections;
Hank Janssenfceaf242009-07-13 15:34:54 -0700318
Haiyang Zhangc1813202011-11-30 07:19:07 -0800319 net_device->recv_section = kmemdup(
320 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
321 net_device->recv_section_cnt *
322 sizeof(struct nvsp_1_receive_buffer_section),
323 GFP_KERNEL);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800324 if (net_device->recv_section == NULL) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700325 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800326 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700327 }
328
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700329 /*
330 * For 1st release, there should only be 1 section that represents the
331 * entire receive buffer
332 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800333 if (net_device->recv_section_cnt != 1 ||
334 net_device->recv_section->offset != 0) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700335 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800336 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700337 }
338
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700339 /* Now setup the send buffer.
340 */
K. Y. Srinivasan5defde52015-05-28 17:08:07 -0700341 net_device->send_buf = vzalloc_node(net_device->send_buf_size, node);
342 if (!net_device->send_buf)
343 net_device->send_buf = vzalloc(net_device->send_buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700344 if (!net_device->send_buf) {
345 netdev_err(ndev, "unable to allocate send "
346 "buffer of size %d\n", net_device->send_buf_size);
347 ret = -ENOMEM;
348 goto cleanup;
349 }
350
351 /* Establish the gpadl handle for this buffer on this
352 * channel. Note: This call uses the vmbus connection rather
353 * than the channel to establish the gpadl handle.
354 */
355 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
356 net_device->send_buf_size,
357 &net_device->send_buf_gpadl_handle);
358 if (ret != 0) {
359 netdev_err(ndev,
360 "unable to establish send buffer's gpadl\n");
361 goto cleanup;
362 }
363
364 /* Notify the NetVsp of the gpadl handle */
365 init_packet = &net_device->channel_init_pkt;
366 memset(init_packet, 0, sizeof(struct nvsp_message));
367 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800368 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700369 net_device->send_buf_gpadl_handle;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800370 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700371
372 /* Send the gpadl notification request */
373 ret = vmbus_sendpacket(device->channel, init_packet,
374 sizeof(struct nvsp_message),
375 (unsigned long)init_packet,
376 VM_PKT_DATA_INBAND,
377 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
378 if (ret != 0) {
379 netdev_err(ndev,
380 "unable to send send buffer's gpadl to netvsp\n");
381 goto cleanup;
382 }
383
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200384 wait_for_completion(&net_device->channel_init_wait);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700385
386 /* Check the response */
387 if (init_packet->msg.v1_msg.
388 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
389 netdev_err(ndev, "Unable to complete send buffer "
390 "initialization with NetVsp - status %d\n",
391 init_packet->msg.v1_msg.
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800392 send_send_buf_complete.status);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700393 ret = -EINVAL;
394 goto cleanup;
395 }
396
397 /* Parse the response */
398 net_device->send_section_size = init_packet->msg.
399 v1_msg.send_send_buf_complete.section_size;
400
401 /* Section count is simply the size divided by the section size.
402 */
403 net_device->send_section_cnt =
Stephen Hemminger796cc882016-08-23 12:17:47 -0700404 net_device->send_buf_size / net_device->send_section_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700405
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100406 netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
407 net_device->send_section_size, net_device->send_section_cnt);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700408
409 /* Setup state for managing the send buffer. */
410 net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
411 BITS_PER_LONG);
412
Stephen Hemmingere53a9c22016-08-23 12:17:46 -0700413 net_device->send_section_map = kcalloc(net_device->map_words,
414 sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800415 if (net_device->send_section_map == NULL) {
416 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700417 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800418 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700419
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800420 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700421
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800422cleanup:
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200423 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700424
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800425exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700426 return ret;
427}
428
Haiyang Zhangf157e782011-12-15 13:45:16 -0800429/* Negotiate NVSP protocol version */
430static int negotiate_nvsp_ver(struct hv_device *device,
431 struct netvsc_device *net_device,
432 struct nvsp_message *init_packet,
433 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700434{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200435 struct net_device *ndev = hv_get_drvdata(device);
Nicholas Mc Guire7390fe92015-01-25 15:46:31 +0100436 int ret;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800437
438 memset(init_packet, 0, sizeof(struct nvsp_message));
439 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
440 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
441 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
442
443 /* Send the init request */
444 ret = vmbus_sendpacket(device->channel, init_packet,
445 sizeof(struct nvsp_message),
446 (unsigned long)init_packet,
447 VM_PKT_DATA_INBAND,
448 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
449
450 if (ret != 0)
451 return ret;
452
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200453 wait_for_completion(&net_device->channel_init_wait);
Haiyang Zhangf157e782011-12-15 13:45:16 -0800454
455 if (init_packet->msg.init_msg.init_complete.status !=
456 NVSP_STAT_SUCCESS)
457 return -EINVAL;
458
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800459 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800460 return 0;
461
Haiyang Zhang71790a22015-07-24 10:08:40 -0700462 /* NVSPv2 or later: Send NDIS config */
Haiyang Zhangf157e782011-12-15 13:45:16 -0800463 memset(init_packet, 0, sizeof(struct nvsp_message));
464 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200465 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000466 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800467
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700468 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
Haiyang Zhang71790a22015-07-24 10:08:40 -0700469 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
470
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700471 /* Teaming bit is needed to receive link speed updates */
472 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
473 }
474
Haiyang Zhangf157e782011-12-15 13:45:16 -0800475 ret = vmbus_sendpacket(device->channel, init_packet,
476 sizeof(struct nvsp_message),
477 (unsigned long)init_packet,
478 VM_PKT_DATA_INBAND, 0);
479
480 return ret;
481}
482
483static int netvsc_connect_vsp(struct hv_device *device)
484{
485 int ret;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800486 struct netvsc_device *net_device;
487 struct nvsp_message *init_packet;
488 int ndis_version;
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700489 const u32 ver_list[] = {
490 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800491 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700492 int i;
Hank Janssenfceaf242009-07-13 15:34:54 -0700493
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800494 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700495 if (!net_device)
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700496 return -ENODEV;
Hank Janssenfceaf242009-07-13 15:34:54 -0700497
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800498 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700499
Haiyang Zhangf157e782011-12-15 13:45:16 -0800500 /* Negotiate the latest NVSP protocol supported */
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700501 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800502 if (negotiate_nvsp_ver(device, net_device, init_packet,
503 ver_list[i]) == 0) {
504 net_device->nvsp_version = ver_list[i];
505 break;
506 }
507
508 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700509 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800510 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700511 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800512
513 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
514
Bill Pemberton454f18a2009-07-27 16:47:24 -0400515 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800516 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700517
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800518 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700519 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800520 else
521 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700522
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800523 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
524 init_packet->msg.v1_msg.
525 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800526 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800527 init_packet->msg.v1_msg.
528 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800529 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700530
Bill Pemberton454f18a2009-07-27 16:47:24 -0400531 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800532 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800533 sizeof(struct nvsp_message),
534 (unsigned long)init_packet,
535 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700536 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800537 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700538
Bill Pemberton454f18a2009-07-27 16:47:24 -0400539 /* Post the big receive buffer to NetVSP */
Haiyang Zhang99d30162014-03-09 16:10:59 -0700540 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
541 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
542 else
543 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700544 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
Haiyang Zhang99d30162014-03-09 16:10:59 -0700545
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700546 ret = netvsc_init_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700547
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800548cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700549 return ret;
550}
551
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200552static void netvsc_disconnect_vsp(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700553{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200554 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700555}
556
Hank Janssen3e189512010-03-04 22:11:00 +0000557/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800558 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700559 */
Stephen Hemmingere08f3ea2016-08-23 12:17:50 -0700560void netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700561{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200562 struct net_device *ndev = hv_get_drvdata(device);
563 struct net_device_context *net_device_ctx = netdev_priv(ndev);
564 struct netvsc_device *net_device = net_device_ctx->nvdev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700565
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200566 netvsc_disconnect_vsp(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700567
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200568 net_device_ctx->nvdev = NULL;
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700569
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700570 /*
571 * At this point, no one should be accessing net_device
572 * except in here
573 */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100574 netdev_dbg(ndev, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700575
Bill Pemberton454f18a2009-07-27 16:47:24 -0400576 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800577 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700578
Bill Pemberton454f18a2009-07-27 16:47:24 -0400579 /* Release all resources */
Haiyang Zhangf90251c2014-08-15 19:18:19 +0000580 free_netvsc_device(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700581}
582
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000583#define RING_AVAIL_PERCENT_HIWATER 20
584#define RING_AVAIL_PERCENT_LOWATER 10
585
586/*
587 * Get the percentage of available bytes to write in the ring.
588 * The return value is in range from 0 to 100.
589 */
590static inline u32 hv_ringbuf_avail_percent(
591 struct hv_ring_buffer_info *ring_info)
592{
593 u32 avail_read, avail_write;
594
595 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
596
597 return avail_write * 100 / ring_info->ring_datasize;
598}
599
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700600static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
601 u32 index)
602{
603 sync_change_bit(index, net_device->send_section_map);
604}
605
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700606static void netvsc_send_tx_complete(struct netvsc_device *net_device,
607 struct vmbus_channel *incoming_channel,
608 struct hv_device *device,
609 struct vmpacket_descriptor *packet)
610{
611 struct sk_buff *skb = (struct sk_buff *)(unsigned long)packet->trans_id;
612 struct net_device *ndev = hv_get_drvdata(device);
613 struct net_device_context *net_device_ctx = netdev_priv(ndev);
614 struct vmbus_channel *channel = device->channel;
615 int num_outstanding_sends;
616 u16 q_idx = 0;
617 int queue_sends;
618
619 /* Notify the layer above us */
620 if (likely(skb)) {
621 struct hv_netvsc_packet *nvsc_packet
622 = (struct hv_netvsc_packet *)skb->cb;
623 u32 send_index = nvsc_packet->send_buf_index;
624
625 if (send_index != NETVSC_INVALID_INDEX)
626 netvsc_free_send_slot(net_device, send_index);
627 q_idx = nvsc_packet->q_idx;
628 channel = incoming_channel;
629
Stephen Hemminger17db4bc2016-09-22 16:56:29 -0700630 dev_consume_skb_any(skb);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700631 }
632
633 num_outstanding_sends =
634 atomic_dec_return(&net_device->num_outstanding_sends);
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800635
636 queue_sends =
637 atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700638
639 if (net_device->destroy && num_outstanding_sends == 0)
640 wake_up(&net_device->wait_drain);
641
642 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
643 !net_device_ctx->start_remove &&
644 (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER ||
645 queue_sends < 1))
646 netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
647}
648
KY Srinivasan97c17232014-02-16 16:38:44 -0800649static void netvsc_send_completion(struct netvsc_device *net_device,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800650 struct vmbus_channel *incoming_channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800651 struct hv_device *device,
Haiyang Zhang85799a32010-12-10 12:03:54 -0800652 struct vmpacket_descriptor *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700653{
Haiyang Zhang85799a32010-12-10 12:03:54 -0800654 struct nvsp_message *nvsp_packet;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200655 struct net_device *ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700656
Haiyang Zhang85799a32010-12-10 12:03:54 -0800657 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700658 (packet->offset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -0700659
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700660 switch (nvsp_packet->hdr.msg_type) {
661 case NVSP_MSG_TYPE_INIT_COMPLETE:
662 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
663 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
664 case NVSP_MSG5_TYPE_SUBCHANNEL:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400665 /* Copy the response back */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800666 memcpy(&net_device->channel_init_pkt, nvsp_packet,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700667 sizeof(struct nvsp_message));
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700668 complete(&net_device->channel_init_wait);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700669 break;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000670
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700671 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
672 netvsc_send_tx_complete(net_device, incoming_channel,
673 device, packet);
674 break;
Hank Janssenfceaf242009-07-13 15:34:54 -0700675
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700676 default:
677 netdev_err(ndev,
678 "Unknown send completion type %d received!!\n",
679 nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700680 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700681}
682
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700683static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
684{
685 unsigned long index;
686 u32 max_words = net_device->map_words;
687 unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
688 u32 section_cnt = net_device->send_section_cnt;
689 int ret_val = NETVSC_INVALID_INDEX;
690 int i;
691 int prev_val;
692
693 for (i = 0; i < max_words; i++) {
694 if (!~(map_addr[i]))
695 continue;
696 index = ffz(map_addr[i]);
697 prev_val = sync_test_and_set_bit(index, &map_addr[i]);
698 if (prev_val)
699 continue;
700 if ((index + (i * BITS_PER_LONG)) >= section_cnt)
701 break;
702 ret_val = (index + (i * BITS_PER_LONG));
703 break;
704 }
705 return ret_val;
706}
707
Lad, Prabhakarda19fcd2015-02-05 15:06:33 +0000708static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
709 unsigned int section_index,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700710 u32 pend_size,
KY Srinivasan24476762015-12-01 16:43:06 -0800711 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800712 struct rndis_message *rndis_msg,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800713 struct hv_page_buffer **pb,
714 struct sk_buff *skb)
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700715{
716 char *start = net_device->send_buf;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700717 char *dest = start + (section_index * net_device->send_section_size)
718 + pend_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700719 int i;
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800720 bool is_data_pkt = (skb != NULL) ? true : false;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800721 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700722 u32 msg_size = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700723 u32 padding = 0;
724 u32 remain = packet->total_data_buflen % net_device->pkt_align;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700725 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
726 packet->page_buf_cnt;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700727
728 /* Add padding */
KY Srinivasanbde79be2015-12-01 16:43:17 -0800729 if (is_data_pkt && xmit_more && remain &&
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700730 !packet->cp_partial) {
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700731 padding = net_device->pkt_align - remain;
KY Srinivasan24476762015-12-01 16:43:06 -0800732 rndis_msg->msg_len += padding;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700733 packet->total_data_buflen += padding;
734 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700735
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700736 for (i = 0; i < page_count; i++) {
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800737 char *src = phys_to_virt((*pb)[i].pfn << PAGE_SHIFT);
738 u32 offset = (*pb)[i].offset;
739 u32 len = (*pb)[i].len;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700740
741 memcpy(dest, (src + offset), len);
742 msg_size += len;
743 dest += len;
744 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700745
746 if (padding) {
747 memset(dest, 0, padding);
748 msg_size += padding;
749 }
750
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700751 return msg_size;
752}
753
Stephen Hemminger3a8963a2016-09-09 12:45:24 -0700754static inline int netvsc_send_pkt(
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200755 struct hv_device *device,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700756 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800757 struct netvsc_device *net_device,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800758 struct hv_page_buffer **pb,
759 struct sk_buff *skb)
Hank Janssenfceaf242009-07-13 15:34:54 -0700760{
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700761 struct nvsp_message nvmsg;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800762 struct netvsc_channel *nvchan
763 = &net_device->chan_table[packet->q_idx];
764 struct vmbus_channel *out_channel = nvchan->channel;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200765 struct net_device *ndev = hv_get_drvdata(device);
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800766 struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700767 u64 req_id;
768 int ret;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700769 struct hv_page_buffer *pgbuf;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700770 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
KY Srinivasanbde79be2015-12-01 16:43:17 -0800771 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700772
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700773 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800774 if (skb != NULL) {
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700775 /* 0 is RMC_DATA; */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700776 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 0;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700777 } else {
778 /* 1 is RMC_CONTROL; */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700779 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 1;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700780 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700781
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700782 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
783 packet->send_buf_index;
784 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
785 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
786 else
787 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size =
788 packet->total_data_buflen;
Hank Janssenfceaf242009-07-13 15:34:54 -0700789
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800790 req_id = (ulong)skb;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000791
Haiyang Zhangc3582a22014-12-01 13:28:39 -0800792 if (out_channel->rescind)
793 return -ENODEV;
794
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700795 /*
796 * It is possible that once we successfully place this packet
797 * on the ringbuffer, we may stop the queue. In that case, we want
798 * to notify the host independent of the xmit_more flag. We don't
799 * need to be precise here; in the worst case we may signal the host
800 * unnecessarily.
801 */
802 if (ring_avail < (RING_AVAIL_PERCENT_LOWATER + 1))
KY Srinivasanbde79be2015-12-01 16:43:17 -0800803 xmit_more = false;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700804
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800805 if (packet->page_buf_cnt) {
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800806 pgbuf = packet->cp_partial ? (*pb) +
807 packet->rmsg_pgcnt : (*pb);
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700808 ret = vmbus_sendpacket_pagebuffer_ctl(out_channel,
809 pgbuf,
810 packet->page_buf_cnt,
811 &nvmsg,
812 sizeof(struct nvsp_message),
813 req_id,
814 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
KY Srinivasanbde79be2015-12-01 16:43:17 -0800815 !xmit_more);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700816 } else {
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700817 ret = vmbus_sendpacket_ctl(out_channel, &nvmsg,
818 sizeof(struct nvsp_message),
819 req_id,
820 VM_PKT_DATA_INBAND,
821 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
KY Srinivasanbde79be2015-12-01 16:43:17 -0800822 !xmit_more);
Hank Janssenfceaf242009-07-13 15:34:54 -0700823 }
824
Haiyang Zhang1d068252011-12-02 11:56:25 -0800825 if (ret == 0) {
826 atomic_inc(&net_device->num_outstanding_sends);
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800827 atomic_inc_return(&nvchan->queue_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700828
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700829 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800830 netif_tx_stop_queue(txq);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700831
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800832 if (atomic_read(&nvchan->queue_sends) < 1)
833 netif_tx_wake_queue(txq);
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000834 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800835 } else if (ret == -EAGAIN) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800836 netif_tx_stop_queue(txq);
837 if (atomic_read(&nvchan->queue_sends) < 1) {
838 netif_tx_wake_queue(txq);
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000839 ret = -ENOSPC;
840 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800841 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700842 netdev_err(ndev, "Unable to send packet %p ret %d\n",
Haiyang Zhang85799a32010-12-10 12:03:54 -0800843 packet, ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800844 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700845
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700846 return ret;
847}
848
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800849/* Move packet out of multi send data (msd), and clear msd */
850static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
851 struct sk_buff **msd_skb,
852 struct multi_send_data *msdp)
853{
854 *msd_skb = msdp->skb;
855 *msd_send = msdp->pkt;
856 msdp->skb = NULL;
857 msdp->pkt = NULL;
858 msdp->count = 0;
859}
860
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700861int netvsc_send(struct hv_device *device,
KY Srinivasan24476762015-12-01 16:43:06 -0800862 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800863 struct rndis_message *rndis_msg,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800864 struct hv_page_buffer **pb,
865 struct sk_buff *skb)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700866{
867 struct netvsc_device *net_device;
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700868 int ret = 0;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800869 struct netvsc_channel *nvchan;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700870 u32 pktlen = packet->total_data_buflen, msd_len = 0;
871 unsigned int section_index = NETVSC_INVALID_INDEX;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700872 struct multi_send_data *msdp;
873 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800874 struct sk_buff *msd_skb = NULL;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700875 bool try_batch;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800876 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700877
878 net_device = get_outbound_net_device(device);
879 if (!net_device)
880 return -ENODEV;
881
Vitaly Kuznetsove8f0a892016-10-19 15:53:01 +0200882 /* We may race with netvsc_connect_vsp()/netvsc_init_buf() and get
883 * here before the negotiation with the host is finished and
884 * send_section_map may not be allocated yet.
885 */
886 if (!net_device->send_section_map)
887 return -EAGAIN;
888
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800889 nvchan = &net_device->chan_table[packet->q_idx];
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700890 packet->send_buf_index = NETVSC_INVALID_INDEX;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700891 packet->cp_partial = false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700892
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800893 /* Send control message directly without accessing msd (Multi-Send
894 * Data) field which may be changed during data packet processing.
895 */
896 if (!skb) {
897 cur_send = packet;
898 goto send_now;
899 }
900
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700901 /* batch packets in send buffer if possible */
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800902 msdp = &nvchan->msd;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700903 if (msdp->pkt)
904 msd_len = msdp->pkt->total_data_buflen;
905
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800906 try_batch = (skb != NULL) && msd_len > 0 && msdp->count <
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700907 net_device->max_pkt;
908
909 if (try_batch && msd_len + pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700910 net_device->send_section_size) {
911 section_index = msdp->pkt->send_buf_index;
912
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700913 } else if (try_batch && msd_len + packet->rmsg_size <
914 net_device->send_section_size) {
915 section_index = msdp->pkt->send_buf_index;
916 packet->cp_partial = true;
917
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800918 } else if ((skb != NULL) && pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700919 net_device->send_section_size) {
920 section_index = netvsc_get_next_send_section(net_device);
921 if (section_index != NETVSC_INVALID_INDEX) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800922 move_pkt_msd(&msd_send, &msd_skb, msdp);
923 msd_len = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700924 }
925 }
926
927 if (section_index != NETVSC_INVALID_INDEX) {
928 netvsc_copy_to_send_buf(net_device,
929 section_index, msd_len,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800930 packet, rndis_msg, pb, skb);
KY Srinivasanb08cc792015-03-29 21:08:42 -0700931
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700932 packet->send_buf_index = section_index;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700933
934 if (packet->cp_partial) {
935 packet->page_buf_cnt -= packet->rmsg_pgcnt;
936 packet->total_data_buflen = msd_len + packet->rmsg_size;
937 } else {
938 packet->page_buf_cnt = 0;
939 packet->total_data_buflen += msd_len;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700940 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700941
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800942 if (msdp->skb)
Stephen Hemminger17db4bc2016-09-22 16:56:29 -0700943 dev_consume_skb_any(msdp->skb);
Haiyang Zhangee90b812015-04-06 15:22:54 -0700944
KY Srinivasanbde79be2015-12-01 16:43:17 -0800945 if (xmit_more && !packet->cp_partial) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800946 msdp->skb = skb;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700947 msdp->pkt = packet;
948 msdp->count++;
949 } else {
950 cur_send = packet;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800951 msdp->skb = NULL;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700952 msdp->pkt = NULL;
953 msdp->count = 0;
954 }
955 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800956 move_pkt_msd(&msd_send, &msd_skb, msdp);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700957 cur_send = packet;
958 }
959
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700960 if (msd_send) {
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700961 int m_ret = netvsc_send_pkt(device, msd_send, net_device,
962 NULL, msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700963
964 if (m_ret != 0) {
965 netvsc_free_send_slot(net_device,
966 msd_send->send_buf_index);
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800967 dev_kfree_skb_any(msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700968 }
969 }
970
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800971send_now:
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700972 if (cur_send)
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200973 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700974
Jerry Snitselaar7aab5152015-05-04 10:57:16 -0700975 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
976 netvsc_free_send_slot(net_device, section_index);
Haiyang Zhangd953ca42015-01-29 12:34:49 -0800977
Hank Janssenfceaf242009-07-13 15:34:54 -0700978 return ret;
979}
980
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700981static int netvsc_send_recv_completion(struct vmbus_channel *channel,
982 u64 transaction_id, u32 status)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700983{
984 struct nvsp_message recvcompMessage;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700985 int ret;
986
987 recvcompMessage.hdr.msg_type =
988 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
989
Haiyang Zhang63f69212012-10-02 05:30:23 +0000990 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700991
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700992 /* Send the completion */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700993 ret = vmbus_sendpacket(channel, &recvcompMessage,
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700994 sizeof(struct nvsp_message_header) + sizeof(u32),
995 transaction_id, VM_PKT_COMP, 0);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700996
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700997 return ret;
998}
999
1000static inline void count_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx,
1001 u32 *filled, u32 *avail)
1002{
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001003 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
1004 u32 first = mrc->first;
1005 u32 next = mrc->next;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001006
1007 *filled = (first > next) ? NETVSC_RECVSLOT_MAX - first + next :
1008 next - first;
1009
1010 *avail = NETVSC_RECVSLOT_MAX - *filled - 1;
1011}
1012
1013/* Read the first filled slot, no change to index */
1014static inline struct recv_comp_data *read_recv_comp_slot(struct netvsc_device
1015 *nvdev, u16 q_idx)
1016{
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001017 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001018 u32 filled, avail;
1019
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001020 if (unlikely(!mrc->buf))
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001021 return NULL;
1022
1023 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1024 if (!filled)
1025 return NULL;
1026
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001027 return mrc->buf + mrc->first * sizeof(struct recv_comp_data);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001028}
1029
1030/* Put the first filled slot back to available pool */
1031static inline void put_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx)
1032{
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001033 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001034 int num_recv;
1035
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001036 mrc->first = (mrc->first + 1) % NETVSC_RECVSLOT_MAX;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001037
1038 num_recv = atomic_dec_return(&nvdev->num_outstanding_recvs);
1039
1040 if (nvdev->destroy && num_recv == 0)
1041 wake_up(&nvdev->wait_drain);
1042}
1043
1044/* Check and send pending recv completions */
1045static void netvsc_chk_recv_comp(struct netvsc_device *nvdev,
1046 struct vmbus_channel *channel, u16 q_idx)
1047{
1048 struct recv_comp_data *rcd;
1049 int ret;
1050
1051 while (true) {
1052 rcd = read_recv_comp_slot(nvdev, q_idx);
1053 if (!rcd)
1054 break;
1055
1056 ret = netvsc_send_recv_completion(channel, rcd->tid,
1057 rcd->status);
1058 if (ret)
1059 break;
1060
1061 put_recv_comp_slot(nvdev, q_idx);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001062 }
1063}
1064
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001065#define NETVSC_RCD_WATERMARK 80
1066
1067/* Get next available slot */
1068static inline struct recv_comp_data *get_recv_comp_slot(
1069 struct netvsc_device *nvdev, struct vmbus_channel *channel, u16 q_idx)
1070{
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001071 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001072 u32 filled, avail, next;
1073 struct recv_comp_data *rcd;
1074
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001075 if (unlikely(!nvdev->recv_section))
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001076 return NULL;
1077
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001078 if (unlikely(!mrc->buf))
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001079 return NULL;
1080
1081 if (atomic_read(&nvdev->num_outstanding_recvs) >
1082 nvdev->recv_section->num_sub_allocs * NETVSC_RCD_WATERMARK / 100)
1083 netvsc_chk_recv_comp(nvdev, channel, q_idx);
1084
1085 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1086 if (!avail)
1087 return NULL;
1088
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001089 next = mrc->next;
1090 rcd = mrc->buf + next * sizeof(struct recv_comp_data);
1091 mrc->next = (next + 1) % NETVSC_RECVSLOT_MAX;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001092
1093 atomic_inc(&nvdev->num_outstanding_recvs);
1094
1095 return rcd;
1096}
1097
KY Srinivasan97c17232014-02-16 16:38:44 -08001098static void netvsc_receive(struct netvsc_device *net_device,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001099 struct vmbus_channel *channel,
KY Srinivasan97c17232014-02-16 16:38:44 -08001100 struct hv_device *device,
1101 struct vmpacket_descriptor *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -07001102{
Haiyang Zhang85799a32010-12-10 12:03:54 -08001103 struct vmtransfer_page_packet_header *vmxferpage_packet;
1104 struct nvsp_message *nvsp_packet;
Haiyang Zhang4baab262014-04-21 14:54:43 -07001105 struct hv_netvsc_packet nv_pkt;
1106 struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
1107 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -08001108 int i;
1109 int count = 0;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001110 struct net_device *ndev = hv_get_drvdata(device);
KY Srinivasanc4b20c62015-12-01 16:43:07 -08001111 void *data;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001112 int ret;
1113 struct recv_comp_data *rcd;
1114 u16 q_idx = channel->offermsg.offer.sub_channel_index;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -07001115
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001116 /*
1117 * All inbound packets other than send completion should be xfer page
1118 * packet
1119 */
Haiyang Zhang415f2282011-01-26 12:12:13 -08001120 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001121 netdev_err(ndev, "Unknown packet type received - %d\n",
Haiyang Zhang415f2282011-01-26 12:12:13 -08001122 packet->type);
Hank Janssenfceaf242009-07-13 15:34:54 -07001123 return;
1124 }
1125
Haiyang Zhang85799a32010-12-10 12:03:54 -08001126 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
Haiyang Zhang415f2282011-01-26 12:12:13 -08001127 (packet->offset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -07001128
Bill Pemberton454f18a2009-07-27 16:47:24 -04001129 /* Make sure this is a valid nvsp packet */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -08001130 if (nvsp_packet->hdr.msg_type !=
1131 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001132 netdev_err(ndev, "Unknown nvsp packet type received-"
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001133 " %d\n", nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -07001134 return;
1135 }
1136
Haiyang Zhang85799a32010-12-10 12:03:54 -08001137 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
Hank Janssenfceaf242009-07-13 15:34:54 -07001138
Haiyang Zhang415f2282011-01-26 12:12:13 -08001139 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001140 netdev_err(ndev, "Invalid xfer page set id - "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001141 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
Haiyang Zhang415f2282011-01-26 12:12:13 -08001142 vmxferpage_packet->xfer_pageset_id);
Hank Janssenfceaf242009-07-13 15:34:54 -07001143 return;
1144 }
1145
Haiyang Zhang4baab262014-04-21 14:54:43 -07001146 count = vmxferpage_packet->range_cnt;
Hank Janssenfceaf242009-07-13 15:34:54 -07001147
Bill Pemberton454f18a2009-07-27 16:47:24 -04001148 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -07001149 for (i = 0; i < count; i++) {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001150 /* Initialize the netvsc packet */
KY Srinivasanc4b20c62015-12-01 16:43:07 -08001151 data = (void *)((unsigned long)net_device->
Haiyang Zhang45326342011-12-15 13:45:15 -08001152 recv_buf + vmxferpage_packet->ranges[i].byte_offset);
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -08001153 netvsc_packet->total_data_buflen =
Haiyang Zhang415f2282011-01-26 12:12:13 -08001154 vmxferpage_packet->ranges[i].byte_count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001155
Bill Pemberton454f18a2009-07-27 16:47:24 -04001156 /* Pass it to the upper layer */
KY Srinivasan10082f92015-12-01 16:43:18 -08001157 status = rndis_filter_receive(device, netvsc_packet, &data,
1158 channel);
Hank Janssenfceaf242009-07-13 15:34:54 -07001159 }
1160
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001161 if (!net_device->chan_table[q_idx].mrc.buf) {
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001162 ret = netvsc_send_recv_completion(channel,
1163 vmxferpage_packet->d.trans_id,
1164 status);
1165 if (ret)
1166 netdev_err(ndev, "Recv_comp q:%hd, tid:%llx, err:%d\n",
1167 q_idx, vmxferpage_packet->d.trans_id, ret);
1168 return;
1169 }
1170
1171 rcd = get_recv_comp_slot(net_device, channel, q_idx);
1172
1173 if (!rcd) {
1174 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1175 q_idx, vmxferpage_packet->d.trans_id);
1176 return;
1177 }
1178
1179 rcd->tid = vmxferpage_packet->d.trans_id;
1180 rcd->status = status;
Hank Janssenfceaf242009-07-13 15:34:54 -07001181}
1182
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001183static void netvsc_send_table(struct hv_device *hdev,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001184 struct nvsp_message *nvmsg)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001185{
1186 struct netvsc_device *nvscdev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001187 struct net_device *ndev = hv_get_drvdata(hdev);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001188 int i;
1189 u32 count, *tab;
1190
1191 nvscdev = get_outbound_net_device(hdev);
1192 if (!nvscdev)
1193 return;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001194
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001195 count = nvmsg->msg.v5_msg.send_table.count;
1196 if (count != VRSS_SEND_TAB_SIZE) {
1197 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1198 return;
1199 }
1200
1201 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1202 nvmsg->msg.v5_msg.send_table.offset);
1203
1204 for (i = 0; i < count; i++)
1205 nvscdev->send_table[i] = tab[i];
1206}
1207
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001208static void netvsc_send_vf(struct net_device_context *net_device_ctx,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001209 struct nvsp_message *nvmsg)
1210{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001211 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1212 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
Haiyang Zhang71790a22015-07-24 10:08:40 -07001213}
1214
1215static inline void netvsc_receive_inband(struct hv_device *hdev,
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001216 struct net_device_context *net_device_ctx,
1217 struct nvsp_message *nvmsg)
Haiyang Zhang71790a22015-07-24 10:08:40 -07001218{
1219 switch (nvmsg->hdr.msg_type) {
1220 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1221 netvsc_send_table(hdev, nvmsg);
1222 break;
1223
1224 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001225 netvsc_send_vf(net_device_ctx, nvmsg);
Haiyang Zhang71790a22015-07-24 10:08:40 -07001226 break;
1227 }
1228}
1229
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001230static void netvsc_process_raw_pkt(struct hv_device *device,
1231 struct vmbus_channel *channel,
1232 struct netvsc_device *net_device,
1233 struct net_device *ndev,
1234 u64 request_id,
1235 struct vmpacket_descriptor *desc)
1236{
1237 struct nvsp_message *nvmsg;
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001238 struct net_device_context *net_device_ctx = netdev_priv(ndev);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001239
1240 nvmsg = (struct nvsp_message *)((unsigned long)
1241 desc + (desc->offset8 << 3));
1242
1243 switch (desc->type) {
1244 case VM_PKT_COMP:
1245 netvsc_send_completion(net_device, channel, device, desc);
1246 break;
1247
1248 case VM_PKT_DATA_USING_XFER_PAGES:
1249 netvsc_receive(net_device, channel, device, desc);
1250 break;
1251
1252 case VM_PKT_DATA_INBAND:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001253 netvsc_receive_inband(device, net_device_ctx, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001254 break;
1255
1256 default:
1257 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
1258 desc->type, request_id);
1259 break;
1260 }
1261}
1262
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001263void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001264{
stephen hemminger0b307eb2017-01-24 13:05:58 -08001265 struct vmbus_channel *channel = context;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001266 u16 q_idx = channel->offermsg.offer.sub_channel_index;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001267 struct hv_device *device;
Haiyang Zhang85799a32010-12-10 12:03:54 -08001268 struct netvsc_device *net_device;
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -07001269 struct vmpacket_descriptor *desc;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001270 struct net_device *ndev;
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001271 bool need_to_commit = false;
Hank Janssenfceaf242009-07-13 15:34:54 -07001272
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001273 if (channel->primary_channel != NULL)
1274 device = channel->primary_channel->device_obj;
1275 else
1276 device = channel->device_obj;
1277
Haiyang Zhang5a71ae32010-12-10 12:03:55 -08001278 net_device = get_inbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001279 if (!net_device)
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001280 return;
stephen hemminger0b307eb2017-01-24 13:05:58 -08001281
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001282 ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -07001283
stephen hemminger0b307eb2017-01-24 13:05:58 -08001284 while ((desc = get_next_pkt_raw(channel)) != NULL) {
1285 netvsc_process_raw_pkt(device, channel, net_device,
1286 ndev, desc->trans_id, desc);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001287
stephen hemminger0b307eb2017-01-24 13:05:58 -08001288 put_pkt_raw(channel, desc);
1289 need_to_commit = true;
1290 }
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001291
stephen hemminger0b307eb2017-01-24 13:05:58 -08001292 if (need_to_commit)
1293 commit_rd_index(channel);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001294
1295 netvsc_chk_recv_comp(net_device, channel, q_idx);
Hank Janssenfceaf242009-07-13 15:34:54 -07001296}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001297
1298/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001299 * netvsc_device_add - Callback when the device belonging to this
1300 * driver is added
1301 */
K. Y. Srinivasan7bd23a42011-05-10 07:54:53 -07001302int netvsc_device_add(struct hv_device *device, void *additional_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001303{
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001304 int i, ret = 0;
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001305 int ring_size =
1306 ((struct netvsc_device_info *)additional_info)->ring_size;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001307 struct netvsc_device *net_device;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001308 struct net_device *ndev = hv_get_drvdata(device);
1309 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001310
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001311 net_device = alloc_net_device();
Dan Carpenterb1c84922014-09-04 14:11:23 +03001312 if (!net_device)
1313 return -ENOMEM;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001314
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001315 net_device->ring_size = ring_size;
1316
Haiyang Zhangb637e022011-04-21 12:30:45 -07001317 /* Open the channel */
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001318 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1319 ring_size * PAGE_SIZE, NULL, 0,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001320 netvsc_channel_cb, device->channel);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001321
1322 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001323 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001324 goto cleanup;
1325 }
1326
1327 /* Channel is opened */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +01001328 netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001329
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001330 /* If we're reopening the device we may have multiple queues, fill the
1331 * chn_table with the default channel to use it before subchannels are
1332 * opened.
1333 */
1334 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001335 net_device->chan_table[i].channel = device->channel;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001336
1337 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1338 * populated.
1339 */
1340 wmb();
1341
1342 net_device_ctx->nvdev = net_device;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001343
Haiyang Zhangb637e022011-04-21 12:30:45 -07001344 /* Connect with the NetVsp */
1345 ret = netvsc_connect_vsp(device);
1346 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001347 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001348 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001349 goto close;
1350 }
1351
1352 return ret;
1353
1354close:
1355 /* Now, we can close the channel safely */
1356 vmbus_close(device->channel);
1357
1358cleanup:
Haiyang Zhangf90251c2014-08-15 19:18:19 +00001359 free_netvsc_device(net_device);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001360
1361 return ret;
1362}