summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4v/io/platsvc.c
blob: db416e79c5499ed449c33b7506628ca97b85b0f3 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * sun4v Platform Services Module
 */

#include <sys/modctl.h>
#include <sys/cmn_err.h>
#include <sys/machsystm.h>
#include <sys/note.h>
#include <sys/uadmin.h>
#include <sys/ds.h>
#include <sys/platsvc.h>
#include <sys/ddi.h>
#include <sys/suspend.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/drctl.h>

/*
 * Debugging routines
 */
#ifdef DEBUG
uint_t ps_debug = 0x0;
#define	DBG	if (ps_debug) printf
#else /* DEBUG */
#define	DBG	_NOTE(CONSTCOND) if (0) printf
#endif /* DEBUG */

/*
 * Time resolution conversions.
 */
#define	MS2NANO(x)	((x) * MICROSEC)
#define	MS2SEC(x)	((x) / MILLISEC)
#define	MS2MIN(x)	(MS2SEC(x) / 60)
#define	SEC2HZ(x)	(drv_usectohz((x) * MICROSEC))

/*
 * Domains Services interaction
 */
static ds_svc_hdl_t	ds_md_handle;
static ds_svc_hdl_t	ds_shutdown_handle;
static ds_svc_hdl_t	ds_panic_handle;
static ds_svc_hdl_t	ds_suspend_handle;

static ds_ver_t		ps_vers[] = {{ 1, 0 }};
#define	PS_NVERS	(sizeof (ps_vers) / sizeof (ps_vers[0]))

static ds_capability_t ps_md_cap = {
	"md-update",		/* svc_id */
	ps_vers,		/* vers */
	PS_NVERS		/* nvers */
};

static ds_capability_t ps_shutdown_cap = {
	"domain-shutdown",	/* svc_id */
	ps_vers,		/* vers */
	PS_NVERS		/* nvers */
};

static ds_capability_t ps_panic_cap = {
	"domain-panic",		/* svc_id */
	ps_vers,		/* vers */
	PS_NVERS		/* nvers */
};

static ds_capability_t ps_suspend_cap = {
	"domain-suspend",	/* svc_id */
	ps_vers,		/* vers */
	PS_NVERS		/* nvers */
};

static void ps_reg_handler(ds_cb_arg_t arg, ds_ver_t *ver, ds_svc_hdl_t hdl);
static void ps_unreg_handler(ds_cb_arg_t arg);

static void ps_md_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen);
static void ps_shutdown_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen);
static void ps_panic_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen);
static void ps_suspend_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen);

static ds_clnt_ops_t ps_md_ops = {
	ps_reg_handler,			/* ds_reg_cb */
	ps_unreg_handler,		/* ds_unreg_cb */
	ps_md_data_handler,		/* ds_data_cb */
	&ds_md_handle			/* cb_arg */
};

static ds_clnt_ops_t ps_shutdown_ops = {
	ps_reg_handler,			/* ds_reg_cb */
	ps_unreg_handler,		/* ds_unreg_cb */
	ps_shutdown_data_handler,	/* ds_data_cb */
	&ds_shutdown_handle		/* cb_arg */
};

static ds_clnt_ops_t ps_panic_ops = {
	ps_reg_handler,			/* ds_reg_cb */
	ps_unreg_handler,		/* ds_unreg_cb */
	ps_panic_data_handler,		/* ds_data_cb */
	&ds_panic_handle		/* cb_arg */
};

static ds_clnt_ops_t ps_suspend_ops = {
	ps_reg_handler,			/* ds_reg_cb */
	ps_unreg_handler,		/* ds_unreg_cb */
	ps_suspend_data_handler,	/* ds_data_cb */
	&ds_suspend_handle		/* cb_arg */
};

static int ps_init(void);
static void ps_fini(void);

/*
 * Power down timeout value of 5 minutes.
 */
#define	PLATSVC_POWERDOWN_DELAY		1200

/*
 * Set to true if OS suspend is supported. If OS suspend is not
 * supported, the suspend service will not be started.
 */
static boolean_t ps_suspend_enabled = B_FALSE;

