| .. | .. |
|---|
| 3 | 3 | * Finite state machine for vfio-ccw device handling |
|---|
| 4 | 4 | * |
|---|
| 5 | 5 | * Copyright IBM Corp. 2017 |
|---|
| 6 | + * Copyright Red Hat, Inc. 2019 |
|---|
| 6 | 7 | * |
|---|
| 7 | 8 | * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> |
|---|
| 9 | + * Cornelia Huck <cohuck@redhat.com> |
|---|
| 8 | 10 | */ |
|---|
| 9 | 11 | |
|---|
| 10 | 12 | #include <linux/vfio.h> |
|---|
| .. | .. |
|---|
| 12 | 14 | |
|---|
| 13 | 15 | #include "ioasm.h" |
|---|
| 14 | 16 | #include "vfio_ccw_private.h" |
|---|
| 15 | | - |
|---|
| 16 | | -#define CREATE_TRACE_POINTS |
|---|
| 17 | | -#include "vfio_ccw_trace.h" |
|---|
| 18 | 17 | |
|---|
| 19 | 18 | static int fsm_io_helper(struct vfio_ccw_private *private) |
|---|
| 20 | 19 | { |
|---|
| .. | .. |
|---|
| 28 | 27 | sch = private->sch; |
|---|
| 29 | 28 | |
|---|
| 30 | 29 | spin_lock_irqsave(sch->lock, flags); |
|---|
| 31 | | - private->state = VFIO_CCW_STATE_BUSY; |
|---|
| 32 | 30 | |
|---|
| 33 | 31 | orb = cp_get_orb(&private->cp, (u32)(addr_t)sch, sch->lpm); |
|---|
| 32 | + if (!orb) { |
|---|
| 33 | + ret = -EIO; |
|---|
| 34 | + goto out; |
|---|
| 35 | + } |
|---|
| 36 | + |
|---|
| 37 | + VFIO_CCW_TRACE_EVENT(5, "stIO"); |
|---|
| 38 | + VFIO_CCW_TRACE_EVENT(5, dev_name(&sch->dev)); |
|---|
| 34 | 39 | |
|---|
| 35 | 40 | /* Issue "Start Subchannel" */ |
|---|
| 36 | 41 | ccode = ssch(sch->schid, orb); |
|---|
| 42 | + |
|---|
| 43 | + VFIO_CCW_HEX_EVENT(5, &ccode, sizeof(ccode)); |
|---|
| 37 | 44 | |
|---|
| 38 | 45 | switch (ccode) { |
|---|
| 39 | 46 | case 0: |
|---|
| .. | .. |
|---|
| 42 | 49 | */ |
|---|
| 43 | 50 | sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND; |
|---|
| 44 | 51 | ret = 0; |
|---|
| 52 | + private->state = VFIO_CCW_STATE_CP_PENDING; |
|---|
| 45 | 53 | break; |
|---|
| 46 | 54 | case 1: /* Status pending */ |
|---|
| 47 | 55 | case 2: /* Busy */ |
|---|
| .. | .. |
|---|
| 64 | 72 | default: |
|---|
| 65 | 73 | ret = ccode; |
|---|
| 66 | 74 | } |
|---|
| 75 | +out: |
|---|
| 76 | + spin_unlock_irqrestore(sch->lock, flags); |
|---|
| 77 | + return ret; |
|---|
| 78 | +} |
|---|
| 79 | + |
|---|
| 80 | +static int fsm_do_halt(struct vfio_ccw_private *private) |
|---|
| 81 | +{ |
|---|
| 82 | + struct subchannel *sch; |
|---|
| 83 | + unsigned long flags; |
|---|
| 84 | + int ccode; |
|---|
| 85 | + int ret; |
|---|
| 86 | + |
|---|
| 87 | + sch = private->sch; |
|---|
| 88 | + |
|---|
| 89 | + spin_lock_irqsave(sch->lock, flags); |
|---|
| 90 | + |
|---|
| 91 | + VFIO_CCW_TRACE_EVENT(2, "haltIO"); |
|---|
| 92 | + VFIO_CCW_TRACE_EVENT(2, dev_name(&sch->dev)); |
|---|
| 93 | + |
|---|
| 94 | + /* Issue "Halt Subchannel" */ |
|---|
| 95 | + ccode = hsch(sch->schid); |
|---|
| 96 | + |
|---|
| 97 | + VFIO_CCW_HEX_EVENT(2, &ccode, sizeof(ccode)); |
|---|
| 98 | + |
|---|
| 99 | + switch (ccode) { |
|---|
| 100 | + case 0: |
|---|
| 101 | + /* |
|---|
| 102 | + * Initialize device status information |
|---|
| 103 | + */ |
|---|
| 104 | + sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND; |
|---|
| 105 | + ret = 0; |
|---|
| 106 | + break; |
|---|
| 107 | + case 1: /* Status pending */ |
|---|
| 108 | + case 2: /* Busy */ |
|---|
| 109 | + ret = -EBUSY; |
|---|
| 110 | + break; |
|---|
| 111 | + case 3: /* Device not operational */ |
|---|
| 112 | + ret = -ENODEV; |
|---|
| 113 | + break; |
|---|
| 114 | + default: |
|---|
| 115 | + ret = ccode; |
|---|
| 116 | + } |
|---|
| 117 | + spin_unlock_irqrestore(sch->lock, flags); |
|---|
| 118 | + return ret; |
|---|
| 119 | +} |
|---|
| 120 | + |
|---|
| 121 | +static int fsm_do_clear(struct vfio_ccw_private *private) |
|---|
| 122 | +{ |
|---|
| 123 | + struct subchannel *sch; |
|---|
| 124 | + unsigned long flags; |
|---|
| 125 | + int ccode; |
|---|
| 126 | + int ret; |
|---|
| 127 | + |
|---|
| 128 | + sch = private->sch; |
|---|
| 129 | + |
|---|
| 130 | + spin_lock_irqsave(sch->lock, flags); |
|---|
| 131 | + |
|---|
| 132 | + VFIO_CCW_TRACE_EVENT(2, "clearIO"); |
|---|
| 133 | + VFIO_CCW_TRACE_EVENT(2, dev_name(&sch->dev)); |
|---|
| 134 | + |
|---|
| 135 | + /* Issue "Clear Subchannel" */ |
|---|
| 136 | + ccode = csch(sch->schid); |
|---|
| 137 | + |
|---|
| 138 | + VFIO_CCW_HEX_EVENT(2, &ccode, sizeof(ccode)); |
|---|
| 139 | + |
|---|
| 140 | + switch (ccode) { |
|---|
| 141 | + case 0: |
|---|
| 142 | + /* |
|---|
| 143 | + * Initialize device status information |
|---|
| 144 | + */ |
|---|
| 145 | + sch->schib.scsw.cmd.actl = SCSW_ACTL_CLEAR_PEND; |
|---|
| 146 | + /* TODO: check what else we might need to clear */ |
|---|
| 147 | + ret = 0; |
|---|
| 148 | + break; |
|---|
| 149 | + case 3: /* Device not operational */ |
|---|
| 150 | + ret = -ENODEV; |
|---|
| 151 | + break; |
|---|
| 152 | + default: |
|---|
| 153 | + ret = ccode; |
|---|
| 154 | + } |
|---|
| 67 | 155 | spin_unlock_irqrestore(sch->lock, flags); |
|---|
| 68 | 156 | return ret; |
|---|
| 69 | 157 | } |
|---|
| .. | .. |
|---|
| 72 | 160 | enum vfio_ccw_event event) |
|---|
| 73 | 161 | { |
|---|
| 74 | 162 | struct subchannel *sch = private->sch; |
|---|
| 163 | + |
|---|
| 164 | + VFIO_CCW_TRACE_EVENT(2, "notoper"); |
|---|
| 165 | + VFIO_CCW_TRACE_EVENT(2, dev_name(&sch->dev)); |
|---|
| 75 | 166 | |
|---|
| 76 | 167 | /* |
|---|
| 77 | 168 | * TODO: |
|---|
| .. | .. |
|---|
| 102 | 193 | private->io_region->ret_code = -EBUSY; |
|---|
| 103 | 194 | } |
|---|
| 104 | 195 | |
|---|
| 196 | +static void fsm_io_retry(struct vfio_ccw_private *private, |
|---|
| 197 | + enum vfio_ccw_event event) |
|---|
| 198 | +{ |
|---|
| 199 | + private->io_region->ret_code = -EAGAIN; |
|---|
| 200 | +} |
|---|
| 201 | + |
|---|
| 202 | +static void fsm_async_error(struct vfio_ccw_private *private, |
|---|
| 203 | + enum vfio_ccw_event event) |
|---|
| 204 | +{ |
|---|
| 205 | + struct ccw_cmd_region *cmd_region = private->cmd_region; |
|---|
| 206 | + |
|---|
| 207 | + pr_err("vfio-ccw: FSM: %s request from state:%d\n", |
|---|
| 208 | + cmd_region->command == VFIO_CCW_ASYNC_CMD_HSCH ? "halt" : |
|---|
| 209 | + cmd_region->command == VFIO_CCW_ASYNC_CMD_CSCH ? "clear" : |
|---|
| 210 | + "<unknown>", private->state); |
|---|
| 211 | + cmd_region->ret_code = -EIO; |
|---|
| 212 | +} |
|---|
| 213 | + |
|---|
| 214 | +static void fsm_async_retry(struct vfio_ccw_private *private, |
|---|
| 215 | + enum vfio_ccw_event event) |
|---|
| 216 | +{ |
|---|
| 217 | + private->cmd_region->ret_code = -EAGAIN; |
|---|
| 218 | +} |
|---|
| 219 | + |
|---|
| 105 | 220 | static void fsm_disabled_irq(struct vfio_ccw_private *private, |
|---|
| 106 | 221 | enum vfio_ccw_event event) |
|---|
| 107 | 222 | { |
|---|
| .. | .. |
|---|
| 129 | 244 | struct ccw_io_region *io_region = private->io_region; |
|---|
| 130 | 245 | struct mdev_device *mdev = private->mdev; |
|---|
| 131 | 246 | char *errstr = "request"; |
|---|
| 247 | + struct subchannel_id schid = get_schid(private); |
|---|
| 132 | 248 | |
|---|
| 133 | | - private->state = VFIO_CCW_STATE_BOXED; |
|---|
| 134 | | - |
|---|
| 249 | + private->state = VFIO_CCW_STATE_CP_PROCESSING; |
|---|
| 135 | 250 | memcpy(scsw, io_region->scsw_area, sizeof(*scsw)); |
|---|
| 136 | 251 | |
|---|
| 137 | 252 | if (scsw->cmd.fctl & SCSW_FCTL_START_FUNC) { |
|---|
| .. | .. |
|---|
| 140 | 255 | /* Don't try to build a cp if transport mode is specified. */ |
|---|
| 141 | 256 | if (orb->tm.b) { |
|---|
| 142 | 257 | io_region->ret_code = -EOPNOTSUPP; |
|---|
| 258 | + VFIO_CCW_MSG_EVENT(2, |
|---|
| 259 | + "%pUl (%x.%x.%04x): transport mode\n", |
|---|
| 260 | + mdev_uuid(mdev), schid.cssid, |
|---|
| 261 | + schid.ssid, schid.sch_no); |
|---|
| 143 | 262 | errstr = "transport mode"; |
|---|
| 144 | 263 | goto err_out; |
|---|
| 145 | 264 | } |
|---|
| 146 | 265 | io_region->ret_code = cp_init(&private->cp, mdev_dev(mdev), |
|---|
| 147 | 266 | orb); |
|---|
| 148 | 267 | if (io_region->ret_code) { |
|---|
| 268 | + VFIO_CCW_MSG_EVENT(2, |
|---|
| 269 | + "%pUl (%x.%x.%04x): cp_init=%d\n", |
|---|
| 270 | + mdev_uuid(mdev), schid.cssid, |
|---|
| 271 | + schid.ssid, schid.sch_no, |
|---|
| 272 | + io_region->ret_code); |
|---|
| 149 | 273 | errstr = "cp init"; |
|---|
| 150 | 274 | goto err_out; |
|---|
| 151 | 275 | } |
|---|
| 152 | 276 | |
|---|
| 153 | 277 | io_region->ret_code = cp_prefetch(&private->cp); |
|---|
| 154 | 278 | if (io_region->ret_code) { |
|---|
| 279 | + VFIO_CCW_MSG_EVENT(2, |
|---|
| 280 | + "%pUl (%x.%x.%04x): cp_prefetch=%d\n", |
|---|
| 281 | + mdev_uuid(mdev), schid.cssid, |
|---|
| 282 | + schid.ssid, schid.sch_no, |
|---|
| 283 | + io_region->ret_code); |
|---|
| 155 | 284 | errstr = "cp prefetch"; |
|---|
| 156 | 285 | cp_free(&private->cp); |
|---|
| 157 | 286 | goto err_out; |
|---|
| .. | .. |
|---|
| 160 | 289 | /* Start channel program and wait for I/O interrupt. */ |
|---|
| 161 | 290 | io_region->ret_code = fsm_io_helper(private); |
|---|
| 162 | 291 | if (io_region->ret_code) { |
|---|
| 292 | + VFIO_CCW_MSG_EVENT(2, |
|---|
| 293 | + "%pUl (%x.%x.%04x): fsm_io_helper=%d\n", |
|---|
| 294 | + mdev_uuid(mdev), schid.cssid, |
|---|
| 295 | + schid.ssid, schid.sch_no, |
|---|
| 296 | + io_region->ret_code); |
|---|
| 163 | 297 | errstr = "cp fsm_io_helper"; |
|---|
| 164 | 298 | cp_free(&private->cp); |
|---|
| 165 | 299 | goto err_out; |
|---|
| 166 | 300 | } |
|---|
| 167 | 301 | return; |
|---|
| 168 | 302 | } else if (scsw->cmd.fctl & SCSW_FCTL_HALT_FUNC) { |
|---|
| 169 | | - /* XXX: Handle halt. */ |
|---|
| 303 | + VFIO_CCW_MSG_EVENT(2, |
|---|
| 304 | + "%pUl (%x.%x.%04x): halt on io_region\n", |
|---|
| 305 | + mdev_uuid(mdev), schid.cssid, |
|---|
| 306 | + schid.ssid, schid.sch_no); |
|---|
| 307 | + /* halt is handled via the async cmd region */ |
|---|
| 170 | 308 | io_region->ret_code = -EOPNOTSUPP; |
|---|
| 171 | 309 | goto err_out; |
|---|
| 172 | 310 | } else if (scsw->cmd.fctl & SCSW_FCTL_CLEAR_FUNC) { |
|---|
| 173 | | - /* XXX: Handle clear. */ |
|---|
| 311 | + VFIO_CCW_MSG_EVENT(2, |
|---|
| 312 | + "%pUl (%x.%x.%04x): clear on io_region\n", |
|---|
| 313 | + mdev_uuid(mdev), schid.cssid, |
|---|
| 314 | + schid.ssid, schid.sch_no); |
|---|
| 315 | + /* clear is handled via the async cmd region */ |
|---|
| 174 | 316 | io_region->ret_code = -EOPNOTSUPP; |
|---|
| 175 | 317 | goto err_out; |
|---|
| 176 | 318 | } |
|---|
| 177 | 319 | |
|---|
| 178 | 320 | err_out: |
|---|
| 179 | 321 | private->state = VFIO_CCW_STATE_IDLE; |
|---|
| 180 | | - trace_vfio_ccw_io_fctl(scsw->cmd.fctl, get_schid(private), |
|---|
| 181 | | - io_region->ret_code, errstr); |
|---|
| 322 | + trace_vfio_ccw_fsm_io_request(scsw->cmd.fctl, schid, |
|---|
| 323 | + io_region->ret_code, errstr); |
|---|
| 324 | +} |
|---|
| 325 | + |
|---|
| 326 | +/* |
|---|
| 327 | + * Deal with an async request from userspace. |
|---|
| 328 | + */ |
|---|
| 329 | +static void fsm_async_request(struct vfio_ccw_private *private, |
|---|
| 330 | + enum vfio_ccw_event event) |
|---|
| 331 | +{ |
|---|
| 332 | + struct ccw_cmd_region *cmd_region = private->cmd_region; |
|---|
| 333 | + |
|---|
| 334 | + switch (cmd_region->command) { |
|---|
| 335 | + case VFIO_CCW_ASYNC_CMD_HSCH: |
|---|
| 336 | + cmd_region->ret_code = fsm_do_halt(private); |
|---|
| 337 | + break; |
|---|
| 338 | + case VFIO_CCW_ASYNC_CMD_CSCH: |
|---|
| 339 | + cmd_region->ret_code = fsm_do_clear(private); |
|---|
| 340 | + break; |
|---|
| 341 | + default: |
|---|
| 342 | + /* should not happen? */ |
|---|
| 343 | + cmd_region->ret_code = -EINVAL; |
|---|
| 344 | + } |
|---|
| 345 | + |
|---|
| 346 | + trace_vfio_ccw_fsm_async_request(get_schid(private), |
|---|
| 347 | + cmd_region->command, |
|---|
| 348 | + cmd_region->ret_code); |
|---|
| 182 | 349 | } |
|---|
| 183 | 350 | |
|---|
| 184 | 351 | /* |
|---|
| .. | .. |
|---|
| 188 | 355 | enum vfio_ccw_event event) |
|---|
| 189 | 356 | { |
|---|
| 190 | 357 | struct irb *irb = this_cpu_ptr(&cio_irb); |
|---|
| 358 | + |
|---|
| 359 | + VFIO_CCW_TRACE_EVENT(6, "IRQ"); |
|---|
| 360 | + VFIO_CCW_TRACE_EVENT(6, dev_name(&private->sch->dev)); |
|---|
| 191 | 361 | |
|---|
| 192 | 362 | memcpy(&private->irb, irb, sizeof(*irb)); |
|---|
| 193 | 363 | |
|---|
| .. | .. |
|---|
| 204 | 374 | [VFIO_CCW_STATE_NOT_OPER] = { |
|---|
| 205 | 375 | [VFIO_CCW_EVENT_NOT_OPER] = fsm_nop, |
|---|
| 206 | 376 | [VFIO_CCW_EVENT_IO_REQ] = fsm_io_error, |
|---|
| 377 | + [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_error, |
|---|
| 207 | 378 | [VFIO_CCW_EVENT_INTERRUPT] = fsm_disabled_irq, |
|---|
| 208 | 379 | }, |
|---|
| 209 | 380 | [VFIO_CCW_STATE_STANDBY] = { |
|---|
| 210 | 381 | [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper, |
|---|
| 211 | 382 | [VFIO_CCW_EVENT_IO_REQ] = fsm_io_error, |
|---|
| 383 | + [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_error, |
|---|
| 212 | 384 | [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq, |
|---|
| 213 | 385 | }, |
|---|
| 214 | 386 | [VFIO_CCW_STATE_IDLE] = { |
|---|
| 215 | 387 | [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper, |
|---|
| 216 | 388 | [VFIO_CCW_EVENT_IO_REQ] = fsm_io_request, |
|---|
| 389 | + [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_request, |
|---|
| 217 | 390 | [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq, |
|---|
| 218 | 391 | }, |
|---|
| 219 | | - [VFIO_CCW_STATE_BOXED] = { |
|---|
| 392 | + [VFIO_CCW_STATE_CP_PROCESSING] = { |
|---|
| 220 | 393 | [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper, |
|---|
| 221 | | - [VFIO_CCW_EVENT_IO_REQ] = fsm_io_busy, |
|---|
| 394 | + [VFIO_CCW_EVENT_IO_REQ] = fsm_io_retry, |
|---|
| 395 | + [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_retry, |
|---|
| 222 | 396 | [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq, |
|---|
| 223 | 397 | }, |
|---|
| 224 | | - [VFIO_CCW_STATE_BUSY] = { |
|---|
| 398 | + [VFIO_CCW_STATE_CP_PENDING] = { |
|---|
| 225 | 399 | [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper, |
|---|
| 226 | 400 | [VFIO_CCW_EVENT_IO_REQ] = fsm_io_busy, |
|---|
| 401 | + [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_request, |
|---|
| 227 | 402 | [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq, |
|---|
| 228 | 403 | }, |
|---|
| 229 | 404 | }; |
|---|