eadm_sch.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <[email protected]>
  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. else
  98. mod_timer(&private->timer, jiffies + expires);
  99. }
  100. static void eadm_subchannel_irq(struct subchannel *sch)
  101. {
  102. struct eadm_private *private = get_eadm_private(sch);
  103. struct eadm_scsw *scsw = &sch->schib.scsw.eadm;
  104. struct irb *irb = this_cpu_ptr(&cio_irb);
  105. blk_status_t error = BLK_STS_OK;
  106. EADM_LOG(6, "irq");
  107. EADM_LOG_HEX(6, irb, sizeof(*irb));
  108. inc_irq_stat(IRQIO_ADM);
  109. if ((scsw->stctl & (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))
  110. && scsw->eswf == 1 && irb->esw.eadm.erw.r)
  111. error = BLK_STS_IOERR;
  112. if (scsw->fctl & SCSW_FCTL_CLEAR_FUNC)
  113. error = BLK_STS_TIMEOUT;
  114. eadm_subchannel_set_timeout(sch, 0);
  115. if (private->state != EADM_BUSY) {
  116. EADM_LOG(1, "irq unsol");
  117. EADM_LOG_HEX(1, irb, sizeof(*irb));
  118. private->state = EADM_NOT_OPER;
  119. css_sched_sch_todo(sch, SCH_TODO_EVAL);
  120. return;
  121. }
  122. scm_irq_handler((struct aob *)(unsigned long)scsw->aob, error);
  123. private->state = EADM_IDLE;
  124. if (private->completion)
  125. complete(private->completion);
  126. }
  127. static struct subchannel *eadm_get_idle_sch(void)
  128. {
  129. struct eadm_private *private;
  130. struct subchannel *sch;
  131. unsigned long flags;
  132. spin_lock_irqsave(&list_lock, flags);
  133. list_for_each_entry(private, &eadm_list, head) {
  134. sch = private->sch;
  135. spin_lock(sch->lock);
  136. if (private->state == EADM_IDLE) {
  137. private->state = EADM_BUSY;
  138. list_move_tail(&private->head, &eadm_list);
  139. spin_unlock(sch->lock);
  140. spin_unlock_irqrestore(&list_lock, flags);
  141. return sch;
  142. }
  143. spin_unlock(sch->lock);
  144. }
  145. spin_unlock_irqrestore(&list_lock, flags);
  146. return NULL;
  147. }
  148. int eadm_start_aob(struct aob *aob)
  149. {
  150. struct eadm_private *private;
  151. struct subchannel *sch;
  152. unsigned long flags;
  153. int ret;
  154. sch = eadm_get_idle_sch();
  155. if (!sch)
  156. return -EBUSY;
  157. spin_lock_irqsave(sch->lock, flags);
  158. eadm_subchannel_set_timeout(sch, EADM_TIMEOUT);
  159. ret = eadm_subchannel_start(sch, aob);
  160. if (!ret)
  161. goto out_unlock;
  162. /* Handle start subchannel failure. */
  163. eadm_subchannel_set_timeout(sch, 0);
  164. private = get_eadm_private(sch);
  165. private->state = EADM_NOT_OPER;
  166. css_sched_sch_todo(sch, SCH_TODO_EVAL);
  167. out_unlock:
  168. spin_unlock_irqrestore(sch->lock, flags);
  169. return ret;
  170. }
  171. EXPORT_SYMBOL_GPL(eadm_start_aob);
  172. static int eadm_subchannel_probe(struct subchannel *sch)
  173. {
  174. struct eadm_private *private;
  175. int ret;
  176. private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
  177. if (!private)
  178. return -ENOMEM;
  179. INIT_LIST_HEAD(&private->head);
  180. timer_setup(&private->timer, eadm_subchannel_timeout, 0);
  181. spin_lock_irq(sch->lock);
  182. set_eadm_private(sch, private);
  183. private->state = EADM_IDLE;
  184. private->sch = sch;
  185. sch->isc = EADM_SCH_ISC;
  186. ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
  187. if (ret) {
  188. set_eadm_private(sch, NULL);
  189. spin_unlock_irq(sch->lock);
  190. kfree(private);
  191. goto out;
  192. }
  193. spin_unlock_irq(sch->lock);
  194. spin_lock_irq(&list_lock);
  195. list_add(&private->head, &eadm_list);
  196. spin_unlock_irq(&list_lock);
  197. out:
  198. return ret;
  199. }
  200. static void eadm_quiesce(struct subchannel *sch)
  201. {
  202. struct eadm_private *private = get_eadm_private(sch);
  203. DECLARE_COMPLETION_ONSTACK(completion);
  204. int ret;
  205. spin_lock_irq(sch->lock);
  206. if (private->state != EADM_BUSY)
  207. goto disable;
  208. if (eadm_subchannel_clear(sch))
  209. goto disable;
  210. private->completion = &completion;
  211. spin_unlock_irq(sch->lock);
  212. wait_for_completion_io(&completion);
  213. spin_lock_irq(sch->lock);
  214. private->completion = NULL;
  215. disable:
  216. eadm_subchannel_set_timeout(sch, 0);
  217. do {
  218. ret = cio_disable_subchannel(sch);
  219. } while (ret == -EBUSY);
  220. spin_unlock_irq(sch->lock);
  221. }
  222. static void eadm_subchannel_remove(struct subchannel *sch)
  223. {
  224. struct eadm_private *private = get_eadm_private(sch);
  225. spin_lock_irq(&list_lock);
  226. list_del(&private->head);
  227. spin_unlock_irq(&list_lock);
  228. eadm_quiesce(sch);
  229. spin_lock_irq(sch->lock);
  230. set_eadm_private(sch, NULL);
  231. spin_unlock_irq(sch->lock);
  232. kfree(private);
  233. }
  234. static void eadm_subchannel_shutdown(struct subchannel *sch)
  235. {
  236. eadm_quiesce(sch);
  237. }
  238. /**
  239. * eadm_subchannel_sch_event - process subchannel event
  240. * @sch: subchannel
  241. * @process: non-zero if function is called in process context
  242. *
  243. * An unspecified event occurred for this subchannel. Adjust data according
  244. * to the current operational state of the subchannel. Return zero when the
  245. * event has been handled sufficiently or -EAGAIN when this function should
  246. * be called again in process context.
  247. */
  248. static int eadm_subchannel_sch_event(struct subchannel *sch, int process)
  249. {
  250. struct eadm_private *private;
  251. unsigned long flags;
  252. spin_lock_irqsave(sch->lock, flags);
  253. if (!device_is_registered(&sch->dev))
  254. goto out_unlock;
  255. if (work_pending(&sch->todo_work))
  256. goto out_unlock;
  257. if (cio_update_schib(sch)) {
  258. css_sched_sch_todo(sch, SCH_TODO_UNREG);
  259. goto out_unlock;
  260. }
  261. private = get_eadm_private(sch);
  262. if (private->state == EADM_NOT_OPER)
  263. private->state = EADM_IDLE;
  264. out_unlock:
  265. spin_unlock_irqrestore(sch->lock, flags);
  266. return 0;
  267. }
  268. static struct css_device_id eadm_subchannel_ids[] = {
  269. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_ADM, },
  270. { /* end of list */ },
  271. };
  272. MODULE_DEVICE_TABLE(css, eadm_subchannel_ids);
  273. static struct css_driver eadm_subchannel_driver = {
  274. .drv = {
  275. .name = "eadm_subchannel",
  276. .owner = THIS_MODULE,
  277. },
  278. .subchannel_type = eadm_subchannel_ids,
  279. .irq = eadm_subchannel_irq,
  280. .probe = eadm_subchannel_probe,
  281. .remove = eadm_subchannel_remove,
  282. .shutdown = eadm_subchannel_shutdown,
  283. .sch_event = eadm_subchannel_sch_event,
  284. };
  285. static int __init eadm_sch_init(void)
  286. {
  287. int ret;
  288. if (!css_general_characteristics.eadm)
  289. return -ENXIO;
  290. eadm_debug = debug_register("eadm_log", 16, 1, 16);
  291. if (!eadm_debug)
  292. return -ENOMEM;
  293. debug_register_view(eadm_debug, &debug_hex_ascii_view);
  294. debug_set_level(eadm_debug, 2);
  295. isc_register(EADM_SCH_ISC);
  296. ret = css_driver_register(&eadm_subchannel_driver);
  297. if (ret)
  298. goto cleanup;
  299. return ret;
  300. cleanup:
  301. isc_unregister(EADM_SCH_ISC);
  302. debug_unregister(eadm_debug);
  303. return ret;
  304. }
  305. static void __exit eadm_sch_exit(void)
  306. {
  307. css_driver_unregister(&eadm_subchannel_driver);
  308. isc_unregister(EADM_SCH_ISC);
  309. debug_unregister(eadm_debug);
  310. }
  311. module_init(eadm_sch_init);
  312. module_exit(eadm_sch_exit);