Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Heiko Carstens | e018ba1 | 2006-02-01 03:06:31 -0800 | [diff] [blame] | 2 | * linux/drivers/s390/cio/cmf.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Linux on zSeries Channel Measurement Facility support |
| 5 | * |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 6 | * Copyright 2000,2006 IBM Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 8 | * Authors: Arnd Bergmann <arndb@de.ibm.com> |
| 9 | * Cornelia Huck <cornelia.huck@de.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * original idea from Natarajan Krishnaswami <nkrishna@us.ibm.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2, or (at your option) |
| 16 | * any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/bootmem.h> |
| 29 | #include <linux/device.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/list.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/moduleparam.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 34 | #include <linux/slab.h> |
| 35 | #include <linux/timex.h> /* get_clock() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <asm/ccwdev.h> |
| 38 | #include <asm/cio.h> |
| 39 | #include <asm/cmb.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 40 | #include <asm/div64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #include "cio.h" |
| 43 | #include "css.h" |
| 44 | #include "device.h" |
| 45 | #include "ioasm.h" |
| 46 | #include "chsc.h" |
| 47 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 48 | /* |
| 49 | * parameter to enable cmf during boot, possible uses are: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be |
| 51 | * used on any subchannel |
| 52 | * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure |
| 53 | * <num> subchannel, where <num> is an integer |
| 54 | * between 1 and 65535, default is 1024 |
| 55 | */ |
| 56 | #define ARGSTRING "s390cmf" |
| 57 | |
| 58 | /* indices for READCMB */ |
| 59 | enum cmb_index { |
| 60 | /* basic and exended format: */ |
| 61 | cmb_ssch_rsch_count, |
| 62 | cmb_sample_count, |
| 63 | cmb_device_connect_time, |
| 64 | cmb_function_pending_time, |
| 65 | cmb_device_disconnect_time, |
| 66 | cmb_control_unit_queuing_time, |
| 67 | cmb_device_active_only_time, |
| 68 | /* extended format only: */ |
| 69 | cmb_device_busy_time, |
| 70 | cmb_initial_command_response_time, |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * enum cmb_format - types of supported measurement block formats |
| 75 | * |
| 76 | * @CMF_BASIC: traditional channel measurement blocks supported |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 77 | * by all machines that we run on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * @CMF_EXTENDED: improved format that was introduced with the z990 |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 79 | * machine |
| 80 | * @CMF_AUTODETECT: default: use extended format when running on a machine |
| 81 | * supporting extended format, otherwise fall back to |
| 82 | * basic format |
| 83 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | enum cmb_format { |
| 85 | CMF_BASIC, |
| 86 | CMF_EXTENDED, |
| 87 | CMF_AUTODETECT = -1, |
| 88 | }; |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 89 | |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 90 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * format - actual format for all measurement blocks |
| 92 | * |
| 93 | * The format module parameter can be set to a value of 0 (zero) |
| 94 | * or 1, indicating basic or extended format as described for |
| 95 | * enum cmb_format. |
| 96 | */ |
| 97 | static int format = CMF_AUTODETECT; |
| 98 | module_param(format, bool, 0444); |
| 99 | |
| 100 | /** |
| 101 | * struct cmb_operations - functions to use depending on cmb_format |
| 102 | * |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 103 | * Most of these functions operate on a struct ccw_device. There is only |
| 104 | * one instance of struct cmb_operations because the format of the measurement |
| 105 | * data is guaranteed to be the same for every ccw_device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * |
| 107 | * @alloc: allocate memory for a channel measurement block, |
| 108 | * either with the help of a special pool or with kmalloc |
| 109 | * @free: free memory allocated with @alloc |
| 110 | * @set: enable or disable measurement |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 111 | * @read: read a measurement entry at an index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | * @readall: read a measurement block in a common format |
| 113 | * @reset: clear the data in the associated measurement block and |
| 114 | * reset its time stamp |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 115 | * @align: align an allocated block so that the hardware can use it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | */ |
| 117 | struct cmb_operations { |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 118 | int (*alloc) (struct ccw_device *); |
| 119 | void (*free) (struct ccw_device *); |
| 120 | int (*set) (struct ccw_device *, u32); |
| 121 | u64 (*read) (struct ccw_device *, int); |
| 122 | int (*readall)(struct ccw_device *, struct cmbdata *); |
| 123 | void (*reset) (struct ccw_device *); |
| 124 | void *(*align) (void *); |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 125 | /* private: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | struct attribute_group *attr_group; |
| 127 | }; |
| 128 | static struct cmb_operations *cmbops; |
| 129 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 130 | struct cmb_data { |
| 131 | void *hw_block; /* Pointer to block updated by hardware */ |
| 132 | void *last_block; /* Last changed block copied from hardware block */ |
| 133 | int size; /* Size of hw_block and last_block */ |
| 134 | unsigned long long last_update; /* when last_block was updated */ |
| 135 | }; |
| 136 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 137 | /* |
| 138 | * Our user interface is designed in terms of nanoseconds, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | * while the hardware measures total times in its own |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 140 | * unit. |
| 141 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | static inline u64 time_to_nsec(u32 value) |
| 143 | { |
| 144 | return ((u64)value) * 128000ull; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Users are usually interested in average times, |
| 149 | * not accumulated time. |
| 150 | * This also helps us with atomicity problems |
| 151 | * when reading sinlge values. |
| 152 | */ |
| 153 | static inline u64 time_to_avg_nsec(u32 value, u32 count) |
| 154 | { |
| 155 | u64 ret; |
| 156 | |
| 157 | /* no samples yet, avoid division by 0 */ |
| 158 | if (count == 0) |
| 159 | return 0; |
| 160 | |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 161 | /* value comes in units of 128 µsec */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | ret = time_to_nsec(value); |
| 163 | do_div(ret, count); |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 168 | /* |
| 169 | * Activate or deactivate the channel monitor. When area is NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * the monitor is deactivated. The channel monitor needs to |
| 171 | * be active in order to measure subchannels, which also need |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 172 | * to be enabled. |
| 173 | */ |
| 174 | static inline void cmf_activate(void *area, unsigned int onoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | register void * __gpr2 asm("2"); |
| 177 | register long __gpr1 asm("1"); |
| 178 | |
| 179 | __gpr2 = area; |
| 180 | __gpr1 = onoff ? 2 : 0; |
| 181 | /* activate channel measurement */ |
| 182 | asm("schm" : : "d" (__gpr2), "d" (__gpr1) ); |
| 183 | } |
| 184 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 185 | static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc, |
| 186 | unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
| 188 | int ret; |
| 189 | int retry; |
| 190 | struct subchannel *sch; |
| 191 | struct schib *schib; |
| 192 | |
| 193 | sch = to_subchannel(cdev->dev.parent); |
| 194 | schib = &sch->schib; |
| 195 | /* msch can silently fail, so do it again if necessary */ |
| 196 | for (retry = 0; retry < 3; retry++) { |
| 197 | /* prepare schib */ |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 198 | stsch(sch->schid, schib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | schib->pmcw.mme = mme; |
| 200 | schib->pmcw.mbfc = mbfc; |
| 201 | /* address can be either a block address or a block index */ |
| 202 | if (mbfc) |
| 203 | schib->mba = address; |
| 204 | else |
| 205 | schib->pmcw.mbi = address; |
| 206 | |
| 207 | /* try to submit it */ |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 208 | switch(ret = msch_err(sch->schid, schib)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | case 0: |
| 210 | break; |
| 211 | case 1: |
| 212 | case 2: /* in I/O or status pending */ |
| 213 | ret = -EBUSY; |
| 214 | break; |
| 215 | case 3: /* subchannel is no longer valid */ |
| 216 | ret = -ENODEV; |
| 217 | break; |
| 218 | default: /* msch caught an exception */ |
| 219 | ret = -EINVAL; |
| 220 | break; |
| 221 | } |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 222 | stsch(sch->schid, schib); /* restore the schib */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | if (ret) |
| 225 | break; |
| 226 | |
| 227 | /* check if it worked */ |
| 228 | if (schib->pmcw.mme == mme && |
| 229 | schib->pmcw.mbfc == mbfc && |
| 230 | (mbfc ? (schib->mba == address) |
| 231 | : (schib->pmcw.mbi == address))) |
| 232 | return 0; |
| 233 | |
| 234 | ret = -EINVAL; |
| 235 | } |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | struct set_schib_struct { |
| 241 | u32 mme; |
| 242 | int mbfc; |
| 243 | unsigned long address; |
| 244 | wait_queue_head_t wait; |
| 245 | int ret; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 246 | struct kref kref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 249 | static void cmf_set_schib_release(struct kref *kref) |
| 250 | { |
| 251 | struct set_schib_struct *set_data; |
| 252 | |
| 253 | set_data = container_of(kref, struct set_schib_struct, kref); |
| 254 | kfree(set_data); |
| 255 | } |
| 256 | |
| 257 | #define CMF_PENDING 1 |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | static int set_schib_wait(struct ccw_device *cdev, u32 mme, |
| 260 | int mbfc, unsigned long address) |
| 261 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 262 | struct set_schib_struct *set_data; |
| 263 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | spin_lock_irq(cdev->ccwlock); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 266 | if (!cdev->private->cmb) { |
| 267 | ret = -ENODEV; |
| 268 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 270 | set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC); |
| 271 | if (!set_data) { |
| 272 | ret = -ENOMEM; |
| 273 | goto out; |
| 274 | } |
| 275 | init_waitqueue_head(&set_data->wait); |
| 276 | kref_init(&set_data->kref); |
| 277 | set_data->mme = mme; |
| 278 | set_data->mbfc = mbfc; |
| 279 | set_data->address = address; |
| 280 | |
| 281 | ret = set_schib(cdev, mme, mbfc, address); |
| 282 | if (ret != -EBUSY) |
| 283 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | if (cdev->private->state != DEV_STATE_ONLINE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* if the device is not online, don't even try again */ |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 287 | ret = -EBUSY; |
| 288 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | cdev->private->state = DEV_STATE_CMFCHANGE; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 292 | set_data->ret = CMF_PENDING; |
| 293 | cdev->private->cmb_wait = set_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | spin_unlock_irq(cdev->ccwlock); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 296 | if (wait_event_interruptible(set_data->wait, |
| 297 | set_data->ret != CMF_PENDING)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | spin_lock_irq(cdev->ccwlock); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 299 | if (set_data->ret == CMF_PENDING) { |
| 300 | set_data->ret = -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (cdev->private->state == DEV_STATE_CMFCHANGE) |
| 302 | cdev->private->state = DEV_STATE_ONLINE; |
| 303 | } |
| 304 | spin_unlock_irq(cdev->ccwlock); |
| 305 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 306 | spin_lock_irq(cdev->ccwlock); |
| 307 | cdev->private->cmb_wait = NULL; |
| 308 | ret = set_data->ret; |
| 309 | out_put: |
| 310 | kref_put(&set_data->kref, cmf_set_schib_release); |
| 311 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | spin_unlock_irq(cdev->ccwlock); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 313 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void retry_set_schib(struct ccw_device *cdev) |
| 317 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 318 | struct set_schib_struct *set_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 320 | set_data = cdev->private->cmb_wait; |
| 321 | if (!set_data) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | WARN_ON(1); |
| 323 | return; |
| 324 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 325 | kref_get(&set_data->kref); |
| 326 | set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc, |
| 327 | set_data->address); |
| 328 | wake_up(&set_data->wait); |
| 329 | kref_put(&set_data->kref, cmf_set_schib_release); |
| 330 | } |
| 331 | |
| 332 | static int cmf_copy_block(struct ccw_device *cdev) |
| 333 | { |
| 334 | struct subchannel *sch; |
| 335 | void *reference_buf; |
| 336 | void *hw_block; |
| 337 | struct cmb_data *cmb_data; |
| 338 | |
| 339 | sch = to_subchannel(cdev->dev.parent); |
| 340 | |
| 341 | if (stsch(sch->schid, &sch->schib)) |
| 342 | return -ENODEV; |
| 343 | |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame^] | 344 | if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 345 | /* Don't copy if a start function is in progress. */ |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame^] | 346 | if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) && |
| 347 | (scsw_actl(&sch->schib.scsw) & |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 348 | (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) && |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame^] | 349 | (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS))) |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 350 | return -EBUSY; |
| 351 | } |
| 352 | cmb_data = cdev->private->cmb; |
| 353 | hw_block = cmbops->align(cmb_data->hw_block); |
| 354 | if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size)) |
| 355 | /* No need to copy. */ |
| 356 | return 0; |
| 357 | reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC); |
| 358 | if (!reference_buf) |
| 359 | return -ENOMEM; |
| 360 | /* Ensure consistency of block copied from hardware. */ |
| 361 | do { |
| 362 | memcpy(cmb_data->last_block, hw_block, cmb_data->size); |
| 363 | memcpy(reference_buf, hw_block, cmb_data->size); |
| 364 | } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size)); |
| 365 | cmb_data->last_update = get_clock(); |
| 366 | kfree(reference_buf); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | struct copy_block_struct { |
| 371 | wait_queue_head_t wait; |
| 372 | int ret; |
| 373 | struct kref kref; |
| 374 | }; |
| 375 | |
| 376 | static void cmf_copy_block_release(struct kref *kref) |
| 377 | { |
| 378 | struct copy_block_struct *copy_block; |
| 379 | |
| 380 | copy_block = container_of(kref, struct copy_block_struct, kref); |
| 381 | kfree(copy_block); |
| 382 | } |
| 383 | |
| 384 | static int cmf_cmb_copy_wait(struct ccw_device *cdev) |
| 385 | { |
| 386 | struct copy_block_struct *copy_block; |
| 387 | int ret; |
| 388 | unsigned long flags; |
| 389 | |
| 390 | spin_lock_irqsave(cdev->ccwlock, flags); |
| 391 | if (!cdev->private->cmb) { |
| 392 | ret = -ENODEV; |
| 393 | goto out; |
| 394 | } |
| 395 | copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC); |
| 396 | if (!copy_block) { |
| 397 | ret = -ENOMEM; |
| 398 | goto out; |
| 399 | } |
| 400 | init_waitqueue_head(©_block->wait); |
| 401 | kref_init(©_block->kref); |
| 402 | |
| 403 | ret = cmf_copy_block(cdev); |
| 404 | if (ret != -EBUSY) |
| 405 | goto out_put; |
| 406 | |
| 407 | if (cdev->private->state != DEV_STATE_ONLINE) { |
| 408 | ret = -EBUSY; |
| 409 | goto out_put; |
| 410 | } |
| 411 | |
| 412 | cdev->private->state = DEV_STATE_CMFUPDATE; |
| 413 | copy_block->ret = CMF_PENDING; |
| 414 | cdev->private->cmb_wait = copy_block; |
| 415 | |
| 416 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 417 | if (wait_event_interruptible(copy_block->wait, |
| 418 | copy_block->ret != CMF_PENDING)) { |
| 419 | spin_lock_irqsave(cdev->ccwlock, flags); |
| 420 | if (copy_block->ret == CMF_PENDING) { |
| 421 | copy_block->ret = -ERESTARTSYS; |
| 422 | if (cdev->private->state == DEV_STATE_CMFUPDATE) |
| 423 | cdev->private->state = DEV_STATE_ONLINE; |
| 424 | } |
| 425 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 426 | } |
| 427 | spin_lock_irqsave(cdev->ccwlock, flags); |
| 428 | cdev->private->cmb_wait = NULL; |
| 429 | ret = copy_block->ret; |
| 430 | out_put: |
| 431 | kref_put(©_block->kref, cmf_copy_block_release); |
| 432 | out: |
| 433 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | void cmf_retry_copy_block(struct ccw_device *cdev) |
| 438 | { |
| 439 | struct copy_block_struct *copy_block; |
| 440 | |
| 441 | copy_block = cdev->private->cmb_wait; |
| 442 | if (!copy_block) { |
| 443 | WARN_ON(1); |
| 444 | return; |
| 445 | } |
| 446 | kref_get(©_block->kref); |
| 447 | copy_block->ret = cmf_copy_block(cdev); |
| 448 | wake_up(©_block->wait); |
| 449 | kref_put(©_block->kref, cmf_copy_block_release); |
| 450 | } |
| 451 | |
| 452 | static void cmf_generic_reset(struct ccw_device *cdev) |
| 453 | { |
| 454 | struct cmb_data *cmb_data; |
| 455 | |
| 456 | spin_lock_irq(cdev->ccwlock); |
| 457 | cmb_data = cdev->private->cmb; |
| 458 | if (cmb_data) { |
| 459 | memset(cmb_data->last_block, 0, cmb_data->size); |
| 460 | /* |
| 461 | * Need to reset hw block as well to make the hardware start |
| 462 | * from 0 again. |
| 463 | */ |
| 464 | memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size); |
| 465 | cmb_data->last_update = 0; |
| 466 | } |
| 467 | cdev->private->cmb_start_time = get_clock(); |
| 468 | spin_unlock_irq(cdev->ccwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
| 472 | * struct cmb_area - container for global cmb data |
| 473 | * |
| 474 | * @mem: pointer to CMBs (only in basic measurement mode) |
| 475 | * @list: contains a linked list of all subchannels |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 476 | * @num_channels: number of channels to be measured |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | * @lock: protect concurrent access to @mem and @list |
| 478 | */ |
| 479 | struct cmb_area { |
| 480 | struct cmb *mem; |
| 481 | struct list_head list; |
| 482 | int num_channels; |
| 483 | spinlock_t lock; |
| 484 | }; |
| 485 | |
| 486 | static struct cmb_area cmb_area = { |
Milind Arun Choudhary | cb629a0 | 2007-04-27 16:02:01 +0200 | [diff] [blame] | 487 | .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | .list = LIST_HEAD_INIT(cmb_area.list), |
| 489 | .num_channels = 1024, |
| 490 | }; |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* ****** old style CMB handling ********/ |
| 493 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 494 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | * Basic channel measurement blocks are allocated in one contiguous |
| 496 | * block of memory, which can not be moved as long as any channel |
| 497 | * is active. Therefore, a maximum number of subchannels needs to |
| 498 | * be defined somewhere. This is a module parameter, defaulting to |
| 499 | * a resonable value of 1024, or 32 kb of memory. |
| 500 | * Current kernels don't allow kmalloc with more than 128kb, so the |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 501 | * maximum is 4096. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | */ |
| 503 | |
| 504 | module_param_named(maxchannels, cmb_area.num_channels, uint, 0444); |
| 505 | |
| 506 | /** |
| 507 | * struct cmb - basic channel measurement block |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 508 | * @ssch_rsch_count: number of ssch and rsch |
| 509 | * @sample_count: number of samples |
| 510 | * @device_connect_time: time of device connect |
| 511 | * @function_pending_time: time of function pending |
| 512 | * @device_disconnect_time: time of device disconnect |
| 513 | * @control_unit_queuing_time: time of control unit queuing |
| 514 | * @device_active_only_time: time of device active only |
| 515 | * @reserved: unused in basic measurement mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | * |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 517 | * The measurement block as used by the hardware. The fields are described |
| 518 | * further in z/Architecture Principles of Operation, chapter 17. |
| 519 | * |
| 520 | * The cmb area made up from these blocks must be a contiguous array and may |
| 521 | * not be reallocated or freed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | * Only one cmb area can be present in the system. |
| 523 | */ |
| 524 | struct cmb { |
| 525 | u16 ssch_rsch_count; |
| 526 | u16 sample_count; |
| 527 | u32 device_connect_time; |
| 528 | u32 function_pending_time; |
| 529 | u32 device_disconnect_time; |
| 530 | u32 control_unit_queuing_time; |
| 531 | u32 device_active_only_time; |
| 532 | u32 reserved[2]; |
| 533 | }; |
| 534 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 535 | /* |
| 536 | * Insert a single device into the cmb_area list. |
| 537 | * Called with cmb_area.lock held from alloc_cmb. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 539 | static int alloc_cmb_single(struct ccw_device *cdev, |
| 540 | struct cmb_data *cmb_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
| 542 | struct cmb *cmb; |
| 543 | struct ccw_device_private *node; |
| 544 | int ret; |
| 545 | |
| 546 | spin_lock_irq(cdev->ccwlock); |
| 547 | if (!list_empty(&cdev->private->cmb_list)) { |
| 548 | ret = -EBUSY; |
| 549 | goto out; |
| 550 | } |
| 551 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 552 | /* |
| 553 | * Find first unused cmb in cmb_area.mem. |
| 554 | * This is a little tricky: cmb_area.list |
| 555 | * remains sorted by ->cmb->hw_data pointers. |
| 556 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | cmb = cmb_area.mem; |
| 558 | list_for_each_entry(node, &cmb_area.list, cmb_list) { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 559 | struct cmb_data *data; |
| 560 | data = node->cmb; |
| 561 | if ((struct cmb*)data->hw_block > cmb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | break; |
| 563 | cmb++; |
| 564 | } |
| 565 | if (cmb - cmb_area.mem >= cmb_area.num_channels) { |
| 566 | ret = -ENOMEM; |
| 567 | goto out; |
| 568 | } |
| 569 | |
| 570 | /* insert new cmb */ |
| 571 | list_add_tail(&cdev->private->cmb_list, &node->cmb_list); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 572 | cmb_data->hw_block = cmb; |
| 573 | cdev->private->cmb = cmb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | ret = 0; |
| 575 | out: |
| 576 | spin_unlock_irq(cdev->ccwlock); |
| 577 | return ret; |
| 578 | } |
| 579 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 580 | static int alloc_cmb(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
| 582 | int ret; |
| 583 | struct cmb *mem; |
| 584 | ssize_t size; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 585 | struct cmb_data *cmb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 587 | /* Allocate private cmb_data. */ |
| 588 | cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL); |
| 589 | if (!cmb_data) |
| 590 | return -ENOMEM; |
| 591 | |
| 592 | cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL); |
| 593 | if (!cmb_data->last_block) { |
| 594 | kfree(cmb_data); |
| 595 | return -ENOMEM; |
| 596 | } |
| 597 | cmb_data->size = sizeof(struct cmb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | spin_lock(&cmb_area.lock); |
| 599 | |
| 600 | if (!cmb_area.mem) { |
| 601 | /* there is no user yet, so we need a new area */ |
| 602 | size = sizeof(struct cmb) * cmb_area.num_channels; |
| 603 | WARN_ON(!list_empty(&cmb_area.list)); |
| 604 | |
| 605 | spin_unlock(&cmb_area.lock); |
| 606 | mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA, |
| 607 | get_order(size)); |
| 608 | spin_lock(&cmb_area.lock); |
| 609 | |
| 610 | if (cmb_area.mem) { |
| 611 | /* ok, another thread was faster */ |
| 612 | free_pages((unsigned long)mem, get_order(size)); |
| 613 | } else if (!mem) { |
| 614 | /* no luck */ |
Cornelia Huck | 23eb68c | 2007-08-22 13:51:37 +0200 | [diff] [blame] | 615 | printk(KERN_WARNING "cio: failed to allocate area " |
| 616 | "for measuring %d subchannels\n", |
| 617 | cmb_area.num_channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | ret = -ENOMEM; |
| 619 | goto out; |
| 620 | } else { |
| 621 | /* everything ok */ |
| 622 | memset(mem, 0, size); |
| 623 | cmb_area.mem = mem; |
| 624 | cmf_activate(cmb_area.mem, 1); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* do the actual allocation */ |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 629 | ret = alloc_cmb_single(cdev, cmb_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | out: |
| 631 | spin_unlock(&cmb_area.lock); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 632 | if (ret) { |
| 633 | kfree(cmb_data->last_block); |
| 634 | kfree(cmb_data); |
| 635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return ret; |
| 637 | } |
| 638 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 639 | static void free_cmb(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | { |
| 641 | struct ccw_device_private *priv; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 642 | struct cmb_data *cmb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | spin_lock(&cmb_area.lock); |
| 645 | spin_lock_irq(cdev->ccwlock); |
| 646 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 647 | priv = cdev->private; |
| 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | if (list_empty(&priv->cmb_list)) { |
| 650 | /* already freed */ |
| 651 | goto out; |
| 652 | } |
| 653 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 654 | cmb_data = priv->cmb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | priv->cmb = NULL; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 656 | if (cmb_data) |
| 657 | kfree(cmb_data->last_block); |
| 658 | kfree(cmb_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | list_del_init(&priv->cmb_list); |
| 660 | |
| 661 | if (list_empty(&cmb_area.list)) { |
| 662 | ssize_t size; |
| 663 | size = sizeof(struct cmb) * cmb_area.num_channels; |
| 664 | cmf_activate(NULL, 0); |
| 665 | free_pages((unsigned long)cmb_area.mem, get_order(size)); |
| 666 | cmb_area.mem = NULL; |
| 667 | } |
| 668 | out: |
| 669 | spin_unlock_irq(cdev->ccwlock); |
| 670 | spin_unlock(&cmb_area.lock); |
| 671 | } |
| 672 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 673 | static int set_cmb(struct ccw_device *cdev, u32 mme) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { |
| 675 | u16 offset; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 676 | struct cmb_data *cmb_data; |
| 677 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 679 | spin_lock_irqsave(cdev->ccwlock, flags); |
| 680 | if (!cdev->private->cmb) { |
| 681 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | return -EINVAL; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 683 | } |
| 684 | cmb_data = cdev->private->cmb; |
| 685 | offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0; |
| 686 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | return set_schib_wait(cdev, mme, 0, offset); |
| 689 | } |
| 690 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 691 | static u64 read_cmb(struct ccw_device *cdev, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 693 | struct cmb *cmb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | u32 val; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 695 | int ret; |
| 696 | unsigned long flags; |
| 697 | |
| 698 | ret = cmf_cmb_copy_wait(cdev); |
| 699 | if (ret < 0) |
| 700 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | spin_lock_irqsave(cdev->ccwlock, flags); |
| 703 | if (!cdev->private->cmb) { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 704 | ret = 0; |
| 705 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 707 | cmb = ((struct cmb_data *)cdev->private->cmb)->last_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
| 709 | switch (index) { |
| 710 | case cmb_ssch_rsch_count: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 711 | ret = cmb->ssch_rsch_count; |
| 712 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | case cmb_sample_count: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 714 | ret = cmb->sample_count; |
| 715 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | case cmb_device_connect_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 717 | val = cmb->device_connect_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | break; |
| 719 | case cmb_function_pending_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 720 | val = cmb->function_pending_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | break; |
| 722 | case cmb_device_disconnect_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 723 | val = cmb->device_disconnect_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | break; |
| 725 | case cmb_control_unit_queuing_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 726 | val = cmb->control_unit_queuing_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | break; |
| 728 | case cmb_device_active_only_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 729 | val = cmb->device_active_only_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | break; |
| 731 | default: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 732 | ret = 0; |
| 733 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 735 | ret = time_to_avg_nsec(val, cmb->sample_count); |
| 736 | out: |
| 737 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 738 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 741 | static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 743 | struct cmb *cmb; |
| 744 | struct cmb_data *cmb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | u64 time; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 746 | unsigned long flags; |
| 747 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 749 | ret = cmf_cmb_copy_wait(cdev); |
| 750 | if (ret < 0) |
| 751 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | spin_lock_irqsave(cdev->ccwlock, flags); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 753 | cmb_data = cdev->private->cmb; |
| 754 | if (!cmb_data) { |
| 755 | ret = -ENODEV; |
| 756 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 758 | if (cmb_data->last_update == 0) { |
| 759 | ret = -EAGAIN; |
| 760 | goto out; |
| 761 | } |
| 762 | cmb = cmb_data->last_block; |
| 763 | time = cmb_data->last_update - cdev->private->cmb_start_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | memset(data, 0, sizeof(struct cmbdata)); |
| 766 | |
| 767 | /* we only know values before device_busy_time */ |
| 768 | data->size = offsetof(struct cmbdata, device_busy_time); |
| 769 | |
| 770 | /* convert to nanoseconds */ |
| 771 | data->elapsed_time = (time * 1000) >> 12; |
| 772 | |
| 773 | /* copy data to new structure */ |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 774 | data->ssch_rsch_count = cmb->ssch_rsch_count; |
| 775 | data->sample_count = cmb->sample_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | /* time fields are converted to nanoseconds while copying */ |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 778 | data->device_connect_time = time_to_nsec(cmb->device_connect_time); |
| 779 | data->function_pending_time = time_to_nsec(cmb->function_pending_time); |
| 780 | data->device_disconnect_time = |
| 781 | time_to_nsec(cmb->device_disconnect_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | data->control_unit_queuing_time |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 783 | = time_to_nsec(cmb->control_unit_queuing_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | data->device_active_only_time |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 785 | = time_to_nsec(cmb->device_active_only_time); |
| 786 | ret = 0; |
| 787 | out: |
| 788 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 789 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 792 | static void reset_cmb(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 794 | cmf_generic_reset(cdev); |
| 795 | } |
| 796 | |
| 797 | static void * align_cmb(void *area) |
| 798 | { |
| 799 | return area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static struct attribute_group cmf_attr_group; |
| 803 | |
| 804 | static struct cmb_operations cmbops_basic = { |
| 805 | .alloc = alloc_cmb, |
| 806 | .free = free_cmb, |
| 807 | .set = set_cmb, |
| 808 | .read = read_cmb, |
| 809 | .readall = readall_cmb, |
| 810 | .reset = reset_cmb, |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 811 | .align = align_cmb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | .attr_group = &cmf_attr_group, |
| 813 | }; |
Heiko Carstens | 364c855 | 2007-10-12 16:11:35 +0200 | [diff] [blame] | 814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | /* ******** extended cmb handling ********/ |
| 816 | |
| 817 | /** |
| 818 | * struct cmbe - extended channel measurement block |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 819 | * @ssch_rsch_count: number of ssch and rsch |
| 820 | * @sample_count: number of samples |
| 821 | * @device_connect_time: time of device connect |
| 822 | * @function_pending_time: time of function pending |
| 823 | * @device_disconnect_time: time of device disconnect |
| 824 | * @control_unit_queuing_time: time of control unit queuing |
| 825 | * @device_active_only_time: time of device active only |
| 826 | * @device_busy_time: time of device busy |
| 827 | * @initial_command_response_time: initial command response time |
| 828 | * @reserved: unused |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | * |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 830 | * The measurement block as used by the hardware. May be in any 64 bit physical |
| 831 | * location. |
| 832 | * The fields are described further in z/Architecture Principles of Operation, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | * third edition, chapter 17. |
| 834 | */ |
| 835 | struct cmbe { |
| 836 | u32 ssch_rsch_count; |
| 837 | u32 sample_count; |
| 838 | u32 device_connect_time; |
| 839 | u32 function_pending_time; |
| 840 | u32 device_disconnect_time; |
| 841 | u32 control_unit_queuing_time; |
| 842 | u32 device_active_only_time; |
| 843 | u32 device_busy_time; |
| 844 | u32 initial_command_response_time; |
| 845 | u32 reserved[7]; |
| 846 | }; |
| 847 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 848 | /* |
| 849 | * kmalloc only guarantees 8 byte alignment, but we need cmbe |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | * pointers to be naturally aligned. Make sure to allocate |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 851 | * enough space for two cmbes. |
| 852 | */ |
| 853 | static inline struct cmbe *cmbe_align(struct cmbe *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | { |
| 855 | unsigned long addr; |
| 856 | addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) & |
| 857 | ~(sizeof (struct cmbe) - sizeof(long)); |
| 858 | return (struct cmbe*)addr; |
| 859 | } |
| 860 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 861 | static int alloc_cmbe(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { |
| 863 | struct cmbe *cmbe; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 864 | struct cmb_data *cmb_data; |
| 865 | int ret; |
| 866 | |
| 867 | cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (!cmbe) |
| 869 | return -ENOMEM; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 870 | cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL); |
| 871 | if (!cmb_data) { |
| 872 | ret = -ENOMEM; |
| 873 | goto out_free; |
| 874 | } |
| 875 | cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL); |
| 876 | if (!cmb_data->last_block) { |
| 877 | ret = -ENOMEM; |
| 878 | goto out_free; |
| 879 | } |
| 880 | cmb_data->size = sizeof(struct cmbe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | spin_lock_irq(cdev->ccwlock); |
| 882 | if (cdev->private->cmb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | spin_unlock_irq(cdev->ccwlock); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 884 | ret = -EBUSY; |
| 885 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 887 | cmb_data->hw_block = cmbe; |
| 888 | cdev->private->cmb = cmb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | spin_unlock_irq(cdev->ccwlock); |
| 890 | |
| 891 | /* activate global measurement if this is the first channel */ |
| 892 | spin_lock(&cmb_area.lock); |
| 893 | if (list_empty(&cmb_area.list)) |
| 894 | cmf_activate(NULL, 1); |
| 895 | list_add_tail(&cdev->private->cmb_list, &cmb_area.list); |
| 896 | spin_unlock(&cmb_area.lock); |
| 897 | |
| 898 | return 0; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 899 | out_free: |
| 900 | if (cmb_data) |
| 901 | kfree(cmb_data->last_block); |
| 902 | kfree(cmb_data); |
| 903 | kfree(cmbe); |
| 904 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 907 | static void free_cmbe(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 909 | struct cmb_data *cmb_data; |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | spin_lock_irq(cdev->ccwlock); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 912 | cmb_data = cdev->private->cmb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | cdev->private->cmb = NULL; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 914 | if (cmb_data) |
| 915 | kfree(cmb_data->last_block); |
| 916 | kfree(cmb_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | spin_unlock_irq(cdev->ccwlock); |
| 918 | |
| 919 | /* deactivate global measurement if this is the last channel */ |
| 920 | spin_lock(&cmb_area.lock); |
| 921 | list_del_init(&cdev->private->cmb_list); |
| 922 | if (list_empty(&cmb_area.list)) |
| 923 | cmf_activate(NULL, 0); |
| 924 | spin_unlock(&cmb_area.lock); |
| 925 | } |
| 926 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 927 | static int set_cmbe(struct ccw_device *cdev, u32 mme) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { |
| 929 | unsigned long mba; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 930 | struct cmb_data *cmb_data; |
| 931 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 933 | spin_lock_irqsave(cdev->ccwlock, flags); |
| 934 | if (!cdev->private->cmb) { |
| 935 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | return -EINVAL; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 937 | } |
| 938 | cmb_data = cdev->private->cmb; |
| 939 | mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0; |
| 940 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
| 942 | return set_schib_wait(cdev, mme, 1, mba); |
| 943 | } |
| 944 | |
| 945 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 946 | static u64 read_cmbe(struct ccw_device *cdev, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 948 | struct cmbe *cmb; |
| 949 | struct cmb_data *cmb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | u32 val; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 951 | int ret; |
| 952 | unsigned long flags; |
| 953 | |
| 954 | ret = cmf_cmb_copy_wait(cdev); |
| 955 | if (ret < 0) |
| 956 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | spin_lock_irqsave(cdev->ccwlock, flags); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 959 | cmb_data = cdev->private->cmb; |
| 960 | if (!cmb_data) { |
| 961 | ret = 0; |
| 962 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 964 | cmb = cmb_data->last_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
| 966 | switch (index) { |
| 967 | case cmb_ssch_rsch_count: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 968 | ret = cmb->ssch_rsch_count; |
| 969 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | case cmb_sample_count: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 971 | ret = cmb->sample_count; |
| 972 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | case cmb_device_connect_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 974 | val = cmb->device_connect_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | break; |
| 976 | case cmb_function_pending_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 977 | val = cmb->function_pending_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | break; |
| 979 | case cmb_device_disconnect_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 980 | val = cmb->device_disconnect_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | break; |
| 982 | case cmb_control_unit_queuing_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 983 | val = cmb->control_unit_queuing_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | break; |
| 985 | case cmb_device_active_only_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 986 | val = cmb->device_active_only_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | break; |
| 988 | case cmb_device_busy_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 989 | val = cmb->device_busy_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | break; |
| 991 | case cmb_initial_command_response_time: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 992 | val = cmb->initial_command_response_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | break; |
| 994 | default: |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 995 | ret = 0; |
| 996 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 998 | ret = time_to_avg_nsec(val, cmb->sample_count); |
| 999 | out: |
| 1000 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 1001 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1004 | static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1006 | struct cmbe *cmb; |
| 1007 | struct cmb_data *cmb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | u64 time; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1009 | unsigned long flags; |
| 1010 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1012 | ret = cmf_cmb_copy_wait(cdev); |
| 1013 | if (ret < 0) |
| 1014 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | spin_lock_irqsave(cdev->ccwlock, flags); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1016 | cmb_data = cdev->private->cmb; |
| 1017 | if (!cmb_data) { |
| 1018 | ret = -ENODEV; |
| 1019 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | } |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1021 | if (cmb_data->last_update == 0) { |
| 1022 | ret = -EAGAIN; |
| 1023 | goto out; |
| 1024 | } |
| 1025 | time = cmb_data->last_update - cdev->private->cmb_start_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
| 1027 | memset (data, 0, sizeof(struct cmbdata)); |
| 1028 | |
| 1029 | /* we only know values before device_busy_time */ |
| 1030 | data->size = offsetof(struct cmbdata, device_busy_time); |
| 1031 | |
| 1032 | /* conver to nanoseconds */ |
| 1033 | data->elapsed_time = (time * 1000) >> 12; |
| 1034 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1035 | cmb = cmb_data->last_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | /* copy data to new structure */ |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1037 | data->ssch_rsch_count = cmb->ssch_rsch_count; |
| 1038 | data->sample_count = cmb->sample_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
| 1040 | /* time fields are converted to nanoseconds while copying */ |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1041 | data->device_connect_time = time_to_nsec(cmb->device_connect_time); |
| 1042 | data->function_pending_time = time_to_nsec(cmb->function_pending_time); |
| 1043 | data->device_disconnect_time = |
| 1044 | time_to_nsec(cmb->device_disconnect_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | data->control_unit_queuing_time |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1046 | = time_to_nsec(cmb->control_unit_queuing_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | data->device_active_only_time |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1048 | = time_to_nsec(cmb->device_active_only_time); |
| 1049 | data->device_busy_time = time_to_nsec(cmb->device_busy_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | data->initial_command_response_time |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1051 | = time_to_nsec(cmb->initial_command_response_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1053 | ret = 0; |
| 1054 | out: |
| 1055 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 1056 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1059 | static void reset_cmbe(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1061 | cmf_generic_reset(cdev); |
| 1062 | } |
| 1063 | |
| 1064 | static void * align_cmbe(void *area) |
| 1065 | { |
| 1066 | return cmbe_align(area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static struct attribute_group cmf_attr_group_ext; |
| 1070 | |
| 1071 | static struct cmb_operations cmbops_extended = { |
| 1072 | .alloc = alloc_cmbe, |
| 1073 | .free = free_cmbe, |
| 1074 | .set = set_cmbe, |
| 1075 | .read = read_cmbe, |
| 1076 | .readall = readall_cmbe, |
| 1077 | .reset = reset_cmbe, |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1078 | .align = align_cmbe, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | .attr_group = &cmf_attr_group_ext, |
| 1080 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1082 | static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | { |
| 1084 | return sprintf(buf, "%lld\n", |
| 1085 | (unsigned long long) cmf_read(to_ccwdev(dev), idx)); |
| 1086 | } |
| 1087 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1088 | static ssize_t cmb_show_avg_sample_interval(struct device *dev, |
| 1089 | struct device_attribute *attr, |
| 1090 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
| 1092 | struct ccw_device *cdev; |
| 1093 | long interval; |
| 1094 | unsigned long count; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1095 | struct cmb_data *cmb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
| 1097 | cdev = to_ccwdev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | count = cmf_read(cdev, cmb_sample_count); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1099 | spin_lock_irq(cdev->ccwlock); |
| 1100 | cmb_data = cdev->private->cmb; |
| 1101 | if (count) { |
| 1102 | interval = cmb_data->last_update - |
| 1103 | cdev->private->cmb_start_time; |
Cornelia Huck | 13ffa92 | 2006-07-17 16:09:28 +0200 | [diff] [blame] | 1104 | interval = (interval * 1000) >> 12; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | interval /= count; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1106 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | interval = -1; |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1108 | spin_unlock_irq(cdev->ccwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | return sprintf(buf, "%ld\n", interval); |
| 1110 | } |
| 1111 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1112 | static ssize_t cmb_show_avg_utilization(struct device *dev, |
| 1113 | struct device_attribute *attr, |
| 1114 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
| 1116 | struct cmbdata data; |
| 1117 | u64 utilization; |
| 1118 | unsigned long t, u; |
| 1119 | int ret; |
| 1120 | |
| 1121 | ret = cmf_readall(to_ccwdev(dev), &data); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1122 | if (ret == -EAGAIN || ret == -ENODEV) |
| 1123 | /* No data (yet/currently) available to use for calculation. */ |
| 1124 | return sprintf(buf, "n/a\n"); |
| 1125 | else if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | return ret; |
| 1127 | |
| 1128 | utilization = data.device_connect_time + |
| 1129 | data.function_pending_time + |
| 1130 | data.device_disconnect_time; |
| 1131 | |
| 1132 | /* shift to avoid long long division */ |
| 1133 | while (-1ul < (data.elapsed_time | utilization)) { |
| 1134 | utilization >>= 8; |
| 1135 | data.elapsed_time >>= 8; |
| 1136 | } |
| 1137 | |
| 1138 | /* calculate value in 0.1 percent units */ |
| 1139 | t = (unsigned long) data.elapsed_time / 1000; |
| 1140 | u = (unsigned long) utilization / t; |
| 1141 | |
| 1142 | return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10); |
| 1143 | } |
| 1144 | |
| 1145 | #define cmf_attr(name) \ |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1146 | static ssize_t show_##name(struct device *dev, \ |
| 1147 | struct device_attribute *attr, char *buf) \ |
| 1148 | { return cmb_show_attr((dev), buf, cmb_##name); } \ |
| 1149 | static DEVICE_ATTR(name, 0444, show_##name, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
| 1151 | #define cmf_attr_avg(name) \ |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1152 | static ssize_t show_avg_##name(struct device *dev, \ |
| 1153 | struct device_attribute *attr, char *buf) \ |
| 1154 | { return cmb_show_attr((dev), buf, cmb_##name); } \ |
| 1155 | static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
| 1157 | cmf_attr(ssch_rsch_count); |
| 1158 | cmf_attr(sample_count); |
| 1159 | cmf_attr_avg(device_connect_time); |
| 1160 | cmf_attr_avg(function_pending_time); |
| 1161 | cmf_attr_avg(device_disconnect_time); |
| 1162 | cmf_attr_avg(control_unit_queuing_time); |
| 1163 | cmf_attr_avg(device_active_only_time); |
| 1164 | cmf_attr_avg(device_busy_time); |
| 1165 | cmf_attr_avg(initial_command_response_time); |
| 1166 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1167 | static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval, |
| 1168 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL); |
| 1170 | |
| 1171 | static struct attribute *cmf_attributes[] = { |
| 1172 | &dev_attr_avg_sample_interval.attr, |
| 1173 | &dev_attr_avg_utilization.attr, |
| 1174 | &dev_attr_ssch_rsch_count.attr, |
| 1175 | &dev_attr_sample_count.attr, |
| 1176 | &dev_attr_avg_device_connect_time.attr, |
| 1177 | &dev_attr_avg_function_pending_time.attr, |
| 1178 | &dev_attr_avg_device_disconnect_time.attr, |
| 1179 | &dev_attr_avg_control_unit_queuing_time.attr, |
| 1180 | &dev_attr_avg_device_active_only_time.attr, |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1181 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | }; |
| 1183 | |
| 1184 | static struct attribute_group cmf_attr_group = { |
| 1185 | .name = "cmf", |
| 1186 | .attrs = cmf_attributes, |
| 1187 | }; |
| 1188 | |
| 1189 | static struct attribute *cmf_attributes_ext[] = { |
| 1190 | &dev_attr_avg_sample_interval.attr, |
| 1191 | &dev_attr_avg_utilization.attr, |
| 1192 | &dev_attr_ssch_rsch_count.attr, |
| 1193 | &dev_attr_sample_count.attr, |
| 1194 | &dev_attr_avg_device_connect_time.attr, |
| 1195 | &dev_attr_avg_function_pending_time.attr, |
| 1196 | &dev_attr_avg_device_disconnect_time.attr, |
| 1197 | &dev_attr_avg_control_unit_queuing_time.attr, |
| 1198 | &dev_attr_avg_device_active_only_time.attr, |
| 1199 | &dev_attr_avg_device_busy_time.attr, |
| 1200 | &dev_attr_avg_initial_command_response_time.attr, |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1201 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | }; |
| 1203 | |
| 1204 | static struct attribute_group cmf_attr_group_ext = { |
| 1205 | .name = "cmf", |
| 1206 | .attrs = cmf_attributes_ext, |
| 1207 | }; |
| 1208 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1209 | static ssize_t cmb_enable_show(struct device *dev, |
| 1210 | struct device_attribute *attr, |
| 1211 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | { |
| 1213 | return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0); |
| 1214 | } |
| 1215 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1216 | static ssize_t cmb_enable_store(struct device *dev, |
| 1217 | struct device_attribute *attr, const char *buf, |
| 1218 | size_t c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | { |
| 1220 | struct ccw_device *cdev; |
| 1221 | int ret; |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 1222 | unsigned long val; |
| 1223 | |
| 1224 | ret = strict_strtoul(buf, 16, &val); |
| 1225 | if (ret) |
| 1226 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
| 1228 | cdev = to_ccwdev(dev); |
| 1229 | |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 1230 | switch (val) { |
| 1231 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | ret = disable_cmf(cdev); |
| 1233 | if (ret) |
Cornelia Huck | e556bbb | 2007-07-27 12:29:19 +0200 | [diff] [blame] | 1234 | dev_info(&cdev->dev, "disable_cmf failed (%d)\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | break; |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 1236 | case 1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | ret = enable_cmf(cdev); |
| 1238 | if (ret && ret != -EBUSY) |
Cornelia Huck | e556bbb | 2007-07-27 12:29:19 +0200 | [diff] [blame] | 1239 | dev_info(&cdev->dev, "enable_cmf failed (%d)\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | break; |
| 1241 | } |
| 1242 | |
| 1243 | return c; |
| 1244 | } |
| 1245 | |
| 1246 | DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store); |
| 1247 | |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 1248 | /** |
| 1249 | * enable_cmf() - switch on the channel measurement for a specific device |
| 1250 | * @cdev: The ccw device to be enabled |
| 1251 | * |
| 1252 | * Returns %0 for success or a negative error value. |
| 1253 | * |
| 1254 | * Context: |
| 1255 | * non-atomic |
| 1256 | */ |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1257 | int enable_cmf(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | { |
| 1259 | int ret; |
| 1260 | |
| 1261 | ret = cmbops->alloc(cdev); |
| 1262 | cmbops->reset(cdev); |
| 1263 | if (ret) |
| 1264 | return ret; |
| 1265 | ret = cmbops->set(cdev, 2); |
| 1266 | if (ret) { |
| 1267 | cmbops->free(cdev); |
| 1268 | return ret; |
| 1269 | } |
| 1270 | ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group); |
| 1271 | if (!ret) |
| 1272 | return 0; |
| 1273 | cmbops->set(cdev, 0); //FIXME: this can fail |
| 1274 | cmbops->free(cdev); |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 1278 | /** |
| 1279 | * disable_cmf() - switch off the channel measurement for a specific device |
| 1280 | * @cdev: The ccw device to be disabled |
| 1281 | * |
| 1282 | * Returns %0 for success or a negative error value. |
| 1283 | * |
| 1284 | * Context: |
| 1285 | * non-atomic |
| 1286 | */ |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1287 | int disable_cmf(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | { |
| 1289 | int ret; |
| 1290 | |
| 1291 | ret = cmbops->set(cdev, 0); |
| 1292 | if (ret) |
| 1293 | return ret; |
| 1294 | cmbops->free(cdev); |
| 1295 | sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group); |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 1299 | /** |
| 1300 | * cmf_read() - read one value from the current channel measurement block |
| 1301 | * @cdev: the channel to be read |
| 1302 | * @index: the index of the value to be read |
| 1303 | * |
| 1304 | * Returns the value read or %0 if the value cannot be read. |
| 1305 | * |
| 1306 | * Context: |
| 1307 | * any |
| 1308 | */ |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1309 | u64 cmf_read(struct ccw_device *cdev, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | { |
| 1311 | return cmbops->read(cdev, index); |
| 1312 | } |
| 1313 | |
Cornelia Huck | c020871 | 2007-10-12 16:11:16 +0200 | [diff] [blame] | 1314 | /** |
| 1315 | * cmf_readall() - read the current channel measurement block |
| 1316 | * @cdev: the channel to be read |
| 1317 | * @data: a pointer to a data block that will be filled |
| 1318 | * |
| 1319 | * Returns %0 on success, a negative error value otherwise. |
| 1320 | * |
| 1321 | * Context: |
| 1322 | * any |
| 1323 | */ |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1324 | int cmf_readall(struct ccw_device *cdev, struct cmbdata *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | { |
| 1326 | return cmbops->readall(cdev, data); |
| 1327 | } |
| 1328 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1329 | /* Reenable cmf when a disconnected device becomes available again. */ |
| 1330 | int cmf_reenable(struct ccw_device *cdev) |
| 1331 | { |
| 1332 | cmbops->reset(cdev); |
| 1333 | return cmbops->set(cdev, 2); |
| 1334 | } |
| 1335 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1336 | static int __init init_cmf(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | { |
| 1338 | char *format_string; |
| 1339 | char *detect_string = "parameter"; |
| 1340 | |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1341 | /* |
| 1342 | * If the user did not give a parameter, see if we are running on a |
| 1343 | * machine supporting extended measurement blocks, otherwise fall back |
| 1344 | * to basic mode. |
| 1345 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | if (format == CMF_AUTODETECT) { |
| 1347 | if (!css_characteristics_avail || |
| 1348 | !css_general_characteristics.ext_mb) { |
| 1349 | format = CMF_BASIC; |
| 1350 | } else { |
| 1351 | format = CMF_EXTENDED; |
| 1352 | } |
| 1353 | detect_string = "autodetected"; |
| 1354 | } else { |
| 1355 | detect_string = "parameter"; |
| 1356 | } |
| 1357 | |
| 1358 | switch (format) { |
| 1359 | case CMF_BASIC: |
| 1360 | format_string = "basic"; |
| 1361 | cmbops = &cmbops_basic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | break; |
| 1363 | case CMF_EXTENDED: |
Cornelia Huck | fc5019c | 2007-10-12 16:11:15 +0200 | [diff] [blame] | 1364 | format_string = "extended"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | cmbops = &cmbops_extended; |
| 1366 | break; |
| 1367 | default: |
Cornelia Huck | e556bbb | 2007-07-27 12:29:19 +0200 | [diff] [blame] | 1368 | printk(KERN_ERR "cio: Invalid format %d for channel " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | "measurement facility\n", format); |
| 1370 | return 1; |
| 1371 | } |
| 1372 | |
Cornelia Huck | e556bbb | 2007-07-27 12:29:19 +0200 | [diff] [blame] | 1373 | printk(KERN_INFO "cio: Channel measurement facility using %s " |
| 1374 | "format (%s)\n", format_string, detect_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | module_init(init_cmf); |
| 1379 | |
| 1380 | |
| 1381 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); |
| 1382 | MODULE_LICENSE("GPL"); |
| 1383 | MODULE_DESCRIPTION("channel measurement facility base driver\n" |
| 1384 | "Copyright 2003 IBM Corporation\n"); |
| 1385 | |
| 1386 | EXPORT_SYMBOL_GPL(enable_cmf); |
| 1387 | EXPORT_SYMBOL_GPL(disable_cmf); |
| 1388 | EXPORT_SYMBOL_GPL(cmf_read); |
| 1389 | EXPORT_SYMBOL_GPL(cmf_readall); |