sclp.c 30 KB

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