1 /*- 2 * Copyright (c) 2012, Bryan Venteicher <bryanv@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/dev/virtio/scsi/virtio_scsi.c 311305 2017-01-04 20:26:42Z asomers $ 27 */ 28 29 /* Driver for VirtIO SCSI devices. */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/kthread.h> 35 #include <sys/malloc.h> 36 #include <sys/module.h> 37 #include <sys/sglist.h> 38 #include <sys/sysctl.h> 39 #include <sys/lock.h> 40 #include <sys/callout.h> 41 #include <sys/queue.h> 42 #include <sys/sbuf.h> 43 44 #include <machine/stdarg.h> 45 46 #include <sys/bus.h> 47 #include <sys/rman.h> 48 49 #include <bus/cam/cam.h> 50 #include <bus/cam/cam_ccb.h> 51 #include <bus/cam/cam_sim.h> 52 #include <bus/cam/cam_periph.h> 53 #include <bus/cam/cam_xpt_periph.h> 54 #include <bus/cam/cam_xpt_sim.h> 55 #include <bus/cam/cam_debug.h> 56 #include <bus/cam/scsi/scsi_all.h> 57 #include <bus/cam/scsi/scsi_message.h> 58 59 #include <dev/virtual/virtio/virtio/virtio.h> 60 #include <dev/virtual/virtio/virtio/virtqueue.h> 61 #include <dev/virtual/virtio/scsi/virtio_scsi.h> 62 #include <dev/virtual/virtio/scsi/virtio_scsivar.h> 63 64 static int vtscsi_modevent(module_t, int, void *); 65 66 static int vtscsi_probe(device_t); 67 static int vtscsi_attach(device_t); 68 static int vtscsi_detach(device_t); 69 static int vtscsi_suspend(device_t); 70 static int vtscsi_resume(device_t); 71 72 static void vtscsi_negotiate_features(struct vtscsi_softc *); 73 static void vtscsi_read_config(struct vtscsi_softc *, 74 struct virtio_scsi_config *); 75 static int vtscsi_maximum_segments(struct vtscsi_softc *, int); 76 static int vtscsi_alloc_intrs(struct vtscsi_softc *); 77 static int vtscsi_alloc_virtqueues(struct vtscsi_softc *); 78 static void vtscsi_write_device_config(struct vtscsi_softc *); 79 static int vtscsi_reinit(struct vtscsi_softc *); 80 81 static int vtscsi_alloc_cam(struct vtscsi_softc *); 82 static int vtscsi_register_cam(struct vtscsi_softc *); 83 static void vtscsi_free_cam(struct vtscsi_softc *); 84 static void vtscsi_cam_async(void *, uint32_t, struct cam_path *, void *); 85 static int vtscsi_register_async(struct vtscsi_softc *); 86 static void vtscsi_deregister_async(struct vtscsi_softc *); 87 static void vtscsi_cam_action(struct cam_sim *, union ccb *); 88 static void vtscsi_cam_poll(struct cam_sim *); 89 90 static void vtscsi_cam_scsi_io(struct vtscsi_softc *, struct cam_sim *, 91 union ccb *); 92 static void vtscsi_cam_get_tran_settings(struct vtscsi_softc *, 93 union ccb *); 94 static void vtscsi_cam_reset_bus(struct vtscsi_softc *, union ccb *); 95 static void vtscsi_cam_reset_dev(struct vtscsi_softc *, union ccb *); 96 static void vtscsi_cam_abort(struct vtscsi_softc *, union ccb *); 97 static void vtscsi_cam_path_inquiry(struct vtscsi_softc *, 98 struct cam_sim *, union ccb *); 99 100 static int vtscsi_sg_append_scsi_buf(struct vtscsi_softc *, 101 struct sglist *, struct ccb_scsiio *); 102 static int vtscsi_fill_scsi_cmd_sglist(struct vtscsi_softc *, 103 struct vtscsi_request *, int *, int *); 104 static int vtscsi_execute_scsi_cmd(struct vtscsi_softc *, 105 struct vtscsi_request *); 106 static int vtscsi_start_scsi_cmd(struct vtscsi_softc *, union ccb *); 107 static void vtscsi_complete_abort_timedout_scsi_cmd(struct vtscsi_softc *, 108 struct vtscsi_request *); 109 static int vtscsi_abort_timedout_scsi_cmd(struct vtscsi_softc *, 110 struct vtscsi_request *); 111 static void vtscsi_timedout_scsi_cmd(void *); 112 static cam_status vtscsi_scsi_cmd_cam_status(struct virtio_scsi_cmd_resp *); 113 static cam_status vtscsi_complete_scsi_cmd_response(struct vtscsi_softc *, 114 struct ccb_scsiio *, struct virtio_scsi_cmd_resp *); 115 static void vtscsi_complete_scsi_cmd(struct vtscsi_softc *, 116 struct vtscsi_request *); 117 118 static void vtscsi_poll_ctrl_req(struct vtscsi_softc *, 119 struct vtscsi_request *); 120 static int vtscsi_execute_ctrl_req(struct vtscsi_softc *, 121 struct vtscsi_request *, struct sglist *, int, int, int); 122 static void vtscsi_complete_abort_task_cmd(struct vtscsi_softc *c, 123 struct vtscsi_request *); 124 static int vtscsi_execute_abort_task_cmd(struct vtscsi_softc *, 125 struct vtscsi_request *); 126 static int vtscsi_execute_reset_dev_cmd(struct vtscsi_softc *, 127 struct vtscsi_request *); 128 129 static void vtscsi_get_request_lun(uint8_t [], target_id_t *, lun_id_t *); 130 static void vtscsi_set_request_lun(struct ccb_hdr *, uint8_t []); 131 static void vtscsi_init_scsi_cmd_req(struct ccb_scsiio *, 132 struct virtio_scsi_cmd_req *); 133 static void vtscsi_init_ctrl_tmf_req(struct ccb_hdr *, uint32_t, 134 uintptr_t, struct virtio_scsi_ctrl_tmf_req *); 135 136 static void vtscsi_freeze_simq(struct vtscsi_softc *, int); 137 static int vtscsi_thaw_simq(struct vtscsi_softc *, int); 138 139 static void vtscsi_announce(struct vtscsi_softc *, uint32_t, target_id_t, 140 lun_id_t); 141 static void vtscsi_cam_rescan_callback(struct cam_periph *periph, 142 union ccb *ccb); 143 static void vtscsi_execute_rescan(struct vtscsi_softc *, target_id_t, 144 lun_id_t); 145 static void vtscsi_execute_rescan_bus(struct vtscsi_softc *); 146 147 static void vtscsi_handle_event(struct vtscsi_softc *, 148 struct virtio_scsi_event *); 149 static int vtscsi_enqueue_event_buf(struct vtscsi_softc *, 150 struct virtio_scsi_event *); 151 static int vtscsi_init_event_vq(struct vtscsi_softc *); 152 static void vtscsi_reinit_event_vq(struct vtscsi_softc *); 153 static void vtscsi_drain_event_vq(struct vtscsi_softc *); 154 155 static void vtscsi_complete_vqs_locked(struct vtscsi_softc *); 156 static void vtscsi_complete_vqs(struct vtscsi_softc *); 157 static void vtscsi_drain_vqs(struct vtscsi_softc *); 158 static void vtscsi_cancel_request(struct vtscsi_softc *, 159 struct vtscsi_request *); 160 static void vtscsi_drain_vq(struct vtscsi_softc *, struct virtqueue *); 161 static void vtscsi_stop(struct vtscsi_softc *); 162 static int vtscsi_reset_bus(struct vtscsi_softc *); 163 164 static void vtscsi_init_request(struct vtscsi_softc *, 165 struct vtscsi_request *); 166 static int vtscsi_alloc_requests(struct vtscsi_softc *); 167 static void vtscsi_free_requests(struct vtscsi_softc *); 168 static void vtscsi_enqueue_request(struct vtscsi_softc *, 169 struct vtscsi_request *); 170 static struct vtscsi_request * vtscsi_dequeue_request(struct vtscsi_softc *); 171 172 static void vtscsi_complete_request(struct vtscsi_request *); 173 static void vtscsi_complete_vq(struct vtscsi_softc *, struct virtqueue *); 174 175 static void vtscsi_control_vq_intr(void *); 176 static void vtscsi_event_vq_intr(void *); 177 static void vtscsi_request_vq_intr(void *); 178 static void vtscsi_disable_vqs_intr(struct vtscsi_softc *); 179 static void vtscsi_enable_vqs_intr(struct vtscsi_softc *); 180 181 static void vtscsi_get_tunables(struct vtscsi_softc *); 182 static void vtscsi_add_sysctl(struct vtscsi_softc *); 183 184 static void vtscsi_printf_req(struct vtscsi_request *, const char *, 185 const char *, ...) __printflike(3, 4); 186 187 /* Global tunables. */ 188 /* 189 * The current QEMU VirtIO SCSI implementation does not cancel in-flight 190 * IO during virtio_stop(). So in-flight requests still complete after the 191 * device reset. We would have to wait for all the in-flight IO to complete, 192 * which defeats the typical purpose of a bus reset. We could simulate the 193 * bus reset with either I_T_NEXUS_RESET of all the targets, or with 194 * LOGICAL_UNIT_RESET of all the LUNs (assuming there is space in the 195 * control virtqueue). But this isn't very useful if things really go off 196 * the rails, so default to disabled for now. 197 */ 198 static int vtscsi_bus_reset_disable = 1; 199 TUNABLE_INT("hw.vtscsi.bus_reset_disable", &vtscsi_bus_reset_disable); 200 201 static struct virtio_feature_desc vtscsi_feature_desc[] = { 202 { VIRTIO_SCSI_F_INOUT, "InOut" }, 203 { VIRTIO_SCSI_F_HOTPLUG, "Hotplug" }, 204 205 { 0, NULL } 206 }; 207 208 static device_method_t vtscsi_methods[] = { 209 /* Device methods. */ 210 DEVMETHOD(device_probe, vtscsi_probe), 211 DEVMETHOD(device_attach, vtscsi_attach), 212 DEVMETHOD(device_detach, vtscsi_detach), 213 DEVMETHOD(device_suspend, vtscsi_suspend), 214 DEVMETHOD(device_resume, vtscsi_resume), 215 216 DEVMETHOD_END 217 }; 218 219 static driver_t vtscsi_driver = { 220 "vtscsi", 221 vtscsi_methods, 222 sizeof(struct vtscsi_softc) 223 }; 224 static devclass_t vtscsi_devclass; 225 226 DRIVER_MODULE(virtio_scsi, virtio_pci, vtscsi_driver, vtscsi_devclass, 227 vtscsi_modevent, NULL); 228 MODULE_VERSION(virtio_scsi, 1); 229 MODULE_DEPEND(virtio_scsi, virtio, 1, 1, 1); 230 MODULE_DEPEND(virtio_scsi, cam, 1, 1, 1); 231 232 static int 233 vtscsi_modevent(module_t mod, int type, void *unused) 234 { 235 int error; 236 237 switch (type) { 238 case MOD_LOAD: 239 case MOD_UNLOAD: 240 case MOD_SHUTDOWN: 241 error = 0; 242 break; 243 default: 244 error = EOPNOTSUPP; 245 break; 246 } 247 248 return (error); 249 } 250 251 static int 252 vtscsi_probe(device_t dev) 253 { 254 255 if (virtio_get_device_type(dev) != VIRTIO_ID_SCSI) 256 return (ENXIO); 257 258 device_set_desc(dev, "VirtIO SCSI Adapter"); 259 260 return (BUS_PROBE_DEFAULT); 261 } 262 263 struct irqmap { 264 int irq; 265 driver_intr_t *handler; 266 }; 267 268 static int 269 vtscsi_attach(device_t dev) 270 { 271 struct vtscsi_softc *sc; 272 struct virtio_scsi_config scsicfg; 273 int i, error; 274 275 sc = device_get_softc(dev); 276 sc->vtscsi_dev = dev; 277 278 VTSCSI_LOCK_INIT(sc, device_get_nameunit(dev)); 279 TAILQ_INIT(&sc->vtscsi_req_free); 280 281 vtscsi_get_tunables(sc); 282 vtscsi_add_sysctl(sc); 283 284 virtio_set_feature_desc(dev, vtscsi_feature_desc); 285 vtscsi_negotiate_features(sc); 286 287 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) 288 sc->vtscsi_flags |= VTSCSI_FLAG_INDIRECT; 289 #ifndef __DragonFly__ /* XXX swildner */ 290 if (virtio_with_feature(dev, VIRTIO_SCSI_F_INOUT)) 291 sc->vtscsi_flags |= VTSCSI_FLAG_BIDIRECTIONAL; 292 #endif 293 if (virtio_with_feature(dev, VIRTIO_SCSI_F_HOTPLUG)) 294 sc->vtscsi_flags |= VTSCSI_FLAG_HOTPLUG; 295 296 vtscsi_read_config(sc, &scsicfg); 297 298 sc->vtscsi_max_channel = scsicfg.max_channel; 299 sc->vtscsi_max_target = scsicfg.max_target; 300 sc->vtscsi_max_lun = scsicfg.max_lun; 301 sc->vtscsi_event_buf_size = scsicfg.event_info_size; 302 303 vtscsi_write_device_config(sc); 304 305 sc->vtscsi_max_nsegs = vtscsi_maximum_segments(sc, scsicfg.seg_max); 306 sc->vtscsi_sglist = sglist_alloc(sc->vtscsi_max_nsegs, M_NOWAIT); 307 if (sc->vtscsi_sglist == NULL) { 308 error = ENOMEM; 309 device_printf(dev, "cannot allocate sglist\n"); 310 goto fail; 311 } 312 313 error = vtscsi_alloc_intrs(sc); 314 if (error) { 315 device_printf(dev, "cannot allocate interrupts\n"); 316 goto fail; 317 } 318 319 error = vtscsi_alloc_virtqueues(sc); 320 if (error) { 321 device_printf(dev, "cannot allocate virtqueues\n"); 322 goto fail; 323 } 324 325 /* XXX Separate function */ 326 struct irqmap info[3]; 327 328 /* Possible "Virtqueue <-> IRQ" configurations */ 329 switch (sc->vtscsi_nintr) { 330 case 1: 331 info[0] = (struct irqmap){0, vtscsi_control_vq_intr}; 332 info[1] = (struct irqmap){0, vtscsi_event_vq_intr}; 333 info[2] = (struct irqmap){0, vtscsi_request_vq_intr}; 334 break; 335 case 2: 336 info[0] = (struct irqmap){0, vtscsi_control_vq_intr}; 337 info[1] = (struct irqmap){0, vtscsi_event_vq_intr}; 338 info[2] = (struct irqmap){1, vtscsi_request_vq_intr}; 339 break; 340 case 3: 341 info[0] = (struct irqmap){0, vtscsi_control_vq_intr}; 342 info[1] = (struct irqmap){1, vtscsi_event_vq_intr}; 343 info[2] = (struct irqmap){2, vtscsi_request_vq_intr}; 344 break; 345 default: 346 device_printf(dev, "Invalid interrupt vector count: %d\n", 347 sc->vtscsi_nintr); 348 goto fail; 349 } 350 for (i = 0; i < 3; i++) { 351 error = virtio_bind_intr(sc->vtscsi_dev, info[i].irq, i, 352 info[i].handler, sc); 353 if (error) { 354 device_printf(dev, 355 "cannot bind virtqueue IRQs\n"); 356 goto fail; 357 } 358 } 359 360 error = vtscsi_init_event_vq(sc); 361 if (error) { 362 device_printf(dev, "cannot populate the eventvq\n"); 363 goto fail; 364 } 365 366 error = vtscsi_alloc_requests(sc); 367 if (error) { 368 device_printf(dev, "cannot allocate requests\n"); 369 goto fail; 370 } 371 372 error = vtscsi_alloc_cam(sc); 373 if (error) { 374 device_printf(dev, "cannot allocate CAM structures\n"); 375 goto fail; 376 } 377 378 for (i = 0; i < sc->vtscsi_nintr; i++) { 379 error = virtio_setup_intr(dev, i, NULL); 380 if (error) { 381 device_printf(dev, "cannot setup virtqueue " 382 "interrupts\n"); 383 goto fail; 384 } 385 } 386 387 vtscsi_enable_vqs_intr(sc); 388 389 /* 390 * Register with CAM after interrupts are enabled so we will get 391 * notified of the probe responses. 392 */ 393 error = vtscsi_register_cam(sc); 394 if (error) { 395 device_printf(dev, "cannot register with CAM\n"); 396 goto fail; 397 } 398 399 fail: 400 if (error) 401 vtscsi_detach(dev); 402 403 return (error); 404 } 405 406 static int 407 vtscsi_detach(device_t dev) 408 { 409 struct vtscsi_softc *sc; 410 int i; 411 412 sc = device_get_softc(dev); 413 414 for (i = 0; i < sc->vtscsi_nintr; i++) 415 virtio_teardown_intr(dev, i); 416 417 VTSCSI_LOCK(sc); 418 sc->vtscsi_flags |= VTSCSI_FLAG_DETACH; 419 if (device_is_attached(dev)) 420 vtscsi_stop(sc); 421 VTSCSI_UNLOCK(sc); 422 423 vtscsi_complete_vqs(sc); 424 vtscsi_drain_vqs(sc); 425 426 vtscsi_free_cam(sc); 427 vtscsi_free_requests(sc); 428 429 if (sc->vtscsi_sglist != NULL) { 430 sglist_free(sc->vtscsi_sglist); 431 sc->vtscsi_sglist = NULL; 432 } 433 434 VTSCSI_LOCK_DESTROY(sc); 435 436 return (0); 437 } 438 439 static int 440 vtscsi_suspend(device_t dev) 441 { 442 443 return (0); 444 } 445 446 static int 447 vtscsi_resume(device_t dev) 448 { 449 450 return (0); 451 } 452 453 static void 454 vtscsi_negotiate_features(struct vtscsi_softc *sc) 455 { 456 device_t dev; 457 uint64_t features; 458 459 dev = sc->vtscsi_dev; 460 features = virtio_negotiate_features(dev, VTSCSI_FEATURES); 461 sc->vtscsi_features = features; 462 } 463 464 #define VTSCSI_GET_CONFIG(_dev, _field, _cfg) \ 465 virtio_read_device_config(_dev, \ 466 offsetof(struct virtio_scsi_config, _field), \ 467 &(_cfg)->_field, sizeof((_cfg)->_field)) \ 468 469 static void 470 vtscsi_read_config(struct vtscsi_softc *sc, 471 struct virtio_scsi_config *scsicfg) 472 { 473 device_t dev; 474 475 dev = sc->vtscsi_dev; 476 477 bzero(scsicfg, sizeof(struct virtio_scsi_config)); 478 479 VTSCSI_GET_CONFIG(dev, num_queues, scsicfg); 480 VTSCSI_GET_CONFIG(dev, seg_max, scsicfg); 481 VTSCSI_GET_CONFIG(dev, max_sectors, scsicfg); 482 VTSCSI_GET_CONFIG(dev, cmd_per_lun, scsicfg); 483 VTSCSI_GET_CONFIG(dev, event_info_size, scsicfg); 484 VTSCSI_GET_CONFIG(dev, sense_size, scsicfg); 485 VTSCSI_GET_CONFIG(dev, cdb_size, scsicfg); 486 VTSCSI_GET_CONFIG(dev, max_channel, scsicfg); 487 VTSCSI_GET_CONFIG(dev, max_target, scsicfg); 488 VTSCSI_GET_CONFIG(dev, max_lun, scsicfg); 489 } 490 491 #undef VTSCSI_GET_CONFIG 492 493 static int 494 vtscsi_maximum_segments(struct vtscsi_softc *sc, int seg_max) 495 { 496 int nsegs; 497 498 nsegs = VTSCSI_MIN_SEGMENTS; 499 500 if (seg_max > 0) { 501 nsegs += MIN(seg_max, MAXPHYS / PAGE_SIZE + 1); 502 if (sc->vtscsi_flags & VTSCSI_FLAG_INDIRECT) 503 nsegs = MIN(nsegs, VIRTIO_MAX_INDIRECT); 504 } else 505 nsegs += 1; 506 507 return (nsegs); 508 } 509 510 511 static int 512 vtscsi_alloc_intrs(struct vtscsi_softc *sc) 513 { 514 int intrcount = virtio_intr_count(sc->vtscsi_dev); 515 int cnt, i, error; 516 517 for (i = 0; i < NELEM(sc->vtscsi_cpus); i++) 518 sc->vtscsi_cpus[i] = -1; 519 520 intrcount = imin(intrcount, 3); 521 if (intrcount < 1) 522 return (ENXIO); 523 524 cnt = intrcount; 525 error = virtio_intr_alloc(sc->vtscsi_dev, &cnt, 0, sc->vtscsi_cpus); 526 if (error != 0) { 527 virtio_intr_release(sc->vtscsi_dev); 528 return (error); 529 } 530 sc->vtscsi_nintr = cnt; 531 return (0); 532 } 533 534 static int 535 vtscsi_alloc_virtqueues(struct vtscsi_softc *sc) 536 { 537 device_t dev = sc->vtscsi_dev; 538 struct vq_alloc_info vq_info[3]; 539 int nvqs = 3; 540 541 VQ_ALLOC_INFO_INIT(&vq_info[0], 0, &sc->vtscsi_control_vq, 542 "%s control", device_get_nameunit(dev)); 543 544 VQ_ALLOC_INFO_INIT(&vq_info[1], 0, &sc->vtscsi_event_vq, 545 "%s event", device_get_nameunit(dev)); 546 547 VQ_ALLOC_INFO_INIT(&vq_info[2], sc->vtscsi_max_nsegs, 548 &sc->vtscsi_request_vq, "%s request", device_get_nameunit(dev)); 549 550 return (virtio_alloc_virtqueues(dev, nvqs, vq_info)); 551 } 552 553 static void 554 vtscsi_write_device_config(struct vtscsi_softc *sc) 555 { 556 557 virtio_write_dev_config_4(sc->vtscsi_dev, 558 offsetof(struct virtio_scsi_config, sense_size), 559 VIRTIO_SCSI_SENSE_SIZE); 560 561 /* 562 * This is the size in the virtio_scsi_cmd_req structure. Note 563 * this value (32) is larger than the maximum CAM CDB size (16). 564 */ 565 virtio_write_dev_config_4(sc->vtscsi_dev, 566 offsetof(struct virtio_scsi_config, cdb_size), 567 VIRTIO_SCSI_CDB_SIZE); 568 } 569 570 static int 571 vtscsi_reinit(struct vtscsi_softc *sc) 572 { 573 device_t dev; 574 int error; 575 576 dev = sc->vtscsi_dev; 577 578 error = virtio_reinit(dev, sc->vtscsi_features); 579 if (error == 0) { 580 vtscsi_write_device_config(sc); 581 vtscsi_reinit_event_vq(sc); 582 virtio_reinit_complete(dev); 583 584 vtscsi_enable_vqs_intr(sc); 585 } 586 587 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d\n", error); 588 589 return (error); 590 } 591 592 static int 593 vtscsi_alloc_cam(struct vtscsi_softc *sc) 594 { 595 device_t dev; 596 struct cam_devq *devq; 597 int openings; 598 599 dev = sc->vtscsi_dev; 600 openings = sc->vtscsi_nrequests - VTSCSI_RESERVED_REQUESTS; 601 602 devq = cam_simq_alloc(openings); 603 if (devq == NULL) { 604 device_printf(dev, "cannot allocate SIM queue\n"); 605 return (ENOMEM); 606 } 607 608 sc->vtscsi_sim = cam_sim_alloc(vtscsi_cam_action, vtscsi_cam_poll, 609 "vtscsi", sc, device_get_unit(dev), VTSCSI_MTX(sc), 1, 610 openings, devq); 611 cam_simq_release(devq); 612 if (sc->vtscsi_sim == NULL) { 613 device_printf(dev, "cannot allocate SIM\n"); 614 return (ENOMEM); 615 } 616 617 return (0); 618 } 619 620 static int 621 vtscsi_register_cam(struct vtscsi_softc *sc) 622 { 623 device_t dev; 624 int registered, error; 625 626 dev = sc->vtscsi_dev; 627 registered = 0; 628 629 VTSCSI_LOCK(sc); 630 631 if (xpt_bus_register(sc->vtscsi_sim, 0) != CAM_SUCCESS) { 632 error = ENOMEM; 633 device_printf(dev, "cannot register XPT bus\n"); 634 goto fail; 635 } 636 637 registered = 1; 638 639 if (xpt_create_path(&sc->vtscsi_path, NULL, 640 cam_sim_path(sc->vtscsi_sim), CAM_TARGET_WILDCARD, 641 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 642 error = ENOMEM; 643 device_printf(dev, "cannot create bus path\n"); 644 goto fail; 645 } 646 647 if (vtscsi_register_async(sc) != CAM_REQ_CMP) { 648 error = EIO; 649 device_printf(dev, "cannot register async callback\n"); 650 goto fail; 651 } 652 653 VTSCSI_UNLOCK(sc); 654 655 return (0); 656 657 fail: 658 if (sc->vtscsi_path != NULL) { 659 xpt_free_path(sc->vtscsi_path); 660 sc->vtscsi_path = NULL; 661 } 662 663 if (registered != 0) 664 xpt_bus_deregister(cam_sim_path(sc->vtscsi_sim)); 665 666 VTSCSI_UNLOCK(sc); 667 668 return (error); 669 } 670 671 static void 672 vtscsi_free_cam(struct vtscsi_softc *sc) 673 { 674 675 VTSCSI_LOCK(sc); 676 677 if (sc->vtscsi_path != NULL) { 678 vtscsi_deregister_async(sc); 679 680 xpt_free_path(sc->vtscsi_path); 681 sc->vtscsi_path = NULL; 682 683 xpt_bus_deregister(cam_sim_path(sc->vtscsi_sim)); 684 } 685 686 if (sc->vtscsi_sim != NULL) { 687 cam_sim_free(sc->vtscsi_sim); 688 sc->vtscsi_sim = NULL; 689 } 690 691 VTSCSI_UNLOCK(sc); 692 } 693 694 static void 695 vtscsi_cam_async(void *cb_arg, uint32_t code, struct cam_path *path, void *arg) 696 { 697 struct cam_sim *sim; 698 struct vtscsi_softc *sc; 699 700 sim = cb_arg; 701 sc = cam_sim_softc(sim); 702 703 vtscsi_dprintf(sc, VTSCSI_TRACE, "code=%u\n", code); 704 705 /* 706 * TODO Once QEMU supports event reporting, we should 707 * (un)subscribe to events here. 708 */ 709 switch (code) { 710 case AC_FOUND_DEVICE: 711 break; 712 case AC_LOST_DEVICE: 713 break; 714 } 715 } 716 717 static int 718 vtscsi_register_async(struct vtscsi_softc *sc) 719 { 720 struct ccb_setasync csa; 721 722 xpt_setup_ccb(&csa.ccb_h, sc->vtscsi_path, 5); 723 csa.ccb_h.func_code = XPT_SASYNC_CB; 724 csa.event_enable = AC_LOST_DEVICE | AC_FOUND_DEVICE; 725 csa.callback = vtscsi_cam_async; 726 csa.callback_arg = sc->vtscsi_sim; 727 728 xpt_action((union ccb *) &csa); 729 730 return (csa.ccb_h.status); 731 } 732 733 static void 734 vtscsi_deregister_async(struct vtscsi_softc *sc) 735 { 736 struct ccb_setasync csa; 737 738 xpt_setup_ccb(&csa.ccb_h, sc->vtscsi_path, 5); 739 csa.ccb_h.func_code = XPT_SASYNC_CB; 740 csa.event_enable = 0; 741 csa.callback = vtscsi_cam_async; 742 csa.callback_arg = sc->vtscsi_sim; 743 744 xpt_action((union ccb *) &csa); 745 } 746 747 static void 748 vtscsi_cam_action(struct cam_sim *sim, union ccb *ccb) 749 { 750 struct vtscsi_softc *sc; 751 struct ccb_hdr *ccbh; 752 753 sc = cam_sim_softc(sim); 754 ccbh = &ccb->ccb_h; 755 756 VTSCSI_LOCK_OWNED(sc); 757 758 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) { 759 /* 760 * The VTSCSI_MTX is briefly dropped between setting 761 * VTSCSI_FLAG_DETACH and deregistering with CAM, so 762 * drop any CCBs that come in during that window. 763 */ 764 ccbh->status = CAM_NO_HBA; 765 xpt_done(ccb); 766 return; 767 } 768 769 switch (ccbh->func_code) { 770 case XPT_SCSI_IO: 771 vtscsi_cam_scsi_io(sc, sim, ccb); 772 break; 773 774 case XPT_SET_TRAN_SETTINGS: 775 ccbh->status = CAM_FUNC_NOTAVAIL; 776 xpt_done(ccb); 777 break; 778 779 case XPT_GET_TRAN_SETTINGS: 780 vtscsi_cam_get_tran_settings(sc, ccb); 781 break; 782 783 case XPT_RESET_BUS: 784 vtscsi_cam_reset_bus(sc, ccb); 785 break; 786 787 case XPT_RESET_DEV: 788 vtscsi_cam_reset_dev(sc, ccb); 789 break; 790 791 case XPT_ABORT: 792 vtscsi_cam_abort(sc, ccb); 793 break; 794 795 case XPT_CALC_GEOMETRY: 796 cam_calc_geometry(&ccb->ccg, 1); 797 xpt_done(ccb); 798 break; 799 800 case XPT_PATH_INQ: 801 vtscsi_cam_path_inquiry(sc, sim, ccb); 802 break; 803 804 default: 805 vtscsi_dprintf(sc, VTSCSI_ERROR, 806 "invalid ccb=%p func=%#x\n", ccb, ccbh->func_code); 807 808 ccbh->status = CAM_REQ_INVALID; 809 xpt_done(ccb); 810 break; 811 } 812 } 813 814 static void 815 vtscsi_cam_poll(struct cam_sim *sim) 816 { 817 struct vtscsi_softc *sc; 818 819 sc = cam_sim_softc(sim); 820 821 vtscsi_complete_vqs_locked(sc); 822 } 823 824 static void 825 vtscsi_cam_scsi_io(struct vtscsi_softc *sc, struct cam_sim *sim, 826 union ccb *ccb) 827 { 828 struct ccb_hdr *ccbh; 829 struct ccb_scsiio *csio; 830 int error; 831 832 ccbh = &ccb->ccb_h; 833 csio = &ccb->csio; 834 835 if (csio->cdb_len > VIRTIO_SCSI_CDB_SIZE) { 836 error = EINVAL; 837 ccbh->status = CAM_REQ_INVALID; 838 goto done; 839 } 840 841 #ifndef __DragonFly__ /* XXX swildner */ 842 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_BOTH && 843 (sc->vtscsi_flags & VTSCSI_FLAG_BIDIRECTIONAL) == 0) { 844 error = EINVAL; 845 ccbh->status = CAM_REQ_INVALID; 846 goto done; 847 } 848 #endif 849 850 error = vtscsi_start_scsi_cmd(sc, ccb); 851 852 done: 853 if (error) { 854 vtscsi_dprintf(sc, VTSCSI_ERROR, 855 "error=%d ccb=%p status=%#x\n", error, ccb, ccbh->status); 856 xpt_done(ccb); 857 } 858 } 859 860 static void 861 vtscsi_cam_get_tran_settings(struct vtscsi_softc *sc, union ccb *ccb) 862 { 863 struct ccb_trans_settings *cts; 864 struct ccb_trans_settings_scsi *scsi; 865 866 cts = &ccb->cts; 867 scsi = &cts->proto_specific.scsi; 868 869 cts->protocol = PROTO_SCSI; 870 cts->protocol_version = SCSI_REV_SPC3; 871 cts->transport = XPORT_SAS; 872 cts->transport_version = 0; 873 874 scsi->valid = CTS_SCSI_VALID_TQ; 875 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 876 877 ccb->ccb_h.status = CAM_REQ_CMP; 878 xpt_done(ccb); 879 } 880 881 static void 882 vtscsi_cam_reset_bus(struct vtscsi_softc *sc, union ccb *ccb) 883 { 884 int error; 885 886 error = vtscsi_reset_bus(sc); 887 if (error == 0) 888 ccb->ccb_h.status = CAM_REQ_CMP; 889 else 890 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 891 892 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d ccb=%p status=%#x\n", 893 error, ccb, ccb->ccb_h.status); 894 895 xpt_done(ccb); 896 } 897 898 static void 899 vtscsi_cam_reset_dev(struct vtscsi_softc *sc, union ccb *ccb) 900 { 901 struct ccb_hdr *ccbh; 902 struct vtscsi_request *req; 903 int error; 904 905 ccbh = &ccb->ccb_h; 906 907 req = vtscsi_dequeue_request(sc); 908 if (req == NULL) { 909 error = EAGAIN; 910 vtscsi_freeze_simq(sc, VTSCSI_REQUEST); 911 goto fail; 912 } 913 914 req->vsr_ccb = ccb; 915 916 error = vtscsi_execute_reset_dev_cmd(sc, req); 917 if (error == 0) 918 return; 919 920 vtscsi_enqueue_request(sc, req); 921 922 fail: 923 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p\n", 924 error, req, ccb); 925 926 if (error == EAGAIN) 927 ccbh->status = CAM_RESRC_UNAVAIL; 928 else 929 ccbh->status = CAM_REQ_CMP_ERR; 930 931 xpt_done(ccb); 932 } 933 934 static void 935 vtscsi_cam_abort(struct vtscsi_softc *sc, union ccb *ccb) 936 { 937 struct vtscsi_request *req; 938 struct ccb_hdr *ccbh; 939 int error; 940 941 ccbh = &ccb->ccb_h; 942 943 req = vtscsi_dequeue_request(sc); 944 if (req == NULL) { 945 error = EAGAIN; 946 vtscsi_freeze_simq(sc, VTSCSI_REQUEST); 947 goto fail; 948 } 949 950 req->vsr_ccb = ccb; 951 952 error = vtscsi_execute_abort_task_cmd(sc, req); 953 if (error == 0) 954 return; 955 956 vtscsi_enqueue_request(sc, req); 957 958 fail: 959 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p\n", 960 error, req, ccb); 961 962 if (error == EAGAIN) 963 ccbh->status = CAM_RESRC_UNAVAIL; 964 else 965 ccbh->status = CAM_REQ_CMP_ERR; 966 967 xpt_done(ccb); 968 } 969 970 static void 971 vtscsi_cam_path_inquiry(struct vtscsi_softc *sc, struct cam_sim *sim, 972 union ccb *ccb) 973 { 974 device_t dev; 975 struct ccb_pathinq *cpi; 976 977 dev = sc->vtscsi_dev; 978 cpi = &ccb->cpi; 979 980 vtscsi_dprintf(sc, VTSCSI_TRACE, "sim=%p ccb=%p\n", sim, ccb); 981 982 cpi->version_num = 1; 983 cpi->hba_inquiry = PI_TAG_ABLE; 984 cpi->target_sprt = 0; 985 cpi->hba_misc = PIM_SEQSCAN; 986 if (vtscsi_bus_reset_disable != 0) 987 cpi->hba_misc |= PIM_NOBUSRESET; 988 cpi->hba_eng_cnt = 0; 989 990 cpi->max_target = sc->vtscsi_max_target; 991 cpi->max_lun = sc->vtscsi_max_lun; 992 cpi->initiator_id = VTSCSI_INITIATOR_ID; 993 994 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 995 strlcpy(cpi->hba_vid, "VirtIO", HBA_IDLEN); 996 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 997 998 cpi->unit_number = cam_sim_unit(sim); 999 cpi->bus_id = cam_sim_bus(sim); 1000 1001 cpi->base_transfer_speed = 300000; 1002 1003 cpi->protocol = PROTO_SCSI; 1004 cpi->protocol_version = SCSI_REV_SPC3; 1005 cpi->transport = XPORT_SAS; 1006 cpi->transport_version = 0; 1007 1008 cpi->maxio = (sc->vtscsi_max_nsegs - VTSCSI_MIN_SEGMENTS - 1) * 1009 PAGE_SIZE; 1010 1011 #if 0 1012 cpi->hba_vendor = virtio_get_vendor(dev); 1013 cpi->hba_device = virtio_get_device(dev); 1014 cpi->hba_subvendor = virtio_get_subvendor(dev); 1015 cpi->hba_subdevice = virtio_get_subdevice(dev); 1016 #endif 1017 1018 ccb->ccb_h.status = CAM_REQ_CMP; 1019 xpt_done(ccb); 1020 } 1021 1022 static int 1023 vtscsi_sg_append_scsi_buf(struct vtscsi_softc *sc, struct sglist *sg, 1024 struct ccb_scsiio *csio) 1025 { 1026 struct ccb_hdr *ccbh; 1027 struct bus_dma_segment *dseg; 1028 int i, error; 1029 1030 ccbh = &csio->ccb_h; 1031 error = 0; 1032 1033 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) { 1034 1035 if ((ccbh->flags & CAM_DATA_PHYS) == 0) 1036 error = sglist_append(sg, 1037 csio->data_ptr, csio->dxfer_len); 1038 else 1039 error = sglist_append_phys(sg, 1040 (vm_paddr_t)(vm_offset_t) csio->data_ptr, 1041 csio->dxfer_len); 1042 } else { 1043 1044 for (i = 0; i < csio->sglist_cnt && error == 0; i++) { 1045 dseg = &((struct bus_dma_segment *)csio->data_ptr)[i]; 1046 1047 if ((ccbh->flags & CAM_SG_LIST_PHYS) == 0) 1048 error = sglist_append(sg, 1049 (void *)(vm_offset_t) dseg->ds_addr, 1050 dseg->ds_len); 1051 else 1052 error = sglist_append_phys(sg, 1053 (vm_paddr_t) dseg->ds_addr, dseg->ds_len); 1054 } 1055 } 1056 1057 return (error); 1058 } 1059 1060 static int 1061 vtscsi_fill_scsi_cmd_sglist(struct vtscsi_softc *sc, struct vtscsi_request *req, 1062 int *readable, int *writable) 1063 { 1064 struct sglist *sg; 1065 struct ccb_hdr *ccbh; 1066 struct ccb_scsiio *csio; 1067 struct virtio_scsi_cmd_req *cmd_req; 1068 struct virtio_scsi_cmd_resp *cmd_resp; 1069 int error; 1070 1071 sg = sc->vtscsi_sglist; 1072 csio = &req->vsr_ccb->csio; 1073 ccbh = &csio->ccb_h; 1074 cmd_req = &req->vsr_cmd_req; 1075 cmd_resp = &req->vsr_cmd_resp; 1076 1077 sglist_reset(sg); 1078 1079 sglist_append(sg, cmd_req, sizeof(struct virtio_scsi_cmd_req)); 1080 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 1081 error = vtscsi_sg_append_scsi_buf(sc, sg, csio); 1082 /* At least one segment must be left for the response. */ 1083 if (error || sg->sg_nseg == sg->sg_maxseg) 1084 goto fail; 1085 } 1086 1087 *readable = sg->sg_nseg; 1088 1089 sglist_append(sg, cmd_resp, sizeof(struct virtio_scsi_cmd_resp)); 1090 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1091 error = vtscsi_sg_append_scsi_buf(sc, sg, csio); 1092 if (error) 1093 goto fail; 1094 } 1095 1096 *writable = sg->sg_nseg - *readable; 1097 1098 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p readable=%d " 1099 "writable=%d\n", req, ccbh, *readable, *writable); 1100 1101 return (0); 1102 1103 fail: 1104 /* 1105 * This should never happen unless maxio was incorrectly set. 1106 */ 1107 vtscsi_set_ccb_status(ccbh, CAM_REQ_TOO_BIG, 0); 1108 1109 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p " 1110 "nseg=%d maxseg=%d\n", 1111 error, req, ccbh, sg->sg_nseg, sg->sg_maxseg); 1112 1113 return (EFBIG); 1114 } 1115 1116 static int 1117 vtscsi_execute_scsi_cmd(struct vtscsi_softc *sc, struct vtscsi_request *req) 1118 { 1119 struct sglist *sg; 1120 struct virtqueue *vq; 1121 struct ccb_scsiio *csio; 1122 struct ccb_hdr *ccbh; 1123 struct virtio_scsi_cmd_req *cmd_req; 1124 struct virtio_scsi_cmd_resp *cmd_resp; 1125 int readable, writable, error; 1126 1127 sg = sc->vtscsi_sglist; 1128 vq = sc->vtscsi_request_vq; 1129 csio = &req->vsr_ccb->csio; 1130 ccbh = &csio->ccb_h; 1131 cmd_req = &req->vsr_cmd_req; 1132 cmd_resp = &req->vsr_cmd_resp; 1133 1134 vtscsi_init_scsi_cmd_req(csio, cmd_req); 1135 1136 error = vtscsi_fill_scsi_cmd_sglist(sc, req, &readable, &writable); 1137 if (error) 1138 return (error); 1139 1140 req->vsr_complete = vtscsi_complete_scsi_cmd; 1141 cmd_resp->response = -1; 1142 1143 error = virtqueue_enqueue(vq, req, sg, readable, writable); 1144 if (error) { 1145 vtscsi_dprintf(sc, VTSCSI_ERROR, 1146 "enqueue error=%d req=%p ccb=%p\n", error, req, ccbh); 1147 1148 ccbh->status = CAM_REQUEUE_REQ; 1149 vtscsi_freeze_simq(sc, VTSCSI_REQUEST_VQ); 1150 return (error); 1151 } 1152 1153 ccbh->status |= CAM_SIM_QUEUED; 1154 ccbh->ccbh_vtscsi_req = req; 1155 1156 virtqueue_notify(vq, NULL); 1157 1158 if (ccbh->timeout != CAM_TIME_INFINITY) { 1159 req->vsr_flags |= VTSCSI_REQ_FLAG_TIMEOUT_SET; 1160 callout_reset(&req->vsr_callout, ccbh->timeout * hz / 1000, 1161 vtscsi_timedout_scsi_cmd, req); 1162 } 1163 1164 vtscsi_dprintf_req(req, VTSCSI_TRACE, "enqueued req=%p ccb=%p\n", 1165 req, ccbh); 1166 1167 return (0); 1168 } 1169 1170 static int 1171 vtscsi_start_scsi_cmd(struct vtscsi_softc *sc, union ccb *ccb) 1172 { 1173 struct vtscsi_request *req; 1174 int error; 1175 1176 req = vtscsi_dequeue_request(sc); 1177 if (req == NULL) { 1178 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1179 vtscsi_freeze_simq(sc, VTSCSI_REQUEST); 1180 return (ENOBUFS); 1181 } 1182 1183 req->vsr_ccb = ccb; 1184 1185 error = vtscsi_execute_scsi_cmd(sc, req); 1186 if (error) 1187 vtscsi_enqueue_request(sc, req); 1188 1189 return (error); 1190 } 1191 1192 static void 1193 vtscsi_complete_abort_timedout_scsi_cmd(struct vtscsi_softc *sc, 1194 struct vtscsi_request *req) 1195 { 1196 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1197 struct vtscsi_request *to_req; 1198 uint8_t response; 1199 1200 tmf_resp = &req->vsr_tmf_resp; 1201 response = tmf_resp->response; 1202 to_req = req->vsr_timedout_req; 1203 1204 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p to_req=%p response=%d\n", 1205 req, to_req, response); 1206 1207 vtscsi_enqueue_request(sc, req); 1208 1209 /* 1210 * The timedout request could have completed between when the 1211 * abort task was sent and when the host processed it. 1212 */ 1213 if (to_req->vsr_state != VTSCSI_REQ_STATE_TIMEDOUT) 1214 return; 1215 1216 /* The timedout request was successfully aborted. */ 1217 if (response == VIRTIO_SCSI_S_FUNCTION_COMPLETE) 1218 return; 1219 1220 /* Don't bother if the device is going away. */ 1221 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) 1222 return; 1223 1224 /* The timedout request will be aborted by the reset. */ 1225 if (sc->vtscsi_flags & VTSCSI_FLAG_RESET) 1226 return; 1227 1228 vtscsi_reset_bus(sc); 1229 } 1230 1231 static int 1232 vtscsi_abort_timedout_scsi_cmd(struct vtscsi_softc *sc, 1233 struct vtscsi_request *to_req) 1234 { 1235 struct sglist *sg; 1236 struct ccb_hdr *to_ccbh; 1237 struct vtscsi_request *req; 1238 struct virtio_scsi_ctrl_tmf_req *tmf_req; 1239 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1240 int error; 1241 1242 sg = sc->vtscsi_sglist; 1243 to_ccbh = &to_req->vsr_ccb->ccb_h; 1244 1245 req = vtscsi_dequeue_request(sc); 1246 if (req == NULL) { 1247 error = ENOBUFS; 1248 goto fail; 1249 } 1250 1251 tmf_req = &req->vsr_tmf_req; 1252 tmf_resp = &req->vsr_tmf_resp; 1253 1254 vtscsi_init_ctrl_tmf_req(to_ccbh, VIRTIO_SCSI_T_TMF_ABORT_TASK, 1255 (uintptr_t) to_ccbh, tmf_req); 1256 1257 sglist_reset(sg); 1258 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req)); 1259 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp)); 1260 1261 req->vsr_timedout_req = to_req; 1262 req->vsr_complete = vtscsi_complete_abort_timedout_scsi_cmd; 1263 tmf_resp->response = -1; 1264 1265 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1, 1266 VTSCSI_EXECUTE_ASYNC); 1267 if (error == 0) 1268 return (0); 1269 1270 vtscsi_enqueue_request(sc, req); 1271 1272 fail: 1273 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p " 1274 "timedout req=%p ccb=%p\n", error, req, to_req, to_ccbh); 1275 1276 return (error); 1277 } 1278 1279 static void 1280 vtscsi_timedout_scsi_cmd(void *xreq) 1281 { 1282 struct vtscsi_softc *sc; 1283 struct vtscsi_request *to_req; 1284 1285 to_req = xreq; 1286 sc = to_req->vsr_softc; 1287 1288 vtscsi_dprintf(sc, VTSCSI_INFO, "timedout req=%p ccb=%p state=%#x\n", 1289 to_req, to_req->vsr_ccb, to_req->vsr_state); 1290 1291 /* Don't bother if the device is going away. */ 1292 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) 1293 return; 1294 1295 /* 1296 * Bail if the request is not in use. We likely raced when 1297 * stopping the callout handler or it has already been aborted. 1298 */ 1299 if (to_req->vsr_state != VTSCSI_REQ_STATE_INUSE || 1300 (to_req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) == 0) 1301 return; 1302 1303 /* 1304 * Complete the request queue in case the timedout request is 1305 * actually just pending. 1306 */ 1307 vtscsi_complete_vq(sc, sc->vtscsi_request_vq); 1308 if (to_req->vsr_state == VTSCSI_REQ_STATE_FREE) 1309 return; 1310 1311 sc->vtscsi_stats.scsi_cmd_timeouts++; 1312 to_req->vsr_state = VTSCSI_REQ_STATE_TIMEDOUT; 1313 1314 if (vtscsi_abort_timedout_scsi_cmd(sc, to_req) == 0) 1315 return; 1316 1317 vtscsi_dprintf(sc, VTSCSI_ERROR, "resetting bus\n"); 1318 vtscsi_reset_bus(sc); 1319 } 1320 1321 static cam_status 1322 vtscsi_scsi_cmd_cam_status(struct virtio_scsi_cmd_resp *cmd_resp) 1323 { 1324 cam_status status; 1325 1326 switch (cmd_resp->response) { 1327 case VIRTIO_SCSI_S_OK: 1328 status = CAM_REQ_CMP; 1329 break; 1330 case VIRTIO_SCSI_S_OVERRUN: 1331 status = CAM_DATA_RUN_ERR; 1332 break; 1333 case VIRTIO_SCSI_S_ABORTED: 1334 status = CAM_REQ_ABORTED; 1335 break; 1336 case VIRTIO_SCSI_S_BAD_TARGET: 1337 status = CAM_SEL_TIMEOUT; 1338 break; 1339 case VIRTIO_SCSI_S_RESET: 1340 status = CAM_SCSI_BUS_RESET; 1341 break; 1342 case VIRTIO_SCSI_S_BUSY: 1343 status = CAM_SCSI_BUSY; 1344 break; 1345 case VIRTIO_SCSI_S_TRANSPORT_FAILURE: 1346 case VIRTIO_SCSI_S_TARGET_FAILURE: 1347 case VIRTIO_SCSI_S_NEXUS_FAILURE: 1348 status = CAM_SCSI_IT_NEXUS_LOST; 1349 break; 1350 default: /* VIRTIO_SCSI_S_FAILURE */ 1351 status = CAM_REQ_CMP_ERR; 1352 break; 1353 } 1354 1355 return (status); 1356 } 1357 1358 static cam_status 1359 vtscsi_complete_scsi_cmd_response(struct vtscsi_softc *sc, 1360 struct ccb_scsiio *csio, struct virtio_scsi_cmd_resp *cmd_resp) 1361 { 1362 cam_status status; 1363 1364 csio->scsi_status = cmd_resp->status; 1365 csio->resid = cmd_resp->resid; 1366 1367 if (csio->scsi_status == SCSI_STATUS_OK) 1368 status = CAM_REQ_CMP; 1369 else 1370 status = CAM_SCSI_STATUS_ERROR; 1371 1372 if (cmd_resp->sense_len > 0) { 1373 status |= CAM_AUTOSNS_VALID; 1374 1375 if (cmd_resp->sense_len < csio->sense_len) 1376 csio->sense_resid = csio->sense_len - 1377 cmd_resp->sense_len; 1378 else 1379 csio->sense_resid = 0; 1380 1381 bzero(&csio->sense_data, sizeof(csio->sense_data)); 1382 memcpy(cmd_resp->sense, &csio->sense_data, 1383 csio->sense_len - csio->sense_resid); 1384 } 1385 1386 vtscsi_dprintf(sc, status == CAM_REQ_CMP ? VTSCSI_TRACE : VTSCSI_ERROR, 1387 "ccb=%p scsi_status=%#x resid=%u sense_resid=%u\n", 1388 csio, csio->scsi_status, csio->resid, csio->sense_resid); 1389 1390 return (status); 1391 } 1392 1393 static void 1394 vtscsi_complete_scsi_cmd(struct vtscsi_softc *sc, struct vtscsi_request *req) 1395 { 1396 struct ccb_hdr *ccbh; 1397 struct ccb_scsiio *csio; 1398 struct virtio_scsi_cmd_resp *cmd_resp; 1399 cam_status status; 1400 1401 csio = &req->vsr_ccb->csio; 1402 ccbh = &csio->ccb_h; 1403 cmd_resp = &req->vsr_cmd_resp; 1404 1405 KASSERT(ccbh->ccbh_vtscsi_req == req, 1406 ("ccb %p req mismatch %p/%p", ccbh, ccbh->ccbh_vtscsi_req, req)); 1407 1408 if (req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) 1409 callout_stop(&req->vsr_callout); 1410 1411 status = vtscsi_scsi_cmd_cam_status(cmd_resp); 1412 if (status == CAM_REQ_ABORTED) { 1413 if (req->vsr_state == VTSCSI_REQ_STATE_TIMEDOUT) 1414 status = CAM_CMD_TIMEOUT; 1415 } else if (status == CAM_REQ_CMP) 1416 status = vtscsi_complete_scsi_cmd_response(sc, csio, cmd_resp); 1417 1418 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1419 status |= CAM_DEV_QFRZN; 1420 xpt_freeze_devq(ccbh->path, 1); 1421 } 1422 1423 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST | VTSCSI_REQUEST_VQ) != 0) 1424 status |= CAM_RELEASE_SIMQ; 1425 1426 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p status=%#x\n", 1427 req, ccbh, status); 1428 1429 ccbh->status = status; 1430 xpt_done(req->vsr_ccb); 1431 vtscsi_enqueue_request(sc, req); 1432 } 1433 1434 static void 1435 vtscsi_poll_ctrl_req(struct vtscsi_softc *sc, struct vtscsi_request *req) 1436 { 1437 1438 /* XXX We probably shouldn't poll forever. */ 1439 req->vsr_flags |= VTSCSI_REQ_FLAG_POLLED; 1440 do 1441 vtscsi_complete_vq(sc, sc->vtscsi_control_vq); 1442 while ((req->vsr_flags & VTSCSI_REQ_FLAG_COMPLETE) == 0); 1443 1444 req->vsr_flags &= ~VTSCSI_REQ_FLAG_POLLED; 1445 } 1446 1447 static int 1448 vtscsi_execute_ctrl_req(struct vtscsi_softc *sc, struct vtscsi_request *req, 1449 struct sglist *sg, int readable, int writable, int flag) 1450 { 1451 struct virtqueue *vq; 1452 int error; 1453 1454 vq = sc->vtscsi_control_vq; 1455 1456 KKASSERT(flag == VTSCSI_EXECUTE_POLL || req->vsr_complete != NULL); 1457 1458 error = virtqueue_enqueue(vq, req, sg, readable, writable); 1459 if (error) { 1460 /* 1461 * Return EAGAIN when the virtqueue does not have enough 1462 * descriptors available. 1463 */ 1464 if (error == ENOSPC || error == EMSGSIZE) 1465 error = EAGAIN; 1466 1467 return (error); 1468 } 1469 1470 virtqueue_notify(vq, NULL); 1471 if (flag == VTSCSI_EXECUTE_POLL) 1472 vtscsi_poll_ctrl_req(sc, req); 1473 1474 return (0); 1475 } 1476 1477 static void 1478 vtscsi_complete_abort_task_cmd(struct vtscsi_softc *sc, 1479 struct vtscsi_request *req) 1480 { 1481 union ccb *ccb; 1482 struct ccb_hdr *ccbh; 1483 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1484 1485 ccb = req->vsr_ccb; 1486 ccbh = &ccb->ccb_h; 1487 tmf_resp = &req->vsr_tmf_resp; 1488 1489 switch (tmf_resp->response) { 1490 case VIRTIO_SCSI_S_FUNCTION_COMPLETE: 1491 ccbh->status = CAM_REQ_CMP; 1492 break; 1493 case VIRTIO_SCSI_S_FUNCTION_REJECTED: 1494 ccbh->status = CAM_UA_ABORT; 1495 break; 1496 default: 1497 ccbh->status = CAM_REQ_CMP_ERR; 1498 break; 1499 } 1500 1501 xpt_done(ccb); 1502 vtscsi_enqueue_request(sc, req); 1503 } 1504 1505 static int 1506 vtscsi_execute_abort_task_cmd(struct vtscsi_softc *sc, 1507 struct vtscsi_request *req) 1508 { 1509 struct sglist *sg; 1510 struct ccb_abort *cab; 1511 struct ccb_hdr *ccbh; 1512 struct ccb_hdr *abort_ccbh; 1513 struct vtscsi_request *abort_req; 1514 struct virtio_scsi_ctrl_tmf_req *tmf_req; 1515 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1516 int error; 1517 1518 sg = sc->vtscsi_sglist; 1519 cab = &req->vsr_ccb->cab; 1520 ccbh = &cab->ccb_h; 1521 tmf_req = &req->vsr_tmf_req; 1522 tmf_resp = &req->vsr_tmf_resp; 1523 1524 /* CCB header and request that's to be aborted. */ 1525 abort_ccbh = &cab->abort_ccb->ccb_h; 1526 abort_req = abort_ccbh->ccbh_vtscsi_req; 1527 1528 if (abort_ccbh->func_code != XPT_SCSI_IO || abort_req == NULL) { 1529 error = EINVAL; 1530 goto fail; 1531 } 1532 1533 /* Only attempt to abort requests that could be in-flight. */ 1534 if (abort_req->vsr_state != VTSCSI_REQ_STATE_INUSE) { 1535 error = EALREADY; 1536 goto fail; 1537 } 1538 1539 abort_req->vsr_state = VTSCSI_REQ_STATE_ABORTED; 1540 if (abort_req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) 1541 callout_stop(&abort_req->vsr_callout); 1542 1543 vtscsi_init_ctrl_tmf_req(ccbh, VIRTIO_SCSI_T_TMF_ABORT_TASK, 1544 (uintptr_t) abort_ccbh, tmf_req); 1545 1546 sglist_reset(sg); 1547 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req)); 1548 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp)); 1549 1550 req->vsr_complete = vtscsi_complete_abort_task_cmd; 1551 tmf_resp->response = -1; 1552 1553 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1, 1554 VTSCSI_EXECUTE_ASYNC); 1555 1556 fail: 1557 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d req=%p abort_ccb=%p " 1558 "abort_req=%p\n", error, req, abort_ccbh, abort_req); 1559 1560 return (error); 1561 } 1562 1563 static void 1564 vtscsi_complete_reset_dev_cmd(struct vtscsi_softc *sc, 1565 struct vtscsi_request *req) 1566 { 1567 union ccb *ccb; 1568 struct ccb_hdr *ccbh; 1569 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1570 1571 ccb = req->vsr_ccb; 1572 ccbh = &ccb->ccb_h; 1573 tmf_resp = &req->vsr_tmf_resp; 1574 1575 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p response=%d\n", 1576 req, ccb, tmf_resp->response); 1577 1578 if (tmf_resp->response == VIRTIO_SCSI_S_FUNCTION_COMPLETE) { 1579 ccbh->status = CAM_REQ_CMP; 1580 vtscsi_announce(sc, AC_SENT_BDR, ccbh->target_id, 1581 ccbh->target_lun); 1582 } else 1583 ccbh->status = CAM_REQ_CMP_ERR; 1584 1585 xpt_done(ccb); 1586 vtscsi_enqueue_request(sc, req); 1587 } 1588 1589 static int 1590 vtscsi_execute_reset_dev_cmd(struct vtscsi_softc *sc, 1591 struct vtscsi_request *req) 1592 { 1593 struct sglist *sg; 1594 struct ccb_resetdev *crd; 1595 struct ccb_hdr *ccbh; 1596 struct virtio_scsi_ctrl_tmf_req *tmf_req; 1597 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1598 uint32_t subtype; 1599 int error; 1600 1601 sg = sc->vtscsi_sglist; 1602 crd = &req->vsr_ccb->crd; 1603 ccbh = &crd->ccb_h; 1604 tmf_req = &req->vsr_tmf_req; 1605 tmf_resp = &req->vsr_tmf_resp; 1606 1607 if (ccbh->target_lun == CAM_LUN_WILDCARD) 1608 subtype = VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET; 1609 else 1610 subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET; 1611 1612 vtscsi_init_ctrl_tmf_req(ccbh, subtype, 0, tmf_req); 1613 1614 sglist_reset(sg); 1615 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req)); 1616 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp)); 1617 1618 req->vsr_complete = vtscsi_complete_reset_dev_cmd; 1619 tmf_resp->response = -1; 1620 1621 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1, 1622 VTSCSI_EXECUTE_ASYNC); 1623 1624 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d req=%p ccb=%p\n", 1625 error, req, ccbh); 1626 1627 return (error); 1628 } 1629 1630 static void 1631 vtscsi_get_request_lun(uint8_t lun[], target_id_t *target_id, lun_id_t *lun_id) 1632 { 1633 1634 *target_id = lun[1]; 1635 *lun_id = (lun[2] << 8) | lun[3]; 1636 } 1637 1638 static void 1639 vtscsi_set_request_lun(struct ccb_hdr *ccbh, uint8_t lun[]) 1640 { 1641 1642 lun[0] = 1; 1643 lun[1] = ccbh->target_id; 1644 lun[2] = 0x40 | ((ccbh->target_lun >> 8) & 0x3F); 1645 lun[3] = ccbh->target_lun & 0xFF; 1646 } 1647 1648 static void 1649 vtscsi_init_scsi_cmd_req(struct ccb_scsiio *csio, 1650 struct virtio_scsi_cmd_req *cmd_req) 1651 { 1652 uint8_t attr; 1653 1654 switch (csio->tag_action) { 1655 case MSG_HEAD_OF_Q_TAG: 1656 attr = VIRTIO_SCSI_S_HEAD; 1657 break; 1658 case MSG_ORDERED_Q_TAG: 1659 attr = VIRTIO_SCSI_S_ORDERED; 1660 break; 1661 case MSG_ACA_TASK: 1662 attr = VIRTIO_SCSI_S_ACA; 1663 break; 1664 default: /* MSG_SIMPLE_Q_TAG */ 1665 attr = VIRTIO_SCSI_S_SIMPLE; 1666 break; 1667 } 1668 1669 vtscsi_set_request_lun(&csio->ccb_h, cmd_req->lun); 1670 cmd_req->tag = (uintptr_t) csio; 1671 cmd_req->task_attr = attr; 1672 1673 memcpy(cmd_req->cdb, 1674 csio->ccb_h.flags & CAM_CDB_POINTER ? 1675 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes, 1676 csio->cdb_len); 1677 } 1678 1679 static void 1680 vtscsi_init_ctrl_tmf_req(struct ccb_hdr *ccbh, uint32_t subtype, 1681 uintptr_t tag, struct virtio_scsi_ctrl_tmf_req *tmf_req) 1682 { 1683 1684 vtscsi_set_request_lun(ccbh, tmf_req->lun); 1685 1686 tmf_req->type = VIRTIO_SCSI_T_TMF; 1687 tmf_req->subtype = subtype; 1688 tmf_req->tag = tag; 1689 } 1690 1691 static void 1692 vtscsi_freeze_simq(struct vtscsi_softc *sc, int reason) 1693 { 1694 int frozen; 1695 1696 frozen = sc->vtscsi_frozen; 1697 1698 if (reason & VTSCSI_REQUEST && 1699 (sc->vtscsi_frozen & VTSCSI_FROZEN_NO_REQUESTS) == 0) 1700 sc->vtscsi_frozen |= VTSCSI_FROZEN_NO_REQUESTS; 1701 1702 if (reason & VTSCSI_REQUEST_VQ && 1703 (sc->vtscsi_frozen & VTSCSI_FROZEN_REQUEST_VQ_FULL) == 0) 1704 sc->vtscsi_frozen |= VTSCSI_FROZEN_REQUEST_VQ_FULL; 1705 1706 /* Freeze the SIMQ if transitioned to frozen. */ 1707 if (frozen == 0 && sc->vtscsi_frozen != 0) { 1708 vtscsi_dprintf(sc, VTSCSI_INFO, "SIMQ frozen\n"); 1709 xpt_freeze_simq(sc->vtscsi_sim, 1); 1710 } 1711 } 1712 1713 static int 1714 vtscsi_thaw_simq(struct vtscsi_softc *sc, int reason) 1715 { 1716 int thawed; 1717 1718 if (sc->vtscsi_frozen == 0 || reason == 0) 1719 return (0); 1720 1721 if (reason & VTSCSI_REQUEST && 1722 sc->vtscsi_frozen & VTSCSI_FROZEN_NO_REQUESTS) 1723 sc->vtscsi_frozen &= ~VTSCSI_FROZEN_NO_REQUESTS; 1724 1725 if (reason & VTSCSI_REQUEST_VQ && 1726 sc->vtscsi_frozen & VTSCSI_FROZEN_REQUEST_VQ_FULL) 1727 sc->vtscsi_frozen &= ~VTSCSI_FROZEN_REQUEST_VQ_FULL; 1728 1729 thawed = sc->vtscsi_frozen == 0; 1730 if (thawed != 0) 1731 vtscsi_dprintf(sc, VTSCSI_INFO, "SIMQ thawed\n"); 1732 1733 return (thawed); 1734 } 1735 1736 static void 1737 vtscsi_announce(struct vtscsi_softc *sc, uint32_t ac_code, 1738 target_id_t target_id, lun_id_t lun_id) 1739 { 1740 struct cam_path *path; 1741 1742 /* Use the wildcard path from our softc for bus announcements. */ 1743 if (target_id == CAM_TARGET_WILDCARD && lun_id == CAM_LUN_WILDCARD) { 1744 xpt_async(ac_code, sc->vtscsi_path, NULL); 1745 return; 1746 } 1747 1748 if (xpt_create_path(&path, NULL, cam_sim_path(sc->vtscsi_sim), 1749 target_id, lun_id) != CAM_REQ_CMP) { 1750 vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot create path\n"); 1751 return; 1752 } 1753 1754 xpt_async(ac_code, path, NULL); 1755 xpt_free_path(path); 1756 } 1757 1758 static void 1759 vtscsi_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) 1760 { 1761 xpt_free_path(ccb->ccb_h.path); 1762 xpt_free_ccb(ccb); 1763 } 1764 1765 static void 1766 vtscsi_execute_rescan(struct vtscsi_softc *sc, target_id_t target_id, 1767 lun_id_t lun_id) 1768 { 1769 union ccb *ccb; 1770 cam_status status; 1771 1772 ccb = xpt_alloc_ccb(); 1773 if (ccb == NULL) { 1774 vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot allocate CCB\n"); 1775 return; 1776 } 1777 1778 status = xpt_create_path(&ccb->ccb_h.path, NULL, 1779 cam_sim_path(sc->vtscsi_sim), target_id, lun_id); 1780 if (status != CAM_REQ_CMP) { 1781 xpt_free_ccb(ccb); 1782 return; 1783 } 1784 1785 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/); 1786 ccb->ccb_h.func_code = XPT_SCAN_LUN; 1787 ccb->ccb_h.cbfcnp = vtscsi_cam_rescan_callback; 1788 ccb->crcn.flags = CAM_FLAG_NONE; 1789 xpt_action(ccb); 1790 } 1791 1792 static void 1793 vtscsi_execute_rescan_bus(struct vtscsi_softc *sc) 1794 { 1795 union ccb *ccb; 1796 cam_status status; 1797 1798 ccb = xpt_alloc_ccb(); 1799 if (ccb == NULL) { 1800 vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot allocate CCB\n"); 1801 return; 1802 } 1803 1804 status = xpt_create_path(&ccb->ccb_h.path, NULL, 1805 cam_sim_path(sc->vtscsi_sim), 1806 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 1807 if (status != CAM_REQ_CMP) { 1808 xpt_free_ccb(ccb); 1809 return; 1810 } 1811 1812 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/); 1813 ccb->ccb_h.func_code = XPT_SCAN_BUS; 1814 ccb->ccb_h.cbfcnp = vtscsi_cam_rescan_callback; 1815 ccb->crcn.flags = CAM_FLAG_NONE; 1816 xpt_action(ccb); 1817 } 1818 1819 static void 1820 vtscsi_transport_reset_event(struct vtscsi_softc *sc, 1821 struct virtio_scsi_event *event) 1822 { 1823 target_id_t target_id; 1824 lun_id_t lun_id; 1825 1826 vtscsi_get_request_lun(event->lun, &target_id, &lun_id); 1827 1828 switch (event->reason) { 1829 case VIRTIO_SCSI_EVT_RESET_RESCAN: 1830 case VIRTIO_SCSI_EVT_RESET_REMOVED: 1831 vtscsi_execute_rescan(sc, target_id, lun_id); 1832 break; 1833 default: 1834 device_printf(sc->vtscsi_dev, 1835 "unhandled transport event reason: %d\n", event->reason); 1836 break; 1837 } 1838 } 1839 1840 static void 1841 vtscsi_handle_event(struct vtscsi_softc *sc, struct virtio_scsi_event *event) 1842 { 1843 int error; 1844 1845 if ((event->event & VIRTIO_SCSI_T_EVENTS_MISSED) == 0) { 1846 switch (event->event) { 1847 case VIRTIO_SCSI_T_TRANSPORT_RESET: 1848 vtscsi_transport_reset_event(sc, event); 1849 break; 1850 default: 1851 device_printf(sc->vtscsi_dev, 1852 "unhandled event: %d\n", event->event); 1853 break; 1854 } 1855 } else 1856 vtscsi_execute_rescan_bus(sc); 1857 1858 /* 1859 * This should always be successful since the buffer 1860 * was just dequeued. 1861 */ 1862 error = vtscsi_enqueue_event_buf(sc, event); 1863 KASSERT(error == 0, 1864 ("cannot requeue event buffer: %d", error)); 1865 } 1866 1867 static int 1868 vtscsi_enqueue_event_buf(struct vtscsi_softc *sc, 1869 struct virtio_scsi_event *event) 1870 { 1871 struct sglist *sg; 1872 struct virtqueue *vq; 1873 int size, error; 1874 1875 sg = sc->vtscsi_sglist; 1876 vq = sc->vtscsi_event_vq; 1877 size = sc->vtscsi_event_buf_size; 1878 1879 bzero(event, size); 1880 1881 sglist_reset(sg); 1882 error = sglist_append(sg, event, size); 1883 if (error) 1884 return (error); 1885 1886 error = virtqueue_enqueue(vq, event, sg, 0, sg->sg_nseg); 1887 if (error) 1888 return (error); 1889 1890 virtqueue_notify(vq, NULL); 1891 1892 return (0); 1893 } 1894 1895 static int 1896 vtscsi_init_event_vq(struct vtscsi_softc *sc) 1897 { 1898 struct virtio_scsi_event *event; 1899 int i, size, error; 1900 1901 /* 1902 * The first release of QEMU with VirtIO SCSI support would crash 1903 * when attempting to notify the event virtqueue. This was fixed 1904 * when hotplug support was added. 1905 */ 1906 if (sc->vtscsi_flags & VTSCSI_FLAG_HOTPLUG) 1907 size = sc->vtscsi_event_buf_size; 1908 else 1909 size = 0; 1910 1911 if (size < sizeof(struct virtio_scsi_event)) 1912 return (0); 1913 1914 for (i = 0; i < VTSCSI_NUM_EVENT_BUFS; i++) { 1915 event = &sc->vtscsi_event_bufs[i]; 1916 1917 error = vtscsi_enqueue_event_buf(sc, event); 1918 if (error) 1919 break; 1920 } 1921 1922 /* 1923 * Even just one buffer is enough. Missed events are 1924 * denoted with the VIRTIO_SCSI_T_EVENTS_MISSED flag. 1925 */ 1926 if (i > 0) 1927 error = 0; 1928 1929 return (error); 1930 } 1931 1932 static void 1933 vtscsi_reinit_event_vq(struct vtscsi_softc *sc) 1934 { 1935 struct virtio_scsi_event *event; 1936 int i, error; 1937 1938 if ((sc->vtscsi_flags & VTSCSI_FLAG_HOTPLUG) == 0 || 1939 sc->vtscsi_event_buf_size < sizeof(struct virtio_scsi_event)) 1940 return; 1941 1942 for (i = 0; i < VTSCSI_NUM_EVENT_BUFS; i++) { 1943 event = &sc->vtscsi_event_bufs[i]; 1944 1945 error = vtscsi_enqueue_event_buf(sc, event); 1946 if (error) 1947 break; 1948 } 1949 1950 KASSERT(i > 0, ("cannot reinit event vq: %d", error)); 1951 } 1952 1953 static void 1954 vtscsi_drain_event_vq(struct vtscsi_softc *sc) 1955 { 1956 struct virtqueue *vq; 1957 int last; 1958 1959 vq = sc->vtscsi_event_vq; 1960 last = 0; 1961 1962 while (virtqueue_drain(vq, &last) != NULL) 1963 ; 1964 1965 KASSERT(virtqueue_empty(vq), ("eventvq not empty")); 1966 } 1967 1968 static void 1969 vtscsi_complete_vqs_locked(struct vtscsi_softc *sc) 1970 { 1971 1972 VTSCSI_LOCK_OWNED(sc); 1973 1974 if (sc->vtscsi_request_vq != NULL) 1975 vtscsi_complete_vq(sc, sc->vtscsi_request_vq); 1976 if (sc->vtscsi_control_vq != NULL) 1977 vtscsi_complete_vq(sc, sc->vtscsi_control_vq); 1978 } 1979 1980 static void 1981 vtscsi_complete_vqs(struct vtscsi_softc *sc) 1982 { 1983 1984 VTSCSI_LOCK(sc); 1985 vtscsi_complete_vqs_locked(sc); 1986 VTSCSI_UNLOCK(sc); 1987 } 1988 1989 static void 1990 vtscsi_cancel_request(struct vtscsi_softc *sc, struct vtscsi_request *req) 1991 { 1992 union ccb *ccb; 1993 int detach; 1994 1995 ccb = req->vsr_ccb; 1996 1997 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p\n", req, ccb); 1998 1999 /* 2000 * The callout must be drained when detaching since the request is 2001 * about to be freed. The VTSCSI_MTX must not be held for this in 2002 * case the callout is pending because there is a deadlock potential. 2003 * Otherwise, the virtqueue is being drained because of a bus reset 2004 * so we only need to attempt to stop the callouts. 2005 */ 2006 detach = (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) != 0; 2007 if (detach != 0) 2008 VTSCSI_LOCK_NOTOWNED(sc); 2009 else 2010 VTSCSI_LOCK_OWNED(sc); 2011 2012 if (req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) { 2013 if (detach != 0) 2014 callout_drain(&req->vsr_callout); 2015 else 2016 callout_stop(&req->vsr_callout); 2017 } 2018 2019 if (ccb != NULL) { 2020 if (detach != 0) { 2021 VTSCSI_LOCK(sc); 2022 ccb->ccb_h.status = CAM_NO_HBA; 2023 } else 2024 ccb->ccb_h.status = CAM_REQUEUE_REQ; 2025 xpt_done(ccb); 2026 if (detach != 0) 2027 VTSCSI_UNLOCK(sc); 2028 } 2029 2030 vtscsi_enqueue_request(sc, req); 2031 } 2032 2033 static void 2034 vtscsi_drain_vq(struct vtscsi_softc *sc, struct virtqueue *vq) 2035 { 2036 struct vtscsi_request *req; 2037 int last; 2038 2039 last = 0; 2040 2041 vtscsi_dprintf(sc, VTSCSI_TRACE, "vq=%p\n", vq); 2042 2043 while ((req = virtqueue_drain(vq, &last)) != NULL) 2044 vtscsi_cancel_request(sc, req); 2045 2046 KASSERT(virtqueue_empty(vq), ("virtqueue not empty")); 2047 } 2048 2049 static void 2050 vtscsi_drain_vqs(struct vtscsi_softc *sc) 2051 { 2052 2053 if (sc->vtscsi_control_vq != NULL) 2054 vtscsi_drain_vq(sc, sc->vtscsi_control_vq); 2055 if (sc->vtscsi_request_vq != NULL) 2056 vtscsi_drain_vq(sc, sc->vtscsi_request_vq); 2057 if (sc->vtscsi_event_vq != NULL) 2058 vtscsi_drain_event_vq(sc); 2059 } 2060 2061 static void 2062 vtscsi_stop(struct vtscsi_softc *sc) 2063 { 2064 2065 vtscsi_disable_vqs_intr(sc); 2066 virtio_stop(sc->vtscsi_dev); 2067 } 2068 2069 static int 2070 vtscsi_reset_bus(struct vtscsi_softc *sc) 2071 { 2072 int error; 2073 2074 VTSCSI_LOCK_OWNED(sc); 2075 2076 if (vtscsi_bus_reset_disable != 0) { 2077 device_printf(sc->vtscsi_dev, "bus reset disabled\n"); 2078 return (0); 2079 } 2080 2081 sc->vtscsi_flags |= VTSCSI_FLAG_RESET; 2082 2083 /* 2084 * vtscsi_stop() will cause the in-flight requests to be canceled. 2085 * Those requests are then completed here so CAM will retry them 2086 * after the reset is complete. 2087 */ 2088 vtscsi_stop(sc); 2089 vtscsi_complete_vqs_locked(sc); 2090 2091 /* Rid the virtqueues of any remaining requests. */ 2092 vtscsi_drain_vqs(sc); 2093 2094 /* 2095 * Any resource shortage that froze the SIMQ cannot persist across 2096 * a bus reset so ensure it gets thawed here. 2097 */ 2098 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST | VTSCSI_REQUEST_VQ) != 0) 2099 xpt_release_simq(sc->vtscsi_sim, 0); 2100 2101 error = vtscsi_reinit(sc); 2102 if (error) { 2103 device_printf(sc->vtscsi_dev, 2104 "reinitialization failed, stopping device...\n"); 2105 vtscsi_stop(sc); 2106 } else 2107 vtscsi_announce(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD, 2108 CAM_LUN_WILDCARD); 2109 2110 sc->vtscsi_flags &= ~VTSCSI_FLAG_RESET; 2111 2112 return (error); 2113 } 2114 2115 static void 2116 vtscsi_init_request(struct vtscsi_softc *sc, struct vtscsi_request *req) 2117 { 2118 2119 #ifdef INVARIANTS 2120 int req_nsegs, resp_nsegs; 2121 2122 req_nsegs = sglist_count(&req->vsr_ureq, sizeof(req->vsr_ureq)); 2123 resp_nsegs = sglist_count(&req->vsr_uresp, sizeof(req->vsr_uresp)); 2124 2125 KASSERT(req_nsegs == 1, ("request crossed page boundary")); 2126 KASSERT(resp_nsegs == 1, ("response crossed page boundary")); 2127 #endif 2128 2129 req->vsr_softc = sc; 2130 callout_init_lk(&req->vsr_callout, VTSCSI_MTX(sc)); 2131 } 2132 2133 static int 2134 vtscsi_alloc_requests(struct vtscsi_softc *sc) 2135 { 2136 struct vtscsi_request *req; 2137 int i, nreqs; 2138 2139 /* 2140 * Commands destined for either the request or control queues come 2141 * from the same SIM queue. Use the size of the request virtqueue 2142 * as it (should) be much more frequently used. Some additional 2143 * requests are allocated for internal (TMF) use. 2144 */ 2145 nreqs = virtqueue_size(sc->vtscsi_request_vq); 2146 if ((sc->vtscsi_flags & VTSCSI_FLAG_INDIRECT) == 0) 2147 nreqs /= VTSCSI_MIN_SEGMENTS; 2148 nreqs += VTSCSI_RESERVED_REQUESTS; 2149 2150 for (i = 0; i < nreqs; i++) { 2151 req = contigmalloc(sizeof(struct vtscsi_request), M_DEVBUF, 2152 M_WAITOK, 0, BUS_SPACE_MAXADDR, 16, 0); 2153 if (req == NULL) 2154 return (ENOMEM); 2155 2156 vtscsi_init_request(sc, req); 2157 2158 sc->vtscsi_nrequests++; 2159 vtscsi_enqueue_request(sc, req); 2160 } 2161 2162 return (0); 2163 } 2164 2165 static void 2166 vtscsi_free_requests(struct vtscsi_softc *sc) 2167 { 2168 struct vtscsi_request *req; 2169 2170 while ((req = vtscsi_dequeue_request(sc)) != NULL) { 2171 KASSERT(callout_active(&req->vsr_callout) == 0, 2172 ("request callout still active")); 2173 2174 sc->vtscsi_nrequests--; 2175 contigfree(req, sizeof(struct vtscsi_request), M_DEVBUF); 2176 } 2177 2178 KASSERT(sc->vtscsi_nrequests == 0, ("leaked requests: %d", 2179 sc->vtscsi_nrequests)); 2180 } 2181 2182 static void 2183 vtscsi_enqueue_request(struct vtscsi_softc *sc, struct vtscsi_request *req) 2184 { 2185 2186 KASSERT(req->vsr_softc == sc, 2187 ("non-matching request vsr_softc %p/%p", req->vsr_softc, sc)); 2188 2189 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p\n", req); 2190 2191 /* A request is available so the SIMQ could be released. */ 2192 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST) != 0) 2193 xpt_release_simq(sc->vtscsi_sim, 1); 2194 2195 req->vsr_ccb = NULL; 2196 req->vsr_complete = NULL; 2197 req->vsr_ptr0 = NULL; 2198 req->vsr_state = VTSCSI_REQ_STATE_FREE; 2199 req->vsr_flags = 0; 2200 2201 bzero(&req->vsr_ureq, sizeof(req->vsr_ureq)); 2202 bzero(&req->vsr_uresp, sizeof(req->vsr_uresp)); 2203 2204 /* 2205 * We insert at the tail of the queue in order to make it 2206 * very unlikely a request will be reused if we race with 2207 * stopping its callout handler. 2208 */ 2209 TAILQ_INSERT_TAIL(&sc->vtscsi_req_free, req, vsr_link); 2210 } 2211 2212 static struct vtscsi_request * 2213 vtscsi_dequeue_request(struct vtscsi_softc *sc) 2214 { 2215 struct vtscsi_request *req; 2216 2217 req = TAILQ_FIRST(&sc->vtscsi_req_free); 2218 if (req != NULL) { 2219 req->vsr_state = VTSCSI_REQ_STATE_INUSE; 2220 TAILQ_REMOVE(&sc->vtscsi_req_free, req, vsr_link); 2221 } else 2222 sc->vtscsi_stats.dequeue_no_requests++; 2223 2224 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p\n", req); 2225 2226 return (req); 2227 } 2228 2229 static void 2230 vtscsi_complete_request(struct vtscsi_request *req) 2231 { 2232 2233 if (req->vsr_flags & VTSCSI_REQ_FLAG_POLLED) 2234 req->vsr_flags |= VTSCSI_REQ_FLAG_COMPLETE; 2235 2236 if (req->vsr_complete != NULL) 2237 req->vsr_complete(req->vsr_softc, req); 2238 } 2239 2240 static void 2241 vtscsi_complete_vq(struct vtscsi_softc *sc, struct virtqueue *vq) 2242 { 2243 struct vtscsi_request *req; 2244 2245 VTSCSI_LOCK_OWNED(sc); 2246 2247 while ((req = virtqueue_dequeue(vq, NULL)) != NULL) 2248 vtscsi_complete_request(req); 2249 } 2250 2251 static void 2252 vtscsi_control_vq_intr(void *xsc) 2253 { 2254 struct vtscsi_softc *sc; 2255 struct virtqueue *vq; 2256 2257 sc = xsc; 2258 vq = sc->vtscsi_control_vq; 2259 2260 again: 2261 VTSCSI_LOCK(sc); 2262 if (!virtqueue_pending(vq)) 2263 goto done; 2264 2265 vtscsi_complete_vq(sc, sc->vtscsi_control_vq); 2266 2267 if (virtqueue_enable_intr(vq) != 0) { 2268 virtqueue_disable_intr(vq); 2269 VTSCSI_UNLOCK(sc); 2270 goto again; 2271 } 2272 2273 done: 2274 VTSCSI_UNLOCK(sc); 2275 } 2276 2277 static void 2278 vtscsi_event_vq_intr(void *xsc) 2279 { 2280 struct vtscsi_softc *sc; 2281 struct virtqueue *vq; 2282 struct virtio_scsi_event *event; 2283 2284 sc = xsc; 2285 vq = sc->vtscsi_event_vq; 2286 2287 again: 2288 VTSCSI_LOCK(sc); 2289 if (!virtqueue_pending(vq)) 2290 goto done; 2291 2292 while ((event = virtqueue_dequeue(vq, NULL)) != NULL) 2293 vtscsi_handle_event(sc, event); 2294 2295 if (virtqueue_enable_intr(vq) != 0) { 2296 virtqueue_disable_intr(vq); 2297 VTSCSI_UNLOCK(sc); 2298 goto again; 2299 } 2300 2301 done: 2302 VTSCSI_UNLOCK(sc); 2303 } 2304 2305 static void 2306 vtscsi_request_vq_intr(void *xsc) 2307 { 2308 struct vtscsi_softc *sc; 2309 struct virtqueue *vq; 2310 2311 sc = xsc; 2312 vq = sc->vtscsi_request_vq; 2313 2314 again: 2315 VTSCSI_LOCK(sc); 2316 if (!virtqueue_pending(vq)) 2317 goto done; 2318 2319 vtscsi_complete_vq(sc, sc->vtscsi_request_vq); 2320 2321 if (virtqueue_enable_intr(vq) != 0) { 2322 virtqueue_disable_intr(vq); 2323 VTSCSI_UNLOCK(sc); 2324 goto again; 2325 } 2326 2327 done: 2328 VTSCSI_UNLOCK(sc); 2329 } 2330 2331 static void 2332 vtscsi_disable_vqs_intr(struct vtscsi_softc *sc) 2333 { 2334 2335 virtqueue_disable_intr(sc->vtscsi_control_vq); 2336 virtqueue_disable_intr(sc->vtscsi_event_vq); 2337 virtqueue_disable_intr(sc->vtscsi_request_vq); 2338 } 2339 2340 static void 2341 vtscsi_enable_vqs_intr(struct vtscsi_softc *sc) 2342 { 2343 2344 virtqueue_enable_intr(sc->vtscsi_control_vq); 2345 virtqueue_enable_intr(sc->vtscsi_event_vq); 2346 virtqueue_enable_intr(sc->vtscsi_request_vq); 2347 } 2348 2349 static void 2350 vtscsi_get_tunables(struct vtscsi_softc *sc) 2351 { 2352 char tmpstr[64]; 2353 2354 TUNABLE_INT_FETCH("hw.vtscsi.debug_level", &sc->vtscsi_debug); 2355 2356 ksnprintf(tmpstr, sizeof(tmpstr), "dev.vtscsi.%d.debug_level", 2357 device_get_unit(sc->vtscsi_dev)); 2358 TUNABLE_INT_FETCH(tmpstr, &sc->vtscsi_debug); 2359 } 2360 2361 static void 2362 vtscsi_add_sysctl(struct vtscsi_softc *sc) 2363 { 2364 device_t dev; 2365 struct vtscsi_statistics *stats; 2366 struct sysctl_ctx_list *ctx; 2367 struct sysctl_oid *tree; 2368 struct sysctl_oid_list *child; 2369 2370 dev = sc->vtscsi_dev; 2371 stats = &sc->vtscsi_stats; 2372 ctx = device_get_sysctl_ctx(dev); 2373 tree = device_get_sysctl_tree(dev); 2374 child = SYSCTL_CHILDREN(tree); 2375 2376 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug_level", 2377 CTLFLAG_RW, &sc->vtscsi_debug, 0, 2378 "Debug level"); 2379 2380 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "scsi_cmd_timeouts", 2381 CTLFLAG_RD, &stats->scsi_cmd_timeouts, 2382 "SCSI command timeouts"); 2383 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dequeue_no_requests", 2384 CTLFLAG_RD, &stats->dequeue_no_requests, 2385 "No available requests to dequeue"); 2386 } 2387 2388 static void 2389 vtscsi_printf_req(struct vtscsi_request *req, const char *func, 2390 const char *fmt, ...) 2391 { 2392 struct vtscsi_softc *sc; 2393 union ccb *ccb; 2394 struct sbuf sb; 2395 __va_list ap; 2396 char str[192]; 2397 char path_str[64]; 2398 2399 if (req == NULL) 2400 return; 2401 2402 sc = req->vsr_softc; 2403 ccb = req->vsr_ccb; 2404 2405 __va_start(ap, fmt); 2406 sbuf_new(&sb, str, sizeof(str), 0); 2407 2408 if (ccb == NULL) { 2409 sbuf_printf(&sb, "(noperiph:%s%d:%u): ", 2410 cam_sim_name(sc->vtscsi_sim), cam_sim_unit(sc->vtscsi_sim), 2411 cam_sim_bus(sc->vtscsi_sim)); 2412 } else { 2413 xpt_path_string(ccb->ccb_h.path, path_str, sizeof(path_str)); 2414 sbuf_cat(&sb, path_str); 2415 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 2416 scsi_command_string(&ccb->csio, &sb); 2417 sbuf_printf(&sb, "length %d ", ccb->csio.dxfer_len); 2418 } 2419 } 2420 2421 sbuf_vprintf(&sb, fmt, ap); 2422 __va_end(ap); 2423 2424 sbuf_finish(&sb); 2425 kprintf("%s: %s: %s", device_get_nameunit(sc->vtscsi_dev), func, 2426 sbuf_data(&sb)); 2427 } 2428