1724117b7SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2eadb86abSSebastian Ott /* 3eadb86abSSebastian Ott * Driver for s390 eadm subchannels 4eadb86abSSebastian Ott * 5eadb86abSSebastian Ott * Copyright IBM Corp. 2012 6eadb86abSSebastian Ott * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com> 7eadb86abSSebastian Ott */ 8eadb86abSSebastian Ott 92e73c2cfSSebastian Ott #include <linux/kernel_stat.h> 106aa2677aSSebastian Ott #include <linux/completion.h> 11eadb86abSSebastian Ott #include <linux/workqueue.h> 12eadb86abSSebastian Ott #include <linux/spinlock.h> 13eadb86abSSebastian Ott #include <linux/device.h> 14eadb86abSSebastian Ott #include <linux/module.h> 15eadb86abSSebastian Ott #include <linux/timer.h> 16eadb86abSSebastian Ott #include <linux/slab.h> 17eadb86abSSebastian Ott #include <linux/list.h> 185c2e5a0cSAlexander Gordeev #include <linux/io.h> 19eadb86abSSebastian Ott 20eadb86abSSebastian Ott #include <asm/css_chars.h> 21eadb86abSSebastian Ott #include <asm/debug.h> 22eadb86abSSebastian Ott #include <asm/isc.h> 23eadb86abSSebastian Ott #include <asm/cio.h> 24eadb86abSSebastian Ott #include <asm/scsw.h> 25eadb86abSSebastian Ott #include <asm/eadm.h> 26eadb86abSSebastian Ott 27eadb86abSSebastian Ott #include "eadm_sch.h" 28eadb86abSSebastian Ott #include "ioasm.h" 29eadb86abSSebastian Ott #include "cio.h" 30eadb86abSSebastian Ott #include "css.h" 31eadb86abSSebastian Ott #include "orb.h" 32eadb86abSSebastian Ott 33eadb86abSSebastian Ott MODULE_DESCRIPTION("driver for s390 eadm subchannels"); 34eadb86abSSebastian Ott MODULE_LICENSE("GPL"); 35eadb86abSSebastian Ott 36e2578b82SSebastian Ott #define EADM_TIMEOUT (7 * HZ) 37eadb86abSSebastian Ott static DEFINE_SPINLOCK(list_lock); 38eadb86abSSebastian Ott static LIST_HEAD(eadm_list); 39eadb86abSSebastian Ott 40eadb86abSSebastian Ott static debug_info_t *eadm_debug; 41eadb86abSSebastian Ott 42eadb86abSSebastian Ott #define EADM_LOG(imp, txt) do { \ 43eadb86abSSebastian Ott debug_text_event(eadm_debug, imp, txt); \ 44eadb86abSSebastian Ott } while (0) 45eadb86abSSebastian Ott 46eadb86abSSebastian Ott static void EADM_LOG_HEX(int level, void *data, int length) 47eadb86abSSebastian Ott { 48eadb86abSSebastian Ott debug_event(eadm_debug, level, data, length); 49eadb86abSSebastian Ott } 50eadb86abSSebastian Ott 51eadb86abSSebastian Ott static void orb_init(union orb *orb) 52eadb86abSSebastian Ott { 53eadb86abSSebastian Ott memset(orb, 0, sizeof(union orb)); 54eadb86abSSebastian Ott orb->eadm.compat1 = 1; 55eadb86abSSebastian Ott orb->eadm.compat2 = 1; 56eadb86abSSebastian Ott orb->eadm.fmt = 1; 57eadb86abSSebastian Ott orb->eadm.x = 1; 58eadb86abSSebastian Ott } 59eadb86abSSebastian Ott 60eadb86abSSebastian Ott static int eadm_subchannel_start(struct subchannel *sch, struct aob *aob) 61eadb86abSSebastian Ott { 62eadb86abSSebastian Ott union orb *orb = &get_eadm_private(sch)->orb; 63eadb86abSSebastian Ott int cc; 64eadb86abSSebastian Ott 65eadb86abSSebastian Ott orb_init(orb); 66*9ff91a33SHeiko Carstens orb->eadm.aob = virt_to_dma32(aob); 675c2e5a0cSAlexander Gordeev orb->eadm.intparm = (u32)virt_to_phys(sch); 68eadb86abSSebastian Ott orb->eadm.key = PAGE_DEFAULT_KEY >> 4; 69eadb86abSSebastian Ott 70eadb86abSSebastian Ott EADM_LOG(6, "start"); 71eadb86abSSebastian Ott EADM_LOG_HEX(6, &sch->schid, sizeof(sch->schid)); 72eadb86abSSebastian Ott 73eadb86abSSebastian Ott cc = ssch(sch->schid, orb); 74eadb86abSSebastian Ott switch (cc) { 75eadb86abSSebastian Ott case 0: 76eadb86abSSebastian Ott sch->schib.scsw.eadm.actl |= SCSW_ACTL_START_PEND; 77eadb86abSSebastian Ott break; 78eadb86abSSebastian Ott case 1: /* status pending */ 79eadb86abSSebastian Ott case 2: /* busy */ 80eadb86abSSebastian Ott return -EBUSY; 81eadb86abSSebastian Ott case 3: /* not operational */ 82eadb86abSSebastian Ott return -ENODEV; 83eadb86abSSebastian Ott } 84eadb86abSSebastian Ott return 0; 85eadb86abSSebastian Ott } 86eadb86abSSebastian Ott 87eadb86abSSebastian Ott static int eadm_subchannel_clear(struct subchannel *sch) 88eadb86abSSebastian Ott { 89eadb86abSSebastian Ott int cc; 90eadb86abSSebastian Ott 91eadb86abSSebastian Ott cc = csch(sch->schid); 92eadb86abSSebastian Ott if (cc) 93eadb86abSSebastian Ott return -ENODEV; 94eadb86abSSebastian Ott 95eadb86abSSebastian Ott sch->schib.scsw.eadm.actl |= SCSW_ACTL_CLEAR_PEND; 96eadb86abSSebastian Ott return 0; 97eadb86abSSebastian Ott } 98eadb86abSSebastian Ott 99846d0c6fSKees Cook static void eadm_subchannel_timeout(struct timer_list *t) 100eadb86abSSebastian Ott { 101846d0c6fSKees Cook struct eadm_private *private = from_timer(private, t, timer); 102846d0c6fSKees Cook struct subchannel *sch = private->sch; 103eadb86abSSebastian Ott 104b8fa3e90SHalil Pasic spin_lock_irq(&sch->lock); 105eadb86abSSebastian Ott EADM_LOG(1, "timeout"); 106eadb86abSSebastian Ott EADM_LOG_HEX(1, &sch->schid, sizeof(sch->schid)); 107eadb86abSSebastian Ott if (eadm_subchannel_clear(sch)) 108eadb86abSSebastian Ott EADM_LOG(0, "clear failed"); 109b8fa3e90SHalil Pasic spin_unlock_irq(&sch->lock); 110eadb86abSSebastian Ott } 111eadb86abSSebastian Ott 112eadb86abSSebastian Ott static void eadm_subchannel_set_timeout(struct subchannel *sch, int expires) 113eadb86abSSebastian Ott { 114eadb86abSSebastian Ott struct eadm_private *private = get_eadm_private(sch); 115eadb86abSSebastian Ott 1164f45c37fSYu Liao if (expires == 0) 117eadb86abSSebastian Ott del_timer(&private->timer); 1184f45c37fSYu Liao else 1194f45c37fSYu Liao mod_timer(&private->timer, jiffies + expires); 120eadb86abSSebastian Ott } 121eadb86abSSebastian Ott 122eadb86abSSebastian Ott static void eadm_subchannel_irq(struct subchannel *sch) 123eadb86abSSebastian Ott { 124eadb86abSSebastian Ott struct eadm_private *private = get_eadm_private(sch); 125eadb86abSSebastian Ott struct eadm_scsw *scsw = &sch->schib.scsw.eadm; 1260bf7fcf1SChristoph Lameter struct irb *irb = this_cpu_ptr(&cio_irb); 1272a842acaSChristoph Hellwig blk_status_t error = BLK_STS_OK; 128eadb86abSSebastian Ott 129eadb86abSSebastian Ott EADM_LOG(6, "irq"); 130eadb86abSSebastian Ott EADM_LOG_HEX(6, irb, sizeof(*irb)); 131eadb86abSSebastian Ott 132420f42ecSHeiko Carstens inc_irq_stat(IRQIO_ADM); 1332e73c2cfSSebastian Ott 134eadb86abSSebastian Ott if ((scsw->stctl & (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) 135eadb86abSSebastian Ott && scsw->eswf == 1 && irb->esw.eadm.erw.r) 1362a842acaSChristoph Hellwig error = BLK_STS_IOERR; 137eadb86abSSebastian Ott 138eadb86abSSebastian Ott if (scsw->fctl & SCSW_FCTL_CLEAR_FUNC) 1392a842acaSChristoph Hellwig error = BLK_STS_TIMEOUT; 140eadb86abSSebastian Ott 141eadb86abSSebastian Ott eadm_subchannel_set_timeout(sch, 0); 142eadb86abSSebastian Ott 143eadb86abSSebastian Ott if (private->state != EADM_BUSY) { 144eadb86abSSebastian Ott EADM_LOG(1, "irq unsol"); 145eadb86abSSebastian Ott EADM_LOG_HEX(1, irb, sizeof(*irb)); 146eadb86abSSebastian Ott private->state = EADM_NOT_OPER; 147eadb86abSSebastian Ott css_sched_sch_todo(sch, SCH_TODO_EVAL); 148eadb86abSSebastian Ott return; 149eadb86abSSebastian Ott } 150*9ff91a33SHeiko Carstens scm_irq_handler(dma32_to_virt(scsw->aob), error); 151eadb86abSSebastian Ott private->state = EADM_IDLE; 1526aa2677aSSebastian Ott 1536aa2677aSSebastian Ott if (private->completion) 1546aa2677aSSebastian Ott complete(private->completion); 155eadb86abSSebastian Ott } 156eadb86abSSebastian Ott 157eadb86abSSebastian Ott static struct subchannel *eadm_get_idle_sch(void) 158eadb86abSSebastian Ott { 159eadb86abSSebastian Ott struct eadm_private *private; 160eadb86abSSebastian Ott struct subchannel *sch; 161eadb86abSSebastian Ott unsigned long flags; 162eadb86abSSebastian Ott 163eadb86abSSebastian Ott spin_lock_irqsave(&list_lock, flags); 164eadb86abSSebastian Ott list_for_each_entry(private, &eadm_list, head) { 165eadb86abSSebastian Ott sch = private->sch; 166b8fa3e90SHalil Pasic spin_lock(&sch->lock); 167eadb86abSSebastian Ott if (private->state == EADM_IDLE) { 168eadb86abSSebastian Ott private->state = EADM_BUSY; 169eadb86abSSebastian Ott list_move_tail(&private->head, &eadm_list); 170b8fa3e90SHalil Pasic spin_unlock(&sch->lock); 171eadb86abSSebastian Ott spin_unlock_irqrestore(&list_lock, flags); 172eadb86abSSebastian Ott 173eadb86abSSebastian Ott return sch; 174eadb86abSSebastian Ott } 175b8fa3e90SHalil Pasic spin_unlock(&sch->lock); 176eadb86abSSebastian Ott } 177eadb86abSSebastian Ott spin_unlock_irqrestore(&list_lock, flags); 178eadb86abSSebastian Ott 179eadb86abSSebastian Ott return NULL; 180eadb86abSSebastian Ott } 181eadb86abSSebastian Ott 182605c3698SSebastian Ott int eadm_start_aob(struct aob *aob) 183eadb86abSSebastian Ott { 184eadb86abSSebastian Ott struct eadm_private *private; 185eadb86abSSebastian Ott struct subchannel *sch; 186eadb86abSSebastian Ott unsigned long flags; 187eadb86abSSebastian Ott int ret; 188eadb86abSSebastian Ott 189eadb86abSSebastian Ott sch = eadm_get_idle_sch(); 190eadb86abSSebastian Ott if (!sch) 191eadb86abSSebastian Ott return -EBUSY; 192eadb86abSSebastian Ott 193b8fa3e90SHalil Pasic spin_lock_irqsave(&sch->lock, flags); 194eadb86abSSebastian Ott eadm_subchannel_set_timeout(sch, EADM_TIMEOUT); 195eadb86abSSebastian Ott ret = eadm_subchannel_start(sch, aob); 196eadb86abSSebastian Ott if (!ret) 197eadb86abSSebastian Ott goto out_unlock; 198eadb86abSSebastian Ott 199eadb86abSSebastian Ott /* Handle start subchannel failure. */ 200eadb86abSSebastian Ott eadm_subchannel_set_timeout(sch, 0); 201eadb86abSSebastian Ott private = get_eadm_private(sch); 202eadb86abSSebastian Ott private->state = EADM_NOT_OPER; 203eadb86abSSebastian Ott css_sched_sch_todo(sch, SCH_TODO_EVAL); 204eadb86abSSebastian Ott 205eadb86abSSebastian Ott out_unlock: 206b8fa3e90SHalil Pasic spin_unlock_irqrestore(&sch->lock, flags); 207eadb86abSSebastian Ott 208eadb86abSSebastian Ott return ret; 209eadb86abSSebastian Ott } 210605c3698SSebastian Ott EXPORT_SYMBOL_GPL(eadm_start_aob); 211eadb86abSSebastian Ott 212eadb86abSSebastian Ott static int eadm_subchannel_probe(struct subchannel *sch) 213eadb86abSSebastian Ott { 214eadb86abSSebastian Ott struct eadm_private *private; 215eadb86abSSebastian Ott int ret; 216eadb86abSSebastian Ott 217eadb86abSSebastian Ott private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA); 218eadb86abSSebastian Ott if (!private) 219eadb86abSSebastian Ott return -ENOMEM; 220eadb86abSSebastian Ott 221eadb86abSSebastian Ott INIT_LIST_HEAD(&private->head); 222846d0c6fSKees Cook timer_setup(&private->timer, eadm_subchannel_timeout, 0); 223eadb86abSSebastian Ott 224b8fa3e90SHalil Pasic spin_lock_irq(&sch->lock); 225eadb86abSSebastian Ott set_eadm_private(sch, private); 226eadb86abSSebastian Ott private->state = EADM_IDLE; 227eadb86abSSebastian Ott private->sch = sch; 228eadb86abSSebastian Ott sch->isc = EADM_SCH_ISC; 2295c2e5a0cSAlexander Gordeev ret = cio_enable_subchannel(sch, (u32)virt_to_phys(sch)); 230eadb86abSSebastian Ott if (ret) { 231eadb86abSSebastian Ott set_eadm_private(sch, NULL); 232b8fa3e90SHalil Pasic spin_unlock_irq(&sch->lock); 233eadb86abSSebastian Ott kfree(private); 234eadb86abSSebastian Ott goto out; 235eadb86abSSebastian Ott } 236b8fa3e90SHalil Pasic spin_unlock_irq(&sch->lock); 237eadb86abSSebastian Ott 238eadb86abSSebastian Ott spin_lock_irq(&list_lock); 239eadb86abSSebastian Ott list_add(&private->head, &eadm_list); 240eadb86abSSebastian Ott spin_unlock_irq(&list_lock); 241eadb86abSSebastian Ott out: 242eadb86abSSebastian Ott return ret; 243eadb86abSSebastian Ott } 244eadb86abSSebastian Ott 245eadb86abSSebastian Ott static void eadm_quiesce(struct subchannel *sch) 246eadb86abSSebastian Ott { 2476aa2677aSSebastian Ott struct eadm_private *private = get_eadm_private(sch); 2486aa2677aSSebastian Ott DECLARE_COMPLETION_ONSTACK(completion); 249eadb86abSSebastian Ott int ret; 250eadb86abSSebastian Ott 251b8fa3e90SHalil Pasic spin_lock_irq(&sch->lock); 2526aa2677aSSebastian Ott if (private->state != EADM_BUSY) 2536aa2677aSSebastian Ott goto disable; 2546aa2677aSSebastian Ott 2556aa2677aSSebastian Ott if (eadm_subchannel_clear(sch)) 2566aa2677aSSebastian Ott goto disable; 2576aa2677aSSebastian Ott 2586aa2677aSSebastian Ott private->completion = &completion; 259b8fa3e90SHalil Pasic spin_unlock_irq(&sch->lock); 2606aa2677aSSebastian Ott 2616aa2677aSSebastian Ott wait_for_completion_io(&completion); 2626aa2677aSSebastian Ott 263b8fa3e90SHalil Pasic spin_lock_irq(&sch->lock); 2646aa2677aSSebastian Ott private->completion = NULL; 2656aa2677aSSebastian Ott 2666aa2677aSSebastian Ott disable: 2676aa2677aSSebastian Ott eadm_subchannel_set_timeout(sch, 0); 2686aa2677aSSebastian Ott do { 2696aa2677aSSebastian Ott ret = cio_disable_subchannel(sch); 270eadb86abSSebastian Ott } while (ret == -EBUSY); 2716aa2677aSSebastian Ott 272b8fa3e90SHalil Pasic spin_unlock_irq(&sch->lock); 273eadb86abSSebastian Ott } 274eadb86abSSebastian Ott 275a7bdb9a9SUwe Kleine-König static void eadm_subchannel_remove(struct subchannel *sch) 276eadb86abSSebastian Ott { 277eadb86abSSebastian Ott struct eadm_private *private = get_eadm_private(sch); 278eadb86abSSebastian Ott 279eadb86abSSebastian Ott spin_lock_irq(&list_lock); 280eadb86abSSebastian Ott list_del(&private->head); 281eadb86abSSebastian Ott spin_unlock_irq(&list_lock); 282eadb86abSSebastian Ott 283eadb86abSSebastian Ott eadm_quiesce(sch); 284eadb86abSSebastian Ott 285b8fa3e90SHalil Pasic spin_lock_irq(&sch->lock); 286eadb86abSSebastian Ott set_eadm_private(sch, NULL); 287b8fa3e90SHalil Pasic spin_unlock_irq(&sch->lock); 288eadb86abSSebastian Ott 289eadb86abSSebastian Ott kfree(private); 290eadb86abSSebastian Ott } 291eadb86abSSebastian Ott 292eadb86abSSebastian Ott static void eadm_subchannel_shutdown(struct subchannel *sch) 293eadb86abSSebastian Ott { 294eadb86abSSebastian Ott eadm_quiesce(sch); 295eadb86abSSebastian Ott } 296eadb86abSSebastian Ott 297eadb86abSSebastian Ott /** 298eadb86abSSebastian Ott * eadm_subchannel_sch_event - process subchannel event 299eadb86abSSebastian Ott * @sch: subchannel 300eadb86abSSebastian Ott * @process: non-zero if function is called in process context 301eadb86abSSebastian Ott * 302eadb86abSSebastian Ott * An unspecified event occurred for this subchannel. Adjust data according 303eadb86abSSebastian Ott * to the current operational state of the subchannel. Return zero when the 304eadb86abSSebastian Ott * event has been handled sufficiently or -EAGAIN when this function should 305eadb86abSSebastian Ott * be called again in process context. 306eadb86abSSebastian Ott */ 307eadb86abSSebastian Ott static int eadm_subchannel_sch_event(struct subchannel *sch, int process) 308eadb86abSSebastian Ott { 309eadb86abSSebastian Ott struct eadm_private *private; 310eadb86abSSebastian Ott unsigned long flags; 311eadb86abSSebastian Ott 312b8fa3e90SHalil Pasic spin_lock_irqsave(&sch->lock, flags); 313eadb86abSSebastian Ott if (!device_is_registered(&sch->dev)) 314eadb86abSSebastian Ott goto out_unlock; 315eadb86abSSebastian Ott 316eadb86abSSebastian Ott if (work_pending(&sch->todo_work)) 317eadb86abSSebastian Ott goto out_unlock; 318eadb86abSSebastian Ott 319eadb86abSSebastian Ott if (cio_update_schib(sch)) { 320eadb86abSSebastian Ott css_sched_sch_todo(sch, SCH_TODO_UNREG); 321eadb86abSSebastian Ott goto out_unlock; 322eadb86abSSebastian Ott } 323eadb86abSSebastian Ott private = get_eadm_private(sch); 324eadb86abSSebastian Ott if (private->state == EADM_NOT_OPER) 325eadb86abSSebastian Ott private->state = EADM_IDLE; 326eadb86abSSebastian Ott 327eadb86abSSebastian Ott out_unlock: 328b8fa3e90SHalil Pasic spin_unlock_irqrestore(&sch->lock, flags); 329eadb86abSSebastian Ott 330b2f4de8bSPeter Senna Tschudin return 0; 331eadb86abSSebastian Ott } 332eadb86abSSebastian Ott 333eadb86abSSebastian Ott static struct css_device_id eadm_subchannel_ids[] = { 334eadb86abSSebastian Ott { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_ADM, }, 335eadb86abSSebastian Ott { /* end of list */ }, 336eadb86abSSebastian Ott }; 337eadb86abSSebastian Ott MODULE_DEVICE_TABLE(css, eadm_subchannel_ids); 338eadb86abSSebastian Ott 339eadb86abSSebastian Ott static struct css_driver eadm_subchannel_driver = { 340eadb86abSSebastian Ott .drv = { 341eadb86abSSebastian Ott .name = "eadm_subchannel", 342eadb86abSSebastian Ott .owner = THIS_MODULE, 343eadb86abSSebastian Ott }, 344eadb86abSSebastian Ott .subchannel_type = eadm_subchannel_ids, 345eadb86abSSebastian Ott .irq = eadm_subchannel_irq, 346eadb86abSSebastian Ott .probe = eadm_subchannel_probe, 347eadb86abSSebastian Ott .remove = eadm_subchannel_remove, 348eadb86abSSebastian Ott .shutdown = eadm_subchannel_shutdown, 349eadb86abSSebastian Ott .sch_event = eadm_subchannel_sch_event, 350eadb86abSSebastian Ott }; 351eadb86abSSebastian Ott 352eadb86abSSebastian Ott static int __init eadm_sch_init(void) 353eadb86abSSebastian Ott { 354eadb86abSSebastian Ott int ret; 355eadb86abSSebastian Ott 356eadb86abSSebastian Ott if (!css_general_characteristics.eadm) 357eadb86abSSebastian Ott return -ENXIO; 358eadb86abSSebastian Ott 359eadb86abSSebastian Ott eadm_debug = debug_register("eadm_log", 16, 1, 16); 360eadb86abSSebastian Ott if (!eadm_debug) 361eadb86abSSebastian Ott return -ENOMEM; 362eadb86abSSebastian Ott 363eadb86abSSebastian Ott debug_register_view(eadm_debug, &debug_hex_ascii_view); 364eadb86abSSebastian Ott debug_set_level(eadm_debug, 2); 365eadb86abSSebastian Ott 366eadb86abSSebastian Ott isc_register(EADM_SCH_ISC); 367eadb86abSSebastian Ott ret = css_driver_register(&eadm_subchannel_driver); 368eadb86abSSebastian Ott if (ret) 369eadb86abSSebastian Ott goto cleanup; 370eadb86abSSebastian Ott 371eadb86abSSebastian Ott return ret; 372eadb86abSSebastian Ott 373eadb86abSSebastian Ott cleanup: 374eadb86abSSebastian Ott isc_unregister(EADM_SCH_ISC); 375eadb86abSSebastian Ott debug_unregister(eadm_debug); 376eadb86abSSebastian Ott return ret; 377eadb86abSSebastian Ott } 378eadb86abSSebastian Ott 379eadb86abSSebastian Ott static void __exit eadm_sch_exit(void) 380eadb86abSSebastian Ott { 381eadb86abSSebastian Ott css_driver_unregister(&eadm_subchannel_driver); 382eadb86abSSebastian Ott isc_unregister(EADM_SCH_ISC); 383eadb86abSSebastian Ott debug_unregister(eadm_debug); 384eadb86abSSebastian Ott } 385eadb86abSSebastian Ott module_init(eadm_sch_init); 386eadb86abSSebastian Ott module_exit(eadm_sch_exit); 387