monreader.c 13 KB

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