|
root / base / usr / src / uts / common / os / dacf.c
dacf.c C 1303 lines 32.4 KB
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * DACF: device autoconfiguration support
 *
 * DACF provides a fast, lightweight policy engine for the I/O subsystem.
 * This policy engine provides a mechanism for auto-configuring and
 * auto-unconfiguring devices.
 *
 * After a device is attach(9E)ed, additional configuration may be needed in
 * order to make the device available for use by the system.  For example,
 * STREAMS modules may need to be pushed atop the driver in order to create
 * a STREAMS stack.  If the device is to be removed from the system, these
 * configuration operations need to be undone, and the device prepared for
 * detach(9E).
 *
 * It is desirable to move the implementation of such policies outside of the
 * kernel proper, since such operations are typically infrequent.  To this end,
 * DACF manages kernel modules in (module_path)/dacf directories.  These adhere
 * to the api defined in sys/dacf.h, and register sets of configuration
 * operations.  The kernel loads these modules when the operations they
 * implement are needed, and can unload them at any time thereafter.
 * Implementing configuration operations in external modules can also increase
 * code reuse.
 *
 * DACF provides a policy database which associates
 *
 *   (device descr., kernel action) --> (configuration operation, parameters)
 *
 * - Device description is matching rule, for example:
 * 	minor-nodetype="ddi_keyboard"
 * - Kernel action is a reference to a dacf kernel hook.
 *      currently supported are "post-attach" and "pre-detach"
 * - Configuration action is a reference to a module and a set of operations
 *      within the module, for example:  consconfig:kbd_config
 * - Parameters is a list of name="value" parameters to be passed to the
 *      configuration operation when invoked.
 *
 * The contents of the rules database are loaded from /etc/dacf.conf upon boot.
 *
 * DACF kernel hooks are comprised of a call into the rule-matching engine,
 * using parameters from the hook in order find a matching rule.  If one is
 * found, the framework can invoke the configuration operation immediately, or
 * defer doing so until later, by putting the rule on a 'reservation list.'
 */

#include <sys/param.h>
#include <sys/modctl.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/pathname.h>
#include <sys/ddi_impldefs.h>
#include <sys/sunddi.h>
#include <sys/autoconf.h>
#include <sys/modhash.h>
#include <sys/dacf.h>
#include <sys/dacf_impl.h>
#include <sys/systm.h>
#include <sys/varargs.h>
#include <sys/debug.h>
#include <sys/log.h>
#include <sys/fs/snode.h>

/*
 * Enumeration of the ops exported by the dacf framework.
 *
 * To add a new op to the framework, add it to this list, update dacf.h,
 * (don't miss DACF_NUM_OPIDS) and modify dacf_rule_matrix.
 *
 */
typedef struct dacf_opmap {
	const char *name;
	dacf_opid_t id;
} dacf_opmap_t;

static dacf_opmap_t dacf_ops[] = {
	{ "post-attach",	DACF_OPID_POSTATTACH		},
	{ "pre-detach",		DACF_OPID_PREDETACH		},
	{ NULL,			0				},
};

/*
 * Enumeration of the options exported by the dacf framework (currently none).
 *
 * To add a new option, add it to this array.
 */
typedef struct dacf_opt {
	const char *optname;
	uint_t optmask;
} dacf_opt_t;

static dacf_opt_t dacf_options[] = {
#ifdef DEBUG
	{ "testopt", 		1		},
	{ "testopt2", 		2		},
#endif
	{ NULL, 		0		},
};

static char kmod_name[] = "__kernel";

/*
 * Enumeration of the device specifiers exported by the dacf framework.
 *
 * To add a new devspec to the framework, add it to this list, update dacf.h,
 * (don't miss DACF_NUM_DEVSPECS), modify dacf_rule_matrix, and modify
 * dacf_match().
 */
typedef struct dacf_ds {
	const char *name;
	dacf_devspec_t id;
} dacf_ds_t;

static dacf_ds_t dacf_devspecs[] = {
	{ "minor-nodetype", 	DACF_DS_MIN_NT 		},
	{ "driver-minorname", 	DACF_DS_DRV_MNAME	},
	{ "device-path",	DACF_DS_DEV_PATH	},
	{ NULL,			DACF_DS_ERROR		},
};

mod_hash_t *posta_mntype, *posta_mname, *posta_devname;	/* post-attach */
mod_hash_t *pred_mntype, *pred_mname, *pred_devname;	/* pre-detach */

mod_hash_t *dacf_module_hash;
mod_hash_t *dacf_info_hash;

/*
 * This is the lookup table for the hash tables that dacf manages.  Given an
 * op id and devspec type, one can obtain the hash for that type of data.
 */
mod_hash_t **dacf_rule_matrix[DACF_NUM_OPIDS][DACF_NUM_DEVSPECS] = {
	{ &posta_mntype, 	&posta_mname,	&posta_devname	},
	{ &pred_mntype,		&pred_mname,	&pred_devname	},
};

kmutex_t dacf_lock;
kmutex_t dacf_module_lock;

int dacfdebug = 0;

static dacf_rule_t *dacf_rule_ctor(char *, char *, char *, dacf_opid_t,
    uint_t, dacf_arg_t *);
