3f79f04a20646c47f0fc0ed76a6b373c57ee0eeb
[deliverable/linux.git] / drivers / staging / hv / netvsc.c
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
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.
7 *
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.
12 *
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.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 */
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
31
32 /* Globals */
33 static const char *gDriverName = "netvsc";
34
35 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
36 static const struct hv_guid gNetVscDeviceType = {
37 .data = {
38 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
39 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
40 }
41 };
42
43 static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo);
44
45 static int NetVscOnDeviceRemove(struct hv_device *Device);
46
47 static void NetVscOnCleanup(struct hv_driver *Driver);
48
49 static void NetVscOnChannelCallback(void *context);
50
51 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device);
52
53 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device);
54
55 static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice);
56
57 static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice);
58
59 static int NetVscConnectToVsp(struct hv_device *Device);
60
61 static void NetVscOnSendCompletion(struct hv_device *Device,
62 struct vmpacket_descriptor *Packet);
63
64 static int NetVscOnSend(struct hv_device *Device,
65 struct hv_netvsc_packet *Packet);
66
67 static void NetVscOnReceive(struct hv_device *Device,
68 struct vmpacket_descriptor *Packet);
69
70 static void NetVscOnReceiveCompletion(void *Context);
71
72 static void NetVscSendReceiveCompletion(struct hv_device *Device,
73 u64 TransactionId);
74
75
76 static struct netvsc_device *AllocNetDevice(struct hv_device *Device)
77 {
78 struct netvsc_device *netDevice;
79
80 netDevice = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
81 if (!netDevice)
82 return NULL;
83
84 /* Set to 2 to allow both inbound and outbound traffic */
85 atomic_cmpxchg(&netDevice->RefCount, 0, 2);
86
87 netDevice->Device = Device;
88 Device->Extension = netDevice;
89
90 return netDevice;
91 }
92
93 static void FreeNetDevice(struct netvsc_device *Device)
94 {
95 WARN_ON(atomic_read(&Device->RefCount) == 0);
96 Device->Device->Extension = NULL;
97 kfree(Device);
98 }
99
100
101 /* Get the net device object iff exists and its refcount > 1 */
102 static struct netvsc_device *GetOutboundNetDevice(struct hv_device *Device)
103 {
104 struct netvsc_device *netDevice;
105
106 netDevice = Device->Extension;
107 if (netDevice && atomic_read(&netDevice->RefCount) > 1)
108 atomic_inc(&netDevice->RefCount);
109 else
110 netDevice = NULL;
111
112 return netDevice;
113 }
114
115 /* Get the net device object iff exists and its refcount > 0 */
116 static struct netvsc_device *GetInboundNetDevice(struct hv_device *Device)
117 {
118 struct netvsc_device *netDevice;
119
120 netDevice = Device->Extension;
121 if (netDevice && atomic_read(&netDevice->RefCount))
122 atomic_inc(&netDevice->RefCount);
123 else
124 netDevice = NULL;
125
126 return netDevice;
127 }
128
129 static void PutNetDevice(struct hv_device *Device)
130 {
131 struct netvsc_device *netDevice;
132
133 netDevice = Device->Extension;
134 /* ASSERT(netDevice); */
135
136 atomic_dec(&netDevice->RefCount);
137 }
138
139 static struct netvsc_device *ReleaseOutboundNetDevice(struct hv_device *Device)
140 {
141 struct netvsc_device *netDevice;
142
143 netDevice = Device->Extension;
144 if (netDevice == NULL)
145 return NULL;
146
147 /* Busy wait until the ref drop to 2, then set it to 1 */
148 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
149 udelay(100);
150
151 return netDevice;
152 }
153
154 static struct netvsc_device *ReleaseInboundNetDevice(struct hv_device *Device)
155 {
156 struct netvsc_device *netDevice;
157
158 netDevice = Device->Extension;
159 if (netDevice == NULL)
160 return NULL;
161
162 /* Busy wait until the ref drop to 1, then set it to 0 */
163 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
164 udelay(100);
165
166 Device->Extension = NULL;
167 return netDevice;
168 }
169
170 /*
171 * NetVscInitialize - Main entry point
172 */
173 int NetVscInitialize(struct hv_driver *drv)
174 {
175 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
176
177 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
178 "sizeof(struct nvsp_message)=%zd, "
179 "sizeof(struct vmtransfer_page_packet_header)=%zd",
180 sizeof(struct hv_netvsc_packet),
181 sizeof(struct nvsp_message),
182 sizeof(struct vmtransfer_page_packet_header));
183
184 /* Make sure we are at least 2 pages since 1 page is used for control */
185 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
186
187 drv->name = gDriverName;
188 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
189
190 /* Make sure it is set by the caller */
191 /* FIXME: These probably should still be tested in some way */
192 /* ASSERT(driver->OnReceiveCallback); */
193 /* ASSERT(driver->OnLinkStatusChanged); */
194
195 /* Setup the dispatch table */
196 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
197 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
198 driver->Base.OnCleanup = NetVscOnCleanup;
199
200 driver->OnSend = NetVscOnSend;
201
202 RndisFilterInit(driver);
203
204 DPRINT_EXIT(NETVSC);
205
206 return 0;
207 }
208
209 static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device)
210 {
211 int ret = 0;
212 struct netvsc_device *netDevice;
213 struct nvsp_message *initPacket;
214
215 netDevice = GetOutboundNetDevice(Device);
216 if (!netDevice) {
217 DPRINT_ERR(NETVSC, "unable to get net device..."
218 "device being destroyed?");
219 DPRINT_EXIT(NETVSC);
220 return -1;
221 }
222 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
223 /* page-size grandularity */
224 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
225
226 netDevice->ReceiveBuffer =
227 osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
228 if (!netDevice->ReceiveBuffer) {
229 DPRINT_ERR(NETVSC,
230 "unable to allocate receive buffer of size %d",
231 netDevice->ReceiveBufferSize);
232 ret = -1;
233 goto Cleanup;
234 }
235 /* page-aligned buffer */
236 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
237 /* 0); */
238
239 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
240
241 /*
242 * Establish the gpadl handle for this buffer on this
243 * channel. Note: This call uses the vmbus connection rather
244 * than the channel to establish the gpadl handle.
245 */
246 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
247 netDevice->ReceiveBuffer,
248 netDevice->ReceiveBufferSize,
249 &netDevice->ReceiveBufferGpadlHandle);
250 if (ret != 0) {
251 DPRINT_ERR(NETVSC,
252 "unable to establish receive buffer's gpadl");
253 goto Cleanup;
254 }
255
256 /* osd_WaitEventWait(ext->ChannelInitEvent); */
257
258 /* Notify the NetVsp of the gpadl handle */
259 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
260
261 initPacket = &netDevice->ChannelInitPacket;
262
263 memset(initPacket, 0, sizeof(struct nvsp_message));
264
265 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
266 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
267 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
268
269 /* Send the gpadl notification request */
270 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
271 initPacket,
272 sizeof(struct nvsp_message),
273 (unsigned long)initPacket,
274 VmbusPacketTypeDataInBand,
275 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
276 if (ret != 0) {
277 DPRINT_ERR(NETVSC,
278 "unable to send receive buffer's gpadl to netvsp");
279 goto Cleanup;
280 }
281
282 osd_WaitEventWait(netDevice->ChannelInitEvent);
283
284 /* Check the response */
285 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess) {
286 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
287 "initialzation with NetVsp - status %d",
288 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
289 ret = -1;
290 goto Cleanup;
291 }
292
293 /* Parse the response */
294 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
295 /* ASSERT(netDevice->ReceiveSections == NULL); */
296
297 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
298
299 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
300 if (netDevice->ReceiveSections == NULL) {
301 ret = -1;
302 goto Cleanup;
303 }
304
305 memcpy(netDevice->ReceiveSections,
306 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
307 netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section));
308
309 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
310 "endoffset %d, suballoc size %d, num suballocs %d)",
311 netDevice->ReceiveSectionCount,
312 netDevice->ReceiveSections[0].Offset,
313 netDevice->ReceiveSections[0].EndOffset,
314 netDevice->ReceiveSections[0].SubAllocationSize,
315 netDevice->ReceiveSections[0].NumSubAllocations);
316
317 /*
318 * For 1st release, there should only be 1 section that represents the
319 * entire receive buffer
320 */
321 if (netDevice->ReceiveSectionCount != 1 ||
322 netDevice->ReceiveSections->Offset != 0) {
323 ret = -1;
324 goto Cleanup;
325 }
326
327 goto Exit;
328
329 Cleanup:
330 NetVscDestroyReceiveBuffer(netDevice);
331
332 Exit:
333 PutNetDevice(Device);
334 DPRINT_EXIT(NETVSC);
335 return ret;
336 }
337
338 static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device)
339 {
340 int ret = 0;
341 struct netvsc_device *netDevice;
342 struct nvsp_message *initPacket;
343
344 netDevice = GetOutboundNetDevice(Device);
345 if (!netDevice) {
346 DPRINT_ERR(NETVSC, "unable to get net device..."
347 "device being destroyed?");
348 DPRINT_EXIT(NETVSC);
349 return -1;
350 }
351 if (netDevice->SendBufferSize <= 0) {
352 ret = -EINVAL;
353 goto Cleanup;
354 }
355
356 /* page-size grandularity */
357 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
358
359 netDevice->SendBuffer =
360 osd_PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
361 if (!netDevice->SendBuffer) {
362 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
363 netDevice->SendBufferSize);
364 ret = -1;
365 goto Cleanup;
366 }
367 /* page-aligned buffer */
368 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
369
370 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
371
372 /*
373 * Establish the gpadl handle for this buffer on this
374 * channel. Note: This call uses the vmbus connection rather
375 * than the channel to establish the gpadl handle.
376 */
377 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
378 netDevice->SendBuffer,
379 netDevice->SendBufferSize,
380 &netDevice->SendBufferGpadlHandle);
381 if (ret != 0) {
382 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
383 goto Cleanup;
384 }
385
386 /* osd_WaitEventWait(ext->ChannelInitEvent); */
387
388 /* Notify the NetVsp of the gpadl handle */
389 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
390
391 initPacket = &netDevice->ChannelInitPacket;
392
393 memset(initPacket, 0, sizeof(struct nvsp_message));
394
395 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
396 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
397 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
398
399 /* Send the gpadl notification request */
400 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
401 initPacket, sizeof(struct nvsp_message),
402 (unsigned long)initPacket,
403 VmbusPacketTypeDataInBand,
404 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
405 if (ret != 0) {
406 DPRINT_ERR(NETVSC,
407 "unable to send receive buffer's gpadl to netvsp");
408 goto Cleanup;
409 }
410
411 osd_WaitEventWait(netDevice->ChannelInitEvent);
412
413 /* Check the response */
414 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess) {
415 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
416 "initialzation with NetVsp - status %d",
417 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
418 ret = -1;
419 goto Cleanup;
420 }
421
422 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
423
424 goto Exit;
425
426 Cleanup:
427 NetVscDestroySendBuffer(netDevice);
428
429 Exit:
430 PutNetDevice(Device);
431 DPRINT_EXIT(NETVSC);
432 return ret;
433 }
434
435 static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice)
436 {
437 struct nvsp_message *revokePacket;
438 int ret = 0;
439
440 /*
441 * If we got a section count, it means we received a
442 * SendReceiveBufferComplete msg (ie sent
443 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
444 * to send a revoke msg here
445 */
446 if (NetDevice->ReceiveSectionCount) {
447 DPRINT_INFO(NETVSC,
448 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
449
450 /* Send the revoke receive buffer */
451 revokePacket = &NetDevice->RevokePacket;
452 memset(revokePacket, 0, sizeof(struct nvsp_message));
453
454 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
455 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
456
457 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(
458 NetDevice->Device,
459 revokePacket,
460 sizeof(struct nvsp_message),
461 (unsigned long)revokePacket,
462 VmbusPacketTypeDataInBand, 0);
463 /*
464 * If we failed here, we might as well return and
465 * have a leak rather than continue and a bugchk
466 */
467 if (ret != 0) {
468 DPRINT_ERR(NETVSC, "unable to send revoke receive "
469 "buffer to netvsp");
470 DPRINT_EXIT(NETVSC);
471 return -1;
472 }
473 }
474
475 /* Teardown the gpadl on the vsp end */
476 if (NetDevice->ReceiveBufferGpadlHandle) {
477 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
478
479 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(
480 NetDevice->Device,
481 NetDevice->ReceiveBufferGpadlHandle);
482
483 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
484 if (ret != 0) {
485 DPRINT_ERR(NETVSC,
486 "unable to teardown receive buffer's gpadl");
487 DPRINT_EXIT(NETVSC);
488 return -1;
489 }
490 NetDevice->ReceiveBufferGpadlHandle = 0;
491 }
492
493 if (NetDevice->ReceiveBuffer) {
494 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
495
496 /* Free up the receive buffer */
497 osd_PageFree(NetDevice->ReceiveBuffer,
498 NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
499 NetDevice->ReceiveBuffer = NULL;
500 }
501
502 if (NetDevice->ReceiveSections) {
503 NetDevice->ReceiveSectionCount = 0;
504 kfree(NetDevice->ReceiveSections);
505 NetDevice->ReceiveSections = NULL;
506 }
507
508 DPRINT_EXIT(NETVSC);
509
510 return ret;
511 }
512
513 static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice)
514 {
515 struct nvsp_message *revokePacket;
516 int ret = 0;
517
518 /*
519 * If we got a section count, it means we received a
520 * SendReceiveBufferComplete msg (ie sent
521 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
522 * to send a revoke msg here
523 */
524 if (NetDevice->SendSectionSize) {
525 DPRINT_INFO(NETVSC,
526 "Sending NvspMessage1TypeRevokeSendBuffer...");
527
528 /* Send the revoke send buffer */
529 revokePacket = &NetDevice->RevokePacket;
530 memset(revokePacket, 0, sizeof(struct nvsp_message));
531
532 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
533 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
534
535 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
536 revokePacket,
537 sizeof(struct nvsp_message),
538 (unsigned long)revokePacket,
539 VmbusPacketTypeDataInBand, 0);
540 /*
541 * If we failed here, we might as well return and have a leak
542 * rather than continue and a bugchk
543 */
544 if (ret != 0) {
545 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
546 "to netvsp");
547 DPRINT_EXIT(NETVSC);
548 return -1;
549 }
550 }
551
552 /* Teardown the gpadl on the vsp end */
553 if (NetDevice->SendBufferGpadlHandle) {
554 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
555
556 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, NetDevice->SendBufferGpadlHandle);
557
558 /*
559 * If we failed here, we might as well return and have a leak
560 * rather than continue and a bugchk
561 */
562 if (ret != 0) {
563 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
564 "gpadl");
565 DPRINT_EXIT(NETVSC);
566 return -1;
567 }
568 NetDevice->SendBufferGpadlHandle = 0;
569 }
570
571 if (NetDevice->SendBuffer) {
572 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
573
574 /* Free up the receive buffer */
575 osd_PageFree(NetDevice->SendBuffer,
576 NetDevice->SendBufferSize >> PAGE_SHIFT);
577 NetDevice->SendBuffer = NULL;
578 }
579
580 DPRINT_EXIT(NETVSC);
581
582 return ret;
583 }
584
585
586 static int NetVscConnectToVsp(struct hv_device *Device)
587 {
588 int ret;
589 struct netvsc_device *netDevice;
590 struct nvsp_message *initPacket;
591 int ndisVersion;
592
593 netDevice = GetOutboundNetDevice(Device);
594 if (!netDevice) {
595 DPRINT_ERR(NETVSC, "unable to get net device..."
596 "device being destroyed?");
597 DPRINT_EXIT(NETVSC);
598 return -1;
599 }
600
601 initPacket = &netDevice->ChannelInitPacket;
602
603 memset(initPacket, 0, sizeof(struct nvsp_message));
604 initPacket->Header.MessageType = NvspMessageTypeInit;
605 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
606 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
607
608 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
609
610 /* Send the init request */
611 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
612 initPacket,
613 sizeof(struct nvsp_message),
614 (unsigned long)initPacket,
615 VmbusPacketTypeDataInBand,
616 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
617
618 if (ret != 0) {
619 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
620 goto Cleanup;
621 }
622
623 osd_WaitEventWait(netDevice->ChannelInitEvent);
624
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 initPacket->Messages.InitMessages.InitComplete.Status,
629 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
630
631 if (initPacket->Messages.InitMessages.InitComplete.Status !=
632 NvspStatusSuccess) {
633 DPRINT_ERR(NETVSC,
634 "unable to initialize with netvsp (status 0x%x)",
635 initPacket->Messages.InitMessages.InitComplete.Status);
636 ret = -1;
637 goto Cleanup;
638 }
639
640 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1) {
641 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
642 "(version expected 1 got %d)",
643 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
644 ret = -1;
645 goto Cleanup;
646 }
647 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
648
649 /* Send the ndis version */
650 memset(initPacket, 0, sizeof(struct nvsp_message));
651
652 ndisVersion = 0x00050000;
653
654 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
655 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion =
656 (ndisVersion & 0xFFFF0000) >> 16;
657 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion =
658 ndisVersion & 0xFFFF;
659
660 /* Send the init request */
661 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
662 initPacket,
663 sizeof(struct nvsp_message),
664 (unsigned long)initPacket,
665 VmbusPacketTypeDataInBand, 0);
666 if (ret != 0) {
667 DPRINT_ERR(NETVSC,
668 "unable to send NvspMessage1TypeSendNdisVersion");
669 ret = -1;
670 goto Cleanup;
671 }
672 /*
673 * BUGBUG - We have to wait for the above msg since the
674 * netvsp uses KMCL which acknowledges packet (completion
675 * packet) since our Vmbus always set the
676 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
677 */
678 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
679
680 /* Post the big receive buffer to NetVSP */
681 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
682 if (ret == 0)
683 ret = NetVscInitializeSendBufferWithNetVsp(Device);
684
685 Cleanup:
686 PutNetDevice(Device);
687 DPRINT_EXIT(NETVSC);
688 return ret;
689 }
690
691 static void NetVscDisconnectFromVsp(struct netvsc_device *NetDevice)
692 {
693 NetVscDestroyReceiveBuffer(NetDevice);
694 NetVscDestroySendBuffer(NetDevice);
695
696 DPRINT_EXIT(NETVSC);
697 }
698
699 /*
700 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
701 */
702 static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
703 {
704 int ret = 0;
705 int i;
706 struct netvsc_device *netDevice;
707 struct hv_netvsc_packet *packet, *pos;
708 struct netvsc_driver *netDriver =
709 (struct netvsc_driver *)Device->Driver;
710
711 netDevice = AllocNetDevice(Device);
712 if (!netDevice) {
713 ret = -1;
714 goto Cleanup;
715 }
716
717 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
718
719 /* Initialize the NetVSC channel extension */
720 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
721 spin_lock_init(&netDevice->receive_packet_list_lock);
722
723 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
724
725 INIT_LIST_HEAD(&netDevice->ReceivePacketList);
726
727 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
728 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
729 (NETVSC_RECEIVE_SG_COUNT *
730 sizeof(struct hv_page_buffer)), GFP_KERNEL);
731 if (!packet) {
732 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
733 "for receive pool (wanted %d got %d)",
734 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
735 break;
736 }
737 list_add_tail(&packet->ListEntry,
738 &netDevice->ReceivePacketList);
739 }
740 netDevice->ChannelInitEvent = osd_WaitEventCreate();
741 if (!netDevice->ChannelInitEvent) {
742 ret = -ENOMEM;
743 goto Cleanup;
744 }
745
746 /* Open the channel */
747 ret = Device->Driver->VmbusChannelInterface.Open(Device,
748 netDriver->RingBufferSize,
749 netDriver->RingBufferSize,
750 NULL, 0,
751 NetVscOnChannelCallback,
752 Device);
753
754 if (ret != 0) {
755 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
756 ret = -1;
757 goto Cleanup;
758 }
759
760 /* Channel is opened */
761 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
762
763 /* Connect with the NetVsp */
764 ret = NetVscConnectToVsp(Device);
765 if (ret != 0) {
766 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
767 ret = -1;
768 goto Close;
769 }
770
771 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
772 ret);
773
774 DPRINT_EXIT(NETVSC);
775 return ret;
776
777 Close:
778 /* Now, we can close the channel safely */
779 Device->Driver->VmbusChannelInterface.Close(Device);
780
781 Cleanup:
782
783 if (netDevice) {
784 kfree(netDevice->ChannelInitEvent);
785
786 list_for_each_entry_safe(packet, pos,
787 &netDevice->ReceivePacketList,
788 ListEntry) {
789 list_del(&packet->ListEntry);
790 kfree(packet);
791 }
792
793 ReleaseOutboundNetDevice(Device);
794 ReleaseInboundNetDevice(Device);
795
796 FreeNetDevice(netDevice);
797 }
798
799 DPRINT_EXIT(NETVSC);
800 return ret;
801 }
802
803 /*
804 * NetVscOnDeviceRemove - Callback when the root bus device is removed
805 */
806 static int NetVscOnDeviceRemove(struct hv_device *Device)
807 {
808 struct netvsc_device *netDevice;
809 struct hv_netvsc_packet *netvscPacket, *pos;
810
811 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
812 Device->Extension);
813
814 /* Stop outbound traffic ie sends and receives completions */
815 netDevice = ReleaseOutboundNetDevice(Device);
816 if (!netDevice) {
817 DPRINT_ERR(NETVSC, "No net device present!!");
818 return -1;
819 }
820
821 /* Wait for all send completions */
822 while (atomic_read(&netDevice->NumOutstandingSends)) {
823 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
824 atomic_read(&netDevice->NumOutstandingSends));
825 udelay(100);
826 }
827
828 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
829
830 NetVscDisconnectFromVsp(netDevice);
831
832 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
833 Device->Extension);
834
835 /* Stop inbound traffic ie receives and sends completions */
836 netDevice = ReleaseInboundNetDevice(Device);
837
838 /* At this point, no one should be accessing netDevice except in here */
839 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
840
841 /* Now, we can close the channel safely */
842 Device->Driver->VmbusChannelInterface.Close(Device);
843
844 /* Release all resources */
845 list_for_each_entry_safe(netvscPacket, pos,
846 &netDevice->ReceivePacketList, ListEntry) {
847 list_del(&netvscPacket->ListEntry);
848 kfree(netvscPacket);
849 }
850
851 kfree(netDevice->ChannelInitEvent);
852 FreeNetDevice(netDevice);
853
854 DPRINT_EXIT(NETVSC);
855 return 0;
856 }
857
858 /*
859 * NetVscOnCleanup - Perform any cleanup when the driver is removed
860 */
861 static void NetVscOnCleanup(struct hv_driver *drv)
862 {
863 DPRINT_EXIT(NETVSC);
864 }
865
866 static void NetVscOnSendCompletion(struct hv_device *Device,
867 struct vmpacket_descriptor *Packet)
868 {
869 struct netvsc_device *netDevice;
870 struct nvsp_message *nvspPacket;
871 struct hv_netvsc_packet *nvscPacket;
872
873 netDevice = GetInboundNetDevice(Device);
874 if (!netDevice) {
875 DPRINT_ERR(NETVSC, "unable to get net device..."
876 "device being destroyed?");
877 DPRINT_EXIT(NETVSC);
878 return;
879 }
880
881 nvspPacket = (struct nvsp_message *)((unsigned long)Packet + (Packet->DataOffset8 << 3));
882
883 DPRINT_DBG(NETVSC, "send completion packet - type %d",
884 nvspPacket->Header.MessageType);
885
886 if ((nvspPacket->Header.MessageType == NvspMessageTypeInitComplete) ||
887 (nvspPacket->Header.MessageType ==
888 NvspMessage1TypeSendReceiveBufferComplete) ||
889 (nvspPacket->Header.MessageType ==
890 NvspMessage1TypeSendSendBufferComplete)) {
891 /* Copy the response back */
892 memcpy(&netDevice->ChannelInitPacket, nvspPacket,
893 sizeof(struct nvsp_message));
894 osd_WaitEventSet(netDevice->ChannelInitEvent);
895 } else if (nvspPacket->Header.MessageType ==
896 NvspMessage1TypeSendRNDISPacketComplete) {
897 /* Get the send context */
898 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
899 /* ASSERT(nvscPacket); */
900
901 /* Notify the layer above us */
902 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
903
904 atomic_dec(&netDevice->NumOutstandingSends);
905 } else {
906 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
907 "%d received!!", nvspPacket->Header.MessageType);
908 }
909
910 PutNetDevice(Device);
911 DPRINT_EXIT(NETVSC);
912 }
913
914 static int NetVscOnSend(struct hv_device *Device,
915 struct hv_netvsc_packet *Packet)
916 {
917 struct netvsc_device *netDevice;
918 int ret = 0;
919
920 struct nvsp_message sendMessage;
921
922 netDevice = GetOutboundNetDevice(Device);
923 if (!netDevice) {
924 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
925 "ignoring outbound packets", netDevice);
926 DPRINT_EXIT(NETVSC);
927 return -2;
928 }
929
930 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
931 if (Packet->IsDataPacket) {
932 /* 0 is RMC_DATA; */
933 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;
934 } else {
935 /* 1 is RMC_CONTROL; */
936 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;
937 }
938
939 /* Not using send buffer section */
940 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
941 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
942
943 if (Packet->PageBufferCount) {
944 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(
945 Device, Packet->PageBuffers,
946 Packet->PageBufferCount,
947 &sendMessage,
948 sizeof(struct nvsp_message),
949 (unsigned long)Packet);
950 } else {
951 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
952 &sendMessage,
953 sizeof(struct nvsp_message),
954 (unsigned long)Packet,
955 VmbusPacketTypeDataInBand,
956 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
957
958 }
959
960 if (ret != 0)
961 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
962 Packet, ret);
963
964 atomic_inc(&netDevice->NumOutstandingSends);
965 PutNetDevice(Device);
966
967 DPRINT_EXIT(NETVSC);
968 return ret;
969 }
970
971 static void NetVscOnReceive(struct hv_device *Device,
972 struct vmpacket_descriptor *Packet)
973 {
974 struct netvsc_device *netDevice;
975 struct vmtransfer_page_packet_header *vmxferpagePacket;
976 struct nvsp_message *nvspPacket;
977 struct hv_netvsc_packet *netvscPacket = NULL;
978 unsigned long start;
979 unsigned long end, endVirtual;
980 /* struct netvsc_driver *netvscDriver; */
981 struct xferpage_packet *xferpagePacket = NULL;
982 int i, j;
983 int count = 0, bytesRemain = 0;
984 unsigned long flags;
985 LIST_HEAD(listHead);
986
987 netDevice = GetInboundNetDevice(Device);
988 if (!netDevice) {
989 DPRINT_ERR(NETVSC, "unable to get net device..."
990 "device being destroyed?");
991 DPRINT_EXIT(NETVSC);
992 return;
993 }
994
995 /*
996 * All inbound packets other than send completion should be xfer page
997 * packet
998 */
999 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) {
1000 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
1001 Packet->Type);
1002 PutNetDevice(Device);
1003 return;
1004 }
1005
1006 nvspPacket = (struct nvsp_message *)((unsigned long)Packet +
1007 (Packet->DataOffset8 << 3));
1008
1009 /* Make sure this is a valid nvsp packet */
1010 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket) {
1011 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1012 nvspPacket->Header.MessageType);
1013 PutNetDevice(Device);
1014 return;
1015 }
1016
1017 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1018 nvspPacket->Header.MessageType);
1019
1020 vmxferpagePacket = (struct vmtransfer_page_packet_header *)Packet;
1021
1022 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1023 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1024 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1025 vmxferpagePacket->TransferPageSetId);
1026 PutNetDevice(Device);
1027 return;
1028 }
1029
1030 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1031 vmxferpagePacket->RangeCount);
1032
1033 /*
1034 * Grab free packets (range count + 1) to represent this xfer
1035 * page packet. +1 to represent the xfer page packet itself.
1036 * We grab it here so that we know exactly how many we can
1037 * fulfil
1038 */
1039 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1040 while (!list_empty(&netDevice->ReceivePacketList)) {
1041 list_move_tail(netDevice->ReceivePacketList.next, &listHead);
1042 if (++count == vmxferpagePacket->RangeCount + 1)
1043 break;
1044 }
1045 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1046
1047 /*
1048 * We need at least 2 netvsc pkts (1 to represent the xfer
1049 * page and at least 1 for the range) i.e. we can handled
1050 * some of the xfer page packet ranges...
1051 */
1052 if (count < 2) {
1053 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1054 "Dropping this xfer page packet completely!",
1055 count, vmxferpagePacket->RangeCount + 1);
1056
1057 /* Return it to the freelist */
1058 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1059 for (i = count; i != 0; i--) {
1060 list_move_tail(listHead.next,
1061 &netDevice->ReceivePacketList);
1062 }
1063 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock,
1064 flags);
1065
1066 NetVscSendReceiveCompletion(Device,
1067 vmxferpagePacket->d.TransactionId);
1068
1069 PutNetDevice(Device);
1070 return;
1071 }
1072
1073 /* Remove the 1st packet to represent the xfer page packet itself */
1074 xferpagePacket = (struct xferpage_packet *)listHead.next;
1075 list_del(&xferpagePacket->ListEntry);
1076
1077 /* This is how much we can satisfy */
1078 xferpagePacket->Count = count - 1;
1079 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1080 /* vmxferpagePacket->RangeCount); */
1081
1082 if (xferpagePacket->Count != vmxferpagePacket->RangeCount) {
1083 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1084 "page...got %d", vmxferpagePacket->RangeCount,
1085 xferpagePacket->Count);
1086 }
1087
1088 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1089 for (i = 0; i < (count - 1); i++) {
1090 netvscPacket = (struct hv_netvsc_packet *)listHead.next;
1091 list_del(&netvscPacket->ListEntry);
1092
1093 /* Initialize the netvsc packet */
1094 netvscPacket->XferPagePacket = xferpagePacket;
1095 netvscPacket->Completion.Recv.OnReceiveCompletion =
1096 NetVscOnReceiveCompletion;
1097 netvscPacket->Completion.Recv.ReceiveCompletionContext =
1098 netvscPacket;
1099 netvscPacket->Device = Device;
1100 /* Save this so that we can send it back */
1101 netvscPacket->Completion.Recv.ReceiveCompletionTid =
1102 vmxferpagePacket->d.TransactionId;
1103
1104 netvscPacket->TotalDataBufferLength =
1105 vmxferpagePacket->Ranges[i].ByteCount;
1106 netvscPacket->PageBufferCount = 1;
1107
1108 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1109 /* vmxferpagePacket->Ranges[i].ByteCount < */
1110 /* netDevice->ReceiveBufferSize); */
1111
1112 netvscPacket->PageBuffers[0].Length =
1113 vmxferpagePacket->Ranges[i].ByteCount;
1114
1115 start = virt_to_phys((void *)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1116
1117 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1118 endVirtual = (unsigned long)netDevice->ReceiveBuffer
1119 + vmxferpagePacket->Ranges[i].ByteOffset
1120 + vmxferpagePacket->Ranges[i].ByteCount - 1;
1121 end = virt_to_phys((void *)endVirtual);
1122
1123 /* Calculate the page relative offset */
1124 netvscPacket->PageBuffers[0].Offset =
1125 vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE - 1);
1126 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1127 /* Handle frame across multiple pages: */
1128 netvscPacket->PageBuffers[0].Length =
1129 (netvscPacket->PageBuffers[0].Pfn << PAGE_SHIFT)
1130 + PAGE_SIZE - start;
1131 bytesRemain = netvscPacket->TotalDataBufferLength -
1132 netvscPacket->PageBuffers[0].Length;
1133 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1134 netvscPacket->PageBuffers[j].Offset = 0;
1135 if (bytesRemain <= PAGE_SIZE) {
1136 netvscPacket->PageBuffers[j].Length = bytesRemain;
1137 bytesRemain = 0;
1138 } else {
1139 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1140 bytesRemain -= PAGE_SIZE;
1141 }
1142 netvscPacket->PageBuffers[j].Pfn =
1143 virt_to_phys((void *)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1144 netvscPacket->PageBufferCount++;
1145 if (bytesRemain == 0)
1146 break;
1147 }
1148 /* ASSERT(bytesRemain == 0); */
1149 }
1150 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1151 "(pfn %llx, offset %u, len %u)", i,
1152 vmxferpagePacket->Ranges[i].ByteOffset,
1153 vmxferpagePacket->Ranges[i].ByteCount,
1154 netvscPacket->PageBuffers[0].Pfn,
1155 netvscPacket->PageBuffers[0].Offset,
1156 netvscPacket->PageBuffers[0].Length);
1157
1158 /* Pass it to the upper layer */
1159 ((struct netvsc_driver *)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1160
1161 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1162 }
1163
1164 /* ASSERT(list_empty(&listHead)); */
1165
1166 PutNetDevice(Device);
1167 DPRINT_EXIT(NETVSC);
1168 }
1169
1170 static void NetVscSendReceiveCompletion(struct hv_device *Device,
1171 u64 TransactionId)
1172 {
1173 struct nvsp_message recvcompMessage;
1174 int retries = 0;
1175 int ret;
1176
1177 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1178 TransactionId);
1179
1180 recvcompMessage.Header.MessageType =
1181 NvspMessage1TypeSendRNDISPacketComplete;
1182
1183 /* FIXME: Pass in the status */
1184 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1185
1186 retry_send_cmplt:
1187 /* Send the completion */
1188 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1189 &recvcompMessage,
1190 sizeof(struct nvsp_message),
1191 TransactionId,
1192 VmbusPacketTypeCompletion, 0);
1193 if (ret == 0) {
1194 /* success */
1195 /* no-op */
1196 } else if (ret == -1) {
1197 /* no more room...wait a bit and attempt to retry 3 times */
1198 retries++;
1199 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1200 "(tid %llx)...retrying %d", TransactionId, retries);
1201
1202 if (retries < 4) {
1203 udelay(100);
1204 goto retry_send_cmplt;
1205 } else {
1206 DPRINT_ERR(NETVSC, "unable to send receive completion "
1207 "pkt (tid %llx)...give up retrying",
1208 TransactionId);
1209 }
1210 } else {
1211 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1212 "%llx", TransactionId);
1213 }
1214 }
1215
1216 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1217 static void NetVscOnReceiveCompletion(void *Context)
1218 {
1219 struct hv_netvsc_packet *packet = Context;
1220 struct hv_device *device = (struct hv_device *)packet->Device;
1221 struct netvsc_device *netDevice;
1222 u64 transactionId = 0;
1223 bool fSendReceiveComp = false;
1224 unsigned long flags;
1225
1226 /* ASSERT(packet->XferPagePacket); */
1227
1228 /*
1229 * Even though it seems logical to do a GetOutboundNetDevice() here to
1230 * send out receive completion, we are using GetInboundNetDevice()
1231 * since we may have disable outbound traffic already.
1232 */
1233 netDevice = GetInboundNetDevice(device);
1234 if (!netDevice) {
1235 DPRINT_ERR(NETVSC, "unable to get net device..."
1236 "device being destroyed?");
1237 DPRINT_EXIT(NETVSC);
1238 return;
1239 }
1240
1241 /* Overloading use of the lock. */
1242 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1243
1244 /* ASSERT(packet->XferPagePacket->Count > 0); */
1245 packet->XferPagePacket->Count--;
1246
1247 /*
1248 * Last one in the line that represent 1 xfer page packet.
1249 * Return the xfer page packet itself to the freelist
1250 */
1251 if (packet->XferPagePacket->Count == 0) {
1252 fSendReceiveComp = true;
1253 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1254 list_add_tail(&packet->XferPagePacket->ListEntry,
1255 &netDevice->ReceivePacketList);
1256
1257 }
1258
1259 /* Put the packet back */
1260 list_add_tail(&packet->ListEntry, &netDevice->ReceivePacketList);
1261 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1262
1263 /* Send a receive completion for the xfer page packet */
1264 if (fSendReceiveComp)
1265 NetVscSendReceiveCompletion(device, transactionId);
1266
1267 PutNetDevice(device);
1268 DPRINT_EXIT(NETVSC);
1269 }
1270
1271 static void NetVscOnChannelCallback(void *Context)
1272 {
1273 int ret;
1274 struct hv_device *device = Context;
1275 struct netvsc_device *netDevice;
1276 u32 bytesRecvd;
1277 u64 requestId;
1278 unsigned char *packet;
1279 struct vmpacket_descriptor *desc;
1280 unsigned char *buffer;
1281 int bufferlen = NETVSC_PACKET_SIZE;
1282
1283 /* ASSERT(device); */
1284
1285 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1286 GFP_KERNEL);
1287 if (!packet)
1288 return;
1289 buffer = packet;
1290
1291 netDevice = GetInboundNetDevice(device);
1292 if (!netDevice) {
1293 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1294 "ignoring inbound packets", netDevice);
1295 DPRINT_EXIT(NETVSC);
1296 goto out;
1297 }
1298
1299 do {
1300 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(
1301 device, buffer, bufferlen,
1302 &bytesRecvd, &requestId);
1303 if (ret == 0) {
1304 if (bytesRecvd > 0) {
1305 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1306 bytesRecvd, requestId);
1307
1308 desc = (struct vmpacket_descriptor *)buffer;
1309 switch (desc->Type) {
1310 case VmbusPacketTypeCompletion:
1311 NetVscOnSendCompletion(device, desc);
1312 break;
1313
1314 case VmbusPacketTypeDataUsingTransferPages:
1315 NetVscOnReceive(device, desc);
1316 break;
1317
1318 default:
1319 DPRINT_ERR(NETVSC,
1320 "unhandled packet type %d, "
1321 "tid %llx len %d\n",
1322 desc->Type, requestId,
1323 bytesRecvd);
1324 break;
1325 }
1326
1327 /* reset */
1328 if (bufferlen > NETVSC_PACKET_SIZE) {
1329 kfree(buffer);
1330 buffer = packet;
1331 bufferlen = NETVSC_PACKET_SIZE;
1332 }
1333 } else {
1334 /* reset */
1335 if (bufferlen > NETVSC_PACKET_SIZE) {
1336 kfree(buffer);
1337 buffer = packet;
1338 bufferlen = NETVSC_PACKET_SIZE;
1339 }
1340
1341 break;
1342 }
1343 } else if (ret == -2) {
1344 /* Handle large packet */
1345 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
1346 if (buffer == NULL) {
1347 /* Try again next time around */
1348 DPRINT_ERR(NETVSC,
1349 "unable to allocate buffer of size "
1350 "(%d)!!", bytesRecvd);
1351 break;
1352 }
1353
1354 bufferlen = bytesRecvd;
1355 }
1356 } while (1);
1357
1358 PutNetDevice(device);
1359 DPRINT_EXIT(NETVSC);
1360 out:
1361 kfree(buffer);
1362 return;
1363 }
This page took 0.064462 seconds and 4 git commands to generate.