summaryrefslogtreecommitdiff
path: root/usr/src/lib/cfgadm_plugins/scsi/common/cfga_ctl.c
blob: f81b54a80490f08e4ed0c3413ff9d10f405d0aaa (plain)
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
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include "cfga_scsi.h"

struct larg {
	int ndevs;
	int nelem;
	char *dev;
	char **dev_list;
};

#define	ETC_VFSTAB	"/etc/vfstab"
#define	SCFGA_LOCK	"/var/run/cfgadm_scsi"

/* Function prototypes */

static scfga_ret_t quiesce_confirm(apid_t *apidp,
    msgid_t cmd_msg, prompt_t *pt, int *okp, int *quiesce, int *l_errnop);
static scfga_ret_t dev_hotplug(apid_t *apidp,
    prompt_t *pt, cfga_flags_t flags, int quiesce, char **errstring);
static int disconnect(struct cfga_confirm *confp);
static int critical_ctrlr(const char *hba_phys);
static cfga_stat_t bus_devctl_to_recep_state(uint_t bus_dc_state);
static int get_hba_children(char *bus_path, char *dev_excl, char ***dev_list);
static char *get_node_path(char *minor_path);
static void free_dev_list_elements(char **dev_list);
static void free_dev_list(char **dev_list);
static int alloc_dev_list(struct larg *largp);

/*
 * Single thread all implicit quiesce operations
 */
static mutex_t	quiesce_mutex = DEFAULTMUTEX;

/*ARGSUSED*/
scfga_ret_t
bus_change_state(
	cfga_cmd_t state_change_cmd,
	apid_t *apidp,
	struct cfga_confirm *confp,
	cfga_flags_t flags,
	char **errstring)
{
	int l_errno = 0, force;
	uint_t state = 0;
	cfga_stat_t bus_state;
	scfga_cmd_t cmd;
	msgid_t errid;
	cfga_stat_t prereq;
	scfga_ret_t ret;
	char **dev_list = NULL;

	assert(apidp->path != NULL);
	assert(apidp->hba_phys != NULL);

	/*
	 * No dynamic components allowed
	 */
	if (apidp->dyncomp != NULL) {
		cfga_err(errstring, 0, ERR_NOT_BUSAPID, 0);
		return (SCFGA_ERR);
	}

	/* Get bus state */
	if (devctl_cmd(apidp->path, SCFGA_BUS_GETSTATE, &state,
	    &l_errno) != SCFGA_OK) {
		cfga_err(errstring, l_errno, ERR_BUS_GETSTATE, 0);
		return (SCFGA_ERR);
	}

	bus_state = bus_devctl_to_recep_state(state);
	force = ((flags & CFGA_FLAG_FORCE) == CFGA_FLAG_FORCE) ? 1 : 0;
	assert(confp->confirm != NULL);

	switch (state_change_cmd) {
	case CFGA_CMD_DISCONNECT:	/* quiesce bus */
		/*
		 * If force flag not specified, check if controller is
		 * critical.
		 */
		if (!force) {
			/*
			 * This check is not foolproof, get user confirmation
			 * if test passes.
			 */
			if (critical_ctrlr(apidp->path)) {
				cfga_err(errstring, 0, ERR_CTRLR_CRIT, 0);
				ret = SCFGA_ERR;
				break;
			} else if (!disconnect(confp)) {
				ret = SCFGA_NACK;
				break;
			}
		}

		cmd = SCFGA_BUS_QUIESCE;
		errid = ERR_BUS_QUIESCE;
		prereq = CFGA_STAT_CONNECTED;

		goto common;

	case CFGA_CMD_CONNECT:		/* unquiesce bus */
		cmd = SCFGA_BUS_UNQUIESCE;
		errid = ERR_BUS_UNQUIESCE;
		prereq = CFGA_STAT_DISCONNECTED;

		goto common;

	case CFGA_CMD_CONFIGURE:
		cmd = SCFGA_BUS_CONFIGURE;
		errid = ERR_BUS_CONFIGURE;
		prereq = CFGA_STAT_CONNECTED;

		goto common;

	case CFGA_CMD_UNCONFIGURE:
		cmd = SCFGA_BUS_UNCONFIGURE;
		errid = ERR_BUS_UNCONFIGURE;
		prereq = CFGA_STAT_CONNECTED;

		/* FALLTHROUGH */
	common:
		if (bus_state != prereq) {
			cfga_err(errstring, 0,
			    (prereq == CFGA_STAT_CONNECTED)
			    ? ERR_BUS_NOTCONNECTED
			    : ERR_BUS_CONNECTED, 0);
			ret = SCFGA_ERR;
			break;
		}

		/*
		 * When quiescing or unconfiguring a bus, first suspend or
		 * offline it through RCM.
		 * For unquiescing, we simple build the dev_list for
		 * resume notification.
		 */
		if (((apidp->flags & FLAG_DISABLE_RCM) == 0) &&
		    ((cmd == SCFGA_BUS_QUIESCE) ||
		    (cmd == SCFGA_BUS_UNQUIESCE) ||
		    (cmd == SCFGA_BUS_UNCONFIGURE))) {
			ret = get_hba_children(apidp->path, NULL, &dev_list);
			if (ret != SCFGA_OK) {
				break;
			}
			if (cmd == SCFGA_BUS_QUIESCE) {
				if ((ret = scsi_rcm_suspend(dev_list,
				    errstring, flags, 1)) != SCFGA_OK) {
					break;
				}
			} else if (cmd == SCFGA_BUS_UNCONFIGURE) {
				if ((ret = scsi_rcm_offline(dev_list,
				    errstring, flags)) != SCFGA_OK) {
					break;
				}
			}
		}

		ret = devctl_cmd(apidp->path, cmd, NULL, &l_errno);
		if (ret != SCFGA_OK) {
			/*
			 * EIO when child devices are busy may confuse user.
			 * So explain it.
			 */
			if (cmd == SCFGA_BUS_UNCONFIGURE && l_errno == EIO)
				errid = ERR_MAYBE_BUSY;

			cfga_err(errstring, l_errno, errid, 0);

			/*
			 * If the bus was suspended in RCM, then cancel the RCM
			 * operation.  Discard RCM failures here because the
			 * devctl's failure is what is most relevant.
			 */
			if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
				if (cmd == SCFGA_BUS_QUIESCE)
					(void) scsi_rcm_resume(dev_list,
					    errstring,
					    (flags & (~CFGA_FLAG_FORCE)), 1);
				else if (cmd == SCFGA_BUS_UNCONFIGURE) {
					(void) devctl_cmd(apidp->path,
					    SCFGA_BUS_CONFIGURE, NULL,
					    &l_errno);
					(void) scsi_rcm_online(dev_list,
					    errstring,
					    (flags & (~CFGA_FLAG_FORCE)));
				}
			}

			break;
		}

		/*
		 * When unquiescing or configuring a bus, resume or online it
		 * in RCM when the devctl command is complete.
		 * When unconfiguring a bus, notify removal of devices.
		 */
		if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
			if (cmd == SCFGA_BUS_UNQUIESCE) {
				ret = scsi_rcm_resume(dev_list, errstring,
				    (flags & (~CFGA_FLAG_FORCE)), 1);
			} else if (cmd == SCFGA_BUS_UNCONFIGURE) {
				ret = scsi_rcm_remove(dev_list, errstring,
				    (flags & (~CFGA_FLAG_FORCE)));
			}
		}
		break;

	case CFGA_CMD_LOAD:
	case CFGA_CMD_UNLOAD:
		ret = SCFGA_OPNOTSUPP;
		break;

	default:
		cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
		ret = SCFGA_ERR;
		break;
	}

	free_dev_list(dev_list);
	return (ret);
}