static mod_hash_t *dacf_get_op_hash(dacf_opid_t, dacf_devspec_t);
static void dacf_rule_val_dtor(mod_hash_val_t);
static void dacf_destroy_opsets(dacf_module_t *module);
static void dacf_opset_copy(dacf_opset_t *dst, dacf_opset_t *src);
static void dprintf(const char *, ...) __KPRINTFLIKE(1);

/*PRINTFLIKE1*/
static void
dprintf(const char *format, ...)
{
	va_list alist;
	char dp_buf[256], *dpbp;
	if (dacfdebug & DACF_DBG_MSGS) {
		va_start(alist, format);
		/*
		 * sprintf up the string that is 'dacf debug: <the message>'
		 */
		(void) sprintf(dp_buf, "dacf debug: ");
		dpbp = &(dp_buf[strlen(dp_buf)]);
		(void) vsnprintf(dpbp, sizeof (dp_buf) - strlen(dp_buf),
		    format, alist);
		printf(dp_buf);
		va_end(alist);
	}
}

/*
 * dacf_init()
 * 	initialize the dacf framework by creating the various hash tables.
 */
void
dacf_init()
{
	int i, j;
	char hbuf[40];

	mutex_enter(&dacf_lock);

	dprintf("dacf_init: creating hashmatrix\n");

#ifdef DEBUG
	/*
	 * Sanity check that DACF_NUM_DEVSPECS and the devspecs are in sync
	 */
	for (i = 0; dacf_devspecs[i].name != NULL; i++)
		continue;
	ASSERT(i == DACF_NUM_DEVSPECS);

	/*
	 * Sanity check that DACF_NUM_OPIDS and the dacf_ops are in sync
	 */
	for (i = 0; dacf_ops[i].name != NULL; i++)
		continue;
	ASSERT(i == DACF_NUM_OPIDS);
#endif

	for (i = 0; i < DACF_NUM_OPIDS; i++) {
		for (j = 0; j < DACF_NUM_DEVSPECS; j++) {
			if (dacf_rule_matrix[i][j] == NULL) {
				continue;
			}
			/*
			 * Set up a hash table with no key destructor.  The
			 * keys are carried in the rule_t, so the val_dtor
			 * will take care of the key as well.
			 */
			(void) snprintf(hbuf, sizeof (hbuf),
			    "dacf hashmatrix [%d][%d]", i, j);
			*(dacf_rule_matrix[i][j]) = mod_hash_create_extended(
			    hbuf,			/* hash name */
			    DACF_RULE_HASHSIZE,		/* # hash elems */
			    mod_hash_null_keydtor,	/* key dtor */
			    dacf_rule_val_dtor,		/* value dtor */
			    mod_hash_bystr, NULL, 	/* hash alg & data */
			    mod_hash_strkey_cmp,	/* key comparator */
			    KM_SLEEP);
		}
	}

	dprintf("dacf_init: creating module_hash\n");
	/*
	 * dacf_module_hash stores the currently registered dacf modules
	 * by name.
	 */
	dacf_module_hash = mod_hash_create_strhash("dacf module hash",
	    DACF_MODULE_HASHSIZE, mod_hash_null_valdtor);

	dprintf("dacf_init: creating info_hash\n");
	/*
	 * dacf_info_hash stores pointers to data that modules can associate
	 * on a per minornode basis.  The type of data stored is opaque to the
	 * framework-- thus there is no destructor supplied.
	 */
	dacf_info_hash = mod_hash_create_ptrhash("dacf info hash",
	    DACF_INFO_HASHSIZE, mod_hash_null_valdtor,
	    sizeof (struct ddi_minor_data));

	mutex_exit(&dacf_lock);

	/*
	 * Register the '__kernel' module.
	 *
	 * These are operations that are provided by the kernel, not by a
	 * module.  We just feed the framework a dacfsw structure; it will get
	 * marked as 'loaded' by dacf_module_register(), and will always be
	 * available.
	 */
	(void) dacf_module_register(kmod_name, &kmod_dacfsw);

	(void) read_dacf_binding_file(NULL);

	dprintf("dacf_init: dacf is ready\n");
}

/*
 * dacf_clear_rules()
 * 	clear the dacf rule database.  This is typically done in advance of
 * 	rereading the dacf binding file.
 */
void
dacf_clear_rules()
{
	int i, j;
	ASSERT(MUTEX_HELD(&dacf_lock));

	for (i = 0; i < DACF_NUM_OPIDS; i++) {
		for (j = 0; j < DACF_NUM_DEVSPECS; j++) {
			if ((dacf_rule_matrix[i][j] != NULL) &&
			    (*(dacf_rule_matrix[i][j]) != NULL)) {
				mod_hash_clear(*(dacf_rule_matrix[i][j]));
			}
		}
	}
}

/*
 * dacf_rule_insert()
 *	Create an entry in the dacf rule database.
 *	If 'module' is null, the kernel is the 'module'. (see dacf_rule_ctor()).
 */
