2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
43 #include <sys/interrupt.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
51 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
66 #include <machine/stdarg.h> /* for xpt_print below */
69 /* Datastructures internal to the xpt layer */
70 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
73 * Definition of an async handler callback block. These are used to add
74 * SIMs and peripherals to the async callback lists.
77 SLIST_ENTRY(async_node) links;
78 u_int32_t event_enable; /* Async Event enables */
79 void (*callback)(void *arg, u_int32_t code,
80 struct cam_path *path, void *args);
84 SLIST_HEAD(async_list, async_node);
85 SLIST_HEAD(periph_list, cam_periph);
86 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
89 * This is the maximum number of high powered commands (e.g. start unit)
90 * that can be outstanding at a particular time.
92 #ifndef CAM_MAX_HIGHPOWER
93 #define CAM_MAX_HIGHPOWER 4
96 /* number of high powered commands that can go through right now */
97 static int num_highpower = CAM_MAX_HIGHPOWER;
100 * Structure for queueing a device in a run queue.
101 * There is one run queue for allocating new ccbs,
102 * and another for sending ccbs to the controller.
104 struct cam_ed_qinfo {
106 struct cam_ed *device;
110 * The CAM EDT (Existing Device Table) contains the device information for
111 * all devices for all busses in the system. The table contains a
112 * cam_ed structure for each device on the bus.
115 TAILQ_ENTRY(cam_ed) links;
116 struct cam_ed_qinfo alloc_ccb_entry;
117 struct cam_ed_qinfo send_ccb_entry;
118 struct cam_et *target;
121 * Queue of type drivers wanting to do
122 * work on this device.
124 struct cam_ccbq ccbq; /* Queue of pending ccbs */
125 struct async_list asyncs; /* Async callback info for this B/T/L */
126 struct periph_list periphs; /* All attached devices */
127 u_int generation; /* Generation number */
128 struct cam_periph *owner; /* Peripheral driver's ownership tag */
129 struct xpt_quirk_entry *quirk; /* Oddities about this device */
130 /* Storage for the inquiry data */
132 u_int protocol_version;
134 u_int transport_version;
135 struct scsi_inquiry_data inq_data;
136 u_int8_t inq_flags; /*
137 * Current settings for inquiry flags.
138 * This allows us to override settings
139 * like disconnection and tagged
140 * queuing for a device.
142 u_int8_t queue_flags; /* Queue flags from the control page */
143 u_int8_t serial_num_len;
144 u_int8_t *serial_num;
145 u_int32_t qfrozen_cnt;
147 #define CAM_DEV_UNCONFIGURED 0x01
148 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
149 #define CAM_DEV_REL_ON_COMPLETE 0x04
150 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
151 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
152 #define CAM_DEV_TAG_AFTER_COUNT 0x20
153 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
154 #define CAM_DEV_IN_DV 0x80
155 #define CAM_DEV_DV_HIT_BOTTOM 0x100
156 u_int32_t tag_delay_count;
157 #define CAM_TAG_DELAY_COUNT 5
158 u_int32_t tag_saved_openings;
160 struct callout_handle c_handle;
164 * Each target is represented by an ET (Existing Target). These
165 * entries are created when a target is successfully probed with an
166 * identify, and removed when a device fails to respond after a number
167 * of retries, or a bus rescan finds the device missing.
170 TAILQ_HEAD(, cam_ed) ed_entries;
171 TAILQ_ENTRY(cam_et) links;
173 target_id_t target_id;
176 struct timeval last_reset;
180 * Each bus is represented by an EB (Existing Bus). These entries
181 * are created by calls to xpt_bus_register and deleted by calls to
182 * xpt_bus_deregister.
185 TAILQ_HEAD(, cam_et) et_entries;
186 TAILQ_ENTRY(cam_eb) links;
189 struct timeval last_reset;
191 #define CAM_EB_RUNQ_SCHEDULED 0x01
197 struct cam_periph *periph;
199 struct cam_et *target;
200 struct cam_ed *device;
203 struct xpt_quirk_entry {
204 struct scsi_inquiry_pattern inq_pat;
206 #define CAM_QUIRK_NOLUNS 0x01
207 #define CAM_QUIRK_NOSERIAL 0x02
208 #define CAM_QUIRK_HILUNS 0x04
209 #define CAM_QUIRK_NOHILUNS 0x08
214 static int cam_srch_hi = 0;
215 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
216 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
217 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
218 sysctl_cam_search_luns, "I",
219 "allow search above LUN 7 for SCSI3 and greater devices");
221 #define CAM_SCSI2_MAXLUN 8
223 * If we're not quirked to search <= the first 8 luns
224 * and we are either quirked to search above lun 8,
225 * or we're > SCSI-2 and we've enabled hilun searching,
226 * or we're > SCSI-2 and the last lun was a success,
227 * we can look for luns above lun 8.
229 #define CAN_SRCH_HI_SPARSE(dv) \
230 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
231 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
232 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
234 #define CAN_SRCH_HI_DENSE(dv) \
235 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
236 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
237 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
245 u_int32_t generation;
248 static const char quantum[] = "QUANTUM";
249 static const char sony[] = "SONY";
250 static const char west_digital[] = "WDIGTL";
251 static const char samsung[] = "SAMSUNG";
252 static const char seagate[] = "SEAGATE";
253 static const char microp[] = "MICROP";
255 static struct xpt_quirk_entry xpt_quirk_table[] =
258 /* Reports QUEUE FULL for temporary resource shortages */
259 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
260 /*quirks*/0, /*mintags*/24, /*maxtags*/32
263 /* Reports QUEUE FULL for temporary resource shortages */
264 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
265 /*quirks*/0, /*mintags*/24, /*maxtags*/32
268 /* Reports QUEUE FULL for temporary resource shortages */
269 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
270 /*quirks*/0, /*mintags*/24, /*maxtags*/32
273 /* Broken tagged queuing drive */
274 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
275 /*quirks*/0, /*mintags*/0, /*maxtags*/0
278 /* Broken tagged queuing drive */
279 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
280 /*quirks*/0, /*mintags*/0, /*maxtags*/0
283 /* Broken tagged queuing drive */
284 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
285 /*quirks*/0, /*mintags*/0, /*maxtags*/0
289 * Unfortunately, the Quantum Atlas III has the same
290 * problem as the Atlas II drives above.
291 * Reported by: "Johan Granlund" <johan@granlund.nu>
293 * For future reference, the drive with the problem was:
294 * QUANTUM QM39100TD-SW N1B0
296 * It's possible that Quantum will fix the problem in later
297 * firmware revisions. If that happens, the quirk entry
298 * will need to be made specific to the firmware revisions
302 /* Reports QUEUE FULL for temporary resource shortages */
303 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
304 /*quirks*/0, /*mintags*/24, /*maxtags*/32
308 * 18 Gig Atlas III, same problem as the 9G version.
309 * Reported by: Andre Albsmeier
310 * <andre.albsmeier@mchp.siemens.de>
312 * For future reference, the drive with the problem was:
313 * QUANTUM QM318000TD-S N491
315 /* Reports QUEUE FULL for temporary resource shortages */
316 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
317 /*quirks*/0, /*mintags*/24, /*maxtags*/32
321 * Broken tagged queuing drive
322 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
323 * and: Martin Renters <martin@tdc.on.ca>
325 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
326 /*quirks*/0, /*mintags*/0, /*maxtags*/0
329 * The Seagate Medalist Pro drives have very poor write
330 * performance with anything more than 2 tags.
332 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
333 * Drive: <SEAGATE ST36530N 1444>
335 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
336 * Drive: <SEAGATE ST34520W 1281>
338 * No one has actually reported that the 9G version
339 * (ST39140*) of the Medalist Pro has the same problem, but
340 * we're assuming that it does because the 4G and 6.5G
341 * versions of the drive are broken.
344 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
345 /*quirks*/0, /*mintags*/2, /*maxtags*/2
348 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
349 /*quirks*/0, /*mintags*/2, /*maxtags*/2
352 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
353 /*quirks*/0, /*mintags*/2, /*maxtags*/2
357 * Slow when tagged queueing is enabled. Write performance
358 * steadily drops off with more and more concurrent
359 * transactions. Best sequential write performance with
360 * tagged queueing turned off and write caching turned on.
363 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
364 * Drive: DCAS-34330 w/ "S65A" firmware.
366 * The drive with the problem had the "S65A" firmware
367 * revision, and has also been reported (by Stephen J.
368 * Roznowski <sjr@home.net>) for a drive with the "S61A"
371 * Although no one has reported problems with the 2 gig
372 * version of the DCAS drive, the assumption is that it
373 * has the same problems as the 4 gig version. Therefore
374 * this quirk entries disables tagged queueing for all
377 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
378 /*quirks*/0, /*mintags*/0, /*maxtags*/0
381 /* Broken tagged queuing drive */
382 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
383 /*quirks*/0, /*mintags*/0, /*maxtags*/0
386 /* Broken tagged queuing drive */
387 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
388 /*quirks*/0, /*mintags*/0, /*maxtags*/0
391 /* This does not support other than LUN 0 */
392 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
393 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
397 * Broken tagged queuing drive.
399 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
402 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
403 /*quirks*/0, /*mintags*/0, /*maxtags*/0
407 * Slow when tagged queueing is enabled. (1.5MB/sec versus
409 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
410 * Best performance with these drives is achieved with
411 * tagged queueing turned off, and write caching turned on.
413 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
414 /*quirks*/0, /*mintags*/0, /*maxtags*/0
418 * Slow when tagged queueing is enabled. (1.5MB/sec versus
420 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
421 * Best performance with these drives is achieved with
422 * tagged queueing turned off, and write caching turned on.
424 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
425 /*quirks*/0, /*mintags*/0, /*maxtags*/0
429 * Doesn't handle queue full condition correctly,
430 * so we need to limit maxtags to what the device
431 * can handle instead of determining this automatically.
433 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
434 /*quirks*/0, /*mintags*/2, /*maxtags*/32
437 /* Really only one LUN */
438 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
439 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
442 /* I can't believe we need a quirk for DPT volumes. */
443 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
444 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
445 /*mintags*/0, /*maxtags*/255
449 * Many Sony CDROM drives don't like multi-LUN probing.
451 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
452 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
456 * This drive doesn't like multiple LUN probing.
457 * Submitted by: Parag Patel <parag@cgt.com>
459 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
460 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
463 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
464 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
468 * The 8200 doesn't like multi-lun probing, and probably
469 * don't like serial number requests either.
472 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
475 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
479 * Let's try the same as above, but for a drive that says
480 * it's an IPL-6860 but is actually an EXB 8200.
483 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
486 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
490 * These Hitachi drives don't like multi-lun probing.
491 * The PR submitter has a DK319H, but says that the Linux
492 * kernel has a similar work-around for the DK312 and DK314,
493 * so all DK31* drives are quirked here.
495 * Submitted by: Paul Haddad <paul@pth.com>
497 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
498 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
502 * The Hitachi CJ series with J8A8 firmware apparantly has
503 * problems with tagged commands.
505 * Reported by: amagai@nue.org
507 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
508 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
512 * These are the large storage arrays.
513 * Submitted by: William Carrel <william.carrel@infospace.com>
515 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
516 CAM_QUIRK_HILUNS, 2, 1024
520 * This old revision of the TDC3600 is also SCSI-1, and
521 * hangs upon serial number probing.
524 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
527 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
531 * Maxtor Personal Storage 3000XT (Firewire)
532 * hangs upon serial number probing.
535 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
538 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
542 * Would repond to all LUNs if asked for.
545 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
548 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
552 * Would repond to all LUNs if asked for.
555 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
558 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
561 /* Submitted by: Matthew Dodd <winter@jurai.net> */
562 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
563 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
566 /* Submitted by: Matthew Dodd <winter@jurai.net> */
567 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
568 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
571 /* TeraSolutions special settings for TRC-22 RAID */
572 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
573 /*quirks*/0, /*mintags*/55, /*maxtags*/255
576 /* Veritas Storage Appliance */
577 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
578 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
582 * Would respond to all LUNs. Device type and removable
583 * flag are jumper-selectable.
585 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
588 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
591 /* EasyRAID E5A aka. areca ARC-6010 */
592 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
593 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
596 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
597 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
600 /* Default tagged queuing parameters for all devices */
602 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
603 /*vendor*/"*", /*product*/"*", /*revision*/"*"
605 /*quirks*/0, /*mintags*/2, /*maxtags*/255
609 static const int xpt_quirk_table_size =
610 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
614 DM_RET_FLAG_MASK = 0x0f,
617 DM_RET_DESCEND = 0x20,
619 DM_RET_ACTION_MASK = 0xf0
627 } xpt_traverse_depth;
629 struct xpt_traverse_config {
630 xpt_traverse_depth depth;
635 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
636 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
637 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
638 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
639 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
641 /* Transport layer configuration information */
642 static struct xpt_softc xsoftc;
644 /* Queues for our software interrupt handler */
645 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
646 static cam_isrq_t cam_bioq;
647 static struct mtx cam_bioq_lock;
649 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
650 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
651 static u_int xpt_max_ccbs; /*
652 * Maximum size of ccb pool. Modified as
653 * devices are added/removed or have their
654 * opening counts changed.
656 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
658 struct cam_periph *xpt_periph;
660 static periph_init_t xpt_periph_init;
662 static periph_init_t probe_periph_init;
664 static struct periph_driver xpt_driver =
666 xpt_periph_init, "xpt",
667 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
670 static struct periph_driver probe_driver =
672 probe_periph_init, "probe",
673 TAILQ_HEAD_INITIALIZER(probe_driver.units)
676 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
677 PERIPHDRIVER_DECLARE(probe, probe_driver);
680 static d_open_t xptopen;
681 static d_close_t xptclose;
682 static d_ioctl_t xptioctl;
684 static struct cdevsw xpt_cdevsw = {
685 .d_version = D_VERSION,
686 .d_flags = D_NEEDGIANT,
693 static struct intr_config_hook *xpt_config_hook;
695 static void dead_sim_action(struct cam_sim *sim, union ccb *ccb);
696 static void dead_sim_poll(struct cam_sim *sim);
698 /* Dummy SIM that is used when the real one has gone. */
699 static struct cam_sim cam_dead_sim = {
700 .sim_action = dead_sim_action,
701 .sim_poll = dead_sim_poll,
702 .sim_name = "dead_sim",
705 #define SIM_DEAD(sim) ((sim) == &cam_dead_sim)
707 /* Registered busses */
708 static TAILQ_HEAD(,cam_eb) xpt_busses;
709 static u_int bus_generation;
711 /* Storage for debugging datastructures */
713 struct cam_path *cam_dpath;
714 u_int32_t cam_dflags;
715 u_int32_t cam_debug_delay;
718 /* Pointers to software interrupt handlers */
719 static void *cambio_ih;
721 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
722 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
726 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
727 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
728 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
730 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
731 || defined(CAM_DEBUG_LUN)
733 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
734 || !defined(CAM_DEBUG_LUN)
735 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
737 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
738 #else /* !CAMDEBUG */
739 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
740 #endif /* CAMDEBUG */
741 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
743 /* Our boot-time initialization hook */
744 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
746 static moduledata_t cam_moduledata = {
748 cam_module_event_handler,
752 static void xpt_init(void *);
754 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
755 MODULE_VERSION(cam, 1);
758 static cam_status xpt_compile_path(struct cam_path *new_path,
759 struct cam_periph *perph,
761 target_id_t target_id,
764 static void xpt_release_path(struct cam_path *path);
766 static void xpt_async_bcast(struct async_list *async_head,
767 u_int32_t async_code,
768 struct cam_path *path,
770 static void xpt_dev_async(u_int32_t async_code,
772 struct cam_et *target,
773 struct cam_ed *device,
775 static path_id_t xptnextfreepathid(void);
776 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
777 static union ccb *xpt_get_ccb(struct cam_ed *device);
778 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
779 u_int32_t new_priority);
780 static void xpt_run_dev_allocq(struct cam_eb *bus);
781 static void xpt_run_dev_sendq(struct cam_eb *bus);
782 static timeout_t xpt_release_devq_timeout;
783 static timeout_t xpt_release_simq_timeout;
784 static void xpt_release_bus(struct cam_eb *bus);
785 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
787 static struct cam_et*
788 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
789 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
790 static struct cam_ed*
791 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
793 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
794 struct cam_ed *device);
795 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
796 static struct cam_eb*
797 xpt_find_bus(path_id_t path_id);
798 static struct cam_et*
799 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
800 static struct cam_ed*
801 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
802 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
803 static void xpt_scan_lun(struct cam_periph *periph,
804 struct cam_path *path, cam_flags flags,
806 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
807 static xpt_busfunc_t xptconfigbuscountfunc;
808 static xpt_busfunc_t xptconfigfunc;
809 static void xpt_config(void *arg);
810 static xpt_devicefunc_t xptpassannouncefunc;
811 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
812 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
813 static void xptpoll(struct cam_sim *sim);
814 static void camisr(void *);
816 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
817 static void xptasync(struct cam_periph *periph,
818 u_int32_t code, cam_path *path);
820 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
821 u_int num_patterns, struct cam_eb *bus);
822 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
824 struct cam_ed *device);
825 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
827 struct cam_periph *periph);
828 static xpt_busfunc_t xptedtbusfunc;
829 static xpt_targetfunc_t xptedttargetfunc;
830 static xpt_devicefunc_t xptedtdevicefunc;
831 static xpt_periphfunc_t xptedtperiphfunc;
832 static xpt_pdrvfunc_t xptplistpdrvfunc;
833 static xpt_periphfunc_t xptplistperiphfunc;
834 static int xptedtmatch(struct ccb_dev_match *cdm);
835 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
836 static int xptbustraverse(struct cam_eb *start_bus,
837 xpt_busfunc_t *tr_func, void *arg);
838 static int xpttargettraverse(struct cam_eb *bus,
839 struct cam_et *start_target,
840 xpt_targetfunc_t *tr_func, void *arg);
841 static int xptdevicetraverse(struct cam_et *target,
842 struct cam_ed *start_device,
843 xpt_devicefunc_t *tr_func, void *arg);
844 static int xptperiphtraverse(struct cam_ed *device,
845 struct cam_periph *start_periph,
846 xpt_periphfunc_t *tr_func, void *arg);
847 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
848 xpt_pdrvfunc_t *tr_func, void *arg);
849 static int xptpdperiphtraverse(struct periph_driver **pdrv,
850 struct cam_periph *start_periph,
851 xpt_periphfunc_t *tr_func,
853 static xpt_busfunc_t xptdefbusfunc;
854 static xpt_targetfunc_t xptdeftargetfunc;
855 static xpt_devicefunc_t xptdefdevicefunc;
856 static xpt_periphfunc_t xptdefperiphfunc;
857 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
859 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
862 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
865 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
868 static xpt_devicefunc_t xptsetasyncfunc;
869 static xpt_busfunc_t xptsetasyncbusfunc;
870 static cam_status xptregister(struct cam_periph *periph,
872 static cam_status proberegister(struct cam_periph *periph,
874 static void probeschedule(struct cam_periph *probe_periph);
875 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
876 static void proberequestdefaultnegotiation(struct cam_periph *periph);
877 static int proberequestbackoff(struct cam_periph *periph,
878 struct cam_ed *device);
879 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
880 static void probecleanup(struct cam_periph *periph);
881 static void xpt_find_quirk(struct cam_ed *device);
882 static void xpt_devise_transport(struct cam_path *path);
883 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
884 struct cam_ed *device,
886 static void xpt_toggle_tags(struct cam_path *path);
887 static void xpt_start_tags(struct cam_path *path);
888 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
890 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
892 static __inline int periph_is_queued(struct cam_periph *periph);
893 static __inline int device_is_alloc_queued(struct cam_ed *device);
894 static __inline int device_is_send_queued(struct cam_ed *device);
895 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
898 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
902 if (dev->ccbq.devq_openings > 0) {
903 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
904 cam_ccbq_resize(&dev->ccbq,
905 dev->ccbq.dev_openings
906 + dev->ccbq.dev_active);
907 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
910 * The priority of a device waiting for CCB resources
911 * is that of the the highest priority peripheral driver
914 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
915 &dev->alloc_ccb_entry.pinfo,
916 CAMQ_GET_HEAD(&dev->drvq)->priority);
925 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
929 if (dev->ccbq.dev_openings > 0) {
931 * The priority of a device waiting for controller
932 * resources is that of the the highest priority CCB
936 xpt_schedule_dev(&bus->sim->devq->send_queue,
937 &dev->send_ccb_entry.pinfo,
938 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
946 periph_is_queued(struct cam_periph *periph)
948 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
952 device_is_alloc_queued(struct cam_ed *device)
954 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
958 device_is_send_queued(struct cam_ed *device)
960 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
964 dev_allocq_is_runnable(struct cam_devq *devq)
968 * Have space to do more work.
969 * Allowed to do work.
971 return ((devq->alloc_queue.qfrozen_cnt == 0)
972 && (devq->alloc_queue.entries > 0)
973 && (devq->alloc_openings > 0));
979 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
989 xptdone(struct cam_periph *periph, union ccb *done_ccb)
991 /* Caller will release the CCB */
992 wakeup(&done_ccb->ccb_h.cbfcnp);
996 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
1000 unit = minor(dev) & 0xff;
1003 * Only allow read-write access.
1005 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
1009 * We don't allow nonblocking access.
1011 if ((flags & O_NONBLOCK) != 0) {
1012 printf("xpt%d: can't do nonblocking access\n", unit);
1017 * We only have one transport layer right now. If someone accesses
1018 * us via something other than minor number 1, point out their
1022 printf("xptopen: got invalid xpt unit %d\n", unit);
1026 /* Mark ourselves open */
1027 xsoftc.flags |= XPT_FLAG_OPEN;
1033 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1037 unit = minor(dev) & 0xff;
1040 * We only have one transport layer right now. If someone accesses
1041 * us via something other than minor number 1, point out their
1045 printf("xptclose: got invalid xpt unit %d\n", unit);
1049 /* Mark ourselves closed */
1050 xsoftc.flags &= ~XPT_FLAG_OPEN;
1056 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1061 unit = minor(dev) & 0xff;
1064 * We only have one transport layer right now. If someone accesses
1065 * us via something other than minor number 1, point out their
1069 printf("xptioctl: got invalid xpt unit %d\n", unit);
1075 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1076 * to accept CCB types that don't quite make sense to send through a
1077 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1080 case CAMIOCOMMAND: {
1084 inccb = (union ccb *)addr;
1086 switch(inccb->ccb_h.func_code) {
1089 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1090 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1099 ccb = xpt_alloc_ccb();
1102 * Create a path using the bus, target, and lun the
1105 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1106 inccb->ccb_h.path_id,
1107 inccb->ccb_h.target_id,
1108 inccb->ccb_h.target_lun) !=
1114 /* Ensure all of our fields are correct */
1115 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1116 inccb->ccb_h.pinfo.priority);
1117 xpt_merge_ccb(ccb, inccb);
1118 ccb->ccb_h.cbfcnp = xptdone;
1119 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1120 bcopy(ccb, inccb, sizeof(union ccb));
1121 xpt_free_path(ccb->ccb_h.path);
1129 * This is an immediate CCB, so it's okay to
1130 * allocate it on the stack.
1134 * Create a path using the bus, target, and lun the
1137 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1138 inccb->ccb_h.path_id,
1139 inccb->ccb_h.target_id,
1140 inccb->ccb_h.target_lun) !=
1145 /* Ensure all of our fields are correct */
1146 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1147 inccb->ccb_h.pinfo.priority);
1148 xpt_merge_ccb(&ccb, inccb);
1149 ccb.ccb_h.cbfcnp = xptdone;
1151 bcopy(&ccb, inccb, sizeof(union ccb));
1152 xpt_free_path(ccb.ccb_h.path);
1156 case XPT_DEV_MATCH: {
1157 struct cam_periph_map_info mapinfo;
1158 struct cam_path *old_path;
1161 * We can't deal with physical addresses for this
1162 * type of transaction.
1164 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1170 * Save this in case the caller had it set to
1171 * something in particular.
1173 old_path = inccb->ccb_h.path;
1176 * We really don't need a path for the matching
1177 * code. The path is needed because of the
1178 * debugging statements in xpt_action(). They
1179 * assume that the CCB has a valid path.
1181 inccb->ccb_h.path = xpt_periph->path;
1183 bzero(&mapinfo, sizeof(mapinfo));
1186 * Map the pattern and match buffers into kernel
1187 * virtual address space.
1189 error = cam_periph_mapmem(inccb, &mapinfo);
1192 inccb->ccb_h.path = old_path;
1197 * This is an immediate CCB, we can send it on directly.
1202 * Map the buffers back into user space.
1204 cam_periph_unmapmem(inccb, &mapinfo);
1206 inccb->ccb_h.path = old_path;
1218 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1219 * with the periphal driver name and unit name filled in. The other
1220 * fields don't really matter as input. The passthrough driver name
1221 * ("pass"), and unit number are passed back in the ccb. The current
1222 * device generation number, and the index into the device peripheral
1223 * driver list, and the status are also passed back. Note that
1224 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1225 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1226 * (or rather should be) impossible for the device peripheral driver
1227 * list to change since we look at the whole thing in one pass, and
1228 * we do it with splcam protection.
1231 case CAMGETPASSTHRU: {
1233 struct cam_periph *periph;
1234 struct periph_driver **p_drv;
1237 u_int cur_generation;
1238 int base_periph_found;
1242 ccb = (union ccb *)addr;
1243 unit = ccb->cgdl.unit_number;
1244 name = ccb->cgdl.periph_name;
1246 * Every 100 devices, we want to drop our spl protection to
1247 * give the software interrupt handler a chance to run.
1248 * Most systems won't run into this check, but this should
1249 * avoid starvation in the software interrupt handler in
1254 ccb = (union ccb *)addr;
1256 base_periph_found = 0;
1259 * Sanity check -- make sure we don't get a null peripheral
1262 if (*ccb->cgdl.periph_name == '\0') {
1267 /* Keep the list from changing while we traverse it */
1270 cur_generation = xsoftc.generation;
1272 /* first find our driver in the list of drivers */
1273 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1274 if (strcmp((*p_drv)->driver_name, name) == 0)
1277 if (*p_drv == NULL) {
1279 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1280 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1281 *ccb->cgdl.periph_name = '\0';
1282 ccb->cgdl.unit_number = 0;
1288 * Run through every peripheral instance of this driver
1289 * and check to see whether it matches the unit passed
1290 * in by the user. If it does, get out of the loops and
1291 * find the passthrough driver associated with that
1292 * peripheral driver.
1294 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1295 periph = TAILQ_NEXT(periph, unit_links)) {
1297 if (periph->unit_number == unit) {
1299 } else if (--splbreaknum == 0) {
1303 if (cur_generation != xsoftc.generation)
1308 * If we found the peripheral driver that the user passed
1309 * in, go through all of the peripheral drivers for that
1310 * particular device and look for a passthrough driver.
1312 if (periph != NULL) {
1313 struct cam_ed *device;
1316 base_periph_found = 1;
1317 device = periph->path->device;
1318 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1320 periph = SLIST_NEXT(periph, periph_links), i++) {
1322 * Check to see whether we have a
1323 * passthrough device or not.
1325 if (strcmp(periph->periph_name, "pass") == 0) {
1327 * Fill in the getdevlist fields.
1329 strcpy(ccb->cgdl.periph_name,
1330 periph->periph_name);
1331 ccb->cgdl.unit_number =
1332 periph->unit_number;
1333 if (SLIST_NEXT(periph, periph_links))
1335 CAM_GDEVLIST_MORE_DEVS;
1338 CAM_GDEVLIST_LAST_DEVICE;
1339 ccb->cgdl.generation =
1341 ccb->cgdl.index = i;
1343 * Fill in some CCB header fields
1344 * that the user may want.
1346 ccb->ccb_h.path_id =
1347 periph->path->bus->path_id;
1348 ccb->ccb_h.target_id =
1349 periph->path->target->target_id;
1350 ccb->ccb_h.target_lun =
1351 periph->path->device->lun_id;
1352 ccb->ccb_h.status = CAM_REQ_CMP;
1359 * If the periph is null here, one of two things has
1360 * happened. The first possibility is that we couldn't
1361 * find the unit number of the particular peripheral driver
1362 * that the user is asking about. e.g. the user asks for
1363 * the passthrough driver for "da11". We find the list of
1364 * "da" peripherals all right, but there is no unit 11.
1365 * The other possibility is that we went through the list
1366 * of peripheral drivers attached to the device structure,
1367 * but didn't find one with the name "pass". Either way,
1368 * we return ENOENT, since we couldn't find something.
1370 if (periph == NULL) {
1371 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1372 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1373 *ccb->cgdl.periph_name = '\0';
1374 ccb->cgdl.unit_number = 0;
1377 * It is unfortunate that this is even necessary,
1378 * but there are many, many clueless users out there.
1379 * If this is true, the user is looking for the
1380 * passthrough driver, but doesn't have one in his
1383 if (base_periph_found == 1) {
1384 printf("xptioctl: pass driver is not in the "
1386 printf("xptioctl: put \"device pass0\" in "
1387 "your kernel config file\n");
1402 cam_module_event_handler(module_t mod, int what, void *arg)
1404 if (what == MOD_LOAD) {
1406 } else if (what == MOD_UNLOAD) {
1415 /* Functions accessed by the peripheral drivers */
1420 struct cam_sim *xpt_sim;
1421 struct cam_path *path;
1422 struct cam_devq *devq;
1425 TAILQ_INIT(&xpt_busses);
1426 TAILQ_INIT(&cam_bioq);
1427 SLIST_INIT(&ccb_freeq);
1428 STAILQ_INIT(&highpowerq);
1430 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1433 * The xpt layer is, itself, the equivelent of a SIM.
1434 * Allow 16 ccbs in the ccb pool for it. This should
1435 * give decent parallelism when we probe busses and
1436 * perform other XPT functions.
1438 devq = cam_simq_alloc(16);
1439 xpt_sim = cam_sim_alloc(xptaction,
1444 /*max_dev_transactions*/0,
1445 /*max_tagged_dev_transactions*/0,
1449 xpt_bus_register(xpt_sim, /*bus #*/0);
1452 * Looking at the XPT from the SIM layer, the XPT is
1453 * the equivelent of a peripheral driver. Allocate
1454 * a peripheral driver entry for us.
1456 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1457 CAM_TARGET_WILDCARD,
1458 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1459 printf("xpt_init: xpt_create_path failed with status %#x,"
1460 " failing attach\n", status);
1464 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1465 path, NULL, 0, NULL);
1466 xpt_free_path(path);
1468 xpt_sim->softc = xpt_periph;
1471 * Register a callback for when interrupts are enabled.
1474 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1475 M_TEMP, M_NOWAIT | M_ZERO);
1476 if (xpt_config_hook == NULL) {
1477 printf("xpt_init: Cannot malloc config hook "
1478 "- failing attach\n");
1482 xpt_config_hook->ich_func = xpt_config;
1483 if (config_intrhook_establish(xpt_config_hook) != 0) {
1484 free (xpt_config_hook, M_TEMP);
1485 printf("xpt_init: config_intrhook_establish failed "
1486 "- failing attach\n");
1489 /* Install our software interrupt handlers */
1490 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1494 xptregister(struct cam_periph *periph, void *arg)
1496 if (periph == NULL) {
1497 printf("xptregister: periph was NULL!!\n");
1498 return(CAM_REQ_CMP_ERR);
1501 periph->softc = NULL;
1503 xpt_periph = periph;
1505 return(CAM_REQ_CMP);
1509 xpt_add_periph(struct cam_periph *periph)
1511 struct cam_ed *device;
1513 struct periph_list *periph_head;
1517 device = periph->path->device;
1519 periph_head = &device->periphs;
1521 status = CAM_REQ_CMP;
1523 if (device != NULL) {
1527 * Make room for this peripheral
1528 * so it will fit in the queue
1529 * when it's scheduled to run
1532 status = camq_resize(&device->drvq,
1533 device->drvq.array_size + 1);
1535 device->generation++;
1537 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1542 xsoftc.generation++;
1548 xpt_remove_periph(struct cam_periph *periph)
1550 struct cam_ed *device;
1554 device = periph->path->device;
1556 if (device != NULL) {
1558 struct periph_list *periph_head;
1560 periph_head = &device->periphs;
1562 /* Release the slot for this peripheral */
1564 camq_resize(&device->drvq, device->drvq.array_size - 1);
1566 device->generation++;
1568 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1573 xsoftc.generation++;
1579 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1581 struct ccb_pathinq cpi;
1582 struct ccb_trans_settings cts;
1583 struct cam_path *path;
1591 path = periph->path;
1593 * To ensure that this is printed in one piece,
1594 * mask out CAM interrupts.
1597 printf("%s%d at %s%d bus %d target %d lun %d\n",
1598 periph->periph_name, periph->unit_number,
1599 path->bus->sim->sim_name,
1600 path->bus->sim->unit_number,
1601 path->bus->sim->bus_id,
1602 path->target->target_id,
1603 path->device->lun_id);
1604 printf("%s%d: ", periph->periph_name, periph->unit_number);
1605 scsi_print_inquiry(&path->device->inq_data);
1606 if (bootverbose && path->device->serial_num_len > 0) {
1607 /* Don't wrap the screen - print only the first 60 chars */
1608 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1609 periph->unit_number, path->device->serial_num);
1611 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1612 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1613 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1614 xpt_action((union ccb*)&cts);
1615 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1619 /* Ask the SIM for its base transfer speed */
1620 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1621 cpi.ccb_h.func_code = XPT_PATH_INQ;
1622 xpt_action((union ccb *)&cpi);
1624 speed = cpi.base_transfer_speed;
1626 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1627 struct ccb_trans_settings_spi *spi;
1629 spi = &cts.xport_specific.spi;
1630 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1631 && spi->sync_offset != 0) {
1632 freq = scsi_calc_syncsrate(spi->sync_period);
1636 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1637 speed *= (0x01 << spi->bus_width);
1640 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1641 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1642 if (fc->valid & CTS_FC_VALID_SPEED) {
1643 speed = fc->bitrate;
1647 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1648 struct ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1649 if (sas->valid & CTS_SAS_VALID_SPEED) {
1650 speed = sas->bitrate;
1656 printf("%s%d: %d.%03dMB/s transfers",
1657 periph->periph_name, periph->unit_number,
1660 printf("%s%d: %dKB/s transfers", periph->periph_name,
1661 periph->unit_number, speed);
1662 /* Report additional information about SPI connections */
1663 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1664 struct ccb_trans_settings_spi *spi;
1666 spi = &cts.xport_specific.spi;
1668 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1670 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1674 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1675 && spi->bus_width > 0) {
1681 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1682 } else if (freq != 0) {
1686 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1687 struct ccb_trans_settings_fc *fc;
1689 fc = &cts.xport_specific.fc;
1690 if (fc->valid & CTS_FC_VALID_WWNN)
1691 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1692 if (fc->valid & CTS_FC_VALID_WWPN)
1693 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1694 if (fc->valid & CTS_FC_VALID_PORT)
1695 printf(" PortID 0x%x", fc->port);
1698 if (path->device->inq_flags & SID_CmdQue
1699 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1700 printf("\n%s%d: Command Queueing Enabled",
1701 periph->periph_name, periph->unit_number);
1706 * We only want to print the caller's announce string if they've
1709 if (announce_string != NULL)
1710 printf("%s%d: %s\n", periph->periph_name,
1711 periph->unit_number, announce_string);
1715 static dev_match_ret
1716 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1719 dev_match_ret retval;
1722 retval = DM_RET_NONE;
1725 * If we aren't given something to match against, that's an error.
1728 return(DM_RET_ERROR);
1731 * If there are no match entries, then this bus matches no
1734 if ((patterns == NULL) || (num_patterns == 0))
1735 return(DM_RET_DESCEND | DM_RET_COPY);
1737 for (i = 0; i < num_patterns; i++) {
1738 struct bus_match_pattern *cur_pattern;
1741 * If the pattern in question isn't for a bus node, we
1742 * aren't interested. However, we do indicate to the
1743 * calling routine that we should continue descending the
1744 * tree, since the user wants to match against lower-level
1747 if (patterns[i].type != DEV_MATCH_BUS) {
1748 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1749 retval |= DM_RET_DESCEND;
1753 cur_pattern = &patterns[i].pattern.bus_pattern;
1756 * If they want to match any bus node, we give them any
1759 if (cur_pattern->flags == BUS_MATCH_ANY) {
1760 /* set the copy flag */
1761 retval |= DM_RET_COPY;
1764 * If we've already decided on an action, go ahead
1767 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1772 * Not sure why someone would do this...
1774 if (cur_pattern->flags == BUS_MATCH_NONE)
1777 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1778 && (cur_pattern->path_id != bus->path_id))
1781 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1782 && (cur_pattern->bus_id != bus->sim->bus_id))
1785 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1786 && (cur_pattern->unit_number != bus->sim->unit_number))
1789 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1790 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1795 * If we get to this point, the user definitely wants
1796 * information on this bus. So tell the caller to copy the
1799 retval |= DM_RET_COPY;
1802 * If the return action has been set to descend, then we
1803 * know that we've already seen a non-bus matching
1804 * expression, therefore we need to further descend the tree.
1805 * This won't change by continuing around the loop, so we
1806 * go ahead and return. If we haven't seen a non-bus
1807 * matching expression, we keep going around the loop until
1808 * we exhaust the matching expressions. We'll set the stop
1809 * flag once we fall out of the loop.
1811 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1816 * If the return action hasn't been set to descend yet, that means
1817 * we haven't seen anything other than bus matching patterns. So
1818 * tell the caller to stop descending the tree -- the user doesn't
1819 * want to match against lower level tree elements.
1821 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1822 retval |= DM_RET_STOP;
1827 static dev_match_ret
1828 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1829 struct cam_ed *device)
1831 dev_match_ret retval;
1834 retval = DM_RET_NONE;
1837 * If we aren't given something to match against, that's an error.
1840 return(DM_RET_ERROR);
1843 * If there are no match entries, then this device matches no
1846 if ((patterns == NULL) || (num_patterns == 0))
1847 return(DM_RET_DESCEND | DM_RET_COPY);
1849 for (i = 0; i < num_patterns; i++) {
1850 struct device_match_pattern *cur_pattern;
1853 * If the pattern in question isn't for a device node, we
1854 * aren't interested.
1856 if (patterns[i].type != DEV_MATCH_DEVICE) {
1857 if ((patterns[i].type == DEV_MATCH_PERIPH)
1858 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1859 retval |= DM_RET_DESCEND;
1863 cur_pattern = &patterns[i].pattern.device_pattern;
1866 * If they want to match any device node, we give them any
1869 if (cur_pattern->flags == DEV_MATCH_ANY) {
1870 /* set the copy flag */
1871 retval |= DM_RET_COPY;
1875 * If we've already decided on an action, go ahead
1878 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1883 * Not sure why someone would do this...
1885 if (cur_pattern->flags == DEV_MATCH_NONE)
1888 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1889 && (cur_pattern->path_id != device->target->bus->path_id))
1892 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1893 && (cur_pattern->target_id != device->target->target_id))
1896 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1897 && (cur_pattern->target_lun != device->lun_id))
1900 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1901 && (cam_quirkmatch((caddr_t)&device->inq_data,
1902 (caddr_t)&cur_pattern->inq_pat,
1903 1, sizeof(cur_pattern->inq_pat),
1904 scsi_static_inquiry_match) == NULL))
1908 * If we get to this point, the user definitely wants
1909 * information on this device. So tell the caller to copy
1912 retval |= DM_RET_COPY;
1915 * If the return action has been set to descend, then we
1916 * know that we've already seen a peripheral matching
1917 * expression, therefore we need to further descend the tree.
1918 * This won't change by continuing around the loop, so we
1919 * go ahead and return. If we haven't seen a peripheral
1920 * matching expression, we keep going around the loop until
1921 * we exhaust the matching expressions. We'll set the stop
1922 * flag once we fall out of the loop.
1924 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1929 * If the return action hasn't been set to descend yet, that means
1930 * we haven't seen any peripheral matching patterns. So tell the
1931 * caller to stop descending the tree -- the user doesn't want to
1932 * match against lower level tree elements.
1934 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1935 retval |= DM_RET_STOP;
1941 * Match a single peripheral against any number of match patterns.
1943 static dev_match_ret
1944 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1945 struct cam_periph *periph)
1947 dev_match_ret retval;
1951 * If we aren't given something to match against, that's an error.
1954 return(DM_RET_ERROR);
1957 * If there are no match entries, then this peripheral matches no
1960 if ((patterns == NULL) || (num_patterns == 0))
1961 return(DM_RET_STOP | DM_RET_COPY);
1964 * There aren't any nodes below a peripheral node, so there's no
1965 * reason to descend the tree any further.
1967 retval = DM_RET_STOP;
1969 for (i = 0; i < num_patterns; i++) {
1970 struct periph_match_pattern *cur_pattern;
1973 * If the pattern in question isn't for a peripheral, we
1974 * aren't interested.
1976 if (patterns[i].type != DEV_MATCH_PERIPH)
1979 cur_pattern = &patterns[i].pattern.periph_pattern;
1982 * If they want to match on anything, then we will do so.
1984 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1985 /* set the copy flag */
1986 retval |= DM_RET_COPY;
1989 * We've already set the return action to stop,
1990 * since there are no nodes below peripherals in
1997 * Not sure why someone would do this...
1999 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2002 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2003 && (cur_pattern->path_id != periph->path->bus->path_id))
2007 * For the target and lun id's, we have to make sure the
2008 * target and lun pointers aren't NULL. The xpt peripheral
2009 * has a wildcard target and device.
2011 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2012 && ((periph->path->target == NULL)
2013 ||(cur_pattern->target_id != periph->path->target->target_id)))
2016 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2017 && ((periph->path->device == NULL)
2018 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2021 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2022 && (cur_pattern->unit_number != periph->unit_number))
2025 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2026 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2031 * If we get to this point, the user definitely wants
2032 * information on this peripheral. So tell the caller to
2033 * copy the data out.
2035 retval |= DM_RET_COPY;
2038 * The return action has already been set to stop, since
2039 * peripherals don't have any nodes below them in the EDT.
2045 * If we get to this point, the peripheral that was passed in
2046 * doesn't match any of the patterns.
2052 xptedtbusfunc(struct cam_eb *bus, void *arg)
2054 struct ccb_dev_match *cdm;
2055 dev_match_ret retval;
2057 cdm = (struct ccb_dev_match *)arg;
2060 * If our position is for something deeper in the tree, that means
2061 * that we've already seen this node. So, we keep going down.
2063 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2064 && (cdm->pos.cookie.bus == bus)
2065 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2066 && (cdm->pos.cookie.target != NULL))
2067 retval = DM_RET_DESCEND;
2069 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2072 * If we got an error, bail out of the search.
2074 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2075 cdm->status = CAM_DEV_MATCH_ERROR;
2080 * If the copy flag is set, copy this bus out.
2082 if (retval & DM_RET_COPY) {
2085 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2086 sizeof(struct dev_match_result));
2089 * If we don't have enough space to put in another
2090 * match result, save our position and tell the
2091 * user there are more devices to check.
2093 if (spaceleft < sizeof(struct dev_match_result)) {
2094 bzero(&cdm->pos, sizeof(cdm->pos));
2095 cdm->pos.position_type =
2096 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2098 cdm->pos.cookie.bus = bus;
2099 cdm->pos.generations[CAM_BUS_GENERATION]=
2101 cdm->status = CAM_DEV_MATCH_MORE;
2104 j = cdm->num_matches;
2106 cdm->matches[j].type = DEV_MATCH_BUS;
2107 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2108 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2109 cdm->matches[j].result.bus_result.unit_number =
2110 bus->sim->unit_number;
2111 strncpy(cdm->matches[j].result.bus_result.dev_name,
2112 bus->sim->sim_name, DEV_IDLEN);
2116 * If the user is only interested in busses, there's no
2117 * reason to descend to the next level in the tree.
2119 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2123 * If there is a target generation recorded, check it to
2124 * make sure the target list hasn't changed.
2126 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2127 && (bus == cdm->pos.cookie.bus)
2128 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2129 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2130 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2132 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2136 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2137 && (cdm->pos.cookie.bus == bus)
2138 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2139 && (cdm->pos.cookie.target != NULL))
2140 return(xpttargettraverse(bus,
2141 (struct cam_et *)cdm->pos.cookie.target,
2142 xptedttargetfunc, arg));
2144 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2148 xptedttargetfunc(struct cam_et *target, void *arg)
2150 struct ccb_dev_match *cdm;
2152 cdm = (struct ccb_dev_match *)arg;
2155 * If there is a device list generation recorded, check it to
2156 * make sure the device list hasn't changed.
2158 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2159 && (cdm->pos.cookie.bus == target->bus)
2160 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2161 && (cdm->pos.cookie.target == target)
2162 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2163 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2164 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2165 target->generation)) {
2166 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2170 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2171 && (cdm->pos.cookie.bus == target->bus)
2172 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2173 && (cdm->pos.cookie.target == target)
2174 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2175 && (cdm->pos.cookie.device != NULL))
2176 return(xptdevicetraverse(target,
2177 (struct cam_ed *)cdm->pos.cookie.device,
2178 xptedtdevicefunc, arg));
2180 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2184 xptedtdevicefunc(struct cam_ed *device, void *arg)
2187 struct ccb_dev_match *cdm;
2188 dev_match_ret retval;
2190 cdm = (struct ccb_dev_match *)arg;
2193 * If our position is for something deeper in the tree, that means
2194 * that we've already seen this node. So, we keep going down.
2196 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2197 && (cdm->pos.cookie.device == device)
2198 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2199 && (cdm->pos.cookie.periph != NULL))
2200 retval = DM_RET_DESCEND;
2202 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2205 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2206 cdm->status = CAM_DEV_MATCH_ERROR;
2211 * If the copy flag is set, copy this device out.
2213 if (retval & DM_RET_COPY) {
2216 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2217 sizeof(struct dev_match_result));
2220 * If we don't have enough space to put in another
2221 * match result, save our position and tell the
2222 * user there are more devices to check.
2224 if (spaceleft < sizeof(struct dev_match_result)) {
2225 bzero(&cdm->pos, sizeof(cdm->pos));
2226 cdm->pos.position_type =
2227 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2228 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2230 cdm->pos.cookie.bus = device->target->bus;
2231 cdm->pos.generations[CAM_BUS_GENERATION]=
2233 cdm->pos.cookie.target = device->target;
2234 cdm->pos.generations[CAM_TARGET_GENERATION] =
2235 device->target->bus->generation;
2236 cdm->pos.cookie.device = device;
2237 cdm->pos.generations[CAM_DEV_GENERATION] =
2238 device->target->generation;
2239 cdm->status = CAM_DEV_MATCH_MORE;
2242 j = cdm->num_matches;
2244 cdm->matches[j].type = DEV_MATCH_DEVICE;
2245 cdm->matches[j].result.device_result.path_id =
2246 device->target->bus->path_id;
2247 cdm->matches[j].result.device_result.target_id =
2248 device->target->target_id;
2249 cdm->matches[j].result.device_result.target_lun =
2251 bcopy(&device->inq_data,
2252 &cdm->matches[j].result.device_result.inq_data,
2253 sizeof(struct scsi_inquiry_data));
2255 /* Let the user know whether this device is unconfigured */
2256 if (device->flags & CAM_DEV_UNCONFIGURED)
2257 cdm->matches[j].result.device_result.flags =
2258 DEV_RESULT_UNCONFIGURED;
2260 cdm->matches[j].result.device_result.flags =
2265 * If the user isn't interested in peripherals, don't descend
2266 * the tree any further.
2268 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2272 * If there is a peripheral list generation recorded, make sure
2273 * it hasn't changed.
2275 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2276 && (device->target->bus == cdm->pos.cookie.bus)
2277 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2278 && (device->target == cdm->pos.cookie.target)
2279 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2280 && (device == cdm->pos.cookie.device)
2281 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2282 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2283 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2284 device->generation)){
2285 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2289 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2290 && (cdm->pos.cookie.bus == device->target->bus)
2291 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2292 && (cdm->pos.cookie.target == device->target)
2293 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2294 && (cdm->pos.cookie.device == device)
2295 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2296 && (cdm->pos.cookie.periph != NULL))
2297 return(xptperiphtraverse(device,
2298 (struct cam_periph *)cdm->pos.cookie.periph,
2299 xptedtperiphfunc, arg));
2301 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2305 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2307 struct ccb_dev_match *cdm;
2308 dev_match_ret retval;
2310 cdm = (struct ccb_dev_match *)arg;
2312 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2314 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2315 cdm->status = CAM_DEV_MATCH_ERROR;
2320 * If the copy flag is set, copy this peripheral out.
2322 if (retval & DM_RET_COPY) {
2325 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2326 sizeof(struct dev_match_result));
2329 * If we don't have enough space to put in another
2330 * match result, save our position and tell the
2331 * user there are more devices to check.
2333 if (spaceleft < sizeof(struct dev_match_result)) {
2334 bzero(&cdm->pos, sizeof(cdm->pos));
2335 cdm->pos.position_type =
2336 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2337 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2340 cdm->pos.cookie.bus = periph->path->bus;
2341 cdm->pos.generations[CAM_BUS_GENERATION]=
2343 cdm->pos.cookie.target = periph->path->target;
2344 cdm->pos.generations[CAM_TARGET_GENERATION] =
2345 periph->path->bus->generation;
2346 cdm->pos.cookie.device = periph->path->device;
2347 cdm->pos.generations[CAM_DEV_GENERATION] =
2348 periph->path->target->generation;
2349 cdm->pos.cookie.periph = periph;
2350 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2351 periph->path->device->generation;
2352 cdm->status = CAM_DEV_MATCH_MORE;
2356 j = cdm->num_matches;
2358 cdm->matches[j].type = DEV_MATCH_PERIPH;
2359 cdm->matches[j].result.periph_result.path_id =
2360 periph->path->bus->path_id;
2361 cdm->matches[j].result.periph_result.target_id =
2362 periph->path->target->target_id;
2363 cdm->matches[j].result.periph_result.target_lun =
2364 periph->path->device->lun_id;
2365 cdm->matches[j].result.periph_result.unit_number =
2366 periph->unit_number;
2367 strncpy(cdm->matches[j].result.periph_result.periph_name,
2368 periph->periph_name, DEV_IDLEN);
2375 xptedtmatch(struct ccb_dev_match *cdm)
2379 cdm->num_matches = 0;
2382 * Check the bus list generation. If it has changed, the user
2383 * needs to reset everything and start over.
2385 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2386 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2387 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2388 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2392 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2393 && (cdm->pos.cookie.bus != NULL))
2394 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2395 xptedtbusfunc, cdm);
2397 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2400 * If we get back 0, that means that we had to stop before fully
2401 * traversing the EDT. It also means that one of the subroutines
2402 * has set the status field to the proper value. If we get back 1,
2403 * we've fully traversed the EDT and copied out any matching entries.
2406 cdm->status = CAM_DEV_MATCH_LAST;
2412 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2414 struct ccb_dev_match *cdm;
2416 cdm = (struct ccb_dev_match *)arg;
2418 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2419 && (cdm->pos.cookie.pdrv == pdrv)
2420 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2421 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2422 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2423 (*pdrv)->generation)) {
2424 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2428 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2429 && (cdm->pos.cookie.pdrv == pdrv)
2430 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2431 && (cdm->pos.cookie.periph != NULL))
2432 return(xptpdperiphtraverse(pdrv,
2433 (struct cam_periph *)cdm->pos.cookie.periph,
2434 xptplistperiphfunc, arg));
2436 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2440 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2442 struct ccb_dev_match *cdm;
2443 dev_match_ret retval;
2445 cdm = (struct ccb_dev_match *)arg;
2447 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2449 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2450 cdm->status = CAM_DEV_MATCH_ERROR;
2455 * If the copy flag is set, copy this peripheral out.
2457 if (retval & DM_RET_COPY) {
2460 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2461 sizeof(struct dev_match_result));
2464 * If we don't have enough space to put in another
2465 * match result, save our position and tell the
2466 * user there are more devices to check.
2468 if (spaceleft < sizeof(struct dev_match_result)) {
2469 struct periph_driver **pdrv;
2472 bzero(&cdm->pos, sizeof(cdm->pos));
2473 cdm->pos.position_type =
2474 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2478 * This may look a bit non-sensical, but it is
2479 * actually quite logical. There are very few
2480 * peripheral drivers, and bloating every peripheral
2481 * structure with a pointer back to its parent
2482 * peripheral driver linker set entry would cost
2483 * more in the long run than doing this quick lookup.
2485 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2486 if (strcmp((*pdrv)->driver_name,
2487 periph->periph_name) == 0)
2491 if (*pdrv == NULL) {
2492 cdm->status = CAM_DEV_MATCH_ERROR;
2496 cdm->pos.cookie.pdrv = pdrv;
2498 * The periph generation slot does double duty, as
2499 * does the periph pointer slot. They are used for
2500 * both edt and pdrv lookups and positioning.
2502 cdm->pos.cookie.periph = periph;
2503 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2504 (*pdrv)->generation;
2505 cdm->status = CAM_DEV_MATCH_MORE;
2509 j = cdm->num_matches;
2511 cdm->matches[j].type = DEV_MATCH_PERIPH;
2512 cdm->matches[j].result.periph_result.path_id =
2513 periph->path->bus->path_id;
2516 * The transport layer peripheral doesn't have a target or
2519 if (periph->path->target)
2520 cdm->matches[j].result.periph_result.target_id =
2521 periph->path->target->target_id;
2523 cdm->matches[j].result.periph_result.target_id = -1;
2525 if (periph->path->device)
2526 cdm->matches[j].result.periph_result.target_lun =
2527 periph->path->device->lun_id;
2529 cdm->matches[j].result.periph_result.target_lun = -1;
2531 cdm->matches[j].result.periph_result.unit_number =
2532 periph->unit_number;
2533 strncpy(cdm->matches[j].result.periph_result.periph_name,
2534 periph->periph_name, DEV_IDLEN);
2541 xptperiphlistmatch(struct ccb_dev_match *cdm)
2545 cdm->num_matches = 0;
2548 * At this point in the edt traversal function, we check the bus
2549 * list generation to make sure that no busses have been added or
2550 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2551 * For the peripheral driver list traversal function, however, we
2552 * don't have to worry about new peripheral driver types coming or
2553 * going; they're in a linker set, and therefore can't change
2554 * without a recompile.
2557 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2558 && (cdm->pos.cookie.pdrv != NULL))
2559 ret = xptpdrvtraverse(
2560 (struct periph_driver **)cdm->pos.cookie.pdrv,
2561 xptplistpdrvfunc, cdm);
2563 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2566 * If we get back 0, that means that we had to stop before fully
2567 * traversing the peripheral driver tree. It also means that one of
2568 * the subroutines has set the status field to the proper value. If
2569 * we get back 1, we've fully traversed the EDT and copied out any
2573 cdm->status = CAM_DEV_MATCH_LAST;
2579 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2581 struct cam_eb *bus, *next_bus;
2586 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2589 next_bus = TAILQ_NEXT(bus, links);
2591 retval = tr_func(bus, arg);
2600 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2601 xpt_targetfunc_t *tr_func, void *arg)
2603 struct cam_et *target, *next_target;
2607 for (target = (start_target ? start_target :
2608 TAILQ_FIRST(&bus->et_entries));
2609 target != NULL; target = next_target) {
2611 next_target = TAILQ_NEXT(target, links);
2613 retval = tr_func(target, arg);
2623 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2624 xpt_devicefunc_t *tr_func, void *arg)
2626 struct cam_ed *device, *next_device;
2630 for (device = (start_device ? start_device :
2631 TAILQ_FIRST(&target->ed_entries));
2633 device = next_device) {
2635 next_device = TAILQ_NEXT(device, links);
2637 retval = tr_func(device, arg);
2647 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2648 xpt_periphfunc_t *tr_func, void *arg)
2650 struct cam_periph *periph, *next_periph;
2655 for (periph = (start_periph ? start_periph :
2656 SLIST_FIRST(&device->periphs));
2658 periph = next_periph) {
2660 next_periph = SLIST_NEXT(periph, periph_links);
2662 retval = tr_func(periph, arg);
2671 xptpdrvtraverse(struct periph_driver **start_pdrv,
2672 xpt_pdrvfunc_t *tr_func, void *arg)
2674 struct periph_driver **pdrv;
2680 * We don't traverse the peripheral driver list like we do the
2681 * other lists, because it is a linker set, and therefore cannot be
2682 * changed during runtime. If the peripheral driver list is ever
2683 * re-done to be something other than a linker set (i.e. it can
2684 * change while the system is running), the list traversal should
2685 * be modified to work like the other traversal functions.
2687 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2688 *pdrv != NULL; pdrv++) {
2689 retval = tr_func(pdrv, arg);
2699 xptpdperiphtraverse(struct periph_driver **pdrv,
2700 struct cam_periph *start_periph,
2701 xpt_periphfunc_t *tr_func, void *arg)
2703 struct cam_periph *periph, *next_periph;
2708 for (periph = (start_periph ? start_periph :
2709 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2710 periph = next_periph) {
2712 next_periph = TAILQ_NEXT(periph, unit_links);
2714 retval = tr_func(periph, arg);
2722 xptdefbusfunc(struct cam_eb *bus, void *arg)
2724 struct xpt_traverse_config *tr_config;
2726 tr_config = (struct xpt_traverse_config *)arg;
2728 if (tr_config->depth == XPT_DEPTH_BUS) {
2729 xpt_busfunc_t *tr_func;
2731 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2733 return(tr_func(bus, tr_config->tr_arg));
2735 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2739 xptdeftargetfunc(struct cam_et *target, void *arg)
2741 struct xpt_traverse_config *tr_config;
2743 tr_config = (struct xpt_traverse_config *)arg;
2745 if (tr_config->depth == XPT_DEPTH_TARGET) {
2746 xpt_targetfunc_t *tr_func;
2748 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2750 return(tr_func(target, tr_config->tr_arg));
2752 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2756 xptdefdevicefunc(struct cam_ed *device, void *arg)
2758 struct xpt_traverse_config *tr_config;
2760 tr_config = (struct xpt_traverse_config *)arg;
2762 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2763 xpt_devicefunc_t *tr_func;
2765 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2767 return(tr_func(device, tr_config->tr_arg));
2769 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2773 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2775 struct xpt_traverse_config *tr_config;
2776 xpt_periphfunc_t *tr_func;
2778 tr_config = (struct xpt_traverse_config *)arg;
2780 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2783 * Unlike the other default functions, we don't check for depth
2784 * here. The peripheral driver level is the last level in the EDT,
2785 * so if we're here, we should execute the function in question.
2787 return(tr_func(periph, tr_config->tr_arg));
2791 * Execute the given function for every bus in the EDT.
2794 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2796 struct xpt_traverse_config tr_config;
2798 tr_config.depth = XPT_DEPTH_BUS;
2799 tr_config.tr_func = tr_func;
2800 tr_config.tr_arg = arg;
2802 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2807 * Execute the given function for every target in the EDT.
2810 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2812 struct xpt_traverse_config tr_config;
2814 tr_config.depth = XPT_DEPTH_TARGET;
2815 tr_config.tr_func = tr_func;
2816 tr_config.tr_arg = arg;
2818 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2820 #endif /* notusedyet */
2823 * Execute the given function for every device in the EDT.
2826 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2828 struct xpt_traverse_config tr_config;
2830 tr_config.depth = XPT_DEPTH_DEVICE;
2831 tr_config.tr_func = tr_func;
2832 tr_config.tr_arg = arg;
2834 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2839 * Execute the given function for every peripheral in the EDT.
2842 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2844 struct xpt_traverse_config tr_config;
2846 tr_config.depth = XPT_DEPTH_PERIPH;
2847 tr_config.tr_func = tr_func;
2848 tr_config.tr_arg = arg;
2850 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2852 #endif /* notusedyet */
2855 xptsetasyncfunc(struct cam_ed *device, void *arg)
2857 struct cam_path path;
2858 struct ccb_getdev cgd;
2859 struct async_node *cur_entry;
2861 cur_entry = (struct async_node *)arg;
2864 * Don't report unconfigured devices (Wildcard devs,
2865 * devices only for target mode, device instances
2866 * that have been invalidated but are waiting for
2867 * their last reference count to be released).
2869 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2872 xpt_compile_path(&path,
2874 device->target->bus->path_id,
2875 device->target->target_id,
2877 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2878 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2879 xpt_action((union ccb *)&cgd);
2880 cur_entry->callback(cur_entry->callback_arg,
2883 xpt_release_path(&path);
2889 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2891 struct cam_path path;
2892 struct ccb_pathinq cpi;
2893 struct async_node *cur_entry;
2895 cur_entry = (struct async_node *)arg;
2897 xpt_compile_path(&path, /*periph*/NULL,
2899 CAM_TARGET_WILDCARD,
2901 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2902 cpi.ccb_h.func_code = XPT_PATH_INQ;
2903 xpt_action((union ccb *)&cpi);
2904 cur_entry->callback(cur_entry->callback_arg,
2907 xpt_release_path(&path);
2913 xpt_action(union ccb *start_ccb)
2919 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2921 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2923 iopl = splsoftcam();
2924 switch (start_ccb->ccb_h.func_code) {
2927 struct cam_ed *device;
2929 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2930 struct cam_path *path;
2932 path = start_ccb->ccb_h.path;
2936 * For the sake of compatibility with SCSI-1
2937 * devices that may not understand the identify
2938 * message, we include lun information in the
2939 * second byte of all commands. SCSI-1 specifies
2940 * that luns are a 3 bit value and reserves only 3
2941 * bits for lun information in the CDB. Later
2942 * revisions of the SCSI spec allow for more than 8
2943 * luns, but have deprecated lun information in the
2944 * CDB. So, if the lun won't fit, we must omit.
2946 * Also be aware that during initial probing for devices,
2947 * the inquiry information is unknown but initialized to 0.
2948 * This means that this code will be exercised while probing
2949 * devices with an ANSI revision greater than 2.
2951 device = start_ccb->ccb_h.path->device;
2952 if (device->protocol_version <= SCSI_REV_2
2953 && start_ccb->ccb_h.target_lun < 8
2954 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2956 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2957 start_ccb->ccb_h.target_lun << 5;
2959 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2960 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2961 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2962 &path->device->inq_data),
2963 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2964 cdb_str, sizeof(cdb_str))));
2968 case XPT_CONT_TARGET_IO:
2969 start_ccb->csio.sense_resid = 0;
2970 start_ccb->csio.resid = 0;
2975 struct cam_path *path;
2976 struct cam_sim *sim;
2980 path = start_ccb->ccb_h.path;
2983 sim = path->bus->sim;
2984 if (SIM_DEAD(sim)) {
2985 /* The SIM has gone; just execute the CCB directly. */
2986 cam_ccbq_send_ccb(&path->device->ccbq, start_ccb);
2987 (*(sim->sim_action))(sim, start_ccb);
2992 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2993 if (path->device->qfrozen_cnt == 0)
2994 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2999 xpt_run_dev_sendq(path->bus);
3002 case XPT_SET_TRAN_SETTINGS:
3004 xpt_set_transfer_settings(&start_ccb->cts,
3005 start_ccb->ccb_h.path->device,
3006 /*async_update*/FALSE);
3009 case XPT_CALC_GEOMETRY:
3011 struct cam_sim *sim;
3013 /* Filter out garbage */
3014 if (start_ccb->ccg.block_size == 0
3015 || start_ccb->ccg.volume_size == 0) {
3016 start_ccb->ccg.cylinders = 0;
3017 start_ccb->ccg.heads = 0;
3018 start_ccb->ccg.secs_per_track = 0;
3019 start_ccb->ccb_h.status = CAM_REQ_CMP;
3024 * In a PC-98 system, geometry translation depens on
3025 * the "real" device geometry obtained from mode page 4.
3026 * SCSI geometry translation is performed in the
3027 * initialization routine of the SCSI BIOS and the result
3028 * stored in host memory. If the translation is available
3029 * in host memory, use it. If not, rely on the default
3030 * translation the device driver performs.
3032 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3033 start_ccb->ccb_h.status = CAM_REQ_CMP;
3037 sim = start_ccb->ccb_h.path->bus->sim;
3038 (*(sim->sim_action))(sim, start_ccb);
3043 union ccb* abort_ccb;
3046 abort_ccb = start_ccb->cab.abort_ccb;
3047 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3049 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3050 struct cam_ccbq *ccbq;
3052 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3053 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3054 abort_ccb->ccb_h.status =
3055 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3056 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3058 xpt_done(abort_ccb);
3060 start_ccb->ccb_h.status = CAM_REQ_CMP;
3063 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3064 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3066 * We've caught this ccb en route to
3067 * the SIM. Flag it for abort and the
3068 * SIM will do so just before starting
3069 * real work on the CCB.
3071 abort_ccb->ccb_h.status =
3072 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3073 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3074 start_ccb->ccb_h.status = CAM_REQ_CMP;
3078 if (XPT_FC_IS_QUEUED(abort_ccb)
3079 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3081 * It's already completed but waiting
3082 * for our SWI to get to it.
3084 start_ccb->ccb_h.status = CAM_UA_ABORT;
3088 * If we weren't able to take care of the abort request
3089 * in the XPT, pass the request down to the SIM for processing.
3093 case XPT_ACCEPT_TARGET_IO:
3095 case XPT_IMMED_NOTIFY:
3096 case XPT_NOTIFY_ACK:
3097 case XPT_GET_TRAN_SETTINGS:
3100 struct cam_sim *sim;
3102 sim = start_ccb->ccb_h.path->bus->sim;
3103 (*(sim->sim_action))(sim, start_ccb);
3108 struct cam_sim *sim;
3110 sim = start_ccb->ccb_h.path->bus->sim;
3111 (*(sim->sim_action))(sim, start_ccb);
3114 case XPT_PATH_STATS:
3115 start_ccb->cpis.last_reset =
3116 start_ccb->ccb_h.path->bus->last_reset;
3117 start_ccb->ccb_h.status = CAM_REQ_CMP;
3124 dev = start_ccb->ccb_h.path->device;
3126 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3127 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3129 struct ccb_getdev *cgd;
3133 cgd = &start_ccb->cgd;
3134 bus = cgd->ccb_h.path->bus;
3135 tar = cgd->ccb_h.path->target;
3136 cgd->inq_data = dev->inq_data;
3137 cgd->ccb_h.status = CAM_REQ_CMP;
3138 cgd->serial_num_len = dev->serial_num_len;
3139 if ((dev->serial_num_len > 0)
3140 && (dev->serial_num != NULL))
3141 bcopy(dev->serial_num, cgd->serial_num,
3142 dev->serial_num_len);
3147 case XPT_GDEV_STATS:
3152 dev = start_ccb->ccb_h.path->device;
3154 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3155 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3157 struct ccb_getdevstats *cgds;
3161 cgds = &start_ccb->cgds;
3162 bus = cgds->ccb_h.path->bus;
3163 tar = cgds->ccb_h.path->target;
3164 cgds->dev_openings = dev->ccbq.dev_openings;
3165 cgds->dev_active = dev->ccbq.dev_active;
3166 cgds->devq_openings = dev->ccbq.devq_openings;
3167 cgds->devq_queued = dev->ccbq.queue.entries;
3168 cgds->held = dev->ccbq.held;
3169 cgds->last_reset = tar->last_reset;
3170 cgds->maxtags = dev->quirk->maxtags;
3171 cgds->mintags = dev->quirk->mintags;
3172 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3173 cgds->last_reset = bus->last_reset;
3174 cgds->ccb_h.status = CAM_REQ_CMP;
3181 struct cam_periph *nperiph;
3182 struct periph_list *periph_head;
3183 struct ccb_getdevlist *cgdl;
3186 struct cam_ed *device;
3193 * Don't want anyone mucking with our data.
3196 device = start_ccb->ccb_h.path->device;
3197 periph_head = &device->periphs;
3198 cgdl = &start_ccb->cgdl;
3201 * Check and see if the list has changed since the user
3202 * last requested a list member. If so, tell them that the
3203 * list has changed, and therefore they need to start over
3204 * from the beginning.
3206 if ((cgdl->index != 0) &&
3207 (cgdl->generation != device->generation)) {
3208 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3214 * Traverse the list of peripherals and attempt to find
3215 * the requested peripheral.
3217 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3218 (nperiph != NULL) && (i <= cgdl->index);
3219 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3220 if (i == cgdl->index) {
3221 strncpy(cgdl->periph_name,
3222 nperiph->periph_name,
3224 cgdl->unit_number = nperiph->unit_number;
3229 cgdl->status = CAM_GDEVLIST_ERROR;
3234 if (nperiph == NULL)
3235 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3237 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3240 cgdl->generation = device->generation;
3243 cgdl->ccb_h.status = CAM_REQ_CMP;
3249 dev_pos_type position_type;
3250 struct ccb_dev_match *cdm;
3252 cdm = &start_ccb->cdm;
3255 * Prevent EDT changes while we traverse it.
3259 * There are two ways of getting at information in the EDT.
3260 * The first way is via the primary EDT tree. It starts
3261 * with a list of busses, then a list of targets on a bus,
3262 * then devices/luns on a target, and then peripherals on a
3263 * device/lun. The "other" way is by the peripheral driver
3264 * lists. The peripheral driver lists are organized by
3265 * peripheral driver. (obviously) So it makes sense to
3266 * use the peripheral driver list if the user is looking
3267 * for something like "da1", or all "da" devices. If the
3268 * user is looking for something on a particular bus/target
3269 * or lun, it's generally better to go through the EDT tree.
3272 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3273 position_type = cdm->pos.position_type;
3277 position_type = CAM_DEV_POS_NONE;
3279 for (i = 0; i < cdm->num_patterns; i++) {
3280 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3281 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3282 position_type = CAM_DEV_POS_EDT;
3287 if (cdm->num_patterns == 0)
3288 position_type = CAM_DEV_POS_EDT;
3289 else if (position_type == CAM_DEV_POS_NONE)
3290 position_type = CAM_DEV_POS_PDRV;
3293 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3294 case CAM_DEV_POS_EDT:
3297 case CAM_DEV_POS_PDRV:
3298 xptperiphlistmatch(cdm);
3301 cdm->status = CAM_DEV_MATCH_ERROR;
3307 if (cdm->status == CAM_DEV_MATCH_ERROR)
3308 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3310 start_ccb->ccb_h.status = CAM_REQ_CMP;
3316 struct ccb_setasync *csa;
3317 struct async_node *cur_entry;
3318 struct async_list *async_head;
3322 csa = &start_ccb->csa;
3323 added = csa->event_enable;
3324 async_head = &csa->ccb_h.path->device->asyncs;
3327 * If there is already an entry for us, simply
3331 cur_entry = SLIST_FIRST(async_head);
3332 while (cur_entry != NULL) {
3333 if ((cur_entry->callback_arg == csa->callback_arg)
3334 && (cur_entry->callback == csa->callback))
3336 cur_entry = SLIST_NEXT(cur_entry, links);
3339 if (cur_entry != NULL) {
3341 * If the request has no flags set,
3344 added &= ~cur_entry->event_enable;
3345 if (csa->event_enable == 0) {
3346 SLIST_REMOVE(async_head, cur_entry,
3348 csa->ccb_h.path->device->refcount--;
3349 free(cur_entry, M_CAMXPT);
3351 cur_entry->event_enable = csa->event_enable;
3354 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3356 if (cur_entry == NULL) {
3358 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3361 cur_entry->event_enable = csa->event_enable;
3362 cur_entry->callback_arg = csa->callback_arg;
3363 cur_entry->callback = csa->callback;
3364 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3365 csa->ccb_h.path->device->refcount++;
3368 if ((added & AC_FOUND_DEVICE) != 0) {
3370 * Get this peripheral up to date with all
3371 * the currently existing devices.
3373 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3375 if ((added & AC_PATH_REGISTERED) != 0) {
3377 * Get this peripheral up to date with all
3378 * the currently existing busses.
3380 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3383 start_ccb->ccb_h.status = CAM_REQ_CMP;
3388 struct ccb_relsim *crs;
3392 crs = &start_ccb->crs;
3393 dev = crs->ccb_h.path->device;
3396 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3402 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3404 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
3405 /* Don't ever go below one opening */
3406 if (crs->openings > 0) {
3407 xpt_dev_ccbq_resize(crs->ccb_h.path,
3411 xpt_print(crs->ccb_h.path,
3412 "tagged openings now %d\n",
3419 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3421 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3424 * Just extend the old timeout and decrement
3425 * the freeze count so that a single timeout
3426 * is sufficient for releasing the queue.
3428 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3429 untimeout(xpt_release_devq_timeout,
3430 dev, dev->c_handle);
3433 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3437 timeout(xpt_release_devq_timeout,
3439 (crs->release_timeout * hz) / 1000);
3441 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3445 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3447 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3449 * Decrement the freeze count so that a single
3450 * completion is still sufficient to unfreeze
3453 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3456 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3457 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3461 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3463 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3464 || (dev->ccbq.dev_active == 0)) {
3466 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3469 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3470 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3475 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3477 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3480 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3481 start_ccb->ccb_h.status = CAM_REQ_CMP;
3485 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3488 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3489 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3497 #ifdef CAM_DEBUG_DELAY
3498 cam_debug_delay = CAM_DEBUG_DELAY;
3500 cam_dflags = start_ccb->cdbg.flags;
3501 if (cam_dpath != NULL) {
3502 xpt_free_path(cam_dpath);
3506 if (cam_dflags != CAM_DEBUG_NONE) {
3507 if (xpt_create_path(&cam_dpath, xpt_periph,
3508 start_ccb->ccb_h.path_id,
3509 start_ccb->ccb_h.target_id,
3510 start_ccb->ccb_h.target_lun) !=
3512 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3513 cam_dflags = CAM_DEBUG_NONE;
3515 start_ccb->ccb_h.status = CAM_REQ_CMP;
3516 xpt_print(cam_dpath, "debugging flags now %x\n",
3521 start_ccb->ccb_h.status = CAM_REQ_CMP;
3524 #else /* !CAMDEBUG */
3525 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3526 #endif /* CAMDEBUG */
3530 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3531 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3532 start_ccb->ccb_h.status = CAM_REQ_CMP;
3539 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3546 xpt_polled_action(union ccb *start_ccb)
3550 struct cam_sim *sim;
3551 struct cam_devq *devq;
3556 timeout = start_ccb->ccb_h.timeout;
3557 sim = start_ccb->ccb_h.path->bus->sim;
3559 dev = start_ccb->ccb_h.path->device;
3564 * Steal an opening so that no other queued requests
3565 * can get it before us while we simulate interrupts.
3567 dev->ccbq.devq_openings--;
3568 dev->ccbq.dev_openings--;
3570 while(((devq != NULL && devq->send_openings <= 0) ||
3571 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3573 (*(sim->sim_poll))(sim);
3577 dev->ccbq.devq_openings++;
3578 dev->ccbq.dev_openings++;
3581 xpt_action(start_ccb);
3582 while(--timeout > 0) {
3583 (*(sim->sim_poll))(sim);
3585 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3592 * XXX Is it worth adding a sim_timeout entry
3593 * point so we can attempt recovery? If
3594 * this is only used for dumps, I don't think
3597 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3600 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3606 * Schedule a peripheral driver to receive a ccb when it's
3607 * target device has space for more transactions.
3610 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3612 struct cam_ed *device;
3613 union ccb *work_ccb;
3619 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3620 device = perph->path->device;
3622 if (periph_is_queued(perph)) {
3623 /* Simply reorder based on new priority */
3624 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3625 (" change priority to %d\n", new_priority));
3626 if (new_priority < perph->pinfo.priority) {
3627 camq_change_priority(&device->drvq,
3632 } else if (SIM_DEAD(perph->path->bus->sim)) {
3633 /* The SIM is gone so just call periph_start directly. */
3634 work_ccb = xpt_get_ccb(perph->path->device);
3636 if (work_ccb == NULL)
3638 xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority);
3639 perph->pinfo.priority = new_priority;
3640 perph->periph_start(perph, work_ccb);
3643 /* New entry on the queue */
3644 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3645 (" added periph to queue\n"));
3646 perph->pinfo.priority = new_priority;
3647 perph->pinfo.generation = ++device->drvq.generation;
3648 camq_insert(&device->drvq, &perph->pinfo);
3649 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3653 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3654 (" calling xpt_run_devq\n"));
3655 xpt_run_dev_allocq(perph->path->bus);
3661 * Schedule a device to run on a given queue.
3662 * If the device was inserted as a new entry on the queue,
3663 * return 1 meaning the device queue should be run. If we
3664 * were already queued, implying someone else has already
3665 * started the queue, return 0 so the caller doesn't attempt
3666 * to run the queue. Must be run at either splsoftcam
3667 * (or splcam since that encompases splsoftcam).
3670 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3671 u_int32_t new_priority)
3674 u_int32_t old_priority;
3676 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3678 old_priority = pinfo->priority;
3681 * Are we already queued?
3683 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3684 /* Simply reorder based on new priority */
3685 if (new_priority < old_priority) {
3686 camq_change_priority(queue, pinfo->index,
3688 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3689 ("changed priority to %d\n",
3694 /* New entry on the queue */
3695 if (new_priority < old_priority)
3696 pinfo->priority = new_priority;
3698 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3699 ("Inserting onto queue\n"));
3700 pinfo->generation = ++queue->generation;
3701 camq_insert(queue, pinfo);
3708 xpt_run_dev_allocq(struct cam_eb *bus)
3710 struct cam_devq *devq;
3713 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3714 devq = bus->sim->devq;
3716 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3717 (" qfrozen_cnt == 0x%x, entries == %d, "
3718 "openings == %d, active == %d\n",
3719 devq->alloc_queue.qfrozen_cnt,
3720 devq->alloc_queue.entries,
3721 devq->alloc_openings,
3722 devq->alloc_active));
3725 devq->alloc_queue.qfrozen_cnt++;
3726 while ((devq->alloc_queue.entries > 0)
3727 && (devq->alloc_openings > 0)
3728 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3729 struct cam_ed_qinfo *qinfo;
3730 struct cam_ed *device;
3731 union ccb *work_ccb;
3732 struct cam_periph *drv;
3735 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3737 device = qinfo->device;
3739 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3740 ("running device %p\n", device));
3742 drvq = &device->drvq;
3745 if (drvq->entries <= 0) {
3746 panic("xpt_run_dev_allocq: "
3747 "Device on queue without any work to do");
3750 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3751 devq->alloc_openings--;
3752 devq->alloc_active++;
3753 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3755 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3756 drv->pinfo.priority);
3757 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3758 ("calling periph start\n"));
3759 drv->periph_start(drv, work_ccb);
3762 * Malloc failure in alloc_ccb
3765 * XXX add us to a list to be run from free_ccb
3766 * if we don't have any ccbs active on this
3767 * device queue otherwise we may never get run
3773 /* Raise IPL for possible insertion and test at top of loop */
3776 if (drvq->entries > 0) {
3777 /* We have more work. Attempt to reschedule */
3778 xpt_schedule_dev_allocq(bus, device);
3781 devq->alloc_queue.qfrozen_cnt--;
3786 xpt_run_dev_sendq(struct cam_eb *bus)
3788 struct cam_devq *devq;
3791 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3793 devq = bus->sim->devq;
3796 devq->send_queue.qfrozen_cnt++;
3799 while ((devq->send_queue.entries > 0)
3800 && (devq->send_openings > 0)) {
3801 struct cam_ed_qinfo *qinfo;
3802 struct cam_ed *device;
3803 union ccb *work_ccb;
3804 struct cam_sim *sim;
3808 if (devq->send_queue.qfrozen_cnt > 1) {
3813 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3815 device = qinfo->device;
3818 * If the device has been "frozen", don't attempt
3821 if (device->qfrozen_cnt > 0) {
3826 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3827 ("running device %p\n", device));
3829 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3830 if (work_ccb == NULL) {
3831 printf("device on run queue with no ccbs???\n");
3836 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3838 if (num_highpower <= 0) {
3840 * We got a high power command, but we
3841 * don't have any available slots. Freeze
3842 * the device queue until we have a slot
3845 device->qfrozen_cnt++;
3846 STAILQ_INSERT_TAIL(&highpowerq,
3854 * Consume a high power slot while
3860 devq->active_dev = device;
3861 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3863 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3866 devq->send_openings--;
3867 devq->send_active++;
3869 if (device->ccbq.queue.entries > 0)
3870 xpt_schedule_dev_sendq(bus, device);
3872 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3874 * The client wants to freeze the queue
3875 * after this CCB is sent.
3878 device->qfrozen_cnt++;
3884 /* In Target mode, the peripheral driver knows best... */
3885 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3886 if ((device->inq_flags & SID_CmdQue) != 0
3887 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3888 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3891 * Clear this in case of a retried CCB that
3892 * failed due to a rejected tag.
3894 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3898 * Device queues can be shared among multiple sim instances
3899 * that reside on different busses. Use the SIM in the queue
3900 * CCB's path, rather than the one in the bus that was passed
3901 * into this function.
3903 sim = work_ccb->ccb_h.path->bus->sim;
3904 (*(sim->sim_action))(sim, work_ccb);
3907 devq->active_dev = NULL;
3909 /* Raise IPL for possible insertion and test at top of loop */
3914 devq->send_queue.qfrozen_cnt--;
3919 * This function merges stuff from the slave ccb into the master ccb, while
3920 * keeping important fields in the master ccb constant.
3923 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3928 * Pull fields that are valid for peripheral drivers to set
3929 * into the master CCB along with the CCB "payload".
3931 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3932 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3933 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3934 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3935 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3936 sizeof(union ccb) - sizeof(struct ccb_hdr));
3940 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3944 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3945 ccb_h->pinfo.priority = priority;
3947 ccb_h->path_id = path->bus->path_id;
3949 ccb_h->target_id = path->target->target_id;
3951 ccb_h->target_id = CAM_TARGET_WILDCARD;
3953 ccb_h->target_lun = path->device->lun_id;
3954 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3956 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3958 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3962 /* Path manipulation functions */
3964 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3965 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3967 struct cam_path *path;
3972 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3975 status = CAM_RESRC_UNAVAIL;
3978 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3979 if (status != CAM_REQ_CMP) {
3980 free(path, M_CAMXPT);
3983 *new_path_ptr = path;
3988 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3989 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3992 struct cam_et *target;
3993 struct cam_ed *device;
3997 status = CAM_REQ_CMP; /* Completed without error */
3998 target = NULL; /* Wildcarded */
3999 device = NULL; /* Wildcarded */
4002 * We will potentially modify the EDT, so block interrupts
4003 * that may attempt to create cam paths.
4006 bus = xpt_find_bus(path_id);
4008 status = CAM_PATH_INVALID;
4010 target = xpt_find_target(bus, target_id);
4011 if (target == NULL) {
4013 struct cam_et *new_target;
4015 new_target = xpt_alloc_target(bus, target_id);
4016 if (new_target == NULL) {
4017 status = CAM_RESRC_UNAVAIL;
4019 target = new_target;
4022 if (target != NULL) {
4023 device = xpt_find_device(target, lun_id);
4024 if (device == NULL) {
4026 struct cam_ed *new_device;
4028 new_device = xpt_alloc_device(bus,
4031 if (new_device == NULL) {
4032 status = CAM_RESRC_UNAVAIL;
4034 device = new_device;
4042 * Only touch the user's data if we are successful.
4044 if (status == CAM_REQ_CMP) {
4045 new_path->periph = perph;
4046 new_path->bus = bus;
4047 new_path->target = target;
4048 new_path->device = device;
4049 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4052 xpt_release_device(bus, target, device);
4054 xpt_release_target(bus, target);
4056 xpt_release_bus(bus);
4062 xpt_release_path(struct cam_path *path)
4064 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4065 if (path->device != NULL) {
4066 xpt_release_device(path->bus, path->target, path->device);
4067 path->device = NULL;
4069 if (path->target != NULL) {
4070 xpt_release_target(path->bus, path->target);
4071 path->target = NULL;
4073 if (path->bus != NULL) {
4074 xpt_release_bus(path->bus);
4080 xpt_free_path(struct cam_path *path)
4084 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4085 xpt_release_path(path);
4086 free(path, M_CAMXPT);
4091 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4092 * in path1, 2 for match with wildcards in path2.
4095 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4101 if (path1->bus != path2->bus) {
4102 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4104 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4109 if (path1->target != path2->target) {
4110 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4113 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4118 if (path1->device != path2->device) {
4119 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4122 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4131 xpt_print_path(struct cam_path *path)
4136 printf("(nopath): ");
4138 if (path->periph != NULL)
4139 printf("(%s%d:", path->periph->periph_name,
4140 path->periph->unit_number);
4142 printf("(noperiph:");
4144 if (path->bus != NULL)
4145 printf("%s%d:%d:", path->bus->sim->sim_name,
4146 path->bus->sim->unit_number,
4147 path->bus->sim->bus_id);
4151 if (path->target != NULL)
4152 printf("%d:", path->target->target_id);
4156 if (path->device != NULL)
4157 printf("%d): ", path->device->lun_id);
4164 xpt_print(struct cam_path *path, const char *fmt, ...)
4167 xpt_print_path(path);
4174 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4180 sbuf_new(&sb, str, str_len, 0);
4183 sbuf_printf(&sb, "(nopath): ");
4185 if (path->periph != NULL)
4186 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4187 path->periph->unit_number);
4189 sbuf_printf(&sb, "(noperiph:");
4191 if (path->bus != NULL)
4192 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4193 path->bus->sim->unit_number,
4194 path->bus->sim->bus_id);
4196 sbuf_printf(&sb, "nobus:");
4198 if (path->target != NULL)
4199 sbuf_printf(&sb, "%d:", path->target->target_id);
4201 sbuf_printf(&sb, "X:");
4203 if (path->device != NULL)
4204 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4206 sbuf_printf(&sb, "X): ");
4210 return(sbuf_len(&sb));
4214 xpt_path_path_id(struct cam_path *path)
4218 return(path->bus->path_id);
4222 xpt_path_target_id(struct cam_path *path)
4226 if (path->target != NULL)
4227 return (path->target->target_id);
4229 return (CAM_TARGET_WILDCARD);
4233 xpt_path_lun_id(struct cam_path *path)
4237 if (path->device != NULL)
4238 return (path->device->lun_id);
4240 return (CAM_LUN_WILDCARD);
4244 xpt_path_sim(struct cam_path *path)
4248 return (path->bus->sim);
4252 xpt_path_periph(struct cam_path *path)
4256 return (path->periph);
4260 * Release a CAM control block for the caller. Remit the cost of the structure
4261 * to the device referenced by the path. If the this device had no 'credits'
4262 * and peripheral drivers have registered async callbacks for this notification
4266 xpt_release_ccb(union ccb *free_ccb)
4269 struct cam_path *path;
4270 struct cam_ed *device;
4275 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4276 path = free_ccb->ccb_h.path;
4277 device = path->device;
4280 cam_ccbq_release_opening(&device->ccbq);
4281 if (xpt_ccb_count > xpt_max_ccbs) {
4282 xpt_free_ccb(free_ccb);
4285 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4287 if (bus->sim->devq == NULL) {
4291 bus->sim->devq->alloc_openings++;
4292 bus->sim->devq->alloc_active--;
4293 /* XXX Turn this into an inline function - xpt_run_device?? */
4294 if ((device_is_alloc_queued(device) == 0)
4295 && (device->drvq.entries > 0)) {
4296 xpt_schedule_dev_allocq(bus, device);
4299 if (dev_allocq_is_runnable(bus->sim->devq))
4300 xpt_run_dev_allocq(bus);
4303 /* Functions accessed by SIM drivers */
4306 * A sim structure, listing the SIM entry points and instance
4307 * identification info is passed to xpt_bus_register to hook the SIM
4308 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4309 * for this new bus and places it in the array of busses and assigns
4310 * it a path_id. The path_id may be influenced by "hard wiring"
4311 * information specified by the user. Once interrupt services are
4312 * availible, the bus will be probed.
4315 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4317 struct cam_eb *new_bus;
4318 struct cam_eb *old_bus;
4319 struct ccb_pathinq cpi;
4325 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4326 M_CAMXPT, M_NOWAIT);
4327 if (new_bus == NULL) {
4328 /* Couldn't satisfy request */
4329 return (CAM_RESRC_UNAVAIL);
4332 if (strcmp(sim->sim_name, "xpt") != 0) {
4335 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4338 TAILQ_INIT(&new_bus->et_entries);
4339 new_bus->path_id = sim->path_id;
4341 timevalclear(&new_bus->last_reset);
4343 new_bus->refcount = 1; /* Held until a bus_deregister event */
4344 new_bus->generation = 0;
4346 old_bus = TAILQ_FIRST(&xpt_busses);
4347 while (old_bus != NULL
4348 && old_bus->path_id < new_bus->path_id)
4349 old_bus = TAILQ_NEXT(old_bus, links);
4350 if (old_bus != NULL)
4351 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4353 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4357 /* Notify interested parties */
4358 if (sim->path_id != CAM_XPT_PATH_ID) {
4359 struct cam_path path;
4361 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4362 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4363 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4364 cpi.ccb_h.func_code = XPT_PATH_INQ;
4365 xpt_action((union ccb *)&cpi);
4366 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4367 xpt_release_path(&path);
4369 return (CAM_SUCCESS);
4373 xpt_bus_deregister(path_id_t pathid)
4375 struct cam_path bus_path;
4376 struct cam_ed *device;
4377 struct cam_ed_qinfo *qinfo;
4378 struct cam_devq *devq;
4379 struct cam_periph *periph;
4380 struct cam_sim *ccbsim;
4381 union ccb *work_ccb;
4386 status = xpt_compile_path(&bus_path, NULL, pathid,
4387 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4388 if (status != CAM_REQ_CMP)
4391 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4392 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4394 /* The SIM may be gone, so use a dummy SIM for any stray operations. */
4395 devq = bus_path.bus->sim->devq;
4396 bus_path.bus->sim = &cam_dead_sim;
4398 /* Execute any pending operations now. */
4399 while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
4400 CAMQ_HEAD)) != NULL ||
4401 (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
4402 CAMQ_HEAD)) != NULL) {
4404 device = qinfo->device;
4405 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
4406 if (work_ccb != NULL) {
4407 devq->active_dev = device;
4408 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
4409 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
4410 ccbsim = work_ccb->ccb_h.path->bus->sim;
4411 (*(ccbsim->sim_action))(ccbsim, work_ccb);
4414 periph = (struct cam_periph *)camq_remove(&device->drvq,
4417 xpt_schedule(periph, periph->pinfo.priority);
4418 } while (work_ccb != NULL || periph != NULL);
4421 /* Make sure all completed CCBs are processed. */
4422 while (!TAILQ_EMPTY(&cam_bioq)) {
4425 /* Repeat the async's for the benefit of any new devices. */
4426 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4427 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4430 /* Release the reference count held while registered. */
4431 xpt_release_bus(bus_path.bus);
4432 xpt_release_path(&bus_path);
4434 /* Recheck for more completed CCBs. */
4435 while (!TAILQ_EMPTY(&cam_bioq))
4438 return (CAM_REQ_CMP);
4442 xptnextfreepathid(void)
4449 bus = TAILQ_FIRST(&xpt_busses);
4451 /* Find an unoccupied pathid */
4453 && bus->path_id <= pathid) {
4454 if (bus->path_id == pathid)
4456 bus = TAILQ_NEXT(bus, links);
4460 * Ensure that this pathid is not reserved for
4461 * a bus that may be registered in the future.
4463 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4465 /* Start the search over */
4472 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4479 pathid = CAM_XPT_PATH_ID;
4480 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4482 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4483 if (strcmp(dname, "scbus")) {
4484 /* Avoid a bit of foot shooting. */
4487 if (dunit < 0) /* unwired?! */
4489 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4490 if (sim_bus == val) {
4494 } else if (sim_bus == 0) {
4495 /* Unspecified matches bus 0 */
4499 printf("Ambiguous scbus configuration for %s%d "
4500 "bus %d, cannot wire down. The kernel "
4501 "config entry for scbus%d should "
4502 "specify a controller bus.\n"
4503 "Scbus will be assigned dynamically.\n",
4504 sim_name, sim_unit, sim_bus, dunit);
4509 if (pathid == CAM_XPT_PATH_ID)
4510 pathid = xptnextfreepathid();
4515 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4518 struct cam_et *target, *next_target;
4519 struct cam_ed *device, *next_device;
4524 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4527 * Most async events come from a CAM interrupt context. In
4528 * a few cases, the error recovery code at the peripheral layer,
4529 * which may run from our SWI or a process context, may signal
4530 * deferred events with a call to xpt_async. Ensure async
4531 * notifications are serialized by blocking cam interrupts.
4537 if (async_code == AC_BUS_RESET) {
4541 /* Update our notion of when the last reset occurred */
4542 microtime(&bus->last_reset);
4546 for (target = TAILQ_FIRST(&bus->et_entries);
4548 target = next_target) {
4550 next_target = TAILQ_NEXT(target, links);
4552 if (path->target != target
4553 && path->target->target_id != CAM_TARGET_WILDCARD
4554 && target->target_id != CAM_TARGET_WILDCARD)
4557 if (async_code == AC_SENT_BDR) {
4560 /* Update our notion of when the last reset occurred */
4562 microtime(&path->target->last_reset);
4566 for (device = TAILQ_FIRST(&target->ed_entries);
4568 device = next_device) {
4570 next_device = TAILQ_NEXT(device, links);
4572 if (path->device != device
4573 && path->device->lun_id != CAM_LUN_WILDCARD
4574 && device->lun_id != CAM_LUN_WILDCARD)
4577 xpt_dev_async(async_code, bus, target,
4580 xpt_async_bcast(&device->asyncs, async_code,
4586 * If this wasn't a fully wildcarded async, tell all
4587 * clients that want all async events.
4589 if (bus != xpt_periph->path->bus)
4590 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4596 xpt_async_bcast(struct async_list *async_head,
4597 u_int32_t async_code,
4598 struct cam_path *path, void *async_arg)
4600 struct async_node *cur_entry;
4602 cur_entry = SLIST_FIRST(async_head);
4603 while (cur_entry != NULL) {
4604 struct async_node *next_entry;
4606 * Grab the next list entry before we call the current
4607 * entry's callback. This is because the callback function
4608 * can delete its async callback entry.
4610 next_entry = SLIST_NEXT(cur_entry, links);
4611 if ((cur_entry->event_enable & async_code) != 0)
4612 cur_entry->callback(cur_entry->callback_arg,
4615 cur_entry = next_entry;
4620 * Handle any per-device event notifications that require action by the XPT.
4623 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4624 struct cam_ed *device, void *async_arg)
4627 struct cam_path newpath;
4630 * We only need to handle events for real devices.
4632 if (target->target_id == CAM_TARGET_WILDCARD
4633 || device->lun_id == CAM_LUN_WILDCARD)
4637 * We need our own path with wildcards expanded to
4638 * handle certain types of events.
4640 if ((async_code == AC_SENT_BDR)
4641 || (async_code == AC_BUS_RESET)
4642 || (async_code == AC_INQ_CHANGED))
4643 status = xpt_compile_path(&newpath, NULL,
4648 status = CAM_REQ_CMP_ERR;
4650 if (status == CAM_REQ_CMP) {
4653 * Allow transfer negotiation to occur in a
4654 * tag free environment.
4656 if (async_code == AC_SENT_BDR
4657 || async_code == AC_BUS_RESET)
4658 xpt_toggle_tags(&newpath);
4660 if (async_code == AC_INQ_CHANGED) {
4662 * We've sent a start unit command, or
4663 * something similar to a device that
4664 * may have caused its inquiry data to
4665 * change. So we re-scan the device to
4666 * refresh the inquiry data for it.
4668 xpt_scan_lun(newpath.periph, &newpath,
4669 CAM_EXPECT_INQ_CHANGE, NULL);
4671 xpt_release_path(&newpath);
4672 } else if (async_code == AC_LOST_DEVICE) {
4673 device->flags |= CAM_DEV_UNCONFIGURED;
4674 } else if (async_code == AC_TRANSFER_NEG) {
4675 struct ccb_trans_settings *settings;
4677 settings = (struct ccb_trans_settings *)async_arg;
4678 xpt_set_transfer_settings(settings, device,
4679 /*async_update*/TRUE);
4684 xpt_freeze_devq(struct cam_path *path, u_int count)
4687 struct ccb_hdr *ccbh;
4692 path->device->qfrozen_cnt += count;
4695 * Mark the last CCB in the queue as needing
4696 * to be requeued if the driver hasn't
4697 * changed it's state yet. This fixes a race
4698 * where a ccb is just about to be queued to
4699 * a controller driver when it's interrupt routine
4700 * freezes the queue. To completly close the
4701 * hole, controller drives must check to see
4702 * if a ccb's status is still CAM_REQ_INPROG
4703 * under spl protection just before they queue
4704 * the CCB. See ahc_action/ahc_freeze_devq for
4707 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4708 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4709 ccbh->status = CAM_REQUEUE_REQ;
4711 return (path->device->qfrozen_cnt);
4715 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4719 sim->devq->send_queue.qfrozen_cnt += count;
4720 if (sim->devq->active_dev != NULL) {
4721 struct ccb_hdr *ccbh;
4723 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4725 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4726 ccbh->status = CAM_REQUEUE_REQ;
4728 return (sim->devq->send_queue.qfrozen_cnt);
4732 xpt_release_devq_timeout(void *arg)
4734 struct cam_ed *device;
4736 device = (struct cam_ed *)arg;
4738 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4742 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4746 xpt_release_devq_device(path->device, count, run_queue);
4750 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4758 if (dev->qfrozen_cnt > 0) {
4760 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4761 dev->qfrozen_cnt -= count;
4762 if (dev->qfrozen_cnt == 0) {
4765 * No longer need to wait for a successful
4766 * command completion.
4768 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4771 * Remove any timeouts that might be scheduled
4772 * to release this queue.
4774 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4775 untimeout(xpt_release_devq_timeout, dev,
4777 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4781 * Now that we are unfrozen schedule the
4782 * device so any pending transactions are
4785 if ((dev->ccbq.queue.entries > 0)
4786 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4787 && (run_queue != 0)) {
4794 xpt_run_dev_sendq(dev->target->bus);
4799 xpt_release_simq(struct cam_sim *sim, int run_queue)
4806 sendq = &(sim->devq->send_queue);
4808 if (sendq->qfrozen_cnt > 0) {
4810 sendq->qfrozen_cnt--;
4811 if (sendq->qfrozen_cnt == 0) {
4815 * If there is a timeout scheduled to release this
4816 * sim queue, remove it. The queue frozen count is
4819 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4820 untimeout(xpt_release_simq_timeout, sim,
4822 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4824 bus = xpt_find_bus(sim->path_id);
4829 * Now that we are unfrozen run the send queue.
4831 xpt_run_dev_sendq(bus);
4833 xpt_release_bus(bus);
4841 xpt_release_simq_timeout(void *arg)
4843 struct cam_sim *sim;
4845 sim = (struct cam_sim *)arg;
4846 xpt_release_simq(sim, /* run_queue */ TRUE);
4850 xpt_done(union ccb *done_ccb)
4856 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4857 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4859 * Queue up the request for handling by our SWI handler
4860 * any of the "non-immediate" type of ccbs.
4862 switch (done_ccb->ccb_h.path->periph->type) {
4863 case CAM_PERIPH_BIO:
4864 mtx_lock(&cam_bioq_lock);
4865 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4867 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4868 mtx_unlock(&cam_bioq_lock);
4869 swi_sched(cambio_ih, 0);
4872 panic("unknown periph type %d",
4873 done_ccb->ccb_h.path->periph->type);
4886 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4891 xpt_alloc_ccb_nowait()
4897 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
4902 xpt_free_ccb(union ccb *free_ccb)
4904 free(free_ccb, M_CAMXPT);
4909 /* Private XPT functions */
4912 * Get a CAM control block for the caller. Charge the structure to the device
4913 * referenced by the path. If the this device has no 'credits' then the
4914 * device already has the maximum number of outstanding operations under way
4915 * and we return NULL. If we don't have sufficient resources to allocate more
4916 * ccbs, we also return NULL.
4919 xpt_get_ccb(struct cam_ed *device)
4925 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4926 new_ccb = xpt_alloc_ccb_nowait();
4927 if (new_ccb == NULL) {
4931 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4932 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4936 cam_ccbq_take_opening(&device->ccbq);
4937 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4943 xpt_release_bus(struct cam_eb *bus)
4948 if ((--bus->refcount == 0)
4949 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4950 TAILQ_REMOVE(&xpt_busses, bus, links);
4953 free(bus, M_CAMXPT);
4958 static struct cam_et *
4959 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4961 struct cam_et *target;
4963 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4964 if (target != NULL) {
4965 struct cam_et *cur_target;
4967 TAILQ_INIT(&target->ed_entries);
4969 target->target_id = target_id;
4970 target->refcount = 1;
4971 target->generation = 0;
4972 timevalclear(&target->last_reset);
4974 * Hold a reference to our parent bus so it
4975 * will not go away before we do.
4979 /* Insertion sort into our bus's target list */
4980 cur_target = TAILQ_FIRST(&bus->et_entries);
4981 while (cur_target != NULL && cur_target->target_id < target_id)
4982 cur_target = TAILQ_NEXT(cur_target, links);
4984 if (cur_target != NULL) {
4985 TAILQ_INSERT_BEFORE(cur_target, target, links);
4987 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4995 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
5000 if ((--target->refcount == 0)
5001 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
5002 TAILQ_REMOVE(&bus->et_entries, target, links);
5005 free(target, M_CAMXPT);
5006 xpt_release_bus(bus);
5011 static struct cam_ed *
5012 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5014 struct cam_path path;
5015 struct cam_ed *device;
5016 struct cam_devq *devq;
5019 if (SIM_DEAD(bus->sim))
5022 /* Make space for us in the device queue on our bus */
5023 devq = bus->sim->devq;
5024 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5026 if (status != CAM_REQ_CMP) {
5029 device = (struct cam_ed *)malloc(sizeof(*device),
5030 M_CAMXPT, M_NOWAIT);
5033 if (device != NULL) {
5034 struct cam_ed *cur_device;
5036 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5037 device->alloc_ccb_entry.device = device;
5038 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5039 device->send_ccb_entry.device = device;
5040 device->target = target;
5041 device->lun_id = lun_id;
5042 /* Initialize our queues */
5043 if (camq_init(&device->drvq, 0) != 0) {
5044 free(device, M_CAMXPT);
5047 if (cam_ccbq_init(&device->ccbq,
5048 bus->sim->max_dev_openings) != 0) {
5049 camq_fini(&device->drvq);
5050 free(device, M_CAMXPT);
5053 SLIST_INIT(&device->asyncs);
5054 SLIST_INIT(&device->periphs);
5055 device->generation = 0;
5056 device->owner = NULL;
5058 * Take the default quirk entry until we have inquiry
5059 * data and can determine a better quirk to use.
5061 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5062 bzero(&device->inq_data, sizeof(device->inq_data));
5063 device->inq_flags = 0;
5064 device->queue_flags = 0;
5065 device->serial_num = NULL;
5066 device->serial_num_len = 0;
5067 device->qfrozen_cnt = 0;
5068 device->flags = CAM_DEV_UNCONFIGURED;
5069 device->tag_delay_count = 0;
5070 device->tag_saved_openings = 0;
5071 device->refcount = 1;
5072 callout_handle_init(&device->c_handle);
5075 * Hold a reference to our parent target so it
5076 * will not go away before we do.
5081 * XXX should be limited by number of CCBs this bus can
5084 xpt_max_ccbs += device->ccbq.devq_openings;
5085 /* Insertion sort into our target's device list */
5086 cur_device = TAILQ_FIRST(&target->ed_entries);
5087 while (cur_device != NULL && cur_device->lun_id < lun_id)
5088 cur_device = TAILQ_NEXT(cur_device, links);
5089 if (cur_device != NULL) {
5090 TAILQ_INSERT_BEFORE(cur_device, device, links);
5092 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5094 target->generation++;
5095 if (lun_id != CAM_LUN_WILDCARD) {
5096 xpt_compile_path(&path,
5101 xpt_devise_transport(&path);
5102 xpt_release_path(&path);
5109 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5110 struct cam_ed *device)
5115 if ((--device->refcount == 0)
5116 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5117 struct cam_devq *devq;
5119 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5120 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5121 panic("Removing device while still queued for ccbs");
5123 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5124 untimeout(xpt_release_devq_timeout, device,
5127 TAILQ_REMOVE(&target->ed_entries, device,links);
5128 target->generation++;
5129 xpt_max_ccbs -= device->ccbq.devq_openings;
5130 if (!SIM_DEAD(bus->sim)) {
5131 /* Release our slot in the devq */
5132 devq = bus->sim->devq;
5133 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5136 camq_fini(&device->drvq);
5137 camq_fini(&device->ccbq.queue);
5138 free(device, M_CAMXPT);
5139 xpt_release_target(bus, target);
5145 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5155 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5156 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5157 if (result == CAM_REQ_CMP && (diff < 0)) {
5158 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5160 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5161 || (dev->inq_flags & SID_CmdQue) != 0)
5162 dev->tag_saved_openings = newopenings;
5163 /* Adjust the global limit */
5164 xpt_max_ccbs += diff;
5169 static struct cam_eb *
5170 xpt_find_bus(path_id_t path_id)
5174 for (bus = TAILQ_FIRST(&xpt_busses);
5176 bus = TAILQ_NEXT(bus, links)) {
5177 if (bus->path_id == path_id) {
5185 static struct cam_et *
5186 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5188 struct cam_et *target;
5190 for (target = TAILQ_FIRST(&bus->et_entries);
5192 target = TAILQ_NEXT(target, links)) {
5193 if (target->target_id == target_id) {
5201 static struct cam_ed *
5202 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5204 struct cam_ed *device;
5206 for (device = TAILQ_FIRST(&target->ed_entries);
5208 device = TAILQ_NEXT(device, links)) {
5209 if (device->lun_id == lun_id) {
5218 union ccb *request_ccb;
5219 struct ccb_pathinq *cpi;
5221 } xpt_scan_bus_info;
5224 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5225 * As the scan progresses, xpt_scan_bus is used as the
5226 * callback on completion function.
5229 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5231 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5232 ("xpt_scan_bus\n"));
5233 switch (request_ccb->ccb_h.func_code) {
5236 xpt_scan_bus_info *scan_info;
5237 union ccb *work_ccb;
5238 struct cam_path *path;
5243 /* Find out the characteristics of the bus */
5244 work_ccb = xpt_alloc_ccb();
5245 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5246 request_ccb->ccb_h.pinfo.priority);
5247 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5248 xpt_action(work_ccb);
5249 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5250 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5251 xpt_free_ccb(work_ccb);
5252 xpt_done(request_ccb);
5256 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5258 * Can't scan the bus on an adapter that
5259 * cannot perform the initiator role.
5261 request_ccb->ccb_h.status = CAM_REQ_CMP;
5262 xpt_free_ccb(work_ccb);
5263 xpt_done(request_ccb);
5267 /* Save some state for use while we probe for devices */
5268 scan_info = (xpt_scan_bus_info *)
5269 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5270 scan_info->request_ccb = request_ccb;
5271 scan_info->cpi = &work_ccb->cpi;
5273 /* Cache on our stack so we can work asynchronously */
5274 max_target = scan_info->cpi->max_target;
5275 initiator_id = scan_info->cpi->initiator_id;
5279 * We can scan all targets in parallel, or do it sequentially.
5281 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5283 scan_info->counter = 0;
5285 scan_info->counter = scan_info->cpi->max_target + 1;
5286 if (scan_info->cpi->initiator_id < scan_info->counter) {
5287 scan_info->counter--;
5291 for (i = 0; i <= max_target; i++) {
5293 if (i == initiator_id)
5296 status = xpt_create_path(&path, xpt_periph,
5297 request_ccb->ccb_h.path_id,
5299 if (status != CAM_REQ_CMP) {
5300 printf("xpt_scan_bus: xpt_create_path failed"
5301 " with status %#x, bus scan halted\n",
5303 free(scan_info, M_TEMP);
5304 request_ccb->ccb_h.status = status;
5305 xpt_free_ccb(work_ccb);
5306 xpt_done(request_ccb);
5309 work_ccb = xpt_alloc_ccb();
5310 xpt_setup_ccb(&work_ccb->ccb_h, path,
5311 request_ccb->ccb_h.pinfo.priority);
5312 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5313 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5314 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5315 work_ccb->crcn.flags = request_ccb->crcn.flags;
5316 xpt_action(work_ccb);
5323 struct cam_path *path;
5324 xpt_scan_bus_info *scan_info;
5326 target_id_t target_id;
5329 /* Reuse the same CCB to query if a device was really found */
5330 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5331 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5332 request_ccb->ccb_h.pinfo.priority);
5333 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5335 path_id = request_ccb->ccb_h.path_id;
5336 target_id = request_ccb->ccb_h.target_id;
5337 lun_id = request_ccb->ccb_h.target_lun;
5338 xpt_action(request_ccb);
5340 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5341 struct cam_ed *device;
5342 struct cam_et *target;
5346 * If we already probed lun 0 successfully, or
5347 * we have additional configured luns on this
5348 * target that might have "gone away", go onto
5351 target = request_ccb->ccb_h.path->target;
5353 * We may touch devices that we don't
5354 * hold references too, so ensure they
5355 * don't disappear out from under us.
5356 * The target above is referenced by the
5357 * path in the request ccb.
5361 device = TAILQ_FIRST(&target->ed_entries);
5362 if (device != NULL) {
5363 phl = CAN_SRCH_HI_SPARSE(device);
5364 if (device->lun_id == 0)
5365 device = TAILQ_NEXT(device, links);
5368 if ((lun_id != 0) || (device != NULL)) {
5369 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5373 struct cam_ed *device;
5375 device = request_ccb->ccb_h.path->device;
5377 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5378 /* Try the next lun */
5379 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5380 || CAN_SRCH_HI_DENSE(device))
5386 * Free the current request path- we're done with it.
5388 xpt_free_path(request_ccb->ccb_h.path);
5391 * Check to see if we scan any further luns.
5393 if (lun_id == request_ccb->ccb_h.target_lun
5394 || lun_id > scan_info->cpi->max_lun) {
5399 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5400 scan_info->counter++;
5401 if (scan_info->counter ==
5402 scan_info->cpi->initiator_id) {
5403 scan_info->counter++;
5405 if (scan_info->counter >=
5406 scan_info->cpi->max_target+1) {
5410 scan_info->counter--;
5411 if (scan_info->counter == 0) {
5416 xpt_free_ccb(request_ccb);
5417 xpt_free_ccb((union ccb *)scan_info->cpi);
5418 request_ccb = scan_info->request_ccb;
5419 free(scan_info, M_TEMP);
5420 request_ccb->ccb_h.status = CAM_REQ_CMP;
5421 xpt_done(request_ccb);
5425 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5428 status = xpt_create_path(&path, xpt_periph,
5429 scan_info->request_ccb->ccb_h.path_id,
5430 scan_info->counter, 0);
5431 if (status != CAM_REQ_CMP) {
5432 printf("xpt_scan_bus: xpt_create_path failed"
5433 " with status %#x, bus scan halted\n",
5435 xpt_free_ccb(request_ccb);
5436 xpt_free_ccb((union ccb *)scan_info->cpi);
5437 request_ccb = scan_info->request_ccb;
5438 free(scan_info, M_TEMP);
5439 request_ccb->ccb_h.status = status;
5440 xpt_done(request_ccb);
5443 xpt_setup_ccb(&request_ccb->ccb_h, path,
5444 request_ccb->ccb_h.pinfo.priority);
5445 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5446 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5447 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5448 request_ccb->crcn.flags =
5449 scan_info->request_ccb->crcn.flags;
5451 status = xpt_create_path(&path, xpt_periph,
5452 path_id, target_id, lun_id);
5453 if (status != CAM_REQ_CMP) {
5454 printf("xpt_scan_bus: xpt_create_path failed "
5455 "with status %#x, halting LUN scan\n",
5459 xpt_setup_ccb(&request_ccb->ccb_h, path,
5460 request_ccb->ccb_h.pinfo.priority);
5461 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5462 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5463 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5464 request_ccb->crcn.flags =
5465 scan_info->request_ccb->crcn.flags;
5467 xpt_action(request_ccb);
5477 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */
5481 PROBE_TUR_FOR_NEGOTIATION,
5482 PROBE_INQUIRY_BASIC_DV1,
5483 PROBE_INQUIRY_BASIC_DV2,
5488 PROBE_INQUIRY_CKSUM = 0x01,
5489 PROBE_SERIAL_CKSUM = 0x02,
5490 PROBE_NO_ANNOUNCE = 0x04
5494 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5495 probe_action action;
5496 union ccb saved_ccb;
5499 u_int8_t digest[16];
5503 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5504 cam_flags flags, union ccb *request_ccb)
5506 struct ccb_pathinq cpi;
5508 struct cam_path *new_path;
5509 struct cam_periph *old_periph;
5512 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5513 ("xpt_scan_lun\n"));
5515 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5516 cpi.ccb_h.func_code = XPT_PATH_INQ;
5517 xpt_action((union ccb *)&cpi);
5519 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5520 if (request_ccb != NULL) {
5521 request_ccb->ccb_h.status = cpi.ccb_h.status;
5522 xpt_done(request_ccb);
5527 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5529 * Can't scan the bus on an adapter that
5530 * cannot perform the initiator role.
5532 if (request_ccb != NULL) {
5533 request_ccb->ccb_h.status = CAM_REQ_CMP;
5534 xpt_done(request_ccb);
5539 if (request_ccb == NULL) {
5540 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5541 if (request_ccb == NULL) {
5542 xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
5543 "can't continue\n");
5546 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5547 if (new_path == NULL) {
5548 xpt_print(path, "xpt_scan_lun: can't allocate path, "
5549 "can't continue\n");
5550 free(request_ccb, M_TEMP);
5553 status = xpt_compile_path(new_path, xpt_periph,
5555 path->target->target_id,
5556 path->device->lun_id);
5558 if (status != CAM_REQ_CMP) {
5559 xpt_print(path, "xpt_scan_lun: can't compile path, "
5560 "can't continue\n");
5561 free(request_ccb, M_TEMP);
5562 free(new_path, M_TEMP);
5565 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5566 request_ccb->ccb_h.cbfcnp = xptscandone;
5567 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5568 request_ccb->crcn.flags = flags;
5572 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5575 softc = (probe_softc *)old_periph->softc;
5576 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5579 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5580 probestart, "probe",
5582 request_ccb->ccb_h.path, NULL, 0,
5585 if (status != CAM_REQ_CMP) {
5586 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
5587 "returned an error, can't continue probe\n");
5588 request_ccb->ccb_h.status = status;
5589 xpt_done(request_ccb);
5596 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5598 xpt_release_path(done_ccb->ccb_h.path);
5599 free(done_ccb->ccb_h.path, M_TEMP);
5600 free(done_ccb, M_TEMP);
5604 proberegister(struct cam_periph *periph, void *arg)
5606 union ccb *request_ccb; /* CCB representing the probe request */
5609 request_ccb = (union ccb *)arg;
5610 if (periph == NULL) {
5611 printf("proberegister: periph was NULL!!\n");
5612 return(CAM_REQ_CMP_ERR);
5615 if (request_ccb == NULL) {
5616 printf("proberegister: no probe CCB, "
5617 "can't register device\n");
5618 return(CAM_REQ_CMP_ERR);
5621 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5623 if (softc == NULL) {
5624 printf("proberegister: Unable to probe new device. "
5625 "Unable to allocate softc\n");
5626 return(CAM_REQ_CMP_ERR);
5628 TAILQ_INIT(&softc->request_ccbs);
5629 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5632 periph->softc = softc;
5633 cam_periph_acquire(periph);
5635 * Ensure we've waited at least a bus settle
5636 * delay before attempting to probe the device.
5637 * For HBAs that don't do bus resets, this won't make a difference.
5639 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5641 probeschedule(periph);
5642 return(CAM_REQ_CMP);
5646 probeschedule(struct cam_periph *periph)
5648 struct ccb_pathinq cpi;
5652 softc = (probe_softc *)periph->softc;
5653 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5655 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5656 cpi.ccb_h.func_code = XPT_PATH_INQ;
5657 xpt_action((union ccb *)&cpi);
5660 * If a device has gone away and another device, or the same one,
5661 * is back in the same place, it should have a unit attention
5662 * condition pending. It will not report the unit attention in
5663 * response to an inquiry, which may leave invalid transfer
5664 * negotiations in effect. The TUR will reveal the unit attention
5665 * condition. Only send the TUR for lun 0, since some devices
5666 * will get confused by commands other than inquiry to non-existent
5667 * luns. If you think a device has gone away start your scan from
5668 * lun 0. This will insure that any bogus transfer settings are
5671 * If we haven't seen the device before and the controller supports
5672 * some kind of transfer negotiation, negotiate with the first
5673 * sent command if no bus reset was performed at startup. This
5674 * ensures that the device is not confused by transfer negotiation
5675 * settings left over by loader or BIOS action.
5677 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5678 && (ccb->ccb_h.target_lun == 0)) {
5679 softc->action = PROBE_TUR;
5680 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5681 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5682 proberequestdefaultnegotiation(periph);
5683 softc->action = PROBE_INQUIRY;
5685 softc->action = PROBE_INQUIRY;
5688 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5689 softc->flags |= PROBE_NO_ANNOUNCE;
5691 softc->flags &= ~PROBE_NO_ANNOUNCE;
5693 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5697 probestart(struct cam_periph *periph, union ccb *start_ccb)
5699 /* Probe the device that our peripheral driver points to */
5700 struct ccb_scsiio *csio;
5703 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5705 softc = (probe_softc *)periph->softc;
5706 csio = &start_ccb->csio;
5708 switch (softc->action) {
5710 case PROBE_TUR_FOR_NEGOTIATION:
5713 scsi_test_unit_ready(csio,
5722 case PROBE_FULL_INQUIRY:
5723 case PROBE_INQUIRY_BASIC_DV1:
5724 case PROBE_INQUIRY_BASIC_DV2:
5727 struct scsi_inquiry_data *inq_buf;
5729 inq_buf = &periph->path->device->inq_data;
5732 * If the device is currently configured, we calculate an
5733 * MD5 checksum of the inquiry data, and if the serial number
5734 * length is greater than 0, add the serial number data
5735 * into the checksum as well. Once the inquiry and the
5736 * serial number check finish, we attempt to figure out
5737 * whether we still have the same device.
5739 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5741 MD5Init(&softc->context);
5742 MD5Update(&softc->context, (unsigned char *)inq_buf,
5743 sizeof(struct scsi_inquiry_data));
5744 softc->flags |= PROBE_INQUIRY_CKSUM;
5745 if (periph->path->device->serial_num_len > 0) {
5746 MD5Update(&softc->context,
5747 periph->path->device->serial_num,
5748 periph->path->device->serial_num_len);
5749 softc->flags |= PROBE_SERIAL_CKSUM;
5751 MD5Final(softc->digest, &softc->context);
5754 if (softc->action == PROBE_INQUIRY)
5755 inquiry_len = SHORT_INQUIRY_LENGTH;
5757 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
5760 * Some parallel SCSI devices fail to send an
5761 * ignore wide residue message when dealing with
5762 * odd length inquiry requests. Round up to be
5765 inquiry_len = roundup2(inquiry_len, 2);
5767 if (softc->action == PROBE_INQUIRY_BASIC_DV1
5768 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
5769 inq_buf = malloc(inquiry_len, M_TEMP, M_NOWAIT);
5771 if (inq_buf == NULL) {
5772 xpt_print(periph->path, "malloc failure- skipping Basic"
5773 "Domain Validation\n");
5774 softc->action = PROBE_DV_EXIT;
5775 scsi_test_unit_ready(csio,
5787 (u_int8_t *)inq_buf,
5792 /*timeout*/60 * 1000);
5795 case PROBE_MODE_SENSE:
5800 mode_buf_len = sizeof(struct scsi_mode_header_6)
5801 + sizeof(struct scsi_mode_blk_desc)
5802 + sizeof(struct scsi_control_page);
5803 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5804 if (mode_buf != NULL) {
5805 scsi_mode_sense(csio,
5810 SMS_PAGE_CTRL_CURRENT,
5811 SMS_CONTROL_MODE_PAGE,
5818 xpt_print(periph->path, "Unable to mode sense control page - "
5819 "malloc failure\n");
5820 softc->action = PROBE_SERIAL_NUM;
5823 case PROBE_SERIAL_NUM:
5825 struct scsi_vpd_unit_serial_number *serial_buf;
5826 struct cam_ed* device;
5829 device = periph->path->device;
5830 device->serial_num = NULL;
5831 device->serial_num_len = 0;
5833 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5834 serial_buf = (struct scsi_vpd_unit_serial_number *)
5835 malloc(sizeof(*serial_buf), M_TEMP,
5838 if (serial_buf != NULL) {
5843 (u_int8_t *)serial_buf,
5844 sizeof(*serial_buf),
5846 SVPD_UNIT_SERIAL_NUMBER,
5848 /*timeout*/60 * 1000);
5852 * We'll have to do without, let our probedone
5853 * routine finish up for us.
5855 start_ccb->csio.data_ptr = NULL;
5856 probedone(periph, start_ccb);
5860 xpt_action(start_ccb);
5864 proberequestdefaultnegotiation(struct cam_periph *periph)
5866 struct ccb_trans_settings cts;
5868 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5869 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5870 cts.type = CTS_TYPE_USER_SETTINGS;
5871 xpt_action((union ccb *)&cts);
5872 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5875 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5876 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5877 xpt_action((union ccb *)&cts);
5881 * Backoff Negotiation Code- only pertinent for SPI devices.
5884 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
5886 struct ccb_trans_settings cts;
5887 struct ccb_trans_settings_spi *spi;
5889 memset(&cts, 0, sizeof (cts));
5890 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5891 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5892 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5893 xpt_action((union ccb *)&cts);
5894 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5896 xpt_print(periph->path,
5897 "failed to get current device settings\n");
5901 if (cts.transport != XPORT_SPI) {
5903 xpt_print(periph->path, "not SPI transport\n");
5907 spi = &cts.xport_specific.spi;
5910 * We cannot renegotiate sync rate if we don't have one.
5912 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
5914 xpt_print(periph->path, "no sync rate known\n");
5920 * We'll assert that we don't have to touch PPR options- the
5921 * SIM will see what we do with period and offset and adjust
5922 * the PPR options as appropriate.
5926 * A sync rate with unknown or zero offset is nonsensical.
5927 * A sync period of zero means Async.
5929 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
5930 || spi->sync_offset == 0 || spi->sync_period == 0) {
5932 xpt_print(periph->path, "no sync rate available\n");
5937 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
5938 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5939 ("hit async: giving up on DV\n"));
5945 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
5946 * We don't try to remember 'last' settings to see if the SIM actually
5947 * gets into the speed we want to set. We check on the SIM telling
5948 * us that a requested speed is bad, but otherwise don't try and
5949 * check the speed due to the asynchronous and handshake nature
5952 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
5955 if (spi->sync_period >= 0xf) {
5956 spi->sync_period = 0;
5957 spi->sync_offset = 0;
5958 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5959 ("setting to async for DV\n"));
5961 * Once we hit async, we don't want to try
5962 * any more settings.
5964 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
5965 } else if (bootverbose) {
5966 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5967 ("DV: period 0x%x\n", spi->sync_period));
5968 printf("setting period to 0x%x\n", spi->sync_period);
5970 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5971 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5972 xpt_action((union ccb *)&cts);
5973 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5976 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5977 ("DV: failed to set period 0x%x\n", spi->sync_period));
5978 if (spi->sync_period == 0) {
5986 probedone(struct cam_periph *periph, union ccb *done_ccb)
5989 struct cam_path *path;
5992 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5994 softc = (probe_softc *)periph->softc;
5995 path = done_ccb->ccb_h.path;
5996 priority = done_ccb->ccb_h.pinfo.priority;
5998 switch (softc->action) {
6001 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6003 if (cam_periph_error(done_ccb, 0,
6004 SF_NO_PRINT, NULL) == ERESTART)
6006 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
6007 /* Don't wedge the queue */
6008 xpt_release_devq(done_ccb->ccb_h.path,
6012 softc->action = PROBE_INQUIRY;
6013 xpt_release_ccb(done_ccb);
6014 xpt_schedule(periph, priority);
6018 case PROBE_FULL_INQUIRY:
6020 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6021 struct scsi_inquiry_data *inq_buf;
6022 u_int8_t periph_qual;
6024 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
6025 inq_buf = &path->device->inq_data;
6027 periph_qual = SID_QUAL(inq_buf);
6029 switch(periph_qual) {
6030 case SID_QUAL_LU_CONNECTED:
6035 * We conservatively request only
6036 * SHORT_INQUIRY_LEN bytes of inquiry
6037 * information during our first try
6038 * at sending an INQUIRY. If the device
6039 * has more information to give,
6040 * perform a second request specifying
6041 * the amount of information the device
6042 * is willing to give.
6044 len = inq_buf->additional_length
6045 + offsetof(struct scsi_inquiry_data,
6046 additional_length) + 1;
6047 if (softc->action == PROBE_INQUIRY
6048 && len > SHORT_INQUIRY_LENGTH) {
6049 softc->action = PROBE_FULL_INQUIRY;
6050 xpt_release_ccb(done_ccb);
6051 xpt_schedule(periph, priority);
6055 xpt_find_quirk(path->device);
6057 xpt_devise_transport(path);
6058 if (INQ_DATA_TQ_ENABLED(inq_buf))
6059 softc->action = PROBE_MODE_SENSE;
6061 softc->action = PROBE_SERIAL_NUM;
6063 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6065 xpt_release_ccb(done_ccb);
6066 xpt_schedule(periph, priority);
6072 } else if (cam_periph_error(done_ccb, 0,
6073 done_ccb->ccb_h.target_lun > 0
6074 ? SF_RETRY_UA|SF_QUIET_IR
6076 &softc->saved_ccb) == ERESTART) {
6078 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6079 /* Don't wedge the queue */
6080 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6084 * If we get to this point, we got an error status back
6085 * from the inquiry and the error status doesn't require
6086 * automatically retrying the command. Therefore, the
6087 * inquiry failed. If we had inquiry information before
6088 * for this device, but this latest inquiry command failed,
6089 * the device has probably gone away. If this device isn't
6090 * already marked unconfigured, notify the peripheral
6091 * drivers that this device is no more.
6093 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
6094 /* Send the async notification. */
6095 xpt_async(AC_LOST_DEVICE, path, NULL);
6097 xpt_release_ccb(done_ccb);
6100 case PROBE_MODE_SENSE:
6102 struct ccb_scsiio *csio;
6103 struct scsi_mode_header_6 *mode_hdr;
6105 csio = &done_ccb->csio;
6106 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
6107 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6108 struct scsi_control_page *page;
6111 offset = ((u_int8_t *)&mode_hdr[1])
6112 + mode_hdr->blk_desc_len;
6113 page = (struct scsi_control_page *)offset;
6114 path->device->queue_flags = page->queue_flags;
6115 } else if (cam_periph_error(done_ccb, 0,
6116 SF_RETRY_UA|SF_NO_PRINT,
6117 &softc->saved_ccb) == ERESTART) {
6119 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6120 /* Don't wedge the queue */
6121 xpt_release_devq(done_ccb->ccb_h.path,
6122 /*count*/1, /*run_queue*/TRUE);
6124 xpt_release_ccb(done_ccb);
6125 free(mode_hdr, M_TEMP);
6126 softc->action = PROBE_SERIAL_NUM;
6127 xpt_schedule(periph, priority);
6130 case PROBE_SERIAL_NUM:
6132 struct ccb_scsiio *csio;
6133 struct scsi_vpd_unit_serial_number *serial_buf;
6140 csio = &done_ccb->csio;
6141 priority = done_ccb->ccb_h.pinfo.priority;
6143 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
6145 /* Clean up from previous instance of this device */
6146 if (path->device->serial_num != NULL) {
6147 free(path->device->serial_num, M_CAMXPT);
6148 path->device->serial_num = NULL;
6149 path->device->serial_num_len = 0;
6152 if (serial_buf == NULL) {
6154 * Don't process the command as it was never sent
6156 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
6157 && (serial_buf->length > 0)) {
6160 path->device->serial_num =
6161 (u_int8_t *)malloc((serial_buf->length + 1),
6162 M_CAMXPT, M_NOWAIT);
6163 if (path->device->serial_num != NULL) {
6164 bcopy(serial_buf->serial_num,
6165 path->device->serial_num,
6166 serial_buf->length);
6167 path->device->serial_num_len =
6169 path->device->serial_num[serial_buf->length]
6172 } else if (cam_periph_error(done_ccb, 0,
6173 SF_RETRY_UA|SF_NO_PRINT,
6174 &softc->saved_ccb) == ERESTART) {
6176 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6177 /* Don't wedge the queue */
6178 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6183 * Let's see if we have seen this device before.
6185 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6187 u_int8_t digest[16];
6192 (unsigned char *)&path->device->inq_data,
6193 sizeof(struct scsi_inquiry_data));
6196 MD5Update(&context, serial_buf->serial_num,
6197 serial_buf->length);
6199 MD5Final(digest, &context);
6200 if (bcmp(softc->digest, digest, 16) == 0)
6204 * XXX Do we need to do a TUR in order to ensure
6205 * that the device really hasn't changed???
6208 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6209 xpt_async(AC_LOST_DEVICE, path, NULL);
6211 if (serial_buf != NULL)
6212 free(serial_buf, M_TEMP);
6216 * Now that we have all the necessary
6217 * information to safely perform transfer
6218 * negotiations... Controllers don't perform
6219 * any negotiation or tagged queuing until
6220 * after the first XPT_SET_TRAN_SETTINGS ccb is
6221 * received. So, on a new device, just retrieve
6222 * the user settings, and set them as the current
6223 * settings to set the device up.
6225 proberequestdefaultnegotiation(periph);
6226 xpt_release_ccb(done_ccb);
6229 * Perform a TUR to allow the controller to
6230 * perform any necessary transfer negotiation.
6232 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6233 xpt_schedule(periph, priority);
6236 xpt_release_ccb(done_ccb);
6239 case PROBE_TUR_FOR_NEGOTIATION:
6241 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6242 /* Don't wedge the queue */
6243 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6247 * Do Domain Validation for lun 0 on devices that claim
6248 * to support Synchronous Transfer modes.
6250 if (softc->action == PROBE_TUR_FOR_NEGOTIATION
6251 && done_ccb->ccb_h.target_lun == 0
6252 && (path->device->inq_data.flags & SID_Sync) != 0
6253 && (path->device->flags & CAM_DEV_IN_DV) == 0) {
6254 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6255 ("Begin Domain Validation\n"));
6256 path->device->flags |= CAM_DEV_IN_DV;
6257 xpt_release_ccb(done_ccb);
6258 softc->action = PROBE_INQUIRY_BASIC_DV1;
6259 xpt_schedule(periph, priority);
6262 if (softc->action == PROBE_DV_EXIT) {
6263 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6264 ("Leave Domain Validation\n"));
6266 path->device->flags &=
6267 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
6268 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6269 /* Inform the XPT that a new device has been found */
6270 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6271 xpt_action(done_ccb);
6272 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6275 xpt_release_ccb(done_ccb);
6277 case PROBE_INQUIRY_BASIC_DV1:
6278 case PROBE_INQUIRY_BASIC_DV2:
6280 struct scsi_inquiry_data *nbuf;
6281 struct ccb_scsiio *csio;
6283 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6284 /* Don't wedge the queue */
6285 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6288 csio = &done_ccb->csio;
6289 nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
6290 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
6292 "inquiry data fails comparison at DV%d step\n",
6293 softc->action == PROBE_INQUIRY_BASIC_DV1? 1 : 2);
6294 if (proberequestbackoff(periph, path->device)) {
6295 path->device->flags &= ~CAM_DEV_IN_DV;
6296 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6299 softc->action = PROBE_DV_EXIT;
6302 xpt_release_ccb(done_ccb);
6303 xpt_schedule(periph, priority);
6307 if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
6308 softc->action = PROBE_INQUIRY_BASIC_DV2;
6309 xpt_release_ccb(done_ccb);
6310 xpt_schedule(periph, priority);
6313 if (softc->action == PROBE_DV_EXIT) {
6314 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6315 ("Leave Domain Validation Successfully\n"));
6317 path->device->flags &=
6318 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
6319 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6320 /* Inform the XPT that a new device has been found */
6321 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6322 xpt_action(done_ccb);
6323 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6326 xpt_release_ccb(done_ccb);
6330 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6331 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6332 done_ccb->ccb_h.status = CAM_REQ_CMP;
6334 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6335 cam_periph_invalidate(periph);
6336 cam_periph_release(periph);
6338 probeschedule(periph);
6343 probecleanup(struct cam_periph *periph)
6345 free(periph->softc, M_TEMP);
6349 xpt_find_quirk(struct cam_ed *device)
6353 match = cam_quirkmatch((caddr_t)&device->inq_data,
6354 (caddr_t)xpt_quirk_table,
6355 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6356 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6359 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6361 device->quirk = (struct xpt_quirk_entry *)match;
6365 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
6370 error = sysctl_handle_int(oidp, &bool, sizeof(bool), req);
6371 if (error != 0 || req->newptr == NULL)
6373 if (bool == 0 || bool == 1) {
6383 xpt_devise_transport(struct cam_path *path)
6385 struct ccb_pathinq cpi;
6386 struct ccb_trans_settings cts;
6387 struct scsi_inquiry_data *inq_buf;
6389 /* Get transport information from the SIM */
6390 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6391 cpi.ccb_h.func_code = XPT_PATH_INQ;
6392 xpt_action((union ccb *)&cpi);
6395 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6396 inq_buf = &path->device->inq_data;
6397 path->device->protocol = PROTO_SCSI;
6398 path->device->protocol_version =
6399 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6400 path->device->transport = cpi.transport;
6401 path->device->transport_version = cpi.transport_version;
6404 * Any device not using SPI3 features should
6405 * be considered SPI2 or lower.
6407 if (inq_buf != NULL) {
6408 if (path->device->transport == XPORT_SPI
6409 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6410 && path->device->transport_version > 2)
6411 path->device->transport_version = 2;
6413 struct cam_ed* otherdev;
6415 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6417 otherdev = TAILQ_NEXT(otherdev, links)) {
6418 if (otherdev != path->device)
6422 if (otherdev != NULL) {
6424 * Initially assume the same versioning as
6425 * prior luns for this target.
6427 path->device->protocol_version =
6428 otherdev->protocol_version;
6429 path->device->transport_version =
6430 otherdev->transport_version;
6432 /* Until we know better, opt for safty */
6433 path->device->protocol_version = 2;
6434 if (path->device->transport == XPORT_SPI)
6435 path->device->transport_version = 2;
6437 path->device->transport_version = 0;
6443 * For a device compliant with SPC-2 we should be able
6444 * to determine the transport version supported by
6445 * scrutinizing the version descriptors in the
6449 /* Tell the controller what we think */
6450 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6451 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6452 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6453 cts.transport = path->device->transport;
6454 cts.transport_version = path->device->transport_version;
6455 cts.protocol = path->device->protocol;
6456 cts.protocol_version = path->device->protocol_version;
6457 cts.proto_specific.valid = 0;
6458 cts.xport_specific.valid = 0;
6459 xpt_action((union ccb *)&cts);
6463 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6466 struct ccb_pathinq cpi;
6467 struct ccb_trans_settings cur_cts;
6468 struct ccb_trans_settings_scsi *scsi;
6469 struct ccb_trans_settings_scsi *cur_scsi;
6470 struct cam_sim *sim;
6471 struct scsi_inquiry_data *inq_data;
6473 if (device == NULL) {
6474 cts->ccb_h.status = CAM_PATH_INVALID;
6475 xpt_done((union ccb *)cts);
6479 if (cts->protocol == PROTO_UNKNOWN
6480 || cts->protocol == PROTO_UNSPECIFIED) {
6481 cts->protocol = device->protocol;
6482 cts->protocol_version = device->protocol_version;
6485 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6486 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6487 cts->protocol_version = device->protocol_version;
6489 if (cts->protocol != device->protocol) {
6490 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
6491 cts->protocol, device->protocol);
6492 cts->protocol = device->protocol;
6495 if (cts->protocol_version > device->protocol_version) {
6497 xpt_print(cts->ccb_h.path, "Down reving Protocol "
6498 "Version from %d to %d?\n", cts->protocol_version,
6499 device->protocol_version);
6501 cts->protocol_version = device->protocol_version;
6504 if (cts->transport == XPORT_UNKNOWN
6505 || cts->transport == XPORT_UNSPECIFIED) {
6506 cts->transport = device->transport;
6507 cts->transport_version = device->transport_version;
6510 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6511 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6512 cts->transport_version = device->transport_version;
6514 if (cts->transport != device->transport) {
6515 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
6516 cts->transport, device->transport);
6517 cts->transport = device->transport;
6520 if (cts->transport_version > device->transport_version) {
6522 xpt_print(cts->ccb_h.path, "Down reving Transport "
6523 "Version from %d to %d?\n", cts->transport_version,
6524 device->transport_version);
6526 cts->transport_version = device->transport_version;
6529 sim = cts->ccb_h.path->bus->sim;
6532 * Nothing more of interest to do unless
6533 * this is a device connected via the
6536 if (cts->protocol != PROTO_SCSI) {
6537 if (async_update == FALSE)
6538 (*(sim->sim_action))(sim, (union ccb *)cts);
6542 inq_data = &device->inq_data;
6543 scsi = &cts->proto_specific.scsi;
6544 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6545 cpi.ccb_h.func_code = XPT_PATH_INQ;
6546 xpt_action((union ccb *)&cpi);
6548 /* SCSI specific sanity checking */
6549 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6550 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
6551 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6552 || (device->quirk->mintags == 0)) {
6554 * Can't tag on hardware that doesn't support tags,
6555 * doesn't have it enabled, or has broken tag support.
6557 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6560 if (async_update == FALSE) {
6562 * Perform sanity checking against what the
6563 * controller and device can do.
6565 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6566 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6567 cur_cts.type = cts->type;
6568 xpt_action((union ccb *)&cur_cts);
6569 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6572 cur_scsi = &cur_cts.proto_specific.scsi;
6573 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6574 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6575 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6577 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6578 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6581 /* SPI specific sanity checking */
6582 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6584 struct ccb_trans_settings_spi *spi;
6585 struct ccb_trans_settings_spi *cur_spi;
6587 spi = &cts->xport_specific.spi;
6589 cur_spi = &cur_cts.xport_specific.spi;
6591 /* Fill in any gaps in what the user gave us */
6592 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6593 spi->sync_period = cur_spi->sync_period;
6594 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6595 spi->sync_period = 0;
6596 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6597 spi->sync_offset = cur_spi->sync_offset;
6598 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6599 spi->sync_offset = 0;
6600 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6601 spi->ppr_options = cur_spi->ppr_options;
6602 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6603 spi->ppr_options = 0;
6604 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6605 spi->bus_width = cur_spi->bus_width;
6606 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6608 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6609 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6610 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6612 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6613 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6614 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6615 && (inq_data->flags & SID_Sync) == 0
6616 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6617 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6618 || (spi->sync_offset == 0)
6619 || (spi->sync_period == 0)) {
6621 spi->sync_period = 0;
6622 spi->sync_offset = 0;
6625 switch (spi->bus_width) {
6626 case MSG_EXT_WDTR_BUS_32_BIT:
6627 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6628 || (inq_data->flags & SID_WBus32) != 0
6629 || cts->type == CTS_TYPE_USER_SETTINGS)
6630 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6632 /* Fall Through to 16-bit */
6633 case MSG_EXT_WDTR_BUS_16_BIT:
6634 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6635 || (inq_data->flags & SID_WBus16) != 0
6636 || cts->type == CTS_TYPE_USER_SETTINGS)
6637 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6638 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6641 /* Fall Through to 8-bit */
6642 default: /* New bus width?? */
6643 case MSG_EXT_WDTR_BUS_8_BIT:
6644 /* All targets can do this */
6645 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6649 spi3caps = cpi.xport_specific.spi.ppr_options;
6650 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6651 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6652 spi3caps &= inq_data->spi3data;
6654 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6655 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6657 if ((spi3caps & SID_SPI_IUS) == 0)
6658 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6660 if ((spi3caps & SID_SPI_QAS) == 0)
6661 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6663 /* No SPI Transfer settings are allowed unless we are wide */
6664 if (spi->bus_width == 0)
6665 spi->ppr_options = 0;
6667 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6669 * Can't tag queue without disconnection.
6671 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6672 scsi->valid |= CTS_SCSI_VALID_TQ;
6676 * If we are currently performing tagged transactions to
6677 * this device and want to change its negotiation parameters,
6678 * go non-tagged for a bit to give the controller a chance to
6679 * negotiate unhampered by tag messages.
6681 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6682 && (device->inq_flags & SID_CmdQue) != 0
6683 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6684 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6685 CTS_SPI_VALID_SYNC_OFFSET|
6686 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6687 xpt_toggle_tags(cts->ccb_h.path);
6690 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6691 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6695 * If we are transitioning from tags to no-tags or
6696 * vice-versa, we need to carefully freeze and restart
6697 * the queue so that we don't overlap tagged and non-tagged
6698 * commands. We also temporarily stop tags if there is
6699 * a change in transfer negotiation settings to allow
6700 * "tag-less" negotiation.
6702 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6703 || (device->inq_flags & SID_CmdQue) != 0)
6704 device_tagenb = TRUE;
6706 device_tagenb = FALSE;
6708 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6709 && device_tagenb == FALSE)
6710 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6711 && device_tagenb == TRUE)) {
6713 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6715 * Delay change to use tags until after a
6716 * few commands have gone to this device so
6717 * the controller has time to perform transfer
6718 * negotiations without tagged messages getting
6721 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6722 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6724 struct ccb_relsim crs;
6726 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6727 device->inq_flags &= ~SID_CmdQue;
6728 xpt_dev_ccbq_resize(cts->ccb_h.path,
6729 sim->max_dev_openings);
6730 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6731 device->tag_delay_count = 0;
6733 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6735 crs.ccb_h.func_code = XPT_REL_SIMQ;
6736 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6738 = crs.release_timeout
6741 xpt_action((union ccb *)&crs);
6745 if (async_update == FALSE)
6746 (*(sim->sim_action))(sim, (union ccb *)cts);
6751 xpt_toggle_tags(struct cam_path *path)
6756 * Give controllers a chance to renegotiate
6757 * before starting tag operations. We
6758 * "toggle" tagged queuing off then on
6759 * which causes the tag enable command delay
6760 * counter to come into effect.
6763 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6764 || ((dev->inq_flags & SID_CmdQue) != 0
6765 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6766 struct ccb_trans_settings cts;
6768 xpt_setup_ccb(&cts.ccb_h, path, 1);
6769 cts.protocol = PROTO_SCSI;
6770 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6771 cts.transport = XPORT_UNSPECIFIED;
6772 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6773 cts.proto_specific.scsi.flags = 0;
6774 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6775 xpt_set_transfer_settings(&cts, path->device,
6776 /*async_update*/TRUE);
6777 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6778 xpt_set_transfer_settings(&cts, path->device,
6779 /*async_update*/TRUE);
6784 xpt_start_tags(struct cam_path *path)
6786 struct ccb_relsim crs;
6787 struct cam_ed *device;
6788 struct cam_sim *sim;
6791 device = path->device;
6792 sim = path->bus->sim;
6793 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6794 xpt_freeze_devq(path, /*count*/1);
6795 device->inq_flags |= SID_CmdQue;
6796 if (device->tag_saved_openings != 0)
6797 newopenings = device->tag_saved_openings;
6799 newopenings = min(device->quirk->maxtags,
6800 sim->max_tagged_dev_openings);
6801 xpt_dev_ccbq_resize(path, newopenings);
6802 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6803 crs.ccb_h.func_code = XPT_REL_SIMQ;
6804 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6806 = crs.release_timeout
6809 xpt_action((union ccb *)&crs);
6812 static int busses_to_config;
6813 static int busses_to_reset;
6816 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6818 if (bus->path_id != CAM_XPT_PATH_ID) {
6819 struct cam_path path;
6820 struct ccb_pathinq cpi;
6824 xpt_compile_path(&path, NULL, bus->path_id,
6825 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6826 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6827 cpi.ccb_h.func_code = XPT_PATH_INQ;
6828 xpt_action((union ccb *)&cpi);
6829 can_negotiate = cpi.hba_inquiry;
6830 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6831 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6834 xpt_release_path(&path);
6841 xptconfigfunc(struct cam_eb *bus, void *arg)
6843 struct cam_path *path;
6844 union ccb *work_ccb;
6846 if (bus->path_id != CAM_XPT_PATH_ID) {
6850 work_ccb = xpt_alloc_ccb();
6851 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6852 CAM_TARGET_WILDCARD,
6853 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6854 printf("xptconfigfunc: xpt_create_path failed with "
6855 "status %#x for bus %d\n", status, bus->path_id);
6856 printf("xptconfigfunc: halting bus configuration\n");
6857 xpt_free_ccb(work_ccb);
6859 xpt_finishconfig(xpt_periph, NULL);
6862 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6863 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6864 xpt_action(work_ccb);
6865 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6866 printf("xptconfigfunc: CPI failed on bus %d "
6867 "with status %d\n", bus->path_id,
6868 work_ccb->ccb_h.status);
6869 xpt_finishconfig(xpt_periph, work_ccb);
6873 can_negotiate = work_ccb->cpi.hba_inquiry;
6874 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6875 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6876 && (can_negotiate != 0)) {
6877 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6878 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6879 work_ccb->ccb_h.cbfcnp = NULL;
6880 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6881 ("Resetting Bus\n"));
6882 xpt_action(work_ccb);
6883 xpt_finishconfig(xpt_periph, work_ccb);
6885 /* Act as though we performed a successful BUS RESET */
6886 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6887 xpt_finishconfig(xpt_periph, work_ccb);
6895 xpt_config(void *arg)
6898 * Now that interrupts are enabled, go find our devices
6902 /* Setup debugging flags and path */
6903 #ifdef CAM_DEBUG_FLAGS
6904 cam_dflags = CAM_DEBUG_FLAGS;
6905 #else /* !CAM_DEBUG_FLAGS */
6906 cam_dflags = CAM_DEBUG_NONE;
6907 #endif /* CAM_DEBUG_FLAGS */
6908 #ifdef CAM_DEBUG_BUS
6909 if (cam_dflags != CAM_DEBUG_NONE) {
6910 if (xpt_create_path(&cam_dpath, xpt_periph,
6911 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6912 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6913 printf("xpt_config: xpt_create_path() failed for debug"
6914 " target %d:%d:%d, debugging disabled\n",
6915 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6916 cam_dflags = CAM_DEBUG_NONE;
6920 #else /* !CAM_DEBUG_BUS */
6922 #endif /* CAM_DEBUG_BUS */
6923 #endif /* CAMDEBUG */
6926 * Scan all installed busses.
6928 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6930 if (busses_to_config == 0) {
6931 /* Call manually because we don't have any busses */
6932 xpt_finishconfig(xpt_periph, NULL);
6934 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6935 printf("Waiting %d seconds for SCSI "
6936 "devices to settle\n", scsi_delay/1000);
6938 xpt_for_all_busses(xptconfigfunc, NULL);
6943 * If the given device only has one peripheral attached to it, and if that
6944 * peripheral is the passthrough driver, announce it. This insures that the
6945 * user sees some sort of announcement for every peripheral in their system.
6948 xptpassannouncefunc(struct cam_ed *device, void *arg)
6950 struct cam_periph *periph;
6953 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6954 periph = SLIST_NEXT(periph, periph_links), i++);
6956 periph = SLIST_FIRST(&device->periphs);
6958 && (strncmp(periph->periph_name, "pass", 4) == 0))
6959 xpt_announce_periph(periph, NULL);
6965 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6967 struct periph_driver **p_drv;
6970 if (done_ccb != NULL) {
6971 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6972 ("xpt_finishconfig\n"));
6973 switch(done_ccb->ccb_h.func_code) {
6975 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6976 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6977 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6978 done_ccb->crcn.flags = 0;
6979 xpt_action(done_ccb);
6985 xpt_free_path(done_ccb->ccb_h.path);
6991 if (busses_to_config == 0) {
6992 /* Register all the peripheral drivers */
6993 /* XXX This will have to change when we have loadable modules */
6994 p_drv = periph_drivers;
6995 for (i = 0; p_drv[i] != NULL; i++) {
6996 (*p_drv[i]->init)();
7000 * Check for devices with no "standard" peripheral driver
7001 * attached. For any devices like that, announce the
7002 * passthrough driver so the user will see something.
7004 xpt_for_all_devices(xptpassannouncefunc, NULL);
7006 /* Release our hook so that the boot can continue. */
7007 config_intrhook_disestablish(xpt_config_hook);
7008 free(xpt_config_hook, M_TEMP);
7009 xpt_config_hook = NULL;
7011 if (done_ccb != NULL)
7012 xpt_free_ccb(done_ccb);
7016 xptaction(struct cam_sim *sim, union ccb *work_ccb)
7018 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
7020 switch (work_ccb->ccb_h.func_code) {
7021 /* Common cases first */
7022 case XPT_PATH_INQ: /* Path routing inquiry */
7024 struct ccb_pathinq *cpi;
7026 cpi = &work_ccb->cpi;
7027 cpi->version_num = 1; /* XXX??? */
7028 cpi->hba_inquiry = 0;
7029 cpi->target_sprt = 0;
7031 cpi->hba_eng_cnt = 0;
7032 cpi->max_target = 0;
7034 cpi->initiator_id = 0;
7035 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
7036 strncpy(cpi->hba_vid, "", HBA_IDLEN);
7037 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
7038 cpi->unit_number = sim->unit_number;
7039 cpi->bus_id = sim->bus_id;
7040 cpi->base_transfer_speed = 0;
7041 cpi->protocol = PROTO_UNSPECIFIED;
7042 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
7043 cpi->transport = XPORT_UNSPECIFIED;
7044 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
7045 cpi->ccb_h.status = CAM_REQ_CMP;
7050 work_ccb->ccb_h.status = CAM_REQ_INVALID;
7057 * The xpt as a "controller" has no interrupt sources, so polling
7061 xptpoll(struct cam_sim *sim)
7066 camisr(void *V_queue)
7068 cam_isrq_t *oqueue = V_queue;
7071 struct ccb_hdr *ccb_h;
7074 * Transfer the ccb_bioq list to a temporary list so we can operate
7075 * on it without needing to lock/unlock on every loop. The concat
7076 * function with re-init the real list for us.
7079 mtx_lock(&cam_bioq_lock);
7081 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7082 mtx_unlock(&cam_bioq_lock);
7084 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7087 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7088 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7091 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7096 if (ccb_h->flags & CAM_HIGH_POWER) {
7097 struct highpowerlist *hphead;
7098 union ccb *send_ccb;
7100 hphead = &highpowerq;
7102 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7105 * Increment the count since this command is done.
7110 * Any high powered commands queued up?
7112 if (send_ccb != NULL) {
7114 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7116 xpt_release_devq(send_ccb->ccb_h.path,
7117 /*count*/1, /*runqueue*/TRUE);
7120 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7123 dev = ccb_h->path->device;
7126 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7128 if (!SIM_DEAD(ccb_h->path->bus->sim)) {
7129 ccb_h->path->bus->sim->devq->send_active--;
7130 ccb_h->path->bus->sim->devq->send_openings++;
7134 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7135 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7136 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7137 && (dev->ccbq.dev_active == 0))) {
7139 xpt_release_devq(ccb_h->path, /*count*/1,
7143 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7144 && (--dev->tag_delay_count == 0))
7145 xpt_start_tags(ccb_h->path);
7147 if ((dev->ccbq.queue.entries > 0)
7148 && (dev->qfrozen_cnt == 0)
7149 && (device_is_send_queued(dev) == 0)) {
7150 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7155 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7156 xpt_release_simq(ccb_h->path->bus->sim,
7158 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7162 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7163 && (ccb_h->status & CAM_DEV_QFRZN)) {
7164 xpt_release_devq(ccb_h->path, /*count*/1,
7166 ccb_h->status &= ~CAM_DEV_QFRZN;
7168 xpt_run_dev_sendq(ccb_h->path->bus);
7171 /* Call the peripheral driver's callback */
7172 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7174 /* Raise IPL for while test */
7181 dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7184 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7189 dead_sim_poll(struct cam_sim *sim)