ibmasmfs.c 15 KB

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