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.15 2004/05/19 22:52:37 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>
44 #include <sys/thread.h>
45 #include <sys/thread2.h>
48 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
51 #include <machine/clock.h>
52 #include <machine/ipl.h>
56 #include "cam_periph.h"
59 #include "cam_xpt_sim.h"
60 #include "cam_xpt_periph.h"
61 #include "cam_debug.h"
63 #include "scsi/scsi_all.h"
64 #include "scsi/scsi_message.h"
65 #include "scsi/scsi_pass.h"
68 /* Datastructures internal to the xpt layer */
71 * Definition of an async handler callback block. These are used to add
72 * SIMs and peripherals to the async callback lists.
75 SLIST_ENTRY(async_node) links;
76 u_int32_t event_enable; /* Async Event enables */
77 void (*callback)(void *arg, u_int32_t code,
78 struct cam_path *path, void *args);
82 SLIST_HEAD(async_list, async_node);
83 SLIST_HEAD(periph_list, cam_periph);
84 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
87 * This is the maximum number of high powered commands (e.g. start unit)
88 * that can be outstanding at a particular time.
90 #ifndef CAM_MAX_HIGHPOWER
91 #define CAM_MAX_HIGHPOWER 4
94 /* number of high powered commands that can go through right now */
95 static int num_highpower = CAM_MAX_HIGHPOWER;
98 * Structure for queueing a device in a run queue.
99 * There is one run queue for allocating new ccbs,
100 * and another for sending ccbs to the controller.
102 struct cam_ed_qinfo {
104 struct cam_ed *device;
108 * The CAM EDT (Existing Device Table) contains the device information for
109 * all devices for all busses in the system. The table contains a
110 * cam_ed structure for each device on the bus.
113 TAILQ_ENTRY(cam_ed) links;
114 struct cam_ed_qinfo alloc_ccb_entry;
115 struct cam_ed_qinfo send_ccb_entry;
116 struct cam_et *target;
119 * Queue of type drivers wanting to do
120 * work on this device.
122 struct cam_ccbq ccbq; /* Queue of pending ccbs */
123 struct async_list asyncs; /* Async callback info for this B/T/L */
124 struct periph_list periphs; /* All attached devices */
125 u_int generation; /* Generation number */
126 struct cam_periph *owner; /* Peripheral driver's ownership tag */
127 struct xpt_quirk_entry *quirk; /* Oddities about this device */
128 /* Storage for the inquiry data */
129 struct scsi_inquiry_data inq_data;
130 u_int8_t inq_flags; /*
131 * Current settings for inquiry flags.
132 * This allows us to override settings
133 * like disconnection and tagged
134 * queuing for a device.
136 u_int8_t queue_flags; /* Queue flags from the control page */
137 u_int8_t serial_num_len;
138 u_int8_t *serial_num;
139 u_int32_t qfrozen_cnt;
141 #define CAM_DEV_UNCONFIGURED 0x01
142 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
143 #define CAM_DEV_REL_ON_COMPLETE 0x04
144 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
145 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
146 #define CAM_DEV_TAG_AFTER_COUNT 0x20
147 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
148 u_int32_t tag_delay_count;
149 #define CAM_TAG_DELAY_COUNT 5
151 struct callout_handle c_handle;
155 * Each target is represented by an ET (Existing Target). These
156 * entries are created when a target is successfully probed with an
157 * identify, and removed when a device fails to respond after a number
158 * of retries, or a bus rescan finds the device missing.
161 TAILQ_HEAD(, cam_ed) ed_entries;
162 TAILQ_ENTRY(cam_et) links;
164 target_id_t target_id;
167 struct timeval last_reset; /* uptime of last reset */
171 * Each bus is represented by an EB (Existing Bus). These entries
172 * are created by calls to xpt_bus_register and deleted by calls to
173 * xpt_bus_deregister.
176 TAILQ_HEAD(, cam_et) et_entries;
177 TAILQ_ENTRY(cam_eb) links;
180 struct timeval last_reset; /* uptime of last reset */
182 #define CAM_EB_RUNQ_SCHEDULED 0x01
188 struct cam_periph *periph;
190 struct cam_et *target;
191 struct cam_ed *device;
194 struct xpt_quirk_entry {
195 struct scsi_inquiry_pattern inq_pat;
197 #define CAM_QUIRK_NOLUNS 0x01
198 #define CAM_QUIRK_NOSERIAL 0x02
199 #define CAM_QUIRK_HILUNS 0x04
203 #define CAM_SCSI2_MAXLUN 8
211 u_int32_t generation;
214 static const char quantum[] = "QUANTUM";
215 static const char sony[] = "SONY";
216 static const char west_digital[] = "WDIGTL";
217 static const char samsung[] = "SAMSUNG";
218 static const char seagate[] = "SEAGATE";
219 static const char microp[] = "MICROP";
221 static struct xpt_quirk_entry xpt_quirk_table[] =
224 /* Reports QUEUE FULL for temporary resource shortages */
225 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
226 /*quirks*/0, /*mintags*/24, /*maxtags*/32
229 /* Reports QUEUE FULL for temporary resource shortages */
230 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
231 /*quirks*/0, /*mintags*/24, /*maxtags*/32
234 /* Reports QUEUE FULL for temporary resource shortages */
235 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
236 /*quirks*/0, /*mintags*/24, /*maxtags*/32
239 /* Broken tagged queuing drive */
240 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
241 /*quirks*/0, /*mintags*/0, /*maxtags*/0
244 /* Broken tagged queuing drive */
245 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
246 /*quirks*/0, /*mintags*/0, /*maxtags*/0
249 /* Broken tagged queuing drive */
250 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
251 /*quirks*/0, /*mintags*/0, /*maxtags*/0
255 * Unfortunately, the Quantum Atlas III has the same
256 * problem as the Atlas II drives above.
257 * Reported by: "Johan Granlund" <johan@granlund.nu>
259 * For future reference, the drive with the problem was:
260 * QUANTUM QM39100TD-SW N1B0
262 * It's possible that Quantum will fix the problem in later
263 * firmware revisions. If that happens, the quirk entry
264 * will need to be made specific to the firmware revisions
268 /* Reports QUEUE FULL for temporary resource shortages */
269 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
270 /*quirks*/0, /*mintags*/24, /*maxtags*/32
274 * 18 Gig Atlas III, same problem as the 9G version.
275 * Reported by: Andre Albsmeier
276 * <andre.albsmeier@mchp.siemens.de>
278 * For future reference, the drive with the problem was:
279 * QUANTUM QM318000TD-S N491
281 /* Reports QUEUE FULL for temporary resource shortages */
282 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
283 /*quirks*/0, /*mintags*/24, /*maxtags*/32
287 * Broken tagged queuing drive
288 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
289 * and: Martin Renters <martin@tdc.on.ca>
291 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
292 /*quirks*/0, /*mintags*/0, /*maxtags*/0
295 * The Seagate Medalist Pro drives have very poor write
296 * performance with anything more than 2 tags.
298 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
299 * Drive: <SEAGATE ST36530N 1444>
301 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
302 * Drive: <SEAGATE ST34520W 1281>
304 * No one has actually reported that the 9G version
305 * (ST39140*) of the Medalist Pro has the same problem, but
306 * we're assuming that it does because the 4G and 6.5G
307 * versions of the drive are broken.
310 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
311 /*quirks*/0, /*mintags*/2, /*maxtags*/2
314 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
315 /*quirks*/0, /*mintags*/2, /*maxtags*/2
318 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
319 /*quirks*/0, /*mintags*/2, /*maxtags*/2
323 * Slow when tagged queueing is enabled. Write performance
324 * steadily drops off with more and more concurrent
325 * transactions. Best sequential write performance with
326 * tagged queueing turned off and write caching turned on.
329 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
330 * Drive: DCAS-34330 w/ "S65A" firmware.
332 * The drive with the problem had the "S65A" firmware
333 * revision, and has also been reported (by Stephen J.
334 * Roznowski <sjr@home.net>) for a drive with the "S61A"
337 * Although no one has reported problems with the 2 gig
338 * version of the DCAS drive, the assumption is that it
339 * has the same problems as the 4 gig version. Therefore
340 * this quirk entries disables tagged queueing for all
343 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
344 /*quirks*/0, /*mintags*/0, /*maxtags*/0
347 /* Broken tagged queuing drive */
348 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
349 /*quirks*/0, /*mintags*/0, /*maxtags*/0
352 /* Broken tagged queuing drive */
353 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
354 /*quirks*/0, /*mintags*/0, /*maxtags*/0
358 * Broken tagged queuing drive.
360 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
363 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
364 /*quirks*/0, /*mintags*/0, /*maxtags*/0
368 * Slow when tagged queueing is enabled. (1.5MB/sec versus
370 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
371 * Best performance with these drives is achieved with
372 * tagged queueing turned off, and write caching turned on.
374 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
375 /*quirks*/0, /*mintags*/0, /*maxtags*/0
379 * Slow when tagged queueing is enabled. (1.5MB/sec versus
381 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
382 * Best performance with these drives is achieved with
383 * tagged queueing turned off, and write caching turned on.
385 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
386 /*quirks*/0, /*mintags*/0, /*maxtags*/0
390 * Doesn't handle queue full condition correctly,
391 * so we need to limit maxtags to what the device
392 * can handle instead of determining this automatically.
394 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
395 /*quirks*/0, /*mintags*/2, /*maxtags*/32
398 /* Really only one LUN */
399 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
400 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
403 /* I can't believe we need a quirk for DPT volumes. */
404 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
405 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
406 /*mintags*/0, /*maxtags*/255
410 * Many Sony CDROM drives don't like multi-LUN probing.
412 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
413 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
417 * This drive doesn't like multiple LUN probing.
418 * Submitted by: Parag Patel <parag@cgt.com>
420 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
421 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
424 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
425 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
429 * The 8200 doesn't like multi-lun probing, and probably
430 * don't like serial number requests either.
433 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
436 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
440 * Let's try the same as above, but for a drive that says
441 * it's an IPL-6860 but is actually an EXB 8200.
444 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
447 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
451 * These Hitachi drives don't like multi-lun probing.
452 * The PR submitter has a DK319H, but says that the Linux
453 * kernel has a similar work-around for the DK312 and DK314,
454 * so all DK31* drives are quirked here.
456 * Submitted by: Paul Haddad <paul@pth.com>
458 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
459 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
463 * This old revision of the TDC3600 is also SCSI-1, and
464 * hangs upon serial number probing.
467 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
470 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
474 * Would repond to all LUNs if asked for.
477 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
480 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
484 * Would repond to all LUNs if asked for.
487 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
490 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
493 /* Submitted by: Matthew Dodd <winter@jurai.net> */
494 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
495 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
498 /* Submitted by: Matthew Dodd <winter@jurai.net> */
499 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
500 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
503 /* TeraSolutions special settings for TRC-22 RAID */
504 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
505 /*quirks*/0, /*mintags*/55, /*maxtags*/255
508 /* Veritas Storage Appliance */
509 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
510 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
514 * Would respond to all LUNs. Device type and removable
515 * flag are jumper-selectable.
517 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
520 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
523 /* Default tagged queuing parameters for all devices */
525 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
526 /*vendor*/"*", /*product*/"*", /*revision*/"*"
528 /*quirks*/0, /*mintags*/2, /*maxtags*/255
532 static const int xpt_quirk_table_size =
533 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
537 DM_RET_FLAG_MASK = 0x0f,
540 DM_RET_DESCEND = 0x20,
542 DM_RET_ACTION_MASK = 0xf0
550 } xpt_traverse_depth;
552 struct xpt_traverse_config {
553 xpt_traverse_depth depth;
558 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
559 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
560 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
561 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
562 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
564 /* Transport layer configuration information */
565 static struct xpt_softc xsoftc;
567 /* Queues for our software interrupt handler */
568 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
569 static cam_isrq_t cam_bioq;
570 static cam_isrq_t cam_netq;
572 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
573 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
574 static u_int xpt_max_ccbs; /*
575 * Maximum size of ccb pool. Modified as
576 * devices are added/removed or have their
577 * opening counts changed.
579 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
581 struct cam_periph *xpt_periph;
583 static periph_init_t xpt_periph_init;
585 static periph_init_t probe_periph_init;
587 static struct periph_driver xpt_driver =
589 xpt_periph_init, "xpt",
590 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
593 static struct periph_driver probe_driver =
595 probe_periph_init, "probe",
596 TAILQ_HEAD_INITIALIZER(probe_driver.units)
599 DATA_SET(periphdriver_set, xpt_driver);
600 DATA_SET(periphdriver_set, probe_driver);
602 #define XPT_CDEV_MAJOR 104
604 static d_open_t xptopen;
605 static d_close_t xptclose;
606 static d_ioctl_t xptioctl;
608 static struct cdevsw xpt_cdevsw = {
610 /* maj */ XPT_CDEV_MAJOR,
616 /* close */ xptclose,
619 /* ioctl */ xptioctl,
622 /* strategy */ nostrategy,
627 static struct intr_config_hook *xpt_config_hook;
629 /* Registered busses */
630 static TAILQ_HEAD(,cam_eb) xpt_busses;
631 static u_int bus_generation;
633 /* Storage for debugging datastructures */
635 struct cam_path *cam_dpath;
636 u_int32_t cam_dflags;
637 u_int32_t cam_debug_delay;
640 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
641 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
645 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
646 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
647 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
649 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
650 || defined(CAM_DEBUG_LUN)
652 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
653 || !defined(CAM_DEBUG_LUN)
654 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
656 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
657 #else /* !CAMDEBUG */
658 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
659 #endif /* CAMDEBUG */
660 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
662 /* Our boot-time initialization hook */
663 static void xpt_init(void *);
664 SYSINIT(cam, SI_SUB_CONFIGURE, SI_ORDER_SECOND, xpt_init, NULL);
666 static cam_status xpt_compile_path(struct cam_path *new_path,
667 struct cam_periph *perph,
669 target_id_t target_id,
672 static void xpt_release_path(struct cam_path *path);
674 static void xpt_async_bcast(struct async_list *async_head,
675 u_int32_t async_code,
676 struct cam_path *path,
678 static void xpt_dev_async(u_int32_t async_code,
680 struct cam_et *target,
681 struct cam_ed *device,
683 static path_id_t xptnextfreepathid(void);
684 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
685 static union ccb *xpt_get_ccb(struct cam_ed *device);
686 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
687 u_int32_t new_priority);
688 static void xpt_run_dev_allocq(struct cam_eb *bus);
689 static void xpt_run_dev_sendq(struct cam_eb *bus);
690 static timeout_t xpt_release_devq_timeout;
691 static timeout_t xpt_release_simq_timeout;
692 static void xpt_release_bus(struct cam_eb *bus);
693 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
695 static struct cam_et*
696 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
697 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
698 static struct cam_ed*
699 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
701 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
702 struct cam_ed *device);
703 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
704 static struct cam_eb*
705 xpt_find_bus(path_id_t path_id);
706 static struct cam_et*
707 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
708 static struct cam_ed*
709 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
710 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
711 static void xpt_scan_lun(struct cam_periph *periph,
712 struct cam_path *path, cam_flags flags,
714 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
715 static xpt_busfunc_t xptconfigbuscountfunc;
716 static xpt_busfunc_t xptconfigfunc;
717 static void xpt_config(void *arg);
718 static xpt_devicefunc_t xptpassannouncefunc;
719 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
720 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
721 static void xptpoll(struct cam_sim *sim);
722 static inthand2_t swi_camnet;
723 static inthand2_t swi_cambio;
724 static void camisr(cam_isrq_t *queue);
726 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
727 static void xptasync(struct cam_periph *periph,
728 u_int32_t code, cam_path *path);
730 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
731 int num_patterns, struct cam_eb *bus);
732 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
733 int num_patterns, struct cam_ed *device);
734 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
736 struct cam_periph *periph);
737 static xpt_busfunc_t xptedtbusfunc;
738 static xpt_targetfunc_t xptedttargetfunc;
739 static xpt_devicefunc_t xptedtdevicefunc;
740 static xpt_periphfunc_t xptedtperiphfunc;
741 static xpt_pdrvfunc_t xptplistpdrvfunc;
742 static xpt_periphfunc_t xptplistperiphfunc;
743 static int xptedtmatch(struct ccb_dev_match *cdm);
744 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
745 static int xptbustraverse(struct cam_eb *start_bus,
746 xpt_busfunc_t *tr_func, void *arg);
747 static int xpttargettraverse(struct cam_eb *bus,
748 struct cam_et *start_target,
749 xpt_targetfunc_t *tr_func, void *arg);
750 static int xptdevicetraverse(struct cam_et *target,
751 struct cam_ed *start_device,
752 xpt_devicefunc_t *tr_func, void *arg);
753 static int xptperiphtraverse(struct cam_ed *device,
754 struct cam_periph *start_periph,
755 xpt_periphfunc_t *tr_func, void *arg);
756 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
757 xpt_pdrvfunc_t *tr_func, void *arg);
758 static int xptpdperiphtraverse(struct periph_driver **pdrv,
759 struct cam_periph *start_periph,
760 xpt_periphfunc_t *tr_func,
762 static xpt_busfunc_t xptdefbusfunc;
763 static xpt_targetfunc_t xptdeftargetfunc;
764 static xpt_devicefunc_t xptdefdevicefunc;
765 static xpt_periphfunc_t xptdefperiphfunc;
766 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
768 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
771 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
774 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
777 static xpt_devicefunc_t xptsetasyncfunc;
778 static xpt_busfunc_t xptsetasyncbusfunc;
779 static cam_status xptregister(struct cam_periph *periph,
781 static cam_status proberegister(struct cam_periph *periph,
783 static void probeschedule(struct cam_periph *probe_periph);
784 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
785 static void proberequestdefaultnegotiation(struct cam_periph *periph);
786 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
787 static void probecleanup(struct cam_periph *periph);
788 static void xpt_find_quirk(struct cam_ed *device);
789 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
790 struct cam_ed *device,
792 static void xpt_toggle_tags(struct cam_path *path);
793 static void xpt_start_tags(struct cam_path *path);
794 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
796 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
798 static __inline int periph_is_queued(struct cam_periph *periph);
799 static __inline int device_is_alloc_queued(struct cam_ed *device);
800 static __inline int device_is_send_queued(struct cam_ed *device);
801 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
804 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
808 if (dev->ccbq.devq_openings > 0) {
809 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
810 cam_ccbq_resize(&dev->ccbq,
811 dev->ccbq.dev_openings
812 + dev->ccbq.dev_active);
813 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
816 * The priority of a device waiting for CCB resources
817 * is that of the the highest priority peripheral driver
820 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
821 &dev->alloc_ccb_entry.pinfo,
822 CAMQ_GET_HEAD(&dev->drvq)->priority);
831 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
835 if (dev->ccbq.dev_openings > 0) {
837 * The priority of a device waiting for controller
838 * resources is that of the the highest priority CCB
842 xpt_schedule_dev(&bus->sim->devq->send_queue,
843 &dev->send_ccb_entry.pinfo,
844 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
852 periph_is_queued(struct cam_periph *periph)
854 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
858 device_is_alloc_queued(struct cam_ed *device)
860 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
864 device_is_send_queued(struct cam_ed *device)
866 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
870 dev_allocq_is_runnable(struct cam_devq *devq)
874 * Have space to do more work.
875 * Allowed to do work.
877 return ((devq->alloc_queue.qfrozen_cnt == 0)
878 && (devq->alloc_queue.entries > 0)
879 && (devq->alloc_openings > 0));
885 cdevsw_add(&xpt_cdevsw, 0, 0);
886 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
896 xptdone(struct cam_periph *periph, union ccb *done_ccb)
898 /* Caller will release the CCB */
899 wakeup(&done_ccb->ccb_h.cbfcnp);
903 xptopen(dev_t dev, int flags, int fmt, struct thread *td)
907 unit = minor(dev) & 0xff;
910 * Only allow read-write access.
912 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
916 * We don't allow nonblocking access.
918 if ((flags & O_NONBLOCK) != 0) {
919 printf("xpt%d: can't do nonblocking access\n", unit);
924 * We only have one transport layer right now. If someone accesses
925 * us via something other than minor number 1, point out their
929 printf("xptopen: got invalid xpt unit %d\n", unit);
933 /* Mark ourselves open */
934 xsoftc.flags |= XPT_FLAG_OPEN;
940 xptclose(dev_t dev, int flag, int fmt, struct thread *td)
944 unit = minor(dev) & 0xff;
947 * We only have one transport layer right now. If someone accesses
948 * us via something other than minor number 1, point out their
952 printf("xptclose: got invalid xpt unit %d\n", unit);
956 /* Mark ourselves closed */
957 xsoftc.flags &= ~XPT_FLAG_OPEN;
963 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
968 unit = minor(dev) & 0xff;
971 * We only have one transport layer right now. If someone accesses
972 * us via something other than minor number 1, point out their
976 printf("xptioctl: got invalid xpt unit %d\n", unit);
982 * For the transport layer CAMIOCOMMAND ioctl, we really only want
983 * to accept CCB types that don't quite make sense to send through a
984 * passthrough driver.
990 inccb = (union ccb *)addr;
992 switch(inccb->ccb_h.func_code) {
995 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
996 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1005 ccb = xpt_alloc_ccb();
1008 * Create a path using the bus, target, and lun the
1011 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1012 inccb->ccb_h.path_id,
1013 inccb->ccb_h.target_id,
1014 inccb->ccb_h.target_lun) !=
1020 /* Ensure all of our fields are correct */
1021 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1022 inccb->ccb_h.pinfo.priority);
1023 xpt_merge_ccb(ccb, inccb);
1024 ccb->ccb_h.cbfcnp = xptdone;
1025 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1026 bcopy(ccb, inccb, sizeof(union ccb));
1027 xpt_free_path(ccb->ccb_h.path);
1035 * This is an immediate CCB, so it's okay to
1036 * allocate it on the stack.
1040 * Create a path using the bus, target, and lun the
1043 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1044 inccb->ccb_h.path_id,
1045 inccb->ccb_h.target_id,
1046 inccb->ccb_h.target_lun) !=
1051 /* Ensure all of our fields are correct */
1052 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1053 inccb->ccb_h.pinfo.priority);
1054 xpt_merge_ccb(&ccb, inccb);
1055 ccb.ccb_h.cbfcnp = xptdone;
1057 bcopy(&ccb, inccb, sizeof(union ccb));
1058 xpt_free_path(ccb.ccb_h.path);
1062 case XPT_DEV_MATCH: {
1063 struct cam_periph_map_info mapinfo;
1064 struct cam_path *old_path;
1067 * We can't deal with physical addresses for this
1068 * type of transaction.
1070 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1076 * Save this in case the caller had it set to
1077 * something in particular.
1079 old_path = inccb->ccb_h.path;
1082 * We really don't need a path for the matching
1083 * code. The path is needed because of the
1084 * debugging statements in xpt_action(). They
1085 * assume that the CCB has a valid path.
1087 inccb->ccb_h.path = xpt_periph->path;
1089 bzero(&mapinfo, sizeof(mapinfo));
1092 * Map the pattern and match buffers into kernel
1093 * virtual address space.
1095 error = cam_periph_mapmem(inccb, &mapinfo);
1098 inccb->ccb_h.path = old_path;
1103 * This is an immediate CCB, we can send it on directly.
1108 * Map the buffers back into user space.
1110 cam_periph_unmapmem(inccb, &mapinfo);
1112 inccb->ccb_h.path = old_path;
1124 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1125 * with the periphal driver name and unit name filled in. The other
1126 * fields don't really matter as input. The passthrough driver name
1127 * ("pass"), and unit number are passed back in the ccb. The current
1128 * device generation number, and the index into the device peripheral
1129 * driver list, and the status are also passed back. Note that
1130 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1131 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1132 * (or rather should be) impossible for the device peripheral driver
1133 * list to change since we look at the whole thing in one pass, and
1134 * we do it with splcam protection.
1137 case CAMGETPASSTHRU: {
1139 struct cam_periph *periph;
1140 struct periph_driver **p_drv;
1144 int base_periph_found;
1148 ccb = (union ccb *)addr;
1149 unit = ccb->cgdl.unit_number;
1150 name = ccb->cgdl.periph_name;
1152 * Every 100 devices, we want to drop our spl protection to
1153 * give the software interrupt handler a chance to run.
1154 * Most systems won't run into this check, but this should
1155 * avoid starvation in the software interrupt handler in
1160 ccb = (union ccb *)addr;
1162 base_periph_found = 0;
1165 * Sanity check -- make sure we don't get a null peripheral
1168 if (*ccb->cgdl.periph_name == '\0') {
1173 /* Keep the list from changing while we traverse it */
1176 cur_generation = xsoftc.generation;
1178 /* first find our driver in the list of drivers */
1179 SET_FOREACH(p_drv, periphdriver_set) {
1180 if (strcmp((*p_drv)->driver_name, name) == 0)
1184 if (*p_drv == NULL) {
1186 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1187 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1188 *ccb->cgdl.periph_name = '\0';
1189 ccb->cgdl.unit_number = 0;
1195 * Run through every peripheral instance of this driver
1196 * and check to see whether it matches the unit passed
1197 * in by the user. If it does, get out of the loops and
1198 * find the passthrough driver associated with that
1199 * peripheral driver.
1201 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1202 periph = TAILQ_NEXT(periph, unit_links)) {
1204 if (periph->unit_number == unit) {
1206 } else if (--splbreaknum == 0) {
1210 if (cur_generation != xsoftc.generation)
1215 * If we found the peripheral driver that the user passed
1216 * in, go through all of the peripheral drivers for that
1217 * particular device and look for a passthrough driver.
1219 if (periph != NULL) {
1220 struct cam_ed *device;
1223 base_periph_found = 1;
1224 device = periph->path->device;
1225 for (i = 0, periph = device->periphs.slh_first;
1227 periph = periph->periph_links.sle_next, i++) {
1229 * Check to see whether we have a
1230 * passthrough device or not.
1232 if (strcmp(periph->periph_name, "pass") == 0) {
1234 * Fill in the getdevlist fields.
1236 strcpy(ccb->cgdl.periph_name,
1237 periph->periph_name);
1238 ccb->cgdl.unit_number =
1239 periph->unit_number;
1240 if (periph->periph_links.sle_next)
1242 CAM_GDEVLIST_MORE_DEVS;
1245 CAM_GDEVLIST_LAST_DEVICE;
1246 ccb->cgdl.generation =
1248 ccb->cgdl.index = i;
1250 * Fill in some CCB header fields
1251 * that the user may want.
1253 ccb->ccb_h.path_id =
1254 periph->path->bus->path_id;
1255 ccb->ccb_h.target_id =
1256 periph->path->target->target_id;
1257 ccb->ccb_h.target_lun =
1258 periph->path->device->lun_id;
1259 ccb->ccb_h.status = CAM_REQ_CMP;
1266 * If the periph is null here, one of two things has
1267 * happened. The first possibility is that we couldn't
1268 * find the unit number of the particular peripheral driver
1269 * that the user is asking about. e.g. the user asks for
1270 * the passthrough driver for "da11". We find the list of
1271 * "da" peripherals all right, but there is no unit 11.
1272 * The other possibility is that we went through the list
1273 * of peripheral drivers attached to the device structure,
1274 * but didn't find one with the name "pass". Either way,
1275 * we return ENOENT, since we couldn't find something.
1277 if (periph == NULL) {
1278 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1279 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1280 *ccb->cgdl.periph_name = '\0';
1281 ccb->cgdl.unit_number = 0;
1284 * It is unfortunate that this is even necessary,
1285 * but there are many, many clueless users out there.
1286 * If this is true, the user is looking for the
1287 * passthrough driver, but doesn't have one in his
1290 if (base_periph_found == 1) {
1291 printf("xptioctl: pass driver is not in the "
1293 printf("xptioctl: put \"device pass0\" in "
1294 "your kernel config file\n");
1308 /* Functions accessed by the peripheral drivers */
1313 struct cam_sim *xpt_sim;
1314 struct cam_path *path;
1315 struct cam_devq *devq;
1318 TAILQ_INIT(&xpt_busses);
1319 TAILQ_INIT(&cam_bioq);
1320 TAILQ_INIT(&cam_netq);
1321 SLIST_INIT(&ccb_freeq);
1322 STAILQ_INIT(&highpowerq);
1325 * The xpt layer is, itself, the equivelent of a SIM.
1326 * Allow 16 ccbs in the ccb pool for it. This should
1327 * give decent parallelism when we probe busses and
1328 * perform other XPT functions.
1330 devq = cam_simq_alloc(16);
1331 xpt_sim = cam_sim_alloc(xptaction,
1336 /*max_dev_transactions*/0,
1337 /*max_tagged_dev_transactions*/0,
1339 cam_simq_release(devq);
1342 xpt_bus_register(xpt_sim, /*bus #*/0);
1345 * Looking at the XPT from the SIM layer, the XPT is
1346 * the equivelent of a peripheral driver. Allocate
1347 * a peripheral driver entry for us.
1349 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1350 CAM_TARGET_WILDCARD,
1351 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1352 printf("xpt_init: xpt_create_path failed with status %#x,"
1353 " failing attach\n", status);
1357 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1358 path, NULL, 0, NULL);
1359 xpt_free_path(path);
1361 xpt_sim->softc = xpt_periph;
1364 * Register a callback for when interrupts are enabled.
1366 xpt_config_hook = malloc(sizeof(struct intr_config_hook),
1367 M_TEMP, M_INTWAIT | M_ZERO);
1368 xpt_config_hook->ich_func = xpt_config;
1369 if (config_intrhook_establish(xpt_config_hook) != 0) {
1370 free (xpt_config_hook, M_TEMP);
1371 printf("xpt_init: config_intrhook_establish failed "
1372 "- failing attach\n");
1375 /* Install our software interrupt handlers */
1376 register_swi(SWI_CAMNET, swi_camnet, NULL, "swi_camnet");
1377 register_swi(SWI_CAMBIO, swi_cambio, NULL, "swi_cambio");
1381 xptregister(struct cam_periph *periph, void *arg)
1383 if (periph == NULL) {
1384 printf("xptregister: periph was NULL!!\n");
1385 return(CAM_REQ_CMP_ERR);
1388 periph->softc = NULL;
1390 xpt_periph = periph;
1392 return(CAM_REQ_CMP);
1396 xpt_add_periph(struct cam_periph *periph)
1398 struct cam_ed *device;
1400 struct periph_list *periph_head;
1402 device = periph->path->device;
1404 periph_head = &device->periphs;
1406 status = CAM_REQ_CMP;
1408 if (device != NULL) {
1412 * Make room for this peripheral
1413 * so it will fit in the queue
1414 * when it's scheduled to run
1417 status = camq_resize(&device->drvq,
1418 device->drvq.array_size + 1);
1420 device->generation++;
1422 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1427 xsoftc.generation++;
1433 xpt_remove_periph(struct cam_periph *periph)
1435 struct cam_ed *device;
1437 device = periph->path->device;
1439 if (device != NULL) {
1441 struct periph_list *periph_head;
1443 periph_head = &device->periphs;
1445 /* Release the slot for this peripheral */
1447 camq_resize(&device->drvq, device->drvq.array_size - 1);
1449 device->generation++;
1451 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1456 xsoftc.generation++;
1461 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1465 struct cam_path *path;
1466 struct ccb_trans_settings cts;
1468 path = periph->path;
1470 * To ensure that this is printed in one piece,
1471 * mask out CAM interrupts.
1474 printf("%s%d at %s%d bus %d target %d lun %d\n",
1475 periph->periph_name, periph->unit_number,
1476 path->bus->sim->sim_name,
1477 path->bus->sim->unit_number,
1478 path->bus->sim->bus_id,
1479 path->target->target_id,
1480 path->device->lun_id);
1481 printf("%s%d: ", periph->periph_name, periph->unit_number);
1482 scsi_print_inquiry(&path->device->inq_data);
1484 && (path->device->serial_num_len > 0)) {
1485 /* Don't wrap the screen - print only the first 60 chars */
1486 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1487 periph->unit_number, path->device->serial_num);
1489 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1490 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1491 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1492 xpt_action((union ccb*)&cts);
1493 if (cts.ccb_h.status == CAM_REQ_CMP) {
1497 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1498 && cts.sync_offset != 0) {
1499 freq = scsi_calc_syncsrate(cts.sync_period);
1502 struct ccb_pathinq cpi;
1504 /* Ask the SIM for its base transfer speed */
1505 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1506 cpi.ccb_h.func_code = XPT_PATH_INQ;
1507 xpt_action((union ccb *)&cpi);
1509 speed = cpi.base_transfer_speed;
1512 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1513 speed *= (0x01 << cts.bus_width);
1516 printf("%s%d: %d.%03dMB/s transfers",
1517 periph->periph_name, periph->unit_number,
1520 printf("%s%d: %dKB/s transfers", periph->periph_name,
1521 periph->unit_number, speed);
1522 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1523 && cts.sync_offset != 0) {
1524 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1525 freq % 1000, cts.sync_offset);
1527 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1528 && cts.bus_width > 0) {
1529 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1530 && cts.sync_offset != 0) {
1535 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1536 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1537 && cts.sync_offset != 0) {
1541 if (path->device->inq_flags & SID_CmdQue
1542 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1543 printf(", Tagged Queueing Enabled");
1547 } else if (path->device->inq_flags & SID_CmdQue
1548 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1549 printf("%s%d: Tagged Queueing Enabled\n",
1550 periph->periph_name, periph->unit_number);
1554 * We only want to print the caller's announce string if they've
1557 if (announce_string != NULL)
1558 printf("%s%d: %s\n", periph->periph_name,
1559 periph->unit_number, announce_string);
1564 static dev_match_ret
1565 xptbusmatch(struct dev_match_pattern *patterns, int num_patterns,
1568 dev_match_ret retval;
1571 retval = DM_RET_NONE;
1574 * If we aren't given something to match against, that's an error.
1577 return(DM_RET_ERROR);
1580 * If there are no match entries, then this bus matches no
1583 if ((patterns == NULL) || (num_patterns == 0))
1584 return(DM_RET_DESCEND | DM_RET_COPY);
1586 for (i = 0; i < num_patterns; i++) {
1587 struct bus_match_pattern *cur_pattern;
1590 * If the pattern in question isn't for a bus node, we
1591 * aren't interested. However, we do indicate to the
1592 * calling routine that we should continue descending the
1593 * tree, since the user wants to match against lower-level
1596 if (patterns[i].type != DEV_MATCH_BUS) {
1597 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1598 retval |= DM_RET_DESCEND;
1602 cur_pattern = &patterns[i].pattern.bus_pattern;
1605 * If they want to match any bus node, we give them any
1608 if (cur_pattern->flags == BUS_MATCH_ANY) {
1609 /* set the copy flag */
1610 retval |= DM_RET_COPY;
1613 * If we've already decided on an action, go ahead
1616 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1621 * Not sure why someone would do this...
1623 if (cur_pattern->flags == BUS_MATCH_NONE)
1626 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1627 && (cur_pattern->path_id != bus->path_id))
1630 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1631 && (cur_pattern->bus_id != bus->sim->bus_id))
1634 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1635 && (cur_pattern->unit_number != bus->sim->unit_number))
1638 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1639 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1644 * If we get to this point, the user definitely wants
1645 * information on this bus. So tell the caller to copy the
1648 retval |= DM_RET_COPY;
1651 * If the return action has been set to descend, then we
1652 * know that we've already seen a non-bus matching
1653 * expression, therefore we need to further descend the tree.
1654 * This won't change by continuing around the loop, so we
1655 * go ahead and return. If we haven't seen a non-bus
1656 * matching expression, we keep going around the loop until
1657 * we exhaust the matching expressions. We'll set the stop
1658 * flag once we fall out of the loop.
1660 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1665 * If the return action hasn't been set to descend yet, that means
1666 * we haven't seen anything other than bus matching patterns. So
1667 * tell the caller to stop descending the tree -- the user doesn't
1668 * want to match against lower level tree elements.
1670 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1671 retval |= DM_RET_STOP;
1676 static dev_match_ret
1677 xptdevicematch(struct dev_match_pattern *patterns, int num_patterns,
1678 struct cam_ed *device)
1680 dev_match_ret retval;
1683 retval = DM_RET_NONE;
1686 * If we aren't given something to match against, that's an error.
1689 return(DM_RET_ERROR);
1692 * If there are no match entries, then this device matches no
1695 if ((patterns == NULL) || (patterns == 0))
1696 return(DM_RET_DESCEND | DM_RET_COPY);
1698 for (i = 0; i < num_patterns; i++) {
1699 struct device_match_pattern *cur_pattern;
1702 * If the pattern in question isn't for a device node, we
1703 * aren't interested.
1705 if (patterns[i].type != DEV_MATCH_DEVICE) {
1706 if ((patterns[i].type == DEV_MATCH_PERIPH)
1707 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1708 retval |= DM_RET_DESCEND;
1712 cur_pattern = &patterns[i].pattern.device_pattern;
1715 * If they want to match any device node, we give them any
1718 if (cur_pattern->flags == DEV_MATCH_ANY) {
1719 /* set the copy flag */
1720 retval |= DM_RET_COPY;
1724 * If we've already decided on an action, go ahead
1727 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1732 * Not sure why someone would do this...
1734 if (cur_pattern->flags == DEV_MATCH_NONE)
1737 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1738 && (cur_pattern->path_id != device->target->bus->path_id))
1741 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1742 && (cur_pattern->target_id != device->target->target_id))
1745 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1746 && (cur_pattern->target_lun != device->lun_id))
1749 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1750 && (cam_quirkmatch((caddr_t)&device->inq_data,
1751 (caddr_t)&cur_pattern->inq_pat,
1752 1, sizeof(cur_pattern->inq_pat),
1753 scsi_static_inquiry_match) == NULL))
1757 * If we get to this point, the user definitely wants
1758 * information on this device. So tell the caller to copy
1761 retval |= DM_RET_COPY;
1764 * If the return action has been set to descend, then we
1765 * know that we've already seen a peripheral matching
1766 * expression, therefore we need to further descend the tree.
1767 * This won't change by continuing around the loop, so we
1768 * go ahead and return. If we haven't seen a peripheral
1769 * matching expression, we keep going around the loop until
1770 * we exhaust the matching expressions. We'll set the stop
1771 * flag once we fall out of the loop.
1773 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1778 * If the return action hasn't been set to descend yet, that means
1779 * we haven't seen any peripheral matching patterns. So tell the
1780 * caller to stop descending the tree -- the user doesn't want to
1781 * match against lower level tree elements.
1783 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1784 retval |= DM_RET_STOP;
1790 * Match a single peripheral against any number of match patterns.
1792 static dev_match_ret
1793 xptperiphmatch(struct dev_match_pattern *patterns, int num_patterns,
1794 struct cam_periph *periph)
1796 dev_match_ret retval;
1800 * If we aren't given something to match against, that's an error.
1803 return(DM_RET_ERROR);
1806 * If there are no match entries, then this peripheral matches no
1809 if ((patterns == NULL) || (num_patterns == 0))
1810 return(DM_RET_STOP | DM_RET_COPY);
1813 * There aren't any nodes below a peripheral node, so there's no
1814 * reason to descend the tree any further.
1816 retval = DM_RET_STOP;
1818 for (i = 0; i < num_patterns; i++) {
1819 struct periph_match_pattern *cur_pattern;
1822 * If the pattern in question isn't for a peripheral, we
1823 * aren't interested.
1825 if (patterns[i].type != DEV_MATCH_PERIPH)
1828 cur_pattern = &patterns[i].pattern.periph_pattern;
1831 * If they want to match on anything, then we will do so.
1833 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1834 /* set the copy flag */
1835 retval |= DM_RET_COPY;
1838 * We've already set the return action to stop,
1839 * since there are no nodes below peripherals in
1846 * Not sure why someone would do this...
1848 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1851 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1852 && (cur_pattern->path_id != periph->path->bus->path_id))
1856 * For the target and lun id's, we have to make sure the
1857 * target and lun pointers aren't NULL. The xpt peripheral
1858 * has a wildcard target and device.
1860 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1861 && ((periph->path->target == NULL)
1862 ||(cur_pattern->target_id != periph->path->target->target_id)))
1865 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1866 && ((periph->path->device == NULL)
1867 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1870 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1871 && (cur_pattern->unit_number != periph->unit_number))
1874 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1875 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1880 * If we get to this point, the user definitely wants
1881 * information on this peripheral. So tell the caller to
1882 * copy the data out.
1884 retval |= DM_RET_COPY;
1887 * The return action has already been set to stop, since
1888 * peripherals don't have any nodes below them in the EDT.
1894 * If we get to this point, the peripheral that was passed in
1895 * doesn't match any of the patterns.
1901 xptedtbusfunc(struct cam_eb *bus, void *arg)
1903 struct ccb_dev_match *cdm;
1904 dev_match_ret retval;
1906 cdm = (struct ccb_dev_match *)arg;
1909 * If our position is for something deeper in the tree, that means
1910 * that we've already seen this node. So, we keep going down.
1912 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1913 && (cdm->pos.cookie.bus == bus)
1914 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1915 && (cdm->pos.cookie.target != NULL))
1916 retval = DM_RET_DESCEND;
1918 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1921 * If we got an error, bail out of the search.
1923 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1924 cdm->status = CAM_DEV_MATCH_ERROR;
1929 * If the copy flag is set, copy this bus out.
1931 if (retval & DM_RET_COPY) {
1934 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1935 sizeof(struct dev_match_result));
1938 * If we don't have enough space to put in another
1939 * match result, save our position and tell the
1940 * user there are more devices to check.
1942 if (spaceleft < sizeof(struct dev_match_result)) {
1943 bzero(&cdm->pos, sizeof(cdm->pos));
1944 cdm->pos.position_type =
1945 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1947 cdm->pos.cookie.bus = bus;
1948 cdm->pos.generations[CAM_BUS_GENERATION]=
1950 cdm->status = CAM_DEV_MATCH_MORE;
1953 j = cdm->num_matches;
1955 cdm->matches[j].type = DEV_MATCH_BUS;
1956 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1957 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1958 cdm->matches[j].result.bus_result.unit_number =
1959 bus->sim->unit_number;
1960 strncpy(cdm->matches[j].result.bus_result.dev_name,
1961 bus->sim->sim_name, DEV_IDLEN);
1965 * If the user is only interested in busses, there's no
1966 * reason to descend to the next level in the tree.
1968 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1972 * If there is a target generation recorded, check it to
1973 * make sure the target list hasn't changed.
1975 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1976 && (bus == cdm->pos.cookie.bus)
1977 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1978 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1979 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1981 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1985 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1986 && (cdm->pos.cookie.bus == bus)
1987 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1988 && (cdm->pos.cookie.target != NULL))
1989 return(xpttargettraverse(bus,
1990 (struct cam_et *)cdm->pos.cookie.target,
1991 xptedttargetfunc, arg));
1993 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1997 xptedttargetfunc(struct cam_et *target, void *arg)
1999 struct ccb_dev_match *cdm;
2001 cdm = (struct ccb_dev_match *)arg;
2004 * If there is a device list generation recorded, check it to
2005 * make sure the device list hasn't changed.
2007 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2008 && (cdm->pos.cookie.bus == target->bus)
2009 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2010 && (cdm->pos.cookie.target == target)
2011 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2012 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2013 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2014 target->generation)) {
2015 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2019 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2020 && (cdm->pos.cookie.bus == target->bus)
2021 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2022 && (cdm->pos.cookie.target == target)
2023 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2024 && (cdm->pos.cookie.device != NULL))
2025 return(xptdevicetraverse(target,
2026 (struct cam_ed *)cdm->pos.cookie.device,
2027 xptedtdevicefunc, arg));
2029 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2033 xptedtdevicefunc(struct cam_ed *device, void *arg)
2036 struct ccb_dev_match *cdm;
2037 dev_match_ret retval;
2039 cdm = (struct ccb_dev_match *)arg;
2042 * If our position is for something deeper in the tree, that means
2043 * that we've already seen this node. So, we keep going down.
2045 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2046 && (cdm->pos.cookie.device == device)
2047 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2048 && (cdm->pos.cookie.periph != NULL))
2049 retval = DM_RET_DESCEND;
2051 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2054 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2055 cdm->status = CAM_DEV_MATCH_ERROR;
2060 * If the copy flag is set, copy this device out.
2062 if (retval & DM_RET_COPY) {
2065 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2066 sizeof(struct dev_match_result));
2069 * If we don't have enough space to put in another
2070 * match result, save our position and tell the
2071 * user there are more devices to check.
2073 if (spaceleft < sizeof(struct dev_match_result)) {
2074 bzero(&cdm->pos, sizeof(cdm->pos));
2075 cdm->pos.position_type =
2076 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2077 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2079 cdm->pos.cookie.bus = device->target->bus;
2080 cdm->pos.generations[CAM_BUS_GENERATION]=
2082 cdm->pos.cookie.target = device->target;
2083 cdm->pos.generations[CAM_TARGET_GENERATION] =
2084 device->target->bus->generation;
2085 cdm->pos.cookie.device = device;
2086 cdm->pos.generations[CAM_DEV_GENERATION] =
2087 device->target->generation;
2088 cdm->status = CAM_DEV_MATCH_MORE;
2091 j = cdm->num_matches;
2093 cdm->matches[j].type = DEV_MATCH_DEVICE;
2094 cdm->matches[j].result.device_result.path_id =
2095 device->target->bus->path_id;
2096 cdm->matches[j].result.device_result.target_id =
2097 device->target->target_id;
2098 cdm->matches[j].result.device_result.target_lun =
2100 bcopy(&device->inq_data,
2101 &cdm->matches[j].result.device_result.inq_data,
2102 sizeof(struct scsi_inquiry_data));
2104 /* Let the user know whether this device is unconfigured */
2105 if (device->flags & CAM_DEV_UNCONFIGURED)
2106 cdm->matches[j].result.device_result.flags =
2107 DEV_RESULT_UNCONFIGURED;
2109 cdm->matches[j].result.device_result.flags =
2114 * If the user isn't interested in peripherals, don't descend
2115 * the tree any further.
2117 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2121 * If there is a peripheral list generation recorded, make sure
2122 * it hasn't changed.
2124 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2125 && (device->target->bus == cdm->pos.cookie.bus)
2126 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2127 && (device->target == cdm->pos.cookie.target)
2128 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2129 && (device == cdm->pos.cookie.device)
2130 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2131 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2132 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2133 device->generation)){
2134 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2138 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2139 && (cdm->pos.cookie.bus == device->target->bus)
2140 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2141 && (cdm->pos.cookie.target == device->target)
2142 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2143 && (cdm->pos.cookie.device == device)
2144 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2145 && (cdm->pos.cookie.periph != NULL))
2146 return(xptperiphtraverse(device,
2147 (struct cam_periph *)cdm->pos.cookie.periph,
2148 xptedtperiphfunc, arg));
2150 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2154 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2156 struct ccb_dev_match *cdm;
2157 dev_match_ret retval;
2159 cdm = (struct ccb_dev_match *)arg;
2161 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2163 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2164 cdm->status = CAM_DEV_MATCH_ERROR;
2169 * If the copy flag is set, copy this peripheral out.
2171 if (retval & DM_RET_COPY) {
2174 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2175 sizeof(struct dev_match_result));
2178 * If we don't have enough space to put in another
2179 * match result, save our position and tell the
2180 * user there are more devices to check.
2182 if (spaceleft < sizeof(struct dev_match_result)) {
2183 bzero(&cdm->pos, sizeof(cdm->pos));
2184 cdm->pos.position_type =
2185 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2186 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2189 cdm->pos.cookie.bus = periph->path->bus;
2190 cdm->pos.generations[CAM_BUS_GENERATION]=
2192 cdm->pos.cookie.target = periph->path->target;
2193 cdm->pos.generations[CAM_TARGET_GENERATION] =
2194 periph->path->bus->generation;
2195 cdm->pos.cookie.device = periph->path->device;
2196 cdm->pos.generations[CAM_DEV_GENERATION] =
2197 periph->path->target->generation;
2198 cdm->pos.cookie.periph = periph;
2199 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2200 periph->path->device->generation;
2201 cdm->status = CAM_DEV_MATCH_MORE;
2205 j = cdm->num_matches;
2207 cdm->matches[j].type = DEV_MATCH_PERIPH;
2208 cdm->matches[j].result.periph_result.path_id =
2209 periph->path->bus->path_id;
2210 cdm->matches[j].result.periph_result.target_id =
2211 periph->path->target->target_id;
2212 cdm->matches[j].result.periph_result.target_lun =
2213 periph->path->device->lun_id;
2214 cdm->matches[j].result.periph_result.unit_number =
2215 periph->unit_number;
2216 strncpy(cdm->matches[j].result.periph_result.periph_name,
2217 periph->periph_name, DEV_IDLEN);
2224 xptedtmatch(struct ccb_dev_match *cdm)
2228 cdm->num_matches = 0;
2231 * Check the bus list generation. If it has changed, the user
2232 * needs to reset everything and start over.
2234 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2235 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2236 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2237 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2241 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2242 && (cdm->pos.cookie.bus != NULL))
2243 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2244 xptedtbusfunc, cdm);
2246 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2249 * If we get back 0, that means that we had to stop before fully
2250 * traversing the EDT. It also means that one of the subroutines
2251 * has set the status field to the proper value. If we get back 1,
2252 * we've fully traversed the EDT and copied out any matching entries.
2255 cdm->status = CAM_DEV_MATCH_LAST;
2261 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2263 struct ccb_dev_match *cdm;
2265 cdm = (struct ccb_dev_match *)arg;
2267 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2268 && (cdm->pos.cookie.pdrv == pdrv)
2269 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2270 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2271 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2272 (*pdrv)->generation)) {
2273 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2277 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2278 && (cdm->pos.cookie.pdrv == pdrv)
2279 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2280 && (cdm->pos.cookie.periph != NULL))
2281 return(xptpdperiphtraverse(pdrv,
2282 (struct cam_periph *)cdm->pos.cookie.periph,
2283 xptplistperiphfunc, arg));
2285 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2289 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2291 struct ccb_dev_match *cdm;
2292 dev_match_ret retval;
2294 cdm = (struct ccb_dev_match *)arg;
2296 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2298 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2299 cdm->status = CAM_DEV_MATCH_ERROR;
2304 * If the copy flag is set, copy this peripheral out.
2306 if (retval & DM_RET_COPY) {
2309 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2310 sizeof(struct dev_match_result));
2313 * If we don't have enough space to put in another
2314 * match result, save our position and tell the
2315 * user there are more devices to check.
2317 if (spaceleft < sizeof(struct dev_match_result)) {
2318 struct periph_driver **pdrv;
2321 bzero(&cdm->pos, sizeof(cdm->pos));
2322 cdm->pos.position_type =
2323 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2327 * This may look a bit non-sensical, but it is
2328 * actually quite logical. There are very few
2329 * peripheral drivers, and bloating every peripheral
2330 * structure with a pointer back to its parent
2331 * peripheral driver linker set entry would cost
2332 * more in the long run than doing this quick lookup.
2334 SET_FOREACH(pdrv, periphdriver_set) {
2335 if (strcmp((*pdrv)->driver_name,
2336 periph->periph_name) == 0)
2341 cdm->status = CAM_DEV_MATCH_ERROR;
2345 cdm->pos.cookie.pdrv = pdrv;
2347 * The periph generation slot does double duty, as
2348 * does the periph pointer slot. They are used for
2349 * both edt and pdrv lookups and positioning.
2351 cdm->pos.cookie.periph = periph;
2352 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2353 (*pdrv)->generation;
2354 cdm->status = CAM_DEV_MATCH_MORE;
2358 j = cdm->num_matches;
2360 cdm->matches[j].type = DEV_MATCH_PERIPH;
2361 cdm->matches[j].result.periph_result.path_id =
2362 periph->path->bus->path_id;
2365 * The transport layer peripheral doesn't have a target or
2368 if (periph->path->target)
2369 cdm->matches[j].result.periph_result.target_id =
2370 periph->path->target->target_id;
2372 cdm->matches[j].result.periph_result.target_id = -1;
2374 if (periph->path->device)
2375 cdm->matches[j].result.periph_result.target_lun =
2376 periph->path->device->lun_id;
2378 cdm->matches[j].result.periph_result.target_lun = -1;
2380 cdm->matches[j].result.periph_result.unit_number =
2381 periph->unit_number;
2382 strncpy(cdm->matches[j].result.periph_result.periph_name,
2383 periph->periph_name, DEV_IDLEN);
2390 xptperiphlistmatch(struct ccb_dev_match *cdm)
2394 cdm->num_matches = 0;
2397 * At this point in the edt traversal function, we check the bus
2398 * list generation to make sure that no busses have been added or
2399 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2400 * For the peripheral driver list traversal function, however, we
2401 * don't have to worry about new peripheral driver types coming or
2402 * going; they're in a linker set, and therefore can't change
2403 * without a recompile.
2406 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2407 && (cdm->pos.cookie.pdrv != NULL))
2408 ret = xptpdrvtraverse(
2409 (struct periph_driver **)cdm->pos.cookie.pdrv,
2410 xptplistpdrvfunc, cdm);
2412 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2415 * If we get back 0, that means that we had to stop before fully
2416 * traversing the peripheral driver tree. It also means that one of
2417 * the subroutines has set the status field to the proper value. If
2418 * we get back 1, we've fully traversed the EDT and copied out any
2422 cdm->status = CAM_DEV_MATCH_LAST;
2428 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2430 struct cam_eb *bus, *next_bus;
2435 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2438 next_bus = TAILQ_NEXT(bus, links);
2440 retval = tr_func(bus, arg);
2449 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2450 xpt_targetfunc_t *tr_func, void *arg)
2452 struct cam_et *target, *next_target;
2456 for (target = (start_target ? start_target :
2457 TAILQ_FIRST(&bus->et_entries));
2458 target != NULL; target = next_target) {
2460 next_target = TAILQ_NEXT(target, links);
2462 retval = tr_func(target, arg);
2472 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2473 xpt_devicefunc_t *tr_func, void *arg)
2475 struct cam_ed *device, *next_device;
2479 for (device = (start_device ? start_device :
2480 TAILQ_FIRST(&target->ed_entries));
2482 device = next_device) {
2484 next_device = TAILQ_NEXT(device, links);
2486 retval = tr_func(device, arg);
2496 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2497 xpt_periphfunc_t *tr_func, void *arg)
2499 struct cam_periph *periph, *next_periph;
2504 for (periph = (start_periph ? start_periph :
2505 SLIST_FIRST(&device->periphs));
2507 periph = next_periph) {
2509 next_periph = SLIST_NEXT(periph, periph_links);
2511 retval = tr_func(periph, arg);
2520 xptpdrvtraverse(struct periph_driver **start_pdrv,
2521 xpt_pdrvfunc_t *tr_func, void *arg)
2523 struct periph_driver **pdrv;
2529 * We don't traverse the peripheral driver list like we do the
2530 * other lists, because it is a linker set, and therefore cannot be
2531 * changed during runtime. If the peripheral driver list is ever
2532 * re-done to be something other than a linker set (i.e. it can
2533 * change while the system is running), the list traversal should
2534 * be modified to work like the other traversal functions.
2536 SET_FOREACH(pdrv, periphdriver_set) {
2537 if (start_pdrv == NULL || start_pdrv == pdrv) {
2538 retval = tr_func(pdrv, arg);
2541 start_pdrv = NULL; /* traverse remainder */
2548 xptpdperiphtraverse(struct periph_driver **pdrv,
2549 struct cam_periph *start_periph,
2550 xpt_periphfunc_t *tr_func, void *arg)
2552 struct cam_periph *periph, *next_periph;
2557 for (periph = (start_periph ? start_periph :
2558 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2559 periph = next_periph) {
2561 next_periph = TAILQ_NEXT(periph, unit_links);
2563 retval = tr_func(periph, arg);
2571 xptdefbusfunc(struct cam_eb *bus, void *arg)
2573 struct xpt_traverse_config *tr_config;
2575 tr_config = (struct xpt_traverse_config *)arg;
2577 if (tr_config->depth == XPT_DEPTH_BUS) {
2578 xpt_busfunc_t *tr_func;
2580 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2582 return(tr_func(bus, tr_config->tr_arg));
2584 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2588 xptdeftargetfunc(struct cam_et *target, void *arg)
2590 struct xpt_traverse_config *tr_config;
2592 tr_config = (struct xpt_traverse_config *)arg;
2594 if (tr_config->depth == XPT_DEPTH_TARGET) {
2595 xpt_targetfunc_t *tr_func;
2597 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2599 return(tr_func(target, tr_config->tr_arg));
2601 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2605 xptdefdevicefunc(struct cam_ed *device, void *arg)
2607 struct xpt_traverse_config *tr_config;
2609 tr_config = (struct xpt_traverse_config *)arg;
2611 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2612 xpt_devicefunc_t *tr_func;
2614 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2616 return(tr_func(device, tr_config->tr_arg));
2618 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2622 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2624 struct xpt_traverse_config *tr_config;
2625 xpt_periphfunc_t *tr_func;
2627 tr_config = (struct xpt_traverse_config *)arg;
2629 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2632 * Unlike the other default functions, we don't check for depth
2633 * here. The peripheral driver level is the last level in the EDT,
2634 * so if we're here, we should execute the function in question.
2636 return(tr_func(periph, tr_config->tr_arg));
2640 * Execute the given function for every bus in the EDT.
2643 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2645 struct xpt_traverse_config tr_config;
2647 tr_config.depth = XPT_DEPTH_BUS;
2648 tr_config.tr_func = tr_func;
2649 tr_config.tr_arg = arg;
2651 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2656 * Execute the given function for every target in the EDT.
2659 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2661 struct xpt_traverse_config tr_config;
2663 tr_config.depth = XPT_DEPTH_TARGET;
2664 tr_config.tr_func = tr_func;
2665 tr_config.tr_arg = arg;
2667 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2669 #endif /* notusedyet */
2672 * Execute the given function for every device in the EDT.
2675 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2677 struct xpt_traverse_config tr_config;
2679 tr_config.depth = XPT_DEPTH_DEVICE;
2680 tr_config.tr_func = tr_func;
2681 tr_config.tr_arg = arg;
2683 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2688 * Execute the given function for every peripheral in the EDT.
2691 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2693 struct xpt_traverse_config tr_config;
2695 tr_config.depth = XPT_DEPTH_PERIPH;
2696 tr_config.tr_func = tr_func;
2697 tr_config.tr_arg = arg;
2699 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2701 #endif /* notusedyet */
2704 xptsetasyncfunc(struct cam_ed *device, void *arg)
2706 struct cam_path path;
2707 struct ccb_getdev cgd;
2708 struct async_node *cur_entry;
2710 cur_entry = (struct async_node *)arg;
2713 * Don't report unconfigured devices (Wildcard devs,
2714 * devices only for target mode, device instances
2715 * that have been invalidated but are waiting for
2716 * their last reference count to be released).
2718 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2721 xpt_compile_path(&path,
2723 device->target->bus->path_id,
2724 device->target->target_id,
2726 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2727 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2728 xpt_action((union ccb *)&cgd);
2729 cur_entry->callback(cur_entry->callback_arg,
2732 xpt_release_path(&path);
2738 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2740 struct cam_path path;
2741 struct ccb_pathinq cpi;
2742 struct async_node *cur_entry;
2744 cur_entry = (struct async_node *)arg;
2746 xpt_compile_path(&path, /*periph*/NULL,
2748 CAM_TARGET_WILDCARD,
2750 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2751 cpi.ccb_h.func_code = XPT_PATH_INQ;
2752 xpt_action((union ccb *)&cpi);
2753 cur_entry->callback(cur_entry->callback_arg,
2756 xpt_release_path(&path);
2762 xpt_action(union ccb *start_ccb)
2766 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2768 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2770 iopl = splsoftcam();
2771 switch (start_ccb->ccb_h.func_code) {
2775 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2776 struct cam_path *path;
2778 path = start_ccb->ccb_h.path;
2782 * For the sake of compatibility with SCSI-1
2783 * devices that may not understand the identify
2784 * message, we include lun information in the
2785 * second byte of all commands. SCSI-1 specifies
2786 * that luns are a 3 bit value and reserves only 3
2787 * bits for lun information in the CDB. Later
2788 * revisions of the SCSI spec allow for more than 8
2789 * luns, but have deprecated lun information in the
2790 * CDB. So, if the lun won't fit, we must omit.
2792 * Also be aware that during initial probing for devices,
2793 * the inquiry information is unknown but initialized to 0.
2794 * This means that this code will be exercised while probing
2795 * devices with an ANSI revision greater than 2.
2797 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2798 && start_ccb->ccb_h.target_lun < 8
2799 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2801 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2802 start_ccb->ccb_h.target_lun << 5;
2804 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2805 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2806 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2807 &path->device->inq_data),
2808 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2809 cdb_str, sizeof(cdb_str))));
2813 case XPT_CONT_TARGET_IO:
2814 start_ccb->csio.sense_resid = 0;
2815 start_ccb->csio.resid = 0;
2820 struct cam_path *path;
2824 path = start_ccb->ccb_h.path;
2827 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2828 if (path->device->qfrozen_cnt == 0)
2829 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2834 xpt_run_dev_sendq(path->bus);
2837 case XPT_SET_TRAN_SETTINGS:
2839 xpt_set_transfer_settings(&start_ccb->cts,
2840 start_ccb->ccb_h.path->device,
2841 /*async_update*/FALSE);
2844 case XPT_CALC_GEOMETRY:
2846 struct cam_sim *sim;
2848 /* Filter out garbage */
2849 if (start_ccb->ccg.block_size == 0
2850 || start_ccb->ccg.volume_size == 0) {
2851 start_ccb->ccg.cylinders = 0;
2852 start_ccb->ccg.heads = 0;
2853 start_ccb->ccg.secs_per_track = 0;
2854 start_ccb->ccb_h.status = CAM_REQ_CMP;
2859 * In a PC-98 system, geometry translation depens on
2860 * the "real" device geometry obtained from mode page 4.
2861 * SCSI geometry translation is performed in the
2862 * initialization routine of the SCSI BIOS and the result
2863 * stored in host memory. If the translation is available
2864 * in host memory, use it. If not, rely on the default
2865 * translation the device driver performs.
2867 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2868 start_ccb->ccb_h.status = CAM_REQ_CMP;
2872 sim = start_ccb->ccb_h.path->bus->sim;
2873 (*(sim->sim_action))(sim, start_ccb);
2878 union ccb* abort_ccb;
2881 abort_ccb = start_ccb->cab.abort_ccb;
2882 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2884 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2885 struct cam_ccbq *ccbq;
2887 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2888 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2889 abort_ccb->ccb_h.status =
2890 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2891 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2893 xpt_done(abort_ccb);
2895 start_ccb->ccb_h.status = CAM_REQ_CMP;
2898 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2899 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2901 * We've caught this ccb en route to
2902 * the SIM. Flag it for abort and the
2903 * SIM will do so just before starting
2904 * real work on the CCB.
2906 abort_ccb->ccb_h.status =
2907 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2908 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2909 start_ccb->ccb_h.status = CAM_REQ_CMP;
2913 if (XPT_FC_IS_QUEUED(abort_ccb)
2914 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2916 * It's already completed but waiting
2917 * for our SWI to get to it.
2919 start_ccb->ccb_h.status = CAM_UA_ABORT;
2923 * If we weren't able to take care of the abort request
2924 * in the XPT, pass the request down to the SIM for processing.
2928 case XPT_ACCEPT_TARGET_IO:
2930 case XPT_IMMED_NOTIFY:
2931 case XPT_NOTIFY_ACK:
2932 case XPT_GET_TRAN_SETTINGS:
2935 struct cam_sim *sim;
2937 sim = start_ccb->ccb_h.path->bus->sim;
2938 (*(sim->sim_action))(sim, start_ccb);
2943 struct cam_sim *sim;
2945 sim = start_ccb->ccb_h.path->bus->sim;
2946 (*(sim->sim_action))(sim, start_ccb);
2949 case XPT_PATH_STATS:
2950 start_ccb->cpis.last_reset =
2951 start_ccb->ccb_h.path->bus->last_reset;
2952 start_ccb->ccb_h.status = CAM_REQ_CMP;
2959 dev = start_ccb->ccb_h.path->device;
2961 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2962 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2964 struct ccb_getdev *cgd;
2968 cgd = &start_ccb->cgd;
2969 bus = cgd->ccb_h.path->bus;
2970 tar = cgd->ccb_h.path->target;
2971 cgd->inq_data = dev->inq_data;
2972 cgd->ccb_h.status = CAM_REQ_CMP;
2973 cgd->serial_num_len = dev->serial_num_len;
2974 if ((dev->serial_num_len > 0)
2975 && (dev->serial_num != NULL))
2976 bcopy(dev->serial_num, cgd->serial_num,
2977 dev->serial_num_len);
2982 case XPT_GDEV_STATS:
2987 dev = start_ccb->ccb_h.path->device;
2989 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2990 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2992 struct ccb_getdevstats *cgds;
2996 cgds = &start_ccb->cgds;
2997 bus = cgds->ccb_h.path->bus;
2998 tar = cgds->ccb_h.path->target;
2999 cgds->dev_openings = dev->ccbq.dev_openings;
3000 cgds->dev_active = dev->ccbq.dev_active;
3001 cgds->devq_openings = dev->ccbq.devq_openings;
3002 cgds->devq_queued = dev->ccbq.queue.entries;
3003 cgds->held = dev->ccbq.held;
3004 cgds->last_reset = tar->last_reset;
3005 cgds->maxtags = dev->quirk->maxtags;
3006 cgds->mintags = dev->quirk->mintags;
3007 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3008 cgds->last_reset = bus->last_reset;
3009 cgds->ccb_h.status = CAM_REQ_CMP;
3016 struct cam_periph *nperiph;
3017 struct periph_list *periph_head;
3018 struct ccb_getdevlist *cgdl;
3021 struct cam_ed *device;
3028 * Don't want anyone mucking with our data.
3031 device = start_ccb->ccb_h.path->device;
3032 periph_head = &device->periphs;
3033 cgdl = &start_ccb->cgdl;
3036 * Check and see if the list has changed since the user
3037 * last requested a list member. If so, tell them that the
3038 * list has changed, and therefore they need to start over
3039 * from the beginning.
3041 if ((cgdl->index != 0) &&
3042 (cgdl->generation != device->generation)) {
3043 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3049 * Traverse the list of peripherals and attempt to find
3050 * the requested peripheral.
3052 for (nperiph = periph_head->slh_first, i = 0;
3053 (nperiph != NULL) && (i <= cgdl->index);
3054 nperiph = nperiph->periph_links.sle_next, i++) {
3055 if (i == cgdl->index) {
3056 strncpy(cgdl->periph_name,
3057 nperiph->periph_name,
3059 cgdl->unit_number = nperiph->unit_number;
3064 cgdl->status = CAM_GDEVLIST_ERROR;
3069 if (nperiph == NULL)
3070 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3072 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3075 cgdl->generation = device->generation;
3078 cgdl->ccb_h.status = CAM_REQ_CMP;
3084 dev_pos_type position_type;
3085 struct ccb_dev_match *cdm;
3088 cdm = &start_ccb->cdm;
3091 * Prevent EDT changes while we traverse it.
3095 * There are two ways of getting at information in the EDT.
3096 * The first way is via the primary EDT tree. It starts
3097 * with a list of busses, then a list of targets on a bus,
3098 * then devices/luns on a target, and then peripherals on a
3099 * device/lun. The "other" way is by the peripheral driver
3100 * lists. The peripheral driver lists are organized by
3101 * peripheral driver. (obviously) So it makes sense to
3102 * use the peripheral driver list if the user is looking
3103 * for something like "da1", or all "da" devices. If the
3104 * user is looking for something on a particular bus/target
3105 * or lun, it's generally better to go through the EDT tree.
3108 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3109 position_type = cdm->pos.position_type;
3113 position_type = CAM_DEV_POS_NONE;
3115 for (i = 0; i < cdm->num_patterns; i++) {
3116 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3117 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3118 position_type = CAM_DEV_POS_EDT;
3123 if (cdm->num_patterns == 0)
3124 position_type = CAM_DEV_POS_EDT;
3125 else if (position_type == CAM_DEV_POS_NONE)
3126 position_type = CAM_DEV_POS_PDRV;
3129 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3130 case CAM_DEV_POS_EDT:
3131 ret = xptedtmatch(cdm);
3133 case CAM_DEV_POS_PDRV:
3134 ret = xptperiphlistmatch(cdm);
3137 cdm->status = CAM_DEV_MATCH_ERROR;
3143 if (cdm->status == CAM_DEV_MATCH_ERROR)
3144 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3146 start_ccb->ccb_h.status = CAM_REQ_CMP;
3152 struct ccb_setasync *csa;
3153 struct async_node *cur_entry;
3154 struct async_list *async_head;
3158 csa = &start_ccb->csa;
3159 added = csa->event_enable;
3160 async_head = &csa->ccb_h.path->device->asyncs;
3163 * If there is already an entry for us, simply
3167 cur_entry = SLIST_FIRST(async_head);
3168 while (cur_entry != NULL) {
3169 if ((cur_entry->callback_arg == csa->callback_arg)
3170 && (cur_entry->callback == csa->callback))
3172 cur_entry = SLIST_NEXT(cur_entry, links);
3175 if (cur_entry != NULL) {
3177 * If the request has no flags set,
3180 added &= ~cur_entry->event_enable;
3181 if (csa->event_enable == 0) {
3182 SLIST_REMOVE(async_head, cur_entry,
3184 csa->ccb_h.path->device->refcount--;
3185 free(cur_entry, M_DEVBUF);
3187 cur_entry->event_enable = csa->event_enable;
3190 cur_entry = malloc(sizeof(*cur_entry),
3191 M_DEVBUF, M_INTWAIT);
3192 cur_entry->event_enable = csa->event_enable;
3193 cur_entry->callback_arg = csa->callback_arg;
3194 cur_entry->callback = csa->callback;
3195 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3196 csa->ccb_h.path->device->refcount++;
3199 if ((added & AC_FOUND_DEVICE) != 0) {
3201 * Get this peripheral up to date with all
3202 * the currently existing devices.
3204 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3206 if ((added & AC_PATH_REGISTERED) != 0) {
3208 * Get this peripheral up to date with all
3209 * the currently existing busses.
3211 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3214 start_ccb->ccb_h.status = CAM_REQ_CMP;
3219 struct ccb_relsim *crs;
3223 crs = &start_ccb->crs;
3224 dev = crs->ccb_h.path->device;
3227 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3233 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3235 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3237 /* Don't ever go below one opening */
3238 if (crs->openings > 0) {
3239 xpt_dev_ccbq_resize(crs->ccb_h.path,
3243 xpt_print_path(crs->ccb_h.path);
3244 printf("tagged openings "
3252 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3254 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3257 * Just extend the old timeout and decrement
3258 * the freeze count so that a single timeout
3259 * is sufficient for releasing the queue.
3261 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3262 untimeout(xpt_release_devq_timeout,
3263 dev, dev->c_handle);
3266 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3270 timeout(xpt_release_devq_timeout,
3272 (crs->release_timeout * hz) / 1000);
3274 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3278 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3280 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3282 * Decrement the freeze count so that a single
3283 * completion is still sufficient to unfreeze
3286 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3289 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3290 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3294 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3296 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3297 || (dev->ccbq.dev_active == 0)) {
3299 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3302 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3303 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3308 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3310 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3313 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3314 start_ccb->ccb_h.status = CAM_REQ_CMP;
3318 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3321 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3322 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3330 #ifdef CAM_DEBUG_DELAY
3331 cam_debug_delay = CAM_DEBUG_DELAY;
3333 cam_dflags = start_ccb->cdbg.flags;
3334 if (cam_dpath != NULL) {
3335 xpt_free_path(cam_dpath);
3339 if (cam_dflags != CAM_DEBUG_NONE) {
3340 if (xpt_create_path(&cam_dpath, xpt_periph,
3341 start_ccb->ccb_h.path_id,
3342 start_ccb->ccb_h.target_id,
3343 start_ccb->ccb_h.target_lun) !=
3345 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3346 cam_dflags = CAM_DEBUG_NONE;
3348 start_ccb->ccb_h.status = CAM_REQ_CMP;
3349 xpt_print_path(cam_dpath);
3350 printf("debugging flags now %x\n", cam_dflags);
3354 start_ccb->ccb_h.status = CAM_REQ_CMP;
3357 #else /* !CAMDEBUG */
3358 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3359 #endif /* CAMDEBUG */
3363 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3364 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3365 start_ccb->ccb_h.status = CAM_REQ_CMP;
3372 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3379 xpt_polled_action(union ccb *start_ccb)
3383 struct cam_sim *sim;
3384 struct cam_devq *devq;
3387 timeout = start_ccb->ccb_h.timeout;
3388 sim = start_ccb->ccb_h.path->bus->sim;
3390 dev = start_ccb->ccb_h.path->device;
3395 * Steal an opening so that no other queued requests
3396 * can get it before us while we simulate interrupts.
3398 dev->ccbq.devq_openings--;
3399 dev->ccbq.dev_openings--;
3401 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3402 && (--timeout > 0)) {
3404 (*(sim->sim_poll))(sim);
3409 dev->ccbq.devq_openings++;
3410 dev->ccbq.dev_openings++;
3413 xpt_action(start_ccb);
3414 while(--timeout > 0) {
3415 (*(sim->sim_poll))(sim);
3418 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3425 * XXX Is it worth adding a sim_timeout entry
3426 * point so we can attempt recovery? If
3427 * this is only used for dumps, I don't think
3430 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3433 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3439 * Schedule a peripheral driver to receive a ccb when it's
3440 * target device has space for more transactions.
3443 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3445 struct cam_ed *device;
3449 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3450 device = perph->path->device;
3452 if (periph_is_queued(perph)) {
3453 /* Simply reorder based on new priority */
3454 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3455 (" change priority to %d\n", new_priority));
3456 if (new_priority < perph->pinfo.priority) {
3457 camq_change_priority(&device->drvq,
3463 /* New entry on the queue */
3464 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3465 (" added periph to queue\n"));
3466 perph->pinfo.priority = new_priority;
3467 perph->pinfo.generation = ++device->drvq.generation;
3468 camq_insert(&device->drvq, &perph->pinfo);
3469 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3473 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3474 (" calling xpt_run_devq\n"));
3475 xpt_run_dev_allocq(perph->path->bus);
3481 * Schedule a device to run on a given queue.
3482 * If the device was inserted as a new entry on the queue,
3483 * return 1 meaning the device queue should be run. If we
3484 * were already queued, implying someone else has already
3485 * started the queue, return 0 so the caller doesn't attempt
3486 * to run the queue. Must be run at either splsoftcam
3487 * (or splcam since that encompases splsoftcam).
3490 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3491 u_int32_t new_priority)
3494 u_int32_t old_priority;
3496 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3498 old_priority = pinfo->priority;
3501 * Are we already queued?
3503 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3504 /* Simply reorder based on new priority */
3505 if (new_priority < old_priority) {
3506 camq_change_priority(queue, pinfo->index,
3508 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3509 ("changed priority to %d\n",
3514 /* New entry on the queue */
3515 if (new_priority < old_priority)
3516 pinfo->priority = new_priority;
3518 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3519 ("Inserting onto queue\n"));
3520 pinfo->generation = ++queue->generation;
3521 camq_insert(queue, pinfo);
3528 xpt_run_dev_allocq(struct cam_eb *bus)
3530 struct cam_devq *devq;
3533 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3534 devq = bus->sim->devq;
3536 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3537 (" qfrozen_cnt == 0x%x, entries == %d, "
3538 "openings == %d, active == %d\n",
3539 devq->alloc_queue.qfrozen_cnt,
3540 devq->alloc_queue.entries,
3541 devq->alloc_openings,
3542 devq->alloc_active));
3545 devq->alloc_queue.qfrozen_cnt++;
3546 while ((devq->alloc_queue.entries > 0)
3547 && (devq->alloc_openings > 0)
3548 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3549 struct cam_ed_qinfo *qinfo;
3550 struct cam_ed *device;
3551 union ccb *work_ccb;
3552 struct cam_periph *drv;
3555 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3557 device = qinfo->device;
3559 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3560 ("running device %p\n", device));
3562 drvq = &device->drvq;
3565 if (drvq->entries <= 0) {
3566 panic("xpt_run_dev_allocq: "
3567 "Device on queue without any work to do");
3570 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3571 devq->alloc_openings--;
3572 devq->alloc_active++;
3573 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3575 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3576 drv->pinfo.priority);
3577 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3578 ("calling periph start\n"));
3579 drv->periph_start(drv, work_ccb);
3582 * Malloc failure in alloc_ccb
3585 * XXX add us to a list to be run from free_ccb
3586 * if we don't have any ccbs active on this
3587 * device queue otherwise we may never get run
3593 /* Raise IPL for possible insertion and test at top of loop */
3596 if (drvq->entries > 0) {
3597 /* We have more work. Attempt to reschedule */
3598 xpt_schedule_dev_allocq(bus, device);
3601 devq->alloc_queue.qfrozen_cnt--;
3606 xpt_run_dev_sendq(struct cam_eb *bus)
3608 struct cam_devq *devq;
3611 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3613 devq = bus->sim->devq;
3616 devq->send_queue.qfrozen_cnt++;
3619 while ((devq->send_queue.entries > 0)
3620 && (devq->send_openings > 0)) {
3621 struct cam_ed_qinfo *qinfo;
3622 struct cam_ed *device;
3623 union ccb *work_ccb;
3624 struct cam_sim *sim;
3628 if (devq->send_queue.qfrozen_cnt > 1) {
3633 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3635 device = qinfo->device;
3638 * If the device has been "frozen", don't attempt
3641 if (device->qfrozen_cnt > 0) {
3646 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3647 ("running device %p\n", device));
3649 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3650 if (work_ccb == NULL) {
3651 printf("device on run queue with no ccbs???\n");
3656 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3658 if (num_highpower <= 0) {
3660 * We got a high power command, but we
3661 * don't have any available slots. Freeze
3662 * the device queue until we have a slot
3665 device->qfrozen_cnt++;
3666 STAILQ_INSERT_TAIL(&highpowerq,
3674 * Consume a high power slot while
3680 devq->active_dev = device;
3681 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3683 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3686 devq->send_openings--;
3687 devq->send_active++;
3689 if (device->ccbq.queue.entries > 0)
3690 xpt_schedule_dev_sendq(bus, device);
3692 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3694 * The client wants to freeze the queue
3695 * after this CCB is sent.
3698 device->qfrozen_cnt++;
3704 /* In Target mode, the peripheral driver knows best... */
3705 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3706 if ((device->inq_flags & SID_CmdQue) != 0
3707 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3708 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3711 * Clear this in case of a retried CCB that
3712 * failed due to a rejected tag.
3714 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3718 * Device queues can be shared among multiple sim instances
3719 * that reside on different busses. Use the SIM in the queue
3720 * CCB's path, rather than the one in the bus that was passed
3721 * into this function.
3723 sim = work_ccb->ccb_h.path->bus->sim;
3724 (*(sim->sim_action))(sim, work_ccb);
3727 devq->active_dev = NULL;
3729 /* Raise IPL for possible insertion and test at top of loop */
3734 devq->send_queue.qfrozen_cnt--;
3739 * This function merges stuff from the slave ccb into the master ccb, while
3740 * keeping important fields in the master ccb constant.
3743 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3746 * Pull fields that are valid for peripheral drivers to set
3747 * into the master CCB along with the CCB "payload".
3749 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3750 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3751 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3752 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3753 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3754 sizeof(union ccb) - sizeof(struct ccb_hdr));
3758 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3760 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3761 ccb_h->pinfo.priority = priority;
3763 ccb_h->path_id = path->bus->path_id;
3765 ccb_h->target_id = path->target->target_id;
3767 ccb_h->target_id = CAM_TARGET_WILDCARD;
3769 ccb_h->target_lun = path->device->lun_id;
3770 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3772 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3774 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3778 /* Path manipulation functions */
3780 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3781 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3783 struct cam_path *path;
3786 path = malloc(sizeof(*path), M_DEVBUF, M_INTWAIT);
3787 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3788 if (status != CAM_REQ_CMP) {
3789 free(path, M_DEVBUF);
3792 *new_path_ptr = path;
3797 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3798 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3801 struct cam_et *target;
3802 struct cam_ed *device;
3806 status = CAM_REQ_CMP; /* Completed without error */
3807 target = NULL; /* Wildcarded */
3808 device = NULL; /* Wildcarded */
3811 * We will potentially modify the EDT, so block interrupts
3812 * that may attempt to create cam paths.
3815 bus = xpt_find_bus(path_id);
3817 status = CAM_PATH_INVALID;
3819 target = xpt_find_target(bus, target_id);
3820 if (target == NULL) {
3822 struct cam_et *new_target;
3824 new_target = xpt_alloc_target(bus, target_id);
3825 if (new_target == NULL) {
3826 status = CAM_RESRC_UNAVAIL;
3828 target = new_target;
3831 if (target != NULL) {
3832 device = xpt_find_device(target, lun_id);
3833 if (device == NULL) {
3835 struct cam_ed *new_device;
3837 new_device = xpt_alloc_device(bus,
3840 if (new_device == NULL) {
3841 status = CAM_RESRC_UNAVAIL;
3843 device = new_device;
3851 * Only touch the user's data if we are successful.
3853 if (status == CAM_REQ_CMP) {
3854 new_path->periph = perph;
3855 new_path->bus = bus;
3856 new_path->target = target;
3857 new_path->device = device;
3858 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3861 xpt_release_device(bus, target, device);
3863 xpt_release_target(bus, target);
3865 xpt_release_bus(bus);
3871 xpt_release_path(struct cam_path *path)
3873 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3874 if (path->device != NULL) {
3875 xpt_release_device(path->bus, path->target, path->device);
3876 path->device = NULL;
3878 if (path->target != NULL) {
3879 xpt_release_target(path->bus, path->target);
3880 path->target = NULL;
3882 if (path->bus != NULL) {
3883 xpt_release_bus(path->bus);
3889 xpt_free_path(struct cam_path *path)
3891 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3892 xpt_release_path(path);
3893 free(path, M_DEVBUF);
3898 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3899 * in path1, 2 for match with wildcards in path2.
3902 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3906 if (path1->bus != path2->bus) {
3907 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3909 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3914 if (path1->target != path2->target) {
3915 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3918 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3923 if (path1->device != path2->device) {
3924 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3927 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3936 xpt_print_path(struct cam_path *path)
3939 printf("(nopath): ");
3941 if (path->periph != NULL)
3942 printf("(%s%d:", path->periph->periph_name,
3943 path->periph->unit_number);
3945 printf("(noperiph:");
3947 if (path->bus != NULL)
3948 printf("%s%d:%d:", path->bus->sim->sim_name,
3949 path->bus->sim->unit_number,
3950 path->bus->sim->bus_id);
3954 if (path->target != NULL)
3955 printf("%d:", path->target->target_id);
3959 if (path->device != NULL)
3960 printf("%d): ", path->device->lun_id);
3967 xpt_path_path_id(struct cam_path *path)
3969 return(path->bus->path_id);
3973 xpt_path_target_id(struct cam_path *path)
3975 if (path->target != NULL)
3976 return (path->target->target_id);
3978 return (CAM_TARGET_WILDCARD);
3982 xpt_path_lun_id(struct cam_path *path)
3984 if (path->device != NULL)
3985 return (path->device->lun_id);
3987 return (CAM_LUN_WILDCARD);
3991 xpt_path_sim(struct cam_path *path)
3993 return (path->bus->sim);
3997 xpt_path_periph(struct cam_path *path)
3999 return (path->periph);
4003 * Release a CAM control block for the caller. Remit the cost of the structure
4004 * to the device referenced by the path. If the this device had no 'credits'
4005 * and peripheral drivers have registered async callbacks for this notification
4009 xpt_release_ccb(union ccb *free_ccb)
4012 struct cam_path *path;
4013 struct cam_ed *device;
4016 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4017 path = free_ccb->ccb_h.path;
4018 device = path->device;
4021 cam_ccbq_release_opening(&device->ccbq);
4022 if (xpt_ccb_count > xpt_max_ccbs) {
4023 xpt_free_ccb(free_ccb);
4026 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4028 bus->sim->devq->alloc_openings++;
4029 bus->sim->devq->alloc_active--;
4030 /* XXX Turn this into an inline function - xpt_run_device?? */
4031 if ((device_is_alloc_queued(device) == 0)
4032 && (device->drvq.entries > 0)) {
4033 xpt_schedule_dev_allocq(bus, device);
4036 if (dev_allocq_is_runnable(bus->sim->devq))
4037 xpt_run_dev_allocq(bus);
4040 /* Functions accessed by SIM drivers */
4043 * A sim structure, listing the SIM entry points and instance
4044 * identification info is passed to xpt_bus_register to hook the SIM
4045 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4046 * for this new bus and places it in the array of busses and assigns
4047 * it a path_id. The path_id may be influenced by "hard wiring"
4048 * information specified by the user. Once interrupt services are
4049 * availible, the bus will be probed.
4052 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4054 struct cam_eb *new_bus;
4055 struct cam_eb *old_bus;
4056 struct ccb_pathinq cpi;
4060 new_bus = malloc(sizeof(*new_bus), M_DEVBUF, M_INTWAIT);
4062 if (strcmp(sim->sim_name, "xpt") != 0) {
4064 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4067 TAILQ_INIT(&new_bus->et_entries);
4068 new_bus->path_id = sim->path_id;
4071 timevalclear(&new_bus->last_reset);
4073 new_bus->refcount = 1; /* Held until a bus_deregister event */
4074 new_bus->generation = 0;
4076 old_bus = TAILQ_FIRST(&xpt_busses);
4077 while (old_bus != NULL
4078 && old_bus->path_id < new_bus->path_id)
4079 old_bus = TAILQ_NEXT(old_bus, links);
4080 if (old_bus != NULL)
4081 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4083 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4087 /* Notify interested parties */
4088 if (sim->path_id != CAM_XPT_PATH_ID) {
4089 struct cam_path path;
4091 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4092 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4093 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4094 cpi.ccb_h.func_code = XPT_PATH_INQ;
4095 xpt_action((union ccb *)&cpi);
4096 xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi);
4097 xpt_release_path(&path);
4099 return (CAM_SUCCESS);
4103 * Deregister a bus. We must clean out all transactions pending on the bus.
4104 * This routine is typically called prior to cam_sim_free() (e.g. see
4105 * dev/usbmisc/umass/umass.c)
4108 xpt_bus_deregister(path_id_t pathid)
4110 struct cam_path bus_path;
4113 status = xpt_compile_path(&bus_path, NULL, pathid,
4114 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4115 if (status != CAM_REQ_CMP)
4119 * This should clear out all pending requests and timeouts, but
4120 * the ccb's may be queued to a software interrupt.
4122 * XXX AC_LOST_DEVICE does not precisely abort the pending requests,
4123 * and it really ought to.
4125 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4126 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4128 /* make sure all responses have been processed */
4132 /* Release the reference count held while registered. */
4133 xpt_release_bus(bus_path.bus);
4134 xpt_release_path(&bus_path);
4136 return (CAM_REQ_CMP);
4140 xptnextfreepathid(void)
4147 bus = TAILQ_FIRST(&xpt_busses);
4149 /* Find an unoccupied pathid */
4151 && bus->path_id <= pathid) {
4152 if (bus->path_id == pathid)
4154 bus = TAILQ_NEXT(bus, links);
4158 * Ensure that this pathid is not reserved for
4159 * a bus that may be registered in the future.
4161 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4163 /* Start the search over */
4170 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4174 char buf[32], *strval;
4176 pathid = CAM_XPT_PATH_ID;
4177 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4179 while ((i = resource_locate(i, "scbus")) != -1) {
4180 dunit = resource_query_unit(i);
4181 if (dunit < 0) /* unwired?! */
4183 if (resource_string_value("scbus", dunit, "at", &strval) != 0)
4185 if (strcmp(buf, strval) != 0)
4187 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4188 if (sim_bus == val) {
4192 } else if (sim_bus == 0) {
4193 /* Unspecified matches bus 0 */
4197 printf("Ambiguous scbus configuration for %s%d "
4198 "bus %d, cannot wire down. The kernel "
4199 "config entry for scbus%d should "
4200 "specify a controller bus.\n"
4201 "Scbus will be assigned dynamically.\n",
4202 sim_name, sim_unit, sim_bus, dunit);
4207 if (pathid == CAM_XPT_PATH_ID)
4208 pathid = xptnextfreepathid();
4213 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4216 struct cam_et *target, *next_target;
4217 struct cam_ed *device, *next_device;
4220 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4223 * Most async events come from a CAM interrupt context. In
4224 * a few cases, the error recovery code at the peripheral layer,
4225 * which may run from our SWI or a process context, may signal
4226 * deferred events with a call to xpt_async. Ensure async
4227 * notifications are serialized by blocking cam interrupts.
4233 if (async_code == AC_BUS_RESET) {
4234 /* Update our notion of when the last reset occurred */
4235 microuptime(&bus->last_reset);
4238 for (target = TAILQ_FIRST(&bus->et_entries);
4240 target = next_target) {
4242 next_target = TAILQ_NEXT(target, links);
4244 if (path->target != target
4245 && path->target->target_id != CAM_TARGET_WILDCARD
4246 && target->target_id != CAM_TARGET_WILDCARD)
4249 if (async_code == AC_SENT_BDR) {
4250 /* Update our notion of when the last reset occurred */
4251 microuptime(&path->target->last_reset);
4254 for (device = TAILQ_FIRST(&target->ed_entries);
4256 device = next_device) {
4258 next_device = TAILQ_NEXT(device, links);
4260 if (path->device != device
4261 && path->device->lun_id != CAM_LUN_WILDCARD
4262 && device->lun_id != CAM_LUN_WILDCARD)
4265 xpt_dev_async(async_code, bus, target,
4268 xpt_async_bcast(&device->asyncs, async_code,
4274 * If this wasn't a fully wildcarded async, tell all
4275 * clients that want all async events.
4277 if (bus != xpt_periph->path->bus)
4278 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4284 xpt_async_bcast(struct async_list *async_head,
4285 u_int32_t async_code,
4286 struct cam_path *path, void *async_arg)
4288 struct async_node *cur_entry;
4290 cur_entry = SLIST_FIRST(async_head);
4291 while (cur_entry != NULL) {
4292 struct async_node *next_entry;
4294 * Grab the next list entry before we call the current
4295 * entry's callback. This is because the callback function
4296 * can delete its async callback entry.
4298 next_entry = SLIST_NEXT(cur_entry, links);
4299 if ((cur_entry->event_enable & async_code) != 0)
4300 cur_entry->callback(cur_entry->callback_arg,
4303 cur_entry = next_entry;
4308 * Handle any per-device event notifications that require action by the XPT.
4311 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4312 struct cam_ed *device, void *async_arg)
4315 struct cam_path newpath;
4318 * We only need to handle events for real devices.
4320 if (target->target_id == CAM_TARGET_WILDCARD
4321 || device->lun_id == CAM_LUN_WILDCARD)
4325 * We need our own path with wildcards expanded to
4326 * handle certain types of events.
4328 if ((async_code == AC_SENT_BDR)
4329 || (async_code == AC_BUS_RESET)
4330 || (async_code == AC_INQ_CHANGED))
4331 status = xpt_compile_path(&newpath, NULL,
4336 status = CAM_REQ_CMP_ERR;
4338 if (status == CAM_REQ_CMP) {
4341 * Allow transfer negotiation to occur in a
4342 * tag free environment.
4344 if (async_code == AC_SENT_BDR
4345 || async_code == AC_BUS_RESET)
4346 xpt_toggle_tags(&newpath);
4348 if (async_code == AC_INQ_CHANGED) {
4350 * We've sent a start unit command, or
4351 * something similar to a device that
4352 * may have caused its inquiry data to
4353 * change. So we re-scan the device to
4354 * refresh the inquiry data for it.
4356 xpt_scan_lun(newpath.periph, &newpath,
4357 CAM_EXPECT_INQ_CHANGE, NULL);
4359 xpt_release_path(&newpath);
4360 } else if (async_code == AC_LOST_DEVICE) {
4362 * When we lose a device the device may be about to detach
4363 * the sim, we have to clear out all pending timeouts and
4364 * requests before that happens. XXX it would be nice if
4365 * we could abort the requests pertaining to the device.
4367 xpt_release_devq_timeout(device);
4368 if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
4369 device->flags |= CAM_DEV_UNCONFIGURED;
4370 xpt_release_device(bus, target, device);
4372 } else if (async_code == AC_TRANSFER_NEG) {
4373 struct ccb_trans_settings *settings;
4375 settings = (struct ccb_trans_settings *)async_arg;
4376 xpt_set_transfer_settings(settings, device,
4377 /*async_update*/TRUE);
4382 xpt_freeze_devq(struct cam_path *path, u_int count)
4385 struct ccb_hdr *ccbh;
4388 path->device->qfrozen_cnt += count;
4391 * Mark the last CCB in the queue as needing
4392 * to be requeued if the driver hasn't
4393 * changed it's state yet. This fixes a race
4394 * where a ccb is just about to be queued to
4395 * a controller driver when it's interrupt routine
4396 * freezes the queue. To completly close the
4397 * hole, controller drives must check to see
4398 * if a ccb's status is still CAM_REQ_INPROG
4399 * under spl protection just before they queue
4400 * the CCB. See ahc_action/ahc_freeze_devq for
4403 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4404 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4405 ccbh->status = CAM_REQUEUE_REQ;
4407 return (path->device->qfrozen_cnt);
4411 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4413 sim->devq->send_queue.qfrozen_cnt += count;
4414 if (sim->devq->active_dev != NULL) {
4415 struct ccb_hdr *ccbh;
4417 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4419 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4420 ccbh->status = CAM_REQUEUE_REQ;
4422 return (sim->devq->send_queue.qfrozen_cnt);
4426 * WARNING: most devices, especially USB/UMASS, may detach their sim early.
4427 * We ref-count the sim (and the bus only NULLs it out when the bus has been
4428 * freed, which is not the case here), but the device queue is also freed XXX
4429 * and we have to check that here.
4431 * XXX fixme: could we simply not null-out the device queue via
4435 xpt_release_devq_timeout(void *arg)
4437 struct cam_ed *device;
4439 device = (struct cam_ed *)arg;
4441 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4445 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4447 xpt_release_devq_device(path->device, count, run_queue);
4451 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4460 if (dev->qfrozen_cnt > 0) {
4462 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4463 dev->qfrozen_cnt -= count;
4464 if (dev->qfrozen_cnt == 0) {
4467 * No longer need to wait for a successful
4468 * command completion.
4470 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4473 * Remove any timeouts that might be scheduled
4474 * to release this queue.
4476 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4477 untimeout(xpt_release_devq_timeout, dev,
4479 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4483 * Now that we are unfrozen schedule the
4484 * device so any pending transactions are
4487 if ((dev->ccbq.queue.entries > 0)
4488 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4489 && (run_queue != 0)) {
4496 xpt_run_dev_sendq(dev->target->bus);
4501 xpt_release_simq(struct cam_sim *sim, int run_queue)
4506 sendq = &(sim->devq->send_queue);
4508 if (sendq->qfrozen_cnt > 0) {
4510 sendq->qfrozen_cnt--;
4511 if (sendq->qfrozen_cnt == 0) {
4515 * If there is a timeout scheduled to release this
4516 * sim queue, remove it. The queue frozen count is
4519 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4520 untimeout(xpt_release_simq_timeout, sim,
4522 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4524 bus = xpt_find_bus(sim->path_id);
4529 * Now that we are unfrozen run the send queue.
4531 xpt_run_dev_sendq(bus);
4533 xpt_release_bus(bus);
4541 xpt_release_simq_timeout(void *arg)
4543 struct cam_sim *sim;
4545 sim = (struct cam_sim *)arg;
4546 xpt_release_simq(sim, /* run_queue */ TRUE);
4550 xpt_done(union ccb *done_ccb)
4556 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4557 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4559 * Queue up the request for handling by our SWI handler
4560 * any of the "non-immediate" type of ccbs.
4562 switch (done_ccb->ccb_h.path->periph->type) {
4563 case CAM_PERIPH_BIO:
4564 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4566 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4569 case CAM_PERIPH_NET:
4570 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4572 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4585 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_INTWAIT);
4590 xpt_free_ccb(union ccb *free_ccb)
4592 free(free_ccb, M_DEVBUF);
4597 /* Private XPT functions */
4600 * Get a CAM control block for the caller. Charge the structure to the device
4601 * referenced by the path. If the this device has no 'credits' then the
4602 * device already has the maximum number of outstanding operations under way
4603 * and we return NULL. If we don't have sufficient resources to allocate more
4604 * ccbs, we also return NULL.
4607 xpt_get_ccb(struct cam_ed *device)
4613 if ((new_ccb = (union ccb *)ccb_freeq.slh_first) == NULL) {
4614 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_INTWAIT);
4615 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4616 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4620 cam_ccbq_take_opening(&device->ccbq);
4621 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4627 xpt_release_bus(struct cam_eb *bus)
4631 if (bus->refcount == 1) {
4632 KKASSERT(TAILQ_FIRST(&bus->et_entries) == NULL);
4633 TAILQ_REMOVE(&xpt_busses, bus, links);
4635 cam_sim_release(bus->sim, 0);
4639 KKASSERT(bus->refcount == 1);
4640 free(bus, M_DEVBUF);
4647 static struct cam_et *
4648 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4650 struct cam_et *target;
4651 struct cam_et *cur_target;
4653 target = malloc(sizeof(*target), M_DEVBUF, M_INTWAIT);
4655 TAILQ_INIT(&target->ed_entries);
4657 target->target_id = target_id;
4658 target->refcount = 1;
4659 target->generation = 0;
4660 timevalclear(&target->last_reset);
4662 * Hold a reference to our parent bus so it
4663 * will not go away before we do.
4667 /* Insertion sort into our bus's target list */
4668 cur_target = TAILQ_FIRST(&bus->et_entries);
4669 while (cur_target != NULL && cur_target->target_id < target_id)
4670 cur_target = TAILQ_NEXT(cur_target, links);
4672 if (cur_target != NULL) {
4673 TAILQ_INSERT_BEFORE(cur_target, target, links);
4675 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4682 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4685 if (target->refcount == 1) {
4686 KKASSERT(TAILQ_FIRST(&target->ed_entries) == NULL);
4687 TAILQ_REMOVE(&bus->et_entries, target, links);
4689 xpt_release_bus(bus);
4690 KKASSERT(target->refcount == 1);
4691 free(target, M_DEVBUF);
4698 static struct cam_ed *
4699 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4701 struct cam_ed *device;
4702 struct cam_devq *devq;
4705 /* Make space for us in the device queue on our bus */
4706 devq = bus->sim->devq;
4707 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4709 if (status != CAM_REQ_CMP) {
4712 device = malloc(sizeof(*device), M_DEVBUF, M_INTWAIT);
4715 if (device != NULL) {
4716 struct cam_ed *cur_device;
4718 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4719 device->alloc_ccb_entry.device = device;
4720 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4721 device->send_ccb_entry.device = device;
4722 device->target = target;
4723 device->lun_id = lun_id;
4724 /* Initialize our queues */
4725 if (camq_init(&device->drvq, 0) != 0) {
4726 free(device, M_DEVBUF);
4729 if (cam_ccbq_init(&device->ccbq,
4730 bus->sim->max_dev_openings) != 0) {
4731 camq_fini(&device->drvq);
4732 free(device, M_DEVBUF);
4735 SLIST_INIT(&device->asyncs);
4736 SLIST_INIT(&device->periphs);
4737 device->generation = 0;
4738 device->owner = NULL;
4740 * Take the default quirk entry until we have inquiry
4741 * data and can determine a better quirk to use.
4743 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4744 bzero(&device->inq_data, sizeof(device->inq_data));
4745 device->inq_flags = 0;
4746 device->queue_flags = 0;
4747 device->serial_num = NULL;
4748 device->serial_num_len = 0;
4749 device->qfrozen_cnt = 0;
4750 device->flags = CAM_DEV_UNCONFIGURED;
4751 device->tag_delay_count = 0;
4752 device->refcount = 1;
4753 callout_handle_init(&device->c_handle);
4756 * Hold a reference to our parent target so it
4757 * will not go away before we do.
4762 * XXX should be limited by number of CCBs this bus can
4765 xpt_max_ccbs += device->ccbq.devq_openings;
4766 /* Insertion sort into our target's device list */
4767 cur_device = TAILQ_FIRST(&target->ed_entries);
4768 while (cur_device != NULL && cur_device->lun_id < lun_id)
4769 cur_device = TAILQ_NEXT(cur_device, links);
4770 if (cur_device != NULL) {
4771 TAILQ_INSERT_BEFORE(cur_device, device, links);
4773 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4775 target->generation++;
4781 xpt_reference_device(struct cam_ed *device)
4787 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4788 struct cam_ed *device)
4790 struct cam_devq *devq;
4793 if (device->refcount == 1) {
4794 KKASSERT(device->flags & CAM_DEV_UNCONFIGURED);
4796 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4797 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4798 panic("Removing device while still queued for ccbs");
4800 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4801 device->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4802 untimeout(xpt_release_devq_timeout, device,
4806 TAILQ_REMOVE(&target->ed_entries, device,links);
4807 target->generation++;
4808 xpt_max_ccbs -= device->ccbq.devq_openings;
4809 /* Release our slot in the devq */
4810 devq = bus->sim->devq;
4811 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4812 xpt_release_target(bus, target);
4813 KKASSERT(device->refcount == 1);
4814 free(device, M_DEVBUF);
4822 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4832 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4833 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4834 if (result == CAM_REQ_CMP && (diff < 0)) {
4835 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4837 /* Adjust the global limit */
4838 xpt_max_ccbs += diff;
4843 static struct cam_eb *
4844 xpt_find_bus(path_id_t path_id)
4848 for (bus = TAILQ_FIRST(&xpt_busses);
4850 bus = TAILQ_NEXT(bus, links)) {
4851 if (bus->path_id == path_id) {
4859 static struct cam_et *
4860 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4862 struct cam_et *target;
4864 for (target = TAILQ_FIRST(&bus->et_entries);
4866 target = TAILQ_NEXT(target, links)) {
4867 if (target->target_id == target_id) {
4875 static struct cam_ed *
4876 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4878 struct cam_ed *device;
4880 for (device = TAILQ_FIRST(&target->ed_entries);
4882 device = TAILQ_NEXT(device, links)) {
4883 if (device->lun_id == lun_id) {
4892 union ccb *request_ccb;
4893 struct ccb_pathinq *cpi;
4895 } xpt_scan_bus_info;
4898 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
4899 * As the scan progresses, xpt_scan_bus is used as the
4900 * callback on completion function.
4903 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
4905 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4906 ("xpt_scan_bus\n"));
4907 switch (request_ccb->ccb_h.func_code) {
4910 xpt_scan_bus_info *scan_info;
4911 union ccb *work_ccb;
4912 struct cam_path *path;
4917 /* Find out the characteristics of the bus */
4918 work_ccb = xpt_alloc_ccb();
4919 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
4920 request_ccb->ccb_h.pinfo.priority);
4921 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4922 xpt_action(work_ccb);
4923 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4924 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
4925 xpt_free_ccb(work_ccb);
4926 xpt_done(request_ccb);
4930 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
4932 * Can't scan the bus on an adapter that
4933 * cannot perform the initiator role.
4935 request_ccb->ccb_h.status = CAM_REQ_CMP;
4936 xpt_free_ccb(work_ccb);
4937 xpt_done(request_ccb);
4941 /* Save some state for use while we probe for devices */
4942 scan_info = (xpt_scan_bus_info *)
4943 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_INTWAIT);
4944 scan_info->request_ccb = request_ccb;
4945 scan_info->cpi = &work_ccb->cpi;
4947 /* Cache on our stack so we can work asynchronously */
4948 max_target = scan_info->cpi->max_target;
4949 initiator_id = scan_info->cpi->initiator_id;
4952 * Don't count the initiator if the
4953 * initiator is addressable.
4955 scan_info->pending_count = max_target + 1;
4956 if (initiator_id <= max_target)
4957 scan_info->pending_count--;
4959 for (i = 0; i <= max_target; i++) {
4961 if (i == initiator_id)
4964 status = xpt_create_path(&path, xpt_periph,
4965 request_ccb->ccb_h.path_id,
4967 if (status != CAM_REQ_CMP) {
4968 printf("xpt_scan_bus: xpt_create_path failed"
4969 " with status %#x, bus scan halted\n",
4973 work_ccb = xpt_alloc_ccb();
4974 xpt_setup_ccb(&work_ccb->ccb_h, path,
4975 request_ccb->ccb_h.pinfo.priority);
4976 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
4977 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
4978 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
4979 work_ccb->crcn.flags = request_ccb->crcn.flags;
4981 printf("xpt_scan_bus: probing %d:%d:%d\n",
4982 request_ccb->ccb_h.path_id, i, 0);
4984 xpt_action(work_ccb);
4990 xpt_scan_bus_info *scan_info;
4992 target_id_t target_id;
4995 /* Reuse the same CCB to query if a device was really found */
4996 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
4997 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
4998 request_ccb->ccb_h.pinfo.priority);
4999 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5001 path_id = request_ccb->ccb_h.path_id;
5002 target_id = request_ccb->ccb_h.target_id;
5003 lun_id = request_ccb->ccb_h.target_lun;
5004 xpt_action(request_ccb);
5007 printf("xpt_scan_bus: got back probe from %d:%d:%d\n",
5008 path_id, target_id, lun_id);
5011 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5012 struct cam_ed *device;
5013 struct cam_et *target;
5017 * If we already probed lun 0 successfully, or
5018 * we have additional configured luns on this
5019 * target that might have "gone away", go onto
5022 target = request_ccb->ccb_h.path->target;
5024 * We may touch devices that we don't
5025 * hold references too, so ensure they
5026 * don't disappear out from under us.
5027 * The target above is referenced by the
5028 * path in the request ccb.
5032 device = TAILQ_FIRST(&target->ed_entries);
5033 if (device != NULL) {
5034 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5035 if (device->lun_id == 0)
5036 device = TAILQ_NEXT(device, links);
5039 if ((lun_id != 0) || (device != NULL)) {
5040 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5044 struct cam_ed *device;
5046 device = request_ccb->ccb_h.path->device;
5048 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5049 /* Try the next lun */
5050 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5051 (device->quirk->quirks & CAM_QUIRK_HILUNS))
5056 xpt_free_path(request_ccb->ccb_h.path);
5059 if ((lun_id == request_ccb->ccb_h.target_lun)
5060 || lun_id > scan_info->cpi->max_lun) {
5063 xpt_free_ccb(request_ccb);
5064 scan_info->pending_count--;
5065 if (scan_info->pending_count == 0) {
5066 xpt_free_ccb((union ccb *)scan_info->cpi);
5067 request_ccb = scan_info->request_ccb;
5068 free(scan_info, M_TEMP);
5069 request_ccb->ccb_h.status = CAM_REQ_CMP;
5070 xpt_done(request_ccb);
5073 /* Try the next device */
5074 struct cam_path *path;
5077 path = request_ccb->ccb_h.path;
5078 status = xpt_create_path(&path, xpt_periph,
5079 path_id, target_id, lun_id);
5080 if (status != CAM_REQ_CMP) {
5081 printf("xpt_scan_bus: xpt_create_path failed "
5082 "with status %#x, halting LUN scan\n",
5084 xpt_free_ccb(request_ccb);
5085 scan_info->pending_count--;
5086 if (scan_info->pending_count == 0) {
5088 (union ccb *)scan_info->cpi);
5089 request_ccb = scan_info->request_ccb;
5090 free(scan_info, M_TEMP);
5091 request_ccb->ccb_h.status = CAM_REQ_CMP;
5092 xpt_done(request_ccb);
5096 xpt_setup_ccb(&request_ccb->ccb_h, path,
5097 request_ccb->ccb_h.pinfo.priority);
5098 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5099 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5100 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5101 request_ccb->crcn.flags =
5102 scan_info->request_ccb->crcn.flags;
5104 xpt_print_path(path);
5105 printf("xpt_scan bus probing\n");
5107 xpt_action(request_ccb);
5122 PROBE_TUR_FOR_NEGOTIATION
5126 PROBE_INQUIRY_CKSUM = 0x01,
5127 PROBE_SERIAL_CKSUM = 0x02,
5128 PROBE_NO_ANNOUNCE = 0x04
5132 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5133 probe_action action;
5134 union ccb saved_ccb;
5137 u_int8_t digest[16];
5141 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5142 cam_flags flags, union ccb *request_ccb)
5144 struct ccb_pathinq cpi;
5146 struct cam_path *new_path;
5147 struct cam_periph *old_periph;
5150 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5151 ("xpt_scan_lun\n"));
5153 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5154 cpi.ccb_h.func_code = XPT_PATH_INQ;
5155 xpt_action((union ccb *)&cpi);
5157 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5158 if (request_ccb != NULL) {
5159 request_ccb->ccb_h.status = cpi.ccb_h.status;
5160 xpt_done(request_ccb);
5165 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5167 * Can't scan the bus on an adapter that
5168 * cannot perform the initiator role.
5170 if (request_ccb != NULL) {
5171 request_ccb->ccb_h.status = CAM_REQ_CMP;
5172 xpt_done(request_ccb);
5177 if (request_ccb == NULL) {
5178 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_INTWAIT);
5179 new_path = malloc(sizeof(*new_path), M_TEMP, M_INTWAIT);
5180 status = xpt_compile_path(new_path, xpt_periph,
5182 path->target->target_id,
5183 path->device->lun_id);
5185 if (status != CAM_REQ_CMP) {
5186 xpt_print_path(path);
5187 printf("xpt_scan_lun: can't compile path, can't "
5189 free(request_ccb, M_TEMP);
5190 free(new_path, M_TEMP);
5193 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5194 request_ccb->ccb_h.cbfcnp = xptscandone;
5195 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5196 request_ccb->crcn.flags = flags;
5200 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5203 softc = (probe_softc *)old_periph->softc;
5204 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5207 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5208 probestart, "probe",
5210 request_ccb->ccb_h.path, NULL, 0,
5213 if (status != CAM_REQ_CMP) {
5214 xpt_print_path(path);
5215 printf("xpt_scan_lun: cam_alloc_periph returned an "
5216 "error, can't continue probe\n");
5217 request_ccb->ccb_h.status = status;
5218 xpt_done(request_ccb);
5225 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5227 xpt_release_path(done_ccb->ccb_h.path);
5228 free(done_ccb->ccb_h.path, M_TEMP);
5229 free(done_ccb, M_TEMP);
5233 proberegister(struct cam_periph *periph, void *arg)
5235 union ccb *request_ccb; /* CCB representing the probe request */
5238 request_ccb = (union ccb *)arg;
5239 if (periph == NULL) {
5240 printf("proberegister: periph was NULL!!\n");
5241 return(CAM_REQ_CMP_ERR);
5244 if (request_ccb == NULL) {
5245 printf("proberegister: no probe CCB, "
5246 "can't register device\n");
5247 return(CAM_REQ_CMP_ERR);
5250 softc = malloc(sizeof(*softc), M_TEMP, M_INTWAIT | M_ZERO);
5251 TAILQ_INIT(&softc->request_ccbs);
5252 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5255 periph->softc = softc;
5256 cam_periph_acquire(periph);
5258 * Ensure we've waited at least a bus settle
5259 * delay before attempting to probe the device.
5260 * For HBAs that don't do bus resets, this won't make a difference.
5262 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5264 probeschedule(periph);
5265 return(CAM_REQ_CMP);
5269 probeschedule(struct cam_periph *periph)
5271 struct ccb_pathinq cpi;
5275 softc = (probe_softc *)periph->softc;
5276 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5278 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5279 cpi.ccb_h.func_code = XPT_PATH_INQ;
5280 xpt_action((union ccb *)&cpi);
5283 * If a device has gone away and another device, or the same one,
5284 * is back in the same place, it should have a unit attention
5285 * condition pending. It will not report the unit attention in
5286 * response to an inquiry, which may leave invalid transfer
5287 * negotiations in effect. The TUR will reveal the unit attention
5288 * condition. Only send the TUR for lun 0, since some devices
5289 * will get confused by commands other than inquiry to non-existent
5290 * luns. If you think a device has gone away start your scan from
5291 * lun 0. This will insure that any bogus transfer settings are
5294 * If we haven't seen the device before and the controller supports
5295 * some kind of transfer negotiation, negotiate with the first
5296 * sent command if no bus reset was performed at startup. This
5297 * ensures that the device is not confused by transfer negotiation
5298 * settings left over by loader or BIOS action.
5300 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5301 && (ccb->ccb_h.target_lun == 0)) {
5302 softc->action = PROBE_TUR;
5303 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5304 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5305 proberequestdefaultnegotiation(periph);
5306 softc->action = PROBE_INQUIRY;
5308 softc->action = PROBE_INQUIRY;
5311 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5312 softc->flags |= PROBE_NO_ANNOUNCE;
5314 softc->flags &= ~PROBE_NO_ANNOUNCE;
5316 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5320 probestart(struct cam_periph *periph, union ccb *start_ccb)
5322 /* Probe the device that our peripheral driver points to */
5323 struct ccb_scsiio *csio;
5326 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5328 softc = (probe_softc *)periph->softc;
5329 csio = &start_ccb->csio;
5331 switch (softc->action) {
5333 case PROBE_TUR_FOR_NEGOTIATION:
5335 scsi_test_unit_ready(csio,
5344 case PROBE_FULL_INQUIRY:
5347 struct scsi_inquiry_data *inq_buf;
5349 inq_buf = &periph->path->device->inq_data;
5351 * If the device is currently configured, we calculate an
5352 * MD5 checksum of the inquiry data, and if the serial number
5353 * length is greater than 0, add the serial number data
5354 * into the checksum as well. Once the inquiry and the
5355 * serial number check finish, we attempt to figure out
5356 * whether we still have the same device.
5358 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5360 MD5Init(&softc->context);
5361 MD5Update(&softc->context, (unsigned char *)inq_buf,
5362 sizeof(struct scsi_inquiry_data));
5363 softc->flags |= PROBE_INQUIRY_CKSUM;
5364 if (periph->path->device->serial_num_len > 0) {
5365 MD5Update(&softc->context,
5366 periph->path->device->serial_num,
5367 periph->path->device->serial_num_len);
5368 softc->flags |= PROBE_SERIAL_CKSUM;
5370 MD5Final(softc->digest, &softc->context);
5373 if (softc->action == PROBE_INQUIRY)
5374 inquiry_len = SHORT_INQUIRY_LENGTH;
5376 inquiry_len = inq_buf->additional_length + 5;
5382 (u_int8_t *)inq_buf,
5387 /*timeout*/60 * 1000);
5390 case PROBE_MODE_SENSE:
5395 mode_buf_len = sizeof(struct scsi_mode_header_6)
5396 + sizeof(struct scsi_mode_blk_desc)
5397 + sizeof(struct scsi_control_page);
5398 mode_buf = malloc(mode_buf_len, M_TEMP, M_INTWAIT);
5399 scsi_mode_sense(csio,
5404 SMS_PAGE_CTRL_CURRENT,
5405 SMS_CONTROL_MODE_PAGE,
5412 case PROBE_SERIAL_NUM:
5414 struct scsi_vpd_unit_serial_number *serial_buf;
5415 struct cam_ed* device;
5418 device = periph->path->device;
5419 device->serial_num = NULL;
5420 device->serial_num_len = 0;
5422 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0) {
5423 serial_buf = malloc(sizeof(*serial_buf), M_TEMP,
5424 M_INTWAIT | M_ZERO);
5429 (u_int8_t *)serial_buf,
5430 sizeof(*serial_buf),
5432 SVPD_UNIT_SERIAL_NUMBER,
5434 /*timeout*/60 * 1000);
5438 * We'll have to do without, let our probedone
5439 * routine finish up for us.
5441 start_ccb->csio.data_ptr = NULL;
5442 probedone(periph, start_ccb);
5446 xpt_action(start_ccb);
5450 proberequestdefaultnegotiation(struct cam_periph *periph)
5452 struct ccb_trans_settings cts;
5454 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5455 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5456 cts.flags = CCB_TRANS_USER_SETTINGS;
5457 xpt_action((union ccb *)&cts);
5458 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5459 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5460 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5461 xpt_action((union ccb *)&cts);
5465 probedone(struct cam_periph *periph, union ccb *done_ccb)
5468 struct cam_path *path;
5471 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5473 softc = (probe_softc *)periph->softc;
5474 path = done_ccb->ccb_h.path;
5475 priority = done_ccb->ccb_h.pinfo.priority;
5477 switch (softc->action) {
5480 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5482 if (cam_periph_error(done_ccb, 0,
5483 SF_NO_PRINT, NULL) == ERESTART)
5485 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5486 /* Don't wedge the queue */
5487 xpt_release_devq(done_ccb->ccb_h.path,
5491 softc->action = PROBE_INQUIRY;
5492 xpt_release_ccb(done_ccb);
5493 xpt_schedule(periph, priority);
5497 case PROBE_FULL_INQUIRY:
5499 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5500 struct scsi_inquiry_data *inq_buf;
5501 u_int8_t periph_qual;
5503 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5504 inq_buf = &path->device->inq_data;
5506 periph_qual = SID_QUAL(inq_buf);
5508 switch(periph_qual) {
5509 case SID_QUAL_LU_CONNECTED:
5514 * We conservatively request only
5515 * SHORT_INQUIRY_LEN bytes of inquiry
5516 * information during our first try
5517 * at sending an INQUIRY. If the device
5518 * has more information to give,
5519 * perform a second request specifying
5520 * the amount of information the device
5521 * is willing to give.
5523 alen = inq_buf->additional_length;
5524 if (softc->action == PROBE_INQUIRY
5525 && alen > (SHORT_INQUIRY_LENGTH - 5)) {
5526 softc->action = PROBE_FULL_INQUIRY;
5527 xpt_release_ccb(done_ccb);
5528 xpt_schedule(periph, priority);
5532 xpt_find_quirk(path->device);
5534 if ((inq_buf->flags & SID_CmdQue) != 0)
5535 softc->action = PROBE_MODE_SENSE;
5537 softc->action = PROBE_SERIAL_NUM;
5539 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5540 xpt_reference_device(path->device);
5542 xpt_release_ccb(done_ccb);
5543 xpt_schedule(periph, priority);
5549 } else if (cam_periph_error(done_ccb, 0,
5550 done_ccb->ccb_h.target_lun > 0
5551 ? SF_RETRY_UA|SF_QUIET_IR
5553 &softc->saved_ccb) == ERESTART) {
5555 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5556 /* Don't wedge the queue */
5557 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5561 * If we get to this point, we got an error status back
5562 * from the inquiry and the error status doesn't require
5563 * automatically retrying the command. Therefore, the
5564 * inquiry failed. If we had inquiry information before
5565 * for this device, but this latest inquiry command failed,
5566 * the device has probably gone away. If this device isn't
5567 * already marked unconfigured, notify the peripheral
5568 * drivers that this device is no more.
5570 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5571 /* Send the async notification. */
5572 xpt_async(AC_LOST_DEVICE, path, NULL);
5575 xpt_release_ccb(done_ccb);
5578 case PROBE_MODE_SENSE:
5580 struct ccb_scsiio *csio;
5581 struct scsi_mode_header_6 *mode_hdr;
5583 csio = &done_ccb->csio;
5584 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5585 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5586 struct scsi_control_page *page;
5589 offset = ((u_int8_t *)&mode_hdr[1])
5590 + mode_hdr->blk_desc_len;
5591 page = (struct scsi_control_page *)offset;
5592 path->device->queue_flags = page->queue_flags;
5593 } else if (cam_periph_error(done_ccb, 0,
5594 SF_RETRY_UA|SF_NO_PRINT,
5595 &softc->saved_ccb) == ERESTART) {
5597 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5598 /* Don't wedge the queue */
5599 xpt_release_devq(done_ccb->ccb_h.path,
5600 /*count*/1, /*run_queue*/TRUE);
5602 xpt_release_ccb(done_ccb);
5603 free(mode_hdr, M_TEMP);
5604 softc->action = PROBE_SERIAL_NUM;
5605 xpt_schedule(periph, priority);
5608 case PROBE_SERIAL_NUM:
5610 struct ccb_scsiio *csio;
5611 struct scsi_vpd_unit_serial_number *serial_buf;
5618 csio = &done_ccb->csio;
5619 priority = done_ccb->ccb_h.pinfo.priority;
5621 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5623 /* Clean up from previous instance of this device */
5624 if (path->device->serial_num != NULL) {
5625 free(path->device->serial_num, M_DEVBUF);
5626 path->device->serial_num = NULL;
5627 path->device->serial_num_len = 0;
5630 if (serial_buf == NULL) {
5632 * Don't process the command as it was never sent
5634 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5635 && (serial_buf->length > 0)) {
5638 path->device->serial_num =
5639 malloc((serial_buf->length + 1),
5640 M_DEVBUF, M_INTWAIT);
5641 bcopy(serial_buf->serial_num,
5642 path->device->serial_num,
5643 serial_buf->length);
5644 path->device->serial_num_len = serial_buf->length;
5645 path->device->serial_num[serial_buf->length] = '\0';
5646 } else if (cam_periph_error(done_ccb, 0,
5647 SF_RETRY_UA|SF_NO_PRINT,
5648 &softc->saved_ccb) == ERESTART) {
5650 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5651 /* Don't wedge the queue */
5652 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5657 * Let's see if we have seen this device before.
5659 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5661 u_int8_t digest[16];
5666 (unsigned char *)&path->device->inq_data,
5667 sizeof(struct scsi_inquiry_data));
5670 MD5Update(&context, serial_buf->serial_num,
5671 serial_buf->length);
5673 MD5Final(digest, &context);
5674 if (bcmp(softc->digest, digest, 16) == 0)
5678 * XXX Do we need to do a TUR in order to ensure
5679 * that the device really hasn't changed???
5682 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5683 xpt_async(AC_LOST_DEVICE, path, NULL);
5685 if (serial_buf != NULL)
5686 free(serial_buf, M_TEMP);
5690 * Now that we have all the necessary
5691 * information to safely perform transfer
5692 * negotiations... Controllers don't perform
5693 * any negotiation or tagged queuing until
5694 * after the first XPT_SET_TRAN_SETTINGS ccb is
5695 * received. So, on a new device, just retreive
5696 * the user settings, and set them as the current
5697 * settings to set the device up.
5699 proberequestdefaultnegotiation(periph);
5700 xpt_release_ccb(done_ccb);
5703 * Perform a TUR to allow the controller to
5704 * perform any necessary transfer negotiation.
5706 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5707 xpt_schedule(periph, priority);
5710 xpt_release_ccb(done_ccb);
5713 case PROBE_TUR_FOR_NEGOTIATION:
5714 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5715 /* Don't wedge the queue */
5716 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5720 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5721 xpt_reference_device(path->device);
5723 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5724 /* Inform the XPT that a new device has been found */
5725 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5726 xpt_action(done_ccb);
5728 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5730 xpt_release_ccb(done_ccb);
5733 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5734 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5735 done_ccb->ccb_h.status = CAM_REQ_CMP;
5737 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5738 cam_periph_invalidate(periph);
5739 cam_periph_release(periph);
5741 probeschedule(periph);
5746 probecleanup(struct cam_periph *periph)
5748 free(periph->softc, M_TEMP);
5752 xpt_find_quirk(struct cam_ed *device)
5756 match = cam_quirkmatch((caddr_t)&device->inq_data,
5757 (caddr_t)xpt_quirk_table,
5758 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5759 sizeof(*xpt_quirk_table), scsi_inquiry_match);
5762 panic("xpt_find_quirk: device didn't match wildcard entry!!");
5764 device->quirk = (struct xpt_quirk_entry *)match;
5768 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
5771 struct cam_sim *sim;
5774 sim = cts->ccb_h.path->bus->sim;
5775 if (async_update == FALSE) {
5776 struct scsi_inquiry_data *inq_data;
5777 struct ccb_pathinq cpi;
5778 struct ccb_trans_settings cur_cts;
5780 if (device == NULL) {
5781 cts->ccb_h.status = CAM_PATH_INVALID;
5782 xpt_done((union ccb *)cts);
5787 * Perform sanity checking against what the
5788 * controller and device can do.
5790 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
5791 cpi.ccb_h.func_code = XPT_PATH_INQ;
5792 xpt_action((union ccb *)&cpi);
5793 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
5794 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5795 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
5796 xpt_action((union ccb *)&cur_cts);
5797 inq_data = &device->inq_data;
5799 /* Fill in any gaps in what the user gave us */
5800 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
5801 cts->sync_period = cur_cts.sync_period;
5802 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
5803 cts->sync_offset = cur_cts.sync_offset;
5804 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
5805 cts->bus_width = cur_cts.bus_width;
5806 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
5807 cts->flags &= ~CCB_TRANS_DISC_ENB;
5808 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
5810 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
5811 cts->flags &= ~CCB_TRANS_TAG_ENB;
5812 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
5815 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5816 && (inq_data->flags & SID_Sync) == 0)
5817 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
5818 || (cts->sync_offset == 0)
5819 || (cts->sync_period == 0)) {
5821 cts->sync_period = 0;
5822 cts->sync_offset = 0;
5823 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
5825 if ((inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
5826 && cts->sync_period <= 0x9) {
5828 * Don't allow DT transmission rates if the
5829 * device does not support it.
5831 cts->sync_period = 0xa;
5833 if ((inq_data->spi3data & SID_SPI_IUS) == 0
5834 && cts->sync_period <= 0x8) {
5836 * Don't allow PACE transmission rates
5837 * if the device does support packetized
5840 cts->sync_period = 0x9;
5844 switch (cts->bus_width) {
5845 case MSG_EXT_WDTR_BUS_32_BIT:
5846 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5847 || (inq_data->flags & SID_WBus32) != 0)
5848 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
5850 /* Fall Through to 16-bit */
5851 case MSG_EXT_WDTR_BUS_16_BIT:
5852 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5853 || (inq_data->flags & SID_WBus16) != 0)
5854 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
5855 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5858 /* Fall Through to 8-bit */
5859 default: /* New bus width?? */
5860 case MSG_EXT_WDTR_BUS_8_BIT:
5861 /* All targets can do this */
5862 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5866 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
5868 * Can't tag queue without disconnection.
5870 cts->flags &= ~CCB_TRANS_TAG_ENB;
5871 cts->valid |= CCB_TRANS_TQ_VALID;
5874 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
5875 || (inq_data->flags & SID_CmdQue) == 0
5876 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
5877 || (device->quirk->mintags == 0)) {
5879 * Can't tag on hardware that doesn't support,
5880 * doesn't have it enabled, or has broken tag support.
5882 cts->flags &= ~CCB_TRANS_TAG_ENB;
5887 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
5891 * If we are transitioning from tags to no-tags or
5892 * vice-versa, we need to carefully freeze and restart
5893 * the queue so that we don't overlap tagged and non-tagged
5894 * commands. We also temporarily stop tags if there is
5895 * a change in transfer negotiation settings to allow
5896 * "tag-less" negotiation.
5898 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5899 || (device->inq_flags & SID_CmdQue) != 0)
5900 device_tagenb = TRUE;
5902 device_tagenb = FALSE;
5904 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
5905 && device_tagenb == FALSE)
5906 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
5907 && device_tagenb == TRUE)) {
5909 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
5911 * Delay change to use tags until after a
5912 * few commands have gone to this device so
5913 * the controller has time to perform transfer
5914 * negotiations without tagged messages getting
5917 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
5918 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
5920 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
5922 device->inq_flags &= ~SID_CmdQue;
5923 xpt_dev_ccbq_resize(cts->ccb_h.path,
5924 sim->max_dev_openings);
5925 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5926 device->tag_delay_count = 0;
5931 if (async_update == FALSE) {
5933 * If we are currently performing tagged transactions to
5934 * this device and want to change its negotiation parameters,
5935 * go non-tagged for a bit to give the controller a chance to
5936 * negotiate unhampered by tag messages.
5938 if ((device->inq_flags & SID_CmdQue) != 0
5939 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
5940 CCB_TRANS_SYNC_OFFSET_VALID|
5941 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
5942 xpt_toggle_tags(cts->ccb_h.path);
5944 (*(sim->sim_action))(sim, (union ccb *)cts);
5948 struct ccb_relsim crs;
5950 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
5952 crs.ccb_h.func_code = XPT_REL_SIMQ;
5953 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5955 = crs.release_timeout
5958 xpt_action((union ccb *)&crs);
5963 xpt_toggle_tags(struct cam_path *path)
5968 * Give controllers a chance to renegotiate
5969 * before starting tag operations. We
5970 * "toggle" tagged queuing off then on
5971 * which causes the tag enable command delay
5972 * counter to come into effect.
5975 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5976 || ((dev->inq_flags & SID_CmdQue) != 0
5977 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
5978 struct ccb_trans_settings cts;
5980 xpt_setup_ccb(&cts.ccb_h, path, 1);
5982 cts.valid = CCB_TRANS_TQ_VALID;
5983 xpt_set_transfer_settings(&cts, path->device,
5984 /*async_update*/TRUE);
5985 cts.flags = CCB_TRANS_TAG_ENB;
5986 xpt_set_transfer_settings(&cts, path->device,
5987 /*async_update*/TRUE);
5992 xpt_start_tags(struct cam_path *path)
5994 struct ccb_relsim crs;
5995 struct cam_ed *device;
5996 struct cam_sim *sim;
5999 device = path->device;
6000 sim = path->bus->sim;
6001 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6002 xpt_freeze_devq(path, /*count*/1);
6003 device->inq_flags |= SID_CmdQue;
6004 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
6005 xpt_dev_ccbq_resize(path, newopenings);
6006 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6007 crs.ccb_h.func_code = XPT_REL_SIMQ;
6008 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6010 = crs.release_timeout
6013 xpt_action((union ccb *)&crs);
6016 static int busses_to_config;
6017 static int busses_to_reset;
6020 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6022 if (bus->path_id != CAM_XPT_PATH_ID) {
6023 struct cam_path path;
6024 struct ccb_pathinq cpi;
6028 xpt_compile_path(&path, NULL, bus->path_id,
6029 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6030 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6031 cpi.ccb_h.func_code = XPT_PATH_INQ;
6032 xpt_action((union ccb *)&cpi);
6033 can_negotiate = cpi.hba_inquiry;
6034 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6035 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6038 xpt_release_path(&path);
6045 xptconfigfunc(struct cam_eb *bus, void *arg)
6047 struct cam_path *path;
6048 union ccb *work_ccb;
6050 if (bus->path_id != CAM_XPT_PATH_ID) {
6054 work_ccb = xpt_alloc_ccb();
6055 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6056 CAM_TARGET_WILDCARD,
6057 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6058 printf("xptconfigfunc: xpt_create_path failed with "
6059 "status %#x for bus %d\n", status, bus->path_id);
6060 printf("xptconfigfunc: halting bus configuration\n");
6061 xpt_free_ccb(work_ccb);
6063 xpt_finishconfig(xpt_periph, NULL);
6066 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6067 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6068 xpt_action(work_ccb);
6069 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6070 printf("xptconfigfunc: CPI failed on bus %d "
6071 "with status %d\n", bus->path_id,
6072 work_ccb->ccb_h.status);
6073 xpt_finishconfig(xpt_periph, work_ccb);
6077 can_negotiate = work_ccb->cpi.hba_inquiry;
6078 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6079 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6080 && (can_negotiate != 0)) {
6081 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6082 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6083 work_ccb->ccb_h.cbfcnp = NULL;
6084 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6085 ("Resetting Bus\n"));
6086 xpt_action(work_ccb);
6087 xpt_finishconfig(xpt_periph, work_ccb);
6089 /* Act as though we performed a successful BUS RESET */
6090 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6091 xpt_finishconfig(xpt_periph, work_ccb);
6099 xpt_config(void *arg)
6101 /* Now that interrupts are enabled, go find our devices */
6104 /* Setup debugging flags and path */
6105 #ifdef CAM_DEBUG_FLAGS
6106 cam_dflags = CAM_DEBUG_FLAGS;
6107 #else /* !CAM_DEBUG_FLAGS */
6108 cam_dflags = CAM_DEBUG_NONE;
6109 #endif /* CAM_DEBUG_FLAGS */
6110 #ifdef CAM_DEBUG_BUS
6111 if (cam_dflags != CAM_DEBUG_NONE) {
6112 if (xpt_create_path(&cam_dpath, xpt_periph,
6113 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6114 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6115 printf("xpt_config: xpt_create_path() failed for debug"
6116 " target %d:%d:%d, debugging disabled\n",
6117 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6118 cam_dflags = CAM_DEBUG_NONE;
6122 #else /* !CAM_DEBUG_BUS */
6124 #endif /* CAM_DEBUG_BUS */
6125 #endif /* CAMDEBUG */
6128 * Scan all installed busses.
6130 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6132 if (busses_to_config == 0) {
6133 /* Call manually because we don't have any busses */
6134 xpt_finishconfig(xpt_periph, NULL);
6136 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6137 printf("Waiting %d seconds for SCSI "
6138 "devices to settle\n", SCSI_DELAY/1000);
6140 xpt_for_all_busses(xptconfigfunc, NULL);
6145 * If the given device only has one peripheral attached to it, and if that
6146 * peripheral is the passthrough driver, announce it. This insures that the
6147 * user sees some sort of announcement for every peripheral in their system.
6150 xptpassannouncefunc(struct cam_ed *device, void *arg)
6152 struct cam_periph *periph;
6155 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6156 periph = SLIST_NEXT(periph, periph_links), i++);
6158 periph = SLIST_FIRST(&device->periphs);
6160 && (strncmp(periph->periph_name, "pass", 4) == 0))
6161 xpt_announce_periph(periph, NULL);
6167 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6169 struct periph_driver **p_drv;
6171 if (done_ccb != NULL) {
6172 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6173 ("xpt_finishconfig\n"));
6174 switch(done_ccb->ccb_h.func_code) {
6176 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6177 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6178 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6179 xpt_action(done_ccb);
6185 xpt_free_path(done_ccb->ccb_h.path);
6191 if (busses_to_config == 0) {
6192 /* Register all the peripheral drivers */
6193 /* XXX This will have to change when we have loadable modules */
6194 SET_FOREACH(p_drv, periphdriver_set) {
6199 * Check for devices with no "standard" peripheral driver
6200 * attached. For any devices like that, announce the
6201 * passthrough driver so the user will see something.
6203 xpt_for_all_devices(xptpassannouncefunc, NULL);
6205 /* Release our hook so that the boot can continue. */
6206 config_intrhook_disestablish(xpt_config_hook);
6207 free(xpt_config_hook, M_TEMP);
6208 xpt_config_hook = NULL;
6210 if (done_ccb != NULL)
6211 xpt_free_ccb(done_ccb);
6215 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6217 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6219 switch (work_ccb->ccb_h.func_code) {
6220 /* Common cases first */
6221 case XPT_PATH_INQ: /* Path routing inquiry */
6223 struct ccb_pathinq *cpi;
6225 cpi = &work_ccb->cpi;
6226 cpi->version_num = 1; /* XXX??? */
6227 cpi->hba_inquiry = 0;
6228 cpi->target_sprt = 0;
6230 cpi->hba_eng_cnt = 0;
6231 cpi->max_target = 0;
6233 cpi->initiator_id = 0;
6234 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6235 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6236 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6237 cpi->unit_number = sim->unit_number;
6238 cpi->bus_id = sim->bus_id;
6239 cpi->base_transfer_speed = 0;
6240 cpi->ccb_h.status = CAM_REQ_CMP;
6245 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6252 * The xpt as a "controller" has no interrupt sources, so polling
6256 xptpoll(struct cam_sim *sim)
6261 * Should only be called by the machine interrupt dispatch routines,
6262 * so put these prototypes here instead of in the header.
6266 swi_camnet(void *arg)
6272 swi_cambio(void *arg)
6278 camisr(cam_isrq_t *queue)
6281 struct ccb_hdr *ccb_h;
6284 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6287 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6288 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6291 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6296 if (ccb_h->flags & CAM_HIGH_POWER) {
6297 struct highpowerlist *hphead;
6298 struct cam_ed *device;
6299 union ccb *send_ccb;
6301 hphead = &highpowerq;
6303 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6306 * Increment the count since this command is done.
6311 * Any high powered commands queued up?
6313 if (send_ccb != NULL) {
6314 device = send_ccb->ccb_h.path->device;
6316 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6318 xpt_release_devq(send_ccb->ccb_h.path,
6319 /*count*/1, /*runqueue*/TRUE);
6322 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6325 dev = ccb_h->path->device;
6328 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6330 ccb_h->path->bus->sim->devq->send_active--;
6331 ccb_h->path->bus->sim->devq->send_openings++;
6334 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6335 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6336 && (dev->ccbq.dev_active == 0))) {
6338 xpt_release_devq(ccb_h->path, /*count*/1,
6342 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6343 && (--dev->tag_delay_count == 0))
6344 xpt_start_tags(ccb_h->path);
6346 if ((dev->ccbq.queue.entries > 0)
6347 && (dev->qfrozen_cnt == 0)
6348 && (device_is_send_queued(dev) == 0)) {
6349 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6354 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6355 xpt_release_simq(ccb_h->path->bus->sim,
6357 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6361 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6362 && (ccb_h->status & CAM_DEV_QFRZN)) {
6363 xpt_release_devq(ccb_h->path, /*count*/1,
6365 ccb_h->status &= ~CAM_DEV_QFRZN;
6367 xpt_run_dev_sendq(ccb_h->path->bus);
6370 /* Call the peripheral driver's callback */
6371 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6373 /* Raise IPL for while test */