summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/cpr/cpr_driver.c
blob: 442473c7cad7333c5a6314ac67bbb0bf2ce4d17e (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
/*
 * 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 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * CPR driver support routines
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/epm.h>
#include <sys/cpr.h>

#define	CPR_BUFSIZE	128

extern int devi_detach(dev_info_t *, int);
extern int devi_attach(dev_info_t *, int);

static char 	*devi_string(dev_info_t *, char *);
static int	cpr_is_real_device(dev_info_t *);
/*
 * Xen uses this code to suspend _all_ drivers quickly and easily.
 * Suspend and Resume uses it for the same reason, but also has
 * to contend with some platform specific code that Xen does not.
 * it is also used as a test entry point for developers/testers to
 * execute code without going through a complete suspend.  So additions
 * that have platform implications shall need #if[n]def's.
 */
#ifndef __xpv
extern void	i_cpr_save_configuration(dev_info_t *);
extern void	i_cpr_restore_configuration(dev_info_t *);
#endif

/*
 * Traverse the dev info tree:
 *	Call each device driver in the system via a special case
 *	of the detach() entry point to quiesce itself.
 *	Suspend children first.
 *
 * We only suspend/resume real devices.
 */

int
cpr_suspend_devices(dev_info_t *dip)
{
	int		error;
	char		buf[CPR_BUFSIZE];

	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
		if (cpr_suspend_devices(ddi_get_child(dip)))
			return (ENXIO);
		if (!cpr_is_real_device(dip))
			continue;
		CPR_DEBUG(CPR_DEBUG2, "Suspending device %s\n",
		    devi_string(dip, buf));
		ASSERT((DEVI(dip)->devi_cpr_flags & DCF_CPR_SUSPENDED) == 0);

#ifndef __xpv
		i_cpr_save_configuration(dip);
#endif


		if (!i_ddi_devi_attached(dip)) {
			error = DDI_FAILURE;
		} else {
#ifndef __xpv
			if (cpr_test_point != DEVICE_SUSPEND_TO_RAM ||
			    (cpr_test_point == DEVICE_SUSPEND_TO_RAM &&
			    cpr_device == ddi_driver_major(dip))) {
#endif
				error = devi_detach(dip, DDI_SUSPEND);
#ifndef __xpv
			} else {
				error = DDI_SUCCESS;
			}
#endif
		}

		if (error == DDI_SUCCESS) {
			DEVI(dip)->devi_cpr_flags |= DCF_CPR_SUSPENDED;
		}

		else {
			CPR_DEBUG(CPR_DEBUG2,
			    "WARNING: Unable to suspend device %s\n",
			    devi_string(dip, buf));
			cpr_err(CE_WARN, "Unable to suspend device %s.",
			    devi_string(dip, buf));
			cpr_err(CE_WARN, "Device is busy or does not "
			    "support suspend/resume.");
#ifndef __xpv
			/*
			 * the device has failed to suspend however,
			 * if cpr_test_point == FORCE_SUSPEND_TO_RAM
			 * after putting out the warning message above,
			 * we carry on as if suspending the device had
			 * been successful
			 */
			if (cpr_test_point == FORCE_SUSPEND_TO_RAM)
				DEVI(dip)->devi_cpr_flags |= DCF_CPR_SUSPENDED;
			else
#endif
				return (ENXIO);
		}
	}
	return (0);
}

/*
 * Traverse the dev info tree:
 *	Call each device driver in the system via a special case
 *	of the attach() entry point to restore itself.
 *	This is a little tricky because it has to reverse the traversal
 *	order of cpr_suspend_devices().
 */
