blob: 9b0532ec8b8743362180712a3ef5c9939a3c3899 [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Hank Janssenfceaf242009-07-13 15:34:54 -07002/*
Hank Janssenfceaf242009-07-13 15:34:54 -07003 * Copyright (c) 2009, Microsoft Corporation.
4 *
Hank Janssenfceaf242009-07-13 15:34:54 -07005 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +00006 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -07007 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -07008 */
Hank Jansseneb335bc2011-03-29 13:58:48 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070011#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080012#include <linux/sched.h>
13#include <linux/wait.h>
Greg Kroah-Hartman0ffa63b2009-07-15 11:06:01 -070014#include <linux/mm.h>
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -070015#include <linux/delay.h>
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070016#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Haiyang Zhangd9871152011-09-01 12:19:41 -070018#include <linux/netdevice.h>
Haiyang Zhangf157e782011-12-15 13:45:16 -080019#include <linux/if_ether.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100020#include <linux/vmalloc.h>
stephen hemminger9749fed2017-07-19 11:53:16 -070021#include <linux/rtnetlink.h>
stephen hemminger43bf99c2017-07-24 10:57:27 -070022#include <linux/prefetch.h>
stephen hemminger9749fed2017-07-19 11:53:16 -070023
KY Srinivasanc25aaf82014-04-30 10:14:31 -070024#include <asm/sync_bitops.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070025
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070026#include "hyperv_net.h"
Stephen Hemmingerec966382018-03-16 15:44:28 -070027#include "netvsc_trace.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070028
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070029/*
30 * Switch the data path from the synthetic interface to the VF
31 * interface.
32 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020033void netvsc_switch_datapath(struct net_device *ndev, bool vf)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070034{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +020035 struct net_device_context *net_device_ctx = netdev_priv(ndev);
36 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger79e8cbe2017-07-19 11:53:13 -070037 struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020038 struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070039
40 memset(init_pkt, 0, sizeof(struct nvsp_message));
41 init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
42 if (vf)
43 init_pkt->msg.v4_msg.active_dp.active_datapath =
44 NVSP_DATAPATH_VF;
45 else
46 init_pkt->msg.v4_msg.active_dp.active_datapath =
47 NVSP_DATAPATH_SYNTHETIC;
48
Stephen Hemmingerec966382018-03-16 15:44:28 -070049 trace_nvsp_send(ndev, init_pkt);
50
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070051 vmbus_sendpacket(dev->channel, init_pkt,
52 sizeof(struct nvsp_message),
53 (unsigned long)init_pkt,
54 VM_PKT_DATA_INBAND, 0);
55}
56
Stephen Hemminger3ffe64f2018-06-29 14:07:16 -070057/* Worker to setup sub channels on initial setup
58 * Initial hotplug event occurs in softirq context
59 * and can't wait for channels.
60 */
61static void netvsc_subchan_work(struct work_struct *w)
62{
63 struct netvsc_device *nvdev =
64 container_of(w, struct netvsc_device, subchan_work);
65 struct rndis_device *rdev;
66 int i, ret;
67
68 /* Avoid deadlock with device removal already under RTNL */
69 if (!rtnl_trylock()) {
70 schedule_work(w);
71 return;
72 }
73
74 rdev = nvdev->extension;
75 if (rdev) {
Haiyang Zhang17d91252019-01-15 00:51:44 +000076 ret = rndis_set_subchannel(rdev->ndev, nvdev, NULL);
Stephen Hemminger3ffe64f2018-06-29 14:07:16 -070077 if (ret == 0) {
78 netif_device_attach(rdev->ndev);
79 } else {
80 /* fallback to only primary channel */
81 for (i = 1; i < nvdev->num_chn; i++)
82 netif_napi_del(&nvdev->chan_table[i].napi);
83
84 nvdev->max_chn = 1;
85 nvdev->num_chn = 1;
86 }
87 }
88
89 rtnl_unlock();
90}
91
Vitaly Kuznetsov88098832016-05-13 13:55:25 +020092static struct netvsc_device *alloc_net_device(void)
Hank Janssenfceaf242009-07-13 15:34:54 -070093{
Haiyang Zhang85799a32010-12-10 12:03:54 -080094 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070095
Haiyang Zhang85799a32010-12-10 12:03:54 -080096 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
97 if (!net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -070098 return NULL;
99
Haiyang Zhangdc5cd892012-06-04 06:42:38 +0000100 init_waitqueue_head(&net_device->wait_drain);
K. Y. Srinivasanc38b9c72011-08-27 11:31:12 -0700101 net_device->destroy = false;
Haiyang Zhang1b704c42019-03-28 19:40:36 +0000102 net_device->tx_disable = false;
Stephen Hemminger0da6edb2017-12-12 16:48:39 -0800103
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700104 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
105 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
stephen hemminger8b532792017-08-09 17:46:11 -0700106
Stephen Hemmingerfd612602016-08-23 12:17:51 -0700107 init_completion(&net_device->channel_init_wait);
stephen hemminger732e4982017-08-03 17:13:54 -0700108 init_waitqueue_head(&net_device->subchan_open);
Stephen Hemminger3ffe64f2018-06-29 14:07:16 -0700109 INIT_WORK(&net_device->subchan_work, netvsc_subchan_work);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700110
Haiyang Zhang85799a32010-12-10 12:03:54 -0800111 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700112}
113
stephen hemminger545a8e72017-03-22 14:51:00 -0700114static void free_netvsc_device(struct rcu_head *head)
Haiyang Zhangf90251c2014-08-15 19:18:19 +0000115{
stephen hemminger545a8e72017-03-22 14:51:00 -0700116 struct netvsc_device *nvdev
117 = container_of(head, struct netvsc_device, rcu);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700118 int i;
119
Stephen Hemminger02400fc2018-03-20 15:03:03 -0700120 kfree(nvdev->extension);
121 vfree(nvdev->recv_buf);
122 vfree(nvdev->send_buf);
123 kfree(nvdev->send_section_map);
124
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700125 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
stephen hemminger7426b1a2017-07-28 08:59:45 -0700126 vfree(nvdev->chan_table[i].mrc.slots);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700127
Haiyang Zhangf90251c2014-08-15 19:18:19 +0000128 kfree(nvdev);
129}
130
stephen hemminger545a8e72017-03-22 14:51:00 -0700131static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
132{
133 call_rcu(&nvdev->rcu, free_netvsc_device);
134}
stephen hemminger46b4f7f2017-01-24 13:06:11 -0800135
Mohammed Gamal79928942018-04-05 21:09:19 +0200136static void netvsc_revoke_recv_buf(struct hv_device *device,
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200137 struct netvsc_device *net_device,
138 struct net_device *ndev)
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700139{
Mohammed Gamal79928942018-04-05 21:09:19 +0200140 struct nvsp_message *revoke_packet;
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700141 int ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700142
143 /*
144 * If we got a section count, it means we received a
145 * SendReceiveBufferComplete msg (ie sent
146 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
147 * to send a revoke msg here
148 */
149 if (net_device->recv_section_cnt) {
150 /* Send the revoke receive buffer */
151 revoke_packet = &net_device->revoke_packet;
152 memset(revoke_packet, 0, sizeof(struct nvsp_message));
153
154 revoke_packet->hdr.msg_type =
155 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
156 revoke_packet->msg.v1_msg.
157 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
158
Stephen Hemmingerec966382018-03-16 15:44:28 -0700159 trace_nvsp_send(ndev, revoke_packet);
160
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200161 ret = vmbus_sendpacket(device->channel,
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700162 revoke_packet,
163 sizeof(struct nvsp_message),
164 (unsigned long)revoke_packet,
165 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700166 /* If the failure is because the channel is rescinded;
167 * ignore the failure since we cannot send on a rescinded
168 * channel. This would allow us to properly cleanup
169 * even when the channel is rescinded.
170 */
171 if (device->channel->rescind)
172 ret = 0;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700173 /*
174 * If we failed here, we might as well return and
175 * have a leak rather than continue and a bugchk
176 */
177 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700178 netdev_err(ndev, "unable to send "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700179 "revoke receive buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700180 return;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700181 }
stephen hemminger8b532792017-08-09 17:46:11 -0700182 net_device->recv_section_cnt = 0;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700183 }
Mohammed Gamal79928942018-04-05 21:09:19 +0200184}
185
186static void netvsc_revoke_send_buf(struct hv_device *device,
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200187 struct netvsc_device *net_device,
188 struct net_device *ndev)
Mohammed Gamal79928942018-04-05 21:09:19 +0200189{
Mohammed Gamal79928942018-04-05 21:09:19 +0200190 struct nvsp_message *revoke_packet;
191 int ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700192
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700193 /* Deal with the send buffer we may have setup.
194 * If we got a send section size, it means we received a
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800195 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
196 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700197 * to send a revoke msg here
198 */
stephen hemminger8b532792017-08-09 17:46:11 -0700199 if (net_device->send_section_cnt) {
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700200 /* Send the revoke receive buffer */
201 revoke_packet = &net_device->revoke_packet;
202 memset(revoke_packet, 0, sizeof(struct nvsp_message));
203
204 revoke_packet->hdr.msg_type =
205 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800206 revoke_packet->msg.v1_msg.revoke_send_buf.id =
207 NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700208
Stephen Hemmingerec966382018-03-16 15:44:28 -0700209 trace_nvsp_send(ndev, revoke_packet);
210
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200211 ret = vmbus_sendpacket(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700212 revoke_packet,
213 sizeof(struct nvsp_message),
214 (unsigned long)revoke_packet,
215 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan73e64fa2017-04-19 13:53:49 -0700216
217 /* If the failure is because the channel is rescinded;
218 * ignore the failure since we cannot send on a rescinded
219 * channel. This would allow us to properly cleanup
220 * even when the channel is rescinded.
221 */
222 if (device->channel->rescind)
223 ret = 0;
224
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700225 /* If we failed here, we might as well return and
226 * have a leak rather than continue and a bugchk
227 */
228 if (ret != 0) {
229 netdev_err(ndev, "unable to send "
230 "revoke send buffer to netvsp\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700231 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700232 }
stephen hemminger8b532792017-08-09 17:46:11 -0700233 net_device->send_section_cnt = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700234 }
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100235}
236
Mohammed Gamal79928942018-04-05 21:09:19 +0200237static void netvsc_teardown_recv_gpadl(struct hv_device *device,
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200238 struct netvsc_device *net_device,
239 struct net_device *ndev)
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100240{
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100241 int ret;
242
243 if (net_device->recv_buf_gpadl_handle) {
244 ret = vmbus_teardown_gpadl(device->channel,
245 net_device->recv_buf_gpadl_handle);
246
247 /* If we failed here, we might as well return and have a leak
248 * rather than continue and a bugchk
249 */
250 if (ret != 0) {
251 netdev_err(ndev,
252 "unable to teardown receive buffer's gpadl\n");
253 return;
254 }
255 net_device->recv_buf_gpadl_handle = 0;
256 }
Mohammed Gamal79928942018-04-05 21:09:19 +0200257}
258
259static void netvsc_teardown_send_gpadl(struct hv_device *device,
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200260 struct netvsc_device *net_device,
261 struct net_device *ndev)
Mohammed Gamal79928942018-04-05 21:09:19 +0200262{
Mohammed Gamal79928942018-04-05 21:09:19 +0200263 int ret;
Vitaly Kuznetsov0cf73782017-11-02 11:35:30 +0100264
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700265 if (net_device->send_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200266 ret = vmbus_teardown_gpadl(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700267 net_device->send_buf_gpadl_handle);
268
269 /* If we failed here, we might as well return and have a leak
270 * rather than continue and a bugchk
271 */
272 if (ret != 0) {
273 netdev_err(ndev,
274 "unable to teardown send buffer's gpadl\n");
Stephen Hemminger7a2a0a82016-08-23 12:17:54 -0700275 return;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700276 }
Dave Jones2f184232014-06-16 16:59:02 -0400277 net_device->send_buf_gpadl_handle = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700278 }
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700279}
280
stephen hemminger7426b1a2017-07-28 08:59:45 -0700281int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx)
282{
283 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
284 int node = cpu_to_node(nvchan->channel->target_cpu);
285 size_t size;
286
287 size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data);
288 nvchan->mrc.slots = vzalloc_node(size, node);
289 if (!nvchan->mrc.slots)
290 nvchan->mrc.slots = vzalloc(size);
291
292 return nvchan->mrc.slots ? 0 : -ENOMEM;
293}
294
stephen hemminger95790832017-06-08 16:21:22 -0700295static int netvsc_init_buf(struct hv_device *device,
stephen hemminger8b532792017-08-09 17:46:11 -0700296 struct netvsc_device *net_device,
297 const struct netvsc_device_info *device_info)
Hank Janssenfceaf242009-07-13 15:34:54 -0700298{
stephen hemminger7426b1a2017-07-28 08:59:45 -0700299 struct nvsp_1_message_send_receive_buffer_complete *resp;
stephen hemminger95833372017-08-09 17:46:07 -0700300 struct net_device *ndev = hv_get_drvdata(device);
301 struct nvsp_message *init_packet;
stephen hemminger8b532792017-08-09 17:46:11 -0700302 unsigned int buf_size;
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700303 size_t map_words;
stephen hemminger95833372017-08-09 17:46:07 -0700304 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700305
stephen hemminger8b532792017-08-09 17:46:11 -0700306 /* Get receive buffer area. */
Alex Ng0ab09be2017-09-20 11:17:35 -0700307 buf_size = device_info->recv_sections * device_info->recv_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700308 buf_size = roundup(buf_size, PAGE_SIZE);
309
Haiyang Zhang11b2b652017-12-11 08:56:57 -0800310 /* Legacy hosts only allow smaller receive buffer */
311 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
312 buf_size = min_t(unsigned int, buf_size,
313 NETVSC_RECEIVE_BUFFER_SIZE_LEGACY);
314
stephen hemminger8b532792017-08-09 17:46:11 -0700315 net_device->recv_buf = vzalloc(buf_size);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800316 if (!net_device->recv_buf) {
stephen hemminger8b532792017-08-09 17:46:11 -0700317 netdev_err(ndev,
318 "unable to allocate receive buffer of size %u\n",
319 buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700320 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800321 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700322 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700323
Haiyang Zhangc5d24bd2018-03-22 12:01:14 -0700324 net_device->recv_buf_size = buf_size;
325
Bill Pemberton454f18a2009-07-27 16:47:24 -0400326 /*
327 * Establish the gpadl handle for this buffer on this
328 * channel. Note: This call uses the vmbus connection rather
329 * than the channel to establish the gpadl handle.
330 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800331 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
stephen hemminger8b532792017-08-09 17:46:11 -0700332 buf_size,
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800333 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700334 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700335 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700336 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800337 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700338 }
339
Bill Pemberton454f18a2009-07-27 16:47:24 -0400340 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800341 init_packet = &net_device->channel_init_pkt;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800342 memset(init_packet, 0, sizeof(struct nvsp_message));
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800343 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
344 init_packet->msg.v1_msg.send_recv_buf.
345 gpadl_handle = net_device->recv_buf_gpadl_handle;
346 init_packet->msg.v1_msg.
347 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700348
Stephen Hemmingerec966382018-03-16 15:44:28 -0700349 trace_nvsp_send(ndev, init_packet);
350
Bill Pemberton454f18a2009-07-27 16:47:24 -0400351 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800352 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700353 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800354 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800355 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700356 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700357 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700358 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700359 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800360 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700361 }
362
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200363 wait_for_completion(&net_device->channel_init_wait);
Hank Janssenfceaf242009-07-13 15:34:54 -0700364
Bill Pemberton454f18a2009-07-27 16:47:24 -0400365 /* Check the response */
stephen hemminger7426b1a2017-07-28 08:59:45 -0700366 resp = &init_packet->msg.v1_msg.send_recv_buf_complete;
367 if (resp->status != NVSP_STAT_SUCCESS) {
368 netdev_err(ndev,
369 "Unable to complete receive buffer initialization with NetVsp - status %d\n",
370 resp->status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700371 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800372 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700373 }
374
Bill Pemberton454f18a2009-07-27 16:47:24 -0400375 /* Parse the response */
stephen hemminger7426b1a2017-07-28 08:59:45 -0700376 netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n",
377 resp->num_sections, resp->sections[0].sub_alloc_size,
378 resp->sections[0].num_sub_allocs);
Hank Janssenfceaf242009-07-13 15:34:54 -0700379
stephen hemminger8b532792017-08-09 17:46:11 -0700380 /* There should only be one section for the entire receive buffer */
381 if (resp->num_sections != 1 || resp->sections[0].offset != 0) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700382 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800383 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700384 }
385
stephen hemminger8b532792017-08-09 17:46:11 -0700386 net_device->recv_section_size = resp->sections[0].sub_alloc_size;
387 net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
388
stephen hemminger7426b1a2017-07-28 08:59:45 -0700389 /* Setup receive completion ring */
390 net_device->recv_completion_cnt
stephen hemminger8b532792017-08-09 17:46:11 -0700391 = round_up(net_device->recv_section_cnt + 1,
stephen hemminger7426b1a2017-07-28 08:59:45 -0700392 PAGE_SIZE / sizeof(u64));
393 ret = netvsc_alloc_recv_comp_ring(net_device, 0);
394 if (ret)
395 goto cleanup;
396
397 /* Now setup the send buffer. */
Alex Ng0ab09be2017-09-20 11:17:35 -0700398 buf_size = device_info->send_sections * device_info->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700399 buf_size = round_up(buf_size, PAGE_SIZE);
400
401 net_device->send_buf = vzalloc(buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700402 if (!net_device->send_buf) {
stephen hemminger8b532792017-08-09 17:46:11 -0700403 netdev_err(ndev, "unable to allocate send buffer of size %u\n",
404 buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700405 ret = -ENOMEM;
406 goto cleanup;
407 }
408
409 /* Establish the gpadl handle for this buffer on this
410 * channel. Note: This call uses the vmbus connection rather
411 * than the channel to establish the gpadl handle.
412 */
413 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
stephen hemminger8b532792017-08-09 17:46:11 -0700414 buf_size,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700415 &net_device->send_buf_gpadl_handle);
416 if (ret != 0) {
417 netdev_err(ndev,
418 "unable to establish send buffer's gpadl\n");
419 goto cleanup;
420 }
421
422 /* Notify the NetVsp of the gpadl handle */
423 init_packet = &net_device->channel_init_pkt;
424 memset(init_packet, 0, sizeof(struct nvsp_message));
425 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800426 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700427 net_device->send_buf_gpadl_handle;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800428 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700429
Stephen Hemmingerec966382018-03-16 15:44:28 -0700430 trace_nvsp_send(ndev, init_packet);
431
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700432 /* Send the gpadl notification request */
433 ret = vmbus_sendpacket(device->channel, init_packet,
434 sizeof(struct nvsp_message),
435 (unsigned long)init_packet,
436 VM_PKT_DATA_INBAND,
437 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
438 if (ret != 0) {
439 netdev_err(ndev,
440 "unable to send send buffer's gpadl to netvsp\n");
441 goto cleanup;
442 }
443
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200444 wait_for_completion(&net_device->channel_init_wait);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700445
446 /* Check the response */
447 if (init_packet->msg.v1_msg.
448 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
449 netdev_err(ndev, "Unable to complete send buffer "
450 "initialization with NetVsp - status %d\n",
451 init_packet->msg.v1_msg.
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800452 send_send_buf_complete.status);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700453 ret = -EINVAL;
454 goto cleanup;
455 }
456
457 /* Parse the response */
458 net_device->send_section_size = init_packet->msg.
459 v1_msg.send_send_buf_complete.section_size;
460
stephen hemminger8b532792017-08-09 17:46:11 -0700461 /* Section count is simply the size divided by the section size. */
462 net_device->send_section_cnt = buf_size / net_device->send_section_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700463
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100464 netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
465 net_device->send_section_size, net_device->send_section_cnt);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700466
467 /* Setup state for managing the send buffer. */
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700468 map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700469
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700470 net_device->send_section_map = kcalloc(map_words, sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800471 if (net_device->send_section_map == NULL) {
472 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700473 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800474 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700475
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800476 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700477
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800478cleanup:
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200479 netvsc_revoke_recv_buf(device, net_device, ndev);
480 netvsc_revoke_send_buf(device, net_device, ndev);
481 netvsc_teardown_recv_gpadl(device, net_device, ndev);
482 netvsc_teardown_send_gpadl(device, net_device, ndev);
Hank Janssenfceaf242009-07-13 15:34:54 -0700483
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800484exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700485 return ret;
486}
487
Haiyang Zhangf157e782011-12-15 13:45:16 -0800488/* Negotiate NVSP protocol version */
489static int negotiate_nvsp_ver(struct hv_device *device,
490 struct netvsc_device *net_device,
491 struct nvsp_message *init_packet,
492 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700493{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200494 struct net_device *ndev = hv_get_drvdata(device);
Nicholas Mc Guire7390fe92015-01-25 15:46:31 +0100495 int ret;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800496
497 memset(init_packet, 0, sizeof(struct nvsp_message));
498 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
499 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
500 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
Stephen Hemmingerec966382018-03-16 15:44:28 -0700501 trace_nvsp_send(ndev, init_packet);
502
Haiyang Zhangf157e782011-12-15 13:45:16 -0800503 /* Send the init request */
504 ret = vmbus_sendpacket(device->channel, init_packet,
505 sizeof(struct nvsp_message),
506 (unsigned long)init_packet,
507 VM_PKT_DATA_INBAND,
508 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
509
510 if (ret != 0)
511 return ret;
512
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200513 wait_for_completion(&net_device->channel_init_wait);
Haiyang Zhangf157e782011-12-15 13:45:16 -0800514
515 if (init_packet->msg.init_msg.init_complete.status !=
516 NVSP_STAT_SUCCESS)
517 return -EINVAL;
518
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800519 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800520 return 0;
521
Haiyang Zhang71790a22015-07-24 10:08:40 -0700522 /* NVSPv2 or later: Send NDIS config */
Haiyang Zhangf157e782011-12-15 13:45:16 -0800523 memset(init_packet, 0, sizeof(struct nvsp_message));
524 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200525 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000526 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800527
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700528 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
Haiyang Zhang71790a22015-07-24 10:08:40 -0700529 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
530
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700531 /* Teaming bit is needed to receive link speed updates */
532 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
533 }
534
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +0000535 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_61)
536 init_packet->msg.v2_msg.send_ndis_config.capability.rsc = 1;
537
Stephen Hemmingerec966382018-03-16 15:44:28 -0700538 trace_nvsp_send(ndev, init_packet);
539
Haiyang Zhangf157e782011-12-15 13:45:16 -0800540 ret = vmbus_sendpacket(device->channel, init_packet,
541 sizeof(struct nvsp_message),
542 (unsigned long)init_packet,
543 VM_PKT_DATA_INBAND, 0);
544
545 return ret;
546}
547
stephen hemminger95790832017-06-08 16:21:22 -0700548static int netvsc_connect_vsp(struct hv_device *device,
stephen hemminger8b532792017-08-09 17:46:11 -0700549 struct netvsc_device *net_device,
550 const struct netvsc_device_info *device_info)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800551{
Stephen Hemmingerec966382018-03-16 15:44:28 -0700552 struct net_device *ndev = hv_get_drvdata(device);
Colin Ian King1b17ca02017-09-22 16:50:23 +0100553 static const u32 ver_list[] = {
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700554 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
Haiyang Zhang0dcec222018-04-17 15:31:47 -0700555 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5,
556 NVSP_PROTOCOL_VERSION_6, NVSP_PROTOCOL_VERSION_61
stephen hemminger95790832017-06-08 16:21:22 -0700557 };
558 struct nvsp_message *init_packet;
559 int ndis_version, i, ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700560
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800561 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700562
Haiyang Zhangf157e782011-12-15 13:45:16 -0800563 /* Negotiate the latest NVSP protocol supported */
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700564 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800565 if (negotiate_nvsp_ver(device, net_device, init_packet,
566 ver_list[i]) == 0) {
567 net_device->nvsp_version = ver_list[i];
568 break;
569 }
570
571 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700572 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800573 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700574 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800575
576 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
577
Bill Pemberton454f18a2009-07-27 16:47:24 -0400578 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800579 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700580
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800581 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700582 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800583 else
584 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700585
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800586 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
587 init_packet->msg.v1_msg.
588 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800589 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800590 init_packet->msg.v1_msg.
591 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800592 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700593
Stephen Hemmingerec966382018-03-16 15:44:28 -0700594 trace_nvsp_send(ndev, init_packet);
595
Bill Pemberton454f18a2009-07-27 16:47:24 -0400596 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800597 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800598 sizeof(struct nvsp_message),
599 (unsigned long)init_packet,
600 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700601 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800602 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700603
Haiyang Zhang99d30162014-03-09 16:10:59 -0700604
stephen hemminger8b532792017-08-09 17:46:11 -0700605 ret = netvsc_init_buf(device, net_device, device_info);
Hank Janssenfceaf242009-07-13 15:34:54 -0700606
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800607cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700608 return ret;
609}
610
Hank Janssen3e189512010-03-04 22:11:00 +0000611/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800612 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700613 */
Stephen Hemmingere08f3ea2016-08-23 12:17:50 -0700614void netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700615{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200616 struct net_device *ndev = hv_get_drvdata(device);
617 struct net_device_context *net_device_ctx = netdev_priv(ndev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -0700618 struct netvsc_device *net_device
619 = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger15a863b2017-02-27 10:26:49 -0800620 int i;
Hank Janssenfceaf242009-07-13 15:34:54 -0700621
Mohammed Gamala56d99d2018-04-05 21:09:20 +0200622 /*
623 * Revoke receive buffer. If host is pre-Win2016 then tear down
624 * receive buffer GPADL. Do the same for send buffer.
625 */
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200626 netvsc_revoke_recv_buf(device, net_device, ndev);
Mohammed Gamala56d99d2018-04-05 21:09:20 +0200627 if (vmbus_proto_version < VERSION_WIN10)
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200628 netvsc_teardown_recv_gpadl(device, net_device, ndev);
Mohammed Gamala56d99d2018-04-05 21:09:20 +0200629
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200630 netvsc_revoke_send_buf(device, net_device, ndev);
Mohammed Gamala56d99d2018-04-05 21:09:20 +0200631 if (vmbus_proto_version < VERSION_WIN10)
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200632 netvsc_teardown_send_gpadl(device, net_device, ndev);
Hank Janssenfceaf242009-07-13 15:34:54 -0700633
stephen hemminger545a8e72017-03-22 14:51:00 -0700634 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700635
Stephen Hemminger8348e042018-03-20 15:03:02 -0700636 /* And disassociate NAPI context from device */
637 for (i = 0; i < net_device->num_chn; i++)
638 netif_napi_del(&net_device->chan_table[i].napi);
639
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700640 /*
641 * At this point, no one should be accessing net_device
642 * except in here
643 */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +0100644 netdev_dbg(ndev, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700645
Bill Pemberton454f18a2009-07-27 16:47:24 -0400646 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800647 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700648
Mohammed Gamala56d99d2018-04-05 21:09:20 +0200649 /*
650 * If host is Win2016 or higher then we do the GPADL tear down
651 * here after VMBus is closed.
652 */
Mohammed Gamal79928942018-04-05 21:09:19 +0200653 if (vmbus_proto_version >= VERSION_WIN10) {
Mohammed Gamal3f076eff2018-04-05 21:09:21 +0200654 netvsc_teardown_recv_gpadl(device, net_device, ndev);
655 netvsc_teardown_send_gpadl(device, net_device, ndev);
Mohammed Gamal79928942018-04-05 21:09:19 +0200656 }
stephen hemminger15a863b2017-02-27 10:26:49 -0800657
Bill Pemberton454f18a2009-07-27 16:47:24 -0400658 /* Release all resources */
stephen hemminger545a8e72017-03-22 14:51:00 -0700659 free_netvsc_device_rcu(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700660}
661
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000662#define RING_AVAIL_PERCENT_HIWATER 20
663#define RING_AVAIL_PERCENT_LOWATER 10
664
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700665static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
666 u32 index)
667{
668 sync_change_bit(index, net_device->send_section_map);
669}
670
Stephen Hemmingerc347b922018-04-26 14:34:25 -0700671static void netvsc_send_tx_complete(struct net_device *ndev,
672 struct netvsc_device *net_device,
673 struct vmbus_channel *channel,
stephen hemmingerf9645432017-04-07 14:41:19 -0400674 const struct vmpacket_descriptor *desc,
675 int budget)
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700676{
stephen hemminger50698d82017-02-27 10:26:47 -0800677 struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id;
Simon Xiao09af87d2017-09-29 11:39:46 -0700678 struct net_device_context *ndev_ctx = netdev_priv(ndev);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700679 u16 q_idx = 0;
680 int queue_sends;
681
682 /* Notify the layer above us */
683 if (likely(skb)) {
stephen hemminger793e3952017-01-24 13:06:12 -0800684 const struct hv_netvsc_packet *packet
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700685 = (struct hv_netvsc_packet *)skb->cb;
stephen hemminger793e3952017-01-24 13:06:12 -0800686 u32 send_index = packet->send_buf_index;
687 struct netvsc_stats *tx_stats;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700688
689 if (send_index != NETVSC_INVALID_INDEX)
690 netvsc_free_send_slot(net_device, send_index);
stephen hemminger793e3952017-01-24 13:06:12 -0800691 q_idx = packet->q_idx;
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700692
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800693 tx_stats = &net_device->chan_table[q_idx].tx_stats;
stephen hemminger793e3952017-01-24 13:06:12 -0800694
695 u64_stats_update_begin(&tx_stats->syncp);
696 tx_stats->packets += packet->total_packets;
697 tx_stats->bytes += packet->total_bytes;
698 u64_stats_update_end(&tx_stats->syncp);
699
stephen hemmingerf9645432017-04-07 14:41:19 -0400700 napi_consume_skb(skb, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700701 }
702
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800703 queue_sends =
704 atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700705
Stephen Hemminger7b2ee502018-03-20 15:03:05 -0700706 if (unlikely(net_device->destroy)) {
707 if (queue_sends == 0)
708 wake_up(&net_device->wait_drain);
709 } else {
710 struct netdev_queue *txq = netdev_get_tx_queue(ndev, q_idx);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700711
Haiyang Zhang1b704c42019-03-28 19:40:36 +0000712 if (netif_tx_queue_stopped(txq) && !net_device->tx_disable &&
Long Li6b1f8372018-03-27 17:48:39 -0700713 (hv_get_avail_to_write_percent(&channel->outbound) >
714 RING_AVAIL_PERCENT_HIWATER || queue_sends < 1)) {
Stephen Hemminger7b2ee502018-03-20 15:03:05 -0700715 netif_tx_wake_queue(txq);
716 ndev_ctx->eth_stats.wake_queue++;
717 }
Simon Xiao09af87d2017-09-29 11:39:46 -0700718 }
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700719}
720
Stephen Hemmingerc347b922018-04-26 14:34:25 -0700721static void netvsc_send_completion(struct net_device *ndev,
722 struct netvsc_device *net_device,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800723 struct vmbus_channel *incoming_channel,
stephen hemmingerf9645432017-04-07 14:41:19 -0400724 const struct vmpacket_descriptor *desc,
725 int budget)
Hank Janssenfceaf242009-07-13 15:34:54 -0700726{
Stephen Hemmingerc347b922018-04-26 14:34:25 -0700727 const struct nvsp_message *nvsp_packet = hv_pkt_data(desc);
Hank Janssenfceaf242009-07-13 15:34:54 -0700728
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700729 switch (nvsp_packet->hdr.msg_type) {
730 case NVSP_MSG_TYPE_INIT_COMPLETE:
731 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
732 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
733 case NVSP_MSG5_TYPE_SUBCHANNEL:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400734 /* Copy the response back */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800735 memcpy(&net_device->channel_init_pkt, nvsp_packet,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700736 sizeof(struct nvsp_message));
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700737 complete(&net_device->channel_init_wait);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700738 break;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000739
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700740 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
Stephen Hemmingerc347b922018-04-26 14:34:25 -0700741 netvsc_send_tx_complete(ndev, net_device, incoming_channel,
742 desc, budget);
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700743 break;
Hank Janssenfceaf242009-07-13 15:34:54 -0700744
Stephen Hemmingerbc304dd2016-08-23 12:17:53 -0700745 default:
746 netdev_err(ndev,
747 "Unknown send completion type %d received!!\n",
748 nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700749 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700750}
751
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700752static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
753{
stephen hemmingerb58a1852017-01-24 13:06:14 -0800754 unsigned long *map_addr = net_device->send_section_map;
755 unsigned int i;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700756
stephen hemmingerfdfb70d2017-04-24 18:33:38 -0700757 for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
stephen hemmingerb58a1852017-01-24 13:06:14 -0800758 if (sync_test_and_set_bit(i, map_addr) == 0)
759 return i;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700760 }
stephen hemmingerb58a1852017-01-24 13:06:14 -0800761
762 return NETVSC_INVALID_INDEX;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700763}
764
Stephen Hemminger26a11262017-12-12 16:48:35 -0800765static void netvsc_copy_to_send_buf(struct netvsc_device *net_device,
766 unsigned int section_index,
767 u32 pend_size,
768 struct hv_netvsc_packet *packet,
769 struct rndis_message *rndis_msg,
770 struct hv_page_buffer *pb,
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800771 bool xmit_more)
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700772{
773 char *start = net_device->send_buf;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700774 char *dest = start + (section_index * net_device->send_section_size)
775 + pend_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700776 int i;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700777 u32 padding = 0;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700778 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
779 packet->page_buf_cnt;
Stephen Hemmingerb85e06f2017-12-01 11:01:46 -0800780 u32 remain;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700781
782 /* Add padding */
Stephen Hemmingerb85e06f2017-12-01 11:01:46 -0800783 remain = packet->total_data_buflen & (net_device->pkt_align - 1);
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800784 if (xmit_more && remain) {
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700785 padding = net_device->pkt_align - remain;
KY Srinivasan24476762015-12-01 16:43:06 -0800786 rndis_msg->msg_len += padding;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700787 packet->total_data_buflen += padding;
788 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700789
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700790 for (i = 0; i < page_count; i++) {
stephen hemminger02b6de02017-07-28 08:59:44 -0700791 char *src = phys_to_virt(pb[i].pfn << PAGE_SHIFT);
792 u32 offset = pb[i].offset;
793 u32 len = pb[i].len;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700794
795 memcpy(dest, (src + offset), len);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700796 dest += len;
797 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700798
Stephen Hemminger26a11262017-12-12 16:48:35 -0800799 if (padding)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700800 memset(dest, 0, padding);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700801}
802
Stephen Hemminger3a8963a2016-09-09 12:45:24 -0700803static inline int netvsc_send_pkt(
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200804 struct hv_device *device,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700805 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800806 struct netvsc_device *net_device,
stephen hemminger02b6de02017-07-28 08:59:44 -0700807 struct hv_page_buffer *pb,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800808 struct sk_buff *skb)
Hank Janssenfceaf242009-07-13 15:34:54 -0700809{
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700810 struct nvsp_message nvmsg;
Stephen Hemmingerec966382018-03-16 15:44:28 -0700811 struct nvsp_1_message_send_rndis_packet *rpkt =
Joe Perches956a25c2017-07-31 10:30:54 -0700812 &nvmsg.msg.v1_msg.send_rndis_pkt;
813 struct netvsc_channel * const nvchan =
814 &net_device->chan_table[packet->q_idx];
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800815 struct vmbus_channel *out_channel = nvchan->channel;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200816 struct net_device *ndev = hv_get_drvdata(device);
Simon Xiao09af87d2017-09-29 11:39:46 -0700817 struct net_device_context *ndev_ctx = netdev_priv(ndev);
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800818 struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700819 u64 req_id;
820 int ret;
Long Li6b1f8372018-03-27 17:48:39 -0700821 u32 ring_avail = hv_get_avail_to_write_percent(&out_channel->outbound);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700822
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700823 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
Joe Perches956a25c2017-07-31 10:30:54 -0700824 if (skb)
825 rpkt->channel_type = 0; /* 0 is RMC_DATA */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700826 else
Joe Perches956a25c2017-07-31 10:30:54 -0700827 rpkt->channel_type = 1; /* 1 is RMC_CONTROL */
828
829 rpkt->send_buf_section_index = packet->send_buf_index;
830 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
831 rpkt->send_buf_section_size = 0;
832 else
833 rpkt->send_buf_section_size = packet->total_data_buflen;
Hank Janssenfceaf242009-07-13 15:34:54 -0700834
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800835 req_id = (ulong)skb;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000836
Haiyang Zhangc3582a22014-12-01 13:28:39 -0800837 if (out_channel->rescind)
838 return -ENODEV;
839
Stephen Hemmingerec966382018-03-16 15:44:28 -0700840 trace_nvsp_send_pkt(ndev, out_channel, rpkt);
841
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800842 if (packet->page_buf_cnt) {
stephen hemminger02b6de02017-07-28 08:59:44 -0700843 if (packet->cp_partial)
844 pb += packet->rmsg_pgcnt;
845
stephen hemminger5a668d82017-08-16 08:56:25 -0700846 ret = vmbus_sendpacket_pagebuffer(out_channel,
847 pb, packet->page_buf_cnt,
848 &nvmsg, sizeof(nvmsg),
849 req_id);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700850 } else {
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700851 ret = vmbus_sendpacket(out_channel,
852 &nvmsg, sizeof(nvmsg),
853 req_id, VM_PKT_DATA_INBAND,
854 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700855 }
856
Haiyang Zhang1d068252011-12-02 11:56:25 -0800857 if (ret == 0) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800858 atomic_inc_return(&nvchan->queue_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700859
Simon Xiao09af87d2017-09-29 11:39:46 -0700860 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800861 netif_tx_stop_queue(txq);
Simon Xiao09af87d2017-09-29 11:39:46 -0700862 ndev_ctx->eth_stats.stop_queue++;
863 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800864 } else if (ret == -EAGAIN) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800865 netif_tx_stop_queue(txq);
Simon Xiao09af87d2017-09-29 11:39:46 -0700866 ndev_ctx->eth_stats.stop_queue++;
Haiyang Zhang1d068252011-12-02 11:56:25 -0800867 } else {
stephen hemminger4a2176c2017-07-28 08:59:43 -0700868 netdev_err(ndev,
869 "Unable to send packet pages %u len %u, ret %d\n",
870 packet->page_buf_cnt, packet->total_data_buflen,
871 ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800872 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700873
Haiyang Zhang93aa4792019-04-30 19:29:07 +0000874 if (netif_tx_queue_stopped(txq) &&
875 atomic_read(&nvchan->queue_sends) < 1 &&
876 !net_device->tx_disable) {
877 netif_tx_wake_queue(txq);
878 ndev_ctx->eth_stats.wake_queue++;
879 if (ret == -EAGAIN)
880 ret = -ENOSPC;
881 }
882
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700883 return ret;
884}
885
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800886/* Move packet out of multi send data (msd), and clear msd */
887static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
888 struct sk_buff **msd_skb,
889 struct multi_send_data *msdp)
890{
891 *msd_skb = msdp->skb;
892 *msd_send = msdp->pkt;
893 msdp->skb = NULL;
894 msdp->pkt = NULL;
895 msdp->count = 0;
896}
897
stephen hemminger2a926f72017-07-19 11:53:17 -0700898/* RCU already held by caller */
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800899int netvsc_send(struct net_device *ndev,
KY Srinivasan24476762015-12-01 16:43:06 -0800900 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800901 struct rndis_message *rndis_msg,
stephen hemminger02b6de02017-07-28 08:59:44 -0700902 struct hv_page_buffer *pb,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800903 struct sk_buff *skb)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700904{
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800905 struct net_device_context *ndev_ctx = netdev_priv(ndev);
stephen hemminger39629812017-07-19 11:53:19 -0700906 struct netvsc_device *net_device
stephen hemminger867047c2017-07-28 08:59:42 -0700907 = rcu_dereference_bh(ndev_ctx->nvdev);
stephen hemminger2a926f72017-07-19 11:53:17 -0700908 struct hv_device *device = ndev_ctx->device_ctx;
Stephen Hemminger6c4c1372016-08-23 12:17:55 -0700909 int ret = 0;
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800910 struct netvsc_channel *nvchan;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700911 u32 pktlen = packet->total_data_buflen, msd_len = 0;
912 unsigned int section_index = NETVSC_INVALID_INDEX;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700913 struct multi_send_data *msdp;
914 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800915 struct sk_buff *msd_skb = NULL;
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800916 bool try_batch, xmit_more;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700917
stephen hemminger592b4fe2017-06-08 16:21:23 -0700918 /* If device is rescinded, return error and packet will get dropped. */
stephen hemminger2a926f72017-07-19 11:53:17 -0700919 if (unlikely(!net_device || net_device->destroy))
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700920 return -ENODEV;
921
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800922 nvchan = &net_device->chan_table[packet->q_idx];
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700923 packet->send_buf_index = NETVSC_INVALID_INDEX;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700924 packet->cp_partial = false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700925
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800926 /* Send control message directly without accessing msd (Multi-Send
927 * Data) field which may be changed during data packet processing.
928 */
Stephen Hemminger12f69662018-03-02 13:49:01 -0800929 if (!skb)
930 return netvsc_send_pkt(device, packet, net_device, pb, skb);
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800931
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700932 /* batch packets in send buffer if possible */
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800933 msdp = &nvchan->msd;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700934 if (msdp->pkt)
935 msd_len = msdp->pkt->total_data_buflen;
936
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700937 try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700938 if (try_batch && msd_len + pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700939 net_device->send_section_size) {
940 section_index = msdp->pkt->send_buf_index;
941
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700942 } else if (try_batch && msd_len + packet->rmsg_size <
943 net_device->send_section_size) {
944 section_index = msdp->pkt->send_buf_index;
945 packet->cp_partial = true;
946
stephen hemmingerebc1dcf2017-03-22 14:51:04 -0700947 } else if (pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700948 net_device->send_section_size) {
949 section_index = netvsc_get_next_send_section(net_device);
stephen hemmingercad5c192017-08-09 17:46:12 -0700950 if (unlikely(section_index == NETVSC_INVALID_INDEX)) {
951 ++ndev_ctx->eth_stats.tx_send_full;
952 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800953 move_pkt_msd(&msd_send, &msd_skb, msdp);
954 msd_len = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700955 }
956 }
957
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800958 /* Keep aggregating only if stack says more data is coming
959 * and not doing mixed modes send and not flow blocked
960 */
Florian Westphal6b16f9e2019-04-01 16:42:14 +0200961 xmit_more = netdev_xmit_more() &&
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800962 !packet->cp_partial &&
963 !netif_xmit_stopped(netdev_get_tx_queue(ndev, packet->q_idx));
964
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700965 if (section_index != NETVSC_INVALID_INDEX) {
966 netvsc_copy_to_send_buf(net_device,
967 section_index, msd_len,
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800968 packet, rndis_msg, pb, xmit_more);
KY Srinivasanb08cc792015-03-29 21:08:42 -0700969
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700970 packet->send_buf_index = section_index;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700971
972 if (packet->cp_partial) {
973 packet->page_buf_cnt -= packet->rmsg_pgcnt;
974 packet->total_data_buflen = msd_len + packet->rmsg_size;
975 } else {
976 packet->page_buf_cnt = 0;
977 packet->total_data_buflen += msd_len;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700978 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700979
stephen hemminger793e3952017-01-24 13:06:12 -0800980 if (msdp->pkt) {
981 packet->total_packets += msdp->pkt->total_packets;
982 packet->total_bytes += msdp->pkt->total_bytes;
983 }
984
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800985 if (msdp->skb)
Stephen Hemminger17db4bc2016-09-22 16:56:29 -0700986 dev_consume_skb_any(msdp->skb);
Haiyang Zhangee90b812015-04-06 15:22:54 -0700987
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800988 if (xmit_more) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800989 msdp->skb = skb;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700990 msdp->pkt = packet;
991 msdp->count++;
992 } else {
993 cur_send = packet;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800994 msdp->skb = NULL;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700995 msdp->pkt = NULL;
996 msdp->count = 0;
997 }
998 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800999 move_pkt_msd(&msd_send, &msd_skb, msdp);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001000 cur_send = packet;
1001 }
1002
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001003 if (msd_send) {
Stephen Hemminger6c4c1372016-08-23 12:17:55 -07001004 int m_ret = netvsc_send_pkt(device, msd_send, net_device,
1005 NULL, msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001006
1007 if (m_ret != 0) {
1008 netvsc_free_send_slot(net_device,
1009 msd_send->send_buf_index);
Haiyang Zhangc85e4922016-01-25 09:49:31 -08001010 dev_kfree_skb_any(msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001011 }
1012 }
1013
1014 if (cur_send)
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001015 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001016
Jerry Snitselaar7aab5152015-05-04 10:57:16 -07001017 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
1018 netvsc_free_send_slot(net_device, section_index);
Haiyang Zhangd953ca42015-01-29 12:34:49 -08001019
Hank Janssenfceaf242009-07-13 15:34:54 -07001020 return ret;
1021}
1022
stephen hemminger7426b1a2017-07-28 08:59:45 -07001023/* Send pending recv completions */
stephen hemmingercad5c192017-08-09 17:46:12 -07001024static int send_recv_completions(struct net_device *ndev,
1025 struct netvsc_device *nvdev,
1026 struct netvsc_channel *nvchan)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001027{
stephen hemminger7426b1a2017-07-28 08:59:45 -07001028 struct multi_recv_comp *mrc = &nvchan->mrc;
1029 struct recv_comp_msg {
1030 struct nvsp_message_header hdr;
1031 u32 status;
1032 } __packed;
1033 struct recv_comp_msg msg = {
1034 .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
1035 };
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001036 int ret;
1037
stephen hemminger7426b1a2017-07-28 08:59:45 -07001038 while (mrc->first != mrc->next) {
1039 const struct recv_comp_data *rcd
1040 = mrc->slots + mrc->first;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001041
stephen hemminger7426b1a2017-07-28 08:59:45 -07001042 msg.status = rcd->status;
1043 ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg),
1044 rcd->tid, VM_PKT_COMP, 0);
stephen hemmingercad5c192017-08-09 17:46:12 -07001045 if (unlikely(ret)) {
1046 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1047
1048 ++ndev_ctx->eth_stats.rx_comp_busy;
stephen hemminger7426b1a2017-07-28 08:59:45 -07001049 return ret;
stephen hemmingercad5c192017-08-09 17:46:12 -07001050 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001051
stephen hemminger7426b1a2017-07-28 08:59:45 -07001052 if (++mrc->first == nvdev->recv_completion_cnt)
1053 mrc->first = 0;
1054 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001055
stephen hemminger7426b1a2017-07-28 08:59:45 -07001056 /* receive completion ring has been emptied */
1057 if (unlikely(nvdev->destroy))
1058 wake_up(&nvdev->wait_drain);
1059
1060 return 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001061}
1062
stephen hemminger7426b1a2017-07-28 08:59:45 -07001063/* Count how many receive completions are outstanding */
1064static void recv_comp_slot_avail(const struct netvsc_device *nvdev,
1065 const struct multi_recv_comp *mrc,
1066 u32 *filled, u32 *avail)
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001067{
stephen hemminger7426b1a2017-07-28 08:59:45 -07001068 u32 count = nvdev->recv_completion_cnt;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001069
stephen hemminger7426b1a2017-07-28 08:59:45 -07001070 if (mrc->next >= mrc->first)
1071 *filled = mrc->next - mrc->first;
1072 else
1073 *filled = (count - mrc->first) + mrc->next;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001074
stephen hemminger7426b1a2017-07-28 08:59:45 -07001075 *avail = count - *filled - 1;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001076}
1077
stephen hemminger7426b1a2017-07-28 08:59:45 -07001078/* Add receive complete to ring to send to host. */
1079static void enq_receive_complete(struct net_device *ndev,
1080 struct netvsc_device *nvdev, u16 q_idx,
1081 u64 tid, u32 status)
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001082{
stephen hemminger7426b1a2017-07-28 08:59:45 -07001083 struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx];
1084 struct multi_recv_comp *mrc = &nvchan->mrc;
1085 struct recv_comp_data *rcd;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001086 u32 filled, avail;
1087
stephen hemminger7426b1a2017-07-28 08:59:45 -07001088 recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001089
stephen hemminger7426b1a2017-07-28 08:59:45 -07001090 if (unlikely(filled > NAPI_POLL_WEIGHT)) {
stephen hemmingercad5c192017-08-09 17:46:12 -07001091 send_recv_completions(ndev, nvdev, nvchan);
stephen hemminger7426b1a2017-07-28 08:59:45 -07001092 recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001093 }
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001094
stephen hemminger7426b1a2017-07-28 08:59:45 -07001095 if (unlikely(!avail)) {
1096 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1097 q_idx, tid);
1098 return;
1099 }
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001100
stephen hemminger7426b1a2017-07-28 08:59:45 -07001101 rcd = mrc->slots + mrc->next;
1102 rcd->tid = tid;
1103 rcd->status = status;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001104
stephen hemminger7426b1a2017-07-28 08:59:45 -07001105 if (++mrc->next == nvdev->recv_completion_cnt)
1106 mrc->next = 0;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001107}
1108
stephen hemminger15a863b2017-02-27 10:26:49 -08001109static int netvsc_receive(struct net_device *ndev,
stephen hemminger7426b1a2017-07-28 08:59:45 -07001110 struct netvsc_device *net_device,
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001111 struct netvsc_channel *nvchan,
stephen hemminger7426b1a2017-07-28 08:59:45 -07001112 const struct vmpacket_descriptor *desc,
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001113 const struct nvsp_message *nvsp)
Hank Janssenfceaf242009-07-13 15:34:54 -07001114{
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001115 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001116 struct vmbus_channel *channel = nvchan->channel;
stephen hemmingerf3dd3f42017-02-27 10:26:48 -08001117 const struct vmtransfer_page_packet_header *vmxferpage_packet
1118 = container_of(desc, const struct vmtransfer_page_packet_header, d);
stephen hemminger15a863b2017-02-27 10:26:49 -08001119 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemmingerdc54a082017-01-24 13:06:08 -08001120 char *recv_buf = net_device->recv_buf;
Haiyang Zhang4baab262014-04-21 14:54:43 -07001121 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -08001122 int i;
1123 int count = 0;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -07001124
Bill Pemberton454f18a2009-07-27 16:47:24 -04001125 /* Make sure this is a valid nvsp packet */
stephen hemmingerdc54a082017-01-24 13:06:08 -08001126 if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
1127 netif_err(net_device_ctx, rx_err, ndev,
1128 "Unknown nvsp packet type received %u\n",
1129 nvsp->hdr.msg_type);
stephen hemminger15a863b2017-02-27 10:26:49 -08001130 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001131 }
1132
stephen hemmingerdc54a082017-01-24 13:06:08 -08001133 if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
1134 netif_err(net_device_ctx, rx_err, ndev,
1135 "Invalid xfer page set id - expecting %x got %x\n",
1136 NETVSC_RECEIVE_BUFFER_ID,
1137 vmxferpage_packet->xfer_pageset_id);
stephen hemminger15a863b2017-02-27 10:26:49 -08001138 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -07001139 }
1140
Haiyang Zhang4baab262014-04-21 14:54:43 -07001141 count = vmxferpage_packet->range_cnt;
Hank Janssenfceaf242009-07-13 15:34:54 -07001142
Bill Pemberton454f18a2009-07-27 16:47:24 -04001143 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -07001144 for (i = 0; i < count; i++) {
Haiyang Zhangc5d24bd2018-03-22 12:01:14 -07001145 u32 offset = vmxferpage_packet->ranges[i].byte_offset;
stephen hemmingerdc54a082017-01-24 13:06:08 -08001146 u32 buflen = vmxferpage_packet->ranges[i].byte_count;
Haiyang Zhangc5d24bd2018-03-22 12:01:14 -07001147 void *data;
Haiyang Zhang5c71dad2018-03-22 12:01:13 -07001148 int ret;
Hank Janssenfceaf242009-07-13 15:34:54 -07001149
Haiyang Zhangc5d24bd2018-03-22 12:01:14 -07001150 if (unlikely(offset + buflen > net_device->recv_buf_size)) {
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001151 nvchan->rsc.cnt = 0;
Haiyang Zhangc5d24bd2018-03-22 12:01:14 -07001152 status = NVSP_STAT_FAIL;
1153 netif_err(net_device_ctx, rx_err, ndev,
1154 "Packet offset:%u + len:%u too big\n",
1155 offset, buflen);
1156
1157 continue;
1158 }
1159
1160 data = recv_buf + offset;
1161
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001162 nvchan->rsc.is_last = (i == count - 1);
1163
Stephen Hemmingerec966382018-03-16 15:44:28 -07001164 trace_rndis_recv(ndev, q_idx, data);
1165
Bill Pemberton454f18a2009-07-27 16:47:24 -04001166 /* Pass it to the upper layer */
Haiyang Zhang5c71dad2018-03-22 12:01:13 -07001167 ret = rndis_filter_receive(ndev, net_device,
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001168 nvchan, data, buflen);
Haiyang Zhang5c71dad2018-03-22 12:01:13 -07001169
1170 if (unlikely(ret != NVSP_STAT_SUCCESS))
1171 status = NVSP_STAT_FAIL;
Hank Janssenfceaf242009-07-13 15:34:54 -07001172 }
1173
stephen hemminger7426b1a2017-07-28 08:59:45 -07001174 enq_receive_complete(ndev, net_device, q_idx,
1175 vmxferpage_packet->d.trans_id, status);
stephen hemminger15a863b2017-02-27 10:26:49 -08001176
stephen hemminger15a863b2017-02-27 10:26:49 -08001177 return count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001178}
1179
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001180static void netvsc_send_table(struct net_device *ndev,
Haiyang Zhang71f21952019-11-21 13:33:40 -08001181 const struct nvsp_message *nvmsg,
1182 u32 msglen)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001183{
stephen hemminger7ce10122017-03-09 14:58:29 -08001184 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhang71f21952019-11-21 13:33:40 -08001185 u32 count, offset, *tab;
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001186 int i;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001187
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001188 count = nvmsg->msg.v5_msg.send_table.count;
Haiyang Zhang71f21952019-11-21 13:33:40 -08001189 offset = nvmsg->msg.v5_msg.send_table.offset;
1190
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001191 if (count != VRSS_SEND_TAB_SIZE) {
1192 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1193 return;
1194 }
1195
Haiyang Zhang71f21952019-11-21 13:33:40 -08001196 if (offset > msglen - count * sizeof(u32)) {
1197 netdev_err(ndev, "Received send-table offset too big:%u\n",
1198 offset);
1199 return;
1200 }
1201
1202 tab = (void *)nvmsg + offset;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001203
1204 for (i = 0; i < count; i++)
Haiyang Zhang39e91cf2017-10-13 12:28:04 -07001205 net_device_ctx->tx_table[i] = tab[i];
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001206}
1207
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001208static void netvsc_send_vf(struct net_device *ndev,
1209 const struct nvsp_message *nvmsg)
Haiyang Zhang71790a22015-07-24 10:08:40 -07001210{
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001211 struct net_device_context *net_device_ctx = netdev_priv(ndev);
1212
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001213 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1214 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
Stephen Hemminger00d7ddb2018-09-14 12:54:57 -07001215 netdev_info(ndev, "VF slot %u %s\n",
1216 net_device_ctx->vf_serial,
1217 net_device_ctx->vf_alloc ? "added" : "removed");
Haiyang Zhang71790a22015-07-24 10:08:40 -07001218}
1219
Haiyang Zhang71f21952019-11-21 13:33:40 -08001220static void netvsc_receive_inband(struct net_device *ndev,
1221 const struct nvsp_message *nvmsg,
1222 u32 msglen)
Haiyang Zhang71790a22015-07-24 10:08:40 -07001223{
1224 switch (nvmsg->hdr.msg_type) {
1225 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
Haiyang Zhang71f21952019-11-21 13:33:40 -08001226 netvsc_send_table(ndev, nvmsg, msglen);
Haiyang Zhang71790a22015-07-24 10:08:40 -07001227 break;
1228
1229 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001230 netvsc_send_vf(ndev, nvmsg);
Haiyang Zhang71790a22015-07-24 10:08:40 -07001231 break;
1232 }
1233}
1234
stephen hemminger15a863b2017-02-27 10:26:49 -08001235static int netvsc_process_raw_pkt(struct hv_device *device,
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001236 struct netvsc_channel *nvchan,
stephen hemminger15a863b2017-02-27 10:26:49 -08001237 struct netvsc_device *net_device,
1238 struct net_device *ndev,
stephen hemmingerf9645432017-04-07 14:41:19 -04001239 const struct vmpacket_descriptor *desc,
1240 int budget)
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001241{
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001242 struct vmbus_channel *channel = nvchan->channel;
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001243 const struct nvsp_message *nvmsg = hv_pkt_data(desc);
Haiyang Zhang71f21952019-11-21 13:33:40 -08001244 u32 msglen = hv_pkt_datalen(desc);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001245
Stephen Hemmingerec966382018-03-16 15:44:28 -07001246 trace_nvsp_recv(ndev, channel, nvmsg);
1247
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001248 switch (desc->type) {
1249 case VM_PKT_COMP:
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001250 netvsc_send_completion(ndev, net_device, channel,
stephen hemmingerf9645432017-04-07 14:41:19 -04001251 desc, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001252 break;
1253
1254 case VM_PKT_DATA_USING_XFER_PAGES:
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001255 return netvsc_receive(ndev, net_device, nvchan,
Stephen Hemmingerc347b922018-04-26 14:34:25 -07001256 desc, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001257 break;
1258
1259 case VM_PKT_DATA_INBAND:
Haiyang Zhang71f21952019-11-21 13:33:40 -08001260 netvsc_receive_inband(ndev, nvmsg, msglen);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001261 break;
1262
1263 default:
1264 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001265 desc->type, desc->trans_id);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001266 break;
1267 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001268
1269 return 0;
1270}
1271
1272static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
1273{
1274 struct vmbus_channel *primary = channel->primary_channel;
1275
1276 return primary ? primary->device_obj : channel->device_obj;
1277}
1278
stephen hemminger262b7f12017-03-16 16:12:38 -07001279/* Network processing softirq
1280 * Process data in incoming ring buffer from host
1281 * Stops when ring is empty or budget is met or exceeded.
1282 */
stephen hemminger15a863b2017-02-27 10:26:49 -08001283int netvsc_poll(struct napi_struct *napi, int budget)
1284{
1285 struct netvsc_channel *nvchan
1286 = container_of(napi, struct netvsc_channel, napi);
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001287 struct netvsc_device *net_device = nvchan->net_device;
stephen hemminger15a863b2017-02-27 10:26:49 -08001288 struct vmbus_channel *channel = nvchan->channel;
1289 struct hv_device *device = netvsc_channel_to_device(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001290 struct net_device *ndev = hv_get_drvdata(device);
stephen hemminger15a863b2017-02-27 10:26:49 -08001291 int work_done = 0;
Haiyang Zhang6b81b192018-07-17 17:11:13 +00001292 int ret;
stephen hemminger15a863b2017-02-27 10:26:49 -08001293
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001294 /* If starting a new interval */
1295 if (!nvchan->desc)
1296 nvchan->desc = hv_pkt_iter_first(channel);
stephen hemminger15a863b2017-02-27 10:26:49 -08001297
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001298 while (nvchan->desc && work_done < budget) {
Haiyang Zhangc8e4eff2018-09-21 18:20:35 +00001299 work_done += netvsc_process_raw_pkt(device, nvchan, net_device,
stephen hemmingerf9645432017-04-07 14:41:19 -04001300 ndev, nvchan->desc, budget);
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001301 nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
stephen hemminger15a863b2017-02-27 10:26:49 -08001302 }
stephen hemminger15a863b2017-02-27 10:26:49 -08001303
Haiyang Zhang6b81b192018-07-17 17:11:13 +00001304 /* Send any pending receive completions */
1305 ret = send_recv_completions(ndev, net_device, nvchan);
1306
1307 /* If it did not exhaust NAPI budget this time
1308 * and not doing busy poll
stephen hemmingerf4e40362017-07-28 08:59:47 -07001309 * then re-enable host interrupts
Haiyang Zhang6b81b192018-07-17 17:11:13 +00001310 * and reschedule if ring is not empty
1311 * or sending receive completion failed.
stephen hemminger262b7f12017-03-16 16:12:38 -07001312 */
Haiyang Zhang6b81b192018-07-17 17:11:13 +00001313 if (work_done < budget &&
stephen hemminger15a863b2017-02-27 10:26:49 -08001314 napi_complete_done(napi, work_done) &&
Haiyang Zhang6b81b192018-07-17 17:11:13 +00001315 (ret || hv_end_read(&channel->inbound)) &&
Stephen Hemmingerd64e38a2018-03-02 13:49:05 -08001316 napi_schedule_prep(napi)) {
stephen hemminger7426b1a2017-07-28 08:59:45 -07001317 hv_begin_read(&channel->inbound);
Stephen Hemmingerd64e38a2018-03-02 13:49:05 -08001318 __napi_schedule(napi);
stephen hemminger7426b1a2017-07-28 08:59:45 -07001319 }
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001320
1321 /* Driver may overshoot since multiple packets per descriptor */
1322 return min(work_done, budget);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001323}
1324
stephen hemminger262b7f12017-03-16 16:12:38 -07001325/* Call back when data is available in host ring buffer.
1326 * Processing is deferred until network softirq (NAPI)
1327 */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001328void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001329{
stephen hemminger6de38af2017-03-16 16:12:37 -07001330 struct netvsc_channel *nvchan = context;
stephen hemminger43bf99c2017-07-24 10:57:27 -07001331 struct vmbus_channel *channel = nvchan->channel;
1332 struct hv_ring_buffer_info *rbi = &channel->inbound;
1333
1334 /* preload first vmpacket descriptor */
1335 prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
stephen hemminger0b307eb2017-01-24 13:05:58 -08001336
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001337 if (napi_schedule_prep(&nvchan->napi)) {
Adrian Vladu52d3b492019-01-03 19:43:08 +00001338 /* disable interrupts from host */
stephen hemminger43bf99c2017-07-24 10:57:27 -07001339 hv_begin_read(rbi);
stephen hemminger0d6dd352017-03-09 15:04:14 -08001340
Stephen Hemminger68633ed2018-03-02 13:49:06 -08001341 __napi_schedule_irqoff(&nvchan->napi);
stephen hemmingerf4f1c232017-03-22 14:50:57 -07001342 }
Hank Janssenfceaf242009-07-13 15:34:54 -07001343}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001344
1345/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001346 * netvsc_device_add - Callback when the device belonging to this
1347 * driver is added
1348 */
stephen hemminger9749fed2017-07-19 11:53:16 -07001349struct netvsc_device *netvsc_device_add(struct hv_device *device,
1350 const struct netvsc_device_info *device_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001351{
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001352 int i, ret = 0;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001353 struct netvsc_device *net_device;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001354 struct net_device *ndev = hv_get_drvdata(device);
1355 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001356
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001357 net_device = alloc_net_device();
Dan Carpenterb1c84922014-09-04 14:11:23 +03001358 if (!net_device)
stephen hemminger9749fed2017-07-19 11:53:16 -07001359 return ERR_PTR(-ENOMEM);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001360
Haiyang Zhang6b0cbe32017-10-13 12:28:05 -07001361 for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
1362 net_device_ctx->tx_table[i] = 0;
1363
stephen hemminger15a863b2017-02-27 10:26:49 -08001364 /* Because the device uses NAPI, all the interrupt batching and
1365 * control is done via Net softirq, not the channel handling
1366 */
1367 set_channel_read_mode(device->channel, HV_CALL_ISR);
1368
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001369 /* If we're reopening the device we may have multiple queues, fill the
1370 * chn_table with the default channel to use it before subchannels are
1371 * opened.
1372 * Initialize the channel state before we open;
1373 * we can be interrupted as soon as we open the channel.
1374 */
1375
1376 for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
1377 struct netvsc_channel *nvchan = &net_device->chan_table[i];
1378
1379 nvchan->channel = device->channel;
stephen hemminger35fbbcc2017-07-19 11:53:18 -07001380 nvchan->net_device = net_device;
Florian Fainelli4a0dee12017-08-01 12:11:12 -07001381 u64_stats_init(&nvchan->tx_stats.syncp);
1382 u64_stats_init(&nvchan->rx_stats.syncp);
K. Y. Srinivasanbffb1842017-04-06 14:59:21 -07001383 }
1384
stephen hemminger2be0f262017-05-03 16:59:21 -07001385 /* Enable NAPI handler before init callbacks */
1386 netif_napi_add(ndev, &net_device->chan_table[0].napi,
1387 netvsc_poll, NAPI_POLL_WEIGHT);
1388
Haiyang Zhangb637e022011-04-21 12:30:45 -07001389 /* Open the channel */
Stephen Hemmingera7f99d02017-12-01 11:01:47 -08001390 ret = vmbus_open(device->channel, netvsc_ring_bytes,
1391 netvsc_ring_bytes, NULL, 0,
1392 netvsc_channel_cb, net_device->chan_table);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001393
1394 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001395 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001396 goto cleanup;
1397 }
1398
1399 /* Channel is opened */
Vitaly Kuznetsov93ba2222016-11-28 18:25:44 +01001400 netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001401
stephen hemminger15a863b2017-02-27 10:26:49 -08001402 napi_enable(&net_device->chan_table[0].napi);
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001403
Haiyang Zhangb637e022011-04-21 12:30:45 -07001404 /* Connect with the NetVsp */
stephen hemminger8b532792017-08-09 17:46:11 -07001405 ret = netvsc_connect_vsp(device, net_device, device_info);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001406 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001407 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001408 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001409 goto close;
1410 }
1411
Stephen Hemminger12f69662018-03-02 13:49:01 -08001412 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1413 * populated.
1414 */
1415 rcu_assign_pointer(net_device_ctx->nvdev, net_device);
1416
stephen hemminger9749fed2017-07-19 11:53:16 -07001417 return net_device;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001418
1419close:
stephen hemminger49393342017-07-28 08:59:46 -07001420 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
1421 napi_disable(&net_device->chan_table[0].napi);
stephen hemminger15a863b2017-02-27 10:26:49 -08001422
Haiyang Zhangb637e022011-04-21 12:30:45 -07001423 /* Now, we can close the channel safely */
1424 vmbus_close(device->channel);
1425
1426cleanup:
Stephen Hemmingerfcfb4a02018-03-02 13:49:03 -08001427 netif_napi_del(&net_device->chan_table[0].napi);
stephen hemminger545a8e72017-03-22 14:51:00 -07001428 free_netvsc_device(&net_device->rcu);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001429
stephen hemminger9749fed2017-07-19 11:53:16 -07001430 return ERR_PTR(ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001431}