blob: a5511b7326af646618658060c8d289b60fb4883b [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);
63}
64
Vitaly Kuznetsov88098832016-05-13 13:55:25 +020065static struct netvsc_device *alloc_net_device(void)
Hank Janssenfceaf242009-07-13 15:34:54 -070066{
Haiyang Zhang85799a32010-12-10 12:03:54 -080067 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070068
Haiyang Zhang85799a32010-12-10 12:03:54 -080069 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
70 if (!net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -070071 return NULL;
72
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 hemminger8b532792017-08-09 17:46:11 -070078
79 net_device->recv_section_size = NETVSC_RECV_SECTION_SIZE;
80 net_device->send_section_size = NETVSC_SEND_SECTION_SIZE;
81
Stephen Hemmingerfd612602016-08-23 12:17:51 -070082 init_completion(&net_device->channel_init_wait);
stephen hemminger732e4982017-08-03 17:13:54 -070083 init_waitqueue_head(&net_device->subchan_open);
Stephen Hemminger8195b132017-09-06 13:53:05 -070084 INIT_WORK(&net_device->subchan_work, rndis_set_subchannel);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -070085
Haiyang Zhang85799a32010-12-10 12:03:54 -080086 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070087}
88
stephen hemminger545a8e72017-03-22 14:51:00 -070089static void free_netvsc_device(struct rcu_head *head)
Haiyang Zhangf90251c2014-08-15 19:18:19 +000090{
stephen hemminger545a8e72017-03-22 14:51:00 -070091 struct netvsc_device *nvdev
92 = container_of(head, struct netvsc_device, rcu);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070093 int i;
94
95 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
stephen hemminger7426b1a2017-07-28 08:59:45 -070096 vfree(nvdev->chan_table[i].mrc.slots);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070097
Haiyang Zhangf90251c2014-08-15 19:18:19 +000098 kfree(nvdev);
99}
100
stephen hemminger545a8e72017-03-22 14:51:00 -0700101static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
102{
103 call_rcu(&nvdev->rcu, free_netvsc_device);
104}
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800105
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700106static void netvsc_destroy_buf(struct hv_device *device)
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700107{
108 struct nvsp_message *revoke_packet;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200109 struct net_device *ndev = hv_get_drvdata(device);
stephen hemminger39629812017-07-19 11:53:19 -0700110 struct net_device_context *ndc = netdev_priv(ndev);
111 struct netvsc_device *net_device = rtnl_dereference(ndc->nvdev);
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700112 int ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700113
114 /*
115 * If we got a section count, it means we received a
116 * SendReceiveBufferComplete msg (ie sent
117 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
118 * to send a revoke msg here
119 */
120 if (net_device->recv_section_cnt) {
121 /* Send the revoke receive buffer */
122 revoke_packet = &net_device->revoke_packet;
123 memset(revoke_packet, 0, sizeof(struct nvsp_message));
124
125 revoke_packet->hdr.msg_type =
126 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
127 revoke_packet->msg.v1_msg.
128 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
129
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200130 ret = vmbus_sendpacket(device->channel,
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700131 revoke_packet,
132 sizeof(struct nvsp_message),
133 (unsigned long)revoke_packet,
134 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700135 /* If the failure is because the channel is rescinded;
136 * ignore the failure since we cannot send on a rescinded
137 * channel. This would allow us to properly cleanup
138 * even when the channel is rescinded.
139 */
140 if (device->channel->rescind)
141 ret = 0;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700142 /*
143 * If we failed here, we might as well return and
144 * have a leak rather than continue and a bugchk
145 */
146 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700147 netdev_err(ndev, "unable to send "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700148 "revoke receive buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700149 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700150 }
stephen hemminger8b532792017-08-09 17:46:11 -0700151 net_device->recv_section_cnt = 0;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700152 }
153
154 /* Teardown the gpadl on the vsp end */
155 if (net_device->recv_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200156 ret = vmbus_teardown_gpadl(device->channel,
157 net_device->recv_buf_gpadl_handle);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700158
159 /* If we failed here, we might as well return and have a leak
160 * rather than continue and a bugchk
161 */
162 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700163 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700164 "unable to teardown receive buffer's gpadl\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700165 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700166 }
167 net_device->recv_buf_gpadl_handle = 0;
168 }
169
170 if (net_device->recv_buf) {
171 /* Free up the receive buffer */
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800172 vfree(net_device->recv_buf);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700173 net_device->recv_buf = NULL;
174 }
175
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700176 /* Deal with the send buffer we may have setup.
177 * If we got a send section size, it means we received a
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800178 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
179 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700180 * to send a revoke msg here
181 */
stephen hemminger8b532792017-08-09 17:46:11 -0700182 if (net_device->send_section_cnt) {
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700183 /* Send the revoke receive buffer */
184 revoke_packet = &net_device->revoke_packet;
185 memset(revoke_packet, 0, sizeof(struct nvsp_message));
186
187 revoke_packet->hdr.msg_type =
188 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800189 revoke_packet->msg.v1_msg.revoke_send_buf.id =
190 NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700191
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200192 ret = vmbus_sendpacket(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700193 revoke_packet,
194 sizeof(struct nvsp_message),
195 (unsigned long)revoke_packet,
196 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700197
198 /* If the failure is because the channel is rescinded;
199 * ignore the failure since we cannot send on a rescinded
200 * channel. This would allow us to properly cleanup
201 * even when the channel is rescinded.
202 */
203 if (device->channel->rescind)
204 ret = 0;
205
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700206 /* If we failed here, we might as well return and
207 * have a leak rather than continue and a bugchk
208 */
209 if (ret != 0) {
210 netdev_err(ndev, "unable to send "
211 "revoke send buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700212 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700213 }
stephen hemminger8b532792017-08-09 17:46:11 -0700214 net_device->send_section_cnt = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700215 }
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
stephen hemminger7426b1a2017-07-28 08:59:45 -0700239int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx)
240{
241 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
242 int node = cpu_to_node(nvchan->channel->target_cpu);
243 size_t size;
244
245 size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data);
246 nvchan->mrc.slots = vzalloc_node(size, node);
247 if (!nvchan->mrc.slots)
248 nvchan->mrc.slots = vzalloc(size);
249
250 return nvchan->mrc.slots ? 0 : -ENOMEM;
251}
252
stephen hemminger95790832017-06-08 16:21:22 -0700253static int netvsc_init_buf(struct hv_device *device,
stephen hemminger8b532792017-08-09 17:46:11 -0700254 struct netvsc_device *net_device,
255 const struct netvsc_device_info *device_info)
Hank Janssenfceaf242009-07-13 15:34:54 -0700256{
stephen hemminger7426b1a2017-07-28 08:59:45 -0700257 struct nvsp_1_message_send_receive_buffer_complete *resp;
stephen hemminger95833372017-08-09 17:46:07 -0700258 struct net_device *ndev = hv_get_drvdata(device);
259 struct nvsp_message *init_packet;
stephen hemminger8b532792017-08-09 17:46:11 -0700260 unsigned int buf_size;
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700261 size_t map_words;
stephen hemminger95833372017-08-09 17:46:07 -0700262 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700263
stephen hemminger8b532792017-08-09 17:46:11 -0700264 /* Get receive buffer area. */
265 buf_size = device_info->recv_sections * net_device->recv_section_size;
266 buf_size = roundup(buf_size, PAGE_SIZE);
267
268 net_device->recv_buf = vzalloc(buf_size);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800269 if (!net_device->recv_buf) {
stephen hemminger8b532792017-08-09 17:46:11 -0700270 netdev_err(ndev,
271 "unable to allocate receive buffer of size %u\n",
272 buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700273 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800274 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700275 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700276
Bill Pemberton454f18a2009-07-27 16:47:24 -0400277 /*
278 * Establish the gpadl handle for this buffer on this
279 * channel. Note: This call uses the vmbus connection rather
280 * than the channel to establish the gpadl handle.
281 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800282 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
stephen hemminger8b532792017-08-09 17:46:11 -0700283 buf_size,
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800284 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700285 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700286 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700287 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800288 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700289 }
290
Bill Pemberton454f18a2009-07-27 16:47:24 -0400291 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800292 init_packet = &net_device->channel_init_pkt;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800293 memset(init_packet, 0, sizeof(struct nvsp_message));
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800294 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
295 init_packet->msg.v1_msg.send_recv_buf.
296 gpadl_handle = net_device->recv_buf_gpadl_handle;
297 init_packet->msg.v1_msg.
298 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700299
Bill Pemberton454f18a2009-07-27 16:47:24 -0400300 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800301 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700302 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800303 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800304 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700305 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700306 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700307 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700308 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800309 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700310 }
311
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200312 wait_for_completion(&net_device->channel_init_wait);
Hank Janssenfceaf242009-07-13 15:34:54 -0700313
Bill Pemberton454f18a2009-07-27 16:47:24 -0400314 /* Check the response */
stephen hemminger7426b1a2017-07-28 08:59:45 -0700315 resp = &init_packet->msg.v1_msg.send_recv_buf_complete;
316 if (resp->status != NVSP_STAT_SUCCESS) {
317 netdev_err(ndev,
318 "Unable to complete receive buffer initialization with NetVsp - status %d\n",
319 resp->status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700320 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800321 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700322 }
323
Bill Pemberton454f18a2009-07-27 16:47:24 -0400324 /* Parse the response */
stephen hemminger7426b1a2017-07-28 08:59:45 -0700325 netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n",
326 resp->num_sections, resp->sections[0].sub_alloc_size,
327 resp->sections[0].num_sub_allocs);
Hank Janssenfceaf242009-07-13 15:34:54 -0700328
stephen hemminger8b532792017-08-09 17:46:11 -0700329 /* There should only be one section for the entire receive buffer */
330 if (resp->num_sections != 1 || resp->sections[0].offset != 0) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700331 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800332 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700333 }
334
stephen hemminger8b532792017-08-09 17:46:11 -0700335 net_device->recv_section_size = resp->sections[0].sub_alloc_size;
336 net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
337
stephen hemminger7426b1a2017-07-28 08:59:45 -0700338 /* Setup receive completion ring */
339 net_device->recv_completion_cnt
stephen hemminger8b532792017-08-09 17:46:11 -0700340 = round_up(net_device->recv_section_cnt + 1,
stephen hemminger7426b1a2017-07-28 08:59:45 -0700341 PAGE_SIZE / sizeof(u64));
342 ret = netvsc_alloc_recv_comp_ring(net_device, 0);
343 if (ret)
344 goto cleanup;
345
346 /* Now setup the send buffer. */
stephen hemminger8b532792017-08-09 17:46:11 -0700347 buf_size = device_info->send_sections * net_device->send_section_size;
348 buf_size = round_up(buf_size, PAGE_SIZE);
349
350 net_device->send_buf = vzalloc(buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700351 if (!net_device->send_buf) {
stephen hemminger8b532792017-08-09 17:46:11 -0700352 netdev_err(ndev, "unable to allocate send buffer of size %u\n",
353 buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700354 ret = -ENOMEM;
355 goto cleanup;
356 }
357
358 /* Establish the gpadl handle for this buffer on this
359 * channel. Note: This call uses the vmbus connection rather
360 * than the channel to establish the gpadl handle.
361 */
362 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
stephen hemminger8b532792017-08-09 17:46:11 -0700363 buf_size,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700364 &net_device->send_buf_gpadl_handle);
365 if (ret != 0) {
366 netdev_err(ndev,
367 "unable to establish send buffer's gpadl\n");
368 goto cleanup;
369 }
370
371 /* Notify the NetVsp of the gpadl handle */
372 init_packet = &net_device->channel_init_pkt;
373 memset(init_packet, 0, sizeof(struct nvsp_message));
374 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800375 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700376 net_device->send_buf_gpadl_handle;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800377 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700378
379 /* Send the gpadl notification request */
380 ret = vmbus_sendpacket(device->channel, init_packet,
381 sizeof(struct nvsp_message),
382 (unsigned long)init_packet,
383 VM_PKT_DATA_INBAND,
384 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
385 if (ret != 0) {
386 netdev_err(ndev,
387 "unable to send send buffer's gpadl to netvsp\n");
388 goto cleanup;
389 }
390
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200391 wait_for_completion(&net_device->channel_init_wait);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700392
393 /* Check the response */
394 if (init_packet->msg.v1_msg.
395 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
396 netdev_err(ndev, "Unable to complete send buffer "
397 "initialization with NetVsp - status %d\n",
398 init_packet->msg.v1_msg.
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800399 send_send_buf_complete.status);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700400 ret = -EINVAL;
401 goto cleanup;
402 }
403
404 /* Parse the response */
405 net_device->send_section_size = init_packet->msg.
406 v1_msg.send_send_buf_complete.section_size;
407
stephen hemminger8b532792017-08-09 17:46:11 -0700408 /* Section count is simply the size divided by the section size. */
409 net_device->send_section_cnt = buf_size / net_device->send_section_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700410
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100411 netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
412 net_device->send_section_size, net_device->send_section_cnt);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700413
414 /* Setup state for managing the send buffer. */
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700415 map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700416
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700417 net_device->send_section_map = kcalloc(map_words, sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800418 if (net_device->send_section_map == NULL) {
419 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700420 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800421 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700422
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800423 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700424
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800425cleanup:
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200426 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700427
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800428exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700429 return ret;
430}
431
Haiyang Zhangf157e782011-12-15 13:45:16 -0800432/* Negotiate NVSP protocol version */
433static int negotiate_nvsp_ver(struct hv_device *device,
434 struct netvsc_device *net_device,
435 struct nvsp_message *init_packet,
436 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700437{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200438 struct net_device *ndev = hv_get_drvdata(device);
Nicholas Mc Guire7390fe92015-01-25 15:46:31 +0100439 int ret;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800440
441 memset(init_packet, 0, sizeof(struct nvsp_message));
442 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
443 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
444 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
445
446 /* Send the init request */
447 ret = vmbus_sendpacket(device->channel, init_packet,
448 sizeof(struct nvsp_message),
449 (unsigned long)init_packet,
450 VM_PKT_DATA_INBAND,
451 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
452
453 if (ret != 0)
454 return ret;
455
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200456 wait_for_completion(&net_device->channel_init_wait);
Haiyang Zhangf157e782011-12-15 13:45:16 -0800457
458 if (init_packet->msg.init_msg.init_complete.status !=
459 NVSP_STAT_SUCCESS)
460 return -EINVAL;
461
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800462 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800463 return 0;
464
Haiyang Zhang71790a22015-07-24 10:08:40 -0700465 /* NVSPv2 or later: Send NDIS config */
Haiyang Zhangf157e782011-12-15 13:45:16 -0800466 memset(init_packet, 0, sizeof(struct nvsp_message));
467 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200468 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000469 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800470
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700471 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
Haiyang Zhang71790a22015-07-24 10:08:40 -0700472 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
473
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700474 /* Teaming bit is needed to receive link speed updates */
475 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
476 }
477
Haiyang Zhangf157e782011-12-15 13:45:16 -0800478 ret = vmbus_sendpacket(device->channel, init_packet,
479 sizeof(struct nvsp_message),
480 (unsigned long)init_packet,
481 VM_PKT_DATA_INBAND, 0);
482
483 return ret;
484}
485
stephen hemminger95790832017-06-08 16:21:22 -0700486static int netvsc_connect_vsp(struct hv_device *device,
stephen hemminger8b532792017-08-09 17:46:11 -0700487 struct netvsc_device *net_device,
488 const struct netvsc_device_info *device_info)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800489{
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700490 const u32 ver_list[] = {
491 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
stephen hemminger95790832017-06-08 16:21:22 -0700492 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5
493 };
494 struct nvsp_message *init_packet;
495 int ndis_version, i, ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700496
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800497 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700498
Haiyang Zhangf157e782011-12-15 13:45:16 -0800499 /* Negotiate the latest NVSP protocol supported */
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700500 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800501 if (negotiate_nvsp_ver(device, net_device, init_packet,
502 ver_list[i]) == 0) {
503 net_device->nvsp_version = ver_list[i];
504 break;
505 }
506
507 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700508 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800509 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700510 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800511
512 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
513
Bill Pemberton454f18a2009-07-27 16:47:24 -0400514 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800515 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700516
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800517 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700518 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800519 else
520 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700521
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800522 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
523 init_packet->msg.v1_msg.
524 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800525 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800526 init_packet->msg.v1_msg.
527 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800528 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700529
Bill Pemberton454f18a2009-07-27 16:47:24 -0400530 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800531 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800532 sizeof(struct nvsp_message),
533 (unsigned long)init_packet,
534 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700535 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800536 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700537
Haiyang Zhang99d30162014-03-09 16:10:59 -0700538
stephen hemminger8b532792017-08-09 17:46:11 -0700539 ret = netvsc_init_buf(device, net_device, device_info);
Hank Janssenfceaf242009-07-13 15:34:54 -0700540
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800541cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700542 return ret;
543}
544
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200545static void netvsc_disconnect_vsp(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700546{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200547 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700548}
549
Hank Janssen3e189512010-03-04 22:11:00 +0000550/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800551 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700552 */
Stephen Hemmingere08f3ea2016-08-23 12:17:50 -0700553void netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700554{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200555 struct net_device *ndev = hv_get_drvdata(device);
556 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -0700557 struct netvsc_device *net_device
558 = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger15a863b2017-02-27 10:26:49 -0800559 int i;
Hank Janssenfceaf242009-07-13 15:34:54 -0700560
Stephen Hemminger8195b132017-09-06 13:53:05 -0700561 cancel_work_sync(&net_device->subchan_work);
562
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200563 netvsc_disconnect_vsp(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700564
stephen hemminger545a8e72017-03-22 14:51:00 -0700565 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700566
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700567 /*
568 * At this point, no one should be accessing net_device
569 * except in here
570 */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100571 netdev_dbg(ndev, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700572
Bill Pemberton454f18a2009-07-27 16:47:24 -0400573 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800574 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700575
stephen hemminger76bb5db2017-04-19 15:22:02 -0700576 /* And dissassociate NAPI context from device */
stephen hemminger79cd8742017-03-09 15:04:15 -0800577 for (i = 0; i < net_device->num_chn; i++)
stephen hemminger76bb5db2017-04-19 15:22:02 -0700578 netif_napi_del(&net_device->chan_table[i].napi);
stephen hemminger15a863b2017-02-27 10:26:49 -0800579
Bill Pemberton454f18a2009-07-27 16:47:24 -0400580 /* Release all resources */
stephen hemminger545a8e72017-03-22 14:51:00 -0700581 free_netvsc_device_rcu(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700582}
583
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000584#define RING_AVAIL_PERCENT_HIWATER 20
585#define RING_AVAIL_PERCENT_LOWATER 10
586
587/*
588 * Get the percentage of available bytes to write in the ring.
589 * The return value is in range from 0 to 100.
590 */
591static inline u32 hv_ringbuf_avail_percent(
592 struct hv_ring_buffer_info *ring_info)
593{
594 u32 avail_read, avail_write;
595
596 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
597
598 return avail_write * 100 / ring_info->ring_datasize;
599}
600
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700601static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
602 u32 index)
603{
604 sync_change_bit(index, net_device->send_section_map);
605}
606
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700607static void netvsc_send_tx_complete(struct netvsc_device *net_device,
608 struct vmbus_channel *incoming_channel,
609 struct hv_device *device,
stephen hemmingerf9645432017-04-07 14:41:19 -0400610 const struct vmpacket_descriptor *desc,
611 int budget)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700612{
stephen hemminger50698d82017-02-27 10:26:47 -0800613 struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700614 struct net_device *ndev = hv_get_drvdata(device);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700615 struct vmbus_channel *channel = device->channel;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700616 u16 q_idx = 0;
617 int queue_sends;
618
619 /* Notify the layer above us */
620 if (likely(skb)) {
stephen hemminger793e3952017-01-24 13:06:12 -0800621 const struct hv_netvsc_packet *packet
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700622 = (struct hv_netvsc_packet *)skb->cb;
stephen hemminger793e3952017-01-24 13:06:12 -0800623 u32 send_index = packet->send_buf_index;
624 struct netvsc_stats *tx_stats;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700625
626 if (send_index != NETVSC_INVALID_INDEX)
627 netvsc_free_send_slot(net_device, send_index);
stephen hemminger793e3952017-01-24 13:06:12 -0800628 q_idx = packet->q_idx;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700629 channel = incoming_channel;
630
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800631 tx_stats = &net_device->chan_table[q_idx].tx_stats;
stephen hemminger793e3952017-01-24 13:06:12 -0800632
633 u64_stats_update_begin(&tx_stats->syncp);
634 tx_stats->packets += packet->total_packets;
635 tx_stats->bytes += packet->total_bytes;
636 u64_stats_update_end(&tx_stats->syncp);
637
stephen hemmingerf9645432017-04-07 14:41:19 -0400638 napi_consume_skb(skb, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700639 }
640
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800641 queue_sends =
642 atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700643
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800644 if (net_device->destroy && queue_sends == 0)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700645 wake_up(&net_device->wait_drain);
646
647 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700648 (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER ||
649 queue_sends < 1))
650 netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
651}
652
KY Srinivasan97c17232014-02-16 16:38:44 -0800653static void netvsc_send_completion(struct netvsc_device *net_device,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800654 struct vmbus_channel *incoming_channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800655 struct hv_device *device,
stephen hemmingerf9645432017-04-07 14:41:19 -0400656 const struct vmpacket_descriptor *desc,
657 int budget)
Hank Janssenfceaf242009-07-13 15:34:54 -0700658{
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800659 struct nvsp_message *nvsp_packet = hv_pkt_data(desc);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200660 struct net_device *ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700661
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700662 switch (nvsp_packet->hdr.msg_type) {
663 case NVSP_MSG_TYPE_INIT_COMPLETE:
664 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
665 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
666 case NVSP_MSG5_TYPE_SUBCHANNEL:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400667 /* Copy the response back */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800668 memcpy(&net_device->channel_init_pkt, nvsp_packet,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700669 sizeof(struct nvsp_message));
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700670 complete(&net_device->channel_init_wait);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700671 break;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000672
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700673 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
674 netvsc_send_tx_complete(net_device, incoming_channel,
stephen hemmingerf9645432017-04-07 14:41:19 -0400675 device, desc, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700676 break;
Hank Janssenfceaf242009-07-13 15:34:54 -0700677
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700678 default:
679 netdev_err(ndev,
680 "Unknown send completion type %d received!!\n",
681 nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700682 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700683}
684
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700685static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
686{
stephen hemmingerb58a1852017-01-24 13:06:14 -0800687 unsigned long *map_addr = net_device->send_section_map;
688 unsigned int i;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700689
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700690 for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
stephen hemmingerb58a1852017-01-24 13:06:14 -0800691 if (sync_test_and_set_bit(i, map_addr) == 0)
692 return i;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700693 }
stephen hemmingerb58a1852017-01-24 13:06:14 -0800694
695 return NETVSC_INVALID_INDEX;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700696}
697
Lad, Prabhakarda19fcd2015-02-05 15:06:33 +0000698static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
699 unsigned int section_index,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700700 u32 pend_size,
KY Srinivasan24476762015-12-01 16:43:06 -0800701 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800702 struct rndis_message *rndis_msg,
stephen hemminger02b6de02017-07-28 08:59:44 -0700703 struct hv_page_buffer *pb,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800704 struct sk_buff *skb)
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700705{
706 char *start = net_device->send_buf;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700707 char *dest = start + (section_index * net_device->send_section_size)
708 + pend_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700709 int i;
710 u32 msg_size = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700711 u32 padding = 0;
712 u32 remain = packet->total_data_buflen % net_device->pkt_align;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700713 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
714 packet->page_buf_cnt;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700715
716 /* Add padding */
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700717 if (skb->xmit_more && remain && !packet->cp_partial) {
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700718 padding = net_device->pkt_align - remain;
KY Srinivasan24476762015-12-01 16:43:06 -0800719 rndis_msg->msg_len += padding;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700720 packet->total_data_buflen += padding;
721 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700722
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700723 for (i = 0; i < page_count; i++) {
stephen hemminger02b6de02017-07-28 08:59:44 -0700724 char *src = phys_to_virt(pb[i].pfn << PAGE_SHIFT);
725 u32 offset = pb[i].offset;
726 u32 len = pb[i].len;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700727
728 memcpy(dest, (src + offset), len);
729 msg_size += len;
730 dest += len;
731 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700732
733 if (padding) {
734 memset(dest, 0, padding);
735 msg_size += padding;
736 }
737
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700738 return msg_size;
739}
740
Stephen Hemminger3a8963a2016-09-09 12:45:24 -0700741static inline int netvsc_send_pkt(
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200742 struct hv_device *device,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700743 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800744 struct netvsc_device *net_device,
stephen hemminger02b6de02017-07-28 08:59:44 -0700745 struct hv_page_buffer *pb,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800746 struct sk_buff *skb)
Hank Janssenfceaf242009-07-13 15:34:54 -0700747{
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700748 struct nvsp_message nvmsg;
Joe Perches956a25c2017-07-31 10:30:54 -0700749 struct nvsp_1_message_send_rndis_packet * const rpkt =
750 &nvmsg.msg.v1_msg.send_rndis_pkt;
751 struct netvsc_channel * const nvchan =
752 &net_device->chan_table[packet->q_idx];
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800753 struct vmbus_channel *out_channel = nvchan->channel;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200754 struct net_device *ndev = hv_get_drvdata(device);
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800755 struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700756 u64 req_id;
757 int ret;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700758 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700759
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700760 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
Joe Perches956a25c2017-07-31 10:30:54 -0700761 if (skb)
762 rpkt->channel_type = 0; /* 0 is RMC_DATA */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700763 else
Joe Perches956a25c2017-07-31 10:30:54 -0700764 rpkt->channel_type = 1; /* 1 is RMC_CONTROL */
765
766 rpkt->send_buf_section_index = packet->send_buf_index;
767 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
768 rpkt->send_buf_section_size = 0;
769 else
770 rpkt->send_buf_section_size = packet->total_data_buflen;
Hank Janssenfceaf242009-07-13 15:34:54 -0700771
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800772 req_id = (ulong)skb;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000773
Haiyang Zhangc3582a22014-12-01 13:28:39 -0800774 if (out_channel->rescind)
775 return -ENODEV;
776
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800777 if (packet->page_buf_cnt) {
stephen hemminger02b6de02017-07-28 08:59:44 -0700778 if (packet->cp_partial)
779 pb += packet->rmsg_pgcnt;
780
stephen hemminger5a668d82017-08-16 08:56:25 -0700781 ret = vmbus_sendpacket_pagebuffer(out_channel,
782 pb, packet->page_buf_cnt,
783 &nvmsg, sizeof(nvmsg),
784 req_id);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700785 } else {
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700786 ret = vmbus_sendpacket(out_channel,
787 &nvmsg, sizeof(nvmsg),
788 req_id, VM_PKT_DATA_INBAND,
789 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700790 }
791
Haiyang Zhang1d068252011-12-02 11:56:25 -0800792 if (ret == 0) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800793 atomic_inc_return(&nvchan->queue_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700794
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800795 if (ring_avail < RING_AVAIL_PERCENT_LOWATER)
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800796 netif_tx_stop_queue(txq);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800797 } else if (ret == -EAGAIN) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800798 netif_tx_stop_queue(txq);
799 if (atomic_read(&nvchan->queue_sends) < 1) {
800 netif_tx_wake_queue(txq);
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000801 ret = -ENOSPC;
802 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800803 } else {
stephen hemminger4a2176c2017-07-28 08:59:43 -0700804 netdev_err(ndev,
805 "Unable to send packet pages %u len %u, ret %d\n",
806 packet->page_buf_cnt, packet->total_data_buflen,
807 ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800808 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700809
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700810 return ret;
811}
812
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800813/* Move packet out of multi send data (msd), and clear msd */
814static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
815 struct sk_buff **msd_skb,
816 struct multi_send_data *msdp)
817{
818 *msd_skb = msdp->skb;
819 *msd_send = msdp->pkt;
820 msdp->skb = NULL;
821 msdp->pkt = NULL;
822 msdp->count = 0;
823}
824
stephen hemminger2a926f72017-07-19 11:53:17 -0700825/* RCU already held by caller */
826int netvsc_send(struct net_device_context *ndev_ctx,
KY Srinivasan24476762015-12-01 16:43:06 -0800827 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800828 struct rndis_message *rndis_msg,
stephen hemminger02b6de02017-07-28 08:59:44 -0700829 struct hv_page_buffer *pb,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800830 struct sk_buff *skb)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700831{
stephen hemminger39629812017-07-19 11:53:19 -0700832 struct netvsc_device *net_device
stephen hemminger867047c2017-07-28 08:59:42 -0700833 = rcu_dereference_bh(ndev_ctx->nvdev);
stephen hemminger2a926f72017-07-19 11:53:17 -0700834 struct hv_device *device = ndev_ctx->device_ctx;
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700835 int ret = 0;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800836 struct netvsc_channel *nvchan;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700837 u32 pktlen = packet->total_data_buflen, msd_len = 0;
838 unsigned int section_index = NETVSC_INVALID_INDEX;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700839 struct multi_send_data *msdp;
840 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800841 struct sk_buff *msd_skb = NULL;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700842 bool try_batch;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800843 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700844
stephen hemminger592b4fe2017-06-08 16:21:23 -0700845 /* If device is rescinded, return error and packet will get dropped. */
stephen hemminger2a926f72017-07-19 11:53:17 -0700846 if (unlikely(!net_device || net_device->destroy))
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700847 return -ENODEV;
848
Vitaly Kuznetsove8f0a892016-10-19 15:53:01 +0200849 /* We may race with netvsc_connect_vsp()/netvsc_init_buf() and get
850 * here before the negotiation with the host is finished and
851 * send_section_map may not be allocated yet.
852 */
stephen hemminger2d694d22017-06-08 16:21:21 -0700853 if (unlikely(!net_device->send_section_map))
Vitaly Kuznetsove8f0a892016-10-19 15:53:01 +0200854 return -EAGAIN;
855
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800856 nvchan = &net_device->chan_table[packet->q_idx];
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700857 packet->send_buf_index = NETVSC_INVALID_INDEX;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700858 packet->cp_partial = false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700859
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800860 /* Send control message directly without accessing msd (Multi-Send
861 * Data) field which may be changed during data packet processing.
862 */
863 if (!skb) {
864 cur_send = packet;
865 goto send_now;
866 }
867
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700868 /* batch packets in send buffer if possible */
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800869 msdp = &nvchan->msd;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700870 if (msdp->pkt)
871 msd_len = msdp->pkt->total_data_buflen;
872
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700873 try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700874 if (try_batch && msd_len + pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700875 net_device->send_section_size) {
876 section_index = msdp->pkt->send_buf_index;
877
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700878 } else if (try_batch && msd_len + packet->rmsg_size <
879 net_device->send_section_size) {
880 section_index = msdp->pkt->send_buf_index;
881 packet->cp_partial = true;
882
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700883 } else if (pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700884 net_device->send_section_size) {
885 section_index = netvsc_get_next_send_section(net_device);
stephen hemmingercad5c192017-08-09 17:46:12 -0700886 if (unlikely(section_index == NETVSC_INVALID_INDEX)) {
887 ++ndev_ctx->eth_stats.tx_send_full;
888 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800889 move_pkt_msd(&msd_send, &msd_skb, msdp);
890 msd_len = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700891 }
892 }
893
894 if (section_index != NETVSC_INVALID_INDEX) {
895 netvsc_copy_to_send_buf(net_device,
896 section_index, msd_len,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800897 packet, rndis_msg, pb, skb);
KY Srinivasanb08cc792015-03-29 21:08:42 -0700898
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700899 packet->send_buf_index = section_index;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700900
901 if (packet->cp_partial) {
902 packet->page_buf_cnt -= packet->rmsg_pgcnt;
903 packet->total_data_buflen = msd_len + packet->rmsg_size;
904 } else {
905 packet->page_buf_cnt = 0;
906 packet->total_data_buflen += msd_len;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700907 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700908
stephen hemminger793e3952017-01-24 13:06:12 -0800909 if (msdp->pkt) {
910 packet->total_packets += msdp->pkt->total_packets;
911 packet->total_bytes += msdp->pkt->total_bytes;
912 }
913
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800914 if (msdp->skb)
Stephen Hemminger17db4bc2016-09-22 16:56:29 -0700915 dev_consume_skb_any(msdp->skb);
Haiyang Zhangee90b812015-04-06 15:22:54 -0700916
KY Srinivasanbde79be2015-12-01 16:43:17 -0800917 if (xmit_more && !packet->cp_partial) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800918 msdp->skb = skb;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700919 msdp->pkt = packet;
920 msdp->count++;
921 } else {
922 cur_send = packet;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800923 msdp->skb = NULL;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700924 msdp->pkt = NULL;
925 msdp->count = 0;
926 }
927 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800928 move_pkt_msd(&msd_send, &msd_skb, msdp);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700929 cur_send = packet;
930 }
931
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700932 if (msd_send) {
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700933 int m_ret = netvsc_send_pkt(device, msd_send, net_device,
934 NULL, msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700935
936 if (m_ret != 0) {
937 netvsc_free_send_slot(net_device,
938 msd_send->send_buf_index);
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800939 dev_kfree_skb_any(msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700940 }
941 }
942
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800943send_now:
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700944 if (cur_send)
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200945 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700946
Jerry Snitselaar7aab5152015-05-04 10:57:16 -0700947 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
948 netvsc_free_send_slot(net_device, section_index);
Haiyang Zhangd953ca42015-01-29 12:34:49 -0800949
Hank Janssenfceaf242009-07-13 15:34:54 -0700950 return ret;
951}
952
stephen hemminger7426b1a2017-07-28 08:59:45 -0700953/* Send pending recv completions */
stephen hemmingercad5c192017-08-09 17:46:12 -0700954static int send_recv_completions(struct net_device *ndev,
955 struct netvsc_device *nvdev,
956 struct netvsc_channel *nvchan)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700957{
stephen hemminger7426b1a2017-07-28 08:59:45 -0700958 struct multi_recv_comp *mrc = &nvchan->mrc;
959 struct recv_comp_msg {
960 struct nvsp_message_header hdr;
961 u32 status;
962 } __packed;
963 struct recv_comp_msg msg = {
964 .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
965 };
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700966 int ret;
967
stephen hemminger7426b1a2017-07-28 08:59:45 -0700968 while (mrc->first != mrc->next) {
969 const struct recv_comp_data *rcd
970 = mrc->slots + mrc->first;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700971
stephen hemminger7426b1a2017-07-28 08:59:45 -0700972 msg.status = rcd->status;
973 ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg),
974 rcd->tid, VM_PKT_COMP, 0);
stephen hemmingercad5c192017-08-09 17:46:12 -0700975 if (unlikely(ret)) {
976 struct net_device_context *ndev_ctx = netdev_priv(ndev);
977
978 ++ndev_ctx->eth_stats.rx_comp_busy;
stephen hemminger7426b1a2017-07-28 08:59:45 -0700979 return ret;
stephen hemmingercad5c192017-08-09 17:46:12 -0700980 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700981
stephen hemminger7426b1a2017-07-28 08:59:45 -0700982 if (++mrc->first == nvdev->recv_completion_cnt)
983 mrc->first = 0;
984 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700985
stephen hemminger7426b1a2017-07-28 08:59:45 -0700986 /* receive completion ring has been emptied */
987 if (unlikely(nvdev->destroy))
988 wake_up(&nvdev->wait_drain);
989
990 return 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700991}
992
stephen hemminger7426b1a2017-07-28 08:59:45 -0700993/* Count how many receive completions are outstanding */
994static void recv_comp_slot_avail(const struct netvsc_device *nvdev,
995 const struct multi_recv_comp *mrc,
996 u32 *filled, u32 *avail)
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700997{
stephen hemminger7426b1a2017-07-28 08:59:45 -0700998 u32 count = nvdev->recv_completion_cnt;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700999
stephen hemminger7426b1a2017-07-28 08:59:45 -07001000 if (mrc->next >= mrc->first)
1001 *filled = mrc->next - mrc->first;
1002 else
1003 *filled = (count - mrc->first) + mrc->next;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001004
stephen hemminger7426b1a2017-07-28 08:59:45 -07001005 *avail = count - *filled - 1;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001006}
1007
stephen hemminger7426b1a2017-07-28 08:59:45 -07001008/* Add receive complete to ring to send to host. */
1009static void enq_receive_complete(struct net_device *ndev,
1010 struct netvsc_device *nvdev, u16 q_idx,
1011 u64 tid, u32 status)
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001012{
stephen hemminger7426b1a2017-07-28 08:59:45 -07001013 struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx];
1014 struct multi_recv_comp *mrc = &nvchan->mrc;
1015 struct recv_comp_data *rcd;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001016 u32 filled, avail;
1017
stephen hemminger7426b1a2017-07-28 08:59:45 -07001018 recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001019
stephen hemminger7426b1a2017-07-28 08:59:45 -07001020 if (unlikely(filled > NAPI_POLL_WEIGHT)) {
stephen hemmingercad5c192017-08-09 17:46:12 -07001021 send_recv_completions(ndev, nvdev, nvchan);
stephen hemminger7426b1a2017-07-28 08:59:45 -07001022 recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001023 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001024
stephen hemminger7426b1a2017-07-28 08:59:45 -07001025 if (unlikely(!avail)) {
1026 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1027 q_idx, tid);
1028 return;
1029 }
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001030
stephen hemminger7426b1a2017-07-28 08:59:45 -07001031 rcd = mrc->slots + mrc->next;
1032 rcd->tid = tid;
1033 rcd->status = status;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001034
stephen hemminger7426b1a2017-07-28 08:59:45 -07001035 if (++mrc->next == nvdev->recv_completion_cnt)
1036 mrc->next = 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001037}
1038
stephen hemminger15a863b2017-02-27 10:26:49 -08001039static int netvsc_receive(struct net_device *ndev,
stephen hemminger7426b1a2017-07-28 08:59:45 -07001040 struct netvsc_device *net_device,
1041 struct net_device_context *net_device_ctx,
1042 struct hv_device *device,
1043 struct vmbus_channel *channel,
1044 const struct vmpacket_descriptor *desc,
1045 struct nvsp_message *nvsp)
Hank Janssenfceaf242009-07-13 15:34:54 -07001046{
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001047 const struct vmtransfer_page_packet_header *vmxferpage_packet
1048 = container_of(desc, const struct vmtransfer_page_packet_header, d);
stephen hemminger15a863b2017-02-27 10:26:49 -08001049 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemmingerdc54a082017-01-24 13:06:08 -08001050 char *recv_buf = net_device->recv_buf;
Haiyang Zhang4baab262014-04-21 14:54:43 -07001051 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -08001052 int i;
1053 int count = 0;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -07001054
Bill Pemberton454f18a2009-07-27 16:47:24 -04001055 /* Make sure this is a valid nvsp packet */
stephen hemmingerdc54a082017-01-24 13:06:08 -08001056 if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
1057 netif_err(net_device_ctx, rx_err, ndev,
1058 "Unknown nvsp packet type received %u\n",
1059 nvsp->hdr.msg_type);
stephen hemminger15a863b2017-02-27 10:26:49 -08001060 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001061 }
1062
stephen hemmingerdc54a082017-01-24 13:06:08 -08001063 if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
1064 netif_err(net_device_ctx, rx_err, ndev,
1065 "Invalid xfer page set id - expecting %x got %x\n",
1066 NETVSC_RECEIVE_BUFFER_ID,
1067 vmxferpage_packet->xfer_pageset_id);
stephen hemminger15a863b2017-02-27 10:26:49 -08001068 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001069 }
1070
Haiyang Zhang4baab262014-04-21 14:54:43 -07001071 count = vmxferpage_packet->range_cnt;
Hank Janssenfceaf242009-07-13 15:34:54 -07001072
Bill Pemberton454f18a2009-07-27 16:47:24 -04001073 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -07001074 for (i = 0; i < count; i++) {
stephen hemmingerdc54a082017-01-24 13:06:08 -08001075 void *data = recv_buf
1076 + vmxferpage_packet->ranges[i].byte_offset;
1077 u32 buflen = vmxferpage_packet->ranges[i].byte_count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001078
Bill Pemberton454f18a2009-07-27 16:47:24 -04001079 /* Pass it to the upper layer */
stephen hemmingerdc54a082017-01-24 13:06:08 -08001080 status = rndis_filter_receive(ndev, net_device, device,
1081 channel, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -07001082 }
1083
stephen hemminger7426b1a2017-07-28 08:59:45 -07001084 enq_receive_complete(ndev, net_device, q_idx,
1085 vmxferpage_packet->d.trans_id, status);
stephen hemminger15a863b2017-02-27 10:26:49 -08001086
stephen hemminger15a863b2017-02-27 10:26:49 -08001087 return count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001088}
1089
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001090static void netvsc_send_table(struct hv_device *hdev,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001091 struct nvsp_message *nvmsg)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001092{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001093 struct net_device *ndev = hv_get_drvdata(hdev);
stephen hemminger7ce10122017-03-09 14:58:29 -08001094 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001095 int i;
1096 u32 count, *tab;
1097
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001098 count = nvmsg->msg.v5_msg.send_table.count;
1099 if (count != VRSS_SEND_TAB_SIZE) {
1100 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1101 return;
1102 }
1103
1104 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1105 nvmsg->msg.v5_msg.send_table.offset);
1106
1107 for (i = 0; i < count; i++)
stephen hemminger7ce10122017-03-09 14:58:29 -08001108 net_device_ctx->tx_send_table[i] = tab[i];
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001109}
1110
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001111static void netvsc_send_vf(struct net_device_context *net_device_ctx,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001112 struct nvsp_message *nvmsg)
1113{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001114 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1115 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
Haiyang Zhang71790a22015-07-24 10:08:40 -07001116}
1117
1118static inline void netvsc_receive_inband(struct hv_device *hdev,
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001119 struct net_device_context *net_device_ctx,
1120 struct nvsp_message *nvmsg)
Haiyang Zhang71790a22015-07-24 10:08:40 -07001121{
1122 switch (nvmsg->hdr.msg_type) {
1123 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1124 netvsc_send_table(hdev, nvmsg);
1125 break;
1126
1127 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001128 netvsc_send_vf(net_device_ctx, nvmsg);
Haiyang Zhang71790a22015-07-24 10:08:40 -07001129 break;
1130 }
1131}
1132
stephen hemminger15a863b2017-02-27 10:26:49 -08001133static int netvsc_process_raw_pkt(struct hv_device *device,
1134 struct vmbus_channel *channel,
1135 struct netvsc_device *net_device,
1136 struct net_device *ndev,
stephen hemmingerf9645432017-04-07 14:41:19 -04001137 const struct vmpacket_descriptor *desc,
1138 int budget)
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001139{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001140 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001141 struct nvsp_message *nvmsg = hv_pkt_data(desc);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001142
1143 switch (desc->type) {
1144 case VM_PKT_COMP:
stephen hemmingerf9645432017-04-07 14:41:19 -04001145 netvsc_send_completion(net_device, channel, device,
1146 desc, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001147 break;
1148
1149 case VM_PKT_DATA_USING_XFER_PAGES:
stephen hemminger15a863b2017-02-27 10:26:49 -08001150 return netvsc_receive(ndev, net_device, net_device_ctx,
1151 device, channel, desc, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001152 break;
1153
1154 case VM_PKT_DATA_INBAND:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001155 netvsc_receive_inband(device, net_device_ctx, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001156 break;
1157
1158 default:
1159 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001160 desc->type, desc->trans_id);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001161 break;
1162 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001163
1164 return 0;
1165}
1166
1167static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
1168{
1169 struct vmbus_channel *primary = channel->primary_channel;
1170
1171 return primary ? primary->device_obj : channel->device_obj;
1172}
1173
stephen hemminger262b7f12017-03-16 16:12:38 -07001174/* Network processing softirq
1175 * Process data in incoming ring buffer from host
1176 * Stops when ring is empty or budget is met or exceeded.
1177 */
stephen hemminger15a863b2017-02-27 10:26:49 -08001178int netvsc_poll(struct napi_struct *napi, int budget)
1179{
1180 struct netvsc_channel *nvchan
1181 = container_of(napi, struct netvsc_channel, napi);
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001182 struct netvsc_device *net_device = nvchan->net_device;
stephen hemminger15a863b2017-02-27 10:26:49 -08001183 struct vmbus_channel *channel = nvchan->channel;
1184 struct hv_device *device = netvsc_channel_to_device(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001185 struct net_device *ndev = hv_get_drvdata(device);
stephen hemminger15a863b2017-02-27 10:26:49 -08001186 int work_done = 0;
1187
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001188 /* If starting a new interval */
1189 if (!nvchan->desc)
1190 nvchan->desc = hv_pkt_iter_first(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001191
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001192 while (nvchan->desc && work_done < budget) {
1193 work_done += netvsc_process_raw_pkt(device, channel, net_device,
stephen hemmingerf9645432017-04-07 14:41:19 -04001194 ndev, nvchan->desc, budget);
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001195 nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
stephen hemminger15a863b2017-02-27 10:26:49 -08001196 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001197
stephen hemmingerf4e40362017-07-28 08:59:47 -07001198 /* If send of pending receive completions suceeded
1199 * and did not exhaust NAPI budget this time
stephen hemminger7426b1a2017-07-28 08:59:45 -07001200 * and not doing busy poll
stephen hemmingerf4e40362017-07-28 08:59:47 -07001201 * then re-enable host interrupts
1202 * and reschedule if ring is not empty.
stephen hemminger262b7f12017-03-16 16:12:38 -07001203 */
stephen hemmingercad5c192017-08-09 17:46:12 -07001204 if (send_recv_completions(ndev, net_device, nvchan) == 0 &&
stephen hemminger7426b1a2017-07-28 08:59:45 -07001205 work_done < budget &&
stephen hemminger15a863b2017-02-27 10:26:49 -08001206 napi_complete_done(napi, work_done) &&
stephen hemminger7426b1a2017-07-28 08:59:45 -07001207 hv_end_read(&channel->inbound)) {
1208 hv_begin_read(&channel->inbound);
stephen hemminger15a863b2017-02-27 10:26:49 -08001209 napi_reschedule(napi);
stephen hemminger7426b1a2017-07-28 08:59:45 -07001210 }
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001211
1212 /* Driver may overshoot since multiple packets per descriptor */
1213 return min(work_done, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001214}
1215
stephen hemminger262b7f12017-03-16 16:12:38 -07001216/* Call back when data is available in host ring buffer.
1217 * Processing is deferred until network softirq (NAPI)
1218 */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001219void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001220{
stephen hemminger6de38af2017-03-16 16:12:37 -07001221 struct netvsc_channel *nvchan = context;
stephen hemminger43bf99c2017-07-24 10:57:27 -07001222 struct vmbus_channel *channel = nvchan->channel;
1223 struct hv_ring_buffer_info *rbi = &channel->inbound;
1224
1225 /* preload first vmpacket descriptor */
1226 prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
stephen hemminger0b307eb2017-01-24 13:05:58 -08001227
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001228 if (napi_schedule_prep(&nvchan->napi)) {
1229 /* disable interupts from host */
stephen hemminger43bf99c2017-07-24 10:57:27 -07001230 hv_begin_read(rbi);
stephen hemminger0d6dd352017-03-09 15:04:14 -08001231
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001232 __napi_schedule(&nvchan->napi);
1233 }
Hank Janssenfceaf242009-07-13 15:34:54 -07001234}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001235
1236/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001237 * netvsc_device_add - Callback when the device belonging to this
1238 * driver is added
1239 */
stephen hemminger9749fed2017-07-19 11:53:16 -07001240struct netvsc_device *netvsc_device_add(struct hv_device *device,
1241 const struct netvsc_device_info *device_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001242{
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001243 int i, ret = 0;
stephen hemminger2c7f83c2017-01-24 13:06:09 -08001244 int ring_size = device_info->ring_size;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001245 struct netvsc_device *net_device;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001246 struct net_device *ndev = hv_get_drvdata(device);
1247 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001248
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001249 net_device = alloc_net_device();
Dan Carpenterb1c84922014-09-04 14:11:23 +03001250 if (!net_device)
stephen hemminger9749fed2017-07-19 11:53:16 -07001251 return ERR_PTR(-ENOMEM);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001252
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001253 net_device->ring_size = ring_size;
1254
stephen hemminger15a863b2017-02-27 10:26:49 -08001255 /* Because the device uses NAPI, all the interrupt batching and
1256 * control is done via Net softirq, not the channel handling
1257 */
1258 set_channel_read_mode(device->channel, HV_CALL_ISR);
1259
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001260 /* If we're reopening the device we may have multiple queues, fill the
1261 * chn_table with the default channel to use it before subchannels are
1262 * opened.
1263 * Initialize the channel state before we open;
1264 * we can be interrupted as soon as we open the channel.
1265 */
1266
1267 for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
1268 struct netvsc_channel *nvchan = &net_device->chan_table[i];
1269
1270 nvchan->channel = device->channel;
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001271 nvchan->net_device = net_device;
Florian Fainelli4a0dee12017-08-01 12:11:12 -07001272 u64_stats_init(&nvchan->tx_stats.syncp);
1273 u64_stats_init(&nvchan->rx_stats.syncp);
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001274 }
1275
stephen hemminger2be0f262017-05-03 16:59:21 -07001276 /* Enable NAPI handler before init callbacks */
1277 netif_napi_add(ndev, &net_device->chan_table[0].napi,
1278 netvsc_poll, NAPI_POLL_WEIGHT);
1279
Haiyang Zhangb637e022011-04-21 12:30:45 -07001280 /* Open the channel */
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001281 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1282 ring_size * PAGE_SIZE, NULL, 0,
stephen hemminger6de38af2017-03-16 16:12:37 -07001283 netvsc_channel_cb,
1284 net_device->chan_table);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001285
1286 if (ret != 0) {
stephen hemminger2be0f262017-05-03 16:59:21 -07001287 netif_napi_del(&net_device->chan_table[0].napi);
Haiyang Zhangd9871152011-09-01 12:19:41 -07001288 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001289 goto cleanup;
1290 }
1291
1292 /* Channel is opened */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +01001293 netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001294
stephen hemminger15a863b2017-02-27 10:26:49 -08001295 napi_enable(&net_device->chan_table[0].napi);
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001296
1297 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1298 * populated.
1299 */
stephen hemminger545a8e72017-03-22 14:51:00 -07001300 rcu_assign_pointer(net_device_ctx->nvdev, net_device);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001301
Haiyang Zhangb637e022011-04-21 12:30:45 -07001302 /* Connect with the NetVsp */
stephen hemminger8b532792017-08-09 17:46:11 -07001303 ret = netvsc_connect_vsp(device, net_device, device_info);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001304 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001305 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001306 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001307 goto close;
1308 }
1309
stephen hemminger9749fed2017-07-19 11:53:16 -07001310 return net_device;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001311
1312close:
stephen hemminger49393342017-07-28 08:59:46 -07001313 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
1314 napi_disable(&net_device->chan_table[0].napi);
stephen hemminger15a863b2017-02-27 10:26:49 -08001315
Haiyang Zhangb637e022011-04-21 12:30:45 -07001316 /* Now, we can close the channel safely */
1317 vmbus_close(device->channel);
1318
1319cleanup:
stephen hemminger545a8e72017-03-22 14:51:00 -07001320 free_netvsc_device(&net_device->rcu);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001321
stephen hemminger9749fed2017-07-19 11:53:16 -07001322 return ERR_PTR(ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001323}