dcdbas.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * dcdbas.c: Dell Systems Management Base Driver
  4. *
  5. * The Dell Systems Management Base Driver provides a sysfs interface for
  6. * systems management software to perform System Management Interrupts (SMIs)
  7. * and Host Control Actions (power cycle or power off after OS shutdown) on
  8. * Dell systems.
  9. *
  10. * See Documentation/driver-api/dcdbas.rst for more information.
  11. *
  12. * Copyright (C) 1995-2006 Dell Inc.
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/acpi.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/dmi.h>
  18. #include <linux/errno.h>
  19. #include <linux/cpu.h>
  20. #include <linux/gfp.h>
  21. #include <linux/init.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mc146818rtc.h>
  25. #include <linux/module.h>
  26. #include <linux/reboot.h>
  27. #include <linux/sched.h>
  28. #include <linux/smp.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/string.h>
  31. #include <linux/types.h>
  32. #include <linux/mutex.h>
  33. #include "dcdbas.h"
  34. #define DRIVER_NAME "dcdbas"
  35. #define DRIVER_VERSION "5.6.0-3.4"
  36. #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
  37. static struct platform_device *dcdbas_pdev;
  38. static unsigned long max_smi_data_buf_size = MAX_SMI_DATA_BUF_SIZE;
  39. static DEFINE_MUTEX(smi_data_lock);
  40. static u8 *bios_buffer;
  41. static struct smi_buffer smi_buf;
  42. static unsigned int host_control_action;
  43. static unsigned int host_control_smi_type;
  44. static unsigned int host_control_on_shutdown;
  45. static bool wsmt_enabled;
  46. int dcdbas_smi_alloc(struct smi_buffer *smi_buffer, unsigned long size)
  47. {
  48. smi_buffer->virt = dma_alloc_coherent(&dcdbas_pdev->dev, size,
  49. &smi_buffer->dma, GFP_KERNEL);
  50. if (!smi_buffer->virt) {
  51. dev_dbg(&dcdbas_pdev->dev,
  52. "%s: failed to allocate memory size %lu\n",
  53. __func__, size);
  54. return -ENOMEM;
  55. }
  56. smi_buffer->size = size;
  57. dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
  58. __func__, (u32)smi_buffer->dma, smi_buffer->size);
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(dcdbas_smi_alloc);
  62. void dcdbas_smi_free(struct smi_buffer *smi_buffer)
  63. {
  64. if (!smi_buffer->virt)
  65. return;
  66. dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
  67. __func__, (u32)smi_buffer->dma, smi_buffer->size);
  68. dma_free_coherent(&dcdbas_pdev->dev, smi_buffer->size,
  69. smi_buffer->virt, smi_buffer->dma);
  70. smi_buffer->virt = NULL;
  71. smi_buffer->dma = 0;
  72. smi_buffer->size = 0;
  73. }
  74. EXPORT_SYMBOL_GPL(dcdbas_smi_free);
  75. /**
  76. * smi_data_buf_free: free SMI data buffer
  77. */
  78. static void smi_data_buf_free(void)
  79. {
  80. if (!smi_buf.virt || wsmt_enabled)
  81. return;
  82. dcdbas_smi_free(&smi_buf);
  83. }
  84. /**
  85. * smi_data_buf_realloc: grow SMI data buffer if needed
  86. */
  87. static int smi_data_buf_realloc(unsigned long size)
  88. {
  89. struct smi_buffer tmp;
  90. int ret;
  91. if (smi_buf.size >= size)
  92. return 0;
  93. if (size > max_smi_data_buf_size)
  94. return -EINVAL;
  95. /* new buffer is needed */
  96. ret = dcdbas_smi_alloc(&tmp, size);
  97. if (ret)
  98. return ret;
  99. /* memory zeroed by dma_alloc_coherent */
  100. if (smi_buf.virt)
  101. memcpy(tmp.virt, smi_buf.virt, smi_buf.size);
  102. /* free any existing buffer */
  103. smi_data_buf_free();
  104. /* set up new buffer for use */
  105. smi_buf = tmp;
  106. return 0;
  107. }
  108. static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
  109. struct device_attribute *attr,
  110. char *buf)
  111. {
  112. return sprintf(buf, "%x\n", (u32)smi_buf.dma);
  113. }
  114. static ssize_t smi_data_buf_size_show(struct device *dev,
  115. struct device_attribute *attr,
  116. char *buf)
  117. {
  118. return sprintf(buf, "%lu\n", smi_buf.size);
  119. }
  120. static ssize_t smi_data_buf_size_store(struct device *dev,
  121. struct device_attribute *attr,
  122. const char *buf, size_t count)
  123. {
  124. unsigned long buf_size;
  125. ssize_t ret;
  126. buf_size = simple_strtoul(buf, NULL, 10);
  127. /* make sure SMI data buffer is at least buf_size */
  128. mutex_lock(&smi_data_lock);
  129. ret = smi_data_buf_realloc(buf_size);
  130. mutex_unlock(&smi_data_lock);
  131. if (ret)
  132. return ret;
  133. return count;
  134. }
  135. static ssize_t smi_data_read(struct file *filp, struct kobject *kobj,
  136. struct bin_attribute *bin_attr,
  137. char *buf, loff_t pos, size_t count)
  138. {
  139. ssize_t ret;
  140. mutex_lock(&smi_data_lock);
  141. ret = memory_read_from_buffer(buf, count, &pos, smi_buf.virt,
  142. smi_buf.size);
  143. mutex_unlock(&smi_data_lock);
  144. return ret;
  145. }
  146. static ssize_t smi_data_write(struct file *filp, struct kobject *kobj,
  147. struct bin_attribute *bin_attr,
  148. char *buf, loff_t pos, size_t count)
  149. {
  150. ssize_t ret;
  151. if ((pos + count) > max_smi_data_buf_size)
  152. return -EINVAL;
  153. mutex_lock(&smi_data_lock);
  154. ret = smi_data_buf_realloc(pos + count);
  155. if (ret)
  156. goto out;
  157. memcpy(smi_buf.virt + pos, buf, count);
  158. ret = count;
  159. out:
  160. mutex_unlock(&smi_data_lock);
  161. return ret;
  162. }
  163. static ssize_t host_control_action_show(struct device *dev,
  164. struct device_attribute *attr,
  165. char *buf)
  166. {
  167. return sprintf(buf, "%u\n", host_control_action);
  168. }
  169. static ssize_t host_control_action_store(struct device *dev,
  170. struct device_attribute *attr,
  171. const char *buf, size_t count)
  172. {
  173. ssize_t ret;
  174. /* make sure buffer is available for host control command */
  175. mutex_lock(&smi_data_lock);
  176. ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
  177. mutex_unlock(&smi_data_lock);
  178. if (ret)
  179. return ret;
  180. host_control_action = simple_strtoul(buf, NULL, 10);
  181. return count;
  182. }
  183. static ssize_t host_control_smi_type_show(struct device *dev,
  184. struct device_attribute *attr,
  185. char *buf)
  186. {
  187. return sprintf(buf, "%u\n", host_control_smi_type);
  188. }
  189. static ssize_t host_control_smi_type_store(struct device *dev,
  190. struct device_attribute *attr,
  191. const char *buf, size_t count)
  192. {
  193. host_control_smi_type = simple_strtoul(buf, NULL, 10);
  194. return count;
  195. }
  196. static ssize_t host_control_on_shutdown_show(struct device *dev,
  197. struct device_attribute *attr,
  198. char *buf)
  199. {
  200. return sprintf(buf, "%u\n", host_control_on_shutdown);
  201. }
  202. static ssize_t host_control_on_shutdown_store(struct device *dev,
  203. struct device_attribute *attr,
  204. const char *buf, size_t count)
  205. {
  206. host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
  207. return count;
  208. }
  209. static int raise_smi(void *par)
  210. {
  211. struct smi_cmd *smi_cmd = par;
  212. if (smp_processor_id() != 0) {
  213. dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
  214. __func__);
  215. return -EBUSY;
  216. }
  217. /* generate SMI */
  218. /* inb to force posted write through and make SMI happen now */
  219. asm volatile (
  220. "outb %b0,%w1\n"
  221. "inb %w1"
  222. : /* no output args */
  223. : "a" (smi_cmd->command_code),
  224. "d" (smi_cmd->command_address),
  225. "b" (smi_cmd->ebx),
  226. "c" (smi_cmd->ecx)
  227. : "memory"
  228. );
  229. return 0;
  230. }
  231. /**
  232. * dcdbas_smi_request: generate SMI request
  233. *
  234. * Called with smi_data_lock.
  235. */
  236. int dcdbas_smi_request(struct smi_cmd *smi_cmd)
  237. {
  238. int ret;
  239. if (smi_cmd->magic != SMI_CMD_MAGIC) {
  240. dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
  241. __func__);
  242. return -EBADR;
  243. }
  244. /* SMI requires CPU 0 */
  245. cpus_read_lock();
  246. ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true);
  247. cpus_read_unlock();
  248. return ret;
  249. }
  250. EXPORT_SYMBOL(dcdbas_smi_request);
  251. /**
  252. * smi_request_store:
  253. *
  254. * The valid values are:
  255. * 0: zero SMI data buffer
  256. * 1: generate calling interface SMI
  257. * 2: generate raw SMI
  258. *
  259. * User application writes smi_cmd to smi_data before telling driver
  260. * to generate SMI.
  261. */
  262. static ssize_t smi_request_store(struct device *dev,
  263. struct device_attribute *attr,
  264. const char *buf, size_t count)
  265. {
  266. struct smi_cmd *smi_cmd;
  267. unsigned long val = simple_strtoul(buf, NULL, 10);
  268. ssize_t ret;
  269. mutex_lock(&smi_data_lock);
  270. if (smi_buf.size < sizeof(struct smi_cmd)) {
  271. ret = -ENODEV;
  272. goto out;
  273. }
  274. smi_cmd = (struct smi_cmd *)smi_buf.virt;
  275. switch (val) {
  276. case 2:
  277. /* Raw SMI */
  278. ret = dcdbas_smi_request(smi_cmd);
  279. if (!ret)
  280. ret = count;
  281. break;
  282. case 1:
  283. /*
  284. * Calling Interface SMI
  285. *
  286. * Provide physical address of command buffer field within
  287. * the struct smi_cmd to BIOS.
  288. *
  289. * Because the address that smi_cmd (smi_buf.virt) points to
  290. * will be from memremap() of a non-memory address if WSMT
  291. * is present, we can't use virt_to_phys() on smi_cmd, so
  292. * we have to use the physical address that was saved when
  293. * the virtual address for smi_cmd was received.
  294. */
  295. smi_cmd->ebx = (u32)smi_buf.dma +
  296. offsetof(struct smi_cmd, command_buffer);
  297. ret = dcdbas_smi_request(smi_cmd);
  298. if (!ret)
  299. ret = count;
  300. break;
  301. case 0:
  302. memset(smi_buf.virt, 0, smi_buf.size);
  303. ret = count;
  304. break;
  305. default:
  306. ret = -EINVAL;
  307. break;
  308. }
  309. out:
  310. mutex_unlock(&smi_data_lock);
  311. return ret;
  312. }
  313. /**
  314. * host_control_smi: generate host control SMI
  315. *
  316. * Caller must set up the host control command in smi_buf.virt.
  317. */
  318. static int host_control_smi(void)
  319. {
  320. struct apm_cmd *apm_cmd;
  321. u8 *data;
  322. unsigned long flags;
  323. u32 num_ticks;
  324. s8 cmd_status;
  325. u8 index;
  326. apm_cmd = (struct apm_cmd *)smi_buf.virt;
  327. apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
  328. switch (host_control_smi_type) {
  329. case HC_SMITYPE_TYPE1:
  330. spin_lock_irqsave(&rtc_lock, flags);
  331. /* write SMI data buffer physical address */
  332. data = (u8 *)&smi_buf.dma;
  333. for (index = PE1300_CMOS_CMD_STRUCT_PTR;
  334. index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
  335. index++, data++) {
  336. outb(index,
  337. (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
  338. outb(*data,
  339. (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
  340. }
  341. /* first set status to -1 as called by spec */
  342. cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
  343. outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
  344. /* generate SMM call */
  345. outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
  346. spin_unlock_irqrestore(&rtc_lock, flags);
  347. /* wait a few to see if it executed */
  348. num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
  349. while ((s8)inb(PCAT_APM_STATUS_PORT) == ESM_STATUS_CMD_UNSUCCESSFUL) {
  350. num_ticks--;
  351. if (num_ticks == EXPIRED_TIMER)
  352. return -ETIME;
  353. }
  354. break;
  355. case HC_SMITYPE_TYPE2:
  356. case HC_SMITYPE_TYPE3:
  357. spin_lock_irqsave(&rtc_lock, flags);
  358. /* write SMI data buffer physical address */
  359. data = (u8 *)&smi_buf.dma;
  360. for (index = PE1400_CMOS_CMD_STRUCT_PTR;
  361. index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
  362. index++, data++) {
  363. outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
  364. outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
  365. }
  366. /* generate SMM call */
  367. if (host_control_smi_type == HC_SMITYPE_TYPE3)
  368. outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
  369. else
  370. outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
  371. /* restore RTC index pointer since it was written to above */
  372. CMOS_READ(RTC_REG_C);
  373. spin_unlock_irqrestore(&rtc_lock, flags);
  374. /* read control port back to serialize write */
  375. cmd_status = inb(PE1400_APM_CONTROL_PORT);
  376. /* wait a few to see if it executed */
  377. num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
  378. while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
  379. num_ticks--;
  380. if (num_ticks == EXPIRED_TIMER)
  381. return -ETIME;
  382. }
  383. break;
  384. default:
  385. dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
  386. __func__, host_control_smi_type);
  387. return -ENOSYS;
  388. }
  389. return 0;
  390. }
  391. /**
  392. * dcdbas_host_control: initiate host control
  393. *
  394. * This function is called by the driver after the system has
  395. * finished shutting down if the user application specified a
  396. * host control action to perform on shutdown. It is safe to
  397. * use smi_buf.virt at this point because the system has finished
  398. * shutting down and no userspace apps are running.
  399. */
  400. static void dcdbas_host_control(void)
  401. {
  402. struct apm_cmd *apm_cmd;
  403. u8 action;
  404. if (host_control_action == HC_ACTION_NONE)
  405. return;
  406. action = host_control_action;
  407. host_control_action = HC_ACTION_NONE;
  408. if (!smi_buf.virt) {
  409. dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
  410. return;
  411. }
  412. if (smi_buf.size < sizeof(struct apm_cmd)) {
  413. dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
  414. __func__);
  415. return;
  416. }
  417. apm_cmd = (struct apm_cmd *)smi_buf.virt;
  418. /* power off takes precedence */
  419. if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
  420. apm_cmd->command = ESM_APM_POWER_CYCLE;
  421. apm_cmd->reserved = 0;
  422. *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
  423. host_control_smi();
  424. } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
  425. apm_cmd->command = ESM_APM_POWER_CYCLE;
  426. apm_cmd->reserved = 0;
  427. *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
  428. host_control_smi();
  429. }
  430. }
  431. /* WSMT */
  432. static u8 checksum(u8 *buffer, u8 length)
  433. {
  434. u8 sum = 0;
  435. u8 *end = buffer + length;
  436. while (buffer < end)
  437. sum += *buffer++;
  438. return sum;
  439. }
  440. static inline struct smm_eps_table *check_eps_table(u8 *addr)
  441. {
  442. struct smm_eps_table *eps = (struct smm_eps_table *)addr;
  443. if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0)
  444. return NULL;
  445. if (checksum(addr, eps->length) != 0)
  446. return NULL;
  447. return eps;
  448. }
  449. static int dcdbas_check_wsmt(void)
  450. {
  451. const struct dmi_device *dev = NULL;
  452. struct acpi_table_wsmt *wsmt = NULL;
  453. struct smm_eps_table *eps = NULL;
  454. u64 bios_buf_paddr;
  455. u64 remap_size;
  456. u8 *addr;
  457. acpi_get_table(ACPI_SIG_WSMT, 0, (struct acpi_table_header **)&wsmt);
  458. if (!wsmt)
  459. return 0;
  460. /* Check if WSMT ACPI table shows that protection is enabled */
  461. if (!(wsmt->protection_flags & ACPI_WSMT_FIXED_COMM_BUFFERS) ||
  462. !(wsmt->protection_flags & ACPI_WSMT_COMM_BUFFER_NESTED_PTR_PROTECTION))
  463. return 0;
  464. /*
  465. * BIOS could provide the address/size of the protected buffer
  466. * in an SMBIOS string or in an EPS structure in 0xFxxxx.
  467. */
  468. /* Check SMBIOS for buffer address */
  469. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev)))
  470. if (sscanf(dev->name, "30[%16llx;%8llx]", &bios_buf_paddr,
  471. &remap_size) == 2)
  472. goto remap;
  473. /* Scan for EPS (entry point structure) */
  474. for (addr = (u8 *)__va(0xf0000);
  475. addr < (u8 *)__va(0x100000 - sizeof(struct smm_eps_table));
  476. addr += 16) {
  477. eps = check_eps_table(addr);
  478. if (eps)
  479. break;
  480. }
  481. if (!eps) {
  482. dev_dbg(&dcdbas_pdev->dev, "found WSMT, but no firmware buffer found\n");
  483. return -ENODEV;
  484. }
  485. bios_buf_paddr = eps->smm_comm_buff_addr;
  486. remap_size = eps->num_of_4k_pages * PAGE_SIZE;
  487. remap:
  488. /*
  489. * Get physical address of buffer and map to virtual address.
  490. * Table gives size in 4K pages, regardless of actual system page size.
  491. */
  492. if (upper_32_bits(bios_buf_paddr + 8)) {
  493. dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer address is above 4GB\n");
  494. return -EINVAL;
  495. }
  496. /*
  497. * Limit remap size to MAX_SMI_DATA_BUF_SIZE + 8 (since the first 8
  498. * bytes are used for a semaphore, not the data buffer itself).
  499. */
  500. if (remap_size > MAX_SMI_DATA_BUF_SIZE + 8)
  501. remap_size = MAX_SMI_DATA_BUF_SIZE + 8;
  502. bios_buffer = memremap(bios_buf_paddr, remap_size, MEMREMAP_WB);
  503. if (!bios_buffer) {
  504. dev_warn(&dcdbas_pdev->dev, "found WSMT, but failed to map buffer\n");
  505. return -ENOMEM;
  506. }
  507. /* First 8 bytes is for a semaphore, not part of the smi_buf.virt */
  508. smi_buf.dma = bios_buf_paddr + 8;
  509. smi_buf.virt = bios_buffer + 8;
  510. smi_buf.size = remap_size - 8;
  511. max_smi_data_buf_size = smi_buf.size;
  512. wsmt_enabled = true;
  513. dev_info(&dcdbas_pdev->dev,
  514. "WSMT found, using firmware-provided SMI buffer.\n");
  515. return 1;
  516. }
  517. /**
  518. * dcdbas_reboot_notify: handle reboot notification for host control
  519. */
  520. static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
  521. void *unused)
  522. {
  523. switch (code) {
  524. case SYS_DOWN:
  525. case SYS_HALT:
  526. case SYS_POWER_OFF:
  527. if (host_control_on_shutdown) {
  528. /* firmware is going to perform host control action */
  529. printk(KERN_WARNING "Please wait for shutdown "
  530. "action to complete...\n");
  531. dcdbas_host_control();
  532. }
  533. break;
  534. }
  535. return NOTIFY_DONE;
  536. }
  537. static struct notifier_block dcdbas_reboot_nb = {
  538. .notifier_call = dcdbas_reboot_notify,
  539. .next = NULL,
  540. .priority = INT_MIN
  541. };
  542. static DCDBAS_BIN_ATTR_RW(smi_data);
  543. static struct bin_attribute *dcdbas_bin_attrs[] = {
  544. &bin_attr_smi_data,
  545. NULL
  546. };
  547. static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
  548. static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
  549. static DCDBAS_DEV_ATTR_WO(smi_request);
  550. static DCDBAS_DEV_ATTR_RW(host_control_action);
  551. static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
  552. static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
  553. static struct attribute *dcdbas_dev_attrs[] = {
  554. &dev_attr_smi_data_buf_size.attr,
  555. &dev_attr_smi_data_buf_phys_addr.attr,
  556. &dev_attr_smi_request.attr,
  557. &dev_attr_host_control_action.attr,
  558. &dev_attr_host_control_smi_type.attr,
  559. &dev_attr_host_control_on_shutdown.attr,
  560. NULL
  561. };
  562. static const struct attribute_group dcdbas_attr_group = {
  563. .attrs = dcdbas_dev_attrs,
  564. .bin_attrs = dcdbas_bin_attrs,
  565. };
  566. static int dcdbas_probe(struct platform_device *dev)
  567. {
  568. int error;
  569. host_control_action = HC_ACTION_NONE;
  570. host_control_smi_type = HC_SMITYPE_NONE;
  571. dcdbas_pdev = dev;
  572. /* Check if ACPI WSMT table specifies protected SMI buffer address */
  573. error = dcdbas_check_wsmt();
  574. if (error < 0)
  575. return error;
  576. /*
  577. * BIOS SMI calls require buffer addresses be in 32-bit address space.
  578. * This is done by setting the DMA mask below.
  579. */
  580. error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
  581. if (error)
  582. return error;
  583. error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
  584. if (error)
  585. return error;
  586. register_reboot_notifier(&dcdbas_reboot_nb);
  587. dev_info(&dev->dev, "%s (version %s)\n",
  588. DRIVER_DESCRIPTION, DRIVER_VERSION);
  589. return 0;
  590. }
  591. static int dcdbas_remove(struct platform_device *dev)
  592. {
  593. unregister_reboot_notifier(&dcdbas_reboot_nb);
  594. sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
  595. return 0;
  596. }
  597. static struct platform_driver dcdbas_driver = {
  598. .driver = {
  599. .name = DRIVER_NAME,
  600. },
  601. .probe = dcdbas_probe,
  602. .remove = dcdbas_remove,
  603. };
  604. static const struct platform_device_info dcdbas_dev_info __initconst = {
  605. .name = DRIVER_NAME,
  606. .id = PLATFORM_DEVID_NONE,
  607. .dma_mask = DMA_BIT_MASK(32),
  608. };
  609. static struct platform_device *dcdbas_pdev_reg;
  610. /**
  611. * dcdbas_init: initialize driver
  612. */
  613. static int __init dcdbas_init(void)
  614. {
  615. int error;
  616. error = platform_driver_register(&dcdbas_driver);
  617. if (error)
  618. return error;
  619. dcdbas_pdev_reg = platform_device_register_full(&dcdbas_dev_info);
  620. if (IS_ERR(dcdbas_pdev_reg)) {
  621. error = PTR_ERR(dcdbas_pdev_reg);
  622. goto err_unregister_driver;
  623. }
  624. return 0;
  625. err_unregister_driver:
  626. platform_driver_unregister(&dcdbas_driver);
  627. return error;
  628. }
  629. /**
  630. * dcdbas_exit: perform driver cleanup
  631. */
  632. static void __exit dcdbas_exit(void)
  633. {
  634. /*
  635. * make sure functions that use dcdbas_pdev are called
  636. * before platform_device_unregister
  637. */
  638. unregister_reboot_notifier(&dcdbas_reboot_nb);
  639. /*
  640. * We have to free the buffer here instead of dcdbas_remove
  641. * because only in module exit function we can be sure that
  642. * all sysfs attributes belonging to this module have been
  643. * released.
  644. */
  645. if (dcdbas_pdev)
  646. smi_data_buf_free();
  647. if (bios_buffer)
  648. memunmap(bios_buffer);
  649. platform_device_unregister(dcdbas_pdev_reg);
  650. platform_driver_unregister(&dcdbas_driver);
  651. }
  652. subsys_initcall_sync(dcdbas_init);
  653. module_exit(dcdbas_exit);
  654. MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
  655. MODULE_VERSION(DRIVER_VERSION);
  656. MODULE_AUTHOR("Dell Inc.");
  657. MODULE_LICENSE("GPL");
  658. /* Any System or BIOS claiming to be by Dell */
  659. MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");