summaryrefslogtreecommitdiff
path: root/util/ipmimv.c
blob: 4404beb9fbbb15bc9180caa625714f6230a7b022 (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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*M*
//  PVCS:
//      $Workfile:   ipmimv.c  $
//      $Revision:   1.1  $
//      $Modtime:   08 Apr 2003 15:31:14  $
//      $Author:   arcress at users.sourceforge.net  $  
//
//  This implements support for the /dev/ipmi0 native interface from
//  the MontaVista OpenIPMI Linux driver.
//
//  To build this as a standalone test program, do this:
//  # gcc -DLINUX  -g -O2  -DTEST_BIN -o ipmimv ipmimv.c
// 
//  01/29/03 ARC - created, derived from ipmi_test.c and ipmitool_mv.c
//  04/08/03 ARC - don't watch stdin on select, since stdin may not be
//                 valid if invoked from cron, etc.
//  06/11/03 ARC - ignore EMSGSIZE errno for get_wdt command
//  05/05/04 ARC - only open/close device once per application,
//		   rely on each app calling ipmi_close, helps performance.
//  08/10/04 ARC - handle alternate device filenames for some 2.6 kernels
//  03/01/05 ARC - fix /dev/ipmi0 IPMB requests (to other than BMC_SA)
//  04/12/07 ARC - check for IPMI_ASYNC_EVENT_RECV_TYPE in ipmicmd_mv
 *M*/
/*----------------------------------------------------------------------*
The BSD License 

Copyright (c) 2002-2005, Intel Corporation
Copyright (c) 2009 Kontron America, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

  a.. Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer. 
  b.. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution. 
  c.. Neither the name of Intel Corporation nor the names of its contributors 
      may be used to endorse or promote products derived from this software 
      without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *----------------------------------------------------------------------*/

#if defined(LINUX) || defined(BSD) || defined(MACOS) || defined(HPUX)
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#if defined(MACOS)
#include <sys/time.h>
#else
#include <sys/poll.h>
#endif
#ifdef SCO_UW
#include <sys/ioccom.h>
#endif

#ifdef TEST_BIN
#define ALONE  1
#endif

static void dbgmsg(char *pattn, ...);
#ifdef ALONE
#define uchar    unsigned char
#define ACCESS_OK        0
#define GET_SEL_ENTRY    0x43
static FILE *fperr = NULL;
static FILE *fpdbg = NULL;
void dump_buf(char *tag,uchar *pbuf,int sz, char fshowascii)
{
   if (pbuf == NULL || sz == 0) return;
   dbgmsg("%s sz=%d buf: %02x %02x %02x %02x\n",tag,sz,
		pbuf[0],pbuf[1], pbuf[2],pbuf[3]);
}
#else
#include "ipmicmd.h"
extern FILE *fperr;  /*defined in ipmicmd.c*/
extern FILE *fpdbg;  /*defined in ipmicmd.c*/
extern ipmi_cmd_t ipmi_cmds[NCMDS];
#endif

#define MV_BUFFER_SIZE   300    /*see IPMI_RSPBUF_SIZE also */
#define IPMI_MAX_ADDR_SIZE 32
#define IPMI_RESPONSE_RECV_TYPE         1
#define IPMI_ASYNC_EVENT_RECV_TYPE      2
#define IPMI_CMD_RECV_TYPE              3
#define IPMI_RESPONSE_RESPONSE_TYPE     4

#ifdef TV_PORT
/* use this to define timeval if it is a portability issue */
struct timeval {
        long int     tv_sec;         /* (time_t) seconds */
        long int     tv_usec;        /* (suseconds_t) microseconds */
};
#endif

int ipmi_timeout_mv = 10;   /* 10 seconds, was 5 sec */
#if defined(BSD) || defined(MACOS) || defined(HPUX)
#pragma pack(1)
#endif

struct ipmi_addr
{
        int   adrtype;
        short channel;
        char  data[IPMI_MAX_ADDR_SIZE];
};

struct ipmi_msg
{
        uchar  netfn;
        uchar  cmd;
        ushort data_len;
        uchar  *data;
};

struct ipmi_req
{
        unsigned char *addr; /* Address to send the message to. */
        unsigned int  addr_len;
        long    msgid; /* The sequence number for the message.  */
        struct ipmi_msg msg;
};
 
struct ipmi_recv
{
        int     recv_type;  	/* Is this a command, response, etc. */
        unsigned char *addr;    /* Address the message was from */
	int  addr_len;  	/* The size of the address buffer. */
        long    msgid;  	/* The sequence number from the request */
        struct ipmi_msg msg; 	/* The data field must point to a buffer. */
};

struct ipmi_cmdspec
{
        unsigned char netfn;
        unsigned char cmd;
};
#if defined(BSD) || defined(MACOS) || defined(HPUX)
#pragma pack()
/* FreeBSD 7.x ipmi ioctls, use _IOW */
#define IPMI_IOC_MAGIC             'i'
#define IPMICTL_RECEIVE_MSG_TRUNC  _IOWR(IPMI_IOC_MAGIC, 11, struct ipmi_recv)
#define IPMICTL_RECEIVE_MSG        _IOWR(IPMI_IOC_MAGIC, 12, struct ipmi_recv)
#define IPMICTL_SEND_COMMAND       _IOW(IPMI_IOC_MAGIC, 13, struct ipmi_req)
#define IPMICTL_REGISTER_FOR_CMD   _IOW(IPMI_IOC_MAGIC, 14, struct ipmi_cmdspec)
#define IPMICTL_UNREGISTER_FOR_CMD _IOW(IPMI_IOC_MAGIC, 15, struct ipmi_cmdspec)
#define IPMICTL_SET_GETS_EVENTS_CMD _IOW(IPMI_IOC_MAGIC, 16, int)
#define IPMICTL_SET_MY_ADDRESS_CMD  _IOW(IPMI_IOC_MAGIC, 17, unsigned int)
#define IPMICTL_GET_MY_ADDRESS_CMD  _IOW(IPMI_IOC_MAGIC, 18, unsigned int)

#else
/* Linux ipmi ioctls */
#define IPMI_IOC_MAGIC 'i'
#define IPMICTL_RECEIVE_MSG_TRUNC   _IOWR(IPMI_IOC_MAGIC, 11, struct ipmi_recv)
#define IPMICTL_RECEIVE_MSG         _IOWR(IPMI_IOC_MAGIC, 12, struct ipmi_recv)
#define IPMICTL_SEND_COMMAND        _IOR(IPMI_IOC_MAGIC,  13, struct ipmi_req)
#define IPMICTL_REGISTER_FOR_CMD    _IOR(IPMI_IOC_MAGIC, 14,struct ipmi_cmdspec)
#define IPMICTL_UNREGISTER_FOR_CMD  _IOR(IPMI_IOC_MAGIC, 15,struct ipmi_cmdspec)
#define IPMICTL_SET_GETS_EVENTS_CMD _IOR(IPMI_IOC_MAGIC,  16, int)
#define IPMICTL_SET_MY_ADDRESS_CMD  _IOR(IPMI_IOC_MAGIC, 17, unsigned int)
#define IPMICTL_GET_MY_ADDRESS_CMD  _IOR(IPMI_IOC_MAGIC, 18, unsigned int)
#endif

/* MAINT ioctls used below, but only valid for Linux */
#define IPMICTL_GET_MAINTENANCE_MODE_CMD _IOR(IPMI_IOC_MAGIC, 30, int)
#define IPMICTL_SET_MAINTENANCE_MODE_CMD _IOW(IPMI_IOC_MAGIC, 31, int)
#define IPMICTL_GETMAINT  0x8004691e   /*only valid if OpenIPMI >=v39.1 */
#define IPMICTL_SETMAINT  0x4004691f   /*only valid if OpenIPMI >=v39.1 */

#define BMC_SA   	  0x20
#define IPMI_BMC_CHANNEL  0xf
#define IPMI_SYSTEM_INTERFACE_ADDR_TYPE 0x0c
#define IPMI_IPMB_ADDR_TYPE             0x01
#define IPMI_IPMB_BROADCAST_ADDR_TYPE   0x41
/* Broadcast get device id is used as described in IPMI 1.5 section 17.9. */
#define IPMI_LAN_ADDR_TYPE              0x04

struct ipmi_system_interface_addr
{
        int           adrtype;
        short         channel;
        unsigned char lun;
};

struct ipmi_ipmb_addr
{
        int    adrtype;
        short  channel;
        uchar  slave_addr;
        uchar  lun;
};

struct ipmi_lan_addr
{
        int           adrtype;
        short         channel;
        unsigned char privilege;
        unsigned char session_handle;
        unsigned char remote_SWID;
        unsigned char local_SWID;
        unsigned char lun;
};

static int ipmi_fd = -1;
static int curr_seq = 0;
static int fdebugmv = 0;
static struct ipmi_addr rsp_addr;  /*used in getevent_mv, ipmi_rsp_mv*/
static int rsp_addrlen = 0;        /*used in getevent_mv, ipmi_rsp_mv*/

void ipmi_get_mymc(uchar *bus, uchar *sa, uchar *lun, uchar *type);

static void dbgmsg(char *pattn, ...)
{
    va_list arglist;

    if (fpdbg == NULL) return;
    // if (fdebugmv == 0) return;
    va_start( arglist, pattn );
    vfprintf( fpdbg, pattn, arglist );
    va_end( arglist );
    fflush( fpdbg );
}

int ipmi_open_mv(char fdebugcmd)
{
    char *pdev;
    uchar bus, sa, lun;

#ifdef ALONE
    fperr = stderr;
    fpdbg = stdout;
#endif

    if (ipmi_fd != -1) return(0); /*already open*/
    fdebugmv = fdebugcmd;
    pdev = "/dev/ipmi/0";
    ipmi_fd = open("/dev/ipmi/0", O_RDWR);
    if (ipmi_fd == -1) {
		if (fdebugcmd) dbgmsg("ipmi_open_mv: cannot open %s\n",pdev);
		pdev = "/dev/ipmi0";
		ipmi_fd = open(pdev, O_RDWR);
    }
    if (ipmi_fd == -1) {
		if (fdebugcmd) dbgmsg("ipmi_open_mv: cannot open %s\n",pdev);
		pdev = "/dev/ipmidev0";
		ipmi_fd = open(pdev, O_RDWR);
    }
    if (ipmi_fd == -1) {
		if (fdebugcmd) dbgmsg("ipmi_open_mv: cannot open %s\n",pdev);
		pdev = "/dev/ipmidev/0";
		ipmi_fd = open(pdev, O_RDWR);
    }
    if (ipmi_fd == -1) { 
		if (fdebugcmd) dbgmsg("ipmi_open_mv: cannot open %s\n",pdev);
		return(-1);
    }
    ipmi_get_mymc(&bus,&sa,&lun,NULL);
    if (sa != BMC_SA) { /* user specified my slave address*/
	int a, rv;
	a = sa;
	rv = ioctl(ipmi_fd, IPMICTL_SET_MY_ADDRESS_CMD, &a);
	if (fdebugcmd) dbgmsg("ipmi_open_mv: set_my_address(%x) rv=%d\n",sa,rv);
	if (rv < 0) {
	   return(rv);
	}
    }
    
    if (fdebugcmd) {
	dbgmsg("ipmi_open_mv: successfully opened %s, fd=%d\n",pdev,ipmi_fd);
    }
    return(0);
}

int ipmi_close_mv(void)
{
    int rc = 0;
    if (ipmi_fd != -1) { 
	rc = close(ipmi_fd);
	ipmi_fd = -1; 
    }
    return(rc);
}

int ipmi_rsp_mv(uchar cmd, uchar netfn, uchar lun, uchar sa, uchar bus,
		uchar *pdata, int sdata, char fdebugcmd)
{
    struct ipmi_req       req;
    // struct ipmi_recv      rsp;
    struct ipmi_lan_addr  lan_addr;
    // int    i, done;
    int    rv;

    rv = ipmi_open_mv(fdebugcmd);
    if (rv != 0) return(rv);

    if (rsp_addrlen > 0) {
        /* rsp_addr was previously saved in getevent_mv */
	req.addr = (char *)&rsp_addr;
	req.addr_len = rsp_addrlen;
    } else {  /* try some defaults */
	lan_addr.adrtype = IPMI_LAN_ADDR_TYPE;
	lan_addr.channel = bus;   /* usu lan_ch == 1 */
        lan_addr.privilege = 0x04;  /*admin*/
	lan_addr.session_handle = 0x01; /*may vary*/
	lan_addr.remote_SWID = sa; /*usu 0x81*/
	lan_addr.local_SWID = 0x81;
	lan_addr.lun = lun;
	req.addr = (char *) &lan_addr;
	req.addr_len = sizeof(lan_addr);
    }
    req.msg.cmd = cmd;
    req.msg.netfn = (netfn | 0x01);
    req.msgid = curr_seq;
    req.msg.data = pdata;
    req.msg.data_len = sdata;
    rv = ioctl(ipmi_fd, IPMICTL_SEND_COMMAND, &req);
    curr_seq++;
    if (rv == -1) { 
        if (fdebugcmd) dbgmsg("mv IPMICTL_SEND_COMMAND errno %d\n",errno);
	rv = errno; 
    }
    return(rv);
}

int ipmicmd_mv(uchar cmd, uchar netfn, uchar lun, uchar sa, uchar bus,
		uchar *pdata, int sdata, uchar *presp, int sresp, int *rlen)
{
    fd_set readfds;
    struct timeval tv;
    struct ipmi_req       req;
    struct ipmi_recv      rsp;
    struct ipmi_addr      addr;
    struct ipmi_ipmb_addr             ipmb_addr;
    struct ipmi_system_interface_addr bmc_addr;
    static int need_set_events = 1;
    int    i, done;
    int    rv;

    rv = ipmi_open_mv(fdebugmv);
    if (rv != 0) return(rv);

    if (need_set_events) {
	i = 1;
        rv = ioctl(ipmi_fd, IPMICTL_SET_GETS_EVENTS_CMD, &i);
	if (fdebugmv) 
	    dbgmsg("getevent_mv: set_gets_events rv=%d errno=%d, n=%d\n",
			rv,errno,i);
        if (rv) { return(errno); }
	need_set_events = 0;
    }

    FD_ZERO(&readfds);
    // FD_SET(0, &readfds);  /* dont watch stdin */
    FD_SET(ipmi_fd, &readfds);  /* only watch ipmi_fd for input */

    /* Special handling for ReadEventMsgBuffer, etc. */
#ifdef TEST_MSG
    recv.msg.data = data;
    recv.msg.data_len = sizeof(data);
    recv.addr = (unsigned char *) &addr;
    recv.addr_len = sizeof(addr);
    rv = ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &recv);
    if (rv == -1) {
        if (errno == EMSGSIZE) {
            /* The message was truncated, handle it as such. */
            data[0] = IPMI_REQUESTED_DATA_LENGTH_EXCEEDED_CC;
            rv = 0;
        } else
            goto out;
    }
#endif

    /* Send the IPMI command */ 
    if (sa == BMC_SA) {
	i = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	bmc_addr.adrtype = i;
	bmc_addr.channel = IPMI_BMC_CHANNEL;
	bmc_addr.lun = lun;       /* usu BMC_LUN = 0 */
	req.addr = (char *) &bmc_addr;
	req.addr_len = sizeof(bmc_addr);
    } else {
	i = IPMI_IPMB_ADDR_TYPE;
	ipmb_addr.adrtype = i;
	ipmb_addr.channel = bus;   /* usu PUBLIC_BUS = 0 */
	ipmb_addr.slave_addr = sa;
	ipmb_addr.lun = lun;
	req.addr = (char *) &ipmb_addr;
	req.addr_len = sizeof(ipmb_addr);
    }
    if (fdebugmv) 
	dbgmsg("mv cmd=%02x netfn=%02x mc=%02x;%02x;%02x adrtype=%x\n",
			cmd,netfn,bus,sa,lun,i);
    req.msg.cmd = cmd;
    req.msg.netfn = netfn;   
    req.msgid = curr_seq;
    req.msg.data = pdata;
    req.msg.data_len = sdata;
    rv = ioctl(ipmi_fd, IPMICTL_SEND_COMMAND, &req);
    curr_seq++;
    if (rv == -1) { 
        if (fdebugmv) dbgmsg("mv IPMICTL_SEND_COMMAND errno %d\n",errno);
	rv = errno; 
	}

    if (netfn & 0x01) done = 1;  /*sending response only*/
    else done = 0;  /*normal request/response*/

    if (rv == 0) while (!done) {
        done = 1;
	tv.tv_sec=ipmi_timeout_mv;
	tv.tv_usec=0;
	rv = select(ipmi_fd+1, &readfds, NULL, NULL, &tv);
	/* expect select rv = 1 here */
	if (rv <= 0) { /* no data within 5 seconds */
	   if (fdebugmv) 
              fprintf(fperr,"mv select timeout, fd = %d, isset = %d, rv = %d, errno = %d\n",
		  ipmi_fd,FD_ISSET(ipmi_fd, &readfds),rv,errno);
	   if (rv == 0) rv = -3;
	   else rv = errno;
	} else {
	   /* receive the IPMI response */
	   rsp.addr = (char *) &addr;
	   rsp.addr_len = sizeof(addr);
	   rsp.msg.data = presp;
	   rsp.msg.data_len = sresp;
	   rv = ioctl(ipmi_fd, IPMICTL_RECEIVE_MSG_TRUNC, &rsp);
	   if (rv == -1) { 
	      if ((errno == EMSGSIZE) && (rsp.msg.data_len == sresp))
		 rv = 0;   /* errno 90 is ok */
	      else { 
		 rv = errno; 
                 fprintf(fperr,"mv rcv_trunc errno = %d, len = %d\n",
			errno, rsp.msg.data_len);
	      }
	   } else rv = 0;
	   /* Driver should ensure matching req.msgid and rsp.msgid */
           /* Skip & retry if async events, only listen for those in 
            * getevent_mv() below. */
           // if (rsp.recv_type == IPMI_ASYNC_EVENT_RECV_TYPE) 
	   if (rsp.recv_type != IPMI_RESPONSE_RECV_TYPE) {
		if (fdebugmv)
		   dbgmsg("mv cmd=%02x netfn=%02x, got recv_type %d\n",
				cmd,netfn,rsp.recv_type);
	       done = 0;
	   }
	   *rlen = rsp.msg.data_len;
	}
    } /*endif send ok, while select/recv*/

    /* ipmi_close_mv();  * rely on the app calling ipmi_close */
    return(rv);
}

