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.6 2003/08/07 21:16:44 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; 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; 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 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items; 1177 *p_drv != NULL; p_drv++) 1178 if (strcmp((*p_drv)->driver_name, name) == 0) 1179 break; 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 for (pdrv = 2338 (struct periph_driver **)periphdriver_set.ls_items; 2339 *pdrv != NULL; pdrv++) { 2340 if (strcmp((*pdrv)->driver_name, 2341 periph->periph_name) == 0) 2342 break; 2343 } 2344 2345 if (pdrv == NULL) { 2346 cdm->status = CAM_DEV_MATCH_ERROR; 2347 return(0); 2348 } 2349 2350 cdm->pos.cookie.pdrv = pdrv; 2351 /* 2352 * The periph generation slot does double duty, as 2353 * does the periph pointer slot. They are used for 2354 * both edt and pdrv lookups and positioning. 2355 */ 2356 cdm->pos.cookie.periph = periph; 2357 cdm->pos.generations[CAM_PERIPH_GENERATION] = 2358 (*pdrv)->generation; 2359 cdm->status = CAM_DEV_MATCH_MORE; 2360 return(0); 2361 } 2362 2363 j = cdm->num_matches; 2364 cdm->num_matches++; 2365 cdm->matches[j].type = DEV_MATCH_PERIPH; 2366 cdm->matches[j].result.periph_result.path_id = 2367 periph->path->bus->path_id; 2368 2369 /* 2370 * The transport layer peripheral doesn't have a target or 2371 * lun. 2372 */ 2373 if (periph->path->target) 2374 cdm->matches[j].result.periph_result.target_id = 2375 periph->path->target->target_id; 2376 else 2377 cdm->matches[j].result.periph_result.target_id = -1; 2378 2379 if (periph->path->device) 2380 cdm->matches[j].result.periph_result.target_lun = 2381 periph->path->device->lun_id; 2382 else 2383 cdm->matches[j].result.periph_result.target_lun = -1; 2384 2385 cdm->matches[j].result.periph_result.unit_number = 2386 periph->unit_number; 2387 strncpy(cdm->matches[j].result.periph_result.periph_name, 2388 periph->periph_name, DEV_IDLEN); 2389 } 2390 2391 return(1); 2392 } 2393 2394 static int 2395 xptperiphlistmatch(struct ccb_dev_match *cdm) 2396 { 2397 int ret; 2398 2399 cdm->num_matches = 0; 2400 2401 /* 2402 * At this point in the edt traversal function, we check the bus 2403 * list generation to make sure that no busses have been added or 2404 * removed since the user last sent a XPT_DEV_MATCH ccb through. 2405 * For the peripheral driver list traversal function, however, we 2406 * don't have to worry about new peripheral driver types coming or 2407 * going; they're in a linker set, and therefore can't change 2408 * without a recompile. 2409 */ 2410 2411 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 2412 && (cdm->pos.cookie.pdrv != NULL)) 2413 ret = xptpdrvtraverse( 2414 (struct periph_driver **)cdm->pos.cookie.pdrv, 2415 xptplistpdrvfunc, cdm); 2416 else 2417 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm); 2418 2419 /* 2420 * If we get back 0, that means that we had to stop before fully 2421 * traversing the peripheral driver tree. It also means that one of 2422 * the subroutines has set the status field to the proper value. If 2423 * we get back 1, we've fully traversed the EDT and copied out any 2424 * matching entries. 2425 */ 2426 if (ret == 1) 2427 cdm->status = CAM_DEV_MATCH_LAST; 2428 2429 return(ret); 2430 } 2431 2432 static int 2433 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg) 2434 { 2435 struct cam_eb *bus, *next_bus; 2436 int retval; 2437 2438 retval = 1; 2439 2440 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses)); 2441 bus != NULL; 2442 bus = next_bus) { 2443 next_bus = TAILQ_NEXT(bus, links); 2444 2445 retval = tr_func(bus, arg); 2446 if (retval == 0) 2447 return(retval); 2448 } 2449 2450 return(retval); 2451 } 2452 2453 static int 2454 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target, 2455 xpt_targetfunc_t *tr_func, void *arg) 2456 { 2457 struct cam_et *target, *next_target; 2458 int retval; 2459 2460 retval = 1; 2461 for (target = (start_target ? start_target : 2462 TAILQ_FIRST(&bus->et_entries)); 2463 target != NULL; target = next_target) { 2464 2465 next_target = TAILQ_NEXT(target, links); 2466 2467 retval = tr_func(target, arg); 2468 2469 if (retval == 0) 2470 return(retval); 2471 } 2472 2473 return(retval); 2474 } 2475 2476 static int 2477 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device, 2478 xpt_devicefunc_t *tr_func, void *arg) 2479 { 2480 struct cam_ed *device, *next_device; 2481 int retval; 2482 2483 retval = 1; 2484 for (device = (start_device ? start_device : 2485 TAILQ_FIRST(&target->ed_entries)); 2486 device != NULL; 2487 device = next_device) { 2488 2489 next_device = TAILQ_NEXT(device, links); 2490 2491 retval = tr_func(device, arg); 2492 2493 if (retval == 0) 2494 return(retval); 2495 } 2496 2497 return(retval); 2498 } 2499 2500 static int 2501 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph, 2502 xpt_periphfunc_t *tr_func, void *arg) 2503 { 2504 struct cam_periph *periph, *next_periph; 2505 int retval; 2506 2507 retval = 1; 2508 2509 for (periph = (start_periph ? start_periph : 2510 SLIST_FIRST(&device->periphs)); 2511 periph != NULL; 2512 periph = next_periph) { 2513 2514 next_periph = SLIST_NEXT(periph, periph_links); 2515 2516 retval = tr_func(periph, arg); 2517 if (retval == 0) 2518 return(retval); 2519 } 2520 2521 return(retval); 2522 } 2523 2524 static int 2525 xptpdrvtraverse(struct periph_driver **start_pdrv, 2526 xpt_pdrvfunc_t *tr_func, void *arg) 2527 { 2528 struct periph_driver **pdrv; 2529 int retval; 2530 2531 retval = 1; 2532 2533 /* 2534 * We don't traverse the peripheral driver list like we do the 2535 * other lists, because it is a linker set, and therefore cannot be 2536 * changed during runtime. If the peripheral driver list is ever 2537 * re-done to be something other than a linker set (i.e. it can 2538 * change while the system is running), the list traversal should 2539 * be modified to work like the other traversal functions. 2540 */ 2541 for (pdrv = (start_pdrv ? start_pdrv : 2542 (struct periph_driver **)periphdriver_set.ls_items); 2543 *pdrv != NULL; pdrv++) { 2544 retval = tr_func(pdrv, arg); 2545 2546 if (retval == 0) 2547 return(retval); 2548 } 2549 2550 return(retval); 2551 } 2552 2553 static int 2554 xptpdperiphtraverse(struct periph_driver **pdrv, 2555 struct cam_periph *start_periph, 2556 xpt_periphfunc_t *tr_func, void *arg) 2557 { 2558 struct cam_periph *periph, *next_periph; 2559 int retval; 2560 2561 retval = 1; 2562 2563 for (periph = (start_periph ? start_periph : 2564 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL; 2565 periph = next_periph) { 2566 2567 next_periph = TAILQ_NEXT(periph, unit_links); 2568 2569 retval = tr_func(periph, arg); 2570 if (retval == 0) 2571 return(retval); 2572 } 2573 return(retval); 2574 } 2575 2576 static int 2577 xptdefbusfunc(struct cam_eb *bus, void *arg) 2578 { 2579 struct xpt_traverse_config *tr_config; 2580 2581 tr_config = (struct xpt_traverse_config *)arg; 2582 2583 if (tr_config->depth == XPT_DEPTH_BUS) { 2584 xpt_busfunc_t *tr_func; 2585 2586 tr_func = (xpt_busfunc_t *)tr_config->tr_func; 2587 2588 return(tr_func(bus, tr_config->tr_arg)); 2589 } else 2590 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg)); 2591 } 2592 2593 static int 2594 xptdeftargetfunc(struct cam_et *target, void *arg) 2595 { 2596 struct xpt_traverse_config *tr_config; 2597 2598 tr_config = (struct xpt_traverse_config *)arg; 2599 2600 if (tr_config->depth == XPT_DEPTH_TARGET) { 2601 xpt_targetfunc_t *tr_func; 2602 2603 tr_func = (xpt_targetfunc_t *)tr_config->tr_func; 2604 2605 return(tr_func(target, tr_config->tr_arg)); 2606 } else 2607 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg)); 2608 } 2609 2610 static int 2611 xptdefdevicefunc(struct cam_ed *device, void *arg) 2612 { 2613 struct xpt_traverse_config *tr_config; 2614 2615 tr_config = (struct xpt_traverse_config *)arg; 2616 2617 if (tr_config->depth == XPT_DEPTH_DEVICE) { 2618 xpt_devicefunc_t *tr_func; 2619 2620 tr_func = (xpt_devicefunc_t *)tr_config->tr_func; 2621 2622 return(tr_func(device, tr_config->tr_arg)); 2623 } else 2624 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg)); 2625 } 2626 2627 static int 2628 xptdefperiphfunc(struct cam_periph *periph, void *arg) 2629 { 2630 struct xpt_traverse_config *tr_config; 2631 xpt_periphfunc_t *tr_func; 2632 2633 tr_config = (struct xpt_traverse_config *)arg; 2634 2635 tr_func = (xpt_periphfunc_t *)tr_config->tr_func; 2636 2637 /* 2638 * Unlike the other default functions, we don't check for depth 2639 * here. The peripheral driver level is the last level in the EDT, 2640 * so if we're here, we should execute the function in question. 2641 */ 2642 return(tr_func(periph, tr_config->tr_arg)); 2643 } 2644 2645 /* 2646 * Execute the given function for every bus in the EDT. 2647 */ 2648 static int 2649 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg) 2650 { 2651 struct xpt_traverse_config tr_config; 2652 2653 tr_config.depth = XPT_DEPTH_BUS; 2654 tr_config.tr_func = tr_func; 2655 tr_config.tr_arg = arg; 2656 2657 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2658 } 2659 2660 #ifdef notusedyet 2661 /* 2662 * Execute the given function for every target in the EDT. 2663 */ 2664 static int 2665 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg) 2666 { 2667 struct xpt_traverse_config tr_config; 2668 2669 tr_config.depth = XPT_DEPTH_TARGET; 2670 tr_config.tr_func = tr_func; 2671 tr_config.tr_arg = arg; 2672 2673 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2674 } 2675 #endif /* notusedyet */ 2676 2677 /* 2678 * Execute the given function for every device in the EDT. 2679 */ 2680 static int 2681 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg) 2682 { 2683 struct xpt_traverse_config tr_config; 2684 2685 tr_config.depth = XPT_DEPTH_DEVICE; 2686 tr_config.tr_func = tr_func; 2687 tr_config.tr_arg = arg; 2688 2689 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2690 } 2691 2692 #ifdef notusedyet 2693 /* 2694 * Execute the given function for every peripheral in the EDT. 2695 */ 2696 static int 2697 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg) 2698 { 2699 struct xpt_traverse_config tr_config; 2700 2701 tr_config.depth = XPT_DEPTH_PERIPH; 2702 tr_config.tr_func = tr_func; 2703 tr_config.tr_arg = arg; 2704 2705 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2706 } 2707 #endif /* notusedyet */ 2708 2709 static int 2710 xptsetasyncfunc(struct cam_ed *device, void *arg) 2711 { 2712 struct cam_path path; 2713 struct ccb_getdev cgd; 2714 struct async_node *cur_entry; 2715 2716 cur_entry = (struct async_node *)arg; 2717 2718 /* 2719 * Don't report unconfigured devices (Wildcard devs, 2720 * devices only for target mode, device instances 2721 * that have been invalidated but are waiting for 2722 * their last reference count to be released). 2723 */ 2724 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0) 2725 return (1); 2726 2727 xpt_compile_path(&path, 2728 NULL, 2729 device->target->bus->path_id, 2730 device->target->target_id, 2731 device->lun_id); 2732 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1); 2733 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 2734 xpt_action((union ccb *)&cgd); 2735 cur_entry->callback(cur_entry->callback_arg, 2736 AC_FOUND_DEVICE, 2737 &path, &cgd); 2738 xpt_release_path(&path); 2739 2740 return(1); 2741 } 2742 2743 static int 2744 xptsetasyncbusfunc(struct cam_eb *bus, void *arg) 2745 { 2746 struct cam_path path; 2747 struct ccb_pathinq cpi; 2748 struct async_node *cur_entry; 2749 2750 cur_entry = (struct async_node *)arg; 2751 2752 xpt_compile_path(&path, /*periph*/NULL, 2753 bus->sim->path_id, 2754 CAM_TARGET_WILDCARD, 2755 CAM_LUN_WILDCARD); 2756 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1); 2757 cpi.ccb_h.func_code = XPT_PATH_INQ; 2758 xpt_action((union ccb *)&cpi); 2759 cur_entry->callback(cur_entry->callback_arg, 2760 AC_PATH_REGISTERED, 2761 &path, &cpi); 2762 xpt_release_path(&path); 2763 2764 return(1); 2765 } 2766 2767 void 2768 xpt_action(union ccb *start_ccb) 2769 { 2770 int iopl; 2771 2772 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n")); 2773 2774 start_ccb->ccb_h.status = CAM_REQ_INPROG; 2775 2776 iopl = splsoftcam(); 2777 switch (start_ccb->ccb_h.func_code) { 2778 case XPT_SCSI_IO: 2779 { 2780 #ifdef CAMDEBUG 2781 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1]; 2782 struct cam_path *path; 2783 2784 path = start_ccb->ccb_h.path; 2785 #endif 2786 2787 /* 2788 * For the sake of compatibility with SCSI-1 2789 * devices that may not understand the identify 2790 * message, we include lun information in the 2791 * second byte of all commands. SCSI-1 specifies 2792 * that luns are a 3 bit value and reserves only 3 2793 * bits for lun information in the CDB. Later 2794 * revisions of the SCSI spec allow for more than 8 2795 * luns, but have deprecated lun information in the 2796 * CDB. So, if the lun won't fit, we must omit. 2797 * 2798 * Also be aware that during initial probing for devices, 2799 * the inquiry information is unknown but initialized to 0. 2800 * This means that this code will be exercised while probing 2801 * devices with an ANSI revision greater than 2. 2802 */ 2803 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2 2804 && start_ccb->ccb_h.target_lun < 8 2805 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) { 2806 2807 start_ccb->csio.cdb_io.cdb_bytes[1] |= 2808 start_ccb->ccb_h.target_lun << 5; 2809 } 2810 start_ccb->csio.scsi_status = SCSI_STATUS_OK; 2811 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n", 2812 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0], 2813 &path->device->inq_data), 2814 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes, 2815 cdb_str, sizeof(cdb_str)))); 2816 /* FALLTHROUGH */ 2817 } 2818 case XPT_TARGET_IO: 2819 case XPT_CONT_TARGET_IO: 2820 start_ccb->csio.sense_resid = 0; 2821 start_ccb->csio.resid = 0; 2822 /* FALLTHROUGH */ 2823 case XPT_RESET_DEV: 2824 case XPT_ENG_EXEC: 2825 { 2826 struct cam_path *path; 2827 int s; 2828 int runq; 2829 2830 path = start_ccb->ccb_h.path; 2831 s = splsoftcam(); 2832 2833 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb); 2834 if (path->device->qfrozen_cnt == 0) 2835 runq = xpt_schedule_dev_sendq(path->bus, path->device); 2836 else 2837 runq = 0; 2838 splx(s); 2839 if (runq != 0) 2840 xpt_run_dev_sendq(path->bus); 2841 break; 2842 } 2843 case XPT_SET_TRAN_SETTINGS: 2844 { 2845 xpt_set_transfer_settings(&start_ccb->cts, 2846 start_ccb->ccb_h.path->device, 2847 /*async_update*/FALSE); 2848 break; 2849 } 2850 case XPT_CALC_GEOMETRY: 2851 { 2852 struct cam_sim *sim; 2853 2854 /* Filter out garbage */ 2855 if (start_ccb->ccg.block_size == 0 2856 || start_ccb->ccg.volume_size == 0) { 2857 start_ccb->ccg.cylinders = 0; 2858 start_ccb->ccg.heads = 0; 2859 start_ccb->ccg.secs_per_track = 0; 2860 start_ccb->ccb_h.status = CAM_REQ_CMP; 2861 break; 2862 } 2863 #ifdef PC98 2864 /* 2865 * In a PC-98 system, geometry translation depens on 2866 * the "real" device geometry obtained from mode page 4. 2867 * SCSI geometry translation is performed in the 2868 * initialization routine of the SCSI BIOS and the result 2869 * stored in host memory. If the translation is available 2870 * in host memory, use it. If not, rely on the default 2871 * translation the device driver performs. 2872 */ 2873 if (scsi_da_bios_params(&start_ccb->ccg) != 0) { 2874 start_ccb->ccb_h.status = CAM_REQ_CMP; 2875 break; 2876 } 2877 #endif 2878 sim = start_ccb->ccb_h.path->bus->sim; 2879 (*(sim->sim_action))(sim, start_ccb); 2880 break; 2881 } 2882 case XPT_ABORT: 2883 { 2884 union ccb* abort_ccb; 2885 int s; 2886 2887 abort_ccb = start_ccb->cab.abort_ccb; 2888 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) { 2889 2890 if (abort_ccb->ccb_h.pinfo.index >= 0) { 2891 struct cam_ccbq *ccbq; 2892 2893 ccbq = &abort_ccb->ccb_h.path->device->ccbq; 2894 cam_ccbq_remove_ccb(ccbq, abort_ccb); 2895 abort_ccb->ccb_h.status = 2896 CAM_REQ_ABORTED|CAM_DEV_QFRZN; 2897 xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 2898 s = splcam(); 2899 xpt_done(abort_ccb); 2900 splx(s); 2901 start_ccb->ccb_h.status = CAM_REQ_CMP; 2902 break; 2903 } 2904 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX 2905 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) { 2906 /* 2907 * We've caught this ccb en route to 2908 * the SIM. Flag it for abort and the 2909 * SIM will do so just before starting 2910 * real work on the CCB. 2911 */ 2912 abort_ccb->ccb_h.status = 2913 CAM_REQ_ABORTED|CAM_DEV_QFRZN; 2914 xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 2915 start_ccb->ccb_h.status = CAM_REQ_CMP; 2916 break; 2917 } 2918 } 2919 if (XPT_FC_IS_QUEUED(abort_ccb) 2920 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) { 2921 /* 2922 * It's already completed but waiting 2923 * for our SWI to get to it. 2924 */ 2925 start_ccb->ccb_h.status = CAM_UA_ABORT; 2926 break; 2927 } 2928 /* 2929 * If we weren't able to take care of the abort request 2930 * in the XPT, pass the request down to the SIM for processing. 2931 */ 2932 /* FALLTHROUGH */ 2933 } 2934 case XPT_ACCEPT_TARGET_IO: 2935 case XPT_EN_LUN: 2936 case XPT_IMMED_NOTIFY: 2937 case XPT_NOTIFY_ACK: 2938 case XPT_GET_TRAN_SETTINGS: 2939 case XPT_RESET_BUS: 2940 { 2941 struct cam_sim *sim; 2942 2943 sim = start_ccb->ccb_h.path->bus->sim; 2944 (*(sim->sim_action))(sim, start_ccb); 2945 break; 2946 } 2947 case XPT_PATH_INQ: 2948 { 2949 struct cam_sim *sim; 2950 2951 sim = start_ccb->ccb_h.path->bus->sim; 2952 (*(sim->sim_action))(sim, start_ccb); 2953 break; 2954 } 2955 case XPT_PATH_STATS: 2956 start_ccb->cpis.last_reset = 2957 start_ccb->ccb_h.path->bus->last_reset; 2958 start_ccb->ccb_h.status = CAM_REQ_CMP; 2959 break; 2960 case XPT_GDEV_TYPE: 2961 { 2962 struct cam_ed *dev; 2963 int s; 2964 2965 dev = start_ccb->ccb_h.path->device; 2966 s = splcam(); 2967 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 2968 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2969 } else { 2970 struct ccb_getdev *cgd; 2971 struct cam_eb *bus; 2972 struct cam_et *tar; 2973 2974 cgd = &start_ccb->cgd; 2975 bus = cgd->ccb_h.path->bus; 2976 tar = cgd->ccb_h.path->target; 2977 cgd->inq_data = dev->inq_data; 2978 cgd->ccb_h.status = CAM_REQ_CMP; 2979 cgd->serial_num_len = dev->serial_num_len; 2980 if ((dev->serial_num_len > 0) 2981 && (dev->serial_num != NULL)) 2982 bcopy(dev->serial_num, cgd->serial_num, 2983 dev->serial_num_len); 2984 } 2985 splx(s); 2986 break; 2987 } 2988 case XPT_GDEV_STATS: 2989 { 2990 struct cam_ed *dev; 2991 int s; 2992 2993 dev = start_ccb->ccb_h.path->device; 2994 s = splcam(); 2995 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 2996 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2997 } else { 2998 struct ccb_getdevstats *cgds; 2999 struct cam_eb *bus; 3000 struct cam_et *tar; 3001 3002 cgds = &start_ccb->cgds; 3003 bus = cgds->ccb_h.path->bus; 3004 tar = cgds->ccb_h.path->target; 3005 cgds->dev_openings = dev->ccbq.dev_openings; 3006 cgds->dev_active = dev->ccbq.dev_active; 3007 cgds->devq_openings = dev->ccbq.devq_openings; 3008 cgds->devq_queued = dev->ccbq.queue.entries; 3009 cgds->held = dev->ccbq.held; 3010 cgds->last_reset = tar->last_reset; 3011 cgds->maxtags = dev->quirk->maxtags; 3012 cgds->mintags = dev->quirk->mintags; 3013 if (timevalcmp(&tar->last_reset, &bus->last_reset, <)) 3014 cgds->last_reset = bus->last_reset; 3015 cgds->ccb_h.status = CAM_REQ_CMP; 3016 } 3017 splx(s); 3018 break; 3019 } 3020 case XPT_GDEVLIST: 3021 { 3022 struct cam_periph *nperiph; 3023 struct periph_list *periph_head; 3024 struct ccb_getdevlist *cgdl; 3025 int i; 3026 int s; 3027 struct cam_ed *device; 3028 int found; 3029 3030 3031 found = 0; 3032 3033 /* 3034 * Don't want anyone mucking with our data. 3035 */ 3036 s = splcam(); 3037 device = start_ccb->ccb_h.path->device; 3038 periph_head = &device->periphs; 3039 cgdl = &start_ccb->cgdl; 3040 3041 /* 3042 * Check and see if the list has changed since the user 3043 * last requested a list member. If so, tell them that the 3044 * list has changed, and therefore they need to start over 3045 * from the beginning. 3046 */ 3047 if ((cgdl->index != 0) && 3048 (cgdl->generation != device->generation)) { 3049 cgdl->status = CAM_GDEVLIST_LIST_CHANGED; 3050 splx(s); 3051 break; 3052 } 3053 3054 /* 3055 * Traverse the list of peripherals and attempt to find 3056 * the requested peripheral. 3057 */ 3058 for (nperiph = periph_head->slh_first, i = 0; 3059 (nperiph != NULL) && (i <= cgdl->index); 3060 nperiph = nperiph->periph_links.sle_next, i++) { 3061 if (i == cgdl->index) { 3062 strncpy(cgdl->periph_name, 3063 nperiph->periph_name, 3064 DEV_IDLEN); 3065 cgdl->unit_number = nperiph->unit_number; 3066 found = 1; 3067 } 3068 } 3069 if (found == 0) { 3070 cgdl->status = CAM_GDEVLIST_ERROR; 3071 splx(s); 3072 break; 3073 } 3074 3075 if (nperiph == NULL) 3076 cgdl->status = CAM_GDEVLIST_LAST_DEVICE; 3077 else 3078 cgdl->status = CAM_GDEVLIST_MORE_DEVS; 3079 3080 cgdl->index++; 3081 cgdl->generation = device->generation; 3082 3083 splx(s); 3084 cgdl->ccb_h.status = CAM_REQ_CMP; 3085 break; 3086 } 3087 case XPT_DEV_MATCH: 3088 { 3089 int s; 3090 dev_pos_type position_type; 3091 struct ccb_dev_match *cdm; 3092 int ret; 3093 3094 cdm = &start_ccb->cdm; 3095 3096 /* 3097 * Prevent EDT changes while we traverse it. 3098 */ 3099 s = splcam(); 3100 /* 3101 * There are two ways of getting at information in the EDT. 3102 * The first way is via the primary EDT tree. It starts 3103 * with a list of busses, then a list of targets on a bus, 3104 * then devices/luns on a target, and then peripherals on a 3105 * device/lun. The "other" way is by the peripheral driver 3106 * lists. The peripheral driver lists are organized by 3107 * peripheral driver. (obviously) So it makes sense to 3108 * use the peripheral driver list if the user is looking 3109 * for something like "da1", or all "da" devices. If the 3110 * user is looking for something on a particular bus/target 3111 * or lun, it's generally better to go through the EDT tree. 3112 */ 3113 3114 if (cdm->pos.position_type != CAM_DEV_POS_NONE) 3115 position_type = cdm->pos.position_type; 3116 else { 3117 int i; 3118 3119 position_type = CAM_DEV_POS_NONE; 3120 3121 for (i = 0; i < cdm->num_patterns; i++) { 3122 if ((cdm->patterns[i].type == DEV_MATCH_BUS) 3123 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){ 3124 position_type = CAM_DEV_POS_EDT; 3125 break; 3126 } 3127 } 3128 3129 if (cdm->num_patterns == 0) 3130 position_type = CAM_DEV_POS_EDT; 3131 else if (position_type == CAM_DEV_POS_NONE) 3132 position_type = CAM_DEV_POS_PDRV; 3133 } 3134 3135 switch(position_type & CAM_DEV_POS_TYPEMASK) { 3136 case CAM_DEV_POS_EDT: 3137 ret = xptedtmatch(cdm); 3138 break; 3139 case CAM_DEV_POS_PDRV: 3140 ret = xptperiphlistmatch(cdm); 3141 break; 3142 default: 3143 cdm->status = CAM_DEV_MATCH_ERROR; 3144 break; 3145 } 3146 3147 splx(s); 3148 3149 if (cdm->status == CAM_DEV_MATCH_ERROR) 3150 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR; 3151 else 3152 start_ccb->ccb_h.status = CAM_REQ_CMP; 3153 3154 break; 3155 } 3156 case XPT_SASYNC_CB: 3157 { 3158 struct ccb_setasync *csa; 3159 struct async_node *cur_entry; 3160 struct async_list *async_head; 3161 u_int32_t added; 3162 int s; 3163 3164 csa = &start_ccb->csa; 3165 added = csa->event_enable; 3166 async_head = &csa->ccb_h.path->device->asyncs; 3167 3168 /* 3169 * If there is already an entry for us, simply 3170 * update it. 3171 */ 3172 s = splcam(); 3173 cur_entry = SLIST_FIRST(async_head); 3174 while (cur_entry != NULL) { 3175 if ((cur_entry->callback_arg == csa->callback_arg) 3176 && (cur_entry->callback == csa->callback)) 3177 break; 3178 cur_entry = SLIST_NEXT(cur_entry, links); 3179 } 3180 3181 if (cur_entry != NULL) { 3182 /* 3183 * If the request has no flags set, 3184 * remove the entry. 3185 */ 3186 added &= ~cur_entry->event_enable; 3187 if (csa->event_enable == 0) { 3188 SLIST_REMOVE(async_head, cur_entry, 3189 async_node, links); 3190 csa->ccb_h.path->device->refcount--; 3191 free(cur_entry, M_DEVBUF); 3192 } else { 3193 cur_entry->event_enable = csa->event_enable; 3194 } 3195 } else { 3196 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF, 3197 M_NOWAIT); 3198 if (cur_entry == NULL) { 3199 splx(s); 3200 csa->ccb_h.status = CAM_RESRC_UNAVAIL; 3201 break; 3202 } 3203 cur_entry->event_enable = csa->event_enable; 3204 cur_entry->callback_arg = csa->callback_arg; 3205 cur_entry->callback = csa->callback; 3206 SLIST_INSERT_HEAD(async_head, cur_entry, links); 3207 csa->ccb_h.path->device->refcount++; 3208 } 3209 3210 if ((added & AC_FOUND_DEVICE) != 0) { 3211 /* 3212 * Get this peripheral up to date with all 3213 * the currently existing devices. 3214 */ 3215 xpt_for_all_devices(xptsetasyncfunc, cur_entry); 3216 } 3217 if ((added & AC_PATH_REGISTERED) != 0) { 3218 /* 3219 * Get this peripheral up to date with all 3220 * the currently existing busses. 3221 */ 3222 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry); 3223 } 3224 splx(s); 3225 start_ccb->ccb_h.status = CAM_REQ_CMP; 3226 break; 3227 } 3228 case XPT_REL_SIMQ: 3229 { 3230 struct ccb_relsim *crs; 3231 struct cam_ed *dev; 3232 int s; 3233 3234 crs = &start_ccb->crs; 3235 dev = crs->ccb_h.path->device; 3236 if (dev == NULL) { 3237 3238 crs->ccb_h.status = CAM_DEV_NOT_THERE; 3239 break; 3240 } 3241 3242 s = splcam(); 3243 3244 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) { 3245 3246 if ((dev->inq_data.flags & SID_CmdQue) != 0) { 3247 3248 /* Don't ever go below one opening */ 3249 if (crs->openings > 0) { 3250 xpt_dev_ccbq_resize(crs->ccb_h.path, 3251 crs->openings); 3252 3253 if (bootverbose) { 3254 xpt_print_path(crs->ccb_h.path); 3255 printf("tagged openings " 3256 "now %d\n", 3257 crs->openings); 3258 } 3259 } 3260 } 3261 } 3262 3263 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) { 3264 3265 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 3266 3267 /* 3268 * Just extend the old timeout and decrement 3269 * the freeze count so that a single timeout 3270 * is sufficient for releasing the queue. 3271 */ 3272 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 3273 untimeout(xpt_release_devq_timeout, 3274 dev, dev->c_handle); 3275 } else { 3276 3277 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 3278 } 3279 3280 dev->c_handle = 3281 timeout(xpt_release_devq_timeout, 3282 dev, 3283 (crs->release_timeout * hz) / 1000); 3284 3285 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING; 3286 3287 } 3288 3289 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) { 3290 3291 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) { 3292 /* 3293 * Decrement the freeze count so that a single 3294 * completion is still sufficient to unfreeze 3295 * the queue. 3296 */ 3297 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 3298 } else { 3299 3300 dev->flags |= CAM_DEV_REL_ON_COMPLETE; 3301 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 3302 } 3303 } 3304 3305 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) { 3306 3307 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0 3308 || (dev->ccbq.dev_active == 0)) { 3309 3310 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 3311 } else { 3312 3313 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY; 3314 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 3315 } 3316 } 3317 splx(s); 3318 3319 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) { 3320 3321 xpt_release_devq(crs->ccb_h.path, /*count*/1, 3322 /*run_queue*/TRUE); 3323 } 3324 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt; 3325 start_ccb->ccb_h.status = CAM_REQ_CMP; 3326 break; 3327 } 3328 case XPT_SCAN_BUS: 3329 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb); 3330 break; 3331 case XPT_SCAN_LUN: 3332 xpt_scan_lun(start_ccb->ccb_h.path->periph, 3333 start_ccb->ccb_h.path, start_ccb->crcn.flags, 3334 start_ccb); 3335 break; 3336 case XPT_DEBUG: { 3337 #ifdef CAMDEBUG 3338 int s; 3339 3340 s = splcam(); 3341 #ifdef CAM_DEBUG_DELAY 3342 cam_debug_delay = CAM_DEBUG_DELAY; 3343 #endif 3344 cam_dflags = start_ccb->cdbg.flags; 3345 if (cam_dpath != NULL) { 3346 xpt_free_path(cam_dpath); 3347 cam_dpath = NULL; 3348 } 3349 3350 if (cam_dflags != CAM_DEBUG_NONE) { 3351 if (xpt_create_path(&cam_dpath, xpt_periph, 3352 start_ccb->ccb_h.path_id, 3353 start_ccb->ccb_h.target_id, 3354 start_ccb->ccb_h.target_lun) != 3355 CAM_REQ_CMP) { 3356 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 3357 cam_dflags = CAM_DEBUG_NONE; 3358 } else { 3359 start_ccb->ccb_h.status = CAM_REQ_CMP; 3360 xpt_print_path(cam_dpath); 3361 printf("debugging flags now %x\n", cam_dflags); 3362 } 3363 } else { 3364 cam_dpath = NULL; 3365 start_ccb->ccb_h.status = CAM_REQ_CMP; 3366 } 3367 splx(s); 3368 #else /* !CAMDEBUG */ 3369 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 3370 #endif /* CAMDEBUG */ 3371 break; 3372 } 3373 case XPT_NOOP: 3374 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) 3375 xpt_freeze_devq(start_ccb->ccb_h.path, 1); 3376 start_ccb->ccb_h.status = CAM_REQ_CMP; 3377 break; 3378 default: 3379 case XPT_SDEV_TYPE: 3380 case XPT_TERM_IO: 3381 case XPT_ENG_INQ: 3382 /* XXX Implement */ 3383 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL; 3384 break; 3385 } 3386 splx(iopl); 3387 } 3388 3389 void 3390 xpt_polled_action(union ccb *start_ccb) 3391 { 3392 int s; 3393 u_int32_t timeout; 3394 struct cam_sim *sim; 3395 struct cam_devq *devq; 3396 struct cam_ed *dev; 3397 3398 timeout = start_ccb->ccb_h.timeout; 3399 sim = start_ccb->ccb_h.path->bus->sim; 3400 devq = sim->devq; 3401 dev = start_ccb->ccb_h.path->device; 3402 3403 s = splcam(); 3404 3405 /* 3406 * Steal an opening so that no other queued requests 3407 * can get it before us while we simulate interrupts. 3408 */ 3409 dev->ccbq.devq_openings--; 3410 dev->ccbq.dev_openings--; 3411 3412 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) 3413 && (--timeout > 0)) { 3414 DELAY(1000); 3415 (*(sim->sim_poll))(sim); 3416 swi_camnet(NULL); 3417 swi_cambio(NULL); 3418 } 3419 3420 dev->ccbq.devq_openings++; 3421 dev->ccbq.dev_openings++; 3422 3423 if (timeout != 0) { 3424 xpt_action(start_ccb); 3425 while(--timeout > 0) { 3426 (*(sim->sim_poll))(sim); 3427 swi_camnet(NULL); 3428 swi_cambio(NULL); 3429 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK) 3430 != CAM_REQ_INPROG) 3431 break; 3432 DELAY(1000); 3433 } 3434 if (timeout == 0) { 3435 /* 3436 * XXX Is it worth adding a sim_timeout entry 3437 * point so we can attempt recovery? If 3438 * this is only used for dumps, I don't think 3439 * it is. 3440 */ 3441 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT; 3442 } 3443 } else { 3444 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 3445 } 3446 splx(s); 3447 } 3448 3449 /* 3450 * Schedule a peripheral driver to receive a ccb when it's 3451 * target device has space for more transactions. 3452 */ 3453 void 3454 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority) 3455 { 3456 struct cam_ed *device; 3457 int s; 3458 int runq; 3459 3460 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n")); 3461 device = perph->path->device; 3462 s = splsoftcam(); 3463 if (periph_is_queued(perph)) { 3464 /* Simply reorder based on new priority */ 3465 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 3466 (" change priority to %d\n", new_priority)); 3467 if (new_priority < perph->pinfo.priority) { 3468 camq_change_priority(&device->drvq, 3469 perph->pinfo.index, 3470 new_priority); 3471 } 3472 runq = 0; 3473 } else { 3474 /* New entry on the queue */ 3475 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 3476 (" added periph to queue\n")); 3477 perph->pinfo.priority = new_priority; 3478 perph->pinfo.generation = ++device->drvq.generation; 3479 camq_insert(&device->drvq, &perph->pinfo); 3480 runq = xpt_schedule_dev_allocq(perph->path->bus, device); 3481 } 3482 splx(s); 3483 if (runq != 0) { 3484 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 3485 (" calling xpt_run_devq\n")); 3486 xpt_run_dev_allocq(perph->path->bus); 3487 } 3488 } 3489 3490 3491 /* 3492 * Schedule a device to run on a given queue. 3493 * If the device was inserted as a new entry on the queue, 3494 * return 1 meaning the device queue should be run. If we 3495 * were already queued, implying someone else has already 3496 * started the queue, return 0 so the caller doesn't attempt 3497 * to run the queue. Must be run at either splsoftcam 3498 * (or splcam since that encompases splsoftcam). 3499 */ 3500 static int 3501 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo, 3502 u_int32_t new_priority) 3503 { 3504 int retval; 3505 u_int32_t old_priority; 3506 3507 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n")); 3508 3509 old_priority = pinfo->priority; 3510 3511 /* 3512 * Are we already queued? 3513 */ 3514 if (pinfo->index != CAM_UNQUEUED_INDEX) { 3515 /* Simply reorder based on new priority */ 3516 if (new_priority < old_priority) { 3517 camq_change_priority(queue, pinfo->index, 3518 new_priority); 3519 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3520 ("changed priority to %d\n", 3521 new_priority)); 3522 } 3523 retval = 0; 3524 } else { 3525 /* New entry on the queue */ 3526 if (new_priority < old_priority) 3527 pinfo->priority = new_priority; 3528 3529 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3530 ("Inserting onto queue\n")); 3531 pinfo->generation = ++queue->generation; 3532 camq_insert(queue, pinfo); 3533 retval = 1; 3534 } 3535 return (retval); 3536 } 3537 3538 static void 3539 xpt_run_dev_allocq(struct cam_eb *bus) 3540 { 3541 struct cam_devq *devq; 3542 int s; 3543 3544 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n")); 3545 devq = bus->sim->devq; 3546 3547 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3548 (" qfrozen_cnt == 0x%x, entries == %d, " 3549 "openings == %d, active == %d\n", 3550 devq->alloc_queue.qfrozen_cnt, 3551 devq->alloc_queue.entries, 3552 devq->alloc_openings, 3553 devq->alloc_active)); 3554 3555 s = splsoftcam(); 3556 devq->alloc_queue.qfrozen_cnt++; 3557 while ((devq->alloc_queue.entries > 0) 3558 && (devq->alloc_openings > 0) 3559 && (devq->alloc_queue.qfrozen_cnt <= 1)) { 3560 struct cam_ed_qinfo *qinfo; 3561 struct cam_ed *device; 3562 union ccb *work_ccb; 3563 struct cam_periph *drv; 3564 struct camq *drvq; 3565 3566 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue, 3567 CAMQ_HEAD); 3568 device = qinfo->device; 3569 3570 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3571 ("running device %p\n", device)); 3572 3573 drvq = &device->drvq; 3574 3575 #ifdef CAMDEBUG 3576 if (drvq->entries <= 0) { 3577 panic("xpt_run_dev_allocq: " 3578 "Device on queue without any work to do"); 3579 } 3580 #endif 3581 if ((work_ccb = xpt_get_ccb(device)) != NULL) { 3582 devq->alloc_openings--; 3583 devq->alloc_active++; 3584 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD); 3585 splx(s); 3586 xpt_setup_ccb(&work_ccb->ccb_h, drv->path, 3587 drv->pinfo.priority); 3588 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3589 ("calling periph start\n")); 3590 drv->periph_start(drv, work_ccb); 3591 } else { 3592 /* 3593 * Malloc failure in alloc_ccb 3594 */ 3595 /* 3596 * XXX add us to a list to be run from free_ccb 3597 * if we don't have any ccbs active on this 3598 * device queue otherwise we may never get run 3599 * again. 3600 */ 3601 break; 3602 } 3603 3604 /* Raise IPL for possible insertion and test at top of loop */ 3605 s = splsoftcam(); 3606 3607 if (drvq->entries > 0) { 3608 /* We have more work. Attempt to reschedule */ 3609 xpt_schedule_dev_allocq(bus, device); 3610 } 3611 } 3612 devq->alloc_queue.qfrozen_cnt--; 3613 splx(s); 3614 } 3615 3616 static void 3617 xpt_run_dev_sendq(struct cam_eb *bus) 3618 { 3619 struct cam_devq *devq; 3620 int s; 3621 3622 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n")); 3623 3624 devq = bus->sim->devq; 3625 3626 s = splcam(); 3627 devq->send_queue.qfrozen_cnt++; 3628 splx(s); 3629 s = splsoftcam(); 3630 while ((devq->send_queue.entries > 0) 3631 && (devq->send_openings > 0)) { 3632 struct cam_ed_qinfo *qinfo; 3633 struct cam_ed *device; 3634 union ccb *work_ccb; 3635 struct cam_sim *sim; 3636 int ospl; 3637 3638 ospl = splcam(); 3639 if (devq->send_queue.qfrozen_cnt > 1) { 3640 splx(ospl); 3641 break; 3642 } 3643 3644 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue, 3645 CAMQ_HEAD); 3646 device = qinfo->device; 3647 3648 /* 3649 * If the device has been "frozen", don't attempt 3650 * to run it. 3651 */ 3652 if (device->qfrozen_cnt > 0) { 3653 splx(ospl); 3654 continue; 3655 } 3656 3657 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3658 ("running device %p\n", device)); 3659 3660 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD); 3661 if (work_ccb == NULL) { 3662 printf("device on run queue with no ccbs???\n"); 3663 splx(ospl); 3664 continue; 3665 } 3666 3667 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) { 3668 3669 if (num_highpower <= 0) { 3670 /* 3671 * We got a high power command, but we 3672 * don't have any available slots. Freeze 3673 * the device queue until we have a slot 3674 * available. 3675 */ 3676 device->qfrozen_cnt++; 3677 STAILQ_INSERT_TAIL(&highpowerq, 3678 &work_ccb->ccb_h, 3679 xpt_links.stqe); 3680 3681 splx(ospl); 3682 continue; 3683 } else { 3684 /* 3685 * Consume a high power slot while 3686 * this ccb runs. 3687 */ 3688 num_highpower--; 3689 } 3690 } 3691 devq->active_dev = device; 3692 cam_ccbq_remove_ccb(&device->ccbq, work_ccb); 3693 3694 cam_ccbq_send_ccb(&device->ccbq, work_ccb); 3695 splx(ospl); 3696 3697 devq->send_openings--; 3698 devq->send_active++; 3699 3700 if (device->ccbq.queue.entries > 0) 3701 xpt_schedule_dev_sendq(bus, device); 3702 3703 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){ 3704 /* 3705 * The client wants to freeze the queue 3706 * after this CCB is sent. 3707 */ 3708 ospl = splcam(); 3709 device->qfrozen_cnt++; 3710 splx(ospl); 3711 } 3712 3713 splx(s); 3714 3715 /* In Target mode, the peripheral driver knows best... */ 3716 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) { 3717 if ((device->inq_flags & SID_CmdQue) != 0 3718 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE) 3719 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID; 3720 else 3721 /* 3722 * Clear this in case of a retried CCB that 3723 * failed due to a rejected tag. 3724 */ 3725 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 3726 } 3727 3728 /* 3729 * Device queues can be shared among multiple sim instances 3730 * that reside on different busses. Use the SIM in the queue 3731 * CCB's path, rather than the one in the bus that was passed 3732 * into this function. 3733 */ 3734 sim = work_ccb->ccb_h.path->bus->sim; 3735 (*(sim->sim_action))(sim, work_ccb); 3736 3737 ospl = splcam(); 3738 devq->active_dev = NULL; 3739 splx(ospl); 3740 /* Raise IPL for possible insertion and test at top of loop */ 3741 s = splsoftcam(); 3742 } 3743 splx(s); 3744 s = splcam(); 3745 devq->send_queue.qfrozen_cnt--; 3746 splx(s); 3747 } 3748 3749 /* 3750 * This function merges stuff from the slave ccb into the master ccb, while 3751 * keeping important fields in the master ccb constant. 3752 */ 3753 void 3754 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb) 3755 { 3756 /* 3757 * Pull fields that are valid for peripheral drivers to set 3758 * into the master CCB along with the CCB "payload". 3759 */ 3760 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count; 3761 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code; 3762 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout; 3763 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags; 3764 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1], 3765 sizeof(union ccb) - sizeof(struct ccb_hdr)); 3766 } 3767 3768 void 3769 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority) 3770 { 3771 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n")); 3772 ccb_h->pinfo.priority = priority; 3773 ccb_h->path = path; 3774 ccb_h->path_id = path->bus->path_id; 3775 if (path->target) 3776 ccb_h->target_id = path->target->target_id; 3777 else 3778 ccb_h->target_id = CAM_TARGET_WILDCARD; 3779 if (path->device) { 3780 ccb_h->target_lun = path->device->lun_id; 3781 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation; 3782 } else { 3783 ccb_h->target_lun = CAM_TARGET_WILDCARD; 3784 } 3785 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX; 3786 ccb_h->flags = 0; 3787 } 3788 3789 /* Path manipulation functions */ 3790 cam_status 3791 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph, 3792 path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 3793 { 3794 struct cam_path *path; 3795 cam_status status; 3796 3797 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT); 3798 3799 if (path == NULL) { 3800 status = CAM_RESRC_UNAVAIL; 3801 return(status); 3802 } 3803 status = xpt_compile_path(path, perph, path_id, target_id, lun_id); 3804 if (status != CAM_REQ_CMP) { 3805 free(path, M_DEVBUF); 3806 path = NULL; 3807 } 3808 *new_path_ptr = path; 3809 return (status); 3810 } 3811 3812 static cam_status 3813 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph, 3814 path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 3815 { 3816 struct cam_eb *bus; 3817 struct cam_et *target; 3818 struct cam_ed *device; 3819 cam_status status; 3820 int s; 3821 3822 status = CAM_REQ_CMP; /* Completed without error */ 3823 target = NULL; /* Wildcarded */ 3824 device = NULL; /* Wildcarded */ 3825 3826 /* 3827 * We will potentially modify the EDT, so block interrupts 3828 * that may attempt to create cam paths. 3829 */ 3830 s = splcam(); 3831 bus = xpt_find_bus(path_id); 3832 if (bus == NULL) { 3833 status = CAM_PATH_INVALID; 3834 } else { 3835 target = xpt_find_target(bus, target_id); 3836 if (target == NULL) { 3837 /* Create one */ 3838 struct cam_et *new_target; 3839 3840 new_target = xpt_alloc_target(bus, target_id); 3841 if (new_target == NULL) { 3842 status = CAM_RESRC_UNAVAIL; 3843 } else { 3844 target = new_target; 3845 } 3846 } 3847 if (target != NULL) { 3848 device = xpt_find_device(target, lun_id); 3849 if (device == NULL) { 3850 /* Create one */ 3851 struct cam_ed *new_device; 3852 3853 new_device = xpt_alloc_device(bus, 3854 target, 3855 lun_id); 3856 if (new_device == NULL) { 3857 status = CAM_RESRC_UNAVAIL; 3858 } else { 3859 device = new_device; 3860 } 3861 } 3862 } 3863 } 3864 splx(s); 3865 3866 /* 3867 * Only touch the user's data if we are successful. 3868 */ 3869 if (status == CAM_REQ_CMP) { 3870 new_path->periph = perph; 3871 new_path->bus = bus; 3872 new_path->target = target; 3873 new_path->device = device; 3874 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n")); 3875 } else { 3876 if (device != NULL) 3877 xpt_release_device(bus, target, device); 3878 if (target != NULL) 3879 xpt_release_target(bus, target); 3880 if (bus != NULL) 3881 xpt_release_bus(bus); 3882 } 3883 return (status); 3884 } 3885 3886 static void 3887 xpt_release_path(struct cam_path *path) 3888 { 3889 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n")); 3890 if (path->device != NULL) { 3891 xpt_release_device(path->bus, path->target, path->device); 3892 path->device = NULL; 3893 } 3894 if (path->target != NULL) { 3895 xpt_release_target(path->bus, path->target); 3896 path->target = NULL; 3897 } 3898 if (path->bus != NULL) { 3899 xpt_release_bus(path->bus); 3900 path->bus = NULL; 3901 } 3902 } 3903 3904 void 3905 xpt_free_path(struct cam_path *path) 3906 { 3907 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n")); 3908 xpt_release_path(path); 3909 free(path, M_DEVBUF); 3910 } 3911 3912 3913 /* 3914 * Return -1 for failure, 0 for exact match, 1 for match with wildcards 3915 * in path1, 2 for match with wildcards in path2. 3916 */ 3917 int 3918 xpt_path_comp(struct cam_path *path1, struct cam_path *path2) 3919 { 3920 int retval = 0; 3921 3922 if (path1->bus != path2->bus) { 3923 if (path1->bus->path_id == CAM_BUS_WILDCARD) 3924 retval = 1; 3925 else if (path2->bus->path_id == CAM_BUS_WILDCARD) 3926 retval = 2; 3927 else 3928 return (-1); 3929 } 3930 if (path1->target != path2->target) { 3931 if (path1->target->target_id == CAM_TARGET_WILDCARD) { 3932 if (retval == 0) 3933 retval = 1; 3934 } else if (path2->target->target_id == CAM_TARGET_WILDCARD) 3935 retval = 2; 3936 else 3937 return (-1); 3938 } 3939 if (path1->device != path2->device) { 3940 if (path1->device->lun_id == CAM_LUN_WILDCARD) { 3941 if (retval == 0) 3942 retval = 1; 3943 } else if (path2->device->lun_id == CAM_LUN_WILDCARD) 3944 retval = 2; 3945 else 3946 return (-1); 3947 } 3948 return (retval); 3949 } 3950 3951 void 3952 xpt_print_path(struct cam_path *path) 3953 { 3954 if (path == NULL) 3955 printf("(nopath): "); 3956 else { 3957 if (path->periph != NULL) 3958 printf("(%s%d:", path->periph->periph_name, 3959 path->periph->unit_number); 3960 else 3961 printf("(noperiph:"); 3962 3963 if (path->bus != NULL) 3964 printf("%s%d:%d:", path->bus->sim->sim_name, 3965 path->bus->sim->unit_number, 3966 path->bus->sim->bus_id); 3967 else 3968 printf("nobus:"); 3969 3970 if (path->target != NULL) 3971 printf("%d:", path->target->target_id); 3972 else 3973 printf("X:"); 3974 3975 if (path->device != NULL) 3976 printf("%d): ", path->device->lun_id); 3977 else 3978 printf("X): "); 3979 } 3980 } 3981 3982 path_id_t 3983 xpt_path_path_id(struct cam_path *path) 3984 { 3985 return(path->bus->path_id); 3986 } 3987 3988 target_id_t 3989 xpt_path_target_id(struct cam_path *path) 3990 { 3991 if (path->target != NULL) 3992 return (path->target->target_id); 3993 else 3994 return (CAM_TARGET_WILDCARD); 3995 } 3996 3997 lun_id_t 3998 xpt_path_lun_id(struct cam_path *path) 3999 { 4000 if (path->device != NULL) 4001 return (path->device->lun_id); 4002 else 4003 return (CAM_LUN_WILDCARD); 4004 } 4005 4006 struct cam_sim * 4007 xpt_path_sim(struct cam_path *path) 4008 { 4009 return (path->bus->sim); 4010 } 4011 4012 struct cam_periph* 4013 xpt_path_periph(struct cam_path *path) 4014 { 4015 return (path->periph); 4016 } 4017 4018 /* 4019 * Release a CAM control block for the caller. Remit the cost of the structure 4020 * to the device referenced by the path. If the this device had no 'credits' 4021 * and peripheral drivers have registered async callbacks for this notification 4022 * call them now. 4023 */ 4024 void 4025 xpt_release_ccb(union ccb *free_ccb) 4026 { 4027 int s; 4028 struct cam_path *path; 4029 struct cam_ed *device; 4030 struct cam_eb *bus; 4031 4032 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n")); 4033 path = free_ccb->ccb_h.path; 4034 device = path->device; 4035 bus = path->bus; 4036 s = splsoftcam(); 4037 cam_ccbq_release_opening(&device->ccbq); 4038 if (xpt_ccb_count > xpt_max_ccbs) { 4039 xpt_free_ccb(free_ccb); 4040 xpt_ccb_count--; 4041 } else { 4042 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle); 4043 } 4044 bus->sim->devq->alloc_openings++; 4045 bus->sim->devq->alloc_active--; 4046 /* XXX Turn this into an inline function - xpt_run_device?? */ 4047 if ((device_is_alloc_queued(device) == 0) 4048 && (device->drvq.entries > 0)) { 4049 xpt_schedule_dev_allocq(bus, device); 4050 } 4051 splx(s); 4052 if (dev_allocq_is_runnable(bus->sim->devq)) 4053 xpt_run_dev_allocq(bus); 4054 } 4055 4056 /* Functions accessed by SIM drivers */ 4057 4058 /* 4059 * A sim structure, listing the SIM entry points and instance 4060 * identification info is passed to xpt_bus_register to hook the SIM 4061 * into the CAM framework. xpt_bus_register creates a cam_eb entry 4062 * for this new bus and places it in the array of busses and assigns 4063 * it a path_id. The path_id may be influenced by "hard wiring" 4064 * information specified by the user. Once interrupt services are 4065 * availible, the bus will be probed. 4066 */ 4067 int32_t 4068 xpt_bus_register(struct cam_sim *sim, u_int32_t bus) 4069 { 4070 struct cam_eb *new_bus; 4071 struct cam_eb *old_bus; 4072 struct ccb_pathinq cpi; 4073 int s; 4074 4075 sim->bus_id = bus; 4076 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus), 4077 M_DEVBUF, M_NOWAIT); 4078 if (new_bus == NULL) { 4079 /* Couldn't satisfy request */ 4080 return (CAM_RESRC_UNAVAIL); 4081 } 4082 4083 if (strcmp(sim->sim_name, "xpt") != 0) { 4084 4085 sim->path_id = 4086 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id); 4087 } 4088 4089 TAILQ_INIT(&new_bus->et_entries); 4090 new_bus->path_id = sim->path_id; 4091 new_bus->sim = sim; 4092 timevalclear(&new_bus->last_reset); 4093 new_bus->flags = 0; 4094 new_bus->refcount = 1; /* Held until a bus_deregister event */ 4095 new_bus->generation = 0; 4096 s = splcam(); 4097 old_bus = TAILQ_FIRST(&xpt_busses); 4098 while (old_bus != NULL 4099 && old_bus->path_id < new_bus->path_id) 4100 old_bus = TAILQ_NEXT(old_bus, links); 4101 if (old_bus != NULL) 4102 TAILQ_INSERT_BEFORE(old_bus, new_bus, links); 4103 else 4104 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links); 4105 bus_generation++; 4106 splx(s); 4107 4108 /* Notify interested parties */ 4109 if (sim->path_id != CAM_XPT_PATH_ID) { 4110 struct cam_path path; 4111 4112 xpt_compile_path(&path, /*periph*/NULL, sim->path_id, 4113 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 4114 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1); 4115 cpi.ccb_h.func_code = XPT_PATH_INQ; 4116 xpt_action((union ccb *)&cpi); 4117 xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi); 4118 xpt_release_path(&path); 4119 } 4120 return (CAM_SUCCESS); 4121 } 4122 4123 int32_t 4124 xpt_bus_deregister(path_id_t pathid) 4125 { 4126 struct cam_path bus_path; 4127 cam_status status; 4128 4129 status = xpt_compile_path(&bus_path, NULL, pathid, 4130 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 4131 if (status != CAM_REQ_CMP) 4132 return (status); 4133 4134 xpt_async(AC_LOST_DEVICE, &bus_path, NULL); 4135 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL); 4136 4137 /* Release the reference count held while registered. */ 4138 xpt_release_bus(bus_path.bus); 4139 xpt_release_path(&bus_path); 4140 4141 return (CAM_REQ_CMP); 4142 } 4143 4144 static path_id_t 4145 xptnextfreepathid(void) 4146 { 4147 struct cam_eb *bus; 4148 path_id_t pathid; 4149 char *strval; 4150 4151 pathid = 0; 4152 bus = TAILQ_FIRST(&xpt_busses); 4153 retry: 4154 /* Find an unoccupied pathid */ 4155 while (bus != NULL 4156 && bus->path_id <= pathid) { 4157 if (bus->path_id == pathid) 4158 pathid++; 4159 bus = TAILQ_NEXT(bus, links); 4160 } 4161 4162 /* 4163 * Ensure that this pathid is not reserved for 4164 * a bus that may be registered in the future. 4165 */ 4166 if (resource_string_value("scbus", pathid, "at", &strval) == 0) { 4167 ++pathid; 4168 /* Start the search over */ 4169 goto retry; 4170 } 4171 return (pathid); 4172 } 4173 4174 static path_id_t 4175 xptpathid(const char *sim_name, int sim_unit, int sim_bus) 4176 { 4177 path_id_t pathid; 4178 int i, dunit, val; 4179 char buf[32], *strval; 4180 4181 pathid = CAM_XPT_PATH_ID; 4182 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit); 4183 i = -1; 4184 while ((i = resource_locate(i, "scbus")) != -1) { 4185 dunit = resource_query_unit(i); 4186 if (dunit < 0) /* unwired?! */ 4187 continue; 4188 if (resource_string_value("scbus", dunit, "at", &strval) != 0) 4189 continue; 4190 if (strcmp(buf, strval) != 0) 4191 continue; 4192 if (resource_int_value("scbus", dunit, "bus", &val) == 0) { 4193 if (sim_bus == val) { 4194 pathid = dunit; 4195 break; 4196 } 4197 } else if (sim_bus == 0) { 4198 /* Unspecified matches bus 0 */ 4199 pathid = dunit; 4200 break; 4201 } else { 4202 printf("Ambiguous scbus configuration for %s%d " 4203 "bus %d, cannot wire down. The kernel " 4204 "config entry for scbus%d should " 4205 "specify a controller bus.\n" 4206 "Scbus will be assigned dynamically.\n", 4207 sim_name, sim_unit, sim_bus, dunit); 4208 break; 4209 } 4210 } 4211 4212 if (pathid == CAM_XPT_PATH_ID) 4213 pathid = xptnextfreepathid(); 4214 return (pathid); 4215 } 4216 4217 void 4218 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg) 4219 { 4220 struct cam_eb *bus; 4221 struct cam_et *target, *next_target; 4222 struct cam_ed *device, *next_device; 4223 int s; 4224 4225 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n")); 4226 4227 /* 4228 * Most async events come from a CAM interrupt context. In 4229 * a few cases, the error recovery code at the peripheral layer, 4230 * which may run from our SWI or a process context, may signal 4231 * deferred events with a call to xpt_async. Ensure async 4232 * notifications are serialized by blocking cam interrupts. 4233 */ 4234 s = splcam(); 4235 4236 bus = path->bus; 4237 4238 if (async_code == AC_BUS_RESET) { 4239 int s; 4240 4241 s = splclock(); 4242 /* Update our notion of when the last reset occurred */ 4243 microtime(&bus->last_reset); 4244 splx(s); 4245 } 4246 4247 for (target = TAILQ_FIRST(&bus->et_entries); 4248 target != NULL; 4249 target = next_target) { 4250 4251 next_target = TAILQ_NEXT(target, links); 4252 4253 if (path->target != target 4254 && path->target->target_id != CAM_TARGET_WILDCARD 4255 && target->target_id != CAM_TARGET_WILDCARD) 4256 continue; 4257 4258 if (async_code == AC_SENT_BDR) { 4259 int s; 4260 4261 /* Update our notion of when the last reset occurred */ 4262 s = splclock(); 4263 microtime(&path->target->last_reset); 4264 splx(s); 4265 } 4266 4267 for (device = TAILQ_FIRST(&target->ed_entries); 4268 device != NULL; 4269 device = next_device) { 4270 4271 next_device = TAILQ_NEXT(device, links); 4272 4273 if (path->device != device 4274 && path->device->lun_id != CAM_LUN_WILDCARD 4275 && device->lun_id != CAM_LUN_WILDCARD) 4276 continue; 4277 4278 xpt_dev_async(async_code, bus, target, 4279 device, async_arg); 4280 4281 xpt_async_bcast(&device->asyncs, async_code, 4282 path, async_arg); 4283 } 4284 } 4285 4286 /* 4287 * If this wasn't a fully wildcarded async, tell all 4288 * clients that want all async events. 4289 */ 4290 if (bus != xpt_periph->path->bus) 4291 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code, 4292 path, async_arg); 4293 splx(s); 4294 } 4295 4296 static void 4297 xpt_async_bcast(struct async_list *async_head, 4298 u_int32_t async_code, 4299 struct cam_path *path, void *async_arg) 4300 { 4301 struct async_node *cur_entry; 4302 4303 cur_entry = SLIST_FIRST(async_head); 4304 while (cur_entry != NULL) { 4305 struct async_node *next_entry; 4306 /* 4307 * Grab the next list entry before we call the current 4308 * entry's callback. This is because the callback function 4309 * can delete its async callback entry. 4310 */ 4311 next_entry = SLIST_NEXT(cur_entry, links); 4312 if ((cur_entry->event_enable & async_code) != 0) 4313 cur_entry->callback(cur_entry->callback_arg, 4314 async_code, path, 4315 async_arg); 4316 cur_entry = next_entry; 4317 } 4318 } 4319 4320 /* 4321 * Handle any per-device event notifications that require action by the XPT. 4322 */ 4323 static void 4324 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target, 4325 struct cam_ed *device, void *async_arg) 4326 { 4327 cam_status status; 4328 struct cam_path newpath; 4329 4330 /* 4331 * We only need to handle events for real devices. 4332 */ 4333 if (target->target_id == CAM_TARGET_WILDCARD 4334 || device->lun_id == CAM_LUN_WILDCARD) 4335 return; 4336 4337 /* 4338 * We need our own path with wildcards expanded to 4339 * handle certain types of events. 4340 */ 4341 if ((async_code == AC_SENT_BDR) 4342 || (async_code == AC_BUS_RESET) 4343 || (async_code == AC_INQ_CHANGED)) 4344 status = xpt_compile_path(&newpath, NULL, 4345 bus->path_id, 4346 target->target_id, 4347 device->lun_id); 4348 else 4349 status = CAM_REQ_CMP_ERR; 4350 4351 if (status == CAM_REQ_CMP) { 4352 4353 /* 4354 * Allow transfer negotiation to occur in a 4355 * tag free environment. 4356 */ 4357 if (async_code == AC_SENT_BDR 4358 || async_code == AC_BUS_RESET) 4359 xpt_toggle_tags(&newpath); 4360 4361 if (async_code == AC_INQ_CHANGED) { 4362 /* 4363 * We've sent a start unit command, or 4364 * something similar to a device that 4365 * may have caused its inquiry data to 4366 * change. So we re-scan the device to 4367 * refresh the inquiry data for it. 4368 */ 4369 xpt_scan_lun(newpath.periph, &newpath, 4370 CAM_EXPECT_INQ_CHANGE, NULL); 4371 } 4372 xpt_release_path(&newpath); 4373 } else if (async_code == AC_LOST_DEVICE) { 4374 device->flags |= CAM_DEV_UNCONFIGURED; 4375 } else if (async_code == AC_TRANSFER_NEG) { 4376 struct ccb_trans_settings *settings; 4377 4378 settings = (struct ccb_trans_settings *)async_arg; 4379 xpt_set_transfer_settings(settings, device, 4380 /*async_update*/TRUE); 4381 } 4382 } 4383 4384 u_int32_t 4385 xpt_freeze_devq(struct cam_path *path, u_int count) 4386 { 4387 int s; 4388 struct ccb_hdr *ccbh; 4389 4390 s = splcam(); 4391 path->device->qfrozen_cnt += count; 4392 4393 /* 4394 * Mark the last CCB in the queue as needing 4395 * to be requeued if the driver hasn't 4396 * changed it's state yet. This fixes a race 4397 * where a ccb is just about to be queued to 4398 * a controller driver when it's interrupt routine 4399 * freezes the queue. To completly close the 4400 * hole, controller drives must check to see 4401 * if a ccb's status is still CAM_REQ_INPROG 4402 * under spl protection just before they queue 4403 * the CCB. See ahc_action/ahc_freeze_devq for 4404 * an example. 4405 */ 4406 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq); 4407 if (ccbh && ccbh->status == CAM_REQ_INPROG) 4408 ccbh->status = CAM_REQUEUE_REQ; 4409 splx(s); 4410 return (path->device->qfrozen_cnt); 4411 } 4412 4413 u_int32_t 4414 xpt_freeze_simq(struct cam_sim *sim, u_int count) 4415 { 4416 sim->devq->send_queue.qfrozen_cnt += count; 4417 if (sim->devq->active_dev != NULL) { 4418 struct ccb_hdr *ccbh; 4419 4420 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs, 4421 ccb_hdr_tailq); 4422 if (ccbh && ccbh->status == CAM_REQ_INPROG) 4423 ccbh->status = CAM_REQUEUE_REQ; 4424 } 4425 return (sim->devq->send_queue.qfrozen_cnt); 4426 } 4427 4428 static void 4429 xpt_release_devq_timeout(void *arg) 4430 { 4431 struct cam_ed *device; 4432 4433 device = (struct cam_ed *)arg; 4434 4435 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE); 4436 } 4437 4438 void 4439 xpt_release_devq(struct cam_path *path, u_int count, int run_queue) 4440 { 4441 xpt_release_devq_device(path->device, count, run_queue); 4442 } 4443 4444 static void 4445 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue) 4446 { 4447 int rundevq; 4448 int s0, s1; 4449 4450 rundevq = 0; 4451 s0 = splsoftcam(); 4452 s1 = splcam(); 4453 if (dev->qfrozen_cnt > 0) { 4454 4455 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count; 4456 dev->qfrozen_cnt -= count; 4457 if (dev->qfrozen_cnt == 0) { 4458 4459 /* 4460 * No longer need to wait for a successful 4461 * command completion. 4462 */ 4463 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE; 4464 4465 /* 4466 * Remove any timeouts that might be scheduled 4467 * to release this queue. 4468 */ 4469 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 4470 untimeout(xpt_release_devq_timeout, dev, 4471 dev->c_handle); 4472 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING; 4473 } 4474 4475 /* 4476 * Now that we are unfrozen schedule the 4477 * device so any pending transactions are 4478 * run. 4479 */ 4480 if ((dev->ccbq.queue.entries > 0) 4481 && (xpt_schedule_dev_sendq(dev->target->bus, dev)) 4482 && (run_queue != 0)) { 4483 rundevq = 1; 4484 } 4485 } 4486 } 4487 splx(s1); 4488 if (rundevq != 0) 4489 xpt_run_dev_sendq(dev->target->bus); 4490 splx(s0); 4491 } 4492 4493 void 4494 xpt_release_simq(struct cam_sim *sim, int run_queue) 4495 { 4496 int s; 4497 struct camq *sendq; 4498 4499 sendq = &(sim->devq->send_queue); 4500 s = splcam(); 4501 if (sendq->qfrozen_cnt > 0) { 4502 4503 sendq->qfrozen_cnt--; 4504 if (sendq->qfrozen_cnt == 0) { 4505 struct cam_eb *bus; 4506 4507 /* 4508 * If there is a timeout scheduled to release this 4509 * sim queue, remove it. The queue frozen count is 4510 * already at 0. 4511 */ 4512 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){ 4513 untimeout(xpt_release_simq_timeout, sim, 4514 sim->c_handle); 4515 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING; 4516 } 4517 bus = xpt_find_bus(sim->path_id); 4518 splx(s); 4519 4520 if (run_queue) { 4521 /* 4522 * Now that we are unfrozen run the send queue. 4523 */ 4524 xpt_run_dev_sendq(bus); 4525 } 4526 xpt_release_bus(bus); 4527 } else 4528 splx(s); 4529 } else 4530 splx(s); 4531 } 4532 4533 static void 4534 xpt_release_simq_timeout(void *arg) 4535 { 4536 struct cam_sim *sim; 4537 4538 sim = (struct cam_sim *)arg; 4539 xpt_release_simq(sim, /* run_queue */ TRUE); 4540 } 4541 4542 void 4543 xpt_done(union ccb *done_ccb) 4544 { 4545 int s; 4546 4547 s = splcam(); 4548 4549 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n")); 4550 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) { 4551 /* 4552 * Queue up the request for handling by our SWI handler 4553 * any of the "non-immediate" type of ccbs. 4554 */ 4555 switch (done_ccb->ccb_h.path->periph->type) { 4556 case CAM_PERIPH_BIO: 4557 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h, 4558 sim_links.tqe); 4559 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX; 4560 setsoftcambio(); 4561 break; 4562 case CAM_PERIPH_NET: 4563 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h, 4564 sim_links.tqe); 4565 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX; 4566 setsoftcamnet(); 4567 break; 4568 } 4569 } 4570 splx(s); 4571 } 4572 4573 union ccb * 4574 xpt_alloc_ccb() 4575 { 4576 union ccb *new_ccb; 4577 4578 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK); 4579 return (new_ccb); 4580 } 4581 4582 void 4583 xpt_free_ccb(union ccb *free_ccb) 4584 { 4585 free(free_ccb, M_DEVBUF); 4586 } 4587 4588 4589 4590 /* Private XPT functions */ 4591 4592 /* 4593 * Get a CAM control block for the caller. Charge the structure to the device 4594 * referenced by the path. If the this device has no 'credits' then the 4595 * device already has the maximum number of outstanding operations under way 4596 * and we return NULL. If we don't have sufficient resources to allocate more 4597 * ccbs, we also return NULL. 4598 */ 4599 static union ccb * 4600 xpt_get_ccb(struct cam_ed *device) 4601 { 4602 union ccb *new_ccb; 4603 int s; 4604 4605 s = splsoftcam(); 4606 if ((new_ccb = (union ccb *)ccb_freeq.slh_first) == NULL) { 4607 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT); 4608 if (new_ccb == NULL) { 4609 splx(s); 4610 return (NULL); 4611 } 4612 callout_handle_init(&new_ccb->ccb_h.timeout_ch); 4613 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h, 4614 xpt_links.sle); 4615 xpt_ccb_count++; 4616 } 4617 cam_ccbq_take_opening(&device->ccbq); 4618 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle); 4619 splx(s); 4620 return (new_ccb); 4621 } 4622 4623 static void 4624 xpt_release_bus(struct cam_eb *bus) 4625 { 4626 int s; 4627 4628 s = splcam(); 4629 if ((--bus->refcount == 0) 4630 && (TAILQ_FIRST(&bus->et_entries) == NULL)) { 4631 TAILQ_REMOVE(&xpt_busses, bus, links); 4632 bus_generation++; 4633 splx(s); 4634 free(bus, M_DEVBUF); 4635 } else 4636 splx(s); 4637 } 4638 4639 static struct cam_et * 4640 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id) 4641 { 4642 struct cam_et *target; 4643 4644 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT); 4645 if (target != NULL) { 4646 struct cam_et *cur_target; 4647 4648 TAILQ_INIT(&target->ed_entries); 4649 target->bus = bus; 4650 target->target_id = target_id; 4651 target->refcount = 1; 4652 target->generation = 0; 4653 timevalclear(&target->last_reset); 4654 /* 4655 * Hold a reference to our parent bus so it 4656 * will not go away before we do. 4657 */ 4658 bus->refcount++; 4659 4660 /* Insertion sort into our bus's target list */ 4661 cur_target = TAILQ_FIRST(&bus->et_entries); 4662 while (cur_target != NULL && cur_target->target_id < target_id) 4663 cur_target = TAILQ_NEXT(cur_target, links); 4664 4665 if (cur_target != NULL) { 4666 TAILQ_INSERT_BEFORE(cur_target, target, links); 4667 } else { 4668 TAILQ_INSERT_TAIL(&bus->et_entries, target, links); 4669 } 4670 bus->generation++; 4671 } 4672 return (target); 4673 } 4674 4675 static void 4676 xpt_release_target(struct cam_eb *bus, struct cam_et *target) 4677 { 4678 int s; 4679 4680 s = splcam(); 4681 if ((--target->refcount == 0) 4682 && (TAILQ_FIRST(&target->ed_entries) == NULL)) { 4683 TAILQ_REMOVE(&bus->et_entries, target, links); 4684 bus->generation++; 4685 splx(s); 4686 free(target, M_DEVBUF); 4687 xpt_release_bus(bus); 4688 } else 4689 splx(s); 4690 } 4691 4692 static struct cam_ed * 4693 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 4694 { 4695 struct cam_ed *device; 4696 struct cam_devq *devq; 4697 cam_status status; 4698 4699 /* Make space for us in the device queue on our bus */ 4700 devq = bus->sim->devq; 4701 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1); 4702 4703 if (status != CAM_REQ_CMP) { 4704 device = NULL; 4705 } else { 4706 device = (struct cam_ed *)malloc(sizeof(*device), 4707 M_DEVBUF, M_NOWAIT); 4708 } 4709 4710 if (device != NULL) { 4711 struct cam_ed *cur_device; 4712 4713 cam_init_pinfo(&device->alloc_ccb_entry.pinfo); 4714 device->alloc_ccb_entry.device = device; 4715 cam_init_pinfo(&device->send_ccb_entry.pinfo); 4716 device->send_ccb_entry.device = device; 4717 device->target = target; 4718 device->lun_id = lun_id; 4719 /* Initialize our queues */ 4720 if (camq_init(&device->drvq, 0) != 0) { 4721 free(device, M_DEVBUF); 4722 return (NULL); 4723 } 4724 if (cam_ccbq_init(&device->ccbq, 4725 bus->sim->max_dev_openings) != 0) { 4726 camq_fini(&device->drvq); 4727 free(device, M_DEVBUF); 4728 return (NULL); 4729 } 4730 SLIST_INIT(&device->asyncs); 4731 SLIST_INIT(&device->periphs); 4732 device->generation = 0; 4733 device->owner = NULL; 4734 /* 4735 * Take the default quirk entry until we have inquiry 4736 * data and can determine a better quirk to use. 4737 */ 4738 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1]; 4739 bzero(&device->inq_data, sizeof(device->inq_data)); 4740 device->inq_flags = 0; 4741 device->queue_flags = 0; 4742 device->serial_num = NULL; 4743 device->serial_num_len = 0; 4744 device->qfrozen_cnt = 0; 4745 device->flags = CAM_DEV_UNCONFIGURED; 4746 device->tag_delay_count = 0; 4747 device->refcount = 1; 4748 callout_handle_init(&device->c_handle); 4749 4750 /* 4751 * Hold a reference to our parent target so it 4752 * will not go away before we do. 4753 */ 4754 target->refcount++; 4755 4756 /* 4757 * XXX should be limited by number of CCBs this bus can 4758 * do. 4759 */ 4760 xpt_max_ccbs += device->ccbq.devq_openings; 4761 /* Insertion sort into our target's device list */ 4762 cur_device = TAILQ_FIRST(&target->ed_entries); 4763 while (cur_device != NULL && cur_device->lun_id < lun_id) 4764 cur_device = TAILQ_NEXT(cur_device, links); 4765 if (cur_device != NULL) { 4766 TAILQ_INSERT_BEFORE(cur_device, device, links); 4767 } else { 4768 TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 4769 } 4770 target->generation++; 4771 } 4772 return (device); 4773 } 4774 4775 static void 4776 xpt_release_device(struct cam_eb *bus, struct cam_et *target, 4777 struct cam_ed *device) 4778 { 4779 int s; 4780 4781 s = splcam(); 4782 if ((--device->refcount == 0) 4783 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) { 4784 struct cam_devq *devq; 4785 4786 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX 4787 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX) 4788 panic("Removing device while still queued for ccbs"); 4789 4790 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) 4791 untimeout(xpt_release_devq_timeout, device, 4792 device->c_handle); 4793 4794 TAILQ_REMOVE(&target->ed_entries, device,links); 4795 target->generation++; 4796 xpt_max_ccbs -= device->ccbq.devq_openings; 4797 /* Release our slot in the devq */ 4798 devq = bus->sim->devq; 4799 cam_devq_resize(devq, devq->alloc_queue.array_size - 1); 4800 splx(s); 4801 free(device, M_DEVBUF); 4802 xpt_release_target(bus, target); 4803 } else 4804 splx(s); 4805 } 4806 4807 static u_int32_t 4808 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings) 4809 { 4810 int s; 4811 int diff; 4812 int result; 4813 struct cam_ed *dev; 4814 4815 dev = path->device; 4816 s = splsoftcam(); 4817 4818 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings); 4819 result = cam_ccbq_resize(&dev->ccbq, newopenings); 4820 if (result == CAM_REQ_CMP && (diff < 0)) { 4821 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED; 4822 } 4823 /* Adjust the global limit */ 4824 xpt_max_ccbs += diff; 4825 splx(s); 4826 return (result); 4827 } 4828 4829 static struct cam_eb * 4830 xpt_find_bus(path_id_t path_id) 4831 { 4832 struct cam_eb *bus; 4833 4834 for (bus = TAILQ_FIRST(&xpt_busses); 4835 bus != NULL; 4836 bus = TAILQ_NEXT(bus, links)) { 4837 if (bus->path_id == path_id) { 4838 bus->refcount++; 4839 break; 4840 } 4841 } 4842 return (bus); 4843 } 4844 4845 static struct cam_et * 4846 xpt_find_target(struct cam_eb *bus, target_id_t target_id) 4847 { 4848 struct cam_et *target; 4849 4850 for (target = TAILQ_FIRST(&bus->et_entries); 4851 target != NULL; 4852 target = TAILQ_NEXT(target, links)) { 4853 if (target->target_id == target_id) { 4854 target->refcount++; 4855 break; 4856 } 4857 } 4858 return (target); 4859 } 4860 4861 static struct cam_ed * 4862 xpt_find_device(struct cam_et *target, lun_id_t lun_id) 4863 { 4864 struct cam_ed *device; 4865 4866 for (device = TAILQ_FIRST(&target->ed_entries); 4867 device != NULL; 4868 device = TAILQ_NEXT(device, links)) { 4869 if (device->lun_id == lun_id) { 4870 device->refcount++; 4871 break; 4872 } 4873 } 4874 return (device); 4875 } 4876 4877 typedef struct { 4878 union ccb *request_ccb; 4879 struct ccb_pathinq *cpi; 4880 int pending_count; 4881 } xpt_scan_bus_info; 4882 4883 /* 4884 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb. 4885 * As the scan progresses, xpt_scan_bus is used as the 4886 * callback on completion function. 4887 */ 4888 static void 4889 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb) 4890 { 4891 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 4892 ("xpt_scan_bus\n")); 4893 switch (request_ccb->ccb_h.func_code) { 4894 case XPT_SCAN_BUS: 4895 { 4896 xpt_scan_bus_info *scan_info; 4897 union ccb *work_ccb; 4898 struct cam_path *path; 4899 u_int i; 4900 u_int max_target; 4901 u_int initiator_id; 4902 4903 /* Find out the characteristics of the bus */ 4904 work_ccb = xpt_alloc_ccb(); 4905 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path, 4906 request_ccb->ccb_h.pinfo.priority); 4907 work_ccb->ccb_h.func_code = XPT_PATH_INQ; 4908 xpt_action(work_ccb); 4909 if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 4910 request_ccb->ccb_h.status = work_ccb->ccb_h.status; 4911 xpt_free_ccb(work_ccb); 4912 xpt_done(request_ccb); 4913 return; 4914 } 4915 4916 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) { 4917 /* 4918 * Can't scan the bus on an adapter that 4919 * cannot perform the initiator role. 4920 */ 4921 request_ccb->ccb_h.status = CAM_REQ_CMP; 4922 xpt_free_ccb(work_ccb); 4923 xpt_done(request_ccb); 4924 return; 4925 } 4926 4927 /* Save some state for use while we probe for devices */ 4928 scan_info = (xpt_scan_bus_info *) 4929 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK); 4930 scan_info->request_ccb = request_ccb; 4931 scan_info->cpi = &work_ccb->cpi; 4932 4933 /* Cache on our stack so we can work asynchronously */ 4934 max_target = scan_info->cpi->max_target; 4935 initiator_id = scan_info->cpi->initiator_id; 4936 4937 /* 4938 * Don't count the initiator if the 4939 * initiator is addressable. 4940 */ 4941 scan_info->pending_count = max_target + 1; 4942 if (initiator_id <= max_target) 4943 scan_info->pending_count--; 4944 4945 for (i = 0; i <= max_target; i++) { 4946 cam_status status; 4947 if (i == initiator_id) 4948 continue; 4949 4950 status = xpt_create_path(&path, xpt_periph, 4951 request_ccb->ccb_h.path_id, 4952 i, 0); 4953 if (status != CAM_REQ_CMP) { 4954 printf("xpt_scan_bus: xpt_create_path failed" 4955 " with status %#x, bus scan halted\n", 4956 status); 4957 break; 4958 } 4959 work_ccb = xpt_alloc_ccb(); 4960 xpt_setup_ccb(&work_ccb->ccb_h, path, 4961 request_ccb->ccb_h.pinfo.priority); 4962 work_ccb->ccb_h.func_code = XPT_SCAN_LUN; 4963 work_ccb->ccb_h.cbfcnp = xpt_scan_bus; 4964 work_ccb->ccb_h.ppriv_ptr0 = scan_info; 4965 work_ccb->crcn.flags = request_ccb->crcn.flags; 4966 #if 0 4967 printf("xpt_scan_bus: probing %d:%d:%d\n", 4968 request_ccb->ccb_h.path_id, i, 0); 4969 #endif 4970 xpt_action(work_ccb); 4971 } 4972 break; 4973 } 4974 case XPT_SCAN_LUN: 4975 { 4976 xpt_scan_bus_info *scan_info; 4977 path_id_t path_id; 4978 target_id_t target_id; 4979 lun_id_t lun_id; 4980 4981 /* Reuse the same CCB to query if a device was really found */ 4982 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0; 4983 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path, 4984 request_ccb->ccb_h.pinfo.priority); 4985 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 4986 4987 path_id = request_ccb->ccb_h.path_id; 4988 target_id = request_ccb->ccb_h.target_id; 4989 lun_id = request_ccb->ccb_h.target_lun; 4990 xpt_action(request_ccb); 4991 4992 #if 0 4993 printf("xpt_scan_bus: got back probe from %d:%d:%d\n", 4994 path_id, target_id, lun_id); 4995 #endif 4996 4997 if (request_ccb->ccb_h.status != CAM_REQ_CMP) { 4998 struct cam_ed *device; 4999 struct cam_et *target; 5000 int s, phl; 5001 5002 /* 5003 * If we already probed lun 0 successfully, or 5004 * we have additional configured luns on this 5005 * target that might have "gone away", go onto 5006 * the next lun. 5007 */ 5008 target = request_ccb->ccb_h.path->target; 5009 /* 5010 * We may touch devices that we don't 5011 * hold references too, so ensure they 5012 * don't disappear out from under us. 5013 * The target above is referenced by the 5014 * path in the request ccb. 5015 */ 5016 phl = 0; 5017 s = splcam(); 5018 device = TAILQ_FIRST(&target->ed_entries); 5019 if (device != NULL) { 5020 phl = device->quirk->quirks & CAM_QUIRK_HILUNS; 5021 if (device->lun_id == 0) 5022 device = TAILQ_NEXT(device, links); 5023 } 5024 splx(s); 5025 if ((lun_id != 0) || (device != NULL)) { 5026 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl) 5027 lun_id++; 5028 } 5029 } else { 5030 struct cam_ed *device; 5031 5032 device = request_ccb->ccb_h.path->device; 5033 5034 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) { 5035 /* Try the next lun */ 5036 if (lun_id < (CAM_SCSI2_MAXLUN-1) || 5037 (device->quirk->quirks & CAM_QUIRK_HILUNS)) 5038 lun_id++; 5039 } 5040 } 5041 5042 xpt_free_path(request_ccb->ccb_h.path); 5043 5044 /* Check Bounds */ 5045 if ((lun_id == request_ccb->ccb_h.target_lun) 5046 || lun_id > scan_info->cpi->max_lun) { 5047 /* We're done */ 5048 5049 xpt_free_ccb(request_ccb); 5050 scan_info->pending_count--; 5051 if (scan_info->pending_count == 0) { 5052 xpt_free_ccb((union ccb *)scan_info->cpi); 5053 request_ccb = scan_info->request_ccb; 5054 free(scan_info, M_TEMP); 5055 request_ccb->ccb_h.status = CAM_REQ_CMP; 5056 xpt_done(request_ccb); 5057 } 5058 } else { 5059 /* Try the next device */ 5060 struct cam_path *path; 5061 cam_status status; 5062 5063 path = request_ccb->ccb_h.path; 5064 status = xpt_create_path(&path, xpt_periph, 5065 path_id, target_id, lun_id); 5066 if (status != CAM_REQ_CMP) { 5067 printf("xpt_scan_bus: xpt_create_path failed " 5068 "with status %#x, halting LUN scan\n", 5069 status); 5070 xpt_free_ccb(request_ccb); 5071 scan_info->pending_count--; 5072 if (scan_info->pending_count == 0) { 5073 xpt_free_ccb( 5074 (union ccb *)scan_info->cpi); 5075 request_ccb = scan_info->request_ccb; 5076 free(scan_info, M_TEMP); 5077 request_ccb->ccb_h.status = CAM_REQ_CMP; 5078 xpt_done(request_ccb); 5079 break; 5080 } 5081 } 5082 xpt_setup_ccb(&request_ccb->ccb_h, path, 5083 request_ccb->ccb_h.pinfo.priority); 5084 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 5085 request_ccb->ccb_h.cbfcnp = xpt_scan_bus; 5086 request_ccb->ccb_h.ppriv_ptr0 = scan_info; 5087 request_ccb->crcn.flags = 5088 scan_info->request_ccb->crcn.flags; 5089 #if 0 5090 xpt_print_path(path); 5091 printf("xpt_scan bus probing\n"); 5092 #endif 5093 xpt_action(request_ccb); 5094 } 5095 break; 5096 } 5097 default: 5098 break; 5099 } 5100 } 5101 5102 typedef enum { 5103 PROBE_TUR, 5104 PROBE_INQUIRY, 5105 PROBE_FULL_INQUIRY, 5106 PROBE_MODE_SENSE, 5107 PROBE_SERIAL_NUM, 5108 PROBE_TUR_FOR_NEGOTIATION 5109 } probe_action; 5110 5111 typedef enum { 5112 PROBE_INQUIRY_CKSUM = 0x01, 5113 PROBE_SERIAL_CKSUM = 0x02, 5114 PROBE_NO_ANNOUNCE = 0x04 5115 } probe_flags; 5116 5117 typedef struct { 5118 TAILQ_HEAD(, ccb_hdr) request_ccbs; 5119 probe_action action; 5120 union ccb saved_ccb; 5121 probe_flags flags; 5122 MD5_CTX context; 5123 u_int8_t digest[16]; 5124 } probe_softc; 5125 5126 static void 5127 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path, 5128 cam_flags flags, union ccb *request_ccb) 5129 { 5130 struct ccb_pathinq cpi; 5131 cam_status status; 5132 struct cam_path *new_path; 5133 struct cam_periph *old_periph; 5134 int s; 5135 5136 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 5137 ("xpt_scan_lun\n")); 5138 5139 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1); 5140 cpi.ccb_h.func_code = XPT_PATH_INQ; 5141 xpt_action((union ccb *)&cpi); 5142 5143 if (cpi.ccb_h.status != CAM_REQ_CMP) { 5144 if (request_ccb != NULL) { 5145 request_ccb->ccb_h.status = cpi.ccb_h.status; 5146 xpt_done(request_ccb); 5147 } 5148 return; 5149 } 5150 5151 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) { 5152 /* 5153 * Can't scan the bus on an adapter that 5154 * cannot perform the initiator role. 5155 */ 5156 if (request_ccb != NULL) { 5157 request_ccb->ccb_h.status = CAM_REQ_CMP; 5158 xpt_done(request_ccb); 5159 } 5160 return; 5161 } 5162 5163 if (request_ccb == NULL) { 5164 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT); 5165 if (request_ccb == NULL) { 5166 xpt_print_path(path); 5167 printf("xpt_scan_lun: can't allocate CCB, can't " 5168 "continue\n"); 5169 return; 5170 } 5171 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT); 5172 if (new_path == NULL) { 5173 xpt_print_path(path); 5174 printf("xpt_scan_lun: can't allocate path, can't " 5175 "continue\n"); 5176 free(request_ccb, M_TEMP); 5177 return; 5178 } 5179 status = xpt_compile_path(new_path, xpt_periph, 5180 path->bus->path_id, 5181 path->target->target_id, 5182 path->device->lun_id); 5183 5184 if (status != CAM_REQ_CMP) { 5185 xpt_print_path(path); 5186 printf("xpt_scan_lun: can't compile path, can't " 5187 "continue\n"); 5188 free(request_ccb, M_TEMP); 5189 free(new_path, M_TEMP); 5190 return; 5191 } 5192 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1); 5193 request_ccb->ccb_h.cbfcnp = xptscandone; 5194 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 5195 request_ccb->crcn.flags = flags; 5196 } 5197 5198 s = splsoftcam(); 5199 if ((old_periph = cam_periph_find(path, "probe")) != NULL) { 5200 probe_softc *softc; 5201 5202 softc = (probe_softc *)old_periph->softc; 5203 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 5204 periph_links.tqe); 5205 } else { 5206 status = cam_periph_alloc(proberegister, NULL, probecleanup, 5207 probestart, "probe", 5208 CAM_PERIPH_BIO, 5209 request_ccb->ccb_h.path, NULL, 0, 5210 request_ccb); 5211 5212 if (status != CAM_REQ_CMP) { 5213 xpt_print_path(path); 5214 printf("xpt_scan_lun: cam_alloc_periph returned an " 5215 "error, can't continue probe\n"); 5216 request_ccb->ccb_h.status = status; 5217 xpt_done(request_ccb); 5218 } 5219 } 5220 splx(s); 5221 } 5222 5223 static void 5224 xptscandone(struct cam_periph *periph, union ccb *done_ccb) 5225 { 5226 xpt_release_path(done_ccb->ccb_h.path); 5227 free(done_ccb->ccb_h.path, M_TEMP); 5228 free(done_ccb, M_TEMP); 5229 } 5230 5231 static cam_status 5232 proberegister(struct cam_periph *periph, void *arg) 5233 { 5234 union ccb *request_ccb; /* CCB representing the probe request */ 5235 probe_softc *softc; 5236 5237 request_ccb = (union ccb *)arg; 5238 if (periph == NULL) { 5239 printf("proberegister: periph was NULL!!\n"); 5240 return(CAM_REQ_CMP_ERR); 5241 } 5242 5243 if (request_ccb == NULL) { 5244 printf("proberegister: no probe CCB, " 5245 "can't register device\n"); 5246 return(CAM_REQ_CMP_ERR); 5247 } 5248 5249 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT); 5250 5251 if (softc == NULL) { 5252 printf("proberegister: Unable to probe new device. " 5253 "Unable to allocate softc\n"); 5254 return(CAM_REQ_CMP_ERR); 5255 } 5256 TAILQ_INIT(&softc->request_ccbs); 5257 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 5258 periph_links.tqe); 5259 softc->flags = 0; 5260 periph->softc = softc; 5261 cam_periph_acquire(periph); 5262 /* 5263 * Ensure we've waited at least a bus settle 5264 * delay before attempting to probe the device. 5265 * For HBAs that don't do bus resets, this won't make a difference. 5266 */ 5267 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset, 5268 SCSI_DELAY); 5269 probeschedule(periph); 5270 return(CAM_REQ_CMP); 5271 } 5272 5273 static void 5274 probeschedule(struct cam_periph *periph) 5275 { 5276 struct ccb_pathinq cpi; 5277 union ccb *ccb; 5278 probe_softc *softc; 5279 5280 softc = (probe_softc *)periph->softc; 5281 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 5282 5283 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1); 5284 cpi.ccb_h.func_code = XPT_PATH_INQ; 5285 xpt_action((union ccb *)&cpi); 5286 5287 /* 5288 * If a device has gone away and another device, or the same one, 5289 * is back in the same place, it should have a unit attention 5290 * condition pending. It will not report the unit attention in 5291 * response to an inquiry, which may leave invalid transfer 5292 * negotiations in effect. The TUR will reveal the unit attention 5293 * condition. Only send the TUR for lun 0, since some devices 5294 * will get confused by commands other than inquiry to non-existent 5295 * luns. If you think a device has gone away start your scan from 5296 * lun 0. This will insure that any bogus transfer settings are 5297 * invalidated. 5298 * 5299 * If we haven't seen the device before and the controller supports 5300 * some kind of transfer negotiation, negotiate with the first 5301 * sent command if no bus reset was performed at startup. This 5302 * ensures that the device is not confused by transfer negotiation 5303 * settings left over by loader or BIOS action. 5304 */ 5305 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 5306 && (ccb->ccb_h.target_lun == 0)) { 5307 softc->action = PROBE_TUR; 5308 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0 5309 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) { 5310 proberequestdefaultnegotiation(periph); 5311 softc->action = PROBE_INQUIRY; 5312 } else { 5313 softc->action = PROBE_INQUIRY; 5314 } 5315 5316 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE) 5317 softc->flags |= PROBE_NO_ANNOUNCE; 5318 else 5319 softc->flags &= ~PROBE_NO_ANNOUNCE; 5320 5321 xpt_schedule(periph, ccb->ccb_h.pinfo.priority); 5322 } 5323 5324 static void 5325 probestart(struct cam_periph *periph, union ccb *start_ccb) 5326 { 5327 /* Probe the device that our peripheral driver points to */ 5328 struct ccb_scsiio *csio; 5329 probe_softc *softc; 5330 5331 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n")); 5332 5333 softc = (probe_softc *)periph->softc; 5334 csio = &start_ccb->csio; 5335 5336 switch (softc->action) { 5337 case PROBE_TUR: 5338 case PROBE_TUR_FOR_NEGOTIATION: 5339 { 5340 scsi_test_unit_ready(csio, 5341 /*retries*/4, 5342 probedone, 5343 MSG_SIMPLE_Q_TAG, 5344 SSD_FULL_SIZE, 5345 /*timeout*/60000); 5346 break; 5347 } 5348 case PROBE_INQUIRY: 5349 case PROBE_FULL_INQUIRY: 5350 { 5351 u_int inquiry_len; 5352 struct scsi_inquiry_data *inq_buf; 5353 5354 inq_buf = &periph->path->device->inq_data; 5355 /* 5356 * If the device is currently configured, we calculate an 5357 * MD5 checksum of the inquiry data, and if the serial number 5358 * length is greater than 0, add the serial number data 5359 * into the checksum as well. Once the inquiry and the 5360 * serial number check finish, we attempt to figure out 5361 * whether we still have the same device. 5362 */ 5363 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 5364 5365 MD5Init(&softc->context); 5366 MD5Update(&softc->context, (unsigned char *)inq_buf, 5367 sizeof(struct scsi_inquiry_data)); 5368 softc->flags |= PROBE_INQUIRY_CKSUM; 5369 if (periph->path->device->serial_num_len > 0) { 5370 MD5Update(&softc->context, 5371 periph->path->device->serial_num, 5372 periph->path->device->serial_num_len); 5373 softc->flags |= PROBE_SERIAL_CKSUM; 5374 } 5375 MD5Final(softc->digest, &softc->context); 5376 } 5377 5378 if (softc->action == PROBE_INQUIRY) 5379 inquiry_len = SHORT_INQUIRY_LENGTH; 5380 else 5381 inquiry_len = inq_buf->additional_length + 5; 5382 5383 scsi_inquiry(csio, 5384 /*retries*/4, 5385 probedone, 5386 MSG_SIMPLE_Q_TAG, 5387 (u_int8_t *)inq_buf, 5388 inquiry_len, 5389 /*evpd*/FALSE, 5390 /*page_code*/0, 5391 SSD_MIN_SIZE, 5392 /*timeout*/60 * 1000); 5393 break; 5394 } 5395 case PROBE_MODE_SENSE: 5396 { 5397 void *mode_buf; 5398 int mode_buf_len; 5399 5400 mode_buf_len = sizeof(struct scsi_mode_header_6) 5401 + sizeof(struct scsi_mode_blk_desc) 5402 + sizeof(struct scsi_control_page); 5403 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT); 5404 if (mode_buf != NULL) { 5405 scsi_mode_sense(csio, 5406 /*retries*/4, 5407 probedone, 5408 MSG_SIMPLE_Q_TAG, 5409 /*dbd*/FALSE, 5410 SMS_PAGE_CTRL_CURRENT, 5411 SMS_CONTROL_MODE_PAGE, 5412 mode_buf, 5413 mode_buf_len, 5414 SSD_FULL_SIZE, 5415 /*timeout*/60000); 5416 break; 5417 } 5418 xpt_print_path(periph->path); 5419 printf("Unable to mode sense control page - malloc failure\n"); 5420 softc->action = PROBE_SERIAL_NUM; 5421 /* FALLTHROUGH */ 5422 } 5423 case PROBE_SERIAL_NUM: 5424 { 5425 struct scsi_vpd_unit_serial_number *serial_buf; 5426 struct cam_ed* device; 5427 5428 serial_buf = NULL; 5429 device = periph->path->device; 5430 device->serial_num = NULL; 5431 device->serial_num_len = 0; 5432 5433 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0) 5434 serial_buf = (struct scsi_vpd_unit_serial_number *) 5435 malloc(sizeof(*serial_buf), M_TEMP, 5436 M_NOWAIT | M_ZERO); 5437 5438 if (serial_buf != NULL) { 5439 scsi_inquiry(csio, 5440 /*retries*/4, 5441 probedone, 5442 MSG_SIMPLE_Q_TAG, 5443 (u_int8_t *)serial_buf, 5444 sizeof(*serial_buf), 5445 /*evpd*/TRUE, 5446 SVPD_UNIT_SERIAL_NUMBER, 5447 SSD_MIN_SIZE, 5448 /*timeout*/60 * 1000); 5449 break; 5450 } 5451 /* 5452 * We'll have to do without, let our probedone 5453 * routine finish up for us. 5454 */ 5455 start_ccb->csio.data_ptr = NULL; 5456 probedone(periph, start_ccb); 5457 return; 5458 } 5459 } 5460 xpt_action(start_ccb); 5461 } 5462 5463 static void 5464 proberequestdefaultnegotiation(struct cam_periph *periph) 5465 { 5466 struct ccb_trans_settings cts; 5467 5468 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1); 5469 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 5470 cts.flags = CCB_TRANS_USER_SETTINGS; 5471 xpt_action((union ccb *)&cts); 5472 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 5473 cts.flags &= ~CCB_TRANS_USER_SETTINGS; 5474 cts.flags |= CCB_TRANS_CURRENT_SETTINGS; 5475 xpt_action((union ccb *)&cts); 5476 } 5477 5478 static void 5479 probedone(struct cam_periph *periph, union ccb *done_ccb) 5480 { 5481 probe_softc *softc; 5482 struct cam_path *path; 5483 u_int32_t priority; 5484 5485 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n")); 5486 5487 softc = (probe_softc *)periph->softc; 5488 path = done_ccb->ccb_h.path; 5489 priority = done_ccb->ccb_h.pinfo.priority; 5490 5491 switch (softc->action) { 5492 case PROBE_TUR: 5493 { 5494 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 5495 5496 if (cam_periph_error(done_ccb, 0, 5497 SF_NO_PRINT, NULL) == ERESTART) 5498 return; 5499 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 5500 /* Don't wedge the queue */ 5501 xpt_release_devq(done_ccb->ccb_h.path, 5502 /*count*/1, 5503 /*run_queue*/TRUE); 5504 } 5505 softc->action = PROBE_INQUIRY; 5506 xpt_release_ccb(done_ccb); 5507 xpt_schedule(periph, priority); 5508 return; 5509 } 5510 case PROBE_INQUIRY: 5511 case PROBE_FULL_INQUIRY: 5512 { 5513 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 5514 struct scsi_inquiry_data *inq_buf; 5515 u_int8_t periph_qual; 5516 5517 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID; 5518 inq_buf = &path->device->inq_data; 5519 5520 periph_qual = SID_QUAL(inq_buf); 5521 5522 switch(periph_qual) { 5523 case SID_QUAL_LU_CONNECTED: 5524 { 5525 u_int8_t alen; 5526 5527 /* 5528 * We conservatively request only 5529 * SHORT_INQUIRY_LEN bytes of inquiry 5530 * information during our first try 5531 * at sending an INQUIRY. If the device 5532 * has more information to give, 5533 * perform a second request specifying 5534 * the amount of information the device 5535 * is willing to give. 5536 */ 5537 alen = inq_buf->additional_length; 5538 if (softc->action == PROBE_INQUIRY 5539 && alen > (SHORT_INQUIRY_LENGTH - 5)) { 5540 softc->action = PROBE_FULL_INQUIRY; 5541 xpt_release_ccb(done_ccb); 5542 xpt_schedule(periph, priority); 5543 return; 5544 } 5545 5546 xpt_find_quirk(path->device); 5547 5548 if ((inq_buf->flags & SID_CmdQue) != 0) 5549 softc->action = PROBE_MODE_SENSE; 5550 else 5551 softc->action = PROBE_SERIAL_NUM; 5552 5553 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 5554 5555 xpt_release_ccb(done_ccb); 5556 xpt_schedule(periph, priority); 5557 return; 5558 } 5559 default: 5560 break; 5561 } 5562 } else if (cam_periph_error(done_ccb, 0, 5563 done_ccb->ccb_h.target_lun > 0 5564 ? SF_RETRY_UA|SF_QUIET_IR 5565 : SF_RETRY_UA, 5566 &softc->saved_ccb) == ERESTART) { 5567 return; 5568 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 5569 /* Don't wedge the queue */ 5570 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 5571 /*run_queue*/TRUE); 5572 } 5573 /* 5574 * If we get to this point, we got an error status back 5575 * from the inquiry and the error status doesn't require 5576 * automatically retrying the command. Therefore, the 5577 * inquiry failed. If we had inquiry information before 5578 * for this device, but this latest inquiry command failed, 5579 * the device has probably gone away. If this device isn't 5580 * already marked unconfigured, notify the peripheral 5581 * drivers that this device is no more. 5582 */ 5583 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 5584 /* Send the async notification. */ 5585 xpt_async(AC_LOST_DEVICE, path, NULL); 5586 5587 xpt_release_ccb(done_ccb); 5588 break; 5589 } 5590 case PROBE_MODE_SENSE: 5591 { 5592 struct ccb_scsiio *csio; 5593 struct scsi_mode_header_6 *mode_hdr; 5594 5595 csio = &done_ccb->csio; 5596 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr; 5597 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 5598 struct scsi_control_page *page; 5599 u_int8_t *offset; 5600 5601 offset = ((u_int8_t *)&mode_hdr[1]) 5602 + mode_hdr->blk_desc_len; 5603 page = (struct scsi_control_page *)offset; 5604 path->device->queue_flags = page->queue_flags; 5605 } else if (cam_periph_error(done_ccb, 0, 5606 SF_RETRY_UA|SF_NO_PRINT, 5607 &softc->saved_ccb) == ERESTART) { 5608 return; 5609 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 5610 /* Don't wedge the queue */ 5611 xpt_release_devq(done_ccb->ccb_h.path, 5612 /*count*/1, /*run_queue*/TRUE); 5613 } 5614 xpt_release_ccb(done_ccb); 5615 free(mode_hdr, M_TEMP); 5616 softc->action = PROBE_SERIAL_NUM; 5617 xpt_schedule(periph, priority); 5618 return; 5619 } 5620 case PROBE_SERIAL_NUM: 5621 { 5622 struct ccb_scsiio *csio; 5623 struct scsi_vpd_unit_serial_number *serial_buf; 5624 u_int32_t priority; 5625 int changed; 5626 int have_serialnum; 5627 5628 changed = 1; 5629 have_serialnum = 0; 5630 csio = &done_ccb->csio; 5631 priority = done_ccb->ccb_h.pinfo.priority; 5632 serial_buf = 5633 (struct scsi_vpd_unit_serial_number *)csio->data_ptr; 5634 5635 /* Clean up from previous instance of this device */ 5636 if (path->device->serial_num != NULL) { 5637 free(path->device->serial_num, M_DEVBUF); 5638 path->device->serial_num = NULL; 5639 path->device->serial_num_len = 0; 5640 } 5641 5642 if (serial_buf == NULL) { 5643 /* 5644 * Don't process the command as it was never sent 5645 */ 5646 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP 5647 && (serial_buf->length > 0)) { 5648 5649 have_serialnum = 1; 5650 path->device->serial_num = 5651 (u_int8_t *)malloc((serial_buf->length + 1), 5652 M_DEVBUF, M_NOWAIT); 5653 if (path->device->serial_num != NULL) { 5654 bcopy(serial_buf->serial_num, 5655 path->device->serial_num, 5656 serial_buf->length); 5657 path->device->serial_num_len = 5658 serial_buf->length; 5659 path->device->serial_num[serial_buf->length] 5660 = '\0'; 5661 } 5662 } else if (cam_periph_error(done_ccb, 0, 5663 SF_RETRY_UA|SF_NO_PRINT, 5664 &softc->saved_ccb) == ERESTART) { 5665 return; 5666 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 5667 /* Don't wedge the queue */ 5668 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 5669 /*run_queue*/TRUE); 5670 } 5671 5672 /* 5673 * Let's see if we have seen this device before. 5674 */ 5675 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) { 5676 MD5_CTX context; 5677 u_int8_t digest[16]; 5678 5679 MD5Init(&context); 5680 5681 MD5Update(&context, 5682 (unsigned char *)&path->device->inq_data, 5683 sizeof(struct scsi_inquiry_data)); 5684 5685 if (have_serialnum) 5686 MD5Update(&context, serial_buf->serial_num, 5687 serial_buf->length); 5688 5689 MD5Final(digest, &context); 5690 if (bcmp(softc->digest, digest, 16) == 0) 5691 changed = 0; 5692 5693 /* 5694 * XXX Do we need to do a TUR in order to ensure 5695 * that the device really hasn't changed??? 5696 */ 5697 if ((changed != 0) 5698 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0)) 5699 xpt_async(AC_LOST_DEVICE, path, NULL); 5700 } 5701 if (serial_buf != NULL) 5702 free(serial_buf, M_TEMP); 5703 5704 if (changed != 0) { 5705 /* 5706 * Now that we have all the necessary 5707 * information to safely perform transfer 5708 * negotiations... Controllers don't perform 5709 * any negotiation or tagged queuing until 5710 * after the first XPT_SET_TRAN_SETTINGS ccb is 5711 * received. So, on a new device, just retreive 5712 * the user settings, and set them as the current 5713 * settings to set the device up. 5714 */ 5715 proberequestdefaultnegotiation(periph); 5716 xpt_release_ccb(done_ccb); 5717 5718 /* 5719 * Perform a TUR to allow the controller to 5720 * perform any necessary transfer negotiation. 5721 */ 5722 softc->action = PROBE_TUR_FOR_NEGOTIATION; 5723 xpt_schedule(periph, priority); 5724 return; 5725 } 5726 xpt_release_ccb(done_ccb); 5727 break; 5728 } 5729 case PROBE_TUR_FOR_NEGOTIATION: 5730 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 5731 /* Don't wedge the queue */ 5732 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 5733 /*run_queue*/TRUE); 5734 } 5735 5736 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 5737 5738 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) { 5739 /* Inform the XPT that a new device has been found */ 5740 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 5741 xpt_action(done_ccb); 5742 5743 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb); 5744 } 5745 xpt_release_ccb(done_ccb); 5746 break; 5747 } 5748 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 5749 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe); 5750 done_ccb->ccb_h.status = CAM_REQ_CMP; 5751 xpt_done(done_ccb); 5752 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) { 5753 cam_periph_invalidate(periph); 5754 cam_periph_release(periph); 5755 } else { 5756 probeschedule(periph); 5757 } 5758 } 5759 5760 static void 5761 probecleanup(struct cam_periph *periph) 5762 { 5763 free(periph->softc, M_TEMP); 5764 } 5765 5766 static void 5767 xpt_find_quirk(struct cam_ed *device) 5768 { 5769 caddr_t match; 5770 5771 match = cam_quirkmatch((caddr_t)&device->inq_data, 5772 (caddr_t)xpt_quirk_table, 5773 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table), 5774 sizeof(*xpt_quirk_table), scsi_inquiry_match); 5775 5776 if (match == NULL) 5777 panic("xpt_find_quirk: device didn't match wildcard entry!!"); 5778 5779 device->quirk = (struct xpt_quirk_entry *)match; 5780 } 5781 5782 static void 5783 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device, 5784 int async_update) 5785 { 5786 struct cam_sim *sim; 5787 int qfrozen; 5788 5789 sim = cts->ccb_h.path->bus->sim; 5790 if (async_update == FALSE) { 5791 struct scsi_inquiry_data *inq_data; 5792 struct ccb_pathinq cpi; 5793 struct ccb_trans_settings cur_cts; 5794 5795 if (device == NULL) { 5796 cts->ccb_h.status = CAM_PATH_INVALID; 5797 xpt_done((union ccb *)cts); 5798 return; 5799 } 5800 5801 /* 5802 * Perform sanity checking against what the 5803 * controller and device can do. 5804 */ 5805 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1); 5806 cpi.ccb_h.func_code = XPT_PATH_INQ; 5807 xpt_action((union ccb *)&cpi); 5808 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1); 5809 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 5810 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS; 5811 xpt_action((union ccb *)&cur_cts); 5812 inq_data = &device->inq_data; 5813 5814 /* Fill in any gaps in what the user gave us */ 5815 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) 5816 cts->sync_period = cur_cts.sync_period; 5817 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) 5818 cts->sync_offset = cur_cts.sync_offset; 5819 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0) 5820 cts->bus_width = cur_cts.bus_width; 5821 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) { 5822 cts->flags &= ~CCB_TRANS_DISC_ENB; 5823 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB; 5824 } 5825 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) { 5826 cts->flags &= ~CCB_TRANS_TAG_ENB; 5827 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB; 5828 } 5829 5830 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0 5831 && (inq_data->flags & SID_Sync) == 0) 5832 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) 5833 || (cts->sync_offset == 0) 5834 || (cts->sync_period == 0)) { 5835 /* Force async */ 5836 cts->sync_period = 0; 5837 cts->sync_offset = 0; 5838 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) { 5839 5840 if ((inq_data->spi3data & SID_SPI_CLOCK_DT) == 0 5841 && cts->sync_period <= 0x9) { 5842 /* 5843 * Don't allow DT transmission rates if the 5844 * device does not support it. 5845 */ 5846 cts->sync_period = 0xa; 5847 } 5848 if ((inq_data->spi3data & SID_SPI_IUS) == 0 5849 && cts->sync_period <= 0x8) { 5850 /* 5851 * Don't allow PACE transmission rates 5852 * if the device does support packetized 5853 * transfers. 5854 */ 5855 cts->sync_period = 0x9; 5856 } 5857 } 5858 5859 switch (cts->bus_width) { 5860 case MSG_EXT_WDTR_BUS_32_BIT: 5861 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0 5862 || (inq_data->flags & SID_WBus32) != 0) 5863 && (cpi.hba_inquiry & PI_WIDE_32) != 0) 5864 break; 5865 /* Fall Through to 16-bit */ 5866 case MSG_EXT_WDTR_BUS_16_BIT: 5867 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0 5868 || (inq_data->flags & SID_WBus16) != 0) 5869 && (cpi.hba_inquiry & PI_WIDE_16) != 0) { 5870 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT; 5871 break; 5872 } 5873 /* Fall Through to 8-bit */ 5874 default: /* New bus width?? */ 5875 case MSG_EXT_WDTR_BUS_8_BIT: 5876 /* All targets can do this */ 5877 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT; 5878 break; 5879 } 5880 5881 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) { 5882 /* 5883 * Can't tag queue without disconnection. 5884 */ 5885 cts->flags &= ~CCB_TRANS_TAG_ENB; 5886 cts->valid |= CCB_TRANS_TQ_VALID; 5887 } 5888 5889 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0 5890 || (inq_data->flags & SID_CmdQue) == 0 5891 || (device->queue_flags & SCP_QUEUE_DQUE) != 0 5892 || (device->quirk->mintags == 0)) { 5893 /* 5894 * Can't tag on hardware that doesn't support, 5895 * doesn't have it enabled, or has broken tag support. 5896 */ 5897 cts->flags &= ~CCB_TRANS_TAG_ENB; 5898 } 5899 } 5900 5901 qfrozen = FALSE; 5902 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) { 5903 int device_tagenb; 5904 5905 /* 5906 * If we are transitioning from tags to no-tags or 5907 * vice-versa, we need to carefully freeze and restart 5908 * the queue so that we don't overlap tagged and non-tagged 5909 * commands. We also temporarily stop tags if there is 5910 * a change in transfer negotiation settings to allow 5911 * "tag-less" negotiation. 5912 */ 5913 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 5914 || (device->inq_flags & SID_CmdQue) != 0) 5915 device_tagenb = TRUE; 5916 else 5917 device_tagenb = FALSE; 5918 5919 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0 5920 && device_tagenb == FALSE) 5921 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0 5922 && device_tagenb == TRUE)) { 5923 5924 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) { 5925 /* 5926 * Delay change to use tags until after a 5927 * few commands have gone to this device so 5928 * the controller has time to perform transfer 5929 * negotiations without tagged messages getting 5930 * in the way. 5931 */ 5932 device->tag_delay_count = CAM_TAG_DELAY_COUNT; 5933 device->flags |= CAM_DEV_TAG_AFTER_COUNT; 5934 } else { 5935 xpt_freeze_devq(cts->ccb_h.path, /*count*/1); 5936 qfrozen = TRUE; 5937 device->inq_flags &= ~SID_CmdQue; 5938 xpt_dev_ccbq_resize(cts->ccb_h.path, 5939 sim->max_dev_openings); 5940 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 5941 device->tag_delay_count = 0; 5942 } 5943 } 5944 } 5945 5946 if (async_update == FALSE) { 5947 /* 5948 * If we are currently performing tagged transactions to 5949 * this device and want to change its negotiation parameters, 5950 * go non-tagged for a bit to give the controller a chance to 5951 * negotiate unhampered by tag messages. 5952 */ 5953 if ((device->inq_flags & SID_CmdQue) != 0 5954 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID| 5955 CCB_TRANS_SYNC_OFFSET_VALID| 5956 CCB_TRANS_BUS_WIDTH_VALID)) != 0) 5957 xpt_toggle_tags(cts->ccb_h.path); 5958 5959 (*(sim->sim_action))(sim, (union ccb *)cts); 5960 } 5961 5962 if (qfrozen) { 5963 struct ccb_relsim crs; 5964 5965 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path, 5966 /*priority*/1); 5967 crs.ccb_h.func_code = XPT_REL_SIMQ; 5968 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 5969 crs.openings 5970 = crs.release_timeout 5971 = crs.qfrozen_cnt 5972 = 0; 5973 xpt_action((union ccb *)&crs); 5974 } 5975 } 5976 5977 static void 5978 xpt_toggle_tags(struct cam_path *path) 5979 { 5980 struct cam_ed *dev; 5981 5982 /* 5983 * Give controllers a chance to renegotiate 5984 * before starting tag operations. We 5985 * "toggle" tagged queuing off then on 5986 * which causes the tag enable command delay 5987 * counter to come into effect. 5988 */ 5989 dev = path->device; 5990 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 5991 || ((dev->inq_flags & SID_CmdQue) != 0 5992 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) { 5993 struct ccb_trans_settings cts; 5994 5995 xpt_setup_ccb(&cts.ccb_h, path, 1); 5996 cts.flags = 0; 5997 cts.valid = CCB_TRANS_TQ_VALID; 5998 xpt_set_transfer_settings(&cts, path->device, 5999 /*async_update*/TRUE); 6000 cts.flags = CCB_TRANS_TAG_ENB; 6001 xpt_set_transfer_settings(&cts, path->device, 6002 /*async_update*/TRUE); 6003 } 6004 } 6005 6006 static void 6007 xpt_start_tags(struct cam_path *path) 6008 { 6009 struct ccb_relsim crs; 6010 struct cam_ed *device; 6011 struct cam_sim *sim; 6012 int newopenings; 6013 6014 device = path->device; 6015 sim = path->bus->sim; 6016 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 6017 xpt_freeze_devq(path, /*count*/1); 6018 device->inq_flags |= SID_CmdQue; 6019 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings); 6020 xpt_dev_ccbq_resize(path, newopenings); 6021 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1); 6022 crs.ccb_h.func_code = XPT_REL_SIMQ; 6023 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 6024 crs.openings 6025 = crs.release_timeout 6026 = crs.qfrozen_cnt 6027 = 0; 6028 xpt_action((union ccb *)&crs); 6029 } 6030 6031 static int busses_to_config; 6032 static int busses_to_reset; 6033 6034 static int 6035 xptconfigbuscountfunc(struct cam_eb *bus, void *arg) 6036 { 6037 if (bus->path_id != CAM_XPT_PATH_ID) { 6038 struct cam_path path; 6039 struct ccb_pathinq cpi; 6040 int can_negotiate; 6041 6042 busses_to_config++; 6043 xpt_compile_path(&path, NULL, bus->path_id, 6044 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 6045 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1); 6046 cpi.ccb_h.func_code = XPT_PATH_INQ; 6047 xpt_action((union ccb *)&cpi); 6048 can_negotiate = cpi.hba_inquiry; 6049 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE); 6050 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0 6051 && can_negotiate) 6052 busses_to_reset++; 6053 xpt_release_path(&path); 6054 } 6055 6056 return(1); 6057 } 6058 6059 static int 6060 xptconfigfunc(struct cam_eb *bus, void *arg) 6061 { 6062 struct cam_path *path; 6063 union ccb *work_ccb; 6064 6065 if (bus->path_id != CAM_XPT_PATH_ID) { 6066 cam_status status; 6067 int can_negotiate; 6068 6069 work_ccb = xpt_alloc_ccb(); 6070 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id, 6071 CAM_TARGET_WILDCARD, 6072 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){ 6073 printf("xptconfigfunc: xpt_create_path failed with " 6074 "status %#x for bus %d\n", status, bus->path_id); 6075 printf("xptconfigfunc: halting bus configuration\n"); 6076 xpt_free_ccb(work_ccb); 6077 busses_to_config--; 6078 xpt_finishconfig(xpt_periph, NULL); 6079 return(0); 6080 } 6081 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1); 6082 work_ccb->ccb_h.func_code = XPT_PATH_INQ; 6083 xpt_action(work_ccb); 6084 if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 6085 printf("xptconfigfunc: CPI failed on bus %d " 6086 "with status %d\n", bus->path_id, 6087 work_ccb->ccb_h.status); 6088 xpt_finishconfig(xpt_periph, work_ccb); 6089 return(1); 6090 } 6091 6092 can_negotiate = work_ccb->cpi.hba_inquiry; 6093 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE); 6094 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0 6095 && (can_negotiate != 0)) { 6096 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1); 6097 work_ccb->ccb_h.func_code = XPT_RESET_BUS; 6098 work_ccb->ccb_h.cbfcnp = NULL; 6099 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE, 6100 ("Resetting Bus\n")); 6101 xpt_action(work_ccb); 6102 xpt_finishconfig(xpt_periph, work_ccb); 6103 } else { 6104 /* Act as though we performed a successful BUS RESET */ 6105 work_ccb->ccb_h.func_code = XPT_RESET_BUS; 6106 xpt_finishconfig(xpt_periph, work_ccb); 6107 } 6108 } 6109 6110 return(1); 6111 } 6112 6113 static void 6114 xpt_config(void *arg) 6115 { 6116 /* Now that interrupts are enabled, go find our devices */ 6117 6118 #ifdef CAMDEBUG 6119 /* Setup debugging flags and path */ 6120 #ifdef CAM_DEBUG_FLAGS 6121 cam_dflags = CAM_DEBUG_FLAGS; 6122 #else /* !CAM_DEBUG_FLAGS */ 6123 cam_dflags = CAM_DEBUG_NONE; 6124 #endif /* CAM_DEBUG_FLAGS */ 6125 #ifdef CAM_DEBUG_BUS 6126 if (cam_dflags != CAM_DEBUG_NONE) { 6127 if (xpt_create_path(&cam_dpath, xpt_periph, 6128 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, 6129 CAM_DEBUG_LUN) != CAM_REQ_CMP) { 6130 printf("xpt_config: xpt_create_path() failed for debug" 6131 " target %d:%d:%d, debugging disabled\n", 6132 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN); 6133 cam_dflags = CAM_DEBUG_NONE; 6134 } 6135 } else 6136 cam_dpath = NULL; 6137 #else /* !CAM_DEBUG_BUS */ 6138 cam_dpath = NULL; 6139 #endif /* CAM_DEBUG_BUS */ 6140 #endif /* CAMDEBUG */ 6141 6142 /* 6143 * Scan all installed busses. 6144 */ 6145 xpt_for_all_busses(xptconfigbuscountfunc, NULL); 6146 6147 if (busses_to_config == 0) { 6148 /* Call manually because we don't have any busses */ 6149 xpt_finishconfig(xpt_periph, NULL); 6150 } else { 6151 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) { 6152 printf("Waiting %d seconds for SCSI " 6153 "devices to settle\n", SCSI_DELAY/1000); 6154 } 6155 xpt_for_all_busses(xptconfigfunc, NULL); 6156 } 6157 } 6158 6159 /* 6160 * If the given device only has one peripheral attached to it, and if that 6161 * peripheral is the passthrough driver, announce it. This insures that the 6162 * user sees some sort of announcement for every peripheral in their system. 6163 */ 6164 static int 6165 xptpassannouncefunc(struct cam_ed *device, void *arg) 6166 { 6167 struct cam_periph *periph; 6168 int i; 6169 6170 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL; 6171 periph = SLIST_NEXT(periph, periph_links), i++); 6172 6173 periph = SLIST_FIRST(&device->periphs); 6174 if ((i == 1) 6175 && (strncmp(periph->periph_name, "pass", 4) == 0)) 6176 xpt_announce_periph(periph, NULL); 6177 6178 return(1); 6179 } 6180 6181 static void 6182 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb) 6183 { 6184 struct periph_driver **p_drv; 6185 int i; 6186 6187 if (done_ccb != NULL) { 6188 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, 6189 ("xpt_finishconfig\n")); 6190 switch(done_ccb->ccb_h.func_code) { 6191 case XPT_RESET_BUS: 6192 if (done_ccb->ccb_h.status == CAM_REQ_CMP) { 6193 done_ccb->ccb_h.func_code = XPT_SCAN_BUS; 6194 done_ccb->ccb_h.cbfcnp = xpt_finishconfig; 6195 xpt_action(done_ccb); 6196 return; 6197 } 6198 /* FALLTHROUGH */ 6199 case XPT_SCAN_BUS: 6200 default: 6201 xpt_free_path(done_ccb->ccb_h.path); 6202 busses_to_config--; 6203 break; 6204 } 6205 } 6206 6207 if (busses_to_config == 0) { 6208 /* Register all the peripheral drivers */ 6209 /* XXX This will have to change when we have loadable modules */ 6210 p_drv = (struct periph_driver **)periphdriver_set.ls_items; 6211 for (i = 0; p_drv[i] != NULL; i++) { 6212 (*p_drv[i]->init)(); 6213 } 6214 6215 /* 6216 * Check for devices with no "standard" peripheral driver 6217 * attached. For any devices like that, announce the 6218 * passthrough driver so the user will see something. 6219 */ 6220 xpt_for_all_devices(xptpassannouncefunc, NULL); 6221 6222 /* Release our hook so that the boot can continue. */ 6223 config_intrhook_disestablish(xpt_config_hook); 6224 free(xpt_config_hook, M_TEMP); 6225 xpt_config_hook = NULL; 6226 } 6227 if (done_ccb != NULL) 6228 xpt_free_ccb(done_ccb); 6229 } 6230 6231 static void 6232 xptaction(struct cam_sim *sim, union ccb *work_ccb) 6233 { 6234 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n")); 6235 6236 switch (work_ccb->ccb_h.func_code) { 6237 /* Common cases first */ 6238 case XPT_PATH_INQ: /* Path routing inquiry */ 6239 { 6240 struct ccb_pathinq *cpi; 6241 6242 cpi = &work_ccb->cpi; 6243 cpi->version_num = 1; /* XXX??? */ 6244 cpi->hba_inquiry = 0; 6245 cpi->target_sprt = 0; 6246 cpi->hba_misc = 0; 6247 cpi->hba_eng_cnt = 0; 6248 cpi->max_target = 0; 6249 cpi->max_lun = 0; 6250 cpi->initiator_id = 0; 6251 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 6252 strncpy(cpi->hba_vid, "", HBA_IDLEN); 6253 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 6254 cpi->unit_number = sim->unit_number; 6255 cpi->bus_id = sim->bus_id; 6256 cpi->base_transfer_speed = 0; 6257 cpi->ccb_h.status = CAM_REQ_CMP; 6258 xpt_done(work_ccb); 6259 break; 6260 } 6261 default: 6262 work_ccb->ccb_h.status = CAM_REQ_INVALID; 6263 xpt_done(work_ccb); 6264 break; 6265 } 6266 } 6267 6268 /* 6269 * The xpt as a "controller" has no interrupt sources, so polling 6270 * is a no-op. 6271 */ 6272 static void 6273 xptpoll(struct cam_sim *sim) 6274 { 6275 } 6276 6277 /* 6278 * Should only be called by the machine interrupt dispatch routines, 6279 * so put these prototypes here instead of in the header. 6280 */ 6281 6282 static void 6283 swi_camnet(void *arg) 6284 { 6285 camisr(&cam_netq); 6286 } 6287 6288 static void 6289 swi_cambio(void *arg) 6290 { 6291 camisr(&cam_bioq); 6292 } 6293 6294 static void 6295 camisr(cam_isrq_t *queue) 6296 { 6297 int s; 6298 struct ccb_hdr *ccb_h; 6299 6300 s = splcam(); 6301 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) { 6302 int runq; 6303 6304 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe); 6305 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX; 6306 splx(s); 6307 6308 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE, 6309 ("camisr\n")); 6310 6311 runq = FALSE; 6312 6313 if (ccb_h->flags & CAM_HIGH_POWER) { 6314 struct highpowerlist *hphead; 6315 struct cam_ed *device; 6316 union ccb *send_ccb; 6317 6318 hphead = &highpowerq; 6319 6320 send_ccb = (union ccb *)STAILQ_FIRST(hphead); 6321 6322 /* 6323 * Increment the count since this command is done. 6324 */ 6325 num_highpower++; 6326 6327 /* 6328 * Any high powered commands queued up? 6329 */ 6330 if (send_ccb != NULL) { 6331 device = send_ccb->ccb_h.path->device; 6332 6333 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe); 6334 6335 xpt_release_devq(send_ccb->ccb_h.path, 6336 /*count*/1, /*runqueue*/TRUE); 6337 } 6338 } 6339 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) { 6340 struct cam_ed *dev; 6341 6342 dev = ccb_h->path->device; 6343 6344 s = splcam(); 6345 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h); 6346 6347 ccb_h->path->bus->sim->devq->send_active--; 6348 ccb_h->path->bus->sim->devq->send_openings++; 6349 splx(s); 6350 6351 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0 6352 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0 6353 && (dev->ccbq.dev_active == 0))) { 6354 6355 xpt_release_devq(ccb_h->path, /*count*/1, 6356 /*run_queue*/TRUE); 6357 } 6358 6359 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 6360 && (--dev->tag_delay_count == 0)) 6361 xpt_start_tags(ccb_h->path); 6362 6363 if ((dev->ccbq.queue.entries > 0) 6364 && (dev->qfrozen_cnt == 0) 6365 && (device_is_send_queued(dev) == 0)) { 6366 runq = xpt_schedule_dev_sendq(ccb_h->path->bus, 6367 dev); 6368 } 6369 } 6370 6371 if (ccb_h->status & CAM_RELEASE_SIMQ) { 6372 xpt_release_simq(ccb_h->path->bus->sim, 6373 /*run_queue*/TRUE); 6374 ccb_h->status &= ~CAM_RELEASE_SIMQ; 6375 runq = FALSE; 6376 } 6377 6378 if ((ccb_h->flags & CAM_DEV_QFRZDIS) 6379 && (ccb_h->status & CAM_DEV_QFRZN)) { 6380 xpt_release_devq(ccb_h->path, /*count*/1, 6381 /*run_queue*/TRUE); 6382 ccb_h->status &= ~CAM_DEV_QFRZN; 6383 } else if (runq) { 6384 xpt_run_dev_sendq(ccb_h->path->bus); 6385 } 6386 6387 /* Call the peripheral driver's callback */ 6388 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h); 6389 6390 /* Raise IPL for while test */ 6391 s = splcam(); 6392 } 6393 splx(s); 6394 } 6395