monwriter.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * drivers/s390/char/monwriter.c
  3. *
  4. * Character device driver for writing z/VM *MONITOR service records.
  5. *
  6. * Copyright (C) IBM Corp. 2006
  7. *
  8. * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
  9. */
  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 <asm/uaccess.h>
  20. #include <asm/ebcdic.h>
  21. #include <asm/io.h>
  22. #include <asm/appldata.h>
  23. #include <asm/monwriter.h>
  24. #define MONWRITE_MAX_DATALEN 4024
  25. static int mon_max_bufs = 255;
  26. static int mon_buf_count;
  27. struct mon_buf {
  28. struct list_head list;
  29. struct monwrite_hdr hdr;
  30. int diag_done;
  31. char *data;
  32. };
  33. struct mon_private {
  34. struct list_head list;
  35. struct monwrite_hdr hdr;
  36. size_t hdr_to_read;
  37. size_t data_to_read;
  38. struct mon_buf *current_buf;
  39. };
  40. /*
  41. * helper functions
  42. */
  43. static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
  44. {
  45. struct appldata_product_id id;
  46. int rc;
  47. strcpy(id.prod_nr, "LNXAPPL");
  48. id.prod_fn = myhdr->applid;
  49. id.record_nr = myhdr->record_num;
  50. id.version_nr = myhdr->version;
  51. id.release_nr = myhdr->release;
  52. id.mod_lvl = myhdr->mod_level;
  53. rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen);
  54. if (rc <= 0)
  55. return rc;
  56. if (rc == 5)
  57. return -EPERM;
  58. printk("DIAG X'DC' error with return code: %i\n", rc);
  59. return -EINVAL;
  60. }
  61. static inline struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
  62. struct monwrite_hdr *monhdr)
  63. {
  64. struct mon_buf *entry, *next;
  65. list_for_each_entry_safe(entry, next, &monpriv->list, list)
  66. if ((entry->hdr.mon_function == monhdr->mon_function ||
  67. monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
  68. entry->hdr.applid == monhdr->applid &&
  69. entry->hdr.record_num == monhdr->record_num &&
  70. entry->hdr.version == monhdr->version &&
  71. entry->hdr.release == monhdr->release &&
  72. entry->hdr.mod_level == monhdr->mod_level)
  73. return entry;
  74. return NULL;
  75. }
  76. static int monwrite_new_hdr(struct mon_private *monpriv)
  77. {
  78. struct monwrite_hdr *monhdr = &monpriv->hdr;
  79. struct mon_buf *monbuf;
  80. int rc;
  81. if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
  82. monhdr->mon_function > MONWRITE_START_CONFIG ||
  83. monhdr->hdrlen != sizeof(struct monwrite_hdr))
  84. return -EINVAL;
  85. monbuf = NULL;
  86. if (monhdr->mon_function != MONWRITE_GEN_EVENT)
  87. monbuf = monwrite_find_hdr(monpriv, monhdr);
  88. if (monbuf) {
  89. if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
  90. monhdr->datalen = monbuf->hdr.datalen;
  91. rc = monwrite_diag(monhdr, monbuf->data,
  92. APPLDATA_STOP_REC);
  93. list_del(&monbuf->list);
  94. mon_buf_count--;
  95. kfree(monbuf->data);
  96. kfree(monbuf);
  97. monbuf = NULL;
  98. }
  99. } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
  100. if (mon_buf_count >= mon_max_bufs)
  101. return -ENOSPC;
  102. monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
  103. if (!monbuf)
  104. return -ENOMEM;
  105. monbuf->data = kzalloc(monhdr->datalen,
  106. GFP_KERNEL | GFP_DMA);
  107. if (!monbuf->data) {
  108. kfree(monbuf);
  109. return -ENOMEM;
  110. }
  111. monbuf->hdr = *monhdr;
  112. list_add_tail(&monbuf->list, &monpriv->list);
  113. if (monhdr->mon_function != MONWRITE_GEN_EVENT)
  114. mon_buf_count++;
  115. }
  116. monpriv->current_buf = monbuf;
  117. return 0;
  118. }
  119. static int monwrite_new_data(struct mon_private *monpriv)
  120. {
  121. struct monwrite_hdr *monhdr = &monpriv->hdr;
  122. struct mon_buf *monbuf = monpriv->current_buf;
  123. int rc = 0;
  124. switch (monhdr->mon_function) {
  125. case MONWRITE_START_INTERVAL:
  126. if (!monbuf->diag_done) {
  127. rc = monwrite_diag(monhdr, monbuf->data,
  128. APPLDATA_START_INTERVAL_REC);
  129. monbuf->diag_done = 1;
  130. }
  131. break;
  132. case MONWRITE_START_CONFIG:
  133. if (!monbuf->diag_done) {
  134. rc = monwrite_diag(monhdr, monbuf->data,
  135. APPLDATA_START_CONFIG_REC);
  136. monbuf->diag_done = 1;
  137. }
  138. break;
  139. case MONWRITE_GEN_EVENT:
  140. rc = monwrite_diag(monhdr, monbuf->data,
  141. APPLDATA_GEN_EVENT_REC);
  142. list_del(&monpriv->current_buf->list);
  143. kfree(monpriv->current_buf->data);
  144. kfree(monpriv->current_buf);
  145. monpriv->current_buf = NULL;
  146. break;
  147. default:
  148. /* monhdr->mon_function is checked in monwrite_new_hdr */
  149. BUG();
  150. }
  151. return rc;
  152. }
  153. /*
  154. * file operations
  155. */
  156. static int monwrite_open(struct inode *inode, struct file *filp)
  157. {
  158. struct mon_private *monpriv;
  159. monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
  160. if (!monpriv)
  161. return -ENOMEM;
  162. INIT_LIST_HEAD(&monpriv->list);
  163. monpriv->hdr_to_read = sizeof(monpriv->hdr);
  164. filp->private_data = monpriv;
  165. return nonseekable_open(inode, filp);
  166. }
  167. static int monwrite_close(struct inode *inode, struct file *filp)
  168. {
  169. struct mon_private *monpriv = filp->private_data;
  170. struct mon_buf *entry, *next;
  171. list_for_each_entry_safe(entry, next, &monpriv->list, list) {
  172. if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
  173. monwrite_diag(&entry->hdr, entry->data,
  174. APPLDATA_STOP_REC);
  175. mon_buf_count--;
  176. list_del(&entry->list);
  177. kfree(entry->data);
  178. kfree(entry);
  179. }
  180. kfree(monpriv);
  181. return 0;
  182. }
  183. static ssize_t monwrite_write(struct file *filp, const char __user *data,
  184. size_t count, loff_t *ppos)
  185. {
  186. struct mon_private *monpriv = filp->private_data;
  187. size_t len, written;
  188. void *to;
  189. int rc;
  190. for (written = 0; written < count; ) {
  191. if (monpriv->hdr_to_read) {
  192. len = min(count - written, monpriv->hdr_to_read);
  193. to = (char *) &monpriv->hdr +
  194. sizeof(monpriv->hdr) - monpriv->hdr_to_read;
  195. if (copy_from_user(to, data + written, len)) {
  196. rc = -EFAULT;
  197. goto out_error;
  198. }
  199. monpriv->hdr_to_read -= len;
  200. written += len;
  201. if (monpriv->hdr_to_read > 0)
  202. continue;
  203. rc = monwrite_new_hdr(monpriv);
  204. if (rc)
  205. goto out_error;
  206. monpriv->data_to_read = monpriv->current_buf ?
  207. monpriv->current_buf->hdr.datalen : 0;
  208. }
  209. if (monpriv->data_to_read) {
  210. len = min(count - written, monpriv->data_to_read);
  211. to = monpriv->current_buf->data +
  212. monpriv->hdr.datalen - monpriv->data_to_read;
  213. if (copy_from_user(to, data + written, len)) {
  214. rc = -EFAULT;
  215. goto out_error;
  216. }
  217. monpriv->data_to_read -= len;
  218. written += len;
  219. if (monpriv->data_to_read > 0)
  220. continue;
  221. rc = monwrite_new_data(monpriv);
  222. if (rc)
  223. goto out_error;
  224. }
  225. monpriv->hdr_to_read = sizeof(monpriv->hdr);
  226. }
  227. return written;
  228. out_error:
  229. monpriv->data_to_read = 0;
  230. monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
  231. return rc;
  232. }
  233. static struct file_operations monwrite_fops = {
  234. .owner = THIS_MODULE,
  235. .open = &monwrite_open,
  236. .release = &monwrite_close,
  237. .write = &monwrite_write,
  238. };
  239. static struct miscdevice mon_dev = {
  240. .name = "monwriter",
  241. .fops = &monwrite_fops,
  242. .minor = MISC_DYNAMIC_MINOR,
  243. };
  244. /*
  245. * module init/exit
  246. */
  247. static int __init mon_init(void)
  248. {
  249. if (MACHINE_IS_VM)
  250. return misc_register(&mon_dev);
  251. else
  252. return -ENODEV;
  253. }
  254. static void __exit mon_exit(void)
  255. {
  256. WARN_ON(misc_deregister(&mon_dev) != 0);
  257. }
  258. module_init(mon_init);
  259. module_exit(mon_exit);
  260. module_param_named(max_bufs, mon_max_bufs, int, 0644);
  261. MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers"
  262. "that can be active at one time");
  263. MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
  264. MODULE_DESCRIPTION("Character device driver for writing z/VM "
  265. "APPLDATA monitor records.");
  266. MODULE_LICENSE("GPL");