1
2
3
4
5
6
7
8
9
10#include <linux/module.h>
11#include <linux/acpi.h>
12#include <linux/ipmi.h>
13#include <linux/spinlock.h>
14
15MODULE_AUTHOR("Zhao Yakui");
16MODULE_DESCRIPTION("ACPI IPMI Opregion driver");
17MODULE_LICENSE("GPL");
18
19#define ACPI_IPMI_OK 0
20#define ACPI_IPMI_TIMEOUT 0x10
21#define ACPI_IPMI_UNKNOWN 0x07
22
23#define IPMI_TIMEOUT (5000)
24#define ACPI_IPMI_MAX_MSG_LENGTH 64
25
26struct acpi_ipmi_device {
27
28 struct list_head head;
29
30
31 struct list_head tx_msg_list;
32
33 spinlock_t tx_msg_lock;
34 acpi_handle handle;
35 struct device *dev;
36 struct ipmi_user *user_interface;
37 int ipmi_ifnum;
38 long curr_msgid;
39 bool dead;
40 struct kref kref;
41};
42
43struct ipmi_driver_data {
44 struct list_head ipmi_devices;
45 struct ipmi_smi_watcher bmc_events;
46 const struct ipmi_user_hndl ipmi_hndlrs;
47 struct mutex ipmi_lock;
48
49
50
51
52
53
54
55
56 struct acpi_ipmi_device *selected_smi;
57};
58
59struct acpi_ipmi_msg {
60 struct list_head head;
61
62
63
64
65
66
67
68
69 struct ipmi_addr addr;
70 long tx_msgid;
71
72
73 struct completion tx_complete;
74
75 struct kernel_ipmi_msg tx_message;
76 int msg_done;
77
78
79 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
80 u8 rx_len;
81
82 struct acpi_ipmi_device *device;
83 struct kref kref;
84};
85
86
87struct acpi_ipmi_buffer {
88 u8 status;
89 u8 length;
90 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
91};
92
93static void ipmi_register_bmc(int iface, struct device *dev);
94static void ipmi_bmc_gone(int iface);
95static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
96
97static struct ipmi_driver_data driver_data = {
98 .ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
99 .bmc_events = {
100 .owner = THIS_MODULE,
101 .new_smi = ipmi_register_bmc,
102 .smi_gone = ipmi_bmc_gone,
103 },
104 .ipmi_hndlrs = {
105 .ipmi_recv_hndl = ipmi_msg_handler,
106 },
107 .ipmi_lock = __MUTEX_INITIALIZER(driver_data.ipmi_lock)
108};
109
110static struct acpi_ipmi_device *
111ipmi_dev_alloc(int iface, struct device *dev, acpi_handle handle)
112{
113 struct acpi_ipmi_device *ipmi_device;
114 int err;
115 struct ipmi_user *user;
116
117 ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
118 if (!ipmi_device)
119 return NULL;
120
121 kref_init(&ipmi_device->kref);
122 INIT_LIST_HEAD(&ipmi_device->head);
123 INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
124 spin_lock_init(&ipmi_device->tx_msg_lock);
125 ipmi_device->handle = handle;
126 ipmi_device->dev = get_device(dev);
127 ipmi_device->ipmi_ifnum = iface;
128
129 err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
130 ipmi_device, &user);
131 if (err) {
132 put_device(dev);
133 kfree(ipmi_device);
134 return NULL;
135 }
136 ipmi_device->user_interface = user;
137
138 return ipmi_device;
139}
140
141static void ipmi_dev_release(struct acpi_ipmi_device *ipmi_device)
142{
143 ipmi_destroy_user(ipmi_device->user_interface);
144 put_device(ipmi_device->dev);
145 kfree(ipmi_device);
146}
147
148static void ipmi_dev_release_kref(struct kref *kref)
149{
150 struct acpi_ipmi_device *ipmi =
151 container_of(kref, struct acpi_ipmi_device, kref);
152
153 ipmi_dev_release(ipmi);
154}
155
156static void __ipmi_dev_kill(struct acpi_ipmi_device *ipmi_device)
157{
158 list_del(&ipmi_device->head);
159 if (driver_data.selected_smi == ipmi_device)
160 driver_data.selected_smi = NULL;
161
162
163
164
165
166 ipmi_device->dead = true;
167}
168
169static struct acpi_ipmi_device *acpi_ipmi_dev_get(void)
170{
171 struct acpi_ipmi_device *ipmi_device = NULL;
172
173 mutex_lock(&driver_data.ipmi_lock);
174 if (driver_data.selected_smi) {
175 ipmi_device = driver_data.selected_smi;
176 kref_get(&ipmi_device->kref);
177 }
178 mutex_unlock(&driver_data.ipmi_lock);
179
180 return ipmi_device;
181}
182
183static void acpi_ipmi_dev_put(struct acpi_ipmi_device *ipmi_device)
184{
185 kref_put(&ipmi_device->kref, ipmi_dev_release_kref);
186}
187
188static struct acpi_ipmi_msg *ipmi_msg_alloc(void)
189{
190 struct acpi_ipmi_device *ipmi;
191 struct acpi_ipmi_msg *ipmi_msg;
192
193 ipmi = acpi_ipmi_dev_get();
194 if (!ipmi)
195 return NULL;
196
197 ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
198 if (!ipmi_msg) {
199 acpi_ipmi_dev_put(ipmi);
200 return NULL;
201 }
202
203 kref_init(&ipmi_msg->kref);
204 init_completion(&ipmi_msg->tx_complete);
205 INIT_LIST_HEAD(&ipmi_msg->head);
206 ipmi_msg->device = ipmi;
207 ipmi_msg->msg_done = ACPI_IPMI_UNKNOWN;
208
209 return ipmi_msg;
210}
211
212static void ipmi_msg_release(struct acpi_ipmi_msg *tx_msg)
213{
214 acpi_ipmi_dev_put(tx_msg->device);
215 kfree(tx_msg);
216}
217
218static void ipmi_msg_release_kref(struct kref *kref)
219{
220 struct acpi_ipmi_msg *tx_msg =
221 container_of(kref, struct acpi_ipmi_msg, kref);
222
223 ipmi_msg_release(tx_msg);
224}
225
226static struct acpi_ipmi_msg *acpi_ipmi_msg_get(struct acpi_ipmi_msg *tx_msg)
227{
228 kref_get(&tx_msg->kref);
229
230 return tx_msg;
231}
232
233static void acpi_ipmi_msg_put(struct acpi_ipmi_msg *tx_msg)
234{
235 kref_put(&tx_msg->kref, ipmi_msg_release_kref);
236}
237
238#define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
239#define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
240static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
241 acpi_physical_address address,
242 acpi_integer *value)
243{
244 struct kernel_ipmi_msg *msg;
245 struct acpi_ipmi_buffer *buffer;
246 struct acpi_ipmi_device *device;
247 unsigned long flags;
248
249 msg = &tx_msg->tx_message;
250
251
252
253
254
255 msg->netfn = IPMI_OP_RGN_NETFN(address);
256 msg->cmd = IPMI_OP_RGN_CMD(address);
257 msg->data = tx_msg->data;
258
259
260
261
262
263 buffer = (struct acpi_ipmi_buffer *)value;
264
265
266 if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
267 dev_WARN_ONCE(tx_msg->device->dev, true,
268 "Unexpected request (msg len %d).\n",
269 buffer->length);
270 return -EINVAL;
271 }
272 msg->data_len = buffer->length;
273 memcpy(tx_msg->data, buffer->data, msg->data_len);
274
275
276
277
278
279
280
281
282 tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
283 tx_msg->addr.channel = IPMI_BMC_CHANNEL;
284 tx_msg->addr.data[0] = 0;
285
286
287 device = tx_msg->device;
288
289 spin_lock_irqsave(&device->tx_msg_lock, flags);
290 device->curr_msgid++;
291 tx_msg->tx_msgid = device->curr_msgid;
292 spin_unlock_irqrestore(&device->tx_msg_lock, flags);
293
294 return 0;
295}
296
297static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
298 acpi_integer *value)
299{
300 struct acpi_ipmi_buffer *buffer;
301
302
303
304
305
306 buffer = (struct acpi_ipmi_buffer *)value;
307
308
309
310
311
312 buffer->status = msg->msg_done;
313 if (msg->msg_done != ACPI_IPMI_OK)
314 return;
315
316
317
318
319
320 buffer->length = msg->rx_len;
321 memcpy(buffer->data, msg->data, msg->rx_len);
322}
323
324static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
325{
326 struct acpi_ipmi_msg *tx_msg;
327 unsigned long flags;
328
329
330
331
332
333
334
335
336
337 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
338 while (!list_empty(&ipmi->tx_msg_list)) {
339 tx_msg = list_first_entry(&ipmi->tx_msg_list,
340 struct acpi_ipmi_msg,
341 head);
342 list_del(&tx_msg->head);
343 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
344
345
346 complete(&tx_msg->tx_complete);
347 acpi_ipmi_msg_put(tx_msg);
348 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
349 }
350 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
351}
352
353static void ipmi_cancel_tx_msg(struct acpi_ipmi_device *ipmi,
354 struct acpi_ipmi_msg *msg)
355{
356 struct acpi_ipmi_msg *tx_msg, *temp;
357 bool msg_found = false;
358 unsigned long flags;
359
360 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
361 list_for_each_entry_safe(tx_msg, temp, &ipmi->tx_msg_list, head) {
362 if (msg == tx_msg) {
363 msg_found = true;
364 list_del(&tx_msg->head);
365 break;
366 }
367 }
368 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
369
370 if (msg_found)
371 acpi_ipmi_msg_put(tx_msg);
372}
373
374static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
375{
376 struct acpi_ipmi_device *ipmi_device = user_msg_data;
377 bool msg_found = false;
378 struct acpi_ipmi_msg *tx_msg, *temp;
379 struct device *dev = ipmi_device->dev;
380 unsigned long flags;
381
382 if (msg->user != ipmi_device->user_interface) {
383 dev_warn(dev,
384 "Unexpected response is returned. returned user %p, expected user %p\n",
385 msg->user, ipmi_device->user_interface);
386 goto out_msg;
387 }
388
389 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
390 list_for_each_entry_safe(tx_msg, temp, &ipmi_device->tx_msg_list, head) {
391 if (msg->msgid == tx_msg->tx_msgid) {
392 msg_found = true;
393 list_del(&tx_msg->head);
394 break;
395 }
396 }
397 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
398
399 if (!msg_found) {
400 dev_warn(dev,
401 "Unexpected response (msg id %ld) is returned.\n",
402 msg->msgid);
403 goto out_msg;
404 }
405
406
407 if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
408 dev_WARN_ONCE(dev, true,
409 "Unexpected response (msg len %d).\n",
410 msg->msg.data_len);
411 goto out_comp;
412 }
413
414
415 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
416 if (msg->recv_type == IPMI_RESPONSE_RECV_TYPE &&
417 msg->msg.data_len == 1) {
418 if (msg->msg.data[0] == IPMI_TIMEOUT_COMPLETION_CODE) {
419 dev_dbg_once(dev, "Unexpected response (timeout).\n");
420 tx_msg->msg_done = ACPI_IPMI_TIMEOUT;
421 }
422 goto out_comp;
423 }
424
425 tx_msg->rx_len = msg->msg.data_len;
426 memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
427 tx_msg->msg_done = ACPI_IPMI_OK;
428
429out_comp:
430 complete(&tx_msg->tx_complete);
431 acpi_ipmi_msg_put(tx_msg);
432out_msg:
433 ipmi_free_recv_msg(msg);
434}
435
436static void ipmi_register_bmc(int iface, struct device *dev)
437{
438 struct acpi_ipmi_device *ipmi_device, *temp;
439 int err;
440 struct ipmi_smi_info smi_data;
441 acpi_handle handle;
442
443 err = ipmi_get_smi_info(iface, &smi_data);
444 if (err)
445 return;
446
447 if (smi_data.addr_src != SI_ACPI)
448 goto err_ref;
449 handle = smi_data.addr_info.acpi_info.acpi_handle;
450 if (!handle)
451 goto err_ref;
452
453 ipmi_device = ipmi_dev_alloc(iface, smi_data.dev, handle);
454 if (!ipmi_device) {
455 dev_warn(smi_data.dev, "Can't create IPMI user interface\n");
456 goto err_ref;
457 }
458
459 mutex_lock(&driver_data.ipmi_lock);
460 list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
461
462
463
464
465 if (temp->handle == handle)
466 goto err_lock;
467 }
468 if (!driver_data.selected_smi)
469 driver_data.selected_smi = ipmi_device;
470 list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
471 mutex_unlock(&driver_data.ipmi_lock);
472
473 put_device(smi_data.dev);
474 return;
475
476err_lock:
477 mutex_unlock(&driver_data.ipmi_lock);
478 ipmi_dev_release(ipmi_device);
479err_ref:
480 put_device(smi_data.dev);
481 return;
482}
483
484static void ipmi_bmc_gone(int iface)
485{
486 struct acpi_ipmi_device *ipmi_device, *temp;
487 bool dev_found = false;
488
489 mutex_lock(&driver_data.ipmi_lock);
490 list_for_each_entry_safe(ipmi_device, temp,
491 &driver_data.ipmi_devices, head) {
492 if (ipmi_device->ipmi_ifnum != iface) {
493 dev_found = true;
494 __ipmi_dev_kill(ipmi_device);
495 break;
496 }
497 }
498 if (!driver_data.selected_smi)
499 driver_data.selected_smi = list_first_entry_or_null(
500 &driver_data.ipmi_devices,
501 struct acpi_ipmi_device, head);
502 mutex_unlock(&driver_data.ipmi_lock);
503
504 if (dev_found) {
505 ipmi_flush_tx_msg(ipmi_device);
506 acpi_ipmi_dev_put(ipmi_device);
507 }
508}
509
510
511
512
513
514
515
516
517
518
519
520
521
522static acpi_status
523acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
524 u32 bits, acpi_integer *value,
525 void *handler_context, void *region_context)
526{
527 struct acpi_ipmi_msg *tx_msg;
528 struct acpi_ipmi_device *ipmi_device;
529 int err;
530 acpi_status status;
531 unsigned long flags;
532
533
534
535
536
537
538
539 if ((function & ACPI_IO_MASK) == ACPI_READ)
540 return AE_TYPE;
541
542 tx_msg = ipmi_msg_alloc();
543 if (!tx_msg)
544 return AE_NOT_EXIST;
545 ipmi_device = tx_msg->device;
546
547 if (acpi_format_ipmi_request(tx_msg, address, value) != 0) {
548 ipmi_msg_release(tx_msg);
549 return AE_TYPE;
550 }
551
552 acpi_ipmi_msg_get(tx_msg);
553 mutex_lock(&driver_data.ipmi_lock);
554
555 if (ipmi_device->dead) {
556 mutex_unlock(&driver_data.ipmi_lock);
557 ipmi_msg_release(tx_msg);
558 return AE_NOT_EXIST;
559 }
560 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
561 list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
562 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
563 mutex_unlock(&driver_data.ipmi_lock);
564
565 err = ipmi_request_settime(ipmi_device->user_interface,
566 &tx_msg->addr,
567 tx_msg->tx_msgid,
568 &tx_msg->tx_message,
569 NULL, 0, 0, IPMI_TIMEOUT);
570 if (err) {
571 status = AE_ERROR;
572 goto out_msg;
573 }
574 wait_for_completion(&tx_msg->tx_complete);
575
576 acpi_format_ipmi_response(tx_msg, value);
577 status = AE_OK;
578
579out_msg:
580 ipmi_cancel_tx_msg(ipmi_device, tx_msg);
581 acpi_ipmi_msg_put(tx_msg);
582 return status;
583}
584
585static int __init acpi_ipmi_init(void)
586{
587 int result;
588 acpi_status status;
589
590 if (acpi_disabled)
591 return 0;
592
593 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
594 ACPI_ADR_SPACE_IPMI,
595 &acpi_ipmi_space_handler,
596 NULL, NULL);
597 if (ACPI_FAILURE(status)) {
598 pr_warn("Can't register IPMI opregion space handle\n");
599 return -EINVAL;
600 }
601 result = ipmi_smi_watcher_register(&driver_data.bmc_events);
602 if (result)
603 pr_err("Can't register IPMI system interface watcher\n");
604
605 return result;
606}
607
608static void __exit acpi_ipmi_exit(void)
609{
610 struct acpi_ipmi_device *ipmi_device;
611
612 if (acpi_disabled)
613 return;
614
615 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
616
617
618
619
620
621
622
623 mutex_lock(&driver_data.ipmi_lock);
624 while (!list_empty(&driver_data.ipmi_devices)) {
625 ipmi_device = list_first_entry(&driver_data.ipmi_devices,
626 struct acpi_ipmi_device,
627 head);
628 __ipmi_dev_kill(ipmi_device);
629 mutex_unlock(&driver_data.ipmi_lock);
630
631 ipmi_flush_tx_msg(ipmi_device);
632 acpi_ipmi_dev_put(ipmi_device);
633
634 mutex_lock(&driver_data.ipmi_lock);
635 }
636 mutex_unlock(&driver_data.ipmi_lock);
637 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
638 ACPI_ADR_SPACE_IPMI,
639 &acpi_ipmi_space_handler);
640}
641
642module_init(acpi_ipmi_init);
643module_exit(acpi_ipmi_exit);
644