Staging: hv: Use native page allocation/free functions
[deliverable/linux.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 */
5654e932 21#include <linux/kernel.h>
0ffa63b0 22#include <linux/mm.h>
b4362c9c 23#include <linux/delay.h>
21a80820 24#include <linux/io.h>
5a0e3ad6 25#include <linux/slab.h>
4983b39a 26#include "osd.h"
645954c5 27#include "logging.h"
af167ae9 28#include "netvsc.h"
043efcc3 29#include "rndis_filter.h"
314bf1d1 30#include "channel.h"
fceaf24a
HJ
31
32
454f18a9 33/* Globals */
85799a37 34static const char *driver_name = "netvsc";
fceaf24a 35
454f18a9 36/* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
85799a37 37static const struct hv_guid netvsc_device_type = {
caf26a31
GKH
38 .data = {
39 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
40 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
41 }
fceaf24a
HJ
42};
43
5a71ae30 44static int netvsc_device_add(struct hv_device *device, void *additional_info);
21a80820 45
5a71ae30 46static int netvsc_device_remove(struct hv_device *device);
21a80820 47
5a71ae30 48static void netvsc_cleanup(struct hv_driver *driver);
21a80820 49
5a71ae30 50static void netvsc_channel_cb(void *context);
21a80820 51
5a71ae30 52static int netvsc_init_send_buf(struct hv_device *device);
21a80820 53
5a71ae30 54static int netvsc_init_recv_buf(struct hv_device *device);
21a80820 55
5a71ae30 56static int netvsc_destroy_send_buf(struct netvsc_device *net_device);
21a80820 57
5a71ae30 58static int netvsc_destroy_recv_buf(struct netvsc_device *net_device);
21a80820 59
5a71ae30 60static int netvsc_connect_vsp(struct hv_device *device);
21a80820 61
5a71ae30 62static void netvsc_send_completion(struct hv_device *device,
85799a37 63 struct vmpacket_descriptor *packet);
21a80820 64
5a71ae30 65static int netvsc_send(struct hv_device *device,
85799a37 66 struct hv_netvsc_packet *packet);
21a80820 67
5a71ae30 68static void netvsc_receive(struct hv_device *device,
85799a37 69 struct vmpacket_descriptor *packet);
21a80820 70
5a71ae30 71static void netvsc_receive_completion(void *context);
21a80820 72
5a71ae30 73static void netvsc_send_recv_completion(struct hv_device *device,
85799a37 74 u64 transaction_id);
21a80820 75
fceaf24a 76
5a71ae30 77static struct netvsc_device *alloc_net_device(struct hv_device *device)
fceaf24a 78{
85799a37 79 struct netvsc_device *net_device;
fceaf24a 80
85799a37
HZ
81 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
82 if (!net_device)
fceaf24a
HJ
83 return NULL;
84
454f18a9 85 /* Set to 2 to allow both inbound and outbound traffic */
53d21fdb 86 atomic_cmpxchg(&net_device->refcnt, 0, 2);
fceaf24a 87
53d21fdb 88 net_device->dev = device;
ca623ad3 89 device->ext = net_device;
fceaf24a 90
85799a37 91 return net_device;
fceaf24a
HJ
92}
93
5a71ae30 94static void free_net_device(struct netvsc_device *device)
fceaf24a 95{
53d21fdb 96 WARN_ON(atomic_read(&device->refcnt) == 0);
ca623ad3 97 device->dev->ext = NULL;
85799a37 98 kfree(device);
fceaf24a
HJ
99}
100
101
454f18a9 102/* Get the net device object iff exists and its refcount > 1 */
5a71ae30 103static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
fceaf24a 104{
85799a37 105 struct netvsc_device *net_device;
fceaf24a 106
ca623ad3 107 net_device = device->ext;
53d21fdb
HZ
108 if (net_device && atomic_read(&net_device->refcnt) > 1)
109 atomic_inc(&net_device->refcnt);
fceaf24a 110 else
85799a37 111 net_device = NULL;
fceaf24a 112
85799a37 113 return net_device;
fceaf24a
HJ
114}
115
454f18a9 116/* Get the net device object iff exists and its refcount > 0 */
5a71ae30 117static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
fceaf24a 118{
85799a37 119 struct netvsc_device *net_device;
fceaf24a 120
ca623ad3 121 net_device = device->ext;
53d21fdb
HZ
122 if (net_device && atomic_read(&net_device->refcnt))
123 atomic_inc(&net_device->refcnt);
fceaf24a 124 else
85799a37 125 net_device = NULL;
fceaf24a 126
85799a37 127 return net_device;
fceaf24a
HJ
128}
129
5a71ae30 130static void put_net_device(struct hv_device *device)
fceaf24a 131{
85799a37 132 struct netvsc_device *net_device;
fceaf24a 133
ca623ad3 134 net_device = device->ext;
972b9529 135 /* ASSERT(netDevice); */
fceaf24a 136
53d21fdb 137 atomic_dec(&net_device->refcnt);
fceaf24a
HJ
138}
139
5a71ae30
HZ
140static struct netvsc_device *release_outbound_net_device(
141 struct hv_device *device)
fceaf24a 142{
85799a37 143 struct netvsc_device *net_device;
fceaf24a 144
ca623ad3 145 net_device = device->ext;
85799a37 146 if (net_device == NULL)
fceaf24a
HJ
147 return NULL;
148
454f18a9 149 /* Busy wait until the ref drop to 2, then set it to 1 */
53d21fdb 150 while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2)
b4362c9c 151 udelay(100);
fceaf24a 152
85799a37 153 return net_device;
fceaf24a
HJ
154}
155
5a71ae30
HZ
156static struct netvsc_device *release_inbound_net_device(
157 struct hv_device *device)
fceaf24a 158{
85799a37 159 struct netvsc_device *net_device;
fceaf24a 160
ca623ad3 161 net_device = device->ext;
85799a37 162 if (net_device == NULL)
fceaf24a
HJ
163 return NULL;
164
454f18a9 165 /* Busy wait until the ref drop to 1, then set it to 0 */
53d21fdb 166 while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
b4362c9c 167 udelay(100);
fceaf24a 168
ca623ad3 169 device->ext = NULL;
85799a37 170 return net_device;
fceaf24a
HJ
171}
172
3e189519 173/*
5a71ae30 174 * netvsc_initialize - Main entry point
21a80820 175 */
5a71ae30 176int netvsc_initialize(struct hv_driver *drv)
fceaf24a 177{
7e23a6e9 178 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
fceaf24a 179
21a80820
GKH
180 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
181 "sizeof(struct nvsp_message)=%zd, "
182 "sizeof(struct vmtransfer_page_packet_header)=%zd",
183 sizeof(struct hv_netvsc_packet),
184 sizeof(struct nvsp_message),
185 sizeof(struct vmtransfer_page_packet_header));
fceaf24a 186
454f18a9 187 /* Make sure we are at least 2 pages since 1 page is used for control */
972b9529 188 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
fceaf24a 189
85799a37 190 drv->name = driver_name;
ca623ad3 191 memcpy(&drv->dev_type, &netvsc_device_type, sizeof(struct hv_guid));
fceaf24a 192
454f18a9 193 /* Make sure it is set by the caller */
972b9529
BP
194 /* FIXME: These probably should still be tested in some way */
195 /* ASSERT(driver->OnReceiveCallback); */
196 /* ASSERT(driver->OnLinkStatusChanged); */
fceaf24a 197
454f18a9 198 /* Setup the dispatch table */
ca623ad3
HZ
199 driver->base.dev_add = netvsc_device_add;
200 driver->base.dev_rm = netvsc_device_remove;
201 driver->base.cleanup = netvsc_cleanup;
fceaf24a 202
72a2f5bd 203 driver->send = netvsc_send;
fceaf24a 204
9c26aa0d 205 rndis_filter_init(driver);
21a80820 206 return 0;
fceaf24a
HJ
207}
208
5a71ae30 209static int netvsc_init_recv_buf(struct hv_device *device)
fceaf24a 210{
21a80820 211 int ret = 0;
85799a37
HZ
212 struct netvsc_device *net_device;
213 struct nvsp_message *init_packet;
fceaf24a 214
5a71ae30 215 net_device = get_outbound_net_device(device);
85799a37 216 if (!net_device) {
21a80820
GKH
217 DPRINT_ERR(NETVSC, "unable to get net device..."
218 "device being destroyed?");
fceaf24a
HJ
219 return -1;
220 }
972b9529 221 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
21a80820 222 /* page-size grandularity */
972b9529 223 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
fceaf24a 224
53d21fdb 225 net_device->recv_buf =
df3493e0
S
226 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
227 get_order(net_device->recv_buf_size));
53d21fdb 228 if (!net_device->recv_buf) {
21a80820
GKH
229 DPRINT_ERR(NETVSC,
230 "unable to allocate receive buffer of size %d",
53d21fdb 231 net_device->recv_buf_size);
fceaf24a
HJ
232 ret = -1;
233 goto Cleanup;
234 }
21a80820 235 /* page-aligned buffer */
972b9529
BP
236 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
237 /* 0); */
fceaf24a
HJ
238
239 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
240
454f18a9
BP
241 /*
242 * Establish the gpadl handle for this buffer on this
243 * channel. Note: This call uses the vmbus connection rather
244 * than the channel to establish the gpadl handle.
245 */
53d21fdb
HZ
246 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
247 net_device->recv_buf_size,
248 &net_device->recv_buf_gpadl_handle);
21a80820
GKH
249 if (ret != 0) {
250 DPRINT_ERR(NETVSC,
251 "unable to establish receive buffer's gpadl");
fceaf24a
HJ
252 goto Cleanup;
253 }
254
203df82d 255 /* osd_waitevent_wait(ext->ChannelInitEvent); */
fceaf24a 256
454f18a9 257 /* Notify the NetVsp of the gpadl handle */
fceaf24a
HJ
258 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
259
53d21fdb 260 init_packet = &net_device->channel_init_pkt;
fceaf24a 261
85799a37 262 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 263
53d21fdb
HZ
264 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
265 init_packet->msg.v1_msg.send_recv_buf.
266 gpadl_handle = net_device->recv_buf_gpadl_handle;
267 init_packet->msg.v1_msg.
268 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
fceaf24a 269
454f18a9 270 /* Send the gpadl notification request */
85799a37 271 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 272 sizeof(struct nvsp_message),
85799a37 273 (unsigned long)init_packet,
415f2287 274 VM_PKT_DATA_INBAND,
5a4df290 275 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820
GKH
276 if (ret != 0) {
277 DPRINT_ERR(NETVSC,
278 "unable to send receive buffer's gpadl to netvsp");
fceaf24a
HJ
279 goto Cleanup;
280 }
281
53d21fdb 282 osd_waitevent_wait(net_device->channel_init_event);
fceaf24a 283
454f18a9 284 /* Check the response */
53d21fdb
HZ
285 if (init_packet->msg.v1_msg.
286 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
21a80820
GKH
287 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
288 "initialzation with NetVsp - status %d",
53d21fdb
HZ
289 init_packet->msg.v1_msg.
290 send_recv_buf_complete.status);
fceaf24a
HJ
291 ret = -1;
292 goto Cleanup;
293 }
294
454f18a9 295 /* Parse the response */
972b9529
BP
296 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
297 /* ASSERT(netDevice->ReceiveSections == NULL); */
fceaf24a 298
53d21fdb
HZ
299 net_device->recv_section_cnt = init_packet->msg.
300 v1_msg.send_recv_buf_complete.num_sections;
fceaf24a 301
53d21fdb 302 net_device->recv_section = kmalloc(net_device->recv_section_cnt
85799a37 303 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
53d21fdb 304 if (net_device->recv_section == NULL) {
fceaf24a
HJ
305 ret = -1;
306 goto Cleanup;
307 }
308
53d21fdb
HZ
309 memcpy(net_device->recv_section,
310 init_packet->msg.v1_msg.
311 send_recv_buf_complete.sections,
312 net_device->recv_section_cnt *
85799a37 313 sizeof(struct nvsp_1_receive_buffer_section));
fceaf24a 314
21a80820
GKH
315 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
316 "endoffset %d, suballoc size %d, num suballocs %d)",
53d21fdb
HZ
317 net_device->recv_section_cnt,
318 net_device->recv_section[0].offset,
319 net_device->recv_section[0].end_offset,
320 net_device->recv_section[0].sub_alloc_size,
321 net_device->recv_section[0].num_sub_allocs);
fceaf24a 322
21a80820
GKH
323 /*
324 * For 1st release, there should only be 1 section that represents the
325 * entire receive buffer
326 */
53d21fdb
HZ
327 if (net_device->recv_section_cnt != 1 ||
328 net_device->recv_section->offset != 0) {
fceaf24a
HJ
329 ret = -1;
330 goto Cleanup;
331 }
332
333 goto Exit;
334
335Cleanup:
5a71ae30 336 netvsc_destroy_recv_buf(net_device);
fceaf24a
HJ
337
338Exit:
5a71ae30 339 put_net_device(device);
fceaf24a
HJ
340 return ret;
341}
342
5a71ae30 343static int netvsc_init_send_buf(struct hv_device *device)
fceaf24a 344{
21a80820 345 int ret = 0;
85799a37
HZ
346 struct netvsc_device *net_device;
347 struct nvsp_message *init_packet;
fceaf24a 348
5a71ae30 349 net_device = get_outbound_net_device(device);
85799a37 350 if (!net_device) {
21a80820
GKH
351 DPRINT_ERR(NETVSC, "unable to get net device..."
352 "device being destroyed?");
fceaf24a
HJ
353 return -1;
354 }
53d21fdb 355 if (net_device->send_buf_size <= 0) {
79069684
BP
356 ret = -EINVAL;
357 goto Cleanup;
358 }
359
21a80820 360 /* page-size grandularity */
972b9529 361 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
21a80820 362
53d21fdb 363 net_device->send_buf =
df3493e0
S
364 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
365 get_order(net_device->send_buf_size));
53d21fdb 366 if (!net_device->send_buf) {
21a80820 367 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
53d21fdb 368 net_device->send_buf_size);
fceaf24a
HJ
369 ret = -1;
370 goto Cleanup;
371 }
21a80820 372 /* page-aligned buffer */
972b9529 373 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
fceaf24a
HJ
374
375 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
376
454f18a9
BP
377 /*
378 * Establish the gpadl handle for this buffer on this
379 * channel. Note: This call uses the vmbus connection rather
380 * than the channel to establish the gpadl handle.
381 */
53d21fdb
HZ
382 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
383 net_device->send_buf_size,
384 &net_device->send_buf_gpadl_handle);
21a80820 385 if (ret != 0) {
fceaf24a
HJ
386 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
387 goto Cleanup;
388 }
389
203df82d 390 /* osd_waitevent_wait(ext->ChannelInitEvent); */
fceaf24a 391
454f18a9 392 /* Notify the NetVsp of the gpadl handle */
fceaf24a
HJ
393 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
394
53d21fdb 395 init_packet = &net_device->channel_init_pkt;
fceaf24a 396
85799a37 397 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 398
53d21fdb
HZ
399 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
400 init_packet->msg.v1_msg.send_recv_buf.
401 gpadl_handle = net_device->send_buf_gpadl_handle;
402 init_packet->msg.v1_msg.send_recv_buf.id =
85799a37 403 NETVSC_SEND_BUFFER_ID;
fceaf24a 404
454f18a9 405 /* Send the gpadl notification request */
85799a37 406 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 407 sizeof(struct nvsp_message),
85799a37 408 (unsigned long)init_packet,
415f2287 409 VM_PKT_DATA_INBAND,
5a4df290 410 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820
GKH
411 if (ret != 0) {
412 DPRINT_ERR(NETVSC,
413 "unable to send receive buffer's gpadl to netvsp");
fceaf24a
HJ
414 goto Cleanup;
415 }
416
53d21fdb 417 osd_waitevent_wait(net_device->channel_init_event);
fceaf24a 418
454f18a9 419 /* Check the response */
53d21fdb
HZ
420 if (init_packet->msg.v1_msg.
421 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
21a80820
GKH
422 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
423 "initialzation with NetVsp - status %d",
53d21fdb
HZ
424 init_packet->msg.v1_msg.
425 send_send_buf_complete.status);
fceaf24a
HJ
426 ret = -1;
427 goto Cleanup;
428 }
429
53d21fdb
HZ
430 net_device->send_section_size = init_packet->
431 msg.v1_msg.send_send_buf_complete.section_size;
fceaf24a
HJ
432
433 goto Exit;
434
435Cleanup:
5a71ae30 436 netvsc_destroy_send_buf(net_device);
fceaf24a
HJ
437
438Exit:
5a71ae30 439 put_net_device(device);
fceaf24a
HJ
440 return ret;
441}
442
5a71ae30 443static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
fceaf24a 444{
85799a37 445 struct nvsp_message *revoke_packet;
21a80820 446 int ret = 0;
fceaf24a 447
454f18a9
BP
448 /*
449 * If we got a section count, it means we received a
450 * SendReceiveBufferComplete msg (ie sent
451 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
452 * to send a revoke msg here
453 */
53d21fdb 454 if (net_device->recv_section_cnt) {
21a80820
GKH
455 DPRINT_INFO(NETVSC,
456 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
fceaf24a 457
454f18a9 458 /* Send the revoke receive buffer */
53d21fdb 459 revoke_packet = &net_device->revoke_packet;
85799a37 460 memset(revoke_packet, 0, sizeof(struct nvsp_message));
fceaf24a 461
53d21fdb
HZ
462 revoke_packet->hdr.msg_type =
463 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
464 revoke_packet->msg.v1_msg.
465 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
fceaf24a 466
53d21fdb 467 ret = vmbus_sendpacket(net_device->dev->channel,
85799a37 468 revoke_packet,
5a4df290 469 sizeof(struct nvsp_message),
85799a37 470 (unsigned long)revoke_packet,
415f2287 471 VM_PKT_DATA_INBAND, 0);
454f18a9
BP
472 /*
473 * If we failed here, we might as well return and
474 * have a leak rather than continue and a bugchk
475 */
21a80820
GKH
476 if (ret != 0) {
477 DPRINT_ERR(NETVSC, "unable to send revoke receive "
478 "buffer to netvsp");
fceaf24a
HJ
479 return -1;
480 }
481 }
482
454f18a9 483 /* Teardown the gpadl on the vsp end */
53d21fdb 484 if (net_device->recv_buf_gpadl_handle) {
fceaf24a
HJ
485 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
486
53d21fdb
HZ
487 ret = vmbus_teardown_gpadl(net_device->dev->channel,
488 net_device->recv_buf_gpadl_handle);
fceaf24a 489
454f18a9 490 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
21a80820
GKH
491 if (ret != 0) {
492 DPRINT_ERR(NETVSC,
493 "unable to teardown receive buffer's gpadl");
fceaf24a
HJ
494 return -1;
495 }
53d21fdb 496 net_device->recv_buf_gpadl_handle = 0;
fceaf24a
HJ
497 }
498
53d21fdb 499 if (net_device->recv_buf) {
fceaf24a
HJ
500 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
501
454f18a9 502 /* Free up the receive buffer */
df3493e0
S
503 free_pages((unsigned long)net_device->recv_buf,
504 get_order(net_device->recv_buf_size));
53d21fdb 505 net_device->recv_buf = NULL;
fceaf24a
HJ
506 }
507
53d21fdb
HZ
508 if (net_device->recv_section) {
509 net_device->recv_section_cnt = 0;
510 kfree(net_device->recv_section);
511 net_device->recv_section = NULL;
fceaf24a
HJ
512 }
513
fceaf24a
HJ
514 return ret;
515}
516
5a71ae30 517static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
fceaf24a 518{
85799a37 519 struct nvsp_message *revoke_packet;
21a80820 520 int ret = 0;
fceaf24a 521
454f18a9
BP
522 /*
523 * If we got a section count, it means we received a
524 * SendReceiveBufferComplete msg (ie sent
525 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
526 * to send a revoke msg here
527 */
53d21fdb 528 if (net_device->send_section_size) {
21a80820
GKH
529 DPRINT_INFO(NETVSC,
530 "Sending NvspMessage1TypeRevokeSendBuffer...");
fceaf24a 531
454f18a9 532 /* Send the revoke send buffer */
53d21fdb 533 revoke_packet = &net_device->revoke_packet;
85799a37 534 memset(revoke_packet, 0, sizeof(struct nvsp_message));
fceaf24a 535
53d21fdb
HZ
536 revoke_packet->hdr.msg_type =
537 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
538 revoke_packet->msg.v1_msg.
539 revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
fceaf24a 540
53d21fdb 541 ret = vmbus_sendpacket(net_device->dev->channel,
85799a37 542 revoke_packet,
5a4df290 543 sizeof(struct nvsp_message),
85799a37 544 (unsigned long)revoke_packet,
415f2287 545 VM_PKT_DATA_INBAND, 0);
21a80820
GKH
546 /*
547 * If we failed here, we might as well return and have a leak
548 * rather than continue and a bugchk
549 */
550 if (ret != 0) {
551 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
552 "to netvsp");
fceaf24a
HJ
553 return -1;
554 }
555 }
556
454f18a9 557 /* Teardown the gpadl on the vsp end */
53d21fdb 558 if (net_device->send_buf_gpadl_handle) {
fceaf24a 559 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
53d21fdb
HZ
560 ret = vmbus_teardown_gpadl(net_device->dev->channel,
561 net_device->send_buf_gpadl_handle);
fceaf24a 562
21a80820
GKH
563 /*
564 * If we failed here, we might as well return and have a leak
565 * rather than continue and a bugchk
566 */
567 if (ret != 0) {
568 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
569 "gpadl");
fceaf24a
HJ
570 return -1;
571 }
53d21fdb 572 net_device->send_buf_gpadl_handle = 0;
fceaf24a
HJ
573 }
574
53d21fdb 575 if (net_device->send_buf) {
fceaf24a
HJ
576 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
577
454f18a9 578 /* Free up the receive buffer */
df3493e0
S
579 free_pages((unsigned long)net_device->send_buf,
580 get_order(net_device->send_buf_size));
53d21fdb 581 net_device->send_buf = NULL;
fceaf24a
HJ
582 }
583
fceaf24a
HJ
584 return ret;
585}
586
587
5a71ae30 588static int netvsc_connect_vsp(struct hv_device *device)
fceaf24a 589{
21a80820 590 int ret;
85799a37
HZ
591 struct netvsc_device *net_device;
592 struct nvsp_message *init_packet;
593 int ndis_version;
fceaf24a 594
5a71ae30 595 net_device = get_outbound_net_device(device);
85799a37 596 if (!net_device) {
21a80820
GKH
597 DPRINT_ERR(NETVSC, "unable to get net device..."
598 "device being destroyed?");
fceaf24a
HJ
599 return -1;
600 }
601
53d21fdb 602 init_packet = &net_device->channel_init_pkt;
fceaf24a 603
85799a37 604 memset(init_packet, 0, sizeof(struct nvsp_message));
53d21fdb
HZ
605 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
606 init_packet->msg.init_msg.init.min_protocol_ver =
85799a37 607 NVSP_MIN_PROTOCOL_VERSION;
53d21fdb 608 init_packet->msg.init_msg.init.max_protocol_ver =
85799a37 609 NVSP_MAX_PROTOCOL_VERSION;
fceaf24a
HJ
610
611 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
612
454f18a9 613 /* Send the init request */
85799a37 614 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 615 sizeof(struct nvsp_message),
85799a37 616 (unsigned long)init_packet,
415f2287 617 VM_PKT_DATA_INBAND,
5a4df290 618 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820
GKH
619
620 if (ret != 0) {
fceaf24a
HJ
621 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
622 goto Cleanup;
623 }
624
53d21fdb 625 osd_waitevent_wait(net_device->channel_init_event);
fceaf24a 626
454f18a9
BP
627 /* Now, check the response */
628 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
fceaf24a 629 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
53d21fdb
HZ
630 init_packet->msg.init_msg.init_complete.status,
631 init_packet->msg.init_msg.
632 init_complete.max_mdl_chain_len);
fceaf24a 633
53d21fdb
HZ
634 if (init_packet->msg.init_msg.init_complete.status !=
635 NVSP_STAT_SUCCESS) {
21a80820
GKH
636 DPRINT_ERR(NETVSC,
637 "unable to initialize with netvsp (status 0x%x)",
53d21fdb 638 init_packet->msg.init_msg.init_complete.status);
fceaf24a
HJ
639 ret = -1;
640 goto Cleanup;
641 }
642
53d21fdb
HZ
643 if (init_packet->msg.init_msg.init_complete.
644 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
21a80820
GKH
645 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
646 "(version expected 1 got %d)",
53d21fdb
HZ
647 init_packet->msg.init_msg.
648 init_complete.negotiated_protocol_ver);
fceaf24a
HJ
649 ret = -1;
650 goto Cleanup;
651 }
652 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
653
454f18a9 654 /* Send the ndis version */
85799a37 655 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 656
85799a37 657 ndis_version = 0x00050000;
fceaf24a 658
53d21fdb
HZ
659 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
660 init_packet->msg.v1_msg.
661 send_ndis_ver.ndis_major_ver =
85799a37 662 (ndis_version & 0xFFFF0000) >> 16;
53d21fdb
HZ
663 init_packet->msg.v1_msg.
664 send_ndis_ver.ndis_minor_ver =
85799a37 665 ndis_version & 0xFFFF;
fceaf24a 666
454f18a9 667 /* Send the init request */
85799a37 668 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 669 sizeof(struct nvsp_message),
85799a37 670 (unsigned long)init_packet,
415f2287 671 VM_PKT_DATA_INBAND, 0);
21a80820
GKH
672 if (ret != 0) {
673 DPRINT_ERR(NETVSC,
674 "unable to send NvspMessage1TypeSendNdisVersion");
fceaf24a
HJ
675 ret = -1;
676 goto Cleanup;
677 }
454f18a9
BP
678 /*
679 * BUGBUG - We have to wait for the above msg since the
680 * netvsp uses KMCL which acknowledges packet (completion
681 * packet) since our Vmbus always set the
682 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
683 */
203df82d 684 /* osd_waitevent_wait(NetVscChannel->ChannelInitEvent); */
454f18a9
BP
685
686 /* Post the big receive buffer to NetVSP */
5a71ae30 687 ret = netvsc_init_recv_buf(device);
fceaf24a 688 if (ret == 0)
5a71ae30 689 ret = netvsc_init_send_buf(device);
fceaf24a
HJ
690
691Cleanup:
5a71ae30 692 put_net_device(device);
fceaf24a
HJ
693 return ret;
694}
695
85799a37 696static void NetVscDisconnectFromVsp(struct netvsc_device *net_device)
fceaf24a 697{
5a71ae30
HZ
698 netvsc_destroy_recv_buf(net_device);
699 netvsc_destroy_send_buf(net_device);
fceaf24a
HJ
700}
701
3e189519 702/*
5a71ae30
HZ
703 * netvsc_device_add - Callback when the device belonging to this
704 * driver is added
21a80820 705 */
5a71ae30 706static int netvsc_device_add(struct hv_device *device, void *additional_info)
fceaf24a 707{
21a80820 708 int ret = 0;
fceaf24a 709 int i;
85799a37 710 struct netvsc_device *net_device;
d29274ef 711 struct hv_netvsc_packet *packet, *pos;
85799a37 712 struct netvsc_driver *net_driver =
ca623ad3 713 (struct netvsc_driver *)device->drv;
fceaf24a 714
5a71ae30 715 net_device = alloc_net_device(device);
85799a37 716 if (!net_device) {
fceaf24a
HJ
717 ret = -1;
718 goto Cleanup;
719 }
720
85799a37 721 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", net_device);
fceaf24a 722
454f18a9 723 /* Initialize the NetVSC channel extension */
53d21fdb
HZ
724 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
725 spin_lock_init(&net_device->recv_pkt_list_lock);
fceaf24a 726
53d21fdb 727 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
fceaf24a 728
53d21fdb 729 INIT_LIST_HEAD(&net_device->recv_pkt_list);
fceaf24a 730
21a80820
GKH
731 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
732 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
733 (NETVSC_RECEIVE_SG_COUNT *
734 sizeof(struct hv_page_buffer)), GFP_KERNEL);
735 if (!packet) {
736 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
737 "for receive pool (wanted %d got %d)",
738 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
fceaf24a
HJ
739 break;
740 }
72a2f5bd 741 list_add_tail(&packet->list_ent,
53d21fdb 742 &net_device->recv_pkt_list);
fceaf24a 743 }
53d21fdb
HZ
744 net_device->channel_init_event = osd_waitevent_create();
745 if (!net_device->channel_init_event) {
80d11b2a
BP
746 ret = -ENOMEM;
747 goto Cleanup;
748 }
fceaf24a 749
454f18a9 750 /* Open the channel */
72a2f5bd
HZ
751 ret = vmbus_open(device->channel, net_driver->ring_buf_size,
752 net_driver->ring_buf_size, NULL, 0,
5a71ae30 753 netvsc_channel_cb, device);
fceaf24a 754
21a80820 755 if (ret != 0) {
fceaf24a
HJ
756 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
757 ret = -1;
758 goto Cleanup;
759 }
760
454f18a9 761 /* Channel is opened */
fceaf24a
HJ
762 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
763
454f18a9 764 /* Connect with the NetVsp */
5a71ae30 765 ret = netvsc_connect_vsp(device);
21a80820 766 if (ret != 0) {
fceaf24a
HJ
767 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
768 ret = -1;
1fb9dff0 769 goto close;
fceaf24a
HJ
770 }
771
21a80820
GKH
772 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
773 ret);
fceaf24a 774
fceaf24a
HJ
775 return ret;
776
1fb9dff0 777close:
454f18a9 778 /* Now, we can close the channel safely */
85799a37 779 vmbus_close(device->channel);
fceaf24a
HJ
780
781Cleanup:
782
85799a37 783 if (net_device) {
53d21fdb 784 kfree(net_device->channel_init_event);
fceaf24a 785
d29274ef 786 list_for_each_entry_safe(packet, pos,
53d21fdb 787 &net_device->recv_pkt_list,
72a2f5bd
HZ
788 list_ent) {
789 list_del(&packet->list_ent);
8c69f52a 790 kfree(packet);
fceaf24a
HJ
791 }
792
5a71ae30
HZ
793 release_outbound_net_device(device);
794 release_inbound_net_device(device);
fceaf24a 795
5a71ae30 796 free_net_device(net_device);
fceaf24a
HJ
797 }
798
fceaf24a
HJ
799 return ret;
800}
801
3e189519 802/*
5a71ae30 803 * netvsc_device_remove - Callback when the root bus device is removed
21a80820 804 */
5a71ae30 805static int netvsc_device_remove(struct hv_device *device)
fceaf24a 806{
85799a37
HZ
807 struct netvsc_device *net_device;
808 struct hv_netvsc_packet *netvsc_packet, *pos;
fceaf24a 809
21a80820 810 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
ca623ad3 811 device->ext);
fceaf24a 812
454f18a9 813 /* Stop outbound traffic ie sends and receives completions */
5a71ae30 814 net_device = release_outbound_net_device(device);
85799a37 815 if (!net_device) {
fceaf24a
HJ
816 DPRINT_ERR(NETVSC, "No net device present!!");
817 return -1;
818 }
819
454f18a9 820 /* Wait for all send completions */
53d21fdb 821 while (atomic_read(&net_device->num_outstanding_sends)) {
21a80820 822 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
53d21fdb 823 atomic_read(&net_device->num_outstanding_sends));
b4362c9c 824 udelay(100);
fceaf24a
HJ
825 }
826
827 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
828
85799a37 829 NetVscDisconnectFromVsp(net_device);
fceaf24a 830
21a80820 831 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
ca623ad3 832 device->ext);
fceaf24a 833
454f18a9 834 /* Stop inbound traffic ie receives and sends completions */
5a71ae30 835 net_device = release_inbound_net_device(device);
fceaf24a 836
454f18a9 837 /* At this point, no one should be accessing netDevice except in here */
85799a37 838 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", net_device);
fceaf24a 839
454f18a9 840 /* Now, we can close the channel safely */
85799a37 841 vmbus_close(device->channel);
fceaf24a 842
454f18a9 843 /* Release all resources */
85799a37 844 list_for_each_entry_safe(netvsc_packet, pos,
53d21fdb 845 &net_device->recv_pkt_list, list_ent) {
72a2f5bd 846 list_del(&netvsc_packet->list_ent);
85799a37 847 kfree(netvsc_packet);
fceaf24a
HJ
848 }
849
53d21fdb 850 kfree(net_device->channel_init_event);
5a71ae30 851 free_net_device(net_device);
21a80820 852 return 0;
fceaf24a
HJ
853}
854
3e189519 855/*
5a71ae30 856 * netvsc_cleanup - Perform any cleanup when the driver is removed
21a80820 857 */
5a71ae30 858static void netvsc_cleanup(struct hv_driver *drv)
fceaf24a 859{
fceaf24a
HJ
860}
861
5a71ae30 862static void netvsc_send_completion(struct hv_device *device,
85799a37 863 struct vmpacket_descriptor *packet)
fceaf24a 864{
85799a37
HZ
865 struct netvsc_device *net_device;
866 struct nvsp_message *nvsp_packet;
867 struct hv_netvsc_packet *nvsc_packet;
fceaf24a 868
5a71ae30 869 net_device = get_inbound_net_device(device);
85799a37 870 if (!net_device) {
21a80820
GKH
871 DPRINT_ERR(NETVSC, "unable to get net device..."
872 "device being destroyed?");
fceaf24a
HJ
873 return;
874 }
875
85799a37 876 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
415f2287 877 (packet->offset8 << 3));
fceaf24a 878
21a80820 879 DPRINT_DBG(NETVSC, "send completion packet - type %d",
53d21fdb 880 nvsp_packet->hdr.msg_type);
fceaf24a 881
53d21fdb
HZ
882 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
883 (nvsp_packet->hdr.msg_type ==
884 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
885 (nvsp_packet->hdr.msg_type ==
886 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
454f18a9 887 /* Copy the response back */
53d21fdb 888 memcpy(&net_device->channel_init_pkt, nvsp_packet,
21a80820 889 sizeof(struct nvsp_message));
53d21fdb
HZ
890 osd_waitevent_set(net_device->channel_init_event);
891 } else if (nvsp_packet->hdr.msg_type ==
892 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
454f18a9 893 /* Get the send context */
85799a37 894 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
415f2287 895 packet->trans_id;
972b9529 896 /* ASSERT(nvscPacket); */
fceaf24a 897
454f18a9 898 /* Notify the layer above us */
72a2f5bd
HZ
899 nvsc_packet->completion.send.send_completion(
900 nvsc_packet->completion.send.send_completion_ctx);
fceaf24a 901
53d21fdb 902 atomic_dec(&net_device->num_outstanding_sends);
21a80820
GKH
903 } else {
904 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
53d21fdb 905 "%d received!!", nvsp_packet->hdr.msg_type);
fceaf24a
HJ
906 }
907
5a71ae30 908 put_net_device(device);
fceaf24a
HJ
909}
910
5a71ae30 911static int netvsc_send(struct hv_device *device,
85799a37 912 struct hv_netvsc_packet *packet)
fceaf24a 913{
85799a37 914 struct netvsc_device *net_device;
21a80820 915 int ret = 0;
fceaf24a 916
223c1aa6 917 struct nvsp_message sendMessage;
fceaf24a 918
5a71ae30 919 net_device = get_outbound_net_device(device);
85799a37 920 if (!net_device) {
21a80820 921 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
85799a37 922 "ignoring outbound packets", net_device);
fceaf24a
HJ
923 return -2;
924 }
925
53d21fdb 926 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
72a2f5bd 927 if (packet->is_data_pkt) {
21a80820 928 /* 0 is RMC_DATA; */
53d21fdb 929 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
21a80820
GKH
930 } else {
931 /* 1 is RMC_CONTROL; */
53d21fdb 932 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
21a80820 933 }
fceaf24a 934
454f18a9 935 /* Not using send buffer section */
53d21fdb
HZ
936 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
937 0xFFFFFFFF;
938 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
21a80820 939
72a2f5bd 940 if (packet->page_buf_cnt) {
85799a37 941 ret = vmbus_sendpacket_pagebuffer(device->channel,
72a2f5bd
HZ
942 packet->page_buf,
943 packet->page_buf_cnt,
ff3f8eec
GKH
944 &sendMessage,
945 sizeof(struct nvsp_message),
85799a37 946 (unsigned long)packet);
21a80820 947 } else {
85799a37 948 ret = vmbus_sendpacket(device->channel, &sendMessage,
5a4df290 949 sizeof(struct nvsp_message),
85799a37 950 (unsigned long)packet,
415f2287 951 VM_PKT_DATA_INBAND,
5a4df290 952 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
fceaf24a
HJ
953
954 }
955
956 if (ret != 0)
21a80820 957 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
85799a37 958 packet, ret);
fceaf24a 959
53d21fdb 960 atomic_inc(&net_device->num_outstanding_sends);
5a71ae30 961 put_net_device(device);
fceaf24a
HJ
962 return ret;
963}
964
5a71ae30 965static void netvsc_receive(struct hv_device *device,
85799a37 966 struct vmpacket_descriptor *packet)
fceaf24a 967{
85799a37
HZ
968 struct netvsc_device *net_device;
969 struct vmtransfer_page_packet_header *vmxferpage_packet;
970 struct nvsp_message *nvsp_packet;
971 struct hv_netvsc_packet *netvsc_packet = NULL;
c4b0bc94 972 unsigned long start;
85799a37 973 unsigned long end, end_virtual;
7e23a6e9 974 /* struct netvsc_driver *netvscDriver; */
85799a37 975 struct xferpage_packet *xferpage_packet = NULL;
21a80820 976 int i, j;
85799a37 977 int count = 0, bytes_remain = 0;
6436873a 978 unsigned long flags;
d29274ef 979 LIST_HEAD(listHead);
fceaf24a 980
5a71ae30 981 net_device = get_inbound_net_device(device);
85799a37 982 if (!net_device) {
21a80820
GKH
983 DPRINT_ERR(NETVSC, "unable to get net device..."
984 "device being destroyed?");
fceaf24a
HJ
985 return;
986 }
987
21a80820
GKH
988 /*
989 * All inbound packets other than send completion should be xfer page
990 * packet
991 */
415f2287 992 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
21a80820 993 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
415f2287 994 packet->type);
5a71ae30 995 put_net_device(device);
fceaf24a
HJ
996 return;
997 }
998
85799a37 999 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
415f2287 1000 (packet->offset8 << 3));
fceaf24a 1001
454f18a9 1002 /* Make sure this is a valid nvsp packet */
53d21fdb
HZ
1003 if (nvsp_packet->hdr.msg_type !=
1004 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
21a80820 1005 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
53d21fdb 1006 nvsp_packet->hdr.msg_type);
5a71ae30 1007 put_net_device(device);
fceaf24a
HJ
1008 return;
1009 }
1010
21a80820 1011 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
53d21fdb 1012 nvsp_packet->hdr.msg_type);
fceaf24a 1013
85799a37 1014 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
fceaf24a 1015
415f2287 1016 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
21a80820
GKH
1017 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1018 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
415f2287 1019 vmxferpage_packet->xfer_pageset_id);
5a71ae30 1020 put_net_device(device);
fceaf24a
HJ
1021 return;
1022 }
1023
21a80820 1024 DPRINT_DBG(NETVSC, "xfer page - range count %d",
415f2287 1025 vmxferpage_packet->range_cnt);
fceaf24a 1026
454f18a9
BP
1027 /*
1028 * Grab free packets (range count + 1) to represent this xfer
1029 * page packet. +1 to represent the xfer page packet itself.
1030 * We grab it here so that we know exactly how many we can
1031 * fulfil
1032 */
53d21fdb
HZ
1033 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1034 while (!list_empty(&net_device->recv_pkt_list)) {
1035 list_move_tail(net_device->recv_pkt_list.next, &listHead);
415f2287 1036 if (++count == vmxferpage_packet->range_cnt + 1)
fceaf24a
HJ
1037 break;
1038 }
53d21fdb 1039 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
fceaf24a 1040
454f18a9
BP
1041 /*
1042 * We need at least 2 netvsc pkts (1 to represent the xfer
1043 * page and at least 1 for the range) i.e. we can handled
1044 * some of the xfer page packet ranges...
1045 */
21a80820
GKH
1046 if (count < 2) {
1047 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1048 "Dropping this xfer page packet completely!",
415f2287 1049 count, vmxferpage_packet->range_cnt + 1);
fceaf24a 1050
454f18a9 1051 /* Return it to the freelist */
53d21fdb 1052 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
21a80820 1053 for (i = count; i != 0; i--) {
92ec0893 1054 list_move_tail(listHead.next,
53d21fdb 1055 &net_device->recv_pkt_list);
fceaf24a 1056 }
53d21fdb 1057 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
21a80820 1058 flags);
fceaf24a 1059
5a71ae30 1060 netvsc_send_recv_completion(device,
415f2287 1061 vmxferpage_packet->d.trans_id);
fceaf24a 1062
5a71ae30 1063 put_net_device(device);
fceaf24a
HJ
1064 return;
1065 }
1066
454f18a9 1067 /* Remove the 1st packet to represent the xfer page packet itself */
85799a37 1068 xferpage_packet = (struct xferpage_packet *)listHead.next;
72a2f5bd 1069 list_del(&xferpage_packet->list_ent);
d29274ef 1070
21a80820 1071 /* This is how much we can satisfy */
72a2f5bd 1072 xferpage_packet->count = count - 1;
972b9529
BP
1073 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1074 /* vmxferpagePacket->RangeCount); */
21a80820 1075
415f2287 1076 if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
21a80820 1077 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
415f2287 1078 "page...got %d", vmxferpage_packet->range_cnt,
72a2f5bd 1079 xferpage_packet->count);
fceaf24a
HJ
1080 }
1081
454f18a9 1082 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
21a80820 1083 for (i = 0; i < (count - 1); i++) {
85799a37 1084 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
72a2f5bd 1085 list_del(&netvsc_packet->list_ent);
fceaf24a 1086
454f18a9 1087 /* Initialize the netvsc packet */
72a2f5bd
HZ
1088 netvsc_packet->xfer_page_pkt = xferpage_packet;
1089 netvsc_packet->completion.recv.recv_completion =
5a71ae30 1090 netvsc_receive_completion;
72a2f5bd 1091 netvsc_packet->completion.recv.recv_completion_ctx =
85799a37 1092 netvsc_packet;
72a2f5bd 1093 netvsc_packet->device = device;
21a80820 1094 /* Save this so that we can send it back */
72a2f5bd 1095 netvsc_packet->completion.recv.recv_completion_tid =
415f2287 1096 vmxferpage_packet->d.trans_id;
fceaf24a 1097
72a2f5bd 1098 netvsc_packet->total_data_buflen =
415f2287 1099 vmxferpage_packet->ranges[i].byte_count;
72a2f5bd 1100 netvsc_packet->page_buf_cnt = 1;
fceaf24a 1101
972b9529
BP
1102 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1103 /* vmxferpagePacket->Ranges[i].ByteCount < */
1104 /* netDevice->ReceiveBufferSize); */
fceaf24a 1105
ca623ad3 1106 netvsc_packet->page_buf[0].len =
415f2287 1107 vmxferpage_packet->ranges[i].byte_count;
fceaf24a 1108
85799a37 1109 start = virt_to_phys((void *)((unsigned long)net_device->
415f2287 1110 recv_buf + vmxferpage_packet->ranges[i].byte_offset));
fceaf24a 1111
ca623ad3 1112 netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
53d21fdb 1113 end_virtual = (unsigned long)net_device->recv_buf
415f2287
HZ
1114 + vmxferpage_packet->ranges[i].byte_offset
1115 + vmxferpage_packet->ranges[i].byte_count - 1;
85799a37 1116 end = virt_to_phys((void *)end_virtual);
fceaf24a 1117
454f18a9 1118 /* Calculate the page relative offset */
ca623ad3 1119 netvsc_packet->page_buf[0].offset =
415f2287 1120 vmxferpage_packet->ranges[i].byte_offset &
85799a37 1121 (PAGE_SIZE - 1);
21a80820
GKH
1122 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1123 /* Handle frame across multiple pages: */
ca623ad3
HZ
1124 netvsc_packet->page_buf[0].len =
1125 (netvsc_packet->page_buf[0].pfn <<
85799a37 1126 PAGE_SHIFT)
21a80820 1127 + PAGE_SIZE - start;
72a2f5bd 1128 bytes_remain = netvsc_packet->total_data_buflen -
ca623ad3 1129 netvsc_packet->page_buf[0].len;
21a80820 1130 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
ca623ad3 1131 netvsc_packet->page_buf[j].offset = 0;
85799a37 1132 if (bytes_remain <= PAGE_SIZE) {
ca623ad3 1133 netvsc_packet->page_buf[j].len =
85799a37
HZ
1134 bytes_remain;
1135 bytes_remain = 0;
21a80820 1136 } else {
ca623ad3 1137 netvsc_packet->page_buf[j].len =
85799a37
HZ
1138 PAGE_SIZE;
1139 bytes_remain -= PAGE_SIZE;
21a80820 1140 }
ca623ad3 1141 netvsc_packet->page_buf[j].pfn =
85799a37
HZ
1142 virt_to_phys((void *)(end_virtual -
1143 bytes_remain)) >> PAGE_SHIFT;
72a2f5bd 1144 netvsc_packet->page_buf_cnt++;
85799a37 1145 if (bytes_remain == 0)
21a80820 1146 break;
fceaf24a 1147 }
972b9529 1148 /* ASSERT(bytesRemain == 0); */
fceaf24a 1149 }
21a80820
GKH
1150 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1151 "(pfn %llx, offset %u, len %u)", i,
415f2287
HZ
1152 vmxferpage_packet->ranges[i].byte_offset,
1153 vmxferpage_packet->ranges[i].byte_count,
ca623ad3
HZ
1154 netvsc_packet->page_buf[0].pfn,
1155 netvsc_packet->page_buf[0].offset,
1156 netvsc_packet->page_buf[0].len);
fceaf24a 1157
454f18a9 1158 /* Pass it to the upper layer */
ca623ad3 1159 ((struct netvsc_driver *)device->drv)->
72a2f5bd 1160 recv_cb(device, netvsc_packet);
fceaf24a 1161
5a71ae30 1162 netvsc_receive_completion(netvsc_packet->
72a2f5bd 1163 completion.recv.recv_completion_ctx);
fceaf24a
HJ
1164 }
1165
972b9529 1166 /* ASSERT(list_empty(&listHead)); */
fceaf24a 1167
5a71ae30 1168 put_net_device(device);
fceaf24a
HJ
1169}
1170
5a71ae30 1171static void netvsc_send_recv_completion(struct hv_device *device,
85799a37 1172 u64 transaction_id)
fceaf24a 1173{
223c1aa6 1174 struct nvsp_message recvcompMessage;
21a80820
GKH
1175 int retries = 0;
1176 int ret;
fceaf24a 1177
21a80820 1178 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
85799a37 1179 transaction_id);
fceaf24a 1180
53d21fdb
HZ
1181 recvcompMessage.hdr.msg_type =
1182 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
fceaf24a 1183
454f18a9 1184 /* FIXME: Pass in the status */
53d21fdb
HZ
1185 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
1186 NVSP_STAT_SUCCESS;
fceaf24a
HJ
1187
1188retry_send_cmplt:
454f18a9 1189 /* Send the completion */
85799a37
HZ
1190 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
1191 sizeof(struct nvsp_message), transaction_id,
415f2287 1192 VM_PKT_COMP, 0);
21a80820
GKH
1193 if (ret == 0) {
1194 /* success */
454f18a9 1195 /* no-op */
21a80820
GKH
1196 } else if (ret == -1) {
1197 /* no more room...wait a bit and attempt to retry 3 times */
fceaf24a 1198 retries++;
21a80820 1199 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
85799a37 1200 "(tid %llx)...retrying %d", transaction_id, retries);
fceaf24a 1201
21a80820 1202 if (retries < 4) {
b4362c9c 1203 udelay(100);
fceaf24a 1204 goto retry_send_cmplt;
21a80820
GKH
1205 } else {
1206 DPRINT_ERR(NETVSC, "unable to send receive completion "
1207 "pkt (tid %llx)...give up retrying",
85799a37 1208 transaction_id);
fceaf24a 1209 }
21a80820
GKH
1210 } else {
1211 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
85799a37 1212 "%llx", transaction_id);
fceaf24a
HJ
1213 }
1214}
1215
454f18a9 1216/* Send a receive completion packet to RNDIS device (ie NetVsp) */
5a71ae30 1217static void netvsc_receive_completion(void *context)
fceaf24a 1218{
85799a37 1219 struct hv_netvsc_packet *packet = context;
72a2f5bd 1220 struct hv_device *device = (struct hv_device *)packet->device;
85799a37
HZ
1221 struct netvsc_device *net_device;
1222 u64 transaction_id = 0;
1223 bool fsend_receive_comp = false;
6436873a 1224 unsigned long flags;
fceaf24a 1225
972b9529 1226 /* ASSERT(packet->XferPagePacket); */
fceaf24a 1227
21a80820
GKH
1228 /*
1229 * Even though it seems logical to do a GetOutboundNetDevice() here to
1230 * send out receive completion, we are using GetInboundNetDevice()
1231 * since we may have disable outbound traffic already.
1232 */
5a71ae30 1233 net_device = get_inbound_net_device(device);
85799a37 1234 if (!net_device) {
21a80820
GKH
1235 DPRINT_ERR(NETVSC, "unable to get net device..."
1236 "device being destroyed?");
fceaf24a
HJ
1237 return;
1238 }
1239
454f18a9 1240 /* Overloading use of the lock. */
53d21fdb 1241 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
fceaf24a 1242
972b9529 1243 /* ASSERT(packet->XferPagePacket->Count > 0); */
72a2f5bd 1244 packet->xfer_page_pkt->count--;
fceaf24a 1245
21a80820
GKH
1246 /*
1247 * Last one in the line that represent 1 xfer page packet.
1248 * Return the xfer page packet itself to the freelist
1249 */
72a2f5bd 1250 if (packet->xfer_page_pkt->count == 0) {
85799a37 1251 fsend_receive_comp = true;
72a2f5bd
HZ
1252 transaction_id = packet->completion.recv.recv_completion_tid;
1253 list_add_tail(&packet->xfer_page_pkt->list_ent,
53d21fdb 1254 &net_device->recv_pkt_list);
fceaf24a 1255
fceaf24a
HJ
1256 }
1257
454f18a9 1258 /* Put the packet back */
53d21fdb
HZ
1259 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
1260 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
fceaf24a 1261
454f18a9 1262 /* Send a receive completion for the xfer page packet */
85799a37 1263 if (fsend_receive_comp)
5a71ae30 1264 netvsc_send_recv_completion(device, transaction_id);
fceaf24a 1265
5a71ae30 1266 put_net_device(device);
fceaf24a
HJ
1267}
1268
5a71ae30 1269static void netvsc_channel_cb(void *context)
fceaf24a 1270{
21a80820 1271 int ret;
85799a37
HZ
1272 struct hv_device *device = context;
1273 struct netvsc_device *net_device;
1274 u32 bytes_recvd;
1275 u64 request_id;
c6fcf0ba 1276 unsigned char *packet;
8dc0a06a 1277 struct vmpacket_descriptor *desc;
c6fcf0ba
BP
1278 unsigned char *buffer;
1279 int bufferlen = NETVSC_PACKET_SIZE;
fceaf24a 1280
972b9529 1281 /* ASSERT(device); */
fceaf24a 1282
c6fcf0ba 1283 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
d70c6731 1284 GFP_ATOMIC);
c6fcf0ba
BP
1285 if (!packet)
1286 return;
1287 buffer = packet;
1288
5a71ae30 1289 net_device = get_inbound_net_device(device);
85799a37 1290 if (!net_device) {
21a80820 1291 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
85799a37 1292 "ignoring inbound packets", net_device);
c6fcf0ba 1293 goto out;
fceaf24a
HJ
1294 }
1295
21a80820 1296 do {
9f630068 1297 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
85799a37 1298 &bytes_recvd, &request_id);
21a80820 1299 if (ret == 0) {
85799a37 1300 if (bytes_recvd > 0) {
21a80820 1301 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
85799a37 1302 bytes_recvd, request_id);
21a80820
GKH
1303
1304 desc = (struct vmpacket_descriptor *)buffer;
415f2287
HZ
1305 switch (desc->type) {
1306 case VM_PKT_COMP:
5a71ae30 1307 netvsc_send_completion(device, desc);
21a80820
GKH
1308 break;
1309
415f2287 1310 case VM_PKT_DATA_USING_XFER_PAGES:
5a71ae30 1311 netvsc_receive(device, desc);
21a80820
GKH
1312 break;
1313
1314 default:
1315 DPRINT_ERR(NETVSC,
1316 "unhandled packet type %d, "
1317 "tid %llx len %d\n",
415f2287 1318 desc->type, request_id,
85799a37 1319 bytes_recvd);
21a80820 1320 break;
fceaf24a
HJ
1321 }
1322
454f18a9 1323 /* reset */
c6fcf0ba 1324 if (bufferlen > NETVSC_PACKET_SIZE) {
8c69f52a 1325 kfree(buffer);
fceaf24a 1326 buffer = packet;
c6fcf0ba 1327 bufferlen = NETVSC_PACKET_SIZE;
fceaf24a 1328 }
21a80820 1329 } else {
454f18a9 1330 /* reset */
c6fcf0ba 1331 if (bufferlen > NETVSC_PACKET_SIZE) {
8c69f52a 1332 kfree(buffer);
fceaf24a 1333 buffer = packet;
c6fcf0ba 1334 bufferlen = NETVSC_PACKET_SIZE;
fceaf24a
HJ
1335 }
1336
1337 break;
1338 }
21a80820
GKH
1339 } else if (ret == -2) {
1340 /* Handle large packet */
85799a37 1341 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
21a80820 1342 if (buffer == NULL) {
454f18a9 1343 /* Try again next time around */
21a80820
GKH
1344 DPRINT_ERR(NETVSC,
1345 "unable to allocate buffer of size "
85799a37 1346 "(%d)!!", bytes_recvd);
fceaf24a
HJ
1347 break;
1348 }
1349
85799a37 1350 bufferlen = bytes_recvd;
fceaf24a
HJ
1351 }
1352 } while (1);
1353
5a71ae30 1354 put_net_device(device);
c6fcf0ba
BP
1355out:
1356 kfree(buffer);
fceaf24a
HJ
1357 return;
1358}
This page took 0.268277 seconds and 5 git commands to generate.