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>
32 #include <sys/systm.h>
33 #include <sys/types.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
40 #include <sys/devicestat.h>
41 #include <sys/interrupt.h>
45 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
48 #include <machine/clock.h>
49 #include <machine/ipl.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_debug.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/scsi/scsi_pass.h>
65 /* Datastructures internal to the xpt layer */
68 * Definition of an async handler callback block. These are used to add
69 * SIMs and peripherals to the async callback lists.
72 SLIST_ENTRY(async_node) links;
73 u_int32_t event_enable; /* Async Event enables */
74 void (*callback)(void *arg, u_int32_t code,
75 struct cam_path *path, void *args);
79 SLIST_HEAD(async_list, async_node);
80 SLIST_HEAD(periph_list, cam_periph);
81 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
84 * This is the maximum number of high powered commands (e.g. start unit)
85 * that can be outstanding at a particular time.
87 #ifndef CAM_MAX_HIGHPOWER
88 #define CAM_MAX_HIGHPOWER 4
91 /* number of high powered commands that can go through right now */
92 static int num_highpower = CAM_MAX_HIGHPOWER;
95 * Structure for queueing a device in a run queue.
96 * There is one run queue for allocating new ccbs,
97 * and another for sending ccbs to the controller.
101 struct cam_ed *device;
105 * The CAM EDT (Existing Device Table) contains the device information for
106 * all devices for all busses in the system. The table contains a
107 * cam_ed structure for each device on the bus.
110 TAILQ_ENTRY(cam_ed) links;
111 struct cam_ed_qinfo alloc_ccb_entry;
112 struct cam_ed_qinfo send_ccb_entry;
113 struct cam_et *target;
116 * Queue of type drivers wanting to do
117 * work on this device.
119 struct cam_ccbq ccbq; /* Queue of pending ccbs */
120 struct async_list asyncs; /* Async callback info for this B/T/L */
121 struct periph_list periphs; /* All attached devices */
122 u_int generation; /* Generation number */
123 struct cam_periph *owner; /* Peripheral driver's ownership tag */
124 struct xpt_quirk_entry *quirk; /* Oddities about this device */
125 /* Storage for the inquiry data */
126 struct scsi_inquiry_data inq_data;
127 u_int8_t inq_flags; /*
128 * Current settings for inquiry flags.
129 * This allows us to override settings
130 * like disconnection and tagged
131 * queuing for a device.
133 u_int8_t queue_flags; /* Queue flags from the control page */
134 u_int8_t serial_num_len;
135 u_int8_t *serial_num;
136 u_int32_t qfrozen_cnt;
138 #define CAM_DEV_UNCONFIGURED 0x01
139 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
140 #define CAM_DEV_REL_ON_COMPLETE 0x04
141 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
142 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
143 #define CAM_DEV_TAG_AFTER_COUNT 0x20
144 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
145 u_int32_t tag_delay_count;
146 #define CAM_TAG_DELAY_COUNT 5
148 struct callout_handle c_handle;
152 * Each target is represented by an ET (Existing Target). These
153 * entries are created when a target is successfully probed with an
154 * identify, and removed when a device fails to respond after a number
155 * of retries, or a bus rescan finds the device missing.
158 TAILQ_HEAD(, cam_ed) ed_entries;
159 TAILQ_ENTRY(cam_et) links;
161 target_id_t target_id;
164 struct timeval last_reset;
168 * Each bus is represented by an EB (Existing Bus). These entries
169 * are created by calls to xpt_bus_register and deleted by calls to
170 * xpt_bus_deregister.
173 TAILQ_HEAD(, cam_et) et_entries;
174 TAILQ_ENTRY(cam_eb) links;
177 struct timeval last_reset;
179 #define CAM_EB_RUNQ_SCHEDULED 0x01
185 struct cam_periph *periph;
187 struct cam_et *target;
188 struct cam_ed *device;
191 struct xpt_quirk_entry {
192 struct scsi_inquiry_pattern inq_pat;
194 #define CAM_QUIRK_NOLUNS 0x01
195 #define CAM_QUIRK_NOSERIAL 0x02
196 #define CAM_QUIRK_HILUNS 0x04
200 #define CAM_SCSI2_MAXLUN 8
208 u_int32_t generation;
211 static const char quantum[] = "QUANTUM";
212 static const char sony[] = "SONY";
213 static const char west_digital[] = "WDIGTL";
214 static const char samsung[] = "SAMSUNG";
215 static const char seagate[] = "SEAGATE";
216 static const char microp[] = "MICROP";
218 static struct xpt_quirk_entry xpt_quirk_table[] =
221 /* Reports QUEUE FULL for temporary resource shortages */
222 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
223 /*quirks*/0, /*mintags*/24, /*maxtags*/32
226 /* Reports QUEUE FULL for temporary resource shortages */
227 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
228 /*quirks*/0, /*mintags*/24, /*maxtags*/32
231 /* Reports QUEUE FULL for temporary resource shortages */
232 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
233 /*quirks*/0, /*mintags*/24, /*maxtags*/32
236 /* Broken tagged queuing drive */
237 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
238 /*quirks*/0, /*mintags*/0, /*maxtags*/0
241 /* Broken tagged queuing drive */
242 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
243 /*quirks*/0, /*mintags*/0, /*maxtags*/0
246 /* Broken tagged queuing drive */
247 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
248 /*quirks*/0, /*mintags*/0, /*maxtags*/0
252 * Unfortunately, the Quantum Atlas III has the same
253 * problem as the Atlas II drives above.
254 * Reported by: "Johan Granlund" <johan@granlund.nu>
256 * For future reference, the drive with the problem was:
257 * QUANTUM QM39100TD-SW N1B0
259 * It's possible that Quantum will fix the problem in later
260 * firmware revisions. If that happens, the quirk entry
261 * will need to be made specific to the firmware revisions
265 /* Reports QUEUE FULL for temporary resource shortages */
266 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
267 /*quirks*/0, /*mintags*/24, /*maxtags*/32
271 * 18 Gig Atlas III, same problem as the 9G version.
272 * Reported by: Andre Albsmeier
273 * <andre.albsmeier@mchp.siemens.de>
275 * For future reference, the drive with the problem was:
276 * QUANTUM QM318000TD-S N491
278 /* Reports QUEUE FULL for temporary resource shortages */
279 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
280 /*quirks*/0, /*mintags*/24, /*maxtags*/32
284 * Broken tagged queuing drive
285 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
286 * and: Martin Renters <martin@tdc.on.ca>
288 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
289 /*quirks*/0, /*mintags*/0, /*maxtags*/0
292 * The Seagate Medalist Pro drives have very poor write
293 * performance with anything more than 2 tags.
295 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
296 * Drive: <SEAGATE ST36530N 1444>
298 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
299 * Drive: <SEAGATE ST34520W 1281>
301 * No one has actually reported that the 9G version
302 * (ST39140*) of the Medalist Pro has the same problem, but
303 * we're assuming that it does because the 4G and 6.5G
304 * versions of the drive are broken.
307 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
308 /*quirks*/0, /*mintags*/2, /*maxtags*/2
311 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
312 /*quirks*/0, /*mintags*/2, /*maxtags*/2
315 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
316 /*quirks*/0, /*mintags*/2, /*maxtags*/2
320 * Slow when tagged queueing is enabled. Write performance
321 * steadily drops off with more and more concurrent
322 * transactions. Best sequential write performance with
323 * tagged queueing turned off and write caching turned on.
326 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
327 * Drive: DCAS-34330 w/ "S65A" firmware.
329 * The drive with the problem had the "S65A" firmware
330 * revision, and has also been reported (by Stephen J.
331 * Roznowski <sjr@home.net>) for a drive with the "S61A"
334 * Although no one has reported problems with the 2 gig
335 * version of the DCAS drive, the assumption is that it
336 * has the same problems as the 4 gig version. Therefore
337 * this quirk entries disables tagged queueing for all
340 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
341 /*quirks*/0, /*mintags*/0, /*maxtags*/0
344 /* Broken tagged queuing drive */
345 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
346 /*quirks*/0, /*mintags*/0, /*maxtags*/0
349 /* Broken tagged queuing drive */
350 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
351 /*quirks*/0, /*mintags*/0, /*maxtags*/0
355 * Broken tagged queuing drive.
357 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
360 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
361 /*quirks*/0, /*mintags*/0, /*maxtags*/0
365 * Slow when tagged queueing is enabled. (1.5MB/sec versus
367 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
368 * Best performance with these drives is achieved with
369 * tagged queueing turned off, and write caching turned on.
371 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
372 /*quirks*/0, /*mintags*/0, /*maxtags*/0
376 * Slow when tagged queueing is enabled. (1.5MB/sec versus
378 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
379 * Best performance with these drives is achieved with
380 * tagged queueing turned off, and write caching turned on.
382 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
383 /*quirks*/0, /*mintags*/0, /*maxtags*/0
387 * Doesn't handle queue full condition correctly,
388 * so we need to limit maxtags to what the device
389 * can handle instead of determining this automatically.
391 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
392 /*quirks*/0, /*mintags*/2, /*maxtags*/32
395 /* Really only one LUN */
396 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA*", "*" },
397 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
400 /* I can't believe we need a quirk for DPT volumes. */
401 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
402 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
403 /*mintags*/0, /*maxtags*/255
407 * Many Sony CDROM drives don't like multi-LUN probing.
409 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
410 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
414 * This drive doesn't like multiple LUN probing.
415 * Submitted by: Parag Patel <parag@cgt.com>
417 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
418 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
422 * The 8200 doesn't like multi-lun probing, and probably
423 * don't like serial number requests either.
426 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
429 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
433 * These Hitachi drives don't like multi-lun probing.
434 * The PR submitter has a DK319H, but says that the Linux
435 * kernel has a similar work-around for the DK312 and DK314,
436 * so all DK31* drives are quirked here.
438 * Submitted by: Paul Haddad <paul@pth.com>
440 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
441 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
445 * This old revision of the TDC3600 is also SCSI-1, and
446 * hangs upon serial number probing.
449 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
452 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
456 * Would repond to all LUNs if asked for.
459 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
462 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
466 * Would repond to all LUNs if asked for.
469 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
472 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
475 /* Submitted by: Matthew Dodd <winter@jurai.net> */
476 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
477 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
480 /* Submitted by: Matthew Dodd <winter@jurai.net> */
481 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
482 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
485 /* Default tagged queuing parameters for all devices */
487 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
488 /*vendor*/"*", /*product*/"*", /*revision*/"*"
490 /*quirks*/0, /*mintags*/2, /*maxtags*/255
494 static const int xpt_quirk_table_size =
495 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
499 DM_RET_FLAG_MASK = 0x0f,
502 DM_RET_DESCEND = 0x20,
504 DM_RET_ACTION_MASK = 0xf0
512 } xpt_traverse_depth;
514 struct xpt_traverse_config {
515 xpt_traverse_depth depth;
520 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
521 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
522 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
523 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
524 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
526 /* Transport layer configuration information */
527 static struct xpt_softc xsoftc;
529 /* Queues for our software interrupt handler */
530 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
531 static cam_isrq_t cam_bioq;
532 static cam_isrq_t cam_netq;
534 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
535 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
536 static u_int xpt_max_ccbs; /*
537 * Maximum size of ccb pool. Modified as
538 * devices are added/removed or have their
539 * opening counts changed.
541 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
543 struct cam_periph *xpt_periph;
545 static periph_init_t xpt_periph_init;
547 static periph_init_t probe_periph_init;
549 static struct periph_driver xpt_driver =
551 xpt_periph_init, "xpt",
552 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
555 static struct periph_driver probe_driver =
557 probe_periph_init, "probe",
558 TAILQ_HEAD_INITIALIZER(probe_driver.units)
561 DATA_SET(periphdriver_set, xpt_driver);
562 DATA_SET(periphdriver_set, probe_driver);
564 #define XPT_CDEV_MAJOR 104
566 static d_open_t xptopen;
567 static d_close_t xptclose;
568 static d_ioctl_t xptioctl;
570 static struct cdevsw xpt_cdevsw = {
572 /* close */ xptclose,
575 /* ioctl */ xptioctl,
578 /* strategy */ nostrategy,
580 /* maj */ XPT_CDEV_MAJOR,
587 static struct intr_config_hook *xpt_config_hook;
589 /* Registered busses */
590 static TAILQ_HEAD(,cam_eb) xpt_busses;
591 static u_int bus_generation;
593 /* Storage for debugging datastructures */
595 struct cam_path *cam_dpath;
596 u_int32_t cam_dflags;
597 u_int32_t cam_debug_delay;
600 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
601 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
605 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
606 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
607 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
609 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
610 || defined(CAM_DEBUG_LUN)
612 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
613 || !defined(CAM_DEBUG_LUN)
614 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
616 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
617 #else /* !CAMDEBUG */
618 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
619 #endif /* CAMDEBUG */
620 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
622 /* Our boot-time initialization hook */
623 static void xpt_init(void *);
624 SYSINIT(cam, SI_SUB_CONFIGURE, SI_ORDER_SECOND, xpt_init, NULL);
626 static cam_status xpt_compile_path(struct cam_path *new_path,
627 struct cam_periph *perph,
629 target_id_t target_id,
632 static void xpt_release_path(struct cam_path *path);
634 static void xpt_async_bcast(struct async_list *async_head,
635 u_int32_t async_code,
636 struct cam_path *path,
638 static path_id_t xptnextfreepathid(void);
639 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
640 static union ccb *xpt_get_ccb(struct cam_ed *device);
641 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
642 u_int32_t new_priority);
643 static void xpt_run_dev_allocq(struct cam_eb *bus);
644 static void xpt_run_dev_sendq(struct cam_eb *bus);
645 static timeout_t xpt_release_devq_timeout;
646 static timeout_t xpt_release_simq_timeout;
647 static void xpt_release_bus(struct cam_eb *bus);
648 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
650 static struct cam_et*
651 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
652 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
653 static struct cam_ed*
654 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
656 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
657 struct cam_ed *device);
658 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
659 static struct cam_eb*
660 xpt_find_bus(path_id_t path_id);
661 static struct cam_et*
662 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
663 static struct cam_ed*
664 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
665 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
666 static void xpt_scan_lun(struct cam_periph *periph,
667 struct cam_path *path, cam_flags flags,
669 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
670 static xpt_busfunc_t xptconfigbuscountfunc;
671 static xpt_busfunc_t xptconfigfunc;
672 static void xpt_config(void *arg);
673 static xpt_devicefunc_t xptpassannouncefunc;
674 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
675 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
676 static void xptpoll(struct cam_sim *sim);
677 static swihand_t swi_camnet;
678 static swihand_t swi_cambio;
679 static void camisr(cam_isrq_t *queue);
681 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
682 static void xptasync(struct cam_periph *periph,
683 u_int32_t code, cam_path *path);
685 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
686 int num_patterns, struct cam_eb *bus);
687 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
688 int num_patterns, struct cam_ed *device);
689 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
691 struct cam_periph *periph);
692 static xpt_busfunc_t xptedtbusfunc;
693 static xpt_targetfunc_t xptedttargetfunc;
694 static xpt_devicefunc_t xptedtdevicefunc;
695 static xpt_periphfunc_t xptedtperiphfunc;
696 static xpt_pdrvfunc_t xptplistpdrvfunc;
697 static xpt_periphfunc_t xptplistperiphfunc;
698 static int xptedtmatch(struct ccb_dev_match *cdm);
699 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
700 static int xptbustraverse(struct cam_eb *start_bus,
701 xpt_busfunc_t *tr_func, void *arg);
702 static int xpttargettraverse(struct cam_eb *bus,
703 struct cam_et *start_target,
704 xpt_targetfunc_t *tr_func, void *arg);
705 static int xptdevicetraverse(struct cam_et *target,
706 struct cam_ed *start_device,
707 xpt_devicefunc_t *tr_func, void *arg);
708 static int xptperiphtraverse(struct cam_ed *device,
709 struct cam_periph *start_periph,
710 xpt_periphfunc_t *tr_func, void *arg);
711 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
712 xpt_pdrvfunc_t *tr_func, void *arg);
713 static int xptpdperiphtraverse(struct periph_driver **pdrv,
714 struct cam_periph *start_periph,
715 xpt_periphfunc_t *tr_func,
717 static xpt_busfunc_t xptdefbusfunc;
718 static xpt_targetfunc_t xptdeftargetfunc;
719 static xpt_devicefunc_t xptdefdevicefunc;
720 static xpt_periphfunc_t xptdefperiphfunc;
721 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
723 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
726 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
729 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
732 static xpt_devicefunc_t xptsetasyncfunc;
733 static xpt_busfunc_t xptsetasyncbusfunc;
734 static cam_status xptregister(struct cam_periph *periph,
736 static cam_status proberegister(struct cam_periph *periph,
738 static void probeschedule(struct cam_periph *probe_periph);
739 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
740 static void proberequestdefaultnegotiation(struct cam_periph *periph);
741 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
742 static void probecleanup(struct cam_periph *periph);
743 static void xpt_find_quirk(struct cam_ed *device);
744 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
745 struct cam_ed *device,
747 static void xpt_toggle_tags(struct cam_path *path);
748 static void xpt_start_tags(struct cam_path *path);
749 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
751 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
753 static __inline int periph_is_queued(struct cam_periph *periph);
754 static __inline int device_is_alloc_queued(struct cam_ed *device);
755 static __inline int device_is_send_queued(struct cam_ed *device);
756 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
759 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
763 if (dev->ccbq.devq_openings > 0) {
764 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
765 cam_ccbq_resize(&dev->ccbq,
766 dev->ccbq.dev_openings
767 + dev->ccbq.dev_active);
768 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
771 * The priority of a device waiting for CCB resources
772 * is that of the the highest priority peripheral driver
775 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
776 &dev->alloc_ccb_entry.pinfo,
777 CAMQ_GET_HEAD(&dev->drvq)->priority);
786 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
790 if (dev->ccbq.dev_openings > 0) {
792 * The priority of a device waiting for controller
793 * resources is that of the the highest priority CCB
797 xpt_schedule_dev(&bus->sim->devq->send_queue,
798 &dev->send_ccb_entry.pinfo,
799 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
807 periph_is_queued(struct cam_periph *periph)
809 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
813 device_is_alloc_queued(struct cam_ed *device)
815 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
819 device_is_send_queued(struct cam_ed *device)
821 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
825 dev_allocq_is_runnable(struct cam_devq *devq)
829 * Have space to do more work.
830 * Allowed to do work.
832 return ((devq->alloc_queue.qfrozen_cnt == 0)
833 && (devq->alloc_queue.entries > 0)
834 && (devq->alloc_openings > 0));
840 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
850 xptdone(struct cam_periph *periph, union ccb *done_ccb)
852 /* Caller will release the CCB */
853 wakeup(&done_ccb->ccb_h.cbfcnp);
857 xptopen(dev_t dev, int flags, int fmt, struct proc *p)
861 unit = minor(dev) & 0xff;
864 * Only allow read-write access.
866 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
870 * We don't allow nonblocking access.
872 if ((flags & O_NONBLOCK) != 0) {
873 printf("xpt%d: can't do nonblocking accesss\n", unit);
878 * We only have one transport layer right now. If someone accesses
879 * us via something other than minor number 1, point out their
883 printf("xptopen: got invalid xpt unit %d\n", unit);
887 /* Mark ourselves open */
888 xsoftc.flags |= XPT_FLAG_OPEN;
894 xptclose(dev_t dev, int flag, int fmt, struct proc *p)
898 unit = minor(dev) & 0xff;
901 * We only have one transport layer right now. If someone accesses
902 * us via something other than minor number 1, point out their
906 printf("xptclose: got invalid xpt unit %d\n", unit);
910 /* Mark ourselves closed */
911 xsoftc.flags &= ~XPT_FLAG_OPEN;
917 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
922 unit = minor(dev) & 0xff;
925 * We only have one transport layer right now. If someone accesses
926 * us via something other than minor number 1, point out their
930 printf("xptioctl: got invalid xpt unit %d\n", unit);
936 * For the transport layer CAMIOCOMMAND ioctl, we really only want
937 * to accept CCB types that don't quite make sense to send through a
938 * passthrough driver.
944 inccb = (union ccb *)addr;
946 switch(inccb->ccb_h.func_code) {
949 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
950 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
957 ccb = xpt_alloc_ccb();
960 * Create a path using the bus, target, and lun the
963 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
964 inccb->ccb_h.path_id,
965 inccb->ccb_h.target_id,
966 inccb->ccb_h.target_lun) !=
972 /* Ensure all of our fields are correct */
973 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
974 inccb->ccb_h.pinfo.priority);
975 xpt_merge_ccb(ccb, inccb);
976 ccb->ccb_h.cbfcnp = xptdone;
977 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
978 bcopy(ccb, inccb, sizeof(union ccb));
979 xpt_free_path(ccb->ccb_h.path);
987 * This is an immediate CCB, so it's okay to
988 * allocate it on the stack.
992 * Create a path using the bus, target, and lun the
995 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
996 inccb->ccb_h.path_id,
997 inccb->ccb_h.target_id,
998 inccb->ccb_h.target_lun) !=
1003 /* Ensure all of our fields are correct */
1004 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1005 inccb->ccb_h.pinfo.priority);
1006 xpt_merge_ccb(&ccb, inccb);
1007 ccb.ccb_h.cbfcnp = xptdone;
1009 bcopy(&ccb, inccb, sizeof(union ccb));
1010 xpt_free_path(ccb.ccb_h.path);
1014 case XPT_DEV_MATCH: {
1015 struct cam_periph_map_info mapinfo;
1016 struct cam_path *old_path;
1019 * We can't deal with physical addresses for this
1020 * type of transaction.
1022 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1028 * Save this in case the caller had it set to
1029 * something in particular.
1031 old_path = inccb->ccb_h.path;
1034 * We really don't need a path for the matching
1035 * code. The path is needed because of the
1036 * debugging statements in xpt_action(). They
1037 * assume that the CCB has a valid path.
1039 inccb->ccb_h.path = xpt_periph->path;
1041 bzero(&mapinfo, sizeof(mapinfo));
1044 * Map the pattern and match buffers into kernel
1045 * virtual address space.
1047 error = cam_periph_mapmem(inccb, &mapinfo);
1050 inccb->ccb_h.path = old_path;
1055 * This is an immediate CCB, we can send it on directly.
1060 * Map the buffers back into user space.
1062 cam_periph_unmapmem(inccb, &mapinfo);
1064 inccb->ccb_h.path = old_path;
1076 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1077 * with the periphal driver name and unit name filled in. The other
1078 * fields don't really matter as input. The passthrough driver name
1079 * ("pass"), and unit number are passed back in the ccb. The current
1080 * device generation number, and the index into the device peripheral
1081 * driver list, and the status are also passed back. Note that
1082 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1083 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1084 * (or rather should be) impossible for the device peripheral driver
1085 * list to change since we look at the whole thing in one pass, and
1086 * we do it with splcam protection.
1089 case CAMGETPASSTHRU: {
1091 struct cam_periph *periph;
1092 struct periph_driver **p_drv;
1096 int base_periph_found;
1100 ccb = (union ccb *)addr;
1101 unit = ccb->cgdl.unit_number;
1102 name = ccb->cgdl.periph_name;
1104 * Every 100 devices, we want to drop our spl protection to
1105 * give the software interrupt handler a chance to run.
1106 * Most systems won't run into this check, but this should
1107 * avoid starvation in the software interrupt handler in
1112 ccb = (union ccb *)addr;
1114 base_periph_found = 0;
1117 * Sanity check -- make sure we don't get a null peripheral
1120 if (*ccb->cgdl.periph_name == '\0') {
1125 /* Keep the list from changing while we traverse it */
1128 cur_generation = xsoftc.generation;
1130 /* first find our driver in the list of drivers */
1131 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
1132 *p_drv != NULL; p_drv++)
1133 if (strcmp((*p_drv)->driver_name, name) == 0)
1136 if (*p_drv == NULL) {
1138 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1139 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1140 *ccb->cgdl.periph_name = '\0';
1141 ccb->cgdl.unit_number = 0;
1147 * Run through every peripheral instance of this driver
1148 * and check to see whether it matches the unit passed
1149 * in by the user. If it does, get out of the loops and
1150 * find the passthrough driver associated with that
1151 * peripheral driver.
1153 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1154 periph = TAILQ_NEXT(periph, unit_links)) {
1156 if (periph->unit_number == unit) {
1158 } else if (--splbreaknum == 0) {
1162 if (cur_generation != xsoftc.generation)
1167 * If we found the peripheral driver that the user passed
1168 * in, go through all of the peripheral drivers for that
1169 * particular device and look for a passthrough driver.
1171 if (periph != NULL) {
1172 struct cam_ed *device;
1175 base_periph_found = 1;
1176 device = periph->path->device;
1177 for (i = 0, periph = device->periphs.slh_first;
1179 periph = periph->periph_links.sle_next, i++) {
1181 * Check to see whether we have a
1182 * passthrough device or not.
1184 if (strcmp(periph->periph_name, "pass") == 0) {
1186 * Fill in the getdevlist fields.
1188 strcpy(ccb->cgdl.periph_name,
1189 periph->periph_name);
1190 ccb->cgdl.unit_number =
1191 periph->unit_number;
1192 if (periph->periph_links.sle_next)
1194 CAM_GDEVLIST_MORE_DEVS;
1197 CAM_GDEVLIST_LAST_DEVICE;
1198 ccb->cgdl.generation =
1200 ccb->cgdl.index = i;
1202 * Fill in some CCB header fields
1203 * that the user may want.
1205 ccb->ccb_h.path_id =
1206 periph->path->bus->path_id;
1207 ccb->ccb_h.target_id =
1208 periph->path->target->target_id;
1209 ccb->ccb_h.target_lun =
1210 periph->path->device->lun_id;
1211 ccb->ccb_h.status = CAM_REQ_CMP;
1218 * If the periph is null here, one of two things has
1219 * happened. The first possibility is that we couldn't
1220 * find the unit number of the particular peripheral driver
1221 * that the user is asking about. e.g. the user asks for
1222 * the passthrough driver for "da11". We find the list of
1223 * "da" peripherals all right, but there is no unit 11.
1224 * The other possibility is that we went through the list
1225 * of peripheral drivers attached to the device structure,
1226 * but didn't find one with the name "pass". Either way,
1227 * we return ENOENT, since we couldn't find something.
1229 if (periph == NULL) {
1230 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1231 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1232 *ccb->cgdl.periph_name = '\0';
1233 ccb->cgdl.unit_number = 0;
1236 * It is unfortunate that this is even necessary,
1237 * but there are many, many clueless users out there.
1238 * If this is true, the user is looking for the
1239 * passthrough driver, but doesn't have one in his
1242 if (base_periph_found == 1) {
1243 printf("xptioctl: pass driver is not in the "
1245 printf("xptioctl: put \"device pass0\" in "
1246 "your kernel config file\n");
1260 /* Functions accessed by the peripheral drivers */
1265 struct cam_sim *xpt_sim;
1266 struct cam_path *path;
1267 struct cam_devq *devq;
1270 TAILQ_INIT(&xpt_busses);
1271 TAILQ_INIT(&cam_bioq);
1272 TAILQ_INIT(&cam_netq);
1273 SLIST_INIT(&ccb_freeq);
1274 STAILQ_INIT(&highpowerq);
1277 * The xpt layer is, itself, the equivelent of a SIM.
1278 * Allow 16 ccbs in the ccb pool for it. This should
1279 * give decent parallelism when we probe busses and
1280 * perform other XPT functions.
1282 devq = cam_simq_alloc(16);
1283 xpt_sim = cam_sim_alloc(xptaction,
1288 /*max_dev_transactions*/0,
1289 /*max_tagged_dev_transactions*/0,
1293 xpt_bus_register(xpt_sim, /*bus #*/0);
1296 * Looking at the XPT from the SIM layer, the XPT is
1297 * the equivelent of a peripheral driver. Allocate
1298 * a peripheral driver entry for us.
1300 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1301 CAM_TARGET_WILDCARD,
1302 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1303 printf("xpt_init: xpt_create_path failed with status %#x,"
1304 " failing attach\n", status);
1308 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1309 path, NULL, 0, NULL);
1310 xpt_free_path(path);
1312 xpt_sim->softc = xpt_periph;
1315 * Register a callback for when interrupts are enabled.
1318 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1320 if (xpt_config_hook == NULL) {
1321 printf("xpt_init: Cannot malloc config hook "
1322 "- failing attach\n");
1325 bzero(xpt_config_hook, sizeof(*xpt_config_hook));
1327 xpt_config_hook->ich_func = xpt_config;
1328 if (config_intrhook_establish(xpt_config_hook) != 0) {
1329 free (xpt_config_hook, M_TEMP);
1330 printf("xpt_init: config_intrhook_establish failed "
1331 "- failing attach\n");
1334 /* Install our software interrupt handlers */
1335 register_swi(SWI_CAMNET, swi_camnet);
1336 register_swi(SWI_CAMBIO, swi_cambio);
1340 xptregister(struct cam_periph *periph, void *arg)
1342 if (periph == NULL) {
1343 printf("xptregister: periph was NULL!!\n");
1344 return(CAM_REQ_CMP_ERR);
1347 periph->softc = NULL;
1349 xpt_periph = periph;
1351 return(CAM_REQ_CMP);
1355 xpt_add_periph(struct cam_periph *periph)
1357 struct cam_ed *device;
1359 struct periph_list *periph_head;
1361 device = periph->path->device;
1363 periph_head = &device->periphs;
1365 status = CAM_REQ_CMP;
1367 if (device != NULL) {
1371 * Make room for this peripheral
1372 * so it will fit in the queue
1373 * when it's scheduled to run
1376 status = camq_resize(&device->drvq,
1377 device->drvq.array_size + 1);
1379 device->generation++;
1381 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1386 xsoftc.generation++;
1392 xpt_remove_periph(struct cam_periph *periph)
1394 struct cam_ed *device;
1396 device = periph->path->device;
1398 if (device != NULL) {
1400 struct periph_list *periph_head;
1402 periph_head = &device->periphs;
1404 /* Release the slot for this peripheral */
1406 camq_resize(&device->drvq, device->drvq.array_size - 1);
1408 device->generation++;
1410 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1415 xsoftc.generation++;
1420 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1424 struct cam_path *path;
1425 struct ccb_trans_settings cts;
1427 path = periph->path;
1429 * To ensure that this is printed in one piece,
1430 * mask out CAM interrupts.
1433 printf("%s%d at %s%d bus %d target %d lun %d\n",
1434 periph->periph_name, periph->unit_number,
1435 path->bus->sim->sim_name,
1436 path->bus->sim->unit_number,
1437 path->bus->sim->bus_id,
1438 path->target->target_id,
1439 path->device->lun_id);
1440 printf("%s%d: ", periph->periph_name, periph->unit_number);
1441 scsi_print_inquiry(&path->device->inq_data);
1443 && (path->device->serial_num_len > 0)) {
1444 /* Don't wrap the screen - print only the first 60 chars */
1445 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1446 periph->unit_number, path->device->serial_num);
1448 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1449 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1450 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1451 xpt_action((union ccb*)&cts);
1452 if (cts.ccb_h.status == CAM_REQ_CMP) {
1456 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1457 && cts.sync_offset != 0) {
1458 freq = scsi_calc_syncsrate(cts.sync_period);
1461 struct ccb_pathinq cpi;
1463 /* Ask the SIM for its base transfer speed */
1464 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1465 cpi.ccb_h.func_code = XPT_PATH_INQ;
1466 xpt_action((union ccb *)&cpi);
1468 speed = cpi.base_transfer_speed;
1471 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1472 speed *= (0x01 << cts.bus_width);
1475 printf("%s%d: %d.%03dMB/s transfers",
1476 periph->periph_name, periph->unit_number,
1479 printf("%s%d: %dKB/s transfers", periph->periph_name,
1480 periph->unit_number, speed);
1481 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1482 && cts.sync_offset != 0) {
1483 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1484 freq % 1000, cts.sync_offset);
1486 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1487 && cts.bus_width > 0) {
1488 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1489 && cts.sync_offset != 0) {
1494 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1495 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1496 && cts.sync_offset != 0) {
1500 if (path->device->inq_flags & SID_CmdQue
1501 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1502 printf(", Tagged Queueing Enabled");
1506 } else if (path->device->inq_flags & SID_CmdQue
1507 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1508 printf("%s%d: Tagged Queueing Enabled\n",
1509 periph->periph_name, periph->unit_number);
1513 * We only want to print the caller's announce string if they've
1516 if (announce_string != NULL)
1517 printf("%s%d: %s\n", periph->periph_name,
1518 periph->unit_number, announce_string);
1523 static dev_match_ret
1524 xptbusmatch(struct dev_match_pattern *patterns, int num_patterns,
1527 dev_match_ret retval;
1530 retval = DM_RET_NONE;
1533 * If we aren't given something to match against, that's an error.
1536 return(DM_RET_ERROR);
1539 * If there are no match entries, then this bus matches no
1542 if ((patterns == NULL) || (num_patterns == 0))
1543 return(DM_RET_DESCEND | DM_RET_COPY);
1545 for (i = 0; i < num_patterns; i++) {
1546 struct bus_match_pattern *cur_pattern;
1549 * If the pattern in question isn't for a bus node, we
1550 * aren't interested. However, we do indicate to the
1551 * calling routine that we should continue descending the
1552 * tree, since the user wants to match against lower-level
1555 if (patterns[i].type != DEV_MATCH_BUS) {
1556 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1557 retval |= DM_RET_DESCEND;
1561 cur_pattern = &patterns[i].pattern.bus_pattern;
1564 * If they want to match any bus node, we give them any
1567 if (cur_pattern->flags == BUS_MATCH_ANY) {
1568 /* set the copy flag */
1569 retval |= DM_RET_COPY;
1572 * If we've already decided on an action, go ahead
1575 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1580 * Not sure why someone would do this...
1582 if (cur_pattern->flags == BUS_MATCH_NONE)
1585 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1586 && (cur_pattern->path_id != bus->path_id))
1589 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1590 && (cur_pattern->bus_id != bus->sim->bus_id))
1593 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1594 && (cur_pattern->unit_number != bus->sim->unit_number))
1597 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1598 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1603 * If we get to this point, the user definitely wants
1604 * information on this bus. So tell the caller to copy the
1607 retval |= DM_RET_COPY;
1610 * If the return action has been set to descend, then we
1611 * know that we've already seen a non-bus matching
1612 * expression, therefore we need to further descend the tree.
1613 * This won't change by continuing around the loop, so we
1614 * go ahead and return. If we haven't seen a non-bus
1615 * matching expression, we keep going around the loop until
1616 * we exhaust the matching expressions. We'll set the stop
1617 * flag once we fall out of the loop.
1619 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1624 * If the return action hasn't been set to descend yet, that means
1625 * we haven't seen anything other than bus matching patterns. So
1626 * tell the caller to stop descending the tree -- the user doesn't
1627 * want to match against lower level tree elements.
1629 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1630 retval |= DM_RET_STOP;
1635 static dev_match_ret
1636 xptdevicematch(struct dev_match_pattern *patterns, int num_patterns,
1637 struct cam_ed *device)
1639 dev_match_ret retval;
1642 retval = DM_RET_NONE;
1645 * If we aren't given something to match against, that's an error.
1648 return(DM_RET_ERROR);
1651 * If there are no match entries, then this device matches no
1654 if ((patterns == NULL) || (patterns == 0))
1655 return(DM_RET_DESCEND | DM_RET_COPY);
1657 for (i = 0; i < num_patterns; i++) {
1658 struct device_match_pattern *cur_pattern;
1661 * If the pattern in question isn't for a device node, we
1662 * aren't interested.
1664 if (patterns[i].type != DEV_MATCH_DEVICE) {
1665 if ((patterns[i].type == DEV_MATCH_PERIPH)
1666 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1667 retval |= DM_RET_DESCEND;
1671 cur_pattern = &patterns[i].pattern.device_pattern;
1674 * If they want to match any device node, we give them any
1677 if (cur_pattern->flags == DEV_MATCH_ANY) {
1678 /* set the copy flag */
1679 retval |= DM_RET_COPY;
1683 * If we've already decided on an action, go ahead
1686 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1691 * Not sure why someone would do this...
1693 if (cur_pattern->flags == DEV_MATCH_NONE)
1696 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1697 && (cur_pattern->path_id != device->target->bus->path_id))
1700 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1701 && (cur_pattern->target_id != device->target->target_id))
1704 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1705 && (cur_pattern->target_lun != device->lun_id))
1708 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1709 && (cam_quirkmatch((caddr_t)&device->inq_data,
1710 (caddr_t)&cur_pattern->inq_pat,
1711 1, sizeof(cur_pattern->inq_pat),
1712 scsi_static_inquiry_match) == NULL))
1716 * If we get to this point, the user definitely wants
1717 * information on this device. So tell the caller to copy
1720 retval |= DM_RET_COPY;
1723 * If the return action has been set to descend, then we
1724 * know that we've already seen a peripheral matching
1725 * expression, therefore we need to further descend the tree.
1726 * This won't change by continuing around the loop, so we
1727 * go ahead and return. If we haven't seen a peripheral
1728 * matching expression, we keep going around the loop until
1729 * we exhaust the matching expressions. We'll set the stop
1730 * flag once we fall out of the loop.
1732 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1737 * If the return action hasn't been set to descend yet, that means
1738 * we haven't seen any peripheral matching patterns. So tell the
1739 * caller to stop descending the tree -- the user doesn't want to
1740 * match against lower level tree elements.
1742 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1743 retval |= DM_RET_STOP;
1749 * Match a single peripheral against any number of match patterns.
1751 static dev_match_ret
1752 xptperiphmatch(struct dev_match_pattern *patterns, int num_patterns,
1753 struct cam_periph *periph)
1755 dev_match_ret retval;
1759 * If we aren't given something to match against, that's an error.
1762 return(DM_RET_ERROR);
1765 * If there are no match entries, then this peripheral matches no
1768 if ((patterns == NULL) || (num_patterns == 0))
1769 return(DM_RET_STOP | DM_RET_COPY);
1772 * There aren't any nodes below a peripheral node, so there's no
1773 * reason to descend the tree any further.
1775 retval = DM_RET_STOP;
1777 for (i = 0; i < num_patterns; i++) {
1778 struct periph_match_pattern *cur_pattern;
1781 * If the pattern in question isn't for a peripheral, we
1782 * aren't interested.
1784 if (patterns[i].type != DEV_MATCH_PERIPH)
1787 cur_pattern = &patterns[i].pattern.periph_pattern;
1790 * If they want to match on anything, then we will do so.
1792 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1793 /* set the copy flag */
1794 retval |= DM_RET_COPY;
1797 * We've already set the return action to stop,
1798 * since there are no nodes below peripherals in
1805 * Not sure why someone would do this...
1807 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1810 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1811 && (cur_pattern->path_id != periph->path->bus->path_id))
1815 * For the target and lun id's, we have to make sure the
1816 * target and lun pointers aren't NULL. The xpt peripheral
1817 * has a wildcard target and device.
1819 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1820 && ((periph->path->target == NULL)
1821 ||(cur_pattern->target_id != periph->path->target->target_id)))
1824 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1825 && ((periph->path->device == NULL)
1826 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1829 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1830 && (cur_pattern->unit_number != periph->unit_number))
1833 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1834 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1839 * If we get to this point, the user definitely wants
1840 * information on this peripheral. So tell the caller to
1841 * copy the data out.
1843 retval |= DM_RET_COPY;
1846 * The return action has already been set to stop, since
1847 * peripherals don't have any nodes below them in the EDT.
1853 * If we get to this point, the peripheral that was passed in
1854 * doesn't match any of the patterns.
1860 xptedtbusfunc(struct cam_eb *bus, void *arg)
1862 struct ccb_dev_match *cdm;
1863 dev_match_ret retval;
1865 cdm = (struct ccb_dev_match *)arg;
1868 * If our position is for something deeper in the tree, that means
1869 * that we've already seen this node. So, we keep going down.
1871 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1872 && (cdm->pos.cookie.bus == bus)
1873 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1874 && (cdm->pos.cookie.target != NULL))
1875 retval = DM_RET_DESCEND;
1877 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1880 * If we got an error, bail out of the search.
1882 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1883 cdm->status = CAM_DEV_MATCH_ERROR;
1888 * If the copy flag is set, copy this bus out.
1890 if (retval & DM_RET_COPY) {
1893 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1894 sizeof(struct dev_match_result));
1897 * If we don't have enough space to put in another
1898 * match result, save our position and tell the
1899 * user there are more devices to check.
1901 if (spaceleft < sizeof(struct dev_match_result)) {
1902 bzero(&cdm->pos, sizeof(cdm->pos));
1903 cdm->pos.position_type =
1904 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1906 cdm->pos.cookie.bus = bus;
1907 cdm->pos.generations[CAM_BUS_GENERATION]=
1909 cdm->status = CAM_DEV_MATCH_MORE;
1912 j = cdm->num_matches;
1914 cdm->matches[j].type = DEV_MATCH_BUS;
1915 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1916 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1917 cdm->matches[j].result.bus_result.unit_number =
1918 bus->sim->unit_number;
1919 strncpy(cdm->matches[j].result.bus_result.dev_name,
1920 bus->sim->sim_name, DEV_IDLEN);
1924 * If the user is only interested in busses, there's no
1925 * reason to descend to the next level in the tree.
1927 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1931 * If there is a target generation recorded, check it to
1932 * make sure the target list hasn't changed.
1934 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1935 && (bus == cdm->pos.cookie.bus)
1936 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1937 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1938 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1940 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1944 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1945 && (cdm->pos.cookie.bus == bus)
1946 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1947 && (cdm->pos.cookie.target != NULL))
1948 return(xpttargettraverse(bus,
1949 (struct cam_et *)cdm->pos.cookie.target,
1950 xptedttargetfunc, arg));
1952 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1956 xptedttargetfunc(struct cam_et *target, void *arg)
1958 struct ccb_dev_match *cdm;
1960 cdm = (struct ccb_dev_match *)arg;
1963 * If there is a device list generation recorded, check it to
1964 * make sure the device list hasn't changed.
1966 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1967 && (cdm->pos.cookie.bus == target->bus)
1968 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1969 && (cdm->pos.cookie.target == target)
1970 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1971 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1972 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1973 target->generation)) {
1974 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1978 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1979 && (cdm->pos.cookie.bus == target->bus)
1980 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1981 && (cdm->pos.cookie.target == target)
1982 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1983 && (cdm->pos.cookie.device != NULL))
1984 return(xptdevicetraverse(target,
1985 (struct cam_ed *)cdm->pos.cookie.device,
1986 xptedtdevicefunc, arg));
1988 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1992 xptedtdevicefunc(struct cam_ed *device, void *arg)
1995 struct ccb_dev_match *cdm;
1996 dev_match_ret retval;
1998 cdm = (struct ccb_dev_match *)arg;
2001 * If our position is for something deeper in the tree, that means
2002 * that we've already seen this node. So, we keep going down.
2004 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2005 && (cdm->pos.cookie.device == device)
2006 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2007 && (cdm->pos.cookie.periph != NULL))
2008 retval = DM_RET_DESCEND;
2010 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2013 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2014 cdm->status = CAM_DEV_MATCH_ERROR;
2019 * If the copy flag is set, copy this device out.
2021 if (retval & DM_RET_COPY) {
2024 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2025 sizeof(struct dev_match_result));
2028 * If we don't have enough space to put in another
2029 * match result, save our position and tell the
2030 * user there are more devices to check.
2032 if (spaceleft < sizeof(struct dev_match_result)) {
2033 bzero(&cdm->pos, sizeof(cdm->pos));
2034 cdm->pos.position_type =
2035 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2036 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2038 cdm->pos.cookie.bus = device->target->bus;
2039 cdm->pos.generations[CAM_BUS_GENERATION]=
2041 cdm->pos.cookie.target = device->target;
2042 cdm->pos.generations[CAM_TARGET_GENERATION] =
2043 device->target->bus->generation;
2044 cdm->pos.cookie.device = device;
2045 cdm->pos.generations[CAM_DEV_GENERATION] =
2046 device->target->generation;
2047 cdm->status = CAM_DEV_MATCH_MORE;
2050 j = cdm->num_matches;
2052 cdm->matches[j].type = DEV_MATCH_DEVICE;
2053 cdm->matches[j].result.device_result.path_id =
2054 device->target->bus->path_id;
2055 cdm->matches[j].result.device_result.target_id =
2056 device->target->target_id;
2057 cdm->matches[j].result.device_result.target_lun =
2059 bcopy(&device->inq_data,
2060 &cdm->matches[j].result.device_result.inq_data,
2061 sizeof(struct scsi_inquiry_data));
2063 /* Let the user know whether this device is unconfigured */
2064 if (device->flags & CAM_DEV_UNCONFIGURED)
2065 cdm->matches[j].result.device_result.flags =
2066 DEV_RESULT_UNCONFIGURED;
2068 cdm->matches[j].result.device_result.flags =
2073 * If the user isn't interested in peripherals, don't descend
2074 * the tree any further.
2076 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2080 * If there is a peripheral list generation recorded, make sure
2081 * it hasn't changed.
2083 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2084 && (device->target->bus == cdm->pos.cookie.bus)
2085 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2086 && (device->target == cdm->pos.cookie.target)
2087 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2088 && (device == cdm->pos.cookie.device)
2089 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2090 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2091 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2092 device->generation)){
2093 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2097 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2098 && (cdm->pos.cookie.bus == device->target->bus)
2099 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2100 && (cdm->pos.cookie.target == device->target)
2101 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2102 && (cdm->pos.cookie.device == device)
2103 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2104 && (cdm->pos.cookie.periph != NULL))
2105 return(xptperiphtraverse(device,
2106 (struct cam_periph *)cdm->pos.cookie.periph,
2107 xptedtperiphfunc, arg));
2109 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2113 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2115 struct ccb_dev_match *cdm;
2116 dev_match_ret retval;
2118 cdm = (struct ccb_dev_match *)arg;
2120 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2122 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2123 cdm->status = CAM_DEV_MATCH_ERROR;
2128 * If the copy flag is set, copy this peripheral out.
2130 if (retval & DM_RET_COPY) {
2133 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2134 sizeof(struct dev_match_result));
2137 * If we don't have enough space to put in another
2138 * match result, save our position and tell the
2139 * user there are more devices to check.
2141 if (spaceleft < sizeof(struct dev_match_result)) {
2142 bzero(&cdm->pos, sizeof(cdm->pos));
2143 cdm->pos.position_type =
2144 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2145 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2148 cdm->pos.cookie.bus = periph->path->bus;
2149 cdm->pos.generations[CAM_BUS_GENERATION]=
2151 cdm->pos.cookie.target = periph->path->target;
2152 cdm->pos.generations[CAM_TARGET_GENERATION] =
2153 periph->path->bus->generation;
2154 cdm->pos.cookie.device = periph->path->device;
2155 cdm->pos.generations[CAM_DEV_GENERATION] =
2156 periph->path->target->generation;
2157 cdm->pos.cookie.periph = periph;
2158 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2159 periph->path->device->generation;
2160 cdm->status = CAM_DEV_MATCH_MORE;
2164 j = cdm->num_matches;
2166 cdm->matches[j].type = DEV_MATCH_PERIPH;
2167 cdm->matches[j].result.periph_result.path_id =
2168 periph->path->bus->path_id;
2169 cdm->matches[j].result.periph_result.target_id =
2170 periph->path->target->target_id;
2171 cdm->matches[j].result.periph_result.target_lun =
2172 periph->path->device->lun_id;
2173 cdm->matches[j].result.periph_result.unit_number =
2174 periph->unit_number;
2175 strncpy(cdm->matches[j].result.periph_result.periph_name,
2176 periph->periph_name, DEV_IDLEN);
2183 xptedtmatch(struct ccb_dev_match *cdm)
2187 cdm->num_matches = 0;
2190 * Check the bus list generation. If it has changed, the user
2191 * needs to reset everything and start over.
2193 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2194 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2195 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2196 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2200 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2201 && (cdm->pos.cookie.bus != NULL))
2202 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2203 xptedtbusfunc, cdm);
2205 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2208 * If we get back 0, that means that we had to stop before fully
2209 * traversing the EDT. It also means that one of the subroutines
2210 * has set the status field to the proper value. If we get back 1,
2211 * we've fully traversed the EDT and copied out any matching entries.
2214 cdm->status = CAM_DEV_MATCH_LAST;
2220 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2222 struct ccb_dev_match *cdm;
2224 cdm = (struct ccb_dev_match *)arg;
2226 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2227 && (cdm->pos.cookie.pdrv == pdrv)
2228 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2229 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2230 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2231 (*pdrv)->generation)) {
2232 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2236 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2237 && (cdm->pos.cookie.pdrv == pdrv)
2238 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2239 && (cdm->pos.cookie.periph != NULL))
2240 return(xptpdperiphtraverse(pdrv,
2241 (struct cam_periph *)cdm->pos.cookie.periph,
2242 xptplistperiphfunc, arg));
2244 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2248 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2250 struct ccb_dev_match *cdm;
2251 dev_match_ret retval;
2253 cdm = (struct ccb_dev_match *)arg;
2255 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2257 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2258 cdm->status = CAM_DEV_MATCH_ERROR;
2263 * If the copy flag is set, copy this peripheral out.
2265 if (retval & DM_RET_COPY) {
2268 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2269 sizeof(struct dev_match_result));
2272 * If we don't have enough space to put in another
2273 * match result, save our position and tell the
2274 * user there are more devices to check.
2276 if (spaceleft < sizeof(struct dev_match_result)) {
2277 struct periph_driver **pdrv;
2280 bzero(&cdm->pos, sizeof(cdm->pos));
2281 cdm->pos.position_type =
2282 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2286 * This may look a bit non-sensical, but it is
2287 * actually quite logical. There are very few
2288 * peripheral drivers, and bloating every peripheral
2289 * structure with a pointer back to its parent
2290 * peripheral driver linker set entry would cost
2291 * more in the long run than doing this quick lookup.
2294 (struct periph_driver **)periphdriver_set.ls_items;
2295 *pdrv != NULL; pdrv++) {
2296 if (strcmp((*pdrv)->driver_name,
2297 periph->periph_name) == 0)
2302 cdm->status = CAM_DEV_MATCH_ERROR;
2306 cdm->pos.cookie.pdrv = pdrv;
2308 * The periph generation slot does double duty, as
2309 * does the periph pointer slot. They are used for
2310 * both edt and pdrv lookups and positioning.
2312 cdm->pos.cookie.periph = periph;
2313 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2314 (*pdrv)->generation;
2315 cdm->status = CAM_DEV_MATCH_MORE;
2319 j = cdm->num_matches;
2321 cdm->matches[j].type = DEV_MATCH_PERIPH;
2322 cdm->matches[j].result.periph_result.path_id =
2323 periph->path->bus->path_id;
2326 * The transport layer peripheral doesn't have a target or
2329 if (periph->path->target)
2330 cdm->matches[j].result.periph_result.target_id =
2331 periph->path->target->target_id;
2333 cdm->matches[j].result.periph_result.target_id = -1;
2335 if (periph->path->device)
2336 cdm->matches[j].result.periph_result.target_lun =
2337 periph->path->device->lun_id;
2339 cdm->matches[j].result.periph_result.target_lun = -1;
2341 cdm->matches[j].result.periph_result.unit_number =
2342 periph->unit_number;
2343 strncpy(cdm->matches[j].result.periph_result.periph_name,
2344 periph->periph_name, DEV_IDLEN);
2351 xptperiphlistmatch(struct ccb_dev_match *cdm)
2355 cdm->num_matches = 0;
2358 * At this point in the edt traversal function, we check the bus
2359 * list generation to make sure that no busses have been added or
2360 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2361 * For the peripheral driver list traversal function, however, we
2362 * don't have to worry about new peripheral driver types coming or
2363 * going; they're in a linker set, and therefore can't change
2364 * without a recompile.
2367 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2368 && (cdm->pos.cookie.pdrv != NULL))
2369 ret = xptpdrvtraverse(
2370 (struct periph_driver **)cdm->pos.cookie.pdrv,
2371 xptplistpdrvfunc, cdm);
2373 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2376 * If we get back 0, that means that we had to stop before fully
2377 * traversing the peripheral driver tree. It also means that one of
2378 * the subroutines has set the status field to the proper value. If
2379 * we get back 1, we've fully traversed the EDT and copied out any
2383 cdm->status = CAM_DEV_MATCH_LAST;
2389 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2391 struct cam_eb *bus, *next_bus;
2396 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2399 next_bus = TAILQ_NEXT(bus, links);
2401 retval = tr_func(bus, arg);
2410 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2411 xpt_targetfunc_t *tr_func, void *arg)
2413 struct cam_et *target, *next_target;
2417 for (target = (start_target ? start_target :
2418 TAILQ_FIRST(&bus->et_entries));
2419 target != NULL; target = next_target) {
2421 next_target = TAILQ_NEXT(target, links);
2423 retval = tr_func(target, arg);
2433 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2434 xpt_devicefunc_t *tr_func, void *arg)
2436 struct cam_ed *device, *next_device;
2440 for (device = (start_device ? start_device :
2441 TAILQ_FIRST(&target->ed_entries));
2443 device = next_device) {
2445 next_device = TAILQ_NEXT(device, links);
2447 retval = tr_func(device, arg);
2457 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2458 xpt_periphfunc_t *tr_func, void *arg)
2460 struct cam_periph *periph, *next_periph;
2465 for (periph = (start_periph ? start_periph :
2466 SLIST_FIRST(&device->periphs));
2468 periph = next_periph) {
2470 next_periph = SLIST_NEXT(periph, periph_links);
2472 retval = tr_func(periph, arg);
2481 xptpdrvtraverse(struct periph_driver **start_pdrv,
2482 xpt_pdrvfunc_t *tr_func, void *arg)
2484 struct periph_driver **pdrv;
2490 * We don't traverse the peripheral driver list like we do the
2491 * other lists, because it is a linker set, and therefore cannot be
2492 * changed during runtime. If the peripheral driver list is ever
2493 * re-done to be something other than a linker set (i.e. it can
2494 * change while the system is running), the list traversal should
2495 * be modified to work like the other traversal functions.
2497 for (pdrv = (start_pdrv ? start_pdrv :
2498 (struct periph_driver **)periphdriver_set.ls_items);
2499 *pdrv != NULL; pdrv++) {
2500 retval = tr_func(pdrv, arg);
2510 xptpdperiphtraverse(struct periph_driver **pdrv,
2511 struct cam_periph *start_periph,
2512 xpt_periphfunc_t *tr_func, void *arg)
2514 struct cam_periph *periph, *next_periph;
2519 for (periph = (start_periph ? start_periph :
2520 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2521 periph = next_periph) {
2523 next_periph = TAILQ_NEXT(periph, unit_links);
2525 retval = tr_func(periph, arg);
2533 xptdefbusfunc(struct cam_eb *bus, void *arg)
2535 struct xpt_traverse_config *tr_config;
2537 tr_config = (struct xpt_traverse_config *)arg;
2539 if (tr_config->depth == XPT_DEPTH_BUS) {
2540 xpt_busfunc_t *tr_func;
2542 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2544 return(tr_func(bus, tr_config->tr_arg));
2546 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2550 xptdeftargetfunc(struct cam_et *target, void *arg)
2552 struct xpt_traverse_config *tr_config;
2554 tr_config = (struct xpt_traverse_config *)arg;
2556 if (tr_config->depth == XPT_DEPTH_TARGET) {
2557 xpt_targetfunc_t *tr_func;
2559 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2561 return(tr_func(target, tr_config->tr_arg));
2563 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2567 xptdefdevicefunc(struct cam_ed *device, void *arg)
2569 struct xpt_traverse_config *tr_config;
2571 tr_config = (struct xpt_traverse_config *)arg;
2573 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2574 xpt_devicefunc_t *tr_func;
2576 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2578 return(tr_func(device, tr_config->tr_arg));
2580 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2584 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2586 struct xpt_traverse_config *tr_config;
2587 xpt_periphfunc_t *tr_func;
2589 tr_config = (struct xpt_traverse_config *)arg;
2591 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2594 * Unlike the other default functions, we don't check for depth
2595 * here. The peripheral driver level is the last level in the EDT,
2596 * so if we're here, we should execute the function in question.
2598 return(tr_func(periph, tr_config->tr_arg));
2602 * Execute the given function for every bus in the EDT.
2605 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2607 struct xpt_traverse_config tr_config;
2609 tr_config.depth = XPT_DEPTH_BUS;
2610 tr_config.tr_func = tr_func;
2611 tr_config.tr_arg = arg;
2613 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2618 * Execute the given function for every target in the EDT.
2621 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2623 struct xpt_traverse_config tr_config;
2625 tr_config.depth = XPT_DEPTH_TARGET;
2626 tr_config.tr_func = tr_func;
2627 tr_config.tr_arg = arg;
2629 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2631 #endif /* notusedyet */
2634 * Execute the given function for every device in the EDT.
2637 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2639 struct xpt_traverse_config tr_config;
2641 tr_config.depth = XPT_DEPTH_DEVICE;
2642 tr_config.tr_func = tr_func;
2643 tr_config.tr_arg = arg;
2645 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2650 * Execute the given function for every peripheral in the EDT.
2653 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2655 struct xpt_traverse_config tr_config;
2657 tr_config.depth = XPT_DEPTH_PERIPH;
2658 tr_config.tr_func = tr_func;
2659 tr_config.tr_arg = arg;
2661 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2663 #endif /* notusedyet */
2666 xptsetasyncfunc(struct cam_ed *device, void *arg)
2668 struct cam_path path;
2669 struct ccb_getdev cgd;
2670 struct async_node *cur_entry;
2672 cur_entry = (struct async_node *)arg;
2675 * Don't report unconfigured devices (Wildcard devs,
2676 * devices only for target mode, device instances
2677 * that have been invalidated but are waiting for
2678 * their last reference count to be released).
2680 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2683 xpt_compile_path(&path,
2685 device->target->bus->path_id,
2686 device->target->target_id,
2688 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2689 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2690 xpt_action((union ccb *)&cgd);
2691 cur_entry->callback(cur_entry->callback_arg,
2694 xpt_release_path(&path);
2700 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2702 struct cam_path path;
2703 struct ccb_pathinq cpi;
2704 struct async_node *cur_entry;
2706 cur_entry = (struct async_node *)arg;
2708 xpt_compile_path(&path, /*periph*/NULL,
2710 CAM_TARGET_WILDCARD,
2712 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2713 cpi.ccb_h.func_code = XPT_PATH_INQ;
2714 xpt_action((union ccb *)&cpi);
2715 cur_entry->callback(cur_entry->callback_arg,
2718 xpt_release_path(&path);
2724 xpt_action(union ccb *start_ccb)
2728 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2730 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2732 iopl = splsoftcam();
2733 switch (start_ccb->ccb_h.func_code) {
2737 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2738 struct cam_path *path;
2740 path = start_ccb->ccb_h.path;
2744 * For the sake of compatibility with SCSI-1
2745 * devices that may not understand the identify
2746 * message, we include lun information in the
2747 * second byte of all commands. SCSI-1 specifies
2748 * that luns are a 3 bit value and reserves only 3
2749 * bits for lun information in the CDB. Later
2750 * revisions of the SCSI spec allow for more than 8
2751 * luns, but have deprecated lun information in the
2752 * CDB. So, if the lun won't fit, we must omit.
2754 * Also be aware that during initial probing for devices,
2755 * the inquiry information is unknown but initialized to 0.
2756 * This means that this code will be exercised while probing
2757 * devices with an ANSI revision greater than 2.
2759 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2760 && start_ccb->ccb_h.target_lun < 8
2761 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2763 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2764 start_ccb->ccb_h.target_lun << 5;
2766 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2767 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2768 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2769 &path->device->inq_data),
2770 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2771 cdb_str, sizeof(cdb_str))));
2775 case XPT_CONT_TARGET_IO:
2776 start_ccb->csio.sense_resid = 0;
2777 start_ccb->csio.resid = 0;
2782 struct cam_path *path;
2786 path = start_ccb->ccb_h.path;
2789 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2790 if (path->device->qfrozen_cnt == 0)
2791 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2796 xpt_run_dev_sendq(path->bus);
2799 case XPT_SET_TRAN_SETTINGS:
2801 xpt_set_transfer_settings(&start_ccb->cts,
2802 start_ccb->ccb_h.path->device,
2803 /*async_update*/FALSE);
2806 case XPT_CALC_GEOMETRY:
2808 struct cam_sim *sim;
2810 /* Filter out garbage */
2811 if (start_ccb->ccg.block_size == 0
2812 || start_ccb->ccg.volume_size == 0) {
2813 start_ccb->ccg.cylinders = 0;
2814 start_ccb->ccg.heads = 0;
2815 start_ccb->ccg.secs_per_track = 0;
2816 start_ccb->ccb_h.status = CAM_REQ_CMP;
2821 * In a PC-98 system, geometry translation depens on
2822 * the "real" device geometry obtained from mode page 4.
2823 * SCSI geometry translation is performed in the
2824 * initialization routine of the SCSI BIOS and the result
2825 * stored in host memory. If the translation is available
2826 * in host memory, use it. If not, rely on the default
2827 * translation the device driver performs.
2829 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2830 start_ccb->ccb_h.status = CAM_REQ_CMP;
2834 sim = start_ccb->ccb_h.path->bus->sim;
2835 (*(sim->sim_action))(sim, start_ccb);
2840 union ccb* abort_ccb;
2843 abort_ccb = start_ccb->cab.abort_ccb;
2844 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2846 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2847 struct cam_ccbq *ccbq;
2849 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2850 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2851 abort_ccb->ccb_h.status =
2852 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2853 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2855 xpt_done(abort_ccb);
2857 start_ccb->ccb_h.status = CAM_REQ_CMP;
2860 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2861 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2863 * We've caught this ccb en route to
2864 * the SIM. Flag it for abort and the
2865 * SIM will do so just before starting
2866 * real work on the CCB.
2868 abort_ccb->ccb_h.status =
2869 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2870 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2871 start_ccb->ccb_h.status = CAM_REQ_CMP;
2875 if (XPT_FC_IS_QUEUED(abort_ccb)
2876 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2878 * It's already completed but waiting
2879 * for our SWI to get to it.
2881 start_ccb->ccb_h.status = CAM_UA_ABORT;
2885 * If we weren't able to take care of the abort request
2886 * in the XPT, pass the request down to the SIM for processing.
2890 case XPT_ACCEPT_TARGET_IO:
2892 case XPT_IMMED_NOTIFY:
2893 case XPT_NOTIFY_ACK:
2894 case XPT_GET_TRAN_SETTINGS:
2897 struct cam_sim *sim;
2899 sim = start_ccb->ccb_h.path->bus->sim;
2900 (*(sim->sim_action))(sim, start_ccb);
2905 struct cam_sim *sim;
2907 sim = start_ccb->ccb_h.path->bus->sim;
2908 (*(sim->sim_action))(sim, start_ccb);
2911 case XPT_PATH_STATS:
2912 start_ccb->cpis.last_reset =
2913 start_ccb->ccb_h.path->bus->last_reset;
2914 start_ccb->ccb_h.status = CAM_REQ_CMP;
2921 dev = start_ccb->ccb_h.path->device;
2923 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2924 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2926 struct ccb_getdev *cgd;
2930 cgd = &start_ccb->cgd;
2931 bus = cgd->ccb_h.path->bus;
2932 tar = cgd->ccb_h.path->target;
2933 cgd->inq_data = dev->inq_data;
2934 cgd->ccb_h.status = CAM_REQ_CMP;
2935 cgd->serial_num_len = dev->serial_num_len;
2936 if ((dev->serial_num_len > 0)
2937 && (dev->serial_num != NULL))
2938 bcopy(dev->serial_num, cgd->serial_num,
2939 dev->serial_num_len);
2944 case XPT_GDEV_STATS:
2949 dev = start_ccb->ccb_h.path->device;
2951 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2952 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2954 struct ccb_getdevstats *cgds;
2958 cgds = &start_ccb->cgds;
2959 bus = cgds->ccb_h.path->bus;
2960 tar = cgds->ccb_h.path->target;
2961 cgds->dev_openings = dev->ccbq.dev_openings;
2962 cgds->dev_active = dev->ccbq.dev_active;
2963 cgds->devq_openings = dev->ccbq.devq_openings;
2964 cgds->devq_queued = dev->ccbq.queue.entries;
2965 cgds->held = dev->ccbq.held;
2966 cgds->last_reset = tar->last_reset;
2967 cgds->maxtags = dev->quirk->maxtags;
2968 cgds->mintags = dev->quirk->mintags;
2969 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2970 cgds->last_reset = bus->last_reset;
2971 cgds->ccb_h.status = CAM_REQ_CMP;
2978 struct cam_periph *nperiph;
2979 struct periph_list *periph_head;
2980 struct ccb_getdevlist *cgdl;
2983 struct cam_ed *device;
2990 * Don't want anyone mucking with our data.
2993 device = start_ccb->ccb_h.path->device;
2994 periph_head = &device->periphs;
2995 cgdl = &start_ccb->cgdl;
2998 * Check and see if the list has changed since the user
2999 * last requested a list member. If so, tell them that the
3000 * list has changed, and therefore they need to start over
3001 * from the beginning.
3003 if ((cgdl->index != 0) &&
3004 (cgdl->generation != device->generation)) {
3005 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3011 * Traverse the list of peripherals and attempt to find
3012 * the requested peripheral.
3014 for (nperiph = periph_head->slh_first, i = 0;
3015 (nperiph != NULL) && (i <= cgdl->index);
3016 nperiph = nperiph->periph_links.sle_next, i++) {
3017 if (i == cgdl->index) {
3018 strncpy(cgdl->periph_name,
3019 nperiph->periph_name,
3021 cgdl->unit_number = nperiph->unit_number;
3026 cgdl->status = CAM_GDEVLIST_ERROR;
3031 if (nperiph == NULL)
3032 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3034 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3037 cgdl->generation = device->generation;
3040 cgdl->ccb_h.status = CAM_REQ_CMP;
3046 dev_pos_type position_type;
3047 struct ccb_dev_match *cdm;
3050 cdm = &start_ccb->cdm;
3053 * Prevent EDT changes while we traverse it.
3057 * There are two ways of getting at information in the EDT.
3058 * The first way is via the primary EDT tree. It starts
3059 * with a list of busses, then a list of targets on a bus,
3060 * then devices/luns on a target, and then peripherals on a
3061 * device/lun. The "other" way is by the peripheral driver
3062 * lists. The peripheral driver lists are organized by
3063 * peripheral driver. (obviously) So it makes sense to
3064 * use the peripheral driver list if the user is looking
3065 * for something like "da1", or all "da" devices. If the
3066 * user is looking for something on a particular bus/target
3067 * or lun, it's generally better to go through the EDT tree.
3070 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3071 position_type = cdm->pos.position_type;
3075 position_type = CAM_DEV_POS_NONE;
3077 for (i = 0; i < cdm->num_patterns; i++) {
3078 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3079 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3080 position_type = CAM_DEV_POS_EDT;
3085 if (cdm->num_patterns == 0)
3086 position_type = CAM_DEV_POS_EDT;
3087 else if (position_type == CAM_DEV_POS_NONE)
3088 position_type = CAM_DEV_POS_PDRV;
3091 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3092 case CAM_DEV_POS_EDT:
3093 ret = xptedtmatch(cdm);
3095 case CAM_DEV_POS_PDRV:
3096 ret = xptperiphlistmatch(cdm);
3099 cdm->status = CAM_DEV_MATCH_ERROR;
3105 if (cdm->status == CAM_DEV_MATCH_ERROR)
3106 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3108 start_ccb->ccb_h.status = CAM_REQ_CMP;
3114 struct ccb_setasync *csa;
3115 struct async_node *cur_entry;
3116 struct async_list *async_head;
3120 csa = &start_ccb->csa;
3121 added = csa->event_enable;
3122 async_head = &csa->ccb_h.path->device->asyncs;
3125 * If there is already an entry for us, simply
3129 cur_entry = SLIST_FIRST(async_head);
3130 while (cur_entry != NULL) {
3131 if ((cur_entry->callback_arg == csa->callback_arg)
3132 && (cur_entry->callback == csa->callback))
3134 cur_entry = SLIST_NEXT(cur_entry, links);
3137 if (cur_entry != NULL) {
3139 * If the request has no flags set,
3142 added &= ~cur_entry->event_enable;
3143 if (csa->event_enable == 0) {
3144 SLIST_REMOVE(async_head, cur_entry,
3146 csa->ccb_h.path->device->refcount--;
3147 free(cur_entry, M_DEVBUF);
3149 cur_entry->event_enable = csa->event_enable;
3152 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3154 if (cur_entry == NULL) {
3156 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3159 cur_entry->event_enable = csa->event_enable;
3160 cur_entry->callback_arg = csa->callback_arg;
3161 cur_entry->callback = csa->callback;
3162 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3163 csa->ccb_h.path->device->refcount++;
3166 if ((added & AC_FOUND_DEVICE) != 0) {
3168 * Get this peripheral up to date with all
3169 * the currently existing devices.
3171 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3173 if ((added & AC_PATH_REGISTERED) != 0) {
3175 * Get this peripheral up to date with all
3176 * the currently existing busses.
3178 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3181 start_ccb->ccb_h.status = CAM_REQ_CMP;
3186 struct ccb_relsim *crs;
3190 crs = &start_ccb->crs;
3191 dev = crs->ccb_h.path->device;
3194 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3200 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3202 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3204 /* Don't ever go below one opening */
3205 if (crs->openings > 0) {
3206 xpt_dev_ccbq_resize(crs->ccb_h.path,
3210 xpt_print_path(crs->ccb_h.path);
3211 printf("tagged openings "
3219 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3221 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3224 * Just extend the old timeout and decrement
3225 * the freeze count so that a single timeout
3226 * is sufficient for releasing the queue.
3228 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3229 untimeout(xpt_release_devq_timeout,
3230 dev, dev->c_handle);
3233 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3237 timeout(xpt_release_devq_timeout,
3239 (crs->release_timeout * hz) / 1000);
3241 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3245 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3247 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3249 * Decrement the freeze count so that a single
3250 * completion is still sufficient to unfreeze
3253 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3256 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3257 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3261 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3263 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3264 || (dev->ccbq.dev_active == 0)) {
3266 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3269 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3270 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3275 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3277 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3280 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3281 start_ccb->ccb_h.status = CAM_REQ_CMP;
3285 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3288 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3289 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3297 #ifdef CAM_DEBUG_DELAY
3298 cam_debug_delay = CAM_DEBUG_DELAY;
3300 cam_dflags = start_ccb->cdbg.flags;
3301 if (cam_dpath != NULL) {
3302 xpt_free_path(cam_dpath);
3306 if (cam_dflags != CAM_DEBUG_NONE) {
3307 if (xpt_create_path(&cam_dpath, xpt_periph,
3308 start_ccb->ccb_h.path_id,
3309 start_ccb->ccb_h.target_id,
3310 start_ccb->ccb_h.target_lun) !=
3312 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3313 cam_dflags = CAM_DEBUG_NONE;
3315 start_ccb->ccb_h.status = CAM_REQ_CMP;
3316 xpt_print_path(cam_dpath);
3317 printf("debugging flags now %x\n", cam_dflags);
3321 start_ccb->ccb_h.status = CAM_REQ_CMP;
3324 #else /* !CAMDEBUG */
3325 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3326 #endif /* CAMDEBUG */
3330 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3331 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3332 start_ccb->ccb_h.status = CAM_REQ_CMP;
3339 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3346 xpt_polled_action(union ccb *start_ccb)
3350 struct cam_sim *sim;
3351 struct cam_devq *devq;
3354 timeout = start_ccb->ccb_h.timeout;
3355 sim = start_ccb->ccb_h.path->bus->sim;
3357 dev = start_ccb->ccb_h.path->device;
3362 * Steal an opening so that no other queued requests
3363 * can get it before us while we simulate interrupts.
3365 dev->ccbq.devq_openings--;
3366 dev->ccbq.dev_openings--;
3368 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3369 && (--timeout > 0)) {
3371 (*(sim->sim_poll))(sim);
3376 dev->ccbq.devq_openings++;
3377 dev->ccbq.dev_openings++;
3380 xpt_action(start_ccb);
3381 while(--timeout > 0) {
3382 (*(sim->sim_poll))(sim);
3385 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3392 * XXX Is it worth adding a sim_timeout entry
3393 * point so we can attempt recovery? If
3394 * this is only used for dumps, I don't think
3397 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3400 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3406 * Schedule a peripheral driver to receive a ccb when it's
3407 * target device has space for more transactions.
3410 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3412 struct cam_ed *device;
3416 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3417 device = perph->path->device;
3419 if (periph_is_queued(perph)) {
3420 /* Simply reorder based on new priority */
3421 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3422 (" change priority to %d\n", new_priority));
3423 if (new_priority < perph->pinfo.priority) {
3424 camq_change_priority(&device->drvq,
3430 /* New entry on the queue */
3431 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3432 (" added periph to queue\n"));
3433 perph->pinfo.priority = new_priority;
3434 perph->pinfo.generation = ++device->drvq.generation;
3435 camq_insert(&device->drvq, &perph->pinfo);
3436 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3440 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3441 (" calling xpt_run_devq\n"));
3442 xpt_run_dev_allocq(perph->path->bus);
3448 * Schedule a device to run on a given queue.
3449 * If the device was inserted as a new entry on the queue,
3450 * return 1 meaning the device queue should be run. If we
3451 * were already queued, implying someone else has already
3452 * started the queue, return 0 so the caller doesn't attempt
3453 * to run the queue. Must be run at either splsoftcam
3454 * (or splcam since that encompases splsoftcam).
3457 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3458 u_int32_t new_priority)
3461 u_int32_t old_priority;
3463 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3465 old_priority = pinfo->priority;
3468 * Are we already queued?
3470 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3471 /* Simply reorder based on new priority */
3472 if (new_priority < old_priority) {
3473 camq_change_priority(queue, pinfo->index,
3475 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3476 ("changed priority to %d\n",
3481 /* New entry on the queue */
3482 if (new_priority < old_priority)
3483 pinfo->priority = new_priority;
3485 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3486 ("Inserting onto queue\n"));
3487 pinfo->generation = ++queue->generation;
3488 camq_insert(queue, pinfo);
3495 xpt_run_dev_allocq(struct cam_eb *bus)
3497 struct cam_devq *devq;
3500 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3501 devq = bus->sim->devq;
3503 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3504 (" qfrozen_cnt == 0x%x, entries == %d, "
3505 "openings == %d, active == %d\n",
3506 devq->alloc_queue.qfrozen_cnt,
3507 devq->alloc_queue.entries,
3508 devq->alloc_openings,
3509 devq->alloc_active));
3512 devq->alloc_queue.qfrozen_cnt++;
3513 while ((devq->alloc_queue.entries > 0)
3514 && (devq->alloc_openings > 0)
3515 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3516 struct cam_ed_qinfo *qinfo;
3517 struct cam_ed *device;
3518 union ccb *work_ccb;
3519 struct cam_periph *drv;
3522 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3524 device = qinfo->device;
3526 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3527 ("running device %p\n", device));
3529 drvq = &device->drvq;
3532 if (drvq->entries <= 0) {
3533 panic("xpt_run_dev_allocq: "
3534 "Device on queue without any work to do");
3537 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3538 devq->alloc_openings--;
3539 devq->alloc_active++;
3540 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3542 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3543 drv->pinfo.priority);
3544 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3545 ("calling periph start\n"));
3546 drv->periph_start(drv, work_ccb);
3549 * Malloc failure in alloc_ccb
3552 * XXX add us to a list to be run from free_ccb
3553 * if we don't have any ccbs active on this
3554 * device queue otherwise we may never get run
3560 /* Raise IPL for possible insertion and test at top of loop */
3563 if (drvq->entries > 0) {
3564 /* We have more work. Attempt to reschedule */
3565 xpt_schedule_dev_allocq(bus, device);
3568 devq->alloc_queue.qfrozen_cnt--;
3573 xpt_run_dev_sendq(struct cam_eb *bus)
3575 struct cam_devq *devq;
3578 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3580 devq = bus->sim->devq;
3583 devq->send_queue.qfrozen_cnt++;
3586 while ((devq->send_queue.entries > 0)
3587 && (devq->send_openings > 0)) {
3588 struct cam_ed_qinfo *qinfo;
3589 struct cam_ed *device;
3590 union ccb *work_ccb;
3591 struct cam_sim *sim;
3595 if (devq->send_queue.qfrozen_cnt > 1) {
3600 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3602 device = qinfo->device;
3605 * If the device has been "frozen", don't attempt
3608 if (device->qfrozen_cnt > 0) {
3613 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3614 ("running device %p\n", device));
3616 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3617 if (work_ccb == NULL) {
3618 printf("device on run queue with no ccbs???");
3623 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3625 if (num_highpower <= 0) {
3627 * We got a high power command, but we
3628 * don't have any available slots. Freeze
3629 * the device queue until we have a slot
3632 device->qfrozen_cnt++;
3633 STAILQ_INSERT_TAIL(&highpowerq,
3641 * Consume a high power slot while
3647 devq->active_dev = device;
3648 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3650 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3653 devq->send_openings--;
3654 devq->send_active++;
3656 if (device->ccbq.queue.entries > 0)
3657 xpt_schedule_dev_sendq(bus, device);
3659 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3661 * The client wants to freeze the queue
3662 * after this CCB is sent.
3665 device->qfrozen_cnt++;
3671 if ((device->inq_flags & SID_CmdQue) != 0)
3672 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3675 * Clear this in case of a retried CCB that failed
3676 * due to a rejected tag.
3678 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3681 * Device queues can be shared among multiple sim instances
3682 * that reside on different busses. Use the SIM in the queue
3683 * CCB's path, rather than the one in the bus that was passed
3684 * into this function.
3686 sim = work_ccb->ccb_h.path->bus->sim;
3687 (*(sim->sim_action))(sim, work_ccb);
3690 devq->active_dev = NULL;
3692 /* Raise IPL for possible insertion and test at top of loop */
3697 devq->send_queue.qfrozen_cnt--;
3702 * This function merges stuff from the slave ccb into the master ccb, while
3703 * keeping important fields in the master ccb constant.
3706 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3709 * Pull fields that are valid for peripheral drivers to set
3710 * into the master CCB along with the CCB "payload".
3712 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3713 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3714 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3715 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3716 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3717 sizeof(union ccb) - sizeof(struct ccb_hdr));
3721 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3723 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3724 ccb_h->pinfo.priority = priority;
3726 ccb_h->path_id = path->bus->path_id;
3728 ccb_h->target_id = path->target->target_id;
3730 ccb_h->target_id = CAM_TARGET_WILDCARD;
3732 ccb_h->target_lun = path->device->lun_id;
3733 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3735 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3737 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3741 /* Path manipulation functions */
3743 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3744 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3746 struct cam_path *path;
3749 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3752 status = CAM_RESRC_UNAVAIL;
3755 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3756 if (status != CAM_REQ_CMP) {
3757 free(path, M_DEVBUF);
3760 *new_path_ptr = path;
3765 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3766 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3769 struct cam_et *target;
3770 struct cam_ed *device;
3774 status = CAM_REQ_CMP; /* Completed without error */
3775 target = NULL; /* Wildcarded */
3776 device = NULL; /* Wildcarded */
3779 * We will potentially modify the EDT, so block interrupts
3780 * that may attempt to create cam paths.
3783 bus = xpt_find_bus(path_id);
3785 status = CAM_PATH_INVALID;
3787 target = xpt_find_target(bus, target_id);
3788 if (target == NULL) {
3790 struct cam_et *new_target;
3792 new_target = xpt_alloc_target(bus, target_id);
3793 if (new_target == NULL) {
3794 status = CAM_RESRC_UNAVAIL;
3796 target = new_target;
3799 if (target != NULL) {
3800 device = xpt_find_device(target, lun_id);
3801 if (device == NULL) {
3803 struct cam_ed *new_device;
3805 new_device = xpt_alloc_device(bus,
3808 if (new_device == NULL) {
3809 status = CAM_RESRC_UNAVAIL;
3811 device = new_device;
3819 * Only touch the user's data if we are successful.
3821 if (status == CAM_REQ_CMP) {
3822 new_path->periph = perph;
3823 new_path->bus = bus;
3824 new_path->target = target;
3825 new_path->device = device;
3826 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3829 xpt_release_device(bus, target, device);
3831 xpt_release_target(bus, target);
3833 xpt_release_bus(bus);
3839 xpt_release_path(struct cam_path *path)
3841 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3842 if (path->device != NULL) {
3843 xpt_release_device(path->bus, path->target, path->device);
3844 path->device = NULL;
3846 if (path->target != NULL) {
3847 xpt_release_target(path->bus, path->target);
3848 path->target = NULL;
3850 if (path->bus != NULL) {
3851 xpt_release_bus(path->bus);
3857 xpt_free_path(struct cam_path *path)
3859 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3860 xpt_release_path(path);
3861 free(path, M_DEVBUF);
3866 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3867 * in path1, 2 for match with wildcards in path2.
3870 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3874 if (path1->bus != path2->bus) {
3875 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3877 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3882 if (path1->target != path2->target) {
3883 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3886 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3891 if (path1->device != path2->device) {
3892 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3895 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3904 xpt_print_path(struct cam_path *path)
3907 printf("(nopath): ");
3909 if (path->periph != NULL)
3910 printf("(%s%d:", path->periph->periph_name,
3911 path->periph->unit_number);
3913 printf("(noperiph:");
3915 if (path->bus != NULL)
3916 printf("%s%d:%d:", path->bus->sim->sim_name,
3917 path->bus->sim->unit_number,
3918 path->bus->sim->bus_id);
3922 if (path->target != NULL)
3923 printf("%d:", path->target->target_id);
3927 if (path->device != NULL)
3928 printf("%d): ", path->device->lun_id);
3935 xpt_path_path_id(struct cam_path *path)
3937 return(path->bus->path_id);
3941 xpt_path_target_id(struct cam_path *path)
3943 if (path->target != NULL)
3944 return (path->target->target_id);
3946 return (CAM_TARGET_WILDCARD);
3950 xpt_path_lun_id(struct cam_path *path)
3952 if (path->device != NULL)
3953 return (path->device->lun_id);
3955 return (CAM_LUN_WILDCARD);
3959 xpt_path_sim(struct cam_path *path)
3961 return (path->bus->sim);
3965 xpt_path_periph(struct cam_path *path)
3967 return (path->periph);
3971 * Release a CAM control block for the caller. Remit the cost of the structure
3972 * to the device referenced by the path. If the this device had no 'credits'
3973 * and peripheral drivers have registered async callbacks for this notification
3977 xpt_release_ccb(union ccb *free_ccb)
3980 struct cam_path *path;
3981 struct cam_ed *device;
3984 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3985 path = free_ccb->ccb_h.path;
3986 device = path->device;
3989 cam_ccbq_release_opening(&device->ccbq);
3990 if (xpt_ccb_count > xpt_max_ccbs) {
3991 xpt_free_ccb(free_ccb);
3994 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
3996 bus->sim->devq->alloc_openings++;
3997 bus->sim->devq->alloc_active--;
3998 /* XXX Turn this into an inline function - xpt_run_device?? */
3999 if ((device_is_alloc_queued(device) == 0)
4000 && (device->drvq.entries > 0)) {
4001 xpt_schedule_dev_allocq(bus, device);
4004 if (dev_allocq_is_runnable(bus->sim->devq))
4005 xpt_run_dev_allocq(bus);
4008 /* Functions accessed by SIM drivers */
4011 * A sim structure, listing the SIM entry points and instance
4012 * identification info is passed to xpt_bus_register to hook the SIM
4013 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4014 * for this new bus and places it in the array of busses and assigns
4015 * it a path_id. The path_id may be influenced by "hard wiring"
4016 * information specified by the user. Once interrupt services are
4017 * availible, the bus will be probed.
4020 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4022 struct cam_eb *new_bus;
4023 struct cam_eb *old_bus;
4024 struct ccb_pathinq cpi;
4028 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4029 M_DEVBUF, M_NOWAIT);
4030 if (new_bus == NULL) {
4031 /* Couldn't satisfy request */
4032 return (CAM_RESRC_UNAVAIL);
4035 if (strcmp(sim->sim_name, "xpt") != 0) {
4038 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4041 TAILQ_INIT(&new_bus->et_entries);
4042 new_bus->path_id = sim->path_id;
4044 timevalclear(&new_bus->last_reset);
4046 new_bus->refcount = 1; /* Held until a bus_deregister event */
4047 new_bus->generation = 0;
4049 old_bus = TAILQ_FIRST(&xpt_busses);
4050 while (old_bus != NULL
4051 && old_bus->path_id < new_bus->path_id)
4052 old_bus = TAILQ_NEXT(old_bus, links);
4053 if (old_bus != NULL)
4054 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4056 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4060 /* Notify interested parties */
4061 if (sim->path_id != CAM_XPT_PATH_ID) {
4062 struct cam_path path;
4064 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4065 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4066 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4067 cpi.ccb_h.func_code = XPT_PATH_INQ;
4068 xpt_action((union ccb *)&cpi);
4069 xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi);
4070 xpt_release_path(&path);
4072 return (CAM_SUCCESS);
4076 xpt_bus_deregister(path_id_t pathid)
4078 struct cam_path bus_path;
4081 status = xpt_compile_path(&bus_path, NULL, pathid,
4082 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4083 if (status != CAM_REQ_CMP)
4086 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4087 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4089 /* Release the reference count held while registered. */
4090 xpt_release_bus(bus_path.bus);
4091 xpt_release_path(&bus_path);
4093 return (CAM_REQ_CMP);
4097 xptnextfreepathid(void)
4104 bus = TAILQ_FIRST(&xpt_busses);
4106 /* Find an unoccupied pathid */
4108 && bus->path_id <= pathid) {
4109 if (bus->path_id == pathid)
4111 bus = TAILQ_NEXT(bus, links);
4115 * Ensure that this pathid is not reserved for
4116 * a bus that may be registered in the future.
4118 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4120 /* Start the search over */
4127 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4131 char buf[32], *strval;
4133 pathid = CAM_XPT_PATH_ID;
4134 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4136 while ((i = resource_locate(i, "scbus")) != -1) {
4137 dunit = resource_query_unit(i);
4138 if (dunit < 0) /* unwired?! */
4140 if (resource_string_value("scbus", dunit, "at", &strval) != 0)
4142 if (strcmp(buf, strval) != 0)
4144 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4145 if (sim_bus == val) {
4149 } else if (sim_bus == 0) {
4150 /* Unspecified matches bus 0 */
4154 printf("Ambiguous scbus configuration for %s%d "
4155 "bus %d, cannot wire down. The kernel "
4156 "config entry for scbus%d should "
4157 "specify a controller bus.\n"
4158 "Scbus will be assigned dynamically.\n",
4159 sim_name, sim_unit, sim_bus, dunit);
4164 if (pathid == CAM_XPT_PATH_ID)
4165 pathid = xptnextfreepathid();
4170 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4173 struct cam_et *target, *next_target;
4174 struct cam_ed *device, *next_device;
4177 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4180 * Most async events come from a CAM interrupt context. In
4181 * a few cases, the error recovery code at the peripheral layer,
4182 * which may run from our SWI or a process context, may signal
4183 * deferred events with a call to xpt_async. Ensure async
4184 * notifications are serialized by blocking cam interrupts.
4190 if (async_code == AC_BUS_RESET) {
4194 /* Update our notion of when the last reset occurred */
4195 microtime(&bus->last_reset);
4199 for (target = TAILQ_FIRST(&bus->et_entries);
4201 target = next_target) {
4203 next_target = TAILQ_NEXT(target, links);
4205 if (path->target != target
4206 && path->target->target_id != CAM_TARGET_WILDCARD)
4209 if (async_code == AC_SENT_BDR) {
4212 /* Update our notion of when the last reset occurred */
4214 microtime(&path->target->last_reset);
4218 for (device = TAILQ_FIRST(&target->ed_entries);
4220 device = next_device) {
4222 struct cam_path newpath;
4224 next_device = TAILQ_NEXT(device, links);
4226 if (path->device != device
4227 && path->device->lun_id != CAM_LUN_WILDCARD)
4231 * We need our own path with wildcards expanded to
4232 * handle certain types of events.
4234 if ((async_code == AC_SENT_BDR)
4235 || (async_code == AC_BUS_RESET)
4236 || (async_code == AC_INQ_CHANGED))
4237 status = xpt_compile_path(&newpath, NULL,
4242 status = CAM_REQ_CMP_ERR;
4244 if (status == CAM_REQ_CMP) {
4247 * Allow transfer negotiation to occur in a
4248 * tag free environment.
4250 if (async_code == AC_SENT_BDR
4251 || async_code == AC_BUS_RESET)
4252 xpt_toggle_tags(&newpath);
4254 if (async_code == AC_INQ_CHANGED) {
4256 * We've sent a start unit command, or
4257 * something similar to a device that
4258 * may have caused its inquiry data to
4259 * change. So we re-scan the device to
4260 * refresh the inquiry data for it.
4262 xpt_scan_lun(newpath.periph, &newpath,
4263 CAM_EXPECT_INQ_CHANGE,
4266 xpt_release_path(&newpath);
4267 } else if (async_code == AC_LOST_DEVICE) {
4268 device->flags |= CAM_DEV_UNCONFIGURED;
4269 } else if (async_code == AC_TRANSFER_NEG) {
4270 struct ccb_trans_settings *settings;
4273 (struct ccb_trans_settings *)async_arg;
4274 xpt_set_transfer_settings(settings, device,
4275 /*async_update*/TRUE);
4278 xpt_async_bcast(&device->asyncs,
4286 * If this wasn't a fully wildcarded async, tell all
4287 * clients that want all async events.
4289 if (bus != xpt_periph->path->bus)
4290 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4296 xpt_async_bcast(struct async_list *async_head,
4297 u_int32_t async_code,
4298 struct cam_path *path, void *async_arg)
4300 struct async_node *cur_entry;
4302 cur_entry = SLIST_FIRST(async_head);
4303 while (cur_entry != NULL) {
4304 struct async_node *next_entry;
4306 * Grab the next list entry before we call the current
4307 * entry's callback. This is because the callback function
4308 * can delete its async callback entry.
4310 next_entry = SLIST_NEXT(cur_entry, links);
4311 if ((cur_entry->event_enable & async_code) != 0)
4312 cur_entry->callback(cur_entry->callback_arg,
4315 cur_entry = next_entry;
4320 xpt_freeze_devq(struct cam_path *path, u_int count)
4323 struct ccb_hdr *ccbh;
4326 path->device->qfrozen_cnt += count;
4329 * Mark the last CCB in the queue as needing
4330 * to be requeued if the driver hasn't
4331 * changed it's state yet. This fixes a race
4332 * where a ccb is just about to be queued to
4333 * a controller driver when it's interrupt routine
4334 * freezes the queue. To completly close the
4335 * hole, controller drives must check to see
4336 * if a ccb's status is still CAM_REQ_INPROG
4337 * under spl protection just before they queue
4338 * the CCB. See ahc_action/ahc_freeze_devq for
4341 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4342 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4343 ccbh->status = CAM_REQUEUE_REQ;
4345 return (path->device->qfrozen_cnt);
4349 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4351 sim->devq->send_queue.qfrozen_cnt += count;
4352 if (sim->devq->active_dev != NULL) {
4353 struct ccb_hdr *ccbh;
4355 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4357 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4358 ccbh->status = CAM_REQUEUE_REQ;
4360 return (sim->devq->send_queue.qfrozen_cnt);
4364 xpt_release_devq_timeout(void *arg)
4366 struct cam_ed *device;
4368 device = (struct cam_ed *)arg;
4370 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4374 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4376 xpt_release_devq_device(path->device, count, run_queue);
4380 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4388 if (dev->qfrozen_cnt > 0) {
4390 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4391 dev->qfrozen_cnt -= count;
4392 if (dev->qfrozen_cnt == 0) {
4395 * No longer need to wait for a successful
4396 * command completion.
4398 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4401 * Remove any timeouts that might be scheduled
4402 * to release this queue.
4404 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4405 untimeout(xpt_release_devq_timeout, dev,
4407 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4411 * Now that we are unfrozen schedule the
4412 * device so any pending transactions are
4415 if ((dev->ccbq.queue.entries > 0)
4416 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4417 && (run_queue != 0)) {
4424 xpt_run_dev_sendq(dev->target->bus);
4429 xpt_release_simq(struct cam_sim *sim, int run_queue)
4434 sendq = &(sim->devq->send_queue);
4436 if (sendq->qfrozen_cnt > 0) {
4438 sendq->qfrozen_cnt--;
4439 if (sendq->qfrozen_cnt == 0) {
4443 * If there is a timeout scheduled to release this
4444 * sim queue, remove it. The queue frozen count is
4447 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4448 untimeout(xpt_release_simq_timeout, sim,
4450 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4452 bus = xpt_find_bus(sim->path_id);
4457 * Now that we are unfrozen run the send queue.
4459 xpt_run_dev_sendq(bus);
4461 xpt_release_bus(bus);
4469 xpt_release_simq_timeout(void *arg)
4471 struct cam_sim *sim;
4473 sim = (struct cam_sim *)arg;
4474 xpt_release_simq(sim, /* run_queue */ TRUE);
4478 xpt_done(union ccb *done_ccb)
4484 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4485 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4487 * Queue up the request for handling by our SWI handler
4488 * any of the "non-immediate" type of ccbs.
4490 switch (done_ccb->ccb_h.path->periph->type) {
4491 case CAM_PERIPH_BIO:
4492 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4494 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4497 case CAM_PERIPH_NET:
4498 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4500 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4513 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4518 xpt_free_ccb(union ccb *free_ccb)
4520 free(free_ccb, M_DEVBUF);
4525 /* Private XPT functions */
4528 * Get a CAM control block for the caller. Charge the structure to the device
4529 * referenced by the path. If the this device has no 'credits' then the
4530 * device already has the maximum number of outstanding operations under way
4531 * and we return NULL. If we don't have sufficient resources to allocate more
4532 * ccbs, we also return NULL.
4535 xpt_get_ccb(struct cam_ed *device)
4541 if ((new_ccb = (union ccb *)ccb_freeq.slh_first) == NULL) {
4542 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4543 if (new_ccb == NULL) {
4547 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4548 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4552 cam_ccbq_take_opening(&device->ccbq);
4553 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4559 xpt_release_bus(struct cam_eb *bus)
4564 if ((--bus->refcount == 0)
4565 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4566 TAILQ_REMOVE(&xpt_busses, bus, links);
4569 free(bus, M_DEVBUF);
4574 static struct cam_et *
4575 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4577 struct cam_et *target;
4579 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4580 if (target != NULL) {
4581 struct cam_et *cur_target;
4583 TAILQ_INIT(&target->ed_entries);
4585 target->target_id = target_id;
4586 target->refcount = 1;
4587 target->generation = 0;
4588 timevalclear(&target->last_reset);
4590 * Hold a reference to our parent bus so it
4591 * will not go away before we do.
4595 /* Insertion sort into our bus's target list */
4596 cur_target = TAILQ_FIRST(&bus->et_entries);
4597 while (cur_target != NULL && cur_target->target_id < target_id)
4598 cur_target = TAILQ_NEXT(cur_target, links);
4600 if (cur_target != NULL) {
4601 TAILQ_INSERT_BEFORE(cur_target, target, links);
4603 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4611 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4616 if ((--target->refcount == 0)
4617 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4618 TAILQ_REMOVE(&bus->et_entries, target, links);
4621 free(target, M_DEVBUF);
4622 xpt_release_bus(bus);
4627 static struct cam_ed *
4628 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4630 struct cam_ed *device;
4631 struct cam_devq *devq;
4634 /* Make space for us in the device queue on our bus */
4635 devq = bus->sim->devq;
4636 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4638 if (status != CAM_REQ_CMP) {
4641 device = (struct cam_ed *)malloc(sizeof(*device),
4642 M_DEVBUF, M_NOWAIT);
4645 if (device != NULL) {
4646 struct cam_ed *cur_device;
4648 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4649 device->alloc_ccb_entry.device = device;
4650 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4651 device->send_ccb_entry.device = device;
4652 device->target = target;
4653 device->lun_id = lun_id;
4654 /* Initialize our queues */
4655 if (camq_init(&device->drvq, 0) != 0) {
4656 free(device, M_DEVBUF);
4659 if (cam_ccbq_init(&device->ccbq,
4660 bus->sim->max_dev_openings) != 0) {
4661 camq_fini(&device->drvq);
4662 free(device, M_DEVBUF);
4665 SLIST_INIT(&device->asyncs);
4666 SLIST_INIT(&device->periphs);
4667 device->generation = 0;
4668 device->owner = NULL;
4670 * Take the default quirk entry until we have inquiry
4671 * data and can determine a better quirk to use.
4673 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4674 bzero(&device->inq_data, sizeof(device->inq_data));
4675 device->inq_flags = 0;
4676 device->queue_flags = 0;
4677 device->serial_num = NULL;
4678 device->serial_num_len = 0;
4679 device->qfrozen_cnt = 0;
4680 device->flags = CAM_DEV_UNCONFIGURED;
4681 device->tag_delay_count = 0;
4682 device->refcount = 1;
4683 callout_handle_init(&device->c_handle);
4686 * Hold a reference to our parent target so it
4687 * will not go away before we do.
4692 * XXX should be limited by number of CCBs this bus can
4695 xpt_max_ccbs += device->ccbq.devq_openings;
4696 /* Insertion sort into our target's device list */
4697 cur_device = TAILQ_FIRST(&target->ed_entries);
4698 while (cur_device != NULL && cur_device->lun_id < lun_id)
4699 cur_device = TAILQ_NEXT(cur_device, links);
4700 if (cur_device != NULL) {
4701 TAILQ_INSERT_BEFORE(cur_device, device, links);
4703 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4705 target->generation++;
4711 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4712 struct cam_ed *device)
4717 if ((--device->refcount == 0)
4718 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4719 struct cam_devq *devq;
4721 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4722 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4723 panic("Removing device while still queued for ccbs");
4725 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4726 untimeout(xpt_release_devq_timeout, device,
4729 TAILQ_REMOVE(&target->ed_entries, device,links);
4730 target->generation++;
4731 xpt_max_ccbs -= device->ccbq.devq_openings;
4732 /* Release our slot in the devq */
4733 devq = bus->sim->devq;
4734 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4736 free(device, M_DEVBUF);
4737 xpt_release_target(bus, target);
4743 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4753 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4754 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4755 if (result == CAM_REQ_CMP && (diff < 0)) {
4756 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4758 /* Adjust the global limit */
4759 xpt_max_ccbs += diff;
4764 static struct cam_eb *
4765 xpt_find_bus(path_id_t path_id)
4769 for (bus = TAILQ_FIRST(&xpt_busses);
4771 bus = TAILQ_NEXT(bus, links)) {
4772 if (bus->path_id == path_id) {
4780 static struct cam_et *
4781 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4783 struct cam_et *target;
4785 for (target = TAILQ_FIRST(&bus->et_entries);
4787 target = TAILQ_NEXT(target, links)) {
4788 if (target->target_id == target_id) {
4796 static struct cam_ed *
4797 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4799 struct cam_ed *device;
4801 for (device = TAILQ_FIRST(&target->ed_entries);
4803 device = TAILQ_NEXT(device, links)) {
4804 if (device->lun_id == lun_id) {
4813 union ccb *request_ccb;
4814 struct ccb_pathinq *cpi;
4816 } xpt_scan_bus_info;
4819 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
4820 * As the scan progresses, xpt_scan_bus is used as the
4821 * callback on completion function.
4824 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
4826 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4827 ("xpt_scan_bus\n"));
4828 switch (request_ccb->ccb_h.func_code) {
4831 xpt_scan_bus_info *scan_info;
4832 union ccb *work_ccb;
4833 struct cam_path *path;
4838 /* Find out the characteristics of the bus */
4839 work_ccb = xpt_alloc_ccb();
4840 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
4841 request_ccb->ccb_h.pinfo.priority);
4842 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4843 xpt_action(work_ccb);
4844 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4845 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
4846 xpt_free_ccb(work_ccb);
4847 xpt_done(request_ccb);
4851 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
4853 * Can't scan the bus on an adapter that
4854 * cannot perform the initiator role.
4856 request_ccb->ccb_h.status = CAM_REQ_CMP;
4857 xpt_free_ccb(work_ccb);
4858 xpt_done(request_ccb);
4862 /* Save some state for use while we probe for devices */
4863 scan_info = (xpt_scan_bus_info *)
4864 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
4865 scan_info->request_ccb = request_ccb;
4866 scan_info->cpi = &work_ccb->cpi;
4868 /* Cache on our stack so we can work asynchronously */
4869 max_target = scan_info->cpi->max_target;
4870 initiator_id = scan_info->cpi->initiator_id;
4873 * Don't count the initiator if the
4874 * initiator is addressable.
4876 scan_info->pending_count = max_target + 1;
4877 if (initiator_id <= max_target)
4878 scan_info->pending_count--;
4880 for (i = 0; i <= max_target; i++) {
4882 if (i == initiator_id)
4885 status = xpt_create_path(&path, xpt_periph,
4886 request_ccb->ccb_h.path_id,
4888 if (status != CAM_REQ_CMP) {
4889 printf("xpt_scan_bus: xpt_create_path failed"
4890 " with status %#x, bus scan halted\n",
4894 work_ccb = xpt_alloc_ccb();
4895 xpt_setup_ccb(&work_ccb->ccb_h, path,
4896 request_ccb->ccb_h.pinfo.priority);
4897 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
4898 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
4899 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
4900 work_ccb->crcn.flags = request_ccb->crcn.flags;
4902 printf("xpt_scan_bus: probing %d:%d:%d\n",
4903 request_ccb->ccb_h.path_id, i, 0);
4905 xpt_action(work_ccb);
4911 xpt_scan_bus_info *scan_info;
4913 target_id_t target_id;
4916 /* Reuse the same CCB to query if a device was really found */
4917 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
4918 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
4919 request_ccb->ccb_h.pinfo.priority);
4920 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
4922 path_id = request_ccb->ccb_h.path_id;
4923 target_id = request_ccb->ccb_h.target_id;
4924 lun_id = request_ccb->ccb_h.target_lun;
4925 xpt_action(request_ccb);
4928 printf("xpt_scan_bus: got back probe from %d:%d:%d\n",
4929 path_id, target_id, lun_id);
4932 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
4933 struct cam_ed *device;
4934 struct cam_et *target;
4938 * If we already probed lun 0 successfully, or
4939 * we have additional configured luns on this
4940 * target that might have "gone away", go onto
4943 target = request_ccb->ccb_h.path->target;
4945 * We may touch devices that we don't
4946 * hold references too, so ensure they
4947 * don't disappear out from under us.
4948 * The target above is referenced by the
4949 * path in the request ccb.
4953 device = TAILQ_FIRST(&target->ed_entries);
4954 if (device != NULL) {
4955 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
4956 if (device->lun_id == 0)
4957 device = TAILQ_NEXT(device, links);
4960 if ((lun_id != 0) || (device != NULL)) {
4961 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
4965 struct cam_ed *device;
4967 device = request_ccb->ccb_h.path->device;
4969 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
4970 /* Try the next lun */
4971 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
4972 (device->quirk->quirks & CAM_QUIRK_HILUNS))
4977 xpt_free_path(request_ccb->ccb_h.path);
4980 if ((lun_id == request_ccb->ccb_h.target_lun)
4981 || lun_id > scan_info->cpi->max_lun) {
4984 xpt_free_ccb(request_ccb);
4985 scan_info->pending_count--;
4986 if (scan_info->pending_count == 0) {
4987 xpt_free_ccb((union ccb *)scan_info->cpi);
4988 request_ccb = scan_info->request_ccb;
4989 free(scan_info, M_TEMP);
4990 request_ccb->ccb_h.status = CAM_REQ_CMP;
4991 xpt_done(request_ccb);
4994 /* Try the next device */
4995 struct cam_path *path;
4998 path = request_ccb->ccb_h.path;
4999 status = xpt_create_path(&path, xpt_periph,
5000 path_id, target_id, lun_id);
5001 if (status != CAM_REQ_CMP) {
5002 printf("xpt_scan_bus: xpt_create_path failed "
5003 "with status %#x, halting LUN scan\n",
5005 xpt_free_ccb(request_ccb);
5006 scan_info->pending_count--;
5007 if (scan_info->pending_count == 0) {
5009 (union ccb *)scan_info->cpi);
5010 request_ccb = scan_info->request_ccb;
5011 free(scan_info, M_TEMP);
5012 request_ccb->ccb_h.status = CAM_REQ_CMP;
5013 xpt_done(request_ccb);
5017 xpt_setup_ccb(&request_ccb->ccb_h, path,
5018 request_ccb->ccb_h.pinfo.priority);
5019 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5020 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5021 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5022 request_ccb->crcn.flags =
5023 scan_info->request_ccb->crcn.flags;
5025 xpt_print_path(path);
5026 printf("xpt_scan bus probing\n");
5028 xpt_action(request_ccb);
5043 PROBE_TUR_FOR_NEGOTIATION
5047 PROBE_INQUIRY_CKSUM = 0x01,
5048 PROBE_SERIAL_CKSUM = 0x02,
5049 PROBE_NO_ANNOUNCE = 0x04
5053 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5054 probe_action action;
5055 union ccb saved_ccb;
5058 u_int8_t digest[16];
5062 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5063 cam_flags flags, union ccb *request_ccb)
5065 struct ccb_pathinq cpi;
5067 struct cam_path *new_path;
5068 struct cam_periph *old_periph;
5071 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5072 ("xpt_scan_lun\n"));
5074 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5075 cpi.ccb_h.func_code = XPT_PATH_INQ;
5076 xpt_action((union ccb *)&cpi);
5078 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5079 if (request_ccb != NULL) {
5080 request_ccb->ccb_h.status = cpi.ccb_h.status;
5081 xpt_done(request_ccb);
5086 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5088 * Can't scan the bus on an adapter that
5089 * cannot perform the initiator role.
5091 if (request_ccb != NULL) {
5092 request_ccb->ccb_h.status = CAM_REQ_CMP;
5093 xpt_done(request_ccb);
5098 if (request_ccb == NULL) {
5099 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5100 if (request_ccb == NULL) {
5101 xpt_print_path(path);
5102 printf("xpt_scan_lun: can't allocate CCB, can't "
5106 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5107 if (new_path == NULL) {
5108 xpt_print_path(path);
5109 printf("xpt_scan_lun: can't allocate path, can't "
5111 free(request_ccb, M_TEMP);
5114 status = xpt_compile_path(new_path, xpt_periph,
5116 path->target->target_id,
5117 path->device->lun_id);
5119 if (status != CAM_REQ_CMP) {
5120 xpt_print_path(path);
5121 printf("xpt_scan_lun: can't compile path, can't "
5123 free(request_ccb, M_TEMP);
5124 free(new_path, M_TEMP);
5127 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5128 request_ccb->ccb_h.cbfcnp = xptscandone;
5129 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5130 request_ccb->crcn.flags = flags;
5134 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5137 softc = (probe_softc *)old_periph->softc;
5138 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5141 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5142 probestart, "probe",
5144 request_ccb->ccb_h.path, NULL, 0,
5147 if (status != CAM_REQ_CMP) {
5148 xpt_print_path(path);
5149 printf("xpt_scan_lun: cam_alloc_periph returned an "
5150 "error, can't continue probe\n");
5151 request_ccb->ccb_h.status = status;
5152 xpt_done(request_ccb);
5159 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5161 xpt_release_path(done_ccb->ccb_h.path);
5162 free(done_ccb->ccb_h.path, M_TEMP);
5163 free(done_ccb, M_TEMP);
5167 proberegister(struct cam_periph *periph, void *arg)
5169 union ccb *request_ccb; /* CCB representing the probe request */
5172 request_ccb = (union ccb *)arg;
5173 if (periph == NULL) {
5174 printf("proberegister: periph was NULL!!\n");
5175 return(CAM_REQ_CMP_ERR);
5178 if (request_ccb == NULL) {
5179 printf("proberegister: no probe CCB, "
5180 "can't register device\n");
5181 return(CAM_REQ_CMP_ERR);
5184 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5186 if (softc == NULL) {
5187 printf("proberegister: Unable to probe new device. "
5188 "Unable to allocate softc\n");
5189 return(CAM_REQ_CMP_ERR);
5191 TAILQ_INIT(&softc->request_ccbs);
5192 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5195 periph->softc = softc;
5196 cam_periph_acquire(periph);
5198 * Ensure we've waited at least a bus settle
5199 * delay before attempting to probe the device.
5200 * For HBAs that don't do bus resets, this won't make a difference.
5202 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5204 probeschedule(periph);
5205 return(CAM_REQ_CMP);
5209 probeschedule(struct cam_periph *periph)
5211 struct ccb_pathinq cpi;
5215 softc = (probe_softc *)periph->softc;
5216 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5218 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5219 cpi.ccb_h.func_code = XPT_PATH_INQ;
5220 xpt_action((union ccb *)&cpi);
5223 * If a device has gone away and another device, or the same one,
5224 * is back in the same place, it should have a unit attention
5225 * condition pending. It will not report the unit attention in
5226 * response to an inquiry, which may leave invalid transfer
5227 * negotiations in effect. The TUR will reveal the unit attention
5228 * condition. Only send the TUR for lun 0, since some devices
5229 * will get confused by commands other than inquiry to non-existent
5230 * luns. If you think a device has gone away start your scan from
5231 * lun 0. This will insure that any bogus transfer settings are
5234 * If we haven't seen the device before and the controller supports
5235 * some kind of transfer negotiation, negotiate with the first
5236 * sent command if no bus reset was performed at startup. This
5237 * ensures that the device is not confused by transfer negotiation
5238 * settings left over by loader or BIOS action.
5240 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5241 && (ccb->ccb_h.target_lun == 0)) {
5242 softc->action = PROBE_TUR;
5243 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5244 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5245 proberequestdefaultnegotiation(periph);
5246 softc->action = PROBE_INQUIRY;
5248 softc->action = PROBE_INQUIRY;
5251 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5252 softc->flags |= PROBE_NO_ANNOUNCE;
5254 softc->flags &= ~PROBE_NO_ANNOUNCE;
5256 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5260 probestart(struct cam_periph *periph, union ccb *start_ccb)
5262 /* Probe the device that our peripheral driver points to */
5263 struct ccb_scsiio *csio;
5266 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5268 softc = (probe_softc *)periph->softc;
5269 csio = &start_ccb->csio;
5271 switch (softc->action) {
5273 case PROBE_TUR_FOR_NEGOTIATION:
5275 scsi_test_unit_ready(csio,
5284 case PROBE_FULL_INQUIRY:
5287 struct scsi_inquiry_data *inq_buf;
5289 inq_buf = &periph->path->device->inq_data;
5291 * If the device is currently configured, we calculate an
5292 * MD5 checksum of the inquiry data, and if the serial number
5293 * length is greater than 0, add the serial number data
5294 * into the checksum as well. Once the inquiry and the
5295 * serial number check finish, we attempt to figure out
5296 * whether we still have the same device.
5298 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5300 MD5Init(&softc->context);
5301 MD5Update(&softc->context, (unsigned char *)inq_buf,
5302 sizeof(struct scsi_inquiry_data));
5303 softc->flags |= PROBE_INQUIRY_CKSUM;
5304 if (periph->path->device->serial_num_len > 0) {
5305 MD5Update(&softc->context,
5306 periph->path->device->serial_num,
5307 periph->path->device->serial_num_len);
5308 softc->flags |= PROBE_SERIAL_CKSUM;
5310 MD5Final(softc->digest, &softc->context);
5313 if (softc->action == PROBE_INQUIRY)
5314 inquiry_len = SHORT_INQUIRY_LENGTH;
5316 inquiry_len = inq_buf->additional_length + 4;
5322 (u_int8_t *)inq_buf,
5327 /*timeout*/60 * 1000);
5330 case PROBE_MODE_SENSE:
5335 mode_buf_len = sizeof(struct scsi_mode_header_6)
5336 + sizeof(struct scsi_mode_blk_desc)
5337 + sizeof(struct scsi_control_page);
5338 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5339 if (mode_buf != NULL) {
5340 scsi_mode_sense(csio,
5345 SMS_PAGE_CTRL_CURRENT,
5346 SMS_CONTROL_MODE_PAGE,
5353 xpt_print_path(periph->path);
5354 printf("Unable to mode sense control page - malloc failure\n");
5355 softc->action = PROBE_SERIAL_NUM;
5358 case PROBE_SERIAL_NUM:
5360 struct scsi_vpd_unit_serial_number *serial_buf;
5361 struct cam_ed* device;
5364 device = periph->path->device;
5365 device->serial_num = NULL;
5366 device->serial_num_len = 0;
5368 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5369 serial_buf = (struct scsi_vpd_unit_serial_number *)
5370 malloc(sizeof(*serial_buf), M_TEMP, M_NOWAIT);
5372 if (serial_buf != NULL) {
5373 bzero(serial_buf, sizeof(*serial_buf));
5378 (u_int8_t *)serial_buf,
5379 sizeof(*serial_buf),
5381 SVPD_UNIT_SERIAL_NUMBER,
5383 /*timeout*/60 * 1000);
5387 * We'll have to do without, let our probedone
5388 * routine finish up for us.
5390 start_ccb->csio.data_ptr = NULL;
5391 probedone(periph, start_ccb);
5395 xpt_action(start_ccb);
5399 proberequestdefaultnegotiation(struct cam_periph *periph)
5401 struct ccb_trans_settings cts;
5403 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5404 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5405 cts.flags = CCB_TRANS_USER_SETTINGS;
5406 xpt_action((union ccb *)&cts);
5407 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5408 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5409 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5410 xpt_action((union ccb *)&cts);
5414 probedone(struct cam_periph *periph, union ccb *done_ccb)
5417 struct cam_path *path;
5420 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5422 softc = (probe_softc *)periph->softc;
5423 path = done_ccb->ccb_h.path;
5424 priority = done_ccb->ccb_h.pinfo.priority;
5426 switch (softc->action) {
5429 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5431 if (cam_periph_error(done_ccb, 0,
5432 SF_NO_PRINT, NULL) == ERESTART)
5434 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5435 /* Don't wedge the queue */
5436 xpt_release_devq(done_ccb->ccb_h.path,
5440 softc->action = PROBE_INQUIRY;
5441 xpt_release_ccb(done_ccb);
5442 xpt_schedule(periph, priority);
5446 case PROBE_FULL_INQUIRY:
5448 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5449 struct scsi_inquiry_data *inq_buf;
5450 u_int8_t periph_qual;
5451 u_int8_t periph_dtype;
5453 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5454 inq_buf = &path->device->inq_data;
5456 periph_qual = SID_QUAL(inq_buf);
5457 periph_dtype = SID_TYPE(inq_buf);
5459 if (periph_dtype != T_NODEVICE) {
5460 switch(periph_qual) {
5461 case SID_QUAL_LU_CONNECTED:
5466 * We conservatively request only
5467 * SHORT_INQUIRY_LEN bytes of inquiry
5468 * information during our first try
5469 * at sending an INQUIRY. If the device
5470 * has more information to give,
5471 * perform a second request specifying
5472 * the amount of information the device
5473 * is willing to give.
5475 alen = inq_buf->additional_length;
5476 if (softc->action == PROBE_INQUIRY
5477 && alen > (SHORT_INQUIRY_LENGTH - 4)) {
5480 xpt_release_ccb(done_ccb);
5481 xpt_schedule(periph, priority);
5485 xpt_find_quirk(path->device);
5487 if ((inq_buf->flags & SID_CmdQue) != 0)
5494 path->device->flags &=
5495 ~CAM_DEV_UNCONFIGURED;
5497 xpt_release_ccb(done_ccb);
5498 xpt_schedule(periph, priority);
5505 } else if (cam_periph_error(done_ccb, 0,
5506 done_ccb->ccb_h.target_lun > 0
5507 ? SF_RETRY_UA|SF_QUIET_IR
5509 &softc->saved_ccb) == ERESTART) {
5511 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5512 /* Don't wedge the queue */
5513 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5517 * If we get to this point, we got an error status back
5518 * from the inquiry and the error status doesn't require
5519 * automatically retrying the command. Therefore, the
5520 * inquiry failed. If we had inquiry information before
5521 * for this device, but this latest inquiry command failed,
5522 * the device has probably gone away. If this device isn't
5523 * already marked unconfigured, notify the peripheral
5524 * drivers that this device is no more.
5526 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5527 /* Send the async notification. */
5528 xpt_async(AC_LOST_DEVICE, path, NULL);
5530 xpt_release_ccb(done_ccb);
5533 case PROBE_MODE_SENSE:
5535 struct ccb_scsiio *csio;
5536 struct scsi_mode_header_6 *mode_hdr;
5538 csio = &done_ccb->csio;
5539 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5540 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5541 struct scsi_control_page *page;
5544 offset = ((u_int8_t *)&mode_hdr[1])
5545 + mode_hdr->blk_desc_len;
5546 page = (struct scsi_control_page *)offset;
5547 path->device->queue_flags = page->queue_flags;
5548 } else if (cam_periph_error(done_ccb, 0,
5549 SF_RETRY_UA|SF_NO_PRINT,
5550 &softc->saved_ccb) == ERESTART) {
5552 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5553 /* Don't wedge the queue */
5554 xpt_release_devq(done_ccb->ccb_h.path,
5555 /*count*/1, /*run_queue*/TRUE);
5557 xpt_release_ccb(done_ccb);
5558 free(mode_hdr, M_TEMP);
5559 softc->action = PROBE_SERIAL_NUM;
5560 xpt_schedule(periph, priority);
5563 case PROBE_SERIAL_NUM:
5565 struct ccb_scsiio *csio;
5566 struct scsi_vpd_unit_serial_number *serial_buf;
5573 csio = &done_ccb->csio;
5574 priority = done_ccb->ccb_h.pinfo.priority;
5576 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5578 /* Clean up from previous instance of this device */
5579 if (path->device->serial_num != NULL) {
5580 free(path->device->serial_num, M_DEVBUF);
5581 path->device->serial_num = NULL;
5582 path->device->serial_num_len = 0;
5585 if (serial_buf == NULL) {
5587 * Don't process the command as it was never sent
5589 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5590 && (serial_buf->length > 0)) {
5593 path->device->serial_num =
5594 (u_int8_t *)malloc((serial_buf->length + 1),
5595 M_DEVBUF, M_NOWAIT);
5596 if (path->device->serial_num != NULL) {
5597 bcopy(serial_buf->serial_num,
5598 path->device->serial_num,
5599 serial_buf->length);
5600 path->device->serial_num_len =
5602 path->device->serial_num[serial_buf->length]
5605 } else if (cam_periph_error(done_ccb, 0,
5606 SF_RETRY_UA|SF_NO_PRINT,
5607 &softc->saved_ccb) == ERESTART) {
5609 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5610 /* Don't wedge the queue */
5611 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5616 * Let's see if we have seen this device before.
5618 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5620 u_int8_t digest[16];
5625 (unsigned char *)&path->device->inq_data,
5626 sizeof(struct scsi_inquiry_data));
5629 MD5Update(&context, serial_buf->serial_num,
5630 serial_buf->length);
5632 MD5Final(digest, &context);
5633 if (bcmp(softc->digest, digest, 16) == 0)
5637 * XXX Do we need to do a TUR in order to ensure
5638 * that the device really hasn't changed???
5641 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5642 xpt_async(AC_LOST_DEVICE, path, NULL);
5644 if (serial_buf != NULL)
5645 free(serial_buf, M_TEMP);
5649 * Now that we have all the necessary
5650 * information to safely perform transfer
5651 * negotiations... Controllers don't perform
5652 * any negotiation or tagged queuing until
5653 * after the first XPT_SET_TRAN_SETTINGS ccb is
5654 * received. So, on a new device, just retreive
5655 * the user settings, and set them as the current
5656 * settings to set the device up.
5658 proberequestdefaultnegotiation(periph);
5659 xpt_release_ccb(done_ccb);
5662 * Perform a TUR to allow the controller to
5663 * perform any necessary transfer negotiation.
5665 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5666 xpt_schedule(periph, priority);
5669 xpt_release_ccb(done_ccb);
5672 case PROBE_TUR_FOR_NEGOTIATION:
5673 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5674 /* Don't wedge the queue */
5675 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5679 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5681 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5682 /* Inform the XPT that a new device has been found */
5683 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5684 xpt_action(done_ccb);
5686 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5688 xpt_release_ccb(done_ccb);
5691 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5692 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5693 done_ccb->ccb_h.status = CAM_REQ_CMP;
5695 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5696 cam_periph_invalidate(periph);
5697 cam_periph_release(periph);
5699 probeschedule(periph);
5704 probecleanup(struct cam_periph *periph)
5706 free(periph->softc, M_TEMP);
5710 xpt_find_quirk(struct cam_ed *device)
5714 match = cam_quirkmatch((caddr_t)&device->inq_data,
5715 (caddr_t)xpt_quirk_table,
5716 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5717 sizeof(*xpt_quirk_table), scsi_inquiry_match);
5720 panic("xpt_find_quirk: device didn't match wildcard entry!!");
5722 device->quirk = (struct xpt_quirk_entry *)match;
5726 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
5729 struct cam_sim *sim;
5732 sim = cts->ccb_h.path->bus->sim;
5733 if (async_update == FALSE) {
5734 struct scsi_inquiry_data *inq_data;
5735 struct ccb_pathinq cpi;
5736 struct ccb_trans_settings cur_cts;
5738 if (device == NULL) {
5739 cts->ccb_h.status = CAM_PATH_INVALID;
5740 xpt_done((union ccb *)cts);
5745 * Perform sanity checking against what the
5746 * controller and device can do.
5748 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
5749 cpi.ccb_h.func_code = XPT_PATH_INQ;
5750 xpt_action((union ccb *)&cpi);
5751 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
5752 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5753 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
5754 xpt_action((union ccb *)&cur_cts);
5755 inq_data = &device->inq_data;
5757 /* Fill in any gaps in what the user gave us */
5758 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
5759 cts->sync_period = cur_cts.sync_period;
5760 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
5761 cts->sync_offset = cur_cts.sync_offset;
5762 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
5763 cts->bus_width = cur_cts.bus_width;
5764 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
5765 cts->flags &= ~CCB_TRANS_DISC_ENB;
5766 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
5768 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
5769 cts->flags &= ~CCB_TRANS_TAG_ENB;
5770 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
5772 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5773 && (inq_data->flags & SID_Sync) == 0)
5774 || (cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
5776 cts->sync_period = 0;
5777 cts->sync_offset = 0;
5780 switch (cts->bus_width) {
5781 case MSG_EXT_WDTR_BUS_32_BIT:
5782 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5783 || (inq_data->flags & SID_WBus32) != 0)
5784 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
5786 /* Fall Through to 16-bit */
5787 case MSG_EXT_WDTR_BUS_16_BIT:
5788 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5789 || (inq_data->flags & SID_WBus16) != 0)
5790 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
5791 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5794 /* Fall Through to 8-bit */
5795 default: /* New bus width?? */
5796 case MSG_EXT_WDTR_BUS_8_BIT:
5797 /* All targets can do this */
5798 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5802 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
5804 * Can't tag queue without disconnection.
5806 cts->flags &= ~CCB_TRANS_TAG_ENB;
5807 cts->valid |= CCB_TRANS_TQ_VALID;
5810 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
5811 || (inq_data->flags & SID_CmdQue) == 0
5812 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
5813 || (device->quirk->mintags == 0)) {
5815 * Can't tag on hardware that doesn't support,
5816 * doesn't have it enabled, or has broken tag support.
5818 cts->flags &= ~CCB_TRANS_TAG_ENB;
5823 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0
5824 && (async_update == FALSE)) {
5828 * If we are transitioning from tags to no-tags or
5829 * vice-versa, we need to carefully freeze and restart
5830 * the queue so that we don't overlap tagged and non-tagged
5831 * commands. We also temporarily stop tags if there is
5832 * a change in transfer negotiation settings to allow
5833 * "tag-less" negotiation.
5835 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5836 || (device->inq_flags & SID_CmdQue) != 0)
5837 device_tagenb = TRUE;
5839 device_tagenb = FALSE;
5841 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
5842 && device_tagenb == FALSE)
5843 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
5844 && device_tagenb == TRUE)) {
5846 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
5848 * Delay change to use tags until after a
5849 * few commands have gone to this device so
5850 * the controller has time to perform transfer
5851 * negotiations without tagged messages getting
5854 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
5855 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
5857 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
5859 device->inq_flags &= ~SID_CmdQue;
5860 xpt_dev_ccbq_resize(cts->ccb_h.path,
5861 sim->max_dev_openings);
5862 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5863 device->tag_delay_count = 0;
5868 if (async_update == FALSE) {
5870 * If we are currently performing tagged transactions to
5871 * this device and want to change its negotiation parameters,
5872 * go non-tagged for a bit to give the controller a chance to
5873 * negotiate unhampered by tag messages.
5875 if ((device->inq_flags & SID_CmdQue) != 0
5876 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
5877 CCB_TRANS_SYNC_OFFSET_VALID|
5878 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
5879 xpt_toggle_tags(cts->ccb_h.path);
5881 (*(sim->sim_action))(sim, (union ccb *)cts);
5885 struct ccb_relsim crs;
5887 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
5889 crs.ccb_h.func_code = XPT_REL_SIMQ;
5890 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5892 = crs.release_timeout
5895 xpt_action((union ccb *)&crs);
5900 xpt_toggle_tags(struct cam_path *path)
5905 * Give controllers a chance to renegotiate
5906 * before starting tag operations. We
5907 * "toggle" tagged queuing off then on
5908 * which causes the tag enable command delay
5909 * counter to come into effect.
5912 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5913 || ((dev->inq_flags & SID_CmdQue) != 0
5914 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
5915 struct ccb_trans_settings cts;
5917 xpt_setup_ccb(&cts.ccb_h, path, 1);
5919 cts.valid = CCB_TRANS_TQ_VALID;
5920 xpt_set_transfer_settings(&cts, path->device,
5921 /*async_update*/TRUE);
5922 cts.flags = CCB_TRANS_TAG_ENB;
5923 xpt_set_transfer_settings(&cts, path->device,
5924 /*async_update*/TRUE);
5929 xpt_start_tags(struct cam_path *path)
5931 struct ccb_relsim crs;
5932 struct cam_ed *device;
5933 struct cam_sim *sim;
5936 device = path->device;
5937 sim = path->bus->sim;
5938 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5939 xpt_freeze_devq(path, /*count*/1);
5940 device->inq_flags |= SID_CmdQue;
5941 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
5942 xpt_dev_ccbq_resize(path, newopenings);
5943 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
5944 crs.ccb_h.func_code = XPT_REL_SIMQ;
5945 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5947 = crs.release_timeout
5950 xpt_action((union ccb *)&crs);
5953 static int busses_to_config;
5954 static int busses_to_reset;
5957 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
5959 if (bus->path_id != CAM_XPT_PATH_ID) {
5960 struct cam_path path;
5961 struct ccb_pathinq cpi;
5965 xpt_compile_path(&path, NULL, bus->path_id,
5966 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5967 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
5968 cpi.ccb_h.func_code = XPT_PATH_INQ;
5969 xpt_action((union ccb *)&cpi);
5970 can_negotiate = cpi.hba_inquiry;
5971 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
5972 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
5975 xpt_release_path(&path);
5982 xptconfigfunc(struct cam_eb *bus, void *arg)
5984 struct cam_path *path;
5985 union ccb *work_ccb;
5987 if (bus->path_id != CAM_XPT_PATH_ID) {
5991 work_ccb = xpt_alloc_ccb();
5992 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
5993 CAM_TARGET_WILDCARD,
5994 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
5995 printf("xptconfigfunc: xpt_create_path failed with "
5996 "status %#x for bus %d\n", status, bus->path_id);
5997 printf("xptconfigfunc: halting bus configuration\n");
5998 xpt_free_ccb(work_ccb);
6000 xpt_finishconfig(xpt_periph, NULL);
6003 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6004 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6005 xpt_action(work_ccb);
6006 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6007 printf("xptconfigfunc: CPI failed on bus %d "
6008 "with status %d\n", bus->path_id,
6009 work_ccb->ccb_h.status);
6010 xpt_finishconfig(xpt_periph, work_ccb);
6014 can_negotiate = work_ccb->cpi.hba_inquiry;
6015 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6016 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6017 && (can_negotiate != 0)) {
6018 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6019 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6020 work_ccb->ccb_h.cbfcnp = NULL;
6021 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6022 ("Resetting Bus\n"));
6023 xpt_action(work_ccb);
6024 xpt_finishconfig(xpt_periph, work_ccb);
6026 /* Act as though we performed a successful BUS RESET */
6027 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6028 xpt_finishconfig(xpt_periph, work_ccb);
6036 xpt_config(void *arg)
6038 /* Now that interrupts are enabled, go find our devices */
6041 /* Setup debugging flags and path */
6042 #ifdef CAM_DEBUG_FLAGS
6043 cam_dflags = CAM_DEBUG_FLAGS;
6044 #else /* !CAM_DEBUG_FLAGS */
6045 cam_dflags = CAM_DEBUG_NONE;
6046 #endif /* CAM_DEBUG_FLAGS */
6047 #ifdef CAM_DEBUG_BUS
6048 if (cam_dflags != CAM_DEBUG_NONE) {
6049 if (xpt_create_path(&cam_dpath, xpt_periph,
6050 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6051 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6052 printf("xpt_config: xpt_create_path() failed for debug"
6053 " target %d:%d:%d, debugging disabled\n",
6054 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6055 cam_dflags = CAM_DEBUG_NONE;
6059 #else /* !CAM_DEBUG_BUS */
6061 #endif /* CAM_DEBUG_BUS */
6062 #endif /* CAMDEBUG */
6065 * Scan all installed busses.
6067 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6069 if (busses_to_config == 0) {
6070 /* Call manually because we don't have any busses */
6071 xpt_finishconfig(xpt_periph, NULL);
6073 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6074 printf("Waiting %d seconds for SCSI "
6075 "devices to settle\n", SCSI_DELAY/1000);
6077 xpt_for_all_busses(xptconfigfunc, NULL);
6082 * If the given device only has one peripheral attached to it, and if that
6083 * peripheral is the passthrough driver, announce it. This insures that the
6084 * user sees some sort of announcement for every peripheral in their system.
6087 xptpassannouncefunc(struct cam_ed *device, void *arg)
6089 struct cam_periph *periph;
6092 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6093 periph = SLIST_NEXT(periph, periph_links), i++);
6095 periph = SLIST_FIRST(&device->periphs);
6097 && (strncmp(periph->periph_name, "pass", 4) == 0))
6098 xpt_announce_periph(periph, NULL);
6104 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6106 struct periph_driver **p_drv;
6109 if (done_ccb != NULL) {
6110 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6111 ("xpt_finishconfig\n"));
6112 switch(done_ccb->ccb_h.func_code) {
6114 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6115 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6116 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6117 xpt_action(done_ccb);
6123 xpt_free_path(done_ccb->ccb_h.path);
6129 if (busses_to_config == 0) {
6130 /* Register all the peripheral drivers */
6131 /* XXX This will have to change when we have loadable modules */
6132 p_drv = (struct periph_driver **)periphdriver_set.ls_items;
6133 for (i = 0; p_drv[i] != NULL; i++) {
6134 (*p_drv[i]->init)();
6138 * Check for devices with no "standard" peripheral driver
6139 * attached. For any devices like that, announce the
6140 * passthrough driver so the user will see something.
6142 xpt_for_all_devices(xptpassannouncefunc, NULL);
6144 /* Release our hook so that the boot can continue. */
6145 config_intrhook_disestablish(xpt_config_hook);
6146 free(xpt_config_hook, M_TEMP);
6147 xpt_config_hook = NULL;
6149 if (done_ccb != NULL)
6150 xpt_free_ccb(done_ccb);
6154 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6156 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6158 switch (work_ccb->ccb_h.func_code) {
6159 /* Common cases first */
6160 case XPT_PATH_INQ: /* Path routing inquiry */
6162 struct ccb_pathinq *cpi;
6164 cpi = &work_ccb->cpi;
6165 cpi->version_num = 1; /* XXX??? */
6166 cpi->hba_inquiry = 0;
6167 cpi->target_sprt = 0;
6169 cpi->hba_eng_cnt = 0;
6170 cpi->max_target = 0;
6172 cpi->initiator_id = 0;
6173 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6174 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6175 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6176 cpi->unit_number = sim->unit_number;
6177 cpi->bus_id = sim->bus_id;
6178 cpi->base_transfer_speed = 0;
6179 cpi->ccb_h.status = CAM_REQ_CMP;
6184 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6191 * The xpt as a "controller" has no interrupt sources, so polling
6195 xptpoll(struct cam_sim *sim)
6200 * Should only be called by the machine interrupt dispatch routines,
6201 * so put these prototypes here instead of in the header.
6217 camisr(cam_isrq_t *queue)
6220 struct ccb_hdr *ccb_h;
6223 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6226 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6227 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6230 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6235 if (ccb_h->flags & CAM_HIGH_POWER) {
6236 struct highpowerlist *hphead;
6237 struct cam_ed *device;
6238 union ccb *send_ccb;
6240 hphead = &highpowerq;
6242 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6245 * Increment the count since this command is done.
6250 * Any high powered commands queued up?
6252 if (send_ccb != NULL) {
6253 device = send_ccb->ccb_h.path->device;
6255 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6257 xpt_release_devq(send_ccb->ccb_h.path,
6258 /*count*/1, /*runqueue*/TRUE);
6261 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6264 dev = ccb_h->path->device;
6267 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6269 ccb_h->path->bus->sim->devq->send_active--;
6270 ccb_h->path->bus->sim->devq->send_openings++;
6273 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6274 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6275 && (dev->ccbq.dev_active == 0))) {
6277 xpt_release_devq(ccb_h->path, /*count*/1,
6281 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6282 && (--dev->tag_delay_count == 0))
6283 xpt_start_tags(ccb_h->path);
6285 if ((dev->ccbq.queue.entries > 0)
6286 && (dev->qfrozen_cnt == 0)
6287 && (device_is_send_queued(dev) == 0)) {
6288 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6293 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6294 xpt_release_simq(ccb_h->path->bus->sim,
6296 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6300 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6301 && (ccb_h->status & CAM_DEV_QFRZN)) {
6302 xpt_release_devq(ccb_h->path, /*count*/1,
6304 ccb_h->status &= ~CAM_DEV_QFRZN;
6306 xpt_run_dev_sendq(ccb_h->path->bus);
6309 /* Call the peripheral driver's callback */
6310 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6312 /* Raise IPL for while test */