ibmasmfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent)
  102. {
  103. struct inode *root;
  104. sb->s_blocksize = PAGE_CACHE_SIZE;
  105. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  106. sb->s_magic = IBMASMFS_MAGIC;
  107. sb->s_op = &ibmasmfs_s_ops;
  108. sb->s_time_gran = 1;
  109. root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
  110. if (!root)
  111. return -ENOMEM;
  112. root->i_op = &simple_dir_inode_operations;
  113. root->i_fop = ibmasmfs_dir_ops;
  114. sb->s_root = d_make_root(root);
  115. if (!sb->s_root)
  116. return -ENOMEM;
  117. ibmasmfs_create_files(sb);
  118. return 0;
  119. }
  120. static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
  121. {
  122. struct inode *ret = new_inode(sb);
  123. if (ret) {
  124. ret->i_ino = get_next_ino();
  125. ret->i_mode = mode;
  126. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  127. }
  128. return ret;
  129. }
  130. static struct dentry *ibmasmfs_create_file (struct super_block *sb,
  131. struct dentry *parent,
  132. const char *name,
  133. const struct file_operations *fops,
  134. void *data,
  135. int mode)
  136. {
  137. struct dentry *dentry;
  138. struct inode *inode;
  139. dentry = d_alloc_name(parent, name);
  140. if (!dentry)
  141. return NULL;
  142. inode = ibmasmfs_make_inode(sb, S_IFREG | mode);
  143. if (!inode) {
  144. dput(dentry);
  145. return NULL;
  146. }
  147. inode->i_fop = fops;
  148. inode->i_private = data;
  149. d_add(dentry, inode);
  150. return dentry;
  151. }
  152. static struct dentry *ibmasmfs_create_dir (struct super_block *sb,
  153. struct dentry *parent,
  154. const char *name)
  155. {
  156. struct dentry *dentry;
  157. struct inode *inode;
  158. dentry = d_alloc_name(parent, name);
  159. if (!dentry)
  160. return NULL;
  161. inode = ibmasmfs_make_inode(sb, S_IFDIR | 0500);
  162. if (!inode) {
  163. dput(dentry);
  164. return NULL;
  165. }
  166. inode->i_op = &simple_dir_inode_operations;
  167. inode->i_fop = ibmasmfs_dir_ops;
  168. d_add(dentry, inode);
  169. return dentry;
  170. }
  171. int ibmasmfs_register(void)
  172. {
  173. return register_filesystem(&ibmasmfs_type);
  174. }
  175. void ibmasmfs_unregister(void)
  176. {
  177. unregister_filesystem(&ibmasmfs_type);
  178. }
  179. void ibmasmfs_add_sp(struct service_processor *sp)
  180. {
  181. list_add(&sp->node, &service_processors);
  182. }
  183. /* struct to save state between command file operations */
  184. struct ibmasmfs_command_data {
  185. struct service_processor *sp;
  186. struct command *command;
  187. };
  188. /* struct to save state between event file operations */
  189. struct ibmasmfs_event_data {
  190. struct service_processor *sp;
  191. struct event_reader reader;
  192. int active;
  193. };
  194. /* struct to save state between reverse heartbeat file operations */
  195. struct ibmasmfs_heartbeat_data {
  196. struct service_processor *sp;
  197. struct reverse_heartbeat heartbeat;
  198. int active;
  199. };
  200. static int command_file_open(struct inode *inode, struct file *file)
  201. {
  202. struct ibmasmfs_command_data *command_data;
  203. if (!inode->i_private)
  204. return -ENODEV;
  205. command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
  206. if (!command_data)
  207. return -ENOMEM;
  208. command_data->command = NULL;
  209. command_data->sp = inode->i_private;
  210. file->private_data = command_data;
  211. return 0;
  212. }
  213. static int command_file_close(struct inode *inode, struct file *file)
  214. {
  215. struct ibmasmfs_command_data *command_data = file->private_data;
  216. if (command_data->command)
  217. command_put(command_data->command);
  218. kfree(command_data);
  219. return 0;
  220. }
  221. static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  222. {
  223. struct ibmasmfs_command_data *command_data = file->private_data;
  224. struct command *cmd;
  225. int len;
  226. unsigned long flags;
  227. if (*offset < 0)
  228. return -EINVAL;
  229. if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
  230. return 0;
  231. if (*offset != 0)
  232. return 0;
  233. spin_lock_irqsave(&command_data->sp->lock, flags);
  234. cmd = command_data->command;
  235. if (cmd == NULL) {
  236. spin_unlock_irqrestore(&command_data->sp->lock, flags);
  237. return 0;
  238. }
  239. command_data->command = NULL;
  240. spin_unlock_irqrestore(&command_data->sp->lock, flags);
  241. if (cmd->status != IBMASM_CMD_COMPLETE) {
  242. command_put(cmd);
  243. return -EIO;
  244. }
  245. len = min(count, cmd->buffer_size);
  246. if (copy_to_user(buf, cmd->buffer, len)) {
  247. command_put(cmd);
  248. return -EFAULT;
  249. }
  250. command_put(cmd);
  251. return len;
  252. }
  253. static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
  254. {
  255. struct ibmasmfs_command_data *command_data = file->private_data;
  256. struct command *cmd;
  257. unsigned long flags;
  258. if (*offset < 0)
  259. return -EINVAL;
  260. if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
  261. return 0;
  262. if (*offset != 0)
  263. return 0;
  264. /* commands are executed sequentially, only one command at a time */
  265. if (command_data->command)
  266. return -EAGAIN;
  267. cmd = ibmasm_new_command(command_data->sp, count);
  268. if (!cmd)
  269. return -ENOMEM;
  270. if (copy_from_user(cmd->buffer, ubuff, count)) {
  271. command_put(cmd);
  272. return -EFAULT;
  273. }
  274. spin_lock_irqsave(&command_data->sp->lock, flags);
  275. if (command_data->command) {
  276. spin_unlock_irqrestore(&command_data->sp->lock, flags);
  277. command_put(cmd);
  278. return -EAGAIN;
  279. }
  280. command_data->command = cmd;
  281. spin_unlock_irqrestore(&command_data->sp->lock, flags);
  282. ibmasm_exec_command(command_data->sp, cmd);
  283. ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
  284. return count;
  285. }
  286. static int event_file_open(struct inode *inode, struct file *file)
  287. {
  288. struct ibmasmfs_event_data *event_data;
  289. struct service_processor *sp;
  290. if (!inode->i_private)
  291. return -ENODEV;
  292. sp = inode->i_private;
  293. event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
  294. if (!event_data)
  295. return -ENOMEM;
  296. ibmasm_event_reader_register(sp, &event_data->reader);
  297. event_data->sp = sp;
  298. event_data->active = 0;
  299. file->private_data = event_data;
  300. return 0;
  301. }
  302. static int event_file_close(struct inode *inode, struct file *file)
  303. {
  304. struct ibmasmfs_event_data *event_data = file->private_data;
  305. ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
  306. kfree(event_data);
  307. return 0;
  308. }
  309. static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  310. {
  311. struct ibmasmfs_event_data *event_data = file->private_data;
  312. struct event_reader *reader = &event_data->reader;
  313. struct service_processor *sp = event_data->sp;
  314. int ret;
  315. unsigned long flags;
  316. if (*offset < 0)
  317. return -EINVAL;
  318. if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
  319. return 0;
  320. if (*offset != 0)
  321. return 0;
  322. spin_lock_irqsave(&sp->lock, flags);
  323. if (event_data->active) {
  324. spin_unlock_irqrestore(&sp->lock, flags);
  325. return -EBUSY;
  326. }
  327. event_data->active = 1;
  328. spin_unlock_irqrestore(&sp->lock, flags);
  329. ret = ibmasm_get_next_event(sp, reader);
  330. if (ret <= 0)
  331. goto out;
  332. if (count < reader->data_size) {
  333. ret = -EINVAL;
  334. goto out;
  335. }
  336. if (copy_to_user(buf, reader->data, reader->data_size)) {
  337. ret = -EFAULT;
  338. goto out;
  339. }
  340. ret = reader->data_size;
  341. out:
  342. event_data->active = 0;
  343. return ret;
  344. }
  345. static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  346. {
  347. struct ibmasmfs_event_data *event_data = file->private_data;
  348. if (*offset < 0)
  349. return -EINVAL;
  350. if (count != 1)
  351. return 0;
  352. if (*offset != 0)
  353. return 0;
  354. ibmasm_cancel_next_event(&event_data->reader);
  355. return 0;
  356. }
  357. static int r_heartbeat_file_open(struct inode *inode, struct file *file)
  358. {
  359. struct ibmasmfs_heartbeat_data *rhbeat;
  360. if (!inode->i_private)
  361. return -ENODEV;
  362. rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
  363. if (!rhbeat)
  364. return -ENOMEM;
  365. rhbeat->sp = inode->i_private;
  366. rhbeat->active = 0;
  367. ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
  368. file->private_data = rhbeat;
  369. return 0;
  370. }
  371. static int r_heartbeat_file_close(struct inode *inode, struct file *file)
  372. {
  373. struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
  374. kfree(rhbeat);
  375. return 0;
  376. }
  377. static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  378. {
  379. struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
  380. unsigned long flags;
  381. int result;
  382. if (*offset < 0)
  383. return -EINVAL;
  384. if (count == 0 || count > 1024)
  385. return 0;
  386. if (*offset != 0)
  387. return 0;
  388. /* allow only one reverse heartbeat per process */
  389. spin_lock_irqsave(&rhbeat->sp->lock, flags);
  390. if (rhbeat->active) {
  391. spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
  392. return -EBUSY;
  393. }
  394. rhbeat->active = 1;
  395. spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
  396. result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
  397. rhbeat->active = 0;
  398. return result;
  399. }
  400. static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  401. {
  402. struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
  403. if (*offset < 0)
  404. return -EINVAL;
  405. if (count != 1)
  406. return 0;
  407. if (*offset != 0)
  408. return 0;
  409. if (rhbeat->active)
  410. ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
  411. return 1;
  412. }
  413. static int remote_settings_file_close(struct inode *inode, struct file *file)
  414. {
  415. return 0;
  416. }
  417. static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  418. {
  419. void __iomem *address = (void __iomem *)file->private_data;
  420. unsigned char *page;
  421. int retval;
  422. int len = 0;
  423. unsigned int value;
  424. if (*offset < 0)
  425. return -EINVAL;
  426. if (count == 0 || count > 1024)
  427. return 0;
  428. if (*offset != 0)
  429. return 0;
  430. page = (unsigned char *)__get_free_page(GFP_KERNEL);
  431. if (!page)
  432. return -ENOMEM;
  433. value = readl(address);
  434. len = sprintf(page, "%d\n", value);
  435. if (copy_to_user(buf, page, len)) {
  436. retval = -EFAULT;
  437. goto exit;
  438. }
  439. *offset += len;
  440. retval = len;
  441. exit:
  442. free_page((unsigned long)page);
  443. return retval;
  444. }
  445. static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
  446. {
  447. void __iomem *address = (void __iomem *)file->private_data;
  448. char *buff;
  449. unsigned int value;
  450. if (*offset < 0)
  451. return -EINVAL;
  452. if (count == 0 || count > 1024)
  453. return 0;
  454. if (*offset != 0)
  455. return 0;
  456. buff = kzalloc (count + 1, GFP_KERNEL);
  457. if (!buff)
  458. return -ENOMEM;
  459. if (copy_from_user(buff, ubuff, count)) {
  460. kfree(buff);
  461. return -EFAULT;
  462. }
  463. value = simple_strtoul(buff, NULL, 10);
  464. writel(value, address);
  465. kfree(buff);
  466. return count;
  467. }
  468. static const struct file_operations command_fops = {
  469. .open = command_file_open,
  470. .release = command_file_close,
  471. .read = command_file_read,
  472. .write = command_file_write,
  473. .llseek = generic_file_llseek,
  474. };
  475. static const struct file_operations event_fops = {
  476. .open = event_file_open,
  477. .release = event_file_close,
  478. .read = event_file_read,
  479. .write = event_file_write,
  480. .llseek = generic_file_llseek,
  481. };
  482. static const struct file_operations r_heartbeat_fops = {
  483. .open = r_heartbeat_file_open,
  484. .release = r_heartbeat_file_close,
  485. .read = r_heartbeat_file_read,
  486. .write = r_heartbeat_file_write,
  487. .llseek = generic_file_llseek,
  488. };
  489. static const struct file_operations remote_settings_fops = {
  490. .open = simple_open,
  491. .release = remote_settings_file_close,
  492. .read = remote_settings_file_read,
  493. .write = remote_settings_file_write,
  494. .llseek = generic_file_llseek,
  495. };
  496. static void ibmasmfs_create_files (struct super_block *sb)
  497. {
  498. struct list_head *entry;
  499. struct service_processor *sp;
  500. list_for_each(entry, &service_processors) {
  501. struct dentry *dir;
  502. struct dentry *remote_dir;
  503. sp = list_entry(entry, struct service_processor, node);
  504. dir = ibmasmfs_create_dir(sb, sb->s_root, sp->dirname);
  505. if (!dir)
  506. continue;
  507. ibmasmfs_create_file(sb, dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
  508. ibmasmfs_create_file(sb, dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
  509. ibmasmfs_create_file(sb, dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
  510. remote_dir = ibmasmfs_create_dir(sb, dir, "remote_video");
  511. if (!remote_dir)
  512. continue;
  513. ibmasmfs_create_file(sb, remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
  514. ibmasmfs_create_file(sb, remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
  515. ibmasmfs_create_file(sb, remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);
  516. }
  517. }