ibmasmfs.c 15 KB

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