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
|
/*
* Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
/* $FreeBSD: src/sys/dev/ipmi/ipmi.c,v 1.16 2011/11/07 15:43:11 ed Exp $ */
/*
* Copyright 2012, Joyent, Inc. All rights reserved.
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
#include <sys/devops.h>
#include <sys/conf.h>
#include <sys/modctl.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/cred.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/cmn_err.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/smbios.h>
#include <sys/smbios_impl.h>
#include <sys/ipmi.h>
#include "ipmivars.h"
/*
* Request management.
*/
/* Allocate a new request with request and reply buffers. */
struct ipmi_request *
ipmi_alloc_request(struct ipmi_device *dev, long msgid, uint8_t addr,
uint8_t command, size_t requestlen, size_t replylen)
{
struct ipmi_request *req;
req = kmem_zalloc(sizeof (struct ipmi_request) + requestlen + replylen,
KM_SLEEP);
req->ir_sz = sizeof (struct ipmi_request) + requestlen + replylen;
req->ir_owner = dev;
req->ir_msgid = msgid;
req->ir_addr = addr;
req->ir_command = command;
if (requestlen) {
req->ir_request = (uchar_t *)&req[1];
req->ir_requestlen = requestlen;
}
if (replylen) {
req->ir_reply = (uchar_t *)&req[1] + requestlen;
req->ir_replybuflen = replylen;
}
cv_init(&req->ir_cv, NULL, CV_DEFAULT, NULL);
req->ir_status = IRS_ALLOCATED;
return (req);
}
/* Free a request no longer in use. */
void
ipmi_free_request(struct ipmi_request *req)
{
if (req == NULL)
return;
cv_destroy(&req->ir_cv);
kmem_free(req, req->ir_sz);
}
/* Store a processed request on the appropriate completion queue. */
/*ARGSUSED*/
void
ipmi_complete_request(struct ipmi_softc *sc, struct ipmi_request *req)
{
struct ipmi_device *dev;
IPMI_LOCK_ASSERT(sc);
if (req->ir_status == IRS_CANCELED) {
ASSERT(req->ir_owner == NULL);
ipmi_free_request(req);
return;
}
req->ir_status = IRS_COMPLETED;
/*
* Anonymous requests (from inside the driver) always have a
* waiter that we awaken.
*/
if (req->ir_owner == NULL) {
cv_signal(&req->ir_cv);
} else {
dev = req->ir_owner;
TAILQ_INSERT_TAIL(&dev->ipmi_completed_requests, req, ir_link);
pollwakeup(dev->ipmi_pollhead, POLLIN | POLLRDNORM);
dev->ipmi_status &= ~IPMI_BUSY;
if (dev->ipmi_status & IPMI_CLOSING)
cv_signal(&dev->ipmi_cv);
}
}
/*
* Enqueue an internal driver request and wait until it is completed.
*/
static int
ipmi_submit_driver_request(struct ipmi_softc *sc, struct ipmi_request **preq,
int timo)
{
int error;
struct ipmi_request *req = *preq;
ASSERT(req->ir_owner == NULL);
IPMI_LOCK(sc);
error = sc->ipmi_enqueue_request(sc, req);
if (error != 0) {
IPMI_UNLOCK(sc);
return (error);
}
while (req->ir_status != IRS_COMPLETED && error >= 0)
if (timo == 0)
cv_wait(&req->ir_cv, &sc->ipmi_lock);
else
error = cv_timedwait(&req->ir_cv, &sc->ipmi_lock,
ddi_get_lbolt() + timo);
switch (req->ir_status) {
case IRS_QUEUED:
TAILQ_REMOVE(&sc->ipmi_pending_requests, req, ir_link);
req->ir_status = IRS_CANCELED;
error = EWOULDBLOCK;
break;
case IRS_PROCESSED:
req->ir_status = IRS_CANCELED;
error = EWOULDBLOCK;
*preq = NULL;
break;
case IRS_COMPLETED:
error = req->ir_error;
break;
default:
panic("IPMI: Invalid request status");
break;
}
IPMI_UNLOCK(sc);
return (error);
}
/*
* Helper routine for polled system interfaces that use
* ipmi_polled_enqueue_request() to queue requests. This request
* waits until there is a pending request and then returns the first
* request. If the driver is shutting down, it returns NULL.
*/
struct ipmi_request *
ipmi_dequeue_request(struct ipmi_softc *sc)
{
struct ipmi_request *req;
IPMI_LOCK_ASSERT(sc);
while (!sc->ipmi_detaching && TAILQ_EMPTY(&sc->ipmi_pending_requests))
cv_wait(&sc->ipmi_request_added, &sc->ipmi_lock);
if (sc->ipmi_detaching)
return (NULL);
req = TAILQ_FIRST(&sc->ipmi_pending_requests);
TAILQ_REMOVE(&sc->ipmi_pending_requests, req, ir_link);
req->ir_status = IRS_PROCESSED;
if (req->ir_owner != NULL)
req->ir_owner->ipmi_status |= IPMI_BUSY;
return (req);
}
int
ipmi_polled_enqueue_request(struct ipmi_softc *sc, struct ipmi_request *req)
{
IPMI_LOCK_ASSERT(sc);
TAILQ_INSERT_TAIL(&sc->ipmi_pending_requests, req, ir_link);
req->ir_status = IRS_QUEUED;
cv_signal(&sc->ipmi_request_added);
return (0);
}
void
ipmi_shutdown(struct ipmi_softc *sc)
{
taskq_destroy(sc->ipmi_kthread);
cv_destroy(&sc->ipmi_request_added);
mutex_destroy(&sc->ipmi_lock);
}
boolean_t
ipmi_startup(struct ipmi_softc *sc)
{
struct ipmi_request *req;
int error, i;
/* Initialize interface-independent state. */
mutex_init(&sc->ipmi_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&sc->ipmi_request_added, NULL, CV_DEFAULT, NULL);
TAILQ_INIT(&sc->ipmi_pending_requests);
/* Initialize interface-dependent state. */
error = sc->ipmi_startup(sc);
if (error) {
cmn_err(CE_WARN, "Failed to initialize interface: %d", error);
return (B_FALSE);
}
/* Send a GET_DEVICE_ID request. */
req = ipmi_alloc_driver_request(IPMI_ADDR(IPMI_APP_REQUEST, 0),
IPMI_GET_DEVICE_ID, 0, 15);
error = ipmi_submit_driver_request(sc, &req, MAX_TIMEOUT);
if (error == EWOULDBLOCK) {
cmn_err(CE_WARN, "Timed out waiting for GET_DEVICE_ID");
ipmi_free_request(req);
return (B_FALSE);
} else if (error) {
cmn_err(CE_WARN, "Failed GET_DEVICE_ID: %d", error);
ipmi_free_request(req);
return (B_FALSE);
} else if (req->ir_compcode != 0) {
cmn_err(CE_WARN,
"Bad completion code for GET_DEVICE_ID: %d",
req->ir_compcode);
ipmi_free_request(req);
return (B_FALSE);
} else if (req->ir_replylen < 5) {
cmn_err(CE_WARN, "Short reply for GET_DEVICE_ID: %d",
req->ir_replylen);
ipmi_free_request(req);
return (B_FALSE);
}
cmn_err(CE_CONT, "!device rev. %d, firmware rev. %d.%d%d, "
"version %d.%d",
req->ir_reply[1] & 0x0f, req->ir_reply[2] & 0x7f,
req->ir_reply[3] >> 4, req->ir_reply[3] & 0x0f,
req->ir_reply[4] & 0x0f, req->ir_reply[4] >> 4);
ipmi_free_request(req);
req = ipmi_alloc_driver_request(IPMI_ADDR(IPMI_APP_REQUEST, 0),
IPMI_CLEAR_FLAGS, 1, 0);
if ((error = ipmi_submit_driver_request(sc, &req, 0)) != 0) {
cmn_err(CE_WARN, "Failed to clear IPMI flags: %d\n", error);
ipmi_free_request(req);
return (B_FALSE);
}
/* Magic numbers */
if (req->ir_compcode == 0xc0) {
cmn_err(CE_NOTE, "!Clear flags is busy");
}
if (req->ir_compcode == 0xc1) {
cmn_err(CE_NOTE, "!Clear flags illegal");
}
ipmi_free_request(req);
for (i = 0; i < 8; i++) {
req = ipmi_alloc_driver_request(IPMI_ADDR(IPMI_APP_REQUEST, 0),
IPMI_GET_CHANNEL_INFO, 1, 0);
req->ir_request[0] = (uchar_t)i;
if (ipmi_submit_driver_request(sc, &req, 0) != 0) {
ipmi_free_request(req);
break;
}
if (req->ir_compcode != 0) {
ipmi_free_request(req);
break;
}
ipmi_free_request(req);
}
cmn_err(CE_CONT, "!number of channels %d", i);
/* probe for watchdog */
req = ipmi_alloc_driver_request(IPMI_ADDR(IPMI_APP_REQUEST, 0),
IPMI_GET_WDOG, 0, 0);
if ((error = ipmi_submit_driver_request(sc, &req, 0)) != 0) {
cmn_err(CE_WARN, "Failed to check IPMI watchdog: %d\n", error);
ipmi_free_request(req);
return (B_FALSE);
}
if (req->ir_compcode == 0x00) {
cmn_err(CE_CONT, "!watchdog supported");
/*
* Here is where we could register a watchdog event handler.
* See ipmi_wd_event() in the FreeBSD code.
*/
}
ipmi_free_request(req);
return (B_TRUE);
}
|