monwriter.c 7.4 KB

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