summaryrefslogtreecommitdiff
path: root/usr/src/lib/cfgadm_plugins/sbd/common/ap_seq.c
blob: 139ff66a2479810e49b7d5ae29f52a73de0f2cfd (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
/*
 * 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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <macros.h>
#include <dirent.h>
#include <libgen.h>
#include <libdevinfo.h>
#define	CFGA_PLUGIN_LIB
#include <config_admin.h>
#include "ap.h"

#ifdef	__x86
#include <libscf_priv.h>

static int fastreboot_disabled;
#endif	/* __x86 */

static cfga_err_t
ap_suspend_check(apd_t *a, int cmd, int first, int last, int *suspend)
{
	int c;
	int rc;
	int skip;
	int check;

	skip = a->opts.skip;

	/*
	 * Check if any of the steps in the sequence
	 * may require a suspension of service and ask
	 * the user to confirm.
	 */
	for (check = 0, c = first; c <= last; c++)
		if (mask(c) & skip)
			continue;
		else if ((rc = ap_suspend_query(a, c, &check)) != CFGA_OK)
			return (rc);

	*suspend = check;

	/*
	 * If a suspend is required, ask for user confirmation.
	 * The force flag overrides the user confirmation.
	 */
	if (check && (!ap_getopt(a, OPT_FORCE)) && (!ap_confirm(a))) {
		ap_err(a, ERR_CMD_NACK, cmd);
		return (CFGA_NACK);
	}

	return (CFGA_OK);
}

#define	AP_SEQ_OK	0
#define	AP_SEQ_NULL	1
#define	AP_SEQ_FAIL	-1

/*
 * Sequence a cfgadm state change command into driver commands.
 * The rstate and ostate of the AP are needed at this point
 * in order to compute the proper sequence.
 */
static int
ap_seq_get(apd_t *a, int cmd, int *first, int *last)
{
	int done = 0;
	int f = CMD_NONE;
	int l = CMD_NONE;
	cfga_stat_t rs, os;

	ap_state(a, &rs, &os);

	switch (rs) {
	case CFGA_STAT_EMPTY:
		switch (os) {
		case CFGA_STAT_UNCONFIGURED:
			switch (cmd) {
			case CMD_UNCONFIGURE:
				done++;
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		break;
	case CFGA_STAT_DISCONNECTED:
		switch (os) {
		case CFGA_STAT_UNCONFIGURED:
			switch (cmd) {
			case CMD_DISCONNECT:
				/*
				 * skip the disconnect command since
				 * the rstate is already disconnected
				 */
				f = CMD_DISCONNECT;
				a->opts.skip |= mask(CMD_DISCONNECT);
				l = CMD_UNASSIGN;
				break;
			case CMD_UNCONFIGURE:
				done++;
				break;
			case CMD_CONNECT:
				f = CMD_ASSIGN;
				l = CMD_CONNECT;
				break;
			case CMD_CONFIGURE:
				f = CMD_ASSIGN;
				l = CMD_RCM_CAP_ADD;
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		break;
	case CFGA_STAT_CONNECTED:
		switch (os) {
		case CFGA_STAT_UNCONFIGURED:
			switch (cmd) {
			case CMD_CONNECT:
			case CMD_UNCONFIGURE:
				done++;
				break;
			case CMD_DISCONNECT:
				f = CMD_DISCONNECT;
				l = CMD_UNASSIGN;
				break;
			case CMD_CONFIGURE:
				f = CMD_CONFIGURE;
				l = CMD_RCM_CAP_ADD;
				break;
			default:
				break;
			}
			break;
		case CFGA_STAT_CONFIGURED:
			switch (cmd) {
			case CMD_CONNECT:
				done++;
				break;
			case CMD_DISCONNECT:
				f = CMD_SUSPEND_CHECK;
				l = CMD_UNASSIGN;
				break;
			case CMD_CONFIGURE:
				f = CMD_CONFIGURE;
				l = CMD_RCM_CAP_ADD;
				break;
			case CMD_UNCONFIGURE:
				f = CMD_SUSPEND_CHECK;
				l = CMD_RCM_CAP_NOTIFY;
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}

	if (f == CMD_NONE) {
		if (done)
			return (AP_SEQ_NULL);
		ap_err(a, ERR_TRANS_INVAL, cmd);
		return (AP_SEQ_FAIL);
	}

	*first = f;
	*last = l;

	DBG("ap_seq(%d, %d, %d, %p, %p) = (%d, %d)\n",
	    rs, os, cmd, (void *)first, (void *)last, f, l);

	return (AP_SEQ_OK);
}

#define	DBG_RECOVER_MSG(f, l) \
	DBG("Sequencing recovery: first = %s, last = %s\n", \
	ap_cmd_name(f), ap_cmd_name(l))

cfga_err_t
ap_seq_exec(apd_t *a, int cmd, int first, int last)
{
	int c;
	int skip;
	int suspend;
	int resume;
	cfga_err_t rc;
	int recover_f = CMD_NONE;	/* first recovery cmd */
	int recover_l = CMD_NONE;	/* last recovery cmd */


	suspend = 0;
	resume = 0;

	skip = a->opts.skip;

	/*
	 * The unassign step is skipped unless explicity requested
	 * either by a -x request or as an option to a disconnect
	 * request.
	 */
	if (cmd != CMD_UNASSIGN && ap_getopt(a, OPT_UNASSIGN) == 0)
		skip |= mask(CMD_UNASSIGN);

	/*
	 * Check for platform options
	 */
	rc = ap_platopts_check(a, first, last);

	if (rc != CFGA_OK) {
		goto done;
	}

	for (c = first; c <= last; c++) {
		if (mask(c) & skip) {
			ap_msg(a, MSG_SKIP, c, a->target);
			continue;
		}

		DBG("exec %s\n", ap_cmd_name(c));

		/*
		 * If the suspend operation does not
		 * succeed, resume any devices already
		 * suspended as well as the device on
		 * which the operation failed.
		 */
		switch (c) {
		case CMD_SUSPEND_CHECK:
			/*
			 * Check whether the user allows a suspend
			 * operation if the suspend is required.
			 * Next step is to allow RCM clients to
			 * interpose on the suspend operation.
			 */
			rc = ap_suspend_check(a, cmd,
			    first + 1, last, &suspend);
			break;
		case CMD_RCM_SUSPEND:
			if (suspend && ((rc = ap_rcm_ctl(a, c)) == CFGA_OK)) {
				/*
				 * Mark the fact that a suspend operation
				 * is required, and that RCM clients have
				 * allowed the suspend.
				 */
				ap_setopt(a, OPT_SUSPEND_OK);
				resume++;
			}
			break;
		case CMD_RCM_RESUME:
			if (resume) {
				(void) ap_rcm_ctl(a, c);
				resume--;
			}
			break;
		case CMD_RCM_OFFLINE:
		case CMD_RCM_CAP_DEL:
			rc = ap_rcm_ctl(a, c);
			break;
		case CMD_RCM_ONLINE:
		case CMD_RCM_CAP_ADD:
		case CMD_RCM_REMOVE:
		case CMD_RCM_CAP_NOTIFY:
			(void) ap_rcm_ctl(a, c);
			break;

#ifdef	__x86
		/*
		 * Disable fast reboot if a CPU/MEM/IOH hotplug event happens.
		 * Note: this is a temporary solution and will be revised when
		 * fast reboot can support CPU/MEM/IOH DR operations in the
		 * future.
		 *
		 * ACPI BIOS generates some static ACPI tables, such as MADT,
		 * SRAT and SLIT, to describe the system hardware configuration
		 * on power-on. When a CPU/MEM/IOH hotplug event happens, those
		 * static tables won't be updated and will become stale.
		 *
		 * If we reset the system by fast reboot, BIOS will have no
		 * chance to regenerate those staled static tables. Fast reboot
		 * can't tolerate such inconsistency between staled ACPI tables
		 * and real hardware configuration yet.
		 *
		 * A temporary solution is introduced to disable fast reboot if
		 * CPU/MEM/IOH hotplug event happens. This solution should be
		 * revised when fast reboot is enhanced to support CPU/MEM/IOH
		 * DR operations.
		 */
		case CMD_ASSIGN:
		case CMD_POWERON:
		case CMD_POWEROFF:
		case CMD_UNASSIGN:
			if (!fastreboot_disabled &&
			    scf_fastreboot_default_set_transient(B_FALSE) ==
			    SCF_SUCCESS) {
				fastreboot_disabled = 1;
			}
#endif	/* __x86 */
			/* FALLTHROUGH */

		default:
			rc = ap_ioctl(a, c);
			break;
		}

		if (rc != CFGA_OK)
			break;

	}
done:

	if (resume)
		(void) ap_rcm_ctl(a, CMD_RCM_RESUME);

	/*
	 * Check if any operations failed. If so, attempt to rollback
	 * to previously known states.
	 * Note: The rollback is currently limited to RCM operations.
	 */
	if (rc != CFGA_OK) {
		if (c == CMD_UNCONFIGURE ||
		    c == CMD_RCM_OFFLINE ||
		    c == CMD_RCM_CAP_DEL) {
			DBG("ap_seq_exec: %s failed\n", ap_cmd_name(c));

			switch (c) {
			case CMD_UNCONFIGURE:
				/*
				 * If the unconfigure operation fails, perform
				 * an RCM_ONLINE and RCM_CAP_NOTIFY only. This
				 * keeps RCM clients consistent with the domain.
				 */
				recover_f = CMD_RCM_ONLINE;
				recover_l = CMD_RCM_ONLINE;
				DBG_RECOVER_MSG(recover_f, recover_l);
				(void) ap_seq_exec(a, cmd, recover_f,
				    recover_l);

				recover_f = CMD_RCM_CAP_NOTIFY;
				recover_l = CMD_RCM_CAP_NOTIFY;
				DBG_RECOVER_MSG(recover_f, recover_l);
				(void) ap_seq_exec(a, cmd, recover_f,
				    recover_l);
				break;
			case CMD_RCM_OFFLINE:
				recover_f = CMD_RCM_ONLINE;
				recover_l = CMD_RCM_CAP_ADD;
				DBG_RECOVER_MSG(recover_f, recover_l);
				(void) ap_seq_exec(a, cmd, recover_f,
				    recover_l);
				break;
			case CMD_RCM_CAP_DEL:
				recover_f = CMD_RCM_CAP_ADD;
				recover_l = CMD_RCM_CAP_ADD;
				DBG_RECOVER_MSG(recover_f, recover_l);
				(void) ap_seq_exec(a, cmd, recover_f,
				    recover_l);
				break;
			default:
				break;
			}

			DBG("recovery complete!\n");
		}
	}
	return (rc);
}

cfga_err_t
ap_cmd_exec(apd_t *a, int cmd)
{
	return (ap_seq_exec(a, cmd, cmd, cmd));
}

cfga_err_t
ap_cmd_seq(apd_t *a, int cmd)
{
	int first, last;
	cfga_err_t rc;

	switch (ap_seq_get(a, cmd, &first, &last)) {
	case AP_SEQ_OK:
		rc = ap_seq_exec(a, cmd, first, last);
		break;
	case AP_SEQ_NULL:
		rc = CFGA_OK;
		break;
	case AP_SEQ_FAIL:
	default:
		rc = CFGA_LIB_ERROR;
		break;
	}

	return (rc);
}