monwriter.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Character device driver for writing z/VM *MONITOR service records.
  4. *
  5. * Copyright IBM Corp. 2006, 2009
  6. *
  7. * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
  8. */
  9. #define KMSG_COMPONENT "monwriter"
  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/poll.h>
  20. #include <linux/mutex.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/ebcdic.h>
  25. #include <asm/io.h>
  26. #include <asm/appldata.h>
  27. #include <asm/monwriter.h>
  28. #define MONWRITE_MAX_DATALEN 4010
  29. static int mon_max_bufs = 255;
  30. static int mon_buf_count;
  31. struct mon_buf {
  32. struct list_head list;
  33. struct monwrite_hdr hdr;
  34. int diag_done;
  35. char *data;
  36. };
  37. static LIST_HEAD(mon_priv_list);
  38. struct mon_private {
  39. struct list_head priv_list;
  40. struct list_head list;
  41. struct monwrite_hdr hdr;
  42. size_t hdr_to_read;
  43. size_t data_to_read;
  44. struct mon_buf *current_buf;
  45. struct mutex thread_mutex;
  46. };
  47. /*
  48. * helper functions
  49. */
  50. static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
  51. {
  52. struct appldata_parameter_list *parm_list;
  53. struct appldata_product_id *id;
  54. int rc;
  55. id = kmalloc(sizeof(*id), GFP_KERNEL);
  56. parm_list = kmalloc(sizeof(*parm_list), GFP_KERNEL);
  57. rc = -ENOMEM;
  58. if (!id || !parm_list)
  59. goto out;
  60. memcpy(id->prod_nr, "LNXAPPL", 7);
  61. id->prod_fn = myhdr->applid;
  62. id->record_nr = myhdr->record_num;
  63. id->version_nr = myhdr->version;
  64. id->release_nr = myhdr->release;
  65. id->mod_lvl = myhdr->mod_level;
  66. rc = appldata_asm(parm_list, id, fcn,
  67. (void *) buffer, myhdr->datalen);
  68. if (rc <= 0)
  69. goto out;
  70. pr_err("Writing monitor data failed with rc=%i\n", rc);
  71. rc = (rc == 5) ? -EPERM : -EINVAL;
  72. out:
  73. kfree(id);
  74. kfree(parm_list);
  75. return rc;
  76. }
  77. static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
  78. struct monwrite_hdr *monhdr)
  79. {
  80. struct mon_buf *entry, *next;
  81. list_for_each_entry_safe(entry, next, &monpriv->list, list)
  82. if ((entry->hdr.mon_function == monhdr->mon_function ||
  83. monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
  84. entry->hdr.applid == monhdr->applid &&
  85. entry->hdr.record_num == monhdr->record_num &&
  86. entry->hdr.version == monhdr->version &&
  87. entry->hdr.release == monhdr->release &&
  88. entry->hdr.mod_level == monhdr->mod_level)
  89. return entry;
  90. return NULL;
  91. }
  92. static int monwrite_new_hdr(struct mon_private *monpriv)
  93. {
  94. struct monwrite_hdr *monhdr = &monpriv->hdr;
  95. struct mon_buf *monbuf;
  96. int rc = 0;
  97. if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
  98. monhdr->mon_function > MONWRITE_START_CONFIG ||
  99. monhdr->hdrlen != sizeof(struct monwrite_hdr))
  100. return -EINVAL;
  101. monbuf = NULL;
  102. if (monhdr->mon_function != MONWRITE_GEN_EVENT)
  103. monbuf = monwrite_find_hdr(monpriv, monhdr);
  104. if (monbuf) {
  105. if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
  106. monhdr->datalen = monbuf->hdr.datalen;
  107. rc = monwrite_diag(monhdr, monbuf->data,
  108. APPLDATA_STOP_REC);
  109. list_del(&monbuf->list);
  110. mon_buf_count--;
  111. kfree(monbuf->data);
  112. kfree(monbuf);
  113. monbuf = NULL;
  114. }
  115. } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
  116. if (mon_buf_count >= mon_max_bufs)
  117. return -ENOSPC;
  118. monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
  119. if (!monbuf)
  120. return -ENOMEM;
  121. monbuf->data = kzalloc(monhdr->datalen,
  122. GFP_KERNEL | GFP_DMA);
  123. if (!monbuf->data) {
  124. kfree(monbuf);
  125. return -ENOMEM;
  126. }
  127. monbuf->hdr = *monhdr;
  128. list_add_tail(&monbuf->list, &monpriv->list);
  129. if (monhdr->mon_function != MONWRITE_GEN_EVENT)
  130. mon_buf_count++;
  131. }
  132. monpriv->current_buf = monbuf;
  133. return rc;
  134. }
  135. static int monwrite_new_data(struct mon_private *monpriv)
  136. {
  137. struct monwrite_hdr *monhdr = &monpriv->hdr;
  138. struct mon_buf *monbuf = monpriv->current_buf;
  139. int rc = 0;
  140. switch (monhdr->mon_function) {
  141. case MONWRITE_START_INTERVAL:
  142. if (!monbuf->diag_done) {
  143. rc = monwrite_diag(monhdr, monbuf->data,
  144. APPLDATA_START_INTERVAL_REC);
  145. monbuf->diag_done = 1;
  146. }
  147. break;
  148. case MONWRITE_START_CONFIG:
  149. if (!monbuf->diag_done) {
  150. rc = monwrite_diag(monhdr, monbuf->data,
  151. APPLDATA_START_CONFIG_REC);
  152. monbuf->diag_done = 1;
  153. }
  154. break;
  155. case MONWRITE_GEN_EVENT:
  156. rc = monwrite_diag(monhdr, monbuf->data,
  157. APPLDATA_GEN_EVENT_REC);
  158. list_del(&monpriv->current_buf->list);
  159. kfree(monpriv->current_buf->data);
  160. kfree(monpriv->current_buf);
  161. monpriv->current_buf = NULL;
  162. break;
  163. default:
  164. /* monhdr->mon_function is checked in monwrite_new_hdr */
  165. BUG();
  166. }
  167. return rc;
  168. }
  169. /*
  170. * file operations
  171. */
  172. static int monwrite_open(struct inode *inode, struct file *filp)
  173. {
  174. struct mon_private *monpriv;
  175. monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
  176. if (!monpriv)
  177. return -ENOMEM;
  178. INIT_LIST_HEAD(&monpriv->list);
  179. monpriv->hdr_to_read = sizeof(monpriv->hdr);
  180. mutex_init(&monpriv->thread_mutex);
  181. filp->private_data = monpriv;
  182. list_add_tail(&monpriv->priv_list, &mon_priv_list);
  183. return nonseekable_open(inode, filp);
  184. }
  185. static int monwrite_close(struct inode *inode, struct file *filp)
  186. {
  187. struct mon_private *monpriv = filp->private_data;
  188. struct mon_buf *entry, *next;
  189. list_for_each_entry_safe(entry, next, &monpriv->list, list) {
  190. if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
  191. monwrite_diag(&entry->hdr, entry->data,
  192. APPLDATA_STOP_REC);
  193. mon_buf_count--;
  194. list_del(&entry->list);
  195. kfree(entry->data);
  196. kfree(entry);
  197. }
  198. list_del(&monpriv->priv_list);
  199. kfree(monpriv);
  200. return 0;
  201. }
  202. static ssize_t monwrite_write(struct file *filp, const char __user *data,
  203. size_t count, loff_t *ppos)
  204. {
  205. struct mon_private *monpriv = filp->private_data;
  206. size_t len, written;
  207. void *to;
  208. int rc;
  209. mutex_lock(&monpriv->thread_mutex);
  210. for (written = 0; written < count; ) {
  211. if (monpriv->hdr_to_read) {
  212. len = min(count - written, monpriv->hdr_to_read);
  213. to = (char *) &monpriv->hdr +
  214. sizeof(monpriv->hdr) - monpriv->hdr_to_read;
  215. if (copy_from_user(to, data + written, len)) {
  216. rc = -EFAULT;
  217. goto out_error;
  218. }
  219. monpriv->hdr_to_read -= len;
  220. written += len;
  221. if (monpriv->hdr_to_read > 0)
  222. continue;
  223. rc = monwrite_new_hdr(monpriv);
  224. if (rc)
  225. goto out_error;
  226. monpriv->data_to_read = monpriv->current_buf ?
  227. monpriv->current_buf->hdr.datalen : 0;
  228. }
  229. if (monpriv->data_to_read) {
  230. len = min(count - written, monpriv->data_to_read);
  231. to = monpriv->current_buf->data +
  232. monpriv->hdr.datalen - monpriv->data_to_read;
  233. if (copy_from_user(to, data + written, len)) {
  234. rc = -EFAULT;
  235. goto out_error;
  236. }
  237. monpriv->data_to_read -= len;
  238. written += len;
  239. if (monpriv->data_to_read > 0)
  240. continue;
  241. rc = monwrite_new_data(monpriv);
  242. if (rc)
  243. goto out_error;
  244. }
  245. monpriv->hdr_to_read = sizeof(monpriv->hdr);
  246. }
  247. mutex_unlock(&monpriv->thread_mutex);
  248. return written;
  249. out_error:
  250. monpriv->data_to_read = 0;
  251. monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
  252. mutex_unlock(&monpriv->thread_mutex);
  253. return rc;
  254. }
  255. static const struct file_operations monwrite_fops = {
  256. .owner = THIS_MODULE,
  257. .open = &monwrite_open,
  258. .release = &monwrite_close,
  259. .write = &monwrite_write,
  260. .llseek = noop_llseek,
  261. };
  262. static struct miscdevice mon_dev = {
  263. .name = "monwriter",
  264. .fops = &monwrite_fops,
  265. .minor = MISC_DYNAMIC_MINOR,
  266. };
  267. /*
  268. * suspend/resume
  269. */
  270. static int monwriter_freeze(struct device *dev)
  271. {
  272. struct mon_private *monpriv;
  273. struct mon_buf *monbuf;
  274. list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
  275. list_for_each_entry(monbuf, &monpriv->list, list) {
  276. if (monbuf->hdr.mon_function != MONWRITE_GEN_EVENT)
  277. monwrite_diag(&monbuf->hdr, monbuf->data,
  278. APPLDATA_STOP_REC);
  279. }
  280. }
  281. return 0;
  282. }
  283. static int monwriter_restore(struct device *dev)
  284. {
  285. struct mon_private *monpriv;
  286. struct mon_buf *monbuf;
  287. list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
  288. list_for_each_entry(monbuf, &monpriv->list, list) {
  289. if (monbuf->hdr.mon_function == MONWRITE_START_INTERVAL)
  290. monwrite_diag(&monbuf->hdr, monbuf->data,
  291. APPLDATA_START_INTERVAL_REC);
  292. if (monbuf->hdr.mon_function == MONWRITE_START_CONFIG)
  293. monwrite_diag(&monbuf->hdr, monbuf->data,
  294. APPLDATA_START_CONFIG_REC);
  295. }
  296. }
  297. return 0;
  298. }
  299. static int monwriter_thaw(struct device *dev)
  300. {
  301. return monwriter_restore(dev);
  302. }
  303. static const struct dev_pm_ops monwriter_pm_ops = {
  304. .freeze = monwriter_freeze,
  305. .thaw = monwriter_thaw,
  306. .restore = monwriter_restore,
  307. };
  308. static struct platform_driver monwriter_pdrv = {
  309. .driver = {
  310. .name = "monwriter",
  311. .pm = &monwriter_pm_ops,
  312. },
  313. };
  314. static struct platform_device *monwriter_pdev;
  315. /*
  316. * module init/exit
  317. */
  318. static int __init mon_init(void)
  319. {
  320. int rc;
  321. if (!MACHINE_IS_VM)
  322. return -ENODEV;
  323. rc = platform_driver_register(&monwriter_pdrv);
  324. if (rc)
  325. return rc;
  326. monwriter_pdev = platform_device_register_simple("monwriter", -1, NULL,
  327. 0);
  328. if (IS_ERR(monwriter_pdev)) {
  329. rc = PTR_ERR(monwriter_pdev);
  330. goto out_driver;
  331. }
  332. /*
  333. * misc_register() has to be the last action in module_init(), because
  334. * file operations will be available right after this.
  335. */
  336. rc = misc_register(&mon_dev);
  337. if (rc)
  338. goto out_device;
  339. return 0;
  340. out_device:
  341. platform_device_unregister(monwriter_pdev);
  342. out_driver:
  343. platform_driver_unregister(&monwriter_pdrv);
  344. return rc;
  345. }
  346. static void __exit mon_exit(void)
  347. {
  348. misc_deregister(&mon_dev);
  349. platform_device_unregister(monwriter_pdev);
  350. platform_driver_unregister(&monwriter_pdrv);
  351. }
  352. module_init(mon_init);
  353. module_exit(mon_exit);
  354. module_param_named(max_bufs, mon_max_bufs, int, 0644);
  355. MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
  356. "that can be active at one time");
  357. MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
  358. MODULE_DESCRIPTION("Character device driver for writing z/VM "
  359. "APPLDATA monitor records.");
  360. MODULE_LICENSE("GPL");