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