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
|
/* Copyright 2013-2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <skiboot.h>
#include <lpc.h>
#include <lock.h>
#include <device.h>
#include <time.h>
#include <ipmi.h>
#include <bt.h>
/* BT registers */
#define BT_CTRL 0
#define BT_CTRL_B_BUSY 0x00000080
#define BT_CTRL_H_BUSY 0x00000040
#define BT_CTRL_OEM0 0x00000020
#define BT_CTRL_SMS_ATN 0x00000010
#define BT_CTRL_B2H_ATN 0x00000008
#define BT_CTRL_H2B_ATN 0x00000004
#define BT_CTRL_CLR_RD_PTR 0x00000002
#define BT_CTRL_CLR_WR_PTR 0x00000001
#define BT_HOST2BMC 1
#define BT_INTMASK 2
#define BT_INTMASK_BMC_HWRST 0x00000001
/*
* Minimum size of an IPMI request/response including
* mandatory headers.
*/
#define BT_MIN_REQ_LEN 3
#define BT_MIN_RESP_LEN 4
/*
* How long (in uS) to poll for new ipmi data.
*/
#define POLL_TIMEOUT 10000
enum bt_states {
BT_STATE_IDLE = 0,
BT_STATE_RESP_WAIT,
BT_STATE_B_BUSY,
};
const char *state_str[] = {
"BT_STATE_IDLE",
"BT_STATE_RESP_WAIT",
"BT_STATE_B_BUSY",
};
struct bt_msg {
struct list_node link;
uint8_t seq;
uint8_t lun;
struct ipmi_msg ipmi_msg;
};
struct bt {
uint32_t base_addr;
enum bt_states state;
struct lock lock;
struct list_head msgq;
};
static struct bt bt;
static int ipmi_seq;
static inline uint8_t bt_inb(uint32_t reg)
{
return lpc_inb(bt.base_addr + reg);
}
static inline void bt_outb(uint8_t data, uint32_t reg)
{
lpc_outb(data, bt.base_addr + reg);
}
static inline bool bt_idle(void)
{
return !(bt_inb(BT_CTRL) & (BT_CTRL_B_BUSY | BT_CTRL_H2B_ATN));
}
static void bt_setmask(uint8_t mask, uint32_t reg)
{
uint8_t tmp;
tmp = bt_inb(reg);
bt_outb(tmp | mask, reg);
}
static void bt_clearmask(uint8_t mask, uint32_t reg)
{
uint8_t tmp;
tmp = bt_inb(reg);
bt_outb(tmp & ~mask, reg);
}
static inline void bt_set_state(enum bt_states next_state)
{
bt.state = next_state;
}
static int bt_add_ipmi_msg(struct ipmi_msg *ipmi_msg)
{
struct bt_msg *bt_msg = container_of(ipmi_msg, struct bt_msg, ipmi_msg);
bt_msg->lun = 0;
bt_msg->seq = ipmi_seq++;
list_add_tail(&bt.msgq, &bt_msg->link);
return 0;
}
static void bt_reset_interface(void)
{
bt_setmask(BT_INTMASK_BMC_HWRST, BT_INTMASK);
bt_set_state(BT_STATE_B_BUSY);
}
static bool bt_try_send_msg(void)
{
int i;
struct bt_msg *bt_msg;
struct ipmi_msg *ipmi_msg;
bt_msg = list_top(&bt.msgq, struct bt_msg, link);
if (!bt_msg)
return true;
ipmi_msg = &bt_msg->ipmi_msg;
if (!bt_idle()) {
prerror("BT: Interface in an unexpected state, attempting reset\n");
bt_reset_interface();
return false;
}
/* Send the message */
bt_setmask(BT_CTRL_CLR_WR_PTR, BT_CTRL);
/* Byte 1 - Length */
bt_outb(ipmi_msg->req_size + BT_MIN_REQ_LEN, BT_HOST2BMC);
/* Byte 2 - NetFn/LUN */
bt_outb((ipmi_msg->netfn << 2) | (bt_msg->lun & 0x3), BT_HOST2BMC);
/* Byte 3 - Seq */
bt_outb(bt_msg->seq, BT_HOST2BMC);
/* Byte 4 - Cmd */
bt_outb(ipmi_msg->cmd, BT_HOST2BMC);
/* Byte 5:N - Data */
for (i = 0; i < ipmi_msg->req_size; i++)
bt_outb(ipmi_msg->data[i], BT_HOST2BMC);
bt_setmask(BT_CTRL_H2B_ATN, BT_CTRL);
bt_set_state(BT_STATE_RESP_WAIT);
return true;
}
static void bt_flush_msg(void)
{
bt_setmask(BT_CTRL_H_BUSY, BT_CTRL);
bt_clearmask(BT_CTRL_B2H_ATN, BT_CTRL);
bt_setmask(BT_CTRL_CLR_RD_PTR, BT_CTRL);
bt_clearmask(BT_CTRL_H_BUSY, BT_CTRL);
}
static bool bt_get_resp(void)
{
int i;
struct bt_msg *bt_msg;
struct ipmi_msg *ipmi_msg;
uint8_t resp_len;
uint8_t netfn;
uint8_t cc = IPMI_CC_NO_ERROR;
/* Wait for BMC to signal response */
if (!(bt_inb(BT_CTRL) & BT_CTRL_B2H_ATN))
return true;
bt_msg = list_top(&bt.msgq, struct bt_msg, link);
if (!bt_msg) {
/* A response to a message we no longer care about. */
prlog(PR_INFO, "BT: Nobody cared about a response to an BT/IPMI message\n");
bt_flush_msg();
bt_set_state(BT_STATE_B_BUSY);
return false;
}
ipmi_msg = &bt_msg->ipmi_msg;
bt_setmask(BT_CTRL_H_BUSY, BT_CTRL);
bt_clearmask(BT_CTRL_B2H_ATN, BT_CTRL);
bt_setmask(BT_CTRL_CLR_RD_PTR, BT_CTRL);
/* Read the response */
/* Byte 1 - Length (includes header size) */
resp_len = bt_inb(BT_HOST2BMC) - BT_MIN_RESP_LEN;
/*
* Make sure we have enough room to store the resposne. As all values
* are unsigned we will also trigger this error if
* bt_inb(BT_HOST2BMC) < BT_MIN_RESP_LEN (which should never occur).
*/
if (resp_len > ipmi_msg->resp_size) {
prerror("BT: Invalid resp_len %d for ipmi_msg->cmd = 0x%02x\n", resp_len, ipmi_msg->cmd);
resp_len = ipmi_msg->resp_size;
cc = IPMI_ERR_MSG_TRUNCATED;
}
ipmi_msg->resp_size = resp_len;
/* Byte 2 - NetFn/LUN */
netfn = bt_inb(BT_HOST2BMC);
ipmi_msg->netfn = netfn >> 2;
bt_msg->lun = netfn & 0x3;
/* Byte 3 - Seq */
bt_msg->seq = bt_inb(BT_HOST2BMC);
/* Byte 4 - Cmd */
ipmi_msg->cmd = bt_inb(BT_HOST2BMC);
/* Byte 5 - Completion Code */
ipmi_msg->cc = bt_inb(BT_HOST2BMC);
/* Byte 6:N - Data */
for (i = 0; i < resp_len; i++)
ipmi_msg->data[i] = bt_inb(BT_HOST2BMC);
bt_clearmask(BT_CTRL_H_BUSY, BT_CTRL);
if (cc != IPMI_CC_NO_ERROR) {
prerror("BT: Host error 0x%02x receiving BT/IPMI response\n", cc);
ipmi_msg->cc = cc;
}
/* Make sure the other side is idle before we move to the idle state */
bt_set_state(BT_STATE_B_BUSY);
list_del(&bt_msg->link);
/*
* Call the IPMI layer to finish processing the message.
*/
ipmi_cmd_done(ipmi_msg);
/* Immediately send the next message */
return false;
}
static void bt_poll(void)
{
bool ret = true;
do {
switch(bt.state) {
case BT_STATE_IDLE:
ret = bt_try_send_msg();
break;
case BT_STATE_RESP_WAIT:
ret = bt_get_resp();
break;
case BT_STATE_B_BUSY:
if (bt_idle()) {
bt_set_state(BT_STATE_IDLE);
ret = false;
}
break;
}
}
while(!ret);
}
/*
* Crank the state machine to wait for a specific state. Returns true on
* success and false if there is a timeout.
*/
static bool bt_wait_state(enum bt_states state)
{
int timeout;
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000;
for (timeout = POLL_TIMEOUT; timeout > 0; timeout--) {
if (bt.state == state)
return true;
bt_poll();
nanosleep(&ts, NULL);
}
return false;
}
/*
* Allocate an ipmi message and bt container and return the ipmi
* message struct. Allocates enough space for the request and response
* data.
*/
static struct ipmi_msg *bt_alloc_ipmi_msg(size_t request_size, size_t response_size)
{
struct bt_msg *bt_msg;
bt_msg = zalloc(sizeof(struct bt_msg) + MAX(request_size, response_size));
if (!bt_msg)
return NULL;
bt_msg->ipmi_msg.req_size = request_size;
bt_msg->ipmi_msg.resp_size = response_size;
bt_msg->ipmi_msg.data = (uint8_t *) (bt_msg + 1);
return &bt_msg->ipmi_msg;
}
/*
* Free a previously allocated ipmi message.
*/
static void bt_free_ipmi_msg(struct ipmi_msg *ipmi_msg)
{
struct bt_msg *bt_msg = container_of(ipmi_msg, struct bt_msg, ipmi_msg);
free(bt_msg);
}
/*
* Add an ipmi message to the queue and wait for a response.
*/
static int bt_add_ipmi_msg_wait(struct ipmi_msg *msg)
{
int ret = 0;
/*
* TODO: We may need finer grained locks if we start using an
* asynchronous operation model, but this should be fine for the moment.
*/
lock(&bt.lock);
if (!bt_wait_state(BT_STATE_IDLE)) {
ret = -1;
goto out;
}
if (bt_add_ipmi_msg(msg)) {
ret = -1;
goto out;
}
/* Make sure we get out of the idle state */
bt_poll();
if (!bt_wait_state(BT_STATE_IDLE)) {
ret = -1;
goto out;
}
out:
unlock(&bt.lock);
return ret;
}
/*
* Remove a message from the queue. The memory allocated for the ipmi message
* will need to be freed by the caller with bt_free_ipmi_msg() as it will no
* longer be in the queue of messages.
*/
static int bt_del_ipmi_msg(struct ipmi_msg *ipmi_msg)
{
struct bt_msg *bt_msg = container_of(ipmi_msg, struct bt_msg, ipmi_msg);
lock(&bt.lock);
list_del(&bt_msg->link);
unlock(&bt.lock);
return 0;
}
struct ipmi_backend bt_backend = {
.alloc_msg = bt_alloc_ipmi_msg,
.free_msg = bt_free_ipmi_msg,
.queue_msg = bt_add_ipmi_msg_wait,
.dequeue_msg = bt_del_ipmi_msg,
};
void bt_init(void)
{
struct dt_node *n;
const struct dt_property *prop;
/* We support only one */
n = dt_find_compatible_node(dt_root, NULL, "ipmi-bt");
if (!n)
return;
/* Get IO base */
prop = dt_find_property(n, "reg");
if (!prop) {
prerror("BT: Can't find reg property\n");
return;
}
if (dt_property_get_cell(prop, 0) != OPAL_LPC_IO) {
prerror("BT: Only supports IO addresses\n");
return;
}
bt.base_addr = dt_property_get_cell(prop, 1);
bt_reset_interface();
init_lock(&bt.lock);
/*
* The iBT interface comes up in the busy state until the daemon has
* initialised it.
*/
bt_set_state(BT_STATE_B_BUSY);
list_head_init(&bt.msgq);
ipmi_register_backend(&bt_backend);
}
|