staging: hv: Remove all unneeded DPRINT from hv_netvsc
[linux-2.6/btrfs-unstable.git] / drivers / staging / hv / netvsc.c
blob227d7d3006c07c5721f1888a21ebc6eeec3c3bf1
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
24 #include <linux/mm.h>
25 #include <linux/delay.h>
26 #include <linux/io.h>
27 #include <linux/slab.h>
28 #include "hv_api.h"
29 #include "logging.h"
30 #include "netvsc.h"
31 #include "rndis_filter.h"
32 #include "channel.h"
35 /* Globals */
36 static const char *driver_name = "netvsc";
38 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
39 static const struct hv_guid netvsc_device_type = {
40 .data = {
41 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
42 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
46 static int netvsc_device_add(struct hv_device *device, void *additional_info);
48 static int netvsc_device_remove(struct hv_device *device);
50 static void netvsc_cleanup(struct hv_driver *driver);
52 static void netvsc_channel_cb(void *context);
54 static int netvsc_init_send_buf(struct hv_device *device);
56 static int netvsc_init_recv_buf(struct hv_device *device);
58 static int netvsc_destroy_send_buf(struct netvsc_device *net_device);
60 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device);
62 static int netvsc_connect_vsp(struct hv_device *device);
64 static void netvsc_send_completion(struct hv_device *device,
65 struct vmpacket_descriptor *packet);
67 static int netvsc_send(struct hv_device *device,
68 struct hv_netvsc_packet *packet);
70 static void netvsc_receive(struct hv_device *device,
71 struct vmpacket_descriptor *packet);
73 static void netvsc_receive_completion(void *context);
75 static void netvsc_send_recv_completion(struct hv_device *device,
76 u64 transaction_id);
79 static struct netvsc_device *alloc_net_device(struct hv_device *device)
81 struct netvsc_device *net_device;
83 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
84 if (!net_device)
85 return NULL;
87 /* Set to 2 to allow both inbound and outbound traffic */
88 atomic_cmpxchg(&net_device->refcnt, 0, 2);
90 net_device->dev = device;
91 device->ext = net_device;
93 return net_device;
96 static void free_net_device(struct netvsc_device *device)
98 WARN_ON(atomic_read(&device->refcnt) != 0);
99 device->dev->ext = NULL;
100 kfree(device);
104 /* Get the net device object iff exists and its refcount > 1 */
105 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
107 struct netvsc_device *net_device;
109 net_device = device->ext;
110 if (net_device && atomic_read(&net_device->refcnt) > 1)
111 atomic_inc(&net_device->refcnt);
112 else
113 net_device = NULL;
115 return net_device;
118 /* Get the net device object iff exists and its refcount > 0 */
119 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
121 struct netvsc_device *net_device;
123 net_device = device->ext;
124 if (net_device && atomic_read(&net_device->refcnt))
125 atomic_inc(&net_device->refcnt);
126 else
127 net_device = NULL;
129 return net_device;
132 static void put_net_device(struct hv_device *device)
134 struct netvsc_device *net_device;
136 net_device = device->ext;
138 atomic_dec(&net_device->refcnt);
141 static struct netvsc_device *release_outbound_net_device(
142 struct hv_device *device)
144 struct netvsc_device *net_device;
146 net_device = device->ext;
147 if (net_device == NULL)
148 return NULL;
150 /* Busy wait until the ref drop to 2, then set it to 1 */
151 while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2)
152 udelay(100);
154 return net_device;
157 static struct netvsc_device *release_inbound_net_device(
158 struct hv_device *device)
160 struct netvsc_device *net_device;
162 net_device = device->ext;
163 if (net_device == NULL)
164 return NULL;
166 /* Busy wait until the ref drop to 1, then set it to 0 */
167 while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
168 udelay(100);
170 device->ext = NULL;
171 return net_device;
175 * netvsc_initialize - Main entry point
177 int netvsc_initialize(struct hv_driver *drv)
179 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
181 drv->name = driver_name;
182 memcpy(&drv->dev_type, &netvsc_device_type, sizeof(struct hv_guid));
184 /* Setup the dispatch table */
185 driver->base.dev_add = netvsc_device_add;
186 driver->base.dev_rm = netvsc_device_remove;
187 driver->base.cleanup = netvsc_cleanup;
189 driver->send = netvsc_send;
191 rndis_filter_init(driver);
192 return 0;
195 static int netvsc_init_recv_buf(struct hv_device *device)
197 int ret = 0;
198 struct netvsc_device *net_device;
199 struct nvsp_message *init_packet;
201 net_device = get_outbound_net_device(device);
202 if (!net_device) {
203 DPRINT_ERR(NETVSC, "unable to get net device..."
204 "device being destroyed?");
205 return -1;
208 net_device->recv_buf =
209 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
210 get_order(net_device->recv_buf_size));
211 if (!net_device->recv_buf) {
212 DPRINT_ERR(NETVSC,
213 "unable to allocate receive buffer of size %d",
214 net_device->recv_buf_size);
215 ret = -1;
216 goto cleanup;
220 * Establish the gpadl handle for this buffer on this
221 * channel. Note: This call uses the vmbus connection rather
222 * than the channel to establish the gpadl handle.
224 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
225 net_device->recv_buf_size,
226 &net_device->recv_buf_gpadl_handle);
227 if (ret != 0) {
228 DPRINT_ERR(NETVSC,
229 "unable to establish receive buffer's gpadl");
230 goto cleanup;
234 /* Notify the NetVsp of the gpadl handle */
235 init_packet = &net_device->channel_init_pkt;
237 memset(init_packet, 0, sizeof(struct nvsp_message));
239 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
240 init_packet->msg.v1_msg.send_recv_buf.
241 gpadl_handle = net_device->recv_buf_gpadl_handle;
242 init_packet->msg.v1_msg.
243 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
245 /* Send the gpadl notification request */
246 net_device->wait_condition = 0;
247 ret = vmbus_sendpacket(device->channel, init_packet,
248 sizeof(struct nvsp_message),
249 (unsigned long)init_packet,
250 VM_PKT_DATA_INBAND,
251 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
252 if (ret != 0) {
253 DPRINT_ERR(NETVSC,
254 "unable to send receive buffer's gpadl to netvsp");
255 goto cleanup;
258 wait_event_timeout(net_device->channel_init_wait,
259 net_device->wait_condition,
260 msecs_to_jiffies(1000));
261 BUG_ON(net_device->wait_condition == 0);
264 /* Check the response */
265 if (init_packet->msg.v1_msg.
266 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
267 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
268 "initialzation with NetVsp - status %d",
269 init_packet->msg.v1_msg.
270 send_recv_buf_complete.status);
271 ret = -1;
272 goto cleanup;
275 /* Parse the response */
277 net_device->recv_section_cnt = init_packet->msg.
278 v1_msg.send_recv_buf_complete.num_sections;
280 net_device->recv_section = kmalloc(net_device->recv_section_cnt
281 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
282 if (net_device->recv_section == NULL) {
283 ret = -1;
284 goto cleanup;
287 memcpy(net_device->recv_section,
288 init_packet->msg.v1_msg.
289 send_recv_buf_complete.sections,
290 net_device->recv_section_cnt *
291 sizeof(struct nvsp_1_receive_buffer_section));
294 * For 1st release, there should only be 1 section that represents the
295 * entire receive buffer
297 if (net_device->recv_section_cnt != 1 ||
298 net_device->recv_section->offset != 0) {
299 ret = -1;
300 goto cleanup;
303 goto exit;
305 cleanup:
306 netvsc_destroy_recv_buf(net_device);
308 exit:
309 put_net_device(device);
310 return ret;
313 static int netvsc_init_send_buf(struct hv_device *device)
315 int ret = 0;
316 struct netvsc_device *net_device;
317 struct nvsp_message *init_packet;
319 net_device = get_outbound_net_device(device);
320 if (!net_device) {
321 DPRINT_ERR(NETVSC, "unable to get net device..."
322 "device being destroyed?");
323 return -1;
325 if (net_device->send_buf_size <= 0) {
326 ret = -EINVAL;
327 goto cleanup;
330 net_device->send_buf =
331 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
332 get_order(net_device->send_buf_size));
333 if (!net_device->send_buf) {
334 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
335 net_device->send_buf_size);
336 ret = -1;
337 goto cleanup;
341 * Establish the gpadl handle for this buffer on this
342 * channel. Note: This call uses the vmbus connection rather
343 * than the channel to establish the gpadl handle.
345 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
346 net_device->send_buf_size,
347 &net_device->send_buf_gpadl_handle);
348 if (ret != 0) {
349 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
350 goto cleanup;
353 /* Notify the NetVsp of the gpadl handle */
354 init_packet = &net_device->channel_init_pkt;
356 memset(init_packet, 0, sizeof(struct nvsp_message));
358 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
359 init_packet->msg.v1_msg.send_recv_buf.
360 gpadl_handle = net_device->send_buf_gpadl_handle;
361 init_packet->msg.v1_msg.send_recv_buf.id =
362 NETVSC_SEND_BUFFER_ID;
364 /* Send the gpadl notification request */
365 net_device->wait_condition = 0;
366 ret = vmbus_sendpacket(device->channel, init_packet,
367 sizeof(struct nvsp_message),
368 (unsigned long)init_packet,
369 VM_PKT_DATA_INBAND,
370 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
371 if (ret != 0) {
372 DPRINT_ERR(NETVSC,
373 "unable to send receive buffer's gpadl to netvsp");
374 goto cleanup;
377 wait_event_timeout(net_device->channel_init_wait,
378 net_device->wait_condition,
379 msecs_to_jiffies(1000));
380 BUG_ON(net_device->wait_condition == 0);
382 /* Check the response */
383 if (init_packet->msg.v1_msg.
384 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
385 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
386 "initialzation with NetVsp - status %d",
387 init_packet->msg.v1_msg.
388 send_send_buf_complete.status);
389 ret = -1;
390 goto cleanup;
393 net_device->send_section_size = init_packet->
394 msg.v1_msg.send_send_buf_complete.section_size;
396 goto exit;
398 cleanup:
399 netvsc_destroy_send_buf(net_device);
401 exit:
402 put_net_device(device);
403 return ret;
406 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
408 struct nvsp_message *revoke_packet;
409 int ret = 0;
412 * If we got a section count, it means we received a
413 * SendReceiveBufferComplete msg (ie sent
414 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
415 * to send a revoke msg here
417 if (net_device->recv_section_cnt) {
418 /* Send the revoke receive buffer */
419 revoke_packet = &net_device->revoke_packet;
420 memset(revoke_packet, 0, sizeof(struct nvsp_message));
422 revoke_packet->hdr.msg_type =
423 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
424 revoke_packet->msg.v1_msg.
425 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
427 ret = vmbus_sendpacket(net_device->dev->channel,
428 revoke_packet,
429 sizeof(struct nvsp_message),
430 (unsigned long)revoke_packet,
431 VM_PKT_DATA_INBAND, 0);
433 * If we failed here, we might as well return and
434 * have a leak rather than continue and a bugchk
436 if (ret != 0) {
437 DPRINT_ERR(NETVSC, "unable to send revoke receive "
438 "buffer to netvsp");
439 return -1;
443 /* Teardown the gpadl on the vsp end */
444 if (net_device->recv_buf_gpadl_handle) {
445 ret = vmbus_teardown_gpadl(net_device->dev->channel,
446 net_device->recv_buf_gpadl_handle);
448 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
449 if (ret != 0) {
450 DPRINT_ERR(NETVSC,
451 "unable to teardown receive buffer's gpadl");
452 return -1;
454 net_device->recv_buf_gpadl_handle = 0;
457 if (net_device->recv_buf) {
458 /* Free up the receive buffer */
459 free_pages((unsigned long)net_device->recv_buf,
460 get_order(net_device->recv_buf_size));
461 net_device->recv_buf = NULL;
464 if (net_device->recv_section) {
465 net_device->recv_section_cnt = 0;
466 kfree(net_device->recv_section);
467 net_device->recv_section = NULL;
470 return ret;
473 static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
475 struct nvsp_message *revoke_packet;
476 int ret = 0;
479 * If we got a section count, it means we received a
480 * SendReceiveBufferComplete msg (ie sent
481 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
482 * to send a revoke msg here
484 if (net_device->send_section_size) {
485 /* Send the revoke send buffer */
486 revoke_packet = &net_device->revoke_packet;
487 memset(revoke_packet, 0, sizeof(struct nvsp_message));
489 revoke_packet->hdr.msg_type =
490 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
491 revoke_packet->msg.v1_msg.
492 revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
494 ret = vmbus_sendpacket(net_device->dev->channel,
495 revoke_packet,
496 sizeof(struct nvsp_message),
497 (unsigned long)revoke_packet,
498 VM_PKT_DATA_INBAND, 0);
500 * If we failed here, we might as well return and have a leak
501 * rather than continue and a bugchk
503 if (ret != 0) {
504 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
505 "to netvsp");
506 return -1;
510 /* Teardown the gpadl on the vsp end */
511 if (net_device->send_buf_gpadl_handle) {
512 ret = vmbus_teardown_gpadl(net_device->dev->channel,
513 net_device->send_buf_gpadl_handle);
516 * If we failed here, we might as well return and have a leak
517 * rather than continue and a bugchk
519 if (ret != 0) {
520 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
521 "gpadl");
522 return -1;
524 net_device->send_buf_gpadl_handle = 0;
527 if (net_device->send_buf) {
528 /* Free up the receive buffer */
529 free_pages((unsigned long)net_device->send_buf,
530 get_order(net_device->send_buf_size));
531 net_device->send_buf = NULL;
534 return ret;
538 static int netvsc_connect_vsp(struct hv_device *device)
540 int ret;
541 struct netvsc_device *net_device;
542 struct nvsp_message *init_packet;
543 int ndis_version;
545 net_device = get_outbound_net_device(device);
546 if (!net_device) {
547 DPRINT_ERR(NETVSC, "unable to get net device..."
548 "device being destroyed?");
549 return -1;
552 init_packet = &net_device->channel_init_pkt;
554 memset(init_packet, 0, sizeof(struct nvsp_message));
555 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
556 init_packet->msg.init_msg.init.min_protocol_ver =
557 NVSP_MIN_PROTOCOL_VERSION;
558 init_packet->msg.init_msg.init.max_protocol_ver =
559 NVSP_MAX_PROTOCOL_VERSION;
561 /* Send the init request */
562 net_device->wait_condition = 0;
563 ret = vmbus_sendpacket(device->channel, init_packet,
564 sizeof(struct nvsp_message),
565 (unsigned long)init_packet,
566 VM_PKT_DATA_INBAND,
567 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
569 if (ret != 0)
570 goto cleanup;
572 wait_event_timeout(net_device->channel_init_wait,
573 net_device->wait_condition,
574 msecs_to_jiffies(1000));
575 if (net_device->wait_condition == 0) {
576 ret = -ETIMEDOUT;
577 goto cleanup;
580 if (init_packet->msg.init_msg.init_complete.status !=
581 NVSP_STAT_SUCCESS) {
582 ret = -1;
583 goto cleanup;
586 if (init_packet->msg.init_msg.init_complete.
587 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
588 ret = -1;
589 goto cleanup;
591 /* Send the ndis version */
592 memset(init_packet, 0, sizeof(struct nvsp_message));
594 ndis_version = 0x00050000;
596 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
597 init_packet->msg.v1_msg.
598 send_ndis_ver.ndis_major_ver =
599 (ndis_version & 0xFFFF0000) >> 16;
600 init_packet->msg.v1_msg.
601 send_ndis_ver.ndis_minor_ver =
602 ndis_version & 0xFFFF;
604 /* Send the init request */
605 ret = vmbus_sendpacket(device->channel, init_packet,
606 sizeof(struct nvsp_message),
607 (unsigned long)init_packet,
608 VM_PKT_DATA_INBAND, 0);
609 if (ret != 0) {
610 ret = -1;
611 goto cleanup;
614 /* Post the big receive buffer to NetVSP */
615 ret = netvsc_init_recv_buf(device);
616 if (ret == 0)
617 ret = netvsc_init_send_buf(device);
619 cleanup:
620 put_net_device(device);
621 return ret;
624 static void NetVscDisconnectFromVsp(struct netvsc_device *net_device)
626 netvsc_destroy_recv_buf(net_device);
627 netvsc_destroy_send_buf(net_device);
631 * netvsc_device_add - Callback when the device belonging to this
632 * driver is added
634 static int netvsc_device_add(struct hv_device *device, void *additional_info)
636 int ret = 0;
637 int i;
638 struct netvsc_device *net_device;
639 struct hv_netvsc_packet *packet, *pos;
640 struct netvsc_driver *net_driver =
641 (struct netvsc_driver *)device->drv;
643 net_device = alloc_net_device(device);
644 if (!net_device) {
645 ret = -1;
646 goto cleanup;
649 /* Initialize the NetVSC channel extension */
650 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
651 spin_lock_init(&net_device->recv_pkt_list_lock);
653 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
655 INIT_LIST_HEAD(&net_device->recv_pkt_list);
657 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
658 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
659 (NETVSC_RECEIVE_SG_COUNT *
660 sizeof(struct hv_page_buffer)), GFP_KERNEL);
661 if (!packet)
662 break;
664 list_add_tail(&packet->list_ent,
665 &net_device->recv_pkt_list);
667 init_waitqueue_head(&net_device->channel_init_wait);
669 /* Open the channel */
670 ret = vmbus_open(device->channel, net_driver->ring_buf_size,
671 net_driver->ring_buf_size, NULL, 0,
672 netvsc_channel_cb, device);
674 if (ret != 0) {
675 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
676 ret = -1;
677 goto cleanup;
680 /* Channel is opened */
681 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
683 /* Connect with the NetVsp */
684 ret = netvsc_connect_vsp(device);
685 if (ret != 0) {
686 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
687 ret = -1;
688 goto close;
691 return ret;
693 close:
694 /* Now, we can close the channel safely */
695 vmbus_close(device->channel);
697 cleanup:
699 if (net_device) {
700 list_for_each_entry_safe(packet, pos,
701 &net_device->recv_pkt_list,
702 list_ent) {
703 list_del(&packet->list_ent);
704 kfree(packet);
707 release_outbound_net_device(device);
708 release_inbound_net_device(device);
710 free_net_device(net_device);
713 return ret;
717 * netvsc_device_remove - Callback when the root bus device is removed
719 static int netvsc_device_remove(struct hv_device *device)
721 struct netvsc_device *net_device;
722 struct hv_netvsc_packet *netvsc_packet, *pos;
724 /* Stop outbound traffic ie sends and receives completions */
725 net_device = release_outbound_net_device(device);
726 if (!net_device) {
727 DPRINT_ERR(NETVSC, "No net device present!!");
728 return -1;
731 /* Wait for all send completions */
732 while (atomic_read(&net_device->num_outstanding_sends)) {
733 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
734 atomic_read(&net_device->num_outstanding_sends));
735 udelay(100);
738 NetVscDisconnectFromVsp(net_device);
740 /* Stop inbound traffic ie receives and sends completions */
741 net_device = release_inbound_net_device(device);
743 /* At this point, no one should be accessing netDevice except in here */
744 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", net_device);
746 /* Now, we can close the channel safely */
747 vmbus_close(device->channel);
749 /* Release all resources */
750 list_for_each_entry_safe(netvsc_packet, pos,
751 &net_device->recv_pkt_list, list_ent) {
752 list_del(&netvsc_packet->list_ent);
753 kfree(netvsc_packet);
756 free_net_device(net_device);
757 return 0;
761 * netvsc_cleanup - Perform any cleanup when the driver is removed
763 static void netvsc_cleanup(struct hv_driver *drv)
767 static void netvsc_send_completion(struct hv_device *device,
768 struct vmpacket_descriptor *packet)
770 struct netvsc_device *net_device;
771 struct nvsp_message *nvsp_packet;
772 struct hv_netvsc_packet *nvsc_packet;
774 net_device = get_inbound_net_device(device);
775 if (!net_device) {
776 DPRINT_ERR(NETVSC, "unable to get net device..."
777 "device being destroyed?");
778 return;
781 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
782 (packet->offset8 << 3));
784 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
785 (nvsp_packet->hdr.msg_type ==
786 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
787 (nvsp_packet->hdr.msg_type ==
788 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
789 /* Copy the response back */
790 memcpy(&net_device->channel_init_pkt, nvsp_packet,
791 sizeof(struct nvsp_message));
792 net_device->wait_condition = 1;
793 wake_up(&net_device->channel_init_wait);
794 } else if (nvsp_packet->hdr.msg_type ==
795 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
796 /* Get the send context */
797 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
798 packet->trans_id;
800 /* Notify the layer above us */
801 nvsc_packet->completion.send.send_completion(
802 nvsc_packet->completion.send.send_completion_ctx);
804 atomic_dec(&net_device->num_outstanding_sends);
805 } else {
806 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
807 "%d received!!", nvsp_packet->hdr.msg_type);
810 put_net_device(device);
813 static int netvsc_send(struct hv_device *device,
814 struct hv_netvsc_packet *packet)
816 struct netvsc_device *net_device;
817 int ret = 0;
819 struct nvsp_message sendMessage;
821 net_device = get_outbound_net_device(device);
822 if (!net_device) {
823 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
824 "ignoring outbound packets", net_device);
825 return -2;
828 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
829 if (packet->is_data_pkt) {
830 /* 0 is RMC_DATA; */
831 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
832 } else {
833 /* 1 is RMC_CONTROL; */
834 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
837 /* Not using send buffer section */
838 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
839 0xFFFFFFFF;
840 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
842 if (packet->page_buf_cnt) {
843 ret = vmbus_sendpacket_pagebuffer(device->channel,
844 packet->page_buf,
845 packet->page_buf_cnt,
846 &sendMessage,
847 sizeof(struct nvsp_message),
848 (unsigned long)packet);
849 } else {
850 ret = vmbus_sendpacket(device->channel, &sendMessage,
851 sizeof(struct nvsp_message),
852 (unsigned long)packet,
853 VM_PKT_DATA_INBAND,
854 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
858 if (ret != 0)
859 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
860 packet, ret);
862 atomic_inc(&net_device->num_outstanding_sends);
863 put_net_device(device);
864 return ret;
867 static void netvsc_receive(struct hv_device *device,
868 struct vmpacket_descriptor *packet)
870 struct netvsc_device *net_device;
871 struct vmtransfer_page_packet_header *vmxferpage_packet;
872 struct nvsp_message *nvsp_packet;
873 struct hv_netvsc_packet *netvsc_packet = NULL;
874 unsigned long start;
875 unsigned long end, end_virtual;
876 /* struct netvsc_driver *netvscDriver; */
877 struct xferpage_packet *xferpage_packet = NULL;
878 int i, j;
879 int count = 0, bytes_remain = 0;
880 unsigned long flags;
881 LIST_HEAD(listHead);
883 net_device = get_inbound_net_device(device);
884 if (!net_device) {
885 DPRINT_ERR(NETVSC, "unable to get net device..."
886 "device being destroyed?");
887 return;
891 * All inbound packets other than send completion should be xfer page
892 * packet
894 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
895 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
896 packet->type);
897 put_net_device(device);
898 return;
901 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
902 (packet->offset8 << 3));
904 /* Make sure this is a valid nvsp packet */
905 if (nvsp_packet->hdr.msg_type !=
906 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
907 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
908 nvsp_packet->hdr.msg_type);
909 put_net_device(device);
910 return;
913 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
915 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
916 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
917 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
918 vmxferpage_packet->xfer_pageset_id);
919 put_net_device(device);
920 return;
924 * Grab free packets (range count + 1) to represent this xfer
925 * page packet. +1 to represent the xfer page packet itself.
926 * We grab it here so that we know exactly how many we can
927 * fulfil
929 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
930 while (!list_empty(&net_device->recv_pkt_list)) {
931 list_move_tail(net_device->recv_pkt_list.next, &listHead);
932 if (++count == vmxferpage_packet->range_cnt + 1)
933 break;
935 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
938 * We need at least 2 netvsc pkts (1 to represent the xfer
939 * page and at least 1 for the range) i.e. we can handled
940 * some of the xfer page packet ranges...
942 if (count < 2) {
943 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
944 "Dropping this xfer page packet completely!",
945 count, vmxferpage_packet->range_cnt + 1);
947 /* Return it to the freelist */
948 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
949 for (i = count; i != 0; i--) {
950 list_move_tail(listHead.next,
951 &net_device->recv_pkt_list);
953 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
954 flags);
956 netvsc_send_recv_completion(device,
957 vmxferpage_packet->d.trans_id);
959 put_net_device(device);
960 return;
963 /* Remove the 1st packet to represent the xfer page packet itself */
964 xferpage_packet = (struct xferpage_packet *)listHead.next;
965 list_del(&xferpage_packet->list_ent);
967 /* This is how much we can satisfy */
968 xferpage_packet->count = count - 1;
970 if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
971 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
972 "page...got %d", vmxferpage_packet->range_cnt,
973 xferpage_packet->count);
976 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
977 for (i = 0; i < (count - 1); i++) {
978 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
979 list_del(&netvsc_packet->list_ent);
981 /* Initialize the netvsc packet */
982 netvsc_packet->xfer_page_pkt = xferpage_packet;
983 netvsc_packet->completion.recv.recv_completion =
984 netvsc_receive_completion;
985 netvsc_packet->completion.recv.recv_completion_ctx =
986 netvsc_packet;
987 netvsc_packet->device = device;
988 /* Save this so that we can send it back */
989 netvsc_packet->completion.recv.recv_completion_tid =
990 vmxferpage_packet->d.trans_id;
992 netvsc_packet->total_data_buflen =
993 vmxferpage_packet->ranges[i].byte_count;
994 netvsc_packet->page_buf_cnt = 1;
996 netvsc_packet->page_buf[0].len =
997 vmxferpage_packet->ranges[i].byte_count;
999 start = virt_to_phys((void *)((unsigned long)net_device->
1000 recv_buf + vmxferpage_packet->ranges[i].byte_offset));
1002 netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
1003 end_virtual = (unsigned long)net_device->recv_buf
1004 + vmxferpage_packet->ranges[i].byte_offset
1005 + vmxferpage_packet->ranges[i].byte_count - 1;
1006 end = virt_to_phys((void *)end_virtual);
1008 /* Calculate the page relative offset */
1009 netvsc_packet->page_buf[0].offset =
1010 vmxferpage_packet->ranges[i].byte_offset &
1011 (PAGE_SIZE - 1);
1012 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1013 /* Handle frame across multiple pages: */
1014 netvsc_packet->page_buf[0].len =
1015 (netvsc_packet->page_buf[0].pfn <<
1016 PAGE_SHIFT)
1017 + PAGE_SIZE - start;
1018 bytes_remain = netvsc_packet->total_data_buflen -
1019 netvsc_packet->page_buf[0].len;
1020 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1021 netvsc_packet->page_buf[j].offset = 0;
1022 if (bytes_remain <= PAGE_SIZE) {
1023 netvsc_packet->page_buf[j].len =
1024 bytes_remain;
1025 bytes_remain = 0;
1026 } else {
1027 netvsc_packet->page_buf[j].len =
1028 PAGE_SIZE;
1029 bytes_remain -= PAGE_SIZE;
1031 netvsc_packet->page_buf[j].pfn =
1032 virt_to_phys((void *)(end_virtual -
1033 bytes_remain)) >> PAGE_SHIFT;
1034 netvsc_packet->page_buf_cnt++;
1035 if (bytes_remain == 0)
1036 break;
1040 /* Pass it to the upper layer */
1041 ((struct netvsc_driver *)device->drv)->
1042 recv_cb(device, netvsc_packet);
1044 netvsc_receive_completion(netvsc_packet->
1045 completion.recv.recv_completion_ctx);
1048 put_net_device(device);
1051 static void netvsc_send_recv_completion(struct hv_device *device,
1052 u64 transaction_id)
1054 struct nvsp_message recvcompMessage;
1055 int retries = 0;
1056 int ret;
1058 recvcompMessage.hdr.msg_type =
1059 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
1061 /* FIXME: Pass in the status */
1062 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
1063 NVSP_STAT_SUCCESS;
1065 retry_send_cmplt:
1066 /* Send the completion */
1067 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
1068 sizeof(struct nvsp_message), transaction_id,
1069 VM_PKT_COMP, 0);
1070 if (ret == 0) {
1071 /* success */
1072 /* no-op */
1073 } else if (ret == -1) {
1074 /* no more room...wait a bit and attempt to retry 3 times */
1075 retries++;
1076 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1077 "(tid %llx)...retrying %d", transaction_id, retries);
1079 if (retries < 4) {
1080 udelay(100);
1081 goto retry_send_cmplt;
1082 } else {
1083 DPRINT_ERR(NETVSC, "unable to send receive completion "
1084 "pkt (tid %llx)...give up retrying",
1085 transaction_id);
1087 } else {
1088 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1089 "%llx", transaction_id);
1093 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1094 static void netvsc_receive_completion(void *context)
1096 struct hv_netvsc_packet *packet = context;
1097 struct hv_device *device = (struct hv_device *)packet->device;
1098 struct netvsc_device *net_device;
1099 u64 transaction_id = 0;
1100 bool fsend_receive_comp = false;
1101 unsigned long flags;
1104 * Even though it seems logical to do a GetOutboundNetDevice() here to
1105 * send out receive completion, we are using GetInboundNetDevice()
1106 * since we may have disable outbound traffic already.
1108 net_device = get_inbound_net_device(device);
1109 if (!net_device) {
1110 DPRINT_ERR(NETVSC, "unable to get net device..."
1111 "device being destroyed?");
1112 return;
1115 /* Overloading use of the lock. */
1116 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1118 packet->xfer_page_pkt->count--;
1121 * Last one in the line that represent 1 xfer page packet.
1122 * Return the xfer page packet itself to the freelist
1124 if (packet->xfer_page_pkt->count == 0) {
1125 fsend_receive_comp = true;
1126 transaction_id = packet->completion.recv.recv_completion_tid;
1127 list_add_tail(&packet->xfer_page_pkt->list_ent,
1128 &net_device->recv_pkt_list);
1132 /* Put the packet back */
1133 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
1134 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
1136 /* Send a receive completion for the xfer page packet */
1137 if (fsend_receive_comp)
1138 netvsc_send_recv_completion(device, transaction_id);
1140 put_net_device(device);
1143 static void netvsc_channel_cb(void *context)
1145 int ret;
1146 struct hv_device *device = context;
1147 struct netvsc_device *net_device;
1148 u32 bytes_recvd;
1149 u64 request_id;
1150 unsigned char *packet;
1151 struct vmpacket_descriptor *desc;
1152 unsigned char *buffer;
1153 int bufferlen = NETVSC_PACKET_SIZE;
1155 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1156 GFP_ATOMIC);
1157 if (!packet)
1158 return;
1159 buffer = packet;
1161 net_device = get_inbound_net_device(device);
1162 if (!net_device) {
1163 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1164 "ignoring inbound packets", net_device);
1165 goto out;
1168 do {
1169 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
1170 &bytes_recvd, &request_id);
1171 if (ret == 0) {
1172 if (bytes_recvd > 0) {
1173 desc = (struct vmpacket_descriptor *)buffer;
1174 switch (desc->type) {
1175 case VM_PKT_COMP:
1176 netvsc_send_completion(device, desc);
1177 break;
1179 case VM_PKT_DATA_USING_XFER_PAGES:
1180 netvsc_receive(device, desc);
1181 break;
1183 default:
1184 DPRINT_ERR(NETVSC,
1185 "unhandled packet type %d, "
1186 "tid %llx len %d\n",
1187 desc->type, request_id,
1188 bytes_recvd);
1189 break;
1192 /* reset */
1193 if (bufferlen > NETVSC_PACKET_SIZE) {
1194 kfree(buffer);
1195 buffer = packet;
1196 bufferlen = NETVSC_PACKET_SIZE;
1198 } else {
1199 /* reset */
1200 if (bufferlen > NETVSC_PACKET_SIZE) {
1201 kfree(buffer);
1202 buffer = packet;
1203 bufferlen = NETVSC_PACKET_SIZE;
1206 break;
1208 } else if (ret == -2) {
1209 /* Handle large packet */
1210 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1211 if (buffer == NULL) {
1212 /* Try again next time around */
1213 DPRINT_ERR(NETVSC,
1214 "unable to allocate buffer of size "
1215 "(%d)!!", bytes_recvd);
1216 break;
1219 bufferlen = bytes_recvd;
1221 } while (1);
1223 put_net_device(device);
1224 out:
1225 kfree(buffer);
1226 return;