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