File: oem_force_conn.c

package info (click to toggle)
openipmi 2.0.25-2.1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 11,560 kB
  • sloc: ansic: 150,609; python: 8,801; sh: 5,752; perl: 1,356; makefile: 561
file content (327 lines) | stat: -rw-r--r-- 8,724 bytes parent folder | download | duplicates (8)
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
/*
 * ipmi_oem_force.c
 *
 * MontaVista IPMI code for handling Force Computer's boards.
 *
 * Author: MontaVista Software, Inc.
 *         Corey Minyard <minyard@mvista.com>
 *         source@mvista.com
 *
 * Copyright 2003 MontaVista Software Inc.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *
 *  THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <OpenIPMI/ipmi_conn.h>
#include <OpenIPMI/ipmi_err.h>

#include <OpenIPMI/internal/ipmi_oem.h>
#include <OpenIPMI/internal/ipmi_int.h>

static int
ipmb_handler(ipmi_con_t *ipmi, ipmi_msgi_t *rspi)
{
    ipmi_msg_t           *msg = &rspi->msg;
    ipmi_ll_ipmb_addr_cb handler = rspi->data1;
    void                 *cb_data = rspi->data2;
    unsigned char        ipmb[MAX_IPMI_USED_CHANNELS];
    int                  err = 0;
    
    memset(ipmb, 0, sizeof(*ipmb));

    if (msg->data[0] != 0)
	err = IPMI_IPMI_ERR_VAL(msg->data[0]);
    else if (msg->data_len < 4)
	err = EINVAL;
    else
	ipmb[0] = msg->data[2];

    if (!err)
	ipmi->set_ipmb_addr(ipmi, ipmb, 1, ipmb[0] == 0x20, 0);

    if (handler)
	handler(ipmi, err, ipmb, 1, ipmb[0] == 0x20, 0, cb_data);
    return IPMI_MSG_ITEM_NOT_USED;
}

static int
force_ipmb_fetch(ipmi_con_t *conn, ipmi_ll_ipmb_addr_cb handler, void *cb_data)
{
    ipmi_system_interface_addr_t si;
    ipmi_msg_t                   msg;
    int                          rv;
    ipmi_msgi_t                  *rspi;

    rspi = ipmi_alloc_msg_item();
    if (!rspi)
	return ENOMEM;

    /* Send the OEM command to get the IPMB address. */
    si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
    si.channel = 0xf;
    si.lun = 0;
    msg.netfn = 0x30;
    msg.cmd = 4;
    msg.data = NULL;
    msg.data_len = 0;

    rspi->data1 = handler;
    rspi->data2 = cb_data;
    rv = conn->send_command(conn, (ipmi_addr_t *) &si, sizeof(si), &msg,
			    ipmb_handler, rspi);
    if (rv)
	ipmi_free_msg_item(rspi);
    return rv;
}

static int
activate_handler(ipmi_con_t *ipmi, ipmi_msgi_t  *rspi)
{
    ipmi_msg_t           *rmsg = &rspi->msg;
    ipmi_ll_ipmb_addr_cb handler = rspi->data1;
    void                 *cb_data = rspi->data2;
    unsigned char        ipmb = 0;
    int                  err = 0;
    int                  rv;
    
    if (rmsg->data[0] != 0) {
	err = IPMI_IPMI_ERR_VAL(rmsg->data[0]);
	if (handler)
	    handler(ipmi, err, &ipmb, 1, 0, 0, cb_data);
    }
    else {
	/* Now fetch the current state. */
	rv = force_ipmb_fetch(ipmi, handler, cb_data);
	if (rv) {
	    if (handler)
		handler(ipmi, rv, &ipmb, 1, 0, 0, cb_data);
	}
    }
    return IPMI_MSG_ITEM_NOT_USED;
}

static int
send_activate(ipmi_con_t           *ipmi,
	      int                  active,
	      ipmi_ll_ipmb_addr_cb handler,
	      void                 *cb_data)
{
    ipmi_system_interface_addr_t si;
    ipmi_msg_t                   msg;
    unsigned char                data[1];
    int                          rv;
    ipmi_msgi_t                  *rspi;

    rspi = ipmi_alloc_msg_item();
    if (!rspi)
	return ENOMEM;

    /* Send the OEM command to set the IPMB address. */
    si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
    si.channel = 0xf;
    si.lun = 0;
    msg.netfn = 0x30;
    msg.cmd = 3;
    if (active)
	data[0] = 0;
    else
	data[0] = 1;
    msg.data = data;
    msg.data_len = 1;

    rspi->data1 = handler;
    rspi->data2 = cb_data;
    rv = ipmi->send_command(ipmi, (ipmi_addr_t *) &si, sizeof(si), &msg,
			    activate_handler, rspi);
    if (rv)
	ipmi_free_msg_item(rspi);
    return rv;
}

