sclp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * drivers/s390/char/sclp.c
  3. * core function to access sclp interface
  4. *
  5. * S390 version
  6. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Martin Peschke <mpeschke@de.ibm.com>
  8. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/timer.h>
  15. #include <linux/reboot.h>
  16. #include <linux/jiffies.h>
  17. #include <asm/types.h>
  18. #include <asm/s390_ext.h>
  19. #include "sclp.h"
  20. #define SCLP_HEADER "sclp: "
  21. /* Structure for register_early_external_interrupt. */
  22. static ext_int_info_t ext_int_info_hwc;
  23. /* Lock to protect internal data consistency. */
  24. static DEFINE_SPINLOCK(sclp_lock);
  25. /* Mask of events that we can receive from the sclp interface. */
  26. static sccb_mask_t sclp_receive_mask;
  27. /* Mask of events that we can send to the sclp interface. */
  28. static sccb_mask_t sclp_send_mask;
  29. /* List of registered event listeners and senders. */
  30. static struct list_head sclp_reg_list;
  31. /* List of queued requests. */
  32. static struct list_head sclp_req_queue;
  33. /* Data for read and and init requests. */
  34. static struct sclp_req sclp_read_req;
  35. static struct sclp_req sclp_init_req;
  36. static char sclp_read_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
  37. static char sclp_init_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
  38. /* Timer for request retries. */
  39. static struct timer_list sclp_request_timer;
  40. /* Internal state: is the driver initialized? */
  41. static volatile enum sclp_init_state_t {
  42. sclp_init_state_uninitialized,
  43. sclp_init_state_initializing,
  44. sclp_init_state_initialized
  45. } sclp_init_state = sclp_init_state_uninitialized;
  46. /* Internal state: is a request active at the sclp? */
  47. static volatile enum sclp_running_state_t {
  48. sclp_running_state_idle,
  49. sclp_running_state_running
  50. } sclp_running_state = sclp_running_state_idle;
  51. /* Internal state: is a read request pending? */
  52. static volatile enum sclp_reading_state_t {
  53. sclp_reading_state_idle,
  54. sclp_reading_state_reading
  55. } sclp_reading_state = sclp_reading_state_idle;
  56. /* Internal state: is the driver currently serving requests? */
  57. static volatile enum sclp_activation_state_t {
  58. sclp_activation_state_active,
  59. sclp_activation_state_deactivating,
  60. sclp_activation_state_inactive,
  61. sclp_activation_state_activating
  62. } sclp_activation_state = sclp_activation_state_active;
  63. /* Internal state: is an init mask request pending? */
  64. static volatile enum sclp_mask_state_t {
  65. sclp_mask_state_idle,
  66. sclp_mask_state_initializing
  67. } sclp_mask_state = sclp_mask_state_idle;
  68. /* Maximum retry counts */
  69. #define SCLP_INIT_RETRY 3
  70. #define SCLP_MASK_RETRY 3
  71. /* Timeout intervals in seconds.*/
  72. #define SCLP_BUSY_INTERVAL 10
  73. #define SCLP_RETRY_INTERVAL 15
  74. static void sclp_process_queue(void);
  75. static int sclp_init_mask(int calculate);
  76. static int sclp_init(void);
  77. /* Perform service call. Return 0 on success, non-zero otherwise. */
  78. static int
  79. service_call(sclp_cmdw_t command, void *sccb)
  80. {
  81. int cc;
  82. asm volatile(
  83. " .insn rre,0xb2200000,%1,%2\n" /* servc %1,%2 */
  84. " ipm %0\n"
  85. " srl %0,28"
  86. : "=&d" (cc) : "d" (command), "a" (__pa(sccb))
  87. : "cc", "memory");
  88. if (cc == 3)
  89. return -EIO;
  90. if (cc == 2)
  91. return -EBUSY;
  92. return 0;
  93. }
  94. /* Request timeout handler. Restart the request queue. If DATA is non-zero,
  95. * force restart of running request. */
  96. static void
  97. sclp_request_timeout(unsigned long data)
  98. {
  99. unsigned long flags;
  100. if (data) {
  101. spin_lock_irqsave(&sclp_lock, flags);
  102. sclp_running_state = sclp_running_state_idle;
  103. spin_unlock_irqrestore(&sclp_lock, flags);
  104. }
  105. sclp_process_queue();
  106. }
  107. /* Set up request retry timer. Called while sclp_lock is locked. */
  108. static inline void
  109. __sclp_set_request_timer(unsigned long time, void (*function)(unsigned long),
  110. unsigned long data)
  111. {
  112. del_timer(&sclp_request_timer);
  113. sclp_request_timer.function = function;
  114. sclp_request_timer.data = data;
  115. sclp_request_timer.expires = jiffies + time;
  116. add_timer(&sclp_request_timer);
  117. }
  118. /* Try to start a request. Return zero if the request was successfully
  119. * started or if it will be started at a later time. Return non-zero otherwise.
  120. * Called while sclp_lock is locked. */
  121. static int
  122. __sclp_start_request(struct sclp_req *req)
  123. {
  124. int rc;
  125. if (sclp_running_state != sclp_running_state_idle)
  126. return 0;
  127. del_timer(&sclp_request_timer);
  128. rc = service_call(req->command, req->sccb);
  129. req->start_count++;
  130. if (rc == 0) {
  131. /* Sucessfully started request */
  132. req->status = SCLP_REQ_RUNNING;
  133. sclp_running_state = sclp_running_state_running;
  134. __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
  135. sclp_request_timeout, 1);
  136. return 0;
  137. } else if (rc == -EBUSY) {
  138. /* Try again later */
  139. __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
  140. sclp_request_timeout, 0);
  141. return 0;
  142. }
  143. /* Request failed */
  144. req->status = SCLP_REQ_FAILED;
  145. return rc;
  146. }
  147. /* Try to start queued requests. */
  148. static void
  149. sclp_process_queue(void)
  150. {
  151. struct sclp_req *req;
  152. int rc;
  153. unsigned long flags;
  154. spin_lock_irqsave(&sclp_lock, flags);
  155. if (sclp_running_state != sclp_running_state_idle) {
  156. spin_unlock_irqrestore(&sclp_lock, flags);
  157. return;
  158. }
  159. del_timer(&sclp_request_timer);
  160. while (!list_empty(&sclp_req_queue)) {
  161. req = list_entry(sclp_req_queue.next, struct sclp_req, list);
  162. rc = __sclp_start_request(req);
  163. if (rc == 0)
  164. break;
  165. /* Request failed. */
  166. list_del(&req->list);
  167. if (req->callback) {
  168. spin_unlock_irqrestore(&sclp_lock, flags);
  169. req->callback(req, req->callback_data);
  170. spin_lock_irqsave(&sclp_lock, flags);
  171. }
  172. }
  173. spin_unlock_irqrestore(&sclp_lock, flags);
  174. }
  175. /* Queue a new request. Return zero on success, non-zero otherwise. */
  176. int
  177. sclp_add_request(struct sclp_req *req)
  178. {
  179. unsigned long flags;
  180. int rc;
  181. spin_lock_irqsave(&sclp_lock, flags);
  182. if ((sclp_init_state != sclp_init_state_initialized ||
  183. sclp_activation_state != sclp_activation_state_active) &&
  184. req != &sclp_init_req) {
  185. spin_unlock_irqrestore(&sclp_lock, flags);
  186. return -EIO;
  187. }
  188. req->status = SCLP_REQ_QUEUED;
  189. req->start_count = 0;
  190. list_add_tail(&req->list, &sclp_req_queue);
  191. rc = 0;
  192. /* Start if request is first in list */
  193. if (req->list.prev == &sclp_req_queue) {
  194. rc = __sclp_start_request(req);
  195. if (rc)
  196. list_del(&req->list);
  197. }
  198. spin_unlock_irqrestore(&sclp_lock, flags);
  199. return rc;
  200. }
  201. EXPORT_SYMBOL(sclp_add_request);
  202. /* Dispatch events found in request buffer to registered listeners. Return 0
  203. * if all events were dispatched, non-zero otherwise. */
  204. static int
  205. sclp_dispatch_evbufs(struct sccb_header *sccb)
  206. {
  207. unsigned long flags;
  208. struct evbuf_header *evbuf;
  209. struct list_head *l;
  210. struct sclp_register *reg;
  211. int offset;
  212. int rc;
  213. spin_lock_irqsave(&sclp_lock, flags);
  214. rc = 0;
  215. for (offset = sizeof(struct sccb_header); offset < sccb->length;
  216. offset += evbuf->length) {
  217. /* Search for event handler */
  218. evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
  219. reg = NULL;
  220. list_for_each(l, &sclp_reg_list) {
  221. reg = list_entry(l, struct sclp_register, list);
  222. if (reg->receive_mask & (1 << (32 - evbuf->type)))
  223. break;
  224. else
  225. reg = NULL;
  226. }
  227. if (reg && reg->receiver_fn) {
  228. spin_unlock_irqrestore(&sclp_lock, flags);
  229. reg->receiver_fn(evbuf);
  230. spin_lock_irqsave(&sclp_lock, flags);
  231. } else if (reg == NULL)
  232. rc = -ENOSYS;
  233. }
  234. spin_unlock_irqrestore(&sclp_lock, flags);
  235. return rc;
  236. }
  237. /* Read event data request callback. */
  238. static void
  239. sclp_read_cb(struct sclp_req *req, void *data)
  240. {
  241. unsigned long flags;
  242. struct sccb_header *sccb;
  243. sccb = (struct sccb_header *) req->sccb;
  244. if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
  245. sccb->response_code == 0x220))
  246. sclp_dispatch_evbufs(sccb);
  247. spin_lock_irqsave(&sclp_lock, flags);
  248. sclp_reading_state = sclp_reading_state_idle;
  249. spin_unlock_irqrestore(&sclp_lock, flags);
  250. }
  251. /* Prepare read event data request. Called while sclp_lock is locked. */
  252. static inline void
  253. __sclp_make_read_req(void)
  254. {
  255. struct sccb_header *sccb;
  256. sccb = (struct sccb_header *) sclp_read_sccb;
  257. clear_page(sccb);
  258. memset(&sclp_read_req, 0, sizeof(struct sclp_req));
  259. sclp_read_req.command = SCLP_CMDW_READDATA;
  260. sclp_read_req.status = SCLP_REQ_QUEUED;
  261. sclp_read_req.start_count = 0;
  262. sclp_read_req.callback = sclp_read_cb;
  263. sclp_read_req.sccb = sccb;
  264. sccb->length = PAGE_SIZE;
  265. sccb->function_code = 0;
  266. sccb->control_mask[2] = 0x80;
  267. }
  268. /* Search request list for request with matching sccb. Return request if found,
  269. * NULL otherwise. Called while sclp_lock is locked. */
  270. static inline struct sclp_req *
  271. __sclp_find_req(u32 sccb)
  272. {
  273. struct list_head *l;
  274. struct sclp_req *req;
  275. list_for_each(l, &sclp_req_queue) {
  276. req = list_entry(l, struct sclp_req, list);
  277. if (sccb == (u32) (addr_t) req->sccb)
  278. return req;
  279. }
  280. return NULL;
  281. }
  282. /* Handler for external interruption. Perform request post-processing.
  283. * Prepare read event data request if necessary. Start processing of next
  284. * request on queue. */
  285. static void
  286. sclp_interrupt_handler(__u16 code)
  287. {
  288. struct sclp_req *req;
  289. u32 finished_sccb;
  290. u32 evbuf_pending;
  291. spin_lock(&sclp_lock);
  292. finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
  293. evbuf_pending = S390_lowcore.ext_params & 0x3;
  294. if (finished_sccb) {
  295. req = __sclp_find_req(finished_sccb);
  296. if (req) {
  297. /* Request post-processing */
  298. list_del(&req->list);
  299. req->status = SCLP_REQ_DONE;
  300. if (req->callback) {
  301. spin_unlock(&sclp_lock);
  302. req->callback(req, req->callback_data);
  303. spin_lock(&sclp_lock);
  304. }
  305. }
  306. sclp_running_state = sclp_running_state_idle;
  307. }
  308. if (evbuf_pending && sclp_receive_mask != 0 &&
  309. sclp_reading_state == sclp_reading_state_idle &&
  310. sclp_activation_state == sclp_activation_state_active ) {
  311. sclp_reading_state = sclp_reading_state_reading;
  312. __sclp_make_read_req();
  313. /* Add request to head of queue */
  314. list_add(&sclp_read_req.list, &sclp_req_queue);
  315. }
  316. spin_unlock(&sclp_lock);
  317. sclp_process_queue();
  318. }
  319. /* Convert interval in jiffies to TOD ticks. */
  320. static inline u64
  321. sclp_tod_from_jiffies(unsigned long jiffies)
  322. {
  323. return (u64) (jiffies / HZ) << 32;
  324. }
  325. /* Wait until a currently running request finished. Note: while this function
  326. * is running, no timers are served on the calling CPU. */
  327. void
  328. sclp_sync_wait(void)
  329. {
  330. unsigned long flags;
  331. unsigned long cr0, cr0_sync;
  332. u64 timeout;
  333. /* We'll be disabling timer interrupts, so we need a custom timeout
  334. * mechanism */
  335. timeout = 0;
  336. if (timer_pending(&sclp_request_timer)) {
  337. /* Get timeout TOD value */
  338. timeout = get_clock() +
  339. sclp_tod_from_jiffies(sclp_request_timer.expires -
  340. jiffies);
  341. }
  342. local_irq_save(flags);
  343. /* Prevent bottom half from executing once we force interrupts open */
  344. local_bh_disable();
  345. /* Enable service-signal interruption, disable timer interrupts */
  346. trace_hardirqs_on();
  347. __ctl_store(cr0, 0, 0);
  348. cr0_sync = cr0;
  349. cr0_sync |= 0x00000200;
  350. cr0_sync &= 0xFFFFF3AC;
  351. __ctl_load(cr0_sync, 0, 0);
  352. __raw_local_irq_stosm(0x01);
  353. /* Loop until driver state indicates finished request */
  354. while (sclp_running_state != sclp_running_state_idle) {
  355. /* Check for expired request timer */
  356. if (timer_pending(&sclp_request_timer) &&
  357. get_clock() > timeout &&
  358. del_timer(&sclp_request_timer))
  359. sclp_request_timer.function(sclp_request_timer.data);
  360. barrier();
  361. cpu_relax();
  362. }
  363. local_irq_disable();
  364. __ctl_load(cr0, 0, 0);
  365. _local_bh_enable();
  366. local_irq_restore(flags);
  367. }
  368. EXPORT_SYMBOL(sclp_sync_wait);
  369. /* Dispatch changes in send and receive mask to registered listeners. */
  370. static inline void
  371. sclp_dispatch_state_change(void)
  372. {
  373. struct list_head *l;
  374. struct sclp_register *reg;
  375. unsigned long flags;
  376. sccb_mask_t receive_mask;
  377. sccb_mask_t send_mask;
  378. do {
  379. spin_lock_irqsave(&sclp_lock, flags);
  380. reg = NULL;
  381. list_for_each(l, &sclp_reg_list) {
  382. reg = list_entry(l, struct sclp_register, list);
  383. receive_mask = reg->receive_mask & sclp_receive_mask;
  384. send_mask = reg->send_mask & sclp_send_mask;
  385. if (reg->sclp_receive_mask != receive_mask ||
  386. reg->sclp_send_mask != send_mask) {
  387. reg->sclp_receive_mask = receive_mask;
  388. reg->sclp_send_mask = send_mask;
  389. break;
  390. } else
  391. reg = NULL;
  392. }
  393. spin_unlock_irqrestore(&sclp_lock, flags);
  394. if (reg && reg->state_change_fn)
  395. reg->state_change_fn(reg);
  396. } while (reg);
  397. }
  398. struct sclp_statechangebuf {
  399. struct evbuf_header header;
  400. u8 validity_sclp_active_facility_mask : 1;
  401. u8 validity_sclp_receive_mask : 1;
  402. u8 validity_sclp_send_mask : 1;
  403. u8 validity_read_data_function_mask : 1;
  404. u16 _zeros : 12;
  405. u16 mask_length;
  406. u64 sclp_active_facility_mask;
  407. sccb_mask_t sclp_receive_mask;
  408. sccb_mask_t sclp_send_mask;
  409. u32 read_data_function_mask;
  410. } __attribute__((packed));
  411. /* State change event callback. Inform listeners of changes. */
  412. static void
  413. sclp_state_change_cb(struct evbuf_header *evbuf)
  414. {
  415. unsigned long flags;
  416. struct sclp_statechangebuf *scbuf;
  417. scbuf = (struct sclp_statechangebuf *) evbuf;
  418. if (scbuf->mask_length != sizeof(sccb_mask_t))
  419. return;
  420. spin_lock_irqsave(&sclp_lock, flags);
  421. if (scbuf->validity_sclp_receive_mask)
  422. sclp_receive_mask = scbuf->sclp_receive_mask;
  423. if (scbuf->validity_sclp_send_mask)
  424. sclp_send_mask = scbuf->sclp_send_mask;
  425. spin_unlock_irqrestore(&sclp_lock, flags);
  426. sclp_dispatch_state_change();
  427. }
  428. static struct sclp_register sclp_state_change_event = {
  429. .receive_mask = EvTyp_StateChange_Mask,
  430. .receiver_fn = sclp_state_change_cb
  431. };
  432. /* Calculate receive and send mask of currently registered listeners.
  433. * Called while sclp_lock is locked. */
  434. static inline void
  435. __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
  436. {
  437. struct list_head *l;
  438. struct sclp_register *t;
  439. *receive_mask = 0;
  440. *send_mask = 0;
  441. list_for_each(l, &sclp_reg_list) {
  442. t = list_entry(l, struct sclp_register, list);
  443. *receive_mask |= t->receive_mask;
  444. *send_mask |= t->send_mask;
  445. }
  446. }
  447. /* Register event listener. Return 0 on success, non-zero otherwise. */
  448. int
  449. sclp_register(struct sclp_register *reg)
  450. {
  451. unsigned long flags;
  452. sccb_mask_t receive_mask;
  453. sccb_mask_t send_mask;
  454. int rc;
  455. rc = sclp_init();
  456. if (rc)
  457. return rc;
  458. spin_lock_irqsave(&sclp_lock, flags);
  459. /* Check event mask for collisions */
  460. __sclp_get_mask(&receive_mask, &send_mask);
  461. if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
  462. spin_unlock_irqrestore(&sclp_lock, flags);
  463. return -EBUSY;
  464. }
  465. /* Trigger initial state change callback */
  466. reg->sclp_receive_mask = 0;
  467. reg->sclp_send_mask = 0;
  468. list_add(&reg->list, &sclp_reg_list);
  469. spin_unlock_irqrestore(&sclp_lock, flags);
  470. rc = sclp_init_mask(1);
  471. if (rc) {
  472. spin_lock_irqsave(&sclp_lock, flags);
  473. list_del(&reg->list);
  474. spin_unlock_irqrestore(&sclp_lock, flags);
  475. }
  476. return rc;
  477. }
  478. EXPORT_SYMBOL(sclp_register);
  479. /* Unregister event listener. */
  480. void
  481. sclp_unregister(struct sclp_register *reg)
  482. {
  483. unsigned long flags;
  484. spin_lock_irqsave(&sclp_lock, flags);
  485. list_del(&reg->list);
  486. spin_unlock_irqrestore(&sclp_lock, flags);
  487. sclp_init_mask(1);
  488. }
  489. EXPORT_SYMBOL(sclp_unregister);
  490. /* Remove event buffers which are marked processed. Return the number of
  491. * remaining event buffers. */
  492. int
  493. sclp_remove_processed(struct sccb_header *sccb)
  494. {
  495. struct evbuf_header *evbuf;
  496. int unprocessed;
  497. u16 remaining;
  498. evbuf = (struct evbuf_header *) (sccb + 1);
  499. unprocessed = 0;
  500. remaining = sccb->length - sizeof(struct sccb_header);
  501. while (remaining > 0) {
  502. remaining -= evbuf->length;
  503. if (evbuf->flags & 0x80) {
  504. sccb->length -= evbuf->length;
  505. memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
  506. remaining);
  507. } else {
  508. unprocessed++;
  509. evbuf = (struct evbuf_header *)
  510. ((addr_t) evbuf + evbuf->length);
  511. }
  512. }
  513. return unprocessed;
  514. }
  515. EXPORT_SYMBOL(sclp_remove_processed);
  516. struct init_sccb {
  517. struct sccb_header header;
  518. u16 _reserved;
  519. u16 mask_length;
  520. sccb_mask_t receive_mask;
  521. sccb_mask_t send_mask;
  522. sccb_mask_t sclp_send_mask;
  523. sccb_mask_t sclp_receive_mask;
  524. } __attribute__((packed));
  525. /* Prepare init mask request. Called while sclp_lock is locked. */
  526. static inline void
  527. __sclp_make_init_req(u32 receive_mask, u32 send_mask)
  528. {
  529. struct init_sccb *sccb;
  530. sccb = (struct init_sccb *) sclp_init_sccb;
  531. clear_page(sccb);
  532. memset(&sclp_init_req, 0, sizeof(struct sclp_req));
  533. sclp_init_req.command = SCLP_CMDW_WRITEMASK;
  534. sclp_init_req.status = SCLP_REQ_FILLED;
  535. sclp_init_req.start_count = 0;
  536. sclp_init_req.callback = NULL;
  537. sclp_init_req.callback_data = NULL;
  538. sclp_init_req.sccb = sccb;
  539. sccb->header.length = sizeof(struct init_sccb);
  540. sccb->mask_length = sizeof(sccb_mask_t);
  541. sccb->receive_mask = receive_mask;
  542. sccb->send_mask = send_mask;
  543. sccb->sclp_receive_mask = 0;
  544. sccb->sclp_send_mask = 0;
  545. }
  546. /* Start init mask request. If calculate is non-zero, calculate the mask as
  547. * requested by registered listeners. Use zero mask otherwise. Return 0 on
  548. * success, non-zero otherwise. */
  549. static int
  550. sclp_init_mask(int calculate)
  551. {
  552. unsigned long flags;
  553. struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
  554. sccb_mask_t receive_mask;
  555. sccb_mask_t send_mask;
  556. int retry;
  557. int rc;
  558. unsigned long wait;
  559. spin_lock_irqsave(&sclp_lock, flags);
  560. /* Check if interface is in appropriate state */
  561. if (sclp_mask_state != sclp_mask_state_idle) {
  562. spin_unlock_irqrestore(&sclp_lock, flags);
  563. return -EBUSY;
  564. }
  565. if (sclp_activation_state == sclp_activation_state_inactive) {
  566. spin_unlock_irqrestore(&sclp_lock, flags);
  567. return -EINVAL;
  568. }
  569. sclp_mask_state = sclp_mask_state_initializing;
  570. /* Determine mask */
  571. if (calculate)
  572. __sclp_get_mask(&receive_mask, &send_mask);
  573. else {
  574. receive_mask = 0;
  575. send_mask = 0;
  576. }
  577. rc = -EIO;
  578. for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
  579. /* Prepare request */
  580. __sclp_make_init_req(receive_mask, send_mask);
  581. spin_unlock_irqrestore(&sclp_lock, flags);
  582. if (sclp_add_request(&sclp_init_req)) {
  583. /* Try again later */
  584. wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
  585. while (time_before(jiffies, wait))
  586. sclp_sync_wait();
  587. spin_lock_irqsave(&sclp_lock, flags);
  588. continue;
  589. }
  590. while (sclp_init_req.status != SCLP_REQ_DONE &&
  591. sclp_init_req.status != SCLP_REQ_FAILED)
  592. sclp_sync_wait();
  593. spin_lock_irqsave(&sclp_lock, flags);
  594. if (sclp_init_req.status == SCLP_REQ_DONE &&
  595. sccb->header.response_code == 0x20) {
  596. /* Successful request */
  597. if (calculate) {
  598. sclp_receive_mask = sccb->sclp_receive_mask;
  599. sclp_send_mask = sccb->sclp_send_mask;
  600. } else {
  601. sclp_receive_mask = 0;
  602. sclp_send_mask = 0;
  603. }
  604. spin_unlock_irqrestore(&sclp_lock, flags);
  605. sclp_dispatch_state_change();
  606. spin_lock_irqsave(&sclp_lock, flags);
  607. rc = 0;
  608. break;
  609. }
  610. }
  611. sclp_mask_state = sclp_mask_state_idle;
  612. spin_unlock_irqrestore(&sclp_lock, flags);
  613. return rc;
  614. }
  615. /* Deactivate SCLP interface. On success, new requests will be rejected,
  616. * events will no longer be dispatched. Return 0 on success, non-zero
  617. * otherwise. */
  618. int
  619. sclp_deactivate(void)
  620. {
  621. unsigned long flags;
  622. int rc;
  623. spin_lock_irqsave(&sclp_lock, flags);
  624. /* Deactivate can only be called when active */
  625. if (sclp_activation_state != sclp_activation_state_active) {
  626. spin_unlock_irqrestore(&sclp_lock, flags);
  627. return -EINVAL;
  628. }
  629. sclp_activation_state = sclp_activation_state_deactivating;
  630. spin_unlock_irqrestore(&sclp_lock, flags);
  631. rc = sclp_init_mask(0);
  632. spin_lock_irqsave(&sclp_lock, flags);
  633. if (rc == 0)
  634. sclp_activation_state = sclp_activation_state_inactive;
  635. else
  636. sclp_activation_state = sclp_activation_state_active;
  637. spin_unlock_irqrestore(&sclp_lock, flags);
  638. return rc;
  639. }
  640. EXPORT_SYMBOL(sclp_deactivate);
  641. /* Reactivate SCLP interface after sclp_deactivate. On success, new
  642. * requests will be accepted, events will be dispatched again. Return 0 on
  643. * success, non-zero otherwise. */
  644. int
  645. sclp_reactivate(void)
  646. {
  647. unsigned long flags;
  648. int rc;
  649. spin_lock_irqsave(&sclp_lock, flags);
  650. /* Reactivate can only be called when inactive */
  651. if (sclp_activation_state != sclp_activation_state_inactive) {
  652. spin_unlock_irqrestore(&sclp_lock, flags);
  653. return -EINVAL;
  654. }
  655. sclp_activation_state = sclp_activation_state_activating;
  656. spin_unlock_irqrestore(&sclp_lock, flags);
  657. rc = sclp_init_mask(1);
  658. spin_lock_irqsave(&sclp_lock, flags);
  659. if (rc == 0)
  660. sclp_activation_state = sclp_activation_state_active;
  661. else
  662. sclp_activation_state = sclp_activation_state_inactive;
  663. spin_unlock_irqrestore(&sclp_lock, flags);
  664. return rc;
  665. }
  666. EXPORT_SYMBOL(sclp_reactivate);
  667. /* Handler for external interruption used during initialization. Modify
  668. * request state to done. */
  669. static void
  670. sclp_check_handler(__u16 code)
  671. {
  672. u32 finished_sccb;
  673. finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
  674. /* Is this the interrupt we are waiting for? */
  675. if (finished_sccb == 0)
  676. return;
  677. if (finished_sccb != (u32) (addr_t) sclp_init_sccb) {
  678. printk(KERN_WARNING SCLP_HEADER "unsolicited interrupt "
  679. "for buffer at 0x%x\n", finished_sccb);
  680. return;
  681. }
  682. spin_lock(&sclp_lock);
  683. if (sclp_running_state == sclp_running_state_running) {
  684. sclp_init_req.status = SCLP_REQ_DONE;
  685. sclp_running_state = sclp_running_state_idle;
  686. }
  687. spin_unlock(&sclp_lock);
  688. }
  689. /* Initial init mask request timed out. Modify request state to failed. */
  690. static void
  691. sclp_check_timeout(unsigned long data)
  692. {
  693. unsigned long flags;
  694. spin_lock_irqsave(&sclp_lock, flags);
  695. if (sclp_running_state == sclp_running_state_running) {
  696. sclp_init_req.status = SCLP_REQ_FAILED;
  697. sclp_running_state = sclp_running_state_idle;
  698. }
  699. spin_unlock_irqrestore(&sclp_lock, flags);
  700. }
  701. /* Perform a check of the SCLP interface. Return zero if the interface is
  702. * available and there are no pending requests from a previous instance.
  703. * Return non-zero otherwise. */
  704. static int
  705. sclp_check_interface(void)
  706. {
  707. struct init_sccb *sccb;
  708. unsigned long flags;
  709. int retry;
  710. int rc;
  711. spin_lock_irqsave(&sclp_lock, flags);
  712. /* Prepare init mask command */
  713. rc = register_early_external_interrupt(0x2401, sclp_check_handler,
  714. &ext_int_info_hwc);
  715. if (rc) {
  716. spin_unlock_irqrestore(&sclp_lock, flags);
  717. return rc;
  718. }
  719. for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
  720. __sclp_make_init_req(0, 0);
  721. sccb = (struct init_sccb *) sclp_init_req.sccb;
  722. rc = service_call(sclp_init_req.command, sccb);
  723. if (rc == -EIO)
  724. break;
  725. sclp_init_req.status = SCLP_REQ_RUNNING;
  726. sclp_running_state = sclp_running_state_running;
  727. __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
  728. sclp_check_timeout, 0);
  729. spin_unlock_irqrestore(&sclp_lock, flags);
  730. /* Enable service-signal interruption - needs to happen
  731. * with IRQs enabled. */
  732. ctl_set_bit(0, 9);
  733. /* Wait for signal from interrupt or timeout */
  734. sclp_sync_wait();
  735. /* Disable service-signal interruption - needs to happen
  736. * with IRQs enabled. */
  737. ctl_clear_bit(0,9);
  738. spin_lock_irqsave(&sclp_lock, flags);
  739. del_timer(&sclp_request_timer);
  740. if (sclp_init_req.status == SCLP_REQ_DONE &&
  741. sccb->header.response_code == 0x20) {
  742. rc = 0;
  743. break;
  744. } else
  745. rc = -EBUSY;
  746. }
  747. unregister_early_external_interrupt(0x2401, sclp_check_handler,
  748. &ext_int_info_hwc);
  749. spin_unlock_irqrestore(&sclp_lock, flags);
  750. return rc;
  751. }
  752. /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
  753. * events from interfering with rebooted system. */
  754. static int
  755. sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
  756. {
  757. sclp_deactivate();
  758. return NOTIFY_DONE;
  759. }
  760. static struct notifier_block sclp_reboot_notifier = {
  761. .notifier_call = sclp_reboot_event
  762. };
  763. /* Initialize SCLP driver. Return zero if driver is operational, non-zero
  764. * otherwise. */
  765. static int
  766. sclp_init(void)
  767. {
  768. unsigned long flags;
  769. int rc;
  770. if (!MACHINE_HAS_SCLP)
  771. return -ENODEV;
  772. spin_lock_irqsave(&sclp_lock, flags);
  773. /* Check for previous or running initialization */
  774. if (sclp_init_state != sclp_init_state_uninitialized) {
  775. spin_unlock_irqrestore(&sclp_lock, flags);
  776. return 0;
  777. }
  778. sclp_init_state = sclp_init_state_initializing;
  779. /* Set up variables */
  780. INIT_LIST_HEAD(&sclp_req_queue);
  781. INIT_LIST_HEAD(&sclp_reg_list);
  782. list_add(&sclp_state_change_event.list, &sclp_reg_list);
  783. init_timer(&sclp_request_timer);
  784. /* Check interface */
  785. spin_unlock_irqrestore(&sclp_lock, flags);
  786. rc = sclp_check_interface();
  787. spin_lock_irqsave(&sclp_lock, flags);
  788. if (rc) {
  789. sclp_init_state = sclp_init_state_uninitialized;
  790. spin_unlock_irqrestore(&sclp_lock, flags);
  791. return rc;
  792. }
  793. /* Register reboot handler */
  794. rc = register_reboot_notifier(&sclp_reboot_notifier);
  795. if (rc) {
  796. sclp_init_state = sclp_init_state_uninitialized;
  797. spin_unlock_irqrestore(&sclp_lock, flags);
  798. return rc;
  799. }
  800. /* Register interrupt handler */
  801. rc = register_early_external_interrupt(0x2401, sclp_interrupt_handler,
  802. &ext_int_info_hwc);
  803. if (rc) {
  804. unregister_reboot_notifier(&sclp_reboot_notifier);
  805. sclp_init_state = sclp_init_state_uninitialized;
  806. spin_unlock_irqrestore(&sclp_lock, flags);
  807. return rc;
  808. }
  809. sclp_init_state = sclp_init_state_initialized;
  810. spin_unlock_irqrestore(&sclp_lock, flags);
  811. /* Enable service-signal external interruption - needs to happen with
  812. * IRQs enabled. */
  813. ctl_set_bit(0, 9);
  814. sclp_init_mask(1);
  815. return 0;
  816. }