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.23 2005/03/15 20:42:12 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>
47 #include <machine/clock.h>
48 #include <machine/ipl.h>
52 #include "cam_periph.h"
55 #include "cam_xpt_sim.h"
56 #include "cam_xpt_periph.h"
57 #include "cam_debug.h"
59 #include "scsi/scsi_all.h"
60 #include "scsi/scsi_message.h"
61 #include "scsi/scsi_pass.h"
64 /* Datastructures internal to the xpt layer */
67 * Definition of an async handler callback block. These are used to add
68 * SIMs and peripherals to the async callback lists.
71 SLIST_ENTRY(async_node) links;
72 u_int32_t event_enable; /* Async Event enables */
73 void (*callback)(void *arg, u_int32_t code,
74 struct cam_path *path, void *args);
78 SLIST_HEAD(async_list, async_node);
79 SLIST_HEAD(periph_list, cam_periph);
80 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
83 * This is the maximum number of high powered commands (e.g. start unit)
84 * that can be outstanding at a particular time.
86 #ifndef CAM_MAX_HIGHPOWER
87 #define CAM_MAX_HIGHPOWER 4
90 /* number of high powered commands that can go through right now */
91 static int num_highpower = CAM_MAX_HIGHPOWER;
94 * Structure for queueing a device in a run queue.
95 * There is one run queue for allocating new ccbs,
96 * and another for sending ccbs to the controller.
100 struct cam_ed *device;
104 * The CAM EDT (Existing Device Table) contains the device information for
105 * all devices for all busses in the system. The table contains a
106 * cam_ed structure for each device on the bus.
109 TAILQ_ENTRY(cam_ed) links;
110 struct cam_ed_qinfo alloc_ccb_entry;
111 struct cam_ed_qinfo send_ccb_entry;
112 struct cam_et *target;
115 * Queue of type drivers wanting to do
116 * work on this device.
118 struct cam_ccbq ccbq; /* Queue of pending ccbs */
119 struct async_list asyncs; /* Async callback info for this B/T/L */
120 struct periph_list periphs; /* All attached devices */
121 u_int generation; /* Generation number */
122 struct cam_periph *owner; /* Peripheral driver's ownership tag */
123 struct xpt_quirk_entry *quirk; /* Oddities about this device */
124 /* Storage for the inquiry data */
125 struct scsi_inquiry_data inq_data;
126 u_int8_t inq_flags; /*
127 * Current settings for inquiry flags.
128 * This allows us to override settings
129 * like disconnection and tagged
130 * queuing for a device.
132 u_int8_t queue_flags; /* Queue flags from the control page */
133 u_int8_t serial_num_len;
134 u_int8_t *serial_num;
135 u_int32_t qfrozen_cnt;
137 #define CAM_DEV_UNCONFIGURED 0x01
138 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
139 #define CAM_DEV_REL_ON_COMPLETE 0x04
140 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
141 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
142 #define CAM_DEV_TAG_AFTER_COUNT 0x20
143 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
144 u_int32_t tag_delay_count;
145 #define CAM_TAG_DELAY_COUNT 5
147 struct callout c_handle;
151 * Each target is represented by an ET (Existing Target). These
152 * entries are created when a target is successfully probed with an
153 * identify, and removed when a device fails to respond after a number
154 * of retries, or a bus rescan finds the device missing.
157 TAILQ_HEAD(, cam_ed) ed_entries;
158 TAILQ_ENTRY(cam_et) links;
160 target_id_t target_id;
163 struct timeval last_reset; /* uptime of last reset */
167 * Each bus is represented by an EB (Existing Bus). These entries
168 * are created by calls to xpt_bus_register and deleted by calls to
169 * xpt_bus_deregister.
172 TAILQ_HEAD(, cam_et) et_entries;
173 TAILQ_ENTRY(cam_eb) links;
176 struct timeval last_reset; /* uptime of last reset */
178 #define CAM_EB_RUNQ_SCHEDULED 0x01
184 struct cam_periph *periph;
186 struct cam_et *target;
187 struct cam_ed *device;
190 struct xpt_quirk_entry {
191 struct scsi_inquiry_pattern inq_pat;
193 #define CAM_QUIRK_NOLUNS 0x01
194 #define CAM_QUIRK_NOSERIAL 0x02
195 #define CAM_QUIRK_HILUNS 0x04
199 #define CAM_SCSI2_MAXLUN 8
207 u_int32_t generation;
210 static const char quantum[] = "QUANTUM";
211 static const char sony[] = "SONY";
212 static const char west_digital[] = "WDIGTL";
213 static const char samsung[] = "SAMSUNG";
214 static const char seagate[] = "SEAGATE";
215 static const char microp[] = "MICROP";
217 static struct xpt_quirk_entry xpt_quirk_table[] =
220 /* Reports QUEUE FULL for temporary resource shortages */
221 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
222 /*quirks*/0, /*mintags*/24, /*maxtags*/32
225 /* Reports QUEUE FULL for temporary resource shortages */
226 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
227 /*quirks*/0, /*mintags*/24, /*maxtags*/32
230 /* Reports QUEUE FULL for temporary resource shortages */
231 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
232 /*quirks*/0, /*mintags*/24, /*maxtags*/32
235 /* Broken tagged queuing drive */
236 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
237 /*quirks*/0, /*mintags*/0, /*maxtags*/0
240 /* Broken tagged queuing drive */
241 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
242 /*quirks*/0, /*mintags*/0, /*maxtags*/0
245 /* Broken tagged queuing drive */
246 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
247 /*quirks*/0, /*mintags*/0, /*maxtags*/0
251 * Unfortunately, the Quantum Atlas III has the same
252 * problem as the Atlas II drives above.
253 * Reported by: "Johan Granlund" <johan@granlund.nu>
255 * For future reference, the drive with the problem was:
256 * QUANTUM QM39100TD-SW N1B0
258 * It's possible that Quantum will fix the problem in later
259 * firmware revisions. If that happens, the quirk entry
260 * will need to be made specific to the firmware revisions
264 /* Reports QUEUE FULL for temporary resource shortages */
265 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
266 /*quirks*/0, /*mintags*/24, /*maxtags*/32
270 * 18 Gig Atlas III, same problem as the 9G version.
271 * Reported by: Andre Albsmeier
272 * <andre.albsmeier@mchp.siemens.de>
274 * For future reference, the drive with the problem was:
275 * QUANTUM QM318000TD-S N491
277 /* Reports QUEUE FULL for temporary resource shortages */
278 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
279 /*quirks*/0, /*mintags*/24, /*maxtags*/32
283 * Broken tagged queuing drive
284 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
285 * and: Martin Renters <martin@tdc.on.ca>
287 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
288 /*quirks*/0, /*mintags*/0, /*maxtags*/0
291 * The Seagate Medalist Pro drives have very poor write
292 * performance with anything more than 2 tags.
294 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
295 * Drive: <SEAGATE ST36530N 1444>
297 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
298 * Drive: <SEAGATE ST34520W 1281>
300 * No one has actually reported that the 9G version
301 * (ST39140*) of the Medalist Pro has the same problem, but
302 * we're assuming that it does because the 4G and 6.5G
303 * versions of the drive are broken.
306 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
307 /*quirks*/0, /*mintags*/2, /*maxtags*/2
310 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
311 /*quirks*/0, /*mintags*/2, /*maxtags*/2
314 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
315 /*quirks*/0, /*mintags*/2, /*maxtags*/2
319 * Slow when tagged queueing is enabled. Write performance
320 * steadily drops off with more and more concurrent
321 * transactions. Best sequential write performance with
322 * tagged queueing turned off and write caching turned on.
325 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
326 * Drive: DCAS-34330 w/ "S65A" firmware.
328 * The drive with the problem had the "S65A" firmware
329 * revision, and has also been reported (by Stephen J.
330 * Roznowski <sjr@home.net>) for a drive with the "S61A"
333 * Although no one has reported problems with the 2 gig
334 * version of the DCAS drive, the assumption is that it
335 * has the same problems as the 4 gig version. Therefore
336 * this quirk entries disables tagged queueing for all
339 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
340 /*quirks*/0, /*mintags*/0, /*maxtags*/0
343 /* Broken tagged queuing drive */
344 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
345 /*quirks*/0, /*mintags*/0, /*maxtags*/0
348 /* Broken tagged queuing drive */
349 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
350 /*quirks*/0, /*mintags*/0, /*maxtags*/0
354 * Broken tagged queuing drive.
356 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
359 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
360 /*quirks*/0, /*mintags*/0, /*maxtags*/0
364 * Slow when tagged queueing is enabled. (1.5MB/sec versus
366 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
367 * Best performance with these drives is achieved with
368 * tagged queueing turned off, and write caching turned on.
370 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
371 /*quirks*/0, /*mintags*/0, /*maxtags*/0
375 * Slow when tagged queueing is enabled. (1.5MB/sec versus
377 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
378 * Best performance with these drives is achieved with
379 * tagged queueing turned off, and write caching turned on.
381 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
382 /*quirks*/0, /*mintags*/0, /*maxtags*/0
386 * Doesn't handle queue full condition correctly,
387 * so we need to limit maxtags to what the device
388 * can handle instead of determining this automatically.
390 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
391 /*quirks*/0, /*mintags*/2, /*maxtags*/32
394 /* Really only one LUN */
395 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
396 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
399 /* I can't believe we need a quirk for DPT volumes. */
400 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
401 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
402 /*mintags*/0, /*maxtags*/255
406 * Many Sony CDROM drives don't like multi-LUN probing.
408 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
409 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
413 * This drive doesn't like multiple LUN probing.
414 * Submitted by: Parag Patel <parag@cgt.com>
416 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
417 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
420 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
421 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
425 * The 8200 doesn't like multi-lun probing, and probably
426 * don't like serial number requests either.
429 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
432 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
436 * Let's try the same as above, but for a drive that says
437 * it's an IPL-6860 but is actually an EXB 8200.
440 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
443 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
447 * These Hitachi drives don't like multi-lun probing.
448 * The PR submitter has a DK319H, but says that the Linux
449 * kernel has a similar work-around for the DK312 and DK314,
450 * so all DK31* drives are quirked here.
452 * Submitted by: Paul Haddad <paul@pth.com>
454 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
455 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
459 * This old revision of the TDC3600 is also SCSI-1, and
460 * hangs upon serial number probing.
463 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
466 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
470 * Would repond to all LUNs if asked for.
473 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
476 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
480 * Would repond to all LUNs if asked for.
483 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
486 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
489 /* Submitted by: Matthew Dodd <winter@jurai.net> */
490 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
491 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
494 /* Submitted by: Matthew Dodd <winter@jurai.net> */
495 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
496 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
499 /* TeraSolutions special settings for TRC-22 RAID */
500 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
501 /*quirks*/0, /*mintags*/55, /*maxtags*/255
504 /* Veritas Storage Appliance */
505 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
506 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
510 * Would respond to all LUNs. Device type and removable
511 * flag are jumper-selectable.
513 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
516 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
519 /* Default tagged queuing parameters for all devices */
521 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
522 /*vendor*/"*", /*product*/"*", /*revision*/"*"
524 /*quirks*/0, /*mintags*/2, /*maxtags*/255
528 static const int xpt_quirk_table_size =
529 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
533 DM_RET_FLAG_MASK = 0x0f,
536 DM_RET_DESCEND = 0x20,
538 DM_RET_ACTION_MASK = 0xf0
546 } xpt_traverse_depth;
548 struct xpt_traverse_config {
549 xpt_traverse_depth depth;
554 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
555 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
556 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
557 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
558 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
560 /* Transport layer configuration information */
561 static struct xpt_softc xsoftc;
563 /* Queues for our software interrupt handler */
564 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
565 static cam_isrq_t cam_bioq;
566 static cam_isrq_t cam_netq;
568 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
569 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
570 static u_int xpt_max_ccbs; /*
571 * Maximum size of ccb pool. Modified as
572 * devices are added/removed or have their
573 * opening counts changed.
575 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
577 struct cam_periph *xpt_periph;
579 static periph_init_t xpt_periph_init;
581 static periph_init_t probe_periph_init;
583 static struct periph_driver xpt_driver =
585 xpt_periph_init, "xpt",
586 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
589 static struct periph_driver probe_driver =
591 probe_periph_init, "probe",
592 TAILQ_HEAD_INITIALIZER(probe_driver.units)
595 DATA_SET(periphdriver_set, xpt_driver);
596 DATA_SET(periphdriver_set, probe_driver);
598 #define XPT_CDEV_MAJOR 104
600 static d_open_t xptopen;
601 static d_close_t xptclose;
602 static d_ioctl_t xptioctl;
604 static struct cdevsw xpt_cdevsw = {
606 /* maj */ XPT_CDEV_MAJOR,
612 /* close */ xptclose,
615 /* ioctl */ xptioctl,
618 /* strategy */ nostrategy,
623 static struct intr_config_hook *xpt_config_hook;
625 /* Registered busses */
626 static TAILQ_HEAD(,cam_eb) xpt_busses;
627 static u_int bus_generation;
629 /* Storage for debugging datastructures */
631 struct cam_path *cam_dpath;
632 u_int32_t cam_dflags;
633 u_int32_t cam_debug_delay;
636 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
637 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
641 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
642 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
643 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
645 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
646 || defined(CAM_DEBUG_LUN)
648 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
649 || !defined(CAM_DEBUG_LUN)
650 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
652 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
653 #else /* !CAMDEBUG */
654 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
655 #endif /* CAMDEBUG */
656 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
658 /* Our boot-time initialization hook */
659 static void xpt_init(void *);
660 SYSINIT(cam, SI_SUB_CONFIGURE, SI_ORDER_SECOND, xpt_init, NULL);
662 static cam_status xpt_compile_path(struct cam_path *new_path,
663 struct cam_periph *perph,
665 target_id_t target_id,
668 static void xpt_release_path(struct cam_path *path);
670 static void xpt_async_bcast(struct async_list *async_head,
671 u_int32_t async_code,
672 struct cam_path *path,
674 static void xpt_dev_async(u_int32_t async_code,
676 struct cam_et *target,
677 struct cam_ed *device,
679 static path_id_t xptnextfreepathid(void);
680 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
681 static union ccb *xpt_get_ccb(struct cam_ed *device);
682 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
683 u_int32_t new_priority);
684 static void xpt_run_dev_allocq(struct cam_eb *bus);
685 static void xpt_run_dev_sendq(struct cam_eb *bus);
686 static timeout_t xpt_release_devq_timeout;
687 static void xpt_release_bus(struct cam_eb *bus);
688 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
690 static struct cam_et*
691 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
692 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
693 static struct cam_ed*
694 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
696 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
697 struct cam_ed *device);
698 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
699 static struct cam_eb*
700 xpt_find_bus(path_id_t path_id);
701 static struct cam_et*
702 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
703 static struct cam_ed*
704 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
705 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
706 static void xpt_scan_lun(struct cam_periph *periph,
707 struct cam_path *path, cam_flags flags,
709 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
710 static xpt_busfunc_t xptconfigbuscountfunc;
711 static xpt_busfunc_t xptconfigfunc;
712 static void xpt_config(void *arg);
713 static xpt_devicefunc_t xptpassannouncefunc;
714 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
715 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
716 static void xptpoll(struct cam_sim *sim);
717 static inthand2_t swi_camnet;
718 static inthand2_t swi_cambio;
719 static void camisr(cam_isrq_t *queue);
721 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
722 static void xptasync(struct cam_periph *periph,
723 u_int32_t code, cam_path *path);
725 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
726 int num_patterns, struct cam_eb *bus);
727 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
728 int num_patterns, struct cam_ed *device);
729 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
731 struct cam_periph *periph);
732 static xpt_busfunc_t xptedtbusfunc;
733 static xpt_targetfunc_t xptedttargetfunc;
734 static xpt_devicefunc_t xptedtdevicefunc;
735 static xpt_periphfunc_t xptedtperiphfunc;
736 static xpt_pdrvfunc_t xptplistpdrvfunc;
737 static xpt_periphfunc_t xptplistperiphfunc;
738 static int xptedtmatch(struct ccb_dev_match *cdm);
739 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
740 static int xptbustraverse(struct cam_eb *start_bus,
741 xpt_busfunc_t *tr_func, void *arg);
742 static int xpttargettraverse(struct cam_eb *bus,
743 struct cam_et *start_target,
744 xpt_targetfunc_t *tr_func, void *arg);
745 static int xptdevicetraverse(struct cam_et *target,
746 struct cam_ed *start_device,
747 xpt_devicefunc_t *tr_func, void *arg);
748 static int xptperiphtraverse(struct cam_ed *device,
749 struct cam_periph *start_periph,
750 xpt_periphfunc_t *tr_func, void *arg);
751 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
752 xpt_pdrvfunc_t *tr_func, void *arg);
753 static int xptpdperiphtraverse(struct periph_driver **pdrv,
754 struct cam_periph *start_periph,
755 xpt_periphfunc_t *tr_func,
757 static xpt_busfunc_t xptdefbusfunc;
758 static xpt_targetfunc_t xptdeftargetfunc;
759 static xpt_devicefunc_t xptdefdevicefunc;
760 static xpt_periphfunc_t xptdefperiphfunc;
761 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
763 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
766 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
769 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
772 static xpt_devicefunc_t xptsetasyncfunc;
773 static xpt_busfunc_t xptsetasyncbusfunc;
774 static cam_status xptregister(struct cam_periph *periph,
776 static cam_status proberegister(struct cam_periph *periph,
778 static void probeschedule(struct cam_periph *probe_periph);
779 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
780 static void proberequestdefaultnegotiation(struct cam_periph *periph);
781 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
782 static void probecleanup(struct cam_periph *periph);
783 static void xpt_find_quirk(struct cam_ed *device);
784 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
785 struct cam_ed *device,
787 static void xpt_toggle_tags(struct cam_path *path);
788 static void xpt_start_tags(struct cam_path *path);
789 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
791 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
793 static __inline int periph_is_queued(struct cam_periph *periph);
794 static __inline int device_is_alloc_queued(struct cam_ed *device);
795 static __inline int device_is_send_queued(struct cam_ed *device);
796 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
799 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
803 if (dev->ccbq.devq_openings > 0) {
804 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
805 cam_ccbq_resize(&dev->ccbq,
806 dev->ccbq.dev_openings
807 + dev->ccbq.dev_active);
808 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
811 * The priority of a device waiting for CCB resources
812 * is that of the the highest priority peripheral driver
815 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
816 &dev->alloc_ccb_entry.pinfo,
817 CAMQ_GET_HEAD(&dev->drvq)->priority);
826 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
830 if (dev->ccbq.dev_openings > 0) {
832 * The priority of a device waiting for controller
833 * resources is that of the the highest priority CCB
837 xpt_schedule_dev(&bus->sim->devq->send_queue,
838 &dev->send_ccb_entry.pinfo,
839 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
847 periph_is_queued(struct cam_periph *periph)
849 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
853 device_is_alloc_queued(struct cam_ed *device)
855 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
859 device_is_send_queued(struct cam_ed *device)
861 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
865 dev_allocq_is_runnable(struct cam_devq *devq)
869 * Have space to do more work.
870 * Allowed to do work.
872 return ((devq->alloc_queue.qfrozen_cnt == 0)
873 && (devq->alloc_queue.entries > 0)
874 && (devq->alloc_openings > 0));
880 cdevsw_add(&xpt_cdevsw, 0, 0);
881 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
891 xptdone(struct cam_periph *periph, union ccb *done_ccb)
893 /* Caller will release the CCB */
894 wakeup(&done_ccb->ccb_h.cbfcnp);
898 xptopen(dev_t dev, int flags, int fmt, struct thread *td)
902 unit = minor(dev) & 0xff;
905 * Only allow read-write access.
907 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
911 * We don't allow nonblocking access.
913 if ((flags & O_NONBLOCK) != 0) {
914 printf("xpt%d: can't do nonblocking access\n", unit);
919 * We only have one transport layer right now. If someone accesses
920 * us via something other than minor number 1, point out their
924 printf("xptopen: got invalid xpt unit %d\n", unit);
928 /* Mark ourselves open */
929 xsoftc.flags |= XPT_FLAG_OPEN;
935 xptclose(dev_t dev, int flag, int fmt, struct thread *td)
939 unit = minor(dev) & 0xff;
942 * We only have one transport layer right now. If someone accesses
943 * us via something other than minor number 1, point out their
947 printf("xptclose: got invalid xpt unit %d\n", unit);
951 /* Mark ourselves closed */
952 xsoftc.flags &= ~XPT_FLAG_OPEN;
958 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
963 unit = minor(dev) & 0xff;
966 * We only have one transport layer right now. If someone accesses
967 * us via something other than minor number 1, point out their
971 printf("xptioctl: got invalid xpt unit %d\n", unit);
977 * For the transport layer CAMIOCOMMAND ioctl, we really only want
978 * to accept CCB types that don't quite make sense to send through a
979 * passthrough driver.
985 inccb = (union ccb *)addr;
987 switch(inccb->ccb_h.func_code) {
990 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
991 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1000 ccb = xpt_alloc_ccb();
1003 * Create a path using the bus, target, and lun the
1006 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1007 inccb->ccb_h.path_id,
1008 inccb->ccb_h.target_id,
1009 inccb->ccb_h.target_lun) !=
1015 /* Ensure all of our fields are correct */
1016 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1017 inccb->ccb_h.pinfo.priority);
1018 xpt_merge_ccb(ccb, inccb);
1019 ccb->ccb_h.cbfcnp = xptdone;
1020 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1021 bcopy(ccb, inccb, sizeof(union ccb));
1022 xpt_free_path(ccb->ccb_h.path);
1030 * This is an immediate CCB, so it's okay to
1031 * allocate it on the stack.
1035 * Create a path using the bus, target, and lun the
1038 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1039 inccb->ccb_h.path_id,
1040 inccb->ccb_h.target_id,
1041 inccb->ccb_h.target_lun) !=
1046 /* Ensure all of our fields are correct */
1047 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1048 inccb->ccb_h.pinfo.priority);
1049 xpt_merge_ccb(&ccb, inccb);
1050 ccb.ccb_h.cbfcnp = xptdone;
1052 bcopy(&ccb, inccb, sizeof(union ccb));
1053 xpt_free_path(ccb.ccb_h.path);
1057 case XPT_DEV_MATCH: {
1058 struct cam_periph_map_info mapinfo;
1059 struct cam_path *old_path;
1062 * We can't deal with physical addresses for this
1063 * type of transaction.
1065 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1071 * Save this in case the caller had it set to
1072 * something in particular.
1074 old_path = inccb->ccb_h.path;
1077 * We really don't need a path for the matching
1078 * code. The path is needed because of the
1079 * debugging statements in xpt_action(). They
1080 * assume that the CCB has a valid path.
1082 inccb->ccb_h.path = xpt_periph->path;
1084 bzero(&mapinfo, sizeof(mapinfo));
1087 * Map the pattern and match buffers into kernel
1088 * virtual address space.
1090 error = cam_periph_mapmem(inccb, &mapinfo);
1093 inccb->ccb_h.path = old_path;
1098 * This is an immediate CCB, we can send it on directly.
1103 * Map the buffers back into user space.
1105 cam_periph_unmapmem(inccb, &mapinfo);
1107 inccb->ccb_h.path = old_path;
1119 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1120 * with the periphal driver name and unit name filled in. The other
1121 * fields don't really matter as input. The passthrough driver name
1122 * ("pass"), and unit number are passed back in the ccb. The current
1123 * device generation number, and the index into the device peripheral
1124 * driver list, and the status are also passed back. Note that
1125 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1126 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1127 * (or rather should be) impossible for the device peripheral driver
1128 * list to change since we look at the whole thing in one pass, and
1129 * we do it with splcam protection.
1132 case CAMGETPASSTHRU: {
1134 struct cam_periph *periph;
1135 struct periph_driver **p_drv;
1139 int base_periph_found;
1143 ccb = (union ccb *)addr;
1144 unit = ccb->cgdl.unit_number;
1145 name = ccb->cgdl.periph_name;
1147 * Every 100 devices, we want to drop our spl protection to
1148 * give the software interrupt handler a chance to run.
1149 * Most systems won't run into this check, but this should
1150 * avoid starvation in the software interrupt handler in
1155 ccb = (union ccb *)addr;
1157 base_periph_found = 0;
1160 * Sanity check -- make sure we don't get a null peripheral
1163 if (*ccb->cgdl.periph_name == '\0') {
1168 /* Keep the list from changing while we traverse it */
1171 cur_generation = xsoftc.generation;
1173 /* first find our driver in the list of drivers */
1174 SET_FOREACH(p_drv, periphdriver_set) {
1175 if (strcmp((*p_drv)->driver_name, name) == 0)
1179 if (*p_drv == NULL) {
1181 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1182 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1183 *ccb->cgdl.periph_name = '\0';
1184 ccb->cgdl.unit_number = 0;
1190 * Run through every peripheral instance of this driver
1191 * and check to see whether it matches the unit passed
1192 * in by the user. If it does, get out of the loops and
1193 * find the passthrough driver associated with that
1194 * peripheral driver.
1196 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1197 periph = TAILQ_NEXT(periph, unit_links)) {
1199 if (periph->unit_number == unit) {
1201 } else if (--splbreaknum == 0) {
1205 if (cur_generation != xsoftc.generation)
1210 * If we found the peripheral driver that the user passed
1211 * in, go through all of the peripheral drivers for that
1212 * particular device and look for a passthrough driver.
1214 if (periph != NULL) {
1215 struct cam_ed *device;
1218 base_periph_found = 1;
1219 device = periph->path->device;
1220 for (i = 0, periph = device->periphs.slh_first;
1222 periph = periph->periph_links.sle_next, i++) {
1224 * Check to see whether we have a
1225 * passthrough device or not.
1227 if (strcmp(periph->periph_name, "pass") == 0) {
1229 * Fill in the getdevlist fields.
1231 strcpy(ccb->cgdl.periph_name,
1232 periph->periph_name);
1233 ccb->cgdl.unit_number =
1234 periph->unit_number;
1235 if (periph->periph_links.sle_next)
1237 CAM_GDEVLIST_MORE_DEVS;
1240 CAM_GDEVLIST_LAST_DEVICE;
1241 ccb->cgdl.generation =
1243 ccb->cgdl.index = i;
1245 * Fill in some CCB header fields
1246 * that the user may want.
1248 ccb->ccb_h.path_id =
1249 periph->path->bus->path_id;
1250 ccb->ccb_h.target_id =
1251 periph->path->target->target_id;
1252 ccb->ccb_h.target_lun =
1253 periph->path->device->lun_id;
1254 ccb->ccb_h.status = CAM_REQ_CMP;
1261 * If the periph is null here, one of two things has
1262 * happened. The first possibility is that we couldn't
1263 * find the unit number of the particular peripheral driver
1264 * that the user is asking about. e.g. the user asks for
1265 * the passthrough driver for "da11". We find the list of
1266 * "da" peripherals all right, but there is no unit 11.
1267 * The other possibility is that we went through the list
1268 * of peripheral drivers attached to the device structure,
1269 * but didn't find one with the name "pass". Either way,
1270 * we return ENOENT, since we couldn't find something.
1272 if (periph == NULL) {
1273 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1274 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1275 *ccb->cgdl.periph_name = '\0';
1276 ccb->cgdl.unit_number = 0;
1279 * It is unfortunate that this is even necessary,
1280 * but there are many, many clueless users out there.
1281 * If this is true, the user is looking for the
1282 * passthrough driver, but doesn't have one in his
1285 if (base_periph_found == 1) {
1286 printf("xptioctl: pass driver is not in the "
1288 printf("xptioctl: put \"device pass0\" in "
1289 "your kernel config file\n");
1303 /* Functions accessed by the peripheral drivers */
1308 struct cam_sim *xpt_sim;
1309 struct cam_path *path;
1310 struct cam_devq *devq;
1313 TAILQ_INIT(&xpt_busses);
1314 TAILQ_INIT(&cam_bioq);
1315 TAILQ_INIT(&cam_netq);
1316 SLIST_INIT(&ccb_freeq);
1317 STAILQ_INIT(&highpowerq);
1320 * The xpt layer is, itself, the equivelent of a SIM.
1321 * Allow 16 ccbs in the ccb pool for it. This should
1322 * give decent parallelism when we probe busses and
1323 * perform other XPT functions.
1325 devq = cam_simq_alloc(16);
1326 xpt_sim = cam_sim_alloc(xptaction,
1331 /*max_dev_transactions*/0,
1332 /*max_tagged_dev_transactions*/0,
1334 cam_simq_release(devq);
1337 xpt_bus_register(xpt_sim, /*bus #*/0);
1340 * Looking at the XPT from the SIM layer, the XPT is
1341 * the equivelent of a peripheral driver. Allocate
1342 * a peripheral driver entry for us.
1344 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1345 CAM_TARGET_WILDCARD,
1346 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1347 printf("xpt_init: xpt_create_path failed with status %#x,"
1348 " failing attach\n", status);
1352 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1353 path, NULL, 0, NULL);
1354 xpt_free_path(path);
1356 xpt_sim->softc = xpt_periph;
1359 * Register a callback for when interrupts are enabled.
1361 xpt_config_hook = malloc(sizeof(struct intr_config_hook),
1362 M_TEMP, M_INTWAIT | M_ZERO);
1363 xpt_config_hook->ich_func = xpt_config;
1364 xpt_config_hook->ich_desc = "xpt";
1365 if (config_intrhook_establish(xpt_config_hook) != 0) {
1366 free (xpt_config_hook, M_TEMP);
1367 printf("xpt_init: config_intrhook_establish failed "
1368 "- failing attach\n");
1371 /* Install our software interrupt handlers */
1372 register_swi(SWI_CAMNET, swi_camnet, NULL, "swi_camnet", NULL);
1373 register_swi(SWI_CAMBIO, swi_cambio, NULL, "swi_cambio", NULL);
1377 xptregister(struct cam_periph *periph, void *arg)
1379 if (periph == NULL) {
1380 printf("xptregister: periph was NULL!!\n");
1381 return(CAM_REQ_CMP_ERR);
1384 periph->softc = NULL;
1386 xpt_periph = periph;
1388 return(CAM_REQ_CMP);
1392 xpt_add_periph(struct cam_periph *periph)
1394 struct cam_ed *device;
1396 struct periph_list *periph_head;
1398 device = periph->path->device;
1400 periph_head = &device->periphs;
1402 status = CAM_REQ_CMP;
1404 if (device != NULL) {
1408 * Make room for this peripheral
1409 * so it will fit in the queue
1410 * when it's scheduled to run
1413 status = camq_resize(&device->drvq,
1414 device->drvq.array_size + 1);
1416 device->generation++;
1418 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1423 xsoftc.generation++;
1429 xpt_remove_periph(struct cam_periph *periph)
1431 struct cam_ed *device;
1433 device = periph->path->device;
1435 if (device != NULL) {
1437 struct periph_list *periph_head;
1439 periph_head = &device->periphs;
1441 /* Release the slot for this peripheral */
1443 camq_resize(&device->drvq, device->drvq.array_size - 1);
1445 device->generation++;
1447 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1452 xsoftc.generation++;
1457 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1461 struct cam_path *path;
1462 struct ccb_trans_settings cts;
1464 path = periph->path;
1466 * To ensure that this is printed in one piece,
1467 * mask out CAM interrupts.
1470 printf("%s%d at %s%d bus %d target %d lun %d\n",
1471 periph->periph_name, periph->unit_number,
1472 path->bus->sim->sim_name,
1473 path->bus->sim->unit_number,
1474 path->bus->sim->bus_id,
1475 path->target->target_id,
1476 path->device->lun_id);
1477 printf("%s%d: ", periph->periph_name, periph->unit_number);
1478 scsi_print_inquiry(&path->device->inq_data);
1480 && (path->device->serial_num_len > 0)) {
1481 /* Don't wrap the screen - print only the first 60 chars */
1482 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1483 periph->unit_number, path->device->serial_num);
1485 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1486 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1487 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1488 xpt_action((union ccb*)&cts);
1489 if (cts.ccb_h.status == CAM_REQ_CMP) {
1493 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1494 && cts.sync_offset != 0) {
1495 freq = scsi_calc_syncsrate(cts.sync_period);
1498 struct ccb_pathinq cpi;
1500 /* Ask the SIM for its base transfer speed */
1501 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1502 cpi.ccb_h.func_code = XPT_PATH_INQ;
1503 xpt_action((union ccb *)&cpi);
1505 speed = cpi.base_transfer_speed;
1508 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1509 speed *= (0x01 << cts.bus_width);
1512 printf("%s%d: %d.%03dMB/s transfers",
1513 periph->periph_name, periph->unit_number,
1516 printf("%s%d: %dKB/s transfers", periph->periph_name,
1517 periph->unit_number, speed);
1518 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1519 && cts.sync_offset != 0) {
1520 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1521 freq % 1000, cts.sync_offset);
1523 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1524 && cts.bus_width > 0) {
1525 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1526 && cts.sync_offset != 0) {
1531 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1532 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1533 && cts.sync_offset != 0) {
1537 if (path->device->inq_flags & SID_CmdQue
1538 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1539 printf(", Tagged Queueing Enabled");
1543 } else if (path->device->inq_flags & SID_CmdQue
1544 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1545 printf("%s%d: Tagged Queueing Enabled\n",
1546 periph->periph_name, periph->unit_number);
1550 * We only want to print the caller's announce string if they've
1553 if (announce_string != NULL)
1554 printf("%s%d: %s\n", periph->periph_name,
1555 periph->unit_number, announce_string);
1560 static dev_match_ret
1561 xptbusmatch(struct dev_match_pattern *patterns, int num_patterns,
1564 dev_match_ret retval;
1567 retval = DM_RET_NONE;
1570 * If we aren't given something to match against, that's an error.
1573 return(DM_RET_ERROR);
1576 * If there are no match entries, then this bus matches no
1579 if ((patterns == NULL) || (num_patterns == 0))
1580 return(DM_RET_DESCEND | DM_RET_COPY);
1582 for (i = 0; i < num_patterns; i++) {
1583 struct bus_match_pattern *cur_pattern;
1586 * If the pattern in question isn't for a bus node, we
1587 * aren't interested. However, we do indicate to the
1588 * calling routine that we should continue descending the
1589 * tree, since the user wants to match against lower-level
1592 if (patterns[i].type != DEV_MATCH_BUS) {
1593 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1594 retval |= DM_RET_DESCEND;
1598 cur_pattern = &patterns[i].pattern.bus_pattern;
1601 * If they want to match any bus node, we give them any
1604 if (cur_pattern->flags == BUS_MATCH_ANY) {
1605 /* set the copy flag */
1606 retval |= DM_RET_COPY;
1609 * If we've already decided on an action, go ahead
1612 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1617 * Not sure why someone would do this...
1619 if (cur_pattern->flags == BUS_MATCH_NONE)
1622 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1623 && (cur_pattern->path_id != bus->path_id))
1626 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1627 && (cur_pattern->bus_id != bus->sim->bus_id))
1630 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1631 && (cur_pattern->unit_number != bus->sim->unit_number))
1634 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1635 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1640 * If we get to this point, the user definitely wants
1641 * information on this bus. So tell the caller to copy the
1644 retval |= DM_RET_COPY;
1647 * If the return action has been set to descend, then we
1648 * know that we've already seen a non-bus matching
1649 * expression, therefore we need to further descend the tree.
1650 * This won't change by continuing around the loop, so we
1651 * go ahead and return. If we haven't seen a non-bus
1652 * matching expression, we keep going around the loop until
1653 * we exhaust the matching expressions. We'll set the stop
1654 * flag once we fall out of the loop.
1656 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1661 * If the return action hasn't been set to descend yet, that means
1662 * we haven't seen anything other than bus matching patterns. So
1663 * tell the caller to stop descending the tree -- the user doesn't
1664 * want to match against lower level tree elements.
1666 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1667 retval |= DM_RET_STOP;
1672 static dev_match_ret
1673 xptdevicematch(struct dev_match_pattern *patterns, int num_patterns,
1674 struct cam_ed *device)
1676 dev_match_ret retval;
1679 retval = DM_RET_NONE;
1682 * If we aren't given something to match against, that's an error.
1685 return(DM_RET_ERROR);
1688 * If there are no match entries, then this device matches no
1691 if ((patterns == NULL) || (patterns == 0))
1692 return(DM_RET_DESCEND | DM_RET_COPY);
1694 for (i = 0; i < num_patterns; i++) {
1695 struct device_match_pattern *cur_pattern;
1698 * If the pattern in question isn't for a device node, we
1699 * aren't interested.
1701 if (patterns[i].type != DEV_MATCH_DEVICE) {
1702 if ((patterns[i].type == DEV_MATCH_PERIPH)
1703 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1704 retval |= DM_RET_DESCEND;
1708 cur_pattern = &patterns[i].pattern.device_pattern;
1711 * If they want to match any device node, we give them any
1714 if (cur_pattern->flags == DEV_MATCH_ANY) {
1715 /* set the copy flag */
1716 retval |= DM_RET_COPY;
1720 * If we've already decided on an action, go ahead
1723 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1728 * Not sure why someone would do this...
1730 if (cur_pattern->flags == DEV_MATCH_NONE)
1733 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1734 && (cur_pattern->path_id != device->target->bus->path_id))
1737 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1738 && (cur_pattern->target_id != device->target->target_id))
1741 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1742 && (cur_pattern->target_lun != device->lun_id))
1745 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1746 && (cam_quirkmatch((caddr_t)&device->inq_data,
1747 (caddr_t)&cur_pattern->inq_pat,
1748 1, sizeof(cur_pattern->inq_pat),
1749 scsi_static_inquiry_match) == NULL))
1753 * If we get to this point, the user definitely wants
1754 * information on this device. So tell the caller to copy
1757 retval |= DM_RET_COPY;
1760 * If the return action has been set to descend, then we
1761 * know that we've already seen a peripheral matching
1762 * expression, therefore we need to further descend the tree.
1763 * This won't change by continuing around the loop, so we
1764 * go ahead and return. If we haven't seen a peripheral
1765 * matching expression, we keep going around the loop until
1766 * we exhaust the matching expressions. We'll set the stop
1767 * flag once we fall out of the loop.
1769 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1774 * If the return action hasn't been set to descend yet, that means
1775 * we haven't seen any peripheral matching patterns. So tell the
1776 * caller to stop descending the tree -- the user doesn't want to
1777 * match against lower level tree elements.
1779 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1780 retval |= DM_RET_STOP;
1786 * Match a single peripheral against any number of match patterns.
1788 static dev_match_ret
1789 xptperiphmatch(struct dev_match_pattern *patterns, int num_patterns,
1790 struct cam_periph *periph)
1792 dev_match_ret retval;
1796 * If we aren't given something to match against, that's an error.
1799 return(DM_RET_ERROR);
1802 * If there are no match entries, then this peripheral matches no
1805 if ((patterns == NULL) || (num_patterns == 0))
1806 return(DM_RET_STOP | DM_RET_COPY);
1809 * There aren't any nodes below a peripheral node, so there's no
1810 * reason to descend the tree any further.
1812 retval = DM_RET_STOP;
1814 for (i = 0; i < num_patterns; i++) {
1815 struct periph_match_pattern *cur_pattern;
1818 * If the pattern in question isn't for a peripheral, we
1819 * aren't interested.
1821 if (patterns[i].type != DEV_MATCH_PERIPH)
1824 cur_pattern = &patterns[i].pattern.periph_pattern;
1827 * If they want to match on anything, then we will do so.
1829 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1830 /* set the copy flag */
1831 retval |= DM_RET_COPY;
1834 * We've already set the return action to stop,
1835 * since there are no nodes below peripherals in
1842 * Not sure why someone would do this...
1844 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1847 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1848 && (cur_pattern->path_id != periph->path->bus->path_id))
1852 * For the target and lun id's, we have to make sure the
1853 * target and lun pointers aren't NULL. The xpt peripheral
1854 * has a wildcard target and device.
1856 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1857 && ((periph->path->target == NULL)
1858 ||(cur_pattern->target_id != periph->path->target->target_id)))
1861 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1862 && ((periph->path->device == NULL)
1863 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1866 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1867 && (cur_pattern->unit_number != periph->unit_number))
1870 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1871 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1876 * If we get to this point, the user definitely wants
1877 * information on this peripheral. So tell the caller to
1878 * copy the data out.
1880 retval |= DM_RET_COPY;
1883 * The return action has already been set to stop, since
1884 * peripherals don't have any nodes below them in the EDT.
1890 * If we get to this point, the peripheral that was passed in
1891 * doesn't match any of the patterns.
1897 xptedtbusfunc(struct cam_eb *bus, void *arg)
1899 struct ccb_dev_match *cdm;
1900 dev_match_ret retval;
1902 cdm = (struct ccb_dev_match *)arg;
1905 * If our position is for something deeper in the tree, that means
1906 * that we've already seen this node. So, we keep going down.
1908 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1909 && (cdm->pos.cookie.bus == bus)
1910 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1911 && (cdm->pos.cookie.target != NULL))
1912 retval = DM_RET_DESCEND;
1914 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1917 * If we got an error, bail out of the search.
1919 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1920 cdm->status = CAM_DEV_MATCH_ERROR;
1925 * If the copy flag is set, copy this bus out.
1927 if (retval & DM_RET_COPY) {
1930 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1931 sizeof(struct dev_match_result));
1934 * If we don't have enough space to put in another
1935 * match result, save our position and tell the
1936 * user there are more devices to check.
1938 if (spaceleft < sizeof(struct dev_match_result)) {
1939 bzero(&cdm->pos, sizeof(cdm->pos));
1940 cdm->pos.position_type =
1941 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1943 cdm->pos.cookie.bus = bus;
1944 cdm->pos.generations[CAM_BUS_GENERATION]=
1946 cdm->status = CAM_DEV_MATCH_MORE;
1949 j = cdm->num_matches;
1951 cdm->matches[j].type = DEV_MATCH_BUS;
1952 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1953 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1954 cdm->matches[j].result.bus_result.unit_number =
1955 bus->sim->unit_number;
1956 strncpy(cdm->matches[j].result.bus_result.dev_name,
1957 bus->sim->sim_name, DEV_IDLEN);
1961 * If the user is only interested in busses, there's no
1962 * reason to descend to the next level in the tree.
1964 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1968 * If there is a target generation recorded, check it to
1969 * make sure the target list hasn't changed.
1971 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1972 && (bus == cdm->pos.cookie.bus)
1973 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1974 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1975 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1977 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1981 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1982 && (cdm->pos.cookie.bus == bus)
1983 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1984 && (cdm->pos.cookie.target != NULL))
1985 return(xpttargettraverse(bus,
1986 (struct cam_et *)cdm->pos.cookie.target,
1987 xptedttargetfunc, arg));
1989 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1993 xptedttargetfunc(struct cam_et *target, void *arg)
1995 struct ccb_dev_match *cdm;
1997 cdm = (struct ccb_dev_match *)arg;
2000 * If there is a device list generation recorded, check it to
2001 * make sure the device list hasn't changed.
2003 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2004 && (cdm->pos.cookie.bus == target->bus)
2005 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2006 && (cdm->pos.cookie.target == target)
2007 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2008 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2009 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2010 target->generation)) {
2011 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2015 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2016 && (cdm->pos.cookie.bus == target->bus)
2017 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2018 && (cdm->pos.cookie.target == target)
2019 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2020 && (cdm->pos.cookie.device != NULL))
2021 return(xptdevicetraverse(target,
2022 (struct cam_ed *)cdm->pos.cookie.device,
2023 xptedtdevicefunc, arg));
2025 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2029 xptedtdevicefunc(struct cam_ed *device, void *arg)
2032 struct ccb_dev_match *cdm;
2033 dev_match_ret retval;
2035 cdm = (struct ccb_dev_match *)arg;
2038 * If our position is for something deeper in the tree, that means
2039 * that we've already seen this node. So, we keep going down.
2041 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2042 && (cdm->pos.cookie.device == device)
2043 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2044 && (cdm->pos.cookie.periph != NULL))
2045 retval = DM_RET_DESCEND;
2047 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2050 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2051 cdm->status = CAM_DEV_MATCH_ERROR;
2056 * If the copy flag is set, copy this device out.
2058 if (retval & DM_RET_COPY) {
2061 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2062 sizeof(struct dev_match_result));
2065 * If we don't have enough space to put in another
2066 * match result, save our position and tell the
2067 * user there are more devices to check.
2069 if (spaceleft < sizeof(struct dev_match_result)) {
2070 bzero(&cdm->pos, sizeof(cdm->pos));
2071 cdm->pos.position_type =
2072 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2073 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2075 cdm->pos.cookie.bus = device->target->bus;
2076 cdm->pos.generations[CAM_BUS_GENERATION]=
2078 cdm->pos.cookie.target = device->target;
2079 cdm->pos.generations[CAM_TARGET_GENERATION] =
2080 device->target->bus->generation;
2081 cdm->pos.cookie.device = device;
2082 cdm->pos.generations[CAM_DEV_GENERATION] =
2083 device->target->generation;
2084 cdm->status = CAM_DEV_MATCH_MORE;
2087 j = cdm->num_matches;
2089 cdm->matches[j].type = DEV_MATCH_DEVICE;
2090 cdm->matches[j].result.device_result.path_id =
2091 device->target->bus->path_id;
2092 cdm->matches[j].result.device_result.target_id =
2093 device->target->target_id;
2094 cdm->matches[j].result.device_result.target_lun =
2096 bcopy(&device->inq_data,
2097 &cdm->matches[j].result.device_result.inq_data,
2098 sizeof(struct scsi_inquiry_data));
2100 /* Let the user know whether this device is unconfigured */
2101 if (device->flags & CAM_DEV_UNCONFIGURED)
2102 cdm->matches[j].result.device_result.flags =
2103 DEV_RESULT_UNCONFIGURED;
2105 cdm->matches[j].result.device_result.flags =
2110 * If the user isn't interested in peripherals, don't descend
2111 * the tree any further.
2113 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2117 * If there is a peripheral list generation recorded, make sure
2118 * it hasn't changed.
2120 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2121 && (device->target->bus == cdm->pos.cookie.bus)
2122 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2123 && (device->target == cdm->pos.cookie.target)
2124 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2125 && (device == cdm->pos.cookie.device)
2126 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2127 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2128 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2129 device->generation)){
2130 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2134 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2135 && (cdm->pos.cookie.bus == device->target->bus)
2136 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2137 && (cdm->pos.cookie.target == device->target)
2138 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2139 && (cdm->pos.cookie.device == device)
2140 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2141 && (cdm->pos.cookie.periph != NULL))
2142 return(xptperiphtraverse(device,
2143 (struct cam_periph *)cdm->pos.cookie.periph,
2144 xptedtperiphfunc, arg));
2146 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2150 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2152 struct ccb_dev_match *cdm;
2153 dev_match_ret retval;
2155 cdm = (struct ccb_dev_match *)arg;
2157 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2159 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2160 cdm->status = CAM_DEV_MATCH_ERROR;
2165 * If the copy flag is set, copy this peripheral out.
2167 if (retval & DM_RET_COPY) {
2170 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2171 sizeof(struct dev_match_result));
2174 * If we don't have enough space to put in another
2175 * match result, save our position and tell the
2176 * user there are more devices to check.
2178 if (spaceleft < sizeof(struct dev_match_result)) {
2179 bzero(&cdm->pos, sizeof(cdm->pos));
2180 cdm->pos.position_type =
2181 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2182 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2185 cdm->pos.cookie.bus = periph->path->bus;
2186 cdm->pos.generations[CAM_BUS_GENERATION]=
2188 cdm->pos.cookie.target = periph->path->target;
2189 cdm->pos.generations[CAM_TARGET_GENERATION] =
2190 periph->path->bus->generation;
2191 cdm->pos.cookie.device = periph->path->device;
2192 cdm->pos.generations[CAM_DEV_GENERATION] =
2193 periph->path->target->generation;
2194 cdm->pos.cookie.periph = periph;
2195 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2196 periph->path->device->generation;
2197 cdm->status = CAM_DEV_MATCH_MORE;
2201 j = cdm->num_matches;
2203 cdm->matches[j].type = DEV_MATCH_PERIPH;
2204 cdm->matches[j].result.periph_result.path_id =
2205 periph->path->bus->path_id;
2206 cdm->matches[j].result.periph_result.target_id =
2207 periph->path->target->target_id;
2208 cdm->matches[j].result.periph_result.target_lun =
2209 periph->path->device->lun_id;
2210 cdm->matches[j].result.periph_result.unit_number =
2211 periph->unit_number;
2212 strncpy(cdm->matches[j].result.periph_result.periph_name,
2213 periph->periph_name, DEV_IDLEN);
2220 xptedtmatch(struct ccb_dev_match *cdm)
2224 cdm->num_matches = 0;
2227 * Check the bus list generation. If it has changed, the user
2228 * needs to reset everything and start over.
2230 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2231 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2232 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2233 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2237 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2238 && (cdm->pos.cookie.bus != NULL))
2239 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2240 xptedtbusfunc, cdm);
2242 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2245 * If we get back 0, that means that we had to stop before fully
2246 * traversing the EDT. It also means that one of the subroutines
2247 * has set the status field to the proper value. If we get back 1,
2248 * we've fully traversed the EDT and copied out any matching entries.
2251 cdm->status = CAM_DEV_MATCH_LAST;
2257 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2259 struct ccb_dev_match *cdm;
2261 cdm = (struct ccb_dev_match *)arg;
2263 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2264 && (cdm->pos.cookie.pdrv == pdrv)
2265 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2266 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2267 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2268 (*pdrv)->generation)) {
2269 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2273 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2274 && (cdm->pos.cookie.pdrv == pdrv)
2275 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2276 && (cdm->pos.cookie.periph != NULL))
2277 return(xptpdperiphtraverse(pdrv,
2278 (struct cam_periph *)cdm->pos.cookie.periph,
2279 xptplistperiphfunc, arg));
2281 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2285 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2287 struct ccb_dev_match *cdm;
2288 dev_match_ret retval;
2290 cdm = (struct ccb_dev_match *)arg;
2292 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2294 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2295 cdm->status = CAM_DEV_MATCH_ERROR;
2300 * If the copy flag is set, copy this peripheral out.
2302 if (retval & DM_RET_COPY) {
2305 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2306 sizeof(struct dev_match_result));
2309 * If we don't have enough space to put in another
2310 * match result, save our position and tell the
2311 * user there are more devices to check.
2313 if (spaceleft < sizeof(struct dev_match_result)) {
2314 struct periph_driver **pdrv;
2317 bzero(&cdm->pos, sizeof(cdm->pos));
2318 cdm->pos.position_type =
2319 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2323 * This may look a bit non-sensical, but it is
2324 * actually quite logical. There are very few
2325 * peripheral drivers, and bloating every peripheral
2326 * structure with a pointer back to its parent
2327 * peripheral driver linker set entry would cost
2328 * more in the long run than doing this quick lookup.
2330 SET_FOREACH(pdrv, periphdriver_set) {
2331 if (strcmp((*pdrv)->driver_name,
2332 periph->periph_name) == 0)
2336 if (*pdrv == NULL) {
2337 cdm->status = CAM_DEV_MATCH_ERROR;
2341 cdm->pos.cookie.pdrv = pdrv;
2343 * The periph generation slot does double duty, as
2344 * does the periph pointer slot. They are used for
2345 * both edt and pdrv lookups and positioning.
2347 cdm->pos.cookie.periph = periph;
2348 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2349 (*pdrv)->generation;
2350 cdm->status = CAM_DEV_MATCH_MORE;
2354 j = cdm->num_matches;
2356 cdm->matches[j].type = DEV_MATCH_PERIPH;
2357 cdm->matches[j].result.periph_result.path_id =
2358 periph->path->bus->path_id;
2361 * The transport layer peripheral doesn't have a target or
2364 if (periph->path->target)
2365 cdm->matches[j].result.periph_result.target_id =
2366 periph->path->target->target_id;
2368 cdm->matches[j].result.periph_result.target_id = -1;
2370 if (periph->path->device)
2371 cdm->matches[j].result.periph_result.target_lun =
2372 periph->path->device->lun_id;
2374 cdm->matches[j].result.periph_result.target_lun = -1;
2376 cdm->matches[j].result.periph_result.unit_number =
2377 periph->unit_number;
2378 strncpy(cdm->matches[j].result.periph_result.periph_name,
2379 periph->periph_name, DEV_IDLEN);
2386 xptperiphlistmatch(struct ccb_dev_match *cdm)
2390 cdm->num_matches = 0;
2393 * At this point in the edt traversal function, we check the bus
2394 * list generation to make sure that no busses have been added or
2395 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2396 * For the peripheral driver list traversal function, however, we
2397 * don't have to worry about new peripheral driver types coming or
2398 * going; they're in a linker set, and therefore can't change
2399 * without a recompile.
2402 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2403 && (cdm->pos.cookie.pdrv != NULL))
2404 ret = xptpdrvtraverse(
2405 (struct periph_driver **)cdm->pos.cookie.pdrv,
2406 xptplistpdrvfunc, cdm);
2408 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2411 * If we get back 0, that means that we had to stop before fully
2412 * traversing the peripheral driver tree. It also means that one of
2413 * the subroutines has set the status field to the proper value. If
2414 * we get back 1, we've fully traversed the EDT and copied out any
2418 cdm->status = CAM_DEV_MATCH_LAST;
2424 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2426 struct cam_eb *bus, *next_bus;
2431 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2434 next_bus = TAILQ_NEXT(bus, links);
2436 retval = tr_func(bus, arg);
2445 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2446 xpt_targetfunc_t *tr_func, void *arg)
2448 struct cam_et *target, *next_target;
2452 for (target = (start_target ? start_target :
2453 TAILQ_FIRST(&bus->et_entries));
2454 target != NULL; target = next_target) {
2456 next_target = TAILQ_NEXT(target, links);
2458 retval = tr_func(target, arg);
2468 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2469 xpt_devicefunc_t *tr_func, void *arg)
2471 struct cam_ed *device, *next_device;
2475 for (device = (start_device ? start_device :
2476 TAILQ_FIRST(&target->ed_entries));
2478 device = next_device) {
2480 next_device = TAILQ_NEXT(device, links);
2482 retval = tr_func(device, arg);
2492 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2493 xpt_periphfunc_t *tr_func, void *arg)
2495 struct cam_periph *periph, *next_periph;
2500 for (periph = (start_periph ? start_periph :
2501 SLIST_FIRST(&device->periphs));
2503 periph = next_periph) {
2505 next_periph = SLIST_NEXT(periph, periph_links);
2507 retval = tr_func(periph, arg);
2516 xptpdrvtraverse(struct periph_driver **start_pdrv,
2517 xpt_pdrvfunc_t *tr_func, void *arg)
2519 struct periph_driver **pdrv;
2525 * We don't traverse the peripheral driver list like we do the
2526 * other lists, because it is a linker set, and therefore cannot be
2527 * changed during runtime. If the peripheral driver list is ever
2528 * re-done to be something other than a linker set (i.e. it can
2529 * change while the system is running), the list traversal should
2530 * be modified to work like the other traversal functions.
2532 SET_FOREACH(pdrv, periphdriver_set) {
2533 if (start_pdrv == NULL || start_pdrv == pdrv) {
2534 retval = tr_func(pdrv, arg);
2537 start_pdrv = NULL; /* traverse remainder */
2544 xptpdperiphtraverse(struct periph_driver **pdrv,
2545 struct cam_periph *start_periph,
2546 xpt_periphfunc_t *tr_func, void *arg)
2548 struct cam_periph *periph, *next_periph;
2553 for (periph = (start_periph ? start_periph :
2554 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2555 periph = next_periph) {
2557 next_periph = TAILQ_NEXT(periph, unit_links);
2559 retval = tr_func(periph, arg);
2567 xptdefbusfunc(struct cam_eb *bus, void *arg)
2569 struct xpt_traverse_config *tr_config;
2571 tr_config = (struct xpt_traverse_config *)arg;
2573 if (tr_config->depth == XPT_DEPTH_BUS) {
2574 xpt_busfunc_t *tr_func;
2576 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2578 return(tr_func(bus, tr_config->tr_arg));
2580 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2584 xptdeftargetfunc(struct cam_et *target, void *arg)
2586 struct xpt_traverse_config *tr_config;
2588 tr_config = (struct xpt_traverse_config *)arg;
2590 if (tr_config->depth == XPT_DEPTH_TARGET) {
2591 xpt_targetfunc_t *tr_func;
2593 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2595 return(tr_func(target, tr_config->tr_arg));
2597 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2601 xptdefdevicefunc(struct cam_ed *device, void *arg)
2603 struct xpt_traverse_config *tr_config;
2605 tr_config = (struct xpt_traverse_config *)arg;
2607 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2608 xpt_devicefunc_t *tr_func;
2610 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2612 return(tr_func(device, tr_config->tr_arg));
2614 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2618 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2620 struct xpt_traverse_config *tr_config;
2621 xpt_periphfunc_t *tr_func;
2623 tr_config = (struct xpt_traverse_config *)arg;
2625 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2628 * Unlike the other default functions, we don't check for depth
2629 * here. The peripheral driver level is the last level in the EDT,
2630 * so if we're here, we should execute the function in question.
2632 return(tr_func(periph, tr_config->tr_arg));
2636 * Execute the given function for every bus in the EDT.
2639 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2641 struct xpt_traverse_config tr_config;
2643 tr_config.depth = XPT_DEPTH_BUS;
2644 tr_config.tr_func = tr_func;
2645 tr_config.tr_arg = arg;
2647 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2652 * Execute the given function for every target in the EDT.
2655 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2657 struct xpt_traverse_config tr_config;
2659 tr_config.depth = XPT_DEPTH_TARGET;
2660 tr_config.tr_func = tr_func;
2661 tr_config.tr_arg = arg;
2663 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2665 #endif /* notusedyet */
2668 * Execute the given function for every device in the EDT.
2671 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2673 struct xpt_traverse_config tr_config;
2675 tr_config.depth = XPT_DEPTH_DEVICE;
2676 tr_config.tr_func = tr_func;
2677 tr_config.tr_arg = arg;
2679 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2684 * Execute the given function for every peripheral in the EDT.
2687 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2689 struct xpt_traverse_config tr_config;
2691 tr_config.depth = XPT_DEPTH_PERIPH;
2692 tr_config.tr_func = tr_func;
2693 tr_config.tr_arg = arg;
2695 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2697 #endif /* notusedyet */
2700 xptsetasyncfunc(struct cam_ed *device, void *arg)
2702 struct cam_path path;
2703 struct ccb_getdev cgd;
2704 struct async_node *cur_entry;
2706 cur_entry = (struct async_node *)arg;
2709 * Don't report unconfigured devices (Wildcard devs,
2710 * devices only for target mode, device instances
2711 * that have been invalidated but are waiting for
2712 * their last reference count to be released).
2714 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2717 xpt_compile_path(&path,
2719 device->target->bus->path_id,
2720 device->target->target_id,
2722 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2723 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2724 xpt_action((union ccb *)&cgd);
2725 cur_entry->callback(cur_entry->callback_arg,
2728 xpt_release_path(&path);
2734 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2736 struct cam_path path;
2737 struct ccb_pathinq cpi;
2738 struct async_node *cur_entry;
2740 cur_entry = (struct async_node *)arg;
2742 xpt_compile_path(&path, /*periph*/NULL,
2744 CAM_TARGET_WILDCARD,
2746 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2747 cpi.ccb_h.func_code = XPT_PATH_INQ;
2748 xpt_action((union ccb *)&cpi);
2749 cur_entry->callback(cur_entry->callback_arg,
2752 xpt_release_path(&path);
2758 xpt_action(union ccb *start_ccb)
2762 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2764 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2766 iopl = splsoftcam();
2767 switch (start_ccb->ccb_h.func_code) {
2771 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2772 struct cam_path *path;
2774 path = start_ccb->ccb_h.path;
2778 * For the sake of compatibility with SCSI-1
2779 * devices that may not understand the identify
2780 * message, we include lun information in the
2781 * second byte of all commands. SCSI-1 specifies
2782 * that luns are a 3 bit value and reserves only 3
2783 * bits for lun information in the CDB. Later
2784 * revisions of the SCSI spec allow for more than 8
2785 * luns, but have deprecated lun information in the
2786 * CDB. So, if the lun won't fit, we must omit.
2788 * Also be aware that during initial probing for devices,
2789 * the inquiry information is unknown but initialized to 0.
2790 * This means that this code will be exercised while probing
2791 * devices with an ANSI revision greater than 2.
2793 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2794 && start_ccb->ccb_h.target_lun < 8
2795 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2797 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2798 start_ccb->ccb_h.target_lun << 5;
2800 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2801 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2802 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2803 &path->device->inq_data),
2804 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2805 cdb_str, sizeof(cdb_str))));
2809 case XPT_CONT_TARGET_IO:
2810 start_ccb->csio.sense_resid = 0;
2811 start_ccb->csio.resid = 0;
2816 struct cam_path *path;
2820 path = start_ccb->ccb_h.path;
2823 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2824 if (path->device->qfrozen_cnt == 0)
2825 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2830 xpt_run_dev_sendq(path->bus);
2833 case XPT_SET_TRAN_SETTINGS:
2835 xpt_set_transfer_settings(&start_ccb->cts,
2836 start_ccb->ccb_h.path->device,
2837 /*async_update*/FALSE);
2840 case XPT_CALC_GEOMETRY:
2842 struct cam_sim *sim;
2844 /* Filter out garbage */
2845 if (start_ccb->ccg.block_size == 0
2846 || start_ccb->ccg.volume_size == 0) {
2847 start_ccb->ccg.cylinders = 0;
2848 start_ccb->ccg.heads = 0;
2849 start_ccb->ccg.secs_per_track = 0;
2850 start_ccb->ccb_h.status = CAM_REQ_CMP;
2853 sim = start_ccb->ccb_h.path->bus->sim;
2854 (*(sim->sim_action))(sim, start_ccb);
2859 union ccb* abort_ccb;
2862 abort_ccb = start_ccb->cab.abort_ccb;
2863 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2865 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2866 struct cam_ccbq *ccbq;
2868 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2869 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2870 abort_ccb->ccb_h.status =
2871 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2872 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2874 xpt_done(abort_ccb);
2876 start_ccb->ccb_h.status = CAM_REQ_CMP;
2879 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2880 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2882 * We've caught this ccb en route to
2883 * the SIM. Flag it for abort and the
2884 * SIM will do so just before starting
2885 * real work on the CCB.
2887 abort_ccb->ccb_h.status =
2888 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2889 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2890 start_ccb->ccb_h.status = CAM_REQ_CMP;
2894 if (XPT_FC_IS_QUEUED(abort_ccb)
2895 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2897 * It's already completed but waiting
2898 * for our SWI to get to it.
2900 start_ccb->ccb_h.status = CAM_UA_ABORT;
2904 * If we weren't able to take care of the abort request
2905 * in the XPT, pass the request down to the SIM for processing.
2909 case XPT_ACCEPT_TARGET_IO:
2911 case XPT_IMMED_NOTIFY:
2912 case XPT_NOTIFY_ACK:
2913 case XPT_GET_TRAN_SETTINGS:
2916 struct cam_sim *sim;
2918 sim = start_ccb->ccb_h.path->bus->sim;
2919 (*(sim->sim_action))(sim, start_ccb);
2924 struct cam_sim *sim;
2926 sim = start_ccb->ccb_h.path->bus->sim;
2927 (*(sim->sim_action))(sim, start_ccb);
2930 case XPT_PATH_STATS:
2931 start_ccb->cpis.last_reset =
2932 start_ccb->ccb_h.path->bus->last_reset;
2933 start_ccb->ccb_h.status = CAM_REQ_CMP;
2940 dev = start_ccb->ccb_h.path->device;
2942 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2943 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2945 struct ccb_getdev *cgd;
2949 cgd = &start_ccb->cgd;
2950 bus = cgd->ccb_h.path->bus;
2951 tar = cgd->ccb_h.path->target;
2952 cgd->inq_data = dev->inq_data;
2953 cgd->ccb_h.status = CAM_REQ_CMP;
2954 cgd->serial_num_len = dev->serial_num_len;
2955 if ((dev->serial_num_len > 0)
2956 && (dev->serial_num != NULL))
2957 bcopy(dev->serial_num, cgd->serial_num,
2958 dev->serial_num_len);
2963 case XPT_GDEV_STATS:
2968 dev = start_ccb->ccb_h.path->device;
2970 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2971 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2973 struct ccb_getdevstats *cgds;
2977 cgds = &start_ccb->cgds;
2978 bus = cgds->ccb_h.path->bus;
2979 tar = cgds->ccb_h.path->target;
2980 cgds->dev_openings = dev->ccbq.dev_openings;
2981 cgds->dev_active = dev->ccbq.dev_active;
2982 cgds->devq_openings = dev->ccbq.devq_openings;
2983 cgds->devq_queued = dev->ccbq.queue.entries;
2984 cgds->held = dev->ccbq.held;
2985 cgds->last_reset = tar->last_reset;
2986 cgds->maxtags = dev->quirk->maxtags;
2987 cgds->mintags = dev->quirk->mintags;
2988 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2989 cgds->last_reset = bus->last_reset;
2990 cgds->ccb_h.status = CAM_REQ_CMP;
2997 struct cam_periph *nperiph;
2998 struct periph_list *periph_head;
2999 struct ccb_getdevlist *cgdl;
3002 struct cam_ed *device;
3009 * Don't want anyone mucking with our data.
3012 device = start_ccb->ccb_h.path->device;
3013 periph_head = &device->periphs;
3014 cgdl = &start_ccb->cgdl;
3017 * Check and see if the list has changed since the user
3018 * last requested a list member. If so, tell them that the
3019 * list has changed, and therefore they need to start over
3020 * from the beginning.
3022 if ((cgdl->index != 0) &&
3023 (cgdl->generation != device->generation)) {
3024 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3030 * Traverse the list of peripherals and attempt to find
3031 * the requested peripheral.
3033 for (nperiph = periph_head->slh_first, i = 0;
3034 (nperiph != NULL) && (i <= cgdl->index);
3035 nperiph = nperiph->periph_links.sle_next, i++) {
3036 if (i == cgdl->index) {
3037 strncpy(cgdl->periph_name,
3038 nperiph->periph_name,
3040 cgdl->unit_number = nperiph->unit_number;
3045 cgdl->status = CAM_GDEVLIST_ERROR;
3050 if (nperiph == NULL)
3051 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3053 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3056 cgdl->generation = device->generation;
3059 cgdl->ccb_h.status = CAM_REQ_CMP;
3065 dev_pos_type position_type;
3066 struct ccb_dev_match *cdm;
3069 cdm = &start_ccb->cdm;
3072 * Prevent EDT changes while we traverse it.
3076 * There are two ways of getting at information in the EDT.
3077 * The first way is via the primary EDT tree. It starts
3078 * with a list of busses, then a list of targets on a bus,
3079 * then devices/luns on a target, and then peripherals on a
3080 * device/lun. The "other" way is by the peripheral driver
3081 * lists. The peripheral driver lists are organized by
3082 * peripheral driver. (obviously) So it makes sense to
3083 * use the peripheral driver list if the user is looking
3084 * for something like "da1", or all "da" devices. If the
3085 * user is looking for something on a particular bus/target
3086 * or lun, it's generally better to go through the EDT tree.
3089 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3090 position_type = cdm->pos.position_type;
3094 position_type = CAM_DEV_POS_NONE;
3096 for (i = 0; i < cdm->num_patterns; i++) {
3097 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3098 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3099 position_type = CAM_DEV_POS_EDT;
3104 if (cdm->num_patterns == 0)
3105 position_type = CAM_DEV_POS_EDT;
3106 else if (position_type == CAM_DEV_POS_NONE)
3107 position_type = CAM_DEV_POS_PDRV;
3110 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3111 case CAM_DEV_POS_EDT:
3112 ret = xptedtmatch(cdm);
3114 case CAM_DEV_POS_PDRV:
3115 ret = xptperiphlistmatch(cdm);
3118 cdm->status = CAM_DEV_MATCH_ERROR;
3124 if (cdm->status == CAM_DEV_MATCH_ERROR)
3125 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3127 start_ccb->ccb_h.status = CAM_REQ_CMP;
3133 struct ccb_setasync *csa;
3134 struct async_node *cur_entry;
3135 struct async_list *async_head;
3139 csa = &start_ccb->csa;
3140 added = csa->event_enable;
3141 async_head = &csa->ccb_h.path->device->asyncs;
3144 * If there is already an entry for us, simply
3148 cur_entry = SLIST_FIRST(async_head);
3149 while (cur_entry != NULL) {
3150 if ((cur_entry->callback_arg == csa->callback_arg)
3151 && (cur_entry->callback == csa->callback))
3153 cur_entry = SLIST_NEXT(cur_entry, links);
3156 if (cur_entry != NULL) {
3158 * If the request has no flags set,
3161 added &= ~cur_entry->event_enable;
3162 if (csa->event_enable == 0) {
3163 SLIST_REMOVE(async_head, cur_entry,
3165 csa->ccb_h.path->device->refcount--;
3166 free(cur_entry, M_DEVBUF);
3168 cur_entry->event_enable = csa->event_enable;
3171 cur_entry = malloc(sizeof(*cur_entry),
3172 M_DEVBUF, M_INTWAIT);
3173 cur_entry->event_enable = csa->event_enable;
3174 cur_entry->callback_arg = csa->callback_arg;
3175 cur_entry->callback = csa->callback;
3176 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3177 csa->ccb_h.path->device->refcount++;
3180 if ((added & AC_FOUND_DEVICE) != 0) {
3182 * Get this peripheral up to date with all
3183 * the currently existing devices.
3185 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3187 if ((added & AC_PATH_REGISTERED) != 0) {
3189 * Get this peripheral up to date with all
3190 * the currently existing busses.
3192 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3195 start_ccb->ccb_h.status = CAM_REQ_CMP;
3200 struct ccb_relsim *crs;
3204 crs = &start_ccb->crs;
3205 dev = crs->ccb_h.path->device;
3208 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3214 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3216 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3218 /* Don't ever go below one opening */
3219 if (crs->openings > 0) {
3220 xpt_dev_ccbq_resize(crs->ccb_h.path,
3224 xpt_print_path(crs->ccb_h.path);
3225 printf("tagged openings "
3233 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3235 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3238 * Just extend the old timeout and decrement
3239 * the freeze count so that a single timeout
3240 * is sufficient for releasing the queue.
3242 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3243 callout_stop(&dev->c_handle);
3246 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3249 callout_reset(&dev->c_handle,
3250 (crs->release_timeout * hz) / 1000,
3251 xpt_release_devq_timeout, dev);
3253 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3257 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3259 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3261 * Decrement the freeze count so that a single
3262 * completion is still sufficient to unfreeze
3265 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3268 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3269 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3273 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3275 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3276 || (dev->ccbq.dev_active == 0)) {
3278 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3281 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3282 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3287 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3289 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3292 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3293 start_ccb->ccb_h.status = CAM_REQ_CMP;
3297 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3300 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3301 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3309 #ifdef CAM_DEBUG_DELAY
3310 cam_debug_delay = CAM_DEBUG_DELAY;
3312 cam_dflags = start_ccb->cdbg.flags;
3313 if (cam_dpath != NULL) {
3314 xpt_free_path(cam_dpath);
3318 if (cam_dflags != CAM_DEBUG_NONE) {
3319 if (xpt_create_path(&cam_dpath, xpt_periph,
3320 start_ccb->ccb_h.path_id,
3321 start_ccb->ccb_h.target_id,
3322 start_ccb->ccb_h.target_lun) !=
3324 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3325 cam_dflags = CAM_DEBUG_NONE;
3327 start_ccb->ccb_h.status = CAM_REQ_CMP;
3328 xpt_print_path(cam_dpath);
3329 printf("debugging flags now %x\n", cam_dflags);
3333 start_ccb->ccb_h.status = CAM_REQ_CMP;
3336 #else /* !CAMDEBUG */
3337 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3338 #endif /* CAMDEBUG */
3342 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3343 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3344 start_ccb->ccb_h.status = CAM_REQ_CMP;
3351 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3358 xpt_polled_action(union ccb *start_ccb)
3362 struct cam_sim *sim;
3363 struct cam_devq *devq;
3366 timeout = start_ccb->ccb_h.timeout;
3367 sim = start_ccb->ccb_h.path->bus->sim;
3369 dev = start_ccb->ccb_h.path->device;
3374 * Steal an opening so that no other queued requests
3375 * can get it before us while we simulate interrupts.
3377 dev->ccbq.devq_openings--;
3378 dev->ccbq.dev_openings--;
3380 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3381 && (--timeout > 0)) {
3383 (*(sim->sim_poll))(sim);
3388 dev->ccbq.devq_openings++;
3389 dev->ccbq.dev_openings++;
3392 xpt_action(start_ccb);
3393 while(--timeout > 0) {
3394 (*(sim->sim_poll))(sim);
3397 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3404 * XXX Is it worth adding a sim_timeout entry
3405 * point so we can attempt recovery? If
3406 * this is only used for dumps, I don't think
3409 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3412 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3418 * Schedule a peripheral driver to receive a ccb when it's
3419 * target device has space for more transactions.
3422 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3424 struct cam_ed *device;
3428 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3429 device = perph->path->device;
3431 if (periph_is_queued(perph)) {
3432 /* Simply reorder based on new priority */
3433 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3434 (" change priority to %d\n", new_priority));
3435 if (new_priority < perph->pinfo.priority) {
3436 camq_change_priority(&device->drvq,
3442 /* New entry on the queue */
3443 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3444 (" added periph to queue\n"));
3445 perph->pinfo.priority = new_priority;
3446 perph->pinfo.generation = ++device->drvq.generation;
3447 camq_insert(&device->drvq, &perph->pinfo);
3448 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3452 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3453 (" calling xpt_run_devq\n"));
3454 xpt_run_dev_allocq(perph->path->bus);
3460 * Schedule a device to run on a given queue.
3461 * If the device was inserted as a new entry on the queue,
3462 * return 1 meaning the device queue should be run. If we
3463 * were already queued, implying someone else has already
3464 * started the queue, return 0 so the caller doesn't attempt
3465 * to run the queue. Must be run at either splsoftcam
3466 * (or splcam since that encompases splsoftcam).
3469 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3470 u_int32_t new_priority)
3473 u_int32_t old_priority;
3475 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3477 old_priority = pinfo->priority;
3480 * Are we already queued?
3482 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3483 /* Simply reorder based on new priority */
3484 if (new_priority < old_priority) {
3485 camq_change_priority(queue, pinfo->index,
3487 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3488 ("changed priority to %d\n",
3493 /* New entry on the queue */
3494 if (new_priority < old_priority)
3495 pinfo->priority = new_priority;
3497 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3498 ("Inserting onto queue\n"));
3499 pinfo->generation = ++queue->generation;
3500 camq_insert(queue, pinfo);
3507 xpt_run_dev_allocq(struct cam_eb *bus)
3509 struct cam_devq *devq;
3512 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3513 devq = bus->sim->devq;
3515 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3516 (" qfrozen_cnt == 0x%x, entries == %d, "
3517 "openings == %d, active == %d\n",
3518 devq->alloc_queue.qfrozen_cnt,
3519 devq->alloc_queue.entries,
3520 devq->alloc_openings,
3521 devq->alloc_active));
3524 devq->alloc_queue.qfrozen_cnt++;
3525 while ((devq->alloc_queue.entries > 0)
3526 && (devq->alloc_openings > 0)
3527 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3528 struct cam_ed_qinfo *qinfo;
3529 struct cam_ed *device;
3530 union ccb *work_ccb;
3531 struct cam_periph *drv;
3534 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3536 device = qinfo->device;
3538 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3539 ("running device %p\n", device));
3541 drvq = &device->drvq;
3544 if (drvq->entries <= 0) {
3545 panic("xpt_run_dev_allocq: "
3546 "Device on queue without any work to do");
3549 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3550 devq->alloc_openings--;
3551 devq->alloc_active++;
3552 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3554 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3555 drv->pinfo.priority);
3556 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3557 ("calling periph start\n"));
3558 drv->periph_start(drv, work_ccb);
3561 * Malloc failure in alloc_ccb
3564 * XXX add us to a list to be run from free_ccb
3565 * if we don't have any ccbs active on this
3566 * device queue otherwise we may never get run
3572 /* Raise IPL for possible insertion and test at top of loop */
3575 if (drvq->entries > 0) {
3576 /* We have more work. Attempt to reschedule */
3577 xpt_schedule_dev_allocq(bus, device);
3580 devq->alloc_queue.qfrozen_cnt--;
3585 xpt_run_dev_sendq(struct cam_eb *bus)
3587 struct cam_devq *devq;
3590 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3592 devq = bus->sim->devq;
3595 devq->send_queue.qfrozen_cnt++;
3598 while ((devq->send_queue.entries > 0)
3599 && (devq->send_openings > 0)) {
3600 struct cam_ed_qinfo *qinfo;
3601 struct cam_ed *device;
3602 union ccb *work_ccb;
3603 struct cam_sim *sim;
3607 if (devq->send_queue.qfrozen_cnt > 1) {
3612 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3614 device = qinfo->device;
3617 * If the device has been "frozen", don't attempt
3620 if (device->qfrozen_cnt > 0) {
3625 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3626 ("running device %p\n", device));
3628 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3629 if (work_ccb == NULL) {
3630 printf("device on run queue with no ccbs???\n");
3635 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3637 if (num_highpower <= 0) {
3639 * We got a high power command, but we
3640 * don't have any available slots. Freeze
3641 * the device queue until we have a slot
3644 device->qfrozen_cnt++;
3645 STAILQ_INSERT_TAIL(&highpowerq,
3653 * Consume a high power slot while
3659 devq->active_dev = device;
3660 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3662 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3665 devq->send_openings--;
3666 devq->send_active++;
3668 if (device->ccbq.queue.entries > 0)
3669 xpt_schedule_dev_sendq(bus, device);
3671 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3673 * The client wants to freeze the queue
3674 * after this CCB is sent.
3677 device->qfrozen_cnt++;
3683 /* In Target mode, the peripheral driver knows best... */
3684 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3685 if ((device->inq_flags & SID_CmdQue) != 0
3686 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3687 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3690 * Clear this in case of a retried CCB that
3691 * failed due to a rejected tag.
3693 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3697 * Device queues can be shared among multiple sim instances
3698 * that reside on different busses. Use the SIM in the queue
3699 * CCB's path, rather than the one in the bus that was passed
3700 * into this function.
3702 sim = work_ccb->ccb_h.path->bus->sim;
3703 (*(sim->sim_action))(sim, work_ccb);
3706 devq->active_dev = NULL;
3708 /* Raise IPL for possible insertion and test at top of loop */
3713 devq->send_queue.qfrozen_cnt--;
3718 * This function merges stuff from the slave ccb into the master ccb, while
3719 * keeping important fields in the master ccb constant.
3722 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3725 * Pull fields that are valid for peripheral drivers to set
3726 * into the master CCB along with the CCB "payload".
3728 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3729 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3730 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3731 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3732 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3733 sizeof(union ccb) - sizeof(struct ccb_hdr));
3737 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3739 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3740 callout_init(&ccb_h->timeout_ch);
3741 ccb_h->pinfo.priority = priority;
3743 ccb_h->path_id = path->bus->path_id;
3745 ccb_h->target_id = path->target->target_id;
3747 ccb_h->target_id = CAM_TARGET_WILDCARD;
3749 ccb_h->target_lun = path->device->lun_id;
3750 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3752 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3754 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3758 /* Path manipulation functions */
3760 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3761 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3763 struct cam_path *path;
3766 path = malloc(sizeof(*path), M_DEVBUF, M_INTWAIT);
3767 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3768 if (status != CAM_REQ_CMP) {
3769 free(path, M_DEVBUF);
3772 *new_path_ptr = path;
3777 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3778 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3781 struct cam_et *target;
3782 struct cam_ed *device;
3786 status = CAM_REQ_CMP; /* Completed without error */
3787 target = NULL; /* Wildcarded */
3788 device = NULL; /* Wildcarded */
3791 * We will potentially modify the EDT, so block interrupts
3792 * that may attempt to create cam paths.
3795 bus = xpt_find_bus(path_id);
3797 status = CAM_PATH_INVALID;
3799 target = xpt_find_target(bus, target_id);
3800 if (target == NULL) {
3802 struct cam_et *new_target;
3804 new_target = xpt_alloc_target(bus, target_id);
3805 if (new_target == NULL) {
3806 status = CAM_RESRC_UNAVAIL;
3808 target = new_target;
3811 if (target != NULL) {
3812 device = xpt_find_device(target, lun_id);
3813 if (device == NULL) {
3815 struct cam_ed *new_device;
3817 new_device = xpt_alloc_device(bus,
3820 if (new_device == NULL) {
3821 status = CAM_RESRC_UNAVAIL;
3823 device = new_device;
3831 * Only touch the user's data if we are successful.
3833 if (status == CAM_REQ_CMP) {
3834 new_path->periph = perph;
3835 new_path->bus = bus;
3836 new_path->target = target;
3837 new_path->device = device;
3838 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3841 xpt_release_device(bus, target, device);
3843 xpt_release_target(bus, target);
3845 xpt_release_bus(bus);
3851 xpt_release_path(struct cam_path *path)
3853 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3854 if (path->device != NULL) {
3855 xpt_release_device(path->bus, path->target, path->device);
3856 path->device = NULL;
3858 if (path->target != NULL) {
3859 xpt_release_target(path->bus, path->target);
3860 path->target = NULL;
3862 if (path->bus != NULL) {
3863 xpt_release_bus(path->bus);
3869 xpt_free_path(struct cam_path *path)
3871 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3872 xpt_release_path(path);
3873 free(path, M_DEVBUF);
3878 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3879 * in path1, 2 for match with wildcards in path2.
3882 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3886 if (path1->bus != path2->bus) {
3887 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3889 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3894 if (path1->target != path2->target) {
3895 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3898 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3903 if (path1->device != path2->device) {
3904 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3907 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3916 xpt_print_path(struct cam_path *path)
3919 printf("(nopath): ");
3921 if (path->periph != NULL)
3922 printf("(%s%d:", path->periph->periph_name,
3923 path->periph->unit_number);
3925 printf("(noperiph:");
3927 if (path->bus != NULL)
3928 printf("%s%d:%d:", path->bus->sim->sim_name,
3929 path->bus->sim->unit_number,
3930 path->bus->sim->bus_id);
3934 if (path->target != NULL)
3935 printf("%d:", path->target->target_id);
3939 if (path->device != NULL)
3940 printf("%d): ", path->device->lun_id);
3947 xpt_path_path_id(struct cam_path *path)
3949 return(path->bus->path_id);
3953 xpt_path_target_id(struct cam_path *path)
3955 if (path->target != NULL)
3956 return (path->target->target_id);
3958 return (CAM_TARGET_WILDCARD);
3962 xpt_path_lun_id(struct cam_path *path)
3964 if (path->device != NULL)
3965 return (path->device->lun_id);
3967 return (CAM_LUN_WILDCARD);
3971 xpt_path_sim(struct cam_path *path)
3973 return (path->bus->sim);
3977 xpt_path_periph(struct cam_path *path)
3979 return (path->periph);
3983 * Release a CAM control block for the caller. Remit the cost of the structure
3984 * to the device referenced by the path. If the this device had no 'credits'
3985 * and peripheral drivers have registered async callbacks for this notification
3989 xpt_release_ccb(union ccb *free_ccb)
3992 struct cam_path *path;
3993 struct cam_ed *device;
3996 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3997 path = free_ccb->ccb_h.path;
3998 device = path->device;
4001 cam_ccbq_release_opening(&device->ccbq);
4002 if (xpt_ccb_count > xpt_max_ccbs) {
4003 xpt_free_ccb(free_ccb);
4006 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4008 bus->sim->devq->alloc_openings++;
4009 bus->sim->devq->alloc_active--;
4010 /* XXX Turn this into an inline function - xpt_run_device?? */
4011 if ((device_is_alloc_queued(device) == 0)
4012 && (device->drvq.entries > 0)) {
4013 xpt_schedule_dev_allocq(bus, device);
4016 if (dev_allocq_is_runnable(bus->sim->devq))
4017 xpt_run_dev_allocq(bus);
4020 /* Functions accessed by SIM drivers */
4023 * A sim structure, listing the SIM entry points and instance
4024 * identification info is passed to xpt_bus_register to hook the SIM
4025 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4026 * for this new bus and places it in the array of busses and assigns
4027 * it a path_id. The path_id may be influenced by "hard wiring"
4028 * information specified by the user. Once interrupt services are
4029 * availible, the bus will be probed.
4032 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4034 struct cam_eb *new_bus;
4035 struct cam_eb *old_bus;
4036 struct ccb_pathinq cpi;
4040 new_bus = malloc(sizeof(*new_bus), M_DEVBUF, M_INTWAIT);
4042 if (strcmp(sim->sim_name, "xpt") != 0) {
4044 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4047 TAILQ_INIT(&new_bus->et_entries);
4048 new_bus->path_id = sim->path_id;
4051 timevalclear(&new_bus->last_reset);
4053 new_bus->refcount = 1; /* Held until a bus_deregister event */
4054 new_bus->generation = 0;
4056 old_bus = TAILQ_FIRST(&xpt_busses);
4057 while (old_bus != NULL
4058 && old_bus->path_id < new_bus->path_id)
4059 old_bus = TAILQ_NEXT(old_bus, links);
4060 if (old_bus != NULL)
4061 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4063 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4067 /* Notify interested parties */
4068 if (sim->path_id != CAM_XPT_PATH_ID) {
4069 struct cam_path path;
4071 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4072 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4073 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4074 cpi.ccb_h.func_code = XPT_PATH_INQ;
4075 xpt_action((union ccb *)&cpi);
4076 xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi);
4077 xpt_release_path(&path);
4079 return (CAM_SUCCESS);
4083 * Deregister a bus. We must clean out all transactions pending on the bus.
4084 * This routine is typically called prior to cam_sim_free() (e.g. see
4085 * dev/usbmisc/umass/umass.c)
4088 xpt_bus_deregister(path_id_t pathid)
4090 struct cam_path bus_path;
4093 status = xpt_compile_path(&bus_path, NULL, pathid,
4094 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4095 if (status != CAM_REQ_CMP)
4099 * This should clear out all pending requests and timeouts, but
4100 * the ccb's may be queued to a software interrupt.
4102 * XXX AC_LOST_DEVICE does not precisely abort the pending requests,
4103 * and it really ought to.
4105 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4106 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4108 /* make sure all responses have been processed */
4112 /* Release the reference count held while registered. */
4113 xpt_release_bus(bus_path.bus);
4114 xpt_release_path(&bus_path);
4116 return (CAM_REQ_CMP);
4120 xptnextfreepathid(void)
4127 bus = TAILQ_FIRST(&xpt_busses);
4129 /* Find an unoccupied pathid */
4131 && bus->path_id <= pathid) {
4132 if (bus->path_id == pathid)
4134 bus = TAILQ_NEXT(bus, links);
4138 * Ensure that this pathid is not reserved for
4139 * a bus that may be registered in the future.
4141 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4143 /* Start the search over */
4150 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4154 char buf[32], *strval;
4156 pathid = CAM_XPT_PATH_ID;
4157 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4159 while ((i = resource_locate(i, "scbus")) != -1) {
4160 dunit = resource_query_unit(i);
4161 if (dunit < 0) /* unwired?! */
4163 if (resource_string_value("scbus", dunit, "at", &strval) != 0)
4165 if (strcmp(buf, strval) != 0)
4167 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4168 if (sim_bus == val) {
4172 } else if (sim_bus == 0) {
4173 /* Unspecified matches bus 0 */
4177 printf("Ambiguous scbus configuration for %s%d "
4178 "bus %d, cannot wire down. The kernel "
4179 "config entry for scbus%d should "
4180 "specify a controller bus.\n"
4181 "Scbus will be assigned dynamically.\n",
4182 sim_name, sim_unit, sim_bus, dunit);
4187 if (pathid == CAM_XPT_PATH_ID)
4188 pathid = xptnextfreepathid();
4193 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4196 struct cam_et *target, *next_target;
4197 struct cam_ed *device, *next_device;
4200 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4203 * Most async events come from a CAM interrupt context. In
4204 * a few cases, the error recovery code at the peripheral layer,
4205 * which may run from our SWI or a process context, may signal
4206 * deferred events with a call to xpt_async. Ensure async
4207 * notifications are serialized by blocking cam interrupts.
4213 if (async_code == AC_BUS_RESET) {
4214 /* Update our notion of when the last reset occurred */
4215 microuptime(&bus->last_reset);
4218 for (target = TAILQ_FIRST(&bus->et_entries);
4220 target = next_target) {
4222 next_target = TAILQ_NEXT(target, links);
4224 if (path->target != target
4225 && path->target->target_id != CAM_TARGET_WILDCARD
4226 && target->target_id != CAM_TARGET_WILDCARD)
4229 if (async_code == AC_SENT_BDR) {
4230 /* Update our notion of when the last reset occurred */
4231 microuptime(&path->target->last_reset);
4234 for (device = TAILQ_FIRST(&target->ed_entries);
4236 device = next_device) {
4238 next_device = TAILQ_NEXT(device, links);
4240 if (path->device != device
4241 && path->device->lun_id != CAM_LUN_WILDCARD
4242 && device->lun_id != CAM_LUN_WILDCARD)
4245 xpt_dev_async(async_code, bus, target,
4248 xpt_async_bcast(&device->asyncs, async_code,
4254 * If this wasn't a fully wildcarded async, tell all
4255 * clients that want all async events.
4257 if (bus != xpt_periph->path->bus)
4258 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4264 xpt_async_bcast(struct async_list *async_head,
4265 u_int32_t async_code,
4266 struct cam_path *path, void *async_arg)
4268 struct async_node *cur_entry;
4270 cur_entry = SLIST_FIRST(async_head);
4271 while (cur_entry != NULL) {
4272 struct async_node *next_entry;
4274 * Grab the next list entry before we call the current
4275 * entry's callback. This is because the callback function
4276 * can delete its async callback entry.
4278 next_entry = SLIST_NEXT(cur_entry, links);
4279 if ((cur_entry->event_enable & async_code) != 0)
4280 cur_entry->callback(cur_entry->callback_arg,
4283 cur_entry = next_entry;
4288 * Handle any per-device event notifications that require action by the XPT.
4291 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4292 struct cam_ed *device, void *async_arg)
4295 struct cam_path newpath;
4298 * We only need to handle events for real devices.
4300 if (target->target_id == CAM_TARGET_WILDCARD
4301 || device->lun_id == CAM_LUN_WILDCARD)
4305 * We need our own path with wildcards expanded to
4306 * handle certain types of events.
4308 if ((async_code == AC_SENT_BDR)
4309 || (async_code == AC_BUS_RESET)
4310 || (async_code == AC_INQ_CHANGED))
4311 status = xpt_compile_path(&newpath, NULL,
4316 status = CAM_REQ_CMP_ERR;
4318 if (status == CAM_REQ_CMP) {
4321 * Allow transfer negotiation to occur in a
4322 * tag free environment.
4324 if (async_code == AC_SENT_BDR
4325 || async_code == AC_BUS_RESET)
4326 xpt_toggle_tags(&newpath);
4328 if (async_code == AC_INQ_CHANGED) {
4330 * We've sent a start unit command, or
4331 * something similar to a device that
4332 * may have caused its inquiry data to
4333 * change. So we re-scan the device to
4334 * refresh the inquiry data for it.
4336 xpt_scan_lun(newpath.periph, &newpath,
4337 CAM_EXPECT_INQ_CHANGE, NULL);
4339 xpt_release_path(&newpath);
4340 } else if (async_code == AC_LOST_DEVICE) {
4342 * When we lose a device the device may be about to detach
4343 * the sim, we have to clear out all pending timeouts and
4344 * requests before that happens. XXX it would be nice if
4345 * we could abort the requests pertaining to the device.
4347 xpt_release_devq_timeout(device);
4348 if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
4349 device->flags |= CAM_DEV_UNCONFIGURED;
4350 xpt_release_device(bus, target, device);
4352 } else if (async_code == AC_TRANSFER_NEG) {
4353 struct ccb_trans_settings *settings;
4355 settings = (struct ccb_trans_settings *)async_arg;
4356 xpt_set_transfer_settings(settings, device,
4357 /*async_update*/TRUE);
4362 xpt_freeze_devq(struct cam_path *path, u_int count)
4365 struct ccb_hdr *ccbh;
4368 path->device->qfrozen_cnt += count;
4371 * Mark the last CCB in the queue as needing
4372 * to be requeued if the driver hasn't
4373 * changed it's state yet. This fixes a race
4374 * where a ccb is just about to be queued to
4375 * a controller driver when it's interrupt routine
4376 * freezes the queue. To completly close the
4377 * hole, controller drives must check to see
4378 * if a ccb's status is still CAM_REQ_INPROG
4379 * under spl protection just before they queue
4380 * the CCB. See ahc_action/ahc_freeze_devq for
4383 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4384 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4385 ccbh->status = CAM_REQUEUE_REQ;
4387 return (path->device->qfrozen_cnt);
4391 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4393 sim->devq->send_queue.qfrozen_cnt += count;
4394 if (sim->devq->active_dev != NULL) {
4395 struct ccb_hdr *ccbh;
4397 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4399 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4400 ccbh->status = CAM_REQUEUE_REQ;
4402 return (sim->devq->send_queue.qfrozen_cnt);
4406 * WARNING: most devices, especially USB/UMASS, may detach their sim early.
4407 * We ref-count the sim (and the bus only NULLs it out when the bus has been
4408 * freed, which is not the case here), but the device queue is also freed XXX
4409 * and we have to check that here.
4411 * XXX fixme: could we simply not null-out the device queue via
4415 xpt_release_devq_timeout(void *arg)
4417 struct cam_ed *device;
4419 device = (struct cam_ed *)arg;
4421 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4425 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4427 xpt_release_devq_device(path->device, count, run_queue);
4431 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4440 if (dev->qfrozen_cnt > 0) {
4442 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4443 dev->qfrozen_cnt -= count;
4444 if (dev->qfrozen_cnt == 0) {
4447 * No longer need to wait for a successful
4448 * command completion.
4450 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4453 * Remove any timeouts that might be scheduled
4454 * to release this queue.
4456 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4457 callout_stop(&dev->c_handle);
4458 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4462 * Now that we are unfrozen schedule the
4463 * device so any pending transactions are
4466 if ((dev->ccbq.queue.entries > 0)
4467 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4468 && (run_queue != 0)) {
4475 xpt_run_dev_sendq(dev->target->bus);
4480 xpt_release_simq(struct cam_sim *sim, int run_queue)
4485 sendq = &(sim->devq->send_queue);
4487 if (sendq->qfrozen_cnt > 0) {
4489 sendq->qfrozen_cnt--;
4490 if (sendq->qfrozen_cnt == 0) {
4494 * If there is a timeout scheduled to release this
4495 * sim queue, remove it. The queue frozen count is
4498 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4499 callout_stop(&sim->c_handle);
4500 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4502 bus = xpt_find_bus(sim->path_id);
4507 * Now that we are unfrozen run the send queue.
4509 xpt_run_dev_sendq(bus);
4511 xpt_release_bus(bus);
4519 xpt_done(union ccb *done_ccb)
4525 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4526 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4528 * Queue up the request for handling by our SWI handler
4529 * any of the "non-immediate" type of ccbs.
4531 switch (done_ccb->ccb_h.path->periph->type) {
4532 case CAM_PERIPH_BIO:
4533 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4535 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4538 case CAM_PERIPH_NET:
4539 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4541 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4554 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_INTWAIT);
4559 xpt_free_ccb(union ccb *free_ccb)
4561 free(free_ccb, M_DEVBUF);
4566 /* Private XPT functions */
4569 * Get a CAM control block for the caller. Charge the structure to the device
4570 * referenced by the path. If the this device has no 'credits' then the
4571 * device already has the maximum number of outstanding operations under way
4572 * and we return NULL. If we don't have sufficient resources to allocate more
4573 * ccbs, we also return NULL.
4576 xpt_get_ccb(struct cam_ed *device)
4582 if ((new_ccb = (union ccb *)ccb_freeq.slh_first) == NULL) {
4583 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_INTWAIT);
4584 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4588 cam_ccbq_take_opening(&device->ccbq);
4589 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4595 xpt_release_bus(struct cam_eb *bus)
4599 if (bus->refcount == 1) {
4600 KKASSERT(TAILQ_FIRST(&bus->et_entries) == NULL);
4601 TAILQ_REMOVE(&xpt_busses, bus, links);
4603 cam_sim_release(bus->sim, 0);
4607 KKASSERT(bus->refcount == 1);
4608 free(bus, M_DEVBUF);
4615 static struct cam_et *
4616 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4618 struct cam_et *target;
4619 struct cam_et *cur_target;
4621 target = malloc(sizeof(*target), M_DEVBUF, M_INTWAIT);
4623 TAILQ_INIT(&target->ed_entries);
4625 target->target_id = target_id;
4626 target->refcount = 1;
4627 target->generation = 0;
4628 timevalclear(&target->last_reset);
4630 * Hold a reference to our parent bus so it
4631 * will not go away before we do.
4635 /* Insertion sort into our bus's target list */
4636 cur_target = TAILQ_FIRST(&bus->et_entries);
4637 while (cur_target != NULL && cur_target->target_id < target_id)
4638 cur_target = TAILQ_NEXT(cur_target, links);
4640 if (cur_target != NULL) {
4641 TAILQ_INSERT_BEFORE(cur_target, target, links);
4643 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4650 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4653 if (target->refcount == 1) {
4654 KKASSERT(TAILQ_FIRST(&target->ed_entries) == NULL);
4655 TAILQ_REMOVE(&bus->et_entries, target, links);
4657 xpt_release_bus(bus);
4658 KKASSERT(target->refcount == 1);
4659 free(target, M_DEVBUF);
4666 static struct cam_ed *
4667 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4669 struct cam_ed *device;
4670 struct cam_devq *devq;
4673 /* Make space for us in the device queue on our bus */
4674 devq = bus->sim->devq;
4675 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4677 if (status != CAM_REQ_CMP) {
4680 device = malloc(sizeof(*device), M_DEVBUF, M_INTWAIT);
4683 if (device != NULL) {
4684 struct cam_ed *cur_device;
4686 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4687 device->alloc_ccb_entry.device = device;
4688 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4689 device->send_ccb_entry.device = device;
4690 device->target = target;
4691 device->lun_id = lun_id;
4692 /* Initialize our queues */
4693 if (camq_init(&device->drvq, 0) != 0) {
4694 free(device, M_DEVBUF);
4697 if (cam_ccbq_init(&device->ccbq,
4698 bus->sim->max_dev_openings) != 0) {
4699 camq_fini(&device->drvq);
4700 free(device, M_DEVBUF);
4703 SLIST_INIT(&device->asyncs);
4704 SLIST_INIT(&device->periphs);
4705 device->generation = 0;
4706 device->owner = NULL;
4708 * Take the default quirk entry until we have inquiry
4709 * data and can determine a better quirk to use.
4711 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4712 bzero(&device->inq_data, sizeof(device->inq_data));
4713 device->inq_flags = 0;
4714 device->queue_flags = 0;
4715 device->serial_num = NULL;
4716 device->serial_num_len = 0;
4717 device->qfrozen_cnt = 0;
4718 device->flags = CAM_DEV_UNCONFIGURED;
4719 device->tag_delay_count = 0;
4720 device->refcount = 1;
4721 callout_init(&device->c_handle);
4724 * Hold a reference to our parent target so it
4725 * will not go away before we do.
4730 * XXX should be limited by number of CCBs this bus can
4733 xpt_max_ccbs += device->ccbq.devq_openings;
4734 /* Insertion sort into our target's device list */
4735 cur_device = TAILQ_FIRST(&target->ed_entries);
4736 while (cur_device != NULL && cur_device->lun_id < lun_id)
4737 cur_device = TAILQ_NEXT(cur_device, links);
4738 if (cur_device != NULL) {
4739 TAILQ_INSERT_BEFORE(cur_device, device, links);
4741 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4743 target->generation++;
4749 xpt_reference_device(struct cam_ed *device)
4755 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4756 struct cam_ed *device)
4758 struct cam_devq *devq;
4761 if (device->refcount == 1) {
4762 KKASSERT(device->flags & CAM_DEV_UNCONFIGURED);
4764 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4765 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4766 panic("Removing device while still queued for ccbs");
4768 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4769 device->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4770 callout_stop(&device->c_handle);
4773 TAILQ_REMOVE(&target->ed_entries, device,links);
4774 target->generation++;
4775 xpt_max_ccbs -= device->ccbq.devq_openings;
4776 /* Release our slot in the devq */
4777 devq = bus->sim->devq;
4778 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4779 xpt_release_target(bus, target);
4780 KKASSERT(device->refcount == 1);
4781 free(device, M_DEVBUF);
4789 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4799 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4800 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4801 if (result == CAM_REQ_CMP && (diff < 0)) {
4802 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4804 /* Adjust the global limit */
4805 xpt_max_ccbs += diff;
4810 static struct cam_eb *
4811 xpt_find_bus(path_id_t path_id)
4815 for (bus = TAILQ_FIRST(&xpt_busses);
4817 bus = TAILQ_NEXT(bus, links)) {
4818 if (bus->path_id == path_id) {
4826 static struct cam_et *
4827 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4829 struct cam_et *target;
4831 for (target = TAILQ_FIRST(&bus->et_entries);
4833 target = TAILQ_NEXT(target, links)) {
4834 if (target->target_id == target_id) {
4842 static struct cam_ed *
4843 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4845 struct cam_ed *device;
4847 for (device = TAILQ_FIRST(&target->ed_entries);
4849 device = TAILQ_NEXT(device, links)) {
4850 if (device->lun_id == lun_id) {
4859 union ccb *request_ccb;
4860 struct ccb_pathinq *cpi;
4862 } xpt_scan_bus_info;
4865 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
4866 * As the scan progresses, xpt_scan_bus is used as the
4867 * callback on completion function.
4870 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
4872 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4873 ("xpt_scan_bus\n"));
4874 switch (request_ccb->ccb_h.func_code) {
4877 xpt_scan_bus_info *scan_info;
4878 union ccb *work_ccb;
4879 struct cam_path *path;
4884 /* Find out the characteristics of the bus */
4885 work_ccb = xpt_alloc_ccb();
4886 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
4887 request_ccb->ccb_h.pinfo.priority);
4888 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4889 xpt_action(work_ccb);
4890 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4891 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
4892 xpt_free_ccb(work_ccb);
4893 xpt_done(request_ccb);
4897 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
4899 * Can't scan the bus on an adapter that
4900 * cannot perform the initiator role.
4902 request_ccb->ccb_h.status = CAM_REQ_CMP;
4903 xpt_free_ccb(work_ccb);
4904 xpt_done(request_ccb);
4908 /* Save some state for use while we probe for devices */
4909 scan_info = (xpt_scan_bus_info *)
4910 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_INTWAIT);
4911 scan_info->request_ccb = request_ccb;
4912 scan_info->cpi = &work_ccb->cpi;
4914 /* Cache on our stack so we can work asynchronously */
4915 max_target = scan_info->cpi->max_target;
4916 initiator_id = scan_info->cpi->initiator_id;
4919 * Don't count the initiator if the
4920 * initiator is addressable.
4922 scan_info->pending_count = max_target + 1;
4923 if (initiator_id <= max_target)
4924 scan_info->pending_count--;
4926 for (i = 0; i <= max_target; i++) {
4928 if (i == initiator_id)
4931 status = xpt_create_path(&path, xpt_periph,
4932 request_ccb->ccb_h.path_id,
4934 if (status != CAM_REQ_CMP) {
4935 printf("xpt_scan_bus: xpt_create_path failed"
4936 " with status %#x, bus scan halted\n",
4940 work_ccb = xpt_alloc_ccb();
4941 xpt_setup_ccb(&work_ccb->ccb_h, path,
4942 request_ccb->ccb_h.pinfo.priority);
4943 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
4944 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
4945 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
4946 work_ccb->crcn.flags = request_ccb->crcn.flags;
4948 printf("xpt_scan_bus: probing %d:%d:%d\n",
4949 request_ccb->ccb_h.path_id, i, 0);
4951 xpt_action(work_ccb);
4957 xpt_scan_bus_info *scan_info;
4959 target_id_t target_id;
4962 /* Reuse the same CCB to query if a device was really found */
4963 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
4964 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
4965 request_ccb->ccb_h.pinfo.priority);
4966 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
4968 path_id = request_ccb->ccb_h.path_id;
4969 target_id = request_ccb->ccb_h.target_id;
4970 lun_id = request_ccb->ccb_h.target_lun;
4971 xpt_action(request_ccb);
4974 printf("xpt_scan_bus: got back probe from %d:%d:%d\n",
4975 path_id, target_id, lun_id);
4978 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
4979 struct cam_ed *device;
4980 struct cam_et *target;
4984 * If we already probed lun 0 successfully, or
4985 * we have additional configured luns on this
4986 * target that might have "gone away", go onto
4989 target = request_ccb->ccb_h.path->target;
4991 * We may touch devices that we don't
4992 * hold references too, so ensure they
4993 * don't disappear out from under us.
4994 * The target above is referenced by the
4995 * path in the request ccb.
4999 device = TAILQ_FIRST(&target->ed_entries);
5000 if (device != NULL) {
5001 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5002 if (device->lun_id == 0)
5003 device = TAILQ_NEXT(device, links);
5006 if ((lun_id != 0) || (device != NULL)) {
5007 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5011 struct cam_ed *device;
5013 device = request_ccb->ccb_h.path->device;
5015 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5016 /* Try the next lun */
5017 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5018 (device->quirk->quirks & CAM_QUIRK_HILUNS))
5023 xpt_free_path(request_ccb->ccb_h.path);
5026 if ((lun_id == request_ccb->ccb_h.target_lun)
5027 || lun_id > scan_info->cpi->max_lun) {
5030 xpt_free_ccb(request_ccb);
5031 scan_info->pending_count--;
5032 if (scan_info->pending_count == 0) {
5033 xpt_free_ccb((union ccb *)scan_info->cpi);
5034 request_ccb = scan_info->request_ccb;
5035 free(scan_info, M_TEMP);
5036 request_ccb->ccb_h.status = CAM_REQ_CMP;
5037 xpt_done(request_ccb);
5040 /* Try the next device */
5041 struct cam_path *path;
5044 path = request_ccb->ccb_h.path;
5045 status = xpt_create_path(&path, xpt_periph,
5046 path_id, target_id, lun_id);
5047 if (status != CAM_REQ_CMP) {
5048 printf("xpt_scan_bus: xpt_create_path failed "
5049 "with status %#x, halting LUN scan\n",
5051 xpt_free_ccb(request_ccb);
5052 scan_info->pending_count--;
5053 if (scan_info->pending_count == 0) {
5055 (union ccb *)scan_info->cpi);
5056 request_ccb = scan_info->request_ccb;
5057 free(scan_info, M_TEMP);
5058 request_ccb->ccb_h.status = CAM_REQ_CMP;
5059 xpt_done(request_ccb);
5063 xpt_setup_ccb(&request_ccb->ccb_h, path,
5064 request_ccb->ccb_h.pinfo.priority);
5065 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5066 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5067 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5068 request_ccb->crcn.flags =
5069 scan_info->request_ccb->crcn.flags;
5071 xpt_print_path(path);
5072 printf("xpt_scan bus probing\n");
5074 xpt_action(request_ccb);
5089 PROBE_TUR_FOR_NEGOTIATION
5093 PROBE_INQUIRY_CKSUM = 0x01,
5094 PROBE_SERIAL_CKSUM = 0x02,
5095 PROBE_NO_ANNOUNCE = 0x04
5099 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5100 probe_action action;
5101 union ccb saved_ccb;
5104 u_int8_t digest[16];
5108 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5109 cam_flags flags, union ccb *request_ccb)
5111 struct ccb_pathinq cpi;
5113 struct cam_path *new_path;
5114 struct cam_periph *old_periph;
5117 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5118 ("xpt_scan_lun\n"));
5120 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5121 cpi.ccb_h.func_code = XPT_PATH_INQ;
5122 xpt_action((union ccb *)&cpi);
5124 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5125 if (request_ccb != NULL) {
5126 request_ccb->ccb_h.status = cpi.ccb_h.status;
5127 xpt_done(request_ccb);
5132 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5134 * Can't scan the bus on an adapter that
5135 * cannot perform the initiator role.
5137 if (request_ccb != NULL) {
5138 request_ccb->ccb_h.status = CAM_REQ_CMP;
5139 xpt_done(request_ccb);
5144 if (request_ccb == NULL) {
5145 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_INTWAIT);
5146 new_path = malloc(sizeof(*new_path), M_TEMP, M_INTWAIT);
5147 status = xpt_compile_path(new_path, xpt_periph,
5149 path->target->target_id,
5150 path->device->lun_id);
5152 if (status != CAM_REQ_CMP) {
5153 xpt_print_path(path);
5154 printf("xpt_scan_lun: can't compile path, can't "
5156 free(request_ccb, M_TEMP);
5157 free(new_path, M_TEMP);
5160 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5161 request_ccb->ccb_h.cbfcnp = xptscandone;
5162 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5163 request_ccb->crcn.flags = flags;
5167 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5170 softc = (probe_softc *)old_periph->softc;
5171 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5174 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5175 probestart, "probe",
5177 request_ccb->ccb_h.path, NULL, 0,
5180 if (status != CAM_REQ_CMP) {
5181 xpt_print_path(path);
5182 printf("xpt_scan_lun: cam_alloc_periph returned an "
5183 "error, can't continue probe\n");
5184 request_ccb->ccb_h.status = status;
5185 xpt_done(request_ccb);
5192 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5194 xpt_release_path(done_ccb->ccb_h.path);
5195 free(done_ccb->ccb_h.path, M_TEMP);
5196 free(done_ccb, M_TEMP);
5200 proberegister(struct cam_periph *periph, void *arg)
5202 union ccb *request_ccb; /* CCB representing the probe request */
5205 request_ccb = (union ccb *)arg;
5206 if (periph == NULL) {
5207 printf("proberegister: periph was NULL!!\n");
5208 return(CAM_REQ_CMP_ERR);
5211 if (request_ccb == NULL) {
5212 printf("proberegister: no probe CCB, "
5213 "can't register device\n");
5214 return(CAM_REQ_CMP_ERR);
5217 softc = malloc(sizeof(*softc), M_TEMP, M_INTWAIT | M_ZERO);
5218 TAILQ_INIT(&softc->request_ccbs);
5219 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5222 periph->softc = softc;
5223 cam_periph_acquire(periph);
5225 * Ensure we've waited at least a bus settle
5226 * delay before attempting to probe the device.
5227 * For HBAs that don't do bus resets, this won't make a difference.
5229 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5231 probeschedule(periph);
5232 return(CAM_REQ_CMP);
5236 probeschedule(struct cam_periph *periph)
5238 struct ccb_pathinq cpi;
5242 softc = (probe_softc *)periph->softc;
5243 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5245 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5246 cpi.ccb_h.func_code = XPT_PATH_INQ;
5247 xpt_action((union ccb *)&cpi);
5250 * If a device has gone away and another device, or the same one,
5251 * is back in the same place, it should have a unit attention
5252 * condition pending. It will not report the unit attention in
5253 * response to an inquiry, which may leave invalid transfer
5254 * negotiations in effect. The TUR will reveal the unit attention
5255 * condition. Only send the TUR for lun 0, since some devices
5256 * will get confused by commands other than inquiry to non-existent
5257 * luns. If you think a device has gone away start your scan from
5258 * lun 0. This will insure that any bogus transfer settings are
5261 * If we haven't seen the device before and the controller supports
5262 * some kind of transfer negotiation, negotiate with the first
5263 * sent command if no bus reset was performed at startup. This
5264 * ensures that the device is not confused by transfer negotiation
5265 * settings left over by loader or BIOS action.
5267 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5268 && (ccb->ccb_h.target_lun == 0)) {
5269 softc->action = PROBE_TUR;
5270 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5271 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5272 proberequestdefaultnegotiation(periph);
5273 softc->action = PROBE_INQUIRY;
5275 softc->action = PROBE_INQUIRY;
5278 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5279 softc->flags |= PROBE_NO_ANNOUNCE;
5281 softc->flags &= ~PROBE_NO_ANNOUNCE;
5283 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5287 probestart(struct cam_periph *periph, union ccb *start_ccb)
5289 /* Probe the device that our peripheral driver points to */
5290 struct ccb_scsiio *csio;
5293 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5295 softc = (probe_softc *)periph->softc;
5296 csio = &start_ccb->csio;
5298 switch (softc->action) {
5300 case PROBE_TUR_FOR_NEGOTIATION:
5302 scsi_test_unit_ready(csio,
5311 case PROBE_FULL_INQUIRY:
5314 struct scsi_inquiry_data *inq_buf;
5316 inq_buf = &periph->path->device->inq_data;
5318 * If the device is currently configured, we calculate an
5319 * MD5 checksum of the inquiry data, and if the serial number
5320 * length is greater than 0, add the serial number data
5321 * into the checksum as well. Once the inquiry and the
5322 * serial number check finish, we attempt to figure out
5323 * whether we still have the same device.
5325 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5327 MD5Init(&softc->context);
5328 MD5Update(&softc->context, (unsigned char *)inq_buf,
5329 sizeof(struct scsi_inquiry_data));
5330 softc->flags |= PROBE_INQUIRY_CKSUM;
5331 if (periph->path->device->serial_num_len > 0) {
5332 MD5Update(&softc->context,
5333 periph->path->device->serial_num,
5334 periph->path->device->serial_num_len);
5335 softc->flags |= PROBE_SERIAL_CKSUM;
5337 MD5Final(softc->digest, &softc->context);
5340 if (softc->action == PROBE_INQUIRY)
5341 inquiry_len = SHORT_INQUIRY_LENGTH;
5343 inquiry_len = inq_buf->additional_length + 5;
5349 (u_int8_t *)inq_buf,
5354 /*timeout*/60 * 1000);
5357 case PROBE_MODE_SENSE:
5362 mode_buf_len = sizeof(struct scsi_mode_header_6)
5363 + sizeof(struct scsi_mode_blk_desc)
5364 + sizeof(struct scsi_control_page);
5365 mode_buf = malloc(mode_buf_len, M_TEMP, M_INTWAIT);
5366 scsi_mode_sense(csio,
5371 SMS_PAGE_CTRL_CURRENT,
5372 SMS_CONTROL_MODE_PAGE,
5379 case PROBE_SERIAL_NUM:
5381 struct scsi_vpd_unit_serial_number *serial_buf;
5382 struct cam_ed* device;
5385 device = periph->path->device;
5386 device->serial_num = NULL;
5387 device->serial_num_len = 0;
5389 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0) {
5390 serial_buf = malloc(sizeof(*serial_buf), M_TEMP,
5391 M_INTWAIT | M_ZERO);
5396 (u_int8_t *)serial_buf,
5397 sizeof(*serial_buf),
5399 SVPD_UNIT_SERIAL_NUMBER,
5401 /*timeout*/60 * 1000);
5405 * We'll have to do without, let our probedone
5406 * routine finish up for us.
5408 start_ccb->csio.data_ptr = NULL;
5409 probedone(periph, start_ccb);
5413 xpt_action(start_ccb);
5417 proberequestdefaultnegotiation(struct cam_periph *periph)
5419 struct ccb_trans_settings cts;
5421 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5422 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5423 cts.flags = CCB_TRANS_USER_SETTINGS;
5424 xpt_action((union ccb *)&cts);
5425 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5426 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5427 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5428 xpt_action((union ccb *)&cts);
5432 probedone(struct cam_periph *periph, union ccb *done_ccb)
5435 struct cam_path *path;
5438 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5440 softc = (probe_softc *)periph->softc;
5441 path = done_ccb->ccb_h.path;
5442 priority = done_ccb->ccb_h.pinfo.priority;
5444 switch (softc->action) {
5447 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5449 if (cam_periph_error(done_ccb, 0,
5450 SF_NO_PRINT, NULL) == ERESTART)
5452 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5453 /* Don't wedge the queue */
5454 xpt_release_devq(done_ccb->ccb_h.path,
5458 softc->action = PROBE_INQUIRY;
5459 xpt_release_ccb(done_ccb);
5460 xpt_schedule(periph, priority);
5464 case PROBE_FULL_INQUIRY:
5466 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5467 struct scsi_inquiry_data *inq_buf;
5468 u_int8_t periph_qual;
5470 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5471 inq_buf = &path->device->inq_data;
5473 periph_qual = SID_QUAL(inq_buf);
5475 switch(periph_qual) {
5476 case SID_QUAL_LU_CONNECTED:
5481 * We conservatively request only
5482 * SHORT_INQUIRY_LEN bytes of inquiry
5483 * information during our first try
5484 * at sending an INQUIRY. If the device
5485 * has more information to give,
5486 * perform a second request specifying
5487 * the amount of information the device
5488 * is willing to give.
5490 alen = inq_buf->additional_length;
5491 if (softc->action == PROBE_INQUIRY
5492 && alen > (SHORT_INQUIRY_LENGTH - 5)) {
5493 softc->action = PROBE_FULL_INQUIRY;
5494 xpt_release_ccb(done_ccb);
5495 xpt_schedule(periph, priority);
5499 xpt_find_quirk(path->device);
5501 if ((inq_buf->flags & SID_CmdQue) != 0)
5502 softc->action = PROBE_MODE_SENSE;
5504 softc->action = PROBE_SERIAL_NUM;
5506 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5507 xpt_reference_device(path->device);
5509 xpt_release_ccb(done_ccb);
5510 xpt_schedule(periph, priority);
5516 } else if (cam_periph_error(done_ccb, 0,
5517 done_ccb->ccb_h.target_lun > 0
5518 ? SF_RETRY_UA|SF_QUIET_IR
5520 &softc->saved_ccb) == ERESTART) {
5522 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5523 /* Don't wedge the queue */
5524 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5528 * If we get to this point, we got an error status back
5529 * from the inquiry and the error status doesn't require
5530 * automatically retrying the command. Therefore, the
5531 * inquiry failed. If we had inquiry information before
5532 * for this device, but this latest inquiry command failed,
5533 * the device has probably gone away. If this device isn't
5534 * already marked unconfigured, notify the peripheral
5535 * drivers that this device is no more.
5537 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5538 /* Send the async notification. */
5539 xpt_async(AC_LOST_DEVICE, path, NULL);
5542 xpt_release_ccb(done_ccb);
5545 case PROBE_MODE_SENSE:
5547 struct ccb_scsiio *csio;
5548 struct scsi_mode_header_6 *mode_hdr;
5550 csio = &done_ccb->csio;
5551 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5552 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5553 struct scsi_control_page *page;
5556 offset = ((u_int8_t *)&mode_hdr[1])
5557 + mode_hdr->blk_desc_len;
5558 page = (struct scsi_control_page *)offset;
5559 path->device->queue_flags = page->queue_flags;
5560 } else if (cam_periph_error(done_ccb, 0,
5561 SF_RETRY_UA|SF_NO_PRINT,
5562 &softc->saved_ccb) == ERESTART) {
5564 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5565 /* Don't wedge the queue */
5566 xpt_release_devq(done_ccb->ccb_h.path,
5567 /*count*/1, /*run_queue*/TRUE);
5569 xpt_release_ccb(done_ccb);
5570 free(mode_hdr, M_TEMP);
5571 softc->action = PROBE_SERIAL_NUM;
5572 xpt_schedule(periph, priority);
5575 case PROBE_SERIAL_NUM:
5577 struct ccb_scsiio *csio;
5578 struct scsi_vpd_unit_serial_number *serial_buf;
5585 csio = &done_ccb->csio;
5586 priority = done_ccb->ccb_h.pinfo.priority;
5588 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5590 /* Clean up from previous instance of this device */
5591 if (path->device->serial_num != NULL) {
5592 free(path->device->serial_num, M_DEVBUF);
5593 path->device->serial_num = NULL;
5594 path->device->serial_num_len = 0;
5597 if (serial_buf == NULL) {
5599 * Don't process the command as it was never sent
5601 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5602 && (serial_buf->length > 0)) {
5605 path->device->serial_num =
5606 malloc((serial_buf->length + 1),
5607 M_DEVBUF, M_INTWAIT);
5608 bcopy(serial_buf->serial_num,
5609 path->device->serial_num,
5610 serial_buf->length);
5611 path->device->serial_num_len = serial_buf->length;
5612 path->device->serial_num[serial_buf->length] = '\0';
5613 } else if (cam_periph_error(done_ccb, 0,
5614 SF_RETRY_UA|SF_NO_PRINT,
5615 &softc->saved_ccb) == ERESTART) {
5617 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5618 /* Don't wedge the queue */
5619 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5624 * Let's see if we have seen this device before.
5626 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5628 u_int8_t digest[16];
5633 (unsigned char *)&path->device->inq_data,
5634 sizeof(struct scsi_inquiry_data));
5637 MD5Update(&context, serial_buf->serial_num,
5638 serial_buf->length);
5640 MD5Final(digest, &context);
5641 if (bcmp(softc->digest, digest, 16) == 0)
5645 * XXX Do we need to do a TUR in order to ensure
5646 * that the device really hasn't changed???
5649 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5650 xpt_async(AC_LOST_DEVICE, path, NULL);
5652 if (serial_buf != NULL)
5653 free(serial_buf, M_TEMP);
5657 * Now that we have all the necessary
5658 * information to safely perform transfer
5659 * negotiations... Controllers don't perform
5660 * any negotiation or tagged queuing until
5661 * after the first XPT_SET_TRAN_SETTINGS ccb is
5662 * received. So, on a new device, just retreive
5663 * the user settings, and set them as the current
5664 * settings to set the device up.
5666 proberequestdefaultnegotiation(periph);
5667 xpt_release_ccb(done_ccb);
5670 * Perform a TUR to allow the controller to
5671 * perform any necessary transfer negotiation.
5673 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5674 xpt_schedule(periph, priority);
5677 xpt_release_ccb(done_ccb);
5680 case PROBE_TUR_FOR_NEGOTIATION:
5681 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5682 /* Don't wedge the queue */
5683 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5687 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5688 xpt_reference_device(path->device);
5690 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5691 /* Inform the XPT that a new device has been found */
5692 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5693 xpt_action(done_ccb);
5695 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5697 xpt_release_ccb(done_ccb);
5700 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5701 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5702 done_ccb->ccb_h.status = CAM_REQ_CMP;
5704 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5705 cam_periph_invalidate(periph);
5706 cam_periph_release(periph);
5708 probeschedule(periph);
5713 probecleanup(struct cam_periph *periph)
5715 free(periph->softc, M_TEMP);
5719 xpt_find_quirk(struct cam_ed *device)
5723 match = cam_quirkmatch((caddr_t)&device->inq_data,
5724 (caddr_t)xpt_quirk_table,
5725 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5726 sizeof(*xpt_quirk_table), scsi_inquiry_match);
5729 panic("xpt_find_quirk: device didn't match wildcard entry!!");
5731 device->quirk = (struct xpt_quirk_entry *)match;
5735 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
5738 struct cam_sim *sim;
5741 sim = cts->ccb_h.path->bus->sim;
5742 if (async_update == FALSE) {
5743 struct scsi_inquiry_data *inq_data;
5744 struct ccb_pathinq cpi;
5745 struct ccb_trans_settings cur_cts;
5747 if (device == NULL) {
5748 cts->ccb_h.status = CAM_PATH_INVALID;
5749 xpt_done((union ccb *)cts);
5754 * Perform sanity checking against what the
5755 * controller and device can do.
5757 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
5758 cpi.ccb_h.func_code = XPT_PATH_INQ;
5759 xpt_action((union ccb *)&cpi);
5760 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
5761 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5762 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
5763 xpt_action((union ccb *)&cur_cts);
5764 inq_data = &device->inq_data;
5766 /* Fill in any gaps in what the user gave us */
5767 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
5768 cts->sync_period = cur_cts.sync_period;
5769 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
5770 cts->sync_offset = cur_cts.sync_offset;
5771 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
5772 cts->bus_width = cur_cts.bus_width;
5773 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
5774 cts->flags &= ~CCB_TRANS_DISC_ENB;
5775 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
5777 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
5778 cts->flags &= ~CCB_TRANS_TAG_ENB;
5779 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
5782 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5783 && (inq_data->flags & SID_Sync) == 0)
5784 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
5785 || (cts->sync_offset == 0)
5786 || (cts->sync_period == 0)) {
5788 cts->sync_period = 0;
5789 cts->sync_offset = 0;
5790 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
5792 if ((inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
5793 && cts->sync_period <= 0x9) {
5795 * Don't allow DT transmission rates if the
5796 * device does not support it.
5798 cts->sync_period = 0xa;
5800 if ((inq_data->spi3data & SID_SPI_IUS) == 0
5801 && cts->sync_period <= 0x8) {
5803 * Don't allow PACE transmission rates
5804 * if the device does support packetized
5807 cts->sync_period = 0x9;
5811 switch (cts->bus_width) {
5812 case MSG_EXT_WDTR_BUS_32_BIT:
5813 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5814 || (inq_data->flags & SID_WBus32) != 0)
5815 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
5817 /* Fall Through to 16-bit */
5818 case MSG_EXT_WDTR_BUS_16_BIT:
5819 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5820 || (inq_data->flags & SID_WBus16) != 0)
5821 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
5822 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5825 /* Fall Through to 8-bit */
5826 default: /* New bus width?? */
5827 case MSG_EXT_WDTR_BUS_8_BIT:
5828 /* All targets can do this */
5829 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5833 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
5835 * Can't tag queue without disconnection.
5837 cts->flags &= ~CCB_TRANS_TAG_ENB;
5838 cts->valid |= CCB_TRANS_TQ_VALID;
5841 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
5842 || (inq_data->flags & SID_CmdQue) == 0
5843 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
5844 || (device->quirk->mintags == 0)) {
5846 * Can't tag on hardware that doesn't support,
5847 * doesn't have it enabled, or has broken tag support.
5849 cts->flags &= ~CCB_TRANS_TAG_ENB;
5854 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
5858 * If we are transitioning from tags to no-tags or
5859 * vice-versa, we need to carefully freeze and restart
5860 * the queue so that we don't overlap tagged and non-tagged
5861 * commands. We also temporarily stop tags if there is
5862 * a change in transfer negotiation settings to allow
5863 * "tag-less" negotiation.
5865 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5866 || (device->inq_flags & SID_CmdQue) != 0)
5867 device_tagenb = TRUE;
5869 device_tagenb = FALSE;
5871 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
5872 && device_tagenb == FALSE)
5873 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
5874 && device_tagenb == TRUE)) {
5876 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
5878 * Delay change to use tags until after a
5879 * few commands have gone to this device so
5880 * the controller has time to perform transfer
5881 * negotiations without tagged messages getting
5884 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
5885 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
5887 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
5889 device->inq_flags &= ~SID_CmdQue;
5890 xpt_dev_ccbq_resize(cts->ccb_h.path,
5891 sim->max_dev_openings);
5892 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5893 device->tag_delay_count = 0;
5898 if (async_update == FALSE) {
5900 * If we are currently performing tagged transactions to
5901 * this device and want to change its negotiation parameters,
5902 * go non-tagged for a bit to give the controller a chance to
5903 * negotiate unhampered by tag messages.
5905 if ((device->inq_flags & SID_CmdQue) != 0
5906 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
5907 CCB_TRANS_SYNC_OFFSET_VALID|
5908 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
5909 xpt_toggle_tags(cts->ccb_h.path);
5911 (*(sim->sim_action))(sim, (union ccb *)cts);
5915 struct ccb_relsim crs;
5917 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
5919 crs.ccb_h.func_code = XPT_REL_SIMQ;
5920 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5922 = crs.release_timeout
5925 xpt_action((union ccb *)&crs);
5930 xpt_toggle_tags(struct cam_path *path)
5935 * Give controllers a chance to renegotiate
5936 * before starting tag operations. We
5937 * "toggle" tagged queuing off then on
5938 * which causes the tag enable command delay
5939 * counter to come into effect.
5942 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5943 || ((dev->inq_flags & SID_CmdQue) != 0
5944 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
5945 struct ccb_trans_settings cts;
5947 xpt_setup_ccb(&cts.ccb_h, path, 1);
5949 cts.valid = CCB_TRANS_TQ_VALID;
5950 xpt_set_transfer_settings(&cts, path->device,
5951 /*async_update*/TRUE);
5952 cts.flags = CCB_TRANS_TAG_ENB;
5953 xpt_set_transfer_settings(&cts, path->device,
5954 /*async_update*/TRUE);
5959 xpt_start_tags(struct cam_path *path)
5961 struct ccb_relsim crs;
5962 struct cam_ed *device;
5963 struct cam_sim *sim;
5966 device = path->device;
5967 sim = path->bus->sim;
5968 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5969 xpt_freeze_devq(path, /*count*/1);
5970 device->inq_flags |= SID_CmdQue;
5971 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
5972 xpt_dev_ccbq_resize(path, newopenings);
5973 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
5974 crs.ccb_h.func_code = XPT_REL_SIMQ;
5975 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5977 = crs.release_timeout
5980 xpt_action((union ccb *)&crs);
5983 static int busses_to_config;
5984 static int busses_to_reset;
5987 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
5989 if (bus->path_id != CAM_XPT_PATH_ID) {
5990 struct cam_path path;
5991 struct ccb_pathinq cpi;
5995 xpt_compile_path(&path, NULL, bus->path_id,
5996 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5997 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
5998 cpi.ccb_h.func_code = XPT_PATH_INQ;
5999 xpt_action((union ccb *)&cpi);
6000 can_negotiate = cpi.hba_inquiry;
6001 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6002 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6005 xpt_release_path(&path);
6012 xptconfigfunc(struct cam_eb *bus, void *arg)
6014 struct cam_path *path;
6015 union ccb *work_ccb;
6017 if (bus->path_id != CAM_XPT_PATH_ID) {
6021 work_ccb = xpt_alloc_ccb();
6022 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6023 CAM_TARGET_WILDCARD,
6024 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6025 printf("xptconfigfunc: xpt_create_path failed with "
6026 "status %#x for bus %d\n", status, bus->path_id);
6027 printf("xptconfigfunc: halting bus configuration\n");
6028 xpt_free_ccb(work_ccb);
6030 xpt_finishconfig(xpt_periph, NULL);
6033 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6034 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6035 xpt_action(work_ccb);
6036 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6037 printf("xptconfigfunc: CPI failed on bus %d "
6038 "with status %d\n", bus->path_id,
6039 work_ccb->ccb_h.status);
6040 xpt_finishconfig(xpt_periph, work_ccb);
6044 can_negotiate = work_ccb->cpi.hba_inquiry;
6045 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6046 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6047 && (can_negotiate != 0)) {
6048 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6049 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6050 work_ccb->ccb_h.cbfcnp = NULL;
6051 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6052 ("Resetting Bus\n"));
6053 xpt_action(work_ccb);
6054 xpt_finishconfig(xpt_periph, work_ccb);
6056 /* Act as though we performed a successful BUS RESET */
6057 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6058 xpt_finishconfig(xpt_periph, work_ccb);
6066 xpt_config(void *arg)
6068 /* Now that interrupts are enabled, go find our devices */
6071 /* Setup debugging flags and path */
6072 #ifdef CAM_DEBUG_FLAGS
6073 cam_dflags = CAM_DEBUG_FLAGS;
6074 #else /* !CAM_DEBUG_FLAGS */
6075 cam_dflags = CAM_DEBUG_NONE;
6076 #endif /* CAM_DEBUG_FLAGS */
6077 #ifdef CAM_DEBUG_BUS
6078 if (cam_dflags != CAM_DEBUG_NONE) {
6079 if (xpt_create_path(&cam_dpath, xpt_periph,
6080 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6081 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6082 printf("xpt_config: xpt_create_path() failed for debug"
6083 " target %d:%d:%d, debugging disabled\n",
6084 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6085 cam_dflags = CAM_DEBUG_NONE;
6089 #else /* !CAM_DEBUG_BUS */
6091 #endif /* CAM_DEBUG_BUS */
6092 #endif /* CAMDEBUG */
6095 * Scan all installed busses.
6097 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6099 if (busses_to_config == 0) {
6100 /* Call manually because we don't have any busses */
6101 xpt_finishconfig(xpt_periph, NULL);
6103 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6104 printf("Waiting %d seconds for SCSI "
6105 "devices to settle\n", SCSI_DELAY/1000);
6107 xpt_for_all_busses(xptconfigfunc, NULL);
6112 * If the given device only has one peripheral attached to it, and if that
6113 * peripheral is the passthrough driver, announce it. This insures that the
6114 * user sees some sort of announcement for every peripheral in their system.
6117 xptpassannouncefunc(struct cam_ed *device, void *arg)
6119 struct cam_periph *periph;
6122 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6123 periph = SLIST_NEXT(periph, periph_links), i++);
6125 periph = SLIST_FIRST(&device->periphs);
6127 && (strncmp(periph->periph_name, "pass", 4) == 0))
6128 xpt_announce_periph(periph, NULL);
6134 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6136 struct periph_driver **p_drv;
6138 if (done_ccb != NULL) {
6139 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6140 ("xpt_finishconfig\n"));
6141 switch(done_ccb->ccb_h.func_code) {
6143 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6144 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6145 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6146 xpt_action(done_ccb);
6152 xpt_free_path(done_ccb->ccb_h.path);
6158 if (busses_to_config == 0) {
6159 /* Register all the peripheral drivers */
6160 /* XXX This will have to change when we have loadable modules */
6161 SET_FOREACH(p_drv, periphdriver_set) {
6166 * Check for devices with no "standard" peripheral driver
6167 * attached. For any devices like that, announce the
6168 * passthrough driver so the user will see something.
6170 xpt_for_all_devices(xptpassannouncefunc, NULL);
6172 /* Release our hook so that the boot can continue. */
6173 config_intrhook_disestablish(xpt_config_hook);
6174 free(xpt_config_hook, M_TEMP);
6175 xpt_config_hook = NULL;
6177 if (done_ccb != NULL)
6178 xpt_free_ccb(done_ccb);
6182 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6184 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6186 switch (work_ccb->ccb_h.func_code) {
6187 /* Common cases first */
6188 case XPT_PATH_INQ: /* Path routing inquiry */
6190 struct ccb_pathinq *cpi;
6192 cpi = &work_ccb->cpi;
6193 cpi->version_num = 1; /* XXX??? */
6194 cpi->hba_inquiry = 0;
6195 cpi->target_sprt = 0;
6197 cpi->hba_eng_cnt = 0;
6198 cpi->max_target = 0;
6200 cpi->initiator_id = 0;
6201 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6202 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6203 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6204 cpi->unit_number = sim->unit_number;
6205 cpi->bus_id = sim->bus_id;
6206 cpi->base_transfer_speed = 0;
6207 cpi->ccb_h.status = CAM_REQ_CMP;
6212 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6219 * The xpt as a "controller" has no interrupt sources, so polling
6223 xptpoll(struct cam_sim *sim)
6228 * Should only be called by the machine interrupt dispatch routines,
6229 * so put these prototypes here instead of in the header.
6233 swi_camnet(void *arg)
6239 swi_cambio(void *arg)
6245 camisr(cam_isrq_t *queue)
6248 struct ccb_hdr *ccb_h;
6251 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6254 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6255 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6258 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6263 if (ccb_h->flags & CAM_HIGH_POWER) {
6264 struct highpowerlist *hphead;
6265 struct cam_ed *device;
6266 union ccb *send_ccb;
6268 hphead = &highpowerq;
6270 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6273 * Increment the count since this command is done.
6278 * Any high powered commands queued up?
6280 if (send_ccb != NULL) {
6281 device = send_ccb->ccb_h.path->device;
6283 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6285 xpt_release_devq(send_ccb->ccb_h.path,
6286 /*count*/1, /*runqueue*/TRUE);
6289 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6292 dev = ccb_h->path->device;
6295 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6297 ccb_h->path->bus->sim->devq->send_active--;
6298 ccb_h->path->bus->sim->devq->send_openings++;
6301 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6302 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6303 && (dev->ccbq.dev_active == 0))) {
6305 xpt_release_devq(ccb_h->path, /*count*/1,
6309 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6310 && (--dev->tag_delay_count == 0))
6311 xpt_start_tags(ccb_h->path);
6313 if ((dev->ccbq.queue.entries > 0)
6314 && (dev->qfrozen_cnt == 0)
6315 && (device_is_send_queued(dev) == 0)) {
6316 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6321 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6322 xpt_release_simq(ccb_h->path->bus->sim,
6324 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6328 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6329 && (ccb_h->status & CAM_DEV_QFRZN)) {
6330 xpt_release_devq(ccb_h->path, /*count*/1,
6332 ccb_h->status &= ~CAM_DEV_QFRZN;
6334 xpt_run_dev_sendq(ccb_h->path->bus);
6337 /* Call the peripheral driver's callback */
6338 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6340 /* Raise IPL for while test */