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