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