monreader.c 13 KB

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