int ipmi_cmdraw_mv(uchar cmd, uchar netfn, uchar lun, uchar sa, uchar bus,
		uchar *pdata, int sdata, uchar *presp, int *sresp, 
		uchar *pcc, char fdebugcmd)
{
    uchar  buf[MV_BUFFER_SIZE];
    int rc, szbuf;
    int rlen = 0;
    uchar cc;

    if (fdebugcmd) { 
         dbgmsg("mv cmd=%02x netfn=%02x lun=%02x sdata=%d sresp=%d\n",
		 cmd,netfn,lun,sdata,*sresp); 
         dump_buf("mv cmd data",pdata,sdata,0);
    }
    szbuf = sizeof(buf);
    if (*sresp <  2) ;   /*just completion code*/
    else if (*sresp < szbuf) szbuf = *sresp + 1;
    else if (fdebugcmd) 
	dbgmsg("mv sresp %d >= szbuf %d, truncated\n",*sresp,szbuf);
    rc = ipmicmd_mv(cmd,netfn,lun,sa, bus, pdata,sdata, buf,szbuf,&rlen);
    cc = buf[0];
    if (fdebugcmd) {
        dbgmsg("ipmi_cmdraw_mv: status=%d ccode=%x rlen=%d\n",
		(uint)rc,cc,rlen);
        if (rc == 0) dump_buf("mv rsp data",buf,rlen,0);
    }
    if (rlen > 0) { /* copy data, except first byte */
       rlen -= 1;
       if (rlen > *sresp) rlen = *sresp;
       memcpy(presp,&buf[1],rlen);
    }
    *pcc = cc;
    *sresp = rlen;
    return(rc);
}

