forked from illumos/illumos-gate
/
blkdev.c
1752 lines (1510 loc) · 37.6 KB
/
blkdev.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2011, 2012 Nexenta Systems, Inc. All rights reserved.
* Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
*/
#include <sys/types.h>
#include <sys/ksynch.h>
#include <sys/kmem.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/aio_req.h>
#include <sys/cred.h>
#include <sys/modctl.h>
#include <sys/cmlb.h>
#include <sys/conf.h>
#include <sys/devops.h>
#include <sys/list.h>
#include <sys/sysmacros.h>
#include <sys/dkio.h>
#include <sys/vtoc.h>
#include <sys/scsi/scsi.h> /* for DTYPE_DIRECT */
#include <sys/kstat.h>
#include <sys/fs/dv_node.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/note.h>
#include <sys/blkdev.h>
#define BD_MAXPART 64
#define BDINST(dev) (getminor(dev) / BD_MAXPART)
#define BDPART(dev) (getminor(dev) % BD_MAXPART)
typedef struct bd bd_t;
typedef struct bd_xfer_impl bd_xfer_impl_t;
struct bd {
void *d_private;
dev_info_t *d_dip;
kmutex_t d_ocmutex;
kmutex_t d_iomutex;
kmutex_t d_statemutex;
kcondvar_t d_statecv;
enum dkio_state d_state;
cmlb_handle_t d_cmlbh;
unsigned d_open_lyr[BD_MAXPART]; /* open count */
uint64_t d_open_excl; /* bit mask indexed by partition */
uint64_t d_open_reg[OTYPCNT]; /* bit mask */
uint32_t d_qsize;
uint32_t d_qactive;
uint32_t d_maxxfer;
uint32_t d_blkshift;
uint64_t d_numblks;
ddi_devid_t d_devid;
kmem_cache_t *d_cache;
list_t d_runq;
list_t d_waitq;
kstat_t *d_ksp;
kstat_io_t *d_kiop;
boolean_t d_rdonly;
boolean_t d_ssd;
boolean_t d_removable;
boolean_t d_hotpluggable;
boolean_t d_use_dma;
ddi_dma_attr_t d_dma;
bd_ops_t d_ops;
bd_handle_t d_handle;
};
struct bd_handle {
bd_ops_t h_ops;
ddi_dma_attr_t *h_dma;
dev_info_t *h_parent;
dev_info_t *h_child;
void *h_private;
bd_t *h_bd;
char *h_name;
char h_addr[20]; /* enough for %X,%X */
};
struct bd_xfer_impl {
bd_xfer_t i_public;
list_node_t i_linkage;
bd_t *i_bd;
buf_t *i_bp;
uint_t i_num_win;
uint_t i_cur_win;
off_t i_offset;
int (*i_func)(void *, bd_xfer_t *);
uint32_t i_blkshift;
size_t i_len;
size_t i_resid;
};
#define i_dmah i_public.x_dmah
#define i_dmac i_public.x_dmac
#define i_ndmac i_public.x_ndmac
#define i_kaddr i_public.x_kaddr
#define i_nblks i_public.x_nblks
#define i_blkno i_public.x_blkno
#define i_flags i_public.x_flags
/*
* Private prototypes.
*/
static int bd_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int bd_attach(dev_info_t *, ddi_attach_cmd_t);
static int bd_detach(dev_info_t *, ddi_detach_cmd_t);
static int bd_open(dev_t *, int, int, cred_t *);
static int bd_close(dev_t, int, int, cred_t *);
static int bd_strategy(struct buf *);
static int bd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int bd_dump(dev_t, caddr_t, daddr_t, int);
static int bd_read(dev_t, struct uio *, cred_t *);
static int bd_write(dev_t, struct uio *, cred_t *);
static int bd_aread(dev_t, struct aio_req *, cred_t *);
static int bd_awrite(dev_t, struct aio_req *, cred_t *);
static int bd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *,
caddr_t, int *);
static int bd_tg_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t, size_t,
void *);
static int bd_tg_getinfo(dev_info_t *, int, void *, void *);
static int bd_xfer_ctor(void *, void *, int);
static void bd_xfer_dtor(void *, void *);
static void bd_sched(bd_t *);
static void bd_submit(bd_t *, bd_xfer_impl_t *);
static void bd_runq_exit(bd_xfer_impl_t *, int);
static void bd_update_state(bd_t *);
static int bd_check_state(bd_t *, enum dkio_state *);
static int bd_flush_write_cache(bd_t *, struct dk_callback *);
struct cmlb_tg_ops bd_tg_ops = {
TG_DK_OPS_VERSION_1,
bd_tg_rdwr,
bd_tg_getinfo,
};
static struct cb_ops bd_cb_ops = {
bd_open, /* open */
bd_close, /* close */
bd_strategy, /* strategy */
nodev, /* print */
bd_dump, /* dump */
bd_read, /* read */
bd_write, /* write */
bd_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
bd_prop_op, /* cb_prop_op */
0, /* streamtab */
D_64BIT | D_MP, /* Driver comaptibility flag */
CB_REV, /* cb_rev */
bd_aread, /* async read */
bd_awrite /* async write */
};
struct dev_ops bd_dev_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
bd_getinfo, /* getinfo */
nulldev, /* identify */
nulldev, /* probe */
bd_attach, /* attach */
bd_detach, /* detach */
nodev, /* reset */
&bd_cb_ops, /* driver operations */
NULL, /* bus operations */
NULL, /* power */
ddi_quiesce_not_needed, /* quiesce */
};
static struct modldrv modldrv = {
&mod_driverops,
"Generic Block Device",
&bd_dev_ops,
};
static struct modlinkage modlinkage = {
MODREV_1, { &modldrv, NULL }
};
static void *bd_state;
static krwlock_t bd_lock;
int
_init(void)
{
int rv;
rv = ddi_soft_state_init(&bd_state, sizeof (struct bd), 2);
if (rv != DDI_SUCCESS) {
return (rv);
}
rw_init(&bd_lock, NULL, RW_DRIVER, NULL);
rv = mod_install(&modlinkage);
if (rv != DDI_SUCCESS) {
rw_destroy(&bd_lock);
ddi_soft_state_fini(&bd_state);
}
return (rv);
}
int
_fini(void)
{
int rv;
rv = mod_remove(&modlinkage);
if (rv == DDI_SUCCESS) {
rw_destroy(&bd_lock);
ddi_soft_state_fini(&bd_state);
}
return (rv);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
bd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
{
bd_t *bd;
minor_t inst;
_NOTE(ARGUNUSED(dip));
inst = BDINST((dev_t)arg);
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
bd = ddi_get_soft_state(bd_state, inst);
if (bd == NULL) {
return (DDI_FAILURE);
}
*resultp = (void *)bd->d_dip;
break;
case DDI_INFO_DEVT2INSTANCE:
*resultp = (void *)(intptr_t)inst;
break;
default:
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
bd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int inst;
bd_handle_t hdl;
bd_t *bd;
bd_drive_t drive;
int rv;
char name[16];
char kcache[32];
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
/* We don't do anything native for suspend/resume */
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
inst = ddi_get_instance(dip);
hdl = ddi_get_parent_data(dip);
(void) snprintf(name, sizeof (name), "%s%d",
ddi_driver_name(dip), ddi_get_instance(dip));
(void) snprintf(kcache, sizeof (kcache), "%s_xfer", name);
if (hdl == NULL) {
cmn_err(CE_WARN, "%s: missing parent data!", name);
return (DDI_FAILURE);
}
if (ddi_soft_state_zalloc(bd_state, inst) != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s: unable to zalloc soft state!", name);
return (DDI_FAILURE);
}
bd = ddi_get_soft_state(bd_state, inst);
if (hdl->h_dma) {
bd->d_dma = *(hdl->h_dma);
bd->d_dma.dma_attr_granular =
max(DEV_BSIZE, bd->d_dma.dma_attr_granular);
bd->d_use_dma = B_TRUE;
if (bd->d_maxxfer &&
(bd->d_maxxfer != bd->d_dma.dma_attr_maxxfer)) {
cmn_err(CE_WARN,
"%s: inconsistent maximum transfer size!",
name);
/* We force it */
bd->d_maxxfer = bd->d_dma.dma_attr_maxxfer;
} else {
bd->d_maxxfer = bd->d_dma.dma_attr_maxxfer;
}
} else {
bd->d_use_dma = B_FALSE;
if (bd->d_maxxfer == 0) {
bd->d_maxxfer = 1024 * 1024;
}
}
bd->d_ops = hdl->h_ops;
bd->d_private = hdl->h_private;
bd->d_blkshift = 9; /* 512 bytes, to start */
if (bd->d_maxxfer % DEV_BSIZE) {
cmn_err(CE_WARN, "%s: maximum transfer misaligned!", name);
bd->d_maxxfer &= ~(DEV_BSIZE - 1);
}
if (bd->d_maxxfer < DEV_BSIZE) {
cmn_err(CE_WARN, "%s: maximum transfer size too small!", name);
ddi_soft_state_free(bd_state, inst);
return (DDI_FAILURE);
}
bd->d_dip = dip;
bd->d_handle = hdl;
hdl->h_bd = bd;
ddi_set_driver_private(dip, bd);
mutex_init(&bd->d_iomutex, NULL, MUTEX_DRIVER, NULL);
mutex_init(&bd->d_ocmutex, NULL, MUTEX_DRIVER, NULL);
mutex_init(&bd->d_statemutex, NULL, MUTEX_DRIVER, NULL);
cv_init(&bd->d_statecv, NULL, CV_DRIVER, NULL);
list_create(&bd->d_waitq, sizeof (bd_xfer_impl_t),
offsetof(struct bd_xfer_impl, i_linkage));
list_create(&bd->d_runq, sizeof (bd_xfer_impl_t),
offsetof(struct bd_xfer_impl, i_linkage));
bd->d_cache = kmem_cache_create(kcache, sizeof (bd_xfer_impl_t), 8,
bd_xfer_ctor, bd_xfer_dtor, NULL, bd, NULL, 0);
bd->d_ksp = kstat_create(ddi_driver_name(dip), inst, NULL, "disk",
KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
if (bd->d_ksp != NULL) {
bd->d_ksp->ks_lock = &bd->d_iomutex;
kstat_install(bd->d_ksp);
bd->d_kiop = bd->d_ksp->ks_data;
} else {
/*
* Even if we cannot create the kstat, we create a
* scratch kstat. The reason for this is to ensure
* that we can update the kstat all of the time,
* without adding an extra branch instruction.
*/
bd->d_kiop = kmem_zalloc(sizeof (kstat_io_t), KM_SLEEP);
}
cmlb_alloc_handle(&bd->d_cmlbh);
bd->d_state = DKIO_NONE;
bzero(&drive, sizeof (drive));
bd->d_ops.o_drive_info(bd->d_private, &drive);
bd->d_qsize = drive.d_qsize;
bd->d_removable = drive.d_removable;
bd->d_hotpluggable = drive.d_hotpluggable;
if (drive.d_maxxfer && drive.d_maxxfer < bd->d_maxxfer)
bd->d_maxxfer = drive.d_maxxfer;
rv = cmlb_attach(dip, &bd_tg_ops, DTYPE_DIRECT,
bd->d_removable, bd->d_hotpluggable,
drive.d_lun >= 0 ? DDI_NT_BLOCK_CHAN : DDI_NT_BLOCK,
CMLB_FAKE_LABEL_ONE_PARTITION, bd->d_cmlbh, 0);
if (rv != 0) {
cmlb_free_handle(&bd->d_cmlbh);
kmem_cache_destroy(bd->d_cache);
mutex_destroy(&bd->d_iomutex);
mutex_destroy(&bd->d_ocmutex);
mutex_destroy(&bd->d_statemutex);
cv_destroy(&bd->d_statecv);
list_destroy(&bd->d_waitq);
list_destroy(&bd->d_runq);
if (bd->d_ksp != NULL) {
kstat_delete(bd->d_ksp);
bd->d_ksp = NULL;
} else {
kmem_free(bd->d_kiop, sizeof (kstat_io_t));
}
ddi_soft_state_free(bd_state, inst);
return (DDI_FAILURE);
}
if (bd->d_ops.o_devid_init != NULL) {
rv = bd->d_ops.o_devid_init(bd->d_private, dip, &bd->d_devid);
if (rv == DDI_SUCCESS) {
if (ddi_devid_register(dip, bd->d_devid) !=
DDI_SUCCESS) {
cmn_err(CE_WARN,
"%s: unable to register devid", name);
}
}
}
/*
* Add a zero-length attribute to tell the world we support
* kernel ioctls (for layered drivers). Also set up properties
* used by HAL to identify removable media.
*/
(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
DDI_KERNEL_IOCTL, NULL, 0);
if (bd->d_removable) {
(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
"removable-media", NULL, 0);
}
if (bd->d_hotpluggable) {
(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
"hotpluggable", NULL, 0);
}
ddi_report_dev(dip);
return (DDI_SUCCESS);
}
static int
bd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
bd_t *bd;
bd = ddi_get_driver_private(dip);
switch (cmd) {
case DDI_DETACH:
break;
case DDI_SUSPEND:
/* We don't suspend, but our parent does */
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
if (bd->d_ksp != NULL) {
kstat_delete(bd->d_ksp);
bd->d_ksp = NULL;
} else {
kmem_free(bd->d_kiop, sizeof (kstat_io_t));
}
cmlb_detach(bd->d_cmlbh, 0);
cmlb_free_handle(&bd->d_cmlbh);
if (bd->d_devid)
ddi_devid_free(bd->d_devid);
kmem_cache_destroy(bd->d_cache);
mutex_destroy(&bd->d_iomutex);
mutex_destroy(&bd->d_ocmutex);
mutex_destroy(&bd->d_statemutex);
cv_destroy(&bd->d_statecv);
list_destroy(&bd->d_waitq);
list_destroy(&bd->d_runq);
ddi_soft_state_free(bd_state, ddi_get_instance(dip));
return (DDI_SUCCESS);
}
static int
bd_xfer_ctor(void *buf, void *arg, int kmflag)
{
bd_xfer_impl_t *xi;
bd_t *bd = arg;
int (*dcb)(caddr_t);
if (kmflag == KM_SLEEP) {
dcb = DDI_DMA_SLEEP;
} else {
dcb = DDI_DMA_DONTWAIT;
}
xi = buf;
bzero(xi, sizeof (*xi));
xi->i_bd = bd;
if (bd->d_use_dma) {
if (ddi_dma_alloc_handle(bd->d_dip, &bd->d_dma, dcb, NULL,
&xi->i_dmah) != DDI_SUCCESS) {
return (-1);
}
}
return (0);
}
static void
bd_xfer_dtor(void *buf, void *arg)
{
bd_xfer_impl_t *xi = buf;
_NOTE(ARGUNUSED(arg));
if (xi->i_dmah)
ddi_dma_free_handle(&xi->i_dmah);
xi->i_dmah = NULL;
}
static bd_xfer_impl_t *
bd_xfer_alloc(bd_t *bd, struct buf *bp, int (*func)(void *, bd_xfer_t *),
int kmflag)
{
bd_xfer_impl_t *xi;
int rv;
int status;
unsigned dir;
int (*cb)(caddr_t);
size_t len;
uint32_t shift;
if (kmflag == KM_SLEEP) {
cb = DDI_DMA_SLEEP;
} else {
cb = DDI_DMA_DONTWAIT;
}
xi = kmem_cache_alloc(bd->d_cache, kmflag);
if (xi == NULL) {
bioerror(bp, ENOMEM);
return (NULL);
}
ASSERT(bp);
xi->i_bp = bp;
xi->i_func = func;
xi->i_blkno = bp->b_lblkno;
if (bp->b_bcount == 0) {
xi->i_len = 0;
xi->i_nblks = 0;
xi->i_kaddr = NULL;
xi->i_resid = 0;
xi->i_num_win = 0;
goto done;
}
if (bp->b_flags & B_READ) {
dir = DDI_DMA_READ;
xi->i_func = bd->d_ops.o_read;
} else {
dir = DDI_DMA_WRITE;
xi->i_func = bd->d_ops.o_write;
}
shift = bd->d_blkshift;
xi->i_blkshift = shift;
if (!bd->d_use_dma) {
bp_mapin(bp);
rv = 0;
xi->i_offset = 0;
xi->i_num_win =
(bp->b_bcount + (bd->d_maxxfer - 1)) / bd->d_maxxfer;
xi->i_cur_win = 0;
xi->i_len = min(bp->b_bcount, bd->d_maxxfer);
xi->i_nblks = xi->i_len >> shift;
xi->i_kaddr = bp->b_un.b_addr;
xi->i_resid = bp->b_bcount;
} else {
/*
* We have to use consistent DMA if the address is misaligned.
*/
if (((bp->b_flags & (B_PAGEIO | B_REMAPPED)) != B_PAGEIO) &&
((uintptr_t)bp->b_un.b_addr & 0x7)) {
dir |= DDI_DMA_CONSISTENT | DDI_DMA_PARTIAL;
} else {
dir |= DDI_DMA_STREAMING | DDI_DMA_PARTIAL;
}
status = ddi_dma_buf_bind_handle(xi->i_dmah, bp, dir, cb,
NULL, &xi->i_dmac, &xi->i_ndmac);
switch (status) {
case DDI_DMA_MAPPED:
xi->i_num_win = 1;
xi->i_cur_win = 0;
xi->i_offset = 0;
xi->i_len = bp->b_bcount;
xi->i_nblks = xi->i_len >> shift;
xi->i_resid = bp->b_bcount;
rv = 0;
break;
case DDI_DMA_PARTIAL_MAP:
xi->i_cur_win = 0;
if ((ddi_dma_numwin(xi->i_dmah, &xi->i_num_win) !=
DDI_SUCCESS) ||
(ddi_dma_getwin(xi->i_dmah, 0, &xi->i_offset,
&len, &xi->i_dmac, &xi->i_ndmac) !=
DDI_SUCCESS) ||
(P2PHASE(len, shift) != 0)) {
(void) ddi_dma_unbind_handle(xi->i_dmah);
rv = EFAULT;
goto done;
}
xi->i_len = len;
xi->i_nblks = xi->i_len >> shift;
xi->i_resid = bp->b_bcount;
rv = 0;
break;
case DDI_DMA_NORESOURCES:
rv = EAGAIN;
goto done;
case DDI_DMA_TOOBIG:
rv = EINVAL;
goto done;
case DDI_DMA_NOMAPPING:
case DDI_DMA_INUSE:
default:
rv = EFAULT;
goto done;
}
}
done:
if (rv != 0) {
kmem_cache_free(bd->d_cache, xi);
bioerror(bp, rv);
return (NULL);
}
return (xi);
}
static void
bd_xfer_free(bd_xfer_impl_t *xi)
{
if (xi->i_dmah) {
(void) ddi_dma_unbind_handle(xi->i_dmah);
}
kmem_cache_free(xi->i_bd->d_cache, xi);
}
static int
bd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
{
dev_t dev = *devp;
bd_t *bd;
minor_t part;
minor_t inst;
uint64_t mask;
boolean_t ndelay;
int rv;
diskaddr_t nblks;
diskaddr_t lba;
_NOTE(ARGUNUSED(credp));
part = BDPART(dev);
inst = BDINST(dev);
if (otyp >= OTYPCNT)
return (EINVAL);
ndelay = (flag & (FNDELAY | FNONBLOCK)) ? B_TRUE : B_FALSE;
/*
* Block any DR events from changing the set of registered
* devices while we function.
*/
rw_enter(&bd_lock, RW_READER);
if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
rw_exit(&bd_lock);
return (ENXIO);
}
mutex_enter(&bd->d_ocmutex);
ASSERT(part < 64);
mask = (1U << part);
bd_update_state(bd);
if (cmlb_validate(bd->d_cmlbh, 0, 0) != 0) {
/* non-blocking opens are allowed to succeed */
if (!ndelay) {
rv = ENXIO;
goto done;
}
} else if (cmlb_partinfo(bd->d_cmlbh, part, &nblks, &lba,
NULL, NULL, 0) == 0) {
/*
* We read the partinfo, verify valid ranges. If the
* partition is invalid, and we aren't blocking or
* doing a raw access, then fail. (Non-blocking and
* raw accesses can still succeed to allow a disk with
* bad partition data to opened by format and fdisk.)
*/
if ((!nblks) && ((!ndelay) || (otyp != OTYP_CHR))) {
rv = ENXIO;
goto done;
}
} else if (!ndelay) {
/*
* cmlb_partinfo failed -- invalid partition or no
* disk label.
*/
rv = ENXIO;
goto done;
}
if ((flag & FWRITE) && bd->d_rdonly) {
rv = EROFS;
goto done;
}
if ((bd->d_open_excl) & (mask)) {
rv = EBUSY;
goto done;
}
if (flag & FEXCL) {
if (bd->d_open_lyr[part]) {
rv = EBUSY;
goto done;
}
for (int i = 0; i < OTYP_LYR; i++) {
if (bd->d_open_reg[i] & mask) {
rv = EBUSY;
goto done;
}
}
}
if (otyp == OTYP_LYR) {
bd->d_open_lyr[part]++;
} else {
bd->d_open_reg[otyp] |= mask;
}
if (flag & FEXCL) {
bd->d_open_excl |= mask;
}
rv = 0;
done:
mutex_exit(&bd->d_ocmutex);
rw_exit(&bd_lock);
return (rv);
}
static int
bd_close(dev_t dev, int flag, int otyp, cred_t *credp)
{
bd_t *bd;
minor_t inst;
minor_t part;
uint64_t mask;
boolean_t last = B_TRUE;
_NOTE(ARGUNUSED(flag));
_NOTE(ARGUNUSED(credp));
part = BDPART(dev);
inst = BDINST(dev);
ASSERT(part < 64);
mask = (1U << part);
rw_enter(&bd_lock, RW_READER);
if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
rw_exit(&bd_lock);
return (ENXIO);
}
mutex_enter(&bd->d_ocmutex);
if (bd->d_open_excl & mask) {
bd->d_open_excl &= ~mask;
}
if (otyp == OTYP_LYR) {
bd->d_open_lyr[part]--;
} else {
bd->d_open_reg[otyp] &= ~mask;
}
for (int i = 0; i < 64; i++) {
if (bd->d_open_lyr[part]) {
last = B_FALSE;
}
}
for (int i = 0; last && (i < OTYP_LYR); i++) {
if (bd->d_open_reg[i]) {
last = B_FALSE;
}
}
mutex_exit(&bd->d_ocmutex);
if (last) {
cmlb_invalidate(bd->d_cmlbh, 0);
}
rw_exit(&bd_lock);
return (0);
}
static int
bd_dump(dev_t dev, caddr_t caddr, daddr_t blkno, int nblk)
{
minor_t inst;
minor_t part;
diskaddr_t pstart;
diskaddr_t psize;
bd_t *bd;
bd_xfer_impl_t *xi;
buf_t *bp;
int rv;
rw_enter(&bd_lock, RW_READER);
part = BDPART(dev);
inst = BDINST(dev);
if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
rw_exit(&bd_lock);
return (ENXIO);
}
/*
* do cmlb, but do it synchronously unless we already have the
* partition (which we probably should.)
*/
if (cmlb_partinfo(bd->d_cmlbh, part, &psize, &pstart, NULL, NULL,
(void *)1)) {
rw_exit(&bd_lock);
return (ENXIO);
}
if ((blkno + nblk) > psize) {
rw_exit(&bd_lock);
return (EINVAL);
}
bp = getrbuf(KM_NOSLEEP);
if (bp == NULL) {
rw_exit(&bd_lock);
return (ENOMEM);
}
bp->b_bcount = nblk << bd->d_blkshift;
bp->b_resid = bp->b_bcount;
bp->b_lblkno = blkno;
bp->b_un.b_addr = caddr;
xi = bd_xfer_alloc(bd, bp, bd->d_ops.o_write, KM_NOSLEEP);
if (xi == NULL) {
rw_exit(&bd_lock);
freerbuf(bp);
return (ENOMEM);
}
xi->i_blkno = blkno + pstart;
xi->i_flags = BD_XFER_POLL;
bd_submit(bd, xi);
rw_exit(&bd_lock);
/*
* Generally, we should have run this entirely synchronously
* at this point and the biowait call should be a no-op. If
* it didn't happen this way, it's a bug in the underlying
* driver not honoring BD_XFER_POLL.
*/
(void) biowait(bp);
rv = geterror(bp);
freerbuf(bp);
return (rv);
}
void
bd_minphys(struct buf *bp)
{
minor_t inst;
bd_t *bd;
inst = BDINST(bp->b_edev);
bd = ddi_get_soft_state(bd_state, inst);
/*
* In a non-debug kernel, bd_strategy will catch !bd as
* well, and will fail nicely.
*/
ASSERT(bd);
if (bp->b_bcount > bd->d_maxxfer)
bp->b_bcount = bd->d_maxxfer;
}
static int
bd_read(dev_t dev, struct uio *uio, cred_t *credp)
{
_NOTE(ARGUNUSED(credp));
return (physio(bd_strategy, NULL, dev, B_READ, bd_minphys, uio));
}
static int
bd_write(dev_t dev, struct uio *uio, cred_t *credp)
{
_NOTE(ARGUNUSED(credp));
return (physio(bd_strategy, NULL, dev, B_WRITE, bd_minphys, uio));
}
static int
bd_aread(dev_t dev, struct aio_req *aio, cred_t *credp)
{
_NOTE(ARGUNUSED(credp));
return (aphysio(bd_strategy, anocancel, dev, B_READ, bd_minphys, aio));
}
static int
bd_awrite(dev_t dev, struct aio_req *aio, cred_t *credp)
{
_NOTE(ARGUNUSED(credp));
return (aphysio(bd_strategy, anocancel, dev, B_WRITE, bd_minphys, aio));
}
static int
bd_strategy(struct buf *bp)
{
minor_t inst;
minor_t part;
bd_t *bd;
diskaddr_t p_lba;
diskaddr_t p_nblks;
diskaddr_t b_nblks;
bd_xfer_impl_t *xi;
uint32_t shift;
int (*func)(void *, bd_xfer_t *);
part = BDPART(bp->b_edev);
inst = BDINST(bp->b_edev);
ASSERT(bp);
bp->b_resid = bp->b_bcount;
if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
bioerror(bp, ENXIO);
biodone(bp);
return (0);
}
if (cmlb_partinfo(bd->d_cmlbh, part, &p_nblks, &p_lba,
NULL, NULL, 0)) {
bioerror(bp, ENXIO);
biodone(bp);
return (0);
}
shift = bd->d_blkshift;
if ((P2PHASE(bp->b_bcount, (1U << shift)) != 0) ||
(bp->b_lblkno > p_nblks)) {
bioerror(bp, ENXIO);
biodone(bp);
return (0);
}
b_nblks = bp->b_bcount >> shift;
if ((bp->b_lblkno == p_nblks) || (bp->b_bcount == 0)) {
biodone(bp);
return (0);
}
if ((b_nblks + bp->b_lblkno) > p_nblks) {