ibmasmfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * IBM ASM Service Processor Device Driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2004
  19. *
  20. * Author: Max Asböck <amax@us.ibm.com>
  21. *
  22. */
  23. /*
  24. * Parts of this code are based on an article by Jonathan Corbet
  25. * that appeared in Linux Weekly News.
  26. */
  27. /*
  28. * The IBMASM file virtual filesystem. It creates the following hierarchy
  29. * dynamically when mounted from user space:
  30. *
  31. * /ibmasm
  32. * |-- 0
  33. * | |-- command
  34. * | |-- event
  35. * | |-- reverse_heartbeat
  36. * | `-- remote_video
  37. * | |-- depth
  38. * | |-- height
  39. * | `-- width
  40. * .
  41. * .
  42. * .
  43. * `-- n
  44. * |-- command
  45. * |-- event
  46. * |-- reverse_heartbeat
  47. * `-- remote_video
  48. * |-- depth
  49. * |-- height
  50. * `-- width
  51. *
  52. * For each service processor the following files are created:
  53. *
  54. * command: execute dot commands
  55. * write: execute a dot command on the service processor
  56. * read: return the result of a previously executed dot command
  57. *
  58. * events: listen for service processor events
  59. * read: sleep (interruptible) until an event occurs
  60. * write: wakeup sleeping event listener
  61. *
  62. * reverse_heartbeat: send a heartbeat to the service processor
  63. * read: sleep (interruptible) until the reverse heartbeat fails
  64. * write: wakeup sleeping heartbeat listener
  65. *
  66. * remote_video/width
  67. * remote_video/height
  68. * remote_video/width: control remote display settings
  69. * write: set value
  70. * read: read value
  71. */
  72. #include <linux/fs.h>
  73. #include <linux/pagemap.h>
  74. #include <linux/slab.h>
  75. #include <asm/uaccess.h>
  76. #include <asm/io.h>
  77. #include "ibmasm.h"
  78. #include "remote.h"
  79. #include "dot_command.h"
  80. #define IBMASMFS_MAGIC 0x66726f67
  81. static LIST_HEAD(service_processors);
  82. static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode);
  83. static void ibmasmfs_create_files (struct super_block *sb);
  84. static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent);
  85. static struct dentry *ibmasmfs_mount(struct file_system_type *fst,
  86. int flags, const char *name, void *data)
  87. {
  88. return mount_single(fst, flags, data, ibmasmfs_fill_super);
  89. }
  90. static const struct super_operations ibmasmfs_s_ops = {
  91. .statfs = simple_statfs,
  92. .drop_inode = generic_delete_inode,
  93. };
  94. static const struct file_operations *ibmasmfs_dir_ops = &simple_dir_operations;
  95. static struct file_system_type ibmasmfs_type = {
  96. .owner = THIS_MODULE,
  97. .name = "ibmasmfs",
  98. .mount = ibmasmfs_mount,
  99. .kill_sb = kill_litter_super,
  100. };
  101. MODULE_ALIAS_FS("ibmasmfs");
  102. static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent)
  103. {
  104. struct inode *root;
  105. sb->s_blocksize = PAGE_CACHE_SIZE;
  106. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  107. sb->s_magic = IBMASMFS_MAGIC;
  108. sb->s_op = &ibmasmfs_s_ops;
  109. sb->s_time_gran = 1;
  110. root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
  111. if (!root)
  112. return -ENOMEM;
  113. root->i_op = &simple_dir_inode_operations;
  114. root->i_fop = ibmasmfs_dir_ops;
  115. sb->s_root = d_make_root(root);
  116. if (!sb->s_root)
  117. return -ENOMEM;
  118. ibmasmfs_create_files(sb);
  119. return 0;
  120. }
  121. static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
  122. {
  123. struct inode *ret = new_inode(sb);
  124. if (ret) {
  125. ret->i_ino = get_next_ino();
  126. ret->i_mode = mode;
  127. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  128. }
  129. return ret;
  130. }
  131. static struct dentry *ibmasmfs_create_file (struct super_block *sb,
  132. struct dentry *parent,
  133. const char *name,
  134. const struct file_operations *fops,
  135. void *data,
  136. int mode)
  137. {
  138. struct dentry *dentry;
  139. struct inode *inode;
  140. dentry = d_alloc_name(parent, name);
  141. if (!dentry)
  142. return NULL;
  143. inode = ibmasmfs_make_inode(sb, S_IFREG | mode);
  144. if (!inode) {
  145. dput(dentry);
  146. return NULL;
  147. }
  148. inode->i_fop = fops;
  149. inode->i_private = data;
  150. d_add(dentry, inode);
  151. return dentry;
  152. }
  153. static struct dentry *ibmasmfs_create_dir (struct super_block *sb,
  154. struct dentry *parent,
  155. const char *name)
  156. {
  157. struct dentry *dentry;
  158. struct inode *inode;
  159. dentry = d_alloc_name(parent, name);
  160. if (!dentry)
  161. return NULL;
  162. inode = ibmasmfs_make_inode(sb, S_IFDIR | 0500);
  163. if (!inode) {
  164. dput(dentry);
  165. return NULL;
  166. }
  167. inode->i_op = &simple_dir_inode_operations;
  168. inode->i_fop = ibmasmfs_dir_ops;
  169. d_add(dentry, inode);
  170. return dentry;
  171. }
  172. int ibmasmfs_register(void)
  173. {
  174. return register_filesystem(&ibmasmfs_type);
  175. }
  176. void ibmasmfs_unregister(void)
  177. {
  178. unregister_filesystem(&ibmasmfs_type);
  179. }
  180. void ibmasmfs_add_sp(struct service_processor *sp)
  181. {
  182. list_add(&sp->node, &service_processors);
  183. }
  184. /* struct to save state between command file operations */
  185. struct ibmasmfs_command_data {
  186. struct service_processor *sp;
  187. struct command *command;
  188. };
  189. /* struct to save state between event file operations */
  190. struct ibmasmfs_event_data {
  191. struct service_processor *sp;
  192. struct event_reader reader;
  193. int active;
  194. };
  195. /* struct to save state between reverse heartbeat file operations */
  196. struct ibmasmfs_heartbeat_data {
  197. struct service_processor *sp;
  198. struct reverse_heartbeat heartbeat;
  199. int active;
  200. };
  201. static int command_file_open(struct inode *inode, struct file *file)
  202. {
  203. struct ibmasmfs_command_data *command_data;
  204. if (!inode->i_private)
  205. return -ENODEV;
  206. command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
  207. if (!command_data)
  208. return -ENOMEM;
  209. command_data->command = NULL;
  210. command_data->sp = inode->i_private;
  211. file->private_data = command_data;
  212. return 0;
  213. }
  214. static int command_file_close(struct inode *inode, struct file *file)
  215. {
  216. struct ibmasmfs_command_data *command_data = file->private_data;
  217. if (command_data->command)
  218. command_put(command_data->command);
  219. kfree(command_data);
  220. return 0;
  221. }
  222. static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  223. {
  224. struct ibmasmfs_command_data *command_data = file->private_data;
  225. struct command *cmd;
  226. int len;
  227. unsigned long flags;
  228. if (*offset < 0)
  229. return -EINVAL;
  230. if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
  231. return 0;
  232. if (*offset != 0)
  233. return 0;
  234. spin_lock_irqsave(&command_data->sp->lock, flags);
  235. cmd = command_data->command;
  236. if (cmd == NULL) {
  237. spin_unlock_irqrestore(&command_data->sp->lock, flags);
  238. return 0;
  239. }
  240. command_data->command = NULL;
  241. spin_unlock_irqrestore(&command_data->sp->lock, flags);
  242. if (cmd->status != IBMASM_CMD_COMPLETE) {
  243. command_put(cmd);
  244. return -EIO;
  245. }
  246. len = min(count, cmd->buffer_size);
  247. if (copy_to_user(buf, cmd->buffer, len)) {
  248. command_put(cmd);
  249. return -EFAULT;
  250. }
  251. command_put(cmd);
  252. return len;
  253. }
  254. static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
  255. {
  256. struct ibmasmfs_command_data *command_data = file->private_data;
  257. struct command *cmd;
  258. unsigned long flags;
  259. if (*offset < 0)
  260. return -EINVAL;
  261. if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
  262. return 0;
  263. if (*offset != 0)
  264. return 0;
  265. /* commands are executed sequentially, only one command at a time */
  266. if (command_data->command)
  267. return -EAGAIN;
  268. cmd = ibmasm_new_command(command_data->sp, count);
  269. if (!cmd)
  270. return -ENOMEM;
  271. if (copy_from_user(cmd->buffer, ubuff, count)) {
  272. command_put(cmd);
  273. return -EFAULT;
  274. }
  275. spin_lock_irqsave(&command_data->sp->lock, flags);
  276. if (command_data->command) {
  277. spin_unlock_irqrestore(&command_data->sp->lock, flags);
  278. command_put(cmd);
  279. return -EAGAIN;
  280. }
  281. command_data->command = cmd;
  282. spin_unlock_irqrestore(&command_data->sp->lock, flags);
  283. ibmasm_exec_command(command_data->sp, cmd);
  284. ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
  285. return count;
  286. }
  287. static int event_file_open(struct inode *inode, struct file *file)
  288. {
  289. struct ibmasmfs_event_data *event_data;
  290. struct service_processor *sp;
  291. if (!inode->i_private)
  292. return -ENODEV;
  293. sp = inode->i_private;
  294. event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
  295. if (!event_data)
  296. return -ENOMEM;
  297. ibmasm_event_reader_register(sp, &event_data->reader);
  298. event_data->sp = sp;
  299. event_data->active = 0;
  300. file->private_data = event_data;
  301. return 0;
  302. }
  303. static int event_file_close(struct inode *inode, struct file *file)
  304. {
  305. struct ibmasmfs_event_data *event_data = file->private_data;
  306. ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
  307. kfree(event_data);
  308. return 0;
  309. }
  310. static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  311. {
  312. struct ibmasmfs_event_data *event_data = file->private_data;
  313. struct event_reader *reader = &event_data->reader;
  314. struct service_processor *sp = event_data->sp;
  315. int ret;
  316. unsigned long flags;
  317. if (*offset < 0)
  318. return -EINVAL;
  319. if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
  320. return 0;
  321. if (*offset != 0)
  322. return 0;
  323. spin_lock_irqsave(&sp->lock, flags);
  324. if (event_data->active) {
  325. spin_unlock_irqrestore(&sp->lock, flags);
  326. return -EBUSY;
  327. }
  328. event_data->active = 1;
  329. spin_unlock_irqrestore(&sp->lock, flags);
  330. ret = ibmasm_get_next_event(sp, reader);
  331. if (ret <= 0)
  332. goto out;
  333. if (count < reader->data_size) {
  334. ret = -EINVAL;
  335. goto out;
  336. }
  337. if (copy_to_user(buf, reader->data, reader->data_size)) {
  338. ret = -EFAULT;
  339. goto out;
  340. }
  341. ret = reader->data_size;
  342. out:
  343. event_data->active = 0;
  344. return ret;
  345. }
  346. static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  347. {
  348. struct ibmasmfs_event_data *event_data = file->private_data;
  349. if (*offset < 0)
  350. return -EINVAL;
  351. if (count != 1)
  352. return 0;
  353. if (*offset != 0)
  354. return 0;
  355. ibmasm_cancel_next_event(&event_data->reader);
  356. return 0;
  357. }
  358. static int r_heartbeat_file_open(struct inode *inode, struct file *file)
  359. {
  360. struct ibmasmfs_heartbeat_data *rhbeat;
  361. if (!inode->i_private)
  362. return -ENODEV;
  363. rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
  364. if (!rhbeat)
  365. return -ENOMEM;
  366. rhbeat->sp = inode->i_private;
  367. rhbeat->active = 0;
  368. ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
  369. file->private_data = rhbeat;
  370. return 0;
  371. }
  372. static int r_heartbeat_file_close(struct inode *inode, struct file *file)
  373. {
  374. struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
  375. kfree(rhbeat);
  376. return 0;
  377. }
  378. static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  379. {
  380. struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
  381. unsigned long flags;
  382. int result;
  383. if (*offset < 0)
  384. return -EINVAL;
  385. if (count == 0 || count > 1024)
  386. return 0;
  387. if (*offset != 0)
  388. return 0;
  389. /* allow only one reverse heartbeat per process */
  390. spin_lock_irqsave(&rhbeat->sp->lock, flags);
  391. if (rhbeat->active) {
  392. spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
  393. return -EBUSY;
  394. }
  395. rhbeat->active = 1;
  396. spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
  397. result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
  398. rhbeat->active = 0;
  399. return result;
  400. }
  401. static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  402. {
  403. struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
  404. if (*offset < 0)
  405. return -EINVAL;
  406. if (count != 1)
  407. return 0;
  408. if (*offset != 0)
  409. return 0;
  410. if (rhbeat->active)
  411. ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
  412. return 1;
  413. }
  414. static int remote_settings_file_close(struct inode *inode, struct file *file)
  415. {
  416. return 0;
  417. }
  418. static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  419. {
  420. void __iomem *address = (void __iomem *)file->private_data;
  421. unsigned char *page;
  422. int retval;
  423. int len = 0;
  424. unsigned int value;
  425. if (*offset < 0)
  426. return -EINVAL;
  427. if (count == 0 || count > 1024)
  428. return 0;
  429. if (*offset != 0)
  430. return 0;
  431. page = (unsigned char *)__get_free_page(GFP_KERNEL);
  432. if (!page)
  433. return -ENOMEM;
  434. value = readl(address);
  435. len = sprintf(page, "%d\n", value);
  436. if (copy_to_user(buf, page, len)) {
  437. retval = -EFAULT;
  438. goto exit;
  439. }
  440. *offset += len;
  441. retval = len;
  442. exit:
  443. free_page((unsigned long)page);
  444. return retval;
  445. }
  446. static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
  447. {
  448. void __iomem *address = (void __iomem *)file->private_data;
  449. char *buff;
  450. unsigned int value;
  451. if (*offset < 0)
  452. return -EINVAL;
  453. if (count == 0 || count > 1024)
  454. return 0;
  455. if (*offset != 0)
  456. return 0;
  457. buff = kzalloc (count + 1, GFP_KERNEL);
  458. if (!buff)
  459. return -ENOMEM;
  460. if (copy_from_user(buff, ubuff, count)) {
  461. kfree(buff);
  462. return -EFAULT;
  463. }
  464. value = simple_strtoul(buff, NULL, 10);
  465. writel(value, address);
  466. kfree(buff);
  467. return count;
  468. }
  469. static const struct file_operations command_fops = {
  470. .open = command_file_open,
  471. .release = command_file_close,
  472. .read = command_file_read,
  473. .write = command_file_write,
  474. .llseek = generic_file_llseek,
  475. };
  476. static const struct file_operations event_fops = {
  477. .open = event_file_open,
  478. .release = event_file_close,
  479. .read = event_file_read,
  480. .write = event_file_write,
  481. .llseek = generic_file_llseek,
  482. };
  483. static const struct file_operations r_heartbeat_fops = {
  484. .open = r_heartbeat_file_open,
  485. .release = r_heartbeat_file_close,
  486. .read = r_heartbeat_file_read,
  487. .write = r_heartbeat_file_write,
  488. .llseek = generic_file_llseek,
  489. };
  490. static const struct file_operations remote_settings_fops = {
  491. .open = simple_open,
  492. .release = remote_settings_file_close,
  493. .read = remote_settings_file_read,
  494. .write = remote_settings_file_write,
  495. .llseek = generic_file_llseek,
  496. };
  497. static void ibmasmfs_create_files (struct super_block *sb)
  498. {
  499. struct list_head *entry;
  500. struct service_processor *sp;
  501. list_for_each(entry, &service_processors) {
  502. struct dentry *dir;
  503. struct dentry *remote_dir;
  504. sp = list_entry(entry, struct service_processor, node);
  505. dir = ibmasmfs_create_dir(sb, sb->s_root, sp->dirname);
  506. if (!dir)
  507. continue;
  508. ibmasmfs_create_file(sb, dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
  509. ibmasmfs_create_file(sb, dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
  510. ibmasmfs_create_file(sb, dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
  511. remote_dir = ibmasmfs_create_dir(sb, dir, "remote_video");
  512. if (!remote_dir)
  513. continue;
  514. ibmasmfs_create_file(sb, remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
  515. ibmasmfs_create_file(sb, remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
  516. ibmasmfs_create_file(sb, remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);
  517. }
  518. }