int
dacf_rule_insert(dacf_devspec_t devspec_type, char *devspec_data,
    char *module, char *opset, dacf_opid_t opid, uint_t opts,
    dacf_arg_t *op_args)
{
	dacf_rule_t *rule;
	mod_hash_t *hash;

	ASSERT(devspec_type != DACF_DS_ERROR);
	ASSERT(devspec_data);
	ASSERT(opset);
	ASSERT(MUTEX_HELD(&dacf_lock));

	dprintf("dacf_rule_insert called: %s=\"%s\", %s:%s, %s\n",
	    dacf_devspec_to_str(devspec_type), devspec_data,
	    module ? module : "[kernel]", opset, dacf_opid_to_str(opid));

	/*
	 * Fetch the hash table associated with this op-name and devspec-type.
	 * Some ops may not support all devspec-types, since they may be
	 * meaningless, so hash may be null.
	 */
	hash = dacf_get_op_hash(opid, devspec_type);
	if (hash == NULL) {
		cmn_err(CE_WARN, "!dacf dev-spec '%s' does not support op '%s'",
		    dacf_devspec_to_str(devspec_type), dacf_opid_to_str(opid));
		return (-1);
	}

	/*
	 * Allocate a rule  and fill it in, take a hold on it.
	 */
	rule = dacf_rule_ctor(devspec_data, module, opset, opid, opts,
	    op_args);
	dacf_rule_hold(rule);

	if (mod_hash_insert(hash, (mod_hash_key_t)rule->r_devspec_data,
	    (mod_hash_val_t)rule) != 0) {
		/*
		 * We failed, so release hold.  This will cause the rule and
		 * associated data to get nuked.
		 */
		dacf_rule_rele(rule);

		cmn_err(CE_WARN, "!dacf rule %s='%s' %s:%s %s duplicates "
		    "another rule, ignored", dacf_devspec_to_str(devspec_type),
		    devspec_data, module, opset, dacf_opid_to_str(opid));
		return (-1);
	}
	return (0);
}

/*
 * dacf_rule_ctor()
 * 	Allocate and fill out entries in a dacf_rule_t.
 */
static dacf_rule_t *
dacf_rule_ctor(char *device_spec, char *module, char *opset, dacf_opid_t opid,
    uint_t opts, dacf_arg_t *op_args)
{
	dacf_rule_t *rule;
	dacf_arg_t *p;

	rule = kmem_alloc(sizeof (dacf_rule_t), KM_SLEEP);

	/*
	 * Fill in the entries
	 */
	rule->r_devspec_data = kmem_alloc(strlen(device_spec) + 1, KM_SLEEP);
	(void) strcpy(rule->r_devspec_data, device_spec);

	/*
	 * If module is 'null' we set it to __kernel, meaning that this op
	 * is implemented by the kernel.
	 */
	if (module == NULL) {
		module = kmod_name;
	}

	rule->r_module = kmem_alloc(strlen(module) + 1, KM_SLEEP);
	(void) strcpy(rule->r_module, module);

	rule->r_opset = kmem_alloc(strlen(opset) + 1, KM_SLEEP);
	(void) strcpy(rule->r_opset, opset);

	rule->r_refs = 0;	/* no refs yet */
	rule->r_opts = opts;
	rule->r_opid = opid;

	rule->r_args = NULL;
	p = op_args;
	while (p != NULL) {
		ASSERT(p->arg_name);
		ASSERT(p->arg_val);
		/*
		 * dacf_arg_insert() should always succeed, since we're copying
		 * another (already duplicate-free) list.
		 */
		(void) dacf_arg_insert(&rule->r_args, p->arg_name, p->arg_val);
		p = p->arg_next;
	}

	return (rule);
}

/*
 * dacf_rule_val_dtor()
 * 	This is the destructor for dacf_rule_t's in the rule database.  It
 * 	simply does a dacf_rule_rele() on the rule.  This function will take
 * 	care of destroying the rule if its ref count has dropped to 0.
 */
static void
dacf_rule_val_dtor(mod_hash_val_t val)
{
	ASSERT((void *)val != NULL);
	dacf_rule_rele((dacf_rule_t *)val);
}

/*
 * dacf_rule_destroy()
 * 	destroy a dacf_rule_t
 */
void
dacf_rule_destroy(dacf_rule_t *rule)
{
	ASSERT(rule->r_refs == 0);
	/*
	 * Free arguments.
	 */
	dacf_arglist_delete(&(rule->r_args));
	kmem_free(rule->r_devspec_data, strlen(rule->r_devspec_data) + 1);
	/*
	 * Module may be null for a kernel-managed op-set
	 */
	kmem_free(rule->r_module, strlen(rule->r_module) + 1);
	kmem_free(rule->r_opset, strlen(rule->r_opset) + 1);
	kmem_free(rule, sizeof (dacf_rule_t));
}

/*
 * dacf_rule_hold()
 * 	dacf rules are ref-counted.  This function increases the reference
 * 	count on an rule.
 */
void
dacf_rule_hold(dacf_rule_t *rule)
{
	ASSERT(MUTEX_HELD(&dacf_lock));

	rule->r_refs++;
}

/*
 * dacf_rule_rele()
 * 	drop the ref count on an rule, and destroy the rule if its
 * 	ref count drops to 0.
 */
