monreader.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * drivers/s390/char/monreader.c
  3. *
  4. * Character device driver for reading z/VM *MONITOR service records.
  5. *
  6. * Copyright 2004 IBM Corporation, IBM Deutschland Entwicklung GmbH.
  7. *
  8. * Author: Gerald Schaefer <geraldsc@de.ibm.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.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 <asm/uaccess.h>
  21. #include <asm/ebcdic.h>
  22. #include <asm/extmem.h>
  23. #include <linux/poll.h>
  24. #include <net/iucv/iucv.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. {
  131. P_DEBUG("READ, IGNORED INVALID MCA\n\n");
  132. return -EINVAL;
  133. }
  134. return 0;
  135. }
  136. static int mon_send_reply(struct mon_msg *monmsg,
  137. struct mon_private *monpriv)
  138. {
  139. int rc;
  140. P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = "
  141. "0x%08X\n\n",
  142. monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
  143. rc = iucv_message_reply(monpriv->path, &monmsg->msg,
  144. IUCV_IPRMDATA, NULL, 0);
  145. atomic_dec(&monpriv->msglim_count);
  146. if (likely(!monmsg->msglim_reached)) {
  147. monmsg->pos = 0;
  148. monmsg->mca_offset = 0;
  149. monpriv->read_index = (monpriv->read_index + 1) %
  150. MON_MSGLIM;
  151. atomic_dec(&monpriv->read_ready);
  152. } else
  153. monmsg->replied_msglim = 1;
  154. if (rc) {
  155. P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
  156. return -EIO;
  157. }
  158. return 0;
  159. }
  160. static void mon_free_mem(struct mon_private *monpriv)
  161. {
  162. int i;
  163. for (i = 0; i < MON_MSGLIM; i++)
  164. if (monpriv->msg_array[i])
  165. kfree(monpriv->msg_array[i]);
  166. kfree(monpriv);
  167. }
  168. static struct mon_private *mon_alloc_mem(void)
  169. {
  170. int i;
  171. struct mon_private *monpriv;
  172. monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
  173. if (!monpriv) {
  174. P_ERROR("no memory for monpriv\n");
  175. return NULL;
  176. }
  177. for (i = 0; i < MON_MSGLIM; i++) {
  178. monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
  179. GFP_KERNEL);
  180. if (!monpriv->msg_array[i]) {
  181. P_ERROR("open, no memory for msg_array\n");
  182. mon_free_mem(monpriv);
  183. return NULL;
  184. }
  185. }
  186. return monpriv;
  187. }
  188. static inline void mon_read_debug(struct mon_msg *monmsg,
  189. struct mon_private *monpriv)
  190. {
  191. #ifdef MON_DEBUG
  192. u8 msg_type[2], mca_type;
  193. unsigned long records_len;
  194. records_len = mon_rec_end(monmsg) - mon_rec_start(monmsg) + 1;
  195. memcpy(msg_type, &monmsg->msg.class, 2);
  196. EBCASC(msg_type, 2);
  197. mca_type = mon_mca_type(monmsg, 0);
  198. EBCASC(&mca_type, 1);
  199. P_DEBUG("read, mon_read_index = %i, mon_write_index = %i\n",
  200. monpriv->read_index, monpriv->write_index);
  201. P_DEBUG("read, pathid = 0x%04X, msgid = 0x%08X, trgcls = 0x%08X\n",
  202. monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
  203. P_DEBUG("read, msg_type = '%c%c', mca_type = '%c' / 0x%X / 0x%X\n",
  204. msg_type[0], msg_type[1], mca_type ? mca_type : 'X',
  205. mon_mca_type(monmsg, 1), mon_mca_type(monmsg, 2));
  206. P_DEBUG("read, MCA: start = 0x%lX, end = 0x%lX\n",
  207. mon_mca_start(monmsg), mon_mca_end(monmsg));
  208. P_DEBUG("read, REC: start = 0x%X, end = 0x%X, len = %lu\n\n",
  209. mon_rec_start(monmsg), mon_rec_end(monmsg), records_len);
  210. if (mon_mca_size(monmsg) > 12)
  211. P_DEBUG("READ, MORE THAN ONE MCA\n\n");
  212. #endif
  213. }
  214. static inline void mon_next_mca(struct mon_msg *monmsg)
  215. {
  216. if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
  217. return;
  218. P_DEBUG("READ, NEXT MCA\n\n");
  219. monmsg->mca_offset += 12;
  220. monmsg->pos = 0;
  221. }
  222. static struct mon_msg *mon_next_message(struct mon_private *monpriv)
  223. {
  224. struct mon_msg *monmsg;
  225. if (!atomic_read(&monpriv->read_ready))
  226. return NULL;
  227. monmsg = monpriv->msg_array[monpriv->read_index];
  228. if (unlikely(monmsg->replied_msglim)) {
  229. monmsg->replied_msglim = 0;
  230. monmsg->msglim_reached = 0;
  231. monmsg->pos = 0;
  232. monmsg->mca_offset = 0;
  233. P_WARNING("read, message limit reached\n");
  234. monpriv->read_index = (monpriv->read_index + 1) %
  235. MON_MSGLIM;
  236. atomic_dec(&monpriv->read_ready);
  237. return ERR_PTR(-EOVERFLOW);
  238. }
  239. return monmsg;
  240. }
  241. /******************************************************************************
  242. * IUCV handler *
  243. *****************************************************************************/
  244. static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
  245. {
  246. struct mon_private *monpriv = path->private;
  247. P_DEBUG("IUCV connection completed\n");
  248. P_DEBUG("IUCV ACCEPT (from *MONITOR): Version = 0x%02X, Event = "
  249. "0x%02X, Sample = 0x%02X\n",
  250. ipuser[0], ipuser[1], ipuser[2]);
  251. atomic_set(&monpriv->iucv_connected, 1);
  252. wake_up(&mon_conn_wait_queue);
  253. }
  254. static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
  255. {
  256. struct mon_private *monpriv = path->private;
  257. P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
  258. iucv_path_sever(path, NULL);
  259. atomic_set(&monpriv->iucv_severed, 1);
  260. wake_up(&mon_conn_wait_queue);
  261. wake_up_interruptible(&mon_read_wait_queue);
  262. }
  263. static void mon_iucv_message_pending(struct iucv_path *path,
  264. struct iucv_message *msg)
  265. {
  266. struct mon_private *monpriv = path->private;
  267. P_DEBUG("IUCV message pending\n");
  268. memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
  269. msg, sizeof(*msg));
  270. if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
  271. P_WARNING("IUCV message pending, message limit (%i) reached\n",
  272. MON_MSGLIM);
  273. monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
  274. }
  275. monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
  276. atomic_inc(&monpriv->read_ready);
  277. wake_up_interruptible(&mon_read_wait_queue);
  278. }
  279. static struct iucv_handler monreader_iucv_handler = {
  280. .path_complete = mon_iucv_path_complete,
  281. .path_severed = mon_iucv_path_severed,
  282. .message_pending = mon_iucv_message_pending,
  283. };
  284. /******************************************************************************
  285. * file operations *
  286. *****************************************************************************/
  287. static int mon_open(struct inode *inode, struct file *filp)
  288. {
  289. struct mon_private *monpriv;
  290. int rc;
  291. /*
  292. * only one user allowed
  293. */
  294. rc = -EBUSY;
  295. if (test_and_set_bit(MON_IN_USE, &mon_in_use))
  296. goto out;
  297. rc = -ENOMEM;
  298. monpriv = mon_alloc_mem();
  299. if (!monpriv)
  300. goto out_use;
  301. /*
  302. * Connect to *MONITOR service
  303. */
  304. monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
  305. if (!monpriv->path)
  306. goto out_priv;
  307. rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
  308. MON_SERVICE, NULL, user_data_connect, monpriv);
  309. if (rc) {
  310. P_ERROR("iucv connection to *MONITOR failed with "
  311. "IPUSER SEVER code = %i\n", rc);
  312. rc = -EIO;
  313. goto out_path;
  314. }
  315. /*
  316. * Wait for connection confirmation
  317. */
  318. wait_event(mon_conn_wait_queue,
  319. atomic_read(&monpriv->iucv_connected) ||
  320. atomic_read(&monpriv->iucv_severed));
  321. if (atomic_read(&monpriv->iucv_severed)) {
  322. atomic_set(&monpriv->iucv_severed, 0);
  323. atomic_set(&monpriv->iucv_connected, 0);
  324. rc = -EIO;
  325. goto out_path;
  326. }
  327. P_INFO("open, established connection to *MONITOR service\n\n");
  328. filp->private_data = monpriv;
  329. return nonseekable_open(inode, filp);
  330. out_path:
  331. kfree(monpriv->path);
  332. out_priv:
  333. mon_free_mem(monpriv);
  334. out_use:
  335. clear_bit(MON_IN_USE, &mon_in_use);
  336. out:
  337. return rc;
  338. }
  339. static int mon_close(struct inode *inode, struct file *filp)
  340. {
  341. int rc, i;
  342. struct mon_private *monpriv = filp->private_data;
  343. /*
  344. * Close IUCV connection and unregister
  345. */
  346. rc = iucv_path_sever(monpriv->path, user_data_sever);
  347. if (rc)
  348. P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
  349. else
  350. P_INFO("close, terminated connection to *MONITOR service\n");
  351. atomic_set(&monpriv->iucv_severed, 0);
  352. atomic_set(&monpriv->iucv_connected, 0);
  353. atomic_set(&monpriv->read_ready, 0);
  354. atomic_set(&monpriv->msglim_count, 0);
  355. monpriv->write_index = 0;
  356. monpriv->read_index = 0;
  357. for (i = 0; i < MON_MSGLIM; i++)
  358. kfree(monpriv->msg_array[i]);
  359. kfree(monpriv);
  360. clear_bit(MON_IN_USE, &mon_in_use);
  361. return 0;
  362. }
  363. static ssize_t mon_read(struct file *filp, char __user *data,
  364. size_t count, loff_t *ppos)
  365. {
  366. struct mon_private *monpriv = filp->private_data;
  367. struct mon_msg *monmsg;
  368. int ret;
  369. u32 mce_start;
  370. monmsg = mon_next_message(monpriv);
  371. if (IS_ERR(monmsg))
  372. return PTR_ERR(monmsg);
  373. if (!monmsg) {
  374. if (filp->f_flags & O_NONBLOCK)
  375. return -EAGAIN;
  376. ret = wait_event_interruptible(mon_read_wait_queue,
  377. atomic_read(&monpriv->read_ready) ||
  378. atomic_read(&monpriv->iucv_severed));
  379. if (ret)
  380. return ret;
  381. if (unlikely(atomic_read(&monpriv->iucv_severed)))
  382. return -EIO;
  383. monmsg = monpriv->msg_array[monpriv->read_index];
  384. }
  385. if (!monmsg->pos) {
  386. monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
  387. mon_read_debug(monmsg, monpriv);
  388. }
  389. if (mon_check_mca(monmsg))
  390. goto reply;
  391. /* read monitor control element (12 bytes) first */
  392. mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
  393. if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
  394. count = min(count, (size_t) mce_start + 12 - monmsg->pos);
  395. ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
  396. count);
  397. if (ret)
  398. return -EFAULT;
  399. monmsg->pos += count;
  400. if (monmsg->pos == mce_start + 12)
  401. monmsg->pos = mon_rec_start(monmsg);
  402. goto out_copy;
  403. }
  404. /* read records */
  405. if (monmsg->pos <= mon_rec_end(monmsg)) {
  406. count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
  407. + 1);
  408. ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
  409. count);
  410. if (ret)
  411. return -EFAULT;
  412. monmsg->pos += count;
  413. if (monmsg->pos > mon_rec_end(monmsg))
  414. mon_next_mca(monmsg);
  415. goto out_copy;
  416. }
  417. reply:
  418. ret = mon_send_reply(monmsg, monpriv);
  419. return ret;
  420. out_copy:
  421. *ppos += count;
  422. return count;
  423. }
  424. static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
  425. {
  426. struct mon_private *monpriv = filp->private_data;
  427. poll_wait(filp, &mon_read_wait_queue, p);
  428. if (unlikely(atomic_read(&monpriv->iucv_severed)))
  429. return POLLERR;
  430. if (atomic_read(&monpriv->read_ready))
  431. return POLLIN | POLLRDNORM;
  432. return 0;
  433. }
  434. static const struct file_operations mon_fops = {
  435. .owner = THIS_MODULE,
  436. .open = &mon_open,
  437. .release = &mon_close,
  438. .read = &mon_read,
  439. .poll = &mon_poll,
  440. };
  441. static struct miscdevice mon_dev = {
  442. .name = "monreader",
  443. .fops = &mon_fops,
  444. .minor = MISC_DYNAMIC_MINOR,
  445. };
  446. /******************************************************************************
  447. * module init/exit *
  448. *****************************************************************************/
  449. static int __init mon_init(void)
  450. {
  451. int rc;
  452. if (!MACHINE_IS_VM) {
  453. P_ERROR("not running under z/VM, driver not loaded\n");
  454. return -ENODEV;
  455. }
  456. /*
  457. * Register with IUCV and connect to *MONITOR service
  458. */
  459. rc = iucv_register(&monreader_iucv_handler, 1);
  460. if (rc) {
  461. P_ERROR("failed to register with iucv driver\n");
  462. return rc;
  463. }
  464. P_INFO("open, registered with IUCV\n");
  465. rc = segment_type(mon_dcss_name);
  466. if (rc < 0) {
  467. segment_warning(rc, mon_dcss_name);
  468. goto out_iucv;
  469. }
  470. if (rc != SEG_TYPE_SC) {
  471. P_ERROR("segment %s has unsupported type, should be SC\n",
  472. mon_dcss_name);
  473. rc = -EINVAL;
  474. goto out_iucv;
  475. }
  476. rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
  477. &mon_dcss_start, &mon_dcss_end);
  478. if (rc < 0) {
  479. segment_warning(rc, mon_dcss_name);
  480. rc = -EINVAL;
  481. goto out_iucv;
  482. }
  483. dcss_mkname(mon_dcss_name, &user_data_connect[8]);
  484. rc = misc_register(&mon_dev);
  485. if (rc < 0 ) {
  486. P_ERROR("misc_register failed, rc = %i\n", rc);
  487. goto out;
  488. }
  489. P_INFO("Loaded segment %s from %p to %p, size = %lu Byte\n",
  490. mon_dcss_name, (void *) mon_dcss_start, (void *) mon_dcss_end,
  491. mon_dcss_end - mon_dcss_start + 1);
  492. return 0;
  493. out:
  494. segment_unload(mon_dcss_name);
  495. out_iucv:
  496. iucv_unregister(&monreader_iucv_handler, 1);
  497. return rc;
  498. }
  499. static void __exit mon_exit(void)
  500. {
  501. segment_unload(mon_dcss_name);
  502. WARN_ON(misc_deregister(&mon_dev) != 0);
  503. iucv_unregister(&monreader_iucv_handler, 1);
  504. return;
  505. }
  506. module_init(mon_init);
  507. module_exit(mon_exit);
  508. module_param_string(mondcss, mon_dcss_name, 9, 0444);
  509. MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
  510. "service, max. 8 chars. Default is MONDCSS");
  511. MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
  512. MODULE_DESCRIPTION("Character device driver for reading z/VM "
  513. "monitor service records.");
  514. MODULE_LICENSE("GPL");