scfga_ret_t
dev_change_state(
	cfga_cmd_t state_change_cmd,
	apid_t *apidp,
	cfga_flags_t flags,
	char **errstring)
{
	uint_t state = 0;
	int l_errno = 0;
	cfga_stat_t bus_state;
	scfga_cmd_t cmd;
	msgid_t errid;
	scfga_ret_t ret;
	char *dev_list[2] = {NULL};

	assert(apidp->path != NULL);
	assert(apidp->hba_phys != NULL);

	/*
	 * For a device, dynamic component must be present
	 */
	if (apidp->dyncomp == NULL) {
		cfga_err(errstring, 0, ERR_APID_INVAL, 0);
		return (SCFGA_ERR);
	}

	/* Get bus state */
	if (devctl_cmd(apidp->hba_phys, SCFGA_BUS_GETSTATE, &state,
	    &l_errno) != SCFGA_OK) {
		cfga_err(errstring, l_errno, ERR_BUS_GETSTATE, 0);
		return (SCFGA_ERR);
	}

	bus_state = bus_devctl_to_recep_state(state);

	switch (state_change_cmd) {
	case CFGA_CMD_CONFIGURE:		/* online device */
		cmd = SCFGA_DEV_CONFIGURE;
		errid = ERR_DEV_CONFIGURE;
		goto common;

	case CFGA_CMD_UNCONFIGURE:		/* offline device */
		cmd = SCFGA_DEV_UNCONFIGURE;
		errid = ERR_DEV_UNCONFIGURE;
		/* FALLTHROUGH */
	common:
		if (bus_state != CFGA_STAT_CONNECTED) {
			cfga_err(errstring, 0, ERR_BUS_NOTCONNECTED, 0);
			ret = SCFGA_ERR;
			break;
		}

		if (apidp->dyntype == PATH_APID) {
			/* call a scsi_vhci ioctl to do online/offline path. */
			ret = path_apid_state_change(apidp, cmd,
			    flags, errstring, &l_errno, errid);
		} else {
			/*
			 * When unconfiguring a device, first offline it
			 * through RCM.
			 */
			if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
				if (cmd == SCFGA_DEV_UNCONFIGURE) {
					dev_list[0] =
					    get_node_path(apidp->path);
					if (dev_list[0] == NULL) {
						ret = SCFGA_ERR;
						break;
					}
					if ((ret = scsi_rcm_offline(dev_list,
					    errstring, flags)) != SCFGA_OK) {
						break;
					}
				}
			}

			ret = devctl_cmd(apidp->path, cmd, NULL, &l_errno);
			if (ret != SCFGA_OK) {
				cfga_err(errstring, l_errno, errid, 0);

			/*
			 * If an unconfigure fails, cancel the RCM offline.
			 * Discard any RCM failures so that the devctl
			 * failure will still be reported.
			 */
				if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
					if (cmd == SCFGA_DEV_UNCONFIGURE)
						(void) scsi_rcm_online(dev_list,
						    errstring, flags);
				}
				break;
			}
			if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
			/*
			 * Unconfigure succeeded, call the RCM notify_remove.
			 */
				if (cmd == SCFGA_DEV_UNCONFIGURE)
					(void) scsi_rcm_remove(dev_list,
					    errstring, flags);
			}
		}
		break;

	/*
	 * Cannot disconnect/connect individual devices without affecting
	 * other devices on the bus. So we don't support these ops.
	 */
	case CFGA_CMD_DISCONNECT:
	case CFGA_CMD_CONNECT:
		cfga_err(errstring, 0, ERR_NOT_DEVOP, 0);
		ret = SCFGA_ERR;
		break;
	case CFGA_CMD_LOAD:
	case CFGA_CMD_UNLOAD:
		ret = SCFGA_OPNOTSUPP;
		break;
	default:
		cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
		ret = SCFGA_ERR;
		break;
	}

	free_dev_list_elements(dev_list);
	return (ret);
}