void
dacf_rule_rele(dacf_rule_t *rule)
{
	ASSERT(MUTEX_HELD(&dacf_lock));
	ASSERT(rule->r_refs > 0);

	rule->r_refs--;
	if (rule->r_refs == 0) {
		dacf_rule_destroy(rule);
	}
}

/*
 * dacf_rsrv_make()
 * 	add an rule to a reservation list to be processed later.
 */
void
dacf_rsrv_make(dacf_rsrvlist_t *rsrv, dacf_rule_t *rule, void *info,
    dacf_rsrvlist_t **list)
{
	dacf_infohdl_t ihdl = info;
	ASSERT(MUTEX_HELD(&dacf_lock));
	ASSERT(info && rule && list);

	/*
	 * Bump the ref count on rule, so it won't get freed as long as it's on
	 * this reservation list.
	 */
	dacf_rule_hold(rule);

	rsrv->rsrv_rule = rule;
	rsrv->rsrv_ihdl = ihdl;
	rsrv->rsrv_result = DDI_SUCCESS;
	rsrv->rsrv_next = *list;
	*list = rsrv;

	dprintf("dacf: reservation made\n");
}

/*
 * dacf_clr_rsrvs()
 * 	clear reservation list of operations of type 'op'
 */
void
dacf_clr_rsrvs(dev_info_t *devi, dacf_opid_t op)
{
	dacf_process_rsrvs(&(DEVI(devi)->devi_dacf_tasks), op, DACF_PROC_RELE);
}

/*
 * dacf_process_rsrvs()
 * 	iterate across a locked reservation list, processing each element
 * 	which matches 'op' according to 'flags'.
 *
 * 	if DACF_PROC_INVOKE is specified, the elements that match 'op'
 * 	will have their operations invoked.  The return value from that
 * 	operation is placed in the rsrv_result field of the dacf_rsrvlist_t
 */
void
dacf_process_rsrvs(dacf_rsrvlist_t **list, dacf_opid_t op, int flags)
{
	dacf_rsrvlist_t *p, *dp;
	dacf_rsrvlist_t **prevptr;

	ASSERT(MUTEX_HELD(&dacf_lock));
	ASSERT(list);
	ASSERT(flags != 0);

	if (*list == NULL)
		return;

	dprintf("dacf_process_rsrvs: opid = %d, flags = 0x%x\n", op, flags);

	/*
	 * Walk the list, finding rules whose opid's match op, and performing
	 * the work described by 'flags'.
	 */
	prevptr = list;
	for (p = *list; p != NULL; ) {

		if (p->rsrv_rule->r_opid != op) {
			prevptr = &(p->rsrv_next);
			p = p->rsrv_next;
			continue;
		}

		if (flags & DACF_PROC_INVOKE) {
			p->rsrv_result = dacf_op_invoke(p->rsrv_rule,
			    p->rsrv_ihdl, 0);
		}

		if (flags & DACF_PROC_RELE) {
			*prevptr = p->rsrv_next;
			dp = p;
			p = p->rsrv_next;
			dacf_rule_rele(dp->rsrv_rule);
			kmem_free(dp, sizeof (dacf_rsrvlist_t));
		} else {
			prevptr = &(p->rsrv_next);
			p = p->rsrv_next;
		}
	}
}

/*
 * dacf_get_op_hash()
 * 	Given an op name, (i.e. "post-attach" or "pre-detach") and a
 * 	devspec-type, return the hash that represents that op indexed
 * 	by that devspec.
 */
static mod_hash_t *
dacf_get_op_hash(dacf_opid_t op, dacf_devspec_t ds_type)
{
	ASSERT(op <= DACF_NUM_OPIDS && op > 0);
	ASSERT(ds_type <= DACF_NUM_DEVSPECS && ds_type > 0);

	/*
	 * dacf_rule_matrix is an array of pointers to pointers to hashes.
	 */
	if (dacf_rule_matrix[op - 1][ds_type - 1] == NULL) {
		return (NULL);
	}
	return (*(dacf_rule_matrix[op - 1][ds_type - 1]));
}

/*
 * dacf_arg_insert()
 * 	Create and insert an entry in an argument list.
 * 	Returns -1 if the argument name is a duplicate of another already
 * 	present in the hash.
 */
int
dacf_arg_insert(dacf_arg_t **list, char *name, char *val)
{
	dacf_arg_t *arg;

	/*
	 * Don't allow duplicates.
	 */
	for (arg = *list; arg != NULL; arg = arg->arg_next) {
		if (strcmp(arg->arg_name, name) == 0) {
			return (-1);
		}
	}

	arg = kmem_alloc(sizeof (dacf_arg_t), KM_SLEEP);
	arg->arg_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
	(void) strcpy(arg->arg_name, name);
	arg->arg_val = kmem_alloc(strlen(val) + 1, KM_SLEEP);
	(void) strcpy(arg->arg_val, val);

	arg->arg_next = *list;
	*list = arg;

	return (0);
}

/*
 * dacf_arglist_delete()
 * 	free all the elements of a list of dacf_arg_t's.
 */
