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