ibmasmfs.c 15 KB

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