monreader.c 16 KB

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