eadm_sch.c 8.8 KB

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