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