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