eadm_sch.c 9.1 KB

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