/*
 * Suspend service request handling
 */
typedef struct ps_suspend_data {
	void		*buf;
	size_t		buflen;
} ps_suspend_data_t;

static kmutex_t ps_suspend_mutex;
static kcondvar_t ps_suspend_cv;

static ps_suspend_data_t *ps_suspend_data = NULL;
static boolean_t ps_suspend_thread_exit = B_FALSE;
static kthread_t *ps_suspend_thread = NULL;

static void ps_suspend_sequence(ps_suspend_data_t *data);
static void ps_suspend_thread_func(void);

/*
 * The DELAY timeout is the time (in seconds) to wait for the
 * suspend service to be re-registered after a suspend/resume
 * operation. The INTVAL time is the time (in seconds) to wait
 * between retry attempts when sending the post-suspend message
 * after a suspend/resume operation.
 */
#define	PLATSVC_SUSPEND_REREG_DELAY	60
#define	PLATSVC_SUSPEND_RETRY_INTVAL	1
static int ps_suspend_rereg_delay = PLATSVC_SUSPEND_REREG_DELAY;
static int ps_suspend_retry_intval = PLATSVC_SUSPEND_RETRY_INTVAL;


static struct modlmisc modlmisc = {
	&mod_miscops,
	"sun4v Platform Services"
};

static struct modlinkage modlinkage = {
	MODREV_1,
	(void *)&modlmisc,
	NULL
};

int
_init(void)
{
	int	rv;

	if ((rv = ps_init()) != 0)
		return (rv);

	if ((rv = mod_install(&modlinkage)) != 0)
		ps_fini();

	return (rv);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

int platsvc_allow_unload;

int
_fini(void)
{
	int	status;

	if (platsvc_allow_unload == 0)
		return (EBUSY);

	if ((status = mod_remove(&modlinkage)) == 0)
		ps_fini();

	return (status);
}

static int
ps_init(void)
{
	int	rv;
	extern int mdeg_init(void);
	extern void mdeg_fini(void);

	/* register with domain services framework */
	rv = ds_cap_init(&ps_md_cap, &ps_md_ops);
	if (rv != 0) {
		cmn_err(CE_WARN, "ds_cap_init md-update failed: %d", rv);
		return (rv);
	}

	rv = mdeg_init();
	if (rv != 0) {
		(void) ds_cap_fini(&ps_md_cap);
		return (rv);
	}

	rv = ds_cap_init(&ps_shutdown_cap, &ps_shutdown_ops);
	if (rv != 0) {
		cmn_err(CE_WARN, "ds_cap_init domain-shutdown failed: %d", rv);
		mdeg_fini();
		(void) ds_cap_fini(&ps_md_cap);
		return (rv);
	}

	rv = ds_cap_init(&ps_panic_cap, &ps_panic_ops);
	if (rv != 0) {
		cmn_err(CE_WARN, "ds_cap_init domain-panic failed: %d", rv);
		(void) ds_cap_fini(&ps_md_cap);
		mdeg_fini();
		(void) ds_cap_fini(&ps_shutdown_cap);
		return (rv);
	}

	ps_suspend_enabled = suspend_supported();

	if (ps_suspend_enabled) {
		mutex_init(&ps_suspend_mutex, NULL, MUTEX_DEFAULT, NULL);
		cv_init(&ps_suspend_cv, NULL, CV_DEFAULT, NULL);
		ps_suspend_thread_exit = B_FALSE;

		rv = ds_cap_init(&ps_suspend_cap, &ps_suspend_ops);
		if (rv != 0) {
			cmn_err(CE_WARN, "ds_cap_init domain-suspend failed: "
			    "%d", rv);
			(void) ds_cap_fini(&ps_md_cap);
			mdeg_fini();
			(void) ds_cap_fini(&ps_shutdown_cap);
			(void) ds_cap_fini(&ps_panic_cap);
			mutex_destroy(&ps_suspend_mutex);
			cv_destroy(&ps_suspend_cv);
			return (rv);
		}

		ps_suspend_thread = thread_create(NULL, 2 * DEFAULTSTKSZ,
		    ps_suspend_thread_func, NULL, 0, &p0, TS_RUN, minclsyspri);
	}

	return (0);
}

static void
ps_fini(void)
{
	extern void mdeg_fini(void);

	/*
	 * Stop incoming requests from Zeus
	 */
	(void) ds_cap_fini(&ps_md_cap);
	(void) ds_cap_fini(&ps_shutdown_cap);
	(void) ds_cap_fini(&ps_panic_cap);

	if (ps_suspend_enabled) {
		(void) ds_cap_fini(&ps_suspend_cap);
		if (ps_suspend_thread != NULL) {
			mutex_enter(&ps_suspend_mutex);
			ps_suspend_thread_exit = B_TRUE;
			cv_signal(&ps_suspend_cv);
			mutex_exit(&ps_suspend_mutex);

			thread_join(ps_suspend_thread->t_did);
			ps_suspend_thread = NULL;

			mutex_destroy(&ps_suspend_mutex);
			cv_destroy(&ps_suspend_cv);
		}
	}

	mdeg_fini();
}

static void
ps_md_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen)
{
	extern int mach_descrip_update(void);
	extern void mdeg_notify_clients(void);
	extern void recalc_xc_timeouts(void);

	ds_svc_hdl_t		 ds_handle = ds_md_handle;
	platsvc_md_update_req_t	 *msg = buf;
	platsvc_md_update_resp_t resp_msg;
	uint_t			 rv;

	if (arg == NULL)
		return;

	if (ds_handle == DS_INVALID_HDL) {
		DBG("ps_md_data_handler: DS handle no longer valid\n");
		return;
	}

	if (msg == NULL || buflen != sizeof (platsvc_md_update_req_t)) {
		resp_msg.req_num = 0;
		resp_msg.result = MD_UPDATE_INVALID_MSG;
		if ((rv = ds_cap_send(ds_handle, &resp_msg,
		    sizeof (resp_msg))) != 0) {
			cmn_err(CE_NOTE, "md ds_cap_send failed (%d)", rv);
		}
		return;
	}

	DBG("MD Reload...\n");
	if (mach_descrip_update()) {
		cmn_err(CE_WARN, "MD reload failed\n");
		return;
	}

	recalc_xc_timeouts();

	/*
	 * notify registered clients that MD has
	 * been updated
	 */
	mdeg_notify_clients();

	resp_msg.req_num = msg->req_num;
	resp_msg.result = MD_UPDATE_SUCCESS;
	if ((rv = ds_cap_send(ds_handle, &resp_msg, sizeof (resp_msg))) != 0) {
		cmn_err(CE_NOTE, "md ds_cap_send resp failed (%d)", rv);
	}
}

