Merge remote-tracking branch 'pm/linux-next'
[linux-2.6/next.git] / drivers / staging / hv / netvsc.c
blobdc5e5c488e3b139e6bcb9bd83186b6b6ea9d615f
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
26 #include <linux/mm.h>
27 #include <linux/delay.h>
28 #include <linux/io.h>
29 #include <linux/slab.h>
31 #include "hyperv.h"
32 #include "hyperv_net.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
47 static struct netvsc_device *alloc_net_device(struct hv_device *device)
49 struct netvsc_device *net_device;
51 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
52 if (!net_device)
53 return NULL;
55 /* Set to 2 to allow both inbound and outbound traffic */
56 atomic_cmpxchg(&net_device->refcnt, 0, 2);
58 net_device->dev = device;
59 device->ext = net_device;
61 return net_device;
64 static void free_net_device(struct netvsc_device *device)
66 WARN_ON(atomic_read(&device->refcnt) != 0);
67 device->dev->ext = NULL;
68 kfree(device);
72 /* Get the net device object iff exists and its refcount > 1 */
73 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
75 struct netvsc_device *net_device;
77 net_device = device->ext;
78 if (net_device && atomic_read(&net_device->refcnt) > 1)
79 atomic_inc(&net_device->refcnt);
80 else
81 net_device = NULL;
83 return net_device;
86 /* Get the net device object iff exists and its refcount > 0 */
87 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
89 struct netvsc_device *net_device;
91 net_device = device->ext;
92 if (net_device && atomic_read(&net_device->refcnt))
93 atomic_inc(&net_device->refcnt);
94 else
95 net_device = NULL;
97 return net_device;
100 static void put_net_device(struct hv_device *device)
102 struct netvsc_device *net_device;
104 net_device = device->ext;
106 atomic_dec(&net_device->refcnt);
109 static struct netvsc_device *release_outbound_net_device(
110 struct hv_device *device)
112 struct netvsc_device *net_device;
114 net_device = device->ext;
115 if (net_device == NULL)
116 return NULL;
118 /* Busy wait until the ref drop to 2, then set it to 1 */
119 while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2)
120 udelay(100);
122 return net_device;
125 static struct netvsc_device *release_inbound_net_device(
126 struct hv_device *device)
128 struct netvsc_device *net_device;
130 net_device = device->ext;
131 if (net_device == NULL)
132 return NULL;
134 /* Busy wait until the ref drop to 1, then set it to 0 */
135 while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
136 udelay(100);
138 device->ext = NULL;
139 return net_device;
142 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
144 struct nvsp_message *revoke_packet;
145 int ret = 0;
148 * If we got a section count, it means we received a
149 * SendReceiveBufferComplete msg (ie sent
150 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
151 * to send a revoke msg here
153 if (net_device->recv_section_cnt) {
154 /* Send the revoke receive buffer */
155 revoke_packet = &net_device->revoke_packet;
156 memset(revoke_packet, 0, sizeof(struct nvsp_message));
158 revoke_packet->hdr.msg_type =
159 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
160 revoke_packet->msg.v1_msg.
161 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
163 ret = vmbus_sendpacket(net_device->dev->channel,
164 revoke_packet,
165 sizeof(struct nvsp_message),
166 (unsigned long)revoke_packet,
167 VM_PKT_DATA_INBAND, 0);
169 * If we failed here, we might as well return and
170 * have a leak rather than continue and a bugchk
172 if (ret != 0) {
173 dev_err(&net_device->dev->device, "unable to send "
174 "revoke receive buffer to netvsp");
175 return -1;
179 /* Teardown the gpadl on the vsp end */
180 if (net_device->recv_buf_gpadl_handle) {
181 ret = vmbus_teardown_gpadl(net_device->dev->channel,
182 net_device->recv_buf_gpadl_handle);
184 /* If we failed here, we might as well return and have a leak
185 * rather than continue and a bugchk
187 if (ret != 0) {
188 dev_err(&net_device->dev->device,
189 "unable to teardown receive buffer's gpadl");
190 return -1;
192 net_device->recv_buf_gpadl_handle = 0;
195 if (net_device->recv_buf) {
196 /* Free up the receive buffer */
197 free_pages((unsigned long)net_device->recv_buf,
198 get_order(net_device->recv_buf_size));
199 net_device->recv_buf = NULL;
202 if (net_device->recv_section) {
203 net_device->recv_section_cnt = 0;
204 kfree(net_device->recv_section);
205 net_device->recv_section = NULL;
208 return ret;
211 static int netvsc_init_recv_buf(struct hv_device *device)
213 int ret = 0;
214 int t;
215 struct netvsc_device *net_device;
216 struct nvsp_message *init_packet;
218 net_device = get_outbound_net_device(device);
219 if (!net_device) {
220 dev_err(&device->device, "unable to get net device..."
221 "device being destroyed?");
222 return -1;
225 net_device->recv_buf =
226 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
227 get_order(net_device->recv_buf_size));
228 if (!net_device->recv_buf) {
229 dev_err(&device->device, "unable to allocate receive "
230 "buffer of size %d", net_device->recv_buf_size);
231 ret = -1;
232 goto cleanup;
236 * Establish the gpadl handle for this buffer on this
237 * channel. Note: This call uses the vmbus connection rather
238 * than the channel to establish the gpadl handle.
240 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
241 net_device->recv_buf_size,
242 &net_device->recv_buf_gpadl_handle);
243 if (ret != 0) {
244 dev_err(&device->device,
245 "unable to establish receive buffer's gpadl");
246 goto cleanup;
250 /* Notify the NetVsp of the gpadl handle */
251 init_packet = &net_device->channel_init_pkt;
253 memset(init_packet, 0, sizeof(struct nvsp_message));
255 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
256 init_packet->msg.v1_msg.send_recv_buf.
257 gpadl_handle = net_device->recv_buf_gpadl_handle;
258 init_packet->msg.v1_msg.
259 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
261 /* Send the gpadl notification request */
262 ret = vmbus_sendpacket(device->channel, init_packet,
263 sizeof(struct nvsp_message),
264 (unsigned long)init_packet,
265 VM_PKT_DATA_INBAND,
266 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
267 if (ret != 0) {
268 dev_err(&device->device,
269 "unable to send receive buffer's gpadl to netvsp");
270 goto cleanup;
273 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
274 BUG_ON(t == 0);
277 /* Check the response */
278 if (init_packet->msg.v1_msg.
279 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
280 dev_err(&device->device, "Unable to complete receive buffer "
281 "initialzation with NetVsp - status %d",
282 init_packet->msg.v1_msg.
283 send_recv_buf_complete.status);
284 ret = -1;
285 goto cleanup;
288 /* Parse the response */
290 net_device->recv_section_cnt = init_packet->msg.
291 v1_msg.send_recv_buf_complete.num_sections;
293 net_device->recv_section = kmalloc(net_device->recv_section_cnt
294 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
295 if (net_device->recv_section == NULL) {
296 ret = -1;
297 goto cleanup;
300 memcpy(net_device->recv_section,
301 init_packet->msg.v1_msg.
302 send_recv_buf_complete.sections,
303 net_device->recv_section_cnt *
304 sizeof(struct nvsp_1_receive_buffer_section));
307 * For 1st release, there should only be 1 section that represents the
308 * entire receive buffer
310 if (net_device->recv_section_cnt != 1 ||
311 net_device->recv_section->offset != 0) {
312 ret = -1;
313 goto cleanup;
316 goto exit;
318 cleanup:
319 netvsc_destroy_recv_buf(net_device);
321 exit:
322 put_net_device(device);
323 return ret;
327 static int netvsc_connect_vsp(struct hv_device *device)
329 int ret, t;
330 struct netvsc_device *net_device;
331 struct nvsp_message *init_packet;
332 int ndis_version;
334 net_device = get_outbound_net_device(device);
335 if (!net_device) {
336 dev_err(&device->device, "unable to get net device..."
337 "device being destroyed?");
338 return -1;
341 init_packet = &net_device->channel_init_pkt;
343 memset(init_packet, 0, sizeof(struct nvsp_message));
344 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
345 init_packet->msg.init_msg.init.min_protocol_ver =
346 NVSP_MIN_PROTOCOL_VERSION;
347 init_packet->msg.init_msg.init.max_protocol_ver =
348 NVSP_MAX_PROTOCOL_VERSION;
350 /* Send the init request */
351 ret = vmbus_sendpacket(device->channel, init_packet,
352 sizeof(struct nvsp_message),
353 (unsigned long)init_packet,
354 VM_PKT_DATA_INBAND,
355 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
357 if (ret != 0)
358 goto cleanup;
360 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
362 if (t == 0) {
363 ret = -ETIMEDOUT;
364 goto cleanup;
367 if (init_packet->msg.init_msg.init_complete.status !=
368 NVSP_STAT_SUCCESS) {
369 ret = -1;
370 goto cleanup;
373 if (init_packet->msg.init_msg.init_complete.
374 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
375 ret = -1;
376 goto cleanup;
378 /* Send the ndis version */
379 memset(init_packet, 0, sizeof(struct nvsp_message));
381 ndis_version = 0x00050000;
383 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
384 init_packet->msg.v1_msg.
385 send_ndis_ver.ndis_major_ver =
386 (ndis_version & 0xFFFF0000) >> 16;
387 init_packet->msg.v1_msg.
388 send_ndis_ver.ndis_minor_ver =
389 ndis_version & 0xFFFF;
391 /* Send the init request */
392 ret = vmbus_sendpacket(device->channel, init_packet,
393 sizeof(struct nvsp_message),
394 (unsigned long)init_packet,
395 VM_PKT_DATA_INBAND, 0);
396 if (ret != 0) {
397 ret = -1;
398 goto cleanup;
401 /* Post the big receive buffer to NetVSP */
402 ret = netvsc_init_recv_buf(device);
404 cleanup:
405 put_net_device(device);
406 return ret;
409 static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
411 netvsc_destroy_recv_buf(net_device);
415 * netvsc_device_remove - Callback when the root bus device is removed
417 int netvsc_device_remove(struct hv_device *device)
419 struct netvsc_device *net_device;
420 struct hv_netvsc_packet *netvsc_packet, *pos;
422 /* Stop outbound traffic ie sends and receives completions */
423 net_device = release_outbound_net_device(device);
424 if (!net_device) {
425 dev_err(&device->device, "No net device present!!");
426 return -1;
429 /* Wait for all send completions */
430 while (atomic_read(&net_device->num_outstanding_sends)) {
431 dev_err(&device->device,
432 "waiting for %d requests to complete...",
433 atomic_read(&net_device->num_outstanding_sends));
434 udelay(100);
437 netvsc_disconnect_vsp(net_device);
439 /* Stop inbound traffic ie receives and sends completions */
440 net_device = release_inbound_net_device(device);
442 /* At this point, no one should be accessing netDevice except in here */
443 dev_notice(&device->device, "net device safe to remove");
445 /* Now, we can close the channel safely */
446 vmbus_close(device->channel);
448 /* Release all resources */
449 list_for_each_entry_safe(netvsc_packet, pos,
450 &net_device->recv_pkt_list, list_ent) {
451 list_del(&netvsc_packet->list_ent);
452 kfree(netvsc_packet);
455 free_net_device(net_device);
456 return 0;
459 static void netvsc_send_completion(struct hv_device *device,
460 struct vmpacket_descriptor *packet)
462 struct netvsc_device *net_device;
463 struct nvsp_message *nvsp_packet;
464 struct hv_netvsc_packet *nvsc_packet;
466 net_device = get_inbound_net_device(device);
467 if (!net_device) {
468 dev_err(&device->device, "unable to get net device..."
469 "device being destroyed?");
470 return;
473 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
474 (packet->offset8 << 3));
476 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
477 (nvsp_packet->hdr.msg_type ==
478 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
479 (nvsp_packet->hdr.msg_type ==
480 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
481 /* Copy the response back */
482 memcpy(&net_device->channel_init_pkt, nvsp_packet,
483 sizeof(struct nvsp_message));
484 complete(&net_device->channel_init_wait);
485 } else if (nvsp_packet->hdr.msg_type ==
486 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
487 /* Get the send context */
488 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
489 packet->trans_id;
491 /* Notify the layer above us */
492 nvsc_packet->completion.send.send_completion(
493 nvsc_packet->completion.send.send_completion_ctx);
495 atomic_dec(&net_device->num_outstanding_sends);
496 } else {
497 dev_err(&device->device, "Unknown send completion packet type- "
498 "%d received!!", nvsp_packet->hdr.msg_type);
501 put_net_device(device);
504 int netvsc_send(struct hv_device *device,
505 struct hv_netvsc_packet *packet)
507 struct netvsc_device *net_device;
508 int ret = 0;
510 struct nvsp_message sendMessage;
512 net_device = get_outbound_net_device(device);
513 if (!net_device) {
514 dev_err(&device->device, "net device (%p) shutting down..."
515 "ignoring outbound packets", net_device);
516 return -2;
519 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
520 if (packet->is_data_pkt) {
521 /* 0 is RMC_DATA; */
522 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
523 } else {
524 /* 1 is RMC_CONTROL; */
525 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
528 /* Not using send buffer section */
529 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
530 0xFFFFFFFF;
531 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
533 if (packet->page_buf_cnt) {
534 ret = vmbus_sendpacket_pagebuffer(device->channel,
535 packet->page_buf,
536 packet->page_buf_cnt,
537 &sendMessage,
538 sizeof(struct nvsp_message),
539 (unsigned long)packet);
540 } else {
541 ret = vmbus_sendpacket(device->channel, &sendMessage,
542 sizeof(struct nvsp_message),
543 (unsigned long)packet,
544 VM_PKT_DATA_INBAND,
545 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
549 if (ret != 0)
550 dev_err(&device->device, "Unable to send packet %p ret %d",
551 packet, ret);
553 atomic_inc(&net_device->num_outstanding_sends);
554 put_net_device(device);
555 return ret;
558 static void netvsc_send_recv_completion(struct hv_device *device,
559 u64 transaction_id)
561 struct nvsp_message recvcompMessage;
562 int retries = 0;
563 int ret;
565 recvcompMessage.hdr.msg_type =
566 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
568 /* FIXME: Pass in the status */
569 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
570 NVSP_STAT_SUCCESS;
572 retry_send_cmplt:
573 /* Send the completion */
574 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
575 sizeof(struct nvsp_message), transaction_id,
576 VM_PKT_COMP, 0);
577 if (ret == 0) {
578 /* success */
579 /* no-op */
580 } else if (ret == -1) {
581 /* no more room...wait a bit and attempt to retry 3 times */
582 retries++;
583 dev_err(&device->device, "unable to send receive completion pkt"
584 " (tid %llx)...retrying %d", transaction_id, retries);
586 if (retries < 4) {
587 udelay(100);
588 goto retry_send_cmplt;
589 } else {
590 dev_err(&device->device, "unable to send receive "
591 "completion pkt (tid %llx)...give up retrying",
592 transaction_id);
594 } else {
595 dev_err(&device->device, "unable to send receive "
596 "completion pkt - %llx", transaction_id);
600 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
601 static void netvsc_receive_completion(void *context)
603 struct hv_netvsc_packet *packet = context;
604 struct hv_device *device = (struct hv_device *)packet->device;
605 struct netvsc_device *net_device;
606 u64 transaction_id = 0;
607 bool fsend_receive_comp = false;
608 unsigned long flags;
611 * Even though it seems logical to do a GetOutboundNetDevice() here to
612 * send out receive completion, we are using GetInboundNetDevice()
613 * since we may have disable outbound traffic already.
615 net_device = get_inbound_net_device(device);
616 if (!net_device) {
617 dev_err(&device->device, "unable to get net device..."
618 "device being destroyed?");
619 return;
622 /* Overloading use of the lock. */
623 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
625 packet->xfer_page_pkt->count--;
628 * Last one in the line that represent 1 xfer page packet.
629 * Return the xfer page packet itself to the freelist
631 if (packet->xfer_page_pkt->count == 0) {
632 fsend_receive_comp = true;
633 transaction_id = packet->completion.recv.recv_completion_tid;
634 list_add_tail(&packet->xfer_page_pkt->list_ent,
635 &net_device->recv_pkt_list);
639 /* Put the packet back */
640 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
641 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
643 /* Send a receive completion for the xfer page packet */
644 if (fsend_receive_comp)
645 netvsc_send_recv_completion(device, transaction_id);
647 put_net_device(device);
650 static void netvsc_receive(struct hv_device *device,
651 struct vmpacket_descriptor *packet)
653 struct netvsc_device *net_device;
654 struct vmtransfer_page_packet_header *vmxferpage_packet;
655 struct nvsp_message *nvsp_packet;
656 struct hv_netvsc_packet *netvsc_packet = NULL;
657 unsigned long start;
658 unsigned long end, end_virtual;
659 /* struct netvsc_driver *netvscDriver; */
660 struct xferpage_packet *xferpage_packet = NULL;
661 int i, j;
662 int count = 0, bytes_remain = 0;
663 unsigned long flags;
665 LIST_HEAD(listHead);
667 net_device = get_inbound_net_device(device);
668 if (!net_device) {
669 dev_err(&device->device, "unable to get net device..."
670 "device being destroyed?");
671 return;
675 * All inbound packets other than send completion should be xfer page
676 * packet
678 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
679 dev_err(&device->device, "Unknown packet type received - %d",
680 packet->type);
681 put_net_device(device);
682 return;
685 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
686 (packet->offset8 << 3));
688 /* Make sure this is a valid nvsp packet */
689 if (nvsp_packet->hdr.msg_type !=
690 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
691 dev_err(&device->device, "Unknown nvsp packet type received-"
692 " %d", nvsp_packet->hdr.msg_type);
693 put_net_device(device);
694 return;
697 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
699 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
700 dev_err(&device->device, "Invalid xfer page set id - "
701 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
702 vmxferpage_packet->xfer_pageset_id);
703 put_net_device(device);
704 return;
708 * Grab free packets (range count + 1) to represent this xfer
709 * page packet. +1 to represent the xfer page packet itself.
710 * We grab it here so that we know exactly how many we can
711 * fulfil
713 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
714 while (!list_empty(&net_device->recv_pkt_list)) {
715 list_move_tail(net_device->recv_pkt_list.next, &listHead);
716 if (++count == vmxferpage_packet->range_cnt + 1)
717 break;
719 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
722 * We need at least 2 netvsc pkts (1 to represent the xfer
723 * page and at least 1 for the range) i.e. we can handled
724 * some of the xfer page packet ranges...
726 if (count < 2) {
727 dev_err(&device->device, "Got only %d netvsc pkt...needed "
728 "%d pkts. Dropping this xfer page packet completely!",
729 count, vmxferpage_packet->range_cnt + 1);
731 /* Return it to the freelist */
732 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
733 for (i = count; i != 0; i--) {
734 list_move_tail(listHead.next,
735 &net_device->recv_pkt_list);
737 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
738 flags);
740 netvsc_send_recv_completion(device,
741 vmxferpage_packet->d.trans_id);
743 put_net_device(device);
744 return;
747 /* Remove the 1st packet to represent the xfer page packet itself */
748 xferpage_packet = (struct xferpage_packet *)listHead.next;
749 list_del(&xferpage_packet->list_ent);
751 /* This is how much we can satisfy */
752 xferpage_packet->count = count - 1;
754 if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
755 dev_err(&device->device, "Needed %d netvsc pkts to satisy "
756 "this xfer page...got %d",
757 vmxferpage_packet->range_cnt, xferpage_packet->count);
760 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
761 for (i = 0; i < (count - 1); i++) {
762 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
763 list_del(&netvsc_packet->list_ent);
765 /* Initialize the netvsc packet */
766 netvsc_packet->xfer_page_pkt = xferpage_packet;
767 netvsc_packet->completion.recv.recv_completion =
768 netvsc_receive_completion;
769 netvsc_packet->completion.recv.recv_completion_ctx =
770 netvsc_packet;
771 netvsc_packet->device = device;
772 /* Save this so that we can send it back */
773 netvsc_packet->completion.recv.recv_completion_tid =
774 vmxferpage_packet->d.trans_id;
776 netvsc_packet->total_data_buflen =
777 vmxferpage_packet->ranges[i].byte_count;
778 netvsc_packet->page_buf_cnt = 1;
780 netvsc_packet->page_buf[0].len =
781 vmxferpage_packet->ranges[i].byte_count;
783 start = virt_to_phys((void *)((unsigned long)net_device->
784 recv_buf + vmxferpage_packet->ranges[i].byte_offset));
786 netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
787 end_virtual = (unsigned long)net_device->recv_buf
788 + vmxferpage_packet->ranges[i].byte_offset
789 + vmxferpage_packet->ranges[i].byte_count - 1;
790 end = virt_to_phys((void *)end_virtual);
792 /* Calculate the page relative offset */
793 netvsc_packet->page_buf[0].offset =
794 vmxferpage_packet->ranges[i].byte_offset &
795 (PAGE_SIZE - 1);
796 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
797 /* Handle frame across multiple pages: */
798 netvsc_packet->page_buf[0].len =
799 (netvsc_packet->page_buf[0].pfn <<
800 PAGE_SHIFT)
801 + PAGE_SIZE - start;
802 bytes_remain = netvsc_packet->total_data_buflen -
803 netvsc_packet->page_buf[0].len;
804 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
805 netvsc_packet->page_buf[j].offset = 0;
806 if (bytes_remain <= PAGE_SIZE) {
807 netvsc_packet->page_buf[j].len =
808 bytes_remain;
809 bytes_remain = 0;
810 } else {
811 netvsc_packet->page_buf[j].len =
812 PAGE_SIZE;
813 bytes_remain -= PAGE_SIZE;
815 netvsc_packet->page_buf[j].pfn =
816 virt_to_phys((void *)(end_virtual -
817 bytes_remain)) >> PAGE_SHIFT;
818 netvsc_packet->page_buf_cnt++;
819 if (bytes_remain == 0)
820 break;
824 /* Pass it to the upper layer */
825 rndis_filter_receive(device, netvsc_packet);
827 netvsc_receive_completion(netvsc_packet->
828 completion.recv.recv_completion_ctx);
831 put_net_device(device);
834 static void netvsc_channel_cb(void *context)
836 int ret;
837 struct hv_device *device = context;
838 struct netvsc_device *net_device;
839 u32 bytes_recvd;
840 u64 request_id;
841 unsigned char *packet;
842 struct vmpacket_descriptor *desc;
843 unsigned char *buffer;
844 int bufferlen = NETVSC_PACKET_SIZE;
846 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
847 GFP_ATOMIC);
848 if (!packet)
849 return;
850 buffer = packet;
852 net_device = get_inbound_net_device(device);
853 if (!net_device) {
854 dev_err(&device->device, "net device (%p) shutting down..."
855 "ignoring inbound packets", net_device);
856 goto out;
859 do {
860 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
861 &bytes_recvd, &request_id);
862 if (ret == 0) {
863 if (bytes_recvd > 0) {
864 desc = (struct vmpacket_descriptor *)buffer;
865 switch (desc->type) {
866 case VM_PKT_COMP:
867 netvsc_send_completion(device, desc);
868 break;
870 case VM_PKT_DATA_USING_XFER_PAGES:
871 netvsc_receive(device, desc);
872 break;
874 default:
875 dev_err(&device->device,
876 "unhandled packet type %d, "
877 "tid %llx len %d\n",
878 desc->type, request_id,
879 bytes_recvd);
880 break;
883 /* reset */
884 if (bufferlen > NETVSC_PACKET_SIZE) {
885 kfree(buffer);
886 buffer = packet;
887 bufferlen = NETVSC_PACKET_SIZE;
889 } else {
890 /* reset */
891 if (bufferlen > NETVSC_PACKET_SIZE) {
892 kfree(buffer);
893 buffer = packet;
894 bufferlen = NETVSC_PACKET_SIZE;
897 break;
899 } else if (ret == -2) {
900 /* Handle large packet */
901 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
902 if (buffer == NULL) {
903 /* Try again next time around */
904 dev_err(&device->device,
905 "unable to allocate buffer of size "
906 "(%d)!!", bytes_recvd);
907 break;
910 bufferlen = bytes_recvd;
912 } while (1);
914 put_net_device(device);
915 out:
916 kfree(buffer);
917 return;
921 * netvsc_device_add - Callback when the device belonging to this
922 * driver is added
924 int netvsc_device_add(struct hv_device *device, void *additional_info)
926 int ret = 0;
927 int i;
928 int ring_size =
929 ((struct netvsc_device_info *)additional_info)->ring_size;
930 struct netvsc_device *net_device;
931 struct hv_netvsc_packet *packet, *pos;
933 net_device = alloc_net_device(device);
934 if (!net_device) {
935 ret = -1;
936 goto cleanup;
939 /* Initialize the NetVSC channel extension */
940 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
941 spin_lock_init(&net_device->recv_pkt_list_lock);
943 INIT_LIST_HEAD(&net_device->recv_pkt_list);
945 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
946 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
947 (NETVSC_RECEIVE_SG_COUNT *
948 sizeof(struct hv_page_buffer)), GFP_KERNEL);
949 if (!packet)
950 break;
952 list_add_tail(&packet->list_ent,
953 &net_device->recv_pkt_list);
955 init_completion(&net_device->channel_init_wait);
957 /* Open the channel */
958 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
959 ring_size * PAGE_SIZE, NULL, 0,
960 netvsc_channel_cb, device);
962 if (ret != 0) {
963 dev_err(&device->device, "unable to open channel: %d", ret);
964 ret = -1;
965 goto cleanup;
968 /* Channel is opened */
969 pr_info("hv_netvsc channel opened successfully");
971 /* Connect with the NetVsp */
972 ret = netvsc_connect_vsp(device);
973 if (ret != 0) {
974 dev_err(&device->device,
975 "unable to connect to NetVSP - %d", ret);
976 ret = -1;
977 goto close;
980 return ret;
982 close:
983 /* Now, we can close the channel safely */
984 vmbus_close(device->channel);
986 cleanup:
988 if (net_device) {
989 list_for_each_entry_safe(packet, pos,
990 &net_device->recv_pkt_list,
991 list_ent) {
992 list_del(&packet->list_ent);
993 kfree(packet);
996 release_outbound_net_device(device);
997 release_inbound_net_device(device);
999 free_net_device(net_device);
1002 return ret;
1006 * netvsc_initialize - Main entry point
1008 int netvsc_initialize(struct hv_driver *drv)
1011 drv->name = driver_name;
1012 memcpy(&drv->dev_type, &netvsc_device_type, sizeof(struct hv_guid));
1014 return 0;