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 | * |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame^] | 6 | * Copyright IBM Corp. 2004, 2008 |
| 7 | * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/moduleparam.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/miscdevice.h> |
| 17 | #include <linux/ctype.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/interrupt.h> |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame^] | 20 | #include <linux/poll.h> |
| 21 | #include <net/iucv/iucv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/uaccess.h> |
| 23 | #include <asm/ebcdic.h> |
| 24 | #include <asm/extmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | //#define MON_DEBUG /* Debug messages on/off */ |
| 27 | |
| 28 | #define MON_NAME "monreader" |
| 29 | |
| 30 | #define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x) |
| 31 | #define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x) |
| 32 | #define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x) |
| 33 | |
| 34 | #ifdef MON_DEBUG |
| 35 | #define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x) |
| 36 | #else |
| 37 | #define P_DEBUG(x...) do {} while (0) |
| 38 | #endif |
| 39 | |
| 40 | #define MON_COLLECT_SAMPLE 0x80 |
| 41 | #define MON_COLLECT_EVENT 0x40 |
| 42 | #define MON_SERVICE "*MONITOR" |
| 43 | #define MON_IN_USE 0x01 |
| 44 | #define MON_MSGLIM 255 |
| 45 | |
| 46 | static char mon_dcss_name[9] = "MONDCSS\0"; |
| 47 | |
| 48 | struct mon_msg { |
| 49 | u32 pos; |
| 50 | u32 mca_offset; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 51 | struct iucv_message msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | char msglim_reached; |
| 53 | char replied_msglim; |
| 54 | }; |
| 55 | |
| 56 | struct mon_private { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 57 | struct iucv_path *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | struct mon_msg *msg_array[MON_MSGLIM]; |
| 59 | unsigned int write_index; |
| 60 | unsigned int read_index; |
| 61 | atomic_t msglim_count; |
| 62 | atomic_t read_ready; |
| 63 | atomic_t iucv_connected; |
| 64 | atomic_t iucv_severed; |
| 65 | }; |
| 66 | |
| 67 | static unsigned long mon_in_use = 0; |
| 68 | |
| 69 | static unsigned long mon_dcss_start; |
| 70 | static unsigned long mon_dcss_end; |
| 71 | |
| 72 | static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue); |
| 73 | static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue); |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static u8 user_data_connect[16] = { |
| 76 | /* Version code, must be 0x01 for shared mode */ |
| 77 | 0x01, |
| 78 | /* what to collect */ |
| 79 | MON_COLLECT_SAMPLE | MON_COLLECT_EVENT, |
| 80 | /* DCSS name in EBCDIC, 8 bytes padded with blanks */ |
| 81 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 82 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 83 | }; |
| 84 | |
| 85 | static u8 user_data_sever[16] = { |
| 86 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 87 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | /****************************************************************************** |
| 92 | * helper functions * |
| 93 | *****************************************************************************/ |
| 94 | /* |
| 95 | * Create the 8 bytes EBCDIC DCSS segment name from |
| 96 | * an ASCII name, incl. padding |
| 97 | */ |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 98 | static void dcss_mkname(char *ascii_name, char *ebcdic_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
| 100 | int i; |
| 101 | |
| 102 | for (i = 0; i < 8; i++) { |
| 103 | if (ascii_name[i] == '\0') |
| 104 | break; |
| 105 | ebcdic_name[i] = toupper(ascii_name[i]); |
| 106 | }; |
| 107 | for (; i < 8; i++) |
| 108 | ebcdic_name[i] = ' '; |
| 109 | ASCEBC(ebcdic_name, 8); |
| 110 | } |
| 111 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 112 | static inline unsigned long mon_mca_start(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 114 | return *(u32 *) &monmsg->msg.rmmsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 117 | static inline unsigned long mon_mca_end(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 119 | return *(u32 *) &monmsg->msg.rmmsg[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 122 | static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index); |
| 125 | } |
| 126 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 127 | static inline u32 mon_mca_size(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1; |
| 130 | } |
| 131 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 132 | static inline u32 mon_rec_start(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4)); |
| 135 | } |
| 136 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 137 | static inline u32 mon_rec_end(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8)); |
| 140 | } |
| 141 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 142 | static int mon_check_mca(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) || |
| 145 | (mon_rec_start(monmsg) < mon_dcss_start) || |
| 146 | (mon_rec_end(monmsg) > mon_dcss_end) || |
| 147 | (mon_mca_type(monmsg, 0) == 0) || |
| 148 | (mon_mca_size(monmsg) % 12 != 0) || |
| 149 | (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) || |
| 150 | (mon_mca_end(monmsg) > mon_dcss_end) || |
| 151 | (mon_mca_start(monmsg) < mon_dcss_start) || |
| 152 | ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 157 | static int mon_send_reply(struct mon_msg *monmsg, |
| 158 | struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | int rc; |
| 161 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 162 | rc = iucv_message_reply(monpriv->path, &monmsg->msg, |
| 163 | IUCV_IPRMDATA, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | atomic_dec(&monpriv->msglim_count); |
| 165 | if (likely(!monmsg->msglim_reached)) { |
| 166 | monmsg->pos = 0; |
| 167 | monmsg->mca_offset = 0; |
| 168 | monpriv->read_index = (monpriv->read_index + 1) % |
| 169 | MON_MSGLIM; |
| 170 | atomic_dec(&monpriv->read_ready); |
| 171 | } else |
| 172 | monmsg->replied_msglim = 1; |
| 173 | if (rc) { |
| 174 | P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc); |
| 175 | return -EIO; |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 180 | static void mon_free_mem(struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 182 | int i; |
| 183 | |
| 184 | for (i = 0; i < MON_MSGLIM; i++) |
| 185 | if (monpriv->msg_array[i]) |
| 186 | kfree(monpriv->msg_array[i]); |
| 187 | kfree(monpriv); |
| 188 | } |
| 189 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 190 | static struct mon_private *mon_alloc_mem(void) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 191 | { |
| 192 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | struct mon_private *monpriv; |
| 194 | |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 195 | monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL); |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame^] | 196 | if (!monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | for (i = 0; i < MON_MSGLIM; i++) { |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 199 | monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | GFP_KERNEL); |
| 201 | if (!monpriv->msg_array[i]) { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 202 | mon_free_mem(monpriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return NULL; |
| 204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | return monpriv; |
| 207 | } |
| 208 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 209 | static inline void mon_next_mca(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12)) |
| 212 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | monmsg->mca_offset += 12; |
| 214 | monmsg->pos = 0; |
| 215 | } |
| 216 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 217 | static struct mon_msg *mon_next_message(struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | struct mon_msg *monmsg; |
| 220 | |
| 221 | if (!atomic_read(&monpriv->read_ready)) |
| 222 | return NULL; |
| 223 | monmsg = monpriv->msg_array[monpriv->read_index]; |
| 224 | if (unlikely(monmsg->replied_msglim)) { |
| 225 | monmsg->replied_msglim = 0; |
| 226 | monmsg->msglim_reached = 0; |
| 227 | monmsg->pos = 0; |
| 228 | monmsg->mca_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | monpriv->read_index = (monpriv->read_index + 1) % |
| 230 | MON_MSGLIM; |
| 231 | atomic_dec(&monpriv->read_ready); |
| 232 | return ERR_PTR(-EOVERFLOW); |
| 233 | } |
| 234 | return monmsg; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /****************************************************************************** |
| 239 | * IUCV handler * |
| 240 | *****************************************************************************/ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 241 | 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] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | atomic_set(&monpriv->iucv_connected, 1); |
| 246 | wake_up(&mon_conn_wait_queue); |
| 247 | } |
| 248 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 249 | 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] | 250 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 251 | struct mon_private *monpriv = path->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 253 | P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]); |
| 254 | iucv_path_sever(path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | atomic_set(&monpriv->iucv_severed, 1); |
| 256 | wake_up(&mon_conn_wait_queue); |
| 257 | wake_up_interruptible(&mon_read_wait_queue); |
| 258 | } |
| 259 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 260 | static void mon_iucv_message_pending(struct iucv_path *path, |
| 261 | struct iucv_message *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 263 | struct mon_private *monpriv = path->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 265 | memcpy(&monpriv->msg_array[monpriv->write_index]->msg, |
| 266 | msg, sizeof(*msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) { |
| 268 | P_WARNING("IUCV message pending, message limit (%i) reached\n", |
| 269 | MON_MSGLIM); |
| 270 | monpriv->msg_array[monpriv->write_index]->msglim_reached = 1; |
| 271 | } |
| 272 | monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM; |
| 273 | atomic_inc(&monpriv->read_ready); |
| 274 | wake_up_interruptible(&mon_read_wait_queue); |
| 275 | } |
| 276 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 277 | static struct iucv_handler monreader_iucv_handler = { |
| 278 | .path_complete = mon_iucv_path_complete, |
| 279 | .path_severed = mon_iucv_path_severed, |
| 280 | .message_pending = mon_iucv_message_pending, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | /****************************************************************************** |
| 284 | * file operations * |
| 285 | *****************************************************************************/ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 286 | static int mon_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | struct mon_private *monpriv; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 289 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * only one user allowed |
| 293 | */ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 294 | rc = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (test_and_set_bit(MON_IN_USE, &mon_in_use)) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 296 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 298 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | monpriv = mon_alloc_mem(); |
| 300 | if (!monpriv) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 301 | goto out_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | /* |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 304 | * Connect to *MONITOR service |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | */ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 306 | monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL); |
| 307 | if (!monpriv->path) |
| 308 | goto out_priv; |
| 309 | rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler, |
| 310 | MON_SERVICE, NULL, user_data_connect, monpriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (rc) { |
| 312 | P_ERROR("iucv connection to *MONITOR failed with " |
| 313 | "IPUSER SEVER code = %i\n", rc); |
| 314 | rc = -EIO; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 315 | goto out_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
| 317 | /* |
| 318 | * Wait for connection confirmation |
| 319 | */ |
| 320 | wait_event(mon_conn_wait_queue, |
| 321 | atomic_read(&monpriv->iucv_connected) || |
| 322 | atomic_read(&monpriv->iucv_severed)); |
| 323 | if (atomic_read(&monpriv->iucv_severed)) { |
| 324 | atomic_set(&monpriv->iucv_severed, 0); |
| 325 | atomic_set(&monpriv->iucv_connected, 0); |
| 326 | rc = -EIO; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 327 | goto out_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | filp->private_data = monpriv; |
| 330 | return nonseekable_open(inode, filp); |
| 331 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 332 | out_path: |
| 333 | kfree(monpriv->path); |
| 334 | out_priv: |
| 335 | mon_free_mem(monpriv); |
| 336 | out_use: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | clear_bit(MON_IN_USE, &mon_in_use); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 338 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return rc; |
| 340 | } |
| 341 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 342 | static int mon_close(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | int rc, i; |
| 345 | struct mon_private *monpriv = filp->private_data; |
| 346 | |
| 347 | /* |
| 348 | * Close IUCV connection and unregister |
| 349 | */ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 350 | rc = iucv_path_sever(monpriv->path, user_data_sever); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (rc) |
| 352 | P_ERROR("close, iucv_sever failed with rc = %i\n", rc); |
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 | |
| 460 | /****************************************************************************** |
| 461 | * module init/exit * |
| 462 | *****************************************************************************/ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 463 | static int __init mon_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
| 465 | int rc; |
| 466 | |
| 467 | if (!MACHINE_IS_VM) { |
| 468 | P_ERROR("not running under z/VM, driver not loaded\n"); |
| 469 | return -ENODEV; |
| 470 | } |
| 471 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 472 | /* |
| 473 | * Register with IUCV and connect to *MONITOR service |
| 474 | */ |
| 475 | rc = iucv_register(&monreader_iucv_handler, 1); |
| 476 | if (rc) { |
| 477 | P_ERROR("failed to register with iucv driver\n"); |
| 478 | return rc; |
| 479 | } |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | rc = segment_type(mon_dcss_name); |
| 482 | if (rc < 0) { |
Martin Schwidefsky | ca68305 | 2008-04-17 07:46:31 +0200 | [diff] [blame] | 483 | segment_warning(rc, mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 484 | goto out_iucv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | if (rc != SEG_TYPE_SC) { |
| 487 | P_ERROR("segment %s has unsupported type, should be SC\n", |
| 488 | mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 489 | rc = -EINVAL; |
| 490 | goto out_iucv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | rc = segment_load(mon_dcss_name, SEGMENT_SHARED, |
| 494 | &mon_dcss_start, &mon_dcss_end); |
| 495 | if (rc < 0) { |
Martin Schwidefsky | ca68305 | 2008-04-17 07:46:31 +0200 | [diff] [blame] | 496 | segment_warning(rc, mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 497 | rc = -EINVAL; |
| 498 | goto out_iucv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
| 500 | dcss_mkname(mon_dcss_name, &user_data_connect[8]); |
| 501 | |
| 502 | rc = misc_register(&mon_dev); |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame^] | 503 | if (rc < 0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | |
| 507 | out: |
| 508 | segment_unload(mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 509 | out_iucv: |
| 510 | iucv_unregister(&monreader_iucv_handler, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return rc; |
| 512 | } |
| 513 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 514 | static void __exit mon_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
| 516 | segment_unload(mon_dcss_name); |
| 517 | WARN_ON(misc_deregister(&mon_dev) != 0); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 518 | iucv_unregister(&monreader_iucv_handler, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | module_init(mon_init); |
| 524 | module_exit(mon_exit); |
| 525 | |
| 526 | module_param_string(mondcss, mon_dcss_name, 9, 0444); |
| 527 | MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR " |
| 528 | "service, max. 8 chars. Default is MONDCSS"); |
| 529 | |
| 530 | MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>"); |
| 531 | MODULE_DESCRIPTION("Character device driver for reading z/VM " |
| 532 | "monitor service records."); |
| 533 | MODULE_LICENSE("GPL"); |