#ifdef ALONE
void ipmi_get_mymc(uchar *bus, uchar *sa, uchar *lun, uchar *type)
{
   if (bus != NULL)  *bus = 0;  //PUBLIC_BUS;
   if (sa != NULL)   *sa  = BMC_SA;
   if (lun != NULL)  *lun = 0;  //BMC_LUN;
   if (type != NULL) *type = 1; //ADDR_SMI;
}
#else
int ipmi_cmd_mv(ushort cmd, uchar *pdata, int sdata, uchar *presp, 
		int *sresp, uchar *pcc, char fdebugcmd)
{
    uchar  buf[MV_BUFFER_SIZE];
    int rc, i, szbuf;
    uchar   cc;
    int rlen = 0;
    int xlen, j;
    uchar sa, lun, bus, mtype;

    for (i = 0; i < NCMDS; i++) {
       if (ipmi_cmds[i].cmdtyp == cmd) break;
    }
    if (i >= NCMDS) {
	fprintf(fperr, "ipmi_cmd_mv: Unknown command %x\n",cmd);
	return(-1); 
	}
    if (cmd >= CMDMASK) cmd = cmd & CMDMASK;  /* unmask it */

    if (fdebugcmd) { 
         dbgmsg( "mv cmd=%02x netfn=%02x lun=%02x sdata=%d sresp=%d\n",
		cmd,ipmi_cmds[i].netfn,ipmi_cmds[i].lun,sdata, *sresp); 
         dump_buf("mv cmd data",pdata,sdata,0);
    }
    szbuf = sizeof(buf);
    if (*sresp < szbuf && *sresp >= 2) szbuf = *sresp + 1;
    else if (fdebugcmd) 
	dbgmsg("mv sresp %d >= szbuf %d, truncated\n",*sresp,szbuf);
    // ipmi_cmds[i].lun, ipmi_cmds[i].sa, ipmi_cmds[i].bus,
    ipmi_get_mc(&bus, &sa, &lun, &mtype);
    rc = ipmicmd_mv(cmd,ipmi_cmds[i].netfn, lun, sa, bus,
			pdata,sdata,buf,szbuf,&rlen);
    // if (rc == -1) dbgmsg("ipmi_cmd_mv: cannot open /dev/ipmi0\n");
    cc = buf[0];
    if (fdebugcmd) {
	    dbgmsg("ipmi_cmd_mv: ipmicmd_mv status=%x, ccode=%x\n", 
                      (uint)rc, cc);
            if (rc == ACCESS_OK) {
               uchar * pc; int sz;
               sz = rlen;
               pc = (uchar *)buf;
               dbgmsg("ipmi_cmd_mv: response (len=%d): ",sz);
               for (j = 0; j < sz; j++) dbgmsg("%02x ",pc[j]);
               dbgmsg("\n");
            }
        }
    xlen = ipmi_cmds[i].rslen + 1;
    if ((ipmi_cmds[i].cmdtyp == GET_SEL_ENTRY) && 
        (rlen < xlen) && (rc == 0) && (cc != 0) &&
        (i > 0) && (rlen > 1))                  /*not temp slot, have data*/ 
    {
          /* Detect/Handle MV driver SEL bug returning missing bytes */
          if (fdebugcmd) {
              dbgmsg("ipmi_cmd_mv[%d] BUG: returned %d, expected %d\n",
				i,rlen,xlen);
          }
          cc = 0x80;  /*flag as busy, retry*/
          j = xlen - rlen;
          j--;  /* omit cc */
          for (i = 0; i < j; i++) presp[i] = 0xff;
          if ((rlen+j) > *sresp) rlen = *sresp - j; 
          memcpy(&presp[j],&buf[0],rlen);
          rlen += j;
    }
    if (rlen > 0) {
       /* copy data, except first byte */
       rlen -= 1;
       if (rlen > *sresp) rlen = *sresp;
       memcpy(presp,&buf[1],rlen);
    }
    *pcc = cc;
    *sresp = rlen;

    return(rc);
}  /*end ipmi_cmd_mv*/
#endif