static void
ps_shutdown_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen)
{
	ds_svc_hdl_t		ds_handle = ds_shutdown_handle;
	platsvc_shutdown_req_t	*msg = buf;
	platsvc_shutdown_resp_t	resp_msg;
	uint_t			rv;
	hrtime_t		start;

	if (arg == NULL)
		return;

	if (ds_handle == DS_INVALID_HDL) {
		DBG("ps_shutdown_data_handler: DS handle no longer valid\n");
		return;
	}

	if (msg == NULL || buflen != sizeof (platsvc_shutdown_req_t)) {
		resp_msg.req_num = 0;
		resp_msg.result = DOMAIN_SHUTDOWN_INVALID_MSG;
		resp_msg.reason[0] = '\0';
		if ((rv = ds_cap_send(ds_handle, &resp_msg,
		    sizeof (resp_msg))) != 0) {
			cmn_err(CE_NOTE, "shutdown ds_cap_send failed (%d)",
			    rv);
		}
		return;
	}

	resp_msg.req_num = msg->req_num;
	resp_msg.result = DOMAIN_SHUTDOWN_SUCCESS;
	resp_msg.reason[0] = '\0';

	if ((rv = ds_cap_send(ds_handle, &resp_msg, sizeof (resp_msg))) != 0) {
		cmn_err(CE_NOTE, "shutdown ds_cap_send resp failed (%d)", rv);
	}

	/*
	 * Honor the ldoms manager's shutdown delay requirement.
	 */
	cmn_err(CE_NOTE, "shutdown requested by ldom manager, "
	    "system shutdown in %d minutes", MS2MIN(msg->delay));

	start = gethrtime();
	while (gethrtime() - start < MS2NANO(msg->delay))
		;

	(void) kadmin(A_SHUTDOWN, AD_POWEROFF, NULL, kcred);
}


