blob: d34cf37e949d20d36249ae0b3b01b8e0c329b4f0 [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
Stephen Hemmingerfd612602016-08-23 12:17:51 -070079 init_completion(&net_device->channel_init_wait);
stephen hemminger732e4982017-08-03 17:13:54 -070080 init_waitqueue_head(&net_device->subchan_open);
Stephen Hemminger8195b132017-09-06 13:53:05 -070081 INIT_WORK(&net_device->subchan_work, rndis_set_subchannel);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -070082
Haiyang Zhang85799a32010-12-10 12:03:54 -080083 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070084}
85
stephen hemminger545a8e72017-03-22 14:51:00 -070086static void free_netvsc_device(struct rcu_head *head)
Haiyang Zhangf90251c2014-08-15 19:18:19 +000087{
stephen hemminger545a8e72017-03-22 14:51:00 -070088 struct netvsc_device *nvdev
89 = container_of(head, struct netvsc_device, rcu);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070090 int i;
91
92 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
stephen hemminger7426b1a2017-07-28 08:59:45 -070093 vfree(nvdev->chan_table[i].mrc.slots);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -070094
Haiyang Zhangf90251c2014-08-15 19:18:19 +000095 kfree(nvdev);
96}
97
stephen hemminger545a8e72017-03-22 14:51:00 -070098static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
99{
100 call_rcu(&nvdev->rcu, free_netvsc_device);
101}
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800102
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700103static void netvsc_destroy_buf(struct hv_device *device)
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700104{
105 struct nvsp_message *revoke_packet;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200106 struct net_device *ndev = hv_get_drvdata(device);
stephen hemminger39629812017-07-19 11:53:19 -0700107 struct net_device_context *ndc = netdev_priv(ndev);
108 struct netvsc_device *net_device = rtnl_dereference(ndc->nvdev);
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
151 /* Teardown the gpadl on the vsp end */
152 if (net_device->recv_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200153 ret = vmbus_teardown_gpadl(device->channel,
154 net_device->recv_buf_gpadl_handle);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700155
156 /* If we failed here, we might as well return and have a leak
157 * rather than continue and a bugchk
158 */
159 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700160 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700161 "unable to teardown receive buffer's gpadl\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700162 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700163 }
164 net_device->recv_buf_gpadl_handle = 0;
165 }
166
167 if (net_device->recv_buf) {
168 /* Free up the receive buffer */
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800169 vfree(net_device->recv_buf);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700170 net_device->recv_buf = NULL;
171 }
172
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700173 /* Deal with the send buffer we may have setup.
174 * If we got a send section size, it means we received a
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800175 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
176 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700177 * to send a revoke msg here
178 */
stephen hemminger8b532792017-08-09 17:46:11 -0700179 if (net_device->send_section_cnt) {
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700180 /* Send the revoke receive buffer */
181 revoke_packet = &net_device->revoke_packet;
182 memset(revoke_packet, 0, sizeof(struct nvsp_message));
183
184 revoke_packet->hdr.msg_type =
185 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800186 revoke_packet->msg.v1_msg.revoke_send_buf.id =
187 NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700188
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200189 ret = vmbus_sendpacket(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700190 revoke_packet,
191 sizeof(struct nvsp_message),
192 (unsigned long)revoke_packet,
193 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700194
195 /* If the failure is because the channel is rescinded;
196 * ignore the failure since we cannot send on a rescinded
197 * channel. This would allow us to properly cleanup
198 * even when the channel is rescinded.
199 */
200 if (device->channel->rescind)
201 ret = 0;
202
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700203 /* If we failed here, we might as well return and
204 * have a leak rather than continue and a bugchk
205 */
206 if (ret != 0) {
207 netdev_err(ndev, "unable to send "
208 "revoke send buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700209 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700210 }
stephen hemminger8b532792017-08-09 17:46:11 -0700211 net_device->send_section_cnt = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700212 }
213 /* Teardown the gpadl on the vsp end */
214 if (net_device->send_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200215 ret = vmbus_teardown_gpadl(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700216 net_device->send_buf_gpadl_handle);
217
218 /* If we failed here, we might as well return and have a leak
219 * rather than continue and a bugchk
220 */
221 if (ret != 0) {
222 netdev_err(ndev,
223 "unable to teardown send buffer's gpadl\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700224 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700225 }
Dave Jones2f184232014-06-16 16:59:02 -0400226 net_device->send_buf_gpadl_handle = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700227 }
228 if (net_device->send_buf) {
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800229 /* Free up the send buffer */
KY Srinivasan06b47aa2014-08-02 10:42:02 -0700230 vfree(net_device->send_buf);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700231 net_device->send_buf = NULL;
232 }
233 kfree(net_device->send_section_map);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700234}
235
stephen hemminger7426b1a2017-07-28 08:59:45 -0700236int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx)
237{
238 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
239 int node = cpu_to_node(nvchan->channel->target_cpu);
240 size_t size;
241
242 size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data);
243 nvchan->mrc.slots = vzalloc_node(size, node);
244 if (!nvchan->mrc.slots)
245 nvchan->mrc.slots = vzalloc(size);
246
247 return nvchan->mrc.slots ? 0 : -ENOMEM;
248}
249
stephen hemminger95790832017-06-08 16:21:22 -0700250static int netvsc_init_buf(struct hv_device *device,
stephen hemminger8b532792017-08-09 17:46:11 -0700251 struct netvsc_device *net_device,
252 const struct netvsc_device_info *device_info)
Hank Janssenfceaf242009-07-13 15:34:54 -0700253{
stephen hemminger7426b1a2017-07-28 08:59:45 -0700254 struct nvsp_1_message_send_receive_buffer_complete *resp;
stephen hemminger95833372017-08-09 17:46:07 -0700255 struct net_device *ndev = hv_get_drvdata(device);
256 struct nvsp_message *init_packet;
stephen hemminger8b532792017-08-09 17:46:11 -0700257 unsigned int buf_size;
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700258 size_t map_words;
stephen hemminger95833372017-08-09 17:46:07 -0700259 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700260
stephen hemminger8b532792017-08-09 17:46:11 -0700261 /* Get receive buffer area. */
Alex Ng0ab09be2017-09-20 11:17:35 -0700262 buf_size = device_info->recv_sections * device_info->recv_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700263 buf_size = roundup(buf_size, PAGE_SIZE);
264
265 net_device->recv_buf = vzalloc(buf_size);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800266 if (!net_device->recv_buf) {
stephen hemminger8b532792017-08-09 17:46:11 -0700267 netdev_err(ndev,
268 "unable to allocate receive buffer of size %u\n",
269 buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700270 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800271 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700272 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700273
Bill Pemberton454f18a2009-07-27 16:47:24 -0400274 /*
275 * Establish the gpadl handle for this buffer on this
276 * channel. Note: This call uses the vmbus connection rather
277 * than the channel to establish the gpadl handle.
278 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800279 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
stephen hemminger8b532792017-08-09 17:46:11 -0700280 buf_size,
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800281 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700282 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700283 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700284 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800285 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700286 }
287
Bill Pemberton454f18a2009-07-27 16:47:24 -0400288 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800289 init_packet = &net_device->channel_init_pkt;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800290 memset(init_packet, 0, sizeof(struct nvsp_message));
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800291 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
292 init_packet->msg.v1_msg.send_recv_buf.
293 gpadl_handle = net_device->recv_buf_gpadl_handle;
294 init_packet->msg.v1_msg.
295 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700296
Bill Pemberton454f18a2009-07-27 16:47:24 -0400297 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800298 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700299 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800300 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800301 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700302 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700303 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700304 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700305 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800306 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700307 }
308
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200309 wait_for_completion(&net_device->channel_init_wait);
Hank Janssenfceaf242009-07-13 15:34:54 -0700310
Bill Pemberton454f18a2009-07-27 16:47:24 -0400311 /* Check the response */
stephen hemminger7426b1a2017-07-28 08:59:45 -0700312 resp = &init_packet->msg.v1_msg.send_recv_buf_complete;
313 if (resp->status != NVSP_STAT_SUCCESS) {
314 netdev_err(ndev,
315 "Unable to complete receive buffer initialization with NetVsp - status %d\n",
316 resp->status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700317 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800318 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700319 }
320
Bill Pemberton454f18a2009-07-27 16:47:24 -0400321 /* Parse the response */
stephen hemminger7426b1a2017-07-28 08:59:45 -0700322 netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n",
323 resp->num_sections, resp->sections[0].sub_alloc_size,
324 resp->sections[0].num_sub_allocs);
Hank Janssenfceaf242009-07-13 15:34:54 -0700325
stephen hemminger8b532792017-08-09 17:46:11 -0700326 /* There should only be one section for the entire receive buffer */
327 if (resp->num_sections != 1 || resp->sections[0].offset != 0) {
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
stephen hemminger8b532792017-08-09 17:46:11 -0700332 net_device->recv_section_size = resp->sections[0].sub_alloc_size;
333 net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
334
stephen hemminger7426b1a2017-07-28 08:59:45 -0700335 /* Setup receive completion ring */
336 net_device->recv_completion_cnt
stephen hemminger8b532792017-08-09 17:46:11 -0700337 = round_up(net_device->recv_section_cnt + 1,
stephen hemminger7426b1a2017-07-28 08:59:45 -0700338 PAGE_SIZE / sizeof(u64));
339 ret = netvsc_alloc_recv_comp_ring(net_device, 0);
340 if (ret)
341 goto cleanup;
342
343 /* Now setup the send buffer. */
Alex Ng0ab09be2017-09-20 11:17:35 -0700344 buf_size = device_info->send_sections * device_info->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700345 buf_size = round_up(buf_size, PAGE_SIZE);
346
347 net_device->send_buf = vzalloc(buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700348 if (!net_device->send_buf) {
stephen hemminger8b532792017-08-09 17:46:11 -0700349 netdev_err(ndev, "unable to allocate send buffer of size %u\n",
350 buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700351 ret = -ENOMEM;
352 goto cleanup;
353 }
354
355 /* Establish the gpadl handle for this buffer on this
356 * channel. Note: This call uses the vmbus connection rather
357 * than the channel to establish the gpadl handle.
358 */
359 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
stephen hemminger8b532792017-08-09 17:46:11 -0700360 buf_size,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700361 &net_device->send_buf_gpadl_handle);
362 if (ret != 0) {
363 netdev_err(ndev,
364 "unable to establish send buffer's gpadl\n");
365 goto cleanup;
366 }
367
368 /* Notify the NetVsp of the gpadl handle */
369 init_packet = &net_device->channel_init_pkt;
370 memset(init_packet, 0, sizeof(struct nvsp_message));
371 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800372 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700373 net_device->send_buf_gpadl_handle;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800374 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700375
376 /* Send the gpadl notification request */
377 ret = vmbus_sendpacket(device->channel, init_packet,
378 sizeof(struct nvsp_message),
379 (unsigned long)init_packet,
380 VM_PKT_DATA_INBAND,
381 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
382 if (ret != 0) {
383 netdev_err(ndev,
384 "unable to send send buffer's gpadl to netvsp\n");
385 goto cleanup;
386 }
387
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200388 wait_for_completion(&net_device->channel_init_wait);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700389
390 /* Check the response */
391 if (init_packet->msg.v1_msg.
392 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
393 netdev_err(ndev, "Unable to complete send buffer "
394 "initialization with NetVsp - status %d\n",
395 init_packet->msg.v1_msg.
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800396 send_send_buf_complete.status);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700397 ret = -EINVAL;
398 goto cleanup;
399 }
400
401 /* Parse the response */
402 net_device->send_section_size = init_packet->msg.
403 v1_msg.send_send_buf_complete.section_size;
404
stephen hemminger8b532792017-08-09 17:46:11 -0700405 /* Section count is simply the size divided by the section size. */
406 net_device->send_section_cnt = buf_size / net_device->send_section_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700407
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100408 netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
409 net_device->send_section_size, net_device->send_section_cnt);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700410
411 /* Setup state for managing the send buffer. */
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700412 map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700413
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700414 net_device->send_section_map = kcalloc(map_words, sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800415 if (net_device->send_section_map == NULL) {
416 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700417 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800418 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700419
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800420 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700421
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800422cleanup:
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200423 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700424
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800425exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700426 return ret;
427}
428
Haiyang Zhangf157e782011-12-15 13:45:16 -0800429/* Negotiate NVSP protocol version */
430static int negotiate_nvsp_ver(struct hv_device *device,
431 struct netvsc_device *net_device,
432 struct nvsp_message *init_packet,
433 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700434{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200435 struct net_device *ndev = hv_get_drvdata(device);
Nicholas Mc Guire7390fe92015-01-25 15:46:31 +0100436 int ret;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800437
438 memset(init_packet, 0, sizeof(struct nvsp_message));
439 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
440 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
441 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
442
443 /* Send the init request */
444 ret = vmbus_sendpacket(device->channel, init_packet,
445 sizeof(struct nvsp_message),
446 (unsigned long)init_packet,
447 VM_PKT_DATA_INBAND,
448 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
449
450 if (ret != 0)
451 return ret;
452
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200453 wait_for_completion(&net_device->channel_init_wait);
Haiyang Zhangf157e782011-12-15 13:45:16 -0800454
455 if (init_packet->msg.init_msg.init_complete.status !=
456 NVSP_STAT_SUCCESS)
457 return -EINVAL;
458
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800459 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800460 return 0;
461
Haiyang Zhang71790a22015-07-24 10:08:40 -0700462 /* NVSPv2 or later: Send NDIS config */
Haiyang Zhangf157e782011-12-15 13:45:16 -0800463 memset(init_packet, 0, sizeof(struct nvsp_message));
464 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200465 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000466 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800467
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700468 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
Haiyang Zhang71790a22015-07-24 10:08:40 -0700469 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
470
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700471 /* Teaming bit is needed to receive link speed updates */
472 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
473 }
474
Haiyang Zhangf157e782011-12-15 13:45:16 -0800475 ret = vmbus_sendpacket(device->channel, init_packet,
476 sizeof(struct nvsp_message),
477 (unsigned long)init_packet,
478 VM_PKT_DATA_INBAND, 0);
479
480 return ret;
481}
482
stephen hemminger95790832017-06-08 16:21:22 -0700483static int netvsc_connect_vsp(struct hv_device *device,
stephen hemminger8b532792017-08-09 17:46:11 -0700484 struct netvsc_device *net_device,
485 const struct netvsc_device_info *device_info)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800486{
Colin Ian King1b17ca02017-09-22 16:50:23 +0100487 static const u32 ver_list[] = {
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700488 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
stephen hemminger95790832017-06-08 16:21:22 -0700489 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5
490 };
491 struct nvsp_message *init_packet;
492 int ndis_version, i, ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700493
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800494 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700495
Haiyang Zhangf157e782011-12-15 13:45:16 -0800496 /* Negotiate the latest NVSP protocol supported */
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700497 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800498 if (negotiate_nvsp_ver(device, net_device, init_packet,
499 ver_list[i]) == 0) {
500 net_device->nvsp_version = ver_list[i];
501 break;
502 }
503
504 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700505 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800506 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700507 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800508
509 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
510
Bill Pemberton454f18a2009-07-27 16:47:24 -0400511 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800512 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700513
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800514 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700515 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800516 else
517 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700518
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800519 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
520 init_packet->msg.v1_msg.
521 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800522 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800523 init_packet->msg.v1_msg.
524 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800525 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700526
Bill Pemberton454f18a2009-07-27 16:47:24 -0400527 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800528 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800529 sizeof(struct nvsp_message),
530 (unsigned long)init_packet,
531 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700532 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800533 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700534
Haiyang Zhang99d30162014-03-09 16:10:59 -0700535
stephen hemminger8b532792017-08-09 17:46:11 -0700536 ret = netvsc_init_buf(device, net_device, device_info);
Hank Janssenfceaf242009-07-13 15:34:54 -0700537
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800538cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700539 return ret;
540}
541
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200542static void netvsc_disconnect_vsp(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700543{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200544 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700545}
546
Hank Janssen3e189512010-03-04 22:11:00 +0000547/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800548 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700549 */
Stephen Hemmingere08f3ea2016-08-23 12:17:50 -0700550void netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700551{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200552 struct net_device *ndev = hv_get_drvdata(device);
553 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -0700554 struct netvsc_device *net_device
555 = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger15a863b2017-02-27 10:26:49 -0800556 int i;
Hank Janssenfceaf242009-07-13 15:34:54 -0700557
Stephen Hemminger8195b132017-09-06 13:53:05 -0700558 cancel_work_sync(&net_device->subchan_work);
559
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200560 netvsc_disconnect_vsp(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700561
stephen hemminger545a8e72017-03-22 14:51:00 -0700562 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700563
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700564 /*
565 * At this point, no one should be accessing net_device
566 * except in here
567 */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100568 netdev_dbg(ndev, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700569
Bill Pemberton454f18a2009-07-27 16:47:24 -0400570 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800571 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700572
stephen hemminger76bb5db2017-04-19 15:22:02 -0700573 /* And dissassociate NAPI context from device */
stephen hemminger79cd8742017-03-09 15:04:15 -0800574 for (i = 0; i < net_device->num_chn; i++)
stephen hemminger76bb5db2017-04-19 15:22:02 -0700575 netif_napi_del(&net_device->chan_table[i].napi);
stephen hemminger15a863b2017-02-27 10:26:49 -0800576
Bill Pemberton454f18a2009-07-27 16:47:24 -0400577 /* Release all resources */
stephen hemminger545a8e72017-03-22 14:51:00 -0700578 free_netvsc_device_rcu(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700579}
580
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000581#define RING_AVAIL_PERCENT_HIWATER 20
582#define RING_AVAIL_PERCENT_LOWATER 10
583
584/*
585 * Get the percentage of available bytes to write in the ring.
586 * The return value is in range from 0 to 100.
587 */
588static inline u32 hv_ringbuf_avail_percent(
589 struct hv_ring_buffer_info *ring_info)
590{
591 u32 avail_read, avail_write;
592
593 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
594
595 return avail_write * 100 / ring_info->ring_datasize;
596}
597
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700598static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
599 u32 index)
600{
601 sync_change_bit(index, net_device->send_section_map);
602}
603
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700604static void netvsc_send_tx_complete(struct netvsc_device *net_device,
605 struct vmbus_channel *incoming_channel,
606 struct hv_device *device,
stephen hemmingerf9645432017-04-07 14:41:19 -0400607 const struct vmpacket_descriptor *desc,
608 int budget)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700609{
stephen hemminger50698d82017-02-27 10:26:47 -0800610 struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700611 struct net_device *ndev = hv_get_drvdata(device);
Simon Xiao09af87d2017-09-29 11:39:46 -0700612 struct net_device_context *ndev_ctx = netdev_priv(ndev);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700613 struct vmbus_channel *channel = device->channel;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700614 u16 q_idx = 0;
615 int queue_sends;
616
617 /* Notify the layer above us */
618 if (likely(skb)) {
stephen hemminger793e3952017-01-24 13:06:12 -0800619 const struct hv_netvsc_packet *packet
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700620 = (struct hv_netvsc_packet *)skb->cb;
stephen hemminger793e3952017-01-24 13:06:12 -0800621 u32 send_index = packet->send_buf_index;
622 struct netvsc_stats *tx_stats;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700623
624 if (send_index != NETVSC_INVALID_INDEX)
625 netvsc_free_send_slot(net_device, send_index);
stephen hemminger793e3952017-01-24 13:06:12 -0800626 q_idx = packet->q_idx;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700627 channel = incoming_channel;
628
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800629 tx_stats = &net_device->chan_table[q_idx].tx_stats;
stephen hemminger793e3952017-01-24 13:06:12 -0800630
631 u64_stats_update_begin(&tx_stats->syncp);
632 tx_stats->packets += packet->total_packets;
633 tx_stats->bytes += packet->total_bytes;
634 u64_stats_update_end(&tx_stats->syncp);
635
stephen hemmingerf9645432017-04-07 14:41:19 -0400636 napi_consume_skb(skb, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700637 }
638
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800639 queue_sends =
640 atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700641
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800642 if (net_device->destroy && queue_sends == 0)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700643 wake_up(&net_device->wait_drain);
644
645 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700646 (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER ||
Simon Xiao09af87d2017-09-29 11:39:46 -0700647 queue_sends < 1)) {
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700648 netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
Simon Xiao09af87d2017-09-29 11:39:46 -0700649 ndev_ctx->eth_stats.wake_queue++;
650 }
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700651}
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);
Simon Xiao09af87d2017-09-29 11:39:46 -0700755 struct net_device_context *ndev_ctx = netdev_priv(ndev);
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800756 struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700757 u64 req_id;
758 int ret;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700759 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700760
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700761 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
Joe Perches956a25c2017-07-31 10:30:54 -0700762 if (skb)
763 rpkt->channel_type = 0; /* 0 is RMC_DATA */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700764 else
Joe Perches956a25c2017-07-31 10:30:54 -0700765 rpkt->channel_type = 1; /* 1 is RMC_CONTROL */
766
767 rpkt->send_buf_section_index = packet->send_buf_index;
768 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
769 rpkt->send_buf_section_size = 0;
770 else
771 rpkt->send_buf_section_size = packet->total_data_buflen;
Hank Janssenfceaf242009-07-13 15:34:54 -0700772
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800773 req_id = (ulong)skb;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000774
Haiyang Zhangc3582a22014-12-01 13:28:39 -0800775 if (out_channel->rescind)
776 return -ENODEV;
777
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800778 if (packet->page_buf_cnt) {
stephen hemminger02b6de02017-07-28 08:59:44 -0700779 if (packet->cp_partial)
780 pb += packet->rmsg_pgcnt;
781
stephen hemminger5a668d82017-08-16 08:56:25 -0700782 ret = vmbus_sendpacket_pagebuffer(out_channel,
783 pb, packet->page_buf_cnt,
784 &nvmsg, sizeof(nvmsg),
785 req_id);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700786 } else {
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700787 ret = vmbus_sendpacket(out_channel,
788 &nvmsg, sizeof(nvmsg),
789 req_id, VM_PKT_DATA_INBAND,
790 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700791 }
792
Haiyang Zhang1d068252011-12-02 11:56:25 -0800793 if (ret == 0) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800794 atomic_inc_return(&nvchan->queue_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700795
Simon Xiao09af87d2017-09-29 11:39:46 -0700796 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800797 netif_tx_stop_queue(txq);
Simon Xiao09af87d2017-09-29 11:39:46 -0700798 ndev_ctx->eth_stats.stop_queue++;
799 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800800 } else if (ret == -EAGAIN) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800801 netif_tx_stop_queue(txq);
Simon Xiao09af87d2017-09-29 11:39:46 -0700802 ndev_ctx->eth_stats.stop_queue++;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800803 if (atomic_read(&nvchan->queue_sends) < 1) {
804 netif_tx_wake_queue(txq);
Simon Xiao09af87d2017-09-29 11:39:46 -0700805 ndev_ctx->eth_stats.wake_queue++;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000806 ret = -ENOSPC;
807 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800808 } else {
stephen hemminger4a2176c2017-07-28 08:59:43 -0700809 netdev_err(ndev,
810 "Unable to send packet pages %u len %u, ret %d\n",
811 packet->page_buf_cnt, packet->total_data_buflen,
812 ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800813 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700814
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700815 return ret;
816}
817
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800818/* Move packet out of multi send data (msd), and clear msd */
819static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
820 struct sk_buff **msd_skb,
821 struct multi_send_data *msdp)
822{
823 *msd_skb = msdp->skb;
824 *msd_send = msdp->pkt;
825 msdp->skb = NULL;
826 msdp->pkt = NULL;
827 msdp->count = 0;
828}
829
stephen hemminger2a926f72017-07-19 11:53:17 -0700830/* RCU already held by caller */
831int netvsc_send(struct net_device_context *ndev_ctx,
KY Srinivasan24476762015-12-01 16:43:06 -0800832 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800833 struct rndis_message *rndis_msg,
stephen hemminger02b6de02017-07-28 08:59:44 -0700834 struct hv_page_buffer *pb,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800835 struct sk_buff *skb)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700836{
stephen hemminger39629812017-07-19 11:53:19 -0700837 struct netvsc_device *net_device
stephen hemminger867047c2017-07-28 08:59:42 -0700838 = rcu_dereference_bh(ndev_ctx->nvdev);
stephen hemminger2a926f72017-07-19 11:53:17 -0700839 struct hv_device *device = ndev_ctx->device_ctx;
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700840 int ret = 0;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800841 struct netvsc_channel *nvchan;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700842 u32 pktlen = packet->total_data_buflen, msd_len = 0;
843 unsigned int section_index = NETVSC_INVALID_INDEX;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700844 struct multi_send_data *msdp;
845 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800846 struct sk_buff *msd_skb = NULL;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700847 bool try_batch;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800848 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700849
stephen hemminger592b4fe2017-06-08 16:21:23 -0700850 /* If device is rescinded, return error and packet will get dropped. */
stephen hemminger2a926f72017-07-19 11:53:17 -0700851 if (unlikely(!net_device || net_device->destroy))
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700852 return -ENODEV;
853
Vitaly Kuznetsove8f0a892016-10-19 15:53:01 +0200854 /* We may race with netvsc_connect_vsp()/netvsc_init_buf() and get
855 * here before the negotiation with the host is finished and
856 * send_section_map may not be allocated yet.
857 */
stephen hemminger2d694d22017-06-08 16:21:21 -0700858 if (unlikely(!net_device->send_section_map))
Vitaly Kuznetsove8f0a892016-10-19 15:53:01 +0200859 return -EAGAIN;
860
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800861 nvchan = &net_device->chan_table[packet->q_idx];
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700862 packet->send_buf_index = NETVSC_INVALID_INDEX;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700863 packet->cp_partial = false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700864
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800865 /* Send control message directly without accessing msd (Multi-Send
866 * Data) field which may be changed during data packet processing.
867 */
868 if (!skb) {
869 cur_send = packet;
870 goto send_now;
871 }
872
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700873 /* batch packets in send buffer if possible */
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800874 msdp = &nvchan->msd;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700875 if (msdp->pkt)
876 msd_len = msdp->pkt->total_data_buflen;
877
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700878 try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700879 if (try_batch && msd_len + pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700880 net_device->send_section_size) {
881 section_index = msdp->pkt->send_buf_index;
882
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700883 } else if (try_batch && msd_len + packet->rmsg_size <
884 net_device->send_section_size) {
885 section_index = msdp->pkt->send_buf_index;
886 packet->cp_partial = true;
887
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700888 } else if (pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700889 net_device->send_section_size) {
890 section_index = netvsc_get_next_send_section(net_device);
stephen hemmingercad5c192017-08-09 17:46:12 -0700891 if (unlikely(section_index == NETVSC_INVALID_INDEX)) {
892 ++ndev_ctx->eth_stats.tx_send_full;
893 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800894 move_pkt_msd(&msd_send, &msd_skb, msdp);
895 msd_len = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700896 }
897 }
898
899 if (section_index != NETVSC_INVALID_INDEX) {
900 netvsc_copy_to_send_buf(net_device,
901 section_index, msd_len,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800902 packet, rndis_msg, pb, skb);
KY Srinivasanb08cc792015-03-29 21:08:42 -0700903
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700904 packet->send_buf_index = section_index;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700905
906 if (packet->cp_partial) {
907 packet->page_buf_cnt -= packet->rmsg_pgcnt;
908 packet->total_data_buflen = msd_len + packet->rmsg_size;
909 } else {
910 packet->page_buf_cnt = 0;
911 packet->total_data_buflen += msd_len;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700912 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700913
stephen hemminger793e3952017-01-24 13:06:12 -0800914 if (msdp->pkt) {
915 packet->total_packets += msdp->pkt->total_packets;
916 packet->total_bytes += msdp->pkt->total_bytes;
917 }
918
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800919 if (msdp->skb)
Stephen Hemminger17db4bc2016-09-22 16:56:29 -0700920 dev_consume_skb_any(msdp->skb);
Haiyang Zhangee90b812015-04-06 15:22:54 -0700921
KY Srinivasanbde79be2015-12-01 16:43:17 -0800922 if (xmit_more && !packet->cp_partial) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800923 msdp->skb = skb;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700924 msdp->pkt = packet;
925 msdp->count++;
926 } else {
927 cur_send = packet;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800928 msdp->skb = NULL;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700929 msdp->pkt = NULL;
930 msdp->count = 0;
931 }
932 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800933 move_pkt_msd(&msd_send, &msd_skb, msdp);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700934 cur_send = packet;
935 }
936
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700937 if (msd_send) {
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700938 int m_ret = netvsc_send_pkt(device, msd_send, net_device,
939 NULL, msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700940
941 if (m_ret != 0) {
942 netvsc_free_send_slot(net_device,
943 msd_send->send_buf_index);
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800944 dev_kfree_skb_any(msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700945 }
946 }
947
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800948send_now:
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700949 if (cur_send)
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200950 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700951
Jerry Snitselaar7aab5152015-05-04 10:57:16 -0700952 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
953 netvsc_free_send_slot(net_device, section_index);
Haiyang Zhangd953ca42015-01-29 12:34:49 -0800954
Hank Janssenfceaf242009-07-13 15:34:54 -0700955 return ret;
956}
957
stephen hemminger7426b1a2017-07-28 08:59:45 -0700958/* Send pending recv completions */
stephen hemmingercad5c192017-08-09 17:46:12 -0700959static int send_recv_completions(struct net_device *ndev,
960 struct netvsc_device *nvdev,
961 struct netvsc_channel *nvchan)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700962{
stephen hemminger7426b1a2017-07-28 08:59:45 -0700963 struct multi_recv_comp *mrc = &nvchan->mrc;
964 struct recv_comp_msg {
965 struct nvsp_message_header hdr;
966 u32 status;
967 } __packed;
968 struct recv_comp_msg msg = {
969 .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
970 };
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700971 int ret;
972
stephen hemminger7426b1a2017-07-28 08:59:45 -0700973 while (mrc->first != mrc->next) {
974 const struct recv_comp_data *rcd
975 = mrc->slots + mrc->first;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700976
stephen hemminger7426b1a2017-07-28 08:59:45 -0700977 msg.status = rcd->status;
978 ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg),
979 rcd->tid, VM_PKT_COMP, 0);
stephen hemmingercad5c192017-08-09 17:46:12 -0700980 if (unlikely(ret)) {
981 struct net_device_context *ndev_ctx = netdev_priv(ndev);
982
983 ++ndev_ctx->eth_stats.rx_comp_busy;
stephen hemminger7426b1a2017-07-28 08:59:45 -0700984 return ret;
stephen hemmingercad5c192017-08-09 17:46:12 -0700985 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700986
stephen hemminger7426b1a2017-07-28 08:59:45 -0700987 if (++mrc->first == nvdev->recv_completion_cnt)
988 mrc->first = 0;
989 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700990
stephen hemminger7426b1a2017-07-28 08:59:45 -0700991 /* receive completion ring has been emptied */
992 if (unlikely(nvdev->destroy))
993 wake_up(&nvdev->wait_drain);
994
995 return 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700996}
997
stephen hemminger7426b1a2017-07-28 08:59:45 -0700998/* Count how many receive completions are outstanding */
999static void recv_comp_slot_avail(const struct netvsc_device *nvdev,
1000 const struct multi_recv_comp *mrc,
1001 u32 *filled, u32 *avail)
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001002{
stephen hemminger7426b1a2017-07-28 08:59:45 -07001003 u32 count = nvdev->recv_completion_cnt;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001004
stephen hemminger7426b1a2017-07-28 08:59:45 -07001005 if (mrc->next >= mrc->first)
1006 *filled = mrc->next - mrc->first;
1007 else
1008 *filled = (count - mrc->first) + mrc->next;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001009
stephen hemminger7426b1a2017-07-28 08:59:45 -07001010 *avail = count - *filled - 1;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001011}
1012
stephen hemminger7426b1a2017-07-28 08:59:45 -07001013/* Add receive complete to ring to send to host. */
1014static void enq_receive_complete(struct net_device *ndev,
1015 struct netvsc_device *nvdev, u16 q_idx,
1016 u64 tid, u32 status)
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001017{
stephen hemminger7426b1a2017-07-28 08:59:45 -07001018 struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx];
1019 struct multi_recv_comp *mrc = &nvchan->mrc;
1020 struct recv_comp_data *rcd;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001021 u32 filled, avail;
1022
stephen hemminger7426b1a2017-07-28 08:59:45 -07001023 recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001024
stephen hemminger7426b1a2017-07-28 08:59:45 -07001025 if (unlikely(filled > NAPI_POLL_WEIGHT)) {
stephen hemmingercad5c192017-08-09 17:46:12 -07001026 send_recv_completions(ndev, nvdev, nvchan);
stephen hemminger7426b1a2017-07-28 08:59:45 -07001027 recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001028 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001029
stephen hemminger7426b1a2017-07-28 08:59:45 -07001030 if (unlikely(!avail)) {
1031 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1032 q_idx, tid);
1033 return;
1034 }
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001035
stephen hemminger7426b1a2017-07-28 08:59:45 -07001036 rcd = mrc->slots + mrc->next;
1037 rcd->tid = tid;
1038 rcd->status = status;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001039
stephen hemminger7426b1a2017-07-28 08:59:45 -07001040 if (++mrc->next == nvdev->recv_completion_cnt)
1041 mrc->next = 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001042}
1043
stephen hemminger15a863b2017-02-27 10:26:49 -08001044static int netvsc_receive(struct net_device *ndev,
stephen hemminger7426b1a2017-07-28 08:59:45 -07001045 struct netvsc_device *net_device,
1046 struct net_device_context *net_device_ctx,
1047 struct hv_device *device,
1048 struct vmbus_channel *channel,
1049 const struct vmpacket_descriptor *desc,
1050 struct nvsp_message *nvsp)
Hank Janssenfceaf242009-07-13 15:34:54 -07001051{
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001052 const struct vmtransfer_page_packet_header *vmxferpage_packet
1053 = container_of(desc, const struct vmtransfer_page_packet_header, d);
stephen hemminger15a863b2017-02-27 10:26:49 -08001054 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemmingerdc54a082017-01-24 13:06:08 -08001055 char *recv_buf = net_device->recv_buf;
Haiyang Zhang4baab262014-04-21 14:54:43 -07001056 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -08001057 int i;
1058 int count = 0;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -07001059
Bill Pemberton454f18a2009-07-27 16:47:24 -04001060 /* Make sure this is a valid nvsp packet */
stephen hemmingerdc54a082017-01-24 13:06:08 -08001061 if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
1062 netif_err(net_device_ctx, rx_err, ndev,
1063 "Unknown nvsp packet type received %u\n",
1064 nvsp->hdr.msg_type);
stephen hemminger15a863b2017-02-27 10:26:49 -08001065 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001066 }
1067
stephen hemmingerdc54a082017-01-24 13:06:08 -08001068 if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
1069 netif_err(net_device_ctx, rx_err, ndev,
1070 "Invalid xfer page set id - expecting %x got %x\n",
1071 NETVSC_RECEIVE_BUFFER_ID,
1072 vmxferpage_packet->xfer_pageset_id);
stephen hemminger15a863b2017-02-27 10:26:49 -08001073 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001074 }
1075
Haiyang Zhang4baab262014-04-21 14:54:43 -07001076 count = vmxferpage_packet->range_cnt;
Hank Janssenfceaf242009-07-13 15:34:54 -07001077
Bill Pemberton454f18a2009-07-27 16:47:24 -04001078 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -07001079 for (i = 0; i < count; i++) {
stephen hemmingerdc54a082017-01-24 13:06:08 -08001080 void *data = recv_buf
1081 + vmxferpage_packet->ranges[i].byte_offset;
1082 u32 buflen = vmxferpage_packet->ranges[i].byte_count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001083
Bill Pemberton454f18a2009-07-27 16:47:24 -04001084 /* Pass it to the upper layer */
stephen hemmingerdc54a082017-01-24 13:06:08 -08001085 status = rndis_filter_receive(ndev, net_device, device,
1086 channel, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -07001087 }
1088
stephen hemminger7426b1a2017-07-28 08:59:45 -07001089 enq_receive_complete(ndev, net_device, q_idx,
1090 vmxferpage_packet->d.trans_id, status);
stephen hemminger15a863b2017-02-27 10:26:49 -08001091
stephen hemminger15a863b2017-02-27 10:26:49 -08001092 return count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001093}
1094
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001095static void netvsc_send_table(struct hv_device *hdev,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001096 struct nvsp_message *nvmsg)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001097{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001098 struct net_device *ndev = hv_get_drvdata(hdev);
stephen hemminger7ce10122017-03-09 14:58:29 -08001099 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001100 int i;
1101 u32 count, *tab;
1102
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001103 count = nvmsg->msg.v5_msg.send_table.count;
1104 if (count != VRSS_SEND_TAB_SIZE) {
1105 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1106 return;
1107 }
1108
1109 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1110 nvmsg->msg.v5_msg.send_table.offset);
1111
1112 for (i = 0; i < count; i++)
Haiyang Zhang39e91cf2017-10-13 12:28:04 -07001113 net_device_ctx->tx_table[i] = tab[i];
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001114}
1115
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001116static void netvsc_send_vf(struct net_device_context *net_device_ctx,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001117 struct nvsp_message *nvmsg)
1118{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001119 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1120 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
Haiyang Zhang71790a22015-07-24 10:08:40 -07001121}
1122
1123static inline void netvsc_receive_inband(struct hv_device *hdev,
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001124 struct net_device_context *net_device_ctx,
1125 struct nvsp_message *nvmsg)
Haiyang Zhang71790a22015-07-24 10:08:40 -07001126{
1127 switch (nvmsg->hdr.msg_type) {
1128 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1129 netvsc_send_table(hdev, nvmsg);
1130 break;
1131
1132 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001133 netvsc_send_vf(net_device_ctx, nvmsg);
Haiyang Zhang71790a22015-07-24 10:08:40 -07001134 break;
1135 }
1136}
1137
stephen hemminger15a863b2017-02-27 10:26:49 -08001138static int netvsc_process_raw_pkt(struct hv_device *device,
1139 struct vmbus_channel *channel,
1140 struct netvsc_device *net_device,
1141 struct net_device *ndev,
stephen hemmingerf9645432017-04-07 14:41:19 -04001142 const struct vmpacket_descriptor *desc,
1143 int budget)
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001144{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001145 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001146 struct nvsp_message *nvmsg = hv_pkt_data(desc);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001147
1148 switch (desc->type) {
1149 case VM_PKT_COMP:
stephen hemmingerf9645432017-04-07 14:41:19 -04001150 netvsc_send_completion(net_device, channel, device,
1151 desc, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001152 break;
1153
1154 case VM_PKT_DATA_USING_XFER_PAGES:
stephen hemminger15a863b2017-02-27 10:26:49 -08001155 return netvsc_receive(ndev, net_device, net_device_ctx,
1156 device, channel, desc, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001157 break;
1158
1159 case VM_PKT_DATA_INBAND:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001160 netvsc_receive_inband(device, net_device_ctx, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001161 break;
1162
1163 default:
1164 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001165 desc->type, desc->trans_id);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001166 break;
1167 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001168
1169 return 0;
1170}
1171
1172static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
1173{
1174 struct vmbus_channel *primary = channel->primary_channel;
1175
1176 return primary ? primary->device_obj : channel->device_obj;
1177}
1178
stephen hemminger262b7f12017-03-16 16:12:38 -07001179/* Network processing softirq
1180 * Process data in incoming ring buffer from host
1181 * Stops when ring is empty or budget is met or exceeded.
1182 */
stephen hemminger15a863b2017-02-27 10:26:49 -08001183int netvsc_poll(struct napi_struct *napi, int budget)
1184{
1185 struct netvsc_channel *nvchan
1186 = container_of(napi, struct netvsc_channel, napi);
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001187 struct netvsc_device *net_device = nvchan->net_device;
stephen hemminger15a863b2017-02-27 10:26:49 -08001188 struct vmbus_channel *channel = nvchan->channel;
1189 struct hv_device *device = netvsc_channel_to_device(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001190 struct net_device *ndev = hv_get_drvdata(device);
stephen hemminger15a863b2017-02-27 10:26:49 -08001191 int work_done = 0;
1192
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001193 /* If starting a new interval */
1194 if (!nvchan->desc)
1195 nvchan->desc = hv_pkt_iter_first(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001196
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001197 while (nvchan->desc && work_done < budget) {
1198 work_done += netvsc_process_raw_pkt(device, channel, net_device,
stephen hemmingerf9645432017-04-07 14:41:19 -04001199 ndev, nvchan->desc, budget);
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001200 nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
stephen hemminger15a863b2017-02-27 10:26:49 -08001201 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001202
stephen hemmingerf4e40362017-07-28 08:59:47 -07001203 /* If send of pending receive completions suceeded
1204 * and did not exhaust NAPI budget this time
stephen hemminger7426b1a2017-07-28 08:59:45 -07001205 * and not doing busy poll
stephen hemmingerf4e40362017-07-28 08:59:47 -07001206 * then re-enable host interrupts
1207 * and reschedule if ring is not empty.
stephen hemminger262b7f12017-03-16 16:12:38 -07001208 */
stephen hemmingercad5c192017-08-09 17:46:12 -07001209 if (send_recv_completions(ndev, net_device, nvchan) == 0 &&
stephen hemminger7426b1a2017-07-28 08:59:45 -07001210 work_done < budget &&
stephen hemminger15a863b2017-02-27 10:26:49 -08001211 napi_complete_done(napi, work_done) &&
stephen hemminger7426b1a2017-07-28 08:59:45 -07001212 hv_end_read(&channel->inbound)) {
1213 hv_begin_read(&channel->inbound);
stephen hemminger15a863b2017-02-27 10:26:49 -08001214 napi_reschedule(napi);
stephen hemminger7426b1a2017-07-28 08:59:45 -07001215 }
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001216
1217 /* Driver may overshoot since multiple packets per descriptor */
1218 return min(work_done, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001219}
1220
stephen hemminger262b7f12017-03-16 16:12:38 -07001221/* Call back when data is available in host ring buffer.
1222 * Processing is deferred until network softirq (NAPI)
1223 */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001224void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001225{
stephen hemminger6de38af2017-03-16 16:12:37 -07001226 struct netvsc_channel *nvchan = context;
stephen hemminger43bf99c2017-07-24 10:57:27 -07001227 struct vmbus_channel *channel = nvchan->channel;
1228 struct hv_ring_buffer_info *rbi = &channel->inbound;
1229
1230 /* preload first vmpacket descriptor */
1231 prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
stephen hemminger0b307eb2017-01-24 13:05:58 -08001232
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001233 if (napi_schedule_prep(&nvchan->napi)) {
1234 /* disable interupts from host */
stephen hemminger43bf99c2017-07-24 10:57:27 -07001235 hv_begin_read(rbi);
stephen hemminger0d6dd352017-03-09 15:04:14 -08001236
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001237 __napi_schedule(&nvchan->napi);
1238 }
Hank Janssenfceaf242009-07-13 15:34:54 -07001239}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001240
1241/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001242 * netvsc_device_add - Callback when the device belonging to this
1243 * driver is added
1244 */
stephen hemminger9749fed2017-07-19 11:53:16 -07001245struct netvsc_device *netvsc_device_add(struct hv_device *device,
1246 const struct netvsc_device_info *device_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001247{
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001248 int i, ret = 0;
stephen hemminger2c7f83c2017-01-24 13:06:09 -08001249 int ring_size = device_info->ring_size;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001250 struct netvsc_device *net_device;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001251 struct net_device *ndev = hv_get_drvdata(device);
1252 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001253
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001254 net_device = alloc_net_device();
Dan Carpenterb1c84922014-09-04 14:11:23 +03001255 if (!net_device)
stephen hemminger9749fed2017-07-19 11:53:16 -07001256 return ERR_PTR(-ENOMEM);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001257
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001258 net_device->ring_size = ring_size;
1259
stephen hemminger15a863b2017-02-27 10:26:49 -08001260 /* Because the device uses NAPI, all the interrupt batching and
1261 * control is done via Net softirq, not the channel handling
1262 */
1263 set_channel_read_mode(device->channel, HV_CALL_ISR);
1264
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001265 /* If we're reopening the device we may have multiple queues, fill the
1266 * chn_table with the default channel to use it before subchannels are
1267 * opened.
1268 * Initialize the channel state before we open;
1269 * we can be interrupted as soon as we open the channel.
1270 */
1271
1272 for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
1273 struct netvsc_channel *nvchan = &net_device->chan_table[i];
1274
1275 nvchan->channel = device->channel;
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001276 nvchan->net_device = net_device;
Florian Fainelli4a0dee12017-08-01 12:11:12 -07001277 u64_stats_init(&nvchan->tx_stats.syncp);
1278 u64_stats_init(&nvchan->rx_stats.syncp);
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001279 }
1280
stephen hemminger2be0f262017-05-03 16:59:21 -07001281 /* Enable NAPI handler before init callbacks */
1282 netif_napi_add(ndev, &net_device->chan_table[0].napi,
1283 netvsc_poll, NAPI_POLL_WEIGHT);
1284
Haiyang Zhangb637e022011-04-21 12:30:45 -07001285 /* Open the channel */
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001286 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1287 ring_size * PAGE_SIZE, NULL, 0,
stephen hemminger6de38af2017-03-16 16:12:37 -07001288 netvsc_channel_cb,
1289 net_device->chan_table);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001290
1291 if (ret != 0) {
stephen hemminger2be0f262017-05-03 16:59:21 -07001292 netif_napi_del(&net_device->chan_table[0].napi);
Haiyang Zhangd9871152011-09-01 12:19:41 -07001293 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001294 goto cleanup;
1295 }
1296
1297 /* Channel is opened */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +01001298 netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001299
stephen hemminger15a863b2017-02-27 10:26:49 -08001300 napi_enable(&net_device->chan_table[0].napi);
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001301
1302 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1303 * populated.
1304 */
stephen hemminger545a8e72017-03-22 14:51:00 -07001305 rcu_assign_pointer(net_device_ctx->nvdev, net_device);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001306
Haiyang Zhangb637e022011-04-21 12:30:45 -07001307 /* Connect with the NetVsp */
stephen hemminger8b532792017-08-09 17:46:11 -07001308 ret = netvsc_connect_vsp(device, net_device, device_info);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001309 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001310 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001311 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001312 goto close;
1313 }
1314
stephen hemminger9749fed2017-07-19 11:53:16 -07001315 return net_device;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001316
1317close:
stephen hemminger49393342017-07-28 08:59:46 -07001318 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
1319 napi_disable(&net_device->chan_table[0].napi);
stephen hemminger15a863b2017-02-27 10:26:49 -08001320
Haiyang Zhangb637e022011-04-21 12:30:45 -07001321 /* Now, we can close the channel safely */
1322 vmbus_close(device->channel);
1323
1324cleanup:
stephen hemminger545a8e72017-03-22 14:51:00 -07001325 free_netvsc_device(&net_device->rcu);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001326
stephen hemminger9749fed2017-07-19 11:53:16 -07001327 return ERR_PTR(ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001328}