1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/platform_device.h>
24#include <linux/dma-mapping.h>
25#include <linux/errno.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/mc146818rtc.h>
29#include <linux/module.h>
30#include <linux/reboot.h>
31#include <linux/sched.h>
32#include <linux/smp.h>
33#include <linux/spinlock.h>
34#include <linux/string.h>
35#include <linux/types.h>
36#include <linux/mutex.h>
37#include <asm/io.h>
38
39#include "dcdbas.h"
40
41#define DRIVER_NAME "dcdbas"
42#define DRIVER_VERSION "5.6.0-3.2"
43#define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
44
45static struct platform_device *dcdbas_pdev;
46
47static u8 *smi_data_buf;
48static dma_addr_t smi_data_buf_handle;
49static unsigned long smi_data_buf_size;
50static u32 smi_data_buf_phys_addr;
51static DEFINE_MUTEX(smi_data_lock);
52
53static unsigned int host_control_action;
54static unsigned int host_control_smi_type;
55static unsigned int host_control_on_shutdown;
56
57
58
59
60static void smi_data_buf_free(void)
61{
62 if (!smi_data_buf)
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 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 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
240
241
242
243
244
245static int smi_request(struct smi_cmd *smi_cmd)
246{
247 cpumask_t old_mask;
248 int ret = 0;
249
250 if (smi_cmd->magic != SMI_CMD_MAGIC) {
251 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
252 __func__);
253 return -EBADR;
254 }
255
256
257 old_mask = current->cpus_allowed;
258 set_cpus_allowed_ptr(current, &cpumask_of_cpu(0));
259 if (smp_processor_id() != 0) {
260 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
261 __func__);
262 ret = -EBUSY;
263 goto out;
264 }
265
266
267
268 asm volatile (
269 "outb %b0,%w1\n"
270 "inb %w1"
271 :
272 : "a" (smi_cmd->command_code),
273 "d" (smi_cmd->command_address),
274 "b" (smi_cmd->ebx),
275 "c" (smi_cmd->ecx)
276 : "memory"
277 );
278
279out:
280 set_cpus_allowed_ptr(current, &old_mask);
281 return ret;
282}
283
284
285
286
287
288
289
290
291
292
293
294
295static ssize_t smi_request_store(struct device *dev,
296 struct device_attribute *attr,
297 const char *buf, size_t count)
298{
299 struct smi_cmd *smi_cmd;
300 unsigned long val = simple_strtoul(buf, NULL, 10);
301 ssize_t ret;
302
303 mutex_lock(&smi_data_lock);
304
305 if (smi_data_buf_size < sizeof(struct smi_cmd)) {
306 ret = -ENODEV;
307 goto out;
308 }
309 smi_cmd = (struct smi_cmd *)smi_data_buf;
310
311 switch (val) {
312 case 2:
313
314 ret = smi_request(smi_cmd);
315 if (!ret)
316 ret = count;
317 break;
318 case 1:
319
320 smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
321 ret = smi_request(smi_cmd);
322 if (!ret)
323 ret = count;
324 break;
325 case 0:
326 memset(smi_data_buf, 0, smi_data_buf_size);
327 ret = count;
328 break;
329 default:
330 ret = -EINVAL;
331 break;
332 }
333
334out:
335 mutex_unlock(&smi_data_lock);
336 return ret;
337}
338
339
340
341
342
343
344static int host_control_smi(void)
345{
346 struct apm_cmd *apm_cmd;
347 u8 *data;
348 unsigned long flags;
349 u32 num_ticks;
350 s8 cmd_status;
351 u8 index;
352
353 apm_cmd = (struct apm_cmd *)smi_data_buf;
354 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
355
356 switch (host_control_smi_type) {
357 case HC_SMITYPE_TYPE1:
358 spin_lock_irqsave(&rtc_lock, flags);
359
360 data = (u8 *)&smi_data_buf_phys_addr;
361 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
362 index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
363 index++, data++) {
364 outb(index,
365 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
366 outb(*data,
367 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
368 }
369
370
371 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
372 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
373
374
375 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
376 spin_unlock_irqrestore(&rtc_lock, flags);
377
378
379 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
380 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
381 == ESM_STATUS_CMD_UNSUCCESSFUL) {
382 num_ticks--;
383 if (num_ticks == EXPIRED_TIMER)
384 return -ETIME;
385 }
386 break;
387
388 case HC_SMITYPE_TYPE2:
389 case HC_SMITYPE_TYPE3:
390 spin_lock_irqsave(&rtc_lock, flags);
391
392 data = (u8 *)&smi_data_buf_phys_addr;
393 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
394 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
395 index++, data++) {
396 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
397 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
398 }
399
400
401 if (host_control_smi_type == HC_SMITYPE_TYPE3)
402 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
403 else
404 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
405
406
407 CMOS_READ(RTC_REG_C);
408 spin_unlock_irqrestore(&rtc_lock, flags);
409
410
411 cmd_status = inb(PE1400_APM_CONTROL_PORT);
412
413
414 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
415 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
416 num_ticks--;
417 if (num_ticks == EXPIRED_TIMER)
418 return -ETIME;
419 }
420 break;
421
422 default:
423 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
424 __func__, host_control_smi_type);
425 return -ENOSYS;
426 }
427
428 return 0;
429}
430
431
432
433
434
435
436
437
438
439
440static void dcdbas_host_control(void)
441{
442 struct apm_cmd *apm_cmd;
443 u8 action;
444
445 if (host_control_action == HC_ACTION_NONE)
446 return;
447
448 action = host_control_action;
449 host_control_action = HC_ACTION_NONE;
450
451 if (!smi_data_buf) {
452 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
453 return;
454 }
455
456 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
457 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
458 __func__);
459 return;
460 }
461
462 apm_cmd = (struct apm_cmd *)smi_data_buf;
463
464
465 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
466 apm_cmd->command = ESM_APM_POWER_CYCLE;
467 apm_cmd->reserved = 0;
468 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
469 host_control_smi();
470 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
471 apm_cmd->command = ESM_APM_POWER_CYCLE;
472 apm_cmd->reserved = 0;
473 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
474 host_control_smi();
475 }
476}
477
478
479
480
481static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
482 void *unused)
483{
484 switch (code) {
485 case SYS_DOWN:
486 case SYS_HALT:
487 case SYS_POWER_OFF:
488 if (host_control_on_shutdown) {
489
490 printk(KERN_WARNING "Please wait for shutdown "
491 "action to complete...\n");
492 dcdbas_host_control();
493 }
494 break;
495 }
496
497 return NOTIFY_DONE;
498}
499
500static struct notifier_block dcdbas_reboot_nb = {
501 .notifier_call = dcdbas_reboot_notify,
502 .next = NULL,
503 .priority = INT_MIN
504};
505
506static DCDBAS_BIN_ATTR_RW(smi_data);
507
508static struct bin_attribute *dcdbas_bin_attrs[] = {
509 &bin_attr_smi_data,
510 NULL
511};
512
513static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
514static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
515static DCDBAS_DEV_ATTR_WO(smi_request);
516static DCDBAS_DEV_ATTR_RW(host_control_action);
517static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
518static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
519
520static struct attribute *dcdbas_dev_attrs[] = {
521 &dev_attr_smi_data_buf_size.attr,
522 &dev_attr_smi_data_buf_phys_addr.attr,
523 &dev_attr_smi_request.attr,
524 &dev_attr_host_control_action.attr,
525 &dev_attr_host_control_smi_type.attr,
526 &dev_attr_host_control_on_shutdown.attr,
527 NULL
528};
529
530static struct attribute_group dcdbas_attr_group = {
531 .attrs = dcdbas_dev_attrs,
532};
533
534static int __devinit dcdbas_probe(struct platform_device *dev)
535{
536 int i, error;
537
538 host_control_action = HC_ACTION_NONE;
539 host_control_smi_type = HC_SMITYPE_NONE;
540
541
542
543
544
545 dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
546 dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
547
548 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
549 if (error)
550 return error;
551
552 for (i = 0; dcdbas_bin_attrs[i]; i++) {
553 error = sysfs_create_bin_file(&dev->dev.kobj,
554 dcdbas_bin_attrs[i]);
555 if (error) {
556 while (--i >= 0)
557 sysfs_remove_bin_file(&dev->dev.kobj,
558 dcdbas_bin_attrs[i]);
559 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
560 return error;
561 }
562 }
563
564 register_reboot_notifier(&dcdbas_reboot_nb);
565
566 dev_info(&dev->dev, "%s (version %s)\n",
567 DRIVER_DESCRIPTION, DRIVER_VERSION);
568
569 return 0;
570}
571
572static int __devexit dcdbas_remove(struct platform_device *dev)
573{
574 int i;
575
576 unregister_reboot_notifier(&dcdbas_reboot_nb);
577 for (i = 0; dcdbas_bin_attrs[i]; i++)
578 sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]);
579 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
580
581 return 0;
582}
583
584static struct platform_driver dcdbas_driver = {
585 .driver = {
586 .name = DRIVER_NAME,
587 .owner = THIS_MODULE,
588 },
589 .probe = dcdbas_probe,
590 .remove = __devexit_p(dcdbas_remove),
591};
592
593
594
595
596static int __init dcdbas_init(void)
597{
598 int error;
599
600 error = platform_driver_register(&dcdbas_driver);
601 if (error)
602 return error;
603
604 dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1);
605 if (!dcdbas_pdev) {
606 error = -ENOMEM;
607 goto err_unregister_driver;
608 }
609
610 error = platform_device_add(dcdbas_pdev);
611 if (error)
612 goto err_free_device;
613
614 return 0;
615
616 err_free_device:
617 platform_device_put(dcdbas_pdev);
618 err_unregister_driver:
619 platform_driver_unregister(&dcdbas_driver);
620 return error;
621}
622
623
624
625
626static void __exit dcdbas_exit(void)
627{
628
629
630
631
632 unregister_reboot_notifier(&dcdbas_reboot_nb);
633 smi_data_buf_free();
634 platform_device_unregister(dcdbas_pdev);
635 platform_driver_unregister(&dcdbas_driver);
636
637
638
639
640
641
642
643 smi_data_buf_free();
644}
645
646module_init(dcdbas_init);
647module_exit(dcdbas_exit);
648
649MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
650MODULE_VERSION(DRIVER_VERSION);
651MODULE_AUTHOR("Dell Inc.");
652MODULE_LICENSE("GPL");
653
654MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");
655