int setmaint_mv(uchar mode, uchar *cc)
{
    int data[2]; 
    int rv;
    /* 
     * Normally OpenIPMI driver issues ReadEventMessageBuffer cmds
     * every 1 second.  Maint mode disables that polling in 
     * driver v39.1 and greater.
     * maintenance mode values: 
     * 2 = turn on maint mode
     * 1 = turn off maint mode
     * 0 = automatic maint mode (on for 30 sec if reset or fw request)
     */

    /* should have called ipmi_open_mv in a previous call */
    rv = ioctl(ipmi_fd, IPMICTL_GETMAINT, &data);
    if (rv == -1) {
        if (errno != 0) *cc = errno;
    } else *cc = 0;
    if (fdebugmv) dbgmsg("getmaint: rv=%d mode=%d\n",rv,data[0]);  

    data[0] = mode;  
    rv = ioctl(ipmi_fd, IPMICTL_SETMAINT, &data);
    if (rv == -1) {
        if (errno != 0) *cc = errno;
    } else *cc = 0;
    return(rv);
}

int register_async_mv(uchar cmd, uchar netfn)
{
    uchar data[2]; 
    int rv;

    data[0] = netfn;
    data[1] = cmd;
    rv = ioctl(ipmi_fd, IPMICTL_REGISTER_FOR_CMD, &data);
    if (fdebugmv) dbgmsg("register_async_mv(%x,%x) rv=%d\n",cmd,netfn,rv);
    return(rv);
}

