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