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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
27 #include <linux/delay.h>
29 #include <linux/slab.h>
32 #include "hyperv_net.h"
35 static struct netvsc_device *alloc_net_device(struct hv_device *device)
37 struct netvsc_device *net_device;
39 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
43 /* Set to 2 to allow both inbound and outbound traffic */
44 atomic_set(&net_device->refcnt, 2);
46 net_device->destroy = false;
47 net_device->dev = device;
48 device->ext = net_device;
53 /* Get the net device object iff exists and its refcount > 1 */
54 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
56 struct netvsc_device *net_device;
58 net_device = device->ext;
59 if (net_device && (atomic_read(&net_device->refcnt) > 1) &&
61 atomic_inc(&net_device->refcnt);
68 /* Get the net device object iff exists and its refcount > 0 */
69 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
71 struct netvsc_device *net_device;
73 net_device = device->ext;
74 if (net_device && atomic_read(&net_device->refcnt))
75 atomic_inc(&net_device->refcnt);
82 static void put_net_device(struct hv_device *device)
84 struct netvsc_device *net_device;
86 net_device = device->ext;
88 atomic_dec(&net_device->refcnt);
91 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
93 struct nvsp_message *revoke_packet;
97 * If we got a section count, it means we received a
98 * SendReceiveBufferComplete msg (ie sent
99 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
100 * to send a revoke msg here
102 if (net_device->recv_section_cnt) {
103 /* Send the revoke receive buffer */
104 revoke_packet = &net_device->revoke_packet;
105 memset(revoke_packet, 0, sizeof(struct nvsp_message));
107 revoke_packet->hdr.msg_type =
108 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
109 revoke_packet->msg.v1_msg.
110 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
112 ret = vmbus_sendpacket(net_device->dev->channel,
114 sizeof(struct nvsp_message),
115 (unsigned long)revoke_packet,
116 VM_PKT_DATA_INBAND, 0);
118 * If we failed here, we might as well return and
119 * have a leak rather than continue and a bugchk
122 dev_err(&net_device->dev->device, "unable to send "
123 "revoke receive buffer to netvsp");
128 /* Teardown the gpadl on the vsp end */
129 if (net_device->recv_buf_gpadl_handle) {
130 ret = vmbus_teardown_gpadl(net_device->dev->channel,
131 net_device->recv_buf_gpadl_handle);
133 /* If we failed here, we might as well return and have a leak
134 * rather than continue and a bugchk
137 dev_err(&net_device->dev->device,
138 "unable to teardown receive buffer's gpadl");
141 net_device->recv_buf_gpadl_handle = 0;
144 if (net_device->recv_buf) {
145 /* Free up the receive buffer */
146 free_pages((unsigned long)net_device->recv_buf,
147 get_order(net_device->recv_buf_size));
148 net_device->recv_buf = NULL;
151 if (net_device->recv_section) {
152 net_device->recv_section_cnt = 0;
153 kfree(net_device->recv_section);
154 net_device->recv_section = NULL;
160 static int netvsc_init_recv_buf(struct hv_device *device)
164 struct netvsc_device *net_device;
165 struct nvsp_message *init_packet;
167 net_device = get_outbound_net_device(device);
169 dev_err(&device->device, "unable to get net device..."
170 "device being destroyed?");
174 net_device->recv_buf =
175 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
176 get_order(net_device->recv_buf_size));
177 if (!net_device->recv_buf) {
178 dev_err(&device->device, "unable to allocate receive "
179 "buffer of size %d", net_device->recv_buf_size);
185 * Establish the gpadl handle for this buffer on this
186 * channel. Note: This call uses the vmbus connection rather
187 * than the channel to establish the gpadl handle.
189 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
190 net_device->recv_buf_size,
191 &net_device->recv_buf_gpadl_handle);
193 dev_err(&device->device,
194 "unable to establish receive buffer's gpadl");
199 /* Notify the NetVsp of the gpadl handle */
200 init_packet = &net_device->channel_init_pkt;
202 memset(init_packet, 0, sizeof(struct nvsp_message));
204 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
205 init_packet->msg.v1_msg.send_recv_buf.
206 gpadl_handle = net_device->recv_buf_gpadl_handle;
207 init_packet->msg.v1_msg.
208 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
210 /* Send the gpadl notification request */
211 ret = vmbus_sendpacket(device->channel, init_packet,
212 sizeof(struct nvsp_message),
213 (unsigned long)init_packet,
215 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
217 dev_err(&device->device,
218 "unable to send receive buffer's gpadl to netvsp");
222 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
226 /* Check the response */
227 if (init_packet->msg.v1_msg.
228 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
229 dev_err(&device->device, "Unable to complete receive buffer "
230 "initialzation with NetVsp - status %d",
231 init_packet->msg.v1_msg.
232 send_recv_buf_complete.status);
237 /* Parse the response */
239 net_device->recv_section_cnt = init_packet->msg.
240 v1_msg.send_recv_buf_complete.num_sections;
242 net_device->recv_section = kmalloc(net_device->recv_section_cnt
243 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
244 if (net_device->recv_section == NULL) {
249 memcpy(net_device->recv_section,
250 init_packet->msg.v1_msg.
251 send_recv_buf_complete.sections,
252 net_device->recv_section_cnt *
253 sizeof(struct nvsp_1_receive_buffer_section));
256 * For 1st release, there should only be 1 section that represents the
257 * entire receive buffer
259 if (net_device->recv_section_cnt != 1 ||
260 net_device->recv_section->offset != 0) {
268 netvsc_destroy_recv_buf(net_device);
271 put_net_device(device);
276 static int netvsc_connect_vsp(struct hv_device *device)
279 struct netvsc_device *net_device;
280 struct nvsp_message *init_packet;
283 net_device = get_outbound_net_device(device);
285 dev_err(&device->device, "unable to get net device..."
286 "device being destroyed?");
290 init_packet = &net_device->channel_init_pkt;
292 memset(init_packet, 0, sizeof(struct nvsp_message));
293 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
294 init_packet->msg.init_msg.init.min_protocol_ver =
295 NVSP_MIN_PROTOCOL_VERSION;
296 init_packet->msg.init_msg.init.max_protocol_ver =
297 NVSP_MAX_PROTOCOL_VERSION;
299 /* Send the init request */
300 ret = vmbus_sendpacket(device->channel, init_packet,
301 sizeof(struct nvsp_message),
302 (unsigned long)init_packet,
304 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
309 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
316 if (init_packet->msg.init_msg.init_complete.status !=
322 if (init_packet->msg.init_msg.init_complete.
323 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
327 /* Send the ndis version */
328 memset(init_packet, 0, sizeof(struct nvsp_message));
330 ndis_version = 0x00050000;
332 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
333 init_packet->msg.v1_msg.
334 send_ndis_ver.ndis_major_ver =
335 (ndis_version & 0xFFFF0000) >> 16;
336 init_packet->msg.v1_msg.
337 send_ndis_ver.ndis_minor_ver =
338 ndis_version & 0xFFFF;
340 /* Send the init request */
341 ret = vmbus_sendpacket(device->channel, init_packet,
342 sizeof(struct nvsp_message),
343 (unsigned long)init_packet,
344 VM_PKT_DATA_INBAND, 0);
348 /* Post the big receive buffer to NetVSP */
349 ret = netvsc_init_recv_buf(device);
352 put_net_device(device);
356 static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
358 netvsc_destroy_recv_buf(net_device);
362 * netvsc_device_remove - Callback when the root bus device is removed
364 int netvsc_device_remove(struct hv_device *device)
366 struct netvsc_device *net_device;
367 struct hv_netvsc_packet *netvsc_packet, *pos;
370 net_device = (struct netvsc_device *)device->ext;
371 atomic_dec(&net_device->refcnt);
372 spin_lock_irqsave(&device->channel->inbound_lock, flags);
373 net_device->destroy = true;
374 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
376 /* Wait for all send completions */
377 while (atomic_read(&net_device->num_outstanding_sends)) {
378 dev_err(&device->device,
379 "waiting for %d requests to complete...",
380 atomic_read(&net_device->num_outstanding_sends));
384 netvsc_disconnect_vsp(net_device);
386 atomic_dec(&net_device->refcnt);
389 * Wait until the ref cnt falls to 0.
390 * We have already stopped any new references
391 * for outgoing traffic. Also, at this point we don't have any
392 * incoming traffic as well. So this must be outgoing refrences
393 * established prior to marking the device as being destroyed.
394 * Since the send path is non-blocking, it is reasonable to busy
397 while (atomic_read(&net_device->refcnt))
400 /* At this point, no one should be accessing netDevice except in here */
401 dev_notice(&device->device, "net device safe to remove");
403 /* Now, we can close the channel safely */
404 vmbus_close(device->channel);
406 /* Release all resources */
407 list_for_each_entry_safe(netvsc_packet, pos,
408 &net_device->recv_pkt_list, list_ent) {
409 list_del(&netvsc_packet->list_ent);
410 kfree(netvsc_packet);
417 static void netvsc_send_completion(struct hv_device *device,
418 struct vmpacket_descriptor *packet)
420 struct netvsc_device *net_device;
421 struct nvsp_message *nvsp_packet;
422 struct hv_netvsc_packet *nvsc_packet;
424 net_device = get_inbound_net_device(device);
426 dev_err(&device->device, "unable to get net device..."
427 "device being destroyed?");
431 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
432 (packet->offset8 << 3));
434 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
435 (nvsp_packet->hdr.msg_type ==
436 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
437 (nvsp_packet->hdr.msg_type ==
438 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
439 /* Copy the response back */
440 memcpy(&net_device->channel_init_pkt, nvsp_packet,
441 sizeof(struct nvsp_message));
442 complete(&net_device->channel_init_wait);
443 } else if (nvsp_packet->hdr.msg_type ==
444 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
445 /* Get the send context */
446 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
449 /* Notify the layer above us */
450 nvsc_packet->completion.send.send_completion(
451 nvsc_packet->completion.send.send_completion_ctx);
453 atomic_dec(&net_device->num_outstanding_sends);
455 dev_err(&device->device, "Unknown send completion packet type- "
456 "%d received!!", nvsp_packet->hdr.msg_type);
459 put_net_device(device);
462 int netvsc_send(struct hv_device *device,
463 struct hv_netvsc_packet *packet)
465 struct netvsc_device *net_device;
468 struct nvsp_message sendMessage;
470 net_device = get_outbound_net_device(device);
472 dev_err(&device->device, "net device (%p) shutting down..."
473 "ignoring outbound packets", net_device);
477 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
478 if (packet->is_data_pkt) {
480 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
482 /* 1 is RMC_CONTROL; */
483 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
486 /* Not using send buffer section */
487 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
489 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
491 if (packet->page_buf_cnt) {
492 ret = vmbus_sendpacket_pagebuffer(device->channel,
494 packet->page_buf_cnt,
496 sizeof(struct nvsp_message),
497 (unsigned long)packet);
499 ret = vmbus_sendpacket(device->channel, &sendMessage,
500 sizeof(struct nvsp_message),
501 (unsigned long)packet,
503 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
508 dev_err(&device->device, "Unable to send packet %p ret %d",
511 atomic_inc(&net_device->num_outstanding_sends);
512 put_net_device(device);
516 static void netvsc_send_recv_completion(struct hv_device *device,
519 struct nvsp_message recvcompMessage;
523 recvcompMessage.hdr.msg_type =
524 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
526 /* FIXME: Pass in the status */
527 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
531 /* Send the completion */
532 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
533 sizeof(struct nvsp_message), transaction_id,
538 } else if (ret == -EAGAIN) {
539 /* no more room...wait a bit and attempt to retry 3 times */
541 dev_err(&device->device, "unable to send receive completion pkt"
542 " (tid %llx)...retrying %d", transaction_id, retries);
546 goto retry_send_cmplt;
548 dev_err(&device->device, "unable to send receive "
549 "completion pkt (tid %llx)...give up retrying",
553 dev_err(&device->device, "unable to send receive "
554 "completion pkt - %llx", transaction_id);
558 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
559 static void netvsc_receive_completion(void *context)
561 struct hv_netvsc_packet *packet = context;
562 struct hv_device *device = (struct hv_device *)packet->device;
563 struct netvsc_device *net_device;
564 u64 transaction_id = 0;
565 bool fsend_receive_comp = false;
569 * Even though it seems logical to do a GetOutboundNetDevice() here to
570 * send out receive completion, we are using GetInboundNetDevice()
571 * since we may have disable outbound traffic already.
573 net_device = get_inbound_net_device(device);
575 dev_err(&device->device, "unable to get net device..."
576 "device being destroyed?");
580 /* Overloading use of the lock. */
581 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
583 packet->xfer_page_pkt->count--;
586 * Last one in the line that represent 1 xfer page packet.
587 * Return the xfer page packet itself to the freelist
589 if (packet->xfer_page_pkt->count == 0) {
590 fsend_receive_comp = true;
591 transaction_id = packet->completion.recv.recv_completion_tid;
592 list_add_tail(&packet->xfer_page_pkt->list_ent,
593 &net_device->recv_pkt_list);
597 /* Put the packet back */
598 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
599 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
601 /* Send a receive completion for the xfer page packet */
602 if (fsend_receive_comp)
603 netvsc_send_recv_completion(device, transaction_id);
605 put_net_device(device);
608 static void netvsc_receive(struct hv_device *device,
609 struct vmpacket_descriptor *packet)
611 struct netvsc_device *net_device;
612 struct vmtransfer_page_packet_header *vmxferpage_packet;
613 struct nvsp_message *nvsp_packet;
614 struct hv_netvsc_packet *netvsc_packet = NULL;
616 unsigned long end, end_virtual;
617 /* struct netvsc_driver *netvscDriver; */
618 struct xferpage_packet *xferpage_packet = NULL;
620 int count = 0, bytes_remain = 0;
625 net_device = get_inbound_net_device(device);
627 dev_err(&device->device, "unable to get net device..."
628 "device being destroyed?");
633 * All inbound packets other than send completion should be xfer page
636 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
637 dev_err(&device->device, "Unknown packet type received - %d",
639 put_net_device(device);
643 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
644 (packet->offset8 << 3));
646 /* Make sure this is a valid nvsp packet */
647 if (nvsp_packet->hdr.msg_type !=
648 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
649 dev_err(&device->device, "Unknown nvsp packet type received-"
650 " %d", nvsp_packet->hdr.msg_type);
651 put_net_device(device);
655 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
657 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
658 dev_err(&device->device, "Invalid xfer page set id - "
659 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
660 vmxferpage_packet->xfer_pageset_id);
661 put_net_device(device);
666 * Grab free packets (range count + 1) to represent this xfer
667 * page packet. +1 to represent the xfer page packet itself.
668 * We grab it here so that we know exactly how many we can
671 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
672 while (!list_empty(&net_device->recv_pkt_list)) {
673 list_move_tail(net_device->recv_pkt_list.next, &listHead);
674 if (++count == vmxferpage_packet->range_cnt + 1)
677 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
680 * We need at least 2 netvsc pkts (1 to represent the xfer
681 * page and at least 1 for the range) i.e. we can handled
682 * some of the xfer page packet ranges...
685 dev_err(&device->device, "Got only %d netvsc pkt...needed "
686 "%d pkts. Dropping this xfer page packet completely!",
687 count, vmxferpage_packet->range_cnt + 1);
689 /* Return it to the freelist */
690 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
691 for (i = count; i != 0; i--) {
692 list_move_tail(listHead.next,
693 &net_device->recv_pkt_list);
695 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
698 netvsc_send_recv_completion(device,
699 vmxferpage_packet->d.trans_id);
701 put_net_device(device);
705 /* Remove the 1st packet to represent the xfer page packet itself */
706 xferpage_packet = (struct xferpage_packet *)listHead.next;
707 list_del(&xferpage_packet->list_ent);
709 /* This is how much we can satisfy */
710 xferpage_packet->count = count - 1;
712 if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
713 dev_err(&device->device, "Needed %d netvsc pkts to satisy "
714 "this xfer page...got %d",
715 vmxferpage_packet->range_cnt, xferpage_packet->count);
718 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
719 for (i = 0; i < (count - 1); i++) {
720 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
721 list_del(&netvsc_packet->list_ent);
723 /* Initialize the netvsc packet */
724 netvsc_packet->xfer_page_pkt = xferpage_packet;
725 netvsc_packet->completion.recv.recv_completion =
726 netvsc_receive_completion;
727 netvsc_packet->completion.recv.recv_completion_ctx =
729 netvsc_packet->device = device;
730 /* Save this so that we can send it back */
731 netvsc_packet->completion.recv.recv_completion_tid =
732 vmxferpage_packet->d.trans_id;
734 netvsc_packet->total_data_buflen =
735 vmxferpage_packet->ranges[i].byte_count;
736 netvsc_packet->page_buf_cnt = 1;
738 netvsc_packet->page_buf[0].len =
739 vmxferpage_packet->ranges[i].byte_count;
741 start = virt_to_phys((void *)((unsigned long)net_device->
742 recv_buf + vmxferpage_packet->ranges[i].byte_offset));
744 netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
745 end_virtual = (unsigned long)net_device->recv_buf
746 + vmxferpage_packet->ranges[i].byte_offset
747 + vmxferpage_packet->ranges[i].byte_count - 1;
748 end = virt_to_phys((void *)end_virtual);
750 /* Calculate the page relative offset */
751 netvsc_packet->page_buf[0].offset =
752 vmxferpage_packet->ranges[i].byte_offset &
754 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
755 /* Handle frame across multiple pages: */
756 netvsc_packet->page_buf[0].len =
757 (netvsc_packet->page_buf[0].pfn <<
760 bytes_remain = netvsc_packet->total_data_buflen -
761 netvsc_packet->page_buf[0].len;
762 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
763 netvsc_packet->page_buf[j].offset = 0;
764 if (bytes_remain <= PAGE_SIZE) {
765 netvsc_packet->page_buf[j].len =
769 netvsc_packet->page_buf[j].len =
771 bytes_remain -= PAGE_SIZE;
773 netvsc_packet->page_buf[j].pfn =
774 virt_to_phys((void *)(end_virtual -
775 bytes_remain)) >> PAGE_SHIFT;
776 netvsc_packet->page_buf_cnt++;
777 if (bytes_remain == 0)
782 /* Pass it to the upper layer */
783 rndis_filter_receive(device, netvsc_packet);
785 netvsc_receive_completion(netvsc_packet->
786 completion.recv.recv_completion_ctx);
789 put_net_device(device);
792 static void netvsc_channel_cb(void *context)
795 struct hv_device *device = context;
796 struct netvsc_device *net_device;
799 unsigned char *packet;
800 struct vmpacket_descriptor *desc;
801 unsigned char *buffer;
802 int bufferlen = NETVSC_PACKET_SIZE;
804 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
810 net_device = get_inbound_net_device(device);
812 dev_err(&device->device, "net device (%p) shutting down..."
813 "ignoring inbound packets", net_device);
818 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
819 &bytes_recvd, &request_id);
821 if (bytes_recvd > 0) {
822 desc = (struct vmpacket_descriptor *)buffer;
823 switch (desc->type) {
825 netvsc_send_completion(device, desc);
828 case VM_PKT_DATA_USING_XFER_PAGES:
829 netvsc_receive(device, desc);
833 dev_err(&device->device,
834 "unhandled packet type %d, "
836 desc->type, request_id,
842 if (bufferlen > NETVSC_PACKET_SIZE) {
845 bufferlen = NETVSC_PACKET_SIZE;
849 if (bufferlen > NETVSC_PACKET_SIZE) {
852 bufferlen = NETVSC_PACKET_SIZE;
857 } else if (ret == -ENOBUFS) {
858 /* Handle large packet */
859 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
860 if (buffer == NULL) {
861 /* Try again next time around */
862 dev_err(&device->device,
863 "unable to allocate buffer of size "
864 "(%d)!!", bytes_recvd);
868 bufferlen = bytes_recvd;
872 put_net_device(device);
879 * netvsc_device_add - Callback when the device belonging to this
882 int netvsc_device_add(struct hv_device *device, void *additional_info)
887 ((struct netvsc_device_info *)additional_info)->ring_size;
888 struct netvsc_device *net_device;
889 struct hv_netvsc_packet *packet, *pos;
891 net_device = alloc_net_device(device);
897 /* Initialize the NetVSC channel extension */
898 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
899 spin_lock_init(&net_device->recv_pkt_list_lock);
901 INIT_LIST_HEAD(&net_device->recv_pkt_list);
903 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
904 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
905 (NETVSC_RECEIVE_SG_COUNT *
906 sizeof(struct hv_page_buffer)), GFP_KERNEL);
910 list_add_tail(&packet->list_ent,
911 &net_device->recv_pkt_list);
913 init_completion(&net_device->channel_init_wait);
915 /* Open the channel */
916 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
917 ring_size * PAGE_SIZE, NULL, 0,
918 netvsc_channel_cb, device);
921 dev_err(&device->device, "unable to open channel: %d", ret);
925 /* Channel is opened */
926 pr_info("hv_netvsc channel opened successfully");
928 /* Connect with the NetVsp */
929 ret = netvsc_connect_vsp(device);
931 dev_err(&device->device,
932 "unable to connect to NetVSP - %d", ret);
939 /* Now, we can close the channel safely */
940 vmbus_close(device->channel);
945 list_for_each_entry_safe(packet, pos,
946 &net_device->recv_pkt_list,
948 list_del(&packet->list_ent);