static int
deactivated(ipmi_con_t *ipmi, ipmi_msgi_t  *rspi)
{
    ipmi_ll_ipmb_addr_cb handler = rspi->data1;
    void                 *cb_data = rspi->data2;
    int                  active = (long) rspi->data3;
    int                  rv;
    unsigned char        dummy;

    /* Don't care about errors from the deactivate, if no BMC was
       present then it doesn't really matter. */

    rv = send_activate(ipmi, active, handler, cb_data);
    if (rv)
	handler(ipmi, rv, &dummy, 0, 0, 0, cb_data);
    return IPMI_MSG_ITEM_NOT_USED;
}

static int
force_activate(ipmi_con_t           *conn,
	       int                  active,
	       ipmi_ll_ipmb_addr_cb handler,
	       void                 *cb_data)
{
    ipmi_ipmb_addr_t ipmb;
    ipmi_msg_t       msg;
    unsigned char    data[1];
    int              rv = EINVAL;

    if (active) {
	/* Deactivate any existing BMCs. */
	ipmi_msgi_t      *rspi;

	rspi = ipmi_alloc_msg_item();
	if (!rspi)
	    return ENOMEM;

	ipmb.addr_type = IPMI_IPMB_ADDR_TYPE;
	ipmb.channel = 0;
	ipmb.lun = 0;
	ipmb.slave_addr = 0x20;

	msg.netfn = 0x30;
	msg.cmd = 3;
	data[0] = 1;
	msg.data = data;
	msg.data_len = 1;

	rspi->data1 = handler;
	rspi->data2 = cb_data;
	rspi->data3 = (void *) (long) active;
	rv = conn->send_command(conn, (ipmi_addr_t *) &ipmb, sizeof(ipmb),
				&msg,
				deactivated, rspi);
	if (rv)
	    ipmi_free_msg_item(rspi);
	return rv;
    }

    if (rv)
	rv = send_activate(conn, active, handler, cb_data);

    return rv;
}

static int
force_oem_conn_handler(ipmi_con_t *conn, void *cb_data)
{
    conn->get_ipmb_addr = force_ipmb_fetch;
    conn->set_active_state = force_activate;
    return 0;
}

void
ipmi_oem_force_conn_init(void)
{
    int rv;

    /* The 735 card */
    rv = ipmi_register_oem_conn_handler(0x000e48,
					0x0804,
					force_oem_conn_handler,
					NULL);
    if (rv)
	ipmi_log(IPMI_LOG_SEVERE,
		 "oem_force_conn.c(ipmi_oem_force_conn_init): "
		 "Unable to initialize the Force 735 OEM handler: %x",
		 rv);

    /* The 740 card */
    rv = ipmi_register_oem_conn_handler(0x000e48,
					0x0808,
					force_oem_conn_handler,
					NULL);
    if (rv)
	ipmi_log(IPMI_LOG_SEVERE,
		 "oem_force_conn.c(ipmi_oem_force_conn_init): "
		 "Unable to initialize the Force 740 OEM handler: %x",
		 rv);

    /* The 786 card */
    rv = ipmi_register_oem_conn_handler(0x000e48,
					0x0810,
					force_oem_conn_handler,
					NULL);
    if (rv)
	ipmi_log(IPMI_LOG_SEVERE,
		 "oem_force_conn.c(ipmi_oem_force_conn_init): "
		 "Unable to initialize the Force 786 OEM handler: %x",
		 rv);

    /* The 550 card */
    rv = ipmi_register_oem_conn_handler(0x000e48,
					0x0880,
					force_oem_conn_handler,
					NULL);
    if (rv)
	ipmi_log(IPMI_LOG_SEVERE,
		 "oem_force_conn.c(ipmi_oem_force_conn_init): "
		 "Unable to initialize the Force 550 OEM handler: %x",
		 rv);

    /* The 560 card */
    rv = ipmi_register_oem_conn_handler(0x000e48,
					0x0888,
					force_oem_conn_handler,
					NULL);
    if (rv)
	ipmi_log(IPMI_LOG_SEVERE,
		 "oem_force_conn.c(ipmi_oem_force_conn_init): "
		 "Unable to initialize the Force 560 OEM handler: %x",
		 rv);

    /* The 690 card */
    rv = ipmi_register_oem_conn_handler(0x000e48,
					0x0900,
					force_oem_conn_handler,
					NULL);
    if (rv)
	ipmi_log(IPMI_LOG_SEVERE,
		 "oem_force_conn.c(ipmi_oem_force_conn_init): "
		 "Unable to initialize the Force 690 OEM handler: %x",
		 rv);

    /* The 695 card */
    rv = ipmi_register_oem_conn_handler(0x000e48,
					0x0904,
					force_oem_conn_handler,
					NULL);
    if (rv)
	ipmi_log(IPMI_LOG_SEVERE,
		 "oem_force_conn.c(ipmi_oem_force_conn_init): "
		 "Unable to initialize the Force 695 OEM handler: %x",
		 rv);
}

#if 0
/* If you include this as a module under Linux, you can use the
   following code to initialize it.  Otherwise, something has to call
   init_oem_force_conn(). */
static void (*const __init_patch_debug[1])                                    \
   (void) __attribute__ ((section(".ctors"))) = { init_oem_force };
#endif