void
dacf_arglist_delete(dacf_arg_t **list)
{
	dacf_arg_t *arg, *narg;
	arg = *list;
	while (arg != NULL) {
		narg = arg->arg_next;
		kmem_free(arg->arg_name, strlen(arg->arg_name) + 1);
		kmem_free(arg->arg_val, strlen(arg->arg_val) + 1);
		kmem_free(arg, sizeof (dacf_arg_t));
		arg = narg;
	}
	*list = NULL;
}

/*
 * dacf_match()
 * 	Match a device-spec to a rule.
 */
dacf_rule_t *
dacf_match(dacf_opid_t op, dacf_devspec_t ds, const void *match_info)
{
	dacf_rule_t *rule;

	ASSERT(MUTEX_HELD(&dacf_lock));

	if (mod_hash_find(dacf_get_op_hash(op, ds), (mod_hash_key_t)match_info,
	    (mod_hash_val_t *)&rule) == 0) {
		return (rule);
	}

	return (NULL);	/* Not Found */
}

/*
 * dacf_module_register()
 * 	register a module with the framework.  Use when a module gets loaded,
 * 	or for the kernel to register a "virtual" module (i.e. a "module"
 * 	which the kernel provides).  Makes a copy of the interface description
 * 	provided by the module.
 */
int
dacf_module_register(char *mod_name, struct dacfsw *sw)
{
	char *str;
	size_t i, nelems;
	dacf_module_t *module;
	dacf_opset_t *opsarray;

	if (sw == NULL) {
		return (EINVAL);
	}

	if (sw->dacf_rev != DACF_MODREV_1) {
		cmn_err(CE_WARN, "dacf: module '%s' exports unsupported "
		    "version %d interface, not registered\n", mod_name,
		    sw->dacf_rev);
		return (EINVAL);
	}

	/*
	 * count how many opsets are provided.
	 */
	for (nelems = 0; sw->dacf_opsets[nelems].opset_name != NULL; nelems++)
		;

	dprintf("dacf_module_register: found %lu opsets\n", nelems);

	/*
	 * Temporary: It's ok for the kernel dacf_sw to have no opsets, since
	 * we don't have any opsets to export yet (in NON-DEBUG).
	 */
	if ((nelems == 0) && (sw != &kmod_dacfsw)) {
		cmn_err(CE_WARN, "dacf module %s exports no opsets, "
		    "not registered.\n", mod_name);
		return (EINVAL);
	}

	/*
	 * Look to see if the module has been previously registered with the
	 * framework.  If so, we can fail with EBUSY.
	 */
	if (mod_hash_find(dacf_module_hash, (mod_hash_key_t)mod_name,
	    (mod_hash_val_t)&module) == 0) {
		/*
		 * See if it is loaded currently
		 */
		rw_enter(&module->dm_lock, RW_WRITER);
		if (module->dm_loaded) {
			rw_exit(&module->dm_lock);
			cmn_err(CE_WARN, "dacf module '%s' is "
			    "already registered.", mod_name);
			return (EBUSY);
		}
	} else {
		/*
		 * This is the first time we've ever seen the module; stick
		 * it into the module hash.  If that fails, we've had a
		 * race between two threads, both trying to insert the same
		 * new module.  It's safe to stick the module into the
		 * hash only partly filled in, since dm_lock protects the
		 * structure, and we've got that write-locked.
		 */
		module = kmem_zalloc(sizeof (dacf_module_t), KM_SLEEP);
		str = kmem_alloc(strlen(mod_name) + 1, KM_SLEEP);
		(void) strcpy(str, mod_name);
		rw_enter(&module->dm_lock, RW_WRITER);

		if (mod_hash_insert(dacf_module_hash, (mod_hash_key_t)str,
		    (mod_hash_val_t)module) != 0) {
			rw_exit(&module->dm_lock);
			kmem_free(str, strlen(str) + 1);
			kmem_free(module, sizeof (dacf_module_t));
			cmn_err(CE_WARN, "dacf module '%s' is "
			    "already registered.", mod_name);
			return (EBUSY);
		}
	}
	/*
	 * In either case (first time we've seen it or not), the module is
	 * not loaded, and we hold it write-locked.
	 */
	ASSERT(RW_WRITE_HELD(&module->dm_lock));

	/*
	 * Alloc array of opsets for this module.  Add one for the final
	 * NULL entry
	 */
	opsarray = kmem_zalloc(sizeof (dacf_opset_t) * (nelems + 1), KM_SLEEP);

	for (i = 0; i < nelems; i++) {
		dacf_opset_copy(&(opsarray[i]), &(sw->dacf_opsets[i]));
		ASSERT(opsarray[i].opset_name != NULL);
		ASSERT(opsarray[i].opset_ops != NULL);
	}
	opsarray[nelems].opset_name = NULL;
	opsarray[nelems].opset_ops = NULL;

	ASSERT(module->dm_opsets == NULL);	/* see dacf_destroy_opsets() */
	module->dm_opsets = opsarray;

	if (dacfdebug & DACF_DBG_MSGS) {
		dprintf("%s registered.\n", mod_name);
		for (i = 0; i < nelems; i++) {
			dprintf("registered %s\n", opsarray[i].opset_name);
		}
	}

	module->dm_loaded = 1;
	rw_exit(&module->dm_lock);

	return (0);
}

