1 /*- 2 * Implementation of the Common Access Method Transport (XPT) layer. 3 * 4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs. 5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions, and the following disclaimer, 13 * without modification, immediately at the beginning of the file. 14 * 2. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/systm.h> 36 #include <sys/types.h> 37 #include <sys/malloc.h> 38 #include <sys/kernel.h> 39 #include <sys/time.h> 40 #include <sys/conf.h> 41 #include <sys/fcntl.h> 42 #include <sys/md5.h> 43 #include <sys/interrupt.h> 44 #include <sys/sbuf.h> 45 #include <sys/taskqueue.h> 46 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/sysctl.h> 50 #include <sys/kthread.h> 51 52 #ifdef PC98 53 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */ 54 #endif 55 56 #include <cam/cam.h> 57 #include <cam/cam_ccb.h> 58 #include <cam/cam_periph.h> 59 #include <cam/cam_sim.h> 60 #include <cam/cam_xpt.h> 61 #include <cam/cam_xpt_sim.h> 62 #include <cam/cam_xpt_periph.h> 63 #include <cam/cam_debug.h> 64 65 #include <cam/scsi/scsi_all.h> 66 #include <cam/scsi/scsi_message.h> 67 #include <cam/scsi/scsi_pass.h> 68 #include <machine/stdarg.h> /* for xpt_print below */ 69 #include "opt_cam.h" 70 71 /* Datastructures internal to the xpt layer */ 72 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers"); 73 74 /* Object for defering XPT actions to a taskqueue */ 75 struct xpt_task { 76 struct task task; 77 void *data1; 78 uintptr_t data2; 79 }; 80 81 /* 82 * Definition of an async handler callback block. These are used to add 83 * SIMs and peripherals to the async callback lists. 84 */ 85 struct async_node { 86 SLIST_ENTRY(async_node) links; 87 u_int32_t event_enable; /* Async Event enables */ 88 void (*callback)(void *arg, u_int32_t code, 89 struct cam_path *path, void *args); 90 void *callback_arg; 91 }; 92 93 SLIST_HEAD(async_list, async_node); 94 SLIST_HEAD(periph_list, cam_periph); 95 96 /* 97 * This is the maximum number of high powered commands (e.g. start unit) 98 * that can be outstanding at a particular time. 99 */ 100 #ifndef CAM_MAX_HIGHPOWER 101 #define CAM_MAX_HIGHPOWER 4 102 #endif 103 104 /* 105 * Structure for queueing a device in a run queue. 106 * There is one run queue for allocating new ccbs, 107 * and another for sending ccbs to the controller. 108 */ 109 struct cam_ed_qinfo { 110 cam_pinfo pinfo; 111 struct cam_ed *device; 112 }; 113 114 /* 115 * The CAM EDT (Existing Device Table) contains the device information for 116 * all devices for all busses in the system. The table contains a 117 * cam_ed structure for each device on the bus. 118 */ 119 struct cam_ed { 120 TAILQ_ENTRY(cam_ed) links; 121 struct cam_ed_qinfo alloc_ccb_entry; 122 struct cam_ed_qinfo send_ccb_entry; 123 struct cam_et *target; 124 struct cam_sim *sim; 125 lun_id_t lun_id; 126 struct camq drvq; /* 127 * Queue of type drivers wanting to do 128 * work on this device. 129 */ 130 struct cam_ccbq ccbq; /* Queue of pending ccbs */ 131 struct async_list asyncs; /* Async callback info for this B/T/L */ 132 struct periph_list periphs; /* All attached devices */ 133 u_int generation; /* Generation number */ 134 struct cam_periph *owner; /* Peripheral driver's ownership tag */ 135 struct xpt_quirk_entry *quirk; /* Oddities about this device */ 136 /* Storage for the inquiry data */ 137 cam_proto protocol; 138 u_int protocol_version; 139 cam_xport transport; 140 u_int transport_version; 141 struct scsi_inquiry_data inq_data; 142 u_int8_t inq_flags; /* 143 * Current settings for inquiry flags. 144 * This allows us to override settings 145 * like disconnection and tagged 146 * queuing for a device. 147 */ 148 u_int8_t queue_flags; /* Queue flags from the control page */ 149 u_int8_t serial_num_len; 150 u_int8_t *serial_num; 151 u_int32_t qfrozen_cnt; 152 u_int32_t flags; 153 #define CAM_DEV_UNCONFIGURED 0x01 154 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02 155 #define CAM_DEV_REL_ON_COMPLETE 0x04 156 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08 157 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10 158 #define CAM_DEV_TAG_AFTER_COUNT 0x20 159 #define CAM_DEV_INQUIRY_DATA_VALID 0x40 160 #define CAM_DEV_IN_DV 0x80 161 #define CAM_DEV_DV_HIT_BOTTOM 0x100 162 u_int32_t tag_delay_count; 163 #define CAM_TAG_DELAY_COUNT 5 164 u_int32_t tag_saved_openings; 165 u_int32_t refcount; 166 struct callout callout; 167 }; 168 169 /* 170 * Each target is represented by an ET (Existing Target). These 171 * entries are created when a target is successfully probed with an 172 * identify, and removed when a device fails to respond after a number 173 * of retries, or a bus rescan finds the device missing. 174 */ 175 struct cam_et { 176 TAILQ_HEAD(, cam_ed) ed_entries; 177 TAILQ_ENTRY(cam_et) links; 178 struct cam_eb *bus; 179 target_id_t target_id; 180 u_int32_t refcount; 181 u_int generation; 182 struct timeval last_reset; 183 }; 184 185 /* 186 * Each bus is represented by an EB (Existing Bus). These entries 187 * are created by calls to xpt_bus_register and deleted by calls to 188 * xpt_bus_deregister. 189 */ 190 struct cam_eb { 191 TAILQ_HEAD(, cam_et) et_entries; 192 TAILQ_ENTRY(cam_eb) links; 193 path_id_t path_id; 194 struct cam_sim *sim; 195 struct timeval last_reset; 196 u_int32_t flags; 197 #define CAM_EB_RUNQ_SCHEDULED 0x01 198 u_int32_t refcount; 199 u_int generation; 200 }; 201 202 struct cam_path { 203 struct cam_periph *periph; 204 struct cam_eb *bus; 205 struct cam_et *target; 206 struct cam_ed *device; 207 }; 208 209 struct xpt_quirk_entry { 210 struct scsi_inquiry_pattern inq_pat; 211 u_int8_t quirks; 212 #define CAM_QUIRK_NOLUNS 0x01 213 #define CAM_QUIRK_NOSERIAL 0x02 214 #define CAM_QUIRK_HILUNS 0x04 215 #define CAM_QUIRK_NOHILUNS 0x08 216 u_int mintags; 217 u_int maxtags; 218 }; 219 220 static int cam_srch_hi = 0; 221 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi); 222 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS); 223 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0, 224 sysctl_cam_search_luns, "I", 225 "allow search above LUN 7 for SCSI3 and greater devices"); 226 227 #define CAM_SCSI2_MAXLUN 8 228 /* 229 * If we're not quirked to search <= the first 8 luns 230 * and we are either quirked to search above lun 8, 231 * or we're > SCSI-2 and we've enabled hilun searching, 232 * or we're > SCSI-2 and the last lun was a success, 233 * we can look for luns above lun 8. 234 */ 235 #define CAN_SRCH_HI_SPARSE(dv) \ 236 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \ 237 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \ 238 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi))) 239 240 #define CAN_SRCH_HI_DENSE(dv) \ 241 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \ 242 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \ 243 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2))) 244 245 typedef enum { 246 XPT_FLAG_OPEN = 0x01 247 } xpt_flags; 248 249 struct xpt_softc { 250 xpt_flags flags; 251 u_int32_t xpt_generation; 252 253 /* number of high powered commands that can go through right now */ 254 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq; 255 int num_highpower; 256 257 /* queue for handling async rescan requests. */ 258 TAILQ_HEAD(, ccb_hdr) ccb_scanq; 259 260 /* Registered busses */ 261 TAILQ_HEAD(,cam_eb) xpt_busses; 262 u_int bus_generation; 263 264 struct intr_config_hook *xpt_config_hook; 265 266 struct mtx xpt_topo_lock; 267 struct mtx xpt_lock; 268 }; 269 270 static const char quantum[] = "QUANTUM"; 271 static const char sony[] = "SONY"; 272 static const char west_digital[] = "WDIGTL"; 273 static const char samsung[] = "SAMSUNG"; 274 static const char seagate[] = "SEAGATE"; 275 static const char microp[] = "MICROP"; 276 277 static struct xpt_quirk_entry xpt_quirk_table[] = 278 { 279 { 280 /* Reports QUEUE FULL for temporary resource shortages */ 281 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" }, 282 /*quirks*/0, /*mintags*/24, /*maxtags*/32 283 }, 284 { 285 /* Reports QUEUE FULL for temporary resource shortages */ 286 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" }, 287 /*quirks*/0, /*mintags*/24, /*maxtags*/32 288 }, 289 { 290 /* Reports QUEUE FULL for temporary resource shortages */ 291 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" }, 292 /*quirks*/0, /*mintags*/24, /*maxtags*/32 293 }, 294 { 295 /* Broken tagged queuing drive */ 296 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" }, 297 /*quirks*/0, /*mintags*/0, /*maxtags*/0 298 }, 299 { 300 /* Broken tagged queuing drive */ 301 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" }, 302 /*quirks*/0, /*mintags*/0, /*maxtags*/0 303 }, 304 { 305 /* Broken tagged queuing drive */ 306 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" }, 307 /*quirks*/0, /*mintags*/0, /*maxtags*/0 308 }, 309 { 310 /* 311 * Unfortunately, the Quantum Atlas III has the same 312 * problem as the Atlas II drives above. 313 * Reported by: "Johan Granlund" <johan@granlund.nu> 314 * 315 * For future reference, the drive with the problem was: 316 * QUANTUM QM39100TD-SW N1B0 317 * 318 * It's possible that Quantum will fix the problem in later 319 * firmware revisions. If that happens, the quirk entry 320 * will need to be made specific to the firmware revisions 321 * with the problem. 322 * 323 */ 324 /* Reports QUEUE FULL for temporary resource shortages */ 325 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" }, 326 /*quirks*/0, /*mintags*/24, /*maxtags*/32 327 }, 328 { 329 /* 330 * 18 Gig Atlas III, same problem as the 9G version. 331 * Reported by: Andre Albsmeier 332 * <andre.albsmeier@mchp.siemens.de> 333 * 334 * For future reference, the drive with the problem was: 335 * QUANTUM QM318000TD-S N491 336 */ 337 /* Reports QUEUE FULL for temporary resource shortages */ 338 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" }, 339 /*quirks*/0, /*mintags*/24, /*maxtags*/32 340 }, 341 { 342 /* 343 * Broken tagged queuing drive 344 * Reported by: Bret Ford <bford@uop.cs.uop.edu> 345 * and: Martin Renters <martin@tdc.on.ca> 346 */ 347 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" }, 348 /*quirks*/0, /*mintags*/0, /*maxtags*/0 349 }, 350 /* 351 * The Seagate Medalist Pro drives have very poor write 352 * performance with anything more than 2 tags. 353 * 354 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl> 355 * Drive: <SEAGATE ST36530N 1444> 356 * 357 * Reported by: Jeremy Lea <reg@shale.csir.co.za> 358 * Drive: <SEAGATE ST34520W 1281> 359 * 360 * No one has actually reported that the 9G version 361 * (ST39140*) of the Medalist Pro has the same problem, but 362 * we're assuming that it does because the 4G and 6.5G 363 * versions of the drive are broken. 364 */ 365 { 366 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"}, 367 /*quirks*/0, /*mintags*/2, /*maxtags*/2 368 }, 369 { 370 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"}, 371 /*quirks*/0, /*mintags*/2, /*maxtags*/2 372 }, 373 { 374 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"}, 375 /*quirks*/0, /*mintags*/2, /*maxtags*/2 376 }, 377 { 378 /* 379 * Slow when tagged queueing is enabled. Write performance 380 * steadily drops off with more and more concurrent 381 * transactions. Best sequential write performance with 382 * tagged queueing turned off and write caching turned on. 383 * 384 * PR: kern/10398 385 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp> 386 * Drive: DCAS-34330 w/ "S65A" firmware. 387 * 388 * The drive with the problem had the "S65A" firmware 389 * revision, and has also been reported (by Stephen J. 390 * Roznowski <sjr@home.net>) for a drive with the "S61A" 391 * firmware revision. 392 * 393 * Although no one has reported problems with the 2 gig 394 * version of the DCAS drive, the assumption is that it 395 * has the same problems as the 4 gig version. Therefore 396 * this quirk entries disables tagged queueing for all 397 * DCAS drives. 398 */ 399 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" }, 400 /*quirks*/0, /*mintags*/0, /*maxtags*/0 401 }, 402 { 403 /* Broken tagged queuing drive */ 404 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" }, 405 /*quirks*/0, /*mintags*/0, /*maxtags*/0 406 }, 407 { 408 /* Broken tagged queuing drive */ 409 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" }, 410 /*quirks*/0, /*mintags*/0, /*maxtags*/0 411 }, 412 { 413 /* This does not support other than LUN 0 */ 414 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" }, 415 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255 416 }, 417 { 418 /* 419 * Broken tagged queuing drive. 420 * Submitted by: 421 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp> 422 * in PR kern/9535 423 */ 424 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" }, 425 /*quirks*/0, /*mintags*/0, /*maxtags*/0 426 }, 427 { 428 /* 429 * Slow when tagged queueing is enabled. (1.5MB/sec versus 430 * 8MB/sec.) 431 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu> 432 * Best performance with these drives is achieved with 433 * tagged queueing turned off, and write caching turned on. 434 */ 435 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" }, 436 /*quirks*/0, /*mintags*/0, /*maxtags*/0 437 }, 438 { 439 /* 440 * Slow when tagged queueing is enabled. (1.5MB/sec versus 441 * 8MB/sec.) 442 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu> 443 * Best performance with these drives is achieved with 444 * tagged queueing turned off, and write caching turned on. 445 */ 446 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" }, 447 /*quirks*/0, /*mintags*/0, /*maxtags*/0 448 }, 449 { 450 /* 451 * Doesn't handle queue full condition correctly, 452 * so we need to limit maxtags to what the device 453 * can handle instead of determining this automatically. 454 */ 455 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" }, 456 /*quirks*/0, /*mintags*/2, /*maxtags*/32 457 }, 458 { 459 /* Really only one LUN */ 460 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" }, 461 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 462 }, 463 { 464 /* I can't believe we need a quirk for DPT volumes. */ 465 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" }, 466 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, 467 /*mintags*/0, /*maxtags*/255 468 }, 469 { 470 /* 471 * Many Sony CDROM drives don't like multi-LUN probing. 472 */ 473 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" }, 474 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 475 }, 476 { 477 /* 478 * This drive doesn't like multiple LUN probing. 479 * Submitted by: Parag Patel <parag@cgt.com> 480 */ 481 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" }, 482 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 483 }, 484 { 485 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" }, 486 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 487 }, 488 { 489 /* 490 * The 8200 doesn't like multi-lun probing, and probably 491 * don't like serial number requests either. 492 */ 493 { 494 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE", 495 "EXB-8200*", "*" 496 }, 497 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 498 }, 499 { 500 /* 501 * Let's try the same as above, but for a drive that says 502 * it's an IPL-6860 but is actually an EXB 8200. 503 */ 504 { 505 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE", 506 "IPL-6860*", "*" 507 }, 508 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 509 }, 510 { 511 /* 512 * These Hitachi drives don't like multi-lun probing. 513 * The PR submitter has a DK319H, but says that the Linux 514 * kernel has a similar work-around for the DK312 and DK314, 515 * so all DK31* drives are quirked here. 516 * PR: misc/18793 517 * Submitted by: Paul Haddad <paul@pth.com> 518 */ 519 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" }, 520 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255 521 }, 522 { 523 /* 524 * The Hitachi CJ series with J8A8 firmware apparantly has 525 * problems with tagged commands. 526 * PR: 23536 527 * Reported by: amagai@nue.org 528 */ 529 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" }, 530 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 531 }, 532 { 533 /* 534 * These are the large storage arrays. 535 * Submitted by: William Carrel <william.carrel@infospace.com> 536 */ 537 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" }, 538 CAM_QUIRK_HILUNS, 2, 1024 539 }, 540 { 541 /* 542 * This old revision of the TDC3600 is also SCSI-1, and 543 * hangs upon serial number probing. 544 */ 545 { 546 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG", 547 " TDC 3600", "U07:" 548 }, 549 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0 550 }, 551 { 552 /* 553 * Maxtor Personal Storage 3000XT (Firewire) 554 * hangs upon serial number probing. 555 */ 556 { 557 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor", 558 "1394 storage", "*" 559 }, 560 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0 561 }, 562 { 563 /* 564 * Would repond to all LUNs if asked for. 565 */ 566 { 567 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER", 568 "CP150", "*" 569 }, 570 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 571 }, 572 { 573 /* 574 * Would repond to all LUNs if asked for. 575 */ 576 { 577 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY", 578 "96X2*", "*" 579 }, 580 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 581 }, 582 { 583 /* Submitted by: Matthew Dodd <winter@jurai.net> */ 584 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" }, 585 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 586 }, 587 { 588 /* Submitted by: Matthew Dodd <winter@jurai.net> */ 589 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" }, 590 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 591 }, 592 { 593 /* TeraSolutions special settings for TRC-22 RAID */ 594 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" }, 595 /*quirks*/0, /*mintags*/55, /*maxtags*/255 596 }, 597 { 598 /* Veritas Storage Appliance */ 599 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" }, 600 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024 601 }, 602 { 603 /* 604 * Would respond to all LUNs. Device type and removable 605 * flag are jumper-selectable. 606 */ 607 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix", 608 "Tahiti 1", "*" 609 }, 610 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 611 }, 612 { 613 /* EasyRAID E5A aka. areca ARC-6010 */ 614 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" }, 615 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255 616 }, 617 { 618 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" }, 619 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0 620 }, 621 { 622 /* Default tagged queuing parameters for all devices */ 623 { 624 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 625 /*vendor*/"*", /*product*/"*", /*revision*/"*" 626 }, 627 /*quirks*/0, /*mintags*/2, /*maxtags*/255 628 }, 629 }; 630 631 static const int xpt_quirk_table_size = 632 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table); 633 634 typedef enum { 635 DM_RET_COPY = 0x01, 636 DM_RET_FLAG_MASK = 0x0f, 637 DM_RET_NONE = 0x00, 638 DM_RET_STOP = 0x10, 639 DM_RET_DESCEND = 0x20, 640 DM_RET_ERROR = 0x30, 641 DM_RET_ACTION_MASK = 0xf0 642 } dev_match_ret; 643 644 typedef enum { 645 XPT_DEPTH_BUS, 646 XPT_DEPTH_TARGET, 647 XPT_DEPTH_DEVICE, 648 XPT_DEPTH_PERIPH 649 } xpt_traverse_depth; 650 651 struct xpt_traverse_config { 652 xpt_traverse_depth depth; 653 void *tr_func; 654 void *tr_arg; 655 }; 656 657 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg); 658 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg); 659 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg); 660 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg); 661 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg); 662 663 /* Transport layer configuration information */ 664 static struct xpt_softc xsoftc; 665 666 /* Queues for our software interrupt handler */ 667 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t; 668 static cam_isrq_t cam_bioq; 669 static struct mtx cam_bioq_lock; 670 671 /* Pointers to software interrupt handlers */ 672 static void *cambio_ih; 673 674 struct cam_periph *xpt_periph; 675 676 static periph_init_t xpt_periph_init; 677 678 static periph_init_t probe_periph_init; 679 680 static struct periph_driver xpt_driver = 681 { 682 xpt_periph_init, "xpt", 683 TAILQ_HEAD_INITIALIZER(xpt_driver.units) 684 }; 685 686 static struct periph_driver probe_driver = 687 { 688 probe_periph_init, "probe", 689 TAILQ_HEAD_INITIALIZER(probe_driver.units) 690 }; 691 692 PERIPHDRIVER_DECLARE(xpt, xpt_driver); 693 PERIPHDRIVER_DECLARE(probe, probe_driver); 694 695 696 static d_open_t xptopen; 697 static d_close_t xptclose; 698 static d_ioctl_t xptioctl; 699 700 static struct cdevsw xpt_cdevsw = { 701 .d_version = D_VERSION, 702 .d_flags = 0, 703 .d_open = xptopen, 704 .d_close = xptclose, 705 .d_ioctl = xptioctl, 706 .d_name = "xpt", 707 }; 708 709 710 static void dead_sim_action(struct cam_sim *sim, union ccb *ccb); 711 static void dead_sim_poll(struct cam_sim *sim); 712 713 /* Dummy SIM that is used when the real one has gone. */ 714 static struct cam_sim cam_dead_sim = { 715 .sim_action = dead_sim_action, 716 .sim_poll = dead_sim_poll, 717 .sim_name = "dead_sim", 718 }; 719 720 #define SIM_DEAD(sim) ((sim) == &cam_dead_sim) 721 722 723 /* Storage for debugging datastructures */ 724 #ifdef CAMDEBUG 725 struct cam_path *cam_dpath; 726 u_int32_t cam_dflags; 727 u_int32_t cam_debug_delay; 728 #endif 729 730 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG) 731 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS" 732 #endif 733 734 /* 735 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG 736 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS, 737 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified. 738 */ 739 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \ 740 || defined(CAM_DEBUG_LUN) 741 #ifdef CAMDEBUG 742 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \ 743 || !defined(CAM_DEBUG_LUN) 744 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \ 745 and CAM_DEBUG_LUN" 746 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */ 747 #else /* !CAMDEBUG */ 748 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options" 749 #endif /* CAMDEBUG */ 750 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */ 751 752 /* Our boot-time initialization hook */ 753 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *); 754 755 static moduledata_t cam_moduledata = { 756 "cam", 757 cam_module_event_handler, 758 NULL 759 }; 760 761 static int xpt_init(void *); 762 763 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND); 764 MODULE_VERSION(cam, 1); 765 766 767 static cam_status xpt_compile_path(struct cam_path *new_path, 768 struct cam_periph *perph, 769 path_id_t path_id, 770 target_id_t target_id, 771 lun_id_t lun_id); 772 773 static void xpt_release_path(struct cam_path *path); 774 775 static void xpt_async_bcast(struct async_list *async_head, 776 u_int32_t async_code, 777 struct cam_path *path, 778 void *async_arg); 779 static void xpt_dev_async(u_int32_t async_code, 780 struct cam_eb *bus, 781 struct cam_et *target, 782 struct cam_ed *device, 783 void *async_arg); 784 static path_id_t xptnextfreepathid(void); 785 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus); 786 static union ccb *xpt_get_ccb(struct cam_ed *device); 787 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo, 788 u_int32_t new_priority); 789 static void xpt_run_dev_allocq(struct cam_eb *bus); 790 static void xpt_run_dev_sendq(struct cam_eb *bus); 791 static timeout_t xpt_release_devq_timeout; 792 static void xpt_release_simq_timeout(void *arg) __unused; 793 static void xpt_release_bus(struct cam_eb *bus); 794 static void xpt_release_devq_device(struct cam_ed *dev, u_int count, 795 int run_queue); 796 static struct cam_et* 797 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id); 798 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target); 799 static struct cam_ed* 800 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, 801 lun_id_t lun_id); 802 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target, 803 struct cam_ed *device); 804 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings); 805 static struct cam_eb* 806 xpt_find_bus(path_id_t path_id); 807 static struct cam_et* 808 xpt_find_target(struct cam_eb *bus, target_id_t target_id); 809 static struct cam_ed* 810 xpt_find_device(struct cam_et *target, lun_id_t lun_id); 811 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb); 812 static void xpt_scan_lun(struct cam_periph *periph, 813 struct cam_path *path, cam_flags flags, 814 union ccb *ccb); 815 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb); 816 static xpt_busfunc_t xptconfigbuscountfunc; 817 static xpt_busfunc_t xptconfigfunc; 818 static void xpt_config(void *arg); 819 static xpt_devicefunc_t xptpassannouncefunc; 820 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb); 821 static void xptaction(struct cam_sim *sim, union ccb *work_ccb); 822 static void xptpoll(struct cam_sim *sim); 823 static void camisr(void *); 824 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns, 825 u_int num_patterns, struct cam_eb *bus); 826 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns, 827 u_int num_patterns, 828 struct cam_ed *device); 829 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns, 830 u_int num_patterns, 831 struct cam_periph *periph); 832 static xpt_busfunc_t xptedtbusfunc; 833 static xpt_targetfunc_t xptedttargetfunc; 834 static xpt_devicefunc_t xptedtdevicefunc; 835 static xpt_periphfunc_t xptedtperiphfunc; 836 static xpt_pdrvfunc_t xptplistpdrvfunc; 837 static xpt_periphfunc_t xptplistperiphfunc; 838 static int xptedtmatch(struct ccb_dev_match *cdm); 839 static int xptperiphlistmatch(struct ccb_dev_match *cdm); 840 static int xptbustraverse(struct cam_eb *start_bus, 841 xpt_busfunc_t *tr_func, void *arg); 842 static int xpttargettraverse(struct cam_eb *bus, 843 struct cam_et *start_target, 844 xpt_targetfunc_t *tr_func, void *arg); 845 static int xptdevicetraverse(struct cam_et *target, 846 struct cam_ed *start_device, 847 xpt_devicefunc_t *tr_func, void *arg); 848 static int xptperiphtraverse(struct cam_ed *device, 849 struct cam_periph *start_periph, 850 xpt_periphfunc_t *tr_func, void *arg); 851 static int xptpdrvtraverse(struct periph_driver **start_pdrv, 852 xpt_pdrvfunc_t *tr_func, void *arg); 853 static int xptpdperiphtraverse(struct periph_driver **pdrv, 854 struct cam_periph *start_periph, 855 xpt_periphfunc_t *tr_func, 856 void *arg); 857 static xpt_busfunc_t xptdefbusfunc; 858 static xpt_targetfunc_t xptdeftargetfunc; 859 static xpt_devicefunc_t xptdefdevicefunc; 860 static xpt_periphfunc_t xptdefperiphfunc; 861 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg); 862 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func, 863 void *arg); 864 static xpt_devicefunc_t xptsetasyncfunc; 865 static xpt_busfunc_t xptsetasyncbusfunc; 866 static cam_status xptregister(struct cam_periph *periph, 867 void *arg); 868 static cam_status proberegister(struct cam_periph *periph, 869 void *arg); 870 static void probeschedule(struct cam_periph *probe_periph); 871 static void probestart(struct cam_periph *periph, union ccb *start_ccb); 872 static void proberequestdefaultnegotiation(struct cam_periph *periph); 873 static int proberequestbackoff(struct cam_periph *periph, 874 struct cam_ed *device); 875 static void probedone(struct cam_periph *periph, union ccb *done_ccb); 876 static void probecleanup(struct cam_periph *periph); 877 static void xpt_find_quirk(struct cam_ed *device); 878 static void xpt_devise_transport(struct cam_path *path); 879 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts, 880 struct cam_ed *device, 881 int async_update); 882 static void xpt_toggle_tags(struct cam_path *path); 883 static void xpt_start_tags(struct cam_path *path); 884 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus, 885 struct cam_ed *dev); 886 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus, 887 struct cam_ed *dev); 888 static __inline int periph_is_queued(struct cam_periph *periph); 889 static __inline int device_is_alloc_queued(struct cam_ed *device); 890 static __inline int device_is_send_queued(struct cam_ed *device); 891 static __inline int dev_allocq_is_runnable(struct cam_devq *devq); 892 893 static __inline int 894 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev) 895 { 896 int retval; 897 898 if (dev->ccbq.devq_openings > 0) { 899 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) { 900 cam_ccbq_resize(&dev->ccbq, 901 dev->ccbq.dev_openings 902 + dev->ccbq.dev_active); 903 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED; 904 } 905 /* 906 * The priority of a device waiting for CCB resources 907 * is that of the the highest priority peripheral driver 908 * enqueued. 909 */ 910 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue, 911 &dev->alloc_ccb_entry.pinfo, 912 CAMQ_GET_HEAD(&dev->drvq)->priority); 913 } else { 914 retval = 0; 915 } 916 917 return (retval); 918 } 919 920 static __inline int 921 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev) 922 { 923 int retval; 924 925 if (dev->ccbq.dev_openings > 0) { 926 /* 927 * The priority of a device waiting for controller 928 * resources is that of the the highest priority CCB 929 * enqueued. 930 */ 931 retval = 932 xpt_schedule_dev(&bus->sim->devq->send_queue, 933 &dev->send_ccb_entry.pinfo, 934 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority); 935 } else { 936 retval = 0; 937 } 938 return (retval); 939 } 940 941 static __inline int 942 periph_is_queued(struct cam_periph *periph) 943 { 944 return (periph->pinfo.index != CAM_UNQUEUED_INDEX); 945 } 946 947 static __inline int 948 device_is_alloc_queued(struct cam_ed *device) 949 { 950 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX); 951 } 952 953 static __inline int 954 device_is_send_queued(struct cam_ed *device) 955 { 956 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX); 957 } 958 959 static __inline int 960 dev_allocq_is_runnable(struct cam_devq *devq) 961 { 962 /* 963 * Have work to do. 964 * Have space to do more work. 965 * Allowed to do work. 966 */ 967 return ((devq->alloc_queue.qfrozen_cnt == 0) 968 && (devq->alloc_queue.entries > 0) 969 && (devq->alloc_openings > 0)); 970 } 971 972 static void 973 xpt_periph_init() 974 { 975 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0"); 976 } 977 978 static void 979 probe_periph_init() 980 { 981 } 982 983 984 static void 985 xptdone(struct cam_periph *periph, union ccb *done_ccb) 986 { 987 /* Caller will release the CCB */ 988 wakeup(&done_ccb->ccb_h.cbfcnp); 989 } 990 991 static int 992 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 993 { 994 995 /* 996 * Only allow read-write access. 997 */ 998 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) 999 return(EPERM); 1000 1001 /* 1002 * We don't allow nonblocking access. 1003 */ 1004 if ((flags & O_NONBLOCK) != 0) { 1005 printf("%s: can't do nonblocking access\n", devtoname(dev)); 1006 return(ENODEV); 1007 } 1008 1009 /* Mark ourselves open */ 1010 mtx_lock(&xsoftc.xpt_lock); 1011 xsoftc.flags |= XPT_FLAG_OPEN; 1012 mtx_unlock(&xsoftc.xpt_lock); 1013 1014 return(0); 1015 } 1016 1017 static int 1018 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td) 1019 { 1020 1021 /* Mark ourselves closed */ 1022 mtx_lock(&xsoftc.xpt_lock); 1023 xsoftc.flags &= ~XPT_FLAG_OPEN; 1024 mtx_unlock(&xsoftc.xpt_lock); 1025 1026 return(0); 1027 } 1028 1029 /* 1030 * Don't automatically grab the xpt softc lock here even though this is going 1031 * through the xpt device. The xpt device is really just a back door for 1032 * accessing other devices and SIMs, so the right thing to do is to grab 1033 * the appropriate SIM lock once the bus/SIM is located. 1034 */ 1035 static int 1036 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1037 { 1038 int error; 1039 1040 error = 0; 1041 1042 switch(cmd) { 1043 /* 1044 * For the transport layer CAMIOCOMMAND ioctl, we really only want 1045 * to accept CCB types that don't quite make sense to send through a 1046 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated 1047 * in the CAM spec. 1048 */ 1049 case CAMIOCOMMAND: { 1050 union ccb *ccb; 1051 union ccb *inccb; 1052 struct cam_eb *bus; 1053 1054 inccb = (union ccb *)addr; 1055 1056 bus = xpt_find_bus(inccb->ccb_h.path_id); 1057 if (bus == NULL) { 1058 error = EINVAL; 1059 break; 1060 } 1061 1062 switch(inccb->ccb_h.func_code) { 1063 case XPT_SCAN_BUS: 1064 case XPT_RESET_BUS: 1065 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD) 1066 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) { 1067 error = EINVAL; 1068 break; 1069 } 1070 /* FALLTHROUGH */ 1071 case XPT_PATH_INQ: 1072 case XPT_ENG_INQ: 1073 case XPT_SCAN_LUN: 1074 1075 ccb = xpt_alloc_ccb(bus->sim); 1076 1077 CAM_SIM_LOCK(bus->sim); 1078 1079 /* 1080 * Create a path using the bus, target, and lun the 1081 * user passed in. 1082 */ 1083 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, 1084 inccb->ccb_h.path_id, 1085 inccb->ccb_h.target_id, 1086 inccb->ccb_h.target_lun) != 1087 CAM_REQ_CMP){ 1088 error = EINVAL; 1089 CAM_SIM_UNLOCK(bus->sim); 1090 xpt_free_ccb(ccb); 1091 break; 1092 } 1093 /* Ensure all of our fields are correct */ 1094 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 1095 inccb->ccb_h.pinfo.priority); 1096 xpt_merge_ccb(ccb, inccb); 1097 ccb->ccb_h.cbfcnp = xptdone; 1098 cam_periph_runccb(ccb, NULL, 0, 0, NULL); 1099 bcopy(ccb, inccb, sizeof(union ccb)); 1100 xpt_free_path(ccb->ccb_h.path); 1101 xpt_free_ccb(ccb); 1102 CAM_SIM_UNLOCK(bus->sim); 1103 break; 1104 1105 case XPT_DEBUG: { 1106 union ccb ccb; 1107 1108 /* 1109 * This is an immediate CCB, so it's okay to 1110 * allocate it on the stack. 1111 */ 1112 1113 CAM_SIM_LOCK(bus->sim); 1114 1115 /* 1116 * Create a path using the bus, target, and lun the 1117 * user passed in. 1118 */ 1119 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph, 1120 inccb->ccb_h.path_id, 1121 inccb->ccb_h.target_id, 1122 inccb->ccb_h.target_lun) != 1123 CAM_REQ_CMP){ 1124 error = EINVAL; 1125 break; 1126 } 1127 /* Ensure all of our fields are correct */ 1128 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path, 1129 inccb->ccb_h.pinfo.priority); 1130 xpt_merge_ccb(&ccb, inccb); 1131 ccb.ccb_h.cbfcnp = xptdone; 1132 xpt_action(&ccb); 1133 CAM_SIM_UNLOCK(bus->sim); 1134 bcopy(&ccb, inccb, sizeof(union ccb)); 1135 xpt_free_path(ccb.ccb_h.path); 1136 break; 1137 1138 } 1139 case XPT_DEV_MATCH: { 1140 struct cam_periph_map_info mapinfo; 1141 struct cam_path *old_path; 1142 1143 /* 1144 * We can't deal with physical addresses for this 1145 * type of transaction. 1146 */ 1147 if (inccb->ccb_h.flags & CAM_DATA_PHYS) { 1148 error = EINVAL; 1149 break; 1150 } 1151 1152 /* 1153 * Save this in case the caller had it set to 1154 * something in particular. 1155 */ 1156 old_path = inccb->ccb_h.path; 1157 1158 /* 1159 * We really don't need a path for the matching 1160 * code. The path is needed because of the 1161 * debugging statements in xpt_action(). They 1162 * assume that the CCB has a valid path. 1163 */ 1164 inccb->ccb_h.path = xpt_periph->path; 1165 1166 bzero(&mapinfo, sizeof(mapinfo)); 1167 1168 /* 1169 * Map the pattern and match buffers into kernel 1170 * virtual address space. 1171 */ 1172 error = cam_periph_mapmem(inccb, &mapinfo); 1173 1174 if (error) { 1175 inccb->ccb_h.path = old_path; 1176 break; 1177 } 1178 1179 /* 1180 * This is an immediate CCB, we can send it on directly. 1181 */ 1182 xpt_action(inccb); 1183 1184 /* 1185 * Map the buffers back into user space. 1186 */ 1187 cam_periph_unmapmem(inccb, &mapinfo); 1188 1189 inccb->ccb_h.path = old_path; 1190 1191 error = 0; 1192 break; 1193 } 1194 default: 1195 error = ENOTSUP; 1196 break; 1197 } 1198 break; 1199 } 1200 /* 1201 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input, 1202 * with the periphal driver name and unit name filled in. The other 1203 * fields don't really matter as input. The passthrough driver name 1204 * ("pass"), and unit number are passed back in the ccb. The current 1205 * device generation number, and the index into the device peripheral 1206 * driver list, and the status are also passed back. Note that 1207 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb, 1208 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is 1209 * (or rather should be) impossible for the device peripheral driver 1210 * list to change since we look at the whole thing in one pass, and 1211 * we do it with splcam protection. 1212 * 1213 */ 1214 case CAMGETPASSTHRU: { 1215 union ccb *ccb; 1216 struct cam_periph *periph; 1217 struct periph_driver **p_drv; 1218 char *name; 1219 u_int unit; 1220 u_int cur_generation; 1221 int base_periph_found; 1222 int splbreaknum; 1223 int s; 1224 1225 ccb = (union ccb *)addr; 1226 unit = ccb->cgdl.unit_number; 1227 name = ccb->cgdl.periph_name; 1228 /* 1229 * Every 100 devices, we want to drop our spl protection to 1230 * give the software interrupt handler a chance to run. 1231 * Most systems won't run into this check, but this should 1232 * avoid starvation in the software interrupt handler in 1233 * large systems. 1234 */ 1235 splbreaknum = 100; 1236 1237 ccb = (union ccb *)addr; 1238 1239 base_periph_found = 0; 1240 1241 /* 1242 * Sanity check -- make sure we don't get a null peripheral 1243 * driver name. 1244 */ 1245 if (*ccb->cgdl.periph_name == '\0') { 1246 error = EINVAL; 1247 break; 1248 } 1249 1250 /* Keep the list from changing while we traverse it */ 1251 s = splcam(); 1252 ptstartover: 1253 cur_generation = xsoftc.xpt_generation; 1254 1255 /* first find our driver in the list of drivers */ 1256 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) 1257 if (strcmp((*p_drv)->driver_name, name) == 0) 1258 break; 1259 1260 if (*p_drv == NULL) { 1261 splx(s); 1262 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1263 ccb->cgdl.status = CAM_GDEVLIST_ERROR; 1264 *ccb->cgdl.periph_name = '\0'; 1265 ccb->cgdl.unit_number = 0; 1266 error = ENOENT; 1267 break; 1268 } 1269 1270 /* 1271 * Run through every peripheral instance of this driver 1272 * and check to see whether it matches the unit passed 1273 * in by the user. If it does, get out of the loops and 1274 * find the passthrough driver associated with that 1275 * peripheral driver. 1276 */ 1277 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL; 1278 periph = TAILQ_NEXT(periph, unit_links)) { 1279 1280 if (periph->unit_number == unit) { 1281 break; 1282 } else if (--splbreaknum == 0) { 1283 splx(s); 1284 s = splcam(); 1285 splbreaknum = 100; 1286 if (cur_generation != xsoftc.xpt_generation) 1287 goto ptstartover; 1288 } 1289 } 1290 /* 1291 * If we found the peripheral driver that the user passed 1292 * in, go through all of the peripheral drivers for that 1293 * particular device and look for a passthrough driver. 1294 */ 1295 if (periph != NULL) { 1296 struct cam_ed *device; 1297 int i; 1298 1299 base_periph_found = 1; 1300 device = periph->path->device; 1301 for (i = 0, periph = SLIST_FIRST(&device->periphs); 1302 periph != NULL; 1303 periph = SLIST_NEXT(periph, periph_links), i++) { 1304 /* 1305 * Check to see whether we have a 1306 * passthrough device or not. 1307 */ 1308 if (strcmp(periph->periph_name, "pass") == 0) { 1309 /* 1310 * Fill in the getdevlist fields. 1311 */ 1312 strcpy(ccb->cgdl.periph_name, 1313 periph->periph_name); 1314 ccb->cgdl.unit_number = 1315 periph->unit_number; 1316 if (SLIST_NEXT(periph, periph_links)) 1317 ccb->cgdl.status = 1318 CAM_GDEVLIST_MORE_DEVS; 1319 else 1320 ccb->cgdl.status = 1321 CAM_GDEVLIST_LAST_DEVICE; 1322 ccb->cgdl.generation = 1323 device->generation; 1324 ccb->cgdl.index = i; 1325 /* 1326 * Fill in some CCB header fields 1327 * that the user may want. 1328 */ 1329 ccb->ccb_h.path_id = 1330 periph->path->bus->path_id; 1331 ccb->ccb_h.target_id = 1332 periph->path->target->target_id; 1333 ccb->ccb_h.target_lun = 1334 periph->path->device->lun_id; 1335 ccb->ccb_h.status = CAM_REQ_CMP; 1336 break; 1337 } 1338 } 1339 } 1340 1341 /* 1342 * If the periph is null here, one of two things has 1343 * happened. The first possibility is that we couldn't 1344 * find the unit number of the particular peripheral driver 1345 * that the user is asking about. e.g. the user asks for 1346 * the passthrough driver for "da11". We find the list of 1347 * "da" peripherals all right, but there is no unit 11. 1348 * The other possibility is that we went through the list 1349 * of peripheral drivers attached to the device structure, 1350 * but didn't find one with the name "pass". Either way, 1351 * we return ENOENT, since we couldn't find something. 1352 */ 1353 if (periph == NULL) { 1354 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1355 ccb->cgdl.status = CAM_GDEVLIST_ERROR; 1356 *ccb->cgdl.periph_name = '\0'; 1357 ccb->cgdl.unit_number = 0; 1358 error = ENOENT; 1359 /* 1360 * It is unfortunate that this is even necessary, 1361 * but there are many, many clueless users out there. 1362 * If this is true, the user is looking for the 1363 * passthrough driver, but doesn't have one in his 1364 * kernel. 1365 */ 1366 if (base_periph_found == 1) { 1367 printf("xptioctl: pass driver is not in the " 1368 "kernel\n"); 1369 printf("xptioctl: put \"device pass0\" in " 1370 "your kernel config file\n"); 1371 } 1372 } 1373 splx(s); 1374 break; 1375 } 1376 default: 1377 error = ENOTTY; 1378 break; 1379 } 1380 1381 return(error); 1382 } 1383 1384 static int 1385 cam_module_event_handler(module_t mod, int what, void *arg) 1386 { 1387 int error; 1388 1389 switch (what) { 1390 case MOD_LOAD: 1391 if ((error = xpt_init(NULL)) != 0) 1392 return (error); 1393 break; 1394 case MOD_UNLOAD: 1395 return EBUSY; 1396 default: 1397 return EOPNOTSUPP; 1398 } 1399 1400 return 0; 1401 } 1402 1403 /* thread to handle bus rescans */ 1404 static void 1405 xpt_scanner_thread(void *dummy) 1406 { 1407 cam_isrq_t queue; 1408 union ccb *ccb; 1409 struct cam_sim *sim; 1410 1411 for (;;) { 1412 /* 1413 * Wait for a rescan request to come in. When it does, splice 1414 * it onto a queue from local storage so that the xpt lock 1415 * doesn't need to be held while the requests are being 1416 * processed. 1417 */ 1418 xpt_lock_buses(); 1419 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO, 1420 "ccb_scanq", 0); 1421 TAILQ_INIT(&queue); 1422 TAILQ_CONCAT(&queue, &xsoftc.ccb_scanq, sim_links.tqe); 1423 xpt_unlock_buses(); 1424 1425 while ((ccb = (union ccb *)TAILQ_FIRST(&queue)) != NULL) { 1426 TAILQ_REMOVE(&queue, &ccb->ccb_h, sim_links.tqe); 1427 1428 sim = ccb->ccb_h.path->bus->sim; 1429 mtx_lock(sim->mtx); 1430 1431 ccb->ccb_h.func_code = XPT_SCAN_BUS; 1432 ccb->ccb_h.cbfcnp = xptdone; 1433 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5); 1434 cam_periph_runccb(ccb, NULL, 0, 0, NULL); 1435 xpt_free_path(ccb->ccb_h.path); 1436 xpt_free_ccb(ccb); 1437 mtx_unlock(sim->mtx); 1438 } 1439 } 1440 } 1441 1442 void 1443 xpt_rescan(union ccb *ccb) 1444 { 1445 struct ccb_hdr *hdr; 1446 1447 /* 1448 * Don't make duplicate entries for the same paths. 1449 */ 1450 xpt_lock_buses(); 1451 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) { 1452 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) { 1453 xpt_unlock_buses(); 1454 xpt_print(ccb->ccb_h.path, "rescan already queued\n"); 1455 xpt_free_path(ccb->ccb_h.path); 1456 xpt_free_ccb(ccb); 1457 return; 1458 } 1459 } 1460 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe); 1461 wakeup(&xsoftc.ccb_scanq); 1462 xpt_unlock_buses(); 1463 } 1464 1465 /* Functions accessed by the peripheral drivers */ 1466 static int 1467 xpt_init(void *dummy) 1468 { 1469 struct cam_sim *xpt_sim; 1470 struct cam_path *path; 1471 struct cam_devq *devq; 1472 cam_status status; 1473 1474 TAILQ_INIT(&xsoftc.xpt_busses); 1475 TAILQ_INIT(&cam_bioq); 1476 TAILQ_INIT(&xsoftc.ccb_scanq); 1477 STAILQ_INIT(&xsoftc.highpowerq); 1478 xsoftc.num_highpower = CAM_MAX_HIGHPOWER; 1479 1480 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF); 1481 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF); 1482 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF); 1483 1484 /* 1485 * The xpt layer is, itself, the equivelent of a SIM. 1486 * Allow 16 ccbs in the ccb pool for it. This should 1487 * give decent parallelism when we probe busses and 1488 * perform other XPT functions. 1489 */ 1490 devq = cam_simq_alloc(16); 1491 xpt_sim = cam_sim_alloc(xptaction, 1492 xptpoll, 1493 "xpt", 1494 /*softc*/NULL, 1495 /*unit*/0, 1496 /*mtx*/&xsoftc.xpt_lock, 1497 /*max_dev_transactions*/0, 1498 /*max_tagged_dev_transactions*/0, 1499 devq); 1500 if (xpt_sim == NULL) 1501 return (ENOMEM); 1502 1503 xpt_sim->max_ccbs = 16; 1504 1505 mtx_lock(&xsoftc.xpt_lock); 1506 if ((status = xpt_bus_register(xpt_sim, /*bus #*/0)) != CAM_SUCCESS) { 1507 printf("xpt_init: xpt_bus_register failed with status %#x," 1508 " failing attach\n", status); 1509 return (EINVAL); 1510 } 1511 1512 /* 1513 * Looking at the XPT from the SIM layer, the XPT is 1514 * the equivelent of a peripheral driver. Allocate 1515 * a peripheral driver entry for us. 1516 */ 1517 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID, 1518 CAM_TARGET_WILDCARD, 1519 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) { 1520 printf("xpt_init: xpt_create_path failed with status %#x," 1521 " failing attach\n", status); 1522 return (EINVAL); 1523 } 1524 1525 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO, 1526 path, NULL, 0, xpt_sim); 1527 xpt_free_path(path); 1528 mtx_unlock(&xsoftc.xpt_lock); 1529 1530 /* 1531 * Register a callback for when interrupts are enabled. 1532 */ 1533 xsoftc.xpt_config_hook = 1534 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook), 1535 M_TEMP, M_NOWAIT | M_ZERO); 1536 if (xsoftc.xpt_config_hook == NULL) { 1537 printf("xpt_init: Cannot malloc config hook " 1538 "- failing attach\n"); 1539 return (ENOMEM); 1540 } 1541 1542 xsoftc.xpt_config_hook->ich_func = xpt_config; 1543 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) { 1544 free (xsoftc.xpt_config_hook, M_TEMP); 1545 printf("xpt_init: config_intrhook_establish failed " 1546 "- failing attach\n"); 1547 } 1548 1549 /* fire up rescan thread */ 1550 if (kthread_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) { 1551 printf("xpt_init: failed to create rescan thread\n"); 1552 } 1553 /* Install our software interrupt handlers */ 1554 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih); 1555 1556 return (0); 1557 } 1558 1559 static cam_status 1560 xptregister(struct cam_periph *periph, void *arg) 1561 { 1562 struct cam_sim *xpt_sim; 1563 1564 if (periph == NULL) { 1565 printf("xptregister: periph was NULL!!\n"); 1566 return(CAM_REQ_CMP_ERR); 1567 } 1568 1569 xpt_sim = (struct cam_sim *)arg; 1570 xpt_sim->softc = periph; 1571 xpt_periph = periph; 1572 periph->softc = NULL; 1573 1574 return(CAM_REQ_CMP); 1575 } 1576 1577 int32_t 1578 xpt_add_periph(struct cam_periph *periph) 1579 { 1580 struct cam_ed *device; 1581 int32_t status; 1582 struct periph_list *periph_head; 1583 1584 mtx_assert(periph->sim->mtx, MA_OWNED); 1585 1586 device = periph->path->device; 1587 1588 periph_head = &device->periphs; 1589 1590 status = CAM_REQ_CMP; 1591 1592 if (device != NULL) { 1593 int s; 1594 1595 /* 1596 * Make room for this peripheral 1597 * so it will fit in the queue 1598 * when it's scheduled to run 1599 */ 1600 s = splsoftcam(); 1601 status = camq_resize(&device->drvq, 1602 device->drvq.array_size + 1); 1603 1604 device->generation++; 1605 1606 SLIST_INSERT_HEAD(periph_head, periph, periph_links); 1607 1608 splx(s); 1609 } 1610 1611 atomic_add_int(&xsoftc.xpt_generation, 1); 1612 1613 return (status); 1614 } 1615 1616 void 1617 xpt_remove_periph(struct cam_periph *periph) 1618 { 1619 struct cam_ed *device; 1620 1621 mtx_assert(periph->sim->mtx, MA_OWNED); 1622 1623 device = periph->path->device; 1624 1625 if (device != NULL) { 1626 int s; 1627 struct periph_list *periph_head; 1628 1629 periph_head = &device->periphs; 1630 1631 /* Release the slot for this peripheral */ 1632 s = splsoftcam(); 1633 camq_resize(&device->drvq, device->drvq.array_size - 1); 1634 1635 device->generation++; 1636 1637 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links); 1638 1639 splx(s); 1640 } 1641 1642 atomic_add_int(&xsoftc.xpt_generation, 1); 1643 1644 } 1645 1646 1647 void 1648 xpt_announce_periph(struct cam_periph *periph, char *announce_string) 1649 { 1650 struct ccb_pathinq cpi; 1651 struct ccb_trans_settings cts; 1652 struct cam_path *path; 1653 u_int speed; 1654 u_int freq; 1655 u_int mb; 1656 int s; 1657 1658 mtx_assert(periph->sim->mtx, MA_OWNED); 1659 1660 path = periph->path; 1661 /* 1662 * To ensure that this is printed in one piece, 1663 * mask out CAM interrupts. 1664 */ 1665 s = splsoftcam(); 1666 printf("%s%d at %s%d bus %d target %d lun %d\n", 1667 periph->periph_name, periph->unit_number, 1668 path->bus->sim->sim_name, 1669 path->bus->sim->unit_number, 1670 path->bus->sim->bus_id, 1671 path->target->target_id, 1672 path->device->lun_id); 1673 printf("%s%d: ", periph->periph_name, periph->unit_number); 1674 scsi_print_inquiry(&path->device->inq_data); 1675 if (bootverbose && path->device->serial_num_len > 0) { 1676 /* Don't wrap the screen - print only the first 60 chars */ 1677 printf("%s%d: Serial Number %.60s\n", periph->periph_name, 1678 periph->unit_number, path->device->serial_num); 1679 } 1680 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1); 1681 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1682 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1683 xpt_action((union ccb*)&cts); 1684 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1685 return; 1686 } 1687 1688 /* Ask the SIM for its base transfer speed */ 1689 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1); 1690 cpi.ccb_h.func_code = XPT_PATH_INQ; 1691 xpt_action((union ccb *)&cpi); 1692 1693 speed = cpi.base_transfer_speed; 1694 freq = 0; 1695 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) { 1696 struct ccb_trans_settings_spi *spi; 1697 1698 spi = &cts.xport_specific.spi; 1699 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0 1700 && spi->sync_offset != 0) { 1701 freq = scsi_calc_syncsrate(spi->sync_period); 1702 speed = freq; 1703 } 1704 1705 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) 1706 speed *= (0x01 << spi->bus_width); 1707 } 1708 1709 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) { 1710 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc; 1711 if (fc->valid & CTS_FC_VALID_SPEED) { 1712 speed = fc->bitrate; 1713 } 1714 } 1715 1716 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) { 1717 struct ccb_trans_settings_sas *sas = &cts.xport_specific.sas; 1718 if (sas->valid & CTS_SAS_VALID_SPEED) { 1719 speed = sas->bitrate; 1720 } 1721 } 1722 1723 mb = speed / 1000; 1724 if (mb > 0) 1725 printf("%s%d: %d.%03dMB/s transfers", 1726 periph->periph_name, periph->unit_number, 1727 mb, speed % 1000); 1728 else 1729 printf("%s%d: %dKB/s transfers", periph->periph_name, 1730 periph->unit_number, speed); 1731 /* Report additional information about SPI connections */ 1732 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) { 1733 struct ccb_trans_settings_spi *spi; 1734 1735 spi = &cts.xport_specific.spi; 1736 if (freq != 0) { 1737 printf(" (%d.%03dMHz%s, offset %d", freq / 1000, 1738 freq % 1000, 1739 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0 1740 ? " DT" : "", 1741 spi->sync_offset); 1742 } 1743 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0 1744 && spi->bus_width > 0) { 1745 if (freq != 0) { 1746 printf(", "); 1747 } else { 1748 printf(" ("); 1749 } 1750 printf("%dbit)", 8 * (0x01 << spi->bus_width)); 1751 } else if (freq != 0) { 1752 printf(")"); 1753 } 1754 } 1755 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) { 1756 struct ccb_trans_settings_fc *fc; 1757 1758 fc = &cts.xport_specific.fc; 1759 if (fc->valid & CTS_FC_VALID_WWNN) 1760 printf(" WWNN 0x%llx", (long long) fc->wwnn); 1761 if (fc->valid & CTS_FC_VALID_WWPN) 1762 printf(" WWPN 0x%llx", (long long) fc->wwpn); 1763 if (fc->valid & CTS_FC_VALID_PORT) 1764 printf(" PortID 0x%x", fc->port); 1765 } 1766 1767 if (path->device->inq_flags & SID_CmdQue 1768 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) { 1769 printf("\n%s%d: Command Queueing Enabled", 1770 periph->periph_name, periph->unit_number); 1771 } 1772 printf("\n"); 1773 1774 /* 1775 * We only want to print the caller's announce string if they've 1776 * passed one in.. 1777 */ 1778 if (announce_string != NULL) 1779 printf("%s%d: %s\n", periph->periph_name, 1780 periph->unit_number, announce_string); 1781 splx(s); 1782 } 1783 1784 static dev_match_ret 1785 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns, 1786 struct cam_eb *bus) 1787 { 1788 dev_match_ret retval; 1789 int i; 1790 1791 retval = DM_RET_NONE; 1792 1793 /* 1794 * If we aren't given something to match against, that's an error. 1795 */ 1796 if (bus == NULL) 1797 return(DM_RET_ERROR); 1798 1799 /* 1800 * If there are no match entries, then this bus matches no 1801 * matter what. 1802 */ 1803 if ((patterns == NULL) || (num_patterns == 0)) 1804 return(DM_RET_DESCEND | DM_RET_COPY); 1805 1806 for (i = 0; i < num_patterns; i++) { 1807 struct bus_match_pattern *cur_pattern; 1808 1809 /* 1810 * If the pattern in question isn't for a bus node, we 1811 * aren't interested. However, we do indicate to the 1812 * calling routine that we should continue descending the 1813 * tree, since the user wants to match against lower-level 1814 * EDT elements. 1815 */ 1816 if (patterns[i].type != DEV_MATCH_BUS) { 1817 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 1818 retval |= DM_RET_DESCEND; 1819 continue; 1820 } 1821 1822 cur_pattern = &patterns[i].pattern.bus_pattern; 1823 1824 /* 1825 * If they want to match any bus node, we give them any 1826 * device node. 1827 */ 1828 if (cur_pattern->flags == BUS_MATCH_ANY) { 1829 /* set the copy flag */ 1830 retval |= DM_RET_COPY; 1831 1832 /* 1833 * If we've already decided on an action, go ahead 1834 * and return. 1835 */ 1836 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE) 1837 return(retval); 1838 } 1839 1840 /* 1841 * Not sure why someone would do this... 1842 */ 1843 if (cur_pattern->flags == BUS_MATCH_NONE) 1844 continue; 1845 1846 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0) 1847 && (cur_pattern->path_id != bus->path_id)) 1848 continue; 1849 1850 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0) 1851 && (cur_pattern->bus_id != bus->sim->bus_id)) 1852 continue; 1853 1854 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0) 1855 && (cur_pattern->unit_number != bus->sim->unit_number)) 1856 continue; 1857 1858 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0) 1859 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name, 1860 DEV_IDLEN) != 0)) 1861 continue; 1862 1863 /* 1864 * If we get to this point, the user definitely wants 1865 * information on this bus. So tell the caller to copy the 1866 * data out. 1867 */ 1868 retval |= DM_RET_COPY; 1869 1870 /* 1871 * If the return action has been set to descend, then we 1872 * know that we've already seen a non-bus matching 1873 * expression, therefore we need to further descend the tree. 1874 * This won't change by continuing around the loop, so we 1875 * go ahead and return. If we haven't seen a non-bus 1876 * matching expression, we keep going around the loop until 1877 * we exhaust the matching expressions. We'll set the stop 1878 * flag once we fall out of the loop. 1879 */ 1880 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND) 1881 return(retval); 1882 } 1883 1884 /* 1885 * If the return action hasn't been set to descend yet, that means 1886 * we haven't seen anything other than bus matching patterns. So 1887 * tell the caller to stop descending the tree -- the user doesn't 1888 * want to match against lower level tree elements. 1889 */ 1890 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 1891 retval |= DM_RET_STOP; 1892 1893 return(retval); 1894 } 1895 1896 static dev_match_ret 1897 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns, 1898 struct cam_ed *device) 1899 { 1900 dev_match_ret retval; 1901 int i; 1902 1903 retval = DM_RET_NONE; 1904 1905 /* 1906 * If we aren't given something to match against, that's an error. 1907 */ 1908 if (device == NULL) 1909 return(DM_RET_ERROR); 1910 1911 /* 1912 * If there are no match entries, then this device matches no 1913 * matter what. 1914 */ 1915 if ((patterns == NULL) || (num_patterns == 0)) 1916 return(DM_RET_DESCEND | DM_RET_COPY); 1917 1918 for (i = 0; i < num_patterns; i++) { 1919 struct device_match_pattern *cur_pattern; 1920 1921 /* 1922 * If the pattern in question isn't for a device node, we 1923 * aren't interested. 1924 */ 1925 if (patterns[i].type != DEV_MATCH_DEVICE) { 1926 if ((patterns[i].type == DEV_MATCH_PERIPH) 1927 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)) 1928 retval |= DM_RET_DESCEND; 1929 continue; 1930 } 1931 1932 cur_pattern = &patterns[i].pattern.device_pattern; 1933 1934 /* 1935 * If they want to match any device node, we give them any 1936 * device node. 1937 */ 1938 if (cur_pattern->flags == DEV_MATCH_ANY) { 1939 /* set the copy flag */ 1940 retval |= DM_RET_COPY; 1941 1942 1943 /* 1944 * If we've already decided on an action, go ahead 1945 * and return. 1946 */ 1947 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE) 1948 return(retval); 1949 } 1950 1951 /* 1952 * Not sure why someone would do this... 1953 */ 1954 if (cur_pattern->flags == DEV_MATCH_NONE) 1955 continue; 1956 1957 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0) 1958 && (cur_pattern->path_id != device->target->bus->path_id)) 1959 continue; 1960 1961 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0) 1962 && (cur_pattern->target_id != device->target->target_id)) 1963 continue; 1964 1965 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0) 1966 && (cur_pattern->target_lun != device->lun_id)) 1967 continue; 1968 1969 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0) 1970 && (cam_quirkmatch((caddr_t)&device->inq_data, 1971 (caddr_t)&cur_pattern->inq_pat, 1972 1, sizeof(cur_pattern->inq_pat), 1973 scsi_static_inquiry_match) == NULL)) 1974 continue; 1975 1976 /* 1977 * If we get to this point, the user definitely wants 1978 * information on this device. So tell the caller to copy 1979 * the data out. 1980 */ 1981 retval |= DM_RET_COPY; 1982 1983 /* 1984 * If the return action has been set to descend, then we 1985 * know that we've already seen a peripheral matching 1986 * expression, therefore we need to further descend the tree. 1987 * This won't change by continuing around the loop, so we 1988 * go ahead and return. If we haven't seen a peripheral 1989 * matching expression, we keep going around the loop until 1990 * we exhaust the matching expressions. We'll set the stop 1991 * flag once we fall out of the loop. 1992 */ 1993 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND) 1994 return(retval); 1995 } 1996 1997 /* 1998 * If the return action hasn't been set to descend yet, that means 1999 * we haven't seen any peripheral matching patterns. So tell the 2000 * caller to stop descending the tree -- the user doesn't want to 2001 * match against lower level tree elements. 2002 */ 2003 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 2004 retval |= DM_RET_STOP; 2005 2006 return(retval); 2007 } 2008 2009 /* 2010 * Match a single peripheral against any number of match patterns. 2011 */ 2012 static dev_match_ret 2013 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns, 2014 struct cam_periph *periph) 2015 { 2016 dev_match_ret retval; 2017 int i; 2018 2019 /* 2020 * If we aren't given something to match against, that's an error. 2021 */ 2022 if (periph == NULL) 2023 return(DM_RET_ERROR); 2024 2025 /* 2026 * If there are no match entries, then this peripheral matches no 2027 * matter what. 2028 */ 2029 if ((patterns == NULL) || (num_patterns == 0)) 2030 return(DM_RET_STOP | DM_RET_COPY); 2031 2032 /* 2033 * There aren't any nodes below a peripheral node, so there's no 2034 * reason to descend the tree any further. 2035 */ 2036 retval = DM_RET_STOP; 2037 2038 for (i = 0; i < num_patterns; i++) { 2039 struct periph_match_pattern *cur_pattern; 2040 2041 /* 2042 * If the pattern in question isn't for a peripheral, we 2043 * aren't interested. 2044 */ 2045 if (patterns[i].type != DEV_MATCH_PERIPH) 2046 continue; 2047 2048 cur_pattern = &patterns[i].pattern.periph_pattern; 2049 2050 /* 2051 * If they want to match on anything, then we will do so. 2052 */ 2053 if (cur_pattern->flags == PERIPH_MATCH_ANY) { 2054 /* set the copy flag */ 2055 retval |= DM_RET_COPY; 2056 2057 /* 2058 * We've already set the return action to stop, 2059 * since there are no nodes below peripherals in 2060 * the tree. 2061 */ 2062 return(retval); 2063 } 2064 2065 /* 2066 * Not sure why someone would do this... 2067 */ 2068 if (cur_pattern->flags == PERIPH_MATCH_NONE) 2069 continue; 2070 2071 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0) 2072 && (cur_pattern->path_id != periph->path->bus->path_id)) 2073 continue; 2074 2075 /* 2076 * For the target and lun id's, we have to make sure the 2077 * target and lun pointers aren't NULL. The xpt peripheral 2078 * has a wildcard target and device. 2079 */ 2080 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0) 2081 && ((periph->path->target == NULL) 2082 ||(cur_pattern->target_id != periph->path->target->target_id))) 2083 continue; 2084 2085 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0) 2086 && ((periph->path->device == NULL) 2087 || (cur_pattern->target_lun != periph->path->device->lun_id))) 2088 continue; 2089 2090 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0) 2091 && (cur_pattern->unit_number != periph->unit_number)) 2092 continue; 2093 2094 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0) 2095 && (strncmp(cur_pattern->periph_name, periph->periph_name, 2096 DEV_IDLEN) != 0)) 2097 continue; 2098 2099 /* 2100 * If we get to this point, the user definitely wants 2101 * information on this peripheral. So tell the caller to 2102 * copy the data out. 2103 */ 2104 retval |= DM_RET_COPY; 2105 2106 /* 2107 * The return action has already been set to stop, since 2108 * peripherals don't have any nodes below them in the EDT. 2109 */ 2110 return(retval); 2111 } 2112 2113 /* 2114 * If we get to this point, the peripheral that was passed in 2115 * doesn't match any of the patterns. 2116 */ 2117 return(retval); 2118 } 2119 2120 static int 2121 xptedtbusfunc(struct cam_eb *bus, void *arg) 2122 { 2123 struct ccb_dev_match *cdm; 2124 dev_match_ret retval; 2125 2126 cdm = (struct ccb_dev_match *)arg; 2127 2128 /* 2129 * If our position is for something deeper in the tree, that means 2130 * that we've already seen this node. So, we keep going down. 2131 */ 2132 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2133 && (cdm->pos.cookie.bus == bus) 2134 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 2135 && (cdm->pos.cookie.target != NULL)) 2136 retval = DM_RET_DESCEND; 2137 else 2138 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus); 2139 2140 /* 2141 * If we got an error, bail out of the search. 2142 */ 2143 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 2144 cdm->status = CAM_DEV_MATCH_ERROR; 2145 return(0); 2146 } 2147 2148 /* 2149 * If the copy flag is set, copy this bus out. 2150 */ 2151 if (retval & DM_RET_COPY) { 2152 int spaceleft, j; 2153 2154 spaceleft = cdm->match_buf_len - (cdm->num_matches * 2155 sizeof(struct dev_match_result)); 2156 2157 /* 2158 * If we don't have enough space to put in another 2159 * match result, save our position and tell the 2160 * user there are more devices to check. 2161 */ 2162 if (spaceleft < sizeof(struct dev_match_result)) { 2163 bzero(&cdm->pos, sizeof(cdm->pos)); 2164 cdm->pos.position_type = 2165 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS; 2166 2167 cdm->pos.cookie.bus = bus; 2168 cdm->pos.generations[CAM_BUS_GENERATION]= 2169 xsoftc.bus_generation; 2170 cdm->status = CAM_DEV_MATCH_MORE; 2171 return(0); 2172 } 2173 j = cdm->num_matches; 2174 cdm->num_matches++; 2175 cdm->matches[j].type = DEV_MATCH_BUS; 2176 cdm->matches[j].result.bus_result.path_id = bus->path_id; 2177 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id; 2178 cdm->matches[j].result.bus_result.unit_number = 2179 bus->sim->unit_number; 2180 strncpy(cdm->matches[j].result.bus_result.dev_name, 2181 bus->sim->sim_name, DEV_IDLEN); 2182 } 2183 2184 /* 2185 * If the user is only interested in busses, there's no 2186 * reason to descend to the next level in the tree. 2187 */ 2188 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP) 2189 return(1); 2190 2191 /* 2192 * If there is a target generation recorded, check it to 2193 * make sure the target list hasn't changed. 2194 */ 2195 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2196 && (bus == cdm->pos.cookie.bus) 2197 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 2198 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0) 2199 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 2200 bus->generation)) { 2201 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 2202 return(0); 2203 } 2204 2205 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2206 && (cdm->pos.cookie.bus == bus) 2207 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 2208 && (cdm->pos.cookie.target != NULL)) 2209 return(xpttargettraverse(bus, 2210 (struct cam_et *)cdm->pos.cookie.target, 2211 xptedttargetfunc, arg)); 2212 else 2213 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg)); 2214 } 2215 2216 static int 2217 xptedttargetfunc(struct cam_et *target, void *arg) 2218 { 2219 struct ccb_dev_match *cdm; 2220 2221 cdm = (struct ccb_dev_match *)arg; 2222 2223 /* 2224 * If there is a device list generation recorded, check it to 2225 * make sure the device list hasn't changed. 2226 */ 2227 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2228 && (cdm->pos.cookie.bus == target->bus) 2229 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 2230 && (cdm->pos.cookie.target == target) 2231 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 2232 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0) 2233 && (cdm->pos.generations[CAM_DEV_GENERATION] != 2234 target->generation)) { 2235 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 2236 return(0); 2237 } 2238 2239 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2240 && (cdm->pos.cookie.bus == target->bus) 2241 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 2242 && (cdm->pos.cookie.target == target) 2243 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 2244 && (cdm->pos.cookie.device != NULL)) 2245 return(xptdevicetraverse(target, 2246 (struct cam_ed *)cdm->pos.cookie.device, 2247 xptedtdevicefunc, arg)); 2248 else 2249 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg)); 2250 } 2251 2252 static int 2253 xptedtdevicefunc(struct cam_ed *device, void *arg) 2254 { 2255 2256 struct ccb_dev_match *cdm; 2257 dev_match_ret retval; 2258 2259 cdm = (struct ccb_dev_match *)arg; 2260 2261 /* 2262 * If our position is for something deeper in the tree, that means 2263 * that we've already seen this node. So, we keep going down. 2264 */ 2265 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE) 2266 && (cdm->pos.cookie.device == device) 2267 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 2268 && (cdm->pos.cookie.periph != NULL)) 2269 retval = DM_RET_DESCEND; 2270 else 2271 retval = xptdevicematch(cdm->patterns, cdm->num_patterns, 2272 device); 2273 2274 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 2275 cdm->status = CAM_DEV_MATCH_ERROR; 2276 return(0); 2277 } 2278 2279 /* 2280 * If the copy flag is set, copy this device out. 2281 */ 2282 if (retval & DM_RET_COPY) { 2283 int spaceleft, j; 2284 2285 spaceleft = cdm->match_buf_len - (cdm->num_matches * 2286 sizeof(struct dev_match_result)); 2287 2288 /* 2289 * If we don't have enough space to put in another 2290 * match result, save our position and tell the 2291 * user there are more devices to check. 2292 */ 2293 if (spaceleft < sizeof(struct dev_match_result)) { 2294 bzero(&cdm->pos, sizeof(cdm->pos)); 2295 cdm->pos.position_type = 2296 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS | 2297 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE; 2298 2299 cdm->pos.cookie.bus = device->target->bus; 2300 cdm->pos.generations[CAM_BUS_GENERATION]= 2301 xsoftc.bus_generation; 2302 cdm->pos.cookie.target = device->target; 2303 cdm->pos.generations[CAM_TARGET_GENERATION] = 2304 device->target->bus->generation; 2305 cdm->pos.cookie.device = device; 2306 cdm->pos.generations[CAM_DEV_GENERATION] = 2307 device->target->generation; 2308 cdm->status = CAM_DEV_MATCH_MORE; 2309 return(0); 2310 } 2311 j = cdm->num_matches; 2312 cdm->num_matches++; 2313 cdm->matches[j].type = DEV_MATCH_DEVICE; 2314 cdm->matches[j].result.device_result.path_id = 2315 device->target->bus->path_id; 2316 cdm->matches[j].result.device_result.target_id = 2317 device->target->target_id; 2318 cdm->matches[j].result.device_result.target_lun = 2319 device->lun_id; 2320 bcopy(&device->inq_data, 2321 &cdm->matches[j].result.device_result.inq_data, 2322 sizeof(struct scsi_inquiry_data)); 2323 2324 /* Let the user know whether this device is unconfigured */ 2325 if (device->flags & CAM_DEV_UNCONFIGURED) 2326 cdm->matches[j].result.device_result.flags = 2327 DEV_RESULT_UNCONFIGURED; 2328 else 2329 cdm->matches[j].result.device_result.flags = 2330 DEV_RESULT_NOFLAG; 2331 } 2332 2333 /* 2334 * If the user isn't interested in peripherals, don't descend 2335 * the tree any further. 2336 */ 2337 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP) 2338 return(1); 2339 2340 /* 2341 * If there is a peripheral list generation recorded, make sure 2342 * it hasn't changed. 2343 */ 2344 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2345 && (device->target->bus == cdm->pos.cookie.bus) 2346 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 2347 && (device->target == cdm->pos.cookie.target) 2348 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 2349 && (device == cdm->pos.cookie.device) 2350 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 2351 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0) 2352 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 2353 device->generation)){ 2354 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 2355 return(0); 2356 } 2357 2358 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2359 && (cdm->pos.cookie.bus == device->target->bus) 2360 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 2361 && (cdm->pos.cookie.target == device->target) 2362 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 2363 && (cdm->pos.cookie.device == device) 2364 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 2365 && (cdm->pos.cookie.periph != NULL)) 2366 return(xptperiphtraverse(device, 2367 (struct cam_periph *)cdm->pos.cookie.periph, 2368 xptedtperiphfunc, arg)); 2369 else 2370 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg)); 2371 } 2372 2373 static int 2374 xptedtperiphfunc(struct cam_periph *periph, void *arg) 2375 { 2376 struct ccb_dev_match *cdm; 2377 dev_match_ret retval; 2378 2379 cdm = (struct ccb_dev_match *)arg; 2380 2381 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph); 2382 2383 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 2384 cdm->status = CAM_DEV_MATCH_ERROR; 2385 return(0); 2386 } 2387 2388 /* 2389 * If the copy flag is set, copy this peripheral out. 2390 */ 2391 if (retval & DM_RET_COPY) { 2392 int spaceleft, j; 2393 2394 spaceleft = cdm->match_buf_len - (cdm->num_matches * 2395 sizeof(struct dev_match_result)); 2396 2397 /* 2398 * If we don't have enough space to put in another 2399 * match result, save our position and tell the 2400 * user there are more devices to check. 2401 */ 2402 if (spaceleft < sizeof(struct dev_match_result)) { 2403 bzero(&cdm->pos, sizeof(cdm->pos)); 2404 cdm->pos.position_type = 2405 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS | 2406 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE | 2407 CAM_DEV_POS_PERIPH; 2408 2409 cdm->pos.cookie.bus = periph->path->bus; 2410 cdm->pos.generations[CAM_BUS_GENERATION]= 2411 xsoftc.bus_generation; 2412 cdm->pos.cookie.target = periph->path->target; 2413 cdm->pos.generations[CAM_TARGET_GENERATION] = 2414 periph->path->bus->generation; 2415 cdm->pos.cookie.device = periph->path->device; 2416 cdm->pos.generations[CAM_DEV_GENERATION] = 2417 periph->path->target->generation; 2418 cdm->pos.cookie.periph = periph; 2419 cdm->pos.generations[CAM_PERIPH_GENERATION] = 2420 periph->path->device->generation; 2421 cdm->status = CAM_DEV_MATCH_MORE; 2422 return(0); 2423 } 2424 2425 j = cdm->num_matches; 2426 cdm->num_matches++; 2427 cdm->matches[j].type = DEV_MATCH_PERIPH; 2428 cdm->matches[j].result.periph_result.path_id = 2429 periph->path->bus->path_id; 2430 cdm->matches[j].result.periph_result.target_id = 2431 periph->path->target->target_id; 2432 cdm->matches[j].result.periph_result.target_lun = 2433 periph->path->device->lun_id; 2434 cdm->matches[j].result.periph_result.unit_number = 2435 periph->unit_number; 2436 strncpy(cdm->matches[j].result.periph_result.periph_name, 2437 periph->periph_name, DEV_IDLEN); 2438 } 2439 2440 return(1); 2441 } 2442 2443 static int 2444 xptedtmatch(struct ccb_dev_match *cdm) 2445 { 2446 int ret; 2447 2448 cdm->num_matches = 0; 2449 2450 /* 2451 * Check the bus list generation. If it has changed, the user 2452 * needs to reset everything and start over. 2453 */ 2454 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2455 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0) 2456 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) { 2457 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 2458 return(0); 2459 } 2460 2461 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 2462 && (cdm->pos.cookie.bus != NULL)) 2463 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus, 2464 xptedtbusfunc, cdm); 2465 else 2466 ret = xptbustraverse(NULL, xptedtbusfunc, cdm); 2467 2468 /* 2469 * If we get back 0, that means that we had to stop before fully 2470 * traversing the EDT. It also means that one of the subroutines 2471 * has set the status field to the proper value. If we get back 1, 2472 * we've fully traversed the EDT and copied out any matching entries. 2473 */ 2474 if (ret == 1) 2475 cdm->status = CAM_DEV_MATCH_LAST; 2476 2477 return(ret); 2478 } 2479 2480 static int 2481 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg) 2482 { 2483 struct ccb_dev_match *cdm; 2484 2485 cdm = (struct ccb_dev_match *)arg; 2486 2487 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 2488 && (cdm->pos.cookie.pdrv == pdrv) 2489 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 2490 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0) 2491 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 2492 (*pdrv)->generation)) { 2493 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 2494 return(0); 2495 } 2496 2497 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 2498 && (cdm->pos.cookie.pdrv == pdrv) 2499 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 2500 && (cdm->pos.cookie.periph != NULL)) 2501 return(xptpdperiphtraverse(pdrv, 2502 (struct cam_periph *)cdm->pos.cookie.periph, 2503 xptplistperiphfunc, arg)); 2504 else 2505 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg)); 2506 } 2507 2508 static int 2509 xptplistperiphfunc(struct cam_periph *periph, void *arg) 2510 { 2511 struct ccb_dev_match *cdm; 2512 dev_match_ret retval; 2513 2514 cdm = (struct ccb_dev_match *)arg; 2515 2516 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph); 2517 2518 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 2519 cdm->status = CAM_DEV_MATCH_ERROR; 2520 return(0); 2521 } 2522 2523 /* 2524 * If the copy flag is set, copy this peripheral out. 2525 */ 2526 if (retval & DM_RET_COPY) { 2527 int spaceleft, j; 2528 2529 spaceleft = cdm->match_buf_len - (cdm->num_matches * 2530 sizeof(struct dev_match_result)); 2531 2532 /* 2533 * If we don't have enough space to put in another 2534 * match result, save our position and tell the 2535 * user there are more devices to check. 2536 */ 2537 if (spaceleft < sizeof(struct dev_match_result)) { 2538 struct periph_driver **pdrv; 2539 2540 pdrv = NULL; 2541 bzero(&cdm->pos, sizeof(cdm->pos)); 2542 cdm->pos.position_type = 2543 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR | 2544 CAM_DEV_POS_PERIPH; 2545 2546 /* 2547 * This may look a bit non-sensical, but it is 2548 * actually quite logical. There are very few 2549 * peripheral drivers, and bloating every peripheral 2550 * structure with a pointer back to its parent 2551 * peripheral driver linker set entry would cost 2552 * more in the long run than doing this quick lookup. 2553 */ 2554 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) { 2555 if (strcmp((*pdrv)->driver_name, 2556 periph->periph_name) == 0) 2557 break; 2558 } 2559 2560 if (*pdrv == NULL) { 2561 cdm->status = CAM_DEV_MATCH_ERROR; 2562 return(0); 2563 } 2564 2565 cdm->pos.cookie.pdrv = pdrv; 2566 /* 2567 * The periph generation slot does double duty, as 2568 * does the periph pointer slot. They are used for 2569 * both edt and pdrv lookups and positioning. 2570 */ 2571 cdm->pos.cookie.periph = periph; 2572 cdm->pos.generations[CAM_PERIPH_GENERATION] = 2573 (*pdrv)->generation; 2574 cdm->status = CAM_DEV_MATCH_MORE; 2575 return(0); 2576 } 2577 2578 j = cdm->num_matches; 2579 cdm->num_matches++; 2580 cdm->matches[j].type = DEV_MATCH_PERIPH; 2581 cdm->matches[j].result.periph_result.path_id = 2582 periph->path->bus->path_id; 2583 2584 /* 2585 * The transport layer peripheral doesn't have a target or 2586 * lun. 2587 */ 2588 if (periph->path->target) 2589 cdm->matches[j].result.periph_result.target_id = 2590 periph->path->target->target_id; 2591 else 2592 cdm->matches[j].result.periph_result.target_id = -1; 2593 2594 if (periph->path->device) 2595 cdm->matches[j].result.periph_result.target_lun = 2596 periph->path->device->lun_id; 2597 else 2598 cdm->matches[j].result.periph_result.target_lun = -1; 2599 2600 cdm->matches[j].result.periph_result.unit_number = 2601 periph->unit_number; 2602 strncpy(cdm->matches[j].result.periph_result.periph_name, 2603 periph->periph_name, DEV_IDLEN); 2604 } 2605 2606 return(1); 2607 } 2608 2609 static int 2610 xptperiphlistmatch(struct ccb_dev_match *cdm) 2611 { 2612 int ret; 2613 2614 cdm->num_matches = 0; 2615 2616 /* 2617 * At this point in the edt traversal function, we check the bus 2618 * list generation to make sure that no busses have been added or 2619 * removed since the user last sent a XPT_DEV_MATCH ccb through. 2620 * For the peripheral driver list traversal function, however, we 2621 * don't have to worry about new peripheral driver types coming or 2622 * going; they're in a linker set, and therefore can't change 2623 * without a recompile. 2624 */ 2625 2626 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 2627 && (cdm->pos.cookie.pdrv != NULL)) 2628 ret = xptpdrvtraverse( 2629 (struct periph_driver **)cdm->pos.cookie.pdrv, 2630 xptplistpdrvfunc, cdm); 2631 else 2632 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm); 2633 2634 /* 2635 * If we get back 0, that means that we had to stop before fully 2636 * traversing the peripheral driver tree. It also means that one of 2637 * the subroutines has set the status field to the proper value. If 2638 * we get back 1, we've fully traversed the EDT and copied out any 2639 * matching entries. 2640 */ 2641 if (ret == 1) 2642 cdm->status = CAM_DEV_MATCH_LAST; 2643 2644 return(ret); 2645 } 2646 2647 static int 2648 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg) 2649 { 2650 struct cam_eb *bus, *next_bus; 2651 int retval; 2652 2653 retval = 1; 2654 2655 mtx_lock(&xsoftc.xpt_topo_lock); 2656 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses)); 2657 bus != NULL; 2658 bus = next_bus) { 2659 next_bus = TAILQ_NEXT(bus, links); 2660 2661 mtx_unlock(&xsoftc.xpt_topo_lock); 2662 mtx_lock(bus->sim->mtx); 2663 retval = tr_func(bus, arg); 2664 mtx_unlock(bus->sim->mtx); 2665 if (retval == 0) 2666 return(retval); 2667 mtx_lock(&xsoftc.xpt_topo_lock); 2668 } 2669 mtx_unlock(&xsoftc.xpt_topo_lock); 2670 2671 return(retval); 2672 } 2673 2674 static int 2675 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target, 2676 xpt_targetfunc_t *tr_func, void *arg) 2677 { 2678 struct cam_et *target, *next_target; 2679 int retval; 2680 2681 retval = 1; 2682 for (target = (start_target ? start_target : 2683 TAILQ_FIRST(&bus->et_entries)); 2684 target != NULL; target = next_target) { 2685 2686 next_target = TAILQ_NEXT(target, links); 2687 2688 retval = tr_func(target, arg); 2689 2690 if (retval == 0) 2691 return(retval); 2692 } 2693 2694 return(retval); 2695 } 2696 2697 static int 2698 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device, 2699 xpt_devicefunc_t *tr_func, void *arg) 2700 { 2701 struct cam_ed *device, *next_device; 2702 int retval; 2703 2704 retval = 1; 2705 for (device = (start_device ? start_device : 2706 TAILQ_FIRST(&target->ed_entries)); 2707 device != NULL; 2708 device = next_device) { 2709 2710 next_device = TAILQ_NEXT(device, links); 2711 2712 retval = tr_func(device, arg); 2713 2714 if (retval == 0) 2715 return(retval); 2716 } 2717 2718 return(retval); 2719 } 2720 2721 static int 2722 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph, 2723 xpt_periphfunc_t *tr_func, void *arg) 2724 { 2725 struct cam_periph *periph, *next_periph; 2726 int retval; 2727 2728 retval = 1; 2729 2730 for (periph = (start_periph ? start_periph : 2731 SLIST_FIRST(&device->periphs)); 2732 periph != NULL; 2733 periph = next_periph) { 2734 2735 next_periph = SLIST_NEXT(periph, periph_links); 2736 2737 retval = tr_func(periph, arg); 2738 if (retval == 0) 2739 return(retval); 2740 } 2741 2742 return(retval); 2743 } 2744 2745 static int 2746 xptpdrvtraverse(struct periph_driver **start_pdrv, 2747 xpt_pdrvfunc_t *tr_func, void *arg) 2748 { 2749 struct periph_driver **pdrv; 2750 int retval; 2751 2752 retval = 1; 2753 2754 /* 2755 * We don't traverse the peripheral driver list like we do the 2756 * other lists, because it is a linker set, and therefore cannot be 2757 * changed during runtime. If the peripheral driver list is ever 2758 * re-done to be something other than a linker set (i.e. it can 2759 * change while the system is running), the list traversal should 2760 * be modified to work like the other traversal functions. 2761 */ 2762 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers); 2763 *pdrv != NULL; pdrv++) { 2764 retval = tr_func(pdrv, arg); 2765 2766 if (retval == 0) 2767 return(retval); 2768 } 2769 2770 return(retval); 2771 } 2772 2773 static int 2774 xptpdperiphtraverse(struct periph_driver **pdrv, 2775 struct cam_periph *start_periph, 2776 xpt_periphfunc_t *tr_func, void *arg) 2777 { 2778 struct cam_periph *periph, *next_periph; 2779 int retval; 2780 2781 retval = 1; 2782 2783 for (periph = (start_periph ? start_periph : 2784 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL; 2785 periph = next_periph) { 2786 2787 next_periph = TAILQ_NEXT(periph, unit_links); 2788 2789 retval = tr_func(periph, arg); 2790 if (retval == 0) 2791 return(retval); 2792 } 2793 return(retval); 2794 } 2795 2796 static int 2797 xptdefbusfunc(struct cam_eb *bus, void *arg) 2798 { 2799 struct xpt_traverse_config *tr_config; 2800 2801 tr_config = (struct xpt_traverse_config *)arg; 2802 2803 if (tr_config->depth == XPT_DEPTH_BUS) { 2804 xpt_busfunc_t *tr_func; 2805 2806 tr_func = (xpt_busfunc_t *)tr_config->tr_func; 2807 2808 return(tr_func(bus, tr_config->tr_arg)); 2809 } else 2810 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg)); 2811 } 2812 2813 static int 2814 xptdeftargetfunc(struct cam_et *target, void *arg) 2815 { 2816 struct xpt_traverse_config *tr_config; 2817 2818 tr_config = (struct xpt_traverse_config *)arg; 2819 2820 if (tr_config->depth == XPT_DEPTH_TARGET) { 2821 xpt_targetfunc_t *tr_func; 2822 2823 tr_func = (xpt_targetfunc_t *)tr_config->tr_func; 2824 2825 return(tr_func(target, tr_config->tr_arg)); 2826 } else 2827 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg)); 2828 } 2829 2830 static int 2831 xptdefdevicefunc(struct cam_ed *device, void *arg) 2832 { 2833 struct xpt_traverse_config *tr_config; 2834 2835 tr_config = (struct xpt_traverse_config *)arg; 2836 2837 if (tr_config->depth == XPT_DEPTH_DEVICE) { 2838 xpt_devicefunc_t *tr_func; 2839 2840 tr_func = (xpt_devicefunc_t *)tr_config->tr_func; 2841 2842 return(tr_func(device, tr_config->tr_arg)); 2843 } else 2844 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg)); 2845 } 2846 2847 static int 2848 xptdefperiphfunc(struct cam_periph *periph, void *arg) 2849 { 2850 struct xpt_traverse_config *tr_config; 2851 xpt_periphfunc_t *tr_func; 2852 2853 tr_config = (struct xpt_traverse_config *)arg; 2854 2855 tr_func = (xpt_periphfunc_t *)tr_config->tr_func; 2856 2857 /* 2858 * Unlike the other default functions, we don't check for depth 2859 * here. The peripheral driver level is the last level in the EDT, 2860 * so if we're here, we should execute the function in question. 2861 */ 2862 return(tr_func(periph, tr_config->tr_arg)); 2863 } 2864 2865 /* 2866 * Execute the given function for every bus in the EDT. 2867 */ 2868 static int 2869 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg) 2870 { 2871 struct xpt_traverse_config tr_config; 2872 2873 tr_config.depth = XPT_DEPTH_BUS; 2874 tr_config.tr_func = tr_func; 2875 tr_config.tr_arg = arg; 2876 2877 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2878 } 2879 2880 /* 2881 * Execute the given function for every device in the EDT. 2882 */ 2883 static int 2884 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg) 2885 { 2886 struct xpt_traverse_config tr_config; 2887 2888 tr_config.depth = XPT_DEPTH_DEVICE; 2889 tr_config.tr_func = tr_func; 2890 tr_config.tr_arg = arg; 2891 2892 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2893 } 2894 2895 static int 2896 xptsetasyncfunc(struct cam_ed *device, void *arg) 2897 { 2898 struct cam_path path; 2899 struct ccb_getdev cgd; 2900 struct async_node *cur_entry; 2901 2902 cur_entry = (struct async_node *)arg; 2903 2904 /* 2905 * Don't report unconfigured devices (Wildcard devs, 2906 * devices only for target mode, device instances 2907 * that have been invalidated but are waiting for 2908 * their last reference count to be released). 2909 */ 2910 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0) 2911 return (1); 2912 2913 xpt_compile_path(&path, 2914 NULL, 2915 device->target->bus->path_id, 2916 device->target->target_id, 2917 device->lun_id); 2918 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1); 2919 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 2920 xpt_action((union ccb *)&cgd); 2921 cur_entry->callback(cur_entry->callback_arg, 2922 AC_FOUND_DEVICE, 2923 &path, &cgd); 2924 xpt_release_path(&path); 2925 2926 return(1); 2927 } 2928 2929 static int 2930 xptsetasyncbusfunc(struct cam_eb *bus, void *arg) 2931 { 2932 struct cam_path path; 2933 struct ccb_pathinq cpi; 2934 struct async_node *cur_entry; 2935 2936 cur_entry = (struct async_node *)arg; 2937 2938 xpt_compile_path(&path, /*periph*/NULL, 2939 bus->sim->path_id, 2940 CAM_TARGET_WILDCARD, 2941 CAM_LUN_WILDCARD); 2942 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1); 2943 cpi.ccb_h.func_code = XPT_PATH_INQ; 2944 xpt_action((union ccb *)&cpi); 2945 cur_entry->callback(cur_entry->callback_arg, 2946 AC_PATH_REGISTERED, 2947 &path, &cpi); 2948 xpt_release_path(&path); 2949 2950 return(1); 2951 } 2952 2953 static void 2954 xpt_action_sasync_cb(void *context, int pending) 2955 { 2956 struct async_node *cur_entry; 2957 struct xpt_task *task; 2958 uint32_t added; 2959 2960 task = (struct xpt_task *)context; 2961 cur_entry = (struct async_node *)task->data1; 2962 added = task->data2; 2963 2964 if ((added & AC_FOUND_DEVICE) != 0) { 2965 /* 2966 * Get this peripheral up to date with all 2967 * the currently existing devices. 2968 */ 2969 xpt_for_all_devices(xptsetasyncfunc, cur_entry); 2970 } 2971 if ((added & AC_PATH_REGISTERED) != 0) { 2972 /* 2973 * Get this peripheral up to date with all 2974 * the currently existing busses. 2975 */ 2976 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry); 2977 } 2978 2979 free(task, M_CAMXPT); 2980 } 2981 2982 void 2983 xpt_action(union ccb *start_ccb) 2984 { 2985 int iopl; 2986 2987 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n")); 2988 2989 start_ccb->ccb_h.status = CAM_REQ_INPROG; 2990 2991 iopl = splsoftcam(); 2992 switch (start_ccb->ccb_h.func_code) { 2993 case XPT_SCSI_IO: 2994 { 2995 struct cam_ed *device; 2996 #ifdef CAMDEBUG 2997 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1]; 2998 struct cam_path *path; 2999 3000 path = start_ccb->ccb_h.path; 3001 #endif 3002 3003 /* 3004 * For the sake of compatibility with SCSI-1 3005 * devices that may not understand the identify 3006 * message, we include lun information in the 3007 * second byte of all commands. SCSI-1 specifies 3008 * that luns are a 3 bit value and reserves only 3 3009 * bits for lun information in the CDB. Later 3010 * revisions of the SCSI spec allow for more than 8 3011 * luns, but have deprecated lun information in the 3012 * CDB. So, if the lun won't fit, we must omit. 3013 * 3014 * Also be aware that during initial probing for devices, 3015 * the inquiry information is unknown but initialized to 0. 3016 * This means that this code will be exercised while probing 3017 * devices with an ANSI revision greater than 2. 3018 */ 3019 device = start_ccb->ccb_h.path->device; 3020 if (device->protocol_version <= SCSI_REV_2 3021 && start_ccb->ccb_h.target_lun < 8 3022 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) { 3023 3024 start_ccb->csio.cdb_io.cdb_bytes[1] |= 3025 start_ccb->ccb_h.target_lun << 5; 3026 } 3027 start_ccb->csio.scsi_status = SCSI_STATUS_OK; 3028 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n", 3029 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0], 3030 &path->device->inq_data), 3031 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes, 3032 cdb_str, sizeof(cdb_str)))); 3033 } 3034 /* FALLTHROUGH */ 3035 case XPT_TARGET_IO: 3036 case XPT_CONT_TARGET_IO: 3037 start_ccb->csio.sense_resid = 0; 3038 start_ccb->csio.resid = 0; 3039 /* FALLTHROUGH */ 3040 case XPT_RESET_DEV: 3041 case XPT_ENG_EXEC: 3042 { 3043 struct cam_path *path; 3044 struct cam_sim *sim; 3045 int s; 3046 int runq; 3047 3048 path = start_ccb->ccb_h.path; 3049 s = splsoftcam(); 3050 3051 sim = path->bus->sim; 3052 if (SIM_DEAD(sim)) { 3053 /* The SIM has gone; just execute the CCB directly. */ 3054 cam_ccbq_send_ccb(&path->device->ccbq, start_ccb); 3055 (*(sim->sim_action))(sim, start_ccb); 3056 splx(s); 3057 break; 3058 } 3059 3060 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb); 3061 if (path->device->qfrozen_cnt == 0) 3062 runq = xpt_schedule_dev_sendq(path->bus, path->device); 3063 else 3064 runq = 0; 3065 splx(s); 3066 if (runq != 0) 3067 xpt_run_dev_sendq(path->bus); 3068 break; 3069 } 3070 case XPT_SET_TRAN_SETTINGS: 3071 { 3072 xpt_set_transfer_settings(&start_ccb->cts, 3073 start_ccb->ccb_h.path->device, 3074 /*async_update*/FALSE); 3075 break; 3076 } 3077 case XPT_CALC_GEOMETRY: 3078 { 3079 struct cam_sim *sim; 3080 3081 /* Filter out garbage */ 3082 if (start_ccb->ccg.block_size == 0 3083 || start_ccb->ccg.volume_size == 0) { 3084 start_ccb->ccg.cylinders = 0; 3085 start_ccb->ccg.heads = 0; 3086 start_ccb->ccg.secs_per_track = 0; 3087 start_ccb->ccb_h.status = CAM_REQ_CMP; 3088 break; 3089 } 3090 #ifdef PC98 3091 /* 3092 * In a PC-98 system, geometry translation depens on 3093 * the "real" device geometry obtained from mode page 4. 3094 * SCSI geometry translation is performed in the 3095 * initialization routine of the SCSI BIOS and the result 3096 * stored in host memory. If the translation is available 3097 * in host memory, use it. If not, rely on the default 3098 * translation the device driver performs. 3099 */ 3100 if (scsi_da_bios_params(&start_ccb->ccg) != 0) { 3101 start_ccb->ccb_h.status = CAM_REQ_CMP; 3102 break; 3103 } 3104 #endif 3105 sim = start_ccb->ccb_h.path->bus->sim; 3106 (*(sim->sim_action))(sim, start_ccb); 3107 break; 3108 } 3109 case XPT_ABORT: 3110 { 3111 union ccb* abort_ccb; 3112 int s; 3113 3114 abort_ccb = start_ccb->cab.abort_ccb; 3115 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) { 3116 3117 if (abort_ccb->ccb_h.pinfo.index >= 0) { 3118 struct cam_ccbq *ccbq; 3119 3120 ccbq = &abort_ccb->ccb_h.path->device->ccbq; 3121 cam_ccbq_remove_ccb(ccbq, abort_ccb); 3122 abort_ccb->ccb_h.status = 3123 CAM_REQ_ABORTED|CAM_DEV_QFRZN; 3124 xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 3125 s = splcam(); 3126 xpt_done(abort_ccb); 3127 splx(s); 3128 start_ccb->ccb_h.status = CAM_REQ_CMP; 3129 break; 3130 } 3131 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX 3132 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) { 3133 /* 3134 * We've caught this ccb en route to 3135 * the SIM. Flag it for abort and the 3136 * SIM will do so just before starting 3137 * real work on the CCB. 3138 */ 3139 abort_ccb->ccb_h.status = 3140 CAM_REQ_ABORTED|CAM_DEV_QFRZN; 3141 xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 3142 start_ccb->ccb_h.status = CAM_REQ_CMP; 3143 break; 3144 } 3145 } 3146 if (XPT_FC_IS_QUEUED(abort_ccb) 3147 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) { 3148 /* 3149 * It's already completed but waiting 3150 * for our SWI to get to it. 3151 */ 3152 start_ccb->ccb_h.status = CAM_UA_ABORT; 3153 break; 3154 } 3155 /* 3156 * If we weren't able to take care of the abort request 3157 * in the XPT, pass the request down to the SIM for processing. 3158 */ 3159 } 3160 /* FALLTHROUGH */ 3161 case XPT_ACCEPT_TARGET_IO: 3162 case XPT_EN_LUN: 3163 case XPT_IMMED_NOTIFY: 3164 case XPT_NOTIFY_ACK: 3165 case XPT_GET_TRAN_SETTINGS: 3166 case XPT_RESET_BUS: 3167 { 3168 struct cam_sim *sim; 3169 3170 sim = start_ccb->ccb_h.path->bus->sim; 3171 (*(sim->sim_action))(sim, start_ccb); 3172 break; 3173 } 3174 case XPT_PATH_INQ: 3175 { 3176 struct cam_sim *sim; 3177 3178 sim = start_ccb->ccb_h.path->bus->sim; 3179 (*(sim->sim_action))(sim, start_ccb); 3180 break; 3181 } 3182 case XPT_PATH_STATS: 3183 start_ccb->cpis.last_reset = 3184 start_ccb->ccb_h.path->bus->last_reset; 3185 start_ccb->ccb_h.status = CAM_REQ_CMP; 3186 break; 3187 case XPT_GDEV_TYPE: 3188 { 3189 struct cam_ed *dev; 3190 int s; 3191 3192 dev = start_ccb->ccb_h.path->device; 3193 s = splcam(); 3194 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 3195 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 3196 } else { 3197 struct ccb_getdev *cgd; 3198 struct cam_eb *bus; 3199 struct cam_et *tar; 3200 3201 cgd = &start_ccb->cgd; 3202 bus = cgd->ccb_h.path->bus; 3203 tar = cgd->ccb_h.path->target; 3204 cgd->inq_data = dev->inq_data; 3205 cgd->ccb_h.status = CAM_REQ_CMP; 3206 cgd->serial_num_len = dev->serial_num_len; 3207 if ((dev->serial_num_len > 0) 3208 && (dev->serial_num != NULL)) 3209 bcopy(dev->serial_num, cgd->serial_num, 3210 dev->serial_num_len); 3211 } 3212 splx(s); 3213 break; 3214 } 3215 case XPT_GDEV_STATS: 3216 { 3217 struct cam_ed *dev; 3218 int s; 3219 3220 dev = start_ccb->ccb_h.path->device; 3221 s = splcam(); 3222 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 3223 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 3224 } else { 3225 struct ccb_getdevstats *cgds; 3226 struct cam_eb *bus; 3227 struct cam_et *tar; 3228 3229 cgds = &start_ccb->cgds; 3230 bus = cgds->ccb_h.path->bus; 3231 tar = cgds->ccb_h.path->target; 3232 cgds->dev_openings = dev->ccbq.dev_openings; 3233 cgds->dev_active = dev->ccbq.dev_active; 3234 cgds->devq_openings = dev->ccbq.devq_openings; 3235 cgds->devq_queued = dev->ccbq.queue.entries; 3236 cgds->held = dev->ccbq.held; 3237 cgds->last_reset = tar->last_reset; 3238 cgds->maxtags = dev->quirk->maxtags; 3239 cgds->mintags = dev->quirk->mintags; 3240 if (timevalcmp(&tar->last_reset, &bus->last_reset, <)) 3241 cgds->last_reset = bus->last_reset; 3242 cgds->ccb_h.status = CAM_REQ_CMP; 3243 } 3244 splx(s); 3245 break; 3246 } 3247 case XPT_GDEVLIST: 3248 { 3249 struct cam_periph *nperiph; 3250 struct periph_list *periph_head; 3251 struct ccb_getdevlist *cgdl; 3252 u_int i; 3253 int s; 3254 struct cam_ed *device; 3255 int found; 3256 3257 3258 found = 0; 3259 3260 /* 3261 * Don't want anyone mucking with our data. 3262 */ 3263 s = splcam(); 3264 device = start_ccb->ccb_h.path->device; 3265 periph_head = &device->periphs; 3266 cgdl = &start_ccb->cgdl; 3267 3268 /* 3269 * Check and see if the list has changed since the user 3270 * last requested a list member. If so, tell them that the 3271 * list has changed, and therefore they need to start over 3272 * from the beginning. 3273 */ 3274 if ((cgdl->index != 0) && 3275 (cgdl->generation != device->generation)) { 3276 cgdl->status = CAM_GDEVLIST_LIST_CHANGED; 3277 splx(s); 3278 break; 3279 } 3280 3281 /* 3282 * Traverse the list of peripherals and attempt to find 3283 * the requested peripheral. 3284 */ 3285 for (nperiph = SLIST_FIRST(periph_head), i = 0; 3286 (nperiph != NULL) && (i <= cgdl->index); 3287 nperiph = SLIST_NEXT(nperiph, periph_links), i++) { 3288 if (i == cgdl->index) { 3289 strncpy(cgdl->periph_name, 3290 nperiph->periph_name, 3291 DEV_IDLEN); 3292 cgdl->unit_number = nperiph->unit_number; 3293 found = 1; 3294 } 3295 } 3296 if (found == 0) { 3297 cgdl->status = CAM_GDEVLIST_ERROR; 3298 splx(s); 3299 break; 3300 } 3301 3302 if (nperiph == NULL) 3303 cgdl->status = CAM_GDEVLIST_LAST_DEVICE; 3304 else 3305 cgdl->status = CAM_GDEVLIST_MORE_DEVS; 3306 3307 cgdl->index++; 3308 cgdl->generation = device->generation; 3309 3310 splx(s); 3311 cgdl->ccb_h.status = CAM_REQ_CMP; 3312 break; 3313 } 3314 case XPT_DEV_MATCH: 3315 { 3316 int s; 3317 dev_pos_type position_type; 3318 struct ccb_dev_match *cdm; 3319 3320 cdm = &start_ccb->cdm; 3321 3322 /* 3323 * Prevent EDT changes while we traverse it. 3324 */ 3325 s = splcam(); 3326 /* 3327 * There are two ways of getting at information in the EDT. 3328 * The first way is via the primary EDT tree. It starts 3329 * with a list of busses, then a list of targets on a bus, 3330 * then devices/luns on a target, and then peripherals on a 3331 * device/lun. The "other" way is by the peripheral driver 3332 * lists. The peripheral driver lists are organized by 3333 * peripheral driver. (obviously) So it makes sense to 3334 * use the peripheral driver list if the user is looking 3335 * for something like "da1", or all "da" devices. If the 3336 * user is looking for something on a particular bus/target 3337 * or lun, it's generally better to go through the EDT tree. 3338 */ 3339 3340 if (cdm->pos.position_type != CAM_DEV_POS_NONE) 3341 position_type = cdm->pos.position_type; 3342 else { 3343 u_int i; 3344 3345 position_type = CAM_DEV_POS_NONE; 3346 3347 for (i = 0; i < cdm->num_patterns; i++) { 3348 if ((cdm->patterns[i].type == DEV_MATCH_BUS) 3349 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){ 3350 position_type = CAM_DEV_POS_EDT; 3351 break; 3352 } 3353 } 3354 3355 if (cdm->num_patterns == 0) 3356 position_type = CAM_DEV_POS_EDT; 3357 else if (position_type == CAM_DEV_POS_NONE) 3358 position_type = CAM_DEV_POS_PDRV; 3359 } 3360 3361 switch(position_type & CAM_DEV_POS_TYPEMASK) { 3362 case CAM_DEV_POS_EDT: 3363 xptedtmatch(cdm); 3364 break; 3365 case CAM_DEV_POS_PDRV: 3366 xptperiphlistmatch(cdm); 3367 break; 3368 default: 3369 cdm->status = CAM_DEV_MATCH_ERROR; 3370 break; 3371 } 3372 3373 splx(s); 3374 3375 if (cdm->status == CAM_DEV_MATCH_ERROR) 3376 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR; 3377 else 3378 start_ccb->ccb_h.status = CAM_REQ_CMP; 3379 3380 break; 3381 } 3382 case XPT_SASYNC_CB: 3383 { 3384 struct ccb_setasync *csa; 3385 struct async_node *cur_entry; 3386 struct async_list *async_head; 3387 u_int32_t added; 3388 3389 csa = &start_ccb->csa; 3390 added = csa->event_enable; 3391 async_head = &csa->ccb_h.path->device->asyncs; 3392 3393 /* 3394 * If there is already an entry for us, simply 3395 * update it. 3396 */ 3397 cur_entry = SLIST_FIRST(async_head); 3398 while (cur_entry != NULL) { 3399 if ((cur_entry->callback_arg == csa->callback_arg) 3400 && (cur_entry->callback == csa->callback)) 3401 break; 3402 cur_entry = SLIST_NEXT(cur_entry, links); 3403 } 3404 3405 if (cur_entry != NULL) { 3406 /* 3407 * If the request has no flags set, 3408 * remove the entry. 3409 */ 3410 added &= ~cur_entry->event_enable; 3411 if (csa->event_enable == 0) { 3412 SLIST_REMOVE(async_head, cur_entry, 3413 async_node, links); 3414 csa->ccb_h.path->device->refcount--; 3415 free(cur_entry, M_CAMXPT); 3416 } else { 3417 cur_entry->event_enable = csa->event_enable; 3418 } 3419 } else { 3420 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT, 3421 M_NOWAIT); 3422 if (cur_entry == NULL) { 3423 csa->ccb_h.status = CAM_RESRC_UNAVAIL; 3424 break; 3425 } 3426 cur_entry->event_enable = csa->event_enable; 3427 cur_entry->callback_arg = csa->callback_arg; 3428 cur_entry->callback = csa->callback; 3429 SLIST_INSERT_HEAD(async_head, cur_entry, links); 3430 csa->ccb_h.path->device->refcount++; 3431 } 3432 3433 /* 3434 * Need to decouple this operation via a taqskqueue so that 3435 * the locking doesn't become a mess. 3436 */ 3437 if ((added & (AC_FOUND_DEVICE | AC_PATH_REGISTERED)) != 0) { 3438 struct xpt_task *task; 3439 3440 task = malloc(sizeof(struct xpt_task), M_CAMXPT, 3441 M_NOWAIT); 3442 if (task == NULL) { 3443 csa->ccb_h.status = CAM_RESRC_UNAVAIL; 3444 break; 3445 } 3446 3447 TASK_INIT(&task->task, 0, xpt_action_sasync_cb, task); 3448 task->data1 = cur_entry; 3449 task->data2 = added; 3450 taskqueue_enqueue(taskqueue_thread, &task->task); 3451 } 3452 3453 start_ccb->ccb_h.status = CAM_REQ_CMP; 3454 break; 3455 } 3456 case XPT_REL_SIMQ: 3457 { 3458 struct ccb_relsim *crs; 3459 struct cam_ed *dev; 3460 int s; 3461 3462 crs = &start_ccb->crs; 3463 dev = crs->ccb_h.path->device; 3464 if (dev == NULL) { 3465 3466 crs->ccb_h.status = CAM_DEV_NOT_THERE; 3467 break; 3468 } 3469 3470 s = splcam(); 3471 3472 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) { 3473 3474 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) { 3475 /* Don't ever go below one opening */ 3476 if (crs->openings > 0) { 3477 xpt_dev_ccbq_resize(crs->ccb_h.path, 3478 crs->openings); 3479 3480 if (bootverbose) { 3481 xpt_print(crs->ccb_h.path, 3482 "tagged openings now %d\n", 3483 crs->openings); 3484 } 3485 } 3486 } 3487 } 3488 3489 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) { 3490 3491 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 3492 3493 /* 3494 * Just extend the old timeout and decrement 3495 * the freeze count so that a single timeout 3496 * is sufficient for releasing the queue. 3497 */ 3498 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 3499 callout_stop(&dev->callout); 3500 } else { 3501 3502 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 3503 } 3504 3505 callout_reset(&dev->callout, 3506 (crs->release_timeout * hz) / 1000, 3507 xpt_release_devq_timeout, dev); 3508 3509 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING; 3510 3511 } 3512 3513 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) { 3514 3515 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) { 3516 /* 3517 * Decrement the freeze count so that a single 3518 * completion is still sufficient to unfreeze 3519 * the queue. 3520 */ 3521 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 3522 } else { 3523 3524 dev->flags |= CAM_DEV_REL_ON_COMPLETE; 3525 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 3526 } 3527 } 3528 3529 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) { 3530 3531 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0 3532 || (dev->ccbq.dev_active == 0)) { 3533 3534 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 3535 } else { 3536 3537 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY; 3538 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 3539 } 3540 } 3541 splx(s); 3542 3543 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) { 3544 3545 xpt_release_devq(crs->ccb_h.path, /*count*/1, 3546 /*run_queue*/TRUE); 3547 } 3548 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt; 3549 start_ccb->ccb_h.status = CAM_REQ_CMP; 3550 break; 3551 } 3552 case XPT_SCAN_BUS: 3553 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb); 3554 break; 3555 case XPT_SCAN_LUN: 3556 xpt_scan_lun(start_ccb->ccb_h.path->periph, 3557 start_ccb->ccb_h.path, start_ccb->crcn.flags, 3558 start_ccb); 3559 break; 3560 case XPT_DEBUG: { 3561 #ifdef CAMDEBUG 3562 int s; 3563 3564 s = splcam(); 3565 #ifdef CAM_DEBUG_DELAY 3566 cam_debug_delay = CAM_DEBUG_DELAY; 3567 #endif 3568 cam_dflags = start_ccb->cdbg.flags; 3569 if (cam_dpath != NULL) { 3570 xpt_free_path(cam_dpath); 3571 cam_dpath = NULL; 3572 } 3573 3574 if (cam_dflags != CAM_DEBUG_NONE) { 3575 if (xpt_create_path(&cam_dpath, xpt_periph, 3576 start_ccb->ccb_h.path_id, 3577 start_ccb->ccb_h.target_id, 3578 start_ccb->ccb_h.target_lun) != 3579 CAM_REQ_CMP) { 3580 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 3581 cam_dflags = CAM_DEBUG_NONE; 3582 } else { 3583 start_ccb->ccb_h.status = CAM_REQ_CMP; 3584 xpt_print(cam_dpath, "debugging flags now %x\n", 3585 cam_dflags); 3586 } 3587 } else { 3588 cam_dpath = NULL; 3589 start_ccb->ccb_h.status = CAM_REQ_CMP; 3590 } 3591 splx(s); 3592 #else /* !CAMDEBUG */ 3593 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 3594 #endif /* CAMDEBUG */ 3595 break; 3596 } 3597 case XPT_NOOP: 3598 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) 3599 xpt_freeze_devq(start_ccb->ccb_h.path, 1); 3600 start_ccb->ccb_h.status = CAM_REQ_CMP; 3601 break; 3602 default: 3603 case XPT_SDEV_TYPE: 3604 case XPT_TERM_IO: 3605 case XPT_ENG_INQ: 3606 /* XXX Implement */ 3607 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL; 3608 break; 3609 } 3610 splx(iopl); 3611 } 3612 3613 void 3614 xpt_polled_action(union ccb *start_ccb) 3615 { 3616 int s; 3617 u_int32_t timeout; 3618 struct cam_sim *sim; 3619 struct cam_devq *devq; 3620 struct cam_ed *dev; 3621 3622 3623 timeout = start_ccb->ccb_h.timeout; 3624 sim = start_ccb->ccb_h.path->bus->sim; 3625 devq = sim->devq; 3626 dev = start_ccb->ccb_h.path->device; 3627 3628 mtx_assert(sim->mtx, MA_OWNED); 3629 s = splcam(); 3630 3631 /* 3632 * Steal an opening so that no other queued requests 3633 * can get it before us while we simulate interrupts. 3634 */ 3635 dev->ccbq.devq_openings--; 3636 dev->ccbq.dev_openings--; 3637 3638 while(((devq != NULL && devq->send_openings <= 0) || 3639 dev->ccbq.dev_openings < 0) && (--timeout > 0)) { 3640 DELAY(1000); 3641 (*(sim->sim_poll))(sim); 3642 camisr(&cam_bioq); 3643 } 3644 3645 dev->ccbq.devq_openings++; 3646 dev->ccbq.dev_openings++; 3647 3648 if (timeout != 0) { 3649 xpt_action(start_ccb); 3650 while(--timeout > 0) { 3651 (*(sim->sim_poll))(sim); 3652 camisr(&cam_bioq); 3653 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK) 3654 != CAM_REQ_INPROG) 3655 break; 3656 DELAY(1000); 3657 } 3658 if (timeout == 0) { 3659 /* 3660 * XXX Is it worth adding a sim_timeout entry 3661 * point so we can attempt recovery? If 3662 * this is only used for dumps, I don't think 3663 * it is. 3664 */ 3665 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT; 3666 } 3667 } else { 3668 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 3669 } 3670 splx(s); 3671 } 3672 3673 /* 3674 * Schedule a peripheral driver to receive a ccb when it's 3675 * target device has space for more transactions. 3676 */ 3677 void 3678 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority) 3679 { 3680 struct cam_ed *device; 3681 union ccb *work_ccb; 3682 int s; 3683 int runq; 3684 3685 mtx_assert(perph->sim->mtx, MA_OWNED); 3686 3687 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n")); 3688 device = perph->path->device; 3689 s = splsoftcam(); 3690 if (periph_is_queued(perph)) { 3691 /* Simply reorder based on new priority */ 3692 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 3693 (" change priority to %d\n", new_priority)); 3694 if (new_priority < perph->pinfo.priority) { 3695 camq_change_priority(&device->drvq, 3696 perph->pinfo.index, 3697 new_priority); 3698 } 3699 runq = 0; 3700 } else if (SIM_DEAD(perph->path->bus->sim)) { 3701 /* The SIM is gone so just call periph_start directly. */ 3702 work_ccb = xpt_get_ccb(perph->path->device); 3703 splx(s); 3704 if (work_ccb == NULL) 3705 return; /* XXX */ 3706 xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority); 3707 perph->pinfo.priority = new_priority; 3708 perph->periph_start(perph, work_ccb); 3709 return; 3710 } else { 3711 /* New entry on the queue */ 3712 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 3713 (" added periph to queue\n")); 3714 perph->pinfo.priority = new_priority; 3715 perph->pinfo.generation = ++device->drvq.generation; 3716 camq_insert(&device->drvq, &perph->pinfo); 3717 runq = xpt_schedule_dev_allocq(perph->path->bus, device); 3718 } 3719 splx(s); 3720 if (runq != 0) { 3721 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 3722 (" calling xpt_run_devq\n")); 3723 xpt_run_dev_allocq(perph->path->bus); 3724 } 3725 } 3726 3727 3728 /* 3729 * Schedule a device to run on a given queue. 3730 * If the device was inserted as a new entry on the queue, 3731 * return 1 meaning the device queue should be run. If we 3732 * were already queued, implying someone else has already 3733 * started the queue, return 0 so the caller doesn't attempt 3734 * to run the queue. Must be run at either splsoftcam 3735 * (or splcam since that encompases splsoftcam). 3736 */ 3737 static int 3738 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo, 3739 u_int32_t new_priority) 3740 { 3741 int retval; 3742 u_int32_t old_priority; 3743 3744 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n")); 3745 3746 old_priority = pinfo->priority; 3747 3748 /* 3749 * Are we already queued? 3750 */ 3751 if (pinfo->index != CAM_UNQUEUED_INDEX) { 3752 /* Simply reorder based on new priority */ 3753 if (new_priority < old_priority) { 3754 camq_change_priority(queue, pinfo->index, 3755 new_priority); 3756 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3757 ("changed priority to %d\n", 3758 new_priority)); 3759 } 3760 retval = 0; 3761 } else { 3762 /* New entry on the queue */ 3763 if (new_priority < old_priority) 3764 pinfo->priority = new_priority; 3765 3766 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3767 ("Inserting onto queue\n")); 3768 pinfo->generation = ++queue->generation; 3769 camq_insert(queue, pinfo); 3770 retval = 1; 3771 } 3772 return (retval); 3773 } 3774 3775 static void 3776 xpt_run_dev_allocq(struct cam_eb *bus) 3777 { 3778 struct cam_devq *devq; 3779 int s; 3780 3781 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n")); 3782 devq = bus->sim->devq; 3783 3784 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3785 (" qfrozen_cnt == 0x%x, entries == %d, " 3786 "openings == %d, active == %d\n", 3787 devq->alloc_queue.qfrozen_cnt, 3788 devq->alloc_queue.entries, 3789 devq->alloc_openings, 3790 devq->alloc_active)); 3791 3792 s = splsoftcam(); 3793 devq->alloc_queue.qfrozen_cnt++; 3794 while ((devq->alloc_queue.entries > 0) 3795 && (devq->alloc_openings > 0) 3796 && (devq->alloc_queue.qfrozen_cnt <= 1)) { 3797 struct cam_ed_qinfo *qinfo; 3798 struct cam_ed *device; 3799 union ccb *work_ccb; 3800 struct cam_periph *drv; 3801 struct camq *drvq; 3802 3803 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue, 3804 CAMQ_HEAD); 3805 device = qinfo->device; 3806 3807 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3808 ("running device %p\n", device)); 3809 3810 drvq = &device->drvq; 3811 3812 #ifdef CAMDEBUG 3813 if (drvq->entries <= 0) { 3814 panic("xpt_run_dev_allocq: " 3815 "Device on queue without any work to do"); 3816 } 3817 #endif 3818 if ((work_ccb = xpt_get_ccb(device)) != NULL) { 3819 devq->alloc_openings--; 3820 devq->alloc_active++; 3821 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD); 3822 splx(s); 3823 xpt_setup_ccb(&work_ccb->ccb_h, drv->path, 3824 drv->pinfo.priority); 3825 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3826 ("calling periph start\n")); 3827 drv->periph_start(drv, work_ccb); 3828 } else { 3829 /* 3830 * Malloc failure in alloc_ccb 3831 */ 3832 /* 3833 * XXX add us to a list to be run from free_ccb 3834 * if we don't have any ccbs active on this 3835 * device queue otherwise we may never get run 3836 * again. 3837 */ 3838 break; 3839 } 3840 3841 /* Raise IPL for possible insertion and test at top of loop */ 3842 s = splsoftcam(); 3843 3844 if (drvq->entries > 0) { 3845 /* We have more work. Attempt to reschedule */ 3846 xpt_schedule_dev_allocq(bus, device); 3847 } 3848 } 3849 devq->alloc_queue.qfrozen_cnt--; 3850 splx(s); 3851 } 3852 3853 static void 3854 xpt_run_dev_sendq(struct cam_eb *bus) 3855 { 3856 struct cam_devq *devq; 3857 int s; 3858 3859 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n")); 3860 3861 devq = bus->sim->devq; 3862 3863 s = splcam(); 3864 devq->send_queue.qfrozen_cnt++; 3865 splx(s); 3866 s = splsoftcam(); 3867 while ((devq->send_queue.entries > 0) 3868 && (devq->send_openings > 0)) { 3869 struct cam_ed_qinfo *qinfo; 3870 struct cam_ed *device; 3871 union ccb *work_ccb; 3872 struct cam_sim *sim; 3873 int ospl; 3874 3875 ospl = splcam(); 3876 if (devq->send_queue.qfrozen_cnt > 1) { 3877 splx(ospl); 3878 break; 3879 } 3880 3881 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue, 3882 CAMQ_HEAD); 3883 device = qinfo->device; 3884 3885 /* 3886 * If the device has been "frozen", don't attempt 3887 * to run it. 3888 */ 3889 if (device->qfrozen_cnt > 0) { 3890 splx(ospl); 3891 continue; 3892 } 3893 3894 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3895 ("running device %p\n", device)); 3896 3897 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD); 3898 if (work_ccb == NULL) { 3899 printf("device on run queue with no ccbs???\n"); 3900 splx(ospl); 3901 continue; 3902 } 3903 3904 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) { 3905 3906 mtx_lock(&xsoftc.xpt_lock); 3907 if (xsoftc.num_highpower <= 0) { 3908 /* 3909 * We got a high power command, but we 3910 * don't have any available slots. Freeze 3911 * the device queue until we have a slot 3912 * available. 3913 */ 3914 device->qfrozen_cnt++; 3915 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, 3916 &work_ccb->ccb_h, 3917 xpt_links.stqe); 3918 3919 splx(ospl); 3920 continue; 3921 } else { 3922 /* 3923 * Consume a high power slot while 3924 * this ccb runs. 3925 */ 3926 xsoftc.num_highpower--; 3927 } 3928 mtx_unlock(&xsoftc.xpt_lock); 3929 } 3930 devq->active_dev = device; 3931 cam_ccbq_remove_ccb(&device->ccbq, work_ccb); 3932 3933 cam_ccbq_send_ccb(&device->ccbq, work_ccb); 3934 splx(ospl); 3935 3936 devq->send_openings--; 3937 devq->send_active++; 3938 3939 if (device->ccbq.queue.entries > 0) 3940 xpt_schedule_dev_sendq(bus, device); 3941 3942 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){ 3943 /* 3944 * The client wants to freeze the queue 3945 * after this CCB is sent. 3946 */ 3947 ospl = splcam(); 3948 device->qfrozen_cnt++; 3949 splx(ospl); 3950 } 3951 3952 splx(s); 3953 3954 /* In Target mode, the peripheral driver knows best... */ 3955 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) { 3956 if ((device->inq_flags & SID_CmdQue) != 0 3957 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE) 3958 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID; 3959 else 3960 /* 3961 * Clear this in case of a retried CCB that 3962 * failed due to a rejected tag. 3963 */ 3964 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 3965 } 3966 3967 /* 3968 * Device queues can be shared among multiple sim instances 3969 * that reside on different busses. Use the SIM in the queue 3970 * CCB's path, rather than the one in the bus that was passed 3971 * into this function. 3972 */ 3973 sim = work_ccb->ccb_h.path->bus->sim; 3974 (*(sim->sim_action))(sim, work_ccb); 3975 3976 ospl = splcam(); 3977 devq->active_dev = NULL; 3978 splx(ospl); 3979 /* Raise IPL for possible insertion and test at top of loop */ 3980 s = splsoftcam(); 3981 } 3982 splx(s); 3983 s = splcam(); 3984 devq->send_queue.qfrozen_cnt--; 3985 splx(s); 3986 } 3987 3988 /* 3989 * This function merges stuff from the slave ccb into the master ccb, while 3990 * keeping important fields in the master ccb constant. 3991 */ 3992 void 3993 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb) 3994 { 3995 3996 /* 3997 * Pull fields that are valid for peripheral drivers to set 3998 * into the master CCB along with the CCB "payload". 3999 */ 4000 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count; 4001 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code; 4002 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout; 4003 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags; 4004 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1], 4005 sizeof(union ccb) - sizeof(struct ccb_hdr)); 4006 } 4007 4008 void 4009 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority) 4010 { 4011 4012 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n")); 4013 ccb_h->pinfo.priority = priority; 4014 ccb_h->path = path; 4015 ccb_h->path_id = path->bus->path_id; 4016 if (path->target) 4017 ccb_h->target_id = path->target->target_id; 4018 else 4019 ccb_h->target_id = CAM_TARGET_WILDCARD; 4020 if (path->device) { 4021 ccb_h->target_lun = path->device->lun_id; 4022 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation; 4023 } else { 4024 ccb_h->target_lun = CAM_TARGET_WILDCARD; 4025 } 4026 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX; 4027 ccb_h->flags = 0; 4028 } 4029 4030 /* Path manipulation functions */ 4031 cam_status 4032 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph, 4033 path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 4034 { 4035 struct cam_path *path; 4036 cam_status status; 4037 4038 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT); 4039 4040 if (path == NULL) { 4041 status = CAM_RESRC_UNAVAIL; 4042 return(status); 4043 } 4044 status = xpt_compile_path(path, perph, path_id, target_id, lun_id); 4045 if (status != CAM_REQ_CMP) { 4046 free(path, M_CAMXPT); 4047 path = NULL; 4048 } 4049 *new_path_ptr = path; 4050 return (status); 4051 } 4052 4053 cam_status 4054 xpt_create_path_unlocked(struct cam_path **new_path_ptr, 4055 struct cam_periph *periph, path_id_t path_id, 4056 target_id_t target_id, lun_id_t lun_id) 4057 { 4058 struct cam_path *path; 4059 struct cam_eb *bus = NULL; 4060 cam_status status; 4061 int need_unlock = 0; 4062 4063 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK); 4064 4065 if (path_id != CAM_BUS_WILDCARD) { 4066 bus = xpt_find_bus(path_id); 4067 if (bus != NULL) { 4068 need_unlock = 1; 4069 mtx_lock(bus->sim->mtx); 4070 } 4071 } 4072 status = xpt_compile_path(path, periph, path_id, target_id, lun_id); 4073 if (need_unlock) 4074 mtx_unlock(bus->sim->mtx); 4075 if (status != CAM_REQ_CMP) { 4076 free(path, M_CAMXPT); 4077 path = NULL; 4078 } 4079 *new_path_ptr = path; 4080 return (status); 4081 } 4082 4083 static cam_status 4084 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph, 4085 path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 4086 { 4087 struct cam_eb *bus; 4088 struct cam_et *target; 4089 struct cam_ed *device; 4090 cam_status status; 4091 int s; 4092 4093 status = CAM_REQ_CMP; /* Completed without error */ 4094 target = NULL; /* Wildcarded */ 4095 device = NULL; /* Wildcarded */ 4096 4097 /* 4098 * We will potentially modify the EDT, so block interrupts 4099 * that may attempt to create cam paths. 4100 */ 4101 s = splcam(); 4102 bus = xpt_find_bus(path_id); 4103 if (bus == NULL) { 4104 status = CAM_PATH_INVALID; 4105 } else { 4106 target = xpt_find_target(bus, target_id); 4107 if (target == NULL) { 4108 /* Create one */ 4109 struct cam_et *new_target; 4110 4111 new_target = xpt_alloc_target(bus, target_id); 4112 if (new_target == NULL) { 4113 status = CAM_RESRC_UNAVAIL; 4114 } else { 4115 target = new_target; 4116 } 4117 } 4118 if (target != NULL) { 4119 device = xpt_find_device(target, lun_id); 4120 if (device == NULL) { 4121 /* Create one */ 4122 struct cam_ed *new_device; 4123 4124 new_device = xpt_alloc_device(bus, 4125 target, 4126 lun_id); 4127 if (new_device == NULL) { 4128 status = CAM_RESRC_UNAVAIL; 4129 } else { 4130 device = new_device; 4131 } 4132 } 4133 } 4134 } 4135 splx(s); 4136 4137 /* 4138 * Only touch the user's data if we are successful. 4139 */ 4140 if (status == CAM_REQ_CMP) { 4141 new_path->periph = perph; 4142 new_path->bus = bus; 4143 new_path->target = target; 4144 new_path->device = device; 4145 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n")); 4146 } else { 4147 if (device != NULL) 4148 xpt_release_device(bus, target, device); 4149 if (target != NULL) 4150 xpt_release_target(bus, target); 4151 if (bus != NULL) 4152 xpt_release_bus(bus); 4153 } 4154 return (status); 4155 } 4156 4157 static void 4158 xpt_release_path(struct cam_path *path) 4159 { 4160 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n")); 4161 if (path->device != NULL) { 4162 xpt_release_device(path->bus, path->target, path->device); 4163 path->device = NULL; 4164 } 4165 if (path->target != NULL) { 4166 xpt_release_target(path->bus, path->target); 4167 path->target = NULL; 4168 } 4169 if (path->bus != NULL) { 4170 xpt_release_bus(path->bus); 4171 path->bus = NULL; 4172 } 4173 } 4174 4175 void 4176 xpt_free_path(struct cam_path *path) 4177 { 4178 4179 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n")); 4180 xpt_release_path(path); 4181 free(path, M_CAMXPT); 4182 } 4183 4184 4185 /* 4186 * Return -1 for failure, 0 for exact match, 1 for match with wildcards 4187 * in path1, 2 for match with wildcards in path2. 4188 */ 4189 int 4190 xpt_path_comp(struct cam_path *path1, struct cam_path *path2) 4191 { 4192 int retval = 0; 4193 4194 if (path1->bus != path2->bus) { 4195 if (path1->bus->path_id == CAM_BUS_WILDCARD) 4196 retval = 1; 4197 else if (path2->bus->path_id == CAM_BUS_WILDCARD) 4198 retval = 2; 4199 else 4200 return (-1); 4201 } 4202 if (path1->target != path2->target) { 4203 if (path1->target->target_id == CAM_TARGET_WILDCARD) { 4204 if (retval == 0) 4205 retval = 1; 4206 } else if (path2->target->target_id == CAM_TARGET_WILDCARD) 4207 retval = 2; 4208 else 4209 return (-1); 4210 } 4211 if (path1->device != path2->device) { 4212 if (path1->device->lun_id == CAM_LUN_WILDCARD) { 4213 if (retval == 0) 4214 retval = 1; 4215 } else if (path2->device->lun_id == CAM_LUN_WILDCARD) 4216 retval = 2; 4217 else 4218 return (-1); 4219 } 4220 return (retval); 4221 } 4222 4223 void 4224 xpt_print_path(struct cam_path *path) 4225 { 4226 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4227 4228 if (path == NULL) 4229 printf("(nopath): "); 4230 else { 4231 if (path->periph != NULL) 4232 printf("(%s%d:", path->periph->periph_name, 4233 path->periph->unit_number); 4234 else 4235 printf("(noperiph:"); 4236 4237 if (path->bus != NULL) 4238 printf("%s%d:%d:", path->bus->sim->sim_name, 4239 path->bus->sim->unit_number, 4240 path->bus->sim->bus_id); 4241 else 4242 printf("nobus:"); 4243 4244 if (path->target != NULL) 4245 printf("%d:", path->target->target_id); 4246 else 4247 printf("X:"); 4248 4249 if (path->device != NULL) 4250 printf("%d): ", path->device->lun_id); 4251 else 4252 printf("X): "); 4253 } 4254 } 4255 4256 void 4257 xpt_print(struct cam_path *path, const char *fmt, ...) 4258 { 4259 va_list ap; 4260 xpt_print_path(path); 4261 va_start(ap, fmt); 4262 vprintf(fmt, ap); 4263 va_end(ap); 4264 } 4265 4266 int 4267 xpt_path_string(struct cam_path *path, char *str, size_t str_len) 4268 { 4269 struct sbuf sb; 4270 4271 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4272 4273 sbuf_new(&sb, str, str_len, 0); 4274 4275 if (path == NULL) 4276 sbuf_printf(&sb, "(nopath): "); 4277 else { 4278 if (path->periph != NULL) 4279 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name, 4280 path->periph->unit_number); 4281 else 4282 sbuf_printf(&sb, "(noperiph:"); 4283 4284 if (path->bus != NULL) 4285 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name, 4286 path->bus->sim->unit_number, 4287 path->bus->sim->bus_id); 4288 else 4289 sbuf_printf(&sb, "nobus:"); 4290 4291 if (path->target != NULL) 4292 sbuf_printf(&sb, "%d:", path->target->target_id); 4293 else 4294 sbuf_printf(&sb, "X:"); 4295 4296 if (path->device != NULL) 4297 sbuf_printf(&sb, "%d): ", path->device->lun_id); 4298 else 4299 sbuf_printf(&sb, "X): "); 4300 } 4301 sbuf_finish(&sb); 4302 4303 return(sbuf_len(&sb)); 4304 } 4305 4306 path_id_t 4307 xpt_path_path_id(struct cam_path *path) 4308 { 4309 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4310 4311 return(path->bus->path_id); 4312 } 4313 4314 target_id_t 4315 xpt_path_target_id(struct cam_path *path) 4316 { 4317 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4318 4319 if (path->target != NULL) 4320 return (path->target->target_id); 4321 else 4322 return (CAM_TARGET_WILDCARD); 4323 } 4324 4325 lun_id_t 4326 xpt_path_lun_id(struct cam_path *path) 4327 { 4328 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4329 4330 if (path->device != NULL) 4331 return (path->device->lun_id); 4332 else 4333 return (CAM_LUN_WILDCARD); 4334 } 4335 4336 struct cam_sim * 4337 xpt_path_sim(struct cam_path *path) 4338 { 4339 4340 return (path->bus->sim); 4341 } 4342 4343 struct cam_periph* 4344 xpt_path_periph(struct cam_path *path) 4345 { 4346 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4347 4348 return (path->periph); 4349 } 4350 4351 /* 4352 * Release a CAM control block for the caller. Remit the cost of the structure 4353 * to the device referenced by the path. If the this device had no 'credits' 4354 * and peripheral drivers have registered async callbacks for this notification 4355 * call them now. 4356 */ 4357 void 4358 xpt_release_ccb(union ccb *free_ccb) 4359 { 4360 int s; 4361 struct cam_path *path; 4362 struct cam_ed *device; 4363 struct cam_eb *bus; 4364 struct cam_sim *sim; 4365 4366 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n")); 4367 path = free_ccb->ccb_h.path; 4368 device = path->device; 4369 bus = path->bus; 4370 sim = bus->sim; 4371 s = splsoftcam(); 4372 4373 mtx_assert(sim->mtx, MA_OWNED); 4374 4375 cam_ccbq_release_opening(&device->ccbq); 4376 if (sim->ccb_count > sim->max_ccbs) { 4377 xpt_free_ccb(free_ccb); 4378 sim->ccb_count--; 4379 } else { 4380 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h, 4381 xpt_links.sle); 4382 } 4383 if (sim->devq == NULL) { 4384 splx(s); 4385 return; 4386 } 4387 sim->devq->alloc_openings++; 4388 sim->devq->alloc_active--; 4389 /* XXX Turn this into an inline function - xpt_run_device?? */ 4390 if ((device_is_alloc_queued(device) == 0) 4391 && (device->drvq.entries > 0)) { 4392 xpt_schedule_dev_allocq(bus, device); 4393 } 4394 splx(s); 4395 if (dev_allocq_is_runnable(sim->devq)) 4396 xpt_run_dev_allocq(bus); 4397 } 4398 4399 /* Functions accessed by SIM drivers */ 4400 4401 /* 4402 * A sim structure, listing the SIM entry points and instance 4403 * identification info is passed to xpt_bus_register to hook the SIM 4404 * into the CAM framework. xpt_bus_register creates a cam_eb entry 4405 * for this new bus and places it in the array of busses and assigns 4406 * it a path_id. The path_id may be influenced by "hard wiring" 4407 * information specified by the user. Once interrupt services are 4408 * availible, the bus will be probed. 4409 */ 4410 int32_t 4411 xpt_bus_register(struct cam_sim *sim, u_int32_t bus) 4412 { 4413 struct cam_eb *new_bus; 4414 struct cam_eb *old_bus; 4415 struct ccb_pathinq cpi; 4416 int s; 4417 4418 mtx_assert(sim->mtx, MA_OWNED); 4419 4420 sim->bus_id = bus; 4421 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus), 4422 M_CAMXPT, M_NOWAIT); 4423 if (new_bus == NULL) { 4424 /* Couldn't satisfy request */ 4425 return (CAM_RESRC_UNAVAIL); 4426 } 4427 4428 if (strcmp(sim->sim_name, "xpt") != 0) { 4429 4430 sim->path_id = 4431 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id); 4432 } 4433 4434 TAILQ_INIT(&new_bus->et_entries); 4435 new_bus->path_id = sim->path_id; 4436 new_bus->sim = sim; 4437 timevalclear(&new_bus->last_reset); 4438 new_bus->flags = 0; 4439 new_bus->refcount = 1; /* Held until a bus_deregister event */ 4440 new_bus->generation = 0; 4441 s = splcam(); 4442 mtx_lock(&xsoftc.xpt_topo_lock); 4443 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses); 4444 while (old_bus != NULL 4445 && old_bus->path_id < new_bus->path_id) 4446 old_bus = TAILQ_NEXT(old_bus, links); 4447 if (old_bus != NULL) 4448 TAILQ_INSERT_BEFORE(old_bus, new_bus, links); 4449 else 4450 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links); 4451 xsoftc.bus_generation++; 4452 mtx_unlock(&xsoftc.xpt_topo_lock); 4453 splx(s); 4454 4455 /* Notify interested parties */ 4456 if (sim->path_id != CAM_XPT_PATH_ID) { 4457 struct cam_path path; 4458 4459 xpt_compile_path(&path, /*periph*/NULL, sim->path_id, 4460 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 4461 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1); 4462 cpi.ccb_h.func_code = XPT_PATH_INQ; 4463 xpt_action((union ccb *)&cpi); 4464 xpt_async(AC_PATH_REGISTERED, &path, &cpi); 4465 xpt_release_path(&path); 4466 } 4467 return (CAM_SUCCESS); 4468 } 4469 4470 int32_t 4471 xpt_bus_deregister(path_id_t pathid) 4472 { 4473 struct cam_path bus_path; 4474 struct cam_ed *device; 4475 struct cam_ed_qinfo *qinfo; 4476 struct cam_devq *devq; 4477 struct cam_periph *periph; 4478 struct cam_sim *ccbsim; 4479 union ccb *work_ccb; 4480 cam_status status; 4481 4482 4483 status = xpt_compile_path(&bus_path, NULL, pathid, 4484 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 4485 if (status != CAM_REQ_CMP) 4486 return (status); 4487 4488 xpt_async(AC_LOST_DEVICE, &bus_path, NULL); 4489 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL); 4490 4491 /* The SIM may be gone, so use a dummy SIM for any stray operations. */ 4492 devq = bus_path.bus->sim->devq; 4493 bus_path.bus->sim = &cam_dead_sim; 4494 4495 /* Execute any pending operations now. */ 4496 while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue, 4497 CAMQ_HEAD)) != NULL || 4498 (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue, 4499 CAMQ_HEAD)) != NULL) { 4500 do { 4501 device = qinfo->device; 4502 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD); 4503 if (work_ccb != NULL) { 4504 devq->active_dev = device; 4505 cam_ccbq_remove_ccb(&device->ccbq, work_ccb); 4506 cam_ccbq_send_ccb(&device->ccbq, work_ccb); 4507 ccbsim = work_ccb->ccb_h.path->bus->sim; 4508 (*(ccbsim->sim_action))(ccbsim, work_ccb); 4509 } 4510 4511 periph = (struct cam_periph *)camq_remove(&device->drvq, 4512 CAMQ_HEAD); 4513 if (periph != NULL) 4514 xpt_schedule(periph, periph->pinfo.priority); 4515 } while (work_ccb != NULL || periph != NULL); 4516 } 4517 4518 /* Make sure all completed CCBs are processed. */ 4519 while (!TAILQ_EMPTY(&cam_bioq)) { 4520 camisr(&cam_bioq); 4521 4522 /* Repeat the async's for the benefit of any new devices. */ 4523 xpt_async(AC_LOST_DEVICE, &bus_path, NULL); 4524 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL); 4525 } 4526 4527 /* Release the reference count held while registered. */ 4528 xpt_release_bus(bus_path.bus); 4529 xpt_release_path(&bus_path); 4530 4531 /* Recheck for more completed CCBs. */ 4532 while (!TAILQ_EMPTY(&cam_bioq)) 4533 camisr(&cam_bioq); 4534 4535 return (CAM_REQ_CMP); 4536 } 4537 4538 static path_id_t 4539 xptnextfreepathid(void) 4540 { 4541 struct cam_eb *bus; 4542 path_id_t pathid; 4543 const char *strval; 4544 4545 pathid = 0; 4546 mtx_lock(&xsoftc.xpt_topo_lock); 4547 bus = TAILQ_FIRST(&xsoftc.xpt_busses); 4548 retry: 4549 /* Find an unoccupied pathid */ 4550 while (bus != NULL && bus->path_id <= pathid) { 4551 if (bus->path_id == pathid) 4552 pathid++; 4553 bus = TAILQ_NEXT(bus, links); 4554 } 4555 mtx_unlock(&xsoftc.xpt_topo_lock); 4556 4557 /* 4558 * Ensure that this pathid is not reserved for 4559 * a bus that may be registered in the future. 4560 */ 4561 if (resource_string_value("scbus", pathid, "at", &strval) == 0) { 4562 ++pathid; 4563 /* Start the search over */ 4564 mtx_lock(&xsoftc.xpt_topo_lock); 4565 goto retry; 4566 } 4567 return (pathid); 4568 } 4569 4570 static path_id_t 4571 xptpathid(const char *sim_name, int sim_unit, int sim_bus) 4572 { 4573 path_id_t pathid; 4574 int i, dunit, val; 4575 char buf[32]; 4576 const char *dname; 4577 4578 pathid = CAM_XPT_PATH_ID; 4579 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit); 4580 i = 0; 4581 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) { 4582 if (strcmp(dname, "scbus")) { 4583 /* Avoid a bit of foot shooting. */ 4584 continue; 4585 } 4586 if (dunit < 0) /* unwired?! */ 4587 continue; 4588 if (resource_int_value("scbus", dunit, "bus", &val) == 0) { 4589 if (sim_bus == val) { 4590 pathid = dunit; 4591 break; 4592 } 4593 } else if (sim_bus == 0) { 4594 /* Unspecified matches bus 0 */ 4595 pathid = dunit; 4596 break; 4597 } else { 4598 printf("Ambiguous scbus configuration for %s%d " 4599 "bus %d, cannot wire down. The kernel " 4600 "config entry for scbus%d should " 4601 "specify a controller bus.\n" 4602 "Scbus will be assigned dynamically.\n", 4603 sim_name, sim_unit, sim_bus, dunit); 4604 break; 4605 } 4606 } 4607 4608 if (pathid == CAM_XPT_PATH_ID) 4609 pathid = xptnextfreepathid(); 4610 return (pathid); 4611 } 4612 4613 void 4614 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg) 4615 { 4616 struct cam_eb *bus; 4617 struct cam_et *target, *next_target; 4618 struct cam_ed *device, *next_device; 4619 int s; 4620 4621 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4622 4623 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n")); 4624 4625 /* 4626 * Most async events come from a CAM interrupt context. In 4627 * a few cases, the error recovery code at the peripheral layer, 4628 * which may run from our SWI or a process context, may signal 4629 * deferred events with a call to xpt_async. Ensure async 4630 * notifications are serialized by blocking cam interrupts. 4631 */ 4632 s = splcam(); 4633 4634 bus = path->bus; 4635 4636 if (async_code == AC_BUS_RESET) { 4637 int s; 4638 4639 s = splclock(); 4640 /* Update our notion of when the last reset occurred */ 4641 microtime(&bus->last_reset); 4642 splx(s); 4643 } 4644 4645 for (target = TAILQ_FIRST(&bus->et_entries); 4646 target != NULL; 4647 target = next_target) { 4648 4649 next_target = TAILQ_NEXT(target, links); 4650 4651 if (path->target != target 4652 && path->target->target_id != CAM_TARGET_WILDCARD 4653 && target->target_id != CAM_TARGET_WILDCARD) 4654 continue; 4655 4656 if (async_code == AC_SENT_BDR) { 4657 int s; 4658 4659 /* Update our notion of when the last reset occurred */ 4660 s = splclock(); 4661 microtime(&path->target->last_reset); 4662 splx(s); 4663 } 4664 4665 for (device = TAILQ_FIRST(&target->ed_entries); 4666 device != NULL; 4667 device = next_device) { 4668 4669 next_device = TAILQ_NEXT(device, links); 4670 4671 if (path->device != device 4672 && path->device->lun_id != CAM_LUN_WILDCARD 4673 && device->lun_id != CAM_LUN_WILDCARD) 4674 continue; 4675 4676 xpt_dev_async(async_code, bus, target, 4677 device, async_arg); 4678 4679 xpt_async_bcast(&device->asyncs, async_code, 4680 path, async_arg); 4681 } 4682 } 4683 4684 /* 4685 * If this wasn't a fully wildcarded async, tell all 4686 * clients that want all async events. 4687 */ 4688 if (bus != xpt_periph->path->bus) 4689 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code, 4690 path, async_arg); 4691 splx(s); 4692 } 4693 4694 static void 4695 xpt_async_bcast(struct async_list *async_head, 4696 u_int32_t async_code, 4697 struct cam_path *path, void *async_arg) 4698 { 4699 struct async_node *cur_entry; 4700 4701 cur_entry = SLIST_FIRST(async_head); 4702 while (cur_entry != NULL) { 4703 struct async_node *next_entry; 4704 /* 4705 * Grab the next list entry before we call the current 4706 * entry's callback. This is because the callback function 4707 * can delete its async callback entry. 4708 */ 4709 next_entry = SLIST_NEXT(cur_entry, links); 4710 if ((cur_entry->event_enable & async_code) != 0) 4711 cur_entry->callback(cur_entry->callback_arg, 4712 async_code, path, 4713 async_arg); 4714 cur_entry = next_entry; 4715 } 4716 } 4717 4718 /* 4719 * Handle any per-device event notifications that require action by the XPT. 4720 */ 4721 static void 4722 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target, 4723 struct cam_ed *device, void *async_arg) 4724 { 4725 cam_status status; 4726 struct cam_path newpath; 4727 4728 /* 4729 * We only need to handle events for real devices. 4730 */ 4731 if (target->target_id == CAM_TARGET_WILDCARD 4732 || device->lun_id == CAM_LUN_WILDCARD) 4733 return; 4734 4735 /* 4736 * We need our own path with wildcards expanded to 4737 * handle certain types of events. 4738 */ 4739 if ((async_code == AC_SENT_BDR) 4740 || (async_code == AC_BUS_RESET) 4741 || (async_code == AC_INQ_CHANGED)) 4742 status = xpt_compile_path(&newpath, NULL, 4743 bus->path_id, 4744 target->target_id, 4745 device->lun_id); 4746 else 4747 status = CAM_REQ_CMP_ERR; 4748 4749 if (status == CAM_REQ_CMP) { 4750 4751 /* 4752 * Allow transfer negotiation to occur in a 4753 * tag free environment. 4754 */ 4755 if (async_code == AC_SENT_BDR 4756 || async_code == AC_BUS_RESET) 4757 xpt_toggle_tags(&newpath); 4758 4759 if (async_code == AC_INQ_CHANGED) { 4760 /* 4761 * We've sent a start unit command, or 4762 * something similar to a device that 4763 * may have caused its inquiry data to 4764 * change. So we re-scan the device to 4765 * refresh the inquiry data for it. 4766 */ 4767 xpt_scan_lun(newpath.periph, &newpath, 4768 CAM_EXPECT_INQ_CHANGE, NULL); 4769 } 4770 xpt_release_path(&newpath); 4771 } else if (async_code == AC_LOST_DEVICE) { 4772 device->flags |= CAM_DEV_UNCONFIGURED; 4773 } else if (async_code == AC_TRANSFER_NEG) { 4774 struct ccb_trans_settings *settings; 4775 4776 settings = (struct ccb_trans_settings *)async_arg; 4777 xpt_set_transfer_settings(settings, device, 4778 /*async_update*/TRUE); 4779 } 4780 } 4781 4782 u_int32_t 4783 xpt_freeze_devq(struct cam_path *path, u_int count) 4784 { 4785 int s; 4786 struct ccb_hdr *ccbh; 4787 4788 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4789 4790 s = splcam(); 4791 path->device->qfrozen_cnt += count; 4792 4793 /* 4794 * Mark the last CCB in the queue as needing 4795 * to be requeued if the driver hasn't 4796 * changed it's state yet. This fixes a race 4797 * where a ccb is just about to be queued to 4798 * a controller driver when it's interrupt routine 4799 * freezes the queue. To completly close the 4800 * hole, controller drives must check to see 4801 * if a ccb's status is still CAM_REQ_INPROG 4802 * under spl protection just before they queue 4803 * the CCB. See ahc_action/ahc_freeze_devq for 4804 * an example. 4805 */ 4806 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq); 4807 if (ccbh && ccbh->status == CAM_REQ_INPROG) 4808 ccbh->status = CAM_REQUEUE_REQ; 4809 splx(s); 4810 return (path->device->qfrozen_cnt); 4811 } 4812 4813 u_int32_t 4814 xpt_freeze_simq(struct cam_sim *sim, u_int count) 4815 { 4816 mtx_assert(sim->mtx, MA_OWNED); 4817 4818 sim->devq->send_queue.qfrozen_cnt += count; 4819 if (sim->devq->active_dev != NULL) { 4820 struct ccb_hdr *ccbh; 4821 4822 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs, 4823 ccb_hdr_tailq); 4824 if (ccbh && ccbh->status == CAM_REQ_INPROG) 4825 ccbh->status = CAM_REQUEUE_REQ; 4826 } 4827 return (sim->devq->send_queue.qfrozen_cnt); 4828 } 4829 4830 static void 4831 xpt_release_devq_timeout(void *arg) 4832 { 4833 struct cam_ed *device; 4834 4835 device = (struct cam_ed *)arg; 4836 4837 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE); 4838 } 4839 4840 void 4841 xpt_release_devq(struct cam_path *path, u_int count, int run_queue) 4842 { 4843 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4844 4845 xpt_release_devq_device(path->device, count, run_queue); 4846 } 4847 4848 static void 4849 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue) 4850 { 4851 int rundevq; 4852 int s0, s1; 4853 4854 rundevq = 0; 4855 s0 = splsoftcam(); 4856 s1 = splcam(); 4857 if (dev->qfrozen_cnt > 0) { 4858 4859 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count; 4860 dev->qfrozen_cnt -= count; 4861 if (dev->qfrozen_cnt == 0) { 4862 4863 /* 4864 * No longer need to wait for a successful 4865 * command completion. 4866 */ 4867 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE; 4868 4869 /* 4870 * Remove any timeouts that might be scheduled 4871 * to release this queue. 4872 */ 4873 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 4874 callout_stop(&dev->callout); 4875 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING; 4876 } 4877 4878 /* 4879 * Now that we are unfrozen schedule the 4880 * device so any pending transactions are 4881 * run. 4882 */ 4883 if ((dev->ccbq.queue.entries > 0) 4884 && (xpt_schedule_dev_sendq(dev->target->bus, dev)) 4885 && (run_queue != 0)) { 4886 rundevq = 1; 4887 } 4888 } 4889 } 4890 splx(s1); 4891 if (rundevq != 0) 4892 xpt_run_dev_sendq(dev->target->bus); 4893 splx(s0); 4894 } 4895 4896 void 4897 xpt_release_simq(struct cam_sim *sim, int run_queue) 4898 { 4899 int s; 4900 struct camq *sendq; 4901 4902 mtx_assert(sim->mtx, MA_OWNED); 4903 4904 sendq = &(sim->devq->send_queue); 4905 s = splcam(); 4906 if (sendq->qfrozen_cnt > 0) { 4907 4908 sendq->qfrozen_cnt--; 4909 if (sendq->qfrozen_cnt == 0) { 4910 struct cam_eb *bus; 4911 4912 /* 4913 * If there is a timeout scheduled to release this 4914 * sim queue, remove it. The queue frozen count is 4915 * already at 0. 4916 */ 4917 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){ 4918 callout_stop(&sim->callout); 4919 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING; 4920 } 4921 bus = xpt_find_bus(sim->path_id); 4922 splx(s); 4923 4924 if (run_queue) { 4925 /* 4926 * Now that we are unfrozen run the send queue. 4927 */ 4928 xpt_run_dev_sendq(bus); 4929 } 4930 xpt_release_bus(bus); 4931 } else 4932 splx(s); 4933 } else 4934 splx(s); 4935 } 4936 4937 /* 4938 * XXX Appears to be unused. 4939 */ 4940 static void 4941 xpt_release_simq_timeout(void *arg) 4942 { 4943 struct cam_sim *sim; 4944 4945 sim = (struct cam_sim *)arg; 4946 xpt_release_simq(sim, /* run_queue */ TRUE); 4947 } 4948 4949 void 4950 xpt_done(union ccb *done_ccb) 4951 { 4952 int s; 4953 4954 s = splcam(); 4955 4956 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n")); 4957 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) { 4958 /* 4959 * Queue up the request for handling by our SWI handler 4960 * any of the "non-immediate" type of ccbs. 4961 */ 4962 switch (done_ccb->ccb_h.path->periph->type) { 4963 case CAM_PERIPH_BIO: 4964 mtx_lock(&cam_bioq_lock); 4965 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h, 4966 sim_links.tqe); 4967 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX; 4968 mtx_unlock(&cam_bioq_lock); 4969 if ((done_ccb->ccb_h.path->periph->flags & 4970 CAM_PERIPH_POLLED) == 0) 4971 swi_sched(cambio_ih, 0); 4972 break; 4973 default: 4974 panic("unknown periph type %d", 4975 done_ccb->ccb_h.path->periph->type); 4976 } 4977 } 4978 splx(s); 4979 } 4980 4981 union ccb * 4982 xpt_alloc_ccb(struct cam_sim *sim) 4983 { 4984 union ccb *new_ccb; 4985 4986 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK); 4987 if ((sim != NULL) && ((sim->flags & CAM_SIM_MPSAFE) == 0)) { 4988 callout_handle_init(&new_ccb->ccb_h.timeout_ch); 4989 } 4990 return (new_ccb); 4991 } 4992 4993 union ccb * 4994 xpt_alloc_ccb_nowait(struct cam_sim *sim) 4995 { 4996 union ccb *new_ccb; 4997 4998 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT); 4999 if ((sim != NULL) && ((sim->flags & CAM_SIM_MPSAFE) == 0)) { 5000 callout_handle_init(&new_ccb->ccb_h.timeout_ch); 5001 } 5002 return (new_ccb); 5003 } 5004 5005 void 5006 xpt_free_ccb(union ccb *free_ccb) 5007 { 5008 free(free_ccb, M_CAMXPT); 5009 } 5010 5011 5012 5013 /* Private XPT functions */ 5014 5015 /* 5016 * Get a CAM control block for the caller. Charge the structure to the device 5017 * referenced by the path. If the this device has no 'credits' then the 5018 * device already has the maximum number of outstanding operations under way 5019 * and we return NULL. If we don't have sufficient resources to allocate more 5020 * ccbs, we also return NULL. 5021 */ 5022 static union ccb * 5023 xpt_get_ccb(struct cam_ed *device) 5024 { 5025 union ccb *new_ccb; 5026 struct cam_sim *sim; 5027 int s; 5028 5029 s = splsoftcam(); 5030 sim = device->sim; 5031 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) { 5032 new_ccb = xpt_alloc_ccb_nowait(sim); 5033 if (new_ccb == NULL) { 5034 splx(s); 5035 return (NULL); 5036 } 5037 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h, 5038 xpt_links.sle); 5039 sim->ccb_count++; 5040 } 5041 cam_ccbq_take_opening(&device->ccbq); 5042 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle); 5043 splx(s); 5044 return (new_ccb); 5045 } 5046 5047 static void 5048 xpt_release_bus(struct cam_eb *bus) 5049 { 5050 int s; 5051 5052 s = splcam(); 5053 if ((--bus->refcount == 0) 5054 && (TAILQ_FIRST(&bus->et_entries) == NULL)) { 5055 mtx_lock(&xsoftc.xpt_topo_lock); 5056 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links); 5057 xsoftc.bus_generation++; 5058 mtx_unlock(&xsoftc.xpt_topo_lock); 5059 splx(s); 5060 free(bus, M_CAMXPT); 5061 } else 5062 splx(s); 5063 } 5064 5065 static struct cam_et * 5066 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id) 5067 { 5068 struct cam_et *target; 5069 5070 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT); 5071 if (target != NULL) { 5072 struct cam_et *cur_target; 5073 5074 TAILQ_INIT(&target->ed_entries); 5075 target->bus = bus; 5076 target->target_id = target_id; 5077 target->refcount = 1; 5078 target->generation = 0; 5079 timevalclear(&target->last_reset); 5080 /* 5081 * Hold a reference to our parent bus so it 5082 * will not go away before we do. 5083 */ 5084 bus->refcount++; 5085 5086 /* Insertion sort into our bus's target list */ 5087 cur_target = TAILQ_FIRST(&bus->et_entries); 5088 while (cur_target != NULL && cur_target->target_id < target_id) 5089 cur_target = TAILQ_NEXT(cur_target, links); 5090 5091 if (cur_target != NULL) { 5092 TAILQ_INSERT_BEFORE(cur_target, target, links); 5093 } else { 5094 TAILQ_INSERT_TAIL(&bus->et_entries, target, links); 5095 } 5096 bus->generation++; 5097 } 5098 return (target); 5099 } 5100 5101 static void 5102 xpt_release_target(struct cam_eb *bus, struct cam_et *target) 5103 { 5104 int s; 5105 5106 s = splcam(); 5107 if ((--target->refcount == 0) 5108 && (TAILQ_FIRST(&target->ed_entries) == NULL)) { 5109 TAILQ_REMOVE(&bus->et_entries, target, links); 5110 bus->generation++; 5111 splx(s); 5112 free(target, M_CAMXPT); 5113 xpt_release_bus(bus); 5114 } else 5115 splx(s); 5116 } 5117 5118 static struct cam_ed * 5119 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 5120 { 5121 struct cam_path path; 5122 struct cam_ed *device; 5123 struct cam_devq *devq; 5124 cam_status status; 5125 5126 if (SIM_DEAD(bus->sim)) 5127 return (NULL); 5128 5129 /* Make space for us in the device queue on our bus */ 5130 devq = bus->sim->devq; 5131 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1); 5132 5133 if (status != CAM_REQ_CMP) { 5134 device = NULL; 5135 } else { 5136 device = (struct cam_ed *)malloc(sizeof(*device), 5137 M_CAMXPT, M_NOWAIT); 5138 } 5139 5140 if (device != NULL) { 5141 struct cam_ed *cur_device; 5142 5143 cam_init_pinfo(&device->alloc_ccb_entry.pinfo); 5144 device->alloc_ccb_entry.device = device; 5145 cam_init_pinfo(&device->send_ccb_entry.pinfo); 5146 device->send_ccb_entry.device = device; 5147 device->target = target; 5148 device->lun_id = lun_id; 5149 device->sim = bus->sim; 5150 /* Initialize our queues */ 5151 if (camq_init(&device->drvq, 0) != 0) { 5152 free(device, M_CAMXPT); 5153 return (NULL); 5154 } 5155 if (cam_ccbq_init(&device->ccbq, 5156 bus->sim->max_dev_openings) != 0) { 5157 camq_fini(&device->drvq); 5158 free(device, M_CAMXPT); 5159 return (NULL); 5160 } 5161 SLIST_INIT(&device->asyncs); 5162 SLIST_INIT(&device->periphs); 5163 device->generation = 0; 5164 device->owner = NULL; 5165 /* 5166 * Take the default quirk entry until we have inquiry 5167 * data and can determine a better quirk to use. 5168 */ 5169 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1]; 5170 bzero(&device->inq_data, sizeof(device->inq_data)); 5171 device->inq_flags = 0; 5172 device->queue_flags = 0; 5173 device->serial_num = NULL; 5174 device->serial_num_len = 0; 5175 device->qfrozen_cnt = 0; 5176 device->flags = CAM_DEV_UNCONFIGURED; 5177 device->tag_delay_count = 0; 5178 device->tag_saved_openings = 0; 5179 device->refcount = 1; 5180 if (bus->sim->flags & CAM_SIM_MPSAFE) 5181 callout_init_mtx(&device->callout, bus->sim->mtx, 0); 5182 else 5183 callout_init_mtx(&device->callout, &Giant, 0); 5184 5185 /* 5186 * Hold a reference to our parent target so it 5187 * will not go away before we do. 5188 */ 5189 target->refcount++; 5190 5191 /* 5192 * XXX should be limited by number of CCBs this bus can 5193 * do. 5194 */ 5195 bus->sim->max_ccbs += device->ccbq.devq_openings; 5196 /* Insertion sort into our target's device list */ 5197 cur_device = TAILQ_FIRST(&target->ed_entries); 5198 while (cur_device != NULL && cur_device->lun_id < lun_id) 5199 cur_device = TAILQ_NEXT(cur_device, links); 5200 if (cur_device != NULL) { 5201 TAILQ_INSERT_BEFORE(cur_device, device, links); 5202 } else { 5203 TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 5204 } 5205 target->generation++; 5206 if (lun_id != CAM_LUN_WILDCARD) { 5207 xpt_compile_path(&path, 5208 NULL, 5209 bus->path_id, 5210 target->target_id, 5211 lun_id); 5212 xpt_devise_transport(&path); 5213 xpt_release_path(&path); 5214 } 5215 } 5216 return (device); 5217 } 5218 5219 static void 5220 xpt_release_device(struct cam_eb *bus, struct cam_et *target, 5221 struct cam_ed *device) 5222 { 5223 int s; 5224 5225 s = splcam(); 5226 if ((--device->refcount == 0) 5227 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) { 5228 struct cam_devq *devq; 5229 5230 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX 5231 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX) 5232 panic("Removing device while still queued for ccbs"); 5233 5234 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) 5235 callout_stop(&device->callout); 5236 5237 TAILQ_REMOVE(&target->ed_entries, device,links); 5238 target->generation++; 5239 bus->sim->max_ccbs -= device->ccbq.devq_openings; 5240 if (!SIM_DEAD(bus->sim)) { 5241 /* Release our slot in the devq */ 5242 devq = bus->sim->devq; 5243 cam_devq_resize(devq, devq->alloc_queue.array_size - 1); 5244 } 5245 splx(s); 5246 camq_fini(&device->drvq); 5247 camq_fini(&device->ccbq.queue); 5248 free(device, M_CAMXPT); 5249 xpt_release_target(bus, target); 5250 } else 5251 splx(s); 5252 } 5253 5254 static u_int32_t 5255 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings) 5256 { 5257 int s; 5258 int diff; 5259 int result; 5260 struct cam_ed *dev; 5261 5262 dev = path->device; 5263 s = splsoftcam(); 5264 5265 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings); 5266 result = cam_ccbq_resize(&dev->ccbq, newopenings); 5267 if (result == CAM_REQ_CMP && (diff < 0)) { 5268 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED; 5269 } 5270 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 5271 || (dev->inq_flags & SID_CmdQue) != 0) 5272 dev->tag_saved_openings = newopenings; 5273 /* Adjust the global limit */ 5274 dev->sim->max_ccbs += diff; 5275 splx(s); 5276 return (result); 5277 } 5278 5279 static struct cam_eb * 5280 xpt_find_bus(path_id_t path_id) 5281 { 5282 struct cam_eb *bus; 5283 5284 mtx_lock(&xsoftc.xpt_topo_lock); 5285 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses); 5286 bus != NULL; 5287 bus = TAILQ_NEXT(bus, links)) { 5288 if (bus->path_id == path_id) { 5289 bus->refcount++; 5290 break; 5291 } 5292 } 5293 mtx_unlock(&xsoftc.xpt_topo_lock); 5294 return (bus); 5295 } 5296 5297 static struct cam_et * 5298 xpt_find_target(struct cam_eb *bus, target_id_t target_id) 5299 { 5300 struct cam_et *target; 5301 5302 for (target = TAILQ_FIRST(&bus->et_entries); 5303 target != NULL; 5304 target = TAILQ_NEXT(target, links)) { 5305 if (target->target_id == target_id) { 5306 target->refcount++; 5307 break; 5308 } 5309 } 5310 return (target); 5311 } 5312 5313 static struct cam_ed * 5314 xpt_find_device(struct cam_et *target, lun_id_t lun_id) 5315 { 5316 struct cam_ed *device; 5317 5318 for (device = TAILQ_FIRST(&target->ed_entries); 5319 device != NULL; 5320 device = TAILQ_NEXT(device, links)) { 5321 if (device->lun_id == lun_id) { 5322 device->refcount++; 5323 break; 5324 } 5325 } 5326 return (device); 5327 } 5328 5329 typedef struct { 5330 union ccb *request_ccb; 5331 struct ccb_pathinq *cpi; 5332 int counter; 5333 } xpt_scan_bus_info; 5334 5335 /* 5336 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb. 5337 * As the scan progresses, xpt_scan_bus is used as the 5338 * callback on completion function. 5339 */ 5340 static void 5341 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb) 5342 { 5343 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 5344 ("xpt_scan_bus\n")); 5345 switch (request_ccb->ccb_h.func_code) { 5346 case XPT_SCAN_BUS: 5347 { 5348 xpt_scan_bus_info *scan_info; 5349 union ccb *work_ccb; 5350 struct cam_path *path; 5351 u_int i; 5352 u_int max_target; 5353 u_int initiator_id; 5354 5355 /* Find out the characteristics of the bus */ 5356 work_ccb = xpt_alloc_ccb_nowait(periph->sim); 5357 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path, 5358 request_ccb->ccb_h.pinfo.priority); 5359 work_ccb->ccb_h.func_code = XPT_PATH_INQ; 5360 xpt_action(work_ccb); 5361 if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 5362 request_ccb->ccb_h.status = work_ccb->ccb_h.status; 5363 xpt_free_ccb(work_ccb); 5364 xpt_done(request_ccb); 5365 return; 5366 } 5367 5368 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) { 5369 /* 5370 * Can't scan the bus on an adapter that 5371 * cannot perform the initiator role. 5372 */ 5373 request_ccb->ccb_h.status = CAM_REQ_CMP; 5374 xpt_free_ccb(work_ccb); 5375 xpt_done(request_ccb); 5376 return; 5377 } 5378 5379 /* Save some state for use while we probe for devices */ 5380 scan_info = (xpt_scan_bus_info *) 5381 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_NOWAIT); 5382 scan_info->request_ccb = request_ccb; 5383 scan_info->cpi = &work_ccb->cpi; 5384 5385 /* Cache on our stack so we can work asynchronously */ 5386 max_target = scan_info->cpi->max_target; 5387 initiator_id = scan_info->cpi->initiator_id; 5388 5389 5390 /* 5391 * We can scan all targets in parallel, or do it sequentially. 5392 */ 5393 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) { 5394 max_target = 0; 5395 scan_info->counter = 0; 5396 } else { 5397 scan_info->counter = scan_info->cpi->max_target + 1; 5398 if (scan_info->cpi->initiator_id < scan_info->counter) { 5399 scan_info->counter--; 5400 } 5401 } 5402 5403 for (i = 0; i <= max_target; i++) { 5404 cam_status status; 5405 if (i == initiator_id) 5406 continue; 5407 5408 status = xpt_create_path(&path, xpt_periph, 5409 request_ccb->ccb_h.path_id, 5410 i, 0); 5411 if (status != CAM_REQ_CMP) { 5412 printf("xpt_scan_bus: xpt_create_path failed" 5413 " with status %#x, bus scan halted\n", 5414 status); 5415 free(scan_info, M_TEMP); 5416 request_ccb->ccb_h.status = status; 5417 xpt_free_ccb(work_ccb); 5418 xpt_done(request_ccb); 5419 break; 5420 } 5421 work_ccb = xpt_alloc_ccb_nowait(periph->sim); 5422 xpt_setup_ccb(&work_ccb->ccb_h, path, 5423 request_ccb->ccb_h.pinfo.priority); 5424 work_ccb->ccb_h.func_code = XPT_SCAN_LUN; 5425 work_ccb->ccb_h.cbfcnp = xpt_scan_bus; 5426 work_ccb->ccb_h.ppriv_ptr0 = scan_info; 5427 work_ccb->crcn.flags = request_ccb->crcn.flags; 5428 xpt_action(work_ccb); 5429 } 5430 break; 5431 } 5432 case XPT_SCAN_LUN: 5433 { 5434 cam_status status; 5435 struct cam_path *path; 5436 xpt_scan_bus_info *scan_info; 5437 path_id_t path_id; 5438 target_id_t target_id; 5439 lun_id_t lun_id; 5440 5441 /* Reuse the same CCB to query if a device was really found */ 5442 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0; 5443 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path, 5444 request_ccb->ccb_h.pinfo.priority); 5445 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 5446 5447 path_id = request_ccb->ccb_h.path_id; 5448 target_id = request_ccb->ccb_h.target_id; 5449 lun_id = request_ccb->ccb_h.target_lun; 5450 xpt_action(request_ccb); 5451 5452 if (request_ccb->ccb_h.status != CAM_REQ_CMP) { 5453 struct cam_ed *device; 5454 struct cam_et *target; 5455 int s, phl; 5456 5457 /* 5458 * If we already probed lun 0 successfully, or 5459 * we have additional configured luns on this 5460 * target that might have "gone away", go onto 5461 * the next lun. 5462 */ 5463 target = request_ccb->ccb_h.path->target; 5464 /* 5465 * We may touch devices that we don't 5466 * hold references too, so ensure they 5467 * don't disappear out from under us. 5468 * The target above is referenced by the 5469 * path in the request ccb. 5470 */ 5471 phl = 0; 5472 s = splcam(); 5473 device = TAILQ_FIRST(&target->ed_entries); 5474 if (device != NULL) { 5475 phl = CAN_SRCH_HI_SPARSE(device); 5476 if (device->lun_id == 0) 5477 device = TAILQ_NEXT(device, links); 5478 } 5479 splx(s); 5480 if ((lun_id != 0) || (device != NULL)) { 5481 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl) 5482 lun_id++; 5483 } 5484 } else { 5485 struct cam_ed *device; 5486 5487 device = request_ccb->ccb_h.path->device; 5488 5489 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) { 5490 /* Try the next lun */ 5491 if (lun_id < (CAM_SCSI2_MAXLUN-1) 5492 || CAN_SRCH_HI_DENSE(device)) 5493 lun_id++; 5494 } 5495 } 5496 5497 /* 5498 * Free the current request path- we're done with it. 5499 */ 5500 xpt_free_path(request_ccb->ccb_h.path); 5501 5502 /* 5503 * Check to see if we scan any further luns. 5504 */ 5505 if (lun_id == request_ccb->ccb_h.target_lun 5506 || lun_id > scan_info->cpi->max_lun) { 5507 int done; 5508 5509 hop_again: 5510 done = 0; 5511 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) { 5512 scan_info->counter++; 5513 if (scan_info->counter == 5514 scan_info->cpi->initiator_id) { 5515 scan_info->counter++; 5516 } 5517 if (scan_info->counter >= 5518 scan_info->cpi->max_target+1) { 5519 done = 1; 5520 } 5521 } else { 5522 scan_info->counter--; 5523 if (scan_info->counter == 0) { 5524 done = 1; 5525 } 5526 } 5527 if (done) { 5528 xpt_free_ccb(request_ccb); 5529 xpt_free_ccb((union ccb *)scan_info->cpi); 5530 request_ccb = scan_info->request_ccb; 5531 free(scan_info, M_TEMP); 5532 request_ccb->ccb_h.status = CAM_REQ_CMP; 5533 xpt_done(request_ccb); 5534 break; 5535 } 5536 5537 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) { 5538 break; 5539 } 5540 status = xpt_create_path(&path, xpt_periph, 5541 scan_info->request_ccb->ccb_h.path_id, 5542 scan_info->counter, 0); 5543 if (status != CAM_REQ_CMP) { 5544 printf("xpt_scan_bus: xpt_create_path failed" 5545 " with status %#x, bus scan halted\n", 5546 status); 5547 xpt_free_ccb(request_ccb); 5548 xpt_free_ccb((union ccb *)scan_info->cpi); 5549 request_ccb = scan_info->request_ccb; 5550 free(scan_info, M_TEMP); 5551 request_ccb->ccb_h.status = status; 5552 xpt_done(request_ccb); 5553 break; 5554 } 5555 xpt_setup_ccb(&request_ccb->ccb_h, path, 5556 request_ccb->ccb_h.pinfo.priority); 5557 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 5558 request_ccb->ccb_h.cbfcnp = xpt_scan_bus; 5559 request_ccb->ccb_h.ppriv_ptr0 = scan_info; 5560 request_ccb->crcn.flags = 5561 scan_info->request_ccb->crcn.flags; 5562 } else { 5563 status = xpt_create_path(&path, xpt_periph, 5564 path_id, target_id, lun_id); 5565 if (status != CAM_REQ_CMP) { 5566 printf("xpt_scan_bus: xpt_create_path failed " 5567 "with status %#x, halting LUN scan\n", 5568 status); 5569 goto hop_again; 5570 } 5571 xpt_setup_ccb(&request_ccb->ccb_h, path, 5572 request_ccb->ccb_h.pinfo.priority); 5573 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 5574 request_ccb->ccb_h.cbfcnp = xpt_scan_bus; 5575 request_ccb->ccb_h.ppriv_ptr0 = scan_info; 5576 request_ccb->crcn.flags = 5577 scan_info->request_ccb->crcn.flags; 5578 } 5579 xpt_action(request_ccb); 5580 break; 5581 } 5582 default: 5583 break; 5584 } 5585 } 5586 5587 typedef enum { 5588 PROBE_TUR, 5589 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */ 5590 PROBE_FULL_INQUIRY, 5591 PROBE_MODE_SENSE, 5592 PROBE_SERIAL_NUM, 5593 PROBE_TUR_FOR_NEGOTIATION, 5594 PROBE_INQUIRY_BASIC_DV1, 5595 PROBE_INQUIRY_BASIC_DV2, 5596 PROBE_DV_EXIT 5597 } probe_action; 5598 5599 typedef enum { 5600 PROBE_INQUIRY_CKSUM = 0x01, 5601 PROBE_SERIAL_CKSUM = 0x02, 5602 PROBE_NO_ANNOUNCE = 0x04 5603 } probe_flags; 5604 5605 typedef struct { 5606 TAILQ_HEAD(, ccb_hdr) request_ccbs; 5607 probe_action action; 5608 union ccb saved_ccb; 5609 probe_flags flags; 5610 MD5_CTX context; 5611 u_int8_t digest[16]; 5612 } probe_softc; 5613 5614 static void 5615 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path, 5616 cam_flags flags, union ccb *request_ccb) 5617 { 5618 struct ccb_pathinq cpi; 5619 cam_status status; 5620 struct cam_path *new_path; 5621 struct cam_periph *old_periph; 5622 int s; 5623 5624 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 5625 ("xpt_scan_lun\n")); 5626 5627 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1); 5628 cpi.ccb_h.func_code = XPT_PATH_INQ; 5629 xpt_action((union ccb *)&cpi); 5630 5631 if (cpi.ccb_h.status != CAM_REQ_CMP) { 5632 if (request_ccb != NULL) { 5633 request_ccb->ccb_h.status = cpi.ccb_h.status; 5634 xpt_done(request_ccb); 5635 } 5636 return; 5637 } 5638 5639 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) { 5640 /* 5641 * Can't scan the bus on an adapter that 5642 * cannot perform the initiator role. 5643 */ 5644 if (request_ccb != NULL) { 5645 request_ccb->ccb_h.status = CAM_REQ_CMP; 5646 xpt_done(request_ccb); 5647 } 5648 return; 5649 } 5650 5651 if (request_ccb == NULL) { 5652 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT); 5653 if (request_ccb == NULL) { 5654 xpt_print(path, "xpt_scan_lun: can't allocate CCB, " 5655 "can't continue\n"); 5656 return; 5657 } 5658 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT); 5659 if (new_path == NULL) { 5660 xpt_print(path, "xpt_scan_lun: can't allocate path, " 5661 "can't continue\n"); 5662 free(request_ccb, M_TEMP); 5663 return; 5664 } 5665 status = xpt_compile_path(new_path, xpt_periph, 5666 path->bus->path_id, 5667 path->target->target_id, 5668 path->device->lun_id); 5669 5670 if (status != CAM_REQ_CMP) { 5671 xpt_print(path, "xpt_scan_lun: can't compile path, " 5672 "can't continue\n"); 5673 free(request_ccb, M_TEMP); 5674 free(new_path, M_TEMP); 5675 return; 5676 } 5677 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1); 5678 request_ccb->ccb_h.cbfcnp = xptscandone; 5679 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 5680 request_ccb->crcn.flags = flags; 5681 } 5682 5683 s = splsoftcam(); 5684 if ((old_periph = cam_periph_find(path, "probe")) != NULL) { 5685 probe_softc *softc; 5686 5687 softc = (probe_softc *)old_periph->softc; 5688 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 5689 periph_links.tqe); 5690 } else { 5691 status = cam_periph_alloc(proberegister, NULL, probecleanup, 5692 probestart, "probe", 5693 CAM_PERIPH_BIO, 5694 request_ccb->ccb_h.path, NULL, 0, 5695 request_ccb); 5696 5697 if (status != CAM_REQ_CMP) { 5698 xpt_print(path, "xpt_scan_lun: cam_alloc_periph " 5699 "returned an error, can't continue probe\n"); 5700 request_ccb->ccb_h.status = status; 5701 xpt_done(request_ccb); 5702 } 5703 } 5704 splx(s); 5705 } 5706 5707 static void 5708 xptscandone(struct cam_periph *periph, union ccb *done_ccb) 5709 { 5710 xpt_release_path(done_ccb->ccb_h.path); 5711 free(done_ccb->ccb_h.path, M_TEMP); 5712 free(done_ccb, M_TEMP); 5713 } 5714 5715 static cam_status 5716 proberegister(struct cam_periph *periph, void *arg) 5717 { 5718 union ccb *request_ccb; /* CCB representing the probe request */ 5719 cam_status status; 5720 probe_softc *softc; 5721 5722 request_ccb = (union ccb *)arg; 5723 if (periph == NULL) { 5724 printf("proberegister: periph was NULL!!\n"); 5725 return(CAM_REQ_CMP_ERR); 5726 } 5727 5728 if (request_ccb == NULL) { 5729 printf("proberegister: no probe CCB, " 5730 "can't register device\n"); 5731 return(CAM_REQ_CMP_ERR); 5732 } 5733 5734 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT); 5735 5736 if (softc == NULL) { 5737 printf("proberegister: Unable to probe new device. " 5738 "Unable to allocate softc\n"); 5739 return(CAM_REQ_CMP_ERR); 5740 } 5741 TAILQ_INIT(&softc->request_ccbs); 5742 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 5743 periph_links.tqe); 5744 softc->flags = 0; 5745 periph->softc = softc; 5746 status = cam_periph_acquire(periph); 5747 if (status != CAM_REQ_CMP) { 5748 return (status); 5749 } 5750 5751 5752 /* 5753 * Ensure we've waited at least a bus settle 5754 * delay before attempting to probe the device. 5755 * For HBAs that don't do bus resets, this won't make a difference. 5756 */ 5757 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset, 5758 scsi_delay); 5759 probeschedule(periph); 5760 return(CAM_REQ_CMP); 5761 } 5762 5763 static void 5764 probeschedule(struct cam_periph *periph) 5765 { 5766 struct ccb_pathinq cpi; 5767 union ccb *ccb; 5768 probe_softc *softc; 5769 5770 softc = (probe_softc *)periph->softc; 5771 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 5772 5773 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1); 5774 cpi.ccb_h.func_code = XPT_PATH_INQ; 5775 xpt_action((union ccb *)&cpi); 5776 5777 /* 5778 * If a device has gone away and another device, or the same one, 5779 * is back in the same place, it should have a unit attention 5780 * condition pending. It will not report the unit attention in 5781 * response to an inquiry, which may leave invalid transfer 5782 * negotiations in effect. The TUR will reveal the unit attention 5783 * condition. Only send the TUR for lun 0, since some devices 5784 * will get confused by commands other than inquiry to non-existent 5785 * luns. If you think a device has gone away start your scan from 5786 * lun 0. This will insure that any bogus transfer settings are 5787 * invalidated. 5788 * 5789 * If we haven't seen the device before and the controller supports 5790 * some kind of transfer negotiation, negotiate with the first 5791 * sent command if no bus reset was performed at startup. This 5792 * ensures that the device is not confused by transfer negotiation 5793 * settings left over by loader or BIOS action. 5794 */ 5795 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 5796 && (ccb->ccb_h.target_lun == 0)) { 5797 softc->action = PROBE_TUR; 5798 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0 5799 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) { 5800 proberequestdefaultnegotiation(periph); 5801 softc->action = PROBE_INQUIRY; 5802 } else { 5803 softc->action = PROBE_INQUIRY; 5804 } 5805 5806 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE) 5807 softc->flags |= PROBE_NO_ANNOUNCE; 5808 else 5809 softc->flags &= ~PROBE_NO_ANNOUNCE; 5810 5811 xpt_schedule(periph, ccb->ccb_h.pinfo.priority); 5812 } 5813 5814 static void 5815 probestart(struct cam_periph *periph, union ccb *start_ccb) 5816 { 5817 /* Probe the device that our peripheral driver points to */ 5818 struct ccb_scsiio *csio; 5819 probe_softc *softc; 5820 5821 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n")); 5822 5823 softc = (probe_softc *)periph->softc; 5824 csio = &start_ccb->csio; 5825 5826 switch (softc->action) { 5827 case PROBE_TUR: 5828 case PROBE_TUR_FOR_NEGOTIATION: 5829 case PROBE_DV_EXIT: 5830 { 5831 scsi_test_unit_ready(csio, 5832 /*retries*/4, 5833 probedone, 5834 MSG_SIMPLE_Q_TAG, 5835 SSD_FULL_SIZE, 5836 /*timeout*/60000); 5837 break; 5838 } 5839 case PROBE_INQUIRY: 5840 case PROBE_FULL_INQUIRY: 5841 case PROBE_INQUIRY_BASIC_DV1: 5842 case PROBE_INQUIRY_BASIC_DV2: 5843 { 5844 u_int inquiry_len; 5845 struct scsi_inquiry_data *inq_buf; 5846 5847 inq_buf = &periph->path->device->inq_data; 5848 5849 /* 5850 * If the device is currently configured, we calculate an 5851 * MD5 checksum of the inquiry data, and if the serial number 5852 * length is greater than 0, add the serial number data 5853 * into the checksum as well. Once the inquiry and the 5854 * serial number check finish, we attempt to figure out 5855 * whether we still have the same device. 5856 */ 5857 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 5858 5859 MD5Init(&softc->context); 5860 MD5Update(&softc->context, (unsigned char *)inq_buf, 5861 sizeof(struct scsi_inquiry_data)); 5862 softc->flags |= PROBE_INQUIRY_CKSUM; 5863 if (periph->path->device->serial_num_len > 0) { 5864 MD5Update(&softc->context, 5865 periph->path->device->serial_num, 5866 periph->path->device->serial_num_len); 5867 softc->flags |= PROBE_SERIAL_CKSUM; 5868 } 5869 MD5Final(softc->digest, &softc->context); 5870 } 5871 5872 if (softc->action == PROBE_INQUIRY) 5873 inquiry_len = SHORT_INQUIRY_LENGTH; 5874 else 5875 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf); 5876 5877 /* 5878 * Some parallel SCSI devices fail to send an 5879 * ignore wide residue message when dealing with 5880 * odd length inquiry requests. Round up to be 5881 * safe. 5882 */ 5883 inquiry_len = roundup2(inquiry_len, 2); 5884 5885 if (softc->action == PROBE_INQUIRY_BASIC_DV1 5886 || softc->action == PROBE_INQUIRY_BASIC_DV2) { 5887 inq_buf = malloc(inquiry_len, M_TEMP, M_NOWAIT); 5888 } 5889 if (inq_buf == NULL) { 5890 xpt_print(periph->path, "malloc failure- skipping Basic" 5891 "Domain Validation\n"); 5892 softc->action = PROBE_DV_EXIT; 5893 scsi_test_unit_ready(csio, 5894 /*retries*/4, 5895 probedone, 5896 MSG_SIMPLE_Q_TAG, 5897 SSD_FULL_SIZE, 5898 /*timeout*/60000); 5899 break; 5900 } 5901 scsi_inquiry(csio, 5902 /*retries*/4, 5903 probedone, 5904 MSG_SIMPLE_Q_TAG, 5905 (u_int8_t *)inq_buf, 5906 inquiry_len, 5907 /*evpd*/FALSE, 5908 /*page_code*/0, 5909 SSD_MIN_SIZE, 5910 /*timeout*/60 * 1000); 5911 break; 5912 } 5913 case PROBE_MODE_SENSE: 5914 { 5915 void *mode_buf; 5916 int mode_buf_len; 5917 5918 mode_buf_len = sizeof(struct scsi_mode_header_6) 5919 + sizeof(struct scsi_mode_blk_desc) 5920 + sizeof(struct scsi_control_page); 5921 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT); 5922 if (mode_buf != NULL) { 5923 scsi_mode_sense(csio, 5924 /*retries*/4, 5925 probedone, 5926 MSG_SIMPLE_Q_TAG, 5927 /*dbd*/FALSE, 5928 SMS_PAGE_CTRL_CURRENT, 5929 SMS_CONTROL_MODE_PAGE, 5930 mode_buf, 5931 mode_buf_len, 5932 SSD_FULL_SIZE, 5933 /*timeout*/60000); 5934 break; 5935 } 5936 xpt_print(periph->path, "Unable to mode sense control page - " 5937 "malloc failure\n"); 5938 softc->action = PROBE_SERIAL_NUM; 5939 } 5940 /* FALLTHROUGH */ 5941 case PROBE_SERIAL_NUM: 5942 { 5943 struct scsi_vpd_unit_serial_number *serial_buf; 5944 struct cam_ed* device; 5945 5946 serial_buf = NULL; 5947 device = periph->path->device; 5948 device->serial_num = NULL; 5949 device->serial_num_len = 0; 5950 5951 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0) 5952 serial_buf = (struct scsi_vpd_unit_serial_number *) 5953 malloc(sizeof(*serial_buf), M_TEMP, 5954 M_NOWAIT | M_ZERO); 5955 5956 if (serial_buf != NULL) { 5957 scsi_inquiry(csio, 5958 /*retries*/4, 5959 probedone, 5960 MSG_SIMPLE_Q_TAG, 5961 (u_int8_t *)serial_buf, 5962 sizeof(*serial_buf), 5963 /*evpd*/TRUE, 5964 SVPD_UNIT_SERIAL_NUMBER, 5965 SSD_MIN_SIZE, 5966 /*timeout*/60 * 1000); 5967 break; 5968 } 5969 /* 5970 * We'll have to do without, let our probedone 5971 * routine finish up for us. 5972 */ 5973 start_ccb->csio.data_ptr = NULL; 5974 probedone(periph, start_ccb); 5975 return; 5976 } 5977 } 5978 xpt_action(start_ccb); 5979 } 5980 5981 static void 5982 proberequestdefaultnegotiation(struct cam_periph *periph) 5983 { 5984 struct ccb_trans_settings cts; 5985 5986 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1); 5987 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 5988 cts.type = CTS_TYPE_USER_SETTINGS; 5989 xpt_action((union ccb *)&cts); 5990 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 5991 return; 5992 } 5993 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 5994 cts.type = CTS_TYPE_CURRENT_SETTINGS; 5995 xpt_action((union ccb *)&cts); 5996 } 5997 5998 /* 5999 * Backoff Negotiation Code- only pertinent for SPI devices. 6000 */ 6001 static int 6002 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device) 6003 { 6004 struct ccb_trans_settings cts; 6005 struct ccb_trans_settings_spi *spi; 6006 6007 memset(&cts, 0, sizeof (cts)); 6008 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1); 6009 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 6010 cts.type = CTS_TYPE_CURRENT_SETTINGS; 6011 xpt_action((union ccb *)&cts); 6012 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 6013 if (bootverbose) { 6014 xpt_print(periph->path, 6015 "failed to get current device settings\n"); 6016 } 6017 return (0); 6018 } 6019 if (cts.transport != XPORT_SPI) { 6020 if (bootverbose) { 6021 xpt_print(periph->path, "not SPI transport\n"); 6022 } 6023 return (0); 6024 } 6025 spi = &cts.xport_specific.spi; 6026 6027 /* 6028 * We cannot renegotiate sync rate if we don't have one. 6029 */ 6030 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) { 6031 if (bootverbose) { 6032 xpt_print(periph->path, "no sync rate known\n"); 6033 } 6034 return (0); 6035 } 6036 6037 /* 6038 * We'll assert that we don't have to touch PPR options- the 6039 * SIM will see what we do with period and offset and adjust 6040 * the PPR options as appropriate. 6041 */ 6042 6043 /* 6044 * A sync rate with unknown or zero offset is nonsensical. 6045 * A sync period of zero means Async. 6046 */ 6047 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0 6048 || spi->sync_offset == 0 || spi->sync_period == 0) { 6049 if (bootverbose) { 6050 xpt_print(periph->path, "no sync rate available\n"); 6051 } 6052 return (0); 6053 } 6054 6055 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) { 6056 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 6057 ("hit async: giving up on DV\n")); 6058 return (0); 6059 } 6060 6061 6062 /* 6063 * Jump sync_period up by one, but stop at 5MHz and fall back to Async. 6064 * We don't try to remember 'last' settings to see if the SIM actually 6065 * gets into the speed we want to set. We check on the SIM telling 6066 * us that a requested speed is bad, but otherwise don't try and 6067 * check the speed due to the asynchronous and handshake nature 6068 * of speed setting. 6069 */ 6070 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET; 6071 for (;;) { 6072 spi->sync_period++; 6073 if (spi->sync_period >= 0xf) { 6074 spi->sync_period = 0; 6075 spi->sync_offset = 0; 6076 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 6077 ("setting to async for DV\n")); 6078 /* 6079 * Once we hit async, we don't want to try 6080 * any more settings. 6081 */ 6082 device->flags |= CAM_DEV_DV_HIT_BOTTOM; 6083 } else if (bootverbose) { 6084 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 6085 ("DV: period 0x%x\n", spi->sync_period)); 6086 printf("setting period to 0x%x\n", spi->sync_period); 6087 } 6088 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 6089 cts.type = CTS_TYPE_CURRENT_SETTINGS; 6090 xpt_action((union ccb *)&cts); 6091 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 6092 break; 6093 } 6094 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 6095 ("DV: failed to set period 0x%x\n", spi->sync_period)); 6096 if (spi->sync_period == 0) { 6097 return (0); 6098 } 6099 } 6100 return (1); 6101 } 6102 6103 static void 6104 probedone(struct cam_periph *periph, union ccb *done_ccb) 6105 { 6106 probe_softc *softc; 6107 struct cam_path *path; 6108 u_int32_t priority; 6109 6110 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n")); 6111 6112 softc = (probe_softc *)periph->softc; 6113 path = done_ccb->ccb_h.path; 6114 priority = done_ccb->ccb_h.pinfo.priority; 6115 6116 switch (softc->action) { 6117 case PROBE_TUR: 6118 { 6119 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 6120 6121 if (cam_periph_error(done_ccb, 0, 6122 SF_NO_PRINT, NULL) == ERESTART) 6123 return; 6124 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 6125 /* Don't wedge the queue */ 6126 xpt_release_devq(done_ccb->ccb_h.path, 6127 /*count*/1, 6128 /*run_queue*/TRUE); 6129 } 6130 softc->action = PROBE_INQUIRY; 6131 xpt_release_ccb(done_ccb); 6132 xpt_schedule(periph, priority); 6133 return; 6134 } 6135 case PROBE_INQUIRY: 6136 case PROBE_FULL_INQUIRY: 6137 { 6138 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 6139 struct scsi_inquiry_data *inq_buf; 6140 u_int8_t periph_qual; 6141 6142 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID; 6143 inq_buf = &path->device->inq_data; 6144 6145 periph_qual = SID_QUAL(inq_buf); 6146 6147 switch(periph_qual) { 6148 case SID_QUAL_LU_CONNECTED: 6149 { 6150 u_int8_t len; 6151 6152 /* 6153 * We conservatively request only 6154 * SHORT_INQUIRY_LEN bytes of inquiry 6155 * information during our first try 6156 * at sending an INQUIRY. If the device 6157 * has more information to give, 6158 * perform a second request specifying 6159 * the amount of information the device 6160 * is willing to give. 6161 */ 6162 len = inq_buf->additional_length 6163 + offsetof(struct scsi_inquiry_data, 6164 additional_length) + 1; 6165 if (softc->action == PROBE_INQUIRY 6166 && len > SHORT_INQUIRY_LENGTH) { 6167 softc->action = PROBE_FULL_INQUIRY; 6168 xpt_release_ccb(done_ccb); 6169 xpt_schedule(periph, priority); 6170 return; 6171 } 6172 6173 xpt_find_quirk(path->device); 6174 6175 xpt_devise_transport(path); 6176 if (INQ_DATA_TQ_ENABLED(inq_buf)) 6177 softc->action = PROBE_MODE_SENSE; 6178 else 6179 softc->action = PROBE_SERIAL_NUM; 6180 6181 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 6182 6183 xpt_release_ccb(done_ccb); 6184 xpt_schedule(periph, priority); 6185 return; 6186 } 6187 default: 6188 break; 6189 } 6190 } else if (cam_periph_error(done_ccb, 0, 6191 done_ccb->ccb_h.target_lun > 0 6192 ? SF_RETRY_UA|SF_QUIET_IR 6193 : SF_RETRY_UA, 6194 &softc->saved_ccb) == ERESTART) { 6195 return; 6196 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 6197 /* Don't wedge the queue */ 6198 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 6199 /*run_queue*/TRUE); 6200 } 6201 /* 6202 * If we get to this point, we got an error status back 6203 * from the inquiry and the error status doesn't require 6204 * automatically retrying the command. Therefore, the 6205 * inquiry failed. If we had inquiry information before 6206 * for this device, but this latest inquiry command failed, 6207 * the device has probably gone away. If this device isn't 6208 * already marked unconfigured, notify the peripheral 6209 * drivers that this device is no more. 6210 */ 6211 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 6212 /* Send the async notification. */ 6213 xpt_async(AC_LOST_DEVICE, path, NULL); 6214 6215 xpt_release_ccb(done_ccb); 6216 break; 6217 } 6218 case PROBE_MODE_SENSE: 6219 { 6220 struct ccb_scsiio *csio; 6221 struct scsi_mode_header_6 *mode_hdr; 6222 6223 csio = &done_ccb->csio; 6224 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr; 6225 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 6226 struct scsi_control_page *page; 6227 u_int8_t *offset; 6228 6229 offset = ((u_int8_t *)&mode_hdr[1]) 6230 + mode_hdr->blk_desc_len; 6231 page = (struct scsi_control_page *)offset; 6232 path->device->queue_flags = page->queue_flags; 6233 } else if (cam_periph_error(done_ccb, 0, 6234 SF_RETRY_UA|SF_NO_PRINT, 6235 &softc->saved_ccb) == ERESTART) { 6236 return; 6237 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 6238 /* Don't wedge the queue */ 6239 xpt_release_devq(done_ccb->ccb_h.path, 6240 /*count*/1, /*run_queue*/TRUE); 6241 } 6242 xpt_release_ccb(done_ccb); 6243 free(mode_hdr, M_TEMP); 6244 softc->action = PROBE_SERIAL_NUM; 6245 xpt_schedule(periph, priority); 6246 return; 6247 } 6248 case PROBE_SERIAL_NUM: 6249 { 6250 struct ccb_scsiio *csio; 6251 struct scsi_vpd_unit_serial_number *serial_buf; 6252 u_int32_t priority; 6253 int changed; 6254 int have_serialnum; 6255 6256 changed = 1; 6257 have_serialnum = 0; 6258 csio = &done_ccb->csio; 6259 priority = done_ccb->ccb_h.pinfo.priority; 6260 serial_buf = 6261 (struct scsi_vpd_unit_serial_number *)csio->data_ptr; 6262 6263 /* Clean up from previous instance of this device */ 6264 if (path->device->serial_num != NULL) { 6265 free(path->device->serial_num, M_CAMXPT); 6266 path->device->serial_num = NULL; 6267 path->device->serial_num_len = 0; 6268 } 6269 6270 if (serial_buf == NULL) { 6271 /* 6272 * Don't process the command as it was never sent 6273 */ 6274 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP 6275 && (serial_buf->length > 0)) { 6276 6277 have_serialnum = 1; 6278 path->device->serial_num = 6279 (u_int8_t *)malloc((serial_buf->length + 1), 6280 M_CAMXPT, M_NOWAIT); 6281 if (path->device->serial_num != NULL) { 6282 bcopy(serial_buf->serial_num, 6283 path->device->serial_num, 6284 serial_buf->length); 6285 path->device->serial_num_len = 6286 serial_buf->length; 6287 path->device->serial_num[serial_buf->length] 6288 = '\0'; 6289 } 6290 } else if (cam_periph_error(done_ccb, 0, 6291 SF_RETRY_UA|SF_NO_PRINT, 6292 &softc->saved_ccb) == ERESTART) { 6293 return; 6294 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 6295 /* Don't wedge the queue */ 6296 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 6297 /*run_queue*/TRUE); 6298 } 6299 6300 /* 6301 * Let's see if we have seen this device before. 6302 */ 6303 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) { 6304 MD5_CTX context; 6305 u_int8_t digest[16]; 6306 6307 MD5Init(&context); 6308 6309 MD5Update(&context, 6310 (unsigned char *)&path->device->inq_data, 6311 sizeof(struct scsi_inquiry_data)); 6312 6313 if (have_serialnum) 6314 MD5Update(&context, serial_buf->serial_num, 6315 serial_buf->length); 6316 6317 MD5Final(digest, &context); 6318 if (bcmp(softc->digest, digest, 16) == 0) 6319 changed = 0; 6320 6321 /* 6322 * XXX Do we need to do a TUR in order to ensure 6323 * that the device really hasn't changed??? 6324 */ 6325 if ((changed != 0) 6326 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0)) 6327 xpt_async(AC_LOST_DEVICE, path, NULL); 6328 } 6329 if (serial_buf != NULL) 6330 free(serial_buf, M_TEMP); 6331 6332 if (changed != 0) { 6333 /* 6334 * Now that we have all the necessary 6335 * information to safely perform transfer 6336 * negotiations... Controllers don't perform 6337 * any negotiation or tagged queuing until 6338 * after the first XPT_SET_TRAN_SETTINGS ccb is 6339 * received. So, on a new device, just retrieve 6340 * the user settings, and set them as the current 6341 * settings to set the device up. 6342 */ 6343 proberequestdefaultnegotiation(periph); 6344 xpt_release_ccb(done_ccb); 6345 6346 /* 6347 * Perform a TUR to allow the controller to 6348 * perform any necessary transfer negotiation. 6349 */ 6350 softc->action = PROBE_TUR_FOR_NEGOTIATION; 6351 xpt_schedule(periph, priority); 6352 return; 6353 } 6354 xpt_release_ccb(done_ccb); 6355 break; 6356 } 6357 case PROBE_TUR_FOR_NEGOTIATION: 6358 case PROBE_DV_EXIT: 6359 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 6360 /* Don't wedge the queue */ 6361 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 6362 /*run_queue*/TRUE); 6363 } 6364 /* 6365 * Do Domain Validation for lun 0 on devices that claim 6366 * to support Synchronous Transfer modes. 6367 */ 6368 if (softc->action == PROBE_TUR_FOR_NEGOTIATION 6369 && done_ccb->ccb_h.target_lun == 0 6370 && (path->device->inq_data.flags & SID_Sync) != 0 6371 && (path->device->flags & CAM_DEV_IN_DV) == 0) { 6372 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 6373 ("Begin Domain Validation\n")); 6374 path->device->flags |= CAM_DEV_IN_DV; 6375 xpt_release_ccb(done_ccb); 6376 softc->action = PROBE_INQUIRY_BASIC_DV1; 6377 xpt_schedule(periph, priority); 6378 return; 6379 } 6380 if (softc->action == PROBE_DV_EXIT) { 6381 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 6382 ("Leave Domain Validation\n")); 6383 } 6384 path->device->flags &= 6385 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM); 6386 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) { 6387 /* Inform the XPT that a new device has been found */ 6388 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 6389 xpt_action(done_ccb); 6390 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 6391 done_ccb); 6392 } 6393 xpt_release_ccb(done_ccb); 6394 break; 6395 case PROBE_INQUIRY_BASIC_DV1: 6396 case PROBE_INQUIRY_BASIC_DV2: 6397 { 6398 struct scsi_inquiry_data *nbuf; 6399 struct ccb_scsiio *csio; 6400 6401 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 6402 /* Don't wedge the queue */ 6403 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 6404 /*run_queue*/TRUE); 6405 } 6406 csio = &done_ccb->csio; 6407 nbuf = (struct scsi_inquiry_data *)csio->data_ptr; 6408 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) { 6409 xpt_print(path, 6410 "inquiry data fails comparison at DV%d step\n", 6411 softc->action == PROBE_INQUIRY_BASIC_DV1? 1 : 2); 6412 if (proberequestbackoff(periph, path->device)) { 6413 path->device->flags &= ~CAM_DEV_IN_DV; 6414 softc->action = PROBE_TUR_FOR_NEGOTIATION; 6415 } else { 6416 /* give up */ 6417 softc->action = PROBE_DV_EXIT; 6418 } 6419 free(nbuf, M_TEMP); 6420 xpt_release_ccb(done_ccb); 6421 xpt_schedule(periph, priority); 6422 return; 6423 } 6424 free(nbuf, M_TEMP); 6425 if (softc->action == PROBE_INQUIRY_BASIC_DV1) { 6426 softc->action = PROBE_INQUIRY_BASIC_DV2; 6427 xpt_release_ccb(done_ccb); 6428 xpt_schedule(periph, priority); 6429 return; 6430 } 6431 if (softc->action == PROBE_DV_EXIT) { 6432 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 6433 ("Leave Domain Validation Successfully\n")); 6434 } 6435 path->device->flags &= 6436 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM); 6437 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) { 6438 /* Inform the XPT that a new device has been found */ 6439 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 6440 xpt_action(done_ccb); 6441 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 6442 done_ccb); 6443 } 6444 xpt_release_ccb(done_ccb); 6445 break; 6446 } 6447 } 6448 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 6449 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe); 6450 done_ccb->ccb_h.status = CAM_REQ_CMP; 6451 xpt_done(done_ccb); 6452 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) { 6453 cam_periph_invalidate(periph); 6454 cam_periph_release(periph); 6455 } else { 6456 probeschedule(periph); 6457 } 6458 } 6459 6460 static void 6461 probecleanup(struct cam_periph *periph) 6462 { 6463 free(periph->softc, M_TEMP); 6464 } 6465 6466 static void 6467 xpt_find_quirk(struct cam_ed *device) 6468 { 6469 caddr_t match; 6470 6471 match = cam_quirkmatch((caddr_t)&device->inq_data, 6472 (caddr_t)xpt_quirk_table, 6473 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table), 6474 sizeof(*xpt_quirk_table), scsi_inquiry_match); 6475 6476 if (match == NULL) 6477 panic("xpt_find_quirk: device didn't match wildcard entry!!"); 6478 6479 device->quirk = (struct xpt_quirk_entry *)match; 6480 } 6481 6482 static int 6483 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS) 6484 { 6485 int error, bool; 6486 6487 bool = cam_srch_hi; 6488 error = sysctl_handle_int(oidp, &bool, sizeof(bool), req); 6489 if (error != 0 || req->newptr == NULL) 6490 return (error); 6491 if (bool == 0 || bool == 1) { 6492 cam_srch_hi = bool; 6493 return (0); 6494 } else { 6495 return (EINVAL); 6496 } 6497 } 6498 6499 6500 static void 6501 xpt_devise_transport(struct cam_path *path) 6502 { 6503 struct ccb_pathinq cpi; 6504 struct ccb_trans_settings cts; 6505 struct scsi_inquiry_data *inq_buf; 6506 6507 /* Get transport information from the SIM */ 6508 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1); 6509 cpi.ccb_h.func_code = XPT_PATH_INQ; 6510 xpt_action((union ccb *)&cpi); 6511 6512 inq_buf = NULL; 6513 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) 6514 inq_buf = &path->device->inq_data; 6515 path->device->protocol = PROTO_SCSI; 6516 path->device->protocol_version = 6517 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version; 6518 path->device->transport = cpi.transport; 6519 path->device->transport_version = cpi.transport_version; 6520 6521 /* 6522 * Any device not using SPI3 features should 6523 * be considered SPI2 or lower. 6524 */ 6525 if (inq_buf != NULL) { 6526 if (path->device->transport == XPORT_SPI 6527 && (inq_buf->spi3data & SID_SPI_MASK) == 0 6528 && path->device->transport_version > 2) 6529 path->device->transport_version = 2; 6530 } else { 6531 struct cam_ed* otherdev; 6532 6533 for (otherdev = TAILQ_FIRST(&path->target->ed_entries); 6534 otherdev != NULL; 6535 otherdev = TAILQ_NEXT(otherdev, links)) { 6536 if (otherdev != path->device) 6537 break; 6538 } 6539 6540 if (otherdev != NULL) { 6541 /* 6542 * Initially assume the same versioning as 6543 * prior luns for this target. 6544 */ 6545 path->device->protocol_version = 6546 otherdev->protocol_version; 6547 path->device->transport_version = 6548 otherdev->transport_version; 6549 } else { 6550 /* Until we know better, opt for safty */ 6551 path->device->protocol_version = 2; 6552 if (path->device->transport == XPORT_SPI) 6553 path->device->transport_version = 2; 6554 else 6555 path->device->transport_version = 0; 6556 } 6557 } 6558 6559 /* 6560 * XXX 6561 * For a device compliant with SPC-2 we should be able 6562 * to determine the transport version supported by 6563 * scrutinizing the version descriptors in the 6564 * inquiry buffer. 6565 */ 6566 6567 /* Tell the controller what we think */ 6568 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1); 6569 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 6570 cts.type = CTS_TYPE_CURRENT_SETTINGS; 6571 cts.transport = path->device->transport; 6572 cts.transport_version = path->device->transport_version; 6573 cts.protocol = path->device->protocol; 6574 cts.protocol_version = path->device->protocol_version; 6575 cts.proto_specific.valid = 0; 6576 cts.xport_specific.valid = 0; 6577 xpt_action((union ccb *)&cts); 6578 } 6579 6580 static void 6581 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device, 6582 int async_update) 6583 { 6584 struct ccb_pathinq cpi; 6585 struct ccb_trans_settings cur_cts; 6586 struct ccb_trans_settings_scsi *scsi; 6587 struct ccb_trans_settings_scsi *cur_scsi; 6588 struct cam_sim *sim; 6589 struct scsi_inquiry_data *inq_data; 6590 6591 if (device == NULL) { 6592 cts->ccb_h.status = CAM_PATH_INVALID; 6593 xpt_done((union ccb *)cts); 6594 return; 6595 } 6596 6597 if (cts->protocol == PROTO_UNKNOWN 6598 || cts->protocol == PROTO_UNSPECIFIED) { 6599 cts->protocol = device->protocol; 6600 cts->protocol_version = device->protocol_version; 6601 } 6602 6603 if (cts->protocol_version == PROTO_VERSION_UNKNOWN 6604 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED) 6605 cts->protocol_version = device->protocol_version; 6606 6607 if (cts->protocol != device->protocol) { 6608 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n", 6609 cts->protocol, device->protocol); 6610 cts->protocol = device->protocol; 6611 } 6612 6613 if (cts->protocol_version > device->protocol_version) { 6614 if (bootverbose) { 6615 xpt_print(cts->ccb_h.path, "Down reving Protocol " 6616 "Version from %d to %d?\n", cts->protocol_version, 6617 device->protocol_version); 6618 } 6619 cts->protocol_version = device->protocol_version; 6620 } 6621 6622 if (cts->transport == XPORT_UNKNOWN 6623 || cts->transport == XPORT_UNSPECIFIED) { 6624 cts->transport = device->transport; 6625 cts->transport_version = device->transport_version; 6626 } 6627 6628 if (cts->transport_version == XPORT_VERSION_UNKNOWN 6629 || cts->transport_version == XPORT_VERSION_UNSPECIFIED) 6630 cts->transport_version = device->transport_version; 6631 6632 if (cts->transport != device->transport) { 6633 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n", 6634 cts->transport, device->transport); 6635 cts->transport = device->transport; 6636 } 6637 6638 if (cts->transport_version > device->transport_version) { 6639 if (bootverbose) { 6640 xpt_print(cts->ccb_h.path, "Down reving Transport " 6641 "Version from %d to %d?\n", cts->transport_version, 6642 device->transport_version); 6643 } 6644 cts->transport_version = device->transport_version; 6645 } 6646 6647 sim = cts->ccb_h.path->bus->sim; 6648 6649 /* 6650 * Nothing more of interest to do unless 6651 * this is a device connected via the 6652 * SCSI protocol. 6653 */ 6654 if (cts->protocol != PROTO_SCSI) { 6655 if (async_update == FALSE) 6656 (*(sim->sim_action))(sim, (union ccb *)cts); 6657 return; 6658 } 6659 6660 inq_data = &device->inq_data; 6661 scsi = &cts->proto_specific.scsi; 6662 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1); 6663 cpi.ccb_h.func_code = XPT_PATH_INQ; 6664 xpt_action((union ccb *)&cpi); 6665 6666 /* SCSI specific sanity checking */ 6667 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0 6668 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0 6669 || (device->queue_flags & SCP_QUEUE_DQUE) != 0 6670 || (device->quirk->mintags == 0)) { 6671 /* 6672 * Can't tag on hardware that doesn't support tags, 6673 * doesn't have it enabled, or has broken tag support. 6674 */ 6675 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 6676 } 6677 6678 if (async_update == FALSE) { 6679 /* 6680 * Perform sanity checking against what the 6681 * controller and device can do. 6682 */ 6683 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1); 6684 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 6685 cur_cts.type = cts->type; 6686 xpt_action((union ccb *)&cur_cts); 6687 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 6688 return; 6689 } 6690 cur_scsi = &cur_cts.proto_specific.scsi; 6691 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) { 6692 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 6693 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB; 6694 } 6695 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0) 6696 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 6697 } 6698 6699 /* SPI specific sanity checking */ 6700 if (cts->transport == XPORT_SPI && async_update == FALSE) { 6701 u_int spi3caps; 6702 struct ccb_trans_settings_spi *spi; 6703 struct ccb_trans_settings_spi *cur_spi; 6704 6705 spi = &cts->xport_specific.spi; 6706 6707 cur_spi = &cur_cts.xport_specific.spi; 6708 6709 /* Fill in any gaps in what the user gave us */ 6710 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) 6711 spi->sync_period = cur_spi->sync_period; 6712 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) 6713 spi->sync_period = 0; 6714 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) 6715 spi->sync_offset = cur_spi->sync_offset; 6716 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) 6717 spi->sync_offset = 0; 6718 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) 6719 spi->ppr_options = cur_spi->ppr_options; 6720 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) 6721 spi->ppr_options = 0; 6722 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0) 6723 spi->bus_width = cur_spi->bus_width; 6724 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0) 6725 spi->bus_width = 0; 6726 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) { 6727 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB; 6728 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB; 6729 } 6730 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0) 6731 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB; 6732 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0 6733 && (inq_data->flags & SID_Sync) == 0 6734 && cts->type == CTS_TYPE_CURRENT_SETTINGS) 6735 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) 6736 || (spi->sync_offset == 0) 6737 || (spi->sync_period == 0)) { 6738 /* Force async */ 6739 spi->sync_period = 0; 6740 spi->sync_offset = 0; 6741 } 6742 6743 switch (spi->bus_width) { 6744 case MSG_EXT_WDTR_BUS_32_BIT: 6745 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0 6746 || (inq_data->flags & SID_WBus32) != 0 6747 || cts->type == CTS_TYPE_USER_SETTINGS) 6748 && (cpi.hba_inquiry & PI_WIDE_32) != 0) 6749 break; 6750 /* Fall Through to 16-bit */ 6751 case MSG_EXT_WDTR_BUS_16_BIT: 6752 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0 6753 || (inq_data->flags & SID_WBus16) != 0 6754 || cts->type == CTS_TYPE_USER_SETTINGS) 6755 && (cpi.hba_inquiry & PI_WIDE_16) != 0) { 6756 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT; 6757 break; 6758 } 6759 /* Fall Through to 8-bit */ 6760 default: /* New bus width?? */ 6761 case MSG_EXT_WDTR_BUS_8_BIT: 6762 /* All targets can do this */ 6763 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT; 6764 break; 6765 } 6766 6767 spi3caps = cpi.xport_specific.spi.ppr_options; 6768 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0 6769 && cts->type == CTS_TYPE_CURRENT_SETTINGS) 6770 spi3caps &= inq_data->spi3data; 6771 6772 if ((spi3caps & SID_SPI_CLOCK_DT) == 0) 6773 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ; 6774 6775 if ((spi3caps & SID_SPI_IUS) == 0) 6776 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ; 6777 6778 if ((spi3caps & SID_SPI_QAS) == 0) 6779 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ; 6780 6781 /* No SPI Transfer settings are allowed unless we are wide */ 6782 if (spi->bus_width == 0) 6783 spi->ppr_options = 0; 6784 6785 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) { 6786 /* 6787 * Can't tag queue without disconnection. 6788 */ 6789 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 6790 scsi->valid |= CTS_SCSI_VALID_TQ; 6791 } 6792 6793 /* 6794 * If we are currently performing tagged transactions to 6795 * this device and want to change its negotiation parameters, 6796 * go non-tagged for a bit to give the controller a chance to 6797 * negotiate unhampered by tag messages. 6798 */ 6799 if (cts->type == CTS_TYPE_CURRENT_SETTINGS 6800 && (device->inq_flags & SID_CmdQue) != 0 6801 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0 6802 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE| 6803 CTS_SPI_VALID_SYNC_OFFSET| 6804 CTS_SPI_VALID_BUS_WIDTH)) != 0) 6805 xpt_toggle_tags(cts->ccb_h.path); 6806 } 6807 6808 if (cts->type == CTS_TYPE_CURRENT_SETTINGS 6809 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) { 6810 int device_tagenb; 6811 6812 /* 6813 * If we are transitioning from tags to no-tags or 6814 * vice-versa, we need to carefully freeze and restart 6815 * the queue so that we don't overlap tagged and non-tagged 6816 * commands. We also temporarily stop tags if there is 6817 * a change in transfer negotiation settings to allow 6818 * "tag-less" negotiation. 6819 */ 6820 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 6821 || (device->inq_flags & SID_CmdQue) != 0) 6822 device_tagenb = TRUE; 6823 else 6824 device_tagenb = FALSE; 6825 6826 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0 6827 && device_tagenb == FALSE) 6828 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0 6829 && device_tagenb == TRUE)) { 6830 6831 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) { 6832 /* 6833 * Delay change to use tags until after a 6834 * few commands have gone to this device so 6835 * the controller has time to perform transfer 6836 * negotiations without tagged messages getting 6837 * in the way. 6838 */ 6839 device->tag_delay_count = CAM_TAG_DELAY_COUNT; 6840 device->flags |= CAM_DEV_TAG_AFTER_COUNT; 6841 } else { 6842 struct ccb_relsim crs; 6843 6844 xpt_freeze_devq(cts->ccb_h.path, /*count*/1); 6845 device->inq_flags &= ~SID_CmdQue; 6846 xpt_dev_ccbq_resize(cts->ccb_h.path, 6847 sim->max_dev_openings); 6848 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 6849 device->tag_delay_count = 0; 6850 6851 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path, 6852 /*priority*/1); 6853 crs.ccb_h.func_code = XPT_REL_SIMQ; 6854 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 6855 crs.openings 6856 = crs.release_timeout 6857 = crs.qfrozen_cnt 6858 = 0; 6859 xpt_action((union ccb *)&crs); 6860 } 6861 } 6862 } 6863 if (async_update == FALSE) 6864 (*(sim->sim_action))(sim, (union ccb *)cts); 6865 } 6866 6867 6868 static void 6869 xpt_toggle_tags(struct cam_path *path) 6870 { 6871 struct cam_ed *dev; 6872 6873 /* 6874 * Give controllers a chance to renegotiate 6875 * before starting tag operations. We 6876 * "toggle" tagged queuing off then on 6877 * which causes the tag enable command delay 6878 * counter to come into effect. 6879 */ 6880 dev = path->device; 6881 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 6882 || ((dev->inq_flags & SID_CmdQue) != 0 6883 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) { 6884 struct ccb_trans_settings cts; 6885 6886 xpt_setup_ccb(&cts.ccb_h, path, 1); 6887 cts.protocol = PROTO_SCSI; 6888 cts.protocol_version = PROTO_VERSION_UNSPECIFIED; 6889 cts.transport = XPORT_UNSPECIFIED; 6890 cts.transport_version = XPORT_VERSION_UNSPECIFIED; 6891 cts.proto_specific.scsi.flags = 0; 6892 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ; 6893 xpt_set_transfer_settings(&cts, path->device, 6894 /*async_update*/TRUE); 6895 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB; 6896 xpt_set_transfer_settings(&cts, path->device, 6897 /*async_update*/TRUE); 6898 } 6899 } 6900 6901 static void 6902 xpt_start_tags(struct cam_path *path) 6903 { 6904 struct ccb_relsim crs; 6905 struct cam_ed *device; 6906 struct cam_sim *sim; 6907 int newopenings; 6908 6909 device = path->device; 6910 sim = path->bus->sim; 6911 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 6912 xpt_freeze_devq(path, /*count*/1); 6913 device->inq_flags |= SID_CmdQue; 6914 if (device->tag_saved_openings != 0) 6915 newopenings = device->tag_saved_openings; 6916 else 6917 newopenings = min(device->quirk->maxtags, 6918 sim->max_tagged_dev_openings); 6919 xpt_dev_ccbq_resize(path, newopenings); 6920 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1); 6921 crs.ccb_h.func_code = XPT_REL_SIMQ; 6922 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 6923 crs.openings 6924 = crs.release_timeout 6925 = crs.qfrozen_cnt 6926 = 0; 6927 xpt_action((union ccb *)&crs); 6928 } 6929 6930 static int busses_to_config; 6931 static int busses_to_reset; 6932 6933 static int 6934 xptconfigbuscountfunc(struct cam_eb *bus, void *arg) 6935 { 6936 6937 mtx_assert(bus->sim->mtx, MA_OWNED); 6938 6939 if (bus->path_id != CAM_XPT_PATH_ID) { 6940 struct cam_path path; 6941 struct ccb_pathinq cpi; 6942 int can_negotiate; 6943 6944 busses_to_config++; 6945 xpt_compile_path(&path, NULL, bus->path_id, 6946 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 6947 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1); 6948 cpi.ccb_h.func_code = XPT_PATH_INQ; 6949 xpt_action((union ccb *)&cpi); 6950 can_negotiate = cpi.hba_inquiry; 6951 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE); 6952 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0 6953 && can_negotiate) 6954 busses_to_reset++; 6955 xpt_release_path(&path); 6956 } 6957 6958 return(1); 6959 } 6960 6961 static int 6962 xptconfigfunc(struct cam_eb *bus, void *arg) 6963 { 6964 struct cam_path *path; 6965 union ccb *work_ccb; 6966 6967 mtx_assert(bus->sim->mtx, MA_OWNED); 6968 6969 if (bus->path_id != CAM_XPT_PATH_ID) { 6970 cam_status status; 6971 int can_negotiate; 6972 6973 work_ccb = xpt_alloc_ccb_nowait(bus->sim); 6974 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id, 6975 CAM_TARGET_WILDCARD, 6976 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){ 6977 printf("xptconfigfunc: xpt_create_path failed with " 6978 "status %#x for bus %d\n", status, bus->path_id); 6979 printf("xptconfigfunc: halting bus configuration\n"); 6980 xpt_free_ccb(work_ccb); 6981 busses_to_config--; 6982 xpt_finishconfig(xpt_periph, NULL); 6983 return(0); 6984 } 6985 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1); 6986 work_ccb->ccb_h.func_code = XPT_PATH_INQ; 6987 xpt_action(work_ccb); 6988 if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 6989 printf("xptconfigfunc: CPI failed on bus %d " 6990 "with status %d\n", bus->path_id, 6991 work_ccb->ccb_h.status); 6992 xpt_finishconfig(xpt_periph, work_ccb); 6993 return(1); 6994 } 6995 6996 can_negotiate = work_ccb->cpi.hba_inquiry; 6997 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE); 6998 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0 6999 && (can_negotiate != 0)) { 7000 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1); 7001 work_ccb->ccb_h.func_code = XPT_RESET_BUS; 7002 work_ccb->ccb_h.cbfcnp = NULL; 7003 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE, 7004 ("Resetting Bus\n")); 7005 xpt_action(work_ccb); 7006 xpt_finishconfig(xpt_periph, work_ccb); 7007 } else { 7008 /* Act as though we performed a successful BUS RESET */ 7009 work_ccb->ccb_h.func_code = XPT_RESET_BUS; 7010 xpt_finishconfig(xpt_periph, work_ccb); 7011 } 7012 } 7013 7014 return(1); 7015 } 7016 7017 static void 7018 xpt_config(void *arg) 7019 { 7020 /* 7021 * Now that interrupts are enabled, go find our devices 7022 */ 7023 7024 #ifdef CAMDEBUG 7025 /* Setup debugging flags and path */ 7026 #ifdef CAM_DEBUG_FLAGS 7027 cam_dflags = CAM_DEBUG_FLAGS; 7028 #else /* !CAM_DEBUG_FLAGS */ 7029 cam_dflags = CAM_DEBUG_NONE; 7030 #endif /* CAM_DEBUG_FLAGS */ 7031 #ifdef CAM_DEBUG_BUS 7032 if (cam_dflags != CAM_DEBUG_NONE) { 7033 /* 7034 * Locking is specifically omitted here. No SIMs have 7035 * registered yet, so xpt_create_path will only be searching 7036 * empty lists of targets and devices. 7037 */ 7038 if (xpt_create_path(&cam_dpath, xpt_periph, 7039 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, 7040 CAM_DEBUG_LUN) != CAM_REQ_CMP) { 7041 printf("xpt_config: xpt_create_path() failed for debug" 7042 " target %d:%d:%d, debugging disabled\n", 7043 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN); 7044 cam_dflags = CAM_DEBUG_NONE; 7045 } 7046 } else 7047 cam_dpath = NULL; 7048 #else /* !CAM_DEBUG_BUS */ 7049 cam_dpath = NULL; 7050 #endif /* CAM_DEBUG_BUS */ 7051 #endif /* CAMDEBUG */ 7052 7053 /* 7054 * Scan all installed busses. 7055 */ 7056 xpt_for_all_busses(xptconfigbuscountfunc, NULL); 7057 7058 if (busses_to_config == 0) { 7059 /* Call manually because we don't have any busses */ 7060 xpt_finishconfig(xpt_periph, NULL); 7061 } else { 7062 if (busses_to_reset > 0 && scsi_delay >= 2000) { 7063 printf("Waiting %d seconds for SCSI " 7064 "devices to settle\n", scsi_delay/1000); 7065 } 7066 xpt_for_all_busses(xptconfigfunc, NULL); 7067 } 7068 } 7069 7070 /* 7071 * If the given device only has one peripheral attached to it, and if that 7072 * peripheral is the passthrough driver, announce it. This insures that the 7073 * user sees some sort of announcement for every peripheral in their system. 7074 */ 7075 static int 7076 xptpassannouncefunc(struct cam_ed *device, void *arg) 7077 { 7078 struct cam_periph *periph; 7079 int i; 7080 7081 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL; 7082 periph = SLIST_NEXT(periph, periph_links), i++); 7083 7084 periph = SLIST_FIRST(&device->periphs); 7085 if ((i == 1) 7086 && (strncmp(periph->periph_name, "pass", 4) == 0)) 7087 xpt_announce_periph(periph, NULL); 7088 7089 return(1); 7090 } 7091 7092 static void 7093 xpt_finishconfig_task(void *context, int pending) 7094 { 7095 struct periph_driver **p_drv; 7096 int i; 7097 7098 if (busses_to_config == 0) { 7099 /* Register all the peripheral drivers */ 7100 /* XXX This will have to change when we have loadable modules */ 7101 p_drv = periph_drivers; 7102 for (i = 0; p_drv[i] != NULL; i++) { 7103 (*p_drv[i]->init)(); 7104 } 7105 7106 /* 7107 * Check for devices with no "standard" peripheral driver 7108 * attached. For any devices like that, announce the 7109 * passthrough driver so the user will see something. 7110 */ 7111 xpt_for_all_devices(xptpassannouncefunc, NULL); 7112 7113 /* Release our hook so that the boot can continue. */ 7114 config_intrhook_disestablish(xsoftc.xpt_config_hook); 7115 free(xsoftc.xpt_config_hook, M_TEMP); 7116 xsoftc.xpt_config_hook = NULL; 7117 } 7118 7119 free(context, M_CAMXPT); 7120 } 7121 7122 static void 7123 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb) 7124 { 7125 struct xpt_task *task; 7126 7127 if (done_ccb != NULL) { 7128 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, 7129 ("xpt_finishconfig\n")); 7130 switch(done_ccb->ccb_h.func_code) { 7131 case XPT_RESET_BUS: 7132 if (done_ccb->ccb_h.status == CAM_REQ_CMP) { 7133 done_ccb->ccb_h.func_code = XPT_SCAN_BUS; 7134 done_ccb->ccb_h.cbfcnp = xpt_finishconfig; 7135 done_ccb->crcn.flags = 0; 7136 xpt_action(done_ccb); 7137 return; 7138 } 7139 /* FALLTHROUGH */ 7140 case XPT_SCAN_BUS: 7141 default: 7142 xpt_free_path(done_ccb->ccb_h.path); 7143 busses_to_config--; 7144 break; 7145 } 7146 } 7147 7148 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT); 7149 if (task != NULL) { 7150 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task); 7151 taskqueue_enqueue(taskqueue_thread, &task->task); 7152 } 7153 7154 if (done_ccb != NULL) 7155 xpt_free_ccb(done_ccb); 7156 } 7157 7158 static void 7159 xptaction(struct cam_sim *sim, union ccb *work_ccb) 7160 { 7161 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n")); 7162 7163 switch (work_ccb->ccb_h.func_code) { 7164 /* Common cases first */ 7165 case XPT_PATH_INQ: /* Path routing inquiry */ 7166 { 7167 struct ccb_pathinq *cpi; 7168 7169 cpi = &work_ccb->cpi; 7170 cpi->version_num = 1; /* XXX??? */ 7171 cpi->hba_inquiry = 0; 7172 cpi->target_sprt = 0; 7173 cpi->hba_misc = 0; 7174 cpi->hba_eng_cnt = 0; 7175 cpi->max_target = 0; 7176 cpi->max_lun = 0; 7177 cpi->initiator_id = 0; 7178 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 7179 strncpy(cpi->hba_vid, "", HBA_IDLEN); 7180 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 7181 cpi->unit_number = sim->unit_number; 7182 cpi->bus_id = sim->bus_id; 7183 cpi->base_transfer_speed = 0; 7184 cpi->protocol = PROTO_UNSPECIFIED; 7185 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; 7186 cpi->transport = XPORT_UNSPECIFIED; 7187 cpi->transport_version = XPORT_VERSION_UNSPECIFIED; 7188 cpi->ccb_h.status = CAM_REQ_CMP; 7189 xpt_done(work_ccb); 7190 break; 7191 } 7192 default: 7193 work_ccb->ccb_h.status = CAM_REQ_INVALID; 7194 xpt_done(work_ccb); 7195 break; 7196 } 7197 } 7198 7199 /* 7200 * The xpt as a "controller" has no interrupt sources, so polling 7201 * is a no-op. 7202 */ 7203 static void 7204 xptpoll(struct cam_sim *sim) 7205 { 7206 } 7207 7208 void 7209 xpt_lock_buses(void) 7210 { 7211 mtx_lock(&xsoftc.xpt_topo_lock); 7212 } 7213 7214 void 7215 xpt_unlock_buses(void) 7216 { 7217 mtx_unlock(&xsoftc.xpt_topo_lock); 7218 } 7219 7220 static void 7221 camisr(void *V_queue) 7222 { 7223 cam_isrq_t *oqueue = V_queue; 7224 cam_isrq_t queue; 7225 int s; 7226 struct ccb_hdr *ccb_h; 7227 struct cam_sim *sim; 7228 7229 /* 7230 * Transfer the ccb_bioq list to a temporary list so we can operate 7231 * on it without needing to lock/unlock on every loop. The concat 7232 * function with re-init the real list for us. 7233 */ 7234 s = splcam(); 7235 mtx_lock(&cam_bioq_lock); 7236 TAILQ_INIT(&queue); 7237 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe); 7238 mtx_unlock(&cam_bioq_lock); 7239 7240 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) { 7241 int runq; 7242 7243 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe); 7244 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX; 7245 splx(s); 7246 7247 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE, 7248 ("camisr\n")); 7249 7250 runq = FALSE; 7251 7252 if (ccb_h->flags & CAM_HIGH_POWER) { 7253 struct highpowerlist *hphead; 7254 union ccb *send_ccb; 7255 7256 mtx_lock(&xsoftc.xpt_lock); 7257 hphead = &xsoftc.highpowerq; 7258 7259 send_ccb = (union ccb *)STAILQ_FIRST(hphead); 7260 7261 /* 7262 * Increment the count since this command is done. 7263 */ 7264 xsoftc.num_highpower++; 7265 7266 /* 7267 * Any high powered commands queued up? 7268 */ 7269 if (send_ccb != NULL) { 7270 7271 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe); 7272 mtx_unlock(&xsoftc.xpt_lock); 7273 7274 xpt_release_devq(send_ccb->ccb_h.path, 7275 /*count*/1, /*runqueue*/TRUE); 7276 } else 7277 mtx_unlock(&xsoftc.xpt_lock); 7278 } 7279 7280 sim = ccb_h->path->bus->sim; 7281 mtx_lock(sim->mtx); 7282 7283 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) { 7284 struct cam_ed *dev; 7285 7286 dev = ccb_h->path->device; 7287 7288 s = splcam(); 7289 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h); 7290 7291 if (!SIM_DEAD(ccb_h->path->bus->sim)) { 7292 ccb_h->path->bus->sim->devq->send_active--; 7293 ccb_h->path->bus->sim->devq->send_openings++; 7294 } 7295 splx(s); 7296 7297 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0 7298 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ) 7299 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0 7300 && (dev->ccbq.dev_active == 0))) { 7301 7302 xpt_release_devq(ccb_h->path, /*count*/1, 7303 /*run_queue*/TRUE); 7304 } 7305 7306 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 7307 && (--dev->tag_delay_count == 0)) 7308 xpt_start_tags(ccb_h->path); 7309 7310 if ((dev->ccbq.queue.entries > 0) 7311 && (dev->qfrozen_cnt == 0) 7312 && (device_is_send_queued(dev) == 0)) { 7313 runq = xpt_schedule_dev_sendq(ccb_h->path->bus, 7314 dev); 7315 } 7316 } 7317 7318 if (ccb_h->status & CAM_RELEASE_SIMQ) { 7319 xpt_release_simq(ccb_h->path->bus->sim, 7320 /*run_queue*/TRUE); 7321 ccb_h->status &= ~CAM_RELEASE_SIMQ; 7322 runq = FALSE; 7323 } 7324 7325 if ((ccb_h->flags & CAM_DEV_QFRZDIS) 7326 && (ccb_h->status & CAM_DEV_QFRZN)) { 7327 xpt_release_devq(ccb_h->path, /*count*/1, 7328 /*run_queue*/TRUE); 7329 ccb_h->status &= ~CAM_DEV_QFRZN; 7330 } else if (runq) { 7331 xpt_run_dev_sendq(ccb_h->path->bus); 7332 } 7333 7334 /* Call the peripheral driver's callback */ 7335 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h); 7336 7337 /* Raise IPL for while test */ 7338 mtx_unlock(sim->mtx); 7339 s = splcam(); 7340 } 7341 splx(s); 7342 } 7343 7344 static void 7345 dead_sim_action(struct cam_sim *sim, union ccb *ccb) 7346 { 7347 7348 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 7349 xpt_done(ccb); 7350 } 7351 7352 static void 7353 dead_sim_poll(struct cam_sim *sim) 7354 { 7355 } 7356