xref: /OK3568_Linux_fs/kernel/drivers/misc/ibmasm/heartbeat.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun  * IBM ASM Service Processor Device Driver
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) IBM Corporation, 2004
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Author: Max Asböck <amax@us.ibm.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/notifier.h>
12*4882a593Smuzhiyun #include "ibmasm.h"
13*4882a593Smuzhiyun #include "dot_command.h"
14*4882a593Smuzhiyun #include "lowlevel.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun static int suspend_heartbeats = 0;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * Once the driver indicates to the service processor that it is running
20*4882a593Smuzhiyun  * - see send_os_state() - the service processor sends periodic heartbeats
21*4882a593Smuzhiyun  * to the driver. The driver must respond to the heartbeats or else the OS
22*4882a593Smuzhiyun  * will be rebooted.
23*4882a593Smuzhiyun  * In the case of a panic the interrupt handler continues to work and thus
24*4882a593Smuzhiyun  * continues to respond to heartbeats, making the service processor believe
25*4882a593Smuzhiyun  * the OS is still running and thus preventing a reboot.
26*4882a593Smuzhiyun  * To prevent this from happening a callback is added the panic_notifier_list.
27*4882a593Smuzhiyun  * Before responding to a heartbeat the driver checks if a panic has happened,
28*4882a593Smuzhiyun  * if yes it suspends heartbeat, causing the service processor to reboot as
29*4882a593Smuzhiyun  * expected.
30*4882a593Smuzhiyun  */
panic_happened(struct notifier_block * n,unsigned long val,void * v)31*4882a593Smuzhiyun static int panic_happened(struct notifier_block *n, unsigned long val, void *v)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	suspend_heartbeats = 1;
34*4882a593Smuzhiyun 	return 0;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static struct notifier_block panic_notifier = { panic_happened, NULL, 1 };
38*4882a593Smuzhiyun 
ibmasm_register_panic_notifier(void)39*4882a593Smuzhiyun void ibmasm_register_panic_notifier(void)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	atomic_notifier_chain_register(&panic_notifier_list, &panic_notifier);
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
ibmasm_unregister_panic_notifier(void)44*4882a593Smuzhiyun void ibmasm_unregister_panic_notifier(void)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	atomic_notifier_chain_unregister(&panic_notifier_list,
47*4882a593Smuzhiyun 			&panic_notifier);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 
ibmasm_heartbeat_init(struct service_processor * sp)51*4882a593Smuzhiyun int ibmasm_heartbeat_init(struct service_processor *sp)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	sp->heartbeat = ibmasm_new_command(sp, HEARTBEAT_BUFFER_SIZE);
54*4882a593Smuzhiyun 	if (sp->heartbeat == NULL)
55*4882a593Smuzhiyun 		return -ENOMEM;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	return 0;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
ibmasm_heartbeat_exit(struct service_processor * sp)60*4882a593Smuzhiyun void ibmasm_heartbeat_exit(struct service_processor *sp)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	char tsbuf[32];
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
65*4882a593Smuzhiyun 	ibmasm_wait_for_response(sp->heartbeat, IBMASM_CMD_TIMEOUT_NORMAL);
66*4882a593Smuzhiyun 	dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
67*4882a593Smuzhiyun 	suspend_heartbeats = 1;
68*4882a593Smuzhiyun 	command_put(sp->heartbeat);
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
ibmasm_receive_heartbeat(struct service_processor * sp,void * message,size_t size)71*4882a593Smuzhiyun void ibmasm_receive_heartbeat(struct service_processor *sp,  void *message, size_t size)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	struct command *cmd = sp->heartbeat;
74*4882a593Smuzhiyun 	struct dot_command_header *header = (struct dot_command_header *)cmd->buffer;
75*4882a593Smuzhiyun 	char tsbuf[32];
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
78*4882a593Smuzhiyun 	if (suspend_heartbeats)
79*4882a593Smuzhiyun 		return;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/* return the received dot command to sender */
82*4882a593Smuzhiyun 	cmd->status = IBMASM_CMD_PENDING;
83*4882a593Smuzhiyun 	size = min(size, cmd->buffer_size);
84*4882a593Smuzhiyun 	memcpy_fromio(cmd->buffer, message, size);
85*4882a593Smuzhiyun 	header->type = sp_write;
86*4882a593Smuzhiyun 	ibmasm_exec_command(sp, cmd);
87*4882a593Smuzhiyun }
88