monreader.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * drivers/s390/char/monreader.c
  3. *
  4. * Character device driver for reading z/VM *MONITOR service records.
  5. *
  6. * Copyright IBM Corp. 2004, 2008
  7. * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/init.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/errno.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/ctype.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/poll.h>
  21. #include <net/iucv/iucv.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/ebcdic.h>
  24. #include <asm/extmem.h>
  25. //#define MON_DEBUG /* Debug messages on/off */
  26. #define MON_NAME "monreader"
  27. #define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x)
  28. #define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x)
  29. #define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
  30. #ifdef MON_DEBUG
  31. #define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x)
  32. #else
  33. #define P_DEBUG(x...) do {} while (0)
  34. #endif
  35. #define MON_COLLECT_SAMPLE 0x80
  36. #define MON_COLLECT_EVENT 0x40
  37. #define MON_SERVICE "*MONITOR"
  38. #define MON_IN_USE 0x01
  39. #define MON_MSGLIM 255
  40. static char mon_dcss_name[9] = "MONDCSS\0";
  41. struct mon_msg {
  42. u32 pos;
  43. u32 mca_offset;
  44. struct iucv_message msg;
  45. char msglim_reached;
  46. char replied_msglim;
  47. };
  48. struct mon_private {
  49. struct iucv_path *path;
  50. struct mon_msg *msg_array[MON_MSGLIM];
  51. unsigned int write_index;
  52. unsigned int read_index;
  53. atomic_t msglim_count;
  54. atomic_t read_ready;
  55. atomic_t iucv_connected;
  56. atomic_t iucv_severed;
  57. };
  58. static unsigned long mon_in_use = 0;
  59. static unsigned long mon_dcss_start;
  60. static unsigned long mon_dcss_end;
  61. static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
  62. static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
  63. static u8 user_data_connect[16] = {
  64. /* Version code, must be 0x01 for shared mode */
  65. 0x01,
  66. /* what to collect */
  67. MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
  68. /* DCSS name in EBCDIC, 8 bytes padded with blanks */
  69. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  70. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  71. };
  72. static u8 user_data_sever[16] = {
  73. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  74. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  75. };
  76. /******************************************************************************
  77. * helper functions *
  78. *****************************************************************************/
  79. /*
  80. * Create the 8 bytes EBCDIC DCSS segment name from
  81. * an ASCII name, incl. padding
  82. */
  83. static void dcss_mkname(char *ascii_name, char *ebcdic_name)
  84. {
  85. int i;
  86. for (i = 0; i < 8; i++) {
  87. if (ascii_name[i] == '\0')
  88. break;
  89. ebcdic_name[i] = toupper(ascii_name[i]);
  90. };
  91. for (; i < 8; i++)
  92. ebcdic_name[i] = ' ';
  93. ASCEBC(ebcdic_name, 8);
  94. }
  95. static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
  96. {
  97. return *(u32 *) &monmsg->msg.rmmsg;
  98. }
  99. static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
  100. {
  101. return *(u32 *) &monmsg->msg.rmmsg[4];
  102. }
  103. static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
  104. {
  105. return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
  106. }
  107. static inline u32 mon_mca_size(struct mon_msg *monmsg)
  108. {
  109. return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
  110. }
  111. static inline u32 mon_rec_start(struct mon_msg *monmsg)
  112. {
  113. return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
  114. }
  115. static inline u32 mon_rec_end(struct mon_msg *monmsg)
  116. {
  117. return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
  118. }
  119. static int mon_check_mca(struct mon_msg *monmsg)
  120. {
  121. if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
  122. (mon_rec_start(monmsg) < mon_dcss_start) ||
  123. (mon_rec_end(monmsg) > mon_dcss_end) ||
  124. (mon_mca_type(monmsg, 0) == 0) ||
  125. (mon_mca_size(monmsg) % 12 != 0) ||
  126. (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
  127. (mon_mca_end(monmsg) > mon_dcss_end) ||
  128. (mon_mca_start(monmsg) < mon_dcss_start) ||
  129. ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
  130. return -EINVAL;
  131. return 0;
  132. }
  133. static int mon_send_reply(struct mon_msg *monmsg,
  134. struct mon_private *monpriv)
  135. {
  136. int rc;
  137. rc = iucv_message_reply(monpriv->path, &monmsg->msg,
  138. IUCV_IPRMDATA, NULL, 0);
  139. atomic_dec(&monpriv->msglim_count);
  140. if (likely(!monmsg->msglim_reached)) {
  141. monmsg->pos = 0;
  142. monmsg->mca_offset = 0;
  143. monpriv->read_index = (monpriv->read_index + 1) %
  144. MON_MSGLIM;
  145. atomic_dec(&monpriv->read_ready);
  146. } else
  147. monmsg->replied_msglim = 1;
  148. if (rc) {
  149. P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
  150. return -EIO;
  151. }
  152. return 0;
  153. }
  154. static void mon_free_mem(struct mon_private *monpriv)
  155. {
  156. int i;
  157. for (i = 0; i < MON_MSGLIM; i++)
  158. if (monpriv->msg_array[i])
  159. kfree(monpriv->msg_array[i]);
  160. kfree(monpriv);
  161. }
  162. static struct mon_private *mon_alloc_mem(void)
  163. {
  164. int i;
  165. struct mon_private *monpriv;
  166. monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
  167. if (!monpriv)
  168. return NULL;
  169. for (i = 0; i < MON_MSGLIM; i++) {
  170. monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
  171. GFP_KERNEL);
  172. if (!monpriv->msg_array[i]) {
  173. mon_free_mem(monpriv);
  174. return NULL;
  175. }
  176. }
  177. return monpriv;
  178. }
  179. static inline void mon_next_mca(struct mon_msg *monmsg)
  180. {
  181. if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
  182. return;
  183. monmsg->mca_offset += 12;
  184. monmsg->pos = 0;
  185. }
  186. static struct mon_msg *mon_next_message(struct mon_private *monpriv)
  187. {
  188. struct mon_msg *monmsg;
  189. if (!atomic_read(&monpriv->read_ready))
  190. return NULL;
  191. monmsg = monpriv->msg_array[monpriv->read_index];
  192. if (unlikely(monmsg->replied_msglim)) {
  193. monmsg->replied_msglim = 0;
  194. monmsg->msglim_reached = 0;
  195. monmsg->pos = 0;
  196. monmsg->mca_offset = 0;
  197. monpriv->read_index = (monpriv->read_index + 1) %
  198. MON_MSGLIM;
  199. atomic_dec(&monpriv->read_ready);
  200. return ERR_PTR(-EOVERFLOW);
  201. }
  202. return monmsg;
  203. }
  204. /******************************************************************************
  205. * IUCV handler *
  206. *****************************************************************************/
  207. static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
  208. {
  209. struct mon_private *monpriv = path->private;
  210. atomic_set(&monpriv->iucv_connected, 1);
  211. wake_up(&mon_conn_wait_queue);
  212. }
  213. static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
  214. {
  215. struct mon_private *monpriv = path->private;
  216. P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
  217. iucv_path_sever(path, NULL);
  218. atomic_set(&monpriv->iucv_severed, 1);
  219. wake_up(&mon_conn_wait_queue);
  220. wake_up_interruptible(&mon_read_wait_queue);
  221. }
  222. static void mon_iucv_message_pending(struct iucv_path *path,
  223. struct iucv_message *msg)
  224. {
  225. struct mon_private *monpriv = path->private;
  226. memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
  227. msg, sizeof(*msg));
  228. if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
  229. P_WARNING("IUCV message pending, message limit (%i) reached\n",
  230. MON_MSGLIM);
  231. monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
  232. }
  233. monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
  234. atomic_inc(&monpriv->read_ready);
  235. wake_up_interruptible(&mon_read_wait_queue);
  236. }
  237. static struct iucv_handler monreader_iucv_handler = {
  238. .path_complete = mon_iucv_path_complete,
  239. .path_severed = mon_iucv_path_severed,
  240. .message_pending = mon_iucv_message_pending,
  241. };
  242. /******************************************************************************
  243. * file operations *
  244. *****************************************************************************/
  245. static int mon_open(struct inode *inode, struct file *filp)
  246. {
  247. struct mon_private *monpriv;
  248. int rc;
  249. /*
  250. * only one user allowed
  251. */
  252. lock_kernel();
  253. rc = -EBUSY;
  254. if (test_and_set_bit(MON_IN_USE, &mon_in_use))
  255. goto out;
  256. rc = -ENOMEM;
  257. monpriv = mon_alloc_mem();
  258. if (!monpriv)
  259. goto out_use;
  260. /*
  261. * Connect to *MONITOR service
  262. */
  263. monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
  264. if (!monpriv->path)
  265. goto out_priv;
  266. rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
  267. MON_SERVICE, NULL, user_data_connect, monpriv);
  268. if (rc) {
  269. P_ERROR("iucv connection to *MONITOR failed with "
  270. "IPUSER SEVER code = %i\n", rc);
  271. rc = -EIO;
  272. goto out_path;
  273. }
  274. /*
  275. * Wait for connection confirmation
  276. */
  277. wait_event(mon_conn_wait_queue,
  278. atomic_read(&monpriv->iucv_connected) ||
  279. atomic_read(&monpriv->iucv_severed));
  280. if (atomic_read(&monpriv->iucv_severed)) {
  281. atomic_set(&monpriv->iucv_severed, 0);
  282. atomic_set(&monpriv->iucv_connected, 0);
  283. rc = -EIO;
  284. goto out_path;
  285. }
  286. filp->private_data = monpriv;
  287. unlock_kernel();
  288. return nonseekable_open(inode, filp);
  289. out_path:
  290. kfree(monpriv->path);
  291. out_priv:
  292. mon_free_mem(monpriv);
  293. out_use:
  294. clear_bit(MON_IN_USE, &mon_in_use);
  295. out:
  296. unlock_kernel();
  297. return rc;
  298. }
  299. static int mon_close(struct inode *inode, struct file *filp)
  300. {
  301. int rc, i;
  302. struct mon_private *monpriv = filp->private_data;
  303. /*
  304. * Close IUCV connection and unregister
  305. */
  306. rc = iucv_path_sever(monpriv->path, user_data_sever);
  307. if (rc)
  308. P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
  309. atomic_set(&monpriv->iucv_severed, 0);
  310. atomic_set(&monpriv->iucv_connected, 0);
  311. atomic_set(&monpriv->read_ready, 0);
  312. atomic_set(&monpriv->msglim_count, 0);
  313. monpriv->write_index = 0;
  314. monpriv->read_index = 0;
  315. for (i = 0; i < MON_MSGLIM; i++)
  316. kfree(monpriv->msg_array[i]);
  317. kfree(monpriv);
  318. clear_bit(MON_IN_USE, &mon_in_use);
  319. return 0;
  320. }
  321. static ssize_t mon_read(struct file *filp, char __user *data,
  322. size_t count, loff_t *ppos)
  323. {
  324. struct mon_private *monpriv = filp->private_data;
  325. struct mon_msg *monmsg;
  326. int ret;
  327. u32 mce_start;
  328. monmsg = mon_next_message(monpriv);
  329. if (IS_ERR(monmsg))
  330. return PTR_ERR(monmsg);
  331. if (!monmsg) {
  332. if (filp->f_flags & O_NONBLOCK)
  333. return -EAGAIN;
  334. ret = wait_event_interruptible(mon_read_wait_queue,
  335. atomic_read(&monpriv->read_ready) ||
  336. atomic_read(&monpriv->iucv_severed));
  337. if (ret)
  338. return ret;
  339. if (unlikely(atomic_read(&monpriv->iucv_severed)))
  340. return -EIO;
  341. monmsg = monpriv->msg_array[monpriv->read_index];
  342. }
  343. if (!monmsg->pos)
  344. monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
  345. if (mon_check_mca(monmsg))
  346. goto reply;
  347. /* read monitor control element (12 bytes) first */
  348. mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
  349. if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
  350. count = min(count, (size_t) mce_start + 12 - monmsg->pos);
  351. ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
  352. count);
  353. if (ret)
  354. return -EFAULT;
  355. monmsg->pos += count;
  356. if (monmsg->pos == mce_start + 12)
  357. monmsg->pos = mon_rec_start(monmsg);
  358. goto out_copy;
  359. }
  360. /* read records */
  361. if (monmsg->pos <= mon_rec_end(monmsg)) {
  362. count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
  363. + 1);
  364. ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
  365. count);
  366. if (ret)
  367. return -EFAULT;
  368. monmsg->pos += count;
  369. if (monmsg->pos > mon_rec_end(monmsg))
  370. mon_next_mca(monmsg);
  371. goto out_copy;
  372. }
  373. reply:
  374. ret = mon_send_reply(monmsg, monpriv);
  375. return ret;
  376. out_copy:
  377. *ppos += count;
  378. return count;
  379. }
  380. static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
  381. {
  382. struct mon_private *monpriv = filp->private_data;
  383. poll_wait(filp, &mon_read_wait_queue, p);
  384. if (unlikely(atomic_read(&monpriv->iucv_severed)))
  385. return POLLERR;
  386. if (atomic_read(&monpriv->read_ready))
  387. return POLLIN | POLLRDNORM;
  388. return 0;
  389. }
  390. static const struct file_operations mon_fops = {
  391. .owner = THIS_MODULE,
  392. .open = &mon_open,
  393. .release = &mon_close,
  394. .read = &mon_read,
  395. .poll = &mon_poll,
  396. };
  397. static struct miscdevice mon_dev = {
  398. .name = "monreader",
  399. .fops = &mon_fops,
  400. .minor = MISC_DYNAMIC_MINOR,
  401. };
  402. /******************************************************************************
  403. * module init/exit *
  404. *****************************************************************************/
  405. static int __init mon_init(void)
  406. {
  407. int rc;
  408. if (!MACHINE_IS_VM) {
  409. P_ERROR("not running under z/VM, driver not loaded\n");
  410. return -ENODEV;
  411. }
  412. /*
  413. * Register with IUCV and connect to *MONITOR service
  414. */
  415. rc = iucv_register(&monreader_iucv_handler, 1);
  416. if (rc) {
  417. P_ERROR("failed to register with iucv driver\n");
  418. return rc;
  419. }
  420. rc = segment_type(mon_dcss_name);
  421. if (rc < 0) {
  422. segment_warning(rc, mon_dcss_name);
  423. goto out_iucv;
  424. }
  425. if (rc != SEG_TYPE_SC) {
  426. P_ERROR("segment %s has unsupported type, should be SC\n",
  427. mon_dcss_name);
  428. rc = -EINVAL;
  429. goto out_iucv;
  430. }
  431. rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
  432. &mon_dcss_start, &mon_dcss_end);
  433. if (rc < 0) {
  434. segment_warning(rc, mon_dcss_name);
  435. rc = -EINVAL;
  436. goto out_iucv;
  437. }
  438. dcss_mkname(mon_dcss_name, &user_data_connect[8]);
  439. rc = misc_register(&mon_dev);
  440. if (rc < 0 )
  441. goto out;
  442. return 0;
  443. out:
  444. segment_unload(mon_dcss_name);
  445. out_iucv:
  446. iucv_unregister(&monreader_iucv_handler, 1);
  447. return rc;
  448. }
  449. static void __exit mon_exit(void)
  450. {
  451. segment_unload(mon_dcss_name);
  452. WARN_ON(misc_deregister(&mon_dev) != 0);
  453. iucv_unregister(&monreader_iucv_handler, 1);
  454. return;
  455. }
  456. module_init(mon_init);
  457. module_exit(mon_exit);
  458. module_param_string(mondcss, mon_dcss_name, 9, 0444);
  459. MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
  460. "service, max. 8 chars. Default is MONDCSS");
  461. MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
  462. MODULE_DESCRIPTION("Character device driver for reading z/VM "
  463. "monitor service records.");
  464. MODULE_LICENSE("GPL");