monwriter.c 7.3 KB

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