sclp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
  248. /* Check for malformed hardware response */
  249. if (evbuf->length == 0)
  250. break;
  251. /* Search for event handler */
  252. reg = NULL;
  253. list_for_each(l, &sclp_reg_list) {
  254. reg = list_entry(l, struct sclp_register, list);
  255. if (reg->receive_mask & (1 << (32 - evbuf->type)))
  256. break;
  257. else
  258. reg = NULL;
  259. }
  260. if (reg && reg->receiver_fn) {
  261. spin_unlock_irqrestore(&sclp_lock, flags);
  262. reg->receiver_fn(evbuf);
  263. spin_lock_irqsave(&sclp_lock, flags);
  264. } else if (reg == NULL)
  265. rc = -ENOSYS;
  266. }
  267. spin_unlock_irqrestore(&sclp_lock, flags);
  268. return rc;
  269. }
  270. /* Read event data request callback. */
  271. static void
  272. sclp_read_cb(struct sclp_req *req, void *data)
  273. {
  274. unsigned long flags;
  275. struct sccb_header *sccb;
  276. sccb = (struct sccb_header *) req->sccb;
  277. if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
  278. sccb->response_code == 0x220))
  279. sclp_dispatch_evbufs(sccb);
  280. spin_lock_irqsave(&sclp_lock, flags);
  281. sclp_reading_state = sclp_reading_state_idle;
  282. spin_unlock_irqrestore(&sclp_lock, flags);
  283. }
  284. /* Prepare read event data request. Called while sclp_lock is locked. */
  285. static void __sclp_make_read_req(void)
  286. {
  287. struct sccb_header *sccb;
  288. sccb = (struct sccb_header *) sclp_read_sccb;
  289. clear_page(sccb);
  290. memset(&sclp_read_req, 0, sizeof(struct sclp_req));
  291. sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
  292. sclp_read_req.status = SCLP_REQ_QUEUED;
  293. sclp_read_req.start_count = 0;
  294. sclp_read_req.callback = sclp_read_cb;
  295. sclp_read_req.sccb = sccb;
  296. sccb->length = PAGE_SIZE;
  297. sccb->function_code = 0;
  298. sccb->control_mask[2] = 0x80;
  299. }
  300. /* Search request list for request with matching sccb. Return request if found,
  301. * NULL otherwise. Called while sclp_lock is locked. */
  302. static inline struct sclp_req *
  303. __sclp_find_req(u32 sccb)
  304. {
  305. struct list_head *l;
  306. struct sclp_req *req;
  307. list_for_each(l, &sclp_req_queue) {
  308. req = list_entry(l, struct sclp_req, list);
  309. if (sccb == (u32) (addr_t) req->sccb)
  310. return req;
  311. }
  312. return NULL;
  313. }
  314. /* Handler for external interruption. Perform request post-processing.
  315. * Prepare read event data request if necessary. Start processing of next
  316. * request on queue. */
  317. static void
  318. sclp_interrupt_handler(__u16 code)
  319. {
  320. struct sclp_req *req;
  321. u32 finished_sccb;
  322. u32 evbuf_pending;
  323. spin_lock(&sclp_lock);
  324. finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
  325. evbuf_pending = S390_lowcore.ext_params & 0x3;
  326. if (finished_sccb) {
  327. del_timer(&sclp_request_timer);
  328. sclp_running_state = sclp_running_state_reset_pending;
  329. req = __sclp_find_req(finished_sccb);
  330. if (req) {
  331. /* Request post-processing */
  332. list_del(&req->list);
  333. req->status = SCLP_REQ_DONE;
  334. if (req->callback) {
  335. spin_unlock(&sclp_lock);
  336. req->callback(req, req->callback_data);
  337. spin_lock(&sclp_lock);
  338. }
  339. }
  340. sclp_running_state = sclp_running_state_idle;
  341. }
  342. if (evbuf_pending &&
  343. sclp_activation_state == sclp_activation_state_active)
  344. __sclp_queue_read_req();
  345. spin_unlock(&sclp_lock);
  346. sclp_process_queue();
  347. }
  348. /* Convert interval in jiffies to TOD ticks. */
  349. static inline u64
  350. sclp_tod_from_jiffies(unsigned long jiffies)
  351. {
  352. return (u64) (jiffies / HZ) << 32;
  353. }
  354. /* Wait until a currently running request finished. Note: while this function
  355. * is running, no timers are served on the calling CPU. */
  356. void
  357. sclp_sync_wait(void)
  358. {
  359. unsigned long long old_tick;
  360. unsigned long flags;
  361. unsigned long cr0, cr0_sync;
  362. u64 timeout;
  363. int irq_context;
  364. /* We'll be disabling timer interrupts, so we need a custom timeout
  365. * mechanism */
  366. timeout = 0;
  367. if (timer_pending(&sclp_request_timer)) {
  368. /* Get timeout TOD value */
  369. timeout = get_clock() +
  370. sclp_tod_from_jiffies(sclp_request_timer.expires -
  371. jiffies);
  372. }
  373. local_irq_save(flags);
  374. /* Prevent bottom half from executing once we force interrupts open */
  375. irq_context = in_interrupt();
  376. if (!irq_context)
  377. local_bh_disable();
  378. /* Enable service-signal interruption, disable timer interrupts */
  379. old_tick = local_tick_disable();
  380. trace_hardirqs_on();
  381. __ctl_store(cr0, 0, 0);
  382. cr0_sync = cr0;
  383. cr0_sync &= 0xffff00a0;
  384. cr0_sync |= 0x00000200;
  385. __ctl_load(cr0_sync, 0, 0);
  386. __raw_local_irq_stosm(0x01);
  387. /* Loop until driver state indicates finished request */
  388. while (sclp_running_state != sclp_running_state_idle) {
  389. /* Check for expired request timer */
  390. if (timer_pending(&sclp_request_timer) &&
  391. get_clock() > timeout &&
  392. del_timer(&sclp_request_timer))
  393. sclp_request_timer.function(sclp_request_timer.data);
  394. cpu_relax();
  395. }
  396. local_irq_disable();
  397. __ctl_load(cr0, 0, 0);
  398. if (!irq_context)
  399. _local_bh_enable();
  400. local_tick_enable(old_tick);
  401. local_irq_restore(flags);
  402. }
  403. EXPORT_SYMBOL(sclp_sync_wait);
  404. /* Dispatch changes in send and receive mask to registered listeners. */
  405. static void
  406. sclp_dispatch_state_change(void)
  407. {
  408. struct list_head *l;
  409. struct sclp_register *reg;
  410. unsigned long flags;
  411. sccb_mask_t receive_mask;
  412. sccb_mask_t send_mask;
  413. do {
  414. spin_lock_irqsave(&sclp_lock, flags);
  415. reg = NULL;
  416. list_for_each(l, &sclp_reg_list) {
  417. reg = list_entry(l, struct sclp_register, list);
  418. receive_mask = reg->send_mask & sclp_receive_mask;
  419. send_mask = reg->receive_mask & sclp_send_mask;
  420. if (reg->sclp_receive_mask != receive_mask ||
  421. reg->sclp_send_mask != send_mask) {
  422. reg->sclp_receive_mask = receive_mask;
  423. reg->sclp_send_mask = send_mask;
  424. break;
  425. } else
  426. reg = NULL;
  427. }
  428. spin_unlock_irqrestore(&sclp_lock, flags);
  429. if (reg && reg->state_change_fn)
  430. reg->state_change_fn(reg);
  431. } while (reg);
  432. }
  433. struct sclp_statechangebuf {
  434. struct evbuf_header header;
  435. u8 validity_sclp_active_facility_mask : 1;
  436. u8 validity_sclp_receive_mask : 1;
  437. u8 validity_sclp_send_mask : 1;
  438. u8 validity_read_data_function_mask : 1;
  439. u16 _zeros : 12;
  440. u16 mask_length;
  441. u64 sclp_active_facility_mask;
  442. sccb_mask_t sclp_receive_mask;
  443. sccb_mask_t sclp_send_mask;
  444. u32 read_data_function_mask;
  445. } __attribute__((packed));
  446. /* State change event callback. Inform listeners of changes. */
  447. static void
  448. sclp_state_change_cb(struct evbuf_header *evbuf)
  449. {
  450. unsigned long flags;
  451. struct sclp_statechangebuf *scbuf;
  452. scbuf = (struct sclp_statechangebuf *) evbuf;
  453. if (scbuf->mask_length != sizeof(sccb_mask_t))
  454. return;
  455. spin_lock_irqsave(&sclp_lock, flags);
  456. if (scbuf->validity_sclp_receive_mask)
  457. sclp_receive_mask = scbuf->sclp_receive_mask;
  458. if (scbuf->validity_sclp_send_mask)
  459. sclp_send_mask = scbuf->sclp_send_mask;
  460. spin_unlock_irqrestore(&sclp_lock, flags);
  461. if (scbuf->validity_sclp_active_facility_mask)
  462. sclp_facilities = scbuf->sclp_active_facility_mask;
  463. sclp_dispatch_state_change();
  464. }
  465. static struct sclp_register sclp_state_change_event = {
  466. .receive_mask = EVTYP_STATECHANGE_MASK,
  467. .receiver_fn = sclp_state_change_cb
  468. };
  469. /* Calculate receive and send mask of currently registered listeners.
  470. * Called while sclp_lock is locked. */
  471. static inline void
  472. __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
  473. {
  474. struct list_head *l;
  475. struct sclp_register *t;
  476. *receive_mask = 0;
  477. *send_mask = 0;
  478. list_for_each(l, &sclp_reg_list) {
  479. t = list_entry(l, struct sclp_register, list);
  480. *receive_mask |= t->receive_mask;
  481. *send_mask |= t->send_mask;
  482. }
  483. }
  484. /* Register event listener. Return 0 on success, non-zero otherwise. */
  485. int
  486. sclp_register(struct sclp_register *reg)
  487. {
  488. unsigned long flags;
  489. sccb_mask_t receive_mask;
  490. sccb_mask_t send_mask;
  491. int rc;
  492. rc = sclp_init();
  493. if (rc)
  494. return rc;
  495. spin_lock_irqsave(&sclp_lock, flags);
  496. /* Check event mask for collisions */
  497. __sclp_get_mask(&receive_mask, &send_mask);
  498. if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
  499. spin_unlock_irqrestore(&sclp_lock, flags);
  500. return -EBUSY;
  501. }
  502. /* Trigger initial state change callback */
  503. reg->sclp_receive_mask = 0;
  504. reg->sclp_send_mask = 0;
  505. list_add(&reg->list, &sclp_reg_list);
  506. spin_unlock_irqrestore(&sclp_lock, flags);
  507. rc = sclp_init_mask(1);
  508. if (rc) {
  509. spin_lock_irqsave(&sclp_lock, flags);
  510. list_del(&reg->list);
  511. spin_unlock_irqrestore(&sclp_lock, flags);
  512. }
  513. return rc;
  514. }
  515. EXPORT_SYMBOL(sclp_register);
  516. /* Unregister event listener. */
  517. void
  518. sclp_unregister(struct sclp_register *reg)
  519. {
  520. unsigned long flags;
  521. spin_lock_irqsave(&sclp_lock, flags);
  522. list_del(&reg->list);
  523. spin_unlock_irqrestore(&sclp_lock, flags);
  524. sclp_init_mask(1);
  525. }
  526. EXPORT_SYMBOL(sclp_unregister);
  527. /* Remove event buffers which are marked processed. Return the number of
  528. * remaining event buffers. */
  529. int
  530. sclp_remove_processed(struct sccb_header *sccb)
  531. {
  532. struct evbuf_header *evbuf;
  533. int unprocessed;
  534. u16 remaining;
  535. evbuf = (struct evbuf_header *) (sccb + 1);
  536. unprocessed = 0;
  537. remaining = sccb->length - sizeof(struct sccb_header);
  538. while (remaining > 0) {
  539. remaining -= evbuf->length;
  540. if (evbuf->flags & 0x80) {
  541. sccb->length -= evbuf->length;
  542. memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
  543. remaining);
  544. } else {
  545. unprocessed++;
  546. evbuf = (struct evbuf_header *)
  547. ((addr_t) evbuf + evbuf->length);
  548. }
  549. }
  550. return unprocessed;
  551. }
  552. EXPORT_SYMBOL(sclp_remove_processed);
  553. struct init_sccb {
  554. struct sccb_header header;
  555. u16 _reserved;
  556. u16 mask_length;
  557. sccb_mask_t receive_mask;
  558. sccb_mask_t send_mask;
  559. sccb_mask_t sclp_receive_mask;
  560. sccb_mask_t sclp_send_mask;
  561. } __attribute__((packed));
  562. /* Prepare init mask request. Called while sclp_lock is locked. */
  563. static inline void
  564. __sclp_make_init_req(u32 receive_mask, u32 send_mask)
  565. {
  566. struct init_sccb *sccb;
  567. sccb = (struct init_sccb *) sclp_init_sccb;
  568. clear_page(sccb);
  569. memset(&sclp_init_req, 0, sizeof(struct sclp_req));
  570. sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
  571. sclp_init_req.status = SCLP_REQ_FILLED;
  572. sclp_init_req.start_count = 0;
  573. sclp_init_req.callback = NULL;
  574. sclp_init_req.callback_data = NULL;
  575. sclp_init_req.sccb = sccb;
  576. sccb->header.length = sizeof(struct init_sccb);
  577. sccb->mask_length = sizeof(sccb_mask_t);
  578. sccb->receive_mask = receive_mask;
  579. sccb->send_mask = send_mask;
  580. sccb->sclp_receive_mask = 0;
  581. sccb->sclp_send_mask = 0;
  582. }
  583. /* Start init mask request. If calculate is non-zero, calculate the mask as
  584. * requested by registered listeners. Use zero mask otherwise. Return 0 on
  585. * success, non-zero otherwise. */
  586. static int
  587. sclp_init_mask(int calculate)
  588. {
  589. unsigned long flags;
  590. struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
  591. sccb_mask_t receive_mask;
  592. sccb_mask_t send_mask;
  593. int retry;
  594. int rc;
  595. unsigned long wait;
  596. spin_lock_irqsave(&sclp_lock, flags);
  597. /* Check if interface is in appropriate state */
  598. if (sclp_mask_state != sclp_mask_state_idle) {
  599. spin_unlock_irqrestore(&sclp_lock, flags);
  600. return -EBUSY;
  601. }
  602. if (sclp_activation_state == sclp_activation_state_inactive) {
  603. spin_unlock_irqrestore(&sclp_lock, flags);
  604. return -EINVAL;
  605. }
  606. sclp_mask_state = sclp_mask_state_initializing;
  607. /* Determine mask */
  608. if (calculate)
  609. __sclp_get_mask(&receive_mask, &send_mask);
  610. else {
  611. receive_mask = 0;
  612. send_mask = 0;
  613. }
  614. rc = -EIO;
  615. for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
  616. /* Prepare request */
  617. __sclp_make_init_req(receive_mask, send_mask);
  618. spin_unlock_irqrestore(&sclp_lock, flags);
  619. if (sclp_add_request(&sclp_init_req)) {
  620. /* Try again later */
  621. wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
  622. while (time_before(jiffies, wait))
  623. sclp_sync_wait();
  624. spin_lock_irqsave(&sclp_lock, flags);
  625. continue;
  626. }
  627. while (sclp_init_req.status != SCLP_REQ_DONE &&
  628. sclp_init_req.status != SCLP_REQ_FAILED)
  629. sclp_sync_wait();
  630. spin_lock_irqsave(&sclp_lock, flags);
  631. if (sclp_init_req.status == SCLP_REQ_DONE &&
  632. sccb->header.response_code == 0x20) {
  633. /* Successful request */
  634. if (calculate) {
  635. sclp_receive_mask = sccb->sclp_receive_mask;
  636. sclp_send_mask = sccb->sclp_send_mask;
  637. } else {
  638. sclp_receive_mask = 0;
  639. sclp_send_mask = 0;
  640. }
  641. spin_unlock_irqrestore(&sclp_lock, flags);
  642. sclp_dispatch_state_change();
  643. spin_lock_irqsave(&sclp_lock, flags);
  644. rc = 0;
  645. break;
  646. }
  647. }
  648. sclp_mask_state = sclp_mask_state_idle;
  649. spin_unlock_irqrestore(&sclp_lock, flags);
  650. return rc;
  651. }
  652. /* Deactivate SCLP interface. On success, new requests will be rejected,
  653. * events will no longer be dispatched. Return 0 on success, non-zero
  654. * otherwise. */
  655. int
  656. sclp_deactivate(void)
  657. {
  658. unsigned long flags;
  659. int rc;
  660. spin_lock_irqsave(&sclp_lock, flags);
  661. /* Deactivate can only be called when active */
  662. if (sclp_activation_state != sclp_activation_state_active) {
  663. spin_unlock_irqrestore(&sclp_lock, flags);
  664. return -EINVAL;
  665. }
  666. sclp_activation_state = sclp_activation_state_deactivating;
  667. spin_unlock_irqrestore(&sclp_lock, flags);
  668. rc = sclp_init_mask(0);
  669. spin_lock_irqsave(&sclp_lock, flags);
  670. if (rc == 0)
  671. sclp_activation_state = sclp_activation_state_inactive;
  672. else
  673. sclp_activation_state = sclp_activation_state_active;
  674. spin_unlock_irqrestore(&sclp_lock, flags);
  675. return rc;
  676. }
  677. EXPORT_SYMBOL(sclp_deactivate);
  678. /* Reactivate SCLP interface after sclp_deactivate. On success, new
  679. * requests will be accepted, events will be dispatched again. Return 0 on
  680. * success, non-zero otherwise. */
  681. int
  682. sclp_reactivate(void)
  683. {
  684. unsigned long flags;
  685. int rc;
  686. spin_lock_irqsave(&sclp_lock, flags);
  687. /* Reactivate can only be called when inactive */
  688. if (sclp_activation_state != sclp_activation_state_inactive) {
  689. spin_unlock_irqrestore(&sclp_lock, flags);
  690. return -EINVAL;
  691. }
  692. sclp_activation_state = sclp_activation_state_activating;
  693. spin_unlock_irqrestore(&sclp_lock, flags);
  694. rc = sclp_init_mask(1);
  695. spin_lock_irqsave(&sclp_lock, flags);
  696. if (rc == 0)
  697. sclp_activation_state = sclp_activation_state_active;
  698. else
  699. sclp_activation_state = sclp_activation_state_inactive;
  700. spin_unlock_irqrestore(&sclp_lock, flags);
  701. return rc;
  702. }
  703. EXPORT_SYMBOL(sclp_reactivate);
  704. /* Handler for external interruption used during initialization. Modify
  705. * request state to done. */
  706. static void
  707. sclp_check_handler(__u16 code)
  708. {
  709. u32 finished_sccb;
  710. finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
  711. /* Is this the interrupt we are waiting for? */
  712. if (finished_sccb == 0)
  713. return;
  714. if (finished_sccb != (u32) (addr_t) sclp_init_sccb)
  715. panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
  716. finished_sccb);
  717. spin_lock(&sclp_lock);
  718. if (sclp_running_state == sclp_running_state_running) {
  719. sclp_init_req.status = SCLP_REQ_DONE;
  720. sclp_running_state = sclp_running_state_idle;
  721. }
  722. spin_unlock(&sclp_lock);
  723. }
  724. /* Initial init mask request timed out. Modify request state to failed. */
  725. static void
  726. sclp_check_timeout(unsigned long data)
  727. {
  728. unsigned long flags;
  729. spin_lock_irqsave(&sclp_lock, flags);
  730. if (sclp_running_state == sclp_running_state_running) {
  731. sclp_init_req.status = SCLP_REQ_FAILED;
  732. sclp_running_state = sclp_running_state_idle;
  733. }
  734. spin_unlock_irqrestore(&sclp_lock, flags);
  735. }
  736. /* Perform a check of the SCLP interface. Return zero if the interface is
  737. * available and there are no pending requests from a previous instance.
  738. * Return non-zero otherwise. */
  739. static int
  740. sclp_check_interface(void)
  741. {
  742. struct init_sccb *sccb;
  743. unsigned long flags;
  744. int retry;
  745. int rc;
  746. spin_lock_irqsave(&sclp_lock, flags);
  747. /* Prepare init mask command */
  748. rc = register_early_external_interrupt(0x2401, sclp_check_handler,
  749. &ext_int_info_hwc);
  750. if (rc) {
  751. spin_unlock_irqrestore(&sclp_lock, flags);
  752. return rc;
  753. }
  754. for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
  755. __sclp_make_init_req(0, 0);
  756. sccb = (struct init_sccb *) sclp_init_req.sccb;
  757. rc = sclp_service_call(sclp_init_req.command, sccb);
  758. if (rc == -EIO)
  759. break;
  760. sclp_init_req.status = SCLP_REQ_RUNNING;
  761. sclp_running_state = sclp_running_state_running;
  762. __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
  763. sclp_check_timeout, 0);
  764. spin_unlock_irqrestore(&sclp_lock, flags);
  765. /* Enable service-signal interruption - needs to happen
  766. * with IRQs enabled. */
  767. ctl_set_bit(0, 9);
  768. /* Wait for signal from interrupt or timeout */
  769. sclp_sync_wait();
  770. /* Disable service-signal interruption - needs to happen
  771. * with IRQs enabled. */
  772. ctl_clear_bit(0,9);
  773. spin_lock_irqsave(&sclp_lock, flags);
  774. del_timer(&sclp_request_timer);
  775. if (sclp_init_req.status == SCLP_REQ_DONE &&
  776. sccb->header.response_code == 0x20) {
  777. rc = 0;
  778. break;
  779. } else
  780. rc = -EBUSY;
  781. }
  782. unregister_early_external_interrupt(0x2401, sclp_check_handler,
  783. &ext_int_info_hwc);
  784. spin_unlock_irqrestore(&sclp_lock, flags);
  785. return rc;
  786. }
  787. /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
  788. * events from interfering with rebooted system. */
  789. static int
  790. sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
  791. {
  792. sclp_deactivate();
  793. return NOTIFY_DONE;
  794. }
  795. static struct notifier_block sclp_reboot_notifier = {
  796. .notifier_call = sclp_reboot_event
  797. };
  798. /* Initialize SCLP driver. Return zero if driver is operational, non-zero
  799. * otherwise. */
  800. static int
  801. sclp_init(void)
  802. {
  803. unsigned long flags;
  804. int rc;
  805. spin_lock_irqsave(&sclp_lock, flags);
  806. /* Check for previous or running initialization */
  807. if (sclp_init_state != sclp_init_state_uninitialized) {
  808. spin_unlock_irqrestore(&sclp_lock, flags);
  809. return 0;
  810. }
  811. sclp_init_state = sclp_init_state_initializing;
  812. /* Set up variables */
  813. INIT_LIST_HEAD(&sclp_req_queue);
  814. INIT_LIST_HEAD(&sclp_reg_list);
  815. list_add(&sclp_state_change_event.list, &sclp_reg_list);
  816. init_timer(&sclp_request_timer);
  817. /* Check interface */
  818. spin_unlock_irqrestore(&sclp_lock, flags);
  819. rc = sclp_check_interface();
  820. spin_lock_irqsave(&sclp_lock, flags);
  821. if (rc) {
  822. sclp_init_state = sclp_init_state_uninitialized;
  823. spin_unlock_irqrestore(&sclp_lock, flags);
  824. return rc;
  825. }
  826. /* Register reboot handler */
  827. rc = register_reboot_notifier(&sclp_reboot_notifier);
  828. if (rc) {
  829. sclp_init_state = sclp_init_state_uninitialized;
  830. spin_unlock_irqrestore(&sclp_lock, flags);
  831. return rc;
  832. }
  833. /* Register interrupt handler */
  834. rc = register_early_external_interrupt(0x2401, sclp_interrupt_handler,
  835. &ext_int_info_hwc);
  836. if (rc) {
  837. unregister_reboot_notifier(&sclp_reboot_notifier);
  838. sclp_init_state = sclp_init_state_uninitialized;
  839. spin_unlock_irqrestore(&sclp_lock, flags);
  840. return rc;
  841. }
  842. sclp_init_state = sclp_init_state_initialized;
  843. spin_unlock_irqrestore(&sclp_lock, flags);
  844. /* Enable service-signal external interruption - needs to happen with
  845. * IRQs enabled. */
  846. ctl_set_bit(0, 9);
  847. sclp_init_mask(1);
  848. return 0;
  849. }
  850. static __init int sclp_initcall(void)
  851. {
  852. return sclp_init();
  853. }
  854. arch_initcall(sclp_initcall);