sclp.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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/module.h>
  10. #include <linux/err.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/timer.h>
  14. #include <linux/reboot.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/init.h>
  17. #include <linux/suspend.h>
  18. #include <linux/completion.h>
  19. #include <linux/platform_device.h>
  20. #include <asm/types.h>
  21. #include <asm/s390_ext.h>
  22. #include "sclp.h"
  23. #define SCLP_HEADER "sclp: "
  24. /* Structure for register_early_external_interrupt. */
  25. static ext_int_info_t ext_int_info_hwc;
  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. /* Sucessfully 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
  351. sclp_interrupt_handler(__u16 code)
  352. {
  353. struct sclp_req *req;
  354. u32 finished_sccb;
  355. u32 evbuf_pending;
  356. spin_lock(&sclp_lock);
  357. finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
  358. evbuf_pending = S390_lowcore.ext_params & 0x3;
  359. if (finished_sccb) {
  360. del_timer(&sclp_request_timer);
  361. sclp_running_state = sclp_running_state_reset_pending;
  362. req = __sclp_find_req(finished_sccb);
  363. if (req) {
  364. /* Request post-processing */
  365. list_del(&req->list);
  366. req->status = SCLP_REQ_DONE;
  367. if (req->callback) {
  368. spin_unlock(&sclp_lock);
  369. req->callback(req, req->callback_data);
  370. spin_lock(&sclp_lock);
  371. }
  372. }
  373. sclp_running_state = sclp_running_state_idle;
  374. }
  375. if (evbuf_pending &&
  376. sclp_activation_state == sclp_activation_state_active)
  377. __sclp_queue_read_req();
  378. spin_unlock(&sclp_lock);
  379. sclp_process_queue();
  380. }
  381. /* Convert interval in jiffies to TOD ticks. */
  382. static inline u64
  383. sclp_tod_from_jiffies(unsigned long jiffies)
  384. {
  385. return (u64) (jiffies / HZ) << 32;
  386. }
  387. /* Wait until a currently running request finished. Note: while this function
  388. * is running, no timers are served on the calling CPU. */
  389. void
  390. sclp_sync_wait(void)
  391. {
  392. unsigned long long old_tick;
  393. unsigned long flags;
  394. unsigned long cr0, cr0_sync;
  395. u64 timeout;
  396. int irq_context;
  397. /* We'll be disabling timer interrupts, so we need a custom timeout
  398. * mechanism */
  399. timeout = 0;
  400. if (timer_pending(&sclp_request_timer)) {
  401. /* Get timeout TOD value */
  402. timeout = get_clock() +
  403. sclp_tod_from_jiffies(sclp_request_timer.expires -
  404. jiffies);
  405. }
  406. local_irq_save(flags);
  407. /* Prevent bottom half from executing once we force interrupts open */
  408. irq_context = in_interrupt();
  409. if (!irq_context)
  410. local_bh_disable();
  411. /* Enable service-signal interruption, disable timer interrupts */
  412. old_tick = local_tick_disable();
  413. trace_hardirqs_on();
  414. __ctl_store(cr0, 0, 0);
  415. cr0_sync = cr0;
  416. cr0_sync &= 0xffff00a0;
  417. cr0_sync |= 0x00000200;
  418. __ctl_load(cr0_sync, 0, 0);
  419. __raw_local_irq_stosm(0x01);
  420. /* Loop until driver state indicates finished request */
  421. while (sclp_running_state != sclp_running_state_idle) {
  422. /* Check for expired request timer */
  423. if (timer_pending(&sclp_request_timer) &&
  424. get_clock() > timeout &&
  425. del_timer(&sclp_request_timer))
  426. sclp_request_timer.function(sclp_request_timer.data);
  427. cpu_relax();
  428. }
  429. local_irq_disable();
  430. __ctl_load(cr0, 0, 0);
  431. if (!irq_context)
  432. _local_bh_enable();
  433. local_tick_enable(old_tick);
  434. local_irq_restore(flags);
  435. }
  436. EXPORT_SYMBOL(sclp_sync_wait);
  437. /* Dispatch changes in send and receive mask to registered listeners. */
  438. static void
  439. sclp_dispatch_state_change(void)
  440. {
  441. struct list_head *l;
  442. struct sclp_register *reg;
  443. unsigned long flags;
  444. sccb_mask_t receive_mask;
  445. sccb_mask_t send_mask;
  446. do {
  447. spin_lock_irqsave(&sclp_lock, flags);
  448. reg = NULL;
  449. list_for_each(l, &sclp_reg_list) {
  450. reg = list_entry(l, struct sclp_register, list);
  451. receive_mask = reg->send_mask & sclp_receive_mask;
  452. send_mask = reg->receive_mask & sclp_send_mask;
  453. if (reg->sclp_receive_mask != receive_mask ||
  454. reg->sclp_send_mask != send_mask) {
  455. reg->sclp_receive_mask = receive_mask;
  456. reg->sclp_send_mask = send_mask;
  457. break;
  458. } else
  459. reg = NULL;
  460. }
  461. spin_unlock_irqrestore(&sclp_lock, flags);
  462. if (reg && reg->state_change_fn)
  463. reg->state_change_fn(reg);
  464. } while (reg);
  465. }
  466. struct sclp_statechangebuf {
  467. struct evbuf_header header;
  468. u8 validity_sclp_active_facility_mask : 1;
  469. u8 validity_sclp_receive_mask : 1;
  470. u8 validity_sclp_send_mask : 1;
  471. u8 validity_read_data_function_mask : 1;
  472. u16 _zeros : 12;
  473. u16 mask_length;
  474. u64 sclp_active_facility_mask;
  475. sccb_mask_t sclp_receive_mask;
  476. sccb_mask_t sclp_send_mask;
  477. u32 read_data_function_mask;
  478. } __attribute__((packed));
  479. /* State change event callback. Inform listeners of changes. */
  480. static void
  481. sclp_state_change_cb(struct evbuf_header *evbuf)
  482. {
  483. unsigned long flags;
  484. struct sclp_statechangebuf *scbuf;
  485. scbuf = (struct sclp_statechangebuf *) evbuf;
  486. if (scbuf->mask_length != sizeof(sccb_mask_t))
  487. return;
  488. spin_lock_irqsave(&sclp_lock, flags);
  489. if (scbuf->validity_sclp_receive_mask)
  490. sclp_receive_mask = scbuf->sclp_receive_mask;
  491. if (scbuf->validity_sclp_send_mask)
  492. sclp_send_mask = scbuf->sclp_send_mask;
  493. spin_unlock_irqrestore(&sclp_lock, flags);
  494. if (scbuf->validity_sclp_active_facility_mask)
  495. sclp_facilities = scbuf->sclp_active_facility_mask;
  496. sclp_dispatch_state_change();
  497. }
  498. static struct sclp_register sclp_state_change_event = {
  499. .receive_mask = EVTYP_STATECHANGE_MASK,
  500. .receiver_fn = sclp_state_change_cb
  501. };
  502. /* Calculate receive and send mask of currently registered listeners.
  503. * Called while sclp_lock is locked. */
  504. static inline void
  505. __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
  506. {
  507. struct list_head *l;
  508. struct sclp_register *t;
  509. *receive_mask = 0;
  510. *send_mask = 0;
  511. list_for_each(l, &sclp_reg_list) {
  512. t = list_entry(l, struct sclp_register, list);
  513. *receive_mask |= t->receive_mask;
  514. *send_mask |= t->send_mask;
  515. }
  516. }
  517. /* Register event listener. Return 0 on success, non-zero otherwise. */
  518. int
  519. sclp_register(struct sclp_register *reg)
  520. {
  521. unsigned long flags;
  522. sccb_mask_t receive_mask;
  523. sccb_mask_t send_mask;
  524. int rc;
  525. rc = sclp_init();
  526. if (rc)
  527. return rc;
  528. spin_lock_irqsave(&sclp_lock, flags);
  529. /* Check event mask for collisions */
  530. __sclp_get_mask(&receive_mask, &send_mask);
  531. if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
  532. spin_unlock_irqrestore(&sclp_lock, flags);
  533. return -EBUSY;
  534. }
  535. /* Trigger initial state change callback */
  536. reg->sclp_receive_mask = 0;
  537. reg->sclp_send_mask = 0;
  538. reg->pm_event_posted = 0;
  539. list_add(&reg->list, &sclp_reg_list);
  540. spin_unlock_irqrestore(&sclp_lock, flags);
  541. rc = sclp_init_mask(1);
  542. if (rc) {
  543. spin_lock_irqsave(&sclp_lock, flags);
  544. list_del(&reg->list);
  545. spin_unlock_irqrestore(&sclp_lock, flags);
  546. }
  547. return rc;
  548. }
  549. EXPORT_SYMBOL(sclp_register);
  550. /* Unregister event listener. */
  551. void
  552. sclp_unregister(struct sclp_register *reg)
  553. {
  554. unsigned long flags;
  555. spin_lock_irqsave(&sclp_lock, flags);
  556. list_del(&reg->list);
  557. spin_unlock_irqrestore(&sclp_lock, flags);
  558. sclp_init_mask(1);
  559. }
  560. EXPORT_SYMBOL(sclp_unregister);
  561. /* Remove event buffers which are marked processed. Return the number of
  562. * remaining event buffers. */
  563. int
  564. sclp_remove_processed(struct sccb_header *sccb)
  565. {
  566. struct evbuf_header *evbuf;
  567. int unprocessed;
  568. u16 remaining;
  569. evbuf = (struct evbuf_header *) (sccb + 1);
  570. unprocessed = 0;
  571. remaining = sccb->length - sizeof(struct sccb_header);
  572. while (remaining > 0) {
  573. remaining -= evbuf->length;
  574. if (evbuf->flags & 0x80) {
  575. sccb->length -= evbuf->length;
  576. memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
  577. remaining);
  578. } else {
  579. unprocessed++;
  580. evbuf = (struct evbuf_header *)
  581. ((addr_t) evbuf + evbuf->length);
  582. }
  583. }
  584. return unprocessed;
  585. }
  586. EXPORT_SYMBOL(sclp_remove_processed);
  587. struct init_sccb {
  588. struct sccb_header header;
  589. u16 _reserved;
  590. u16 mask_length;
  591. sccb_mask_t receive_mask;
  592. sccb_mask_t send_mask;
  593. sccb_mask_t sclp_receive_mask;
  594. sccb_mask_t sclp_send_mask;
  595. } __attribute__((packed));
  596. /* Prepare init mask request. Called while sclp_lock is locked. */
  597. static inline void
  598. __sclp_make_init_req(u32 receive_mask, u32 send_mask)
  599. {
  600. struct init_sccb *sccb;
  601. sccb = (struct init_sccb *) sclp_init_sccb;
  602. clear_page(sccb);
  603. memset(&sclp_init_req, 0, sizeof(struct sclp_req));
  604. sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
  605. sclp_init_req.status = SCLP_REQ_FILLED;
  606. sclp_init_req.start_count = 0;
  607. sclp_init_req.callback = NULL;
  608. sclp_init_req.callback_data = NULL;
  609. sclp_init_req.sccb = sccb;
  610. sccb->header.length = sizeof(struct init_sccb);
  611. sccb->mask_length = sizeof(sccb_mask_t);
  612. sccb->receive_mask = receive_mask;
  613. sccb->send_mask = send_mask;
  614. sccb->sclp_receive_mask = 0;
  615. sccb->sclp_send_mask = 0;
  616. }
  617. /* Start init mask request. If calculate is non-zero, calculate the mask as
  618. * requested by registered listeners. Use zero mask otherwise. Return 0 on
  619. * success, non-zero otherwise. */
  620. static int
  621. sclp_init_mask(int calculate)
  622. {
  623. unsigned long flags;
  624. struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
  625. sccb_mask_t receive_mask;
  626. sccb_mask_t send_mask;
  627. int retry;
  628. int rc;
  629. unsigned long wait;
  630. spin_lock_irqsave(&sclp_lock, flags);
  631. /* Check if interface is in appropriate state */
  632. if (sclp_mask_state != sclp_mask_state_idle) {
  633. spin_unlock_irqrestore(&sclp_lock, flags);
  634. return -EBUSY;
  635. }
  636. if (sclp_activation_state == sclp_activation_state_inactive) {
  637. spin_unlock_irqrestore(&sclp_lock, flags);
  638. return -EINVAL;
  639. }
  640. sclp_mask_state = sclp_mask_state_initializing;
  641. /* Determine mask */
  642. if (calculate)
  643. __sclp_get_mask(&receive_mask, &send_mask);
  644. else {
  645. receive_mask = 0;
  646. send_mask = 0;
  647. }
  648. rc = -EIO;
  649. for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
  650. /* Prepare request */
  651. __sclp_make_init_req(receive_mask, send_mask);
  652. spin_unlock_irqrestore(&sclp_lock, flags);
  653. if (sclp_add_request(&sclp_init_req)) {
  654. /* Try again later */
  655. wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
  656. while (time_before(jiffies, wait))
  657. sclp_sync_wait();
  658. spin_lock_irqsave(&sclp_lock, flags);
  659. continue;
  660. }
  661. while (sclp_init_req.status != SCLP_REQ_DONE &&
  662. sclp_init_req.status != SCLP_REQ_FAILED)
  663. sclp_sync_wait();
  664. spin_lock_irqsave(&sclp_lock, flags);
  665. if (sclp_init_req.status == SCLP_REQ_DONE &&
  666. sccb->header.response_code == 0x20) {
  667. /* Successful request */
  668. if (calculate) {
  669. sclp_receive_mask = sccb->sclp_receive_mask;
  670. sclp_send_mask = sccb->sclp_send_mask;
  671. } else {
  672. sclp_receive_mask = 0;
  673. sclp_send_mask = 0;
  674. }
  675. spin_unlock_irqrestore(&sclp_lock, flags);
  676. sclp_dispatch_state_change();
  677. spin_lock_irqsave(&sclp_lock, flags);
  678. rc = 0;
  679. break;
  680. }
  681. }
  682. sclp_mask_state = sclp_mask_state_idle;
  683. spin_unlock_irqrestore(&sclp_lock, flags);
  684. return rc;
  685. }
  686. /* Deactivate SCLP interface. On success, new requests will be rejected,
  687. * events will no longer be dispatched. Return 0 on success, non-zero
  688. * otherwise. */
  689. int
  690. sclp_deactivate(void)
  691. {
  692. unsigned long flags;
  693. int rc;
  694. spin_lock_irqsave(&sclp_lock, flags);
  695. /* Deactivate can only be called when active */
  696. if (sclp_activation_state != sclp_activation_state_active) {
  697. spin_unlock_irqrestore(&sclp_lock, flags);
  698. return -EINVAL;
  699. }
  700. sclp_activation_state = sclp_activation_state_deactivating;
  701. spin_unlock_irqrestore(&sclp_lock, flags);
  702. rc = sclp_init_mask(0);
  703. spin_lock_irqsave(&sclp_lock, flags);
  704. if (rc == 0)
  705. sclp_activation_state = sclp_activation_state_inactive;
  706. else
  707. sclp_activation_state = sclp_activation_state_active;
  708. spin_unlock_irqrestore(&sclp_lock, flags);
  709. return rc;
  710. }
  711. EXPORT_SYMBOL(sclp_deactivate);
  712. /* Reactivate SCLP interface after sclp_deactivate. On success, new
  713. * requests will be accepted, events will be dispatched again. Return 0 on
  714. * success, non-zero otherwise. */
  715. int
  716. sclp_reactivate(void)
  717. {
  718. unsigned long flags;
  719. int rc;
  720. spin_lock_irqsave(&sclp_lock, flags);
  721. /* Reactivate can only be called when inactive */
  722. if (sclp_activation_state != sclp_activation_state_inactive) {
  723. spin_unlock_irqrestore(&sclp_lock, flags);
  724. return -EINVAL;
  725. }
  726. sclp_activation_state = sclp_activation_state_activating;
  727. spin_unlock_irqrestore(&sclp_lock, flags);
  728. rc = sclp_init_mask(1);
  729. spin_lock_irqsave(&sclp_lock, flags);
  730. if (rc == 0)
  731. sclp_activation_state = sclp_activation_state_active;
  732. else
  733. sclp_activation_state = sclp_activation_state_inactive;
  734. spin_unlock_irqrestore(&sclp_lock, flags);
  735. return rc;
  736. }
  737. EXPORT_SYMBOL(sclp_reactivate);
  738. /* Handler for external interruption used during initialization. Modify
  739. * request state to done. */
  740. static void
  741. sclp_check_handler(__u16 code)
  742. {
  743. u32 finished_sccb;
  744. finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
  745. /* Is this the interrupt we are waiting for? */
  746. if (finished_sccb == 0)
  747. return;
  748. if (finished_sccb != (u32) (addr_t) sclp_init_sccb)
  749. panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
  750. finished_sccb);
  751. spin_lock(&sclp_lock);
  752. if (sclp_running_state == sclp_running_state_running) {
  753. sclp_init_req.status = SCLP_REQ_DONE;
  754. sclp_running_state = sclp_running_state_idle;
  755. }
  756. spin_unlock(&sclp_lock);
  757. }
  758. /* Initial init mask request timed out. Modify request state to failed. */
  759. static void
  760. sclp_check_timeout(unsigned long data)
  761. {
  762. unsigned long flags;
  763. spin_lock_irqsave(&sclp_lock, flags);
  764. if (sclp_running_state == sclp_running_state_running) {
  765. sclp_init_req.status = SCLP_REQ_FAILED;
  766. sclp_running_state = sclp_running_state_idle;
  767. }
  768. spin_unlock_irqrestore(&sclp_lock, flags);
  769. }
  770. /* Perform a check of the SCLP interface. Return zero if the interface is
  771. * available and there are no pending requests from a previous instance.
  772. * Return non-zero otherwise. */
  773. static int
  774. sclp_check_interface(void)
  775. {
  776. struct init_sccb *sccb;
  777. unsigned long flags;
  778. int retry;
  779. int rc;
  780. spin_lock_irqsave(&sclp_lock, flags);
  781. /* Prepare init mask command */
  782. rc = register_early_external_interrupt(0x2401, sclp_check_handler,
  783. &ext_int_info_hwc);
  784. if (rc) {
  785. spin_unlock_irqrestore(&sclp_lock, flags);
  786. return rc;
  787. }
  788. for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
  789. __sclp_make_init_req(0, 0);
  790. sccb = (struct init_sccb *) sclp_init_req.sccb;
  791. rc = sclp_service_call(sclp_init_req.command, sccb);
  792. if (rc == -EIO)
  793. break;
  794. sclp_init_req.status = SCLP_REQ_RUNNING;
  795. sclp_running_state = sclp_running_state_running;
  796. __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
  797. sclp_check_timeout, 0);
  798. spin_unlock_irqrestore(&sclp_lock, flags);
  799. /* Enable service-signal interruption - needs to happen
  800. * with IRQs enabled. */
  801. ctl_set_bit(0, 9);
  802. /* Wait for signal from interrupt or timeout */
  803. sclp_sync_wait();
  804. /* Disable service-signal interruption - needs to happen
  805. * with IRQs enabled. */
  806. ctl_clear_bit(0,9);
  807. spin_lock_irqsave(&sclp_lock, flags);
  808. del_timer(&sclp_request_timer);
  809. if (sclp_init_req.status == SCLP_REQ_DONE &&
  810. sccb->header.response_code == 0x20) {
  811. rc = 0;
  812. break;
  813. } else
  814. rc = -EBUSY;
  815. }
  816. unregister_early_external_interrupt(0x2401, sclp_check_handler,
  817. &ext_int_info_hwc);
  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_early_external_interrupt(0x2401, sclp_interrupt_handler,
  957. &ext_int_info_hwc);
  958. if (rc)
  959. goto fail_unregister_reboot_notifier;
  960. sclp_init_state = sclp_init_state_initialized;
  961. spin_unlock_irqrestore(&sclp_lock, flags);
  962. /* Enable service-signal external interruption - needs to happen with
  963. * IRQs enabled. */
  964. ctl_set_bit(0, 9);
  965. sclp_init_mask(1);
  966. return 0;
  967. fail_unregister_reboot_notifier:
  968. unregister_reboot_notifier(&sclp_reboot_notifier);
  969. fail_init_state_uninitialized:
  970. sclp_init_state = sclp_init_state_uninitialized;
  971. fail_unlock:
  972. spin_unlock_irqrestore(&sclp_lock, flags);
  973. return rc;
  974. }
  975. /*
  976. * SCLP panic notifier: If we are suspended, we thaw SCLP in order to be able
  977. * to print the panic message.
  978. */
  979. static int sclp_panic_notify(struct notifier_block *self,
  980. unsigned long event, void *data)
  981. {
  982. if (sclp_suspend_state == sclp_suspend_state_suspended)
  983. sclp_undo_suspend(SCLP_PM_EVENT_THAW);
  984. return NOTIFY_OK;
  985. }
  986. static struct notifier_block sclp_on_panic_nb = {
  987. .notifier_call = sclp_panic_notify,
  988. .priority = SCLP_PANIC_PRIO,
  989. };
  990. static __init int sclp_initcall(void)
  991. {
  992. int rc;
  993. rc = platform_driver_register(&sclp_pdrv);
  994. if (rc)
  995. return rc;
  996. sclp_pdev = platform_device_register_simple("sclp", -1, NULL, 0);
  997. rc = IS_ERR(sclp_pdev) ? PTR_ERR(sclp_pdev) : 0;
  998. if (rc)
  999. goto fail_platform_driver_unregister;
  1000. rc = atomic_notifier_chain_register(&panic_notifier_list,
  1001. &sclp_on_panic_nb);
  1002. if (rc)
  1003. goto fail_platform_device_unregister;
  1004. return sclp_init();
  1005. fail_platform_device_unregister:
  1006. platform_device_unregister(sclp_pdev);
  1007. fail_platform_driver_unregister:
  1008. platform_driver_unregister(&sclp_pdrv);
  1009. return rc;
  1010. }
  1011. arch_initcall(sclp_initcall);