monwriter.c 9.5 KB

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