^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-or-later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * IBM ASM Service Processor Device Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) IBM Corporation, 2004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author: Max Asböck <amax@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/time64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Driver identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define DRIVER_NAME "ibmasm"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define DRIVER_VERSION "1.0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define DRIVER_AUTHOR "Max Asbock <masbock@us.ibm.com>, Vernon Mauery <vernux@us.ibm.com>"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define DRIVER_DESC "IBM ASM Service Processor Driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define info(msg) printk(KERN_INFO "%s: " msg "\n", DRIVER_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) extern int ibmasm_debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define dbg(STR, ARGS...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (ibmasm_debug) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) printk(KERN_DEBUG STR , ##ARGS); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static inline char *get_timestamp(char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct timespec64 now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ktime_get_real_ts64(&now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) sprintf(buf, "%llu.%.08lu", (long long)now.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) now.tv_nsec / NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define IBMASM_CMD_PENDING 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define IBMASM_CMD_COMPLETE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define IBMASM_CMD_FAILED 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define IBMASM_CMD_TIMEOUT_NORMAL 45
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define IBMASM_CMD_TIMEOUT_EXTRA 240
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define IBMASM_CMD_MAX_BUFFER_SIZE 0x8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define REVERSE_HEARTBEAT_TIMEOUT 120
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define HEARTBEAT_BUFFER_SIZE 0x400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #ifdef IA64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define IBMASM_DRIVER_VPD "Lin64 6.08 "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define IBMASM_DRIVER_VPD "Lin32 6.08 "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define SYSTEM_STATE_OS_UP 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define SYSTEM_STATE_OS_DOWN 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define IBMASM_NAME_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define IBMASM_NUM_EVENTS 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define IBMASM_EVENT_MAX_SIZE 2048u
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct command {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct list_head queue_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) size_t buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct kref kref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) spinlock_t *lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #define to_command(c) container_of(c, struct command, kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) void ibmasm_free_command(struct kref *kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static inline void command_put(struct command *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) spinlock_t *lock = cmd->lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) spin_lock_irqsave(lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) kref_put(&cmd->kref, ibmasm_free_command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) spin_unlock_irqrestore(lock, flags);
^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) static inline void command_get(struct command *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) kref_get(&cmd->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^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) struct ibmasm_event {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) unsigned int serial_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) unsigned int data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) unsigned char data[IBMASM_EVENT_MAX_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct event_buffer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct ibmasm_event events[IBMASM_NUM_EVENTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned int next_serial_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned int next_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct list_head readers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct event_reader {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int cancelled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned int next_serial_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) unsigned int data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned char data[IBMASM_EVENT_MAX_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct reverse_heartbeat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned int stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct ibmasm_remote {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct input_dev *keybd_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct input_dev *mouse_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct service_processor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void __iomem *base_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct command *current_command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct command *heartbeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct list_head command_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct event_buffer *event_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) char dirname[IBMASM_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) char devname[IBMASM_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) unsigned int number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct ibmasm_remote remote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int serial_line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* command processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) void ibmasm_exec_command(struct service_processor *sp, struct command *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void ibmasm_wait_for_response(struct command *cmd, int timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* event processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int ibmasm_event_buffer_init(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) void ibmasm_event_buffer_exit(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void ibmasm_receive_event(struct service_processor *sp, void *data, unsigned int data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) void ibmasm_event_reader_register(struct service_processor *sp, struct event_reader *reader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) void ibmasm_event_reader_unregister(struct service_processor *sp, struct event_reader *reader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int ibmasm_get_next_event(struct service_processor *sp, struct event_reader *reader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) void ibmasm_cancel_next_event(struct event_reader *reader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* heartbeat - from SP to OS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) void ibmasm_register_panic_notifier(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) void ibmasm_unregister_panic_notifier(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int ibmasm_heartbeat_init(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) void ibmasm_heartbeat_exit(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) void ibmasm_receive_heartbeat(struct service_processor *sp, void *message, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* reverse heartbeat - from OS to SP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) void ibmasm_init_reverse_heartbeat(struct service_processor *sp, struct reverse_heartbeat *rhb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) int ibmasm_start_reverse_heartbeat(struct service_processor *sp, struct reverse_heartbeat *rhb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) void ibmasm_stop_reverse_heartbeat(struct reverse_heartbeat *rhb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* dot commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) void ibmasm_receive_message(struct service_processor *sp, void *data, int data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int ibmasm_send_driver_vpd(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) int ibmasm_send_os_state(struct service_processor *sp, int os_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* low level message processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int ibmasm_send_i2o_message(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) irqreturn_t ibmasm_interrupt_handler(int irq, void * dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* remote console */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) void ibmasm_handle_mouse_interrupt(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int ibmasm_init_remote_input_dev(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) void ibmasm_free_remote_input_dev(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /* file system */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int ibmasmfs_register(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) void ibmasmfs_unregister(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) void ibmasmfs_add_sp(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* uart */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #if IS_ENABLED(CONFIG_SERIAL_8250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) void ibmasm_register_uart(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) void ibmasm_unregister_uart(struct service_processor *sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #define ibmasm_register_uart(sp) do { } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #define ibmasm_unregister_uart(sp) do { } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) #endif