/*ARGSUSED*/
scfga_ret_t
dev_remove(
	const char *func,
	scfga_cmd_t cmd,
	apid_t *apidp,
	prompt_t *prp,
	cfga_flags_t flags,
	char **errstring)
{
	int proceed, l_errno = 0;
	scfga_ret_t ret;
	int do_quiesce;
	char *dev_list[2] = {NULL};

	assert(apidp->hba_phys != NULL);
	assert(apidp->path != NULL);

	/* device operation only */
	if (apidp->dyncomp == NULL) {
		cfga_err(errstring, 0, ERR_NOT_BUSOP, 0);
		return (SCFGA_ERR);
	}

	proceed = 1;
	ret = quiesce_confirm(apidp, MSG_RMDEV, prp, &proceed, &do_quiesce,
	    &l_errno);
	if (ret != SCFGA_OK) {
		cfga_err(errstring, l_errno, ERR_DEV_REMOVE, 0);
		return (ret);
	}

	if (!proceed) {
		return (SCFGA_NACK);
	}

	/*
	 * Offline the device in RCM
	 */
	if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
		dev_list[0] = get_node_path(apidp->path);
		if (dev_list[0] == NULL)
			return (SCFGA_ERR);
		if ((ret = scsi_rcm_offline(dev_list, errstring, flags))
		    != SCFGA_OK) {
			free_dev_list_elements(dev_list);
			return (ret);
		}
	}

	/*
	 * Offline the device
	 */
	ret = devctl_cmd(apidp->path, SCFGA_DEV_UNCONFIGURE, NULL, &l_errno);
	if (ret != SCFGA_OK) {

		cfga_err(errstring, l_errno, ERR_DEV_REMOVE, 0);

		/*
		 * Cancel the RCM offline.  Discard the RCM failures so that
		 * the above devctl failure is still reported.
		 */
		if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
			(void) scsi_rcm_online(dev_list, errstring, flags);
		free_dev_list_elements(dev_list);
		return (ret);
	}

	/* Do the physical removal */
	ret = dev_hotplug(apidp, prp, flags, do_quiesce, errstring);

	if (ret == SCFGA_OK) {
		/*
		 * Complete the remove.
		 * Since the device is already offlined, remove shouldn't
		 * fail. Even if remove fails, there is no side effect.
		 */
		(void) devctl_cmd(apidp->path, SCFGA_DEV_REMOVE,
		    NULL, &l_errno);
		if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
			ret = scsi_rcm_remove(dev_list, errstring, flags);
	} else {
		/*
		 * Reconfigure the device and restore the device's RCM state.
		 * If reconfigure succeeds, restore the state to online.
		 * If reconfigure fails (e.g. a typo from user), we treat
		 * the device as removed.
		 */
		if (devctl_cmd(apidp->path, SCFGA_DEV_CONFIGURE, NULL, &l_errno)
		    == SCFGA_OK) {
			if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
				(void) scsi_rcm_online(dev_list, errstring,
				    flags);
		} else {
			char *cp = strrchr(apidp->path, ':');
			if (cp)
				*cp = '\0';
			cfga_err(errstring, l_errno, ERR_DEV_RECONFIGURE,
			    apidp->path, 0);
			if (cp)
				*cp = ':';
			if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
				(void) scsi_rcm_remove(dev_list, errstring,
				    flags);
		}
	}

	free_dev_list_elements(dev_list);
	return (ret);
}

/*ARGSUSED*/
scfga_ret_t
dev_insert(
	const char *func,
	scfga_cmd_t cmd,
	apid_t *apidp,
	prompt_t *prp,
	cfga_flags_t flags,
	char **errstring)
{
	int proceed, l_errno = 0;
	scfga_ret_t ret;
	int do_quiesce;

	assert(apidp->hba_phys != NULL);
	assert(apidp->path != NULL);

	/* Currently, insert operation only allowed for bus */
	if (apidp->dyncomp != NULL) {
		cfga_err(errstring, 0, ERR_NOT_DEVOP, 0);
		return (SCFGA_ERR);
	}

	proceed = 1;
	ret = quiesce_confirm(apidp, MSG_INSDEV, prp, &proceed, &do_quiesce,
	    &l_errno);
	if (ret != SCFGA_OK) {
		cfga_err(errstring, l_errno, ERR_DEV_INSERT, 0);
		return (ret);
	}

	if (!proceed) {
		return (SCFGA_NACK);
	}

	/* Do the physical addition */
	ret = dev_hotplug(apidp, prp, flags, do_quiesce, errstring);
	if (ret != SCFGA_OK) {
		return (ret);
	}

	/*
	 * Configure bus to online new device(s).
	 * Previously offlined devices will not be onlined.
	 */
	ret = devctl_cmd(apidp->hba_phys, SCFGA_BUS_CONFIGURE, NULL, &l_errno);
	if (ret != SCFGA_OK) {
		cfga_err(errstring, l_errno, ERR_DEV_INSERT, 0);
		return (SCFGA_ERR);
	}

	return (SCFGA_OK);
}

