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