Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * VFIO based Physical Subchannel device driver |
| 3 | * |
| 4 | * Copyright IBM Corp. 2017 |
| 5 | * |
| 6 | * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> |
| 7 | * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/slab.h> |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 14 | #include <linux/uuid.h> |
| 15 | #include <linux/mdev.h> |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 16 | |
| 17 | #include <asm/isc.h> |
| 18 | |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 19 | #include "ioasm.h" |
| 20 | #include "css.h" |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 21 | #include "vfio_ccw_private.h" |
| 22 | |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 23 | struct workqueue_struct *vfio_ccw_work_q; |
| 24 | |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 25 | /* |
| 26 | * Helpers |
| 27 | */ |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 28 | int vfio_ccw_sch_quiesce(struct subchannel *sch) |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 29 | { |
| 30 | struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev); |
| 31 | DECLARE_COMPLETION_ONSTACK(completion); |
| 32 | int iretry, ret = 0; |
| 33 | |
| 34 | spin_lock_irq(sch->lock); |
| 35 | if (!sch->schib.pmcw.ena) |
| 36 | goto out_unlock; |
| 37 | ret = cio_disable_subchannel(sch); |
| 38 | if (ret != -EBUSY) |
| 39 | goto out_unlock; |
| 40 | |
| 41 | do { |
| 42 | iretry = 255; |
| 43 | |
| 44 | ret = cio_cancel_halt_clear(sch, &iretry); |
| 45 | while (ret == -EBUSY) { |
| 46 | /* |
| 47 | * Flush all I/O and wait for |
| 48 | * cancel/halt/clear completion. |
| 49 | */ |
| 50 | private->completion = &completion; |
| 51 | spin_unlock_irq(sch->lock); |
| 52 | |
| 53 | wait_for_completion_timeout(&completion, 3*HZ); |
| 54 | |
| 55 | spin_lock_irq(sch->lock); |
| 56 | private->completion = NULL; |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 57 | flush_workqueue(vfio_ccw_work_q); |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 58 | ret = cio_cancel_halt_clear(sch, &iretry); |
| 59 | }; |
| 60 | |
| 61 | ret = cio_disable_subchannel(sch); |
| 62 | } while (ret == -EBUSY); |
| 63 | |
| 64 | out_unlock: |
| 65 | spin_unlock_irq(sch->lock); |
| 66 | return ret; |
| 67 | } |
| 68 | |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 69 | static int vfio_ccw_sch_io_helper(struct vfio_ccw_private *private) |
| 70 | { |
| 71 | struct subchannel *sch; |
| 72 | union orb *orb; |
| 73 | int ccode; |
| 74 | __u8 lpm; |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 75 | |
| 76 | sch = private->sch; |
| 77 | |
| 78 | orb = cp_get_orb(&private->cp, (u32)(addr_t)sch, sch->lpm); |
| 79 | |
| 80 | /* Issue "Start Subchannel" */ |
| 81 | ccode = ssch(sch->schid, orb); |
| 82 | |
| 83 | switch (ccode) { |
| 84 | case 0: |
| 85 | /* |
| 86 | * Initialize device status information |
| 87 | */ |
| 88 | sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND; |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 89 | return 0; |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 90 | case 1: /* Status pending */ |
| 91 | case 2: /* Busy */ |
| 92 | return -EBUSY; |
| 93 | case 3: /* Device/path not operational */ |
| 94 | { |
| 95 | lpm = orb->cmd.lpm; |
| 96 | if (lpm != 0) |
| 97 | sch->lpm &= ~lpm; |
| 98 | else |
| 99 | sch->lpm = 0; |
| 100 | |
| 101 | if (cio_update_schib(sch)) |
| 102 | return -ENODEV; |
| 103 | |
| 104 | return sch->lpm ? -EACCES : -ENODEV; |
| 105 | } |
| 106 | default: |
| 107 | return ccode; |
| 108 | } |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 109 | } |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 110 | |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 111 | static void vfio_ccw_sch_io_todo(struct work_struct *work) |
| 112 | { |
| 113 | struct vfio_ccw_private *private; |
| 114 | struct subchannel *sch; |
| 115 | struct irb *irb; |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 116 | |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 117 | private = container_of(work, struct vfio_ccw_private, io_work); |
| 118 | irb = &private->irb; |
| 119 | sch = private->sch; |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 120 | |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 121 | if (scsw_is_solicited(&irb->scsw)) { |
| 122 | cp_update_scsw(&private->cp, &irb->scsw); |
| 123 | cp_free(&private->cp); |
| 124 | } |
| 125 | memcpy(private->io_region.irb_area, irb, sizeof(*irb)); |
| 126 | |
| 127 | if (private->io_trigger) |
| 128 | eventfd_signal(private->io_trigger, 1); |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Deal with the ccw command request from the userspace. */ |
| 132 | int vfio_ccw_sch_cmd_request(struct vfio_ccw_private *private) |
| 133 | { |
| 134 | struct mdev_device *mdev = private->mdev; |
| 135 | union orb *orb; |
| 136 | union scsw *scsw = &private->scsw; |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 137 | struct ccw_io_region *io_region = &private->io_region; |
| 138 | int ret; |
| 139 | |
| 140 | memcpy(scsw, io_region->scsw_area, sizeof(*scsw)); |
| 141 | |
| 142 | if (scsw->cmd.fctl & SCSW_FCTL_START_FUNC) { |
| 143 | orb = (union orb *)io_region->orb_area; |
| 144 | |
| 145 | ret = cp_init(&private->cp, mdev_dev(mdev), orb); |
| 146 | if (ret) |
| 147 | return ret; |
| 148 | |
| 149 | ret = cp_prefetch(&private->cp); |
| 150 | if (ret) { |
| 151 | cp_free(&private->cp); |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | /* Start channel program and wait for I/O interrupt. */ |
| 156 | ret = vfio_ccw_sch_io_helper(private); |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 157 | if (!ret) |
| 158 | cp_free(&private->cp); |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 159 | } else if (scsw->cmd.fctl & SCSW_FCTL_HALT_FUNC) { |
| 160 | /* XXX: Handle halt. */ |
| 161 | ret = -EOPNOTSUPP; |
| 162 | } else if (scsw->cmd.fctl & SCSW_FCTL_CLEAR_FUNC) { |
| 163 | /* XXX: Handle clear. */ |
| 164 | ret = -EOPNOTSUPP; |
| 165 | } else { |
| 166 | ret = -EOPNOTSUPP; |
| 167 | } |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 172 | /* |
| 173 | * Sysfs interfaces |
| 174 | */ |
| 175 | static ssize_t chpids_show(struct device *dev, |
| 176 | struct device_attribute *attr, |
| 177 | char *buf) |
| 178 | { |
| 179 | struct subchannel *sch = to_subchannel(dev); |
| 180 | struct chsc_ssd_info *ssd = &sch->ssd_info; |
| 181 | ssize_t ret = 0; |
| 182 | int chp; |
| 183 | int mask; |
| 184 | |
| 185 | for (chp = 0; chp < 8; chp++) { |
| 186 | mask = 0x80 >> chp; |
| 187 | if (ssd->path_mask & mask) |
| 188 | ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id); |
| 189 | else |
| 190 | ret += sprintf(buf + ret, "00 "); |
| 191 | } |
| 192 | ret += sprintf(buf+ret, "\n"); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static ssize_t pimpampom_show(struct device *dev, |
| 197 | struct device_attribute *attr, |
| 198 | char *buf) |
| 199 | { |
| 200 | struct subchannel *sch = to_subchannel(dev); |
| 201 | struct pmcw *pmcw = &sch->schib.pmcw; |
| 202 | |
| 203 | return sprintf(buf, "%02x %02x %02x\n", |
| 204 | pmcw->pim, pmcw->pam, pmcw->pom); |
| 205 | } |
| 206 | |
| 207 | static DEVICE_ATTR(chpids, 0444, chpids_show, NULL); |
| 208 | static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL); |
| 209 | |
| 210 | static struct attribute *vfio_subchannel_attrs[] = { |
| 211 | &dev_attr_chpids.attr, |
| 212 | &dev_attr_pimpampom.attr, |
| 213 | NULL, |
| 214 | }; |
| 215 | |
| 216 | static struct attribute_group vfio_subchannel_attr_group = { |
| 217 | .attrs = vfio_subchannel_attrs, |
| 218 | }; |
| 219 | |
| 220 | /* |
| 221 | * Css driver callbacks |
| 222 | */ |
| 223 | static void vfio_ccw_sch_irq(struct subchannel *sch) |
| 224 | { |
| 225 | struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev); |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 226 | struct irb *irb; |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 227 | |
| 228 | inc_irq_stat(IRQIO_CIO); |
| 229 | |
| 230 | if (!private) |
| 231 | return; |
| 232 | |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 233 | irb = this_cpu_ptr(&cio_irb); |
| 234 | memcpy(&private->irb, irb, sizeof(*irb)); |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 235 | |
| 236 | queue_work(vfio_ccw_work_q, &private->io_work); |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 237 | |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 238 | if (private->completion) |
| 239 | complete(private->completion); |
| 240 | } |
| 241 | |
| 242 | static int vfio_ccw_sch_probe(struct subchannel *sch) |
| 243 | { |
| 244 | struct pmcw *pmcw = &sch->schib.pmcw; |
| 245 | struct vfio_ccw_private *private; |
| 246 | int ret; |
| 247 | |
| 248 | if (pmcw->qf) { |
| 249 | dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n", |
| 250 | dev_name(&sch->dev)); |
| 251 | return -ENODEV; |
| 252 | } |
| 253 | |
| 254 | private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA); |
| 255 | if (!private) |
| 256 | return -ENOMEM; |
| 257 | private->sch = sch; |
| 258 | dev_set_drvdata(&sch->dev, private); |
| 259 | |
| 260 | spin_lock_irq(sch->lock); |
| 261 | sch->isc = VFIO_CCW_ISC; |
| 262 | ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch); |
| 263 | spin_unlock_irq(sch->lock); |
| 264 | if (ret) |
| 265 | goto out_free; |
| 266 | |
| 267 | ret = sysfs_create_group(&sch->dev.kobj, &vfio_subchannel_attr_group); |
| 268 | if (ret) |
| 269 | goto out_disable; |
| 270 | |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 271 | ret = vfio_ccw_mdev_reg(sch); |
| 272 | if (ret) |
| 273 | goto out_rm_group; |
| 274 | |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 275 | INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo); |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 276 | atomic_set(&private->avail, 1); |
| 277 | |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 278 | return 0; |
| 279 | |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 280 | out_rm_group: |
| 281 | sysfs_remove_group(&sch->dev.kobj, &vfio_subchannel_attr_group); |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 282 | out_disable: |
| 283 | cio_disable_subchannel(sch); |
| 284 | out_free: |
| 285 | dev_set_drvdata(&sch->dev, NULL); |
| 286 | kfree(private); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static int vfio_ccw_sch_remove(struct subchannel *sch) |
| 291 | { |
| 292 | struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev); |
| 293 | |
| 294 | vfio_ccw_sch_quiesce(sch); |
| 295 | |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 296 | vfio_ccw_mdev_unreg(sch); |
| 297 | |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 298 | sysfs_remove_group(&sch->dev.kobj, &vfio_subchannel_attr_group); |
| 299 | |
| 300 | dev_set_drvdata(&sch->dev, NULL); |
| 301 | |
| 302 | kfree(private); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static void vfio_ccw_sch_shutdown(struct subchannel *sch) |
| 308 | { |
| 309 | vfio_ccw_sch_quiesce(sch); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * vfio_ccw_sch_event - process subchannel event |
| 314 | * @sch: subchannel |
| 315 | * @process: non-zero if function is called in process context |
| 316 | * |
| 317 | * An unspecified event occurred for this subchannel. Adjust data according |
| 318 | * to the current operational state of the subchannel. Return zero when the |
| 319 | * event has been handled sufficiently or -EAGAIN when this function should |
| 320 | * be called again in process context. |
| 321 | */ |
| 322 | static int vfio_ccw_sch_event(struct subchannel *sch, int process) |
| 323 | { |
| 324 | unsigned long flags; |
| 325 | |
| 326 | spin_lock_irqsave(sch->lock, flags); |
| 327 | if (!device_is_registered(&sch->dev)) |
| 328 | goto out_unlock; |
| 329 | |
| 330 | if (work_pending(&sch->todo_work)) |
| 331 | goto out_unlock; |
| 332 | |
| 333 | if (cio_update_schib(sch)) { |
| 334 | /* Not operational. */ |
| 335 | css_sched_sch_todo(sch, SCH_TODO_UNREG); |
| 336 | |
| 337 | /* |
| 338 | * TODO: |
| 339 | * Probably we should send the machine check to the guest. |
| 340 | */ |
| 341 | goto out_unlock; |
| 342 | } |
| 343 | |
| 344 | out_unlock: |
| 345 | spin_unlock_irqrestore(sch->lock, flags); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static struct css_device_id vfio_ccw_sch_ids[] = { |
| 351 | { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, }, |
| 352 | { /* end of list */ }, |
| 353 | }; |
| 354 | MODULE_DEVICE_TABLE(css, vfio_ccw_sch_ids); |
| 355 | |
| 356 | static struct css_driver vfio_ccw_sch_driver = { |
| 357 | .drv = { |
| 358 | .name = "vfio_ccw", |
| 359 | .owner = THIS_MODULE, |
| 360 | }, |
| 361 | .subchannel_type = vfio_ccw_sch_ids, |
| 362 | .irq = vfio_ccw_sch_irq, |
| 363 | .probe = vfio_ccw_sch_probe, |
| 364 | .remove = vfio_ccw_sch_remove, |
| 365 | .shutdown = vfio_ccw_sch_shutdown, |
| 366 | .sch_event = vfio_ccw_sch_event, |
| 367 | }; |
| 368 | |
| 369 | static int __init vfio_ccw_sch_init(void) |
| 370 | { |
| 371 | int ret; |
| 372 | |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 373 | vfio_ccw_work_q = create_singlethread_workqueue("vfio-ccw"); |
| 374 | if (!vfio_ccw_work_q) |
| 375 | return -ENOMEM; |
| 376 | |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 377 | isc_register(VFIO_CCW_ISC); |
| 378 | ret = css_driver_register(&vfio_ccw_sch_driver); |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 379 | if (ret) { |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 380 | isc_unregister(VFIO_CCW_ISC); |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 381 | destroy_workqueue(vfio_ccw_work_q); |
| 382 | } |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | static void __exit vfio_ccw_sch_exit(void) |
| 388 | { |
| 389 | css_driver_unregister(&vfio_ccw_sch_driver); |
| 390 | isc_unregister(VFIO_CCW_ISC); |
Dong Jia Shi | e5f84db | 2017-03-17 04:17:39 +0100 | [diff] [blame^] | 391 | destroy_workqueue(vfio_ccw_work_q); |
Dong Jia Shi | 63f1934 | 2017-03-17 04:17:31 +0100 | [diff] [blame] | 392 | } |
| 393 | module_init(vfio_ccw_sch_init); |
| 394 | module_exit(vfio_ccw_sch_exit); |
| 395 | |
| 396 | MODULE_LICENSE("GPL v2"); |