svc_watchdog.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SVC Greybus "watchdog" driver.
  4. *
  5. * Copyright 2016 Google Inc.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/suspend.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/greybus.h>
  11. #define SVC_WATCHDOG_PERIOD (2 * HZ)
  12. struct gb_svc_watchdog {
  13. struct delayed_work work;
  14. struct gb_svc *svc;
  15. bool enabled;
  16. struct notifier_block pm_notifier;
  17. };
  18. static struct delayed_work reset_work;
  19. static int svc_watchdog_pm_notifier(struct notifier_block *notifier,
  20. unsigned long pm_event, void *unused)
  21. {
  22. struct gb_svc_watchdog *watchdog =
  23. container_of(notifier, struct gb_svc_watchdog, pm_notifier);
  24. switch (pm_event) {
  25. case PM_SUSPEND_PREPARE:
  26. gb_svc_watchdog_disable(watchdog->svc);
  27. break;
  28. case PM_POST_SUSPEND:
  29. gb_svc_watchdog_enable(watchdog->svc);
  30. break;
  31. default:
  32. break;
  33. }
  34. return NOTIFY_DONE;
  35. }
  36. static void greybus_reset(struct work_struct *work)
  37. {
  38. static char const start_path[] = "/system/bin/start";
  39. static char *envp[] = {
  40. "HOME=/",
  41. "PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin",
  42. NULL,
  43. };
  44. static char *argv[] = {
  45. (char *)start_path,
  46. "unipro_reset",
  47. NULL,
  48. };
  49. pr_err("svc_watchdog: calling \"%s %s\" to reset greybus network!\n",
  50. argv[0], argv[1]);
  51. call_usermodehelper(start_path, argv, envp, UMH_WAIT_EXEC);
  52. }
  53. static void do_work(struct work_struct *work)
  54. {
  55. struct gb_svc_watchdog *watchdog;
  56. struct gb_svc *svc;
  57. int retval;
  58. watchdog = container_of(work, struct gb_svc_watchdog, work.work);
  59. svc = watchdog->svc;
  60. dev_dbg(&svc->dev, "%s: ping.\n", __func__);
  61. retval = gb_svc_ping(svc);
  62. if (retval) {
  63. /*
  64. * Something went really wrong, let's warn userspace and then
  65. * pull the plug and reset the whole greybus network.
  66. * We need to do this outside of this workqueue as we will be
  67. * tearing down the svc device itself. So queue up
  68. * yet-another-callback to do that.
  69. */
  70. dev_err(&svc->dev,
  71. "SVC ping has returned %d, something is wrong!!!\n",
  72. retval);
  73. if (svc->action == GB_SVC_WATCHDOG_BITE_PANIC_KERNEL) {
  74. panic("SVC is not responding\n");
  75. } else if (svc->action == GB_SVC_WATCHDOG_BITE_RESET_UNIPRO) {
  76. dev_err(&svc->dev, "Resetting the greybus network, watch out!!!\n");
  77. INIT_DELAYED_WORK(&reset_work, greybus_reset);
  78. schedule_delayed_work(&reset_work, HZ / 2);
  79. /*
  80. * Disable ourselves, we don't want to trip again unless
  81. * userspace wants us to.
  82. */
  83. watchdog->enabled = false;
  84. }
  85. }
  86. /* resubmit our work to happen again, if we are still "alive" */
  87. if (watchdog->enabled)
  88. schedule_delayed_work(&watchdog->work, SVC_WATCHDOG_PERIOD);
  89. }
  90. int gb_svc_watchdog_create(struct gb_svc *svc)
  91. {
  92. struct gb_svc_watchdog *watchdog;
  93. int retval;
  94. if (svc->watchdog)
  95. return 0;
  96. watchdog = kmalloc(sizeof(*watchdog), GFP_KERNEL);
  97. if (!watchdog)
  98. return -ENOMEM;
  99. watchdog->enabled = false;
  100. watchdog->svc = svc;
  101. INIT_DELAYED_WORK(&watchdog->work, do_work);
  102. svc->watchdog = watchdog;
  103. watchdog->pm_notifier.notifier_call = svc_watchdog_pm_notifier;
  104. retval = register_pm_notifier(&watchdog->pm_notifier);
  105. if (retval) {
  106. dev_err(&svc->dev, "error registering pm notifier(%d)\n",
  107. retval);
  108. goto svc_watchdog_create_err;
  109. }
  110. retval = gb_svc_watchdog_enable(svc);
  111. if (retval) {
  112. dev_err(&svc->dev, "error enabling watchdog (%d)\n", retval);
  113. unregister_pm_notifier(&watchdog->pm_notifier);
  114. goto svc_watchdog_create_err;
  115. }
  116. return retval;
  117. svc_watchdog_create_err:
  118. svc->watchdog = NULL;
  119. kfree(watchdog);
  120. return retval;
  121. }
  122. void gb_svc_watchdog_destroy(struct gb_svc *svc)
  123. {
  124. struct gb_svc_watchdog *watchdog = svc->watchdog;
  125. if (!watchdog)
  126. return;
  127. unregister_pm_notifier(&watchdog->pm_notifier);
  128. gb_svc_watchdog_disable(svc);
  129. svc->watchdog = NULL;
  130. kfree(watchdog);
  131. }
  132. bool gb_svc_watchdog_enabled(struct gb_svc *svc)
  133. {
  134. if (!svc || !svc->watchdog)
  135. return false;
  136. return svc->watchdog->enabled;
  137. }
  138. int gb_svc_watchdog_enable(struct gb_svc *svc)
  139. {
  140. struct gb_svc_watchdog *watchdog;
  141. if (!svc->watchdog)
  142. return -ENODEV;
  143. watchdog = svc->watchdog;
  144. if (watchdog->enabled)
  145. return 0;
  146. watchdog->enabled = true;
  147. schedule_delayed_work(&watchdog->work, SVC_WATCHDOG_PERIOD);
  148. return 0;
  149. }
  150. int gb_svc_watchdog_disable(struct gb_svc *svc)
  151. {
  152. struct gb_svc_watchdog *watchdog;
  153. if (!svc->watchdog)
  154. return -ENODEV;
  155. watchdog = svc->watchdog;
  156. if (!watchdog->enabled)
  157. return 0;
  158. watchdog->enabled = false;
  159. cancel_delayed_work_sync(&watchdog->work);
  160. return 0;
  161. }