sclp.c 25 KB

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