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