int unregister_async_mv(uchar cmd, uchar netfn)
{
    uchar data[2]; 
    int rv;

    data[0] = netfn;
    data[1] = cmd;
    rv = ioctl(ipmi_fd, IPMICTL_UNREGISTER_FOR_CMD, &data);
    if (fdebugmv) dbgmsg("unregister_async_mv(%x,%x) rv=%d\n",cmd,netfn,rv);
    return(rv);
}

int getevent_mv(uchar *evt_data, int *evt_len, uchar *cc, int timeout)
{
    struct ipmi_recv rsp;
    uchar data[36];  /* #define MAX_IPMI_DATA_SIZE 36 */
    struct ipmi_addr  addr;
    static int need_set_events = 1;
    int rv = 0;
    int n;
 
    if (need_set_events) {
	n = 1;
        rv = ioctl(ipmi_fd, IPMICTL_SET_GETS_EVENTS_CMD, &n);
	if (fdebugmv) 
	    dbgmsg("getevent_mv: set_gets_events rv=%d errno=%d, n=%d\n",
			rv,errno,n);
	need_set_events = 0;
    }

    /* wait for the mv openipmi driver to provide input to fd */
    if (timeout == 0) 
    {   /*do poll*/
#if defined(MACOS)
	/* there is no poll function in MACOS, so skip this. */
#else
    	struct pollfd myfd;
    	myfd.fd = ipmi_fd;
    	myfd.events = POLLIN;
    	myfd.revents = 0;
    	rv = poll(&myfd,1,-1);
    	if (rv <= 0) {
		if (fdebugmv) dbgmsg("getevent_mv poll rv=%d\n",rv);
		return(rv);
    	}
        /* else have input ready to read (myfd.revents & POLLIN)*/
	if (fdebugmv) dbgmsg("getevent_mv poll revents %x\n",myfd.revents);
#endif
    }

    /* read the message from the ipmi_fd */
    rsp.msg.data = data;
    rsp.msg.data_len = sizeof(data);
    rsp.addr = (unsigned char *) &addr;
    rsp.addr_len = sizeof(addr);
    rv = ioctl(ipmi_fd, IPMICTL_RECEIVE_MSG_TRUNC, &rsp);
    if (rv < 0) {  
	if (fdebugmv) dbgmsg("getevent_mv rv=%d, errno=%d\n",rv,errno);
        if (errno == EMSGSIZE) { /* The message was truncated */
            *cc = 0xC8;   /*IPMI_REQUESTED_DATA_LENGTH_EXCEEDED_CC;*/
            rsp.msg.data_len = sizeof(data);
            rv = 0;
        }
        else if (errno == EINTR) { return(EINTR); }
    } else *cc = 0;
    if (rv == 0) {
        n = rsp.msg.data_len;
	if (fdebugmv) {
	    dbgmsg("getevent_mv: recv_type=%x cmd=%x data_len=%d\n",
		   rsp.recv_type,rsp.msg.cmd,n);
	    // if (n > 0) dump_buf("mv rsp.msg.data",rsp.msg.data, n, 0);
	    // dump_buf("mv rsp.addr",rsp.addr,rsp.addr_len,0);
	}
	if (rsp.recv_type == IPMI_CMD_RECV_TYPE) {
	    evt_data[0] = rsp.recv_type;
            evt_data[1] = rsp.msg.netfn;
            evt_data[2] = rsp.msg.cmd;
	    if (n > 0) 
               memcpy(&evt_data[3],&data[0],n);
	    n += 3;
	    /* save the response address */
	    memcpy(&rsp_addr,rsp.addr,rsp.addr_len);
	    rsp_addrlen = rsp.addr_len;
	} else if (rsp.recv_type == IPMI_RESPONSE_RESPONSE_TYPE) {
	    evt_data[0] = rsp.recv_type;
            evt_data[1] = rsp.msg.netfn;
            evt_data[2] = rsp.msg.cmd;
            evt_data[3] = data[0];
	    n += 3;
        } else {  /* rsp.recv_type == IPMI_ASYNC_EVENT_RECV_TYPE */
	    if (n > 0) 
               memcpy(evt_data,&data[0],n);
	}
        *evt_len = n;
    } else if (rv == -1 || rv == -11) {
        rv = 0x80;  /* -EAGAIN, no data, try again */
    }
    return(rv);
}

