sclp_async.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Enable Asynchronous Notification via SCLP.
  3. *
  4. * Copyright IBM Corp. 2009
  5. * Author(s): Hans-Joachim Picht <hans@linux.vnet.ibm.com>
  6. *
  7. */
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/stat.h>
  12. #include <linux/string.h>
  13. #include <linux/slab.h>
  14. #include <linux/ctype.h>
  15. #include <linux/kmod.h>
  16. #include <linux/err.h>
  17. #include <linux/errno.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/sysctl.h>
  20. #include <linux/utsname.h>
  21. #include "sclp.h"
  22. static int callhome_enabled;
  23. static struct sclp_req *request;
  24. static struct sclp_async_sccb *sccb;
  25. static int sclp_async_send_wait(char *message);
  26. static struct ctl_table_header *callhome_sysctl_header;
  27. static DEFINE_SPINLOCK(sclp_async_lock);
  28. #define SCLP_NORMAL_WRITE 0x00
  29. struct async_evbuf {
  30. struct evbuf_header header;
  31. u64 reserved;
  32. u8 rflags;
  33. u8 empty;
  34. u8 rtype;
  35. u8 otype;
  36. char comp_id[12];
  37. char data[3000]; /* there is still some space left */
  38. } __attribute__((packed));
  39. struct sclp_async_sccb {
  40. struct sccb_header header;
  41. struct async_evbuf evbuf;
  42. } __attribute__((packed));
  43. static struct sclp_register sclp_async_register = {
  44. .send_mask = EVTYP_ASYNC_MASK,
  45. };
  46. static int call_home_on_panic(struct notifier_block *self,
  47. unsigned long event, void *data)
  48. {
  49. strncat(data, init_utsname()->nodename,
  50. sizeof(init_utsname()->nodename));
  51. sclp_async_send_wait(data);
  52. return NOTIFY_DONE;
  53. }
  54. static struct notifier_block call_home_panic_nb = {
  55. .notifier_call = call_home_on_panic,
  56. .priority = INT_MAX,
  57. };
  58. static int proc_handler_callhome(struct ctl_table *ctl, int write,
  59. void __user *buffer, size_t *count,
  60. loff_t *ppos)
  61. {
  62. unsigned long val;
  63. int len, rc;
  64. char buf[3];
  65. if (!*count || (*ppos && !write)) {
  66. *count = 0;
  67. return 0;
  68. }
  69. if (!write) {
  70. len = snprintf(buf, sizeof(buf), "%d\n", callhome_enabled);
  71. rc = copy_to_user(buffer, buf, sizeof(buf));
  72. if (rc != 0)
  73. return -EFAULT;
  74. } else {
  75. len = *count;
  76. rc = kstrtoul_from_user(buffer, len, 0, &val);
  77. if (rc)
  78. return rc;
  79. if (val != 0 && val != 1)
  80. return -EINVAL;
  81. callhome_enabled = val;
  82. }
  83. *count = len;
  84. *ppos += len;
  85. return 0;
  86. }
  87. static struct ctl_table callhome_table[] = {
  88. {
  89. .procname = "callhome",
  90. .mode = 0644,
  91. .proc_handler = proc_handler_callhome,
  92. },
  93. {}
  94. };
  95. static struct ctl_table kern_dir_table[] = {
  96. {
  97. .procname = "kernel",
  98. .maxlen = 0,
  99. .mode = 0555,
  100. .child = callhome_table,
  101. },
  102. {}
  103. };
  104. /*
  105. * Function used to transfer asynchronous notification
  106. * records which waits for send completion
  107. */
  108. static int sclp_async_send_wait(char *message)
  109. {
  110. struct async_evbuf *evb;
  111. int rc;
  112. unsigned long flags;
  113. if (!callhome_enabled)
  114. return 0;
  115. sccb->evbuf.header.type = EVTYP_ASYNC;
  116. sccb->evbuf.rtype = 0xA5;
  117. sccb->evbuf.otype = 0x00;
  118. evb = &sccb->evbuf;
  119. request->command = SCLP_CMDW_WRITE_EVENT_DATA;
  120. request->sccb = sccb;
  121. request->status = SCLP_REQ_FILLED;
  122. strncpy(sccb->evbuf.data, message, sizeof(sccb->evbuf.data));
  123. /*
  124. * Retain Queue
  125. * e.g. 5639CC140 500 Red Hat RHEL5 Linux for zSeries (RHEL AS)
  126. */
  127. strncpy(sccb->evbuf.comp_id, CONFIG_SCLP_ASYNC_ID,
  128. sizeof(sccb->evbuf.comp_id));
  129. sccb->evbuf.header.length = sizeof(sccb->evbuf);
  130. sccb->header.length = sizeof(sccb->evbuf) + sizeof(sccb->header);
  131. sccb->header.function_code = SCLP_NORMAL_WRITE;
  132. rc = sclp_add_request(request);
  133. if (rc)
  134. return rc;
  135. spin_lock_irqsave(&sclp_async_lock, flags);
  136. while (request->status != SCLP_REQ_DONE &&
  137. request->status != SCLP_REQ_FAILED) {
  138. sclp_sync_wait();
  139. }
  140. spin_unlock_irqrestore(&sclp_async_lock, flags);
  141. if (request->status != SCLP_REQ_DONE)
  142. return -EIO;
  143. rc = ((struct sclp_async_sccb *)
  144. request->sccb)->header.response_code;
  145. if (rc != 0x0020)
  146. return -EIO;
  147. if (evb->header.flags != 0x80)
  148. return -EIO;
  149. return rc;
  150. }
  151. static int __init sclp_async_init(void)
  152. {
  153. int rc;
  154. rc = sclp_register(&sclp_async_register);
  155. if (rc)
  156. return rc;
  157. rc = -EOPNOTSUPP;
  158. if (!(sclp_async_register.sclp_receive_mask & EVTYP_ASYNC_MASK))
  159. goto out_sclp;
  160. rc = -ENOMEM;
  161. callhome_sysctl_header = register_sysctl_table(kern_dir_table);
  162. if (!callhome_sysctl_header)
  163. goto out_sclp;
  164. request = kzalloc(sizeof(struct sclp_req), GFP_KERNEL);
  165. sccb = (struct sclp_async_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  166. if (!request || !sccb)
  167. goto out_mem;
  168. rc = atomic_notifier_chain_register(&panic_notifier_list,
  169. &call_home_panic_nb);
  170. if (!rc)
  171. goto out;
  172. out_mem:
  173. kfree(request);
  174. free_page((unsigned long) sccb);
  175. unregister_sysctl_table(callhome_sysctl_header);
  176. out_sclp:
  177. sclp_unregister(&sclp_async_register);
  178. out:
  179. return rc;
  180. }
  181. module_init(sclp_async_init);
  182. static void __exit sclp_async_exit(void)
  183. {
  184. atomic_notifier_chain_unregister(&panic_notifier_list,
  185. &call_home_panic_nb);
  186. unregister_sysctl_table(callhome_sysctl_header);
  187. sclp_unregister(&sclp_async_register);
  188. free_page((unsigned long) sccb);
  189. kfree(request);
  190. }
  191. module_exit(sclp_async_exit);
  192. MODULE_AUTHOR("Copyright IBM Corp. 2009");
  193. MODULE_AUTHOR("Hans-Joachim Picht <hans@linux.vnet.ibm.com>");
  194. MODULE_LICENSE("GPL");
  195. MODULE_DESCRIPTION("SCLP Asynchronous Notification Records");