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
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* PICL Ontario platform plug-in to message the SC to light
* or extinguish the hdd 'OK2RM' ready-to-service light in
* the event of a soft unconfigure or configure, respectively.
*
* Erie platforms (T1000) do not have ok-to-remove LEDs
* so they do not need handlers for the SBL events.
*/
#include <picl.h>
#include <picltree.h>
#include <picldefs.h>
#include <stdio.h>
#include <umem.h>
#include <unistd.h>
#include <libnvpair.h>
#include <strings.h>
#include <syslog.h>
#include <dlfcn.h>
#include <link.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/raidioctl.h>
#include <sys/utsname.h>
#include <sys/systeminfo.h>
#include <libpcp.h>
#include "piclsbl.h"
#include "errno.h"
#pragma init(piclsbl_register)
static void *pcp_handle;
static char hba_devctl[MAXPATHLEN];
static int (* pcp_init_ptr)();
static int (* pcp_send_recv_ptr)();
static int (* pcp_close_ptr)();
static int load_pcp_libs(void);
static void piclsbl_init(void);
static void piclsbl_fini(void);
static void piclsbl_register(void);
static void piclsbl_handler(const char *ename, const void *earg,
size_t size, void *cookie);
static picld_plugin_reg_t piclsbl_reg = {
PICLD_PLUGIN_VERSION_1,
PICLD_PLUGIN_CRITICAL,
"piclsbl",
piclsbl_init,
piclsbl_fini
};
/*
* called from init to load the pcp library
*/
static int
load_pcp_libs()
{
char pcp_dl_lib[80];
(void) snprintf(pcp_dl_lib, sizeof (pcp_dl_lib), "%s%s",
LIB_PCP_PATH, PCPLIB);
/* load the library and set up function pointers */
if ((pcp_handle = dlopen(pcp_dl_lib, RTLD_NOW)) == (void *) NULL)
return (1);
pcp_init_ptr = (int(*)())dlsym(pcp_handle, "pcp_init");
pcp_close_ptr = (int(*)())dlsym(pcp_handle, "pcp_close");
pcp_send_recv_ptr = (int(*)())dlsym(pcp_handle, "pcp_send_recv");
if (pcp_init_ptr == NULL || pcp_send_recv_ptr == NULL ||
pcp_close_ptr == NULL)
return (1);
return (0);
}
/*
* callback routine for ptree_walk_tree_by_class()
*/
static int
cb_find_disk(picl_nodehdl_t node, void *args)
{
disk_lookup_t *lookup = (disk_lookup_t *)args;
int status = -1;
char *n;
char path[PICL_PROPNAMELEN_MAX];
status = ptree_get_propval_by_name(node, "Path", (void *)&path,
PICL_PROPNAMELEN_MAX);
if (status != PICL_SUCCESS) {
return (PICL_WALK_CONTINUE);
}
if (strcmp(path, lookup->path) == 0) {
lookup->disk = node;
lookup->result = DISK_FOUND;
/* store the HBA's device path for use in check_raid() */
n = strstr(path, "/sd");
strncpy(n, "\0", 1);
(void) snprintf(hba_devctl, MAXPATHLEN, "/devices%s:devctl",
path);
return (PICL_WALK_TERMINATE);
}
return (PICL_WALK_CONTINUE);
}
/*
* check a target for RAID membership
*/
static int
check_raid(int target)
{
raid_config_t config;
int fd;
int numvols;
int i;
int j;
/*
* hba_devctl is set to the onboard hba, so it will
* always house any onboard RAID volumes
*/
if ((fd = open(hba_devctl, O_RDONLY)) < 0) {
syslog(LOG_ERR, "%s", strerror(errno));
return (0);
}
/*
* look up the RAID configurations for the onboard
* HBA and check target against all member targets
*/
if (ioctl(fd, RAID_NUMVOLUMES, &numvols)) {
syslog(LOG_ERR, "%s", strerror(errno));
(void) close(fd);
return (0);
}
for (i = 0; i < numvols; i++) {
config.unitid = i;
if (ioctl(fd, RAID_GETCONFIG, &config)) {
syslog(LOG_ERR, "%s", strerror(errno));
(void) close(fd);
return (0);
}
for (j = 0; j < config.ndisks; j++) {
if (config.disk[j] == target) {
(void) close(fd);
return (1);
}
}
}
(void) close(fd);
return (0);
}
/*
* Ontario SBL event handler, subscribed to:
* PICLEVENT_SYSEVENT_DEVICE_ADDED
* PICLEVENT_SYSEVENT_DEVICE_REMOVED
*/
static void
piclsbl_handler(const char *ename, const void *earg, size_t size,
void *cookie)
{
char *devfs_path;
char hdd_location[PICL_PROPNAMELEN_MAX];
nvlist_t *nvlp = NULL;
pcp_msg_t send_msg;
pcp_msg_t recv_msg;
pcp_sbl_req_t *req_ptr = NULL;
pcp_sbl_resp_t *resp_ptr = NULL;
int status = -1;
int target;
disk_lookup_t lookup;
int channel_fd;
/*
* setup the request data to attach to the libpcp msg
*/
if ((req_ptr = (pcp_sbl_req_t *)umem_zalloc(sizeof (pcp_sbl_req_t),
UMEM_DEFAULT)) == NULL)
goto sbl_return;
/*
* This plugin serves to enable or disable the blue RAS
* 'ok-to-remove' LED that is on each of the 4 disks on the
* Ontario. We catch the event via the picl handler, and
* if the event is DEVICE_ADDED for one of our onboard disks,
* then we'll be turning off the LED. Otherwise, if the event
* is DEVICE_REMOVED, then we turn it on.
*/
if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0)
req_ptr->sbl_action = PCP_SBL_DISABLE;
else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0)
req_ptr->sbl_action = PCP_SBL_ENABLE;
else
goto sbl_return;
/*
* retrieve the device's physical path from the event payload
*/
if (nvlist_unpack((char *)earg, size, &nvlp, 0))
goto sbl_return;
if (nvlist_lookup_string(nvlp, "devfs-path", &devfs_path))
goto sbl_return;
/*
* look for this disk in the picl tree, and if it's
* location indicates that it's one of our internal
* disks, then set sbl_id to incdicate which one.
* otherwise, return as it is not one of our disks.
*/
lookup.path = strdup(devfs_path);
lookup.disk = 0;
lookup.result = DISK_NOT_FOUND;
/* first, find the disk */
status = ptree_walk_tree_by_class(root_node, "disk", (void *)&lookup,
cb_find_disk);
if (status != PICL_SUCCESS)
goto sbl_return;
if (lookup.result == DISK_FOUND) {
/* now, lookup it's location in the node */
status = ptree_get_propval_by_name(lookup.disk, "Location",
(void *)&hdd_location, PICL_PROPNAMELEN_MAX);
if (status != PICL_SUCCESS) {
syslog(LOG_ERR, "piclsbl: failed hdd discovery");
goto sbl_return;
}
}
/*
* Strip off the target from the NAC name.
* The disk NAC will always be HDD#
*/
if (strncmp(hdd_location, NAC_DISK_PREFIX,
strlen(NAC_DISK_PREFIX)) == 0) {
(void) sscanf(hdd_location, "%*3s%d", &req_ptr->sbl_id);
target = (int)req_ptr->sbl_id;
} else {
/* this is not one of the onboard disks */
goto sbl_return;
}
/*
* check the onboard RAID configuration for this disk. if it is
* a member of a RAID and is not the RAID itself, ignore the event
*/
if (check_raid(target))
goto sbl_return;
/*
* we have the information we need, init the platform channel.
* the platform channel driver will only allow one connection
* at a time on this socket. on the offchance that more than
* one event comes in, we'll retry to initialize this connection
* up to 3 times
*/
if ((channel_fd = (*pcp_init_ptr)(LED_CHANNEL)) < 0) {
/* failed to init; wait and retry up to 3 times */
int s = PCPINIT_TIMEOUT;
int retries = 0;
while (++retries) {
(void) sleep(s);
if ((channel_fd = (*pcp_init_ptr)(LED_CHANNEL)) >= 0)
break;
else if (retries == 3) {
syslog(LOG_ERR, "piclsbl: ",
"SC channel initialization failed");
goto sbl_return;
}
/* continue */
}
}
/*
* populate the message for libpcp
*/
send_msg.msg_type = PCP_SBL_CONTROL;
send_msg.sub_type = 0;
send_msg.msg_len = sizeof (pcp_sbl_req_t);
send_msg.msg_data = (uint8_t *)req_ptr;
/*
* send the request, receive the response
*/
if ((*pcp_send_recv_ptr)(channel_fd, &send_msg, &recv_msg,
PCPCOMM_TIMEOUT) < 0) {
/* we either timed out or erred; either way try again */
int s = PCPCOMM_TIMEOUT;
(void) sleep(s);
if ((*pcp_send_recv_ptr)(channel_fd, &send_msg, &recv_msg,
PCPCOMM_TIMEOUT) < 0) {
syslog(LOG_ERR, "piclsbl: communication failure");
goto sbl_return;
}
}
/*
* validate that this data was meant for us
*/
if (recv_msg.msg_type != PCP_SBL_CONTROL_R) {
syslog(LOG_ERR, "piclsbl: unbound packet received");
goto sbl_return;
}
/*
* verify that the LED action has taken place
*/
resp_ptr = (pcp_sbl_resp_t *)recv_msg.msg_data;
if (resp_ptr->status == PCP_SBL_ERROR) {
syslog(LOG_ERR, "piclsbl: OK2RM LED action error");
goto sbl_return;
}
/*
* ensure the LED action taken is the one requested
*/
if ((req_ptr->sbl_action == PCP_SBL_DISABLE) &&
(resp_ptr->sbl_state != SBL_STATE_OFF))
syslog(LOG_ERR, "piclsbl: OK2RM LED not OFF after disk "
"configuration");
else if ((req_ptr->sbl_action == PCP_SBL_ENABLE) &&
(resp_ptr->sbl_state != SBL_STATE_ON))
syslog(LOG_ERR, "piclsbl: OK2RM LED not ON after disk "
"unconfiguration");
else if (resp_ptr->sbl_state == SBL_STATE_UNKNOWN)
syslog(LOG_ERR, "piclsbl: OK2RM LED set to unknown state");
sbl_return:
(*pcp_close_ptr)(channel_fd);
if (req_ptr != NULL)
umem_free(req_ptr, sizeof (pcp_sbl_req_t));
if (resp_ptr != NULL)
free(resp_ptr);
nvlist_free(nvlp);
}
static void
piclsbl_init(void)
{
char platbuf[SYS_NMLN];
/* check for Erie platform name */
if ((sysinfo(SI_PLATFORM, platbuf, SYS_NMLN) != -1) &&
((strcmp(platbuf, ERIE_PLATFORM) == 0) ||
(strcmp(platbuf, ERIE_PLATFORM2) == 0)))
return;
/* retrieve the root node for lookups in the event handler */
if ((ptree_get_root(&root_node)) != 0)
return;
/* load libpcp */
if (load_pcp_libs()) {
syslog(LOG_ERR, "piclsbl: failed to load libpcp");
syslog(LOG_ERR, "piclsbl: aborting");
return;
}
/*
* register piclsbl_handler for both "sysevent-device-added" and
* and for "sysevent-device-removed" PICL events
*/
(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
piclsbl_handler, NULL);
(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
piclsbl_handler, NULL);
}
static void
piclsbl_fini(void)
{
/* unregister the event handler */
(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
piclsbl_handler, NULL);
(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
piclsbl_handler, NULL);
}
static void
piclsbl_register(void)
{
picld_plugin_register(&piclsbl_reg);
}
|