1 /* 2 * drivers/s390/char/sclp_cmd.c 3 * 4 * Copyright IBM Corp. 2007 5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>, 6 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com> 7 */ 8 9 #define KMSG_COMPONENT "sclp_cmd" 10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 11 12 #include <linux/completion.h> 13 #include <linux/init.h> 14 #include <linux/errno.h> 15 #include <linux/slab.h> 16 #include <linux/string.h> 17 #include <linux/mm.h> 18 #include <linux/mmzone.h> 19 #include <linux/memory.h> 20 #include <asm/chpid.h> 21 #include <asm/sclp.h> 22 23 #include "sclp.h" 24 25 #define SCLP_CMDW_READ_SCP_INFO 0x00020001 26 #define SCLP_CMDW_READ_SCP_INFO_FORCED 0x00120001 27 28 struct read_info_sccb { 29 struct sccb_header header; /* 0-7 */ 30 u16 rnmax; /* 8-9 */ 31 u8 rnsize; /* 10 */ 32 u8 _reserved0[24 - 11]; /* 11-15 */ 33 u8 loadparm[8]; /* 24-31 */ 34 u8 _reserved1[48 - 32]; /* 32-47 */ 35 u64 facilities; /* 48-55 */ 36 u8 _reserved2[84 - 56]; /* 56-83 */ 37 u8 fac84; /* 84 */ 38 u8 _reserved3[91 - 85]; /* 85-90 */ 39 u8 flags; /* 91 */ 40 u8 _reserved4[100 - 92]; /* 92-99 */ 41 u32 rnsize2; /* 100-103 */ 42 u64 rnmax2; /* 104-111 */ 43 u8 _reserved5[4096 - 112]; /* 112-4095 */ 44 } __attribute__((packed, aligned(PAGE_SIZE))); 45 46 static struct read_info_sccb __initdata early_read_info_sccb; 47 static int __initdata early_read_info_sccb_valid; 48 49 u64 sclp_facilities; 50 static u8 sclp_fac84; 51 static unsigned long long rzm; 52 static unsigned long long rnmax; 53 54 static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb) 55 { 56 int rc; 57 58 __ctl_set_bit(0, 9); 59 rc = sclp_service_call(cmd, sccb); 60 if (rc) 61 goto out; 62 __load_psw_mask(PSW_BASE_BITS | PSW_MASK_EXT | 63 PSW_MASK_WAIT | PSW_DEFAULT_KEY); 64 local_irq_disable(); 65 out: 66 /* Contents of the sccb might have changed. */ 67 barrier(); 68 __ctl_clear_bit(0, 9); 69 return rc; 70 } 71 72 static void __init sclp_read_info_early(void) 73 { 74 int rc; 75 int i; 76 struct read_info_sccb *sccb; 77 sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED, 78 SCLP_CMDW_READ_SCP_INFO}; 79 80 sccb = &early_read_info_sccb; 81 for (i = 0; i < ARRAY_SIZE(commands); i++) { 82 do { 83 memset(sccb, 0, sizeof(*sccb)); 84 sccb->header.length = sizeof(*sccb); 85 sccb->header.control_mask[2] = 0x80; 86 rc = sclp_cmd_sync_early(commands[i], sccb); 87 } while (rc == -EBUSY); 88 89 if (rc) 90 break; 91 if (sccb->header.response_code == 0x10) { 92 early_read_info_sccb_valid = 1; 93 break; 94 } 95 if (sccb->header.response_code != 0x1f0) 96 break; 97 } 98 } 99 100 void __init sclp_facilities_detect(void) 101 { 102 struct read_info_sccb *sccb; 103 104 sclp_read_info_early(); 105 if (!early_read_info_sccb_valid) 106 return; 107 108 sccb = &early_read_info_sccb; 109 sclp_facilities = sccb->facilities; 110 sclp_fac84 = sccb->fac84; 111 rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2; 112 rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2; 113 rzm <<= 20; 114 } 115 116 unsigned long long sclp_get_rnmax(void) 117 { 118 return rnmax; 119 } 120 121 unsigned long long sclp_get_rzm(void) 122 { 123 return rzm; 124 } 125 126 /* 127 * This function will be called after sclp_facilities_detect(), which gets 128 * called from early.c code. Therefore the sccb should have valid contents. 129 */ 130 void __init sclp_get_ipl_info(struct sclp_ipl_info *info) 131 { 132 struct read_info_sccb *sccb; 133 134 if (!early_read_info_sccb_valid) 135 return; 136 sccb = &early_read_info_sccb; 137 info->is_valid = 1; 138 if (sccb->flags & 0x2) 139 info->has_dump = 1; 140 memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN); 141 } 142 143 static void sclp_sync_callback(struct sclp_req *req, void *data) 144 { 145 struct completion *completion = data; 146 147 complete(completion); 148 } 149 150 static int do_sync_request(sclp_cmdw_t cmd, void *sccb) 151 { 152 struct completion completion; 153 struct sclp_req *request; 154 int rc; 155 156 request = kzalloc(sizeof(*request), GFP_KERNEL); 157 if (!request) 158 return -ENOMEM; 159 request->command = cmd; 160 request->sccb = sccb; 161 request->status = SCLP_REQ_FILLED; 162 request->callback = sclp_sync_callback; 163 request->callback_data = &completion; 164 init_completion(&completion); 165 166 /* Perform sclp request. */ 167 rc = sclp_add_request(request); 168 if (rc) 169 goto out; 170 wait_for_completion(&completion); 171 172 /* Check response. */ 173 if (request->status != SCLP_REQ_DONE) { 174 pr_warning("sync request failed (cmd=0x%08x, " 175 "status=0x%02x)\n", cmd, request->status); 176 rc = -EIO; 177 } 178 out: 179 kfree(request); 180 return rc; 181 } 182 183 /* 184 * CPU configuration related functions. 185 */ 186 187 #define SCLP_CMDW_READ_CPU_INFO 0x00010001 188 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001 189 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001 190 191 struct read_cpu_info_sccb { 192 struct sccb_header header; 193 u16 nr_configured; 194 u16 offset_configured; 195 u16 nr_standby; 196 u16 offset_standby; 197 u8 reserved[4096 - 16]; 198 } __attribute__((packed, aligned(PAGE_SIZE))); 199 200 static void sclp_fill_cpu_info(struct sclp_cpu_info *info, 201 struct read_cpu_info_sccb *sccb) 202 { 203 char *page = (char *) sccb; 204 205 memset(info, 0, sizeof(*info)); 206 info->configured = sccb->nr_configured; 207 info->standby = sccb->nr_standby; 208 info->combined = sccb->nr_configured + sccb->nr_standby; 209 info->has_cpu_type = sclp_fac84 & 0x1; 210 memcpy(&info->cpu, page + sccb->offset_configured, 211 info->combined * sizeof(struct sclp_cpu_entry)); 212 } 213 214 int sclp_get_cpu_info(struct sclp_cpu_info *info) 215 { 216 int rc; 217 struct read_cpu_info_sccb *sccb; 218 219 if (!SCLP_HAS_CPU_INFO) 220 return -EOPNOTSUPP; 221 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 222 if (!sccb) 223 return -ENOMEM; 224 sccb->header.length = sizeof(*sccb); 225 rc = do_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb); 226 if (rc) 227 goto out; 228 if (sccb->header.response_code != 0x0010) { 229 pr_warning("readcpuinfo failed (response=0x%04x)\n", 230 sccb->header.response_code); 231 rc = -EIO; 232 goto out; 233 } 234 sclp_fill_cpu_info(info, sccb); 235 out: 236 free_page((unsigned long) sccb); 237 return rc; 238 } 239 240 struct cpu_configure_sccb { 241 struct sccb_header header; 242 } __attribute__((packed, aligned(8))); 243 244 static int do_cpu_configure(sclp_cmdw_t cmd) 245 { 246 struct cpu_configure_sccb *sccb; 247 int rc; 248 249 if (!SCLP_HAS_CPU_RECONFIG) 250 return -EOPNOTSUPP; 251 /* 252 * This is not going to cross a page boundary since we force 253 * kmalloc to have a minimum alignment of 8 bytes on s390. 254 */ 255 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA); 256 if (!sccb) 257 return -ENOMEM; 258 sccb->header.length = sizeof(*sccb); 259 rc = do_sync_request(cmd, sccb); 260 if (rc) 261 goto out; 262 switch (sccb->header.response_code) { 263 case 0x0020: 264 case 0x0120: 265 break; 266 default: 267 pr_warning("configure cpu failed (cmd=0x%08x, " 268 "response=0x%04x)\n", cmd, 269 sccb->header.response_code); 270 rc = -EIO; 271 break; 272 } 273 out: 274 kfree(sccb); 275 return rc; 276 } 277 278 int sclp_cpu_configure(u8 cpu) 279 { 280 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8); 281 } 282 283 int sclp_cpu_deconfigure(u8 cpu) 284 { 285 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8); 286 } 287 288 #ifdef CONFIG_MEMORY_HOTPLUG 289 290 static DEFINE_MUTEX(sclp_mem_mutex); 291 static LIST_HEAD(sclp_mem_list); 292 static u8 sclp_max_storage_id; 293 static unsigned long sclp_storage_ids[256 / BITS_PER_LONG]; 294 295 struct memory_increment { 296 struct list_head list; 297 u16 rn; 298 int standby; 299 int usecount; 300 }; 301 302 struct assign_storage_sccb { 303 struct sccb_header header; 304 u16 rn; 305 } __packed; 306 307 static unsigned long long rn2addr(u16 rn) 308 { 309 return (unsigned long long) (rn - 1) * rzm; 310 } 311 312 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn) 313 { 314 struct assign_storage_sccb *sccb; 315 int rc; 316 317 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 318 if (!sccb) 319 return -ENOMEM; 320 sccb->header.length = PAGE_SIZE; 321 sccb->rn = rn; 322 rc = do_sync_request(cmd, sccb); 323 if (rc) 324 goto out; 325 switch (sccb->header.response_code) { 326 case 0x0020: 327 case 0x0120: 328 break; 329 default: 330 pr_warning("assign storage failed (cmd=0x%08x, " 331 "response=0x%04x, rn=0x%04x)\n", cmd, 332 sccb->header.response_code, rn); 333 rc = -EIO; 334 break; 335 } 336 out: 337 free_page((unsigned long) sccb); 338 return rc; 339 } 340 341 static int sclp_assign_storage(u16 rn) 342 { 343 return do_assign_storage(0x000d0001, rn); 344 } 345 346 static int sclp_unassign_storage(u16 rn) 347 { 348 return do_assign_storage(0x000c0001, rn); 349 } 350 351 struct attach_storage_sccb { 352 struct sccb_header header; 353 u16 :16; 354 u16 assigned; 355 u32 :32; 356 u32 entries[0]; 357 } __packed; 358 359 static int sclp_attach_storage(u8 id) 360 { 361 struct attach_storage_sccb *sccb; 362 int rc; 363 int i; 364 365 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 366 if (!sccb) 367 return -ENOMEM; 368 sccb->header.length = PAGE_SIZE; 369 rc = do_sync_request(0x00080001 | id << 8, sccb); 370 if (rc) 371 goto out; 372 switch (sccb->header.response_code) { 373 case 0x0020: 374 set_bit(id, sclp_storage_ids); 375 for (i = 0; i < sccb->assigned; i++) 376 sclp_unassign_storage(sccb->entries[i] >> 16); 377 break; 378 default: 379 rc = -EIO; 380 break; 381 } 382 out: 383 free_page((unsigned long) sccb); 384 return rc; 385 } 386 387 static int sclp_mem_change_state(unsigned long start, unsigned long size, 388 int online) 389 { 390 struct memory_increment *incr; 391 unsigned long long istart; 392 int rc = 0; 393 394 list_for_each_entry(incr, &sclp_mem_list, list) { 395 istart = rn2addr(incr->rn); 396 if (start + size - 1 < istart) 397 break; 398 if (start > istart + rzm - 1) 399 continue; 400 if (online) { 401 if (incr->usecount++) 402 continue; 403 /* 404 * Don't break the loop if one assign fails. Loop may 405 * be walked again on CANCEL and we can't save 406 * information if state changed before or not. 407 * So continue and increase usecount for all increments. 408 */ 409 rc |= sclp_assign_storage(incr->rn); 410 } else { 411 if (--incr->usecount) 412 continue; 413 sclp_unassign_storage(incr->rn); 414 } 415 } 416 return rc ? -EIO : 0; 417 } 418 419 static int sclp_mem_notifier(struct notifier_block *nb, 420 unsigned long action, void *data) 421 { 422 unsigned long start, size; 423 struct memory_notify *arg; 424 unsigned char id; 425 int rc = 0; 426 427 arg = data; 428 start = arg->start_pfn << PAGE_SHIFT; 429 size = arg->nr_pages << PAGE_SHIFT; 430 mutex_lock(&sclp_mem_mutex); 431 for (id = 0; id <= sclp_max_storage_id; id++) 432 if (!test_bit(id, sclp_storage_ids)) 433 sclp_attach_storage(id); 434 switch (action) { 435 case MEM_ONLINE: 436 case MEM_GOING_OFFLINE: 437 case MEM_CANCEL_OFFLINE: 438 break; 439 case MEM_GOING_ONLINE: 440 rc = sclp_mem_change_state(start, size, 1); 441 break; 442 case MEM_CANCEL_ONLINE: 443 sclp_mem_change_state(start, size, 0); 444 break; 445 case MEM_OFFLINE: 446 sclp_mem_change_state(start, size, 0); 447 break; 448 default: 449 rc = -EINVAL; 450 break; 451 } 452 mutex_unlock(&sclp_mem_mutex); 453 return rc ? NOTIFY_BAD : NOTIFY_OK; 454 } 455 456 static struct notifier_block sclp_mem_nb = { 457 .notifier_call = sclp_mem_notifier, 458 }; 459 460 static void __init add_memory_merged(u16 rn) 461 { 462 static u16 first_rn, num; 463 unsigned long long start, size; 464 465 if (rn && first_rn && (first_rn + num == rn)) { 466 num++; 467 return; 468 } 469 if (!first_rn) 470 goto skip_add; 471 start = rn2addr(first_rn); 472 size = (unsigned long long ) num * rzm; 473 if (start >= VMEM_MAX_PHYS) 474 goto skip_add; 475 if (start + size > VMEM_MAX_PHYS) 476 size = VMEM_MAX_PHYS - start; 477 add_memory(0, start, size); 478 skip_add: 479 first_rn = rn; 480 num = 1; 481 } 482 483 static void __init sclp_add_standby_memory(void) 484 { 485 struct memory_increment *incr; 486 487 list_for_each_entry(incr, &sclp_mem_list, list) 488 if (incr->standby) 489 add_memory_merged(incr->rn); 490 add_memory_merged(0); 491 } 492 493 static void __init insert_increment(u16 rn, int standby, int assigned) 494 { 495 struct memory_increment *incr, *new_incr; 496 struct list_head *prev; 497 u16 last_rn; 498 499 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL); 500 if (!new_incr) 501 return; 502 new_incr->rn = rn; 503 new_incr->standby = standby; 504 last_rn = 0; 505 prev = &sclp_mem_list; 506 list_for_each_entry(incr, &sclp_mem_list, list) { 507 if (assigned && incr->rn > rn) 508 break; 509 if (!assigned && incr->rn - last_rn > 1) 510 break; 511 last_rn = incr->rn; 512 prev = &incr->list; 513 } 514 if (!assigned) 515 new_incr->rn = last_rn + 1; 516 if (new_incr->rn > rnmax) { 517 kfree(new_incr); 518 return; 519 } 520 list_add(&new_incr->list, prev); 521 } 522 523 struct read_storage_sccb { 524 struct sccb_header header; 525 u16 max_id; 526 u16 assigned; 527 u16 standby; 528 u16 :16; 529 u32 entries[0]; 530 } __packed; 531 532 static int __init sclp_detect_standby_memory(void) 533 { 534 struct read_storage_sccb *sccb; 535 int i, id, assigned, rc; 536 537 if (!early_read_info_sccb_valid) 538 return 0; 539 if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL) 540 return 0; 541 rc = -ENOMEM; 542 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA); 543 if (!sccb) 544 goto out; 545 assigned = 0; 546 for (id = 0; id <= sclp_max_storage_id; id++) { 547 memset(sccb, 0, PAGE_SIZE); 548 sccb->header.length = PAGE_SIZE; 549 rc = do_sync_request(0x00040001 | id << 8, sccb); 550 if (rc) 551 goto out; 552 switch (sccb->header.response_code) { 553 case 0x0010: 554 set_bit(id, sclp_storage_ids); 555 for (i = 0; i < sccb->assigned; i++) { 556 if (!sccb->entries[i]) 557 continue; 558 assigned++; 559 insert_increment(sccb->entries[i] >> 16, 0, 1); 560 } 561 break; 562 case 0x0310: 563 break; 564 case 0x0410: 565 for (i = 0; i < sccb->assigned; i++) { 566 if (!sccb->entries[i]) 567 continue; 568 assigned++; 569 insert_increment(sccb->entries[i] >> 16, 1, 1); 570 } 571 break; 572 default: 573 rc = -EIO; 574 break; 575 } 576 if (!rc) 577 sclp_max_storage_id = sccb->max_id; 578 } 579 if (rc || list_empty(&sclp_mem_list)) 580 goto out; 581 for (i = 1; i <= rnmax - assigned; i++) 582 insert_increment(0, 1, 0); 583 rc = register_memory_notifier(&sclp_mem_nb); 584 if (rc) 585 goto out; 586 sclp_add_standby_memory(); 587 out: 588 free_page((unsigned long) sccb); 589 return rc; 590 } 591 __initcall(sclp_detect_standby_memory); 592 593 #endif /* CONFIG_MEMORY_HOTPLUG */ 594 595 /* 596 * Channel path configuration related functions. 597 */ 598 599 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001 600 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001 601 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001 602 603 struct chp_cfg_sccb { 604 struct sccb_header header; 605 u8 ccm; 606 u8 reserved[6]; 607 u8 cssid; 608 } __attribute__((packed)); 609 610 static int do_chp_configure(sclp_cmdw_t cmd) 611 { 612 struct chp_cfg_sccb *sccb; 613 int rc; 614 615 if (!SCLP_HAS_CHP_RECONFIG) 616 return -EOPNOTSUPP; 617 /* Prepare sccb. */ 618 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 619 if (!sccb) 620 return -ENOMEM; 621 sccb->header.length = sizeof(*sccb); 622 rc = do_sync_request(cmd, sccb); 623 if (rc) 624 goto out; 625 switch (sccb->header.response_code) { 626 case 0x0020: 627 case 0x0120: 628 case 0x0440: 629 case 0x0450: 630 break; 631 default: 632 pr_warning("configure channel-path failed " 633 "(cmd=0x%08x, response=0x%04x)\n", cmd, 634 sccb->header.response_code); 635 rc = -EIO; 636 break; 637 } 638 out: 639 free_page((unsigned long) sccb); 640 return rc; 641 } 642 643 /** 644 * sclp_chp_configure - perform configure channel-path sclp command 645 * @chpid: channel-path ID 646 * 647 * Perform configure channel-path command sclp command for specified chpid. 648 * Return 0 after command successfully finished, non-zero otherwise. 649 */ 650 int sclp_chp_configure(struct chp_id chpid) 651 { 652 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8); 653 } 654 655 /** 656 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command 657 * @chpid: channel-path ID 658 * 659 * Perform deconfigure channel-path command sclp command for specified chpid 660 * and wait for completion. On success return 0. Return non-zero otherwise. 661 */ 662 int sclp_chp_deconfigure(struct chp_id chpid) 663 { 664 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8); 665 } 666 667 struct chp_info_sccb { 668 struct sccb_header header; 669 u8 recognized[SCLP_CHP_INFO_MASK_SIZE]; 670 u8 standby[SCLP_CHP_INFO_MASK_SIZE]; 671 u8 configured[SCLP_CHP_INFO_MASK_SIZE]; 672 u8 ccm; 673 u8 reserved[6]; 674 u8 cssid; 675 } __attribute__((packed)); 676 677 /** 678 * sclp_chp_read_info - perform read channel-path information sclp command 679 * @info: resulting channel-path information data 680 * 681 * Perform read channel-path information sclp command and wait for completion. 682 * On success, store channel-path information in @info and return 0. Return 683 * non-zero otherwise. 684 */ 685 int sclp_chp_read_info(struct sclp_chp_info *info) 686 { 687 struct chp_info_sccb *sccb; 688 int rc; 689 690 if (!SCLP_HAS_CHP_INFO) 691 return -EOPNOTSUPP; 692 /* Prepare sccb. */ 693 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 694 if (!sccb) 695 return -ENOMEM; 696 sccb->header.length = sizeof(*sccb); 697 rc = do_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb); 698 if (rc) 699 goto out; 700 if (sccb->header.response_code != 0x0010) { 701 pr_warning("read channel-path info failed " 702 "(response=0x%04x)\n", sccb->header.response_code); 703 rc = -EIO; 704 goto out; 705 } 706 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE); 707 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE); 708 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE); 709 out: 710 free_page((unsigned long) sccb); 711 return rc; 712 } 713