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>
45 #include <sys/taskqueue.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/kthread.h>
53 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_sim.h>
60 #include <cam/cam_xpt.h>
61 #include <cam/cam_xpt_sim.h>
62 #include <cam/cam_xpt_periph.h>
63 #include <cam/cam_debug.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_pass.h>
68 #include <machine/stdarg.h> /* for xpt_print below */
71 /* Datastructures internal to the xpt layer */
72 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
74 /* Object for defering XPT actions to a taskqueue */
82 * Definition of an async handler callback block. These are used to add
83 * SIMs and peripherals to the async callback lists.
86 SLIST_ENTRY(async_node) links;
87 u_int32_t event_enable; /* Async Event enables */
88 void (*callback)(void *arg, u_int32_t code,
89 struct cam_path *path, void *args);
93 SLIST_HEAD(async_list, async_node);
94 SLIST_HEAD(periph_list, cam_periph);
97 * This is the maximum number of high powered commands (e.g. start unit)
98 * that can be outstanding at a particular time.
100 #ifndef CAM_MAX_HIGHPOWER
101 #define CAM_MAX_HIGHPOWER 4
105 * Structure for queueing a device in a run queue.
106 * There is one run queue for allocating new ccbs,
107 * and another for sending ccbs to the controller.
109 struct cam_ed_qinfo {
111 struct cam_ed *device;
115 * The CAM EDT (Existing Device Table) contains the device information for
116 * all devices for all busses in the system. The table contains a
117 * cam_ed structure for each device on the bus.
120 TAILQ_ENTRY(cam_ed) links;
121 struct cam_ed_qinfo alloc_ccb_entry;
122 struct cam_ed_qinfo send_ccb_entry;
123 struct cam_et *target;
127 * Queue of type drivers wanting to do
128 * work on this device.
130 struct cam_ccbq ccbq; /* Queue of pending ccbs */
131 struct async_list asyncs; /* Async callback info for this B/T/L */
132 struct periph_list periphs; /* All attached devices */
133 u_int generation; /* Generation number */
134 struct cam_periph *owner; /* Peripheral driver's ownership tag */
135 struct xpt_quirk_entry *quirk; /* Oddities about this device */
136 /* Storage for the inquiry data */
138 u_int protocol_version;
140 u_int transport_version;
141 struct scsi_inquiry_data inq_data;
142 u_int8_t inq_flags; /*
143 * Current settings for inquiry flags.
144 * This allows us to override settings
145 * like disconnection and tagged
146 * queuing for a device.
148 u_int8_t queue_flags; /* Queue flags from the control page */
149 u_int8_t serial_num_len;
150 u_int8_t *serial_num;
151 u_int32_t qfrozen_cnt;
153 #define CAM_DEV_UNCONFIGURED 0x01
154 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
155 #define CAM_DEV_REL_ON_COMPLETE 0x04
156 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
157 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
158 #define CAM_DEV_TAG_AFTER_COUNT 0x20
159 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
160 #define CAM_DEV_IN_DV 0x80
161 #define CAM_DEV_DV_HIT_BOTTOM 0x100
162 u_int32_t tag_delay_count;
163 #define CAM_TAG_DELAY_COUNT 5
164 u_int32_t tag_saved_openings;
166 struct callout callout;
170 * Each target is represented by an ET (Existing Target). These
171 * entries are created when a target is successfully probed with an
172 * identify, and removed when a device fails to respond after a number
173 * of retries, or a bus rescan finds the device missing.
176 TAILQ_HEAD(, cam_ed) ed_entries;
177 TAILQ_ENTRY(cam_et) links;
179 target_id_t target_id;
182 struct timeval last_reset;
186 * Each bus is represented by an EB (Existing Bus). These entries
187 * are created by calls to xpt_bus_register and deleted by calls to
188 * xpt_bus_deregister.
191 TAILQ_HEAD(, cam_et) et_entries;
192 TAILQ_ENTRY(cam_eb) links;
195 struct timeval last_reset;
197 #define CAM_EB_RUNQ_SCHEDULED 0x01
203 struct cam_periph *periph;
205 struct cam_et *target;
206 struct cam_ed *device;
209 struct xpt_quirk_entry {
210 struct scsi_inquiry_pattern inq_pat;
212 #define CAM_QUIRK_NOLUNS 0x01
213 #define CAM_QUIRK_NOSERIAL 0x02
214 #define CAM_QUIRK_HILUNS 0x04
215 #define CAM_QUIRK_NOHILUNS 0x08
220 static int cam_srch_hi = 0;
221 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
222 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
223 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
224 sysctl_cam_search_luns, "I",
225 "allow search above LUN 7 for SCSI3 and greater devices");
227 #define CAM_SCSI2_MAXLUN 8
229 * If we're not quirked to search <= the first 8 luns
230 * and we are either quirked to search above lun 8,
231 * or we're > SCSI-2 and we've enabled hilun searching,
232 * or we're > SCSI-2 and the last lun was a success,
233 * we can look for luns above lun 8.
235 #define CAN_SRCH_HI_SPARSE(dv) \
236 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
237 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
238 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
240 #define CAN_SRCH_HI_DENSE(dv) \
241 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
242 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
243 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
251 u_int32_t xpt_generation;
253 /* number of high powered commands that can go through right now */
254 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
257 /* queue for handling async rescan requests. */
258 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
260 /* Registered busses */
261 TAILQ_HEAD(,cam_eb) xpt_busses;
262 u_int bus_generation;
264 struct intr_config_hook *xpt_config_hook;
266 struct mtx xpt_topo_lock;
270 static const char quantum[] = "QUANTUM";
271 static const char sony[] = "SONY";
272 static const char west_digital[] = "WDIGTL";
273 static const char samsung[] = "SAMSUNG";
274 static const char seagate[] = "SEAGATE";
275 static const char microp[] = "MICROP";
277 static struct xpt_quirk_entry xpt_quirk_table[] =
280 /* Reports QUEUE FULL for temporary resource shortages */
281 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
282 /*quirks*/0, /*mintags*/24, /*maxtags*/32
285 /* Reports QUEUE FULL for temporary resource shortages */
286 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
287 /*quirks*/0, /*mintags*/24, /*maxtags*/32
290 /* Reports QUEUE FULL for temporary resource shortages */
291 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
292 /*quirks*/0, /*mintags*/24, /*maxtags*/32
295 /* Broken tagged queuing drive */
296 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
297 /*quirks*/0, /*mintags*/0, /*maxtags*/0
300 /* Broken tagged queuing drive */
301 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
302 /*quirks*/0, /*mintags*/0, /*maxtags*/0
305 /* Broken tagged queuing drive */
306 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
307 /*quirks*/0, /*mintags*/0, /*maxtags*/0
311 * Unfortunately, the Quantum Atlas III has the same
312 * problem as the Atlas II drives above.
313 * Reported by: "Johan Granlund" <johan@granlund.nu>
315 * For future reference, the drive with the problem was:
316 * QUANTUM QM39100TD-SW N1B0
318 * It's possible that Quantum will fix the problem in later
319 * firmware revisions. If that happens, the quirk entry
320 * will need to be made specific to the firmware revisions
324 /* Reports QUEUE FULL for temporary resource shortages */
325 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
326 /*quirks*/0, /*mintags*/24, /*maxtags*/32
330 * 18 Gig Atlas III, same problem as the 9G version.
331 * Reported by: Andre Albsmeier
332 * <andre.albsmeier@mchp.siemens.de>
334 * For future reference, the drive with the problem was:
335 * QUANTUM QM318000TD-S N491
337 /* Reports QUEUE FULL for temporary resource shortages */
338 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
339 /*quirks*/0, /*mintags*/24, /*maxtags*/32
343 * Broken tagged queuing drive
344 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
345 * and: Martin Renters <martin@tdc.on.ca>
347 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
348 /*quirks*/0, /*mintags*/0, /*maxtags*/0
351 * The Seagate Medalist Pro drives have very poor write
352 * performance with anything more than 2 tags.
354 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
355 * Drive: <SEAGATE ST36530N 1444>
357 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
358 * Drive: <SEAGATE ST34520W 1281>
360 * No one has actually reported that the 9G version
361 * (ST39140*) of the Medalist Pro has the same problem, but
362 * we're assuming that it does because the 4G and 6.5G
363 * versions of the drive are broken.
366 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
367 /*quirks*/0, /*mintags*/2, /*maxtags*/2
370 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
371 /*quirks*/0, /*mintags*/2, /*maxtags*/2
374 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
375 /*quirks*/0, /*mintags*/2, /*maxtags*/2
379 * Slow when tagged queueing is enabled. Write performance
380 * steadily drops off with more and more concurrent
381 * transactions. Best sequential write performance with
382 * tagged queueing turned off and write caching turned on.
385 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
386 * Drive: DCAS-34330 w/ "S65A" firmware.
388 * The drive with the problem had the "S65A" firmware
389 * revision, and has also been reported (by Stephen J.
390 * Roznowski <sjr@home.net>) for a drive with the "S61A"
393 * Although no one has reported problems with the 2 gig
394 * version of the DCAS drive, the assumption is that it
395 * has the same problems as the 4 gig version. Therefore
396 * this quirk entries disables tagged queueing for all
399 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
400 /*quirks*/0, /*mintags*/0, /*maxtags*/0
403 /* Broken tagged queuing drive */
404 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
405 /*quirks*/0, /*mintags*/0, /*maxtags*/0
408 /* Broken tagged queuing drive */
409 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
410 /*quirks*/0, /*mintags*/0, /*maxtags*/0
413 /* This does not support other than LUN 0 */
414 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
415 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
419 * Broken tagged queuing drive.
421 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
424 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
425 /*quirks*/0, /*mintags*/0, /*maxtags*/0
429 * Slow when tagged queueing is enabled. (1.5MB/sec versus
431 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
432 * Best performance with these drives is achieved with
433 * tagged queueing turned off, and write caching turned on.
435 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
436 /*quirks*/0, /*mintags*/0, /*maxtags*/0
440 * Slow when tagged queueing is enabled. (1.5MB/sec versus
442 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
443 * Best performance with these drives is achieved with
444 * tagged queueing turned off, and write caching turned on.
446 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
447 /*quirks*/0, /*mintags*/0, /*maxtags*/0
451 * Doesn't handle queue full condition correctly,
452 * so we need to limit maxtags to what the device
453 * can handle instead of determining this automatically.
455 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
456 /*quirks*/0, /*mintags*/2, /*maxtags*/32
459 /* Really only one LUN */
460 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
461 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
464 /* I can't believe we need a quirk for DPT volumes. */
465 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
466 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
467 /*mintags*/0, /*maxtags*/255
471 * Many Sony CDROM drives don't like multi-LUN probing.
473 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
474 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
478 * This drive doesn't like multiple LUN probing.
479 * Submitted by: Parag Patel <parag@cgt.com>
481 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
482 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
485 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
486 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
490 * The 8200 doesn't like multi-lun probing, and probably
491 * don't like serial number requests either.
494 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
497 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
501 * Let's try the same as above, but for a drive that says
502 * it's an IPL-6860 but is actually an EXB 8200.
505 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
508 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
512 * These Hitachi drives don't like multi-lun probing.
513 * The PR submitter has a DK319H, but says that the Linux
514 * kernel has a similar work-around for the DK312 and DK314,
515 * so all DK31* drives are quirked here.
517 * Submitted by: Paul Haddad <paul@pth.com>
519 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
520 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
524 * The Hitachi CJ series with J8A8 firmware apparantly has
525 * problems with tagged commands.
527 * Reported by: amagai@nue.org
529 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
530 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
534 * These are the large storage arrays.
535 * Submitted by: William Carrel <william.carrel@infospace.com>
537 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
538 CAM_QUIRK_HILUNS, 2, 1024
542 * This old revision of the TDC3600 is also SCSI-1, and
543 * hangs upon serial number probing.
546 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
549 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
553 * Maxtor Personal Storage 3000XT (Firewire)
554 * hangs upon serial number probing.
557 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
560 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
564 * Would repond to all LUNs if asked for.
567 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
570 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
574 * Would repond to all LUNs if asked for.
577 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
580 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
583 /* Submitted by: Matthew Dodd <winter@jurai.net> */
584 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
585 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
588 /* Submitted by: Matthew Dodd <winter@jurai.net> */
589 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
590 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
593 /* TeraSolutions special settings for TRC-22 RAID */
594 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
595 /*quirks*/0, /*mintags*/55, /*maxtags*/255
598 /* Veritas Storage Appliance */
599 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
600 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
604 * Would respond to all LUNs. Device type and removable
605 * flag are jumper-selectable.
607 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
610 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
613 /* EasyRAID E5A aka. areca ARC-6010 */
614 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
615 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
618 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
619 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
622 /* Default tagged queuing parameters for all devices */
624 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
625 /*vendor*/"*", /*product*/"*", /*revision*/"*"
627 /*quirks*/0, /*mintags*/2, /*maxtags*/255
631 static const int xpt_quirk_table_size =
632 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
636 DM_RET_FLAG_MASK = 0x0f,
639 DM_RET_DESCEND = 0x20,
641 DM_RET_ACTION_MASK = 0xf0
649 } xpt_traverse_depth;
651 struct xpt_traverse_config {
652 xpt_traverse_depth depth;
657 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
658 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
659 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
660 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
661 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
663 /* Transport layer configuration information */
664 static struct xpt_softc xsoftc;
666 /* Queues for our software interrupt handler */
667 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
668 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
669 static cam_simq_t cam_simq;
670 static struct mtx cam_simq_lock;
672 /* Pointers to software interrupt handlers */
673 static void *cambio_ih;
675 struct cam_periph *xpt_periph;
677 static periph_init_t xpt_periph_init;
679 static periph_init_t probe_periph_init;
681 static struct periph_driver xpt_driver =
683 xpt_periph_init, "xpt",
684 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
687 static struct periph_driver probe_driver =
689 probe_periph_init, "probe",
690 TAILQ_HEAD_INITIALIZER(probe_driver.units)
693 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
694 PERIPHDRIVER_DECLARE(probe, probe_driver);
697 static d_open_t xptopen;
698 static d_close_t xptclose;
699 static d_ioctl_t xptioctl;
701 static struct cdevsw xpt_cdevsw = {
702 .d_version = D_VERSION,
711 static void dead_sim_action(struct cam_sim *sim, union ccb *ccb);
712 static void dead_sim_poll(struct cam_sim *sim);
714 /* Dummy SIM that is used when the real one has gone. */
715 static struct cam_sim cam_dead_sim = {
716 .sim_action = dead_sim_action,
717 .sim_poll = dead_sim_poll,
718 .sim_name = "dead_sim",
721 #define SIM_DEAD(sim) ((sim) == &cam_dead_sim)
724 /* Storage for debugging datastructures */
726 struct cam_path *cam_dpath;
727 u_int32_t cam_dflags;
728 u_int32_t cam_debug_delay;
731 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
732 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
736 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
737 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
738 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
740 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
741 || defined(CAM_DEBUG_LUN)
743 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
744 || !defined(CAM_DEBUG_LUN)
745 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
747 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
748 #else /* !CAMDEBUG */
749 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
750 #endif /* CAMDEBUG */
751 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
753 /* Our boot-time initialization hook */
754 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
756 static moduledata_t cam_moduledata = {
758 cam_module_event_handler,
762 static int xpt_init(void *);
764 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
765 MODULE_VERSION(cam, 1);
768 static cam_status xpt_compile_path(struct cam_path *new_path,
769 struct cam_periph *perph,
771 target_id_t target_id,
774 static void xpt_release_path(struct cam_path *path);
776 static void xpt_async_bcast(struct async_list *async_head,
777 u_int32_t async_code,
778 struct cam_path *path,
780 static void xpt_dev_async(u_int32_t async_code,
782 struct cam_et *target,
783 struct cam_ed *device,
785 static path_id_t xptnextfreepathid(void);
786 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
787 static union ccb *xpt_get_ccb(struct cam_ed *device);
788 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
789 u_int32_t new_priority);
790 static void xpt_run_dev_allocq(struct cam_eb *bus);
791 static void xpt_run_dev_sendq(struct cam_eb *bus);
792 static timeout_t xpt_release_devq_timeout;
793 static void xpt_release_simq_timeout(void *arg) __unused;
794 static void xpt_release_bus(struct cam_eb *bus);
795 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
797 static struct cam_et*
798 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
799 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
800 static struct cam_ed*
801 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
803 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
804 struct cam_ed *device);
805 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
806 static struct cam_eb*
807 xpt_find_bus(path_id_t path_id);
808 static struct cam_et*
809 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
810 static struct cam_ed*
811 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
812 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
813 static void xpt_scan_lun(struct cam_periph *periph,
814 struct cam_path *path, cam_flags flags,
816 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
817 static xpt_busfunc_t xptconfigbuscountfunc;
818 static xpt_busfunc_t xptconfigfunc;
819 static void xpt_config(void *arg);
820 static xpt_devicefunc_t xptpassannouncefunc;
821 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
822 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
823 static void xptpoll(struct cam_sim *sim);
824 static void camisr(void *);
825 static void camisr_runqueue(void *);
826 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
827 u_int num_patterns, struct cam_eb *bus);
828 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
830 struct cam_ed *device);
831 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
833 struct cam_periph *periph);
834 static xpt_busfunc_t xptedtbusfunc;
835 static xpt_targetfunc_t xptedttargetfunc;
836 static xpt_devicefunc_t xptedtdevicefunc;
837 static xpt_periphfunc_t xptedtperiphfunc;
838 static xpt_pdrvfunc_t xptplistpdrvfunc;
839 static xpt_periphfunc_t xptplistperiphfunc;
840 static int xptedtmatch(struct ccb_dev_match *cdm);
841 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
842 static int xptbustraverse(struct cam_eb *start_bus,
843 xpt_busfunc_t *tr_func, void *arg);
844 static int xpttargettraverse(struct cam_eb *bus,
845 struct cam_et *start_target,
846 xpt_targetfunc_t *tr_func, void *arg);
847 static int xptdevicetraverse(struct cam_et *target,
848 struct cam_ed *start_device,
849 xpt_devicefunc_t *tr_func, void *arg);
850 static int xptperiphtraverse(struct cam_ed *device,
851 struct cam_periph *start_periph,
852 xpt_periphfunc_t *tr_func, void *arg);
853 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
854 xpt_pdrvfunc_t *tr_func, void *arg);
855 static int xptpdperiphtraverse(struct periph_driver **pdrv,
856 struct cam_periph *start_periph,
857 xpt_periphfunc_t *tr_func,
859 static xpt_busfunc_t xptdefbusfunc;
860 static xpt_targetfunc_t xptdeftargetfunc;
861 static xpt_devicefunc_t xptdefdevicefunc;
862 static xpt_periphfunc_t xptdefperiphfunc;
863 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
864 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
866 static xpt_devicefunc_t xptsetasyncfunc;
867 static xpt_busfunc_t xptsetasyncbusfunc;
868 static cam_status xptregister(struct cam_periph *periph,
870 static cam_status proberegister(struct cam_periph *periph,
872 static void probeschedule(struct cam_periph *probe_periph);
873 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
874 static void proberequestdefaultnegotiation(struct cam_periph *periph);
875 static int proberequestbackoff(struct cam_periph *periph,
876 struct cam_ed *device);
877 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
878 static void probecleanup(struct cam_periph *periph);
879 static void xpt_find_quirk(struct cam_ed *device);
880 static void xpt_devise_transport(struct cam_path *path);
881 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
882 struct cam_ed *device,
884 static void xpt_toggle_tags(struct cam_path *path);
885 static void xpt_start_tags(struct cam_path *path);
886 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
888 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
890 static __inline int periph_is_queued(struct cam_periph *periph);
891 static __inline int device_is_alloc_queued(struct cam_ed *device);
892 static __inline int device_is_send_queued(struct cam_ed *device);
893 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
896 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
900 if (dev->ccbq.devq_openings > 0) {
901 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
902 cam_ccbq_resize(&dev->ccbq,
903 dev->ccbq.dev_openings
904 + dev->ccbq.dev_active);
905 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
908 * The priority of a device waiting for CCB resources
909 * is that of the the highest priority peripheral driver
912 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
913 &dev->alloc_ccb_entry.pinfo,
914 CAMQ_GET_HEAD(&dev->drvq)->priority);
923 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
927 if (dev->ccbq.dev_openings > 0) {
929 * The priority of a device waiting for controller
930 * resources is that of the the highest priority CCB
934 xpt_schedule_dev(&bus->sim->devq->send_queue,
935 &dev->send_ccb_entry.pinfo,
936 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
944 periph_is_queued(struct cam_periph *periph)
946 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
950 device_is_alloc_queued(struct cam_ed *device)
952 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
956 device_is_send_queued(struct cam_ed *device)
958 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
962 dev_allocq_is_runnable(struct cam_devq *devq)
966 * Have space to do more work.
967 * Allowed to do work.
969 return ((devq->alloc_queue.qfrozen_cnt == 0)
970 && (devq->alloc_queue.entries > 0)
971 && (devq->alloc_openings > 0));
977 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
987 xptdone(struct cam_periph *periph, union ccb *done_ccb)
989 /* Caller will release the CCB */
990 wakeup(&done_ccb->ccb_h.cbfcnp);
994 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
998 * Only allow read-write access.
1000 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
1004 * We don't allow nonblocking access.
1006 if ((flags & O_NONBLOCK) != 0) {
1007 printf("%s: can't do nonblocking access\n", devtoname(dev));
1011 /* Mark ourselves open */
1012 mtx_lock(&xsoftc.xpt_lock);
1013 xsoftc.flags |= XPT_FLAG_OPEN;
1014 mtx_unlock(&xsoftc.xpt_lock);
1020 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1023 /* Mark ourselves closed */
1024 mtx_lock(&xsoftc.xpt_lock);
1025 xsoftc.flags &= ~XPT_FLAG_OPEN;
1026 mtx_unlock(&xsoftc.xpt_lock);
1032 * Don't automatically grab the xpt softc lock here even though this is going
1033 * through the xpt device. The xpt device is really just a back door for
1034 * accessing other devices and SIMs, so the right thing to do is to grab
1035 * the appropriate SIM lock once the bus/SIM is located.
1038 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1046 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1047 * to accept CCB types that don't quite make sense to send through a
1048 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1051 case CAMIOCOMMAND: {
1056 inccb = (union ccb *)addr;
1058 bus = xpt_find_bus(inccb->ccb_h.path_id);
1064 switch(inccb->ccb_h.func_code) {
1067 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1068 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1077 ccb = xpt_alloc_ccb();
1079 CAM_SIM_LOCK(bus->sim);
1082 * Create a path using the bus, target, and lun the
1085 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1086 inccb->ccb_h.path_id,
1087 inccb->ccb_h.target_id,
1088 inccb->ccb_h.target_lun) !=
1091 CAM_SIM_UNLOCK(bus->sim);
1095 /* Ensure all of our fields are correct */
1096 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1097 inccb->ccb_h.pinfo.priority);
1098 xpt_merge_ccb(ccb, inccb);
1099 ccb->ccb_h.cbfcnp = xptdone;
1100 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1101 bcopy(ccb, inccb, sizeof(union ccb));
1102 xpt_free_path(ccb->ccb_h.path);
1104 CAM_SIM_UNLOCK(bus->sim);
1111 * This is an immediate CCB, so it's okay to
1112 * allocate it on the stack.
1115 CAM_SIM_LOCK(bus->sim);
1118 * Create a path using the bus, target, and lun the
1121 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1122 inccb->ccb_h.path_id,
1123 inccb->ccb_h.target_id,
1124 inccb->ccb_h.target_lun) !=
1129 /* Ensure all of our fields are correct */
1130 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1131 inccb->ccb_h.pinfo.priority);
1132 xpt_merge_ccb(&ccb, inccb);
1133 ccb.ccb_h.cbfcnp = xptdone;
1135 CAM_SIM_UNLOCK(bus->sim);
1136 bcopy(&ccb, inccb, sizeof(union ccb));
1137 xpt_free_path(ccb.ccb_h.path);
1141 case XPT_DEV_MATCH: {
1142 struct cam_periph_map_info mapinfo;
1143 struct cam_path *old_path;
1146 * We can't deal with physical addresses for this
1147 * type of transaction.
1149 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1155 * Save this in case the caller had it set to
1156 * something in particular.
1158 old_path = inccb->ccb_h.path;
1161 * We really don't need a path for the matching
1162 * code. The path is needed because of the
1163 * debugging statements in xpt_action(). They
1164 * assume that the CCB has a valid path.
1166 inccb->ccb_h.path = xpt_periph->path;
1168 bzero(&mapinfo, sizeof(mapinfo));
1171 * Map the pattern and match buffers into kernel
1172 * virtual address space.
1174 error = cam_periph_mapmem(inccb, &mapinfo);
1177 inccb->ccb_h.path = old_path;
1182 * This is an immediate CCB, we can send it on directly.
1187 * Map the buffers back into user space.
1189 cam_periph_unmapmem(inccb, &mapinfo);
1191 inccb->ccb_h.path = old_path;
1203 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1204 * with the periphal driver name and unit name filled in. The other
1205 * fields don't really matter as input. The passthrough driver name
1206 * ("pass"), and unit number are passed back in the ccb. The current
1207 * device generation number, and the index into the device peripheral
1208 * driver list, and the status are also passed back. Note that
1209 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1210 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1211 * (or rather should be) impossible for the device peripheral driver
1212 * list to change since we look at the whole thing in one pass, and
1213 * we do it with splcam protection.
1216 case CAMGETPASSTHRU: {
1218 struct cam_periph *periph;
1219 struct periph_driver **p_drv;
1222 u_int cur_generation;
1223 int base_periph_found;
1227 ccb = (union ccb *)addr;
1228 unit = ccb->cgdl.unit_number;
1229 name = ccb->cgdl.periph_name;
1231 * Every 100 devices, we want to drop our spl protection to
1232 * give the software interrupt handler a chance to run.
1233 * Most systems won't run into this check, but this should
1234 * avoid starvation in the software interrupt handler in
1239 ccb = (union ccb *)addr;
1241 base_periph_found = 0;
1244 * Sanity check -- make sure we don't get a null peripheral
1247 if (*ccb->cgdl.periph_name == '\0') {
1252 /* Keep the list from changing while we traverse it */
1255 cur_generation = xsoftc.xpt_generation;
1257 /* first find our driver in the list of drivers */
1258 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1259 if (strcmp((*p_drv)->driver_name, name) == 0)
1262 if (*p_drv == NULL) {
1264 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1265 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1266 *ccb->cgdl.periph_name = '\0';
1267 ccb->cgdl.unit_number = 0;
1273 * Run through every peripheral instance of this driver
1274 * and check to see whether it matches the unit passed
1275 * in by the user. If it does, get out of the loops and
1276 * find the passthrough driver associated with that
1277 * peripheral driver.
1279 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1280 periph = TAILQ_NEXT(periph, unit_links)) {
1282 if (periph->unit_number == unit) {
1284 } else if (--splbreaknum == 0) {
1288 if (cur_generation != xsoftc.xpt_generation)
1293 * If we found the peripheral driver that the user passed
1294 * in, go through all of the peripheral drivers for that
1295 * particular device and look for a passthrough driver.
1297 if (periph != NULL) {
1298 struct cam_ed *device;
1301 base_periph_found = 1;
1302 device = periph->path->device;
1303 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1305 periph = SLIST_NEXT(periph, periph_links), i++) {
1307 * Check to see whether we have a
1308 * passthrough device or not.
1310 if (strcmp(periph->periph_name, "pass") == 0) {
1312 * Fill in the getdevlist fields.
1314 strcpy(ccb->cgdl.periph_name,
1315 periph->periph_name);
1316 ccb->cgdl.unit_number =
1317 periph->unit_number;
1318 if (SLIST_NEXT(periph, periph_links))
1320 CAM_GDEVLIST_MORE_DEVS;
1323 CAM_GDEVLIST_LAST_DEVICE;
1324 ccb->cgdl.generation =
1326 ccb->cgdl.index = i;
1328 * Fill in some CCB header fields
1329 * that the user may want.
1331 ccb->ccb_h.path_id =
1332 periph->path->bus->path_id;
1333 ccb->ccb_h.target_id =
1334 periph->path->target->target_id;
1335 ccb->ccb_h.target_lun =
1336 periph->path->device->lun_id;
1337 ccb->ccb_h.status = CAM_REQ_CMP;
1344 * If the periph is null here, one of two things has
1345 * happened. The first possibility is that we couldn't
1346 * find the unit number of the particular peripheral driver
1347 * that the user is asking about. e.g. the user asks for
1348 * the passthrough driver for "da11". We find the list of
1349 * "da" peripherals all right, but there is no unit 11.
1350 * The other possibility is that we went through the list
1351 * of peripheral drivers attached to the device structure,
1352 * but didn't find one with the name "pass". Either way,
1353 * we return ENOENT, since we couldn't find something.
1355 if (periph == NULL) {
1356 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1357 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1358 *ccb->cgdl.periph_name = '\0';
1359 ccb->cgdl.unit_number = 0;
1362 * It is unfortunate that this is even necessary,
1363 * but there are many, many clueless users out there.
1364 * If this is true, the user is looking for the
1365 * passthrough driver, but doesn't have one in his
1368 if (base_periph_found == 1) {
1369 printf("xptioctl: pass driver is not in the "
1371 printf("xptioctl: put \"device pass0\" in "
1372 "your kernel config file\n");
1387 cam_module_event_handler(module_t mod, int what, void *arg)
1393 if ((error = xpt_init(NULL)) != 0)
1405 /* thread to handle bus rescans */
1407 xpt_scanner_thread(void *dummy)
1411 struct cam_sim *sim;
1415 * Wait for a rescan request to come in. When it does, splice
1416 * it onto a queue from local storage so that the xpt lock
1417 * doesn't need to be held while the requests are being
1421 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
1424 TAILQ_CONCAT(&queue, &xsoftc.ccb_scanq, sim_links.tqe);
1427 while ((ccb = (union ccb *)TAILQ_FIRST(&queue)) != NULL) {
1428 TAILQ_REMOVE(&queue, &ccb->ccb_h, sim_links.tqe);
1430 sim = ccb->ccb_h.path->bus->sim;
1433 ccb->ccb_h.func_code = XPT_SCAN_BUS;
1434 ccb->ccb_h.cbfcnp = xptdone;
1435 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5);
1436 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1437 xpt_free_path(ccb->ccb_h.path);
1439 CAM_SIM_UNLOCK(sim);
1445 xpt_rescan(union ccb *ccb)
1447 struct ccb_hdr *hdr;
1450 * Don't make duplicate entries for the same paths.
1453 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
1454 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
1456 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
1457 xpt_free_path(ccb->ccb_h.path);
1462 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
1463 wakeup(&xsoftc.ccb_scanq);
1467 /* Functions accessed by the peripheral drivers */
1469 xpt_init(void *dummy)
1471 struct cam_sim *xpt_sim;
1472 struct cam_path *path;
1473 struct cam_devq *devq;
1476 TAILQ_INIT(&xsoftc.xpt_busses);
1477 TAILQ_INIT(&cam_simq);
1478 TAILQ_INIT(&xsoftc.ccb_scanq);
1479 STAILQ_INIT(&xsoftc.highpowerq);
1480 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
1482 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
1483 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
1484 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
1487 * The xpt layer is, itself, the equivelent of a SIM.
1488 * Allow 16 ccbs in the ccb pool for it. This should
1489 * give decent parallelism when we probe busses and
1490 * perform other XPT functions.
1492 devq = cam_simq_alloc(16);
1493 xpt_sim = cam_sim_alloc(xptaction,
1498 /*mtx*/&xsoftc.xpt_lock,
1499 /*max_dev_transactions*/0,
1500 /*max_tagged_dev_transactions*/0,
1502 if (xpt_sim == NULL)
1505 xpt_sim->max_ccbs = 16;
1507 mtx_lock(&xsoftc.xpt_lock);
1508 if ((status = xpt_bus_register(xpt_sim, /*bus #*/0)) != CAM_SUCCESS) {
1509 printf("xpt_init: xpt_bus_register failed with status %#x,"
1510 " failing attach\n", status);
1515 * Looking at the XPT from the SIM layer, the XPT is
1516 * the equivelent of a peripheral driver. Allocate
1517 * a peripheral driver entry for us.
1519 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1520 CAM_TARGET_WILDCARD,
1521 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1522 printf("xpt_init: xpt_create_path failed with status %#x,"
1523 " failing attach\n", status);
1527 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1528 path, NULL, 0, xpt_sim);
1529 xpt_free_path(path);
1530 mtx_unlock(&xsoftc.xpt_lock);
1533 * Register a callback for when interrupts are enabled.
1535 xsoftc.xpt_config_hook =
1536 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1537 M_TEMP, M_NOWAIT | M_ZERO);
1538 if (xsoftc.xpt_config_hook == NULL) {
1539 printf("xpt_init: Cannot malloc config hook "
1540 "- failing attach\n");
1544 xsoftc.xpt_config_hook->ich_func = xpt_config;
1545 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
1546 free (xsoftc.xpt_config_hook, M_TEMP);
1547 printf("xpt_init: config_intrhook_establish failed "
1548 "- failing attach\n");
1551 /* fire up rescan thread */
1552 if (kthread_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
1553 printf("xpt_init: failed to create rescan thread\n");
1555 /* Install our software interrupt handlers */
1556 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
1562 xptregister(struct cam_periph *periph, void *arg)
1564 struct cam_sim *xpt_sim;
1566 if (periph == NULL) {
1567 printf("xptregister: periph was NULL!!\n");
1568 return(CAM_REQ_CMP_ERR);
1571 xpt_sim = (struct cam_sim *)arg;
1572 xpt_sim->softc = periph;
1573 xpt_periph = periph;
1574 periph->softc = NULL;
1576 return(CAM_REQ_CMP);
1580 xpt_add_periph(struct cam_periph *periph)
1582 struct cam_ed *device;
1584 struct periph_list *periph_head;
1586 mtx_assert(periph->sim->mtx, MA_OWNED);
1588 device = periph->path->device;
1590 periph_head = &device->periphs;
1592 status = CAM_REQ_CMP;
1594 if (device != NULL) {
1598 * Make room for this peripheral
1599 * so it will fit in the queue
1600 * when it's scheduled to run
1603 status = camq_resize(&device->drvq,
1604 device->drvq.array_size + 1);
1606 device->generation++;
1608 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1613 atomic_add_int(&xsoftc.xpt_generation, 1);
1619 xpt_remove_periph(struct cam_periph *periph)
1621 struct cam_ed *device;
1623 mtx_assert(periph->sim->mtx, MA_OWNED);
1625 device = periph->path->device;
1627 if (device != NULL) {
1629 struct periph_list *periph_head;
1631 periph_head = &device->periphs;
1633 /* Release the slot for this peripheral */
1635 camq_resize(&device->drvq, device->drvq.array_size - 1);
1637 device->generation++;
1639 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1644 atomic_add_int(&xsoftc.xpt_generation, 1);
1650 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1652 struct ccb_pathinq cpi;
1653 struct ccb_trans_settings cts;
1654 struct cam_path *path;
1660 mtx_assert(periph->sim->mtx, MA_OWNED);
1662 path = periph->path;
1664 * To ensure that this is printed in one piece,
1665 * mask out CAM interrupts.
1668 printf("%s%d at %s%d bus %d target %d lun %d\n",
1669 periph->periph_name, periph->unit_number,
1670 path->bus->sim->sim_name,
1671 path->bus->sim->unit_number,
1672 path->bus->sim->bus_id,
1673 path->target->target_id,
1674 path->device->lun_id);
1675 printf("%s%d: ", periph->periph_name, periph->unit_number);
1676 scsi_print_inquiry(&path->device->inq_data);
1677 if (bootverbose && path->device->serial_num_len > 0) {
1678 /* Don't wrap the screen - print only the first 60 chars */
1679 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1680 periph->unit_number, path->device->serial_num);
1682 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1683 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1684 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1685 xpt_action((union ccb*)&cts);
1686 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1690 /* Ask the SIM for its base transfer speed */
1691 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1692 cpi.ccb_h.func_code = XPT_PATH_INQ;
1693 xpt_action((union ccb *)&cpi);
1695 speed = cpi.base_transfer_speed;
1697 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1698 struct ccb_trans_settings_spi *spi;
1700 spi = &cts.xport_specific.spi;
1701 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1702 && spi->sync_offset != 0) {
1703 freq = scsi_calc_syncsrate(spi->sync_period);
1707 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1708 speed *= (0x01 << spi->bus_width);
1711 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1712 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1713 if (fc->valid & CTS_FC_VALID_SPEED) {
1714 speed = fc->bitrate;
1718 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1719 struct ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1720 if (sas->valid & CTS_SAS_VALID_SPEED) {
1721 speed = sas->bitrate;
1727 printf("%s%d: %d.%03dMB/s transfers",
1728 periph->periph_name, periph->unit_number,
1731 printf("%s%d: %dKB/s transfers", periph->periph_name,
1732 periph->unit_number, speed);
1733 /* Report additional information about SPI connections */
1734 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1735 struct ccb_trans_settings_spi *spi;
1737 spi = &cts.xport_specific.spi;
1739 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1741 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1745 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1746 && spi->bus_width > 0) {
1752 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1753 } else if (freq != 0) {
1757 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1758 struct ccb_trans_settings_fc *fc;
1760 fc = &cts.xport_specific.fc;
1761 if (fc->valid & CTS_FC_VALID_WWNN)
1762 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1763 if (fc->valid & CTS_FC_VALID_WWPN)
1764 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1765 if (fc->valid & CTS_FC_VALID_PORT)
1766 printf(" PortID 0x%x", fc->port);
1769 if (path->device->inq_flags & SID_CmdQue
1770 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1771 printf("\n%s%d: Command Queueing Enabled",
1772 periph->periph_name, periph->unit_number);
1777 * We only want to print the caller's announce string if they've
1780 if (announce_string != NULL)
1781 printf("%s%d: %s\n", periph->periph_name,
1782 periph->unit_number, announce_string);
1786 static dev_match_ret
1787 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1790 dev_match_ret retval;
1793 retval = DM_RET_NONE;
1796 * If we aren't given something to match against, that's an error.
1799 return(DM_RET_ERROR);
1802 * If there are no match entries, then this bus matches no
1805 if ((patterns == NULL) || (num_patterns == 0))
1806 return(DM_RET_DESCEND | DM_RET_COPY);
1808 for (i = 0; i < num_patterns; i++) {
1809 struct bus_match_pattern *cur_pattern;
1812 * If the pattern in question isn't for a bus node, we
1813 * aren't interested. However, we do indicate to the
1814 * calling routine that we should continue descending the
1815 * tree, since the user wants to match against lower-level
1818 if (patterns[i].type != DEV_MATCH_BUS) {
1819 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1820 retval |= DM_RET_DESCEND;
1824 cur_pattern = &patterns[i].pattern.bus_pattern;
1827 * If they want to match any bus node, we give them any
1830 if (cur_pattern->flags == BUS_MATCH_ANY) {
1831 /* set the copy flag */
1832 retval |= DM_RET_COPY;
1835 * If we've already decided on an action, go ahead
1838 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1843 * Not sure why someone would do this...
1845 if (cur_pattern->flags == BUS_MATCH_NONE)
1848 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1849 && (cur_pattern->path_id != bus->path_id))
1852 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1853 && (cur_pattern->bus_id != bus->sim->bus_id))
1856 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1857 && (cur_pattern->unit_number != bus->sim->unit_number))
1860 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1861 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1866 * If we get to this point, the user definitely wants
1867 * information on this bus. So tell the caller to copy the
1870 retval |= DM_RET_COPY;
1873 * If the return action has been set to descend, then we
1874 * know that we've already seen a non-bus matching
1875 * expression, therefore we need to further descend the tree.
1876 * This won't change by continuing around the loop, so we
1877 * go ahead and return. If we haven't seen a non-bus
1878 * matching expression, we keep going around the loop until
1879 * we exhaust the matching expressions. We'll set the stop
1880 * flag once we fall out of the loop.
1882 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1887 * If the return action hasn't been set to descend yet, that means
1888 * we haven't seen anything other than bus matching patterns. So
1889 * tell the caller to stop descending the tree -- the user doesn't
1890 * want to match against lower level tree elements.
1892 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1893 retval |= DM_RET_STOP;
1898 static dev_match_ret
1899 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1900 struct cam_ed *device)
1902 dev_match_ret retval;
1905 retval = DM_RET_NONE;
1908 * If we aren't given something to match against, that's an error.
1911 return(DM_RET_ERROR);
1914 * If there are no match entries, then this device matches no
1917 if ((patterns == NULL) || (num_patterns == 0))
1918 return(DM_RET_DESCEND | DM_RET_COPY);
1920 for (i = 0; i < num_patterns; i++) {
1921 struct device_match_pattern *cur_pattern;
1924 * If the pattern in question isn't for a device node, we
1925 * aren't interested.
1927 if (patterns[i].type != DEV_MATCH_DEVICE) {
1928 if ((patterns[i].type == DEV_MATCH_PERIPH)
1929 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1930 retval |= DM_RET_DESCEND;
1934 cur_pattern = &patterns[i].pattern.device_pattern;
1937 * If they want to match any device node, we give them any
1940 if (cur_pattern->flags == DEV_MATCH_ANY) {
1941 /* set the copy flag */
1942 retval |= DM_RET_COPY;
1946 * If we've already decided on an action, go ahead
1949 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1954 * Not sure why someone would do this...
1956 if (cur_pattern->flags == DEV_MATCH_NONE)
1959 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1960 && (cur_pattern->path_id != device->target->bus->path_id))
1963 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1964 && (cur_pattern->target_id != device->target->target_id))
1967 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1968 && (cur_pattern->target_lun != device->lun_id))
1971 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1972 && (cam_quirkmatch((caddr_t)&device->inq_data,
1973 (caddr_t)&cur_pattern->inq_pat,
1974 1, sizeof(cur_pattern->inq_pat),
1975 scsi_static_inquiry_match) == NULL))
1979 * If we get to this point, the user definitely wants
1980 * information on this device. So tell the caller to copy
1983 retval |= DM_RET_COPY;
1986 * If the return action has been set to descend, then we
1987 * know that we've already seen a peripheral matching
1988 * expression, therefore we need to further descend the tree.
1989 * This won't change by continuing around the loop, so we
1990 * go ahead and return. If we haven't seen a peripheral
1991 * matching expression, we keep going around the loop until
1992 * we exhaust the matching expressions. We'll set the stop
1993 * flag once we fall out of the loop.
1995 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
2000 * If the return action hasn't been set to descend yet, that means
2001 * we haven't seen any peripheral matching patterns. So tell the
2002 * caller to stop descending the tree -- the user doesn't want to
2003 * match against lower level tree elements.
2005 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
2006 retval |= DM_RET_STOP;
2012 * Match a single peripheral against any number of match patterns.
2014 static dev_match_ret
2015 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
2016 struct cam_periph *periph)
2018 dev_match_ret retval;
2022 * If we aren't given something to match against, that's an error.
2025 return(DM_RET_ERROR);
2028 * If there are no match entries, then this peripheral matches no
2031 if ((patterns == NULL) || (num_patterns == 0))
2032 return(DM_RET_STOP | DM_RET_COPY);
2035 * There aren't any nodes below a peripheral node, so there's no
2036 * reason to descend the tree any further.
2038 retval = DM_RET_STOP;
2040 for (i = 0; i < num_patterns; i++) {
2041 struct periph_match_pattern *cur_pattern;
2044 * If the pattern in question isn't for a peripheral, we
2045 * aren't interested.
2047 if (patterns[i].type != DEV_MATCH_PERIPH)
2050 cur_pattern = &patterns[i].pattern.periph_pattern;
2053 * If they want to match on anything, then we will do so.
2055 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2056 /* set the copy flag */
2057 retval |= DM_RET_COPY;
2060 * We've already set the return action to stop,
2061 * since there are no nodes below peripherals in
2068 * Not sure why someone would do this...
2070 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2073 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2074 && (cur_pattern->path_id != periph->path->bus->path_id))
2078 * For the target and lun id's, we have to make sure the
2079 * target and lun pointers aren't NULL. The xpt peripheral
2080 * has a wildcard target and device.
2082 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2083 && ((periph->path->target == NULL)
2084 ||(cur_pattern->target_id != periph->path->target->target_id)))
2087 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2088 && ((periph->path->device == NULL)
2089 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2092 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2093 && (cur_pattern->unit_number != periph->unit_number))
2096 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2097 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2102 * If we get to this point, the user definitely wants
2103 * information on this peripheral. So tell the caller to
2104 * copy the data out.
2106 retval |= DM_RET_COPY;
2109 * The return action has already been set to stop, since
2110 * peripherals don't have any nodes below them in the EDT.
2116 * If we get to this point, the peripheral that was passed in
2117 * doesn't match any of the patterns.
2123 xptedtbusfunc(struct cam_eb *bus, void *arg)
2125 struct ccb_dev_match *cdm;
2126 dev_match_ret retval;
2128 cdm = (struct ccb_dev_match *)arg;
2131 * If our position is for something deeper in the tree, that means
2132 * that we've already seen this node. So, we keep going down.
2134 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2135 && (cdm->pos.cookie.bus == bus)
2136 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2137 && (cdm->pos.cookie.target != NULL))
2138 retval = DM_RET_DESCEND;
2140 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2143 * If we got an error, bail out of the search.
2145 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2146 cdm->status = CAM_DEV_MATCH_ERROR;
2151 * If the copy flag is set, copy this bus out.
2153 if (retval & DM_RET_COPY) {
2156 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2157 sizeof(struct dev_match_result));
2160 * If we don't have enough space to put in another
2161 * match result, save our position and tell the
2162 * user there are more devices to check.
2164 if (spaceleft < sizeof(struct dev_match_result)) {
2165 bzero(&cdm->pos, sizeof(cdm->pos));
2166 cdm->pos.position_type =
2167 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2169 cdm->pos.cookie.bus = bus;
2170 cdm->pos.generations[CAM_BUS_GENERATION]=
2171 xsoftc.bus_generation;
2172 cdm->status = CAM_DEV_MATCH_MORE;
2175 j = cdm->num_matches;
2177 cdm->matches[j].type = DEV_MATCH_BUS;
2178 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2179 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2180 cdm->matches[j].result.bus_result.unit_number =
2181 bus->sim->unit_number;
2182 strncpy(cdm->matches[j].result.bus_result.dev_name,
2183 bus->sim->sim_name, DEV_IDLEN);
2187 * If the user is only interested in busses, there's no
2188 * reason to descend to the next level in the tree.
2190 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2194 * If there is a target generation recorded, check it to
2195 * make sure the target list hasn't changed.
2197 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2198 && (bus == cdm->pos.cookie.bus)
2199 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2200 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2201 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2203 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2207 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2208 && (cdm->pos.cookie.bus == bus)
2209 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2210 && (cdm->pos.cookie.target != NULL))
2211 return(xpttargettraverse(bus,
2212 (struct cam_et *)cdm->pos.cookie.target,
2213 xptedttargetfunc, arg));
2215 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2219 xptedttargetfunc(struct cam_et *target, void *arg)
2221 struct ccb_dev_match *cdm;
2223 cdm = (struct ccb_dev_match *)arg;
2226 * If there is a device list generation recorded, check it to
2227 * make sure the device list hasn't changed.
2229 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2230 && (cdm->pos.cookie.bus == target->bus)
2231 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2232 && (cdm->pos.cookie.target == target)
2233 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2234 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2235 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2236 target->generation)) {
2237 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2241 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2242 && (cdm->pos.cookie.bus == target->bus)
2243 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2244 && (cdm->pos.cookie.target == target)
2245 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2246 && (cdm->pos.cookie.device != NULL))
2247 return(xptdevicetraverse(target,
2248 (struct cam_ed *)cdm->pos.cookie.device,
2249 xptedtdevicefunc, arg));
2251 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2255 xptedtdevicefunc(struct cam_ed *device, void *arg)
2258 struct ccb_dev_match *cdm;
2259 dev_match_ret retval;
2261 cdm = (struct ccb_dev_match *)arg;
2264 * If our position is for something deeper in the tree, that means
2265 * that we've already seen this node. So, we keep going down.
2267 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2268 && (cdm->pos.cookie.device == device)
2269 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2270 && (cdm->pos.cookie.periph != NULL))
2271 retval = DM_RET_DESCEND;
2273 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2276 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2277 cdm->status = CAM_DEV_MATCH_ERROR;
2282 * If the copy flag is set, copy this device out.
2284 if (retval & DM_RET_COPY) {
2287 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2288 sizeof(struct dev_match_result));
2291 * If we don't have enough space to put in another
2292 * match result, save our position and tell the
2293 * user there are more devices to check.
2295 if (spaceleft < sizeof(struct dev_match_result)) {
2296 bzero(&cdm->pos, sizeof(cdm->pos));
2297 cdm->pos.position_type =
2298 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2299 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2301 cdm->pos.cookie.bus = device->target->bus;
2302 cdm->pos.generations[CAM_BUS_GENERATION]=
2303 xsoftc.bus_generation;
2304 cdm->pos.cookie.target = device->target;
2305 cdm->pos.generations[CAM_TARGET_GENERATION] =
2306 device->target->bus->generation;
2307 cdm->pos.cookie.device = device;
2308 cdm->pos.generations[CAM_DEV_GENERATION] =
2309 device->target->generation;
2310 cdm->status = CAM_DEV_MATCH_MORE;
2313 j = cdm->num_matches;
2315 cdm->matches[j].type = DEV_MATCH_DEVICE;
2316 cdm->matches[j].result.device_result.path_id =
2317 device->target->bus->path_id;
2318 cdm->matches[j].result.device_result.target_id =
2319 device->target->target_id;
2320 cdm->matches[j].result.device_result.target_lun =
2322 bcopy(&device->inq_data,
2323 &cdm->matches[j].result.device_result.inq_data,
2324 sizeof(struct scsi_inquiry_data));
2326 /* Let the user know whether this device is unconfigured */
2327 if (device->flags & CAM_DEV_UNCONFIGURED)
2328 cdm->matches[j].result.device_result.flags =
2329 DEV_RESULT_UNCONFIGURED;
2331 cdm->matches[j].result.device_result.flags =
2336 * If the user isn't interested in peripherals, don't descend
2337 * the tree any further.
2339 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2343 * If there is a peripheral list generation recorded, make sure
2344 * it hasn't changed.
2346 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2347 && (device->target->bus == cdm->pos.cookie.bus)
2348 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2349 && (device->target == cdm->pos.cookie.target)
2350 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2351 && (device == cdm->pos.cookie.device)
2352 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2353 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2354 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2355 device->generation)){
2356 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2360 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2361 && (cdm->pos.cookie.bus == device->target->bus)
2362 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2363 && (cdm->pos.cookie.target == device->target)
2364 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2365 && (cdm->pos.cookie.device == device)
2366 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2367 && (cdm->pos.cookie.periph != NULL))
2368 return(xptperiphtraverse(device,
2369 (struct cam_periph *)cdm->pos.cookie.periph,
2370 xptedtperiphfunc, arg));
2372 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2376 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2378 struct ccb_dev_match *cdm;
2379 dev_match_ret retval;
2381 cdm = (struct ccb_dev_match *)arg;
2383 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2385 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2386 cdm->status = CAM_DEV_MATCH_ERROR;
2391 * If the copy flag is set, copy this peripheral out.
2393 if (retval & DM_RET_COPY) {
2396 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2397 sizeof(struct dev_match_result));
2400 * If we don't have enough space to put in another
2401 * match result, save our position and tell the
2402 * user there are more devices to check.
2404 if (spaceleft < sizeof(struct dev_match_result)) {
2405 bzero(&cdm->pos, sizeof(cdm->pos));
2406 cdm->pos.position_type =
2407 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2408 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2411 cdm->pos.cookie.bus = periph->path->bus;
2412 cdm->pos.generations[CAM_BUS_GENERATION]=
2413 xsoftc.bus_generation;
2414 cdm->pos.cookie.target = periph->path->target;
2415 cdm->pos.generations[CAM_TARGET_GENERATION] =
2416 periph->path->bus->generation;
2417 cdm->pos.cookie.device = periph->path->device;
2418 cdm->pos.generations[CAM_DEV_GENERATION] =
2419 periph->path->target->generation;
2420 cdm->pos.cookie.periph = periph;
2421 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2422 periph->path->device->generation;
2423 cdm->status = CAM_DEV_MATCH_MORE;
2427 j = cdm->num_matches;
2429 cdm->matches[j].type = DEV_MATCH_PERIPH;
2430 cdm->matches[j].result.periph_result.path_id =
2431 periph->path->bus->path_id;
2432 cdm->matches[j].result.periph_result.target_id =
2433 periph->path->target->target_id;
2434 cdm->matches[j].result.periph_result.target_lun =
2435 periph->path->device->lun_id;
2436 cdm->matches[j].result.periph_result.unit_number =
2437 periph->unit_number;
2438 strncpy(cdm->matches[j].result.periph_result.periph_name,
2439 periph->periph_name, DEV_IDLEN);
2446 xptedtmatch(struct ccb_dev_match *cdm)
2450 cdm->num_matches = 0;
2453 * Check the bus list generation. If it has changed, the user
2454 * needs to reset everything and start over.
2456 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2457 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2458 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
2459 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2463 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2464 && (cdm->pos.cookie.bus != NULL))
2465 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2466 xptedtbusfunc, cdm);
2468 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2471 * If we get back 0, that means that we had to stop before fully
2472 * traversing the EDT. It also means that one of the subroutines
2473 * has set the status field to the proper value. If we get back 1,
2474 * we've fully traversed the EDT and copied out any matching entries.
2477 cdm->status = CAM_DEV_MATCH_LAST;
2483 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2485 struct ccb_dev_match *cdm;
2487 cdm = (struct ccb_dev_match *)arg;
2489 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2490 && (cdm->pos.cookie.pdrv == pdrv)
2491 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2492 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2493 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2494 (*pdrv)->generation)) {
2495 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2499 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2500 && (cdm->pos.cookie.pdrv == pdrv)
2501 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2502 && (cdm->pos.cookie.periph != NULL))
2503 return(xptpdperiphtraverse(pdrv,
2504 (struct cam_periph *)cdm->pos.cookie.periph,
2505 xptplistperiphfunc, arg));
2507 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2511 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2513 struct ccb_dev_match *cdm;
2514 dev_match_ret retval;
2516 cdm = (struct ccb_dev_match *)arg;
2518 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2520 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2521 cdm->status = CAM_DEV_MATCH_ERROR;
2526 * If the copy flag is set, copy this peripheral out.
2528 if (retval & DM_RET_COPY) {
2531 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2532 sizeof(struct dev_match_result));
2535 * If we don't have enough space to put in another
2536 * match result, save our position and tell the
2537 * user there are more devices to check.
2539 if (spaceleft < sizeof(struct dev_match_result)) {
2540 struct periph_driver **pdrv;
2543 bzero(&cdm->pos, sizeof(cdm->pos));
2544 cdm->pos.position_type =
2545 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2549 * This may look a bit non-sensical, but it is
2550 * actually quite logical. There are very few
2551 * peripheral drivers, and bloating every peripheral
2552 * structure with a pointer back to its parent
2553 * peripheral driver linker set entry would cost
2554 * more in the long run than doing this quick lookup.
2556 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2557 if (strcmp((*pdrv)->driver_name,
2558 periph->periph_name) == 0)
2562 if (*pdrv == NULL) {
2563 cdm->status = CAM_DEV_MATCH_ERROR;
2567 cdm->pos.cookie.pdrv = pdrv;
2569 * The periph generation slot does double duty, as
2570 * does the periph pointer slot. They are used for
2571 * both edt and pdrv lookups and positioning.
2573 cdm->pos.cookie.periph = periph;
2574 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2575 (*pdrv)->generation;
2576 cdm->status = CAM_DEV_MATCH_MORE;
2580 j = cdm->num_matches;
2582 cdm->matches[j].type = DEV_MATCH_PERIPH;
2583 cdm->matches[j].result.periph_result.path_id =
2584 periph->path->bus->path_id;
2587 * The transport layer peripheral doesn't have a target or
2590 if (periph->path->target)
2591 cdm->matches[j].result.periph_result.target_id =
2592 periph->path->target->target_id;
2594 cdm->matches[j].result.periph_result.target_id = -1;
2596 if (periph->path->device)
2597 cdm->matches[j].result.periph_result.target_lun =
2598 periph->path->device->lun_id;
2600 cdm->matches[j].result.periph_result.target_lun = -1;
2602 cdm->matches[j].result.periph_result.unit_number =
2603 periph->unit_number;
2604 strncpy(cdm->matches[j].result.periph_result.periph_name,
2605 periph->periph_name, DEV_IDLEN);
2612 xptperiphlistmatch(struct ccb_dev_match *cdm)
2616 cdm->num_matches = 0;
2619 * At this point in the edt traversal function, we check the bus
2620 * list generation to make sure that no busses have been added or
2621 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2622 * For the peripheral driver list traversal function, however, we
2623 * don't have to worry about new peripheral driver types coming or
2624 * going; they're in a linker set, and therefore can't change
2625 * without a recompile.
2628 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2629 && (cdm->pos.cookie.pdrv != NULL))
2630 ret = xptpdrvtraverse(
2631 (struct periph_driver **)cdm->pos.cookie.pdrv,
2632 xptplistpdrvfunc, cdm);
2634 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2637 * If we get back 0, that means that we had to stop before fully
2638 * traversing the peripheral driver tree. It also means that one of
2639 * the subroutines has set the status field to the proper value. If
2640 * we get back 1, we've fully traversed the EDT and copied out any
2644 cdm->status = CAM_DEV_MATCH_LAST;
2650 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2652 struct cam_eb *bus, *next_bus;
2657 mtx_lock(&xsoftc.xpt_topo_lock);
2658 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2661 next_bus = TAILQ_NEXT(bus, links);
2663 mtx_unlock(&xsoftc.xpt_topo_lock);
2664 CAM_SIM_LOCK(bus->sim);
2665 retval = tr_func(bus, arg);
2666 CAM_SIM_UNLOCK(bus->sim);
2669 mtx_lock(&xsoftc.xpt_topo_lock);
2671 mtx_unlock(&xsoftc.xpt_topo_lock);
2677 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2678 xpt_targetfunc_t *tr_func, void *arg)
2680 struct cam_et *target, *next_target;
2684 for (target = (start_target ? start_target :
2685 TAILQ_FIRST(&bus->et_entries));
2686 target != NULL; target = next_target) {
2688 next_target = TAILQ_NEXT(target, links);
2690 retval = tr_func(target, arg);
2700 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2701 xpt_devicefunc_t *tr_func, void *arg)
2703 struct cam_ed *device, *next_device;
2707 for (device = (start_device ? start_device :
2708 TAILQ_FIRST(&target->ed_entries));
2710 device = next_device) {
2712 next_device = TAILQ_NEXT(device, links);
2714 retval = tr_func(device, arg);
2724 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2725 xpt_periphfunc_t *tr_func, void *arg)
2727 struct cam_periph *periph, *next_periph;
2732 for (periph = (start_periph ? start_periph :
2733 SLIST_FIRST(&device->periphs));
2735 periph = next_periph) {
2737 next_periph = SLIST_NEXT(periph, periph_links);
2739 retval = tr_func(periph, arg);
2748 xptpdrvtraverse(struct periph_driver **start_pdrv,
2749 xpt_pdrvfunc_t *tr_func, void *arg)
2751 struct periph_driver **pdrv;
2757 * We don't traverse the peripheral driver list like we do the
2758 * other lists, because it is a linker set, and therefore cannot be
2759 * changed during runtime. If the peripheral driver list is ever
2760 * re-done to be something other than a linker set (i.e. it can
2761 * change while the system is running), the list traversal should
2762 * be modified to work like the other traversal functions.
2764 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2765 *pdrv != NULL; pdrv++) {
2766 retval = tr_func(pdrv, arg);
2776 xptpdperiphtraverse(struct periph_driver **pdrv,
2777 struct cam_periph *start_periph,
2778 xpt_periphfunc_t *tr_func, void *arg)
2780 struct cam_periph *periph, *next_periph;
2785 for (periph = (start_periph ? start_periph :
2786 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2787 periph = next_periph) {
2789 next_periph = TAILQ_NEXT(periph, unit_links);
2791 retval = tr_func(periph, arg);
2799 xptdefbusfunc(struct cam_eb *bus, void *arg)
2801 struct xpt_traverse_config *tr_config;
2803 tr_config = (struct xpt_traverse_config *)arg;
2805 if (tr_config->depth == XPT_DEPTH_BUS) {
2806 xpt_busfunc_t *tr_func;
2808 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2810 return(tr_func(bus, tr_config->tr_arg));
2812 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2816 xptdeftargetfunc(struct cam_et *target, void *arg)
2818 struct xpt_traverse_config *tr_config;
2820 tr_config = (struct xpt_traverse_config *)arg;
2822 if (tr_config->depth == XPT_DEPTH_TARGET) {
2823 xpt_targetfunc_t *tr_func;
2825 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2827 return(tr_func(target, tr_config->tr_arg));
2829 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2833 xptdefdevicefunc(struct cam_ed *device, void *arg)
2835 struct xpt_traverse_config *tr_config;
2837 tr_config = (struct xpt_traverse_config *)arg;
2839 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2840 xpt_devicefunc_t *tr_func;
2842 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2844 return(tr_func(device, tr_config->tr_arg));
2846 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2850 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2852 struct xpt_traverse_config *tr_config;
2853 xpt_periphfunc_t *tr_func;
2855 tr_config = (struct xpt_traverse_config *)arg;
2857 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2860 * Unlike the other default functions, we don't check for depth
2861 * here. The peripheral driver level is the last level in the EDT,
2862 * so if we're here, we should execute the function in question.
2864 return(tr_func(periph, tr_config->tr_arg));
2868 * Execute the given function for every bus in the EDT.
2871 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2873 struct xpt_traverse_config tr_config;
2875 tr_config.depth = XPT_DEPTH_BUS;
2876 tr_config.tr_func = tr_func;
2877 tr_config.tr_arg = arg;
2879 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2883 * Execute the given function for every device in the EDT.
2886 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2888 struct xpt_traverse_config tr_config;
2890 tr_config.depth = XPT_DEPTH_DEVICE;
2891 tr_config.tr_func = tr_func;
2892 tr_config.tr_arg = arg;
2894 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2898 xptsetasyncfunc(struct cam_ed *device, void *arg)
2900 struct cam_path path;
2901 struct ccb_getdev cgd;
2902 struct async_node *cur_entry;
2904 cur_entry = (struct async_node *)arg;
2907 * Don't report unconfigured devices (Wildcard devs,
2908 * devices only for target mode, device instances
2909 * that have been invalidated but are waiting for
2910 * their last reference count to be released).
2912 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2915 xpt_compile_path(&path,
2917 device->target->bus->path_id,
2918 device->target->target_id,
2920 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2921 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2922 xpt_action((union ccb *)&cgd);
2923 cur_entry->callback(cur_entry->callback_arg,
2926 xpt_release_path(&path);
2932 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2934 struct cam_path path;
2935 struct ccb_pathinq cpi;
2936 struct async_node *cur_entry;
2938 cur_entry = (struct async_node *)arg;
2940 xpt_compile_path(&path, /*periph*/NULL,
2942 CAM_TARGET_WILDCARD,
2944 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2945 cpi.ccb_h.func_code = XPT_PATH_INQ;
2946 xpt_action((union ccb *)&cpi);
2947 cur_entry->callback(cur_entry->callback_arg,
2950 xpt_release_path(&path);
2956 xpt_action_sasync_cb(void *context, int pending)
2958 struct async_node *cur_entry;
2959 struct xpt_task *task;
2962 task = (struct xpt_task *)context;
2963 cur_entry = (struct async_node *)task->data1;
2964 added = task->data2;
2966 if ((added & AC_FOUND_DEVICE) != 0) {
2968 * Get this peripheral up to date with all
2969 * the currently existing devices.
2971 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
2973 if ((added & AC_PATH_REGISTERED) != 0) {
2975 * Get this peripheral up to date with all
2976 * the currently existing busses.
2978 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
2981 free(task, M_CAMXPT);
2985 xpt_action(union ccb *start_ccb)
2989 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2991 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2993 iopl = splsoftcam();
2994 switch (start_ccb->ccb_h.func_code) {
2997 struct cam_ed *device;
2999 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3000 struct cam_path *path;
3002 path = start_ccb->ccb_h.path;
3006 * For the sake of compatibility with SCSI-1
3007 * devices that may not understand the identify
3008 * message, we include lun information in the
3009 * second byte of all commands. SCSI-1 specifies
3010 * that luns are a 3 bit value and reserves only 3
3011 * bits for lun information in the CDB. Later
3012 * revisions of the SCSI spec allow for more than 8
3013 * luns, but have deprecated lun information in the
3014 * CDB. So, if the lun won't fit, we must omit.
3016 * Also be aware that during initial probing for devices,
3017 * the inquiry information is unknown but initialized to 0.
3018 * This means that this code will be exercised while probing
3019 * devices with an ANSI revision greater than 2.
3021 device = start_ccb->ccb_h.path->device;
3022 if (device->protocol_version <= SCSI_REV_2
3023 && start_ccb->ccb_h.target_lun < 8
3024 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
3026 start_ccb->csio.cdb_io.cdb_bytes[1] |=
3027 start_ccb->ccb_h.target_lun << 5;
3029 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3030 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3031 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3032 &path->device->inq_data),
3033 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3034 cdb_str, sizeof(cdb_str))));
3038 case XPT_CONT_TARGET_IO:
3039 start_ccb->csio.sense_resid = 0;
3040 start_ccb->csio.resid = 0;
3045 struct cam_path *path;
3046 struct cam_sim *sim;
3050 path = start_ccb->ccb_h.path;
3053 sim = path->bus->sim;
3054 if (SIM_DEAD(sim)) {
3055 /* The SIM has gone; just execute the CCB directly. */
3056 cam_ccbq_send_ccb(&path->device->ccbq, start_ccb);
3057 (*(sim->sim_action))(sim, start_ccb);
3062 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3063 if (path->device->qfrozen_cnt == 0)
3064 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3069 xpt_run_dev_sendq(path->bus);
3072 case XPT_SET_TRAN_SETTINGS:
3074 xpt_set_transfer_settings(&start_ccb->cts,
3075 start_ccb->ccb_h.path->device,
3076 /*async_update*/FALSE);
3079 case XPT_CALC_GEOMETRY:
3081 struct cam_sim *sim;
3083 /* Filter out garbage */
3084 if (start_ccb->ccg.block_size == 0
3085 || start_ccb->ccg.volume_size == 0) {
3086 start_ccb->ccg.cylinders = 0;
3087 start_ccb->ccg.heads = 0;
3088 start_ccb->ccg.secs_per_track = 0;
3089 start_ccb->ccb_h.status = CAM_REQ_CMP;
3094 * In a PC-98 system, geometry translation depens on
3095 * the "real" device geometry obtained from mode page 4.
3096 * SCSI geometry translation is performed in the
3097 * initialization routine of the SCSI BIOS and the result
3098 * stored in host memory. If the translation is available
3099 * in host memory, use it. If not, rely on the default
3100 * translation the device driver performs.
3102 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3103 start_ccb->ccb_h.status = CAM_REQ_CMP;
3107 sim = start_ccb->ccb_h.path->bus->sim;
3108 (*(sim->sim_action))(sim, start_ccb);
3113 union ccb* abort_ccb;
3116 abort_ccb = start_ccb->cab.abort_ccb;
3117 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3119 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3120 struct cam_ccbq *ccbq;
3122 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3123 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3124 abort_ccb->ccb_h.status =
3125 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3126 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3128 xpt_done(abort_ccb);
3130 start_ccb->ccb_h.status = CAM_REQ_CMP;
3133 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3134 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3136 * We've caught this ccb en route to
3137 * the SIM. Flag it for abort and the
3138 * SIM will do so just before starting
3139 * real work on the CCB.
3141 abort_ccb->ccb_h.status =
3142 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3143 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3144 start_ccb->ccb_h.status = CAM_REQ_CMP;
3148 if (XPT_FC_IS_QUEUED(abort_ccb)
3149 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3151 * It's already completed but waiting
3152 * for our SWI to get to it.
3154 start_ccb->ccb_h.status = CAM_UA_ABORT;
3158 * If we weren't able to take care of the abort request
3159 * in the XPT, pass the request down to the SIM for processing.
3163 case XPT_ACCEPT_TARGET_IO:
3165 case XPT_IMMED_NOTIFY:
3166 case XPT_NOTIFY_ACK:
3167 case XPT_GET_TRAN_SETTINGS:
3170 struct cam_sim *sim;
3172 sim = start_ccb->ccb_h.path->bus->sim;
3173 (*(sim->sim_action))(sim, start_ccb);
3178 struct cam_sim *sim;
3180 sim = start_ccb->ccb_h.path->bus->sim;
3181 (*(sim->sim_action))(sim, start_ccb);
3184 case XPT_PATH_STATS:
3185 start_ccb->cpis.last_reset =
3186 start_ccb->ccb_h.path->bus->last_reset;
3187 start_ccb->ccb_h.status = CAM_REQ_CMP;
3194 dev = start_ccb->ccb_h.path->device;
3196 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3197 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3199 struct ccb_getdev *cgd;
3203 cgd = &start_ccb->cgd;
3204 bus = cgd->ccb_h.path->bus;
3205 tar = cgd->ccb_h.path->target;
3206 cgd->inq_data = dev->inq_data;
3207 cgd->ccb_h.status = CAM_REQ_CMP;
3208 cgd->serial_num_len = dev->serial_num_len;
3209 if ((dev->serial_num_len > 0)
3210 && (dev->serial_num != NULL))
3211 bcopy(dev->serial_num, cgd->serial_num,
3212 dev->serial_num_len);
3217 case XPT_GDEV_STATS:
3222 dev = start_ccb->ccb_h.path->device;
3224 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3225 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3227 struct ccb_getdevstats *cgds;
3231 cgds = &start_ccb->cgds;
3232 bus = cgds->ccb_h.path->bus;
3233 tar = cgds->ccb_h.path->target;
3234 cgds->dev_openings = dev->ccbq.dev_openings;
3235 cgds->dev_active = dev->ccbq.dev_active;
3236 cgds->devq_openings = dev->ccbq.devq_openings;
3237 cgds->devq_queued = dev->ccbq.queue.entries;
3238 cgds->held = dev->ccbq.held;
3239 cgds->last_reset = tar->last_reset;
3240 cgds->maxtags = dev->quirk->maxtags;
3241 cgds->mintags = dev->quirk->mintags;
3242 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3243 cgds->last_reset = bus->last_reset;
3244 cgds->ccb_h.status = CAM_REQ_CMP;
3251 struct cam_periph *nperiph;
3252 struct periph_list *periph_head;
3253 struct ccb_getdevlist *cgdl;
3256 struct cam_ed *device;
3263 * Don't want anyone mucking with our data.
3266 device = start_ccb->ccb_h.path->device;
3267 periph_head = &device->periphs;
3268 cgdl = &start_ccb->cgdl;
3271 * Check and see if the list has changed since the user
3272 * last requested a list member. If so, tell them that the
3273 * list has changed, and therefore they need to start over
3274 * from the beginning.
3276 if ((cgdl->index != 0) &&
3277 (cgdl->generation != device->generation)) {
3278 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3284 * Traverse the list of peripherals and attempt to find
3285 * the requested peripheral.
3287 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3288 (nperiph != NULL) && (i <= cgdl->index);
3289 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3290 if (i == cgdl->index) {
3291 strncpy(cgdl->periph_name,
3292 nperiph->periph_name,
3294 cgdl->unit_number = nperiph->unit_number;
3299 cgdl->status = CAM_GDEVLIST_ERROR;
3304 if (nperiph == NULL)
3305 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3307 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3310 cgdl->generation = device->generation;
3313 cgdl->ccb_h.status = CAM_REQ_CMP;
3319 dev_pos_type position_type;
3320 struct ccb_dev_match *cdm;
3322 cdm = &start_ccb->cdm;
3325 * Prevent EDT changes while we traverse it.
3329 * There are two ways of getting at information in the EDT.
3330 * The first way is via the primary EDT tree. It starts
3331 * with a list of busses, then a list of targets on a bus,
3332 * then devices/luns on a target, and then peripherals on a
3333 * device/lun. The "other" way is by the peripheral driver
3334 * lists. The peripheral driver lists are organized by
3335 * peripheral driver. (obviously) So it makes sense to
3336 * use the peripheral driver list if the user is looking
3337 * for something like "da1", or all "da" devices. If the
3338 * user is looking for something on a particular bus/target
3339 * or lun, it's generally better to go through the EDT tree.
3342 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3343 position_type = cdm->pos.position_type;
3347 position_type = CAM_DEV_POS_NONE;
3349 for (i = 0; i < cdm->num_patterns; i++) {
3350 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3351 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3352 position_type = CAM_DEV_POS_EDT;
3357 if (cdm->num_patterns == 0)
3358 position_type = CAM_DEV_POS_EDT;
3359 else if (position_type == CAM_DEV_POS_NONE)
3360 position_type = CAM_DEV_POS_PDRV;
3363 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3364 case CAM_DEV_POS_EDT:
3367 case CAM_DEV_POS_PDRV:
3368 xptperiphlistmatch(cdm);
3371 cdm->status = CAM_DEV_MATCH_ERROR;
3377 if (cdm->status == CAM_DEV_MATCH_ERROR)
3378 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3380 start_ccb->ccb_h.status = CAM_REQ_CMP;
3386 struct ccb_setasync *csa;
3387 struct async_node *cur_entry;
3388 struct async_list *async_head;
3391 csa = &start_ccb->csa;
3392 added = csa->event_enable;
3393 async_head = &csa->ccb_h.path->device->asyncs;
3396 * If there is already an entry for us, simply
3399 cur_entry = SLIST_FIRST(async_head);
3400 while (cur_entry != NULL) {
3401 if ((cur_entry->callback_arg == csa->callback_arg)
3402 && (cur_entry->callback == csa->callback))
3404 cur_entry = SLIST_NEXT(cur_entry, links);
3407 if (cur_entry != NULL) {
3409 * If the request has no flags set,
3412 added &= ~cur_entry->event_enable;
3413 if (csa->event_enable == 0) {
3414 SLIST_REMOVE(async_head, cur_entry,
3416 csa->ccb_h.path->device->refcount--;
3417 free(cur_entry, M_CAMXPT);
3419 cur_entry->event_enable = csa->event_enable;
3422 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3424 if (cur_entry == NULL) {
3425 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3428 cur_entry->event_enable = csa->event_enable;
3429 cur_entry->callback_arg = csa->callback_arg;
3430 cur_entry->callback = csa->callback;
3431 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3432 csa->ccb_h.path->device->refcount++;
3436 * Need to decouple this operation via a taqskqueue so that
3437 * the locking doesn't become a mess.
3439 if ((added & (AC_FOUND_DEVICE | AC_PATH_REGISTERED)) != 0) {
3440 struct xpt_task *task;
3442 task = malloc(sizeof(struct xpt_task), M_CAMXPT,
3445 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3449 TASK_INIT(&task->task, 0, xpt_action_sasync_cb, task);
3450 task->data1 = cur_entry;
3451 task->data2 = added;
3452 taskqueue_enqueue(taskqueue_thread, &task->task);
3455 start_ccb->ccb_h.status = CAM_REQ_CMP;
3460 struct ccb_relsim *crs;
3464 crs = &start_ccb->crs;
3465 dev = crs->ccb_h.path->device;
3468 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3474 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3476 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
3477 /* Don't ever go below one opening */
3478 if (crs->openings > 0) {
3479 xpt_dev_ccbq_resize(crs->ccb_h.path,
3483 xpt_print(crs->ccb_h.path,
3484 "tagged openings now %d\n",
3491 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3493 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3496 * Just extend the old timeout and decrement
3497 * the freeze count so that a single timeout
3498 * is sufficient for releasing the queue.
3500 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3501 callout_stop(&dev->callout);
3504 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3507 callout_reset(&dev->callout,
3508 (crs->release_timeout * hz) / 1000,
3509 xpt_release_devq_timeout, dev);
3511 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3515 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3517 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3519 * Decrement the freeze count so that a single
3520 * completion is still sufficient to unfreeze
3523 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3526 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3527 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3531 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3533 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3534 || (dev->ccbq.dev_active == 0)) {
3536 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3539 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3540 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3545 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3547 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3550 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3551 start_ccb->ccb_h.status = CAM_REQ_CMP;
3555 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3558 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3559 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3567 #ifdef CAM_DEBUG_DELAY
3568 cam_debug_delay = CAM_DEBUG_DELAY;
3570 cam_dflags = start_ccb->cdbg.flags;
3571 if (cam_dpath != NULL) {
3572 xpt_free_path(cam_dpath);
3576 if (cam_dflags != CAM_DEBUG_NONE) {
3577 if (xpt_create_path(&cam_dpath, xpt_periph,
3578 start_ccb->ccb_h.path_id,
3579 start_ccb->ccb_h.target_id,
3580 start_ccb->ccb_h.target_lun) !=
3582 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3583 cam_dflags = CAM_DEBUG_NONE;
3585 start_ccb->ccb_h.status = CAM_REQ_CMP;
3586 xpt_print(cam_dpath, "debugging flags now %x\n",
3591 start_ccb->ccb_h.status = CAM_REQ_CMP;
3594 #else /* !CAMDEBUG */
3595 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3596 #endif /* CAMDEBUG */
3600 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3601 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3602 start_ccb->ccb_h.status = CAM_REQ_CMP;
3609 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3616 xpt_polled_action(union ccb *start_ccb)
3620 struct cam_sim *sim;
3621 struct cam_devq *devq;
3625 timeout = start_ccb->ccb_h.timeout;
3626 sim = start_ccb->ccb_h.path->bus->sim;
3628 dev = start_ccb->ccb_h.path->device;
3630 mtx_assert(sim->mtx, MA_OWNED);
3634 * Steal an opening so that no other queued requests
3635 * can get it before us while we simulate interrupts.
3637 dev->ccbq.devq_openings--;
3638 dev->ccbq.dev_openings--;
3640 while(((devq != NULL && devq->send_openings <= 0) ||
3641 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3643 (*(sim->sim_poll))(sim);
3644 camisr_runqueue(&sim->sim_doneq);
3647 dev->ccbq.devq_openings++;
3648 dev->ccbq.dev_openings++;
3651 xpt_action(start_ccb);
3652 while(--timeout > 0) {
3653 (*(sim->sim_poll))(sim);
3654 camisr_runqueue(&sim->sim_doneq);
3655 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3662 * XXX Is it worth adding a sim_timeout entry
3663 * point so we can attempt recovery? If
3664 * this is only used for dumps, I don't think
3667 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3670 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3676 * Schedule a peripheral driver to receive a ccb when it's
3677 * target device has space for more transactions.
3680 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3682 struct cam_ed *device;
3683 union ccb *work_ccb;
3687 mtx_assert(perph->sim->mtx, MA_OWNED);
3689 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3690 device = perph->path->device;
3692 if (periph_is_queued(perph)) {
3693 /* Simply reorder based on new priority */
3694 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3695 (" change priority to %d\n", new_priority));
3696 if (new_priority < perph->pinfo.priority) {
3697 camq_change_priority(&device->drvq,
3702 } else if (SIM_DEAD(perph->path->bus->sim)) {
3703 /* The SIM is gone so just call periph_start directly. */
3704 work_ccb = xpt_get_ccb(perph->path->device);
3706 if (work_ccb == NULL)
3708 xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority);
3709 perph->pinfo.priority = new_priority;
3710 perph->periph_start(perph, work_ccb);
3713 /* New entry on the queue */
3714 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3715 (" added periph to queue\n"));
3716 perph->pinfo.priority = new_priority;
3717 perph->pinfo.generation = ++device->drvq.generation;
3718 camq_insert(&device->drvq, &perph->pinfo);
3719 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3723 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3724 (" calling xpt_run_devq\n"));
3725 xpt_run_dev_allocq(perph->path->bus);
3731 * Schedule a device to run on a given queue.
3732 * If the device was inserted as a new entry on the queue,
3733 * return 1 meaning the device queue should be run. If we
3734 * were already queued, implying someone else has already
3735 * started the queue, return 0 so the caller doesn't attempt
3736 * to run the queue. Must be run at either splsoftcam
3737 * (or splcam since that encompases splsoftcam).
3740 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3741 u_int32_t new_priority)
3744 u_int32_t old_priority;
3746 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3748 old_priority = pinfo->priority;
3751 * Are we already queued?
3753 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3754 /* Simply reorder based on new priority */
3755 if (new_priority < old_priority) {
3756 camq_change_priority(queue, pinfo->index,
3758 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3759 ("changed priority to %d\n",
3764 /* New entry on the queue */
3765 if (new_priority < old_priority)
3766 pinfo->priority = new_priority;
3768 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3769 ("Inserting onto queue\n"));
3770 pinfo->generation = ++queue->generation;
3771 camq_insert(queue, pinfo);
3778 xpt_run_dev_allocq(struct cam_eb *bus)
3780 struct cam_devq *devq;
3783 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3784 devq = bus->sim->devq;
3786 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3787 (" qfrozen_cnt == 0x%x, entries == %d, "
3788 "openings == %d, active == %d\n",
3789 devq->alloc_queue.qfrozen_cnt,
3790 devq->alloc_queue.entries,
3791 devq->alloc_openings,
3792 devq->alloc_active));
3795 devq->alloc_queue.qfrozen_cnt++;
3796 while ((devq->alloc_queue.entries > 0)
3797 && (devq->alloc_openings > 0)
3798 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3799 struct cam_ed_qinfo *qinfo;
3800 struct cam_ed *device;
3801 union ccb *work_ccb;
3802 struct cam_periph *drv;
3805 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3807 device = qinfo->device;
3809 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3810 ("running device %p\n", device));
3812 drvq = &device->drvq;
3815 if (drvq->entries <= 0) {
3816 panic("xpt_run_dev_allocq: "
3817 "Device on queue without any work to do");
3820 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3821 devq->alloc_openings--;
3822 devq->alloc_active++;
3823 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3825 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3826 drv->pinfo.priority);
3827 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3828 ("calling periph start\n"));
3829 drv->periph_start(drv, work_ccb);
3832 * Malloc failure in alloc_ccb
3835 * XXX add us to a list to be run from free_ccb
3836 * if we don't have any ccbs active on this
3837 * device queue otherwise we may never get run
3843 /* Raise IPL for possible insertion and test at top of loop */
3846 if (drvq->entries > 0) {
3847 /* We have more work. Attempt to reschedule */
3848 xpt_schedule_dev_allocq(bus, device);
3851 devq->alloc_queue.qfrozen_cnt--;
3856 xpt_run_dev_sendq(struct cam_eb *bus)
3858 struct cam_devq *devq;
3861 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3863 devq = bus->sim->devq;
3866 devq->send_queue.qfrozen_cnt++;
3869 while ((devq->send_queue.entries > 0)
3870 && (devq->send_openings > 0)) {
3871 struct cam_ed_qinfo *qinfo;
3872 struct cam_ed *device;
3873 union ccb *work_ccb;
3874 struct cam_sim *sim;
3878 if (devq->send_queue.qfrozen_cnt > 1) {
3883 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3885 device = qinfo->device;
3888 * If the device has been "frozen", don't attempt
3891 if (device->qfrozen_cnt > 0) {
3896 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3897 ("running device %p\n", device));
3899 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3900 if (work_ccb == NULL) {
3901 printf("device on run queue with no ccbs???\n");
3906 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3908 mtx_lock(&xsoftc.xpt_lock);
3909 if (xsoftc.num_highpower <= 0) {
3911 * We got a high power command, but we
3912 * don't have any available slots. Freeze
3913 * the device queue until we have a slot
3916 device->qfrozen_cnt++;
3917 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3925 * Consume a high power slot while
3928 xsoftc.num_highpower--;
3930 mtx_unlock(&xsoftc.xpt_lock);
3932 devq->active_dev = device;
3933 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3935 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3938 devq->send_openings--;
3939 devq->send_active++;
3941 if (device->ccbq.queue.entries > 0)
3942 xpt_schedule_dev_sendq(bus, device);
3944 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3946 * The client wants to freeze the queue
3947 * after this CCB is sent.
3950 device->qfrozen_cnt++;
3956 /* In Target mode, the peripheral driver knows best... */
3957 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3958 if ((device->inq_flags & SID_CmdQue) != 0
3959 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3960 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3963 * Clear this in case of a retried CCB that
3964 * failed due to a rejected tag.
3966 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3970 * Device queues can be shared among multiple sim instances
3971 * that reside on different busses. Use the SIM in the queue
3972 * CCB's path, rather than the one in the bus that was passed
3973 * into this function.
3975 sim = work_ccb->ccb_h.path->bus->sim;
3976 (*(sim->sim_action))(sim, work_ccb);
3979 devq->active_dev = NULL;
3981 /* Raise IPL for possible insertion and test at top of loop */
3986 devq->send_queue.qfrozen_cnt--;
3991 * This function merges stuff from the slave ccb into the master ccb, while
3992 * keeping important fields in the master ccb constant.
3995 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3999 * Pull fields that are valid for peripheral drivers to set
4000 * into the master CCB along with the CCB "payload".
4002 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
4003 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
4004 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
4005 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
4006 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
4007 sizeof(union ccb) - sizeof(struct ccb_hdr));
4011 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
4014 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
4015 ccb_h->pinfo.priority = priority;
4017 ccb_h->path_id = path->bus->path_id;
4019 ccb_h->target_id = path->target->target_id;
4021 ccb_h->target_id = CAM_TARGET_WILDCARD;
4023 ccb_h->target_lun = path->device->lun_id;
4024 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4026 ccb_h->target_lun = CAM_TARGET_WILDCARD;
4028 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4032 /* Path manipulation functions */
4034 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4035 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4037 struct cam_path *path;
4040 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
4043 status = CAM_RESRC_UNAVAIL;
4046 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4047 if (status != CAM_REQ_CMP) {
4048 free(path, M_CAMXPT);
4051 *new_path_ptr = path;
4056 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
4057 struct cam_periph *periph, path_id_t path_id,
4058 target_id_t target_id, lun_id_t lun_id)
4060 struct cam_path *path;
4061 struct cam_eb *bus = NULL;
4063 int need_unlock = 0;
4065 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
4067 if (path_id != CAM_BUS_WILDCARD) {
4068 bus = xpt_find_bus(path_id);
4071 CAM_SIM_LOCK(bus->sim);
4074 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
4076 CAM_SIM_UNLOCK(bus->sim);
4077 if (status != CAM_REQ_CMP) {
4078 free(path, M_CAMXPT);
4081 *new_path_ptr = path;
4086 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4087 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4090 struct cam_et *target;
4091 struct cam_ed *device;
4095 status = CAM_REQ_CMP; /* Completed without error */
4096 target = NULL; /* Wildcarded */
4097 device = NULL; /* Wildcarded */
4100 * We will potentially modify the EDT, so block interrupts
4101 * that may attempt to create cam paths.
4104 bus = xpt_find_bus(path_id);
4106 status = CAM_PATH_INVALID;
4108 target = xpt_find_target(bus, target_id);
4109 if (target == NULL) {
4111 struct cam_et *new_target;
4113 new_target = xpt_alloc_target(bus, target_id);
4114 if (new_target == NULL) {
4115 status = CAM_RESRC_UNAVAIL;
4117 target = new_target;
4120 if (target != NULL) {
4121 device = xpt_find_device(target, lun_id);
4122 if (device == NULL) {
4124 struct cam_ed *new_device;
4126 new_device = xpt_alloc_device(bus,
4129 if (new_device == NULL) {
4130 status = CAM_RESRC_UNAVAIL;
4132 device = new_device;
4140 * Only touch the user's data if we are successful.
4142 if (status == CAM_REQ_CMP) {
4143 new_path->periph = perph;
4144 new_path->bus = bus;
4145 new_path->target = target;
4146 new_path->device = device;
4147 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4150 xpt_release_device(bus, target, device);
4152 xpt_release_target(bus, target);
4154 xpt_release_bus(bus);
4160 xpt_release_path(struct cam_path *path)
4162 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4163 if (path->device != NULL) {
4164 xpt_release_device(path->bus, path->target, path->device);
4165 path->device = NULL;
4167 if (path->target != NULL) {
4168 xpt_release_target(path->bus, path->target);
4169 path->target = NULL;
4171 if (path->bus != NULL) {
4172 xpt_release_bus(path->bus);
4178 xpt_free_path(struct cam_path *path)
4181 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4182 xpt_release_path(path);
4183 free(path, M_CAMXPT);
4188 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4189 * in path1, 2 for match with wildcards in path2.
4192 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4196 if (path1->bus != path2->bus) {
4197 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4199 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4204 if (path1->target != path2->target) {
4205 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4208 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4213 if (path1->device != path2->device) {
4214 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4217 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4226 xpt_print_path(struct cam_path *path)
4228 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4231 printf("(nopath): ");
4233 if (path->periph != NULL)
4234 printf("(%s%d:", path->periph->periph_name,
4235 path->periph->unit_number);
4237 printf("(noperiph:");
4239 if (path->bus != NULL)
4240 printf("%s%d:%d:", path->bus->sim->sim_name,
4241 path->bus->sim->unit_number,
4242 path->bus->sim->bus_id);
4246 if (path->target != NULL)
4247 printf("%d:", path->target->target_id);
4251 if (path->device != NULL)
4252 printf("%d): ", path->device->lun_id);
4259 xpt_print(struct cam_path *path, const char *fmt, ...)
4262 xpt_print_path(path);
4269 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4273 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4275 sbuf_new(&sb, str, str_len, 0);
4278 sbuf_printf(&sb, "(nopath): ");
4280 if (path->periph != NULL)
4281 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4282 path->periph->unit_number);
4284 sbuf_printf(&sb, "(noperiph:");
4286 if (path->bus != NULL)
4287 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4288 path->bus->sim->unit_number,
4289 path->bus->sim->bus_id);
4291 sbuf_printf(&sb, "nobus:");
4293 if (path->target != NULL)
4294 sbuf_printf(&sb, "%d:", path->target->target_id);
4296 sbuf_printf(&sb, "X:");
4298 if (path->device != NULL)
4299 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4301 sbuf_printf(&sb, "X): ");
4305 return(sbuf_len(&sb));
4309 xpt_path_path_id(struct cam_path *path)
4311 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4313 return(path->bus->path_id);
4317 xpt_path_target_id(struct cam_path *path)
4319 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4321 if (path->target != NULL)
4322 return (path->target->target_id);
4324 return (CAM_TARGET_WILDCARD);
4328 xpt_path_lun_id(struct cam_path *path)
4330 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4332 if (path->device != NULL)
4333 return (path->device->lun_id);
4335 return (CAM_LUN_WILDCARD);
4339 xpt_path_sim(struct cam_path *path)
4342 return (path->bus->sim);
4346 xpt_path_periph(struct cam_path *path)
4348 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4350 return (path->periph);
4354 * Release a CAM control block for the caller. Remit the cost of the structure
4355 * to the device referenced by the path. If the this device had no 'credits'
4356 * and peripheral drivers have registered async callbacks for this notification
4360 xpt_release_ccb(union ccb *free_ccb)
4363 struct cam_path *path;
4364 struct cam_ed *device;
4366 struct cam_sim *sim;
4368 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4369 path = free_ccb->ccb_h.path;
4370 device = path->device;
4375 mtx_assert(sim->mtx, MA_OWNED);
4377 cam_ccbq_release_opening(&device->ccbq);
4378 if (sim->ccb_count > sim->max_ccbs) {
4379 xpt_free_ccb(free_ccb);
4382 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
4385 if (sim->devq == NULL) {
4389 sim->devq->alloc_openings++;
4390 sim->devq->alloc_active--;
4391 /* XXX Turn this into an inline function - xpt_run_device?? */
4392 if ((device_is_alloc_queued(device) == 0)
4393 && (device->drvq.entries > 0)) {
4394 xpt_schedule_dev_allocq(bus, device);
4397 if (dev_allocq_is_runnable(sim->devq))
4398 xpt_run_dev_allocq(bus);
4401 /* Functions accessed by SIM drivers */
4404 * A sim structure, listing the SIM entry points and instance
4405 * identification info is passed to xpt_bus_register to hook the SIM
4406 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4407 * for this new bus and places it in the array of busses and assigns
4408 * it a path_id. The path_id may be influenced by "hard wiring"
4409 * information specified by the user. Once interrupt services are
4410 * availible, the bus will be probed.
4413 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4415 struct cam_eb *new_bus;
4416 struct cam_eb *old_bus;
4417 struct ccb_pathinq cpi;
4420 mtx_assert(sim->mtx, MA_OWNED);
4423 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4424 M_CAMXPT, M_NOWAIT);
4425 if (new_bus == NULL) {
4426 /* Couldn't satisfy request */
4427 return (CAM_RESRC_UNAVAIL);
4430 if (strcmp(sim->sim_name, "xpt") != 0) {
4433 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4436 TAILQ_INIT(&new_bus->et_entries);
4437 new_bus->path_id = sim->path_id;
4439 timevalclear(&new_bus->last_reset);
4441 new_bus->refcount = 1; /* Held until a bus_deregister event */
4442 new_bus->generation = 0;
4444 mtx_lock(&xsoftc.xpt_topo_lock);
4445 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4446 while (old_bus != NULL
4447 && old_bus->path_id < new_bus->path_id)
4448 old_bus = TAILQ_NEXT(old_bus, links);
4449 if (old_bus != NULL)
4450 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4452 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
4453 xsoftc.bus_generation++;
4454 mtx_unlock(&xsoftc.xpt_topo_lock);
4457 /* Notify interested parties */
4458 if (sim->path_id != CAM_XPT_PATH_ID) {
4459 struct cam_path path;
4461 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4462 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4463 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4464 cpi.ccb_h.func_code = XPT_PATH_INQ;
4465 xpt_action((union ccb *)&cpi);
4466 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4467 xpt_release_path(&path);
4469 return (CAM_SUCCESS);
4473 xpt_bus_deregister(path_id_t pathid)
4475 struct cam_path bus_path;
4476 struct cam_ed *device;
4477 struct cam_ed_qinfo *qinfo;
4478 struct cam_devq *devq;
4479 struct cam_periph *periph;
4480 struct cam_sim *ccbsim;
4481 union ccb *work_ccb;
4485 status = xpt_compile_path(&bus_path, NULL, pathid,
4486 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4487 if (status != CAM_REQ_CMP)
4490 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4491 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4493 /* The SIM may be gone, so use a dummy SIM for any stray operations. */
4494 devq = bus_path.bus->sim->devq;
4495 ccbsim = bus_path.bus->sim;
4496 bus_path.bus->sim = &cam_dead_sim;
4498 /* Execute any pending operations now. */
4499 while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
4500 CAMQ_HEAD)) != NULL ||
4501 (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
4502 CAMQ_HEAD)) != NULL) {
4504 device = qinfo->device;
4505 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
4506 if (work_ccb != NULL) {
4507 devq->active_dev = device;
4508 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
4509 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
4510 (*(ccbsim->sim_action))(ccbsim, work_ccb);
4513 periph = (struct cam_periph *)camq_remove(&device->drvq,
4516 xpt_schedule(periph, periph->pinfo.priority);
4517 } while (work_ccb != NULL || periph != NULL);
4520 /* Make sure all completed CCBs are processed. */
4521 while (!TAILQ_EMPTY(&ccbsim->sim_doneq)) {
4522 camisr_runqueue(&ccbsim->sim_doneq);
4524 /* Repeat the async's for the benefit of any new devices. */
4525 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4526 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4529 /* Release the reference count held while registered. */
4530 xpt_release_bus(bus_path.bus);
4531 xpt_release_path(&bus_path);
4533 return (CAM_REQ_CMP);
4537 xptnextfreepathid(void)
4544 mtx_lock(&xsoftc.xpt_topo_lock);
4545 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4547 /* Find an unoccupied pathid */
4548 while (bus != NULL && bus->path_id <= pathid) {
4549 if (bus->path_id == pathid)
4551 bus = TAILQ_NEXT(bus, links);
4553 mtx_unlock(&xsoftc.xpt_topo_lock);
4556 * Ensure that this pathid is not reserved for
4557 * a bus that may be registered in the future.
4559 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4561 /* Start the search over */
4562 mtx_lock(&xsoftc.xpt_topo_lock);
4569 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4576 pathid = CAM_XPT_PATH_ID;
4577 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4579 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4580 if (strcmp(dname, "scbus")) {
4581 /* Avoid a bit of foot shooting. */
4584 if (dunit < 0) /* unwired?! */
4586 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4587 if (sim_bus == val) {
4591 } else if (sim_bus == 0) {
4592 /* Unspecified matches bus 0 */
4596 printf("Ambiguous scbus configuration for %s%d "
4597 "bus %d, cannot wire down. The kernel "
4598 "config entry for scbus%d should "
4599 "specify a controller bus.\n"
4600 "Scbus will be assigned dynamically.\n",
4601 sim_name, sim_unit, sim_bus, dunit);
4606 if (pathid == CAM_XPT_PATH_ID)
4607 pathid = xptnextfreepathid();
4612 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4615 struct cam_et *target, *next_target;
4616 struct cam_ed *device, *next_device;
4619 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4621 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4624 * Most async events come from a CAM interrupt context. In
4625 * a few cases, the error recovery code at the peripheral layer,
4626 * which may run from our SWI or a process context, may signal
4627 * deferred events with a call to xpt_async. Ensure async
4628 * notifications are serialized by blocking cam interrupts.
4634 if (async_code == AC_BUS_RESET) {
4638 /* Update our notion of when the last reset occurred */
4639 microtime(&bus->last_reset);
4643 for (target = TAILQ_FIRST(&bus->et_entries);
4645 target = next_target) {
4647 next_target = TAILQ_NEXT(target, links);
4649 if (path->target != target
4650 && path->target->target_id != CAM_TARGET_WILDCARD
4651 && target->target_id != CAM_TARGET_WILDCARD)
4654 if (async_code == AC_SENT_BDR) {
4657 /* Update our notion of when the last reset occurred */
4659 microtime(&path->target->last_reset);
4663 for (device = TAILQ_FIRST(&target->ed_entries);
4665 device = next_device) {
4667 next_device = TAILQ_NEXT(device, links);
4669 if (path->device != device
4670 && path->device->lun_id != CAM_LUN_WILDCARD
4671 && device->lun_id != CAM_LUN_WILDCARD)
4674 xpt_dev_async(async_code, bus, target,
4677 xpt_async_bcast(&device->asyncs, async_code,
4683 * If this wasn't a fully wildcarded async, tell all
4684 * clients that want all async events.
4686 if (bus != xpt_periph->path->bus)
4687 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4693 xpt_async_bcast(struct async_list *async_head,
4694 u_int32_t async_code,
4695 struct cam_path *path, void *async_arg)
4697 struct async_node *cur_entry;
4699 cur_entry = SLIST_FIRST(async_head);
4700 while (cur_entry != NULL) {
4701 struct async_node *next_entry;
4703 * Grab the next list entry before we call the current
4704 * entry's callback. This is because the callback function
4705 * can delete its async callback entry.
4707 next_entry = SLIST_NEXT(cur_entry, links);
4708 if ((cur_entry->event_enable & async_code) != 0)
4709 cur_entry->callback(cur_entry->callback_arg,
4712 cur_entry = next_entry;
4717 * Handle any per-device event notifications that require action by the XPT.
4720 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4721 struct cam_ed *device, void *async_arg)
4724 struct cam_path newpath;
4727 * We only need to handle events for real devices.
4729 if (target->target_id == CAM_TARGET_WILDCARD
4730 || device->lun_id == CAM_LUN_WILDCARD)
4734 * We need our own path with wildcards expanded to
4735 * handle certain types of events.
4737 if ((async_code == AC_SENT_BDR)
4738 || (async_code == AC_BUS_RESET)
4739 || (async_code == AC_INQ_CHANGED))
4740 status = xpt_compile_path(&newpath, NULL,
4745 status = CAM_REQ_CMP_ERR;
4747 if (status == CAM_REQ_CMP) {
4750 * Allow transfer negotiation to occur in a
4751 * tag free environment.
4753 if (async_code == AC_SENT_BDR
4754 || async_code == AC_BUS_RESET)
4755 xpt_toggle_tags(&newpath);
4757 if (async_code == AC_INQ_CHANGED) {
4759 * We've sent a start unit command, or
4760 * something similar to a device that
4761 * may have caused its inquiry data to
4762 * change. So we re-scan the device to
4763 * refresh the inquiry data for it.
4765 xpt_scan_lun(newpath.periph, &newpath,
4766 CAM_EXPECT_INQ_CHANGE, NULL);
4768 xpt_release_path(&newpath);
4769 } else if (async_code == AC_LOST_DEVICE) {
4770 device->flags |= CAM_DEV_UNCONFIGURED;
4771 } else if (async_code == AC_TRANSFER_NEG) {
4772 struct ccb_trans_settings *settings;
4774 settings = (struct ccb_trans_settings *)async_arg;
4775 xpt_set_transfer_settings(settings, device,
4776 /*async_update*/TRUE);
4781 xpt_freeze_devq(struct cam_path *path, u_int count)
4784 struct ccb_hdr *ccbh;
4786 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4789 path->device->qfrozen_cnt += count;
4792 * Mark the last CCB in the queue as needing
4793 * to be requeued if the driver hasn't
4794 * changed it's state yet. This fixes a race
4795 * where a ccb is just about to be queued to
4796 * a controller driver when it's interrupt routine
4797 * freezes the queue. To completly close the
4798 * hole, controller drives must check to see
4799 * if a ccb's status is still CAM_REQ_INPROG
4800 * under spl protection just before they queue
4801 * the CCB. See ahc_action/ahc_freeze_devq for
4804 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4805 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4806 ccbh->status = CAM_REQUEUE_REQ;
4808 return (path->device->qfrozen_cnt);
4812 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4814 mtx_assert(sim->mtx, MA_OWNED);
4816 sim->devq->send_queue.qfrozen_cnt += count;
4817 if (sim->devq->active_dev != NULL) {
4818 struct ccb_hdr *ccbh;
4820 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4822 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4823 ccbh->status = CAM_REQUEUE_REQ;
4825 return (sim->devq->send_queue.qfrozen_cnt);
4829 xpt_release_devq_timeout(void *arg)
4831 struct cam_ed *device;
4833 device = (struct cam_ed *)arg;
4835 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4839 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4841 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4843 xpt_release_devq_device(path->device, count, run_queue);
4847 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4855 if (dev->qfrozen_cnt > 0) {
4857 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4858 dev->qfrozen_cnt -= count;
4859 if (dev->qfrozen_cnt == 0) {
4862 * No longer need to wait for a successful
4863 * command completion.
4865 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4868 * Remove any timeouts that might be scheduled
4869 * to release this queue.
4871 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4872 callout_stop(&dev->callout);
4873 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4877 * Now that we are unfrozen schedule the
4878 * device so any pending transactions are
4881 if ((dev->ccbq.queue.entries > 0)
4882 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4883 && (run_queue != 0)) {
4890 xpt_run_dev_sendq(dev->target->bus);
4895 xpt_release_simq(struct cam_sim *sim, int run_queue)
4900 mtx_assert(sim->mtx, MA_OWNED);
4902 sendq = &(sim->devq->send_queue);
4904 if (sendq->qfrozen_cnt > 0) {
4906 sendq->qfrozen_cnt--;
4907 if (sendq->qfrozen_cnt == 0) {
4911 * If there is a timeout scheduled to release this
4912 * sim queue, remove it. The queue frozen count is
4915 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4916 callout_stop(&sim->callout);
4917 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4919 bus = xpt_find_bus(sim->path_id);
4924 * Now that we are unfrozen run the send queue.
4926 xpt_run_dev_sendq(bus);
4928 xpt_release_bus(bus);
4936 * XXX Appears to be unused.
4939 xpt_release_simq_timeout(void *arg)
4941 struct cam_sim *sim;
4943 sim = (struct cam_sim *)arg;
4944 xpt_release_simq(sim, /* run_queue */ TRUE);
4948 xpt_done(union ccb *done_ccb)
4950 struct cam_sim *sim;
4955 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4956 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4958 * Queue up the request for handling by our SWI handler
4959 * any of the "non-immediate" type of ccbs.
4961 sim = done_ccb->ccb_h.path->bus->sim;
4962 switch (done_ccb->ccb_h.path->periph->type) {
4963 case CAM_PERIPH_BIO:
4964 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4966 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4967 if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) {
4968 mtx_lock(&cam_simq_lock);
4969 TAILQ_INSERT_TAIL(&cam_simq, sim,
4971 sim->flags |= CAM_SIM_ON_DONEQ;
4972 mtx_unlock(&cam_simq_lock);
4974 if ((done_ccb->ccb_h.path->periph->flags &
4975 CAM_PERIPH_POLLED) == 0)
4976 swi_sched(cambio_ih, 0);
4979 panic("unknown periph type %d",
4980 done_ccb->ccb_h.path->periph->type);
4991 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4996 xpt_alloc_ccb_nowait()
5000 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
5005 xpt_free_ccb(union ccb *free_ccb)
5007 free(free_ccb, M_CAMXPT);
5012 /* Private XPT functions */
5015 * Get a CAM control block for the caller. Charge the structure to the device
5016 * referenced by the path. If the this device has no 'credits' then the
5017 * device already has the maximum number of outstanding operations under way
5018 * and we return NULL. If we don't have sufficient resources to allocate more
5019 * ccbs, we also return NULL.
5022 xpt_get_ccb(struct cam_ed *device)
5025 struct cam_sim *sim;
5030 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
5031 new_ccb = xpt_alloc_ccb_nowait();
5032 if (new_ccb == NULL) {
5036 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
5037 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
5038 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
5042 cam_ccbq_take_opening(&device->ccbq);
5043 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
5049 xpt_release_bus(struct cam_eb *bus)
5054 if ((--bus->refcount == 0)
5055 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
5056 mtx_lock(&xsoftc.xpt_topo_lock);
5057 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
5058 xsoftc.bus_generation++;
5059 mtx_unlock(&xsoftc.xpt_topo_lock);
5061 free(bus, M_CAMXPT);
5066 static struct cam_et *
5067 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
5069 struct cam_et *target;
5071 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
5072 if (target != NULL) {
5073 struct cam_et *cur_target;
5075 TAILQ_INIT(&target->ed_entries);
5077 target->target_id = target_id;
5078 target->refcount = 1;
5079 target->generation = 0;
5080 timevalclear(&target->last_reset);
5082 * Hold a reference to our parent bus so it
5083 * will not go away before we do.
5087 /* Insertion sort into our bus's target list */
5088 cur_target = TAILQ_FIRST(&bus->et_entries);
5089 while (cur_target != NULL && cur_target->target_id < target_id)
5090 cur_target = TAILQ_NEXT(cur_target, links);
5092 if (cur_target != NULL) {
5093 TAILQ_INSERT_BEFORE(cur_target, target, links);
5095 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
5103 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
5108 if ((--target->refcount == 0)
5109 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
5110 TAILQ_REMOVE(&bus->et_entries, target, links);
5113 free(target, M_CAMXPT);
5114 xpt_release_bus(bus);
5119 static struct cam_ed *
5120 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5122 struct cam_path path;
5123 struct cam_ed *device;
5124 struct cam_devq *devq;
5127 if (SIM_DEAD(bus->sim))
5130 /* Make space for us in the device queue on our bus */
5131 devq = bus->sim->devq;
5132 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5134 if (status != CAM_REQ_CMP) {
5137 device = (struct cam_ed *)malloc(sizeof(*device),
5138 M_CAMXPT, M_NOWAIT);
5141 if (device != NULL) {
5142 struct cam_ed *cur_device;
5144 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5145 device->alloc_ccb_entry.device = device;
5146 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5147 device->send_ccb_entry.device = device;
5148 device->target = target;
5149 device->lun_id = lun_id;
5150 device->sim = bus->sim;
5151 /* Initialize our queues */
5152 if (camq_init(&device->drvq, 0) != 0) {
5153 free(device, M_CAMXPT);
5156 if (cam_ccbq_init(&device->ccbq,
5157 bus->sim->max_dev_openings) != 0) {
5158 camq_fini(&device->drvq);
5159 free(device, M_CAMXPT);
5162 SLIST_INIT(&device->asyncs);
5163 SLIST_INIT(&device->periphs);
5164 device->generation = 0;
5165 device->owner = NULL;
5167 * Take the default quirk entry until we have inquiry
5168 * data and can determine a better quirk to use.
5170 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5171 bzero(&device->inq_data, sizeof(device->inq_data));
5172 device->inq_flags = 0;
5173 device->queue_flags = 0;
5174 device->serial_num = NULL;
5175 device->serial_num_len = 0;
5176 device->qfrozen_cnt = 0;
5177 device->flags = CAM_DEV_UNCONFIGURED;
5178 device->tag_delay_count = 0;
5179 device->tag_saved_openings = 0;
5180 device->refcount = 1;
5181 if (bus->sim->flags & CAM_SIM_MPSAFE)
5182 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
5184 callout_init_mtx(&device->callout, &Giant, 0);
5187 * Hold a reference to our parent target so it
5188 * will not go away before we do.
5193 * XXX should be limited by number of CCBs this bus can
5196 bus->sim->max_ccbs += device->ccbq.devq_openings;
5197 /* Insertion sort into our target's device list */
5198 cur_device = TAILQ_FIRST(&target->ed_entries);
5199 while (cur_device != NULL && cur_device->lun_id < lun_id)
5200 cur_device = TAILQ_NEXT(cur_device, links);
5201 if (cur_device != NULL) {
5202 TAILQ_INSERT_BEFORE(cur_device, device, links);
5204 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5206 target->generation++;
5207 if (lun_id != CAM_LUN_WILDCARD) {
5208 xpt_compile_path(&path,
5213 xpt_devise_transport(&path);
5214 xpt_release_path(&path);
5221 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5222 struct cam_ed *device)
5227 if ((--device->refcount == 0)
5228 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5229 struct cam_devq *devq;
5231 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5232 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5233 panic("Removing device while still queued for ccbs");
5235 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5236 callout_stop(&device->callout);
5238 TAILQ_REMOVE(&target->ed_entries, device,links);
5239 target->generation++;
5240 bus->sim->max_ccbs -= device->ccbq.devq_openings;
5241 if (!SIM_DEAD(bus->sim)) {
5242 /* Release our slot in the devq */
5243 devq = bus->sim->devq;
5244 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5247 camq_fini(&device->drvq);
5248 camq_fini(&device->ccbq.queue);
5249 free(device, M_CAMXPT);
5250 xpt_release_target(bus, target);
5256 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5266 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5267 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5268 if (result == CAM_REQ_CMP && (diff < 0)) {
5269 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5271 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5272 || (dev->inq_flags & SID_CmdQue) != 0)
5273 dev->tag_saved_openings = newopenings;
5274 /* Adjust the global limit */
5275 dev->sim->max_ccbs += diff;
5280 static struct cam_eb *
5281 xpt_find_bus(path_id_t path_id)
5285 mtx_lock(&xsoftc.xpt_topo_lock);
5286 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
5288 bus = TAILQ_NEXT(bus, links)) {
5289 if (bus->path_id == path_id) {
5294 mtx_unlock(&xsoftc.xpt_topo_lock);
5298 static struct cam_et *
5299 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5301 struct cam_et *target;
5303 for (target = TAILQ_FIRST(&bus->et_entries);
5305 target = TAILQ_NEXT(target, links)) {
5306 if (target->target_id == target_id) {
5314 static struct cam_ed *
5315 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5317 struct cam_ed *device;
5319 for (device = TAILQ_FIRST(&target->ed_entries);
5321 device = TAILQ_NEXT(device, links)) {
5322 if (device->lun_id == lun_id) {
5331 union ccb *request_ccb;
5332 struct ccb_pathinq *cpi;
5334 } xpt_scan_bus_info;
5337 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5338 * As the scan progresses, xpt_scan_bus is used as the
5339 * callback on completion function.
5342 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5344 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5345 ("xpt_scan_bus\n"));
5346 switch (request_ccb->ccb_h.func_code) {
5349 xpt_scan_bus_info *scan_info;
5350 union ccb *work_ccb;
5351 struct cam_path *path;
5356 /* Find out the characteristics of the bus */
5357 work_ccb = xpt_alloc_ccb_nowait();
5358 if (work_ccb == NULL) {
5359 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
5360 xpt_done(request_ccb);
5363 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5364 request_ccb->ccb_h.pinfo.priority);
5365 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5366 xpt_action(work_ccb);
5367 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5368 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5369 xpt_free_ccb(work_ccb);
5370 xpt_done(request_ccb);
5374 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5376 * Can't scan the bus on an adapter that
5377 * cannot perform the initiator role.
5379 request_ccb->ccb_h.status = CAM_REQ_CMP;
5380 xpt_free_ccb(work_ccb);
5381 xpt_done(request_ccb);
5385 /* Save some state for use while we probe for devices */
5386 scan_info = (xpt_scan_bus_info *)
5387 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_NOWAIT);
5388 scan_info->request_ccb = request_ccb;
5389 scan_info->cpi = &work_ccb->cpi;
5391 /* Cache on our stack so we can work asynchronously */
5392 max_target = scan_info->cpi->max_target;
5393 initiator_id = scan_info->cpi->initiator_id;
5397 * We can scan all targets in parallel, or do it sequentially.
5399 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5401 scan_info->counter = 0;
5403 scan_info->counter = scan_info->cpi->max_target + 1;
5404 if (scan_info->cpi->initiator_id < scan_info->counter) {
5405 scan_info->counter--;
5409 for (i = 0; i <= max_target; i++) {
5411 if (i == initiator_id)
5414 status = xpt_create_path(&path, xpt_periph,
5415 request_ccb->ccb_h.path_id,
5417 if (status != CAM_REQ_CMP) {
5418 printf("xpt_scan_bus: xpt_create_path failed"
5419 " with status %#x, bus scan halted\n",
5421 free(scan_info, M_TEMP);
5422 request_ccb->ccb_h.status = status;
5423 xpt_free_ccb(work_ccb);
5424 xpt_done(request_ccb);
5427 work_ccb = xpt_alloc_ccb_nowait();
5428 if (work_ccb == NULL) {
5429 free(scan_info, M_TEMP);
5430 xpt_free_path(path);
5431 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
5432 xpt_done(request_ccb);
5435 xpt_setup_ccb(&work_ccb->ccb_h, path,
5436 request_ccb->ccb_h.pinfo.priority);
5437 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5438 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5439 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5440 work_ccb->crcn.flags = request_ccb->crcn.flags;
5441 xpt_action(work_ccb);
5448 struct cam_path *path;
5449 xpt_scan_bus_info *scan_info;
5451 target_id_t target_id;
5454 /* Reuse the same CCB to query if a device was really found */
5455 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5456 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5457 request_ccb->ccb_h.pinfo.priority);
5458 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5460 path_id = request_ccb->ccb_h.path_id;
5461 target_id = request_ccb->ccb_h.target_id;
5462 lun_id = request_ccb->ccb_h.target_lun;
5463 xpt_action(request_ccb);
5465 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5466 struct cam_ed *device;
5467 struct cam_et *target;
5471 * If we already probed lun 0 successfully, or
5472 * we have additional configured luns on this
5473 * target that might have "gone away", go onto
5476 target = request_ccb->ccb_h.path->target;
5478 * We may touch devices that we don't
5479 * hold references too, so ensure they
5480 * don't disappear out from under us.
5481 * The target above is referenced by the
5482 * path in the request ccb.
5486 device = TAILQ_FIRST(&target->ed_entries);
5487 if (device != NULL) {
5488 phl = CAN_SRCH_HI_SPARSE(device);
5489 if (device->lun_id == 0)
5490 device = TAILQ_NEXT(device, links);
5493 if ((lun_id != 0) || (device != NULL)) {
5494 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5498 struct cam_ed *device;
5500 device = request_ccb->ccb_h.path->device;
5502 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5503 /* Try the next lun */
5504 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5505 || CAN_SRCH_HI_DENSE(device))
5511 * Free the current request path- we're done with it.
5513 xpt_free_path(request_ccb->ccb_h.path);
5516 * Check to see if we scan any further luns.
5518 if (lun_id == request_ccb->ccb_h.target_lun
5519 || lun_id > scan_info->cpi->max_lun) {
5524 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5525 scan_info->counter++;
5526 if (scan_info->counter ==
5527 scan_info->cpi->initiator_id) {
5528 scan_info->counter++;
5530 if (scan_info->counter >=
5531 scan_info->cpi->max_target+1) {
5535 scan_info->counter--;
5536 if (scan_info->counter == 0) {
5541 xpt_free_ccb(request_ccb);
5542 xpt_free_ccb((union ccb *)scan_info->cpi);
5543 request_ccb = scan_info->request_ccb;
5544 free(scan_info, M_TEMP);
5545 request_ccb->ccb_h.status = CAM_REQ_CMP;
5546 xpt_done(request_ccb);
5550 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5553 status = xpt_create_path(&path, xpt_periph,
5554 scan_info->request_ccb->ccb_h.path_id,
5555 scan_info->counter, 0);
5556 if (status != CAM_REQ_CMP) {
5557 printf("xpt_scan_bus: xpt_create_path failed"
5558 " with status %#x, bus scan halted\n",
5560 xpt_free_ccb(request_ccb);
5561 xpt_free_ccb((union ccb *)scan_info->cpi);
5562 request_ccb = scan_info->request_ccb;
5563 free(scan_info, M_TEMP);
5564 request_ccb->ccb_h.status = status;
5565 xpt_done(request_ccb);
5568 xpt_setup_ccb(&request_ccb->ccb_h, path,
5569 request_ccb->ccb_h.pinfo.priority);
5570 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5571 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5572 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5573 request_ccb->crcn.flags =
5574 scan_info->request_ccb->crcn.flags;
5576 status = xpt_create_path(&path, xpt_periph,
5577 path_id, target_id, lun_id);
5578 if (status != CAM_REQ_CMP) {
5579 printf("xpt_scan_bus: xpt_create_path failed "
5580 "with status %#x, halting LUN scan\n",
5584 xpt_setup_ccb(&request_ccb->ccb_h, path,
5585 request_ccb->ccb_h.pinfo.priority);
5586 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5587 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5588 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5589 request_ccb->crcn.flags =
5590 scan_info->request_ccb->crcn.flags;
5592 xpt_action(request_ccb);
5602 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */
5606 PROBE_TUR_FOR_NEGOTIATION,
5607 PROBE_INQUIRY_BASIC_DV1,
5608 PROBE_INQUIRY_BASIC_DV2,
5613 PROBE_INQUIRY_CKSUM = 0x01,
5614 PROBE_SERIAL_CKSUM = 0x02,
5615 PROBE_NO_ANNOUNCE = 0x04
5619 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5620 probe_action action;
5621 union ccb saved_ccb;
5624 u_int8_t digest[16];
5628 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5629 cam_flags flags, union ccb *request_ccb)
5631 struct ccb_pathinq cpi;
5633 struct cam_path *new_path;
5634 struct cam_periph *old_periph;
5637 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5638 ("xpt_scan_lun\n"));
5640 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5641 cpi.ccb_h.func_code = XPT_PATH_INQ;
5642 xpt_action((union ccb *)&cpi);
5644 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5645 if (request_ccb != NULL) {
5646 request_ccb->ccb_h.status = cpi.ccb_h.status;
5647 xpt_done(request_ccb);
5652 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5654 * Can't scan the bus on an adapter that
5655 * cannot perform the initiator role.
5657 if (request_ccb != NULL) {
5658 request_ccb->ccb_h.status = CAM_REQ_CMP;
5659 xpt_done(request_ccb);
5664 if (request_ccb == NULL) {
5665 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5666 if (request_ccb == NULL) {
5667 xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
5668 "can't continue\n");
5671 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5672 if (new_path == NULL) {
5673 xpt_print(path, "xpt_scan_lun: can't allocate path, "
5674 "can't continue\n");
5675 free(request_ccb, M_TEMP);
5678 status = xpt_compile_path(new_path, xpt_periph,
5680 path->target->target_id,
5681 path->device->lun_id);
5683 if (status != CAM_REQ_CMP) {
5684 xpt_print(path, "xpt_scan_lun: can't compile path, "
5685 "can't continue\n");
5686 free(request_ccb, M_TEMP);
5687 free(new_path, M_TEMP);
5690 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5691 request_ccb->ccb_h.cbfcnp = xptscandone;
5692 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5693 request_ccb->crcn.flags = flags;
5697 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5700 softc = (probe_softc *)old_periph->softc;
5701 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5704 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5705 probestart, "probe",
5707 request_ccb->ccb_h.path, NULL, 0,
5710 if (status != CAM_REQ_CMP) {
5711 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
5712 "returned an error, can't continue probe\n");
5713 request_ccb->ccb_h.status = status;
5714 xpt_done(request_ccb);
5721 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5723 xpt_release_path(done_ccb->ccb_h.path);
5724 free(done_ccb->ccb_h.path, M_TEMP);
5725 free(done_ccb, M_TEMP);
5729 proberegister(struct cam_periph *periph, void *arg)
5731 union ccb *request_ccb; /* CCB representing the probe request */
5735 request_ccb = (union ccb *)arg;
5736 if (periph == NULL) {
5737 printf("proberegister: periph was NULL!!\n");
5738 return(CAM_REQ_CMP_ERR);
5741 if (request_ccb == NULL) {
5742 printf("proberegister: no probe CCB, "
5743 "can't register device\n");
5744 return(CAM_REQ_CMP_ERR);
5747 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5749 if (softc == NULL) {
5750 printf("proberegister: Unable to probe new device. "
5751 "Unable to allocate softc\n");
5752 return(CAM_REQ_CMP_ERR);
5754 TAILQ_INIT(&softc->request_ccbs);
5755 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5758 periph->softc = softc;
5759 status = cam_periph_acquire(periph);
5760 if (status != CAM_REQ_CMP) {
5766 * Ensure we've waited at least a bus settle
5767 * delay before attempting to probe the device.
5768 * For HBAs that don't do bus resets, this won't make a difference.
5770 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5772 probeschedule(periph);
5773 return(CAM_REQ_CMP);
5777 probeschedule(struct cam_periph *periph)
5779 struct ccb_pathinq cpi;
5783 softc = (probe_softc *)periph->softc;
5784 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5786 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5787 cpi.ccb_h.func_code = XPT_PATH_INQ;
5788 xpt_action((union ccb *)&cpi);
5791 * If a device has gone away and another device, or the same one,
5792 * is back in the same place, it should have a unit attention
5793 * condition pending. It will not report the unit attention in
5794 * response to an inquiry, which may leave invalid transfer
5795 * negotiations in effect. The TUR will reveal the unit attention
5796 * condition. Only send the TUR for lun 0, since some devices
5797 * will get confused by commands other than inquiry to non-existent
5798 * luns. If you think a device has gone away start your scan from
5799 * lun 0. This will insure that any bogus transfer settings are
5802 * If we haven't seen the device before and the controller supports
5803 * some kind of transfer negotiation, negotiate with the first
5804 * sent command if no bus reset was performed at startup. This
5805 * ensures that the device is not confused by transfer negotiation
5806 * settings left over by loader or BIOS action.
5808 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5809 && (ccb->ccb_h.target_lun == 0)) {
5810 softc->action = PROBE_TUR;
5811 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5812 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5813 proberequestdefaultnegotiation(periph);
5814 softc->action = PROBE_INQUIRY;
5816 softc->action = PROBE_INQUIRY;
5819 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5820 softc->flags |= PROBE_NO_ANNOUNCE;
5822 softc->flags &= ~PROBE_NO_ANNOUNCE;
5824 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5828 probestart(struct cam_periph *periph, union ccb *start_ccb)
5830 /* Probe the device that our peripheral driver points to */
5831 struct ccb_scsiio *csio;
5834 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5836 softc = (probe_softc *)periph->softc;
5837 csio = &start_ccb->csio;
5839 switch (softc->action) {
5841 case PROBE_TUR_FOR_NEGOTIATION:
5844 scsi_test_unit_ready(csio,
5853 case PROBE_FULL_INQUIRY:
5854 case PROBE_INQUIRY_BASIC_DV1:
5855 case PROBE_INQUIRY_BASIC_DV2:
5858 struct scsi_inquiry_data *inq_buf;
5860 inq_buf = &periph->path->device->inq_data;
5863 * If the device is currently configured, we calculate an
5864 * MD5 checksum of the inquiry data, and if the serial number
5865 * length is greater than 0, add the serial number data
5866 * into the checksum as well. Once the inquiry and the
5867 * serial number check finish, we attempt to figure out
5868 * whether we still have the same device.
5870 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5872 MD5Init(&softc->context);
5873 MD5Update(&softc->context, (unsigned char *)inq_buf,
5874 sizeof(struct scsi_inquiry_data));
5875 softc->flags |= PROBE_INQUIRY_CKSUM;
5876 if (periph->path->device->serial_num_len > 0) {
5877 MD5Update(&softc->context,
5878 periph->path->device->serial_num,
5879 periph->path->device->serial_num_len);
5880 softc->flags |= PROBE_SERIAL_CKSUM;
5882 MD5Final(softc->digest, &softc->context);
5885 if (softc->action == PROBE_INQUIRY)
5886 inquiry_len = SHORT_INQUIRY_LENGTH;
5888 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
5891 * Some parallel SCSI devices fail to send an
5892 * ignore wide residue message when dealing with
5893 * odd length inquiry requests. Round up to be
5896 inquiry_len = roundup2(inquiry_len, 2);
5898 if (softc->action == PROBE_INQUIRY_BASIC_DV1
5899 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
5900 inq_buf = malloc(inquiry_len, M_TEMP, M_NOWAIT);
5902 if (inq_buf == NULL) {
5903 xpt_print(periph->path, "malloc failure- skipping Basic"
5904 "Domain Validation\n");
5905 softc->action = PROBE_DV_EXIT;
5906 scsi_test_unit_ready(csio,
5918 (u_int8_t *)inq_buf,
5923 /*timeout*/60 * 1000);
5926 case PROBE_MODE_SENSE:
5931 mode_buf_len = sizeof(struct scsi_mode_header_6)
5932 + sizeof(struct scsi_mode_blk_desc)
5933 + sizeof(struct scsi_control_page);
5934 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5935 if (mode_buf != NULL) {
5936 scsi_mode_sense(csio,
5941 SMS_PAGE_CTRL_CURRENT,
5942 SMS_CONTROL_MODE_PAGE,
5949 xpt_print(periph->path, "Unable to mode sense control page - "
5950 "malloc failure\n");
5951 softc->action = PROBE_SERIAL_NUM;
5954 case PROBE_SERIAL_NUM:
5956 struct scsi_vpd_unit_serial_number *serial_buf;
5957 struct cam_ed* device;
5960 device = periph->path->device;
5961 device->serial_num = NULL;
5962 device->serial_num_len = 0;
5964 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5965 serial_buf = (struct scsi_vpd_unit_serial_number *)
5966 malloc(sizeof(*serial_buf), M_TEMP,
5969 if (serial_buf != NULL) {
5974 (u_int8_t *)serial_buf,
5975 sizeof(*serial_buf),
5977 SVPD_UNIT_SERIAL_NUMBER,
5979 /*timeout*/60 * 1000);
5983 * We'll have to do without, let our probedone
5984 * routine finish up for us.
5986 start_ccb->csio.data_ptr = NULL;
5987 probedone(periph, start_ccb);
5991 xpt_action(start_ccb);
5995 proberequestdefaultnegotiation(struct cam_periph *periph)
5997 struct ccb_trans_settings cts;
5999 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
6000 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6001 cts.type = CTS_TYPE_USER_SETTINGS;
6002 xpt_action((union ccb *)&cts);
6003 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6006 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6007 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6008 xpt_action((union ccb *)&cts);
6012 * Backoff Negotiation Code- only pertinent for SPI devices.
6015 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
6017 struct ccb_trans_settings cts;
6018 struct ccb_trans_settings_spi *spi;
6020 memset(&cts, 0, sizeof (cts));
6021 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
6022 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6023 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6024 xpt_action((union ccb *)&cts);
6025 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6027 xpt_print(periph->path,
6028 "failed to get current device settings\n");
6032 if (cts.transport != XPORT_SPI) {
6034 xpt_print(periph->path, "not SPI transport\n");
6038 spi = &cts.xport_specific.spi;
6041 * We cannot renegotiate sync rate if we don't have one.
6043 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
6045 xpt_print(periph->path, "no sync rate known\n");
6051 * We'll assert that we don't have to touch PPR options- the
6052 * SIM will see what we do with period and offset and adjust
6053 * the PPR options as appropriate.
6057 * A sync rate with unknown or zero offset is nonsensical.
6058 * A sync period of zero means Async.
6060 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
6061 || spi->sync_offset == 0 || spi->sync_period == 0) {
6063 xpt_print(periph->path, "no sync rate available\n");
6068 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
6069 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6070 ("hit async: giving up on DV\n"));
6076 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
6077 * We don't try to remember 'last' settings to see if the SIM actually
6078 * gets into the speed we want to set. We check on the SIM telling
6079 * us that a requested speed is bad, but otherwise don't try and
6080 * check the speed due to the asynchronous and handshake nature
6083 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
6086 if (spi->sync_period >= 0xf) {
6087 spi->sync_period = 0;
6088 spi->sync_offset = 0;
6089 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6090 ("setting to async for DV\n"));
6092 * Once we hit async, we don't want to try
6093 * any more settings.
6095 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
6096 } else if (bootverbose) {
6097 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6098 ("DV: period 0x%x\n", spi->sync_period));
6099 printf("setting period to 0x%x\n", spi->sync_period);
6101 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6102 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6103 xpt_action((union ccb *)&cts);
6104 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6107 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6108 ("DV: failed to set period 0x%x\n", spi->sync_period));
6109 if (spi->sync_period == 0) {
6117 probedone(struct cam_periph *periph, union ccb *done_ccb)
6120 struct cam_path *path;
6123 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
6125 softc = (probe_softc *)periph->softc;
6126 path = done_ccb->ccb_h.path;
6127 priority = done_ccb->ccb_h.pinfo.priority;
6129 switch (softc->action) {
6132 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6134 if (cam_periph_error(done_ccb, 0,
6135 SF_NO_PRINT, NULL) == ERESTART)
6137 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
6138 /* Don't wedge the queue */
6139 xpt_release_devq(done_ccb->ccb_h.path,
6143 softc->action = PROBE_INQUIRY;
6144 xpt_release_ccb(done_ccb);
6145 xpt_schedule(periph, priority);
6149 case PROBE_FULL_INQUIRY:
6151 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6152 struct scsi_inquiry_data *inq_buf;
6153 u_int8_t periph_qual;
6155 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
6156 inq_buf = &path->device->inq_data;
6158 periph_qual = SID_QUAL(inq_buf);
6160 switch(periph_qual) {
6161 case SID_QUAL_LU_CONNECTED:
6166 * We conservatively request only
6167 * SHORT_INQUIRY_LEN bytes of inquiry
6168 * information during our first try
6169 * at sending an INQUIRY. If the device
6170 * has more information to give,
6171 * perform a second request specifying
6172 * the amount of information the device
6173 * is willing to give.
6175 len = inq_buf->additional_length
6176 + offsetof(struct scsi_inquiry_data,
6177 additional_length) + 1;
6178 if (softc->action == PROBE_INQUIRY
6179 && len > SHORT_INQUIRY_LENGTH) {
6180 softc->action = PROBE_FULL_INQUIRY;
6181 xpt_release_ccb(done_ccb);
6182 xpt_schedule(periph, priority);
6186 xpt_find_quirk(path->device);
6188 xpt_devise_transport(path);
6189 if (INQ_DATA_TQ_ENABLED(inq_buf))
6190 softc->action = PROBE_MODE_SENSE;
6192 softc->action = PROBE_SERIAL_NUM;
6194 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6196 xpt_release_ccb(done_ccb);
6197 xpt_schedule(periph, priority);
6203 } else if (cam_periph_error(done_ccb, 0,
6204 done_ccb->ccb_h.target_lun > 0
6205 ? SF_RETRY_UA|SF_QUIET_IR
6207 &softc->saved_ccb) == ERESTART) {
6209 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6210 /* Don't wedge the queue */
6211 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6215 * If we get to this point, we got an error status back
6216 * from the inquiry and the error status doesn't require
6217 * automatically retrying the command. Therefore, the
6218 * inquiry failed. If we had inquiry information before
6219 * for this device, but this latest inquiry command failed,
6220 * the device has probably gone away. If this device isn't
6221 * already marked unconfigured, notify the peripheral
6222 * drivers that this device is no more.
6224 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
6225 /* Send the async notification. */
6226 xpt_async(AC_LOST_DEVICE, path, NULL);
6228 xpt_release_ccb(done_ccb);
6231 case PROBE_MODE_SENSE:
6233 struct ccb_scsiio *csio;
6234 struct scsi_mode_header_6 *mode_hdr;
6236 csio = &done_ccb->csio;
6237 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
6238 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6239 struct scsi_control_page *page;
6242 offset = ((u_int8_t *)&mode_hdr[1])
6243 + mode_hdr->blk_desc_len;
6244 page = (struct scsi_control_page *)offset;
6245 path->device->queue_flags = page->queue_flags;
6246 } else if (cam_periph_error(done_ccb, 0,
6247 SF_RETRY_UA|SF_NO_PRINT,
6248 &softc->saved_ccb) == ERESTART) {
6250 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6251 /* Don't wedge the queue */
6252 xpt_release_devq(done_ccb->ccb_h.path,
6253 /*count*/1, /*run_queue*/TRUE);
6255 xpt_release_ccb(done_ccb);
6256 free(mode_hdr, M_TEMP);
6257 softc->action = PROBE_SERIAL_NUM;
6258 xpt_schedule(periph, priority);
6261 case PROBE_SERIAL_NUM:
6263 struct ccb_scsiio *csio;
6264 struct scsi_vpd_unit_serial_number *serial_buf;
6271 csio = &done_ccb->csio;
6272 priority = done_ccb->ccb_h.pinfo.priority;
6274 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
6276 /* Clean up from previous instance of this device */
6277 if (path->device->serial_num != NULL) {
6278 free(path->device->serial_num, M_CAMXPT);
6279 path->device->serial_num = NULL;
6280 path->device->serial_num_len = 0;
6283 if (serial_buf == NULL) {
6285 * Don't process the command as it was never sent
6287 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
6288 && (serial_buf->length > 0)) {
6291 path->device->serial_num =
6292 (u_int8_t *)malloc((serial_buf->length + 1),
6293 M_CAMXPT, M_NOWAIT);
6294 if (path->device->serial_num != NULL) {
6295 bcopy(serial_buf->serial_num,
6296 path->device->serial_num,
6297 serial_buf->length);
6298 path->device->serial_num_len =
6300 path->device->serial_num[serial_buf->length]
6303 } else if (cam_periph_error(done_ccb, 0,
6304 SF_RETRY_UA|SF_NO_PRINT,
6305 &softc->saved_ccb) == ERESTART) {
6307 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6308 /* Don't wedge the queue */
6309 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6314 * Let's see if we have seen this device before.
6316 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6318 u_int8_t digest[16];
6323 (unsigned char *)&path->device->inq_data,
6324 sizeof(struct scsi_inquiry_data));
6327 MD5Update(&context, serial_buf->serial_num,
6328 serial_buf->length);
6330 MD5Final(digest, &context);
6331 if (bcmp(softc->digest, digest, 16) == 0)
6335 * XXX Do we need to do a TUR in order to ensure
6336 * that the device really hasn't changed???
6339 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6340 xpt_async(AC_LOST_DEVICE, path, NULL);
6342 if (serial_buf != NULL)
6343 free(serial_buf, M_TEMP);
6347 * Now that we have all the necessary
6348 * information to safely perform transfer
6349 * negotiations... Controllers don't perform
6350 * any negotiation or tagged queuing until
6351 * after the first XPT_SET_TRAN_SETTINGS ccb is
6352 * received. So, on a new device, just retrieve
6353 * the user settings, and set them as the current
6354 * settings to set the device up.
6356 proberequestdefaultnegotiation(periph);
6357 xpt_release_ccb(done_ccb);
6360 * Perform a TUR to allow the controller to
6361 * perform any necessary transfer negotiation.
6363 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6364 xpt_schedule(periph, priority);
6367 xpt_release_ccb(done_ccb);
6370 case PROBE_TUR_FOR_NEGOTIATION:
6372 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6373 /* Don't wedge the queue */
6374 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6378 * Do Domain Validation for lun 0 on devices that claim
6379 * to support Synchronous Transfer modes.
6381 if (softc->action == PROBE_TUR_FOR_NEGOTIATION
6382 && done_ccb->ccb_h.target_lun == 0
6383 && (path->device->inq_data.flags & SID_Sync) != 0
6384 && (path->device->flags & CAM_DEV_IN_DV) == 0) {
6385 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6386 ("Begin Domain Validation\n"));
6387 path->device->flags |= CAM_DEV_IN_DV;
6388 xpt_release_ccb(done_ccb);
6389 softc->action = PROBE_INQUIRY_BASIC_DV1;
6390 xpt_schedule(periph, priority);
6393 if (softc->action == PROBE_DV_EXIT) {
6394 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6395 ("Leave Domain Validation\n"));
6397 path->device->flags &=
6398 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
6399 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6400 /* Inform the XPT that a new device has been found */
6401 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6402 xpt_action(done_ccb);
6403 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6406 xpt_release_ccb(done_ccb);
6408 case PROBE_INQUIRY_BASIC_DV1:
6409 case PROBE_INQUIRY_BASIC_DV2:
6411 struct scsi_inquiry_data *nbuf;
6412 struct ccb_scsiio *csio;
6414 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6415 /* Don't wedge the queue */
6416 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6419 csio = &done_ccb->csio;
6420 nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
6421 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
6423 "inquiry data fails comparison at DV%d step\n",
6424 softc->action == PROBE_INQUIRY_BASIC_DV1? 1 : 2);
6425 if (proberequestbackoff(periph, path->device)) {
6426 path->device->flags &= ~CAM_DEV_IN_DV;
6427 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6430 softc->action = PROBE_DV_EXIT;
6433 xpt_release_ccb(done_ccb);
6434 xpt_schedule(periph, priority);
6438 if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
6439 softc->action = PROBE_INQUIRY_BASIC_DV2;
6440 xpt_release_ccb(done_ccb);
6441 xpt_schedule(periph, priority);
6444 if (softc->action == PROBE_DV_EXIT) {
6445 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6446 ("Leave Domain Validation Successfully\n"));
6448 path->device->flags &=
6449 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
6450 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6451 /* Inform the XPT that a new device has been found */
6452 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6453 xpt_action(done_ccb);
6454 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6457 xpt_release_ccb(done_ccb);
6461 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6462 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6463 done_ccb->ccb_h.status = CAM_REQ_CMP;
6465 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6466 cam_periph_invalidate(periph);
6467 cam_periph_release(periph);
6469 probeschedule(periph);
6474 probecleanup(struct cam_periph *periph)
6476 free(periph->softc, M_TEMP);
6480 xpt_find_quirk(struct cam_ed *device)
6484 match = cam_quirkmatch((caddr_t)&device->inq_data,
6485 (caddr_t)xpt_quirk_table,
6486 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6487 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6490 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6492 device->quirk = (struct xpt_quirk_entry *)match;
6496 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
6501 error = sysctl_handle_int(oidp, &bool, sizeof(bool), req);
6502 if (error != 0 || req->newptr == NULL)
6504 if (bool == 0 || bool == 1) {
6514 xpt_devise_transport(struct cam_path *path)
6516 struct ccb_pathinq cpi;
6517 struct ccb_trans_settings cts;
6518 struct scsi_inquiry_data *inq_buf;
6520 /* Get transport information from the SIM */
6521 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6522 cpi.ccb_h.func_code = XPT_PATH_INQ;
6523 xpt_action((union ccb *)&cpi);
6526 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6527 inq_buf = &path->device->inq_data;
6528 path->device->protocol = PROTO_SCSI;
6529 path->device->protocol_version =
6530 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6531 path->device->transport = cpi.transport;
6532 path->device->transport_version = cpi.transport_version;
6535 * Any device not using SPI3 features should
6536 * be considered SPI2 or lower.
6538 if (inq_buf != NULL) {
6539 if (path->device->transport == XPORT_SPI
6540 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6541 && path->device->transport_version > 2)
6542 path->device->transport_version = 2;
6544 struct cam_ed* otherdev;
6546 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6548 otherdev = TAILQ_NEXT(otherdev, links)) {
6549 if (otherdev != path->device)
6553 if (otherdev != NULL) {
6555 * Initially assume the same versioning as
6556 * prior luns for this target.
6558 path->device->protocol_version =
6559 otherdev->protocol_version;
6560 path->device->transport_version =
6561 otherdev->transport_version;
6563 /* Until we know better, opt for safty */
6564 path->device->protocol_version = 2;
6565 if (path->device->transport == XPORT_SPI)
6566 path->device->transport_version = 2;
6568 path->device->transport_version = 0;
6574 * For a device compliant with SPC-2 we should be able
6575 * to determine the transport version supported by
6576 * scrutinizing the version descriptors in the
6580 /* Tell the controller what we think */
6581 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6582 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6583 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6584 cts.transport = path->device->transport;
6585 cts.transport_version = path->device->transport_version;
6586 cts.protocol = path->device->protocol;
6587 cts.protocol_version = path->device->protocol_version;
6588 cts.proto_specific.valid = 0;
6589 cts.xport_specific.valid = 0;
6590 xpt_action((union ccb *)&cts);
6594 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6597 struct ccb_pathinq cpi;
6598 struct ccb_trans_settings cur_cts;
6599 struct ccb_trans_settings_scsi *scsi;
6600 struct ccb_trans_settings_scsi *cur_scsi;
6601 struct cam_sim *sim;
6602 struct scsi_inquiry_data *inq_data;
6604 if (device == NULL) {
6605 cts->ccb_h.status = CAM_PATH_INVALID;
6606 xpt_done((union ccb *)cts);
6610 if (cts->protocol == PROTO_UNKNOWN
6611 || cts->protocol == PROTO_UNSPECIFIED) {
6612 cts->protocol = device->protocol;
6613 cts->protocol_version = device->protocol_version;
6616 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6617 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6618 cts->protocol_version = device->protocol_version;
6620 if (cts->protocol != device->protocol) {
6621 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
6622 cts->protocol, device->protocol);
6623 cts->protocol = device->protocol;
6626 if (cts->protocol_version > device->protocol_version) {
6628 xpt_print(cts->ccb_h.path, "Down reving Protocol "
6629 "Version from %d to %d?\n", cts->protocol_version,
6630 device->protocol_version);
6632 cts->protocol_version = device->protocol_version;
6635 if (cts->transport == XPORT_UNKNOWN
6636 || cts->transport == XPORT_UNSPECIFIED) {
6637 cts->transport = device->transport;
6638 cts->transport_version = device->transport_version;
6641 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6642 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6643 cts->transport_version = device->transport_version;
6645 if (cts->transport != device->transport) {
6646 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
6647 cts->transport, device->transport);
6648 cts->transport = device->transport;
6651 if (cts->transport_version > device->transport_version) {
6653 xpt_print(cts->ccb_h.path, "Down reving Transport "
6654 "Version from %d to %d?\n", cts->transport_version,
6655 device->transport_version);
6657 cts->transport_version = device->transport_version;
6660 sim = cts->ccb_h.path->bus->sim;
6663 * Nothing more of interest to do unless
6664 * this is a device connected via the
6667 if (cts->protocol != PROTO_SCSI) {
6668 if (async_update == FALSE)
6669 (*(sim->sim_action))(sim, (union ccb *)cts);
6673 inq_data = &device->inq_data;
6674 scsi = &cts->proto_specific.scsi;
6675 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6676 cpi.ccb_h.func_code = XPT_PATH_INQ;
6677 xpt_action((union ccb *)&cpi);
6679 /* SCSI specific sanity checking */
6680 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6681 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
6682 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6683 || (device->quirk->mintags == 0)) {
6685 * Can't tag on hardware that doesn't support tags,
6686 * doesn't have it enabled, or has broken tag support.
6688 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6691 if (async_update == FALSE) {
6693 * Perform sanity checking against what the
6694 * controller and device can do.
6696 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6697 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6698 cur_cts.type = cts->type;
6699 xpt_action((union ccb *)&cur_cts);
6700 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6703 cur_scsi = &cur_cts.proto_specific.scsi;
6704 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6705 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6706 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6708 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6709 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6712 /* SPI specific sanity checking */
6713 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6715 struct ccb_trans_settings_spi *spi;
6716 struct ccb_trans_settings_spi *cur_spi;
6718 spi = &cts->xport_specific.spi;
6720 cur_spi = &cur_cts.xport_specific.spi;
6722 /* Fill in any gaps in what the user gave us */
6723 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6724 spi->sync_period = cur_spi->sync_period;
6725 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6726 spi->sync_period = 0;
6727 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6728 spi->sync_offset = cur_spi->sync_offset;
6729 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6730 spi->sync_offset = 0;
6731 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6732 spi->ppr_options = cur_spi->ppr_options;
6733 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6734 spi->ppr_options = 0;
6735 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6736 spi->bus_width = cur_spi->bus_width;
6737 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6739 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6740 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6741 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6743 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6744 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6745 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6746 && (inq_data->flags & SID_Sync) == 0
6747 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6748 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6749 || (spi->sync_offset == 0)
6750 || (spi->sync_period == 0)) {
6752 spi->sync_period = 0;
6753 spi->sync_offset = 0;
6756 switch (spi->bus_width) {
6757 case MSG_EXT_WDTR_BUS_32_BIT:
6758 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6759 || (inq_data->flags & SID_WBus32) != 0
6760 || cts->type == CTS_TYPE_USER_SETTINGS)
6761 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6763 /* Fall Through to 16-bit */
6764 case MSG_EXT_WDTR_BUS_16_BIT:
6765 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6766 || (inq_data->flags & SID_WBus16) != 0
6767 || cts->type == CTS_TYPE_USER_SETTINGS)
6768 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6769 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6772 /* Fall Through to 8-bit */
6773 default: /* New bus width?? */
6774 case MSG_EXT_WDTR_BUS_8_BIT:
6775 /* All targets can do this */
6776 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6780 spi3caps = cpi.xport_specific.spi.ppr_options;
6781 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6782 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6783 spi3caps &= inq_data->spi3data;
6785 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6786 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6788 if ((spi3caps & SID_SPI_IUS) == 0)
6789 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6791 if ((spi3caps & SID_SPI_QAS) == 0)
6792 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6794 /* No SPI Transfer settings are allowed unless we are wide */
6795 if (spi->bus_width == 0)
6796 spi->ppr_options = 0;
6798 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6800 * Can't tag queue without disconnection.
6802 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6803 scsi->valid |= CTS_SCSI_VALID_TQ;
6807 * If we are currently performing tagged transactions to
6808 * this device and want to change its negotiation parameters,
6809 * go non-tagged for a bit to give the controller a chance to
6810 * negotiate unhampered by tag messages.
6812 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6813 && (device->inq_flags & SID_CmdQue) != 0
6814 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6815 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6816 CTS_SPI_VALID_SYNC_OFFSET|
6817 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6818 xpt_toggle_tags(cts->ccb_h.path);
6821 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6822 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6826 * If we are transitioning from tags to no-tags or
6827 * vice-versa, we need to carefully freeze and restart
6828 * the queue so that we don't overlap tagged and non-tagged
6829 * commands. We also temporarily stop tags if there is
6830 * a change in transfer negotiation settings to allow
6831 * "tag-less" negotiation.
6833 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6834 || (device->inq_flags & SID_CmdQue) != 0)
6835 device_tagenb = TRUE;
6837 device_tagenb = FALSE;
6839 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6840 && device_tagenb == FALSE)
6841 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6842 && device_tagenb == TRUE)) {
6844 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6846 * Delay change to use tags until after a
6847 * few commands have gone to this device so
6848 * the controller has time to perform transfer
6849 * negotiations without tagged messages getting
6852 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6853 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6855 struct ccb_relsim crs;
6857 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6858 device->inq_flags &= ~SID_CmdQue;
6859 xpt_dev_ccbq_resize(cts->ccb_h.path,
6860 sim->max_dev_openings);
6861 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6862 device->tag_delay_count = 0;
6864 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6866 crs.ccb_h.func_code = XPT_REL_SIMQ;
6867 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6869 = crs.release_timeout
6872 xpt_action((union ccb *)&crs);
6876 if (async_update == FALSE)
6877 (*(sim->sim_action))(sim, (union ccb *)cts);
6882 xpt_toggle_tags(struct cam_path *path)
6887 * Give controllers a chance to renegotiate
6888 * before starting tag operations. We
6889 * "toggle" tagged queuing off then on
6890 * which causes the tag enable command delay
6891 * counter to come into effect.
6894 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6895 || ((dev->inq_flags & SID_CmdQue) != 0
6896 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6897 struct ccb_trans_settings cts;
6899 xpt_setup_ccb(&cts.ccb_h, path, 1);
6900 cts.protocol = PROTO_SCSI;
6901 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6902 cts.transport = XPORT_UNSPECIFIED;
6903 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6904 cts.proto_specific.scsi.flags = 0;
6905 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6906 xpt_set_transfer_settings(&cts, path->device,
6907 /*async_update*/TRUE);
6908 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6909 xpt_set_transfer_settings(&cts, path->device,
6910 /*async_update*/TRUE);
6915 xpt_start_tags(struct cam_path *path)
6917 struct ccb_relsim crs;
6918 struct cam_ed *device;
6919 struct cam_sim *sim;
6922 device = path->device;
6923 sim = path->bus->sim;
6924 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6925 xpt_freeze_devq(path, /*count*/1);
6926 device->inq_flags |= SID_CmdQue;
6927 if (device->tag_saved_openings != 0)
6928 newopenings = device->tag_saved_openings;
6930 newopenings = min(device->quirk->maxtags,
6931 sim->max_tagged_dev_openings);
6932 xpt_dev_ccbq_resize(path, newopenings);
6933 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6934 crs.ccb_h.func_code = XPT_REL_SIMQ;
6935 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6937 = crs.release_timeout
6940 xpt_action((union ccb *)&crs);
6943 static int busses_to_config;
6944 static int busses_to_reset;
6947 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6950 mtx_assert(bus->sim->mtx, MA_OWNED);
6952 if (bus->path_id != CAM_XPT_PATH_ID) {
6953 struct cam_path path;
6954 struct ccb_pathinq cpi;
6958 xpt_compile_path(&path, NULL, bus->path_id,
6959 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6960 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6961 cpi.ccb_h.func_code = XPT_PATH_INQ;
6962 xpt_action((union ccb *)&cpi);
6963 can_negotiate = cpi.hba_inquiry;
6964 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6965 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6968 xpt_release_path(&path);
6975 xptconfigfunc(struct cam_eb *bus, void *arg)
6977 struct cam_path *path;
6978 union ccb *work_ccb;
6980 mtx_assert(bus->sim->mtx, MA_OWNED);
6982 if (bus->path_id != CAM_XPT_PATH_ID) {
6986 work_ccb = xpt_alloc_ccb_nowait();
6987 if (work_ccb == NULL) {
6989 xpt_finishconfig(xpt_periph, NULL);
6992 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6993 CAM_TARGET_WILDCARD,
6994 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6995 printf("xptconfigfunc: xpt_create_path failed with "
6996 "status %#x for bus %d\n", status, bus->path_id);
6997 printf("xptconfigfunc: halting bus configuration\n");
6998 xpt_free_ccb(work_ccb);
7000 xpt_finishconfig(xpt_periph, NULL);
7003 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
7004 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
7005 xpt_action(work_ccb);
7006 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
7007 printf("xptconfigfunc: CPI failed on bus %d "
7008 "with status %d\n", bus->path_id,
7009 work_ccb->ccb_h.status);
7010 xpt_finishconfig(xpt_periph, work_ccb);
7014 can_negotiate = work_ccb->cpi.hba_inquiry;
7015 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
7016 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
7017 && (can_negotiate != 0)) {
7018 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
7019 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
7020 work_ccb->ccb_h.cbfcnp = NULL;
7021 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
7022 ("Resetting Bus\n"));
7023 xpt_action(work_ccb);
7024 xpt_finishconfig(xpt_periph, work_ccb);
7026 /* Act as though we performed a successful BUS RESET */
7027 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
7028 xpt_finishconfig(xpt_periph, work_ccb);
7036 xpt_config(void *arg)
7039 * Now that interrupts are enabled, go find our devices
7043 /* Setup debugging flags and path */
7044 #ifdef CAM_DEBUG_FLAGS
7045 cam_dflags = CAM_DEBUG_FLAGS;
7046 #else /* !CAM_DEBUG_FLAGS */
7047 cam_dflags = CAM_DEBUG_NONE;
7048 #endif /* CAM_DEBUG_FLAGS */
7049 #ifdef CAM_DEBUG_BUS
7050 if (cam_dflags != CAM_DEBUG_NONE) {
7052 * Locking is specifically omitted here. No SIMs have
7053 * registered yet, so xpt_create_path will only be searching
7054 * empty lists of targets and devices.
7056 if (xpt_create_path(&cam_dpath, xpt_periph,
7057 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
7058 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
7059 printf("xpt_config: xpt_create_path() failed for debug"
7060 " target %d:%d:%d, debugging disabled\n",
7061 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
7062 cam_dflags = CAM_DEBUG_NONE;
7066 #else /* !CAM_DEBUG_BUS */
7068 #endif /* CAM_DEBUG_BUS */
7069 #endif /* CAMDEBUG */
7072 * Scan all installed busses.
7074 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
7076 if (busses_to_config == 0) {
7077 /* Call manually because we don't have any busses */
7078 xpt_finishconfig(xpt_periph, NULL);
7080 if (busses_to_reset > 0 && scsi_delay >= 2000) {
7081 printf("Waiting %d seconds for SCSI "
7082 "devices to settle\n", scsi_delay/1000);
7084 xpt_for_all_busses(xptconfigfunc, NULL);
7089 * If the given device only has one peripheral attached to it, and if that
7090 * peripheral is the passthrough driver, announce it. This insures that the
7091 * user sees some sort of announcement for every peripheral in their system.
7094 xptpassannouncefunc(struct cam_ed *device, void *arg)
7096 struct cam_periph *periph;
7099 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
7100 periph = SLIST_NEXT(periph, periph_links), i++);
7102 periph = SLIST_FIRST(&device->periphs);
7104 && (strncmp(periph->periph_name, "pass", 4) == 0))
7105 xpt_announce_periph(periph, NULL);
7111 xpt_finishconfig_task(void *context, int pending)
7113 struct periph_driver **p_drv;
7116 if (busses_to_config == 0) {
7117 /* Register all the peripheral drivers */
7118 /* XXX This will have to change when we have loadable modules */
7119 p_drv = periph_drivers;
7120 for (i = 0; p_drv[i] != NULL; i++) {
7121 (*p_drv[i]->init)();
7125 * Check for devices with no "standard" peripheral driver
7126 * attached. For any devices like that, announce the
7127 * passthrough driver so the user will see something.
7129 xpt_for_all_devices(xptpassannouncefunc, NULL);
7131 /* Release our hook so that the boot can continue. */
7132 config_intrhook_disestablish(xsoftc.xpt_config_hook);
7133 free(xsoftc.xpt_config_hook, M_TEMP);
7134 xsoftc.xpt_config_hook = NULL;
7137 free(context, M_CAMXPT);
7141 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
7143 struct xpt_task *task;
7145 if (done_ccb != NULL) {
7146 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
7147 ("xpt_finishconfig\n"));
7148 switch(done_ccb->ccb_h.func_code) {
7150 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
7151 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
7152 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
7153 done_ccb->crcn.flags = 0;
7154 xpt_action(done_ccb);
7160 xpt_free_path(done_ccb->ccb_h.path);
7166 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
7168 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
7169 taskqueue_enqueue(taskqueue_thread, &task->task);
7172 if (done_ccb != NULL)
7173 xpt_free_ccb(done_ccb);
7177 xptaction(struct cam_sim *sim, union ccb *work_ccb)
7179 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
7181 switch (work_ccb->ccb_h.func_code) {
7182 /* Common cases first */
7183 case XPT_PATH_INQ: /* Path routing inquiry */
7185 struct ccb_pathinq *cpi;
7187 cpi = &work_ccb->cpi;
7188 cpi->version_num = 1; /* XXX??? */
7189 cpi->hba_inquiry = 0;
7190 cpi->target_sprt = 0;
7192 cpi->hba_eng_cnt = 0;
7193 cpi->max_target = 0;
7195 cpi->initiator_id = 0;
7196 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
7197 strncpy(cpi->hba_vid, "", HBA_IDLEN);
7198 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
7199 cpi->unit_number = sim->unit_number;
7200 cpi->bus_id = sim->bus_id;
7201 cpi->base_transfer_speed = 0;
7202 cpi->protocol = PROTO_UNSPECIFIED;
7203 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
7204 cpi->transport = XPORT_UNSPECIFIED;
7205 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
7206 cpi->ccb_h.status = CAM_REQ_CMP;
7211 work_ccb->ccb_h.status = CAM_REQ_INVALID;
7218 * The xpt as a "controller" has no interrupt sources, so polling
7222 xptpoll(struct cam_sim *sim)
7227 xpt_lock_buses(void)
7229 mtx_lock(&xsoftc.xpt_topo_lock);
7233 xpt_unlock_buses(void)
7235 mtx_unlock(&xsoftc.xpt_topo_lock);
7242 struct cam_sim *sim;
7244 mtx_lock(&cam_simq_lock);
7246 TAILQ_CONCAT(&queue, &cam_simq, links);
7247 mtx_unlock(&cam_simq_lock);
7249 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
7250 TAILQ_REMOVE(&queue, sim, links);
7252 sim->flags &= ~CAM_SIM_ON_DONEQ;
7253 camisr_runqueue(&sim->sim_doneq);
7254 CAM_SIM_UNLOCK(sim);
7259 camisr_runqueue(void *V_queue)
7261 cam_isrq_t *queue = V_queue;
7262 struct ccb_hdr *ccb_h;
7264 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
7267 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
7268 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7270 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7275 if (ccb_h->flags & CAM_HIGH_POWER) {
7276 struct highpowerlist *hphead;
7277 union ccb *send_ccb;
7279 mtx_lock(&xsoftc.xpt_lock);
7280 hphead = &xsoftc.highpowerq;
7282 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7285 * Increment the count since this command is done.
7287 xsoftc.num_highpower++;
7290 * Any high powered commands queued up?
7292 if (send_ccb != NULL) {
7294 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7295 mtx_unlock(&xsoftc.xpt_lock);
7297 xpt_release_devq(send_ccb->ccb_h.path,
7298 /*count*/1, /*runqueue*/TRUE);
7300 mtx_unlock(&xsoftc.xpt_lock);
7303 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7306 dev = ccb_h->path->device;
7308 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7310 if (!SIM_DEAD(ccb_h->path->bus->sim)) {
7311 ccb_h->path->bus->sim->devq->send_active--;
7312 ccb_h->path->bus->sim->devq->send_openings++;
7315 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7316 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7317 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7318 && (dev->ccbq.dev_active == 0))) {
7320 xpt_release_devq(ccb_h->path, /*count*/1,
7324 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7325 && (--dev->tag_delay_count == 0))
7326 xpt_start_tags(ccb_h->path);
7328 if ((dev->ccbq.queue.entries > 0)
7329 && (dev->qfrozen_cnt == 0)
7330 && (device_is_send_queued(dev) == 0)) {
7331 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7336 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7337 xpt_release_simq(ccb_h->path->bus->sim,
7339 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7343 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7344 && (ccb_h->status & CAM_DEV_QFRZN)) {
7345 xpt_release_devq(ccb_h->path, /*count*/1,
7347 ccb_h->status &= ~CAM_DEV_QFRZN;
7349 xpt_run_dev_sendq(ccb_h->path->bus);
7352 /* Call the peripheral driver's callback */
7353 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7358 dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7361 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7366 dead_sim_poll(struct cam_sim *sim)