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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
43 #include <sys/interrupt.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
51 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
68 /* Datastructures internal to the xpt layer */
69 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
72 * Definition of an async handler callback block. These are used to add
73 * SIMs and peripherals to the async callback lists.
76 SLIST_ENTRY(async_node) links;
77 u_int32_t event_enable; /* Async Event enables */
78 void (*callback)(void *arg, u_int32_t code,
79 struct cam_path *path, void *args);
83 SLIST_HEAD(async_list, async_node);
84 SLIST_HEAD(periph_list, cam_periph);
85 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
88 * This is the maximum number of high powered commands (e.g. start unit)
89 * that can be outstanding at a particular time.
91 #ifndef CAM_MAX_HIGHPOWER
92 #define CAM_MAX_HIGHPOWER 4
95 /* number of high powered commands that can go through right now */
96 static int num_highpower = CAM_MAX_HIGHPOWER;
99 * Structure for queueing a device in a run queue.
100 * There is one run queue for allocating new ccbs,
101 * and another for sending ccbs to the controller.
103 struct cam_ed_qinfo {
105 struct cam_ed *device;
109 * The CAM EDT (Existing Device Table) contains the device information for
110 * all devices for all busses in the system. The table contains a
111 * cam_ed structure for each device on the bus.
114 TAILQ_ENTRY(cam_ed) links;
115 struct cam_ed_qinfo alloc_ccb_entry;
116 struct cam_ed_qinfo send_ccb_entry;
117 struct cam_et *target;
120 * Queue of type drivers wanting to do
121 * work on this device.
123 struct cam_ccbq ccbq; /* Queue of pending ccbs */
124 struct async_list asyncs; /* Async callback info for this B/T/L */
125 struct periph_list periphs; /* All attached devices */
126 u_int generation; /* Generation number */
127 struct cam_periph *owner; /* Peripheral driver's ownership tag */
128 struct xpt_quirk_entry *quirk; /* Oddities about this device */
129 /* Storage for the inquiry data */
130 #ifdef CAM_NEW_TRAN_CODE
132 u_int protocol_version;
134 u_int transport_version;
135 #endif /* CAM_NEW_TRAN_CODE */
136 struct scsi_inquiry_data inq_data;
137 u_int8_t inq_flags; /*
138 * Current settings for inquiry flags.
139 * This allows us to override settings
140 * like disconnection and tagged
141 * queuing for a device.
143 u_int8_t queue_flags; /* Queue flags from the control page */
144 u_int8_t serial_num_len;
145 u_int8_t *serial_num;
146 u_int32_t qfrozen_cnt;
148 #define CAM_DEV_UNCONFIGURED 0x01
149 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
150 #define CAM_DEV_REL_ON_COMPLETE 0x04
151 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
152 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
153 #define CAM_DEV_TAG_AFTER_COUNT 0x20
154 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
155 u_int32_t tag_delay_count;
156 #define CAM_TAG_DELAY_COUNT 5
157 u_int32_t tag_saved_openings;
159 struct callout_handle c_handle;
163 * Each target is represented by an ET (Existing Target). These
164 * entries are created when a target is successfully probed with an
165 * identify, and removed when a device fails to respond after a number
166 * of retries, or a bus rescan finds the device missing.
169 TAILQ_HEAD(, cam_ed) ed_entries;
170 TAILQ_ENTRY(cam_et) links;
172 target_id_t target_id;
175 struct timeval last_reset;
179 * Each bus is represented by an EB (Existing Bus). These entries
180 * are created by calls to xpt_bus_register and deleted by calls to
181 * xpt_bus_deregister.
184 TAILQ_HEAD(, cam_et) et_entries;
185 TAILQ_ENTRY(cam_eb) links;
188 struct timeval last_reset;
190 #define CAM_EB_RUNQ_SCHEDULED 0x01
196 struct cam_periph *periph;
198 struct cam_et *target;
199 struct cam_ed *device;
202 struct xpt_quirk_entry {
203 struct scsi_inquiry_pattern inq_pat;
205 #define CAM_QUIRK_NOLUNS 0x01
206 #define CAM_QUIRK_NOSERIAL 0x02
207 #define CAM_QUIRK_HILUNS 0x04
208 #define CAM_QUIRK_NOHILUNS 0x08
213 static int cam_srch_hi = 0;
214 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
215 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
216 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
217 sysctl_cam_search_luns, "I",
218 "allow search above LUN 7 for SCSI3 and greater devices");
220 #define CAM_SCSI2_MAXLUN 8
222 * If we're not quirked to search <= the first 8 luns
223 * and we are either quirked to search above lun 8,
224 * or we're > SCSI-2 and we've enabled hilun searching,
225 * or we're > SCSI-2 and the last lun was a success,
226 * we can look for luns above lun 8.
228 #define CAN_SRCH_HI_SPARSE(dv) \
229 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
230 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
231 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
233 #define CAN_SRCH_HI_DENSE(dv) \
234 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
235 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
236 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
244 u_int32_t generation;
247 static const char quantum[] = "QUANTUM";
248 static const char sony[] = "SONY";
249 static const char west_digital[] = "WDIGTL";
250 static const char samsung[] = "SAMSUNG";
251 static const char seagate[] = "SEAGATE";
252 static const char microp[] = "MICROP";
254 static struct xpt_quirk_entry xpt_quirk_table[] =
257 /* Reports QUEUE FULL for temporary resource shortages */
258 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
259 /*quirks*/0, /*mintags*/24, /*maxtags*/32
262 /* Reports QUEUE FULL for temporary resource shortages */
263 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
264 /*quirks*/0, /*mintags*/24, /*maxtags*/32
267 /* Reports QUEUE FULL for temporary resource shortages */
268 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
269 /*quirks*/0, /*mintags*/24, /*maxtags*/32
272 /* Broken tagged queuing drive */
273 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
274 /*quirks*/0, /*mintags*/0, /*maxtags*/0
277 /* Broken tagged queuing drive */
278 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
279 /*quirks*/0, /*mintags*/0, /*maxtags*/0
282 /* Broken tagged queuing drive */
283 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
284 /*quirks*/0, /*mintags*/0, /*maxtags*/0
288 * Unfortunately, the Quantum Atlas III has the same
289 * problem as the Atlas II drives above.
290 * Reported by: "Johan Granlund" <johan@granlund.nu>
292 * For future reference, the drive with the problem was:
293 * QUANTUM QM39100TD-SW N1B0
295 * It's possible that Quantum will fix the problem in later
296 * firmware revisions. If that happens, the quirk entry
297 * will need to be made specific to the firmware revisions
301 /* Reports QUEUE FULL for temporary resource shortages */
302 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
303 /*quirks*/0, /*mintags*/24, /*maxtags*/32
307 * 18 Gig Atlas III, same problem as the 9G version.
308 * Reported by: Andre Albsmeier
309 * <andre.albsmeier@mchp.siemens.de>
311 * For future reference, the drive with the problem was:
312 * QUANTUM QM318000TD-S N491
314 /* Reports QUEUE FULL for temporary resource shortages */
315 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
316 /*quirks*/0, /*mintags*/24, /*maxtags*/32
320 * Broken tagged queuing drive
321 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
322 * and: Martin Renters <martin@tdc.on.ca>
324 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
325 /*quirks*/0, /*mintags*/0, /*maxtags*/0
328 * The Seagate Medalist Pro drives have very poor write
329 * performance with anything more than 2 tags.
331 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
332 * Drive: <SEAGATE ST36530N 1444>
334 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
335 * Drive: <SEAGATE ST34520W 1281>
337 * No one has actually reported that the 9G version
338 * (ST39140*) of the Medalist Pro has the same problem, but
339 * we're assuming that it does because the 4G and 6.5G
340 * versions of the drive are broken.
343 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
344 /*quirks*/0, /*mintags*/2, /*maxtags*/2
347 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
348 /*quirks*/0, /*mintags*/2, /*maxtags*/2
351 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
352 /*quirks*/0, /*mintags*/2, /*maxtags*/2
356 * Slow when tagged queueing is enabled. Write performance
357 * steadily drops off with more and more concurrent
358 * transactions. Best sequential write performance with
359 * tagged queueing turned off and write caching turned on.
362 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
363 * Drive: DCAS-34330 w/ "S65A" firmware.
365 * The drive with the problem had the "S65A" firmware
366 * revision, and has also been reported (by Stephen J.
367 * Roznowski <sjr@home.net>) for a drive with the "S61A"
370 * Although no one has reported problems with the 2 gig
371 * version of the DCAS drive, the assumption is that it
372 * has the same problems as the 4 gig version. Therefore
373 * this quirk entries disables tagged queueing for all
376 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
377 /*quirks*/0, /*mintags*/0, /*maxtags*/0
380 /* Broken tagged queuing drive */
381 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
382 /*quirks*/0, /*mintags*/0, /*maxtags*/0
385 /* Broken tagged queuing drive */
386 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
387 /*quirks*/0, /*mintags*/0, /*maxtags*/0
390 /* This does not support other than LUN 0 */
391 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
392 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
396 * Broken tagged queuing drive.
398 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
401 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
402 /*quirks*/0, /*mintags*/0, /*maxtags*/0
406 * Slow when tagged queueing is enabled. (1.5MB/sec versus
408 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
409 * Best performance with these drives is achieved with
410 * tagged queueing turned off, and write caching turned on.
412 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
413 /*quirks*/0, /*mintags*/0, /*maxtags*/0
417 * Slow when tagged queueing is enabled. (1.5MB/sec versus
419 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
420 * Best performance with these drives is achieved with
421 * tagged queueing turned off, and write caching turned on.
423 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
424 /*quirks*/0, /*mintags*/0, /*maxtags*/0
428 * Doesn't handle queue full condition correctly,
429 * so we need to limit maxtags to what the device
430 * can handle instead of determining this automatically.
432 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
433 /*quirks*/0, /*mintags*/2, /*maxtags*/32
436 /* Really only one LUN */
437 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
438 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
441 /* I can't believe we need a quirk for DPT volumes. */
442 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
443 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
444 /*mintags*/0, /*maxtags*/255
448 * Many Sony CDROM drives don't like multi-LUN probing.
450 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
451 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
455 * This drive doesn't like multiple LUN probing.
456 * Submitted by: Parag Patel <parag@cgt.com>
458 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
459 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
462 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
463 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
467 * The 8200 doesn't like multi-lun probing, and probably
468 * don't like serial number requests either.
471 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
474 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
478 * Let's try the same as above, but for a drive that says
479 * it's an IPL-6860 but is actually an EXB 8200.
482 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
485 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
489 * These Hitachi drives don't like multi-lun probing.
490 * The PR submitter has a DK319H, but says that the Linux
491 * kernel has a similar work-around for the DK312 and DK314,
492 * so all DK31* drives are quirked here.
494 * Submitted by: Paul Haddad <paul@pth.com>
496 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
497 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
501 * The Hitachi CJ series with J8A8 firmware apparantly has
502 * problems with tagged commands.
504 * Reported by: amagai@nue.org
506 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
507 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
511 * These are the large storage arrays.
512 * Submitted by: William Carrel <william.carrel@infospace.com>
514 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
515 CAM_QUIRK_HILUNS, 2, 1024
519 * This old revision of the TDC3600 is also SCSI-1, and
520 * hangs upon serial number probing.
523 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
526 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
530 * Maxtor Personal Storage 3000XT (Firewire)
531 * hangs upon serial number probing.
534 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
537 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
541 * Would repond to all LUNs if asked for.
544 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
547 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
551 * Would repond to all LUNs if asked for.
554 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
557 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
560 /* Submitted by: Matthew Dodd <winter@jurai.net> */
561 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
562 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
565 /* Submitted by: Matthew Dodd <winter@jurai.net> */
566 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
567 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
570 /* TeraSolutions special settings for TRC-22 RAID */
571 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
572 /*quirks*/0, /*mintags*/55, /*maxtags*/255
575 /* Veritas Storage Appliance */
576 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
577 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
581 * Would respond to all LUNs. Device type and removable
582 * flag are jumper-selectable.
584 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
587 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
590 /* EasyRAID E5A aka. areca ARC-6010 */
591 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
592 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
595 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
596 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
599 /* Default tagged queuing parameters for all devices */
601 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
602 /*vendor*/"*", /*product*/"*", /*revision*/"*"
604 /*quirks*/0, /*mintags*/2, /*maxtags*/255
608 static const int xpt_quirk_table_size =
609 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
613 DM_RET_FLAG_MASK = 0x0f,
616 DM_RET_DESCEND = 0x20,
618 DM_RET_ACTION_MASK = 0xf0
626 } xpt_traverse_depth;
628 struct xpt_traverse_config {
629 xpt_traverse_depth depth;
634 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
635 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
636 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
637 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
638 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
640 /* Transport layer configuration information */
641 static struct xpt_softc xsoftc;
643 /* Queues for our software interrupt handler */
644 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
645 static cam_isrq_t cam_bioq;
646 static struct mtx cam_bioq_lock;
648 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
649 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
650 static u_int xpt_max_ccbs; /*
651 * Maximum size of ccb pool. Modified as
652 * devices are added/removed or have their
653 * opening counts changed.
655 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
657 struct cam_periph *xpt_periph;
659 static periph_init_t xpt_periph_init;
661 static periph_init_t probe_periph_init;
663 static struct periph_driver xpt_driver =
665 xpt_periph_init, "xpt",
666 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
669 static struct periph_driver probe_driver =
671 probe_periph_init, "probe",
672 TAILQ_HEAD_INITIALIZER(probe_driver.units)
675 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
676 PERIPHDRIVER_DECLARE(probe, probe_driver);
679 static d_open_t xptopen;
680 static d_close_t xptclose;
681 static d_ioctl_t xptioctl;
683 static struct cdevsw xpt_cdevsw = {
684 .d_version = D_VERSION,
685 .d_flags = D_NEEDGIANT,
692 static struct intr_config_hook *xpt_config_hook;
694 static void dead_sim_action(struct cam_sim *sim, union ccb *ccb);
695 static void dead_sim_poll(struct cam_sim *sim);
697 /* Dummy SIM that is used when the real one has gone. */
698 static struct cam_sim cam_dead_sim = {
699 .sim_action = dead_sim_action,
700 .sim_poll = dead_sim_poll,
701 .sim_name = "dead_sim",
704 #define SIM_DEAD(sim) ((sim) == &cam_dead_sim)
706 /* Registered busses */
707 static TAILQ_HEAD(,cam_eb) xpt_busses;
708 static u_int bus_generation;
710 /* Storage for debugging datastructures */
712 struct cam_path *cam_dpath;
713 u_int32_t cam_dflags;
714 u_int32_t cam_debug_delay;
717 /* Pointers to software interrupt handlers */
718 static void *cambio_ih;
720 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
721 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
725 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
726 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
727 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
729 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
730 || defined(CAM_DEBUG_LUN)
732 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
733 || !defined(CAM_DEBUG_LUN)
734 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
736 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
737 #else /* !CAMDEBUG */
738 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
739 #endif /* CAMDEBUG */
740 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
742 /* Our boot-time initialization hook */
743 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
745 static moduledata_t cam_moduledata = {
747 cam_module_event_handler,
751 static void xpt_init(void *);
753 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
754 MODULE_VERSION(cam, 1);
757 static cam_status xpt_compile_path(struct cam_path *new_path,
758 struct cam_periph *perph,
760 target_id_t target_id,
763 static void xpt_release_path(struct cam_path *path);
765 static void xpt_async_bcast(struct async_list *async_head,
766 u_int32_t async_code,
767 struct cam_path *path,
769 static void xpt_dev_async(u_int32_t async_code,
771 struct cam_et *target,
772 struct cam_ed *device,
774 static path_id_t xptnextfreepathid(void);
775 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
776 static union ccb *xpt_get_ccb(struct cam_ed *device);
777 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
778 u_int32_t new_priority);
779 static void xpt_run_dev_allocq(struct cam_eb *bus);
780 static void xpt_run_dev_sendq(struct cam_eb *bus);
781 static timeout_t xpt_release_devq_timeout;
782 static timeout_t xpt_release_simq_timeout;
783 static void xpt_release_bus(struct cam_eb *bus);
784 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
786 static struct cam_et*
787 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
788 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
789 static struct cam_ed*
790 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
792 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
793 struct cam_ed *device);
794 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
795 static struct cam_eb*
796 xpt_find_bus(path_id_t path_id);
797 static struct cam_et*
798 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
799 static struct cam_ed*
800 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
801 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
802 static void xpt_scan_lun(struct cam_periph *periph,
803 struct cam_path *path, cam_flags flags,
805 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
806 static xpt_busfunc_t xptconfigbuscountfunc;
807 static xpt_busfunc_t xptconfigfunc;
808 static void xpt_config(void *arg);
809 static xpt_devicefunc_t xptpassannouncefunc;
810 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
811 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
812 static void xptpoll(struct cam_sim *sim);
813 static void camisr(void *);
815 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
816 static void xptasync(struct cam_periph *periph,
817 u_int32_t code, cam_path *path);
819 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
820 u_int num_patterns, struct cam_eb *bus);
821 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
823 struct cam_ed *device);
824 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
826 struct cam_periph *periph);
827 static xpt_busfunc_t xptedtbusfunc;
828 static xpt_targetfunc_t xptedttargetfunc;
829 static xpt_devicefunc_t xptedtdevicefunc;
830 static xpt_periphfunc_t xptedtperiphfunc;
831 static xpt_pdrvfunc_t xptplistpdrvfunc;
832 static xpt_periphfunc_t xptplistperiphfunc;
833 static int xptedtmatch(struct ccb_dev_match *cdm);
834 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
835 static int xptbustraverse(struct cam_eb *start_bus,
836 xpt_busfunc_t *tr_func, void *arg);
837 static int xpttargettraverse(struct cam_eb *bus,
838 struct cam_et *start_target,
839 xpt_targetfunc_t *tr_func, void *arg);
840 static int xptdevicetraverse(struct cam_et *target,
841 struct cam_ed *start_device,
842 xpt_devicefunc_t *tr_func, void *arg);
843 static int xptperiphtraverse(struct cam_ed *device,
844 struct cam_periph *start_periph,
845 xpt_periphfunc_t *tr_func, void *arg);
846 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
847 xpt_pdrvfunc_t *tr_func, void *arg);
848 static int xptpdperiphtraverse(struct periph_driver **pdrv,
849 struct cam_periph *start_periph,
850 xpt_periphfunc_t *tr_func,
852 static xpt_busfunc_t xptdefbusfunc;
853 static xpt_targetfunc_t xptdeftargetfunc;
854 static xpt_devicefunc_t xptdefdevicefunc;
855 static xpt_periphfunc_t xptdefperiphfunc;
856 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
858 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
861 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
864 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
867 static xpt_devicefunc_t xptsetasyncfunc;
868 static xpt_busfunc_t xptsetasyncbusfunc;
869 static cam_status xptregister(struct cam_periph *periph,
871 static cam_status proberegister(struct cam_periph *periph,
873 static void probeschedule(struct cam_periph *probe_periph);
874 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
875 static void proberequestdefaultnegotiation(struct cam_periph *periph);
876 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
877 static void probecleanup(struct cam_periph *periph);
878 static void xpt_find_quirk(struct cam_ed *device);
879 #ifdef CAM_NEW_TRAN_CODE
880 static void xpt_devise_transport(struct cam_path *path);
881 #endif /* CAM_NEW_TRAN_CODE */
882 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
883 struct cam_ed *device,
885 static void xpt_toggle_tags(struct cam_path *path);
886 static void xpt_start_tags(struct cam_path *path);
887 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
889 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
891 static __inline int periph_is_queued(struct cam_periph *periph);
892 static __inline int device_is_alloc_queued(struct cam_ed *device);
893 static __inline int device_is_send_queued(struct cam_ed *device);
894 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
897 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
901 if (dev->ccbq.devq_openings > 0) {
902 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
903 cam_ccbq_resize(&dev->ccbq,
904 dev->ccbq.dev_openings
905 + dev->ccbq.dev_active);
906 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
909 * The priority of a device waiting for CCB resources
910 * is that of the the highest priority peripheral driver
913 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
914 &dev->alloc_ccb_entry.pinfo,
915 CAMQ_GET_HEAD(&dev->drvq)->priority);
924 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
928 if (dev->ccbq.dev_openings > 0) {
930 * The priority of a device waiting for controller
931 * resources is that of the the highest priority CCB
935 xpt_schedule_dev(&bus->sim->devq->send_queue,
936 &dev->send_ccb_entry.pinfo,
937 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
945 periph_is_queued(struct cam_periph *periph)
947 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
951 device_is_alloc_queued(struct cam_ed *device)
953 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
957 device_is_send_queued(struct cam_ed *device)
959 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
963 dev_allocq_is_runnable(struct cam_devq *devq)
967 * Have space to do more work.
968 * Allowed to do work.
970 return ((devq->alloc_queue.qfrozen_cnt == 0)
971 && (devq->alloc_queue.entries > 0)
972 && (devq->alloc_openings > 0));
978 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
988 xptdone(struct cam_periph *periph, union ccb *done_ccb)
990 /* Caller will release the CCB */
991 wakeup(&done_ccb->ccb_h.cbfcnp);
995 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
999 unit = minor(dev) & 0xff;
1002 * Only allow read-write access.
1004 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
1008 * We don't allow nonblocking access.
1010 if ((flags & O_NONBLOCK) != 0) {
1011 printf("xpt%d: can't do nonblocking access\n", unit);
1016 * We only have one transport layer right now. If someone accesses
1017 * us via something other than minor number 1, point out their
1021 printf("xptopen: got invalid xpt unit %d\n", unit);
1025 /* Mark ourselves open */
1026 xsoftc.flags |= XPT_FLAG_OPEN;
1032 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1036 unit = minor(dev) & 0xff;
1039 * We only have one transport layer right now. If someone accesses
1040 * us via something other than minor number 1, point out their
1044 printf("xptclose: got invalid xpt unit %d\n", unit);
1048 /* Mark ourselves closed */
1049 xsoftc.flags &= ~XPT_FLAG_OPEN;
1055 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1060 unit = minor(dev) & 0xff;
1063 * We only have one transport layer right now. If someone accesses
1064 * us via something other than minor number 1, point out their
1068 printf("xptioctl: got invalid xpt unit %d\n", unit);
1074 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1075 * to accept CCB types that don't quite make sense to send through a
1076 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1079 case CAMIOCOMMAND: {
1083 inccb = (union ccb *)addr;
1085 switch(inccb->ccb_h.func_code) {
1088 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1089 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1098 ccb = xpt_alloc_ccb();
1101 * Create a path using the bus, target, and lun the
1104 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1105 inccb->ccb_h.path_id,
1106 inccb->ccb_h.target_id,
1107 inccb->ccb_h.target_lun) !=
1113 /* Ensure all of our fields are correct */
1114 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1115 inccb->ccb_h.pinfo.priority);
1116 xpt_merge_ccb(ccb, inccb);
1117 ccb->ccb_h.cbfcnp = xptdone;
1118 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1119 bcopy(ccb, inccb, sizeof(union ccb));
1120 xpt_free_path(ccb->ccb_h.path);
1128 * This is an immediate CCB, so it's okay to
1129 * allocate it on the stack.
1133 * Create a path using the bus, target, and lun the
1136 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1137 inccb->ccb_h.path_id,
1138 inccb->ccb_h.target_id,
1139 inccb->ccb_h.target_lun) !=
1144 /* Ensure all of our fields are correct */
1145 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1146 inccb->ccb_h.pinfo.priority);
1147 xpt_merge_ccb(&ccb, inccb);
1148 ccb.ccb_h.cbfcnp = xptdone;
1150 bcopy(&ccb, inccb, sizeof(union ccb));
1151 xpt_free_path(ccb.ccb_h.path);
1155 case XPT_DEV_MATCH: {
1156 struct cam_periph_map_info mapinfo;
1157 struct cam_path *old_path;
1160 * We can't deal with physical addresses for this
1161 * type of transaction.
1163 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1169 * Save this in case the caller had it set to
1170 * something in particular.
1172 old_path = inccb->ccb_h.path;
1175 * We really don't need a path for the matching
1176 * code. The path is needed because of the
1177 * debugging statements in xpt_action(). They
1178 * assume that the CCB has a valid path.
1180 inccb->ccb_h.path = xpt_periph->path;
1182 bzero(&mapinfo, sizeof(mapinfo));
1185 * Map the pattern and match buffers into kernel
1186 * virtual address space.
1188 error = cam_periph_mapmem(inccb, &mapinfo);
1191 inccb->ccb_h.path = old_path;
1196 * This is an immediate CCB, we can send it on directly.
1201 * Map the buffers back into user space.
1203 cam_periph_unmapmem(inccb, &mapinfo);
1205 inccb->ccb_h.path = old_path;
1217 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1218 * with the periphal driver name and unit name filled in. The other
1219 * fields don't really matter as input. The passthrough driver name
1220 * ("pass"), and unit number are passed back in the ccb. The current
1221 * device generation number, and the index into the device peripheral
1222 * driver list, and the status are also passed back. Note that
1223 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1224 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1225 * (or rather should be) impossible for the device peripheral driver
1226 * list to change since we look at the whole thing in one pass, and
1227 * we do it with splcam protection.
1230 case CAMGETPASSTHRU: {
1232 struct cam_periph *periph;
1233 struct periph_driver **p_drv;
1236 u_int cur_generation;
1237 int base_periph_found;
1241 ccb = (union ccb *)addr;
1242 unit = ccb->cgdl.unit_number;
1243 name = ccb->cgdl.periph_name;
1245 * Every 100 devices, we want to drop our spl protection to
1246 * give the software interrupt handler a chance to run.
1247 * Most systems won't run into this check, but this should
1248 * avoid starvation in the software interrupt handler in
1253 ccb = (union ccb *)addr;
1255 base_periph_found = 0;
1258 * Sanity check -- make sure we don't get a null peripheral
1261 if (*ccb->cgdl.periph_name == '\0') {
1266 /* Keep the list from changing while we traverse it */
1269 cur_generation = xsoftc.generation;
1271 /* first find our driver in the list of drivers */
1272 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1273 if (strcmp((*p_drv)->driver_name, name) == 0)
1276 if (*p_drv == 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;
1287 * Run through every peripheral instance of this driver
1288 * and check to see whether it matches the unit passed
1289 * in by the user. If it does, get out of the loops and
1290 * find the passthrough driver associated with that
1291 * peripheral driver.
1293 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1294 periph = TAILQ_NEXT(periph, unit_links)) {
1296 if (periph->unit_number == unit) {
1298 } else if (--splbreaknum == 0) {
1302 if (cur_generation != xsoftc.generation)
1307 * If we found the peripheral driver that the user passed
1308 * in, go through all of the peripheral drivers for that
1309 * particular device and look for a passthrough driver.
1311 if (periph != NULL) {
1312 struct cam_ed *device;
1315 base_periph_found = 1;
1316 device = periph->path->device;
1317 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1319 periph = SLIST_NEXT(periph, periph_links), i++) {
1321 * Check to see whether we have a
1322 * passthrough device or not.
1324 if (strcmp(periph->periph_name, "pass") == 0) {
1326 * Fill in the getdevlist fields.
1328 strcpy(ccb->cgdl.periph_name,
1329 periph->periph_name);
1330 ccb->cgdl.unit_number =
1331 periph->unit_number;
1332 if (SLIST_NEXT(periph, periph_links))
1334 CAM_GDEVLIST_MORE_DEVS;
1337 CAM_GDEVLIST_LAST_DEVICE;
1338 ccb->cgdl.generation =
1340 ccb->cgdl.index = i;
1342 * Fill in some CCB header fields
1343 * that the user may want.
1345 ccb->ccb_h.path_id =
1346 periph->path->bus->path_id;
1347 ccb->ccb_h.target_id =
1348 periph->path->target->target_id;
1349 ccb->ccb_h.target_lun =
1350 periph->path->device->lun_id;
1351 ccb->ccb_h.status = CAM_REQ_CMP;
1358 * If the periph is null here, one of two things has
1359 * happened. The first possibility is that we couldn't
1360 * find the unit number of the particular peripheral driver
1361 * that the user is asking about. e.g. the user asks for
1362 * the passthrough driver for "da11". We find the list of
1363 * "da" peripherals all right, but there is no unit 11.
1364 * The other possibility is that we went through the list
1365 * of peripheral drivers attached to the device structure,
1366 * but didn't find one with the name "pass". Either way,
1367 * we return ENOENT, since we couldn't find something.
1369 if (periph == NULL) {
1370 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1371 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1372 *ccb->cgdl.periph_name = '\0';
1373 ccb->cgdl.unit_number = 0;
1376 * It is unfortunate that this is even necessary,
1377 * but there are many, many clueless users out there.
1378 * If this is true, the user is looking for the
1379 * passthrough driver, but doesn't have one in his
1382 if (base_periph_found == 1) {
1383 printf("xptioctl: pass driver is not in the "
1385 printf("xptioctl: put \"device pass0\" in "
1386 "your kernel config file\n");
1401 cam_module_event_handler(module_t mod, int what, void *arg)
1403 if (what == MOD_LOAD) {
1405 } else if (what == MOD_UNLOAD) {
1414 /* Functions accessed by the peripheral drivers */
1419 struct cam_sim *xpt_sim;
1420 struct cam_path *path;
1421 struct cam_devq *devq;
1424 TAILQ_INIT(&xpt_busses);
1425 TAILQ_INIT(&cam_bioq);
1426 SLIST_INIT(&ccb_freeq);
1427 STAILQ_INIT(&highpowerq);
1429 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1432 * The xpt layer is, itself, the equivelent of a SIM.
1433 * Allow 16 ccbs in the ccb pool for it. This should
1434 * give decent parallelism when we probe busses and
1435 * perform other XPT functions.
1437 devq = cam_simq_alloc(16);
1438 xpt_sim = cam_sim_alloc(xptaction,
1443 /*max_dev_transactions*/0,
1444 /*max_tagged_dev_transactions*/0,
1448 xpt_bus_register(xpt_sim, /*bus #*/0);
1451 * Looking at the XPT from the SIM layer, the XPT is
1452 * the equivelent of a peripheral driver. Allocate
1453 * a peripheral driver entry for us.
1455 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1456 CAM_TARGET_WILDCARD,
1457 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1458 printf("xpt_init: xpt_create_path failed with status %#x,"
1459 " failing attach\n", status);
1463 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1464 path, NULL, 0, NULL);
1465 xpt_free_path(path);
1467 xpt_sim->softc = xpt_periph;
1470 * Register a callback for when interrupts are enabled.
1473 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1474 M_TEMP, M_NOWAIT | M_ZERO);
1475 if (xpt_config_hook == NULL) {
1476 printf("xpt_init: Cannot malloc config hook "
1477 "- failing attach\n");
1481 xpt_config_hook->ich_func = xpt_config;
1482 if (config_intrhook_establish(xpt_config_hook) != 0) {
1483 free (xpt_config_hook, M_TEMP);
1484 printf("xpt_init: config_intrhook_establish failed "
1485 "- failing attach\n");
1488 /* Install our software interrupt handlers */
1489 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1493 xptregister(struct cam_periph *periph, void *arg)
1495 if (periph == NULL) {
1496 printf("xptregister: periph was NULL!!\n");
1497 return(CAM_REQ_CMP_ERR);
1500 periph->softc = NULL;
1502 xpt_periph = periph;
1504 return(CAM_REQ_CMP);
1508 xpt_add_periph(struct cam_periph *periph)
1510 struct cam_ed *device;
1512 struct periph_list *periph_head;
1516 device = periph->path->device;
1518 periph_head = &device->periphs;
1520 status = CAM_REQ_CMP;
1522 if (device != NULL) {
1526 * Make room for this peripheral
1527 * so it will fit in the queue
1528 * when it's scheduled to run
1531 status = camq_resize(&device->drvq,
1532 device->drvq.array_size + 1);
1534 device->generation++;
1536 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1541 xsoftc.generation++;
1547 xpt_remove_periph(struct cam_periph *periph)
1549 struct cam_ed *device;
1553 device = periph->path->device;
1555 if (device != NULL) {
1557 struct periph_list *periph_head;
1559 periph_head = &device->periphs;
1561 /* Release the slot for this peripheral */
1563 camq_resize(&device->drvq, device->drvq.array_size - 1);
1565 device->generation++;
1567 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1572 xsoftc.generation++;
1576 #ifdef CAM_NEW_TRAN_CODE
1579 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1581 struct ccb_pathinq cpi;
1582 struct ccb_trans_settings cts;
1583 struct cam_path *path;
1591 path = periph->path;
1593 * To ensure that this is printed in one piece,
1594 * mask out CAM interrupts.
1597 printf("%s%d at %s%d bus %d target %d lun %d\n",
1598 periph->periph_name, periph->unit_number,
1599 path->bus->sim->sim_name,
1600 path->bus->sim->unit_number,
1601 path->bus->sim->bus_id,
1602 path->target->target_id,
1603 path->device->lun_id);
1604 printf("%s%d: ", periph->periph_name, periph->unit_number);
1605 scsi_print_inquiry(&path->device->inq_data);
1606 if (bootverbose && path->device->serial_num_len > 0) {
1607 /* Don't wrap the screen - print only the first 60 chars */
1608 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1609 periph->unit_number, path->device->serial_num);
1611 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1612 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1613 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1614 xpt_action((union ccb*)&cts);
1616 /* Ask the SIM for its base transfer speed */
1617 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1618 cpi.ccb_h.func_code = XPT_PATH_INQ;
1619 xpt_action((union ccb *)&cpi);
1621 speed = cpi.base_transfer_speed;
1623 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1624 struct ccb_trans_settings_spi *spi;
1626 spi = &cts.xport_specific.spi;
1627 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1628 && spi->sync_offset != 0) {
1629 freq = scsi_calc_syncsrate(spi->sync_period);
1633 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1634 speed *= (0x01 << spi->bus_width);
1637 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1638 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1639 if (fc->valid & CTS_FC_VALID_SPEED) {
1640 speed = fc->bitrate;
1644 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1645 struct ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1646 if (sas->valid & CTS_SAS_VALID_SPEED) {
1647 speed = sas->bitrate;
1653 printf("%s%d: %d.%03dMB/s transfers",
1654 periph->periph_name, periph->unit_number,
1657 printf("%s%d: %dKB/s transfers", periph->periph_name,
1658 periph->unit_number, speed);
1659 /* Report additional information about SPI connections */
1660 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1661 struct ccb_trans_settings_spi *spi;
1663 spi = &cts.xport_specific.spi;
1665 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1667 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1671 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1672 && spi->bus_width > 0) {
1678 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1679 } else if (freq != 0) {
1683 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1684 struct ccb_trans_settings_fc *fc;
1686 fc = &cts.xport_specific.fc;
1687 if (fc->valid & CTS_FC_VALID_WWNN)
1688 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1689 if (fc->valid & CTS_FC_VALID_WWPN)
1690 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1691 if (fc->valid & CTS_FC_VALID_PORT)
1692 printf(" PortID 0x%x", fc->port);
1695 if (path->device->inq_flags & SID_CmdQue
1696 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1697 printf("\n%s%d: Tagged Queueing Enabled",
1698 periph->periph_name, periph->unit_number);
1703 * We only want to print the caller's announce string if they've
1706 if (announce_string != NULL)
1707 printf("%s%d: %s\n", periph->periph_name,
1708 periph->unit_number, announce_string);
1711 #else /* CAM_NEW_TRAN_CODE */
1713 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1717 struct cam_path *path;
1718 struct ccb_trans_settings cts;
1722 path = periph->path;
1724 * To ensure that this is printed in one piece,
1725 * mask out CAM interrupts.
1728 printf("%s%d at %s%d bus %d target %d lun %d\n",
1729 periph->periph_name, periph->unit_number,
1730 path->bus->sim->sim_name,
1731 path->bus->sim->unit_number,
1732 path->bus->sim->bus_id,
1733 path->target->target_id,
1734 path->device->lun_id);
1735 printf("%s%d: ", periph->periph_name, periph->unit_number);
1736 scsi_print_inquiry(&path->device->inq_data);
1738 && (path->device->serial_num_len > 0)) {
1739 /* Don't wrap the screen - print only the first 60 chars */
1740 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1741 periph->unit_number, path->device->serial_num);
1743 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1744 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1745 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1746 xpt_action((union ccb*)&cts);
1747 if (cts.ccb_h.status == CAM_REQ_CMP) {
1751 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1752 && cts.sync_offset != 0) {
1753 freq = scsi_calc_syncsrate(cts.sync_period);
1756 struct ccb_pathinq cpi;
1758 /* Ask the SIM for its base transfer speed */
1759 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1760 cpi.ccb_h.func_code = XPT_PATH_INQ;
1761 xpt_action((union ccb *)&cpi);
1763 speed = cpi.base_transfer_speed;
1766 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1767 speed *= (0x01 << cts.bus_width);
1770 printf("%s%d: %d.%03dMB/s transfers",
1771 periph->periph_name, periph->unit_number,
1774 printf("%s%d: %dKB/s transfers", periph->periph_name,
1775 periph->unit_number, speed);
1776 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1777 && cts.sync_offset != 0) {
1778 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1779 freq % 1000, cts.sync_offset);
1781 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1782 && cts.bus_width > 0) {
1783 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1784 && cts.sync_offset != 0) {
1789 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1790 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1791 && cts.sync_offset != 0) {
1795 if (path->device->inq_flags & SID_CmdQue
1796 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1797 printf(", Tagged Queueing Enabled");
1801 } else if (path->device->inq_flags & SID_CmdQue
1802 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1803 printf("%s%d: Tagged Queueing Enabled\n",
1804 periph->periph_name, periph->unit_number);
1808 * We only want to print the caller's announce string if they've
1811 if (announce_string != NULL)
1812 printf("%s%d: %s\n", periph->periph_name,
1813 periph->unit_number, announce_string);
1817 #endif /* CAM_NEW_TRAN_CODE */
1819 static dev_match_ret
1820 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1823 dev_match_ret retval;
1826 retval = DM_RET_NONE;
1829 * If we aren't given something to match against, that's an error.
1832 return(DM_RET_ERROR);
1835 * If there are no match entries, then this bus matches no
1838 if ((patterns == NULL) || (num_patterns == 0))
1839 return(DM_RET_DESCEND | DM_RET_COPY);
1841 for (i = 0; i < num_patterns; i++) {
1842 struct bus_match_pattern *cur_pattern;
1845 * If the pattern in question isn't for a bus node, we
1846 * aren't interested. However, we do indicate to the
1847 * calling routine that we should continue descending the
1848 * tree, since the user wants to match against lower-level
1851 if (patterns[i].type != DEV_MATCH_BUS) {
1852 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1853 retval |= DM_RET_DESCEND;
1857 cur_pattern = &patterns[i].pattern.bus_pattern;
1860 * If they want to match any bus node, we give them any
1863 if (cur_pattern->flags == BUS_MATCH_ANY) {
1864 /* set the copy flag */
1865 retval |= DM_RET_COPY;
1868 * If we've already decided on an action, go ahead
1871 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1876 * Not sure why someone would do this...
1878 if (cur_pattern->flags == BUS_MATCH_NONE)
1881 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1882 && (cur_pattern->path_id != bus->path_id))
1885 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1886 && (cur_pattern->bus_id != bus->sim->bus_id))
1889 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1890 && (cur_pattern->unit_number != bus->sim->unit_number))
1893 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1894 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1899 * If we get to this point, the user definitely wants
1900 * information on this bus. So tell the caller to copy the
1903 retval |= DM_RET_COPY;
1906 * If the return action has been set to descend, then we
1907 * know that we've already seen a non-bus matching
1908 * expression, therefore we need to further descend the tree.
1909 * This won't change by continuing around the loop, so we
1910 * go ahead and return. If we haven't seen a non-bus
1911 * matching expression, we keep going around the loop until
1912 * we exhaust the matching expressions. We'll set the stop
1913 * flag once we fall out of the loop.
1915 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1920 * If the return action hasn't been set to descend yet, that means
1921 * we haven't seen anything other than bus matching patterns. So
1922 * tell the caller to stop descending the tree -- the user doesn't
1923 * want to match against lower level tree elements.
1925 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1926 retval |= DM_RET_STOP;
1931 static dev_match_ret
1932 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1933 struct cam_ed *device)
1935 dev_match_ret retval;
1938 retval = DM_RET_NONE;
1941 * If we aren't given something to match against, that's an error.
1944 return(DM_RET_ERROR);
1947 * If there are no match entries, then this device matches no
1950 if ((patterns == NULL) || (num_patterns == 0))
1951 return(DM_RET_DESCEND | DM_RET_COPY);
1953 for (i = 0; i < num_patterns; i++) {
1954 struct device_match_pattern *cur_pattern;
1957 * If the pattern in question isn't for a device node, we
1958 * aren't interested.
1960 if (patterns[i].type != DEV_MATCH_DEVICE) {
1961 if ((patterns[i].type == DEV_MATCH_PERIPH)
1962 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1963 retval |= DM_RET_DESCEND;
1967 cur_pattern = &patterns[i].pattern.device_pattern;
1970 * If they want to match any device node, we give them any
1973 if (cur_pattern->flags == DEV_MATCH_ANY) {
1974 /* set the copy flag */
1975 retval |= DM_RET_COPY;
1979 * If we've already decided on an action, go ahead
1982 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1987 * Not sure why someone would do this...
1989 if (cur_pattern->flags == DEV_MATCH_NONE)
1992 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1993 && (cur_pattern->path_id != device->target->bus->path_id))
1996 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1997 && (cur_pattern->target_id != device->target->target_id))
2000 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
2001 && (cur_pattern->target_lun != device->lun_id))
2004 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
2005 && (cam_quirkmatch((caddr_t)&device->inq_data,
2006 (caddr_t)&cur_pattern->inq_pat,
2007 1, sizeof(cur_pattern->inq_pat),
2008 scsi_static_inquiry_match) == NULL))
2012 * If we get to this point, the user definitely wants
2013 * information on this device. So tell the caller to copy
2016 retval |= DM_RET_COPY;
2019 * If the return action has been set to descend, then we
2020 * know that we've already seen a peripheral matching
2021 * expression, therefore we need to further descend the tree.
2022 * This won't change by continuing around the loop, so we
2023 * go ahead and return. If we haven't seen a peripheral
2024 * matching expression, we keep going around the loop until
2025 * we exhaust the matching expressions. We'll set the stop
2026 * flag once we fall out of the loop.
2028 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
2033 * If the return action hasn't been set to descend yet, that means
2034 * we haven't seen any peripheral matching patterns. So tell the
2035 * caller to stop descending the tree -- the user doesn't want to
2036 * match against lower level tree elements.
2038 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
2039 retval |= DM_RET_STOP;
2045 * Match a single peripheral against any number of match patterns.
2047 static dev_match_ret
2048 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
2049 struct cam_periph *periph)
2051 dev_match_ret retval;
2055 * If we aren't given something to match against, that's an error.
2058 return(DM_RET_ERROR);
2061 * If there are no match entries, then this peripheral matches no
2064 if ((patterns == NULL) || (num_patterns == 0))
2065 return(DM_RET_STOP | DM_RET_COPY);
2068 * There aren't any nodes below a peripheral node, so there's no
2069 * reason to descend the tree any further.
2071 retval = DM_RET_STOP;
2073 for (i = 0; i < num_patterns; i++) {
2074 struct periph_match_pattern *cur_pattern;
2077 * If the pattern in question isn't for a peripheral, we
2078 * aren't interested.
2080 if (patterns[i].type != DEV_MATCH_PERIPH)
2083 cur_pattern = &patterns[i].pattern.periph_pattern;
2086 * If they want to match on anything, then we will do so.
2088 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2089 /* set the copy flag */
2090 retval |= DM_RET_COPY;
2093 * We've already set the return action to stop,
2094 * since there are no nodes below peripherals in
2101 * Not sure why someone would do this...
2103 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2106 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2107 && (cur_pattern->path_id != periph->path->bus->path_id))
2111 * For the target and lun id's, we have to make sure the
2112 * target and lun pointers aren't NULL. The xpt peripheral
2113 * has a wildcard target and device.
2115 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2116 && ((periph->path->target == NULL)
2117 ||(cur_pattern->target_id != periph->path->target->target_id)))
2120 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2121 && ((periph->path->device == NULL)
2122 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2125 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2126 && (cur_pattern->unit_number != periph->unit_number))
2129 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2130 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2135 * If we get to this point, the user definitely wants
2136 * information on this peripheral. So tell the caller to
2137 * copy the data out.
2139 retval |= DM_RET_COPY;
2142 * The return action has already been set to stop, since
2143 * peripherals don't have any nodes below them in the EDT.
2149 * If we get to this point, the peripheral that was passed in
2150 * doesn't match any of the patterns.
2156 xptedtbusfunc(struct cam_eb *bus, void *arg)
2158 struct ccb_dev_match *cdm;
2159 dev_match_ret retval;
2161 cdm = (struct ccb_dev_match *)arg;
2164 * If our position is for something deeper in the tree, that means
2165 * that we've already seen this node. So, we keep going down.
2167 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2168 && (cdm->pos.cookie.bus == bus)
2169 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2170 && (cdm->pos.cookie.target != NULL))
2171 retval = DM_RET_DESCEND;
2173 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2176 * If we got an error, bail out of the search.
2178 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2179 cdm->status = CAM_DEV_MATCH_ERROR;
2184 * If the copy flag is set, copy this bus out.
2186 if (retval & DM_RET_COPY) {
2189 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2190 sizeof(struct dev_match_result));
2193 * If we don't have enough space to put in another
2194 * match result, save our position and tell the
2195 * user there are more devices to check.
2197 if (spaceleft < sizeof(struct dev_match_result)) {
2198 bzero(&cdm->pos, sizeof(cdm->pos));
2199 cdm->pos.position_type =
2200 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2202 cdm->pos.cookie.bus = bus;
2203 cdm->pos.generations[CAM_BUS_GENERATION]=
2205 cdm->status = CAM_DEV_MATCH_MORE;
2208 j = cdm->num_matches;
2210 cdm->matches[j].type = DEV_MATCH_BUS;
2211 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2212 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2213 cdm->matches[j].result.bus_result.unit_number =
2214 bus->sim->unit_number;
2215 strncpy(cdm->matches[j].result.bus_result.dev_name,
2216 bus->sim->sim_name, DEV_IDLEN);
2220 * If the user is only interested in busses, there's no
2221 * reason to descend to the next level in the tree.
2223 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2227 * If there is a target generation recorded, check it to
2228 * make sure the target list hasn't changed.
2230 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2231 && (bus == cdm->pos.cookie.bus)
2232 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2233 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2234 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2236 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2240 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2241 && (cdm->pos.cookie.bus == bus)
2242 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2243 && (cdm->pos.cookie.target != NULL))
2244 return(xpttargettraverse(bus,
2245 (struct cam_et *)cdm->pos.cookie.target,
2246 xptedttargetfunc, arg));
2248 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2252 xptedttargetfunc(struct cam_et *target, void *arg)
2254 struct ccb_dev_match *cdm;
2256 cdm = (struct ccb_dev_match *)arg;
2259 * If there is a device list generation recorded, check it to
2260 * make sure the device list hasn't changed.
2262 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2263 && (cdm->pos.cookie.bus == target->bus)
2264 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2265 && (cdm->pos.cookie.target == target)
2266 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2267 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2268 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2269 target->generation)) {
2270 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2274 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2275 && (cdm->pos.cookie.bus == target->bus)
2276 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2277 && (cdm->pos.cookie.target == target)
2278 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2279 && (cdm->pos.cookie.device != NULL))
2280 return(xptdevicetraverse(target,
2281 (struct cam_ed *)cdm->pos.cookie.device,
2282 xptedtdevicefunc, arg));
2284 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2288 xptedtdevicefunc(struct cam_ed *device, void *arg)
2291 struct ccb_dev_match *cdm;
2292 dev_match_ret retval;
2294 cdm = (struct ccb_dev_match *)arg;
2297 * If our position is for something deeper in the tree, that means
2298 * that we've already seen this node. So, we keep going down.
2300 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2301 && (cdm->pos.cookie.device == device)
2302 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2303 && (cdm->pos.cookie.periph != NULL))
2304 retval = DM_RET_DESCEND;
2306 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2309 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2310 cdm->status = CAM_DEV_MATCH_ERROR;
2315 * If the copy flag is set, copy this device out.
2317 if (retval & DM_RET_COPY) {
2320 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2321 sizeof(struct dev_match_result));
2324 * If we don't have enough space to put in another
2325 * match result, save our position and tell the
2326 * user there are more devices to check.
2328 if (spaceleft < sizeof(struct dev_match_result)) {
2329 bzero(&cdm->pos, sizeof(cdm->pos));
2330 cdm->pos.position_type =
2331 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2332 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2334 cdm->pos.cookie.bus = device->target->bus;
2335 cdm->pos.generations[CAM_BUS_GENERATION]=
2337 cdm->pos.cookie.target = device->target;
2338 cdm->pos.generations[CAM_TARGET_GENERATION] =
2339 device->target->bus->generation;
2340 cdm->pos.cookie.device = device;
2341 cdm->pos.generations[CAM_DEV_GENERATION] =
2342 device->target->generation;
2343 cdm->status = CAM_DEV_MATCH_MORE;
2346 j = cdm->num_matches;
2348 cdm->matches[j].type = DEV_MATCH_DEVICE;
2349 cdm->matches[j].result.device_result.path_id =
2350 device->target->bus->path_id;
2351 cdm->matches[j].result.device_result.target_id =
2352 device->target->target_id;
2353 cdm->matches[j].result.device_result.target_lun =
2355 bcopy(&device->inq_data,
2356 &cdm->matches[j].result.device_result.inq_data,
2357 sizeof(struct scsi_inquiry_data));
2359 /* Let the user know whether this device is unconfigured */
2360 if (device->flags & CAM_DEV_UNCONFIGURED)
2361 cdm->matches[j].result.device_result.flags =
2362 DEV_RESULT_UNCONFIGURED;
2364 cdm->matches[j].result.device_result.flags =
2369 * If the user isn't interested in peripherals, don't descend
2370 * the tree any further.
2372 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2376 * If there is a peripheral list generation recorded, make sure
2377 * it hasn't changed.
2379 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2380 && (device->target->bus == cdm->pos.cookie.bus)
2381 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2382 && (device->target == cdm->pos.cookie.target)
2383 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2384 && (device == cdm->pos.cookie.device)
2385 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2386 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2387 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2388 device->generation)){
2389 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2393 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2394 && (cdm->pos.cookie.bus == device->target->bus)
2395 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2396 && (cdm->pos.cookie.target == device->target)
2397 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2398 && (cdm->pos.cookie.device == device)
2399 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2400 && (cdm->pos.cookie.periph != NULL))
2401 return(xptperiphtraverse(device,
2402 (struct cam_periph *)cdm->pos.cookie.periph,
2403 xptedtperiphfunc, arg));
2405 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2409 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2411 struct ccb_dev_match *cdm;
2412 dev_match_ret retval;
2414 cdm = (struct ccb_dev_match *)arg;
2416 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2418 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2419 cdm->status = CAM_DEV_MATCH_ERROR;
2424 * If the copy flag is set, copy this peripheral out.
2426 if (retval & DM_RET_COPY) {
2429 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2430 sizeof(struct dev_match_result));
2433 * If we don't have enough space to put in another
2434 * match result, save our position and tell the
2435 * user there are more devices to check.
2437 if (spaceleft < sizeof(struct dev_match_result)) {
2438 bzero(&cdm->pos, sizeof(cdm->pos));
2439 cdm->pos.position_type =
2440 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2441 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2444 cdm->pos.cookie.bus = periph->path->bus;
2445 cdm->pos.generations[CAM_BUS_GENERATION]=
2447 cdm->pos.cookie.target = periph->path->target;
2448 cdm->pos.generations[CAM_TARGET_GENERATION] =
2449 periph->path->bus->generation;
2450 cdm->pos.cookie.device = periph->path->device;
2451 cdm->pos.generations[CAM_DEV_GENERATION] =
2452 periph->path->target->generation;
2453 cdm->pos.cookie.periph = periph;
2454 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2455 periph->path->device->generation;
2456 cdm->status = CAM_DEV_MATCH_MORE;
2460 j = cdm->num_matches;
2462 cdm->matches[j].type = DEV_MATCH_PERIPH;
2463 cdm->matches[j].result.periph_result.path_id =
2464 periph->path->bus->path_id;
2465 cdm->matches[j].result.periph_result.target_id =
2466 periph->path->target->target_id;
2467 cdm->matches[j].result.periph_result.target_lun =
2468 periph->path->device->lun_id;
2469 cdm->matches[j].result.periph_result.unit_number =
2470 periph->unit_number;
2471 strncpy(cdm->matches[j].result.periph_result.periph_name,
2472 periph->periph_name, DEV_IDLEN);
2479 xptedtmatch(struct ccb_dev_match *cdm)
2483 cdm->num_matches = 0;
2486 * Check the bus list generation. If it has changed, the user
2487 * needs to reset everything and start over.
2489 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2490 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2491 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2492 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2496 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2497 && (cdm->pos.cookie.bus != NULL))
2498 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2499 xptedtbusfunc, cdm);
2501 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2504 * If we get back 0, that means that we had to stop before fully
2505 * traversing the EDT. It also means that one of the subroutines
2506 * has set the status field to the proper value. If we get back 1,
2507 * we've fully traversed the EDT and copied out any matching entries.
2510 cdm->status = CAM_DEV_MATCH_LAST;
2516 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2518 struct ccb_dev_match *cdm;
2520 cdm = (struct ccb_dev_match *)arg;
2522 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2523 && (cdm->pos.cookie.pdrv == pdrv)
2524 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2525 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2526 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2527 (*pdrv)->generation)) {
2528 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2532 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2533 && (cdm->pos.cookie.pdrv == pdrv)
2534 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2535 && (cdm->pos.cookie.periph != NULL))
2536 return(xptpdperiphtraverse(pdrv,
2537 (struct cam_periph *)cdm->pos.cookie.periph,
2538 xptplistperiphfunc, arg));
2540 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2544 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2546 struct ccb_dev_match *cdm;
2547 dev_match_ret retval;
2549 cdm = (struct ccb_dev_match *)arg;
2551 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2553 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2554 cdm->status = CAM_DEV_MATCH_ERROR;
2559 * If the copy flag is set, copy this peripheral out.
2561 if (retval & DM_RET_COPY) {
2564 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2565 sizeof(struct dev_match_result));
2568 * If we don't have enough space to put in another
2569 * match result, save our position and tell the
2570 * user there are more devices to check.
2572 if (spaceleft < sizeof(struct dev_match_result)) {
2573 struct periph_driver **pdrv;
2576 bzero(&cdm->pos, sizeof(cdm->pos));
2577 cdm->pos.position_type =
2578 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2582 * This may look a bit non-sensical, but it is
2583 * actually quite logical. There are very few
2584 * peripheral drivers, and bloating every peripheral
2585 * structure with a pointer back to its parent
2586 * peripheral driver linker set entry would cost
2587 * more in the long run than doing this quick lookup.
2589 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2590 if (strcmp((*pdrv)->driver_name,
2591 periph->periph_name) == 0)
2595 if (*pdrv == NULL) {
2596 cdm->status = CAM_DEV_MATCH_ERROR;
2600 cdm->pos.cookie.pdrv = pdrv;
2602 * The periph generation slot does double duty, as
2603 * does the periph pointer slot. They are used for
2604 * both edt and pdrv lookups and positioning.
2606 cdm->pos.cookie.periph = periph;
2607 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2608 (*pdrv)->generation;
2609 cdm->status = CAM_DEV_MATCH_MORE;
2613 j = cdm->num_matches;
2615 cdm->matches[j].type = DEV_MATCH_PERIPH;
2616 cdm->matches[j].result.periph_result.path_id =
2617 periph->path->bus->path_id;
2620 * The transport layer peripheral doesn't have a target or
2623 if (periph->path->target)
2624 cdm->matches[j].result.periph_result.target_id =
2625 periph->path->target->target_id;
2627 cdm->matches[j].result.periph_result.target_id = -1;
2629 if (periph->path->device)
2630 cdm->matches[j].result.periph_result.target_lun =
2631 periph->path->device->lun_id;
2633 cdm->matches[j].result.periph_result.target_lun = -1;
2635 cdm->matches[j].result.periph_result.unit_number =
2636 periph->unit_number;
2637 strncpy(cdm->matches[j].result.periph_result.periph_name,
2638 periph->periph_name, DEV_IDLEN);
2645 xptperiphlistmatch(struct ccb_dev_match *cdm)
2649 cdm->num_matches = 0;
2652 * At this point in the edt traversal function, we check the bus
2653 * list generation to make sure that no busses have been added or
2654 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2655 * For the peripheral driver list traversal function, however, we
2656 * don't have to worry about new peripheral driver types coming or
2657 * going; they're in a linker set, and therefore can't change
2658 * without a recompile.
2661 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2662 && (cdm->pos.cookie.pdrv != NULL))
2663 ret = xptpdrvtraverse(
2664 (struct periph_driver **)cdm->pos.cookie.pdrv,
2665 xptplistpdrvfunc, cdm);
2667 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2670 * If we get back 0, that means that we had to stop before fully
2671 * traversing the peripheral driver tree. It also means that one of
2672 * the subroutines has set the status field to the proper value. If
2673 * we get back 1, we've fully traversed the EDT and copied out any
2677 cdm->status = CAM_DEV_MATCH_LAST;
2683 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2685 struct cam_eb *bus, *next_bus;
2690 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2693 next_bus = TAILQ_NEXT(bus, links);
2695 retval = tr_func(bus, arg);
2704 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2705 xpt_targetfunc_t *tr_func, void *arg)
2707 struct cam_et *target, *next_target;
2711 for (target = (start_target ? start_target :
2712 TAILQ_FIRST(&bus->et_entries));
2713 target != NULL; target = next_target) {
2715 next_target = TAILQ_NEXT(target, links);
2717 retval = tr_func(target, arg);
2727 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2728 xpt_devicefunc_t *tr_func, void *arg)
2730 struct cam_ed *device, *next_device;
2734 for (device = (start_device ? start_device :
2735 TAILQ_FIRST(&target->ed_entries));
2737 device = next_device) {
2739 next_device = TAILQ_NEXT(device, links);
2741 retval = tr_func(device, arg);
2751 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2752 xpt_periphfunc_t *tr_func, void *arg)
2754 struct cam_periph *periph, *next_periph;
2759 for (periph = (start_periph ? start_periph :
2760 SLIST_FIRST(&device->periphs));
2762 periph = next_periph) {
2764 next_periph = SLIST_NEXT(periph, periph_links);
2766 retval = tr_func(periph, arg);
2775 xptpdrvtraverse(struct periph_driver **start_pdrv,
2776 xpt_pdrvfunc_t *tr_func, void *arg)
2778 struct periph_driver **pdrv;
2784 * We don't traverse the peripheral driver list like we do the
2785 * other lists, because it is a linker set, and therefore cannot be
2786 * changed during runtime. If the peripheral driver list is ever
2787 * re-done to be something other than a linker set (i.e. it can
2788 * change while the system is running), the list traversal should
2789 * be modified to work like the other traversal functions.
2791 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2792 *pdrv != NULL; pdrv++) {
2793 retval = tr_func(pdrv, arg);
2803 xptpdperiphtraverse(struct periph_driver **pdrv,
2804 struct cam_periph *start_periph,
2805 xpt_periphfunc_t *tr_func, void *arg)
2807 struct cam_periph *periph, *next_periph;
2812 for (periph = (start_periph ? start_periph :
2813 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2814 periph = next_periph) {
2816 next_periph = TAILQ_NEXT(periph, unit_links);
2818 retval = tr_func(periph, arg);
2826 xptdefbusfunc(struct cam_eb *bus, void *arg)
2828 struct xpt_traverse_config *tr_config;
2830 tr_config = (struct xpt_traverse_config *)arg;
2832 if (tr_config->depth == XPT_DEPTH_BUS) {
2833 xpt_busfunc_t *tr_func;
2835 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2837 return(tr_func(bus, tr_config->tr_arg));
2839 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2843 xptdeftargetfunc(struct cam_et *target, void *arg)
2845 struct xpt_traverse_config *tr_config;
2847 tr_config = (struct xpt_traverse_config *)arg;
2849 if (tr_config->depth == XPT_DEPTH_TARGET) {
2850 xpt_targetfunc_t *tr_func;
2852 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2854 return(tr_func(target, tr_config->tr_arg));
2856 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2860 xptdefdevicefunc(struct cam_ed *device, void *arg)
2862 struct xpt_traverse_config *tr_config;
2864 tr_config = (struct xpt_traverse_config *)arg;
2866 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2867 xpt_devicefunc_t *tr_func;
2869 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2871 return(tr_func(device, tr_config->tr_arg));
2873 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2877 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2879 struct xpt_traverse_config *tr_config;
2880 xpt_periphfunc_t *tr_func;
2882 tr_config = (struct xpt_traverse_config *)arg;
2884 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2887 * Unlike the other default functions, we don't check for depth
2888 * here. The peripheral driver level is the last level in the EDT,
2889 * so if we're here, we should execute the function in question.
2891 return(tr_func(periph, tr_config->tr_arg));
2895 * Execute the given function for every bus in the EDT.
2898 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2900 struct xpt_traverse_config tr_config;
2902 tr_config.depth = XPT_DEPTH_BUS;
2903 tr_config.tr_func = tr_func;
2904 tr_config.tr_arg = arg;
2906 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2911 * Execute the given function for every target in the EDT.
2914 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2916 struct xpt_traverse_config tr_config;
2918 tr_config.depth = XPT_DEPTH_TARGET;
2919 tr_config.tr_func = tr_func;
2920 tr_config.tr_arg = arg;
2922 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2924 #endif /* notusedyet */
2927 * Execute the given function for every device in the EDT.
2930 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2932 struct xpt_traverse_config tr_config;
2934 tr_config.depth = XPT_DEPTH_DEVICE;
2935 tr_config.tr_func = tr_func;
2936 tr_config.tr_arg = arg;
2938 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2943 * Execute the given function for every peripheral in the EDT.
2946 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2948 struct xpt_traverse_config tr_config;
2950 tr_config.depth = XPT_DEPTH_PERIPH;
2951 tr_config.tr_func = tr_func;
2952 tr_config.tr_arg = arg;
2954 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2956 #endif /* notusedyet */
2959 xptsetasyncfunc(struct cam_ed *device, void *arg)
2961 struct cam_path path;
2962 struct ccb_getdev cgd;
2963 struct async_node *cur_entry;
2965 cur_entry = (struct async_node *)arg;
2968 * Don't report unconfigured devices (Wildcard devs,
2969 * devices only for target mode, device instances
2970 * that have been invalidated but are waiting for
2971 * their last reference count to be released).
2973 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2976 xpt_compile_path(&path,
2978 device->target->bus->path_id,
2979 device->target->target_id,
2981 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2982 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2983 xpt_action((union ccb *)&cgd);
2984 cur_entry->callback(cur_entry->callback_arg,
2987 xpt_release_path(&path);
2993 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2995 struct cam_path path;
2996 struct ccb_pathinq cpi;
2997 struct async_node *cur_entry;
2999 cur_entry = (struct async_node *)arg;
3001 xpt_compile_path(&path, /*periph*/NULL,
3003 CAM_TARGET_WILDCARD,
3005 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
3006 cpi.ccb_h.func_code = XPT_PATH_INQ;
3007 xpt_action((union ccb *)&cpi);
3008 cur_entry->callback(cur_entry->callback_arg,
3011 xpt_release_path(&path);
3017 xpt_action(union ccb *start_ccb)
3023 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
3025 start_ccb->ccb_h.status = CAM_REQ_INPROG;
3027 iopl = splsoftcam();
3028 switch (start_ccb->ccb_h.func_code) {
3031 #ifdef CAM_NEW_TRAN_CODE
3032 struct cam_ed *device;
3033 #endif /* CAM_NEW_TRAN_CODE */
3035 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3036 struct cam_path *path;
3038 path = start_ccb->ccb_h.path;
3042 * For the sake of compatibility with SCSI-1
3043 * devices that may not understand the identify
3044 * message, we include lun information in the
3045 * second byte of all commands. SCSI-1 specifies
3046 * that luns are a 3 bit value and reserves only 3
3047 * bits for lun information in the CDB. Later
3048 * revisions of the SCSI spec allow for more than 8
3049 * luns, but have deprecated lun information in the
3050 * CDB. So, if the lun won't fit, we must omit.
3052 * Also be aware that during initial probing for devices,
3053 * the inquiry information is unknown but initialized to 0.
3054 * This means that this code will be exercised while probing
3055 * devices with an ANSI revision greater than 2.
3057 #ifdef CAM_NEW_TRAN_CODE
3058 device = start_ccb->ccb_h.path->device;
3059 if (device->protocol_version <= SCSI_REV_2
3060 #else /* CAM_NEW_TRAN_CODE */
3061 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
3062 #endif /* CAM_NEW_TRAN_CODE */
3063 && start_ccb->ccb_h.target_lun < 8
3064 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
3066 start_ccb->csio.cdb_io.cdb_bytes[1] |=
3067 start_ccb->ccb_h.target_lun << 5;
3069 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3070 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3071 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3072 &path->device->inq_data),
3073 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3074 cdb_str, sizeof(cdb_str))));
3078 case XPT_CONT_TARGET_IO:
3079 start_ccb->csio.sense_resid = 0;
3080 start_ccb->csio.resid = 0;
3085 struct cam_path *path;
3086 struct cam_sim *sim;
3090 path = start_ccb->ccb_h.path;
3093 sim = path->bus->sim;
3094 if (SIM_DEAD(sim)) {
3095 /* The SIM has gone; just execute the CCB directly. */
3096 cam_ccbq_send_ccb(&path->device->ccbq, start_ccb);
3097 (*(sim->sim_action))(sim, start_ccb);
3102 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3103 if (path->device->qfrozen_cnt == 0)
3104 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3109 xpt_run_dev_sendq(path->bus);
3112 case XPT_SET_TRAN_SETTINGS:
3114 xpt_set_transfer_settings(&start_ccb->cts,
3115 start_ccb->ccb_h.path->device,
3116 /*async_update*/FALSE);
3119 case XPT_CALC_GEOMETRY:
3121 struct cam_sim *sim;
3123 /* Filter out garbage */
3124 if (start_ccb->ccg.block_size == 0
3125 || start_ccb->ccg.volume_size == 0) {
3126 start_ccb->ccg.cylinders = 0;
3127 start_ccb->ccg.heads = 0;
3128 start_ccb->ccg.secs_per_track = 0;
3129 start_ccb->ccb_h.status = CAM_REQ_CMP;
3134 * In a PC-98 system, geometry translation depens on
3135 * the "real" device geometry obtained from mode page 4.
3136 * SCSI geometry translation is performed in the
3137 * initialization routine of the SCSI BIOS and the result
3138 * stored in host memory. If the translation is available
3139 * in host memory, use it. If not, rely on the default
3140 * translation the device driver performs.
3142 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3143 start_ccb->ccb_h.status = CAM_REQ_CMP;
3147 sim = start_ccb->ccb_h.path->bus->sim;
3148 (*(sim->sim_action))(sim, start_ccb);
3153 union ccb* abort_ccb;
3156 abort_ccb = start_ccb->cab.abort_ccb;
3157 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3159 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3160 struct cam_ccbq *ccbq;
3162 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3163 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3164 abort_ccb->ccb_h.status =
3165 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3166 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3168 xpt_done(abort_ccb);
3170 start_ccb->ccb_h.status = CAM_REQ_CMP;
3173 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3174 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3176 * We've caught this ccb en route to
3177 * the SIM. Flag it for abort and the
3178 * SIM will do so just before starting
3179 * real work on the CCB.
3181 abort_ccb->ccb_h.status =
3182 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3183 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3184 start_ccb->ccb_h.status = CAM_REQ_CMP;
3188 if (XPT_FC_IS_QUEUED(abort_ccb)
3189 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3191 * It's already completed but waiting
3192 * for our SWI to get to it.
3194 start_ccb->ccb_h.status = CAM_UA_ABORT;
3198 * If we weren't able to take care of the abort request
3199 * in the XPT, pass the request down to the SIM for processing.
3203 case XPT_ACCEPT_TARGET_IO:
3205 case XPT_IMMED_NOTIFY:
3206 case XPT_NOTIFY_ACK:
3207 case XPT_GET_TRAN_SETTINGS:
3210 struct cam_sim *sim;
3212 sim = start_ccb->ccb_h.path->bus->sim;
3213 (*(sim->sim_action))(sim, start_ccb);
3218 struct cam_sim *sim;
3220 sim = start_ccb->ccb_h.path->bus->sim;
3221 (*(sim->sim_action))(sim, start_ccb);
3224 case XPT_PATH_STATS:
3225 start_ccb->cpis.last_reset =
3226 start_ccb->ccb_h.path->bus->last_reset;
3227 start_ccb->ccb_h.status = CAM_REQ_CMP;
3234 dev = start_ccb->ccb_h.path->device;
3236 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3237 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3239 struct ccb_getdev *cgd;
3243 cgd = &start_ccb->cgd;
3244 bus = cgd->ccb_h.path->bus;
3245 tar = cgd->ccb_h.path->target;
3246 cgd->inq_data = dev->inq_data;
3247 cgd->ccb_h.status = CAM_REQ_CMP;
3248 cgd->serial_num_len = dev->serial_num_len;
3249 if ((dev->serial_num_len > 0)
3250 && (dev->serial_num != NULL))
3251 bcopy(dev->serial_num, cgd->serial_num,
3252 dev->serial_num_len);
3257 case XPT_GDEV_STATS:
3262 dev = start_ccb->ccb_h.path->device;
3264 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3265 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3267 struct ccb_getdevstats *cgds;
3271 cgds = &start_ccb->cgds;
3272 bus = cgds->ccb_h.path->bus;
3273 tar = cgds->ccb_h.path->target;
3274 cgds->dev_openings = dev->ccbq.dev_openings;
3275 cgds->dev_active = dev->ccbq.dev_active;
3276 cgds->devq_openings = dev->ccbq.devq_openings;
3277 cgds->devq_queued = dev->ccbq.queue.entries;
3278 cgds->held = dev->ccbq.held;
3279 cgds->last_reset = tar->last_reset;
3280 cgds->maxtags = dev->quirk->maxtags;
3281 cgds->mintags = dev->quirk->mintags;
3282 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3283 cgds->last_reset = bus->last_reset;
3284 cgds->ccb_h.status = CAM_REQ_CMP;
3291 struct cam_periph *nperiph;
3292 struct periph_list *periph_head;
3293 struct ccb_getdevlist *cgdl;
3296 struct cam_ed *device;
3303 * Don't want anyone mucking with our data.
3306 device = start_ccb->ccb_h.path->device;
3307 periph_head = &device->periphs;
3308 cgdl = &start_ccb->cgdl;
3311 * Check and see if the list has changed since the user
3312 * last requested a list member. If so, tell them that the
3313 * list has changed, and therefore they need to start over
3314 * from the beginning.
3316 if ((cgdl->index != 0) &&
3317 (cgdl->generation != device->generation)) {
3318 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3324 * Traverse the list of peripherals and attempt to find
3325 * the requested peripheral.
3327 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3328 (nperiph != NULL) && (i <= cgdl->index);
3329 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3330 if (i == cgdl->index) {
3331 strncpy(cgdl->periph_name,
3332 nperiph->periph_name,
3334 cgdl->unit_number = nperiph->unit_number;
3339 cgdl->status = CAM_GDEVLIST_ERROR;
3344 if (nperiph == NULL)
3345 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3347 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3350 cgdl->generation = device->generation;
3353 cgdl->ccb_h.status = CAM_REQ_CMP;
3359 dev_pos_type position_type;
3360 struct ccb_dev_match *cdm;
3362 cdm = &start_ccb->cdm;
3365 * Prevent EDT changes while we traverse it.
3369 * There are two ways of getting at information in the EDT.
3370 * The first way is via the primary EDT tree. It starts
3371 * with a list of busses, then a list of targets on a bus,
3372 * then devices/luns on a target, and then peripherals on a
3373 * device/lun. The "other" way is by the peripheral driver
3374 * lists. The peripheral driver lists are organized by
3375 * peripheral driver. (obviously) So it makes sense to
3376 * use the peripheral driver list if the user is looking
3377 * for something like "da1", or all "da" devices. If the
3378 * user is looking for something on a particular bus/target
3379 * or lun, it's generally better to go through the EDT tree.
3382 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3383 position_type = cdm->pos.position_type;
3387 position_type = CAM_DEV_POS_NONE;
3389 for (i = 0; i < cdm->num_patterns; i++) {
3390 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3391 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3392 position_type = CAM_DEV_POS_EDT;
3397 if (cdm->num_patterns == 0)
3398 position_type = CAM_DEV_POS_EDT;
3399 else if (position_type == CAM_DEV_POS_NONE)
3400 position_type = CAM_DEV_POS_PDRV;
3403 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3404 case CAM_DEV_POS_EDT:
3407 case CAM_DEV_POS_PDRV:
3408 xptperiphlistmatch(cdm);
3411 cdm->status = CAM_DEV_MATCH_ERROR;
3417 if (cdm->status == CAM_DEV_MATCH_ERROR)
3418 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3420 start_ccb->ccb_h.status = CAM_REQ_CMP;
3426 struct ccb_setasync *csa;
3427 struct async_node *cur_entry;
3428 struct async_list *async_head;
3432 csa = &start_ccb->csa;
3433 added = csa->event_enable;
3434 async_head = &csa->ccb_h.path->device->asyncs;
3437 * If there is already an entry for us, simply
3441 cur_entry = SLIST_FIRST(async_head);
3442 while (cur_entry != NULL) {
3443 if ((cur_entry->callback_arg == csa->callback_arg)
3444 && (cur_entry->callback == csa->callback))
3446 cur_entry = SLIST_NEXT(cur_entry, links);
3449 if (cur_entry != NULL) {
3451 * If the request has no flags set,
3454 added &= ~cur_entry->event_enable;
3455 if (csa->event_enable == 0) {
3456 SLIST_REMOVE(async_head, cur_entry,
3458 csa->ccb_h.path->device->refcount--;
3459 free(cur_entry, M_CAMXPT);
3461 cur_entry->event_enable = csa->event_enable;
3464 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3466 if (cur_entry == NULL) {
3468 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3471 cur_entry->event_enable = csa->event_enable;
3472 cur_entry->callback_arg = csa->callback_arg;
3473 cur_entry->callback = csa->callback;
3474 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3475 csa->ccb_h.path->device->refcount++;
3478 if ((added & AC_FOUND_DEVICE) != 0) {
3480 * Get this peripheral up to date with all
3481 * the currently existing devices.
3483 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3485 if ((added & AC_PATH_REGISTERED) != 0) {
3487 * Get this peripheral up to date with all
3488 * the currently existing busses.
3490 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3493 start_ccb->ccb_h.status = CAM_REQ_CMP;
3498 struct ccb_relsim *crs;
3502 crs = &start_ccb->crs;
3503 dev = crs->ccb_h.path->device;
3506 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3512 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3514 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
3515 /* Don't ever go below one opening */
3516 if (crs->openings > 0) {
3517 xpt_dev_ccbq_resize(crs->ccb_h.path,
3521 xpt_print_path(crs->ccb_h.path);
3522 printf("tagged openings "
3530 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3532 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3535 * Just extend the old timeout and decrement
3536 * the freeze count so that a single timeout
3537 * is sufficient for releasing the queue.
3539 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3540 untimeout(xpt_release_devq_timeout,
3541 dev, dev->c_handle);
3544 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3548 timeout(xpt_release_devq_timeout,
3550 (crs->release_timeout * hz) / 1000);
3552 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3556 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3558 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3560 * Decrement the freeze count so that a single
3561 * completion is still sufficient to unfreeze
3564 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3567 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3568 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3572 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3574 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3575 || (dev->ccbq.dev_active == 0)) {
3577 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3580 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3581 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3586 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3588 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3591 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3592 start_ccb->ccb_h.status = CAM_REQ_CMP;
3596 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3599 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3600 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3608 #ifdef CAM_DEBUG_DELAY
3609 cam_debug_delay = CAM_DEBUG_DELAY;
3611 cam_dflags = start_ccb->cdbg.flags;
3612 if (cam_dpath != NULL) {
3613 xpt_free_path(cam_dpath);
3617 if (cam_dflags != CAM_DEBUG_NONE) {
3618 if (xpt_create_path(&cam_dpath, xpt_periph,
3619 start_ccb->ccb_h.path_id,
3620 start_ccb->ccb_h.target_id,
3621 start_ccb->ccb_h.target_lun) !=
3623 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3624 cam_dflags = CAM_DEBUG_NONE;
3626 start_ccb->ccb_h.status = CAM_REQ_CMP;
3627 xpt_print_path(cam_dpath);
3628 printf("debugging flags now %x\n", cam_dflags);
3632 start_ccb->ccb_h.status = CAM_REQ_CMP;
3635 #else /* !CAMDEBUG */
3636 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3637 #endif /* CAMDEBUG */
3641 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3642 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3643 start_ccb->ccb_h.status = CAM_REQ_CMP;
3650 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3657 xpt_polled_action(union ccb *start_ccb)
3661 struct cam_sim *sim;
3662 struct cam_devq *devq;
3667 timeout = start_ccb->ccb_h.timeout;
3668 sim = start_ccb->ccb_h.path->bus->sim;
3670 dev = start_ccb->ccb_h.path->device;
3675 * Steal an opening so that no other queued requests
3676 * can get it before us while we simulate interrupts.
3678 dev->ccbq.devq_openings--;
3679 dev->ccbq.dev_openings--;
3681 while(((devq != NULL && devq->send_openings <= 0) ||
3682 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3684 (*(sim->sim_poll))(sim);
3688 dev->ccbq.devq_openings++;
3689 dev->ccbq.dev_openings++;
3692 xpt_action(start_ccb);
3693 while(--timeout > 0) {
3694 (*(sim->sim_poll))(sim);
3696 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3703 * XXX Is it worth adding a sim_timeout entry
3704 * point so we can attempt recovery? If
3705 * this is only used for dumps, I don't think
3708 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3711 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3717 * Schedule a peripheral driver to receive a ccb when it's
3718 * target device has space for more transactions.
3721 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3723 struct cam_ed *device;
3724 union ccb *work_ccb;
3730 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3731 device = perph->path->device;
3733 if (periph_is_queued(perph)) {
3734 /* Simply reorder based on new priority */
3735 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3736 (" change priority to %d\n", new_priority));
3737 if (new_priority < perph->pinfo.priority) {
3738 camq_change_priority(&device->drvq,
3743 } else if (SIM_DEAD(perph->path->bus->sim)) {
3744 /* The SIM is gone so just call periph_start directly. */
3745 work_ccb = xpt_get_ccb(perph->path->device);
3747 if (work_ccb == NULL)
3749 xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority);
3750 perph->pinfo.priority = new_priority;
3751 perph->periph_start(perph, work_ccb);
3754 /* New entry on the queue */
3755 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3756 (" added periph to queue\n"));
3757 perph->pinfo.priority = new_priority;
3758 perph->pinfo.generation = ++device->drvq.generation;
3759 camq_insert(&device->drvq, &perph->pinfo);
3760 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3764 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3765 (" calling xpt_run_devq\n"));
3766 xpt_run_dev_allocq(perph->path->bus);
3772 * Schedule a device to run on a given queue.
3773 * If the device was inserted as a new entry on the queue,
3774 * return 1 meaning the device queue should be run. If we
3775 * were already queued, implying someone else has already
3776 * started the queue, return 0 so the caller doesn't attempt
3777 * to run the queue. Must be run at either splsoftcam
3778 * (or splcam since that encompases splsoftcam).
3781 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3782 u_int32_t new_priority)
3785 u_int32_t old_priority;
3787 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3789 old_priority = pinfo->priority;
3792 * Are we already queued?
3794 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3795 /* Simply reorder based on new priority */
3796 if (new_priority < old_priority) {
3797 camq_change_priority(queue, pinfo->index,
3799 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3800 ("changed priority to %d\n",
3805 /* New entry on the queue */
3806 if (new_priority < old_priority)
3807 pinfo->priority = new_priority;
3809 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3810 ("Inserting onto queue\n"));
3811 pinfo->generation = ++queue->generation;
3812 camq_insert(queue, pinfo);
3819 xpt_run_dev_allocq(struct cam_eb *bus)
3821 struct cam_devq *devq;
3824 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3825 devq = bus->sim->devq;
3827 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3828 (" qfrozen_cnt == 0x%x, entries == %d, "
3829 "openings == %d, active == %d\n",
3830 devq->alloc_queue.qfrozen_cnt,
3831 devq->alloc_queue.entries,
3832 devq->alloc_openings,
3833 devq->alloc_active));
3836 devq->alloc_queue.qfrozen_cnt++;
3837 while ((devq->alloc_queue.entries > 0)
3838 && (devq->alloc_openings > 0)
3839 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3840 struct cam_ed_qinfo *qinfo;
3841 struct cam_ed *device;
3842 union ccb *work_ccb;
3843 struct cam_periph *drv;
3846 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3848 device = qinfo->device;
3850 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3851 ("running device %p\n", device));
3853 drvq = &device->drvq;
3856 if (drvq->entries <= 0) {
3857 panic("xpt_run_dev_allocq: "
3858 "Device on queue without any work to do");
3861 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3862 devq->alloc_openings--;
3863 devq->alloc_active++;
3864 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3866 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3867 drv->pinfo.priority);
3868 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3869 ("calling periph start\n"));
3870 drv->periph_start(drv, work_ccb);
3873 * Malloc failure in alloc_ccb
3876 * XXX add us to a list to be run from free_ccb
3877 * if we don't have any ccbs active on this
3878 * device queue otherwise we may never get run
3884 /* Raise IPL for possible insertion and test at top of loop */
3887 if (drvq->entries > 0) {
3888 /* We have more work. Attempt to reschedule */
3889 xpt_schedule_dev_allocq(bus, device);
3892 devq->alloc_queue.qfrozen_cnt--;
3897 xpt_run_dev_sendq(struct cam_eb *bus)
3899 struct cam_devq *devq;
3902 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3904 devq = bus->sim->devq;
3907 devq->send_queue.qfrozen_cnt++;
3910 while ((devq->send_queue.entries > 0)
3911 && (devq->send_openings > 0)) {
3912 struct cam_ed_qinfo *qinfo;
3913 struct cam_ed *device;
3914 union ccb *work_ccb;
3915 struct cam_sim *sim;
3919 if (devq->send_queue.qfrozen_cnt > 1) {
3924 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3926 device = qinfo->device;
3929 * If the device has been "frozen", don't attempt
3932 if (device->qfrozen_cnt > 0) {
3937 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3938 ("running device %p\n", device));
3940 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3941 if (work_ccb == NULL) {
3942 printf("device on run queue with no ccbs???\n");
3947 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3949 if (num_highpower <= 0) {
3951 * We got a high power command, but we
3952 * don't have any available slots. Freeze
3953 * the device queue until we have a slot
3956 device->qfrozen_cnt++;
3957 STAILQ_INSERT_TAIL(&highpowerq,
3965 * Consume a high power slot while
3971 devq->active_dev = device;
3972 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3974 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3977 devq->send_openings--;
3978 devq->send_active++;
3980 if (device->ccbq.queue.entries > 0)
3981 xpt_schedule_dev_sendq(bus, device);
3983 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3985 * The client wants to freeze the queue
3986 * after this CCB is sent.
3989 device->qfrozen_cnt++;
3995 /* In Target mode, the peripheral driver knows best... */
3996 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3997 if ((device->inq_flags & SID_CmdQue) != 0
3998 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3999 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
4002 * Clear this in case of a retried CCB that
4003 * failed due to a rejected tag.
4005 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
4009 * Device queues can be shared among multiple sim instances
4010 * that reside on different busses. Use the SIM in the queue
4011 * CCB's path, rather than the one in the bus that was passed
4012 * into this function.
4014 sim = work_ccb->ccb_h.path->bus->sim;
4015 (*(sim->sim_action))(sim, work_ccb);
4018 devq->active_dev = NULL;
4020 /* Raise IPL for possible insertion and test at top of loop */
4025 devq->send_queue.qfrozen_cnt--;
4030 * This function merges stuff from the slave ccb into the master ccb, while
4031 * keeping important fields in the master ccb constant.
4034 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
4039 * Pull fields that are valid for peripheral drivers to set
4040 * into the master CCB along with the CCB "payload".
4042 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
4043 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
4044 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
4045 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
4046 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
4047 sizeof(union ccb) - sizeof(struct ccb_hdr));
4051 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
4055 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
4056 ccb_h->pinfo.priority = priority;
4058 ccb_h->path_id = path->bus->path_id;
4060 ccb_h->target_id = path->target->target_id;
4062 ccb_h->target_id = CAM_TARGET_WILDCARD;
4064 ccb_h->target_lun = path->device->lun_id;
4065 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4067 ccb_h->target_lun = CAM_TARGET_WILDCARD;
4069 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4073 /* Path manipulation functions */
4075 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4076 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4078 struct cam_path *path;
4083 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
4086 status = CAM_RESRC_UNAVAIL;
4089 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4090 if (status != CAM_REQ_CMP) {
4091 free(path, M_CAMXPT);
4094 *new_path_ptr = path;
4099 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4100 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4103 struct cam_et *target;
4104 struct cam_ed *device;
4108 status = CAM_REQ_CMP; /* Completed without error */
4109 target = NULL; /* Wildcarded */
4110 device = NULL; /* Wildcarded */
4113 * We will potentially modify the EDT, so block interrupts
4114 * that may attempt to create cam paths.
4117 bus = xpt_find_bus(path_id);
4119 status = CAM_PATH_INVALID;
4121 target = xpt_find_target(bus, target_id);
4122 if (target == NULL) {
4124 struct cam_et *new_target;
4126 new_target = xpt_alloc_target(bus, target_id);
4127 if (new_target == NULL) {
4128 status = CAM_RESRC_UNAVAIL;
4130 target = new_target;
4133 if (target != NULL) {
4134 device = xpt_find_device(target, lun_id);
4135 if (device == NULL) {
4137 struct cam_ed *new_device;
4139 new_device = xpt_alloc_device(bus,
4142 if (new_device == NULL) {
4143 status = CAM_RESRC_UNAVAIL;
4145 device = new_device;
4153 * Only touch the user's data if we are successful.
4155 if (status == CAM_REQ_CMP) {
4156 new_path->periph = perph;
4157 new_path->bus = bus;
4158 new_path->target = target;
4159 new_path->device = device;
4160 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4163 xpt_release_device(bus, target, device);
4165 xpt_release_target(bus, target);
4167 xpt_release_bus(bus);
4173 xpt_release_path(struct cam_path *path)
4175 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4176 if (path->device != NULL) {
4177 xpt_release_device(path->bus, path->target, path->device);
4178 path->device = NULL;
4180 if (path->target != NULL) {
4181 xpt_release_target(path->bus, path->target);
4182 path->target = NULL;
4184 if (path->bus != NULL) {
4185 xpt_release_bus(path->bus);
4191 xpt_free_path(struct cam_path *path)
4195 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4196 xpt_release_path(path);
4197 free(path, M_CAMXPT);
4202 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4203 * in path1, 2 for match with wildcards in path2.
4206 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4212 if (path1->bus != path2->bus) {
4213 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4215 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4220 if (path1->target != path2->target) {
4221 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4224 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4229 if (path1->device != path2->device) {
4230 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4233 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4242 xpt_print_path(struct cam_path *path)
4247 printf("(nopath): ");
4249 if (path->periph != NULL)
4250 printf("(%s%d:", path->periph->periph_name,
4251 path->periph->unit_number);
4253 printf("(noperiph:");
4255 if (path->bus != NULL)
4256 printf("%s%d:%d:", path->bus->sim->sim_name,
4257 path->bus->sim->unit_number,
4258 path->bus->sim->bus_id);
4262 if (path->target != NULL)
4263 printf("%d:", path->target->target_id);
4267 if (path->device != NULL)
4268 printf("%d): ", path->device->lun_id);
4275 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4281 sbuf_new(&sb, str, str_len, 0);
4284 sbuf_printf(&sb, "(nopath): ");
4286 if (path->periph != NULL)
4287 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4288 path->periph->unit_number);
4290 sbuf_printf(&sb, "(noperiph:");
4292 if (path->bus != NULL)
4293 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4294 path->bus->sim->unit_number,
4295 path->bus->sim->bus_id);
4297 sbuf_printf(&sb, "nobus:");
4299 if (path->target != NULL)
4300 sbuf_printf(&sb, "%d:", path->target->target_id);
4302 sbuf_printf(&sb, "X:");
4304 if (path->device != NULL)
4305 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4307 sbuf_printf(&sb, "X): ");
4311 return(sbuf_len(&sb));
4315 xpt_path_path_id(struct cam_path *path)
4319 return(path->bus->path_id);
4323 xpt_path_target_id(struct cam_path *path)
4327 if (path->target != NULL)
4328 return (path->target->target_id);
4330 return (CAM_TARGET_WILDCARD);
4334 xpt_path_lun_id(struct cam_path *path)
4338 if (path->device != NULL)
4339 return (path->device->lun_id);
4341 return (CAM_LUN_WILDCARD);
4345 xpt_path_sim(struct cam_path *path)
4349 return (path->bus->sim);
4353 xpt_path_periph(struct cam_path *path)
4357 return (path->periph);
4361 * Release a CAM control block for the caller. Remit the cost of the structure
4362 * to the device referenced by the path. If the this device had no 'credits'
4363 * and peripheral drivers have registered async callbacks for this notification
4367 xpt_release_ccb(union ccb *free_ccb)
4370 struct cam_path *path;
4371 struct cam_ed *device;
4376 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4377 path = free_ccb->ccb_h.path;
4378 device = path->device;
4381 cam_ccbq_release_opening(&device->ccbq);
4382 if (xpt_ccb_count > xpt_max_ccbs) {
4383 xpt_free_ccb(free_ccb);
4386 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4388 if (bus->sim->devq == NULL) {
4392 bus->sim->devq->alloc_openings++;
4393 bus->sim->devq->alloc_active--;
4394 /* XXX Turn this into an inline function - xpt_run_device?? */
4395 if ((device_is_alloc_queued(device) == 0)
4396 && (device->drvq.entries > 0)) {
4397 xpt_schedule_dev_allocq(bus, device);
4400 if (dev_allocq_is_runnable(bus->sim->devq))
4401 xpt_run_dev_allocq(bus);
4404 /* Functions accessed by SIM drivers */
4407 * A sim structure, listing the SIM entry points and instance
4408 * identification info is passed to xpt_bus_register to hook the SIM
4409 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4410 * for this new bus and places it in the array of busses and assigns
4411 * it a path_id. The path_id may be influenced by "hard wiring"
4412 * information specified by the user. Once interrupt services are
4413 * availible, the bus will be probed.
4416 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4418 struct cam_eb *new_bus;
4419 struct cam_eb *old_bus;
4420 struct ccb_pathinq cpi;
4426 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4427 M_CAMXPT, M_NOWAIT);
4428 if (new_bus == NULL) {
4429 /* Couldn't satisfy request */
4430 return (CAM_RESRC_UNAVAIL);
4433 if (strcmp(sim->sim_name, "xpt") != 0) {
4436 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4439 TAILQ_INIT(&new_bus->et_entries);
4440 new_bus->path_id = sim->path_id;
4442 timevalclear(&new_bus->last_reset);
4444 new_bus->refcount = 1; /* Held until a bus_deregister event */
4445 new_bus->generation = 0;
4447 old_bus = TAILQ_FIRST(&xpt_busses);
4448 while (old_bus != NULL
4449 && old_bus->path_id < new_bus->path_id)
4450 old_bus = TAILQ_NEXT(old_bus, links);
4451 if (old_bus != NULL)
4452 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4454 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4458 /* Notify interested parties */
4459 if (sim->path_id != CAM_XPT_PATH_ID) {
4460 struct cam_path path;
4462 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4463 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4464 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4465 cpi.ccb_h.func_code = XPT_PATH_INQ;
4466 xpt_action((union ccb *)&cpi);
4467 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4468 xpt_release_path(&path);
4470 return (CAM_SUCCESS);
4474 xpt_bus_deregister(path_id_t pathid)
4476 struct cam_path bus_path;
4477 struct cam_ed *device;
4478 struct cam_ed_qinfo *qinfo;
4479 struct cam_devq *devq;
4480 struct cam_periph *periph;
4481 struct cam_sim *ccbsim;
4482 union ccb *work_ccb;
4487 status = xpt_compile_path(&bus_path, NULL, pathid,
4488 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4489 if (status != CAM_REQ_CMP)
4492 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4493 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4495 /* The SIM may be gone, so use a dummy SIM for any stray operations. */
4496 devq = bus_path.bus->sim->devq;
4497 bus_path.bus->sim = &cam_dead_sim;
4499 /* Execute any pending operations now. */
4500 while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
4501 CAMQ_HEAD)) != NULL ||
4502 (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
4503 CAMQ_HEAD)) != NULL) {
4505 device = qinfo->device;
4506 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
4507 if (work_ccb != NULL) {
4508 devq->active_dev = device;
4509 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
4510 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
4511 ccbsim = work_ccb->ccb_h.path->bus->sim;
4512 (*(ccbsim->sim_action))(ccbsim, work_ccb);
4515 periph = (struct cam_periph *)camq_remove(&device->drvq,
4518 xpt_schedule(periph, periph->pinfo.priority);
4519 } while (work_ccb != NULL || periph != NULL);
4522 /* Make sure all completed CCBs are processed. */
4523 while (!TAILQ_EMPTY(&cam_bioq)) {
4526 /* Repeat the async's for the benefit of any new devices. */
4527 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4528 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4531 /* Release the reference count held while registered. */
4532 xpt_release_bus(bus_path.bus);
4533 xpt_release_path(&bus_path);
4535 /* Recheck for more completed CCBs. */
4536 while (!TAILQ_EMPTY(&cam_bioq))
4539 return (CAM_REQ_CMP);
4543 xptnextfreepathid(void)
4550 bus = TAILQ_FIRST(&xpt_busses);
4552 /* Find an unoccupied pathid */
4554 && bus->path_id <= pathid) {
4555 if (bus->path_id == pathid)
4557 bus = TAILQ_NEXT(bus, links);
4561 * Ensure that this pathid is not reserved for
4562 * a bus that may be registered in the future.
4564 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4566 /* Start the search over */
4573 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4580 pathid = CAM_XPT_PATH_ID;
4581 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4583 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4584 if (strcmp(dname, "scbus")) {
4585 /* Avoid a bit of foot shooting. */
4588 if (dunit < 0) /* unwired?! */
4590 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4591 if (sim_bus == val) {
4595 } else if (sim_bus == 0) {
4596 /* Unspecified matches bus 0 */
4600 printf("Ambiguous scbus configuration for %s%d "
4601 "bus %d, cannot wire down. The kernel "
4602 "config entry for scbus%d should "
4603 "specify a controller bus.\n"
4604 "Scbus will be assigned dynamically.\n",
4605 sim_name, sim_unit, sim_bus, dunit);
4610 if (pathid == CAM_XPT_PATH_ID)
4611 pathid = xptnextfreepathid();
4616 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4619 struct cam_et *target, *next_target;
4620 struct cam_ed *device, *next_device;
4625 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4628 * Most async events come from a CAM interrupt context. In
4629 * a few cases, the error recovery code at the peripheral layer,
4630 * which may run from our SWI or a process context, may signal
4631 * deferred events with a call to xpt_async. Ensure async
4632 * notifications are serialized by blocking cam interrupts.
4638 if (async_code == AC_BUS_RESET) {
4642 /* Update our notion of when the last reset occurred */
4643 microtime(&bus->last_reset);
4647 for (target = TAILQ_FIRST(&bus->et_entries);
4649 target = next_target) {
4651 next_target = TAILQ_NEXT(target, links);
4653 if (path->target != target
4654 && path->target->target_id != CAM_TARGET_WILDCARD
4655 && target->target_id != CAM_TARGET_WILDCARD)
4658 if (async_code == AC_SENT_BDR) {
4661 /* Update our notion of when the last reset occurred */
4663 microtime(&path->target->last_reset);
4667 for (device = TAILQ_FIRST(&target->ed_entries);
4669 device = next_device) {
4671 next_device = TAILQ_NEXT(device, links);
4673 if (path->device != device
4674 && path->device->lun_id != CAM_LUN_WILDCARD
4675 && device->lun_id != CAM_LUN_WILDCARD)
4678 xpt_dev_async(async_code, bus, target,
4681 xpt_async_bcast(&device->asyncs, async_code,
4687 * If this wasn't a fully wildcarded async, tell all
4688 * clients that want all async events.
4690 if (bus != xpt_periph->path->bus)
4691 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4697 xpt_async_bcast(struct async_list *async_head,
4698 u_int32_t async_code,
4699 struct cam_path *path, void *async_arg)
4701 struct async_node *cur_entry;
4703 cur_entry = SLIST_FIRST(async_head);
4704 while (cur_entry != NULL) {
4705 struct async_node *next_entry;
4707 * Grab the next list entry before we call the current
4708 * entry's callback. This is because the callback function
4709 * can delete its async callback entry.
4711 next_entry = SLIST_NEXT(cur_entry, links);
4712 if ((cur_entry->event_enable & async_code) != 0)
4713 cur_entry->callback(cur_entry->callback_arg,
4716 cur_entry = next_entry;
4721 * Handle any per-device event notifications that require action by the XPT.
4724 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4725 struct cam_ed *device, void *async_arg)
4728 struct cam_path newpath;
4731 * We only need to handle events for real devices.
4733 if (target->target_id == CAM_TARGET_WILDCARD
4734 || device->lun_id == CAM_LUN_WILDCARD)
4738 * We need our own path with wildcards expanded to
4739 * handle certain types of events.
4741 if ((async_code == AC_SENT_BDR)
4742 || (async_code == AC_BUS_RESET)
4743 || (async_code == AC_INQ_CHANGED))
4744 status = xpt_compile_path(&newpath, NULL,
4749 status = CAM_REQ_CMP_ERR;
4751 if (status == CAM_REQ_CMP) {
4754 * Allow transfer negotiation to occur in a
4755 * tag free environment.
4757 if (async_code == AC_SENT_BDR
4758 || async_code == AC_BUS_RESET)
4759 xpt_toggle_tags(&newpath);
4761 if (async_code == AC_INQ_CHANGED) {
4763 * We've sent a start unit command, or
4764 * something similar to a device that
4765 * may have caused its inquiry data to
4766 * change. So we re-scan the device to
4767 * refresh the inquiry data for it.
4769 xpt_scan_lun(newpath.periph, &newpath,
4770 CAM_EXPECT_INQ_CHANGE, NULL);
4772 xpt_release_path(&newpath);
4773 } else if (async_code == AC_LOST_DEVICE) {
4774 device->flags |= CAM_DEV_UNCONFIGURED;
4775 } else if (async_code == AC_TRANSFER_NEG) {
4776 struct ccb_trans_settings *settings;
4778 settings = (struct ccb_trans_settings *)async_arg;
4779 xpt_set_transfer_settings(settings, device,
4780 /*async_update*/TRUE);
4785 xpt_freeze_devq(struct cam_path *path, u_int count)
4788 struct ccb_hdr *ccbh;
4793 path->device->qfrozen_cnt += count;
4796 * Mark the last CCB in the queue as needing
4797 * to be requeued if the driver hasn't
4798 * changed it's state yet. This fixes a race
4799 * where a ccb is just about to be queued to
4800 * a controller driver when it's interrupt routine
4801 * freezes the queue. To completly close the
4802 * hole, controller drives must check to see
4803 * if a ccb's status is still CAM_REQ_INPROG
4804 * under spl protection just before they queue
4805 * the CCB. See ahc_action/ahc_freeze_devq for
4808 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4809 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4810 ccbh->status = CAM_REQUEUE_REQ;
4812 return (path->device->qfrozen_cnt);
4816 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4820 sim->devq->send_queue.qfrozen_cnt += count;
4821 if (sim->devq->active_dev != NULL) {
4822 struct ccb_hdr *ccbh;
4824 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4826 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4827 ccbh->status = CAM_REQUEUE_REQ;
4829 return (sim->devq->send_queue.qfrozen_cnt);
4833 xpt_release_devq_timeout(void *arg)
4835 struct cam_ed *device;
4837 device = (struct cam_ed *)arg;
4839 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4843 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4847 xpt_release_devq_device(path->device, count, run_queue);
4851 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4859 if (dev->qfrozen_cnt > 0) {
4861 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4862 dev->qfrozen_cnt -= count;
4863 if (dev->qfrozen_cnt == 0) {
4866 * No longer need to wait for a successful
4867 * command completion.
4869 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4872 * Remove any timeouts that might be scheduled
4873 * to release this queue.
4875 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4876 untimeout(xpt_release_devq_timeout, dev,
4878 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4882 * Now that we are unfrozen schedule the
4883 * device so any pending transactions are
4886 if ((dev->ccbq.queue.entries > 0)
4887 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4888 && (run_queue != 0)) {
4895 xpt_run_dev_sendq(dev->target->bus);
4900 xpt_release_simq(struct cam_sim *sim, int run_queue)
4907 sendq = &(sim->devq->send_queue);
4909 if (sendq->qfrozen_cnt > 0) {
4911 sendq->qfrozen_cnt--;
4912 if (sendq->qfrozen_cnt == 0) {
4916 * If there is a timeout scheduled to release this
4917 * sim queue, remove it. The queue frozen count is
4920 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4921 untimeout(xpt_release_simq_timeout, sim,
4923 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4925 bus = xpt_find_bus(sim->path_id);
4930 * Now that we are unfrozen run the send queue.
4932 xpt_run_dev_sendq(bus);
4934 xpt_release_bus(bus);
4942 xpt_release_simq_timeout(void *arg)
4944 struct cam_sim *sim;
4946 sim = (struct cam_sim *)arg;
4947 xpt_release_simq(sim, /* run_queue */ TRUE);
4951 xpt_done(union ccb *done_ccb)
4957 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4958 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4960 * Queue up the request for handling by our SWI handler
4961 * any of the "non-immediate" type of ccbs.
4963 switch (done_ccb->ccb_h.path->periph->type) {
4964 case CAM_PERIPH_BIO:
4965 mtx_lock(&cam_bioq_lock);
4966 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4968 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4969 mtx_unlock(&cam_bioq_lock);
4970 swi_sched(cambio_ih, 0);
4973 panic("unknown periph type %d",
4974 done_ccb->ccb_h.path->periph->type);
4987 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4992 xpt_alloc_ccb_nowait()
4998 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
5003 xpt_free_ccb(union ccb *free_ccb)
5005 free(free_ccb, M_CAMXPT);
5010 /* Private XPT functions */
5013 * Get a CAM control block for the caller. Charge the structure to the device
5014 * referenced by the path. If the this device has no 'credits' then the
5015 * device already has the maximum number of outstanding operations under way
5016 * and we return NULL. If we don't have sufficient resources to allocate more
5017 * ccbs, we also return NULL.
5020 xpt_get_ccb(struct cam_ed *device)
5026 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
5027 new_ccb = xpt_alloc_ccb_nowait();
5028 if (new_ccb == NULL) {
5032 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
5033 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
5037 cam_ccbq_take_opening(&device->ccbq);
5038 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
5044 xpt_release_bus(struct cam_eb *bus)
5049 if ((--bus->refcount == 0)
5050 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
5051 TAILQ_REMOVE(&xpt_busses, bus, links);
5054 free(bus, M_CAMXPT);
5059 static struct cam_et *
5060 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
5062 struct cam_et *target;
5064 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
5065 if (target != NULL) {
5066 struct cam_et *cur_target;
5068 TAILQ_INIT(&target->ed_entries);
5070 target->target_id = target_id;
5071 target->refcount = 1;
5072 target->generation = 0;
5073 timevalclear(&target->last_reset);
5075 * Hold a reference to our parent bus so it
5076 * will not go away before we do.
5080 /* Insertion sort into our bus's target list */
5081 cur_target = TAILQ_FIRST(&bus->et_entries);
5082 while (cur_target != NULL && cur_target->target_id < target_id)
5083 cur_target = TAILQ_NEXT(cur_target, links);
5085 if (cur_target != NULL) {
5086 TAILQ_INSERT_BEFORE(cur_target, target, links);
5088 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
5096 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
5101 if ((--target->refcount == 0)
5102 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
5103 TAILQ_REMOVE(&bus->et_entries, target, links);
5106 free(target, M_CAMXPT);
5107 xpt_release_bus(bus);
5112 static struct cam_ed *
5113 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5115 #ifdef CAM_NEW_TRAN_CODE
5116 struct cam_path path;
5117 #endif /* CAM_NEW_TRAN_CODE */
5118 struct cam_ed *device;
5119 struct cam_devq *devq;
5122 if (SIM_DEAD(bus->sim))
5125 /* Make space for us in the device queue on our bus */
5126 devq = bus->sim->devq;
5127 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5129 if (status != CAM_REQ_CMP) {
5132 device = (struct cam_ed *)malloc(sizeof(*device),
5133 M_CAMXPT, M_NOWAIT);
5136 if (device != NULL) {
5137 struct cam_ed *cur_device;
5139 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5140 device->alloc_ccb_entry.device = device;
5141 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5142 device->send_ccb_entry.device = device;
5143 device->target = target;
5144 device->lun_id = lun_id;
5145 /* Initialize our queues */
5146 if (camq_init(&device->drvq, 0) != 0) {
5147 free(device, M_CAMXPT);
5150 if (cam_ccbq_init(&device->ccbq,
5151 bus->sim->max_dev_openings) != 0) {
5152 camq_fini(&device->drvq);
5153 free(device, M_CAMXPT);
5156 SLIST_INIT(&device->asyncs);
5157 SLIST_INIT(&device->periphs);
5158 device->generation = 0;
5159 device->owner = NULL;
5161 * Take the default quirk entry until we have inquiry
5162 * data and can determine a better quirk to use.
5164 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5165 bzero(&device->inq_data, sizeof(device->inq_data));
5166 device->inq_flags = 0;
5167 device->queue_flags = 0;
5168 device->serial_num = NULL;
5169 device->serial_num_len = 0;
5170 device->qfrozen_cnt = 0;
5171 device->flags = CAM_DEV_UNCONFIGURED;
5172 device->tag_delay_count = 0;
5173 device->tag_saved_openings = 0;
5174 device->refcount = 1;
5175 callout_handle_init(&device->c_handle);
5178 * Hold a reference to our parent target so it
5179 * will not go away before we do.
5184 * XXX should be limited by number of CCBs this bus can
5187 xpt_max_ccbs += device->ccbq.devq_openings;
5188 /* Insertion sort into our target's device list */
5189 cur_device = TAILQ_FIRST(&target->ed_entries);
5190 while (cur_device != NULL && cur_device->lun_id < lun_id)
5191 cur_device = TAILQ_NEXT(cur_device, links);
5192 if (cur_device != NULL) {
5193 TAILQ_INSERT_BEFORE(cur_device, device, links);
5195 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5197 target->generation++;
5198 #ifdef CAM_NEW_TRAN_CODE
5199 if (lun_id != CAM_LUN_WILDCARD) {
5200 xpt_compile_path(&path,
5205 xpt_devise_transport(&path);
5206 xpt_release_path(&path);
5208 #endif /* CAM_NEW_TRAN_CODE */
5214 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5215 struct cam_ed *device)
5220 if ((--device->refcount == 0)
5221 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5222 struct cam_devq *devq;
5224 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5225 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5226 panic("Removing device while still queued for ccbs");
5228 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5229 untimeout(xpt_release_devq_timeout, device,
5232 TAILQ_REMOVE(&target->ed_entries, device,links);
5233 target->generation++;
5234 xpt_max_ccbs -= device->ccbq.devq_openings;
5235 if (!SIM_DEAD(bus->sim)) {
5236 /* Release our slot in the devq */
5237 devq = bus->sim->devq;
5238 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5241 camq_fini(&device->drvq);
5242 camq_fini(&device->ccbq.queue);
5243 free(device, M_CAMXPT);
5244 xpt_release_target(bus, target);
5250 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5260 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5261 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5262 if (result == CAM_REQ_CMP && (diff < 0)) {
5263 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5265 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5266 || (dev->inq_flags & SID_CmdQue) != 0)
5267 dev->tag_saved_openings = newopenings;
5268 /* Adjust the global limit */
5269 xpt_max_ccbs += diff;
5274 static struct cam_eb *
5275 xpt_find_bus(path_id_t path_id)
5279 for (bus = TAILQ_FIRST(&xpt_busses);
5281 bus = TAILQ_NEXT(bus, links)) {
5282 if (bus->path_id == path_id) {
5290 static struct cam_et *
5291 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5293 struct cam_et *target;
5295 for (target = TAILQ_FIRST(&bus->et_entries);
5297 target = TAILQ_NEXT(target, links)) {
5298 if (target->target_id == target_id) {
5306 static struct cam_ed *
5307 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5309 struct cam_ed *device;
5311 for (device = TAILQ_FIRST(&target->ed_entries);
5313 device = TAILQ_NEXT(device, links)) {
5314 if (device->lun_id == lun_id) {
5323 union ccb *request_ccb;
5324 struct ccb_pathinq *cpi;
5326 } xpt_scan_bus_info;
5329 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5330 * As the scan progresses, xpt_scan_bus is used as the
5331 * callback on completion function.
5334 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5336 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5337 ("xpt_scan_bus\n"));
5338 switch (request_ccb->ccb_h.func_code) {
5341 xpt_scan_bus_info *scan_info;
5342 union ccb *work_ccb;
5343 struct cam_path *path;
5348 /* Find out the characteristics of the bus */
5349 work_ccb = xpt_alloc_ccb();
5350 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5351 request_ccb->ccb_h.pinfo.priority);
5352 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5353 xpt_action(work_ccb);
5354 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5355 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5356 xpt_free_ccb(work_ccb);
5357 xpt_done(request_ccb);
5361 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5363 * Can't scan the bus on an adapter that
5364 * cannot perform the initiator role.
5366 request_ccb->ccb_h.status = CAM_REQ_CMP;
5367 xpt_free_ccb(work_ccb);
5368 xpt_done(request_ccb);
5372 /* Save some state for use while we probe for devices */
5373 scan_info = (xpt_scan_bus_info *)
5374 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5375 scan_info->request_ccb = request_ccb;
5376 scan_info->cpi = &work_ccb->cpi;
5378 /* Cache on our stack so we can work asynchronously */
5379 max_target = scan_info->cpi->max_target;
5380 initiator_id = scan_info->cpi->initiator_id;
5384 * We can scan all targets in parallel, or do it sequentially.
5386 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5388 scan_info->counter = 0;
5390 scan_info->counter = scan_info->cpi->max_target + 1;
5391 if (scan_info->cpi->initiator_id < scan_info->counter) {
5392 scan_info->counter--;
5396 for (i = 0; i <= max_target; i++) {
5398 if (i == initiator_id)
5401 status = xpt_create_path(&path, xpt_periph,
5402 request_ccb->ccb_h.path_id,
5404 if (status != CAM_REQ_CMP) {
5405 printf("xpt_scan_bus: xpt_create_path failed"
5406 " with status %#x, bus scan halted\n",
5408 free(scan_info, M_TEMP);
5409 request_ccb->ccb_h.status = status;
5410 xpt_free_ccb(work_ccb);
5411 xpt_done(request_ccb);
5414 work_ccb = xpt_alloc_ccb();
5415 xpt_setup_ccb(&work_ccb->ccb_h, path,
5416 request_ccb->ccb_h.pinfo.priority);
5417 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5418 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5419 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5420 work_ccb->crcn.flags = request_ccb->crcn.flags;
5421 xpt_action(work_ccb);
5428 struct cam_path *path;
5429 xpt_scan_bus_info *scan_info;
5431 target_id_t target_id;
5434 /* Reuse the same CCB to query if a device was really found */
5435 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5436 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5437 request_ccb->ccb_h.pinfo.priority);
5438 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5440 path_id = request_ccb->ccb_h.path_id;
5441 target_id = request_ccb->ccb_h.target_id;
5442 lun_id = request_ccb->ccb_h.target_lun;
5443 xpt_action(request_ccb);
5445 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5446 struct cam_ed *device;
5447 struct cam_et *target;
5451 * If we already probed lun 0 successfully, or
5452 * we have additional configured luns on this
5453 * target that might have "gone away", go onto
5456 target = request_ccb->ccb_h.path->target;
5458 * We may touch devices that we don't
5459 * hold references too, so ensure they
5460 * don't disappear out from under us.
5461 * The target above is referenced by the
5462 * path in the request ccb.
5466 device = TAILQ_FIRST(&target->ed_entries);
5467 if (device != NULL) {
5468 phl = CAN_SRCH_HI_SPARSE(device);
5469 if (device->lun_id == 0)
5470 device = TAILQ_NEXT(device, links);
5473 if ((lun_id != 0) || (device != NULL)) {
5474 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5478 struct cam_ed *device;
5480 device = request_ccb->ccb_h.path->device;
5482 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5483 /* Try the next lun */
5484 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5485 || CAN_SRCH_HI_DENSE(device))
5491 * Free the current request path- we're done with it.
5493 xpt_free_path(request_ccb->ccb_h.path);
5496 * Check to see if we scan any further luns.
5498 if (lun_id == request_ccb->ccb_h.target_lun
5499 || lun_id > scan_info->cpi->max_lun) {
5504 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5505 scan_info->counter++;
5506 if (scan_info->counter ==
5507 scan_info->cpi->initiator_id) {
5508 scan_info->counter++;
5510 if (scan_info->counter >=
5511 scan_info->cpi->max_target+1) {
5515 scan_info->counter--;
5516 if (scan_info->counter == 0) {
5521 xpt_free_ccb(request_ccb);
5522 xpt_free_ccb((union ccb *)scan_info->cpi);
5523 request_ccb = scan_info->request_ccb;
5524 free(scan_info, M_TEMP);
5525 request_ccb->ccb_h.status = CAM_REQ_CMP;
5526 xpt_done(request_ccb);
5530 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5533 status = xpt_create_path(&path, xpt_periph,
5534 scan_info->request_ccb->ccb_h.path_id,
5535 scan_info->counter, 0);
5536 if (status != CAM_REQ_CMP) {
5537 printf("xpt_scan_bus: xpt_create_path failed"
5538 " with status %#x, bus scan halted\n",
5540 xpt_free_ccb(request_ccb);
5541 xpt_free_ccb((union ccb *)scan_info->cpi);
5542 request_ccb = scan_info->request_ccb;
5543 free(scan_info, M_TEMP);
5544 request_ccb->ccb_h.status = status;
5545 xpt_done(request_ccb);
5548 xpt_setup_ccb(&request_ccb->ccb_h, path,
5549 request_ccb->ccb_h.pinfo.priority);
5550 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5551 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5552 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5553 request_ccb->crcn.flags =
5554 scan_info->request_ccb->crcn.flags;
5556 status = xpt_create_path(&path, xpt_periph,
5557 path_id, target_id, lun_id);
5558 if (status != CAM_REQ_CMP) {
5559 printf("xpt_scan_bus: xpt_create_path failed "
5560 "with status %#x, halting LUN scan\n",
5564 xpt_setup_ccb(&request_ccb->ccb_h, path,
5565 request_ccb->ccb_h.pinfo.priority);
5566 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5567 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5568 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5569 request_ccb->crcn.flags =
5570 scan_info->request_ccb->crcn.flags;
5572 xpt_action(request_ccb);
5586 PROBE_TUR_FOR_NEGOTIATION
5590 PROBE_INQUIRY_CKSUM = 0x01,
5591 PROBE_SERIAL_CKSUM = 0x02,
5592 PROBE_NO_ANNOUNCE = 0x04
5596 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5597 probe_action action;
5598 union ccb saved_ccb;
5601 u_int8_t digest[16];
5605 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5606 cam_flags flags, union ccb *request_ccb)
5608 struct ccb_pathinq cpi;
5610 struct cam_path *new_path;
5611 struct cam_periph *old_periph;
5614 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5615 ("xpt_scan_lun\n"));
5617 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5618 cpi.ccb_h.func_code = XPT_PATH_INQ;
5619 xpt_action((union ccb *)&cpi);
5621 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5622 if (request_ccb != NULL) {
5623 request_ccb->ccb_h.status = cpi.ccb_h.status;
5624 xpt_done(request_ccb);
5629 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5631 * Can't scan the bus on an adapter that
5632 * cannot perform the initiator role.
5634 if (request_ccb != NULL) {
5635 request_ccb->ccb_h.status = CAM_REQ_CMP;
5636 xpt_done(request_ccb);
5641 if (request_ccb == NULL) {
5642 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5643 if (request_ccb == NULL) {
5644 xpt_print_path(path);
5645 printf("xpt_scan_lun: can't allocate CCB, can't "
5649 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5650 if (new_path == NULL) {
5651 xpt_print_path(path);
5652 printf("xpt_scan_lun: can't allocate path, can't "
5654 free(request_ccb, M_TEMP);
5657 status = xpt_compile_path(new_path, xpt_periph,
5659 path->target->target_id,
5660 path->device->lun_id);
5662 if (status != CAM_REQ_CMP) {
5663 xpt_print_path(path);
5664 printf("xpt_scan_lun: can't compile path, can't "
5666 free(request_ccb, M_TEMP);
5667 free(new_path, M_TEMP);
5670 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5671 request_ccb->ccb_h.cbfcnp = xptscandone;
5672 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5673 request_ccb->crcn.flags = flags;
5677 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5680 softc = (probe_softc *)old_periph->softc;
5681 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5684 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5685 probestart, "probe",
5687 request_ccb->ccb_h.path, NULL, 0,
5690 if (status != CAM_REQ_CMP) {
5691 xpt_print_path(path);
5692 printf("xpt_scan_lun: cam_alloc_periph returned an "
5693 "error, can't continue probe\n");
5694 request_ccb->ccb_h.status = status;
5695 xpt_done(request_ccb);
5702 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5704 xpt_release_path(done_ccb->ccb_h.path);
5705 free(done_ccb->ccb_h.path, M_TEMP);
5706 free(done_ccb, M_TEMP);
5710 proberegister(struct cam_periph *periph, void *arg)
5712 union ccb *request_ccb; /* CCB representing the probe request */
5715 request_ccb = (union ccb *)arg;
5716 if (periph == NULL) {
5717 printf("proberegister: periph was NULL!!\n");
5718 return(CAM_REQ_CMP_ERR);
5721 if (request_ccb == NULL) {
5722 printf("proberegister: no probe CCB, "
5723 "can't register device\n");
5724 return(CAM_REQ_CMP_ERR);
5727 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5729 if (softc == NULL) {
5730 printf("proberegister: Unable to probe new device. "
5731 "Unable to allocate softc\n");
5732 return(CAM_REQ_CMP_ERR);
5734 TAILQ_INIT(&softc->request_ccbs);
5735 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5738 periph->softc = softc;
5739 cam_periph_acquire(periph);
5741 * Ensure we've waited at least a bus settle
5742 * delay before attempting to probe the device.
5743 * For HBAs that don't do bus resets, this won't make a difference.
5745 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5747 probeschedule(periph);
5748 return(CAM_REQ_CMP);
5752 probeschedule(struct cam_periph *periph)
5754 struct ccb_pathinq cpi;
5758 softc = (probe_softc *)periph->softc;
5759 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5761 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5762 cpi.ccb_h.func_code = XPT_PATH_INQ;
5763 xpt_action((union ccb *)&cpi);
5766 * If a device has gone away and another device, or the same one,
5767 * is back in the same place, it should have a unit attention
5768 * condition pending. It will not report the unit attention in
5769 * response to an inquiry, which may leave invalid transfer
5770 * negotiations in effect. The TUR will reveal the unit attention
5771 * condition. Only send the TUR for lun 0, since some devices
5772 * will get confused by commands other than inquiry to non-existent
5773 * luns. If you think a device has gone away start your scan from
5774 * lun 0. This will insure that any bogus transfer settings are
5777 * If we haven't seen the device before and the controller supports
5778 * some kind of transfer negotiation, negotiate with the first
5779 * sent command if no bus reset was performed at startup. This
5780 * ensures that the device is not confused by transfer negotiation
5781 * settings left over by loader or BIOS action.
5783 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5784 && (ccb->ccb_h.target_lun == 0)) {
5785 softc->action = PROBE_TUR;
5786 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5787 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5788 proberequestdefaultnegotiation(periph);
5789 softc->action = PROBE_INQUIRY;
5791 softc->action = PROBE_INQUIRY;
5794 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5795 softc->flags |= PROBE_NO_ANNOUNCE;
5797 softc->flags &= ~PROBE_NO_ANNOUNCE;
5799 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5803 probestart(struct cam_periph *periph, union ccb *start_ccb)
5805 /* Probe the device that our peripheral driver points to */
5806 struct ccb_scsiio *csio;
5809 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5811 softc = (probe_softc *)periph->softc;
5812 csio = &start_ccb->csio;
5814 switch (softc->action) {
5816 case PROBE_TUR_FOR_NEGOTIATION:
5818 scsi_test_unit_ready(csio,
5827 case PROBE_FULL_INQUIRY:
5830 struct scsi_inquiry_data *inq_buf;
5832 inq_buf = &periph->path->device->inq_data;
5834 * If the device is currently configured, we calculate an
5835 * MD5 checksum of the inquiry data, and if the serial number
5836 * length is greater than 0, add the serial number data
5837 * into the checksum as well. Once the inquiry and the
5838 * serial number check finish, we attempt to figure out
5839 * whether we still have the same device.
5841 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5843 MD5Init(&softc->context);
5844 MD5Update(&softc->context, (unsigned char *)inq_buf,
5845 sizeof(struct scsi_inquiry_data));
5846 softc->flags |= PROBE_INQUIRY_CKSUM;
5847 if (periph->path->device->serial_num_len > 0) {
5848 MD5Update(&softc->context,
5849 periph->path->device->serial_num,
5850 periph->path->device->serial_num_len);
5851 softc->flags |= PROBE_SERIAL_CKSUM;
5853 MD5Final(softc->digest, &softc->context);
5856 if (softc->action == PROBE_INQUIRY)
5857 inquiry_len = SHORT_INQUIRY_LENGTH;
5859 inquiry_len = inq_buf->additional_length
5860 + offsetof(struct scsi_inquiry_data,
5861 additional_length) + 1;
5864 * Some parallel SCSI devices fail to send an
5865 * ignore wide residue message when dealing with
5866 * odd length inquiry requests. Round up to be
5869 inquiry_len = roundup2(inquiry_len, 2);
5875 (u_int8_t *)inq_buf,
5880 /*timeout*/60 * 1000);
5883 case PROBE_MODE_SENSE:
5888 mode_buf_len = sizeof(struct scsi_mode_header_6)
5889 + sizeof(struct scsi_mode_blk_desc)
5890 + sizeof(struct scsi_control_page);
5891 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5892 if (mode_buf != NULL) {
5893 scsi_mode_sense(csio,
5898 SMS_PAGE_CTRL_CURRENT,
5899 SMS_CONTROL_MODE_PAGE,
5906 xpt_print_path(periph->path);
5907 printf("Unable to mode sense control page - malloc failure\n");
5908 softc->action = PROBE_SERIAL_NUM;
5911 case PROBE_SERIAL_NUM:
5913 struct scsi_vpd_unit_serial_number *serial_buf;
5914 struct cam_ed* device;
5917 device = periph->path->device;
5918 device->serial_num = NULL;
5919 device->serial_num_len = 0;
5921 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5922 serial_buf = (struct scsi_vpd_unit_serial_number *)
5923 malloc(sizeof(*serial_buf), M_TEMP,
5926 if (serial_buf != NULL) {
5931 (u_int8_t *)serial_buf,
5932 sizeof(*serial_buf),
5934 SVPD_UNIT_SERIAL_NUMBER,
5936 /*timeout*/60 * 1000);
5940 * We'll have to do without, let our probedone
5941 * routine finish up for us.
5943 start_ccb->csio.data_ptr = NULL;
5944 probedone(periph, start_ccb);
5948 xpt_action(start_ccb);
5952 proberequestdefaultnegotiation(struct cam_periph *periph)
5954 struct ccb_trans_settings cts;
5956 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5957 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5958 #ifdef CAM_NEW_TRAN_CODE
5959 cts.type = CTS_TYPE_USER_SETTINGS;
5960 #else /* CAM_NEW_TRAN_CODE */
5961 cts.flags = CCB_TRANS_USER_SETTINGS;
5962 #endif /* CAM_NEW_TRAN_CODE */
5963 xpt_action((union ccb *)&cts);
5964 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5965 #ifdef CAM_NEW_TRAN_CODE
5966 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5967 #else /* CAM_NEW_TRAN_CODE */
5968 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5969 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5970 #endif /* CAM_NEW_TRAN_CODE */
5971 xpt_action((union ccb *)&cts);
5975 probedone(struct cam_periph *periph, union ccb *done_ccb)
5978 struct cam_path *path;
5981 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5983 softc = (probe_softc *)periph->softc;
5984 path = done_ccb->ccb_h.path;
5985 priority = done_ccb->ccb_h.pinfo.priority;
5987 switch (softc->action) {
5990 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5992 if (cam_periph_error(done_ccb, 0,
5993 SF_NO_PRINT, NULL) == ERESTART)
5995 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5996 /* Don't wedge the queue */
5997 xpt_release_devq(done_ccb->ccb_h.path,
6001 softc->action = PROBE_INQUIRY;
6002 xpt_release_ccb(done_ccb);
6003 xpt_schedule(periph, priority);
6007 case PROBE_FULL_INQUIRY:
6009 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6010 struct scsi_inquiry_data *inq_buf;
6011 u_int8_t periph_qual;
6013 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
6014 inq_buf = &path->device->inq_data;
6016 periph_qual = SID_QUAL(inq_buf);
6018 switch(periph_qual) {
6019 case SID_QUAL_LU_CONNECTED:
6024 * We conservatively request only
6025 * SHORT_INQUIRY_LEN bytes of inquiry
6026 * information during our first try
6027 * at sending an INQUIRY. If the device
6028 * has more information to give,
6029 * perform a second request specifying
6030 * the amount of information the device
6031 * is willing to give.
6033 len = inq_buf->additional_length
6034 + offsetof(struct scsi_inquiry_data,
6035 additional_length) + 1;
6036 if (softc->action == PROBE_INQUIRY
6037 && len > SHORT_INQUIRY_LENGTH) {
6038 softc->action = PROBE_FULL_INQUIRY;
6039 xpt_release_ccb(done_ccb);
6040 xpt_schedule(periph, priority);
6044 xpt_find_quirk(path->device);
6046 #ifdef CAM_NEW_TRAN_CODE
6047 xpt_devise_transport(path);
6048 #endif /* CAM_NEW_TRAN_CODE */
6049 if (INQ_DATA_TQ_ENABLED(inq_buf))
6050 softc->action = PROBE_MODE_SENSE;
6052 softc->action = PROBE_SERIAL_NUM;
6054 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6056 xpt_release_ccb(done_ccb);
6057 xpt_schedule(periph, priority);
6063 } else if (cam_periph_error(done_ccb, 0,
6064 done_ccb->ccb_h.target_lun > 0
6065 ? SF_RETRY_UA|SF_QUIET_IR
6067 &softc->saved_ccb) == ERESTART) {
6069 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6070 /* Don't wedge the queue */
6071 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6075 * If we get to this point, we got an error status back
6076 * from the inquiry and the error status doesn't require
6077 * automatically retrying the command. Therefore, the
6078 * inquiry failed. If we had inquiry information before
6079 * for this device, but this latest inquiry command failed,
6080 * the device has probably gone away. If this device isn't
6081 * already marked unconfigured, notify the peripheral
6082 * drivers that this device is no more.
6084 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
6085 /* Send the async notification. */
6086 xpt_async(AC_LOST_DEVICE, path, NULL);
6088 xpt_release_ccb(done_ccb);
6091 case PROBE_MODE_SENSE:
6093 struct ccb_scsiio *csio;
6094 struct scsi_mode_header_6 *mode_hdr;
6096 csio = &done_ccb->csio;
6097 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
6098 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6099 struct scsi_control_page *page;
6102 offset = ((u_int8_t *)&mode_hdr[1])
6103 + mode_hdr->blk_desc_len;
6104 page = (struct scsi_control_page *)offset;
6105 path->device->queue_flags = page->queue_flags;
6106 } else if (cam_periph_error(done_ccb, 0,
6107 SF_RETRY_UA|SF_NO_PRINT,
6108 &softc->saved_ccb) == ERESTART) {
6110 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6111 /* Don't wedge the queue */
6112 xpt_release_devq(done_ccb->ccb_h.path,
6113 /*count*/1, /*run_queue*/TRUE);
6115 xpt_release_ccb(done_ccb);
6116 free(mode_hdr, M_TEMP);
6117 softc->action = PROBE_SERIAL_NUM;
6118 xpt_schedule(periph, priority);
6121 case PROBE_SERIAL_NUM:
6123 struct ccb_scsiio *csio;
6124 struct scsi_vpd_unit_serial_number *serial_buf;
6131 csio = &done_ccb->csio;
6132 priority = done_ccb->ccb_h.pinfo.priority;
6134 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
6136 /* Clean up from previous instance of this device */
6137 if (path->device->serial_num != NULL) {
6138 free(path->device->serial_num, M_CAMXPT);
6139 path->device->serial_num = NULL;
6140 path->device->serial_num_len = 0;
6143 if (serial_buf == NULL) {
6145 * Don't process the command as it was never sent
6147 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
6148 && (serial_buf->length > 0)) {
6151 path->device->serial_num =
6152 (u_int8_t *)malloc((serial_buf->length + 1),
6153 M_CAMXPT, M_NOWAIT);
6154 if (path->device->serial_num != NULL) {
6155 bcopy(serial_buf->serial_num,
6156 path->device->serial_num,
6157 serial_buf->length);
6158 path->device->serial_num_len =
6160 path->device->serial_num[serial_buf->length]
6163 } else if (cam_periph_error(done_ccb, 0,
6164 SF_RETRY_UA|SF_NO_PRINT,
6165 &softc->saved_ccb) == ERESTART) {
6167 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6168 /* Don't wedge the queue */
6169 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6174 * Let's see if we have seen this device before.
6176 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6178 u_int8_t digest[16];
6183 (unsigned char *)&path->device->inq_data,
6184 sizeof(struct scsi_inquiry_data));
6187 MD5Update(&context, serial_buf->serial_num,
6188 serial_buf->length);
6190 MD5Final(digest, &context);
6191 if (bcmp(softc->digest, digest, 16) == 0)
6195 * XXX Do we need to do a TUR in order to ensure
6196 * that the device really hasn't changed???
6199 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6200 xpt_async(AC_LOST_DEVICE, path, NULL);
6202 if (serial_buf != NULL)
6203 free(serial_buf, M_TEMP);
6207 * Now that we have all the necessary
6208 * information to safely perform transfer
6209 * negotiations... Controllers don't perform
6210 * any negotiation or tagged queuing until
6211 * after the first XPT_SET_TRAN_SETTINGS ccb is
6212 * received. So, on a new device, just retreive
6213 * the user settings, and set them as the current
6214 * settings to set the device up.
6216 proberequestdefaultnegotiation(periph);
6217 xpt_release_ccb(done_ccb);
6220 * Perform a TUR to allow the controller to
6221 * perform any necessary transfer negotiation.
6223 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6224 xpt_schedule(periph, priority);
6227 xpt_release_ccb(done_ccb);
6230 case PROBE_TUR_FOR_NEGOTIATION:
6231 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6232 /* Don't wedge the queue */
6233 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6237 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6239 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6240 /* Inform the XPT that a new device has been found */
6241 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6242 xpt_action(done_ccb);
6244 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6247 xpt_release_ccb(done_ccb);
6250 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6251 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6252 done_ccb->ccb_h.status = CAM_REQ_CMP;
6254 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6255 cam_periph_invalidate(periph);
6256 cam_periph_release(periph);
6258 probeschedule(periph);
6263 probecleanup(struct cam_periph *periph)
6265 free(periph->softc, M_TEMP);
6269 xpt_find_quirk(struct cam_ed *device)
6273 match = cam_quirkmatch((caddr_t)&device->inq_data,
6274 (caddr_t)xpt_quirk_table,
6275 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6276 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6279 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6281 device->quirk = (struct xpt_quirk_entry *)match;
6285 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
6290 error = sysctl_handle_int(oidp, &bool, sizeof(bool), req);
6291 if (error != 0 || req->newptr == NULL)
6293 if (bool == 0 || bool == 1) {
6301 #ifdef CAM_NEW_TRAN_CODE
6304 xpt_devise_transport(struct cam_path *path)
6306 struct ccb_pathinq cpi;
6307 struct ccb_trans_settings cts;
6308 struct scsi_inquiry_data *inq_buf;
6310 /* Get transport information from the SIM */
6311 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6312 cpi.ccb_h.func_code = XPT_PATH_INQ;
6313 xpt_action((union ccb *)&cpi);
6316 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6317 inq_buf = &path->device->inq_data;
6318 path->device->protocol = PROTO_SCSI;
6319 path->device->protocol_version =
6320 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6321 path->device->transport = cpi.transport;
6322 path->device->transport_version = cpi.transport_version;
6325 * Any device not using SPI3 features should
6326 * be considered SPI2 or lower.
6328 if (inq_buf != NULL) {
6329 if (path->device->transport == XPORT_SPI
6330 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6331 && path->device->transport_version > 2)
6332 path->device->transport_version = 2;
6334 struct cam_ed* otherdev;
6336 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6338 otherdev = TAILQ_NEXT(otherdev, links)) {
6339 if (otherdev != path->device)
6343 if (otherdev != NULL) {
6345 * Initially assume the same versioning as
6346 * prior luns for this target.
6348 path->device->protocol_version =
6349 otherdev->protocol_version;
6350 path->device->transport_version =
6351 otherdev->transport_version;
6353 /* Until we know better, opt for safty */
6354 path->device->protocol_version = 2;
6355 if (path->device->transport == XPORT_SPI)
6356 path->device->transport_version = 2;
6358 path->device->transport_version = 0;
6364 * For a device compliant with SPC-2 we should be able
6365 * to determine the transport version supported by
6366 * scrutinizing the version descriptors in the
6370 /* Tell the controller what we think */
6371 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6372 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6373 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6374 cts.transport = path->device->transport;
6375 cts.transport_version = path->device->transport_version;
6376 cts.protocol = path->device->protocol;
6377 cts.protocol_version = path->device->protocol_version;
6378 cts.proto_specific.valid = 0;
6379 cts.xport_specific.valid = 0;
6380 xpt_action((union ccb *)&cts);
6384 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6387 struct ccb_pathinq cpi;
6388 struct ccb_trans_settings cur_cts;
6389 struct ccb_trans_settings_scsi *scsi;
6390 struct ccb_trans_settings_scsi *cur_scsi;
6391 struct cam_sim *sim;
6392 struct scsi_inquiry_data *inq_data;
6394 if (device == NULL) {
6395 cts->ccb_h.status = CAM_PATH_INVALID;
6396 xpt_done((union ccb *)cts);
6400 if (cts->protocol == PROTO_UNKNOWN
6401 || cts->protocol == PROTO_UNSPECIFIED) {
6402 cts->protocol = device->protocol;
6403 cts->protocol_version = device->protocol_version;
6406 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6407 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6408 cts->protocol_version = device->protocol_version;
6410 if (cts->protocol != device->protocol) {
6411 xpt_print_path(cts->ccb_h.path);
6412 printf("Uninitialized Protocol %x:%x?\n",
6413 cts->protocol, device->protocol);
6414 cts->protocol = device->protocol;
6417 if (cts->protocol_version > device->protocol_version) {
6419 xpt_print_path(cts->ccb_h.path);
6420 printf("Down reving Protocol Version from %d to %d?\n",
6421 cts->protocol_version, device->protocol_version);
6423 cts->protocol_version = device->protocol_version;
6426 if (cts->transport == XPORT_UNKNOWN
6427 || cts->transport == XPORT_UNSPECIFIED) {
6428 cts->transport = device->transport;
6429 cts->transport_version = device->transport_version;
6432 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6433 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6434 cts->transport_version = device->transport_version;
6436 if (cts->transport != device->transport) {
6437 xpt_print_path(cts->ccb_h.path);
6438 printf("Uninitialized Transport %x:%x?\n",
6439 cts->transport, device->transport);
6440 cts->transport = device->transport;
6443 if (cts->transport_version > device->transport_version) {
6445 xpt_print_path(cts->ccb_h.path);
6446 printf("Down reving Transport Version from %d to %d?\n",
6447 cts->transport_version,
6448 device->transport_version);
6450 cts->transport_version = device->transport_version;
6453 sim = cts->ccb_h.path->bus->sim;
6456 * Nothing more of interest to do unless
6457 * this is a device connected via the
6460 if (cts->protocol != PROTO_SCSI) {
6461 if (async_update == FALSE)
6462 (*(sim->sim_action))(sim, (union ccb *)cts);
6466 inq_data = &device->inq_data;
6467 scsi = &cts->proto_specific.scsi;
6468 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6469 cpi.ccb_h.func_code = XPT_PATH_INQ;
6470 xpt_action((union ccb *)&cpi);
6472 /* SCSI specific sanity checking */
6473 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6474 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
6475 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6476 || (device->quirk->mintags == 0)) {
6478 * Can't tag on hardware that doesn't support tags,
6479 * doesn't have it enabled, or has broken tag support.
6481 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6484 if (async_update == FALSE) {
6486 * Perform sanity checking against what the
6487 * controller and device can do.
6489 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6490 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6491 cur_cts.type = cts->type;
6492 xpt_action((union ccb *)&cur_cts);
6494 cur_scsi = &cur_cts.proto_specific.scsi;
6495 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6496 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6497 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6499 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6500 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6503 /* SPI specific sanity checking */
6504 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6506 struct ccb_trans_settings_spi *spi;
6507 struct ccb_trans_settings_spi *cur_spi;
6509 spi = &cts->xport_specific.spi;
6511 cur_spi = &cur_cts.xport_specific.spi;
6513 /* Fill in any gaps in what the user gave us */
6514 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6515 spi->sync_period = cur_spi->sync_period;
6516 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6517 spi->sync_period = 0;
6518 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6519 spi->sync_offset = cur_spi->sync_offset;
6520 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6521 spi->sync_offset = 0;
6522 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6523 spi->ppr_options = cur_spi->ppr_options;
6524 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6525 spi->ppr_options = 0;
6526 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6527 spi->bus_width = cur_spi->bus_width;
6528 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6530 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6531 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6532 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6534 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6535 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6536 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6537 && (inq_data->flags & SID_Sync) == 0
6538 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6539 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6540 || (cur_spi->sync_offset == 0)
6541 || (cur_spi->sync_period == 0)) {
6543 spi->sync_period = 0;
6544 spi->sync_offset = 0;
6547 switch (spi->bus_width) {
6548 case MSG_EXT_WDTR_BUS_32_BIT:
6549 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6550 || (inq_data->flags & SID_WBus32) != 0
6551 || cts->type == CTS_TYPE_USER_SETTINGS)
6552 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6554 /* Fall Through to 16-bit */
6555 case MSG_EXT_WDTR_BUS_16_BIT:
6556 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6557 || (inq_data->flags & SID_WBus16) != 0
6558 || cts->type == CTS_TYPE_USER_SETTINGS)
6559 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6560 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6563 /* Fall Through to 8-bit */
6564 default: /* New bus width?? */
6565 case MSG_EXT_WDTR_BUS_8_BIT:
6566 /* All targets can do this */
6567 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6571 spi3caps = cpi.xport_specific.spi.ppr_options;
6572 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6573 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6574 spi3caps &= inq_data->spi3data;
6576 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6577 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6579 if ((spi3caps & SID_SPI_IUS) == 0)
6580 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6582 if ((spi3caps & SID_SPI_QAS) == 0)
6583 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6585 /* No SPI Transfer settings are allowed unless we are wide */
6586 if (spi->bus_width == 0)
6587 spi->ppr_options = 0;
6589 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6591 * Can't tag queue without disconnection.
6593 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6594 scsi->valid |= CTS_SCSI_VALID_TQ;
6598 * If we are currently performing tagged transactions to
6599 * this device and want to change its negotiation parameters,
6600 * go non-tagged for a bit to give the controller a chance to
6601 * negotiate unhampered by tag messages.
6603 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6604 && (device->inq_flags & SID_CmdQue) != 0
6605 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6606 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6607 CTS_SPI_VALID_SYNC_OFFSET|
6608 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6609 xpt_toggle_tags(cts->ccb_h.path);
6612 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6613 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6617 * If we are transitioning from tags to no-tags or
6618 * vice-versa, we need to carefully freeze and restart
6619 * the queue so that we don't overlap tagged and non-tagged
6620 * commands. We also temporarily stop tags if there is
6621 * a change in transfer negotiation settings to allow
6622 * "tag-less" negotiation.
6624 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6625 || (device->inq_flags & SID_CmdQue) != 0)
6626 device_tagenb = TRUE;
6628 device_tagenb = FALSE;
6630 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6631 && device_tagenb == FALSE)
6632 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6633 && device_tagenb == TRUE)) {
6635 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6637 * Delay change to use tags until after a
6638 * few commands have gone to this device so
6639 * the controller has time to perform transfer
6640 * negotiations without tagged messages getting
6643 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6644 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6646 struct ccb_relsim crs;
6648 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6649 device->inq_flags &= ~SID_CmdQue;
6650 xpt_dev_ccbq_resize(cts->ccb_h.path,
6651 sim->max_dev_openings);
6652 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6653 device->tag_delay_count = 0;
6655 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6657 crs.ccb_h.func_code = XPT_REL_SIMQ;
6658 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6660 = crs.release_timeout
6663 xpt_action((union ccb *)&crs);
6667 if (async_update == FALSE)
6668 (*(sim->sim_action))(sim, (union ccb *)cts);
6671 #else /* CAM_NEW_TRAN_CODE */
6674 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6677 struct cam_sim *sim;
6680 sim = cts->ccb_h.path->bus->sim;
6681 if (async_update == FALSE) {
6682 struct scsi_inquiry_data *inq_data;
6683 struct ccb_pathinq cpi;
6684 struct ccb_trans_settings cur_cts;
6686 if (device == NULL) {
6687 cts->ccb_h.status = CAM_PATH_INVALID;
6688 xpt_done((union ccb *)cts);
6693 * Perform sanity checking against what the
6694 * controller and device can do.
6696 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6697 cpi.ccb_h.func_code = XPT_PATH_INQ;
6698 xpt_action((union ccb *)&cpi);
6699 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6700 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6701 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6702 xpt_action((union ccb *)&cur_cts);
6703 inq_data = &device->inq_data;
6705 /* Fill in any gaps in what the user gave us */
6706 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6707 cts->sync_period = cur_cts.sync_period;
6708 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6709 cts->sync_offset = cur_cts.sync_offset;
6710 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6711 cts->bus_width = cur_cts.bus_width;
6712 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6713 cts->flags &= ~CCB_TRANS_DISC_ENB;
6714 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6716 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6717 cts->flags &= ~CCB_TRANS_TAG_ENB;
6718 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6721 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6722 && (inq_data->flags & SID_Sync) == 0)
6723 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6724 || (cts->sync_offset == 0)
6725 || (cts->sync_period == 0)) {
6727 cts->sync_period = 0;
6728 cts->sync_offset = 0;
6729 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6730 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6731 && cts->sync_period <= 0x9) {
6733 * Don't allow DT transmission rates if the
6734 * device does not support it.
6736 cts->sync_period = 0xa;
6739 switch (cts->bus_width) {
6740 case MSG_EXT_WDTR_BUS_32_BIT:
6741 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6742 || (inq_data->flags & SID_WBus32) != 0)
6743 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6745 /* FALLTHROUGH to 16-bit */
6746 case MSG_EXT_WDTR_BUS_16_BIT:
6747 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6748 || (inq_data->flags & SID_WBus16) != 0)
6749 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6750 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6753 /* FALLTHROUGH to 8-bit */
6754 default: /* New bus width?? */
6755 case MSG_EXT_WDTR_BUS_8_BIT:
6756 /* All targets can do this */
6757 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6761 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6763 * Can't tag queue without disconnection.
6765 cts->flags &= ~CCB_TRANS_TAG_ENB;
6766 cts->valid |= CCB_TRANS_TQ_VALID;
6769 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6770 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
6771 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6772 || (device->quirk->mintags == 0)) {
6774 * Can't tag on hardware that doesn't support,
6775 * doesn't have it enabled, or has broken tag support.
6777 cts->flags &= ~CCB_TRANS_TAG_ENB;
6782 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6786 * If we are transitioning from tags to no-tags or
6787 * vice-versa, we need to carefully freeze and restart
6788 * the queue so that we don't overlap tagged and non-tagged
6789 * commands. We also temporarily stop tags if there is
6790 * a change in transfer negotiation settings to allow
6791 * "tag-less" negotiation.
6793 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6794 || (device->inq_flags & SID_CmdQue) != 0)
6795 device_tagenb = TRUE;
6797 device_tagenb = FALSE;
6799 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6800 && device_tagenb == FALSE)
6801 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6802 && device_tagenb == TRUE)) {
6804 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6806 * Delay change to use tags until after a
6807 * few commands have gone to this device so
6808 * the controller has time to perform transfer
6809 * negotiations without tagged messages getting
6812 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6813 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6815 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6817 device->inq_flags &= ~SID_CmdQue;
6818 xpt_dev_ccbq_resize(cts->ccb_h.path,
6819 sim->max_dev_openings);
6820 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6821 device->tag_delay_count = 0;
6826 if (async_update == FALSE) {
6828 * If we are currently performing tagged transactions to
6829 * this device and want to change its negotiation parameters,
6830 * go non-tagged for a bit to give the controller a chance to
6831 * negotiate unhampered by tag messages.
6833 if ((device->inq_flags & SID_CmdQue) != 0
6834 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6835 CCB_TRANS_SYNC_OFFSET_VALID|
6836 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6837 xpt_toggle_tags(cts->ccb_h.path);
6839 (*(sim->sim_action))(sim, (union ccb *)cts);
6843 struct ccb_relsim crs;
6845 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6847 crs.ccb_h.func_code = XPT_REL_SIMQ;
6848 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6850 = crs.release_timeout
6853 xpt_action((union ccb *)&crs);
6858 #endif /* CAM_NEW_TRAN_CODE */
6861 xpt_toggle_tags(struct cam_path *path)
6866 * Give controllers a chance to renegotiate
6867 * before starting tag operations. We
6868 * "toggle" tagged queuing off then on
6869 * which causes the tag enable command delay
6870 * counter to come into effect.
6873 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6874 || ((dev->inq_flags & SID_CmdQue) != 0
6875 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6876 struct ccb_trans_settings cts;
6878 xpt_setup_ccb(&cts.ccb_h, path, 1);
6879 #ifdef CAM_NEW_TRAN_CODE
6880 cts.protocol = PROTO_SCSI;
6881 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6882 cts.transport = XPORT_UNSPECIFIED;
6883 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6884 cts.proto_specific.scsi.flags = 0;
6885 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6886 #else /* CAM_NEW_TRAN_CODE */
6888 cts.valid = CCB_TRANS_TQ_VALID;
6889 #endif /* CAM_NEW_TRAN_CODE */
6890 xpt_set_transfer_settings(&cts, path->device,
6891 /*async_update*/TRUE);
6892 #ifdef CAM_NEW_TRAN_CODE
6893 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6894 #else /* CAM_NEW_TRAN_CODE */
6895 cts.flags = CCB_TRANS_TAG_ENB;
6896 #endif /* CAM_NEW_TRAN_CODE */
6897 xpt_set_transfer_settings(&cts, path->device,
6898 /*async_update*/TRUE);
6903 xpt_start_tags(struct cam_path *path)
6905 struct ccb_relsim crs;
6906 struct cam_ed *device;
6907 struct cam_sim *sim;
6910 device = path->device;
6911 sim = path->bus->sim;
6912 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6913 xpt_freeze_devq(path, /*count*/1);
6914 device->inq_flags |= SID_CmdQue;
6915 if (device->tag_saved_openings != 0)
6916 newopenings = device->tag_saved_openings;
6918 newopenings = min(device->quirk->maxtags,
6919 sim->max_tagged_dev_openings);
6920 xpt_dev_ccbq_resize(path, newopenings);
6921 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6922 crs.ccb_h.func_code = XPT_REL_SIMQ;
6923 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6925 = crs.release_timeout
6928 xpt_action((union ccb *)&crs);
6931 static int busses_to_config;
6932 static int busses_to_reset;
6935 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6937 if (bus->path_id != CAM_XPT_PATH_ID) {
6938 struct cam_path path;
6939 struct ccb_pathinq cpi;
6943 xpt_compile_path(&path, NULL, bus->path_id,
6944 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6945 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6946 cpi.ccb_h.func_code = XPT_PATH_INQ;
6947 xpt_action((union ccb *)&cpi);
6948 can_negotiate = cpi.hba_inquiry;
6949 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6950 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6953 xpt_release_path(&path);
6960 xptconfigfunc(struct cam_eb *bus, void *arg)
6962 struct cam_path *path;
6963 union ccb *work_ccb;
6965 if (bus->path_id != CAM_XPT_PATH_ID) {
6969 work_ccb = xpt_alloc_ccb();
6970 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6971 CAM_TARGET_WILDCARD,
6972 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6973 printf("xptconfigfunc: xpt_create_path failed with "
6974 "status %#x for bus %d\n", status, bus->path_id);
6975 printf("xptconfigfunc: halting bus configuration\n");
6976 xpt_free_ccb(work_ccb);
6978 xpt_finishconfig(xpt_periph, NULL);
6981 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6982 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6983 xpt_action(work_ccb);
6984 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6985 printf("xptconfigfunc: CPI failed on bus %d "
6986 "with status %d\n", bus->path_id,
6987 work_ccb->ccb_h.status);
6988 xpt_finishconfig(xpt_periph, work_ccb);
6992 can_negotiate = work_ccb->cpi.hba_inquiry;
6993 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6994 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6995 && (can_negotiate != 0)) {
6996 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6997 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6998 work_ccb->ccb_h.cbfcnp = NULL;
6999 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
7000 ("Resetting Bus\n"));
7001 xpt_action(work_ccb);
7002 xpt_finishconfig(xpt_periph, work_ccb);
7004 /* Act as though we performed a successful BUS RESET */
7005 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
7006 xpt_finishconfig(xpt_periph, work_ccb);
7014 xpt_config(void *arg)
7017 * Now that interrupts are enabled, go find our devices
7021 /* Setup debugging flags and path */
7022 #ifdef CAM_DEBUG_FLAGS
7023 cam_dflags = CAM_DEBUG_FLAGS;
7024 #else /* !CAM_DEBUG_FLAGS */
7025 cam_dflags = CAM_DEBUG_NONE;
7026 #endif /* CAM_DEBUG_FLAGS */
7027 #ifdef CAM_DEBUG_BUS
7028 if (cam_dflags != CAM_DEBUG_NONE) {
7029 if (xpt_create_path(&cam_dpath, xpt_periph,
7030 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
7031 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
7032 printf("xpt_config: xpt_create_path() failed for debug"
7033 " target %d:%d:%d, debugging disabled\n",
7034 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
7035 cam_dflags = CAM_DEBUG_NONE;
7039 #else /* !CAM_DEBUG_BUS */
7041 #endif /* CAM_DEBUG_BUS */
7042 #endif /* CAMDEBUG */
7045 * Scan all installed busses.
7047 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
7049 if (busses_to_config == 0) {
7050 /* Call manually because we don't have any busses */
7051 xpt_finishconfig(xpt_periph, NULL);
7053 if (busses_to_reset > 0 && scsi_delay >= 2000) {
7054 printf("Waiting %d seconds for SCSI "
7055 "devices to settle\n", scsi_delay/1000);
7057 xpt_for_all_busses(xptconfigfunc, NULL);
7062 * If the given device only has one peripheral attached to it, and if that
7063 * peripheral is the passthrough driver, announce it. This insures that the
7064 * user sees some sort of announcement for every peripheral in their system.
7067 xptpassannouncefunc(struct cam_ed *device, void *arg)
7069 struct cam_periph *periph;
7072 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
7073 periph = SLIST_NEXT(periph, periph_links), i++);
7075 periph = SLIST_FIRST(&device->periphs);
7077 && (strncmp(periph->periph_name, "pass", 4) == 0))
7078 xpt_announce_periph(periph, NULL);
7084 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
7086 struct periph_driver **p_drv;
7089 if (done_ccb != NULL) {
7090 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
7091 ("xpt_finishconfig\n"));
7092 switch(done_ccb->ccb_h.func_code) {
7094 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
7095 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
7096 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
7097 done_ccb->crcn.flags = 0;
7098 xpt_action(done_ccb);
7104 xpt_free_path(done_ccb->ccb_h.path);
7110 if (busses_to_config == 0) {
7111 /* Register all the peripheral drivers */
7112 /* XXX This will have to change when we have loadable modules */
7113 p_drv = periph_drivers;
7114 for (i = 0; p_drv[i] != NULL; i++) {
7115 (*p_drv[i]->init)();
7119 * Check for devices with no "standard" peripheral driver
7120 * attached. For any devices like that, announce the
7121 * passthrough driver so the user will see something.
7123 xpt_for_all_devices(xptpassannouncefunc, NULL);
7125 /* Release our hook so that the boot can continue. */
7126 config_intrhook_disestablish(xpt_config_hook);
7127 free(xpt_config_hook, M_TEMP);
7128 xpt_config_hook = NULL;
7130 if (done_ccb != NULL)
7131 xpt_free_ccb(done_ccb);
7135 xptaction(struct cam_sim *sim, union ccb *work_ccb)
7137 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
7139 switch (work_ccb->ccb_h.func_code) {
7140 /* Common cases first */
7141 case XPT_PATH_INQ: /* Path routing inquiry */
7143 struct ccb_pathinq *cpi;
7145 cpi = &work_ccb->cpi;
7146 cpi->version_num = 1; /* XXX??? */
7147 cpi->hba_inquiry = 0;
7148 cpi->target_sprt = 0;
7150 cpi->hba_eng_cnt = 0;
7151 cpi->max_target = 0;
7153 cpi->initiator_id = 0;
7154 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
7155 strncpy(cpi->hba_vid, "", HBA_IDLEN);
7156 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
7157 cpi->unit_number = sim->unit_number;
7158 cpi->bus_id = sim->bus_id;
7159 cpi->base_transfer_speed = 0;
7160 #ifdef CAM_NEW_TRAN_CODE
7161 cpi->protocol = PROTO_UNSPECIFIED;
7162 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
7163 cpi->transport = XPORT_UNSPECIFIED;
7164 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
7165 #endif /* CAM_NEW_TRAN_CODE */
7166 cpi->ccb_h.status = CAM_REQ_CMP;
7171 work_ccb->ccb_h.status = CAM_REQ_INVALID;
7178 * The xpt as a "controller" has no interrupt sources, so polling
7182 xptpoll(struct cam_sim *sim)
7187 camisr(void *V_queue)
7189 cam_isrq_t *oqueue = V_queue;
7192 struct ccb_hdr *ccb_h;
7195 * Transfer the ccb_bioq list to a temporary list so we can operate
7196 * on it without needing to lock/unlock on every loop. The concat
7197 * function with re-init the real list for us.
7200 mtx_lock(&cam_bioq_lock);
7202 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7203 mtx_unlock(&cam_bioq_lock);
7205 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7208 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7209 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7212 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7217 if (ccb_h->flags & CAM_HIGH_POWER) {
7218 struct highpowerlist *hphead;
7219 union ccb *send_ccb;
7221 hphead = &highpowerq;
7223 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7226 * Increment the count since this command is done.
7231 * Any high powered commands queued up?
7233 if (send_ccb != NULL) {
7235 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7237 xpt_release_devq(send_ccb->ccb_h.path,
7238 /*count*/1, /*runqueue*/TRUE);
7241 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7244 dev = ccb_h->path->device;
7247 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7249 if (!SIM_DEAD(ccb_h->path->bus->sim)) {
7250 ccb_h->path->bus->sim->devq->send_active--;
7251 ccb_h->path->bus->sim->devq->send_openings++;
7255 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7256 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7257 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7258 && (dev->ccbq.dev_active == 0))) {
7260 xpt_release_devq(ccb_h->path, /*count*/1,
7264 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7265 && (--dev->tag_delay_count == 0))
7266 xpt_start_tags(ccb_h->path);
7268 if ((dev->ccbq.queue.entries > 0)
7269 && (dev->qfrozen_cnt == 0)
7270 && (device_is_send_queued(dev) == 0)) {
7271 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7276 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7277 xpt_release_simq(ccb_h->path->bus->sim,
7279 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7283 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7284 && (ccb_h->status & CAM_DEV_QFRZN)) {
7285 xpt_release_devq(ccb_h->path, /*count*/1,
7287 ccb_h->status &= ~CAM_DEV_QFRZN;
7289 xpt_run_dev_sendq(ccb_h->path->bus);
7292 /* Call the peripheral driver's callback */
7293 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7295 /* Raise IPL for while test */
7302 dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7305 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7310 dead_sim_poll(struct cam_sim *sim)