int
cpr_resume_devices(dev_info_t *start, int resume_failed)
{
	dev_info_t	*dip, *next, *last = NULL;
	int		did_suspend;
	int		error = resume_failed;
	char		buf[CPR_BUFSIZE];

	while (last != start) {
		dip = start;
		next = ddi_get_next_sibling(dip);
		while (next != last) {
			dip = next;
			next = ddi_get_next_sibling(dip);
		}

		/*
		 * cpr is the only one that uses this field and the device
		 * itself hasn't resumed yet, there is no need to use a
		 * lock, even though kernel threads are active by now.
		 */
		did_suspend = DEVI(dip)->devi_cpr_flags & DCF_CPR_SUSPENDED;
		if (did_suspend)
			DEVI(dip)->devi_cpr_flags &= ~DCF_CPR_SUSPENDED;

		/*
		 * Always attempt to restore device configuration before
		 * attempting resume
		 */
#ifndef __xpv
		i_cpr_restore_configuration(dip);
#endif

		/*
		 * There may be background attaches happening on devices
		 * that were not originally suspended by cpr, so resume
		 * only devices that were suspended by cpr. Also, stop
		 * resuming after the first resume failure, but traverse
		 * the entire tree to clear the suspend flag unless the
		 * FORCE_SUSPEND_TO_RAM test point is set.
		 */
#ifndef __xpv
		if (did_suspend && (!error ||
		    cpr_test_point == FORCE_SUSPEND_TO_RAM)) {
#else
		if (did_suspend && !error) {
#endif
			CPR_DEBUG(CPR_DEBUG2, "Resuming device %s\n",
			    devi_string(dip, buf));
			/*
			 * If a device suspended by cpr gets detached during
			 * the resume process (for example, due to hotplugging)
			 * before cpr gets around to issuing it a DDI_RESUME,
			 * we'll have problems.
			 */
			if (!i_ddi_devi_attached(dip)) {
				CPR_DEBUG(CPR_DEBUG2, "WARNING: Skipping "
				    "%s, device not ready for resume\n",
				    devi_string(dip, buf));
				cpr_err(CE_WARN, "Skipping %s, device "
				    "not ready for resume",
				    devi_string(dip, buf));
#ifndef __xpv
			} else if (cpr_test_point != DEVICE_SUSPEND_TO_RAM ||
			    (cpr_test_point == DEVICE_SUSPEND_TO_RAM &&
			    cpr_device == ddi_driver_major(dip))) {
#else
			} else {
#endif
				if (devi_attach(dip, DDI_RESUME) !=
				    DDI_SUCCESS) {
					error = ENXIO;
				}
			}
		}

		if (error == ENXIO) {
			CPR_DEBUG(CPR_DEBUG2,
			    "WARNING: Unable to resume device %s\n",
			    devi_string(dip, buf));
			cpr_err(CE_WARN, "Unable to resume device %s",
			    devi_string(dip, buf));
		}

		error = cpr_resume_devices(ddi_get_child(dip), error);
		last = dip;
	}

	return (error);
}

/*
 * Returns a string which contains device name and address.
 */
static char *
devi_string(dev_info_t *devi, char *buf)
{
	char *name;
	char *address;
	int size;

	name = ddi_node_name(devi);
	address = ddi_get_name_addr(devi);
	size = (name == NULL) ? strlen("<null name>") : strlen(name);
	size += (address == NULL) ? strlen("<null>") : strlen(address);

	/*
	 * Make sure that we don't over-run the buffer.
	 * There are 2 additional characters in the string.
	 */
	ASSERT((size + 2) <= CPR_BUFSIZE);

	if (name == NULL)
		(void) strcpy(buf, "<null name>");
	else
		(void) strcpy(buf, name);

	(void) strcat(buf, "@");
	if (address == NULL)
		(void) strcat(buf, "<null>");
	else
		(void) strcat(buf, address);

	return (buf);
}

/*
 * This function determines whether the given device is real (and should
 * be suspended) or not (pseudo like).  If the device has a "reg" property
 * then it is presumed to have register state to save/restore.
 */
static int
cpr_is_real_device(dev_info_t *dip)
{
	struct regspec *regbuf;
	int length;
	int rc;

	if (ddi_get_driver(dip) == NULL)
		return (0);

	/*
	 * First those devices for which special arrangements have been made
	 */
	if (DEVI(dip)->devi_pm_flags & (PMC_NEEDS_SR|PMC_PARENTAL_SR))
		return (1);
	if (DEVI(dip)->devi_pm_flags & PMC_NO_SR)
		return (0);

	/*
	 * now the general case
	 */
	rc = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg",
	    (caddr_t)&regbuf, &length);
	ASSERT(rc != DDI_PROP_NO_MEMORY);
	if (rc != DDI_PROP_SUCCESS) {
		return (0);
	} else {
		kmem_free((caddr_t)regbuf, length);
		return (1);
	}
}