dcdbas.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * dcdbas.c: Dell Systems Management Base Driver
  3. *
  4. * The Dell Systems Management Base Driver provides a sysfs interface for
  5. * systems management software to perform System Management Interrupts (SMIs)
  6. * and Host Control Actions (power cycle or power off after OS shutdown) on
  7. * Dell systems.
  8. *
  9. * See Documentation/dcdbas.txt for more information.
  10. *
  11. * Copyright (C) 1995-2006 Dell Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License v2.0 as published by
  15. * the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/platform_device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mc146818rtc.h>
  28. #include <linux/module.h>
  29. #include <linux/reboot.h>
  30. #include <linux/sched.h>
  31. #include <linux/smp.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/string.h>
  34. #include <linux/types.h>
  35. #include <linux/mutex.h>
  36. #include <asm/io.h>
  37. #include "dcdbas.h"
  38. #define DRIVER_NAME "dcdbas"
  39. #define DRIVER_VERSION "5.6.0-3.2"
  40. #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
  41. static struct platform_device *dcdbas_pdev;
  42. static u8 *smi_data_buf;
  43. static dma_addr_t smi_data_buf_handle;
  44. static unsigned long smi_data_buf_size;
  45. static u32 smi_data_buf_phys_addr;
  46. static DEFINE_MUTEX(smi_data_lock);
  47. static unsigned int host_control_action;
  48. static unsigned int host_control_smi_type;
  49. static unsigned int host_control_on_shutdown;
  50. /**
  51. * smi_data_buf_free: free SMI data buffer
  52. */
  53. static void smi_data_buf_free(void)
  54. {
  55. if (!smi_data_buf)
  56. return;
  57. dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
  58. __func__, smi_data_buf_phys_addr, smi_data_buf_size);
  59. dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf,
  60. smi_data_buf_handle);
  61. smi_data_buf = NULL;
  62. smi_data_buf_handle = 0;
  63. smi_data_buf_phys_addr = 0;
  64. smi_data_buf_size = 0;
  65. }
  66. /**
  67. * smi_data_buf_realloc: grow SMI data buffer if needed
  68. */
  69. static int smi_data_buf_realloc(unsigned long size)
  70. {
  71. void *buf;
  72. dma_addr_t handle;
  73. if (smi_data_buf_size >= size)
  74. return 0;
  75. if (size > MAX_SMI_DATA_BUF_SIZE)
  76. return -EINVAL;
  77. /* new buffer is needed */
  78. buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL);
  79. if (!buf) {
  80. dev_dbg(&dcdbas_pdev->dev,
  81. "%s: failed to allocate memory size %lu\n",
  82. __func__, size);
  83. return -ENOMEM;
  84. }
  85. /* memory zeroed by dma_alloc_coherent */
  86. if (smi_data_buf)
  87. memcpy(buf, smi_data_buf, smi_data_buf_size);
  88. /* free any existing buffer */
  89. smi_data_buf_free();
  90. /* set up new buffer for use */
  91. smi_data_buf = buf;
  92. smi_data_buf_handle = handle;
  93. smi_data_buf_phys_addr = (u32) virt_to_phys(buf);
  94. smi_data_buf_size = size;
  95. dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
  96. __func__, smi_data_buf_phys_addr, smi_data_buf_size);
  97. return 0;
  98. }
  99. static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
  100. struct device_attribute *attr,
  101. char *buf)
  102. {
  103. return sprintf(buf, "%x\n", smi_data_buf_phys_addr);
  104. }
  105. static ssize_t smi_data_buf_size_show(struct device *dev,
  106. struct device_attribute *attr,
  107. char *buf)
  108. {
  109. return sprintf(buf, "%lu\n", smi_data_buf_size);
  110. }
  111. static ssize_t smi_data_buf_size_store(struct device *dev,
  112. struct device_attribute *attr,
  113. const char *buf, size_t count)
  114. {
  115. unsigned long buf_size;
  116. ssize_t ret;
  117. buf_size = simple_strtoul(buf, NULL, 10);
  118. /* make sure SMI data buffer is at least buf_size */
  119. mutex_lock(&smi_data_lock);
  120. ret = smi_data_buf_realloc(buf_size);
  121. mutex_unlock(&smi_data_lock);
  122. if (ret)
  123. return ret;
  124. return count;
  125. }
  126. static ssize_t smi_data_read(struct kobject *kobj,
  127. struct bin_attribute *bin_attr,
  128. char *buf, loff_t pos, size_t count)
  129. {
  130. ssize_t ret;
  131. mutex_lock(&smi_data_lock);
  132. ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf,
  133. smi_data_buf_size);
  134. mutex_unlock(&smi_data_lock);
  135. return ret;
  136. }
  137. static ssize_t smi_data_write(struct kobject *kobj,
  138. struct bin_attribute *bin_attr,
  139. char *buf, loff_t pos, size_t count)
  140. {
  141. ssize_t ret;
  142. if ((pos + count) > MAX_SMI_DATA_BUF_SIZE)
  143. return -EINVAL;
  144. mutex_lock(&smi_data_lock);
  145. ret = smi_data_buf_realloc(pos + count);
  146. if (ret)
  147. goto out;
  148. memcpy(smi_data_buf + pos, buf, count);
  149. ret = count;
  150. out:
  151. mutex_unlock(&smi_data_lock);
  152. return ret;
  153. }
  154. static ssize_t host_control_action_show(struct device *dev,
  155. struct device_attribute *attr,
  156. char *buf)
  157. {
  158. return sprintf(buf, "%u\n", host_control_action);
  159. }
  160. static ssize_t host_control_action_store(struct device *dev,
  161. struct device_attribute *attr,
  162. const char *buf, size_t count)
  163. {
  164. ssize_t ret;
  165. /* make sure buffer is available for host control command */
  166. mutex_lock(&smi_data_lock);
  167. ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
  168. mutex_unlock(&smi_data_lock);
  169. if (ret)
  170. return ret;
  171. host_control_action = simple_strtoul(buf, NULL, 10);
  172. return count;
  173. }
  174. static ssize_t host_control_smi_type_show(struct device *dev,
  175. struct device_attribute *attr,
  176. char *buf)
  177. {
  178. return sprintf(buf, "%u\n", host_control_smi_type);
  179. }
  180. static ssize_t host_control_smi_type_store(struct device *dev,
  181. struct device_attribute *attr,
  182. const char *buf, size_t count)
  183. {
  184. host_control_smi_type = simple_strtoul(buf, NULL, 10);
  185. return count;
  186. }
  187. static ssize_t host_control_on_shutdown_show(struct device *dev,
  188. struct device_attribute *attr,
  189. char *buf)
  190. {
  191. return sprintf(buf, "%u\n", host_control_on_shutdown);
  192. }
  193. static ssize_t host_control_on_shutdown_store(struct device *dev,
  194. struct device_attribute *attr,
  195. const char *buf, size_t count)
  196. {
  197. host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
  198. return count;
  199. }
  200. /**
  201. * smi_request: generate SMI request
  202. *
  203. * Called with smi_data_lock.
  204. */
  205. static int smi_request(struct smi_cmd *smi_cmd)
  206. {
  207. cpumask_t old_mask;
  208. int ret = 0;
  209. if (smi_cmd->magic != SMI_CMD_MAGIC) {
  210. dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
  211. __func__);
  212. return -EBADR;
  213. }
  214. /* SMI requires CPU 0 */
  215. old_mask = current->cpus_allowed;
  216. set_cpus_allowed_ptr(current, &cpumask_of_cpu(0));
  217. if (smp_processor_id() != 0) {
  218. dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
  219. __func__);
  220. ret = -EBUSY;
  221. goto out;
  222. }
  223. /* generate SMI */
  224. asm volatile (
  225. "outb %b0,%w1"
  226. : /* no output args */
  227. : "a" (smi_cmd->command_code),
  228. "d" (smi_cmd->command_address),
  229. "b" (smi_cmd->ebx),
  230. "c" (smi_cmd->ecx)
  231. : "memory"
  232. );
  233. out:
  234. set_cpus_allowed_ptr(current, &old_mask);
  235. return ret;
  236. }
  237. /**
  238. * smi_request_store:
  239. *
  240. * The valid values are:
  241. * 0: zero SMI data buffer
  242. * 1: generate calling interface SMI
  243. * 2: generate raw SMI
  244. *
  245. * User application writes smi_cmd to smi_data before telling driver
  246. * to generate SMI.
  247. */
  248. static ssize_t smi_request_store(struct device *dev,
  249. struct device_attribute *attr,
  250. const char *buf, size_t count)
  251. {
  252. struct smi_cmd *smi_cmd;
  253. unsigned long val = simple_strtoul(buf, NULL, 10);
  254. ssize_t ret;
  255. mutex_lock(&smi_data_lock);
  256. if (smi_data_buf_size < sizeof(struct smi_cmd)) {
  257. ret = -ENODEV;
  258. goto out;
  259. }
  260. smi_cmd = (struct smi_cmd *)smi_data_buf;
  261. switch (val) {
  262. case 2:
  263. /* Raw SMI */
  264. ret = smi_request(smi_cmd);
  265. if (!ret)
  266. ret = count;
  267. break;
  268. case 1:
  269. /* Calling Interface SMI */
  270. smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
  271. ret = smi_request(smi_cmd);
  272. if (!ret)
  273. ret = count;
  274. break;
  275. case 0:
  276. memset(smi_data_buf, 0, smi_data_buf_size);
  277. ret = count;
  278. break;
  279. default:
  280. ret = -EINVAL;
  281. break;
  282. }
  283. out:
  284. mutex_unlock(&smi_data_lock);
  285. return ret;
  286. }
  287. /**
  288. * host_control_smi: generate host control SMI
  289. *
  290. * Caller must set up the host control command in smi_data_buf.
  291. */
  292. static int host_control_smi(void)
  293. {
  294. struct apm_cmd *apm_cmd;
  295. u8 *data;
  296. unsigned long flags;
  297. u32 num_ticks;
  298. s8 cmd_status;
  299. u8 index;
  300. apm_cmd = (struct apm_cmd *)smi_data_buf;
  301. apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
  302. switch (host_control_smi_type) {
  303. case HC_SMITYPE_TYPE1:
  304. spin_lock_irqsave(&rtc_lock, flags);
  305. /* write SMI data buffer physical address */
  306. data = (u8 *)&smi_data_buf_phys_addr;
  307. for (index = PE1300_CMOS_CMD_STRUCT_PTR;
  308. index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
  309. index++, data++) {
  310. outb(index,
  311. (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
  312. outb(*data,
  313. (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
  314. }
  315. /* first set status to -1 as called by spec */
  316. cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
  317. outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
  318. /* generate SMM call */
  319. outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
  320. spin_unlock_irqrestore(&rtc_lock, flags);
  321. /* wait a few to see if it executed */
  322. num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
  323. while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
  324. == ESM_STATUS_CMD_UNSUCCESSFUL) {
  325. num_ticks--;
  326. if (num_ticks == EXPIRED_TIMER)
  327. return -ETIME;
  328. }
  329. break;
  330. case HC_SMITYPE_TYPE2:
  331. case HC_SMITYPE_TYPE3:
  332. spin_lock_irqsave(&rtc_lock, flags);
  333. /* write SMI data buffer physical address */
  334. data = (u8 *)&smi_data_buf_phys_addr;
  335. for (index = PE1400_CMOS_CMD_STRUCT_PTR;
  336. index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
  337. index++, data++) {
  338. outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
  339. outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
  340. }
  341. /* generate SMM call */
  342. if (host_control_smi_type == HC_SMITYPE_TYPE3)
  343. outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
  344. else
  345. outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
  346. /* restore RTC index pointer since it was written to above */
  347. CMOS_READ(RTC_REG_C);
  348. spin_unlock_irqrestore(&rtc_lock, flags);
  349. /* read control port back to serialize write */
  350. cmd_status = inb(PE1400_APM_CONTROL_PORT);
  351. /* wait a few to see if it executed */
  352. num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
  353. while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
  354. num_ticks--;
  355. if (num_ticks == EXPIRED_TIMER)
  356. return -ETIME;
  357. }
  358. break;
  359. default:
  360. dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
  361. __func__, host_control_smi_type);
  362. return -ENOSYS;
  363. }
  364. return 0;
  365. }
  366. /**
  367. * dcdbas_host_control: initiate host control
  368. *
  369. * This function is called by the driver after the system has
  370. * finished shutting down if the user application specified a
  371. * host control action to perform on shutdown. It is safe to
  372. * use smi_data_buf at this point because the system has finished
  373. * shutting down and no userspace apps are running.
  374. */
  375. static void dcdbas_host_control(void)
  376. {
  377. struct apm_cmd *apm_cmd;
  378. u8 action;
  379. if (host_control_action == HC_ACTION_NONE)
  380. return;
  381. action = host_control_action;
  382. host_control_action = HC_ACTION_NONE;
  383. if (!smi_data_buf) {
  384. dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
  385. return;
  386. }
  387. if (smi_data_buf_size < sizeof(struct apm_cmd)) {
  388. dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
  389. __func__);
  390. return;
  391. }
  392. apm_cmd = (struct apm_cmd *)smi_data_buf;
  393. /* power off takes precedence */
  394. if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
  395. apm_cmd->command = ESM_APM_POWER_CYCLE;
  396. apm_cmd->reserved = 0;
  397. *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
  398. host_control_smi();
  399. } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
  400. apm_cmd->command = ESM_APM_POWER_CYCLE;
  401. apm_cmd->reserved = 0;
  402. *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
  403. host_control_smi();
  404. }
  405. }
  406. /**
  407. * dcdbas_reboot_notify: handle reboot notification for host control
  408. */
  409. static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
  410. void *unused)
  411. {
  412. switch (code) {
  413. case SYS_DOWN:
  414. case SYS_HALT:
  415. case SYS_POWER_OFF:
  416. if (host_control_on_shutdown) {
  417. /* firmware is going to perform host control action */
  418. printk(KERN_WARNING "Please wait for shutdown "
  419. "action to complete...\n");
  420. dcdbas_host_control();
  421. }
  422. break;
  423. }
  424. return NOTIFY_DONE;
  425. }
  426. static struct notifier_block dcdbas_reboot_nb = {
  427. .notifier_call = dcdbas_reboot_notify,
  428. .next = NULL,
  429. .priority = INT_MIN
  430. };
  431. static DCDBAS_BIN_ATTR_RW(smi_data);
  432. static struct bin_attribute *dcdbas_bin_attrs[] = {
  433. &bin_attr_smi_data,
  434. NULL
  435. };
  436. static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
  437. static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
  438. static DCDBAS_DEV_ATTR_WO(smi_request);
  439. static DCDBAS_DEV_ATTR_RW(host_control_action);
  440. static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
  441. static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
  442. static struct attribute *dcdbas_dev_attrs[] = {
  443. &dev_attr_smi_data_buf_size.attr,
  444. &dev_attr_smi_data_buf_phys_addr.attr,
  445. &dev_attr_smi_request.attr,
  446. &dev_attr_host_control_action.attr,
  447. &dev_attr_host_control_smi_type.attr,
  448. &dev_attr_host_control_on_shutdown.attr,
  449. NULL
  450. };
  451. static struct attribute_group dcdbas_attr_group = {
  452. .attrs = dcdbas_dev_attrs,
  453. };
  454. static int __devinit dcdbas_probe(struct platform_device *dev)
  455. {
  456. int i, error;
  457. host_control_action = HC_ACTION_NONE;
  458. host_control_smi_type = HC_SMITYPE_NONE;
  459. /*
  460. * BIOS SMI calls require buffer addresses be in 32-bit address space.
  461. * This is done by setting the DMA mask below.
  462. */
  463. dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
  464. dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
  465. error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
  466. if (error)
  467. return error;
  468. for (i = 0; dcdbas_bin_attrs[i]; i++) {
  469. error = sysfs_create_bin_file(&dev->dev.kobj,
  470. dcdbas_bin_attrs[i]);
  471. if (error) {
  472. while (--i >= 0)
  473. sysfs_remove_bin_file(&dev->dev.kobj,
  474. dcdbas_bin_attrs[i]);
  475. sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
  476. return error;
  477. }
  478. }
  479. register_reboot_notifier(&dcdbas_reboot_nb);
  480. dev_info(&dev->dev, "%s (version %s)\n",
  481. DRIVER_DESCRIPTION, DRIVER_VERSION);
  482. return 0;
  483. }
  484. static int __devexit dcdbas_remove(struct platform_device *dev)
  485. {
  486. int i;
  487. unregister_reboot_notifier(&dcdbas_reboot_nb);
  488. for (i = 0; dcdbas_bin_attrs[i]; i++)
  489. sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]);
  490. sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
  491. return 0;
  492. }
  493. static struct platform_driver dcdbas_driver = {
  494. .driver = {
  495. .name = DRIVER_NAME,
  496. .owner = THIS_MODULE,
  497. },
  498. .probe = dcdbas_probe,
  499. .remove = __devexit_p(dcdbas_remove),
  500. };
  501. /**
  502. * dcdbas_init: initialize driver
  503. */
  504. static int __init dcdbas_init(void)
  505. {
  506. int error;
  507. error = platform_driver_register(&dcdbas_driver);
  508. if (error)
  509. return error;
  510. dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1);
  511. if (!dcdbas_pdev) {
  512. error = -ENOMEM;
  513. goto err_unregister_driver;
  514. }
  515. error = platform_device_add(dcdbas_pdev);
  516. if (error)
  517. goto err_free_device;
  518. return 0;
  519. err_free_device:
  520. platform_device_put(dcdbas_pdev);
  521. err_unregister_driver:
  522. platform_driver_unregister(&dcdbas_driver);
  523. return error;
  524. }
  525. /**
  526. * dcdbas_exit: perform driver cleanup
  527. */
  528. static void __exit dcdbas_exit(void)
  529. {
  530. /*
  531. * make sure functions that use dcdbas_pdev are called
  532. * before platform_device_unregister
  533. */
  534. unregister_reboot_notifier(&dcdbas_reboot_nb);
  535. smi_data_buf_free();
  536. platform_device_unregister(dcdbas_pdev);
  537. platform_driver_unregister(&dcdbas_driver);
  538. /*
  539. * We have to free the buffer here instead of dcdbas_remove
  540. * because only in module exit function we can be sure that
  541. * all sysfs attributes belonging to this module have been
  542. * released.
  543. */
  544. smi_data_buf_free();
  545. }
  546. module_init(dcdbas_init);
  547. module_exit(dcdbas_exit);
  548. MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
  549. MODULE_VERSION(DRIVER_VERSION);
  550. MODULE_AUTHOR("Dell Inc.");
  551. MODULE_LICENSE("GPL");
  552. /* Any System or BIOS claiming to be by Dell */
  553. MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");