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