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