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