forked from OpenXT/xc-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xen.c
2673 lines (2151 loc) · 83.6 KB
/
xen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// xen.c - Xen interfaces for SCSI Miniport driver.
//
// Copyright (c) 2006 XenSource, Inc.
//
/*
* Copyright (c) 2010 Citrix Systems, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma warning (push, 3)
#include "xenvbd.h"
#include "scsiboot.h"
#include "xsapi.h"
#include "../xenutil/xenbus.h"
#include "../xenutil/reexport.h"
#pragma warning (pop)
//
// Dont care about unreferenced formal parameters here
//
#pragma warning( disable : 4100 )
VOID XenvbdEvtchnCallback(PVOID Context);
static BOOLEAN SubmitSrb(PXHBD_TARGET_INFO ptargetInfo, PSCSI_REQUEST_BLOCK srb);
static void FreeBounceBuffer(int bid);
static VOID XenvbdCompleteRequests(PXHBD_TARGET_INFO ptargetInfo);
static VOID
SortDeviceListToMatchQemu(
IN OUT PCHAR *devices,
IN OUT int *deviceIds,
IN OUT PCHAR *names,
IN ULONG numVbds
)
/*++
Description:
Sort the device list so that it matches the order in which qemu
will present them
--*/
{
ULONG div;
ULONG probe;
int this_device_id;
CHAR *this_device;
ULONG i;
TraceDebug(("Before sort:\n"));
for (i = 0; i < numVbds; i++)
{
TraceDebug(("%d -> %s-%s.\n", deviceIds[i], devices[i], names[i]));
}
//
// insertion sort
//
for (div = 1; div < numVbds; div++)
{
for (probe = 0; probe < div; probe++)
{
//
// check for SCSI (Major number 0x8)
// if probe is SCSI and div is not, then probe is larger
// if probe is IDE and div is SCSI, then div is larger
// if probe and div are IDE, then sort on device_id
// if probe and div are SCSI, then sort on device_id
//
if ((((deviceIds[probe] & 0xff00) == 0x800) &&
((deviceIds[div] & 0xff00) == 0x800)) ||
(((deviceIds[probe] & 0xff00) != 0x800) &&
((deviceIds[div] & 0xff00) != 0x800)) ||
(((deviceIds[probe] & 0xff00) == 0x800) &&
((deviceIds[div] & 0xff00) != 0x800)))
{
//
// 1) probe and div are either both SCSI or both IDE
// OR
// 2) probe is SCSI and div is IDE
//
//
if (deviceIds[div] < deviceIds[probe])
{
this_device_id = deviceIds[probe];
deviceIds[probe] = deviceIds[div];
deviceIds[div] = this_device_id;
this_device = devices[probe];
devices[probe] = devices[div];
devices[div] = this_device;
this_device = names[probe];
names[probe] = names[div];
names[div] = this_device;
}
}
}
}
TraceDebug(("After sort:\n"));
for (i = 0; i < numVbds; i++)
{
TraceDebug(("%d -> %s-%s.\n", deviceIds[i], devices[i], names[i]));
}
}
PCHAR
find_backend_path(PXHBD_TARGET_INFO ptargetInfo, SUSPEND_TOKEN token)
{
PCHAR tmp, devPath;
/* backend_path = ptargetInfo->xenbusNodeName + "/backend"; */
tmp = Xmasprintf("%s/backend", ptargetInfo->xenbusNodeName);
if (!tmp) {
TraceError(("cannot find backend path for %s.\n",
ptargetInfo->xenbusNodeName));
return NULL;
}
xenbus_read(XBT_NIL, tmp, &devPath);
XmFreeMemory(tmp);
TraceDebug(("backend path is %s.\n", devPath));
return devPath;
}
static NTSTATUS
XenbusGetBackendInfo(
PXHBD_TARGET_INFO ptargetInfo,
CHAR *BackendPath,
SUSPEND_TOKEN token
)
{
ULONG64 tmp;
NTSTATUS status;
status = xenbus_read_int(XBT_NIL, BackendPath, "info",
&tmp);
if (!NT_SUCCESS(status))
return status;
ptargetInfo->info = (ULONG)tmp;
status = xenbus_read_int(XBT_NIL, BackendPath, "sectors",
&ptargetInfo->sectors);
if (!NT_SUCCESS(status))
return status;
status = xenbus_read_int(XBT_NIL, BackendPath, "sector-size",
&tmp);
if (!NT_SUCCESS(status))
return status;
ptargetInfo->sector_size = (ULONG)tmp;
return STATUS_SUCCESS;
}
VOID
CloseFrontend(
IN PXHBD_TARGET_INFO ptargetInfo,
IN PCHAR backend_path,
IN SUSPEND_TOKEN token
)
{
CHAR *prefix = ptargetInfo->xenbusNodeName;
XENBUS_STATE frontend_state;
XENBUS_STATE backend_state;
NTSTATUS status;
TraceNotice(("target %d: closing frontend...\n", ptargetInfo->targetId));
// Get initial frontend state
status = xenbus_read_state(XBT_NIL, prefix, "state", &frontend_state);
if (!NT_SUCCESS(status))
frontend_state = null_XENBUS_STATE();
// Wait for the backend to stabilise
backend_state = null_XENBUS_STATE();
do {
backend_state = XenbusWaitForBackendStateChange(backend_path, backend_state,
NULL, token);
} while (same_XENBUS_STATE(backend_state, XENBUS_STATE_INITIALISING));
TraceVerbose(("%s: target %d: backend state = %s, frontend state = %s\n",
__FUNCTION__,
ptargetInfo->targetId, XenbusStateName(backend_state),
XenbusStateName(frontend_state)));
frontend_state = XENBUS_STATE_CLOSING;
while (!same_XENBUS_STATE(backend_state, XENBUS_STATE_CLOSING) &&
!same_XENBUS_STATE(backend_state, XENBUS_STATE_CLOSED) &&
!is_null_XENBUS_STATE(backend_state)) {
xenbus_change_state(XBT_NIL, prefix, "state", frontend_state);
backend_state = XenbusWaitForBackendStateChange(backend_path, backend_state,
NULL, token);
}
TraceVerbose(("%s: target %d: backend state = %s, frontend state = %s\n",
__FUNCTION__,
ptargetInfo->targetId, XenbusStateName(backend_state),
XenbusStateName(frontend_state)));
frontend_state = XENBUS_STATE_CLOSED;
while (!same_XENBUS_STATE(backend_state, XENBUS_STATE_CLOSED) &&
!is_null_XENBUS_STATE(backend_state)) {
xenbus_change_state(XBT_NIL, prefix, "state", frontend_state);
backend_state = XenbusWaitForBackendStateChange(backend_path, backend_state,
NULL, token);
}
TraceVerbose(("%s: target %d: backend state = %s, frontend state = %s\n",
__FUNCTION__,
ptargetInfo->targetId, XenbusStateName(backend_state),
XenbusStateName(frontend_state)));
TraceNotice(("target %d: backend is closed\n", ptargetInfo->targetId));
}
NTSTATUS
PrepareBackendForReconnect(
IN PXHBD_TARGET_INFO Target,
IN PCHAR backend_path,
IN SUSPEND_TOKEN token
)
{
CHAR *prefix = Target->xenbusNodeName;
XENBUS_STATE frontend_state;
XENBUS_STATE backend_state;
NTSTATUS status;
TraceNotice(("target %d: preparing backend for reconnection...\n",
Target->targetId));
frontend_state = null_XENBUS_STATE();
do {
xenbus_transaction_t xbt;
ULONG64 Id;
xenbus_transaction_start(&xbt);
status = xenbus_read_state(xbt, Target->xenbusNodeName, "state",
&frontend_state);
if (!NT_SUCCESS(status)) {
TraceWarning(("target %d: VBD %d frontend area has disappeared.\n",
Target->targetId, Target->handle));
(VOID) xenbus_transaction_end(xbt, 1);
break;
}
// Check we're still talking to the same incarnation of the frontend area that we
// originally enumerated.
status = xenbus_read_int(xbt, Target->xenbusNodeName, "target-id", &Id);
if (!NT_SUCCESS(status) || Id != (ULONG64)Target->targetId) {
TraceWarning(("target %d: VBD %d frontend area has been re-created?\n",
Target->targetId, Target->handle));
(VOID) xenbus_transaction_end(xbt, 1);
break;
}
status = STATUS_UNSUCCESSFUL;
if (!same_XENBUS_STATE(frontend_state, XENBUS_STATE_CLOSED)) {
TraceWarning(("target %d: VBD %d frontend area is not CLOSED.\n",
Target->targetId, Target->handle));
(VOID) xenbus_transaction_end(xbt, 1);
break;
}
frontend_state = XENBUS_STATE_INITIALISING;
xenbus_change_state(xbt, prefix, "state", frontend_state);
status = xenbus_transaction_end(xbt, 0);
} while (status == STATUS_RETRY);
if (!NT_SUCCESS(status))
goto fail1;
// Wait for the backend
backend_state = null_XENBUS_STATE();
do {
backend_state = XenbusWaitForBackendStateChange(backend_path, backend_state,
NULL, token);
} while (same_XENBUS_STATE(backend_state, XENBUS_STATE_CLOSED) ||
same_XENBUS_STATE(backend_state, XENBUS_STATE_INITIALISING));
TraceVerbose(("%s: target %d: backend state = %s, frontend state = %s\n",
__FUNCTION__,
Target->targetId, XenbusStateName(backend_state),
XenbusStateName(frontend_state)));
status = STATUS_UNSUCCESSFUL;
if (!same_XENBUS_STATE(backend_state, XENBUS_STATE_INITWAIT))
goto fail2;
TraceNotice(("target %d: backend is waiting\n", Target->targetId));
return STATUS_SUCCESS;
fail2:
TraceError(("%s: fail2\n", __FUNCTION__));
CloseFrontend(Target, backend_path, token);
fail1:
TraceError(("%s: fail1 (%08x)\n", __FUNCTION__, status));
return status;
}
static VOID
InitialiseSharedRing(PXHBD_TARGET_INFO ptargetInfo)
{
SHARED_RING_INIT(ptargetInfo->sring);
FRONT_RING_INIT(&ptargetInfo->ring, ptargetInfo->sring,
PAGE_SIZE << ptargetInfo->sring_order);
}
/* Allocate Xen-side resources for a target: sring grant reference and
event channel port. */
static NTSTATUS
XenvbdSetupTargetXen(PXHBD_TARGET_INFO ptargetInfo)
{
NTSTATUS stat;
PHYSICAL_ADDRESS pa;
unsigned min_grefs;
unsigned nr_ring_pages;
unsigned x;
/* The scsifilt IO path can tolerate running out of grefs by just
re-queueing the request, provided it can always keep at least
one request outstanding. The non-scsifilt path can't, and we
need to guarantee that we always have enough grefs available
for it. */
if (!AustereMode)
min_grefs = XENVBD_MAX_SEGMENTS_PER_SRB;
else
min_grefs = XENVBD_MAX_SEGMENTS_PER_SRB * BLK_RING_SIZE;
/* Make sure we can share the ring itself. */
nr_ring_pages = 1 << ptargetInfo->sring_order;
min_grefs += nr_ring_pages;
ptargetInfo->grant_cache = GnttabAllocCache(min_grefs);
if (!ptargetInfo->grant_cache) {
TraceError(("Failed to set up grant cache.\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
for (x = 0; x < nr_ring_pages; x++) {
pa = XenGetPhysicalAddress((PVOID)((ULONG_PTR)ptargetInfo->sring +
(x * PAGE_SIZE)));
ptargetInfo->ring_refs[x] =
GnttabGrantForeignAccessCache(ptargetInfo->backendId,
PHYS_TO_PFN(pa),
GRANT_MODE_RW,
ptargetInfo->grant_cache);
XM_ASSERT(!is_null_GRANT_REF(ptargetInfo->ring_refs[x]));
}
ptargetInfo->evtchn_port =
EvtchnAllocUnbound(ptargetInfo->backendId,XenvbdEvtchnCallback,
ptargetInfo);
if (is_null_EVTCHN_PORT(ptargetInfo->evtchn_port)) {
TraceError(("Failed to allocate event channel port.\n"));
for (x = 0; x < nr_ring_pages; x++) {
stat = GnttabEndForeignAccessCache(ptargetInfo->ring_refs[x],
ptargetInfo->grant_cache);
XM_ASSERT(NT_SUCCESS(stat));
ptargetInfo->ring_refs[x] = null_GRANT_REF();
}
GnttabFreeCache(ptargetInfo->grant_cache);
ptargetInfo->grant_cache = NULL;
return STATUS_INSUFFICIENT_RESOURCES;
}
return STATUS_SUCCESS;
}
/* Final stage of connecting to a backend: do the xenbus transaction
* and wait for the backend to give us the go-ahead. This is invoked
* from both initial ring connection and a late recover from suspend
* handler. The latter provides very little synchronisation: there
* are requests coming in all the time while this is running. On the
* plus side, nobody's going to try and free the target info or change
* xenbusNodeName, and that's pretty much all we need. */
static NTSTATUS
XenvbdSetupTargetXenbus(PXHBD_TARGET_INFO ptargetInfo,
CHAR *BackendPath,
SUSPEND_TOKEN token)
{
XENBUS_STATE state;
NTSTATUS status;
xenbus_transaction_t xbt;
PCHAR xenbusNode = ptargetInfo->xenbusNodeName;
unsigned x;
char buffer[16];
do {
TraceDebug(("XBST: tx start %s\n", xenbusNode));
xenbus_transaction_start(&xbt);
xenbus_write_evtchn_port(xbt, xenbusNode, "event-channel",
ptargetInfo->evtchn_port);
if (ptargetInfo->sring_single_page) {
TraceNotice(("%s: using single page handshake\n", xenbusNode));
xenbus_write_grant_ref(xbt, xenbusNode, "ring-ref",
ptargetInfo->ring_refs[0]);
} else {
TraceNotice(("%s: using multi-page handshake\n", xenbusNode));
xenbus_printf(xbt, xenbusNode, "ring-page-order", "%u",
ptargetInfo->sring_order);
for (x = 0; x < (1u << ptargetInfo->sring_order); x++) {
Xmsnprintf(buffer, sizeof(buffer), "ring-ref%d", x);
xenbus_write_grant_ref(xbt, xenbusNode, buffer,
ptargetInfo->ring_refs[x]);
}
}
xenbus_printf(xbt, xenbusNode, "protocol", "x86_32-abi");
xenbus_write_feature_flag(xbt, xenbusNode, "feature-surprise-remove",
TRUE);
xenbus_write_feature_flag(xbt, xenbusNode, "feature-online-resize",
TRUE);
xenbus_change_state(xbt, xenbusNode, "state",
XENBUS_STATE_INITIALISED);
TraceDebug(("XBST: tx end\n"));
status = xenbus_transaction_end(xbt, 0);
TraceDebug(("XBST: tx end status = %x\n", status));
} while (status == STATUS_RETRY);
if (status != STATUS_SUCCESS) {
TraceError(("XenbusSetupTarget: trans failed: %x\n",
status));
return status;
}
/* Wait for backend to become connected. */
state = null_XENBUS_STATE();
do {
state = XenbusWaitForBackendStateChange(BackendPath, state, NULL,
token);
} while (same_XENBUS_STATE(state, XENBUS_STATE_INITWAIT) ||
same_XENBUS_STATE(state, XENBUS_STATE_INITIALISING) ||
same_XENBUS_STATE(state, XENBUS_STATE_INITIALISED));
status = STATUS_UNSUCCESSFUL;
if (!same_XENBUS_STATE(state, XENBUS_STATE_CONNECTED)) {
TraceVerbose(("backend not connected.\n"));
return status;
}
xenbus_change_state(XBT_NIL, xenbusNode, "state",
XENBUS_STATE_CONNECTED);
return STATUS_SUCCESS;
}
void
CompleteSrb(PSCSI_REQUEST_BLOCK srb)
{
ScsiPortNotification(RequestComplete, XenvbdDeviceExtension, srb);
XM_ASSERT(XenvbdDeviceExtension->totInFlight != 0);
XenvbdDeviceExtension->totInFlight--;
}
static void
CleanupSrb(PXHBD_TARGET_INFO TargetInfo, PSCSI_REQUEST_BLOCK srb)
{
PSRB_EXTENSION srb_ext = SrbExtension(srb);
ULONG i;
for (i = 0; i < srb_ext->nr_requests; i++) {
xenvbd_request_t *request = &(srb_ext->request[i]);
ULONG j;
for (j = 0; j < request->nr_segments; j++) {
GRANT_REF gref = request->seg[j].gref;
XM_ASSERT(!is_null_GRANT_REF(gref));
GnttabEndForeignAccessCache(gref, TargetInfo->grant_cache);
if (srb_ext->bounced) {
BUFFER_ID bid = request->seg[j].bid;
XM_ASSERT(bid >= 0);
FreeBounceBuffer(bid);
}
}
memset(request, 0, sizeof (xenvbd_request_t));
}
}
static void
AbortSrbQueue(PSRB_QUEUE_RAW queue, PXHBD_TARGET_INFO TargetInfo,
BOOLEAN releaseResources)
{
PSCSI_REQUEST_BLOCK srb;
while ((srb = DequeueSrbRaw(queue)) != NULL) {
if (releaseResources)
CleanupSrb(TargetInfo, srb);
srb->ScsiStatus = 0x40; /* SCSI_ABORTED */
CompleteSrb(srb);
}
}
VOID
XenbusUpdateTargetUsage(
IN XHBD_TARGET_INFO *Target
)
{
XM_ASSERT(!AustereMode);
(VOID) xenbus_write_feature_flag(XBT_NIL, Target->targetPath, "paging",
Target->Paging);
(VOID) xenbus_write_feature_flag(XBT_NIL, Target->targetPath, "hibernation",
Target->Hibernation);
(VOID) xenbus_write_feature_flag(XBT_NIL, Target->targetPath, "dump",
Target->DumpFile);
}
VOID
XenbusEjectTarget(
IN XHBD_TARGET_INFO *Target
)
{
NTSTATUS status;
Target->Ejected = TRUE;
do {
xenbus_transaction_t xbt;
XENBUS_STATE State;
ULONG64 Id;
xenbus_transaction_start(&xbt);
// Check we're still talking to the same incarnation of the frontend area that we
// originally enumerated.
status = xenbus_read_int(XBT_NIL, Target->xenbusNodeName, "target-id", &Id);
if (!NT_SUCCESS(status) || Id != (ULONG64)Target->targetId) {
(VOID) xenbus_transaction_end(xbt, 1);
return;
}
status = xenbus_read_state(xbt, Target->xenbusNodeName, "state",
&State);
if (!NT_SUCCESS(status)) {
(VOID) xenbus_transaction_end(xbt, 1);
return;
}
// Prevent re-enumeration once the target is destroyed
(VOID) xenbus_write_feature_flag(xbt, Target->xenbusNodeName, "ejected", TRUE);
status = xenbus_transaction_end(xbt, 0);
} while (status == STATUS_RETRY);
if (!NT_SUCCESS(status))
TraceVerbose(("target %d: VBD %d frontend area has disappeared.\n",
Target->targetId, Target->handle));
}
// Should only be called from XenvbdScanTargets
VOID
XenbusDestroyTarget(
IN PXHBD_TARGET_INFO TargetInfo
)
{
ULONG targetId = TargetInfo->targetId;
ULONG References;
XM_ASSERT(TargetInfo->Next == NULL);
XM_ASSERT(KeGetCurrentThread() != XenvbdEnumLock.Owner);
while ((References = TargetInfo->References) != 0) {
LARGE_INTEGER Period;
TraceVerbose(("%s: %d thing(s) are holding a reference to target %d.\n",
__FUNCTION__, References, targetId));
Period.QuadPart = -100000;
KeDelayExecutionThread(KernelMode, FALSE, &Period);
}
XM_ASSERT(TargetInfo->FilterTarget.scsifilt == NULL);
xenbus_remove(XBT_NIL, TargetInfo->targetPath);
XmFreeMemory(TargetInfo->targetPath);
XM_ASSERT(PeekSrb(&TargetInfo->FreshSrbs) == NULL);
XM_ASSERT(PeekSrb(&TargetInfo->PreparedSrbs) == NULL);
XM_ASSERT(PeekSrb(&TargetInfo->SubmittedSrbs) == NULL);
if (TargetInfo->LateSuspendHandler != NULL)
EvtchnUnregisterSuspendHandler(TargetInfo->LateSuspendHandler);
if (TargetInfo->EarlySuspendHandler != NULL)
EvtchnUnregisterSuspendHandler(TargetInfo->EarlySuspendHandler);
if (TargetInfo->BackendStateWatch != NULL)
xenbus_unregister_watch(TargetInfo->BackendStateWatch);
XM_ASSERT(is_null_EVTCHN_PORT(TargetInfo->evtchn_port));
XM_ASSERT(TargetInfo->grant_cache == NULL);
XM_ASSERT(TargetInfo->sring == NULL);
ReleaseScsiInquiryData(TargetInfo);
XmFreeMemory(TargetInfo->xenbusNodeName);
XmFreeMemory(TargetInfo);
TraceNotice(("target %d: destroyed\n", targetId));
return;
}
static void
ProbeBackendCapabilities(PXHBD_TARGET_INFO ptargetInfo, CHAR *BackendPath)
{
NTSTATUS status;
ULONG64 order;
status = xenbus_read_domain_id(XBT_NIL, ptargetInfo->xenbusNodeName,
"backend-id", &ptargetInfo->backendId);
if (!NT_SUCCESS(status)) {
TraceError(("Failed to read backend id from %s (%x)\n",
ptargetInfo->xenbusNodeName, status));
/* Assume dom0. The alternative is pretty much just to
* crash. */
ptargetInfo->backendId = DOMAIN_ID_0();
}
/* Set defaults for when we can't probe. */
ptargetInfo->sring_single_page = TRUE;
ptargetInfo->sring_order = 0;
status = xenbus_read_int(XBT_NIL, BackendPath, "max-ring-page-order", &order);
if (NT_SUCCESS(status)) {
if (XenvbdDeviceExtension->MaxRingPageOrder >= 0 &&
order > (ULONG64)XenvbdDeviceExtension->MaxRingPageOrder)
order = (ULONG64)XenvbdDeviceExtension->MaxRingPageOrder;
if (order > MAX_RING_PAGE_ORDER)
order = MAX_RING_PAGE_ORDER;
if (AustereMode)
order = 0;
ptargetInfo->sring_single_page = FALSE;
ptargetInfo->sring_order = (ULONG)order;
}
TraceInfo(("Using %u sring page(s).\n", 1 << ptargetInfo->sring_order));
}
NTSTATUS
XenvbdConnectTarget(
IN XHBD_TARGET_INFO *Target,
IN CHAR *BackendPath,
IN SUSPEND_TOKEN Token
)
{
ULONG x;
XENBUS_STATE state;
NTSTATUS status;
state = XenbusWaitForBackendStateChange(BackendPath, null_XENBUS_STATE(), NULL,
Token);
status = STATUS_UNSUCCESSFUL;
if (!same_XENBUS_STATE(state, XENBUS_STATE_INITWAIT))
goto fail1;
ProbeBackendCapabilities(Target, BackendPath);
Target->sring = XmAllocateZeroedMemory(PAGE_SIZE << Target->sring_order);
status = STATUS_NO_MEMORY;
if (Target->sring == NULL)
goto fail2;
status = XenvbdSetupTargetXen(Target);
if (status != STATUS_SUCCESS)
goto fail3;
InitialiseSharedRing(Target);
status = XenvbdSetupTargetXenbus(Target, BackendPath, Token);
if (status != STATUS_SUCCESS)
goto fail4;
XenbusGetBackendInfo(Target, BackendPath, Token);
return STATUS_SUCCESS;
fail4:
TraceError(("%s: fail4\n", __FUNCTION__));
CloseFrontend(Target, BackendPath, Token);
for (x = 0; x < (1u << Target->sring_order); x++) {
if (!is_null_GRANT_REF(Target->ring_refs[x])) {
GnttabEndForeignAccessCache(Target->ring_refs[x],
Target->grant_cache);
Target->ring_refs[x] = null_GRANT_REF();
}
}
GnttabFreeCache(Target->grant_cache);
Target->grant_cache = NULL;
Target->ring.sring = NULL;
fail3:
TraceError(("%s: fail3\n", __FUNCTION__));
XmFreeMemory(Target->sring);
Target->sring = NULL;
fail2:
TraceError(("%s: fail2\n", __FUNCTION__));
fail1:
TraceError(("%s: fail1 (0x%08x)\n", __FUNCTION__, status));
return status;
}
VOID
XenvbdDisconnectTarget(
IN XHBD_TARGET_INFO *Target,
IN CHAR *BackendPath,
IN SUSPEND_TOKEN Token
)
{
AbortSrbQueue(&Target->FreshSrbs, Target, FALSE);
AbortSrbQueue(&Target->PreparedSrbs, Target, TRUE);
AbortSrbQueue(&Target->SubmittedSrbs, Target, TRUE);
EvtchnClose(Target->evtchn_port);
Target->evtchn_port = null_EVTCHN_PORT();
if (Target->grant_cache) {
ULONG x;
for (x = 0; x < (1u << Target->sring_order); x++) {
if (!is_null_GRANT_REF(Target->ring_refs[x])) {
GnttabEndForeignAccessCache(Target->ring_refs[x],
Target->grant_cache);
Target->ring_refs[x] = null_GRANT_REF();
}
}
GnttabFreeCache(Target->grant_cache);
Target->grant_cache = NULL;
}
Target->ring.sring = NULL;
if (Target->sring) {
XmFreeMemory(Target->sring);
Target->sring = NULL;
}
}
static VOID
BackendStateChanged(
IN VOID *Context
)
{
ULONG TargetID = (ULONG)(ULONG_PTR)Context;
XHBD_TARGET_INFO *Target;
SUSPEND_TOKEN Token;
CHAR *BackendPath;
XENBUS_STATE BackendState;
CHAR *FrontendPath;
XENBUS_STATE FrontendState;
KIRQL Irql;
NTSTATUS status;
Irql = acquire_irqsafe_lock(XenvbdTargetInfoLock);
Target = XenvbdTargetInfo[TargetID];
if (Target == NULL)
goto unlock;
if (Target->Suspended)
goto unlock;
Target->References++;
release_irqsafe_lock(XenvbdTargetInfoLock, Irql);
FrontendPath = Target->xenbusNodeName;
Token = EvtchnAllocateSuspendToken(__FUNCTION__);
BackendPath = find_backend_path(Target, Token);
if (BackendPath == NULL)
goto done;
FrontendState = null_XENBUS_STATE();
BackendState = null_XENBUS_STATE();
do {
xenbus_transaction_t xbt;
xenbus_transaction_start(&xbt);
status = xenbus_read_state(xbt, BackendPath, "state", &BackendState);
if (!NT_SUCCESS(status)) {
(VOID) xenbus_transaction_end(xbt, 1);
break;
}
status = xenbus_read_state(xbt, FrontendPath, "state", &FrontendState);
if (!NT_SUCCESS(status)) {
(VOID) xenbus_transaction_end(xbt, 1);
break;
}
status = xenbus_transaction_end(xbt, 0);
} while (status == STATUS_RETRY);
if (!NT_SUCCESS(status))
goto done;
TraceVerbose(("%s: target %d: backend state = %s, frontend state = %s\n",
__FUNCTION__, TargetID,
XenbusStateName(BackendState),
XenbusStateName(FrontendState)));
if (same_XENBUS_STATE(FrontendState, XENBUS_STATE_CONNECTED) &&
(same_XENBUS_STATE(BackendState, XENBUS_STATE_CLOSING) ||
same_XENBUS_STATE(BackendState, XENBUS_STATE_CLOSED))) {
BOOLEAN DoEject;
DoEject = FALSE;
KeAcquireSpinLock(&Target->EjectLock, &Irql);
if (Target->DeviceObject != NULL) {
DoEject = TRUE;
Target->EjectRequested = TRUE;
} else {
Target->EjectPending = TRUE;
}
KeReleaseSpinLock(&Target->EjectLock, Irql);
if (DoEject) {
TraceNotice(("%s: Issuing eject request for target %d\n",
__FUNCTION__, TargetID));
IoRequestDeviceEject(Target->DeviceObject);
}
}
done:
EvtchnReleaseSuspendToken(Token);
Irql = acquire_irqsafe_lock(XenvbdTargetInfoLock);
Target->References--;
unlock:
release_irqsafe_lock(XenvbdTargetInfoLock, Irql);
}
static VOID
SetupBackendStateWatch(
IN XHBD_TARGET_INFO *Target,
IN CHAR *BackendPath
)
{
CHAR *StatePath;
NTSTATUS status;
if (AustereMode)
return;
StatePath = Xmasprintf("%s/state", BackendPath);
status = STATUS_NO_MEMORY;
if (StatePath == NULL)
goto fail1;
if (Target->BackendStateWatch != NULL) {
status = xenbus_redirect_watch(Target->BackendStateWatch, StatePath);
if (!NT_SUCCESS(status))
goto fail2;
} else {
Target->BackendStateWatch = xenbus_watch_path(StatePath,
BackendStateChanged,
(VOID *)(ULONG_PTR)Target->targetId);
status = STATUS_UNSUCCESSFUL;
if (Target->BackendStateWatch == NULL) {
goto fail2;
}
}
XmFreeMemory(StatePath);
return;
fail2:
TraceError(("%s: fail2\n", __FUNCTION__));
XmFreeMemory(StatePath);
fail1:
TraceError(("%s: fail1 (0x%08x)\n", __FUNCTION__, status));
}
static NTSTATUS
ClaimFrontend(
IN XHBD_TARGET_INFO *Target,
IN CHAR *BackendPath,
IN SUSPEND_TOKEN Token
)
{
xenbus_transaction_t xbt;
XENBUS_STATE FrontendState;
XENBUS_STATE BackendState;
NTSTATUS status;
TraceNotice(("target %d: claiming frontend...\n", Target->targetId));
BackendState = XenbusWaitForBackendStateChange(BackendPath,
null_XENBUS_STATE(),
NULL,
Token);
if (same_XENBUS_STATE(BackendState, XENBUS_STATE_INITIALISING)) {
LARGE_INTEGER Timeout;
// The tools create the frontend and backend areas in state
// INITIALISING, and the backend then quickly moves to INITWAIT.
// Check that the observed states are sane and then write our
// target ID into the frontend area to claim that we have
// enumerated it.
// If the backend is buggy it may never move out of INITIALISING
// so, after a suitably long time, just fail to claim and hope
// the tools can clean up.
Timeout.QuadPart = -1 // Relative
* 10 // to us
* 1000 // to ms
* 1000 // to s
* 30;
BackendState = XenbusWaitForBackendStateChange(BackendPath,
XENBUS_STATE_INITIALISING,
&Timeout,
Token);
}
// The backend may still be stuck in INITIALISING
status = STATUS_UNSUCCESSFUL;
if (same_XENBUS_STATE(BackendState, XENBUS_STATE_INITIALISING))
goto done;
if (!AustereMode) {
do {
xenbus_transaction_start(&xbt);
status = xenbus_read_state(xbt, Target->xenbusNodeName, "state", &FrontendState);
if (!NT_SUCCESS(status))
goto abort;
status = xenbus_read_state(xbt, BackendPath, "state", &BackendState);
if (!NT_SUCCESS(status))
goto abort;
if (same_XENBUS_STATE(FrontendState, XENBUS_STATE_INITIALISING) &&
same_XENBUS_STATE(BackendState, XENBUS_STATE_INITWAIT)) {
// Claim the target
status = xenbus_printf(xbt, Target->xenbusNodeName, "target-id", "%d",
Target->targetId);