eadm_sch.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Driver for s390 eadm subchannels
  3. *
  4. * Copyright IBM Corp. 2012
  5. * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
  6. */
  7. #include <linux/kernel_stat.h>
  8. #include <linux/completion.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/timer.h>
  14. #include <linux/slab.h>
  15. #include <linux/list.h>
  16. #include <asm/css_chars.h>
  17. #include <asm/debug.h>
  18. #include <asm/isc.h>
  19. #include <asm/cio.h>
  20. #include <asm/scsw.h>
  21. #include <asm/eadm.h>
  22. #include "eadm_sch.h"
  23. #include "ioasm.h"
  24. #include "cio.h"
  25. #include "css.h"
  26. #include "orb.h"
  27. MODULE_DESCRIPTION("driver for s390 eadm subchannels");
  28. MODULE_LICENSE("GPL");
  29. #define EADM_TIMEOUT (5 * HZ)
  30. static DEFINE_SPINLOCK(list_lock);
  31. static LIST_HEAD(eadm_list);
  32. static debug_info_t *eadm_debug;
  33. #define EADM_LOG(imp, txt) do { \
  34. debug_text_event(eadm_debug, imp, txt); \
  35. } while (0)
  36. static void EADM_LOG_HEX(int level, void *data, int length)
  37. {
  38. if (!debug_level_enabled(eadm_debug, level))
  39. return;
  40. while (length > 0) {
  41. debug_event(eadm_debug, level, data, length);
  42. length -= eadm_debug->buf_size;
  43. data += eadm_debug->buf_size;
  44. }
  45. }
  46. static void orb_init(union orb *orb)
  47. {
  48. memset(orb, 0, sizeof(union orb));
  49. orb->eadm.compat1 = 1;
  50. orb->eadm.compat2 = 1;
  51. orb->eadm.fmt = 1;
  52. orb->eadm.x = 1;
  53. }
  54. static int eadm_subchannel_start(struct subchannel *sch, struct aob *aob)
  55. {
  56. union orb *orb = &get_eadm_private(sch)->orb;
  57. int cc;
  58. orb_init(orb);
  59. orb->eadm.aob = (u32)__pa(aob);
  60. orb->eadm.intparm = (u32)(addr_t)sch;
  61. orb->eadm.key = PAGE_DEFAULT_KEY >> 4;
  62. EADM_LOG(6, "start");
  63. EADM_LOG_HEX(6, &sch->schid, sizeof(sch->schid));
  64. cc = ssch(sch->schid, orb);
  65. switch (cc) {
  66. case 0:
  67. sch->schib.scsw.eadm.actl |= SCSW_ACTL_START_PEND;
  68. break;
  69. case 1: /* status pending */
  70. case 2: /* busy */
  71. return -EBUSY;
  72. case 3: /* not operational */
  73. return -ENODEV;
  74. }
  75. return 0;
  76. }
  77. static int eadm_subchannel_clear(struct subchannel *sch)
  78. {
  79. int cc;
  80. cc = csch(sch->schid);
  81. if (cc)
  82. return -ENODEV;
  83. sch->schib.scsw.eadm.actl |= SCSW_ACTL_CLEAR_PEND;
  84. return 0;
  85. }
  86. static void eadm_subchannel_timeout(unsigned long data)
  87. {
  88. struct subchannel *sch = (struct subchannel *) data;
  89. spin_lock_irq(sch->lock);
  90. EADM_LOG(1, "timeout");
  91. EADM_LOG_HEX(1, &sch->schid, sizeof(sch->schid));
  92. if (eadm_subchannel_clear(sch))
  93. EADM_LOG(0, "clear failed");
  94. spin_unlock_irq(sch->lock);
  95. }
  96. static void eadm_subchannel_set_timeout(struct subchannel *sch, int expires)
  97. {
  98. struct eadm_private *private = get_eadm_private(sch);
  99. if (expires == 0) {
  100. del_timer(&private->timer);
  101. return;
  102. }
  103. if (timer_pending(&private->timer)) {
  104. if (mod_timer(&private->timer, jiffies + expires))
  105. return;
  106. }
  107. private->timer.function = eadm_subchannel_timeout;
  108. private->timer.data = (unsigned long) sch;
  109. private->timer.expires = jiffies + expires;
  110. add_timer(&private->timer);
  111. }
  112. static void eadm_subchannel_irq(struct subchannel *sch)
  113. {
  114. struct eadm_private *private = get_eadm_private(sch);
  115. struct eadm_scsw *scsw = &sch->schib.scsw.eadm;
  116. struct irb *irb = (struct irb *)&S390_lowcore.irb;
  117. int error = 0;
  118. EADM_LOG(6, "irq");
  119. EADM_LOG_HEX(6, irb, sizeof(*irb));
  120. inc_irq_stat(IRQIO_ADM);
  121. if ((scsw->stctl & (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))
  122. && scsw->eswf == 1 && irb->esw.eadm.erw.r)
  123. error = -EIO;
  124. if (scsw->fctl & SCSW_FCTL_CLEAR_FUNC)
  125. error = -ETIMEDOUT;
  126. eadm_subchannel_set_timeout(sch, 0);
  127. if (private->state != EADM_BUSY) {
  128. EADM_LOG(1, "irq unsol");
  129. EADM_LOG_HEX(1, irb, sizeof(*irb));
  130. private->state = EADM_NOT_OPER;
  131. css_sched_sch_todo(sch, SCH_TODO_EVAL);
  132. return;
  133. }
  134. scm_irq_handler((struct aob *)(unsigned long)scsw->aob, error);
  135. private->state = EADM_IDLE;
  136. if (private->completion)
  137. complete(private->completion);
  138. }
  139. static struct subchannel *eadm_get_idle_sch(void)
  140. {
  141. struct eadm_private *private;
  142. struct subchannel *sch;
  143. unsigned long flags;
  144. spin_lock_irqsave(&list_lock, flags);
  145. list_for_each_entry(private, &eadm_list, head) {
  146. sch = private->sch;
  147. spin_lock(sch->lock);
  148. if (private->state == EADM_IDLE) {
  149. private->state = EADM_BUSY;
  150. list_move_tail(&private->head, &eadm_list);
  151. spin_unlock(sch->lock);
  152. spin_unlock_irqrestore(&list_lock, flags);
  153. return sch;
  154. }
  155. spin_unlock(sch->lock);
  156. }
  157. spin_unlock_irqrestore(&list_lock, flags);
  158. return NULL;
  159. }
  160. static int eadm_start_aob(struct aob *aob)
  161. {
  162. struct eadm_private *private;
  163. struct subchannel *sch;
  164. unsigned long flags;
  165. int ret;
  166. sch = eadm_get_idle_sch();
  167. if (!sch)
  168. return -EBUSY;
  169. spin_lock_irqsave(sch->lock, flags);
  170. eadm_subchannel_set_timeout(sch, EADM_TIMEOUT);
  171. ret = eadm_subchannel_start(sch, aob);
  172. if (!ret)
  173. goto out_unlock;
  174. /* Handle start subchannel failure. */
  175. eadm_subchannel_set_timeout(sch, 0);
  176. private = get_eadm_private(sch);
  177. private->state = EADM_NOT_OPER;
  178. css_sched_sch_todo(sch, SCH_TODO_EVAL);
  179. out_unlock:
  180. spin_unlock_irqrestore(sch->lock, flags);
  181. return ret;
  182. }
  183. static int eadm_subchannel_probe(struct subchannel *sch)
  184. {
  185. struct eadm_private *private;
  186. int ret;
  187. private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
  188. if (!private)
  189. return -ENOMEM;
  190. INIT_LIST_HEAD(&private->head);
  191. init_timer(&private->timer);
  192. spin_lock_irq(sch->lock);
  193. set_eadm_private(sch, private);
  194. private->state = EADM_IDLE;
  195. private->sch = sch;
  196. sch->isc = EADM_SCH_ISC;
  197. ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
  198. if (ret) {
  199. set_eadm_private(sch, NULL);
  200. spin_unlock_irq(sch->lock);
  201. kfree(private);
  202. goto out;
  203. }
  204. spin_unlock_irq(sch->lock);
  205. spin_lock_irq(&list_lock);
  206. list_add(&private->head, &eadm_list);
  207. spin_unlock_irq(&list_lock);
  208. if (dev_get_uevent_suppress(&sch->dev)) {
  209. dev_set_uevent_suppress(&sch->dev, 0);
  210. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  211. }
  212. out:
  213. return ret;
  214. }
  215. static void eadm_quiesce(struct subchannel *sch)
  216. {
  217. struct eadm_private *private = get_eadm_private(sch);
  218. DECLARE_COMPLETION_ONSTACK(completion);
  219. int ret;
  220. spin_lock_irq(sch->lock);
  221. if (private->state != EADM_BUSY)
  222. goto disable;
  223. if (eadm_subchannel_clear(sch))
  224. goto disable;
  225. private->completion = &completion;
  226. spin_unlock_irq(sch->lock);
  227. wait_for_completion_io(&completion);
  228. spin_lock_irq(sch->lock);
  229. private->completion = NULL;
  230. disable:
  231. eadm_subchannel_set_timeout(sch, 0);
  232. do {
  233. ret = cio_disable_subchannel(sch);
  234. } while (ret == -EBUSY);
  235. spin_unlock_irq(sch->lock);
  236. }
  237. static int eadm_subchannel_remove(struct subchannel *sch)
  238. {
  239. struct eadm_private *private = get_eadm_private(sch);
  240. spin_lock_irq(&list_lock);
  241. list_del(&private->head);
  242. spin_unlock_irq(&list_lock);
  243. eadm_quiesce(sch);
  244. spin_lock_irq(sch->lock);
  245. set_eadm_private(sch, NULL);
  246. spin_unlock_irq(sch->lock);
  247. kfree(private);
  248. return 0;
  249. }
  250. static void eadm_subchannel_shutdown(struct subchannel *sch)
  251. {
  252. eadm_quiesce(sch);
  253. }
  254. static int eadm_subchannel_freeze(struct subchannel *sch)
  255. {
  256. return cio_disable_subchannel(sch);
  257. }
  258. static int eadm_subchannel_restore(struct subchannel *sch)
  259. {
  260. return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
  261. }
  262. /**
  263. * eadm_subchannel_sch_event - process subchannel event
  264. * @sch: subchannel
  265. * @process: non-zero if function is called in process context
  266. *
  267. * An unspecified event occurred for this subchannel. Adjust data according
  268. * to the current operational state of the subchannel. Return zero when the
  269. * event has been handled sufficiently or -EAGAIN when this function should
  270. * be called again in process context.
  271. */
  272. static int eadm_subchannel_sch_event(struct subchannel *sch, int process)
  273. {
  274. struct eadm_private *private;
  275. unsigned long flags;
  276. int ret = 0;
  277. spin_lock_irqsave(sch->lock, flags);
  278. if (!device_is_registered(&sch->dev))
  279. goto out_unlock;
  280. if (work_pending(&sch->todo_work))
  281. goto out_unlock;
  282. if (cio_update_schib(sch)) {
  283. css_sched_sch_todo(sch, SCH_TODO_UNREG);
  284. goto out_unlock;
  285. }
  286. private = get_eadm_private(sch);
  287. if (private->state == EADM_NOT_OPER)
  288. private->state = EADM_IDLE;
  289. out_unlock:
  290. spin_unlock_irqrestore(sch->lock, flags);
  291. return ret;
  292. }
  293. static struct css_device_id eadm_subchannel_ids[] = {
  294. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_ADM, },
  295. { /* end of list */ },
  296. };
  297. MODULE_DEVICE_TABLE(css, eadm_subchannel_ids);
  298. static struct css_driver eadm_subchannel_driver = {
  299. .drv = {
  300. .name = "eadm_subchannel",
  301. .owner = THIS_MODULE,
  302. },
  303. .subchannel_type = eadm_subchannel_ids,
  304. .irq = eadm_subchannel_irq,
  305. .probe = eadm_subchannel_probe,
  306. .remove = eadm_subchannel_remove,
  307. .shutdown = eadm_subchannel_shutdown,
  308. .sch_event = eadm_subchannel_sch_event,
  309. .freeze = eadm_subchannel_freeze,
  310. .thaw = eadm_subchannel_restore,
  311. .restore = eadm_subchannel_restore,
  312. };
  313. static struct eadm_ops eadm_ops = {
  314. .eadm_start = eadm_start_aob,
  315. .owner = THIS_MODULE,
  316. };
  317. static int __init eadm_sch_init(void)
  318. {
  319. int ret;
  320. if (!css_general_characteristics.eadm)
  321. return -ENXIO;
  322. eadm_debug = debug_register("eadm_log", 16, 1, 16);
  323. if (!eadm_debug)
  324. return -ENOMEM;
  325. debug_register_view(eadm_debug, &debug_hex_ascii_view);
  326. debug_set_level(eadm_debug, 2);
  327. isc_register(EADM_SCH_ISC);
  328. ret = css_driver_register(&eadm_subchannel_driver);
  329. if (ret)
  330. goto cleanup;
  331. register_eadm_ops(&eadm_ops);
  332. return ret;
  333. cleanup:
  334. isc_unregister(EADM_SCH_ISC);
  335. debug_unregister(eadm_debug);
  336. return ret;
  337. }
  338. static void __exit eadm_sch_exit(void)
  339. {
  340. unregister_eadm_ops(&eadm_ops);
  341. css_driver_unregister(&eadm_subchannel_driver);
  342. isc_unregister(EADM_SCH_ISC);
  343. debug_unregister(eadm_debug);
  344. }
  345. module_init(eadm_sch_init);
  346. module_exit(eadm_sch_exit);