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