sclp.c 26 KB

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