monwriter.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. struct mon_buf {
  27. struct list_head list;
  28. struct monwrite_hdr hdr;
  29. int diag_done;
  30. char *data;
  31. };
  32. struct mon_private {
  33. struct list_head list;
  34. struct monwrite_hdr hdr;
  35. size_t hdr_to_read;
  36. size_t data_to_read;
  37. struct mon_buf *current_buf;
  38. int mon_buf_count;
  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.applid == monhdr->applid &&
  67. entry->hdr.record_num == monhdr->record_num &&
  68. entry->hdr.version == monhdr->version &&
  69. entry->hdr.release == monhdr->release &&
  70. entry->hdr.mod_level == monhdr->mod_level)
  71. return entry;
  72. return NULL;
  73. }
  74. static int monwrite_new_hdr(struct mon_private *monpriv)
  75. {
  76. struct monwrite_hdr *monhdr = &monpriv->hdr;
  77. struct mon_buf *monbuf;
  78. int rc;
  79. if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
  80. monhdr->mon_function > MONWRITE_START_CONFIG ||
  81. monhdr->hdrlen != sizeof(struct monwrite_hdr))
  82. return -EINVAL;
  83. monbuf = monwrite_find_hdr(monpriv, monhdr);
  84. if (monbuf) {
  85. if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
  86. monhdr->datalen = monbuf->hdr.datalen;
  87. rc = monwrite_diag(monhdr, monbuf->data,
  88. APPLDATA_STOP_REC);
  89. list_del(&monbuf->list);
  90. monpriv->mon_buf_count--;
  91. kfree(monbuf->data);
  92. kfree(monbuf);
  93. monbuf = NULL;
  94. }
  95. } else {
  96. if (monpriv->mon_buf_count >= mon_max_bufs)
  97. return -ENOSPC;
  98. monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
  99. if (!monbuf)
  100. return -ENOMEM;
  101. monbuf->data = kzalloc(monbuf->hdr.datalen,
  102. GFP_KERNEL | GFP_DMA);
  103. if (!monbuf->data) {
  104. kfree(monbuf);
  105. return -ENOMEM;
  106. }
  107. monbuf->hdr = *monhdr;
  108. list_add_tail(&monbuf->list, &monpriv->list);
  109. monpriv->mon_buf_count++;
  110. }
  111. monpriv->current_buf = monbuf;
  112. return 0;
  113. }
  114. static int monwrite_new_data(struct mon_private *monpriv)
  115. {
  116. struct monwrite_hdr *monhdr = &monpriv->hdr;
  117. struct mon_buf *monbuf = monpriv->current_buf;
  118. int rc = 0;
  119. switch (monhdr->mon_function) {
  120. case MONWRITE_START_INTERVAL:
  121. if (!monbuf->diag_done) {
  122. rc = monwrite_diag(monhdr, monbuf->data,
  123. APPLDATA_START_INTERVAL_REC);
  124. monbuf->diag_done = 1;
  125. }
  126. break;
  127. case MONWRITE_START_CONFIG:
  128. if (!monbuf->diag_done) {
  129. rc = monwrite_diag(monhdr, monbuf->data,
  130. APPLDATA_START_CONFIG_REC);
  131. monbuf->diag_done = 1;
  132. }
  133. break;
  134. case MONWRITE_GEN_EVENT:
  135. rc = monwrite_diag(monhdr, monbuf->data,
  136. APPLDATA_GEN_EVENT_REC);
  137. list_del(&monpriv->current_buf->list);
  138. kfree(monpriv->current_buf->data);
  139. kfree(monpriv->current_buf);
  140. monpriv->current_buf = NULL;
  141. break;
  142. default:
  143. /* monhdr->mon_function is checked in monwrite_new_hdr */
  144. BUG();
  145. }
  146. return rc;
  147. }
  148. /*
  149. * file operations
  150. */
  151. static int monwrite_open(struct inode *inode, struct file *filp)
  152. {
  153. struct mon_private *monpriv;
  154. monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
  155. if (!monpriv)
  156. return -ENOMEM;
  157. INIT_LIST_HEAD(&monpriv->list);
  158. monpriv->hdr_to_read = sizeof(monpriv->hdr);
  159. filp->private_data = monpriv;
  160. return nonseekable_open(inode, filp);
  161. }
  162. static int monwrite_close(struct inode *inode, struct file *filp)
  163. {
  164. struct mon_private *monpriv = filp->private_data;
  165. struct mon_buf *entry, *next;
  166. list_for_each_entry_safe(entry, next, &monpriv->list, list) {
  167. if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
  168. monwrite_diag(&entry->hdr, entry->data,
  169. APPLDATA_STOP_REC);
  170. monpriv->mon_buf_count--;
  171. list_del(&entry->list);
  172. kfree(entry->data);
  173. kfree(entry);
  174. }
  175. kfree(monpriv);
  176. return 0;
  177. }
  178. static ssize_t monwrite_write(struct file *filp, const char __user *data,
  179. size_t count, loff_t *ppos)
  180. {
  181. struct mon_private *monpriv = filp->private_data;
  182. size_t len, written;
  183. void *to;
  184. int rc;
  185. for (written = 0; written < count; ) {
  186. if (monpriv->hdr_to_read) {
  187. len = min(count - written, monpriv->hdr_to_read);
  188. to = (char *) &monpriv->hdr +
  189. sizeof(monpriv->hdr) - monpriv->hdr_to_read;
  190. if (copy_from_user(to, data + written, len)) {
  191. rc = -EFAULT;
  192. goto out_error;
  193. }
  194. monpriv->hdr_to_read -= len;
  195. written += len;
  196. if (monpriv->hdr_to_read > 0)
  197. continue;
  198. rc = monwrite_new_hdr(monpriv);
  199. if (rc)
  200. goto out_error;
  201. monpriv->data_to_read = monpriv->current_buf ?
  202. monpriv->current_buf->hdr.datalen : 0;
  203. }
  204. if (monpriv->data_to_read) {
  205. len = min(count - written, monpriv->data_to_read);
  206. to = monpriv->current_buf->data +
  207. monpriv->hdr.datalen - monpriv->data_to_read;
  208. if (copy_from_user(to, data + written, len)) {
  209. rc = -EFAULT;
  210. goto out_error;
  211. }
  212. monpriv->data_to_read -= len;
  213. written += len;
  214. if (monpriv->data_to_read > 0)
  215. continue;
  216. rc = monwrite_new_data(monpriv);
  217. if (rc)
  218. goto out_error;
  219. }
  220. monpriv->hdr_to_read = sizeof(monpriv->hdr);
  221. }
  222. return written;
  223. out_error:
  224. monpriv->data_to_read = 0;
  225. monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
  226. return rc;
  227. }
  228. static struct file_operations monwrite_fops = {
  229. .owner = THIS_MODULE,
  230. .open = &monwrite_open,
  231. .release = &monwrite_close,
  232. .write = &monwrite_write,
  233. };
  234. static struct miscdevice mon_dev = {
  235. .name = "monwriter",
  236. .fops = &monwrite_fops,
  237. .minor = MISC_DYNAMIC_MINOR,
  238. };
  239. /*
  240. * module init/exit
  241. */
  242. static int __init mon_init(void)
  243. {
  244. if (MACHINE_IS_VM)
  245. return misc_register(&mon_dev);
  246. else
  247. return -ENODEV;
  248. }
  249. static void __exit mon_exit(void)
  250. {
  251. WARN_ON(misc_deregister(&mon_dev) != 0);
  252. }
  253. module_init(mon_init);
  254. module_exit(mon_exit);
  255. module_param_named(max_bufs, mon_max_bufs, int, 0644);
  256. MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers"
  257. "that can be active at one time");
  258. MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
  259. MODULE_DESCRIPTION("Character device driver for writing z/VM "
  260. "APPLDATA monitor records.");
  261. MODULE_LICENSE("GPL");