sclp.c 25 KB

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