staging: hv: fix netvsc sleeping while atomic
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / netvsc.c
blob0edbe7483a4c4606cce94c2f001806ebf23ed234
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/delay.h>
24 #include <linux/io.h>
25 #include <linux/slab.h>
26 #include "osd.h"
27 #include "logging.h"
28 #include "netvsc.h"
29 #include "rndis_filter.h"
30 #include "channel.h"
33 /* Globals */
34 static const char *driver_name = "netvsc";
36 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
37 static const struct hv_guid netvsc_device_type = {
38 .data = {
39 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
40 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
44 static int netvsc_device_add(struct hv_device *device, void *additional_info);
46 static int netvsc_device_remove(struct hv_device *device);
48 static void netvsc_cleanup(struct hv_driver *driver);
50 static void netvsc_channel_cb(void *context);
52 static int netvsc_init_send_buf(struct hv_device *device);
54 static int netvsc_init_recv_buf(struct hv_device *device);
56 static int netvsc_destroy_send_buf(struct netvsc_device *net_device);
58 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device);
60 static int netvsc_connect_vsp(struct hv_device *device);
62 static void netvsc_send_completion(struct hv_device *device,
63 struct vmpacket_descriptor *packet);
65 static int netvsc_send(struct hv_device *device,
66 struct hv_netvsc_packet *packet);
68 static void netvsc_receive(struct hv_device *device,
69 struct vmpacket_descriptor *packet);
71 static void netvsc_receive_completion(void *context);
73 static void netvsc_send_recv_completion(struct hv_device *device,
74 u64 transaction_id);
77 static struct netvsc_device *alloc_net_device(struct hv_device *device)
79 struct netvsc_device *net_device;
81 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
82 if (!net_device)
83 return NULL;
85 /* Set to 2 to allow both inbound and outbound traffic */
86 atomic_cmpxchg(&net_device->refcnt, 0, 2);
88 net_device->dev = device;
89 device->Extension = net_device;
91 return net_device;
94 static void free_net_device(struct netvsc_device *device)
96 WARN_ON(atomic_read(&device->refcnt) == 0);
97 device->dev->Extension = NULL;
98 kfree(device);
102 /* Get the net device object iff exists and its refcount > 1 */
103 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
105 struct netvsc_device *net_device;
107 net_device = device->Extension;
108 if (net_device && atomic_read(&net_device->refcnt) > 1)
109 atomic_inc(&net_device->refcnt);
110 else
111 net_device = NULL;
113 return net_device;
116 /* Get the net device object iff exists and its refcount > 0 */
117 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
119 struct netvsc_device *net_device;
121 net_device = device->Extension;
122 if (net_device && atomic_read(&net_device->refcnt))
123 atomic_inc(&net_device->refcnt);
124 else
125 net_device = NULL;
127 return net_device;
130 static void put_net_device(struct hv_device *device)
132 struct netvsc_device *net_device;
134 net_device = device->Extension;
135 /* ASSERT(netDevice); */
137 atomic_dec(&net_device->refcnt);
140 static struct netvsc_device *release_outbound_net_device(
141 struct hv_device *device)
143 struct netvsc_device *net_device;
145 net_device = device->Extension;
146 if (net_device == NULL)
147 return NULL;
149 /* Busy wait until the ref drop to 2, then set it to 1 */
150 while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2)
151 udelay(100);
153 return net_device;
156 static struct netvsc_device *release_inbound_net_device(
157 struct hv_device *device)
159 struct netvsc_device *net_device;
161 net_device = device->Extension;
162 if (net_device == NULL)
163 return NULL;
165 /* Busy wait until the ref drop to 1, then set it to 0 */
166 while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
167 udelay(100);
169 device->Extension = NULL;
170 return net_device;
174 * netvsc_initialize - Main entry point
176 int netvsc_initialize(struct hv_driver *drv)
178 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
180 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
181 "sizeof(struct nvsp_message)=%zd, "
182 "sizeof(struct vmtransfer_page_packet_header)=%zd",
183 sizeof(struct hv_netvsc_packet),
184 sizeof(struct nvsp_message),
185 sizeof(struct vmtransfer_page_packet_header));
187 /* Make sure we are at least 2 pages since 1 page is used for control */
188 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
190 drv->name = driver_name;
191 memcpy(&drv->deviceType, &netvsc_device_type, sizeof(struct hv_guid));
193 /* Make sure it is set by the caller */
194 /* FIXME: These probably should still be tested in some way */
195 /* ASSERT(driver->OnReceiveCallback); */
196 /* ASSERT(driver->OnLinkStatusChanged); */
198 /* Setup the dispatch table */
199 driver->base.OnDeviceAdd = netvsc_device_add;
200 driver->base.OnDeviceRemove = netvsc_device_remove;
201 driver->base.OnCleanup = netvsc_cleanup;
203 driver->send = netvsc_send;
205 rndis_filter_init(driver);
206 return 0;
209 static int netvsc_init_recv_buf(struct hv_device *device)
211 int ret = 0;
212 struct netvsc_device *net_device;
213 struct nvsp_message *init_packet;
215 net_device = get_outbound_net_device(device);
216 if (!net_device) {
217 DPRINT_ERR(NETVSC, "unable to get net device..."
218 "device being destroyed?");
219 return -1;
221 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
222 /* page-size grandularity */
223 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
225 net_device->recv_buf =
226 osd_page_alloc(net_device->recv_buf_size >> PAGE_SHIFT);
227 if (!net_device->recv_buf) {
228 DPRINT_ERR(NETVSC,
229 "unable to allocate receive buffer of size %d",
230 net_device->recv_buf_size);
231 ret = -1;
232 goto Cleanup;
234 /* page-aligned buffer */
235 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
236 /* 0); */
238 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
241 * Establish the gpadl handle for this buffer on this
242 * channel. Note: This call uses the vmbus connection rather
243 * than the channel to establish the gpadl handle.
245 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
246 net_device->recv_buf_size,
247 &net_device->recv_buf_gpadl_handle);
248 if (ret != 0) {
249 DPRINT_ERR(NETVSC,
250 "unable to establish receive buffer's gpadl");
251 goto Cleanup;
254 /* osd_waitevent_wait(ext->ChannelInitEvent); */
256 /* Notify the NetVsp of the gpadl handle */
257 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
259 init_packet = &net_device->channel_init_pkt;
261 memset(init_packet, 0, sizeof(struct nvsp_message));
263 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
264 init_packet->msg.v1_msg.send_recv_buf.
265 gpadl_handle = net_device->recv_buf_gpadl_handle;
266 init_packet->msg.v1_msg.
267 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
269 /* Send the gpadl notification request */
270 ret = vmbus_sendpacket(device->channel, init_packet,
271 sizeof(struct nvsp_message),
272 (unsigned long)init_packet,
273 VmbusPacketTypeDataInBand,
274 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
275 if (ret != 0) {
276 DPRINT_ERR(NETVSC,
277 "unable to send receive buffer's gpadl to netvsp");
278 goto Cleanup;
281 osd_waitevent_wait(net_device->channel_init_event);
283 /* Check the response */
284 if (init_packet->msg.v1_msg.
285 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
286 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
287 "initialzation with NetVsp - status %d",
288 init_packet->msg.v1_msg.
289 send_recv_buf_complete.status);
290 ret = -1;
291 goto Cleanup;
294 /* Parse the response */
295 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
296 /* ASSERT(netDevice->ReceiveSections == NULL); */
298 net_device->recv_section_cnt = init_packet->msg.
299 v1_msg.send_recv_buf_complete.num_sections;
301 net_device->recv_section = kmalloc(net_device->recv_section_cnt
302 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
303 if (net_device->recv_section == NULL) {
304 ret = -1;
305 goto Cleanup;
308 memcpy(net_device->recv_section,
309 init_packet->msg.v1_msg.
310 send_recv_buf_complete.sections,
311 net_device->recv_section_cnt *
312 sizeof(struct nvsp_1_receive_buffer_section));
314 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
315 "endoffset %d, suballoc size %d, num suballocs %d)",
316 net_device->recv_section_cnt,
317 net_device->recv_section[0].offset,
318 net_device->recv_section[0].end_offset,
319 net_device->recv_section[0].sub_alloc_size,
320 net_device->recv_section[0].num_sub_allocs);
323 * For 1st release, there should only be 1 section that represents the
324 * entire receive buffer
326 if (net_device->recv_section_cnt != 1 ||
327 net_device->recv_section->offset != 0) {
328 ret = -1;
329 goto Cleanup;
332 goto Exit;
334 Cleanup:
335 netvsc_destroy_recv_buf(net_device);
337 Exit:
338 put_net_device(device);
339 return ret;
342 static int netvsc_init_send_buf(struct hv_device *device)
344 int ret = 0;
345 struct netvsc_device *net_device;
346 struct nvsp_message *init_packet;
348 net_device = get_outbound_net_device(device);
349 if (!net_device) {
350 DPRINT_ERR(NETVSC, "unable to get net device..."
351 "device being destroyed?");
352 return -1;
354 if (net_device->send_buf_size <= 0) {
355 ret = -EINVAL;
356 goto Cleanup;
359 /* page-size grandularity */
360 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
362 net_device->send_buf =
363 osd_page_alloc(net_device->send_buf_size >> PAGE_SHIFT);
364 if (!net_device->send_buf) {
365 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
366 net_device->send_buf_size);
367 ret = -1;
368 goto Cleanup;
370 /* page-aligned buffer */
371 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
373 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
376 * Establish the gpadl handle for this buffer on this
377 * channel. Note: This call uses the vmbus connection rather
378 * than the channel to establish the gpadl handle.
380 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
381 net_device->send_buf_size,
382 &net_device->send_buf_gpadl_handle);
383 if (ret != 0) {
384 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
385 goto Cleanup;
388 /* osd_waitevent_wait(ext->ChannelInitEvent); */
390 /* Notify the NetVsp of the gpadl handle */
391 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
393 init_packet = &net_device->channel_init_pkt;
395 memset(init_packet, 0, sizeof(struct nvsp_message));
397 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
398 init_packet->msg.v1_msg.send_recv_buf.
399 gpadl_handle = net_device->send_buf_gpadl_handle;
400 init_packet->msg.v1_msg.send_recv_buf.id =
401 NETVSC_SEND_BUFFER_ID;
403 /* Send the gpadl notification request */
404 ret = vmbus_sendpacket(device->channel, init_packet,
405 sizeof(struct nvsp_message),
406 (unsigned long)init_packet,
407 VmbusPacketTypeDataInBand,
408 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
409 if (ret != 0) {
410 DPRINT_ERR(NETVSC,
411 "unable to send receive buffer's gpadl to netvsp");
412 goto Cleanup;
415 osd_waitevent_wait(net_device->channel_init_event);
417 /* Check the response */
418 if (init_packet->msg.v1_msg.
419 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
420 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
421 "initialzation with NetVsp - status %d",
422 init_packet->msg.v1_msg.
423 send_send_buf_complete.status);
424 ret = -1;
425 goto Cleanup;
428 net_device->send_section_size = init_packet->
429 msg.v1_msg.send_send_buf_complete.section_size;
431 goto Exit;
433 Cleanup:
434 netvsc_destroy_send_buf(net_device);
436 Exit:
437 put_net_device(device);
438 return ret;
441 static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
443 struct nvsp_message *revoke_packet;
444 int ret = 0;
447 * If we got a section count, it means we received a
448 * SendReceiveBufferComplete msg (ie sent
449 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
450 * to send a revoke msg here
452 if (net_device->recv_section_cnt) {
453 DPRINT_INFO(NETVSC,
454 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
456 /* Send the revoke receive buffer */
457 revoke_packet = &net_device->revoke_packet;
458 memset(revoke_packet, 0, sizeof(struct nvsp_message));
460 revoke_packet->hdr.msg_type =
461 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
462 revoke_packet->msg.v1_msg.
463 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
465 ret = vmbus_sendpacket(net_device->dev->channel,
466 revoke_packet,
467 sizeof(struct nvsp_message),
468 (unsigned long)revoke_packet,
469 VmbusPacketTypeDataInBand, 0);
471 * If we failed here, we might as well return and
472 * have a leak rather than continue and a bugchk
474 if (ret != 0) {
475 DPRINT_ERR(NETVSC, "unable to send revoke receive "
476 "buffer to netvsp");
477 return -1;
481 /* Teardown the gpadl on the vsp end */
482 if (net_device->recv_buf_gpadl_handle) {
483 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
485 ret = vmbus_teardown_gpadl(net_device->dev->channel,
486 net_device->recv_buf_gpadl_handle);
488 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
489 if (ret != 0) {
490 DPRINT_ERR(NETVSC,
491 "unable to teardown receive buffer's gpadl");
492 return -1;
494 net_device->recv_buf_gpadl_handle = 0;
497 if (net_device->recv_buf) {
498 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
500 /* Free up the receive buffer */
501 osd_page_free(net_device->recv_buf,
502 net_device->recv_buf_size >> PAGE_SHIFT);
503 net_device->recv_buf = NULL;
506 if (net_device->recv_section) {
507 net_device->recv_section_cnt = 0;
508 kfree(net_device->recv_section);
509 net_device->recv_section = NULL;
512 return ret;
515 static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
517 struct nvsp_message *revoke_packet;
518 int ret = 0;
521 * If we got a section count, it means we received a
522 * SendReceiveBufferComplete msg (ie sent
523 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
524 * to send a revoke msg here
526 if (net_device->send_section_size) {
527 DPRINT_INFO(NETVSC,
528 "Sending NvspMessage1TypeRevokeSendBuffer...");
530 /* Send the revoke send buffer */
531 revoke_packet = &net_device->revoke_packet;
532 memset(revoke_packet, 0, sizeof(struct nvsp_message));
534 revoke_packet->hdr.msg_type =
535 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
536 revoke_packet->msg.v1_msg.
537 revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
539 ret = vmbus_sendpacket(net_device->dev->channel,
540 revoke_packet,
541 sizeof(struct nvsp_message),
542 (unsigned long)revoke_packet,
543 VmbusPacketTypeDataInBand, 0);
545 * If we failed here, we might as well return and have a leak
546 * rather than continue and a bugchk
548 if (ret != 0) {
549 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
550 "to netvsp");
551 return -1;
555 /* Teardown the gpadl on the vsp end */
556 if (net_device->send_buf_gpadl_handle) {
557 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
558 ret = vmbus_teardown_gpadl(net_device->dev->channel,
559 net_device->send_buf_gpadl_handle);
562 * If we failed here, we might as well return and have a leak
563 * rather than continue and a bugchk
565 if (ret != 0) {
566 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
567 "gpadl");
568 return -1;
570 net_device->send_buf_gpadl_handle = 0;
573 if (net_device->send_buf) {
574 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
576 /* Free up the receive buffer */
577 osd_page_free(net_device->send_buf,
578 net_device->send_buf_size >> PAGE_SHIFT);
579 net_device->send_buf = NULL;
582 return ret;
586 static int netvsc_connect_vsp(struct hv_device *device)
588 int ret;
589 struct netvsc_device *net_device;
590 struct nvsp_message *init_packet;
591 int ndis_version;
593 net_device = get_outbound_net_device(device);
594 if (!net_device) {
595 DPRINT_ERR(NETVSC, "unable to get net device..."
596 "device being destroyed?");
597 return -1;
600 init_packet = &net_device->channel_init_pkt;
602 memset(init_packet, 0, sizeof(struct nvsp_message));
603 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
604 init_packet->msg.init_msg.init.min_protocol_ver =
605 NVSP_MIN_PROTOCOL_VERSION;
606 init_packet->msg.init_msg.init.max_protocol_ver =
607 NVSP_MAX_PROTOCOL_VERSION;
609 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
611 /* Send the init request */
612 ret = vmbus_sendpacket(device->channel, init_packet,
613 sizeof(struct nvsp_message),
614 (unsigned long)init_packet,
615 VmbusPacketTypeDataInBand,
616 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
618 if (ret != 0) {
619 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
620 goto Cleanup;
623 osd_waitevent_wait(net_device->channel_init_event);
625 /* Now, check the response */
626 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
627 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
628 init_packet->msg.init_msg.init_complete.status,
629 init_packet->msg.init_msg.
630 init_complete.max_mdl_chain_len);
632 if (init_packet->msg.init_msg.init_complete.status !=
633 NVSP_STAT_SUCCESS) {
634 DPRINT_ERR(NETVSC,
635 "unable to initialize with netvsp (status 0x%x)",
636 init_packet->msg.init_msg.init_complete.status);
637 ret = -1;
638 goto Cleanup;
641 if (init_packet->msg.init_msg.init_complete.
642 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
643 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
644 "(version expected 1 got %d)",
645 init_packet->msg.init_msg.
646 init_complete.negotiated_protocol_ver);
647 ret = -1;
648 goto Cleanup;
650 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
652 /* Send the ndis version */
653 memset(init_packet, 0, sizeof(struct nvsp_message));
655 ndis_version = 0x00050000;
657 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
658 init_packet->msg.v1_msg.
659 send_ndis_ver.ndis_major_ver =
660 (ndis_version & 0xFFFF0000) >> 16;
661 init_packet->msg.v1_msg.
662 send_ndis_ver.ndis_minor_ver =
663 ndis_version & 0xFFFF;
665 /* Send the init request */
666 ret = vmbus_sendpacket(device->channel, init_packet,
667 sizeof(struct nvsp_message),
668 (unsigned long)init_packet,
669 VmbusPacketTypeDataInBand, 0);
670 if (ret != 0) {
671 DPRINT_ERR(NETVSC,
672 "unable to send NvspMessage1TypeSendNdisVersion");
673 ret = -1;
674 goto Cleanup;
677 * BUGBUG - We have to wait for the above msg since the
678 * netvsp uses KMCL which acknowledges packet (completion
679 * packet) since our Vmbus always set the
680 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
682 /* osd_waitevent_wait(NetVscChannel->ChannelInitEvent); */
684 /* Post the big receive buffer to NetVSP */
685 ret = netvsc_init_recv_buf(device);
686 if (ret == 0)
687 ret = netvsc_init_send_buf(device);
689 Cleanup:
690 put_net_device(device);
691 return ret;
694 static void NetVscDisconnectFromVsp(struct netvsc_device *net_device)
696 netvsc_destroy_recv_buf(net_device);
697 netvsc_destroy_send_buf(net_device);
701 * netvsc_device_add - Callback when the device belonging to this
702 * driver is added
704 static int netvsc_device_add(struct hv_device *device, void *additional_info)
706 int ret = 0;
707 int i;
708 struct netvsc_device *net_device;
709 struct hv_netvsc_packet *packet, *pos;
710 struct netvsc_driver *net_driver =
711 (struct netvsc_driver *)device->Driver;
713 net_device = alloc_net_device(device);
714 if (!net_device) {
715 ret = -1;
716 goto Cleanup;
719 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", net_device);
721 /* Initialize the NetVSC channel extension */
722 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
723 spin_lock_init(&net_device->recv_pkt_list_lock);
725 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
727 INIT_LIST_HEAD(&net_device->recv_pkt_list);
729 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
730 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
731 (NETVSC_RECEIVE_SG_COUNT *
732 sizeof(struct hv_page_buffer)), GFP_KERNEL);
733 if (!packet) {
734 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
735 "for receive pool (wanted %d got %d)",
736 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
737 break;
739 list_add_tail(&packet->list_ent,
740 &net_device->recv_pkt_list);
742 net_device->channel_init_event = osd_waitevent_create();
743 if (!net_device->channel_init_event) {
744 ret = -ENOMEM;
745 goto Cleanup;
748 /* Open the channel */
749 ret = vmbus_open(device->channel, net_driver->ring_buf_size,
750 net_driver->ring_buf_size, NULL, 0,
751 netvsc_channel_cb, device);
753 if (ret != 0) {
754 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
755 ret = -1;
756 goto Cleanup;
759 /* Channel is opened */
760 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
762 /* Connect with the NetVsp */
763 ret = netvsc_connect_vsp(device);
764 if (ret != 0) {
765 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
766 ret = -1;
767 goto close;
770 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
771 ret);
773 return ret;
775 close:
776 /* Now, we can close the channel safely */
777 vmbus_close(device->channel);
779 Cleanup:
781 if (net_device) {
782 kfree(net_device->channel_init_event);
784 list_for_each_entry_safe(packet, pos,
785 &net_device->recv_pkt_list,
786 list_ent) {
787 list_del(&packet->list_ent);
788 kfree(packet);
791 release_outbound_net_device(device);
792 release_inbound_net_device(device);
794 free_net_device(net_device);
797 return ret;
801 * netvsc_device_remove - Callback when the root bus device is removed
803 static int netvsc_device_remove(struct hv_device *device)
805 struct netvsc_device *net_device;
806 struct hv_netvsc_packet *netvsc_packet, *pos;
808 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
809 device->Extension);
811 /* Stop outbound traffic ie sends and receives completions */
812 net_device = release_outbound_net_device(device);
813 if (!net_device) {
814 DPRINT_ERR(NETVSC, "No net device present!!");
815 return -1;
818 /* Wait for all send completions */
819 while (atomic_read(&net_device->num_outstanding_sends)) {
820 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
821 atomic_read(&net_device->num_outstanding_sends));
822 udelay(100);
825 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
827 NetVscDisconnectFromVsp(net_device);
829 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
830 device->Extension);
832 /* Stop inbound traffic ie receives and sends completions */
833 net_device = release_inbound_net_device(device);
835 /* At this point, no one should be accessing netDevice except in here */
836 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", net_device);
838 /* Now, we can close the channel safely */
839 vmbus_close(device->channel);
841 /* Release all resources */
842 list_for_each_entry_safe(netvsc_packet, pos,
843 &net_device->recv_pkt_list, list_ent) {
844 list_del(&netvsc_packet->list_ent);
845 kfree(netvsc_packet);
848 kfree(net_device->channel_init_event);
849 free_net_device(net_device);
850 return 0;
854 * netvsc_cleanup - Perform any cleanup when the driver is removed
856 static void netvsc_cleanup(struct hv_driver *drv)
860 static void netvsc_send_completion(struct hv_device *device,
861 struct vmpacket_descriptor *packet)
863 struct netvsc_device *net_device;
864 struct nvsp_message *nvsp_packet;
865 struct hv_netvsc_packet *nvsc_packet;
867 net_device = get_inbound_net_device(device);
868 if (!net_device) {
869 DPRINT_ERR(NETVSC, "unable to get net device..."
870 "device being destroyed?");
871 return;
874 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
875 (packet->DataOffset8 << 3));
877 DPRINT_DBG(NETVSC, "send completion packet - type %d",
878 nvsp_packet->hdr.msg_type);
880 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
881 (nvsp_packet->hdr.msg_type ==
882 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
883 (nvsp_packet->hdr.msg_type ==
884 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
885 /* Copy the response back */
886 memcpy(&net_device->channel_init_pkt, nvsp_packet,
887 sizeof(struct nvsp_message));
888 osd_waitevent_set(net_device->channel_init_event);
889 } else if (nvsp_packet->hdr.msg_type ==
890 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
891 /* Get the send context */
892 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
893 packet->TransactionId;
894 /* ASSERT(nvscPacket); */
896 /* Notify the layer above us */
897 nvsc_packet->completion.send.send_completion(
898 nvsc_packet->completion.send.send_completion_ctx);
900 atomic_dec(&net_device->num_outstanding_sends);
901 } else {
902 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
903 "%d received!!", nvsp_packet->hdr.msg_type);
906 put_net_device(device);
909 static int netvsc_send(struct hv_device *device,
910 struct hv_netvsc_packet *packet)
912 struct netvsc_device *net_device;
913 int ret = 0;
915 struct nvsp_message sendMessage;
917 net_device = get_outbound_net_device(device);
918 if (!net_device) {
919 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
920 "ignoring outbound packets", net_device);
921 return -2;
924 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
925 if (packet->is_data_pkt) {
926 /* 0 is RMC_DATA; */
927 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
928 } else {
929 /* 1 is RMC_CONTROL; */
930 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
933 /* Not using send buffer section */
934 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
935 0xFFFFFFFF;
936 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
938 if (packet->page_buf_cnt) {
939 ret = vmbus_sendpacket_pagebuffer(device->channel,
940 packet->page_buf,
941 packet->page_buf_cnt,
942 &sendMessage,
943 sizeof(struct nvsp_message),
944 (unsigned long)packet);
945 } else {
946 ret = vmbus_sendpacket(device->channel, &sendMessage,
947 sizeof(struct nvsp_message),
948 (unsigned long)packet,
949 VmbusPacketTypeDataInBand,
950 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
954 if (ret != 0)
955 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
956 packet, ret);
958 atomic_inc(&net_device->num_outstanding_sends);
959 put_net_device(device);
960 return ret;
963 static void netvsc_receive(struct hv_device *device,
964 struct vmpacket_descriptor *packet)
966 struct netvsc_device *net_device;
967 struct vmtransfer_page_packet_header *vmxferpage_packet;
968 struct nvsp_message *nvsp_packet;
969 struct hv_netvsc_packet *netvsc_packet = NULL;
970 unsigned long start;
971 unsigned long end, end_virtual;
972 /* struct netvsc_driver *netvscDriver; */
973 struct xferpage_packet *xferpage_packet = NULL;
974 int i, j;
975 int count = 0, bytes_remain = 0;
976 unsigned long flags;
977 LIST_HEAD(listHead);
979 net_device = get_inbound_net_device(device);
980 if (!net_device) {
981 DPRINT_ERR(NETVSC, "unable to get net device..."
982 "device being destroyed?");
983 return;
987 * All inbound packets other than send completion should be xfer page
988 * packet
990 if (packet->Type != VmbusPacketTypeDataUsingTransferPages) {
991 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
992 packet->Type);
993 put_net_device(device);
994 return;
997 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
998 (packet->DataOffset8 << 3));
1000 /* Make sure this is a valid nvsp packet */
1001 if (nvsp_packet->hdr.msg_type !=
1002 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
1003 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1004 nvsp_packet->hdr.msg_type);
1005 put_net_device(device);
1006 return;
1009 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1010 nvsp_packet->hdr.msg_type);
1012 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
1014 if (vmxferpage_packet->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1015 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1016 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1017 vmxferpage_packet->TransferPageSetId);
1018 put_net_device(device);
1019 return;
1022 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1023 vmxferpage_packet->RangeCount);
1026 * Grab free packets (range count + 1) to represent this xfer
1027 * page packet. +1 to represent the xfer page packet itself.
1028 * We grab it here so that we know exactly how many we can
1029 * fulfil
1031 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1032 while (!list_empty(&net_device->recv_pkt_list)) {
1033 list_move_tail(net_device->recv_pkt_list.next, &listHead);
1034 if (++count == vmxferpage_packet->RangeCount + 1)
1035 break;
1037 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
1040 * We need at least 2 netvsc pkts (1 to represent the xfer
1041 * page and at least 1 for the range) i.e. we can handled
1042 * some of the xfer page packet ranges...
1044 if (count < 2) {
1045 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1046 "Dropping this xfer page packet completely!",
1047 count, vmxferpage_packet->RangeCount + 1);
1049 /* Return it to the freelist */
1050 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1051 for (i = count; i != 0; i--) {
1052 list_move_tail(listHead.next,
1053 &net_device->recv_pkt_list);
1055 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
1056 flags);
1058 netvsc_send_recv_completion(device,
1059 vmxferpage_packet->d.TransactionId);
1061 put_net_device(device);
1062 return;
1065 /* Remove the 1st packet to represent the xfer page packet itself */
1066 xferpage_packet = (struct xferpage_packet *)listHead.next;
1067 list_del(&xferpage_packet->list_ent);
1069 /* This is how much we can satisfy */
1070 xferpage_packet->count = count - 1;
1071 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1072 /* vmxferpagePacket->RangeCount); */
1074 if (xferpage_packet->count != vmxferpage_packet->RangeCount) {
1075 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1076 "page...got %d", vmxferpage_packet->RangeCount,
1077 xferpage_packet->count);
1080 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1081 for (i = 0; i < (count - 1); i++) {
1082 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
1083 list_del(&netvsc_packet->list_ent);
1085 /* Initialize the netvsc packet */
1086 netvsc_packet->xfer_page_pkt = xferpage_packet;
1087 netvsc_packet->completion.recv.recv_completion =
1088 netvsc_receive_completion;
1089 netvsc_packet->completion.recv.recv_completion_ctx =
1090 netvsc_packet;
1091 netvsc_packet->device = device;
1092 /* Save this so that we can send it back */
1093 netvsc_packet->completion.recv.recv_completion_tid =
1094 vmxferpage_packet->d.TransactionId;
1096 netvsc_packet->total_data_buflen =
1097 vmxferpage_packet->Ranges[i].ByteCount;
1098 netvsc_packet->page_buf_cnt = 1;
1100 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1101 /* vmxferpagePacket->Ranges[i].ByteCount < */
1102 /* netDevice->ReceiveBufferSize); */
1104 netvsc_packet->page_buf[0].Length =
1105 vmxferpage_packet->Ranges[i].ByteCount;
1107 start = virt_to_phys((void *)((unsigned long)net_device->
1108 recv_buf + vmxferpage_packet->Ranges[i].ByteOffset));
1110 netvsc_packet->page_buf[0].Pfn = start >> PAGE_SHIFT;
1111 end_virtual = (unsigned long)net_device->recv_buf
1112 + vmxferpage_packet->Ranges[i].ByteOffset
1113 + vmxferpage_packet->Ranges[i].ByteCount - 1;
1114 end = virt_to_phys((void *)end_virtual);
1116 /* Calculate the page relative offset */
1117 netvsc_packet->page_buf[0].Offset =
1118 vmxferpage_packet->Ranges[i].ByteOffset &
1119 (PAGE_SIZE - 1);
1120 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1121 /* Handle frame across multiple pages: */
1122 netvsc_packet->page_buf[0].Length =
1123 (netvsc_packet->page_buf[0].Pfn <<
1124 PAGE_SHIFT)
1125 + PAGE_SIZE - start;
1126 bytes_remain = netvsc_packet->total_data_buflen -
1127 netvsc_packet->page_buf[0].Length;
1128 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1129 netvsc_packet->page_buf[j].Offset = 0;
1130 if (bytes_remain <= PAGE_SIZE) {
1131 netvsc_packet->page_buf[j].Length =
1132 bytes_remain;
1133 bytes_remain = 0;
1134 } else {
1135 netvsc_packet->page_buf[j].Length =
1136 PAGE_SIZE;
1137 bytes_remain -= PAGE_SIZE;
1139 netvsc_packet->page_buf[j].Pfn =
1140 virt_to_phys((void *)(end_virtual -
1141 bytes_remain)) >> PAGE_SHIFT;
1142 netvsc_packet->page_buf_cnt++;
1143 if (bytes_remain == 0)
1144 break;
1146 /* ASSERT(bytesRemain == 0); */
1148 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1149 "(pfn %llx, offset %u, len %u)", i,
1150 vmxferpage_packet->Ranges[i].ByteOffset,
1151 vmxferpage_packet->Ranges[i].ByteCount,
1152 netvsc_packet->page_buf[0].Pfn,
1153 netvsc_packet->page_buf[0].Offset,
1154 netvsc_packet->page_buf[0].Length);
1156 /* Pass it to the upper layer */
1157 ((struct netvsc_driver *)device->Driver)->
1158 recv_cb(device, netvsc_packet);
1160 netvsc_receive_completion(netvsc_packet->
1161 completion.recv.recv_completion_ctx);
1164 /* ASSERT(list_empty(&listHead)); */
1166 put_net_device(device);
1169 static void netvsc_send_recv_completion(struct hv_device *device,
1170 u64 transaction_id)
1172 struct nvsp_message recvcompMessage;
1173 int retries = 0;
1174 int ret;
1176 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1177 transaction_id);
1179 recvcompMessage.hdr.msg_type =
1180 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
1182 /* FIXME: Pass in the status */
1183 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
1184 NVSP_STAT_SUCCESS;
1186 retry_send_cmplt:
1187 /* Send the completion */
1188 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
1189 sizeof(struct nvsp_message), transaction_id,
1190 VmbusPacketTypeCompletion, 0);
1191 if (ret == 0) {
1192 /* success */
1193 /* no-op */
1194 } else if (ret == -1) {
1195 /* no more room...wait a bit and attempt to retry 3 times */
1196 retries++;
1197 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1198 "(tid %llx)...retrying %d", transaction_id, retries);
1200 if (retries < 4) {
1201 udelay(100);
1202 goto retry_send_cmplt;
1203 } else {
1204 DPRINT_ERR(NETVSC, "unable to send receive completion "
1205 "pkt (tid %llx)...give up retrying",
1206 transaction_id);
1208 } else {
1209 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1210 "%llx", transaction_id);
1214 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1215 static void netvsc_receive_completion(void *context)
1217 struct hv_netvsc_packet *packet = context;
1218 struct hv_device *device = (struct hv_device *)packet->device;
1219 struct netvsc_device *net_device;
1220 u64 transaction_id = 0;
1221 bool fsend_receive_comp = false;
1222 unsigned long flags;
1224 /* ASSERT(packet->XferPagePacket); */
1227 * Even though it seems logical to do a GetOutboundNetDevice() here to
1228 * send out receive completion, we are using GetInboundNetDevice()
1229 * since we may have disable outbound traffic already.
1231 net_device = get_inbound_net_device(device);
1232 if (!net_device) {
1233 DPRINT_ERR(NETVSC, "unable to get net device..."
1234 "device being destroyed?");
1235 return;
1238 /* Overloading use of the lock. */
1239 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1241 /* ASSERT(packet->XferPagePacket->Count > 0); */
1242 packet->xfer_page_pkt->count--;
1245 * Last one in the line that represent 1 xfer page packet.
1246 * Return the xfer page packet itself to the freelist
1248 if (packet->xfer_page_pkt->count == 0) {
1249 fsend_receive_comp = true;
1250 transaction_id = packet->completion.recv.recv_completion_tid;
1251 list_add_tail(&packet->xfer_page_pkt->list_ent,
1252 &net_device->recv_pkt_list);
1256 /* Put the packet back */
1257 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
1258 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
1260 /* Send a receive completion for the xfer page packet */
1261 if (fsend_receive_comp)
1262 netvsc_send_recv_completion(device, transaction_id);
1264 put_net_device(device);
1267 static void netvsc_channel_cb(void *context)
1269 int ret;
1270 struct hv_device *device = context;
1271 struct netvsc_device *net_device;
1272 u32 bytes_recvd;
1273 u64 request_id;
1274 unsigned char *packet;
1275 struct vmpacket_descriptor *desc;
1276 unsigned char *buffer;
1277 int bufferlen = NETVSC_PACKET_SIZE;
1279 /* ASSERT(device); */
1281 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1282 GFP_ATOMIC);
1283 if (!packet)
1284 return;
1285 buffer = packet;
1287 net_device = get_inbound_net_device(device);
1288 if (!net_device) {
1289 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1290 "ignoring inbound packets", net_device);
1291 goto out;
1294 do {
1295 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
1296 &bytes_recvd, &request_id);
1297 if (ret == 0) {
1298 if (bytes_recvd > 0) {
1299 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1300 bytes_recvd, request_id);
1302 desc = (struct vmpacket_descriptor *)buffer;
1303 switch (desc->Type) {
1304 case VmbusPacketTypeCompletion:
1305 netvsc_send_completion(device, desc);
1306 break;
1308 case VmbusPacketTypeDataUsingTransferPages:
1309 netvsc_receive(device, desc);
1310 break;
1312 default:
1313 DPRINT_ERR(NETVSC,
1314 "unhandled packet type %d, "
1315 "tid %llx len %d\n",
1316 desc->Type, request_id,
1317 bytes_recvd);
1318 break;
1321 /* reset */
1322 if (bufferlen > NETVSC_PACKET_SIZE) {
1323 kfree(buffer);
1324 buffer = packet;
1325 bufferlen = NETVSC_PACKET_SIZE;
1327 } else {
1328 /* reset */
1329 if (bufferlen > NETVSC_PACKET_SIZE) {
1330 kfree(buffer);
1331 buffer = packet;
1332 bufferlen = NETVSC_PACKET_SIZE;
1335 break;
1337 } else if (ret == -2) {
1338 /* Handle large packet */
1339 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1340 if (buffer == NULL) {
1341 /* Try again next time around */
1342 DPRINT_ERR(NETVSC,
1343 "unable to allocate buffer of size "
1344 "(%d)!!", bytes_recvd);
1345 break;
1348 bufferlen = bytes_recvd;
1350 } while (1);
1352 put_net_device(device);
1353 out:
1354 kfree(buffer);
1355 return;