static void
ps_panic_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen)
{
	ds_svc_hdl_t		ds_handle = ds_panic_handle;
	platsvc_panic_req_t	*msg = buf;
	platsvc_panic_resp_t	resp_msg;
	uint_t			rv;

	if (arg == NULL)
		return;

	if (ds_handle == DS_INVALID_HDL) {
		DBG("ps_panic_data_handler: DS handle no longer valid\n");
		return;
	}

	if (msg == NULL || buflen != sizeof (platsvc_panic_req_t)) {
		resp_msg.req_num = 0;
		resp_msg.result = DOMAIN_PANIC_INVALID_MSG;
		resp_msg.reason[0] = '\0';
		if ((rv = ds_cap_send(ds_handle, &resp_msg,
		    sizeof (resp_msg))) != 0) {
			cmn_err(CE_NOTE, "panic ds_cap_send resp failed (%d)",
			    rv);
		}
		return;
	}

	resp_msg.req_num = msg->req_num;
	resp_msg.result = DOMAIN_PANIC_SUCCESS;
	resp_msg.reason[0] = '\0';
	if ((rv = ds_cap_send(ds_handle, &resp_msg, sizeof (resp_msg))) != 0) {
		cmn_err(CE_NOTE, "panic ds_cap_send resp failed (%d)", rv);
	}

	cmn_err(CE_PANIC, "Panic forced by ldom manager");
	_NOTE(NOTREACHED)
}

/*
 * Send a suspend response message. If a timeout is specified, wait
 * intval seconds between attempts to send the message. The timeout
 * and intval arguments are in seconds.
 */
static void
ps_suspend_send_response(ds_svc_hdl_t *ds_handle, uint64_t req_num,
    uint32_t result, uint32_t rec_result, char *reason, int timeout,
    int intval)
{
	platsvc_suspend_resp_t	*resp;
	size_t			reason_length;
	int			tries = 0;
	int			rv = -1;
	time_t			deadline;

	if (reason == NULL) {
		reason_length = 0;
	} else {
		/* Get number of non-NULL bytes */
		reason_length = strnlen(reason, SUSPEND_MAX_REASON_SIZE - 1);
		ASSERT(reason[reason_length] == '\0');
		/* Account for NULL terminator */
		reason_length++;
	}

	resp = (platsvc_suspend_resp_t *)
	    kmem_zalloc(sizeof (platsvc_suspend_resp_t) + reason_length,
	    KM_SLEEP);

	resp->req_num = req_num;
	resp->result = result;
	resp->rec_result = rec_result;
	if (reason_length > 0) {
		bcopy(reason, &resp->reason, reason_length - 1);
		/* Ensure NULL terminator is present */
		resp->reason[reason_length] = '\0';
	}

	if (timeout == 0) {
		tries++;
		rv = ds_cap_send(*ds_handle, resp,
		    sizeof (platsvc_suspend_resp_t) + reason_length);
	} else {
		deadline = gethrestime_sec() + timeout;
		do {
			ds_svc_hdl_t hdl;
			/*
			 * Copy the handle so we can ensure we never pass
			 * an invalid handle to ds_cap_send. We don't want
			 * to trigger warning messages just because the
			 * service was temporarily unregistered.
			 */
			if ((hdl = *ds_handle) == DS_INVALID_HDL) {
				delay(SEC2HZ(intval));
			} else if ((rv = ds_cap_send(hdl, resp,
			    sizeof (platsvc_suspend_resp_t) +
			    reason_length)) != 0) {
				tries++;
				delay(SEC2HZ(intval));
			}
		} while ((rv != 0) && (gethrestime_sec() < deadline));
	}

	if (rv != 0) {
		cmn_err(CE_NOTE, "suspend ds_cap_send resp failed (%d) "
		    "sending message: %d, attempts: %d", rv, resp->result,
		    tries);
	}

	kmem_free(resp, sizeof (platsvc_suspend_resp_t) + reason_length);
}

