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