166
135
while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
169
device->Extension = NULL;
170
139
return net_device;
174
* netvsc_initialize - Main entry point
176
int netvsc_initialize(struct hv_driver *drv)
142
static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
178
struct netvsc_driver *driver = (struct netvsc_driver *)drv;
180
DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
181
"sizeof(struct nvsp_message)=%zd, "
182
"sizeof(struct vmtransfer_page_packet_header)=%zd",
183
sizeof(struct hv_netvsc_packet),
184
sizeof(struct nvsp_message),
185
sizeof(struct vmtransfer_page_packet_header));
187
/* Make sure we are at least 2 pages since 1 page is used for control */
188
/* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
190
drv->name = driver_name;
191
memcpy(&drv->deviceType, &netvsc_device_type, sizeof(struct hv_guid));
193
/* Make sure it is set by the caller */
194
/* FIXME: These probably should still be tested in some way */
195
/* ASSERT(driver->OnReceiveCallback); */
196
/* ASSERT(driver->OnLinkStatusChanged); */
198
/* Setup the dispatch table */
199
driver->base.OnDeviceAdd = netvsc_device_add;
200
driver->base.OnDeviceRemove = netvsc_device_remove;
201
driver->base.OnCleanup = netvsc_cleanup;
203
driver->send = netvsc_send;
205
rndis_filter_init(driver);
144
struct nvsp_message *revoke_packet;
148
* If we got a section count, it means we received a
149
* SendReceiveBufferComplete msg (ie sent
150
* NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
151
* to send a revoke msg here
153
if (net_device->recv_section_cnt) {
154
/* Send the revoke receive buffer */
155
revoke_packet = &net_device->revoke_packet;
156
memset(revoke_packet, 0, sizeof(struct nvsp_message));
158
revoke_packet->hdr.msg_type =
159
NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
160
revoke_packet->msg.v1_msg.
161
revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
163
ret = vmbus_sendpacket(net_device->dev->channel,
165
sizeof(struct nvsp_message),
166
(unsigned long)revoke_packet,
167
VM_PKT_DATA_INBAND, 0);
169
* If we failed here, we might as well return and
170
* have a leak rather than continue and a bugchk
173
dev_err(&net_device->dev->device, "unable to send "
174
"revoke receive buffer to netvsp");
179
/* Teardown the gpadl on the vsp end */
180
if (net_device->recv_buf_gpadl_handle) {
181
ret = vmbus_teardown_gpadl(net_device->dev->channel,
182
net_device->recv_buf_gpadl_handle);
184
/* If we failed here, we might as well return and have a leak
185
* rather than continue and a bugchk
188
dev_err(&net_device->dev->device,
189
"unable to teardown receive buffer's gpadl");
192
net_device->recv_buf_gpadl_handle = 0;
195
if (net_device->recv_buf) {
196
/* Free up the receive buffer */
197
free_pages((unsigned long)net_device->recv_buf,
198
get_order(net_device->recv_buf_size));
199
net_device->recv_buf = NULL;
202
if (net_device->recv_section) {
203
net_device->recv_section_cnt = 0;
204
kfree(net_device->recv_section);
205
net_device->recv_section = NULL;
209
211
static int netvsc_init_recv_buf(struct hv_device *device)
212
215
struct netvsc_device *net_device;
213
216
struct nvsp_message *init_packet;
215
218
net_device = get_outbound_net_device(device);
216
219
if (!net_device) {
217
DPRINT_ERR(NETVSC, "unable to get net device..."
220
dev_err(&device->device, "unable to get net device..."
218
221
"device being destroyed?");
221
/* ASSERT(netDevice->ReceiveBufferSize > 0); */
222
/* page-size grandularity */
223
/* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
225
225
net_device->recv_buf =
226
osd_page_alloc(net_device->recv_buf_size >> PAGE_SHIFT);
226
(void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
227
get_order(net_device->recv_buf_size));
227
228
if (!net_device->recv_buf) {
229
"unable to allocate receive buffer of size %d",
230
net_device->recv_buf_size);
229
dev_err(&device->device, "unable to allocate receive "
230
"buffer of size %d", net_device->recv_buf_size);
234
/* page-aligned buffer */
235
/* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
238
DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
241
236
* Establish the gpadl handle for this buffer on this
270
262
ret = vmbus_sendpacket(device->channel, init_packet,
271
263
sizeof(struct nvsp_message),
272
264
(unsigned long)init_packet,
273
VmbusPacketTypeDataInBand,
274
266
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
277
"unable to send receive buffer's gpadl to netvsp");
268
dev_err(&device->device,
269
"unable to send receive buffer's gpadl to netvsp");
281
osd_waitevent_wait(net_device->channel_init_event);
273
t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
283
277
/* Check the response */
284
278
if (init_packet->msg.v1_msg.
285
279
send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
286
DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
280
dev_err(&device->device, "Unable to complete receive buffer "
287
281
"initialzation with NetVsp - status %d",
288
282
init_packet->msg.v1_msg.
289
283
send_recv_buf_complete.status);
294
288
/* Parse the response */
295
/* ASSERT(netDevice->ReceiveSectionCount == 0); */
296
/* ASSERT(netDevice->ReceiveSections == NULL); */
298
290
net_device->recv_section_cnt = init_packet->msg.
299
291
v1_msg.send_recv_buf_complete.num_sections;
326
310
if (net_device->recv_section_cnt != 1 ||
327
311
net_device->recv_section->offset != 0) {
335
319
netvsc_destroy_recv_buf(net_device);
338
322
put_net_device(device);
326
static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
328
struct nvsp_message *revoke_packet;
332
* If we got a section count, it means we received a
333
* SendReceiveBufferComplete msg (ie sent
334
* NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
335
* to send a revoke msg here
337
if (net_device->send_section_size) {
338
/* Send the revoke send buffer */
339
revoke_packet = &net_device->revoke_packet;
340
memset(revoke_packet, 0, sizeof(struct nvsp_message));
342
revoke_packet->hdr.msg_type =
343
NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
344
revoke_packet->msg.v1_msg.
345
revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
347
ret = vmbus_sendpacket(net_device->dev->channel,
349
sizeof(struct nvsp_message),
350
(unsigned long)revoke_packet,
351
VM_PKT_DATA_INBAND, 0);
353
* If we failed here, we might as well return and have a leak
354
* rather than continue and a bugchk
357
dev_err(&net_device->dev->device, "unable to send "
358
"revoke send buffer to netvsp");
363
/* Teardown the gpadl on the vsp end */
364
if (net_device->send_buf_gpadl_handle) {
365
ret = vmbus_teardown_gpadl(net_device->dev->channel,
366
net_device->send_buf_gpadl_handle);
369
* If we failed here, we might as well return and have a leak
370
* rather than continue and a bugchk
373
dev_err(&net_device->dev->device,
374
"unable to teardown send buffer's gpadl");
377
net_device->send_buf_gpadl_handle = 0;
380
if (net_device->send_buf) {
381
/* Free up the receive buffer */
382
free_pages((unsigned long)net_device->send_buf,
383
get_order(net_device->send_buf_size));
384
net_device->send_buf = NULL;
342
390
static int netvsc_init_send_buf(struct hv_device *device)
345
394
struct netvsc_device *net_device;
346
395
struct nvsp_message *init_packet;
348
397
net_device = get_outbound_net_device(device);
349
398
if (!net_device) {
350
DPRINT_ERR(NETVSC, "unable to get net device..."
399
dev_err(&device->device, "unable to get net device..."
351
400
"device being destroyed?");
354
403
if (net_device->send_buf_size <= 0) {
359
/* page-size grandularity */
360
/* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
362
408
net_device->send_buf =
363
osd_page_alloc(net_device->send_buf_size >> PAGE_SHIFT);
409
(void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
410
get_order(net_device->send_buf_size));
364
411
if (!net_device->send_buf) {
365
DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
366
net_device->send_buf_size);
412
dev_err(&device->device, "unable to allocate send "
413
"buffer of size %d", net_device->send_buf_size);
370
/* page-aligned buffer */
371
/* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
373
DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
376
419
* Establish the gpadl handle for this buffer on this
404
443
ret = vmbus_sendpacket(device->channel, init_packet,
405
444
sizeof(struct nvsp_message),
406
445
(unsigned long)init_packet,
407
VmbusPacketTypeDataInBand,
408
447
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
449
dev_err(&device->device,
411
450
"unable to send receive buffer's gpadl to netvsp");
415
osd_waitevent_wait(net_device->channel_init_event);
454
t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
417
458
/* Check the response */
418
459
if (init_packet->msg.v1_msg.
419
460
send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
420
DPRINT_ERR(NETVSC, "Unable to complete send buffer "
461
dev_err(&device->device, "Unable to complete send buffer "
421
462
"initialzation with NetVsp - status %d",
422
463
init_packet->msg.v1_msg.
423
464
send_send_buf_complete.status);
428
469
net_device->send_section_size = init_packet->
429
470
msg.v1_msg.send_send_buf_complete.section_size;
434
475
netvsc_destroy_send_buf(net_device);
437
478
put_net_device(device);
441
static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
443
struct nvsp_message *revoke_packet;
447
* If we got a section count, it means we received a
448
* SendReceiveBufferComplete msg (ie sent
449
* NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
450
* to send a revoke msg here
452
if (net_device->recv_section_cnt) {
454
"Sending NvspMessage1TypeRevokeReceiveBuffer...");
456
/* Send the revoke receive buffer */
457
revoke_packet = &net_device->revoke_packet;
458
memset(revoke_packet, 0, sizeof(struct nvsp_message));
460
revoke_packet->hdr.msg_type =
461
NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
462
revoke_packet->msg.v1_msg.
463
revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
465
ret = vmbus_sendpacket(net_device->dev->channel,
467
sizeof(struct nvsp_message),
468
(unsigned long)revoke_packet,
469
VmbusPacketTypeDataInBand, 0);
471
* If we failed here, we might as well return and
472
* have a leak rather than continue and a bugchk
475
DPRINT_ERR(NETVSC, "unable to send revoke receive "
481
/* Teardown the gpadl on the vsp end */
482
if (net_device->recv_buf_gpadl_handle) {
483
DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
485
ret = vmbus_teardown_gpadl(net_device->dev->channel,
486
net_device->recv_buf_gpadl_handle);
488
/* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
491
"unable to teardown receive buffer's gpadl");
494
net_device->recv_buf_gpadl_handle = 0;
497
if (net_device->recv_buf) {
498
DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
500
/* Free up the receive buffer */
501
osd_page_free(net_device->recv_buf,
502
net_device->recv_buf_size >> PAGE_SHIFT);
503
net_device->recv_buf = NULL;
506
if (net_device->recv_section) {
507
net_device->recv_section_cnt = 0;
508
kfree(net_device->recv_section);
509
net_device->recv_section = NULL;
515
static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
517
struct nvsp_message *revoke_packet;
521
* If we got a section count, it means we received a
522
* SendReceiveBufferComplete msg (ie sent
523
* NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
524
* to send a revoke msg here
526
if (net_device->send_section_size) {
528
"Sending NvspMessage1TypeRevokeSendBuffer...");
530
/* Send the revoke send buffer */
531
revoke_packet = &net_device->revoke_packet;
532
memset(revoke_packet, 0, sizeof(struct nvsp_message));
534
revoke_packet->hdr.msg_type =
535
NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
536
revoke_packet->msg.v1_msg.
537
revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
539
ret = vmbus_sendpacket(net_device->dev->channel,
541
sizeof(struct nvsp_message),
542
(unsigned long)revoke_packet,
543
VmbusPacketTypeDataInBand, 0);
545
* If we failed here, we might as well return and have a leak
546
* rather than continue and a bugchk
549
DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
555
/* Teardown the gpadl on the vsp end */
556
if (net_device->send_buf_gpadl_handle) {
557
DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
558
ret = vmbus_teardown_gpadl(net_device->dev->channel,
559
net_device->send_buf_gpadl_handle);
562
* If we failed here, we might as well return and have a leak
563
* rather than continue and a bugchk
566
DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
570
net_device->send_buf_gpadl_handle = 0;
573
if (net_device->send_buf) {
574
DPRINT_INFO(NETVSC, "Freeing up send buffer...");
576
/* Free up the receive buffer */
577
osd_page_free(net_device->send_buf,
578
net_device->send_buf_size >> PAGE_SHIFT);
579
net_device->send_buf = NULL;
586
483
static int netvsc_connect_vsp(struct hv_device *device)
589
486
struct netvsc_device *net_device;
590
487
struct nvsp_message *init_packet;
591
488
int ndis_version;
593
490
net_device = get_outbound_net_device(device);
594
491
if (!net_device) {
595
DPRINT_ERR(NETVSC, "unable to get net device..."
492
dev_err(&device->device, "unable to get net device..."
596
493
"device being destroyed?");
606
503
init_packet->msg.init_msg.init.max_protocol_ver =
607
504
NVSP_MAX_PROTOCOL_VERSION;
609
DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
611
506
/* Send the init request */
612
507
ret = vmbus_sendpacket(device->channel, init_packet,
613
508
sizeof(struct nvsp_message),
614
509
(unsigned long)init_packet,
615
VmbusPacketTypeDataInBand,
616
511
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
619
DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
516
t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
623
osd_waitevent_wait(net_device->channel_init_event);
625
/* Now, check the response */
626
/* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
627
DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
628
init_packet->msg.init_msg.init_complete.status,
629
init_packet->msg.init_msg.
630
init_complete.max_mdl_chain_len);
632
523
if (init_packet->msg.init_msg.init_complete.status !=
633
524
NVSP_STAT_SUCCESS) {
635
"unable to initialize with netvsp (status 0x%x)",
636
init_packet->msg.init_msg.init_complete.status);
641
529
if (init_packet->msg.init_msg.init_complete.
642
530
negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
643
DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
644
"(version expected 1 got %d)",
645
init_packet->msg.init_msg.
646
init_complete.negotiated_protocol_ver);
650
DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
652
534
/* Send the ndis version */
653
535
memset(init_packet, 0, sizeof(struct nvsp_message));
665
547
/* Send the init request */
666
548
ret = vmbus_sendpacket(device->channel, init_packet,
667
sizeof(struct nvsp_message),
668
(unsigned long)init_packet,
669
VmbusPacketTypeDataInBand, 0);
549
sizeof(struct nvsp_message),
550
(unsigned long)init_packet,
551
VM_PKT_DATA_INBAND, 0);
672
"unable to send NvspMessage1TypeSendNdisVersion");
677
* BUGBUG - We have to wait for the above msg since the
678
* netvsp uses KMCL which acknowledges packet (completion
679
* packet) since our Vmbus always set the
680
* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
682
/* osd_waitevent_wait(NetVscChannel->ChannelInitEvent); */
684
557
/* Post the big receive buffer to NetVSP */
685
558
ret = netvsc_init_recv_buf(device);
687
560
ret = netvsc_init_send_buf(device);
690
563
put_net_device(device);
694
static void NetVscDisconnectFromVsp(struct netvsc_device *net_device)
567
static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
696
569
netvsc_destroy_recv_buf(net_device);
697
570
netvsc_destroy_send_buf(net_device);
701
* netvsc_device_add - Callback when the device belonging to this
704
static int netvsc_device_add(struct hv_device *device, void *additional_info)
708
struct netvsc_device *net_device;
709
struct hv_netvsc_packet *packet, *pos;
710
struct netvsc_driver *net_driver =
711
(struct netvsc_driver *)device->Driver;
713
net_device = alloc_net_device(device);
719
DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", net_device);
721
/* Initialize the NetVSC channel extension */
722
net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
723
spin_lock_init(&net_device->recv_pkt_list_lock);
725
net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
727
INIT_LIST_HEAD(&net_device->recv_pkt_list);
729
for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
730
packet = kzalloc(sizeof(struct hv_netvsc_packet) +
731
(NETVSC_RECEIVE_SG_COUNT *
732
sizeof(struct hv_page_buffer)), GFP_KERNEL);
734
DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
735
"for receive pool (wanted %d got %d)",
736
NETVSC_RECEIVE_PACKETLIST_COUNT, i);
739
list_add_tail(&packet->list_ent,
740
&net_device->recv_pkt_list);
742
net_device->channel_init_event = osd_waitevent_create();
743
if (!net_device->channel_init_event) {
748
/* Open the channel */
749
ret = vmbus_open(device->channel, net_driver->ring_buf_size,
750
net_driver->ring_buf_size, NULL, 0,
751
netvsc_channel_cb, device);
754
DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
759
/* Channel is opened */
760
DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
762
/* Connect with the NetVsp */
763
ret = netvsc_connect_vsp(device);
765
DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
770
DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
776
/* Now, we can close the channel safely */
777
vmbus_close(device->channel);
782
kfree(net_device->channel_init_event);
784
list_for_each_entry_safe(packet, pos,
785
&net_device->recv_pkt_list,
787
list_del(&packet->list_ent);
791
release_outbound_net_device(device);
792
release_inbound_net_device(device);
794
free_net_device(net_device);
801
574
* netvsc_device_remove - Callback when the root bus device is removed
803
static int netvsc_device_remove(struct hv_device *device)
576
int netvsc_device_remove(struct hv_device *device)
805
578
struct netvsc_device *net_device;
806
579
struct hv_netvsc_packet *netvsc_packet, *pos;
808
DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
811
581
/* Stop outbound traffic ie sends and receives completions */
812
582
net_device = release_outbound_net_device(device);
813
583
if (!net_device) {
814
DPRINT_ERR(NETVSC, "No net device present!!");
584
dev_err(&device->device, "No net device present!!");
818
588
/* Wait for all send completions */
819
589
while (atomic_read(&net_device->num_outstanding_sends)) {
820
DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
821
atomic_read(&net_device->num_outstanding_sends));
590
dev_err(&device->device,
591
"waiting for %d requests to complete...",
592
atomic_read(&net_device->num_outstanding_sends));
825
DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
827
NetVscDisconnectFromVsp(net_device);
829
DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
596
netvsc_disconnect_vsp(net_device);
832
598
/* Stop inbound traffic ie receives and sends completions */
833
599
net_device = release_inbound_net_device(device);
835
601
/* At this point, no one should be accessing netDevice except in here */
836
DPRINT_INFO(NETVSC, "net device (%p) safe to remove", net_device);
602
dev_notice(&device->device, "net device safe to remove");
838
604
/* Now, we can close the channel safely */
839
605
vmbus_close(device->channel);
717
static void netvsc_send_recv_completion(struct hv_device *device,
720
struct nvsp_message recvcompMessage;
724
recvcompMessage.hdr.msg_type =
725
NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
727
/* FIXME: Pass in the status */
728
recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
732
/* Send the completion */
733
ret = vmbus_sendpacket(device->channel, &recvcompMessage,
734
sizeof(struct nvsp_message), transaction_id,
739
} else if (ret == -1) {
740
/* no more room...wait a bit and attempt to retry 3 times */
742
dev_err(&device->device, "unable to send receive completion pkt"
743
" (tid %llx)...retrying %d", transaction_id, retries);
747
goto retry_send_cmplt;
749
dev_err(&device->device, "unable to send receive "
750
"completion pkt (tid %llx)...give up retrying",
754
dev_err(&device->device, "unable to send receive "
755
"completion pkt - %llx", transaction_id);
759
/* Send a receive completion packet to RNDIS device (ie NetVsp) */
760
static void netvsc_receive_completion(void *context)
762
struct hv_netvsc_packet *packet = context;
763
struct hv_device *device = (struct hv_device *)packet->device;
764
struct netvsc_device *net_device;
765
u64 transaction_id = 0;
766
bool fsend_receive_comp = false;
770
* Even though it seems logical to do a GetOutboundNetDevice() here to
771
* send out receive completion, we are using GetInboundNetDevice()
772
* since we may have disable outbound traffic already.
774
net_device = get_inbound_net_device(device);
776
dev_err(&device->device, "unable to get net device..."
777
"device being destroyed?");
781
/* Overloading use of the lock. */
782
spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
784
packet->xfer_page_pkt->count--;
787
* Last one in the line that represent 1 xfer page packet.
788
* Return the xfer page packet itself to the freelist
790
if (packet->xfer_page_pkt->count == 0) {
791
fsend_receive_comp = true;
792
transaction_id = packet->completion.recv.recv_completion_tid;
793
list_add_tail(&packet->xfer_page_pkt->list_ent,
794
&net_device->recv_pkt_list);
798
/* Put the packet back */
799
list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
800
spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
802
/* Send a receive completion for the xfer page packet */
803
if (fsend_receive_comp)
804
netvsc_send_recv_completion(device, transaction_id);
806
put_net_device(device);
963
809
static void netvsc_receive(struct hv_device *device,
964
810
struct vmpacket_descriptor *packet)
987
834
* All inbound packets other than send completion should be xfer page
990
if (packet->Type != VmbusPacketTypeDataUsingTransferPages) {
991
DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
837
if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
838
dev_err(&device->device, "Unknown packet type received - %d",
993
840
put_net_device(device);
997
844
nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
998
(packet->DataOffset8 << 3));
845
(packet->offset8 << 3));
1000
847
/* Make sure this is a valid nvsp packet */
1001
848
if (nvsp_packet->hdr.msg_type !=
1002
849
NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
1003
DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1004
nvsp_packet->hdr.msg_type);
850
dev_err(&device->device, "Unknown nvsp packet type received-"
851
" %d", nvsp_packet->hdr.msg_type);
1005
852
put_net_device(device);
1009
DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1010
nvsp_packet->hdr.msg_type);
1012
856
vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
1014
if (vmxferpage_packet->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1015
DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
858
if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
859
dev_err(&device->device, "Invalid xfer page set id - "
1016
860
"expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1017
vmxferpage_packet->TransferPageSetId);
861
vmxferpage_packet->xfer_pageset_id);
1018
862
put_net_device(device);
1022
DPRINT_DBG(NETVSC, "xfer page - range count %d",
1023
vmxferpage_packet->RangeCount);
1026
867
* Grab free packets (range count + 1) to represent this xfer
1027
868
* page packet. +1 to represent the xfer page packet itself.
1091
930
netvsc_packet->device = device;
1092
931
/* Save this so that we can send it back */
1093
932
netvsc_packet->completion.recv.recv_completion_tid =
1094
vmxferpage_packet->d.TransactionId;
933
vmxferpage_packet->d.trans_id;
1096
935
netvsc_packet->total_data_buflen =
1097
vmxferpage_packet->Ranges[i].ByteCount;
936
vmxferpage_packet->ranges[i].byte_count;
1098
937
netvsc_packet->page_buf_cnt = 1;
1100
/* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1101
/* vmxferpagePacket->Ranges[i].ByteCount < */
1102
/* netDevice->ReceiveBufferSize); */
1104
netvsc_packet->page_buf[0].Length =
1105
vmxferpage_packet->Ranges[i].ByteCount;
939
netvsc_packet->page_buf[0].len =
940
vmxferpage_packet->ranges[i].byte_count;
1107
942
start = virt_to_phys((void *)((unsigned long)net_device->
1108
recv_buf + vmxferpage_packet->Ranges[i].ByteOffset));
943
recv_buf + vmxferpage_packet->ranges[i].byte_offset));
1110
netvsc_packet->page_buf[0].Pfn = start >> PAGE_SHIFT;
945
netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
1111
946
end_virtual = (unsigned long)net_device->recv_buf
1112
+ vmxferpage_packet->Ranges[i].ByteOffset
1113
+ vmxferpage_packet->Ranges[i].ByteCount - 1;
947
+ vmxferpage_packet->ranges[i].byte_offset
948
+ vmxferpage_packet->ranges[i].byte_count - 1;
1114
949
end = virt_to_phys((void *)end_virtual);
1116
951
/* Calculate the page relative offset */
1117
netvsc_packet->page_buf[0].Offset =
1118
vmxferpage_packet->Ranges[i].ByteOffset &
952
netvsc_packet->page_buf[0].offset =
953
vmxferpage_packet->ranges[i].byte_offset &
1119
954
(PAGE_SIZE - 1);
1120
955
if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1121
956
/* Handle frame across multiple pages: */
1122
netvsc_packet->page_buf[0].Length =
1123
(netvsc_packet->page_buf[0].Pfn <<
957
netvsc_packet->page_buf[0].len =
958
(netvsc_packet->page_buf[0].pfn <<
1125
960
+ PAGE_SIZE - start;
1126
961
bytes_remain = netvsc_packet->total_data_buflen -
1127
netvsc_packet->page_buf[0].Length;
962
netvsc_packet->page_buf[0].len;
1128
963
for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1129
netvsc_packet->page_buf[j].Offset = 0;
964
netvsc_packet->page_buf[j].offset = 0;
1130
965
if (bytes_remain <= PAGE_SIZE) {
1131
netvsc_packet->page_buf[j].Length =
966
netvsc_packet->page_buf[j].len =
1133
968
bytes_remain = 0;
1135
netvsc_packet->page_buf[j].Length =
970
netvsc_packet->page_buf[j].len =
1137
972
bytes_remain -= PAGE_SIZE;
1139
netvsc_packet->page_buf[j].Pfn =
974
netvsc_packet->page_buf[j].pfn =
1140
975
virt_to_phys((void *)(end_virtual -
1141
976
bytes_remain)) >> PAGE_SHIFT;
1142
977
netvsc_packet->page_buf_cnt++;
1143
978
if (bytes_remain == 0)
1146
/* ASSERT(bytesRemain == 0); */
1148
DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1149
"(pfn %llx, offset %u, len %u)", i,
1150
vmxferpage_packet->Ranges[i].ByteOffset,
1151
vmxferpage_packet->Ranges[i].ByteCount,
1152
netvsc_packet->page_buf[0].Pfn,
1153
netvsc_packet->page_buf[0].Offset,
1154
netvsc_packet->page_buf[0].Length);
1156
983
/* Pass it to the upper layer */
1157
((struct netvsc_driver *)device->Driver)->
1158
recv_cb(device, netvsc_packet);
984
rndis_filter_receive(device, netvsc_packet);
1160
986
netvsc_receive_completion(netvsc_packet->
1161
987
completion.recv.recv_completion_ctx);
1164
/* ASSERT(list_empty(&listHead)); */
1166
put_net_device(device);
1169
static void netvsc_send_recv_completion(struct hv_device *device,
1172
struct nvsp_message recvcompMessage;
1176
DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1179
recvcompMessage.hdr.msg_type =
1180
NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
1182
/* FIXME: Pass in the status */
1183
recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
1187
/* Send the completion */
1188
ret = vmbus_sendpacket(device->channel, &recvcompMessage,
1189
sizeof(struct nvsp_message), transaction_id,
1190
VmbusPacketTypeCompletion, 0);
1194
} else if (ret == -1) {
1195
/* no more room...wait a bit and attempt to retry 3 times */
1197
DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1198
"(tid %llx)...retrying %d", transaction_id, retries);
1202
goto retry_send_cmplt;
1204
DPRINT_ERR(NETVSC, "unable to send receive completion "
1205
"pkt (tid %llx)...give up retrying",
1209
DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1210
"%llx", transaction_id);
1214
/* Send a receive completion packet to RNDIS device (ie NetVsp) */
1215
static void netvsc_receive_completion(void *context)
1217
struct hv_netvsc_packet *packet = context;
1218
struct hv_device *device = (struct hv_device *)packet->device;
1219
struct netvsc_device *net_device;
1220
u64 transaction_id = 0;
1221
bool fsend_receive_comp = false;
1222
unsigned long flags;
1224
/* ASSERT(packet->XferPagePacket); */
1227
* Even though it seems logical to do a GetOutboundNetDevice() here to
1228
* send out receive completion, we are using GetInboundNetDevice()
1229
* since we may have disable outbound traffic already.
1231
net_device = get_inbound_net_device(device);
1233
DPRINT_ERR(NETVSC, "unable to get net device..."
1234
"device being destroyed?");
1238
/* Overloading use of the lock. */
1239
spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1241
/* ASSERT(packet->XferPagePacket->Count > 0); */
1242
packet->xfer_page_pkt->count--;
1245
* Last one in the line that represent 1 xfer page packet.
1246
* Return the xfer page packet itself to the freelist
1248
if (packet->xfer_page_pkt->count == 0) {
1249
fsend_receive_comp = true;
1250
transaction_id = packet->completion.recv.recv_completion_tid;
1251
list_add_tail(&packet->xfer_page_pkt->list_ent,
1252
&net_device->recv_pkt_list);
1256
/* Put the packet back */
1257
list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
1258
spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
1260
/* Send a receive completion for the xfer page packet */
1261
if (fsend_receive_comp)
1262
netvsc_send_recv_completion(device, transaction_id);
1264
990
put_net_device(device);
1080
* netvsc_device_add - Callback when the device belonging to this
1083
int netvsc_device_add(struct hv_device *device, void *additional_info)
1088
((struct netvsc_device_info *)additional_info)->ring_size;
1089
struct netvsc_device *net_device;
1090
struct hv_netvsc_packet *packet, *pos;
1092
net_device = alloc_net_device(device);
1098
/* Initialize the NetVSC channel extension */
1099
net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1100
spin_lock_init(&net_device->recv_pkt_list_lock);
1102
net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
1104
INIT_LIST_HEAD(&net_device->recv_pkt_list);
1106
for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
1107
packet = kzalloc(sizeof(struct hv_netvsc_packet) +
1108
(NETVSC_RECEIVE_SG_COUNT *
1109
sizeof(struct hv_page_buffer)), GFP_KERNEL);
1113
list_add_tail(&packet->list_ent,
1114
&net_device->recv_pkt_list);
1116
init_completion(&net_device->channel_init_wait);
1118
/* Open the channel */
1119
ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1120
ring_size * PAGE_SIZE, NULL, 0,
1121
netvsc_channel_cb, device);
1124
dev_err(&device->device, "unable to open channel: %d", ret);
1129
/* Channel is opened */
1130
pr_info("hv_netvsc channel opened successfully");
1132
/* Connect with the NetVsp */
1133
ret = netvsc_connect_vsp(device);
1135
dev_err(&device->device,
1136
"unable to connect to NetVSP - %d", ret);
1144
/* Now, we can close the channel safely */
1145
vmbus_close(device->channel);
1150
list_for_each_entry_safe(packet, pos,
1151
&net_device->recv_pkt_list,
1153
list_del(&packet->list_ent);
1157
release_outbound_net_device(device);
1158
release_inbound_net_device(device);
1160
free_net_device(net_device);
1167
* netvsc_initialize - Main entry point
1169
int netvsc_initialize(struct hv_driver *drv)
1172
drv->name = driver_name;
1173
memcpy(&drv->dev_type, &netvsc_device_type, sizeof(struct hv_guid));