/*
 * Handle data coming in for the suspend service. The suspend is
 * sequenced by the ps_suspend_thread, but perform some checks here
 * to make sure that the request is a valid request message and that
 * a suspend operation is not already in progress.
 */
/*ARGSUSED*/
static void
ps_suspend_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen)
{
	platsvc_suspend_req_t	*msg = buf;

	if (arg == NULL)
		return;

	if (ds_suspend_handle == DS_INVALID_HDL) {
		DBG("ps_suspend_data_handler: DS handle no longer valid\n");
		return;
	}

	/* Handle invalid requests */
	if (msg == NULL || buflen != sizeof (platsvc_suspend_req_t) ||
	    msg->type != DOMAIN_SUSPEND_SUSPEND) {
		ps_suspend_send_response(&ds_suspend_handle, msg->req_num,
		    DOMAIN_SUSPEND_INVALID_MSG, DOMAIN_SUSPEND_REC_SUCCESS,
		    NULL, 0, 0);
		return;
	}

	/*
	 * If ps_suspend_thread_exit is set, ds_cap_fini has been
	 * called and we shouldn't be receving data. Handle this unexpected
	 * case by returning without sending a response.
	 */
	if (ps_suspend_thread_exit) {
		DBG("ps_suspend_data_handler: ps_suspend_thread is exiting\n");
		return;
	}

	mutex_enter(&ps_suspend_mutex);

	/* If a suspend operation is in progress, abort now */
	if (ps_suspend_data != NULL) {
		mutex_exit(&ps_suspend_mutex);
		ps_suspend_send_response(&ds_suspend_handle, msg->req_num,
		    DOMAIN_SUSPEND_INPROGRESS, DOMAIN_SUSPEND_REC_SUCCESS,
		    NULL, 0, 0);
		return;
	}

	ps_suspend_data = kmem_alloc(sizeof (ps_suspend_data_t), KM_SLEEP);
	ps_suspend_data->buf = kmem_alloc(buflen, KM_SLEEP);
	ps_suspend_data->buflen = buflen;
	bcopy(buf, ps_suspend_data->buf, buflen);

	cv_signal(&ps_suspend_cv);
	mutex_exit(&ps_suspend_mutex);
}

/*
 * Schedule the suspend operation by calling the pre-suspend, suspend,
 * and post-suspend functions. When sending back response messages, we
 * only use a timeout for the post-suspend response because after
 * a resume, domain services will be re-registered and we may not
 * be able to send the response immediately.
 */