/*
 * dacf_module_unregister()
 * 	remove a module from the framework, and free framework-allocated
 * 	resources.
 */
int
dacf_module_unregister(char *mod_name)
{
	dacf_module_t *module;

	/*
	 * Can't unregister __kernel, since there is no real way to get it
	 * back-- Once it gets marked with dm_loaded == 0, the kernel will
	 * try to modload() if it is ever needed, which will fail utterly,
	 * and send op_invoke into a loop in it's modload logic
	 *
	 * If this is behavior is ever needed in the future, we can just
	 * add a flag indicating that this module is really a fake.
	 */
	ASSERT(strcmp(mod_name, kmod_name) != 0);

	dprintf("dacf_module_unregister: called for '%s'!\n", mod_name);

	/*
	 * If NOAUL_DACF is set, or we try to get a write-lock on dm_lock and
	 * that fails, return EBUSY, and fail to unregister.
	 */
	if (mod_hash_find(dacf_module_hash, (mod_hash_key_t)mod_name,
	    (mod_hash_val_t)&module) == 0) {
		if ((moddebug & MODDEBUG_NOAUL_DACF) ||
		    !rw_tryenter(&module->dm_lock, RW_WRITER)) {
			return (EBUSY);
		}
	} else {
		return (EINVAL);
	}

	ASSERT(RW_WRITE_HELD(&module->dm_lock));
	dacf_destroy_opsets(module);
	module->dm_loaded = 0;
	rw_exit(&module->dm_lock);
	return (0);
}

/*
 * dacf_destroy_opsets()
 * 	given a module, destroy all of it's associated op-sets.
 */
static void
dacf_destroy_opsets(dacf_module_t *module)
{
	dacf_opset_t *array = module->dm_opsets;
	dacf_opset_t *p;
	int i;
	size_t nelems;

	ASSERT(RW_WRITE_HELD(&module->dm_lock));
	ASSERT(module->dm_loaded == 1);

	for (i = 0; array[i].opset_name != NULL; i++) {
		p = &(array[i]);
		kmem_free(p->opset_name, strlen(p->opset_name) + 1);
		/*
		 * count nelems in opset_ops
		 */
		for (nelems = 0; ; nelems++) {
			if (p->opset_ops[nelems].op_id == DACF_OPID_END) {
				break;
			}
		}
		/*
		 * Free the array of op ptrs.
		 */
		kmem_free(p->opset_ops, sizeof (dacf_op_t) * (nelems + 1));
	}

	/*
	 * i has counted how big array is; +1 to account for the last element.
	 */
	kmem_free(array, (sizeof (dacf_opset_t)) * (i + 1));
	module->dm_opsets = NULL;
}

/*
 * dacf_opset_copy()
 * 	makes a copy of a dacf_opset_t.
 */
static void
dacf_opset_copy(dacf_opset_t *dst, dacf_opset_t *src)
{
	size_t nelems, i;
	ASSERT(src && dst);

	dprintf("dacf_opset_copy: called\n");

	dst->opset_name = kmem_alloc(strlen(src->opset_name) + 1, KM_SLEEP);
	(void) strcpy(dst->opset_name, src->opset_name);

	dprintf("dacf_opset_copy: counting ops\n");

	for (nelems = 0; ; nelems++) {
		if ((src->opset_ops[nelems].op_id == DACF_OPID_END) ||
		    (src->opset_ops[nelems].op_func == NULL)) {
			break;
		}
	}

	dprintf("dacf_opset_copy: found %lu ops\n", nelems);

	dst->opset_ops = kmem_alloc(sizeof (dacf_op_t) * (nelems + 1),
	    KM_SLEEP);

	dprintf("dacf_opset_copy: copying ops\n");
	for (i = 0; i < nelems; i++) {
		dst->opset_ops[i].op_id = src->opset_ops[i].op_id;
		dst->opset_ops[i].op_func = src->opset_ops[i].op_func;
	}
	dst->opset_ops[nelems].op_id = DACF_OPID_END;
	dst->opset_ops[nelems].op_func = NULL;

	dprintf("dacf_opset_copy: done copying ops\n");
}

int dacf_modload_laps = 0;	/* just a diagnostic aid */

/*
 * dacf_op_invoke()
 *	Invoke a op in a opset in a module given the rule to invoke.
 *
 *	If the return value of dacf_op_invoke is 0, then rval contains the
 *	return value of the _op_ being invoked. Otherwise, dacf_op_invoke's
 *	return value indicates why the op invocation failed.
 */