/*ARGSUSED*/
scfga_ret_t
dev_replace(
	const char *func,
	scfga_cmd_t cmd,
	apid_t *apidp,
	prompt_t *prp,
	cfga_flags_t flags,
	char **errstring)
{
	int proceed, l_errno = 0;
	scfga_ret_t ret, ret2;
	int do_quiesce;
	char *dev_list[2] = {NULL};

	assert(apidp->hba_phys != NULL);
	assert(apidp->path != NULL);

	/* device operation only */
	if (apidp->dyncomp == NULL) {
		cfga_err(errstring, 0, ERR_NOT_BUSOP, 0);
		return (SCFGA_ERR);
	}

	proceed = 1;
	ret = quiesce_confirm(apidp, MSG_REPLDEV, prp, &proceed, &do_quiesce,
	    &l_errno);
	if (ret != SCFGA_OK) {
		cfga_err(errstring, l_errno, ERR_DEV_REPLACE, 0);
		return (ret);
	}

	if (!proceed) {
		return (SCFGA_NACK);
	}

	/* Offline the device in RCM */
	if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
		dev_list[0] = get_node_path(apidp->path);
		if (dev_list[0] == NULL)
			return (SCFGA_ERR);
		if ((ret = scsi_rcm_offline(dev_list, errstring, flags))
		    != SCFGA_OK) {
			free_dev_list_elements(dev_list);
			return (ret);
		}
	}

	ret = devctl_cmd(apidp->path, SCFGA_DEV_REMOVE, NULL, &l_errno);
	if (ret != SCFGA_OK) {

		/*
		 * Cancel the RCM offline.  Discard any RCM failures so that
		 * the devctl failure can still be reported.
		 */
		if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
			(void) scsi_rcm_online(dev_list, errstring, flags);

		cfga_err(errstring, l_errno, ERR_DEV_REPLACE, 0);
		free_dev_list_elements(dev_list);
		return (ret);
	}

	/* do the physical replace */
	ret = dev_hotplug(apidp, prp, flags, do_quiesce, errstring);

	/* Online the replacement, or restore state on error */
	ret2 = devctl_cmd(apidp->path, SCFGA_DEV_CONFIGURE, NULL, &l_errno);

	if (ret2 != SCFGA_OK) {
		cfga_err(errstring, l_errno, ERR_DEV_REPLACE, 0);
	}

	/*
	 * Remove the replaced device in RCM, or online the device in RCM
	 * to recover.
	 */
	if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
		if (ret == SCFGA_OK)
			ret = scsi_rcm_remove(dev_list, errstring, flags);
		else if (ret2 == SCFGA_OK)
			ret2 = scsi_rcm_online(dev_list, errstring, flags);
	}
	free_dev_list_elements(dev_list);

	return (ret == SCFGA_OK ? ret2 : ret);
}

#pragma weak plat_dev_led
/*ARGSUSED*/
scfga_ret_t
dev_led(
	const char *func,
	scfga_cmd_t cmd,
	apid_t *apidp,
	prompt_t *prp,
	cfga_flags_t flags,
	char **errstring)
{

	/*
	 * The implementation of the led command is platform-specific, so
	 * the default behavior is to say that the functionality is not
	 * available for this device.
	 */
	if (plat_dev_led) {
		return (plat_dev_led(func, cmd, apidp, prp, flags, errstring));
	}
	cfga_err(errstring, 0, ERR_UNAVAILABLE, 0);
	return (SCFGA_ERR);
}

/*ARGSUSED*/
scfga_ret_t
reset_common(
	const char *func,
	scfga_cmd_t cmd,
	apid_t *apidp,
	prompt_t *prp,
	cfga_flags_t flags,
	char **errstring)
{
	int l_errno = 0;
	scfga_ret_t ret;


	assert(apidp->path != NULL);
	assert(apidp->hba_phys != NULL);

	switch (cmd) {
	case SCFGA_RESET_DEV:
		if (apidp->dyncomp == NULL) {
			cfga_err(errstring, 0, ERR_NOT_BUSOP, 0);
			return (SCFGA_ERR);
		}
		break;

	case SCFGA_RESET_BUS:
	case SCFGA_RESET_ALL:
		if (apidp->dyncomp != NULL) {
			cfga_err(errstring, 0, ERR_NOT_DEVOP, 0);
			return (SCFGA_ERR);
		}
		break;
	default:
		cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
		return (SCFGA_ERR);
	}

	ret = devctl_cmd(apidp->path, cmd, NULL, &l_errno);
	if (ret != SCFGA_OK) {
		cfga_err(errstring, l_errno, ERR_RESET, 0);
	}

	return (ret);
}

static int
disconnect(struct cfga_confirm *confp)
{
	int ans, append_newline;
	char *cq;

	append_newline = 0;
	cq = cfga_str(append_newline, WARN_DISCONNECT, 0);

	ans = confp->confirm(confp->appdata_ptr, cq);

	S_FREE(cq);

	return (ans == 1);
}

/*
 * Check for "scsi-no-quiesce" property
 * Return code: -1 error, 0 quiesce not required, 1 quiesce required
 */
static int
quiesce_required(apid_t *apidp, int *l_errnop)
{
	di_node_t bus_node, dev_node;
	char *bus_path, *bus_end;
	char *dev_path, *dev_end;
	int *propval;

	/* take libdevinfo snapshot of subtree at hba */
	bus_path = apidp->hba_phys + strlen(DEVICES_DIR);
	bus_end = strrchr(bus_path, ':');
	if (bus_end)
		*bus_end = '\0';

	bus_node = di_init(bus_path, DINFOSUBTREE|DINFOPROP);
	if (bus_end)
		*bus_end = ':';
	if (bus_node == DI_NODE_NIL) {
		*l_errnop = errno;
		return (-1);	/* error */
	}

	/* check bus node for property */
	if (di_prop_lookup_ints(DDI_DEV_T_ANY, bus_node, SCSI_NO_QUIESCE,
	    &propval) == 1) {
		di_fini(bus_node);
		return (0);	/* quiesce not required */
	}

	/* if this ap is HBA, return with quiesce required */
	if (apidp->dyncomp == NULL) {
		di_fini(bus_node);
		return (1);
	}

	/* check device node for property */
	dev_path = apidp->path + strlen(DEVICES_DIR);
	dev_end = strrchr(dev_path, ':');
	if (dev_end)
		*dev_end = '\0';

	dev_node = di_child_node(bus_node);
	while (dev_node != DI_NODE_NIL) {
		char *child_path;
		child_path = di_devfs_path(dev_node);
		if (strcmp(child_path, dev_path) == 0) {
			di_devfs_path_free(child_path);
			break;
		}
		di_devfs_path_free(child_path);
		dev_node = di_sibling_node(dev_node);
	}

	if (dev_end)
		*dev_end = ':';
	if (dev_node == DI_NODE_NIL) {
		di_fini(bus_node);
		return (1);	/* dev not found (insert case) */
	}

	/* check child node for property */
	if (di_prop_lookup_ints(DDI_DEV_T_ANY, dev_node, "scsi-no-quiesce",
	    &propval) == 1) {
		di_fini(bus_node);
		return (0);	/* quiesce not required */
	}
	return (1);	/* quiesce required */
}

