ibmasmfs.c 15 KB

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