]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/hv/netvsc.c
Staging: hv: netvsc: Prevent outgoing traffic when netvsc dev is destroyed
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / hv / netvsc.c
CommitLineData
fceaf24a 1/*
fceaf24a
HJ
2 * 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
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
d0e94d17 18 * Haiyang Zhang <haiyangz@microsoft.com>
fceaf24a 19 * Hank Janssen <hjanssen@microsoft.com>
fceaf24a 20 */
eb335bc4
HJ
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
5654e932 23#include <linux/kernel.h>
0c3b7b2f
S
24#include <linux/sched.h>
25#include <linux/wait.h>
0ffa63b0 26#include <linux/mm.h>
b4362c9c 27#include <linux/delay.h>
21a80820 28#include <linux/io.h>
5a0e3ad6 29#include <linux/slab.h>
3f335ea2
S
30
31#include "hyperv.h"
5ca7252a 32#include "hyperv_net.h"
fceaf24a
HJ
33
34
5a71ae30 35static struct netvsc_device *alloc_net_device(struct hv_device *device)
fceaf24a 36{
85799a37 37 struct netvsc_device *net_device;
fceaf24a 38
85799a37
HZ
39 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
40 if (!net_device)
fceaf24a
HJ
41 return NULL;
42
454f18a9 43 /* Set to 2 to allow both inbound and outbound traffic */
509ee387 44 atomic_set(&net_device->refcnt, 2);
fceaf24a 45
c38b9c71 46 net_device->destroy = false;
53d21fdb 47 net_device->dev = device;
ca623ad3 48 device->ext = net_device;
fceaf24a 49
85799a37 50 return net_device;
fceaf24a
HJ
51}
52
454f18a9 53/* Get the net device object iff exists and its refcount > 1 */
5a71ae30 54static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
fceaf24a 55{
85799a37 56 struct netvsc_device *net_device;
fceaf24a 57
ca623ad3 58 net_device = device->ext;
2ef77143
S
59 if (net_device && (atomic_read(&net_device->refcnt) > 1) &&
60 !net_device->destroy)
53d21fdb 61 atomic_inc(&net_device->refcnt);
fceaf24a 62 else
85799a37 63 net_device = NULL;
fceaf24a 64
85799a37 65 return net_device;
fceaf24a
HJ
66}
67
454f18a9 68/* Get the net device object iff exists and its refcount > 0 */
5a71ae30 69static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
fceaf24a 70{
85799a37 71 struct netvsc_device *net_device;
fceaf24a 72
ca623ad3 73 net_device = device->ext;
53d21fdb
HZ
74 if (net_device && atomic_read(&net_device->refcnt))
75 atomic_inc(&net_device->refcnt);
fceaf24a 76 else
85799a37 77 net_device = NULL;
fceaf24a 78
85799a37 79 return net_device;
fceaf24a
HJ
80}
81
5a71ae30 82static void put_net_device(struct hv_device *device)
fceaf24a 83{
85799a37 84 struct netvsc_device *net_device;
fceaf24a 85
ca623ad3 86 net_device = device->ext;
fceaf24a 87
53d21fdb 88 atomic_dec(&net_device->refcnt);
fceaf24a
HJ
89}
90
5a71ae30
HZ
91static struct netvsc_device *release_outbound_net_device(
92 struct hv_device *device)
fceaf24a 93{
85799a37 94 struct netvsc_device *net_device;
fceaf24a 95
ca623ad3 96 net_device = device->ext;
85799a37 97 if (net_device == NULL)
fceaf24a
HJ
98 return NULL;
99
454f18a9 100 /* Busy wait until the ref drop to 2, then set it to 1 */
53d21fdb 101 while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2)
b4362c9c 102 udelay(100);
fceaf24a 103
85799a37 104 return net_device;
fceaf24a
HJ
105}
106
5a71ae30
HZ
107static struct netvsc_device *release_inbound_net_device(
108 struct hv_device *device)
fceaf24a 109{
85799a37 110 struct netvsc_device *net_device;
fceaf24a 111
ca623ad3 112 net_device = device->ext;
85799a37 113 if (net_device == NULL)
fceaf24a
HJ
114 return NULL;
115
454f18a9 116 /* Busy wait until the ref drop to 1, then set it to 0 */
53d21fdb 117 while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
b4362c9c 118 udelay(100);
fceaf24a 119
ca623ad3 120 device->ext = NULL;
85799a37 121 return net_device;
fceaf24a
HJ
122}
123
ec91cd09
HZ
124static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
125{
126 struct nvsp_message *revoke_packet;
127 int ret = 0;
128
129 /*
130 * If we got a section count, it means we received a
131 * SendReceiveBufferComplete msg (ie sent
132 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
133 * to send a revoke msg here
134 */
135 if (net_device->recv_section_cnt) {
136 /* Send the revoke receive buffer */
137 revoke_packet = &net_device->revoke_packet;
138 memset(revoke_packet, 0, sizeof(struct nvsp_message));
139
140 revoke_packet->hdr.msg_type =
141 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
142 revoke_packet->msg.v1_msg.
143 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
144
145 ret = vmbus_sendpacket(net_device->dev->channel,
146 revoke_packet,
147 sizeof(struct nvsp_message),
148 (unsigned long)revoke_packet,
149 VM_PKT_DATA_INBAND, 0);
150 /*
151 * If we failed here, we might as well return and
152 * have a leak rather than continue and a bugchk
153 */
154 if (ret != 0) {
155 dev_err(&net_device->dev->device, "unable to send "
156 "revoke receive buffer to netvsp");
a3e00530 157 return ret;
ec91cd09
HZ
158 }
159 }
160
161 /* Teardown the gpadl on the vsp end */
162 if (net_device->recv_buf_gpadl_handle) {
163 ret = vmbus_teardown_gpadl(net_device->dev->channel,
164 net_device->recv_buf_gpadl_handle);
165
166 /* If we failed here, we might as well return and have a leak
167 * rather than continue and a bugchk
168 */
169 if (ret != 0) {
170 dev_err(&net_device->dev->device,
171 "unable to teardown receive buffer's gpadl");
a3e00530 172 return -ret;
ec91cd09
HZ
173 }
174 net_device->recv_buf_gpadl_handle = 0;
175 }
176
177 if (net_device->recv_buf) {
178 /* Free up the receive buffer */
179 free_pages((unsigned long)net_device->recv_buf,
180 get_order(net_device->recv_buf_size));
181 net_device->recv_buf = NULL;
182 }
183
184 if (net_device->recv_section) {
185 net_device->recv_section_cnt = 0;
186 kfree(net_device->recv_section);
187 net_device->recv_section = NULL;
188 }
189
190 return ret;
191}
192
5a71ae30 193static int netvsc_init_recv_buf(struct hv_device *device)
fceaf24a 194{
21a80820 195 int ret = 0;
35abb21a 196 int t;
85799a37
HZ
197 struct netvsc_device *net_device;
198 struct nvsp_message *init_packet;
fceaf24a 199
5a71ae30 200 net_device = get_outbound_net_device(device);
85799a37 201 if (!net_device) {
eb335bc4 202 dev_err(&device->device, "unable to get net device..."
21a80820 203 "device being destroyed?");
927bc33c 204 return -ENODEV;
fceaf24a 205 }
fceaf24a 206
53d21fdb 207 net_device->recv_buf =
df3493e0
S
208 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
209 get_order(net_device->recv_buf_size));
53d21fdb 210 if (!net_device->recv_buf) {
eb335bc4
HJ
211 dev_err(&device->device, "unable to allocate receive "
212 "buffer of size %d", net_device->recv_buf_size);
927bc33c 213 ret = -ENOMEM;
0c3b7b2f 214 goto cleanup;
fceaf24a 215 }
fceaf24a 216
454f18a9
BP
217 /*
218 * Establish the gpadl handle for this buffer on this
219 * channel. Note: This call uses the vmbus connection rather
220 * than the channel to establish the gpadl handle.
221 */
53d21fdb
HZ
222 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
223 net_device->recv_buf_size,
224 &net_device->recv_buf_gpadl_handle);
21a80820 225 if (ret != 0) {
eb335bc4
HJ
226 dev_err(&device->device,
227 "unable to establish receive buffer's gpadl");
0c3b7b2f 228 goto cleanup;
fceaf24a
HJ
229 }
230
fceaf24a 231
454f18a9 232 /* Notify the NetVsp of the gpadl handle */
53d21fdb 233 init_packet = &net_device->channel_init_pkt;
fceaf24a 234
85799a37 235 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 236
53d21fdb
HZ
237 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
238 init_packet->msg.v1_msg.send_recv_buf.
239 gpadl_handle = net_device->recv_buf_gpadl_handle;
240 init_packet->msg.v1_msg.
241 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
fceaf24a 242
454f18a9 243 /* Send the gpadl notification request */
85799a37 244 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 245 sizeof(struct nvsp_message),
85799a37 246 (unsigned long)init_packet,
415f2287 247 VM_PKT_DATA_INBAND,
5a4df290 248 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 249 if (ret != 0) {
eb335bc4
HJ
250 dev_err(&device->device,
251 "unable to send receive buffer's gpadl to netvsp");
0c3b7b2f 252 goto cleanup;
fceaf24a
HJ
253 }
254
5c5781b3 255 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
35abb21a 256 BUG_ON(t == 0);
0c3b7b2f 257
fceaf24a 258
454f18a9 259 /* Check the response */
53d21fdb
HZ
260 if (init_packet->msg.v1_msg.
261 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
eb335bc4 262 dev_err(&device->device, "Unable to complete receive buffer "
21a80820 263 "initialzation with NetVsp - status %d",
53d21fdb
HZ
264 init_packet->msg.v1_msg.
265 send_recv_buf_complete.status);
927bc33c 266 ret = -EINVAL;
0c3b7b2f 267 goto cleanup;
fceaf24a
HJ
268 }
269
454f18a9 270 /* Parse the response */
fceaf24a 271
53d21fdb
HZ
272 net_device->recv_section_cnt = init_packet->msg.
273 v1_msg.send_recv_buf_complete.num_sections;
fceaf24a 274
53d21fdb 275 net_device->recv_section = kmalloc(net_device->recv_section_cnt
85799a37 276 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
53d21fdb 277 if (net_device->recv_section == NULL) {
927bc33c 278 ret = -EINVAL;
0c3b7b2f 279 goto cleanup;
fceaf24a
HJ
280 }
281
53d21fdb
HZ
282 memcpy(net_device->recv_section,
283 init_packet->msg.v1_msg.
284 send_recv_buf_complete.sections,
285 net_device->recv_section_cnt *
85799a37 286 sizeof(struct nvsp_1_receive_buffer_section));
fceaf24a 287
21a80820
GKH
288 /*
289 * For 1st release, there should only be 1 section that represents the
290 * entire receive buffer
291 */
53d21fdb
HZ
292 if (net_device->recv_section_cnt != 1 ||
293 net_device->recv_section->offset != 0) {
927bc33c 294 ret = -EINVAL;
0c3b7b2f 295 goto cleanup;
fceaf24a
HJ
296 }
297
0c3b7b2f 298 goto exit;
fceaf24a 299
0c3b7b2f 300cleanup:
5a71ae30 301 netvsc_destroy_recv_buf(net_device);
fceaf24a 302
0c3b7b2f 303exit:
5a71ae30 304 put_net_device(device);
fceaf24a
HJ
305 return ret;
306}
307
fceaf24a 308
5a71ae30 309static int netvsc_connect_vsp(struct hv_device *device)
fceaf24a 310{
35abb21a 311 int ret, t;
85799a37
HZ
312 struct netvsc_device *net_device;
313 struct nvsp_message *init_packet;
314 int ndis_version;
fceaf24a 315
5a71ae30 316 net_device = get_outbound_net_device(device);
85799a37 317 if (!net_device) {
eb335bc4 318 dev_err(&device->device, "unable to get net device..."
21a80820 319 "device being destroyed?");
0f48c72c 320 return -ENODEV;
fceaf24a
HJ
321 }
322
53d21fdb 323 init_packet = &net_device->channel_init_pkt;
fceaf24a 324
85799a37 325 memset(init_packet, 0, sizeof(struct nvsp_message));
53d21fdb
HZ
326 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
327 init_packet->msg.init_msg.init.min_protocol_ver =
85799a37 328 NVSP_MIN_PROTOCOL_VERSION;
53d21fdb 329 init_packet->msg.init_msg.init.max_protocol_ver =
85799a37 330 NVSP_MAX_PROTOCOL_VERSION;
fceaf24a 331
454f18a9 332 /* Send the init request */
85799a37 333 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 334 sizeof(struct nvsp_message),
85799a37 335 (unsigned long)init_packet,
415f2287 336 VM_PKT_DATA_INBAND,
5a4df290 337 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 338
b8a3d52b 339 if (ret != 0)
0c3b7b2f 340 goto cleanup;
fceaf24a 341
5c5781b3 342 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
35abb21a
S
343
344 if (t == 0) {
0c3b7b2f
S
345 ret = -ETIMEDOUT;
346 goto cleanup;
347 }
fceaf24a 348
53d21fdb
HZ
349 if (init_packet->msg.init_msg.init_complete.status !=
350 NVSP_STAT_SUCCESS) {
0f48c72c 351 ret = -EINVAL;
0c3b7b2f 352 goto cleanup;
fceaf24a
HJ
353 }
354
53d21fdb
HZ
355 if (init_packet->msg.init_msg.init_complete.
356 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
0f48c72c 357 ret = -EPROTO;
0c3b7b2f 358 goto cleanup;
fceaf24a 359 }
454f18a9 360 /* Send the ndis version */
85799a37 361 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 362
85799a37 363 ndis_version = 0x00050000;
fceaf24a 364
53d21fdb
HZ
365 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
366 init_packet->msg.v1_msg.
367 send_ndis_ver.ndis_major_ver =
85799a37 368 (ndis_version & 0xFFFF0000) >> 16;
53d21fdb
HZ
369 init_packet->msg.v1_msg.
370 send_ndis_ver.ndis_minor_ver =
85799a37 371 ndis_version & 0xFFFF;
fceaf24a 372
454f18a9 373 /* Send the init request */
85799a37 374 ret = vmbus_sendpacket(device->channel, init_packet,
0c3b7b2f
S
375 sizeof(struct nvsp_message),
376 (unsigned long)init_packet,
377 VM_PKT_DATA_INBAND, 0);
0f48c72c 378 if (ret != 0)
0c3b7b2f 379 goto cleanup;
454f18a9
BP
380
381 /* Post the big receive buffer to NetVSP */
5a71ae30 382 ret = netvsc_init_recv_buf(device);
fceaf24a 383
0c3b7b2f 384cleanup:
5a71ae30 385 put_net_device(device);
fceaf24a
HJ
386 return ret;
387}
388
648dc598 389static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
fceaf24a 390{
5a71ae30 391 netvsc_destroy_recv_buf(net_device);
fceaf24a
HJ
392}
393
3e189519 394/*
5a71ae30 395 * netvsc_device_remove - Callback when the root bus device is removed
21a80820 396 */
905620d1 397int netvsc_device_remove(struct hv_device *device)
fceaf24a 398{
85799a37
HZ
399 struct netvsc_device *net_device;
400 struct hv_netvsc_packet *netvsc_packet, *pos;
c38b9c71 401 unsigned long flags;
fceaf24a 402
454f18a9 403 /* Stop outbound traffic ie sends and receives completions */
5a71ae30 404 net_device = release_outbound_net_device(device);
85799a37 405 if (!net_device) {
eb335bc4 406 dev_err(&device->device, "No net device present!!");
ff2bd69a 407 return -ENODEV;
fceaf24a
HJ
408 }
409
c38b9c71
S
410 spin_lock_irqsave(&device->channel->inbound_lock, flags);
411 net_device->destroy = true;
412 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
413
454f18a9 414 /* Wait for all send completions */
53d21fdb 415 while (atomic_read(&net_device->num_outstanding_sends)) {
eb335bc4
HJ
416 dev_err(&device->device,
417 "waiting for %d requests to complete...",
418 atomic_read(&net_device->num_outstanding_sends));
b4362c9c 419 udelay(100);
fceaf24a
HJ
420 }
421
648dc598 422 netvsc_disconnect_vsp(net_device);
fceaf24a 423
454f18a9 424 /* Stop inbound traffic ie receives and sends completions */
5a71ae30 425 net_device = release_inbound_net_device(device);
fceaf24a 426
454f18a9 427 /* At this point, no one should be accessing netDevice except in here */
eb335bc4 428 dev_notice(&device->device, "net device safe to remove");
fceaf24a 429
454f18a9 430 /* Now, we can close the channel safely */
85799a37 431 vmbus_close(device->channel);
fceaf24a 432
454f18a9 433 /* Release all resources */
85799a37 434 list_for_each_entry_safe(netvsc_packet, pos,
53d21fdb 435 &net_device->recv_pkt_list, list_ent) {
72a2f5bd 436 list_del(&netvsc_packet->list_ent);
85799a37 437 kfree(netvsc_packet);
fceaf24a
HJ
438 }
439
356c4657 440 kfree(net_device);
21a80820 441 return 0;
fceaf24a
HJ
442}
443
5a71ae30 444static void netvsc_send_completion(struct hv_device *device,
85799a37 445 struct vmpacket_descriptor *packet)
fceaf24a 446{
85799a37
HZ
447 struct netvsc_device *net_device;
448 struct nvsp_message *nvsp_packet;
449 struct hv_netvsc_packet *nvsc_packet;
fceaf24a 450
5a71ae30 451 net_device = get_inbound_net_device(device);
85799a37 452 if (!net_device) {
eb335bc4 453 dev_err(&device->device, "unable to get net device..."
21a80820 454 "device being destroyed?");
fceaf24a
HJ
455 return;
456 }
457
85799a37 458 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
415f2287 459 (packet->offset8 << 3));
fceaf24a 460
53d21fdb
HZ
461 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
462 (nvsp_packet->hdr.msg_type ==
463 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
464 (nvsp_packet->hdr.msg_type ==
465 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
454f18a9 466 /* Copy the response back */
53d21fdb 467 memcpy(&net_device->channel_init_pkt, nvsp_packet,
21a80820 468 sizeof(struct nvsp_message));
35abb21a 469 complete(&net_device->channel_init_wait);
53d21fdb
HZ
470 } else if (nvsp_packet->hdr.msg_type ==
471 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
454f18a9 472 /* Get the send context */
85799a37 473 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
415f2287 474 packet->trans_id;
fceaf24a 475
454f18a9 476 /* Notify the layer above us */
72a2f5bd
HZ
477 nvsc_packet->completion.send.send_completion(
478 nvsc_packet->completion.send.send_completion_ctx);
fceaf24a 479
53d21fdb 480 atomic_dec(&net_device->num_outstanding_sends);
21a80820 481 } else {
eb335bc4 482 dev_err(&device->device, "Unknown send completion packet type- "
53d21fdb 483 "%d received!!", nvsp_packet->hdr.msg_type);
fceaf24a
HJ
484 }
485
5a71ae30 486 put_net_device(device);
fceaf24a
HJ
487}
488
f9819f05 489int netvsc_send(struct hv_device *device,
85799a37 490 struct hv_netvsc_packet *packet)
fceaf24a 491{
85799a37 492 struct netvsc_device *net_device;
21a80820 493 int ret = 0;
fceaf24a 494
223c1aa6 495 struct nvsp_message sendMessage;
fceaf24a 496
5a71ae30 497 net_device = get_outbound_net_device(device);
85799a37 498 if (!net_device) {
eb335bc4 499 dev_err(&device->device, "net device (%p) shutting down..."
85799a37 500 "ignoring outbound packets", net_device);
ff2bd69a 501 return -ENODEV;
fceaf24a
HJ
502 }
503
53d21fdb 504 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
72a2f5bd 505 if (packet->is_data_pkt) {
21a80820 506 /* 0 is RMC_DATA; */
53d21fdb 507 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
21a80820
GKH
508 } else {
509 /* 1 is RMC_CONTROL; */
53d21fdb 510 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
21a80820 511 }
fceaf24a 512
454f18a9 513 /* Not using send buffer section */
53d21fdb
HZ
514 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
515 0xFFFFFFFF;
516 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
21a80820 517
72a2f5bd 518 if (packet->page_buf_cnt) {
85799a37 519 ret = vmbus_sendpacket_pagebuffer(device->channel,
72a2f5bd
HZ
520 packet->page_buf,
521 packet->page_buf_cnt,
ff3f8eec
GKH
522 &sendMessage,
523 sizeof(struct nvsp_message),
85799a37 524 (unsigned long)packet);
21a80820 525 } else {
85799a37 526 ret = vmbus_sendpacket(device->channel, &sendMessage,
e4d59ac5
HZ
527 sizeof(struct nvsp_message),
528 (unsigned long)packet,
529 VM_PKT_DATA_INBAND,
530 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
fceaf24a
HJ
531
532 }
533
534 if (ret != 0)
eb335bc4 535 dev_err(&device->device, "Unable to send packet %p ret %d",
85799a37 536 packet, ret);
fceaf24a 537
53d21fdb 538 atomic_inc(&net_device->num_outstanding_sends);
5a71ae30 539 put_net_device(device);
fceaf24a
HJ
540 return ret;
541}
542
5fa9d3c5
HZ
543static void netvsc_send_recv_completion(struct hv_device *device,
544 u64 transaction_id)
545{
546 struct nvsp_message recvcompMessage;
547 int retries = 0;
548 int ret;
549
550 recvcompMessage.hdr.msg_type =
551 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
552
553 /* FIXME: Pass in the status */
554 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
555 NVSP_STAT_SUCCESS;
556
557retry_send_cmplt:
558 /* Send the completion */
559 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
560 sizeof(struct nvsp_message), transaction_id,
561 VM_PKT_COMP, 0);
562 if (ret == 0) {
563 /* success */
564 /* no-op */
d2598f01 565 } else if (ret == -EAGAIN) {
5fa9d3c5
HZ
566 /* no more room...wait a bit and attempt to retry 3 times */
567 retries++;
568 dev_err(&device->device, "unable to send receive completion pkt"
569 " (tid %llx)...retrying %d", transaction_id, retries);
570
571 if (retries < 4) {
572 udelay(100);
573 goto retry_send_cmplt;
574 } else {
575 dev_err(&device->device, "unable to send receive "
576 "completion pkt (tid %llx)...give up retrying",
577 transaction_id);
578 }
579 } else {
580 dev_err(&device->device, "unable to send receive "
581 "completion pkt - %llx", transaction_id);
582 }
583}
584
57991156
HZ
585/* Send a receive completion packet to RNDIS device (ie NetVsp) */
586static void netvsc_receive_completion(void *context)
587{
588 struct hv_netvsc_packet *packet = context;
589 struct hv_device *device = (struct hv_device *)packet->device;
590 struct netvsc_device *net_device;
591 u64 transaction_id = 0;
592 bool fsend_receive_comp = false;
593 unsigned long flags;
594
595 /*
596 * Even though it seems logical to do a GetOutboundNetDevice() here to
597 * send out receive completion, we are using GetInboundNetDevice()
598 * since we may have disable outbound traffic already.
599 */
600 net_device = get_inbound_net_device(device);
601 if (!net_device) {
602 dev_err(&device->device, "unable to get net device..."
603 "device being destroyed?");
604 return;
605 }
606
607 /* Overloading use of the lock. */
608 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
609
610 packet->xfer_page_pkt->count--;
611
612 /*
613 * Last one in the line that represent 1 xfer page packet.
614 * Return the xfer page packet itself to the freelist
615 */
616 if (packet->xfer_page_pkt->count == 0) {
617 fsend_receive_comp = true;
618 transaction_id = packet->completion.recv.recv_completion_tid;
619 list_add_tail(&packet->xfer_page_pkt->list_ent,
620 &net_device->recv_pkt_list);
621
622 }
623
624 /* Put the packet back */
625 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
626 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
627
628 /* Send a receive completion for the xfer page packet */
629 if (fsend_receive_comp)
630 netvsc_send_recv_completion(device, transaction_id);
631
632 put_net_device(device);
633}
634
5a71ae30 635static void netvsc_receive(struct hv_device *device,
85799a37 636 struct vmpacket_descriptor *packet)
fceaf24a 637{
85799a37
HZ
638 struct netvsc_device *net_device;
639 struct vmtransfer_page_packet_header *vmxferpage_packet;
640 struct nvsp_message *nvsp_packet;
641 struct hv_netvsc_packet *netvsc_packet = NULL;
c4b0bc94 642 unsigned long start;
85799a37 643 unsigned long end, end_virtual;
7e23a6e9 644 /* struct netvsc_driver *netvscDriver; */
85799a37 645 struct xferpage_packet *xferpage_packet = NULL;
21a80820 646 int i, j;
85799a37 647 int count = 0, bytes_remain = 0;
6436873a 648 unsigned long flags;
779b4d17 649
d29274ef 650 LIST_HEAD(listHead);
fceaf24a 651
5a71ae30 652 net_device = get_inbound_net_device(device);
85799a37 653 if (!net_device) {
eb335bc4 654 dev_err(&device->device, "unable to get net device..."
21a80820 655 "device being destroyed?");
fceaf24a
HJ
656 return;
657 }
658
21a80820
GKH
659 /*
660 * All inbound packets other than send completion should be xfer page
661 * packet
662 */
415f2287 663 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
eb335bc4 664 dev_err(&device->device, "Unknown packet type received - %d",
415f2287 665 packet->type);
5a71ae30 666 put_net_device(device);
fceaf24a
HJ
667 return;
668 }
669
85799a37 670 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
415f2287 671 (packet->offset8 << 3));
fceaf24a 672
454f18a9 673 /* Make sure this is a valid nvsp packet */
53d21fdb
HZ
674 if (nvsp_packet->hdr.msg_type !=
675 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
eb335bc4
HJ
676 dev_err(&device->device, "Unknown nvsp packet type received-"
677 " %d", nvsp_packet->hdr.msg_type);
5a71ae30 678 put_net_device(device);
fceaf24a
HJ
679 return;
680 }
681
85799a37 682 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
fceaf24a 683
415f2287 684 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
eb335bc4 685 dev_err(&device->device, "Invalid xfer page set id - "
21a80820 686 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
415f2287 687 vmxferpage_packet->xfer_pageset_id);
5a71ae30 688 put_net_device(device);
fceaf24a
HJ
689 return;
690 }
691
454f18a9
BP
692 /*
693 * Grab free packets (range count + 1) to represent this xfer
694 * page packet. +1 to represent the xfer page packet itself.
695 * We grab it here so that we know exactly how many we can
696 * fulfil
697 */
53d21fdb
HZ
698 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
699 while (!list_empty(&net_device->recv_pkt_list)) {
700 list_move_tail(net_device->recv_pkt_list.next, &listHead);
415f2287 701 if (++count == vmxferpage_packet->range_cnt + 1)
fceaf24a
HJ
702 break;
703 }
53d21fdb 704 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
fceaf24a 705
454f18a9
BP
706 /*
707 * We need at least 2 netvsc pkts (1 to represent the xfer
708 * page and at least 1 for the range) i.e. we can handled
709 * some of the xfer page packet ranges...
710 */
21a80820 711 if (count < 2) {
eb335bc4
HJ
712 dev_err(&device->device, "Got only %d netvsc pkt...needed "
713 "%d pkts. Dropping this xfer page packet completely!",
714 count, vmxferpage_packet->range_cnt + 1);
fceaf24a 715
454f18a9 716 /* Return it to the freelist */
53d21fdb 717 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
21a80820 718 for (i = count; i != 0; i--) {
92ec0893 719 list_move_tail(listHead.next,
53d21fdb 720 &net_device->recv_pkt_list);
fceaf24a 721 }
53d21fdb 722 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
21a80820 723 flags);
fceaf24a 724
5a71ae30 725 netvsc_send_recv_completion(device,
415f2287 726 vmxferpage_packet->d.trans_id);
fceaf24a 727
5a71ae30 728 put_net_device(device);
fceaf24a
HJ
729 return;
730 }
731
454f18a9 732 /* Remove the 1st packet to represent the xfer page packet itself */
85799a37 733 xferpage_packet = (struct xferpage_packet *)listHead.next;
72a2f5bd 734 list_del(&xferpage_packet->list_ent);
d29274ef 735
21a80820 736 /* This is how much we can satisfy */
72a2f5bd 737 xferpage_packet->count = count - 1;
21a80820 738
415f2287 739 if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
eb335bc4
HJ
740 dev_err(&device->device, "Needed %d netvsc pkts to satisy "
741 "this xfer page...got %d",
742 vmxferpage_packet->range_cnt, xferpage_packet->count);
fceaf24a
HJ
743 }
744
454f18a9 745 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
21a80820 746 for (i = 0; i < (count - 1); i++) {
85799a37 747 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
72a2f5bd 748 list_del(&netvsc_packet->list_ent);
fceaf24a 749
454f18a9 750 /* Initialize the netvsc packet */
72a2f5bd
HZ
751 netvsc_packet->xfer_page_pkt = xferpage_packet;
752 netvsc_packet->completion.recv.recv_completion =
5a71ae30 753 netvsc_receive_completion;
72a2f5bd 754 netvsc_packet->completion.recv.recv_completion_ctx =
85799a37 755 netvsc_packet;
72a2f5bd 756 netvsc_packet->device = device;
21a80820 757 /* Save this so that we can send it back */
72a2f5bd 758 netvsc_packet->completion.recv.recv_completion_tid =
415f2287 759 vmxferpage_packet->d.trans_id;
fceaf24a 760
72a2f5bd 761 netvsc_packet->total_data_buflen =
415f2287 762 vmxferpage_packet->ranges[i].byte_count;
72a2f5bd 763 netvsc_packet->page_buf_cnt = 1;
fceaf24a 764
ca623ad3 765 netvsc_packet->page_buf[0].len =
415f2287 766 vmxferpage_packet->ranges[i].byte_count;
fceaf24a 767
85799a37 768 start = virt_to_phys((void *)((unsigned long)net_device->
415f2287 769 recv_buf + vmxferpage_packet->ranges[i].byte_offset));
fceaf24a 770
ca623ad3 771 netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
53d21fdb 772 end_virtual = (unsigned long)net_device->recv_buf
415f2287
HZ
773 + vmxferpage_packet->ranges[i].byte_offset
774 + vmxferpage_packet->ranges[i].byte_count - 1;
85799a37 775 end = virt_to_phys((void *)end_virtual);
fceaf24a 776
454f18a9 777 /* Calculate the page relative offset */
ca623ad3 778 netvsc_packet->page_buf[0].offset =
415f2287 779 vmxferpage_packet->ranges[i].byte_offset &
85799a37 780 (PAGE_SIZE - 1);
21a80820
GKH
781 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
782 /* Handle frame across multiple pages: */
ca623ad3
HZ
783 netvsc_packet->page_buf[0].len =
784 (netvsc_packet->page_buf[0].pfn <<
85799a37 785 PAGE_SHIFT)
21a80820 786 + PAGE_SIZE - start;
72a2f5bd 787 bytes_remain = netvsc_packet->total_data_buflen -
ca623ad3 788 netvsc_packet->page_buf[0].len;
21a80820 789 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
ca623ad3 790 netvsc_packet->page_buf[j].offset = 0;
85799a37 791 if (bytes_remain <= PAGE_SIZE) {
ca623ad3 792 netvsc_packet->page_buf[j].len =
85799a37
HZ
793 bytes_remain;
794 bytes_remain = 0;
21a80820 795 } else {
ca623ad3 796 netvsc_packet->page_buf[j].len =
85799a37
HZ
797 PAGE_SIZE;
798 bytes_remain -= PAGE_SIZE;
21a80820 799 }
ca623ad3 800 netvsc_packet->page_buf[j].pfn =
85799a37
HZ
801 virt_to_phys((void *)(end_virtual -
802 bytes_remain)) >> PAGE_SHIFT;
72a2f5bd 803 netvsc_packet->page_buf_cnt++;
85799a37 804 if (bytes_remain == 0)
21a80820 805 break;
fceaf24a 806 }
fceaf24a 807 }
fceaf24a 808
454f18a9 809 /* Pass it to the upper layer */
ac6f7859 810 rndis_filter_receive(device, netvsc_packet);
fceaf24a 811
5a71ae30 812 netvsc_receive_completion(netvsc_packet->
72a2f5bd 813 completion.recv.recv_completion_ctx);
fceaf24a
HJ
814 }
815
5a71ae30 816 put_net_device(device);
fceaf24a
HJ
817}
818
5a71ae30 819static void netvsc_channel_cb(void *context)
fceaf24a 820{
21a80820 821 int ret;
85799a37
HZ
822 struct hv_device *device = context;
823 struct netvsc_device *net_device;
824 u32 bytes_recvd;
825 u64 request_id;
c6fcf0ba 826 unsigned char *packet;
8dc0a06a 827 struct vmpacket_descriptor *desc;
c6fcf0ba
BP
828 unsigned char *buffer;
829 int bufferlen = NETVSC_PACKET_SIZE;
fceaf24a 830
c6fcf0ba 831 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
d70c6731 832 GFP_ATOMIC);
c6fcf0ba
BP
833 if (!packet)
834 return;
835 buffer = packet;
836
5a71ae30 837 net_device = get_inbound_net_device(device);
85799a37 838 if (!net_device) {
eb335bc4 839 dev_err(&device->device, "net device (%p) shutting down..."
85799a37 840 "ignoring inbound packets", net_device);
c6fcf0ba 841 goto out;
fceaf24a
HJ
842 }
843
21a80820 844 do {
9f630068 845 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
85799a37 846 &bytes_recvd, &request_id);
21a80820 847 if (ret == 0) {
85799a37 848 if (bytes_recvd > 0) {
21a80820 849 desc = (struct vmpacket_descriptor *)buffer;
415f2287
HZ
850 switch (desc->type) {
851 case VM_PKT_COMP:
5a71ae30 852 netvsc_send_completion(device, desc);
21a80820
GKH
853 break;
854
415f2287 855 case VM_PKT_DATA_USING_XFER_PAGES:
5a71ae30 856 netvsc_receive(device, desc);
21a80820
GKH
857 break;
858
859 default:
eb335bc4 860 dev_err(&device->device,
21a80820
GKH
861 "unhandled packet type %d, "
862 "tid %llx len %d\n",
415f2287 863 desc->type, request_id,
85799a37 864 bytes_recvd);
21a80820 865 break;
fceaf24a
HJ
866 }
867
454f18a9 868 /* reset */
c6fcf0ba 869 if (bufferlen > NETVSC_PACKET_SIZE) {
8c69f52a 870 kfree(buffer);
fceaf24a 871 buffer = packet;
c6fcf0ba 872 bufferlen = NETVSC_PACKET_SIZE;
fceaf24a 873 }
21a80820 874 } else {
454f18a9 875 /* reset */
c6fcf0ba 876 if (bufferlen > NETVSC_PACKET_SIZE) {
8c69f52a 877 kfree(buffer);
fceaf24a 878 buffer = packet;
c6fcf0ba 879 bufferlen = NETVSC_PACKET_SIZE;
fceaf24a
HJ
880 }
881
882 break;
883 }
3d5cad97 884 } else if (ret == -ENOBUFS) {
21a80820 885 /* Handle large packet */
85799a37 886 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
21a80820 887 if (buffer == NULL) {
454f18a9 888 /* Try again next time around */
eb335bc4 889 dev_err(&device->device,
21a80820 890 "unable to allocate buffer of size "
85799a37 891 "(%d)!!", bytes_recvd);
fceaf24a
HJ
892 break;
893 }
894
85799a37 895 bufferlen = bytes_recvd;
fceaf24a
HJ
896 }
897 } while (1);
898
5a71ae30 899 put_net_device(device);
c6fcf0ba
BP
900out:
901 kfree(buffer);
fceaf24a
HJ
902 return;
903}
af24ce42 904
b637e023
HZ
905/*
906 * netvsc_device_add - Callback when the device belonging to this
907 * driver is added
908 */
7bd23a4d 909int netvsc_device_add(struct hv_device *device, void *additional_info)
b637e023
HZ
910{
911 int ret = 0;
912 int i;
aae23986
S
913 int ring_size =
914 ((struct netvsc_device_info *)additional_info)->ring_size;
b637e023
HZ
915 struct netvsc_device *net_device;
916 struct hv_netvsc_packet *packet, *pos;
b637e023
HZ
917
918 net_device = alloc_net_device(device);
919 if (!net_device) {
ace163a8 920 ret = -ENOMEM;
b637e023
HZ
921 goto cleanup;
922 }
923
924 /* Initialize the NetVSC channel extension */
925 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
926 spin_lock_init(&net_device->recv_pkt_list_lock);
927
b637e023
HZ
928 INIT_LIST_HEAD(&net_device->recv_pkt_list);
929
930 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
931 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
932 (NETVSC_RECEIVE_SG_COUNT *
933 sizeof(struct hv_page_buffer)), GFP_KERNEL);
934 if (!packet)
935 break;
936
937 list_add_tail(&packet->list_ent,
938 &net_device->recv_pkt_list);
939 }
35abb21a 940 init_completion(&net_device->channel_init_wait);
b637e023
HZ
941
942 /* Open the channel */
aae23986
S
943 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
944 ring_size * PAGE_SIZE, NULL, 0,
b637e023
HZ
945 netvsc_channel_cb, device);
946
947 if (ret != 0) {
948 dev_err(&device->device, "unable to open channel: %d", ret);
b637e023
HZ
949 goto cleanup;
950 }
951
952 /* Channel is opened */
953 pr_info("hv_netvsc channel opened successfully");
954
955 /* Connect with the NetVsp */
956 ret = netvsc_connect_vsp(device);
957 if (ret != 0) {
958 dev_err(&device->device,
959 "unable to connect to NetVSP - %d", ret);
b637e023
HZ
960 goto close;
961 }
962
963 return ret;
964
965close:
966 /* Now, we can close the channel safely */
967 vmbus_close(device->channel);
968
969cleanup:
970
971 if (net_device) {
972 list_for_each_entry_safe(packet, pos,
973 &net_device->recv_pkt_list,
974 list_ent) {
975 list_del(&packet->list_ent);
976 kfree(packet);
977 }
978
979 release_outbound_net_device(device);
980 release_inbound_net_device(device);
981
356c4657 982 kfree(net_device);
b637e023
HZ
983 }
984
985 return ret;
986}