blob: f0c15e782ce0a66da1d423ca37f1fadc1ccb8cce [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>
stephen hemminger9749fed2017-07-19 11:53:16 -070032#include <linux/rtnetlink.h>
stephen hemminger43bf99c2017-07-24 10:57:27 -070033#include <linux/prefetch.h>
stephen hemminger9749fed2017-07-19 11:53:16 -070034
KY Srinivasanc25aaf82014-04-30 10:14:31 -070035#include <asm/sync_bitops.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070036
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070037#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070038
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070039/*
40 * Switch the data path from the synthetic interface to the VF
41 * interface.
42 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020043void netvsc_switch_datapath(struct net_device *ndev, bool vf)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070044{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +020045 struct net_device_context *net_device_ctx = netdev_priv(ndev);
46 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger79e8cbe2017-07-19 11:53:13 -070047 struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020048 struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070049
50 memset(init_pkt, 0, sizeof(struct nvsp_message));
51 init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
52 if (vf)
53 init_pkt->msg.v4_msg.active_dp.active_datapath =
54 NVSP_DATAPATH_VF;
55 else
56 init_pkt->msg.v4_msg.active_dp.active_datapath =
57 NVSP_DATAPATH_SYNTHETIC;
58
59 vmbus_sendpacket(dev->channel, init_pkt,
60 sizeof(struct nvsp_message),
61 (unsigned long)init_pkt,
62 VM_PKT_DATA_INBAND, 0);
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070063
64 net_device_ctx->datapath = vf;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070065}
66
Vitaly Kuznetsov88098832016-05-13 13:55:25 +020067static struct netvsc_device *alloc_net_device(void)
Hank Janssenfceaf242009-07-13 15:34:54 -070068{
Haiyang Zhang85799a32010-12-10 12:03:54 -080069 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070070
Haiyang Zhang85799a32010-12-10 12:03:54 -080071 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
72 if (!net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -070073 return NULL;
74
stephen hemmingerb8b835a2017-01-24 13:06:07 -080075 net_device->chan_table[0].mrc.buf
76 = vzalloc(NETVSC_RECVSLOT_MAX * sizeof(struct recv_comp_data));
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070077
Haiyang Zhangdc5cd892012-06-04 06:42:38 +000078 init_waitqueue_head(&net_device->wait_drain);
K. Y. Srinivasanc38b9c72011-08-27 11:31:12 -070079 net_device->destroy = false;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070080 atomic_set(&net_device->open_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;
Stephen Hemmingerfd612602016-08-23 12:17:51 -070083 init_completion(&net_device->channel_init_wait);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -070084
Haiyang Zhang85799a32010-12-10 12:03:54 -080085 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070086}
87
stephen hemminger545a8e72017-03-22 14:51:00 -070088static void free_netvsc_device(struct rcu_head *head)
Haiyang Zhangf90251c2014-08-15 19:18:19 +000089{
stephen hemminger545a8e72017-03-22 14:51:00 -070090 struct netvsc_device *nvdev
91 = container_of(head, struct netvsc_device, rcu);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070092 int i;
93
94 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
stephen hemmingerb8b835a2017-01-24 13:06:07 -080095 vfree(nvdev->chan_table[i].mrc.buf);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070096
Haiyang Zhangf90251c2014-08-15 19:18:19 +000097 kfree(nvdev);
98}
99
stephen hemminger545a8e72017-03-22 14:51:00 -0700100static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
101{
102 call_rcu(&nvdev->rcu, free_netvsc_device);
103}
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800104
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700105static void netvsc_destroy_buf(struct hv_device *device)
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700106{
107 struct nvsp_message *revoke_packet;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200108 struct net_device *ndev = hv_get_drvdata(device);
stephen hemminger39629812017-07-19 11:53:19 -0700109 struct net_device_context *ndc = netdev_priv(ndev);
110 struct netvsc_device *net_device = rtnl_dereference(ndc->nvdev);
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700111 int ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700112
113 /*
114 * If we got a section count, it means we received a
115 * SendReceiveBufferComplete msg (ie sent
116 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
117 * to send a revoke msg here
118 */
119 if (net_device->recv_section_cnt) {
120 /* Send the revoke receive buffer */
121 revoke_packet = &net_device->revoke_packet;
122 memset(revoke_packet, 0, sizeof(struct nvsp_message));
123
124 revoke_packet->hdr.msg_type =
125 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
126 revoke_packet->msg.v1_msg.
127 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
128
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200129 ret = vmbus_sendpacket(device->channel,
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700130 revoke_packet,
131 sizeof(struct nvsp_message),
132 (unsigned long)revoke_packet,
133 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700134 /* If the failure is because the channel is rescinded;
135 * ignore the failure since we cannot send on a rescinded
136 * channel. This would allow us to properly cleanup
137 * even when the channel is rescinded.
138 */
139 if (device->channel->rescind)
140 ret = 0;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700141 /*
142 * If we failed here, we might as well return and
143 * have a leak rather than continue and a bugchk
144 */
145 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700146 netdev_err(ndev, "unable to send "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700147 "revoke receive buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700148 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700149 }
150 }
151
152 /* Teardown the gpadl on the vsp end */
153 if (net_device->recv_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200154 ret = vmbus_teardown_gpadl(device->channel,
155 net_device->recv_buf_gpadl_handle);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700156
157 /* If we failed here, we might as well return and have a leak
158 * rather than continue and a bugchk
159 */
160 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700161 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700162 "unable to teardown receive buffer's gpadl\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700163 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700164 }
165 net_device->recv_buf_gpadl_handle = 0;
166 }
167
168 if (net_device->recv_buf) {
169 /* Free up the receive buffer */
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800170 vfree(net_device->recv_buf);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700171 net_device->recv_buf = NULL;
172 }
173
174 if (net_device->recv_section) {
175 net_device->recv_section_cnt = 0;
176 kfree(net_device->recv_section);
177 net_device->recv_section = NULL;
178 }
179
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700180 /* Deal with the send buffer we may have setup.
181 * If we got a send section size, it means we received a
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800182 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
183 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700184 * to send a revoke msg here
185 */
186 if (net_device->send_section_size) {
187 /* Send the revoke receive buffer */
188 revoke_packet = &net_device->revoke_packet;
189 memset(revoke_packet, 0, sizeof(struct nvsp_message));
190
191 revoke_packet->hdr.msg_type =
192 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800193 revoke_packet->msg.v1_msg.revoke_send_buf.id =
194 NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700195
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200196 ret = vmbus_sendpacket(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700197 revoke_packet,
198 sizeof(struct nvsp_message),
199 (unsigned long)revoke_packet,
200 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700201
202 /* If the failure is because the channel is rescinded;
203 * ignore the failure since we cannot send on a rescinded
204 * channel. This would allow us to properly cleanup
205 * even when the channel is rescinded.
206 */
207 if (device->channel->rescind)
208 ret = 0;
209
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700210 /* 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");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700216 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700217 }
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");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700230 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700231 }
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 Zhangc51ed182014-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);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700240}
241
stephen hemminger95790832017-06-08 16:21:22 -0700242static int netvsc_init_buf(struct hv_device *device,
243 struct netvsc_device *net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700244{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700245 int ret = 0;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800246 struct nvsp_message *init_packet;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700247 struct net_device *ndev;
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700248 size_t map_words;
K. Y. Srinivasan0a726c22015-05-28 17:08:06 -0700249 int node;
Hank Janssenfceaf242009-07-13 15:34:54 -0700250
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200251 ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700252
K. Y. Srinivasan0a726c22015-05-28 17:08:06 -0700253 node = cpu_to_node(device->channel->target_cpu);
254 net_device->recv_buf = vzalloc_node(net_device->recv_buf_size, node);
255 if (!net_device->recv_buf)
256 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
257
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800258 if (!net_device->recv_buf) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700259 netdev_err(ndev, "unable to allocate receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700260 "buffer of size %d\n", net_device->recv_buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700261 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800262 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700263 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700264
Bill Pemberton454f18a2009-07-27 16:47:24 -0400265 /*
266 * Establish the gpadl handle for this buffer on this
267 * channel. Note: This call uses the vmbus connection rather
268 * than the channel to establish the gpadl handle.
269 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800270 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
271 net_device->recv_buf_size,
272 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700273 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700274 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700275 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800276 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700277 }
278
Bill Pemberton454f18a2009-07-27 16:47:24 -0400279 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800280 init_packet = &net_device->channel_init_pkt;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800281 memset(init_packet, 0, sizeof(struct nvsp_message));
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800282 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
283 init_packet->msg.v1_msg.send_recv_buf.
284 gpadl_handle = net_device->recv_buf_gpadl_handle;
285 init_packet->msg.v1_msg.
286 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700287
Bill Pemberton454f18a2009-07-27 16:47:24 -0400288 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800289 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700290 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800291 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800292 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700293 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700294 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700295 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700296 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800297 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700298 }
299
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200300 wait_for_completion(&net_device->channel_init_wait);
Hank Janssenfceaf242009-07-13 15:34:54 -0700301
Bill Pemberton454f18a2009-07-27 16:47:24 -0400302 /* Check the response */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800303 if (init_packet->msg.v1_msg.
304 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700305 netdev_err(ndev, "Unable to complete receive buffer "
Haiyang Zhang8bff33a2011-09-01 12:19:48 -0700306 "initialization with NetVsp - status %d\n",
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800307 init_packet->msg.v1_msg.
308 send_recv_buf_complete.status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700309 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800310 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700311 }
312
Bill Pemberton454f18a2009-07-27 16:47:24 -0400313 /* Parse the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700314
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800315 net_device->recv_section_cnt = init_packet->msg.
316 v1_msg.send_recv_buf_complete.num_sections;
Hank Janssenfceaf242009-07-13 15:34:54 -0700317
Haiyang Zhangc1813202011-11-30 07:19:07 -0800318 net_device->recv_section = kmemdup(
319 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
320 net_device->recv_section_cnt *
321 sizeof(struct nvsp_1_receive_buffer_section),
322 GFP_KERNEL);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800323 if (net_device->recv_section == NULL) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700324 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800325 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700326 }
327
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700328 /*
329 * For 1st release, there should only be 1 section that represents the
330 * entire receive buffer
331 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800332 if (net_device->recv_section_cnt != 1 ||
333 net_device->recv_section->offset != 0) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700334 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800335 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700336 }
337
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700338 /* Now setup the send buffer.
339 */
K. Y. Srinivasan5defde52015-05-28 17:08:07 -0700340 net_device->send_buf = vzalloc_node(net_device->send_buf_size, node);
341 if (!net_device->send_buf)
342 net_device->send_buf = vzalloc(net_device->send_buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700343 if (!net_device->send_buf) {
344 netdev_err(ndev, "unable to allocate send "
345 "buffer of size %d\n", net_device->send_buf_size);
346 ret = -ENOMEM;
347 goto cleanup;
348 }
349
350 /* Establish the gpadl handle for this buffer on this
351 * channel. Note: This call uses the vmbus connection rather
352 * than the channel to establish the gpadl handle.
353 */
354 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
355 net_device->send_buf_size,
356 &net_device->send_buf_gpadl_handle);
357 if (ret != 0) {
358 netdev_err(ndev,
359 "unable to establish send buffer's gpadl\n");
360 goto cleanup;
361 }
362
363 /* Notify the NetVsp of the gpadl handle */
364 init_packet = &net_device->channel_init_pkt;
365 memset(init_packet, 0, sizeof(struct nvsp_message));
366 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800367 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700368 net_device->send_buf_gpadl_handle;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800369 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700370
371 /* Send the gpadl notification request */
372 ret = vmbus_sendpacket(device->channel, init_packet,
373 sizeof(struct nvsp_message),
374 (unsigned long)init_packet,
375 VM_PKT_DATA_INBAND,
376 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
377 if (ret != 0) {
378 netdev_err(ndev,
379 "unable to send send buffer's gpadl to netvsp\n");
380 goto cleanup;
381 }
382
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200383 wait_for_completion(&net_device->channel_init_wait);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700384
385 /* Check the response */
386 if (init_packet->msg.v1_msg.
387 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
388 netdev_err(ndev, "Unable to complete send buffer "
389 "initialization with NetVsp - status %d\n",
390 init_packet->msg.v1_msg.
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800391 send_send_buf_complete.status);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700392 ret = -EINVAL;
393 goto cleanup;
394 }
395
396 /* Parse the response */
397 net_device->send_section_size = init_packet->msg.
398 v1_msg.send_send_buf_complete.section_size;
399
400 /* Section count is simply the size divided by the section size.
401 */
402 net_device->send_section_cnt =
Stephen Hemminger796cc882016-08-23 12:17:47 -0700403 net_device->send_buf_size / net_device->send_section_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700404
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100405 netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
406 net_device->send_section_size, net_device->send_section_cnt);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700407
408 /* Setup state for managing the send buffer. */
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700409 map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700410
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700411 net_device->send_section_map = kcalloc(map_words, sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800412 if (net_device->send_section_map == NULL) {
413 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700414 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800415 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700416
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800417 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700418
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800419cleanup:
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200420 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700421
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800422exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700423 return ret;
424}
425
Haiyang Zhangf157e782011-12-15 13:45:16 -0800426/* Negotiate NVSP protocol version */
427static int negotiate_nvsp_ver(struct hv_device *device,
428 struct netvsc_device *net_device,
429 struct nvsp_message *init_packet,
430 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700431{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200432 struct net_device *ndev = hv_get_drvdata(device);
Nicholas Mc Guire7390fe92015-01-25 15:46:31 +0100433 int ret;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800434
435 memset(init_packet, 0, sizeof(struct nvsp_message));
436 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
437 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
438 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
439
440 /* Send the init request */
441 ret = vmbus_sendpacket(device->channel, init_packet,
442 sizeof(struct nvsp_message),
443 (unsigned long)init_packet,
444 VM_PKT_DATA_INBAND,
445 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
446
447 if (ret != 0)
448 return ret;
449
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200450 wait_for_completion(&net_device->channel_init_wait);
Haiyang Zhangf157e782011-12-15 13:45:16 -0800451
452 if (init_packet->msg.init_msg.init_complete.status !=
453 NVSP_STAT_SUCCESS)
454 return -EINVAL;
455
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800456 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800457 return 0;
458
Haiyang Zhang71790a22015-07-24 10:08:40 -0700459 /* NVSPv2 or later: Send NDIS config */
Haiyang Zhangf157e782011-12-15 13:45:16 -0800460 memset(init_packet, 0, sizeof(struct nvsp_message));
461 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200462 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000463 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800464
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700465 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
Haiyang Zhang71790a22015-07-24 10:08:40 -0700466 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
467
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700468 /* Teaming bit is needed to receive link speed updates */
469 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
470 }
471
Haiyang Zhangf157e782011-12-15 13:45:16 -0800472 ret = vmbus_sendpacket(device->channel, init_packet,
473 sizeof(struct nvsp_message),
474 (unsigned long)init_packet,
475 VM_PKT_DATA_INBAND, 0);
476
477 return ret;
478}
479
stephen hemminger95790832017-06-08 16:21:22 -0700480static int netvsc_connect_vsp(struct hv_device *device,
481 struct netvsc_device *net_device)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800482{
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700483 const u32 ver_list[] = {
484 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
stephen hemminger95790832017-06-08 16:21:22 -0700485 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5
486 };
487 struct nvsp_message *init_packet;
488 int ndis_version, i, ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700489
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800490 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700491
Haiyang Zhangf157e782011-12-15 13:45:16 -0800492 /* Negotiate the latest NVSP protocol supported */
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700493 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800494 if (negotiate_nvsp_ver(device, net_device, init_packet,
495 ver_list[i]) == 0) {
496 net_device->nvsp_version = ver_list[i];
497 break;
498 }
499
500 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700501 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800502 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700503 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800504
505 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
506
Bill Pemberton454f18a2009-07-27 16:47:24 -0400507 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800508 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700509
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800510 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700511 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800512 else
513 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700514
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800515 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
516 init_packet->msg.v1_msg.
517 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800518 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800519 init_packet->msg.v1_msg.
520 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800521 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700522
Bill Pemberton454f18a2009-07-27 16:47:24 -0400523 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800524 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800525 sizeof(struct nvsp_message),
526 (unsigned long)init_packet,
527 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700528 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800529 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700530
Bill Pemberton454f18a2009-07-27 16:47:24 -0400531 /* Post the big receive buffer to NetVSP */
Haiyang Zhang99d30162014-03-09 16:10:59 -0700532 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
533 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
534 else
535 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700536 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
Haiyang Zhang99d30162014-03-09 16:10:59 -0700537
stephen hemminger95790832017-06-08 16:21:22 -0700538 ret = netvsc_init_buf(device, net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700539
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800540cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700541 return ret;
542}
543
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200544static void netvsc_disconnect_vsp(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700545{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200546 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700547}
548
Hank Janssen3e189512010-03-04 22:11:00 +0000549/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800550 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700551 */
Stephen Hemmingere08f3ea2016-08-23 12:17:50 -0700552void netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700553{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200554 struct net_device *ndev = hv_get_drvdata(device);
555 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -0700556 struct netvsc_device *net_device
557 = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger15a863b2017-02-27 10:26:49 -0800558 int i;
Hank Janssenfceaf242009-07-13 15:34:54 -0700559
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200560 netvsc_disconnect_vsp(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700561
stephen hemminger545a8e72017-03-22 14:51:00 -0700562 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700563
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700564 /*
565 * At this point, no one should be accessing net_device
566 * except in here
567 */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100568 netdev_dbg(ndev, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700569
Bill Pemberton454f18a2009-07-27 16:47:24 -0400570 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800571 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700572
stephen hemminger76bb5db2017-04-19 15:22:02 -0700573 /* And dissassociate NAPI context from device */
stephen hemminger79cd8742017-03-09 15:04:15 -0800574 for (i = 0; i < net_device->num_chn; i++)
stephen hemminger76bb5db2017-04-19 15:22:02 -0700575 netif_napi_del(&net_device->chan_table[i].napi);
stephen hemminger15a863b2017-02-27 10:26:49 -0800576
Bill Pemberton454f18a2009-07-27 16:47:24 -0400577 /* Release all resources */
stephen hemminger545a8e72017-03-22 14:51:00 -0700578 free_netvsc_device_rcu(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700579}
580
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000581#define RING_AVAIL_PERCENT_HIWATER 20
582#define RING_AVAIL_PERCENT_LOWATER 10
583
584/*
585 * Get the percentage of available bytes to write in the ring.
586 * The return value is in range from 0 to 100.
587 */
588static inline u32 hv_ringbuf_avail_percent(
589 struct hv_ring_buffer_info *ring_info)
590{
591 u32 avail_read, avail_write;
592
593 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
594
595 return avail_write * 100 / ring_info->ring_datasize;
596}
597
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700598static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
599 u32 index)
600{
601 sync_change_bit(index, net_device->send_section_map);
602}
603
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700604static void netvsc_send_tx_complete(struct netvsc_device *net_device,
605 struct vmbus_channel *incoming_channel,
606 struct hv_device *device,
stephen hemmingerf9645432017-04-07 14:41:19 -0400607 const struct vmpacket_descriptor *desc,
608 int budget)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700609{
stephen hemminger50698d82017-02-27 10:26:47 -0800610 struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700611 struct net_device *ndev = hv_get_drvdata(device);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700612 struct vmbus_channel *channel = device->channel;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700613 u16 q_idx = 0;
614 int queue_sends;
615
616 /* Notify the layer above us */
617 if (likely(skb)) {
stephen hemminger793e3952017-01-24 13:06:12 -0800618 const struct hv_netvsc_packet *packet
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700619 = (struct hv_netvsc_packet *)skb->cb;
stephen hemminger793e3952017-01-24 13:06:12 -0800620 u32 send_index = packet->send_buf_index;
621 struct netvsc_stats *tx_stats;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700622
623 if (send_index != NETVSC_INVALID_INDEX)
624 netvsc_free_send_slot(net_device, send_index);
stephen hemminger793e3952017-01-24 13:06:12 -0800625 q_idx = packet->q_idx;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700626 channel = incoming_channel;
627
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800628 tx_stats = &net_device->chan_table[q_idx].tx_stats;
stephen hemminger793e3952017-01-24 13:06:12 -0800629
630 u64_stats_update_begin(&tx_stats->syncp);
631 tx_stats->packets += packet->total_packets;
632 tx_stats->bytes += packet->total_bytes;
633 u64_stats_update_end(&tx_stats->syncp);
634
stephen hemmingerf9645432017-04-07 14:41:19 -0400635 napi_consume_skb(skb, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700636 }
637
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800638 queue_sends =
639 atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700640
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800641 if (net_device->destroy && queue_sends == 0)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700642 wake_up(&net_device->wait_drain);
643
644 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700645 (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER ||
646 queue_sends < 1))
647 netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
648}
649
KY Srinivasan97c17232014-02-16 16:38:44 -0800650static void netvsc_send_completion(struct netvsc_device *net_device,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800651 struct vmbus_channel *incoming_channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800652 struct hv_device *device,
stephen hemmingerf9645432017-04-07 14:41:19 -0400653 const struct vmpacket_descriptor *desc,
654 int budget)
Hank Janssenfceaf242009-07-13 15:34:54 -0700655{
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800656 struct nvsp_message *nvsp_packet = hv_pkt_data(desc);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200657 struct net_device *ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700658
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700659 switch (nvsp_packet->hdr.msg_type) {
660 case NVSP_MSG_TYPE_INIT_COMPLETE:
661 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
662 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
663 case NVSP_MSG5_TYPE_SUBCHANNEL:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400664 /* Copy the response back */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800665 memcpy(&net_device->channel_init_pkt, nvsp_packet,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700666 sizeof(struct nvsp_message));
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700667 complete(&net_device->channel_init_wait);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700668 break;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000669
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700670 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
671 netvsc_send_tx_complete(net_device, incoming_channel,
stephen hemmingerf9645432017-04-07 14:41:19 -0400672 device, desc, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700673 break;
Hank Janssenfceaf242009-07-13 15:34:54 -0700674
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700675 default:
676 netdev_err(ndev,
677 "Unknown send completion type %d received!!\n",
678 nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700679 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700680}
681
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700682static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
683{
stephen hemmingerb58a1852017-01-24 13:06:14 -0800684 unsigned long *map_addr = net_device->send_section_map;
685 unsigned int i;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700686
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700687 for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
stephen hemmingerb58a1852017-01-24 13:06:14 -0800688 if (sync_test_and_set_bit(i, map_addr) == 0)
689 return i;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700690 }
stephen hemmingerb58a1852017-01-24 13:06:14 -0800691
692 return NETVSC_INVALID_INDEX;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700693}
694
Lad, Prabhakarda19fcd2015-02-05 15:06:33 +0000695static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
696 unsigned int section_index,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700697 u32 pend_size,
KY Srinivasan24476762015-12-01 16:43:06 -0800698 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800699 struct rndis_message *rndis_msg,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800700 struct hv_page_buffer **pb,
701 struct sk_buff *skb)
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700702{
703 char *start = net_device->send_buf;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700704 char *dest = start + (section_index * net_device->send_section_size)
705 + pend_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700706 int i;
707 u32 msg_size = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700708 u32 padding = 0;
709 u32 remain = packet->total_data_buflen % net_device->pkt_align;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700710 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
711 packet->page_buf_cnt;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700712
713 /* Add padding */
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700714 if (skb->xmit_more && remain && !packet->cp_partial) {
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700715 padding = net_device->pkt_align - remain;
KY Srinivasan24476762015-12-01 16:43:06 -0800716 rndis_msg->msg_len += padding;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700717 packet->total_data_buflen += padding;
718 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700719
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700720 for (i = 0; i < page_count; i++) {
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800721 char *src = phys_to_virt((*pb)[i].pfn << PAGE_SHIFT);
722 u32 offset = (*pb)[i].offset;
723 u32 len = (*pb)[i].len;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700724
725 memcpy(dest, (src + offset), len);
726 msg_size += len;
727 dest += len;
728 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700729
730 if (padding) {
731 memset(dest, 0, padding);
732 msg_size += padding;
733 }
734
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700735 return msg_size;
736}
737
Stephen Hemminger3a8963a2016-09-09 12:45:24 -0700738static inline int netvsc_send_pkt(
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200739 struct hv_device *device,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700740 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800741 struct netvsc_device *net_device,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800742 struct hv_page_buffer **pb,
743 struct sk_buff *skb)
Hank Janssenfceaf242009-07-13 15:34:54 -0700744{
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700745 struct nvsp_message nvmsg;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800746 struct netvsc_channel *nvchan
747 = &net_device->chan_table[packet->q_idx];
748 struct vmbus_channel *out_channel = nvchan->channel;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200749 struct net_device *ndev = hv_get_drvdata(device);
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800750 struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700751 u64 req_id;
752 int ret;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700753 struct hv_page_buffer *pgbuf;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700754 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700755
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700756 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800757 if (skb != NULL) {
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700758 /* 0 is RMC_DATA; */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700759 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 0;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700760 } else {
761 /* 1 is RMC_CONTROL; */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700762 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 1;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700763 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700764
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700765 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
766 packet->send_buf_index;
767 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
768 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
769 else
770 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size =
771 packet->total_data_buflen;
Hank Janssenfceaf242009-07-13 15:34:54 -0700772
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800773 req_id = (ulong)skb;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000774
Haiyang Zhangc3582a22014-12-01 13:28:39 -0800775 if (out_channel->rescind)
776 return -ENODEV;
777
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800778 if (packet->page_buf_cnt) {
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800779 pgbuf = packet->cp_partial ? (*pb) +
780 packet->rmsg_pgcnt : (*pb);
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700781 ret = vmbus_sendpacket_pagebuffer_ctl(out_channel,
782 pgbuf,
783 packet->page_buf_cnt,
784 &nvmsg,
785 sizeof(struct nvsp_message),
786 req_id,
Stephen Hemminger34543232017-02-05 17:20:34 -0700787 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700788 } else {
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700789 ret = vmbus_sendpacket_ctl(out_channel, &nvmsg,
790 sizeof(struct nvsp_message),
791 req_id,
792 VM_PKT_DATA_INBAND,
Stephen Hemminger34543232017-02-05 17:20:34 -0700793 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700794 }
795
Haiyang Zhang1d068252011-12-02 11:56:25 -0800796 if (ret == 0) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800797 atomic_inc_return(&nvchan->queue_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700798
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800799 if (ring_avail < RING_AVAIL_PERCENT_LOWATER)
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800800 netif_tx_stop_queue(txq);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800801 } else if (ret == -EAGAIN) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800802 netif_tx_stop_queue(txq);
803 if (atomic_read(&nvchan->queue_sends) < 1) {
804 netif_tx_wake_queue(txq);
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000805 ret = -ENOSPC;
806 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800807 } else {
stephen hemminger4a2176c2017-07-28 08:59:43 -0700808 netdev_err(ndev,
809 "Unable to send packet pages %u len %u, ret %d\n",
810 packet->page_buf_cnt, packet->total_data_buflen,
811 ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800812 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700813
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700814 return ret;
815}
816
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800817/* Move packet out of multi send data (msd), and clear msd */
818static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
819 struct sk_buff **msd_skb,
820 struct multi_send_data *msdp)
821{
822 *msd_skb = msdp->skb;
823 *msd_send = msdp->pkt;
824 msdp->skb = NULL;
825 msdp->pkt = NULL;
826 msdp->count = 0;
827}
828
stephen hemminger2a926f72017-07-19 11:53:17 -0700829/* RCU already held by caller */
830int netvsc_send(struct net_device_context *ndev_ctx,
KY Srinivasan24476762015-12-01 16:43:06 -0800831 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800832 struct rndis_message *rndis_msg,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800833 struct hv_page_buffer **pb,
834 struct sk_buff *skb)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700835{
stephen hemminger39629812017-07-19 11:53:19 -0700836 struct netvsc_device *net_device
stephen hemminger867047c2017-07-28 08:59:42 -0700837 = rcu_dereference_bh(ndev_ctx->nvdev);
stephen hemminger2a926f72017-07-19 11:53:17 -0700838 struct hv_device *device = ndev_ctx->device_ctx;
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700839 int ret = 0;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800840 struct netvsc_channel *nvchan;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700841 u32 pktlen = packet->total_data_buflen, msd_len = 0;
842 unsigned int section_index = NETVSC_INVALID_INDEX;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700843 struct multi_send_data *msdp;
844 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800845 struct sk_buff *msd_skb = NULL;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700846 bool try_batch;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800847 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700848
stephen hemminger592b4fe2017-06-08 16:21:23 -0700849 /* If device is rescinded, return error and packet will get dropped. */
stephen hemminger2a926f72017-07-19 11:53:17 -0700850 if (unlikely(!net_device || net_device->destroy))
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700851 return -ENODEV;
852
Vitaly Kuznetsove8f0a892016-10-19 15:53:01 +0200853 /* We may race with netvsc_connect_vsp()/netvsc_init_buf() and get
854 * here before the negotiation with the host is finished and
855 * send_section_map may not be allocated yet.
856 */
stephen hemminger2d694d22017-06-08 16:21:21 -0700857 if (unlikely(!net_device->send_section_map))
Vitaly Kuznetsove8f0a892016-10-19 15:53:01 +0200858 return -EAGAIN;
859
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800860 nvchan = &net_device->chan_table[packet->q_idx];
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700861 packet->send_buf_index = NETVSC_INVALID_INDEX;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700862 packet->cp_partial = false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700863
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800864 /* Send control message directly without accessing msd (Multi-Send
865 * Data) field which may be changed during data packet processing.
866 */
867 if (!skb) {
868 cur_send = packet;
869 goto send_now;
870 }
871
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700872 /* batch packets in send buffer if possible */
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800873 msdp = &nvchan->msd;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700874 if (msdp->pkt)
875 msd_len = msdp->pkt->total_data_buflen;
876
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700877 try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700878 if (try_batch && msd_len + pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700879 net_device->send_section_size) {
880 section_index = msdp->pkt->send_buf_index;
881
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700882 } else if (try_batch && msd_len + packet->rmsg_size <
883 net_device->send_section_size) {
884 section_index = msdp->pkt->send_buf_index;
885 packet->cp_partial = true;
886
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700887 } else if (pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700888 net_device->send_section_size) {
889 section_index = netvsc_get_next_send_section(net_device);
890 if (section_index != NETVSC_INVALID_INDEX) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800891 move_pkt_msd(&msd_send, &msd_skb, msdp);
892 msd_len = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700893 }
894 }
895
896 if (section_index != NETVSC_INVALID_INDEX) {
897 netvsc_copy_to_send_buf(net_device,
898 section_index, msd_len,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800899 packet, rndis_msg, pb, skb);
KY Srinivasanb08cc792015-03-29 21:08:42 -0700900
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700901 packet->send_buf_index = section_index;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700902
903 if (packet->cp_partial) {
904 packet->page_buf_cnt -= packet->rmsg_pgcnt;
905 packet->total_data_buflen = msd_len + packet->rmsg_size;
906 } else {
907 packet->page_buf_cnt = 0;
908 packet->total_data_buflen += msd_len;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700909 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700910
stephen hemminger793e3952017-01-24 13:06:12 -0800911 if (msdp->pkt) {
912 packet->total_packets += msdp->pkt->total_packets;
913 packet->total_bytes += msdp->pkt->total_bytes;
914 }
915
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800916 if (msdp->skb)
Stephen Hemminger17db4bc2016-09-22 16:56:29 -0700917 dev_consume_skb_any(msdp->skb);
Haiyang Zhangee90b812015-04-06 15:22:54 -0700918
KY Srinivasanbde79be2015-12-01 16:43:17 -0800919 if (xmit_more && !packet->cp_partial) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800920 msdp->skb = skb;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700921 msdp->pkt = packet;
922 msdp->count++;
923 } else {
924 cur_send = packet;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800925 msdp->skb = NULL;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700926 msdp->pkt = NULL;
927 msdp->count = 0;
928 }
929 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800930 move_pkt_msd(&msd_send, &msd_skb, msdp);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700931 cur_send = packet;
932 }
933
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700934 if (msd_send) {
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700935 int m_ret = netvsc_send_pkt(device, msd_send, net_device,
936 NULL, msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700937
938 if (m_ret != 0) {
939 netvsc_free_send_slot(net_device,
940 msd_send->send_buf_index);
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800941 dev_kfree_skb_any(msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700942 }
943 }
944
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800945send_now:
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700946 if (cur_send)
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200947 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700948
Jerry Snitselaar7aab5152015-05-04 10:57:16 -0700949 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
950 netvsc_free_send_slot(net_device, section_index);
Haiyang Zhangd953ca42015-01-29 12:34:49 -0800951
Hank Janssenfceaf242009-07-13 15:34:54 -0700952 return ret;
953}
954
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700955static int netvsc_send_recv_completion(struct vmbus_channel *channel,
956 u64 transaction_id, u32 status)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700957{
958 struct nvsp_message recvcompMessage;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700959 int ret;
960
961 recvcompMessage.hdr.msg_type =
962 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
963
Haiyang Zhang63f69212012-10-02 05:30:23 +0000964 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700965
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700966 /* Send the completion */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700967 ret = vmbus_sendpacket(channel, &recvcompMessage,
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700968 sizeof(struct nvsp_message_header) + sizeof(u32),
969 transaction_id, VM_PKT_COMP, 0);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700970
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700971 return ret;
972}
973
974static inline void count_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx,
975 u32 *filled, u32 *avail)
976{
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800977 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
978 u32 first = mrc->first;
979 u32 next = mrc->next;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700980
981 *filled = (first > next) ? NETVSC_RECVSLOT_MAX - first + next :
982 next - first;
983
984 *avail = NETVSC_RECVSLOT_MAX - *filled - 1;
985}
986
987/* Read the first filled slot, no change to index */
988static inline struct recv_comp_data *read_recv_comp_slot(struct netvsc_device
989 *nvdev, u16 q_idx)
990{
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800991 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700992 u32 filled, avail;
993
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800994 if (unlikely(!mrc->buf))
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700995 return NULL;
996
997 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
998 if (!filled)
999 return NULL;
1000
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001001 return mrc->buf + mrc->first * sizeof(struct recv_comp_data);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001002}
1003
1004/* Put the first filled slot back to available pool */
1005static inline void put_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx)
1006{
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001007 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001008 int num_recv;
1009
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001010 mrc->first = (mrc->first + 1) % NETVSC_RECVSLOT_MAX;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001011
1012 num_recv = atomic_dec_return(&nvdev->num_outstanding_recvs);
1013
1014 if (nvdev->destroy && num_recv == 0)
1015 wake_up(&nvdev->wait_drain);
1016}
1017
1018/* Check and send pending recv completions */
1019static void netvsc_chk_recv_comp(struct netvsc_device *nvdev,
1020 struct vmbus_channel *channel, u16 q_idx)
1021{
1022 struct recv_comp_data *rcd;
1023 int ret;
1024
1025 while (true) {
1026 rcd = read_recv_comp_slot(nvdev, q_idx);
1027 if (!rcd)
1028 break;
1029
1030 ret = netvsc_send_recv_completion(channel, rcd->tid,
1031 rcd->status);
1032 if (ret)
1033 break;
1034
1035 put_recv_comp_slot(nvdev, q_idx);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001036 }
1037}
1038
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001039#define NETVSC_RCD_WATERMARK 80
1040
1041/* Get next available slot */
1042static inline struct recv_comp_data *get_recv_comp_slot(
1043 struct netvsc_device *nvdev, struct vmbus_channel *channel, u16 q_idx)
1044{
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001045 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001046 u32 filled, avail, next;
1047 struct recv_comp_data *rcd;
1048
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001049 if (unlikely(!nvdev->recv_section))
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001050 return NULL;
1051
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001052 if (unlikely(!mrc->buf))
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001053 return NULL;
1054
1055 if (atomic_read(&nvdev->num_outstanding_recvs) >
1056 nvdev->recv_section->num_sub_allocs * NETVSC_RCD_WATERMARK / 100)
1057 netvsc_chk_recv_comp(nvdev, channel, q_idx);
1058
1059 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1060 if (!avail)
1061 return NULL;
1062
stephen hemmingerb8b835a2017-01-24 13:06:07 -08001063 next = mrc->next;
1064 rcd = mrc->buf + next * sizeof(struct recv_comp_data);
1065 mrc->next = (next + 1) % NETVSC_RECVSLOT_MAX;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001066
1067 atomic_inc(&nvdev->num_outstanding_recvs);
1068
1069 return rcd;
1070}
1071
stephen hemminger15a863b2017-02-27 10:26:49 -08001072static int netvsc_receive(struct net_device *ndev,
stephen hemmingerdc54a082017-01-24 13:06:08 -08001073 struct netvsc_device *net_device,
1074 struct net_device_context *net_device_ctx,
1075 struct hv_device *device,
1076 struct vmbus_channel *channel,
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001077 const struct vmpacket_descriptor *desc,
stephen hemmingerdc54a082017-01-24 13:06:08 -08001078 struct nvsp_message *nvsp)
Hank Janssenfceaf242009-07-13 15:34:54 -07001079{
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001080 const struct vmtransfer_page_packet_header *vmxferpage_packet
1081 = container_of(desc, const struct vmtransfer_page_packet_header, d);
stephen hemminger15a863b2017-02-27 10:26:49 -08001082 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemmingerdc54a082017-01-24 13:06:08 -08001083 char *recv_buf = net_device->recv_buf;
Haiyang Zhang4baab262014-04-21 14:54:43 -07001084 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -08001085 int i;
1086 int count = 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001087 int ret;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -07001088
Bill Pemberton454f18a2009-07-27 16:47:24 -04001089 /* Make sure this is a valid nvsp packet */
stephen hemmingerdc54a082017-01-24 13:06:08 -08001090 if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
1091 netif_err(net_device_ctx, rx_err, ndev,
1092 "Unknown nvsp packet type received %u\n",
1093 nvsp->hdr.msg_type);
stephen hemminger15a863b2017-02-27 10:26:49 -08001094 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001095 }
1096
stephen hemmingerdc54a082017-01-24 13:06:08 -08001097 if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
1098 netif_err(net_device_ctx, rx_err, ndev,
1099 "Invalid xfer page set id - expecting %x got %x\n",
1100 NETVSC_RECEIVE_BUFFER_ID,
1101 vmxferpage_packet->xfer_pageset_id);
stephen hemminger15a863b2017-02-27 10:26:49 -08001102 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001103 }
1104
Haiyang Zhang4baab262014-04-21 14:54:43 -07001105 count = vmxferpage_packet->range_cnt;
Hank Janssenfceaf242009-07-13 15:34:54 -07001106
Bill Pemberton454f18a2009-07-27 16:47:24 -04001107 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -07001108 for (i = 0; i < count; i++) {
stephen hemmingerdc54a082017-01-24 13:06:08 -08001109 void *data = recv_buf
1110 + vmxferpage_packet->ranges[i].byte_offset;
1111 u32 buflen = vmxferpage_packet->ranges[i].byte_count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001112
Bill Pemberton454f18a2009-07-27 16:47:24 -04001113 /* Pass it to the upper layer */
stephen hemmingerdc54a082017-01-24 13:06:08 -08001114 status = rndis_filter_receive(ndev, net_device, device,
1115 channel, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -07001116 }
1117
stephen hemminger15a863b2017-02-27 10:26:49 -08001118 if (net_device->chan_table[q_idx].mrc.buf) {
1119 struct recv_comp_data *rcd;
1120
1121 rcd = get_recv_comp_slot(net_device, channel, q_idx);
1122 if (rcd) {
1123 rcd->tid = vmxferpage_packet->d.trans_id;
1124 rcd->status = status;
1125 } else {
1126 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1127 q_idx, vmxferpage_packet->d.trans_id);
1128 }
1129 } else {
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001130 ret = netvsc_send_recv_completion(channel,
1131 vmxferpage_packet->d.trans_id,
1132 status);
1133 if (ret)
1134 netdev_err(ndev, "Recv_comp q:%hd, tid:%llx, err:%d\n",
1135 q_idx, vmxferpage_packet->d.trans_id, ret);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001136 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001137 return count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001138}
1139
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001140static void netvsc_send_table(struct hv_device *hdev,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001141 struct nvsp_message *nvmsg)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001142{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001143 struct net_device *ndev = hv_get_drvdata(hdev);
stephen hemminger7ce10122017-03-09 14:58:29 -08001144 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001145 int i;
1146 u32 count, *tab;
1147
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001148 count = nvmsg->msg.v5_msg.send_table.count;
1149 if (count != VRSS_SEND_TAB_SIZE) {
1150 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1151 return;
1152 }
1153
1154 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1155 nvmsg->msg.v5_msg.send_table.offset);
1156
1157 for (i = 0; i < count; i++)
stephen hemminger7ce10122017-03-09 14:58:29 -08001158 net_device_ctx->tx_send_table[i] = tab[i];
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001159}
1160
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001161static void netvsc_send_vf(struct net_device_context *net_device_ctx,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001162 struct nvsp_message *nvmsg)
1163{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001164 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1165 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
Haiyang Zhang71790a22015-07-24 10:08:40 -07001166}
1167
1168static inline void netvsc_receive_inband(struct hv_device *hdev,
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001169 struct net_device_context *net_device_ctx,
1170 struct nvsp_message *nvmsg)
Haiyang Zhang71790a22015-07-24 10:08:40 -07001171{
1172 switch (nvmsg->hdr.msg_type) {
1173 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1174 netvsc_send_table(hdev, nvmsg);
1175 break;
1176
1177 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001178 netvsc_send_vf(net_device_ctx, nvmsg);
Haiyang Zhang71790a22015-07-24 10:08:40 -07001179 break;
1180 }
1181}
1182
stephen hemminger15a863b2017-02-27 10:26:49 -08001183static int netvsc_process_raw_pkt(struct hv_device *device,
1184 struct vmbus_channel *channel,
1185 struct netvsc_device *net_device,
1186 struct net_device *ndev,
stephen hemmingerf9645432017-04-07 14:41:19 -04001187 const struct vmpacket_descriptor *desc,
1188 int budget)
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001189{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001190 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001191 struct nvsp_message *nvmsg = hv_pkt_data(desc);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001192
1193 switch (desc->type) {
1194 case VM_PKT_COMP:
stephen hemmingerf9645432017-04-07 14:41:19 -04001195 netvsc_send_completion(net_device, channel, device,
1196 desc, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001197 break;
1198
1199 case VM_PKT_DATA_USING_XFER_PAGES:
stephen hemminger15a863b2017-02-27 10:26:49 -08001200 return netvsc_receive(ndev, net_device, net_device_ctx,
1201 device, channel, desc, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001202 break;
1203
1204 case VM_PKT_DATA_INBAND:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001205 netvsc_receive_inband(device, net_device_ctx, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001206 break;
1207
1208 default:
1209 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001210 desc->type, desc->trans_id);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001211 break;
1212 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001213
1214 return 0;
1215}
1216
1217static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
1218{
1219 struct vmbus_channel *primary = channel->primary_channel;
1220
1221 return primary ? primary->device_obj : channel->device_obj;
1222}
1223
stephen hemminger262b7f12017-03-16 16:12:38 -07001224/* Network processing softirq
1225 * Process data in incoming ring buffer from host
1226 * Stops when ring is empty or budget is met or exceeded.
1227 */
stephen hemminger15a863b2017-02-27 10:26:49 -08001228int netvsc_poll(struct napi_struct *napi, int budget)
1229{
1230 struct netvsc_channel *nvchan
1231 = container_of(napi, struct netvsc_channel, napi);
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001232 struct netvsc_device *net_device = nvchan->net_device;
stephen hemminger15a863b2017-02-27 10:26:49 -08001233 struct vmbus_channel *channel = nvchan->channel;
1234 struct hv_device *device = netvsc_channel_to_device(channel);
1235 u16 q_idx = channel->offermsg.offer.sub_channel_index;
1236 struct net_device *ndev = hv_get_drvdata(device);
stephen hemminger15a863b2017-02-27 10:26:49 -08001237 int work_done = 0;
1238
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001239 /* If starting a new interval */
1240 if (!nvchan->desc)
1241 nvchan->desc = hv_pkt_iter_first(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001242
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001243 while (nvchan->desc && work_done < budget) {
1244 work_done += netvsc_process_raw_pkt(device, channel, net_device,
stephen hemmingerf9645432017-04-07 14:41:19 -04001245 ndev, nvchan->desc, budget);
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001246 nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
stephen hemminger15a863b2017-02-27 10:26:49 -08001247 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001248
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001249 /* If receive ring was exhausted
1250 * and not doing busy poll
stephen hemminger262b7f12017-03-16 16:12:38 -07001251 * then re-enable host interrupts
1252 * and reschedule if ring is not empty.
1253 */
stephen hemminger15a863b2017-02-27 10:26:49 -08001254 if (work_done < budget &&
1255 napi_complete_done(napi, work_done) &&
1256 hv_end_read(&channel->inbound) != 0)
1257 napi_reschedule(napi);
1258
1259 netvsc_chk_recv_comp(net_device, channel, q_idx);
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001260
1261 /* Driver may overshoot since multiple packets per descriptor */
1262 return min(work_done, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001263}
1264
stephen hemminger262b7f12017-03-16 16:12:38 -07001265/* Call back when data is available in host ring buffer.
1266 * Processing is deferred until network softirq (NAPI)
1267 */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001268void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001269{
stephen hemminger6de38af2017-03-16 16:12:37 -07001270 struct netvsc_channel *nvchan = context;
stephen hemminger43bf99c2017-07-24 10:57:27 -07001271 struct vmbus_channel *channel = nvchan->channel;
1272 struct hv_ring_buffer_info *rbi = &channel->inbound;
1273
1274 /* preload first vmpacket descriptor */
1275 prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
stephen hemminger0b307eb2017-01-24 13:05:58 -08001276
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001277 if (napi_schedule_prep(&nvchan->napi)) {
1278 /* disable interupts from host */
stephen hemminger43bf99c2017-07-24 10:57:27 -07001279 hv_begin_read(rbi);
stephen hemminger0d6dd352017-03-09 15:04:14 -08001280
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001281 __napi_schedule(&nvchan->napi);
1282 }
Hank Janssenfceaf242009-07-13 15:34:54 -07001283}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001284
1285/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001286 * netvsc_device_add - Callback when the device belonging to this
1287 * driver is added
1288 */
stephen hemminger9749fed2017-07-19 11:53:16 -07001289struct netvsc_device *netvsc_device_add(struct hv_device *device,
1290 const struct netvsc_device_info *device_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001291{
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001292 int i, ret = 0;
stephen hemminger2c7f83c2017-01-24 13:06:09 -08001293 int ring_size = device_info->ring_size;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001294 struct netvsc_device *net_device;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001295 struct net_device *ndev = hv_get_drvdata(device);
1296 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001297
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001298 net_device = alloc_net_device();
Dan Carpenterb1c84922014-09-04 14:11:23 +03001299 if (!net_device)
stephen hemminger9749fed2017-07-19 11:53:16 -07001300 return ERR_PTR(-ENOMEM);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001301
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001302 net_device->ring_size = ring_size;
1303
stephen hemminger15a863b2017-02-27 10:26:49 -08001304 /* Because the device uses NAPI, all the interrupt batching and
1305 * control is done via Net softirq, not the channel handling
1306 */
1307 set_channel_read_mode(device->channel, HV_CALL_ISR);
1308
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001309 /* If we're reopening the device we may have multiple queues, fill the
1310 * chn_table with the default channel to use it before subchannels are
1311 * opened.
1312 * Initialize the channel state before we open;
1313 * we can be interrupted as soon as we open the channel.
1314 */
1315
1316 for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
1317 struct netvsc_channel *nvchan = &net_device->chan_table[i];
1318
1319 nvchan->channel = device->channel;
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001320 nvchan->net_device = net_device;
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001321 }
1322
stephen hemminger2be0f262017-05-03 16:59:21 -07001323 /* Enable NAPI handler before init callbacks */
1324 netif_napi_add(ndev, &net_device->chan_table[0].napi,
1325 netvsc_poll, NAPI_POLL_WEIGHT);
1326
Haiyang Zhangb637e022011-04-21 12:30:45 -07001327 /* Open the channel */
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001328 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1329 ring_size * PAGE_SIZE, NULL, 0,
stephen hemminger6de38af2017-03-16 16:12:37 -07001330 netvsc_channel_cb,
1331 net_device->chan_table);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001332
1333 if (ret != 0) {
stephen hemminger2be0f262017-05-03 16:59:21 -07001334 netif_napi_del(&net_device->chan_table[0].napi);
Haiyang Zhangd9871152011-09-01 12:19:41 -07001335 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001336 goto cleanup;
1337 }
1338
1339 /* Channel is opened */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +01001340 netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001341
stephen hemminger15a863b2017-02-27 10:26:49 -08001342 napi_enable(&net_device->chan_table[0].napi);
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001343
1344 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1345 * populated.
1346 */
stephen hemminger545a8e72017-03-22 14:51:00 -07001347 rcu_assign_pointer(net_device_ctx->nvdev, net_device);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001348
Haiyang Zhangb637e022011-04-21 12:30:45 -07001349 /* Connect with the NetVsp */
stephen hemminger95790832017-06-08 16:21:22 -07001350 ret = netvsc_connect_vsp(device, net_device);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001351 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001352 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001353 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001354 goto close;
1355 }
1356
stephen hemminger9749fed2017-07-19 11:53:16 -07001357 return net_device;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001358
1359close:
stephen hemminger76bb5db2017-04-19 15:22:02 -07001360 netif_napi_del(&net_device->chan_table[0].napi);
stephen hemminger15a863b2017-02-27 10:26:49 -08001361
Haiyang Zhangb637e022011-04-21 12:30:45 -07001362 /* Now, we can close the channel safely */
1363 vmbus_close(device->channel);
1364
1365cleanup:
stephen hemminger545a8e72017-03-22 14:51:00 -07001366 free_netvsc_device(&net_device->rcu);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001367
stephen hemminger9749fed2017-07-19 11:53:16 -07001368 return ERR_PTR(ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001369}