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