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>
45 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt.h>
55 #include <cam/cam_xpt_sim.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_debug.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/scsi/scsi_pass.h>
64 /* Datastructures internal to the xpt layer */
67 * Definition of an async handler callback block. These are used to add
68 * SIMs and peripherals to the async callback lists.
71 SLIST_ENTRY(async_node) links;
72 u_int32_t event_enable; /* Async Event enables */
73 void (*callback)(void *arg, u_int32_t code,
74 struct cam_path *path, void *args);
78 SLIST_HEAD(async_list, async_node);
79 SLIST_HEAD(periph_list, cam_periph);
80 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
83 * This is the maximum number of high powered commands (e.g. start unit)
84 * that can be outstanding at a particular time.
86 #ifndef CAM_MAX_HIGHPOWER
87 #define CAM_MAX_HIGHPOWER 4
90 /* number of high powered commands that can go through right now */
91 static int num_highpower = CAM_MAX_HIGHPOWER;
94 * Structure for queueing a device in a run queue.
95 * There is one run queue for allocating new ccbs,
96 * and another for sending ccbs to the controller.
100 struct cam_ed *device;
104 * The CAM EDT (Existing Device Table) contains the device information for
105 * all devices for all busses in the system. The table contains a
106 * cam_ed structure for each device on the bus.
109 TAILQ_ENTRY(cam_ed) links;
110 struct cam_ed_qinfo alloc_ccb_entry;
111 struct cam_ed_qinfo send_ccb_entry;
112 struct cam_et *target;
115 * Queue of type drivers wanting to do
116 * work on this device.
118 struct cam_ccbq ccbq; /* Queue of pending ccbs */
119 struct async_list asyncs; /* Async callback info for this B/T/L */
120 struct periph_list periphs; /* All attached devices */
121 u_int generation; /* Generation number */
122 struct cam_periph *owner; /* Peripheral driver's ownership tag */
123 struct xpt_quirk_entry *quirk; /* Oddities about this device */
124 /* Storage for the inquiry data */
125 struct scsi_inquiry_data inq_data;
126 u_int8_t inq_flags; /*
127 * Current settings for inquiry flags.
128 * This allows us to override settings
129 * like disconnection and tagged
130 * queuing for a device.
132 u_int8_t queue_flags; /* Queue flags from the control page */
133 u_int8_t serial_num_len;
134 u_int8_t *serial_num;
135 u_int32_t qfrozen_cnt;
137 #define CAM_DEV_UNCONFIGURED 0x01
138 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
139 #define CAM_DEV_REL_ON_COMPLETE 0x04
140 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
141 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
142 #define CAM_DEV_TAG_AFTER_COUNT 0x20
143 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
144 u_int32_t tag_delay_count;
145 #define CAM_TAG_DELAY_COUNT 5
147 struct callout_handle c_handle;
151 * Each target is represented by an ET (Existing Target). These
152 * entries are created when a target is successfully probed with an
153 * identify, and removed when a device fails to respond after a number
154 * of retries, or a bus rescan finds the device missing.
157 TAILQ_HEAD(, cam_ed) ed_entries;
158 TAILQ_ENTRY(cam_et) links;
160 target_id_t target_id;
163 struct timeval last_reset;
167 * Each bus is represented by an EB (Existing Bus). These entries
168 * are created by calls to xpt_bus_register and deleted by calls to
169 * xpt_bus_deregister.
172 TAILQ_HEAD(, cam_et) et_entries;
173 TAILQ_ENTRY(cam_eb) links;
176 struct timeval last_reset;
178 #define CAM_EB_RUNQ_SCHEDULED 0x01
184 struct cam_periph *periph;
186 struct cam_et *target;
187 struct cam_ed *device;
190 struct xpt_quirk_entry {
191 struct scsi_inquiry_pattern inq_pat;
193 #define CAM_QUIRK_NOLUNS 0x01
194 #define CAM_QUIRK_NOSERIAL 0x02
195 #define CAM_QUIRK_HILUNS 0x04
199 #define CAM_SCSI2_MAXLUN 8
207 u_int32_t generation;
210 static const char quantum[] = "QUANTUM";
211 static const char sony[] = "SONY";
212 static const char west_digital[] = "WDIGTL";
213 static const char samsung[] = "SAMSUNG";
214 static const char seagate[] = "SEAGATE";
215 static const char microp[] = "MICROP";
217 static struct xpt_quirk_entry xpt_quirk_table[] =
220 /* Reports QUEUE FULL for temporary resource shortages */
221 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
222 /*quirks*/0, /*mintags*/24, /*maxtags*/32
225 /* Reports QUEUE FULL for temporary resource shortages */
226 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
227 /*quirks*/0, /*mintags*/24, /*maxtags*/32
230 /* Reports QUEUE FULL for temporary resource shortages */
231 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
232 /*quirks*/0, /*mintags*/24, /*maxtags*/32
235 /* Broken tagged queuing drive */
236 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
237 /*quirks*/0, /*mintags*/0, /*maxtags*/0
240 /* Broken tagged queuing drive */
241 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
242 /*quirks*/0, /*mintags*/0, /*maxtags*/0
245 /* Broken tagged queuing drive */
246 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
247 /*quirks*/0, /*mintags*/0, /*maxtags*/0
251 * Unfortunately, the Quantum Atlas III has the same
252 * problem as the Atlas II drives above.
253 * Reported by: "Johan Granlund" <johan@granlund.nu>
255 * For future reference, the drive with the problem was:
256 * QUANTUM QM39100TD-SW N1B0
258 * It's possible that Quantum will fix the problem in later
259 * firmware revisions. If that happens, the quirk entry
260 * will need to be made specific to the firmware revisions
264 /* Reports QUEUE FULL for temporary resource shortages */
265 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
266 /*quirks*/0, /*mintags*/24, /*maxtags*/32
270 * 18 Gig Atlas III, same problem as the 9G version.
271 * Reported by: Andre Albsmeier
272 * <andre.albsmeier@mchp.siemens.de>
274 * For future reference, the drive with the problem was:
275 * QUANTUM QM318000TD-S N491
277 /* Reports QUEUE FULL for temporary resource shortages */
278 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
279 /*quirks*/0, /*mintags*/24, /*maxtags*/32
283 * Broken tagged queuing drive
284 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
285 * and: Martin Renters <martin@tdc.on.ca>
287 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
288 /*quirks*/0, /*mintags*/0, /*maxtags*/0
291 * The Seagate Medalist Pro drives have very poor write
292 * performance with anything more than 2 tags.
294 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
295 * Drive: <SEAGATE ST36530N 1444>
297 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
298 * Drive: <SEAGATE ST34520W 1281>
300 * No one has actually reported that the 9G version
301 * (ST39140*) of the Medalist Pro has the same problem, but
302 * we're assuming that it does because the 4G and 6.5G
303 * versions of the drive are broken.
306 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
307 /*quirks*/0, /*mintags*/2, /*maxtags*/2
310 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
311 /*quirks*/0, /*mintags*/2, /*maxtags*/2
314 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
315 /*quirks*/0, /*mintags*/2, /*maxtags*/2
319 * Slow when tagged queueing is enabled. Write performance
320 * steadily drops off with more and more concurrent
321 * transactions. Best sequential write performance with
322 * tagged queueing turned off and write caching turned on.
325 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
326 * Drive: DCAS-34330 w/ "S65A" firmware.
328 * The drive with the problem had the "S65A" firmware
329 * revision, and has also been reported (by Stephen J.
330 * Roznowski <sjr@home.net>) for a drive with the "S61A"
333 * Although no one has reported problems with the 2 gig
334 * version of the DCAS drive, the assumption is that it
335 * has the same problems as the 4 gig version. Therefore
336 * this quirk entries disables tagged queueing for all
339 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
340 /*quirks*/0, /*mintags*/0, /*maxtags*/0
343 /* Broken tagged queuing drive */
344 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
345 /*quirks*/0, /*mintags*/0, /*maxtags*/0
348 /* Broken tagged queuing drive */
349 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
350 /*quirks*/0, /*mintags*/0, /*maxtags*/0
354 * Broken tagged queuing drive.
356 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
359 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
360 /*quirks*/0, /*mintags*/0, /*maxtags*/0
364 * Slow when tagged queueing is enabled. (1.5MB/sec versus
366 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
367 * Best performance with these drives is achieved with
368 * tagged queueing turned off, and write caching turned on.
370 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
371 /*quirks*/0, /*mintags*/0, /*maxtags*/0
375 * Slow when tagged queueing is enabled. (1.5MB/sec versus
377 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
378 * Best performance with these drives is achieved with
379 * tagged queueing turned off, and write caching turned on.
381 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
382 /*quirks*/0, /*mintags*/0, /*maxtags*/0
386 * Doesn't handle queue full condition correctly,
387 * so we need to limit maxtags to what the device
388 * can handle instead of determining this automatically.
390 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
391 /*quirks*/0, /*mintags*/2, /*maxtags*/32
394 /* Really only one LUN */
395 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA*", "*" },
396 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
399 /* I can't believe we need a quirk for DPT volumes. */
400 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
401 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
402 /*mintags*/0, /*maxtags*/255
406 * Many Sony CDROM drives don't like multi-LUN probing.
408 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
409 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
413 * This drive doesn't like multiple LUN probing.
414 * Submitted by: Parag Patel <parag@cgt.com>
416 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
417 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
420 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
421 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
425 * The 8200 doesn't like multi-lun probing, and probably
426 * don't like serial number requests either.
429 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
432 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
436 * These Hitachi drives don't like multi-lun probing.
437 * The PR submitter has a DK319H, but says that the Linux
438 * kernel has a similar work-around for the DK312 and DK314,
439 * so all DK31* drives are quirked here.
441 * Submitted by: Paul Haddad <paul@pth.com>
443 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
444 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
448 * This old revision of the TDC3600 is also SCSI-1, and
449 * hangs upon serial number probing.
452 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
455 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
459 * Would repond to all LUNs if asked for.
462 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
465 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
469 * Would repond to all LUNs if asked for.
472 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
475 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
478 /* Submitted by: Matthew Dodd <winter@jurai.net> */
479 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
480 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
483 /* Submitted by: Matthew Dodd <winter@jurai.net> */
484 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
485 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
488 /* TeraSolutions special settings for TRC-22 RAID */
489 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
490 /*quirks*/0, /*mintags*/55, /*maxtags*/255
494 * Would respond to all LUNs. Device type and removable
495 * flag are jumper-selectable.
497 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
500 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
503 /* Default tagged queuing parameters for all devices */
505 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
506 /*vendor*/"*", /*product*/"*", /*revision*/"*"
508 /*quirks*/0, /*mintags*/2, /*maxtags*/255
512 static const int xpt_quirk_table_size =
513 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
517 DM_RET_FLAG_MASK = 0x0f,
520 DM_RET_DESCEND = 0x20,
522 DM_RET_ACTION_MASK = 0xf0
530 } xpt_traverse_depth;
532 struct xpt_traverse_config {
533 xpt_traverse_depth depth;
538 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
539 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
540 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
541 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
542 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
544 /* Transport layer configuration information */
545 static struct xpt_softc xsoftc;
547 /* Queues for our software interrupt handler */
548 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
549 static cam_isrq_t cam_bioq;
550 static cam_isrq_t cam_netq;
552 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
553 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
554 static u_int xpt_max_ccbs; /*
555 * Maximum size of ccb pool. Modified as
556 * devices are added/removed or have their
557 * opening counts changed.
559 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
561 struct cam_periph *xpt_periph;
563 static periph_init_t xpt_periph_init;
565 static periph_init_t probe_periph_init;
567 static struct periph_driver xpt_driver =
569 xpt_periph_init, "xpt",
570 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
573 static struct periph_driver probe_driver =
575 probe_periph_init, "probe",
576 TAILQ_HEAD_INITIALIZER(probe_driver.units)
579 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
580 PERIPHDRIVER_DECLARE(probe, probe_driver);
582 #define XPT_CDEV_MAJOR 104
584 static d_open_t xptopen;
585 static d_close_t xptclose;
586 static d_ioctl_t xptioctl;
588 static struct cdevsw xpt_cdevsw = {
590 /* close */ xptclose,
593 /* ioctl */ xptioctl,
596 /* strategy */ nostrategy,
598 /* maj */ XPT_CDEV_MAJOR,
605 static struct intr_config_hook *xpt_config_hook;
607 /* Registered busses */
608 static TAILQ_HEAD(,cam_eb) xpt_busses;
609 static u_int bus_generation;
611 /* Storage for debugging datastructures */
613 struct cam_path *cam_dpath;
614 u_int32_t cam_dflags;
615 u_int32_t cam_debug_delay;
618 /* Pointers to software interrupt handlers */
622 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
623 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
627 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
628 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
629 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
631 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
632 || defined(CAM_DEBUG_LUN)
634 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
635 || !defined(CAM_DEBUG_LUN)
636 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
638 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
639 #else /* !CAMDEBUG */
640 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
641 #endif /* CAMDEBUG */
642 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
644 /* Our boot-time initialization hook */
645 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
647 static moduledata_t cam_moduledata = {
649 cam_module_event_handler,
653 static void xpt_init(void *);
655 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
656 MODULE_VERSION(cam, 1);
659 static cam_status xpt_compile_path(struct cam_path *new_path,
660 struct cam_periph *perph,
662 target_id_t target_id,
665 static void xpt_release_path(struct cam_path *path);
667 static void xpt_async_bcast(struct async_list *async_head,
668 u_int32_t async_code,
669 struct cam_path *path,
671 static void xpt_dev_async(u_int32_t async_code,
673 struct cam_et *target,
674 struct cam_ed *device,
676 static path_id_t xptnextfreepathid(void);
677 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
678 static union ccb *xpt_get_ccb(struct cam_ed *device);
679 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
680 u_int32_t new_priority);
681 static void xpt_run_dev_allocq(struct cam_eb *bus);
682 static void xpt_run_dev_sendq(struct cam_eb *bus);
683 static timeout_t xpt_release_devq_timeout;
684 static timeout_t xpt_release_simq_timeout;
685 static void xpt_release_bus(struct cam_eb *bus);
686 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
688 static struct cam_et*
689 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
690 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
691 static struct cam_ed*
692 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
694 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
695 struct cam_ed *device);
696 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
697 static struct cam_eb*
698 xpt_find_bus(path_id_t path_id);
699 static struct cam_et*
700 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
701 static struct cam_ed*
702 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
703 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
704 static void xpt_scan_lun(struct cam_periph *periph,
705 struct cam_path *path, cam_flags flags,
707 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
708 static xpt_busfunc_t xptconfigbuscountfunc;
709 static xpt_busfunc_t xptconfigfunc;
710 static void xpt_config(void *arg);
711 static xpt_devicefunc_t xptpassannouncefunc;
712 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
713 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
714 static void xptpoll(struct cam_sim *sim);
715 static void camisr(void *);
717 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
718 static void xptasync(struct cam_periph *periph,
719 u_int32_t code, cam_path *path);
721 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
722 int num_patterns, struct cam_eb *bus);
723 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
724 int num_patterns, struct cam_ed *device);
725 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
727 struct cam_periph *periph);
728 static xpt_busfunc_t xptedtbusfunc;
729 static xpt_targetfunc_t xptedttargetfunc;
730 static xpt_devicefunc_t xptedtdevicefunc;
731 static xpt_periphfunc_t xptedtperiphfunc;
732 static xpt_pdrvfunc_t xptplistpdrvfunc;
733 static xpt_periphfunc_t xptplistperiphfunc;
734 static int xptedtmatch(struct ccb_dev_match *cdm);
735 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
736 static int xptbustraverse(struct cam_eb *start_bus,
737 xpt_busfunc_t *tr_func, void *arg);
738 static int xpttargettraverse(struct cam_eb *bus,
739 struct cam_et *start_target,
740 xpt_targetfunc_t *tr_func, void *arg);
741 static int xptdevicetraverse(struct cam_et *target,
742 struct cam_ed *start_device,
743 xpt_devicefunc_t *tr_func, void *arg);
744 static int xptperiphtraverse(struct cam_ed *device,
745 struct cam_periph *start_periph,
746 xpt_periphfunc_t *tr_func, void *arg);
747 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
748 xpt_pdrvfunc_t *tr_func, void *arg);
749 static int xptpdperiphtraverse(struct periph_driver **pdrv,
750 struct cam_periph *start_periph,
751 xpt_periphfunc_t *tr_func,
753 static xpt_busfunc_t xptdefbusfunc;
754 static xpt_targetfunc_t xptdeftargetfunc;
755 static xpt_devicefunc_t xptdefdevicefunc;
756 static xpt_periphfunc_t xptdefperiphfunc;
757 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
759 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
762 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
765 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
768 static xpt_devicefunc_t xptsetasyncfunc;
769 static xpt_busfunc_t xptsetasyncbusfunc;
770 static cam_status xptregister(struct cam_periph *periph,
772 static cam_status proberegister(struct cam_periph *periph,
774 static void probeschedule(struct cam_periph *probe_periph);
775 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
776 static void proberequestdefaultnegotiation(struct cam_periph *periph);
777 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
778 static void probecleanup(struct cam_periph *periph);
779 static void xpt_find_quirk(struct cam_ed *device);
780 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
781 struct cam_ed *device,
783 static void xpt_toggle_tags(struct cam_path *path);
784 static void xpt_start_tags(struct cam_path *path);
785 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
787 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
789 static __inline int periph_is_queued(struct cam_periph *periph);
790 static __inline int device_is_alloc_queued(struct cam_ed *device);
791 static __inline int device_is_send_queued(struct cam_ed *device);
792 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
795 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
799 if (dev->ccbq.devq_openings > 0) {
800 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
801 cam_ccbq_resize(&dev->ccbq,
802 dev->ccbq.dev_openings
803 + dev->ccbq.dev_active);
804 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
807 * The priority of a device waiting for CCB resources
808 * is that of the the highest priority peripheral driver
811 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
812 &dev->alloc_ccb_entry.pinfo,
813 CAMQ_GET_HEAD(&dev->drvq)->priority);
822 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
826 if (dev->ccbq.dev_openings > 0) {
828 * The priority of a device waiting for controller
829 * resources is that of the the highest priority CCB
833 xpt_schedule_dev(&bus->sim->devq->send_queue,
834 &dev->send_ccb_entry.pinfo,
835 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
843 periph_is_queued(struct cam_periph *periph)
845 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
849 device_is_alloc_queued(struct cam_ed *device)
851 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
855 device_is_send_queued(struct cam_ed *device)
857 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
861 dev_allocq_is_runnable(struct cam_devq *devq)
865 * Have space to do more work.
866 * Allowed to do work.
868 return ((devq->alloc_queue.qfrozen_cnt == 0)
869 && (devq->alloc_queue.entries > 0)
870 && (devq->alloc_openings > 0));
876 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
886 xptdone(struct cam_periph *periph, union ccb *done_ccb)
888 /* Caller will release the CCB */
889 wakeup(&done_ccb->ccb_h.cbfcnp);
893 xptopen(dev_t dev, int flags, int fmt, struct proc *p)
897 unit = minor(dev) & 0xff;
900 * Only allow read-write access.
902 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
906 * We don't allow nonblocking access.
908 if ((flags & O_NONBLOCK) != 0) {
909 printf("xpt%d: can't do nonblocking accesss\n", unit);
914 * We only have one transport layer right now. If someone accesses
915 * us via something other than minor number 1, point out their
919 printf("xptopen: got invalid xpt unit %d\n", unit);
923 /* Mark ourselves open */
924 xsoftc.flags |= XPT_FLAG_OPEN;
930 xptclose(dev_t dev, int flag, int fmt, struct proc *p)
934 unit = minor(dev) & 0xff;
937 * We only have one transport layer right now. If someone accesses
938 * us via something other than minor number 1, point out their
942 printf("xptclose: got invalid xpt unit %d\n", unit);
946 /* Mark ourselves closed */
947 xsoftc.flags &= ~XPT_FLAG_OPEN;
953 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
958 unit = minor(dev) & 0xff;
961 * We only have one transport layer right now. If someone accesses
962 * us via something other than minor number 1, point out their
966 printf("xptioctl: got invalid xpt unit %d\n", unit);
972 * For the transport layer CAMIOCOMMAND ioctl, we really only want
973 * to accept CCB types that don't quite make sense to send through a
974 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
981 inccb = (union ccb *)addr;
983 switch(inccb->ccb_h.func_code) {
986 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
987 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
996 ccb = xpt_alloc_ccb();
999 * Create a path using the bus, target, and lun the
1002 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1003 inccb->ccb_h.path_id,
1004 inccb->ccb_h.target_id,
1005 inccb->ccb_h.target_lun) !=
1011 /* Ensure all of our fields are correct */
1012 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1013 inccb->ccb_h.pinfo.priority);
1014 xpt_merge_ccb(ccb, inccb);
1015 ccb->ccb_h.cbfcnp = xptdone;
1016 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1017 bcopy(ccb, inccb, sizeof(union ccb));
1018 xpt_free_path(ccb->ccb_h.path);
1026 * This is an immediate CCB, so it's okay to
1027 * allocate it on the stack.
1031 * Create a path using the bus, target, and lun the
1034 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1035 inccb->ccb_h.path_id,
1036 inccb->ccb_h.target_id,
1037 inccb->ccb_h.target_lun) !=
1042 /* Ensure all of our fields are correct */
1043 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1044 inccb->ccb_h.pinfo.priority);
1045 xpt_merge_ccb(&ccb, inccb);
1046 ccb.ccb_h.cbfcnp = xptdone;
1048 bcopy(&ccb, inccb, sizeof(union ccb));
1049 xpt_free_path(ccb.ccb_h.path);
1053 case XPT_DEV_MATCH: {
1054 struct cam_periph_map_info mapinfo;
1055 struct cam_path *old_path;
1058 * We can't deal with physical addresses for this
1059 * type of transaction.
1061 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1067 * Save this in case the caller had it set to
1068 * something in particular.
1070 old_path = inccb->ccb_h.path;
1073 * We really don't need a path for the matching
1074 * code. The path is needed because of the
1075 * debugging statements in xpt_action(). They
1076 * assume that the CCB has a valid path.
1078 inccb->ccb_h.path = xpt_periph->path;
1080 bzero(&mapinfo, sizeof(mapinfo));
1083 * Map the pattern and match buffers into kernel
1084 * virtual address space.
1086 error = cam_periph_mapmem(inccb, &mapinfo);
1089 inccb->ccb_h.path = old_path;
1094 * This is an immediate CCB, we can send it on directly.
1099 * Map the buffers back into user space.
1101 cam_periph_unmapmem(inccb, &mapinfo);
1103 inccb->ccb_h.path = old_path;
1115 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1116 * with the periphal driver name and unit name filled in. The other
1117 * fields don't really matter as input. The passthrough driver name
1118 * ("pass"), and unit number are passed back in the ccb. The current
1119 * device generation number, and the index into the device peripheral
1120 * driver list, and the status are also passed back. Note that
1121 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1122 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1123 * (or rather should be) impossible for the device peripheral driver
1124 * list to change since we look at the whole thing in one pass, and
1125 * we do it with splcam protection.
1128 case CAMGETPASSTHRU: {
1130 struct cam_periph *periph;
1131 struct periph_driver **p_drv;
1135 int base_periph_found;
1139 ccb = (union ccb *)addr;
1140 unit = ccb->cgdl.unit_number;
1141 name = ccb->cgdl.periph_name;
1143 * Every 100 devices, we want to drop our spl protection to
1144 * give the software interrupt handler a chance to run.
1145 * Most systems won't run into this check, but this should
1146 * avoid starvation in the software interrupt handler in
1151 ccb = (union ccb *)addr;
1153 base_periph_found = 0;
1156 * Sanity check -- make sure we don't get a null peripheral
1159 if (*ccb->cgdl.periph_name == '\0') {
1164 /* Keep the list from changing while we traverse it */
1167 cur_generation = xsoftc.generation;
1169 /* first find our driver in the list of drivers */
1170 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1171 if (strcmp((*p_drv)->driver_name, name) == 0)
1174 if (*p_drv == NULL) {
1176 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1177 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1178 *ccb->cgdl.periph_name = '\0';
1179 ccb->cgdl.unit_number = 0;
1185 * Run through every peripheral instance of this driver
1186 * and check to see whether it matches the unit passed
1187 * in by the user. If it does, get out of the loops and
1188 * find the passthrough driver associated with that
1189 * peripheral driver.
1191 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1192 periph = TAILQ_NEXT(periph, unit_links)) {
1194 if (periph->unit_number == unit) {
1196 } else if (--splbreaknum == 0) {
1200 if (cur_generation != xsoftc.generation)
1205 * If we found the peripheral driver that the user passed
1206 * in, go through all of the peripheral drivers for that
1207 * particular device and look for a passthrough driver.
1209 if (periph != NULL) {
1210 struct cam_ed *device;
1213 base_periph_found = 1;
1214 device = periph->path->device;
1215 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1217 periph = SLIST_NEXT(periph, periph_links), i++) {
1219 * Check to see whether we have a
1220 * passthrough device or not.
1222 if (strcmp(periph->periph_name, "pass") == 0) {
1224 * Fill in the getdevlist fields.
1226 strcpy(ccb->cgdl.periph_name,
1227 periph->periph_name);
1228 ccb->cgdl.unit_number =
1229 periph->unit_number;
1230 if (SLIST_NEXT(periph, periph_links))
1232 CAM_GDEVLIST_MORE_DEVS;
1235 CAM_GDEVLIST_LAST_DEVICE;
1236 ccb->cgdl.generation =
1238 ccb->cgdl.index = i;
1240 * Fill in some CCB header fields
1241 * that the user may want.
1243 ccb->ccb_h.path_id =
1244 periph->path->bus->path_id;
1245 ccb->ccb_h.target_id =
1246 periph->path->target->target_id;
1247 ccb->ccb_h.target_lun =
1248 periph->path->device->lun_id;
1249 ccb->ccb_h.status = CAM_REQ_CMP;
1256 * If the periph is null here, one of two things has
1257 * happened. The first possibility is that we couldn't
1258 * find the unit number of the particular peripheral driver
1259 * that the user is asking about. e.g. the user asks for
1260 * the passthrough driver for "da11". We find the list of
1261 * "da" peripherals all right, but there is no unit 11.
1262 * The other possibility is that we went through the list
1263 * of peripheral drivers attached to the device structure,
1264 * but didn't find one with the name "pass". Either way,
1265 * we return ENOENT, since we couldn't find something.
1267 if (periph == NULL) {
1268 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1269 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1270 *ccb->cgdl.periph_name = '\0';
1271 ccb->cgdl.unit_number = 0;
1274 * It is unfortunate that this is even necessary,
1275 * but there are many, many clueless users out there.
1276 * If this is true, the user is looking for the
1277 * passthrough driver, but doesn't have one in his
1280 if (base_periph_found == 1) {
1281 printf("xptioctl: pass driver is not in the "
1283 printf("xptioctl: put \"device pass0\" in "
1284 "your kernel config file\n");
1299 cam_module_event_handler(module_t mod, int what, void *arg)
1301 if (what == MOD_LOAD) {
1303 } else if (what == MOD_UNLOAD) {
1310 /* Functions accessed by the peripheral drivers */
1315 struct cam_sim *xpt_sim;
1316 struct cam_path *path;
1317 struct cam_devq *devq;
1320 TAILQ_INIT(&xpt_busses);
1321 TAILQ_INIT(&cam_bioq);
1322 TAILQ_INIT(&cam_netq);
1323 SLIST_INIT(&ccb_freeq);
1324 STAILQ_INIT(&highpowerq);
1327 * The xpt layer is, itself, the equivelent of a SIM.
1328 * Allow 16 ccbs in the ccb pool for it. This should
1329 * give decent parallelism when we probe busses and
1330 * perform other XPT functions.
1332 devq = cam_simq_alloc(16);
1333 xpt_sim = cam_sim_alloc(xptaction,
1338 /*max_dev_transactions*/0,
1339 /*max_tagged_dev_transactions*/0,
1343 xpt_bus_register(xpt_sim, /*bus #*/0);
1346 * Looking at the XPT from the SIM layer, the XPT is
1347 * the equivelent of a peripheral driver. Allocate
1348 * a peripheral driver entry for us.
1350 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1351 CAM_TARGET_WILDCARD,
1352 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1353 printf("xpt_init: xpt_create_path failed with status %#x,"
1354 " failing attach\n", status);
1358 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1359 path, NULL, 0, NULL);
1360 xpt_free_path(path);
1362 xpt_sim->softc = xpt_periph;
1365 * Register a callback for when interrupts are enabled.
1368 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1369 M_TEMP, M_NOWAIT | M_ZERO);
1370 if (xpt_config_hook == NULL) {
1371 printf("xpt_init: Cannot malloc config hook "
1372 "- failing attach\n");
1376 xpt_config_hook->ich_func = xpt_config;
1377 if (config_intrhook_establish(xpt_config_hook) != 0) {
1378 free (xpt_config_hook, M_TEMP);
1379 printf("xpt_init: config_intrhook_establish failed "
1380 "- failing attach\n");
1383 /* Install our software interrupt handlers */
1384 swi_add(NULL, "camnet", camisr, &cam_netq, SWI_CAMNET, 0, &camnet_ih);
1385 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1389 xptregister(struct cam_periph *periph, void *arg)
1391 if (periph == NULL) {
1392 printf("xptregister: periph was NULL!!\n");
1393 return(CAM_REQ_CMP_ERR);
1396 periph->softc = NULL;
1398 xpt_periph = periph;
1400 return(CAM_REQ_CMP);
1404 xpt_add_periph(struct cam_periph *periph)
1406 struct cam_ed *device;
1408 struct periph_list *periph_head;
1410 device = periph->path->device;
1412 periph_head = &device->periphs;
1414 status = CAM_REQ_CMP;
1416 if (device != NULL) {
1420 * Make room for this peripheral
1421 * so it will fit in the queue
1422 * when it's scheduled to run
1425 status = camq_resize(&device->drvq,
1426 device->drvq.array_size + 1);
1428 device->generation++;
1430 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1435 xsoftc.generation++;
1441 xpt_remove_periph(struct cam_periph *periph)
1443 struct cam_ed *device;
1445 device = periph->path->device;
1447 if (device != NULL) {
1449 struct periph_list *periph_head;
1451 periph_head = &device->periphs;
1453 /* Release the slot for this peripheral */
1455 camq_resize(&device->drvq, device->drvq.array_size - 1);
1457 device->generation++;
1459 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1464 xsoftc.generation++;
1469 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1473 struct cam_path *path;
1474 struct ccb_trans_settings cts;
1476 path = periph->path;
1478 * To ensure that this is printed in one piece,
1479 * mask out CAM interrupts.
1482 printf("%s%d at %s%d bus %d target %d lun %d\n",
1483 periph->periph_name, periph->unit_number,
1484 path->bus->sim->sim_name,
1485 path->bus->sim->unit_number,
1486 path->bus->sim->bus_id,
1487 path->target->target_id,
1488 path->device->lun_id);
1489 printf("%s%d: ", periph->periph_name, periph->unit_number);
1490 scsi_print_inquiry(&path->device->inq_data);
1492 && (path->device->serial_num_len > 0)) {
1493 /* Don't wrap the screen - print only the first 60 chars */
1494 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1495 periph->unit_number, path->device->serial_num);
1497 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1498 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1499 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1500 xpt_action((union ccb*)&cts);
1501 if (cts.ccb_h.status == CAM_REQ_CMP) {
1505 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1506 && cts.sync_offset != 0) {
1507 freq = scsi_calc_syncsrate(cts.sync_period);
1510 struct ccb_pathinq cpi;
1512 /* Ask the SIM for its base transfer speed */
1513 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1514 cpi.ccb_h.func_code = XPT_PATH_INQ;
1515 xpt_action((union ccb *)&cpi);
1517 speed = cpi.base_transfer_speed;
1520 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1521 speed *= (0x01 << cts.bus_width);
1524 printf("%s%d: %d.%03dMB/s transfers",
1525 periph->periph_name, periph->unit_number,
1528 printf("%s%d: %dKB/s transfers", periph->periph_name,
1529 periph->unit_number, speed);
1530 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1531 && cts.sync_offset != 0) {
1532 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1533 freq % 1000, cts.sync_offset);
1535 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1536 && cts.bus_width > 0) {
1537 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1538 && cts.sync_offset != 0) {
1543 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1544 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1545 && cts.sync_offset != 0) {
1549 if (path->device->inq_flags & SID_CmdQue
1550 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1551 printf(", Tagged Queueing Enabled");
1555 } else if (path->device->inq_flags & SID_CmdQue
1556 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1557 printf("%s%d: Tagged Queueing Enabled\n",
1558 periph->periph_name, periph->unit_number);
1562 * We only want to print the caller's announce string if they've
1565 if (announce_string != NULL)
1566 printf("%s%d: %s\n", periph->periph_name,
1567 periph->unit_number, announce_string);
1572 static dev_match_ret
1573 xptbusmatch(struct dev_match_pattern *patterns, int num_patterns,
1576 dev_match_ret retval;
1579 retval = DM_RET_NONE;
1582 * If we aren't given something to match against, that's an error.
1585 return(DM_RET_ERROR);
1588 * If there are no match entries, then this bus matches no
1591 if ((patterns == NULL) || (num_patterns == 0))
1592 return(DM_RET_DESCEND | DM_RET_COPY);
1594 for (i = 0; i < num_patterns; i++) {
1595 struct bus_match_pattern *cur_pattern;
1598 * If the pattern in question isn't for a bus node, we
1599 * aren't interested. However, we do indicate to the
1600 * calling routine that we should continue descending the
1601 * tree, since the user wants to match against lower-level
1604 if (patterns[i].type != DEV_MATCH_BUS) {
1605 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1606 retval |= DM_RET_DESCEND;
1610 cur_pattern = &patterns[i].pattern.bus_pattern;
1613 * If they want to match any bus node, we give them any
1616 if (cur_pattern->flags == BUS_MATCH_ANY) {
1617 /* set the copy flag */
1618 retval |= DM_RET_COPY;
1621 * If we've already decided on an action, go ahead
1624 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1629 * Not sure why someone would do this...
1631 if (cur_pattern->flags == BUS_MATCH_NONE)
1634 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1635 && (cur_pattern->path_id != bus->path_id))
1638 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1639 && (cur_pattern->bus_id != bus->sim->bus_id))
1642 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1643 && (cur_pattern->unit_number != bus->sim->unit_number))
1646 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1647 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1652 * If we get to this point, the user definitely wants
1653 * information on this bus. So tell the caller to copy the
1656 retval |= DM_RET_COPY;
1659 * If the return action has been set to descend, then we
1660 * know that we've already seen a non-bus matching
1661 * expression, therefore we need to further descend the tree.
1662 * This won't change by continuing around the loop, so we
1663 * go ahead and return. If we haven't seen a non-bus
1664 * matching expression, we keep going around the loop until
1665 * we exhaust the matching expressions. We'll set the stop
1666 * flag once we fall out of the loop.
1668 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1673 * If the return action hasn't been set to descend yet, that means
1674 * we haven't seen anything other than bus matching patterns. So
1675 * tell the caller to stop descending the tree -- the user doesn't
1676 * want to match against lower level tree elements.
1678 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1679 retval |= DM_RET_STOP;
1684 static dev_match_ret
1685 xptdevicematch(struct dev_match_pattern *patterns, int num_patterns,
1686 struct cam_ed *device)
1688 dev_match_ret retval;
1691 retval = DM_RET_NONE;
1694 * If we aren't given something to match against, that's an error.
1697 return(DM_RET_ERROR);
1700 * If there are no match entries, then this device matches no
1703 if ((patterns == NULL) || (patterns == 0))
1704 return(DM_RET_DESCEND | DM_RET_COPY);
1706 for (i = 0; i < num_patterns; i++) {
1707 struct device_match_pattern *cur_pattern;
1710 * If the pattern in question isn't for a device node, we
1711 * aren't interested.
1713 if (patterns[i].type != DEV_MATCH_DEVICE) {
1714 if ((patterns[i].type == DEV_MATCH_PERIPH)
1715 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1716 retval |= DM_RET_DESCEND;
1720 cur_pattern = &patterns[i].pattern.device_pattern;
1723 * If they want to match any device node, we give them any
1726 if (cur_pattern->flags == DEV_MATCH_ANY) {
1727 /* set the copy flag */
1728 retval |= DM_RET_COPY;
1732 * If we've already decided on an action, go ahead
1735 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1740 * Not sure why someone would do this...
1742 if (cur_pattern->flags == DEV_MATCH_NONE)
1745 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1746 && (cur_pattern->path_id != device->target->bus->path_id))
1749 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1750 && (cur_pattern->target_id != device->target->target_id))
1753 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1754 && (cur_pattern->target_lun != device->lun_id))
1757 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1758 && (cam_quirkmatch((caddr_t)&device->inq_data,
1759 (caddr_t)&cur_pattern->inq_pat,
1760 1, sizeof(cur_pattern->inq_pat),
1761 scsi_static_inquiry_match) == NULL))
1765 * If we get to this point, the user definitely wants
1766 * information on this device. So tell the caller to copy
1769 retval |= DM_RET_COPY;
1772 * If the return action has been set to descend, then we
1773 * know that we've already seen a peripheral matching
1774 * expression, therefore we need to further descend the tree.
1775 * This won't change by continuing around the loop, so we
1776 * go ahead and return. If we haven't seen a peripheral
1777 * matching expression, we keep going around the loop until
1778 * we exhaust the matching expressions. We'll set the stop
1779 * flag once we fall out of the loop.
1781 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1786 * If the return action hasn't been set to descend yet, that means
1787 * we haven't seen any peripheral matching patterns. So tell the
1788 * caller to stop descending the tree -- the user doesn't want to
1789 * match against lower level tree elements.
1791 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1792 retval |= DM_RET_STOP;
1798 * Match a single peripheral against any number of match patterns.
1800 static dev_match_ret
1801 xptperiphmatch(struct dev_match_pattern *patterns, int num_patterns,
1802 struct cam_periph *periph)
1804 dev_match_ret retval;
1808 * If we aren't given something to match against, that's an error.
1811 return(DM_RET_ERROR);
1814 * If there are no match entries, then this peripheral matches no
1817 if ((patterns == NULL) || (num_patterns == 0))
1818 return(DM_RET_STOP | DM_RET_COPY);
1821 * There aren't any nodes below a peripheral node, so there's no
1822 * reason to descend the tree any further.
1824 retval = DM_RET_STOP;
1826 for (i = 0; i < num_patterns; i++) {
1827 struct periph_match_pattern *cur_pattern;
1830 * If the pattern in question isn't for a peripheral, we
1831 * aren't interested.
1833 if (patterns[i].type != DEV_MATCH_PERIPH)
1836 cur_pattern = &patterns[i].pattern.periph_pattern;
1839 * If they want to match on anything, then we will do so.
1841 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1842 /* set the copy flag */
1843 retval |= DM_RET_COPY;
1846 * We've already set the return action to stop,
1847 * since there are no nodes below peripherals in
1854 * Not sure why someone would do this...
1856 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1859 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1860 && (cur_pattern->path_id != periph->path->bus->path_id))
1864 * For the target and lun id's, we have to make sure the
1865 * target and lun pointers aren't NULL. The xpt peripheral
1866 * has a wildcard target and device.
1868 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1869 && ((periph->path->target == NULL)
1870 ||(cur_pattern->target_id != periph->path->target->target_id)))
1873 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1874 && ((periph->path->device == NULL)
1875 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1878 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1879 && (cur_pattern->unit_number != periph->unit_number))
1882 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1883 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1888 * If we get to this point, the user definitely wants
1889 * information on this peripheral. So tell the caller to
1890 * copy the data out.
1892 retval |= DM_RET_COPY;
1895 * The return action has already been set to stop, since
1896 * peripherals don't have any nodes below them in the EDT.
1902 * If we get to this point, the peripheral that was passed in
1903 * doesn't match any of the patterns.
1909 xptedtbusfunc(struct cam_eb *bus, void *arg)
1911 struct ccb_dev_match *cdm;
1912 dev_match_ret retval;
1914 cdm = (struct ccb_dev_match *)arg;
1917 * If our position is for something deeper in the tree, that means
1918 * that we've already seen this node. So, we keep going down.
1920 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1921 && (cdm->pos.cookie.bus == bus)
1922 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1923 && (cdm->pos.cookie.target != NULL))
1924 retval = DM_RET_DESCEND;
1926 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1929 * If we got an error, bail out of the search.
1931 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1932 cdm->status = CAM_DEV_MATCH_ERROR;
1937 * If the copy flag is set, copy this bus out.
1939 if (retval & DM_RET_COPY) {
1942 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1943 sizeof(struct dev_match_result));
1946 * If we don't have enough space to put in another
1947 * match result, save our position and tell the
1948 * user there are more devices to check.
1950 if (spaceleft < sizeof(struct dev_match_result)) {
1951 bzero(&cdm->pos, sizeof(cdm->pos));
1952 cdm->pos.position_type =
1953 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1955 cdm->pos.cookie.bus = bus;
1956 cdm->pos.generations[CAM_BUS_GENERATION]=
1958 cdm->status = CAM_DEV_MATCH_MORE;
1961 j = cdm->num_matches;
1963 cdm->matches[j].type = DEV_MATCH_BUS;
1964 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1965 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1966 cdm->matches[j].result.bus_result.unit_number =
1967 bus->sim->unit_number;
1968 strncpy(cdm->matches[j].result.bus_result.dev_name,
1969 bus->sim->sim_name, DEV_IDLEN);
1973 * If the user is only interested in busses, there's no
1974 * reason to descend to the next level in the tree.
1976 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1980 * If there is a target generation recorded, check it to
1981 * make sure the target list hasn't changed.
1983 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1984 && (bus == cdm->pos.cookie.bus)
1985 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1986 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1987 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1989 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1993 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1994 && (cdm->pos.cookie.bus == bus)
1995 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1996 && (cdm->pos.cookie.target != NULL))
1997 return(xpttargettraverse(bus,
1998 (struct cam_et *)cdm->pos.cookie.target,
1999 xptedttargetfunc, arg));
2001 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2005 xptedttargetfunc(struct cam_et *target, void *arg)
2007 struct ccb_dev_match *cdm;
2009 cdm = (struct ccb_dev_match *)arg;
2012 * If there is a device list generation recorded, check it to
2013 * make sure the device list hasn't changed.
2015 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2016 && (cdm->pos.cookie.bus == target->bus)
2017 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2018 && (cdm->pos.cookie.target == target)
2019 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2020 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2021 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2022 target->generation)) {
2023 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2027 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2028 && (cdm->pos.cookie.bus == target->bus)
2029 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2030 && (cdm->pos.cookie.target == target)
2031 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2032 && (cdm->pos.cookie.device != NULL))
2033 return(xptdevicetraverse(target,
2034 (struct cam_ed *)cdm->pos.cookie.device,
2035 xptedtdevicefunc, arg));
2037 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2041 xptedtdevicefunc(struct cam_ed *device, void *arg)
2044 struct ccb_dev_match *cdm;
2045 dev_match_ret retval;
2047 cdm = (struct ccb_dev_match *)arg;
2050 * If our position is for something deeper in the tree, that means
2051 * that we've already seen this node. So, we keep going down.
2053 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2054 && (cdm->pos.cookie.device == device)
2055 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2056 && (cdm->pos.cookie.periph != NULL))
2057 retval = DM_RET_DESCEND;
2059 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2062 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2063 cdm->status = CAM_DEV_MATCH_ERROR;
2068 * If the copy flag is set, copy this device out.
2070 if (retval & DM_RET_COPY) {
2073 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2074 sizeof(struct dev_match_result));
2077 * If we don't have enough space to put in another
2078 * match result, save our position and tell the
2079 * user there are more devices to check.
2081 if (spaceleft < sizeof(struct dev_match_result)) {
2082 bzero(&cdm->pos, sizeof(cdm->pos));
2083 cdm->pos.position_type =
2084 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2085 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2087 cdm->pos.cookie.bus = device->target->bus;
2088 cdm->pos.generations[CAM_BUS_GENERATION]=
2090 cdm->pos.cookie.target = device->target;
2091 cdm->pos.generations[CAM_TARGET_GENERATION] =
2092 device->target->bus->generation;
2093 cdm->pos.cookie.device = device;
2094 cdm->pos.generations[CAM_DEV_GENERATION] =
2095 device->target->generation;
2096 cdm->status = CAM_DEV_MATCH_MORE;
2099 j = cdm->num_matches;
2101 cdm->matches[j].type = DEV_MATCH_DEVICE;
2102 cdm->matches[j].result.device_result.path_id =
2103 device->target->bus->path_id;
2104 cdm->matches[j].result.device_result.target_id =
2105 device->target->target_id;
2106 cdm->matches[j].result.device_result.target_lun =
2108 bcopy(&device->inq_data,
2109 &cdm->matches[j].result.device_result.inq_data,
2110 sizeof(struct scsi_inquiry_data));
2112 /* Let the user know whether this device is unconfigured */
2113 if (device->flags & CAM_DEV_UNCONFIGURED)
2114 cdm->matches[j].result.device_result.flags =
2115 DEV_RESULT_UNCONFIGURED;
2117 cdm->matches[j].result.device_result.flags =
2122 * If the user isn't interested in peripherals, don't descend
2123 * the tree any further.
2125 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2129 * If there is a peripheral list generation recorded, make sure
2130 * it hasn't changed.
2132 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2133 && (device->target->bus == cdm->pos.cookie.bus)
2134 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2135 && (device->target == cdm->pos.cookie.target)
2136 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2137 && (device == cdm->pos.cookie.device)
2138 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2139 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2140 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2141 device->generation)){
2142 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2146 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2147 && (cdm->pos.cookie.bus == device->target->bus)
2148 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2149 && (cdm->pos.cookie.target == device->target)
2150 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2151 && (cdm->pos.cookie.device == device)
2152 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2153 && (cdm->pos.cookie.periph != NULL))
2154 return(xptperiphtraverse(device,
2155 (struct cam_periph *)cdm->pos.cookie.periph,
2156 xptedtperiphfunc, arg));
2158 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2162 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2164 struct ccb_dev_match *cdm;
2165 dev_match_ret retval;
2167 cdm = (struct ccb_dev_match *)arg;
2169 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2171 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2172 cdm->status = CAM_DEV_MATCH_ERROR;
2177 * If the copy flag is set, copy this peripheral out.
2179 if (retval & DM_RET_COPY) {
2182 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2183 sizeof(struct dev_match_result));
2186 * If we don't have enough space to put in another
2187 * match result, save our position and tell the
2188 * user there are more devices to check.
2190 if (spaceleft < sizeof(struct dev_match_result)) {
2191 bzero(&cdm->pos, sizeof(cdm->pos));
2192 cdm->pos.position_type =
2193 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2194 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2197 cdm->pos.cookie.bus = periph->path->bus;
2198 cdm->pos.generations[CAM_BUS_GENERATION]=
2200 cdm->pos.cookie.target = periph->path->target;
2201 cdm->pos.generations[CAM_TARGET_GENERATION] =
2202 periph->path->bus->generation;
2203 cdm->pos.cookie.device = periph->path->device;
2204 cdm->pos.generations[CAM_DEV_GENERATION] =
2205 periph->path->target->generation;
2206 cdm->pos.cookie.periph = periph;
2207 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2208 periph->path->device->generation;
2209 cdm->status = CAM_DEV_MATCH_MORE;
2213 j = cdm->num_matches;
2215 cdm->matches[j].type = DEV_MATCH_PERIPH;
2216 cdm->matches[j].result.periph_result.path_id =
2217 periph->path->bus->path_id;
2218 cdm->matches[j].result.periph_result.target_id =
2219 periph->path->target->target_id;
2220 cdm->matches[j].result.periph_result.target_lun =
2221 periph->path->device->lun_id;
2222 cdm->matches[j].result.periph_result.unit_number =
2223 periph->unit_number;
2224 strncpy(cdm->matches[j].result.periph_result.periph_name,
2225 periph->periph_name, DEV_IDLEN);
2232 xptedtmatch(struct ccb_dev_match *cdm)
2236 cdm->num_matches = 0;
2239 * Check the bus list generation. If it has changed, the user
2240 * needs to reset everything and start over.
2242 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2243 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2244 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2245 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2249 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2250 && (cdm->pos.cookie.bus != NULL))
2251 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2252 xptedtbusfunc, cdm);
2254 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2257 * If we get back 0, that means that we had to stop before fully
2258 * traversing the EDT. It also means that one of the subroutines
2259 * has set the status field to the proper value. If we get back 1,
2260 * we've fully traversed the EDT and copied out any matching entries.
2263 cdm->status = CAM_DEV_MATCH_LAST;
2269 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2271 struct ccb_dev_match *cdm;
2273 cdm = (struct ccb_dev_match *)arg;
2275 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2276 && (cdm->pos.cookie.pdrv == pdrv)
2277 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2278 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2279 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2280 (*pdrv)->generation)) {
2281 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2285 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2286 && (cdm->pos.cookie.pdrv == pdrv)
2287 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2288 && (cdm->pos.cookie.periph != NULL))
2289 return(xptpdperiphtraverse(pdrv,
2290 (struct cam_periph *)cdm->pos.cookie.periph,
2291 xptplistperiphfunc, arg));
2293 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2297 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2299 struct ccb_dev_match *cdm;
2300 dev_match_ret retval;
2302 cdm = (struct ccb_dev_match *)arg;
2304 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2306 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2307 cdm->status = CAM_DEV_MATCH_ERROR;
2312 * If the copy flag is set, copy this peripheral out.
2314 if (retval & DM_RET_COPY) {
2317 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2318 sizeof(struct dev_match_result));
2321 * If we don't have enough space to put in another
2322 * match result, save our position and tell the
2323 * user there are more devices to check.
2325 if (spaceleft < sizeof(struct dev_match_result)) {
2326 struct periph_driver **pdrv;
2329 bzero(&cdm->pos, sizeof(cdm->pos));
2330 cdm->pos.position_type =
2331 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2335 * This may look a bit non-sensical, but it is
2336 * actually quite logical. There are very few
2337 * peripheral drivers, and bloating every peripheral
2338 * structure with a pointer back to its parent
2339 * peripheral driver linker set entry would cost
2340 * more in the long run than doing this quick lookup.
2342 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2343 if (strcmp((*pdrv)->driver_name,
2344 periph->periph_name) == 0)
2349 cdm->status = CAM_DEV_MATCH_ERROR;
2353 cdm->pos.cookie.pdrv = pdrv;
2355 * The periph generation slot does double duty, as
2356 * does the periph pointer slot. They are used for
2357 * both edt and pdrv lookups and positioning.
2359 cdm->pos.cookie.periph = periph;
2360 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2361 (*pdrv)->generation;
2362 cdm->status = CAM_DEV_MATCH_MORE;
2366 j = cdm->num_matches;
2368 cdm->matches[j].type = DEV_MATCH_PERIPH;
2369 cdm->matches[j].result.periph_result.path_id =
2370 periph->path->bus->path_id;
2373 * The transport layer peripheral doesn't have a target or
2376 if (periph->path->target)
2377 cdm->matches[j].result.periph_result.target_id =
2378 periph->path->target->target_id;
2380 cdm->matches[j].result.periph_result.target_id = -1;
2382 if (periph->path->device)
2383 cdm->matches[j].result.periph_result.target_lun =
2384 periph->path->device->lun_id;
2386 cdm->matches[j].result.periph_result.target_lun = -1;
2388 cdm->matches[j].result.periph_result.unit_number =
2389 periph->unit_number;
2390 strncpy(cdm->matches[j].result.periph_result.periph_name,
2391 periph->periph_name, DEV_IDLEN);
2398 xptperiphlistmatch(struct ccb_dev_match *cdm)
2402 cdm->num_matches = 0;
2405 * At this point in the edt traversal function, we check the bus
2406 * list generation to make sure that no busses have been added or
2407 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2408 * For the peripheral driver list traversal function, however, we
2409 * don't have to worry about new peripheral driver types coming or
2410 * going; they're in a linker set, and therefore can't change
2411 * without a recompile.
2414 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2415 && (cdm->pos.cookie.pdrv != NULL))
2416 ret = xptpdrvtraverse(
2417 (struct periph_driver **)cdm->pos.cookie.pdrv,
2418 xptplistpdrvfunc, cdm);
2420 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2423 * If we get back 0, that means that we had to stop before fully
2424 * traversing the peripheral driver tree. It also means that one of
2425 * the subroutines has set the status field to the proper value. If
2426 * we get back 1, we've fully traversed the EDT and copied out any
2430 cdm->status = CAM_DEV_MATCH_LAST;
2436 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2438 struct cam_eb *bus, *next_bus;
2443 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2446 next_bus = TAILQ_NEXT(bus, links);
2448 retval = tr_func(bus, arg);
2457 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2458 xpt_targetfunc_t *tr_func, void *arg)
2460 struct cam_et *target, *next_target;
2464 for (target = (start_target ? start_target :
2465 TAILQ_FIRST(&bus->et_entries));
2466 target != NULL; target = next_target) {
2468 next_target = TAILQ_NEXT(target, links);
2470 retval = tr_func(target, arg);
2480 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2481 xpt_devicefunc_t *tr_func, void *arg)
2483 struct cam_ed *device, *next_device;
2487 for (device = (start_device ? start_device :
2488 TAILQ_FIRST(&target->ed_entries));
2490 device = next_device) {
2492 next_device = TAILQ_NEXT(device, links);
2494 retval = tr_func(device, arg);
2504 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2505 xpt_periphfunc_t *tr_func, void *arg)
2507 struct cam_periph *periph, *next_periph;
2512 for (periph = (start_periph ? start_periph :
2513 SLIST_FIRST(&device->periphs));
2515 periph = next_periph) {
2517 next_periph = SLIST_NEXT(periph, periph_links);
2519 retval = tr_func(periph, arg);
2528 xptpdrvtraverse(struct periph_driver **start_pdrv,
2529 xpt_pdrvfunc_t *tr_func, void *arg)
2531 struct periph_driver **pdrv;
2537 * We don't traverse the peripheral driver list like we do the
2538 * other lists, because it is a linker set, and therefore cannot be
2539 * changed during runtime. If the peripheral driver list is ever
2540 * re-done to be something other than a linker set (i.e. it can
2541 * change while the system is running), the list traversal should
2542 * be modified to work like the other traversal functions.
2544 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2545 *pdrv != NULL; pdrv++) {
2546 retval = tr_func(pdrv, arg);
2556 xptpdperiphtraverse(struct periph_driver **pdrv,
2557 struct cam_periph *start_periph,
2558 xpt_periphfunc_t *tr_func, void *arg)
2560 struct cam_periph *periph, *next_periph;
2565 for (periph = (start_periph ? start_periph :
2566 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2567 periph = next_periph) {
2569 next_periph = TAILQ_NEXT(periph, unit_links);
2571 retval = tr_func(periph, arg);
2579 xptdefbusfunc(struct cam_eb *bus, void *arg)
2581 struct xpt_traverse_config *tr_config;
2583 tr_config = (struct xpt_traverse_config *)arg;
2585 if (tr_config->depth == XPT_DEPTH_BUS) {
2586 xpt_busfunc_t *tr_func;
2588 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2590 return(tr_func(bus, tr_config->tr_arg));
2592 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2596 xptdeftargetfunc(struct cam_et *target, void *arg)
2598 struct xpt_traverse_config *tr_config;
2600 tr_config = (struct xpt_traverse_config *)arg;
2602 if (tr_config->depth == XPT_DEPTH_TARGET) {
2603 xpt_targetfunc_t *tr_func;
2605 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2607 return(tr_func(target, tr_config->tr_arg));
2609 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2613 xptdefdevicefunc(struct cam_ed *device, void *arg)
2615 struct xpt_traverse_config *tr_config;
2617 tr_config = (struct xpt_traverse_config *)arg;
2619 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2620 xpt_devicefunc_t *tr_func;
2622 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2624 return(tr_func(device, tr_config->tr_arg));
2626 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2630 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2632 struct xpt_traverse_config *tr_config;
2633 xpt_periphfunc_t *tr_func;
2635 tr_config = (struct xpt_traverse_config *)arg;
2637 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2640 * Unlike the other default functions, we don't check for depth
2641 * here. The peripheral driver level is the last level in the EDT,
2642 * so if we're here, we should execute the function in question.
2644 return(tr_func(periph, tr_config->tr_arg));
2648 * Execute the given function for every bus in the EDT.
2651 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2653 struct xpt_traverse_config tr_config;
2655 tr_config.depth = XPT_DEPTH_BUS;
2656 tr_config.tr_func = tr_func;
2657 tr_config.tr_arg = arg;
2659 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2664 * Execute the given function for every target in the EDT.
2667 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2669 struct xpt_traverse_config tr_config;
2671 tr_config.depth = XPT_DEPTH_TARGET;
2672 tr_config.tr_func = tr_func;
2673 tr_config.tr_arg = arg;
2675 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2677 #endif /* notusedyet */
2680 * Execute the given function for every device in the EDT.
2683 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2685 struct xpt_traverse_config tr_config;
2687 tr_config.depth = XPT_DEPTH_DEVICE;
2688 tr_config.tr_func = tr_func;
2689 tr_config.tr_arg = arg;
2691 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2696 * Execute the given function for every peripheral in the EDT.
2699 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2701 struct xpt_traverse_config tr_config;
2703 tr_config.depth = XPT_DEPTH_PERIPH;
2704 tr_config.tr_func = tr_func;
2705 tr_config.tr_arg = arg;
2707 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2709 #endif /* notusedyet */
2712 xptsetasyncfunc(struct cam_ed *device, void *arg)
2714 struct cam_path path;
2715 struct ccb_getdev cgd;
2716 struct async_node *cur_entry;
2718 cur_entry = (struct async_node *)arg;
2721 * Don't report unconfigured devices (Wildcard devs,
2722 * devices only for target mode, device instances
2723 * that have been invalidated but are waiting for
2724 * their last reference count to be released).
2726 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2729 xpt_compile_path(&path,
2731 device->target->bus->path_id,
2732 device->target->target_id,
2734 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2735 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2736 xpt_action((union ccb *)&cgd);
2737 cur_entry->callback(cur_entry->callback_arg,
2740 xpt_release_path(&path);
2746 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2748 struct cam_path path;
2749 struct ccb_pathinq cpi;
2750 struct async_node *cur_entry;
2752 cur_entry = (struct async_node *)arg;
2754 xpt_compile_path(&path, /*periph*/NULL,
2756 CAM_TARGET_WILDCARD,
2758 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2759 cpi.ccb_h.func_code = XPT_PATH_INQ;
2760 xpt_action((union ccb *)&cpi);
2761 cur_entry->callback(cur_entry->callback_arg,
2764 xpt_release_path(&path);
2770 xpt_action(union ccb *start_ccb)
2774 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2776 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2778 iopl = splsoftcam();
2779 switch (start_ccb->ccb_h.func_code) {
2783 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2784 struct cam_path *path;
2786 path = start_ccb->ccb_h.path;
2790 * For the sake of compatibility with SCSI-1
2791 * devices that may not understand the identify
2792 * message, we include lun information in the
2793 * second byte of all commands. SCSI-1 specifies
2794 * that luns are a 3 bit value and reserves only 3
2795 * bits for lun information in the CDB. Later
2796 * revisions of the SCSI spec allow for more than 8
2797 * luns, but have deprecated lun information in the
2798 * CDB. So, if the lun won't fit, we must omit.
2800 * Also be aware that during initial probing for devices,
2801 * the inquiry information is unknown but initialized to 0.
2802 * This means that this code will be exercised while probing
2803 * devices with an ANSI revision greater than 2.
2805 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2806 && start_ccb->ccb_h.target_lun < 8
2807 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2809 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2810 start_ccb->ccb_h.target_lun << 5;
2812 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2813 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2814 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2815 &path->device->inq_data),
2816 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2817 cdb_str, sizeof(cdb_str))));
2821 case XPT_CONT_TARGET_IO:
2822 start_ccb->csio.sense_resid = 0;
2823 start_ccb->csio.resid = 0;
2828 struct cam_path *path;
2832 path = start_ccb->ccb_h.path;
2835 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2836 if (path->device->qfrozen_cnt == 0)
2837 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2842 xpt_run_dev_sendq(path->bus);
2845 case XPT_SET_TRAN_SETTINGS:
2847 xpt_set_transfer_settings(&start_ccb->cts,
2848 start_ccb->ccb_h.path->device,
2849 /*async_update*/FALSE);
2852 case XPT_CALC_GEOMETRY:
2854 struct cam_sim *sim;
2856 /* Filter out garbage */
2857 if (start_ccb->ccg.block_size == 0
2858 || start_ccb->ccg.volume_size == 0) {
2859 start_ccb->ccg.cylinders = 0;
2860 start_ccb->ccg.heads = 0;
2861 start_ccb->ccg.secs_per_track = 0;
2862 start_ccb->ccb_h.status = CAM_REQ_CMP;
2867 * In a PC-98 system, geometry translation depens on
2868 * the "real" device geometry obtained from mode page 4.
2869 * SCSI geometry translation is performed in the
2870 * initialization routine of the SCSI BIOS and the result
2871 * stored in host memory. If the translation is available
2872 * in host memory, use it. If not, rely on the default
2873 * translation the device driver performs.
2875 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2876 start_ccb->ccb_h.status = CAM_REQ_CMP;
2880 sim = start_ccb->ccb_h.path->bus->sim;
2881 (*(sim->sim_action))(sim, start_ccb);
2886 union ccb* abort_ccb;
2889 abort_ccb = start_ccb->cab.abort_ccb;
2890 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2892 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2893 struct cam_ccbq *ccbq;
2895 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2896 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2897 abort_ccb->ccb_h.status =
2898 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2899 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2901 xpt_done(abort_ccb);
2903 start_ccb->ccb_h.status = CAM_REQ_CMP;
2906 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2907 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2909 * We've caught this ccb en route to
2910 * the SIM. Flag it for abort and the
2911 * SIM will do so just before starting
2912 * real work on the CCB.
2914 abort_ccb->ccb_h.status =
2915 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2916 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2917 start_ccb->ccb_h.status = CAM_REQ_CMP;
2921 if (XPT_FC_IS_QUEUED(abort_ccb)
2922 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2924 * It's already completed but waiting
2925 * for our SWI to get to it.
2927 start_ccb->ccb_h.status = CAM_UA_ABORT;
2931 * If we weren't able to take care of the abort request
2932 * in the XPT, pass the request down to the SIM for processing.
2936 case XPT_ACCEPT_TARGET_IO:
2938 case XPT_IMMED_NOTIFY:
2939 case XPT_NOTIFY_ACK:
2940 case XPT_GET_TRAN_SETTINGS:
2943 struct cam_sim *sim;
2945 sim = start_ccb->ccb_h.path->bus->sim;
2946 (*(sim->sim_action))(sim, start_ccb);
2951 struct cam_sim *sim;
2953 sim = start_ccb->ccb_h.path->bus->sim;
2954 (*(sim->sim_action))(sim, start_ccb);
2957 case XPT_PATH_STATS:
2958 start_ccb->cpis.last_reset =
2959 start_ccb->ccb_h.path->bus->last_reset;
2960 start_ccb->ccb_h.status = CAM_REQ_CMP;
2967 dev = start_ccb->ccb_h.path->device;
2969 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2970 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2972 struct ccb_getdev *cgd;
2976 cgd = &start_ccb->cgd;
2977 bus = cgd->ccb_h.path->bus;
2978 tar = cgd->ccb_h.path->target;
2979 cgd->inq_data = dev->inq_data;
2980 cgd->ccb_h.status = CAM_REQ_CMP;
2981 cgd->serial_num_len = dev->serial_num_len;
2982 if ((dev->serial_num_len > 0)
2983 && (dev->serial_num != NULL))
2984 bcopy(dev->serial_num, cgd->serial_num,
2985 dev->serial_num_len);
2990 case XPT_GDEV_STATS:
2995 dev = start_ccb->ccb_h.path->device;
2997 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2998 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3000 struct ccb_getdevstats *cgds;
3004 cgds = &start_ccb->cgds;
3005 bus = cgds->ccb_h.path->bus;
3006 tar = cgds->ccb_h.path->target;
3007 cgds->dev_openings = dev->ccbq.dev_openings;
3008 cgds->dev_active = dev->ccbq.dev_active;
3009 cgds->devq_openings = dev->ccbq.devq_openings;
3010 cgds->devq_queued = dev->ccbq.queue.entries;
3011 cgds->held = dev->ccbq.held;
3012 cgds->last_reset = tar->last_reset;
3013 cgds->maxtags = dev->quirk->maxtags;
3014 cgds->mintags = dev->quirk->mintags;
3015 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3016 cgds->last_reset = bus->last_reset;
3017 cgds->ccb_h.status = CAM_REQ_CMP;
3024 struct cam_periph *nperiph;
3025 struct periph_list *periph_head;
3026 struct ccb_getdevlist *cgdl;
3029 struct cam_ed *device;
3036 * Don't want anyone mucking with our data.
3039 device = start_ccb->ccb_h.path->device;
3040 periph_head = &device->periphs;
3041 cgdl = &start_ccb->cgdl;
3044 * Check and see if the list has changed since the user
3045 * last requested a list member. If so, tell them that the
3046 * list has changed, and therefore they need to start over
3047 * from the beginning.
3049 if ((cgdl->index != 0) &&
3050 (cgdl->generation != device->generation)) {
3051 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3057 * Traverse the list of peripherals and attempt to find
3058 * the requested peripheral.
3060 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3061 (nperiph != NULL) && (i <= cgdl->index);
3062 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3063 if (i == cgdl->index) {
3064 strncpy(cgdl->periph_name,
3065 nperiph->periph_name,
3067 cgdl->unit_number = nperiph->unit_number;
3072 cgdl->status = CAM_GDEVLIST_ERROR;
3077 if (nperiph == NULL)
3078 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3080 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3083 cgdl->generation = device->generation;
3086 cgdl->ccb_h.status = CAM_REQ_CMP;
3092 dev_pos_type position_type;
3093 struct ccb_dev_match *cdm;
3096 cdm = &start_ccb->cdm;
3099 * Prevent EDT changes while we traverse it.
3103 * There are two ways of getting at information in the EDT.
3104 * The first way is via the primary EDT tree. It starts
3105 * with a list of busses, then a list of targets on a bus,
3106 * then devices/luns on a target, and then peripherals on a
3107 * device/lun. The "other" way is by the peripheral driver
3108 * lists. The peripheral driver lists are organized by
3109 * peripheral driver. (obviously) So it makes sense to
3110 * use the peripheral driver list if the user is looking
3111 * for something like "da1", or all "da" devices. If the
3112 * user is looking for something on a particular bus/target
3113 * or lun, it's generally better to go through the EDT tree.
3116 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3117 position_type = cdm->pos.position_type;
3121 position_type = CAM_DEV_POS_NONE;
3123 for (i = 0; i < cdm->num_patterns; i++) {
3124 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3125 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3126 position_type = CAM_DEV_POS_EDT;
3131 if (cdm->num_patterns == 0)
3132 position_type = CAM_DEV_POS_EDT;
3133 else if (position_type == CAM_DEV_POS_NONE)
3134 position_type = CAM_DEV_POS_PDRV;
3137 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3138 case CAM_DEV_POS_EDT:
3139 ret = xptedtmatch(cdm);
3141 case CAM_DEV_POS_PDRV:
3142 ret = xptperiphlistmatch(cdm);
3145 cdm->status = CAM_DEV_MATCH_ERROR;
3151 if (cdm->status == CAM_DEV_MATCH_ERROR)
3152 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3154 start_ccb->ccb_h.status = CAM_REQ_CMP;
3160 struct ccb_setasync *csa;
3161 struct async_node *cur_entry;
3162 struct async_list *async_head;
3166 csa = &start_ccb->csa;
3167 added = csa->event_enable;
3168 async_head = &csa->ccb_h.path->device->asyncs;
3171 * If there is already an entry for us, simply
3175 cur_entry = SLIST_FIRST(async_head);
3176 while (cur_entry != NULL) {
3177 if ((cur_entry->callback_arg == csa->callback_arg)
3178 && (cur_entry->callback == csa->callback))
3180 cur_entry = SLIST_NEXT(cur_entry, links);
3183 if (cur_entry != NULL) {
3185 * If the request has no flags set,
3188 added &= ~cur_entry->event_enable;
3189 if (csa->event_enable == 0) {
3190 SLIST_REMOVE(async_head, cur_entry,
3192 csa->ccb_h.path->device->refcount--;
3193 free(cur_entry, M_DEVBUF);
3195 cur_entry->event_enable = csa->event_enable;
3198 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3200 if (cur_entry == NULL) {
3202 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3205 cur_entry->event_enable = csa->event_enable;
3206 cur_entry->callback_arg = csa->callback_arg;
3207 cur_entry->callback = csa->callback;
3208 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3209 csa->ccb_h.path->device->refcount++;
3212 if ((added & AC_FOUND_DEVICE) != 0) {
3214 * Get this peripheral up to date with all
3215 * the currently existing devices.
3217 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3219 if ((added & AC_PATH_REGISTERED) != 0) {
3221 * Get this peripheral up to date with all
3222 * the currently existing busses.
3224 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3227 start_ccb->ccb_h.status = CAM_REQ_CMP;
3232 struct ccb_relsim *crs;
3236 crs = &start_ccb->crs;
3237 dev = crs->ccb_h.path->device;
3240 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3246 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3248 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3250 /* Don't ever go below one opening */
3251 if (crs->openings > 0) {
3252 xpt_dev_ccbq_resize(crs->ccb_h.path,
3256 xpt_print_path(crs->ccb_h.path);
3257 printf("tagged openings "
3265 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3267 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3270 * Just extend the old timeout and decrement
3271 * the freeze count so that a single timeout
3272 * is sufficient for releasing the queue.
3274 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3275 untimeout(xpt_release_devq_timeout,
3276 dev, dev->c_handle);
3279 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3283 timeout(xpt_release_devq_timeout,
3285 (crs->release_timeout * hz) / 1000);
3287 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3291 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3293 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3295 * Decrement the freeze count so that a single
3296 * completion is still sufficient to unfreeze
3299 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3302 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3303 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3307 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3309 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3310 || (dev->ccbq.dev_active == 0)) {
3312 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3315 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3316 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3321 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3323 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3326 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3327 start_ccb->ccb_h.status = CAM_REQ_CMP;
3331 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3334 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3335 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3343 #ifdef CAM_DEBUG_DELAY
3344 cam_debug_delay = CAM_DEBUG_DELAY;
3346 cam_dflags = start_ccb->cdbg.flags;
3347 if (cam_dpath != NULL) {
3348 xpt_free_path(cam_dpath);
3352 if (cam_dflags != CAM_DEBUG_NONE) {
3353 if (xpt_create_path(&cam_dpath, xpt_periph,
3354 start_ccb->ccb_h.path_id,
3355 start_ccb->ccb_h.target_id,
3356 start_ccb->ccb_h.target_lun) !=
3358 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3359 cam_dflags = CAM_DEBUG_NONE;
3361 start_ccb->ccb_h.status = CAM_REQ_CMP;
3362 xpt_print_path(cam_dpath);
3363 printf("debugging flags now %x\n", cam_dflags);
3367 start_ccb->ccb_h.status = CAM_REQ_CMP;
3370 #else /* !CAMDEBUG */
3371 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3372 #endif /* CAMDEBUG */
3376 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3377 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3378 start_ccb->ccb_h.status = CAM_REQ_CMP;
3385 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3392 xpt_polled_action(union ccb *start_ccb)
3396 struct cam_sim *sim;
3397 struct cam_devq *devq;
3400 timeout = start_ccb->ccb_h.timeout;
3401 sim = start_ccb->ccb_h.path->bus->sim;
3403 dev = start_ccb->ccb_h.path->device;
3408 * Steal an opening so that no other queued requests
3409 * can get it before us while we simulate interrupts.
3411 dev->ccbq.devq_openings--;
3412 dev->ccbq.dev_openings--;
3414 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3415 && (--timeout > 0)) {
3417 (*(sim->sim_poll))(sim);
3422 dev->ccbq.devq_openings++;
3423 dev->ccbq.dev_openings++;
3426 xpt_action(start_ccb);
3427 while(--timeout > 0) {
3428 (*(sim->sim_poll))(sim);
3431 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3438 * XXX Is it worth adding a sim_timeout entry
3439 * point so we can attempt recovery? If
3440 * this is only used for dumps, I don't think
3443 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3446 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3452 * Schedule a peripheral driver to receive a ccb when it's
3453 * target device has space for more transactions.
3456 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3458 struct cam_ed *device;
3462 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3463 device = perph->path->device;
3465 if (periph_is_queued(perph)) {
3466 /* Simply reorder based on new priority */
3467 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3468 (" change priority to %d\n", new_priority));
3469 if (new_priority < perph->pinfo.priority) {
3470 camq_change_priority(&device->drvq,
3476 /* New entry on the queue */
3477 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3478 (" added periph to queue\n"));
3479 perph->pinfo.priority = new_priority;
3480 perph->pinfo.generation = ++device->drvq.generation;
3481 camq_insert(&device->drvq, &perph->pinfo);
3482 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3486 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3487 (" calling xpt_run_devq\n"));
3488 xpt_run_dev_allocq(perph->path->bus);
3494 * Schedule a device to run on a given queue.
3495 * If the device was inserted as a new entry on the queue,
3496 * return 1 meaning the device queue should be run. If we
3497 * were already queued, implying someone else has already
3498 * started the queue, return 0 so the caller doesn't attempt
3499 * to run the queue. Must be run at either splsoftcam
3500 * (or splcam since that encompases splsoftcam).
3503 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3504 u_int32_t new_priority)
3507 u_int32_t old_priority;
3509 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3511 old_priority = pinfo->priority;
3514 * Are we already queued?
3516 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3517 /* Simply reorder based on new priority */
3518 if (new_priority < old_priority) {
3519 camq_change_priority(queue, pinfo->index,
3521 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3522 ("changed priority to %d\n",
3527 /* New entry on the queue */
3528 if (new_priority < old_priority)
3529 pinfo->priority = new_priority;
3531 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3532 ("Inserting onto queue\n"));
3533 pinfo->generation = ++queue->generation;
3534 camq_insert(queue, pinfo);
3541 xpt_run_dev_allocq(struct cam_eb *bus)
3543 struct cam_devq *devq;
3546 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3547 devq = bus->sim->devq;
3549 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3550 (" qfrozen_cnt == 0x%x, entries == %d, "
3551 "openings == %d, active == %d\n",
3552 devq->alloc_queue.qfrozen_cnt,
3553 devq->alloc_queue.entries,
3554 devq->alloc_openings,
3555 devq->alloc_active));
3558 devq->alloc_queue.qfrozen_cnt++;
3559 while ((devq->alloc_queue.entries > 0)
3560 && (devq->alloc_openings > 0)
3561 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3562 struct cam_ed_qinfo *qinfo;
3563 struct cam_ed *device;
3564 union ccb *work_ccb;
3565 struct cam_periph *drv;
3568 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3570 device = qinfo->device;
3572 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3573 ("running device %p\n", device));
3575 drvq = &device->drvq;
3578 if (drvq->entries <= 0) {
3579 panic("xpt_run_dev_allocq: "
3580 "Device on queue without any work to do");
3583 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3584 devq->alloc_openings--;
3585 devq->alloc_active++;
3586 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3588 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3589 drv->pinfo.priority);
3590 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3591 ("calling periph start\n"));
3592 drv->periph_start(drv, work_ccb);
3595 * Malloc failure in alloc_ccb
3598 * XXX add us to a list to be run from free_ccb
3599 * if we don't have any ccbs active on this
3600 * device queue otherwise we may never get run
3606 /* Raise IPL for possible insertion and test at top of loop */
3609 if (drvq->entries > 0) {
3610 /* We have more work. Attempt to reschedule */
3611 xpt_schedule_dev_allocq(bus, device);
3614 devq->alloc_queue.qfrozen_cnt--;
3619 xpt_run_dev_sendq(struct cam_eb *bus)
3621 struct cam_devq *devq;
3624 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3626 devq = bus->sim->devq;
3629 devq->send_queue.qfrozen_cnt++;
3632 while ((devq->send_queue.entries > 0)
3633 && (devq->send_openings > 0)) {
3634 struct cam_ed_qinfo *qinfo;
3635 struct cam_ed *device;
3636 union ccb *work_ccb;
3637 struct cam_sim *sim;
3641 if (devq->send_queue.qfrozen_cnt > 1) {
3646 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3648 device = qinfo->device;
3651 * If the device has been "frozen", don't attempt
3654 if (device->qfrozen_cnt > 0) {
3659 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3660 ("running device %p\n", device));
3662 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3663 if (work_ccb == NULL) {
3664 printf("device on run queue with no ccbs???");
3669 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3671 if (num_highpower <= 0) {
3673 * We got a high power command, but we
3674 * don't have any available slots. Freeze
3675 * the device queue until we have a slot
3678 device->qfrozen_cnt++;
3679 STAILQ_INSERT_TAIL(&highpowerq,
3687 * Consume a high power slot while
3693 devq->active_dev = device;
3694 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3696 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3699 devq->send_openings--;
3700 devq->send_active++;
3702 if (device->ccbq.queue.entries > 0)
3703 xpt_schedule_dev_sendq(bus, device);
3705 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3707 * The client wants to freeze the queue
3708 * after this CCB is sent.
3711 device->qfrozen_cnt++;
3717 /* In Target mode, the peripheral driver knows best... */
3718 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3719 if ((device->inq_flags & SID_CmdQue) != 0
3720 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3721 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3724 * Clear this in case of a retried CCB that
3725 * failed due to a rejected tag.
3727 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3731 * Device queues can be shared among multiple sim instances
3732 * that reside on different busses. Use the SIM in the queue
3733 * CCB's path, rather than the one in the bus that was passed
3734 * into this function.
3736 sim = work_ccb->ccb_h.path->bus->sim;
3737 (*(sim->sim_action))(sim, work_ccb);
3740 devq->active_dev = NULL;
3742 /* Raise IPL for possible insertion and test at top of loop */
3747 devq->send_queue.qfrozen_cnt--;
3752 * This function merges stuff from the slave ccb into the master ccb, while
3753 * keeping important fields in the master ccb constant.
3756 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3759 * Pull fields that are valid for peripheral drivers to set
3760 * into the master CCB along with the CCB "payload".
3762 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3763 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3764 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3765 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3766 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3767 sizeof(union ccb) - sizeof(struct ccb_hdr));
3771 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3773 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3774 ccb_h->pinfo.priority = priority;
3776 ccb_h->path_id = path->bus->path_id;
3778 ccb_h->target_id = path->target->target_id;
3780 ccb_h->target_id = CAM_TARGET_WILDCARD;
3782 ccb_h->target_lun = path->device->lun_id;
3783 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3785 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3787 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3791 /* Path manipulation functions */
3793 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3794 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3796 struct cam_path *path;
3799 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3802 status = CAM_RESRC_UNAVAIL;
3805 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3806 if (status != CAM_REQ_CMP) {
3807 free(path, M_DEVBUF);
3810 *new_path_ptr = path;
3815 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3816 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3819 struct cam_et *target;
3820 struct cam_ed *device;
3824 status = CAM_REQ_CMP; /* Completed without error */
3825 target = NULL; /* Wildcarded */
3826 device = NULL; /* Wildcarded */
3829 * We will potentially modify the EDT, so block interrupts
3830 * that may attempt to create cam paths.
3833 bus = xpt_find_bus(path_id);
3835 status = CAM_PATH_INVALID;
3837 target = xpt_find_target(bus, target_id);
3838 if (target == NULL) {
3840 struct cam_et *new_target;
3842 new_target = xpt_alloc_target(bus, target_id);
3843 if (new_target == NULL) {
3844 status = CAM_RESRC_UNAVAIL;
3846 target = new_target;
3849 if (target != NULL) {
3850 device = xpt_find_device(target, lun_id);
3851 if (device == NULL) {
3853 struct cam_ed *new_device;
3855 new_device = xpt_alloc_device(bus,
3858 if (new_device == NULL) {
3859 status = CAM_RESRC_UNAVAIL;
3861 device = new_device;
3869 * Only touch the user's data if we are successful.
3871 if (status == CAM_REQ_CMP) {
3872 new_path->periph = perph;
3873 new_path->bus = bus;
3874 new_path->target = target;
3875 new_path->device = device;
3876 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3879 xpt_release_device(bus, target, device);
3881 xpt_release_target(bus, target);
3883 xpt_release_bus(bus);
3889 xpt_release_path(struct cam_path *path)
3891 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3892 if (path->device != NULL) {
3893 xpt_release_device(path->bus, path->target, path->device);
3894 path->device = NULL;
3896 if (path->target != NULL) {
3897 xpt_release_target(path->bus, path->target);
3898 path->target = NULL;
3900 if (path->bus != NULL) {
3901 xpt_release_bus(path->bus);
3907 xpt_free_path(struct cam_path *path)
3909 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3910 xpt_release_path(path);
3911 free(path, M_DEVBUF);
3916 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3917 * in path1, 2 for match with wildcards in path2.
3920 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3924 if (path1->bus != path2->bus) {
3925 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3927 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3932 if (path1->target != path2->target) {
3933 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3936 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3941 if (path1->device != path2->device) {
3942 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3945 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3954 xpt_print_path(struct cam_path *path)
3957 printf("(nopath): ");
3959 if (path->periph != NULL)
3960 printf("(%s%d:", path->periph->periph_name,
3961 path->periph->unit_number);
3963 printf("(noperiph:");
3965 if (path->bus != NULL)
3966 printf("%s%d:%d:", path->bus->sim->sim_name,
3967 path->bus->sim->unit_number,
3968 path->bus->sim->bus_id);
3972 if (path->target != NULL)
3973 printf("%d:", path->target->target_id);
3977 if (path->device != NULL)
3978 printf("%d): ", path->device->lun_id);
3985 xpt_path_path_id(struct cam_path *path)
3987 return(path->bus->path_id);
3991 xpt_path_target_id(struct cam_path *path)
3993 if (path->target != NULL)
3994 return (path->target->target_id);
3996 return (CAM_TARGET_WILDCARD);
4000 xpt_path_lun_id(struct cam_path *path)
4002 if (path->device != NULL)
4003 return (path->device->lun_id);
4005 return (CAM_LUN_WILDCARD);
4009 xpt_path_sim(struct cam_path *path)
4011 return (path->bus->sim);
4015 xpt_path_periph(struct cam_path *path)
4017 return (path->periph);
4021 * Release a CAM control block for the caller. Remit the cost of the structure
4022 * to the device referenced by the path. If the this device had no 'credits'
4023 * and peripheral drivers have registered async callbacks for this notification
4027 xpt_release_ccb(union ccb *free_ccb)
4030 struct cam_path *path;
4031 struct cam_ed *device;
4034 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4035 path = free_ccb->ccb_h.path;
4036 device = path->device;
4039 cam_ccbq_release_opening(&device->ccbq);
4040 if (xpt_ccb_count > xpt_max_ccbs) {
4041 xpt_free_ccb(free_ccb);
4044 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4046 bus->sim->devq->alloc_openings++;
4047 bus->sim->devq->alloc_active--;
4048 /* XXX Turn this into an inline function - xpt_run_device?? */
4049 if ((device_is_alloc_queued(device) == 0)
4050 && (device->drvq.entries > 0)) {
4051 xpt_schedule_dev_allocq(bus, device);
4054 if (dev_allocq_is_runnable(bus->sim->devq))
4055 xpt_run_dev_allocq(bus);
4058 /* Functions accessed by SIM drivers */
4061 * A sim structure, listing the SIM entry points and instance
4062 * identification info is passed to xpt_bus_register to hook the SIM
4063 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4064 * for this new bus and places it in the array of busses and assigns
4065 * it a path_id. The path_id may be influenced by "hard wiring"
4066 * information specified by the user. Once interrupt services are
4067 * availible, the bus will be probed.
4070 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4072 struct cam_eb *new_bus;
4073 struct cam_eb *old_bus;
4074 struct ccb_pathinq cpi;
4078 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4079 M_DEVBUF, M_NOWAIT);
4080 if (new_bus == NULL) {
4081 /* Couldn't satisfy request */
4082 return (CAM_RESRC_UNAVAIL);
4085 if (strcmp(sim->sim_name, "xpt") != 0) {
4088 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4091 TAILQ_INIT(&new_bus->et_entries);
4092 new_bus->path_id = sim->path_id;
4094 timevalclear(&new_bus->last_reset);
4096 new_bus->refcount = 1; /* Held until a bus_deregister event */
4097 new_bus->generation = 0;
4099 old_bus = TAILQ_FIRST(&xpt_busses);
4100 while (old_bus != NULL
4101 && old_bus->path_id < new_bus->path_id)
4102 old_bus = TAILQ_NEXT(old_bus, links);
4103 if (old_bus != NULL)
4104 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4106 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4110 /* Notify interested parties */
4111 if (sim->path_id != CAM_XPT_PATH_ID) {
4112 struct cam_path path;
4114 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4115 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4116 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4117 cpi.ccb_h.func_code = XPT_PATH_INQ;
4118 xpt_action((union ccb *)&cpi);
4119 xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi);
4120 xpt_release_path(&path);
4122 return (CAM_SUCCESS);
4126 xpt_bus_deregister(path_id_t pathid)
4128 struct cam_path bus_path;
4131 status = xpt_compile_path(&bus_path, NULL, pathid,
4132 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4133 if (status != CAM_REQ_CMP)
4136 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4137 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4139 /* Release the reference count held while registered. */
4140 xpt_release_bus(bus_path.bus);
4141 xpt_release_path(&bus_path);
4143 return (CAM_REQ_CMP);
4147 xptnextfreepathid(void)
4154 bus = TAILQ_FIRST(&xpt_busses);
4156 /* Find an unoccupied pathid */
4158 && bus->path_id <= pathid) {
4159 if (bus->path_id == pathid)
4161 bus = TAILQ_NEXT(bus, links);
4165 * Ensure that this pathid is not reserved for
4166 * a bus that may be registered in the future.
4168 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4170 /* Start the search over */
4177 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4183 pathid = CAM_XPT_PATH_ID;
4184 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4186 while ((i = resource_query_string(i, "at", buf)) != -1) {
4187 if (strcmp(resource_query_name(i), "scbus")) {
4188 /* Avoid a bit of foot shooting. */
4191 dunit = resource_query_unit(i);
4192 if (dunit < 0) /* unwired?! */
4194 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4195 if (sim_bus == val) {
4199 } else if (sim_bus == 0) {
4200 /* Unspecified matches bus 0 */
4204 printf("Ambiguous scbus configuration for %s%d "
4205 "bus %d, cannot wire down. The kernel "
4206 "config entry for scbus%d should "
4207 "specify a controller bus.\n"
4208 "Scbus will be assigned dynamically.\n",
4209 sim_name, sim_unit, sim_bus, dunit);
4214 if (pathid == CAM_XPT_PATH_ID)
4215 pathid = xptnextfreepathid();
4220 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4223 struct cam_et *target, *next_target;
4224 struct cam_ed *device, *next_device;
4227 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4230 * Most async events come from a CAM interrupt context. In
4231 * a few cases, the error recovery code at the peripheral layer,
4232 * which may run from our SWI or a process context, may signal
4233 * deferred events with a call to xpt_async. Ensure async
4234 * notifications are serialized by blocking cam interrupts.
4240 if (async_code == AC_BUS_RESET) {
4244 /* Update our notion of when the last reset occurred */
4245 microtime(&bus->last_reset);
4249 for (target = TAILQ_FIRST(&bus->et_entries);
4251 target = next_target) {
4253 next_target = TAILQ_NEXT(target, links);
4255 if (path->target != target
4256 && path->target->target_id != CAM_TARGET_WILDCARD
4257 && target->target_id != CAM_TARGET_WILDCARD)
4260 if (async_code == AC_SENT_BDR) {
4263 /* Update our notion of when the last reset occurred */
4265 microtime(&path->target->last_reset);
4269 for (device = TAILQ_FIRST(&target->ed_entries);
4271 device = next_device) {
4273 next_device = TAILQ_NEXT(device, links);
4275 if (path->device != device
4276 && path->device->lun_id != CAM_LUN_WILDCARD
4277 && device->lun_id != CAM_LUN_WILDCARD)
4280 xpt_dev_async(async_code, bus, target,
4283 xpt_async_bcast(&device->asyncs, async_code,
4289 * If this wasn't a fully wildcarded async, tell all
4290 * clients that want all async events.
4292 if (bus != xpt_periph->path->bus)
4293 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4299 xpt_async_bcast(struct async_list *async_head,
4300 u_int32_t async_code,
4301 struct cam_path *path, void *async_arg)
4303 struct async_node *cur_entry;
4305 cur_entry = SLIST_FIRST(async_head);
4306 while (cur_entry != NULL) {
4307 struct async_node *next_entry;
4309 * Grab the next list entry before we call the current
4310 * entry's callback. This is because the callback function
4311 * can delete its async callback entry.
4313 next_entry = SLIST_NEXT(cur_entry, links);
4314 if ((cur_entry->event_enable & async_code) != 0)
4315 cur_entry->callback(cur_entry->callback_arg,
4318 cur_entry = next_entry;
4323 * Handle any per-device event notifications that require action by the XPT.
4326 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4327 struct cam_ed *device, void *async_arg)
4330 struct cam_path newpath;
4333 * We only need to handle events for real devices.
4335 if (target->target_id == CAM_TARGET_WILDCARD
4336 || device->lun_id == CAM_LUN_WILDCARD)
4340 * We need our own path with wildcards expanded to
4341 * handle certain types of events.
4343 if ((async_code == AC_SENT_BDR)
4344 || (async_code == AC_BUS_RESET)
4345 || (async_code == AC_INQ_CHANGED))
4346 status = xpt_compile_path(&newpath, NULL,
4351 status = CAM_REQ_CMP_ERR;
4353 if (status == CAM_REQ_CMP) {
4356 * Allow transfer negotiation to occur in a
4357 * tag free environment.
4359 if (async_code == AC_SENT_BDR
4360 || async_code == AC_BUS_RESET)
4361 xpt_toggle_tags(&newpath);
4363 if (async_code == AC_INQ_CHANGED) {
4365 * We've sent a start unit command, or
4366 * something similar to a device that
4367 * may have caused its inquiry data to
4368 * change. So we re-scan the device to
4369 * refresh the inquiry data for it.
4371 xpt_scan_lun(newpath.periph, &newpath,
4372 CAM_EXPECT_INQ_CHANGE, NULL);
4374 xpt_release_path(&newpath);
4375 } else if (async_code == AC_LOST_DEVICE) {
4376 device->flags |= CAM_DEV_UNCONFIGURED;
4377 } else if (async_code == AC_TRANSFER_NEG) {
4378 struct ccb_trans_settings *settings;
4380 settings = (struct ccb_trans_settings *)async_arg;
4381 xpt_set_transfer_settings(settings, device,
4382 /*async_update*/TRUE);
4387 xpt_freeze_devq(struct cam_path *path, u_int count)
4390 struct ccb_hdr *ccbh;
4393 path->device->qfrozen_cnt += count;
4396 * Mark the last CCB in the queue as needing
4397 * to be requeued if the driver hasn't
4398 * changed it's state yet. This fixes a race
4399 * where a ccb is just about to be queued to
4400 * a controller driver when it's interrupt routine
4401 * freezes the queue. To completly close the
4402 * hole, controller drives must check to see
4403 * if a ccb's status is still CAM_REQ_INPROG
4404 * under spl protection just before they queue
4405 * the CCB. See ahc_action/ahc_freeze_devq for
4408 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4409 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4410 ccbh->status = CAM_REQUEUE_REQ;
4412 return (path->device->qfrozen_cnt);
4416 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4418 sim->devq->send_queue.qfrozen_cnt += count;
4419 if (sim->devq->active_dev != NULL) {
4420 struct ccb_hdr *ccbh;
4422 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4424 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4425 ccbh->status = CAM_REQUEUE_REQ;
4427 return (sim->devq->send_queue.qfrozen_cnt);
4431 xpt_release_devq_timeout(void *arg)
4433 struct cam_ed *device;
4435 device = (struct cam_ed *)arg;
4437 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4441 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4443 xpt_release_devq_device(path->device, count, run_queue);
4447 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4455 if (dev->qfrozen_cnt > 0) {
4457 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4458 dev->qfrozen_cnt -= count;
4459 if (dev->qfrozen_cnt == 0) {
4462 * No longer need to wait for a successful
4463 * command completion.
4465 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4468 * Remove any timeouts that might be scheduled
4469 * to release this queue.
4471 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4472 untimeout(xpt_release_devq_timeout, dev,
4474 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4478 * Now that we are unfrozen schedule the
4479 * device so any pending transactions are
4482 if ((dev->ccbq.queue.entries > 0)
4483 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4484 && (run_queue != 0)) {
4491 xpt_run_dev_sendq(dev->target->bus);
4496 xpt_release_simq(struct cam_sim *sim, int run_queue)
4501 sendq = &(sim->devq->send_queue);
4503 if (sendq->qfrozen_cnt > 0) {
4505 sendq->qfrozen_cnt--;
4506 if (sendq->qfrozen_cnt == 0) {
4510 * If there is a timeout scheduled to release this
4511 * sim queue, remove it. The queue frozen count is
4514 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4515 untimeout(xpt_release_simq_timeout, sim,
4517 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4519 bus = xpt_find_bus(sim->path_id);
4524 * Now that we are unfrozen run the send queue.
4526 xpt_run_dev_sendq(bus);
4528 xpt_release_bus(bus);
4536 xpt_release_simq_timeout(void *arg)
4538 struct cam_sim *sim;
4540 sim = (struct cam_sim *)arg;
4541 xpt_release_simq(sim, /* run_queue */ TRUE);
4545 xpt_done(union ccb *done_ccb)
4551 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4552 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4554 * Queue up the request for handling by our SWI handler
4555 * any of the "non-immediate" type of ccbs.
4557 switch (done_ccb->ccb_h.path->periph->type) {
4558 case CAM_PERIPH_BIO:
4559 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4561 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4562 swi_sched(cambio_ih, SWI_NOSWITCH);
4564 case CAM_PERIPH_NET:
4565 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4567 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4568 swi_sched(camnet_ih, SWI_NOSWITCH);
4580 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4585 xpt_free_ccb(union ccb *free_ccb)
4587 free(free_ccb, M_DEVBUF);
4592 /* Private XPT functions */
4595 * Get a CAM control block for the caller. Charge the structure to the device
4596 * referenced by the path. If the this device has no 'credits' then the
4597 * device already has the maximum number of outstanding operations under way
4598 * and we return NULL. If we don't have sufficient resources to allocate more
4599 * ccbs, we also return NULL.
4602 xpt_get_ccb(struct cam_ed *device)
4608 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4609 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4610 if (new_ccb == NULL) {
4614 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4615 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4619 cam_ccbq_take_opening(&device->ccbq);
4620 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4626 xpt_release_bus(struct cam_eb *bus)
4631 if ((--bus->refcount == 0)
4632 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4633 TAILQ_REMOVE(&xpt_busses, bus, links);
4636 free(bus, M_DEVBUF);
4641 static struct cam_et *
4642 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4644 struct cam_et *target;
4646 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4647 if (target != NULL) {
4648 struct cam_et *cur_target;
4650 TAILQ_INIT(&target->ed_entries);
4652 target->target_id = target_id;
4653 target->refcount = 1;
4654 target->generation = 0;
4655 timevalclear(&target->last_reset);
4657 * Hold a reference to our parent bus so it
4658 * will not go away before we do.
4662 /* Insertion sort into our bus's target list */
4663 cur_target = TAILQ_FIRST(&bus->et_entries);
4664 while (cur_target != NULL && cur_target->target_id < target_id)
4665 cur_target = TAILQ_NEXT(cur_target, links);
4667 if (cur_target != NULL) {
4668 TAILQ_INSERT_BEFORE(cur_target, target, links);
4670 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4678 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4683 if ((--target->refcount == 0)
4684 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4685 TAILQ_REMOVE(&bus->et_entries, target, links);
4688 free(target, M_DEVBUF);
4689 xpt_release_bus(bus);
4694 static struct cam_ed *
4695 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4697 struct cam_ed *device;
4698 struct cam_devq *devq;
4701 /* Make space for us in the device queue on our bus */
4702 devq = bus->sim->devq;
4703 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4705 if (status != CAM_REQ_CMP) {
4708 device = (struct cam_ed *)malloc(sizeof(*device),
4709 M_DEVBUF, M_NOWAIT);
4712 if (device != NULL) {
4713 struct cam_ed *cur_device;
4715 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4716 device->alloc_ccb_entry.device = device;
4717 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4718 device->send_ccb_entry.device = device;
4719 device->target = target;
4720 device->lun_id = lun_id;
4721 /* Initialize our queues */
4722 if (camq_init(&device->drvq, 0) != 0) {
4723 free(device, M_DEVBUF);
4726 if (cam_ccbq_init(&device->ccbq,
4727 bus->sim->max_dev_openings) != 0) {
4728 camq_fini(&device->drvq);
4729 free(device, M_DEVBUF);
4732 SLIST_INIT(&device->asyncs);
4733 SLIST_INIT(&device->periphs);
4734 device->generation = 0;
4735 device->owner = NULL;
4737 * Take the default quirk entry until we have inquiry
4738 * data and can determine a better quirk to use.
4740 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4741 bzero(&device->inq_data, sizeof(device->inq_data));
4742 device->inq_flags = 0;
4743 device->queue_flags = 0;
4744 device->serial_num = NULL;
4745 device->serial_num_len = 0;
4746 device->qfrozen_cnt = 0;
4747 device->flags = CAM_DEV_UNCONFIGURED;
4748 device->tag_delay_count = 0;
4749 device->refcount = 1;
4750 callout_handle_init(&device->c_handle);
4753 * Hold a reference to our parent target so it
4754 * will not go away before we do.
4759 * XXX should be limited by number of CCBs this bus can
4762 xpt_max_ccbs += device->ccbq.devq_openings;
4763 /* Insertion sort into our target's device list */
4764 cur_device = TAILQ_FIRST(&target->ed_entries);
4765 while (cur_device != NULL && cur_device->lun_id < lun_id)
4766 cur_device = TAILQ_NEXT(cur_device, links);
4767 if (cur_device != NULL) {
4768 TAILQ_INSERT_BEFORE(cur_device, device, links);
4770 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4772 target->generation++;
4778 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4779 struct cam_ed *device)
4784 if ((--device->refcount == 0)
4785 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4786 struct cam_devq *devq;
4788 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4789 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4790 panic("Removing device while still queued for ccbs");
4792 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4793 untimeout(xpt_release_devq_timeout, device,
4796 TAILQ_REMOVE(&target->ed_entries, device,links);
4797 target->generation++;
4798 xpt_max_ccbs -= device->ccbq.devq_openings;
4799 /* Release our slot in the devq */
4800 devq = bus->sim->devq;
4801 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4803 free(device, M_DEVBUF);
4804 xpt_release_target(bus, target);
4810 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4820 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4821 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4822 if (result == CAM_REQ_CMP && (diff < 0)) {
4823 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4825 /* Adjust the global limit */
4826 xpt_max_ccbs += diff;
4831 static struct cam_eb *
4832 xpt_find_bus(path_id_t path_id)
4836 for (bus = TAILQ_FIRST(&xpt_busses);
4838 bus = TAILQ_NEXT(bus, links)) {
4839 if (bus->path_id == path_id) {
4847 static struct cam_et *
4848 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4850 struct cam_et *target;
4852 for (target = TAILQ_FIRST(&bus->et_entries);
4854 target = TAILQ_NEXT(target, links)) {
4855 if (target->target_id == target_id) {
4863 static struct cam_ed *
4864 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4866 struct cam_ed *device;
4868 for (device = TAILQ_FIRST(&target->ed_entries);
4870 device = TAILQ_NEXT(device, links)) {
4871 if (device->lun_id == lun_id) {
4880 union ccb *request_ccb;
4881 struct ccb_pathinq *cpi;
4883 } xpt_scan_bus_info;
4886 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
4887 * As the scan progresses, xpt_scan_bus is used as the
4888 * callback on completion function.
4891 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
4893 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4894 ("xpt_scan_bus\n"));
4895 switch (request_ccb->ccb_h.func_code) {
4898 xpt_scan_bus_info *scan_info;
4899 union ccb *work_ccb;
4900 struct cam_path *path;
4905 /* Find out the characteristics of the bus */
4906 work_ccb = xpt_alloc_ccb();
4907 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
4908 request_ccb->ccb_h.pinfo.priority);
4909 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4910 xpt_action(work_ccb);
4911 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4912 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
4913 xpt_free_ccb(work_ccb);
4914 xpt_done(request_ccb);
4918 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
4920 * Can't scan the bus on an adapter that
4921 * cannot perform the initiator role.
4923 request_ccb->ccb_h.status = CAM_REQ_CMP;
4924 xpt_free_ccb(work_ccb);
4925 xpt_done(request_ccb);
4929 /* Save some state for use while we probe for devices */
4930 scan_info = (xpt_scan_bus_info *)
4931 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
4932 scan_info->request_ccb = request_ccb;
4933 scan_info->cpi = &work_ccb->cpi;
4935 /* Cache on our stack so we can work asynchronously */
4936 max_target = scan_info->cpi->max_target;
4937 initiator_id = scan_info->cpi->initiator_id;
4940 * Don't count the initiator if the
4941 * initiator is addressable.
4943 scan_info->pending_count = max_target + 1;
4944 if (initiator_id <= max_target)
4945 scan_info->pending_count--;
4947 for (i = 0; i <= max_target; i++) {
4949 if (i == initiator_id)
4952 status = xpt_create_path(&path, xpt_periph,
4953 request_ccb->ccb_h.path_id,
4955 if (status != CAM_REQ_CMP) {
4956 printf("xpt_scan_bus: xpt_create_path failed"
4957 " with status %#x, bus scan halted\n",
4961 work_ccb = xpt_alloc_ccb();
4962 xpt_setup_ccb(&work_ccb->ccb_h, path,
4963 request_ccb->ccb_h.pinfo.priority);
4964 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
4965 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
4966 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
4967 work_ccb->crcn.flags = request_ccb->crcn.flags;
4969 printf("xpt_scan_bus: probing %d:%d:%d\n",
4970 request_ccb->ccb_h.path_id, i, 0);
4972 xpt_action(work_ccb);
4978 xpt_scan_bus_info *scan_info;
4980 target_id_t target_id;
4983 /* Reuse the same CCB to query if a device was really found */
4984 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
4985 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
4986 request_ccb->ccb_h.pinfo.priority);
4987 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
4989 path_id = request_ccb->ccb_h.path_id;
4990 target_id = request_ccb->ccb_h.target_id;
4991 lun_id = request_ccb->ccb_h.target_lun;
4992 xpt_action(request_ccb);
4995 printf("xpt_scan_bus: got back probe from %d:%d:%d\n",
4996 path_id, target_id, lun_id);
4999 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5000 struct cam_ed *device;
5001 struct cam_et *target;
5005 * If we already probed lun 0 successfully, or
5006 * we have additional configured luns on this
5007 * target that might have "gone away", go onto
5010 target = request_ccb->ccb_h.path->target;
5012 * We may touch devices that we don't
5013 * hold references too, so ensure they
5014 * don't disappear out from under us.
5015 * The target above is referenced by the
5016 * path in the request ccb.
5020 device = TAILQ_FIRST(&target->ed_entries);
5021 if (device != NULL) {
5022 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5023 if (device->lun_id == 0)
5024 device = TAILQ_NEXT(device, links);
5027 if ((lun_id != 0) || (device != NULL)) {
5028 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5032 struct cam_ed *device;
5034 device = request_ccb->ccb_h.path->device;
5036 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5037 /* Try the next lun */
5038 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5039 (device->quirk->quirks & CAM_QUIRK_HILUNS))
5044 xpt_free_path(request_ccb->ccb_h.path);
5047 if ((lun_id == request_ccb->ccb_h.target_lun)
5048 || lun_id > scan_info->cpi->max_lun) {
5051 xpt_free_ccb(request_ccb);
5052 scan_info->pending_count--;
5053 if (scan_info->pending_count == 0) {
5054 xpt_free_ccb((union ccb *)scan_info->cpi);
5055 request_ccb = scan_info->request_ccb;
5056 free(scan_info, M_TEMP);
5057 request_ccb->ccb_h.status = CAM_REQ_CMP;
5058 xpt_done(request_ccb);
5061 /* Try the next device */
5062 struct cam_path *path;
5065 path = request_ccb->ccb_h.path;
5066 status = xpt_create_path(&path, xpt_periph,
5067 path_id, target_id, lun_id);
5068 if (status != CAM_REQ_CMP) {
5069 printf("xpt_scan_bus: xpt_create_path failed "
5070 "with status %#x, halting LUN scan\n",
5072 xpt_free_ccb(request_ccb);
5073 scan_info->pending_count--;
5074 if (scan_info->pending_count == 0) {
5076 (union ccb *)scan_info->cpi);
5077 request_ccb = scan_info->request_ccb;
5078 free(scan_info, M_TEMP);
5079 request_ccb->ccb_h.status = CAM_REQ_CMP;
5080 xpt_done(request_ccb);
5084 xpt_setup_ccb(&request_ccb->ccb_h, path,
5085 request_ccb->ccb_h.pinfo.priority);
5086 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5087 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5088 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5089 request_ccb->crcn.flags =
5090 scan_info->request_ccb->crcn.flags;
5092 xpt_print_path(path);
5093 printf("xpt_scan bus probing\n");
5095 xpt_action(request_ccb);
5110 PROBE_TUR_FOR_NEGOTIATION
5114 PROBE_INQUIRY_CKSUM = 0x01,
5115 PROBE_SERIAL_CKSUM = 0x02,
5116 PROBE_NO_ANNOUNCE = 0x04
5120 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5121 probe_action action;
5122 union ccb saved_ccb;
5125 u_int8_t digest[16];
5129 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5130 cam_flags flags, union ccb *request_ccb)
5132 struct ccb_pathinq cpi;
5134 struct cam_path *new_path;
5135 struct cam_periph *old_periph;
5138 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5139 ("xpt_scan_lun\n"));
5141 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5142 cpi.ccb_h.func_code = XPT_PATH_INQ;
5143 xpt_action((union ccb *)&cpi);
5145 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5146 if (request_ccb != NULL) {
5147 request_ccb->ccb_h.status = cpi.ccb_h.status;
5148 xpt_done(request_ccb);
5153 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5155 * Can't scan the bus on an adapter that
5156 * cannot perform the initiator role.
5158 if (request_ccb != NULL) {
5159 request_ccb->ccb_h.status = CAM_REQ_CMP;
5160 xpt_done(request_ccb);
5165 if (request_ccb == NULL) {
5166 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5167 if (request_ccb == NULL) {
5168 xpt_print_path(path);
5169 printf("xpt_scan_lun: can't allocate CCB, can't "
5173 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5174 if (new_path == NULL) {
5175 xpt_print_path(path);
5176 printf("xpt_scan_lun: can't allocate path, can't "
5178 free(request_ccb, M_TEMP);
5181 status = xpt_compile_path(new_path, xpt_periph,
5183 path->target->target_id,
5184 path->device->lun_id);
5186 if (status != CAM_REQ_CMP) {
5187 xpt_print_path(path);
5188 printf("xpt_scan_lun: can't compile path, can't "
5190 free(request_ccb, M_TEMP);
5191 free(new_path, M_TEMP);
5194 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5195 request_ccb->ccb_h.cbfcnp = xptscandone;
5196 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5197 request_ccb->crcn.flags = flags;
5201 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5204 softc = (probe_softc *)old_periph->softc;
5205 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5208 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5209 probestart, "probe",
5211 request_ccb->ccb_h.path, NULL, 0,
5214 if (status != CAM_REQ_CMP) {
5215 xpt_print_path(path);
5216 printf("xpt_scan_lun: cam_alloc_periph returned an "
5217 "error, can't continue probe\n");
5218 request_ccb->ccb_h.status = status;
5219 xpt_done(request_ccb);
5226 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5228 xpt_release_path(done_ccb->ccb_h.path);
5229 free(done_ccb->ccb_h.path, M_TEMP);
5230 free(done_ccb, M_TEMP);
5234 proberegister(struct cam_periph *periph, void *arg)
5236 union ccb *request_ccb; /* CCB representing the probe request */
5239 request_ccb = (union ccb *)arg;
5240 if (periph == NULL) {
5241 printf("proberegister: periph was NULL!!\n");
5242 return(CAM_REQ_CMP_ERR);
5245 if (request_ccb == NULL) {
5246 printf("proberegister: no probe CCB, "
5247 "can't register device\n");
5248 return(CAM_REQ_CMP_ERR);
5251 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5253 if (softc == NULL) {
5254 printf("proberegister: Unable to probe new device. "
5255 "Unable to allocate softc\n");
5256 return(CAM_REQ_CMP_ERR);
5258 TAILQ_INIT(&softc->request_ccbs);
5259 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5262 periph->softc = softc;
5263 cam_periph_acquire(periph);
5265 * Ensure we've waited at least a bus settle
5266 * delay before attempting to probe the device.
5267 * For HBAs that don't do bus resets, this won't make a difference.
5269 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5271 probeschedule(periph);
5272 return(CAM_REQ_CMP);
5276 probeschedule(struct cam_periph *periph)
5278 struct ccb_pathinq cpi;
5282 softc = (probe_softc *)periph->softc;
5283 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5285 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5286 cpi.ccb_h.func_code = XPT_PATH_INQ;
5287 xpt_action((union ccb *)&cpi);
5290 * If a device has gone away and another device, or the same one,
5291 * is back in the same place, it should have a unit attention
5292 * condition pending. It will not report the unit attention in
5293 * response to an inquiry, which may leave invalid transfer
5294 * negotiations in effect. The TUR will reveal the unit attention
5295 * condition. Only send the TUR for lun 0, since some devices
5296 * will get confused by commands other than inquiry to non-existent
5297 * luns. If you think a device has gone away start your scan from
5298 * lun 0. This will insure that any bogus transfer settings are
5301 * If we haven't seen the device before and the controller supports
5302 * some kind of transfer negotiation, negotiate with the first
5303 * sent command if no bus reset was performed at startup. This
5304 * ensures that the device is not confused by transfer negotiation
5305 * settings left over by loader or BIOS action.
5307 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5308 && (ccb->ccb_h.target_lun == 0)) {
5309 softc->action = PROBE_TUR;
5310 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5311 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5312 proberequestdefaultnegotiation(periph);
5313 softc->action = PROBE_INQUIRY;
5315 softc->action = PROBE_INQUIRY;
5318 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5319 softc->flags |= PROBE_NO_ANNOUNCE;
5321 softc->flags &= ~PROBE_NO_ANNOUNCE;
5323 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5327 probestart(struct cam_periph *periph, union ccb *start_ccb)
5329 /* Probe the device that our peripheral driver points to */
5330 struct ccb_scsiio *csio;
5333 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5335 softc = (probe_softc *)periph->softc;
5336 csio = &start_ccb->csio;
5338 switch (softc->action) {
5340 case PROBE_TUR_FOR_NEGOTIATION:
5342 scsi_test_unit_ready(csio,
5351 case PROBE_FULL_INQUIRY:
5354 struct scsi_inquiry_data *inq_buf;
5356 inq_buf = &periph->path->device->inq_data;
5358 * If the device is currently configured, we calculate an
5359 * MD5 checksum of the inquiry data, and if the serial number
5360 * length is greater than 0, add the serial number data
5361 * into the checksum as well. Once the inquiry and the
5362 * serial number check finish, we attempt to figure out
5363 * whether we still have the same device.
5365 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5367 MD5Init(&softc->context);
5368 MD5Update(&softc->context, (unsigned char *)inq_buf,
5369 sizeof(struct scsi_inquiry_data));
5370 softc->flags |= PROBE_INQUIRY_CKSUM;
5371 if (periph->path->device->serial_num_len > 0) {
5372 MD5Update(&softc->context,
5373 periph->path->device->serial_num,
5374 periph->path->device->serial_num_len);
5375 softc->flags |= PROBE_SERIAL_CKSUM;
5377 MD5Final(softc->digest, &softc->context);
5380 if (softc->action == PROBE_INQUIRY)
5381 inquiry_len = SHORT_INQUIRY_LENGTH;
5383 inquiry_len = inq_buf->additional_length + 4;
5389 (u_int8_t *)inq_buf,
5394 /*timeout*/60 * 1000);
5397 case PROBE_MODE_SENSE:
5402 mode_buf_len = sizeof(struct scsi_mode_header_6)
5403 + sizeof(struct scsi_mode_blk_desc)
5404 + sizeof(struct scsi_control_page);
5405 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5406 if (mode_buf != NULL) {
5407 scsi_mode_sense(csio,
5412 SMS_PAGE_CTRL_CURRENT,
5413 SMS_CONTROL_MODE_PAGE,
5420 xpt_print_path(periph->path);
5421 printf("Unable to mode sense control page - malloc failure\n");
5422 softc->action = PROBE_SERIAL_NUM;
5425 case PROBE_SERIAL_NUM:
5427 struct scsi_vpd_unit_serial_number *serial_buf;
5428 struct cam_ed* device;
5431 device = periph->path->device;
5432 device->serial_num = NULL;
5433 device->serial_num_len = 0;
5435 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5436 serial_buf = (struct scsi_vpd_unit_serial_number *)
5437 malloc(sizeof(*serial_buf), M_TEMP,
5440 if (serial_buf != NULL) {
5445 (u_int8_t *)serial_buf,
5446 sizeof(*serial_buf),
5448 SVPD_UNIT_SERIAL_NUMBER,
5450 /*timeout*/60 * 1000);
5454 * We'll have to do without, let our probedone
5455 * routine finish up for us.
5457 start_ccb->csio.data_ptr = NULL;
5458 probedone(periph, start_ccb);
5462 xpt_action(start_ccb);
5466 proberequestdefaultnegotiation(struct cam_periph *periph)
5468 struct ccb_trans_settings cts;
5470 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5471 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5472 cts.flags = CCB_TRANS_USER_SETTINGS;
5473 xpt_action((union ccb *)&cts);
5474 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5475 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5476 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5477 xpt_action((union ccb *)&cts);
5481 probedone(struct cam_periph *periph, union ccb *done_ccb)
5484 struct cam_path *path;
5487 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5489 softc = (probe_softc *)periph->softc;
5490 path = done_ccb->ccb_h.path;
5491 priority = done_ccb->ccb_h.pinfo.priority;
5493 switch (softc->action) {
5496 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5498 if (cam_periph_error(done_ccb, 0,
5499 SF_NO_PRINT, NULL) == ERESTART)
5501 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5502 /* Don't wedge the queue */
5503 xpt_release_devq(done_ccb->ccb_h.path,
5507 softc->action = PROBE_INQUIRY;
5508 xpt_release_ccb(done_ccb);
5509 xpt_schedule(periph, priority);
5513 case PROBE_FULL_INQUIRY:
5515 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5516 struct scsi_inquiry_data *inq_buf;
5517 u_int8_t periph_qual;
5518 u_int8_t periph_dtype;
5520 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5521 inq_buf = &path->device->inq_data;
5523 periph_qual = SID_QUAL(inq_buf);
5524 periph_dtype = SID_TYPE(inq_buf);
5526 if (periph_dtype != T_NODEVICE) {
5527 switch(periph_qual) {
5528 case SID_QUAL_LU_CONNECTED:
5533 * We conservatively request only
5534 * SHORT_INQUIRY_LEN bytes of inquiry
5535 * information during our first try
5536 * at sending an INQUIRY. If the device
5537 * has more information to give,
5538 * perform a second request specifying
5539 * the amount of information the device
5540 * is willing to give.
5542 alen = inq_buf->additional_length;
5543 if (softc->action == PROBE_INQUIRY
5544 && alen > (SHORT_INQUIRY_LENGTH - 4)) {
5547 xpt_release_ccb(done_ccb);
5548 xpt_schedule(periph, priority);
5552 xpt_find_quirk(path->device);
5554 if ((inq_buf->flags & SID_CmdQue) != 0)
5561 path->device->flags &=
5562 ~CAM_DEV_UNCONFIGURED;
5564 xpt_release_ccb(done_ccb);
5565 xpt_schedule(periph, priority);
5572 } else if (cam_periph_error(done_ccb, 0,
5573 done_ccb->ccb_h.target_lun > 0
5574 ? SF_RETRY_UA|SF_QUIET_IR
5576 &softc->saved_ccb) == ERESTART) {
5578 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5579 /* Don't wedge the queue */
5580 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5584 * If we get to this point, we got an error status back
5585 * from the inquiry and the error status doesn't require
5586 * automatically retrying the command. Therefore, the
5587 * inquiry failed. If we had inquiry information before
5588 * for this device, but this latest inquiry command failed,
5589 * the device has probably gone away. If this device isn't
5590 * already marked unconfigured, notify the peripheral
5591 * drivers that this device is no more.
5593 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5594 /* Send the async notification. */
5595 xpt_async(AC_LOST_DEVICE, path, NULL);
5597 xpt_release_ccb(done_ccb);
5600 case PROBE_MODE_SENSE:
5602 struct ccb_scsiio *csio;
5603 struct scsi_mode_header_6 *mode_hdr;
5605 csio = &done_ccb->csio;
5606 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5607 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5608 struct scsi_control_page *page;
5611 offset = ((u_int8_t *)&mode_hdr[1])
5612 + mode_hdr->blk_desc_len;
5613 page = (struct scsi_control_page *)offset;
5614 path->device->queue_flags = page->queue_flags;
5615 } else if (cam_periph_error(done_ccb, 0,
5616 SF_RETRY_UA|SF_NO_PRINT,
5617 &softc->saved_ccb) == ERESTART) {
5619 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5620 /* Don't wedge the queue */
5621 xpt_release_devq(done_ccb->ccb_h.path,
5622 /*count*/1, /*run_queue*/TRUE);
5624 xpt_release_ccb(done_ccb);
5625 free(mode_hdr, M_TEMP);
5626 softc->action = PROBE_SERIAL_NUM;
5627 xpt_schedule(periph, priority);
5630 case PROBE_SERIAL_NUM:
5632 struct ccb_scsiio *csio;
5633 struct scsi_vpd_unit_serial_number *serial_buf;
5640 csio = &done_ccb->csio;
5641 priority = done_ccb->ccb_h.pinfo.priority;
5643 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5645 /* Clean up from previous instance of this device */
5646 if (path->device->serial_num != NULL) {
5647 free(path->device->serial_num, M_DEVBUF);
5648 path->device->serial_num = NULL;
5649 path->device->serial_num_len = 0;
5652 if (serial_buf == NULL) {
5654 * Don't process the command as it was never sent
5656 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5657 && (serial_buf->length > 0)) {
5660 path->device->serial_num =
5661 (u_int8_t *)malloc((serial_buf->length + 1),
5662 M_DEVBUF, M_NOWAIT);
5663 if (path->device->serial_num != NULL) {
5664 bcopy(serial_buf->serial_num,
5665 path->device->serial_num,
5666 serial_buf->length);
5667 path->device->serial_num_len =
5669 path->device->serial_num[serial_buf->length]
5672 } else if (cam_periph_error(done_ccb, 0,
5673 SF_RETRY_UA|SF_NO_PRINT,
5674 &softc->saved_ccb) == ERESTART) {
5676 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5677 /* Don't wedge the queue */
5678 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5683 * Let's see if we have seen this device before.
5685 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5687 u_int8_t digest[16];
5692 (unsigned char *)&path->device->inq_data,
5693 sizeof(struct scsi_inquiry_data));
5696 MD5Update(&context, serial_buf->serial_num,
5697 serial_buf->length);
5699 MD5Final(digest, &context);
5700 if (bcmp(softc->digest, digest, 16) == 0)
5704 * XXX Do we need to do a TUR in order to ensure
5705 * that the device really hasn't changed???
5708 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5709 xpt_async(AC_LOST_DEVICE, path, NULL);
5711 if (serial_buf != NULL)
5712 free(serial_buf, M_TEMP);
5716 * Now that we have all the necessary
5717 * information to safely perform transfer
5718 * negotiations... Controllers don't perform
5719 * any negotiation or tagged queuing until
5720 * after the first XPT_SET_TRAN_SETTINGS ccb is
5721 * received. So, on a new device, just retreive
5722 * the user settings, and set them as the current
5723 * settings to set the device up.
5725 proberequestdefaultnegotiation(periph);
5726 xpt_release_ccb(done_ccb);
5729 * Perform a TUR to allow the controller to
5730 * perform any necessary transfer negotiation.
5732 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5733 xpt_schedule(periph, priority);
5736 xpt_release_ccb(done_ccb);
5739 case PROBE_TUR_FOR_NEGOTIATION:
5740 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5741 /* Don't wedge the queue */
5742 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5746 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5748 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5749 /* Inform the XPT that a new device has been found */
5750 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5751 xpt_action(done_ccb);
5753 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5755 xpt_release_ccb(done_ccb);
5758 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5759 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5760 done_ccb->ccb_h.status = CAM_REQ_CMP;
5762 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5763 cam_periph_invalidate(periph);
5764 cam_periph_release(periph);
5766 probeschedule(periph);
5771 probecleanup(struct cam_periph *periph)
5773 free(periph->softc, M_TEMP);
5777 xpt_find_quirk(struct cam_ed *device)
5781 match = cam_quirkmatch((caddr_t)&device->inq_data,
5782 (caddr_t)xpt_quirk_table,
5783 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5784 sizeof(*xpt_quirk_table), scsi_inquiry_match);
5787 panic("xpt_find_quirk: device didn't match wildcard entry!!");
5789 device->quirk = (struct xpt_quirk_entry *)match;
5793 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
5796 struct cam_sim *sim;
5799 sim = cts->ccb_h.path->bus->sim;
5800 if (async_update == FALSE) {
5801 struct scsi_inquiry_data *inq_data;
5802 struct ccb_pathinq cpi;
5803 struct ccb_trans_settings cur_cts;
5805 if (device == NULL) {
5806 cts->ccb_h.status = CAM_PATH_INVALID;
5807 xpt_done((union ccb *)cts);
5812 * Perform sanity checking against what the
5813 * controller and device can do.
5815 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
5816 cpi.ccb_h.func_code = XPT_PATH_INQ;
5817 xpt_action((union ccb *)&cpi);
5818 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
5819 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5820 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
5821 xpt_action((union ccb *)&cur_cts);
5822 inq_data = &device->inq_data;
5824 /* Fill in any gaps in what the user gave us */
5825 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
5826 cts->sync_period = cur_cts.sync_period;
5827 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
5828 cts->sync_offset = cur_cts.sync_offset;
5829 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
5830 cts->bus_width = cur_cts.bus_width;
5831 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
5832 cts->flags &= ~CCB_TRANS_DISC_ENB;
5833 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
5835 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
5836 cts->flags &= ~CCB_TRANS_TAG_ENB;
5837 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
5839 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5840 && (inq_data->flags & SID_Sync) == 0)
5841 || (cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
5843 cts->sync_period = 0;
5844 cts->sync_offset = 0;
5848 * Don't allow DT transmission rates if the
5849 * device does not support it.
5851 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5852 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
5853 && cts->sync_period <= 0x9)
5854 cts->sync_period = 0xa;
5856 switch (cts->bus_width) {
5857 case MSG_EXT_WDTR_BUS_32_BIT:
5858 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5859 || (inq_data->flags & SID_WBus32) != 0)
5860 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
5862 /* Fall Through to 16-bit */
5863 case MSG_EXT_WDTR_BUS_16_BIT:
5864 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5865 || (inq_data->flags & SID_WBus16) != 0)
5866 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
5867 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5870 /* Fall Through to 8-bit */
5871 default: /* New bus width?? */
5872 case MSG_EXT_WDTR_BUS_8_BIT:
5873 /* All targets can do this */
5874 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5878 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
5880 * Can't tag queue without disconnection.
5882 cts->flags &= ~CCB_TRANS_TAG_ENB;
5883 cts->valid |= CCB_TRANS_TQ_VALID;
5886 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
5887 || (inq_data->flags & SID_CmdQue) == 0
5888 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
5889 || (device->quirk->mintags == 0)) {
5891 * Can't tag on hardware that doesn't support,
5892 * doesn't have it enabled, or has broken tag support.
5894 cts->flags &= ~CCB_TRANS_TAG_ENB;
5899 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
5903 * If we are transitioning from tags to no-tags or
5904 * vice-versa, we need to carefully freeze and restart
5905 * the queue so that we don't overlap tagged and non-tagged
5906 * commands. We also temporarily stop tags if there is
5907 * a change in transfer negotiation settings to allow
5908 * "tag-less" negotiation.
5910 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5911 || (device->inq_flags & SID_CmdQue) != 0)
5912 device_tagenb = TRUE;
5914 device_tagenb = FALSE;
5916 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
5917 && device_tagenb == FALSE)
5918 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
5919 && device_tagenb == TRUE)) {
5921 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
5923 * Delay change to use tags until after a
5924 * few commands have gone to this device so
5925 * the controller has time to perform transfer
5926 * negotiations without tagged messages getting
5929 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
5930 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
5932 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
5934 device->inq_flags &= ~SID_CmdQue;
5935 xpt_dev_ccbq_resize(cts->ccb_h.path,
5936 sim->max_dev_openings);
5937 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5938 device->tag_delay_count = 0;
5943 if (async_update == FALSE) {
5945 * If we are currently performing tagged transactions to
5946 * this device and want to change its negotiation parameters,
5947 * go non-tagged for a bit to give the controller a chance to
5948 * negotiate unhampered by tag messages.
5950 if ((device->inq_flags & SID_CmdQue) != 0
5951 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
5952 CCB_TRANS_SYNC_OFFSET_VALID|
5953 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
5954 xpt_toggle_tags(cts->ccb_h.path);
5956 (*(sim->sim_action))(sim, (union ccb *)cts);
5960 struct ccb_relsim crs;
5962 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
5964 crs.ccb_h.func_code = XPT_REL_SIMQ;
5965 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5967 = crs.release_timeout
5970 xpt_action((union ccb *)&crs);
5975 xpt_toggle_tags(struct cam_path *path)
5980 * Give controllers a chance to renegotiate
5981 * before starting tag operations. We
5982 * "toggle" tagged queuing off then on
5983 * which causes the tag enable command delay
5984 * counter to come into effect.
5987 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5988 || ((dev->inq_flags & SID_CmdQue) != 0
5989 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
5990 struct ccb_trans_settings cts;
5992 xpt_setup_ccb(&cts.ccb_h, path, 1);
5994 cts.valid = CCB_TRANS_TQ_VALID;
5995 xpt_set_transfer_settings(&cts, path->device,
5996 /*async_update*/TRUE);
5997 cts.flags = CCB_TRANS_TAG_ENB;
5998 xpt_set_transfer_settings(&cts, path->device,
5999 /*async_update*/TRUE);
6004 xpt_start_tags(struct cam_path *path)
6006 struct ccb_relsim crs;
6007 struct cam_ed *device;
6008 struct cam_sim *sim;
6011 device = path->device;
6012 sim = path->bus->sim;
6013 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6014 xpt_freeze_devq(path, /*count*/1);
6015 device->inq_flags |= SID_CmdQue;
6016 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
6017 xpt_dev_ccbq_resize(path, newopenings);
6018 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6019 crs.ccb_h.func_code = XPT_REL_SIMQ;
6020 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6022 = crs.release_timeout
6025 xpt_action((union ccb *)&crs);
6028 static int busses_to_config;
6029 static int busses_to_reset;
6032 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6034 if (bus->path_id != CAM_XPT_PATH_ID) {
6035 struct cam_path path;
6036 struct ccb_pathinq cpi;
6040 xpt_compile_path(&path, NULL, bus->path_id,
6041 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6042 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6043 cpi.ccb_h.func_code = XPT_PATH_INQ;
6044 xpt_action((union ccb *)&cpi);
6045 can_negotiate = cpi.hba_inquiry;
6046 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6047 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6050 xpt_release_path(&path);
6057 xptconfigfunc(struct cam_eb *bus, void *arg)
6059 struct cam_path *path;
6060 union ccb *work_ccb;
6062 if (bus->path_id != CAM_XPT_PATH_ID) {
6066 work_ccb = xpt_alloc_ccb();
6067 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6068 CAM_TARGET_WILDCARD,
6069 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6070 printf("xptconfigfunc: xpt_create_path failed with "
6071 "status %#x for bus %d\n", status, bus->path_id);
6072 printf("xptconfigfunc: halting bus configuration\n");
6073 xpt_free_ccb(work_ccb);
6075 xpt_finishconfig(xpt_periph, NULL);
6078 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6079 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6080 xpt_action(work_ccb);
6081 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6082 printf("xptconfigfunc: CPI failed on bus %d "
6083 "with status %d\n", bus->path_id,
6084 work_ccb->ccb_h.status);
6085 xpt_finishconfig(xpt_periph, work_ccb);
6089 can_negotiate = work_ccb->cpi.hba_inquiry;
6090 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6091 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6092 && (can_negotiate != 0)) {
6093 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6094 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6095 work_ccb->ccb_h.cbfcnp = NULL;
6096 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6097 ("Resetting Bus\n"));
6098 xpt_action(work_ccb);
6099 xpt_finishconfig(xpt_periph, work_ccb);
6101 /* Act as though we performed a successful BUS RESET */
6102 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6103 xpt_finishconfig(xpt_periph, work_ccb);
6111 xpt_config(void *arg)
6113 /* Now that interrupts are enabled, go find our devices */
6116 /* Setup debugging flags and path */
6117 #ifdef CAM_DEBUG_FLAGS
6118 cam_dflags = CAM_DEBUG_FLAGS;
6119 #else /* !CAM_DEBUG_FLAGS */
6120 cam_dflags = CAM_DEBUG_NONE;
6121 #endif /* CAM_DEBUG_FLAGS */
6122 #ifdef CAM_DEBUG_BUS
6123 if (cam_dflags != CAM_DEBUG_NONE) {
6124 if (xpt_create_path(&cam_dpath, xpt_periph,
6125 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6126 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6127 printf("xpt_config: xpt_create_path() failed for debug"
6128 " target %d:%d:%d, debugging disabled\n",
6129 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6130 cam_dflags = CAM_DEBUG_NONE;
6134 #else /* !CAM_DEBUG_BUS */
6136 #endif /* CAM_DEBUG_BUS */
6137 #endif /* CAMDEBUG */
6140 * Scan all installed busses.
6142 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6144 if (busses_to_config == 0) {
6145 /* Call manually because we don't have any busses */
6146 xpt_finishconfig(xpt_periph, NULL);
6148 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6149 printf("Waiting %d seconds for SCSI "
6150 "devices to settle\n", SCSI_DELAY/1000);
6152 xpt_for_all_busses(xptconfigfunc, NULL);
6157 * If the given device only has one peripheral attached to it, and if that
6158 * peripheral is the passthrough driver, announce it. This insures that the
6159 * user sees some sort of announcement for every peripheral in their system.
6162 xptpassannouncefunc(struct cam_ed *device, void *arg)
6164 struct cam_periph *periph;
6167 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6168 periph = SLIST_NEXT(periph, periph_links), i++);
6170 periph = SLIST_FIRST(&device->periphs);
6172 && (strncmp(periph->periph_name, "pass", 4) == 0))
6173 xpt_announce_periph(periph, NULL);
6179 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6181 struct periph_driver **p_drv;
6184 if (done_ccb != NULL) {
6185 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6186 ("xpt_finishconfig\n"));
6187 switch(done_ccb->ccb_h.func_code) {
6189 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6190 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6191 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6192 xpt_action(done_ccb);
6198 xpt_free_path(done_ccb->ccb_h.path);
6204 if (busses_to_config == 0) {
6205 /* Register all the peripheral drivers */
6206 /* XXX This will have to change when we have loadable modules */
6207 p_drv = periph_drivers;
6208 for (i = 0; p_drv[i] != NULL; i++) {
6209 (*p_drv[i]->init)();
6213 * Check for devices with no "standard" peripheral driver
6214 * attached. For any devices like that, announce the
6215 * passthrough driver so the user will see something.
6217 xpt_for_all_devices(xptpassannouncefunc, NULL);
6219 /* Release our hook so that the boot can continue. */
6220 config_intrhook_disestablish(xpt_config_hook);
6221 free(xpt_config_hook, M_TEMP);
6222 xpt_config_hook = NULL;
6224 if (done_ccb != NULL)
6225 xpt_free_ccb(done_ccb);
6229 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6231 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6233 switch (work_ccb->ccb_h.func_code) {
6234 /* Common cases first */
6235 case XPT_PATH_INQ: /* Path routing inquiry */
6237 struct ccb_pathinq *cpi;
6239 cpi = &work_ccb->cpi;
6240 cpi->version_num = 1; /* XXX??? */
6241 cpi->hba_inquiry = 0;
6242 cpi->target_sprt = 0;
6244 cpi->hba_eng_cnt = 0;
6245 cpi->max_target = 0;
6247 cpi->initiator_id = 0;
6248 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6249 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6250 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6251 cpi->unit_number = sim->unit_number;
6252 cpi->bus_id = sim->bus_id;
6253 cpi->base_transfer_speed = 0;
6254 cpi->ccb_h.status = CAM_REQ_CMP;
6259 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6266 * The xpt as a "controller" has no interrupt sources, so polling
6270 xptpoll(struct cam_sim *sim)
6275 camisr(void *V_queue)
6277 cam_isrq_t *queue = V_queue;
6279 struct ccb_hdr *ccb_h;
6282 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6285 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6286 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6289 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6294 if (ccb_h->flags & CAM_HIGH_POWER) {
6295 struct highpowerlist *hphead;
6296 struct cam_ed *device;
6297 union ccb *send_ccb;
6299 hphead = &highpowerq;
6301 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6304 * Increment the count since this command is done.
6309 * Any high powered commands queued up?
6311 if (send_ccb != NULL) {
6312 device = send_ccb->ccb_h.path->device;
6314 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6316 xpt_release_devq(send_ccb->ccb_h.path,
6317 /*count*/1, /*runqueue*/TRUE);
6320 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6323 dev = ccb_h->path->device;
6326 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6328 ccb_h->path->bus->sim->devq->send_active--;
6329 ccb_h->path->bus->sim->devq->send_openings++;
6332 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6333 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6334 && (dev->ccbq.dev_active == 0))) {
6336 xpt_release_devq(ccb_h->path, /*count*/1,
6340 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6341 && (--dev->tag_delay_count == 0))
6342 xpt_start_tags(ccb_h->path);
6344 if ((dev->ccbq.queue.entries > 0)
6345 && (dev->qfrozen_cnt == 0)
6346 && (device_is_send_queued(dev) == 0)) {
6347 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6352 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6353 xpt_release_simq(ccb_h->path->bus->sim,
6355 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6359 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6360 && (ccb_h->status & CAM_DEV_QFRZN)) {
6361 xpt_release_devq(ccb_h->path, /*count*/1,
6363 ccb_h->status &= ~CAM_DEV_QFRZN;
6365 xpt_run_dev_sendq(ccb_h->path->bus);
6368 /* Call the peripheral driver's callback */
6369 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6371 /* Raise IPL for while test */