sclp.c 25 KB

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