#ifdef TEST_BIN
int main(int argc, char *argv[])
{
    fd_set readfds;
    struct timeval tv;
    uchar  data[40];
    int    i, j;
    int    err, rv;
    int    rlen = 0;
    uchar cc;

    fperr = stderr;
    fpdbg = stdout;
    fdebugmv = 1;
    rlen = sizeof(data);
    err = ipmi_cmdraw_mv(0x01, 0x06, 0, 0x20, 0,  /*get_device_id*/
			NULL, 0, data, &rlen, &cc, fdebugmv);
    dbgmsg("ipmi_cmdraw_mv ret=%d, cc=%02x\n",err,cc);
    rv = err;
    if (err == 0) {
       rv = cc;
       dbgmsg(" ** Return Code: %2.2X\n", cc);
       dbgmsg(" ** Data[%d]:",rlen);
       for (i=0; i < rlen; i++)
	    dbgmsg(" %2.2X", (uchar)data[i]);
       dbgmsg("\n");
    }

    err = setmaint_mv(2,&cc);
    dbgmsg("setmaint_mv(2) err=%d cc=%d\n",err,cc);
    err = setmaint_mv(1,&cc);
    dbgmsg("setmaint_mv(1) err=%d cc=%d\n",err,cc);
    err = setmaint_mv(0,&cc);
    dbgmsg("setmaint_mv(0) err=%d cc=%d\n",err,cc);

    err = register_async_mv(0x10,0x06);
    dbgmsg("register_async_mv(10,6) rv=%d\n",err);
    err = register_async_mv(0x01,0x06);
    dbgmsg("register_async_mv(01,6) rv=%d\n",err);
    err = unregister_async_mv(0x01,0x06);
    dbgmsg("unregister_async_mv(01,6) rv=%d\n",err);
    err = unregister_async_mv(0x10,0x06);
    dbgmsg("unregister_async_mv(10,6) rv=%d\n",err);

    dbgmsg("\n");
    ipmi_close_mv();
    return rv;
}
#endif

#endif