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