Staging: hv: netvsc: Inline the code for free_net_device()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / netvsc.c
blob75c6ed7b790aee5a1424086612db5837406671ab
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 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);
40 if (!net_device)
41 return NULL;
43 /* Set to 2 to allow both inbound and outbound traffic */
44 atomic_cmpxchg(&net_device->refcnt, 0, 2);
46 net_device->dev = device;
47 device->ext = net_device;
49 return net_device;
52 /* Get the net device object iff exists and its refcount > 1 */
53 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
55 struct netvsc_device *net_device;
57 net_device = device->ext;
58 if (net_device && atomic_read(&net_device->refcnt) > 1)
59 atomic_inc(&net_device->refcnt);
60 else
61 net_device = NULL;
63 return net_device;
66 /* Get the net device object iff exists and its refcount > 0 */
67 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
69 struct netvsc_device *net_device;
71 net_device = device->ext;
72 if (net_device && atomic_read(&net_device->refcnt))
73 atomic_inc(&net_device->refcnt);
74 else
75 net_device = NULL;
77 return net_device;
80 static void put_net_device(struct hv_device *device)
82 struct netvsc_device *net_device;
84 net_device = device->ext;
86 atomic_dec(&net_device->refcnt);
89 static struct netvsc_device *release_outbound_net_device(
90 struct hv_device *device)
92 struct netvsc_device *net_device;
94 net_device = device->ext;
95 if (net_device == NULL)
96 return NULL;
98 /* Busy wait until the ref drop to 2, then set it to 1 */
99 while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2)
100 udelay(100);
102 return net_device;
105 static struct netvsc_device *release_inbound_net_device(
106 struct hv_device *device)
108 struct netvsc_device *net_device;
110 net_device = device->ext;
111 if (net_device == NULL)
112 return NULL;
114 /* Busy wait until the ref drop to 1, then set it to 0 */
115 while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
116 udelay(100);
118 device->ext = NULL;
119 return net_device;
122 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
124 struct nvsp_message *revoke_packet;
125 int ret = 0;
128 * If we got a section count, it means we received a
129 * SendReceiveBufferComplete msg (ie sent
130 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
131 * to send a revoke msg here
133 if (net_device->recv_section_cnt) {
134 /* Send the revoke receive buffer */
135 revoke_packet = &net_device->revoke_packet;
136 memset(revoke_packet, 0, sizeof(struct nvsp_message));
138 revoke_packet->hdr.msg_type =
139 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
140 revoke_packet->msg.v1_msg.
141 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
143 ret = vmbus_sendpacket(net_device->dev->channel,
144 revoke_packet,
145 sizeof(struct nvsp_message),
146 (unsigned long)revoke_packet,
147 VM_PKT_DATA_INBAND, 0);
149 * If we failed here, we might as well return and
150 * have a leak rather than continue and a bugchk
152 if (ret != 0) {
153 dev_err(&net_device->dev->device, "unable to send "
154 "revoke receive buffer to netvsp");
155 return ret;
159 /* Teardown the gpadl on the vsp end */
160 if (net_device->recv_buf_gpadl_handle) {
161 ret = vmbus_teardown_gpadl(net_device->dev->channel,
162 net_device->recv_buf_gpadl_handle);
164 /* If we failed here, we might as well return and have a leak
165 * rather than continue and a bugchk
167 if (ret != 0) {
168 dev_err(&net_device->dev->device,
169 "unable to teardown receive buffer's gpadl");
170 return -ret;
172 net_device->recv_buf_gpadl_handle = 0;
175 if (net_device->recv_buf) {
176 /* Free up the receive buffer */
177 free_pages((unsigned long)net_device->recv_buf,
178 get_order(net_device->recv_buf_size));
179 net_device->recv_buf = NULL;
182 if (net_device->recv_section) {
183 net_device->recv_section_cnt = 0;
184 kfree(net_device->recv_section);
185 net_device->recv_section = NULL;
188 return ret;
191 static int netvsc_init_recv_buf(struct hv_device *device)
193 int ret = 0;
194 int t;
195 struct netvsc_device *net_device;
196 struct nvsp_message *init_packet;
198 net_device = get_outbound_net_device(device);
199 if (!net_device) {
200 dev_err(&device->device, "unable to get net device..."
201 "device being destroyed?");
202 return -ENODEV;
205 net_device->recv_buf =
206 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
207 get_order(net_device->recv_buf_size));
208 if (!net_device->recv_buf) {
209 dev_err(&device->device, "unable to allocate receive "
210 "buffer of size %d", net_device->recv_buf_size);
211 ret = -ENOMEM;
212 goto cleanup;
216 * Establish the gpadl handle for this buffer on this
217 * channel. Note: This call uses the vmbus connection rather
218 * than the channel to establish the gpadl handle.
220 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
221 net_device->recv_buf_size,
222 &net_device->recv_buf_gpadl_handle);
223 if (ret != 0) {
224 dev_err(&device->device,
225 "unable to establish receive buffer's gpadl");
226 goto cleanup;
230 /* Notify the NetVsp of the gpadl handle */
231 init_packet = &net_device->channel_init_pkt;
233 memset(init_packet, 0, sizeof(struct nvsp_message));
235 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
236 init_packet->msg.v1_msg.send_recv_buf.
237 gpadl_handle = net_device->recv_buf_gpadl_handle;
238 init_packet->msg.v1_msg.
239 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
241 /* Send the gpadl notification request */
242 ret = vmbus_sendpacket(device->channel, init_packet,
243 sizeof(struct nvsp_message),
244 (unsigned long)init_packet,
245 VM_PKT_DATA_INBAND,
246 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
247 if (ret != 0) {
248 dev_err(&device->device,
249 "unable to send receive buffer's gpadl to netvsp");
250 goto cleanup;
253 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
254 BUG_ON(t == 0);
257 /* Check the response */
258 if (init_packet->msg.v1_msg.
259 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
260 dev_err(&device->device, "Unable to complete receive buffer "
261 "initialzation with NetVsp - status %d",
262 init_packet->msg.v1_msg.
263 send_recv_buf_complete.status);
264 ret = -EINVAL;
265 goto cleanup;
268 /* Parse the response */
270 net_device->recv_section_cnt = init_packet->msg.
271 v1_msg.send_recv_buf_complete.num_sections;
273 net_device->recv_section = kmalloc(net_device->recv_section_cnt
274 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
275 if (net_device->recv_section == NULL) {
276 ret = -EINVAL;
277 goto cleanup;
280 memcpy(net_device->recv_section,
281 init_packet->msg.v1_msg.
282 send_recv_buf_complete.sections,
283 net_device->recv_section_cnt *
284 sizeof(struct nvsp_1_receive_buffer_section));
287 * For 1st release, there should only be 1 section that represents the
288 * entire receive buffer
290 if (net_device->recv_section_cnt != 1 ||
291 net_device->recv_section->offset != 0) {
292 ret = -EINVAL;
293 goto cleanup;
296 goto exit;
298 cleanup:
299 netvsc_destroy_recv_buf(net_device);
301 exit:
302 put_net_device(device);
303 return ret;
307 static int netvsc_connect_vsp(struct hv_device *device)
309 int ret, t;
310 struct netvsc_device *net_device;
311 struct nvsp_message *init_packet;
312 int ndis_version;
314 net_device = get_outbound_net_device(device);
315 if (!net_device) {
316 dev_err(&device->device, "unable to get net device..."
317 "device being destroyed?");
318 return -ENODEV;
321 init_packet = &net_device->channel_init_pkt;
323 memset(init_packet, 0, sizeof(struct nvsp_message));
324 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
325 init_packet->msg.init_msg.init.min_protocol_ver =
326 NVSP_MIN_PROTOCOL_VERSION;
327 init_packet->msg.init_msg.init.max_protocol_ver =
328 NVSP_MAX_PROTOCOL_VERSION;
330 /* Send the init request */
331 ret = vmbus_sendpacket(device->channel, init_packet,
332 sizeof(struct nvsp_message),
333 (unsigned long)init_packet,
334 VM_PKT_DATA_INBAND,
335 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
337 if (ret != 0)
338 goto cleanup;
340 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
342 if (t == 0) {
343 ret = -ETIMEDOUT;
344 goto cleanup;
347 if (init_packet->msg.init_msg.init_complete.status !=
348 NVSP_STAT_SUCCESS) {
349 ret = -EINVAL;
350 goto cleanup;
353 if (init_packet->msg.init_msg.init_complete.
354 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
355 ret = -EPROTO;
356 goto cleanup;
358 /* Send the ndis version */
359 memset(init_packet, 0, sizeof(struct nvsp_message));
361 ndis_version = 0x00050000;
363 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
364 init_packet->msg.v1_msg.
365 send_ndis_ver.ndis_major_ver =
366 (ndis_version & 0xFFFF0000) >> 16;
367 init_packet->msg.v1_msg.
368 send_ndis_ver.ndis_minor_ver =
369 ndis_version & 0xFFFF;
371 /* Send the init request */
372 ret = vmbus_sendpacket(device->channel, init_packet,
373 sizeof(struct nvsp_message),
374 (unsigned long)init_packet,
375 VM_PKT_DATA_INBAND, 0);
376 if (ret != 0)
377 goto cleanup;
379 /* Post the big receive buffer to NetVSP */
380 ret = netvsc_init_recv_buf(device);
382 cleanup:
383 put_net_device(device);
384 return ret;
387 static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
389 netvsc_destroy_recv_buf(net_device);
393 * netvsc_device_remove - Callback when the root bus device is removed
395 int netvsc_device_remove(struct hv_device *device)
397 struct netvsc_device *net_device;
398 struct hv_netvsc_packet *netvsc_packet, *pos;
400 /* Stop outbound traffic ie sends and receives completions */
401 net_device = release_outbound_net_device(device);
402 if (!net_device) {
403 dev_err(&device->device, "No net device present!!");
404 return -ENODEV;
407 /* Wait for all send completions */
408 while (atomic_read(&net_device->num_outstanding_sends)) {
409 dev_err(&device->device,
410 "waiting for %d requests to complete...",
411 atomic_read(&net_device->num_outstanding_sends));
412 udelay(100);
415 netvsc_disconnect_vsp(net_device);
417 /* Stop inbound traffic ie receives and sends completions */
418 net_device = release_inbound_net_device(device);
420 /* At this point, no one should be accessing netDevice except in here */
421 dev_notice(&device->device, "net device safe to remove");
423 /* Now, we can close the channel safely */
424 vmbus_close(device->channel);
426 /* Release all resources */
427 list_for_each_entry_safe(netvsc_packet, pos,
428 &net_device->recv_pkt_list, list_ent) {
429 list_del(&netvsc_packet->list_ent);
430 kfree(netvsc_packet);
433 kfree(net_device);
434 return 0;
437 static void netvsc_send_completion(struct hv_device *device,
438 struct vmpacket_descriptor *packet)
440 struct netvsc_device *net_device;
441 struct nvsp_message *nvsp_packet;
442 struct hv_netvsc_packet *nvsc_packet;
444 net_device = get_inbound_net_device(device);
445 if (!net_device) {
446 dev_err(&device->device, "unable to get net device..."
447 "device being destroyed?");
448 return;
451 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
452 (packet->offset8 << 3));
454 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
455 (nvsp_packet->hdr.msg_type ==
456 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
457 (nvsp_packet->hdr.msg_type ==
458 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
459 /* Copy the response back */
460 memcpy(&net_device->channel_init_pkt, nvsp_packet,
461 sizeof(struct nvsp_message));
462 complete(&net_device->channel_init_wait);
463 } else if (nvsp_packet->hdr.msg_type ==
464 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
465 /* Get the send context */
466 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
467 packet->trans_id;
469 /* Notify the layer above us */
470 nvsc_packet->completion.send.send_completion(
471 nvsc_packet->completion.send.send_completion_ctx);
473 atomic_dec(&net_device->num_outstanding_sends);
474 } else {
475 dev_err(&device->device, "Unknown send completion packet type- "
476 "%d received!!", nvsp_packet->hdr.msg_type);
479 put_net_device(device);
482 int netvsc_send(struct hv_device *device,
483 struct hv_netvsc_packet *packet)
485 struct netvsc_device *net_device;
486 int ret = 0;
488 struct nvsp_message sendMessage;
490 net_device = get_outbound_net_device(device);
491 if (!net_device) {
492 dev_err(&device->device, "net device (%p) shutting down..."
493 "ignoring outbound packets", net_device);
494 return -ENODEV;
497 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
498 if (packet->is_data_pkt) {
499 /* 0 is RMC_DATA; */
500 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
501 } else {
502 /* 1 is RMC_CONTROL; */
503 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
506 /* Not using send buffer section */
507 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
508 0xFFFFFFFF;
509 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
511 if (packet->page_buf_cnt) {
512 ret = vmbus_sendpacket_pagebuffer(device->channel,
513 packet->page_buf,
514 packet->page_buf_cnt,
515 &sendMessage,
516 sizeof(struct nvsp_message),
517 (unsigned long)packet);
518 } else {
519 ret = vmbus_sendpacket(device->channel, &sendMessage,
520 sizeof(struct nvsp_message),
521 (unsigned long)packet,
522 VM_PKT_DATA_INBAND,
523 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
527 if (ret != 0)
528 dev_err(&device->device, "Unable to send packet %p ret %d",
529 packet, ret);
531 atomic_inc(&net_device->num_outstanding_sends);
532 put_net_device(device);
533 return ret;
536 static void netvsc_send_recv_completion(struct hv_device *device,
537 u64 transaction_id)
539 struct nvsp_message recvcompMessage;
540 int retries = 0;
541 int ret;
543 recvcompMessage.hdr.msg_type =
544 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
546 /* FIXME: Pass in the status */
547 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
548 NVSP_STAT_SUCCESS;
550 retry_send_cmplt:
551 /* Send the completion */
552 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
553 sizeof(struct nvsp_message), transaction_id,
554 VM_PKT_COMP, 0);
555 if (ret == 0) {
556 /* success */
557 /* no-op */
558 } else if (ret == -EAGAIN) {
559 /* no more room...wait a bit and attempt to retry 3 times */
560 retries++;
561 dev_err(&device->device, "unable to send receive completion pkt"
562 " (tid %llx)...retrying %d", transaction_id, retries);
564 if (retries < 4) {
565 udelay(100);
566 goto retry_send_cmplt;
567 } else {
568 dev_err(&device->device, "unable to send receive "
569 "completion pkt (tid %llx)...give up retrying",
570 transaction_id);
572 } else {
573 dev_err(&device->device, "unable to send receive "
574 "completion pkt - %llx", transaction_id);
578 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
579 static void netvsc_receive_completion(void *context)
581 struct hv_netvsc_packet *packet = context;
582 struct hv_device *device = (struct hv_device *)packet->device;
583 struct netvsc_device *net_device;
584 u64 transaction_id = 0;
585 bool fsend_receive_comp = false;
586 unsigned long flags;
589 * Even though it seems logical to do a GetOutboundNetDevice() here to
590 * send out receive completion, we are using GetInboundNetDevice()
591 * since we may have disable outbound traffic already.
593 net_device = get_inbound_net_device(device);
594 if (!net_device) {
595 dev_err(&device->device, "unable to get net device..."
596 "device being destroyed?");
597 return;
600 /* Overloading use of the lock. */
601 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
603 packet->xfer_page_pkt->count--;
606 * Last one in the line that represent 1 xfer page packet.
607 * Return the xfer page packet itself to the freelist
609 if (packet->xfer_page_pkt->count == 0) {
610 fsend_receive_comp = true;
611 transaction_id = packet->completion.recv.recv_completion_tid;
612 list_add_tail(&packet->xfer_page_pkt->list_ent,
613 &net_device->recv_pkt_list);
617 /* Put the packet back */
618 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
619 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
621 /* Send a receive completion for the xfer page packet */
622 if (fsend_receive_comp)
623 netvsc_send_recv_completion(device, transaction_id);
625 put_net_device(device);
628 static void netvsc_receive(struct hv_device *device,
629 struct vmpacket_descriptor *packet)
631 struct netvsc_device *net_device;
632 struct vmtransfer_page_packet_header *vmxferpage_packet;
633 struct nvsp_message *nvsp_packet;
634 struct hv_netvsc_packet *netvsc_packet = NULL;
635 unsigned long start;
636 unsigned long end, end_virtual;
637 /* struct netvsc_driver *netvscDriver; */
638 struct xferpage_packet *xferpage_packet = NULL;
639 int i, j;
640 int count = 0, bytes_remain = 0;
641 unsigned long flags;
643 LIST_HEAD(listHead);
645 net_device = get_inbound_net_device(device);
646 if (!net_device) {
647 dev_err(&device->device, "unable to get net device..."
648 "device being destroyed?");
649 return;
653 * All inbound packets other than send completion should be xfer page
654 * packet
656 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
657 dev_err(&device->device, "Unknown packet type received - %d",
658 packet->type);
659 put_net_device(device);
660 return;
663 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
664 (packet->offset8 << 3));
666 /* Make sure this is a valid nvsp packet */
667 if (nvsp_packet->hdr.msg_type !=
668 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
669 dev_err(&device->device, "Unknown nvsp packet type received-"
670 " %d", nvsp_packet->hdr.msg_type);
671 put_net_device(device);
672 return;
675 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
677 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
678 dev_err(&device->device, "Invalid xfer page set id - "
679 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
680 vmxferpage_packet->xfer_pageset_id);
681 put_net_device(device);
682 return;
686 * Grab free packets (range count + 1) to represent this xfer
687 * page packet. +1 to represent the xfer page packet itself.
688 * We grab it here so that we know exactly how many we can
689 * fulfil
691 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
692 while (!list_empty(&net_device->recv_pkt_list)) {
693 list_move_tail(net_device->recv_pkt_list.next, &listHead);
694 if (++count == vmxferpage_packet->range_cnt + 1)
695 break;
697 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
700 * We need at least 2 netvsc pkts (1 to represent the xfer
701 * page and at least 1 for the range) i.e. we can handled
702 * some of the xfer page packet ranges...
704 if (count < 2) {
705 dev_err(&device->device, "Got only %d netvsc pkt...needed "
706 "%d pkts. Dropping this xfer page packet completely!",
707 count, vmxferpage_packet->range_cnt + 1);
709 /* Return it to the freelist */
710 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
711 for (i = count; i != 0; i--) {
712 list_move_tail(listHead.next,
713 &net_device->recv_pkt_list);
715 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
716 flags);
718 netvsc_send_recv_completion(device,
719 vmxferpage_packet->d.trans_id);
721 put_net_device(device);
722 return;
725 /* Remove the 1st packet to represent the xfer page packet itself */
726 xferpage_packet = (struct xferpage_packet *)listHead.next;
727 list_del(&xferpage_packet->list_ent);
729 /* This is how much we can satisfy */
730 xferpage_packet->count = count - 1;
732 if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
733 dev_err(&device->device, "Needed %d netvsc pkts to satisy "
734 "this xfer page...got %d",
735 vmxferpage_packet->range_cnt, xferpage_packet->count);
738 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
739 for (i = 0; i < (count - 1); i++) {
740 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
741 list_del(&netvsc_packet->list_ent);
743 /* Initialize the netvsc packet */
744 netvsc_packet->xfer_page_pkt = xferpage_packet;
745 netvsc_packet->completion.recv.recv_completion =
746 netvsc_receive_completion;
747 netvsc_packet->completion.recv.recv_completion_ctx =
748 netvsc_packet;
749 netvsc_packet->device = device;
750 /* Save this so that we can send it back */
751 netvsc_packet->completion.recv.recv_completion_tid =
752 vmxferpage_packet->d.trans_id;
754 netvsc_packet->total_data_buflen =
755 vmxferpage_packet->ranges[i].byte_count;
756 netvsc_packet->page_buf_cnt = 1;
758 netvsc_packet->page_buf[0].len =
759 vmxferpage_packet->ranges[i].byte_count;
761 start = virt_to_phys((void *)((unsigned long)net_device->
762 recv_buf + vmxferpage_packet->ranges[i].byte_offset));
764 netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
765 end_virtual = (unsigned long)net_device->recv_buf
766 + vmxferpage_packet->ranges[i].byte_offset
767 + vmxferpage_packet->ranges[i].byte_count - 1;
768 end = virt_to_phys((void *)end_virtual);
770 /* Calculate the page relative offset */
771 netvsc_packet->page_buf[0].offset =
772 vmxferpage_packet->ranges[i].byte_offset &
773 (PAGE_SIZE - 1);
774 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
775 /* Handle frame across multiple pages: */
776 netvsc_packet->page_buf[0].len =
777 (netvsc_packet->page_buf[0].pfn <<
778 PAGE_SHIFT)
779 + PAGE_SIZE - start;
780 bytes_remain = netvsc_packet->total_data_buflen -
781 netvsc_packet->page_buf[0].len;
782 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
783 netvsc_packet->page_buf[j].offset = 0;
784 if (bytes_remain <= PAGE_SIZE) {
785 netvsc_packet->page_buf[j].len =
786 bytes_remain;
787 bytes_remain = 0;
788 } else {
789 netvsc_packet->page_buf[j].len =
790 PAGE_SIZE;
791 bytes_remain -= PAGE_SIZE;
793 netvsc_packet->page_buf[j].pfn =
794 virt_to_phys((void *)(end_virtual -
795 bytes_remain)) >> PAGE_SHIFT;
796 netvsc_packet->page_buf_cnt++;
797 if (bytes_remain == 0)
798 break;
802 /* Pass it to the upper layer */
803 rndis_filter_receive(device, netvsc_packet);
805 netvsc_receive_completion(netvsc_packet->
806 completion.recv.recv_completion_ctx);
809 put_net_device(device);
812 static void netvsc_channel_cb(void *context)
814 int ret;
815 struct hv_device *device = context;
816 struct netvsc_device *net_device;
817 u32 bytes_recvd;
818 u64 request_id;
819 unsigned char *packet;
820 struct vmpacket_descriptor *desc;
821 unsigned char *buffer;
822 int bufferlen = NETVSC_PACKET_SIZE;
824 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
825 GFP_ATOMIC);
826 if (!packet)
827 return;
828 buffer = packet;
830 net_device = get_inbound_net_device(device);
831 if (!net_device) {
832 dev_err(&device->device, "net device (%p) shutting down..."
833 "ignoring inbound packets", net_device);
834 goto out;
837 do {
838 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
839 &bytes_recvd, &request_id);
840 if (ret == 0) {
841 if (bytes_recvd > 0) {
842 desc = (struct vmpacket_descriptor *)buffer;
843 switch (desc->type) {
844 case VM_PKT_COMP:
845 netvsc_send_completion(device, desc);
846 break;
848 case VM_PKT_DATA_USING_XFER_PAGES:
849 netvsc_receive(device, desc);
850 break;
852 default:
853 dev_err(&device->device,
854 "unhandled packet type %d, "
855 "tid %llx len %d\n",
856 desc->type, request_id,
857 bytes_recvd);
858 break;
861 /* reset */
862 if (bufferlen > NETVSC_PACKET_SIZE) {
863 kfree(buffer);
864 buffer = packet;
865 bufferlen = NETVSC_PACKET_SIZE;
867 } else {
868 /* reset */
869 if (bufferlen > NETVSC_PACKET_SIZE) {
870 kfree(buffer);
871 buffer = packet;
872 bufferlen = NETVSC_PACKET_SIZE;
875 break;
877 } else if (ret == -ENOBUFS) {
878 /* Handle large packet */
879 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
880 if (buffer == NULL) {
881 /* Try again next time around */
882 dev_err(&device->device,
883 "unable to allocate buffer of size "
884 "(%d)!!", bytes_recvd);
885 break;
888 bufferlen = bytes_recvd;
890 } while (1);
892 put_net_device(device);
893 out:
894 kfree(buffer);
895 return;
899 * netvsc_device_add - Callback when the device belonging to this
900 * driver is added
902 int netvsc_device_add(struct hv_device *device, void *additional_info)
904 int ret = 0;
905 int i;
906 int ring_size =
907 ((struct netvsc_device_info *)additional_info)->ring_size;
908 struct netvsc_device *net_device;
909 struct hv_netvsc_packet *packet, *pos;
911 net_device = alloc_net_device(device);
912 if (!net_device) {
913 ret = -ENOMEM;
914 goto cleanup;
917 /* Initialize the NetVSC channel extension */
918 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
919 spin_lock_init(&net_device->recv_pkt_list_lock);
921 INIT_LIST_HEAD(&net_device->recv_pkt_list);
923 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
924 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
925 (NETVSC_RECEIVE_SG_COUNT *
926 sizeof(struct hv_page_buffer)), GFP_KERNEL);
927 if (!packet)
928 break;
930 list_add_tail(&packet->list_ent,
931 &net_device->recv_pkt_list);
933 init_completion(&net_device->channel_init_wait);
935 /* Open the channel */
936 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
937 ring_size * PAGE_SIZE, NULL, 0,
938 netvsc_channel_cb, device);
940 if (ret != 0) {
941 dev_err(&device->device, "unable to open channel: %d", ret);
942 goto cleanup;
945 /* Channel is opened */
946 pr_info("hv_netvsc channel opened successfully");
948 /* Connect with the NetVsp */
949 ret = netvsc_connect_vsp(device);
950 if (ret != 0) {
951 dev_err(&device->device,
952 "unable to connect to NetVSP - %d", ret);
953 goto close;
956 return ret;
958 close:
959 /* Now, we can close the channel safely */
960 vmbus_close(device->channel);
962 cleanup:
964 if (net_device) {
965 list_for_each_entry_safe(packet, pos,
966 &net_device->recv_pkt_list,
967 list_ent) {
968 list_del(&packet->list_ent);
969 kfree(packet);
972 release_outbound_net_device(device);
973 release_inbound_net_device(device);
975 kfree(net_device);
978 return ret;