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