2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
41 #include <sys/devicestat.h>
42 #include <sys/interrupt.h>
46 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_periph.h>
52 #include <cam/cam_sim.h>
53 #include <cam/cam_xpt.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_debug.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 #include <cam/scsi/scsi_pass.h>
63 /* Datastructures internal to the xpt layer */
66 * Definition of an async handler callback block. These are used to add
67 * SIMs and peripherals to the async callback lists.
70 SLIST_ENTRY(async_node) links;
71 u_int32_t event_enable; /* Async Event enables */
72 void (*callback)(void *arg, u_int32_t code,
73 struct cam_path *path, void *args);
77 SLIST_HEAD(async_list, async_node);
78 SLIST_HEAD(periph_list, cam_periph);
79 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
82 * This is the maximum number of high powered commands (e.g. start unit)
83 * that can be outstanding at a particular time.
85 #ifndef CAM_MAX_HIGHPOWER
86 #define CAM_MAX_HIGHPOWER 4
89 /* number of high powered commands that can go through right now */
90 static int num_highpower = CAM_MAX_HIGHPOWER;
93 * Structure for queueing a device in a run queue.
94 * There is one run queue for allocating new ccbs,
95 * and another for sending ccbs to the controller.
99 struct cam_ed *device;
103 * The CAM EDT (Existing Device Table) contains the device information for
104 * all devices for all busses in the system. The table contains a
105 * cam_ed structure for each device on the bus.
108 TAILQ_ENTRY(cam_ed) links;
109 struct cam_ed_qinfo alloc_ccb_entry;
110 struct cam_ed_qinfo send_ccb_entry;
111 struct cam_et *target;
114 * Queue of type drivers wanting to do
115 * work on this device.
117 struct cam_ccbq ccbq; /* Queue of pending ccbs */
118 struct async_list asyncs; /* Async callback info for this B/T/L */
119 struct periph_list periphs; /* All attached devices */
120 u_int generation; /* Generation number */
121 struct cam_periph *owner; /* Peripheral driver's ownership tag */
122 struct xpt_quirk_entry *quirk; /* Oddities about this device */
123 /* Storage for the inquiry data */
124 #ifdef CAM_NEW_TRAN_CODE
126 u_int protocol_version;
128 u_int transport_version;
129 #endif /* CAM_NEW_TRAN_CODE */
130 struct scsi_inquiry_data inq_data;
131 u_int8_t inq_flags; /*
132 * Current settings for inquiry flags.
133 * This allows us to override settings
134 * like disconnection and tagged
135 * queuing for a device.
137 u_int8_t queue_flags; /* Queue flags from the control page */
138 u_int8_t serial_num_len;
139 u_int8_t *serial_num;
140 u_int32_t qfrozen_cnt;
142 #define CAM_DEV_UNCONFIGURED 0x01
143 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
144 #define CAM_DEV_REL_ON_COMPLETE 0x04
145 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
146 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
147 #define CAM_DEV_TAG_AFTER_COUNT 0x20
148 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
149 u_int32_t tag_delay_count;
150 #define CAM_TAG_DELAY_COUNT 5
152 struct callout_handle c_handle;
156 * Each target is represented by an ET (Existing Target). These
157 * entries are created when a target is successfully probed with an
158 * identify, and removed when a device fails to respond after a number
159 * of retries, or a bus rescan finds the device missing.
162 TAILQ_HEAD(, cam_ed) ed_entries;
163 TAILQ_ENTRY(cam_et) links;
165 target_id_t target_id;
168 struct timeval last_reset;
172 * Each bus is represented by an EB (Existing Bus). These entries
173 * are created by calls to xpt_bus_register and deleted by calls to
174 * xpt_bus_deregister.
177 TAILQ_HEAD(, cam_et) et_entries;
178 TAILQ_ENTRY(cam_eb) links;
181 struct timeval last_reset;
183 #define CAM_EB_RUNQ_SCHEDULED 0x01
189 struct cam_periph *periph;
191 struct cam_et *target;
192 struct cam_ed *device;
195 struct xpt_quirk_entry {
196 struct scsi_inquiry_pattern inq_pat;
198 #define CAM_QUIRK_NOLUNS 0x01
199 #define CAM_QUIRK_NOSERIAL 0x02
200 #define CAM_QUIRK_HILUNS 0x04
204 #define CAM_SCSI2_MAXLUN 8
212 u_int32_t generation;
215 static const char quantum[] = "QUANTUM";
216 static const char sony[] = "SONY";
217 static const char west_digital[] = "WDIGTL";
218 static const char samsung[] = "SAMSUNG";
219 static const char seagate[] = "SEAGATE";
220 static const char microp[] = "MICROP";
222 static struct xpt_quirk_entry xpt_quirk_table[] =
225 /* Reports QUEUE FULL for temporary resource shortages */
226 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
227 /*quirks*/0, /*mintags*/24, /*maxtags*/32
230 /* Reports QUEUE FULL for temporary resource shortages */
231 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
232 /*quirks*/0, /*mintags*/24, /*maxtags*/32
235 /* Reports QUEUE FULL for temporary resource shortages */
236 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
237 /*quirks*/0, /*mintags*/24, /*maxtags*/32
240 /* Broken tagged queuing drive */
241 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
242 /*quirks*/0, /*mintags*/0, /*maxtags*/0
245 /* Broken tagged queuing drive */
246 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
247 /*quirks*/0, /*mintags*/0, /*maxtags*/0
250 /* Broken tagged queuing drive */
251 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
252 /*quirks*/0, /*mintags*/0, /*maxtags*/0
256 * Unfortunately, the Quantum Atlas III has the same
257 * problem as the Atlas II drives above.
258 * Reported by: "Johan Granlund" <johan@granlund.nu>
260 * For future reference, the drive with the problem was:
261 * QUANTUM QM39100TD-SW N1B0
263 * It's possible that Quantum will fix the problem in later
264 * firmware revisions. If that happens, the quirk entry
265 * will need to be made specific to the firmware revisions
269 /* Reports QUEUE FULL for temporary resource shortages */
270 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
271 /*quirks*/0, /*mintags*/24, /*maxtags*/32
275 * 18 Gig Atlas III, same problem as the 9G version.
276 * Reported by: Andre Albsmeier
277 * <andre.albsmeier@mchp.siemens.de>
279 * For future reference, the drive with the problem was:
280 * QUANTUM QM318000TD-S N491
282 /* Reports QUEUE FULL for temporary resource shortages */
283 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
284 /*quirks*/0, /*mintags*/24, /*maxtags*/32
288 * Broken tagged queuing drive
289 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
290 * and: Martin Renters <martin@tdc.on.ca>
292 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
293 /*quirks*/0, /*mintags*/0, /*maxtags*/0
296 * The Seagate Medalist Pro drives have very poor write
297 * performance with anything more than 2 tags.
299 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
300 * Drive: <SEAGATE ST36530N 1444>
302 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
303 * Drive: <SEAGATE ST34520W 1281>
305 * No one has actually reported that the 9G version
306 * (ST39140*) of the Medalist Pro has the same problem, but
307 * we're assuming that it does because the 4G and 6.5G
308 * versions of the drive are broken.
311 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
312 /*quirks*/0, /*mintags*/2, /*maxtags*/2
315 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
316 /*quirks*/0, /*mintags*/2, /*maxtags*/2
319 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
320 /*quirks*/0, /*mintags*/2, /*maxtags*/2
324 * Slow when tagged queueing is enabled. Write performance
325 * steadily drops off with more and more concurrent
326 * transactions. Best sequential write performance with
327 * tagged queueing turned off and write caching turned on.
330 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
331 * Drive: DCAS-34330 w/ "S65A" firmware.
333 * The drive with the problem had the "S65A" firmware
334 * revision, and has also been reported (by Stephen J.
335 * Roznowski <sjr@home.net>) for a drive with the "S61A"
338 * Although no one has reported problems with the 2 gig
339 * version of the DCAS drive, the assumption is that it
340 * has the same problems as the 4 gig version. Therefore
341 * this quirk entries disables tagged queueing for all
344 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
345 /*quirks*/0, /*mintags*/0, /*maxtags*/0
348 /* Broken tagged queuing drive */
349 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
350 /*quirks*/0, /*mintags*/0, /*maxtags*/0
353 /* Broken tagged queuing drive */
354 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
355 /*quirks*/0, /*mintags*/0, /*maxtags*/0
359 * Broken tagged queuing drive.
361 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
364 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
365 /*quirks*/0, /*mintags*/0, /*maxtags*/0
369 * Slow when tagged queueing is enabled. (1.5MB/sec versus
371 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
372 * Best performance with these drives is achieved with
373 * tagged queueing turned off, and write caching turned on.
375 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
376 /*quirks*/0, /*mintags*/0, /*maxtags*/0
380 * Slow when tagged queueing is enabled. (1.5MB/sec versus
382 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
383 * Best performance with these drives is achieved with
384 * tagged queueing turned off, and write caching turned on.
386 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
387 /*quirks*/0, /*mintags*/0, /*maxtags*/0
391 * Doesn't handle queue full condition correctly,
392 * so we need to limit maxtags to what the device
393 * can handle instead of determining this automatically.
395 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
396 /*quirks*/0, /*mintags*/2, /*maxtags*/32
399 /* Really only one LUN */
400 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
401 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
404 /* I can't believe we need a quirk for DPT volumes. */
405 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
406 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
407 /*mintags*/0, /*maxtags*/255
411 * Many Sony CDROM drives don't like multi-LUN probing.
413 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
414 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
418 * This drive doesn't like multiple LUN probing.
419 * Submitted by: Parag Patel <parag@cgt.com>
421 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
422 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
425 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
426 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
430 * The 8200 doesn't like multi-lun probing, and probably
431 * don't like serial number requests either.
434 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
437 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
441 * Let's try the same as above, but for a drive that says
442 * it's an IPL-6860 but is actually an EXB 8200.
445 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
448 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
452 * These Hitachi drives don't like multi-lun probing.
453 * The PR submitter has a DK319H, but says that the Linux
454 * kernel has a similar work-around for the DK312 and DK314,
455 * so all DK31* drives are quirked here.
457 * Submitted by: Paul Haddad <paul@pth.com>
459 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
460 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
464 * The Hitachi CJ series with J8A8 firmware apparantly has
465 * problems with tagged commands.
467 * Reported by: amagai@nue.org
469 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
470 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
474 * This old revision of the TDC3600 is also SCSI-1, and
475 * hangs upon serial number probing.
478 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
481 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
485 * Maxtor Personal Storage 3000XT (Firewire)
486 * hangs upon serial number probing.
489 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
492 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
496 * Would repond to all LUNs if asked for.
499 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
502 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
506 * Would repond to all LUNs if asked for.
509 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
512 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
515 /* Submitted by: Matthew Dodd <winter@jurai.net> */
516 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
517 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
520 /* Submitted by: Matthew Dodd <winter@jurai.net> */
521 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
522 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
525 /* TeraSolutions special settings for TRC-22 RAID */
526 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
527 /*quirks*/0, /*mintags*/55, /*maxtags*/255
531 * Would respond to all LUNs. Device type and removable
532 * flag are jumper-selectable.
534 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
537 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
540 /* Default tagged queuing parameters for all devices */
542 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
543 /*vendor*/"*", /*product*/"*", /*revision*/"*"
545 /*quirks*/0, /*mintags*/2, /*maxtags*/255
549 static const int xpt_quirk_table_size =
550 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
554 DM_RET_FLAG_MASK = 0x0f,
557 DM_RET_DESCEND = 0x20,
559 DM_RET_ACTION_MASK = 0xf0
567 } xpt_traverse_depth;
569 struct xpt_traverse_config {
570 xpt_traverse_depth depth;
575 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
576 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
577 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
578 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
579 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
581 /* Transport layer configuration information */
582 static struct xpt_softc xsoftc;
584 /* Queues for our software interrupt handler */
585 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
586 static cam_isrq_t cam_bioq;
587 static cam_isrq_t cam_netq;
589 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
590 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
591 static u_int xpt_max_ccbs; /*
592 * Maximum size of ccb pool. Modified as
593 * devices are added/removed or have their
594 * opening counts changed.
596 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
598 struct cam_periph *xpt_periph;
600 static periph_init_t xpt_periph_init;
602 static periph_init_t probe_periph_init;
604 static struct periph_driver xpt_driver =
606 xpt_periph_init, "xpt",
607 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
610 static struct periph_driver probe_driver =
612 probe_periph_init, "probe",
613 TAILQ_HEAD_INITIALIZER(probe_driver.units)
616 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
617 PERIPHDRIVER_DECLARE(probe, probe_driver);
619 #define XPT_CDEV_MAJOR 104
621 static d_open_t xptopen;
622 static d_close_t xptclose;
623 static d_ioctl_t xptioctl;
625 static struct cdevsw xpt_cdevsw = {
627 /* close */ xptclose,
630 /* ioctl */ xptioctl,
633 /* strategy */ nostrategy,
635 /* maj */ XPT_CDEV_MAJOR,
641 static struct intr_config_hook *xpt_config_hook;
643 /* Registered busses */
644 static TAILQ_HEAD(,cam_eb) xpt_busses;
645 static u_int bus_generation;
647 /* Storage for debugging datastructures */
649 struct cam_path *cam_dpath;
650 u_int32_t cam_dflags;
651 u_int32_t cam_debug_delay;
654 /* Pointers to software interrupt handlers */
655 static void *camnet_ih;
656 static void *cambio_ih;
658 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
659 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
663 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
664 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
665 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
667 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
668 || defined(CAM_DEBUG_LUN)
670 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
671 || !defined(CAM_DEBUG_LUN)
672 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
674 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
675 #else /* !CAMDEBUG */
676 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
677 #endif /* CAMDEBUG */
678 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
680 /* Our boot-time initialization hook */
681 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
683 static moduledata_t cam_moduledata = {
685 cam_module_event_handler,
689 static void xpt_init(void *);
691 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
692 MODULE_VERSION(cam, 1);
695 static cam_status xpt_compile_path(struct cam_path *new_path,
696 struct cam_periph *perph,
698 target_id_t target_id,
701 static void xpt_release_path(struct cam_path *path);
703 static void xpt_async_bcast(struct async_list *async_head,
704 u_int32_t async_code,
705 struct cam_path *path,
707 static void xpt_dev_async(u_int32_t async_code,
709 struct cam_et *target,
710 struct cam_ed *device,
712 static path_id_t xptnextfreepathid(void);
713 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
714 static union ccb *xpt_get_ccb(struct cam_ed *device);
715 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
716 u_int32_t new_priority);
717 static void xpt_run_dev_allocq(struct cam_eb *bus);
718 static void xpt_run_dev_sendq(struct cam_eb *bus);
719 static timeout_t xpt_release_devq_timeout;
720 static timeout_t xpt_release_simq_timeout;
721 static void xpt_release_bus(struct cam_eb *bus);
722 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
724 static struct cam_et*
725 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
726 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
727 static struct cam_ed*
728 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
730 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
731 struct cam_ed *device);
732 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
733 static struct cam_eb*
734 xpt_find_bus(path_id_t path_id);
735 static struct cam_et*
736 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
737 static struct cam_ed*
738 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
739 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
740 static void xpt_scan_lun(struct cam_periph *periph,
741 struct cam_path *path, cam_flags flags,
743 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
744 static xpt_busfunc_t xptconfigbuscountfunc;
745 static xpt_busfunc_t xptconfigfunc;
746 static void xpt_config(void *arg);
747 static xpt_devicefunc_t xptpassannouncefunc;
748 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
749 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
750 static void xptpoll(struct cam_sim *sim);
751 static void camisr(void *);
753 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
754 static void xptasync(struct cam_periph *periph,
755 u_int32_t code, cam_path *path);
757 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
758 u_int num_patterns, struct cam_eb *bus);
759 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
761 struct cam_ed *device);
762 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
764 struct cam_periph *periph);
765 static xpt_busfunc_t xptedtbusfunc;
766 static xpt_targetfunc_t xptedttargetfunc;
767 static xpt_devicefunc_t xptedtdevicefunc;
768 static xpt_periphfunc_t xptedtperiphfunc;
769 static xpt_pdrvfunc_t xptplistpdrvfunc;
770 static xpt_periphfunc_t xptplistperiphfunc;
771 static int xptedtmatch(struct ccb_dev_match *cdm);
772 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
773 static int xptbustraverse(struct cam_eb *start_bus,
774 xpt_busfunc_t *tr_func, void *arg);
775 static int xpttargettraverse(struct cam_eb *bus,
776 struct cam_et *start_target,
777 xpt_targetfunc_t *tr_func, void *arg);
778 static int xptdevicetraverse(struct cam_et *target,
779 struct cam_ed *start_device,
780 xpt_devicefunc_t *tr_func, void *arg);
781 static int xptperiphtraverse(struct cam_ed *device,
782 struct cam_periph *start_periph,
783 xpt_periphfunc_t *tr_func, void *arg);
784 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
785 xpt_pdrvfunc_t *tr_func, void *arg);
786 static int xptpdperiphtraverse(struct periph_driver **pdrv,
787 struct cam_periph *start_periph,
788 xpt_periphfunc_t *tr_func,
790 static xpt_busfunc_t xptdefbusfunc;
791 static xpt_targetfunc_t xptdeftargetfunc;
792 static xpt_devicefunc_t xptdefdevicefunc;
793 static xpt_periphfunc_t xptdefperiphfunc;
794 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
796 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
799 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
802 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
805 static xpt_devicefunc_t xptsetasyncfunc;
806 static xpt_busfunc_t xptsetasyncbusfunc;
807 static cam_status xptregister(struct cam_periph *periph,
809 static cam_status proberegister(struct cam_periph *periph,
811 static void probeschedule(struct cam_periph *probe_periph);
812 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
813 static void proberequestdefaultnegotiation(struct cam_periph *periph);
814 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
815 static void probecleanup(struct cam_periph *periph);
816 static void xpt_find_quirk(struct cam_ed *device);
817 #ifdef CAM_NEW_TRAN_CODE
818 static void xpt_devise_transport(struct cam_path *path);
819 #endif /* CAM_NEW_TRAN_CODE */
820 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
821 struct cam_ed *device,
823 static void xpt_toggle_tags(struct cam_path *path);
824 static void xpt_start_tags(struct cam_path *path);
825 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
827 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
829 static __inline int periph_is_queued(struct cam_periph *periph);
830 static __inline int device_is_alloc_queued(struct cam_ed *device);
831 static __inline int device_is_send_queued(struct cam_ed *device);
832 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
835 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
839 if (dev->ccbq.devq_openings > 0) {
840 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
841 cam_ccbq_resize(&dev->ccbq,
842 dev->ccbq.dev_openings
843 + dev->ccbq.dev_active);
844 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
847 * The priority of a device waiting for CCB resources
848 * is that of the the highest priority peripheral driver
851 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
852 &dev->alloc_ccb_entry.pinfo,
853 CAMQ_GET_HEAD(&dev->drvq)->priority);
862 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
866 if (dev->ccbq.dev_openings > 0) {
868 * The priority of a device waiting for controller
869 * resources is that of the the highest priority CCB
873 xpt_schedule_dev(&bus->sim->devq->send_queue,
874 &dev->send_ccb_entry.pinfo,
875 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
883 periph_is_queued(struct cam_periph *periph)
885 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
889 device_is_alloc_queued(struct cam_ed *device)
891 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
895 device_is_send_queued(struct cam_ed *device)
897 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
901 dev_allocq_is_runnable(struct cam_devq *devq)
905 * Have space to do more work.
906 * Allowed to do work.
908 return ((devq->alloc_queue.qfrozen_cnt == 0)
909 && (devq->alloc_queue.entries > 0)
910 && (devq->alloc_openings > 0));
916 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
926 xptdone(struct cam_periph *periph, union ccb *done_ccb)
928 /* Caller will release the CCB */
929 wakeup(&done_ccb->ccb_h.cbfcnp);
933 xptopen(dev_t dev, int flags, int fmt, struct thread *td)
937 unit = minor(dev) & 0xff;
940 * Only allow read-write access.
942 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
946 * We don't allow nonblocking access.
948 if ((flags & O_NONBLOCK) != 0) {
949 printf("xpt%d: can't do nonblocking accesss\n", unit);
954 * We only have one transport layer right now. If someone accesses
955 * us via something other than minor number 1, point out their
959 printf("xptopen: got invalid xpt unit %d\n", unit);
963 /* Mark ourselves open */
964 xsoftc.flags |= XPT_FLAG_OPEN;
970 xptclose(dev_t dev, int flag, int fmt, struct thread *td)
974 unit = minor(dev) & 0xff;
977 * We only have one transport layer right now. If someone accesses
978 * us via something other than minor number 1, point out their
982 printf("xptclose: got invalid xpt unit %d\n", unit);
986 /* Mark ourselves closed */
987 xsoftc.flags &= ~XPT_FLAG_OPEN;
993 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
998 unit = minor(dev) & 0xff;
1001 * We only have one transport layer right now. If someone accesses
1002 * us via something other than minor number 1, point out their
1006 printf("xptioctl: got invalid xpt unit %d\n", unit);
1012 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1013 * to accept CCB types that don't quite make sense to send through a
1014 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1017 case CAMIOCOMMAND: {
1021 inccb = (union ccb *)addr;
1023 switch(inccb->ccb_h.func_code) {
1026 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1027 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1036 ccb = xpt_alloc_ccb();
1039 * Create a path using the bus, target, and lun the
1042 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1043 inccb->ccb_h.path_id,
1044 inccb->ccb_h.target_id,
1045 inccb->ccb_h.target_lun) !=
1051 /* Ensure all of our fields are correct */
1052 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1053 inccb->ccb_h.pinfo.priority);
1054 xpt_merge_ccb(ccb, inccb);
1055 ccb->ccb_h.cbfcnp = xptdone;
1056 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1057 bcopy(ccb, inccb, sizeof(union ccb));
1058 xpt_free_path(ccb->ccb_h.path);
1066 * This is an immediate CCB, so it's okay to
1067 * allocate it on the stack.
1071 * Create a path using the bus, target, and lun the
1074 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1075 inccb->ccb_h.path_id,
1076 inccb->ccb_h.target_id,
1077 inccb->ccb_h.target_lun) !=
1082 /* Ensure all of our fields are correct */
1083 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1084 inccb->ccb_h.pinfo.priority);
1085 xpt_merge_ccb(&ccb, inccb);
1086 ccb.ccb_h.cbfcnp = xptdone;
1088 bcopy(&ccb, inccb, sizeof(union ccb));
1089 xpt_free_path(ccb.ccb_h.path);
1093 case XPT_DEV_MATCH: {
1094 struct cam_periph_map_info mapinfo;
1095 struct cam_path *old_path;
1098 * We can't deal with physical addresses for this
1099 * type of transaction.
1101 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1107 * Save this in case the caller had it set to
1108 * something in particular.
1110 old_path = inccb->ccb_h.path;
1113 * We really don't need a path for the matching
1114 * code. The path is needed because of the
1115 * debugging statements in xpt_action(). They
1116 * assume that the CCB has a valid path.
1118 inccb->ccb_h.path = xpt_periph->path;
1120 bzero(&mapinfo, sizeof(mapinfo));
1123 * Map the pattern and match buffers into kernel
1124 * virtual address space.
1126 error = cam_periph_mapmem(inccb, &mapinfo);
1129 inccb->ccb_h.path = old_path;
1134 * This is an immediate CCB, we can send it on directly.
1139 * Map the buffers back into user space.
1141 cam_periph_unmapmem(inccb, &mapinfo);
1143 inccb->ccb_h.path = old_path;
1155 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1156 * with the periphal driver name and unit name filled in. The other
1157 * fields don't really matter as input. The passthrough driver name
1158 * ("pass"), and unit number are passed back in the ccb. The current
1159 * device generation number, and the index into the device peripheral
1160 * driver list, and the status are also passed back. Note that
1161 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1162 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1163 * (or rather should be) impossible for the device peripheral driver
1164 * list to change since we look at the whole thing in one pass, and
1165 * we do it with splcam protection.
1168 case CAMGETPASSTHRU: {
1170 struct cam_periph *periph;
1171 struct periph_driver **p_drv;
1174 u_int cur_generation;
1175 int base_periph_found;
1179 ccb = (union ccb *)addr;
1180 unit = ccb->cgdl.unit_number;
1181 name = ccb->cgdl.periph_name;
1183 * Every 100 devices, we want to drop our spl protection to
1184 * give the software interrupt handler a chance to run.
1185 * Most systems won't run into this check, but this should
1186 * avoid starvation in the software interrupt handler in
1191 ccb = (union ccb *)addr;
1193 base_periph_found = 0;
1196 * Sanity check -- make sure we don't get a null peripheral
1199 if (*ccb->cgdl.periph_name == '\0') {
1204 /* Keep the list from changing while we traverse it */
1207 cur_generation = xsoftc.generation;
1209 /* first find our driver in the list of drivers */
1210 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1211 if (strcmp((*p_drv)->driver_name, name) == 0)
1214 if (*p_drv == NULL) {
1216 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1217 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1218 *ccb->cgdl.periph_name = '\0';
1219 ccb->cgdl.unit_number = 0;
1225 * Run through every peripheral instance of this driver
1226 * and check to see whether it matches the unit passed
1227 * in by the user. If it does, get out of the loops and
1228 * find the passthrough driver associated with that
1229 * peripheral driver.
1231 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1232 periph = TAILQ_NEXT(periph, unit_links)) {
1234 if (periph->unit_number == unit) {
1236 } else if (--splbreaknum == 0) {
1240 if (cur_generation != xsoftc.generation)
1245 * If we found the peripheral driver that the user passed
1246 * in, go through all of the peripheral drivers for that
1247 * particular device and look for a passthrough driver.
1249 if (periph != NULL) {
1250 struct cam_ed *device;
1253 base_periph_found = 1;
1254 device = periph->path->device;
1255 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1257 periph = SLIST_NEXT(periph, periph_links), i++) {
1259 * Check to see whether we have a
1260 * passthrough device or not.
1262 if (strcmp(periph->periph_name, "pass") == 0) {
1264 * Fill in the getdevlist fields.
1266 strcpy(ccb->cgdl.periph_name,
1267 periph->periph_name);
1268 ccb->cgdl.unit_number =
1269 periph->unit_number;
1270 if (SLIST_NEXT(periph, periph_links))
1272 CAM_GDEVLIST_MORE_DEVS;
1275 CAM_GDEVLIST_LAST_DEVICE;
1276 ccb->cgdl.generation =
1278 ccb->cgdl.index = i;
1280 * Fill in some CCB header fields
1281 * that the user may want.
1283 ccb->ccb_h.path_id =
1284 periph->path->bus->path_id;
1285 ccb->ccb_h.target_id =
1286 periph->path->target->target_id;
1287 ccb->ccb_h.target_lun =
1288 periph->path->device->lun_id;
1289 ccb->ccb_h.status = CAM_REQ_CMP;
1296 * If the periph is null here, one of two things has
1297 * happened. The first possibility is that we couldn't
1298 * find the unit number of the particular peripheral driver
1299 * that the user is asking about. e.g. the user asks for
1300 * the passthrough driver for "da11". We find the list of
1301 * "da" peripherals all right, but there is no unit 11.
1302 * The other possibility is that we went through the list
1303 * of peripheral drivers attached to the device structure,
1304 * but didn't find one with the name "pass". Either way,
1305 * we return ENOENT, since we couldn't find something.
1307 if (periph == NULL) {
1308 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1309 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1310 *ccb->cgdl.periph_name = '\0';
1311 ccb->cgdl.unit_number = 0;
1314 * It is unfortunate that this is even necessary,
1315 * but there are many, many clueless users out there.
1316 * If this is true, the user is looking for the
1317 * passthrough driver, but doesn't have one in his
1320 if (base_periph_found == 1) {
1321 printf("xptioctl: pass driver is not in the "
1323 printf("xptioctl: put \"device pass0\" in "
1324 "your kernel config file\n");
1339 cam_module_event_handler(module_t mod, int what, void *arg)
1341 if (what == MOD_LOAD) {
1343 } else if (what == MOD_UNLOAD) {
1350 /* Functions accessed by the peripheral drivers */
1355 struct cam_sim *xpt_sim;
1356 struct cam_path *path;
1357 struct cam_devq *devq;
1360 TAILQ_INIT(&xpt_busses);
1361 TAILQ_INIT(&cam_bioq);
1362 TAILQ_INIT(&cam_netq);
1363 SLIST_INIT(&ccb_freeq);
1364 STAILQ_INIT(&highpowerq);
1367 * The xpt layer is, itself, the equivelent of a SIM.
1368 * Allow 16 ccbs in the ccb pool for it. This should
1369 * give decent parallelism when we probe busses and
1370 * perform other XPT functions.
1372 devq = cam_simq_alloc(16);
1373 xpt_sim = cam_sim_alloc(xptaction,
1378 /*max_dev_transactions*/0,
1379 /*max_tagged_dev_transactions*/0,
1383 xpt_bus_register(xpt_sim, /*bus #*/0);
1386 * Looking at the XPT from the SIM layer, the XPT is
1387 * the equivelent of a peripheral driver. Allocate
1388 * a peripheral driver entry for us.
1390 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1391 CAM_TARGET_WILDCARD,
1392 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1393 printf("xpt_init: xpt_create_path failed with status %#x,"
1394 " failing attach\n", status);
1398 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1399 path, NULL, 0, NULL);
1400 xpt_free_path(path);
1402 xpt_sim->softc = xpt_periph;
1405 * Register a callback for when interrupts are enabled.
1408 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1409 M_TEMP, M_NOWAIT | M_ZERO);
1410 if (xpt_config_hook == NULL) {
1411 printf("xpt_init: Cannot malloc config hook "
1412 "- failing attach\n");
1416 xpt_config_hook->ich_func = xpt_config;
1417 if (config_intrhook_establish(xpt_config_hook) != 0) {
1418 free (xpt_config_hook, M_TEMP);
1419 printf("xpt_init: config_intrhook_establish failed "
1420 "- failing attach\n");
1423 /* Install our software interrupt handlers */
1424 swi_add(NULL, "camnet", camisr, &cam_netq, SWI_CAMNET, 0, &camnet_ih);
1425 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1429 xptregister(struct cam_periph *periph, void *arg)
1431 if (periph == NULL) {
1432 printf("xptregister: periph was NULL!!\n");
1433 return(CAM_REQ_CMP_ERR);
1436 periph->softc = NULL;
1438 xpt_periph = periph;
1440 return(CAM_REQ_CMP);
1444 xpt_add_periph(struct cam_periph *periph)
1446 struct cam_ed *device;
1448 struct periph_list *periph_head;
1450 device = periph->path->device;
1452 periph_head = &device->periphs;
1454 status = CAM_REQ_CMP;
1456 if (device != NULL) {
1460 * Make room for this peripheral
1461 * so it will fit in the queue
1462 * when it's scheduled to run
1465 status = camq_resize(&device->drvq,
1466 device->drvq.array_size + 1);
1468 device->generation++;
1470 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1475 xsoftc.generation++;
1481 xpt_remove_periph(struct cam_periph *periph)
1483 struct cam_ed *device;
1485 device = periph->path->device;
1487 if (device != NULL) {
1489 struct periph_list *periph_head;
1491 periph_head = &device->periphs;
1493 /* Release the slot for this peripheral */
1495 camq_resize(&device->drvq, device->drvq.array_size - 1);
1497 device->generation++;
1499 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1504 xsoftc.generation++;
1508 #ifdef CAM_NEW_TRAN_CODE
1511 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1513 struct ccb_pathinq cpi;
1514 struct ccb_trans_settings cts;
1515 struct cam_path *path;
1521 path = periph->path;
1523 * To ensure that this is printed in one piece,
1524 * mask out CAM interrupts.
1527 printf("%s%d at %s%d bus %d target %d lun %d\n",
1528 periph->periph_name, periph->unit_number,
1529 path->bus->sim->sim_name,
1530 path->bus->sim->unit_number,
1531 path->bus->sim->bus_id,
1532 path->target->target_id,
1533 path->device->lun_id);
1534 printf("%s%d: ", periph->periph_name, periph->unit_number);
1535 scsi_print_inquiry(&path->device->inq_data);
1536 if (bootverbose && path->device->serial_num_len > 0) {
1537 /* Don't wrap the screen - print only the first 60 chars */
1538 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1539 periph->unit_number, path->device->serial_num);
1541 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1542 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1543 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1544 xpt_action((union ccb*)&cts);
1546 /* Ask the SIM for its base transfer speed */
1547 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1548 cpi.ccb_h.func_code = XPT_PATH_INQ;
1549 xpt_action((union ccb *)&cpi);
1551 speed = cpi.base_transfer_speed;
1553 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1554 struct ccb_trans_settings_spi *spi;
1556 spi = &cts.xport_specific.spi;
1557 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1558 && spi->sync_offset != 0) {
1559 freq = scsi_calc_syncsrate(spi->sync_period);
1563 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1564 speed *= (0x01 << spi->bus_width);
1567 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1568 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1569 if (fc->valid & CTS_FC_VALID_SPEED) {
1570 speed = fc->bitrate;
1576 printf("%s%d: %d.%03dMB/s transfers",
1577 periph->periph_name, periph->unit_number,
1580 printf("%s%d: %dKB/s transfers", periph->periph_name,
1581 periph->unit_number, speed);
1582 /* Report additional information about SPI connections */
1583 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1584 struct ccb_trans_settings_spi *spi;
1586 spi = &cts.xport_specific.spi;
1588 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1590 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1594 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1595 && spi->bus_width > 0) {
1601 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1602 } else if (freq != 0) {
1606 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1607 struct ccb_trans_settings_fc *fc;
1609 fc = &cts.xport_specific.fc;
1610 if (fc->valid & CTS_FC_VALID_WWNN)
1611 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1612 if (fc->valid & CTS_FC_VALID_WWPN)
1613 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1614 if (fc->valid & CTS_FC_VALID_PORT)
1615 printf(" PortID 0x%x", fc->port);
1618 if (path->device->inq_flags & SID_CmdQue
1619 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1620 printf("\n%s%d: Tagged Queueing Enabled",
1621 periph->periph_name, periph->unit_number);
1626 * We only want to print the caller's announce string if they've
1629 if (announce_string != NULL)
1630 printf("%s%d: %s\n", periph->periph_name,
1631 periph->unit_number, announce_string);
1634 #else /* CAM_NEW_TRAN_CODE */
1636 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1640 struct cam_path *path;
1641 struct ccb_trans_settings cts;
1643 path = periph->path;
1645 * To ensure that this is printed in one piece,
1646 * mask out CAM interrupts.
1649 printf("%s%d at %s%d bus %d target %d lun %d\n",
1650 periph->periph_name, periph->unit_number,
1651 path->bus->sim->sim_name,
1652 path->bus->sim->unit_number,
1653 path->bus->sim->bus_id,
1654 path->target->target_id,
1655 path->device->lun_id);
1656 printf("%s%d: ", periph->periph_name, periph->unit_number);
1657 scsi_print_inquiry(&path->device->inq_data);
1659 && (path->device->serial_num_len > 0)) {
1660 /* Don't wrap the screen - print only the first 60 chars */
1661 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1662 periph->unit_number, path->device->serial_num);
1664 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1665 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1666 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1667 xpt_action((union ccb*)&cts);
1668 if (cts.ccb_h.status == CAM_REQ_CMP) {
1672 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1673 && cts.sync_offset != 0) {
1674 freq = scsi_calc_syncsrate(cts.sync_period);
1677 struct ccb_pathinq cpi;
1679 /* Ask the SIM for its base transfer speed */
1680 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1681 cpi.ccb_h.func_code = XPT_PATH_INQ;
1682 xpt_action((union ccb *)&cpi);
1684 speed = cpi.base_transfer_speed;
1687 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1688 speed *= (0x01 << cts.bus_width);
1691 printf("%s%d: %d.%03dMB/s transfers",
1692 periph->periph_name, periph->unit_number,
1695 printf("%s%d: %dKB/s transfers", periph->periph_name,
1696 periph->unit_number, speed);
1697 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1698 && cts.sync_offset != 0) {
1699 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1700 freq % 1000, cts.sync_offset);
1702 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1703 && cts.bus_width > 0) {
1704 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1705 && cts.sync_offset != 0) {
1710 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1711 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1712 && cts.sync_offset != 0) {
1716 if (path->device->inq_flags & SID_CmdQue
1717 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1718 printf(", Tagged Queueing Enabled");
1722 } else if (path->device->inq_flags & SID_CmdQue
1723 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1724 printf("%s%d: Tagged Queueing Enabled\n",
1725 periph->periph_name, periph->unit_number);
1729 * We only want to print the caller's announce string if they've
1732 if (announce_string != NULL)
1733 printf("%s%d: %s\n", periph->periph_name,
1734 periph->unit_number, announce_string);
1738 #endif /* CAM_NEW_TRAN_CODE */
1740 static dev_match_ret
1741 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1744 dev_match_ret retval;
1747 retval = DM_RET_NONE;
1750 * If we aren't given something to match against, that's an error.
1753 return(DM_RET_ERROR);
1756 * If there are no match entries, then this bus matches no
1759 if ((patterns == NULL) || (num_patterns == 0))
1760 return(DM_RET_DESCEND | DM_RET_COPY);
1762 for (i = 0; i < num_patterns; i++) {
1763 struct bus_match_pattern *cur_pattern;
1766 * If the pattern in question isn't for a bus node, we
1767 * aren't interested. However, we do indicate to the
1768 * calling routine that we should continue descending the
1769 * tree, since the user wants to match against lower-level
1772 if (patterns[i].type != DEV_MATCH_BUS) {
1773 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1774 retval |= DM_RET_DESCEND;
1778 cur_pattern = &patterns[i].pattern.bus_pattern;
1781 * If they want to match any bus node, we give them any
1784 if (cur_pattern->flags == BUS_MATCH_ANY) {
1785 /* set the copy flag */
1786 retval |= DM_RET_COPY;
1789 * If we've already decided on an action, go ahead
1792 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1797 * Not sure why someone would do this...
1799 if (cur_pattern->flags == BUS_MATCH_NONE)
1802 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1803 && (cur_pattern->path_id != bus->path_id))
1806 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1807 && (cur_pattern->bus_id != bus->sim->bus_id))
1810 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1811 && (cur_pattern->unit_number != bus->sim->unit_number))
1814 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1815 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1820 * If we get to this point, the user definitely wants
1821 * information on this bus. So tell the caller to copy the
1824 retval |= DM_RET_COPY;
1827 * If the return action has been set to descend, then we
1828 * know that we've already seen a non-bus matching
1829 * expression, therefore we need to further descend the tree.
1830 * This won't change by continuing around the loop, so we
1831 * go ahead and return. If we haven't seen a non-bus
1832 * matching expression, we keep going around the loop until
1833 * we exhaust the matching expressions. We'll set the stop
1834 * flag once we fall out of the loop.
1836 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1841 * If the return action hasn't been set to descend yet, that means
1842 * we haven't seen anything other than bus matching patterns. So
1843 * tell the caller to stop descending the tree -- the user doesn't
1844 * want to match against lower level tree elements.
1846 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1847 retval |= DM_RET_STOP;
1852 static dev_match_ret
1853 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1854 struct cam_ed *device)
1856 dev_match_ret retval;
1859 retval = DM_RET_NONE;
1862 * If we aren't given something to match against, that's an error.
1865 return(DM_RET_ERROR);
1868 * If there are no match entries, then this device matches no
1871 if ((patterns == NULL) || (patterns == 0))
1872 return(DM_RET_DESCEND | DM_RET_COPY);
1874 for (i = 0; i < num_patterns; i++) {
1875 struct device_match_pattern *cur_pattern;
1878 * If the pattern in question isn't for a device node, we
1879 * aren't interested.
1881 if (patterns[i].type != DEV_MATCH_DEVICE) {
1882 if ((patterns[i].type == DEV_MATCH_PERIPH)
1883 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1884 retval |= DM_RET_DESCEND;
1888 cur_pattern = &patterns[i].pattern.device_pattern;
1891 * If they want to match any device node, we give them any
1894 if (cur_pattern->flags == DEV_MATCH_ANY) {
1895 /* set the copy flag */
1896 retval |= DM_RET_COPY;
1900 * If we've already decided on an action, go ahead
1903 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1908 * Not sure why someone would do this...
1910 if (cur_pattern->flags == DEV_MATCH_NONE)
1913 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1914 && (cur_pattern->path_id != device->target->bus->path_id))
1917 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1918 && (cur_pattern->target_id != device->target->target_id))
1921 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1922 && (cur_pattern->target_lun != device->lun_id))
1925 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1926 && (cam_quirkmatch((caddr_t)&device->inq_data,
1927 (caddr_t)&cur_pattern->inq_pat,
1928 1, sizeof(cur_pattern->inq_pat),
1929 scsi_static_inquiry_match) == NULL))
1933 * If we get to this point, the user definitely wants
1934 * information on this device. So tell the caller to copy
1937 retval |= DM_RET_COPY;
1940 * If the return action has been set to descend, then we
1941 * know that we've already seen a peripheral matching
1942 * expression, therefore we need to further descend the tree.
1943 * This won't change by continuing around the loop, so we
1944 * go ahead and return. If we haven't seen a peripheral
1945 * matching expression, we keep going around the loop until
1946 * we exhaust the matching expressions. We'll set the stop
1947 * flag once we fall out of the loop.
1949 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1954 * If the return action hasn't been set to descend yet, that means
1955 * we haven't seen any peripheral matching patterns. So tell the
1956 * caller to stop descending the tree -- the user doesn't want to
1957 * match against lower level tree elements.
1959 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1960 retval |= DM_RET_STOP;
1966 * Match a single peripheral against any number of match patterns.
1968 static dev_match_ret
1969 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1970 struct cam_periph *periph)
1972 dev_match_ret retval;
1976 * If we aren't given something to match against, that's an error.
1979 return(DM_RET_ERROR);
1982 * If there are no match entries, then this peripheral matches no
1985 if ((patterns == NULL) || (num_patterns == 0))
1986 return(DM_RET_STOP | DM_RET_COPY);
1989 * There aren't any nodes below a peripheral node, so there's no
1990 * reason to descend the tree any further.
1992 retval = DM_RET_STOP;
1994 for (i = 0; i < num_patterns; i++) {
1995 struct periph_match_pattern *cur_pattern;
1998 * If the pattern in question isn't for a peripheral, we
1999 * aren't interested.
2001 if (patterns[i].type != DEV_MATCH_PERIPH)
2004 cur_pattern = &patterns[i].pattern.periph_pattern;
2007 * If they want to match on anything, then we will do so.
2009 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2010 /* set the copy flag */
2011 retval |= DM_RET_COPY;
2014 * We've already set the return action to stop,
2015 * since there are no nodes below peripherals in
2022 * Not sure why someone would do this...
2024 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2027 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2028 && (cur_pattern->path_id != periph->path->bus->path_id))
2032 * For the target and lun id's, we have to make sure the
2033 * target and lun pointers aren't NULL. The xpt peripheral
2034 * has a wildcard target and device.
2036 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2037 && ((periph->path->target == NULL)
2038 ||(cur_pattern->target_id != periph->path->target->target_id)))
2041 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2042 && ((periph->path->device == NULL)
2043 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2046 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2047 && (cur_pattern->unit_number != periph->unit_number))
2050 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2051 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2056 * If we get to this point, the user definitely wants
2057 * information on this peripheral. So tell the caller to
2058 * copy the data out.
2060 retval |= DM_RET_COPY;
2063 * The return action has already been set to stop, since
2064 * peripherals don't have any nodes below them in the EDT.
2070 * If we get to this point, the peripheral that was passed in
2071 * doesn't match any of the patterns.
2077 xptedtbusfunc(struct cam_eb *bus, void *arg)
2079 struct ccb_dev_match *cdm;
2080 dev_match_ret retval;
2082 cdm = (struct ccb_dev_match *)arg;
2085 * If our position is for something deeper in the tree, that means
2086 * that we've already seen this node. So, we keep going down.
2088 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2089 && (cdm->pos.cookie.bus == bus)
2090 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2091 && (cdm->pos.cookie.target != NULL))
2092 retval = DM_RET_DESCEND;
2094 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2097 * If we got an error, bail out of the search.
2099 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2100 cdm->status = CAM_DEV_MATCH_ERROR;
2105 * If the copy flag is set, copy this bus out.
2107 if (retval & DM_RET_COPY) {
2110 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2111 sizeof(struct dev_match_result));
2114 * If we don't have enough space to put in another
2115 * match result, save our position and tell the
2116 * user there are more devices to check.
2118 if (spaceleft < sizeof(struct dev_match_result)) {
2119 bzero(&cdm->pos, sizeof(cdm->pos));
2120 cdm->pos.position_type =
2121 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2123 cdm->pos.cookie.bus = bus;
2124 cdm->pos.generations[CAM_BUS_GENERATION]=
2126 cdm->status = CAM_DEV_MATCH_MORE;
2129 j = cdm->num_matches;
2131 cdm->matches[j].type = DEV_MATCH_BUS;
2132 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2133 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2134 cdm->matches[j].result.bus_result.unit_number =
2135 bus->sim->unit_number;
2136 strncpy(cdm->matches[j].result.bus_result.dev_name,
2137 bus->sim->sim_name, DEV_IDLEN);
2141 * If the user is only interested in busses, there's no
2142 * reason to descend to the next level in the tree.
2144 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2148 * If there is a target generation recorded, check it to
2149 * make sure the target list hasn't changed.
2151 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2152 && (bus == cdm->pos.cookie.bus)
2153 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2154 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2155 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2157 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2161 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2162 && (cdm->pos.cookie.bus == bus)
2163 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2164 && (cdm->pos.cookie.target != NULL))
2165 return(xpttargettraverse(bus,
2166 (struct cam_et *)cdm->pos.cookie.target,
2167 xptedttargetfunc, arg));
2169 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2173 xptedttargetfunc(struct cam_et *target, void *arg)
2175 struct ccb_dev_match *cdm;
2177 cdm = (struct ccb_dev_match *)arg;
2180 * If there is a device list generation recorded, check it to
2181 * make sure the device list hasn't changed.
2183 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2184 && (cdm->pos.cookie.bus == target->bus)
2185 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2186 && (cdm->pos.cookie.target == target)
2187 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2188 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2189 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2190 target->generation)) {
2191 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2195 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2196 && (cdm->pos.cookie.bus == target->bus)
2197 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2198 && (cdm->pos.cookie.target == target)
2199 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2200 && (cdm->pos.cookie.device != NULL))
2201 return(xptdevicetraverse(target,
2202 (struct cam_ed *)cdm->pos.cookie.device,
2203 xptedtdevicefunc, arg));
2205 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2209 xptedtdevicefunc(struct cam_ed *device, void *arg)
2212 struct ccb_dev_match *cdm;
2213 dev_match_ret retval;
2215 cdm = (struct ccb_dev_match *)arg;
2218 * If our position is for something deeper in the tree, that means
2219 * that we've already seen this node. So, we keep going down.
2221 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2222 && (cdm->pos.cookie.device == device)
2223 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2224 && (cdm->pos.cookie.periph != NULL))
2225 retval = DM_RET_DESCEND;
2227 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2230 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2231 cdm->status = CAM_DEV_MATCH_ERROR;
2236 * If the copy flag is set, copy this device out.
2238 if (retval & DM_RET_COPY) {
2241 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2242 sizeof(struct dev_match_result));
2245 * If we don't have enough space to put in another
2246 * match result, save our position and tell the
2247 * user there are more devices to check.
2249 if (spaceleft < sizeof(struct dev_match_result)) {
2250 bzero(&cdm->pos, sizeof(cdm->pos));
2251 cdm->pos.position_type =
2252 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2253 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2255 cdm->pos.cookie.bus = device->target->bus;
2256 cdm->pos.generations[CAM_BUS_GENERATION]=
2258 cdm->pos.cookie.target = device->target;
2259 cdm->pos.generations[CAM_TARGET_GENERATION] =
2260 device->target->bus->generation;
2261 cdm->pos.cookie.device = device;
2262 cdm->pos.generations[CAM_DEV_GENERATION] =
2263 device->target->generation;
2264 cdm->status = CAM_DEV_MATCH_MORE;
2267 j = cdm->num_matches;
2269 cdm->matches[j].type = DEV_MATCH_DEVICE;
2270 cdm->matches[j].result.device_result.path_id =
2271 device->target->bus->path_id;
2272 cdm->matches[j].result.device_result.target_id =
2273 device->target->target_id;
2274 cdm->matches[j].result.device_result.target_lun =
2276 bcopy(&device->inq_data,
2277 &cdm->matches[j].result.device_result.inq_data,
2278 sizeof(struct scsi_inquiry_data));
2280 /* Let the user know whether this device is unconfigured */
2281 if (device->flags & CAM_DEV_UNCONFIGURED)
2282 cdm->matches[j].result.device_result.flags =
2283 DEV_RESULT_UNCONFIGURED;
2285 cdm->matches[j].result.device_result.flags =
2290 * If the user isn't interested in peripherals, don't descend
2291 * the tree any further.
2293 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2297 * If there is a peripheral list generation recorded, make sure
2298 * it hasn't changed.
2300 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2301 && (device->target->bus == cdm->pos.cookie.bus)
2302 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2303 && (device->target == cdm->pos.cookie.target)
2304 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2305 && (device == cdm->pos.cookie.device)
2306 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2307 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2308 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2309 device->generation)){
2310 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2314 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2315 && (cdm->pos.cookie.bus == device->target->bus)
2316 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2317 && (cdm->pos.cookie.target == device->target)
2318 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2319 && (cdm->pos.cookie.device == device)
2320 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2321 && (cdm->pos.cookie.periph != NULL))
2322 return(xptperiphtraverse(device,
2323 (struct cam_periph *)cdm->pos.cookie.periph,
2324 xptedtperiphfunc, arg));
2326 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2330 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2332 struct ccb_dev_match *cdm;
2333 dev_match_ret retval;
2335 cdm = (struct ccb_dev_match *)arg;
2337 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2339 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2340 cdm->status = CAM_DEV_MATCH_ERROR;
2345 * If the copy flag is set, copy this peripheral out.
2347 if (retval & DM_RET_COPY) {
2350 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2351 sizeof(struct dev_match_result));
2354 * If we don't have enough space to put in another
2355 * match result, save our position and tell the
2356 * user there are more devices to check.
2358 if (spaceleft < sizeof(struct dev_match_result)) {
2359 bzero(&cdm->pos, sizeof(cdm->pos));
2360 cdm->pos.position_type =
2361 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2362 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2365 cdm->pos.cookie.bus = periph->path->bus;
2366 cdm->pos.generations[CAM_BUS_GENERATION]=
2368 cdm->pos.cookie.target = periph->path->target;
2369 cdm->pos.generations[CAM_TARGET_GENERATION] =
2370 periph->path->bus->generation;
2371 cdm->pos.cookie.device = periph->path->device;
2372 cdm->pos.generations[CAM_DEV_GENERATION] =
2373 periph->path->target->generation;
2374 cdm->pos.cookie.periph = periph;
2375 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2376 periph->path->device->generation;
2377 cdm->status = CAM_DEV_MATCH_MORE;
2381 j = cdm->num_matches;
2383 cdm->matches[j].type = DEV_MATCH_PERIPH;
2384 cdm->matches[j].result.periph_result.path_id =
2385 periph->path->bus->path_id;
2386 cdm->matches[j].result.periph_result.target_id =
2387 periph->path->target->target_id;
2388 cdm->matches[j].result.periph_result.target_lun =
2389 periph->path->device->lun_id;
2390 cdm->matches[j].result.periph_result.unit_number =
2391 periph->unit_number;
2392 strncpy(cdm->matches[j].result.periph_result.periph_name,
2393 periph->periph_name, DEV_IDLEN);
2400 xptedtmatch(struct ccb_dev_match *cdm)
2404 cdm->num_matches = 0;
2407 * Check the bus list generation. If it has changed, the user
2408 * needs to reset everything and start over.
2410 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2411 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2412 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2413 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2417 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2418 && (cdm->pos.cookie.bus != NULL))
2419 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2420 xptedtbusfunc, cdm);
2422 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2425 * If we get back 0, that means that we had to stop before fully
2426 * traversing the EDT. It also means that one of the subroutines
2427 * has set the status field to the proper value. If we get back 1,
2428 * we've fully traversed the EDT and copied out any matching entries.
2431 cdm->status = CAM_DEV_MATCH_LAST;
2437 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2439 struct ccb_dev_match *cdm;
2441 cdm = (struct ccb_dev_match *)arg;
2443 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2444 && (cdm->pos.cookie.pdrv == pdrv)
2445 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2446 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2447 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2448 (*pdrv)->generation)) {
2449 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2453 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2454 && (cdm->pos.cookie.pdrv == pdrv)
2455 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2456 && (cdm->pos.cookie.periph != NULL))
2457 return(xptpdperiphtraverse(pdrv,
2458 (struct cam_periph *)cdm->pos.cookie.periph,
2459 xptplistperiphfunc, arg));
2461 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2465 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2467 struct ccb_dev_match *cdm;
2468 dev_match_ret retval;
2470 cdm = (struct ccb_dev_match *)arg;
2472 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2474 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2475 cdm->status = CAM_DEV_MATCH_ERROR;
2480 * If the copy flag is set, copy this peripheral out.
2482 if (retval & DM_RET_COPY) {
2485 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2486 sizeof(struct dev_match_result));
2489 * If we don't have enough space to put in another
2490 * match result, save our position and tell the
2491 * user there are more devices to check.
2493 if (spaceleft < sizeof(struct dev_match_result)) {
2494 struct periph_driver **pdrv;
2497 bzero(&cdm->pos, sizeof(cdm->pos));
2498 cdm->pos.position_type =
2499 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2503 * This may look a bit non-sensical, but it is
2504 * actually quite logical. There are very few
2505 * peripheral drivers, and bloating every peripheral
2506 * structure with a pointer back to its parent
2507 * peripheral driver linker set entry would cost
2508 * more in the long run than doing this quick lookup.
2510 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2511 if (strcmp((*pdrv)->driver_name,
2512 periph->periph_name) == 0)
2517 cdm->status = CAM_DEV_MATCH_ERROR;
2521 cdm->pos.cookie.pdrv = pdrv;
2523 * The periph generation slot does double duty, as
2524 * does the periph pointer slot. They are used for
2525 * both edt and pdrv lookups and positioning.
2527 cdm->pos.cookie.periph = periph;
2528 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2529 (*pdrv)->generation;
2530 cdm->status = CAM_DEV_MATCH_MORE;
2534 j = cdm->num_matches;
2536 cdm->matches[j].type = DEV_MATCH_PERIPH;
2537 cdm->matches[j].result.periph_result.path_id =
2538 periph->path->bus->path_id;
2541 * The transport layer peripheral doesn't have a target or
2544 if (periph->path->target)
2545 cdm->matches[j].result.periph_result.target_id =
2546 periph->path->target->target_id;
2548 cdm->matches[j].result.periph_result.target_id = -1;
2550 if (periph->path->device)
2551 cdm->matches[j].result.periph_result.target_lun =
2552 periph->path->device->lun_id;
2554 cdm->matches[j].result.periph_result.target_lun = -1;
2556 cdm->matches[j].result.periph_result.unit_number =
2557 periph->unit_number;
2558 strncpy(cdm->matches[j].result.periph_result.periph_name,
2559 periph->periph_name, DEV_IDLEN);
2566 xptperiphlistmatch(struct ccb_dev_match *cdm)
2570 cdm->num_matches = 0;
2573 * At this point in the edt traversal function, we check the bus
2574 * list generation to make sure that no busses have been added or
2575 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2576 * For the peripheral driver list traversal function, however, we
2577 * don't have to worry about new peripheral driver types coming or
2578 * going; they're in a linker set, and therefore can't change
2579 * without a recompile.
2582 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2583 && (cdm->pos.cookie.pdrv != NULL))
2584 ret = xptpdrvtraverse(
2585 (struct periph_driver **)cdm->pos.cookie.pdrv,
2586 xptplistpdrvfunc, cdm);
2588 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2591 * If we get back 0, that means that we had to stop before fully
2592 * traversing the peripheral driver tree. It also means that one of
2593 * the subroutines has set the status field to the proper value. If
2594 * we get back 1, we've fully traversed the EDT and copied out any
2598 cdm->status = CAM_DEV_MATCH_LAST;
2604 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2606 struct cam_eb *bus, *next_bus;
2611 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2614 next_bus = TAILQ_NEXT(bus, links);
2616 retval = tr_func(bus, arg);
2625 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2626 xpt_targetfunc_t *tr_func, void *arg)
2628 struct cam_et *target, *next_target;
2632 for (target = (start_target ? start_target :
2633 TAILQ_FIRST(&bus->et_entries));
2634 target != NULL; target = next_target) {
2636 next_target = TAILQ_NEXT(target, links);
2638 retval = tr_func(target, arg);
2648 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2649 xpt_devicefunc_t *tr_func, void *arg)
2651 struct cam_ed *device, *next_device;
2655 for (device = (start_device ? start_device :
2656 TAILQ_FIRST(&target->ed_entries));
2658 device = next_device) {
2660 next_device = TAILQ_NEXT(device, links);
2662 retval = tr_func(device, arg);
2672 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2673 xpt_periphfunc_t *tr_func, void *arg)
2675 struct cam_periph *periph, *next_periph;
2680 for (periph = (start_periph ? start_periph :
2681 SLIST_FIRST(&device->periphs));
2683 periph = next_periph) {
2685 next_periph = SLIST_NEXT(periph, periph_links);
2687 retval = tr_func(periph, arg);
2696 xptpdrvtraverse(struct periph_driver **start_pdrv,
2697 xpt_pdrvfunc_t *tr_func, void *arg)
2699 struct periph_driver **pdrv;
2705 * We don't traverse the peripheral driver list like we do the
2706 * other lists, because it is a linker set, and therefore cannot be
2707 * changed during runtime. If the peripheral driver list is ever
2708 * re-done to be something other than a linker set (i.e. it can
2709 * change while the system is running), the list traversal should
2710 * be modified to work like the other traversal functions.
2712 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2713 *pdrv != NULL; pdrv++) {
2714 retval = tr_func(pdrv, arg);
2724 xptpdperiphtraverse(struct periph_driver **pdrv,
2725 struct cam_periph *start_periph,
2726 xpt_periphfunc_t *tr_func, void *arg)
2728 struct cam_periph *periph, *next_periph;
2733 for (periph = (start_periph ? start_periph :
2734 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2735 periph = next_periph) {
2737 next_periph = TAILQ_NEXT(periph, unit_links);
2739 retval = tr_func(periph, arg);
2747 xptdefbusfunc(struct cam_eb *bus, void *arg)
2749 struct xpt_traverse_config *tr_config;
2751 tr_config = (struct xpt_traverse_config *)arg;
2753 if (tr_config->depth == XPT_DEPTH_BUS) {
2754 xpt_busfunc_t *tr_func;
2756 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2758 return(tr_func(bus, tr_config->tr_arg));
2760 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2764 xptdeftargetfunc(struct cam_et *target, void *arg)
2766 struct xpt_traverse_config *tr_config;
2768 tr_config = (struct xpt_traverse_config *)arg;
2770 if (tr_config->depth == XPT_DEPTH_TARGET) {
2771 xpt_targetfunc_t *tr_func;
2773 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2775 return(tr_func(target, tr_config->tr_arg));
2777 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2781 xptdefdevicefunc(struct cam_ed *device, void *arg)
2783 struct xpt_traverse_config *tr_config;
2785 tr_config = (struct xpt_traverse_config *)arg;
2787 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2788 xpt_devicefunc_t *tr_func;
2790 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2792 return(tr_func(device, tr_config->tr_arg));
2794 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2798 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2800 struct xpt_traverse_config *tr_config;
2801 xpt_periphfunc_t *tr_func;
2803 tr_config = (struct xpt_traverse_config *)arg;
2805 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2808 * Unlike the other default functions, we don't check for depth
2809 * here. The peripheral driver level is the last level in the EDT,
2810 * so if we're here, we should execute the function in question.
2812 return(tr_func(periph, tr_config->tr_arg));
2816 * Execute the given function for every bus in the EDT.
2819 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2821 struct xpt_traverse_config tr_config;
2823 tr_config.depth = XPT_DEPTH_BUS;
2824 tr_config.tr_func = tr_func;
2825 tr_config.tr_arg = arg;
2827 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2832 * Execute the given function for every target in the EDT.
2835 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2837 struct xpt_traverse_config tr_config;
2839 tr_config.depth = XPT_DEPTH_TARGET;
2840 tr_config.tr_func = tr_func;
2841 tr_config.tr_arg = arg;
2843 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2845 #endif /* notusedyet */
2848 * Execute the given function for every device in the EDT.
2851 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2853 struct xpt_traverse_config tr_config;
2855 tr_config.depth = XPT_DEPTH_DEVICE;
2856 tr_config.tr_func = tr_func;
2857 tr_config.tr_arg = arg;
2859 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2864 * Execute the given function for every peripheral in the EDT.
2867 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2869 struct xpt_traverse_config tr_config;
2871 tr_config.depth = XPT_DEPTH_PERIPH;
2872 tr_config.tr_func = tr_func;
2873 tr_config.tr_arg = arg;
2875 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2877 #endif /* notusedyet */
2880 xptsetasyncfunc(struct cam_ed *device, void *arg)
2882 struct cam_path path;
2883 struct ccb_getdev cgd;
2884 struct async_node *cur_entry;
2886 cur_entry = (struct async_node *)arg;
2889 * Don't report unconfigured devices (Wildcard devs,
2890 * devices only for target mode, device instances
2891 * that have been invalidated but are waiting for
2892 * their last reference count to be released).
2894 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2897 xpt_compile_path(&path,
2899 device->target->bus->path_id,
2900 device->target->target_id,
2902 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2903 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2904 xpt_action((union ccb *)&cgd);
2905 cur_entry->callback(cur_entry->callback_arg,
2908 xpt_release_path(&path);
2914 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2916 struct cam_path path;
2917 struct ccb_pathinq cpi;
2918 struct async_node *cur_entry;
2920 cur_entry = (struct async_node *)arg;
2922 xpt_compile_path(&path, /*periph*/NULL,
2924 CAM_TARGET_WILDCARD,
2926 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2927 cpi.ccb_h.func_code = XPT_PATH_INQ;
2928 xpt_action((union ccb *)&cpi);
2929 cur_entry->callback(cur_entry->callback_arg,
2932 xpt_release_path(&path);
2938 xpt_action(union ccb *start_ccb)
2942 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2944 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2946 iopl = splsoftcam();
2947 switch (start_ccb->ccb_h.func_code) {
2950 #ifdef CAM_NEW_TRAN_CODE
2951 struct cam_ed *device;
2952 #endif /* CAM_NEW_TRAN_CODE */
2954 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2955 struct cam_path *path;
2957 path = start_ccb->ccb_h.path;
2961 * For the sake of compatibility with SCSI-1
2962 * devices that may not understand the identify
2963 * message, we include lun information in the
2964 * second byte of all commands. SCSI-1 specifies
2965 * that luns are a 3 bit value and reserves only 3
2966 * bits for lun information in the CDB. Later
2967 * revisions of the SCSI spec allow for more than 8
2968 * luns, but have deprecated lun information in the
2969 * CDB. So, if the lun won't fit, we must omit.
2971 * Also be aware that during initial probing for devices,
2972 * the inquiry information is unknown but initialized to 0.
2973 * This means that this code will be exercised while probing
2974 * devices with an ANSI revision greater than 2.
2976 #ifdef CAM_NEW_TRAN_CODE
2977 device = start_ccb->ccb_h.path->device;
2978 if (device->protocol_version <= SCSI_REV_2
2979 #else /* CAM_NEW_TRAN_CODE */
2980 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2981 #endif /* CAM_NEW_TRAN_CODE */
2982 && start_ccb->ccb_h.target_lun < 8
2983 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2985 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2986 start_ccb->ccb_h.target_lun << 5;
2988 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2989 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2990 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2991 &path->device->inq_data),
2992 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2993 cdb_str, sizeof(cdb_str))));
2997 case XPT_CONT_TARGET_IO:
2998 start_ccb->csio.sense_resid = 0;
2999 start_ccb->csio.resid = 0;
3004 struct cam_path *path;
3008 path = start_ccb->ccb_h.path;
3011 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3012 if (path->device->qfrozen_cnt == 0)
3013 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3018 xpt_run_dev_sendq(path->bus);
3021 case XPT_SET_TRAN_SETTINGS:
3023 xpt_set_transfer_settings(&start_ccb->cts,
3024 start_ccb->ccb_h.path->device,
3025 /*async_update*/FALSE);
3028 case XPT_CALC_GEOMETRY:
3030 struct cam_sim *sim;
3032 /* Filter out garbage */
3033 if (start_ccb->ccg.block_size == 0
3034 || start_ccb->ccg.volume_size == 0) {
3035 start_ccb->ccg.cylinders = 0;
3036 start_ccb->ccg.heads = 0;
3037 start_ccb->ccg.secs_per_track = 0;
3038 start_ccb->ccb_h.status = CAM_REQ_CMP;
3043 * In a PC-98 system, geometry translation depens on
3044 * the "real" device geometry obtained from mode page 4.
3045 * SCSI geometry translation is performed in the
3046 * initialization routine of the SCSI BIOS and the result
3047 * stored in host memory. If the translation is available
3048 * in host memory, use it. If not, rely on the default
3049 * translation the device driver performs.
3051 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3052 start_ccb->ccb_h.status = CAM_REQ_CMP;
3056 sim = start_ccb->ccb_h.path->bus->sim;
3057 (*(sim->sim_action))(sim, start_ccb);
3062 union ccb* abort_ccb;
3065 abort_ccb = start_ccb->cab.abort_ccb;
3066 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3068 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3069 struct cam_ccbq *ccbq;
3071 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3072 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3073 abort_ccb->ccb_h.status =
3074 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3075 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3077 xpt_done(abort_ccb);
3079 start_ccb->ccb_h.status = CAM_REQ_CMP;
3082 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3083 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3085 * We've caught this ccb en route to
3086 * the SIM. Flag it for abort and the
3087 * SIM will do so just before starting
3088 * real work on the CCB.
3090 abort_ccb->ccb_h.status =
3091 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3092 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3093 start_ccb->ccb_h.status = CAM_REQ_CMP;
3097 if (XPT_FC_IS_QUEUED(abort_ccb)
3098 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3100 * It's already completed but waiting
3101 * for our SWI to get to it.
3103 start_ccb->ccb_h.status = CAM_UA_ABORT;
3107 * If we weren't able to take care of the abort request
3108 * in the XPT, pass the request down to the SIM for processing.
3112 case XPT_ACCEPT_TARGET_IO:
3114 case XPT_IMMED_NOTIFY:
3115 case XPT_NOTIFY_ACK:
3116 case XPT_GET_TRAN_SETTINGS:
3119 struct cam_sim *sim;
3121 sim = start_ccb->ccb_h.path->bus->sim;
3122 (*(sim->sim_action))(sim, start_ccb);
3127 struct cam_sim *sim;
3129 sim = start_ccb->ccb_h.path->bus->sim;
3130 (*(sim->sim_action))(sim, start_ccb);
3133 case XPT_PATH_STATS:
3134 start_ccb->cpis.last_reset =
3135 start_ccb->ccb_h.path->bus->last_reset;
3136 start_ccb->ccb_h.status = CAM_REQ_CMP;
3143 dev = start_ccb->ccb_h.path->device;
3145 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3146 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3148 struct ccb_getdev *cgd;
3152 cgd = &start_ccb->cgd;
3153 bus = cgd->ccb_h.path->bus;
3154 tar = cgd->ccb_h.path->target;
3155 cgd->inq_data = dev->inq_data;
3156 cgd->ccb_h.status = CAM_REQ_CMP;
3157 cgd->serial_num_len = dev->serial_num_len;
3158 if ((dev->serial_num_len > 0)
3159 && (dev->serial_num != NULL))
3160 bcopy(dev->serial_num, cgd->serial_num,
3161 dev->serial_num_len);
3166 case XPT_GDEV_STATS:
3171 dev = start_ccb->ccb_h.path->device;
3173 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3174 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3176 struct ccb_getdevstats *cgds;
3180 cgds = &start_ccb->cgds;
3181 bus = cgds->ccb_h.path->bus;
3182 tar = cgds->ccb_h.path->target;
3183 cgds->dev_openings = dev->ccbq.dev_openings;
3184 cgds->dev_active = dev->ccbq.dev_active;
3185 cgds->devq_openings = dev->ccbq.devq_openings;
3186 cgds->devq_queued = dev->ccbq.queue.entries;
3187 cgds->held = dev->ccbq.held;
3188 cgds->last_reset = tar->last_reset;
3189 cgds->maxtags = dev->quirk->maxtags;
3190 cgds->mintags = dev->quirk->mintags;
3191 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3192 cgds->last_reset = bus->last_reset;
3193 cgds->ccb_h.status = CAM_REQ_CMP;
3200 struct cam_periph *nperiph;
3201 struct periph_list *periph_head;
3202 struct ccb_getdevlist *cgdl;
3205 struct cam_ed *device;
3212 * Don't want anyone mucking with our data.
3215 device = start_ccb->ccb_h.path->device;
3216 periph_head = &device->periphs;
3217 cgdl = &start_ccb->cgdl;
3220 * Check and see if the list has changed since the user
3221 * last requested a list member. If so, tell them that the
3222 * list has changed, and therefore they need to start over
3223 * from the beginning.
3225 if ((cgdl->index != 0) &&
3226 (cgdl->generation != device->generation)) {
3227 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3233 * Traverse the list of peripherals and attempt to find
3234 * the requested peripheral.
3236 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3237 (nperiph != NULL) && (i <= cgdl->index);
3238 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3239 if (i == cgdl->index) {
3240 strncpy(cgdl->periph_name,
3241 nperiph->periph_name,
3243 cgdl->unit_number = nperiph->unit_number;
3248 cgdl->status = CAM_GDEVLIST_ERROR;
3253 if (nperiph == NULL)
3254 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3256 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3259 cgdl->generation = device->generation;
3262 cgdl->ccb_h.status = CAM_REQ_CMP;
3268 dev_pos_type position_type;
3269 struct ccb_dev_match *cdm;
3272 cdm = &start_ccb->cdm;
3275 * Prevent EDT changes while we traverse it.
3279 * There are two ways of getting at information in the EDT.
3280 * The first way is via the primary EDT tree. It starts
3281 * with a list of busses, then a list of targets on a bus,
3282 * then devices/luns on a target, and then peripherals on a
3283 * device/lun. The "other" way is by the peripheral driver
3284 * lists. The peripheral driver lists are organized by
3285 * peripheral driver. (obviously) So it makes sense to
3286 * use the peripheral driver list if the user is looking
3287 * for something like "da1", or all "da" devices. If the
3288 * user is looking for something on a particular bus/target
3289 * or lun, it's generally better to go through the EDT tree.
3292 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3293 position_type = cdm->pos.position_type;
3297 position_type = CAM_DEV_POS_NONE;
3299 for (i = 0; i < cdm->num_patterns; i++) {
3300 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3301 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3302 position_type = CAM_DEV_POS_EDT;
3307 if (cdm->num_patterns == 0)
3308 position_type = CAM_DEV_POS_EDT;
3309 else if (position_type == CAM_DEV_POS_NONE)
3310 position_type = CAM_DEV_POS_PDRV;
3313 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3314 case CAM_DEV_POS_EDT:
3315 ret = xptedtmatch(cdm);
3317 case CAM_DEV_POS_PDRV:
3318 ret = xptperiphlistmatch(cdm);
3321 cdm->status = CAM_DEV_MATCH_ERROR;
3327 if (cdm->status == CAM_DEV_MATCH_ERROR)
3328 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3330 start_ccb->ccb_h.status = CAM_REQ_CMP;
3336 struct ccb_setasync *csa;
3337 struct async_node *cur_entry;
3338 struct async_list *async_head;
3342 csa = &start_ccb->csa;
3343 added = csa->event_enable;
3344 async_head = &csa->ccb_h.path->device->asyncs;
3347 * If there is already an entry for us, simply
3351 cur_entry = SLIST_FIRST(async_head);
3352 while (cur_entry != NULL) {
3353 if ((cur_entry->callback_arg == csa->callback_arg)
3354 && (cur_entry->callback == csa->callback))
3356 cur_entry = SLIST_NEXT(cur_entry, links);
3359 if (cur_entry != NULL) {
3361 * If the request has no flags set,
3364 added &= ~cur_entry->event_enable;
3365 if (csa->event_enable == 0) {
3366 SLIST_REMOVE(async_head, cur_entry,
3368 csa->ccb_h.path->device->refcount--;
3369 free(cur_entry, M_DEVBUF);
3371 cur_entry->event_enable = csa->event_enable;
3374 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3376 if (cur_entry == NULL) {
3378 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3381 cur_entry->event_enable = csa->event_enable;
3382 cur_entry->callback_arg = csa->callback_arg;
3383 cur_entry->callback = csa->callback;
3384 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3385 csa->ccb_h.path->device->refcount++;
3388 if ((added & AC_FOUND_DEVICE) != 0) {
3390 * Get this peripheral up to date with all
3391 * the currently existing devices.
3393 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3395 if ((added & AC_PATH_REGISTERED) != 0) {
3397 * Get this peripheral up to date with all
3398 * the currently existing busses.
3400 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3403 start_ccb->ccb_h.status = CAM_REQ_CMP;
3408 struct ccb_relsim *crs;
3412 crs = &start_ccb->crs;
3413 dev = crs->ccb_h.path->device;
3416 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3422 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3424 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3426 /* Don't ever go below one opening */
3427 if (crs->openings > 0) {
3428 xpt_dev_ccbq_resize(crs->ccb_h.path,
3432 xpt_print_path(crs->ccb_h.path);
3433 printf("tagged openings "
3441 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3443 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3446 * Just extend the old timeout and decrement
3447 * the freeze count so that a single timeout
3448 * is sufficient for releasing the queue.
3450 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3451 untimeout(xpt_release_devq_timeout,
3452 dev, dev->c_handle);
3455 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3459 timeout(xpt_release_devq_timeout,
3461 (crs->release_timeout * hz) / 1000);
3463 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3467 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3469 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3471 * Decrement the freeze count so that a single
3472 * completion is still sufficient to unfreeze
3475 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3478 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3479 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3483 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3485 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3486 || (dev->ccbq.dev_active == 0)) {
3488 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3491 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3492 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3497 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3499 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3502 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3503 start_ccb->ccb_h.status = CAM_REQ_CMP;
3507 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3510 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3511 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3519 #ifdef CAM_DEBUG_DELAY
3520 cam_debug_delay = CAM_DEBUG_DELAY;
3522 cam_dflags = start_ccb->cdbg.flags;
3523 if (cam_dpath != NULL) {
3524 xpt_free_path(cam_dpath);
3528 if (cam_dflags != CAM_DEBUG_NONE) {
3529 if (xpt_create_path(&cam_dpath, xpt_periph,
3530 start_ccb->ccb_h.path_id,
3531 start_ccb->ccb_h.target_id,
3532 start_ccb->ccb_h.target_lun) !=
3534 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3535 cam_dflags = CAM_DEBUG_NONE;
3537 start_ccb->ccb_h.status = CAM_REQ_CMP;
3538 xpt_print_path(cam_dpath);
3539 printf("debugging flags now %x\n", cam_dflags);
3543 start_ccb->ccb_h.status = CAM_REQ_CMP;
3546 #else /* !CAMDEBUG */
3547 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3548 #endif /* CAMDEBUG */
3552 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3553 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3554 start_ccb->ccb_h.status = CAM_REQ_CMP;
3561 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3568 xpt_polled_action(union ccb *start_ccb)
3572 struct cam_sim *sim;
3573 struct cam_devq *devq;
3576 timeout = start_ccb->ccb_h.timeout;
3577 sim = start_ccb->ccb_h.path->bus->sim;
3579 dev = start_ccb->ccb_h.path->device;
3584 * Steal an opening so that no other queued requests
3585 * can get it before us while we simulate interrupts.
3587 dev->ccbq.devq_openings--;
3588 dev->ccbq.dev_openings--;
3590 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3591 && (--timeout > 0)) {
3593 (*(sim->sim_poll))(sim);
3598 dev->ccbq.devq_openings++;
3599 dev->ccbq.dev_openings++;
3602 xpt_action(start_ccb);
3603 while(--timeout > 0) {
3604 (*(sim->sim_poll))(sim);
3607 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3614 * XXX Is it worth adding a sim_timeout entry
3615 * point so we can attempt recovery? If
3616 * this is only used for dumps, I don't think
3619 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3622 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3628 * Schedule a peripheral driver to receive a ccb when it's
3629 * target device has space for more transactions.
3632 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3634 struct cam_ed *device;
3638 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3639 device = perph->path->device;
3641 if (periph_is_queued(perph)) {
3642 /* Simply reorder based on new priority */
3643 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3644 (" change priority to %d\n", new_priority));
3645 if (new_priority < perph->pinfo.priority) {
3646 camq_change_priority(&device->drvq,
3652 /* New entry on the queue */
3653 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3654 (" added periph to queue\n"));
3655 perph->pinfo.priority = new_priority;
3656 perph->pinfo.generation = ++device->drvq.generation;
3657 camq_insert(&device->drvq, &perph->pinfo);
3658 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3662 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3663 (" calling xpt_run_devq\n"));
3664 xpt_run_dev_allocq(perph->path->bus);
3670 * Schedule a device to run on a given queue.
3671 * If the device was inserted as a new entry on the queue,
3672 * return 1 meaning the device queue should be run. If we
3673 * were already queued, implying someone else has already
3674 * started the queue, return 0 so the caller doesn't attempt
3675 * to run the queue. Must be run at either splsoftcam
3676 * (or splcam since that encompases splsoftcam).
3679 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3680 u_int32_t new_priority)
3683 u_int32_t old_priority;
3685 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3687 old_priority = pinfo->priority;
3690 * Are we already queued?
3692 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3693 /* Simply reorder based on new priority */
3694 if (new_priority < old_priority) {
3695 camq_change_priority(queue, pinfo->index,
3697 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3698 ("changed priority to %d\n",
3703 /* New entry on the queue */
3704 if (new_priority < old_priority)
3705 pinfo->priority = new_priority;
3707 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3708 ("Inserting onto queue\n"));
3709 pinfo->generation = ++queue->generation;
3710 camq_insert(queue, pinfo);
3717 xpt_run_dev_allocq(struct cam_eb *bus)
3719 struct cam_devq *devq;
3722 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3723 devq = bus->sim->devq;
3725 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3726 (" qfrozen_cnt == 0x%x, entries == %d, "
3727 "openings == %d, active == %d\n",
3728 devq->alloc_queue.qfrozen_cnt,
3729 devq->alloc_queue.entries,
3730 devq->alloc_openings,
3731 devq->alloc_active));
3734 devq->alloc_queue.qfrozen_cnt++;
3735 while ((devq->alloc_queue.entries > 0)
3736 && (devq->alloc_openings > 0)
3737 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3738 struct cam_ed_qinfo *qinfo;
3739 struct cam_ed *device;
3740 union ccb *work_ccb;
3741 struct cam_periph *drv;
3744 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3746 device = qinfo->device;
3748 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3749 ("running device %p\n", device));
3751 drvq = &device->drvq;
3754 if (drvq->entries <= 0) {
3755 panic("xpt_run_dev_allocq: "
3756 "Device on queue without any work to do");
3759 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3760 devq->alloc_openings--;
3761 devq->alloc_active++;
3762 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3764 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3765 drv->pinfo.priority);
3766 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3767 ("calling periph start\n"));
3768 drv->periph_start(drv, work_ccb);
3771 * Malloc failure in alloc_ccb
3774 * XXX add us to a list to be run from free_ccb
3775 * if we don't have any ccbs active on this
3776 * device queue otherwise we may never get run
3782 /* Raise IPL for possible insertion and test at top of loop */
3785 if (drvq->entries > 0) {
3786 /* We have more work. Attempt to reschedule */
3787 xpt_schedule_dev_allocq(bus, device);
3790 devq->alloc_queue.qfrozen_cnt--;
3795 xpt_run_dev_sendq(struct cam_eb *bus)
3797 struct cam_devq *devq;
3800 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3802 devq = bus->sim->devq;
3805 devq->send_queue.qfrozen_cnt++;
3808 while ((devq->send_queue.entries > 0)
3809 && (devq->send_openings > 0)) {
3810 struct cam_ed_qinfo *qinfo;
3811 struct cam_ed *device;
3812 union ccb *work_ccb;
3813 struct cam_sim *sim;
3817 if (devq->send_queue.qfrozen_cnt > 1) {
3822 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3824 device = qinfo->device;
3827 * If the device has been "frozen", don't attempt
3830 if (device->qfrozen_cnt > 0) {
3835 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3836 ("running device %p\n", device));
3838 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3839 if (work_ccb == NULL) {
3840 printf("device on run queue with no ccbs???");
3845 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3847 if (num_highpower <= 0) {
3849 * We got a high power command, but we
3850 * don't have any available slots. Freeze
3851 * the device queue until we have a slot
3854 device->qfrozen_cnt++;
3855 STAILQ_INSERT_TAIL(&highpowerq,
3863 * Consume a high power slot while
3869 devq->active_dev = device;
3870 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3872 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3875 devq->send_openings--;
3876 devq->send_active++;
3878 if (device->ccbq.queue.entries > 0)
3879 xpt_schedule_dev_sendq(bus, device);
3881 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3883 * The client wants to freeze the queue
3884 * after this CCB is sent.
3887 device->qfrozen_cnt++;
3893 /* In Target mode, the peripheral driver knows best... */
3894 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3895 if ((device->inq_flags & SID_CmdQue) != 0
3896 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3897 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3900 * Clear this in case of a retried CCB that
3901 * failed due to a rejected tag.
3903 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3907 * Device queues can be shared among multiple sim instances
3908 * that reside on different busses. Use the SIM in the queue
3909 * CCB's path, rather than the one in the bus that was passed
3910 * into this function.
3912 sim = work_ccb->ccb_h.path->bus->sim;
3913 (*(sim->sim_action))(sim, work_ccb);
3916 devq->active_dev = NULL;
3918 /* Raise IPL for possible insertion and test at top of loop */
3923 devq->send_queue.qfrozen_cnt--;
3928 * This function merges stuff from the slave ccb into the master ccb, while
3929 * keeping important fields in the master ccb constant.
3932 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3935 * Pull fields that are valid for peripheral drivers to set
3936 * into the master CCB along with the CCB "payload".
3938 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3939 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3940 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3941 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3942 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3943 sizeof(union ccb) - sizeof(struct ccb_hdr));
3947 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3949 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3950 ccb_h->pinfo.priority = priority;
3952 ccb_h->path_id = path->bus->path_id;
3954 ccb_h->target_id = path->target->target_id;
3956 ccb_h->target_id = CAM_TARGET_WILDCARD;
3958 ccb_h->target_lun = path->device->lun_id;
3959 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3961 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3963 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3967 /* Path manipulation functions */
3969 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3970 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3972 struct cam_path *path;
3975 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3978 status = CAM_RESRC_UNAVAIL;
3981 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3982 if (status != CAM_REQ_CMP) {
3983 free(path, M_DEVBUF);
3986 *new_path_ptr = path;
3991 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3992 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3995 struct cam_et *target;
3996 struct cam_ed *device;
4000 status = CAM_REQ_CMP; /* Completed without error */
4001 target = NULL; /* Wildcarded */
4002 device = NULL; /* Wildcarded */
4005 * We will potentially modify the EDT, so block interrupts
4006 * that may attempt to create cam paths.
4009 bus = xpt_find_bus(path_id);
4011 status = CAM_PATH_INVALID;
4013 target = xpt_find_target(bus, target_id);
4014 if (target == NULL) {
4016 struct cam_et *new_target;
4018 new_target = xpt_alloc_target(bus, target_id);
4019 if (new_target == NULL) {
4020 status = CAM_RESRC_UNAVAIL;
4022 target = new_target;
4025 if (target != NULL) {
4026 device = xpt_find_device(target, lun_id);
4027 if (device == NULL) {
4029 struct cam_ed *new_device;
4031 new_device = xpt_alloc_device(bus,
4034 if (new_device == NULL) {
4035 status = CAM_RESRC_UNAVAIL;
4037 device = new_device;
4045 * Only touch the user's data if we are successful.
4047 if (status == CAM_REQ_CMP) {
4048 new_path->periph = perph;
4049 new_path->bus = bus;
4050 new_path->target = target;
4051 new_path->device = device;
4052 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4055 xpt_release_device(bus, target, device);
4057 xpt_release_target(bus, target);
4059 xpt_release_bus(bus);
4065 xpt_release_path(struct cam_path *path)
4067 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4068 if (path->device != NULL) {
4069 xpt_release_device(path->bus, path->target, path->device);
4070 path->device = NULL;
4072 if (path->target != NULL) {
4073 xpt_release_target(path->bus, path->target);
4074 path->target = NULL;
4076 if (path->bus != NULL) {
4077 xpt_release_bus(path->bus);
4083 xpt_free_path(struct cam_path *path)
4085 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4086 xpt_release_path(path);
4087 free(path, M_DEVBUF);
4092 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4093 * in path1, 2 for match with wildcards in path2.
4096 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4100 if (path1->bus != path2->bus) {
4101 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4103 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4108 if (path1->target != path2->target) {
4109 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4112 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4117 if (path1->device != path2->device) {
4118 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4121 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4130 xpt_print_path(struct cam_path *path)
4133 printf("(nopath): ");
4135 if (path->periph != NULL)
4136 printf("(%s%d:", path->periph->periph_name,
4137 path->periph->unit_number);
4139 printf("(noperiph:");
4141 if (path->bus != NULL)
4142 printf("%s%d:%d:", path->bus->sim->sim_name,
4143 path->bus->sim->unit_number,
4144 path->bus->sim->bus_id);
4148 if (path->target != NULL)
4149 printf("%d:", path->target->target_id);
4153 if (path->device != NULL)
4154 printf("%d): ", path->device->lun_id);
4161 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4165 sbuf_new(&sb, str, str_len, 0);
4168 sbuf_printf(&sb, "(nopath): ");
4170 if (path->periph != NULL)
4171 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4172 path->periph->unit_number);
4174 sbuf_printf(&sb, "(noperiph:");
4176 if (path->bus != NULL)
4177 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4178 path->bus->sim->unit_number,
4179 path->bus->sim->bus_id);
4181 sbuf_printf(&sb, "nobus:");
4183 if (path->target != NULL)
4184 sbuf_printf(&sb, "%d:", path->target->target_id);
4186 sbuf_printf(&sb, "X:");
4188 if (path->device != NULL)
4189 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4191 sbuf_printf(&sb, "X): ");
4195 return(sbuf_len(&sb));
4199 xpt_path_path_id(struct cam_path *path)
4201 return(path->bus->path_id);
4205 xpt_path_target_id(struct cam_path *path)
4207 if (path->target != NULL)
4208 return (path->target->target_id);
4210 return (CAM_TARGET_WILDCARD);
4214 xpt_path_lun_id(struct cam_path *path)
4216 if (path->device != NULL)
4217 return (path->device->lun_id);
4219 return (CAM_LUN_WILDCARD);
4223 xpt_path_sim(struct cam_path *path)
4225 return (path->bus->sim);
4229 xpt_path_periph(struct cam_path *path)
4231 return (path->periph);
4235 * Release a CAM control block for the caller. Remit the cost of the structure
4236 * to the device referenced by the path. If the this device had no 'credits'
4237 * and peripheral drivers have registered async callbacks for this notification
4241 xpt_release_ccb(union ccb *free_ccb)
4244 struct cam_path *path;
4245 struct cam_ed *device;
4248 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4249 path = free_ccb->ccb_h.path;
4250 device = path->device;
4253 cam_ccbq_release_opening(&device->ccbq);
4254 if (xpt_ccb_count > xpt_max_ccbs) {
4255 xpt_free_ccb(free_ccb);
4258 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4260 bus->sim->devq->alloc_openings++;
4261 bus->sim->devq->alloc_active--;
4262 /* XXX Turn this into an inline function - xpt_run_device?? */
4263 if ((device_is_alloc_queued(device) == 0)
4264 && (device->drvq.entries > 0)) {
4265 xpt_schedule_dev_allocq(bus, device);
4268 if (dev_allocq_is_runnable(bus->sim->devq))
4269 xpt_run_dev_allocq(bus);
4272 /* Functions accessed by SIM drivers */
4275 * A sim structure, listing the SIM entry points and instance
4276 * identification info is passed to xpt_bus_register to hook the SIM
4277 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4278 * for this new bus and places it in the array of busses and assigns
4279 * it a path_id. The path_id may be influenced by "hard wiring"
4280 * information specified by the user. Once interrupt services are
4281 * availible, the bus will be probed.
4284 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4286 struct cam_eb *new_bus;
4287 struct cam_eb *old_bus;
4288 struct ccb_pathinq cpi;
4292 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4293 M_DEVBUF, M_NOWAIT);
4294 if (new_bus == NULL) {
4295 /* Couldn't satisfy request */
4296 return (CAM_RESRC_UNAVAIL);
4299 if (strcmp(sim->sim_name, "xpt") != 0) {
4302 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4305 TAILQ_INIT(&new_bus->et_entries);
4306 new_bus->path_id = sim->path_id;
4308 timevalclear(&new_bus->last_reset);
4310 new_bus->refcount = 1; /* Held until a bus_deregister event */
4311 new_bus->generation = 0;
4313 old_bus = TAILQ_FIRST(&xpt_busses);
4314 while (old_bus != NULL
4315 && old_bus->path_id < new_bus->path_id)
4316 old_bus = TAILQ_NEXT(old_bus, links);
4317 if (old_bus != NULL)
4318 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4320 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4324 /* Notify interested parties */
4325 if (sim->path_id != CAM_XPT_PATH_ID) {
4326 struct cam_path path;
4328 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4329 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4330 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4331 cpi.ccb_h.func_code = XPT_PATH_INQ;
4332 xpt_action((union ccb *)&cpi);
4333 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4334 xpt_release_path(&path);
4336 return (CAM_SUCCESS);
4340 xpt_bus_deregister(path_id_t pathid)
4342 struct cam_path bus_path;
4345 status = xpt_compile_path(&bus_path, NULL, pathid,
4346 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4347 if (status != CAM_REQ_CMP)
4350 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4351 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4353 /* Release the reference count held while registered. */
4354 xpt_release_bus(bus_path.bus);
4355 xpt_release_path(&bus_path);
4357 return (CAM_REQ_CMP);
4361 xptnextfreepathid(void)
4368 bus = TAILQ_FIRST(&xpt_busses);
4370 /* Find an unoccupied pathid */
4372 && bus->path_id <= pathid) {
4373 if (bus->path_id == pathid)
4375 bus = TAILQ_NEXT(bus, links);
4379 * Ensure that this pathid is not reserved for
4380 * a bus that may be registered in the future.
4382 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4384 /* Start the search over */
4391 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4398 pathid = CAM_XPT_PATH_ID;
4399 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4401 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4402 if (strcmp(dname, "scbus")) {
4403 /* Avoid a bit of foot shooting. */
4406 if (dunit < 0) /* unwired?! */
4408 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4409 if (sim_bus == val) {
4413 } else if (sim_bus == 0) {
4414 /* Unspecified matches bus 0 */
4418 printf("Ambiguous scbus configuration for %s%d "
4419 "bus %d, cannot wire down. The kernel "
4420 "config entry for scbus%d should "
4421 "specify a controller bus.\n"
4422 "Scbus will be assigned dynamically.\n",
4423 sim_name, sim_unit, sim_bus, dunit);
4428 if (pathid == CAM_XPT_PATH_ID)
4429 pathid = xptnextfreepathid();
4434 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4437 struct cam_et *target, *next_target;
4438 struct cam_ed *device, *next_device;
4441 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4444 * Most async events come from a CAM interrupt context. In
4445 * a few cases, the error recovery code at the peripheral layer,
4446 * which may run from our SWI or a process context, may signal
4447 * deferred events with a call to xpt_async. Ensure async
4448 * notifications are serialized by blocking cam interrupts.
4454 if (async_code == AC_BUS_RESET) {
4458 /* Update our notion of when the last reset occurred */
4459 microtime(&bus->last_reset);
4463 for (target = TAILQ_FIRST(&bus->et_entries);
4465 target = next_target) {
4467 next_target = TAILQ_NEXT(target, links);
4469 if (path->target != target
4470 && path->target->target_id != CAM_TARGET_WILDCARD
4471 && target->target_id != CAM_TARGET_WILDCARD)
4474 if (async_code == AC_SENT_BDR) {
4477 /* Update our notion of when the last reset occurred */
4479 microtime(&path->target->last_reset);
4483 for (device = TAILQ_FIRST(&target->ed_entries);
4485 device = next_device) {
4487 next_device = TAILQ_NEXT(device, links);
4489 if (path->device != device
4490 && path->device->lun_id != CAM_LUN_WILDCARD
4491 && device->lun_id != CAM_LUN_WILDCARD)
4494 xpt_dev_async(async_code, bus, target,
4497 xpt_async_bcast(&device->asyncs, async_code,
4503 * If this wasn't a fully wildcarded async, tell all
4504 * clients that want all async events.
4506 if (bus != xpt_periph->path->bus)
4507 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4513 xpt_async_bcast(struct async_list *async_head,
4514 u_int32_t async_code,
4515 struct cam_path *path, void *async_arg)
4517 struct async_node *cur_entry;
4519 cur_entry = SLIST_FIRST(async_head);
4520 while (cur_entry != NULL) {
4521 struct async_node *next_entry;
4523 * Grab the next list entry before we call the current
4524 * entry's callback. This is because the callback function
4525 * can delete its async callback entry.
4527 next_entry = SLIST_NEXT(cur_entry, links);
4528 if ((cur_entry->event_enable & async_code) != 0)
4529 cur_entry->callback(cur_entry->callback_arg,
4532 cur_entry = next_entry;
4537 * Handle any per-device event notifications that require action by the XPT.
4540 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4541 struct cam_ed *device, void *async_arg)
4544 struct cam_path newpath;
4547 * We only need to handle events for real devices.
4549 if (target->target_id == CAM_TARGET_WILDCARD
4550 || device->lun_id == CAM_LUN_WILDCARD)
4554 * We need our own path with wildcards expanded to
4555 * handle certain types of events.
4557 if ((async_code == AC_SENT_BDR)
4558 || (async_code == AC_BUS_RESET)
4559 || (async_code == AC_INQ_CHANGED))
4560 status = xpt_compile_path(&newpath, NULL,
4565 status = CAM_REQ_CMP_ERR;
4567 if (status == CAM_REQ_CMP) {
4570 * Allow transfer negotiation to occur in a
4571 * tag free environment.
4573 if (async_code == AC_SENT_BDR
4574 || async_code == AC_BUS_RESET)
4575 xpt_toggle_tags(&newpath);
4577 if (async_code == AC_INQ_CHANGED) {
4579 * We've sent a start unit command, or
4580 * something similar to a device that
4581 * may have caused its inquiry data to
4582 * change. So we re-scan the device to
4583 * refresh the inquiry data for it.
4585 xpt_scan_lun(newpath.periph, &newpath,
4586 CAM_EXPECT_INQ_CHANGE, NULL);
4588 xpt_release_path(&newpath);
4589 } else if (async_code == AC_LOST_DEVICE) {
4590 device->flags |= CAM_DEV_UNCONFIGURED;
4591 } else if (async_code == AC_TRANSFER_NEG) {
4592 struct ccb_trans_settings *settings;
4594 settings = (struct ccb_trans_settings *)async_arg;
4595 xpt_set_transfer_settings(settings, device,
4596 /*async_update*/TRUE);
4601 xpt_freeze_devq(struct cam_path *path, u_int count)
4604 struct ccb_hdr *ccbh;
4607 path->device->qfrozen_cnt += count;
4610 * Mark the last CCB in the queue as needing
4611 * to be requeued if the driver hasn't
4612 * changed it's state yet. This fixes a race
4613 * where a ccb is just about to be queued to
4614 * a controller driver when it's interrupt routine
4615 * freezes the queue. To completly close the
4616 * hole, controller drives must check to see
4617 * if a ccb's status is still CAM_REQ_INPROG
4618 * under spl protection just before they queue
4619 * the CCB. See ahc_action/ahc_freeze_devq for
4622 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4623 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4624 ccbh->status = CAM_REQUEUE_REQ;
4626 return (path->device->qfrozen_cnt);
4630 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4632 sim->devq->send_queue.qfrozen_cnt += count;
4633 if (sim->devq->active_dev != NULL) {
4634 struct ccb_hdr *ccbh;
4636 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4638 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4639 ccbh->status = CAM_REQUEUE_REQ;
4641 return (sim->devq->send_queue.qfrozen_cnt);
4645 xpt_release_devq_timeout(void *arg)
4647 struct cam_ed *device;
4649 device = (struct cam_ed *)arg;
4651 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4655 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4657 xpt_release_devq_device(path->device, count, run_queue);
4661 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4669 if (dev->qfrozen_cnt > 0) {
4671 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4672 dev->qfrozen_cnt -= count;
4673 if (dev->qfrozen_cnt == 0) {
4676 * No longer need to wait for a successful
4677 * command completion.
4679 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4682 * Remove any timeouts that might be scheduled
4683 * to release this queue.
4685 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4686 untimeout(xpt_release_devq_timeout, dev,
4688 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4692 * Now that we are unfrozen schedule the
4693 * device so any pending transactions are
4696 if ((dev->ccbq.queue.entries > 0)
4697 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4698 && (run_queue != 0)) {
4705 xpt_run_dev_sendq(dev->target->bus);
4710 xpt_release_simq(struct cam_sim *sim, int run_queue)
4715 sendq = &(sim->devq->send_queue);
4717 if (sendq->qfrozen_cnt > 0) {
4719 sendq->qfrozen_cnt--;
4720 if (sendq->qfrozen_cnt == 0) {
4724 * If there is a timeout scheduled to release this
4725 * sim queue, remove it. The queue frozen count is
4728 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4729 untimeout(xpt_release_simq_timeout, sim,
4731 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4733 bus = xpt_find_bus(sim->path_id);
4738 * Now that we are unfrozen run the send queue.
4740 xpt_run_dev_sendq(bus);
4742 xpt_release_bus(bus);
4750 xpt_release_simq_timeout(void *arg)
4752 struct cam_sim *sim;
4754 sim = (struct cam_sim *)arg;
4755 xpt_release_simq(sim, /* run_queue */ TRUE);
4759 xpt_done(union ccb *done_ccb)
4765 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4766 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4768 * Queue up the request for handling by our SWI handler
4769 * any of the "non-immediate" type of ccbs.
4771 switch (done_ccb->ccb_h.path->periph->type) {
4772 case CAM_PERIPH_BIO:
4773 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4775 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4776 swi_sched(cambio_ih, 0);
4778 case CAM_PERIPH_NET:
4779 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4781 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4782 swi_sched(camnet_ih, 0);
4794 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4799 xpt_free_ccb(union ccb *free_ccb)
4801 free(free_ccb, M_DEVBUF);
4806 /* Private XPT functions */
4809 * Get a CAM control block for the caller. Charge the structure to the device
4810 * referenced by the path. If the this device has no 'credits' then the
4811 * device already has the maximum number of outstanding operations under way
4812 * and we return NULL. If we don't have sufficient resources to allocate more
4813 * ccbs, we also return NULL.
4816 xpt_get_ccb(struct cam_ed *device)
4822 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4823 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4824 if (new_ccb == NULL) {
4828 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4829 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4833 cam_ccbq_take_opening(&device->ccbq);
4834 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4840 xpt_release_bus(struct cam_eb *bus)
4845 if ((--bus->refcount == 0)
4846 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4847 TAILQ_REMOVE(&xpt_busses, bus, links);
4850 free(bus, M_DEVBUF);
4855 static struct cam_et *
4856 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4858 struct cam_et *target;
4860 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4861 if (target != NULL) {
4862 struct cam_et *cur_target;
4864 TAILQ_INIT(&target->ed_entries);
4866 target->target_id = target_id;
4867 target->refcount = 1;
4868 target->generation = 0;
4869 timevalclear(&target->last_reset);
4871 * Hold a reference to our parent bus so it
4872 * will not go away before we do.
4876 /* Insertion sort into our bus's target list */
4877 cur_target = TAILQ_FIRST(&bus->et_entries);
4878 while (cur_target != NULL && cur_target->target_id < target_id)
4879 cur_target = TAILQ_NEXT(cur_target, links);
4881 if (cur_target != NULL) {
4882 TAILQ_INSERT_BEFORE(cur_target, target, links);
4884 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4892 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4897 if ((--target->refcount == 0)
4898 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4899 TAILQ_REMOVE(&bus->et_entries, target, links);
4902 free(target, M_DEVBUF);
4903 xpt_release_bus(bus);
4908 static struct cam_ed *
4909 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4911 #ifdef CAM_NEW_TRAN_CODE
4912 struct cam_path path;
4913 #endif /* CAM_NEW_TRAN_CODE */
4914 struct cam_ed *device;
4915 struct cam_devq *devq;
4918 /* Make space for us in the device queue on our bus */
4919 devq = bus->sim->devq;
4920 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4922 if (status != CAM_REQ_CMP) {
4925 device = (struct cam_ed *)malloc(sizeof(*device),
4926 M_DEVBUF, M_NOWAIT);
4929 if (device != NULL) {
4930 struct cam_ed *cur_device;
4932 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4933 device->alloc_ccb_entry.device = device;
4934 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4935 device->send_ccb_entry.device = device;
4936 device->target = target;
4937 device->lun_id = lun_id;
4938 /* Initialize our queues */
4939 if (camq_init(&device->drvq, 0) != 0) {
4940 free(device, M_DEVBUF);
4943 if (cam_ccbq_init(&device->ccbq,
4944 bus->sim->max_dev_openings) != 0) {
4945 camq_fini(&device->drvq);
4946 free(device, M_DEVBUF);
4949 SLIST_INIT(&device->asyncs);
4950 SLIST_INIT(&device->periphs);
4951 device->generation = 0;
4952 device->owner = NULL;
4954 * Take the default quirk entry until we have inquiry
4955 * data and can determine a better quirk to use.
4957 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4958 bzero(&device->inq_data, sizeof(device->inq_data));
4959 device->inq_flags = 0;
4960 device->queue_flags = 0;
4961 device->serial_num = NULL;
4962 device->serial_num_len = 0;
4963 device->qfrozen_cnt = 0;
4964 device->flags = CAM_DEV_UNCONFIGURED;
4965 device->tag_delay_count = 0;
4966 device->refcount = 1;
4967 callout_handle_init(&device->c_handle);
4970 * Hold a reference to our parent target so it
4971 * will not go away before we do.
4976 * XXX should be limited by number of CCBs this bus can
4979 xpt_max_ccbs += device->ccbq.devq_openings;
4980 /* Insertion sort into our target's device list */
4981 cur_device = TAILQ_FIRST(&target->ed_entries);
4982 while (cur_device != NULL && cur_device->lun_id < lun_id)
4983 cur_device = TAILQ_NEXT(cur_device, links);
4984 if (cur_device != NULL) {
4985 TAILQ_INSERT_BEFORE(cur_device, device, links);
4987 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4989 target->generation++;
4990 #ifdef CAM_NEW_TRAN_CODE
4991 if (lun_id != CAM_LUN_WILDCARD) {
4992 xpt_compile_path(&path,
4997 xpt_devise_transport(&path);
4998 xpt_release_path(&path);
5000 #endif /* CAM_NEW_TRAN_CODE */
5006 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5007 struct cam_ed *device)
5012 if ((--device->refcount == 0)
5013 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5014 struct cam_devq *devq;
5016 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5017 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5018 panic("Removing device while still queued for ccbs");
5020 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5021 untimeout(xpt_release_devq_timeout, device,
5024 TAILQ_REMOVE(&target->ed_entries, device,links);
5025 target->generation++;
5026 xpt_max_ccbs -= device->ccbq.devq_openings;
5027 /* Release our slot in the devq */
5028 devq = bus->sim->devq;
5029 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5031 free(device, M_DEVBUF);
5032 xpt_release_target(bus, target);
5038 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5048 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5049 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5050 if (result == CAM_REQ_CMP && (diff < 0)) {
5051 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5053 /* Adjust the global limit */
5054 xpt_max_ccbs += diff;
5059 static struct cam_eb *
5060 xpt_find_bus(path_id_t path_id)
5064 for (bus = TAILQ_FIRST(&xpt_busses);
5066 bus = TAILQ_NEXT(bus, links)) {
5067 if (bus->path_id == path_id) {
5075 static struct cam_et *
5076 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5078 struct cam_et *target;
5080 for (target = TAILQ_FIRST(&bus->et_entries);
5082 target = TAILQ_NEXT(target, links)) {
5083 if (target->target_id == target_id) {
5091 static struct cam_ed *
5092 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5094 struct cam_ed *device;
5096 for (device = TAILQ_FIRST(&target->ed_entries);
5098 device = TAILQ_NEXT(device, links)) {
5099 if (device->lun_id == lun_id) {
5108 union ccb *request_ccb;
5109 struct ccb_pathinq *cpi;
5111 } xpt_scan_bus_info;
5114 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5115 * As the scan progresses, xpt_scan_bus is used as the
5116 * callback on completion function.
5119 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5121 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5122 ("xpt_scan_bus\n"));
5123 switch (request_ccb->ccb_h.func_code) {
5126 xpt_scan_bus_info *scan_info;
5127 union ccb *work_ccb;
5128 struct cam_path *path;
5133 /* Find out the characteristics of the bus */
5134 work_ccb = xpt_alloc_ccb();
5135 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5136 request_ccb->ccb_h.pinfo.priority);
5137 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5138 xpt_action(work_ccb);
5139 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5140 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5141 xpt_free_ccb(work_ccb);
5142 xpt_done(request_ccb);
5146 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5148 * Can't scan the bus on an adapter that
5149 * cannot perform the initiator role.
5151 request_ccb->ccb_h.status = CAM_REQ_CMP;
5152 xpt_free_ccb(work_ccb);
5153 xpt_done(request_ccb);
5157 /* Save some state for use while we probe for devices */
5158 scan_info = (xpt_scan_bus_info *)
5159 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5160 scan_info->request_ccb = request_ccb;
5161 scan_info->cpi = &work_ccb->cpi;
5163 /* Cache on our stack so we can work asynchronously */
5164 max_target = scan_info->cpi->max_target;
5165 initiator_id = scan_info->cpi->initiator_id;
5168 * Don't count the initiator if the
5169 * initiator is addressable.
5171 scan_info->pending_count = max_target + 1;
5172 if (initiator_id <= max_target)
5173 scan_info->pending_count--;
5175 for (i = 0; i <= max_target; i++) {
5177 if (i == initiator_id)
5180 status = xpt_create_path(&path, xpt_periph,
5181 request_ccb->ccb_h.path_id,
5183 if (status != CAM_REQ_CMP) {
5184 printf("xpt_scan_bus: xpt_create_path failed"
5185 " with status %#x, bus scan halted\n",
5189 work_ccb = xpt_alloc_ccb();
5190 xpt_setup_ccb(&work_ccb->ccb_h, path,
5191 request_ccb->ccb_h.pinfo.priority);
5192 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5193 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5194 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5195 work_ccb->crcn.flags = request_ccb->crcn.flags;
5196 xpt_action(work_ccb);
5202 xpt_scan_bus_info *scan_info;
5204 target_id_t target_id;
5207 /* Reuse the same CCB to query if a device was really found */
5208 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5209 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5210 request_ccb->ccb_h.pinfo.priority);
5211 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5213 path_id = request_ccb->ccb_h.path_id;
5214 target_id = request_ccb->ccb_h.target_id;
5215 lun_id = request_ccb->ccb_h.target_lun;
5216 xpt_action(request_ccb);
5218 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5219 struct cam_ed *device;
5220 struct cam_et *target;
5224 * If we already probed lun 0 successfully, or
5225 * we have additional configured luns on this
5226 * target that might have "gone away", go onto
5229 target = request_ccb->ccb_h.path->target;
5231 * We may touch devices that we don't
5232 * hold references too, so ensure they
5233 * don't disappear out from under us.
5234 * The target above is referenced by the
5235 * path in the request ccb.
5239 device = TAILQ_FIRST(&target->ed_entries);
5240 if (device != NULL) {
5241 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5242 if (device->lun_id == 0)
5243 device = TAILQ_NEXT(device, links);
5246 if ((lun_id != 0) || (device != NULL)) {
5247 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5251 struct cam_ed *device;
5253 device = request_ccb->ccb_h.path->device;
5255 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5256 /* Try the next lun */
5257 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5258 (device->quirk->quirks & CAM_QUIRK_HILUNS))
5263 xpt_free_path(request_ccb->ccb_h.path);
5266 if ((lun_id == request_ccb->ccb_h.target_lun)
5267 || lun_id > scan_info->cpi->max_lun) {
5270 xpt_free_ccb(request_ccb);
5271 scan_info->pending_count--;
5272 if (scan_info->pending_count == 0) {
5273 xpt_free_ccb((union ccb *)scan_info->cpi);
5274 request_ccb = scan_info->request_ccb;
5275 free(scan_info, M_TEMP);
5276 request_ccb->ccb_h.status = CAM_REQ_CMP;
5277 xpt_done(request_ccb);
5280 /* Try the next device */
5281 struct cam_path *path;
5284 path = request_ccb->ccb_h.path;
5285 status = xpt_create_path(&path, xpt_periph,
5286 path_id, target_id, lun_id);
5287 if (status != CAM_REQ_CMP) {
5288 printf("xpt_scan_bus: xpt_create_path failed "
5289 "with status %#x, halting LUN scan\n",
5291 xpt_free_ccb(request_ccb);
5292 scan_info->pending_count--;
5293 if (scan_info->pending_count == 0) {
5295 (union ccb *)scan_info->cpi);
5296 request_ccb = scan_info->request_ccb;
5297 free(scan_info, M_TEMP);
5298 request_ccb->ccb_h.status = CAM_REQ_CMP;
5299 xpt_done(request_ccb);
5303 xpt_setup_ccb(&request_ccb->ccb_h, path,
5304 request_ccb->ccb_h.pinfo.priority);
5305 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5306 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5307 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5308 request_ccb->crcn.flags =
5309 scan_info->request_ccb->crcn.flags;
5310 xpt_action(request_ccb);
5325 PROBE_TUR_FOR_NEGOTIATION
5329 PROBE_INQUIRY_CKSUM = 0x01,
5330 PROBE_SERIAL_CKSUM = 0x02,
5331 PROBE_NO_ANNOUNCE = 0x04
5335 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5336 probe_action action;
5337 union ccb saved_ccb;
5340 u_int8_t digest[16];
5344 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5345 cam_flags flags, union ccb *request_ccb)
5347 struct ccb_pathinq cpi;
5349 struct cam_path *new_path;
5350 struct cam_periph *old_periph;
5353 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5354 ("xpt_scan_lun\n"));
5356 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5357 cpi.ccb_h.func_code = XPT_PATH_INQ;
5358 xpt_action((union ccb *)&cpi);
5360 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5361 if (request_ccb != NULL) {
5362 request_ccb->ccb_h.status = cpi.ccb_h.status;
5363 xpt_done(request_ccb);
5368 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5370 * Can't scan the bus on an adapter that
5371 * cannot perform the initiator role.
5373 if (request_ccb != NULL) {
5374 request_ccb->ccb_h.status = CAM_REQ_CMP;
5375 xpt_done(request_ccb);
5380 if (request_ccb == NULL) {
5381 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5382 if (request_ccb == NULL) {
5383 xpt_print_path(path);
5384 printf("xpt_scan_lun: can't allocate CCB, can't "
5388 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5389 if (new_path == NULL) {
5390 xpt_print_path(path);
5391 printf("xpt_scan_lun: can't allocate path, can't "
5393 free(request_ccb, M_TEMP);
5396 status = xpt_compile_path(new_path, xpt_periph,
5398 path->target->target_id,
5399 path->device->lun_id);
5401 if (status != CAM_REQ_CMP) {
5402 xpt_print_path(path);
5403 printf("xpt_scan_lun: can't compile path, can't "
5405 free(request_ccb, M_TEMP);
5406 free(new_path, M_TEMP);
5409 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5410 request_ccb->ccb_h.cbfcnp = xptscandone;
5411 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5412 request_ccb->crcn.flags = flags;
5416 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5419 softc = (probe_softc *)old_periph->softc;
5420 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5423 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5424 probestart, "probe",
5426 request_ccb->ccb_h.path, NULL, 0,
5429 if (status != CAM_REQ_CMP) {
5430 xpt_print_path(path);
5431 printf("xpt_scan_lun: cam_alloc_periph returned an "
5432 "error, can't continue probe\n");
5433 request_ccb->ccb_h.status = status;
5434 xpt_done(request_ccb);
5441 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5443 xpt_release_path(done_ccb->ccb_h.path);
5444 free(done_ccb->ccb_h.path, M_TEMP);
5445 free(done_ccb, M_TEMP);
5449 proberegister(struct cam_periph *periph, void *arg)
5451 union ccb *request_ccb; /* CCB representing the probe request */
5454 request_ccb = (union ccb *)arg;
5455 if (periph == NULL) {
5456 printf("proberegister: periph was NULL!!\n");
5457 return(CAM_REQ_CMP_ERR);
5460 if (request_ccb == NULL) {
5461 printf("proberegister: no probe CCB, "
5462 "can't register device\n");
5463 return(CAM_REQ_CMP_ERR);
5466 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5468 if (softc == NULL) {
5469 printf("proberegister: Unable to probe new device. "
5470 "Unable to allocate softc\n");
5471 return(CAM_REQ_CMP_ERR);
5473 TAILQ_INIT(&softc->request_ccbs);
5474 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5477 periph->softc = softc;
5478 cam_periph_acquire(periph);
5480 * Ensure we've waited at least a bus settle
5481 * delay before attempting to probe the device.
5482 * For HBAs that don't do bus resets, this won't make a difference.
5484 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5486 probeschedule(periph);
5487 return(CAM_REQ_CMP);
5491 probeschedule(struct cam_periph *periph)
5493 struct ccb_pathinq cpi;
5497 softc = (probe_softc *)periph->softc;
5498 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5500 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5501 cpi.ccb_h.func_code = XPT_PATH_INQ;
5502 xpt_action((union ccb *)&cpi);
5505 * If a device has gone away and another device, or the same one,
5506 * is back in the same place, it should have a unit attention
5507 * condition pending. It will not report the unit attention in
5508 * response to an inquiry, which may leave invalid transfer
5509 * negotiations in effect. The TUR will reveal the unit attention
5510 * condition. Only send the TUR for lun 0, since some devices
5511 * will get confused by commands other than inquiry to non-existent
5512 * luns. If you think a device has gone away start your scan from
5513 * lun 0. This will insure that any bogus transfer settings are
5516 * If we haven't seen the device before and the controller supports
5517 * some kind of transfer negotiation, negotiate with the first
5518 * sent command if no bus reset was performed at startup. This
5519 * ensures that the device is not confused by transfer negotiation
5520 * settings left over by loader or BIOS action.
5522 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5523 && (ccb->ccb_h.target_lun == 0)) {
5524 softc->action = PROBE_TUR;
5525 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5526 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5527 proberequestdefaultnegotiation(periph);
5528 softc->action = PROBE_INQUIRY;
5530 softc->action = PROBE_INQUIRY;
5533 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5534 softc->flags |= PROBE_NO_ANNOUNCE;
5536 softc->flags &= ~PROBE_NO_ANNOUNCE;
5538 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5542 probestart(struct cam_periph *periph, union ccb *start_ccb)
5544 /* Probe the device that our peripheral driver points to */
5545 struct ccb_scsiio *csio;
5548 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5550 softc = (probe_softc *)periph->softc;
5551 csio = &start_ccb->csio;
5553 switch (softc->action) {
5555 case PROBE_TUR_FOR_NEGOTIATION:
5557 scsi_test_unit_ready(csio,
5566 case PROBE_FULL_INQUIRY:
5569 struct scsi_inquiry_data *inq_buf;
5571 inq_buf = &periph->path->device->inq_data;
5573 * If the device is currently configured, we calculate an
5574 * MD5 checksum of the inquiry data, and if the serial number
5575 * length is greater than 0, add the serial number data
5576 * into the checksum as well. Once the inquiry and the
5577 * serial number check finish, we attempt to figure out
5578 * whether we still have the same device.
5580 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5582 MD5Init(&softc->context);
5583 MD5Update(&softc->context, (unsigned char *)inq_buf,
5584 sizeof(struct scsi_inquiry_data));
5585 softc->flags |= PROBE_INQUIRY_CKSUM;
5586 if (periph->path->device->serial_num_len > 0) {
5587 MD5Update(&softc->context,
5588 periph->path->device->serial_num,
5589 periph->path->device->serial_num_len);
5590 softc->flags |= PROBE_SERIAL_CKSUM;
5592 MD5Final(softc->digest, &softc->context);
5595 if (softc->action == PROBE_INQUIRY)
5596 inquiry_len = SHORT_INQUIRY_LENGTH;
5598 inquiry_len = inq_buf->additional_length + 4;
5604 (u_int8_t *)inq_buf,
5609 /*timeout*/60 * 1000);
5612 case PROBE_MODE_SENSE:
5617 mode_buf_len = sizeof(struct scsi_mode_header_6)
5618 + sizeof(struct scsi_mode_blk_desc)
5619 + sizeof(struct scsi_control_page);
5620 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5621 if (mode_buf != NULL) {
5622 scsi_mode_sense(csio,
5627 SMS_PAGE_CTRL_CURRENT,
5628 SMS_CONTROL_MODE_PAGE,
5635 xpt_print_path(periph->path);
5636 printf("Unable to mode sense control page - malloc failure\n");
5637 softc->action = PROBE_SERIAL_NUM;
5640 case PROBE_SERIAL_NUM:
5642 struct scsi_vpd_unit_serial_number *serial_buf;
5643 struct cam_ed* device;
5646 device = periph->path->device;
5647 device->serial_num = NULL;
5648 device->serial_num_len = 0;
5650 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5651 serial_buf = (struct scsi_vpd_unit_serial_number *)
5652 malloc(sizeof(*serial_buf), M_TEMP,
5655 if (serial_buf != NULL) {
5660 (u_int8_t *)serial_buf,
5661 sizeof(*serial_buf),
5663 SVPD_UNIT_SERIAL_NUMBER,
5665 /*timeout*/60 * 1000);
5669 * We'll have to do without, let our probedone
5670 * routine finish up for us.
5672 start_ccb->csio.data_ptr = NULL;
5673 probedone(periph, start_ccb);
5677 xpt_action(start_ccb);
5681 proberequestdefaultnegotiation(struct cam_periph *periph)
5683 struct ccb_trans_settings cts;
5685 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5686 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5687 #ifdef CAM_NEW_TRAN_CODE
5688 cts.type = CTS_TYPE_USER_SETTINGS;
5689 #else /* CAM_NEW_TRAN_CODE */
5690 cts.flags = CCB_TRANS_USER_SETTINGS;
5691 #endif /* CAM_NEW_TRAN_CODE */
5692 xpt_action((union ccb *)&cts);
5693 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5694 #ifdef CAM_NEW_TRAN_CODE
5695 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5696 #else /* CAM_NEW_TRAN_CODE */
5697 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5698 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5699 #endif /* CAM_NEW_TRAN_CODE */
5700 xpt_action((union ccb *)&cts);
5704 probedone(struct cam_periph *periph, union ccb *done_ccb)
5707 struct cam_path *path;
5710 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5712 softc = (probe_softc *)periph->softc;
5713 path = done_ccb->ccb_h.path;
5714 priority = done_ccb->ccb_h.pinfo.priority;
5716 switch (softc->action) {
5719 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5721 if (cam_periph_error(done_ccb, 0,
5722 SF_NO_PRINT, NULL) == ERESTART)
5724 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5725 /* Don't wedge the queue */
5726 xpt_release_devq(done_ccb->ccb_h.path,
5730 softc->action = PROBE_INQUIRY;
5731 xpt_release_ccb(done_ccb);
5732 xpt_schedule(periph, priority);
5736 case PROBE_FULL_INQUIRY:
5738 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5739 struct scsi_inquiry_data *inq_buf;
5740 u_int8_t periph_qual;
5742 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5743 inq_buf = &path->device->inq_data;
5745 periph_qual = SID_QUAL(inq_buf);
5747 switch(periph_qual) {
5748 case SID_QUAL_LU_CONNECTED:
5753 * We conservatively request only
5754 * SHORT_INQUIRY_LEN bytes of inquiry
5755 * information during our first try
5756 * at sending an INQUIRY. If the device
5757 * has more information to give,
5758 * perform a second request specifying
5759 * the amount of information the device
5760 * is willing to give.
5762 alen = inq_buf->additional_length;
5763 if (softc->action == PROBE_INQUIRY
5764 && alen > (SHORT_INQUIRY_LENGTH - 4)) {
5765 softc->action = PROBE_FULL_INQUIRY;
5766 xpt_release_ccb(done_ccb);
5767 xpt_schedule(periph, priority);
5771 xpt_find_quirk(path->device);
5773 #ifdef CAM_NEW_TRAN_CODE
5774 xpt_devise_transport(path);
5775 #endif /* CAM_NEW_TRAN_CODE */
5776 if ((inq_buf->flags & SID_CmdQue) != 0)
5777 softc->action = PROBE_MODE_SENSE;
5779 softc->action = PROBE_SERIAL_NUM;
5781 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5783 xpt_release_ccb(done_ccb);
5784 xpt_schedule(periph, priority);
5790 } else if (cam_periph_error(done_ccb, 0,
5791 done_ccb->ccb_h.target_lun > 0
5792 ? SF_RETRY_UA|SF_QUIET_IR
5794 &softc->saved_ccb) == ERESTART) {
5796 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5797 /* Don't wedge the queue */
5798 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5802 * If we get to this point, we got an error status back
5803 * from the inquiry and the error status doesn't require
5804 * automatically retrying the command. Therefore, the
5805 * inquiry failed. If we had inquiry information before
5806 * for this device, but this latest inquiry command failed,
5807 * the device has probably gone away. If this device isn't
5808 * already marked unconfigured, notify the peripheral
5809 * drivers that this device is no more.
5811 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5812 /* Send the async notification. */
5813 xpt_async(AC_LOST_DEVICE, path, NULL);
5815 xpt_release_ccb(done_ccb);
5818 case PROBE_MODE_SENSE:
5820 struct ccb_scsiio *csio;
5821 struct scsi_mode_header_6 *mode_hdr;
5823 csio = &done_ccb->csio;
5824 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5825 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5826 struct scsi_control_page *page;
5829 offset = ((u_int8_t *)&mode_hdr[1])
5830 + mode_hdr->blk_desc_len;
5831 page = (struct scsi_control_page *)offset;
5832 path->device->queue_flags = page->queue_flags;
5833 } else if (cam_periph_error(done_ccb, 0,
5834 SF_RETRY_UA|SF_NO_PRINT,
5835 &softc->saved_ccb) == ERESTART) {
5837 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5838 /* Don't wedge the queue */
5839 xpt_release_devq(done_ccb->ccb_h.path,
5840 /*count*/1, /*run_queue*/TRUE);
5842 xpt_release_ccb(done_ccb);
5843 free(mode_hdr, M_TEMP);
5844 softc->action = PROBE_SERIAL_NUM;
5845 xpt_schedule(periph, priority);
5848 case PROBE_SERIAL_NUM:
5850 struct ccb_scsiio *csio;
5851 struct scsi_vpd_unit_serial_number *serial_buf;
5858 csio = &done_ccb->csio;
5859 priority = done_ccb->ccb_h.pinfo.priority;
5861 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5863 /* Clean up from previous instance of this device */
5864 if (path->device->serial_num != NULL) {
5865 free(path->device->serial_num, M_DEVBUF);
5866 path->device->serial_num = NULL;
5867 path->device->serial_num_len = 0;
5870 if (serial_buf == NULL) {
5872 * Don't process the command as it was never sent
5874 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5875 && (serial_buf->length > 0)) {
5878 path->device->serial_num =
5879 (u_int8_t *)malloc((serial_buf->length + 1),
5880 M_DEVBUF, M_NOWAIT);
5881 if (path->device->serial_num != NULL) {
5882 bcopy(serial_buf->serial_num,
5883 path->device->serial_num,
5884 serial_buf->length);
5885 path->device->serial_num_len =
5887 path->device->serial_num[serial_buf->length]
5890 } else if (cam_periph_error(done_ccb, 0,
5891 SF_RETRY_UA|SF_NO_PRINT,
5892 &softc->saved_ccb) == ERESTART) {
5894 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5895 /* Don't wedge the queue */
5896 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5901 * Let's see if we have seen this device before.
5903 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5905 u_int8_t digest[16];
5910 (unsigned char *)&path->device->inq_data,
5911 sizeof(struct scsi_inquiry_data));
5914 MD5Update(&context, serial_buf->serial_num,
5915 serial_buf->length);
5917 MD5Final(digest, &context);
5918 if (bcmp(softc->digest, digest, 16) == 0)
5922 * XXX Do we need to do a TUR in order to ensure
5923 * that the device really hasn't changed???
5926 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5927 xpt_async(AC_LOST_DEVICE, path, NULL);
5929 if (serial_buf != NULL)
5930 free(serial_buf, M_TEMP);
5934 * Now that we have all the necessary
5935 * information to safely perform transfer
5936 * negotiations... Controllers don't perform
5937 * any negotiation or tagged queuing until
5938 * after the first XPT_SET_TRAN_SETTINGS ccb is
5939 * received. So, on a new device, just retreive
5940 * the user settings, and set them as the current
5941 * settings to set the device up.
5943 proberequestdefaultnegotiation(periph);
5944 xpt_release_ccb(done_ccb);
5947 * Perform a TUR to allow the controller to
5948 * perform any necessary transfer negotiation.
5950 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5951 xpt_schedule(periph, priority);
5954 xpt_release_ccb(done_ccb);
5957 case PROBE_TUR_FOR_NEGOTIATION:
5958 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5959 /* Don't wedge the queue */
5960 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5964 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5966 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5967 /* Inform the XPT that a new device has been found */
5968 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5969 xpt_action(done_ccb);
5971 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5973 xpt_release_ccb(done_ccb);
5976 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5977 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5978 done_ccb->ccb_h.status = CAM_REQ_CMP;
5980 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5981 cam_periph_invalidate(periph);
5982 cam_periph_release(periph);
5984 probeschedule(periph);
5989 probecleanup(struct cam_periph *periph)
5991 free(periph->softc, M_TEMP);
5995 xpt_find_quirk(struct cam_ed *device)
5999 match = cam_quirkmatch((caddr_t)&device->inq_data,
6000 (caddr_t)xpt_quirk_table,
6001 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6002 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6005 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6007 device->quirk = (struct xpt_quirk_entry *)match;
6010 #ifdef CAM_NEW_TRAN_CODE
6013 xpt_devise_transport(struct cam_path *path)
6015 struct ccb_pathinq cpi;
6016 struct ccb_trans_settings cts;
6017 struct scsi_inquiry_data *inq_buf;
6019 /* Get transport information from the SIM */
6020 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6021 cpi.ccb_h.func_code = XPT_PATH_INQ;
6022 xpt_action((union ccb *)&cpi);
6025 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6026 inq_buf = &path->device->inq_data;
6027 path->device->protocol = PROTO_SCSI;
6028 path->device->protocol_version =
6029 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6030 path->device->transport = cpi.transport;
6031 path->device->transport_version = cpi.transport_version;
6034 * Any device not using SPI3 features should
6035 * be considered SPI2 or lower.
6037 if (inq_buf != NULL) {
6038 if (path->device->transport == XPORT_SPI
6039 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6040 && path->device->transport_version > 2)
6041 path->device->transport_version = 2;
6043 struct cam_ed* otherdev;
6045 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6047 otherdev = TAILQ_NEXT(otherdev, links)) {
6048 if (otherdev != path->device)
6052 if (otherdev != NULL) {
6054 * Initially assume the same versioning as
6055 * prior luns for this target.
6057 path->device->protocol_version =
6058 otherdev->protocol_version;
6059 path->device->transport_version =
6060 otherdev->transport_version;
6062 /* Until we know better, opt for safty */
6063 path->device->protocol_version = 2;
6064 if (path->device->transport == XPORT_SPI)
6065 path->device->transport_version = 2;
6067 path->device->transport_version = 0;
6073 * For a device compliant with SPC-2 we should be able
6074 * to determine the transport version supported by
6075 * scrutinizing the version descriptors in the
6079 /* Tell the controller what we think */
6080 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6081 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6082 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6083 cts.transport = path->device->transport;
6084 cts.transport_version = path->device->transport_version;
6085 cts.protocol = path->device->protocol;
6086 cts.protocol_version = path->device->protocol_version;
6087 cts.proto_specific.valid = 0;
6088 cts.xport_specific.valid = 0;
6089 xpt_action((union ccb *)&cts);
6093 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6096 struct ccb_pathinq cpi;
6097 struct ccb_trans_settings cur_cts;
6098 struct ccb_trans_settings_scsi *scsi;
6099 struct ccb_trans_settings_scsi *cur_scsi;
6100 struct cam_sim *sim;
6101 struct scsi_inquiry_data *inq_data;
6103 if (device == NULL) {
6104 cts->ccb_h.status = CAM_PATH_INVALID;
6105 xpt_done((union ccb *)cts);
6109 if (cts->protocol == PROTO_UNKNOWN
6110 || cts->protocol == PROTO_UNSPECIFIED) {
6111 cts->protocol = device->protocol;
6112 cts->protocol_version = device->protocol_version;
6115 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6116 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6117 cts->protocol_version = device->protocol_version;
6119 if (cts->protocol != device->protocol) {
6120 xpt_print_path(cts->ccb_h.path);
6121 printf("Uninitialized Protocol %x:%x?\n",
6122 cts->protocol, device->protocol);
6123 cts->protocol = device->protocol;
6126 if (cts->protocol_version > device->protocol_version) {
6128 xpt_print_path(cts->ccb_h.path);
6129 printf("Down reving Protocol Version from %d to %d?\n",
6130 cts->protocol_version, device->protocol_version);
6132 cts->protocol_version = device->protocol_version;
6135 if (cts->transport == XPORT_UNKNOWN
6136 || cts->transport == XPORT_UNSPECIFIED) {
6137 cts->transport = device->transport;
6138 cts->transport_version = device->transport_version;
6141 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6142 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6143 cts->transport_version = device->transport_version;
6145 if (cts->transport != device->transport) {
6146 xpt_print_path(cts->ccb_h.path);
6147 printf("Uninitialized Transport %x:%x?\n",
6148 cts->transport, device->transport);
6149 cts->transport = device->transport;
6152 if (cts->transport_version > device->transport_version) {
6154 xpt_print_path(cts->ccb_h.path);
6155 printf("Down reving Transport Version from %d to %d?\n",
6156 cts->transport_version,
6157 device->transport_version);
6159 cts->transport_version = device->transport_version;
6162 sim = cts->ccb_h.path->bus->sim;
6165 * Nothing more of interest to do unless
6166 * this is a device connected via the
6169 if (cts->protocol != PROTO_SCSI) {
6170 if (async_update == FALSE)
6171 (*(sim->sim_action))(sim, (union ccb *)cts);
6175 inq_data = &device->inq_data;
6176 scsi = &cts->proto_specific.scsi;
6177 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6178 cpi.ccb_h.func_code = XPT_PATH_INQ;
6179 xpt_action((union ccb *)&cpi);
6181 /* SCSI specific sanity checking */
6182 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6183 || (inq_data->flags & SID_CmdQue) == 0
6184 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6185 || (device->quirk->mintags == 0)) {
6187 * Can't tag on hardware that doesn't support tags,
6188 * doesn't have it enabled, or has broken tag support.
6190 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6193 if (async_update == FALSE) {
6195 * Perform sanity checking against what the
6196 * controller and device can do.
6198 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6199 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6200 cur_cts.type = cts->type;
6201 xpt_action((union ccb *)&cur_cts);
6203 cur_scsi = &cur_cts.proto_specific.scsi;
6204 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6205 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6206 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6208 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6209 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6212 /* SPI specific sanity checking */
6213 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6215 struct ccb_trans_settings_spi *spi;
6216 struct ccb_trans_settings_spi *cur_spi;
6218 spi = &cts->xport_specific.spi;
6220 cur_spi = &cur_cts.xport_specific.spi;
6222 /* Fill in any gaps in what the user gave us */
6223 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6224 spi->sync_period = cur_spi->sync_period;
6225 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6226 spi->sync_period = 0;
6227 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6228 spi->sync_offset = cur_spi->sync_offset;
6229 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6230 spi->sync_offset = 0;
6231 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6232 spi->ppr_options = cur_spi->ppr_options;
6233 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6234 spi->ppr_options = 0;
6235 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6236 spi->bus_width = cur_spi->bus_width;
6237 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6239 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6240 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6241 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6243 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6244 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6245 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6246 && (inq_data->flags & SID_Sync) == 0
6247 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6248 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6249 || (cur_spi->sync_offset == 0)
6250 || (cur_spi->sync_period == 0)) {
6252 spi->sync_period = 0;
6253 spi->sync_offset = 0;
6256 switch (spi->bus_width) {
6257 case MSG_EXT_WDTR_BUS_32_BIT:
6258 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6259 || (inq_data->flags & SID_WBus32) != 0
6260 || cts->type == CTS_TYPE_USER_SETTINGS)
6261 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6263 /* Fall Through to 16-bit */
6264 case MSG_EXT_WDTR_BUS_16_BIT:
6265 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6266 || (inq_data->flags & SID_WBus16) != 0
6267 || cts->type == CTS_TYPE_USER_SETTINGS)
6268 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6269 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6272 /* Fall Through to 8-bit */
6273 default: /* New bus width?? */
6274 case MSG_EXT_WDTR_BUS_8_BIT:
6275 /* All targets can do this */
6276 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6280 spi3caps = cpi.xport_specific.spi.ppr_options;
6281 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6282 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6283 spi3caps &= inq_data->spi3data;
6285 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6286 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6288 if ((spi3caps & SID_SPI_IUS) == 0)
6289 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6291 if ((spi3caps & SID_SPI_QAS) == 0)
6292 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6294 /* No SPI Transfer settings are allowed unless we are wide */
6295 if (spi->bus_width == 0)
6296 spi->ppr_options = 0;
6298 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6300 * Can't tag queue without disconnection.
6302 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6303 scsi->valid |= CTS_SCSI_VALID_TQ;
6307 * If we are currently performing tagged transactions to
6308 * this device and want to change its negotiation parameters,
6309 * go non-tagged for a bit to give the controller a chance to
6310 * negotiate unhampered by tag messages.
6312 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6313 && (device->inq_flags & SID_CmdQue) != 0
6314 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6315 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6316 CTS_SPI_VALID_SYNC_OFFSET|
6317 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6318 xpt_toggle_tags(cts->ccb_h.path);
6321 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6322 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6326 * If we are transitioning from tags to no-tags or
6327 * vice-versa, we need to carefully freeze and restart
6328 * the queue so that we don't overlap tagged and non-tagged
6329 * commands. We also temporarily stop tags if there is
6330 * a change in transfer negotiation settings to allow
6331 * "tag-less" negotiation.
6333 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6334 || (device->inq_flags & SID_CmdQue) != 0)
6335 device_tagenb = TRUE;
6337 device_tagenb = FALSE;
6339 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6340 && device_tagenb == FALSE)
6341 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6342 && device_tagenb == TRUE)) {
6344 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6346 * Delay change to use tags until after a
6347 * few commands have gone to this device so
6348 * the controller has time to perform transfer
6349 * negotiations without tagged messages getting
6352 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6353 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6355 struct ccb_relsim crs;
6357 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6358 device->inq_flags &= ~SID_CmdQue;
6359 xpt_dev_ccbq_resize(cts->ccb_h.path,
6360 sim->max_dev_openings);
6361 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6362 device->tag_delay_count = 0;
6364 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6366 crs.ccb_h.func_code = XPT_REL_SIMQ;
6367 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6369 = crs.release_timeout
6372 xpt_action((union ccb *)&crs);
6376 if (async_update == FALSE)
6377 (*(sim->sim_action))(sim, (union ccb *)cts);
6380 #else /* CAM_NEW_TRAN_CODE */
6383 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6386 struct cam_sim *sim;
6389 sim = cts->ccb_h.path->bus->sim;
6390 if (async_update == FALSE) {
6391 struct scsi_inquiry_data *inq_data;
6392 struct ccb_pathinq cpi;
6393 struct ccb_trans_settings cur_cts;
6395 if (device == NULL) {
6396 cts->ccb_h.status = CAM_PATH_INVALID;
6397 xpt_done((union ccb *)cts);
6402 * Perform sanity checking against what the
6403 * controller and device can do.
6405 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6406 cpi.ccb_h.func_code = XPT_PATH_INQ;
6407 xpt_action((union ccb *)&cpi);
6408 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6409 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6410 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6411 xpt_action((union ccb *)&cur_cts);
6412 inq_data = &device->inq_data;
6414 /* Fill in any gaps in what the user gave us */
6415 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6416 cts->sync_period = cur_cts.sync_period;
6417 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6418 cts->sync_offset = cur_cts.sync_offset;
6419 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6420 cts->bus_width = cur_cts.bus_width;
6421 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6422 cts->flags &= ~CCB_TRANS_DISC_ENB;
6423 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6425 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6426 cts->flags &= ~CCB_TRANS_TAG_ENB;
6427 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6430 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6431 && (inq_data->flags & SID_Sync) == 0)
6432 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6433 || (cts->sync_offset == 0)
6434 || (cts->sync_period == 0)) {
6436 cts->sync_period = 0;
6437 cts->sync_offset = 0;
6438 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6439 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6440 && cts->sync_period <= 0x9) {
6442 * Don't allow DT transmission rates if the
6443 * device does not support it.
6445 cts->sync_period = 0xa;
6448 switch (cts->bus_width) {
6449 case MSG_EXT_WDTR_BUS_32_BIT:
6450 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6451 || (inq_data->flags & SID_WBus32) != 0)
6452 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6454 /* Fall Through to 16-bit */
6455 case MSG_EXT_WDTR_BUS_16_BIT:
6456 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6457 || (inq_data->flags & SID_WBus16) != 0)
6458 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6459 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6462 /* Fall Through to 8-bit */
6463 default: /* New bus width?? */
6464 case MSG_EXT_WDTR_BUS_8_BIT:
6465 /* All targets can do this */
6466 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6470 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6472 * Can't tag queue without disconnection.
6474 cts->flags &= ~CCB_TRANS_TAG_ENB;
6475 cts->valid |= CCB_TRANS_TQ_VALID;
6478 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6479 || (inq_data->flags & SID_CmdQue) == 0
6480 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6481 || (device->quirk->mintags == 0)) {
6483 * Can't tag on hardware that doesn't support,
6484 * doesn't have it enabled, or has broken tag support.
6486 cts->flags &= ~CCB_TRANS_TAG_ENB;
6491 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6495 * If we are transitioning from tags to no-tags or
6496 * vice-versa, we need to carefully freeze and restart
6497 * the queue so that we don't overlap tagged and non-tagged
6498 * commands. We also temporarily stop tags if there is
6499 * a change in transfer negotiation settings to allow
6500 * "tag-less" negotiation.
6502 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6503 || (device->inq_flags & SID_CmdQue) != 0)
6504 device_tagenb = TRUE;
6506 device_tagenb = FALSE;
6508 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6509 && device_tagenb == FALSE)
6510 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6511 && device_tagenb == TRUE)) {
6513 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6515 * Delay change to use tags until after a
6516 * few commands have gone to this device so
6517 * the controller has time to perform transfer
6518 * negotiations without tagged messages getting
6521 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6522 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6524 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6526 device->inq_flags &= ~SID_CmdQue;
6527 xpt_dev_ccbq_resize(cts->ccb_h.path,
6528 sim->max_dev_openings);
6529 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6530 device->tag_delay_count = 0;
6535 if (async_update == FALSE) {
6537 * If we are currently performing tagged transactions to
6538 * this device and want to change its negotiation parameters,
6539 * go non-tagged for a bit to give the controller a chance to
6540 * negotiate unhampered by tag messages.
6542 if ((device->inq_flags & SID_CmdQue) != 0
6543 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6544 CCB_TRANS_SYNC_OFFSET_VALID|
6545 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6546 xpt_toggle_tags(cts->ccb_h.path);
6548 (*(sim->sim_action))(sim, (union ccb *)cts);
6552 struct ccb_relsim crs;
6554 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6556 crs.ccb_h.func_code = XPT_REL_SIMQ;
6557 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6559 = crs.release_timeout
6562 xpt_action((union ccb *)&crs);
6567 #endif /* CAM_NEW_TRAN_CODE */
6570 xpt_toggle_tags(struct cam_path *path)
6575 * Give controllers a chance to renegotiate
6576 * before starting tag operations. We
6577 * "toggle" tagged queuing off then on
6578 * which causes the tag enable command delay
6579 * counter to come into effect.
6582 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6583 || ((dev->inq_flags & SID_CmdQue) != 0
6584 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6585 struct ccb_trans_settings cts;
6587 xpt_setup_ccb(&cts.ccb_h, path, 1);
6588 #ifdef CAM_NEW_TRAN_CODE
6589 cts.protocol = PROTO_SCSI;
6590 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6591 cts.transport = XPORT_UNSPECIFIED;
6592 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6593 cts.proto_specific.scsi.flags = 0;
6594 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6595 #else /* CAM_NEW_TRAN_CODE */
6597 cts.valid = CCB_TRANS_TQ_VALID;
6598 #endif /* CAM_NEW_TRAN_CODE */
6599 xpt_set_transfer_settings(&cts, path->device,
6600 /*async_update*/TRUE);
6601 #ifdef CAM_NEW_TRAN_CODE
6602 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6603 #else /* CAM_NEW_TRAN_CODE */
6604 cts.flags = CCB_TRANS_TAG_ENB;
6605 #endif /* CAM_NEW_TRAN_CODE */
6606 xpt_set_transfer_settings(&cts, path->device,
6607 /*async_update*/TRUE);
6612 xpt_start_tags(struct cam_path *path)
6614 struct ccb_relsim crs;
6615 struct cam_ed *device;
6616 struct cam_sim *sim;
6619 device = path->device;
6620 sim = path->bus->sim;
6621 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6622 xpt_freeze_devq(path, /*count*/1);
6623 device->inq_flags |= SID_CmdQue;
6624 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
6625 xpt_dev_ccbq_resize(path, newopenings);
6626 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6627 crs.ccb_h.func_code = XPT_REL_SIMQ;
6628 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6630 = crs.release_timeout
6633 xpt_action((union ccb *)&crs);
6636 static int busses_to_config;
6637 static int busses_to_reset;
6640 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6642 if (bus->path_id != CAM_XPT_PATH_ID) {
6643 struct cam_path path;
6644 struct ccb_pathinq cpi;
6648 xpt_compile_path(&path, NULL, bus->path_id,
6649 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6650 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6651 cpi.ccb_h.func_code = XPT_PATH_INQ;
6652 xpt_action((union ccb *)&cpi);
6653 can_negotiate = cpi.hba_inquiry;
6654 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6655 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6658 xpt_release_path(&path);
6665 xptconfigfunc(struct cam_eb *bus, void *arg)
6667 struct cam_path *path;
6668 union ccb *work_ccb;
6670 if (bus->path_id != CAM_XPT_PATH_ID) {
6674 work_ccb = xpt_alloc_ccb();
6675 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6676 CAM_TARGET_WILDCARD,
6677 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6678 printf("xptconfigfunc: xpt_create_path failed with "
6679 "status %#x for bus %d\n", status, bus->path_id);
6680 printf("xptconfigfunc: halting bus configuration\n");
6681 xpt_free_ccb(work_ccb);
6683 xpt_finishconfig(xpt_periph, NULL);
6686 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6687 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6688 xpt_action(work_ccb);
6689 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6690 printf("xptconfigfunc: CPI failed on bus %d "
6691 "with status %d\n", bus->path_id,
6692 work_ccb->ccb_h.status);
6693 xpt_finishconfig(xpt_periph, work_ccb);
6697 can_negotiate = work_ccb->cpi.hba_inquiry;
6698 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6699 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6700 && (can_negotiate != 0)) {
6701 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6702 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6703 work_ccb->ccb_h.cbfcnp = NULL;
6704 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6705 ("Resetting Bus\n"));
6706 xpt_action(work_ccb);
6707 xpt_finishconfig(xpt_periph, work_ccb);
6709 /* Act as though we performed a successful BUS RESET */
6710 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6711 xpt_finishconfig(xpt_periph, work_ccb);
6719 xpt_config(void *arg)
6722 * Now that interrupts are enabled, go find our devices
6726 /* Setup debugging flags and path */
6727 #ifdef CAM_DEBUG_FLAGS
6728 cam_dflags = CAM_DEBUG_FLAGS;
6729 #else /* !CAM_DEBUG_FLAGS */
6730 cam_dflags = CAM_DEBUG_NONE;
6731 #endif /* CAM_DEBUG_FLAGS */
6732 #ifdef CAM_DEBUG_BUS
6733 if (cam_dflags != CAM_DEBUG_NONE) {
6734 if (xpt_create_path(&cam_dpath, xpt_periph,
6735 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6736 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6737 printf("xpt_config: xpt_create_path() failed for debug"
6738 " target %d:%d:%d, debugging disabled\n",
6739 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6740 cam_dflags = CAM_DEBUG_NONE;
6744 #else /* !CAM_DEBUG_BUS */
6746 #endif /* CAM_DEBUG_BUS */
6747 #endif /* CAMDEBUG */
6750 * Scan all installed busses.
6752 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6754 if (busses_to_config == 0) {
6755 /* Call manually because we don't have any busses */
6756 xpt_finishconfig(xpt_periph, NULL);
6758 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6759 printf("Waiting %d seconds for SCSI "
6760 "devices to settle\n", SCSI_DELAY/1000);
6762 xpt_for_all_busses(xptconfigfunc, NULL);
6767 * If the given device only has one peripheral attached to it, and if that
6768 * peripheral is the passthrough driver, announce it. This insures that the
6769 * user sees some sort of announcement for every peripheral in their system.
6772 xptpassannouncefunc(struct cam_ed *device, void *arg)
6774 struct cam_periph *periph;
6777 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6778 periph = SLIST_NEXT(periph, periph_links), i++);
6780 periph = SLIST_FIRST(&device->periphs);
6782 && (strncmp(periph->periph_name, "pass", 4) == 0))
6783 xpt_announce_periph(periph, NULL);
6789 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6791 struct periph_driver **p_drv;
6794 if (done_ccb != NULL) {
6795 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6796 ("xpt_finishconfig\n"));
6797 switch(done_ccb->ccb_h.func_code) {
6799 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6800 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6801 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6802 xpt_action(done_ccb);
6808 xpt_free_path(done_ccb->ccb_h.path);
6814 if (busses_to_config == 0) {
6815 /* Register all the peripheral drivers */
6816 /* XXX This will have to change when we have loadable modules */
6817 p_drv = periph_drivers;
6818 for (i = 0; p_drv[i] != NULL; i++) {
6819 (*p_drv[i]->init)();
6823 * Check for devices with no "standard" peripheral driver
6824 * attached. For any devices like that, announce the
6825 * passthrough driver so the user will see something.
6827 xpt_for_all_devices(xptpassannouncefunc, NULL);
6829 /* Release our hook so that the boot can continue. */
6830 config_intrhook_disestablish(xpt_config_hook);
6831 free(xpt_config_hook, M_TEMP);
6832 xpt_config_hook = NULL;
6834 if (done_ccb != NULL)
6835 xpt_free_ccb(done_ccb);
6839 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6841 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6843 switch (work_ccb->ccb_h.func_code) {
6844 /* Common cases first */
6845 case XPT_PATH_INQ: /* Path routing inquiry */
6847 struct ccb_pathinq *cpi;
6849 cpi = &work_ccb->cpi;
6850 cpi->version_num = 1; /* XXX??? */
6851 cpi->hba_inquiry = 0;
6852 cpi->target_sprt = 0;
6854 cpi->hba_eng_cnt = 0;
6855 cpi->max_target = 0;
6857 cpi->initiator_id = 0;
6858 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6859 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6860 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6861 cpi->unit_number = sim->unit_number;
6862 cpi->bus_id = sim->bus_id;
6863 cpi->base_transfer_speed = 0;
6864 #ifdef CAM_NEW_TRAN_CODE
6865 cpi->protocol = PROTO_UNSPECIFIED;
6866 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6867 cpi->transport = XPORT_UNSPECIFIED;
6868 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6869 #endif /* CAM_NEW_TRAN_CODE */
6870 cpi->ccb_h.status = CAM_REQ_CMP;
6875 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6882 * The xpt as a "controller" has no interrupt sources, so polling
6886 xptpoll(struct cam_sim *sim)
6891 camisr(void *V_queue)
6893 cam_isrq_t *queue = V_queue;
6895 struct ccb_hdr *ccb_h;
6898 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6901 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6902 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6905 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6910 if (ccb_h->flags & CAM_HIGH_POWER) {
6911 struct highpowerlist *hphead;
6912 struct cam_ed *device;
6913 union ccb *send_ccb;
6915 hphead = &highpowerq;
6917 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6920 * Increment the count since this command is done.
6925 * Any high powered commands queued up?
6927 if (send_ccb != NULL) {
6928 device = send_ccb->ccb_h.path->device;
6930 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6932 xpt_release_devq(send_ccb->ccb_h.path,
6933 /*count*/1, /*runqueue*/TRUE);
6936 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6939 dev = ccb_h->path->device;
6942 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6944 ccb_h->path->bus->sim->devq->send_active--;
6945 ccb_h->path->bus->sim->devq->send_openings++;
6948 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6949 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
6950 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6951 && (dev->ccbq.dev_active == 0))) {
6953 xpt_release_devq(ccb_h->path, /*count*/1,
6957 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6958 && (--dev->tag_delay_count == 0))
6959 xpt_start_tags(ccb_h->path);
6961 if ((dev->ccbq.queue.entries > 0)
6962 && (dev->qfrozen_cnt == 0)
6963 && (device_is_send_queued(dev) == 0)) {
6964 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6969 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6970 xpt_release_simq(ccb_h->path->bus->sim,
6972 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6976 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6977 && (ccb_h->status & CAM_DEV_QFRZN)) {
6978 xpt_release_devq(ccb_h->path, /*count*/1,
6980 ccb_h->status &= ~CAM_DEV_QFRZN;
6982 xpt_run_dev_sendq(ccb_h->path->bus);
6985 /* Call the peripheral driver's callback */
6986 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6988 /* Raise IPL for while test */