sclp.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /*
  2. * core function to access sclp interface
  3. *
  4. * Copyright IBM Corp. 1999, 2009
  5. *
  6. * Author(s): Martin Peschke <mpeschke@de.ibm.com>
  7. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. */
  9. #include <linux/kernel_stat.h>
  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 <linux/suspend.h>
  19. #include <linux/completion.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/s390_ext.h>
  22. #include <asm/types.h>
  23. #include <asm/irq.h>
  24. #include "sclp.h"
  25. #define SCLP_HEADER "sclp: "
  26. /* Lock to protect internal data consistency. */
  27. static DEFINE_SPINLOCK(sclp_lock);
  28. /* Mask of events that we can send to the sclp interface. */
  29. static sccb_mask_t sclp_receive_mask;
  30. /* Mask of events that we can receive from the sclp interface. */
  31. static sccb_mask_t sclp_send_mask;
  32. /* List of registered event listeners and senders. */
  33. static struct list_head sclp_reg_list;
  34. /* List of queued requests. */
  35. static struct list_head sclp_req_queue;
  36. /* Data for read and and init requests. */
  37. static struct sclp_req sclp_read_req;
  38. static struct sclp_req sclp_init_req;
  39. static char sclp_read_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
  40. static char sclp_init_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
  41. /* Suspend request */
  42. static DECLARE_COMPLETION(sclp_request_queue_flushed);
  43. static void sclp_suspend_req_cb(struct sclp_req *req, void *data)
  44. {
  45. complete(&sclp_request_queue_flushed);
  46. }
  47. static struct sclp_req sclp_suspend_req;
  48. /* Timer for request retries. */
  49. static struct timer_list sclp_request_timer;
  50. /* Internal state: is the driver initialized? */
  51. static volatile enum sclp_init_state_t {
  52. sclp_init_state_uninitialized,
  53. sclp_init_state_initializing,
  54. sclp_init_state_initialized
  55. } sclp_init_state = sclp_init_state_uninitialized;
  56. /* Internal state: is a request active at the sclp? */
  57. static volatile enum sclp_running_state_t {
  58. sclp_running_state_idle,
  59. sclp_running_state_running,
  60. sclp_running_state_reset_pending
  61. } sclp_running_state = sclp_running_state_idle;
  62. /* Internal state: is a read request pending? */
  63. static volatile enum sclp_reading_state_t {
  64. sclp_reading_state_idle,
  65. sclp_reading_state_reading
  66. } sclp_reading_state = sclp_reading_state_idle;
  67. /* Internal state: is the driver currently serving requests? */
  68. static volatile enum sclp_activation_state_t {
  69. sclp_activation_state_active,
  70. sclp_activation_state_deactivating,
  71. sclp_activation_state_inactive,
  72. sclp_activation_state_activating
  73. } sclp_activation_state = sclp_activation_state_active;
  74. /* Internal state: is an init mask request pending? */
  75. static volatile enum sclp_mask_state_t {
  76. sclp_mask_state_idle,
  77. sclp_mask_state_initializing
  78. } sclp_mask_state = sclp_mask_state_idle;
  79. /* Internal state: is the driver suspended? */
  80. static enum sclp_suspend_state_t {
  81. sclp_suspend_state_running,
  82. sclp_suspend_state_suspended,
  83. } sclp_suspend_state = sclp_suspend_state_running;
  84. /* Maximum retry counts */
  85. #define SCLP_INIT_RETRY 3
  86. #define SCLP_MASK_RETRY 3
  87. /* Timeout intervals in seconds.*/
  88. #define SCLP_BUSY_INTERVAL 10
  89. #define SCLP_RETRY_INTERVAL 30
  90. static void sclp_process_queue(void);
  91. static void __sclp_make_read_req(void);
  92. static int sclp_init_mask(int calculate);
  93. static int sclp_init(void);
  94. /* Perform service call. Return 0 on success, non-zero otherwise. */
  95. int
  96. sclp_service_call(sclp_cmdw_t command, void *sccb)
  97. {
  98. int cc;
  99. asm volatile(
  100. " .insn rre,0xb2200000,%1,%2\n" /* servc %1,%2 */
  101. " ipm %0\n"
  102. " srl %0,28"
  103. : "=&d" (cc) : "d" (command), "a" (__pa(sccb))
  104. : "cc", "memory");
  105. if (cc == 3)
  106. return -EIO;
  107. if (cc == 2)
  108. return -EBUSY;
  109. return 0;
  110. }
  111. static void
  112. __sclp_queue_read_req(void)
  113. {
  114. if (sclp_reading_state == sclp_reading_state_idle) {
  115. sclp_reading_state = sclp_reading_state_reading;
  116. __sclp_make_read_req();
  117. /* Add request to head of queue */
  118. list_add(&sclp_read_req.list, &sclp_req_queue);
  119. }
  120. }
  121. /* Set up request retry timer. Called while sclp_lock is locked. */
  122. static inline void
  123. __sclp_set_request_timer(unsigned long time, void (*function)(unsigned long),
  124. unsigned long data)
  125. {
  126. del_timer(&sclp_request_timer);
  127. sclp_request_timer.function = function;
  128. sclp_request_timer.data = data;
  129. sclp_request_timer.expires = jiffies + time;
  130. add_timer(&sclp_request_timer);
  131. }
  132. /* Request timeout handler. Restart the request queue. If DATA is non-zero,
  133. * force restart of running request. */
  134. static void
  135. sclp_request_timeout(unsigned long data)
  136. {
  137. unsigned long flags;
  138. spin_lock_irqsave(&sclp_lock, flags);
  139. if (data) {
  140. if (sclp_running_state == sclp_running_state_running) {
  141. /* Break running state and queue NOP read event request
  142. * to get a defined interface state. */
  143. __sclp_queue_read_req();
  144. sclp_running_state = sclp_running_state_idle;
  145. }
  146. } else {
  147. __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
  148. sclp_request_timeout, 0);
  149. }
  150. spin_unlock_irqrestore(&sclp_lock, flags);
  151. sclp_process_queue();
  152. }
  153. /* Try to start a request. Return zero if the request was successfully
  154. * started or if it will be started at a later time. Return non-zero otherwise.
  155. * Called while sclp_lock is locked. */
  156. static int
  157. __sclp_start_request(struct sclp_req *req)
  158. {
  159. int rc;
  160. if (sclp_running_state != sclp_running_state_idle)
  161. return 0;
  162. del_timer(&sclp_request_timer);
  163. rc = sclp_service_call(req->command, req->sccb);
  164. req->start_count++;
  165. if (rc == 0) {
  166. /* Successfully started request */
  167. req->status = SCLP_REQ_RUNNING;
  168. sclp_running_state = sclp_running_state_running;
  169. __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
  170. sclp_request_timeout, 1);
  171. return 0;
  172. } else if (rc == -EBUSY) {
  173. /* Try again later */
  174. __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
  175. sclp_request_timeout, 0);
  176. return 0;
  177. }
  178. /* Request failed */
  179. req->status = SCLP_REQ_FAILED;
  180. return rc;
  181. }
  182. /* Try to start queued requests. */
  183. static void
  184. sclp_process_queue(void)
  185. {
  186. struct sclp_req *req;
  187. int rc;
  188. unsigned long flags;
  189. spin_lock_irqsave(&sclp_lock, flags);
  190. if (sclp_running_state != sclp_running_state_idle) {
  191. spin_unlock_irqrestore(&sclp_lock, flags);
  192. return;
  193. }
  194. del_timer(&sclp_request_timer);
  195. while (!list_empty(&sclp_req_queue)) {
  196. req = list_entry(sclp_req_queue.next, struct sclp_req, list);
  197. if (!req->sccb)
  198. goto do_post;
  199. rc = __sclp_start_request(req);
  200. if (rc == 0)
  201. break;
  202. /* Request failed */
  203. if (req->start_count > 1) {
  204. /* Cannot abort already submitted request - could still
  205. * be active at the SCLP */
  206. __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
  207. sclp_request_timeout, 0);
  208. break;
  209. }
  210. do_post:
  211. /* Post-processing for aborted request */
  212. list_del(&req->list);
  213. if (req->callback) {
  214. spin_unlock_irqrestore(&sclp_lock, flags);
  215. req->callback(req, req->callback_data);
  216. spin_lock_irqsave(&sclp_lock, flags);
  217. }
  218. }
  219. spin_unlock_irqrestore(&sclp_lock, flags);
  220. }
  221. static int __sclp_can_add_request(struct sclp_req *req)
  222. {
  223. if (req == &sclp_suspend_req || req == &sclp_init_req)
  224. return 1;
  225. if (sclp_suspend_state != sclp_suspend_state_running)
  226. return 0;
  227. if (sclp_init_state != sclp_init_state_initialized)
  228. return 0;
  229. if (sclp_activation_state != sclp_activation_state_active)
  230. return 0;
  231. return 1;
  232. }
  233. /* Queue a new request. Return zero on success, non-zero otherwise. */
  234. int
  235. sclp_add_request(struct sclp_req *req)
  236. {
  237. unsigned long flags;
  238. int rc;
  239. spin_lock_irqsave(&sclp_lock, flags);
  240. if (!__sclp_can_add_request(req)) {
  241. spin_unlock_irqrestore(&sclp_lock, flags);
  242. return -EIO;
  243. }
  244. req->status = SCLP_REQ_QUEUED;
  245. req->start_count = 0;
  246. list_add_tail(&req->list, &sclp_req_queue);
  247. rc = 0;
  248. /* Start if request is first in list */
  249. if (sclp_running_state == sclp_running_state_idle &&
  250. req->list.prev == &sclp_req_queue) {
  251. if (!req->sccb) {
  252. list_del(&req->list);
  253. rc = -ENODATA;
  254. goto out;
  255. }
  256. rc = __sclp_start_request(req);
  257. if (rc)
  258. list_del(&req->list);
  259. }
  260. out:
  261. spin_unlock_irqrestore(&sclp_lock, flags);
  262. return rc;
  263. }
  264. EXPORT_SYMBOL(sclp_add_request);
  265. /* Dispatch events found in request buffer to registered listeners. Return 0
  266. * if all events were dispatched, non-zero otherwise. */
  267. static int
  268. sclp_dispatch_evbufs(struct sccb_header *sccb)
  269. {
  270. unsigned long flags;
  271. struct evbuf_header *evbuf;
  272. struct list_head *l;
  273. struct sclp_register *reg;
  274. int offset;
  275. int rc;
  276. spin_lock_irqsave(&sclp_lock, flags);
  277. rc = 0;
  278. for (offset = sizeof(struct sccb_header); offset < sccb->length;
  279. offset += evbuf->length) {
  280. evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
  281. /* Check for malformed hardware response */
  282. if (evbuf->length == 0)
  283. break;
  284. /* Search for event handler */
  285. reg = NULL;
  286. list_for_each(l, &sclp_reg_list) {
  287. reg = list_entry(l, struct sclp_register, list);
  288. if (reg->receive_mask & (1 << (32 - evbuf->type)))
  289. break;
  290. else
  291. reg = NULL;
  292. }
  293. if (reg && reg->receiver_fn) {
  294. spin_unlock_irqrestore(&sclp_lock, flags);
  295. reg->receiver_fn(evbuf);
  296. spin_lock_irqsave(&sclp_lock, flags);
  297. } else if (reg == NULL)
  298. rc = -ENOSYS;
  299. }
  300. spin_unlock_irqrestore(&sclp_lock, flags);
  301. return rc;
  302. }
  303. /* Read event data request callback. */
  304. static void
  305. sclp_read_cb(struct sclp_req *req, void *data)
  306. {
  307. unsigned long flags;
  308. struct sccb_header *sccb;
  309. sccb = (struct sccb_header *) req->sccb;
  310. if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
  311. sccb->response_code == 0x220))
  312. sclp_dispatch_evbufs(sccb);
  313. spin_lock_irqsave(&sclp_lock, flags);
  314. sclp_reading_state = sclp_reading_state_idle;
  315. spin_unlock_irqrestore(&sclp_lock, flags);
  316. }
  317. /* Prepare read event data request. Called while sclp_lock is locked. */
  318. static void __sclp_make_read_req(void)
  319. {
  320. struct sccb_header *sccb;
  321. sccb = (struct sccb_header *) sclp_read_sccb;
  322. clear_page(sccb);
  323. memset(&sclp_read_req, 0, sizeof(struct sclp_req));
  324. sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
  325. sclp_read_req.status = SCLP_REQ_QUEUED;
  326. sclp_read_req.start_count = 0;
  327. sclp_read_req.callback = sclp_read_cb;
  328. sclp_read_req.sccb = sccb;
  329. sccb->length = PAGE_SIZE;
  330. sccb->function_code = 0;
  331. sccb->control_mask[2] = 0x80;
  332. }
  333. /* Search request list for request with matching sccb. Return request if found,
  334. * NULL otherwise. Called while sclp_lock is locked. */
  335. static inline struct sclp_req *
  336. __sclp_find_req(u32 sccb)
  337. {
  338. struct list_head *l;
  339. struct sclp_req *req;
  340. list_for_each(l, &sclp_req_queue) {
  341. req = list_entry(l, struct sclp_req, list);
  342. if (sccb == (u32) (addr_t) req->sccb)
  343. return req;
  344. }
  345. return NULL;
  346. }
  347. /* Handler for external interruption. Perform request post-processing.
  348. * Prepare read event data request if necessary. Start processing of next
  349. * request on queue. */
  350. static void sclp_interrupt_handler(unsigned int ext_int_code,
  351. unsigned int param32, unsigned long param64)
  352. {
  353. struct sclp_req *req;
  354. u32 finished_sccb;
  355. u32 evbuf_pending;
  356. kstat_cpu(smp_processor_id()).irqs[EXTINT_SCP]++;
  357. spin_lock(&sclp_lock);
  358. finished_sccb = param32 & 0xfffffff8;
  359. evbuf_pending = param32 & 0x3;
  360. if (finished_sccb) {
  361. del_timer(&sclp_request_timer);
  362. sclp_running_state = sclp_running_state_reset_pending;
  363. req = __sclp_find_req(finished_sccb);
  364. if (req) {
  365. /* Request post-processing */
  366. list_del(&req->list);
  367. req->status = SCLP_REQ_DONE;
  368. if (req->callback) {
  369. spin_unlock(&sclp_lock);
  370. req->callback(req, req->callback_data);
  371. spin_lock(&sclp_lock);
  372. }
  373. }
  374. sclp_running_state = sclp_running_state_idle;
  375. }
  376. if (evbuf_pending &&
  377. sclp_activation_state == sclp_activation_state_active)
  378. __sclp_queue_read_req();
  379. spin_unlock(&sclp_lock);
  380. sclp_process_queue();
  381. }
  382. /* Convert interval in jiffies to TOD ticks. */
  383. static inline u64
  384. sclp_tod_from_jiffies(unsigned long jiffies)
  385. {
  386. return (u64) (jiffies / HZ) << 32;
  387. }
  388. /* Wait until a currently running request finished. Note: while this function
  389. * is running, no timers are served on the calling CPU. */
  390. void
  391. sclp_sync_wait(void)
  392. {
  393. unsigned long long old_tick;
  394. unsigned long flags;
  395. unsigned long cr0, cr0_sync;
  396. u64 timeout;
  397. int irq_context;
  398. /* We'll be disabling timer interrupts, so we need a custom timeout
  399. * mechanism */
  400. timeout = 0;
  401. if (timer_pending(&sclp_request_timer)) {
  402. /* Get timeout TOD value */
  403. timeout = get_clock() +
  404. sclp_tod_from_jiffies(sclp_request_timer.expires -
  405. jiffies);
  406. }
  407. local_irq_save(flags);
  408. /* Prevent bottom half from executing once we force interrupts open */
  409. irq_context = in_interrupt();
  410. if (!irq_context)
  411. local_bh_disable();
  412. /* Enable service-signal interruption, disable timer interrupts */
  413. old_tick = local_tick_disable();
  414. trace_hardirqs_on();
  415. __ctl_store(cr0, 0, 0);
  416. cr0_sync = cr0;
  417. cr0_sync &= 0xffff00a0;
  418. cr0_sync |= 0x00000200;
  419. __ctl_load(cr0_sync, 0, 0);
  420. __arch_local_irq_stosm(0x01);
  421. /* Loop until driver state indicates finished request */
  422. while (sclp_running_state != sclp_running_state_idle) {
  423. /* Check for expired request timer */
  424. if (timer_pending(&sclp_request_timer) &&
  425. get_clock() > timeout &&
  426. del_timer(&sclp_request_timer))
  427. sclp_request_timer.function(sclp_request_timer.data);
  428. cpu_relax();
  429. }
  430. local_irq_disable();
  431. __ctl_load(cr0, 0, 0);
  432. if (!irq_context)
  433. _local_bh_enable();
  434. local_tick_enable(old_tick);
  435. local_irq_restore(flags);
  436. }
  437. EXPORT_SYMBOL(sclp_sync_wait);
  438. /* Dispatch changes in send and receive mask to registered listeners. */
  439. static void
  440. sclp_dispatch_state_change(void)
  441. {
  442. struct list_head *l;
  443. struct sclp_register *reg;
  444. unsigned long flags;
  445. sccb_mask_t receive_mask;
  446. sccb_mask_t send_mask;
  447. do {
  448. spin_lock_irqsave(&sclp_lock, flags);
  449. reg = NULL;
  450. list_for_each(l, &sclp_reg_list) {
  451. reg = list_entry(l, struct sclp_register, list);
  452. receive_mask = reg->send_mask & sclp_receive_mask;
  453. send_mask = reg->receive_mask & sclp_send_mask;
  454. if (reg->sclp_receive_mask != receive_mask ||
  455. reg->sclp_send_mask != send_mask) {
  456. reg->sclp_receive_mask = receive_mask;
  457. reg->sclp_send_mask = send_mask;
  458. break;
  459. } else
  460. reg = NULL;
  461. }
  462. spin_unlock_irqrestore(&sclp_lock, flags);
  463. if (reg && reg->state_change_fn)
  464. reg->state_change_fn(reg);
  465. } while (reg);
  466. }
  467. struct sclp_statechangebuf {
  468. struct evbuf_header header;
  469. u8 validity_sclp_active_facility_mask : 1;
  470. u8 validity_sclp_receive_mask : 1;
  471. u8 validity_sclp_send_mask : 1;
  472. u8 validity_read_data_function_mask : 1;
  473. u16 _zeros : 12;
  474. u16 mask_length;
  475. u64 sclp_active_facility_mask;
  476. sccb_mask_t sclp_receive_mask;
  477. sccb_mask_t sclp_send_mask;
  478. u32 read_data_function_mask;
  479. } __attribute__((packed));
  480. /* State change event callback. Inform listeners of changes. */
  481. static void
  482. sclp_state_change_cb(struct evbuf_header *evbuf)
  483. {
  484. unsigned long flags;
  485. struct sclp_statechangebuf *scbuf;
  486. scbuf = (struct sclp_statechangebuf *) evbuf;
  487. if (scbuf->mask_length != sizeof(sccb_mask_t))
  488. return;
  489. spin_lock_irqsave(&sclp_lock, flags);
  490. if (scbuf->validity_sclp_receive_mask)
  491. sclp_receive_mask = scbuf->sclp_receive_mask;
  492. if (scbuf->validity_sclp_send_mask)
  493. sclp_send_mask = scbuf->sclp_send_mask;
  494. spin_unlock_irqrestore(&sclp_lock, flags);
  495. if (scbuf->validity_sclp_active_facility_mask)
  496. sclp_facilities = scbuf->sclp_active_facility_mask;
  497. sclp_dispatch_state_change();
  498. }
  499. static struct sclp_register sclp_state_change_event = {
  500. .receive_mask = EVTYP_STATECHANGE_MASK,
  501. .receiver_fn = sclp_state_change_cb
  502. };
  503. /* Calculate receive and send mask of currently registered listeners.
  504. * Called while sclp_lock is locked. */
  505. static inline void
  506. __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
  507. {
  508. struct list_head *l;
  509. struct sclp_register *t;
  510. *receive_mask = 0;
  511. *send_mask = 0;
  512. list_for_each(l, &sclp_reg_list) {
  513. t = list_entry(l, struct sclp_register, list);
  514. *receive_mask |= t->receive_mask;
  515. *send_mask |= t->send_mask;
  516. }
  517. }
  518. /* Register event listener. Return 0 on success, non-zero otherwise. */
  519. int
  520. sclp_register(struct sclp_register *reg)
  521. {
  522. unsigned long flags;
  523. sccb_mask_t receive_mask;
  524. sccb_mask_t send_mask;
  525. int rc;
  526. rc = sclp_init();
  527. if (rc)
  528. return rc;
  529. spin_lock_irqsave(&sclp_lock, flags);
  530. /* Check event mask for collisions */
  531. __sclp_get_mask(&receive_mask, &send_mask);
  532. if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
  533. spin_unlock_irqrestore(&sclp_lock, flags);
  534. return -EBUSY;
  535. }
  536. /* Trigger initial state change callback */
  537. reg->sclp_receive_mask = 0;
  538. reg->sclp_send_mask = 0;
  539. reg->pm_event_posted = 0;
  540. list_add(&reg->list, &sclp_reg_list);
  541. spin_unlock_irqrestore(&sclp_lock, flags);
  542. rc = sclp_init_mask(1);
  543. if (rc) {
  544. spin_lock_irqsave(&sclp_lock, flags);
  545. list_del(&reg->list);
  546. spin_unlock_irqrestore(&sclp_lock, flags);
  547. }
  548. return rc;
  549. }
  550. EXPORT_SYMBOL(sclp_register);
  551. /* Unregister event listener. */
  552. void
  553. sclp_unregister(struct sclp_register *reg)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(&sclp_lock, flags);
  557. list_del(&reg->list);
  558. spin_unlock_irqrestore(&sclp_lock, flags);
  559. sclp_init_mask(1);
  560. }
  561. EXPORT_SYMBOL(sclp_unregister);
  562. /* Remove event buffers which are marked processed. Return the number of
  563. * remaining event buffers. */
  564. int
  565. sclp_remove_processed(struct sccb_header *sccb)
  566. {
  567. struct evbuf_header *evbuf;
  568. int unprocessed;
  569. u16 remaining;
  570. evbuf = (struct evbuf_header *) (sccb + 1);
  571. unprocessed = 0;
  572. remaining = sccb->length - sizeof(struct sccb_header);
  573. while (remaining > 0) {
  574. remaining -= evbuf->length;
  575. if (evbuf->flags & 0x80) {
  576. sccb->length -= evbuf->length;
  577. memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
  578. remaining);
  579. } else {
  580. unprocessed++;
  581. evbuf = (struct evbuf_header *)
  582. ((addr_t) evbuf + evbuf->length);
  583. }
  584. }
  585. return unprocessed;
  586. }
  587. EXPORT_SYMBOL(sclp_remove_processed);
  588. struct init_sccb {
  589. struct sccb_header header;
  590. u16 _reserved;
  591. u16 mask_length;
  592. sccb_mask_t receive_mask;
  593. sccb_mask_t send_mask;
  594. sccb_mask_t sclp_receive_mask;
  595. sccb_mask_t sclp_send_mask;
  596. } __attribute__((packed));
  597. /* Prepare init mask request. Called while sclp_lock is locked. */
  598. static inline void
  599. __sclp_make_init_req(u32 receive_mask, u32 send_mask)
  600. {
  601. struct init_sccb *sccb;
  602. sccb = (struct init_sccb *) sclp_init_sccb;
  603. clear_page(sccb);
  604. memset(&sclp_init_req, 0, sizeof(struct sclp_req));
  605. sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
  606. sclp_init_req.status = SCLP_REQ_FILLED;
  607. sclp_init_req.start_count = 0;
  608. sclp_init_req.callback = NULL;
  609. sclp_init_req.callback_data = NULL;
  610. sclp_init_req.sccb = sccb;
  611. sccb->header.length = sizeof(struct init_sccb);
  612. sccb->mask_length = sizeof(sccb_mask_t);
  613. sccb->receive_mask = receive_mask;
  614. sccb->send_mask = send_mask;
  615. sccb->sclp_receive_mask = 0;
  616. sccb->sclp_send_mask = 0;
  617. }
  618. /* Start init mask request. If calculate is non-zero, calculate the mask as
  619. * requested by registered listeners. Use zero mask otherwise. Return 0 on
  620. * success, non-zero otherwise. */
  621. static int
  622. sclp_init_mask(int calculate)
  623. {
  624. unsigned long flags;
  625. struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
  626. sccb_mask_t receive_mask;
  627. sccb_mask_t send_mask;
  628. int retry;
  629. int rc;
  630. unsigned long wait;
  631. spin_lock_irqsave(&sclp_lock, flags);
  632. /* Check if interface is in appropriate state */
  633. if (sclp_mask_state != sclp_mask_state_idle) {
  634. spin_unlock_irqrestore(&sclp_lock, flags);
  635. return -EBUSY;
  636. }
  637. if (sclp_activation_state == sclp_activation_state_inactive) {
  638. spin_unlock_irqrestore(&sclp_lock, flags);
  639. return -EINVAL;
  640. }
  641. sclp_mask_state = sclp_mask_state_initializing;
  642. /* Determine mask */
  643. if (calculate)
  644. __sclp_get_mask(&receive_mask, &send_mask);
  645. else {
  646. receive_mask = 0;
  647. send_mask = 0;
  648. }
  649. rc = -EIO;
  650. for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
  651. /* Prepare request */
  652. __sclp_make_init_req(receive_mask, send_mask);
  653. spin_unlock_irqrestore(&sclp_lock, flags);
  654. if (sclp_add_request(&sclp_init_req)) {
  655. /* Try again later */
  656. wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
  657. while (time_before(jiffies, wait))
  658. sclp_sync_wait();
  659. spin_lock_irqsave(&sclp_lock, flags);
  660. continue;
  661. }
  662. while (sclp_init_req.status != SCLP_REQ_DONE &&
  663. sclp_init_req.status != SCLP_REQ_FAILED)
  664. sclp_sync_wait();
  665. spin_lock_irqsave(&sclp_lock, flags);
  666. if (sclp_init_req.status == SCLP_REQ_DONE &&
  667. sccb->header.response_code == 0x20) {
  668. /* Successful request */
  669. if (calculate) {
  670. sclp_receive_mask = sccb->sclp_receive_mask;
  671. sclp_send_mask = sccb->sclp_send_mask;
  672. } else {
  673. sclp_receive_mask = 0;
  674. sclp_send_mask = 0;
  675. }
  676. spin_unlock_irqrestore(&sclp_lock, flags);
  677. sclp_dispatch_state_change();
  678. spin_lock_irqsave(&sclp_lock, flags);
  679. rc = 0;
  680. break;
  681. }
  682. }
  683. sclp_mask_state = sclp_mask_state_idle;
  684. spin_unlock_irqrestore(&sclp_lock, flags);
  685. return rc;
  686. }
  687. /* Deactivate SCLP interface. On success, new requests will be rejected,
  688. * events will no longer be dispatched. Return 0 on success, non-zero
  689. * otherwise. */
  690. int
  691. sclp_deactivate(void)
  692. {
  693. unsigned long flags;
  694. int rc;
  695. spin_lock_irqsave(&sclp_lock, flags);
  696. /* Deactivate can only be called when active */
  697. if (sclp_activation_state != sclp_activation_state_active) {
  698. spin_unlock_irqrestore(&sclp_lock, flags);
  699. return -EINVAL;
  700. }
  701. sclp_activation_state = sclp_activation_state_deactivating;
  702. spin_unlock_irqrestore(&sclp_lock, flags);
  703. rc = sclp_init_mask(0);
  704. spin_lock_irqsave(&sclp_lock, flags);
  705. if (rc == 0)
  706. sclp_activation_state = sclp_activation_state_inactive;
  707. else
  708. sclp_activation_state = sclp_activation_state_active;
  709. spin_unlock_irqrestore(&sclp_lock, flags);
  710. return rc;
  711. }
  712. EXPORT_SYMBOL(sclp_deactivate);
  713. /* Reactivate SCLP interface after sclp_deactivate. On success, new
  714. * requests will be accepted, events will be dispatched again. Return 0 on
  715. * success, non-zero otherwise. */
  716. int
  717. sclp_reactivate(void)
  718. {
  719. unsigned long flags;
  720. int rc;
  721. spin_lock_irqsave(&sclp_lock, flags);
  722. /* Reactivate can only be called when inactive */
  723. if (sclp_activation_state != sclp_activation_state_inactive) {
  724. spin_unlock_irqrestore(&sclp_lock, flags);
  725. return -EINVAL;
  726. }
  727. sclp_activation_state = sclp_activation_state_activating;
  728. spin_unlock_irqrestore(&sclp_lock, flags);
  729. rc = sclp_init_mask(1);
  730. spin_lock_irqsave(&sclp_lock, flags);
  731. if (rc == 0)
  732. sclp_activation_state = sclp_activation_state_active;
  733. else
  734. sclp_activation_state = sclp_activation_state_inactive;
  735. spin_unlock_irqrestore(&sclp_lock, flags);
  736. return rc;
  737. }
  738. EXPORT_SYMBOL(sclp_reactivate);
  739. /* Handler for external interruption used during initialization. Modify
  740. * request state to done. */
  741. static void sclp_check_handler(unsigned int ext_int_code,
  742. unsigned int param32, unsigned long param64)
  743. {
  744. u32 finished_sccb;
  745. kstat_cpu(smp_processor_id()).irqs[EXTINT_SCP]++;
  746. finished_sccb = param32 & 0xfffffff8;
  747. /* Is this the interrupt we are waiting for? */
  748. if (finished_sccb == 0)
  749. return;
  750. if (finished_sccb != (u32) (addr_t) sclp_init_sccb)
  751. panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
  752. finished_sccb);
  753. spin_lock(&sclp_lock);
  754. if (sclp_running_state == sclp_running_state_running) {
  755. sclp_init_req.status = SCLP_REQ_DONE;
  756. sclp_running_state = sclp_running_state_idle;
  757. }
  758. spin_unlock(&sclp_lock);
  759. }
  760. /* Initial init mask request timed out. Modify request state to failed. */
  761. static void
  762. sclp_check_timeout(unsigned long data)
  763. {
  764. unsigned long flags;
  765. spin_lock_irqsave(&sclp_lock, flags);
  766. if (sclp_running_state == sclp_running_state_running) {
  767. sclp_init_req.status = SCLP_REQ_FAILED;
  768. sclp_running_state = sclp_running_state_idle;
  769. }
  770. spin_unlock_irqrestore(&sclp_lock, flags);
  771. }
  772. /* Perform a check of the SCLP interface. Return zero if the interface is
  773. * available and there are no pending requests from a previous instance.
  774. * Return non-zero otherwise. */
  775. static int
  776. sclp_check_interface(void)
  777. {
  778. struct init_sccb *sccb;
  779. unsigned long flags;
  780. int retry;
  781. int rc;
  782. spin_lock_irqsave(&sclp_lock, flags);
  783. /* Prepare init mask command */
  784. rc = register_external_interrupt(0x2401, sclp_check_handler);
  785. if (rc) {
  786. spin_unlock_irqrestore(&sclp_lock, flags);
  787. return rc;
  788. }
  789. for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
  790. __sclp_make_init_req(0, 0);
  791. sccb = (struct init_sccb *) sclp_init_req.sccb;
  792. rc = sclp_service_call(sclp_init_req.command, sccb);
  793. if (rc == -EIO)
  794. break;
  795. sclp_init_req.status = SCLP_REQ_RUNNING;
  796. sclp_running_state = sclp_running_state_running;
  797. __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
  798. sclp_check_timeout, 0);
  799. spin_unlock_irqrestore(&sclp_lock, flags);
  800. /* Enable service-signal interruption - needs to happen
  801. * with IRQs enabled. */
  802. ctl_set_bit(0, 9);
  803. /* Wait for signal from interrupt or timeout */
  804. sclp_sync_wait();
  805. /* Disable service-signal interruption - needs to happen
  806. * with IRQs enabled. */
  807. ctl_clear_bit(0,9);
  808. spin_lock_irqsave(&sclp_lock, flags);
  809. del_timer(&sclp_request_timer);
  810. if (sclp_init_req.status == SCLP_REQ_DONE &&
  811. sccb->header.response_code == 0x20) {
  812. rc = 0;
  813. break;
  814. } else
  815. rc = -EBUSY;
  816. }
  817. unregister_external_interrupt(0x2401, sclp_check_handler);
  818. spin_unlock_irqrestore(&sclp_lock, flags);
  819. return rc;
  820. }
  821. /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
  822. * events from interfering with rebooted system. */
  823. static int
  824. sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
  825. {
  826. sclp_deactivate();
  827. return NOTIFY_DONE;
  828. }
  829. static struct notifier_block sclp_reboot_notifier = {
  830. .notifier_call = sclp_reboot_event
  831. };
  832. /*
  833. * Suspend/resume SCLP notifier implementation
  834. */
  835. static void sclp_pm_event(enum sclp_pm_event sclp_pm_event, int rollback)
  836. {
  837. struct sclp_register *reg;
  838. unsigned long flags;
  839. if (!rollback) {
  840. spin_lock_irqsave(&sclp_lock, flags);
  841. list_for_each_entry(reg, &sclp_reg_list, list)
  842. reg->pm_event_posted = 0;
  843. spin_unlock_irqrestore(&sclp_lock, flags);
  844. }
  845. do {
  846. spin_lock_irqsave(&sclp_lock, flags);
  847. list_for_each_entry(reg, &sclp_reg_list, list) {
  848. if (rollback && reg->pm_event_posted)
  849. goto found;
  850. if (!rollback && !reg->pm_event_posted)
  851. goto found;
  852. }
  853. spin_unlock_irqrestore(&sclp_lock, flags);
  854. return;
  855. found:
  856. spin_unlock_irqrestore(&sclp_lock, flags);
  857. if (reg->pm_event_fn)
  858. reg->pm_event_fn(reg, sclp_pm_event);
  859. reg->pm_event_posted = rollback ? 0 : 1;
  860. } while (1);
  861. }
  862. /*
  863. * Susend/resume callbacks for platform device
  864. */
  865. static int sclp_freeze(struct device *dev)
  866. {
  867. unsigned long flags;
  868. int rc;
  869. sclp_pm_event(SCLP_PM_EVENT_FREEZE, 0);
  870. spin_lock_irqsave(&sclp_lock, flags);
  871. sclp_suspend_state = sclp_suspend_state_suspended;
  872. spin_unlock_irqrestore(&sclp_lock, flags);
  873. /* Init supend data */
  874. memset(&sclp_suspend_req, 0, sizeof(sclp_suspend_req));
  875. sclp_suspend_req.callback = sclp_suspend_req_cb;
  876. sclp_suspend_req.status = SCLP_REQ_FILLED;
  877. init_completion(&sclp_request_queue_flushed);
  878. rc = sclp_add_request(&sclp_suspend_req);
  879. if (rc == 0)
  880. wait_for_completion(&sclp_request_queue_flushed);
  881. else if (rc != -ENODATA)
  882. goto fail_thaw;
  883. rc = sclp_deactivate();
  884. if (rc)
  885. goto fail_thaw;
  886. return 0;
  887. fail_thaw:
  888. spin_lock_irqsave(&sclp_lock, flags);
  889. sclp_suspend_state = sclp_suspend_state_running;
  890. spin_unlock_irqrestore(&sclp_lock, flags);
  891. sclp_pm_event(SCLP_PM_EVENT_THAW, 1);
  892. return rc;
  893. }
  894. static int sclp_undo_suspend(enum sclp_pm_event event)
  895. {
  896. unsigned long flags;
  897. int rc;
  898. rc = sclp_reactivate();
  899. if (rc)
  900. return rc;
  901. spin_lock_irqsave(&sclp_lock, flags);
  902. sclp_suspend_state = sclp_suspend_state_running;
  903. spin_unlock_irqrestore(&sclp_lock, flags);
  904. sclp_pm_event(event, 0);
  905. return 0;
  906. }
  907. static int sclp_thaw(struct device *dev)
  908. {
  909. return sclp_undo_suspend(SCLP_PM_EVENT_THAW);
  910. }
  911. static int sclp_restore(struct device *dev)
  912. {
  913. return sclp_undo_suspend(SCLP_PM_EVENT_RESTORE);
  914. }
  915. static const struct dev_pm_ops sclp_pm_ops = {
  916. .freeze = sclp_freeze,
  917. .thaw = sclp_thaw,
  918. .restore = sclp_restore,
  919. };
  920. static struct platform_driver sclp_pdrv = {
  921. .driver = {
  922. .name = "sclp",
  923. .owner = THIS_MODULE,
  924. .pm = &sclp_pm_ops,
  925. },
  926. };
  927. static struct platform_device *sclp_pdev;
  928. /* Initialize SCLP driver. Return zero if driver is operational, non-zero
  929. * otherwise. */
  930. static int
  931. sclp_init(void)
  932. {
  933. unsigned long flags;
  934. int rc = 0;
  935. spin_lock_irqsave(&sclp_lock, flags);
  936. /* Check for previous or running initialization */
  937. if (sclp_init_state != sclp_init_state_uninitialized)
  938. goto fail_unlock;
  939. sclp_init_state = sclp_init_state_initializing;
  940. /* Set up variables */
  941. INIT_LIST_HEAD(&sclp_req_queue);
  942. INIT_LIST_HEAD(&sclp_reg_list);
  943. list_add(&sclp_state_change_event.list, &sclp_reg_list);
  944. init_timer(&sclp_request_timer);
  945. /* Check interface */
  946. spin_unlock_irqrestore(&sclp_lock, flags);
  947. rc = sclp_check_interface();
  948. spin_lock_irqsave(&sclp_lock, flags);
  949. if (rc)
  950. goto fail_init_state_uninitialized;
  951. /* Register reboot handler */
  952. rc = register_reboot_notifier(&sclp_reboot_notifier);
  953. if (rc)
  954. goto fail_init_state_uninitialized;
  955. /* Register interrupt handler */
  956. rc = register_external_interrupt(0x2401, sclp_interrupt_handler);
  957. if (rc)
  958. goto fail_unregister_reboot_notifier;
  959. sclp_init_state = sclp_init_state_initialized;
  960. spin_unlock_irqrestore(&sclp_lock, flags);
  961. /* Enable service-signal external interruption - needs to happen with
  962. * IRQs enabled. */
  963. ctl_set_bit(0, 9);
  964. sclp_init_mask(1);
  965. return 0;
  966. fail_unregister_reboot_notifier:
  967. unregister_reboot_notifier(&sclp_reboot_notifier);
  968. fail_init_state_uninitialized:
  969. sclp_init_state = sclp_init_state_uninitialized;
  970. fail_unlock:
  971. spin_unlock_irqrestore(&sclp_lock, flags);
  972. return rc;
  973. }
  974. /*
  975. * SCLP panic notifier: If we are suspended, we thaw SCLP in order to be able
  976. * to print the panic message.
  977. */
  978. static int sclp_panic_notify(struct notifier_block *self,
  979. unsigned long event, void *data)
  980. {
  981. if (sclp_suspend_state == sclp_suspend_state_suspended)
  982. sclp_undo_suspend(SCLP_PM_EVENT_THAW);
  983. return NOTIFY_OK;
  984. }
  985. static struct notifier_block sclp_on_panic_nb = {
  986. .notifier_call = sclp_panic_notify,
  987. .priority = SCLP_PANIC_PRIO,
  988. };
  989. static __init int sclp_initcall(void)
  990. {
  991. int rc;
  992. rc = platform_driver_register(&sclp_pdrv);
  993. if (rc)
  994. return rc;
  995. sclp_pdev = platform_device_register_simple("sclp", -1, NULL, 0);
  996. rc = IS_ERR(sclp_pdev) ? PTR_ERR(sclp_pdev) : 0;
  997. if (rc)
  998. goto fail_platform_driver_unregister;
  999. rc = atomic_notifier_chain_register(&panic_notifier_list,
  1000. &sclp_on_panic_nb);
  1001. if (rc)
  1002. goto fail_platform_device_unregister;
  1003. return sclp_init();
  1004. fail_platform_device_unregister:
  1005. platform_device_unregister(sclp_pdev);
  1006. fail_platform_driver_unregister:
  1007. platform_driver_unregister(&sclp_pdrv);
  1008. return rc;
  1009. }
  1010. arch_initcall(sclp_initcall);