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
|
/*
* 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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* ENXS platform-specific functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "librsc.h"
/* rmcadm driver file descriptor */
static int rsc_fd = -1;
/*
* librsc receive buffer - it is used as temporary buffer to store replies
* from the remote side
*/
static uchar_t rsc_rx_buffer[RSC_MAX_RX_BUFFER];
static int rsc_rx_resp_len = 0;
static int rsc_rx_error = 0;
static rsci8 rsc_rx_resp_type = 0;
/*
* Registered boot-protocol message callback routine. This routine will be
* called whenever a boot protocol message is received.
*/
static rscp_bpmsg_cb_t *bpmsg_cb;
/* lookup table to match request and response . This is in order to support */
/* obsolete functions (rscp_send, rscp_recv) */
static req_resp_table_t rr_table[] = {
{ DP_GET_DATE_TIME, DP_GET_DATE_TIME_R,
sizeof (dp_get_date_time_r_t), RR_TIMEOUT },
{ DP_SET_DATE_TIME, DP_SET_DATE_TIME_R,
sizeof (dp_set_date_time_r_t), RR_TIMEOUT },
{ DP_GET_EVENT_LOG, DP_GET_EVENT_LOG_R,
sizeof (dp_get_event_log_r_t), RR_TIMEOUT },
{ DP_MODEM_CONNECT, DP_MODEM_CONNECT_R,
sizeof (dp_modem_connect_r_t), RR_TIMEOUT },
{ DP_MODEM_DISCONNECT, DP_MODEM_DISCONNECT_R,
sizeof (dp_modem_disconnect_r_t), RR_TIMEOUT },
{ DP_SEND_ALERT, DP_SEND_ALERT_R,
sizeof (dp_send_alert_r_t), RR_TIMEOUT },
{ DP_SET_CFGVAR, DP_SET_CFGVAR_R,
sizeof (dp_set_cfgvar_r_t), RR_TIMEOUT },
{ DP_GET_CFGVAR, DP_GET_CFGVAR_R,
sizeof (dp_get_cfgvar_r_t), RR_TIMEOUT },
{ DP_GET_CFGVAR_NAME, DP_GET_CFGVAR_NAME_R,
sizeof (dp_get_cfgvar_name_r_t), RR_TIMEOUT },
{ DP_GET_NETWORK_CFG, DP_GET_NETWORK_CFG_R,
sizeof (dp_get_network_cfg_r_t), RR_TIMEOUT },
{ DP_RSC_STATUS, DP_RSC_STATUS_R,
sizeof (dp_rsc_status_r_t), RR_TIMEOUT },
{ DP_USER_ADM, DP_USER_ADM_R,
sizeof (dp_user_adm_r_t), RR_SEPROM_TIMEOUT},
{ DP_RESET_RSC, DP_NULL_MSG,
0, 1 },
{ DP_GET_CONSOLE_LOG, DP_GET_CONSOLE_LOG_R,
sizeof (dp_get_console_log_r_t), RR_TIMEOUT },
{ DP_GET_CONFIG_LOG, DP_GET_CONFIG_LOG_R,
sizeof (dp_get_config_log_r_t), RR_TIMEOUT },
{ DP_GET_EVENT_LOG2, DP_GET_EVENT_LOG2_R,
sizeof (dp_get_event_log2_r_t), RR_TIMEOUT },
};
static const int rr_table_cnt = sizeof (rr_table) / sizeof (rr_table[0]);
/* lookup table to get timeout value for BP cmd reply. This is in order to */
/* support obsolete functions (rscp_send_bpmsg, rsc_raw_write) */
static req_resp_table_t rr_bp_table[] = {
{ BP_OBP_BOOTINIT, NULL, sizeof (bp_msg_t),
RR_BOOT_INIT_TIMEOUT },
{ BP_OBP_RESET, NULL, sizeof (bp_msg_t),
RR_BOOT_RESET_TIMEOUT }
};
static const int rr_bp_table_cnt =
sizeof (rr_bp_table) / sizeof (rr_bp_table[0]);
static rsci8 unsupported_cmds[] = { DP_SET_DATE_TIME };
static int unsupported_cmds_cnt = sizeof (unsupported_cmds) /
sizeof (unsupported_cmds[0]);
/*
* Protocol version number, used to determine whether ALOM will
* time out on unknown commands.
*/
static int sdp_version = -1;
/* function prototypes */
static req_resp_table_t *rsc_lookup_rr_table(req_resp_table_t *, int, rsci8);
static int rsc_check_unsupported_cmd(rsci8);
static int rsc_cmd_response_guaranteed(rsci8);
/*
* Initialize the generic librsc data protocol routines. basically, it
* open the rmcadm (pseudo) device and initialize data
*/
int
rscp_init(void)
{
rscp_msg_t request, response;
dp_get_sdp_version_r_t version_msg;
/*
* 'erase' the rx buffer
*/
(void) memset(rsc_rx_buffer, 0, sizeof (RSC_MAX_RX_BUFFER));
rsc_rx_resp_len = 0;
rsc_rx_error = 0;
rsc_rx_resp_type = DP_NULL_MSG;
/*
* open rmcadm driver
*/
if ((rsc_fd = open(RSC_RMCADM_DRV, O_RDWR)) < 0) {
#ifdef DEBUG
printf("rscp_init: Error opening %s, error code = %d\n",
RSC_RMCADM_DRV, errno);
#endif
return (errno);
}
/*
* Fetch the protocol version number in use between the host
* and ALOM.
*/
request.type = DP_GET_SDP_VERSION;
request.len = 0;
request.data = 0;
response.type = DP_GET_SDP_VERSION_R;
response.len = sizeof (version_msg);
response.data = (caddr_t)&version_msg;
if ((errno = rscp_send_recv(&request, &response, 0)) != 0)
return (errno);
sdp_version = version_msg.version;
#ifdef DEBUG
printf("rscp_init: sdp version number is %d\n", sdp_version);
#endif
return (0);
}
/*
* send/receive interface: this is the new interface where application
* (currently scadm, SunVTS) send a request and wait for a reply in a
* single call. If a response is not required (resp=NULL), the function
* will only return the status of the request (whether it has been successfully
* or not).
*/
int
rscp_send_recv(rscp_msg_t *req, rscp_msg_t *resp, struct timespec *timeout)
{
rmcadm_request_response_t rr;
rmcadm_msg_t *rr_req = &rr.req;
rmcadm_msg_t *rr_resp = &rr.resp;
if (rsc_fd < 0)
return (EBADF);
/*
* the request is required, it should not be NULL!
*/
if (req == NULL)
return (EINVAL);
/*
* Check if the command is actually supported
* if not, return an error
*/
if (rsc_check_unsupported_cmd(req->type) != 0)
return (ENOTSUP);
/*
* Check if this command will generate a response and if it will not,
* return an error.
*/
if (!rsc_cmd_response_guaranteed(req->type))
return (ENOTSUP);
rr_req->msg_type = req->type;
rr_req->msg_len = req->len;
rr_req->msg_buf = (caddr_t)req->data;
if (resp != NULL) {
rr_resp->msg_type = resp->type;
rr_resp->msg_len = resp->len;
rr_resp->msg_buf = (caddr_t)resp->data;
rr_resp->msg_bytes = 0;
} else {
rr_resp->msg_type = DP_NULL_MSG;
rr_resp->msg_buf = (caddr_t)NULL;
rr_resp->msg_len = 0;
rr_resp->msg_bytes = 0;
}
if (timeout == NULL) {
rr.wait_time = RR_TIMEOUT;
} else {
rr.wait_time = timeout->tv_sec * 1000 +
timeout->tv_nsec / 1000000;
}
rr.status = 0;
if (ioctl(rsc_fd, RMCADM_REQUEST_RESPONSE, &rr) < 0) {
#ifdef DEBUG
printf("rscp_send_recv: req. failed, status=%d errno=%d\n",
rr_req->msg_type, rr.status, errno);
#endif
return (errno);
}
return (0);
}
/*
* function used to look up at the request/response table. Given a request
* type, will return a record which provides the following information:
* response expected and a timeout value
*/
static req_resp_table_t *
rsc_lookup_rr_table(req_resp_table_t *rr_table, int cnt, rsci8 type)
{
int i;
#ifdef DEBUG
printf("lookup for type %x, count %d\n", type, cnt);
#endif
for (i = 0; i < cnt; i++)
if (rr_table[i].req_type == type) {
return (rr_table + i);
}
return (NULL);
}
/*
* function to check if a message type is in the list of unsupported commands
* If so, will return 1.
*/
static int
rsc_check_unsupported_cmd(rsci8 type)
{
int i;
for (i = 0; i < unsupported_cmds_cnt; i++)
if (unsupported_cmds[i] == type) {
return (1);
}
return (0);
}
/*
* Returns 1 if ALOM will generate a response to the given command code,
* otherwise it returns 0. If a command is not in the following list,
* and the protocol version is 2 or less, then ALOM will not generate
* a response to the command. This causes the driver to time out,
* and we want to avoid that situation.
*/
static int
rsc_cmd_response_guaranteed(rsci8 type)
{
switch (type) {
case DP_GET_ALARM_STATE:
case DP_GET_CFGVAR:
case DP_GET_CFGVAR_NAME:
case DP_GET_CIRCUIT_BRKS:
case DP_GET_DATE_TIME:
case DP_GET_DEVICE:
case DP_GET_EVENT_LOG:
case DP_GET_FAN_STATUS:
case DP_GET_FRU_STATUS:
case DP_GET_HANDLE:
case DP_GET_HANDLE_NAME:
case DP_GET_LED_STATE:
case DP_GET_NETWORK_CFG:
case DP_GET_PCMCIA_INFO:
case DP_GET_PSU_STATUS:
case DP_GET_SDP_VERSION:
case DP_GET_SYSINFO:
case DP_GET_TEMP:
case DP_GET_TEMPERATURES:
case DP_GET_TICKCNT:
case DP_GET_TOD_CLOCK:
case DP_GET_USER_WATCHDOG:
case DP_GET_VOLTS:
case DP_MODEM_CONNECT:
case DP_MODEM_DATA:
case DP_MODEM_DISCONNECT:
case DP_RESET_RSC:
case DP_RMC_EVENTS:
case DP_RSC_STATUS:
case DP_RUN_TEST:
case DP_SEND_ALERT:
case DP_SET_ALARM_STATE:
case DP_SET_CFGVAR:
case DP_SET_CPU_SIGNATURE:
case DP_SET_DATE_TIME:
case DP_SET_DEFAULT_CFG:
case DP_SET_HOST_WATCHDOG:
case DP_SET_LED_STATE:
case DP_SET_USER_WATCHDOG:
case DP_UPDATE_FLASH:
case DP_USER_ADM:
return (1);
default:
return (sdp_version >= SDP_RESPONDS_TO_ALL_CMDS);
}
}
/*
* RSC hard reset. Returns 0 on success, non-zero on error.
*/
int
rsc_nmi(void)
{
if (rsc_fd < 0)
return (EBADF);
if (ioctl(rsc_fd, RMCADM_RESET_SP, NULL) < 0)
return (errno);
return (0);
}
/*
* functions used (exclusively) for the firmware download
*/
/*
* Call this routine to register a callback that will be called by the
* generic data protocol routines when a boot protocol message is
* received. Only one of these routines may be registered at a time.
* Note that receiving a boot protocol message has the effect of
* re-initializing the data protocol. Returns 0 on success, or non-
* zero on failure.
*/
int
rscp_register_bpmsg_cb(rscp_bpmsg_cb_t *cb)
{
if (rsc_fd < 0)
return (EBADF);
if (bpmsg_cb == NULL) {
bpmsg_cb = cb;
return (0);
} else {
return (EALREADY);
}
}
/*
* This routine un-registers a boot protocol message callback.
*/
int
rscp_unregister_bpmsg_cb(rscp_bpmsg_cb_t *cb)
{
if (rsc_fd < 0)
return (EBADF);
if (bpmsg_cb == cb) {
bpmsg_cb = NULL;
return (0);
} else {
return (EINPROGRESS);
}
}
/*
* Call this routine to send a boot protocol message.
*/
void
rscp_send_bpmsg(bp_msg_t *bpmsg)
{
rmcadm_request_response_t rr_bp;
rmcadm_msg_t *req_bp = &rr_bp.req;
rmcadm_msg_t *resp_bp = &rr_bp.resp;
req_resp_table_t *rr_bp_item;
bp_msg_t bpmsg_reply;
if (rsc_fd < 0 || bpmsg == NULL)
return;
/*
* get the timeout value
*/
if ((rr_bp_item = rsc_lookup_rr_table(rr_bp_table, rr_bp_table_cnt,
bpmsg->cmd)) != NULL) {
rr_bp.wait_time = rr_bp_item->timeout;
} else {
rr_bp.wait_time = RR_BP_TIMEOUT;
}
rr_bp.status = 0;
req_bp->msg_len = sizeof (bp_msg_t);
req_bp->msg_buf = (caddr_t)bpmsg;
if (rr_bp.wait_time == 0) {
resp_bp->msg_buf = (caddr_t)NULL;
} else {
resp_bp->msg_len = sizeof (bp_msg_t);
resp_bp->msg_buf = (caddr_t)&bpmsg_reply;
}
#ifdef DEBUG
printf("send BP cmd %x, expect reply %x/%d\n",
bpmsg->cmd, resp_bp->msg_buf, resp_bp->msg_len);
#endif
if (ioctl(rsc_fd, RMCADM_REQUEST_RESPONSE_BP, &rr_bp) < 0) {
#ifdef DEBUG
printf("rscp_send_bpmsg: BP cmd %x failed status=%d "
"errno=%d\n", bpmsg->cmd, rr_bp.status, errno);
#endif
return;
}
#ifdef DEBUG
printf("got BP reply type=%x,%x,%x\n",
bpmsg_reply.cmd, bpmsg_reply.dat1, bpmsg_reply.dat2);
#endif
/*
* reply received. call the registered callback (if any)
*/
if (bpmsg_cb != NULL && resp_bp->msg_buf != NULL)
bpmsg_cb(&bpmsg_reply);
}
/*
* Write raw characters to the RSC control device. Returns 0 on success,
* non-zero on error.
*/
int
rsc_raw_write(char *buf, int nbytes)
{
rmcadm_send_srecord_bp_t srec_bp;
bp_msg_t bpmsg_reply;
if (rsc_fd < 0)
return (EBADF);
srec_bp.data_len = (uint_t)nbytes;
srec_bp.data_buf = (caddr_t)buf;
srec_bp.resp_bp.msg_len = sizeof (bp_msg_t);
srec_bp.resp_bp.msg_buf = (caddr_t)&bpmsg_reply;
srec_bp.wait_time = RR_BOOT_LOAD_TIMEOUT;
srec_bp.status = 0;
#ifdef DEBUG
printf("send srecord BP len=%d\n", nbytes);
#endif
if (ioctl(rsc_fd, RMCADM_SEND_SRECORD_BP, &srec_bp) < 0) {
#ifdef DEBUG
printf("rsc_raw_write: failed. status=%d ioctl error=%d\n",
srec_bp.status, errno);
#endif
return (errno);
}
#ifdef DEBUG
printf("got BP reply type=%x\n", bpmsg_reply.cmd);
#endif
/*
* reply received. call the registered callback (if any)
*/
if (bpmsg_cb != NULL)
bpmsg_cb(&bpmsg_reply);
return (0);
}
/*
* obsolete functions provided for backward compatibility
*/
/*
* This function is obsolete and it is provided for backward compatibility.
* (no-op function). It was used to start up the data protocol. low-level
* protocol has moved to the kernel and the rmc_comm driver is responsible
* for setting up the data protocol.
* (obsolete)
*/
int
rscp_start(void)
{
if (rsc_fd < 0)
return (EBADF);
return (0);
}
/*
* This function is obsolete and it is provided for backward compatibility.
* Previously, rscp_send() and rscp_recv() where used to send a request and
* read a reply respectively. Now, rscp_send_recv() should be used instead
* (request/response in one call).
*
* This is used to send a message by making an RMCADM_REQUEST_RESPONSE ioctl
* call. A lookup table (rr_table) is used to find out the expected reply
* (if any) and the timeout value for a message to be sent. The reply is then
* stored in a buffer (rsc_rx_buffer) to be returned by calling rscp_recv()
*/
int
rscp_send(rscp_msg_t *msgp)
{
rmcadm_request_response_t rr;
rmcadm_msg_t *req = &rr.req;
rmcadm_msg_t *resp = &rr.resp;
req_resp_table_t *rr_item;
if (rsc_fd < 0)
return (EBADF);
/*
* sanity check
*/
if (msgp == NULL)
return (EINVAL);
/*
* Check if the command is actually supported
* if not, return an error
*/
if (rsc_check_unsupported_cmd(msgp->type) != 0)
return (ENOTSUP);
/*
* Check if this command will generate a response and if it will not,
* return an error.
*/
if (!rsc_cmd_response_guaranteed(msgp->type))
return (ENOTSUP);
/*
* init rx buffer
*/
rsc_rx_resp_len = 0;
rsc_rx_error = 0;
req->msg_type = msgp->type;
req->msg_len = msgp->len;
req->msg_buf = msgp->data;
if ((rr_item = rsc_lookup_rr_table(rr_table, rr_table_cnt,
msgp->type)) != NULL) {
resp->msg_type = rr_item->resp_type;
if (rr_item->resp_type == DP_NULL_MSG) {
/*
* no reply expected. so, no reply buffer needed
* (set to NULL)
*/
resp->msg_len = 0;
resp->msg_buf = (caddr_t)NULL;
} else {
resp->msg_len = RSC_MAX_RX_BUFFER;
resp->msg_buf = (caddr_t)rsc_rx_buffer;
}
rr.wait_time = rr_item->timeout;
rsc_rx_resp_type = rr_item->resp_type;
} else {
return (ENOTSUP);
}
rr.status = 0;
#ifdef DEBUG
printf("request/response %x/%x\n", req->msg_type, resp->msg_type);
#endif
if (ioctl(rsc_fd, RMCADM_REQUEST_RESPONSE, &rr) < 0) {
#ifdef DEBUG
printf("rscp_send: req %x failed, status=%d errno=%d\n",
rr.req.msg_type, rr.status, errno);
#endif
rsc_rx_error = errno;
return (errno);
}
/*
* reply received. get the number of bytes effectively returned
*/
rsc_rx_resp_len = resp->msg_bytes;
rsc_rx_resp_type = resp->msg_type;
#ifdef DEBUG
printf("got reply type=%x len=%d\n", rsc_rx_resp_type, rsc_rx_resp_len);
#endif
return (0);
}
/*
* This function is obsolete and it is provided for backward compatibility
* Previously, rscp_send() and rscp_recv() where used to send a request and
* read a reply repectively. Now, rscp_send_recv() should be used instead
* (request/response in one call).
*
* This function returns the reply received when a request was previously sent
* using the rscp_send() function (stored in the rsc_rx_buffer buffer). If a
* reply was not received, then an error is returned.
*
* timeout parameter is declared for backward compatibility but it is not used.
*/
/*ARGSUSED*/
int
rscp_recv(rscp_msg_t *msgp, struct timespec *timeout)
{
int err = 0;
if (rsc_fd < 0)
return (EBADF);
/*
* sanity check
*/
if (msgp == NULL)
return (EINVAL);
if (rsc_rx_error < 0) {
msgp->type = DP_NULL_MSG;
msgp->len = 0;
msgp->data = NULL;
err = rsc_rx_error;
} else {
msgp->type = rsc_rx_resp_type;
msgp->len = rsc_rx_resp_len;
msgp->data = rsc_rx_buffer;
}
#ifdef DEBUG
printf("read reply. type=%x, err=%d\n", msgp->type, err);
#endif
rsc_rx_resp_len = 0;
rsc_rx_error = 0;
rsc_rx_resp_type = DP_NULL_MSG;
return (err);
}
/*
* used to free up a (received) message. no-op function
*/
/*ARGSUSED*/
int
rscp_free_msg(rscp_msg_t *msgp)
{
if (rsc_fd < 0)
return (EBADF);
return (0);
}
|