ibmasr.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * IBM Automatic Server Restart driver.
  3. *
  4. * Copyright (c) 2005 Andrey Panin <pazke@donpac.ru>
  5. *
  6. * Based on driver written by Pete Reynolds.
  7. * Copyright (c) IBM Corporation, 1998-2004.
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU Public License, incorporated herein by reference.
  11. */
  12. #include <linux/fs.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/pci.h>
  16. #include <linux/timer.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/dmi.h>
  20. #include <linux/io.h>
  21. #include <linux/uaccess.h>
  22. enum {
  23. ASMTYPE_UNKNOWN,
  24. ASMTYPE_TOPAZ,
  25. ASMTYPE_JASPER,
  26. ASMTYPE_PEARL,
  27. ASMTYPE_JUNIPER,
  28. ASMTYPE_SPRUCE,
  29. };
  30. #define PFX "ibmasr: "
  31. #define TOPAZ_ASR_REG_OFFSET 4
  32. #define TOPAZ_ASR_TOGGLE 0x40
  33. #define TOPAZ_ASR_DISABLE 0x80
  34. /* PEARL ASR S/W REGISTER SUPERIO PORT ADDRESSES */
  35. #define PEARL_BASE 0xe04
  36. #define PEARL_WRITE 0xe06
  37. #define PEARL_READ 0xe07
  38. #define PEARL_ASR_DISABLE_MASK 0x80 /* bit 7: disable = 1, enable = 0 */
  39. #define PEARL_ASR_TOGGLE_MASK 0x40 /* bit 6: 0, then 1, then 0 */
  40. /* JASPER OFFSET FROM SIO BASE ADDR TO ASR S/W REGISTERS. */
  41. #define JASPER_ASR_REG_OFFSET 0x38
  42. #define JASPER_ASR_DISABLE_MASK 0x01 /* bit 0: disable = 1, enable = 0 */
  43. #define JASPER_ASR_TOGGLE_MASK 0x02 /* bit 1: 0, then 1, then 0 */
  44. #define JUNIPER_BASE_ADDRESS 0x54b /* Base address of Juniper ASR */
  45. #define JUNIPER_ASR_DISABLE_MASK 0x01 /* bit 0: disable = 1 enable = 0 */
  46. #define JUNIPER_ASR_TOGGLE_MASK 0x02 /* bit 1: 0, then 1, then 0 */
  47. #define SPRUCE_BASE_ADDRESS 0x118e /* Base address of Spruce ASR */
  48. #define SPRUCE_ASR_DISABLE_MASK 0x01 /* bit 1: disable = 1 enable = 0 */
  49. #define SPRUCE_ASR_TOGGLE_MASK 0x02 /* bit 0: 0, then 1, then 0 */
  50. static int nowayout = WATCHDOG_NOWAYOUT;
  51. static unsigned long asr_is_open;
  52. static char asr_expect_close;
  53. static unsigned int asr_type, asr_base, asr_length;
  54. static unsigned int asr_read_addr, asr_write_addr;
  55. static unsigned char asr_toggle_mask, asr_disable_mask;
  56. static spinlock_t asr_lock;
  57. static void __asr_toggle(void)
  58. {
  59. unsigned char reg;
  60. reg = inb(asr_read_addr);
  61. outb(reg & ~asr_toggle_mask, asr_write_addr);
  62. reg = inb(asr_read_addr);
  63. outb(reg | asr_toggle_mask, asr_write_addr);
  64. reg = inb(asr_read_addr);
  65. outb(reg & ~asr_toggle_mask, asr_write_addr);
  66. reg = inb(asr_read_addr);
  67. }
  68. static void asr_toggle(void)
  69. {
  70. spin_lock(&asr_lock);
  71. __asr_toggle();
  72. spin_unlock(&asr_lock);
  73. }
  74. static void asr_enable(void)
  75. {
  76. unsigned char reg;
  77. spin_lock(&asr_lock);
  78. if (asr_type == ASMTYPE_TOPAZ) {
  79. /* asr_write_addr == asr_read_addr */
  80. reg = inb(asr_read_addr);
  81. outb(reg & ~(TOPAZ_ASR_TOGGLE | TOPAZ_ASR_DISABLE),
  82. asr_read_addr);
  83. } else {
  84. /*
  85. * First make sure the hardware timer is reset by toggling
  86. * ASR hardware timer line.
  87. */
  88. __asr_toggle();
  89. reg = inb(asr_read_addr);
  90. outb(reg & ~asr_disable_mask, asr_write_addr);
  91. }
  92. reg = inb(asr_read_addr);
  93. spin_unlock(&asr_lock);
  94. }
  95. static void asr_disable(void)
  96. {
  97. unsigned char reg;
  98. spin_lock(&asr_lock);
  99. reg = inb(asr_read_addr);
  100. if (asr_type == ASMTYPE_TOPAZ)
  101. /* asr_write_addr == asr_read_addr */
  102. outb(reg | TOPAZ_ASR_TOGGLE | TOPAZ_ASR_DISABLE,
  103. asr_read_addr);
  104. else {
  105. outb(reg | asr_toggle_mask, asr_write_addr);
  106. reg = inb(asr_read_addr);
  107. outb(reg | asr_disable_mask, asr_write_addr);
  108. }
  109. reg = inb(asr_read_addr);
  110. spin_unlock(&asr_lock);
  111. }
  112. static int __init asr_get_base_address(void)
  113. {
  114. unsigned char low, high;
  115. const char *type = "";
  116. asr_length = 1;
  117. switch (asr_type) {
  118. case ASMTYPE_TOPAZ:
  119. /* SELECT SuperIO CHIP FOR QUERYING
  120. (WRITE 0x07 TO BOTH 0x2E and 0x2F) */
  121. outb(0x07, 0x2e);
  122. outb(0x07, 0x2f);
  123. /* SELECT AND READ THE HIGH-NIBBLE OF THE GPIO BASE ADDRESS */
  124. outb(0x60, 0x2e);
  125. high = inb(0x2f);
  126. /* SELECT AND READ THE LOW-NIBBLE OF THE GPIO BASE ADDRESS */
  127. outb(0x61, 0x2e);
  128. low = inb(0x2f);
  129. asr_base = (high << 16) | low;
  130. asr_read_addr = asr_write_addr =
  131. asr_base + TOPAZ_ASR_REG_OFFSET;
  132. asr_length = 5;
  133. break;
  134. case ASMTYPE_JASPER:
  135. type = "Jaspers ";
  136. #if 0
  137. u32 r;
  138. /* Suggested fix */
  139. pdev = pci_get_bus_and_slot(0, DEVFN(0x1f, 0));
  140. if (pdev == NULL)
  141. return -ENODEV;
  142. pci_read_config_dword(pdev, 0x58, &r);
  143. asr_base = r & 0xFFFE;
  144. pci_dev_put(pdev);
  145. #else
  146. /* FIXME: need to use pci_config_lock here,
  147. but it's not exported */
  148. /* spin_lock_irqsave(&pci_config_lock, flags);*/
  149. /* Select the SuperIO chip in the PCI I/O port register */
  150. outl(0x8000f858, 0xcf8);
  151. /* BUS 0, Slot 1F, fnc 0, offset 58 */
  152. /*
  153. * Read the base address for the SuperIO chip.
  154. * Only the lower 16 bits are valid, but the address is word
  155. * aligned so the last bit must be masked off.
  156. */
  157. asr_base = inl(0xcfc) & 0xfffe;
  158. /* spin_unlock_irqrestore(&pci_config_lock, flags);*/
  159. #endif
  160. asr_read_addr = asr_write_addr =
  161. asr_base + JASPER_ASR_REG_OFFSET;
  162. asr_toggle_mask = JASPER_ASR_TOGGLE_MASK;
  163. asr_disable_mask = JASPER_ASR_DISABLE_MASK;
  164. asr_length = JASPER_ASR_REG_OFFSET + 1;
  165. break;
  166. case ASMTYPE_PEARL:
  167. type = "Pearls ";
  168. asr_base = PEARL_BASE;
  169. asr_read_addr = PEARL_READ;
  170. asr_write_addr = PEARL_WRITE;
  171. asr_toggle_mask = PEARL_ASR_TOGGLE_MASK;
  172. asr_disable_mask = PEARL_ASR_DISABLE_MASK;
  173. asr_length = 4;
  174. break;
  175. case ASMTYPE_JUNIPER:
  176. type = "Junipers ";
  177. asr_base = JUNIPER_BASE_ADDRESS;
  178. asr_read_addr = asr_write_addr = asr_base;
  179. asr_toggle_mask = JUNIPER_ASR_TOGGLE_MASK;
  180. asr_disable_mask = JUNIPER_ASR_DISABLE_MASK;
  181. break;
  182. case ASMTYPE_SPRUCE:
  183. type = "Spruce's ";
  184. asr_base = SPRUCE_BASE_ADDRESS;
  185. asr_read_addr = asr_write_addr = asr_base;
  186. asr_toggle_mask = SPRUCE_ASR_TOGGLE_MASK;
  187. asr_disable_mask = SPRUCE_ASR_DISABLE_MASK;
  188. break;
  189. }
  190. if (!request_region(asr_base, asr_length, "ibmasr")) {
  191. printk(KERN_ERR PFX "address %#x already in use\n",
  192. asr_base);
  193. return -EBUSY;
  194. }
  195. printk(KERN_INFO PFX "found %sASR @ addr %#x\n", type, asr_base);
  196. return 0;
  197. }
  198. static ssize_t asr_write(struct file *file, const char __user *buf,
  199. size_t count, loff_t *ppos)
  200. {
  201. if (count) {
  202. if (!nowayout) {
  203. size_t i;
  204. /* In case it was set long ago */
  205. asr_expect_close = 0;
  206. for (i = 0; i != count; i++) {
  207. char c;
  208. if (get_user(c, buf + i))
  209. return -EFAULT;
  210. if (c == 'V')
  211. asr_expect_close = 42;
  212. }
  213. }
  214. asr_toggle();
  215. }
  216. return count;
  217. }
  218. static long asr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  219. {
  220. static const struct watchdog_info ident = {
  221. .options = WDIOF_KEEPALIVEPING |
  222. WDIOF_MAGICCLOSE,
  223. .identity = "IBM ASR",
  224. };
  225. void __user *argp = (void __user *)arg;
  226. int __user *p = argp;
  227. int heartbeat;
  228. switch (cmd) {
  229. case WDIOC_GETSUPPORT:
  230. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  231. case WDIOC_GETSTATUS:
  232. case WDIOC_GETBOOTSTATUS:
  233. return put_user(0, p);
  234. case WDIOC_SETOPTIONS:
  235. {
  236. int new_options, retval = -EINVAL;
  237. if (get_user(new_options, p))
  238. return -EFAULT;
  239. if (new_options & WDIOS_DISABLECARD) {
  240. asr_disable();
  241. retval = 0;
  242. }
  243. if (new_options & WDIOS_ENABLECARD) {
  244. asr_enable();
  245. asr_toggle();
  246. retval = 0;
  247. }
  248. return retval;
  249. }
  250. case WDIOC_KEEPALIVE:
  251. asr_toggle();
  252. return 0;
  253. /*
  254. * The hardware has a fixed timeout value, so no WDIOC_SETTIMEOUT
  255. * and WDIOC_GETTIMEOUT always returns 256.
  256. */
  257. case WDIOC_GETTIMEOUT:
  258. heartbeat = 256;
  259. return put_user(heartbeat, p);
  260. default:
  261. return -ENOTTY;
  262. }
  263. }
  264. static int asr_open(struct inode *inode, struct file *file)
  265. {
  266. if (test_and_set_bit(0, &asr_is_open))
  267. return -EBUSY;
  268. asr_toggle();
  269. asr_enable();
  270. return nonseekable_open(inode, file);
  271. }
  272. static int asr_release(struct inode *inode, struct file *file)
  273. {
  274. if (asr_expect_close == 42)
  275. asr_disable();
  276. else {
  277. printk(KERN_CRIT PFX
  278. "unexpected close, not stopping watchdog!\n");
  279. asr_toggle();
  280. }
  281. clear_bit(0, &asr_is_open);
  282. asr_expect_close = 0;
  283. return 0;
  284. }
  285. static const struct file_operations asr_fops = {
  286. .owner = THIS_MODULE,
  287. .llseek = no_llseek,
  288. .write = asr_write,
  289. .unlocked_ioctl = asr_ioctl,
  290. .open = asr_open,
  291. .release = asr_release,
  292. };
  293. static struct miscdevice asr_miscdev = {
  294. .minor = WATCHDOG_MINOR,
  295. .name = "watchdog",
  296. .fops = &asr_fops,
  297. };
  298. struct ibmasr_id {
  299. const char *desc;
  300. int type;
  301. };
  302. static struct ibmasr_id __initdata ibmasr_id_table[] = {
  303. { "IBM Automatic Server Restart - eserver xSeries 220", ASMTYPE_TOPAZ },
  304. { "IBM Automatic Server Restart - Machine Type 8673", ASMTYPE_PEARL },
  305. { "IBM Automatic Server Restart - Machine Type 8480", ASMTYPE_JASPER },
  306. { "IBM Automatic Server Restart - Machine Type 8482", ASMTYPE_JUNIPER },
  307. { "IBM Automatic Server Restart - Machine Type 8648", ASMTYPE_SPRUCE },
  308. { NULL }
  309. };
  310. static int __init ibmasr_init(void)
  311. {
  312. struct ibmasr_id *id;
  313. int rc;
  314. for (id = ibmasr_id_table; id->desc; id++) {
  315. if (dmi_find_device(DMI_DEV_TYPE_OTHER, id->desc, NULL)) {
  316. asr_type = id->type;
  317. break;
  318. }
  319. }
  320. if (!asr_type)
  321. return -ENODEV;
  322. spin_lock_init(&asr_lock);
  323. rc = asr_get_base_address();
  324. if (rc)
  325. return rc;
  326. rc = misc_register(&asr_miscdev);
  327. if (rc < 0) {
  328. release_region(asr_base, asr_length);
  329. printk(KERN_ERR PFX "failed to register misc device\n");
  330. return rc;
  331. }
  332. return 0;
  333. }
  334. static void __exit ibmasr_exit(void)
  335. {
  336. if (!nowayout)
  337. asr_disable();
  338. misc_deregister(&asr_miscdev);
  339. release_region(asr_base, asr_length);
  340. }
  341. module_init(ibmasr_init);
  342. module_exit(ibmasr_exit);
  343. module_param(nowayout, int, 0);
  344. MODULE_PARM_DESC(nowayout,
  345. "Watchdog cannot be stopped once started (default="
  346. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  347. MODULE_DESCRIPTION("IBM Automatic Server Restart driver");
  348. MODULE_AUTHOR("Andrey Panin");
  349. MODULE_LICENSE("GPL");
  350. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);