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