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