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