static scfga_ret_t
quiesce_confirm(
	apid_t *apidp,
	msgid_t cmd_msg,
	prompt_t *prp,
	int *okp,
	int *quiesce,
	int *l_errnop)
{
	char *buf = NULL, *hbap = NULL, *cq1 = NULL, *cq2 = NULL;
	char *cp;
	size_t len = 0;
	int i = 0, append_newline;
	scfga_ret_t ret;

	assert(apidp->path != NULL);
	assert(apidp->hba_phys != NULL);

	*quiesce = quiesce_required(apidp, l_errnop);
	if (*quiesce == -1)
		return (SCFGA_ERR);
	else if (*quiesce == 0)
		return (SCFGA_OK);

	/*
	 * Try to create HBA logical ap_id.
	 * If that fails use physical path
	 */
	ret = make_hba_logid(apidp->hba_phys, &hbap, &i);
	if (ret != SCFGA_OK) {
		if ((hbap = get_node_path(apidp->hba_phys)) == NULL) {
			*l_errnop = errno;
			return (SCFGA_LIB_ERR);
		}
	}

	assert(hbap != NULL);

	append_newline = 0;
	cq1 = cfga_str(append_newline, CONF_QUIESCE_1, hbap, 0);
	cq2 = cfga_str(append_newline, CONF_QUIESCE_2, 0);
	len = strlen(cq1) + strlen(cq2) + 1; /* Includes term. NULL */

	if ((buf = calloc(1, len)) == NULL) {
		*l_errnop = errno;
		ret = SCFGA_LIB_ERR;
		S_FREE(cq1);
		S_FREE(cq2);
		goto out;
	}
	(void) strcpy(buf, cq1);
	(void) strcat(buf, cq2);

	S_FREE(cq1);
	S_FREE(cq2);


	/* Remove minor name (if any) from phys path */
	if ((cp = strrchr(apidp->path, ':')) != NULL) {
		*cp = '\0';
	}

	/* describe operation being attempted */
	cfga_msg(prp->msgp, cmd_msg, apidp->path, 0);

	/* Restore minor name */
	if (cp != NULL) {
		*cp = ':';
	}

	/* request permission to quiesce */
	assert(prp->confp != NULL && prp->confp->confirm != NULL);
	*okp = prp->confp->confirm(prp->confp->appdata_ptr, buf);

	ret = SCFGA_OK;
	/*FALLTHRU*/
out:
	S_FREE(buf);
	S_FREE(hbap);
	return (ret);
}

static scfga_ret_t
suspend_in_rcm(
	apid_t		*apidp,
	char		***suspend_list_ptr,
	char		**errstring,
	cfga_flags_t	flags)
{
	scfga_ret_t	ret;
	char		*bus_path = NULL;
	char		*dev_path = NULL;
	char		**suspend_list = NULL;

	*suspend_list_ptr = NULL;

	/* Suspend the bus through RCM */
	if (apidp->flags & FLAG_DISABLE_RCM)
		return (SCFGA_OK);

	/* The bus_path is the HBA path without its minor */
	if ((bus_path = get_node_path(apidp->hba_phys)) == NULL)
		return (SCFGA_ERR);

	/*
	 * The dev_path is already initialized to NULL.  If the AP Id
	 * path differs from the HBA path, then the dev_path should
	 * instead be set to the AP Id path without its minor.
	 */
	if (strcmp(apidp->hba_phys, apidp->path) != 0) {
		if ((dev_path = get_node_path(apidp->path)) == NULL) {
			ret = SCFGA_ERR;
			goto out;
		}
	}

	if ((ret = get_hba_children(bus_path, dev_path, &suspend_list))
	    != SCFGA_OK) {
		free_dev_list(suspend_list);
		goto out;
	}

	if (scsi_rcm_suspend(suspend_list, errstring, flags, 0) != SCFGA_OK) {
		ret = SCFGA_ERR;
		free_dev_list(suspend_list);
	} else {
		ret = SCFGA_OK;
		*suspend_list_ptr = suspend_list;
	}
	/*FALLTHROUGH*/
out:
	S_FREE(bus_path);
	S_FREE(dev_path);
	return (ret);
}

/*
 * Resume the bus through RCM if it successfully
 * unquiesced.
 */
static void
resume_in_rcm(
	apid_t		*apidp,
	char		**suspend_list,
	char		**errstring,
	cfga_flags_t	flags)
{
	if (apidp->flags & FLAG_DISABLE_RCM)
		return;

	(void) scsi_rcm_resume(suspend_list, errstring, flags, 0);

	free_dev_list(suspend_list);
}

static scfga_ret_t
wait_for_hotplug(prompt_t *pt, int msg)
{
	char		*cu = NULL;
	int		append_newline = 0;
	scfga_ret_t	ret;

	cu = cfga_str(append_newline, msg, 0);
	if (pt->confp->confirm(pt->confp->appdata_ptr, cu) != 1) {
		ret = SCFGA_NACK;
	} else {
		ret = SCFGA_OK;
	}
	S_FREE(cu);
	return (ret);
}