int
dacf_op_invoke(dacf_rule_t *rule, dacf_infohdl_t info_hdl, int flags)
{
	dacf_module_t *module;
	dacf_opset_t *opsarray;
	dacf_opset_t *opset;
	dacf_op_t *op = NULL;
	dacf_opid_t op_id;
	dacf_arghdl_t arg_hdl;
	dev_info_t *dip;
	int i, rval = -1;

	ASSERT(rule);
	ASSERT(MUTEX_HELD(&dacf_lock));

	op_id = rule->r_opid;
	dprintf("dacf_op_invoke: opid=%d\n", op_id);

	/*
	 * Take laps, trying to load the dacf module.  For the case of kernel-
	 * provided operations, __kernel will be found in the hash table, and
	 * no modload will be needed.
	 */
	for (;;) {
		if (mod_hash_find(dacf_module_hash,
		    (mod_hash_key_t)rule->r_module,
		    (mod_hash_val_t *)&module) == 0) {
			rw_enter(&module->dm_lock, RW_READER);
			/*
			 * Found the module, and it is loaded.
			 */
			if (module->dm_loaded != 0) {
				break;
			}
			rw_exit(&module->dm_lock);
		}

		/*
		 * If we're here, either: 1) it's not in the hash, or 2) it is,
		 * but dm_loaded is 0, meaning the module needs to be loaded.
		 */
		dprintf("dacf_op_invoke: calling modload\n");
		if (modload("dacf", rule->r_module) < 0) {
			return (DACF_ERR_MOD_NOTFOUND);
		}
		dacf_modload_laps++;
	}

	ASSERT(RW_READ_HELD(&module->dm_lock));

	opsarray = module->dm_opsets;

	/*
	 * Loop through the opsets exported by this module, and find the one
	 * we care about.
	 */
	opset = NULL;
	for (i = 0; opsarray[i].opset_name != NULL; i++) {
		if (strcmp(opsarray[i].opset_name, rule->r_opset) == 0) {
			opset = &opsarray[i];
			break;
		}
	}

	if (opset == NULL) {
		cmn_err(CE_WARN, "!dacf: couldn't invoke op, opset '%s' not "
		    "found in module '%s'", rule->r_opset, rule->r_module);
		rw_exit(&module->dm_lock);
		return (DACF_ERR_OPSET_NOTFOUND);
	}

	arg_hdl = (dacf_arghdl_t)rule->r_args;

	/*
	 * Call the appropriate routine in the target by looping across the
	 * ops until we find the one whose id matches opid.
	 */
	op = NULL;
	for (i = 0; opset->opset_ops[i].op_id != DACF_OPID_END; i++) {
		if (opset->opset_ops[i].op_id == op_id) {
			op = &(opset->opset_ops[i]);
			break;
		}
	}

	if (op == NULL) {
		cmn_err(CE_WARN, "!dacf: couldn't invoke op, op '%s' not found "
		    "in opset '%s' in module '%s'", dacf_opid_to_str(op_id),
		    rule->r_opset, rule->r_module);
		rw_exit(&module->dm_lock);
		return (DACF_ERR_OP_NOTFOUND);
	}

	dprintf("dacf_op_invoke: found op, invoking...\n");

	/*
	 * Drop dacf_lock here, so that op_func's that cause drivers to
	 * get loaded don't wedge the system when they try to acquire dacf_lock
	 * to do matching.
	 *
	 * Mark that an invoke is happening to prevent recursive invokes
	 */
	dip = ((struct ddi_minor_data *)info_hdl)->dip;

	mutex_enter(&(DEVI(dip)->devi_lock));
	DEVI_SET_INVOKING_DACF(dip);
	mutex_exit(&(DEVI(dip)->devi_lock));

	mutex_exit(&dacf_lock);

	rval = op->op_func(info_hdl, arg_hdl, flags);

	mutex_enter(&dacf_lock);

	/*
	 * Completed the invocation against module, so let go of it.
	 */
	mutex_enter(&(DEVI(dip)->devi_lock));
	DEVI_CLR_INVOKING_DACF(dip);
	mutex_exit(&(DEVI(dip)->devi_lock));

	/*
	 * Drop our r-lock on the module, now that we no longer need the module
	 * to stay loaded.
	 */
	rw_exit(&module->dm_lock);

	if (rval == DACF_SUCCESS) {
		return (DACF_SUCCESS);
	} else {
		return (DACF_ERR_OP_FAILED);
	}
}

/*
 * dacf_get_devspec()
 * 	given a devspec-type as a string, return a corresponding dacf_devspec_t
 */
dacf_devspec_t
dacf_get_devspec(char *name)
{
	dacf_ds_t *p = &dacf_devspecs[0];

	while (p->name != NULL) {
		if (strcmp(p->name, name) == 0) {
			return (p->id);
		}
		p++;
	}
	return (DACF_DS_ERROR);
}

/*
 * dacf_devspec_to_str()
 * 	given a dacf_devspec_t, return a pointer to the human readable string
 * 	representation of that device specifier.
 */
const char *
dacf_devspec_to_str(dacf_devspec_t ds)
{
	dacf_ds_t *p = &dacf_devspecs[0];

	while (p->name != NULL) {
		if (p->id == ds) {
			return (p->name);
		}
		p++;
	}
	return (NULL);
}

/*
 * dacf_get_op()
 * 	given a op name, returns the corresponding dacf_opid_t.
 */
dacf_opid_t
dacf_get_op(char *name)
{
	dacf_opmap_t *p = &dacf_ops[0];

	while (p->name != NULL) {
		if (strcmp(p->name, name) == 0) {
			return (p->id);
		}
		p++;
	}
	return (DACF_OPID_ERROR);
}

