blob: e70a44273f55421da0dbcee62e39b9af4a9f8e64 [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 Hemmingera7f99d02017-12-01 11:01:47 -080034#include <linux/reciprocal_div.h>
stephen hemminger9749fed2017-07-19 11:53:16 -070035
KY Srinivasanc25aaf82014-04-30 10:14:31 -070036#include <asm/sync_bitops.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070037
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070038#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070039
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070040/*
41 * Switch the data path from the synthetic interface to the VF
42 * interface.
43 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020044void netvsc_switch_datapath(struct net_device *ndev, bool vf)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070045{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +020046 struct net_device_context *net_device_ctx = netdev_priv(ndev);
47 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger79e8cbe2017-07-19 11:53:13 -070048 struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020049 struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070050
51 memset(init_pkt, 0, sizeof(struct nvsp_message));
52 init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
53 if (vf)
54 init_pkt->msg.v4_msg.active_dp.active_datapath =
55 NVSP_DATAPATH_VF;
56 else
57 init_pkt->msg.v4_msg.active_dp.active_datapath =
58 NVSP_DATAPATH_SYNTHETIC;
59
60 vmbus_sendpacket(dev->channel, init_pkt,
61 sizeof(struct nvsp_message),
62 (unsigned long)init_pkt,
63 VM_PKT_DATA_INBAND, 0);
64}
65
Vitaly Kuznetsov88098832016-05-13 13:55:25 +020066static struct netvsc_device *alloc_net_device(void)
Hank Janssenfceaf242009-07-13 15:34:54 -070067{
Haiyang Zhang85799a32010-12-10 12:03:54 -080068 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070069
Haiyang Zhang85799a32010-12-10 12:03:54 -080070 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
71 if (!net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -070072 return NULL;
73
Haiyang Zhangdc5cd892012-06-04 06:42:38 +000074 init_waitqueue_head(&net_device->wait_drain);
K. Y. Srinivasanc38b9c72011-08-27 11:31:12 -070075 net_device->destroy = false;
Stephen Hemminger0da6edb2017-12-12 16:48:39 -080076
Haiyang Zhang7c3877f2015-03-26 09:03:37 -070077 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
78 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
stephen hemminger8b532792017-08-09 17:46:11 -070079
Stephen Hemmingerfd612602016-08-23 12:17:51 -070080 init_completion(&net_device->channel_init_wait);
stephen hemminger732e4982017-08-03 17:13:54 -070081 init_waitqueue_head(&net_device->subchan_open);
Stephen Hemminger8195b132017-09-06 13:53:05 -070082 INIT_WORK(&net_device->subchan_work, rndis_set_subchannel);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -070083
Haiyang Zhang85799a32010-12-10 12:03:54 -080084 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070085}
86
stephen hemminger545a8e72017-03-22 14:51:00 -070087static void free_netvsc_device(struct rcu_head *head)
Haiyang Zhangf90251c2014-08-15 19:18:19 +000088{
stephen hemminger545a8e72017-03-22 14:51:00 -070089 struct netvsc_device *nvdev
90 = container_of(head, struct netvsc_device, rcu);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070091 int i;
92
93 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
stephen hemminger7426b1a2017-07-28 08:59:45 -070094 vfree(nvdev->chan_table[i].mrc.slots);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070095
Haiyang Zhangf90251c2014-08-15 19:18:19 +000096 kfree(nvdev);
97}
98
stephen hemminger545a8e72017-03-22 14:51:00 -070099static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
100{
101 call_rcu(&nvdev->rcu, free_netvsc_device);
102}
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800103
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100104static void netvsc_revoke_buf(struct hv_device *device,
105 struct netvsc_device *net_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 Hemminger7a2a0a82016-08-23 12:17:54 -0700109 int ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700110
111 /*
112 * If we got a section count, it means we received a
113 * SendReceiveBufferComplete msg (ie sent
114 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
115 * to send a revoke msg here
116 */
117 if (net_device->recv_section_cnt) {
118 /* Send the revoke receive buffer */
119 revoke_packet = &net_device->revoke_packet;
120 memset(revoke_packet, 0, sizeof(struct nvsp_message));
121
122 revoke_packet->hdr.msg_type =
123 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
124 revoke_packet->msg.v1_msg.
125 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
126
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200127 ret = vmbus_sendpacket(device->channel,
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700128 revoke_packet,
129 sizeof(struct nvsp_message),
130 (unsigned long)revoke_packet,
131 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700132 /* If the failure is because the channel is rescinded;
133 * ignore the failure since we cannot send on a rescinded
134 * channel. This would allow us to properly cleanup
135 * even when the channel is rescinded.
136 */
137 if (device->channel->rescind)
138 ret = 0;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700139 /*
140 * If we failed here, we might as well return and
141 * have a leak rather than continue and a bugchk
142 */
143 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700144 netdev_err(ndev, "unable to send "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700145 "revoke receive buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700146 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700147 }
stephen hemminger8b532792017-08-09 17:46:11 -0700148 net_device->recv_section_cnt = 0;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700149 }
150
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700151 /* Deal with the send buffer we may have setup.
152 * If we got a send section size, it means we received a
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800153 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
154 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700155 * to send a revoke msg here
156 */
stephen hemminger8b532792017-08-09 17:46:11 -0700157 if (net_device->send_section_cnt) {
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700158 /* Send the revoke receive buffer */
159 revoke_packet = &net_device->revoke_packet;
160 memset(revoke_packet, 0, sizeof(struct nvsp_message));
161
162 revoke_packet->hdr.msg_type =
163 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800164 revoke_packet->msg.v1_msg.revoke_send_buf.id =
165 NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700166
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200167 ret = vmbus_sendpacket(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700168 revoke_packet,
169 sizeof(struct nvsp_message),
170 (unsigned long)revoke_packet,
171 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700172
173 /* If the failure is because the channel is rescinded;
174 * ignore the failure since we cannot send on a rescinded
175 * channel. This would allow us to properly cleanup
176 * even when the channel is rescinded.
177 */
178 if (device->channel->rescind)
179 ret = 0;
180
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700181 /* If we failed here, we might as well return and
182 * have a leak rather than continue and a bugchk
183 */
184 if (ret != 0) {
185 netdev_err(ndev, "unable to send "
186 "revoke send buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700187 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700188 }
stephen hemminger8b532792017-08-09 17:46:11 -0700189 net_device->send_section_cnt = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700190 }
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100191}
192
193static void netvsc_teardown_gpadl(struct hv_device *device,
194 struct netvsc_device *net_device)
195{
196 struct net_device *ndev = hv_get_drvdata(device);
197 int ret;
198
199 if (net_device->recv_buf_gpadl_handle) {
200 ret = vmbus_teardown_gpadl(device->channel,
201 net_device->recv_buf_gpadl_handle);
202
203 /* If we failed here, we might as well return and have a leak
204 * rather than continue and a bugchk
205 */
206 if (ret != 0) {
207 netdev_err(ndev,
208 "unable to teardown receive buffer's gpadl\n");
209 return;
210 }
211 net_device->recv_buf_gpadl_handle = 0;
212 }
213
214 if (net_device->recv_buf) {
215 /* Free up the receive buffer */
216 vfree(net_device->recv_buf);
217 net_device->recv_buf = NULL;
218 }
219
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700220 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 hemminger7426b1a2017-07-28 08:59:45 -0700242int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx)
243{
244 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
245 int node = cpu_to_node(nvchan->channel->target_cpu);
246 size_t size;
247
248 size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data);
249 nvchan->mrc.slots = vzalloc_node(size, node);
250 if (!nvchan->mrc.slots)
251 nvchan->mrc.slots = vzalloc(size);
252
253 return nvchan->mrc.slots ? 0 : -ENOMEM;
254}
255
stephen hemminger95790832017-06-08 16:21:22 -0700256static int netvsc_init_buf(struct hv_device *device,
stephen hemminger8b532792017-08-09 17:46:11 -0700257 struct netvsc_device *net_device,
258 const struct netvsc_device_info *device_info)
Hank Janssenfceaf242009-07-13 15:34:54 -0700259{
stephen hemminger7426b1a2017-07-28 08:59:45 -0700260 struct nvsp_1_message_send_receive_buffer_complete *resp;
stephen hemminger95833372017-08-09 17:46:07 -0700261 struct net_device *ndev = hv_get_drvdata(device);
262 struct nvsp_message *init_packet;
stephen hemminger8b532792017-08-09 17:46:11 -0700263 unsigned int buf_size;
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700264 size_t map_words;
stephen hemminger95833372017-08-09 17:46:07 -0700265 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700266
stephen hemminger8b532792017-08-09 17:46:11 -0700267 /* Get receive buffer area. */
Alex Ng0ab09be2017-09-20 11:17:35 -0700268 buf_size = device_info->recv_sections * device_info->recv_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700269 buf_size = roundup(buf_size, PAGE_SIZE);
270
Haiyang Zhang11b2b652017-12-11 08:56:57 -0800271 /* Legacy hosts only allow smaller receive buffer */
272 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
273 buf_size = min_t(unsigned int, buf_size,
274 NETVSC_RECEIVE_BUFFER_SIZE_LEGACY);
275
stephen hemminger8b532792017-08-09 17:46:11 -0700276 net_device->recv_buf = vzalloc(buf_size);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800277 if (!net_device->recv_buf) {
stephen hemminger8b532792017-08-09 17:46:11 -0700278 netdev_err(ndev,
279 "unable to allocate receive buffer of size %u\n",
280 buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700281 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800282 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700283 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700284
Bill Pemberton454f18a2009-07-27 16:47:24 -0400285 /*
286 * Establish the gpadl handle for this buffer on this
287 * channel. Note: This call uses the vmbus connection rather
288 * than the channel to establish the gpadl handle.
289 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800290 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
stephen hemminger8b532792017-08-09 17:46:11 -0700291 buf_size,
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800292 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700293 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700294 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700295 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800296 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700297 }
298
Bill Pemberton454f18a2009-07-27 16:47:24 -0400299 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800300 init_packet = &net_device->channel_init_pkt;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800301 memset(init_packet, 0, sizeof(struct nvsp_message));
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800302 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
303 init_packet->msg.v1_msg.send_recv_buf.
304 gpadl_handle = net_device->recv_buf_gpadl_handle;
305 init_packet->msg.v1_msg.
306 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700307
Bill Pemberton454f18a2009-07-27 16:47:24 -0400308 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800309 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700310 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800311 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800312 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700313 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700314 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700315 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700316 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800317 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700318 }
319
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200320 wait_for_completion(&net_device->channel_init_wait);
Hank Janssenfceaf242009-07-13 15:34:54 -0700321
Bill Pemberton454f18a2009-07-27 16:47:24 -0400322 /* Check the response */
stephen hemminger7426b1a2017-07-28 08:59:45 -0700323 resp = &init_packet->msg.v1_msg.send_recv_buf_complete;
324 if (resp->status != NVSP_STAT_SUCCESS) {
325 netdev_err(ndev,
326 "Unable to complete receive buffer initialization with NetVsp - status %d\n",
327 resp->status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700328 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800329 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700330 }
331
Bill Pemberton454f18a2009-07-27 16:47:24 -0400332 /* Parse the response */
stephen hemminger7426b1a2017-07-28 08:59:45 -0700333 netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n",
334 resp->num_sections, resp->sections[0].sub_alloc_size,
335 resp->sections[0].num_sub_allocs);
Hank Janssenfceaf242009-07-13 15:34:54 -0700336
stephen hemminger8b532792017-08-09 17:46:11 -0700337 /* There should only be one section for the entire receive buffer */
338 if (resp->num_sections != 1 || resp->sections[0].offset != 0) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700339 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800340 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700341 }
342
stephen hemminger8b532792017-08-09 17:46:11 -0700343 net_device->recv_section_size = resp->sections[0].sub_alloc_size;
344 net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
345
stephen hemminger7426b1a2017-07-28 08:59:45 -0700346 /* Setup receive completion ring */
347 net_device->recv_completion_cnt
stephen hemminger8b532792017-08-09 17:46:11 -0700348 = round_up(net_device->recv_section_cnt + 1,
stephen hemminger7426b1a2017-07-28 08:59:45 -0700349 PAGE_SIZE / sizeof(u64));
350 ret = netvsc_alloc_recv_comp_ring(net_device, 0);
351 if (ret)
352 goto cleanup;
353
354 /* Now setup the send buffer. */
Alex Ng0ab09be2017-09-20 11:17:35 -0700355 buf_size = device_info->send_sections * device_info->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700356 buf_size = round_up(buf_size, PAGE_SIZE);
357
358 net_device->send_buf = vzalloc(buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700359 if (!net_device->send_buf) {
stephen hemminger8b532792017-08-09 17:46:11 -0700360 netdev_err(ndev, "unable to allocate send buffer of size %u\n",
361 buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700362 ret = -ENOMEM;
363 goto cleanup;
364 }
365
366 /* Establish the gpadl handle for this buffer on this
367 * channel. Note: This call uses the vmbus connection rather
368 * than the channel to establish the gpadl handle.
369 */
370 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
stephen hemminger8b532792017-08-09 17:46:11 -0700371 buf_size,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700372 &net_device->send_buf_gpadl_handle);
373 if (ret != 0) {
374 netdev_err(ndev,
375 "unable to establish send buffer's gpadl\n");
376 goto cleanup;
377 }
378
379 /* Notify the NetVsp of the gpadl handle */
380 init_packet = &net_device->channel_init_pkt;
381 memset(init_packet, 0, sizeof(struct nvsp_message));
382 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800383 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700384 net_device->send_buf_gpadl_handle;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800385 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700386
387 /* Send the gpadl notification request */
388 ret = vmbus_sendpacket(device->channel, init_packet,
389 sizeof(struct nvsp_message),
390 (unsigned long)init_packet,
391 VM_PKT_DATA_INBAND,
392 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
393 if (ret != 0) {
394 netdev_err(ndev,
395 "unable to send send buffer's gpadl to netvsp\n");
396 goto cleanup;
397 }
398
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200399 wait_for_completion(&net_device->channel_init_wait);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700400
401 /* Check the response */
402 if (init_packet->msg.v1_msg.
403 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
404 netdev_err(ndev, "Unable to complete send buffer "
405 "initialization with NetVsp - status %d\n",
406 init_packet->msg.v1_msg.
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800407 send_send_buf_complete.status);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700408 ret = -EINVAL;
409 goto cleanup;
410 }
411
412 /* Parse the response */
413 net_device->send_section_size = init_packet->msg.
414 v1_msg.send_send_buf_complete.section_size;
415
stephen hemminger8b532792017-08-09 17:46:11 -0700416 /* Section count is simply the size divided by the section size. */
417 net_device->send_section_cnt = buf_size / net_device->send_section_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700418
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100419 netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
420 net_device->send_section_size, net_device->send_section_cnt);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700421
422 /* Setup state for managing the send buffer. */
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700423 map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700424
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700425 net_device->send_section_map = kcalloc(map_words, sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800426 if (net_device->send_section_map == NULL) {
427 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700428 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800429 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700430
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800431 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700432
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800433cleanup:
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100434 netvsc_revoke_buf(device, net_device);
435 netvsc_teardown_gpadl(device, net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700436
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800437exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700438 return ret;
439}
440
Haiyang Zhangf157e782011-12-15 13:45:16 -0800441/* Negotiate NVSP protocol version */
442static int negotiate_nvsp_ver(struct hv_device *device,
443 struct netvsc_device *net_device,
444 struct nvsp_message *init_packet,
445 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700446{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200447 struct net_device *ndev = hv_get_drvdata(device);
Nicholas Mc Guire7390fe92015-01-25 15:46:31 +0100448 int ret;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800449
450 memset(init_packet, 0, sizeof(struct nvsp_message));
451 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
452 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
453 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
454
455 /* Send the init request */
456 ret = vmbus_sendpacket(device->channel, init_packet,
457 sizeof(struct nvsp_message),
458 (unsigned long)init_packet,
459 VM_PKT_DATA_INBAND,
460 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
461
462 if (ret != 0)
463 return ret;
464
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200465 wait_for_completion(&net_device->channel_init_wait);
Haiyang Zhangf157e782011-12-15 13:45:16 -0800466
467 if (init_packet->msg.init_msg.init_complete.status !=
468 NVSP_STAT_SUCCESS)
469 return -EINVAL;
470
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800471 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800472 return 0;
473
Haiyang Zhang71790a22015-07-24 10:08:40 -0700474 /* NVSPv2 or later: Send NDIS config */
Haiyang Zhangf157e782011-12-15 13:45:16 -0800475 memset(init_packet, 0, sizeof(struct nvsp_message));
476 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200477 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000478 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800479
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700480 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
Haiyang Zhang71790a22015-07-24 10:08:40 -0700481 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
482
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700483 /* Teaming bit is needed to receive link speed updates */
484 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
485 }
486
Haiyang Zhangf157e782011-12-15 13:45:16 -0800487 ret = vmbus_sendpacket(device->channel, init_packet,
488 sizeof(struct nvsp_message),
489 (unsigned long)init_packet,
490 VM_PKT_DATA_INBAND, 0);
491
492 return ret;
493}
494
stephen hemminger95790832017-06-08 16:21:22 -0700495static int netvsc_connect_vsp(struct hv_device *device,
stephen hemminger8b532792017-08-09 17:46:11 -0700496 struct netvsc_device *net_device,
497 const struct netvsc_device_info *device_info)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800498{
Colin Ian King1b17ca02017-09-22 16:50:23 +0100499 static const u32 ver_list[] = {
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700500 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
stephen hemminger95790832017-06-08 16:21:22 -0700501 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5
502 };
503 struct nvsp_message *init_packet;
504 int ndis_version, i, ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700505
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800506 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700507
Haiyang Zhangf157e782011-12-15 13:45:16 -0800508 /* Negotiate the latest NVSP protocol supported */
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700509 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800510 if (negotiate_nvsp_ver(device, net_device, init_packet,
511 ver_list[i]) == 0) {
512 net_device->nvsp_version = ver_list[i];
513 break;
514 }
515
516 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700517 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800518 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700519 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800520
521 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
522
Bill Pemberton454f18a2009-07-27 16:47:24 -0400523 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800524 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700525
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800526 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700527 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800528 else
529 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700530
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800531 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
532 init_packet->msg.v1_msg.
533 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800534 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800535 init_packet->msg.v1_msg.
536 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800537 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700538
Bill Pemberton454f18a2009-07-27 16:47:24 -0400539 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800540 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800541 sizeof(struct nvsp_message),
542 (unsigned long)init_packet,
543 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700544 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800545 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700546
Haiyang Zhang99d30162014-03-09 16:10:59 -0700547
stephen hemminger8b532792017-08-09 17:46:11 -0700548 ret = netvsc_init_buf(device, net_device, device_info);
Hank Janssenfceaf242009-07-13 15:34:54 -0700549
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800550cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700551 return ret;
552}
553
Hank Janssen3e189512010-03-04 22:11:00 +0000554/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800555 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700556 */
Stephen Hemmingere08f3ea2016-08-23 12:17:50 -0700557void netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700558{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200559 struct net_device *ndev = hv_get_drvdata(device);
560 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -0700561 struct netvsc_device *net_device
562 = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger15a863b2017-02-27 10:26:49 -0800563 int i;
Hank Janssenfceaf242009-07-13 15:34:54 -0700564
Stephen Hemminger8195b132017-09-06 13:53:05 -0700565 cancel_work_sync(&net_device->subchan_work);
566
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100567 netvsc_revoke_buf(device, net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700568
stephen hemminger545a8e72017-03-22 14:51:00 -0700569 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700570
Stephen Hemminger8348e042018-03-20 15:03:02 -0700571 /* And disassociate NAPI context from device */
572 for (i = 0; i < net_device->num_chn; i++)
573 netif_napi_del(&net_device->chan_table[i].napi);
574
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700575 /*
576 * At this point, no one should be accessing net_device
577 * except in here
578 */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100579 netdev_dbg(ndev, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700580
Bill Pemberton454f18a2009-07-27 16:47:24 -0400581 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800582 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700583
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100584 netvsc_teardown_gpadl(device, net_device);
585
Bill Pemberton454f18a2009-07-27 16:47:24 -0400586 /* Release all resources */
stephen hemminger545a8e72017-03-22 14:51:00 -0700587 free_netvsc_device_rcu(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700588}
589
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000590#define RING_AVAIL_PERCENT_HIWATER 20
591#define RING_AVAIL_PERCENT_LOWATER 10
592
593/*
594 * Get the percentage of available bytes to write in the ring.
595 * The return value is in range from 0 to 100.
596 */
Stephen Hemmingera7f99d02017-12-01 11:01:47 -0800597static u32 hv_ringbuf_avail_percent(const struct hv_ring_buffer_info *ring_info)
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000598{
Stephen Hemmingera7f99d02017-12-01 11:01:47 -0800599 u32 avail_write = hv_get_bytes_to_write(ring_info);
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000600
Stephen Hemmingera7f99d02017-12-01 11:01:47 -0800601 return reciprocal_divide(avail_write * 100, netvsc_ring_reciprocal);
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000602}
603
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700604static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
605 u32 index)
606{
607 sync_change_bit(index, net_device->send_section_map);
608}
609
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700610static void netvsc_send_tx_complete(struct netvsc_device *net_device,
611 struct vmbus_channel *incoming_channel,
612 struct hv_device *device,
stephen hemmingerf9645432017-04-07 14:41:19 -0400613 const struct vmpacket_descriptor *desc,
614 int budget)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700615{
stephen hemminger50698d82017-02-27 10:26:47 -0800616 struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700617 struct net_device *ndev = hv_get_drvdata(device);
Simon Xiao09af87d2017-09-29 11:39:46 -0700618 struct net_device_context *ndev_ctx = netdev_priv(ndev);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700619 struct vmbus_channel *channel = device->channel;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700620 u16 q_idx = 0;
621 int queue_sends;
622
623 /* Notify the layer above us */
624 if (likely(skb)) {
stephen hemminger793e3952017-01-24 13:06:12 -0800625 const struct hv_netvsc_packet *packet
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700626 = (struct hv_netvsc_packet *)skb->cb;
stephen hemminger793e3952017-01-24 13:06:12 -0800627 u32 send_index = packet->send_buf_index;
628 struct netvsc_stats *tx_stats;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700629
630 if (send_index != NETVSC_INVALID_INDEX)
631 netvsc_free_send_slot(net_device, send_index);
stephen hemminger793e3952017-01-24 13:06:12 -0800632 q_idx = packet->q_idx;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700633 channel = incoming_channel;
634
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800635 tx_stats = &net_device->chan_table[q_idx].tx_stats;
stephen hemminger793e3952017-01-24 13:06:12 -0800636
637 u64_stats_update_begin(&tx_stats->syncp);
638 tx_stats->packets += packet->total_packets;
639 tx_stats->bytes += packet->total_bytes;
640 u64_stats_update_end(&tx_stats->syncp);
641
stephen hemmingerf9645432017-04-07 14:41:19 -0400642 napi_consume_skb(skb, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700643 }
644
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800645 queue_sends =
646 atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700647
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800648 if (net_device->destroy && queue_sends == 0)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700649 wake_up(&net_device->wait_drain);
650
651 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700652 (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER ||
Simon Xiao09af87d2017-09-29 11:39:46 -0700653 queue_sends < 1)) {
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700654 netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
Simon Xiao09af87d2017-09-29 11:39:46 -0700655 ndev_ctx->eth_stats.wake_queue++;
656 }
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700657}
658
KY Srinivasan97c17232014-02-16 16:38:44 -0800659static void netvsc_send_completion(struct netvsc_device *net_device,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800660 struct vmbus_channel *incoming_channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800661 struct hv_device *device,
stephen hemmingerf9645432017-04-07 14:41:19 -0400662 const struct vmpacket_descriptor *desc,
663 int budget)
Hank Janssenfceaf242009-07-13 15:34:54 -0700664{
stephen hemmingerf3dd3f42017-02-27 10:26:48 -0800665 struct nvsp_message *nvsp_packet = hv_pkt_data(desc);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200666 struct net_device *ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700667
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700668 switch (nvsp_packet->hdr.msg_type) {
669 case NVSP_MSG_TYPE_INIT_COMPLETE:
670 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
671 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
672 case NVSP_MSG5_TYPE_SUBCHANNEL:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400673 /* Copy the response back */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800674 memcpy(&net_device->channel_init_pkt, nvsp_packet,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700675 sizeof(struct nvsp_message));
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700676 complete(&net_device->channel_init_wait);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700677 break;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000678
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700679 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
680 netvsc_send_tx_complete(net_device, incoming_channel,
stephen hemmingerf9645432017-04-07 14:41:19 -0400681 device, desc, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700682 break;
Hank Janssenfceaf242009-07-13 15:34:54 -0700683
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700684 default:
685 netdev_err(ndev,
686 "Unknown send completion type %d received!!\n",
687 nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700688 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700689}
690
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700691static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
692{
stephen hemmingerb58a1852017-01-24 13:06:14 -0800693 unsigned long *map_addr = net_device->send_section_map;
694 unsigned int i;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700695
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700696 for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
stephen hemmingerb58a1852017-01-24 13:06:14 -0800697 if (sync_test_and_set_bit(i, map_addr) == 0)
698 return i;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700699 }
stephen hemmingerb58a1852017-01-24 13:06:14 -0800700
701 return NETVSC_INVALID_INDEX;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700702}
703
Stephen Hemminger26a11262017-12-12 16:48:35 -0800704static void netvsc_copy_to_send_buf(struct netvsc_device *net_device,
705 unsigned int section_index,
706 u32 pend_size,
707 struct hv_netvsc_packet *packet,
708 struct rndis_message *rndis_msg,
709 struct hv_page_buffer *pb,
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800710 bool xmit_more)
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700711{
712 char *start = net_device->send_buf;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700713 char *dest = start + (section_index * net_device->send_section_size)
714 + pend_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700715 int i;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700716 u32 padding = 0;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700717 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
718 packet->page_buf_cnt;
Stephen Hemmingerb85e06f2017-12-01 11:01:46 -0800719 u32 remain;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700720
721 /* Add padding */
Stephen Hemmingerb85e06f2017-12-01 11:01:46 -0800722 remain = packet->total_data_buflen & (net_device->pkt_align - 1);
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800723 if (xmit_more && remain) {
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700724 padding = net_device->pkt_align - remain;
KY Srinivasan24476762015-12-01 16:43:06 -0800725 rndis_msg->msg_len += padding;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700726 packet->total_data_buflen += padding;
727 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700728
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700729 for (i = 0; i < page_count; i++) {
stephen hemminger02b6de02017-07-28 08:59:44 -0700730 char *src = phys_to_virt(pb[i].pfn << PAGE_SHIFT);
731 u32 offset = pb[i].offset;
732 u32 len = pb[i].len;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700733
734 memcpy(dest, (src + offset), len);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700735 dest += len;
736 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700737
Stephen Hemminger26a11262017-12-12 16:48:35 -0800738 if (padding)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700739 memset(dest, 0, padding);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700740}
741
Stephen Hemminger3a8963a2016-09-09 12:45:24 -0700742static inline int netvsc_send_pkt(
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200743 struct hv_device *device,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700744 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800745 struct netvsc_device *net_device,
stephen hemminger02b6de02017-07-28 08:59:44 -0700746 struct hv_page_buffer *pb,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800747 struct sk_buff *skb)
Hank Janssenfceaf242009-07-13 15:34:54 -0700748{
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700749 struct nvsp_message nvmsg;
Joe Perches956a25c2017-07-31 10:30:54 -0700750 struct nvsp_1_message_send_rndis_packet * const rpkt =
751 &nvmsg.msg.v1_msg.send_rndis_pkt;
752 struct netvsc_channel * const nvchan =
753 &net_device->chan_table[packet->q_idx];
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800754 struct vmbus_channel *out_channel = nvchan->channel;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200755 struct net_device *ndev = hv_get_drvdata(device);
Simon Xiao09af87d2017-09-29 11:39:46 -0700756 struct net_device_context *ndev_ctx = netdev_priv(ndev);
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800757 struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700758 u64 req_id;
759 int ret;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700760 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700761
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700762 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
Joe Perches956a25c2017-07-31 10:30:54 -0700763 if (skb)
764 rpkt->channel_type = 0; /* 0 is RMC_DATA */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700765 else
Joe Perches956a25c2017-07-31 10:30:54 -0700766 rpkt->channel_type = 1; /* 1 is RMC_CONTROL */
767
768 rpkt->send_buf_section_index = packet->send_buf_index;
769 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
770 rpkt->send_buf_section_size = 0;
771 else
772 rpkt->send_buf_section_size = packet->total_data_buflen;
Hank Janssenfceaf242009-07-13 15:34:54 -0700773
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800774 req_id = (ulong)skb;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000775
Haiyang Zhangc3582a22014-12-01 13:28:39 -0800776 if (out_channel->rescind)
777 return -ENODEV;
778
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800779 if (packet->page_buf_cnt) {
stephen hemminger02b6de02017-07-28 08:59:44 -0700780 if (packet->cp_partial)
781 pb += packet->rmsg_pgcnt;
782
stephen hemminger5a668d82017-08-16 08:56:25 -0700783 ret = vmbus_sendpacket_pagebuffer(out_channel,
784 pb, packet->page_buf_cnt,
785 &nvmsg, sizeof(nvmsg),
786 req_id);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700787 } else {
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700788 ret = vmbus_sendpacket(out_channel,
789 &nvmsg, sizeof(nvmsg),
790 req_id, VM_PKT_DATA_INBAND,
791 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700792 }
793
Haiyang Zhang1d068252011-12-02 11:56:25 -0800794 if (ret == 0) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800795 atomic_inc_return(&nvchan->queue_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700796
Simon Xiao09af87d2017-09-29 11:39:46 -0700797 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800798 netif_tx_stop_queue(txq);
Simon Xiao09af87d2017-09-29 11:39:46 -0700799 ndev_ctx->eth_stats.stop_queue++;
800 }
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);
Simon Xiao09af87d2017-09-29 11:39:46 -0700803 ndev_ctx->eth_stats.stop_queue++;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800804 if (atomic_read(&nvchan->queue_sends) < 1) {
805 netif_tx_wake_queue(txq);
Simon Xiao09af87d2017-09-29 11:39:46 -0700806 ndev_ctx->eth_stats.wake_queue++;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000807 ret = -ENOSPC;
808 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800809 } else {
stephen hemminger4a2176c2017-07-28 08:59:43 -0700810 netdev_err(ndev,
811 "Unable to send packet pages %u len %u, ret %d\n",
812 packet->page_buf_cnt, packet->total_data_buflen,
813 ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800814 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700815
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700816 return ret;
817}
818
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800819/* Move packet out of multi send data (msd), and clear msd */
820static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
821 struct sk_buff **msd_skb,
822 struct multi_send_data *msdp)
823{
824 *msd_skb = msdp->skb;
825 *msd_send = msdp->pkt;
826 msdp->skb = NULL;
827 msdp->pkt = NULL;
828 msdp->count = 0;
829}
830
stephen hemminger2a926f72017-07-19 11:53:17 -0700831/* RCU already held by caller */
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800832int netvsc_send(struct net_device *ndev,
KY Srinivasan24476762015-12-01 16:43:06 -0800833 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800834 struct rndis_message *rndis_msg,
stephen hemminger02b6de02017-07-28 08:59:44 -0700835 struct hv_page_buffer *pb,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800836 struct sk_buff *skb)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700837{
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800838 struct net_device_context *ndev_ctx = netdev_priv(ndev);
stephen hemminger39629812017-07-19 11:53:19 -0700839 struct netvsc_device *net_device
stephen hemminger867047c2017-07-28 08:59:42 -0700840 = rcu_dereference_bh(ndev_ctx->nvdev);
stephen hemminger2a926f72017-07-19 11:53:17 -0700841 struct hv_device *device = ndev_ctx->device_ctx;
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700842 int ret = 0;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800843 struct netvsc_channel *nvchan;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700844 u32 pktlen = packet->total_data_buflen, msd_len = 0;
845 unsigned int section_index = NETVSC_INVALID_INDEX;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700846 struct multi_send_data *msdp;
847 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800848 struct sk_buff *msd_skb = NULL;
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800849 bool try_batch, xmit_more;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700850
stephen hemminger592b4fe2017-06-08 16:21:23 -0700851 /* If device is rescinded, return error and packet will get dropped. */
stephen hemminger2a926f72017-07-19 11:53:17 -0700852 if (unlikely(!net_device || net_device->destroy))
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700853 return -ENODEV;
854
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800855 nvchan = &net_device->chan_table[packet->q_idx];
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700856 packet->send_buf_index = NETVSC_INVALID_INDEX;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700857 packet->cp_partial = false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700858
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800859 /* Send control message directly without accessing msd (Multi-Send
860 * Data) field which may be changed during data packet processing.
861 */
Stephen Hemminger12f69662018-03-02 13:49:01 -0800862 if (!skb)
863 return netvsc_send_pkt(device, packet, net_device, pb, skb);
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800864
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700865 /* batch packets in send buffer if possible */
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800866 msdp = &nvchan->msd;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700867 if (msdp->pkt)
868 msd_len = msdp->pkt->total_data_buflen;
869
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700870 try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700871 if (try_batch && msd_len + pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700872 net_device->send_section_size) {
873 section_index = msdp->pkt->send_buf_index;
874
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700875 } else if (try_batch && msd_len + packet->rmsg_size <
876 net_device->send_section_size) {
877 section_index = msdp->pkt->send_buf_index;
878 packet->cp_partial = true;
879
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700880 } else if (pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700881 net_device->send_section_size) {
882 section_index = netvsc_get_next_send_section(net_device);
stephen hemmingercad5c192017-08-09 17:46:12 -0700883 if (unlikely(section_index == NETVSC_INVALID_INDEX)) {
884 ++ndev_ctx->eth_stats.tx_send_full;
885 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800886 move_pkt_msd(&msd_send, &msd_skb, msdp);
887 msd_len = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700888 }
889 }
890
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800891 /* Keep aggregating only if stack says more data is coming
892 * and not doing mixed modes send and not flow blocked
893 */
894 xmit_more = skb->xmit_more &&
895 !packet->cp_partial &&
896 !netif_xmit_stopped(netdev_get_tx_queue(ndev, packet->q_idx));
897
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700898 if (section_index != NETVSC_INVALID_INDEX) {
899 netvsc_copy_to_send_buf(net_device,
900 section_index, msd_len,
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800901 packet, rndis_msg, pb, xmit_more);
KY Srinivasanb08cc792015-03-29 21:08:42 -0700902
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700903 packet->send_buf_index = section_index;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700904
905 if (packet->cp_partial) {
906 packet->page_buf_cnt -= packet->rmsg_pgcnt;
907 packet->total_data_buflen = msd_len + packet->rmsg_size;
908 } else {
909 packet->page_buf_cnt = 0;
910 packet->total_data_buflen += msd_len;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700911 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700912
stephen hemminger793e3952017-01-24 13:06:12 -0800913 if (msdp->pkt) {
914 packet->total_packets += msdp->pkt->total_packets;
915 packet->total_bytes += msdp->pkt->total_bytes;
916 }
917
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800918 if (msdp->skb)
Stephen Hemminger17db4bc2016-09-22 16:56:29 -0700919 dev_consume_skb_any(msdp->skb);
Haiyang Zhangee90b812015-04-06 15:22:54 -0700920
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800921 if (xmit_more) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800922 msdp->skb = skb;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700923 msdp->pkt = packet;
924 msdp->count++;
925 } else {
926 cur_send = packet;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800927 msdp->skb = NULL;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700928 msdp->pkt = NULL;
929 msdp->count = 0;
930 }
931 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800932 move_pkt_msd(&msd_send, &msd_skb, msdp);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700933 cur_send = packet;
934 }
935
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700936 if (msd_send) {
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700937 int m_ret = netvsc_send_pkt(device, msd_send, net_device,
938 NULL, msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700939
940 if (m_ret != 0) {
941 netvsc_free_send_slot(net_device,
942 msd_send->send_buf_index);
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800943 dev_kfree_skb_any(msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700944 }
945 }
946
947 if (cur_send)
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200948 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700949
Jerry Snitselaar7aab5152015-05-04 10:57:16 -0700950 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
951 netvsc_free_send_slot(net_device, section_index);
Haiyang Zhangd953ca42015-01-29 12:34:49 -0800952
Hank Janssenfceaf242009-07-13 15:34:54 -0700953 return ret;
954}
955
stephen hemminger7426b1a2017-07-28 08:59:45 -0700956/* Send pending recv completions */
stephen hemmingercad5c192017-08-09 17:46:12 -0700957static int send_recv_completions(struct net_device *ndev,
958 struct netvsc_device *nvdev,
959 struct netvsc_channel *nvchan)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700960{
stephen hemminger7426b1a2017-07-28 08:59:45 -0700961 struct multi_recv_comp *mrc = &nvchan->mrc;
962 struct recv_comp_msg {
963 struct nvsp_message_header hdr;
964 u32 status;
965 } __packed;
966 struct recv_comp_msg msg = {
967 .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
968 };
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700969 int ret;
970
stephen hemminger7426b1a2017-07-28 08:59:45 -0700971 while (mrc->first != mrc->next) {
972 const struct recv_comp_data *rcd
973 = mrc->slots + mrc->first;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700974
stephen hemminger7426b1a2017-07-28 08:59:45 -0700975 msg.status = rcd->status;
976 ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg),
977 rcd->tid, VM_PKT_COMP, 0);
stephen hemmingercad5c192017-08-09 17:46:12 -0700978 if (unlikely(ret)) {
979 struct net_device_context *ndev_ctx = netdev_priv(ndev);
980
981 ++ndev_ctx->eth_stats.rx_comp_busy;
stephen hemminger7426b1a2017-07-28 08:59:45 -0700982 return ret;
stephen hemmingercad5c192017-08-09 17:46:12 -0700983 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700984
stephen hemminger7426b1a2017-07-28 08:59:45 -0700985 if (++mrc->first == nvdev->recv_completion_cnt)
986 mrc->first = 0;
987 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700988
stephen hemminger7426b1a2017-07-28 08:59:45 -0700989 /* receive completion ring has been emptied */
990 if (unlikely(nvdev->destroy))
991 wake_up(&nvdev->wait_drain);
992
993 return 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700994}
995
stephen hemminger7426b1a2017-07-28 08:59:45 -0700996/* Count how many receive completions are outstanding */
997static void recv_comp_slot_avail(const struct netvsc_device *nvdev,
998 const struct multi_recv_comp *mrc,
999 u32 *filled, u32 *avail)
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001000{
stephen hemminger7426b1a2017-07-28 08:59:45 -07001001 u32 count = nvdev->recv_completion_cnt;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001002
stephen hemminger7426b1a2017-07-28 08:59:45 -07001003 if (mrc->next >= mrc->first)
1004 *filled = mrc->next - mrc->first;
1005 else
1006 *filled = (count - mrc->first) + mrc->next;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001007
stephen hemminger7426b1a2017-07-28 08:59:45 -07001008 *avail = count - *filled - 1;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001009}
1010
stephen hemminger7426b1a2017-07-28 08:59:45 -07001011/* Add receive complete to ring to send to host. */
1012static void enq_receive_complete(struct net_device *ndev,
1013 struct netvsc_device *nvdev, u16 q_idx,
1014 u64 tid, u32 status)
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001015{
stephen hemminger7426b1a2017-07-28 08:59:45 -07001016 struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx];
1017 struct multi_recv_comp *mrc = &nvchan->mrc;
1018 struct recv_comp_data *rcd;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001019 u32 filled, avail;
1020
stephen hemminger7426b1a2017-07-28 08:59:45 -07001021 recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001022
stephen hemminger7426b1a2017-07-28 08:59:45 -07001023 if (unlikely(filled > NAPI_POLL_WEIGHT)) {
stephen hemmingercad5c192017-08-09 17:46:12 -07001024 send_recv_completions(ndev, nvdev, nvchan);
stephen hemminger7426b1a2017-07-28 08:59:45 -07001025 recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001026 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001027
stephen hemminger7426b1a2017-07-28 08:59:45 -07001028 if (unlikely(!avail)) {
1029 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1030 q_idx, tid);
1031 return;
1032 }
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001033
stephen hemminger7426b1a2017-07-28 08:59:45 -07001034 rcd = mrc->slots + mrc->next;
1035 rcd->tid = tid;
1036 rcd->status = status;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001037
stephen hemminger7426b1a2017-07-28 08:59:45 -07001038 if (++mrc->next == nvdev->recv_completion_cnt)
1039 mrc->next = 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001040}
1041
stephen hemminger15a863b2017-02-27 10:26:49 -08001042static int netvsc_receive(struct net_device *ndev,
stephen hemminger7426b1a2017-07-28 08:59:45 -07001043 struct netvsc_device *net_device,
1044 struct net_device_context *net_device_ctx,
1045 struct hv_device *device,
1046 struct vmbus_channel *channel,
1047 const struct vmpacket_descriptor *desc,
1048 struct nvsp_message *nvsp)
Hank Janssenfceaf242009-07-13 15:34:54 -07001049{
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001050 const struct vmtransfer_page_packet_header *vmxferpage_packet
1051 = container_of(desc, const struct vmtransfer_page_packet_header, d);
stephen hemminger15a863b2017-02-27 10:26:49 -08001052 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemmingerdc54a082017-01-24 13:06:08 -08001053 char *recv_buf = net_device->recv_buf;
Haiyang Zhang4baab262014-04-21 14:54:43 -07001054 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -08001055 int i;
1056 int count = 0;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -07001057
Bill Pemberton454f18a2009-07-27 16:47:24 -04001058 /* Make sure this is a valid nvsp packet */
stephen hemmingerdc54a082017-01-24 13:06:08 -08001059 if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
1060 netif_err(net_device_ctx, rx_err, ndev,
1061 "Unknown nvsp packet type received %u\n",
1062 nvsp->hdr.msg_type);
stephen hemminger15a863b2017-02-27 10:26:49 -08001063 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001064 }
1065
stephen hemmingerdc54a082017-01-24 13:06:08 -08001066 if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
1067 netif_err(net_device_ctx, rx_err, ndev,
1068 "Invalid xfer page set id - expecting %x got %x\n",
1069 NETVSC_RECEIVE_BUFFER_ID,
1070 vmxferpage_packet->xfer_pageset_id);
stephen hemminger15a863b2017-02-27 10:26:49 -08001071 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001072 }
1073
Haiyang Zhang4baab262014-04-21 14:54:43 -07001074 count = vmxferpage_packet->range_cnt;
Hank Janssenfceaf242009-07-13 15:34:54 -07001075
Bill Pemberton454f18a2009-07-27 16:47:24 -04001076 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -07001077 for (i = 0; i < count; i++) {
stephen hemmingerdc54a082017-01-24 13:06:08 -08001078 void *data = recv_buf
1079 + vmxferpage_packet->ranges[i].byte_offset;
1080 u32 buflen = vmxferpage_packet->ranges[i].byte_count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001081
Bill Pemberton454f18a2009-07-27 16:47:24 -04001082 /* Pass it to the upper layer */
Stephen Hemminger79cf1ba2017-12-12 16:48:37 -08001083 status = rndis_filter_receive(ndev, net_device,
stephen hemmingerdc54a082017-01-24 13:06:08 -08001084 channel, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -07001085 }
1086
stephen hemminger7426b1a2017-07-28 08:59:45 -07001087 enq_receive_complete(ndev, net_device, q_idx,
1088 vmxferpage_packet->d.trans_id, status);
stephen hemminger15a863b2017-02-27 10:26:49 -08001089
stephen hemminger15a863b2017-02-27 10:26:49 -08001090 return count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001091}
1092
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001093static void netvsc_send_table(struct hv_device *hdev,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001094 struct nvsp_message *nvmsg)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001095{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001096 struct net_device *ndev = hv_get_drvdata(hdev);
stephen hemminger7ce10122017-03-09 14:58:29 -08001097 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001098 int i;
1099 u32 count, *tab;
1100
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001101 count = nvmsg->msg.v5_msg.send_table.count;
1102 if (count != VRSS_SEND_TAB_SIZE) {
1103 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1104 return;
1105 }
1106
1107 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1108 nvmsg->msg.v5_msg.send_table.offset);
1109
1110 for (i = 0; i < count; i++)
Haiyang Zhang39e91cf2017-10-13 12:28:04 -07001111 net_device_ctx->tx_table[i] = tab[i];
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001112}
1113
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001114static void netvsc_send_vf(struct net_device_context *net_device_ctx,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001115 struct nvsp_message *nvmsg)
1116{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001117 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1118 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
Haiyang Zhang71790a22015-07-24 10:08:40 -07001119}
1120
1121static inline void netvsc_receive_inband(struct hv_device *hdev,
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001122 struct net_device_context *net_device_ctx,
1123 struct nvsp_message *nvmsg)
Haiyang Zhang71790a22015-07-24 10:08:40 -07001124{
1125 switch (nvmsg->hdr.msg_type) {
1126 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1127 netvsc_send_table(hdev, nvmsg);
1128 break;
1129
1130 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001131 netvsc_send_vf(net_device_ctx, nvmsg);
Haiyang Zhang71790a22015-07-24 10:08:40 -07001132 break;
1133 }
1134}
1135
stephen hemminger15a863b2017-02-27 10:26:49 -08001136static int netvsc_process_raw_pkt(struct hv_device *device,
1137 struct vmbus_channel *channel,
1138 struct netvsc_device *net_device,
1139 struct net_device *ndev,
stephen hemmingerf9645432017-04-07 14:41:19 -04001140 const struct vmpacket_descriptor *desc,
1141 int budget)
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001142{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001143 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001144 struct nvsp_message *nvmsg = hv_pkt_data(desc);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001145
1146 switch (desc->type) {
1147 case VM_PKT_COMP:
stephen hemmingerf9645432017-04-07 14:41:19 -04001148 netvsc_send_completion(net_device, channel, device,
1149 desc, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001150 break;
1151
1152 case VM_PKT_DATA_USING_XFER_PAGES:
stephen hemminger15a863b2017-02-27 10:26:49 -08001153 return netvsc_receive(ndev, net_device, net_device_ctx,
1154 device, channel, desc, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001155 break;
1156
1157 case VM_PKT_DATA_INBAND:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001158 netvsc_receive_inband(device, net_device_ctx, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001159 break;
1160
1161 default:
1162 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001163 desc->type, desc->trans_id);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001164 break;
1165 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001166
1167 return 0;
1168}
1169
1170static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
1171{
1172 struct vmbus_channel *primary = channel->primary_channel;
1173
1174 return primary ? primary->device_obj : channel->device_obj;
1175}
1176
stephen hemminger262b7f12017-03-16 16:12:38 -07001177/* Network processing softirq
1178 * Process data in incoming ring buffer from host
1179 * Stops when ring is empty or budget is met or exceeded.
1180 */
stephen hemminger15a863b2017-02-27 10:26:49 -08001181int netvsc_poll(struct napi_struct *napi, int budget)
1182{
1183 struct netvsc_channel *nvchan
1184 = container_of(napi, struct netvsc_channel, napi);
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001185 struct netvsc_device *net_device = nvchan->net_device;
stephen hemminger15a863b2017-02-27 10:26:49 -08001186 struct vmbus_channel *channel = nvchan->channel;
1187 struct hv_device *device = netvsc_channel_to_device(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001188 struct net_device *ndev = hv_get_drvdata(device);
stephen hemminger15a863b2017-02-27 10:26:49 -08001189 int work_done = 0;
1190
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001191 /* If starting a new interval */
1192 if (!nvchan->desc)
1193 nvchan->desc = hv_pkt_iter_first(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001194
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001195 while (nvchan->desc && work_done < budget) {
1196 work_done += netvsc_process_raw_pkt(device, channel, net_device,
stephen hemmingerf9645432017-04-07 14:41:19 -04001197 ndev, nvchan->desc, budget);
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001198 nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
stephen hemminger15a863b2017-02-27 10:26:49 -08001199 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001200
stephen hemmingerf4e40362017-07-28 08:59:47 -07001201 /* If send of pending receive completions suceeded
1202 * and did not exhaust NAPI budget this time
stephen hemminger7426b1a2017-07-28 08:59:45 -07001203 * and not doing busy poll
stephen hemmingerf4e40362017-07-28 08:59:47 -07001204 * then re-enable host interrupts
1205 * and reschedule if ring is not empty.
stephen hemminger262b7f12017-03-16 16:12:38 -07001206 */
stephen hemmingercad5c192017-08-09 17:46:12 -07001207 if (send_recv_completions(ndev, net_device, nvchan) == 0 &&
stephen hemminger7426b1a2017-07-28 08:59:45 -07001208 work_done < budget &&
stephen hemminger15a863b2017-02-27 10:26:49 -08001209 napi_complete_done(napi, work_done) &&
Stephen Hemmingerd64e38a2018-03-02 13:49:05 -08001210 hv_end_read(&channel->inbound) &&
1211 napi_schedule_prep(napi)) {
stephen hemminger7426b1a2017-07-28 08:59:45 -07001212 hv_begin_read(&channel->inbound);
Stephen Hemmingerd64e38a2018-03-02 13:49:05 -08001213 __napi_schedule(napi);
stephen hemminger7426b1a2017-07-28 08:59:45 -07001214 }
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001215
1216 /* Driver may overshoot since multiple packets per descriptor */
1217 return min(work_done, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001218}
1219
stephen hemminger262b7f12017-03-16 16:12:38 -07001220/* Call back when data is available in host ring buffer.
1221 * Processing is deferred until network softirq (NAPI)
1222 */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001223void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001224{
stephen hemminger6de38af2017-03-16 16:12:37 -07001225 struct netvsc_channel *nvchan = context;
stephen hemminger43bf99c2017-07-24 10:57:27 -07001226 struct vmbus_channel *channel = nvchan->channel;
1227 struct hv_ring_buffer_info *rbi = &channel->inbound;
1228
1229 /* preload first vmpacket descriptor */
1230 prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
stephen hemminger0b307eb2017-01-24 13:05:58 -08001231
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001232 if (napi_schedule_prep(&nvchan->napi)) {
1233 /* disable interupts from host */
stephen hemminger43bf99c2017-07-24 10:57:27 -07001234 hv_begin_read(rbi);
stephen hemminger0d6dd352017-03-09 15:04:14 -08001235
Stephen Hemminger68633ed2018-03-02 13:49:06 -08001236 __napi_schedule_irqoff(&nvchan->napi);
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001237 }
Hank Janssenfceaf242009-07-13 15:34:54 -07001238}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001239
1240/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001241 * netvsc_device_add - Callback when the device belonging to this
1242 * driver is added
1243 */
stephen hemminger9749fed2017-07-19 11:53:16 -07001244struct netvsc_device *netvsc_device_add(struct hv_device *device,
1245 const struct netvsc_device_info *device_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001246{
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001247 int i, ret = 0;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001248 struct netvsc_device *net_device;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001249 struct net_device *ndev = hv_get_drvdata(device);
1250 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001251
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001252 net_device = alloc_net_device();
Dan Carpenterb1c84922014-09-04 14:11:23 +03001253 if (!net_device)
stephen hemminger9749fed2017-07-19 11:53:16 -07001254 return ERR_PTR(-ENOMEM);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001255
Haiyang Zhang6b0cbe32017-10-13 12:28:05 -07001256 for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
1257 net_device_ctx->tx_table[i] = 0;
1258
stephen hemminger15a863b2017-02-27 10:26:49 -08001259 /* Because the device uses NAPI, all the interrupt batching and
1260 * control is done via Net softirq, not the channel handling
1261 */
1262 set_channel_read_mode(device->channel, HV_CALL_ISR);
1263
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001264 /* If we're reopening the device we may have multiple queues, fill the
1265 * chn_table with the default channel to use it before subchannels are
1266 * opened.
1267 * Initialize the channel state before we open;
1268 * we can be interrupted as soon as we open the channel.
1269 */
1270
1271 for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
1272 struct netvsc_channel *nvchan = &net_device->chan_table[i];
1273
1274 nvchan->channel = device->channel;
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001275 nvchan->net_device = net_device;
Florian Fainelli4a0dee12017-08-01 12:11:12 -07001276 u64_stats_init(&nvchan->tx_stats.syncp);
1277 u64_stats_init(&nvchan->rx_stats.syncp);
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001278 }
1279
stephen hemminger2be0f262017-05-03 16:59:21 -07001280 /* Enable NAPI handler before init callbacks */
1281 netif_napi_add(ndev, &net_device->chan_table[0].napi,
1282 netvsc_poll, NAPI_POLL_WEIGHT);
1283
Haiyang Zhangb637e022011-04-21 12:30:45 -07001284 /* Open the channel */
Stephen Hemmingera7f99d02017-12-01 11:01:47 -08001285 ret = vmbus_open(device->channel, netvsc_ring_bytes,
1286 netvsc_ring_bytes, NULL, 0,
1287 netvsc_channel_cb, net_device->chan_table);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001288
1289 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001290 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001291 goto cleanup;
1292 }
1293
1294 /* Channel is opened */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +01001295 netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001296
stephen hemminger15a863b2017-02-27 10:26:49 -08001297 napi_enable(&net_device->chan_table[0].napi);
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001298
Haiyang Zhangb637e022011-04-21 12:30:45 -07001299 /* Connect with the NetVsp */
stephen hemminger8b532792017-08-09 17:46:11 -07001300 ret = netvsc_connect_vsp(device, net_device, device_info);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001301 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001302 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001303 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001304 goto close;
1305 }
1306
Stephen Hemminger12f69662018-03-02 13:49:01 -08001307 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1308 * populated.
1309 */
1310 rcu_assign_pointer(net_device_ctx->nvdev, net_device);
1311
stephen hemminger9749fed2017-07-19 11:53:16 -07001312 return net_device;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001313
1314close:
stephen hemminger49393342017-07-28 08:59:46 -07001315 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
1316 napi_disable(&net_device->chan_table[0].napi);
stephen hemminger15a863b2017-02-27 10:26:49 -08001317
Haiyang Zhangb637e022011-04-21 12:30:45 -07001318 /* Now, we can close the channel safely */
1319 vmbus_close(device->channel);
1320
1321cleanup:
Stephen Hemmingerfcfb4a02018-03-02 13:49:03 -08001322 netif_napi_del(&net_device->chan_table[0].napi);
stephen hemminger545a8e72017-03-22 14:51:00 -07001323 free_netvsc_device(&net_device->rcu);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001324
stephen hemminger9749fed2017-07-19 11:53:16 -07001325 return ERR_PTR(ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001326}