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