-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
blkfront.cc
1943 lines (1620 loc) · 53.8 KB
/
blkfront.cc
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
/*
* XenBSD block device driver
*
* Copyright (c) 2009 Scott Long, Yahoo!
* Copyright (c) 2009 Frank Suchomel, Citrix
* Copyright (c) 2009 Doug F. Rabson, Citrix
* Copyright (c) 2005 Kip Macy
* Copyright (c) 2003-2004, Keir Fraser & Steve Hand
* Modifications by Mark A. Williamson are (c) Intel Research Cambridge
*
*
* 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.
*/
#define __STDC_FORMAT_MACROS
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <osv/mempool.hh>
#include <osv/contiguous_alloc.hh>
#include <bsd/porting/netport.h>
#include <bsd/porting/synch.h>
#include <bsd/porting/bus.h>
#include <bsd/porting/mmu.h>
#include <bsd/porting/kthread.h>
#include <bsd/porting/netport.h>
#include <bsd/porting/synch.h>
#include <bsd/porting/bus.h>
#include <bsd/porting/mmu.h>
#include <bsd/porting/kthread.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <sys/bio.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/resource.h>
#include <machine/intr_machdep.h>
#include <machine/vmparam.h>
#include <sys/bus_dma.h>
#include <machine/_inttypes.h>
#include <machine/xen/xen-os.h>
#include <machine/xen/xenvar.h>
#include <xen/hypervisor.h>
#include <xen/xen_intr.h>
#include <xen/evtchn.h>
#include <xen/gnttab.h>
#include <xen/interface/grant_table.h>
#include <xen/interface/io/protocols.h>
#include <xen/xenbus/xenbusvar.h>
#include <geom/geom_disk.h>
#include <dev/xen/blkfront/block.h>
#include "xenbus_if.h"
#include <stack>
/* prototypes */
static void xb_free_command(struct xb_command *cm);
static void xb_startio(struct xb_softc *sc);
static void blkfront_connect(struct xb_softc *);
static void blkfront_closing(device_t);
static int blkfront_detach(device_t);
static int setup_blkring(struct xb_softc *);
static void blkif_int(void *);
static void blkfront_initialize(struct xb_softc *);
static int blkif_completion(struct xb_command *);
static void blkif_free(struct xb_softc *);
static void blkif_queue_cb(void *, bus_dma_segment_t *, int, int);
#define GRANT_INVALID_REF 0
/* Control whether runtime update of vbds is enabled. */
#define ENABLE_VBD_UPDATE 0
#if ENABLE_VBD_UPDATE
static void vbd_update(void);
#endif
#define BLKIF_STATE_DISCONNECTED 0
#define BLKIF_STATE_CONNECTED 1
#define BLKIF_STATE_SUSPENDED 2
#ifdef notyet
static char *blkif_state_name[] = {
[BLKIF_STATE_DISCONNECTED] = "disconnected",
[BLKIF_STATE_CONNECTED] = "connected",
[BLKIF_STATE_SUSPENDED] = "closed",
};
static char * blkif_status_name[] = {
[BLKIF_INTERFACE_STATUS_CLOSED] = "closed",
[BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
[BLKIF_INTERFACE_STATUS_CONNECTED] = "connected",
[BLKIF_INTERFACE_STATUS_CHANGED] = "changed",
};
#endif
#if 0
#define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
#else
#define DPRINTK(fmt, args...)
#endif
static int blkif_open(struct disk *dp);
static int blkif_close(struct disk *dp);
static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td);
static int blkif_queue_request(struct xb_softc *sc, struct xb_command *cm);
static void xb_quiesce(struct xb_softc *sc);
static void xb_strategy(struct bio *bp);
// In order to quiesce the device during kernel dumps, outstanding requests to
// DOM0 for disk reads/writes need to be accounted for.
static int xb_dump(void *, void *, vm_offset_t, off_t, size_t);
/* XXX move to xb_vbd.c when VBD update support is added */
#define MAX_VBDS 64
#define XBD_SECTOR_SIZE 512 /* XXX: assume for now */
#define XBD_SECTOR_SHFT 9
/*
* Translate Linux major/minor to an appropriate name and unit
* number. For HVM guests, this allows us to use the same drive names
* with blkfront as the emulated drives, easing transition slightly.
*/
static void
blkfront_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
{
static struct vdev_info {
int major;
int shift;
int base;
const char *name;
} info[] = {
{3, 6, 0, "vblk"}, /* ide0 */
{22, 6, 2, "vblk"}, /* ide1 */
{33, 6, 4, "vblk"}, /* ide2 */
{34, 6, 6, "vblk"}, /* ide3 */
{56, 6, 8, "vblk"}, /* ide4 */
{57, 6, 10, "vblk"}, /* ide5 */
{88, 6, 12, "vblk"}, /* ide6 */
{89, 6, 14, "vblk"}, /* ide7 */
{90, 6, 16, "vblk"}, /* ide8 */
{91, 6, 18, "vblk"}, /* ide9 */
{8, 4, 0, "da"}, /* scsi disk0 */
{65, 4, 16, "da"}, /* scsi disk1 */
{66, 4, 32, "da"}, /* scsi disk2 */
{67, 4, 48, "da"}, /* scsi disk3 */
{68, 4, 64, "da"}, /* scsi disk4 */
{69, 4, 80, "da"}, /* scsi disk5 */
{70, 4, 96, "da"}, /* scsi disk6 */
{71, 4, 112, "da"}, /* scsi disk7 */
{128, 4, 128, "da"}, /* scsi disk8 */
{129, 4, 144, "da"}, /* scsi disk9 */
{130, 4, 160, "da"}, /* scsi disk10 */
{131, 4, 176, "da"}, /* scsi disk11 */
{132, 4, 192, "da"}, /* scsi disk12 */
{133, 4, 208, "da"}, /* scsi disk13 */
{134, 4, 224, "da"}, /* scsi disk14 */
{135, 4, 240, "da"}, /* scsi disk15 */
{202, 4, 0, "xbd"}, /* xbd */
{0, 0, 0, NULL},
};
int major = vdevice >> 8;
int minor = vdevice & 0xff;
int i;
if (vdevice & (1 << 28)) {
*unit = (vdevice & ((1 << 28) - 1)) >> 8;
*name = "xbd";
return;
}
for (i = 0; info[i].major; i++) {
if (info[i].major == major) {
*unit = info[i].base + (minor >> info[i].shift);
*name = info[i].name;
return;
}
}
*unit = minor >> 4;
*name = "xbd";
}
struct disk *disk_alloc(void)
{
return (disk *)malloc(sizeof(struct disk) , M_WHATEVER, 0);
}
int
xlvbd_add(struct xb_softc *sc, blkif_sector_t sectors,
int vdevice, uint16_t vdisk_info, unsigned long sector_size)
{
int unit, error = 0;
const char *name;
blkfront_vdevice_to_unit(vdevice, &unit, &name);
sc->xb_unit = unit;
if (strcmp(name, "xbd"))
device_printf(sc->xb_dev, " attaching as %s%d\n", name, unit);
sc->xb_disk = disk_alloc();
sc->xb_disk->d_unit = sc->xb_unit;
sc->xb_disk->d_open = blkif_open;
sc->xb_disk->d_close = blkif_close;
sc->xb_disk->d_ioctl = blkif_ioctl;
sc->xb_disk->d_strategy = xb_strategy;
sc->xb_disk->d_dump = xb_dump;
sc->xb_disk->d_name = name;
sc->xb_disk->d_drv1 = sc;
sc->xb_disk->d_sectorsize = sector_size;
sc->xb_disk->d_mediasize = sectors * sector_size;
sc->xb_disk->d_maxsize = sc->max_request_size;
sc->xb_disk->d_flags = 0;
disk_create(sc->xb_disk, DISK_VERSION);
return error;
}
/************************ end VBD support *****************/
/*
* Read/write routine for a buffer. Finds the proper unit, place it on
* the sortq and kick the controller.
*/
static void
xb_strategy(struct bio *bp)
{
struct xb_softc *sc = (xb_softc *)bp->bio_dev->softc;
bf_softc *xsc = reinterpret_cast<bf_softc *>(sc);
/* bogus disk? */
if (sc == NULL) {
bp->bio_error = EINVAL;
bp->bio_resid = bp->bio_bcount;
biodone(bp, false);
return;
}
if ((bp->bio_cmd == BIO_FLUSH) &&
!((sc->xb_flags & XB_BARRIER) || (sc->xb_flags & XB_FLUSH))) {
bp->bio_error = EOPNOTSUPP;
bp->bio_resid = bp->bio_bcount;
biodone(bp, false);
return;
}
/*
* Place it in the queue of disk activities for this disk
*/
mutex_lock(&xsc->xb_io_lock);
xb_enqueue_bio(sc, bp);
xb_startio(sc);
mutex_unlock(&xsc->xb_io_lock);
return;
}
static void
xb_bio_complete(struct xb_softc *sc, struct xb_command *cm)
{
struct bio *bp;
bp = cm->bp;
if ( unlikely(cm->status != BLKIF_RSP_OKAY) ) {
disk_err(bp, "disk error" , -1, 0);
printf(" status: %x\n", cm->status);
bp->bio_flags |= BIO_ERROR;
}
if (bp->bio_flags & BIO_ERROR)
bp->bio_error = EIO;
else
bp->bio_resid = 0;
xb_free_command(cm);
biodone(bp, !(bp->bio_flags & BIO_ERROR));
}
// Quiesce the disk writes for a dump file before allowing the next buffer.
static void
xb_quiesce(struct xb_softc *sc)
{
int mtd;
// While there are outstanding requests
while (!TAILQ_EMPTY(&sc->cm_busy)) {
RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, mtd);
if (mtd) {
/* Recieved request completions, update queue. */
blkif_int(sc);
}
if (!TAILQ_EMPTY(&sc->cm_busy)) {
/*
* Still pending requests, wait for the disk i/o
* to complete.
*/
HYPERVISOR_yield();
}
}
}
/* Kernel dump function for a paravirtualized disk device */
static void
xb_dump_complete(struct xb_command *cm)
{
xb_enqueue_complete(cm);
}
static int
xb_dump(void *arg, void *vvirtual, vm_offset_t physical, off_t offset,
size_t length)
{
struct disk *dp = (disk *)arg;
struct xb_softc *sc = (struct xb_softc *) dp->d_drv1;
struct xb_command *cm;
size_t chunk;
int sbp;
int rc = 0;
bf_softc *xsc = reinterpret_cast<bf_softc *>(sc);
if (length <= 0)
return (rc);
xb_quiesce(sc); /* All quiet on the western front. */
/*
* If this lock is held, then this module is failing, and a
* successful kernel dump is highly unlikely anyway.
*/
mutex_lock(&xsc->xb_io_lock);
/* Split the 64KB block as needed */
for (sbp=0; length > 0; sbp++) {
cm = xb_dequeue_free(sc);
if (cm == NULL) {
mutex_unlock(&xsc->xb_io_lock);
device_printf(sc->xb_dev, "dump: no more commands?\n");
return (EBUSY);
}
if (gnttab_alloc_grant_references(sc->max_request_segments,
&cm->gref_head) != 0) {
xb_free_command(cm);
mutex_unlock(&xsc->xb_io_lock);
device_printf(sc->xb_dev, "no more grant allocs?\n");
return (EBUSY);
}
chunk = length > sc->max_request_size
? sc->max_request_size : length;
cm->data = vvirtual;
cm->datalen = chunk;
cm->operation = BLKIF_OP_WRITE;
cm->sector_number = offset / dp->d_sectorsize;
cm->cm_complete = xb_dump_complete;
xb_enqueue_ready(cm);
length -= chunk;
offset += chunk;
vvirtual = (char *) vvirtual + chunk;
}
/* Tell DOM0 to do the I/O */
xb_startio(sc);
mutex_unlock(&xsc->xb_io_lock);
/* Poll for the completion. */
xb_quiesce(sc); /* All quite on the eastern front */
/* If there were any errors, bail out... */
while ((cm = xb_dequeue_complete(sc)) != NULL) {
if (cm->status != BLKIF_RSP_OKAY) {
device_printf(sc->xb_dev,
"Dump I/O failed at sector %jd\n",
cm->sector_number);
rc = EIO;
}
xb_free_command(cm);
}
return (rc);
}
static int
blkfront_probe(device_t dev)
{
if (!strcmp(xenbus_get_type(dev), "vbd")) {
device_set_desc(dev, "Virtual Block Device");
device_quiet(dev);
return (0);
}
return (ENXIO);
}
static void
xb_setup_sysctl(struct xb_softc *xb)
{
#if 0
struct sysctl_ctx_list *sysctl_ctx = NULL;
struct sysctl_oid *sysctl_tree = NULL;
sysctl_ctx = device_get_sysctl_ctx(xb->xb_dev);
if (sysctl_ctx == NULL)
return;
sysctl_tree = device_get_sysctl_tree(xb->xb_dev);
if (sysctl_tree == NULL)
return;
SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
"max_requests", CTLFLAG_RD, &xb->max_requests, -1,
"maximum outstanding requests (negotiated)");
SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
"max_request_segments", CTLFLAG_RD,
&xb->max_request_segments, 0,
"maximum number of pages per requests (negotiated)");
SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
"max_request_size", CTLFLAG_RD,
&xb->max_request_size, 0,
"maximum size in bytes of a request (negotiated)");
SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
"ring_pages", CTLFLAG_RD,
&xb->ring_pages, 0,
"communication channel pages (negotiated)");
#endif
}
static int xs_id_by_name(const char* name)
{
const char *id_str = strrchr(name, '/');
id_str = id_str ? id_str + 1 : name;
return atoi(id_str);
}
/*
* Setup supplies the backend dir, virtual device. We place an event
* channel and shared frame entries. We watch backend to wait if it's
* ok.
*/
static int
blkfront_attach(device_t dev)
{
struct xb_softc *sc;
const char *name, *node_name;
uint32_t vdevice;
int error;
int i;
int unit;
node_name = xenbus_get_node(dev);
/* FIXME: Use dynamic device id if this is not set. */
error = xs_scanf(XST_NIL, node_name,
"virtual-device", NULL, "%" PRIu32, &vdevice);
if (error) {
/* On some Xen versions there is no virtual-device property,
* last part of device name should be used instead
*/
vdevice = xs_id_by_name(node_name);
}
blkfront_vdevice_to_unit(vdevice, &unit, &name);
if (!strcmp(name, "vblk"))
device_set_unit(dev, unit);
sc = (xb_softc *)device_get_softc(dev);
xb_initq_free(sc);
xb_initq_busy(sc);
xb_initq_ready(sc);
xb_initq_complete(sc);
xb_initq_bio(sc);
for (i = 0; i < XBF_MAX_RING_PAGES; i++)
sc->ring_ref[i] = GRANT_INVALID_REF;
sc->xb_dev = dev;
sc->xb_disk = NULL;
sc->vdevice = vdevice;
sc->connected = BLKIF_STATE_DISCONNECTED;
xb_setup_sysctl(sc);
/* Wait for backend device to publish its protocol capabilities. */
xenbus_set_state(dev, XenbusStateInitialising);
return (0);
}
static int
blkfront_suspend(device_t dev)
{
struct xb_softc *sc = (xb_softc *)device_get_softc(dev);
bf_softc *xsc = reinterpret_cast<bf_softc *>(sc);
int retval;
int saved_state;
/* Prevent new requests being issued until we fix things up. */
mutex_lock(&xsc->xb_io_lock);
saved_state = sc->connected;
sc->connected = BLKIF_STATE_SUSPENDED;
/* Wait for outstanding I/O to drain. */
retval = 0;
while (TAILQ_EMPTY(&sc->cm_busy) == 0) {
if (msleep(&sc->cm_busy, &xsc->xb_io_lock,
PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
retval = EBUSY;
break;
}
}
mutex_unlock(&xsc->xb_io_lock);
if (retval != 0)
sc->connected = saved_state;
return (retval);
}
static int
blkfront_resume(device_t dev)
{
struct xb_softc *sc = (xb_softc *)device_get_softc(dev);
DPRINTK("blkfront_resume: %s\n", xenbus_get_node(dev));
blkif_free(sc);
blkfront_initialize(sc);
return (0);
}
static unsigned int
blkfront_read_feature(device_t dev, char *name)
{
unsigned int res = 0;
auto err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
name, "%u", &res, NULL);
return err ? 0 : res;
}
static inline unsigned int
blkfront_check_feature(device_t dev, char *name, int flag)
{
return blkfront_read_feature(dev, name) ? flag : 0;
}
static int
blkif_claim_gref(grant_ref_t *gref_head,
device_t dev,
bus_addr_t addr,
int readonly)
{
auto ref = gnttab_claim_grant_reference(gref_head);
/*
* GNTTAB_LIST_END == 0xffffffff, but it is private
* to gnttab.c.
*/
KASSERT(ref != ~0, ("grant_reference failed"));
gnttab_grant_foreign_access_ref(
ref,
xenbus_get_otherend_id(dev),
addr >> PAGE_SHIFT,
readonly);
return ref;
}
class indirect_page
{
public:
indirect_page()
: _va(memory::alloc_page()) {}
~indirect_page() { memory::free_page(_va); }
static constexpr unsigned capacity()
{ return memory::page_size / sizeof(blkif_segment_indirect_t); }
grant_ref_t alloc_gref(device_t dev);
void free_gref();
void set_segment(int seg_num, grant_ref_t gref,
u8 first_sect, u8 last_seq);
private:
void* _va;
grant_ref_t _gref_list;
grant_ref_t _gref;
};
class blkfront_indirect_descriptor
{
public:
blkfront_indirect_descriptor(int max_segs)
: _pages(pages_required(max_segs))
, _max_segs(max_segs)
{}
static constexpr unsigned total_capacity()
{
return indirect_page::capacity()
* BLKIF_MAX_INDIRECT_PAGES_PER_HEADER_BLOCK;
}
void attach(blkif_request_indirect_t *descr)
{
_descr = descr;
_seg_number = 0;
}
void configure(uint64_t id,
uint8_t operation,
blkif_sector_t sector_number,
blkif_vdev_t handle);
void add_segment(grant_ref_t gref, u8 first_sect, u8 last_seq);
void map(device_t dev);
void unmap();
bool has_space() { return _seg_number < _max_segs; }
private:
static constexpr unsigned pages_required(unsigned max_segs)
{
return (max_segs + indirect_page::capacity() - 1) /
indirect_page::capacity();
}
std::vector<indirect_page> _pages;
int _max_segs;
int _seg_number = 0;
blkif_request_indirect_t *_descr = nullptr;
};
class blkfront_indirect_descriptors
{
public:
blkfront_indirect_descriptors(device_t &dev, uint32_t max_requests);
~blkfront_indirect_descriptors();
blkfront_indirect_descriptor *get()
{
auto descr = _descriptors.top();
_descriptors.pop();
return descr;
}
void put(blkfront_indirect_descriptor *descr)
{
_descriptors.push(descr);
}
unsigned descriptor_capacity()
{
return std::min(_max_segs,
blkfront_indirect_descriptor::total_capacity());
}
bool empty() { return _descriptors.empty(); }
private:
std::stack<blkfront_indirect_descriptor *> _descriptors;
unsigned _max_segs;
};
class blkfront_head_descr_base
{
public:
void attach(blkif_request_t *descr)
{
_descr = descr;
_curr_seg = &descr->seg[0];
}
void configure(uint64_t id,
uint8_t operation,
blkif_sector_t sector_number,
blkif_vdev_t handle,
uint8_t nr_segments);
static constexpr unsigned capacity()
{ return BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK; }
bool has_space()
{
return _curr_seg != &_descr->seg[BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK];
}
protected:
blkif_request_t *_descr = nullptr;
blkif_request_segment_t *_curr_seg = nullptr;
};
class blkfront_segment_descr_base
{
public:
void attach(blkif_segment_block_t *descr)
{
_descr = descr;
_curr_seg = &_descr->seg[0];
}
bool has_space()
{
return _curr_seg != &_descr->seg[BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK];
}
protected:
blkif_segment_block_t *_descr = nullptr;
blkif_request_segment_t *_curr_seg = nullptr;
};
template<typename baseT>
class blkfront_descriptor : public baseT
{
public:
void add_segment(grant_ref_t gref, u8 first_sect, u8 last_seq)
{
assert(baseT::has_space());
baseT::_curr_seg->gref = gref;
baseT::_curr_seg->first_sect = first_sect;
baseT::_curr_seg->last_sect = last_seq;
baseT::_curr_seg++;
}
};
typedef blkfront_descriptor<blkfront_head_descr_base> blkfront_head_descr;
typedef blkfront_descriptor<blkfront_segment_descr_base> blkfront_segment_descr;
void indirect_page::set_segment(int seg_num, grant_ref_t gref,
u8 first_sect, u8 last_seq)
{
assert(seg_num < capacity());
auto seg = static_cast<blkif_segment_indirect_t *>(_va) + seg_num;
seg->gref = gref;
seg->first_sect = first_sect;
seg->last_sect = last_seq;
}
grant_ref_t indirect_page::alloc_gref(device_t dev)
{
if (gnttab_alloc_grant_references(1, &_gref_list) != 0) {
device_printf(dev, "No memory for grant references");
abort();
}
auto pa = virt_to_phys(_va);
_gref = blkif_claim_gref(&_gref_list, dev, pa, 1);
return _gref;
}
void indirect_page::free_gref()
{
gnttab_end_foreign_access_references(1, &_gref);
gnttab_free_grant_references(_gref_list);
}
void blkfront_indirect_descriptor::configure(uint64_t id,
uint8_t operation,
blkif_sector_t sector_number,
blkif_vdev_t handle)
{
assert(operation == BLKIF_OP_READ ||
operation == BLKIF_OP_WRITE ||
operation == BLKIF_OP_WRITE_BARRIER);
_descr->operation = BLKIF_OP_INDIRECT;
_descr->indirect_op = operation;
_descr->id = id;
_descr->sector_number = sector_number;
_descr->handle = handle;
}
void blkfront_indirect_descriptor::add_segment(grant_ref_t gref,
u8 first_sect,
u8 last_seq)
{
assert(has_space());
auto page_num = _seg_number / indirect_page::capacity();
auto in_page_num = _seg_number % indirect_page::capacity();
_pages[page_num].set_segment(in_page_num, gref, first_sect, last_seq);
_seg_number++;
}
void blkfront_indirect_descriptor::map(device_t dev)
{
for(auto i = 0; i < pages_required(_seg_number); i++)
{
_descr->indirect_grefs[i] = _pages[i].alloc_gref(dev);
}
_descr->nr_segments = _seg_number;
}
void blkfront_indirect_descriptor::unmap()
{
for(auto i = 0; i < pages_required(_seg_number); i++)
{
_pages[i].free_gref();
}
}
blkfront_indirect_descriptors::blkfront_indirect_descriptors(device_t &dev,
uint32_t max_requests)
{
_max_segs = blkfront_read_feature(dev, "feature-max-indirect-segments");
_max_segs = std::min(_max_segs, BLKIF_MAX_INDIRECT_SEGMENTS);
if (_max_segs != 0) {
for(auto i = 0; i < max_requests; i++) {
_descriptors.emplace(new blkfront_indirect_descriptor(_max_segs));
}
}
}
blkfront_indirect_descriptors::~blkfront_indirect_descriptors()
{
while (!_descriptors.empty())
{
delete _descriptors.top();
_descriptors.pop();
}
}
void blkfront_head_descr_base::configure(uint64_t id,
uint8_t operation,
blkif_sector_t sector_number,
blkif_vdev_t handle,
uint8_t nr_segments)
{
_descr->id = id;
_descr->operation = operation;
_descr->sector_number = sector_number;
_descr->handle = handle;
_descr->nr_segments = nr_segments;
}
static void
blkfront_alloc_commands(struct xb_softc* sc)
{
bf_softc *xsc = reinterpret_cast<bf_softc *>(sc);
/* Allocate datastructures based on negotiated values. */
auto error = bus_dma_tag_create(bus_get_dma_tag(sc->xb_dev), /* parent */
512, PAGE_SIZE, /* algnmnt, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
sc->max_request_size,
sc->max_request_segments,
PAGE_SIZE, /* maxsegsize */
BUS_DMA_ALLOCNOW, /* flags */
busdma_lock_mutex, /* lockfunc */
&xsc->xb_io_lock, /* lockarg */
&sc->xb_io_dmat);
if (error != 0) {
xenbus_dev_fatal(sc->xb_dev, error,
"Cannot allocate parent DMA tag\n");
return;
}
/* Per-transaction data allocation. */
sc->shadow = (xb_command *)malloc(sizeof(*sc->shadow) * sc->max_requests,
M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
for (uint32_t i = 0; i < sc->max_requests; i++) {
struct xb_command *cm;
cm = &sc->shadow[i];
cm->sg_refs = (grant_ref_t *)malloc(sizeof(grant_ref_t)
* sc->max_request_segments,
M_XENBLOCKFRONT, M_NOWAIT);
cm->id = i;
cm->cm_sc = sc;
if (bus_dmamap_create(sc->xb_io_dmat, 0, &cm->map) != 0)
break;
xb_free_command(cm);
}
}
static void
blkfront_initialize(struct xb_softc *sc)
{
const char *otherend_path;
const char *node_path;
uint32_t max_ring_page_order;
int error;
if (xenbus_get_state(sc->xb_dev) != XenbusStateInitialising) {
/* Initialization has already been performed. */
return;
}
/*
* Protocol defaults valid even if negotiation for a
* setting fails.
*/
max_ring_page_order = 0;
sc->ring_pages = 1;
sc->max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
sc->max_request_size = XBF_SEGS_TO_SIZE(sc->max_request_segments);
sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments);
/*
* Protocol negotiation.
*
* \note xs_gather() returns on the first encountered error, so
* we must use independant calls in order to guarantee
* we don't miss information in a sparsly populated back-end
* tree.
*
* \note xs_scanf() does not update variables for unmatched
* fields.
*/
otherend_path = xenbus_get_otherend_path(sc->xb_dev);
node_path = xenbus_get_node(sc->xb_dev);
/* Support both backend schemes for relaying ring page limits. */
(void)xs_scanf(XST_NIL, otherend_path,
"max-ring-page-order", NULL, "%" PRIu32,
&max_ring_page_order);
sc->ring_pages = 1 << max_ring_page_order;
(void)xs_scanf(XST_NIL, otherend_path,
"max-ring-pages", NULL, "%" PRIu32,
&sc->ring_pages);
if (sc->ring_pages < 1)
sc->ring_pages = 1;
sc->max_requests = BLKIF_MAX_RING_REQUESTS(sc->ring_pages * PAGE_SIZE);
(void)xs_scanf(XST_NIL, otherend_path,
"max-requests", NULL, "%" PRIu32,
&sc->max_requests);
(void)xs_scanf(XST_NIL, otherend_path,
"max-request-segments", NULL, "%" PRIu32,
&sc->max_request_segments);
(void)xs_scanf(XST_NIL, otherend_path,
"max-request-size", NULL, "%" PRIu32,
&sc->max_request_size);
if (sc->ring_pages > XBF_MAX_RING_PAGES) {
device_printf(sc->xb_dev, "Back-end specified ring-pages of "
"%u limited to front-end limit of %zu.\n",
sc->ring_pages, XBF_MAX_RING_PAGES);
sc->ring_pages = XBF_MAX_RING_PAGES;
}