1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/platform_device.h>
16#include <linux/acpi.h>
17#include <linux/dma-mapping.h>
18#include <linux/dmi.h>
19#include <linux/errno.h>
20#include <linux/cpu.h>
21#include <linux/gfp.h>
22#include <linux/init.h>
23#include <linux/io.h>
24#include <linux/kernel.h>
25#include <linux/mc146818rtc.h>
26#include <linux/module.h>
27#include <linux/reboot.h>
28#include <linux/sched.h>
29#include <linux/smp.h>
30#include <linux/spinlock.h>
31#include <linux/string.h>
32#include <linux/types.h>
33#include <linux/mutex.h>
34
35#include "dcdbas.h"
36
37#define DRIVER_NAME "dcdbas"
38#define DRIVER_VERSION "5.6.0-3.4"
39#define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
40
41static struct platform_device *dcdbas_pdev;
42
43static u8 *smi_data_buf;
44static dma_addr_t smi_data_buf_handle;
45static unsigned long smi_data_buf_size;
46static unsigned long max_smi_data_buf_size = MAX_SMI_DATA_BUF_SIZE;
47static u32 smi_data_buf_phys_addr;
48static DEFINE_MUTEX(smi_data_lock);
49static u8 *bios_buffer;
50
51static unsigned int host_control_action;
52static unsigned int host_control_smi_type;
53static unsigned int host_control_on_shutdown;
54
55static bool wsmt_enabled;
56
57
58
59
60static void smi_data_buf_free(void)
61{
62 if (!smi_data_buf || wsmt_enabled)
63 return;
64
65 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
66 __func__, smi_data_buf_phys_addr, smi_data_buf_size);
67
68 dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf,
69 smi_data_buf_handle);
70 smi_data_buf = NULL;
71 smi_data_buf_handle = 0;
72 smi_data_buf_phys_addr = 0;
73 smi_data_buf_size = 0;
74}
75
76
77
78
79static int smi_data_buf_realloc(unsigned long size)
80{
81 void *buf;
82 dma_addr_t handle;
83
84 if (smi_data_buf_size >= size)
85 return 0;
86
87 if (size > max_smi_data_buf_size)
88 return -EINVAL;
89
90
91 buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL);
92 if (!buf) {
93 dev_dbg(&dcdbas_pdev->dev,
94 "%s: failed to allocate memory size %lu\n",
95 __func__, size);
96 return -ENOMEM;
97 }
98
99
100 if (smi_data_buf)
101 memcpy(buf, smi_data_buf, smi_data_buf_size);
102
103
104 smi_data_buf_free();
105
106
107 smi_data_buf = buf;
108 smi_data_buf_handle = handle;
109 smi_data_buf_phys_addr = (u32) virt_to_phys(buf);
110 smi_data_buf_size = size;
111
112 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
113 __func__, smi_data_buf_phys_addr, smi_data_buf_size);
114
115 return 0;
116}
117
118static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
119 struct device_attribute *attr,
120 char *buf)
121{
122 return sprintf(buf, "%x\n", smi_data_buf_phys_addr);
123}
124
125static ssize_t smi_data_buf_size_show(struct device *dev,
126 struct device_attribute *attr,
127 char *buf)
128{
129 return sprintf(buf, "%lu\n", smi_data_buf_size);
130}
131
132static ssize_t smi_data_buf_size_store(struct device *dev,
133 struct device_attribute *attr,
134 const char *buf, size_t count)
135{
136 unsigned long buf_size;
137 ssize_t ret;
138
139 buf_size = simple_strtoul(buf, NULL, 10);
140
141
142 mutex_lock(&smi_data_lock);
143 ret = smi_data_buf_realloc(buf_size);
144 mutex_unlock(&smi_data_lock);
145 if (ret)
146 return ret;
147
148 return count;
149}
150
151static ssize_t smi_data_read(struct file *filp, struct kobject *kobj,
152 struct bin_attribute *bin_attr,
153 char *buf, loff_t pos, size_t count)
154{
155 ssize_t ret;
156
157 mutex_lock(&smi_data_lock);
158 ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf,
159 smi_data_buf_size);
160 mutex_unlock(&smi_data_lock);
161 return ret;
162}
163
164static ssize_t smi_data_write(struct file *filp, struct kobject *kobj,
165 struct bin_attribute *bin_attr,
166 char *buf, loff_t pos, size_t count)
167{
168 ssize_t ret;
169
170 if ((pos + count) > max_smi_data_buf_size)
171 return -EINVAL;
172
173 mutex_lock(&smi_data_lock);
174
175 ret = smi_data_buf_realloc(pos + count);
176 if (ret)
177 goto out;
178
179 memcpy(smi_data_buf + pos, buf, count);
180 ret = count;
181out:
182 mutex_unlock(&smi_data_lock);
183 return ret;
184}
185
186static ssize_t host_control_action_show(struct device *dev,
187 struct device_attribute *attr,
188 char *buf)
189{
190 return sprintf(buf, "%u\n", host_control_action);
191}
192
193static ssize_t host_control_action_store(struct device *dev,
194 struct device_attribute *attr,
195 const char *buf, size_t count)
196{
197 ssize_t ret;
198
199
200 mutex_lock(&smi_data_lock);
201 ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
202 mutex_unlock(&smi_data_lock);
203 if (ret)
204 return ret;
205
206 host_control_action = simple_strtoul(buf, NULL, 10);
207 return count;
208}
209
210static ssize_t host_control_smi_type_show(struct device *dev,
211 struct device_attribute *attr,
212 char *buf)
213{
214 return sprintf(buf, "%u\n", host_control_smi_type);
215}
216
217static ssize_t host_control_smi_type_store(struct device *dev,
218 struct device_attribute *attr,
219 const char *buf, size_t count)
220{
221 host_control_smi_type = simple_strtoul(buf, NULL, 10);
222 return count;
223}
224
225static ssize_t host_control_on_shutdown_show(struct device *dev,
226 struct device_attribute *attr,
227 char *buf)
228{
229 return sprintf(buf, "%u\n", host_control_on_shutdown);
230}
231
232static ssize_t host_control_on_shutdown_store(struct device *dev,
233 struct device_attribute *attr,
234 const char *buf, size_t count)
235{
236 host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
237 return count;
238}
239
240static int raise_smi(void *par)
241{
242 struct smi_cmd *smi_cmd = par;
243
244 if (smp_processor_id() != 0) {
245 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
246 __func__);
247 return -EBUSY;
248 }
249
250
251
252 asm volatile (
253 "outb %b0,%w1\n"
254 "inb %w1"
255 :
256 : "a" (smi_cmd->command_code),
257 "d" (smi_cmd->command_address),
258 "b" (smi_cmd->ebx),
259 "c" (smi_cmd->ecx)
260 : "memory"
261 );
262
263 return 0;
264}
265
266
267
268
269
270int dcdbas_smi_request(struct smi_cmd *smi_cmd)
271{
272 int ret;
273
274 if (smi_cmd->magic != SMI_CMD_MAGIC) {
275 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
276 __func__);
277 return -EBADR;
278 }
279
280
281 get_online_cpus();
282 ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true);
283 put_online_cpus();
284
285 return ret;
286}
287
288
289
290
291
292
293
294
295
296
297
298
299static ssize_t smi_request_store(struct device *dev,
300 struct device_attribute *attr,
301 const char *buf, size_t count)
302{
303 struct smi_cmd *smi_cmd;
304 unsigned long val = simple_strtoul(buf, NULL, 10);
305 ssize_t ret;
306
307 mutex_lock(&smi_data_lock);
308
309 if (smi_data_buf_size < sizeof(struct smi_cmd)) {
310 ret = -ENODEV;
311 goto out;
312 }
313 smi_cmd = (struct smi_cmd *)smi_data_buf;
314
315 switch (val) {
316 case 2:
317
318 ret = dcdbas_smi_request(smi_cmd);
319 if (!ret)
320 ret = count;
321 break;
322 case 1:
323
324
325
326
327
328
329
330
331
332
333
334
335 smi_cmd->ebx = smi_data_buf_phys_addr +
336 offsetof(struct smi_cmd, command_buffer);
337 ret = dcdbas_smi_request(smi_cmd);
338 if (!ret)
339 ret = count;
340 break;
341 case 0:
342 memset(smi_data_buf, 0, smi_data_buf_size);
343 ret = count;
344 break;
345 default:
346 ret = -EINVAL;
347 break;
348 }
349
350out:
351 mutex_unlock(&smi_data_lock);
352 return ret;
353}
354EXPORT_SYMBOL(dcdbas_smi_request);
355
356
357
358
359
360
361static int host_control_smi(void)
362{
363 struct apm_cmd *apm_cmd;
364 u8 *data;
365 unsigned long flags;
366 u32 num_ticks;
367 s8 cmd_status;
368 u8 index;
369
370 apm_cmd = (struct apm_cmd *)smi_data_buf;
371 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
372
373 switch (host_control_smi_type) {
374 case HC_SMITYPE_TYPE1:
375 spin_lock_irqsave(&rtc_lock, flags);
376
377 data = (u8 *)&smi_data_buf_phys_addr;
378 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
379 index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
380 index++, data++) {
381 outb(index,
382 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
383 outb(*data,
384 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
385 }
386
387
388 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
389 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
390
391
392 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
393 spin_unlock_irqrestore(&rtc_lock, flags);
394
395
396 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
397 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
398 == ESM_STATUS_CMD_UNSUCCESSFUL) {
399 num_ticks--;
400 if (num_ticks == EXPIRED_TIMER)
401 return -ETIME;
402 }
403 break;
404
405 case HC_SMITYPE_TYPE2:
406 case HC_SMITYPE_TYPE3:
407 spin_lock_irqsave(&rtc_lock, flags);
408
409 data = (u8 *)&smi_data_buf_phys_addr;
410 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
411 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
412 index++, data++) {
413 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
414 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
415 }
416
417
418 if (host_control_smi_type == HC_SMITYPE_TYPE3)
419 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
420 else
421 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
422
423
424 CMOS_READ(RTC_REG_C);
425 spin_unlock_irqrestore(&rtc_lock, flags);
426
427
428 cmd_status = inb(PE1400_APM_CONTROL_PORT);
429
430
431 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
432 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
433 num_ticks--;
434 if (num_ticks == EXPIRED_TIMER)
435 return -ETIME;
436 }
437 break;
438
439 default:
440 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
441 __func__, host_control_smi_type);
442 return -ENOSYS;
443 }
444
445 return 0;
446}
447
448
449
450
451
452
453
454
455
456
457static void dcdbas_host_control(void)
458{
459 struct apm_cmd *apm_cmd;
460 u8 action;
461
462 if (host_control_action == HC_ACTION_NONE)
463 return;
464
465 action = host_control_action;
466 host_control_action = HC_ACTION_NONE;
467
468 if (!smi_data_buf) {
469 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
470 return;
471 }
472
473 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
474 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
475 __func__);
476 return;
477 }
478
479 apm_cmd = (struct apm_cmd *)smi_data_buf;
480
481
482 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
483 apm_cmd->command = ESM_APM_POWER_CYCLE;
484 apm_cmd->reserved = 0;
485 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
486 host_control_smi();
487 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
488 apm_cmd->command = ESM_APM_POWER_CYCLE;
489 apm_cmd->reserved = 0;
490 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
491 host_control_smi();
492 }
493}
494
495
496
497static u8 checksum(u8 *buffer, u8 length)
498{
499 u8 sum = 0;
500 u8 *end = buffer + length;
501
502 while (buffer < end)
503 sum += *buffer++;
504 return sum;
505}
506
507static inline struct smm_eps_table *check_eps_table(u8 *addr)
508{
509 struct smm_eps_table *eps = (struct smm_eps_table *)addr;
510
511 if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0)
512 return NULL;
513
514 if (checksum(addr, eps->length) != 0)
515 return NULL;
516
517 return eps;
518}
519
520static int dcdbas_check_wsmt(void)
521{
522 const struct dmi_device *dev = NULL;
523 struct acpi_table_wsmt *wsmt = NULL;
524 struct smm_eps_table *eps = NULL;
525 u64 bios_buf_paddr;
526 u64 remap_size;
527 u8 *addr;
528
529 acpi_get_table(ACPI_SIG_WSMT, 0, (struct acpi_table_header **)&wsmt);
530 if (!wsmt)
531 return 0;
532
533
534 if (!(wsmt->protection_flags & ACPI_WSMT_FIXED_COMM_BUFFERS) ||
535 !(wsmt->protection_flags & ACPI_WSMT_COMM_BUFFER_NESTED_PTR_PROTECTION))
536 return 0;
537
538
539
540
541
542
543
544 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev)))
545 if (sscanf(dev->name, "30[%16llx;%8llx]", &bios_buf_paddr,
546 &remap_size) == 2)
547 goto remap;
548
549
550 for (addr = (u8 *)__va(0xf0000);
551 addr < (u8 *)__va(0x100000 - sizeof(struct smm_eps_table));
552 addr += 16) {
553 eps = check_eps_table(addr);
554 if (eps)
555 break;
556 }
557
558 if (!eps) {
559 dev_dbg(&dcdbas_pdev->dev, "found WSMT, but no firmware buffer found\n");
560 return -ENODEV;
561 }
562 bios_buf_paddr = eps->smm_comm_buff_addr;
563 remap_size = eps->num_of_4k_pages * PAGE_SIZE;
564
565remap:
566
567
568
569
570 if (upper_32_bits(bios_buf_paddr + 8)) {
571 dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer address is above 4GB\n");
572 return -EINVAL;
573 }
574
575
576
577
578 if (remap_size > MAX_SMI_DATA_BUF_SIZE + 8)
579 remap_size = MAX_SMI_DATA_BUF_SIZE + 8;
580
581 bios_buffer = memremap(bios_buf_paddr, remap_size, MEMREMAP_WB);
582 if (!bios_buffer) {
583 dev_warn(&dcdbas_pdev->dev, "found WSMT, but failed to map buffer\n");
584 return -ENOMEM;
585 }
586
587
588 smi_data_buf_phys_addr = bios_buf_paddr + 8;
589 smi_data_buf = bios_buffer + 8;
590 smi_data_buf_size = remap_size - 8;
591 max_smi_data_buf_size = smi_data_buf_size;
592 wsmt_enabled = true;
593 dev_info(&dcdbas_pdev->dev,
594 "WSMT found, using firmware-provided SMI buffer.\n");
595 return 1;
596}
597
598
599
600
601static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
602 void *unused)
603{
604 switch (code) {
605 case SYS_DOWN:
606 case SYS_HALT:
607 case SYS_POWER_OFF:
608 if (host_control_on_shutdown) {
609
610 printk(KERN_WARNING "Please wait for shutdown "
611 "action to complete...\n");
612 dcdbas_host_control();
613 }
614 break;
615 }
616
617 return NOTIFY_DONE;
618}
619
620static struct notifier_block dcdbas_reboot_nb = {
621 .notifier_call = dcdbas_reboot_notify,
622 .next = NULL,
623 .priority = INT_MIN
624};
625
626static DCDBAS_BIN_ATTR_RW(smi_data);
627
628static struct bin_attribute *dcdbas_bin_attrs[] = {
629 &bin_attr_smi_data,
630 NULL
631};
632
633static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
634static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
635static DCDBAS_DEV_ATTR_WO(smi_request);
636static DCDBAS_DEV_ATTR_RW(host_control_action);
637static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
638static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
639
640static struct attribute *dcdbas_dev_attrs[] = {
641 &dev_attr_smi_data_buf_size.attr,
642 &dev_attr_smi_data_buf_phys_addr.attr,
643 &dev_attr_smi_request.attr,
644 &dev_attr_host_control_action.attr,
645 &dev_attr_host_control_smi_type.attr,
646 &dev_attr_host_control_on_shutdown.attr,
647 NULL
648};
649
650static const struct attribute_group dcdbas_attr_group = {
651 .attrs = dcdbas_dev_attrs,
652 .bin_attrs = dcdbas_bin_attrs,
653};
654
655static int dcdbas_probe(struct platform_device *dev)
656{
657 int error;
658
659 host_control_action = HC_ACTION_NONE;
660 host_control_smi_type = HC_SMITYPE_NONE;
661
662 dcdbas_pdev = dev;
663
664
665 error = dcdbas_check_wsmt();
666 if (error < 0)
667 return error;
668
669
670
671
672
673 error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
674 if (error)
675 return error;
676
677 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
678 if (error)
679 return error;
680
681 register_reboot_notifier(&dcdbas_reboot_nb);
682
683 dev_info(&dev->dev, "%s (version %s)\n",
684 DRIVER_DESCRIPTION, DRIVER_VERSION);
685
686 return 0;
687}
688
689static int dcdbas_remove(struct platform_device *dev)
690{
691 unregister_reboot_notifier(&dcdbas_reboot_nb);
692 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
693
694 return 0;
695}
696
697static struct platform_driver dcdbas_driver = {
698 .driver = {
699 .name = DRIVER_NAME,
700 },
701 .probe = dcdbas_probe,
702 .remove = dcdbas_remove,
703};
704
705static const struct platform_device_info dcdbas_dev_info __initconst = {
706 .name = DRIVER_NAME,
707 .id = -1,
708 .dma_mask = DMA_BIT_MASK(32),
709};
710
711static struct platform_device *dcdbas_pdev_reg;
712
713
714
715
716static int __init dcdbas_init(void)
717{
718 int error;
719
720 error = platform_driver_register(&dcdbas_driver);
721 if (error)
722 return error;
723
724 dcdbas_pdev_reg = platform_device_register_full(&dcdbas_dev_info);
725 if (IS_ERR(dcdbas_pdev_reg)) {
726 error = PTR_ERR(dcdbas_pdev_reg);
727 goto err_unregister_driver;
728 }
729
730 return 0;
731
732 err_unregister_driver:
733 platform_driver_unregister(&dcdbas_driver);
734 return error;
735}
736
737
738
739
740static void __exit dcdbas_exit(void)
741{
742
743
744
745
746 unregister_reboot_notifier(&dcdbas_reboot_nb);
747
748
749
750
751
752
753
754 if (dcdbas_pdev)
755 smi_data_buf_free();
756 if (bios_buffer)
757 memunmap(bios_buffer);
758 platform_device_unregister(dcdbas_pdev_reg);
759 platform_driver_unregister(&dcdbas_driver);
760}
761
762subsys_initcall_sync(dcdbas_init);
763module_exit(dcdbas_exit);
764
765MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
766MODULE_VERSION(DRIVER_VERSION);
767MODULE_AUTHOR("Dell Inc.");
768MODULE_LICENSE("GPL");
769
770MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");
771