/*
 * dacf_opid_to_str()
 * 	given a dacf_opid_t, return the human-readable op-name.
 */
const char *
dacf_opid_to_str(dacf_opid_t tid)
{
	dacf_opmap_t *p = &dacf_ops[0];

	while (p->name != NULL) {
		if (p->id == tid) {
			return (p->name);
		}
		p++;
	}
	return (NULL);
}

/*
 * dacf_getopt()
 * 	given an option specified as a string, add it to the bit-field of
 * 	options given.  Returns -1 if the option is unrecognized.
 */
int
dacf_getopt(char *opt_str, uint_t *opts)
{
	dacf_opt_t *p = &dacf_options[0];

	/*
	 * Look through the list for the option given
	 */
	while (p->optname != NULL) {
		if (strcmp(opt_str, p->optname) == 0) {
			*opts |= p->optmask;
			return (0);
		}
		p++;
	}
	return (-1);
}



/*
 * This family of functions forms the dacf interface which is exported to
 * kernel/dacf modules.  Modules _should_not_ use any dacf_* functions
 * presented above this point.
 *
 * Note: These routines use a dacf_infohdl_t to struct ddi_minor_data * and
 * assume that the resulting pointer is not to an alias node.  That is true
 * because dacf_op_invoke guarantees it by first resolving the alias.
 */

/*
 * dacf_minor_name()
 * 	given a dacf_infohdl_t, obtain the minor name of the device instance
 * 	being configured.
 */
const char *
dacf_minor_name(dacf_infohdl_t info_hdl)
{
	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;

	return (dmdp->ddm_name);
}

/*
 * dacf_minor_number()
 * 	given a dacf_infohdl_t, obtain the device minor number of the instance
 * 	being configured.
 */
minor_t
dacf_minor_number(dacf_infohdl_t info_hdl)
{
	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;

	return (getminor(dmdp->ddm_dev));
}

/*
 * dacf_get_dev()
 *	given a dacf_infohdl_t, obtain the dev_t of the instance being
 *	configured.
 */
dev_t
dacf_get_dev(dacf_infohdl_t info_hdl)
{
	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;

	return (dmdp->ddm_dev);
}

/*
 * dacf_driver_name()
 * 	given a dacf_infohdl_t, obtain the device driver name of the device
 * 	instance being configured.
 */
const char *
dacf_driver_name(dacf_infohdl_t info_hdl)
{
	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;

	return (ddi_driver_name(dmdp->dip));
}

/*
 * dacf_devinfo_node()
 * 	given a dacf_infohdl_t, obtain the dev_info_t of the device instance
 * 	being configured.
 */
dev_info_t *
dacf_devinfo_node(dacf_infohdl_t info_hdl)
{
	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;

	return (dmdp->dip);
}

/*
 * dacf_get_arg()
 * 	given the dacf_arghdl_t passed to a op and the name of an argument,
 * 	return the value of that argument.
 *
 * 	returns NULL if the argument is not found.
 */
const char *
dacf_get_arg(dacf_arghdl_t arghdl, char *arg_name)
{
	dacf_arg_t *arg_list = (dacf_arg_t *)arghdl;
	ASSERT(arg_name);

	while (arg_list != NULL) {
		if (strcmp(arg_list->arg_name, arg_name) == 0) {
			return (arg_list->arg_val);
		}
		arg_list = arg_list->arg_next;
	}

	return (NULL);
}

/*
 * dacf_store_info()
 * 	associate instance-specific data with a device instance.  Future
 * 	configuration ops invoked for this instance can retrieve this data using
 * 	dacf_retrieve_info() below.  Modules are responsible for cleaning up
 * 	this data as appropriate, and should store NULL as the value of 'data'
 * 	when the data is no longer valid.
 */
void
dacf_store_info(dacf_infohdl_t info_hdl, void *data)
{
	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;

	/*
	 * If the client is 'storing NULL' we can represent that by blowing
	 * the info entry out of the hash.
	 */
	if (data == NULL) {
		(void) mod_hash_destroy(dacf_info_hash, (mod_hash_key_t)dmdp);
	} else {
		/*
		 * mod_hash_replace can only fail on out of memory, but we sleep
		 * for memory in this hash, so it is safe to ignore the retval.
		 */
		(void) mod_hash_replace(dacf_info_hash, (mod_hash_key_t)dmdp,
		    (mod_hash_val_t)data);
	}
}

/*
 * dacf_retrieve_info()
 * 	retrieve instance-specific data associated with a device instance.
 */
void *
dacf_retrieve_info(dacf_infohdl_t info_hdl)
{
	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
	void *data;

	if (mod_hash_find(dacf_info_hash, (mod_hash_key_t)dmdp,
	    (mod_hash_val_t *)&data) != 0) {
		return (NULL);
	}

	return (data);
}

/*
 * dacf_makevp()
 * 	make a vnode for the specified dacf_infohdl_t.
 */
struct vnode *
dacf_makevp(dacf_infohdl_t info_hdl)
{
	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
	struct vnode	*vp;

	vp = makespecvp(dmdp->ddm_dev, VCHR);
	spec_assoc_vp_with_devi(vp, dmdp->dip);
	return (vp);
}