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