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