static scfga_ret_t
bus_quiesce(apid_t *apidp, prompt_t *pt, char **errstring, cfga_flags_t flags)
{
	int		l_errno;
	scfga_ret_t	ret;
	scfga_ret_t	hpret;
	char		**suspend_list = NULL;

	ret = suspend_in_rcm(apidp, &suspend_list, errstring, flags);
	if (ret != SCFGA_OK) {
		return (ret);
	}

	/*
	 * If the quiesce fails, then cancel the RCM suspend.
	 * Discard any RCM failures so that the devctl failure
	 * can still be reported.
	 */
	l_errno = 0;
	ret = devctl_cmd(apidp->hba_phys, SCFGA_BUS_QUIESCE, NULL, &l_errno);
	if (ret != SCFGA_OK) {
		resume_in_rcm(apidp, suspend_list, errstring, flags);
		cfga_err(errstring, l_errno, ERR_BUS_QUIESCE, 0);
		return (ret);
	}

	/*
	 * Prompt user to proceed with physical hotplug
	 * and wait until they are done.
	 */
	hpret = wait_for_hotplug(pt, CONF_UNQUIESCE);

	/*
	 * The unquiesce may fail with EALREADY (which is ok)
	 * or some other error (which is not ok).
	 */
	l_errno = 0;
	ret = devctl_cmd(apidp->hba_phys, SCFGA_BUS_UNQUIESCE, NULL, &l_errno);
	if (ret != SCFGA_OK && l_errno != EALREADY) {
		free_dev_list(suspend_list);
		cfga_err(errstring, l_errno, ERR_BUS_UNQUIESCE, 0);
		return (SCFGA_ERR);
	}

	resume_in_rcm(apidp, suspend_list, errstring, flags);

	return (hpret);
}

#define	MAX_LOCK_TRIES		20
#define	MAX_UNLINK_TRIES	60
#define	s_getpid		(int)getpid	/* else lint causes problems */

static void
s_unlink(char *file)
{
	int	count = 0;

retry:
	if (unlink(file) == -1) {
		if (errno != EINTR && errno != EAGAIN) {
			CFGA_TRACE1((stdout, "s_unlink[%d]: unlink failed: "
			    "%s: %s\n", s_getpid(), file, strerror(errno)));
			return;
		}

		if (++count < MAX_UNLINK_TRIES) {
			(void) sleep(1);
			goto retry;
		}
		CFGA_TRACE1((stdout, "s_unlink[%d]: retry limit: %s\n",
		    s_getpid(), file));
	} else {
		CFGA_TRACE3((stdout, "s_unlink[%d]: unlinked: %s\n",
		    s_getpid(), file));
	}
}

