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