1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * core function to access sclp interface 4 * 5 * Copyright IBM Corp. 1999, 2009 6 * 7 * Author(s): Martin Peschke <mpeschke@de.ibm.com> 8 * Martin Schwidefsky <schwidefsky@de.ibm.com> 9 */ 10 11 #include <linux/kernel_stat.h> 12 #include <linux/module.h> 13 #include <linux/err.h> 14 #include <linux/panic_notifier.h> 15 #include <linux/spinlock.h> 16 #include <linux/interrupt.h> 17 #include <linux/timer.h> 18 #include <linux/reboot.h> 19 #include <linux/jiffies.h> 20 #include <linux/init.h> 21 #include <linux/platform_device.h> 22 #include <asm/types.h> 23 #include <asm/irq.h> 24 #include <asm/debug.h> 25 26 #include "sclp.h" 27 28 #define SCLP_HEADER "sclp: " 29 30 struct sclp_trace_entry { 31 char id[4] __nonstring; 32 u32 a; 33 u64 b; 34 }; 35 36 #define SCLP_TRACE_ENTRY_SIZE sizeof(struct sclp_trace_entry) 37 #define SCLP_TRACE_MAX_SIZE 128 38 #define SCLP_TRACE_EVENT_MAX_SIZE 64 39 40 /* Debug trace area intended for all entries in abbreviated form. */ 41 DEFINE_STATIC_DEBUG_INFO(sclp_debug, "sclp", 8, 1, SCLP_TRACE_ENTRY_SIZE, 42 &debug_hex_ascii_view); 43 44 /* Error trace area intended for full entries relating to failed requests. */ 45 DEFINE_STATIC_DEBUG_INFO(sclp_debug_err, "sclp_err", 4, 1, 46 SCLP_TRACE_ENTRY_SIZE, &debug_hex_ascii_view); 47 48 /* Lock to protect internal data consistency. */ 49 static DEFINE_SPINLOCK(sclp_lock); 50 51 /* Mask of events that we can send to the sclp interface. */ 52 static sccb_mask_t sclp_receive_mask; 53 54 /* Mask of events that we can receive from the sclp interface. */ 55 static sccb_mask_t sclp_send_mask; 56 57 /* List of registered event listeners and senders. */ 58 static LIST_HEAD(sclp_reg_list); 59 60 /* List of queued requests. */ 61 static LIST_HEAD(sclp_req_queue); 62 63 /* Data for read and init requests. */ 64 static struct sclp_req sclp_read_req; 65 static struct sclp_req sclp_init_req; 66 static void *sclp_read_sccb; 67 static struct init_sccb *sclp_init_sccb; 68 69 /* Number of console pages to allocate, used by sclp_con.c and sclp_vt220.c */ 70 int sclp_console_pages = SCLP_CONSOLE_PAGES; 71 /* Flag to indicate if buffer pages are dropped on buffer full condition */ 72 bool sclp_console_drop = true; 73 /* Number of times the console dropped buffer pages */ 74 unsigned long sclp_console_full; 75 76 /* The currently active SCLP command word. */ 77 static sclp_cmdw_t active_cmd; 78 79 static inline void sclp_trace(int prio, char *id, u32 a, u64 b, bool err) 80 { 81 struct sclp_trace_entry e; 82 83 memset(&e, 0, sizeof(e)); 84 strtomem(e.id, id); 85 e.a = a; 86 e.b = b; 87 debug_event(&sclp_debug, prio, &e, sizeof(e)); 88 if (err) 89 debug_event(&sclp_debug_err, 0, &e, sizeof(e)); 90 } 91 92 static inline int no_zeroes_len(void *data, int len) 93 { 94 char *d = data; 95 96 /* Minimize trace area usage by not tracing trailing zeroes. */ 97 while (len > SCLP_TRACE_ENTRY_SIZE && d[len - 1] == 0) 98 len--; 99 100 return len; 101 } 102 103 static inline void sclp_trace_bin(int prio, void *d, int len, int errlen) 104 { 105 debug_event(&sclp_debug, prio, d, no_zeroes_len(d, len)); 106 if (errlen) 107 debug_event(&sclp_debug_err, 0, d, no_zeroes_len(d, errlen)); 108 } 109 110 static inline int abbrev_len(sclp_cmdw_t cmd, struct sccb_header *sccb) 111 { 112 struct evbuf_header *evbuf = (struct evbuf_header *)(sccb + 1); 113 int len = sccb->length, limit = SCLP_TRACE_MAX_SIZE; 114 115 /* Full SCCB tracing if debug level is set to max. */ 116 if (sclp_debug.level == DEBUG_MAX_LEVEL) 117 return len; 118 119 /* Minimal tracing for console writes. */ 120 if (cmd == SCLP_CMDW_WRITE_EVENT_DATA && 121 (evbuf->type == EVTYP_MSG || evbuf->type == EVTYP_VT220MSG)) 122 limit = SCLP_TRACE_ENTRY_SIZE; 123 124 return min(len, limit); 125 } 126 127 static inline void sclp_trace_sccb(int prio, char *id, u32 a, u64 b, 128 sclp_cmdw_t cmd, struct sccb_header *sccb, 129 bool err) 130 { 131 sclp_trace(prio, id, a, b, err); 132 if (sccb) { 133 sclp_trace_bin(prio + 1, sccb, abbrev_len(cmd, sccb), 134 err ? sccb->length : 0); 135 } 136 } 137 138 static inline void sclp_trace_evbuf(int prio, char *id, u32 a, u64 b, 139 struct evbuf_header *evbuf, bool err) 140 { 141 sclp_trace(prio, id, a, b, err); 142 sclp_trace_bin(prio + 1, evbuf, 143 min((int)evbuf->length, (int)SCLP_TRACE_EVENT_MAX_SIZE), 144 err ? evbuf->length : 0); 145 } 146 147 static inline void sclp_trace_req(int prio, char *id, struct sclp_req *req, 148 bool err) 149 { 150 struct sccb_header *sccb = req->sccb; 151 union { 152 struct { 153 u16 status; 154 u16 response; 155 u16 timeout; 156 u16 start_count; 157 }; 158 u64 b; 159 } summary; 160 161 summary.status = req->status; 162 summary.response = sccb ? sccb->response_code : 0; 163 summary.timeout = (u16)req->queue_timeout; 164 summary.start_count = (u16)req->start_count; 165 166 sclp_trace(prio, id, __pa(sccb), summary.b, err); 167 } 168 169 static inline void sclp_trace_register(int prio, char *id, u32 a, u64 b, 170 struct sclp_register *reg) 171 { 172 struct { 173 u64 receive; 174 u64 send; 175 } d; 176 177 d.receive = reg->receive_mask; 178 d.send = reg->send_mask; 179 180 sclp_trace(prio, id, a, b, false); 181 sclp_trace_bin(prio, &d, sizeof(d), 0); 182 } 183 184 static int __init sclp_setup_console_pages(char *str) 185 { 186 int pages, rc; 187 188 rc = kstrtoint(str, 0, &pages); 189 if (!rc && pages >= SCLP_CONSOLE_PAGES) 190 sclp_console_pages = pages; 191 return 1; 192 } 193 194 __setup("sclp_con_pages=", sclp_setup_console_pages); 195 196 static int __init sclp_setup_console_drop(char *str) 197 { 198 return kstrtobool(str, &sclp_console_drop) == 0; 199 } 200 201 __setup("sclp_con_drop=", sclp_setup_console_drop); 202 203 /* Timer for request retries. */ 204 static struct timer_list sclp_request_timer; 205 206 /* Timer for queued requests. */ 207 static struct timer_list sclp_queue_timer; 208 209 /* Internal state: is a request active at the sclp? */ 210 static volatile enum sclp_running_state_t { 211 sclp_running_state_idle, 212 sclp_running_state_running, 213 sclp_running_state_reset_pending 214 } sclp_running_state = sclp_running_state_idle; 215 216 /* Internal state: is a read request pending? */ 217 static volatile enum sclp_reading_state_t { 218 sclp_reading_state_idle, 219 sclp_reading_state_reading 220 } sclp_reading_state = sclp_reading_state_idle; 221 222 /* Internal state: is the driver currently serving requests? */ 223 static volatile enum sclp_activation_state_t { 224 sclp_activation_state_active, 225 sclp_activation_state_deactivating, 226 sclp_activation_state_inactive, 227 sclp_activation_state_activating 228 } sclp_activation_state = sclp_activation_state_active; 229 230 /* Internal state: is an init mask request pending? */ 231 static volatile enum sclp_mask_state_t { 232 sclp_mask_state_idle, 233 sclp_mask_state_initializing 234 } sclp_mask_state = sclp_mask_state_idle; 235 236 /* Maximum retry counts */ 237 #define SCLP_INIT_RETRY 3 238 #define SCLP_MASK_RETRY 3 239 240 /* Timeout intervals in seconds.*/ 241 #define SCLP_BUSY_INTERVAL 10 242 #define SCLP_RETRY_INTERVAL 30 243 244 static void sclp_request_timeout(bool force_restart); 245 static void sclp_process_queue(void); 246 static void __sclp_make_read_req(void); 247 static int sclp_init_mask(int calculate); 248 249 static void 250 __sclp_queue_read_req(void) 251 { 252 if (sclp_reading_state == sclp_reading_state_idle) { 253 sclp_reading_state = sclp_reading_state_reading; 254 __sclp_make_read_req(); 255 /* Add request to head of queue */ 256 list_add(&sclp_read_req.list, &sclp_req_queue); 257 } 258 } 259 260 /* Set up request retry timer. Called while sclp_lock is locked. */ 261 static inline void 262 __sclp_set_request_timer(unsigned long time, void (*cb)(struct timer_list *)) 263 { 264 timer_delete(&sclp_request_timer); 265 sclp_request_timer.function = cb; 266 sclp_request_timer.expires = jiffies + time; 267 add_timer(&sclp_request_timer); 268 } 269 270 static void sclp_request_timeout_restart(struct timer_list *unused) 271 { 272 sclp_request_timeout(true); 273 } 274 275 static void sclp_request_timeout_normal(struct timer_list *unused) 276 { 277 sclp_request_timeout(false); 278 } 279 280 /* Request timeout handler. Restart the request queue. If force_restart, 281 * force restart of running request. */ 282 static void sclp_request_timeout(bool force_restart) 283 { 284 unsigned long flags; 285 286 /* TMO: A timeout occurred (a=force_restart) */ 287 sclp_trace(2, "TMO", force_restart, 0, true); 288 289 spin_lock_irqsave(&sclp_lock, flags); 290 if (force_restart) { 291 if (sclp_running_state == sclp_running_state_running) { 292 /* Break running state and queue NOP read event request 293 * to get a defined interface state. */ 294 __sclp_queue_read_req(); 295 sclp_running_state = sclp_running_state_idle; 296 } 297 } else { 298 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ, 299 sclp_request_timeout_normal); 300 } 301 spin_unlock_irqrestore(&sclp_lock, flags); 302 sclp_process_queue(); 303 } 304 305 /* 306 * Returns the expire value in jiffies of the next pending request timeout, 307 * if any. Needs to be called with sclp_lock. 308 */ 309 static unsigned long __sclp_req_queue_find_next_timeout(void) 310 { 311 unsigned long expires_next = 0; 312 struct sclp_req *req; 313 314 list_for_each_entry(req, &sclp_req_queue, list) { 315 if (!req->queue_expires) 316 continue; 317 if (!expires_next || 318 (time_before(req->queue_expires, expires_next))) 319 expires_next = req->queue_expires; 320 } 321 return expires_next; 322 } 323 324 /* 325 * Returns expired request, if any, and removes it from the list. 326 */ 327 static struct sclp_req *__sclp_req_queue_remove_expired_req(void) 328 { 329 unsigned long flags, now; 330 struct sclp_req *req; 331 332 spin_lock_irqsave(&sclp_lock, flags); 333 now = jiffies; 334 /* Don't need list_for_each_safe because we break out after list_del */ 335 list_for_each_entry(req, &sclp_req_queue, list) { 336 if (!req->queue_expires) 337 continue; 338 if (time_before_eq(req->queue_expires, now)) { 339 if (req->status == SCLP_REQ_QUEUED) { 340 req->status = SCLP_REQ_QUEUED_TIMEOUT; 341 list_del(&req->list); 342 goto out; 343 } 344 } 345 } 346 req = NULL; 347 out: 348 spin_unlock_irqrestore(&sclp_lock, flags); 349 return req; 350 } 351 352 /* 353 * Timeout handler for queued requests. Removes request from list and 354 * invokes callback. This timer can be set per request in situations where 355 * waiting too long would be harmful to the system, e.g. during SE reboot. 356 */ 357 static void sclp_req_queue_timeout(struct timer_list *unused) 358 { 359 unsigned long flags, expires_next; 360 struct sclp_req *req; 361 362 do { 363 req = __sclp_req_queue_remove_expired_req(); 364 365 if (req) { 366 /* RQTM: Request timed out (a=sccb, b=summary) */ 367 sclp_trace_req(2, "RQTM", req, true); 368 } 369 370 if (req && req->callback) 371 req->callback(req, req->callback_data); 372 } while (req); 373 374 spin_lock_irqsave(&sclp_lock, flags); 375 expires_next = __sclp_req_queue_find_next_timeout(); 376 if (expires_next) 377 mod_timer(&sclp_queue_timer, expires_next); 378 spin_unlock_irqrestore(&sclp_lock, flags); 379 } 380 381 static int sclp_service_call_trace(sclp_cmdw_t command, void *sccb) 382 { 383 static u64 srvc_count; 384 int rc; 385 386 /* SRV1: Service call about to be issued (a=command, b=sccb address) */ 387 sclp_trace_sccb(0, "SRV1", command, (u64)sccb, command, sccb, false); 388 389 rc = sclp_service_call(command, sccb); 390 391 /* SRV2: Service call was issued (a=rc, b=SRVC sequence number) */ 392 sclp_trace(0, "SRV2", -rc, ++srvc_count, rc != 0); 393 394 if (rc == 0) 395 active_cmd = command; 396 397 return rc; 398 } 399 400 /* Try to start a request. Return zero if the request was successfully 401 * started or if it will be started at a later time. Return non-zero otherwise. 402 * Called while sclp_lock is locked. */ 403 static int 404 __sclp_start_request(struct sclp_req *req) 405 { 406 int rc; 407 408 if (sclp_running_state != sclp_running_state_idle) 409 return 0; 410 timer_delete(&sclp_request_timer); 411 rc = sclp_service_call_trace(req->command, req->sccb); 412 req->start_count++; 413 414 if (rc == 0) { 415 /* Successfully started request */ 416 req->status = SCLP_REQ_RUNNING; 417 sclp_running_state = sclp_running_state_running; 418 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ, 419 sclp_request_timeout_restart); 420 return 0; 421 } else if (rc == -EBUSY) { 422 /* Try again later */ 423 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ, 424 sclp_request_timeout_normal); 425 return 0; 426 } 427 /* Request failed */ 428 req->status = SCLP_REQ_FAILED; 429 return rc; 430 } 431 432 /* Try to start queued requests. */ 433 static void 434 sclp_process_queue(void) 435 { 436 struct sclp_req *req; 437 int rc; 438 unsigned long flags; 439 440 spin_lock_irqsave(&sclp_lock, flags); 441 if (sclp_running_state != sclp_running_state_idle) { 442 spin_unlock_irqrestore(&sclp_lock, flags); 443 return; 444 } 445 timer_delete(&sclp_request_timer); 446 while (!list_empty(&sclp_req_queue)) { 447 req = list_entry(sclp_req_queue.next, struct sclp_req, list); 448 rc = __sclp_start_request(req); 449 if (rc == 0) 450 break; 451 /* Request failed */ 452 if (req->start_count > 1) { 453 /* Cannot abort already submitted request - could still 454 * be active at the SCLP */ 455 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ, 456 sclp_request_timeout_normal); 457 break; 458 } 459 /* Post-processing for aborted request */ 460 list_del(&req->list); 461 462 /* RQAB: Request aborted (a=sccb, b=summary) */ 463 sclp_trace_req(2, "RQAB", req, true); 464 465 if (req->callback) { 466 spin_unlock_irqrestore(&sclp_lock, flags); 467 req->callback(req, req->callback_data); 468 spin_lock_irqsave(&sclp_lock, flags); 469 } 470 } 471 spin_unlock_irqrestore(&sclp_lock, flags); 472 } 473 474 static int __sclp_can_add_request(struct sclp_req *req) 475 { 476 if (req == &sclp_init_req) 477 return 1; 478 if (sclp_init_state != sclp_init_state_initialized) 479 return 0; 480 if (sclp_activation_state != sclp_activation_state_active) 481 return 0; 482 return 1; 483 } 484 485 /* Queue a new request. Return zero on success, non-zero otherwise. */ 486 int 487 sclp_add_request(struct sclp_req *req) 488 { 489 unsigned long flags; 490 int rc; 491 492 spin_lock_irqsave(&sclp_lock, flags); 493 if (!__sclp_can_add_request(req)) { 494 spin_unlock_irqrestore(&sclp_lock, flags); 495 return -EIO; 496 } 497 498 /* RQAD: Request was added (a=sccb, b=caller) */ 499 sclp_trace(2, "RQAD", __pa(req->sccb), _RET_IP_, false); 500 501 req->status = SCLP_REQ_QUEUED; 502 req->start_count = 0; 503 list_add_tail(&req->list, &sclp_req_queue); 504 rc = 0; 505 if (req->queue_timeout) { 506 req->queue_expires = jiffies + req->queue_timeout * HZ; 507 if (!timer_pending(&sclp_queue_timer) || 508 time_after(sclp_queue_timer.expires, req->queue_expires)) 509 mod_timer(&sclp_queue_timer, req->queue_expires); 510 } else 511 req->queue_expires = 0; 512 /* Start if request is first in list */ 513 if (sclp_running_state == sclp_running_state_idle && 514 req->list.prev == &sclp_req_queue) { 515 rc = __sclp_start_request(req); 516 if (rc) 517 list_del(&req->list); 518 } 519 spin_unlock_irqrestore(&sclp_lock, flags); 520 return rc; 521 } 522 523 EXPORT_SYMBOL(sclp_add_request); 524 525 /* Dispatch events found in request buffer to registered listeners. Return 0 526 * if all events were dispatched, non-zero otherwise. */ 527 static int 528 sclp_dispatch_evbufs(struct sccb_header *sccb) 529 { 530 unsigned long flags; 531 struct evbuf_header *evbuf; 532 struct list_head *l; 533 struct sclp_register *reg; 534 int offset; 535 int rc; 536 537 spin_lock_irqsave(&sclp_lock, flags); 538 rc = 0; 539 for (offset = sizeof(struct sccb_header); offset < sccb->length; 540 offset += evbuf->length) { 541 evbuf = (struct evbuf_header *) ((addr_t) sccb + offset); 542 /* Check for malformed hardware response */ 543 if (evbuf->length == 0) 544 break; 545 /* Search for event handler */ 546 reg = NULL; 547 list_for_each(l, &sclp_reg_list) { 548 reg = list_entry(l, struct sclp_register, list); 549 if (reg->receive_mask & SCLP_EVTYP_MASK(evbuf->type)) 550 break; 551 else 552 reg = NULL; 553 } 554 555 /* EVNT: Event callback (b=receiver) */ 556 sclp_trace_evbuf(2, "EVNT", 0, reg ? (u64)reg->receiver_fn : 0, 557 evbuf, !reg); 558 559 if (reg && reg->receiver_fn) { 560 spin_unlock_irqrestore(&sclp_lock, flags); 561 reg->receiver_fn(evbuf); 562 spin_lock_irqsave(&sclp_lock, flags); 563 } else if (reg == NULL) 564 rc = -EOPNOTSUPP; 565 } 566 spin_unlock_irqrestore(&sclp_lock, flags); 567 return rc; 568 } 569 570 /* Read event data request callback. */ 571 static void 572 sclp_read_cb(struct sclp_req *req, void *data) 573 { 574 unsigned long flags; 575 struct sccb_header *sccb; 576 577 sccb = (struct sccb_header *) req->sccb; 578 if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 || 579 sccb->response_code == 0x220)) 580 sclp_dispatch_evbufs(sccb); 581 spin_lock_irqsave(&sclp_lock, flags); 582 sclp_reading_state = sclp_reading_state_idle; 583 spin_unlock_irqrestore(&sclp_lock, flags); 584 } 585 586 /* Prepare read event data request. Called while sclp_lock is locked. */ 587 static void __sclp_make_read_req(void) 588 { 589 struct sccb_header *sccb; 590 591 sccb = (struct sccb_header *) sclp_read_sccb; 592 clear_page(sccb); 593 memset(&sclp_read_req, 0, sizeof(struct sclp_req)); 594 sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA; 595 sclp_read_req.status = SCLP_REQ_QUEUED; 596 sclp_read_req.start_count = 0; 597 sclp_read_req.callback = sclp_read_cb; 598 sclp_read_req.sccb = sccb; 599 sccb->length = PAGE_SIZE; 600 sccb->function_code = 0; 601 sccb->control_mask[2] = 0x80; 602 } 603 604 /* Search request list for request with matching sccb. Return request if found, 605 * NULL otherwise. Called while sclp_lock is locked. */ 606 static inline struct sclp_req * 607 __sclp_find_req(u32 sccb) 608 { 609 struct list_head *l; 610 struct sclp_req *req; 611 612 list_for_each(l, &sclp_req_queue) { 613 req = list_entry(l, struct sclp_req, list); 614 if (sccb == __pa(req->sccb)) 615 return req; 616 } 617 return NULL; 618 } 619 620 static bool ok_response(u32 sccb_int, sclp_cmdw_t cmd) 621 { 622 struct sccb_header *sccb = (struct sccb_header *)__va(sccb_int); 623 struct evbuf_header *evbuf; 624 u16 response; 625 626 if (!sccb) 627 return true; 628 629 /* Check SCCB response. */ 630 response = sccb->response_code & 0xff; 631 if (response != 0x10 && response != 0x20) 632 return false; 633 634 /* Check event-processed flag on outgoing events. */ 635 if (cmd == SCLP_CMDW_WRITE_EVENT_DATA) { 636 evbuf = (struct evbuf_header *)(sccb + 1); 637 if (!(evbuf->flags & 0x80)) 638 return false; 639 } 640 641 return true; 642 } 643 644 /* Handler for external interruption. Perform request post-processing. 645 * Prepare read event data request if necessary. Start processing of next 646 * request on queue. */ 647 static void sclp_interrupt_handler(struct ext_code ext_code, 648 unsigned int param32, unsigned long param64) 649 { 650 struct sclp_req *req; 651 u32 finished_sccb; 652 u32 evbuf_pending; 653 654 inc_irq_stat(IRQEXT_SCP); 655 spin_lock(&sclp_lock); 656 finished_sccb = param32 & 0xfffffff8; 657 evbuf_pending = param32 & 0x3; 658 659 /* INT: Interrupt received (a=intparm, b=cmd) */ 660 sclp_trace_sccb(0, "INT", param32, active_cmd, active_cmd, 661 (struct sccb_header *)__va(finished_sccb), 662 !ok_response(finished_sccb, active_cmd)); 663 664 if (finished_sccb) { 665 timer_delete(&sclp_request_timer); 666 sclp_running_state = sclp_running_state_reset_pending; 667 req = __sclp_find_req(finished_sccb); 668 if (req) { 669 /* Request post-processing */ 670 list_del(&req->list); 671 req->status = SCLP_REQ_DONE; 672 673 /* RQOK: Request success (a=sccb, b=summary) */ 674 sclp_trace_req(2, "RQOK", req, false); 675 676 if (req->callback) { 677 spin_unlock(&sclp_lock); 678 req->callback(req, req->callback_data); 679 spin_lock(&sclp_lock); 680 } 681 } else { 682 /* UNEX: Unexpected SCCB completion (a=sccb address) */ 683 sclp_trace(0, "UNEX", finished_sccb, 0, true); 684 } 685 sclp_running_state = sclp_running_state_idle; 686 active_cmd = 0; 687 } 688 if (evbuf_pending && 689 sclp_activation_state == sclp_activation_state_active) 690 __sclp_queue_read_req(); 691 spin_unlock(&sclp_lock); 692 sclp_process_queue(); 693 } 694 695 /* Convert interval in jiffies to TOD ticks. */ 696 static inline u64 697 sclp_tod_from_jiffies(unsigned long jiffies) 698 { 699 return (u64) (jiffies / HZ) << 32; 700 } 701 702 /* Wait until a currently running request finished. Note: while this function 703 * is running, no timers are served on the calling CPU. */ 704 void 705 sclp_sync_wait(void) 706 { 707 unsigned long long old_tick; 708 struct ctlreg cr0, cr0_sync; 709 unsigned long flags; 710 static u64 sync_count; 711 u64 timeout; 712 int irq_context; 713 714 /* SYN1: Synchronous wait start (a=runstate, b=sync count) */ 715 sclp_trace(4, "SYN1", sclp_running_state, ++sync_count, false); 716 717 /* We'll be disabling timer interrupts, so we need a custom timeout 718 * mechanism */ 719 timeout = 0; 720 if (timer_pending(&sclp_request_timer)) { 721 /* Get timeout TOD value */ 722 timeout = get_tod_clock_fast() + 723 sclp_tod_from_jiffies(sclp_request_timer.expires - 724 jiffies); 725 } 726 local_irq_save(flags); 727 /* Prevent bottom half from executing once we force interrupts open */ 728 irq_context = in_interrupt(); 729 if (!irq_context) 730 local_bh_disable(); 731 /* Enable service-signal interruption, disable timer interrupts */ 732 old_tick = local_tick_disable(); 733 trace_hardirqs_on(); 734 local_ctl_store(0, &cr0); 735 cr0_sync.val = cr0.val & ~CR0_IRQ_SUBCLASS_MASK; 736 cr0_sync.val |= 1UL << (63 - 54); 737 local_ctl_load(0, &cr0_sync); 738 arch_local_irq_enable_external(); 739 /* Loop until driver state indicates finished request */ 740 while (sclp_running_state != sclp_running_state_idle) { 741 /* Check for expired request timer */ 742 if (get_tod_clock_fast() > timeout && timer_delete(&sclp_request_timer)) 743 sclp_request_timer.function(&sclp_request_timer); 744 cpu_relax(); 745 } 746 local_irq_disable(); 747 local_ctl_load(0, &cr0); 748 if (!irq_context) 749 _local_bh_enable(); 750 local_tick_enable(old_tick); 751 local_irq_restore(flags); 752 753 /* SYN2: Synchronous wait end (a=runstate, b=sync_count) */ 754 sclp_trace(4, "SYN2", sclp_running_state, sync_count, false); 755 } 756 EXPORT_SYMBOL(sclp_sync_wait); 757 758 /* Dispatch changes in send and receive mask to registered listeners. */ 759 static void 760 sclp_dispatch_state_change(void) 761 { 762 struct list_head *l; 763 struct sclp_register *reg; 764 unsigned long flags; 765 sccb_mask_t receive_mask; 766 sccb_mask_t send_mask; 767 768 do { 769 spin_lock_irqsave(&sclp_lock, flags); 770 reg = NULL; 771 list_for_each(l, &sclp_reg_list) { 772 reg = list_entry(l, struct sclp_register, list); 773 receive_mask = reg->send_mask & sclp_receive_mask; 774 send_mask = reg->receive_mask & sclp_send_mask; 775 if (reg->sclp_receive_mask != receive_mask || 776 reg->sclp_send_mask != send_mask) { 777 reg->sclp_receive_mask = receive_mask; 778 reg->sclp_send_mask = send_mask; 779 break; 780 } else 781 reg = NULL; 782 } 783 spin_unlock_irqrestore(&sclp_lock, flags); 784 if (reg && reg->state_change_fn) { 785 /* STCG: State-change callback (b=callback) */ 786 sclp_trace(2, "STCG", 0, (u64)reg->state_change_fn, 787 false); 788 789 reg->state_change_fn(reg); 790 } 791 } while (reg); 792 } 793 794 struct sclp_statechangebuf { 795 struct evbuf_header header; 796 u8 validity_sclp_active_facility_mask : 1; 797 u8 validity_sclp_receive_mask : 1; 798 u8 validity_sclp_send_mask : 1; 799 u8 validity_read_data_function_mask : 1; 800 u16 _zeros : 12; 801 u16 mask_length; 802 u64 sclp_active_facility_mask; 803 u8 masks[2 * 1021 + 4]; /* variable length */ 804 /* 805 * u8 sclp_receive_mask[mask_length]; 806 * u8 sclp_send_mask[mask_length]; 807 * u32 read_data_function_mask; 808 */ 809 } __attribute__((packed)); 810 811 812 /* State change event callback. Inform listeners of changes. */ 813 static void 814 sclp_state_change_cb(struct evbuf_header *evbuf) 815 { 816 unsigned long flags; 817 struct sclp_statechangebuf *scbuf; 818 819 BUILD_BUG_ON(sizeof(struct sclp_statechangebuf) > PAGE_SIZE); 820 821 scbuf = (struct sclp_statechangebuf *) evbuf; 822 spin_lock_irqsave(&sclp_lock, flags); 823 if (scbuf->validity_sclp_receive_mask) 824 sclp_receive_mask = sccb_get_recv_mask(scbuf); 825 if (scbuf->validity_sclp_send_mask) 826 sclp_send_mask = sccb_get_send_mask(scbuf); 827 spin_unlock_irqrestore(&sclp_lock, flags); 828 if (scbuf->validity_sclp_active_facility_mask) 829 sclp.facilities = scbuf->sclp_active_facility_mask; 830 sclp_dispatch_state_change(); 831 } 832 833 static struct sclp_register sclp_state_change_event = { 834 .receive_mask = EVTYP_STATECHANGE_MASK, 835 .receiver_fn = sclp_state_change_cb 836 }; 837 838 /* Calculate receive and send mask of currently registered listeners. 839 * Called while sclp_lock is locked. */ 840 static inline void 841 __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask) 842 { 843 struct list_head *l; 844 struct sclp_register *t; 845 846 *receive_mask = 0; 847 *send_mask = 0; 848 list_for_each(l, &sclp_reg_list) { 849 t = list_entry(l, struct sclp_register, list); 850 *receive_mask |= t->receive_mask; 851 *send_mask |= t->send_mask; 852 } 853 } 854 855 /* Register event listener. Return 0 on success, non-zero otherwise. */ 856 int 857 sclp_register(struct sclp_register *reg) 858 { 859 unsigned long flags; 860 sccb_mask_t receive_mask; 861 sccb_mask_t send_mask; 862 int rc; 863 864 /* REG: Event listener registered (b=caller) */ 865 sclp_trace_register(2, "REG", 0, _RET_IP_, reg); 866 867 rc = sclp_init(); 868 if (rc) 869 return rc; 870 spin_lock_irqsave(&sclp_lock, flags); 871 /* Check event mask for collisions */ 872 __sclp_get_mask(&receive_mask, &send_mask); 873 if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) { 874 spin_unlock_irqrestore(&sclp_lock, flags); 875 return -EBUSY; 876 } 877 /* Trigger initial state change callback */ 878 reg->sclp_receive_mask = 0; 879 reg->sclp_send_mask = 0; 880 list_add(®->list, &sclp_reg_list); 881 spin_unlock_irqrestore(&sclp_lock, flags); 882 rc = sclp_init_mask(1); 883 if (rc) { 884 spin_lock_irqsave(&sclp_lock, flags); 885 list_del(®->list); 886 spin_unlock_irqrestore(&sclp_lock, flags); 887 } 888 return rc; 889 } 890 891 EXPORT_SYMBOL(sclp_register); 892 893 /* Unregister event listener. */ 894 void 895 sclp_unregister(struct sclp_register *reg) 896 { 897 unsigned long flags; 898 899 /* UREG: Event listener unregistered (b=caller) */ 900 sclp_trace_register(2, "UREG", 0, _RET_IP_, reg); 901 902 spin_lock_irqsave(&sclp_lock, flags); 903 list_del(®->list); 904 spin_unlock_irqrestore(&sclp_lock, flags); 905 sclp_init_mask(1); 906 } 907 908 EXPORT_SYMBOL(sclp_unregister); 909 910 /* Remove event buffers which are marked processed. Return the number of 911 * remaining event buffers. */ 912 int 913 sclp_remove_processed(struct sccb_header *sccb) 914 { 915 struct evbuf_header *evbuf; 916 int unprocessed; 917 u16 remaining; 918 919 evbuf = (struct evbuf_header *) (sccb + 1); 920 unprocessed = 0; 921 remaining = sccb->length - sizeof(struct sccb_header); 922 while (remaining > 0) { 923 remaining -= evbuf->length; 924 if (evbuf->flags & 0x80) { 925 sccb->length -= evbuf->length; 926 memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length), 927 remaining); 928 } else { 929 unprocessed++; 930 evbuf = (struct evbuf_header *) 931 ((addr_t) evbuf + evbuf->length); 932 } 933 } 934 return unprocessed; 935 } 936 937 EXPORT_SYMBOL(sclp_remove_processed); 938 939 /* Prepare init mask request. Called while sclp_lock is locked. */ 940 static inline void 941 __sclp_make_init_req(sccb_mask_t receive_mask, sccb_mask_t send_mask) 942 { 943 struct init_sccb *sccb = sclp_init_sccb; 944 945 clear_page(sccb); 946 memset(&sclp_init_req, 0, sizeof(struct sclp_req)); 947 sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK; 948 sclp_init_req.status = SCLP_REQ_FILLED; 949 sclp_init_req.start_count = 0; 950 sclp_init_req.callback = NULL; 951 sclp_init_req.callback_data = NULL; 952 sclp_init_req.sccb = sccb; 953 sccb->header.length = sizeof(*sccb); 954 if (sclp_mask_compat_mode) 955 sccb->mask_length = SCLP_MASK_SIZE_COMPAT; 956 else 957 sccb->mask_length = sizeof(sccb_mask_t); 958 sccb_set_recv_mask(sccb, receive_mask); 959 sccb_set_send_mask(sccb, send_mask); 960 sccb_set_sclp_recv_mask(sccb, 0); 961 sccb_set_sclp_send_mask(sccb, 0); 962 } 963 964 /* Start init mask request. If calculate is non-zero, calculate the mask as 965 * requested by registered listeners. Use zero mask otherwise. Return 0 on 966 * success, non-zero otherwise. */ 967 static int 968 sclp_init_mask(int calculate) 969 { 970 unsigned long flags; 971 struct init_sccb *sccb = sclp_init_sccb; 972 sccb_mask_t receive_mask; 973 sccb_mask_t send_mask; 974 int retry; 975 int rc; 976 unsigned long wait; 977 978 spin_lock_irqsave(&sclp_lock, flags); 979 /* Check if interface is in appropriate state */ 980 if (sclp_mask_state != sclp_mask_state_idle) { 981 spin_unlock_irqrestore(&sclp_lock, flags); 982 return -EBUSY; 983 } 984 if (sclp_activation_state == sclp_activation_state_inactive) { 985 spin_unlock_irqrestore(&sclp_lock, flags); 986 return -EINVAL; 987 } 988 sclp_mask_state = sclp_mask_state_initializing; 989 /* Determine mask */ 990 if (calculate) 991 __sclp_get_mask(&receive_mask, &send_mask); 992 else { 993 receive_mask = 0; 994 send_mask = 0; 995 } 996 rc = -EIO; 997 for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) { 998 /* Prepare request */ 999 __sclp_make_init_req(receive_mask, send_mask); 1000 spin_unlock_irqrestore(&sclp_lock, flags); 1001 if (sclp_add_request(&sclp_init_req)) { 1002 /* Try again later */ 1003 wait = jiffies + SCLP_BUSY_INTERVAL * HZ; 1004 while (time_before(jiffies, wait)) 1005 sclp_sync_wait(); 1006 spin_lock_irqsave(&sclp_lock, flags); 1007 continue; 1008 } 1009 while (sclp_init_req.status != SCLP_REQ_DONE && 1010 sclp_init_req.status != SCLP_REQ_FAILED) 1011 sclp_sync_wait(); 1012 spin_lock_irqsave(&sclp_lock, flags); 1013 if (sclp_init_req.status == SCLP_REQ_DONE && 1014 sccb->header.response_code == 0x20) { 1015 /* Successful request */ 1016 if (calculate) { 1017 sclp_receive_mask = sccb_get_sclp_recv_mask(sccb); 1018 sclp_send_mask = sccb_get_sclp_send_mask(sccb); 1019 } else { 1020 sclp_receive_mask = 0; 1021 sclp_send_mask = 0; 1022 } 1023 spin_unlock_irqrestore(&sclp_lock, flags); 1024 sclp_dispatch_state_change(); 1025 spin_lock_irqsave(&sclp_lock, flags); 1026 rc = 0; 1027 break; 1028 } 1029 } 1030 sclp_mask_state = sclp_mask_state_idle; 1031 spin_unlock_irqrestore(&sclp_lock, flags); 1032 return rc; 1033 } 1034 1035 /* Deactivate SCLP interface. On success, new requests will be rejected, 1036 * events will no longer be dispatched. Return 0 on success, non-zero 1037 * otherwise. */ 1038 int 1039 sclp_deactivate(void) 1040 { 1041 unsigned long flags; 1042 int rc; 1043 1044 spin_lock_irqsave(&sclp_lock, flags); 1045 /* Deactivate can only be called when active */ 1046 if (sclp_activation_state != sclp_activation_state_active) { 1047 spin_unlock_irqrestore(&sclp_lock, flags); 1048 return -EINVAL; 1049 } 1050 sclp_activation_state = sclp_activation_state_deactivating; 1051 spin_unlock_irqrestore(&sclp_lock, flags); 1052 rc = sclp_init_mask(0); 1053 spin_lock_irqsave(&sclp_lock, flags); 1054 if (rc == 0) 1055 sclp_activation_state = sclp_activation_state_inactive; 1056 else 1057 sclp_activation_state = sclp_activation_state_active; 1058 spin_unlock_irqrestore(&sclp_lock, flags); 1059 return rc; 1060 } 1061 1062 EXPORT_SYMBOL(sclp_deactivate); 1063 1064 /* Reactivate SCLP interface after sclp_deactivate. On success, new 1065 * requests will be accepted, events will be dispatched again. Return 0 on 1066 * success, non-zero otherwise. */ 1067 int 1068 sclp_reactivate(void) 1069 { 1070 unsigned long flags; 1071 int rc; 1072 1073 spin_lock_irqsave(&sclp_lock, flags); 1074 /* Reactivate can only be called when inactive */ 1075 if (sclp_activation_state != sclp_activation_state_inactive) { 1076 spin_unlock_irqrestore(&sclp_lock, flags); 1077 return -EINVAL; 1078 } 1079 sclp_activation_state = sclp_activation_state_activating; 1080 spin_unlock_irqrestore(&sclp_lock, flags); 1081 rc = sclp_init_mask(1); 1082 spin_lock_irqsave(&sclp_lock, flags); 1083 if (rc == 0) 1084 sclp_activation_state = sclp_activation_state_active; 1085 else 1086 sclp_activation_state = sclp_activation_state_inactive; 1087 spin_unlock_irqrestore(&sclp_lock, flags); 1088 return rc; 1089 } 1090 1091 EXPORT_SYMBOL(sclp_reactivate); 1092 1093 /* Handler for external interruption used during initialization. Modify 1094 * request state to done. */ 1095 static void sclp_check_handler(struct ext_code ext_code, 1096 unsigned int param32, unsigned long param64) 1097 { 1098 u32 finished_sccb; 1099 1100 inc_irq_stat(IRQEXT_SCP); 1101 finished_sccb = param32 & 0xfffffff8; 1102 /* Is this the interrupt we are waiting for? */ 1103 if (finished_sccb == 0) 1104 return; 1105 if (finished_sccb != __pa(sclp_init_sccb)) 1106 panic("sclp: unsolicited interrupt for buffer at 0x%x\n", 1107 finished_sccb); 1108 spin_lock(&sclp_lock); 1109 if (sclp_running_state == sclp_running_state_running) { 1110 sclp_init_req.status = SCLP_REQ_DONE; 1111 sclp_running_state = sclp_running_state_idle; 1112 } 1113 spin_unlock(&sclp_lock); 1114 } 1115 1116 /* Initial init mask request timed out. Modify request state to failed. */ 1117 static void 1118 sclp_check_timeout(struct timer_list *unused) 1119 { 1120 unsigned long flags; 1121 1122 spin_lock_irqsave(&sclp_lock, flags); 1123 if (sclp_running_state == sclp_running_state_running) { 1124 sclp_init_req.status = SCLP_REQ_FAILED; 1125 sclp_running_state = sclp_running_state_idle; 1126 } 1127 spin_unlock_irqrestore(&sclp_lock, flags); 1128 } 1129 1130 /* Perform a check of the SCLP interface. Return zero if the interface is 1131 * available and there are no pending requests from a previous instance. 1132 * Return non-zero otherwise. */ 1133 static int 1134 sclp_check_interface(void) 1135 { 1136 struct init_sccb *sccb; 1137 unsigned long flags; 1138 int retry; 1139 int rc; 1140 1141 spin_lock_irqsave(&sclp_lock, flags); 1142 /* Prepare init mask command */ 1143 rc = register_external_irq(EXT_IRQ_SERVICE_SIG, sclp_check_handler); 1144 if (rc) { 1145 spin_unlock_irqrestore(&sclp_lock, flags); 1146 return rc; 1147 } 1148 for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) { 1149 __sclp_make_init_req(0, 0); 1150 sccb = (struct init_sccb *) sclp_init_req.sccb; 1151 rc = sclp_service_call_trace(sclp_init_req.command, sccb); 1152 if (rc == -EIO) 1153 break; 1154 sclp_init_req.status = SCLP_REQ_RUNNING; 1155 sclp_running_state = sclp_running_state_running; 1156 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ, 1157 sclp_check_timeout); 1158 spin_unlock_irqrestore(&sclp_lock, flags); 1159 /* Enable service-signal interruption - needs to happen 1160 * with IRQs enabled. */ 1161 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL); 1162 /* Wait for signal from interrupt or timeout */ 1163 sclp_sync_wait(); 1164 /* Disable service-signal interruption - needs to happen 1165 * with IRQs enabled. */ 1166 irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL); 1167 spin_lock_irqsave(&sclp_lock, flags); 1168 timer_delete(&sclp_request_timer); 1169 rc = -EBUSY; 1170 if (sclp_init_req.status == SCLP_REQ_DONE) { 1171 if (sccb->header.response_code == 0x20) { 1172 rc = 0; 1173 break; 1174 } else if (sccb->header.response_code == 0x74f0) { 1175 if (!sclp_mask_compat_mode) { 1176 sclp_mask_compat_mode = true; 1177 retry = 0; 1178 } 1179 } 1180 } 1181 } 1182 unregister_external_irq(EXT_IRQ_SERVICE_SIG, sclp_check_handler); 1183 spin_unlock_irqrestore(&sclp_lock, flags); 1184 return rc; 1185 } 1186 1187 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP 1188 * events from interfering with rebooted system. */ 1189 static int 1190 sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr) 1191 { 1192 sclp_deactivate(); 1193 return NOTIFY_DONE; 1194 } 1195 1196 static struct notifier_block sclp_reboot_notifier = { 1197 .notifier_call = sclp_reboot_event, 1198 .priority = INT_MIN, 1199 }; 1200 1201 static ssize_t con_pages_show(struct device_driver *dev, char *buf) 1202 { 1203 return sysfs_emit(buf, "%i\n", sclp_console_pages); 1204 } 1205 1206 static DRIVER_ATTR_RO(con_pages); 1207 1208 static ssize_t con_drop_store(struct device_driver *dev, const char *buf, size_t count) 1209 { 1210 int rc; 1211 1212 rc = kstrtobool(buf, &sclp_console_drop); 1213 return rc ?: count; 1214 } 1215 1216 static ssize_t con_drop_show(struct device_driver *dev, char *buf) 1217 { 1218 return sysfs_emit(buf, "%i\n", sclp_console_drop); 1219 } 1220 1221 static DRIVER_ATTR_RW(con_drop); 1222 1223 static ssize_t con_full_show(struct device_driver *dev, char *buf) 1224 { 1225 return sysfs_emit(buf, "%lu\n", sclp_console_full); 1226 } 1227 1228 static DRIVER_ATTR_RO(con_full); 1229 1230 static struct attribute *sclp_drv_attrs[] = { 1231 &driver_attr_con_pages.attr, 1232 &driver_attr_con_drop.attr, 1233 &driver_attr_con_full.attr, 1234 NULL, 1235 }; 1236 static struct attribute_group sclp_drv_attr_group = { 1237 .attrs = sclp_drv_attrs, 1238 }; 1239 static const struct attribute_group *sclp_drv_attr_groups[] = { 1240 &sclp_drv_attr_group, 1241 NULL, 1242 }; 1243 1244 static struct platform_driver sclp_pdrv = { 1245 .driver = { 1246 .name = "sclp", 1247 .groups = sclp_drv_attr_groups, 1248 }, 1249 }; 1250 1251 /* Initialize SCLP driver. Return zero if driver is operational, non-zero 1252 * otherwise. */ 1253 int sclp_init(void) 1254 { 1255 unsigned long flags; 1256 int rc = 0; 1257 1258 spin_lock_irqsave(&sclp_lock, flags); 1259 /* Check for previous or running initialization */ 1260 if (sclp_init_state != sclp_init_state_uninitialized) 1261 goto fail_unlock; 1262 sclp_init_state = sclp_init_state_initializing; 1263 sclp_read_sccb = (void *) __get_free_page(GFP_ATOMIC | GFP_DMA); 1264 sclp_init_sccb = (void *) __get_free_page(GFP_ATOMIC | GFP_DMA); 1265 BUG_ON(!sclp_read_sccb || !sclp_init_sccb); 1266 /* Set up variables */ 1267 list_add(&sclp_state_change_event.list, &sclp_reg_list); 1268 timer_setup(&sclp_request_timer, NULL, 0); 1269 timer_setup(&sclp_queue_timer, sclp_req_queue_timeout, 0); 1270 /* Check interface */ 1271 spin_unlock_irqrestore(&sclp_lock, flags); 1272 rc = sclp_check_interface(); 1273 spin_lock_irqsave(&sclp_lock, flags); 1274 if (rc) 1275 goto fail_init_state_uninitialized; 1276 /* Register reboot handler */ 1277 rc = register_reboot_notifier(&sclp_reboot_notifier); 1278 if (rc) 1279 goto fail_init_state_uninitialized; 1280 /* Register interrupt handler */ 1281 rc = register_external_irq(EXT_IRQ_SERVICE_SIG, sclp_interrupt_handler); 1282 if (rc) 1283 goto fail_unregister_reboot_notifier; 1284 sclp_init_state = sclp_init_state_initialized; 1285 spin_unlock_irqrestore(&sclp_lock, flags); 1286 /* Enable service-signal external interruption - needs to happen with 1287 * IRQs enabled. */ 1288 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL); 1289 sclp_init_mask(1); 1290 return 0; 1291 1292 fail_unregister_reboot_notifier: 1293 unregister_reboot_notifier(&sclp_reboot_notifier); 1294 fail_init_state_uninitialized: 1295 list_del(&sclp_state_change_event.list); 1296 sclp_init_state = sclp_init_state_uninitialized; 1297 free_page((unsigned long) sclp_read_sccb); 1298 free_page((unsigned long) sclp_init_sccb); 1299 fail_unlock: 1300 spin_unlock_irqrestore(&sclp_lock, flags); 1301 return rc; 1302 } 1303 1304 static __init int sclp_initcall(void) 1305 { 1306 return platform_driver_register(&sclp_pdrv); 1307 } 1308 1309 arch_initcall(sclp_initcall); 1310