eadm_sch.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 (7 * 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 = this_cpu_ptr(&cio_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. 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. EXPORT_SYMBOL_GPL(eadm_start_aob);
  184. static int eadm_subchannel_probe(struct subchannel *sch)
  185. {
  186. struct eadm_private *private;
  187. int ret;
  188. private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
  189. if (!private)
  190. return -ENOMEM;
  191. INIT_LIST_HEAD(&private->head);
  192. init_timer(&private->timer);
  193. spin_lock_irq(sch->lock);
  194. set_eadm_private(sch, private);
  195. private->state = EADM_IDLE;
  196. private->sch = sch;
  197. sch->isc = EADM_SCH_ISC;
  198. ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
  199. if (ret) {
  200. set_eadm_private(sch, NULL);
  201. spin_unlock_irq(sch->lock);
  202. kfree(private);
  203. goto out;
  204. }
  205. spin_unlock_irq(sch->lock);
  206. spin_lock_irq(&list_lock);
  207. list_add(&private->head, &eadm_list);
  208. spin_unlock_irq(&list_lock);
  209. if (dev_get_uevent_suppress(&sch->dev)) {
  210. dev_set_uevent_suppress(&sch->dev, 0);
  211. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  212. }
  213. out:
  214. return ret;
  215. }
  216. static void eadm_quiesce(struct subchannel *sch)
  217. {
  218. struct eadm_private *private = get_eadm_private(sch);
  219. DECLARE_COMPLETION_ONSTACK(completion);
  220. int ret;
  221. spin_lock_irq(sch->lock);
  222. if (private->state != EADM_BUSY)
  223. goto disable;
  224. if (eadm_subchannel_clear(sch))
  225. goto disable;
  226. private->completion = &completion;
  227. spin_unlock_irq(sch->lock);
  228. wait_for_completion_io(&completion);
  229. spin_lock_irq(sch->lock);
  230. private->completion = NULL;
  231. disable:
  232. eadm_subchannel_set_timeout(sch, 0);
  233. do {
  234. ret = cio_disable_subchannel(sch);
  235. } while (ret == -EBUSY);
  236. spin_unlock_irq(sch->lock);
  237. }
  238. static int eadm_subchannel_remove(struct subchannel *sch)
  239. {
  240. struct eadm_private *private = get_eadm_private(sch);
  241. spin_lock_irq(&list_lock);
  242. list_del(&private->head);
  243. spin_unlock_irq(&list_lock);
  244. eadm_quiesce(sch);
  245. spin_lock_irq(sch->lock);
  246. set_eadm_private(sch, NULL);
  247. spin_unlock_irq(sch->lock);
  248. kfree(private);
  249. return 0;
  250. }
  251. static void eadm_subchannel_shutdown(struct subchannel *sch)
  252. {
  253. eadm_quiesce(sch);
  254. }
  255. static int eadm_subchannel_freeze(struct subchannel *sch)
  256. {
  257. return cio_disable_subchannel(sch);
  258. }
  259. static int eadm_subchannel_restore(struct subchannel *sch)
  260. {
  261. return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
  262. }
  263. /**
  264. * eadm_subchannel_sch_event - process subchannel event
  265. * @sch: subchannel
  266. * @process: non-zero if function is called in process context
  267. *
  268. * An unspecified event occurred for this subchannel. Adjust data according
  269. * to the current operational state of the subchannel. Return zero when the
  270. * event has been handled sufficiently or -EAGAIN when this function should
  271. * be called again in process context.
  272. */
  273. static int eadm_subchannel_sch_event(struct subchannel *sch, int process)
  274. {
  275. struct eadm_private *private;
  276. unsigned long flags;
  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 0;
  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 int __init eadm_sch_init(void)
  314. {
  315. int ret;
  316. if (!css_general_characteristics.eadm)
  317. return -ENXIO;
  318. eadm_debug = debug_register("eadm_log", 16, 1, 16);
  319. if (!eadm_debug)
  320. return -ENOMEM;
  321. debug_register_view(eadm_debug, &debug_hex_ascii_view);
  322. debug_set_level(eadm_debug, 2);
  323. isc_register(EADM_SCH_ISC);
  324. ret = css_driver_register(&eadm_subchannel_driver);
  325. if (ret)
  326. goto cleanup;
  327. return ret;
  328. cleanup:
  329. isc_unregister(EADM_SCH_ISC);
  330. debug_unregister(eadm_debug);
  331. return ret;
  332. }
  333. static void __exit eadm_sch_exit(void)
  334. {
  335. css_driver_unregister(&eadm_subchannel_driver);
  336. isc_unregister(EADM_SCH_ISC);
  337. debug_unregister(eadm_debug);
  338. }
  339. module_init(eadm_sch_init);
  340. module_exit(eadm_sch_exit);