2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
41 #include <sys/devicestat.h>
42 #include <sys/interrupt.h>
46 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_debug.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/scsi/scsi_pass.h>
65 /* Datastructures internal to the xpt layer */
68 * Definition of an async handler callback block. These are used to add
69 * SIMs and peripherals to the async callback lists.
72 SLIST_ENTRY(async_node) links;
73 u_int32_t event_enable; /* Async Event enables */
74 void (*callback)(void *arg, u_int32_t code,
75 struct cam_path *path, void *args);
79 SLIST_HEAD(async_list, async_node);
80 SLIST_HEAD(periph_list, cam_periph);
81 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
84 * This is the maximum number of high powered commands (e.g. start unit)
85 * that can be outstanding at a particular time.
87 #ifndef CAM_MAX_HIGHPOWER
88 #define CAM_MAX_HIGHPOWER 4
91 /* number of high powered commands that can go through right now */
92 static int num_highpower = CAM_MAX_HIGHPOWER;
95 * Structure for queueing a device in a run queue.
96 * There is one run queue for allocating new ccbs,
97 * and another for sending ccbs to the controller.
101 struct cam_ed *device;
105 * The CAM EDT (Existing Device Table) contains the device information for
106 * all devices for all busses in the system. The table contains a
107 * cam_ed structure for each device on the bus.
110 TAILQ_ENTRY(cam_ed) links;
111 struct cam_ed_qinfo alloc_ccb_entry;
112 struct cam_ed_qinfo send_ccb_entry;
113 struct cam_et *target;
116 * Queue of type drivers wanting to do
117 * work on this device.
119 struct cam_ccbq ccbq; /* Queue of pending ccbs */
120 struct async_list asyncs; /* Async callback info for this B/T/L */
121 struct periph_list periphs; /* All attached devices */
122 u_int generation; /* Generation number */
123 struct cam_periph *owner; /* Peripheral driver's ownership tag */
124 struct xpt_quirk_entry *quirk; /* Oddities about this device */
125 /* Storage for the inquiry data */
126 #ifdef CAM_NEW_TRAN_CODE
128 u_int protocol_version;
130 u_int transport_version;
131 #endif /* CAM_NEW_TRAN_CODE */
132 struct scsi_inquiry_data inq_data;
133 u_int8_t inq_flags; /*
134 * Current settings for inquiry flags.
135 * This allows us to override settings
136 * like disconnection and tagged
137 * queuing for a device.
139 u_int8_t queue_flags; /* Queue flags from the control page */
140 u_int8_t serial_num_len;
141 u_int8_t *serial_num;
142 u_int32_t qfrozen_cnt;
144 #define CAM_DEV_UNCONFIGURED 0x01
145 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
146 #define CAM_DEV_REL_ON_COMPLETE 0x04
147 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
148 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
149 #define CAM_DEV_TAG_AFTER_COUNT 0x20
150 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
151 u_int32_t tag_delay_count;
152 #define CAM_TAG_DELAY_COUNT 5
154 struct callout_handle c_handle;
158 * Each target is represented by an ET (Existing Target). These
159 * entries are created when a target is successfully probed with an
160 * identify, and removed when a device fails to respond after a number
161 * of retries, or a bus rescan finds the device missing.
164 TAILQ_HEAD(, cam_ed) ed_entries;
165 TAILQ_ENTRY(cam_et) links;
167 target_id_t target_id;
170 struct timeval last_reset;
174 * Each bus is represented by an EB (Existing Bus). These entries
175 * are created by calls to xpt_bus_register and deleted by calls to
176 * xpt_bus_deregister.
179 TAILQ_HEAD(, cam_et) et_entries;
180 TAILQ_ENTRY(cam_eb) links;
183 struct timeval last_reset;
185 #define CAM_EB_RUNQ_SCHEDULED 0x01
191 struct cam_periph *periph;
193 struct cam_et *target;
194 struct cam_ed *device;
197 struct xpt_quirk_entry {
198 struct scsi_inquiry_pattern inq_pat;
200 #define CAM_QUIRK_NOLUNS 0x01
201 #define CAM_QUIRK_NOSERIAL 0x02
202 #define CAM_QUIRK_HILUNS 0x04
206 #define CAM_SCSI2_MAXLUN 8
214 u_int32_t generation;
217 static const char quantum[] = "QUANTUM";
218 static const char sony[] = "SONY";
219 static const char west_digital[] = "WDIGTL";
220 static const char samsung[] = "SAMSUNG";
221 static const char seagate[] = "SEAGATE";
222 static const char microp[] = "MICROP";
224 static struct xpt_quirk_entry xpt_quirk_table[] =
227 /* Reports QUEUE FULL for temporary resource shortages */
228 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
229 /*quirks*/0, /*mintags*/24, /*maxtags*/32
232 /* Reports QUEUE FULL for temporary resource shortages */
233 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
234 /*quirks*/0, /*mintags*/24, /*maxtags*/32
237 /* Reports QUEUE FULL for temporary resource shortages */
238 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
239 /*quirks*/0, /*mintags*/24, /*maxtags*/32
242 /* Broken tagged queuing drive */
243 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
244 /*quirks*/0, /*mintags*/0, /*maxtags*/0
247 /* Broken tagged queuing drive */
248 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
249 /*quirks*/0, /*mintags*/0, /*maxtags*/0
252 /* Broken tagged queuing drive */
253 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
254 /*quirks*/0, /*mintags*/0, /*maxtags*/0
258 * Unfortunately, the Quantum Atlas III has the same
259 * problem as the Atlas II drives above.
260 * Reported by: "Johan Granlund" <johan@granlund.nu>
262 * For future reference, the drive with the problem was:
263 * QUANTUM QM39100TD-SW N1B0
265 * It's possible that Quantum will fix the problem in later
266 * firmware revisions. If that happens, the quirk entry
267 * will need to be made specific to the firmware revisions
271 /* Reports QUEUE FULL for temporary resource shortages */
272 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
273 /*quirks*/0, /*mintags*/24, /*maxtags*/32
277 * 18 Gig Atlas III, same problem as the 9G version.
278 * Reported by: Andre Albsmeier
279 * <andre.albsmeier@mchp.siemens.de>
281 * For future reference, the drive with the problem was:
282 * QUANTUM QM318000TD-S N491
284 /* Reports QUEUE FULL for temporary resource shortages */
285 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
286 /*quirks*/0, /*mintags*/24, /*maxtags*/32
290 * Broken tagged queuing drive
291 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
292 * and: Martin Renters <martin@tdc.on.ca>
294 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
295 /*quirks*/0, /*mintags*/0, /*maxtags*/0
298 * The Seagate Medalist Pro drives have very poor write
299 * performance with anything more than 2 tags.
301 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
302 * Drive: <SEAGATE ST36530N 1444>
304 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
305 * Drive: <SEAGATE ST34520W 1281>
307 * No one has actually reported that the 9G version
308 * (ST39140*) of the Medalist Pro has the same problem, but
309 * we're assuming that it does because the 4G and 6.5G
310 * versions of the drive are broken.
313 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
314 /*quirks*/0, /*mintags*/2, /*maxtags*/2
317 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
318 /*quirks*/0, /*mintags*/2, /*maxtags*/2
321 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
322 /*quirks*/0, /*mintags*/2, /*maxtags*/2
326 * Slow when tagged queueing is enabled. Write performance
327 * steadily drops off with more and more concurrent
328 * transactions. Best sequential write performance with
329 * tagged queueing turned off and write caching turned on.
332 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
333 * Drive: DCAS-34330 w/ "S65A" firmware.
335 * The drive with the problem had the "S65A" firmware
336 * revision, and has also been reported (by Stephen J.
337 * Roznowski <sjr@home.net>) for a drive with the "S61A"
340 * Although no one has reported problems with the 2 gig
341 * version of the DCAS drive, the assumption is that it
342 * has the same problems as the 4 gig version. Therefore
343 * this quirk entries disables tagged queueing for all
346 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
347 /*quirks*/0, /*mintags*/0, /*maxtags*/0
350 /* Broken tagged queuing drive */
351 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
352 /*quirks*/0, /*mintags*/0, /*maxtags*/0
355 /* Broken tagged queuing drive */
356 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
357 /*quirks*/0, /*mintags*/0, /*maxtags*/0
361 * Broken tagged queuing drive.
363 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
366 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
367 /*quirks*/0, /*mintags*/0, /*maxtags*/0
371 * Slow when tagged queueing is enabled. (1.5MB/sec versus
373 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
374 * Best performance with these drives is achieved with
375 * tagged queueing turned off, and write caching turned on.
377 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
378 /*quirks*/0, /*mintags*/0, /*maxtags*/0
382 * Slow when tagged queueing is enabled. (1.5MB/sec versus
384 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
385 * Best performance with these drives is achieved with
386 * tagged queueing turned off, and write caching turned on.
388 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
389 /*quirks*/0, /*mintags*/0, /*maxtags*/0
393 * Doesn't handle queue full condition correctly,
394 * so we need to limit maxtags to what the device
395 * can handle instead of determining this automatically.
397 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
398 /*quirks*/0, /*mintags*/2, /*maxtags*/32
401 /* Really only one LUN */
402 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA*", "*" },
403 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
406 /* I can't believe we need a quirk for DPT volumes. */
407 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
408 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
409 /*mintags*/0, /*maxtags*/255
413 * Many Sony CDROM drives don't like multi-LUN probing.
415 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
416 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
420 * This drive doesn't like multiple LUN probing.
421 * Submitted by: Parag Patel <parag@cgt.com>
423 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
424 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
427 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
428 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
432 * The 8200 doesn't like multi-lun probing, and probably
433 * don't like serial number requests either.
436 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
439 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
443 * These Hitachi drives don't like multi-lun probing.
444 * The PR submitter has a DK319H, but says that the Linux
445 * kernel has a similar work-around for the DK312 and DK314,
446 * so all DK31* drives are quirked here.
448 * Submitted by: Paul Haddad <paul@pth.com>
450 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
451 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
455 * This old revision of the TDC3600 is also SCSI-1, and
456 * hangs upon serial number probing.
459 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
462 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
466 * Would repond to all LUNs if asked for.
469 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
472 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
476 * Would repond to all LUNs if asked for.
479 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
482 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
485 /* Submitted by: Matthew Dodd <winter@jurai.net> */
486 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
487 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
490 /* Submitted by: Matthew Dodd <winter@jurai.net> */
491 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
492 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
495 /* TeraSolutions special settings for TRC-22 RAID */
496 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
497 /*quirks*/0, /*mintags*/55, /*maxtags*/255
501 * Would respond to all LUNs. Device type and removable
502 * flag are jumper-selectable.
504 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
507 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
510 /* Default tagged queuing parameters for all devices */
512 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
513 /*vendor*/"*", /*product*/"*", /*revision*/"*"
515 /*quirks*/0, /*mintags*/2, /*maxtags*/255
519 static const int xpt_quirk_table_size =
520 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
524 DM_RET_FLAG_MASK = 0x0f,
527 DM_RET_DESCEND = 0x20,
529 DM_RET_ACTION_MASK = 0xf0
537 } xpt_traverse_depth;
539 struct xpt_traverse_config {
540 xpt_traverse_depth depth;
545 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
546 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
547 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
548 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
549 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
551 /* Transport layer configuration information */
552 static struct xpt_softc xsoftc;
554 /* Queues for our software interrupt handler */
555 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
556 static cam_isrq_t cam_bioq;
557 static cam_isrq_t cam_netq;
559 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
560 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
561 static u_int xpt_max_ccbs; /*
562 * Maximum size of ccb pool. Modified as
563 * devices are added/removed or have their
564 * opening counts changed.
566 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
568 struct cam_periph *xpt_periph;
570 static periph_init_t xpt_periph_init;
572 static periph_init_t probe_periph_init;
574 static struct periph_driver xpt_driver =
576 xpt_periph_init, "xpt",
577 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
580 static struct periph_driver probe_driver =
582 probe_periph_init, "probe",
583 TAILQ_HEAD_INITIALIZER(probe_driver.units)
586 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
587 PERIPHDRIVER_DECLARE(probe, probe_driver);
589 #define XPT_CDEV_MAJOR 104
591 static d_open_t xptopen;
592 static d_close_t xptclose;
593 static d_ioctl_t xptioctl;
595 static struct cdevsw xpt_cdevsw = {
597 /* close */ xptclose,
600 /* ioctl */ xptioctl,
603 /* strategy */ nostrategy,
605 /* maj */ XPT_CDEV_MAJOR,
611 static struct intr_config_hook *xpt_config_hook;
613 /* Registered busses */
614 static TAILQ_HEAD(,cam_eb) xpt_busses;
615 static u_int bus_generation;
617 /* Storage for debugging datastructures */
619 struct cam_path *cam_dpath;
620 u_int32_t cam_dflags;
621 u_int32_t cam_debug_delay;
624 /* Pointers to software interrupt handlers */
628 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
629 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
633 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
634 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
635 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
637 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
638 || defined(CAM_DEBUG_LUN)
640 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
641 || !defined(CAM_DEBUG_LUN)
642 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
644 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
645 #else /* !CAMDEBUG */
646 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
647 #endif /* CAMDEBUG */
648 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
650 /* Our boot-time initialization hook */
651 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
653 static moduledata_t cam_moduledata = {
655 cam_module_event_handler,
659 static void xpt_init(void *);
661 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
662 MODULE_VERSION(cam, 1);
665 static cam_status xpt_compile_path(struct cam_path *new_path,
666 struct cam_periph *perph,
668 target_id_t target_id,
671 static void xpt_release_path(struct cam_path *path);
673 static void xpt_async_bcast(struct async_list *async_head,
674 u_int32_t async_code,
675 struct cam_path *path,
677 static void xpt_dev_async(u_int32_t async_code,
679 struct cam_et *target,
680 struct cam_ed *device,
682 static path_id_t xptnextfreepathid(void);
683 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
684 static union ccb *xpt_get_ccb(struct cam_ed *device);
685 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
686 u_int32_t new_priority);
687 static void xpt_run_dev_allocq(struct cam_eb *bus);
688 static void xpt_run_dev_sendq(struct cam_eb *bus);
689 static timeout_t xpt_release_devq_timeout;
690 static timeout_t xpt_release_simq_timeout;
691 static void xpt_release_bus(struct cam_eb *bus);
692 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
694 static struct cam_et*
695 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
696 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
697 static struct cam_ed*
698 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
700 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
701 struct cam_ed *device);
702 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
703 static struct cam_eb*
704 xpt_find_bus(path_id_t path_id);
705 static struct cam_et*
706 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
707 static struct cam_ed*
708 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
709 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
710 static void xpt_scan_lun(struct cam_periph *periph,
711 struct cam_path *path, cam_flags flags,
713 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
714 static xpt_busfunc_t xptconfigbuscountfunc;
715 static xpt_busfunc_t xptconfigfunc;
716 static void xpt_config(void *arg);
717 static xpt_devicefunc_t xptpassannouncefunc;
718 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
719 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
720 static void xptpoll(struct cam_sim *sim);
721 static void camisr(void *);
723 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
724 static void xptasync(struct cam_periph *periph,
725 u_int32_t code, cam_path *path);
727 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
728 u_int num_patterns, struct cam_eb *bus);
729 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
731 struct cam_ed *device);
732 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
734 struct cam_periph *periph);
735 static xpt_busfunc_t xptedtbusfunc;
736 static xpt_targetfunc_t xptedttargetfunc;
737 static xpt_devicefunc_t xptedtdevicefunc;
738 static xpt_periphfunc_t xptedtperiphfunc;
739 static xpt_pdrvfunc_t xptplistpdrvfunc;
740 static xpt_periphfunc_t xptplistperiphfunc;
741 static int xptedtmatch(struct ccb_dev_match *cdm);
742 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
743 static int xptbustraverse(struct cam_eb *start_bus,
744 xpt_busfunc_t *tr_func, void *arg);
745 static int xpttargettraverse(struct cam_eb *bus,
746 struct cam_et *start_target,
747 xpt_targetfunc_t *tr_func, void *arg);
748 static int xptdevicetraverse(struct cam_et *target,
749 struct cam_ed *start_device,
750 xpt_devicefunc_t *tr_func, void *arg);
751 static int xptperiphtraverse(struct cam_ed *device,
752 struct cam_periph *start_periph,
753 xpt_periphfunc_t *tr_func, void *arg);
754 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
755 xpt_pdrvfunc_t *tr_func, void *arg);
756 static int xptpdperiphtraverse(struct periph_driver **pdrv,
757 struct cam_periph *start_periph,
758 xpt_periphfunc_t *tr_func,
760 static xpt_busfunc_t xptdefbusfunc;
761 static xpt_targetfunc_t xptdeftargetfunc;
762 static xpt_devicefunc_t xptdefdevicefunc;
763 static xpt_periphfunc_t xptdefperiphfunc;
764 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
766 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
769 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
772 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
775 static xpt_devicefunc_t xptsetasyncfunc;
776 static xpt_busfunc_t xptsetasyncbusfunc;
777 static cam_status xptregister(struct cam_periph *periph,
779 static cam_status proberegister(struct cam_periph *periph,
781 static void probeschedule(struct cam_periph *probe_periph);
782 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
783 static void proberequestdefaultnegotiation(struct cam_periph *periph);
784 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
785 static void probecleanup(struct cam_periph *periph);
786 static void xpt_find_quirk(struct cam_ed *device);
787 #ifdef CAM_NEW_TRAN_CODE
788 static void xpt_devise_transport(struct cam_path *path);
789 #endif /* CAM_NEW_TRAN_CODE */
790 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
791 struct cam_ed *device,
793 static void xpt_toggle_tags(struct cam_path *path);
794 static void xpt_start_tags(struct cam_path *path);
795 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
797 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
799 static __inline int periph_is_queued(struct cam_periph *periph);
800 static __inline int device_is_alloc_queued(struct cam_ed *device);
801 static __inline int device_is_send_queued(struct cam_ed *device);
802 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
805 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
809 if (dev->ccbq.devq_openings > 0) {
810 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
811 cam_ccbq_resize(&dev->ccbq,
812 dev->ccbq.dev_openings
813 + dev->ccbq.dev_active);
814 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
817 * The priority of a device waiting for CCB resources
818 * is that of the the highest priority peripheral driver
821 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
822 &dev->alloc_ccb_entry.pinfo,
823 CAMQ_GET_HEAD(&dev->drvq)->priority);
832 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
836 if (dev->ccbq.dev_openings > 0) {
838 * The priority of a device waiting for controller
839 * resources is that of the the highest priority CCB
843 xpt_schedule_dev(&bus->sim->devq->send_queue,
844 &dev->send_ccb_entry.pinfo,
845 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
853 periph_is_queued(struct cam_periph *periph)
855 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
859 device_is_alloc_queued(struct cam_ed *device)
861 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
865 device_is_send_queued(struct cam_ed *device)
867 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
871 dev_allocq_is_runnable(struct cam_devq *devq)
875 * Have space to do more work.
876 * Allowed to do work.
878 return ((devq->alloc_queue.qfrozen_cnt == 0)
879 && (devq->alloc_queue.entries > 0)
880 && (devq->alloc_openings > 0));
886 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
896 xptdone(struct cam_periph *periph, union ccb *done_ccb)
898 /* Caller will release the CCB */
899 wakeup(&done_ccb->ccb_h.cbfcnp);
903 xptopen(dev_t dev, int flags, int fmt, struct proc *p)
907 unit = minor(dev) & 0xff;
910 * Only allow read-write access.
912 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
916 * We don't allow nonblocking access.
918 if ((flags & O_NONBLOCK) != 0) {
919 printf("xpt%d: can't do nonblocking accesss\n", unit);
924 * We only have one transport layer right now. If someone accesses
925 * us via something other than minor number 1, point out their
929 printf("xptopen: got invalid xpt unit %d\n", unit);
933 /* Mark ourselves open */
934 xsoftc.flags |= XPT_FLAG_OPEN;
940 xptclose(dev_t dev, int flag, int fmt, struct proc *p)
944 unit = minor(dev) & 0xff;
947 * We only have one transport layer right now. If someone accesses
948 * us via something other than minor number 1, point out their
952 printf("xptclose: got invalid xpt unit %d\n", unit);
956 /* Mark ourselves closed */
957 xsoftc.flags &= ~XPT_FLAG_OPEN;
963 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
968 unit = minor(dev) & 0xff;
971 * We only have one transport layer right now. If someone accesses
972 * us via something other than minor number 1, point out their
976 printf("xptioctl: got invalid xpt unit %d\n", unit);
982 * For the transport layer CAMIOCOMMAND ioctl, we really only want
983 * to accept CCB types that don't quite make sense to send through a
984 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
991 inccb = (union ccb *)addr;
993 switch(inccb->ccb_h.func_code) {
996 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
997 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1006 ccb = xpt_alloc_ccb();
1009 * Create a path using the bus, target, and lun the
1012 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1013 inccb->ccb_h.path_id,
1014 inccb->ccb_h.target_id,
1015 inccb->ccb_h.target_lun) !=
1021 /* Ensure all of our fields are correct */
1022 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1023 inccb->ccb_h.pinfo.priority);
1024 xpt_merge_ccb(ccb, inccb);
1025 ccb->ccb_h.cbfcnp = xptdone;
1026 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1027 bcopy(ccb, inccb, sizeof(union ccb));
1028 xpt_free_path(ccb->ccb_h.path);
1036 * This is an immediate CCB, so it's okay to
1037 * allocate it on the stack.
1041 * Create a path using the bus, target, and lun the
1044 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1045 inccb->ccb_h.path_id,
1046 inccb->ccb_h.target_id,
1047 inccb->ccb_h.target_lun) !=
1052 /* Ensure all of our fields are correct */
1053 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1054 inccb->ccb_h.pinfo.priority);
1055 xpt_merge_ccb(&ccb, inccb);
1056 ccb.ccb_h.cbfcnp = xptdone;
1058 bcopy(&ccb, inccb, sizeof(union ccb));
1059 xpt_free_path(ccb.ccb_h.path);
1063 case XPT_DEV_MATCH: {
1064 struct cam_periph_map_info mapinfo;
1065 struct cam_path *old_path;
1068 * We can't deal with physical addresses for this
1069 * type of transaction.
1071 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1077 * Save this in case the caller had it set to
1078 * something in particular.
1080 old_path = inccb->ccb_h.path;
1083 * We really don't need a path for the matching
1084 * code. The path is needed because of the
1085 * debugging statements in xpt_action(). They
1086 * assume that the CCB has a valid path.
1088 inccb->ccb_h.path = xpt_periph->path;
1090 bzero(&mapinfo, sizeof(mapinfo));
1093 * Map the pattern and match buffers into kernel
1094 * virtual address space.
1096 error = cam_periph_mapmem(inccb, &mapinfo);
1099 inccb->ccb_h.path = old_path;
1104 * This is an immediate CCB, we can send it on directly.
1109 * Map the buffers back into user space.
1111 cam_periph_unmapmem(inccb, &mapinfo);
1113 inccb->ccb_h.path = old_path;
1125 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1126 * with the periphal driver name and unit name filled in. The other
1127 * fields don't really matter as input. The passthrough driver name
1128 * ("pass"), and unit number are passed back in the ccb. The current
1129 * device generation number, and the index into the device peripheral
1130 * driver list, and the status are also passed back. Note that
1131 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1132 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1133 * (or rather should be) impossible for the device peripheral driver
1134 * list to change since we look at the whole thing in one pass, and
1135 * we do it with splcam protection.
1138 case CAMGETPASSTHRU: {
1140 struct cam_periph *periph;
1141 struct periph_driver **p_drv;
1144 u_int cur_generation;
1145 int base_periph_found;
1149 ccb = (union ccb *)addr;
1150 unit = ccb->cgdl.unit_number;
1151 name = ccb->cgdl.periph_name;
1153 * Every 100 devices, we want to drop our spl protection to
1154 * give the software interrupt handler a chance to run.
1155 * Most systems won't run into this check, but this should
1156 * avoid starvation in the software interrupt handler in
1161 ccb = (union ccb *)addr;
1163 base_periph_found = 0;
1166 * Sanity check -- make sure we don't get a null peripheral
1169 if (*ccb->cgdl.periph_name == '\0') {
1174 /* Keep the list from changing while we traverse it */
1177 cur_generation = xsoftc.generation;
1179 /* first find our driver in the list of drivers */
1180 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1181 if (strcmp((*p_drv)->driver_name, name) == 0)
1184 if (*p_drv == NULL) {
1186 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1187 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1188 *ccb->cgdl.periph_name = '\0';
1189 ccb->cgdl.unit_number = 0;
1195 * Run through every peripheral instance of this driver
1196 * and check to see whether it matches the unit passed
1197 * in by the user. If it does, get out of the loops and
1198 * find the passthrough driver associated with that
1199 * peripheral driver.
1201 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1202 periph = TAILQ_NEXT(periph, unit_links)) {
1204 if (periph->unit_number == unit) {
1206 } else if (--splbreaknum == 0) {
1210 if (cur_generation != xsoftc.generation)
1215 * If we found the peripheral driver that the user passed
1216 * in, go through all of the peripheral drivers for that
1217 * particular device and look for a passthrough driver.
1219 if (periph != NULL) {
1220 struct cam_ed *device;
1223 base_periph_found = 1;
1224 device = periph->path->device;
1225 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1227 periph = SLIST_NEXT(periph, periph_links), i++) {
1229 * Check to see whether we have a
1230 * passthrough device or not.
1232 if (strcmp(periph->periph_name, "pass") == 0) {
1234 * Fill in the getdevlist fields.
1236 strcpy(ccb->cgdl.periph_name,
1237 periph->periph_name);
1238 ccb->cgdl.unit_number =
1239 periph->unit_number;
1240 if (SLIST_NEXT(periph, periph_links))
1242 CAM_GDEVLIST_MORE_DEVS;
1245 CAM_GDEVLIST_LAST_DEVICE;
1246 ccb->cgdl.generation =
1248 ccb->cgdl.index = i;
1250 * Fill in some CCB header fields
1251 * that the user may want.
1253 ccb->ccb_h.path_id =
1254 periph->path->bus->path_id;
1255 ccb->ccb_h.target_id =
1256 periph->path->target->target_id;
1257 ccb->ccb_h.target_lun =
1258 periph->path->device->lun_id;
1259 ccb->ccb_h.status = CAM_REQ_CMP;
1266 * If the periph is null here, one of two things has
1267 * happened. The first possibility is that we couldn't
1268 * find the unit number of the particular peripheral driver
1269 * that the user is asking about. e.g. the user asks for
1270 * the passthrough driver for "da11". We find the list of
1271 * "da" peripherals all right, but there is no unit 11.
1272 * The other possibility is that we went through the list
1273 * of peripheral drivers attached to the device structure,
1274 * but didn't find one with the name "pass". Either way,
1275 * we return ENOENT, since we couldn't find something.
1277 if (periph == NULL) {
1278 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1279 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1280 *ccb->cgdl.periph_name = '\0';
1281 ccb->cgdl.unit_number = 0;
1284 * It is unfortunate that this is even necessary,
1285 * but there are many, many clueless users out there.
1286 * If this is true, the user is looking for the
1287 * passthrough driver, but doesn't have one in his
1290 if (base_periph_found == 1) {
1291 printf("xptioctl: pass driver is not in the "
1293 printf("xptioctl: put \"device pass0\" in "
1294 "your kernel config file\n");
1309 cam_module_event_handler(module_t mod, int what, void *arg)
1311 if (what == MOD_LOAD) {
1313 } else if (what == MOD_UNLOAD) {
1320 /* Functions accessed by the peripheral drivers */
1325 struct cam_sim *xpt_sim;
1326 struct cam_path *path;
1327 struct cam_devq *devq;
1330 TAILQ_INIT(&xpt_busses);
1331 TAILQ_INIT(&cam_bioq);
1332 TAILQ_INIT(&cam_netq);
1333 SLIST_INIT(&ccb_freeq);
1334 STAILQ_INIT(&highpowerq);
1337 * The xpt layer is, itself, the equivelent of a SIM.
1338 * Allow 16 ccbs in the ccb pool for it. This should
1339 * give decent parallelism when we probe busses and
1340 * perform other XPT functions.
1342 devq = cam_simq_alloc(16);
1343 xpt_sim = cam_sim_alloc(xptaction,
1348 /*max_dev_transactions*/0,
1349 /*max_tagged_dev_transactions*/0,
1353 xpt_bus_register(xpt_sim, /*bus #*/0);
1356 * Looking at the XPT from the SIM layer, the XPT is
1357 * the equivelent of a peripheral driver. Allocate
1358 * a peripheral driver entry for us.
1360 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1361 CAM_TARGET_WILDCARD,
1362 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1363 printf("xpt_init: xpt_create_path failed with status %#x,"
1364 " failing attach\n", status);
1368 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1369 path, NULL, 0, NULL);
1370 xpt_free_path(path);
1372 xpt_sim->softc = xpt_periph;
1375 * Register a callback for when interrupts are enabled.
1378 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1379 M_TEMP, M_NOWAIT | M_ZERO);
1380 if (xpt_config_hook == NULL) {
1381 printf("xpt_init: Cannot malloc config hook "
1382 "- failing attach\n");
1386 xpt_config_hook->ich_func = xpt_config;
1387 if (config_intrhook_establish(xpt_config_hook) != 0) {
1388 free (xpt_config_hook, M_TEMP);
1389 printf("xpt_init: config_intrhook_establish failed "
1390 "- failing attach\n");
1393 /* Install our software interrupt handlers */
1394 swi_add(NULL, "camnet", camisr, &cam_netq, SWI_CAMNET, 0, &camnet_ih);
1395 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1399 xptregister(struct cam_periph *periph, void *arg)
1401 if (periph == NULL) {
1402 printf("xptregister: periph was NULL!!\n");
1403 return(CAM_REQ_CMP_ERR);
1406 periph->softc = NULL;
1408 xpt_periph = periph;
1410 return(CAM_REQ_CMP);
1414 xpt_add_periph(struct cam_periph *periph)
1416 struct cam_ed *device;
1418 struct periph_list *periph_head;
1420 device = periph->path->device;
1422 periph_head = &device->periphs;
1424 status = CAM_REQ_CMP;
1426 if (device != NULL) {
1430 * Make room for this peripheral
1431 * so it will fit in the queue
1432 * when it's scheduled to run
1435 status = camq_resize(&device->drvq,
1436 device->drvq.array_size + 1);
1438 device->generation++;
1440 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1445 xsoftc.generation++;
1451 xpt_remove_periph(struct cam_periph *periph)
1453 struct cam_ed *device;
1455 device = periph->path->device;
1457 if (device != NULL) {
1459 struct periph_list *periph_head;
1461 periph_head = &device->periphs;
1463 /* Release the slot for this peripheral */
1465 camq_resize(&device->drvq, device->drvq.array_size - 1);
1467 device->generation++;
1469 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1474 xsoftc.generation++;
1478 #ifdef CAM_NEW_TRAN_CODE
1481 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1483 struct ccb_pathinq cpi;
1484 struct ccb_trans_settings cts;
1485 struct cam_path *path;
1491 path = periph->path;
1493 * To ensure that this is printed in one piece,
1494 * mask out CAM interrupts.
1497 printf("%s%d at %s%d bus %d target %d lun %d\n",
1498 periph->periph_name, periph->unit_number,
1499 path->bus->sim->sim_name,
1500 path->bus->sim->unit_number,
1501 path->bus->sim->bus_id,
1502 path->target->target_id,
1503 path->device->lun_id);
1504 printf("%s%d: ", periph->periph_name, periph->unit_number);
1505 scsi_print_inquiry(&path->device->inq_data);
1507 && (path->device->serial_num_len > 0)) {
1508 /* Don't wrap the screen - print only the first 60 chars */
1509 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1510 periph->unit_number, path->device->serial_num);
1512 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1513 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1514 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1515 xpt_action((union ccb*)&cts);
1517 /* Ask the SIM for its base transfer speed */
1518 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1519 cpi.ccb_h.func_code = XPT_PATH_INQ;
1520 xpt_action((union ccb *)&cpi);
1522 speed = cpi.base_transfer_speed;
1524 if (cts.ccb_h.status == CAM_REQ_CMP
1525 && cts.transport == XPORT_SPI) {
1526 struct ccb_trans_settings_spi *spi;
1528 spi = &cts.xport_specific.spi;
1529 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1530 && spi->sync_offset != 0) {
1531 freq = scsi_calc_syncsrate(spi->sync_period);
1535 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1536 speed *= (0x01 << spi->bus_width);
1541 printf("%s%d: %d.%03dMB/s transfers",
1542 periph->periph_name, periph->unit_number,
1545 printf("%s%d: %dKB/s transfers", periph->periph_name,
1546 periph->unit_number, speed);
1547 /* Report additional information about SPI connections */
1548 if (cts.ccb_h.status == CAM_REQ_CMP
1549 && cts.transport == XPORT_SPI) {
1550 struct ccb_trans_settings_spi *spi;
1552 spi = &cts.xport_specific.spi;
1554 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1556 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1560 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1561 && spi->bus_width > 0) {
1567 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1568 } else if (freq != 0) {
1573 if (path->device->inq_flags & SID_CmdQue
1574 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1575 printf("\n%s%d: Tagged Queueing Enabled",
1576 periph->periph_name, periph->unit_number);
1581 * We only want to print the caller's announce string if they've
1584 if (announce_string != NULL)
1585 printf("%s%d: %s\n", periph->periph_name,
1586 periph->unit_number, announce_string);
1589 #else /* CAM_NEW_TRAN_CODE */
1591 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1595 struct cam_path *path;
1596 struct ccb_trans_settings cts;
1598 path = periph->path;
1600 * To ensure that this is printed in one piece,
1601 * mask out CAM interrupts.
1604 printf("%s%d at %s%d bus %d target %d lun %d\n",
1605 periph->periph_name, periph->unit_number,
1606 path->bus->sim->sim_name,
1607 path->bus->sim->unit_number,
1608 path->bus->sim->bus_id,
1609 path->target->target_id,
1610 path->device->lun_id);
1611 printf("%s%d: ", periph->periph_name, periph->unit_number);
1612 scsi_print_inquiry(&path->device->inq_data);
1614 && (path->device->serial_num_len > 0)) {
1615 /* Don't wrap the screen - print only the first 60 chars */
1616 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1617 periph->unit_number, path->device->serial_num);
1619 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1620 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1621 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1622 xpt_action((union ccb*)&cts);
1623 if (cts.ccb_h.status == CAM_REQ_CMP) {
1627 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1628 && cts.sync_offset != 0) {
1629 freq = scsi_calc_syncsrate(cts.sync_period);
1632 struct ccb_pathinq cpi;
1634 /* Ask the SIM for its base transfer speed */
1635 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1636 cpi.ccb_h.func_code = XPT_PATH_INQ;
1637 xpt_action((union ccb *)&cpi);
1639 speed = cpi.base_transfer_speed;
1642 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1643 speed *= (0x01 << cts.bus_width);
1646 printf("%s%d: %d.%03dMB/s transfers",
1647 periph->periph_name, periph->unit_number,
1650 printf("%s%d: %dKB/s transfers", periph->periph_name,
1651 periph->unit_number, speed);
1652 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1653 && cts.sync_offset != 0) {
1654 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1655 freq % 1000, cts.sync_offset);
1657 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1658 && cts.bus_width > 0) {
1659 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1660 && cts.sync_offset != 0) {
1665 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1666 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1667 && cts.sync_offset != 0) {
1671 if (path->device->inq_flags & SID_CmdQue
1672 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1673 printf(", Tagged Queueing Enabled");
1677 } else if (path->device->inq_flags & SID_CmdQue
1678 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1679 printf("%s%d: Tagged Queueing Enabled\n",
1680 periph->periph_name, periph->unit_number);
1684 * We only want to print the caller's announce string if they've
1687 if (announce_string != NULL)
1688 printf("%s%d: %s\n", periph->periph_name,
1689 periph->unit_number, announce_string);
1693 #endif /* CAM_NEW_TRAN_CODE */
1695 static dev_match_ret
1696 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1699 dev_match_ret retval;
1702 retval = DM_RET_NONE;
1705 * If we aren't given something to match against, that's an error.
1708 return(DM_RET_ERROR);
1711 * If there are no match entries, then this bus matches no
1714 if ((patterns == NULL) || (num_patterns == 0))
1715 return(DM_RET_DESCEND | DM_RET_COPY);
1717 for (i = 0; i < num_patterns; i++) {
1718 struct bus_match_pattern *cur_pattern;
1721 * If the pattern in question isn't for a bus node, we
1722 * aren't interested. However, we do indicate to the
1723 * calling routine that we should continue descending the
1724 * tree, since the user wants to match against lower-level
1727 if (patterns[i].type != DEV_MATCH_BUS) {
1728 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1729 retval |= DM_RET_DESCEND;
1733 cur_pattern = &patterns[i].pattern.bus_pattern;
1736 * If they want to match any bus node, we give them any
1739 if (cur_pattern->flags == BUS_MATCH_ANY) {
1740 /* set the copy flag */
1741 retval |= DM_RET_COPY;
1744 * If we've already decided on an action, go ahead
1747 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1752 * Not sure why someone would do this...
1754 if (cur_pattern->flags == BUS_MATCH_NONE)
1757 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1758 && (cur_pattern->path_id != bus->path_id))
1761 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1762 && (cur_pattern->bus_id != bus->sim->bus_id))
1765 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1766 && (cur_pattern->unit_number != bus->sim->unit_number))
1769 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1770 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1775 * If we get to this point, the user definitely wants
1776 * information on this bus. So tell the caller to copy the
1779 retval |= DM_RET_COPY;
1782 * If the return action has been set to descend, then we
1783 * know that we've already seen a non-bus matching
1784 * expression, therefore we need to further descend the tree.
1785 * This won't change by continuing around the loop, so we
1786 * go ahead and return. If we haven't seen a non-bus
1787 * matching expression, we keep going around the loop until
1788 * we exhaust the matching expressions. We'll set the stop
1789 * flag once we fall out of the loop.
1791 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1796 * If the return action hasn't been set to descend yet, that means
1797 * we haven't seen anything other than bus matching patterns. So
1798 * tell the caller to stop descending the tree -- the user doesn't
1799 * want to match against lower level tree elements.
1801 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1802 retval |= DM_RET_STOP;
1807 static dev_match_ret
1808 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1809 struct cam_ed *device)
1811 dev_match_ret retval;
1814 retval = DM_RET_NONE;
1817 * If we aren't given something to match against, that's an error.
1820 return(DM_RET_ERROR);
1823 * If there are no match entries, then this device matches no
1826 if ((patterns == NULL) || (patterns == 0))
1827 return(DM_RET_DESCEND | DM_RET_COPY);
1829 for (i = 0; i < num_patterns; i++) {
1830 struct device_match_pattern *cur_pattern;
1833 * If the pattern in question isn't for a device node, we
1834 * aren't interested.
1836 if (patterns[i].type != DEV_MATCH_DEVICE) {
1837 if ((patterns[i].type == DEV_MATCH_PERIPH)
1838 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1839 retval |= DM_RET_DESCEND;
1843 cur_pattern = &patterns[i].pattern.device_pattern;
1846 * If they want to match any device node, we give them any
1849 if (cur_pattern->flags == DEV_MATCH_ANY) {
1850 /* set the copy flag */
1851 retval |= DM_RET_COPY;
1855 * If we've already decided on an action, go ahead
1858 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1863 * Not sure why someone would do this...
1865 if (cur_pattern->flags == DEV_MATCH_NONE)
1868 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1869 && (cur_pattern->path_id != device->target->bus->path_id))
1872 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1873 && (cur_pattern->target_id != device->target->target_id))
1876 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1877 && (cur_pattern->target_lun != device->lun_id))
1880 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1881 && (cam_quirkmatch((caddr_t)&device->inq_data,
1882 (caddr_t)&cur_pattern->inq_pat,
1883 1, sizeof(cur_pattern->inq_pat),
1884 scsi_static_inquiry_match) == NULL))
1888 * If we get to this point, the user definitely wants
1889 * information on this device. So tell the caller to copy
1892 retval |= DM_RET_COPY;
1895 * If the return action has been set to descend, then we
1896 * know that we've already seen a peripheral matching
1897 * expression, therefore we need to further descend the tree.
1898 * This won't change by continuing around the loop, so we
1899 * go ahead and return. If we haven't seen a peripheral
1900 * matching expression, we keep going around the loop until
1901 * we exhaust the matching expressions. We'll set the stop
1902 * flag once we fall out of the loop.
1904 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1909 * If the return action hasn't been set to descend yet, that means
1910 * we haven't seen any peripheral matching patterns. So tell the
1911 * caller to stop descending the tree -- the user doesn't want to
1912 * match against lower level tree elements.
1914 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1915 retval |= DM_RET_STOP;
1921 * Match a single peripheral against any number of match patterns.
1923 static dev_match_ret
1924 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1925 struct cam_periph *periph)
1927 dev_match_ret retval;
1931 * If we aren't given something to match against, that's an error.
1934 return(DM_RET_ERROR);
1937 * If there are no match entries, then this peripheral matches no
1940 if ((patterns == NULL) || (num_patterns == 0))
1941 return(DM_RET_STOP | DM_RET_COPY);
1944 * There aren't any nodes below a peripheral node, so there's no
1945 * reason to descend the tree any further.
1947 retval = DM_RET_STOP;
1949 for (i = 0; i < num_patterns; i++) {
1950 struct periph_match_pattern *cur_pattern;
1953 * If the pattern in question isn't for a peripheral, we
1954 * aren't interested.
1956 if (patterns[i].type != DEV_MATCH_PERIPH)
1959 cur_pattern = &patterns[i].pattern.periph_pattern;
1962 * If they want to match on anything, then we will do so.
1964 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1965 /* set the copy flag */
1966 retval |= DM_RET_COPY;
1969 * We've already set the return action to stop,
1970 * since there are no nodes below peripherals in
1977 * Not sure why someone would do this...
1979 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1982 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1983 && (cur_pattern->path_id != periph->path->bus->path_id))
1987 * For the target and lun id's, we have to make sure the
1988 * target and lun pointers aren't NULL. The xpt peripheral
1989 * has a wildcard target and device.
1991 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1992 && ((periph->path->target == NULL)
1993 ||(cur_pattern->target_id != periph->path->target->target_id)))
1996 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1997 && ((periph->path->device == NULL)
1998 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2001 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2002 && (cur_pattern->unit_number != periph->unit_number))
2005 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2006 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2011 * If we get to this point, the user definitely wants
2012 * information on this peripheral. So tell the caller to
2013 * copy the data out.
2015 retval |= DM_RET_COPY;
2018 * The return action has already been set to stop, since
2019 * peripherals don't have any nodes below them in the EDT.
2025 * If we get to this point, the peripheral that was passed in
2026 * doesn't match any of the patterns.
2032 xptedtbusfunc(struct cam_eb *bus, void *arg)
2034 struct ccb_dev_match *cdm;
2035 dev_match_ret retval;
2037 cdm = (struct ccb_dev_match *)arg;
2040 * If our position is for something deeper in the tree, that means
2041 * that we've already seen this node. So, we keep going down.
2043 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2044 && (cdm->pos.cookie.bus == bus)
2045 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2046 && (cdm->pos.cookie.target != NULL))
2047 retval = DM_RET_DESCEND;
2049 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2052 * If we got an error, bail out of the search.
2054 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2055 cdm->status = CAM_DEV_MATCH_ERROR;
2060 * If the copy flag is set, copy this bus out.
2062 if (retval & DM_RET_COPY) {
2065 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2066 sizeof(struct dev_match_result));
2069 * If we don't have enough space to put in another
2070 * match result, save our position and tell the
2071 * user there are more devices to check.
2073 if (spaceleft < sizeof(struct dev_match_result)) {
2074 bzero(&cdm->pos, sizeof(cdm->pos));
2075 cdm->pos.position_type =
2076 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2078 cdm->pos.cookie.bus = bus;
2079 cdm->pos.generations[CAM_BUS_GENERATION]=
2081 cdm->status = CAM_DEV_MATCH_MORE;
2084 j = cdm->num_matches;
2086 cdm->matches[j].type = DEV_MATCH_BUS;
2087 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2088 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2089 cdm->matches[j].result.bus_result.unit_number =
2090 bus->sim->unit_number;
2091 strncpy(cdm->matches[j].result.bus_result.dev_name,
2092 bus->sim->sim_name, DEV_IDLEN);
2096 * If the user is only interested in busses, there's no
2097 * reason to descend to the next level in the tree.
2099 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2103 * If there is a target generation recorded, check it to
2104 * make sure the target list hasn't changed.
2106 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2107 && (bus == cdm->pos.cookie.bus)
2108 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2109 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2110 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2112 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2116 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2117 && (cdm->pos.cookie.bus == bus)
2118 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2119 && (cdm->pos.cookie.target != NULL))
2120 return(xpttargettraverse(bus,
2121 (struct cam_et *)cdm->pos.cookie.target,
2122 xptedttargetfunc, arg));
2124 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2128 xptedttargetfunc(struct cam_et *target, void *arg)
2130 struct ccb_dev_match *cdm;
2132 cdm = (struct ccb_dev_match *)arg;
2135 * If there is a device list generation recorded, check it to
2136 * make sure the device list hasn't changed.
2138 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2139 && (cdm->pos.cookie.bus == target->bus)
2140 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2141 && (cdm->pos.cookie.target == target)
2142 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2143 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2144 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2145 target->generation)) {
2146 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2150 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2151 && (cdm->pos.cookie.bus == target->bus)
2152 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2153 && (cdm->pos.cookie.target == target)
2154 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2155 && (cdm->pos.cookie.device != NULL))
2156 return(xptdevicetraverse(target,
2157 (struct cam_ed *)cdm->pos.cookie.device,
2158 xptedtdevicefunc, arg));
2160 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2164 xptedtdevicefunc(struct cam_ed *device, void *arg)
2167 struct ccb_dev_match *cdm;
2168 dev_match_ret retval;
2170 cdm = (struct ccb_dev_match *)arg;
2173 * If our position is for something deeper in the tree, that means
2174 * that we've already seen this node. So, we keep going down.
2176 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2177 && (cdm->pos.cookie.device == device)
2178 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2179 && (cdm->pos.cookie.periph != NULL))
2180 retval = DM_RET_DESCEND;
2182 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2185 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2186 cdm->status = CAM_DEV_MATCH_ERROR;
2191 * If the copy flag is set, copy this device out.
2193 if (retval & DM_RET_COPY) {
2196 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2197 sizeof(struct dev_match_result));
2200 * If we don't have enough space to put in another
2201 * match result, save our position and tell the
2202 * user there are more devices to check.
2204 if (spaceleft < sizeof(struct dev_match_result)) {
2205 bzero(&cdm->pos, sizeof(cdm->pos));
2206 cdm->pos.position_type =
2207 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2208 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2210 cdm->pos.cookie.bus = device->target->bus;
2211 cdm->pos.generations[CAM_BUS_GENERATION]=
2213 cdm->pos.cookie.target = device->target;
2214 cdm->pos.generations[CAM_TARGET_GENERATION] =
2215 device->target->bus->generation;
2216 cdm->pos.cookie.device = device;
2217 cdm->pos.generations[CAM_DEV_GENERATION] =
2218 device->target->generation;
2219 cdm->status = CAM_DEV_MATCH_MORE;
2222 j = cdm->num_matches;
2224 cdm->matches[j].type = DEV_MATCH_DEVICE;
2225 cdm->matches[j].result.device_result.path_id =
2226 device->target->bus->path_id;
2227 cdm->matches[j].result.device_result.target_id =
2228 device->target->target_id;
2229 cdm->matches[j].result.device_result.target_lun =
2231 bcopy(&device->inq_data,
2232 &cdm->matches[j].result.device_result.inq_data,
2233 sizeof(struct scsi_inquiry_data));
2235 /* Let the user know whether this device is unconfigured */
2236 if (device->flags & CAM_DEV_UNCONFIGURED)
2237 cdm->matches[j].result.device_result.flags =
2238 DEV_RESULT_UNCONFIGURED;
2240 cdm->matches[j].result.device_result.flags =
2245 * If the user isn't interested in peripherals, don't descend
2246 * the tree any further.
2248 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2252 * If there is a peripheral list generation recorded, make sure
2253 * it hasn't changed.
2255 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2256 && (device->target->bus == cdm->pos.cookie.bus)
2257 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2258 && (device->target == cdm->pos.cookie.target)
2259 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2260 && (device == cdm->pos.cookie.device)
2261 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2262 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2263 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2264 device->generation)){
2265 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2269 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2270 && (cdm->pos.cookie.bus == device->target->bus)
2271 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2272 && (cdm->pos.cookie.target == device->target)
2273 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2274 && (cdm->pos.cookie.device == device)
2275 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2276 && (cdm->pos.cookie.periph != NULL))
2277 return(xptperiphtraverse(device,
2278 (struct cam_periph *)cdm->pos.cookie.periph,
2279 xptedtperiphfunc, arg));
2281 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2285 xptedtperiphfunc(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 bzero(&cdm->pos, sizeof(cdm->pos));
2315 cdm->pos.position_type =
2316 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2317 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2320 cdm->pos.cookie.bus = periph->path->bus;
2321 cdm->pos.generations[CAM_BUS_GENERATION]=
2323 cdm->pos.cookie.target = periph->path->target;
2324 cdm->pos.generations[CAM_TARGET_GENERATION] =
2325 periph->path->bus->generation;
2326 cdm->pos.cookie.device = periph->path->device;
2327 cdm->pos.generations[CAM_DEV_GENERATION] =
2328 periph->path->target->generation;
2329 cdm->pos.cookie.periph = periph;
2330 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2331 periph->path->device->generation;
2332 cdm->status = CAM_DEV_MATCH_MORE;
2336 j = cdm->num_matches;
2338 cdm->matches[j].type = DEV_MATCH_PERIPH;
2339 cdm->matches[j].result.periph_result.path_id =
2340 periph->path->bus->path_id;
2341 cdm->matches[j].result.periph_result.target_id =
2342 periph->path->target->target_id;
2343 cdm->matches[j].result.periph_result.target_lun =
2344 periph->path->device->lun_id;
2345 cdm->matches[j].result.periph_result.unit_number =
2346 periph->unit_number;
2347 strncpy(cdm->matches[j].result.periph_result.periph_name,
2348 periph->periph_name, DEV_IDLEN);
2355 xptedtmatch(struct ccb_dev_match *cdm)
2359 cdm->num_matches = 0;
2362 * Check the bus list generation. If it has changed, the user
2363 * needs to reset everything and start over.
2365 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2366 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2367 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2368 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2372 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2373 && (cdm->pos.cookie.bus != NULL))
2374 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2375 xptedtbusfunc, cdm);
2377 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2380 * If we get back 0, that means that we had to stop before fully
2381 * traversing the EDT. It also means that one of the subroutines
2382 * has set the status field to the proper value. If we get back 1,
2383 * we've fully traversed the EDT and copied out any matching entries.
2386 cdm->status = CAM_DEV_MATCH_LAST;
2392 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2394 struct ccb_dev_match *cdm;
2396 cdm = (struct ccb_dev_match *)arg;
2398 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2399 && (cdm->pos.cookie.pdrv == pdrv)
2400 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2401 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2402 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2403 (*pdrv)->generation)) {
2404 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2408 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2409 && (cdm->pos.cookie.pdrv == pdrv)
2410 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2411 && (cdm->pos.cookie.periph != NULL))
2412 return(xptpdperiphtraverse(pdrv,
2413 (struct cam_periph *)cdm->pos.cookie.periph,
2414 xptplistperiphfunc, arg));
2416 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2420 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2422 struct ccb_dev_match *cdm;
2423 dev_match_ret retval;
2425 cdm = (struct ccb_dev_match *)arg;
2427 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2429 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2430 cdm->status = CAM_DEV_MATCH_ERROR;
2435 * If the copy flag is set, copy this peripheral out.
2437 if (retval & DM_RET_COPY) {
2440 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2441 sizeof(struct dev_match_result));
2444 * If we don't have enough space to put in another
2445 * match result, save our position and tell the
2446 * user there are more devices to check.
2448 if (spaceleft < sizeof(struct dev_match_result)) {
2449 struct periph_driver **pdrv;
2452 bzero(&cdm->pos, sizeof(cdm->pos));
2453 cdm->pos.position_type =
2454 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2458 * This may look a bit non-sensical, but it is
2459 * actually quite logical. There are very few
2460 * peripheral drivers, and bloating every peripheral
2461 * structure with a pointer back to its parent
2462 * peripheral driver linker set entry would cost
2463 * more in the long run than doing this quick lookup.
2465 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2466 if (strcmp((*pdrv)->driver_name,
2467 periph->periph_name) == 0)
2472 cdm->status = CAM_DEV_MATCH_ERROR;
2476 cdm->pos.cookie.pdrv = pdrv;
2478 * The periph generation slot does double duty, as
2479 * does the periph pointer slot. They are used for
2480 * both edt and pdrv lookups and positioning.
2482 cdm->pos.cookie.periph = periph;
2483 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2484 (*pdrv)->generation;
2485 cdm->status = CAM_DEV_MATCH_MORE;
2489 j = cdm->num_matches;
2491 cdm->matches[j].type = DEV_MATCH_PERIPH;
2492 cdm->matches[j].result.periph_result.path_id =
2493 periph->path->bus->path_id;
2496 * The transport layer peripheral doesn't have a target or
2499 if (periph->path->target)
2500 cdm->matches[j].result.periph_result.target_id =
2501 periph->path->target->target_id;
2503 cdm->matches[j].result.periph_result.target_id = -1;
2505 if (periph->path->device)
2506 cdm->matches[j].result.periph_result.target_lun =
2507 periph->path->device->lun_id;
2509 cdm->matches[j].result.periph_result.target_lun = -1;
2511 cdm->matches[j].result.periph_result.unit_number =
2512 periph->unit_number;
2513 strncpy(cdm->matches[j].result.periph_result.periph_name,
2514 periph->periph_name, DEV_IDLEN);
2521 xptperiphlistmatch(struct ccb_dev_match *cdm)
2525 cdm->num_matches = 0;
2528 * At this point in the edt traversal function, we check the bus
2529 * list generation to make sure that no busses have been added or
2530 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2531 * For the peripheral driver list traversal function, however, we
2532 * don't have to worry about new peripheral driver types coming or
2533 * going; they're in a linker set, and therefore can't change
2534 * without a recompile.
2537 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2538 && (cdm->pos.cookie.pdrv != NULL))
2539 ret = xptpdrvtraverse(
2540 (struct periph_driver **)cdm->pos.cookie.pdrv,
2541 xptplistpdrvfunc, cdm);
2543 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2546 * If we get back 0, that means that we had to stop before fully
2547 * traversing the peripheral driver tree. It also means that one of
2548 * the subroutines has set the status field to the proper value. If
2549 * we get back 1, we've fully traversed the EDT and copied out any
2553 cdm->status = CAM_DEV_MATCH_LAST;
2559 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2561 struct cam_eb *bus, *next_bus;
2566 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2569 next_bus = TAILQ_NEXT(bus, links);
2571 retval = tr_func(bus, arg);
2580 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2581 xpt_targetfunc_t *tr_func, void *arg)
2583 struct cam_et *target, *next_target;
2587 for (target = (start_target ? start_target :
2588 TAILQ_FIRST(&bus->et_entries));
2589 target != NULL; target = next_target) {
2591 next_target = TAILQ_NEXT(target, links);
2593 retval = tr_func(target, arg);
2603 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2604 xpt_devicefunc_t *tr_func, void *arg)
2606 struct cam_ed *device, *next_device;
2610 for (device = (start_device ? start_device :
2611 TAILQ_FIRST(&target->ed_entries));
2613 device = next_device) {
2615 next_device = TAILQ_NEXT(device, links);
2617 retval = tr_func(device, arg);
2627 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2628 xpt_periphfunc_t *tr_func, void *arg)
2630 struct cam_periph *periph, *next_periph;
2635 for (periph = (start_periph ? start_periph :
2636 SLIST_FIRST(&device->periphs));
2638 periph = next_periph) {
2640 next_periph = SLIST_NEXT(periph, periph_links);
2642 retval = tr_func(periph, arg);
2651 xptpdrvtraverse(struct periph_driver **start_pdrv,
2652 xpt_pdrvfunc_t *tr_func, void *arg)
2654 struct periph_driver **pdrv;
2660 * We don't traverse the peripheral driver list like we do the
2661 * other lists, because it is a linker set, and therefore cannot be
2662 * changed during runtime. If the peripheral driver list is ever
2663 * re-done to be something other than a linker set (i.e. it can
2664 * change while the system is running), the list traversal should
2665 * be modified to work like the other traversal functions.
2667 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2668 *pdrv != NULL; pdrv++) {
2669 retval = tr_func(pdrv, arg);
2679 xptpdperiphtraverse(struct periph_driver **pdrv,
2680 struct cam_periph *start_periph,
2681 xpt_periphfunc_t *tr_func, void *arg)
2683 struct cam_periph *periph, *next_periph;
2688 for (periph = (start_periph ? start_periph :
2689 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2690 periph = next_periph) {
2692 next_periph = TAILQ_NEXT(periph, unit_links);
2694 retval = tr_func(periph, arg);
2702 xptdefbusfunc(struct cam_eb *bus, void *arg)
2704 struct xpt_traverse_config *tr_config;
2706 tr_config = (struct xpt_traverse_config *)arg;
2708 if (tr_config->depth == XPT_DEPTH_BUS) {
2709 xpt_busfunc_t *tr_func;
2711 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2713 return(tr_func(bus, tr_config->tr_arg));
2715 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2719 xptdeftargetfunc(struct cam_et *target, void *arg)
2721 struct xpt_traverse_config *tr_config;
2723 tr_config = (struct xpt_traverse_config *)arg;
2725 if (tr_config->depth == XPT_DEPTH_TARGET) {
2726 xpt_targetfunc_t *tr_func;
2728 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2730 return(tr_func(target, tr_config->tr_arg));
2732 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2736 xptdefdevicefunc(struct cam_ed *device, void *arg)
2738 struct xpt_traverse_config *tr_config;
2740 tr_config = (struct xpt_traverse_config *)arg;
2742 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2743 xpt_devicefunc_t *tr_func;
2745 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2747 return(tr_func(device, tr_config->tr_arg));
2749 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2753 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2755 struct xpt_traverse_config *tr_config;
2756 xpt_periphfunc_t *tr_func;
2758 tr_config = (struct xpt_traverse_config *)arg;
2760 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2763 * Unlike the other default functions, we don't check for depth
2764 * here. The peripheral driver level is the last level in the EDT,
2765 * so if we're here, we should execute the function in question.
2767 return(tr_func(periph, tr_config->tr_arg));
2771 * Execute the given function for every bus in the EDT.
2774 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2776 struct xpt_traverse_config tr_config;
2778 tr_config.depth = XPT_DEPTH_BUS;
2779 tr_config.tr_func = tr_func;
2780 tr_config.tr_arg = arg;
2782 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2787 * Execute the given function for every target in the EDT.
2790 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2792 struct xpt_traverse_config tr_config;
2794 tr_config.depth = XPT_DEPTH_TARGET;
2795 tr_config.tr_func = tr_func;
2796 tr_config.tr_arg = arg;
2798 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2800 #endif /* notusedyet */
2803 * Execute the given function for every device in the EDT.
2806 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2808 struct xpt_traverse_config tr_config;
2810 tr_config.depth = XPT_DEPTH_DEVICE;
2811 tr_config.tr_func = tr_func;
2812 tr_config.tr_arg = arg;
2814 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2819 * Execute the given function for every peripheral in the EDT.
2822 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2824 struct xpt_traverse_config tr_config;
2826 tr_config.depth = XPT_DEPTH_PERIPH;
2827 tr_config.tr_func = tr_func;
2828 tr_config.tr_arg = arg;
2830 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2832 #endif /* notusedyet */
2835 xptsetasyncfunc(struct cam_ed *device, void *arg)
2837 struct cam_path path;
2838 struct ccb_getdev cgd;
2839 struct async_node *cur_entry;
2841 cur_entry = (struct async_node *)arg;
2844 * Don't report unconfigured devices (Wildcard devs,
2845 * devices only for target mode, device instances
2846 * that have been invalidated but are waiting for
2847 * their last reference count to be released).
2849 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2852 xpt_compile_path(&path,
2854 device->target->bus->path_id,
2855 device->target->target_id,
2857 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2858 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2859 xpt_action((union ccb *)&cgd);
2860 cur_entry->callback(cur_entry->callback_arg,
2863 xpt_release_path(&path);
2869 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2871 struct cam_path path;
2872 struct ccb_pathinq cpi;
2873 struct async_node *cur_entry;
2875 cur_entry = (struct async_node *)arg;
2877 xpt_compile_path(&path, /*periph*/NULL,
2879 CAM_TARGET_WILDCARD,
2881 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2882 cpi.ccb_h.func_code = XPT_PATH_INQ;
2883 xpt_action((union ccb *)&cpi);
2884 cur_entry->callback(cur_entry->callback_arg,
2887 xpt_release_path(&path);
2893 xpt_action(union ccb *start_ccb)
2897 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2899 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2901 iopl = splsoftcam();
2902 switch (start_ccb->ccb_h.func_code) {
2905 #ifdef CAM_NEW_TRAN_CODE
2906 struct cam_ed *device;
2907 #endif /* CAM_NEW_TRAN_CODE */
2909 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2910 struct cam_path *path;
2912 path = start_ccb->ccb_h.path;
2916 * For the sake of compatibility with SCSI-1
2917 * devices that may not understand the identify
2918 * message, we include lun information in the
2919 * second byte of all commands. SCSI-1 specifies
2920 * that luns are a 3 bit value and reserves only 3
2921 * bits for lun information in the CDB. Later
2922 * revisions of the SCSI spec allow for more than 8
2923 * luns, but have deprecated lun information in the
2924 * CDB. So, if the lun won't fit, we must omit.
2926 * Also be aware that during initial probing for devices,
2927 * the inquiry information is unknown but initialized to 0.
2928 * This means that this code will be exercised while probing
2929 * devices with an ANSI revision greater than 2.
2931 #ifdef CAM_NEW_TRAN_CODE
2932 device = start_ccb->ccb_h.path->device;
2933 if (device->protocol_version <= SCSI_REV_2
2934 #else /* CAM_NEW_TRAN_CODE */
2935 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2936 #endif /* CAM_NEW_TRAN_CODE */
2937 && start_ccb->ccb_h.target_lun < 8
2938 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2940 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2941 start_ccb->ccb_h.target_lun << 5;
2943 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2944 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2945 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2946 &path->device->inq_data),
2947 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2948 cdb_str, sizeof(cdb_str))));
2952 case XPT_CONT_TARGET_IO:
2953 start_ccb->csio.sense_resid = 0;
2954 start_ccb->csio.resid = 0;
2959 struct cam_path *path;
2963 path = start_ccb->ccb_h.path;
2966 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2967 if (path->device->qfrozen_cnt == 0)
2968 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2973 xpt_run_dev_sendq(path->bus);
2976 case XPT_SET_TRAN_SETTINGS:
2978 xpt_set_transfer_settings(&start_ccb->cts,
2979 start_ccb->ccb_h.path->device,
2980 /*async_update*/FALSE);
2983 case XPT_CALC_GEOMETRY:
2985 struct cam_sim *sim;
2987 /* Filter out garbage */
2988 if (start_ccb->ccg.block_size == 0
2989 || start_ccb->ccg.volume_size == 0) {
2990 start_ccb->ccg.cylinders = 0;
2991 start_ccb->ccg.heads = 0;
2992 start_ccb->ccg.secs_per_track = 0;
2993 start_ccb->ccb_h.status = CAM_REQ_CMP;
2998 * In a PC-98 system, geometry translation depens on
2999 * the "real" device geometry obtained from mode page 4.
3000 * SCSI geometry translation is performed in the
3001 * initialization routine of the SCSI BIOS and the result
3002 * stored in host memory. If the translation is available
3003 * in host memory, use it. If not, rely on the default
3004 * translation the device driver performs.
3006 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3007 start_ccb->ccb_h.status = CAM_REQ_CMP;
3011 sim = start_ccb->ccb_h.path->bus->sim;
3012 (*(sim->sim_action))(sim, start_ccb);
3017 union ccb* abort_ccb;
3020 abort_ccb = start_ccb->cab.abort_ccb;
3021 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3023 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3024 struct cam_ccbq *ccbq;
3026 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3027 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3028 abort_ccb->ccb_h.status =
3029 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3030 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3032 xpt_done(abort_ccb);
3034 start_ccb->ccb_h.status = CAM_REQ_CMP;
3037 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3038 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3040 * We've caught this ccb en route to
3041 * the SIM. Flag it for abort and the
3042 * SIM will do so just before starting
3043 * real work on the CCB.
3045 abort_ccb->ccb_h.status =
3046 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3047 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3048 start_ccb->ccb_h.status = CAM_REQ_CMP;
3052 if (XPT_FC_IS_QUEUED(abort_ccb)
3053 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3055 * It's already completed but waiting
3056 * for our SWI to get to it.
3058 start_ccb->ccb_h.status = CAM_UA_ABORT;
3062 * If we weren't able to take care of the abort request
3063 * in the XPT, pass the request down to the SIM for processing.
3067 case XPT_ACCEPT_TARGET_IO:
3069 case XPT_IMMED_NOTIFY:
3070 case XPT_NOTIFY_ACK:
3071 case XPT_GET_TRAN_SETTINGS:
3074 struct cam_sim *sim;
3076 sim = start_ccb->ccb_h.path->bus->sim;
3077 (*(sim->sim_action))(sim, start_ccb);
3082 struct cam_sim *sim;
3084 sim = start_ccb->ccb_h.path->bus->sim;
3085 (*(sim->sim_action))(sim, start_ccb);
3088 case XPT_PATH_STATS:
3089 start_ccb->cpis.last_reset =
3090 start_ccb->ccb_h.path->bus->last_reset;
3091 start_ccb->ccb_h.status = CAM_REQ_CMP;
3098 dev = start_ccb->ccb_h.path->device;
3100 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3101 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3103 struct ccb_getdev *cgd;
3107 cgd = &start_ccb->cgd;
3108 bus = cgd->ccb_h.path->bus;
3109 tar = cgd->ccb_h.path->target;
3110 cgd->inq_data = dev->inq_data;
3111 cgd->ccb_h.status = CAM_REQ_CMP;
3112 cgd->serial_num_len = dev->serial_num_len;
3113 if ((dev->serial_num_len > 0)
3114 && (dev->serial_num != NULL))
3115 bcopy(dev->serial_num, cgd->serial_num,
3116 dev->serial_num_len);
3121 case XPT_GDEV_STATS:
3126 dev = start_ccb->ccb_h.path->device;
3128 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3129 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3131 struct ccb_getdevstats *cgds;
3135 cgds = &start_ccb->cgds;
3136 bus = cgds->ccb_h.path->bus;
3137 tar = cgds->ccb_h.path->target;
3138 cgds->dev_openings = dev->ccbq.dev_openings;
3139 cgds->dev_active = dev->ccbq.dev_active;
3140 cgds->devq_openings = dev->ccbq.devq_openings;
3141 cgds->devq_queued = dev->ccbq.queue.entries;
3142 cgds->held = dev->ccbq.held;
3143 cgds->last_reset = tar->last_reset;
3144 cgds->maxtags = dev->quirk->maxtags;
3145 cgds->mintags = dev->quirk->mintags;
3146 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3147 cgds->last_reset = bus->last_reset;
3148 cgds->ccb_h.status = CAM_REQ_CMP;
3155 struct cam_periph *nperiph;
3156 struct periph_list *periph_head;
3157 struct ccb_getdevlist *cgdl;
3160 struct cam_ed *device;
3167 * Don't want anyone mucking with our data.
3170 device = start_ccb->ccb_h.path->device;
3171 periph_head = &device->periphs;
3172 cgdl = &start_ccb->cgdl;
3175 * Check and see if the list has changed since the user
3176 * last requested a list member. If so, tell them that the
3177 * list has changed, and therefore they need to start over
3178 * from the beginning.
3180 if ((cgdl->index != 0) &&
3181 (cgdl->generation != device->generation)) {
3182 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3188 * Traverse the list of peripherals and attempt to find
3189 * the requested peripheral.
3191 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3192 (nperiph != NULL) && (i <= cgdl->index);
3193 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3194 if (i == cgdl->index) {
3195 strncpy(cgdl->periph_name,
3196 nperiph->periph_name,
3198 cgdl->unit_number = nperiph->unit_number;
3203 cgdl->status = CAM_GDEVLIST_ERROR;
3208 if (nperiph == NULL)
3209 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3211 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3214 cgdl->generation = device->generation;
3217 cgdl->ccb_h.status = CAM_REQ_CMP;
3223 dev_pos_type position_type;
3224 struct ccb_dev_match *cdm;
3227 cdm = &start_ccb->cdm;
3230 * Prevent EDT changes while we traverse it.
3234 * There are two ways of getting at information in the EDT.
3235 * The first way is via the primary EDT tree. It starts
3236 * with a list of busses, then a list of targets on a bus,
3237 * then devices/luns on a target, and then peripherals on a
3238 * device/lun. The "other" way is by the peripheral driver
3239 * lists. The peripheral driver lists are organized by
3240 * peripheral driver. (obviously) So it makes sense to
3241 * use the peripheral driver list if the user is looking
3242 * for something like "da1", or all "da" devices. If the
3243 * user is looking for something on a particular bus/target
3244 * or lun, it's generally better to go through the EDT tree.
3247 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3248 position_type = cdm->pos.position_type;
3252 position_type = CAM_DEV_POS_NONE;
3254 for (i = 0; i < cdm->num_patterns; i++) {
3255 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3256 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3257 position_type = CAM_DEV_POS_EDT;
3262 if (cdm->num_patterns == 0)
3263 position_type = CAM_DEV_POS_EDT;
3264 else if (position_type == CAM_DEV_POS_NONE)
3265 position_type = CAM_DEV_POS_PDRV;
3268 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3269 case CAM_DEV_POS_EDT:
3270 ret = xptedtmatch(cdm);
3272 case CAM_DEV_POS_PDRV:
3273 ret = xptperiphlistmatch(cdm);
3276 cdm->status = CAM_DEV_MATCH_ERROR;
3282 if (cdm->status == CAM_DEV_MATCH_ERROR)
3283 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3285 start_ccb->ccb_h.status = CAM_REQ_CMP;
3291 struct ccb_setasync *csa;
3292 struct async_node *cur_entry;
3293 struct async_list *async_head;
3297 csa = &start_ccb->csa;
3298 added = csa->event_enable;
3299 async_head = &csa->ccb_h.path->device->asyncs;
3302 * If there is already an entry for us, simply
3306 cur_entry = SLIST_FIRST(async_head);
3307 while (cur_entry != NULL) {
3308 if ((cur_entry->callback_arg == csa->callback_arg)
3309 && (cur_entry->callback == csa->callback))
3311 cur_entry = SLIST_NEXT(cur_entry, links);
3314 if (cur_entry != NULL) {
3316 * If the request has no flags set,
3319 added &= ~cur_entry->event_enable;
3320 if (csa->event_enable == 0) {
3321 SLIST_REMOVE(async_head, cur_entry,
3323 csa->ccb_h.path->device->refcount--;
3324 free(cur_entry, M_DEVBUF);
3326 cur_entry->event_enable = csa->event_enable;
3329 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3331 if (cur_entry == NULL) {
3333 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3336 cur_entry->event_enable = csa->event_enable;
3337 cur_entry->callback_arg = csa->callback_arg;
3338 cur_entry->callback = csa->callback;
3339 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3340 csa->ccb_h.path->device->refcount++;
3343 if ((added & AC_FOUND_DEVICE) != 0) {
3345 * Get this peripheral up to date with all
3346 * the currently existing devices.
3348 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3350 if ((added & AC_PATH_REGISTERED) != 0) {
3352 * Get this peripheral up to date with all
3353 * the currently existing busses.
3355 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3358 start_ccb->ccb_h.status = CAM_REQ_CMP;
3363 struct ccb_relsim *crs;
3367 crs = &start_ccb->crs;
3368 dev = crs->ccb_h.path->device;
3371 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3377 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3379 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3381 /* Don't ever go below one opening */
3382 if (crs->openings > 0) {
3383 xpt_dev_ccbq_resize(crs->ccb_h.path,
3387 xpt_print_path(crs->ccb_h.path);
3388 printf("tagged openings "
3396 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3398 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3401 * Just extend the old timeout and decrement
3402 * the freeze count so that a single timeout
3403 * is sufficient for releasing the queue.
3405 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3406 untimeout(xpt_release_devq_timeout,
3407 dev, dev->c_handle);
3410 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3414 timeout(xpt_release_devq_timeout,
3416 (crs->release_timeout * hz) / 1000);
3418 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3422 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3424 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3426 * Decrement the freeze count so that a single
3427 * completion is still sufficient to unfreeze
3430 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3433 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3434 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3438 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3440 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3441 || (dev->ccbq.dev_active == 0)) {
3443 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3446 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3447 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3452 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3454 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3457 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3458 start_ccb->ccb_h.status = CAM_REQ_CMP;
3462 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3465 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3466 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3474 #ifdef CAM_DEBUG_DELAY
3475 cam_debug_delay = CAM_DEBUG_DELAY;
3477 cam_dflags = start_ccb->cdbg.flags;
3478 if (cam_dpath != NULL) {
3479 xpt_free_path(cam_dpath);
3483 if (cam_dflags != CAM_DEBUG_NONE) {
3484 if (xpt_create_path(&cam_dpath, xpt_periph,
3485 start_ccb->ccb_h.path_id,
3486 start_ccb->ccb_h.target_id,
3487 start_ccb->ccb_h.target_lun) !=
3489 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3490 cam_dflags = CAM_DEBUG_NONE;
3492 start_ccb->ccb_h.status = CAM_REQ_CMP;
3493 xpt_print_path(cam_dpath);
3494 printf("debugging flags now %x\n", cam_dflags);
3498 start_ccb->ccb_h.status = CAM_REQ_CMP;
3501 #else /* !CAMDEBUG */
3502 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3503 #endif /* CAMDEBUG */
3507 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3508 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3509 start_ccb->ccb_h.status = CAM_REQ_CMP;
3516 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3523 xpt_polled_action(union ccb *start_ccb)
3527 struct cam_sim *sim;
3528 struct cam_devq *devq;
3531 timeout = start_ccb->ccb_h.timeout;
3532 sim = start_ccb->ccb_h.path->bus->sim;
3534 dev = start_ccb->ccb_h.path->device;
3539 * Steal an opening so that no other queued requests
3540 * can get it before us while we simulate interrupts.
3542 dev->ccbq.devq_openings--;
3543 dev->ccbq.dev_openings--;
3545 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3546 && (--timeout > 0)) {
3548 (*(sim->sim_poll))(sim);
3553 dev->ccbq.devq_openings++;
3554 dev->ccbq.dev_openings++;
3557 xpt_action(start_ccb);
3558 while(--timeout > 0) {
3559 (*(sim->sim_poll))(sim);
3562 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3569 * XXX Is it worth adding a sim_timeout entry
3570 * point so we can attempt recovery? If
3571 * this is only used for dumps, I don't think
3574 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3577 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3583 * Schedule a peripheral driver to receive a ccb when it's
3584 * target device has space for more transactions.
3587 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3589 struct cam_ed *device;
3593 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3594 device = perph->path->device;
3596 if (periph_is_queued(perph)) {
3597 /* Simply reorder based on new priority */
3598 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3599 (" change priority to %d\n", new_priority));
3600 if (new_priority < perph->pinfo.priority) {
3601 camq_change_priority(&device->drvq,
3607 /* New entry on the queue */
3608 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3609 (" added periph to queue\n"));
3610 perph->pinfo.priority = new_priority;
3611 perph->pinfo.generation = ++device->drvq.generation;
3612 camq_insert(&device->drvq, &perph->pinfo);
3613 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3617 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3618 (" calling xpt_run_devq\n"));
3619 xpt_run_dev_allocq(perph->path->bus);
3625 * Schedule a device to run on a given queue.
3626 * If the device was inserted as a new entry on the queue,
3627 * return 1 meaning the device queue should be run. If we
3628 * were already queued, implying someone else has already
3629 * started the queue, return 0 so the caller doesn't attempt
3630 * to run the queue. Must be run at either splsoftcam
3631 * (or splcam since that encompases splsoftcam).
3634 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3635 u_int32_t new_priority)
3638 u_int32_t old_priority;
3640 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3642 old_priority = pinfo->priority;
3645 * Are we already queued?
3647 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3648 /* Simply reorder based on new priority */
3649 if (new_priority < old_priority) {
3650 camq_change_priority(queue, pinfo->index,
3652 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3653 ("changed priority to %d\n",
3658 /* New entry on the queue */
3659 if (new_priority < old_priority)
3660 pinfo->priority = new_priority;
3662 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3663 ("Inserting onto queue\n"));
3664 pinfo->generation = ++queue->generation;
3665 camq_insert(queue, pinfo);
3672 xpt_run_dev_allocq(struct cam_eb *bus)
3674 struct cam_devq *devq;
3677 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3678 devq = bus->sim->devq;
3680 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3681 (" qfrozen_cnt == 0x%x, entries == %d, "
3682 "openings == %d, active == %d\n",
3683 devq->alloc_queue.qfrozen_cnt,
3684 devq->alloc_queue.entries,
3685 devq->alloc_openings,
3686 devq->alloc_active));
3689 devq->alloc_queue.qfrozen_cnt++;
3690 while ((devq->alloc_queue.entries > 0)
3691 && (devq->alloc_openings > 0)
3692 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3693 struct cam_ed_qinfo *qinfo;
3694 struct cam_ed *device;
3695 union ccb *work_ccb;
3696 struct cam_periph *drv;
3699 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3701 device = qinfo->device;
3703 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3704 ("running device %p\n", device));
3706 drvq = &device->drvq;
3709 if (drvq->entries <= 0) {
3710 panic("xpt_run_dev_allocq: "
3711 "Device on queue without any work to do");
3714 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3715 devq->alloc_openings--;
3716 devq->alloc_active++;
3717 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3719 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3720 drv->pinfo.priority);
3721 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3722 ("calling periph start\n"));
3723 drv->periph_start(drv, work_ccb);
3726 * Malloc failure in alloc_ccb
3729 * XXX add us to a list to be run from free_ccb
3730 * if we don't have any ccbs active on this
3731 * device queue otherwise we may never get run
3737 /* Raise IPL for possible insertion and test at top of loop */
3740 if (drvq->entries > 0) {
3741 /* We have more work. Attempt to reschedule */
3742 xpt_schedule_dev_allocq(bus, device);
3745 devq->alloc_queue.qfrozen_cnt--;
3750 xpt_run_dev_sendq(struct cam_eb *bus)
3752 struct cam_devq *devq;
3755 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3757 devq = bus->sim->devq;
3760 devq->send_queue.qfrozen_cnt++;
3763 while ((devq->send_queue.entries > 0)
3764 && (devq->send_openings > 0)) {
3765 struct cam_ed_qinfo *qinfo;
3766 struct cam_ed *device;
3767 union ccb *work_ccb;
3768 struct cam_sim *sim;
3772 if (devq->send_queue.qfrozen_cnt > 1) {
3777 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3779 device = qinfo->device;
3782 * If the device has been "frozen", don't attempt
3785 if (device->qfrozen_cnt > 0) {
3790 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3791 ("running device %p\n", device));
3793 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3794 if (work_ccb == NULL) {
3795 printf("device on run queue with no ccbs???");
3800 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3802 if (num_highpower <= 0) {
3804 * We got a high power command, but we
3805 * don't have any available slots. Freeze
3806 * the device queue until we have a slot
3809 device->qfrozen_cnt++;
3810 STAILQ_INSERT_TAIL(&highpowerq,
3818 * Consume a high power slot while
3824 devq->active_dev = device;
3825 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3827 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3830 devq->send_openings--;
3831 devq->send_active++;
3833 if (device->ccbq.queue.entries > 0)
3834 xpt_schedule_dev_sendq(bus, device);
3836 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3838 * The client wants to freeze the queue
3839 * after this CCB is sent.
3842 device->qfrozen_cnt++;
3848 /* In Target mode, the peripheral driver knows best... */
3849 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3850 if ((device->inq_flags & SID_CmdQue) != 0
3851 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3852 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3855 * Clear this in case of a retried CCB that
3856 * failed due to a rejected tag.
3858 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3862 * Device queues can be shared among multiple sim instances
3863 * that reside on different busses. Use the SIM in the queue
3864 * CCB's path, rather than the one in the bus that was passed
3865 * into this function.
3867 sim = work_ccb->ccb_h.path->bus->sim;
3868 (*(sim->sim_action))(sim, work_ccb);
3871 devq->active_dev = NULL;
3873 /* Raise IPL for possible insertion and test at top of loop */
3878 devq->send_queue.qfrozen_cnt--;
3883 * This function merges stuff from the slave ccb into the master ccb, while
3884 * keeping important fields in the master ccb constant.
3887 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3890 * Pull fields that are valid for peripheral drivers to set
3891 * into the master CCB along with the CCB "payload".
3893 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3894 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3895 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3896 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3897 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3898 sizeof(union ccb) - sizeof(struct ccb_hdr));
3902 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3904 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3905 ccb_h->pinfo.priority = priority;
3907 ccb_h->path_id = path->bus->path_id;
3909 ccb_h->target_id = path->target->target_id;
3911 ccb_h->target_id = CAM_TARGET_WILDCARD;
3913 ccb_h->target_lun = path->device->lun_id;
3914 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3916 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3918 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3922 /* Path manipulation functions */
3924 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3925 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3927 struct cam_path *path;
3930 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3933 status = CAM_RESRC_UNAVAIL;
3936 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3937 if (status != CAM_REQ_CMP) {
3938 free(path, M_DEVBUF);
3941 *new_path_ptr = path;
3946 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3947 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3950 struct cam_et *target;
3951 struct cam_ed *device;
3955 status = CAM_REQ_CMP; /* Completed without error */
3956 target = NULL; /* Wildcarded */
3957 device = NULL; /* Wildcarded */
3960 * We will potentially modify the EDT, so block interrupts
3961 * that may attempt to create cam paths.
3964 bus = xpt_find_bus(path_id);
3966 status = CAM_PATH_INVALID;
3968 target = xpt_find_target(bus, target_id);
3969 if (target == NULL) {
3971 struct cam_et *new_target;
3973 new_target = xpt_alloc_target(bus, target_id);
3974 if (new_target == NULL) {
3975 status = CAM_RESRC_UNAVAIL;
3977 target = new_target;
3980 if (target != NULL) {
3981 device = xpt_find_device(target, lun_id);
3982 if (device == NULL) {
3984 struct cam_ed *new_device;
3986 new_device = xpt_alloc_device(bus,
3989 if (new_device == NULL) {
3990 status = CAM_RESRC_UNAVAIL;
3992 device = new_device;
4000 * Only touch the user's data if we are successful.
4002 if (status == CAM_REQ_CMP) {
4003 new_path->periph = perph;
4004 new_path->bus = bus;
4005 new_path->target = target;
4006 new_path->device = device;
4007 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4010 xpt_release_device(bus, target, device);
4012 xpt_release_target(bus, target);
4014 xpt_release_bus(bus);
4020 xpt_release_path(struct cam_path *path)
4022 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4023 if (path->device != NULL) {
4024 xpt_release_device(path->bus, path->target, path->device);
4025 path->device = NULL;
4027 if (path->target != NULL) {
4028 xpt_release_target(path->bus, path->target);
4029 path->target = NULL;
4031 if (path->bus != NULL) {
4032 xpt_release_bus(path->bus);
4038 xpt_free_path(struct cam_path *path)
4040 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4041 xpt_release_path(path);
4042 free(path, M_DEVBUF);
4047 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4048 * in path1, 2 for match with wildcards in path2.
4051 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4055 if (path1->bus != path2->bus) {
4056 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4058 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4063 if (path1->target != path2->target) {
4064 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4067 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4072 if (path1->device != path2->device) {
4073 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4076 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4085 xpt_print_path(struct cam_path *path)
4088 printf("(nopath): ");
4090 if (path->periph != NULL)
4091 printf("(%s%d:", path->periph->periph_name,
4092 path->periph->unit_number);
4094 printf("(noperiph:");
4096 if (path->bus != NULL)
4097 printf("%s%d:%d:", path->bus->sim->sim_name,
4098 path->bus->sim->unit_number,
4099 path->bus->sim->bus_id);
4103 if (path->target != NULL)
4104 printf("%d:", path->target->target_id);
4108 if (path->device != NULL)
4109 printf("%d): ", path->device->lun_id);
4116 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4120 sbuf_new(&sb, str, str_len, 0);
4123 sbuf_printf(&sb, "(nopath): ");
4125 if (path->periph != NULL)
4126 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4127 path->periph->unit_number);
4129 sbuf_printf(&sb, "(noperiph:");
4131 if (path->bus != NULL)
4132 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4133 path->bus->sim->unit_number,
4134 path->bus->sim->bus_id);
4136 sbuf_printf(&sb, "nobus:");
4138 if (path->target != NULL)
4139 sbuf_printf(&sb, "%d:", path->target->target_id);
4141 sbuf_printf(&sb, "X:");
4143 if (path->device != NULL)
4144 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4146 sbuf_printf(&sb, "X): ");
4150 return(sbuf_len(&sb));
4154 xpt_path_path_id(struct cam_path *path)
4156 return(path->bus->path_id);
4160 xpt_path_target_id(struct cam_path *path)
4162 if (path->target != NULL)
4163 return (path->target->target_id);
4165 return (CAM_TARGET_WILDCARD);
4169 xpt_path_lun_id(struct cam_path *path)
4171 if (path->device != NULL)
4172 return (path->device->lun_id);
4174 return (CAM_LUN_WILDCARD);
4178 xpt_path_sim(struct cam_path *path)
4180 return (path->bus->sim);
4184 xpt_path_periph(struct cam_path *path)
4186 return (path->periph);
4190 * Release a CAM control block for the caller. Remit the cost of the structure
4191 * to the device referenced by the path. If the this device had no 'credits'
4192 * and peripheral drivers have registered async callbacks for this notification
4196 xpt_release_ccb(union ccb *free_ccb)
4199 struct cam_path *path;
4200 struct cam_ed *device;
4203 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4204 path = free_ccb->ccb_h.path;
4205 device = path->device;
4208 cam_ccbq_release_opening(&device->ccbq);
4209 if (xpt_ccb_count > xpt_max_ccbs) {
4210 xpt_free_ccb(free_ccb);
4213 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4215 bus->sim->devq->alloc_openings++;
4216 bus->sim->devq->alloc_active--;
4217 /* XXX Turn this into an inline function - xpt_run_device?? */
4218 if ((device_is_alloc_queued(device) == 0)
4219 && (device->drvq.entries > 0)) {
4220 xpt_schedule_dev_allocq(bus, device);
4223 if (dev_allocq_is_runnable(bus->sim->devq))
4224 xpt_run_dev_allocq(bus);
4227 /* Functions accessed by SIM drivers */
4230 * A sim structure, listing the SIM entry points and instance
4231 * identification info is passed to xpt_bus_register to hook the SIM
4232 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4233 * for this new bus and places it in the array of busses and assigns
4234 * it a path_id. The path_id may be influenced by "hard wiring"
4235 * information specified by the user. Once interrupt services are
4236 * availible, the bus will be probed.
4239 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4241 struct cam_eb *new_bus;
4242 struct cam_eb *old_bus;
4243 struct ccb_pathinq cpi;
4247 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4248 M_DEVBUF, M_NOWAIT);
4249 if (new_bus == NULL) {
4250 /* Couldn't satisfy request */
4251 return (CAM_RESRC_UNAVAIL);
4254 if (strcmp(sim->sim_name, "xpt") != 0) {
4257 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4260 TAILQ_INIT(&new_bus->et_entries);
4261 new_bus->path_id = sim->path_id;
4263 timevalclear(&new_bus->last_reset);
4265 new_bus->refcount = 1; /* Held until a bus_deregister event */
4266 new_bus->generation = 0;
4268 old_bus = TAILQ_FIRST(&xpt_busses);
4269 while (old_bus != NULL
4270 && old_bus->path_id < new_bus->path_id)
4271 old_bus = TAILQ_NEXT(old_bus, links);
4272 if (old_bus != NULL)
4273 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4275 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4279 /* Notify interested parties */
4280 if (sim->path_id != CAM_XPT_PATH_ID) {
4281 struct cam_path path;
4283 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4284 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4285 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4286 cpi.ccb_h.func_code = XPT_PATH_INQ;
4287 xpt_action((union ccb *)&cpi);
4288 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4289 xpt_release_path(&path);
4291 return (CAM_SUCCESS);
4295 xpt_bus_deregister(path_id_t pathid)
4297 struct cam_path bus_path;
4300 status = xpt_compile_path(&bus_path, NULL, pathid,
4301 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4302 if (status != CAM_REQ_CMP)
4305 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4306 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4308 /* Release the reference count held while registered. */
4309 xpt_release_bus(bus_path.bus);
4310 xpt_release_path(&bus_path);
4312 return (CAM_REQ_CMP);
4316 xptnextfreepathid(void)
4323 bus = TAILQ_FIRST(&xpt_busses);
4325 /* Find an unoccupied pathid */
4327 && bus->path_id <= pathid) {
4328 if (bus->path_id == pathid)
4330 bus = TAILQ_NEXT(bus, links);
4334 * Ensure that this pathid is not reserved for
4335 * a bus that may be registered in the future.
4337 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4339 /* Start the search over */
4346 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4352 pathid = CAM_XPT_PATH_ID;
4353 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4355 while ((i = resource_query_string(i, "at", buf)) != -1) {
4356 if (strcmp(resource_query_name(i), "scbus")) {
4357 /* Avoid a bit of foot shooting. */
4360 dunit = resource_query_unit(i);
4361 if (dunit < 0) /* unwired?! */
4363 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4364 if (sim_bus == val) {
4368 } else if (sim_bus == 0) {
4369 /* Unspecified matches bus 0 */
4373 printf("Ambiguous scbus configuration for %s%d "
4374 "bus %d, cannot wire down. The kernel "
4375 "config entry for scbus%d should "
4376 "specify a controller bus.\n"
4377 "Scbus will be assigned dynamically.\n",
4378 sim_name, sim_unit, sim_bus, dunit);
4383 if (pathid == CAM_XPT_PATH_ID)
4384 pathid = xptnextfreepathid();
4389 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4392 struct cam_et *target, *next_target;
4393 struct cam_ed *device, *next_device;
4396 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4399 * Most async events come from a CAM interrupt context. In
4400 * a few cases, the error recovery code at the peripheral layer,
4401 * which may run from our SWI or a process context, may signal
4402 * deferred events with a call to xpt_async. Ensure async
4403 * notifications are serialized by blocking cam interrupts.
4409 if (async_code == AC_BUS_RESET) {
4413 /* Update our notion of when the last reset occurred */
4414 microtime(&bus->last_reset);
4418 for (target = TAILQ_FIRST(&bus->et_entries);
4420 target = next_target) {
4422 next_target = TAILQ_NEXT(target, links);
4424 if (path->target != target
4425 && path->target->target_id != CAM_TARGET_WILDCARD
4426 && target->target_id != CAM_TARGET_WILDCARD)
4429 if (async_code == AC_SENT_BDR) {
4432 /* Update our notion of when the last reset occurred */
4434 microtime(&path->target->last_reset);
4438 for (device = TAILQ_FIRST(&target->ed_entries);
4440 device = next_device) {
4442 next_device = TAILQ_NEXT(device, links);
4444 if (path->device != device
4445 && path->device->lun_id != CAM_LUN_WILDCARD
4446 && device->lun_id != CAM_LUN_WILDCARD)
4449 xpt_dev_async(async_code, bus, target,
4452 xpt_async_bcast(&device->asyncs, async_code,
4458 * If this wasn't a fully wildcarded async, tell all
4459 * clients that want all async events.
4461 if (bus != xpt_periph->path->bus)
4462 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4468 xpt_async_bcast(struct async_list *async_head,
4469 u_int32_t async_code,
4470 struct cam_path *path, void *async_arg)
4472 struct async_node *cur_entry;
4474 cur_entry = SLIST_FIRST(async_head);
4475 while (cur_entry != NULL) {
4476 struct async_node *next_entry;
4478 * Grab the next list entry before we call the current
4479 * entry's callback. This is because the callback function
4480 * can delete its async callback entry.
4482 next_entry = SLIST_NEXT(cur_entry, links);
4483 if ((cur_entry->event_enable & async_code) != 0)
4484 cur_entry->callback(cur_entry->callback_arg,
4487 cur_entry = next_entry;
4492 * Handle any per-device event notifications that require action by the XPT.
4495 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4496 struct cam_ed *device, void *async_arg)
4499 struct cam_path newpath;
4502 * We only need to handle events for real devices.
4504 if (target->target_id == CAM_TARGET_WILDCARD
4505 || device->lun_id == CAM_LUN_WILDCARD)
4509 * We need our own path with wildcards expanded to
4510 * handle certain types of events.
4512 if ((async_code == AC_SENT_BDR)
4513 || (async_code == AC_BUS_RESET)
4514 || (async_code == AC_INQ_CHANGED))
4515 status = xpt_compile_path(&newpath, NULL,
4520 status = CAM_REQ_CMP_ERR;
4522 if (status == CAM_REQ_CMP) {
4525 * Allow transfer negotiation to occur in a
4526 * tag free environment.
4528 if (async_code == AC_SENT_BDR
4529 || async_code == AC_BUS_RESET)
4530 xpt_toggle_tags(&newpath);
4532 if (async_code == AC_INQ_CHANGED) {
4534 * We've sent a start unit command, or
4535 * something similar to a device that
4536 * may have caused its inquiry data to
4537 * change. So we re-scan the device to
4538 * refresh the inquiry data for it.
4540 xpt_scan_lun(newpath.periph, &newpath,
4541 CAM_EXPECT_INQ_CHANGE, NULL);
4543 xpt_release_path(&newpath);
4544 } else if (async_code == AC_LOST_DEVICE) {
4545 device->flags |= CAM_DEV_UNCONFIGURED;
4546 } else if (async_code == AC_TRANSFER_NEG) {
4547 struct ccb_trans_settings *settings;
4549 settings = (struct ccb_trans_settings *)async_arg;
4550 xpt_set_transfer_settings(settings, device,
4551 /*async_update*/TRUE);
4556 xpt_freeze_devq(struct cam_path *path, u_int count)
4559 struct ccb_hdr *ccbh;
4562 path->device->qfrozen_cnt += count;
4565 * Mark the last CCB in the queue as needing
4566 * to be requeued if the driver hasn't
4567 * changed it's state yet. This fixes a race
4568 * where a ccb is just about to be queued to
4569 * a controller driver when it's interrupt routine
4570 * freezes the queue. To completly close the
4571 * hole, controller drives must check to see
4572 * if a ccb's status is still CAM_REQ_INPROG
4573 * under spl protection just before they queue
4574 * the CCB. See ahc_action/ahc_freeze_devq for
4577 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4578 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4579 ccbh->status = CAM_REQUEUE_REQ;
4581 return (path->device->qfrozen_cnt);
4585 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4587 sim->devq->send_queue.qfrozen_cnt += count;
4588 if (sim->devq->active_dev != NULL) {
4589 struct ccb_hdr *ccbh;
4591 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4593 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4594 ccbh->status = CAM_REQUEUE_REQ;
4596 return (sim->devq->send_queue.qfrozen_cnt);
4600 xpt_release_devq_timeout(void *arg)
4602 struct cam_ed *device;
4604 device = (struct cam_ed *)arg;
4606 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4610 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4612 xpt_release_devq_device(path->device, count, run_queue);
4616 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4624 if (dev->qfrozen_cnt > 0) {
4626 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4627 dev->qfrozen_cnt -= count;
4628 if (dev->qfrozen_cnt == 0) {
4631 * No longer need to wait for a successful
4632 * command completion.
4634 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4637 * Remove any timeouts that might be scheduled
4638 * to release this queue.
4640 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4641 untimeout(xpt_release_devq_timeout, dev,
4643 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4647 * Now that we are unfrozen schedule the
4648 * device so any pending transactions are
4651 if ((dev->ccbq.queue.entries > 0)
4652 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4653 && (run_queue != 0)) {
4660 xpt_run_dev_sendq(dev->target->bus);
4665 xpt_release_simq(struct cam_sim *sim, int run_queue)
4670 sendq = &(sim->devq->send_queue);
4672 if (sendq->qfrozen_cnt > 0) {
4674 sendq->qfrozen_cnt--;
4675 if (sendq->qfrozen_cnt == 0) {
4679 * If there is a timeout scheduled to release this
4680 * sim queue, remove it. The queue frozen count is
4683 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4684 untimeout(xpt_release_simq_timeout, sim,
4686 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4688 bus = xpt_find_bus(sim->path_id);
4693 * Now that we are unfrozen run the send queue.
4695 xpt_run_dev_sendq(bus);
4697 xpt_release_bus(bus);
4705 xpt_release_simq_timeout(void *arg)
4707 struct cam_sim *sim;
4709 sim = (struct cam_sim *)arg;
4710 xpt_release_simq(sim, /* run_queue */ TRUE);
4714 xpt_done(union ccb *done_ccb)
4720 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4721 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4723 * Queue up the request for handling by our SWI handler
4724 * any of the "non-immediate" type of ccbs.
4726 switch (done_ccb->ccb_h.path->periph->type) {
4727 case CAM_PERIPH_BIO:
4728 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4730 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4731 swi_sched(cambio_ih, SWI_NOSWITCH);
4733 case CAM_PERIPH_NET:
4734 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4736 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4737 swi_sched(camnet_ih, SWI_NOSWITCH);
4749 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4754 xpt_free_ccb(union ccb *free_ccb)
4756 free(free_ccb, M_DEVBUF);
4761 /* Private XPT functions */
4764 * Get a CAM control block for the caller. Charge the structure to the device
4765 * referenced by the path. If the this device has no 'credits' then the
4766 * device already has the maximum number of outstanding operations under way
4767 * and we return NULL. If we don't have sufficient resources to allocate more
4768 * ccbs, we also return NULL.
4771 xpt_get_ccb(struct cam_ed *device)
4777 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4778 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4779 if (new_ccb == NULL) {
4783 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4784 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4788 cam_ccbq_take_opening(&device->ccbq);
4789 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4795 xpt_release_bus(struct cam_eb *bus)
4800 if ((--bus->refcount == 0)
4801 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4802 TAILQ_REMOVE(&xpt_busses, bus, links);
4805 free(bus, M_DEVBUF);
4810 static struct cam_et *
4811 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4813 struct cam_et *target;
4815 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4816 if (target != NULL) {
4817 struct cam_et *cur_target;
4819 TAILQ_INIT(&target->ed_entries);
4821 target->target_id = target_id;
4822 target->refcount = 1;
4823 target->generation = 0;
4824 timevalclear(&target->last_reset);
4826 * Hold a reference to our parent bus so it
4827 * will not go away before we do.
4831 /* Insertion sort into our bus's target list */
4832 cur_target = TAILQ_FIRST(&bus->et_entries);
4833 while (cur_target != NULL && cur_target->target_id < target_id)
4834 cur_target = TAILQ_NEXT(cur_target, links);
4836 if (cur_target != NULL) {
4837 TAILQ_INSERT_BEFORE(cur_target, target, links);
4839 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4847 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4852 if ((--target->refcount == 0)
4853 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4854 TAILQ_REMOVE(&bus->et_entries, target, links);
4857 free(target, M_DEVBUF);
4858 xpt_release_bus(bus);
4863 static struct cam_ed *
4864 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4866 #ifdef CAM_NEW_TRAN_CODE
4867 struct cam_path path;
4868 #endif /* CAM_NEW_TRAN_CODE */
4869 struct cam_ed *device;
4870 struct cam_devq *devq;
4873 /* Make space for us in the device queue on our bus */
4874 devq = bus->sim->devq;
4875 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4877 if (status != CAM_REQ_CMP) {
4880 device = (struct cam_ed *)malloc(sizeof(*device),
4881 M_DEVBUF, M_NOWAIT);
4884 if (device != NULL) {
4885 struct cam_ed *cur_device;
4887 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4888 device->alloc_ccb_entry.device = device;
4889 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4890 device->send_ccb_entry.device = device;
4891 device->target = target;
4892 device->lun_id = lun_id;
4893 /* Initialize our queues */
4894 if (camq_init(&device->drvq, 0) != 0) {
4895 free(device, M_DEVBUF);
4898 if (cam_ccbq_init(&device->ccbq,
4899 bus->sim->max_dev_openings) != 0) {
4900 camq_fini(&device->drvq);
4901 free(device, M_DEVBUF);
4904 SLIST_INIT(&device->asyncs);
4905 SLIST_INIT(&device->periphs);
4906 device->generation = 0;
4907 device->owner = NULL;
4909 * Take the default quirk entry until we have inquiry
4910 * data and can determine a better quirk to use.
4912 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4913 bzero(&device->inq_data, sizeof(device->inq_data));
4914 device->inq_flags = 0;
4915 device->queue_flags = 0;
4916 device->serial_num = NULL;
4917 device->serial_num_len = 0;
4918 device->qfrozen_cnt = 0;
4919 device->flags = CAM_DEV_UNCONFIGURED;
4920 device->tag_delay_count = 0;
4921 device->refcount = 1;
4922 callout_handle_init(&device->c_handle);
4925 * Hold a reference to our parent target so it
4926 * will not go away before we do.
4931 * XXX should be limited by number of CCBs this bus can
4934 xpt_max_ccbs += device->ccbq.devq_openings;
4935 /* Insertion sort into our target's device list */
4936 cur_device = TAILQ_FIRST(&target->ed_entries);
4937 while (cur_device != NULL && cur_device->lun_id < lun_id)
4938 cur_device = TAILQ_NEXT(cur_device, links);
4939 if (cur_device != NULL) {
4940 TAILQ_INSERT_BEFORE(cur_device, device, links);
4942 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4944 target->generation++;
4945 #ifdef CAM_NEW_TRAN_CODE
4946 if (lun_id != CAM_LUN_WILDCARD) {
4947 xpt_compile_path(&path,
4952 xpt_devise_transport(&path);
4953 xpt_release_path(&path);
4955 #endif /* CAM_NEW_TRAN_CODE */
4961 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4962 struct cam_ed *device)
4967 if ((--device->refcount == 0)
4968 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4969 struct cam_devq *devq;
4971 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4972 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4973 panic("Removing device while still queued for ccbs");
4975 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4976 untimeout(xpt_release_devq_timeout, device,
4979 TAILQ_REMOVE(&target->ed_entries, device,links);
4980 target->generation++;
4981 xpt_max_ccbs -= device->ccbq.devq_openings;
4982 /* Release our slot in the devq */
4983 devq = bus->sim->devq;
4984 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4986 free(device, M_DEVBUF);
4987 xpt_release_target(bus, target);
4993 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5003 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5004 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5005 if (result == CAM_REQ_CMP && (diff < 0)) {
5006 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5008 /* Adjust the global limit */
5009 xpt_max_ccbs += diff;
5014 static struct cam_eb *
5015 xpt_find_bus(path_id_t path_id)
5019 for (bus = TAILQ_FIRST(&xpt_busses);
5021 bus = TAILQ_NEXT(bus, links)) {
5022 if (bus->path_id == path_id) {
5030 static struct cam_et *
5031 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5033 struct cam_et *target;
5035 for (target = TAILQ_FIRST(&bus->et_entries);
5037 target = TAILQ_NEXT(target, links)) {
5038 if (target->target_id == target_id) {
5046 static struct cam_ed *
5047 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5049 struct cam_ed *device;
5051 for (device = TAILQ_FIRST(&target->ed_entries);
5053 device = TAILQ_NEXT(device, links)) {
5054 if (device->lun_id == lun_id) {
5063 union ccb *request_ccb;
5064 struct ccb_pathinq *cpi;
5066 } xpt_scan_bus_info;
5069 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5070 * As the scan progresses, xpt_scan_bus is used as the
5071 * callback on completion function.
5074 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5076 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5077 ("xpt_scan_bus\n"));
5078 switch (request_ccb->ccb_h.func_code) {
5081 xpt_scan_bus_info *scan_info;
5082 union ccb *work_ccb;
5083 struct cam_path *path;
5088 /* Find out the characteristics of the bus */
5089 work_ccb = xpt_alloc_ccb();
5090 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5091 request_ccb->ccb_h.pinfo.priority);
5092 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5093 xpt_action(work_ccb);
5094 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5095 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5096 xpt_free_ccb(work_ccb);
5097 xpt_done(request_ccb);
5101 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5103 * Can't scan the bus on an adapter that
5104 * cannot perform the initiator role.
5106 request_ccb->ccb_h.status = CAM_REQ_CMP;
5107 xpt_free_ccb(work_ccb);
5108 xpt_done(request_ccb);
5112 /* Save some state for use while we probe for devices */
5113 scan_info = (xpt_scan_bus_info *)
5114 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5115 scan_info->request_ccb = request_ccb;
5116 scan_info->cpi = &work_ccb->cpi;
5118 /* Cache on our stack so we can work asynchronously */
5119 max_target = scan_info->cpi->max_target;
5120 initiator_id = scan_info->cpi->initiator_id;
5123 * Don't count the initiator if the
5124 * initiator is addressable.
5126 scan_info->pending_count = max_target + 1;
5127 if (initiator_id <= max_target)
5128 scan_info->pending_count--;
5130 for (i = 0; i <= max_target; i++) {
5132 if (i == initiator_id)
5135 status = xpt_create_path(&path, xpt_periph,
5136 request_ccb->ccb_h.path_id,
5138 if (status != CAM_REQ_CMP) {
5139 printf("xpt_scan_bus: xpt_create_path failed"
5140 " with status %#x, bus scan halted\n",
5144 work_ccb = xpt_alloc_ccb();
5145 xpt_setup_ccb(&work_ccb->ccb_h, path,
5146 request_ccb->ccb_h.pinfo.priority);
5147 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5148 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5149 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5150 work_ccb->crcn.flags = request_ccb->crcn.flags;
5151 xpt_action(work_ccb);
5157 xpt_scan_bus_info *scan_info;
5159 target_id_t target_id;
5162 /* Reuse the same CCB to query if a device was really found */
5163 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5164 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5165 request_ccb->ccb_h.pinfo.priority);
5166 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5168 path_id = request_ccb->ccb_h.path_id;
5169 target_id = request_ccb->ccb_h.target_id;
5170 lun_id = request_ccb->ccb_h.target_lun;
5171 xpt_action(request_ccb);
5173 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5174 struct cam_ed *device;
5175 struct cam_et *target;
5179 * If we already probed lun 0 successfully, or
5180 * we have additional configured luns on this
5181 * target that might have "gone away", go onto
5184 target = request_ccb->ccb_h.path->target;
5186 * We may touch devices that we don't
5187 * hold references too, so ensure they
5188 * don't disappear out from under us.
5189 * The target above is referenced by the
5190 * path in the request ccb.
5194 device = TAILQ_FIRST(&target->ed_entries);
5195 if (device != NULL) {
5196 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5197 if (device->lun_id == 0)
5198 device = TAILQ_NEXT(device, links);
5201 if ((lun_id != 0) || (device != NULL)) {
5202 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5206 struct cam_ed *device;
5208 device = request_ccb->ccb_h.path->device;
5210 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5211 /* Try the next lun */
5212 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5213 (device->quirk->quirks & CAM_QUIRK_HILUNS))
5218 xpt_free_path(request_ccb->ccb_h.path);
5221 if ((lun_id == request_ccb->ccb_h.target_lun)
5222 || lun_id > scan_info->cpi->max_lun) {
5225 xpt_free_ccb(request_ccb);
5226 scan_info->pending_count--;
5227 if (scan_info->pending_count == 0) {
5228 xpt_free_ccb((union ccb *)scan_info->cpi);
5229 request_ccb = scan_info->request_ccb;
5230 free(scan_info, M_TEMP);
5231 request_ccb->ccb_h.status = CAM_REQ_CMP;
5232 xpt_done(request_ccb);
5235 /* Try the next device */
5236 struct cam_path *path;
5239 path = request_ccb->ccb_h.path;
5240 status = xpt_create_path(&path, xpt_periph,
5241 path_id, target_id, lun_id);
5242 if (status != CAM_REQ_CMP) {
5243 printf("xpt_scan_bus: xpt_create_path failed "
5244 "with status %#x, halting LUN scan\n",
5246 xpt_free_ccb(request_ccb);
5247 scan_info->pending_count--;
5248 if (scan_info->pending_count == 0) {
5250 (union ccb *)scan_info->cpi);
5251 request_ccb = scan_info->request_ccb;
5252 free(scan_info, M_TEMP);
5253 request_ccb->ccb_h.status = CAM_REQ_CMP;
5254 xpt_done(request_ccb);
5258 xpt_setup_ccb(&request_ccb->ccb_h, path,
5259 request_ccb->ccb_h.pinfo.priority);
5260 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5261 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5262 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5263 request_ccb->crcn.flags =
5264 scan_info->request_ccb->crcn.flags;
5265 xpt_action(request_ccb);
5280 PROBE_TUR_FOR_NEGOTIATION
5284 PROBE_INQUIRY_CKSUM = 0x01,
5285 PROBE_SERIAL_CKSUM = 0x02,
5286 PROBE_NO_ANNOUNCE = 0x04
5290 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5291 probe_action action;
5292 union ccb saved_ccb;
5295 u_int8_t digest[16];
5299 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5300 cam_flags flags, union ccb *request_ccb)
5302 struct ccb_pathinq cpi;
5304 struct cam_path *new_path;
5305 struct cam_periph *old_periph;
5308 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5309 ("xpt_scan_lun\n"));
5311 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5312 cpi.ccb_h.func_code = XPT_PATH_INQ;
5313 xpt_action((union ccb *)&cpi);
5315 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5316 if (request_ccb != NULL) {
5317 request_ccb->ccb_h.status = cpi.ccb_h.status;
5318 xpt_done(request_ccb);
5323 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5325 * Can't scan the bus on an adapter that
5326 * cannot perform the initiator role.
5328 if (request_ccb != NULL) {
5329 request_ccb->ccb_h.status = CAM_REQ_CMP;
5330 xpt_done(request_ccb);
5335 if (request_ccb == NULL) {
5336 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5337 if (request_ccb == NULL) {
5338 xpt_print_path(path);
5339 printf("xpt_scan_lun: can't allocate CCB, can't "
5343 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5344 if (new_path == NULL) {
5345 xpt_print_path(path);
5346 printf("xpt_scan_lun: can't allocate path, can't "
5348 free(request_ccb, M_TEMP);
5351 status = xpt_compile_path(new_path, xpt_periph,
5353 path->target->target_id,
5354 path->device->lun_id);
5356 if (status != CAM_REQ_CMP) {
5357 xpt_print_path(path);
5358 printf("xpt_scan_lun: can't compile path, can't "
5360 free(request_ccb, M_TEMP);
5361 free(new_path, M_TEMP);
5364 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5365 request_ccb->ccb_h.cbfcnp = xptscandone;
5366 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5367 request_ccb->crcn.flags = flags;
5371 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5374 softc = (probe_softc *)old_periph->softc;
5375 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5378 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5379 probestart, "probe",
5381 request_ccb->ccb_h.path, NULL, 0,
5384 if (status != CAM_REQ_CMP) {
5385 xpt_print_path(path);
5386 printf("xpt_scan_lun: cam_alloc_periph returned an "
5387 "error, can't continue probe\n");
5388 request_ccb->ccb_h.status = status;
5389 xpt_done(request_ccb);
5396 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5398 xpt_release_path(done_ccb->ccb_h.path);
5399 free(done_ccb->ccb_h.path, M_TEMP);
5400 free(done_ccb, M_TEMP);
5404 proberegister(struct cam_periph *periph, void *arg)
5406 union ccb *request_ccb; /* CCB representing the probe request */
5409 request_ccb = (union ccb *)arg;
5410 if (periph == NULL) {
5411 printf("proberegister: periph was NULL!!\n");
5412 return(CAM_REQ_CMP_ERR);
5415 if (request_ccb == NULL) {
5416 printf("proberegister: no probe CCB, "
5417 "can't register device\n");
5418 return(CAM_REQ_CMP_ERR);
5421 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5423 if (softc == NULL) {
5424 printf("proberegister: Unable to probe new device. "
5425 "Unable to allocate softc\n");
5426 return(CAM_REQ_CMP_ERR);
5428 TAILQ_INIT(&softc->request_ccbs);
5429 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5432 periph->softc = softc;
5433 cam_periph_acquire(periph);
5435 * Ensure we've waited at least a bus settle
5436 * delay before attempting to probe the device.
5437 * For HBAs that don't do bus resets, this won't make a difference.
5439 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5441 probeschedule(periph);
5442 return(CAM_REQ_CMP);
5446 probeschedule(struct cam_periph *periph)
5448 struct ccb_pathinq cpi;
5452 softc = (probe_softc *)periph->softc;
5453 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5455 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5456 cpi.ccb_h.func_code = XPT_PATH_INQ;
5457 xpt_action((union ccb *)&cpi);
5460 * If a device has gone away and another device, or the same one,
5461 * is back in the same place, it should have a unit attention
5462 * condition pending. It will not report the unit attention in
5463 * response to an inquiry, which may leave invalid transfer
5464 * negotiations in effect. The TUR will reveal the unit attention
5465 * condition. Only send the TUR for lun 0, since some devices
5466 * will get confused by commands other than inquiry to non-existent
5467 * luns. If you think a device has gone away start your scan from
5468 * lun 0. This will insure that any bogus transfer settings are
5471 * If we haven't seen the device before and the controller supports
5472 * some kind of transfer negotiation, negotiate with the first
5473 * sent command if no bus reset was performed at startup. This
5474 * ensures that the device is not confused by transfer negotiation
5475 * settings left over by loader or BIOS action.
5477 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5478 && (ccb->ccb_h.target_lun == 0)) {
5479 softc->action = PROBE_TUR;
5480 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5481 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5482 proberequestdefaultnegotiation(periph);
5483 softc->action = PROBE_INQUIRY;
5485 softc->action = PROBE_INQUIRY;
5488 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5489 softc->flags |= PROBE_NO_ANNOUNCE;
5491 softc->flags &= ~PROBE_NO_ANNOUNCE;
5493 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5497 probestart(struct cam_periph *periph, union ccb *start_ccb)
5499 /* Probe the device that our peripheral driver points to */
5500 struct ccb_scsiio *csio;
5503 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5505 softc = (probe_softc *)periph->softc;
5506 csio = &start_ccb->csio;
5508 switch (softc->action) {
5510 case PROBE_TUR_FOR_NEGOTIATION:
5512 scsi_test_unit_ready(csio,
5521 case PROBE_FULL_INQUIRY:
5524 struct scsi_inquiry_data *inq_buf;
5526 inq_buf = &periph->path->device->inq_data;
5528 * If the device is currently configured, we calculate an
5529 * MD5 checksum of the inquiry data, and if the serial number
5530 * length is greater than 0, add the serial number data
5531 * into the checksum as well. Once the inquiry and the
5532 * serial number check finish, we attempt to figure out
5533 * whether we still have the same device.
5535 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5537 MD5Init(&softc->context);
5538 MD5Update(&softc->context, (unsigned char *)inq_buf,
5539 sizeof(struct scsi_inquiry_data));
5540 softc->flags |= PROBE_INQUIRY_CKSUM;
5541 if (periph->path->device->serial_num_len > 0) {
5542 MD5Update(&softc->context,
5543 periph->path->device->serial_num,
5544 periph->path->device->serial_num_len);
5545 softc->flags |= PROBE_SERIAL_CKSUM;
5547 MD5Final(softc->digest, &softc->context);
5550 if (softc->action == PROBE_INQUIRY)
5551 inquiry_len = SHORT_INQUIRY_LENGTH;
5553 inquiry_len = inq_buf->additional_length + 4;
5559 (u_int8_t *)inq_buf,
5564 /*timeout*/60 * 1000);
5567 case PROBE_MODE_SENSE:
5572 mode_buf_len = sizeof(struct scsi_mode_header_6)
5573 + sizeof(struct scsi_mode_blk_desc)
5574 + sizeof(struct scsi_control_page);
5575 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5576 if (mode_buf != NULL) {
5577 scsi_mode_sense(csio,
5582 SMS_PAGE_CTRL_CURRENT,
5583 SMS_CONTROL_MODE_PAGE,
5590 xpt_print_path(periph->path);
5591 printf("Unable to mode sense control page - malloc failure\n");
5592 softc->action = PROBE_SERIAL_NUM;
5595 case PROBE_SERIAL_NUM:
5597 struct scsi_vpd_unit_serial_number *serial_buf;
5598 struct cam_ed* device;
5601 device = periph->path->device;
5602 device->serial_num = NULL;
5603 device->serial_num_len = 0;
5605 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5606 serial_buf = (struct scsi_vpd_unit_serial_number *)
5607 malloc(sizeof(*serial_buf), M_TEMP,
5610 if (serial_buf != NULL) {
5615 (u_int8_t *)serial_buf,
5616 sizeof(*serial_buf),
5618 SVPD_UNIT_SERIAL_NUMBER,
5620 /*timeout*/60 * 1000);
5624 * We'll have to do without, let our probedone
5625 * routine finish up for us.
5627 start_ccb->csio.data_ptr = NULL;
5628 probedone(periph, start_ccb);
5632 xpt_action(start_ccb);
5636 proberequestdefaultnegotiation(struct cam_periph *periph)
5638 struct ccb_trans_settings cts;
5640 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5641 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5642 #ifdef CAM_NEW_TRAN_CODE
5643 cts.type = CTS_TYPE_USER_SETTINGS;
5644 #else /* CAM_NEW_TRAN_CODE */
5645 cts.flags = CCB_TRANS_USER_SETTINGS;
5646 #endif /* CAM_NEW_TRAN_CODE */
5647 xpt_action((union ccb *)&cts);
5648 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5649 #ifdef CAM_NEW_TRAN_CODE
5650 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5651 #else /* CAM_NEW_TRAN_CODE */
5652 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5653 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5654 #endif /* CAM_NEW_TRAN_CODE */
5655 xpt_action((union ccb *)&cts);
5659 probedone(struct cam_periph *periph, union ccb *done_ccb)
5662 struct cam_path *path;
5665 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5667 softc = (probe_softc *)periph->softc;
5668 path = done_ccb->ccb_h.path;
5669 priority = done_ccb->ccb_h.pinfo.priority;
5671 switch (softc->action) {
5674 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5676 if (cam_periph_error(done_ccb, 0,
5677 SF_NO_PRINT, NULL) == ERESTART)
5679 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5680 /* Don't wedge the queue */
5681 xpt_release_devq(done_ccb->ccb_h.path,
5685 softc->action = PROBE_INQUIRY;
5686 xpt_release_ccb(done_ccb);
5687 xpt_schedule(periph, priority);
5691 case PROBE_FULL_INQUIRY:
5693 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5694 struct scsi_inquiry_data *inq_buf;
5695 u_int8_t periph_qual;
5696 u_int8_t periph_dtype;
5698 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5699 inq_buf = &path->device->inq_data;
5701 periph_qual = SID_QUAL(inq_buf);
5702 periph_dtype = SID_TYPE(inq_buf);
5704 if (periph_dtype != T_NODEVICE) {
5705 switch(periph_qual) {
5706 case SID_QUAL_LU_CONNECTED:
5711 * We conservatively request only
5712 * SHORT_INQUIRY_LEN bytes of inquiry
5713 * information during our first try
5714 * at sending an INQUIRY. If the device
5715 * has more information to give,
5716 * perform a second request specifying
5717 * the amount of information the device
5718 * is willing to give.
5720 alen = inq_buf->additional_length;
5721 if (softc->action == PROBE_INQUIRY
5722 && alen > (SHORT_INQUIRY_LENGTH - 4)) {
5725 xpt_release_ccb(done_ccb);
5726 xpt_schedule(periph, priority);
5730 xpt_find_quirk(path->device);
5732 #ifdef CAM_NEW_TRAN_CODE
5733 xpt_devise_transport(path);
5734 #endif /* CAM_NEW_TRAN_CODE */
5735 if ((inq_buf->flags & SID_CmdQue) != 0)
5742 path->device->flags &=
5743 ~CAM_DEV_UNCONFIGURED;
5745 xpt_release_ccb(done_ccb);
5746 xpt_schedule(periph, priority);
5753 } else if (cam_periph_error(done_ccb, 0,
5754 done_ccb->ccb_h.target_lun > 0
5755 ? SF_RETRY_UA|SF_QUIET_IR
5757 &softc->saved_ccb) == ERESTART) {
5759 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5760 /* Don't wedge the queue */
5761 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5765 * If we get to this point, we got an error status back
5766 * from the inquiry and the error status doesn't require
5767 * automatically retrying the command. Therefore, the
5768 * inquiry failed. If we had inquiry information before
5769 * for this device, but this latest inquiry command failed,
5770 * the device has probably gone away. If this device isn't
5771 * already marked unconfigured, notify the peripheral
5772 * drivers that this device is no more.
5774 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5775 /* Send the async notification. */
5776 xpt_async(AC_LOST_DEVICE, path, NULL);
5778 xpt_release_ccb(done_ccb);
5781 case PROBE_MODE_SENSE:
5783 struct ccb_scsiio *csio;
5784 struct scsi_mode_header_6 *mode_hdr;
5786 csio = &done_ccb->csio;
5787 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5788 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5789 struct scsi_control_page *page;
5792 offset = ((u_int8_t *)&mode_hdr[1])
5793 + mode_hdr->blk_desc_len;
5794 page = (struct scsi_control_page *)offset;
5795 path->device->queue_flags = page->queue_flags;
5796 } else if (cam_periph_error(done_ccb, 0,
5797 SF_RETRY_UA|SF_NO_PRINT,
5798 &softc->saved_ccb) == ERESTART) {
5800 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5801 /* Don't wedge the queue */
5802 xpt_release_devq(done_ccb->ccb_h.path,
5803 /*count*/1, /*run_queue*/TRUE);
5805 xpt_release_ccb(done_ccb);
5806 free(mode_hdr, M_TEMP);
5807 softc->action = PROBE_SERIAL_NUM;
5808 xpt_schedule(periph, priority);
5811 case PROBE_SERIAL_NUM:
5813 struct ccb_scsiio *csio;
5814 struct scsi_vpd_unit_serial_number *serial_buf;
5821 csio = &done_ccb->csio;
5822 priority = done_ccb->ccb_h.pinfo.priority;
5824 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5826 /* Clean up from previous instance of this device */
5827 if (path->device->serial_num != NULL) {
5828 free(path->device->serial_num, M_DEVBUF);
5829 path->device->serial_num = NULL;
5830 path->device->serial_num_len = 0;
5833 if (serial_buf == NULL) {
5835 * Don't process the command as it was never sent
5837 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5838 && (serial_buf->length > 0)) {
5841 path->device->serial_num =
5842 (u_int8_t *)malloc((serial_buf->length + 1),
5843 M_DEVBUF, M_NOWAIT);
5844 if (path->device->serial_num != NULL) {
5845 bcopy(serial_buf->serial_num,
5846 path->device->serial_num,
5847 serial_buf->length);
5848 path->device->serial_num_len =
5850 path->device->serial_num[serial_buf->length]
5853 } else if (cam_periph_error(done_ccb, 0,
5854 SF_RETRY_UA|SF_NO_PRINT,
5855 &softc->saved_ccb) == ERESTART) {
5857 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5858 /* Don't wedge the queue */
5859 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5864 * Let's see if we have seen this device before.
5866 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5868 u_int8_t digest[16];
5873 (unsigned char *)&path->device->inq_data,
5874 sizeof(struct scsi_inquiry_data));
5877 MD5Update(&context, serial_buf->serial_num,
5878 serial_buf->length);
5880 MD5Final(digest, &context);
5881 if (bcmp(softc->digest, digest, 16) == 0)
5885 * XXX Do we need to do a TUR in order to ensure
5886 * that the device really hasn't changed???
5889 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5890 xpt_async(AC_LOST_DEVICE, path, NULL);
5892 if (serial_buf != NULL)
5893 free(serial_buf, M_TEMP);
5897 * Now that we have all the necessary
5898 * information to safely perform transfer
5899 * negotiations... Controllers don't perform
5900 * any negotiation or tagged queuing until
5901 * after the first XPT_SET_TRAN_SETTINGS ccb is
5902 * received. So, on a new device, just retreive
5903 * the user settings, and set them as the current
5904 * settings to set the device up.
5906 proberequestdefaultnegotiation(periph);
5907 xpt_release_ccb(done_ccb);
5910 * Perform a TUR to allow the controller to
5911 * perform any necessary transfer negotiation.
5913 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5914 xpt_schedule(periph, priority);
5917 xpt_release_ccb(done_ccb);
5920 case PROBE_TUR_FOR_NEGOTIATION:
5921 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5922 /* Don't wedge the queue */
5923 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5927 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5929 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5930 /* Inform the XPT that a new device has been found */
5931 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5932 xpt_action(done_ccb);
5934 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5936 xpt_release_ccb(done_ccb);
5939 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5940 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5941 done_ccb->ccb_h.status = CAM_REQ_CMP;
5943 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5944 cam_periph_invalidate(periph);
5945 cam_periph_release(periph);
5947 probeschedule(periph);
5952 probecleanup(struct cam_periph *periph)
5954 free(periph->softc, M_TEMP);
5958 xpt_find_quirk(struct cam_ed *device)
5962 match = cam_quirkmatch((caddr_t)&device->inq_data,
5963 (caddr_t)xpt_quirk_table,
5964 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5965 sizeof(*xpt_quirk_table), scsi_inquiry_match);
5968 panic("xpt_find_quirk: device didn't match wildcard entry!!");
5970 device->quirk = (struct xpt_quirk_entry *)match;
5973 #ifdef CAM_NEW_TRAN_CODE
5976 xpt_devise_transport(struct cam_path *path)
5978 struct ccb_pathinq cpi;
5979 struct ccb_trans_settings cts;
5980 struct scsi_inquiry_data *inq_buf;
5982 /* Get transport information from the SIM */
5983 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5984 cpi.ccb_h.func_code = XPT_PATH_INQ;
5985 xpt_action((union ccb *)&cpi);
5988 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
5989 inq_buf = &path->device->inq_data;
5990 path->device->protocol = PROTO_SCSI;
5991 path->device->protocol_version =
5992 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
5993 path->device->transport = cpi.transport;
5994 path->device->transport_version = cpi.transport_version;
5997 * Any device not using SPI3 features should
5998 * be considered SPI2 or lower.
6000 if (inq_buf != NULL) {
6001 if (path->device->transport == XPORT_SPI
6002 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6003 && path->device->transport_version > 2)
6004 path->device->transport_version = 2;
6006 struct cam_ed* otherdev;
6008 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6010 otherdev = TAILQ_NEXT(otherdev, links)) {
6011 if (otherdev != path->device)
6015 if (otherdev != NULL) {
6017 * Initially assume the same versioning as
6018 * prior luns for this target.
6020 path->device->protocol_version =
6021 otherdev->protocol_version;
6022 path->device->transport_version =
6023 otherdev->transport_version;
6025 /* Until we know better, opt for safty */
6026 path->device->protocol_version = 2;
6027 if (path->device->transport == XPORT_SPI)
6028 path->device->transport_version = 2;
6030 path->device->transport_version = 0;
6036 * For a device compliant with SPC-2 we should be able
6037 * to determine the transport version supported by
6038 * scrutinizing the version descriptors in the
6042 /* Tell the controller what we think */
6043 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6044 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6045 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6046 cts.transport = path->device->transport;
6047 cts.transport_version = path->device->transport_version;
6048 cts.protocol = path->device->protocol;
6049 cts.protocol_version = path->device->protocol_version;
6050 cts.proto_specific.valid = 0;
6051 cts.xport_specific.valid = 0;
6052 xpt_action((union ccb *)&cts);
6056 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6059 struct ccb_pathinq cpi;
6060 struct ccb_trans_settings cur_cts;
6061 struct ccb_trans_settings_scsi *scsi;
6062 struct ccb_trans_settings_scsi *cur_scsi;
6063 struct cam_sim *sim;
6064 struct scsi_inquiry_data *inq_data;
6066 if (device == NULL) {
6067 cts->ccb_h.status = CAM_PATH_INVALID;
6068 xpt_done((union ccb *)cts);
6072 if (cts->protocol == PROTO_UNKNOWN
6073 || cts->protocol == PROTO_UNSPECIFIED) {
6074 cts->protocol = device->protocol;
6075 cts->protocol_version = device->protocol_version;
6078 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6079 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6080 cts->protocol_version = device->protocol_version;
6082 if (cts->protocol != device->protocol) {
6083 xpt_print_path(cts->ccb_h.path);
6084 printf("Uninitialized Protocol %x:%x?\n",
6085 cts->protocol, device->protocol);
6086 cts->protocol = device->protocol;
6089 if (cts->protocol_version > device->protocol_version) {
6091 xpt_print_path(cts->ccb_h.path);
6092 printf("Down reving Protocol Version from %d to %d?\n",
6093 cts->protocol_version, device->protocol_version);
6095 cts->protocol_version = device->protocol_version;
6098 if (cts->transport == XPORT_UNKNOWN
6099 || cts->transport == XPORT_UNSPECIFIED) {
6100 cts->transport = device->transport;
6101 cts->transport_version = device->transport_version;
6104 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6105 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6106 cts->transport_version = device->transport_version;
6108 if (cts->transport != device->transport) {
6109 xpt_print_path(cts->ccb_h.path);
6110 printf("Uninitialized Transport %x:%x?\n",
6111 cts->transport, device->transport);
6112 cts->transport = device->transport;
6115 if (cts->transport_version > device->transport_version) {
6117 xpt_print_path(cts->ccb_h.path);
6118 printf("Down reving Transport Version from %d to %d?\n",
6119 cts->transport_version,
6120 device->transport_version);
6122 cts->transport_version = device->transport_version;
6125 sim = cts->ccb_h.path->bus->sim;
6128 * Nothing more of interest to do unless
6129 * this is a device connected via the
6132 if (cts->protocol != PROTO_SCSI) {
6133 if (async_update == FALSE)
6134 (*(sim->sim_action))(sim, (union ccb *)cts);
6138 inq_data = &device->inq_data;
6139 scsi = &cts->proto_specific.scsi;
6140 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6141 cpi.ccb_h.func_code = XPT_PATH_INQ;
6142 xpt_action((union ccb *)&cpi);
6144 /* SCSI specific sanity checking */
6145 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6146 || (inq_data->flags & SID_CmdQue) == 0
6147 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6148 || (device->quirk->mintags == 0)) {
6150 * Can't tag on hardware that doesn't support tags,
6151 * doesn't have it enabled, or has broken tag support.
6153 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6156 if (async_update == FALSE) {
6158 * Perform sanity checking against what the
6159 * controller and device can do.
6161 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6162 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6163 cur_cts.type = cts->type;
6164 xpt_action((union ccb *)&cur_cts);
6166 cur_scsi = &cur_cts.proto_specific.scsi;
6167 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6168 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6169 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6171 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6172 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6175 /* SPI specific sanity checking */
6176 if (cts->transport == XPORT_SPI
6177 && async_update == FALSE) {
6179 struct ccb_trans_settings_spi *spi;
6180 struct ccb_trans_settings_spi *cur_spi;
6182 spi = &cts->xport_specific.spi;
6184 cur_spi = &cur_cts.xport_specific.spi;
6186 /* Fill in any gaps in what the user gave us */
6187 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6188 spi->sync_period = cur_spi->sync_period;
6189 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6190 spi->sync_period = 0;
6191 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6192 spi->sync_offset = cur_spi->sync_offset;
6193 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6194 spi->sync_offset = 0;
6195 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6196 spi->ppr_options = cur_spi->ppr_options;
6197 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6198 spi->ppr_options = 0;
6199 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6200 spi->bus_width = cur_spi->bus_width;
6201 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6203 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6204 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6205 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6207 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6208 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6209 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6210 && (inq_data->flags & SID_Sync) == 0
6211 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6212 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6213 || (cts->sync_offset == 0)
6214 || (cts->sync_period == 0)) {
6216 spi->sync_period = 0;
6217 spi->sync_offset = 0;
6220 switch (spi->bus_width) {
6221 case MSG_EXT_WDTR_BUS_32_BIT:
6222 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6223 || (inq_data->flags & SID_WBus32) != 0
6224 || cts->type == CTS_TYPE_USER_SETTINGS)
6225 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6227 /* Fall Through to 16-bit */
6228 case MSG_EXT_WDTR_BUS_16_BIT:
6229 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6230 || (inq_data->flags & SID_WBus16) != 0
6231 || cts->type == CTS_TYPE_USER_SETTINGS)
6232 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6233 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6236 /* Fall Through to 8-bit */
6237 default: /* New bus width?? */
6238 case MSG_EXT_WDTR_BUS_8_BIT:
6239 /* All targets can do this */
6240 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6244 spi3caps = cpi.xport_specific.spi.ppr_options;
6245 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6246 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6247 spi3caps &= inq_data->spi3data;
6249 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6250 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6252 if ((spi3caps & SID_SPI_IUS) == 0)
6253 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6255 if ((spi3caps & SID_SPI_QAS) == 0)
6256 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6258 /* No SPI Transfer settings are allowed unless we are wide */
6259 if (spi->bus_width == 0)
6260 spi->ppr_options = 0;
6262 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6264 * Can't tag queue without disconnection.
6266 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6267 scsi->valid |= CTS_SCSI_VALID_TQ;
6271 * If we are currently performing tagged transactions to
6272 * this device and want to change its negotiation parameters,
6273 * go non-tagged for a bit to give the controller a chance to
6274 * negotiate unhampered by tag messages.
6276 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6277 && (device->inq_flags & SID_CmdQue) != 0
6278 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6279 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6280 CTS_SPI_VALID_SYNC_OFFSET|
6281 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6282 xpt_toggle_tags(cts->ccb_h.path);
6285 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6286 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6290 * If we are transitioning from tags to no-tags or
6291 * vice-versa, we need to carefully freeze and restart
6292 * the queue so that we don't overlap tagged and non-tagged
6293 * commands. We also temporarily stop tags if there is
6294 * a change in transfer negotiation settings to allow
6295 * "tag-less" negotiation.
6297 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6298 || (device->inq_flags & SID_CmdQue) != 0)
6299 device_tagenb = TRUE;
6301 device_tagenb = FALSE;
6303 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6304 && device_tagenb == FALSE)
6305 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6306 && device_tagenb == TRUE)) {
6308 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6310 * Delay change to use tags until after a
6311 * few commands have gone to this device so
6312 * the controller has time to perform transfer
6313 * negotiations without tagged messages getting
6316 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6317 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6319 struct ccb_relsim crs;
6321 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6322 device->inq_flags &= ~SID_CmdQue;
6323 xpt_dev_ccbq_resize(cts->ccb_h.path,
6324 sim->max_dev_openings);
6325 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6326 device->tag_delay_count = 0;
6328 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6330 crs.ccb_h.func_code = XPT_REL_SIMQ;
6331 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6333 = crs.release_timeout
6336 xpt_action((union ccb *)&crs);
6340 if (async_update == FALSE)
6341 (*(sim->sim_action))(sim, (union ccb *)cts);
6344 #else /* CAM_NEW_TRAN_CODE */
6347 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6350 struct cam_sim *sim;
6353 sim = cts->ccb_h.path->bus->sim;
6354 if (async_update == FALSE) {
6355 struct scsi_inquiry_data *inq_data;
6356 struct ccb_pathinq cpi;
6357 struct ccb_trans_settings cur_cts;
6359 if (device == NULL) {
6360 cts->ccb_h.status = CAM_PATH_INVALID;
6361 xpt_done((union ccb *)cts);
6366 * Perform sanity checking against what the
6367 * controller and device can do.
6369 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6370 cpi.ccb_h.func_code = XPT_PATH_INQ;
6371 xpt_action((union ccb *)&cpi);
6372 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6373 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6374 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6375 xpt_action((union ccb *)&cur_cts);
6376 inq_data = &device->inq_data;
6378 /* Fill in any gaps in what the user gave us */
6379 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6380 cts->sync_period = cur_cts.sync_period;
6381 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6382 cts->sync_offset = cur_cts.sync_offset;
6383 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6384 cts->bus_width = cur_cts.bus_width;
6385 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6386 cts->flags &= ~CCB_TRANS_DISC_ENB;
6387 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6389 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6390 cts->flags &= ~CCB_TRANS_TAG_ENB;
6391 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6394 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6395 && (inq_data->flags & SID_Sync) == 0)
6396 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6397 || (cts->sync_offset == 0)
6398 || (cts->sync_period == 0)) {
6400 cts->sync_period = 0;
6401 cts->sync_offset = 0;
6402 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6403 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6404 && cts->sync_period <= 0x9) {
6406 * Don't allow DT transmission rates if the
6407 * device does not support it.
6409 cts->sync_period = 0xa;
6412 switch (cts->bus_width) {
6413 case MSG_EXT_WDTR_BUS_32_BIT:
6414 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6415 || (inq_data->flags & SID_WBus32) != 0)
6416 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6418 /* Fall Through to 16-bit */
6419 case MSG_EXT_WDTR_BUS_16_BIT:
6420 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6421 || (inq_data->flags & SID_WBus16) != 0)
6422 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6423 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6426 /* Fall Through to 8-bit */
6427 default: /* New bus width?? */
6428 case MSG_EXT_WDTR_BUS_8_BIT:
6429 /* All targets can do this */
6430 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6434 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6436 * Can't tag queue without disconnection.
6438 cts->flags &= ~CCB_TRANS_TAG_ENB;
6439 cts->valid |= CCB_TRANS_TQ_VALID;
6442 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6443 || (inq_data->flags & SID_CmdQue) == 0
6444 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6445 || (device->quirk->mintags == 0)) {
6447 * Can't tag on hardware that doesn't support,
6448 * doesn't have it enabled, or has broken tag support.
6450 cts->flags &= ~CCB_TRANS_TAG_ENB;
6455 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6459 * If we are transitioning from tags to no-tags or
6460 * vice-versa, we need to carefully freeze and restart
6461 * the queue so that we don't overlap tagged and non-tagged
6462 * commands. We also temporarily stop tags if there is
6463 * a change in transfer negotiation settings to allow
6464 * "tag-less" negotiation.
6466 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6467 || (device->inq_flags & SID_CmdQue) != 0)
6468 device_tagenb = TRUE;
6470 device_tagenb = FALSE;
6472 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6473 && device_tagenb == FALSE)
6474 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6475 && device_tagenb == TRUE)) {
6477 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6479 * Delay change to use tags until after a
6480 * few commands have gone to this device so
6481 * the controller has time to perform transfer
6482 * negotiations without tagged messages getting
6485 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6486 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6488 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6490 device->inq_flags &= ~SID_CmdQue;
6491 xpt_dev_ccbq_resize(cts->ccb_h.path,
6492 sim->max_dev_openings);
6493 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6494 device->tag_delay_count = 0;
6499 if (async_update == FALSE) {
6501 * If we are currently performing tagged transactions to
6502 * this device and want to change its negotiation parameters,
6503 * go non-tagged for a bit to give the controller a chance to
6504 * negotiate unhampered by tag messages.
6506 if ((device->inq_flags & SID_CmdQue) != 0
6507 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6508 CCB_TRANS_SYNC_OFFSET_VALID|
6509 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6510 xpt_toggle_tags(cts->ccb_h.path);
6512 (*(sim->sim_action))(sim, (union ccb *)cts);
6516 struct ccb_relsim crs;
6518 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6520 crs.ccb_h.func_code = XPT_REL_SIMQ;
6521 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6523 = crs.release_timeout
6526 xpt_action((union ccb *)&crs);
6531 #endif /* CAM_NEW_TRAN_CODE */
6534 xpt_toggle_tags(struct cam_path *path)
6539 * Give controllers a chance to renegotiate
6540 * before starting tag operations. We
6541 * "toggle" tagged queuing off then on
6542 * which causes the tag enable command delay
6543 * counter to come into effect.
6546 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6547 || ((dev->inq_flags & SID_CmdQue) != 0
6548 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6549 struct ccb_trans_settings cts;
6551 xpt_setup_ccb(&cts.ccb_h, path, 1);
6552 #ifdef CAM_NEW_TRAN_CODE
6553 cts.protocol = PROTO_SCSI;
6554 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6555 cts.transport = XPORT_UNSPECIFIED;
6556 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6557 cts.proto_specific.scsi.flags = 0;
6558 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6559 #else /* CAM_NEW_TRAN_CODE */
6561 cts.valid = CCB_TRANS_TQ_VALID;
6562 #endif /* CAM_NEW_TRAN_CODE */
6563 xpt_set_transfer_settings(&cts, path->device,
6564 /*async_update*/TRUE);
6565 #ifdef CAM_NEW_TRAN_CODE
6566 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6567 #else /* CAM_NEW_TRAN_CODE */
6568 cts.flags = CCB_TRANS_TAG_ENB;
6569 #endif /* CAM_NEW_TRAN_CODE */
6570 xpt_set_transfer_settings(&cts, path->device,
6571 /*async_update*/TRUE);
6576 xpt_start_tags(struct cam_path *path)
6578 struct ccb_relsim crs;
6579 struct cam_ed *device;
6580 struct cam_sim *sim;
6583 device = path->device;
6584 sim = path->bus->sim;
6585 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6586 xpt_freeze_devq(path, /*count*/1);
6587 device->inq_flags |= SID_CmdQue;
6588 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
6589 xpt_dev_ccbq_resize(path, newopenings);
6590 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6591 crs.ccb_h.func_code = XPT_REL_SIMQ;
6592 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6594 = crs.release_timeout
6597 xpt_action((union ccb *)&crs);
6600 static int busses_to_config;
6601 static int busses_to_reset;
6604 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6606 if (bus->path_id != CAM_XPT_PATH_ID) {
6607 struct cam_path path;
6608 struct ccb_pathinq cpi;
6612 xpt_compile_path(&path, NULL, bus->path_id,
6613 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6614 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6615 cpi.ccb_h.func_code = XPT_PATH_INQ;
6616 xpt_action((union ccb *)&cpi);
6617 can_negotiate = cpi.hba_inquiry;
6618 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6619 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6622 xpt_release_path(&path);
6629 xptconfigfunc(struct cam_eb *bus, void *arg)
6631 struct cam_path *path;
6632 union ccb *work_ccb;
6634 if (bus->path_id != CAM_XPT_PATH_ID) {
6638 work_ccb = xpt_alloc_ccb();
6639 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6640 CAM_TARGET_WILDCARD,
6641 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6642 printf("xptconfigfunc: xpt_create_path failed with "
6643 "status %#x for bus %d\n", status, bus->path_id);
6644 printf("xptconfigfunc: halting bus configuration\n");
6645 xpt_free_ccb(work_ccb);
6647 xpt_finishconfig(xpt_periph, NULL);
6650 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6651 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6652 xpt_action(work_ccb);
6653 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6654 printf("xptconfigfunc: CPI failed on bus %d "
6655 "with status %d\n", bus->path_id,
6656 work_ccb->ccb_h.status);
6657 xpt_finishconfig(xpt_periph, work_ccb);
6661 can_negotiate = work_ccb->cpi.hba_inquiry;
6662 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6663 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6664 && (can_negotiate != 0)) {
6665 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6666 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6667 work_ccb->ccb_h.cbfcnp = NULL;
6668 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6669 ("Resetting Bus\n"));
6670 xpt_action(work_ccb);
6671 xpt_finishconfig(xpt_periph, work_ccb);
6673 /* Act as though we performed a successful BUS RESET */
6674 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6675 xpt_finishconfig(xpt_periph, work_ccb);
6683 xpt_config(void *arg)
6686 * Now that interrupts are enabled, go find our devices
6690 /* Setup debugging flags and path */
6691 #ifdef CAM_DEBUG_FLAGS
6692 cam_dflags = CAM_DEBUG_FLAGS;
6693 #else /* !CAM_DEBUG_FLAGS */
6694 cam_dflags = CAM_DEBUG_NONE;
6695 #endif /* CAM_DEBUG_FLAGS */
6696 #ifdef CAM_DEBUG_BUS
6697 if (cam_dflags != CAM_DEBUG_NONE) {
6698 if (xpt_create_path(&cam_dpath, xpt_periph,
6699 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6700 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6701 printf("xpt_config: xpt_create_path() failed for debug"
6702 " target %d:%d:%d, debugging disabled\n",
6703 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6704 cam_dflags = CAM_DEBUG_NONE;
6708 #else /* !CAM_DEBUG_BUS */
6710 #endif /* CAM_DEBUG_BUS */
6711 #endif /* CAMDEBUG */
6714 * Scan all installed busses.
6716 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6718 if (busses_to_config == 0) {
6719 /* Call manually because we don't have any busses */
6720 xpt_finishconfig(xpt_periph, NULL);
6722 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6723 printf("Waiting %d seconds for SCSI "
6724 "devices to settle\n", SCSI_DELAY/1000);
6726 xpt_for_all_busses(xptconfigfunc, NULL);
6731 * If the given device only has one peripheral attached to it, and if that
6732 * peripheral is the passthrough driver, announce it. This insures that the
6733 * user sees some sort of announcement for every peripheral in their system.
6736 xptpassannouncefunc(struct cam_ed *device, void *arg)
6738 struct cam_periph *periph;
6741 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6742 periph = SLIST_NEXT(periph, periph_links), i++);
6744 periph = SLIST_FIRST(&device->periphs);
6746 && (strncmp(periph->periph_name, "pass", 4) == 0))
6747 xpt_announce_periph(periph, NULL);
6753 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6755 struct periph_driver **p_drv;
6758 if (done_ccb != NULL) {
6759 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6760 ("xpt_finishconfig\n"));
6761 switch(done_ccb->ccb_h.func_code) {
6763 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6764 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6765 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6766 xpt_action(done_ccb);
6772 xpt_free_path(done_ccb->ccb_h.path);
6778 if (busses_to_config == 0) {
6779 /* Register all the peripheral drivers */
6780 /* XXX This will have to change when we have loadable modules */
6781 p_drv = periph_drivers;
6782 for (i = 0; p_drv[i] != NULL; i++) {
6783 (*p_drv[i]->init)();
6787 * Check for devices with no "standard" peripheral driver
6788 * attached. For any devices like that, announce the
6789 * passthrough driver so the user will see something.
6791 xpt_for_all_devices(xptpassannouncefunc, NULL);
6793 /* Release our hook so that the boot can continue. */
6794 config_intrhook_disestablish(xpt_config_hook);
6795 free(xpt_config_hook, M_TEMP);
6796 xpt_config_hook = NULL;
6798 if (done_ccb != NULL)
6799 xpt_free_ccb(done_ccb);
6803 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6805 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6807 switch (work_ccb->ccb_h.func_code) {
6808 /* Common cases first */
6809 case XPT_PATH_INQ: /* Path routing inquiry */
6811 struct ccb_pathinq *cpi;
6813 cpi = &work_ccb->cpi;
6814 cpi->version_num = 1; /* XXX??? */
6815 cpi->hba_inquiry = 0;
6816 cpi->target_sprt = 0;
6818 cpi->hba_eng_cnt = 0;
6819 cpi->max_target = 0;
6821 cpi->initiator_id = 0;
6822 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6823 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6824 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6825 cpi->unit_number = sim->unit_number;
6826 cpi->bus_id = sim->bus_id;
6827 cpi->base_transfer_speed = 0;
6828 #ifdef CAM_NEW_TRAN_CODE
6829 cpi->protocol = PROTO_UNSPECIFIED;
6830 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6831 cpi->transport = XPORT_UNSPECIFIED;
6832 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6833 #endif /* CAM_NEW_TRAN_CODE */
6834 cpi->ccb_h.status = CAM_REQ_CMP;
6839 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6846 * The xpt as a "controller" has no interrupt sources, so polling
6850 xptpoll(struct cam_sim *sim)
6855 camisr(void *V_queue)
6857 cam_isrq_t *queue = V_queue;
6859 struct ccb_hdr *ccb_h;
6862 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6865 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6866 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6869 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6874 if (ccb_h->flags & CAM_HIGH_POWER) {
6875 struct highpowerlist *hphead;
6876 struct cam_ed *device;
6877 union ccb *send_ccb;
6879 hphead = &highpowerq;
6881 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6884 * Increment the count since this command is done.
6889 * Any high powered commands queued up?
6891 if (send_ccb != NULL) {
6892 device = send_ccb->ccb_h.path->device;
6894 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6896 xpt_release_devq(send_ccb->ccb_h.path,
6897 /*count*/1, /*runqueue*/TRUE);
6900 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6903 dev = ccb_h->path->device;
6906 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6908 ccb_h->path->bus->sim->devq->send_active--;
6909 ccb_h->path->bus->sim->devq->send_openings++;
6912 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6913 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
6914 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6915 && (dev->ccbq.dev_active == 0))) {
6917 xpt_release_devq(ccb_h->path, /*count*/1,
6921 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6922 && (--dev->tag_delay_count == 0))
6923 xpt_start_tags(ccb_h->path);
6925 if ((dev->ccbq.queue.entries > 0)
6926 && (dev->qfrozen_cnt == 0)
6927 && (device_is_send_queued(dev) == 0)) {
6928 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6933 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6934 xpt_release_simq(ccb_h->path->bus->sim,
6936 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6940 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6941 && (ccb_h->status & CAM_DEV_QFRZN)) {
6942 xpt_release_devq(ccb_h->path, /*count*/1,
6944 ccb_h->status &= ~CAM_DEV_QFRZN;
6946 xpt_run_dev_sendq(ccb_h->path->bus);
6949 /* Call the peripheral driver's callback */
6950 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6952 /* Raise IPL for while test */