monwriter.c 9.5 KB

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