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