dcdbas.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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-2005 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 <asm/io.h>
  36. #include <asm/semaphore.h>
  37. #include "dcdbas.h"
  38. #define DRIVER_NAME "dcdbas"
  39. #define DRIVER_VERSION "5.6.0-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 DECLARE_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. __FUNCTION__, 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. __FUNCTION__, 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. __FUNCTION__, 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. down(&smi_data_lock);
  120. ret = smi_data_buf_realloc(buf_size);
  121. up(&smi_data_lock);
  122. if (ret)
  123. return ret;
  124. return count;
  125. }
  126. static ssize_t smi_data_read(struct kobject *kobj, char *buf, loff_t pos,
  127. size_t count)
  128. {
  129. size_t max_read;
  130. ssize_t ret;
  131. down(&smi_data_lock);
  132. if (pos >= smi_data_buf_size) {
  133. ret = 0;
  134. goto out;
  135. }
  136. max_read = smi_data_buf_size - pos;
  137. ret = min(max_read, count);
  138. memcpy(buf, smi_data_buf + pos, ret);
  139. out:
  140. up(&smi_data_lock);
  141. return ret;
  142. }
  143. static ssize_t smi_data_write(struct kobject *kobj, char *buf, loff_t pos,
  144. size_t count)
  145. {
  146. ssize_t ret;
  147. down(&smi_data_lock);
  148. ret = smi_data_buf_realloc(pos + count);
  149. if (ret)
  150. goto out;
  151. memcpy(smi_data_buf + pos, buf, count);
  152. ret = count;
  153. out:
  154. up(&smi_data_lock);
  155. return ret;
  156. }
  157. static ssize_t host_control_action_show(struct device *dev,
  158. struct device_attribute *attr,
  159. char *buf)
  160. {
  161. return sprintf(buf, "%u\n", host_control_action);
  162. }
  163. static ssize_t host_control_action_store(struct device *dev,
  164. struct device_attribute *attr,
  165. const char *buf, size_t count)
  166. {
  167. ssize_t ret;
  168. /* make sure buffer is available for host control command */
  169. down(&smi_data_lock);
  170. ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
  171. up(&smi_data_lock);
  172. if (ret)
  173. return ret;
  174. host_control_action = simple_strtoul(buf, NULL, 10);
  175. return count;
  176. }
  177. static ssize_t host_control_smi_type_show(struct device *dev,
  178. struct device_attribute *attr,
  179. char *buf)
  180. {
  181. return sprintf(buf, "%u\n", host_control_smi_type);
  182. }
  183. static ssize_t host_control_smi_type_store(struct device *dev,
  184. struct device_attribute *attr,
  185. const char *buf, size_t count)
  186. {
  187. host_control_smi_type = simple_strtoul(buf, NULL, 10);
  188. return count;
  189. }
  190. static ssize_t host_control_on_shutdown_show(struct device *dev,
  191. struct device_attribute *attr,
  192. char *buf)
  193. {
  194. return sprintf(buf, "%u\n", host_control_on_shutdown);
  195. }
  196. static ssize_t host_control_on_shutdown_store(struct device *dev,
  197. struct device_attribute *attr,
  198. const char *buf, size_t count)
  199. {
  200. host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
  201. return count;
  202. }
  203. /**
  204. * smi_request: generate SMI request
  205. *
  206. * Called with smi_data_lock.
  207. */
  208. static int smi_request(struct smi_cmd *smi_cmd)
  209. {
  210. cpumask_t old_mask;
  211. int ret = 0;
  212. if (smi_cmd->magic != SMI_CMD_MAGIC) {
  213. dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
  214. __FUNCTION__);
  215. return -EBADR;
  216. }
  217. /* SMI requires CPU 0 */
  218. old_mask = current->cpus_allowed;
  219. set_cpus_allowed(current, cpumask_of_cpu(0));
  220. if (smp_processor_id() != 0) {
  221. dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
  222. __FUNCTION__);
  223. ret = -EBUSY;
  224. goto out;
  225. }
  226. /* generate SMI */
  227. asm volatile (
  228. "outb %b0,%w1"
  229. : /* no output args */
  230. : "a" (smi_cmd->command_code),
  231. "d" (smi_cmd->command_address),
  232. "b" (smi_cmd->ebx),
  233. "c" (smi_cmd->ecx)
  234. : "memory"
  235. );
  236. out:
  237. set_cpus_allowed(current, old_mask);
  238. return ret;
  239. }
  240. /**
  241. * smi_request_store:
  242. *
  243. * The valid values are:
  244. * 0: zero SMI data buffer
  245. * 1: generate calling interface SMI
  246. * 2: generate raw SMI
  247. *
  248. * User application writes smi_cmd to smi_data before telling driver
  249. * to generate SMI.
  250. */
  251. static ssize_t smi_request_store(struct device *dev,
  252. struct device_attribute *attr,
  253. const char *buf, size_t count)
  254. {
  255. struct smi_cmd *smi_cmd;
  256. unsigned long val = simple_strtoul(buf, NULL, 10);
  257. ssize_t ret;
  258. down(&smi_data_lock);
  259. if (smi_data_buf_size < sizeof(struct smi_cmd)) {
  260. ret = -ENODEV;
  261. goto out;
  262. }
  263. smi_cmd = (struct smi_cmd *)smi_data_buf;
  264. switch (val) {
  265. case 2:
  266. /* Raw SMI */
  267. ret = smi_request(smi_cmd);
  268. if (!ret)
  269. ret = count;
  270. break;
  271. case 1:
  272. /* Calling Interface SMI */
  273. smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
  274. ret = smi_request(smi_cmd);
  275. if (!ret)
  276. ret = count;
  277. break;
  278. case 0:
  279. memset(smi_data_buf, 0, smi_data_buf_size);
  280. ret = count;
  281. break;
  282. default:
  283. ret = -EINVAL;
  284. break;
  285. }
  286. out:
  287. up(&smi_data_lock);
  288. return ret;
  289. }
  290. /**
  291. * host_control_smi: generate host control SMI
  292. *
  293. * Caller must set up the host control command in smi_data_buf.
  294. */
  295. static int host_control_smi(void)
  296. {
  297. struct apm_cmd *apm_cmd;
  298. u8 *data;
  299. unsigned long flags;
  300. u32 num_ticks;
  301. s8 cmd_status;
  302. u8 index;
  303. apm_cmd = (struct apm_cmd *)smi_data_buf;
  304. apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
  305. switch (host_control_smi_type) {
  306. case HC_SMITYPE_TYPE1:
  307. spin_lock_irqsave(&rtc_lock, flags);
  308. /* write SMI data buffer physical address */
  309. data = (u8 *)&smi_data_buf_phys_addr;
  310. for (index = PE1300_CMOS_CMD_STRUCT_PTR;
  311. index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
  312. index++, data++) {
  313. outb(index,
  314. (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
  315. outb(*data,
  316. (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
  317. }
  318. /* first set status to -1 as called by spec */
  319. cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
  320. outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
  321. /* generate SMM call */
  322. outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
  323. spin_unlock_irqrestore(&rtc_lock, flags);
  324. /* wait a few to see if it executed */
  325. num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
  326. while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
  327. == ESM_STATUS_CMD_UNSUCCESSFUL) {
  328. num_ticks--;
  329. if (num_ticks == EXPIRED_TIMER)
  330. return -ETIME;
  331. }
  332. break;
  333. case HC_SMITYPE_TYPE2:
  334. case HC_SMITYPE_TYPE3:
  335. spin_lock_irqsave(&rtc_lock, flags);
  336. /* write SMI data buffer physical address */
  337. data = (u8 *)&smi_data_buf_phys_addr;
  338. for (index = PE1400_CMOS_CMD_STRUCT_PTR;
  339. index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
  340. index++, data++) {
  341. outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
  342. outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
  343. }
  344. /* generate SMM call */
  345. if (host_control_smi_type == HC_SMITYPE_TYPE3)
  346. outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
  347. else
  348. outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
  349. /* restore RTC index pointer since it was written to above */
  350. CMOS_READ(RTC_REG_C);
  351. spin_unlock_irqrestore(&rtc_lock, flags);
  352. /* read control port back to serialize write */
  353. cmd_status = inb(PE1400_APM_CONTROL_PORT);
  354. /* wait a few to see if it executed */
  355. num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
  356. while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
  357. num_ticks--;
  358. if (num_ticks == EXPIRED_TIMER)
  359. return -ETIME;
  360. }
  361. break;
  362. default:
  363. dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
  364. __FUNCTION__, host_control_smi_type);
  365. return -ENOSYS;
  366. }
  367. return 0;
  368. }
  369. /**
  370. * dcdbas_host_control: initiate host control
  371. *
  372. * This function is called by the driver after the system has
  373. * finished shutting down if the user application specified a
  374. * host control action to perform on shutdown. It is safe to
  375. * use smi_data_buf at this point because the system has finished
  376. * shutting down and no userspace apps are running.
  377. */
  378. static void dcdbas_host_control(void)
  379. {
  380. struct apm_cmd *apm_cmd;
  381. u8 action;
  382. if (host_control_action == HC_ACTION_NONE)
  383. return;
  384. action = host_control_action;
  385. host_control_action = HC_ACTION_NONE;
  386. if (!smi_data_buf) {
  387. dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __FUNCTION__);
  388. return;
  389. }
  390. if (smi_data_buf_size < sizeof(struct apm_cmd)) {
  391. dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
  392. __FUNCTION__);
  393. return;
  394. }
  395. apm_cmd = (struct apm_cmd *)smi_data_buf;
  396. /* power off takes precedence */
  397. if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
  398. apm_cmd->command = ESM_APM_POWER_CYCLE;
  399. apm_cmd->reserved = 0;
  400. *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
  401. host_control_smi();
  402. } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
  403. apm_cmd->command = ESM_APM_POWER_CYCLE;
  404. apm_cmd->reserved = 0;
  405. *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
  406. host_control_smi();
  407. }
  408. }
  409. /**
  410. * dcdbas_reboot_notify: handle reboot notification for host control
  411. */
  412. static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
  413. void *unused)
  414. {
  415. static unsigned int notify_cnt = 0;
  416. switch (code) {
  417. case SYS_DOWN:
  418. case SYS_HALT:
  419. case SYS_POWER_OFF:
  420. if (host_control_on_shutdown) {
  421. /* firmware is going to perform host control action */
  422. if (++notify_cnt == 2) {
  423. printk(KERN_WARNING
  424. "Please wait for shutdown "
  425. "action to complete...\n");
  426. dcdbas_host_control();
  427. }
  428. /*
  429. * register again and initiate the host control
  430. * action on the second notification to allow
  431. * everyone that registered to be notified
  432. */
  433. register_reboot_notifier(nb);
  434. }
  435. break;
  436. }
  437. return NOTIFY_DONE;
  438. }
  439. static struct notifier_block dcdbas_reboot_nb = {
  440. .notifier_call = dcdbas_reboot_notify,
  441. .next = NULL,
  442. .priority = 0
  443. };
  444. static DCDBAS_BIN_ATTR_RW(smi_data);
  445. static struct bin_attribute *dcdbas_bin_attrs[] = {
  446. &bin_attr_smi_data,
  447. NULL
  448. };
  449. static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
  450. static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
  451. static DCDBAS_DEV_ATTR_WO(smi_request);
  452. static DCDBAS_DEV_ATTR_RW(host_control_action);
  453. static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
  454. static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
  455. static struct attribute *dcdbas_dev_attrs[] = {
  456. &dev_attr_smi_data_buf_size.attr,
  457. &dev_attr_smi_data_buf_phys_addr.attr,
  458. &dev_attr_smi_request.attr,
  459. &dev_attr_host_control_action.attr,
  460. &dev_attr_host_control_smi_type.attr,
  461. &dev_attr_host_control_on_shutdown.attr,
  462. NULL
  463. };
  464. static struct attribute_group dcdbas_attr_group = {
  465. .attrs = dcdbas_dev_attrs,
  466. };
  467. static int __devinit dcdbas_probe(struct platform_device *dev)
  468. {
  469. int i, error;
  470. host_control_action = HC_ACTION_NONE;
  471. host_control_smi_type = HC_SMITYPE_NONE;
  472. /*
  473. * BIOS SMI calls require buffer addresses be in 32-bit address space.
  474. * This is done by setting the DMA mask below.
  475. */
  476. dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
  477. dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
  478. error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
  479. if (error)
  480. return error;
  481. for (i = 0; dcdbas_bin_attrs[i]; i++) {
  482. error = sysfs_create_bin_file(&dev->dev.kobj,
  483. dcdbas_bin_attrs[i]);
  484. if (error) {
  485. while (--i >= 0)
  486. sysfs_remove_bin_file(&dev->dev.kobj,
  487. dcdbas_bin_attrs[i]);
  488. sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
  489. return error;
  490. }
  491. }
  492. register_reboot_notifier(&dcdbas_reboot_nb);
  493. dev_info(&dev->dev, "%s (version %s)\n",
  494. DRIVER_DESCRIPTION, DRIVER_VERSION);
  495. return 0;
  496. }
  497. static int __devexit dcdbas_remove(struct platform_device *dev)
  498. {
  499. int i;
  500. unregister_reboot_notifier(&dcdbas_reboot_nb);
  501. for (i = 0; dcdbas_bin_attrs[i]; i++)
  502. sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]);
  503. sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
  504. return 0;
  505. }
  506. static struct platform_driver dcdbas_driver = {
  507. .driver = {
  508. .name = DRIVER_NAME,
  509. .owner = THIS_MODULE,
  510. },
  511. .probe = dcdbas_probe,
  512. .remove = __devexit_p(dcdbas_remove),
  513. };
  514. /**
  515. * dcdbas_init: initialize driver
  516. */
  517. static int __init dcdbas_init(void)
  518. {
  519. int error;
  520. error = platform_driver_register(&dcdbas_driver);
  521. if (error)
  522. return error;
  523. dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1);
  524. if (!dcdbas_pdev) {
  525. error = -ENOMEM;
  526. goto err_unregister_driver;
  527. }
  528. error = platform_device_add(dcdbas_pdev);
  529. if (error)
  530. goto err_free_device;
  531. return 0;
  532. err_free_device:
  533. platform_device_put(dcdbas_pdev);
  534. err_unregister_driver:
  535. platform_driver_unregister(&dcdbas_driver);
  536. return error;
  537. }
  538. /**
  539. * dcdbas_exit: perform driver cleanup
  540. */
  541. static void __exit dcdbas_exit(void)
  542. {
  543. /*
  544. * make sure functions that use dcdbas_pdev are called
  545. * before platform_device_unregister
  546. */
  547. unregister_reboot_notifier(&dcdbas_reboot_nb);
  548. smi_data_buf_free();
  549. platform_device_unregister(dcdbas_pdev);
  550. platform_driver_unregister(&dcdbas_driver);
  551. /*
  552. * We have to free the buffer here instead of dcdbas_remove
  553. * because only in module exit function we can be sure that
  554. * all sysfs attributes belonging to this module have been
  555. * released.
  556. */
  557. smi_data_buf_free();
  558. }
  559. module_init(dcdbas_init);
  560. module_exit(dcdbas_exit);
  561. MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
  562. MODULE_VERSION(DRIVER_VERSION);
  563. MODULE_AUTHOR("Dell Inc.");
  564. MODULE_LICENSE("GPL");