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
29 * $FreeBSD: src/sys/cam/cam_xpt.c,v 1.80.2.18 2002/12/09 17:31:55 gibbs Exp $
30 * $DragonFly: src/sys/bus/cam/cam_xpt.c,v 1.10 2004/03/12 03:23:13 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
41 #include <sys/devicestat.h>
42 #include <sys/interrupt.h>
46 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
49 #include <machine/clock.h>
50 #include <machine/ipl.h>
54 #include "cam_periph.h"
57 #include "cam_xpt_sim.h"
58 #include "cam_xpt_periph.h"
59 #include "cam_debug.h"
61 #include "scsi/scsi_all.h"
62 #include "scsi/scsi_message.h"
63 #include "scsi/scsi_pass.h"
66 /* Datastructures internal to the xpt layer */
69 * Definition of an async handler callback block. These are used to add
70 * SIMs and peripherals to the async callback lists.
73 SLIST_ENTRY(async_node) links;
74 u_int32_t event_enable; /* Async Event enables */
75 void (*callback)(void *arg, u_int32_t code,
76 struct cam_path *path, void *args);
80 SLIST_HEAD(async_list, async_node);
81 SLIST_HEAD(periph_list, cam_periph);
82 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
85 * This is the maximum number of high powered commands (e.g. start unit)
86 * that can be outstanding at a particular time.
88 #ifndef CAM_MAX_HIGHPOWER
89 #define CAM_MAX_HIGHPOWER 4
92 /* number of high powered commands that can go through right now */
93 static int num_highpower = CAM_MAX_HIGHPOWER;
96 * Structure for queueing a device in a run queue.
97 * There is one run queue for allocating new ccbs,
98 * and another for sending ccbs to the controller.
100 struct cam_ed_qinfo {
102 struct cam_ed *device;
106 * The CAM EDT (Existing Device Table) contains the device information for
107 * all devices for all busses in the system. The table contains a
108 * cam_ed structure for each device on the bus.
111 TAILQ_ENTRY(cam_ed) links;
112 struct cam_ed_qinfo alloc_ccb_entry;
113 struct cam_ed_qinfo send_ccb_entry;
114 struct cam_et *target;
117 * Queue of type drivers wanting to do
118 * work on this device.
120 struct cam_ccbq ccbq; /* Queue of pending ccbs */
121 struct async_list asyncs; /* Async callback info for this B/T/L */
122 struct periph_list periphs; /* All attached devices */
123 u_int generation; /* Generation number */
124 struct cam_periph *owner; /* Peripheral driver's ownership tag */
125 struct xpt_quirk_entry *quirk; /* Oddities about this device */
126 /* Storage for the inquiry data */
127 struct scsi_inquiry_data inq_data;
128 u_int8_t inq_flags; /*
129 * Current settings for inquiry flags.
130 * This allows us to override settings
131 * like disconnection and tagged
132 * queuing for a device.
134 u_int8_t queue_flags; /* Queue flags from the control page */
135 u_int8_t serial_num_len;
136 u_int8_t *serial_num;
137 u_int32_t qfrozen_cnt;
139 #define CAM_DEV_UNCONFIGURED 0x01
140 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
141 #define CAM_DEV_REL_ON_COMPLETE 0x04
142 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
143 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
144 #define CAM_DEV_TAG_AFTER_COUNT 0x20
145 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
146 u_int32_t tag_delay_count;
147 #define CAM_TAG_DELAY_COUNT 5
149 struct callout_handle c_handle;
153 * Each target is represented by an ET (Existing Target). These
154 * entries are created when a target is successfully probed with an
155 * identify, and removed when a device fails to respond after a number
156 * of retries, or a bus rescan finds the device missing.
159 TAILQ_HEAD(, cam_ed) ed_entries;
160 TAILQ_ENTRY(cam_et) links;
162 target_id_t target_id;
165 struct timeval last_reset; /* uptime of last reset */
169 * Each bus is represented by an EB (Existing Bus). These entries
170 * are created by calls to xpt_bus_register and deleted by calls to
171 * xpt_bus_deregister.
174 TAILQ_HEAD(, cam_et) et_entries;
175 TAILQ_ENTRY(cam_eb) links;
178 struct timeval last_reset; /* uptime of last reset */
180 #define CAM_EB_RUNQ_SCHEDULED 0x01
186 struct cam_periph *periph;
188 struct cam_et *target;
189 struct cam_ed *device;
192 struct xpt_quirk_entry {
193 struct scsi_inquiry_pattern inq_pat;
195 #define CAM_QUIRK_NOLUNS 0x01
196 #define CAM_QUIRK_NOSERIAL 0x02
197 #define CAM_QUIRK_HILUNS 0x04
201 #define CAM_SCSI2_MAXLUN 8
209 u_int32_t generation;
212 static const char quantum[] = "QUANTUM";
213 static const char sony[] = "SONY";
214 static const char west_digital[] = "WDIGTL";
215 static const char samsung[] = "SAMSUNG";
216 static const char seagate[] = "SEAGATE";
217 static const char microp[] = "MICROP";
219 static struct xpt_quirk_entry xpt_quirk_table[] =
222 /* Reports QUEUE FULL for temporary resource shortages */
223 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
224 /*quirks*/0, /*mintags*/24, /*maxtags*/32
227 /* Reports QUEUE FULL for temporary resource shortages */
228 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
229 /*quirks*/0, /*mintags*/24, /*maxtags*/32
232 /* Reports QUEUE FULL for temporary resource shortages */
233 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
234 /*quirks*/0, /*mintags*/24, /*maxtags*/32
237 /* Broken tagged queuing drive */
238 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
239 /*quirks*/0, /*mintags*/0, /*maxtags*/0
242 /* Broken tagged queuing drive */
243 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
244 /*quirks*/0, /*mintags*/0, /*maxtags*/0
247 /* Broken tagged queuing drive */
248 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
249 /*quirks*/0, /*mintags*/0, /*maxtags*/0
253 * Unfortunately, the Quantum Atlas III has the same
254 * problem as the Atlas II drives above.
255 * Reported by: "Johan Granlund" <johan@granlund.nu>
257 * For future reference, the drive with the problem was:
258 * QUANTUM QM39100TD-SW N1B0
260 * It's possible that Quantum will fix the problem in later
261 * firmware revisions. If that happens, the quirk entry
262 * will need to be made specific to the firmware revisions
266 /* Reports QUEUE FULL for temporary resource shortages */
267 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
268 /*quirks*/0, /*mintags*/24, /*maxtags*/32
272 * 18 Gig Atlas III, same problem as the 9G version.
273 * Reported by: Andre Albsmeier
274 * <andre.albsmeier@mchp.siemens.de>
276 * For future reference, the drive with the problem was:
277 * QUANTUM QM318000TD-S N491
279 /* Reports QUEUE FULL for temporary resource shortages */
280 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
281 /*quirks*/0, /*mintags*/24, /*maxtags*/32
285 * Broken tagged queuing drive
286 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
287 * and: Martin Renters <martin@tdc.on.ca>
289 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
290 /*quirks*/0, /*mintags*/0, /*maxtags*/0
293 * The Seagate Medalist Pro drives have very poor write
294 * performance with anything more than 2 tags.
296 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
297 * Drive: <SEAGATE ST36530N 1444>
299 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
300 * Drive: <SEAGATE ST34520W 1281>
302 * No one has actually reported that the 9G version
303 * (ST39140*) of the Medalist Pro has the same problem, but
304 * we're assuming that it does because the 4G and 6.5G
305 * versions of the drive are broken.
308 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
309 /*quirks*/0, /*mintags*/2, /*maxtags*/2
312 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
313 /*quirks*/0, /*mintags*/2, /*maxtags*/2
316 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
317 /*quirks*/0, /*mintags*/2, /*maxtags*/2
321 * Slow when tagged queueing is enabled. Write performance
322 * steadily drops off with more and more concurrent
323 * transactions. Best sequential write performance with
324 * tagged queueing turned off and write caching turned on.
327 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
328 * Drive: DCAS-34330 w/ "S65A" firmware.
330 * The drive with the problem had the "S65A" firmware
331 * revision, and has also been reported (by Stephen J.
332 * Roznowski <sjr@home.net>) for a drive with the "S61A"
335 * Although no one has reported problems with the 2 gig
336 * version of the DCAS drive, the assumption is that it
337 * has the same problems as the 4 gig version. Therefore
338 * this quirk entries disables tagged queueing for all
341 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
342 /*quirks*/0, /*mintags*/0, /*maxtags*/0
345 /* Broken tagged queuing drive */
346 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
347 /*quirks*/0, /*mintags*/0, /*maxtags*/0
350 /* Broken tagged queuing drive */
351 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
352 /*quirks*/0, /*mintags*/0, /*maxtags*/0
356 * Broken tagged queuing drive.
358 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
361 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
362 /*quirks*/0, /*mintags*/0, /*maxtags*/0
366 * Slow when tagged queueing is enabled. (1.5MB/sec versus
368 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
369 * Best performance with these drives is achieved with
370 * tagged queueing turned off, and write caching turned on.
372 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
373 /*quirks*/0, /*mintags*/0, /*maxtags*/0
377 * Slow when tagged queueing is enabled. (1.5MB/sec versus
379 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
380 * Best performance with these drives is achieved with
381 * tagged queueing turned off, and write caching turned on.
383 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
384 /*quirks*/0, /*mintags*/0, /*maxtags*/0
388 * Doesn't handle queue full condition correctly,
389 * so we need to limit maxtags to what the device
390 * can handle instead of determining this automatically.
392 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
393 /*quirks*/0, /*mintags*/2, /*maxtags*/32
396 /* Really only one LUN */
397 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
398 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
401 /* I can't believe we need a quirk for DPT volumes. */
402 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
403 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
404 /*mintags*/0, /*maxtags*/255
408 * Many Sony CDROM drives don't like multi-LUN probing.
410 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
411 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
415 * This drive doesn't like multiple LUN probing.
416 * Submitted by: Parag Patel <parag@cgt.com>
418 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
419 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
422 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
423 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
427 * The 8200 doesn't like multi-lun probing, and probably
428 * don't like serial number requests either.
431 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
434 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
438 * Let's try the same as above, but for a drive that says
439 * it's an IPL-6860 but is actually an EXB 8200.
442 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
445 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
449 * These Hitachi drives don't like multi-lun probing.
450 * The PR submitter has a DK319H, but says that the Linux
451 * kernel has a similar work-around for the DK312 and DK314,
452 * so all DK31* drives are quirked here.
454 * Submitted by: Paul Haddad <paul@pth.com>
456 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
457 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
461 * This old revision of the TDC3600 is also SCSI-1, and
462 * hangs upon serial number probing.
465 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
468 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
472 * Would repond to all LUNs if asked for.
475 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
478 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
482 * Would repond to all LUNs if asked for.
485 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
488 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
491 /* Submitted by: Matthew Dodd <winter@jurai.net> */
492 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
493 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
496 /* Submitted by: Matthew Dodd <winter@jurai.net> */
497 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
498 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
501 /* TeraSolutions special settings for TRC-22 RAID */
502 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
503 /*quirks*/0, /*mintags*/55, /*maxtags*/255
506 /* Veritas Storage Appliance */
507 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
508 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
512 * Would respond to all LUNs. Device type and removable
513 * flag are jumper-selectable.
515 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
518 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
521 /* Default tagged queuing parameters for all devices */
523 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
524 /*vendor*/"*", /*product*/"*", /*revision*/"*"
526 /*quirks*/0, /*mintags*/2, /*maxtags*/255
530 static const int xpt_quirk_table_size =
531 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
535 DM_RET_FLAG_MASK = 0x0f,
538 DM_RET_DESCEND = 0x20,
540 DM_RET_ACTION_MASK = 0xf0
548 } xpt_traverse_depth;
550 struct xpt_traverse_config {
551 xpt_traverse_depth depth;
556 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
557 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
558 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
559 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
560 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
562 /* Transport layer configuration information */
563 static struct xpt_softc xsoftc;
565 /* Queues for our software interrupt handler */
566 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
567 static cam_isrq_t cam_bioq;
568 static cam_isrq_t cam_netq;
570 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
571 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
572 static u_int xpt_max_ccbs; /*
573 * Maximum size of ccb pool. Modified as
574 * devices are added/removed or have their
575 * opening counts changed.
577 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
579 struct cam_periph *xpt_periph;
581 static periph_init_t xpt_periph_init;
583 static periph_init_t probe_periph_init;
585 static struct periph_driver xpt_driver =
587 xpt_periph_init, "xpt",
588 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
591 static struct periph_driver probe_driver =
593 probe_periph_init, "probe",
594 TAILQ_HEAD_INITIALIZER(probe_driver.units)
597 DATA_SET(periphdriver_set, xpt_driver);
598 DATA_SET(periphdriver_set, probe_driver);
600 #define XPT_CDEV_MAJOR 104
602 static d_open_t xptopen;
603 static d_close_t xptclose;
604 static d_ioctl_t xptioctl;
606 static struct cdevsw xpt_cdevsw = {
608 /* maj */ XPT_CDEV_MAJOR,
614 /* close */ xptclose,
617 /* ioctl */ xptioctl,
620 /* strategy */ nostrategy,
625 static struct intr_config_hook *xpt_config_hook;
627 /* Registered busses */
628 static TAILQ_HEAD(,cam_eb) xpt_busses;
629 static u_int bus_generation;
631 /* Storage for debugging datastructures */
633 struct cam_path *cam_dpath;
634 u_int32_t cam_dflags;
635 u_int32_t cam_debug_delay;
638 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
639 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
643 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
644 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
645 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
647 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
648 || defined(CAM_DEBUG_LUN)
650 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
651 || !defined(CAM_DEBUG_LUN)
652 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
654 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
655 #else /* !CAMDEBUG */
656 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
657 #endif /* CAMDEBUG */
658 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
660 /* Our boot-time initialization hook */
661 static void xpt_init(void *);
662 SYSINIT(cam, SI_SUB_CONFIGURE, SI_ORDER_SECOND, xpt_init, NULL);
664 static cam_status xpt_compile_path(struct cam_path *new_path,
665 struct cam_periph *perph,
667 target_id_t target_id,
670 static void xpt_release_path(struct cam_path *path);
672 static void xpt_async_bcast(struct async_list *async_head,
673 u_int32_t async_code,
674 struct cam_path *path,
676 static void xpt_dev_async(u_int32_t async_code,
678 struct cam_et *target,
679 struct cam_ed *device,
681 static path_id_t xptnextfreepathid(void);
682 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
683 static union ccb *xpt_get_ccb(struct cam_ed *device);
684 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
685 u_int32_t new_priority);
686 static void xpt_run_dev_allocq(struct cam_eb *bus);
687 static void xpt_run_dev_sendq(struct cam_eb *bus);
688 static timeout_t xpt_release_devq_timeout;
689 static timeout_t xpt_release_simq_timeout;
690 static void xpt_release_bus(struct cam_eb *bus);
691 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
693 static struct cam_et*
694 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
695 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
696 static struct cam_ed*
697 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
699 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
700 struct cam_ed *device);
701 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
702 static struct cam_eb*
703 xpt_find_bus(path_id_t path_id);
704 static struct cam_et*
705 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
706 static struct cam_ed*
707 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
708 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
709 static void xpt_scan_lun(struct cam_periph *periph,
710 struct cam_path *path, cam_flags flags,
712 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
713 static xpt_busfunc_t xptconfigbuscountfunc;
714 static xpt_busfunc_t xptconfigfunc;
715 static void xpt_config(void *arg);
716 static xpt_devicefunc_t xptpassannouncefunc;
717 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
718 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
719 static void xptpoll(struct cam_sim *sim);
720 static inthand2_t swi_camnet;
721 static inthand2_t swi_cambio;
722 static void camisr(cam_isrq_t *queue);
724 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
725 static void xptasync(struct cam_periph *periph,
726 u_int32_t code, cam_path *path);
728 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
729 int num_patterns, struct cam_eb *bus);
730 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
731 int num_patterns, struct cam_ed *device);
732 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
734 struct cam_periph *periph);
735 static xpt_busfunc_t xptedtbusfunc;
736 static xpt_targetfunc_t xptedttargetfunc;
737 static xpt_devicefunc_t xptedtdevicefunc;
738 static xpt_periphfunc_t xptedtperiphfunc;
739 static xpt_pdrvfunc_t xptplistpdrvfunc;
740 static xpt_periphfunc_t xptplistperiphfunc;
741 static int xptedtmatch(struct ccb_dev_match *cdm);
742 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
743 static int xptbustraverse(struct cam_eb *start_bus,
744 xpt_busfunc_t *tr_func, void *arg);
745 static int xpttargettraverse(struct cam_eb *bus,
746 struct cam_et *start_target,
747 xpt_targetfunc_t *tr_func, void *arg);
748 static int xptdevicetraverse(struct cam_et *target,
749 struct cam_ed *start_device,
750 xpt_devicefunc_t *tr_func, void *arg);
751 static int xptperiphtraverse(struct cam_ed *device,
752 struct cam_periph *start_periph,
753 xpt_periphfunc_t *tr_func, void *arg);
754 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
755 xpt_pdrvfunc_t *tr_func, void *arg);
756 static int xptpdperiphtraverse(struct periph_driver **pdrv,
757 struct cam_periph *start_periph,
758 xpt_periphfunc_t *tr_func,
760 static xpt_busfunc_t xptdefbusfunc;
761 static xpt_targetfunc_t xptdeftargetfunc;
762 static xpt_devicefunc_t xptdefdevicefunc;
763 static xpt_periphfunc_t xptdefperiphfunc;
764 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
766 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
769 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
772 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
775 static xpt_devicefunc_t xptsetasyncfunc;
776 static xpt_busfunc_t xptsetasyncbusfunc;
777 static cam_status xptregister(struct cam_periph *periph,
779 static cam_status proberegister(struct cam_periph *periph,
781 static void probeschedule(struct cam_periph *probe_periph);
782 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
783 static void proberequestdefaultnegotiation(struct cam_periph *periph);
784 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
785 static void probecleanup(struct cam_periph *periph);
786 static void xpt_find_quirk(struct cam_ed *device);
787 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
788 struct cam_ed *device,
790 static void xpt_toggle_tags(struct cam_path *path);
791 static void xpt_start_tags(struct cam_path *path);
792 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
794 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
796 static __inline int periph_is_queued(struct cam_periph *periph);
797 static __inline int device_is_alloc_queued(struct cam_ed *device);
798 static __inline int device_is_send_queued(struct cam_ed *device);
799 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
802 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
806 if (dev->ccbq.devq_openings > 0) {
807 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
808 cam_ccbq_resize(&dev->ccbq,
809 dev->ccbq.dev_openings
810 + dev->ccbq.dev_active);
811 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
814 * The priority of a device waiting for CCB resources
815 * is that of the the highest priority peripheral driver
818 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
819 &dev->alloc_ccb_entry.pinfo,
820 CAMQ_GET_HEAD(&dev->drvq)->priority);
829 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
833 if (dev->ccbq.dev_openings > 0) {
835 * The priority of a device waiting for controller
836 * resources is that of the the highest priority CCB
840 xpt_schedule_dev(&bus->sim->devq->send_queue,
841 &dev->send_ccb_entry.pinfo,
842 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
850 periph_is_queued(struct cam_periph *periph)
852 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
856 device_is_alloc_queued(struct cam_ed *device)
858 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
862 device_is_send_queued(struct cam_ed *device)
864 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
868 dev_allocq_is_runnable(struct cam_devq *devq)
872 * Have space to do more work.
873 * Allowed to do work.
875 return ((devq->alloc_queue.qfrozen_cnt == 0)
876 && (devq->alloc_queue.entries > 0)
877 && (devq->alloc_openings > 0));
883 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
893 xptdone(struct cam_periph *periph, union ccb *done_ccb)
895 /* Caller will release the CCB */
896 wakeup(&done_ccb->ccb_h.cbfcnp);
900 xptopen(dev_t dev, int flags, int fmt, struct thread *td)
904 unit = minor(dev) & 0xff;
907 * Only allow read-write access.
909 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
913 * We don't allow nonblocking access.
915 if ((flags & O_NONBLOCK) != 0) {
916 printf("xpt%d: can't do nonblocking access\n", unit);
921 * We only have one transport layer right now. If someone accesses
922 * us via something other than minor number 1, point out their
926 printf("xptopen: got invalid xpt unit %d\n", unit);
930 /* Mark ourselves open */
931 xsoftc.flags |= XPT_FLAG_OPEN;
937 xptclose(dev_t dev, int flag, int fmt, struct thread *td)
941 unit = minor(dev) & 0xff;
944 * We only have one transport layer right now. If someone accesses
945 * us via something other than minor number 1, point out their
949 printf("xptclose: got invalid xpt unit %d\n", unit);
953 /* Mark ourselves closed */
954 xsoftc.flags &= ~XPT_FLAG_OPEN;
960 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
965 unit = minor(dev) & 0xff;
968 * We only have one transport layer right now. If someone accesses
969 * us via something other than minor number 1, point out their
973 printf("xptioctl: got invalid xpt unit %d\n", unit);
979 * For the transport layer CAMIOCOMMAND ioctl, we really only want
980 * to accept CCB types that don't quite make sense to send through a
981 * passthrough driver.
987 inccb = (union ccb *)addr;
989 switch(inccb->ccb_h.func_code) {
992 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
993 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1002 ccb = xpt_alloc_ccb();
1005 * Create a path using the bus, target, and lun the
1008 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1009 inccb->ccb_h.path_id,
1010 inccb->ccb_h.target_id,
1011 inccb->ccb_h.target_lun) !=
1017 /* Ensure all of our fields are correct */
1018 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1019 inccb->ccb_h.pinfo.priority);
1020 xpt_merge_ccb(ccb, inccb);
1021 ccb->ccb_h.cbfcnp = xptdone;
1022 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1023 bcopy(ccb, inccb, sizeof(union ccb));
1024 xpt_free_path(ccb->ccb_h.path);
1032 * This is an immediate CCB, so it's okay to
1033 * allocate it on the stack.
1037 * Create a path using the bus, target, and lun the
1040 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1041 inccb->ccb_h.path_id,
1042 inccb->ccb_h.target_id,
1043 inccb->ccb_h.target_lun) !=
1048 /* Ensure all of our fields are correct */
1049 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1050 inccb->ccb_h.pinfo.priority);
1051 xpt_merge_ccb(&ccb, inccb);
1052 ccb.ccb_h.cbfcnp = xptdone;
1054 bcopy(&ccb, inccb, sizeof(union ccb));
1055 xpt_free_path(ccb.ccb_h.path);
1059 case XPT_DEV_MATCH: {
1060 struct cam_periph_map_info mapinfo;
1061 struct cam_path *old_path;
1064 * We can't deal with physical addresses for this
1065 * type of transaction.
1067 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1073 * Save this in case the caller had it set to
1074 * something in particular.
1076 old_path = inccb->ccb_h.path;
1079 * We really don't need a path for the matching
1080 * code. The path is needed because of the
1081 * debugging statements in xpt_action(). They
1082 * assume that the CCB has a valid path.
1084 inccb->ccb_h.path = xpt_periph->path;
1086 bzero(&mapinfo, sizeof(mapinfo));
1089 * Map the pattern and match buffers into kernel
1090 * virtual address space.
1092 error = cam_periph_mapmem(inccb, &mapinfo);
1095 inccb->ccb_h.path = old_path;
1100 * This is an immediate CCB, we can send it on directly.
1105 * Map the buffers back into user space.
1107 cam_periph_unmapmem(inccb, &mapinfo);
1109 inccb->ccb_h.path = old_path;
1121 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1122 * with the periphal driver name and unit name filled in. The other
1123 * fields don't really matter as input. The passthrough driver name
1124 * ("pass"), and unit number are passed back in the ccb. The current
1125 * device generation number, and the index into the device peripheral
1126 * driver list, and the status are also passed back. Note that
1127 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1128 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1129 * (or rather should be) impossible for the device peripheral driver
1130 * list to change since we look at the whole thing in one pass, and
1131 * we do it with splcam protection.
1134 case CAMGETPASSTHRU: {
1136 struct cam_periph *periph;
1137 struct periph_driver **p_drv;
1141 int base_periph_found;
1145 ccb = (union ccb *)addr;
1146 unit = ccb->cgdl.unit_number;
1147 name = ccb->cgdl.periph_name;
1149 * Every 100 devices, we want to drop our spl protection to
1150 * give the software interrupt handler a chance to run.
1151 * Most systems won't run into this check, but this should
1152 * avoid starvation in the software interrupt handler in
1157 ccb = (union ccb *)addr;
1159 base_periph_found = 0;
1162 * Sanity check -- make sure we don't get a null peripheral
1165 if (*ccb->cgdl.periph_name == '\0') {
1170 /* Keep the list from changing while we traverse it */
1173 cur_generation = xsoftc.generation;
1175 /* first find our driver in the list of drivers */
1176 SET_FOREACH(p_drv, periphdriver_set) {
1177 if (strcmp((*p_drv)->driver_name, name) == 0)
1181 if (*p_drv == NULL) {
1183 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1184 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1185 *ccb->cgdl.periph_name = '\0';
1186 ccb->cgdl.unit_number = 0;
1192 * Run through every peripheral instance of this driver
1193 * and check to see whether it matches the unit passed
1194 * in by the user. If it does, get out of the loops and
1195 * find the passthrough driver associated with that
1196 * peripheral driver.
1198 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1199 periph = TAILQ_NEXT(periph, unit_links)) {
1201 if (periph->unit_number == unit) {
1203 } else if (--splbreaknum == 0) {
1207 if (cur_generation != xsoftc.generation)
1212 * If we found the peripheral driver that the user passed
1213 * in, go through all of the peripheral drivers for that
1214 * particular device and look for a passthrough driver.
1216 if (periph != NULL) {
1217 struct cam_ed *device;
1220 base_periph_found = 1;
1221 device = periph->path->device;
1222 for (i = 0, periph = device->periphs.slh_first;
1224 periph = periph->periph_links.sle_next, i++) {
1226 * Check to see whether we have a
1227 * passthrough device or not.
1229 if (strcmp(periph->periph_name, "pass") == 0) {
1231 * Fill in the getdevlist fields.
1233 strcpy(ccb->cgdl.periph_name,
1234 periph->periph_name);
1235 ccb->cgdl.unit_number =
1236 periph->unit_number;
1237 if (periph->periph_links.sle_next)
1239 CAM_GDEVLIST_MORE_DEVS;
1242 CAM_GDEVLIST_LAST_DEVICE;
1243 ccb->cgdl.generation =
1245 ccb->cgdl.index = i;
1247 * Fill in some CCB header fields
1248 * that the user may want.
1250 ccb->ccb_h.path_id =
1251 periph->path->bus->path_id;
1252 ccb->ccb_h.target_id =
1253 periph->path->target->target_id;
1254 ccb->ccb_h.target_lun =
1255 periph->path->device->lun_id;
1256 ccb->ccb_h.status = CAM_REQ_CMP;
1263 * If the periph is null here, one of two things has
1264 * happened. The first possibility is that we couldn't
1265 * find the unit number of the particular peripheral driver
1266 * that the user is asking about. e.g. the user asks for
1267 * the passthrough driver for "da11". We find the list of
1268 * "da" peripherals all right, but there is no unit 11.
1269 * The other possibility is that we went through the list
1270 * of peripheral drivers attached to the device structure,
1271 * but didn't find one with the name "pass". Either way,
1272 * we return ENOENT, since we couldn't find something.
1274 if (periph == NULL) {
1275 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1276 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1277 *ccb->cgdl.periph_name = '\0';
1278 ccb->cgdl.unit_number = 0;
1281 * It is unfortunate that this is even necessary,
1282 * but there are many, many clueless users out there.
1283 * If this is true, the user is looking for the
1284 * passthrough driver, but doesn't have one in his
1287 if (base_periph_found == 1) {
1288 printf("xptioctl: pass driver is not in the "
1290 printf("xptioctl: put \"device pass0\" in "
1291 "your kernel config file\n");
1305 /* Functions accessed by the peripheral drivers */
1310 struct cam_sim *xpt_sim;
1311 struct cam_path *path;
1312 struct cam_devq *devq;
1315 TAILQ_INIT(&xpt_busses);
1316 TAILQ_INIT(&cam_bioq);
1317 TAILQ_INIT(&cam_netq);
1318 SLIST_INIT(&ccb_freeq);
1319 STAILQ_INIT(&highpowerq);
1322 * The xpt layer is, itself, the equivelent of a SIM.
1323 * Allow 16 ccbs in the ccb pool for it. This should
1324 * give decent parallelism when we probe busses and
1325 * perform other XPT functions.
1327 devq = cam_simq_alloc(16);
1328 xpt_sim = cam_sim_alloc(xptaction,
1333 /*max_dev_transactions*/0,
1334 /*max_tagged_dev_transactions*/0,
1338 xpt_bus_register(xpt_sim, /*bus #*/0);
1341 * Looking at the XPT from the SIM layer, the XPT is
1342 * the equivelent of a peripheral driver. Allocate
1343 * a peripheral driver entry for us.
1345 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1346 CAM_TARGET_WILDCARD,
1347 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1348 printf("xpt_init: xpt_create_path failed with status %#x,"
1349 " failing attach\n", status);
1353 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1354 path, NULL, 0, NULL);
1355 xpt_free_path(path);
1357 xpt_sim->softc = xpt_periph;
1360 * Register a callback for when interrupts are enabled.
1362 xpt_config_hook = malloc(sizeof(struct intr_config_hook),
1363 M_TEMP, M_INTWAIT | M_ZERO);
1364 xpt_config_hook->ich_func = xpt_config;
1365 if (config_intrhook_establish(xpt_config_hook) != 0) {
1366 free (xpt_config_hook, M_TEMP);
1367 printf("xpt_init: config_intrhook_establish failed "
1368 "- failing attach\n");
1371 /* Install our software interrupt handlers */
1372 register_swi(SWI_CAMNET, swi_camnet, NULL, "swi_camnet");
1373 register_swi(SWI_CAMBIO, swi_cambio, NULL, "swi_cambio");
1377 xptregister(struct cam_periph *periph, void *arg)
1379 if (periph == NULL) {
1380 printf("xptregister: periph was NULL!!\n");
1381 return(CAM_REQ_CMP_ERR);
1384 periph->softc = NULL;
1386 xpt_periph = periph;
1388 return(CAM_REQ_CMP);
1392 xpt_add_periph(struct cam_periph *periph)
1394 struct cam_ed *device;
1396 struct periph_list *periph_head;
1398 device = periph->path->device;
1400 periph_head = &device->periphs;
1402 status = CAM_REQ_CMP;
1404 if (device != NULL) {
1408 * Make room for this peripheral
1409 * so it will fit in the queue
1410 * when it's scheduled to run
1413 status = camq_resize(&device->drvq,
1414 device->drvq.array_size + 1);
1416 device->generation++;
1418 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1423 xsoftc.generation++;
1429 xpt_remove_periph(struct cam_periph *periph)
1431 struct cam_ed *device;
1433 device = periph->path->device;
1435 if (device != NULL) {
1437 struct periph_list *periph_head;
1439 periph_head = &device->periphs;
1441 /* Release the slot for this peripheral */
1443 camq_resize(&device->drvq, device->drvq.array_size - 1);
1445 device->generation++;
1447 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1452 xsoftc.generation++;
1457 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1461 struct cam_path *path;
1462 struct ccb_trans_settings cts;
1464 path = periph->path;
1466 * To ensure that this is printed in one piece,
1467 * mask out CAM interrupts.
1470 printf("%s%d at %s%d bus %d target %d lun %d\n",
1471 periph->periph_name, periph->unit_number,
1472 path->bus->sim->sim_name,
1473 path->bus->sim->unit_number,
1474 path->bus->sim->bus_id,
1475 path->target->target_id,
1476 path->device->lun_id);
1477 printf("%s%d: ", periph->periph_name, periph->unit_number);
1478 scsi_print_inquiry(&path->device->inq_data);
1480 && (path->device->serial_num_len > 0)) {
1481 /* Don't wrap the screen - print only the first 60 chars */
1482 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1483 periph->unit_number, path->device->serial_num);
1485 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1486 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1487 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1488 xpt_action((union ccb*)&cts);
1489 if (cts.ccb_h.status == CAM_REQ_CMP) {
1493 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1494 && cts.sync_offset != 0) {
1495 freq = scsi_calc_syncsrate(cts.sync_period);
1498 struct ccb_pathinq cpi;
1500 /* Ask the SIM for its base transfer speed */
1501 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1502 cpi.ccb_h.func_code = XPT_PATH_INQ;
1503 xpt_action((union ccb *)&cpi);
1505 speed = cpi.base_transfer_speed;
1508 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1509 speed *= (0x01 << cts.bus_width);
1512 printf("%s%d: %d.%03dMB/s transfers",
1513 periph->periph_name, periph->unit_number,
1516 printf("%s%d: %dKB/s transfers", periph->periph_name,
1517 periph->unit_number, speed);
1518 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1519 && cts.sync_offset != 0) {
1520 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1521 freq % 1000, cts.sync_offset);
1523 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1524 && cts.bus_width > 0) {
1525 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1526 && cts.sync_offset != 0) {
1531 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1532 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1533 && cts.sync_offset != 0) {
1537 if (path->device->inq_flags & SID_CmdQue
1538 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1539 printf(", Tagged Queueing Enabled");
1543 } else if (path->device->inq_flags & SID_CmdQue
1544 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1545 printf("%s%d: Tagged Queueing Enabled\n",
1546 periph->periph_name, periph->unit_number);
1550 * We only want to print the caller's announce string if they've
1553 if (announce_string != NULL)
1554 printf("%s%d: %s\n", periph->periph_name,
1555 periph->unit_number, announce_string);
1560 static dev_match_ret
1561 xptbusmatch(struct dev_match_pattern *patterns, int num_patterns,
1564 dev_match_ret retval;
1567 retval = DM_RET_NONE;
1570 * If we aren't given something to match against, that's an error.
1573 return(DM_RET_ERROR);
1576 * If there are no match entries, then this bus matches no
1579 if ((patterns == NULL) || (num_patterns == 0))
1580 return(DM_RET_DESCEND | DM_RET_COPY);
1582 for (i = 0; i < num_patterns; i++) {
1583 struct bus_match_pattern *cur_pattern;
1586 * If the pattern in question isn't for a bus node, we
1587 * aren't interested. However, we do indicate to the
1588 * calling routine that we should continue descending the
1589 * tree, since the user wants to match against lower-level
1592 if (patterns[i].type != DEV_MATCH_BUS) {
1593 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1594 retval |= DM_RET_DESCEND;
1598 cur_pattern = &patterns[i].pattern.bus_pattern;
1601 * If they want to match any bus node, we give them any
1604 if (cur_pattern->flags == BUS_MATCH_ANY) {
1605 /* set the copy flag */
1606 retval |= DM_RET_COPY;
1609 * If we've already decided on an action, go ahead
1612 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1617 * Not sure why someone would do this...
1619 if (cur_pattern->flags == BUS_MATCH_NONE)
1622 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1623 && (cur_pattern->path_id != bus->path_id))
1626 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1627 && (cur_pattern->bus_id != bus->sim->bus_id))
1630 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1631 && (cur_pattern->unit_number != bus->sim->unit_number))
1634 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1635 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1640 * If we get to this point, the user definitely wants
1641 * information on this bus. So tell the caller to copy the
1644 retval |= DM_RET_COPY;
1647 * If the return action has been set to descend, then we
1648 * know that we've already seen a non-bus matching
1649 * expression, therefore we need to further descend the tree.
1650 * This won't change by continuing around the loop, so we
1651 * go ahead and return. If we haven't seen a non-bus
1652 * matching expression, we keep going around the loop until
1653 * we exhaust the matching expressions. We'll set the stop
1654 * flag once we fall out of the loop.
1656 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1661 * If the return action hasn't been set to descend yet, that means
1662 * we haven't seen anything other than bus matching patterns. So
1663 * tell the caller to stop descending the tree -- the user doesn't
1664 * want to match against lower level tree elements.
1666 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1667 retval |= DM_RET_STOP;
1672 static dev_match_ret
1673 xptdevicematch(struct dev_match_pattern *patterns, int num_patterns,
1674 struct cam_ed *device)
1676 dev_match_ret retval;
1679 retval = DM_RET_NONE;
1682 * If we aren't given something to match against, that's an error.
1685 return(DM_RET_ERROR);
1688 * If there are no match entries, then this device matches no
1691 if ((patterns == NULL) || (patterns == 0))
1692 return(DM_RET_DESCEND | DM_RET_COPY);
1694 for (i = 0; i < num_patterns; i++) {
1695 struct device_match_pattern *cur_pattern;
1698 * If the pattern in question isn't for a device node, we
1699 * aren't interested.
1701 if (patterns[i].type != DEV_MATCH_DEVICE) {
1702 if ((patterns[i].type == DEV_MATCH_PERIPH)
1703 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1704 retval |= DM_RET_DESCEND;
1708 cur_pattern = &patterns[i].pattern.device_pattern;
1711 * If they want to match any device node, we give them any
1714 if (cur_pattern->flags == DEV_MATCH_ANY) {
1715 /* set the copy flag */
1716 retval |= DM_RET_COPY;
1720 * If we've already decided on an action, go ahead
1723 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1728 * Not sure why someone would do this...
1730 if (cur_pattern->flags == DEV_MATCH_NONE)
1733 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1734 && (cur_pattern->path_id != device->target->bus->path_id))
1737 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1738 && (cur_pattern->target_id != device->target->target_id))
1741 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1742 && (cur_pattern->target_lun != device->lun_id))
1745 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1746 && (cam_quirkmatch((caddr_t)&device->inq_data,
1747 (caddr_t)&cur_pattern->inq_pat,
1748 1, sizeof(cur_pattern->inq_pat),
1749 scsi_static_inquiry_match) == NULL))
1753 * If we get to this point, the user definitely wants
1754 * information on this device. So tell the caller to copy
1757 retval |= DM_RET_COPY;
1760 * If the return action has been set to descend, then we
1761 * know that we've already seen a peripheral matching
1762 * expression, therefore we need to further descend the tree.
1763 * This won't change by continuing around the loop, so we
1764 * go ahead and return. If we haven't seen a peripheral
1765 * matching expression, we keep going around the loop until
1766 * we exhaust the matching expressions. We'll set the stop
1767 * flag once we fall out of the loop.
1769 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1774 * If the return action hasn't been set to descend yet, that means
1775 * we haven't seen any peripheral matching patterns. So tell the
1776 * caller to stop descending the tree -- the user doesn't want to
1777 * match against lower level tree elements.
1779 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1780 retval |= DM_RET_STOP;
1786 * Match a single peripheral against any number of match patterns.
1788 static dev_match_ret
1789 xptperiphmatch(struct dev_match_pattern *patterns, int num_patterns,
1790 struct cam_periph *periph)
1792 dev_match_ret retval;
1796 * If we aren't given something to match against, that's an error.
1799 return(DM_RET_ERROR);
1802 * If there are no match entries, then this peripheral matches no
1805 if ((patterns == NULL) || (num_patterns == 0))
1806 return(DM_RET_STOP | DM_RET_COPY);
1809 * There aren't any nodes below a peripheral node, so there's no
1810 * reason to descend the tree any further.
1812 retval = DM_RET_STOP;
1814 for (i = 0; i < num_patterns; i++) {
1815 struct periph_match_pattern *cur_pattern;
1818 * If the pattern in question isn't for a peripheral, we
1819 * aren't interested.
1821 if (patterns[i].type != DEV_MATCH_PERIPH)
1824 cur_pattern = &patterns[i].pattern.periph_pattern;
1827 * If they want to match on anything, then we will do so.
1829 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1830 /* set the copy flag */
1831 retval |= DM_RET_COPY;
1834 * We've already set the return action to stop,
1835 * since there are no nodes below peripherals in
1842 * Not sure why someone would do this...
1844 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1847 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1848 && (cur_pattern->path_id != periph->path->bus->path_id))
1852 * For the target and lun id's, we have to make sure the
1853 * target and lun pointers aren't NULL. The xpt peripheral
1854 * has a wildcard target and device.
1856 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1857 && ((periph->path->target == NULL)
1858 ||(cur_pattern->target_id != periph->path->target->target_id)))
1861 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1862 && ((periph->path->device == NULL)
1863 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1866 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1867 && (cur_pattern->unit_number != periph->unit_number))
1870 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1871 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1876 * If we get to this point, the user definitely wants
1877 * information on this peripheral. So tell the caller to
1878 * copy the data out.
1880 retval |= DM_RET_COPY;
1883 * The return action has already been set to stop, since
1884 * peripherals don't have any nodes below them in the EDT.
1890 * If we get to this point, the peripheral that was passed in
1891 * doesn't match any of the patterns.
1897 xptedtbusfunc(struct cam_eb *bus, void *arg)
1899 struct ccb_dev_match *cdm;
1900 dev_match_ret retval;
1902 cdm = (struct ccb_dev_match *)arg;
1905 * If our position is for something deeper in the tree, that means
1906 * that we've already seen this node. So, we keep going down.
1908 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1909 && (cdm->pos.cookie.bus == bus)
1910 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1911 && (cdm->pos.cookie.target != NULL))
1912 retval = DM_RET_DESCEND;
1914 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1917 * If we got an error, bail out of the search.
1919 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1920 cdm->status = CAM_DEV_MATCH_ERROR;
1925 * If the copy flag is set, copy this bus out.
1927 if (retval & DM_RET_COPY) {
1930 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1931 sizeof(struct dev_match_result));
1934 * If we don't have enough space to put in another
1935 * match result, save our position and tell the
1936 * user there are more devices to check.
1938 if (spaceleft < sizeof(struct dev_match_result)) {
1939 bzero(&cdm->pos, sizeof(cdm->pos));
1940 cdm->pos.position_type =
1941 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1943 cdm->pos.cookie.bus = bus;
1944 cdm->pos.generations[CAM_BUS_GENERATION]=
1946 cdm->status = CAM_DEV_MATCH_MORE;
1949 j = cdm->num_matches;
1951 cdm->matches[j].type = DEV_MATCH_BUS;
1952 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1953 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1954 cdm->matches[j].result.bus_result.unit_number =
1955 bus->sim->unit_number;
1956 strncpy(cdm->matches[j].result.bus_result.dev_name,
1957 bus->sim->sim_name, DEV_IDLEN);
1961 * If the user is only interested in busses, there's no
1962 * reason to descend to the next level in the tree.
1964 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1968 * If there is a target generation recorded, check it to
1969 * make sure the target list hasn't changed.
1971 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1972 && (bus == cdm->pos.cookie.bus)
1973 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1974 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1975 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1977 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1981 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1982 && (cdm->pos.cookie.bus == bus)
1983 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1984 && (cdm->pos.cookie.target != NULL))
1985 return(xpttargettraverse(bus,
1986 (struct cam_et *)cdm->pos.cookie.target,
1987 xptedttargetfunc, arg));
1989 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1993 xptedttargetfunc(struct cam_et *target, void *arg)
1995 struct ccb_dev_match *cdm;
1997 cdm = (struct ccb_dev_match *)arg;
2000 * If there is a device list generation recorded, check it to
2001 * make sure the device list hasn't changed.
2003 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2004 && (cdm->pos.cookie.bus == target->bus)
2005 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2006 && (cdm->pos.cookie.target == target)
2007 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2008 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2009 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2010 target->generation)) {
2011 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2015 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2016 && (cdm->pos.cookie.bus == target->bus)
2017 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2018 && (cdm->pos.cookie.target == target)
2019 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2020 && (cdm->pos.cookie.device != NULL))
2021 return(xptdevicetraverse(target,
2022 (struct cam_ed *)cdm->pos.cookie.device,
2023 xptedtdevicefunc, arg));
2025 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2029 xptedtdevicefunc(struct cam_ed *device, void *arg)
2032 struct ccb_dev_match *cdm;
2033 dev_match_ret retval;
2035 cdm = (struct ccb_dev_match *)arg;
2038 * If our position is for something deeper in the tree, that means
2039 * that we've already seen this node. So, we keep going down.
2041 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2042 && (cdm->pos.cookie.device == device)
2043 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2044 && (cdm->pos.cookie.periph != NULL))
2045 retval = DM_RET_DESCEND;
2047 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2050 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2051 cdm->status = CAM_DEV_MATCH_ERROR;
2056 * If the copy flag is set, copy this device out.
2058 if (retval & DM_RET_COPY) {
2061 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2062 sizeof(struct dev_match_result));
2065 * If we don't have enough space to put in another
2066 * match result, save our position and tell the
2067 * user there are more devices to check.
2069 if (spaceleft < sizeof(struct dev_match_result)) {
2070 bzero(&cdm->pos, sizeof(cdm->pos));
2071 cdm->pos.position_type =
2072 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2073 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2075 cdm->pos.cookie.bus = device->target->bus;
2076 cdm->pos.generations[CAM_BUS_GENERATION]=
2078 cdm->pos.cookie.target = device->target;
2079 cdm->pos.generations[CAM_TARGET_GENERATION] =
2080 device->target->bus->generation;
2081 cdm->pos.cookie.device = device;
2082 cdm->pos.generations[CAM_DEV_GENERATION] =
2083 device->target->generation;
2084 cdm->status = CAM_DEV_MATCH_MORE;
2087 j = cdm->num_matches;
2089 cdm->matches[j].type = DEV_MATCH_DEVICE;
2090 cdm->matches[j].result.device_result.path_id =
2091 device->target->bus->path_id;
2092 cdm->matches[j].result.device_result.target_id =
2093 device->target->target_id;
2094 cdm->matches[j].result.device_result.target_lun =
2096 bcopy(&device->inq_data,
2097 &cdm->matches[j].result.device_result.inq_data,
2098 sizeof(struct scsi_inquiry_data));
2100 /* Let the user know whether this device is unconfigured */
2101 if (device->flags & CAM_DEV_UNCONFIGURED)
2102 cdm->matches[j].result.device_result.flags =
2103 DEV_RESULT_UNCONFIGURED;
2105 cdm->matches[j].result.device_result.flags =
2110 * If the user isn't interested in peripherals, don't descend
2111 * the tree any further.
2113 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2117 * If there is a peripheral list generation recorded, make sure
2118 * it hasn't changed.
2120 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2121 && (device->target->bus == cdm->pos.cookie.bus)
2122 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2123 && (device->target == cdm->pos.cookie.target)
2124 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2125 && (device == cdm->pos.cookie.device)
2126 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2127 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2128 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2129 device->generation)){
2130 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2134 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2135 && (cdm->pos.cookie.bus == device->target->bus)
2136 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2137 && (cdm->pos.cookie.target == device->target)
2138 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2139 && (cdm->pos.cookie.device == device)
2140 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2141 && (cdm->pos.cookie.periph != NULL))
2142 return(xptperiphtraverse(device,
2143 (struct cam_periph *)cdm->pos.cookie.periph,
2144 xptedtperiphfunc, arg));
2146 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2150 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2152 struct ccb_dev_match *cdm;
2153 dev_match_ret retval;
2155 cdm = (struct ccb_dev_match *)arg;
2157 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2159 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2160 cdm->status = CAM_DEV_MATCH_ERROR;
2165 * If the copy flag is set, copy this peripheral out.
2167 if (retval & DM_RET_COPY) {
2170 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2171 sizeof(struct dev_match_result));
2174 * If we don't have enough space to put in another
2175 * match result, save our position and tell the
2176 * user there are more devices to check.
2178 if (spaceleft < sizeof(struct dev_match_result)) {
2179 bzero(&cdm->pos, sizeof(cdm->pos));
2180 cdm->pos.position_type =
2181 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2182 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2185 cdm->pos.cookie.bus = periph->path->bus;
2186 cdm->pos.generations[CAM_BUS_GENERATION]=
2188 cdm->pos.cookie.target = periph->path->target;
2189 cdm->pos.generations[CAM_TARGET_GENERATION] =
2190 periph->path->bus->generation;
2191 cdm->pos.cookie.device = periph->path->device;
2192 cdm->pos.generations[CAM_DEV_GENERATION] =
2193 periph->path->target->generation;
2194 cdm->pos.cookie.periph = periph;
2195 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2196 periph->path->device->generation;
2197 cdm->status = CAM_DEV_MATCH_MORE;
2201 j = cdm->num_matches;
2203 cdm->matches[j].type = DEV_MATCH_PERIPH;
2204 cdm->matches[j].result.periph_result.path_id =
2205 periph->path->bus->path_id;
2206 cdm->matches[j].result.periph_result.target_id =
2207 periph->path->target->target_id;
2208 cdm->matches[j].result.periph_result.target_lun =
2209 periph->path->device->lun_id;
2210 cdm->matches[j].result.periph_result.unit_number =
2211 periph->unit_number;
2212 strncpy(cdm->matches[j].result.periph_result.periph_name,
2213 periph->periph_name, DEV_IDLEN);
2220 xptedtmatch(struct ccb_dev_match *cdm)
2224 cdm->num_matches = 0;
2227 * Check the bus list generation. If it has changed, the user
2228 * needs to reset everything and start over.
2230 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2231 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2232 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2233 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2237 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2238 && (cdm->pos.cookie.bus != NULL))
2239 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2240 xptedtbusfunc, cdm);
2242 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2245 * If we get back 0, that means that we had to stop before fully
2246 * traversing the EDT. It also means that one of the subroutines
2247 * has set the status field to the proper value. If we get back 1,
2248 * we've fully traversed the EDT and copied out any matching entries.
2251 cdm->status = CAM_DEV_MATCH_LAST;
2257 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2259 struct ccb_dev_match *cdm;
2261 cdm = (struct ccb_dev_match *)arg;
2263 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2264 && (cdm->pos.cookie.pdrv == pdrv)
2265 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2266 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2267 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2268 (*pdrv)->generation)) {
2269 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2273 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2274 && (cdm->pos.cookie.pdrv == pdrv)
2275 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2276 && (cdm->pos.cookie.periph != NULL))
2277 return(xptpdperiphtraverse(pdrv,
2278 (struct cam_periph *)cdm->pos.cookie.periph,
2279 xptplistperiphfunc, arg));
2281 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2285 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2287 struct ccb_dev_match *cdm;
2288 dev_match_ret retval;
2290 cdm = (struct ccb_dev_match *)arg;
2292 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2294 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2295 cdm->status = CAM_DEV_MATCH_ERROR;
2300 * If the copy flag is set, copy this peripheral out.
2302 if (retval & DM_RET_COPY) {
2305 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2306 sizeof(struct dev_match_result));
2309 * If we don't have enough space to put in another
2310 * match result, save our position and tell the
2311 * user there are more devices to check.
2313 if (spaceleft < sizeof(struct dev_match_result)) {
2314 struct periph_driver **pdrv;
2317 bzero(&cdm->pos, sizeof(cdm->pos));
2318 cdm->pos.position_type =
2319 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2323 * This may look a bit non-sensical, but it is
2324 * actually quite logical. There are very few
2325 * peripheral drivers, and bloating every peripheral
2326 * structure with a pointer back to its parent
2327 * peripheral driver linker set entry would cost
2328 * more in the long run than doing this quick lookup.
2330 SET_FOREACH(pdrv, periphdriver_set) {
2331 if (strcmp((*pdrv)->driver_name,
2332 periph->periph_name) == 0)
2337 cdm->status = CAM_DEV_MATCH_ERROR;
2341 cdm->pos.cookie.pdrv = pdrv;
2343 * The periph generation slot does double duty, as
2344 * does the periph pointer slot. They are used for
2345 * both edt and pdrv lookups and positioning.
2347 cdm->pos.cookie.periph = periph;
2348 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2349 (*pdrv)->generation;
2350 cdm->status = CAM_DEV_MATCH_MORE;
2354 j = cdm->num_matches;
2356 cdm->matches[j].type = DEV_MATCH_PERIPH;
2357 cdm->matches[j].result.periph_result.path_id =
2358 periph->path->bus->path_id;
2361 * The transport layer peripheral doesn't have a target or
2364 if (periph->path->target)
2365 cdm->matches[j].result.periph_result.target_id =
2366 periph->path->target->target_id;
2368 cdm->matches[j].result.periph_result.target_id = -1;
2370 if (periph->path->device)
2371 cdm->matches[j].result.periph_result.target_lun =
2372 periph->path->device->lun_id;
2374 cdm->matches[j].result.periph_result.target_lun = -1;
2376 cdm->matches[j].result.periph_result.unit_number =
2377 periph->unit_number;
2378 strncpy(cdm->matches[j].result.periph_result.periph_name,
2379 periph->periph_name, DEV_IDLEN);
2386 xptperiphlistmatch(struct ccb_dev_match *cdm)
2390 cdm->num_matches = 0;
2393 * At this point in the edt traversal function, we check the bus
2394 * list generation to make sure that no busses have been added or
2395 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2396 * For the peripheral driver list traversal function, however, we
2397 * don't have to worry about new peripheral driver types coming or
2398 * going; they're in a linker set, and therefore can't change
2399 * without a recompile.
2402 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2403 && (cdm->pos.cookie.pdrv != NULL))
2404 ret = xptpdrvtraverse(
2405 (struct periph_driver **)cdm->pos.cookie.pdrv,
2406 xptplistpdrvfunc, cdm);
2408 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2411 * If we get back 0, that means that we had to stop before fully
2412 * traversing the peripheral driver tree. It also means that one of
2413 * the subroutines has set the status field to the proper value. If
2414 * we get back 1, we've fully traversed the EDT and copied out any
2418 cdm->status = CAM_DEV_MATCH_LAST;
2424 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2426 struct cam_eb *bus, *next_bus;
2431 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2434 next_bus = TAILQ_NEXT(bus, links);
2436 retval = tr_func(bus, arg);
2445 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2446 xpt_targetfunc_t *tr_func, void *arg)
2448 struct cam_et *target, *next_target;
2452 for (target = (start_target ? start_target :
2453 TAILQ_FIRST(&bus->et_entries));
2454 target != NULL; target = next_target) {
2456 next_target = TAILQ_NEXT(target, links);
2458 retval = tr_func(target, arg);
2468 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2469 xpt_devicefunc_t *tr_func, void *arg)
2471 struct cam_ed *device, *next_device;
2475 for (device = (start_device ? start_device :
2476 TAILQ_FIRST(&target->ed_entries));
2478 device = next_device) {
2480 next_device = TAILQ_NEXT(device, links);
2482 retval = tr_func(device, arg);
2492 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2493 xpt_periphfunc_t *tr_func, void *arg)
2495 struct cam_periph *periph, *next_periph;
2500 for (periph = (start_periph ? start_periph :
2501 SLIST_FIRST(&device->periphs));
2503 periph = next_periph) {
2505 next_periph = SLIST_NEXT(periph, periph_links);
2507 retval = tr_func(periph, arg);
2516 xptpdrvtraverse(struct periph_driver **start_pdrv,
2517 xpt_pdrvfunc_t *tr_func, void *arg)
2519 struct periph_driver **pdrv;
2525 * We don't traverse the peripheral driver list like we do the
2526 * other lists, because it is a linker set, and therefore cannot be
2527 * changed during runtime. If the peripheral driver list is ever
2528 * re-done to be something other than a linker set (i.e. it can
2529 * change while the system is running), the list traversal should
2530 * be modified to work like the other traversal functions.
2532 SET_FOREACH(pdrv, periphdriver_set) {
2533 if (start_pdrv == NULL || start_pdrv == pdrv) {
2534 retval = tr_func(pdrv, arg);
2537 start_pdrv = NULL; /* traverse remainder */
2544 xptpdperiphtraverse(struct periph_driver **pdrv,
2545 struct cam_periph *start_periph,
2546 xpt_periphfunc_t *tr_func, void *arg)
2548 struct cam_periph *periph, *next_periph;
2553 for (periph = (start_periph ? start_periph :
2554 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2555 periph = next_periph) {
2557 next_periph = TAILQ_NEXT(periph, unit_links);
2559 retval = tr_func(periph, arg);
2567 xptdefbusfunc(struct cam_eb *bus, void *arg)
2569 struct xpt_traverse_config *tr_config;
2571 tr_config = (struct xpt_traverse_config *)arg;
2573 if (tr_config->depth == XPT_DEPTH_BUS) {
2574 xpt_busfunc_t *tr_func;
2576 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2578 return(tr_func(bus, tr_config->tr_arg));
2580 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2584 xptdeftargetfunc(struct cam_et *target, void *arg)
2586 struct xpt_traverse_config *tr_config;
2588 tr_config = (struct xpt_traverse_config *)arg;
2590 if (tr_config->depth == XPT_DEPTH_TARGET) {
2591 xpt_targetfunc_t *tr_func;
2593 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2595 return(tr_func(target, tr_config->tr_arg));
2597 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2601 xptdefdevicefunc(struct cam_ed *device, void *arg)
2603 struct xpt_traverse_config *tr_config;
2605 tr_config = (struct xpt_traverse_config *)arg;
2607 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2608 xpt_devicefunc_t *tr_func;
2610 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2612 return(tr_func(device, tr_config->tr_arg));
2614 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2618 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2620 struct xpt_traverse_config *tr_config;
2621 xpt_periphfunc_t *tr_func;
2623 tr_config = (struct xpt_traverse_config *)arg;
2625 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2628 * Unlike the other default functions, we don't check for depth
2629 * here. The peripheral driver level is the last level in the EDT,
2630 * so if we're here, we should execute the function in question.
2632 return(tr_func(periph, tr_config->tr_arg));
2636 * Execute the given function for every bus in the EDT.
2639 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2641 struct xpt_traverse_config tr_config;
2643 tr_config.depth = XPT_DEPTH_BUS;
2644 tr_config.tr_func = tr_func;
2645 tr_config.tr_arg = arg;
2647 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2652 * Execute the given function for every target in the EDT.
2655 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2657 struct xpt_traverse_config tr_config;
2659 tr_config.depth = XPT_DEPTH_TARGET;
2660 tr_config.tr_func = tr_func;
2661 tr_config.tr_arg = arg;
2663 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2665 #endif /* notusedyet */
2668 * Execute the given function for every device in the EDT.
2671 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2673 struct xpt_traverse_config tr_config;
2675 tr_config.depth = XPT_DEPTH_DEVICE;
2676 tr_config.tr_func = tr_func;
2677 tr_config.tr_arg = arg;
2679 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2684 * Execute the given function for every peripheral in the EDT.
2687 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2689 struct xpt_traverse_config tr_config;
2691 tr_config.depth = XPT_DEPTH_PERIPH;
2692 tr_config.tr_func = tr_func;
2693 tr_config.tr_arg = arg;
2695 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2697 #endif /* notusedyet */
2700 xptsetasyncfunc(struct cam_ed *device, void *arg)
2702 struct cam_path path;
2703 struct ccb_getdev cgd;
2704 struct async_node *cur_entry;
2706 cur_entry = (struct async_node *)arg;
2709 * Don't report unconfigured devices (Wildcard devs,
2710 * devices only for target mode, device instances
2711 * that have been invalidated but are waiting for
2712 * their last reference count to be released).
2714 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2717 xpt_compile_path(&path,
2719 device->target->bus->path_id,
2720 device->target->target_id,
2722 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2723 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2724 xpt_action((union ccb *)&cgd);
2725 cur_entry->callback(cur_entry->callback_arg,
2728 xpt_release_path(&path);
2734 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2736 struct cam_path path;
2737 struct ccb_pathinq cpi;
2738 struct async_node *cur_entry;
2740 cur_entry = (struct async_node *)arg;
2742 xpt_compile_path(&path, /*periph*/NULL,
2744 CAM_TARGET_WILDCARD,
2746 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2747 cpi.ccb_h.func_code = XPT_PATH_INQ;
2748 xpt_action((union ccb *)&cpi);
2749 cur_entry->callback(cur_entry->callback_arg,
2752 xpt_release_path(&path);
2758 xpt_action(union ccb *start_ccb)
2762 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2764 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2766 iopl = splsoftcam();
2767 switch (start_ccb->ccb_h.func_code) {
2771 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2772 struct cam_path *path;
2774 path = start_ccb->ccb_h.path;
2778 * For the sake of compatibility with SCSI-1
2779 * devices that may not understand the identify
2780 * message, we include lun information in the
2781 * second byte of all commands. SCSI-1 specifies
2782 * that luns are a 3 bit value and reserves only 3
2783 * bits for lun information in the CDB. Later
2784 * revisions of the SCSI spec allow for more than 8
2785 * luns, but have deprecated lun information in the
2786 * CDB. So, if the lun won't fit, we must omit.
2788 * Also be aware that during initial probing for devices,
2789 * the inquiry information is unknown but initialized to 0.
2790 * This means that this code will be exercised while probing
2791 * devices with an ANSI revision greater than 2.
2793 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2794 && start_ccb->ccb_h.target_lun < 8
2795 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2797 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2798 start_ccb->ccb_h.target_lun << 5;
2800 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2801 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2802 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2803 &path->device->inq_data),
2804 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2805 cdb_str, sizeof(cdb_str))));
2809 case XPT_CONT_TARGET_IO:
2810 start_ccb->csio.sense_resid = 0;
2811 start_ccb->csio.resid = 0;
2816 struct cam_path *path;
2820 path = start_ccb->ccb_h.path;
2823 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2824 if (path->device->qfrozen_cnt == 0)
2825 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2830 xpt_run_dev_sendq(path->bus);
2833 case XPT_SET_TRAN_SETTINGS:
2835 xpt_set_transfer_settings(&start_ccb->cts,
2836 start_ccb->ccb_h.path->device,
2837 /*async_update*/FALSE);
2840 case XPT_CALC_GEOMETRY:
2842 struct cam_sim *sim;
2844 /* Filter out garbage */
2845 if (start_ccb->ccg.block_size == 0
2846 || start_ccb->ccg.volume_size == 0) {
2847 start_ccb->ccg.cylinders = 0;
2848 start_ccb->ccg.heads = 0;
2849 start_ccb->ccg.secs_per_track = 0;
2850 start_ccb->ccb_h.status = CAM_REQ_CMP;
2855 * In a PC-98 system, geometry translation depens on
2856 * the "real" device geometry obtained from mode page 4.
2857 * SCSI geometry translation is performed in the
2858 * initialization routine of the SCSI BIOS and the result
2859 * stored in host memory. If the translation is available
2860 * in host memory, use it. If not, rely on the default
2861 * translation the device driver performs.
2863 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2864 start_ccb->ccb_h.status = CAM_REQ_CMP;
2868 sim = start_ccb->ccb_h.path->bus->sim;
2869 (*(sim->sim_action))(sim, start_ccb);
2874 union ccb* abort_ccb;
2877 abort_ccb = start_ccb->cab.abort_ccb;
2878 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2880 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2881 struct cam_ccbq *ccbq;
2883 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2884 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2885 abort_ccb->ccb_h.status =
2886 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2887 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2889 xpt_done(abort_ccb);
2891 start_ccb->ccb_h.status = CAM_REQ_CMP;
2894 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2895 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2897 * We've caught this ccb en route to
2898 * the SIM. Flag it for abort and the
2899 * SIM will do so just before starting
2900 * real work on the CCB.
2902 abort_ccb->ccb_h.status =
2903 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2904 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2905 start_ccb->ccb_h.status = CAM_REQ_CMP;
2909 if (XPT_FC_IS_QUEUED(abort_ccb)
2910 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2912 * It's already completed but waiting
2913 * for our SWI to get to it.
2915 start_ccb->ccb_h.status = CAM_UA_ABORT;
2919 * If we weren't able to take care of the abort request
2920 * in the XPT, pass the request down to the SIM for processing.
2924 case XPT_ACCEPT_TARGET_IO:
2926 case XPT_IMMED_NOTIFY:
2927 case XPT_NOTIFY_ACK:
2928 case XPT_GET_TRAN_SETTINGS:
2931 struct cam_sim *sim;
2933 sim = start_ccb->ccb_h.path->bus->sim;
2934 (*(sim->sim_action))(sim, start_ccb);
2939 struct cam_sim *sim;
2941 sim = start_ccb->ccb_h.path->bus->sim;
2942 (*(sim->sim_action))(sim, start_ccb);
2945 case XPT_PATH_STATS:
2946 start_ccb->cpis.last_reset =
2947 start_ccb->ccb_h.path->bus->last_reset;
2948 start_ccb->ccb_h.status = CAM_REQ_CMP;
2955 dev = start_ccb->ccb_h.path->device;
2957 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2958 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2960 struct ccb_getdev *cgd;
2964 cgd = &start_ccb->cgd;
2965 bus = cgd->ccb_h.path->bus;
2966 tar = cgd->ccb_h.path->target;
2967 cgd->inq_data = dev->inq_data;
2968 cgd->ccb_h.status = CAM_REQ_CMP;
2969 cgd->serial_num_len = dev->serial_num_len;
2970 if ((dev->serial_num_len > 0)
2971 && (dev->serial_num != NULL))
2972 bcopy(dev->serial_num, cgd->serial_num,
2973 dev->serial_num_len);
2978 case XPT_GDEV_STATS:
2983 dev = start_ccb->ccb_h.path->device;
2985 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2986 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2988 struct ccb_getdevstats *cgds;
2992 cgds = &start_ccb->cgds;
2993 bus = cgds->ccb_h.path->bus;
2994 tar = cgds->ccb_h.path->target;
2995 cgds->dev_openings = dev->ccbq.dev_openings;
2996 cgds->dev_active = dev->ccbq.dev_active;
2997 cgds->devq_openings = dev->ccbq.devq_openings;
2998 cgds->devq_queued = dev->ccbq.queue.entries;
2999 cgds->held = dev->ccbq.held;
3000 cgds->last_reset = tar->last_reset;
3001 cgds->maxtags = dev->quirk->maxtags;
3002 cgds->mintags = dev->quirk->mintags;
3003 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3004 cgds->last_reset = bus->last_reset;
3005 cgds->ccb_h.status = CAM_REQ_CMP;
3012 struct cam_periph *nperiph;
3013 struct periph_list *periph_head;
3014 struct ccb_getdevlist *cgdl;
3017 struct cam_ed *device;
3024 * Don't want anyone mucking with our data.
3027 device = start_ccb->ccb_h.path->device;
3028 periph_head = &device->periphs;
3029 cgdl = &start_ccb->cgdl;
3032 * Check and see if the list has changed since the user
3033 * last requested a list member. If so, tell them that the
3034 * list has changed, and therefore they need to start over
3035 * from the beginning.
3037 if ((cgdl->index != 0) &&
3038 (cgdl->generation != device->generation)) {
3039 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3045 * Traverse the list of peripherals and attempt to find
3046 * the requested peripheral.
3048 for (nperiph = periph_head->slh_first, i = 0;
3049 (nperiph != NULL) && (i <= cgdl->index);
3050 nperiph = nperiph->periph_links.sle_next, i++) {
3051 if (i == cgdl->index) {
3052 strncpy(cgdl->periph_name,
3053 nperiph->periph_name,
3055 cgdl->unit_number = nperiph->unit_number;
3060 cgdl->status = CAM_GDEVLIST_ERROR;
3065 if (nperiph == NULL)
3066 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3068 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3071 cgdl->generation = device->generation;
3074 cgdl->ccb_h.status = CAM_REQ_CMP;
3080 dev_pos_type position_type;
3081 struct ccb_dev_match *cdm;
3084 cdm = &start_ccb->cdm;
3087 * Prevent EDT changes while we traverse it.
3091 * There are two ways of getting at information in the EDT.
3092 * The first way is via the primary EDT tree. It starts
3093 * with a list of busses, then a list of targets on a bus,
3094 * then devices/luns on a target, and then peripherals on a
3095 * device/lun. The "other" way is by the peripheral driver
3096 * lists. The peripheral driver lists are organized by
3097 * peripheral driver. (obviously) So it makes sense to
3098 * use the peripheral driver list if the user is looking
3099 * for something like "da1", or all "da" devices. If the
3100 * user is looking for something on a particular bus/target
3101 * or lun, it's generally better to go through the EDT tree.
3104 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3105 position_type = cdm->pos.position_type;
3109 position_type = CAM_DEV_POS_NONE;
3111 for (i = 0; i < cdm->num_patterns; i++) {
3112 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3113 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3114 position_type = CAM_DEV_POS_EDT;
3119 if (cdm->num_patterns == 0)
3120 position_type = CAM_DEV_POS_EDT;
3121 else if (position_type == CAM_DEV_POS_NONE)
3122 position_type = CAM_DEV_POS_PDRV;
3125 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3126 case CAM_DEV_POS_EDT:
3127 ret = xptedtmatch(cdm);
3129 case CAM_DEV_POS_PDRV:
3130 ret = xptperiphlistmatch(cdm);
3133 cdm->status = CAM_DEV_MATCH_ERROR;
3139 if (cdm->status == CAM_DEV_MATCH_ERROR)
3140 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3142 start_ccb->ccb_h.status = CAM_REQ_CMP;
3148 struct ccb_setasync *csa;
3149 struct async_node *cur_entry;
3150 struct async_list *async_head;
3154 csa = &start_ccb->csa;
3155 added = csa->event_enable;
3156 async_head = &csa->ccb_h.path->device->asyncs;
3159 * If there is already an entry for us, simply
3163 cur_entry = SLIST_FIRST(async_head);
3164 while (cur_entry != NULL) {
3165 if ((cur_entry->callback_arg == csa->callback_arg)
3166 && (cur_entry->callback == csa->callback))
3168 cur_entry = SLIST_NEXT(cur_entry, links);
3171 if (cur_entry != NULL) {
3173 * If the request has no flags set,
3176 added &= ~cur_entry->event_enable;
3177 if (csa->event_enable == 0) {
3178 SLIST_REMOVE(async_head, cur_entry,
3180 csa->ccb_h.path->device->refcount--;
3181 free(cur_entry, M_DEVBUF);
3183 cur_entry->event_enable = csa->event_enable;
3186 cur_entry = malloc(sizeof(*cur_entry),
3187 M_DEVBUF, M_INTWAIT);
3188 cur_entry->event_enable = csa->event_enable;
3189 cur_entry->callback_arg = csa->callback_arg;
3190 cur_entry->callback = csa->callback;
3191 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3192 csa->ccb_h.path->device->refcount++;
3195 if ((added & AC_FOUND_DEVICE) != 0) {
3197 * Get this peripheral up to date with all
3198 * the currently existing devices.
3200 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3202 if ((added & AC_PATH_REGISTERED) != 0) {
3204 * Get this peripheral up to date with all
3205 * the currently existing busses.
3207 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3210 start_ccb->ccb_h.status = CAM_REQ_CMP;
3215 struct ccb_relsim *crs;
3219 crs = &start_ccb->crs;
3220 dev = crs->ccb_h.path->device;
3223 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3229 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3231 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3233 /* Don't ever go below one opening */
3234 if (crs->openings > 0) {
3235 xpt_dev_ccbq_resize(crs->ccb_h.path,
3239 xpt_print_path(crs->ccb_h.path);
3240 printf("tagged openings "
3248 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3250 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3253 * Just extend the old timeout and decrement
3254 * the freeze count so that a single timeout
3255 * is sufficient for releasing the queue.
3257 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3258 untimeout(xpt_release_devq_timeout,
3259 dev, dev->c_handle);
3262 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3266 timeout(xpt_release_devq_timeout,
3268 (crs->release_timeout * hz) / 1000);
3270 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3274 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3276 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3278 * Decrement the freeze count so that a single
3279 * completion is still sufficient to unfreeze
3282 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3285 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3286 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3290 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3292 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3293 || (dev->ccbq.dev_active == 0)) {
3295 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3298 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3299 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3304 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3306 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3309 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3310 start_ccb->ccb_h.status = CAM_REQ_CMP;
3314 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3317 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3318 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3326 #ifdef CAM_DEBUG_DELAY
3327 cam_debug_delay = CAM_DEBUG_DELAY;
3329 cam_dflags = start_ccb->cdbg.flags;
3330 if (cam_dpath != NULL) {
3331 xpt_free_path(cam_dpath);
3335 if (cam_dflags != CAM_DEBUG_NONE) {
3336 if (xpt_create_path(&cam_dpath, xpt_periph,
3337 start_ccb->ccb_h.path_id,
3338 start_ccb->ccb_h.target_id,
3339 start_ccb->ccb_h.target_lun) !=
3341 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3342 cam_dflags = CAM_DEBUG_NONE;
3344 start_ccb->ccb_h.status = CAM_REQ_CMP;
3345 xpt_print_path(cam_dpath);
3346 printf("debugging flags now %x\n", cam_dflags);
3350 start_ccb->ccb_h.status = CAM_REQ_CMP;
3353 #else /* !CAMDEBUG */
3354 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3355 #endif /* CAMDEBUG */
3359 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3360 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3361 start_ccb->ccb_h.status = CAM_REQ_CMP;
3368 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3375 xpt_polled_action(union ccb *start_ccb)
3379 struct cam_sim *sim;
3380 struct cam_devq *devq;
3383 timeout = start_ccb->ccb_h.timeout;
3384 sim = start_ccb->ccb_h.path->bus->sim;
3386 dev = start_ccb->ccb_h.path->device;
3391 * Steal an opening so that no other queued requests
3392 * can get it before us while we simulate interrupts.
3394 dev->ccbq.devq_openings--;
3395 dev->ccbq.dev_openings--;
3397 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3398 && (--timeout > 0)) {
3400 (*(sim->sim_poll))(sim);
3405 dev->ccbq.devq_openings++;
3406 dev->ccbq.dev_openings++;
3409 xpt_action(start_ccb);
3410 while(--timeout > 0) {
3411 (*(sim->sim_poll))(sim);
3414 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3421 * XXX Is it worth adding a sim_timeout entry
3422 * point so we can attempt recovery? If
3423 * this is only used for dumps, I don't think
3426 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3429 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3435 * Schedule a peripheral driver to receive a ccb when it's
3436 * target device has space for more transactions.
3439 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3441 struct cam_ed *device;
3445 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3446 device = perph->path->device;
3448 if (periph_is_queued(perph)) {
3449 /* Simply reorder based on new priority */
3450 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3451 (" change priority to %d\n", new_priority));
3452 if (new_priority < perph->pinfo.priority) {
3453 camq_change_priority(&device->drvq,
3459 /* New entry on the queue */
3460 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3461 (" added periph to queue\n"));
3462 perph->pinfo.priority = new_priority;
3463 perph->pinfo.generation = ++device->drvq.generation;
3464 camq_insert(&device->drvq, &perph->pinfo);
3465 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3469 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3470 (" calling xpt_run_devq\n"));
3471 xpt_run_dev_allocq(perph->path->bus);
3477 * Schedule a device to run on a given queue.
3478 * If the device was inserted as a new entry on the queue,
3479 * return 1 meaning the device queue should be run. If we
3480 * were already queued, implying someone else has already
3481 * started the queue, return 0 so the caller doesn't attempt
3482 * to run the queue. Must be run at either splsoftcam
3483 * (or splcam since that encompases splsoftcam).
3486 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3487 u_int32_t new_priority)
3490 u_int32_t old_priority;
3492 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3494 old_priority = pinfo->priority;
3497 * Are we already queued?
3499 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3500 /* Simply reorder based on new priority */
3501 if (new_priority < old_priority) {
3502 camq_change_priority(queue, pinfo->index,
3504 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3505 ("changed priority to %d\n",
3510 /* New entry on the queue */
3511 if (new_priority < old_priority)
3512 pinfo->priority = new_priority;
3514 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3515 ("Inserting onto queue\n"));
3516 pinfo->generation = ++queue->generation;
3517 camq_insert(queue, pinfo);
3524 xpt_run_dev_allocq(struct cam_eb *bus)
3526 struct cam_devq *devq;
3529 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3530 devq = bus->sim->devq;
3532 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3533 (" qfrozen_cnt == 0x%x, entries == %d, "
3534 "openings == %d, active == %d\n",
3535 devq->alloc_queue.qfrozen_cnt,
3536 devq->alloc_queue.entries,
3537 devq->alloc_openings,
3538 devq->alloc_active));
3541 devq->alloc_queue.qfrozen_cnt++;
3542 while ((devq->alloc_queue.entries > 0)
3543 && (devq->alloc_openings > 0)
3544 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3545 struct cam_ed_qinfo *qinfo;
3546 struct cam_ed *device;
3547 union ccb *work_ccb;
3548 struct cam_periph *drv;
3551 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3553 device = qinfo->device;
3555 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3556 ("running device %p\n", device));
3558 drvq = &device->drvq;
3561 if (drvq->entries <= 0) {
3562 panic("xpt_run_dev_allocq: "
3563 "Device on queue without any work to do");
3566 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3567 devq->alloc_openings--;
3568 devq->alloc_active++;
3569 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3571 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3572 drv->pinfo.priority);
3573 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3574 ("calling periph start\n"));
3575 drv->periph_start(drv, work_ccb);
3578 * Malloc failure in alloc_ccb
3581 * XXX add us to a list to be run from free_ccb
3582 * if we don't have any ccbs active on this
3583 * device queue otherwise we may never get run
3589 /* Raise IPL for possible insertion and test at top of loop */
3592 if (drvq->entries > 0) {
3593 /* We have more work. Attempt to reschedule */
3594 xpt_schedule_dev_allocq(bus, device);
3597 devq->alloc_queue.qfrozen_cnt--;
3602 xpt_run_dev_sendq(struct cam_eb *bus)
3604 struct cam_devq *devq;
3607 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3609 devq = bus->sim->devq;
3612 devq->send_queue.qfrozen_cnt++;
3615 while ((devq->send_queue.entries > 0)
3616 && (devq->send_openings > 0)) {
3617 struct cam_ed_qinfo *qinfo;
3618 struct cam_ed *device;
3619 union ccb *work_ccb;
3620 struct cam_sim *sim;
3624 if (devq->send_queue.qfrozen_cnt > 1) {
3629 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3631 device = qinfo->device;
3634 * If the device has been "frozen", don't attempt
3637 if (device->qfrozen_cnt > 0) {
3642 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3643 ("running device %p\n", device));
3645 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3646 if (work_ccb == NULL) {
3647 printf("device on run queue with no ccbs???\n");
3652 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3654 if (num_highpower <= 0) {
3656 * We got a high power command, but we
3657 * don't have any available slots. Freeze
3658 * the device queue until we have a slot
3661 device->qfrozen_cnt++;
3662 STAILQ_INSERT_TAIL(&highpowerq,
3670 * Consume a high power slot while
3676 devq->active_dev = device;
3677 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3679 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3682 devq->send_openings--;
3683 devq->send_active++;
3685 if (device->ccbq.queue.entries > 0)
3686 xpt_schedule_dev_sendq(bus, device);
3688 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3690 * The client wants to freeze the queue
3691 * after this CCB is sent.
3694 device->qfrozen_cnt++;
3700 /* In Target mode, the peripheral driver knows best... */
3701 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3702 if ((device->inq_flags & SID_CmdQue) != 0
3703 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3704 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3707 * Clear this in case of a retried CCB that
3708 * failed due to a rejected tag.
3710 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3714 * Device queues can be shared among multiple sim instances
3715 * that reside on different busses. Use the SIM in the queue
3716 * CCB's path, rather than the one in the bus that was passed
3717 * into this function.
3719 sim = work_ccb->ccb_h.path->bus->sim;
3720 (*(sim->sim_action))(sim, work_ccb);
3723 devq->active_dev = NULL;
3725 /* Raise IPL for possible insertion and test at top of loop */
3730 devq->send_queue.qfrozen_cnt--;
3735 * This function merges stuff from the slave ccb into the master ccb, while
3736 * keeping important fields in the master ccb constant.
3739 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3742 * Pull fields that are valid for peripheral drivers to set
3743 * into the master CCB along with the CCB "payload".
3745 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3746 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3747 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3748 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3749 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3750 sizeof(union ccb) - sizeof(struct ccb_hdr));
3754 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3756 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3757 ccb_h->pinfo.priority = priority;
3759 ccb_h->path_id = path->bus->path_id;
3761 ccb_h->target_id = path->target->target_id;
3763 ccb_h->target_id = CAM_TARGET_WILDCARD;
3765 ccb_h->target_lun = path->device->lun_id;
3766 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3768 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3770 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3774 /* Path manipulation functions */
3776 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3777 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3779 struct cam_path *path;
3782 path = malloc(sizeof(*path), M_DEVBUF, M_INTWAIT);
3783 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3784 if (status != CAM_REQ_CMP) {
3785 free(path, M_DEVBUF);
3788 *new_path_ptr = path;
3793 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3794 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3797 struct cam_et *target;
3798 struct cam_ed *device;
3802 status = CAM_REQ_CMP; /* Completed without error */
3803 target = NULL; /* Wildcarded */
3804 device = NULL; /* Wildcarded */
3807 * We will potentially modify the EDT, so block interrupts
3808 * that may attempt to create cam paths.
3811 bus = xpt_find_bus(path_id);
3813 status = CAM_PATH_INVALID;
3815 target = xpt_find_target(bus, target_id);
3816 if (target == NULL) {
3818 struct cam_et *new_target;
3820 new_target = xpt_alloc_target(bus, target_id);
3821 if (new_target == NULL) {
3822 status = CAM_RESRC_UNAVAIL;
3824 target = new_target;
3827 if (target != NULL) {
3828 device = xpt_find_device(target, lun_id);
3829 if (device == NULL) {
3831 struct cam_ed *new_device;
3833 new_device = xpt_alloc_device(bus,
3836 if (new_device == NULL) {
3837 status = CAM_RESRC_UNAVAIL;
3839 device = new_device;
3847 * Only touch the user's data if we are successful.
3849 if (status == CAM_REQ_CMP) {
3850 new_path->periph = perph;
3851 new_path->bus = bus;
3852 new_path->target = target;
3853 new_path->device = device;
3854 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3857 xpt_release_device(bus, target, device);
3859 xpt_release_target(bus, target);
3861 xpt_release_bus(bus);
3867 xpt_release_path(struct cam_path *path)
3869 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3870 if (path->device != NULL) {
3871 xpt_release_device(path->bus, path->target, path->device);
3872 path->device = NULL;
3874 if (path->target != NULL) {
3875 xpt_release_target(path->bus, path->target);
3876 path->target = NULL;
3878 if (path->bus != NULL) {
3879 xpt_release_bus(path->bus);
3885 xpt_free_path(struct cam_path *path)
3887 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3888 xpt_release_path(path);
3889 free(path, M_DEVBUF);
3894 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3895 * in path1, 2 for match with wildcards in path2.
3898 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3902 if (path1->bus != path2->bus) {
3903 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3905 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3910 if (path1->target != path2->target) {
3911 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3914 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3919 if (path1->device != path2->device) {
3920 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3923 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3932 xpt_print_path(struct cam_path *path)
3935 printf("(nopath): ");
3937 if (path->periph != NULL)
3938 printf("(%s%d:", path->periph->periph_name,
3939 path->periph->unit_number);
3941 printf("(noperiph:");
3943 if (path->bus != NULL)
3944 printf("%s%d:%d:", path->bus->sim->sim_name,
3945 path->bus->sim->unit_number,
3946 path->bus->sim->bus_id);
3950 if (path->target != NULL)
3951 printf("%d:", path->target->target_id);
3955 if (path->device != NULL)
3956 printf("%d): ", path->device->lun_id);
3963 xpt_path_path_id(struct cam_path *path)
3965 return(path->bus->path_id);
3969 xpt_path_target_id(struct cam_path *path)
3971 if (path->target != NULL)
3972 return (path->target->target_id);
3974 return (CAM_TARGET_WILDCARD);
3978 xpt_path_lun_id(struct cam_path *path)
3980 if (path->device != NULL)
3981 return (path->device->lun_id);
3983 return (CAM_LUN_WILDCARD);
3987 xpt_path_sim(struct cam_path *path)
3989 return (path->bus->sim);
3993 xpt_path_periph(struct cam_path *path)
3995 return (path->periph);
3999 * Release a CAM control block for the caller. Remit the cost of the structure
4000 * to the device referenced by the path. If the this device had no 'credits'
4001 * and peripheral drivers have registered async callbacks for this notification
4005 xpt_release_ccb(union ccb *free_ccb)
4008 struct cam_path *path;
4009 struct cam_ed *device;
4012 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4013 path = free_ccb->ccb_h.path;
4014 device = path->device;
4017 cam_ccbq_release_opening(&device->ccbq);
4018 if (xpt_ccb_count > xpt_max_ccbs) {
4019 xpt_free_ccb(free_ccb);
4022 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4024 bus->sim->devq->alloc_openings++;
4025 bus->sim->devq->alloc_active--;
4026 /* XXX Turn this into an inline function - xpt_run_device?? */
4027 if ((device_is_alloc_queued(device) == 0)
4028 && (device->drvq.entries > 0)) {
4029 xpt_schedule_dev_allocq(bus, device);
4032 if (dev_allocq_is_runnable(bus->sim->devq))
4033 xpt_run_dev_allocq(bus);
4036 /* Functions accessed by SIM drivers */
4039 * A sim structure, listing the SIM entry points and instance
4040 * identification info is passed to xpt_bus_register to hook the SIM
4041 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4042 * for this new bus and places it in the array of busses and assigns
4043 * it a path_id. The path_id may be influenced by "hard wiring"
4044 * information specified by the user. Once interrupt services are
4045 * availible, the bus will be probed.
4048 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4050 struct cam_eb *new_bus;
4051 struct cam_eb *old_bus;
4052 struct ccb_pathinq cpi;
4056 new_bus = malloc(sizeof(*new_bus), M_DEVBUF, M_INTWAIT);
4058 if (strcmp(sim->sim_name, "xpt") != 0) {
4060 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4063 TAILQ_INIT(&new_bus->et_entries);
4064 new_bus->path_id = sim->path_id;
4066 timevalclear(&new_bus->last_reset);
4068 new_bus->refcount = 1; /* Held until a bus_deregister event */
4069 new_bus->generation = 0;
4071 old_bus = TAILQ_FIRST(&xpt_busses);
4072 while (old_bus != NULL
4073 && old_bus->path_id < new_bus->path_id)
4074 old_bus = TAILQ_NEXT(old_bus, links);
4075 if (old_bus != NULL)
4076 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4078 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4082 /* Notify interested parties */
4083 if (sim->path_id != CAM_XPT_PATH_ID) {
4084 struct cam_path path;
4086 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4087 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4088 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4089 cpi.ccb_h.func_code = XPT_PATH_INQ;
4090 xpt_action((union ccb *)&cpi);
4091 xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi);
4092 xpt_release_path(&path);
4094 return (CAM_SUCCESS);
4098 xpt_bus_deregister(path_id_t pathid)
4100 struct cam_path bus_path;
4103 status = xpt_compile_path(&bus_path, NULL, pathid,
4104 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4105 if (status != CAM_REQ_CMP)
4108 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4109 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4111 /* Release the reference count held while registered. */
4112 xpt_release_bus(bus_path.bus);
4113 xpt_release_path(&bus_path);
4115 return (CAM_REQ_CMP);
4119 xptnextfreepathid(void)
4126 bus = TAILQ_FIRST(&xpt_busses);
4128 /* Find an unoccupied pathid */
4130 && bus->path_id <= pathid) {
4131 if (bus->path_id == pathid)
4133 bus = TAILQ_NEXT(bus, links);
4137 * Ensure that this pathid is not reserved for
4138 * a bus that may be registered in the future.
4140 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4142 /* Start the search over */
4149 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4153 char buf[32], *strval;
4155 pathid = CAM_XPT_PATH_ID;
4156 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4158 while ((i = resource_locate(i, "scbus")) != -1) {
4159 dunit = resource_query_unit(i);
4160 if (dunit < 0) /* unwired?! */
4162 if (resource_string_value("scbus", dunit, "at", &strval) != 0)
4164 if (strcmp(buf, strval) != 0)
4166 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4167 if (sim_bus == val) {
4171 } else if (sim_bus == 0) {
4172 /* Unspecified matches bus 0 */
4176 printf("Ambiguous scbus configuration for %s%d "
4177 "bus %d, cannot wire down. The kernel "
4178 "config entry for scbus%d should "
4179 "specify a controller bus.\n"
4180 "Scbus will be assigned dynamically.\n",
4181 sim_name, sim_unit, sim_bus, dunit);
4186 if (pathid == CAM_XPT_PATH_ID)
4187 pathid = xptnextfreepathid();
4192 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4195 struct cam_et *target, *next_target;
4196 struct cam_ed *device, *next_device;
4199 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4202 * Most async events come from a CAM interrupt context. In
4203 * a few cases, the error recovery code at the peripheral layer,
4204 * which may run from our SWI or a process context, may signal
4205 * deferred events with a call to xpt_async. Ensure async
4206 * notifications are serialized by blocking cam interrupts.
4212 if (async_code == AC_BUS_RESET) {
4213 /* Update our notion of when the last reset occurred */
4214 microuptime(&bus->last_reset);
4217 for (target = TAILQ_FIRST(&bus->et_entries);
4219 target = next_target) {
4221 next_target = TAILQ_NEXT(target, links);
4223 if (path->target != target
4224 && path->target->target_id != CAM_TARGET_WILDCARD
4225 && target->target_id != CAM_TARGET_WILDCARD)
4228 if (async_code == AC_SENT_BDR) {
4229 /* Update our notion of when the last reset occurred */
4230 microuptime(&path->target->last_reset);
4233 for (device = TAILQ_FIRST(&target->ed_entries);
4235 device = next_device) {
4237 next_device = TAILQ_NEXT(device, links);
4239 if (path->device != device
4240 && path->device->lun_id != CAM_LUN_WILDCARD
4241 && device->lun_id != CAM_LUN_WILDCARD)
4244 xpt_dev_async(async_code, bus, target,
4247 xpt_async_bcast(&device->asyncs, async_code,
4253 * If this wasn't a fully wildcarded async, tell all
4254 * clients that want all async events.
4256 if (bus != xpt_periph->path->bus)
4257 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4263 xpt_async_bcast(struct async_list *async_head,
4264 u_int32_t async_code,
4265 struct cam_path *path, void *async_arg)
4267 struct async_node *cur_entry;
4269 cur_entry = SLIST_FIRST(async_head);
4270 while (cur_entry != NULL) {
4271 struct async_node *next_entry;
4273 * Grab the next list entry before we call the current
4274 * entry's callback. This is because the callback function
4275 * can delete its async callback entry.
4277 next_entry = SLIST_NEXT(cur_entry, links);
4278 if ((cur_entry->event_enable & async_code) != 0)
4279 cur_entry->callback(cur_entry->callback_arg,
4282 cur_entry = next_entry;
4287 * Handle any per-device event notifications that require action by the XPT.
4290 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4291 struct cam_ed *device, void *async_arg)
4294 struct cam_path newpath;
4297 * We only need to handle events for real devices.
4299 if (target->target_id == CAM_TARGET_WILDCARD
4300 || device->lun_id == CAM_LUN_WILDCARD)
4304 * We need our own path with wildcards expanded to
4305 * handle certain types of events.
4307 if ((async_code == AC_SENT_BDR)
4308 || (async_code == AC_BUS_RESET)
4309 || (async_code == AC_INQ_CHANGED))
4310 status = xpt_compile_path(&newpath, NULL,
4315 status = CAM_REQ_CMP_ERR;
4317 if (status == CAM_REQ_CMP) {
4320 * Allow transfer negotiation to occur in a
4321 * tag free environment.
4323 if (async_code == AC_SENT_BDR
4324 || async_code == AC_BUS_RESET)
4325 xpt_toggle_tags(&newpath);
4327 if (async_code == AC_INQ_CHANGED) {
4329 * We've sent a start unit command, or
4330 * something similar to a device that
4331 * may have caused its inquiry data to
4332 * change. So we re-scan the device to
4333 * refresh the inquiry data for it.
4335 xpt_scan_lun(newpath.periph, &newpath,
4336 CAM_EXPECT_INQ_CHANGE, NULL);
4338 xpt_release_path(&newpath);
4339 } else if (async_code == AC_LOST_DEVICE) {
4340 device->flags |= CAM_DEV_UNCONFIGURED;
4341 } else if (async_code == AC_TRANSFER_NEG) {
4342 struct ccb_trans_settings *settings;
4344 settings = (struct ccb_trans_settings *)async_arg;
4345 xpt_set_transfer_settings(settings, device,
4346 /*async_update*/TRUE);
4351 xpt_freeze_devq(struct cam_path *path, u_int count)
4354 struct ccb_hdr *ccbh;
4357 path->device->qfrozen_cnt += count;
4360 * Mark the last CCB in the queue as needing
4361 * to be requeued if the driver hasn't
4362 * changed it's state yet. This fixes a race
4363 * where a ccb is just about to be queued to
4364 * a controller driver when it's interrupt routine
4365 * freezes the queue. To completly close the
4366 * hole, controller drives must check to see
4367 * if a ccb's status is still CAM_REQ_INPROG
4368 * under spl protection just before they queue
4369 * the CCB. See ahc_action/ahc_freeze_devq for
4372 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4373 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4374 ccbh->status = CAM_REQUEUE_REQ;
4376 return (path->device->qfrozen_cnt);
4380 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4382 sim->devq->send_queue.qfrozen_cnt += count;
4383 if (sim->devq->active_dev != NULL) {
4384 struct ccb_hdr *ccbh;
4386 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4388 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4389 ccbh->status = CAM_REQUEUE_REQ;
4391 return (sim->devq->send_queue.qfrozen_cnt);
4395 xpt_release_devq_timeout(void *arg)
4397 struct cam_ed *device;
4399 device = (struct cam_ed *)arg;
4401 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4405 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4407 xpt_release_devq_device(path->device, count, run_queue);
4411 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4419 if (dev->qfrozen_cnt > 0) {
4421 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4422 dev->qfrozen_cnt -= count;
4423 if (dev->qfrozen_cnt == 0) {
4426 * No longer need to wait for a successful
4427 * command completion.
4429 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4432 * Remove any timeouts that might be scheduled
4433 * to release this queue.
4435 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4436 untimeout(xpt_release_devq_timeout, dev,
4438 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4442 * Now that we are unfrozen schedule the
4443 * device so any pending transactions are
4446 if ((dev->ccbq.queue.entries > 0)
4447 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4448 && (run_queue != 0)) {
4455 xpt_run_dev_sendq(dev->target->bus);
4460 xpt_release_simq(struct cam_sim *sim, int run_queue)
4465 sendq = &(sim->devq->send_queue);
4467 if (sendq->qfrozen_cnt > 0) {
4469 sendq->qfrozen_cnt--;
4470 if (sendq->qfrozen_cnt == 0) {
4474 * If there is a timeout scheduled to release this
4475 * sim queue, remove it. The queue frozen count is
4478 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4479 untimeout(xpt_release_simq_timeout, sim,
4481 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4483 bus = xpt_find_bus(sim->path_id);
4488 * Now that we are unfrozen run the send queue.
4490 xpt_run_dev_sendq(bus);
4492 xpt_release_bus(bus);
4500 xpt_release_simq_timeout(void *arg)
4502 struct cam_sim *sim;
4504 sim = (struct cam_sim *)arg;
4505 xpt_release_simq(sim, /* run_queue */ TRUE);
4509 xpt_done(union ccb *done_ccb)
4515 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4516 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4518 * Queue up the request for handling by our SWI handler
4519 * any of the "non-immediate" type of ccbs.
4521 switch (done_ccb->ccb_h.path->periph->type) {
4522 case CAM_PERIPH_BIO:
4523 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4525 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4528 case CAM_PERIPH_NET:
4529 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4531 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4544 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_INTWAIT);
4549 xpt_free_ccb(union ccb *free_ccb)
4551 free(free_ccb, M_DEVBUF);
4556 /* Private XPT functions */
4559 * Get a CAM control block for the caller. Charge the structure to the device
4560 * referenced by the path. If the this device has no 'credits' then the
4561 * device already has the maximum number of outstanding operations under way
4562 * and we return NULL. If we don't have sufficient resources to allocate more
4563 * ccbs, we also return NULL.
4566 xpt_get_ccb(struct cam_ed *device)
4572 if ((new_ccb = (union ccb *)ccb_freeq.slh_first) == NULL) {
4573 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_INTWAIT);
4574 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4575 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4579 cam_ccbq_take_opening(&device->ccbq);
4580 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4586 xpt_release_bus(struct cam_eb *bus)
4591 #ifdef XPT_DEBUG_RELEASE
4592 printf("xpt_release_bus(%p): %d %p\n",
4593 bus, bus->refcount, TAILQ_FIRST(&bus->et_entries));
4595 if ((--bus->refcount == 0)
4596 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4597 TAILQ_REMOVE(&xpt_busses, bus, links);
4600 free(bus, M_DEVBUF);
4605 static struct cam_et *
4606 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4608 struct cam_et *target;
4609 struct cam_et *cur_target;
4611 target = malloc(sizeof(*target), M_DEVBUF, M_INTWAIT);
4613 TAILQ_INIT(&target->ed_entries);
4615 target->target_id = target_id;
4616 target->refcount = 1;
4617 target->generation = 0;
4618 timevalclear(&target->last_reset);
4620 * Hold a reference to our parent bus so it
4621 * will not go away before we do.
4625 /* Insertion sort into our bus's target list */
4626 cur_target = TAILQ_FIRST(&bus->et_entries);
4627 while (cur_target != NULL && cur_target->target_id < target_id)
4628 cur_target = TAILQ_NEXT(cur_target, links);
4630 if (cur_target != NULL) {
4631 TAILQ_INSERT_BEFORE(cur_target, target, links);
4633 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4640 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4645 #ifdef XPT_DEBUG_RELEASE
4646 printf("xpt_release_target(%p,%p): %d %p\n",
4648 target->refcount, TAILQ_FIRST(&target->ed_entries));
4650 if ((--target->refcount == 0)
4651 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4652 TAILQ_REMOVE(&bus->et_entries, target, links);
4655 free(target, M_DEVBUF);
4656 xpt_release_bus(bus);
4661 static struct cam_ed *
4662 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4664 struct cam_ed *device;
4665 struct cam_devq *devq;
4668 /* Make space for us in the device queue on our bus */
4669 devq = bus->sim->devq;
4670 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4672 if (status != CAM_REQ_CMP) {
4675 device = malloc(sizeof(*device), M_DEVBUF, M_INTWAIT);
4678 if (device != NULL) {
4679 struct cam_ed *cur_device;
4681 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4682 device->alloc_ccb_entry.device = device;
4683 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4684 device->send_ccb_entry.device = device;
4685 device->target = target;
4686 device->lun_id = lun_id;
4687 /* Initialize our queues */
4688 if (camq_init(&device->drvq, 0) != 0) {
4689 free(device, M_DEVBUF);
4692 if (cam_ccbq_init(&device->ccbq,
4693 bus->sim->max_dev_openings) != 0) {
4694 camq_fini(&device->drvq);
4695 free(device, M_DEVBUF);
4698 SLIST_INIT(&device->asyncs);
4699 SLIST_INIT(&device->periphs);
4700 device->generation = 0;
4701 device->owner = NULL;
4703 * Take the default quirk entry until we have inquiry
4704 * data and can determine a better quirk to use.
4706 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4707 bzero(&device->inq_data, sizeof(device->inq_data));
4708 device->inq_flags = 0;
4709 device->queue_flags = 0;
4710 device->serial_num = NULL;
4711 device->serial_num_len = 0;
4712 device->qfrozen_cnt = 0;
4713 device->flags = CAM_DEV_UNCONFIGURED;
4714 device->tag_delay_count = 0;
4715 device->refcount = 1;
4716 callout_handle_init(&device->c_handle);
4719 * Hold a reference to our parent target so it
4720 * will not go away before we do.
4725 * XXX should be limited by number of CCBs this bus can
4728 xpt_max_ccbs += device->ccbq.devq_openings;
4729 /* Insertion sort into our target's device list */
4730 cur_device = TAILQ_FIRST(&target->ed_entries);
4731 while (cur_device != NULL && cur_device->lun_id < lun_id)
4732 cur_device = TAILQ_NEXT(cur_device, links);
4733 if (cur_device != NULL) {
4734 TAILQ_INSERT_BEFORE(cur_device, device, links);
4736 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4738 target->generation++;
4744 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4745 struct cam_ed *device)
4750 #ifdef XPT_DEBUG_RELEASE
4751 printf("xpt_release_device(%p,%p,%p): %d %08x\n",
4752 bus, target, device,
4753 device->refcount, device->flags);
4756 if ((--device->refcount == 0)
4757 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4758 struct cam_devq *devq;
4760 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4761 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4762 panic("Removing device while still queued for ccbs");
4764 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4765 device->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4766 untimeout(xpt_release_devq_timeout, device,
4770 TAILQ_REMOVE(&target->ed_entries, device,links);
4771 target->generation++;
4772 xpt_max_ccbs -= device->ccbq.devq_openings;
4773 /* Release our slot in the devq */
4774 devq = bus->sim->devq;
4775 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4777 free(device, M_DEVBUF);
4778 xpt_release_target(bus, target);
4784 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4794 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4795 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4796 if (result == CAM_REQ_CMP && (diff < 0)) {
4797 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4799 /* Adjust the global limit */
4800 xpt_max_ccbs += diff;
4805 static struct cam_eb *
4806 xpt_find_bus(path_id_t path_id)
4810 for (bus = TAILQ_FIRST(&xpt_busses);
4812 bus = TAILQ_NEXT(bus, links)) {
4813 if (bus->path_id == path_id) {
4821 static struct cam_et *
4822 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4824 struct cam_et *target;
4826 for (target = TAILQ_FIRST(&bus->et_entries);
4828 target = TAILQ_NEXT(target, links)) {
4829 if (target->target_id == target_id) {
4837 static struct cam_ed *
4838 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4840 struct cam_ed *device;
4842 for (device = TAILQ_FIRST(&target->ed_entries);
4844 device = TAILQ_NEXT(device, links)) {
4845 if (device->lun_id == lun_id) {
4854 union ccb *request_ccb;
4855 struct ccb_pathinq *cpi;
4857 } xpt_scan_bus_info;
4860 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
4861 * As the scan progresses, xpt_scan_bus is used as the
4862 * callback on completion function.
4865 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
4867 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4868 ("xpt_scan_bus\n"));
4869 switch (request_ccb->ccb_h.func_code) {
4872 xpt_scan_bus_info *scan_info;
4873 union ccb *work_ccb;
4874 struct cam_path *path;
4879 /* Find out the characteristics of the bus */
4880 work_ccb = xpt_alloc_ccb();
4881 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
4882 request_ccb->ccb_h.pinfo.priority);
4883 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4884 xpt_action(work_ccb);
4885 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4886 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
4887 xpt_free_ccb(work_ccb);
4888 xpt_done(request_ccb);
4892 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
4894 * Can't scan the bus on an adapter that
4895 * cannot perform the initiator role.
4897 request_ccb->ccb_h.status = CAM_REQ_CMP;
4898 xpt_free_ccb(work_ccb);
4899 xpt_done(request_ccb);
4903 /* Save some state for use while we probe for devices */
4904 scan_info = (xpt_scan_bus_info *)
4905 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_INTWAIT);
4906 scan_info->request_ccb = request_ccb;
4907 scan_info->cpi = &work_ccb->cpi;
4909 /* Cache on our stack so we can work asynchronously */
4910 max_target = scan_info->cpi->max_target;
4911 initiator_id = scan_info->cpi->initiator_id;
4914 * Don't count the initiator if the
4915 * initiator is addressable.
4917 scan_info->pending_count = max_target + 1;
4918 if (initiator_id <= max_target)
4919 scan_info->pending_count--;
4921 for (i = 0; i <= max_target; i++) {
4923 if (i == initiator_id)
4926 status = xpt_create_path(&path, xpt_periph,
4927 request_ccb->ccb_h.path_id,
4929 if (status != CAM_REQ_CMP) {
4930 printf("xpt_scan_bus: xpt_create_path failed"
4931 " with status %#x, bus scan halted\n",
4935 work_ccb = xpt_alloc_ccb();
4936 xpt_setup_ccb(&work_ccb->ccb_h, path,
4937 request_ccb->ccb_h.pinfo.priority);
4938 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
4939 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
4940 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
4941 work_ccb->crcn.flags = request_ccb->crcn.flags;
4943 printf("xpt_scan_bus: probing %d:%d:%d\n",
4944 request_ccb->ccb_h.path_id, i, 0);
4946 xpt_action(work_ccb);
4952 xpt_scan_bus_info *scan_info;
4954 target_id_t target_id;
4957 /* Reuse the same CCB to query if a device was really found */
4958 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
4959 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
4960 request_ccb->ccb_h.pinfo.priority);
4961 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
4963 path_id = request_ccb->ccb_h.path_id;
4964 target_id = request_ccb->ccb_h.target_id;
4965 lun_id = request_ccb->ccb_h.target_lun;
4966 xpt_action(request_ccb);
4969 printf("xpt_scan_bus: got back probe from %d:%d:%d\n",
4970 path_id, target_id, lun_id);
4973 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
4974 struct cam_ed *device;
4975 struct cam_et *target;
4979 * If we already probed lun 0 successfully, or
4980 * we have additional configured luns on this
4981 * target that might have "gone away", go onto
4984 target = request_ccb->ccb_h.path->target;
4986 * We may touch devices that we don't
4987 * hold references too, so ensure they
4988 * don't disappear out from under us.
4989 * The target above is referenced by the
4990 * path in the request ccb.
4994 device = TAILQ_FIRST(&target->ed_entries);
4995 if (device != NULL) {
4996 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
4997 if (device->lun_id == 0)
4998 device = TAILQ_NEXT(device, links);
5001 if ((lun_id != 0) || (device != NULL)) {
5002 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5006 struct cam_ed *device;
5008 device = request_ccb->ccb_h.path->device;
5010 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5011 /* Try the next lun */
5012 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5013 (device->quirk->quirks & CAM_QUIRK_HILUNS))
5018 xpt_free_path(request_ccb->ccb_h.path);
5021 if ((lun_id == request_ccb->ccb_h.target_lun)
5022 || lun_id > scan_info->cpi->max_lun) {
5025 xpt_free_ccb(request_ccb);
5026 scan_info->pending_count--;
5027 if (scan_info->pending_count == 0) {
5028 xpt_free_ccb((union ccb *)scan_info->cpi);
5029 request_ccb = scan_info->request_ccb;
5030 free(scan_info, M_TEMP);
5031 request_ccb->ccb_h.status = CAM_REQ_CMP;
5032 xpt_done(request_ccb);
5035 /* Try the next device */
5036 struct cam_path *path;
5039 path = request_ccb->ccb_h.path;
5040 status = xpt_create_path(&path, xpt_periph,
5041 path_id, target_id, lun_id);
5042 if (status != CAM_REQ_CMP) {
5043 printf("xpt_scan_bus: xpt_create_path failed "
5044 "with status %#x, halting LUN scan\n",
5046 xpt_free_ccb(request_ccb);
5047 scan_info->pending_count--;
5048 if (scan_info->pending_count == 0) {
5050 (union ccb *)scan_info->cpi);
5051 request_ccb = scan_info->request_ccb;
5052 free(scan_info, M_TEMP);
5053 request_ccb->ccb_h.status = CAM_REQ_CMP;
5054 xpt_done(request_ccb);
5058 xpt_setup_ccb(&request_ccb->ccb_h, path,
5059 request_ccb->ccb_h.pinfo.priority);
5060 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5061 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5062 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5063 request_ccb->crcn.flags =
5064 scan_info->request_ccb->crcn.flags;
5066 xpt_print_path(path);
5067 printf("xpt_scan bus probing\n");
5069 xpt_action(request_ccb);
5084 PROBE_TUR_FOR_NEGOTIATION
5088 PROBE_INQUIRY_CKSUM = 0x01,
5089 PROBE_SERIAL_CKSUM = 0x02,
5090 PROBE_NO_ANNOUNCE = 0x04
5094 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5095 probe_action action;
5096 union ccb saved_ccb;
5099 u_int8_t digest[16];
5103 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5104 cam_flags flags, union ccb *request_ccb)
5106 struct ccb_pathinq cpi;
5108 struct cam_path *new_path;
5109 struct cam_periph *old_periph;
5112 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5113 ("xpt_scan_lun\n"));
5115 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5116 cpi.ccb_h.func_code = XPT_PATH_INQ;
5117 xpt_action((union ccb *)&cpi);
5119 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5120 if (request_ccb != NULL) {
5121 request_ccb->ccb_h.status = cpi.ccb_h.status;
5122 xpt_done(request_ccb);
5127 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5129 * Can't scan the bus on an adapter that
5130 * cannot perform the initiator role.
5132 if (request_ccb != NULL) {
5133 request_ccb->ccb_h.status = CAM_REQ_CMP;
5134 xpt_done(request_ccb);
5139 if (request_ccb == NULL) {
5140 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_INTWAIT);
5141 new_path = malloc(sizeof(*new_path), M_TEMP, M_INTWAIT);
5142 status = xpt_compile_path(new_path, xpt_periph,
5144 path->target->target_id,
5145 path->device->lun_id);
5147 if (status != CAM_REQ_CMP) {
5148 xpt_print_path(path);
5149 printf("xpt_scan_lun: can't compile path, can't "
5151 free(request_ccb, M_TEMP);
5152 free(new_path, M_TEMP);
5155 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5156 request_ccb->ccb_h.cbfcnp = xptscandone;
5157 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5158 request_ccb->crcn.flags = flags;
5162 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5165 softc = (probe_softc *)old_periph->softc;
5166 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5169 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5170 probestart, "probe",
5172 request_ccb->ccb_h.path, NULL, 0,
5175 if (status != CAM_REQ_CMP) {
5176 xpt_print_path(path);
5177 printf("xpt_scan_lun: cam_alloc_periph returned an "
5178 "error, can't continue probe\n");
5179 request_ccb->ccb_h.status = status;
5180 xpt_done(request_ccb);
5187 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5189 xpt_release_path(done_ccb->ccb_h.path);
5190 free(done_ccb->ccb_h.path, M_TEMP);
5191 free(done_ccb, M_TEMP);
5195 proberegister(struct cam_periph *periph, void *arg)
5197 union ccb *request_ccb; /* CCB representing the probe request */
5200 request_ccb = (union ccb *)arg;
5201 if (periph == NULL) {
5202 printf("proberegister: periph was NULL!!\n");
5203 return(CAM_REQ_CMP_ERR);
5206 if (request_ccb == NULL) {
5207 printf("proberegister: no probe CCB, "
5208 "can't register device\n");
5209 return(CAM_REQ_CMP_ERR);
5212 softc = malloc(sizeof(*softc), M_TEMP, M_WAITOK | M_ZERO);
5213 TAILQ_INIT(&softc->request_ccbs);
5214 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5217 periph->softc = softc;
5218 cam_periph_acquire(periph);
5220 * Ensure we've waited at least a bus settle
5221 * delay before attempting to probe the device.
5222 * For HBAs that don't do bus resets, this won't make a difference.
5224 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5226 probeschedule(periph);
5227 return(CAM_REQ_CMP);
5231 probeschedule(struct cam_periph *periph)
5233 struct ccb_pathinq cpi;
5237 softc = (probe_softc *)periph->softc;
5238 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5240 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5241 cpi.ccb_h.func_code = XPT_PATH_INQ;
5242 xpt_action((union ccb *)&cpi);
5245 * If a device has gone away and another device, or the same one,
5246 * is back in the same place, it should have a unit attention
5247 * condition pending. It will not report the unit attention in
5248 * response to an inquiry, which may leave invalid transfer
5249 * negotiations in effect. The TUR will reveal the unit attention
5250 * condition. Only send the TUR for lun 0, since some devices
5251 * will get confused by commands other than inquiry to non-existent
5252 * luns. If you think a device has gone away start your scan from
5253 * lun 0. This will insure that any bogus transfer settings are
5256 * If we haven't seen the device before and the controller supports
5257 * some kind of transfer negotiation, negotiate with the first
5258 * sent command if no bus reset was performed at startup. This
5259 * ensures that the device is not confused by transfer negotiation
5260 * settings left over by loader or BIOS action.
5262 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5263 && (ccb->ccb_h.target_lun == 0)) {
5264 softc->action = PROBE_TUR;
5265 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5266 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5267 proberequestdefaultnegotiation(periph);
5268 softc->action = PROBE_INQUIRY;
5270 softc->action = PROBE_INQUIRY;
5273 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5274 softc->flags |= PROBE_NO_ANNOUNCE;
5276 softc->flags &= ~PROBE_NO_ANNOUNCE;
5278 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5282 probestart(struct cam_periph *periph, union ccb *start_ccb)
5284 /* Probe the device that our peripheral driver points to */
5285 struct ccb_scsiio *csio;
5288 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5290 softc = (probe_softc *)periph->softc;
5291 csio = &start_ccb->csio;
5293 switch (softc->action) {
5295 case PROBE_TUR_FOR_NEGOTIATION:
5297 scsi_test_unit_ready(csio,
5306 case PROBE_FULL_INQUIRY:
5309 struct scsi_inquiry_data *inq_buf;
5311 inq_buf = &periph->path->device->inq_data;
5313 * If the device is currently configured, we calculate an
5314 * MD5 checksum of the inquiry data, and if the serial number
5315 * length is greater than 0, add the serial number data
5316 * into the checksum as well. Once the inquiry and the
5317 * serial number check finish, we attempt to figure out
5318 * whether we still have the same device.
5320 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5322 MD5Init(&softc->context);
5323 MD5Update(&softc->context, (unsigned char *)inq_buf,
5324 sizeof(struct scsi_inquiry_data));
5325 softc->flags |= PROBE_INQUIRY_CKSUM;
5326 if (periph->path->device->serial_num_len > 0) {
5327 MD5Update(&softc->context,
5328 periph->path->device->serial_num,
5329 periph->path->device->serial_num_len);
5330 softc->flags |= PROBE_SERIAL_CKSUM;
5332 MD5Final(softc->digest, &softc->context);
5335 if (softc->action == PROBE_INQUIRY)
5336 inquiry_len = SHORT_INQUIRY_LENGTH;
5338 inquiry_len = inq_buf->additional_length + 5;
5344 (u_int8_t *)inq_buf,
5349 /*timeout*/60 * 1000);
5352 case PROBE_MODE_SENSE:
5357 mode_buf_len = sizeof(struct scsi_mode_header_6)
5358 + sizeof(struct scsi_mode_blk_desc)
5359 + sizeof(struct scsi_control_page);
5360 mode_buf = malloc(mode_buf_len, M_TEMP, M_INTWAIT);
5361 scsi_mode_sense(csio,
5366 SMS_PAGE_CTRL_CURRENT,
5367 SMS_CONTROL_MODE_PAGE,
5374 case PROBE_SERIAL_NUM:
5376 struct scsi_vpd_unit_serial_number *serial_buf;
5377 struct cam_ed* device;
5380 device = periph->path->device;
5381 device->serial_num = NULL;
5382 device->serial_num_len = 0;
5384 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0) {
5385 serial_buf = malloc(sizeof(*serial_buf), M_TEMP,
5386 M_INTWAIT | M_ZERO);
5391 (u_int8_t *)serial_buf,
5392 sizeof(*serial_buf),
5394 SVPD_UNIT_SERIAL_NUMBER,
5396 /*timeout*/60 * 1000);
5400 * We'll have to do without, let our probedone
5401 * routine finish up for us.
5403 start_ccb->csio.data_ptr = NULL;
5404 probedone(periph, start_ccb);
5408 xpt_action(start_ccb);
5412 proberequestdefaultnegotiation(struct cam_periph *periph)
5414 struct ccb_trans_settings cts;
5416 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5417 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5418 cts.flags = CCB_TRANS_USER_SETTINGS;
5419 xpt_action((union ccb *)&cts);
5420 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5421 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5422 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5423 xpt_action((union ccb *)&cts);
5427 probedone(struct cam_periph *periph, union ccb *done_ccb)
5430 struct cam_path *path;
5433 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5435 softc = (probe_softc *)periph->softc;
5436 path = done_ccb->ccb_h.path;
5437 priority = done_ccb->ccb_h.pinfo.priority;
5439 switch (softc->action) {
5442 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5444 if (cam_periph_error(done_ccb, 0,
5445 SF_NO_PRINT, NULL) == ERESTART)
5447 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5448 /* Don't wedge the queue */
5449 xpt_release_devq(done_ccb->ccb_h.path,
5453 softc->action = PROBE_INQUIRY;
5454 xpt_release_ccb(done_ccb);
5455 xpt_schedule(periph, priority);
5459 case PROBE_FULL_INQUIRY:
5461 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5462 struct scsi_inquiry_data *inq_buf;
5463 u_int8_t periph_qual;
5465 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5466 inq_buf = &path->device->inq_data;
5468 periph_qual = SID_QUAL(inq_buf);
5470 switch(periph_qual) {
5471 case SID_QUAL_LU_CONNECTED:
5476 * We conservatively request only
5477 * SHORT_INQUIRY_LEN bytes of inquiry
5478 * information during our first try
5479 * at sending an INQUIRY. If the device
5480 * has more information to give,
5481 * perform a second request specifying
5482 * the amount of information the device
5483 * is willing to give.
5485 alen = inq_buf->additional_length;
5486 if (softc->action == PROBE_INQUIRY
5487 && alen > (SHORT_INQUIRY_LENGTH - 5)) {
5488 softc->action = PROBE_FULL_INQUIRY;
5489 xpt_release_ccb(done_ccb);
5490 xpt_schedule(periph, priority);
5494 xpt_find_quirk(path->device);
5496 if ((inq_buf->flags & SID_CmdQue) != 0)
5497 softc->action = PROBE_MODE_SENSE;
5499 softc->action = PROBE_SERIAL_NUM;
5501 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5503 xpt_release_ccb(done_ccb);
5504 xpt_schedule(periph, priority);
5510 } else if (cam_periph_error(done_ccb, 0,
5511 done_ccb->ccb_h.target_lun > 0
5512 ? SF_RETRY_UA|SF_QUIET_IR
5514 &softc->saved_ccb) == ERESTART) {
5516 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5517 /* Don't wedge the queue */
5518 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5522 * If we get to this point, we got an error status back
5523 * from the inquiry and the error status doesn't require
5524 * automatically retrying the command. Therefore, the
5525 * inquiry failed. If we had inquiry information before
5526 * for this device, but this latest inquiry command failed,
5527 * the device has probably gone away. If this device isn't
5528 * already marked unconfigured, notify the peripheral
5529 * drivers that this device is no more.
5531 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5532 /* Send the async notification. */
5533 xpt_async(AC_LOST_DEVICE, path, NULL);
5535 xpt_release_ccb(done_ccb);
5538 case PROBE_MODE_SENSE:
5540 struct ccb_scsiio *csio;
5541 struct scsi_mode_header_6 *mode_hdr;
5543 csio = &done_ccb->csio;
5544 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5545 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5546 struct scsi_control_page *page;
5549 offset = ((u_int8_t *)&mode_hdr[1])
5550 + mode_hdr->blk_desc_len;
5551 page = (struct scsi_control_page *)offset;
5552 path->device->queue_flags = page->queue_flags;
5553 } else if (cam_periph_error(done_ccb, 0,
5554 SF_RETRY_UA|SF_NO_PRINT,
5555 &softc->saved_ccb) == ERESTART) {
5557 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5558 /* Don't wedge the queue */
5559 xpt_release_devq(done_ccb->ccb_h.path,
5560 /*count*/1, /*run_queue*/TRUE);
5562 xpt_release_ccb(done_ccb);
5563 free(mode_hdr, M_TEMP);
5564 softc->action = PROBE_SERIAL_NUM;
5565 xpt_schedule(periph, priority);
5568 case PROBE_SERIAL_NUM:
5570 struct ccb_scsiio *csio;
5571 struct scsi_vpd_unit_serial_number *serial_buf;
5578 csio = &done_ccb->csio;
5579 priority = done_ccb->ccb_h.pinfo.priority;
5581 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5583 /* Clean up from previous instance of this device */
5584 if (path->device->serial_num != NULL) {
5585 free(path->device->serial_num, M_DEVBUF);
5586 path->device->serial_num = NULL;
5587 path->device->serial_num_len = 0;
5590 if (serial_buf == NULL) {
5592 * Don't process the command as it was never sent
5594 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5595 && (serial_buf->length > 0)) {
5598 path->device->serial_num =
5599 malloc((serial_buf->length + 1),
5600 M_DEVBUF, M_INTWAIT);
5601 bcopy(serial_buf->serial_num,
5602 path->device->serial_num,
5603 serial_buf->length);
5604 path->device->serial_num_len = serial_buf->length;
5605 path->device->serial_num[serial_buf->length] = '\0';
5606 } else if (cam_periph_error(done_ccb, 0,
5607 SF_RETRY_UA|SF_NO_PRINT,
5608 &softc->saved_ccb) == ERESTART) {
5610 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5611 /* Don't wedge the queue */
5612 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5617 * Let's see if we have seen this device before.
5619 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5621 u_int8_t digest[16];
5626 (unsigned char *)&path->device->inq_data,
5627 sizeof(struct scsi_inquiry_data));
5630 MD5Update(&context, serial_buf->serial_num,
5631 serial_buf->length);
5633 MD5Final(digest, &context);
5634 if (bcmp(softc->digest, digest, 16) == 0)
5638 * XXX Do we need to do a TUR in order to ensure
5639 * that the device really hasn't changed???
5642 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5643 xpt_async(AC_LOST_DEVICE, path, NULL);
5645 if (serial_buf != NULL)
5646 free(serial_buf, M_TEMP);
5650 * Now that we have all the necessary
5651 * information to safely perform transfer
5652 * negotiations... Controllers don't perform
5653 * any negotiation or tagged queuing until
5654 * after the first XPT_SET_TRAN_SETTINGS ccb is
5655 * received. So, on a new device, just retreive
5656 * the user settings, and set them as the current
5657 * settings to set the device up.
5659 proberequestdefaultnegotiation(periph);
5660 xpt_release_ccb(done_ccb);
5663 * Perform a TUR to allow the controller to
5664 * perform any necessary transfer negotiation.
5666 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5667 xpt_schedule(periph, priority);
5670 xpt_release_ccb(done_ccb);
5673 case PROBE_TUR_FOR_NEGOTIATION:
5674 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5675 /* Don't wedge the queue */
5676 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5680 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5682 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5683 /* Inform the XPT that a new device has been found */
5684 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5685 xpt_action(done_ccb);
5687 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5689 xpt_release_ccb(done_ccb);
5692 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5693 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5694 done_ccb->ccb_h.status = CAM_REQ_CMP;
5696 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5697 cam_periph_invalidate(periph);
5698 cam_periph_release(periph);
5700 probeschedule(periph);
5705 probecleanup(struct cam_periph *periph)
5707 free(periph->softc, M_TEMP);
5711 xpt_find_quirk(struct cam_ed *device)
5715 match = cam_quirkmatch((caddr_t)&device->inq_data,
5716 (caddr_t)xpt_quirk_table,
5717 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5718 sizeof(*xpt_quirk_table), scsi_inquiry_match);
5721 panic("xpt_find_quirk: device didn't match wildcard entry!!");
5723 device->quirk = (struct xpt_quirk_entry *)match;
5727 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
5730 struct cam_sim *sim;
5733 sim = cts->ccb_h.path->bus->sim;
5734 if (async_update == FALSE) {
5735 struct scsi_inquiry_data *inq_data;
5736 struct ccb_pathinq cpi;
5737 struct ccb_trans_settings cur_cts;
5739 if (device == NULL) {
5740 cts->ccb_h.status = CAM_PATH_INVALID;
5741 xpt_done((union ccb *)cts);
5746 * Perform sanity checking against what the
5747 * controller and device can do.
5749 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
5750 cpi.ccb_h.func_code = XPT_PATH_INQ;
5751 xpt_action((union ccb *)&cpi);
5752 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
5753 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5754 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
5755 xpt_action((union ccb *)&cur_cts);
5756 inq_data = &device->inq_data;
5758 /* Fill in any gaps in what the user gave us */
5759 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
5760 cts->sync_period = cur_cts.sync_period;
5761 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
5762 cts->sync_offset = cur_cts.sync_offset;
5763 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
5764 cts->bus_width = cur_cts.bus_width;
5765 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
5766 cts->flags &= ~CCB_TRANS_DISC_ENB;
5767 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
5769 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
5770 cts->flags &= ~CCB_TRANS_TAG_ENB;
5771 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
5774 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5775 && (inq_data->flags & SID_Sync) == 0)
5776 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
5777 || (cts->sync_offset == 0)
5778 || (cts->sync_period == 0)) {
5780 cts->sync_period = 0;
5781 cts->sync_offset = 0;
5782 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
5784 if ((inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
5785 && cts->sync_period <= 0x9) {
5787 * Don't allow DT transmission rates if the
5788 * device does not support it.
5790 cts->sync_period = 0xa;
5792 if ((inq_data->spi3data & SID_SPI_IUS) == 0
5793 && cts->sync_period <= 0x8) {
5795 * Don't allow PACE transmission rates
5796 * if the device does support packetized
5799 cts->sync_period = 0x9;
5803 switch (cts->bus_width) {
5804 case MSG_EXT_WDTR_BUS_32_BIT:
5805 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5806 || (inq_data->flags & SID_WBus32) != 0)
5807 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
5809 /* Fall Through to 16-bit */
5810 case MSG_EXT_WDTR_BUS_16_BIT:
5811 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5812 || (inq_data->flags & SID_WBus16) != 0)
5813 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
5814 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5817 /* Fall Through to 8-bit */
5818 default: /* New bus width?? */
5819 case MSG_EXT_WDTR_BUS_8_BIT:
5820 /* All targets can do this */
5821 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5825 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
5827 * Can't tag queue without disconnection.
5829 cts->flags &= ~CCB_TRANS_TAG_ENB;
5830 cts->valid |= CCB_TRANS_TQ_VALID;
5833 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
5834 || (inq_data->flags & SID_CmdQue) == 0
5835 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
5836 || (device->quirk->mintags == 0)) {
5838 * Can't tag on hardware that doesn't support,
5839 * doesn't have it enabled, or has broken tag support.
5841 cts->flags &= ~CCB_TRANS_TAG_ENB;
5846 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
5850 * If we are transitioning from tags to no-tags or
5851 * vice-versa, we need to carefully freeze and restart
5852 * the queue so that we don't overlap tagged and non-tagged
5853 * commands. We also temporarily stop tags if there is
5854 * a change in transfer negotiation settings to allow
5855 * "tag-less" negotiation.
5857 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5858 || (device->inq_flags & SID_CmdQue) != 0)
5859 device_tagenb = TRUE;
5861 device_tagenb = FALSE;
5863 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
5864 && device_tagenb == FALSE)
5865 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
5866 && device_tagenb == TRUE)) {
5868 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
5870 * Delay change to use tags until after a
5871 * few commands have gone to this device so
5872 * the controller has time to perform transfer
5873 * negotiations without tagged messages getting
5876 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
5877 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
5879 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
5881 device->inq_flags &= ~SID_CmdQue;
5882 xpt_dev_ccbq_resize(cts->ccb_h.path,
5883 sim->max_dev_openings);
5884 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5885 device->tag_delay_count = 0;
5890 if (async_update == FALSE) {
5892 * If we are currently performing tagged transactions to
5893 * this device and want to change its negotiation parameters,
5894 * go non-tagged for a bit to give the controller a chance to
5895 * negotiate unhampered by tag messages.
5897 if ((device->inq_flags & SID_CmdQue) != 0
5898 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
5899 CCB_TRANS_SYNC_OFFSET_VALID|
5900 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
5901 xpt_toggle_tags(cts->ccb_h.path);
5903 (*(sim->sim_action))(sim, (union ccb *)cts);
5907 struct ccb_relsim crs;
5909 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
5911 crs.ccb_h.func_code = XPT_REL_SIMQ;
5912 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5914 = crs.release_timeout
5917 xpt_action((union ccb *)&crs);
5922 xpt_toggle_tags(struct cam_path *path)
5927 * Give controllers a chance to renegotiate
5928 * before starting tag operations. We
5929 * "toggle" tagged queuing off then on
5930 * which causes the tag enable command delay
5931 * counter to come into effect.
5934 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5935 || ((dev->inq_flags & SID_CmdQue) != 0
5936 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
5937 struct ccb_trans_settings cts;
5939 xpt_setup_ccb(&cts.ccb_h, path, 1);
5941 cts.valid = CCB_TRANS_TQ_VALID;
5942 xpt_set_transfer_settings(&cts, path->device,
5943 /*async_update*/TRUE);
5944 cts.flags = CCB_TRANS_TAG_ENB;
5945 xpt_set_transfer_settings(&cts, path->device,
5946 /*async_update*/TRUE);
5951 xpt_start_tags(struct cam_path *path)
5953 struct ccb_relsim crs;
5954 struct cam_ed *device;
5955 struct cam_sim *sim;
5958 device = path->device;
5959 sim = path->bus->sim;
5960 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5961 xpt_freeze_devq(path, /*count*/1);
5962 device->inq_flags |= SID_CmdQue;
5963 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
5964 xpt_dev_ccbq_resize(path, newopenings);
5965 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
5966 crs.ccb_h.func_code = XPT_REL_SIMQ;
5967 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5969 = crs.release_timeout
5972 xpt_action((union ccb *)&crs);
5975 static int busses_to_config;
5976 static int busses_to_reset;
5979 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
5981 if (bus->path_id != CAM_XPT_PATH_ID) {
5982 struct cam_path path;
5983 struct ccb_pathinq cpi;
5987 xpt_compile_path(&path, NULL, bus->path_id,
5988 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5989 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
5990 cpi.ccb_h.func_code = XPT_PATH_INQ;
5991 xpt_action((union ccb *)&cpi);
5992 can_negotiate = cpi.hba_inquiry;
5993 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
5994 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
5997 xpt_release_path(&path);
6004 xptconfigfunc(struct cam_eb *bus, void *arg)
6006 struct cam_path *path;
6007 union ccb *work_ccb;
6009 if (bus->path_id != CAM_XPT_PATH_ID) {
6013 work_ccb = xpt_alloc_ccb();
6014 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6015 CAM_TARGET_WILDCARD,
6016 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6017 printf("xptconfigfunc: xpt_create_path failed with "
6018 "status %#x for bus %d\n", status, bus->path_id);
6019 printf("xptconfigfunc: halting bus configuration\n");
6020 xpt_free_ccb(work_ccb);
6022 xpt_finishconfig(xpt_periph, NULL);
6025 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6026 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6027 xpt_action(work_ccb);
6028 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6029 printf("xptconfigfunc: CPI failed on bus %d "
6030 "with status %d\n", bus->path_id,
6031 work_ccb->ccb_h.status);
6032 xpt_finishconfig(xpt_periph, work_ccb);
6036 can_negotiate = work_ccb->cpi.hba_inquiry;
6037 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6038 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6039 && (can_negotiate != 0)) {
6040 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6041 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6042 work_ccb->ccb_h.cbfcnp = NULL;
6043 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6044 ("Resetting Bus\n"));
6045 xpt_action(work_ccb);
6046 xpt_finishconfig(xpt_periph, work_ccb);
6048 /* Act as though we performed a successful BUS RESET */
6049 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6050 xpt_finishconfig(xpt_periph, work_ccb);
6058 xpt_config(void *arg)
6060 /* Now that interrupts are enabled, go find our devices */
6063 /* Setup debugging flags and path */
6064 #ifdef CAM_DEBUG_FLAGS
6065 cam_dflags = CAM_DEBUG_FLAGS;
6066 #else /* !CAM_DEBUG_FLAGS */
6067 cam_dflags = CAM_DEBUG_NONE;
6068 #endif /* CAM_DEBUG_FLAGS */
6069 #ifdef CAM_DEBUG_BUS
6070 if (cam_dflags != CAM_DEBUG_NONE) {
6071 if (xpt_create_path(&cam_dpath, xpt_periph,
6072 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6073 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6074 printf("xpt_config: xpt_create_path() failed for debug"
6075 " target %d:%d:%d, debugging disabled\n",
6076 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6077 cam_dflags = CAM_DEBUG_NONE;
6081 #else /* !CAM_DEBUG_BUS */
6083 #endif /* CAM_DEBUG_BUS */
6084 #endif /* CAMDEBUG */
6087 * Scan all installed busses.
6089 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6091 if (busses_to_config == 0) {
6092 /* Call manually because we don't have any busses */
6093 xpt_finishconfig(xpt_periph, NULL);
6095 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6096 printf("Waiting %d seconds for SCSI "
6097 "devices to settle\n", SCSI_DELAY/1000);
6099 xpt_for_all_busses(xptconfigfunc, NULL);
6104 * If the given device only has one peripheral attached to it, and if that
6105 * peripheral is the passthrough driver, announce it. This insures that the
6106 * user sees some sort of announcement for every peripheral in their system.
6109 xptpassannouncefunc(struct cam_ed *device, void *arg)
6111 struct cam_periph *periph;
6114 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6115 periph = SLIST_NEXT(periph, periph_links), i++);
6117 periph = SLIST_FIRST(&device->periphs);
6119 && (strncmp(periph->periph_name, "pass", 4) == 0))
6120 xpt_announce_periph(periph, NULL);
6126 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6128 struct periph_driver **p_drv;
6130 if (done_ccb != NULL) {
6131 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6132 ("xpt_finishconfig\n"));
6133 switch(done_ccb->ccb_h.func_code) {
6135 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6136 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6137 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6138 xpt_action(done_ccb);
6144 xpt_free_path(done_ccb->ccb_h.path);
6150 if (busses_to_config == 0) {
6151 /* Register all the peripheral drivers */
6152 /* XXX This will have to change when we have loadable modules */
6153 SET_FOREACH(p_drv, periphdriver_set) {
6158 * Check for devices with no "standard" peripheral driver
6159 * attached. For any devices like that, announce the
6160 * passthrough driver so the user will see something.
6162 xpt_for_all_devices(xptpassannouncefunc, NULL);
6164 /* Release our hook so that the boot can continue. */
6165 config_intrhook_disestablish(xpt_config_hook);
6166 free(xpt_config_hook, M_TEMP);
6167 xpt_config_hook = NULL;
6169 if (done_ccb != NULL)
6170 xpt_free_ccb(done_ccb);
6174 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6176 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6178 switch (work_ccb->ccb_h.func_code) {
6179 /* Common cases first */
6180 case XPT_PATH_INQ: /* Path routing inquiry */
6182 struct ccb_pathinq *cpi;
6184 cpi = &work_ccb->cpi;
6185 cpi->version_num = 1; /* XXX??? */
6186 cpi->hba_inquiry = 0;
6187 cpi->target_sprt = 0;
6189 cpi->hba_eng_cnt = 0;
6190 cpi->max_target = 0;
6192 cpi->initiator_id = 0;
6193 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6194 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6195 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6196 cpi->unit_number = sim->unit_number;
6197 cpi->bus_id = sim->bus_id;
6198 cpi->base_transfer_speed = 0;
6199 cpi->ccb_h.status = CAM_REQ_CMP;
6204 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6211 * The xpt as a "controller" has no interrupt sources, so polling
6215 xptpoll(struct cam_sim *sim)
6220 * Should only be called by the machine interrupt dispatch routines,
6221 * so put these prototypes here instead of in the header.
6225 swi_camnet(void *arg)
6231 swi_cambio(void *arg)
6237 camisr(cam_isrq_t *queue)
6240 struct ccb_hdr *ccb_h;
6243 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6246 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6247 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6250 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6255 if (ccb_h->flags & CAM_HIGH_POWER) {
6256 struct highpowerlist *hphead;
6257 struct cam_ed *device;
6258 union ccb *send_ccb;
6260 hphead = &highpowerq;
6262 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6265 * Increment the count since this command is done.
6270 * Any high powered commands queued up?
6272 if (send_ccb != NULL) {
6273 device = send_ccb->ccb_h.path->device;
6275 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6277 xpt_release_devq(send_ccb->ccb_h.path,
6278 /*count*/1, /*runqueue*/TRUE);
6281 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6284 dev = ccb_h->path->device;
6287 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6289 ccb_h->path->bus->sim->devq->send_active--;
6290 ccb_h->path->bus->sim->devq->send_openings++;
6293 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6294 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6295 && (dev->ccbq.dev_active == 0))) {
6297 xpt_release_devq(ccb_h->path, /*count*/1,
6301 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6302 && (--dev->tag_delay_count == 0))
6303 xpt_start_tags(ccb_h->path);
6305 if ((dev->ccbq.queue.entries > 0)
6306 && (dev->qfrozen_cnt == 0)
6307 && (device_is_send_queued(dev) == 0)) {
6308 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6313 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6314 xpt_release_simq(ccb_h->path->bus->sim,
6316 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6320 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6321 && (ccb_h->status & CAM_DEV_QFRZN)) {
6322 xpt_release_devq(ccb_h->path, /*count*/1,
6324 ccb_h->status &= ~CAM_DEV_QFRZN;
6326 xpt_run_dev_sendq(ccb_h->path->bus);
6329 /* Call the peripheral driver's callback */
6330 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6332 /* Raise IPL for while test */