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