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