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>
44 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
47 #include <machine/clock.h>
48 #include <machine/ipl.h>
51 #include <cam/cam_conf.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_debug.h>
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;
135 u_int8_t serial_num_len;
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 * This old revision of the TDC3600 is also SCSI-1, and
434 * hangs upon serial number probing.
437 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
440 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
444 * Would repond to all LUNs if asked for.
447 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
450 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
454 * Would repond to all LUNs if asked for.
457 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
460 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
463 /* Submitted by: Matthew Dodd <winter@jurai.net> */
464 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
465 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
468 /* Submitted by: Matthew Dodd <winter@jurai.net> */
469 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
470 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
473 /* Default tagged queuing parameters for all devices */
475 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
476 /*vendor*/"*", /*product*/"*", /*revision*/"*"
478 /*quirks*/0, /*mintags*/2, /*maxtags*/255
482 static const int xpt_quirk_table_size =
483 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
487 DM_RET_FLAG_MASK = 0x0f,
490 DM_RET_DESCEND = 0x20,
492 DM_RET_ACTION_MASK = 0xf0
500 } xpt_traverse_depth;
502 struct xpt_traverse_config {
503 xpt_traverse_depth depth;
508 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
509 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
510 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
511 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
512 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
514 /* Transport layer configuration information */
515 static struct xpt_softc xsoftc;
517 /* Queues for our software interrupt handler */
518 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
519 static cam_isrq_t cam_bioq;
520 static cam_isrq_t cam_netq;
522 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
523 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
524 static u_int xpt_max_ccbs; /*
525 * Maximum size of ccb pool. Modified as
526 * devices are added/removed or have their
527 * opening counts changed.
529 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
531 static struct cam_periph *xpt_periph;
533 static periph_init_t xpt_periph_init;
535 static periph_init_t probe_periph_init;
537 static struct periph_driver xpt_driver =
539 xpt_periph_init, "xpt",
540 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
543 static struct periph_driver probe_driver =
545 probe_periph_init, "probe",
546 TAILQ_HEAD_INITIALIZER(probe_driver.units)
549 DATA_SET(periphdriver_set, xpt_driver);
550 DATA_SET(periphdriver_set, probe_driver);
552 #define XPT_CDEV_MAJOR 104
554 static d_open_t xptopen;
555 static d_close_t xptclose;
556 static d_ioctl_t xptioctl;
558 static struct cdevsw xpt_cdevsw = {
560 /* close */ xptclose,
563 /* ioctl */ xptioctl,
566 /* strategy */ nostrategy,
568 /* maj */ XPT_CDEV_MAJOR,
575 static struct intr_config_hook *xpt_config_hook;
577 /* Registered busses */
578 static TAILQ_HEAD(,cam_eb) xpt_busses;
579 static u_int bus_generation;
581 /* Storage for debugging datastructures */
583 struct cam_path *cam_dpath;
584 u_int32_t cam_dflags;
585 u_int32_t cam_debug_delay;
588 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
589 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
593 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
594 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
595 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
597 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
598 || defined(CAM_DEBUG_LUN)
600 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
601 || !defined(CAM_DEBUG_LUN)
602 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
604 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
605 #else /* !CAMDEBUG */
606 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
607 #endif /* CAMDEBUG */
608 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
610 /* Our boot-time initialization hook */
611 static void xpt_init(void *);
612 SYSINIT(cam, SI_SUB_CONFIGURE, SI_ORDER_SECOND, xpt_init, NULL);
614 static cam_status xpt_compile_path(struct cam_path *new_path,
615 struct cam_periph *perph,
617 target_id_t target_id,
620 static void xpt_release_path(struct cam_path *path);
622 static void xpt_async_bcast(struct async_list *async_head,
623 u_int32_t async_code,
624 struct cam_path *path,
626 static int xptnextfreebus(path_id_t startbus);
627 static int xptpathid(const char *sim_name, int sim_unit, int sim_bus,
628 path_id_t *nextpath);
629 static union ccb *xpt_get_ccb(struct cam_ed *device);
630 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
631 u_int32_t new_priority);
632 static void xpt_run_dev_allocq(struct cam_eb *bus);
633 static void xpt_run_dev_sendq(struct cam_eb *bus);
634 static timeout_t xpt_release_devq_timeout;
635 static timeout_t xpt_release_simq_timeout;
636 static void xpt_release_bus(struct cam_eb *bus);
637 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
639 static struct cam_et*
640 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
641 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
642 static struct cam_ed*
643 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
645 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
646 struct cam_ed *device);
647 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
648 static struct cam_eb*
649 xpt_find_bus(path_id_t path_id);
650 static struct cam_et*
651 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
652 static struct cam_ed*
653 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
654 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
655 static void xpt_scan_lun(struct cam_periph *periph,
656 struct cam_path *path, cam_flags flags,
658 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
659 static xpt_busfunc_t xptconfigbuscountfunc;
660 static xpt_busfunc_t xptconfigfunc;
661 static void xpt_config(void *arg);
662 static xpt_devicefunc_t xptpassannouncefunc;
663 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
664 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
665 static swihand_t swi_camnet;
666 static swihand_t swi_cambio;
667 static void camisr(cam_isrq_t *queue);
669 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
670 static void xptasync(struct cam_periph *periph,
671 u_int32_t code, cam_path *path);
673 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
674 int num_patterns, struct cam_eb *bus);
675 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
676 int num_patterns, struct cam_ed *device);
677 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
679 struct cam_periph *periph);
680 static xpt_busfunc_t xptedtbusfunc;
681 static xpt_targetfunc_t xptedttargetfunc;
682 static xpt_devicefunc_t xptedtdevicefunc;
683 static xpt_periphfunc_t xptedtperiphfunc;
684 static xpt_pdrvfunc_t xptplistpdrvfunc;
685 static xpt_periphfunc_t xptplistperiphfunc;
686 static int xptedtmatch(struct ccb_dev_match *cdm);
687 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
688 static int xptbustraverse(struct cam_eb *start_bus,
689 xpt_busfunc_t *tr_func, void *arg);
690 static int xpttargettraverse(struct cam_eb *bus,
691 struct cam_et *start_target,
692 xpt_targetfunc_t *tr_func, void *arg);
693 static int xptdevicetraverse(struct cam_et *target,
694 struct cam_ed *start_device,
695 xpt_devicefunc_t *tr_func, void *arg);
696 static int xptperiphtraverse(struct cam_ed *device,
697 struct cam_periph *start_periph,
698 xpt_periphfunc_t *tr_func, void *arg);
699 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
700 xpt_pdrvfunc_t *tr_func, void *arg);
701 static int xptpdperiphtraverse(struct periph_driver **pdrv,
702 struct cam_periph *start_periph,
703 xpt_periphfunc_t *tr_func,
705 static xpt_busfunc_t xptdefbusfunc;
706 static xpt_targetfunc_t xptdeftargetfunc;
707 static xpt_devicefunc_t xptdefdevicefunc;
708 static xpt_periphfunc_t xptdefperiphfunc;
709 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
711 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
714 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
717 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
720 static xpt_devicefunc_t xptsetasyncfunc;
721 static xpt_busfunc_t xptsetasyncbusfunc;
722 static cam_status xptregister(struct cam_periph *periph,
724 static cam_status proberegister(struct cam_periph *periph,
726 static void probeschedule(struct cam_periph *probe_periph);
727 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
728 static void proberequestdefaultnegotiation(struct cam_periph *periph);
729 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
730 static void probecleanup(struct cam_periph *periph);
731 static void xpt_find_quirk(struct cam_ed *device);
732 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
733 struct cam_ed *device,
735 static void xpt_toggle_tags(struct cam_path *path);
736 static void xpt_start_tags(struct cam_path *path);
737 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
739 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
741 static __inline int periph_is_queued(struct cam_periph *periph);
742 static __inline int device_is_alloc_queued(struct cam_ed *device);
743 static __inline int device_is_send_queued(struct cam_ed *device);
744 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
747 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
751 if (dev->ccbq.devq_openings > 0) {
752 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
753 cam_ccbq_resize(&dev->ccbq,
754 dev->ccbq.dev_openings
755 + dev->ccbq.dev_active);
756 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
759 * The priority of a device waiting for CCB resources
760 * is that of the the highest priority peripheral driver
763 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
764 &dev->alloc_ccb_entry.pinfo,
765 CAMQ_GET_HEAD(&dev->drvq)->priority);
774 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
778 if (dev->ccbq.dev_openings > 0) {
780 * The priority of a device waiting for controller
781 * resources is that of the the highest priority CCB
785 xpt_schedule_dev(&bus->sim->devq->send_queue,
786 &dev->send_ccb_entry.pinfo,
787 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
795 periph_is_queued(struct cam_periph *periph)
797 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
801 device_is_alloc_queued(struct cam_ed *device)
803 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
807 device_is_send_queued(struct cam_ed *device)
809 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
813 dev_allocq_is_runnable(struct cam_devq *devq)
817 * Have space to do more work.
818 * Allowed to do work.
820 return ((devq->alloc_queue.qfrozen_cnt == 0)
821 && (devq->alloc_queue.entries > 0)
822 && (devq->alloc_openings > 0));
828 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
838 xptdone(struct cam_periph *periph, union ccb *done_ccb)
840 /* Caller will release the CCB */
841 wakeup(&done_ccb->ccb_h.cbfcnp);
845 xptopen(dev_t dev, int flags, int fmt, struct proc *p)
849 unit = minor(dev) & 0xff;
852 * Only allow read-write access.
854 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
858 * We don't allow nonblocking access.
860 if ((flags & O_NONBLOCK) != 0) {
861 printf("xpt%d: can't do nonblocking accesss\n", unit);
866 * We only have one transport layer right now. If someone accesses
867 * us via something other than minor number 1, point out their
871 printf("xptopen: got invalid xpt unit %d\n", unit);
875 /* Mark ourselves open */
876 xsoftc.flags |= XPT_FLAG_OPEN;
882 xptclose(dev_t dev, int flag, int fmt, struct proc *p)
886 unit = minor(dev) & 0xff;
889 * We only have one transport layer right now. If someone accesses
890 * us via something other than minor number 1, point out their
894 printf("xptclose: got invalid xpt unit %d\n", unit);
898 /* Mark ourselves closed */
899 xsoftc.flags &= ~XPT_FLAG_OPEN;
905 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
910 unit = minor(dev) & 0xff;
913 * We only have one transport layer right now. If someone accesses
914 * us via something other than minor number 1, point out their
918 printf("xptioctl: got invalid xpt unit %d\n", unit);
924 * For the transport layer CAMIOCOMMAND ioctl, we really only want
925 * to accept CCB types that don't quite make sense to send through a
926 * passthrough driver.
932 inccb = (union ccb *)addr;
934 switch(inccb->ccb_h.func_code) {
937 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
938 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
945 ccb = xpt_alloc_ccb();
948 * Create a path using the bus, target, and lun the
951 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
952 inccb->ccb_h.path_id,
953 inccb->ccb_h.target_id,
954 inccb->ccb_h.target_lun) !=
960 /* Ensure all of our fields are correct */
961 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
962 inccb->ccb_h.pinfo.priority);
963 xpt_merge_ccb(ccb, inccb);
964 ccb->ccb_h.cbfcnp = xptdone;
965 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
966 bcopy(ccb, inccb, sizeof(union ccb));
967 xpt_free_path(ccb->ccb_h.path);
975 * This is an immediate CCB, so it's okay to
976 * allocate it on the stack.
980 * Create a path using the bus, target, and lun the
983 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
984 inccb->ccb_h.path_id,
985 inccb->ccb_h.target_id,
986 inccb->ccb_h.target_lun) !=
991 /* Ensure all of our fields are correct */
992 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
993 inccb->ccb_h.pinfo.priority);
994 xpt_merge_ccb(&ccb, inccb);
995 ccb.ccb_h.cbfcnp = xptdone;
997 bcopy(&ccb, inccb, sizeof(union ccb));
998 xpt_free_path(ccb.ccb_h.path);
1002 case XPT_DEV_MATCH: {
1003 struct cam_periph_map_info mapinfo;
1004 struct cam_path *old_path;
1007 * We can't deal with physical addresses for this
1008 * type of transaction.
1010 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1016 * Save this in case the caller had it set to
1017 * something in particular.
1019 old_path = inccb->ccb_h.path;
1022 * We really don't need a path for the matching
1023 * code. The path is needed because of the
1024 * debugging statements in xpt_action(). They
1025 * assume that the CCB has a valid path.
1027 inccb->ccb_h.path = xpt_periph->path;
1029 bzero(&mapinfo, sizeof(mapinfo));
1032 * Map the pattern and match buffers into kernel
1033 * virtual address space.
1035 error = cam_periph_mapmem(inccb, &mapinfo);
1038 inccb->ccb_h.path = old_path;
1043 * This is an immediate CCB, we can send it on directly.
1048 * Map the buffers back into user space.
1050 cam_periph_unmapmem(inccb, &mapinfo);
1052 inccb->ccb_h.path = old_path;
1064 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1065 * with the periphal driver name and unit name filled in. The other
1066 * fields don't really matter as input. The passthrough driver name
1067 * ("pass"), and unit number are passed back in the ccb. The current
1068 * device generation number, and the index into the device peripheral
1069 * driver list, and the status are also passed back. Note that
1070 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1071 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1072 * (or rather should be) impossible for the device peripheral driver
1073 * list to change since we look at the whole thing in one pass, and
1074 * we do it with splcam protection.
1077 case CAMGETPASSTHRU: {
1079 struct cam_periph *periph;
1080 struct periph_driver **p_drv;
1084 int base_periph_found;
1088 ccb = (union ccb *)addr;
1089 unit = ccb->cgdl.unit_number;
1090 name = ccb->cgdl.periph_name;
1092 * Every 100 devices, we want to drop our spl protection to
1093 * give the software interrupt handler a chance to run.
1094 * Most systems won't run into this check, but this should
1095 * avoid starvation in the software interrupt handler in
1100 ccb = (union ccb *)addr;
1102 base_periph_found = 0;
1105 * Sanity check -- make sure we don't get a null peripheral
1108 if (*ccb->cgdl.periph_name == '\0') {
1113 /* Keep the list from changing while we traverse it */
1116 cur_generation = xsoftc.generation;
1118 /* first find our driver in the list of drivers */
1119 for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
1120 *p_drv != NULL; p_drv++)
1121 if (strcmp((*p_drv)->driver_name, name) == 0)
1124 if (*p_drv == NULL) {
1126 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1127 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1128 *ccb->cgdl.periph_name = '\0';
1129 ccb->cgdl.unit_number = 0;
1135 * Run through every peripheral instance of this driver
1136 * and check to see whether it matches the unit passed
1137 * in by the user. If it does, get out of the loops and
1138 * find the passthrough driver associated with that
1139 * peripheral driver.
1141 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1142 periph = TAILQ_NEXT(periph, unit_links)) {
1144 if (periph->unit_number == unit) {
1146 } else if (--splbreaknum == 0) {
1150 if (cur_generation != xsoftc.generation)
1155 * If we found the peripheral driver that the user passed
1156 * in, go through all of the peripheral drivers for that
1157 * particular device and look for a passthrough driver.
1159 if (periph != NULL) {
1160 struct cam_ed *device;
1163 base_periph_found = 1;
1164 device = periph->path->device;
1165 for (i = 0, periph = device->periphs.slh_first;
1167 periph = periph->periph_links.sle_next, i++) {
1169 * Check to see whether we have a
1170 * passthrough device or not.
1172 if (strcmp(periph->periph_name, "pass") == 0) {
1174 * Fill in the getdevlist fields.
1176 strcpy(ccb->cgdl.periph_name,
1177 periph->periph_name);
1178 ccb->cgdl.unit_number =
1179 periph->unit_number;
1180 if (periph->periph_links.sle_next)
1182 CAM_GDEVLIST_MORE_DEVS;
1185 CAM_GDEVLIST_LAST_DEVICE;
1186 ccb->cgdl.generation =
1188 ccb->cgdl.index = i;
1190 * Fill in some CCB header fields
1191 * that the user may want.
1193 ccb->ccb_h.path_id =
1194 periph->path->bus->path_id;
1195 ccb->ccb_h.target_id =
1196 periph->path->target->target_id;
1197 ccb->ccb_h.target_lun =
1198 periph->path->device->lun_id;
1199 ccb->ccb_h.status = CAM_REQ_CMP;
1206 * If the periph is null here, one of two things has
1207 * happened. The first possibility is that we couldn't
1208 * find the unit number of the particular peripheral driver
1209 * that the user is asking about. e.g. the user asks for
1210 * the passthrough driver for "da11". We find the list of
1211 * "da" peripherals all right, but there is no unit 11.
1212 * The other possibility is that we went through the list
1213 * of peripheral drivers attached to the device structure,
1214 * but didn't find one with the name "pass". Either way,
1215 * we return ENOENT, since we couldn't find something.
1217 if (periph == NULL) {
1218 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1219 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1220 *ccb->cgdl.periph_name = '\0';
1221 ccb->cgdl.unit_number = 0;
1224 * It is unfortunate that this is even necessary,
1225 * but there are many, many clueless users out there.
1226 * If this is true, the user is looking for the
1227 * passthrough driver, but doesn't have one in his
1230 if (base_periph_found == 1) {
1231 printf("xptioctl: pass driver is not in the "
1233 printf("xptioctl: put \"device pass0\" in "
1234 "your kernel config file\n");
1248 /* Functions accessed by the peripheral drivers */
1253 struct cam_sim *xpt_sim;
1254 struct cam_path *path;
1258 TAILQ_INIT(&xpt_busses);
1259 TAILQ_INIT(&cam_bioq);
1260 TAILQ_INIT(&cam_netq);
1261 SLIST_INIT(&ccb_freeq);
1262 STAILQ_INIT(&highpowerq);
1265 * The xpt layer is, itself, the equivelent of a SIM.
1266 * Allow 16 ccbs in the ccb pool for it. This should
1267 * give decent parallelism when we probe busses and
1268 * perform other XPT functions.
1270 xpt_sim = (struct cam_sim *)malloc(sizeof(*xpt_sim),
1271 M_DEVBUF, M_WAITOK);
1272 xpt_sim->sim_action = xptaction;
1273 xpt_sim->sim_name = "xpt";
1274 xpt_sim->path_id = CAM_XPT_PATH_ID;
1275 xpt_sim->bus_id = 0;
1276 xpt_sim->max_tagged_dev_openings = 0;
1277 xpt_sim->max_dev_openings = 0;
1278 xpt_sim->devq = cam_simq_alloc(16);
1281 xpt_bus_register(xpt_sim, 0);
1284 * Looking at the XPT from the SIM layer, the XPT is
1285 * the equivelent of a peripheral driver. Allocate
1286 * a peripheral driver entry for us.
1288 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1289 CAM_TARGET_WILDCARD,
1290 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1291 printf("xpt_init: xpt_create_path failed with status %#x,"
1292 " failing attach\n", status);
1296 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1297 path, NULL, 0, NULL);
1298 xpt_free_path(path);
1300 xpt_sim->softc = xpt_periph;
1303 * Register a callback for when interrupts are enabled.
1306 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1308 if (xpt_config_hook == NULL) {
1309 printf("xpt_init: Cannot malloc config hook "
1310 "- failing attach\n");
1313 bzero(xpt_config_hook, sizeof(*xpt_config_hook));
1315 xpt_config_hook->ich_func = xpt_config;
1316 if (config_intrhook_establish(xpt_config_hook) != 0) {
1317 free (xpt_config_hook, M_TEMP);
1318 printf("xpt_init: config_intrhook_establish failed "
1319 "- failing attach\n");
1322 /* Install our software interrupt handlers */
1323 register_swi(SWI_CAMNET, swi_camnet);
1324 register_swi(SWI_CAMBIO, swi_cambio);
1328 xptregister(struct cam_periph *periph, void *arg)
1330 if (periph == NULL) {
1331 printf("xptregister: periph was NULL!!\n");
1332 return(CAM_REQ_CMP_ERR);
1335 periph->softc = NULL;
1337 xpt_periph = periph;
1339 return(CAM_REQ_CMP);
1343 xpt_add_periph(struct cam_periph *periph)
1345 struct cam_ed *device;
1347 struct periph_list *periph_head;
1349 device = periph->path->device;
1351 periph_head = &device->periphs;
1353 status = CAM_REQ_CMP;
1355 if (device != NULL) {
1359 * Make room for this peripheral
1360 * so it will fit in the queue
1361 * when it's scheduled to run
1364 status = camq_resize(&device->drvq,
1365 device->drvq.array_size + 1);
1367 device->generation++;
1369 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1374 xsoftc.generation++;
1380 xpt_remove_periph(struct cam_periph *periph)
1382 struct cam_ed *device;
1384 device = periph->path->device;
1386 if (device != NULL) {
1388 struct periph_list *periph_head;
1390 periph_head = &device->periphs;
1392 /* Release the slot for this peripheral */
1394 camq_resize(&device->drvq, device->drvq.array_size - 1);
1396 device->generation++;
1398 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1403 xsoftc.generation++;
1408 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1412 struct cam_path *path;
1413 struct ccb_trans_settings cts;
1415 path = periph->path;
1417 * To ensure that this is printed in one piece,
1418 * mask out CAM interrupts.
1421 printf("%s%d at %s%d bus %d target %d lun %d\n",
1422 periph->periph_name, periph->unit_number,
1423 path->bus->sim->sim_name,
1424 path->bus->sim->unit_number,
1425 path->bus->sim->bus_id,
1426 path->target->target_id,
1427 path->device->lun_id);
1428 printf("%s%d: ", periph->periph_name, periph->unit_number);
1429 scsi_print_inquiry(&path->device->inq_data);
1431 && (path->device->serial_num_len > 0)) {
1432 /* Don't wrap the screen - print only the first 60 chars */
1433 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1434 periph->unit_number, path->device->serial_num);
1436 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1437 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1438 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1439 xpt_action((union ccb*)&cts);
1440 if (cts.ccb_h.status == CAM_REQ_CMP) {
1444 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1445 && cts.sync_offset != 0) {
1446 freq = scsi_calc_syncsrate(cts.sync_period);
1449 struct ccb_pathinq cpi;
1451 /* Ask the SIM for its base transfer speed */
1452 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1453 cpi.ccb_h.func_code = XPT_PATH_INQ;
1454 xpt_action((union ccb *)&cpi);
1456 speed = cpi.base_transfer_speed;
1459 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1460 speed *= (0x01 << cts.bus_width);
1463 printf("%s%d: %d.%03dMB/s transfers",
1464 periph->periph_name, periph->unit_number,
1467 printf("%s%d: %dKB/s transfers", periph->periph_name,
1468 periph->unit_number, (speed % 1000) * 1000);
1469 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1470 && cts.sync_offset != 0) {
1471 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1472 freq % 1000, cts.sync_offset);
1474 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1475 && cts.bus_width > 0) {
1476 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1477 && cts.sync_offset != 0) {
1482 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1483 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1484 && cts.sync_offset != 0) {
1488 if (path->device->inq_flags & SID_CmdQue
1489 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1490 printf(", Tagged Queueing Enabled");
1494 } else if (path->device->inq_flags & SID_CmdQue
1495 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1496 printf("%s%d: Tagged Queueing Enabled\n",
1497 periph->periph_name, periph->unit_number);
1501 * We only want to print the caller's announce string if they've
1504 if (announce_string != NULL)
1505 printf("%s%d: %s\n", periph->periph_name,
1506 periph->unit_number, announce_string);
1511 static dev_match_ret
1512 xptbusmatch(struct dev_match_pattern *patterns, int num_patterns,
1515 dev_match_ret retval;
1518 retval = DM_RET_NONE;
1521 * If we aren't given something to match against, that's an error.
1524 return(DM_RET_ERROR);
1527 * If there are no match entries, then this bus matches no
1530 if ((patterns == NULL) || (num_patterns == 0))
1531 return(DM_RET_DESCEND | DM_RET_COPY);
1533 for (i = 0; i < num_patterns; i++) {
1534 struct bus_match_pattern *cur_pattern;
1537 * If the pattern in question isn't for a bus node, we
1538 * aren't interested. However, we do indicate to the
1539 * calling routine that we should continue descending the
1540 * tree, since the user wants to match against lower-level
1543 if (patterns[i].type != DEV_MATCH_BUS) {
1544 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1545 retval |= DM_RET_DESCEND;
1549 cur_pattern = &patterns[i].pattern.bus_pattern;
1552 * If they want to match any bus node, we give them any
1555 if (cur_pattern->flags == BUS_MATCH_ANY) {
1556 /* set the copy flag */
1557 retval |= DM_RET_COPY;
1560 * If we've already decided on an action, go ahead
1563 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1568 * Not sure why someone would do this...
1570 if (cur_pattern->flags == BUS_MATCH_NONE)
1573 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1574 && (cur_pattern->path_id != bus->path_id))
1577 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1578 && (cur_pattern->bus_id != bus->sim->bus_id))
1581 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1582 && (cur_pattern->unit_number != bus->sim->unit_number))
1585 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1586 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1591 * If we get to this point, the user definitely wants
1592 * information on this bus. So tell the caller to copy the
1595 retval |= DM_RET_COPY;
1598 * If the return action has been set to descend, then we
1599 * know that we've already seen a non-bus matching
1600 * expression, therefore we need to further descend the tree.
1601 * This won't change by continuing around the loop, so we
1602 * go ahead and return. If we haven't seen a non-bus
1603 * matching expression, we keep going around the loop until
1604 * we exhaust the matching expressions. We'll set the stop
1605 * flag once we fall out of the loop.
1607 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1612 * If the return action hasn't been set to descend yet, that means
1613 * we haven't seen anything other than bus matching patterns. So
1614 * tell the caller to stop descending the tree -- the user doesn't
1615 * want to match against lower level tree elements.
1617 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1618 retval |= DM_RET_STOP;
1623 static dev_match_ret
1624 xptdevicematch(struct dev_match_pattern *patterns, int num_patterns,
1625 struct cam_ed *device)
1627 dev_match_ret retval;
1630 retval = DM_RET_NONE;
1633 * If we aren't given something to match against, that's an error.
1636 return(DM_RET_ERROR);
1639 * If there are no match entries, then this device matches no
1642 if ((patterns == NULL) || (patterns == 0))
1643 return(DM_RET_DESCEND | DM_RET_COPY);
1645 for (i = 0; i < num_patterns; i++) {
1646 struct device_match_pattern *cur_pattern;
1649 * If the pattern in question isn't for a device node, we
1650 * aren't interested.
1652 if (patterns[i].type != DEV_MATCH_DEVICE) {
1653 if ((patterns[i].type == DEV_MATCH_PERIPH)
1654 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1655 retval |= DM_RET_DESCEND;
1659 cur_pattern = &patterns[i].pattern.device_pattern;
1662 * If they want to match any device node, we give them any
1665 if (cur_pattern->flags == DEV_MATCH_ANY) {
1666 /* set the copy flag */
1667 retval |= DM_RET_COPY;
1671 * If we've already decided on an action, go ahead
1674 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1679 * Not sure why someone would do this...
1681 if (cur_pattern->flags == DEV_MATCH_NONE)
1684 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1685 && (cur_pattern->path_id != device->target->bus->path_id))
1688 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1689 && (cur_pattern->target_id != device->target->target_id))
1692 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1693 && (cur_pattern->target_lun != device->lun_id))
1696 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1697 && (cam_quirkmatch((caddr_t)&device->inq_data,
1698 (caddr_t)&cur_pattern->inq_pat,
1699 1, sizeof(cur_pattern->inq_pat),
1700 scsi_static_inquiry_match) == NULL))
1704 * If we get to this point, the user definitely wants
1705 * information on this device. So tell the caller to copy
1708 retval |= DM_RET_COPY;
1711 * If the return action has been set to descend, then we
1712 * know that we've already seen a peripheral matching
1713 * expression, therefore we need to further descend the tree.
1714 * This won't change by continuing around the loop, so we
1715 * go ahead and return. If we haven't seen a peripheral
1716 * matching expression, we keep going around the loop until
1717 * we exhaust the matching expressions. We'll set the stop
1718 * flag once we fall out of the loop.
1720 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1725 * If the return action hasn't been set to descend yet, that means
1726 * we haven't seen any peripheral matching patterns. So tell the
1727 * caller to stop descending the tree -- the user doesn't want to
1728 * match against lower level tree elements.
1730 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1731 retval |= DM_RET_STOP;
1737 * Match a single peripheral against any number of match patterns.
1739 static dev_match_ret
1740 xptperiphmatch(struct dev_match_pattern *patterns, int num_patterns,
1741 struct cam_periph *periph)
1743 dev_match_ret retval;
1747 * If we aren't given something to match against, that's an error.
1750 return(DM_RET_ERROR);
1753 * If there are no match entries, then this peripheral matches no
1756 if ((patterns == NULL) || (num_patterns == 0))
1757 return(DM_RET_STOP | DM_RET_COPY);
1760 * There aren't any nodes below a peripheral node, so there's no
1761 * reason to descend the tree any further.
1763 retval = DM_RET_STOP;
1765 for (i = 0; i < num_patterns; i++) {
1766 struct periph_match_pattern *cur_pattern;
1769 * If the pattern in question isn't for a peripheral, we
1770 * aren't interested.
1772 if (patterns[i].type != DEV_MATCH_PERIPH)
1775 cur_pattern = &patterns[i].pattern.periph_pattern;
1778 * If they want to match on anything, then we will do so.
1780 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1781 /* set the copy flag */
1782 retval |= DM_RET_COPY;
1785 * We've already set the return action to stop,
1786 * since there are no nodes below peripherals in
1793 * Not sure why someone would do this...
1795 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1798 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1799 && (cur_pattern->path_id != periph->path->bus->path_id))
1803 * For the target and lun id's, we have to make sure the
1804 * target and lun pointers aren't NULL. The xpt peripheral
1805 * has a wildcard target and device.
1807 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1808 && ((periph->path->target == NULL)
1809 ||(cur_pattern->target_id != periph->path->target->target_id)))
1812 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1813 && ((periph->path->device == NULL)
1814 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1817 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1818 && (cur_pattern->unit_number != periph->unit_number))
1821 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1822 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1827 * If we get to this point, the user definitely wants
1828 * information on this peripheral. So tell the caller to
1829 * copy the data out.
1831 retval |= DM_RET_COPY;
1834 * The return action has already been set to stop, since
1835 * peripherals don't have any nodes below them in the EDT.
1841 * If we get to this point, the peripheral that was passed in
1842 * doesn't match any of the patterns.
1848 xptedtbusfunc(struct cam_eb *bus, void *arg)
1850 struct ccb_dev_match *cdm;
1851 dev_match_ret retval;
1853 cdm = (struct ccb_dev_match *)arg;
1856 * If our position is for something deeper in the tree, that means
1857 * that we've already seen this node. So, we keep going down.
1859 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1860 && (cdm->pos.cookie.bus == bus)
1861 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1862 && (cdm->pos.cookie.target != NULL))
1863 retval = DM_RET_DESCEND;
1865 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1868 * If we got an error, bail out of the search.
1870 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1871 cdm->status = CAM_DEV_MATCH_ERROR;
1876 * If the copy flag is set, copy this bus out.
1878 if (retval & DM_RET_COPY) {
1881 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1882 sizeof(struct dev_match_result));
1885 * If we don't have enough space to put in another
1886 * match result, save our position and tell the
1887 * user there are more devices to check.
1889 if (spaceleft < sizeof(struct dev_match_result)) {
1890 bzero(&cdm->pos, sizeof(cdm->pos));
1891 cdm->pos.position_type =
1892 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1894 cdm->pos.cookie.bus = bus;
1895 cdm->pos.generations[CAM_BUS_GENERATION]=
1897 cdm->status = CAM_DEV_MATCH_MORE;
1900 j = cdm->num_matches;
1902 cdm->matches[j].type = DEV_MATCH_BUS;
1903 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1904 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1905 cdm->matches[j].result.bus_result.unit_number =
1906 bus->sim->unit_number;
1907 strncpy(cdm->matches[j].result.bus_result.dev_name,
1908 bus->sim->sim_name, DEV_IDLEN);
1912 * If the user is only interested in busses, there's no
1913 * reason to descend to the next level in the tree.
1915 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1919 * If there is a target generation recorded, check it to
1920 * make sure the target list hasn't changed.
1922 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1923 && (bus == cdm->pos.cookie.bus)
1924 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1925 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1926 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1928 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1932 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1933 && (cdm->pos.cookie.bus == bus)
1934 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1935 && (cdm->pos.cookie.target != NULL))
1936 return(xpttargettraverse(bus,
1937 (struct cam_et *)cdm->pos.cookie.target,
1938 xptedttargetfunc, arg));
1940 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1944 xptedttargetfunc(struct cam_et *target, void *arg)
1946 struct ccb_dev_match *cdm;
1948 cdm = (struct ccb_dev_match *)arg;
1951 * If there is a device list generation recorded, check it to
1952 * make sure the device list hasn't changed.
1954 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1955 && (cdm->pos.cookie.bus == target->bus)
1956 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1957 && (cdm->pos.cookie.target == target)
1958 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1959 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1960 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1961 target->generation)) {
1962 cdm->status = CAM_DEV_MATCH_LIST_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.cookie.device != NULL))
1972 return(xptdevicetraverse(target,
1973 (struct cam_ed *)cdm->pos.cookie.device,
1974 xptedtdevicefunc, arg));
1976 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1980 xptedtdevicefunc(struct cam_ed *device, void *arg)
1983 struct ccb_dev_match *cdm;
1984 dev_match_ret retval;
1986 cdm = (struct ccb_dev_match *)arg;
1989 * If our position is for something deeper in the tree, that means
1990 * that we've already seen this node. So, we keep going down.
1992 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1993 && (cdm->pos.cookie.device == device)
1994 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1995 && (cdm->pos.cookie.periph != NULL))
1996 retval = DM_RET_DESCEND;
1998 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2001 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2002 cdm->status = CAM_DEV_MATCH_ERROR;
2007 * If the copy flag is set, copy this device out.
2009 if (retval & DM_RET_COPY) {
2012 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2013 sizeof(struct dev_match_result));
2016 * If we don't have enough space to put in another
2017 * match result, save our position and tell the
2018 * user there are more devices to check.
2020 if (spaceleft < sizeof(struct dev_match_result)) {
2021 bzero(&cdm->pos, sizeof(cdm->pos));
2022 cdm->pos.position_type =
2023 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2024 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2026 cdm->pos.cookie.bus = device->target->bus;
2027 cdm->pos.generations[CAM_BUS_GENERATION]=
2029 cdm->pos.cookie.target = device->target;
2030 cdm->pos.generations[CAM_TARGET_GENERATION] =
2031 device->target->bus->generation;
2032 cdm->pos.cookie.device = device;
2033 cdm->pos.generations[CAM_DEV_GENERATION] =
2034 device->target->generation;
2035 cdm->status = CAM_DEV_MATCH_MORE;
2038 j = cdm->num_matches;
2040 cdm->matches[j].type = DEV_MATCH_DEVICE;
2041 cdm->matches[j].result.device_result.path_id =
2042 device->target->bus->path_id;
2043 cdm->matches[j].result.device_result.target_id =
2044 device->target->target_id;
2045 cdm->matches[j].result.device_result.target_lun =
2047 bcopy(&device->inq_data,
2048 &cdm->matches[j].result.device_result.inq_data,
2049 sizeof(struct scsi_inquiry_data));
2051 /* Let the user know whether this device is unconfigured */
2052 if (device->flags & CAM_DEV_UNCONFIGURED)
2053 cdm->matches[j].result.device_result.flags =
2054 DEV_RESULT_UNCONFIGURED;
2056 cdm->matches[j].result.device_result.flags =
2061 * If the user isn't interested in peripherals, don't descend
2062 * the tree any further.
2064 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2068 * If there is a peripheral list generation recorded, make sure
2069 * it hasn't changed.
2071 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2072 && (device->target->bus == cdm->pos.cookie.bus)
2073 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2074 && (device->target == cdm->pos.cookie.target)
2075 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2076 && (device == cdm->pos.cookie.device)
2077 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2078 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2079 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2080 device->generation)){
2081 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2085 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2086 && (cdm->pos.cookie.bus == device->target->bus)
2087 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2088 && (cdm->pos.cookie.target == device->target)
2089 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2090 && (cdm->pos.cookie.device == device)
2091 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2092 && (cdm->pos.cookie.periph != NULL))
2093 return(xptperiphtraverse(device,
2094 (struct cam_periph *)cdm->pos.cookie.periph,
2095 xptedtperiphfunc, arg));
2097 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2101 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2103 struct ccb_dev_match *cdm;
2104 dev_match_ret retval;
2106 cdm = (struct ccb_dev_match *)arg;
2108 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2110 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2111 cdm->status = CAM_DEV_MATCH_ERROR;
2116 * If the copy flag is set, copy this peripheral out.
2118 if (retval & DM_RET_COPY) {
2121 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2122 sizeof(struct dev_match_result));
2125 * If we don't have enough space to put in another
2126 * match result, save our position and tell the
2127 * user there are more devices to check.
2129 if (spaceleft < sizeof(struct dev_match_result)) {
2130 bzero(&cdm->pos, sizeof(cdm->pos));
2131 cdm->pos.position_type =
2132 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2133 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2136 cdm->pos.cookie.bus = periph->path->bus;
2137 cdm->pos.generations[CAM_BUS_GENERATION]=
2139 cdm->pos.cookie.target = periph->path->target;
2140 cdm->pos.generations[CAM_TARGET_GENERATION] =
2141 periph->path->bus->generation;
2142 cdm->pos.cookie.device = periph->path->device;
2143 cdm->pos.generations[CAM_DEV_GENERATION] =
2144 periph->path->target->generation;
2145 cdm->pos.cookie.periph = periph;
2146 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2147 periph->path->device->generation;
2148 cdm->status = CAM_DEV_MATCH_MORE;
2152 j = cdm->num_matches;
2154 cdm->matches[j].type = DEV_MATCH_PERIPH;
2155 cdm->matches[j].result.periph_result.path_id =
2156 periph->path->bus->path_id;
2157 cdm->matches[j].result.periph_result.target_id =
2158 periph->path->target->target_id;
2159 cdm->matches[j].result.periph_result.target_lun =
2160 periph->path->device->lun_id;
2161 cdm->matches[j].result.periph_result.unit_number =
2162 periph->unit_number;
2163 strncpy(cdm->matches[j].result.periph_result.periph_name,
2164 periph->periph_name, DEV_IDLEN);
2171 xptedtmatch(struct ccb_dev_match *cdm)
2175 cdm->num_matches = 0;
2178 * Check the bus list generation. If it has changed, the user
2179 * needs to reset everything and start over.
2181 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2182 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2183 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2184 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2188 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2189 && (cdm->pos.cookie.bus != NULL))
2190 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2191 xptedtbusfunc, cdm);
2193 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2196 * If we get back 0, that means that we had to stop before fully
2197 * traversing the EDT. It also means that one of the subroutines
2198 * has set the status field to the proper value. If we get back 1,
2199 * we've fully traversed the EDT and copied out any matching entries.
2202 cdm->status = CAM_DEV_MATCH_LAST;
2208 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2210 struct ccb_dev_match *cdm;
2212 cdm = (struct ccb_dev_match *)arg;
2214 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2215 && (cdm->pos.cookie.pdrv == pdrv)
2216 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2217 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2218 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2219 (*pdrv)->generation)) {
2220 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2224 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2225 && (cdm->pos.cookie.pdrv == pdrv)
2226 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2227 && (cdm->pos.cookie.periph != NULL))
2228 return(xptpdperiphtraverse(pdrv,
2229 (struct cam_periph *)cdm->pos.cookie.periph,
2230 xptplistperiphfunc, arg));
2232 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2236 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2238 struct ccb_dev_match *cdm;
2239 dev_match_ret retval;
2241 cdm = (struct ccb_dev_match *)arg;
2243 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2245 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2246 cdm->status = CAM_DEV_MATCH_ERROR;
2251 * If the copy flag is set, copy this peripheral out.
2253 if (retval & DM_RET_COPY) {
2256 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2257 sizeof(struct dev_match_result));
2260 * If we don't have enough space to put in another
2261 * match result, save our position and tell the
2262 * user there are more devices to check.
2264 if (spaceleft < sizeof(struct dev_match_result)) {
2265 struct periph_driver **pdrv;
2268 bzero(&cdm->pos, sizeof(cdm->pos));
2269 cdm->pos.position_type =
2270 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2274 * This may look a bit non-sensical, but it is
2275 * actually quite logical. There are very few
2276 * peripheral drivers, and bloating every peripheral
2277 * structure with a pointer back to its parent
2278 * peripheral driver linker set entry would cost
2279 * more in the long run than doing this quick lookup.
2282 (struct periph_driver **)periphdriver_set.ls_items;
2283 *pdrv != NULL; pdrv++) {
2284 if (strcmp((*pdrv)->driver_name,
2285 periph->periph_name) == 0)
2290 cdm->status = CAM_DEV_MATCH_ERROR;
2294 cdm->pos.cookie.pdrv = pdrv;
2296 * The periph generation slot does double duty, as
2297 * does the periph pointer slot. They are used for
2298 * both edt and pdrv lookups and positioning.
2300 cdm->pos.cookie.periph = periph;
2301 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2302 (*pdrv)->generation;
2303 cdm->status = CAM_DEV_MATCH_MORE;
2307 j = cdm->num_matches;
2309 cdm->matches[j].type = DEV_MATCH_PERIPH;
2310 cdm->matches[j].result.periph_result.path_id =
2311 periph->path->bus->path_id;
2314 * The transport layer peripheral doesn't have a target or
2317 if (periph->path->target)
2318 cdm->matches[j].result.periph_result.target_id =
2319 periph->path->target->target_id;
2321 cdm->matches[j].result.periph_result.target_id = -1;
2323 if (periph->path->device)
2324 cdm->matches[j].result.periph_result.target_lun =
2325 periph->path->device->lun_id;
2327 cdm->matches[j].result.periph_result.target_lun = -1;
2329 cdm->matches[j].result.periph_result.unit_number =
2330 periph->unit_number;
2331 strncpy(cdm->matches[j].result.periph_result.periph_name,
2332 periph->periph_name, DEV_IDLEN);
2339 xptperiphlistmatch(struct ccb_dev_match *cdm)
2343 cdm->num_matches = 0;
2346 * At this point in the edt traversal function, we check the bus
2347 * list generation to make sure that no busses have been added or
2348 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2349 * For the peripheral driver list traversal function, however, we
2350 * don't have to worry about new peripheral driver types coming or
2351 * going; they're in a linker set, and therefore can't change
2352 * without a recompile.
2355 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2356 && (cdm->pos.cookie.pdrv != NULL))
2357 ret = xptpdrvtraverse(
2358 (struct periph_driver **)cdm->pos.cookie.pdrv,
2359 xptplistpdrvfunc, cdm);
2361 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2364 * If we get back 0, that means that we had to stop before fully
2365 * traversing the peripheral driver tree. It also means that one of
2366 * the subroutines has set the status field to the proper value. If
2367 * we get back 1, we've fully traversed the EDT and copied out any
2371 cdm->status = CAM_DEV_MATCH_LAST;
2377 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2379 struct cam_eb *bus, *next_bus;
2384 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2387 next_bus = TAILQ_NEXT(bus, links);
2389 retval = tr_func(bus, arg);
2398 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2399 xpt_targetfunc_t *tr_func, void *arg)
2401 struct cam_et *target, *next_target;
2405 for (target = (start_target ? start_target :
2406 TAILQ_FIRST(&bus->et_entries));
2407 target != NULL; target = next_target) {
2409 next_target = TAILQ_NEXT(target, links);
2411 retval = tr_func(target, arg);
2421 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2422 xpt_devicefunc_t *tr_func, void *arg)
2424 struct cam_ed *device, *next_device;
2428 for (device = (start_device ? start_device :
2429 TAILQ_FIRST(&target->ed_entries));
2431 device = next_device) {
2433 next_device = TAILQ_NEXT(device, links);
2435 retval = tr_func(device, arg);
2445 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2446 xpt_periphfunc_t *tr_func, void *arg)
2448 struct cam_periph *periph, *next_periph;
2453 for (periph = (start_periph ? start_periph :
2454 SLIST_FIRST(&device->periphs));
2456 periph = next_periph) {
2458 next_periph = SLIST_NEXT(periph, periph_links);
2460 retval = tr_func(periph, arg);
2469 xptpdrvtraverse(struct periph_driver **start_pdrv,
2470 xpt_pdrvfunc_t *tr_func, void *arg)
2472 struct periph_driver **pdrv;
2478 * We don't traverse the peripheral driver list like we do the
2479 * other lists, because it is a linker set, and therefore cannot be
2480 * changed during runtime. If the peripheral driver list is ever
2481 * re-done to be something other than a linker set (i.e. it can
2482 * change while the system is running), the list traversal should
2483 * be modified to work like the other traversal functions.
2485 for (pdrv = (start_pdrv ? start_pdrv :
2486 (struct periph_driver **)periphdriver_set.ls_items);
2487 *pdrv != NULL; pdrv++) {
2488 retval = tr_func(pdrv, arg);
2498 xptpdperiphtraverse(struct periph_driver **pdrv,
2499 struct cam_periph *start_periph,
2500 xpt_periphfunc_t *tr_func, void *arg)
2502 struct cam_periph *periph, *next_periph;
2507 for (periph = (start_periph ? start_periph :
2508 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2509 periph = next_periph) {
2511 next_periph = TAILQ_NEXT(periph, unit_links);
2513 retval = tr_func(periph, arg);
2521 xptdefbusfunc(struct cam_eb *bus, void *arg)
2523 struct xpt_traverse_config *tr_config;
2525 tr_config = (struct xpt_traverse_config *)arg;
2527 if (tr_config->depth == XPT_DEPTH_BUS) {
2528 xpt_busfunc_t *tr_func;
2530 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2532 return(tr_func(bus, tr_config->tr_arg));
2534 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2538 xptdeftargetfunc(struct cam_et *target, void *arg)
2540 struct xpt_traverse_config *tr_config;
2542 tr_config = (struct xpt_traverse_config *)arg;
2544 if (tr_config->depth == XPT_DEPTH_TARGET) {
2545 xpt_targetfunc_t *tr_func;
2547 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2549 return(tr_func(target, tr_config->tr_arg));
2551 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2555 xptdefdevicefunc(struct cam_ed *device, void *arg)
2557 struct xpt_traverse_config *tr_config;
2559 tr_config = (struct xpt_traverse_config *)arg;
2561 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2562 xpt_devicefunc_t *tr_func;
2564 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2566 return(tr_func(device, tr_config->tr_arg));
2568 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2572 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2574 struct xpt_traverse_config *tr_config;
2575 xpt_periphfunc_t *tr_func;
2577 tr_config = (struct xpt_traverse_config *)arg;
2579 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2582 * Unlike the other default functions, we don't check for depth
2583 * here. The peripheral driver level is the last level in the EDT,
2584 * so if we're here, we should execute the function in question.
2586 return(tr_func(periph, tr_config->tr_arg));
2590 * Execute the given function for every bus in the EDT.
2593 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2595 struct xpt_traverse_config tr_config;
2597 tr_config.depth = XPT_DEPTH_BUS;
2598 tr_config.tr_func = tr_func;
2599 tr_config.tr_arg = arg;
2601 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2606 * Execute the given function for every target in the EDT.
2609 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2611 struct xpt_traverse_config tr_config;
2613 tr_config.depth = XPT_DEPTH_TARGET;
2614 tr_config.tr_func = tr_func;
2615 tr_config.tr_arg = arg;
2617 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2619 #endif /* notusedyet */
2622 * Execute the given function for every device in the EDT.
2625 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2627 struct xpt_traverse_config tr_config;
2629 tr_config.depth = XPT_DEPTH_DEVICE;
2630 tr_config.tr_func = tr_func;
2631 tr_config.tr_arg = arg;
2633 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2638 * Execute the given function for every peripheral in the EDT.
2641 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2643 struct xpt_traverse_config tr_config;
2645 tr_config.depth = XPT_DEPTH_PERIPH;
2646 tr_config.tr_func = tr_func;
2647 tr_config.tr_arg = arg;
2649 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2651 #endif /* notusedyet */
2654 xptsetasyncfunc(struct cam_ed *device, void *arg)
2656 struct cam_path path;
2657 struct ccb_getdev cgd;
2658 struct async_node *cur_entry;
2660 cur_entry = (struct async_node *)arg;
2663 * Don't report unconfigured devices (Wildcard devs,
2664 * devices only for target mode, device instances
2665 * that have been invalidated but are waiting for
2666 * their last reference count to be released).
2668 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2671 xpt_compile_path(&path,
2673 device->target->bus->path_id,
2674 device->target->target_id,
2676 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2677 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2678 xpt_action((union ccb *)&cgd);
2679 cur_entry->callback(cur_entry->callback_arg,
2682 xpt_release_path(&path);
2688 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2690 struct cam_path path;
2691 struct ccb_pathinq cpi;
2692 struct async_node *cur_entry;
2694 cur_entry = (struct async_node *)arg;
2696 xpt_compile_path(&path, /*periph*/NULL,
2698 CAM_TARGET_WILDCARD,
2700 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2701 cpi.ccb_h.func_code = XPT_PATH_INQ;
2702 xpt_action((union ccb *)&cpi);
2703 cur_entry->callback(cur_entry->callback_arg,
2706 xpt_release_path(&path);
2712 xpt_action(union ccb *start_ccb)
2716 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2718 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2720 iopl = splsoftcam();
2721 switch (start_ccb->ccb_h.func_code) {
2725 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2726 struct cam_path *path;
2728 path = start_ccb->ccb_h.path;
2732 * For the sake of compatibility with SCSI-1
2733 * devices that may not understand the identify
2734 * message, we include lun information in the
2735 * second byte of all commands. SCSI-1 specifies
2736 * that luns are a 3 bit value and reserves only 3
2737 * bits for lun information in the CDB. Later
2738 * revisions of the SCSI spec allow for more than 8
2739 * luns, but have deprecated lun information in the
2740 * CDB. So, if the lun won't fit, we must omit.
2742 * Also be aware that during initial probing for devices,
2743 * the inquiry information is unknown but initialized to 0.
2744 * This means that this code will be exercised while probing
2745 * devices with an ANSI revision greater than 2.
2747 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2748 && start_ccb->ccb_h.target_lun < 8
2749 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2751 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2752 start_ccb->ccb_h.target_lun << 5;
2754 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2755 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2756 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2757 &path->device->inq_data),
2758 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2759 cdb_str, sizeof(cdb_str))));
2763 case XPT_CONT_TARGET_IO:
2764 start_ccb->csio.sense_resid = 0;
2765 start_ccb->csio.resid = 0;
2770 struct cam_path *path;
2774 path = start_ccb->ccb_h.path;
2777 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2778 if (path->device->qfrozen_cnt == 0)
2779 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2784 xpt_run_dev_sendq(path->bus);
2787 case XPT_SET_TRAN_SETTINGS:
2789 xpt_set_transfer_settings(&start_ccb->cts,
2790 start_ccb->ccb_h.path->device,
2791 /*async_update*/FALSE);
2794 case XPT_CALC_GEOMETRY:
2796 struct cam_sim *sim;
2798 /* Filter out garbage */
2799 if (start_ccb->ccg.block_size == 0
2800 || start_ccb->ccg.volume_size == 0) {
2801 start_ccb->ccg.cylinders = 0;
2802 start_ccb->ccg.heads = 0;
2803 start_ccb->ccg.secs_per_track = 0;
2804 start_ccb->ccb_h.status = CAM_REQ_CMP;
2809 * In a PC-98 system, geometry translation depens on
2810 * the "real" device geometry obtained from mode page 4.
2811 * SCSI geometry translation is performed in the
2812 * initialization routine of the SCSI BIOS and the result
2813 * stored in host memory. If the translation is available
2814 * in host memory, use it. If not, rely on the default
2815 * translation the device driver performs.
2817 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2818 start_ccb->ccb_h.status = CAM_REQ_CMP;
2822 sim = start_ccb->ccb_h.path->bus->sim;
2823 (*(sim->sim_action))(sim, start_ccb);
2828 union ccb* abort_ccb;
2831 abort_ccb = start_ccb->cab.abort_ccb;
2832 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2834 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2835 struct cam_ccbq *ccbq;
2837 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2838 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2839 abort_ccb->ccb_h.status =
2840 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2841 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2843 xpt_done(abort_ccb);
2845 start_ccb->ccb_h.status = CAM_REQ_CMP;
2848 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2849 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2851 * We've caught this ccb en route to
2852 * the SIM. Flag it for abort and the
2853 * SIM will do so just before starting
2854 * real work on the CCB.
2856 abort_ccb->ccb_h.status =
2857 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2858 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2859 start_ccb->ccb_h.status = CAM_REQ_CMP;
2863 if (XPT_FC_IS_QUEUED(abort_ccb)
2864 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2866 * It's already completed but waiting
2867 * for our SWI to get to it.
2869 start_ccb->ccb_h.status = CAM_UA_ABORT;
2873 * If we weren't able to take care of the abort request
2874 * in the XPT, pass the request down to the SIM for processing.
2878 case XPT_ACCEPT_TARGET_IO:
2880 case XPT_IMMED_NOTIFY:
2881 case XPT_NOTIFY_ACK:
2882 case XPT_GET_TRAN_SETTINGS:
2885 struct cam_sim *sim;
2887 sim = start_ccb->ccb_h.path->bus->sim;
2888 (*(sim->sim_action))(sim, start_ccb);
2893 struct cam_sim *sim;
2895 sim = start_ccb->ccb_h.path->bus->sim;
2896 (*(sim->sim_action))(sim, start_ccb);
2899 case XPT_PATH_STATS:
2900 start_ccb->cpis.last_reset =
2901 start_ccb->ccb_h.path->bus->last_reset;
2902 start_ccb->ccb_h.status = CAM_REQ_CMP;
2909 dev = start_ccb->ccb_h.path->device;
2911 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2912 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2914 struct ccb_getdev *cgd;
2918 cgd = &start_ccb->cgd;
2919 bus = cgd->ccb_h.path->bus;
2920 tar = cgd->ccb_h.path->target;
2921 cgd->inq_data = dev->inq_data;
2922 cgd->pd_type = SID_TYPE(&dev->inq_data);
2923 #ifndef GARBAGE_COLLECT
2924 cgd->dev_openings = dev->ccbq.dev_openings;
2925 cgd->dev_active = dev->ccbq.dev_active;
2926 cgd->devq_openings = dev->ccbq.devq_openings;
2927 cgd->devq_queued = dev->ccbq.queue.entries;
2928 cgd->held = dev->ccbq.held;
2929 cgd->maxtags = dev->quirk->maxtags;
2930 cgd->mintags = dev->quirk->mintags;
2932 cgd->ccb_h.status = CAM_REQ_CMP;
2933 cgd->serial_num_len = dev->serial_num_len;
2934 if ((dev->serial_num_len > 0)
2935 && (dev->serial_num != NULL))
2936 bcopy(dev->serial_num, cgd->serial_num,
2937 dev->serial_num_len);
2942 case XPT_GDEV_STATS:
2947 dev = start_ccb->ccb_h.path->device;
2949 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2950 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2952 struct ccb_getdevstats *cgds;
2956 cgds = &start_ccb->cgds;
2957 bus = cgds->ccb_h.path->bus;
2958 tar = cgds->ccb_h.path->target;
2959 cgds->dev_openings = dev->ccbq.dev_openings;
2960 cgds->dev_active = dev->ccbq.dev_active;
2961 cgds->devq_openings = dev->ccbq.devq_openings;
2962 cgds->devq_queued = dev->ccbq.queue.entries;
2963 cgds->held = dev->ccbq.held;
2964 cgds->last_reset = tar->last_reset;
2965 cgds->maxtags = dev->quirk->maxtags;
2966 cgds->mintags = dev->quirk->mintags;
2967 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2968 cgds->last_reset = bus->last_reset;
2969 cgds->ccb_h.status = CAM_REQ_CMP;
2976 struct cam_periph *nperiph;
2977 struct periph_list *periph_head;
2978 struct ccb_getdevlist *cgdl;
2981 struct cam_ed *device;
2988 * Don't want anyone mucking with our data.
2991 device = start_ccb->ccb_h.path->device;
2992 periph_head = &device->periphs;
2993 cgdl = &start_ccb->cgdl;
2996 * Check and see if the list has changed since the user
2997 * last requested a list member. If so, tell them that the
2998 * list has changed, and therefore they need to start over
2999 * from the beginning.
3001 if ((cgdl->index != 0) &&
3002 (cgdl->generation != device->generation)) {
3003 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3009 * Traverse the list of peripherals and attempt to find
3010 * the requested peripheral.
3012 for (nperiph = periph_head->slh_first, i = 0;
3013 (nperiph != NULL) && (i <= cgdl->index);
3014 nperiph = nperiph->periph_links.sle_next, i++) {
3015 if (i == cgdl->index) {
3016 strncpy(cgdl->periph_name,
3017 nperiph->periph_name,
3019 cgdl->unit_number = nperiph->unit_number;
3024 cgdl->status = CAM_GDEVLIST_ERROR;
3029 if (nperiph == NULL)
3030 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3032 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3035 cgdl->generation = device->generation;
3038 cgdl->ccb_h.status = CAM_REQ_CMP;
3044 dev_pos_type position_type;
3045 struct ccb_dev_match *cdm;
3048 cdm = &start_ccb->cdm;
3051 * Prevent EDT changes while we traverse it.
3055 * There are two ways of getting at information in the EDT.
3056 * The first way is via the primary EDT tree. It starts
3057 * with a list of busses, then a list of targets on a bus,
3058 * then devices/luns on a target, and then peripherals on a
3059 * device/lun. The "other" way is by the peripheral driver
3060 * lists. The peripheral driver lists are organized by
3061 * peripheral driver. (obviously) So it makes sense to
3062 * use the peripheral driver list if the user is looking
3063 * for something like "da1", or all "da" devices. If the
3064 * user is looking for something on a particular bus/target
3065 * or lun, it's generally better to go through the EDT tree.
3068 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3069 position_type = cdm->pos.position_type;
3073 position_type = CAM_DEV_POS_NONE;
3075 for (i = 0; i < cdm->num_patterns; i++) {
3076 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3077 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3078 position_type = CAM_DEV_POS_EDT;
3083 if (cdm->num_patterns == 0)
3084 position_type = CAM_DEV_POS_EDT;
3085 else if (position_type == CAM_DEV_POS_NONE)
3086 position_type = CAM_DEV_POS_PDRV;
3089 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3090 case CAM_DEV_POS_EDT:
3091 ret = xptedtmatch(cdm);
3093 case CAM_DEV_POS_PDRV:
3094 ret = xptperiphlistmatch(cdm);
3097 cdm->status = CAM_DEV_MATCH_ERROR;
3103 if (cdm->status == CAM_DEV_MATCH_ERROR)
3104 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3106 start_ccb->ccb_h.status = CAM_REQ_CMP;
3112 struct ccb_setasync *csa;
3113 struct async_node *cur_entry;
3114 struct async_list *async_head;
3118 csa = &start_ccb->csa;
3119 added = csa->event_enable;
3120 async_head = &csa->ccb_h.path->device->asyncs;
3123 * If there is already an entry for us, simply
3127 cur_entry = SLIST_FIRST(async_head);
3128 while (cur_entry != NULL) {
3129 if ((cur_entry->callback_arg == csa->callback_arg)
3130 && (cur_entry->callback == csa->callback))
3132 cur_entry = SLIST_NEXT(cur_entry, links);
3135 if (cur_entry != NULL) {
3137 * If the request has no flags set,
3140 added &= ~cur_entry->event_enable;
3141 if (csa->event_enable == 0) {
3142 SLIST_REMOVE(async_head, cur_entry,
3144 csa->ccb_h.path->device->refcount--;
3145 free(cur_entry, M_DEVBUF);
3147 cur_entry->event_enable = csa->event_enable;
3150 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3152 if (cur_entry == NULL) {
3154 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3157 cur_entry->callback_arg = csa->callback_arg;
3158 cur_entry->callback = csa->callback;
3159 cur_entry->event_enable = csa->event_enable;
3160 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3161 csa->ccb_h.path->device->refcount++;
3164 if ((added & AC_FOUND_DEVICE) != 0) {
3166 * Get this peripheral up to date with all
3167 * the currently existing devices.
3169 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3171 if ((added & AC_PATH_REGISTERED) != 0) {
3173 * Get this peripheral up to date with all
3174 * the currently existing busses.
3176 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3179 start_ccb->ccb_h.status = CAM_REQ_CMP;
3184 struct ccb_relsim *crs;
3188 crs = &start_ccb->crs;
3189 dev = crs->ccb_h.path->device;
3192 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3198 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3200 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3202 /* Don't ever go below one opening */
3203 if (crs->openings > 0) {
3204 xpt_dev_ccbq_resize(crs->ccb_h.path,
3208 xpt_print_path(crs->ccb_h.path);
3209 printf("tagged openings "
3217 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3219 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3222 * Just extend the old timeout and decrement
3223 * the freeze count so that a single timeout
3224 * is sufficient for releasing the queue.
3226 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3227 untimeout(xpt_release_devq_timeout,
3228 dev, dev->c_handle);
3231 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3235 timeout(xpt_release_devq_timeout,
3237 (crs->release_timeout * hz) / 1000);
3239 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3243 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3245 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3247 * Decrement the freeze count so that a single
3248 * completion is still sufficient to unfreeze
3251 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3254 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3255 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3259 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3261 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3262 || (dev->ccbq.dev_active == 0)) {
3264 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3267 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3268 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3273 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3275 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3278 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3279 start_ccb->ccb_h.status = CAM_REQ_CMP;
3283 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3286 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3287 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3295 #ifdef CAM_DEBUG_DELAY
3296 cam_debug_delay = CAM_DEBUG_DELAY;
3298 cam_dflags = start_ccb->cdbg.flags;
3299 if (cam_dpath != NULL) {
3300 xpt_free_path(cam_dpath);
3304 if (cam_dflags != CAM_DEBUG_NONE) {
3305 if (xpt_create_path(&cam_dpath, xpt_periph,
3306 start_ccb->ccb_h.path_id,
3307 start_ccb->ccb_h.target_id,
3308 start_ccb->ccb_h.target_lun) !=
3310 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3311 cam_dflags = CAM_DEBUG_NONE;
3313 start_ccb->ccb_h.status = CAM_REQ_CMP;
3314 xpt_print_path(cam_dpath);
3315 printf("debugging flags now %x\n", cam_dflags);
3319 start_ccb->ccb_h.status = CAM_REQ_CMP;
3322 #else /* !CAMDEBUG */
3323 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3324 #endif /* CAMDEBUG */
3328 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3329 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3330 start_ccb->ccb_h.status = CAM_REQ_CMP;
3337 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3344 xpt_polled_action(union ccb *start_ccb)
3348 struct cam_sim *sim;
3349 struct cam_devq *devq;
3352 timeout = start_ccb->ccb_h.timeout;
3353 sim = start_ccb->ccb_h.path->bus->sim;
3355 dev = start_ccb->ccb_h.path->device;
3360 * Steal an opening so that no other queued requests
3361 * can get it before us while we simulate interrupts.
3363 dev->ccbq.devq_openings--;
3364 dev->ccbq.dev_openings--;
3366 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3367 && (--timeout > 0)) {
3369 (*(sim->sim_poll))(sim);
3374 dev->ccbq.devq_openings++;
3375 dev->ccbq.dev_openings++;
3378 xpt_action(start_ccb);
3379 while(--timeout > 0) {
3380 (*(sim->sim_poll))(sim);
3383 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3390 * XXX Is it worth adding a sim_timeout entry
3391 * point so we can attempt recovery? If
3392 * this is only used for dumps, I don't think
3395 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3398 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3404 * Schedule a peripheral driver to receive a ccb when it's
3405 * target device has space for more transactions.
3408 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3410 struct cam_ed *device;
3414 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3415 device = perph->path->device;
3417 if (periph_is_queued(perph)) {
3418 /* Simply reorder based on new priority */
3419 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3420 (" change priority to %d\n", new_priority));
3421 if (new_priority < perph->pinfo.priority) {
3422 camq_change_priority(&device->drvq,
3428 /* New entry on the queue */
3429 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3430 (" added periph to queue\n"));
3431 perph->pinfo.priority = new_priority;
3432 perph->pinfo.generation = ++device->drvq.generation;
3433 camq_insert(&device->drvq, &perph->pinfo);
3434 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3438 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3439 (" calling xpt_run_devq\n"));
3440 xpt_run_dev_allocq(perph->path->bus);
3446 * Schedule a device to run on a given queue.
3447 * If the device was inserted as a new entry on the queue,
3448 * return 1 meaning the device queue should be run. If we
3449 * were already queued, implying someone else has already
3450 * started the queue, return 0 so the caller doesn't attempt
3451 * to run the queue. Must be run at either splsoftcam
3452 * (or splcam since that encompases splsoftcam).
3455 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3456 u_int32_t new_priority)
3459 u_int32_t old_priority;
3461 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3463 old_priority = pinfo->priority;
3466 * Are we already queued?
3468 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3469 /* Simply reorder based on new priority */
3470 if (new_priority < old_priority) {
3471 camq_change_priority(queue, pinfo->index,
3473 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3474 ("changed priority to %d\n",
3479 /* New entry on the queue */
3480 if (new_priority < old_priority)
3481 pinfo->priority = new_priority;
3483 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3484 ("Inserting onto queue\n"));
3485 pinfo->generation = ++queue->generation;
3486 camq_insert(queue, pinfo);
3493 xpt_run_dev_allocq(struct cam_eb *bus)
3495 struct cam_devq *devq;
3498 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3499 devq = bus->sim->devq;
3501 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3502 (" qfrozen_cnt == 0x%x, entries == %d, "
3503 "openings == %d, active == %d\n",
3504 devq->alloc_queue.qfrozen_cnt,
3505 devq->alloc_queue.entries,
3506 devq->alloc_openings,
3507 devq->alloc_active));
3510 devq->alloc_queue.qfrozen_cnt++;
3511 while ((devq->alloc_queue.entries > 0)
3512 && (devq->alloc_openings > 0)
3513 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3514 struct cam_ed_qinfo *qinfo;
3515 struct cam_ed *device;
3516 union ccb *work_ccb;
3517 struct cam_periph *drv;
3520 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3522 device = qinfo->device;
3524 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3525 ("running device %p\n", device));
3527 drvq = &device->drvq;
3530 if (drvq->entries <= 0) {
3531 panic("xpt_run_dev_allocq: "
3532 "Device on queue without any work to do");
3535 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3536 devq->alloc_openings--;
3537 devq->alloc_active++;
3538 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3540 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3541 drv->pinfo.priority);
3542 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3543 ("calling periph start\n"));
3544 drv->periph_start(drv, work_ccb);
3547 * Malloc failure in alloc_ccb
3550 * XXX add us to a list to be run from free_ccb
3551 * if we don't have any ccbs active on this
3552 * device queue otherwise we may never get run
3558 /* Raise IPL for possible insertion and test at top of loop */
3561 if (drvq->entries > 0) {
3562 /* We have more work. Attempt to reschedule */
3563 xpt_schedule_dev_allocq(bus, device);
3566 devq->alloc_queue.qfrozen_cnt--;
3571 xpt_run_dev_sendq(struct cam_eb *bus)
3573 struct cam_devq *devq;
3576 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3578 devq = bus->sim->devq;
3581 devq->send_queue.qfrozen_cnt++;
3584 while ((devq->send_queue.entries > 0)
3585 && (devq->send_openings > 0)) {
3586 struct cam_ed_qinfo *qinfo;
3587 struct cam_ed *device;
3588 union ccb *work_ccb;
3589 struct cam_sim *sim;
3593 if (devq->send_queue.qfrozen_cnt > 1) {
3598 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3600 device = qinfo->device;
3603 * If the device has been "frozen", don't attempt
3606 if (device->qfrozen_cnt > 0) {
3611 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3612 ("running device %p\n", device));
3614 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3615 if (work_ccb == NULL) {
3616 printf("device on run queue with no ccbs???");
3621 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3623 if (num_highpower <= 0) {
3625 * We got a high power command, but we
3626 * don't have any available slots. Freeze
3627 * the device queue until we have a slot
3630 device->qfrozen_cnt++;
3631 STAILQ_INSERT_TAIL(&highpowerq,
3639 * Consume a high power slot while
3645 devq->active_dev = device;
3646 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3648 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3651 devq->send_openings--;
3652 devq->send_active++;
3654 if (device->ccbq.queue.entries > 0)
3655 xpt_schedule_dev_sendq(bus, device);
3657 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3659 * The client wants to freeze the queue
3660 * after this CCB is sent.
3663 device->qfrozen_cnt++;
3669 if ((device->inq_flags & SID_CmdQue) != 0)
3670 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3673 * Clear this in case of a retried CCB that failed
3674 * due to a rejected tag.
3676 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3679 * Device queues can be shared among multiple sim instances
3680 * that reside on different busses. Use the SIM in the queue
3681 * CCB's path, rather than the one in the bus that was passed
3682 * into this function.
3684 sim = work_ccb->ccb_h.path->bus->sim;
3685 (*(sim->sim_action))(sim, work_ccb);
3688 devq->active_dev = NULL;
3690 /* Raise IPL for possible insertion and test at top of loop */
3695 devq->send_queue.qfrozen_cnt--;
3700 * This function merges stuff from the slave ccb into the master ccb, while
3701 * keeping important fields in the master ccb constant.
3704 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3707 * Pull fields that are valid for peripheral drivers to set
3708 * into the master CCB along with the CCB "payload".
3710 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3711 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3712 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3713 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3714 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3715 sizeof(union ccb) - sizeof(struct ccb_hdr));
3719 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3721 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3722 ccb_h->pinfo.priority = priority;
3724 ccb_h->path_id = path->bus->path_id;
3726 ccb_h->target_id = path->target->target_id;
3728 ccb_h->target_id = CAM_TARGET_WILDCARD;
3730 ccb_h->target_lun = path->device->lun_id;
3731 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3733 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3735 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3739 /* Path manipulation functions */
3741 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3742 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3744 struct cam_path *path;
3747 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3750 status = CAM_RESRC_UNAVAIL;
3753 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3754 if (status != CAM_REQ_CMP) {
3755 free(path, M_DEVBUF);
3758 *new_path_ptr = path;
3763 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3764 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3767 struct cam_et *target;
3768 struct cam_ed *device;
3772 status = CAM_REQ_CMP; /* Completed without error */
3773 target = NULL; /* Wildcarded */
3774 device = NULL; /* Wildcarded */
3777 * We will potentially modify the EDT, so block interrupts
3778 * that may attempt to create cam paths.
3781 bus = xpt_find_bus(path_id);
3783 status = CAM_PATH_INVALID;
3785 target = xpt_find_target(bus, target_id);
3786 if (target == NULL) {
3788 struct cam_et *new_target;
3790 new_target = xpt_alloc_target(bus, target_id);
3791 if (new_target == NULL) {
3792 status = CAM_RESRC_UNAVAIL;
3794 target = new_target;
3797 if (target != NULL) {
3798 device = xpt_find_device(target, lun_id);
3799 if (device == NULL) {
3801 struct cam_ed *new_device;
3803 new_device = xpt_alloc_device(bus,
3806 if (new_device == NULL) {
3807 status = CAM_RESRC_UNAVAIL;
3809 device = new_device;
3817 * Only touch the user's data if we are successful.
3819 if (status == CAM_REQ_CMP) {
3820 new_path->periph = perph;
3821 new_path->bus = bus;
3822 new_path->target = target;
3823 new_path->device = device;
3824 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3827 xpt_release_device(bus, target, device);
3829 xpt_release_target(bus, target);
3831 xpt_release_bus(bus);
3837 xpt_release_path(struct cam_path *path)
3839 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3840 if (path->device != NULL) {
3841 xpt_release_device(path->bus, path->target, path->device);
3842 path->device = NULL;
3844 if (path->target != NULL) {
3845 xpt_release_target(path->bus, path->target);
3846 path->target = NULL;
3848 if (path->bus != NULL) {
3849 xpt_release_bus(path->bus);
3855 xpt_free_path(struct cam_path *path)
3857 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3858 xpt_release_path(path);
3859 free(path, M_DEVBUF);
3864 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3865 * in path1, 2 for match with wildcards in path2.
3868 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3872 if (path1->bus != path2->bus) {
3873 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3875 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3880 if (path1->target != path2->target) {
3881 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3884 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3889 if (path1->device != path2->device) {
3890 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3893 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3902 xpt_print_path(struct cam_path *path)
3905 printf("(nopath): ");
3907 if (path->periph != NULL)
3908 printf("(%s%d:", path->periph->periph_name,
3909 path->periph->unit_number);
3911 printf("(noperiph:");
3913 if (path->bus != NULL)
3914 printf("%s%d:%d:", path->bus->sim->sim_name,
3915 path->bus->sim->unit_number,
3916 path->bus->sim->bus_id);
3920 if (path->target != NULL)
3921 printf("%d:", path->target->target_id);
3925 if (path->device != NULL)
3926 printf("%d): ", path->device->lun_id);
3933 xpt_path_path_id(struct cam_path *path)
3935 return(path->bus->path_id);
3939 xpt_path_target_id(struct cam_path *path)
3941 if (path->target != NULL)
3942 return (path->target->target_id);
3944 return (CAM_TARGET_WILDCARD);
3948 xpt_path_lun_id(struct cam_path *path)
3950 if (path->device != NULL)
3951 return (path->device->lun_id);
3953 return (CAM_LUN_WILDCARD);
3957 xpt_path_sim(struct cam_path *path)
3959 return (path->bus->sim);
3963 xpt_path_periph(struct cam_path *path)
3965 return (path->periph);
3969 * Release a CAM control block for the caller. Remit the cost of the structure
3970 * to the device referenced by the path. If the this device had no 'credits'
3971 * and peripheral drivers have registered async callbacks for this notification
3975 xpt_release_ccb(union ccb *free_ccb)
3978 struct cam_path *path;
3979 struct cam_ed *device;
3982 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3983 path = free_ccb->ccb_h.path;
3984 device = path->device;
3987 cam_ccbq_release_opening(&device->ccbq);
3988 if (xpt_ccb_count > xpt_max_ccbs) {
3989 xpt_free_ccb(free_ccb);
3992 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
3994 bus->sim->devq->alloc_openings++;
3995 bus->sim->devq->alloc_active--;
3996 /* XXX Turn this into an inline function - xpt_run_device?? */
3997 if ((device_is_alloc_queued(device) == 0)
3998 && (device->drvq.entries > 0)) {
3999 xpt_schedule_dev_allocq(bus, device);
4002 if (dev_allocq_is_runnable(bus->sim->devq))
4003 xpt_run_dev_allocq(bus);
4006 /* Functions accessed by SIM drivers */
4009 * A sim structure, listing the SIM entry points and instance
4010 * identification info is passed to xpt_bus_register to hook the SIM
4011 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4012 * for this new bus and places it in the array of busses and assigns
4013 * it a path_id. The path_id may be influenced by "hard wiring"
4014 * information specified by the user. Once interrupt services are
4015 * availible, the bus will be probed.
4018 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4020 static path_id_t buscount;
4021 struct cam_eb *new_bus;
4022 struct ccb_pathinq cpi;
4026 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4027 M_DEVBUF, M_NOWAIT);
4028 if (new_bus == NULL) {
4029 /* Couldn't satisfy request */
4030 return (CAM_RESRC_UNAVAIL);
4033 bzero(new_bus, sizeof(*new_bus));
4035 if (strcmp(sim->sim_name, "xpt") != 0) {
4037 sim->path_id = xptpathid(sim->sim_name, sim->unit_number,
4038 sim->bus_id, &buscount);
4041 new_bus->path_id = sim->path_id;
4043 TAILQ_INIT(&new_bus->et_entries);
4044 timevalclear(&new_bus->last_reset);
4045 new_bus->refcount = 1; /* Held until a bus_deregister event */
4047 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4051 /* Notify interested parties */
4052 if (sim->path_id != CAM_XPT_PATH_ID) {
4053 struct cam_path path;
4055 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4056 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4057 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4058 cpi.ccb_h.func_code = XPT_PATH_INQ;
4059 xpt_action((union ccb *)&cpi);
4060 xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi);
4061 xpt_release_path(&path);
4063 return (CAM_SUCCESS);
4067 xptnextfreebus(path_id_t startbus)
4069 struct cam_sim_config *sim_conf;
4071 sim_conf = cam_sinit;
4072 while (sim_conf->sim_name != NULL) {
4074 if (IS_SPECIFIED(sim_conf->pathid)
4075 && (startbus == sim_conf->pathid)) {
4077 /* Start the search over */
4078 sim_conf = cam_sinit;
4087 xptpathid(const char *sim_name, int sim_unit,
4088 int sim_bus, path_id_t *nextpath)
4090 struct cam_sim_config *sim_conf;
4093 pathid = CAM_XPT_PATH_ID;
4094 for (sim_conf = cam_sinit; sim_conf->sim_name != NULL; sim_conf++) {
4096 if (!IS_SPECIFIED(sim_conf->pathid))
4099 if (!strcmp(sim_name, sim_conf->sim_name)
4100 && (sim_unit == sim_conf->sim_unit)) {
4102 if (IS_SPECIFIED(sim_conf->sim_bus)) {
4103 if (sim_bus == sim_conf->sim_bus) {
4104 pathid = sim_conf->pathid;
4107 } else if (sim_bus == 0) {
4108 /* Unspecified matches bus 0 */
4109 pathid = sim_conf->pathid;
4112 printf("Ambiguous scbus configuration for %s%d "
4113 "bus %d, cannot wire down. The kernel "
4114 "config entry for scbus%d should "
4115 "specify a controller bus.\n"
4116 "Scbus will be assigned dynamically.\n",
4117 sim_name, sim_unit, sim_bus,
4124 if (pathid == CAM_XPT_PATH_ID) {
4125 pathid = xptnextfreebus(*nextpath);
4126 *nextpath = pathid + 1;
4132 xpt_bus_deregister(path_id)
4136 return (CAM_SUCCESS);
4140 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4143 struct cam_et *target, *next_target;
4144 struct cam_ed *device, *next_device;
4147 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4150 * Most async events come from a CAM interrupt context. In
4151 * a few cases, the error recovery code at the peripheral layer,
4152 * which may run from our SWI or a process context, may signal
4153 * deferred events with a call to xpt_async. Ensure async
4154 * notifications are serialized by blocking cam interrupts.
4160 if (async_code == AC_BUS_RESET) {
4164 /* Update our notion of when the last reset occurred */
4165 microtime(&bus->last_reset);
4169 for (target = TAILQ_FIRST(&bus->et_entries);
4171 target = next_target) {
4173 next_target = TAILQ_NEXT(target, links);
4175 if (path->target != target
4176 && path->target->target_id != CAM_TARGET_WILDCARD)
4179 if (async_code == AC_SENT_BDR) {
4182 /* Update our notion of when the last reset occurred */
4184 microtime(&path->target->last_reset);
4188 for (device = TAILQ_FIRST(&target->ed_entries);
4190 device = next_device) {
4192 struct cam_path newpath;
4194 next_device = TAILQ_NEXT(device, links);
4196 if (path->device != device
4197 && path->device->lun_id != CAM_LUN_WILDCARD)
4201 * We need our own path with wildcards expanded to
4202 * handle certain types of events.
4204 if ((async_code == AC_SENT_BDR)
4205 || (async_code == AC_BUS_RESET)
4206 || (async_code == AC_INQ_CHANGED))
4207 status = xpt_compile_path(&newpath, NULL,
4212 status = CAM_REQ_CMP_ERR;
4214 if (status == CAM_REQ_CMP) {
4217 * Allow transfer negotiation to occur in a
4218 * tag free environment.
4220 if (async_code == AC_SENT_BDR
4221 || async_code == AC_BUS_RESET)
4222 xpt_toggle_tags(&newpath);
4224 if (async_code == AC_INQ_CHANGED) {
4226 * We've sent a start unit command, or
4227 * something similar to a device that
4228 * may have caused its inquiry data to
4229 * change. So we re-scan the device to
4230 * refresh the inquiry data for it.
4232 xpt_scan_lun(newpath.periph, &newpath,
4233 CAM_EXPECT_INQ_CHANGE,
4236 xpt_release_path(&newpath);
4237 } else if (async_code == AC_LOST_DEVICE) {
4238 device->flags |= CAM_DEV_UNCONFIGURED;
4239 } else if (async_code == AC_TRANSFER_NEG) {
4240 struct ccb_trans_settings *settings;
4243 (struct ccb_trans_settings *)async_arg;
4244 xpt_set_transfer_settings(settings, device,
4245 /*async_update*/TRUE);
4248 xpt_async_bcast(&device->asyncs,
4256 * If this wasn't a fully wildcarded async, tell all
4257 * clients that want all async events.
4259 if (bus != xpt_periph->path->bus)
4260 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4266 xpt_async_bcast(struct async_list *async_head,
4267 u_int32_t async_code,
4268 struct cam_path *path, void *async_arg)
4270 struct async_node *cur_entry;
4272 cur_entry = SLIST_FIRST(async_head);
4273 while (cur_entry != NULL) {
4274 struct async_node *next_entry;
4276 * Grab the next list entry before we call the current
4277 * entry's callback. This is because the callback function
4278 * can delete its async callback entry.
4280 next_entry = SLIST_NEXT(cur_entry, links);
4281 if ((cur_entry->event_enable & async_code) != 0)
4282 cur_entry->callback(cur_entry->callback_arg,
4285 cur_entry = next_entry;
4290 xpt_freeze_devq(struct cam_path *path, u_int count)
4293 struct ccb_hdr *ccbh;
4296 path->device->qfrozen_cnt += count;
4299 * Mark the last CCB in the queue as needing
4300 * to be requeued if the driver hasn't
4301 * changed it's state yet. This fixes a race
4302 * where a ccb is just about to be queued to
4303 * a controller driver when it's interrupt routine
4304 * freezes the queue. To completly close the
4305 * hole, controller drives must check to see
4306 * if a ccb's status is still CAM_REQ_INPROG
4307 * under spl protection just before they queue
4308 * the CCB. See ahc_action/ahc_freeze_devq for
4311 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4312 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4313 ccbh->status = CAM_REQUEUE_REQ;
4315 return (path->device->qfrozen_cnt);
4319 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4321 sim->devq->send_queue.qfrozen_cnt += count;
4322 if (sim->devq->active_dev != NULL) {
4323 struct ccb_hdr *ccbh;
4325 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4327 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4328 ccbh->status = CAM_REQUEUE_REQ;
4330 return (sim->devq->send_queue.qfrozen_cnt);
4334 xpt_release_devq_timeout(void *arg)
4336 struct cam_ed *device;
4338 device = (struct cam_ed *)arg;
4340 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4344 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4346 xpt_release_devq_device(path->device, count, run_queue);
4350 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4358 if (dev->qfrozen_cnt > 0) {
4360 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4361 dev->qfrozen_cnt -= count;
4362 if (dev->qfrozen_cnt == 0) {
4365 * No longer need to wait for a successful
4366 * command completion.
4368 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4371 * Remove any timeouts that might be scheduled
4372 * to release this queue.
4374 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4375 untimeout(xpt_release_devq_timeout, dev,
4377 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4381 * Now that we are unfrozen schedule the
4382 * device so any pending transactions are
4385 if ((dev->ccbq.queue.entries > 0)
4386 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4387 && (run_queue != 0)) {
4394 xpt_run_dev_sendq(dev->target->bus);
4399 xpt_release_simq(struct cam_sim *sim, int run_queue)
4404 sendq = &(sim->devq->send_queue);
4406 if (sendq->qfrozen_cnt > 0) {
4408 sendq->qfrozen_cnt--;
4409 if (sendq->qfrozen_cnt == 0) {
4413 * If there is a timeout scheduled to release this
4414 * sim queue, remove it. The queue frozen count is
4417 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4418 untimeout(xpt_release_simq_timeout, sim,
4420 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4422 bus = xpt_find_bus(sim->path_id);
4427 * Now that we are unfrozen run the send queue.
4429 xpt_run_dev_sendq(bus);
4431 xpt_release_bus(bus);
4439 xpt_release_simq_timeout(void *arg)
4441 struct cam_sim *sim;
4443 sim = (struct cam_sim *)arg;
4444 xpt_release_simq(sim, /* run_queue */ TRUE);
4448 xpt_done(union ccb *done_ccb)
4454 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4455 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4457 * Queue up the request for handling by our SWI handler
4458 * any of the "non-immediate" type of ccbs.
4460 switch (done_ccb->ccb_h.path->periph->type) {
4461 case CAM_PERIPH_BIO:
4462 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4464 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4467 case CAM_PERIPH_NET:
4468 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4470 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4483 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4488 xpt_free_ccb(union ccb *free_ccb)
4490 free(free_ccb, M_DEVBUF);
4495 /* Private XPT functions */
4498 * Get a CAM control block for the caller. Charge the structure to the device
4499 * referenced by the path. If the this device has no 'credits' then the
4500 * device already has the maximum number of outstanding operations under way
4501 * and we return NULL. If we don't have sufficient resources to allocate more
4502 * ccbs, we also return NULL.
4505 xpt_get_ccb(struct cam_ed *device)
4511 if ((new_ccb = (union ccb *)ccb_freeq.slh_first) == NULL) {
4512 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4513 if (new_ccb == NULL) {
4517 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4518 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4522 cam_ccbq_take_opening(&device->ccbq);
4523 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4529 xpt_release_bus(struct cam_eb *bus)
4534 if ((--bus->refcount == 0)
4535 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4536 TAILQ_REMOVE(&xpt_busses, bus, links);
4539 free(bus, M_DEVBUF);
4544 static struct cam_et *
4545 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4547 struct cam_et *target;
4549 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4550 if (target != NULL) {
4551 struct cam_et *cur_target;
4554 target->target_id = target_id;
4555 target->refcount = 1;
4557 * Hold a reference to our parent bus so it
4558 * will not go away before we do.
4561 TAILQ_INIT(&target->ed_entries);
4562 timevalclear(&target->last_reset);
4564 /* Insertion sort into our bus's target list */
4565 cur_target = TAILQ_FIRST(&bus->et_entries);
4566 while (cur_target != NULL && cur_target->target_id < target_id)
4567 cur_target = TAILQ_NEXT(cur_target, links);
4569 if (cur_target != NULL) {
4570 TAILQ_INSERT_BEFORE(cur_target, target, links);
4572 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4580 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4585 if ((--target->refcount == 0)
4586 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4587 TAILQ_REMOVE(&bus->et_entries, target, links);
4590 free(target, M_DEVBUF);
4591 xpt_release_bus(bus);
4596 static struct cam_ed *
4597 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4599 struct cam_ed *device;
4600 struct cam_devq *devq;
4603 /* Make space for us in the device queue on our bus */
4604 devq = bus->sim->devq;
4605 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4607 if (status != CAM_REQ_CMP) {
4610 device = (struct cam_ed *)malloc(sizeof(*device),
4611 M_DEVBUF, M_NOWAIT);
4614 if (device != NULL) {
4615 struct cam_ed *cur_device;
4617 bzero(device, sizeof(*device));
4619 SLIST_INIT(&device->asyncs);
4620 SLIST_INIT(&device->periphs);
4621 callout_handle_init(&device->c_handle);
4622 device->refcount = 1;
4623 device->flags |= CAM_DEV_UNCONFIGURED;
4625 * Take the default quirk entry until we have inquiry
4626 * data and can determine a better quirk to use.
4628 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4630 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4631 device->alloc_ccb_entry.device = device;
4632 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4633 device->send_ccb_entry.device = device;
4635 device->target = target;
4637 * Hold a reference to our parent target so it
4638 * will not go away before we do.
4642 device->lun_id = lun_id;
4644 /* Initialize our queues */
4645 if (camq_init(&device->drvq, 0) != 0) {
4646 free(device, M_DEVBUF);
4650 if (cam_ccbq_init(&device->ccbq,
4651 bus->sim->max_dev_openings) != 0) {
4652 camq_fini(&device->drvq);
4653 free(device, M_DEVBUF);
4657 * XXX should be limited by number of CCBs this bus can
4660 xpt_max_ccbs += device->ccbq.devq_openings;
4661 /* Insertion sort into our target's device list */
4662 cur_device = TAILQ_FIRST(&target->ed_entries);
4663 while (cur_device != NULL && cur_device->lun_id < lun_id)
4664 cur_device = TAILQ_NEXT(cur_device, links);
4665 if (cur_device != NULL) {
4666 TAILQ_INSERT_BEFORE(cur_device, device, links);
4668 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4670 target->generation++;
4676 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4677 struct cam_ed *device)
4682 if ((--device->refcount == 0)
4683 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4684 struct cam_devq *devq;
4686 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4687 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4688 panic("Removing device while still queued for ccbs");
4690 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4691 untimeout(xpt_release_devq_timeout, device,
4694 TAILQ_REMOVE(&target->ed_entries, device,links);
4695 target->generation++;
4696 xpt_max_ccbs -= device->ccbq.devq_openings;
4697 /* Release our slot in the devq */
4698 devq = bus->sim->devq;
4699 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4701 free(device, M_DEVBUF);
4707 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4717 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4718 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4719 if (result == CAM_REQ_CMP && (diff < 0)) {
4720 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4722 /* Adjust the global limit */
4723 xpt_max_ccbs += diff;
4728 static struct cam_eb *
4729 xpt_find_bus(path_id_t path_id)
4733 for (bus = TAILQ_FIRST(&xpt_busses);
4735 bus = TAILQ_NEXT(bus, links)) {
4736 if (bus->path_id == path_id) {
4744 static struct cam_et *
4745 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4747 struct cam_et *target;
4749 for (target = TAILQ_FIRST(&bus->et_entries);
4751 target = TAILQ_NEXT(target, links)) {
4752 if (target->target_id == target_id) {
4760 static struct cam_ed *
4761 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4763 struct cam_ed *device;
4765 for (device = TAILQ_FIRST(&target->ed_entries);
4767 device = TAILQ_NEXT(device, links)) {
4768 if (device->lun_id == lun_id) {
4777 union ccb *request_ccb;
4778 struct ccb_pathinq *cpi;
4780 } xpt_scan_bus_info;
4783 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
4784 * As the scan progresses, xpt_scan_bus is used as the
4785 * callback on completion function.
4788 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
4790 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4791 ("xpt_scan_bus\n"));
4792 switch (request_ccb->ccb_h.func_code) {
4795 xpt_scan_bus_info *scan_info;
4796 union ccb *work_ccb;
4797 struct cam_path *path;
4802 /* Find out the characteristics of the bus */
4803 work_ccb = xpt_alloc_ccb();
4804 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
4805 request_ccb->ccb_h.pinfo.priority);
4806 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4807 xpt_action(work_ccb);
4808 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4809 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
4810 xpt_free_ccb(work_ccb);
4811 xpt_done(request_ccb);
4815 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
4817 * Can't scan the bus on an adapter that
4818 * cannot perform the initiator role.
4820 request_ccb->ccb_h.status = CAM_REQ_CMP;
4821 xpt_free_ccb(work_ccb);
4822 xpt_done(request_ccb);
4826 /* Save some state for use while we probe for devices */
4827 scan_info = (xpt_scan_bus_info *)
4828 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
4829 scan_info->request_ccb = request_ccb;
4830 scan_info->cpi = &work_ccb->cpi;
4832 /* Cache on our stack so we can work asynchronously */
4833 max_target = scan_info->cpi->max_target;
4834 initiator_id = scan_info->cpi->initiator_id;
4837 * Don't count the initiator if the
4838 * initiator is addressable.
4840 scan_info->pending_count = max_target + 1;
4841 if (initiator_id <= max_target)
4842 scan_info->pending_count--;
4844 for (i = 0; i <= max_target; i++) {
4846 if (i == initiator_id)
4849 status = xpt_create_path(&path, xpt_periph,
4850 request_ccb->ccb_h.path_id,
4852 if (status != CAM_REQ_CMP) {
4853 printf("xpt_scan_bus: xpt_create_path failed"
4854 " with status %#x, bus scan halted\n",
4858 work_ccb = xpt_alloc_ccb();
4859 xpt_setup_ccb(&work_ccb->ccb_h, path,
4860 request_ccb->ccb_h.pinfo.priority);
4861 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
4862 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
4863 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
4864 work_ccb->crcn.flags = request_ccb->crcn.flags;
4866 printf("xpt_scan_bus: probing %d:%d:%d\n",
4867 request_ccb->ccb_h.path_id, i, 0);
4869 xpt_action(work_ccb);
4875 xpt_scan_bus_info *scan_info;
4877 target_id_t target_id;
4880 /* Reuse the same CCB to query if a device was really found */
4881 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
4882 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
4883 request_ccb->ccb_h.pinfo.priority);
4884 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
4886 path_id = request_ccb->ccb_h.path_id;
4887 target_id = request_ccb->ccb_h.target_id;
4888 lun_id = request_ccb->ccb_h.target_lun;
4889 xpt_action(request_ccb);
4892 printf("xpt_scan_bus: got back probe from %d:%d:%d\n",
4893 path_id, target_id, lun_id);
4896 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
4897 struct cam_ed *device;
4898 struct cam_et *target;
4902 * If we already probed lun 0 successfully, or
4903 * we have additional configured luns on this
4904 * target that might have "gone away", go onto
4907 target = request_ccb->ccb_h.path->target;
4909 device = TAILQ_FIRST(&target->ed_entries);
4911 device = TAILQ_NEXT(device, links);
4914 if ((lun_id != 0) || (device != NULL)) {
4915 /* Try the next lun */
4916 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
4917 (device->quirk->quirks & CAM_QUIRK_HILUNS))
4921 struct cam_ed *device;
4923 device = request_ccb->ccb_h.path->device;
4925 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
4926 /* Try the next lun */
4927 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
4928 (device->quirk->quirks & CAM_QUIRK_HILUNS))
4933 xpt_free_path(request_ccb->ccb_h.path);
4936 if ((lun_id == request_ccb->ccb_h.target_lun)
4937 || lun_id > scan_info->cpi->max_lun) {
4940 xpt_free_ccb(request_ccb);
4941 scan_info->pending_count--;
4942 if (scan_info->pending_count == 0) {
4943 xpt_free_ccb((union ccb *)scan_info->cpi);
4944 request_ccb = scan_info->request_ccb;
4945 free(scan_info, M_TEMP);
4946 request_ccb->ccb_h.status = CAM_REQ_CMP;
4947 xpt_done(request_ccb);
4950 /* Try the next device */
4951 struct cam_path *path;
4954 path = request_ccb->ccb_h.path;
4955 status = xpt_create_path(&path, xpt_periph,
4956 path_id, target_id, lun_id);
4957 if (status != CAM_REQ_CMP) {
4958 printf("xpt_scan_bus: xpt_create_path failed "
4959 "with status %#x, halting LUN scan\n",
4961 xpt_free_ccb(request_ccb);
4962 scan_info->pending_count--;
4963 if (scan_info->pending_count == 0) {
4965 (union ccb *)scan_info->cpi);
4966 request_ccb = scan_info->request_ccb;
4967 free(scan_info, M_TEMP);
4968 request_ccb->ccb_h.status = CAM_REQ_CMP;
4969 xpt_done(request_ccb);
4973 xpt_setup_ccb(&request_ccb->ccb_h, path,
4974 request_ccb->ccb_h.pinfo.priority);
4975 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
4976 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
4977 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
4978 request_ccb->crcn.flags =
4979 scan_info->request_ccb->crcn.flags;
4981 xpt_print_path(path);
4982 printf("xpt_scan bus probing\n");
4984 xpt_action(request_ccb);
4998 PROBE_TUR_FOR_NEGOTIATION
5002 PROBE_INQUIRY_CKSUM = 0x01,
5003 PROBE_SERIAL_CKSUM = 0x02,
5004 PROBE_NO_ANNOUNCE = 0x04
5008 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5009 probe_action action;
5010 union ccb saved_ccb;
5013 u_int8_t digest[16];
5017 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5018 cam_flags flags, union ccb *request_ccb)
5020 struct ccb_pathinq cpi;
5022 struct cam_path *new_path;
5023 struct cam_periph *old_periph;
5026 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5027 ("xpt_scan_lun\n"));
5029 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5030 cpi.ccb_h.func_code = XPT_PATH_INQ;
5031 xpt_action((union ccb *)&cpi);
5033 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5034 if (request_ccb != NULL) {
5035 request_ccb->ccb_h.status = cpi.ccb_h.status;
5036 xpt_done(request_ccb);
5041 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5043 * Can't scan the bus on an adapter that
5044 * cannot perform the initiator role.
5046 if (request_ccb != NULL) {
5047 request_ccb->ccb_h.status = CAM_REQ_CMP;
5048 xpt_done(request_ccb);
5053 if (request_ccb == NULL) {
5054 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5055 if (request_ccb == NULL) {
5056 xpt_print_path(path);
5057 printf("xpt_scan_lun: can't allocate CCB, can't "
5061 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5062 if (new_path == NULL) {
5063 xpt_print_path(path);
5064 printf("xpt_scan_lun: can't allocate path, can't "
5066 free(request_ccb, M_TEMP);
5069 status = xpt_compile_path(new_path, xpt_periph,
5071 path->target->target_id,
5072 path->device->lun_id);
5074 if (status != CAM_REQ_CMP) {
5075 xpt_print_path(path);
5076 printf("xpt_scan_lun: can't compile path, can't "
5078 free(request_ccb, M_TEMP);
5079 free(new_path, M_TEMP);
5082 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5083 request_ccb->ccb_h.cbfcnp = xptscandone;
5084 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5085 request_ccb->crcn.flags = flags;
5089 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5092 softc = (probe_softc *)old_periph->softc;
5093 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5096 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5097 probestart, "probe",
5099 request_ccb->ccb_h.path, NULL, 0,
5102 if (status != CAM_REQ_CMP) {
5103 xpt_print_path(path);
5104 printf("xpt_scan_lun: cam_alloc_periph returned an "
5105 "error, can't continue probe\n");
5106 request_ccb->ccb_h.status = status;
5107 xpt_done(request_ccb);
5114 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5116 xpt_release_path(done_ccb->ccb_h.path);
5117 free(done_ccb->ccb_h.path, M_TEMP);
5118 free(done_ccb, M_TEMP);
5122 proberegister(struct cam_periph *periph, void *arg)
5124 union ccb *request_ccb; /* CCB representing the probe request */
5127 request_ccb = (union ccb *)arg;
5128 if (periph == NULL) {
5129 printf("proberegister: periph was NULL!!\n");
5130 return(CAM_REQ_CMP_ERR);
5133 if (request_ccb == NULL) {
5134 printf("proberegister: no probe CCB, can't register device\n");
5135 return(CAM_REQ_CMP_ERR);
5138 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5140 if (softc == NULL) {
5141 printf("proberegister: Unable to probe new device. "
5142 "Unable to allocate softc\n");
5143 return(CAM_REQ_CMP_ERR);
5145 TAILQ_INIT(&softc->request_ccbs);
5146 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5149 periph->softc = softc;
5150 cam_periph_acquire(periph);
5152 * Ensure we've waited at least a bus settle
5153 * delay before attempting to probe the device.
5155 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5157 probeschedule(periph);
5158 return(CAM_REQ_CMP);
5162 probeschedule(struct cam_periph *periph)
5164 struct ccb_pathinq cpi;
5168 softc = (probe_softc *)periph->softc;
5169 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5171 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5172 cpi.ccb_h.func_code = XPT_PATH_INQ;
5173 xpt_action((union ccb *)&cpi);
5176 * If a device has gone away and another device, or the same one,
5177 * is back in the same place, it should have a unit attention
5178 * condition pending. It will not report the unit attention in
5179 * response to an inquiry, which may leave invalid transfer
5180 * negotiations in effect. The TUR will reveal the unit attention
5181 * condition. Only send the TUR for lun 0, since some devices
5182 * will get confused by commands other than inquiry to non-existent
5183 * luns. If you think a device has gone away start your scan from
5184 * lun 0. This will insure that any bogus transfer settings are
5187 * If we haven't seen the device before and the controller supports
5188 * some kind of transfer negotiation, negotiate with the first
5189 * sent command if no bus reset was performed at startup. This
5190 * ensures that the device is not confused by transfer negotiation
5191 * settings left over by loader or BIOS action.
5193 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5194 && (ccb->ccb_h.target_lun == 0)) {
5195 softc->action = PROBE_TUR;
5196 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5197 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5198 proberequestdefaultnegotiation(periph);
5199 softc->action = PROBE_INQUIRY;
5201 softc->action = PROBE_INQUIRY;
5204 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5205 softc->flags |= PROBE_NO_ANNOUNCE;
5207 softc->flags &= ~PROBE_NO_ANNOUNCE;
5209 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5213 probestart(struct cam_periph *periph, union ccb *start_ccb)
5215 /* Probe the device that our peripheral driver points to */
5216 struct ccb_scsiio *csio;
5219 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5221 softc = (probe_softc *)periph->softc;
5222 csio = &start_ccb->csio;
5224 switch (softc->action) {
5226 case PROBE_TUR_FOR_NEGOTIATION:
5228 scsi_test_unit_ready(csio,
5238 struct scsi_inquiry_data *inq_buf;
5240 inq_buf = &periph->path->device->inq_data;
5242 * If the device is currently configured, we calculate an
5243 * MD5 checksum of the inquiry data, and if the serial number
5244 * length is greater than 0, add the serial number data
5245 * into the checksum as well. Once the inquiry and the
5246 * serial number check finish, we attempt to figure out
5247 * whether we still have the same device.
5249 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5251 MD5Init(&softc->context);
5252 MD5Update(&softc->context, (unsigned char *)inq_buf,
5253 sizeof(struct scsi_inquiry_data));
5254 softc->flags |= PROBE_INQUIRY_CKSUM;
5255 if (periph->path->device->serial_num_len > 0) {
5256 MD5Update(&softc->context,
5257 periph->path->device->serial_num,
5258 periph->path->device->serial_num_len);
5259 softc->flags |= PROBE_SERIAL_CKSUM;
5261 MD5Final(softc->digest, &softc->context);
5268 (u_int8_t *)inq_buf,
5273 /*timeout*/60 * 1000);
5276 case PROBE_MODE_SENSE:
5281 mode_buf_len = sizeof(struct scsi_mode_header_6)
5282 + sizeof(struct scsi_mode_blk_desc)
5283 + sizeof(struct scsi_control_page);
5284 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5285 if (mode_buf != NULL) {
5286 scsi_mode_sense(csio,
5291 SMS_PAGE_CTRL_CURRENT,
5292 SMS_CONTROL_MODE_PAGE,
5299 xpt_print_path(periph->path);
5300 printf("Unable to mode sense control page - malloc failure\n");
5301 softc->action = PROBE_SERIAL_NUM;
5304 case PROBE_SERIAL_NUM:
5306 struct scsi_vpd_unit_serial_number *serial_buf;
5307 struct cam_ed* device;
5310 device = periph->path->device;
5311 device->serial_num = NULL;
5312 device->serial_num_len = 0;
5314 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5315 serial_buf = (struct scsi_vpd_unit_serial_number *)
5316 malloc(sizeof(*serial_buf), M_TEMP, M_NOWAIT);
5318 if (serial_buf != NULL) {
5319 bzero(serial_buf, sizeof(*serial_buf));
5324 (u_int8_t *)serial_buf,
5325 sizeof(*serial_buf),
5327 SVPD_UNIT_SERIAL_NUMBER,
5329 /*timeout*/60 * 1000);
5333 * We'll have to do without, let our probedone
5334 * routine finish up for us.
5336 start_ccb->csio.data_ptr = NULL;
5337 probedone(periph, start_ccb);
5341 xpt_action(start_ccb);
5345 proberequestdefaultnegotiation(struct cam_periph *periph)
5347 struct ccb_trans_settings cts;
5349 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5350 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5351 cts.flags = CCB_TRANS_USER_SETTINGS;
5352 xpt_action((union ccb *)&cts);
5353 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5354 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5355 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5356 xpt_action((union ccb *)&cts);
5360 probedone(struct cam_periph *periph, union ccb *done_ccb)
5363 struct cam_path *path;
5366 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5368 softc = (probe_softc *)periph->softc;
5369 path = done_ccb->ccb_h.path;
5370 priority = done_ccb->ccb_h.pinfo.priority;
5372 switch (softc->action) {
5375 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5377 if (cam_periph_error(done_ccb, 0,
5378 SF_NO_PRINT, NULL) == ERESTART)
5380 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5381 /* Don't wedge the queue */
5382 xpt_release_devq(done_ccb->ccb_h.path,
5386 softc->action = PROBE_INQUIRY;
5387 xpt_release_ccb(done_ccb);
5388 xpt_schedule(periph, priority);
5393 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5394 struct scsi_inquiry_data *inq_buf;
5395 u_int8_t periph_qual;
5396 u_int8_t periph_dtype;
5398 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5399 inq_buf = &path->device->inq_data;
5401 periph_qual = SID_QUAL(inq_buf);
5402 periph_dtype = SID_TYPE(inq_buf);
5403 if (periph_dtype != T_NODEVICE) {
5404 switch(periph_qual) {
5405 case SID_QUAL_LU_CONNECTED:
5407 xpt_find_quirk(path->device);
5409 if ((inq_buf->flags & SID_CmdQue) != 0)
5416 path->device->flags &=
5417 ~CAM_DEV_UNCONFIGURED;
5419 xpt_release_ccb(done_ccb);
5420 xpt_schedule(periph, priority);
5427 } else if (cam_periph_error(done_ccb, 0,
5428 done_ccb->ccb_h.target_lun > 0
5429 ? SF_RETRY_UA|SF_QUIET_IR
5431 &softc->saved_ccb) == ERESTART) {
5433 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5434 /* Don't wedge the queue */
5435 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5439 * If we get to this point, we got an error status back
5440 * from the inquiry and the error status doesn't require
5441 * automatically retrying the command. Therefore, the
5442 * inquiry failed. If we had inquiry information before
5443 * for this device, but this latest inquiry command failed,
5444 * the device has probably gone away. If this device isn't
5445 * already marked unconfigured, notify the peripheral
5446 * drivers that this device is no more.
5448 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5449 /* Send the async notification. */
5450 xpt_async(AC_LOST_DEVICE, path, NULL);
5452 xpt_release_ccb(done_ccb);
5455 case PROBE_MODE_SENSE:
5457 struct ccb_scsiio *csio;
5458 struct scsi_mode_header_6 *mode_hdr;
5460 csio = &done_ccb->csio;
5461 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5462 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5463 struct scsi_control_page *page;
5466 offset = ((u_int8_t *)&mode_hdr[1])
5467 + mode_hdr->blk_desc_len;
5468 page = (struct scsi_control_page *)offset;
5469 path->device->queue_flags = page->queue_flags;
5470 } else if (cam_periph_error(done_ccb, 0,
5471 SF_RETRY_UA|SF_NO_PRINT,
5472 &softc->saved_ccb) == ERESTART) {
5474 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5475 /* Don't wedge the queue */
5476 xpt_release_devq(done_ccb->ccb_h.path,
5477 /*count*/1, /*run_queue*/TRUE);
5479 xpt_release_ccb(done_ccb);
5480 free(mode_hdr, M_TEMP);
5481 softc->action = PROBE_SERIAL_NUM;
5482 xpt_schedule(periph, priority);
5485 case PROBE_SERIAL_NUM:
5487 struct ccb_scsiio *csio;
5488 struct scsi_vpd_unit_serial_number *serial_buf;
5495 csio = &done_ccb->csio;
5496 priority = done_ccb->ccb_h.pinfo.priority;
5498 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5500 /* Clean up from previous instance of this device */
5501 if (path->device->serial_num != NULL) {
5502 free(path->device->serial_num, M_DEVBUF);
5503 path->device->serial_num = NULL;
5504 path->device->serial_num_len = 0;
5507 if (serial_buf == NULL) {
5509 * Don't process the command as it was never sent
5511 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5512 && (serial_buf->length > 0)) {
5515 path->device->serial_num =
5516 (u_int8_t *)malloc((serial_buf->length + 1),
5517 M_DEVBUF, M_NOWAIT);
5518 if (path->device->serial_num != NULL) {
5519 bcopy(serial_buf->serial_num,
5520 path->device->serial_num,
5521 serial_buf->length);
5522 path->device->serial_num_len =
5524 path->device->serial_num[serial_buf->length]
5527 } else if (cam_periph_error(done_ccb, 0,
5528 SF_RETRY_UA|SF_NO_PRINT,
5529 &softc->saved_ccb) == ERESTART) {
5531 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5532 /* Don't wedge the queue */
5533 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5538 * Let's see if we have seen this device before.
5540 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5542 u_int8_t digest[16];
5547 (unsigned char *)&path->device->inq_data,
5548 sizeof(struct scsi_inquiry_data));
5551 MD5Update(&context, serial_buf->serial_num,
5552 serial_buf->length);
5554 MD5Final(digest, &context);
5555 if (bcmp(softc->digest, digest, 16) == 0)
5559 * XXX Do we need to do a TUR in order to ensure
5560 * that the device really hasn't changed???
5563 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5564 xpt_async(AC_LOST_DEVICE, path, NULL);
5566 if (serial_buf != NULL)
5567 free(serial_buf, M_TEMP);
5571 * Now that we have all the necessary
5572 * information to safely perform transfer
5573 * negotiations... Controllers don't perform
5574 * any negotiation or tagged queuing until
5575 * after the first XPT_SET_TRAN_SETTINGS ccb is
5576 * received. So, on a new device, just retreive
5577 * the user settings, and set them as the current
5578 * settings to set the device up.
5580 proberequestdefaultnegotiation(periph);
5581 xpt_release_ccb(done_ccb);
5584 * Perform a TUR to allow the controller to
5585 * perform any necessary transfer negotiation.
5587 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5588 xpt_schedule(periph, priority);
5591 xpt_release_ccb(done_ccb);
5594 case PROBE_TUR_FOR_NEGOTIATION:
5595 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5596 /* Don't wedge the queue */
5597 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5601 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5603 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5604 /* Inform the XPT that a new device has been found */
5605 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5606 xpt_action(done_ccb);
5608 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5610 xpt_release_ccb(done_ccb);
5613 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5614 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5615 done_ccb->ccb_h.status = CAM_REQ_CMP;
5617 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5618 cam_periph_invalidate(periph);
5619 cam_periph_release(periph);
5621 probeschedule(periph);
5626 probecleanup(struct cam_periph *periph)
5628 free(periph->softc, M_TEMP);
5632 xpt_find_quirk(struct cam_ed *device)
5636 match = cam_quirkmatch((caddr_t)&device->inq_data,
5637 (caddr_t)xpt_quirk_table,
5638 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5639 sizeof(*xpt_quirk_table), scsi_inquiry_match);
5642 panic("xpt_find_quirk: device didn't match wildcard entry!!");
5644 device->quirk = (struct xpt_quirk_entry *)match;
5648 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
5651 struct cam_sim *sim;
5654 sim = cts->ccb_h.path->bus->sim;
5655 if (async_update == FALSE) {
5656 struct scsi_inquiry_data *inq_data;
5657 struct ccb_pathinq cpi;
5658 struct ccb_trans_settings cur_cts;
5660 if (device == NULL) {
5661 cts->ccb_h.status = CAM_PATH_INVALID;
5662 xpt_done((union ccb *)cts);
5667 * Perform sanity checking against what the
5668 * controller and device can do.
5670 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
5671 cpi.ccb_h.func_code = XPT_PATH_INQ;
5672 xpt_action((union ccb *)&cpi);
5673 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
5674 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5675 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
5676 xpt_action((union ccb *)&cur_cts);
5677 inq_data = &device->inq_data;
5679 /* Fill in any gaps in what the user gave us */
5680 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
5681 cts->sync_period = cur_cts.sync_period;
5682 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
5683 cts->sync_offset = cur_cts.sync_offset;
5684 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
5685 cts->bus_width = cur_cts.bus_width;
5686 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
5687 cts->flags &= ~CCB_TRANS_DISC_ENB;
5688 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
5690 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
5691 cts->flags &= ~CCB_TRANS_TAG_ENB;
5692 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
5694 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5695 && (inq_data->flags & SID_Sync) == 0)
5696 || (cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
5698 cts->sync_period = 0;
5699 cts->sync_offset = 0;
5702 switch (cts->bus_width) {
5703 case MSG_EXT_WDTR_BUS_32_BIT:
5704 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5705 || (inq_data->flags & SID_WBus32) != 0)
5706 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
5708 /* Fall Through to 16-bit */
5709 case MSG_EXT_WDTR_BUS_16_BIT:
5710 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5711 || (inq_data->flags & SID_WBus16) != 0)
5712 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
5713 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5716 /* Fall Through to 8-bit */
5717 default: /* New bus width?? */
5718 case MSG_EXT_WDTR_BUS_8_BIT:
5719 /* All targets can do this */
5720 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5724 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
5726 * Can't tag queue without disconnection.
5728 cts->flags &= ~CCB_TRANS_TAG_ENB;
5729 cts->valid |= CCB_TRANS_TQ_VALID;
5732 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
5733 || (inq_data->flags & SID_CmdQue) == 0
5734 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
5735 || (device->quirk->mintags == 0)) {
5737 * Can't tag on hardware that doesn't support,
5738 * doesn't have it enabled, or has broken tag support.
5740 cts->flags &= ~CCB_TRANS_TAG_ENB;
5745 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0
5746 && (async_update == FALSE)) {
5750 * If we are transitioning from tags to no-tags or
5751 * vice-versa, we need to carefully freeze and restart
5752 * the queue so that we don't overlap tagged and non-tagged
5753 * commands. We also temporarily stop tags if there is
5754 * a change in transfer negotiation settings to allow
5755 * "tag-less" negotiation.
5757 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5758 || (device->inq_flags & SID_CmdQue) != 0)
5759 device_tagenb = TRUE;
5761 device_tagenb = FALSE;
5763 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
5764 && device_tagenb == FALSE)
5765 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
5766 && device_tagenb == TRUE)) {
5768 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
5770 * Delay change to use tags until after a
5771 * few commands have gone to this device so
5772 * the controller has time to perform transfer
5773 * negotiations without tagged messages getting
5776 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
5777 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
5779 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
5781 device->inq_flags &= ~SID_CmdQue;
5782 xpt_dev_ccbq_resize(cts->ccb_h.path,
5783 sim->max_dev_openings);
5784 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5785 device->tag_delay_count = 0;
5790 if (async_update == FALSE) {
5792 * If we are currently performing tagged transactions to
5793 * this device and want to change its negotiation parameters,
5794 * go non-tagged for a bit to give the controller a chance to
5795 * negotiate unhampered by tag messages.
5797 if ((device->inq_flags & SID_CmdQue) != 0
5798 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
5799 CCB_TRANS_SYNC_OFFSET_VALID|
5800 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
5801 xpt_toggle_tags(cts->ccb_h.path);
5803 (*(sim->sim_action))(sim, (union ccb *)cts);
5807 struct ccb_relsim crs;
5809 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
5811 crs.ccb_h.func_code = XPT_REL_SIMQ;
5812 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5814 = crs.release_timeout
5817 xpt_action((union ccb *)&crs);
5822 xpt_toggle_tags(struct cam_path *path)
5827 * Give controllers a chance to renegotiate
5828 * before starting tag operations. We
5829 * "toggle" tagged queuing off then on
5830 * which causes the tag enable command delay
5831 * counter to come into effect.
5834 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5835 || ((dev->inq_flags & SID_CmdQue) != 0
5836 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
5837 struct ccb_trans_settings cts;
5839 xpt_setup_ccb(&cts.ccb_h, path, 1);
5841 cts.valid = CCB_TRANS_TQ_VALID;
5842 xpt_set_transfer_settings(&cts, path->device,
5843 /*async_update*/TRUE);
5844 cts.flags = CCB_TRANS_TAG_ENB;
5845 xpt_set_transfer_settings(&cts, path->device,
5846 /*async_update*/TRUE);
5851 xpt_start_tags(struct cam_path *path)
5853 struct ccb_relsim crs;
5854 struct cam_ed *device;
5855 struct cam_sim *sim;
5858 device = path->device;
5859 sim = path->bus->sim;
5860 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5861 xpt_freeze_devq(path, /*count*/1);
5862 device->inq_flags |= SID_CmdQue;
5863 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
5864 xpt_dev_ccbq_resize(path, newopenings);
5865 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
5866 crs.ccb_h.func_code = XPT_REL_SIMQ;
5867 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5869 = crs.release_timeout
5872 xpt_action((union ccb *)&crs);
5875 static int busses_to_config;
5876 static int busses_to_reset;
5879 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
5881 if (bus->path_id != CAM_XPT_PATH_ID) {
5882 struct cam_path path;
5883 struct ccb_pathinq cpi;
5887 xpt_compile_path(&path, NULL, bus->path_id,
5888 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5889 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
5890 cpi.ccb_h.func_code = XPT_PATH_INQ;
5891 xpt_action((union ccb *)&cpi);
5892 can_negotiate = cpi.hba_inquiry;
5893 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
5894 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
5897 xpt_release_path(&path);
5904 xptconfigfunc(struct cam_eb *bus, void *arg)
5906 struct cam_path *path;
5907 union ccb *work_ccb;
5909 if (bus->path_id != CAM_XPT_PATH_ID) {
5913 work_ccb = xpt_alloc_ccb();
5914 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
5915 CAM_TARGET_WILDCARD,
5916 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
5917 printf("xptconfigfunc: xpt_create_path failed with "
5918 "status %#x for bus %d\n", status, bus->path_id);
5919 printf("xptconfigfunc: halting bus configuration\n");
5920 xpt_free_ccb(work_ccb);
5922 xpt_finishconfig(xpt_periph, NULL);
5925 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
5926 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5927 xpt_action(work_ccb);
5928 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5929 printf("xptconfigfunc: CPI failed on bus %d "
5930 "with status %d\n", bus->path_id,
5931 work_ccb->ccb_h.status);
5932 xpt_finishconfig(xpt_periph, work_ccb);
5936 can_negotiate = work_ccb->cpi.hba_inquiry;
5937 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
5938 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
5939 && (can_negotiate != 0)) {
5940 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
5941 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
5942 work_ccb->ccb_h.cbfcnp = NULL;
5943 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
5944 ("Resetting Bus\n"));
5945 xpt_action(work_ccb);
5946 xpt_finishconfig(xpt_periph, work_ccb);
5948 /* Act as though we performed a successful BUS RESET */
5949 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
5950 xpt_finishconfig(xpt_periph, work_ccb);
5958 xpt_config(void *arg)
5960 /* Now that interrupts are enabled, go find our devices */
5963 /* Setup debugging flags and path */
5964 #ifdef CAM_DEBUG_FLAGS
5965 cam_dflags = CAM_DEBUG_FLAGS;
5966 #else /* !CAM_DEBUG_FLAGS */
5967 cam_dflags = CAM_DEBUG_NONE;
5968 #endif /* CAM_DEBUG_FLAGS */
5969 #ifdef CAM_DEBUG_BUS
5970 if (cam_dflags != CAM_DEBUG_NONE) {
5971 if (xpt_create_path(&cam_dpath, xpt_periph,
5972 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5973 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5974 printf("xpt_config: xpt_create_path() failed for debug"
5975 " target %d:%d:%d, debugging disabled\n",
5976 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5977 cam_dflags = CAM_DEBUG_NONE;
5981 #else /* !CAM_DEBUG_BUS */
5983 #endif /* CAM_DEBUG_BUS */
5984 #endif /* CAMDEBUG */
5987 * Scan all installed busses.
5989 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
5991 if (busses_to_config == 0) {
5992 /* Call manually because we don't have any busses */
5993 xpt_finishconfig(xpt_periph, NULL);
5995 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
5996 printf("Waiting %d seconds for SCSI "
5997 "devices to settle\n", SCSI_DELAY/1000);
5999 xpt_for_all_busses(xptconfigfunc, NULL);
6004 * If the given device only has one peripheral attached to it, and if that
6005 * peripheral is the passthrough driver, announce it. This insures that the
6006 * user sees some sort of announcement for every peripheral in their system.
6009 xptpassannouncefunc(struct cam_ed *device, void *arg)
6011 struct cam_periph *periph;
6014 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6015 periph = SLIST_NEXT(periph, periph_links), i++);
6017 periph = SLIST_FIRST(&device->periphs);
6019 && (strncmp(periph->periph_name, "pass", 4) == 0))
6020 xpt_announce_periph(periph, NULL);
6026 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6028 struct periph_driver **p_drv;
6031 if (done_ccb != NULL) {
6032 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6033 ("xpt_finishconfig\n"));
6034 switch(done_ccb->ccb_h.func_code) {
6036 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6037 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6038 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6039 xpt_action(done_ccb);
6045 xpt_free_path(done_ccb->ccb_h.path);
6051 if (busses_to_config == 0) {
6052 /* Register all the peripheral drivers */
6053 /* XXX This will have to change when we have loadable modules */
6054 p_drv = (struct periph_driver **)periphdriver_set.ls_items;
6055 for (i = 0; p_drv[i] != NULL; i++) {
6056 (*p_drv[i]->init)();
6060 * Check for devices with no "standard" peripheral driver
6061 * attached. For any devices like that, announce the
6062 * passthrough driver so the user will see something.
6064 xpt_for_all_devices(xptpassannouncefunc, NULL);
6066 /* Release our hook so that the boot can continue. */
6067 config_intrhook_disestablish(xpt_config_hook);
6068 free(xpt_config_hook, M_TEMP);
6069 xpt_config_hook = NULL;
6071 if (done_ccb != NULL)
6072 xpt_free_ccb(done_ccb);
6076 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6078 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6080 switch (work_ccb->ccb_h.func_code) {
6081 /* Common cases first */
6082 case XPT_PATH_INQ: /* Path routing inquiry */
6084 struct ccb_pathinq *cpi;
6086 cpi = &work_ccb->cpi;
6087 cpi->version_num = 1; /* XXX??? */
6088 cpi->hba_inquiry = 0;
6089 cpi->target_sprt = 0;
6091 cpi->hba_eng_cnt = 0;
6092 cpi->max_target = 0;
6094 cpi->initiator_id = 0;
6095 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6096 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6097 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6098 cpi->unit_number = sim->unit_number;
6099 cpi->bus_id = sim->bus_id;
6100 cpi->base_transfer_speed = 0;
6101 cpi->ccb_h.status = CAM_REQ_CMP;
6106 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6113 * Should only be called by the machine interrupt dispatch routines,
6114 * so put these prototypes here instead of in the header.
6130 camisr(cam_isrq_t *queue)
6133 struct ccb_hdr *ccb_h;
6136 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6139 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6140 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6143 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6148 if (ccb_h->flags & CAM_HIGH_POWER) {
6149 struct highpowerlist *hphead;
6150 struct cam_ed *device;
6151 union ccb *send_ccb;
6153 hphead = &highpowerq;
6155 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6158 * Increment the count since this command is done.
6163 * Any high powered commands queued up?
6165 if (send_ccb != NULL) {
6166 device = send_ccb->ccb_h.path->device;
6168 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6170 xpt_release_devq(send_ccb->ccb_h.path,
6171 /*count*/1, /*runqueue*/TRUE);
6174 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6177 dev = ccb_h->path->device;
6180 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6182 ccb_h->path->bus->sim->devq->send_active--;
6183 ccb_h->path->bus->sim->devq->send_openings++;
6186 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6187 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6188 && (dev->ccbq.dev_active == 0))) {
6190 xpt_release_devq(ccb_h->path, /*count*/1,
6194 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6195 && (--dev->tag_delay_count == 0))
6196 xpt_start_tags(ccb_h->path);
6198 if ((dev->ccbq.queue.entries > 0)
6199 && (dev->qfrozen_cnt == 0)
6200 && (device_is_send_queued(dev) == 0)) {
6201 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6206 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6207 xpt_release_simq(ccb_h->path->bus->sim,
6209 } else if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6210 && (ccb_h->status & CAM_DEV_QFRZN)) {
6211 xpt_release_devq(ccb_h->path, /*count*/1,
6213 ccb_h->status &= ~CAM_DEV_QFRZN;
6215 xpt_run_dev_sendq(ccb_h->path->bus);
6218 /* Call the peripheral driver's callback */
6219 (*ccb_h->cbfcnp)(ccb_h->path->periph,
6220 (union ccb *)ccb_h);
6222 /* Raise IPL for while test */