162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci/*
462306a36Sopenharmony_ci * IBM ASM Service Processor Device Driver
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) IBM Corporation, 2004
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Author: Max Asböck <amax@us.ibm.com>
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/notifier.h>
1262306a36Sopenharmony_ci#include <linux/panic_notifier.h>
1362306a36Sopenharmony_ci#include "ibmasm.h"
1462306a36Sopenharmony_ci#include "dot_command.h"
1562306a36Sopenharmony_ci#include "lowlevel.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistatic int suspend_heartbeats = 0;
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/*
2062306a36Sopenharmony_ci * Once the driver indicates to the service processor that it is running
2162306a36Sopenharmony_ci * - see send_os_state() - the service processor sends periodic heartbeats
2262306a36Sopenharmony_ci * to the driver. The driver must respond to the heartbeats or else the OS
2362306a36Sopenharmony_ci * will be rebooted.
2462306a36Sopenharmony_ci * In the case of a panic the interrupt handler continues to work and thus
2562306a36Sopenharmony_ci * continues to respond to heartbeats, making the service processor believe
2662306a36Sopenharmony_ci * the OS is still running and thus preventing a reboot.
2762306a36Sopenharmony_ci * To prevent this from happening a callback is added the panic_notifier_list.
2862306a36Sopenharmony_ci * Before responding to a heartbeat the driver checks if a panic has happened,
2962306a36Sopenharmony_ci * if yes it suspends heartbeat, causing the service processor to reboot as
3062306a36Sopenharmony_ci * expected.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_cistatic int panic_happened(struct notifier_block *n, unsigned long val, void *v)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	suspend_heartbeats = 1;
3562306a36Sopenharmony_ci	return 0;
3662306a36Sopenharmony_ci}
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic struct notifier_block panic_notifier = { panic_happened, NULL, 1 };
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_civoid ibmasm_register_panic_notifier(void)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	atomic_notifier_chain_register(&panic_notifier_list, &panic_notifier);
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_civoid ibmasm_unregister_panic_notifier(void)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	atomic_notifier_chain_unregister(&panic_notifier_list,
4862306a36Sopenharmony_ci			&panic_notifier);
4962306a36Sopenharmony_ci}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciint ibmasm_heartbeat_init(struct service_processor *sp)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	sp->heartbeat = ibmasm_new_command(sp, HEARTBEAT_BUFFER_SIZE);
5562306a36Sopenharmony_ci	if (sp->heartbeat == NULL)
5662306a36Sopenharmony_ci		return -ENOMEM;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	return 0;
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_civoid ibmasm_heartbeat_exit(struct service_processor *sp)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	char tsbuf[32];
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
6662306a36Sopenharmony_ci	ibmasm_wait_for_response(sp->heartbeat, IBMASM_CMD_TIMEOUT_NORMAL);
6762306a36Sopenharmony_ci	dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
6862306a36Sopenharmony_ci	suspend_heartbeats = 1;
6962306a36Sopenharmony_ci	command_put(sp->heartbeat);
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_civoid ibmasm_receive_heartbeat(struct service_processor *sp,  void *message, size_t size)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	struct command *cmd = sp->heartbeat;
7562306a36Sopenharmony_ci	struct dot_command_header *header = (struct dot_command_header *)cmd->buffer;
7662306a36Sopenharmony_ci	char tsbuf[32];
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
7962306a36Sopenharmony_ci	if (suspend_heartbeats)
8062306a36Sopenharmony_ci		return;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	/* return the received dot command to sender */
8362306a36Sopenharmony_ci	cmd->status = IBMASM_CMD_PENDING;
8462306a36Sopenharmony_ci	size = min(size, cmd->buffer_size);
8562306a36Sopenharmony_ci	memcpy_fromio(cmd->buffer, message, size);
8662306a36Sopenharmony_ci	header->type = sp_write;
8762306a36Sopenharmony_ci	ibmasm_exec_command(sp, cmd);
8862306a36Sopenharmony_ci}
89