1
2
3
4
5
6
7
8
9
10
11
12
13#define KMSG_COMPONENT "cio"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16#include <linux/memblock.h>
17#include <linux/device.h>
18#include <linux/init.h>
19#include <linux/list.h>
20#include <linux/export.h>
21#include <linux/moduleparam.h>
22#include <linux/slab.h>
23#include <linux/timex.h>
24
25#include <asm/ccwdev.h>
26#include <asm/cio.h>
27#include <asm/cmb.h>
28#include <asm/div64.h>
29
30#include "cio.h"
31#include "css.h"
32#include "device.h"
33#include "ioasm.h"
34#include "chsc.h"
35
36
37
38
39
40
41
42
43
44#define ARGSTRING "s390cmf"
45
46
47enum cmb_index {
48 avg_utilization = -1,
49
50 cmb_ssch_rsch_count = 0,
51 cmb_sample_count,
52 cmb_device_connect_time,
53 cmb_function_pending_time,
54 cmb_device_disconnect_time,
55 cmb_control_unit_queuing_time,
56 cmb_device_active_only_time,
57
58 cmb_device_busy_time,
59 cmb_initial_command_response_time,
60};
61
62
63
64
65
66
67
68
69
70
71
72
73enum cmb_format {
74 CMF_BASIC,
75 CMF_EXTENDED,
76 CMF_AUTODETECT = -1,
77};
78
79
80
81
82
83
84
85
86static int format = CMF_AUTODETECT;
87module_param(format, bint, 0444);
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105struct cmb_operations {
106 int (*alloc) (struct ccw_device *);
107 void (*free) (struct ccw_device *);
108 int (*set) (struct ccw_device *, u32);
109 u64 (*read) (struct ccw_device *, int);
110 int (*readall)(struct ccw_device *, struct cmbdata *);
111 void (*reset) (struct ccw_device *);
112
113 struct attribute_group *attr_group;
114};
115static struct cmb_operations *cmbops;
116
117struct cmb_data {
118 void *hw_block;
119 void *last_block;
120 int size;
121 unsigned long long last_update;
122};
123
124
125
126
127
128
129static inline u64 time_to_nsec(u32 value)
130{
131 return ((u64)value) * 128000ull;
132}
133
134
135
136
137
138
139
140static inline u64 time_to_avg_nsec(u32 value, u32 count)
141{
142 u64 ret;
143
144
145 if (count == 0)
146 return 0;
147
148
149 ret = time_to_nsec(value);
150 do_div(ret, count);
151
152 return ret;
153}
154
155#define CMF_OFF 0
156#define CMF_ON 2
157
158
159
160
161
162
163
164static inline void cmf_activate(void *area, unsigned int onoff)
165{
166 register void * __gpr2 asm("2");
167 register long __gpr1 asm("1");
168
169 __gpr2 = area;
170 __gpr1 = onoff;
171
172 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
173}
174
175static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
176 unsigned long address)
177{
178 struct subchannel *sch = to_subchannel(cdev->dev.parent);
179 int ret;
180
181 sch->config.mme = mme;
182 sch->config.mbfc = mbfc;
183
184 if (mbfc)
185 sch->config.mba = address;
186 else
187 sch->config.mbi = address;
188
189 ret = cio_commit_config(sch);
190 if (!mme && ret == -ENODEV) {
191
192
193
194
195 ret = 0;
196 }
197 return ret;
198}
199
200struct set_schib_struct {
201 u32 mme;
202 int mbfc;
203 unsigned long address;
204 wait_queue_head_t wait;
205 int ret;
206};
207
208#define CMF_PENDING 1
209#define SET_SCHIB_TIMEOUT (10 * HZ)
210
211static int set_schib_wait(struct ccw_device *cdev, u32 mme,
212 int mbfc, unsigned long address)
213{
214 struct set_schib_struct set_data;
215 int ret = -ENODEV;
216
217 spin_lock_irq(cdev->ccwlock);
218 if (!cdev->private->cmb)
219 goto out;
220
221 ret = set_schib(cdev, mme, mbfc, address);
222 if (ret != -EBUSY)
223 goto out;
224
225
226 if (cdev->private->state != DEV_STATE_ONLINE)
227 goto out;
228
229 init_waitqueue_head(&set_data.wait);
230 set_data.mme = mme;
231 set_data.mbfc = mbfc;
232 set_data.address = address;
233 set_data.ret = CMF_PENDING;
234
235 cdev->private->state = DEV_STATE_CMFCHANGE;
236 cdev->private->cmb_wait = &set_data;
237 spin_unlock_irq(cdev->ccwlock);
238
239 ret = wait_event_interruptible_timeout(set_data.wait,
240 set_data.ret != CMF_PENDING,
241 SET_SCHIB_TIMEOUT);
242 spin_lock_irq(cdev->ccwlock);
243 if (ret <= 0) {
244 if (set_data.ret == CMF_PENDING) {
245 set_data.ret = (ret == 0) ? -ETIME : ret;
246 if (cdev->private->state == DEV_STATE_CMFCHANGE)
247 cdev->private->state = DEV_STATE_ONLINE;
248 }
249 }
250 cdev->private->cmb_wait = NULL;
251 ret = set_data.ret;
252out:
253 spin_unlock_irq(cdev->ccwlock);
254 return ret;
255}
256
257void retry_set_schib(struct ccw_device *cdev)
258{
259 struct set_schib_struct *set_data = cdev->private->cmb_wait;
260
261 if (!set_data)
262 return;
263
264 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
265 set_data->address);
266 wake_up(&set_data->wait);
267}
268
269static int cmf_copy_block(struct ccw_device *cdev)
270{
271 struct subchannel *sch = to_subchannel(cdev->dev.parent);
272 struct cmb_data *cmb_data;
273 void *hw_block;
274
275 if (cio_update_schib(sch))
276 return -ENODEV;
277
278 if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
279
280 if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
281 (scsw_actl(&sch->schib.scsw) &
282 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
283 (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
284 return -EBUSY;
285 }
286 cmb_data = cdev->private->cmb;
287 hw_block = cmb_data->hw_block;
288 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
289 cmb_data->last_update = get_tod_clock();
290 return 0;
291}
292
293struct copy_block_struct {
294 wait_queue_head_t wait;
295 int ret;
296};
297
298static int cmf_cmb_copy_wait(struct ccw_device *cdev)
299{
300 struct copy_block_struct copy_block;
301 int ret = -ENODEV;
302
303 spin_lock_irq(cdev->ccwlock);
304 if (!cdev->private->cmb)
305 goto out;
306
307 ret = cmf_copy_block(cdev);
308 if (ret != -EBUSY)
309 goto out;
310
311 if (cdev->private->state != DEV_STATE_ONLINE)
312 goto out;
313
314 init_waitqueue_head(©_block.wait);
315 copy_block.ret = CMF_PENDING;
316
317 cdev->private->state = DEV_STATE_CMFUPDATE;
318 cdev->private->cmb_wait = ©_block;
319 spin_unlock_irq(cdev->ccwlock);
320
321 ret = wait_event_interruptible(copy_block.wait,
322 copy_block.ret != CMF_PENDING);
323 spin_lock_irq(cdev->ccwlock);
324 if (ret) {
325 if (copy_block.ret == CMF_PENDING) {
326 copy_block.ret = -ERESTARTSYS;
327 if (cdev->private->state == DEV_STATE_CMFUPDATE)
328 cdev->private->state = DEV_STATE_ONLINE;
329 }
330 }
331 cdev->private->cmb_wait = NULL;
332 ret = copy_block.ret;
333out:
334 spin_unlock_irq(cdev->ccwlock);
335 return ret;
336}
337
338void cmf_retry_copy_block(struct ccw_device *cdev)
339{
340 struct copy_block_struct *copy_block = cdev->private->cmb_wait;
341
342 if (!copy_block)
343 return;
344
345 copy_block->ret = cmf_copy_block(cdev);
346 wake_up(©_block->wait);
347}
348
349static void cmf_generic_reset(struct ccw_device *cdev)
350{
351 struct cmb_data *cmb_data;
352
353 spin_lock_irq(cdev->ccwlock);
354 cmb_data = cdev->private->cmb;
355 if (cmb_data) {
356 memset(cmb_data->last_block, 0, cmb_data->size);
357
358
359
360
361 memset(cmb_data->hw_block, 0, cmb_data->size);
362 cmb_data->last_update = 0;
363 }
364 cdev->private->cmb_start_time = get_tod_clock();
365 spin_unlock_irq(cdev->ccwlock);
366}
367
368
369
370
371
372
373
374
375
376struct cmb_area {
377 struct cmb *mem;
378 struct list_head list;
379 int num_channels;
380 spinlock_t lock;
381};
382
383static struct cmb_area cmb_area = {
384 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
385 .list = LIST_HEAD_INIT(cmb_area.list),
386 .num_channels = 1024,
387};
388
389
390
391
392
393
394
395
396
397
398
399
400
401module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421struct cmb {
422 u16 ssch_rsch_count;
423 u16 sample_count;
424 u32 device_connect_time;
425 u32 function_pending_time;
426 u32 device_disconnect_time;
427 u32 control_unit_queuing_time;
428 u32 device_active_only_time;
429 u32 reserved[2];
430};
431
432
433
434
435
436static int alloc_cmb_single(struct ccw_device *cdev,
437 struct cmb_data *cmb_data)
438{
439 struct cmb *cmb;
440 struct ccw_device_private *node;
441 int ret;
442
443 spin_lock_irq(cdev->ccwlock);
444 if (!list_empty(&cdev->private->cmb_list)) {
445 ret = -EBUSY;
446 goto out;
447 }
448
449
450
451
452
453
454 cmb = cmb_area.mem;
455 list_for_each_entry(node, &cmb_area.list, cmb_list) {
456 struct cmb_data *data;
457 data = node->cmb;
458 if ((struct cmb*)data->hw_block > cmb)
459 break;
460 cmb++;
461 }
462 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
463 ret = -ENOMEM;
464 goto out;
465 }
466
467
468 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
469 cmb_data->hw_block = cmb;
470 cdev->private->cmb = cmb_data;
471 ret = 0;
472out:
473 spin_unlock_irq(cdev->ccwlock);
474 return ret;
475}
476
477static int alloc_cmb(struct ccw_device *cdev)
478{
479 int ret;
480 struct cmb *mem;
481 ssize_t size;
482 struct cmb_data *cmb_data;
483
484
485 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
486 if (!cmb_data)
487 return -ENOMEM;
488
489 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
490 if (!cmb_data->last_block) {
491 kfree(cmb_data);
492 return -ENOMEM;
493 }
494 cmb_data->size = sizeof(struct cmb);
495 spin_lock(&cmb_area.lock);
496
497 if (!cmb_area.mem) {
498
499 size = sizeof(struct cmb) * cmb_area.num_channels;
500 WARN_ON(!list_empty(&cmb_area.list));
501
502 spin_unlock(&cmb_area.lock);
503 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
504 get_order(size));
505 spin_lock(&cmb_area.lock);
506
507 if (cmb_area.mem) {
508
509 free_pages((unsigned long)mem, get_order(size));
510 } else if (!mem) {
511
512 ret = -ENOMEM;
513 goto out;
514 } else {
515
516 memset(mem, 0, size);
517 cmb_area.mem = mem;
518 cmf_activate(cmb_area.mem, CMF_ON);
519 }
520 }
521
522
523 ret = alloc_cmb_single(cdev, cmb_data);
524out:
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
533static 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
560static 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
579static 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
595static 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);
641out:
642 spin_unlock_irqrestore(cdev->ccwlock, flags);
643 return ret;
644}
645
646static 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
673 data->size = offsetof(struct cmbdata, device_busy_time);
674
675 data->elapsed_time = tod_to_ns(time);
676
677
678 data->ssch_rsch_count = cmb->ssch_rsch_count;
679 data->sample_count = cmb->sample_count;
680
681
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;
691out:
692 spin_unlock_irqrestore(cdev->ccwlock, flags);
693 return ret;
694}
695
696static void reset_cmb(struct ccw_device *cdev)
697{
698 cmf_generic_reset(cdev);
699}
700
701static 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
712static struct attribute_group cmf_attr_group;
713
714static 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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744struct 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
757static struct kmem_cache *cmbe_cache;
758
759static 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(sizeof(*cmb_data), GFP_KERNEL);
770 if (!cmb_data)
771 goto out_free;
772
773 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
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
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
796out_unlock:
797 spin_unlock_irq(cdev->ccwlock);
798 spin_unlock(&cmb_area.lock);
799 ret = -EBUSY;
800out_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
809static 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
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
831static 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
849static 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);
901out:
902 spin_unlock_irqrestore(cdev->ccwlock, flags);
903 return ret;
904}
905
906static 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
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
938 data->ssch_rsch_count = cmb->ssch_rsch_count;
939 data->sample_count = cmb->sample_count;
940
941
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;
955out:
956 spin_unlock_irqrestore(cdev->ccwlock, flags);
957 return ret;
958}
959
960static void reset_cmbe(struct ccw_device *cdev)
961{
962 cmf_generic_reset(cdev);
963}
964
965static struct attribute_group cmf_attr_group_ext;
966
967static 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
977static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
978{
979 return sprintf(buf, "%lld\n",
980 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
981}
982
983static ssize_t cmb_show_avg_sample_interval(struct device *dev,
984 struct device_attribute *attr,
985 char *buf)
986{
987 struct ccw_device *cdev = to_ccwdev(dev);
988 unsigned long count;
989 long interval;
990
991 count = cmf_read(cdev, cmb_sample_count);
992 spin_lock_irq(cdev->ccwlock);
993 if (count) {
994 interval = get_tod_clock() - cdev->private->cmb_start_time;
995 interval = tod_to_ns(interval);
996 interval /= count;
997 } else
998 interval = -1;
999 spin_unlock_irq(cdev->ccwlock);
1000 return sprintf(buf, "%ld\n", interval);
1001}
1002
1003static ssize_t cmb_show_avg_utilization(struct device *dev,
1004 struct device_attribute *attr,
1005 char *buf)
1006{
1007 unsigned long u = cmf_read(to_ccwdev(dev), avg_utilization);
1008
1009 return sprintf(buf, "%02lu.%01lu%%\n", u / 10, u % 10);
1010}
1011
1012#define cmf_attr(name) \
1013static ssize_t show_##name(struct device *dev, \
1014 struct device_attribute *attr, char *buf) \
1015{ return cmb_show_attr((dev), buf, cmb_##name); } \
1016static DEVICE_ATTR(name, 0444, show_##name, NULL);
1017
1018#define cmf_attr_avg(name) \
1019static ssize_t show_avg_##name(struct device *dev, \
1020 struct device_attribute *attr, char *buf) \
1021{ return cmb_show_attr((dev), buf, cmb_##name); } \
1022static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1023
1024cmf_attr(ssch_rsch_count);
1025cmf_attr(sample_count);
1026cmf_attr_avg(device_connect_time);
1027cmf_attr_avg(function_pending_time);
1028cmf_attr_avg(device_disconnect_time);
1029cmf_attr_avg(control_unit_queuing_time);
1030cmf_attr_avg(device_active_only_time);
1031cmf_attr_avg(device_busy_time);
1032cmf_attr_avg(initial_command_response_time);
1033
1034static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1035 NULL);
1036static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1037
1038static struct attribute *cmf_attributes[] = {
1039 &dev_attr_avg_sample_interval.attr,
1040 &dev_attr_avg_utilization.attr,
1041 &dev_attr_ssch_rsch_count.attr,
1042 &dev_attr_sample_count.attr,
1043 &dev_attr_avg_device_connect_time.attr,
1044 &dev_attr_avg_function_pending_time.attr,
1045 &dev_attr_avg_device_disconnect_time.attr,
1046 &dev_attr_avg_control_unit_queuing_time.attr,
1047 &dev_attr_avg_device_active_only_time.attr,
1048 NULL,
1049};
1050
1051static struct attribute_group cmf_attr_group = {
1052 .name = "cmf",
1053 .attrs = cmf_attributes,
1054};
1055
1056static struct attribute *cmf_attributes_ext[] = {
1057 &dev_attr_avg_sample_interval.attr,
1058 &dev_attr_avg_utilization.attr,
1059 &dev_attr_ssch_rsch_count.attr,
1060 &dev_attr_sample_count.attr,
1061 &dev_attr_avg_device_connect_time.attr,
1062 &dev_attr_avg_function_pending_time.attr,
1063 &dev_attr_avg_device_disconnect_time.attr,
1064 &dev_attr_avg_control_unit_queuing_time.attr,
1065 &dev_attr_avg_device_active_only_time.attr,
1066 &dev_attr_avg_device_busy_time.attr,
1067 &dev_attr_avg_initial_command_response_time.attr,
1068 NULL,
1069};
1070
1071static struct attribute_group cmf_attr_group_ext = {
1072 .name = "cmf",
1073 .attrs = cmf_attributes_ext,
1074};
1075
1076static ssize_t cmb_enable_show(struct device *dev,
1077 struct device_attribute *attr,
1078 char *buf)
1079{
1080 struct ccw_device *cdev = to_ccwdev(dev);
1081
1082 return sprintf(buf, "%d\n", cmf_enabled(cdev));
1083}
1084
1085static ssize_t cmb_enable_store(struct device *dev,
1086 struct device_attribute *attr, const char *buf,
1087 size_t c)
1088{
1089 struct ccw_device *cdev = to_ccwdev(dev);
1090 unsigned long val;
1091 int ret;
1092
1093 ret = kstrtoul(buf, 16, &val);
1094 if (ret)
1095 return ret;
1096
1097 switch (val) {
1098 case 0:
1099 ret = disable_cmf(cdev);
1100 break;
1101 case 1:
1102 ret = enable_cmf(cdev);
1103 break;
1104 default:
1105 ret = -EINVAL;
1106 }
1107
1108 return ret ? ret : c;
1109}
1110DEVICE_ATTR_RW(cmb_enable);
1111
1112int ccw_set_cmf(struct ccw_device *cdev, int enable)
1113{
1114 return cmbops->set(cdev, enable ? 2 : 0);
1115}
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128int enable_cmf(struct ccw_device *cdev)
1129{
1130 int ret = 0;
1131
1132 device_lock(&cdev->dev);
1133 if (cmf_enabled(cdev)) {
1134 cmbops->reset(cdev);
1135 goto out_unlock;
1136 }
1137 get_device(&cdev->dev);
1138 ret = cmbops->alloc(cdev);
1139 if (ret)
1140 goto out;
1141 cmbops->reset(cdev);
1142 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1143 if (ret) {
1144 cmbops->free(cdev);
1145 goto out;
1146 }
1147 ret = cmbops->set(cdev, 2);
1148 if (ret) {
1149 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1150 cmbops->free(cdev);
1151 }
1152out:
1153 if (ret)
1154 put_device(&cdev->dev);
1155out_unlock:
1156 device_unlock(&cdev->dev);
1157 return ret;
1158}
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169int __disable_cmf(struct ccw_device *cdev)
1170{
1171 int ret;
1172
1173 ret = cmbops->set(cdev, 0);
1174 if (ret)
1175 return ret;
1176
1177 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1178 cmbops->free(cdev);
1179 put_device(&cdev->dev);
1180
1181 return ret;
1182}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193int disable_cmf(struct ccw_device *cdev)
1194{
1195 int ret;
1196
1197 device_lock(&cdev->dev);
1198 ret = __disable_cmf(cdev);
1199 device_unlock(&cdev->dev);
1200
1201 return ret;
1202}
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214u64 cmf_read(struct ccw_device *cdev, int index)
1215{
1216 return cmbops->read(cdev, index);
1217}
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1230{
1231 return cmbops->readall(cdev, data);
1232}
1233
1234
1235int cmf_reenable(struct ccw_device *cdev)
1236{
1237 cmbops->reset(cdev);
1238 return cmbops->set(cdev, 2);
1239}
1240
1241
1242
1243
1244
1245
1246void cmf_reactivate(void)
1247{
1248 spin_lock(&cmb_area.lock);
1249 if (!list_empty(&cmb_area.list))
1250 cmf_activate(cmb_area.mem, CMF_ON);
1251 spin_unlock(&cmb_area.lock);
1252}
1253
1254static int __init init_cmbe(void)
1255{
1256 cmbe_cache = kmem_cache_create("cmbe_cache", sizeof(struct cmbe),
1257 __alignof__(struct cmbe), 0, NULL);
1258
1259 return cmbe_cache ? 0 : -ENOMEM;
1260}
1261
1262static int __init init_cmf(void)
1263{
1264 char *format_string;
1265 char *detect_string;
1266 int ret;
1267
1268
1269
1270
1271
1272
1273 if (format == CMF_AUTODETECT) {
1274 if (!css_general_characteristics.ext_mb) {
1275 format = CMF_BASIC;
1276 } else {
1277 format = CMF_EXTENDED;
1278 }
1279 detect_string = "autodetected";
1280 } else {
1281 detect_string = "parameter";
1282 }
1283
1284 switch (format) {
1285 case CMF_BASIC:
1286 format_string = "basic";
1287 cmbops = &cmbops_basic;
1288 break;
1289 case CMF_EXTENDED:
1290 format_string = "extended";
1291 cmbops = &cmbops_extended;
1292
1293 ret = init_cmbe();
1294 if (ret)
1295 return ret;
1296 break;
1297 default:
1298 return -EINVAL;
1299 }
1300 pr_info("Channel measurement facility initialized using format "
1301 "%s (mode %s)\n", format_string, detect_string);
1302 return 0;
1303}
1304device_initcall(init_cmf);
1305
1306EXPORT_SYMBOL_GPL(enable_cmf);
1307EXPORT_SYMBOL_GPL(disable_cmf);
1308EXPORT_SYMBOL_GPL(cmf_read);
1309EXPORT_SYMBOL_GPL(cmf_readall);
1310