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