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