static void
ps_suspend_sequence(ps_suspend_data_t *data)
{
	platsvc_suspend_req_t	*msg;
	uint32_t		rec_result;
	char			*error_reason;
	boolean_t		recovered = B_TRUE;
	uint_t			rv = 0;
	int			dr_block;

	ASSERT(data != NULL);

	msg = data->buf;
	error_reason = (char *)kmem_zalloc(SUSPEND_MAX_REASON_SIZE, KM_SLEEP);

	/*
	 * Abort the suspend if a DR operation is in progress. Otherwise,
	 * continue whilst blocking any new DR operations.
	 */
	if ((dr_block = drctl_tryblock()) == 0) {
		/* Pre-suspend */
		rv = suspend_pre(error_reason, SUSPEND_MAX_REASON_SIZE,
		    &recovered);
	} else {
		/* A DR operation is in progress */
		(void) strncpy(error_reason, DOMAIN_SUSPEND_DR_ERROR_STR,
		    SUSPEND_MAX_REASON_SIZE);
	}

	if (dr_block != 0 || rv != 0) {
		rec_result = (recovered ? DOMAIN_SUSPEND_REC_SUCCESS :
		    DOMAIN_SUSPEND_REC_FAILURE);

		ps_suspend_send_response(&ds_suspend_handle, msg->req_num,
		    DOMAIN_SUSPEND_PRE_FAILURE, rec_result, error_reason, 0, 0);

		if (dr_block == 0)
			drctl_unblock();
		kmem_free(error_reason, SUSPEND_MAX_REASON_SIZE);
		return;
	}

	ps_suspend_send_response(&ds_suspend_handle, msg->req_num,
	    DOMAIN_SUSPEND_PRE_SUCCESS, 0, NULL, 0, 0);

	/* Suspend */
	rv = suspend_start(error_reason, SUSPEND_MAX_REASON_SIZE);
	if (rv != 0) {
		rec_result = (suspend_post(NULL, 0) == 0 ?
		    DOMAIN_SUSPEND_REC_SUCCESS : DOMAIN_SUSPEND_REC_FAILURE);

		ps_suspend_send_response(&ds_suspend_handle, msg->req_num,
		    DOMAIN_SUSPEND_SUSPEND_FAILURE, rec_result, error_reason,
		    0, 0);

		drctl_unblock();
		kmem_free(error_reason, SUSPEND_MAX_REASON_SIZE);
		return;
	}

	/* Post-suspend */
	rv = suspend_post(error_reason, SUSPEND_MAX_REASON_SIZE);
	if (rv != 0) {
		ps_suspend_send_response(&ds_suspend_handle, msg->req_num,
		    DOMAIN_SUSPEND_POST_FAILURE, 0, error_reason,
		    ps_suspend_rereg_delay, ps_suspend_retry_intval);
	} else {
		ps_suspend_send_response(&ds_suspend_handle, msg->req_num,
		    DOMAIN_SUSPEND_POST_SUCCESS, 0, error_reason,
		    ps_suspend_rereg_delay, ps_suspend_retry_intval);
	}

	drctl_unblock();
	kmem_free(error_reason, SUSPEND_MAX_REASON_SIZE);
}

/*
 * Wait for a suspend request or for ps_suspend_thread_exit to be set.
 */
static void
ps_suspend_thread_func(void)
{
	mutex_enter(&ps_suspend_mutex);

	while (ps_suspend_thread_exit == B_FALSE) {

		if (ps_suspend_data == NULL) {
			cv_wait(&ps_suspend_cv, &ps_suspend_mutex);
			continue;
		}

		mutex_exit(&ps_suspend_mutex);
		ps_suspend_sequence(ps_suspend_data);
		mutex_enter(&ps_suspend_mutex);

		kmem_free(ps_suspend_data->buf, ps_suspend_data->buflen);
		kmem_free(ps_suspend_data, sizeof (ps_suspend_data_t));
		ps_suspend_data = NULL;
	}

	mutex_exit(&ps_suspend_mutex);

	thread_exit();
}

static void
ps_reg_handler(ds_cb_arg_t arg, ds_ver_t *ver, ds_svc_hdl_t hdl)
{
	DBG("ps_reg_handler: arg=0x%p, ver=%d.%d, hdl=0x%lx\n",
	    arg, ver->major, ver->minor, hdl);

	if ((ds_svc_hdl_t *)arg == &ds_md_handle)
		ds_md_handle = hdl;
	if ((ds_svc_hdl_t *)arg == &ds_shutdown_handle)
		ds_shutdown_handle = hdl;
	if ((ds_svc_hdl_t *)arg == &ds_panic_handle)
		ds_panic_handle = hdl;
	if ((ds_svc_hdl_t *)arg == &ds_suspend_handle)
		ds_suspend_handle = hdl;
}

static void
ps_unreg_handler(ds_cb_arg_t arg)
{
	DBG("ps_unreg_handler: arg=0x%p\n", arg);

	if ((ds_svc_hdl_t *)arg == &ds_md_handle)
		ds_md_handle = DS_INVALID_HDL;
	if ((ds_svc_hdl_t *)arg == &ds_shutdown_handle)
		ds_shutdown_handle = DS_INVALID_HDL;
	if ((ds_svc_hdl_t *)arg == &ds_panic_handle)
		ds_panic_handle = DS_INVALID_HDL;
	if ((ds_svc_hdl_t *)arg == &ds_suspend_handle)
		ds_suspend_handle = DS_INVALID_HDL;
}