Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Character device driver for reading z/VM *MONITOR service records. |
| 3 | * |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2004, 2009 |
| 5 | * |
| 6 | * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 9 | #define KMSG_COMPONENT "monreader" |
| 10 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/init.h> |
Arnd Bergmann | 6ce46a4 | 2008-05-20 19:16:17 +0200 | [diff] [blame] | 15 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/interrupt.h> |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 23 | #include <linux/poll.h> |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 24 | #include <linux/device.h> |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 25 | #include <net/iucv/iucv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/uaccess.h> |
| 27 | #include <asm/ebcdic.h> |
| 28 | #include <asm/extmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #define MON_COLLECT_SAMPLE 0x80 |
| 32 | #define MON_COLLECT_EVENT 0x40 |
| 33 | #define MON_SERVICE "*MONITOR" |
| 34 | #define MON_IN_USE 0x01 |
| 35 | #define MON_MSGLIM 255 |
| 36 | |
| 37 | static char mon_dcss_name[9] = "MONDCSS\0"; |
| 38 | |
| 39 | struct mon_msg { |
| 40 | u32 pos; |
| 41 | u32 mca_offset; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 42 | struct iucv_message msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | char msglim_reached; |
| 44 | char replied_msglim; |
| 45 | }; |
| 46 | |
| 47 | struct mon_private { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 48 | struct iucv_path *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | struct mon_msg *msg_array[MON_MSGLIM]; |
| 50 | unsigned int write_index; |
| 51 | unsigned int read_index; |
| 52 | atomic_t msglim_count; |
| 53 | atomic_t read_ready; |
| 54 | atomic_t iucv_connected; |
| 55 | atomic_t iucv_severed; |
| 56 | }; |
| 57 | |
| 58 | static unsigned long mon_in_use = 0; |
| 59 | |
| 60 | static unsigned long mon_dcss_start; |
| 61 | static unsigned long mon_dcss_end; |
| 62 | |
| 63 | static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue); |
| 64 | static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue); |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static u8 user_data_connect[16] = { |
| 67 | /* Version code, must be 0x01 for shared mode */ |
| 68 | 0x01, |
| 69 | /* what to collect */ |
| 70 | MON_COLLECT_SAMPLE | MON_COLLECT_EVENT, |
| 71 | /* DCSS name in EBCDIC, 8 bytes padded with blanks */ |
| 72 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 73 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 74 | }; |
| 75 | |
| 76 | static u8 user_data_sever[16] = { |
| 77 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 78 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 79 | }; |
| 80 | |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 81 | static struct device *monreader_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /****************************************************************************** |
| 84 | * helper functions * |
| 85 | *****************************************************************************/ |
| 86 | /* |
| 87 | * Create the 8 bytes EBCDIC DCSS segment name from |
| 88 | * an ASCII name, incl. padding |
| 89 | */ |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 90 | static void dcss_mkname(char *ascii_name, char *ebcdic_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
| 92 | int i; |
| 93 | |
| 94 | for (i = 0; i < 8; i++) { |
| 95 | if (ascii_name[i] == '\0') |
| 96 | break; |
| 97 | ebcdic_name[i] = toupper(ascii_name[i]); |
| 98 | }; |
| 99 | for (; i < 8; i++) |
| 100 | ebcdic_name[i] = ' '; |
| 101 | ASCEBC(ebcdic_name, 8); |
| 102 | } |
| 103 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 104 | static inline unsigned long mon_mca_start(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 106 | return *(u32 *) &monmsg->msg.rmmsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 109 | static inline unsigned long mon_mca_end(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 111 | return *(u32 *) &monmsg->msg.rmmsg[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 114 | static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index); |
| 117 | } |
| 118 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 119 | static inline u32 mon_mca_size(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
| 121 | return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1; |
| 122 | } |
| 123 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 124 | static inline u32 mon_rec_start(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4)); |
| 127 | } |
| 128 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 129 | static inline u32 mon_rec_end(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
| 131 | return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8)); |
| 132 | } |
| 133 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 134 | static int mon_check_mca(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) || |
| 137 | (mon_rec_start(monmsg) < mon_dcss_start) || |
| 138 | (mon_rec_end(monmsg) > mon_dcss_end) || |
| 139 | (mon_mca_type(monmsg, 0) == 0) || |
| 140 | (mon_mca_size(monmsg) % 12 != 0) || |
| 141 | (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) || |
| 142 | (mon_mca_end(monmsg) > mon_dcss_end) || |
| 143 | (mon_mca_start(monmsg) < mon_dcss_start) || |
| 144 | ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 149 | static int mon_send_reply(struct mon_msg *monmsg, |
| 150 | struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | int rc; |
| 153 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 154 | rc = iucv_message_reply(monpriv->path, &monmsg->msg, |
| 155 | IUCV_IPRMDATA, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | atomic_dec(&monpriv->msglim_count); |
| 157 | if (likely(!monmsg->msglim_reached)) { |
| 158 | monmsg->pos = 0; |
| 159 | monmsg->mca_offset = 0; |
| 160 | monpriv->read_index = (monpriv->read_index + 1) % |
| 161 | MON_MSGLIM; |
| 162 | atomic_dec(&monpriv->read_ready); |
| 163 | } else |
| 164 | monmsg->replied_msglim = 1; |
| 165 | if (rc) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 166 | pr_err("Reading monitor data failed with rc=%i\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return -EIO; |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 172 | static void mon_free_mem(struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 174 | int i; |
| 175 | |
| 176 | for (i = 0; i < MON_MSGLIM; i++) |
| 177 | if (monpriv->msg_array[i]) |
| 178 | kfree(monpriv->msg_array[i]); |
| 179 | kfree(monpriv); |
| 180 | } |
| 181 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 182 | static struct mon_private *mon_alloc_mem(void) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 183 | { |
| 184 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | struct mon_private *monpriv; |
| 186 | |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 187 | monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL); |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 188 | if (!monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | for (i = 0; i < MON_MSGLIM; i++) { |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 191 | monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | GFP_KERNEL); |
| 193 | if (!monpriv->msg_array[i]) { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 194 | mon_free_mem(monpriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return NULL; |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | return monpriv; |
| 199 | } |
| 200 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 201 | static inline void mon_next_mca(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12)) |
| 204 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | monmsg->mca_offset += 12; |
| 206 | monmsg->pos = 0; |
| 207 | } |
| 208 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 209 | static struct mon_msg *mon_next_message(struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | struct mon_msg *monmsg; |
| 212 | |
| 213 | if (!atomic_read(&monpriv->read_ready)) |
| 214 | return NULL; |
| 215 | monmsg = monpriv->msg_array[monpriv->read_index]; |
| 216 | if (unlikely(monmsg->replied_msglim)) { |
| 217 | monmsg->replied_msglim = 0; |
| 218 | monmsg->msglim_reached = 0; |
| 219 | monmsg->pos = 0; |
| 220 | monmsg->mca_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | monpriv->read_index = (monpriv->read_index + 1) % |
| 222 | MON_MSGLIM; |
| 223 | atomic_dec(&monpriv->read_ready); |
| 224 | return ERR_PTR(-EOVERFLOW); |
| 225 | } |
| 226 | return monmsg; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /****************************************************************************** |
| 231 | * IUCV handler * |
| 232 | *****************************************************************************/ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 233 | static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 235 | struct mon_private *monpriv = path->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | atomic_set(&monpriv->iucv_connected, 1); |
| 238 | wake_up(&mon_conn_wait_queue); |
| 239 | } |
| 240 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 241 | static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 243 | struct mon_private *monpriv = path->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 245 | pr_err("z/VM *MONITOR system service disconnected with rc=%i\n", |
| 246 | ipuser[0]); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 247 | iucv_path_sever(path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | atomic_set(&monpriv->iucv_severed, 1); |
| 249 | wake_up(&mon_conn_wait_queue); |
| 250 | wake_up_interruptible(&mon_read_wait_queue); |
| 251 | } |
| 252 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 253 | static void mon_iucv_message_pending(struct iucv_path *path, |
| 254 | struct iucv_message *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 256 | struct mon_private *monpriv = path->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 258 | memcpy(&monpriv->msg_array[monpriv->write_index]->msg, |
| 259 | msg, sizeof(*msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 261 | pr_warning("The read queue for monitor data is full\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | monpriv->msg_array[monpriv->write_index]->msglim_reached = 1; |
| 263 | } |
| 264 | monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM; |
| 265 | atomic_inc(&monpriv->read_ready); |
| 266 | wake_up_interruptible(&mon_read_wait_queue); |
| 267 | } |
| 268 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 269 | static struct iucv_handler monreader_iucv_handler = { |
| 270 | .path_complete = mon_iucv_path_complete, |
| 271 | .path_severed = mon_iucv_path_severed, |
| 272 | .message_pending = mon_iucv_message_pending, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | /****************************************************************************** |
| 276 | * file operations * |
| 277 | *****************************************************************************/ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 278 | static int mon_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | struct mon_private *monpriv; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 281 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | /* |
| 284 | * only one user allowed |
| 285 | */ |
Arnd Bergmann | 6ce46a4 | 2008-05-20 19:16:17 +0200 | [diff] [blame] | 286 | lock_kernel(); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 287 | rc = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | if (test_and_set_bit(MON_IN_USE, &mon_in_use)) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 289 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 291 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | monpriv = mon_alloc_mem(); |
| 293 | if (!monpriv) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 294 | goto out_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | /* |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 297 | * Connect to *MONITOR service |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | */ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 299 | monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL); |
| 300 | if (!monpriv->path) |
| 301 | goto out_priv; |
| 302 | rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler, |
| 303 | MON_SERVICE, NULL, user_data_connect, monpriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | if (rc) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 305 | pr_err("Connecting to the z/VM *MONITOR system service " |
| 306 | "failed with rc=%i\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | rc = -EIO; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 308 | goto out_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | /* |
| 311 | * Wait for connection confirmation |
| 312 | */ |
| 313 | wait_event(mon_conn_wait_queue, |
| 314 | atomic_read(&monpriv->iucv_connected) || |
| 315 | atomic_read(&monpriv->iucv_severed)); |
| 316 | if (atomic_read(&monpriv->iucv_severed)) { |
| 317 | atomic_set(&monpriv->iucv_severed, 0); |
| 318 | atomic_set(&monpriv->iucv_connected, 0); |
| 319 | rc = -EIO; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 320 | goto out_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | filp->private_data = monpriv; |
Heiko Carstens | 9935774 | 2009-07-07 16:37:04 +0200 | [diff] [blame] | 323 | dev_set_drvdata(monreader_device, monpriv); |
Arnd Bergmann | 6ce46a4 | 2008-05-20 19:16:17 +0200 | [diff] [blame] | 324 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return nonseekable_open(inode, filp); |
| 326 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 327 | out_path: |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 328 | iucv_path_free(monpriv->path); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 329 | out_priv: |
| 330 | mon_free_mem(monpriv); |
| 331 | out_use: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | clear_bit(MON_IN_USE, &mon_in_use); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 333 | out: |
Arnd Bergmann | 6ce46a4 | 2008-05-20 19:16:17 +0200 | [diff] [blame] | 334 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return rc; |
| 336 | } |
| 337 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 338 | static int mon_close(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
| 340 | int rc, i; |
| 341 | struct mon_private *monpriv = filp->private_data; |
| 342 | |
| 343 | /* |
| 344 | * Close IUCV connection and unregister |
| 345 | */ |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 346 | if (monpriv->path) { |
| 347 | rc = iucv_path_sever(monpriv->path, user_data_sever); |
| 348 | if (rc) |
| 349 | pr_warning("Disconnecting the z/VM *MONITOR system " |
| 350 | "service failed with rc=%i\n", rc); |
| 351 | iucv_path_free(monpriv->path); |
| 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | atomic_set(&monpriv->iucv_severed, 0); |
| 355 | atomic_set(&monpriv->iucv_connected, 0); |
| 356 | atomic_set(&monpriv->read_ready, 0); |
| 357 | atomic_set(&monpriv->msglim_count, 0); |
| 358 | monpriv->write_index = 0; |
| 359 | monpriv->read_index = 0; |
| 360 | |
| 361 | for (i = 0; i < MON_MSGLIM; i++) |
| 362 | kfree(monpriv->msg_array[i]); |
| 363 | kfree(monpriv); |
| 364 | clear_bit(MON_IN_USE, &mon_in_use); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 368 | static ssize_t mon_read(struct file *filp, char __user *data, |
| 369 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | struct mon_private *monpriv = filp->private_data; |
| 372 | struct mon_msg *monmsg; |
| 373 | int ret; |
| 374 | u32 mce_start; |
| 375 | |
| 376 | monmsg = mon_next_message(monpriv); |
| 377 | if (IS_ERR(monmsg)) |
| 378 | return PTR_ERR(monmsg); |
| 379 | |
| 380 | if (!monmsg) { |
| 381 | if (filp->f_flags & O_NONBLOCK) |
| 382 | return -EAGAIN; |
| 383 | ret = wait_event_interruptible(mon_read_wait_queue, |
| 384 | atomic_read(&monpriv->read_ready) || |
| 385 | atomic_read(&monpriv->iucv_severed)); |
| 386 | if (ret) |
| 387 | return ret; |
| 388 | if (unlikely(atomic_read(&monpriv->iucv_severed))) |
| 389 | return -EIO; |
| 390 | monmsg = monpriv->msg_array[monpriv->read_index]; |
| 391 | } |
| 392 | |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 393 | if (!monmsg->pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (mon_check_mca(monmsg)) |
| 396 | goto reply; |
| 397 | |
| 398 | /* read monitor control element (12 bytes) first */ |
| 399 | mce_start = mon_mca_start(monmsg) + monmsg->mca_offset; |
| 400 | if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) { |
| 401 | count = min(count, (size_t) mce_start + 12 - monmsg->pos); |
| 402 | ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos, |
| 403 | count); |
| 404 | if (ret) |
| 405 | return -EFAULT; |
| 406 | monmsg->pos += count; |
| 407 | if (monmsg->pos == mce_start + 12) |
| 408 | monmsg->pos = mon_rec_start(monmsg); |
| 409 | goto out_copy; |
| 410 | } |
| 411 | |
| 412 | /* read records */ |
| 413 | if (monmsg->pos <= mon_rec_end(monmsg)) { |
| 414 | count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos |
| 415 | + 1); |
| 416 | ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos, |
| 417 | count); |
| 418 | if (ret) |
| 419 | return -EFAULT; |
| 420 | monmsg->pos += count; |
| 421 | if (monmsg->pos > mon_rec_end(monmsg)) |
| 422 | mon_next_mca(monmsg); |
| 423 | goto out_copy; |
| 424 | } |
| 425 | reply: |
| 426 | ret = mon_send_reply(monmsg, monpriv); |
| 427 | return ret; |
| 428 | |
| 429 | out_copy: |
| 430 | *ppos += count; |
| 431 | return count; |
| 432 | } |
| 433 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 434 | static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
| 436 | struct mon_private *monpriv = filp->private_data; |
| 437 | |
| 438 | poll_wait(filp, &mon_read_wait_queue, p); |
| 439 | if (unlikely(atomic_read(&monpriv->iucv_severed))) |
| 440 | return POLLERR; |
| 441 | if (atomic_read(&monpriv->read_ready)) |
| 442 | return POLLIN | POLLRDNORM; |
| 443 | return 0; |
| 444 | } |
| 445 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 446 | static const struct file_operations mon_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | .owner = THIS_MODULE, |
| 448 | .open = &mon_open, |
| 449 | .release = &mon_close, |
| 450 | .read = &mon_read, |
| 451 | .poll = &mon_poll, |
| 452 | }; |
| 453 | |
| 454 | static struct miscdevice mon_dev = { |
| 455 | .name = "monreader", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | .fops = &mon_fops, |
| 457 | .minor = MISC_DYNAMIC_MINOR, |
| 458 | }; |
| 459 | |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 460 | |
| 461 | /****************************************************************************** |
| 462 | * suspend / resume * |
| 463 | *****************************************************************************/ |
| 464 | static int monreader_freeze(struct device *dev) |
| 465 | { |
Heiko Carstens | 9935774 | 2009-07-07 16:37:04 +0200 | [diff] [blame] | 466 | struct mon_private *monpriv = dev_get_drvdata(dev); |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 467 | int rc; |
| 468 | |
| 469 | if (!monpriv) |
| 470 | return 0; |
| 471 | if (monpriv->path) { |
| 472 | rc = iucv_path_sever(monpriv->path, user_data_sever); |
| 473 | if (rc) |
| 474 | pr_warning("Disconnecting the z/VM *MONITOR system " |
| 475 | "service failed with rc=%i\n", rc); |
| 476 | iucv_path_free(monpriv->path); |
| 477 | } |
| 478 | atomic_set(&monpriv->iucv_severed, 0); |
| 479 | atomic_set(&monpriv->iucv_connected, 0); |
| 480 | atomic_set(&monpriv->read_ready, 0); |
| 481 | atomic_set(&monpriv->msglim_count, 0); |
| 482 | monpriv->write_index = 0; |
| 483 | monpriv->read_index = 0; |
| 484 | monpriv->path = NULL; |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int monreader_thaw(struct device *dev) |
| 489 | { |
Martin Schwidefsky | 4f0076f | 2009-06-22 12:08:19 +0200 | [diff] [blame] | 490 | struct mon_private *monpriv = dev_get_drvdata(dev); |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 491 | int rc; |
| 492 | |
| 493 | if (!monpriv) |
| 494 | return 0; |
| 495 | rc = -ENOMEM; |
| 496 | monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL); |
| 497 | if (!monpriv->path) |
| 498 | goto out; |
| 499 | rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler, |
| 500 | MON_SERVICE, NULL, user_data_connect, monpriv); |
| 501 | if (rc) { |
| 502 | pr_err("Connecting to the z/VM *MONITOR system service " |
| 503 | "failed with rc=%i\n", rc); |
| 504 | goto out_path; |
| 505 | } |
| 506 | wait_event(mon_conn_wait_queue, |
| 507 | atomic_read(&monpriv->iucv_connected) || |
| 508 | atomic_read(&monpriv->iucv_severed)); |
| 509 | if (atomic_read(&monpriv->iucv_severed)) |
| 510 | goto out_path; |
| 511 | return 0; |
| 512 | out_path: |
| 513 | rc = -EIO; |
| 514 | iucv_path_free(monpriv->path); |
| 515 | monpriv->path = NULL; |
| 516 | out: |
| 517 | atomic_set(&monpriv->iucv_severed, 1); |
| 518 | return rc; |
| 519 | } |
| 520 | |
| 521 | static int monreader_restore(struct device *dev) |
| 522 | { |
| 523 | int rc; |
| 524 | |
| 525 | segment_unload(mon_dcss_name); |
| 526 | rc = segment_load(mon_dcss_name, SEGMENT_SHARED, |
| 527 | &mon_dcss_start, &mon_dcss_end); |
| 528 | if (rc < 0) { |
| 529 | segment_warning(rc, mon_dcss_name); |
| 530 | panic("fatal monreader resume error: no monitor dcss\n"); |
| 531 | } |
| 532 | return monreader_thaw(dev); |
| 533 | } |
| 534 | |
| 535 | static struct dev_pm_ops monreader_pm_ops = { |
| 536 | .freeze = monreader_freeze, |
| 537 | .thaw = monreader_thaw, |
| 538 | .restore = monreader_restore, |
| 539 | }; |
| 540 | |
| 541 | static struct device_driver monreader_driver = { |
| 542 | .name = "monreader", |
| 543 | .bus = &iucv_bus, |
| 544 | .pm = &monreader_pm_ops, |
| 545 | }; |
| 546 | |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | /****************************************************************************** |
| 549 | * module init/exit * |
| 550 | *****************************************************************************/ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 551 | static int __init mon_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | int rc; |
| 554 | |
| 555 | if (!MACHINE_IS_VM) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 556 | pr_err("The z/VM *MONITOR record device driver cannot be " |
| 557 | "loaded without z/VM\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return -ENODEV; |
| 559 | } |
| 560 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 561 | /* |
| 562 | * Register with IUCV and connect to *MONITOR service |
| 563 | */ |
| 564 | rc = iucv_register(&monreader_iucv_handler, 1); |
| 565 | if (rc) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 566 | pr_err("The z/VM *MONITOR record device driver failed to " |
| 567 | "register with IUCV\n"); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 568 | return rc; |
| 569 | } |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 570 | |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 571 | rc = driver_register(&monreader_driver); |
| 572 | if (rc) |
| 573 | goto out_iucv; |
| 574 | monreader_device = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 575 | if (!monreader_device) |
| 576 | goto out_driver; |
| 577 | dev_set_name(monreader_device, "monreader-dev"); |
| 578 | monreader_device->bus = &iucv_bus; |
| 579 | monreader_device->parent = iucv_root; |
| 580 | monreader_device->driver = &monreader_driver; |
| 581 | monreader_device->release = (void (*)(struct device *))kfree; |
| 582 | rc = device_register(monreader_device); |
| 583 | if (rc) { |
Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 584 | put_device(monreader_device); |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 585 | goto out_driver; |
| 586 | } |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | rc = segment_type(mon_dcss_name); |
| 589 | if (rc < 0) { |
Martin Schwidefsky | ca68305 | 2008-04-17 07:46:31 +0200 | [diff] [blame] | 590 | segment_warning(rc, mon_dcss_name); |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 591 | goto out_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | if (rc != SEG_TYPE_SC) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 594 | pr_err("The specified *MONITOR DCSS %s does not have the " |
| 595 | "required type SC\n", mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 596 | rc = -EINVAL; |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 597 | goto out_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | rc = segment_load(mon_dcss_name, SEGMENT_SHARED, |
| 601 | &mon_dcss_start, &mon_dcss_end); |
| 602 | if (rc < 0) { |
Martin Schwidefsky | ca68305 | 2008-04-17 07:46:31 +0200 | [diff] [blame] | 603 | segment_warning(rc, mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 604 | rc = -EINVAL; |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 605 | goto out_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | dcss_mkname(mon_dcss_name, &user_data_connect[8]); |
| 608 | |
| 609 | rc = misc_register(&mon_dev); |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 610 | if (rc < 0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | return 0; |
| 613 | |
| 614 | out: |
| 615 | segment_unload(mon_dcss_name); |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 616 | out_device: |
| 617 | device_unregister(monreader_device); |
| 618 | out_driver: |
| 619 | driver_unregister(&monreader_driver); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 620 | out_iucv: |
| 621 | iucv_unregister(&monreader_iucv_handler, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return rc; |
| 623 | } |
| 624 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 625 | static void __exit mon_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
| 627 | segment_unload(mon_dcss_name); |
| 628 | WARN_ON(misc_deregister(&mon_dev) != 0); |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 629 | device_unregister(monreader_device); |
| 630 | driver_unregister(&monreader_driver); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 631 | iucv_unregister(&monreader_iucv_handler, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | return; |
| 633 | } |
| 634 | |
| 635 | |
| 636 | module_init(mon_init); |
| 637 | module_exit(mon_exit); |
| 638 | |
| 639 | module_param_string(mondcss, mon_dcss_name, 9, 0444); |
| 640 | MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR " |
| 641 | "service, max. 8 chars. Default is MONDCSS"); |
| 642 | |
| 643 | MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>"); |
| 644 | MODULE_DESCRIPTION("Character device driver for reading z/VM " |
| 645 | "monitor service records."); |
| 646 | MODULE_LICENSE("GPL"); |