summaryrefslogtreecommitdiff
path: root/usr/src/cmd/acpihpd/notify.c
blob: 138b1a233dac744488e67e802eb8085d9ea82036 (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
/*
 * 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) 2010, Intel Corporation.
 * All rights reserved.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <config_admin.h>
#include <strings.h>
#include <syslog.h>
#include <libsysevent.h>
#include <libdevinfo.h>
#include <libnvpair.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <stropts.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/sysevent/dr.h>
#include <sys/sbd_ioctl.h>
#include <sys/acpidev.h>

#define	PMCONFIG_PATH			"/usr/sbin/pmconfig"

#define	CFGADM_CMD_ASSIGN		"assign"
#define	CFGADM_CMD_POWERON		"poweron"
#define	CFGADM_CMD_PASSTHRU		"passthru"

#define	STATUS_INPROGRESS		0
#define	STATUS_SUCCESS			1
#define	STATUS_FAILURE			2
#define	STATUS_NOOP			3

static char *s_status_array[] = {
	ACPIDEV_CMD_OST_INPROGRESS,
	ACPIDEV_CMD_OST_SUCCESS,
	ACPIDEV_CMD_OST_FAILURE,
	ACPIDEV_CMD_OST_NOOP
};

extern void debug_print(int, const char *, ...);

/*ARGSUSED*/
static int
confirm_no(void *appdata_ptr, const char *message)
{
	return (0);
}

/*ARGSUSED*/
static int
message_output(void *appdata_ptr, const char *message)
{
	debug_print(2, "cfgadm message: %s", message);
	return (CFGA_OK);
}

static char *
plat_opt_str_alloc(int cmd, char *acpi_event_type, int status)
{
	char *opt;
	size_t len;

	if (cmd == SBD_CMD_PASSTHRU) {
		len = strlen(s_status_array[status]) +
		    strlen(ACPIDEV_EVENT_TYPE_ATTR_NAME) +
		    strlen(acpi_event_type) + 10;
		if ((opt = malloc(len)) != NULL) {
			(void) snprintf(opt, len, "%s %s=%s",
			    s_status_array[status],
			    ACPIDEV_EVENT_TYPE_ATTR_NAME,
			    acpi_event_type);
			debug_print(2, "plat_opt_str_alloc = '%s'", opt);
		}
	} else {
		len = strlen("platform=''") +
		    strlen(s_status_array[status]) +
		    strlen(ACPIDEV_EVENT_TYPE_ATTR_NAME) +
		    strlen(acpi_event_type) + 10;
		if ((opt = malloc(len)) != NULL) {
			(void) snprintf(opt, len, "platform='%s %s=%s'",
			    s_status_array[status],
			    ACPIDEV_EVENT_TYPE_ATTR_NAME,
			    acpi_event_type);
			debug_print(2, "plat_opt_str_alloc = '%s'", opt);
		}
	}

	return (opt);
}

static int
cfgadm_cmd_wrapper(int cmd, int apid_num, char **apids,
	char *acpi_event_type, int status,
	struct cfga_confirm *confirm, struct cfga_msg *message)
{
	cfga_err_t ret;
	char *plat_opts;
	char *estrp = NULL;

	assert(apid_num == 1);
	assert(apids != NULL);

	plat_opts = plat_opt_str_alloc(cmd, acpi_event_type, status);
	if (plat_opts == NULL) {
		debug_print(0,
		    "failed to generate platform option string for cfgadm");
		return (-1);
	}

	switch (cmd) {
	case SBD_CMD_CONNECT:
		ret = config_change_state(CFGA_CMD_CONNECT, apid_num, apids,
		    plat_opts, confirm, message, &estrp, 0);
		if (ret != CFGA_OK) {
			debug_print(0, "cfgadm('connect', '%s') failed, "
			    "ret = %d, errstr = '%s'", apids[0], ret, estrp);
		}
		break;

	case SBD_CMD_CONFIGURE:
		ret = config_change_state(CFGA_CMD_CONFIGURE, apid_num, apids,
		    plat_opts, confirm, message, &estrp, 0);
		if (ret != CFGA_OK) {
			debug_print(0, "cfgadm('configure', '%s') failed, "
			    "ret = %d, errstr = '%s'", apids[0], ret, estrp);
		}
		break;

	case SBD_CMD_ASSIGN:
		ret = config_private_func(CFGADM_CMD_ASSIGN, apid_num, apids,
		    plat_opts, confirm, message, &estrp, 0);
		if (ret != CFGA_OK) {
			debug_print(0, "cfgadm('assign', '%s') failed, "
			    "ret = %d, errstr = '%s'", apids[0], ret, estrp);
		}
		break;

	case SBD_CMD_POWERON:
		ret = config_private_func(CFGADM_CMD_POWERON, apid_num, apids,
		    plat_opts, confirm, message, &estrp, 0);
		if (ret != CFGA_OK) {
			debug_print(0, "cfgadm('poweron', '%s') failed, "
			    "ret = %d, errstr = '%s'", apids[0], ret, estrp);
		}
		break;

	case SBD_CMD_PASSTHRU:
		ret = config_private_func(CFGADM_CMD_PASSTHRU, apid_num, apids,
		    plat_opts, confirm, message, &estrp, 0);
		if (ret != CFGA_OK) {
			debug_print(0, "cfgadm('passthru', '%s') failed, "
			    "ret = %d, errstr = '%s'", apids[0], ret, estrp);
		}
		break;

	default:
		debug_print(2, "unknown command (%d) to cfgadm_cmd_wrapper()");
		ret = CFGA_ERROR;
		break;
	}

	if (plat_opts != NULL)
		free(plat_opts);

	return (ret == CFGA_OK ? 0 : -1);
}

static int
event_process(char *ap_id, char *req, char *acpi_event_type)
{
	char *apids[1];
	struct cfga_msg message;
	struct cfga_confirm confirm;

	if (strcmp(req, DR_REQ_INCOMING_RES) != 0) {
		debug_print(2,
		    "Event is not supported (ap_id = %s, req = %s)",
		    ap_id, req);
		return (-1);
	}

	apids[0] = ap_id;
	(void) memset(&confirm, 0, sizeof (confirm));
	confirm.confirm = confirm_no;
	(void) memset(&message, 0, sizeof (message));
	message.message_routine = message_output;

	if (cfgadm_cmd_wrapper(SBD_CMD_ASSIGN, 1, apids,
	    acpi_event_type, STATUS_NOOP, &confirm, &message) != 0) {
		goto L_ERR;
	}
	syslog(LOG_NOTICE,
	    "board '%s' has been assigned successfully", ap_id);
	(void) cfgadm_cmd_wrapper(SBD_CMD_PASSTHRU, 1, apids,
	    acpi_event_type, STATUS_INPROGRESS, &confirm, &message);

	if (cfgadm_cmd_wrapper(SBD_CMD_POWERON, 1, apids,
	    acpi_event_type, STATUS_NOOP, &confirm, &message) != 0) {
		goto L_ERR_SIG;
	}
	syslog(LOG_NOTICE,
	    "board '%s' has been powered on successfully", ap_id);
	(void) cfgadm_cmd_wrapper(SBD_CMD_PASSTHRU, 1, apids,
	    acpi_event_type, STATUS_INPROGRESS, &confirm, &message);

	if (cfgadm_cmd_wrapper(SBD_CMD_CONNECT, 1, apids,
	    acpi_event_type, STATUS_INPROGRESS, &confirm, &message) != 0) {
		goto L_ERR_SIG;
	}
	syslog(LOG_NOTICE,
	    "board '%s' has been connected successfully", ap_id);
	(void) cfgadm_cmd_wrapper(SBD_CMD_PASSTHRU, 1, apids,
	    acpi_event_type, STATUS_INPROGRESS, &confirm, &message);

	if (cfgadm_cmd_wrapper(SBD_CMD_CONFIGURE, 1, apids,
	    acpi_event_type, STATUS_INPROGRESS, &confirm, &message) != 0) {
		goto L_ERR_SIG;
	}
	syslog(LOG_NOTICE,
	    "board '%s' has been configured successfully", ap_id);
	(void) cfgadm_cmd_wrapper(SBD_CMD_PASSTHRU, 1, apids,
	    acpi_event_type, STATUS_SUCCESS, &confirm, &message);

	(void) system(PMCONFIG_PATH);
	syslog(LOG_NOTICE,
	    "board '%s' has been added into system successfully", ap_id);

	return (0);

L_ERR_SIG:
	(void) cfgadm_cmd_wrapper(SBD_CMD_PASSTHRU, 1, apids,
	    acpi_event_type, STATUS_FAILURE, &confirm, &message);
L_ERR:
	syslog(LOG_ERR, "failed to add board '%s' into system", ap_id);

	return (-1);
}

void
notify_hotplug(sysevent_t *ev)
{
	char *vendor = NULL;
	nvlist_t *attr_list = NULL;
	char *class, *subclass;
	char *ap_id, *req, *acpi_event_type;

	vendor = sysevent_get_vendor_name(ev);
	debug_print(2, "message_vendor = '%s'", vendor ? vendor : "unknown");
	if (vendor == NULL || strcmp(vendor, SUNW_VENDOR) != 0) {
		debug_print(2,
		    "vendor id of message is not '%s'", SUNW_VENDOR);
		goto L_EXIT;
	}

	class = sysevent_get_class_name(ev);
	debug_print(2, "message_class = '%s'", class ? class : "unknown");
	if (class == NULL || strcmp(class, EC_DR) != 0) {
		debug_print(2, "class of message is not '%s'", EC_DR);
		goto L_EXIT;
	}

	subclass = sysevent_get_subclass_name(ev);
	debug_print(2,
	    "message_subclass = '%s'", subclass ? subclass : "unknown");
	if (subclass == NULL || strcmp(subclass, ESC_DR_REQ) != 0) {
		debug_print(2,
		    "subclass of message is not '%s'", ESC_DR_REQ);
		goto L_EXIT;
	}

	if (sysevent_get_attr_list(ev, &attr_list) != 0) {
		debug_print(2,
		    "can't retrieve attribute list from DR message");
		goto L_EXIT;
	}

	if (nvlist_lookup_string(attr_list, DR_AP_ID, &ap_id) != 0) {
		debug_print(2,
		    "can't retrieve '%s' property from attribute list",
		    DR_AP_ID);
		goto L_EXIT;
	}
	debug_print(2, "%s = '%s'", DR_AP_ID, ap_id ? ap_id : "<null>");
	if ((ap_id == NULL) || (strlen(ap_id) == 0)) {
		debug_print(2, "'%s' property in message is NULL", DR_AP_ID);
		goto L_EXIT;
	}

	if (nvlist_lookup_string(attr_list, DR_REQ_TYPE, &req) != 0) {
		debug_print(2,
		    "can't retrieve '%s' property from attribute list",
		    DR_REQ_TYPE);
		goto L_EXIT;
	}
	debug_print(2, "%s = '%s'", DR_REQ_TYPE, req ? req : "<null>");
	if ((req == NULL) || (strlen(req) == 0)) {
		debug_print(2, "'%s' property in message is NULL", DR_REQ_TYPE);
		goto L_EXIT;
	}

	if (nvlist_lookup_string(attr_list, ACPIDEV_EVENT_TYPE_ATTR_NAME,
	    &acpi_event_type) != 0) {
		debug_print(2,
		    "can't retrieve '%s' property from attribute list",
		    ACPIDEV_EVENT_TYPE_ATTR_NAME);
		goto L_EXIT;
	}
	debug_print(2, "%s = '%s'", ACPIDEV_EVENT_TYPE_ATTR_NAME,
	    acpi_event_type ? acpi_event_type : "<null>");
	if ((acpi_event_type == NULL) || (strlen(acpi_event_type) == 0)) {
		debug_print(2, "'%s' property in message is NULL",
		    ACPIDEV_EVENT_TYPE_ATTR_NAME);
		goto L_EXIT;
	}

	(void) event_process(ap_id, req, acpi_event_type);

L_EXIT:
	if (vendor != NULL) {
		free(vendor);
	}

	if (attr_list != NULL) {
		nvlist_free(attr_list);
	}

	/* No need to free class & subclass. */
}