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 u_int32_t status; 722 723 csa = &xpt_alloc_ccb()->csa; 724 725 xpt_setup_ccb(&csa->ccb_h, sc->vtscsi_path, 5); 726 csa->ccb_h.func_code = XPT_SASYNC_CB; 727 csa->event_enable = AC_LOST_DEVICE | AC_FOUND_DEVICE; 728 csa->callback = vtscsi_cam_async; 729 csa->callback_arg = sc->vtscsi_sim; 730 731 xpt_action((union ccb *)csa); 732 status = csa->ccb_h.status; 733 xpt_free_ccb(&csa->ccb_h); 734 735 return status; 736 } 737 738 static void 739 vtscsi_deregister_async(struct vtscsi_softc *sc) 740 { 741 struct ccb_setasync *csa; 742 743 csa = &xpt_alloc_ccb()->csa; 744 xpt_setup_ccb(&csa->ccb_h, sc->vtscsi_path, 5); 745 csa->ccb_h.func_code = XPT_SASYNC_CB; 746 csa->event_enable = 0; 747 csa->callback = vtscsi_cam_async; 748 csa->callback_arg = sc->vtscsi_sim; 749 xpt_action((union ccb *)csa); 750 xpt_free_ccb(&csa->ccb_h); 751 } 752 753 static void 754 vtscsi_cam_action(struct cam_sim *sim, union ccb *ccb) 755 { 756 struct vtscsi_softc *sc; 757 struct ccb_hdr *ccbh; 758 759 sc = cam_sim_softc(sim); 760 ccbh = &ccb->ccb_h; 761 762 VTSCSI_LOCK_OWNED(sc); 763 764 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) { 765 /* 766 * The VTSCSI_MTX is briefly dropped between setting 767 * VTSCSI_FLAG_DETACH and deregistering with CAM, so 768 * drop any CCBs that come in during that window. 769 */ 770 ccbh->status = CAM_NO_HBA; 771 xpt_done(ccb); 772 return; 773 } 774 775 switch (ccbh->func_code) { 776 case XPT_SCSI_IO: 777 vtscsi_cam_scsi_io(sc, sim, ccb); 778 break; 779 780 case XPT_SET_TRAN_SETTINGS: 781 ccbh->status = CAM_FUNC_NOTAVAIL; 782 xpt_done(ccb); 783 break; 784 785 case XPT_GET_TRAN_SETTINGS: 786 vtscsi_cam_get_tran_settings(sc, ccb); 787 break; 788 789 case XPT_RESET_BUS: 790 vtscsi_cam_reset_bus(sc, ccb); 791 break; 792 793 case XPT_RESET_DEV: 794 vtscsi_cam_reset_dev(sc, ccb); 795 break; 796 797 case XPT_ABORT: 798 vtscsi_cam_abort(sc, ccb); 799 break; 800 801 case XPT_CALC_GEOMETRY: 802 cam_calc_geometry(&ccb->ccg, 1); 803 xpt_done(ccb); 804 break; 805 806 case XPT_PATH_INQ: 807 vtscsi_cam_path_inquiry(sc, sim, ccb); 808 break; 809 810 default: 811 vtscsi_dprintf(sc, VTSCSI_ERROR, 812 "invalid ccb=%p func=%#x\n", ccb, ccbh->func_code); 813 814 ccbh->status = CAM_REQ_INVALID; 815 xpt_done(ccb); 816 break; 817 } 818 } 819 820 static void 821 vtscsi_cam_poll(struct cam_sim *sim) 822 { 823 struct vtscsi_softc *sc; 824 825 sc = cam_sim_softc(sim); 826 827 vtscsi_complete_vqs_locked(sc); 828 } 829 830 static void 831 vtscsi_cam_scsi_io(struct vtscsi_softc *sc, struct cam_sim *sim, 832 union ccb *ccb) 833 { 834 struct ccb_hdr *ccbh; 835 struct ccb_scsiio *csio; 836 int error; 837 838 ccbh = &ccb->ccb_h; 839 csio = &ccb->csio; 840 841 if (csio->cdb_len > VIRTIO_SCSI_CDB_SIZE) { 842 error = EINVAL; 843 ccbh->status = CAM_REQ_INVALID; 844 goto done; 845 } 846 847 #ifndef __DragonFly__ /* XXX swildner */ 848 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_BOTH && 849 (sc->vtscsi_flags & VTSCSI_FLAG_BIDIRECTIONAL) == 0) { 850 error = EINVAL; 851 ccbh->status = CAM_REQ_INVALID; 852 goto done; 853 } 854 #endif 855 856 error = vtscsi_start_scsi_cmd(sc, ccb); 857 858 done: 859 if (error) { 860 vtscsi_dprintf(sc, VTSCSI_ERROR, 861 "error=%d ccb=%p status=%#x\n", error, ccb, ccbh->status); 862 xpt_done(ccb); 863 } 864 } 865 866 static void 867 vtscsi_cam_get_tran_settings(struct vtscsi_softc *sc, union ccb *ccb) 868 { 869 struct ccb_trans_settings *cts; 870 struct ccb_trans_settings_scsi *scsi; 871 872 cts = &ccb->cts; 873 scsi = &cts->proto_specific.scsi; 874 875 cts->protocol = PROTO_SCSI; 876 cts->protocol_version = SCSI_REV_SPC3; 877 cts->transport = XPORT_SAS; 878 cts->transport_version = 0; 879 880 scsi->valid = CTS_SCSI_VALID_TQ; 881 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 882 883 ccb->ccb_h.status = CAM_REQ_CMP; 884 xpt_done(ccb); 885 } 886 887 static void 888 vtscsi_cam_reset_bus(struct vtscsi_softc *sc, union ccb *ccb) 889 { 890 int error; 891 892 error = vtscsi_reset_bus(sc); 893 if (error == 0) 894 ccb->ccb_h.status = CAM_REQ_CMP; 895 else 896 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 897 898 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d ccb=%p status=%#x\n", 899 error, ccb, ccb->ccb_h.status); 900 901 xpt_done(ccb); 902 } 903 904 static void 905 vtscsi_cam_reset_dev(struct vtscsi_softc *sc, union ccb *ccb) 906 { 907 struct ccb_hdr *ccbh; 908 struct vtscsi_request *req; 909 int error; 910 911 ccbh = &ccb->ccb_h; 912 913 req = vtscsi_dequeue_request(sc); 914 if (req == NULL) { 915 error = EAGAIN; 916 vtscsi_freeze_simq(sc, VTSCSI_REQUEST); 917 goto fail; 918 } 919 920 req->vsr_ccb = ccb; 921 922 error = vtscsi_execute_reset_dev_cmd(sc, req); 923 if (error == 0) 924 return; 925 926 vtscsi_enqueue_request(sc, req); 927 928 fail: 929 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p\n", 930 error, req, ccb); 931 932 if (error == EAGAIN) 933 ccbh->status = CAM_RESRC_UNAVAIL; 934 else 935 ccbh->status = CAM_REQ_CMP_ERR; 936 937 xpt_done(ccb); 938 } 939 940 static void 941 vtscsi_cam_abort(struct vtscsi_softc *sc, union ccb *ccb) 942 { 943 struct vtscsi_request *req; 944 struct ccb_hdr *ccbh; 945 int error; 946 947 ccbh = &ccb->ccb_h; 948 949 req = vtscsi_dequeue_request(sc); 950 if (req == NULL) { 951 error = EAGAIN; 952 vtscsi_freeze_simq(sc, VTSCSI_REQUEST); 953 goto fail; 954 } 955 956 req->vsr_ccb = ccb; 957 958 error = vtscsi_execute_abort_task_cmd(sc, req); 959 if (error == 0) 960 return; 961 962 vtscsi_enqueue_request(sc, req); 963 964 fail: 965 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p\n", 966 error, req, ccb); 967 968 if (error == EAGAIN) 969 ccbh->status = CAM_RESRC_UNAVAIL; 970 else 971 ccbh->status = CAM_REQ_CMP_ERR; 972 973 xpt_done(ccb); 974 } 975 976 static void 977 vtscsi_cam_path_inquiry(struct vtscsi_softc *sc, struct cam_sim *sim, 978 union ccb *ccb) 979 { 980 device_t dev; 981 struct ccb_pathinq *cpi; 982 983 dev = sc->vtscsi_dev; 984 cpi = &ccb->cpi; 985 986 vtscsi_dprintf(sc, VTSCSI_TRACE, "sim=%p ccb=%p\n", sim, ccb); 987 988 cpi->version_num = 1; 989 cpi->hba_inquiry = PI_TAG_ABLE; 990 cpi->target_sprt = 0; 991 cpi->hba_misc = PIM_SEQSCAN; 992 if (vtscsi_bus_reset_disable != 0) 993 cpi->hba_misc |= PIM_NOBUSRESET; 994 cpi->hba_eng_cnt = 0; 995 996 cpi->max_target = sc->vtscsi_max_target; 997 cpi->max_lun = sc->vtscsi_max_lun; 998 cpi->initiator_id = VTSCSI_INITIATOR_ID; 999 1000 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1001 strlcpy(cpi->hba_vid, "VirtIO", HBA_IDLEN); 1002 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1003 1004 cpi->unit_number = cam_sim_unit(sim); 1005 cpi->bus_id = cam_sim_bus(sim); 1006 1007 cpi->base_transfer_speed = 300000; 1008 1009 cpi->protocol = PROTO_SCSI; 1010 cpi->protocol_version = SCSI_REV_SPC3; 1011 cpi->transport = XPORT_SAS; 1012 cpi->transport_version = 0; 1013 1014 cpi->maxio = (sc->vtscsi_max_nsegs - VTSCSI_MIN_SEGMENTS - 1) * 1015 PAGE_SIZE; 1016 1017 #if 0 1018 cpi->hba_vendor = virtio_get_vendor(dev); 1019 cpi->hba_device = virtio_get_device(dev); 1020 cpi->hba_subvendor = virtio_get_subvendor(dev); 1021 cpi->hba_subdevice = virtio_get_subdevice(dev); 1022 #endif 1023 1024 ccb->ccb_h.status = CAM_REQ_CMP; 1025 xpt_done(ccb); 1026 } 1027 1028 static int 1029 vtscsi_sg_append_scsi_buf(struct vtscsi_softc *sc, struct sglist *sg, 1030 struct ccb_scsiio *csio) 1031 { 1032 struct ccb_hdr *ccbh; 1033 struct bus_dma_segment *dseg; 1034 int i, error; 1035 1036 ccbh = &csio->ccb_h; 1037 error = 0; 1038 1039 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) { 1040 1041 if ((ccbh->flags & CAM_DATA_PHYS) == 0) 1042 error = sglist_append(sg, 1043 csio->data_ptr, csio->dxfer_len); 1044 else 1045 error = sglist_append_phys(sg, 1046 (vm_paddr_t)(vm_offset_t) csio->data_ptr, 1047 csio->dxfer_len); 1048 } else { 1049 1050 for (i = 0; i < csio->sglist_cnt && error == 0; i++) { 1051 dseg = &((struct bus_dma_segment *)csio->data_ptr)[i]; 1052 1053 if ((ccbh->flags & CAM_SG_LIST_PHYS) == 0) 1054 error = sglist_append(sg, 1055 (void *)(vm_offset_t) dseg->ds_addr, 1056 dseg->ds_len); 1057 else 1058 error = sglist_append_phys(sg, 1059 (vm_paddr_t) dseg->ds_addr, dseg->ds_len); 1060 } 1061 } 1062 1063 return (error); 1064 } 1065 1066 static int 1067 vtscsi_fill_scsi_cmd_sglist(struct vtscsi_softc *sc, struct vtscsi_request *req, 1068 int *readable, int *writable) 1069 { 1070 struct sglist *sg; 1071 struct ccb_hdr *ccbh; 1072 struct ccb_scsiio *csio; 1073 struct virtio_scsi_cmd_req *cmd_req; 1074 struct virtio_scsi_cmd_resp *cmd_resp; 1075 int error; 1076 1077 sg = sc->vtscsi_sglist; 1078 csio = &req->vsr_ccb->csio; 1079 ccbh = &csio->ccb_h; 1080 cmd_req = &req->vsr_cmd_req; 1081 cmd_resp = &req->vsr_cmd_resp; 1082 1083 sglist_reset(sg); 1084 1085 sglist_append(sg, cmd_req, sizeof(struct virtio_scsi_cmd_req)); 1086 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 1087 error = vtscsi_sg_append_scsi_buf(sc, sg, csio); 1088 /* At least one segment must be left for the response. */ 1089 if (error || sg->sg_nseg == sg->sg_maxseg) 1090 goto fail; 1091 } 1092 1093 *readable = sg->sg_nseg; 1094 1095 sglist_append(sg, cmd_resp, sizeof(struct virtio_scsi_cmd_resp)); 1096 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1097 error = vtscsi_sg_append_scsi_buf(sc, sg, csio); 1098 if (error) 1099 goto fail; 1100 } 1101 1102 *writable = sg->sg_nseg - *readable; 1103 1104 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p readable=%d " 1105 "writable=%d\n", req, ccbh, *readable, *writable); 1106 1107 return (0); 1108 1109 fail: 1110 /* 1111 * This should never happen unless maxio was incorrectly set. 1112 */ 1113 vtscsi_set_ccb_status(ccbh, CAM_REQ_TOO_BIG, 0); 1114 1115 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p " 1116 "nseg=%d maxseg=%d\n", 1117 error, req, ccbh, sg->sg_nseg, sg->sg_maxseg); 1118 1119 return (EFBIG); 1120 } 1121 1122 static int 1123 vtscsi_execute_scsi_cmd(struct vtscsi_softc *sc, struct vtscsi_request *req) 1124 { 1125 struct sglist *sg; 1126 struct virtqueue *vq; 1127 struct ccb_scsiio *csio; 1128 struct ccb_hdr *ccbh; 1129 struct virtio_scsi_cmd_req *cmd_req; 1130 struct virtio_scsi_cmd_resp *cmd_resp; 1131 int readable, writable, error; 1132 1133 sg = sc->vtscsi_sglist; 1134 vq = sc->vtscsi_request_vq; 1135 csio = &req->vsr_ccb->csio; 1136 ccbh = &csio->ccb_h; 1137 cmd_req = &req->vsr_cmd_req; 1138 cmd_resp = &req->vsr_cmd_resp; 1139 1140 vtscsi_init_scsi_cmd_req(csio, cmd_req); 1141 1142 error = vtscsi_fill_scsi_cmd_sglist(sc, req, &readable, &writable); 1143 if (error) 1144 return (error); 1145 1146 req->vsr_complete = vtscsi_complete_scsi_cmd; 1147 cmd_resp->response = -1; 1148 1149 error = virtqueue_enqueue(vq, req, sg, readable, writable); 1150 if (error) { 1151 vtscsi_dprintf(sc, VTSCSI_ERROR, 1152 "enqueue error=%d req=%p ccb=%p\n", error, req, ccbh); 1153 1154 ccbh->status = CAM_REQUEUE_REQ; 1155 vtscsi_freeze_simq(sc, VTSCSI_REQUEST_VQ); 1156 return (error); 1157 } 1158 1159 ccbh->status |= CAM_SIM_QUEUED; 1160 ccbh->ccbh_vtscsi_req = req; 1161 1162 virtqueue_notify(vq, NULL); 1163 1164 if (ccbh->timeout != CAM_TIME_INFINITY) { 1165 req->vsr_flags |= VTSCSI_REQ_FLAG_TIMEOUT_SET; 1166 callout_reset(&req->vsr_callout, ccbh->timeout * hz / 1000, 1167 vtscsi_timedout_scsi_cmd, req); 1168 } 1169 1170 vtscsi_dprintf_req(req, VTSCSI_TRACE, "enqueued req=%p ccb=%p\n", 1171 req, ccbh); 1172 1173 return (0); 1174 } 1175 1176 static int 1177 vtscsi_start_scsi_cmd(struct vtscsi_softc *sc, union ccb *ccb) 1178 { 1179 struct vtscsi_request *req; 1180 int error; 1181 1182 req = vtscsi_dequeue_request(sc); 1183 if (req == NULL) { 1184 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1185 vtscsi_freeze_simq(sc, VTSCSI_REQUEST); 1186 return (ENOBUFS); 1187 } 1188 1189 req->vsr_ccb = ccb; 1190 1191 error = vtscsi_execute_scsi_cmd(sc, req); 1192 if (error) 1193 vtscsi_enqueue_request(sc, req); 1194 1195 return (error); 1196 } 1197 1198 static void 1199 vtscsi_complete_abort_timedout_scsi_cmd(struct vtscsi_softc *sc, 1200 struct vtscsi_request *req) 1201 { 1202 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1203 struct vtscsi_request *to_req; 1204 uint8_t response; 1205 1206 tmf_resp = &req->vsr_tmf_resp; 1207 response = tmf_resp->response; 1208 to_req = req->vsr_timedout_req; 1209 1210 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p to_req=%p response=%d\n", 1211 req, to_req, response); 1212 1213 vtscsi_enqueue_request(sc, req); 1214 1215 /* 1216 * The timedout request could have completed between when the 1217 * abort task was sent and when the host processed it. 1218 */ 1219 if (to_req->vsr_state != VTSCSI_REQ_STATE_TIMEDOUT) 1220 return; 1221 1222 /* The timedout request was successfully aborted. */ 1223 if (response == VIRTIO_SCSI_S_FUNCTION_COMPLETE) 1224 return; 1225 1226 /* Don't bother if the device is going away. */ 1227 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) 1228 return; 1229 1230 /* The timedout request will be aborted by the reset. */ 1231 if (sc->vtscsi_flags & VTSCSI_FLAG_RESET) 1232 return; 1233 1234 vtscsi_reset_bus(sc); 1235 } 1236 1237 static int 1238 vtscsi_abort_timedout_scsi_cmd(struct vtscsi_softc *sc, 1239 struct vtscsi_request *to_req) 1240 { 1241 struct sglist *sg; 1242 struct ccb_hdr *to_ccbh; 1243 struct vtscsi_request *req; 1244 struct virtio_scsi_ctrl_tmf_req *tmf_req; 1245 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1246 int error; 1247 1248 sg = sc->vtscsi_sglist; 1249 to_ccbh = &to_req->vsr_ccb->ccb_h; 1250 1251 req = vtscsi_dequeue_request(sc); 1252 if (req == NULL) { 1253 error = ENOBUFS; 1254 goto fail; 1255 } 1256 1257 tmf_req = &req->vsr_tmf_req; 1258 tmf_resp = &req->vsr_tmf_resp; 1259 1260 vtscsi_init_ctrl_tmf_req(to_ccbh, VIRTIO_SCSI_T_TMF_ABORT_TASK, 1261 (uintptr_t) to_ccbh, tmf_req); 1262 1263 sglist_reset(sg); 1264 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req)); 1265 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp)); 1266 1267 req->vsr_timedout_req = to_req; 1268 req->vsr_complete = vtscsi_complete_abort_timedout_scsi_cmd; 1269 tmf_resp->response = -1; 1270 1271 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1, 1272 VTSCSI_EXECUTE_ASYNC); 1273 if (error == 0) 1274 return (0); 1275 1276 vtscsi_enqueue_request(sc, req); 1277 1278 fail: 1279 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p " 1280 "timedout req=%p ccb=%p\n", error, req, to_req, to_ccbh); 1281 1282 return (error); 1283 } 1284 1285 static void 1286 vtscsi_timedout_scsi_cmd(void *xreq) 1287 { 1288 struct vtscsi_softc *sc; 1289 struct vtscsi_request *to_req; 1290 1291 to_req = xreq; 1292 sc = to_req->vsr_softc; 1293 1294 vtscsi_dprintf(sc, VTSCSI_INFO, "timedout req=%p ccb=%p state=%#x\n", 1295 to_req, to_req->vsr_ccb, to_req->vsr_state); 1296 1297 /* Don't bother if the device is going away. */ 1298 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) 1299 return; 1300 1301 /* 1302 * Bail if the request is not in use. We likely raced when 1303 * stopping the callout handler or it has already been aborted. 1304 */ 1305 if (to_req->vsr_state != VTSCSI_REQ_STATE_INUSE || 1306 (to_req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) == 0) 1307 return; 1308 1309 /* 1310 * Complete the request queue in case the timedout request is 1311 * actually just pending. 1312 */ 1313 vtscsi_complete_vq(sc, sc->vtscsi_request_vq); 1314 if (to_req->vsr_state == VTSCSI_REQ_STATE_FREE) 1315 return; 1316 1317 sc->vtscsi_stats.scsi_cmd_timeouts++; 1318 to_req->vsr_state = VTSCSI_REQ_STATE_TIMEDOUT; 1319 1320 if (vtscsi_abort_timedout_scsi_cmd(sc, to_req) == 0) 1321 return; 1322 1323 vtscsi_dprintf(sc, VTSCSI_ERROR, "resetting bus\n"); 1324 vtscsi_reset_bus(sc); 1325 } 1326 1327 static cam_status 1328 vtscsi_scsi_cmd_cam_status(struct virtio_scsi_cmd_resp *cmd_resp) 1329 { 1330 cam_status status; 1331 1332 switch (cmd_resp->response) { 1333 case VIRTIO_SCSI_S_OK: 1334 status = CAM_REQ_CMP; 1335 break; 1336 case VIRTIO_SCSI_S_OVERRUN: 1337 status = CAM_DATA_RUN_ERR; 1338 break; 1339 case VIRTIO_SCSI_S_ABORTED: 1340 status = CAM_REQ_ABORTED; 1341 break; 1342 case VIRTIO_SCSI_S_BAD_TARGET: 1343 status = CAM_SEL_TIMEOUT; 1344 break; 1345 case VIRTIO_SCSI_S_RESET: 1346 status = CAM_SCSI_BUS_RESET; 1347 break; 1348 case VIRTIO_SCSI_S_BUSY: 1349 status = CAM_SCSI_BUSY; 1350 break; 1351 case VIRTIO_SCSI_S_TRANSPORT_FAILURE: 1352 case VIRTIO_SCSI_S_TARGET_FAILURE: 1353 case VIRTIO_SCSI_S_NEXUS_FAILURE: 1354 status = CAM_SCSI_IT_NEXUS_LOST; 1355 break; 1356 default: /* VIRTIO_SCSI_S_FAILURE */ 1357 status = CAM_REQ_CMP_ERR; 1358 break; 1359 } 1360 1361 return (status); 1362 } 1363 1364 static cam_status 1365 vtscsi_complete_scsi_cmd_response(struct vtscsi_softc *sc, 1366 struct ccb_scsiio *csio, struct virtio_scsi_cmd_resp *cmd_resp) 1367 { 1368 cam_status status; 1369 1370 csio->scsi_status = cmd_resp->status; 1371 csio->resid = cmd_resp->resid; 1372 1373 if (csio->scsi_status == SCSI_STATUS_OK) 1374 status = CAM_REQ_CMP; 1375 else 1376 status = CAM_SCSI_STATUS_ERROR; 1377 1378 if (cmd_resp->sense_len > 0) { 1379 status |= CAM_AUTOSNS_VALID; 1380 1381 if (cmd_resp->sense_len < csio->sense_len) 1382 csio->sense_resid = csio->sense_len - 1383 cmd_resp->sense_len; 1384 else 1385 csio->sense_resid = 0; 1386 1387 bzero(&csio->sense_data, sizeof(csio->sense_data)); 1388 memcpy(cmd_resp->sense, &csio->sense_data, 1389 csio->sense_len - csio->sense_resid); 1390 } 1391 1392 vtscsi_dprintf(sc, status == CAM_REQ_CMP ? VTSCSI_TRACE : VTSCSI_ERROR, 1393 "ccb=%p scsi_status=%#x resid=%u sense_resid=%u\n", 1394 csio, csio->scsi_status, csio->resid, csio->sense_resid); 1395 1396 return (status); 1397 } 1398 1399 static void 1400 vtscsi_complete_scsi_cmd(struct vtscsi_softc *sc, struct vtscsi_request *req) 1401 { 1402 struct ccb_hdr *ccbh; 1403 struct ccb_scsiio *csio; 1404 struct virtio_scsi_cmd_resp *cmd_resp; 1405 cam_status status; 1406 1407 csio = &req->vsr_ccb->csio; 1408 ccbh = &csio->ccb_h; 1409 cmd_resp = &req->vsr_cmd_resp; 1410 1411 KASSERT(ccbh->ccbh_vtscsi_req == req, 1412 ("ccb %p req mismatch %p/%p", ccbh, ccbh->ccbh_vtscsi_req, req)); 1413 1414 if (req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) 1415 callout_stop(&req->vsr_callout); 1416 1417 status = vtscsi_scsi_cmd_cam_status(cmd_resp); 1418 if (status == CAM_REQ_ABORTED) { 1419 if (req->vsr_state == VTSCSI_REQ_STATE_TIMEDOUT) 1420 status = CAM_CMD_TIMEOUT; 1421 } else if (status == CAM_REQ_CMP) 1422 status = vtscsi_complete_scsi_cmd_response(sc, csio, cmd_resp); 1423 1424 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1425 status |= CAM_DEV_QFRZN; 1426 xpt_freeze_devq(ccbh->path, 1); 1427 } 1428 1429 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST | VTSCSI_REQUEST_VQ) != 0) 1430 status |= CAM_RELEASE_SIMQ; 1431 1432 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p status=%#x\n", 1433 req, ccbh, status); 1434 1435 ccbh->status = status; 1436 xpt_done(req->vsr_ccb); 1437 vtscsi_enqueue_request(sc, req); 1438 } 1439 1440 static void 1441 vtscsi_poll_ctrl_req(struct vtscsi_softc *sc, struct vtscsi_request *req) 1442 { 1443 1444 /* XXX We probably shouldn't poll forever. */ 1445 req->vsr_flags |= VTSCSI_REQ_FLAG_POLLED; 1446 do 1447 vtscsi_complete_vq(sc, sc->vtscsi_control_vq); 1448 while ((req->vsr_flags & VTSCSI_REQ_FLAG_COMPLETE) == 0); 1449 1450 req->vsr_flags &= ~VTSCSI_REQ_FLAG_POLLED; 1451 } 1452 1453 static int 1454 vtscsi_execute_ctrl_req(struct vtscsi_softc *sc, struct vtscsi_request *req, 1455 struct sglist *sg, int readable, int writable, int flag) 1456 { 1457 struct virtqueue *vq; 1458 int error; 1459 1460 vq = sc->vtscsi_control_vq; 1461 1462 KKASSERT(flag == VTSCSI_EXECUTE_POLL || req->vsr_complete != NULL); 1463 1464 error = virtqueue_enqueue(vq, req, sg, readable, writable); 1465 if (error) { 1466 /* 1467 * Return EAGAIN when the virtqueue does not have enough 1468 * descriptors available. 1469 */ 1470 if (error == ENOSPC || error == EMSGSIZE) 1471 error = EAGAIN; 1472 1473 return (error); 1474 } 1475 1476 virtqueue_notify(vq, NULL); 1477 if (flag == VTSCSI_EXECUTE_POLL) 1478 vtscsi_poll_ctrl_req(sc, req); 1479 1480 return (0); 1481 } 1482 1483 static void 1484 vtscsi_complete_abort_task_cmd(struct vtscsi_softc *sc, 1485 struct vtscsi_request *req) 1486 { 1487 union ccb *ccb; 1488 struct ccb_hdr *ccbh; 1489 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1490 1491 ccb = req->vsr_ccb; 1492 ccbh = &ccb->ccb_h; 1493 tmf_resp = &req->vsr_tmf_resp; 1494 1495 switch (tmf_resp->response) { 1496 case VIRTIO_SCSI_S_FUNCTION_COMPLETE: 1497 ccbh->status = CAM_REQ_CMP; 1498 break; 1499 case VIRTIO_SCSI_S_FUNCTION_REJECTED: 1500 ccbh->status = CAM_UA_ABORT; 1501 break; 1502 default: 1503 ccbh->status = CAM_REQ_CMP_ERR; 1504 break; 1505 } 1506 1507 xpt_done(ccb); 1508 vtscsi_enqueue_request(sc, req); 1509 } 1510 1511 static int 1512 vtscsi_execute_abort_task_cmd(struct vtscsi_softc *sc, 1513 struct vtscsi_request *req) 1514 { 1515 struct sglist *sg; 1516 struct ccb_abort *cab; 1517 struct ccb_hdr *ccbh; 1518 struct ccb_hdr *abort_ccbh; 1519 struct vtscsi_request *abort_req; 1520 struct virtio_scsi_ctrl_tmf_req *tmf_req; 1521 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1522 int error; 1523 1524 sg = sc->vtscsi_sglist; 1525 cab = &req->vsr_ccb->cab; 1526 ccbh = &cab->ccb_h; 1527 tmf_req = &req->vsr_tmf_req; 1528 tmf_resp = &req->vsr_tmf_resp; 1529 1530 /* CCB header and request that's to be aborted. */ 1531 abort_ccbh = &cab->abort_ccb->ccb_h; 1532 abort_req = abort_ccbh->ccbh_vtscsi_req; 1533 1534 if (abort_ccbh->func_code != XPT_SCSI_IO || abort_req == NULL) { 1535 error = EINVAL; 1536 goto fail; 1537 } 1538 1539 /* Only attempt to abort requests that could be in-flight. */ 1540 if (abort_req->vsr_state != VTSCSI_REQ_STATE_INUSE) { 1541 error = EALREADY; 1542 goto fail; 1543 } 1544 1545 abort_req->vsr_state = VTSCSI_REQ_STATE_ABORTED; 1546 if (abort_req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) 1547 callout_stop(&abort_req->vsr_callout); 1548 1549 vtscsi_init_ctrl_tmf_req(ccbh, VIRTIO_SCSI_T_TMF_ABORT_TASK, 1550 (uintptr_t) abort_ccbh, tmf_req); 1551 1552 sglist_reset(sg); 1553 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req)); 1554 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp)); 1555 1556 req->vsr_complete = vtscsi_complete_abort_task_cmd; 1557 tmf_resp->response = -1; 1558 1559 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1, 1560 VTSCSI_EXECUTE_ASYNC); 1561 1562 fail: 1563 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d req=%p abort_ccb=%p " 1564 "abort_req=%p\n", error, req, abort_ccbh, abort_req); 1565 1566 return (error); 1567 } 1568 1569 static void 1570 vtscsi_complete_reset_dev_cmd(struct vtscsi_softc *sc, 1571 struct vtscsi_request *req) 1572 { 1573 union ccb *ccb; 1574 struct ccb_hdr *ccbh; 1575 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1576 1577 ccb = req->vsr_ccb; 1578 ccbh = &ccb->ccb_h; 1579 tmf_resp = &req->vsr_tmf_resp; 1580 1581 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p response=%d\n", 1582 req, ccb, tmf_resp->response); 1583 1584 if (tmf_resp->response == VIRTIO_SCSI_S_FUNCTION_COMPLETE) { 1585 ccbh->status = CAM_REQ_CMP; 1586 vtscsi_announce(sc, AC_SENT_BDR, ccbh->target_id, 1587 ccbh->target_lun); 1588 } else 1589 ccbh->status = CAM_REQ_CMP_ERR; 1590 1591 xpt_done(ccb); 1592 vtscsi_enqueue_request(sc, req); 1593 } 1594 1595 static int 1596 vtscsi_execute_reset_dev_cmd(struct vtscsi_softc *sc, 1597 struct vtscsi_request *req) 1598 { 1599 struct sglist *sg; 1600 struct ccb_resetdev *crd; 1601 struct ccb_hdr *ccbh; 1602 struct virtio_scsi_ctrl_tmf_req *tmf_req; 1603 struct virtio_scsi_ctrl_tmf_resp *tmf_resp; 1604 uint32_t subtype; 1605 int error; 1606 1607 sg = sc->vtscsi_sglist; 1608 crd = &req->vsr_ccb->crd; 1609 ccbh = &crd->ccb_h; 1610 tmf_req = &req->vsr_tmf_req; 1611 tmf_resp = &req->vsr_tmf_resp; 1612 1613 if (ccbh->target_lun == CAM_LUN_WILDCARD) 1614 subtype = VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET; 1615 else 1616 subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET; 1617 1618 vtscsi_init_ctrl_tmf_req(ccbh, subtype, 0, tmf_req); 1619 1620 sglist_reset(sg); 1621 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req)); 1622 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp)); 1623 1624 req->vsr_complete = vtscsi_complete_reset_dev_cmd; 1625 tmf_resp->response = -1; 1626 1627 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1, 1628 VTSCSI_EXECUTE_ASYNC); 1629 1630 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d req=%p ccb=%p\n", 1631 error, req, ccbh); 1632 1633 return (error); 1634 } 1635 1636 static void 1637 vtscsi_get_request_lun(uint8_t lun[], target_id_t *target_id, lun_id_t *lun_id) 1638 { 1639 1640 *target_id = lun[1]; 1641 *lun_id = (lun[2] << 8) | lun[3]; 1642 } 1643 1644 static void 1645 vtscsi_set_request_lun(struct ccb_hdr *ccbh, uint8_t lun[]) 1646 { 1647 1648 lun[0] = 1; 1649 lun[1] = ccbh->target_id; 1650 lun[2] = 0x40 | ((ccbh->target_lun >> 8) & 0x3F); 1651 lun[3] = ccbh->target_lun & 0xFF; 1652 } 1653 1654 static void 1655 vtscsi_init_scsi_cmd_req(struct ccb_scsiio *csio, 1656 struct virtio_scsi_cmd_req *cmd_req) 1657 { 1658 uint8_t attr; 1659 1660 switch (csio->tag_action) { 1661 case MSG_HEAD_OF_Q_TAG: 1662 attr = VIRTIO_SCSI_S_HEAD; 1663 break; 1664 case MSG_ORDERED_Q_TAG: 1665 attr = VIRTIO_SCSI_S_ORDERED; 1666 break; 1667 case MSG_ACA_TASK: 1668 attr = VIRTIO_SCSI_S_ACA; 1669 break; 1670 default: /* MSG_SIMPLE_Q_TAG */ 1671 attr = VIRTIO_SCSI_S_SIMPLE; 1672 break; 1673 } 1674 1675 vtscsi_set_request_lun(&csio->ccb_h, cmd_req->lun); 1676 cmd_req->tag = (uintptr_t) csio; 1677 cmd_req->task_attr = attr; 1678 1679 memcpy(cmd_req->cdb, 1680 csio->ccb_h.flags & CAM_CDB_POINTER ? 1681 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes, 1682 csio->cdb_len); 1683 } 1684 1685 static void 1686 vtscsi_init_ctrl_tmf_req(struct ccb_hdr *ccbh, uint32_t subtype, 1687 uintptr_t tag, struct virtio_scsi_ctrl_tmf_req *tmf_req) 1688 { 1689 1690 vtscsi_set_request_lun(ccbh, tmf_req->lun); 1691 1692 tmf_req->type = VIRTIO_SCSI_T_TMF; 1693 tmf_req->subtype = subtype; 1694 tmf_req->tag = tag; 1695 } 1696 1697 static void 1698 vtscsi_freeze_simq(struct vtscsi_softc *sc, int reason) 1699 { 1700 int frozen; 1701 1702 frozen = sc->vtscsi_frozen; 1703 1704 if (reason & VTSCSI_REQUEST && 1705 (sc->vtscsi_frozen & VTSCSI_FROZEN_NO_REQUESTS) == 0) 1706 sc->vtscsi_frozen |= VTSCSI_FROZEN_NO_REQUESTS; 1707 1708 if (reason & VTSCSI_REQUEST_VQ && 1709 (sc->vtscsi_frozen & VTSCSI_FROZEN_REQUEST_VQ_FULL) == 0) 1710 sc->vtscsi_frozen |= VTSCSI_FROZEN_REQUEST_VQ_FULL; 1711 1712 /* Freeze the SIMQ if transitioned to frozen. */ 1713 if (frozen == 0 && sc->vtscsi_frozen != 0) { 1714 vtscsi_dprintf(sc, VTSCSI_INFO, "SIMQ frozen\n"); 1715 xpt_freeze_simq(sc->vtscsi_sim, 1); 1716 } 1717 } 1718 1719 static int 1720 vtscsi_thaw_simq(struct vtscsi_softc *sc, int reason) 1721 { 1722 int thawed; 1723 1724 if (sc->vtscsi_frozen == 0 || reason == 0) 1725 return (0); 1726 1727 if (reason & VTSCSI_REQUEST && 1728 sc->vtscsi_frozen & VTSCSI_FROZEN_NO_REQUESTS) 1729 sc->vtscsi_frozen &= ~VTSCSI_FROZEN_NO_REQUESTS; 1730 1731 if (reason & VTSCSI_REQUEST_VQ && 1732 sc->vtscsi_frozen & VTSCSI_FROZEN_REQUEST_VQ_FULL) 1733 sc->vtscsi_frozen &= ~VTSCSI_FROZEN_REQUEST_VQ_FULL; 1734 1735 thawed = sc->vtscsi_frozen == 0; 1736 if (thawed != 0) 1737 vtscsi_dprintf(sc, VTSCSI_INFO, "SIMQ thawed\n"); 1738 1739 return (thawed); 1740 } 1741 1742 static void 1743 vtscsi_announce(struct vtscsi_softc *sc, uint32_t ac_code, 1744 target_id_t target_id, lun_id_t lun_id) 1745 { 1746 struct cam_path *path; 1747 1748 /* Use the wildcard path from our softc for bus announcements. */ 1749 if (target_id == CAM_TARGET_WILDCARD && lun_id == CAM_LUN_WILDCARD) { 1750 xpt_async(ac_code, sc->vtscsi_path, NULL); 1751 return; 1752 } 1753 1754 if (xpt_create_path(&path, NULL, cam_sim_path(sc->vtscsi_sim), 1755 target_id, lun_id) != CAM_REQ_CMP) { 1756 vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot create path\n"); 1757 return; 1758 } 1759 1760 xpt_async(ac_code, path, NULL); 1761 xpt_free_path(path); 1762 } 1763 1764 static void 1765 vtscsi_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) 1766 { 1767 xpt_free_path(ccb->ccb_h.path); 1768 xpt_free_ccb(&ccb->ccb_h); 1769 } 1770 1771 static void 1772 vtscsi_execute_rescan(struct vtscsi_softc *sc, target_id_t target_id, 1773 lun_id_t lun_id) 1774 { 1775 union ccb *ccb; 1776 cam_status status; 1777 1778 ccb = xpt_alloc_ccb(); 1779 if (ccb == NULL) { 1780 vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot allocate CCB\n"); 1781 return; 1782 } 1783 1784 status = xpt_create_path(&ccb->ccb_h.path, NULL, 1785 cam_sim_path(sc->vtscsi_sim), target_id, lun_id); 1786 if (status != CAM_REQ_CMP) { 1787 xpt_free_ccb(&ccb->ccb_h); 1788 return; 1789 } 1790 1791 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/); 1792 ccb->ccb_h.func_code = XPT_SCAN_LUN; 1793 ccb->ccb_h.cbfcnp = vtscsi_cam_rescan_callback; 1794 ccb->crcn.flags = CAM_FLAG_NONE; 1795 xpt_action(ccb); 1796 } 1797 1798 static void 1799 vtscsi_execute_rescan_bus(struct vtscsi_softc *sc) 1800 { 1801 union ccb *ccb; 1802 cam_status status; 1803 1804 ccb = xpt_alloc_ccb(); 1805 if (ccb == NULL) { 1806 vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot allocate CCB\n"); 1807 return; 1808 } 1809 1810 status = xpt_create_path(&ccb->ccb_h.path, NULL, 1811 cam_sim_path(sc->vtscsi_sim), 1812 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 1813 if (status != CAM_REQ_CMP) { 1814 xpt_free_ccb(&ccb->ccb_h); 1815 return; 1816 } 1817 1818 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/); 1819 ccb->ccb_h.func_code = XPT_SCAN_BUS; 1820 ccb->ccb_h.cbfcnp = vtscsi_cam_rescan_callback; 1821 ccb->crcn.flags = CAM_FLAG_NONE; 1822 xpt_action(ccb); 1823 } 1824 1825 static void 1826 vtscsi_transport_reset_event(struct vtscsi_softc *sc, 1827 struct virtio_scsi_event *event) 1828 { 1829 target_id_t target_id; 1830 lun_id_t lun_id; 1831 1832 vtscsi_get_request_lun(event->lun, &target_id, &lun_id); 1833 1834 switch (event->reason) { 1835 case VIRTIO_SCSI_EVT_RESET_RESCAN: 1836 case VIRTIO_SCSI_EVT_RESET_REMOVED: 1837 vtscsi_execute_rescan(sc, target_id, lun_id); 1838 break; 1839 default: 1840 device_printf(sc->vtscsi_dev, 1841 "unhandled transport event reason: %d\n", event->reason); 1842 break; 1843 } 1844 } 1845 1846 static void 1847 vtscsi_handle_event(struct vtscsi_softc *sc, struct virtio_scsi_event *event) 1848 { 1849 int error; 1850 1851 if ((event->event & VIRTIO_SCSI_T_EVENTS_MISSED) == 0) { 1852 switch (event->event) { 1853 case VIRTIO_SCSI_T_TRANSPORT_RESET: 1854 vtscsi_transport_reset_event(sc, event); 1855 break; 1856 default: 1857 device_printf(sc->vtscsi_dev, 1858 "unhandled event: %d\n", event->event); 1859 break; 1860 } 1861 } else 1862 vtscsi_execute_rescan_bus(sc); 1863 1864 /* 1865 * This should always be successful since the buffer 1866 * was just dequeued. 1867 */ 1868 error = vtscsi_enqueue_event_buf(sc, event); 1869 KASSERT(error == 0, 1870 ("cannot requeue event buffer: %d", error)); 1871 } 1872 1873 static int 1874 vtscsi_enqueue_event_buf(struct vtscsi_softc *sc, 1875 struct virtio_scsi_event *event) 1876 { 1877 struct sglist *sg; 1878 struct virtqueue *vq; 1879 int size, error; 1880 1881 sg = sc->vtscsi_sglist; 1882 vq = sc->vtscsi_event_vq; 1883 size = sc->vtscsi_event_buf_size; 1884 1885 bzero(event, size); 1886 1887 sglist_reset(sg); 1888 error = sglist_append(sg, event, size); 1889 if (error) 1890 return (error); 1891 1892 error = virtqueue_enqueue(vq, event, sg, 0, sg->sg_nseg); 1893 if (error) 1894 return (error); 1895 1896 virtqueue_notify(vq, NULL); 1897 1898 return (0); 1899 } 1900 1901 static int 1902 vtscsi_init_event_vq(struct vtscsi_softc *sc) 1903 { 1904 struct virtio_scsi_event *event; 1905 int i, size, error; 1906 1907 /* 1908 * The first release of QEMU with VirtIO SCSI support would crash 1909 * when attempting to notify the event virtqueue. This was fixed 1910 * when hotplug support was added. 1911 */ 1912 if (sc->vtscsi_flags & VTSCSI_FLAG_HOTPLUG) 1913 size = sc->vtscsi_event_buf_size; 1914 else 1915 size = 0; 1916 1917 if (size < sizeof(struct virtio_scsi_event)) 1918 return (0); 1919 1920 for (i = 0; i < VTSCSI_NUM_EVENT_BUFS; i++) { 1921 event = &sc->vtscsi_event_bufs[i]; 1922 1923 error = vtscsi_enqueue_event_buf(sc, event); 1924 if (error) 1925 break; 1926 } 1927 1928 /* 1929 * Even just one buffer is enough. Missed events are 1930 * denoted with the VIRTIO_SCSI_T_EVENTS_MISSED flag. 1931 */ 1932 if (i > 0) 1933 error = 0; 1934 1935 return (error); 1936 } 1937 1938 static void 1939 vtscsi_reinit_event_vq(struct vtscsi_softc *sc) 1940 { 1941 struct virtio_scsi_event *event; 1942 int i, error; 1943 1944 if ((sc->vtscsi_flags & VTSCSI_FLAG_HOTPLUG) == 0 || 1945 sc->vtscsi_event_buf_size < sizeof(struct virtio_scsi_event)) 1946 return; 1947 1948 for (i = 0; i < VTSCSI_NUM_EVENT_BUFS; i++) { 1949 event = &sc->vtscsi_event_bufs[i]; 1950 1951 error = vtscsi_enqueue_event_buf(sc, event); 1952 if (error) 1953 break; 1954 } 1955 1956 KASSERT(i > 0, ("cannot reinit event vq: %d", error)); 1957 } 1958 1959 static void 1960 vtscsi_drain_event_vq(struct vtscsi_softc *sc) 1961 { 1962 struct virtqueue *vq; 1963 int last; 1964 1965 vq = sc->vtscsi_event_vq; 1966 last = 0; 1967 1968 while (virtqueue_drain(vq, &last) != NULL) 1969 ; 1970 1971 KASSERT(virtqueue_empty(vq), ("eventvq not empty")); 1972 } 1973 1974 static void 1975 vtscsi_complete_vqs_locked(struct vtscsi_softc *sc) 1976 { 1977 1978 VTSCSI_LOCK_OWNED(sc); 1979 1980 if (sc->vtscsi_request_vq != NULL) 1981 vtscsi_complete_vq(sc, sc->vtscsi_request_vq); 1982 if (sc->vtscsi_control_vq != NULL) 1983 vtscsi_complete_vq(sc, sc->vtscsi_control_vq); 1984 } 1985 1986 static void 1987 vtscsi_complete_vqs(struct vtscsi_softc *sc) 1988 { 1989 1990 VTSCSI_LOCK(sc); 1991 vtscsi_complete_vqs_locked(sc); 1992 VTSCSI_UNLOCK(sc); 1993 } 1994 1995 static void 1996 vtscsi_cancel_request(struct vtscsi_softc *sc, struct vtscsi_request *req) 1997 { 1998 union ccb *ccb; 1999 int detach; 2000 2001 ccb = req->vsr_ccb; 2002 2003 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p\n", req, ccb); 2004 2005 /* 2006 * The callout must be drained when detaching since the request is 2007 * about to be freed. The VTSCSI_MTX must not be held for this in 2008 * case the callout is pending because there is a deadlock potential. 2009 * Otherwise, the virtqueue is being drained because of a bus reset 2010 * so we only need to attempt to stop the callouts. 2011 */ 2012 detach = (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) != 0; 2013 if (detach != 0) 2014 VTSCSI_LOCK_NOTOWNED(sc); 2015 else 2016 VTSCSI_LOCK_OWNED(sc); 2017 2018 if (req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) { 2019 if (detach != 0) 2020 callout_drain(&req->vsr_callout); 2021 else 2022 callout_stop(&req->vsr_callout); 2023 } 2024 2025 if (ccb != NULL) { 2026 if (detach != 0) { 2027 VTSCSI_LOCK(sc); 2028 ccb->ccb_h.status = CAM_NO_HBA; 2029 } else 2030 ccb->ccb_h.status = CAM_REQUEUE_REQ; 2031 xpt_done(ccb); 2032 if (detach != 0) 2033 VTSCSI_UNLOCK(sc); 2034 } 2035 2036 vtscsi_enqueue_request(sc, req); 2037 } 2038 2039 static void 2040 vtscsi_drain_vq(struct vtscsi_softc *sc, struct virtqueue *vq) 2041 { 2042 struct vtscsi_request *req; 2043 int last; 2044 2045 last = 0; 2046 2047 vtscsi_dprintf(sc, VTSCSI_TRACE, "vq=%p\n", vq); 2048 2049 while ((req = virtqueue_drain(vq, &last)) != NULL) 2050 vtscsi_cancel_request(sc, req); 2051 2052 KASSERT(virtqueue_empty(vq), ("virtqueue not empty")); 2053 } 2054 2055 static void 2056 vtscsi_drain_vqs(struct vtscsi_softc *sc) 2057 { 2058 2059 if (sc->vtscsi_control_vq != NULL) 2060 vtscsi_drain_vq(sc, sc->vtscsi_control_vq); 2061 if (sc->vtscsi_request_vq != NULL) 2062 vtscsi_drain_vq(sc, sc->vtscsi_request_vq); 2063 if (sc->vtscsi_event_vq != NULL) 2064 vtscsi_drain_event_vq(sc); 2065 } 2066 2067 static void 2068 vtscsi_stop(struct vtscsi_softc *sc) 2069 { 2070 2071 vtscsi_disable_vqs_intr(sc); 2072 virtio_stop(sc->vtscsi_dev); 2073 } 2074 2075 static int 2076 vtscsi_reset_bus(struct vtscsi_softc *sc) 2077 { 2078 int error; 2079 2080 VTSCSI_LOCK_OWNED(sc); 2081 2082 if (vtscsi_bus_reset_disable != 0) { 2083 device_printf(sc->vtscsi_dev, "bus reset disabled\n"); 2084 return (0); 2085 } 2086 2087 sc->vtscsi_flags |= VTSCSI_FLAG_RESET; 2088 2089 /* 2090 * vtscsi_stop() will cause the in-flight requests to be canceled. 2091 * Those requests are then completed here so CAM will retry them 2092 * after the reset is complete. 2093 */ 2094 vtscsi_stop(sc); 2095 vtscsi_complete_vqs_locked(sc); 2096 2097 /* Rid the virtqueues of any remaining requests. */ 2098 vtscsi_drain_vqs(sc); 2099 2100 /* 2101 * Any resource shortage that froze the SIMQ cannot persist across 2102 * a bus reset so ensure it gets thawed here. 2103 */ 2104 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST | VTSCSI_REQUEST_VQ) != 0) 2105 xpt_release_simq(sc->vtscsi_sim, 0); 2106 2107 error = vtscsi_reinit(sc); 2108 if (error) { 2109 device_printf(sc->vtscsi_dev, 2110 "reinitialization failed, stopping device...\n"); 2111 vtscsi_stop(sc); 2112 } else 2113 vtscsi_announce(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD, 2114 CAM_LUN_WILDCARD); 2115 2116 sc->vtscsi_flags &= ~VTSCSI_FLAG_RESET; 2117 2118 return (error); 2119 } 2120 2121 static void 2122 vtscsi_init_request(struct vtscsi_softc *sc, struct vtscsi_request *req) 2123 { 2124 2125 #ifdef INVARIANTS 2126 int req_nsegs, resp_nsegs; 2127 2128 req_nsegs = sglist_count(&req->vsr_ureq, sizeof(req->vsr_ureq)); 2129 resp_nsegs = sglist_count(&req->vsr_uresp, sizeof(req->vsr_uresp)); 2130 2131 KASSERT(req_nsegs == 1, ("request crossed page boundary")); 2132 KASSERT(resp_nsegs == 1, ("response crossed page boundary")); 2133 #endif 2134 2135 req->vsr_softc = sc; 2136 callout_init_lk(&req->vsr_callout, VTSCSI_MTX(sc)); 2137 } 2138 2139 static int 2140 vtscsi_alloc_requests(struct vtscsi_softc *sc) 2141 { 2142 struct vtscsi_request *req; 2143 int i, nreqs; 2144 2145 /* 2146 * Commands destined for either the request or control queues come 2147 * from the same SIM queue. Use the size of the request virtqueue 2148 * as it (should) be much more frequently used. Some additional 2149 * requests are allocated for internal (TMF) use. 2150 */ 2151 nreqs = virtqueue_size(sc->vtscsi_request_vq); 2152 if ((sc->vtscsi_flags & VTSCSI_FLAG_INDIRECT) == 0) 2153 nreqs /= VTSCSI_MIN_SEGMENTS; 2154 nreqs += VTSCSI_RESERVED_REQUESTS; 2155 2156 for (i = 0; i < nreqs; i++) { 2157 req = contigmalloc(sizeof(struct vtscsi_request), M_DEVBUF, 2158 M_WAITOK, 0, BUS_SPACE_MAXADDR, 16, 0); 2159 if (req == NULL) 2160 return (ENOMEM); 2161 2162 vtscsi_init_request(sc, req); 2163 2164 sc->vtscsi_nrequests++; 2165 vtscsi_enqueue_request(sc, req); 2166 } 2167 2168 return (0); 2169 } 2170 2171 static void 2172 vtscsi_free_requests(struct vtscsi_softc *sc) 2173 { 2174 struct vtscsi_request *req; 2175 2176 while ((req = vtscsi_dequeue_request(sc)) != NULL) { 2177 KASSERT(callout_active(&req->vsr_callout) == 0, 2178 ("request callout still active")); 2179 2180 sc->vtscsi_nrequests--; 2181 contigfree(req, sizeof(struct vtscsi_request), M_DEVBUF); 2182 } 2183 2184 KASSERT(sc->vtscsi_nrequests == 0, ("leaked requests: %d", 2185 sc->vtscsi_nrequests)); 2186 } 2187 2188 static void 2189 vtscsi_enqueue_request(struct vtscsi_softc *sc, struct vtscsi_request *req) 2190 { 2191 2192 KASSERT(req->vsr_softc == sc, 2193 ("non-matching request vsr_softc %p/%p", req->vsr_softc, sc)); 2194 2195 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p\n", req); 2196 2197 /* A request is available so the SIMQ could be released. */ 2198 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST) != 0) 2199 xpt_release_simq(sc->vtscsi_sim, 1); 2200 2201 req->vsr_ccb = NULL; 2202 req->vsr_complete = NULL; 2203 req->vsr_ptr0 = NULL; 2204 req->vsr_state = VTSCSI_REQ_STATE_FREE; 2205 req->vsr_flags = 0; 2206 2207 bzero(&req->vsr_ureq, sizeof(req->vsr_ureq)); 2208 bzero(&req->vsr_uresp, sizeof(req->vsr_uresp)); 2209 2210 /* 2211 * We insert at the tail of the queue in order to make it 2212 * very unlikely a request will be reused if we race with 2213 * stopping its callout handler. 2214 */ 2215 TAILQ_INSERT_TAIL(&sc->vtscsi_req_free, req, vsr_link); 2216 } 2217 2218 static struct vtscsi_request * 2219 vtscsi_dequeue_request(struct vtscsi_softc *sc) 2220 { 2221 struct vtscsi_request *req; 2222 2223 req = TAILQ_FIRST(&sc->vtscsi_req_free); 2224 if (req != NULL) { 2225 req->vsr_state = VTSCSI_REQ_STATE_INUSE; 2226 TAILQ_REMOVE(&sc->vtscsi_req_free, req, vsr_link); 2227 } else 2228 sc->vtscsi_stats.dequeue_no_requests++; 2229 2230 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p\n", req); 2231 2232 return (req); 2233 } 2234 2235 static void 2236 vtscsi_complete_request(struct vtscsi_request *req) 2237 { 2238 2239 if (req->vsr_flags & VTSCSI_REQ_FLAG_POLLED) 2240 req->vsr_flags |= VTSCSI_REQ_FLAG_COMPLETE; 2241 2242 if (req->vsr_complete != NULL) 2243 req->vsr_complete(req->vsr_softc, req); 2244 } 2245 2246 static void 2247 vtscsi_complete_vq(struct vtscsi_softc *sc, struct virtqueue *vq) 2248 { 2249 struct vtscsi_request *req; 2250 2251 VTSCSI_LOCK_OWNED(sc); 2252 2253 while ((req = virtqueue_dequeue(vq, NULL)) != NULL) 2254 vtscsi_complete_request(req); 2255 } 2256 2257 static void 2258 vtscsi_control_vq_intr(void *xsc) 2259 { 2260 struct vtscsi_softc *sc; 2261 struct virtqueue *vq; 2262 2263 sc = xsc; 2264 vq = sc->vtscsi_control_vq; 2265 2266 again: 2267 VTSCSI_LOCK(sc); 2268 if (!virtqueue_pending(vq)) 2269 goto done; 2270 2271 vtscsi_complete_vq(sc, sc->vtscsi_control_vq); 2272 2273 if (virtqueue_enable_intr(vq) != 0) { 2274 virtqueue_disable_intr(vq); 2275 VTSCSI_UNLOCK(sc); 2276 goto again; 2277 } 2278 2279 done: 2280 VTSCSI_UNLOCK(sc); 2281 } 2282 2283 static void 2284 vtscsi_event_vq_intr(void *xsc) 2285 { 2286 struct vtscsi_softc *sc; 2287 struct virtqueue *vq; 2288 struct virtio_scsi_event *event; 2289 2290 sc = xsc; 2291 vq = sc->vtscsi_event_vq; 2292 2293 again: 2294 VTSCSI_LOCK(sc); 2295 if (!virtqueue_pending(vq)) 2296 goto done; 2297 2298 while ((event = virtqueue_dequeue(vq, NULL)) != NULL) 2299 vtscsi_handle_event(sc, event); 2300 2301 if (virtqueue_enable_intr(vq) != 0) { 2302 virtqueue_disable_intr(vq); 2303 VTSCSI_UNLOCK(sc); 2304 goto again; 2305 } 2306 2307 done: 2308 VTSCSI_UNLOCK(sc); 2309 } 2310 2311 static void 2312 vtscsi_request_vq_intr(void *xsc) 2313 { 2314 struct vtscsi_softc *sc; 2315 struct virtqueue *vq; 2316 2317 sc = xsc; 2318 vq = sc->vtscsi_request_vq; 2319 2320 again: 2321 VTSCSI_LOCK(sc); 2322 if (!virtqueue_pending(vq)) 2323 goto done; 2324 2325 vtscsi_complete_vq(sc, sc->vtscsi_request_vq); 2326 2327 if (virtqueue_enable_intr(vq) != 0) { 2328 virtqueue_disable_intr(vq); 2329 VTSCSI_UNLOCK(sc); 2330 goto again; 2331 } 2332 2333 done: 2334 VTSCSI_UNLOCK(sc); 2335 } 2336 2337 static void 2338 vtscsi_disable_vqs_intr(struct vtscsi_softc *sc) 2339 { 2340 2341 virtqueue_disable_intr(sc->vtscsi_control_vq); 2342 virtqueue_disable_intr(sc->vtscsi_event_vq); 2343 virtqueue_disable_intr(sc->vtscsi_request_vq); 2344 } 2345 2346 static void 2347 vtscsi_enable_vqs_intr(struct vtscsi_softc *sc) 2348 { 2349 2350 virtqueue_enable_intr(sc->vtscsi_control_vq); 2351 virtqueue_enable_intr(sc->vtscsi_event_vq); 2352 virtqueue_enable_intr(sc->vtscsi_request_vq); 2353 } 2354 2355 static void 2356 vtscsi_get_tunables(struct vtscsi_softc *sc) 2357 { 2358 char tmpstr[64]; 2359 2360 TUNABLE_INT_FETCH("hw.vtscsi.debug_level", &sc->vtscsi_debug); 2361 2362 ksnprintf(tmpstr, sizeof(tmpstr), "dev.vtscsi.%d.debug_level", 2363 device_get_unit(sc->vtscsi_dev)); 2364 TUNABLE_INT_FETCH(tmpstr, &sc->vtscsi_debug); 2365 } 2366 2367 static void 2368 vtscsi_add_sysctl(struct vtscsi_softc *sc) 2369 { 2370 device_t dev; 2371 struct vtscsi_statistics *stats; 2372 struct sysctl_ctx_list *ctx; 2373 struct sysctl_oid *tree; 2374 struct sysctl_oid_list *child; 2375 2376 dev = sc->vtscsi_dev; 2377 stats = &sc->vtscsi_stats; 2378 ctx = device_get_sysctl_ctx(dev); 2379 tree = device_get_sysctl_tree(dev); 2380 child = SYSCTL_CHILDREN(tree); 2381 2382 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug_level", 2383 CTLFLAG_RW, &sc->vtscsi_debug, 0, 2384 "Debug level"); 2385 2386 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "scsi_cmd_timeouts", 2387 CTLFLAG_RD, &stats->scsi_cmd_timeouts, 2388 "SCSI command timeouts"); 2389 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dequeue_no_requests", 2390 CTLFLAG_RD, &stats->dequeue_no_requests, 2391 "No available requests to dequeue"); 2392 } 2393 2394 static void 2395 vtscsi_printf_req(struct vtscsi_request *req, const char *func, 2396 const char *fmt, ...) 2397 { 2398 struct vtscsi_softc *sc; 2399 union ccb *ccb; 2400 struct sbuf sb; 2401 __va_list ap; 2402 char str[192]; 2403 char path_str[64]; 2404 2405 if (req == NULL) 2406 return; 2407 2408 sc = req->vsr_softc; 2409 ccb = req->vsr_ccb; 2410 2411 __va_start(ap, fmt); 2412 sbuf_new(&sb, str, sizeof(str), 0); 2413 2414 if (ccb == NULL) { 2415 sbuf_printf(&sb, "(noperiph:%s%d:%u): ", 2416 cam_sim_name(sc->vtscsi_sim), cam_sim_unit(sc->vtscsi_sim), 2417 cam_sim_bus(sc->vtscsi_sim)); 2418 } else { 2419 xpt_path_string(ccb->ccb_h.path, path_str, sizeof(path_str)); 2420 sbuf_cat(&sb, path_str); 2421 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 2422 scsi_command_string(&ccb->csio, &sb); 2423 sbuf_printf(&sb, "length %d ", ccb->csio.dxfer_len); 2424 } 2425 } 2426 2427 sbuf_vprintf(&sb, fmt, ap); 2428 __va_end(ap); 2429 2430 sbuf_finish(&sb); 2431 kprintf("%s: %s: %s", device_get_nameunit(sc->vtscsi_dev), func, 2432 sbuf_data(&sb)); 2433 } 2434