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
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.
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>
25 #include <linux/delay.h>
27 #include <linux/slab.h>
31 #include "rndis_filter.h"
36 static const char *driver_name = "netvsc";
38 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
39 static const struct hv_guid netvsc_device_type = {
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,
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);
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;
96 static void free_net_device(struct netvsc_device *device)
98 WARN_ON(atomic_read(&device->refcnt) == 0);
99 device->dev->ext = NULL;
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);
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);
132 static void put_net_device(struct hv_device *device)
134 struct netvsc_device *net_device;
136 net_device = device->ext;
137 /* ASSERT(netDevice); */
139 atomic_dec(&net_device->refcnt);
142 static struct netvsc_device *release_outbound_net_device(
143 struct hv_device *device)
145 struct netvsc_device *net_device;
147 net_device = device->ext;
148 if (net_device == NULL)
151 /* Busy wait until the ref drop to 2, then set it to 1 */
152 while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2)
158 static struct netvsc_device *release_inbound_net_device(
159 struct hv_device *device)
161 struct netvsc_device *net_device;
163 net_device = device->ext;
164 if (net_device == NULL)
167 /* Busy wait until the ref drop to 1, then set it to 0 */
168 while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
176 * netvsc_initialize - Main entry point
178 int netvsc_initialize(struct hv_driver *drv)
180 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
182 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
183 "sizeof(struct nvsp_message)=%zd, "
184 "sizeof(struct vmtransfer_page_packet_header)=%zd",
185 sizeof(struct hv_netvsc_packet),
186 sizeof(struct nvsp_message),
187 sizeof(struct vmtransfer_page_packet_header));
189 /* Make sure we are at least 2 pages since 1 page is used for control */
190 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
192 drv->name = driver_name;
193 memcpy(&drv->dev_type, &netvsc_device_type, sizeof(struct hv_guid));
195 /* Make sure it is set by the caller */
196 /* FIXME: These probably should still be tested in some way */
197 /* ASSERT(driver->OnReceiveCallback); */
198 /* ASSERT(driver->OnLinkStatusChanged); */
200 /* Setup the dispatch table */
201 driver->base.dev_add = netvsc_device_add;
202 driver->base.dev_rm = netvsc_device_remove;
203 driver->base.cleanup = netvsc_cleanup;
205 driver->send = netvsc_send;
207 rndis_filter_init(driver);
211 static int netvsc_init_recv_buf(struct hv_device *device)
214 struct netvsc_device *net_device;
215 struct nvsp_message *init_packet;
217 net_device = get_outbound_net_device(device);
219 DPRINT_ERR(NETVSC, "unable to get net device..."
220 "device being destroyed?");
223 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
224 /* page-size grandularity */
225 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
227 net_device->recv_buf =
228 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
229 get_order(net_device->recv_buf_size));
230 if (!net_device->recv_buf) {
232 "unable to allocate receive buffer of size %d",
233 net_device->recv_buf_size);
237 /* page-aligned buffer */
238 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
241 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
244 * Establish the gpadl handle for this buffer on this
245 * channel. Note: This call uses the vmbus connection rather
246 * than the channel to establish the gpadl handle.
248 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
249 net_device->recv_buf_size,
250 &net_device->recv_buf_gpadl_handle);
253 "unable to establish receive buffer's gpadl");
258 /* Notify the NetVsp of the gpadl handle */
259 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
261 init_packet = &net_device->channel_init_pkt;
263 memset(init_packet, 0, sizeof(struct nvsp_message));
265 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
266 init_packet->msg.v1_msg.send_recv_buf.
267 gpadl_handle = net_device->recv_buf_gpadl_handle;
268 init_packet->msg.v1_msg.
269 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
271 /* Send the gpadl notification request */
272 net_device->wait_condition = 0;
273 ret = vmbus_sendpacket(device->channel, init_packet,
274 sizeof(struct nvsp_message),
275 (unsigned long)init_packet,
277 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
280 "unable to send receive buffer's gpadl to netvsp");
284 wait_event_timeout(net_device->channel_init_wait,
285 net_device->wait_condition,
286 msecs_to_jiffies(1000));
287 BUG_ON(net_device->wait_condition == 0);
290 /* Check the response */
291 if (init_packet->msg.v1_msg.
292 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
293 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
294 "initialzation with NetVsp - status %d",
295 init_packet->msg.v1_msg.
296 send_recv_buf_complete.status);
301 /* Parse the response */
302 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
303 /* ASSERT(netDevice->ReceiveSections == NULL); */
305 net_device->recv_section_cnt = init_packet->msg.
306 v1_msg.send_recv_buf_complete.num_sections;
308 net_device->recv_section = kmalloc(net_device->recv_section_cnt
309 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
310 if (net_device->recv_section == NULL) {
315 memcpy(net_device->recv_section,
316 init_packet->msg.v1_msg.
317 send_recv_buf_complete.sections,
318 net_device->recv_section_cnt *
319 sizeof(struct nvsp_1_receive_buffer_section));
321 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
322 "endoffset %d, suballoc size %d, num suballocs %d)",
323 net_device->recv_section_cnt,
324 net_device->recv_section[0].offset,
325 net_device->recv_section[0].end_offset,
326 net_device->recv_section[0].sub_alloc_size,
327 net_device->recv_section[0].num_sub_allocs);
330 * For 1st release, there should only be 1 section that represents the
331 * entire receive buffer
333 if (net_device->recv_section_cnt != 1 ||
334 net_device->recv_section->offset != 0) {
342 netvsc_destroy_recv_buf(net_device);
345 put_net_device(device);
349 static int netvsc_init_send_buf(struct hv_device *device)
352 struct netvsc_device *net_device;
353 struct nvsp_message *init_packet;
355 net_device = get_outbound_net_device(device);
357 DPRINT_ERR(NETVSC, "unable to get net device..."
358 "device being destroyed?");
361 if (net_device->send_buf_size <= 0) {
366 /* page-size grandularity */
367 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
369 net_device->send_buf =
370 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
371 get_order(net_device->send_buf_size));
372 if (!net_device->send_buf) {
373 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
374 net_device->send_buf_size);
378 /* page-aligned buffer */
379 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
381 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
384 * Establish the gpadl handle for this buffer on this
385 * channel. Note: This call uses the vmbus connection rather
386 * than the channel to establish the gpadl handle.
388 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
389 net_device->send_buf_size,
390 &net_device->send_buf_gpadl_handle);
392 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
396 /* Notify the NetVsp of the gpadl handle */
397 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
399 init_packet = &net_device->channel_init_pkt;
401 memset(init_packet, 0, sizeof(struct nvsp_message));
403 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
404 init_packet->msg.v1_msg.send_recv_buf.
405 gpadl_handle = net_device->send_buf_gpadl_handle;
406 init_packet->msg.v1_msg.send_recv_buf.id =
407 NETVSC_SEND_BUFFER_ID;
409 /* Send the gpadl notification request */
410 net_device->wait_condition = 0;
411 ret = vmbus_sendpacket(device->channel, init_packet,
412 sizeof(struct nvsp_message),
413 (unsigned long)init_packet,
415 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
418 "unable to send receive buffer's gpadl to netvsp");
422 wait_event_timeout(net_device->channel_init_wait,
423 net_device->wait_condition,
424 msecs_to_jiffies(1000));
425 BUG_ON(net_device->wait_condition == 0);
427 /* Check the response */
428 if (init_packet->msg.v1_msg.
429 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
430 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
431 "initialzation with NetVsp - status %d",
432 init_packet->msg.v1_msg.
433 send_send_buf_complete.status);
438 net_device->send_section_size = init_packet->
439 msg.v1_msg.send_send_buf_complete.section_size;
444 netvsc_destroy_send_buf(net_device);
447 put_net_device(device);
451 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
453 struct nvsp_message *revoke_packet;
457 * If we got a section count, it means we received a
458 * SendReceiveBufferComplete msg (ie sent
459 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
460 * to send a revoke msg here
462 if (net_device->recv_section_cnt) {
464 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
466 /* Send the revoke receive buffer */
467 revoke_packet = &net_device->revoke_packet;
468 memset(revoke_packet, 0, sizeof(struct nvsp_message));
470 revoke_packet->hdr.msg_type =
471 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
472 revoke_packet->msg.v1_msg.
473 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
475 ret = vmbus_sendpacket(net_device->dev->channel,
477 sizeof(struct nvsp_message),
478 (unsigned long)revoke_packet,
479 VM_PKT_DATA_INBAND, 0);
481 * If we failed here, we might as well return and
482 * have a leak rather than continue and a bugchk
485 DPRINT_ERR(NETVSC, "unable to send revoke receive "
491 /* Teardown the gpadl on the vsp end */
492 if (net_device->recv_buf_gpadl_handle) {
493 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
495 ret = vmbus_teardown_gpadl(net_device->dev->channel,
496 net_device->recv_buf_gpadl_handle);
498 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
501 "unable to teardown receive buffer's gpadl");
504 net_device->recv_buf_gpadl_handle = 0;
507 if (net_device->recv_buf) {
508 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
510 /* Free up the receive buffer */
511 free_pages((unsigned long)net_device->recv_buf,
512 get_order(net_device->recv_buf_size));
513 net_device->recv_buf = NULL;
516 if (net_device->recv_section) {
517 net_device->recv_section_cnt = 0;
518 kfree(net_device->recv_section);
519 net_device->recv_section = NULL;
525 static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
527 struct nvsp_message *revoke_packet;
531 * If we got a section count, it means we received a
532 * SendReceiveBufferComplete msg (ie sent
533 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
534 * to send a revoke msg here
536 if (net_device->send_section_size) {
538 "Sending NvspMessage1TypeRevokeSendBuffer...");
540 /* Send the revoke send buffer */
541 revoke_packet = &net_device->revoke_packet;
542 memset(revoke_packet, 0, sizeof(struct nvsp_message));
544 revoke_packet->hdr.msg_type =
545 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
546 revoke_packet->msg.v1_msg.
547 revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
549 ret = vmbus_sendpacket(net_device->dev->channel,
551 sizeof(struct nvsp_message),
552 (unsigned long)revoke_packet,
553 VM_PKT_DATA_INBAND, 0);
555 * If we failed here, we might as well return and have a leak
556 * rather than continue and a bugchk
559 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
565 /* Teardown the gpadl on the vsp end */
566 if (net_device->send_buf_gpadl_handle) {
567 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
568 ret = vmbus_teardown_gpadl(net_device->dev->channel,
569 net_device->send_buf_gpadl_handle);
572 * If we failed here, we might as well return and have a leak
573 * rather than continue and a bugchk
576 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
580 net_device->send_buf_gpadl_handle = 0;
583 if (net_device->send_buf) {
584 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
586 /* Free up the receive buffer */
587 free_pages((unsigned long)net_device->send_buf,
588 get_order(net_device->send_buf_size));
589 net_device->send_buf = NULL;
596 static int netvsc_connect_vsp(struct hv_device *device)
599 struct netvsc_device *net_device;
600 struct nvsp_message *init_packet;
603 net_device = get_outbound_net_device(device);
605 DPRINT_ERR(NETVSC, "unable to get net device..."
606 "device being destroyed?");
610 init_packet = &net_device->channel_init_pkt;
612 memset(init_packet, 0, sizeof(struct nvsp_message));
613 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
614 init_packet->msg.init_msg.init.min_protocol_ver =
615 NVSP_MIN_PROTOCOL_VERSION;
616 init_packet->msg.init_msg.init.max_protocol_ver =
617 NVSP_MAX_PROTOCOL_VERSION;
619 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
621 /* Send the init request */
622 net_device->wait_condition = 0;
623 ret = vmbus_sendpacket(device->channel, init_packet,
624 sizeof(struct nvsp_message),
625 (unsigned long)init_packet,
627 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
630 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
634 wait_event_timeout(net_device->channel_init_wait,
635 net_device->wait_condition,
636 msecs_to_jiffies(1000));
637 if (net_device->wait_condition == 0) {
642 /* Now, check the response */
643 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
644 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
645 init_packet->msg.init_msg.init_complete.status,
646 init_packet->msg.init_msg.
647 init_complete.max_mdl_chain_len);
649 if (init_packet->msg.init_msg.init_complete.status !=
652 "unable to initialize with netvsp (status 0x%x)",
653 init_packet->msg.init_msg.init_complete.status);
658 if (init_packet->msg.init_msg.init_complete.
659 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
660 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
661 "(version expected 1 got %d)",
662 init_packet->msg.init_msg.
663 init_complete.negotiated_protocol_ver);
667 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
669 /* Send the ndis version */
670 memset(init_packet, 0, sizeof(struct nvsp_message));
672 ndis_version = 0x00050000;
674 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
675 init_packet->msg.v1_msg.
676 send_ndis_ver.ndis_major_ver =
677 (ndis_version & 0xFFFF0000) >> 16;
678 init_packet->msg.v1_msg.
679 send_ndis_ver.ndis_minor_ver =
680 ndis_version & 0xFFFF;
682 /* Send the init request */
683 ret = vmbus_sendpacket(device->channel, init_packet,
684 sizeof(struct nvsp_message),
685 (unsigned long)init_packet,
686 VM_PKT_DATA_INBAND, 0);
689 "unable to send NvspMessage1TypeSendNdisVersion");
694 /* Post the big receive buffer to NetVSP */
695 ret = netvsc_init_recv_buf(device);
697 ret = netvsc_init_send_buf(device);
700 put_net_device(device);
704 static void NetVscDisconnectFromVsp(struct netvsc_device *net_device)
706 netvsc_destroy_recv_buf(net_device);
707 netvsc_destroy_send_buf(net_device);
711 * netvsc_device_add - Callback when the device belonging to this
714 static int netvsc_device_add(struct hv_device *device, void *additional_info)
718 struct netvsc_device *net_device;
719 struct hv_netvsc_packet *packet, *pos;
720 struct netvsc_driver *net_driver =
721 (struct netvsc_driver *)device->drv;
723 net_device = alloc_net_device(device);
729 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", net_device);
731 /* Initialize the NetVSC channel extension */
732 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
733 spin_lock_init(&net_device->recv_pkt_list_lock);
735 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
737 INIT_LIST_HEAD(&net_device->recv_pkt_list);
739 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
740 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
741 (NETVSC_RECEIVE_SG_COUNT *
742 sizeof(struct hv_page_buffer)), GFP_KERNEL);
744 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
745 "for receive pool (wanted %d got %d)",
746 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
749 list_add_tail(&packet->list_ent,
750 &net_device->recv_pkt_list);
752 init_waitqueue_head(&net_device->channel_init_wait);
754 /* Open the channel */
755 ret = vmbus_open(device->channel, net_driver->ring_buf_size,
756 net_driver->ring_buf_size, NULL, 0,
757 netvsc_channel_cb, device);
760 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
765 /* Channel is opened */
766 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
768 /* Connect with the NetVsp */
769 ret = netvsc_connect_vsp(device);
771 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
776 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
782 /* Now, we can close the channel safely */
783 vmbus_close(device->channel);
788 list_for_each_entry_safe(packet, pos,
789 &net_device->recv_pkt_list,
791 list_del(&packet->list_ent);
795 release_outbound_net_device(device);
796 release_inbound_net_device(device);
798 free_net_device(net_device);
805 * netvsc_device_remove - Callback when the root bus device is removed
807 static int netvsc_device_remove(struct hv_device *device)
809 struct netvsc_device *net_device;
810 struct hv_netvsc_packet *netvsc_packet, *pos;
812 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
815 /* Stop outbound traffic ie sends and receives completions */
816 net_device = release_outbound_net_device(device);
818 DPRINT_ERR(NETVSC, "No net device present!!");
822 /* Wait for all send completions */
823 while (atomic_read(&net_device->num_outstanding_sends)) {
824 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
825 atomic_read(&net_device->num_outstanding_sends));
829 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
831 NetVscDisconnectFromVsp(net_device);
833 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
836 /* Stop inbound traffic ie receives and sends completions */
837 net_device = release_inbound_net_device(device);
839 /* At this point, no one should be accessing netDevice except in here */
840 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", net_device);
842 /* Now, we can close the channel safely */
843 vmbus_close(device->channel);
845 /* Release all resources */
846 list_for_each_entry_safe(netvsc_packet, pos,
847 &net_device->recv_pkt_list, list_ent) {
848 list_del(&netvsc_packet->list_ent);
849 kfree(netvsc_packet);
852 free_net_device(net_device);
857 * netvsc_cleanup - Perform any cleanup when the driver is removed
859 static void netvsc_cleanup(struct hv_driver *drv)
863 static void netvsc_send_completion(struct hv_device *device,
864 struct vmpacket_descriptor *packet)
866 struct netvsc_device *net_device;
867 struct nvsp_message *nvsp_packet;
868 struct hv_netvsc_packet *nvsc_packet;
870 net_device = get_inbound_net_device(device);
872 DPRINT_ERR(NETVSC, "unable to get net device..."
873 "device being destroyed?");
877 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
878 (packet->offset8 << 3));
880 DPRINT_DBG(NETVSC, "send completion packet - type %d",
881 nvsp_packet->hdr.msg_type);
883 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
884 (nvsp_packet->hdr.msg_type ==
885 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
886 (nvsp_packet->hdr.msg_type ==
887 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
888 /* Copy the response back */
889 memcpy(&net_device->channel_init_pkt, nvsp_packet,
890 sizeof(struct nvsp_message));
891 net_device->wait_condition = 1;
892 wake_up(&net_device->channel_init_wait);
893 } else if (nvsp_packet->hdr.msg_type ==
894 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
895 /* Get the send context */
896 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
898 /* ASSERT(nvscPacket); */
900 /* Notify the layer above us */
901 nvsc_packet->completion.send.send_completion(
902 nvsc_packet->completion.send.send_completion_ctx);
904 atomic_dec(&net_device->num_outstanding_sends);
906 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
907 "%d received!!", nvsp_packet->hdr.msg_type);
910 put_net_device(device);
913 static int netvsc_send(struct hv_device *device,
914 struct hv_netvsc_packet *packet)
916 struct netvsc_device *net_device;
919 struct nvsp_message sendMessage;
921 net_device = get_outbound_net_device(device);
923 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
924 "ignoring outbound packets", net_device);
928 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
929 if (packet->is_data_pkt) {
931 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
933 /* 1 is RMC_CONTROL; */
934 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
937 /* Not using send buffer section */
938 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
940 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
942 if (packet->page_buf_cnt) {
943 ret = vmbus_sendpacket_pagebuffer(device->channel,
945 packet->page_buf_cnt,
947 sizeof(struct nvsp_message),
948 (unsigned long)packet);
950 ret = vmbus_sendpacket(device->channel, &sendMessage,
951 sizeof(struct nvsp_message),
952 (unsigned long)packet,
954 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
959 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
962 atomic_inc(&net_device->num_outstanding_sends);
963 put_net_device(device);
967 static void netvsc_receive(struct hv_device *device,
968 struct vmpacket_descriptor *packet)
970 struct netvsc_device *net_device;
971 struct vmtransfer_page_packet_header *vmxferpage_packet;
972 struct nvsp_message *nvsp_packet;
973 struct hv_netvsc_packet *netvsc_packet = NULL;
975 unsigned long end, end_virtual;
976 /* struct netvsc_driver *netvscDriver; */
977 struct xferpage_packet *xferpage_packet = NULL;
979 int count = 0, bytes_remain = 0;
983 net_device = get_inbound_net_device(device);
985 DPRINT_ERR(NETVSC, "unable to get net device..."
986 "device being destroyed?");
991 * All inbound packets other than send completion should be xfer page
994 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
995 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
997 put_net_device(device);
1001 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
1002 (packet->offset8 << 3));
1004 /* Make sure this is a valid nvsp packet */
1005 if (nvsp_packet->hdr.msg_type !=
1006 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
1007 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1008 nvsp_packet->hdr.msg_type);
1009 put_net_device(device);
1013 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1014 nvsp_packet->hdr.msg_type);
1016 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
1018 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
1019 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1020 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1021 vmxferpage_packet->xfer_pageset_id);
1022 put_net_device(device);
1026 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1027 vmxferpage_packet->range_cnt);
1030 * Grab free packets (range count + 1) to represent this xfer
1031 * page packet. +1 to represent the xfer page packet itself.
1032 * We grab it here so that we know exactly how many we can
1035 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1036 while (!list_empty(&net_device->recv_pkt_list)) {
1037 list_move_tail(net_device->recv_pkt_list.next, &listHead);
1038 if (++count == vmxferpage_packet->range_cnt + 1)
1041 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
1044 * We need at least 2 netvsc pkts (1 to represent the xfer
1045 * page and at least 1 for the range) i.e. we can handled
1046 * some of the xfer page packet ranges...
1049 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1050 "Dropping this xfer page packet completely!",
1051 count, vmxferpage_packet->range_cnt + 1);
1053 /* Return it to the freelist */
1054 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1055 for (i = count; i != 0; i--) {
1056 list_move_tail(listHead.next,
1057 &net_device->recv_pkt_list);
1059 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
1062 netvsc_send_recv_completion(device,
1063 vmxferpage_packet->d.trans_id);
1065 put_net_device(device);
1069 /* Remove the 1st packet to represent the xfer page packet itself */
1070 xferpage_packet = (struct xferpage_packet *)listHead.next;
1071 list_del(&xferpage_packet->list_ent);
1073 /* This is how much we can satisfy */
1074 xferpage_packet->count = count - 1;
1075 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1076 /* vmxferpagePacket->RangeCount); */
1078 if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
1079 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1080 "page...got %d", vmxferpage_packet->range_cnt,
1081 xferpage_packet->count);
1084 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1085 for (i = 0; i < (count - 1); i++) {
1086 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
1087 list_del(&netvsc_packet->list_ent);
1089 /* Initialize the netvsc packet */
1090 netvsc_packet->xfer_page_pkt = xferpage_packet;
1091 netvsc_packet->completion.recv.recv_completion =
1092 netvsc_receive_completion;
1093 netvsc_packet->completion.recv.recv_completion_ctx =
1095 netvsc_packet->device = device;
1096 /* Save this so that we can send it back */
1097 netvsc_packet->completion.recv.recv_completion_tid =
1098 vmxferpage_packet->d.trans_id;
1100 netvsc_packet->total_data_buflen =
1101 vmxferpage_packet->ranges[i].byte_count;
1102 netvsc_packet->page_buf_cnt = 1;
1104 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1105 /* vmxferpagePacket->Ranges[i].ByteCount < */
1106 /* netDevice->ReceiveBufferSize); */
1108 netvsc_packet->page_buf[0].len =
1109 vmxferpage_packet->ranges[i].byte_count;
1111 start = virt_to_phys((void *)((unsigned long)net_device->
1112 recv_buf + vmxferpage_packet->ranges[i].byte_offset));
1114 netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
1115 end_virtual = (unsigned long)net_device->recv_buf
1116 + vmxferpage_packet->ranges[i].byte_offset
1117 + vmxferpage_packet->ranges[i].byte_count - 1;
1118 end = virt_to_phys((void *)end_virtual);
1120 /* Calculate the page relative offset */
1121 netvsc_packet->page_buf[0].offset =
1122 vmxferpage_packet->ranges[i].byte_offset &
1124 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1125 /* Handle frame across multiple pages: */
1126 netvsc_packet->page_buf[0].len =
1127 (netvsc_packet->page_buf[0].pfn <<
1129 + PAGE_SIZE - start;
1130 bytes_remain = netvsc_packet->total_data_buflen -
1131 netvsc_packet->page_buf[0].len;
1132 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1133 netvsc_packet->page_buf[j].offset = 0;
1134 if (bytes_remain <= PAGE_SIZE) {
1135 netvsc_packet->page_buf[j].len =
1139 netvsc_packet->page_buf[j].len =
1141 bytes_remain -= PAGE_SIZE;
1143 netvsc_packet->page_buf[j].pfn =
1144 virt_to_phys((void *)(end_virtual -
1145 bytes_remain)) >> PAGE_SHIFT;
1146 netvsc_packet->page_buf_cnt++;
1147 if (bytes_remain == 0)
1150 /* ASSERT(bytesRemain == 0); */
1152 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1153 "(pfn %llx, offset %u, len %u)", i,
1154 vmxferpage_packet->ranges[i].byte_offset,
1155 vmxferpage_packet->ranges[i].byte_count,
1156 netvsc_packet->page_buf[0].pfn,
1157 netvsc_packet->page_buf[0].offset,
1158 netvsc_packet->page_buf[0].len);
1160 /* Pass it to the upper layer */
1161 ((struct netvsc_driver *)device->drv)->
1162 recv_cb(device, netvsc_packet);
1164 netvsc_receive_completion(netvsc_packet->
1165 completion.recv.recv_completion_ctx);
1168 /* ASSERT(list_empty(&listHead)); */
1170 put_net_device(device);
1173 static void netvsc_send_recv_completion(struct hv_device *device,
1176 struct nvsp_message recvcompMessage;
1180 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1183 recvcompMessage.hdr.msg_type =
1184 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
1186 /* FIXME: Pass in the status */
1187 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
1191 /* Send the completion */
1192 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
1193 sizeof(struct nvsp_message), transaction_id,
1198 } else if (ret == -1) {
1199 /* no more room...wait a bit and attempt to retry 3 times */
1201 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1202 "(tid %llx)...retrying %d", transaction_id, retries);
1206 goto retry_send_cmplt;
1208 DPRINT_ERR(NETVSC, "unable to send receive completion "
1209 "pkt (tid %llx)...give up retrying",
1213 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1214 "%llx", transaction_id);
1218 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1219 static void netvsc_receive_completion(void *context)
1221 struct hv_netvsc_packet *packet = context;
1222 struct hv_device *device = (struct hv_device *)packet->device;
1223 struct netvsc_device *net_device;
1224 u64 transaction_id = 0;
1225 bool fsend_receive_comp = false;
1226 unsigned long flags;
1228 /* ASSERT(packet->XferPagePacket); */
1231 * Even though it seems logical to do a GetOutboundNetDevice() here to
1232 * send out receive completion, we are using GetInboundNetDevice()
1233 * since we may have disable outbound traffic already.
1235 net_device = get_inbound_net_device(device);
1237 DPRINT_ERR(NETVSC, "unable to get net device..."
1238 "device being destroyed?");
1242 /* Overloading use of the lock. */
1243 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1245 /* ASSERT(packet->XferPagePacket->Count > 0); */
1246 packet->xfer_page_pkt->count--;
1249 * Last one in the line that represent 1 xfer page packet.
1250 * Return the xfer page packet itself to the freelist
1252 if (packet->xfer_page_pkt->count == 0) {
1253 fsend_receive_comp = true;
1254 transaction_id = packet->completion.recv.recv_completion_tid;
1255 list_add_tail(&packet->xfer_page_pkt->list_ent,
1256 &net_device->recv_pkt_list);
1260 /* Put the packet back */
1261 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
1262 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
1264 /* Send a receive completion for the xfer page packet */
1265 if (fsend_receive_comp)
1266 netvsc_send_recv_completion(device, transaction_id);
1268 put_net_device(device);
1271 static void netvsc_channel_cb(void *context)
1274 struct hv_device *device = context;
1275 struct netvsc_device *net_device;
1278 unsigned char *packet;
1279 struct vmpacket_descriptor *desc;
1280 unsigned char *buffer;
1281 int bufferlen = NETVSC_PACKET_SIZE;
1283 /* ASSERT(device); */
1285 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1291 net_device = get_inbound_net_device(device);
1293 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1294 "ignoring inbound packets", net_device);
1299 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
1300 &bytes_recvd, &request_id);
1302 if (bytes_recvd > 0) {
1303 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1304 bytes_recvd, request_id);
1306 desc = (struct vmpacket_descriptor *)buffer;
1307 switch (desc->type) {
1309 netvsc_send_completion(device, desc);
1312 case VM_PKT_DATA_USING_XFER_PAGES:
1313 netvsc_receive(device, desc);
1318 "unhandled packet type %d, "
1319 "tid %llx len %d\n",
1320 desc->type, request_id,
1326 if (bufferlen > NETVSC_PACKET_SIZE) {
1329 bufferlen = NETVSC_PACKET_SIZE;
1333 if (bufferlen > NETVSC_PACKET_SIZE) {
1336 bufferlen = NETVSC_PACKET_SIZE;
1341 } else if (ret == -2) {
1342 /* Handle large packet */
1343 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1344 if (buffer == NULL) {
1345 /* Try again next time around */
1347 "unable to allocate buffer of size "
1348 "(%d)!!", bytes_recvd);
1352 bufferlen = bytes_recvd;
1356 put_net_device(device);