blob: 268aa23afa0166a3a509fb3d7b7e50d1f1f4515c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Linux on zSeries Channel Measurement Facility support
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2000, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Cornelia Huck94bb0632006-06-29 15:08:41 +02006 * Authors: Arnd Bergmann <arndb@de.ibm.com>
7 * Cornelia Huck <cornelia.huck@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 Ernste6d5a422008-12-25 13:39:36 +010026#define KMSG_COMPONENT "cio"
27#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/bootmem.h>
30#include <linux/device.h>
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080035#include <linux/slab.h>
Heiko Carstens1aae0562013-01-30 09:49:40 +010036#include <linux/timex.h> /* get_tod_clock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/ccwdev.h>
39#include <asm/cio.h>
40#include <asm/cmb.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080041#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "cio.h"
44#include "css.h"
45#include "device.h"
46#include "ioasm.h"
47#include "chsc.h"
48
Cornelia Huckfc5019c2007-10-12 16:11:15 +020049/*
50 * parameter to enable cmf during boot, possible uses are:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * "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 */
60enum 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 Huckc0208712007-10-12 16:11:16 +020078 * by all machines that we run on
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * @CMF_EXTENDED: improved format that was introduced with the z990
Cornelia Huckc0208712007-10-12 16:11:16 +020080 * 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 Torvalds1da177e2005-04-16 15:20:36 -070085enum cmb_format {
86 CMF_BASIC,
87 CMF_EXTENDED,
88 CMF_AUTODETECT = -1,
89};
Cornelia Huckfc5019c2007-10-12 16:11:15 +020090
Cornelia Huckc0208712007-10-12 16:11:16 +020091/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * 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 */
98static int format = CMF_AUTODETECT;
Rusty Russell69116f22012-01-13 09:32:17 +103099module_param(format, bint, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/**
102 * struct cmb_operations - functions to use depending on cmb_format
103 *
Cornelia Huck94bb0632006-06-29 15:08:41 +0200104 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700107 *
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 Huckc0208712007-10-12 16:11:16 +0200112 * @read: read a measurement entry at an index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * @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 */
117struct cmb_operations {
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200118 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 Huckc0208712007-10-12 16:11:16 +0200124/* private: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct attribute_group *attr_group;
126};
127static struct cmb_operations *cmbops;
128
Cornelia Huck94bb0632006-06-29 15:08:41 +0200129struct 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 Huckfc5019c2007-10-12 16:11:15 +0200136/*
137 * Our user interface is designed in terms of nanoseconds,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * while the hardware measures total times in its own
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200139 * unit.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static 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 */
152static 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 Engelhardt96de0e22007-10-19 23:21:04 +0200160 /* value comes in units of 128 µsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 ret = time_to_nsec(value);
162 do_div(ret, count);
163
164 return ret;
165}
166
Heiko Carstens7b4ff872016-06-20 14:03:38 +0200167#define CMF_OFF 0
168#define CMF_ON 2
169
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200170/*
171 * Activate or deactivate the channel monitor. When area is NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * the monitor is deactivated. The channel monitor needs to
173 * be active in order to measure subchannels, which also need
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200174 * to be enabled.
175 */
176static inline void cmf_activate(void *area, unsigned int onoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 register void * __gpr2 asm("2");
179 register long __gpr1 asm("1");
180
181 __gpr2 = area;
Heiko Carstens7b4ff872016-06-20 14:03:38 +0200182 __gpr1 = onoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* activate channel measurement */
184 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
185}
186
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200187static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
188 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Sebastian Otta6ef1562015-09-07 19:51:39 +0200190 struct subchannel *sch = to_subchannel(cdev->dev.parent);
191 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Sebastian Ott13952ec2008-12-25 13:39:13 +0100193 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 Torvalds1da177e2005-04-16 15:20:36 -0700200
Sebastian Otta6ef1562015-09-07 19:51:39 +0200201 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 Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212struct set_schib_struct {
213 u32 mme;
214 int mbfc;
215 unsigned long address;
216 wait_queue_head_t wait;
217 int ret;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200218 struct kref kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
Cornelia Huck94bb0632006-06-29 15:08:41 +0200221static 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 Torvalds1da177e2005-04-16 15:20:36 -0700231static int set_schib_wait(struct ccw_device *cdev, u32 mme,
232 int mbfc, unsigned long address)
233{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200234 struct set_schib_struct *set_data;
235 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200238 if (!cdev->private->cmb) {
239 ret = -ENODEV;
240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200242 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 Torvalds1da177e2005-04-16 15:20:36 -0700256
257 if (cdev->private->state != DEV_STATE_ONLINE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* if the device is not online, don't even try again */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200259 ret = -EBUSY;
260 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 cdev->private->state = DEV_STATE_CMFCHANGE;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200264 set_data->ret = CMF_PENDING;
265 cdev->private->cmb_wait = set_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200268 if (wait_event_interruptible(set_data->wait,
269 set_data->ret != CMF_PENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200271 if (set_data->ret == CMF_PENDING) {
272 set_data->ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (cdev->private->state == DEV_STATE_CMFCHANGE)
274 cdev->private->state = DEV_STATE_ONLINE;
275 }
276 spin_unlock_irq(cdev->ccwlock);
277 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200278 spin_lock_irq(cdev->ccwlock);
279 cdev->private->cmb_wait = NULL;
280 ret = set_data->ret;
281out_put:
282 kref_put(&set_data->kref, cmf_set_schib_release);
283out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288void retry_set_schib(struct ccw_device *cdev)
289{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200290 struct set_schib_struct *set_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Cornelia Huck94bb0632006-06-29 15:08:41 +0200292 set_data = cdev->private->cmb_wait;
293 if (!set_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 WARN_ON(1);
295 return;
296 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200297 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
304static 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 Ottcdb912a2008-12-25 13:39:12 +0100313 if (cio_update_schib(sch))
Cornelia Huck94bb0632006-06-29 15:08:41 +0200314 return -ENODEV;
315
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200316 if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200317 /* Don't copy if a start function is in progress. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200318 if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
319 (scsw_actl(&sch->schib.scsw) &
Cornelia Huck94bb0632006-06-29 15:08:41 +0200320 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200321 (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
Cornelia Huck94bb0632006-06-29 15:08:41 +0200322 return -EBUSY;
323 }
324 cmb_data = cdev->private->cmb;
Sebastian Ott45bf4b92015-09-07 19:53:01 +0200325 hw_block = cmb_data->hw_block;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200326 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 Carstens1aae0562013-01-30 09:49:40 +0100337 cmb_data->last_update = get_tod_clock();
Cornelia Huck94bb0632006-06-29 15:08:41 +0200338 kfree(reference_buf);
339 return 0;
340}
341
342struct copy_block_struct {
343 wait_queue_head_t wait;
344 int ret;
345 struct kref kref;
346};
347
348static 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
356static 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(&copy_block->wait);
373 kref_init(&copy_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;
402out_put:
403 kref_put(&copy_block->kref, cmf_copy_block_release);
404out:
405 spin_unlock_irqrestore(cdev->ccwlock, flags);
406 return ret;
407}
408
409void 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(&copy_block->kref);
419 copy_block->ret = cmf_copy_block(cdev);
420 wake_up(&copy_block->wait);
421 kref_put(&copy_block->kref, cmf_copy_block_release);
422}
423
424static 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 Ott45bf4b92015-09-07 19:53:01 +0200436 memset(cmb_data->hw_block, 0, cmb_data->size);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200437 cmb_data->last_update = 0;
438 }
Heiko Carstens1aae0562013-01-30 09:49:40 +0100439 cdev->private->cmb_start_time = get_tod_clock();
Cornelia Huck94bb0632006-06-29 15:08:41 +0200440 spin_unlock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
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 Huckc0208712007-10-12 16:11:16 +0200448 * @num_channels: number of channels to be measured
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * @lock: protect concurrent access to @mem and @list
450 */
451struct cmb_area {
452 struct cmb *mem;
453 struct list_head list;
454 int num_channels;
455 spinlock_t lock;
456};
457
458static struct cmb_area cmb_area = {
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200459 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 .list = LIST_HEAD_INIT(cmb_area.list),
461 .num_channels = 1024,
462};
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/* ****** old style CMB handling ********/
465
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * 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 Rosaaf901ca2009-11-14 13:09:05 -0200471 * a reasonable value of 1024, or 32 kb of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * Current kernels don't allow kmalloc with more than 128kb, so the
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200473 * maximum is 4096.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
475
476module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
477
478/**
479 * struct cmb - basic channel measurement block
Cornelia Huckc0208712007-10-12 16:11:16 +0200480 * @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 Torvalds1da177e2005-04-16 15:20:36 -0700488 *
Cornelia Huckc0208712007-10-12 16:11:16 +0200489 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700494 * Only one cmb area can be present in the system.
495 */
496struct 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 Huckfc5019c2007-10-12 16:11:15 +0200507/*
508 * Insert a single device into the cmb_area list.
509 * Called with cmb_area.lock held from alloc_cmb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100511static int alloc_cmb_single(struct ccw_device *cdev,
512 struct cmb_data *cmb_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
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 Huckfc5019c2007-10-12 16:11:15 +0200524 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700529 cmb = cmb_area.mem;
530 list_for_each_entry(node, &cmb_area.list, cmb_list) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200531 struct cmb_data *data;
532 data = node->cmb;
533 if ((struct cmb*)data->hw_block > cmb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 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 Huck94bb0632006-06-29 15:08:41 +0200544 cmb_data->hw_block = cmb;
545 cdev->private->cmb = cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ret = 0;
547out:
548 spin_unlock_irq(cdev->ccwlock);
549 return ret;
550}
551
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200552static int alloc_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 int ret;
555 struct cmb *mem;
556 ssize_t size;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200557 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Cornelia Huck94bb0632006-06-29 15:08:41 +0200559 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700570 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 Carstens7b4ff872016-06-20 14:03:38 +0200593 cmf_activate(cmb_area.mem, CMF_ON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 }
596
597 /* do the actual allocation */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200598 ret = alloc_cmb_single(cdev, cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599out:
600 spin_unlock(&cmb_area.lock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200601 if (ret) {
602 kfree(cmb_data->last_block);
603 kfree(cmb_data);
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return ret;
606}
607
Cornelia Huck94bb0632006-06-29 15:08:41 +0200608static void free_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct ccw_device_private *priv;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200611 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 spin_lock(&cmb_area.lock);
614 spin_lock_irq(cdev->ccwlock);
615
Cornelia Huck94bb0632006-06-29 15:08:41 +0200616 priv = cdev->private;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200617 cmb_data = priv->cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 priv->cmb = NULL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200619 if (cmb_data)
620 kfree(cmb_data->last_block);
621 kfree(cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 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 Carstens7b4ff872016-06-20 14:03:38 +0200627 cmf_activate(NULL, CMF_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 free_pages((unsigned long)cmb_area.mem, get_order(size));
629 cmb_area.mem = NULL;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 spin_unlock_irq(cdev->ccwlock);
632 spin_unlock(&cmb_area.lock);
633}
634
Cornelia Huck94bb0632006-06-29 15:08:41 +0200635static int set_cmb(struct ccw_device *cdev, u32 mme)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 u16 offset;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200638 struct cmb_data *cmb_data;
639 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Cornelia Huck94bb0632006-06-29 15:08:41 +0200641 spin_lock_irqsave(cdev->ccwlock, flags);
642 if (!cdev->private->cmb) {
643 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EINVAL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200645 }
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 Torvalds1da177e2005-04-16 15:20:36 -0700649
650 return set_schib_wait(cdev, mme, 0, offset);
651}
652
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200653static u64 read_cmb(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200655 struct cmb *cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u32 val;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200657 int ret;
658 unsigned long flags;
659
660 ret = cmf_cmb_copy_wait(cdev);
661 if (ret < 0)
662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 spin_lock_irqsave(cdev->ccwlock, flags);
665 if (!cdev->private->cmb) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200666 ret = 0;
667 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200669 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 switch (index) {
672 case cmb_ssch_rsch_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200673 ret = cmb->ssch_rsch_count;
674 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 case cmb_sample_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200676 ret = cmb->sample_count;
677 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 case cmb_device_connect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200679 val = cmb->device_connect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 break;
681 case cmb_function_pending_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200682 val = cmb->function_pending_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
684 case cmb_device_disconnect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200685 val = cmb->device_disconnect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
687 case cmb_control_unit_queuing_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200688 val = cmb->control_unit_queuing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 break;
690 case cmb_device_active_only_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200691 val = cmb->device_active_only_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
693 default:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200694 ret = 0;
695 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200697 ret = time_to_avg_nsec(val, cmb->sample_count);
698out:
699 spin_unlock_irqrestore(cdev->ccwlock, flags);
700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200703static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200705 struct cmb *cmb;
706 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 u64 time;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200708 unsigned long flags;
709 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Cornelia Huck94bb0632006-06-29 15:08:41 +0200711 ret = cmf_cmb_copy_wait(cdev);
712 if (ret < 0)
713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200715 cmb_data = cdev->private->cmb;
716 if (!cmb_data) {
717 ret = -ENODEV;
718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200720 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 Torvalds1da177e2005-04-16 15:20:36 -0700726
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 Huck94bb0632006-06-29 15:08:41 +0200736 data->ssch_rsch_count = cmb->ssch_rsch_count;
737 data->sample_count = cmb->sample_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* time fields are converted to nanoseconds while copying */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200740 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 Torvalds1da177e2005-04-16 15:20:36 -0700744 data->control_unit_queuing_time
Cornelia Huck94bb0632006-06-29 15:08:41 +0200745 = time_to_nsec(cmb->control_unit_queuing_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 data->device_active_only_time
Cornelia Huck94bb0632006-06-29 15:08:41 +0200747 = time_to_nsec(cmb->device_active_only_time);
748 ret = 0;
749out:
750 spin_unlock_irqrestore(cdev->ccwlock, flags);
751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Cornelia Huck94bb0632006-06-29 15:08:41 +0200754static void reset_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200756 cmf_generic_reset(cdev);
757}
758
Sebastian Ott0f5d0502016-07-12 19:57:57 +0200759static 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 Torvalds1da177e2005-04-16 15:20:36 -0700770static struct attribute_group cmf_attr_group;
771
772static 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 Carstens364c8552007-10-12 16:11:35 +0200781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/* ******** extended cmb handling ********/
783
784/**
785 * struct cmbe - extended channel measurement block
Cornelia Huckc0208712007-10-12 16:11:16 +0200786 * @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 Torvalds1da177e2005-04-16 15:20:36 -0700796 *
Cornelia Huckc0208712007-10-12 16:11:16 +0200797 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700800 * third edition, chapter 17.
801 */
802struct 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 Ott45bf4b92015-09-07 19:53:01 +0200813} __packed __aligned(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Sebastian Ott45bf4b92015-09-07 19:53:01 +0200815static struct kmem_cache *cmbe_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200817static int alloc_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200819 struct cmb_data *cmb_data;
Sebastian Ott616503d2015-09-07 19:52:06 +0200820 struct cmbe *cmbe;
821 int ret = -ENOMEM;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200822
Sebastian Ott45bf4b92015-09-07 19:53:01 +0200823 cmbe = kmem_cache_zalloc(cmbe_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!cmbe)
Sebastian Ott616503d2015-09-07 19:52:06 +0200825 return ret;
826
Sebastian Ott45bf4b92015-09-07 19:53:01 +0200827 cmb_data = kzalloc(sizeof(*cmb_data), GFP_KERNEL);
Sebastian Ott616503d2015-09-07 19:52:06 +0200828 if (!cmb_data)
Cornelia Huck94bb0632006-06-29 15:08:41 +0200829 goto out_free;
Sebastian Ott616503d2015-09-07 19:52:06 +0200830
Cornelia Huck94bb0632006-06-29 15:08:41 +0200831 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
Sebastian Ott616503d2015-09-07 19:52:06 +0200832 if (!cmb_data->last_block)
Cornelia Huck94bb0632006-06-29 15:08:41 +0200833 goto out_free;
Sebastian Ott616503d2015-09-07 19:52:06 +0200834
Sebastian Ott45bf4b92015-09-07 19:53:01 +0200835 cmb_data->size = sizeof(*cmbe);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200836 cmb_data->hw_block = cmbe;
Sebastian Ott616503d2015-09-07 19:52:06 +0200837
838 spin_lock(&cmb_area.lock);
839 spin_lock_irq(cdev->ccwlock);
840 if (cdev->private->cmb)
841 goto out_unlock;
842
Cornelia Huck94bb0632006-06-29 15:08:41 +0200843 cdev->private->cmb = cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* activate global measurement if this is the first channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (list_empty(&cmb_area.list))
Heiko Carstens7b4ff872016-06-20 14:03:38 +0200847 cmf_activate(NULL, CMF_ON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Sebastian Ott616503d2015-09-07 19:52:06 +0200850 spin_unlock_irq(cdev->ccwlock);
851 spin_unlock(&cmb_area.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return 0;
Sebastian Ott616503d2015-09-07 19:52:06 +0200853
854out_unlock:
855 spin_unlock_irq(cdev->ccwlock);
856 spin_unlock(&cmb_area.lock);
857 ret = -EBUSY;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200858out_free:
859 if (cmb_data)
860 kfree(cmb_data->last_block);
861 kfree(cmb_data);
Sebastian Ott45bf4b92015-09-07 19:53:01 +0200862 kmem_cache_free(cmbe_cache, cmbe);
863
Cornelia Huck94bb0632006-06-29 15:08:41 +0200864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200867static void free_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200869 struct cmb_data *cmb_data;
870
Sebastian Ott616503d2015-09-07 19:52:06 +0200871 spin_lock(&cmb_area.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200873 cmb_data = cdev->private->cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 cdev->private->cmb = NULL;
Sebastian Otta5e9ca52015-09-07 19:52:31 +0200875 if (cmb_data) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200876 kfree(cmb_data->last_block);
Sebastian Ott45bf4b92015-09-07 19:53:01 +0200877 kmem_cache_free(cmbe_cache, cmb_data->hw_block);
Sebastian Otta5e9ca52015-09-07 19:52:31 +0200878 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200879 kfree(cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* deactivate global measurement if this is the last channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 list_del_init(&cdev->private->cmb_list);
883 if (list_empty(&cmb_area.list))
Heiko Carstens7b4ff872016-06-20 14:03:38 +0200884 cmf_activate(NULL, CMF_OFF);
Sebastian Ott616503d2015-09-07 19:52:06 +0200885 spin_unlock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 spin_unlock(&cmb_area.lock);
887}
888
Cornelia Huck94bb0632006-06-29 15:08:41 +0200889static int set_cmbe(struct ccw_device *cdev, u32 mme)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 unsigned long mba;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200892 struct cmb_data *cmb_data;
893 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Cornelia Huck94bb0632006-06-29 15:08:41 +0200895 spin_lock_irqsave(cdev->ccwlock, flags);
896 if (!cdev->private->cmb) {
897 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -EINVAL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200899 }
900 cmb_data = cdev->private->cmb;
Sebastian Ott45bf4b92015-09-07 19:53:01 +0200901 mba = mme ? (unsigned long) cmb_data->hw_block : 0;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200902 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return set_schib_wait(cdev, mme, 1, mba);
905}
906
907
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200908static u64 read_cmbe(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200910 struct cmbe *cmb;
911 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 u32 val;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200913 int ret;
914 unsigned long flags;
915
916 ret = cmf_cmb_copy_wait(cdev);
917 if (ret < 0)
918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200921 cmb_data = cdev->private->cmb;
922 if (!cmb_data) {
923 ret = 0;
924 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200926 cmb = cmb_data->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 switch (index) {
929 case cmb_ssch_rsch_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200930 ret = cmb->ssch_rsch_count;
931 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 case cmb_sample_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200933 ret = cmb->sample_count;
934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 case cmb_device_connect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200936 val = cmb->device_connect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 break;
938 case cmb_function_pending_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200939 val = cmb->function_pending_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
941 case cmb_device_disconnect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200942 val = cmb->device_disconnect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944 case cmb_control_unit_queuing_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200945 val = cmb->control_unit_queuing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
947 case cmb_device_active_only_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200948 val = cmb->device_active_only_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950 case cmb_device_busy_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200951 val = cmb->device_busy_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
953 case cmb_initial_command_response_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200954 val = cmb->initial_command_response_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 default:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200957 ret = 0;
958 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200960 ret = time_to_avg_nsec(val, cmb->sample_count);
961out:
962 spin_unlock_irqrestore(cdev->ccwlock, flags);
963 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200966static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200968 struct cmbe *cmb;
969 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 u64 time;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200971 unsigned long flags;
972 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Cornelia Huck94bb0632006-06-29 15:08:41 +0200974 ret = cmf_cmb_copy_wait(cdev);
975 if (ret < 0)
976 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200978 cmb_data = cdev->private->cmb;
979 if (!cmb_data) {
980 ret = -ENODEV;
981 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200983 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 Torvalds1da177e2005-04-16 15:20:36 -0700988
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 Huck94bb0632006-06-29 15:08:41 +0200997 cmb = cmb_data->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* copy data to new structure */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200999 data->ssch_rsch_count = cmb->ssch_rsch_count;
1000 data->sample_count = cmb->sample_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* time fields are converted to nanoseconds while copying */
Cornelia Huck94bb0632006-06-29 15:08:41 +02001003 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 Torvalds1da177e2005-04-16 15:20:36 -07001007 data->control_unit_queuing_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001008 = time_to_nsec(cmb->control_unit_queuing_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 data->device_active_only_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001010 = time_to_nsec(cmb->device_active_only_time);
1011 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 data->initial_command_response_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001013 = time_to_nsec(cmb->initial_command_response_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Cornelia Huck94bb0632006-06-29 15:08:41 +02001015 ret = 0;
1016out:
1017 spin_unlock_irqrestore(cdev->ccwlock, flags);
1018 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Cornelia Huck94bb0632006-06-29 15:08:41 +02001021static void reset_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Cornelia Huck94bb0632006-06-29 15:08:41 +02001023 cmf_generic_reset(cdev);
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026static struct attribute_group cmf_attr_group_ext;
1027
1028static 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 Torvalds1da177e2005-04-16 15:20:36 -07001037
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001038static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 return sprintf(buf, "%lld\n",
1041 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1042}
1043
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001044static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1045 struct device_attribute *attr,
1046 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct ccw_device *cdev;
1049 long interval;
1050 unsigned long count;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001051 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 cdev = to_ccwdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 count = cmf_read(cdev, cmb_sample_count);
Cornelia Huck94bb0632006-06-29 15:08:41 +02001055 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 Huck13ffa922006-07-17 16:09:28 +02001060 interval = (interval * 1000) >> 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 interval /= count;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001062 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 interval = -1;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001064 spin_unlock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return sprintf(buf, "%ld\n", interval);
1066}
1067
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001068static ssize_t cmb_show_avg_utilization(struct device *dev,
1069 struct device_attribute *attr,
1070 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
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 Huck94bb0632006-06-29 15:08:41 +02001078 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 Torvalds1da177e2005-04-16 15:20:36 -07001082 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 Huckfc5019c2007-10-12 16:11:15 +02001102static ssize_t show_##name(struct device *dev, \
1103 struct device_attribute *attr, char *buf) \
1104{ return cmb_show_attr((dev), buf, cmb_##name); } \
1105static DEVICE_ATTR(name, 0444, show_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107#define cmf_attr_avg(name) \
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001108static ssize_t show_avg_##name(struct device *dev, \
1109 struct device_attribute *attr, char *buf) \
1110{ return cmb_show_attr((dev), buf, cmb_##name); } \
1111static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113cmf_attr(ssch_rsch_count);
1114cmf_attr(sample_count);
1115cmf_attr_avg(device_connect_time);
1116cmf_attr_avg(function_pending_time);
1117cmf_attr_avg(device_disconnect_time);
1118cmf_attr_avg(control_unit_queuing_time);
1119cmf_attr_avg(device_active_only_time);
1120cmf_attr_avg(device_busy_time);
1121cmf_attr_avg(initial_command_response_time);
1122
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001123static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1124 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1126
1127static 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 Carstensd2c993d2006-07-12 16:41:55 +02001137 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138};
1139
1140static struct attribute_group cmf_attr_group = {
1141 .name = "cmf",
1142 .attrs = cmf_attributes,
1143};
1144
1145static 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 Carstensd2c993d2006-07-12 16:41:55 +02001157 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158};
1159
1160static struct attribute_group cmf_attr_group_ext = {
1161 .name = "cmf",
1162 .attrs = cmf_attributes_ext,
1163};
1164
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001165static ssize_t cmb_enable_show(struct device *dev,
1166 struct device_attribute *attr,
1167 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Sebastian Ott279b8f92015-09-07 19:50:25 +02001169 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott279b8f92015-09-07 19:50:25 +02001170
Sebastian Ott0f5d0502016-07-12 19:57:57 +02001171 return sprintf(buf, "%d\n", cmf_enabled(cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001174static ssize_t cmb_enable_store(struct device *dev,
1175 struct device_attribute *attr, const char *buf,
1176 size_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Sebastian Ott279b8f92015-09-07 19:50:25 +02001178 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +02001179 unsigned long val;
Sebastian Ott279b8f92015-09-07 19:50:25 +02001180 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +02001181
Jingoo Han01787222013-07-22 10:18:15 +09001182 ret = kstrtoul(buf, 16, &val);
Cornelia Huck2f972202008-04-30 13:38:33 +02001183 if (ret)
1184 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Cornelia Huck2f972202008-04-30 13:38:33 +02001186 switch (val) {
1187 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 ret = disable_cmf(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 break;
Cornelia Huck2f972202008-04-30 13:38:33 +02001190 case 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 ret = enable_cmf(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 break;
Sebastian Ott279b8f92015-09-07 19:50:25 +02001193 default:
1194 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
Sebastian Ott279b8f92015-09-07 19:50:25 +02001197 return ret ? ret : c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
Sebastian Ott279b8f92015-09-07 19:50:25 +02001199DEVICE_ATTR_RW(cmb_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Sebastian Ott823d4942009-06-16 10:30:20 +02001201int ccw_set_cmf(struct ccw_device *cdev, int enable)
1202{
1203 return cmbops->set(cdev, enable ? 2 : 0);
1204}
1205
Cornelia Huckc0208712007-10-12 16:11:16 +02001206/**
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 Ott0f5d0502016-07-12 19:57:57 +02001211 * 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 Huckc0208712007-10-12 16:11:16 +02001213 * Context:
1214 * non-atomic
1215 */
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001216int enable_cmf(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Sebastian Ott0f5d0502016-07-12 19:57:57 +02001218 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Sebastian Ott1bc66642015-09-15 13:11:42 +02001220 device_lock(&cdev->dev);
Sebastian Ott0f5d0502016-07-12 19:57:57 +02001221 if (cmf_enabled(cdev)) {
1222 cmbops->reset(cdev);
1223 goto out_unlock;
1224 }
Sebastian Otta6ef1562015-09-07 19:51:39 +02001225 get_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 ret = cmbops->alloc(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (ret)
Sebastian Ott1bc66642015-09-15 13:11:42 +02001228 goto out;
1229 cmbops->reset(cdev);
1230 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (ret) {
1232 cmbops->free(cdev);
Sebastian Ott1bc66642015-09-15 13:11:42 +02001233 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Sebastian Ott1bc66642015-09-15 13:11:42 +02001235 ret = cmbops->set(cdev, 2);
1236 if (ret) {
1237 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1238 cmbops->free(cdev);
1239 }
1240out:
Sebastian Otta6ef1562015-09-07 19:51:39 +02001241 if (ret)
1242 put_device(&cdev->dev);
Sebastian Ott0f5d0502016-07-12 19:57:57 +02001243out_unlock:
Sebastian Ott1bc66642015-09-15 13:11:42 +02001244 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 */
1257int __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 Torvalds1da177e2005-04-16 15:20:36 -07001266 cmbops->free(cdev);
Sebastian Otta6ef1562015-09-07 19:51:39 +02001267 put_device(&cdev->dev);
Sebastian Ott1bc66642015-09-15 13:11:42 +02001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return ret;
1270}
1271
Cornelia Huckc0208712007-10-12 16:11:16 +02001272/**
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 Huckfc5019c2007-10-12 16:11:15 +02001281int disable_cmf(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 int ret;
1284
Sebastian Ott1bc66642015-09-15 13:11:42 +02001285 device_lock(&cdev->dev);
1286 ret = __disable_cmf(cdev);
1287 device_unlock(&cdev->dev);
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return ret;
1290}
1291
Cornelia Huckc0208712007-10-12 16:11:16 +02001292/**
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 Huckfc5019c2007-10-12 16:11:15 +02001302u64 cmf_read(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 return cmbops->read(cdev, index);
1305}
1306
Cornelia Huckc0208712007-10-12 16:11:16 +02001307/**
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 Huckfc5019c2007-10-12 16:11:15 +02001317int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 return cmbops->readall(cdev, data);
1320}
1321
Cornelia Huck94bb0632006-06-29 15:08:41 +02001322/* Reenable cmf when a disconnected device becomes available again. */
1323int cmf_reenable(struct ccw_device *cdev)
1324{
1325 cmbops->reset(cdev);
1326 return cmbops->set(cdev, 2);
1327}
1328
Sebastian Ottab97d212015-09-09 10:29:59 +02001329/**
1330 * cmf_reactivate() - reactivate measurement block updates
1331 *
1332 * Use this during resume from hibernate.
1333 */
1334void cmf_reactivate(void)
1335{
1336 spin_lock(&cmb_area.lock);
1337 if (!list_empty(&cmb_area.list))
Heiko Carstens7b4ff872016-06-20 14:03:38 +02001338 cmf_activate(cmb_area.mem, CMF_ON);
Sebastian Ottab97d212015-09-09 10:29:59 +02001339 spin_unlock(&cmb_area.lock);
1340}
1341
Sebastian Ott45bf4b92015-09-07 19:53:01 +02001342static 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 Huckfc5019c2007-10-12 16:11:15 +02001350static int __init init_cmf(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 char *format_string;
Sebastian Ott45bf4b92015-09-07 19:53:01 +02001353 char *detect_string;
1354 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001356 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07001361 if (format == CMF_AUTODETECT) {
Cornelia Huck75784c02008-07-14 09:58:57 +02001362 if (!css_general_characteristics.ext_mb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 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 Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
1377 case CMF_EXTENDED:
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001378 format_string = "extended";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 cmbops = &cmbops_extended;
Sebastian Ott45bf4b92015-09-07 19:53:01 +02001380
1381 ret = init_cmbe();
1382 if (ret)
1383 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385 default:
Sebastian Ott45bf4b92015-09-07 19:53:01 +02001386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
Michael Ernste6d5a422008-12-25 13:39:36 +01001388 pr_info("Channel measurement facility initialized using format "
1389 "%s (mode %s)\n", format_string, detect_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return 0;
1391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392module_init(init_cmf);
1393
1394
1395MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1396MODULE_LICENSE("GPL");
1397MODULE_DESCRIPTION("channel measurement facility base driver\n"
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02001398 "Copyright IBM Corp. 2003\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400EXPORT_SYMBOL_GPL(enable_cmf);
1401EXPORT_SYMBOL_GPL(disable_cmf);
1402EXPORT_SYMBOL_GPL(cmf_read);
1403EXPORT_SYMBOL_GPL(cmf_readall);