sclp.c 32 KB

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