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 cpus_read_lock();
282 ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true);
283 cpus_read_unlock();
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 ((s8)inb(PCAT_APM_STATUS_PORT) == ESM_STATUS_CMD_UNSUCCESSFUL) {
398 num_ticks--;
399 if (num_ticks == EXPIRED_TIMER)
400 return -ETIME;
401 }
402 break;
403
404 case HC_SMITYPE_TYPE2:
405 case HC_SMITYPE_TYPE3:
406 spin_lock_irqsave(&rtc_lock, flags);
407
408 data = (u8 *)&smi_data_buf_phys_addr;
409 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
410 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
411 index++, data++) {
412 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
413 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
414 }
415
416
417 if (host_control_smi_type == HC_SMITYPE_TYPE3)
418 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
419 else
420 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
421
422
423 CMOS_READ(RTC_REG_C);
424 spin_unlock_irqrestore(&rtc_lock, flags);
425
426
427 cmd_status = inb(PE1400_APM_CONTROL_PORT);
428
429
430 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
431 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
432 num_ticks--;
433 if (num_ticks == EXPIRED_TIMER)
434 return -ETIME;
435 }
436 break;
437
438 default:
439 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
440 __func__, host_control_smi_type);
441 return -ENOSYS;
442 }
443
444 return 0;
445}
446
447
448
449
450
451
452
453
454
455
456static void dcdbas_host_control(void)
457{
458 struct apm_cmd *apm_cmd;
459 u8 action;
460
461 if (host_control_action == HC_ACTION_NONE)
462 return;
463
464 action = host_control_action;
465 host_control_action = HC_ACTION_NONE;
466
467 if (!smi_data_buf) {
468 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
469 return;
470 }
471
472 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
473 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
474 __func__);
475 return;
476 }
477
478 apm_cmd = (struct apm_cmd *)smi_data_buf;
479
480
481 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
482 apm_cmd->command = ESM_APM_POWER_CYCLE;
483 apm_cmd->reserved = 0;
484 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
485 host_control_smi();
486 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
487 apm_cmd->command = ESM_APM_POWER_CYCLE;
488 apm_cmd->reserved = 0;
489 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
490 host_control_smi();
491 }
492}
493
494
495
496static u8 checksum(u8 *buffer, u8 length)
497{
498 u8 sum = 0;
499 u8 *end = buffer + length;
500
501 while (buffer < end)
502 sum += *buffer++;
503 return sum;
504}
505
506static inline struct smm_eps_table *check_eps_table(u8 *addr)
507{
508 struct smm_eps_table *eps = (struct smm_eps_table *)addr;
509
510 if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0)
511 return NULL;
512
513 if (checksum(addr, eps->length) != 0)
514 return NULL;
515
516 return eps;
517}
518
519static int dcdbas_check_wsmt(void)
520{
521 const struct dmi_device *dev = NULL;
522 struct acpi_table_wsmt *wsmt = NULL;
523 struct smm_eps_table *eps = NULL;
524 u64 bios_buf_paddr;
525 u64 remap_size;
526 u8 *addr;
527
528 acpi_get_table(ACPI_SIG_WSMT, 0, (struct acpi_table_header **)&wsmt);
529 if (!wsmt)
530 return 0;
531
532
533 if (!(wsmt->protection_flags & ACPI_WSMT_FIXED_COMM_BUFFERS) ||
534 !(wsmt->protection_flags & ACPI_WSMT_COMM_BUFFER_NESTED_PTR_PROTECTION))
535 return 0;
536
537
538
539
540
541
542
543 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev)))
544 if (sscanf(dev->name, "30[%16llx;%8llx]", &bios_buf_paddr,
545 &remap_size) == 2)
546 goto remap;
547
548
549 for (addr = (u8 *)__va(0xf0000);
550 addr < (u8 *)__va(0x100000 - sizeof(struct smm_eps_table));
551 addr += 16) {
552 eps = check_eps_table(addr);
553 if (eps)
554 break;
555 }
556
557 if (!eps) {
558 dev_dbg(&dcdbas_pdev->dev, "found WSMT, but no firmware buffer found\n");
559 return -ENODEV;
560 }
561 bios_buf_paddr = eps->smm_comm_buff_addr;
562 remap_size = eps->num_of_4k_pages * PAGE_SIZE;
563
564remap:
565
566
567
568
569 if (upper_32_bits(bios_buf_paddr + 8)) {
570 dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer address is above 4GB\n");
571 return -EINVAL;
572 }
573
574
575
576
577 if (remap_size > MAX_SMI_DATA_BUF_SIZE + 8)
578 remap_size = MAX_SMI_DATA_BUF_SIZE + 8;
579
580 bios_buffer = memremap(bios_buf_paddr, remap_size, MEMREMAP_WB);
581 if (!bios_buffer) {
582 dev_warn(&dcdbas_pdev->dev, "found WSMT, but failed to map buffer\n");
583 return -ENOMEM;
584 }
585
586
587 smi_data_buf_phys_addr = bios_buf_paddr + 8;
588 smi_data_buf = bios_buffer + 8;
589 smi_data_buf_size = remap_size - 8;
590 max_smi_data_buf_size = smi_data_buf_size;
591 wsmt_enabled = true;
592 dev_info(&dcdbas_pdev->dev,
593 "WSMT found, using firmware-provided SMI buffer.\n");
594 return 1;
595}
596
597
598
599
600static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
601 void *unused)
602{
603 switch (code) {
604 case SYS_DOWN:
605 case SYS_HALT:
606 case SYS_POWER_OFF:
607 if (host_control_on_shutdown) {
608
609 printk(KERN_WARNING "Please wait for shutdown "
610 "action to complete...\n");
611 dcdbas_host_control();
612 }
613 break;
614 }
615
616 return NOTIFY_DONE;
617}
618
619static struct notifier_block dcdbas_reboot_nb = {
620 .notifier_call = dcdbas_reboot_notify,
621 .next = NULL,
622 .priority = INT_MIN
623};
624
625static DCDBAS_BIN_ATTR_RW(smi_data);
626
627static struct bin_attribute *dcdbas_bin_attrs[] = {
628 &bin_attr_smi_data,
629 NULL
630};
631
632static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
633static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
634static DCDBAS_DEV_ATTR_WO(smi_request);
635static DCDBAS_DEV_ATTR_RW(host_control_action);
636static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
637static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
638
639static struct attribute *dcdbas_dev_attrs[] = {
640 &dev_attr_smi_data_buf_size.attr,
641 &dev_attr_smi_data_buf_phys_addr.attr,
642 &dev_attr_smi_request.attr,
643 &dev_attr_host_control_action.attr,
644 &dev_attr_host_control_smi_type.attr,
645 &dev_attr_host_control_on_shutdown.attr,
646 NULL
647};
648
649static const struct attribute_group dcdbas_attr_group = {
650 .attrs = dcdbas_dev_attrs,
651 .bin_attrs = dcdbas_bin_attrs,
652};
653
654static int dcdbas_probe(struct platform_device *dev)
655{
656 int error;
657
658 host_control_action = HC_ACTION_NONE;
659 host_control_smi_type = HC_SMITYPE_NONE;
660
661 dcdbas_pdev = dev;
662
663
664 error = dcdbas_check_wsmt();
665 if (error < 0)
666 return error;
667
668
669
670
671
672 error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
673 if (error)
674 return error;
675
676 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
677 if (error)
678 return error;
679
680 register_reboot_notifier(&dcdbas_reboot_nb);
681
682 dev_info(&dev->dev, "%s (version %s)\n",
683 DRIVER_DESCRIPTION, DRIVER_VERSION);
684
685 return 0;
686}
687
688static int dcdbas_remove(struct platform_device *dev)
689{
690 unregister_reboot_notifier(&dcdbas_reboot_nb);
691 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
692
693 return 0;
694}
695
696static struct platform_driver dcdbas_driver = {
697 .driver = {
698 .name = DRIVER_NAME,
699 },
700 .probe = dcdbas_probe,
701 .remove = dcdbas_remove,
702};
703
704static const struct platform_device_info dcdbas_dev_info __initconst = {
705 .name = DRIVER_NAME,
706 .id = -1,
707 .dma_mask = DMA_BIT_MASK(32),
708};
709
710static struct platform_device *dcdbas_pdev_reg;
711
712
713
714
715static int __init dcdbas_init(void)
716{
717 int error;
718
719 error = platform_driver_register(&dcdbas_driver);
720 if (error)
721 return error;
722
723 dcdbas_pdev_reg = platform_device_register_full(&dcdbas_dev_info);
724 if (IS_ERR(dcdbas_pdev_reg)) {
725 error = PTR_ERR(dcdbas_pdev_reg);
726 goto err_unregister_driver;
727 }
728
729 return 0;
730
731 err_unregister_driver:
732 platform_driver_unregister(&dcdbas_driver);
733 return error;
734}
735
736
737
738
739static void __exit dcdbas_exit(void)
740{
741
742
743
744
745 unregister_reboot_notifier(&dcdbas_reboot_nb);
746
747
748
749
750
751
752
753 if (dcdbas_pdev)
754 smi_data_buf_free();
755 if (bios_buffer)
756 memunmap(bios_buffer);
757 platform_device_unregister(dcdbas_pdev_reg);
758 platform_driver_unregister(&dcdbas_driver);
759}
760
761subsys_initcall_sync(dcdbas_init);
762module_exit(dcdbas_exit);
763
764MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
765MODULE_VERSION(DRIVER_VERSION);
766MODULE_AUTHOR("Dell Inc.");
767MODULE_LICENSE("GPL");
768
769MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");
770