static scfga_ret_t
create_lock(int *fdp, struct cfga_msg *msgp, char **errstring)
{
	FILE			*fp;
	int			count;
	struct extmnttab	ent;
	int			mnted;


	*fdp = -1;

	/*
	 * Check that /var/run is mounted. In the unlikely event
	 * that the lock file is left behind, we want it
	 * cleared on the next reboot.
	 */
	errno = 0;
	if ((fp = fopen(MNTTAB, "r")) == NULL) {
		cfga_err(errstring, errno, ERRARG_OPEN, MNTTAB, 0);
		return (SCFGA_LIB_ERR);
	}

	resetmnttab(fp);

	mnted = 0;
	while (getextmntent(fp, &ent, sizeof (ent)) == 0) {
		if (strcmp(ent.mnt_mountp, "/var/run") == 0) {
			mnted = 1;
			break;
		}
	}

	(void) fclose(fp);

	if (!mnted) {
		cfga_err(errstring, 0, ERR_VAR_RUN, 0);
		return (SCFGA_LIB_ERR);
	}

	/*
	 * Wait for a short period of time if we cannot O_EXCL create
	 * lock file. If some other cfgadm process is finishing up, we
	 * can get in. If the wait required is long however, just
	 * return SYSTEM_BUSY to the user - a hotplug operation is
	 * probably in progress.
	 */
	count = 0;
retry:
	*fdp = open(SCFGA_LOCK, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
	if (*fdp == -1 && errno == EEXIST) {
		if (++count < MAX_LOCK_TRIES) {
			if (count == 1)
				cfga_msg(msgp, MSG_WAIT_LOCK, 0);
			(void) sleep(1);
			goto retry;
		}
	}

	if (*fdp == -1 && errno == EEXIST) {
		cfga_err(errstring, 0, ERRARG_QUIESCE_LOCK, SCFGA_LOCK, 0);
		return (SCFGA_SYSTEM_BUSY);
	} else if (*fdp == -1) {
		cfga_err(errstring, errno, ERRARG_QUIESCE_LOCK, SCFGA_LOCK, 0);
		return (SCFGA_LIB_ERR);
	}

	CFGA_TRACE3((stdout, "create_lock[%d]: created lockfile: %s\n",
	    s_getpid(), SCFGA_LOCK));

	return (SCFGA_OK);
}

static scfga_ret_t
syslock(int fd, char **errstring)
{
	struct flock	lock;
	int		count;
	int		rval;

	assert(fd != -1);

	CFGA_TRACE3((stdout, "syslock[%d]: trying lock: %s\n",
	    s_getpid(), SCFGA_LOCK));

	lock.l_type = F_WRLCK;
	lock.l_whence = SEEK_SET;
	lock.l_start = 0;
	lock.l_len = 0;

	count = 0;
	while ((rval = fcntl(fd, F_SETLKW, &lock)) == -1 && errno == EINTR) {
		if (++count >= MAX_LOCK_TRIES) {
			CFGA_TRACE1((stdout, "syslock[%d]: retry limit: %s\n",
			    s_getpid(), SCFGA_LOCK));
			goto badlock;
		}
		(void) sleep(1);
	}

	if (rval != -1) {
		CFGA_TRACE3((stdout, "syslock[%d]: locked file: %s\n",
		    s_getpid(), SCFGA_LOCK));
		return (SCFGA_OK);
	}

	/*FALLTHROUGH*/
badlock:
	cfga_err(errstring, errno, ERRARG_LOCK, SCFGA_LOCK, 0);
	/* trace message to display pid */
	CFGA_TRACE1((stdout, "syslock[%d]: cannot lock %s\n",
	    s_getpid(), SCFGA_LOCK));
	return (SCFGA_LIB_ERR);
}

static void
wait_for_child(pid_t cpid)
{
	int	status;
	pid_t	rval;

	CFGA_TRACE2((stdout, "wait_for_child[%d]: child[%d]\n",
	    s_getpid(), (int)cpid));

	for (;;) {
		while ((rval = waitpid(cpid, &status, 0)) != cpid) {
			if (errno == ECHILD) {
				CFGA_TRACE1((stdout, "waitpid[%d]: child[%d] "
				    "doesn't exist\n", s_getpid(), (int)cpid));
				return;
			}

			CFGA_TRACE3((stdout, "waitpid[%d]: returned: %d"
			    ": errno: %s\n", s_getpid(), (int)rval,
			    strerror(errno)));
		}

		if (WIFEXITED(status)) {
			CFGA_TRACE2((stdout, "waitpid[%d]: child[%d]: "
			    "normal exit\n", s_getpid(), (int)cpid));
			return;
		}

		if (WIFSIGNALED(status)) {
			CFGA_TRACE2((stdout, "waitpid[%d]: child[%d]: "
			    "signal exit\n", s_getpid(), (int)cpid));
			return;
		}

		/*
		 * The child has not terminated. We received status
		 * because the child was either stopped or continued.
		 * Wait for child termination by calling waitpid() again.
		 */
	}
}

static void
wait_and_cleanup(int fd, apid_t *apidp)
{
	int		l_errno;
	scfga_ret_t	ret;

	/* This is the child */
	CFGA_TRACE2((stdout, "child[%d]: Entering wait_cleanup\n", s_getpid()));

	if (syslock(fd, NULL) != SCFGA_OK) {
		CFGA_TRACE1((stdout, "child[%d]: lock failure "
		    " - _exit(1)\n", s_getpid()));
		/*
		 * As a last resort, unlink the lock file. This is relatively
		 * safe as the child doesn't unquiesce the bus in this case.
		 */
		s_unlink(SCFGA_LOCK);
		_exit(1);
	}

	l_errno = 0;
	ret = devctl_cmd(apidp->hba_phys, SCFGA_BUS_UNQUIESCE, NULL, &l_errno);
	if (ret != SCFGA_OK) {
		if (l_errno == EALREADY)
			CFGA_TRACE3((stdout, "child[%d]: bus already "
			    "unquiesced: %s\n", s_getpid(), apidp->hba_phys));
		else
			CFGA_TRACE1((stdout, "child[%d]: unquiesce failed: "
			    "%s\n", s_getpid(), strerror(l_errno)));
	} else {
		CFGA_TRACE1((stdout, "child[%d]: unquiesced bus: %s\n",
		    s_getpid(), apidp->hba_phys));
	}

	s_unlink(SCFGA_LOCK);

	CFGA_TRACE2((stdout, "child[%d]: _exit(0)\n", s_getpid()));

	_exit(0);
}

static void
sigblk(sigset_t *osp)
{
	sigset_t set;

	(void) sigemptyset(&set);
	(void) sigemptyset(osp);
	(void) sigaddset(&set, SIGHUP);
	(void) sigaddset(&set, SIGINT);
	(void) sigaddset(&set, SIGQUIT);
	(void) sigaddset(&set, SIGTERM);
	(void) sigaddset(&set, SIGUSR1);
	(void) sigaddset(&set, SIGUSR2);
	(void) sigprocmask(SIG_BLOCK, &set, osp);
}

static void
sigunblk(sigset_t *osp)
{
	(void) sigprocmask(SIG_SETMASK, osp, NULL);
}

/*
 * Here is the algorithm used to ensure that a SCSI bus is not
 * left in the quiesced state:
 *
 *	lock quiesce mutex	// single threads this code
 *	open(O_CREAT|O_EXCL) lock file	// only 1 process at a time
 *	exclusive record lock on lock file
 *	fork1()
 *	quiesce bus
 *	do the physical hotplug operation
 *	unquiesce bus
 *	unlock record lock
 *		-> *child*
 *		-> wait for record lock
 *		-> unconditionally unquiesce bus
 *		-> unlink lock file
 *		-> exit
 *	wait for child to exit
 *	unlock quiesce mutex
 *
 * NOTE1: since record locks are per-process and a close() can
 * release a lock, to keep things MT-safe we need a quiesce mutex.
 *
 * NOTE2: To ensure that the child does not unquiesce a bus quiesced
 * by an unrelated cfgadm_scsi operation, exactly 1 process in the
 * system can be doing an implicit quiesce operation  The exclusive
 * creation of the lock file guarantees this.
 *
 * NOTE3: This works even if the parent process dumps core and/or is
 * abnormally terminated. If the parent dies before the child is
 * forked, the bus is not quiesced. If the parent dies after the
 * bus is quiesced, the child process will ensure that the bus is
 * unquiesced.
 */
static scfga_ret_t
dev_hotplug(
	apid_t *apidp,
	prompt_t *pt,
	cfga_flags_t flags,
	int do_quiesce,
	char **errstring)
{
	scfga_ret_t	ret;
	pid_t		cpid;
	int		fd;
	sigset_t	oset;

	assert(apidp->hba_phys != NULL);
	assert(apidp->path != NULL);

	/* If no quiesce required, prompt the user to do the operation */
	if (!do_quiesce)
		return (wait_for_hotplug(pt, CONF_NO_QUIESCE));

	(void) mutex_lock(&quiesce_mutex);

	ret = create_lock(&fd, pt->msgp, errstring);
	if (ret != SCFGA_OK) {
		(void) mutex_unlock(&quiesce_mutex);
		return (ret);
	}

	ret = syslock(fd, errstring);
	if (ret != SCFGA_OK) {
		goto bad;
	}

	/*
	 * block signals in the child. Parent may
	 * exit, causing signal to be sent to child.
	 */
	sigblk(&oset);

	switch (cpid = fork1()) {
		case 0:
			/* child */
			wait_and_cleanup(fd, apidp);
			_exit(0); /* paranoia */
			/*NOTREACHED*/
		case -1:
			cfga_err(errstring, errno, ERR_FORK, 0);
			sigunblk(&oset);
			ret = SCFGA_LIB_ERR;
			goto bad;
		default:
			/* parent */
			break;
	}

	sigunblk(&oset);

	/* We have forked successfully - this is the parent */
	ret = bus_quiesce(apidp, pt, errstring, flags);

	(void) close(fd);	/* also unlocks */

	wait_for_child(cpid);

	(void) mutex_unlock(&quiesce_mutex);

	return (ret);
bad:
	(void) close(fd);
	s_unlink(SCFGA_LOCK);
	(void) mutex_unlock(&quiesce_mutex);
	return (ret);
}

/*
 * Checks if HBA controls a critical file-system (/, /usr or swap)
 * This routine reads /etc/vfstab and is NOT foolproof.
 * If an error occurs, assumes that controller is NOT critical.
 */
static int
critical_ctrlr(const char *hba_phys)
{
	FILE *fp;
	struct vfstab vfst;
	int vfsret = 1, rv = -1;
	char *bufp;
	const size_t buflen = PATH_MAX;
	char mount[MAXPATHLEN], fstype[MAXPATHLEN], spec[MAXPATHLEN];


	if ((bufp = calloc(1, buflen)) == NULL) {
		return (0);
	}

	fp = NULL;
	if ((fp = fopen(ETC_VFSTAB, "r")) == NULL) {
		rv = 0;
		goto out;
	}

	while ((vfsret = getvfsent(fp, &vfst)) == 0) {

		(void) strcpy(mount, S_STR(vfst.vfs_mountp));
		(void) strcpy(fstype, S_STR(vfst.vfs_fstype));
		(void) strcpy(spec, S_STR(vfst.vfs_special));

		/* Ignore non-critical entries */
		if (strcmp(mount, "/") && strcmp(mount, "/usr") &&
		    strcmp(fstype, "swap")) {
			continue;
		}

		/* get physical path */
		if (realpath(spec, bufp) == NULL) {
			continue;
		}

		/* Check if critical partition is on the HBA */
		if (!(rv = hba_dev_cmp(hba_phys, bufp))) {
			break;
		}
	}

	rv = !vfsret;

	/*FALLTHRU*/
out:
	S_FREE(bufp);
	if (fp != NULL) {
		(void) fclose(fp);
	}
	return (rv);
}

/*
 * Convert bus state to receptacle state
 */
static cfga_stat_t
bus_devctl_to_recep_state(uint_t bus_dc_state)
{
	cfga_stat_t rs;

	switch (bus_dc_state) {
	case BUS_ACTIVE:
		rs = CFGA_STAT_CONNECTED;
		break;
	case BUS_QUIESCED:
	case BUS_SHUTDOWN:
		rs = CFGA_STAT_DISCONNECTED;
		break;
	default:
		rs = CFGA_STAT_NONE;
		break;
	}

	return (rs);
}

static int
add_dev(di_node_t node, void *arg)
{
	int ndevs, len;
	char *path, *p;
	struct larg *largp = (struct larg *)arg;

	/* ignore hba itself and all detached nodes */
	if (di_parent_node(node) == DI_NODE_NIL ||
	    di_node_state(node) < DS_ATTACHED)
		return (DI_WALK_CONTINUE);

	if ((path = di_devfs_path(node)) == NULL) {
		largp->ndevs = -1;
		return (DI_WALK_TERMINATE);
	}

	/* sizeof (DEVICES_DIR) includes the null terminator */
	len = strlen(path) + sizeof (DEVICES_DIR);
	if ((p = malloc(len)) == NULL) {
		di_devfs_path_free(path);
		largp->ndevs = -1;
		return (DI_WALK_TERMINATE);
	}
	(void) snprintf(p, len, "%s%s", DEVICES_DIR, path);
	di_devfs_path_free(path);

	/* ignore device to be excluded */
	if (largp->dev && strcmp(largp->dev, p) == 0) {
		free(p);
		return (DI_WALK_CONTINUE);
	}

	/* grow dev_list to allow room for one more device */
	if (alloc_dev_list(largp) != 0) {
		free(p);
		return (DI_WALK_TERMINATE);
	}
	ndevs = largp->ndevs;
	largp->ndevs++;
	largp->dev_list[ndevs] = p;
	largp->dev_list[ndevs + 1] = NULL;
	return (DI_WALK_CONTINUE);
}

/*
 * Get list of children excluding dev_excl (if not null).
 */
static int
get_hba_children(char *bus_path, char *dev_excl, char ***dev_listp)
{
	int err, ret;
	walkarg_t u;
	struct larg larg;

	*dev_listp = NULL;

	u.node_args.flags = DI_WALK_CLDFIRST;
	u.node_args.fcn = add_dev;

	larg.ndevs = 0;
	larg.nelem = 0;
	larg.dev = dev_excl;
	larg.dev_list = NULL;

	ret = walk_tree(bus_path, &larg, DINFOSUBTREE, &u, SCFGA_WALK_NODE,
	    &err);
	if (larg.ndevs == -1) {
		free_dev_list(larg.dev_list);
		return (SCFGA_ERR);
	}
	*dev_listp = larg.dev_list;
	return (ret);
}

static char *
get_node_path(char *minor_path)
{
	char *path, *cp;

	if ((path = strdup(minor_path)) == NULL)
		return (NULL);
	if ((cp = strrchr(path, ':')) != NULL)
		*cp = '\0';
	return (path);
}

/*
 * Ensure largp->dev_list has room for one more device.
 * Returns 0 on success, -1 on failure.
 */
static int
alloc_dev_list(struct larg *largp)
{
	int nelem;
	char **p;

	if (largp->nelem > largp->ndevs + 2)	/* +1 for NULL termination */
		return (0);

	nelem =  largp->nelem + 16;
	p = realloc(largp->dev_list, nelem * sizeof (char *));
	if (p == NULL)
		return (-1);

	largp->dev_list = p;
	largp->nelem = nelem;
	return (0);
}

static void
free_dev_list_elements(char **dev_list)
{
	while (*dev_list) {
		free(*dev_list);
		dev_list++;
	}
}

static void
free_dev_list(char **dev_list)
{
	if (dev_list == NULL)
		return;

	free_dev_list_elements(dev_list);
	free(dev_list);
}