Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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