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