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 if ((status = xpt_bus_register(xpt_sim, /*bus #*/0)) != CAM_SUCCESS) {
1450 printf("xpt_init: xpt_bus_register failed with status %#x,"
1451 " failing attach\n", status);
1456 * Looking at the XPT from the SIM layer, the XPT is
1457 * the equivelent of a peripheral driver. Allocate
1458 * a peripheral driver entry for us.
1460 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1461 CAM_TARGET_WILDCARD,
1462 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1463 printf("xpt_init: xpt_create_path failed with status %#x,"
1464 " failing attach\n", status);
1468 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1469 path, NULL, 0, NULL);
1470 xpt_free_path(path);
1472 xpt_sim->softc = xpt_periph;
1475 * Register a callback for when interrupts are enabled.
1478 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1479 M_TEMP, M_NOWAIT | M_ZERO);
1480 if (xpt_config_hook == NULL) {
1481 printf("xpt_init: Cannot malloc config hook "
1482 "- failing attach\n");
1486 xpt_config_hook->ich_func = xpt_config;
1487 if (config_intrhook_establish(xpt_config_hook) != 0) {
1488 free (xpt_config_hook, M_TEMP);
1489 printf("xpt_init: config_intrhook_establish failed "
1490 "- failing attach\n");
1493 /* Install our software interrupt handlers */
1494 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1498 xptregister(struct cam_periph *periph, void *arg)
1500 if (periph == NULL) {
1501 printf("xptregister: periph was NULL!!\n");
1502 return(CAM_REQ_CMP_ERR);
1505 periph->softc = NULL;
1507 xpt_periph = periph;
1509 return(CAM_REQ_CMP);
1513 xpt_add_periph(struct cam_periph *periph)
1515 struct cam_ed *device;
1517 struct periph_list *periph_head;
1521 device = periph->path->device;
1523 periph_head = &device->periphs;
1525 status = CAM_REQ_CMP;
1527 if (device != NULL) {
1531 * Make room for this peripheral
1532 * so it will fit in the queue
1533 * when it's scheduled to run
1536 status = camq_resize(&device->drvq,
1537 device->drvq.array_size + 1);
1539 device->generation++;
1541 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1546 xsoftc.generation++;
1552 xpt_remove_periph(struct cam_periph *periph)
1554 struct cam_ed *device;
1558 device = periph->path->device;
1560 if (device != NULL) {
1562 struct periph_list *periph_head;
1564 periph_head = &device->periphs;
1566 /* Release the slot for this peripheral */
1568 camq_resize(&device->drvq, device->drvq.array_size - 1);
1570 device->generation++;
1572 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1577 xsoftc.generation++;
1583 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1585 struct ccb_pathinq cpi;
1586 struct ccb_trans_settings cts;
1587 struct cam_path *path;
1595 path = periph->path;
1597 * To ensure that this is printed in one piece,
1598 * mask out CAM interrupts.
1601 printf("%s%d at %s%d bus %d target %d lun %d\n",
1602 periph->periph_name, periph->unit_number,
1603 path->bus->sim->sim_name,
1604 path->bus->sim->unit_number,
1605 path->bus->sim->bus_id,
1606 path->target->target_id,
1607 path->device->lun_id);
1608 printf("%s%d: ", periph->periph_name, periph->unit_number);
1609 scsi_print_inquiry(&path->device->inq_data);
1610 if (bootverbose && path->device->serial_num_len > 0) {
1611 /* Don't wrap the screen - print only the first 60 chars */
1612 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1613 periph->unit_number, path->device->serial_num);
1615 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1616 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1617 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1618 xpt_action((union ccb*)&cts);
1619 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1623 /* Ask the SIM for its base transfer speed */
1624 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1625 cpi.ccb_h.func_code = XPT_PATH_INQ;
1626 xpt_action((union ccb *)&cpi);
1628 speed = cpi.base_transfer_speed;
1630 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1631 struct ccb_trans_settings_spi *spi;
1633 spi = &cts.xport_specific.spi;
1634 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1635 && spi->sync_offset != 0) {
1636 freq = scsi_calc_syncsrate(spi->sync_period);
1640 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1641 speed *= (0x01 << spi->bus_width);
1644 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1645 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1646 if (fc->valid & CTS_FC_VALID_SPEED) {
1647 speed = fc->bitrate;
1651 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1652 struct ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1653 if (sas->valid & CTS_SAS_VALID_SPEED) {
1654 speed = sas->bitrate;
1660 printf("%s%d: %d.%03dMB/s transfers",
1661 periph->periph_name, periph->unit_number,
1664 printf("%s%d: %dKB/s transfers", periph->periph_name,
1665 periph->unit_number, speed);
1666 /* Report additional information about SPI connections */
1667 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1668 struct ccb_trans_settings_spi *spi;
1670 spi = &cts.xport_specific.spi;
1672 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1674 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1678 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1679 && spi->bus_width > 0) {
1685 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1686 } else if (freq != 0) {
1690 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1691 struct ccb_trans_settings_fc *fc;
1693 fc = &cts.xport_specific.fc;
1694 if (fc->valid & CTS_FC_VALID_WWNN)
1695 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1696 if (fc->valid & CTS_FC_VALID_WWPN)
1697 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1698 if (fc->valid & CTS_FC_VALID_PORT)
1699 printf(" PortID 0x%x", fc->port);
1702 if (path->device->inq_flags & SID_CmdQue
1703 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1704 printf("\n%s%d: Command Queueing Enabled",
1705 periph->periph_name, periph->unit_number);
1710 * We only want to print the caller's announce string if they've
1713 if (announce_string != NULL)
1714 printf("%s%d: %s\n", periph->periph_name,
1715 periph->unit_number, announce_string);
1719 static dev_match_ret
1720 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1723 dev_match_ret retval;
1726 retval = DM_RET_NONE;
1729 * If we aren't given something to match against, that's an error.
1732 return(DM_RET_ERROR);
1735 * If there are no match entries, then this bus matches no
1738 if ((patterns == NULL) || (num_patterns == 0))
1739 return(DM_RET_DESCEND | DM_RET_COPY);
1741 for (i = 0; i < num_patterns; i++) {
1742 struct bus_match_pattern *cur_pattern;
1745 * If the pattern in question isn't for a bus node, we
1746 * aren't interested. However, we do indicate to the
1747 * calling routine that we should continue descending the
1748 * tree, since the user wants to match against lower-level
1751 if (patterns[i].type != DEV_MATCH_BUS) {
1752 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1753 retval |= DM_RET_DESCEND;
1757 cur_pattern = &patterns[i].pattern.bus_pattern;
1760 * If they want to match any bus node, we give them any
1763 if (cur_pattern->flags == BUS_MATCH_ANY) {
1764 /* set the copy flag */
1765 retval |= DM_RET_COPY;
1768 * If we've already decided on an action, go ahead
1771 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1776 * Not sure why someone would do this...
1778 if (cur_pattern->flags == BUS_MATCH_NONE)
1781 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1782 && (cur_pattern->path_id != bus->path_id))
1785 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1786 && (cur_pattern->bus_id != bus->sim->bus_id))
1789 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1790 && (cur_pattern->unit_number != bus->sim->unit_number))
1793 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1794 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1799 * If we get to this point, the user definitely wants
1800 * information on this bus. So tell the caller to copy the
1803 retval |= DM_RET_COPY;
1806 * If the return action has been set to descend, then we
1807 * know that we've already seen a non-bus matching
1808 * expression, therefore we need to further descend the tree.
1809 * This won't change by continuing around the loop, so we
1810 * go ahead and return. If we haven't seen a non-bus
1811 * matching expression, we keep going around the loop until
1812 * we exhaust the matching expressions. We'll set the stop
1813 * flag once we fall out of the loop.
1815 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1820 * If the return action hasn't been set to descend yet, that means
1821 * we haven't seen anything other than bus matching patterns. So
1822 * tell the caller to stop descending the tree -- the user doesn't
1823 * want to match against lower level tree elements.
1825 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1826 retval |= DM_RET_STOP;
1831 static dev_match_ret
1832 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1833 struct cam_ed *device)
1835 dev_match_ret retval;
1838 retval = DM_RET_NONE;
1841 * If we aren't given something to match against, that's an error.
1844 return(DM_RET_ERROR);
1847 * If there are no match entries, then this device matches no
1850 if ((patterns == NULL) || (num_patterns == 0))
1851 return(DM_RET_DESCEND | DM_RET_COPY);
1853 for (i = 0; i < num_patterns; i++) {
1854 struct device_match_pattern *cur_pattern;
1857 * If the pattern in question isn't for a device node, we
1858 * aren't interested.
1860 if (patterns[i].type != DEV_MATCH_DEVICE) {
1861 if ((patterns[i].type == DEV_MATCH_PERIPH)
1862 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1863 retval |= DM_RET_DESCEND;
1867 cur_pattern = &patterns[i].pattern.device_pattern;
1870 * If they want to match any device node, we give them any
1873 if (cur_pattern->flags == DEV_MATCH_ANY) {
1874 /* set the copy flag */
1875 retval |= DM_RET_COPY;
1879 * If we've already decided on an action, go ahead
1882 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1887 * Not sure why someone would do this...
1889 if (cur_pattern->flags == DEV_MATCH_NONE)
1892 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1893 && (cur_pattern->path_id != device->target->bus->path_id))
1896 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1897 && (cur_pattern->target_id != device->target->target_id))
1900 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1901 && (cur_pattern->target_lun != device->lun_id))
1904 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1905 && (cam_quirkmatch((caddr_t)&device->inq_data,
1906 (caddr_t)&cur_pattern->inq_pat,
1907 1, sizeof(cur_pattern->inq_pat),
1908 scsi_static_inquiry_match) == NULL))
1912 * If we get to this point, the user definitely wants
1913 * information on this device. So tell the caller to copy
1916 retval |= DM_RET_COPY;
1919 * If the return action has been set to descend, then we
1920 * know that we've already seen a peripheral matching
1921 * expression, therefore we need to further descend the tree.
1922 * This won't change by continuing around the loop, so we
1923 * go ahead and return. If we haven't seen a peripheral
1924 * matching expression, we keep going around the loop until
1925 * we exhaust the matching expressions. We'll set the stop
1926 * flag once we fall out of the loop.
1928 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1933 * If the return action hasn't been set to descend yet, that means
1934 * we haven't seen any peripheral matching patterns. So tell the
1935 * caller to stop descending the tree -- the user doesn't want to
1936 * match against lower level tree elements.
1938 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1939 retval |= DM_RET_STOP;
1945 * Match a single peripheral against any number of match patterns.
1947 static dev_match_ret
1948 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1949 struct cam_periph *periph)
1951 dev_match_ret retval;
1955 * If we aren't given something to match against, that's an error.
1958 return(DM_RET_ERROR);
1961 * If there are no match entries, then this peripheral matches no
1964 if ((patterns == NULL) || (num_patterns == 0))
1965 return(DM_RET_STOP | DM_RET_COPY);
1968 * There aren't any nodes below a peripheral node, so there's no
1969 * reason to descend the tree any further.
1971 retval = DM_RET_STOP;
1973 for (i = 0; i < num_patterns; i++) {
1974 struct periph_match_pattern *cur_pattern;
1977 * If the pattern in question isn't for a peripheral, we
1978 * aren't interested.
1980 if (patterns[i].type != DEV_MATCH_PERIPH)
1983 cur_pattern = &patterns[i].pattern.periph_pattern;
1986 * If they want to match on anything, then we will do so.
1988 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1989 /* set the copy flag */
1990 retval |= DM_RET_COPY;
1993 * We've already set the return action to stop,
1994 * since there are no nodes below peripherals in
2001 * Not sure why someone would do this...
2003 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2006 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2007 && (cur_pattern->path_id != periph->path->bus->path_id))
2011 * For the target and lun id's, we have to make sure the
2012 * target and lun pointers aren't NULL. The xpt peripheral
2013 * has a wildcard target and device.
2015 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2016 && ((periph->path->target == NULL)
2017 ||(cur_pattern->target_id != periph->path->target->target_id)))
2020 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2021 && ((periph->path->device == NULL)
2022 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2025 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2026 && (cur_pattern->unit_number != periph->unit_number))
2029 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2030 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2035 * If we get to this point, the user definitely wants
2036 * information on this peripheral. So tell the caller to
2037 * copy the data out.
2039 retval |= DM_RET_COPY;
2042 * The return action has already been set to stop, since
2043 * peripherals don't have any nodes below them in the EDT.
2049 * If we get to this point, the peripheral that was passed in
2050 * doesn't match any of the patterns.
2056 xptedtbusfunc(struct cam_eb *bus, void *arg)
2058 struct ccb_dev_match *cdm;
2059 dev_match_ret retval;
2061 cdm = (struct ccb_dev_match *)arg;
2064 * If our position is for something deeper in the tree, that means
2065 * that we've already seen this node. So, we keep going down.
2067 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2068 && (cdm->pos.cookie.bus == bus)
2069 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2070 && (cdm->pos.cookie.target != NULL))
2071 retval = DM_RET_DESCEND;
2073 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2076 * If we got an error, bail out of the search.
2078 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2079 cdm->status = CAM_DEV_MATCH_ERROR;
2084 * If the copy flag is set, copy this bus out.
2086 if (retval & DM_RET_COPY) {
2089 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2090 sizeof(struct dev_match_result));
2093 * If we don't have enough space to put in another
2094 * match result, save our position and tell the
2095 * user there are more devices to check.
2097 if (spaceleft < sizeof(struct dev_match_result)) {
2098 bzero(&cdm->pos, sizeof(cdm->pos));
2099 cdm->pos.position_type =
2100 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2102 cdm->pos.cookie.bus = bus;
2103 cdm->pos.generations[CAM_BUS_GENERATION]=
2105 cdm->status = CAM_DEV_MATCH_MORE;
2108 j = cdm->num_matches;
2110 cdm->matches[j].type = DEV_MATCH_BUS;
2111 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2112 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2113 cdm->matches[j].result.bus_result.unit_number =
2114 bus->sim->unit_number;
2115 strncpy(cdm->matches[j].result.bus_result.dev_name,
2116 bus->sim->sim_name, DEV_IDLEN);
2120 * If the user is only interested in busses, there's no
2121 * reason to descend to the next level in the tree.
2123 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2127 * If there is a target generation recorded, check it to
2128 * make sure the target list hasn't changed.
2130 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2131 && (bus == cdm->pos.cookie.bus)
2132 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2133 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2134 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2136 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2140 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2141 && (cdm->pos.cookie.bus == bus)
2142 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2143 && (cdm->pos.cookie.target != NULL))
2144 return(xpttargettraverse(bus,
2145 (struct cam_et *)cdm->pos.cookie.target,
2146 xptedttargetfunc, arg));
2148 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2152 xptedttargetfunc(struct cam_et *target, void *arg)
2154 struct ccb_dev_match *cdm;
2156 cdm = (struct ccb_dev_match *)arg;
2159 * If there is a device list generation recorded, check it to
2160 * make sure the device list hasn't changed.
2162 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2163 && (cdm->pos.cookie.bus == target->bus)
2164 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2165 && (cdm->pos.cookie.target == target)
2166 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2167 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2168 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2169 target->generation)) {
2170 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2174 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2175 && (cdm->pos.cookie.bus == target->bus)
2176 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2177 && (cdm->pos.cookie.target == target)
2178 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2179 && (cdm->pos.cookie.device != NULL))
2180 return(xptdevicetraverse(target,
2181 (struct cam_ed *)cdm->pos.cookie.device,
2182 xptedtdevicefunc, arg));
2184 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2188 xptedtdevicefunc(struct cam_ed *device, void *arg)
2191 struct ccb_dev_match *cdm;
2192 dev_match_ret retval;
2194 cdm = (struct ccb_dev_match *)arg;
2197 * If our position is for something deeper in the tree, that means
2198 * that we've already seen this node. So, we keep going down.
2200 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2201 && (cdm->pos.cookie.device == device)
2202 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2203 && (cdm->pos.cookie.periph != NULL))
2204 retval = DM_RET_DESCEND;
2206 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2209 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2210 cdm->status = CAM_DEV_MATCH_ERROR;
2215 * If the copy flag is set, copy this device out.
2217 if (retval & DM_RET_COPY) {
2220 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2221 sizeof(struct dev_match_result));
2224 * If we don't have enough space to put in another
2225 * match result, save our position and tell the
2226 * user there are more devices to check.
2228 if (spaceleft < sizeof(struct dev_match_result)) {
2229 bzero(&cdm->pos, sizeof(cdm->pos));
2230 cdm->pos.position_type =
2231 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2232 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2234 cdm->pos.cookie.bus = device->target->bus;
2235 cdm->pos.generations[CAM_BUS_GENERATION]=
2237 cdm->pos.cookie.target = device->target;
2238 cdm->pos.generations[CAM_TARGET_GENERATION] =
2239 device->target->bus->generation;
2240 cdm->pos.cookie.device = device;
2241 cdm->pos.generations[CAM_DEV_GENERATION] =
2242 device->target->generation;
2243 cdm->status = CAM_DEV_MATCH_MORE;
2246 j = cdm->num_matches;
2248 cdm->matches[j].type = DEV_MATCH_DEVICE;
2249 cdm->matches[j].result.device_result.path_id =
2250 device->target->bus->path_id;
2251 cdm->matches[j].result.device_result.target_id =
2252 device->target->target_id;
2253 cdm->matches[j].result.device_result.target_lun =
2255 bcopy(&device->inq_data,
2256 &cdm->matches[j].result.device_result.inq_data,
2257 sizeof(struct scsi_inquiry_data));
2259 /* Let the user know whether this device is unconfigured */
2260 if (device->flags & CAM_DEV_UNCONFIGURED)
2261 cdm->matches[j].result.device_result.flags =
2262 DEV_RESULT_UNCONFIGURED;
2264 cdm->matches[j].result.device_result.flags =
2269 * If the user isn't interested in peripherals, don't descend
2270 * the tree any further.
2272 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2276 * If there is a peripheral list generation recorded, make sure
2277 * it hasn't changed.
2279 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2280 && (device->target->bus == cdm->pos.cookie.bus)
2281 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2282 && (device->target == cdm->pos.cookie.target)
2283 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2284 && (device == cdm->pos.cookie.device)
2285 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2286 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2287 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2288 device->generation)){
2289 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2293 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2294 && (cdm->pos.cookie.bus == device->target->bus)
2295 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2296 && (cdm->pos.cookie.target == device->target)
2297 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2298 && (cdm->pos.cookie.device == device)
2299 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2300 && (cdm->pos.cookie.periph != NULL))
2301 return(xptperiphtraverse(device,
2302 (struct cam_periph *)cdm->pos.cookie.periph,
2303 xptedtperiphfunc, arg));
2305 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2309 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2311 struct ccb_dev_match *cdm;
2312 dev_match_ret retval;
2314 cdm = (struct ccb_dev_match *)arg;
2316 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2318 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2319 cdm->status = CAM_DEV_MATCH_ERROR;
2324 * If the copy flag is set, copy this peripheral out.
2326 if (retval & DM_RET_COPY) {
2329 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2330 sizeof(struct dev_match_result));
2333 * If we don't have enough space to put in another
2334 * match result, save our position and tell the
2335 * user there are more devices to check.
2337 if (spaceleft < sizeof(struct dev_match_result)) {
2338 bzero(&cdm->pos, sizeof(cdm->pos));
2339 cdm->pos.position_type =
2340 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2341 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2344 cdm->pos.cookie.bus = periph->path->bus;
2345 cdm->pos.generations[CAM_BUS_GENERATION]=
2347 cdm->pos.cookie.target = periph->path->target;
2348 cdm->pos.generations[CAM_TARGET_GENERATION] =
2349 periph->path->bus->generation;
2350 cdm->pos.cookie.device = periph->path->device;
2351 cdm->pos.generations[CAM_DEV_GENERATION] =
2352 periph->path->target->generation;
2353 cdm->pos.cookie.periph = periph;
2354 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2355 periph->path->device->generation;
2356 cdm->status = CAM_DEV_MATCH_MORE;
2360 j = cdm->num_matches;
2362 cdm->matches[j].type = DEV_MATCH_PERIPH;
2363 cdm->matches[j].result.periph_result.path_id =
2364 periph->path->bus->path_id;
2365 cdm->matches[j].result.periph_result.target_id =
2366 periph->path->target->target_id;
2367 cdm->matches[j].result.periph_result.target_lun =
2368 periph->path->device->lun_id;
2369 cdm->matches[j].result.periph_result.unit_number =
2370 periph->unit_number;
2371 strncpy(cdm->matches[j].result.periph_result.periph_name,
2372 periph->periph_name, DEV_IDLEN);
2379 xptedtmatch(struct ccb_dev_match *cdm)
2383 cdm->num_matches = 0;
2386 * Check the bus list generation. If it has changed, the user
2387 * needs to reset everything and start over.
2389 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2390 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2391 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2392 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2396 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2397 && (cdm->pos.cookie.bus != NULL))
2398 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2399 xptedtbusfunc, cdm);
2401 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2404 * If we get back 0, that means that we had to stop before fully
2405 * traversing the EDT. It also means that one of the subroutines
2406 * has set the status field to the proper value. If we get back 1,
2407 * we've fully traversed the EDT and copied out any matching entries.
2410 cdm->status = CAM_DEV_MATCH_LAST;
2416 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2418 struct ccb_dev_match *cdm;
2420 cdm = (struct ccb_dev_match *)arg;
2422 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2423 && (cdm->pos.cookie.pdrv == pdrv)
2424 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2425 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2426 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2427 (*pdrv)->generation)) {
2428 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2432 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2433 && (cdm->pos.cookie.pdrv == pdrv)
2434 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2435 && (cdm->pos.cookie.periph != NULL))
2436 return(xptpdperiphtraverse(pdrv,
2437 (struct cam_periph *)cdm->pos.cookie.periph,
2438 xptplistperiphfunc, arg));
2440 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2444 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2446 struct ccb_dev_match *cdm;
2447 dev_match_ret retval;
2449 cdm = (struct ccb_dev_match *)arg;
2451 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2453 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2454 cdm->status = CAM_DEV_MATCH_ERROR;
2459 * If the copy flag is set, copy this peripheral out.
2461 if (retval & DM_RET_COPY) {
2464 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2465 sizeof(struct dev_match_result));
2468 * If we don't have enough space to put in another
2469 * match result, save our position and tell the
2470 * user there are more devices to check.
2472 if (spaceleft < sizeof(struct dev_match_result)) {
2473 struct periph_driver **pdrv;
2476 bzero(&cdm->pos, sizeof(cdm->pos));
2477 cdm->pos.position_type =
2478 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2482 * This may look a bit non-sensical, but it is
2483 * actually quite logical. There are very few
2484 * peripheral drivers, and bloating every peripheral
2485 * structure with a pointer back to its parent
2486 * peripheral driver linker set entry would cost
2487 * more in the long run than doing this quick lookup.
2489 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2490 if (strcmp((*pdrv)->driver_name,
2491 periph->periph_name) == 0)
2495 if (*pdrv == NULL) {
2496 cdm->status = CAM_DEV_MATCH_ERROR;
2500 cdm->pos.cookie.pdrv = pdrv;
2502 * The periph generation slot does double duty, as
2503 * does the periph pointer slot. They are used for
2504 * both edt and pdrv lookups and positioning.
2506 cdm->pos.cookie.periph = periph;
2507 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2508 (*pdrv)->generation;
2509 cdm->status = CAM_DEV_MATCH_MORE;
2513 j = cdm->num_matches;
2515 cdm->matches[j].type = DEV_MATCH_PERIPH;
2516 cdm->matches[j].result.periph_result.path_id =
2517 periph->path->bus->path_id;
2520 * The transport layer peripheral doesn't have a target or
2523 if (periph->path->target)
2524 cdm->matches[j].result.periph_result.target_id =
2525 periph->path->target->target_id;
2527 cdm->matches[j].result.periph_result.target_id = -1;
2529 if (periph->path->device)
2530 cdm->matches[j].result.periph_result.target_lun =
2531 periph->path->device->lun_id;
2533 cdm->matches[j].result.periph_result.target_lun = -1;
2535 cdm->matches[j].result.periph_result.unit_number =
2536 periph->unit_number;
2537 strncpy(cdm->matches[j].result.periph_result.periph_name,
2538 periph->periph_name, DEV_IDLEN);
2545 xptperiphlistmatch(struct ccb_dev_match *cdm)
2549 cdm->num_matches = 0;
2552 * At this point in the edt traversal function, we check the bus
2553 * list generation to make sure that no busses have been added or
2554 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2555 * For the peripheral driver list traversal function, however, we
2556 * don't have to worry about new peripheral driver types coming or
2557 * going; they're in a linker set, and therefore can't change
2558 * without a recompile.
2561 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2562 && (cdm->pos.cookie.pdrv != NULL))
2563 ret = xptpdrvtraverse(
2564 (struct periph_driver **)cdm->pos.cookie.pdrv,
2565 xptplistpdrvfunc, cdm);
2567 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2570 * If we get back 0, that means that we had to stop before fully
2571 * traversing the peripheral driver tree. It also means that one of
2572 * the subroutines has set the status field to the proper value. If
2573 * we get back 1, we've fully traversed the EDT and copied out any
2577 cdm->status = CAM_DEV_MATCH_LAST;
2583 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2585 struct cam_eb *bus, *next_bus;
2590 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2593 next_bus = TAILQ_NEXT(bus, links);
2595 retval = tr_func(bus, arg);
2604 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2605 xpt_targetfunc_t *tr_func, void *arg)
2607 struct cam_et *target, *next_target;
2611 for (target = (start_target ? start_target :
2612 TAILQ_FIRST(&bus->et_entries));
2613 target != NULL; target = next_target) {
2615 next_target = TAILQ_NEXT(target, links);
2617 retval = tr_func(target, arg);
2627 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2628 xpt_devicefunc_t *tr_func, void *arg)
2630 struct cam_ed *device, *next_device;
2634 for (device = (start_device ? start_device :
2635 TAILQ_FIRST(&target->ed_entries));
2637 device = next_device) {
2639 next_device = TAILQ_NEXT(device, links);
2641 retval = tr_func(device, arg);
2651 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2652 xpt_periphfunc_t *tr_func, void *arg)
2654 struct cam_periph *periph, *next_periph;
2659 for (periph = (start_periph ? start_periph :
2660 SLIST_FIRST(&device->periphs));
2662 periph = next_periph) {
2664 next_periph = SLIST_NEXT(periph, periph_links);
2666 retval = tr_func(periph, arg);
2675 xptpdrvtraverse(struct periph_driver **start_pdrv,
2676 xpt_pdrvfunc_t *tr_func, void *arg)
2678 struct periph_driver **pdrv;
2684 * We don't traverse the peripheral driver list like we do the
2685 * other lists, because it is a linker set, and therefore cannot be
2686 * changed during runtime. If the peripheral driver list is ever
2687 * re-done to be something other than a linker set (i.e. it can
2688 * change while the system is running), the list traversal should
2689 * be modified to work like the other traversal functions.
2691 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2692 *pdrv != NULL; pdrv++) {
2693 retval = tr_func(pdrv, arg);
2703 xptpdperiphtraverse(struct periph_driver **pdrv,
2704 struct cam_periph *start_periph,
2705 xpt_periphfunc_t *tr_func, void *arg)
2707 struct cam_periph *periph, *next_periph;
2712 for (periph = (start_periph ? start_periph :
2713 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2714 periph = next_periph) {
2716 next_periph = TAILQ_NEXT(periph, unit_links);
2718 retval = tr_func(periph, arg);
2726 xptdefbusfunc(struct cam_eb *bus, void *arg)
2728 struct xpt_traverse_config *tr_config;
2730 tr_config = (struct xpt_traverse_config *)arg;
2732 if (tr_config->depth == XPT_DEPTH_BUS) {
2733 xpt_busfunc_t *tr_func;
2735 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2737 return(tr_func(bus, tr_config->tr_arg));
2739 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2743 xptdeftargetfunc(struct cam_et *target, void *arg)
2745 struct xpt_traverse_config *tr_config;
2747 tr_config = (struct xpt_traverse_config *)arg;
2749 if (tr_config->depth == XPT_DEPTH_TARGET) {
2750 xpt_targetfunc_t *tr_func;
2752 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2754 return(tr_func(target, tr_config->tr_arg));
2756 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2760 xptdefdevicefunc(struct cam_ed *device, void *arg)
2762 struct xpt_traverse_config *tr_config;
2764 tr_config = (struct xpt_traverse_config *)arg;
2766 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2767 xpt_devicefunc_t *tr_func;
2769 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2771 return(tr_func(device, tr_config->tr_arg));
2773 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2777 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2779 struct xpt_traverse_config *tr_config;
2780 xpt_periphfunc_t *tr_func;
2782 tr_config = (struct xpt_traverse_config *)arg;
2784 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2787 * Unlike the other default functions, we don't check for depth
2788 * here. The peripheral driver level is the last level in the EDT,
2789 * so if we're here, we should execute the function in question.
2791 return(tr_func(periph, tr_config->tr_arg));
2795 * Execute the given function for every bus in the EDT.
2798 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2800 struct xpt_traverse_config tr_config;
2802 tr_config.depth = XPT_DEPTH_BUS;
2803 tr_config.tr_func = tr_func;
2804 tr_config.tr_arg = arg;
2806 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2811 * Execute the given function for every target in the EDT.
2814 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2816 struct xpt_traverse_config tr_config;
2818 tr_config.depth = XPT_DEPTH_TARGET;
2819 tr_config.tr_func = tr_func;
2820 tr_config.tr_arg = arg;
2822 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2824 #endif /* notusedyet */
2827 * Execute the given function for every device in the EDT.
2830 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2832 struct xpt_traverse_config tr_config;
2834 tr_config.depth = XPT_DEPTH_DEVICE;
2835 tr_config.tr_func = tr_func;
2836 tr_config.tr_arg = arg;
2838 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2843 * Execute the given function for every peripheral in the EDT.
2846 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2848 struct xpt_traverse_config tr_config;
2850 tr_config.depth = XPT_DEPTH_PERIPH;
2851 tr_config.tr_func = tr_func;
2852 tr_config.tr_arg = arg;
2854 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2856 #endif /* notusedyet */
2859 xptsetasyncfunc(struct cam_ed *device, void *arg)
2861 struct cam_path path;
2862 struct ccb_getdev cgd;
2863 struct async_node *cur_entry;
2865 cur_entry = (struct async_node *)arg;
2868 * Don't report unconfigured devices (Wildcard devs,
2869 * devices only for target mode, device instances
2870 * that have been invalidated but are waiting for
2871 * their last reference count to be released).
2873 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2876 xpt_compile_path(&path,
2878 device->target->bus->path_id,
2879 device->target->target_id,
2881 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2882 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2883 xpt_action((union ccb *)&cgd);
2884 cur_entry->callback(cur_entry->callback_arg,
2887 xpt_release_path(&path);
2893 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2895 struct cam_path path;
2896 struct ccb_pathinq cpi;
2897 struct async_node *cur_entry;
2899 cur_entry = (struct async_node *)arg;
2901 xpt_compile_path(&path, /*periph*/NULL,
2903 CAM_TARGET_WILDCARD,
2905 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2906 cpi.ccb_h.func_code = XPT_PATH_INQ;
2907 xpt_action((union ccb *)&cpi);
2908 cur_entry->callback(cur_entry->callback_arg,
2911 xpt_release_path(&path);
2917 xpt_action(union ccb *start_ccb)
2923 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2925 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2927 iopl = splsoftcam();
2928 switch (start_ccb->ccb_h.func_code) {
2931 struct cam_ed *device;
2933 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2934 struct cam_path *path;
2936 path = start_ccb->ccb_h.path;
2940 * For the sake of compatibility with SCSI-1
2941 * devices that may not understand the identify
2942 * message, we include lun information in the
2943 * second byte of all commands. SCSI-1 specifies
2944 * that luns are a 3 bit value and reserves only 3
2945 * bits for lun information in the CDB. Later
2946 * revisions of the SCSI spec allow for more than 8
2947 * luns, but have deprecated lun information in the
2948 * CDB. So, if the lun won't fit, we must omit.
2950 * Also be aware that during initial probing for devices,
2951 * the inquiry information is unknown but initialized to 0.
2952 * This means that this code will be exercised while probing
2953 * devices with an ANSI revision greater than 2.
2955 device = start_ccb->ccb_h.path->device;
2956 if (device->protocol_version <= SCSI_REV_2
2957 && start_ccb->ccb_h.target_lun < 8
2958 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2960 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2961 start_ccb->ccb_h.target_lun << 5;
2963 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2964 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2965 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2966 &path->device->inq_data),
2967 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2968 cdb_str, sizeof(cdb_str))));
2972 case XPT_CONT_TARGET_IO:
2973 start_ccb->csio.sense_resid = 0;
2974 start_ccb->csio.resid = 0;
2979 struct cam_path *path;
2980 struct cam_sim *sim;
2984 path = start_ccb->ccb_h.path;
2987 sim = path->bus->sim;
2988 if (SIM_DEAD(sim)) {
2989 /* The SIM has gone; just execute the CCB directly. */
2990 cam_ccbq_send_ccb(&path->device->ccbq, start_ccb);
2991 (*(sim->sim_action))(sim, start_ccb);
2996 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2997 if (path->device->qfrozen_cnt == 0)
2998 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3003 xpt_run_dev_sendq(path->bus);
3006 case XPT_SET_TRAN_SETTINGS:
3008 xpt_set_transfer_settings(&start_ccb->cts,
3009 start_ccb->ccb_h.path->device,
3010 /*async_update*/FALSE);
3013 case XPT_CALC_GEOMETRY:
3015 struct cam_sim *sim;
3017 /* Filter out garbage */
3018 if (start_ccb->ccg.block_size == 0
3019 || start_ccb->ccg.volume_size == 0) {
3020 start_ccb->ccg.cylinders = 0;
3021 start_ccb->ccg.heads = 0;
3022 start_ccb->ccg.secs_per_track = 0;
3023 start_ccb->ccb_h.status = CAM_REQ_CMP;
3028 * In a PC-98 system, geometry translation depens on
3029 * the "real" device geometry obtained from mode page 4.
3030 * SCSI geometry translation is performed in the
3031 * initialization routine of the SCSI BIOS and the result
3032 * stored in host memory. If the translation is available
3033 * in host memory, use it. If not, rely on the default
3034 * translation the device driver performs.
3036 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3037 start_ccb->ccb_h.status = CAM_REQ_CMP;
3041 sim = start_ccb->ccb_h.path->bus->sim;
3042 (*(sim->sim_action))(sim, start_ccb);
3047 union ccb* abort_ccb;
3050 abort_ccb = start_ccb->cab.abort_ccb;
3051 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3053 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3054 struct cam_ccbq *ccbq;
3056 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3057 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3058 abort_ccb->ccb_h.status =
3059 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3060 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3062 xpt_done(abort_ccb);
3064 start_ccb->ccb_h.status = CAM_REQ_CMP;
3067 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3068 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3070 * We've caught this ccb en route to
3071 * the SIM. Flag it for abort and the
3072 * SIM will do so just before starting
3073 * real work on the CCB.
3075 abort_ccb->ccb_h.status =
3076 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3077 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3078 start_ccb->ccb_h.status = CAM_REQ_CMP;
3082 if (XPT_FC_IS_QUEUED(abort_ccb)
3083 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3085 * It's already completed but waiting
3086 * for our SWI to get to it.
3088 start_ccb->ccb_h.status = CAM_UA_ABORT;
3092 * If we weren't able to take care of the abort request
3093 * in the XPT, pass the request down to the SIM for processing.
3097 case XPT_ACCEPT_TARGET_IO:
3099 case XPT_IMMED_NOTIFY:
3100 case XPT_NOTIFY_ACK:
3101 case XPT_GET_TRAN_SETTINGS:
3104 struct cam_sim *sim;
3106 sim = start_ccb->ccb_h.path->bus->sim;
3107 (*(sim->sim_action))(sim, start_ccb);
3112 struct cam_sim *sim;
3114 sim = start_ccb->ccb_h.path->bus->sim;
3115 (*(sim->sim_action))(sim, start_ccb);
3118 case XPT_PATH_STATS:
3119 start_ccb->cpis.last_reset =
3120 start_ccb->ccb_h.path->bus->last_reset;
3121 start_ccb->ccb_h.status = CAM_REQ_CMP;
3128 dev = start_ccb->ccb_h.path->device;
3130 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3131 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3133 struct ccb_getdev *cgd;
3137 cgd = &start_ccb->cgd;
3138 bus = cgd->ccb_h.path->bus;
3139 tar = cgd->ccb_h.path->target;
3140 cgd->inq_data = dev->inq_data;
3141 cgd->ccb_h.status = CAM_REQ_CMP;
3142 cgd->serial_num_len = dev->serial_num_len;
3143 if ((dev->serial_num_len > 0)
3144 && (dev->serial_num != NULL))
3145 bcopy(dev->serial_num, cgd->serial_num,
3146 dev->serial_num_len);
3151 case XPT_GDEV_STATS:
3156 dev = start_ccb->ccb_h.path->device;
3158 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3159 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3161 struct ccb_getdevstats *cgds;
3165 cgds = &start_ccb->cgds;
3166 bus = cgds->ccb_h.path->bus;
3167 tar = cgds->ccb_h.path->target;
3168 cgds->dev_openings = dev->ccbq.dev_openings;
3169 cgds->dev_active = dev->ccbq.dev_active;
3170 cgds->devq_openings = dev->ccbq.devq_openings;
3171 cgds->devq_queued = dev->ccbq.queue.entries;
3172 cgds->held = dev->ccbq.held;
3173 cgds->last_reset = tar->last_reset;
3174 cgds->maxtags = dev->quirk->maxtags;
3175 cgds->mintags = dev->quirk->mintags;
3176 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3177 cgds->last_reset = bus->last_reset;
3178 cgds->ccb_h.status = CAM_REQ_CMP;
3185 struct cam_periph *nperiph;
3186 struct periph_list *periph_head;
3187 struct ccb_getdevlist *cgdl;
3190 struct cam_ed *device;
3197 * Don't want anyone mucking with our data.
3200 device = start_ccb->ccb_h.path->device;
3201 periph_head = &device->periphs;
3202 cgdl = &start_ccb->cgdl;
3205 * Check and see if the list has changed since the user
3206 * last requested a list member. If so, tell them that the
3207 * list has changed, and therefore they need to start over
3208 * from the beginning.
3210 if ((cgdl->index != 0) &&
3211 (cgdl->generation != device->generation)) {
3212 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3218 * Traverse the list of peripherals and attempt to find
3219 * the requested peripheral.
3221 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3222 (nperiph != NULL) && (i <= cgdl->index);
3223 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3224 if (i == cgdl->index) {
3225 strncpy(cgdl->periph_name,
3226 nperiph->periph_name,
3228 cgdl->unit_number = nperiph->unit_number;
3233 cgdl->status = CAM_GDEVLIST_ERROR;
3238 if (nperiph == NULL)
3239 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3241 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3244 cgdl->generation = device->generation;
3247 cgdl->ccb_h.status = CAM_REQ_CMP;
3253 dev_pos_type position_type;
3254 struct ccb_dev_match *cdm;
3256 cdm = &start_ccb->cdm;
3259 * Prevent EDT changes while we traverse it.
3263 * There are two ways of getting at information in the EDT.
3264 * The first way is via the primary EDT tree. It starts
3265 * with a list of busses, then a list of targets on a bus,
3266 * then devices/luns on a target, and then peripherals on a
3267 * device/lun. The "other" way is by the peripheral driver
3268 * lists. The peripheral driver lists are organized by
3269 * peripheral driver. (obviously) So it makes sense to
3270 * use the peripheral driver list if the user is looking
3271 * for something like "da1", or all "da" devices. If the
3272 * user is looking for something on a particular bus/target
3273 * or lun, it's generally better to go through the EDT tree.
3276 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3277 position_type = cdm->pos.position_type;
3281 position_type = CAM_DEV_POS_NONE;
3283 for (i = 0; i < cdm->num_patterns; i++) {
3284 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3285 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3286 position_type = CAM_DEV_POS_EDT;
3291 if (cdm->num_patterns == 0)
3292 position_type = CAM_DEV_POS_EDT;
3293 else if (position_type == CAM_DEV_POS_NONE)
3294 position_type = CAM_DEV_POS_PDRV;
3297 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3298 case CAM_DEV_POS_EDT:
3301 case CAM_DEV_POS_PDRV:
3302 xptperiphlistmatch(cdm);
3305 cdm->status = CAM_DEV_MATCH_ERROR;
3311 if (cdm->status == CAM_DEV_MATCH_ERROR)
3312 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3314 start_ccb->ccb_h.status = CAM_REQ_CMP;
3320 struct ccb_setasync *csa;
3321 struct async_node *cur_entry;
3322 struct async_list *async_head;
3326 csa = &start_ccb->csa;
3327 added = csa->event_enable;
3328 async_head = &csa->ccb_h.path->device->asyncs;
3331 * If there is already an entry for us, simply
3335 cur_entry = SLIST_FIRST(async_head);
3336 while (cur_entry != NULL) {
3337 if ((cur_entry->callback_arg == csa->callback_arg)
3338 && (cur_entry->callback == csa->callback))
3340 cur_entry = SLIST_NEXT(cur_entry, links);
3343 if (cur_entry != NULL) {
3345 * If the request has no flags set,
3348 added &= ~cur_entry->event_enable;
3349 if (csa->event_enable == 0) {
3350 SLIST_REMOVE(async_head, cur_entry,
3352 csa->ccb_h.path->device->refcount--;
3353 free(cur_entry, M_CAMXPT);
3355 cur_entry->event_enable = csa->event_enable;
3358 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3360 if (cur_entry == NULL) {
3362 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3365 cur_entry->event_enable = csa->event_enable;
3366 cur_entry->callback_arg = csa->callback_arg;
3367 cur_entry->callback = csa->callback;
3368 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3369 csa->ccb_h.path->device->refcount++;
3372 if ((added & AC_FOUND_DEVICE) != 0) {
3374 * Get this peripheral up to date with all
3375 * the currently existing devices.
3377 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3379 if ((added & AC_PATH_REGISTERED) != 0) {
3381 * Get this peripheral up to date with all
3382 * the currently existing busses.
3384 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3387 start_ccb->ccb_h.status = CAM_REQ_CMP;
3392 struct ccb_relsim *crs;
3396 crs = &start_ccb->crs;
3397 dev = crs->ccb_h.path->device;
3400 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3406 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3408 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
3409 /* Don't ever go below one opening */
3410 if (crs->openings > 0) {
3411 xpt_dev_ccbq_resize(crs->ccb_h.path,
3415 xpt_print(crs->ccb_h.path,
3416 "tagged openings now %d\n",
3423 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3425 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3428 * Just extend the old timeout and decrement
3429 * the freeze count so that a single timeout
3430 * is sufficient for releasing the queue.
3432 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3433 untimeout(xpt_release_devq_timeout,
3434 dev, dev->c_handle);
3437 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3441 timeout(xpt_release_devq_timeout,
3443 (crs->release_timeout * hz) / 1000);
3445 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3449 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3451 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3453 * Decrement the freeze count so that a single
3454 * completion is still sufficient to unfreeze
3457 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3460 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3461 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3465 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3467 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3468 || (dev->ccbq.dev_active == 0)) {
3470 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3473 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3474 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3479 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3481 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3484 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3485 start_ccb->ccb_h.status = CAM_REQ_CMP;
3489 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3492 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3493 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3501 #ifdef CAM_DEBUG_DELAY
3502 cam_debug_delay = CAM_DEBUG_DELAY;
3504 cam_dflags = start_ccb->cdbg.flags;
3505 if (cam_dpath != NULL) {
3506 xpt_free_path(cam_dpath);
3510 if (cam_dflags != CAM_DEBUG_NONE) {
3511 if (xpt_create_path(&cam_dpath, xpt_periph,
3512 start_ccb->ccb_h.path_id,
3513 start_ccb->ccb_h.target_id,
3514 start_ccb->ccb_h.target_lun) !=
3516 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3517 cam_dflags = CAM_DEBUG_NONE;
3519 start_ccb->ccb_h.status = CAM_REQ_CMP;
3520 xpt_print(cam_dpath, "debugging flags now %x\n",
3525 start_ccb->ccb_h.status = CAM_REQ_CMP;
3528 #else /* !CAMDEBUG */
3529 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3530 #endif /* CAMDEBUG */
3534 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3535 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3536 start_ccb->ccb_h.status = CAM_REQ_CMP;
3543 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3550 xpt_polled_action(union ccb *start_ccb)
3554 struct cam_sim *sim;
3555 struct cam_devq *devq;
3560 timeout = start_ccb->ccb_h.timeout;
3561 sim = start_ccb->ccb_h.path->bus->sim;
3563 dev = start_ccb->ccb_h.path->device;
3568 * Steal an opening so that no other queued requests
3569 * can get it before us while we simulate interrupts.
3571 dev->ccbq.devq_openings--;
3572 dev->ccbq.dev_openings--;
3574 while(((devq != NULL && devq->send_openings <= 0) ||
3575 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3577 (*(sim->sim_poll))(sim);
3581 dev->ccbq.devq_openings++;
3582 dev->ccbq.dev_openings++;
3585 xpt_action(start_ccb);
3586 while(--timeout > 0) {
3587 (*(sim->sim_poll))(sim);
3589 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3596 * XXX Is it worth adding a sim_timeout entry
3597 * point so we can attempt recovery? If
3598 * this is only used for dumps, I don't think
3601 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3604 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3610 * Schedule a peripheral driver to receive a ccb when it's
3611 * target device has space for more transactions.
3614 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3616 struct cam_ed *device;
3617 union ccb *work_ccb;
3623 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3624 device = perph->path->device;
3626 if (periph_is_queued(perph)) {
3627 /* Simply reorder based on new priority */
3628 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3629 (" change priority to %d\n", new_priority));
3630 if (new_priority < perph->pinfo.priority) {
3631 camq_change_priority(&device->drvq,
3636 } else if (SIM_DEAD(perph->path->bus->sim)) {
3637 /* The SIM is gone so just call periph_start directly. */
3638 work_ccb = xpt_get_ccb(perph->path->device);
3640 if (work_ccb == NULL)
3642 xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority);
3643 perph->pinfo.priority = new_priority;
3644 perph->periph_start(perph, work_ccb);
3647 /* New entry on the queue */
3648 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3649 (" added periph to queue\n"));
3650 perph->pinfo.priority = new_priority;
3651 perph->pinfo.generation = ++device->drvq.generation;
3652 camq_insert(&device->drvq, &perph->pinfo);
3653 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3657 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3658 (" calling xpt_run_devq\n"));
3659 xpt_run_dev_allocq(perph->path->bus);
3665 * Schedule a device to run on a given queue.
3666 * If the device was inserted as a new entry on the queue,
3667 * return 1 meaning the device queue should be run. If we
3668 * were already queued, implying someone else has already
3669 * started the queue, return 0 so the caller doesn't attempt
3670 * to run the queue. Must be run at either splsoftcam
3671 * (or splcam since that encompases splsoftcam).
3674 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3675 u_int32_t new_priority)
3678 u_int32_t old_priority;
3680 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3682 old_priority = pinfo->priority;
3685 * Are we already queued?
3687 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3688 /* Simply reorder based on new priority */
3689 if (new_priority < old_priority) {
3690 camq_change_priority(queue, pinfo->index,
3692 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3693 ("changed priority to %d\n",
3698 /* New entry on the queue */
3699 if (new_priority < old_priority)
3700 pinfo->priority = new_priority;
3702 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3703 ("Inserting onto queue\n"));
3704 pinfo->generation = ++queue->generation;
3705 camq_insert(queue, pinfo);
3712 xpt_run_dev_allocq(struct cam_eb *bus)
3714 struct cam_devq *devq;
3717 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3718 devq = bus->sim->devq;
3720 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3721 (" qfrozen_cnt == 0x%x, entries == %d, "
3722 "openings == %d, active == %d\n",
3723 devq->alloc_queue.qfrozen_cnt,
3724 devq->alloc_queue.entries,
3725 devq->alloc_openings,
3726 devq->alloc_active));
3729 devq->alloc_queue.qfrozen_cnt++;
3730 while ((devq->alloc_queue.entries > 0)
3731 && (devq->alloc_openings > 0)
3732 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3733 struct cam_ed_qinfo *qinfo;
3734 struct cam_ed *device;
3735 union ccb *work_ccb;
3736 struct cam_periph *drv;
3739 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3741 device = qinfo->device;
3743 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3744 ("running device %p\n", device));
3746 drvq = &device->drvq;
3749 if (drvq->entries <= 0) {
3750 panic("xpt_run_dev_allocq: "
3751 "Device on queue without any work to do");
3754 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3755 devq->alloc_openings--;
3756 devq->alloc_active++;
3757 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3759 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3760 drv->pinfo.priority);
3761 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3762 ("calling periph start\n"));
3763 drv->periph_start(drv, work_ccb);
3766 * Malloc failure in alloc_ccb
3769 * XXX add us to a list to be run from free_ccb
3770 * if we don't have any ccbs active on this
3771 * device queue otherwise we may never get run
3777 /* Raise IPL for possible insertion and test at top of loop */
3780 if (drvq->entries > 0) {
3781 /* We have more work. Attempt to reschedule */
3782 xpt_schedule_dev_allocq(bus, device);
3785 devq->alloc_queue.qfrozen_cnt--;
3790 xpt_run_dev_sendq(struct cam_eb *bus)
3792 struct cam_devq *devq;
3795 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3797 devq = bus->sim->devq;
3800 devq->send_queue.qfrozen_cnt++;
3803 while ((devq->send_queue.entries > 0)
3804 && (devq->send_openings > 0)) {
3805 struct cam_ed_qinfo *qinfo;
3806 struct cam_ed *device;
3807 union ccb *work_ccb;
3808 struct cam_sim *sim;
3812 if (devq->send_queue.qfrozen_cnt > 1) {
3817 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3819 device = qinfo->device;
3822 * If the device has been "frozen", don't attempt
3825 if (device->qfrozen_cnt > 0) {
3830 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3831 ("running device %p\n", device));
3833 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3834 if (work_ccb == NULL) {
3835 printf("device on run queue with no ccbs???\n");
3840 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3842 if (num_highpower <= 0) {
3844 * We got a high power command, but we
3845 * don't have any available slots. Freeze
3846 * the device queue until we have a slot
3849 device->qfrozen_cnt++;
3850 STAILQ_INSERT_TAIL(&highpowerq,
3858 * Consume a high power slot while
3864 devq->active_dev = device;
3865 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3867 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3870 devq->send_openings--;
3871 devq->send_active++;
3873 if (device->ccbq.queue.entries > 0)
3874 xpt_schedule_dev_sendq(bus, device);
3876 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3878 * The client wants to freeze the queue
3879 * after this CCB is sent.
3882 device->qfrozen_cnt++;
3888 /* In Target mode, the peripheral driver knows best... */
3889 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3890 if ((device->inq_flags & SID_CmdQue) != 0
3891 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3892 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3895 * Clear this in case of a retried CCB that
3896 * failed due to a rejected tag.
3898 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3902 * Device queues can be shared among multiple sim instances
3903 * that reside on different busses. Use the SIM in the queue
3904 * CCB's path, rather than the one in the bus that was passed
3905 * into this function.
3907 sim = work_ccb->ccb_h.path->bus->sim;
3908 (*(sim->sim_action))(sim, work_ccb);
3911 devq->active_dev = NULL;
3913 /* Raise IPL for possible insertion and test at top of loop */
3918 devq->send_queue.qfrozen_cnt--;
3923 * This function merges stuff from the slave ccb into the master ccb, while
3924 * keeping important fields in the master ccb constant.
3927 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3932 * Pull fields that are valid for peripheral drivers to set
3933 * into the master CCB along with the CCB "payload".
3935 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3936 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3937 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3938 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3939 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3940 sizeof(union ccb) - sizeof(struct ccb_hdr));
3944 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3948 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3949 ccb_h->pinfo.priority = priority;
3951 ccb_h->path_id = path->bus->path_id;
3953 ccb_h->target_id = path->target->target_id;
3955 ccb_h->target_id = CAM_TARGET_WILDCARD;
3957 ccb_h->target_lun = path->device->lun_id;
3958 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3960 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3962 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3966 /* Path manipulation functions */
3968 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3969 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3971 struct cam_path *path;
3976 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3979 status = CAM_RESRC_UNAVAIL;
3982 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3983 if (status != CAM_REQ_CMP) {
3984 free(path, M_CAMXPT);
3987 *new_path_ptr = path;
3992 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3993 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3996 struct cam_et *target;
3997 struct cam_ed *device;
4001 status = CAM_REQ_CMP; /* Completed without error */
4002 target = NULL; /* Wildcarded */
4003 device = NULL; /* Wildcarded */
4006 * We will potentially modify the EDT, so block interrupts
4007 * that may attempt to create cam paths.
4010 bus = xpt_find_bus(path_id);
4012 status = CAM_PATH_INVALID;
4014 target = xpt_find_target(bus, target_id);
4015 if (target == NULL) {
4017 struct cam_et *new_target;
4019 new_target = xpt_alloc_target(bus, target_id);
4020 if (new_target == NULL) {
4021 status = CAM_RESRC_UNAVAIL;
4023 target = new_target;
4026 if (target != NULL) {
4027 device = xpt_find_device(target, lun_id);
4028 if (device == NULL) {
4030 struct cam_ed *new_device;
4032 new_device = xpt_alloc_device(bus,
4035 if (new_device == NULL) {
4036 status = CAM_RESRC_UNAVAIL;
4038 device = new_device;
4046 * Only touch the user's data if we are successful.
4048 if (status == CAM_REQ_CMP) {
4049 new_path->periph = perph;
4050 new_path->bus = bus;
4051 new_path->target = target;
4052 new_path->device = device;
4053 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4056 xpt_release_device(bus, target, device);
4058 xpt_release_target(bus, target);
4060 xpt_release_bus(bus);
4066 xpt_release_path(struct cam_path *path)
4068 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4069 if (path->device != NULL) {
4070 xpt_release_device(path->bus, path->target, path->device);
4071 path->device = NULL;
4073 if (path->target != NULL) {
4074 xpt_release_target(path->bus, path->target);
4075 path->target = NULL;
4077 if (path->bus != NULL) {
4078 xpt_release_bus(path->bus);
4084 xpt_free_path(struct cam_path *path)
4088 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4089 xpt_release_path(path);
4090 free(path, M_CAMXPT);
4095 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4096 * in path1, 2 for match with wildcards in path2.
4099 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4105 if (path1->bus != path2->bus) {
4106 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4108 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4113 if (path1->target != path2->target) {
4114 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4117 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4122 if (path1->device != path2->device) {
4123 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4126 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4135 xpt_print_path(struct cam_path *path)
4140 printf("(nopath): ");
4142 if (path->periph != NULL)
4143 printf("(%s%d:", path->periph->periph_name,
4144 path->periph->unit_number);
4146 printf("(noperiph:");
4148 if (path->bus != NULL)
4149 printf("%s%d:%d:", path->bus->sim->sim_name,
4150 path->bus->sim->unit_number,
4151 path->bus->sim->bus_id);
4155 if (path->target != NULL)
4156 printf("%d:", path->target->target_id);
4160 if (path->device != NULL)
4161 printf("%d): ", path->device->lun_id);
4168 xpt_print(struct cam_path *path, const char *fmt, ...)
4171 xpt_print_path(path);
4178 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4184 sbuf_new(&sb, str, str_len, 0);
4187 sbuf_printf(&sb, "(nopath): ");
4189 if (path->periph != NULL)
4190 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4191 path->periph->unit_number);
4193 sbuf_printf(&sb, "(noperiph:");
4195 if (path->bus != NULL)
4196 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4197 path->bus->sim->unit_number,
4198 path->bus->sim->bus_id);
4200 sbuf_printf(&sb, "nobus:");
4202 if (path->target != NULL)
4203 sbuf_printf(&sb, "%d:", path->target->target_id);
4205 sbuf_printf(&sb, "X:");
4207 if (path->device != NULL)
4208 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4210 sbuf_printf(&sb, "X): ");
4214 return(sbuf_len(&sb));
4218 xpt_path_path_id(struct cam_path *path)
4222 return(path->bus->path_id);
4226 xpt_path_target_id(struct cam_path *path)
4230 if (path->target != NULL)
4231 return (path->target->target_id);
4233 return (CAM_TARGET_WILDCARD);
4237 xpt_path_lun_id(struct cam_path *path)
4241 if (path->device != NULL)
4242 return (path->device->lun_id);
4244 return (CAM_LUN_WILDCARD);
4248 xpt_path_sim(struct cam_path *path)
4252 return (path->bus->sim);
4256 xpt_path_periph(struct cam_path *path)
4260 return (path->periph);
4264 * Release a CAM control block for the caller. Remit the cost of the structure
4265 * to the device referenced by the path. If the this device had no 'credits'
4266 * and peripheral drivers have registered async callbacks for this notification
4270 xpt_release_ccb(union ccb *free_ccb)
4273 struct cam_path *path;
4274 struct cam_ed *device;
4279 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4280 path = free_ccb->ccb_h.path;
4281 device = path->device;
4284 cam_ccbq_release_opening(&device->ccbq);
4285 if (xpt_ccb_count > xpt_max_ccbs) {
4286 xpt_free_ccb(free_ccb);
4289 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4291 if (bus->sim->devq == NULL) {
4295 bus->sim->devq->alloc_openings++;
4296 bus->sim->devq->alloc_active--;
4297 /* XXX Turn this into an inline function - xpt_run_device?? */
4298 if ((device_is_alloc_queued(device) == 0)
4299 && (device->drvq.entries > 0)) {
4300 xpt_schedule_dev_allocq(bus, device);
4303 if (dev_allocq_is_runnable(bus->sim->devq))
4304 xpt_run_dev_allocq(bus);
4307 /* Functions accessed by SIM drivers */
4310 * A sim structure, listing the SIM entry points and instance
4311 * identification info is passed to xpt_bus_register to hook the SIM
4312 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4313 * for this new bus and places it in the array of busses and assigns
4314 * it a path_id. The path_id may be influenced by "hard wiring"
4315 * information specified by the user. Once interrupt services are
4316 * availible, the bus will be probed.
4319 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4321 struct cam_eb *new_bus;
4322 struct cam_eb *old_bus;
4323 struct ccb_pathinq cpi;
4329 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4330 M_CAMXPT, M_NOWAIT);
4331 if (new_bus == NULL) {
4332 /* Couldn't satisfy request */
4333 return (CAM_RESRC_UNAVAIL);
4336 if (strcmp(sim->sim_name, "xpt") != 0) {
4339 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4342 TAILQ_INIT(&new_bus->et_entries);
4343 new_bus->path_id = sim->path_id;
4345 timevalclear(&new_bus->last_reset);
4347 new_bus->refcount = 1; /* Held until a bus_deregister event */
4348 new_bus->generation = 0;
4350 old_bus = TAILQ_FIRST(&xpt_busses);
4351 while (old_bus != NULL
4352 && old_bus->path_id < new_bus->path_id)
4353 old_bus = TAILQ_NEXT(old_bus, links);
4354 if (old_bus != NULL)
4355 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4357 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4361 /* Notify interested parties */
4362 if (sim->path_id != CAM_XPT_PATH_ID) {
4363 struct cam_path path;
4365 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4366 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4367 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4368 cpi.ccb_h.func_code = XPT_PATH_INQ;
4369 xpt_action((union ccb *)&cpi);
4370 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4371 xpt_release_path(&path);
4373 return (CAM_SUCCESS);
4377 xpt_bus_deregister(path_id_t pathid)
4379 struct cam_path bus_path;
4380 struct cam_ed *device;
4381 struct cam_ed_qinfo *qinfo;
4382 struct cam_devq *devq;
4383 struct cam_periph *periph;
4384 struct cam_sim *ccbsim;
4385 union ccb *work_ccb;
4390 status = xpt_compile_path(&bus_path, NULL, pathid,
4391 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4392 if (status != CAM_REQ_CMP)
4395 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4396 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4398 /* The SIM may be gone, so use a dummy SIM for any stray operations. */
4399 devq = bus_path.bus->sim->devq;
4400 bus_path.bus->sim = &cam_dead_sim;
4402 /* Execute any pending operations now. */
4403 while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
4404 CAMQ_HEAD)) != NULL ||
4405 (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
4406 CAMQ_HEAD)) != NULL) {
4408 device = qinfo->device;
4409 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
4410 if (work_ccb != NULL) {
4411 devq->active_dev = device;
4412 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
4413 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
4414 ccbsim = work_ccb->ccb_h.path->bus->sim;
4415 (*(ccbsim->sim_action))(ccbsim, work_ccb);
4418 periph = (struct cam_periph *)camq_remove(&device->drvq,
4421 xpt_schedule(periph, periph->pinfo.priority);
4422 } while (work_ccb != NULL || periph != NULL);
4425 /* Make sure all completed CCBs are processed. */
4426 while (!TAILQ_EMPTY(&cam_bioq)) {
4429 /* Repeat the async's for the benefit of any new devices. */
4430 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4431 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4434 /* Release the reference count held while registered. */
4435 xpt_release_bus(bus_path.bus);
4436 xpt_release_path(&bus_path);
4438 /* Recheck for more completed CCBs. */
4439 while (!TAILQ_EMPTY(&cam_bioq))
4442 return (CAM_REQ_CMP);
4446 xptnextfreepathid(void)
4453 bus = TAILQ_FIRST(&xpt_busses);
4455 /* Find an unoccupied pathid */
4457 && bus->path_id <= pathid) {
4458 if (bus->path_id == pathid)
4460 bus = TAILQ_NEXT(bus, links);
4464 * Ensure that this pathid is not reserved for
4465 * a bus that may be registered in the future.
4467 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4469 /* Start the search over */
4476 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4483 pathid = CAM_XPT_PATH_ID;
4484 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4486 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4487 if (strcmp(dname, "scbus")) {
4488 /* Avoid a bit of foot shooting. */
4491 if (dunit < 0) /* unwired?! */
4493 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4494 if (sim_bus == val) {
4498 } else if (sim_bus == 0) {
4499 /* Unspecified matches bus 0 */
4503 printf("Ambiguous scbus configuration for %s%d "
4504 "bus %d, cannot wire down. The kernel "
4505 "config entry for scbus%d should "
4506 "specify a controller bus.\n"
4507 "Scbus will be assigned dynamically.\n",
4508 sim_name, sim_unit, sim_bus, dunit);
4513 if (pathid == CAM_XPT_PATH_ID)
4514 pathid = xptnextfreepathid();
4519 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4522 struct cam_et *target, *next_target;
4523 struct cam_ed *device, *next_device;
4528 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4531 * Most async events come from a CAM interrupt context. In
4532 * a few cases, the error recovery code at the peripheral layer,
4533 * which may run from our SWI or a process context, may signal
4534 * deferred events with a call to xpt_async. Ensure async
4535 * notifications are serialized by blocking cam interrupts.
4541 if (async_code == AC_BUS_RESET) {
4545 /* Update our notion of when the last reset occurred */
4546 microtime(&bus->last_reset);
4550 for (target = TAILQ_FIRST(&bus->et_entries);
4552 target = next_target) {
4554 next_target = TAILQ_NEXT(target, links);
4556 if (path->target != target
4557 && path->target->target_id != CAM_TARGET_WILDCARD
4558 && target->target_id != CAM_TARGET_WILDCARD)
4561 if (async_code == AC_SENT_BDR) {
4564 /* Update our notion of when the last reset occurred */
4566 microtime(&path->target->last_reset);
4570 for (device = TAILQ_FIRST(&target->ed_entries);
4572 device = next_device) {
4574 next_device = TAILQ_NEXT(device, links);
4576 if (path->device != device
4577 && path->device->lun_id != CAM_LUN_WILDCARD
4578 && device->lun_id != CAM_LUN_WILDCARD)
4581 xpt_dev_async(async_code, bus, target,
4584 xpt_async_bcast(&device->asyncs, async_code,
4590 * If this wasn't a fully wildcarded async, tell all
4591 * clients that want all async events.
4593 if (bus != xpt_periph->path->bus)
4594 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4600 xpt_async_bcast(struct async_list *async_head,
4601 u_int32_t async_code,
4602 struct cam_path *path, void *async_arg)
4604 struct async_node *cur_entry;
4606 cur_entry = SLIST_FIRST(async_head);
4607 while (cur_entry != NULL) {
4608 struct async_node *next_entry;
4610 * Grab the next list entry before we call the current
4611 * entry's callback. This is because the callback function
4612 * can delete its async callback entry.
4614 next_entry = SLIST_NEXT(cur_entry, links);
4615 if ((cur_entry->event_enable & async_code) != 0)
4616 cur_entry->callback(cur_entry->callback_arg,
4619 cur_entry = next_entry;
4624 * Handle any per-device event notifications that require action by the XPT.
4627 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4628 struct cam_ed *device, void *async_arg)
4631 struct cam_path newpath;
4634 * We only need to handle events for real devices.
4636 if (target->target_id == CAM_TARGET_WILDCARD
4637 || device->lun_id == CAM_LUN_WILDCARD)
4641 * We need our own path with wildcards expanded to
4642 * handle certain types of events.
4644 if ((async_code == AC_SENT_BDR)
4645 || (async_code == AC_BUS_RESET)
4646 || (async_code == AC_INQ_CHANGED))
4647 status = xpt_compile_path(&newpath, NULL,
4652 status = CAM_REQ_CMP_ERR;
4654 if (status == CAM_REQ_CMP) {
4657 * Allow transfer negotiation to occur in a
4658 * tag free environment.
4660 if (async_code == AC_SENT_BDR
4661 || async_code == AC_BUS_RESET)
4662 xpt_toggle_tags(&newpath);
4664 if (async_code == AC_INQ_CHANGED) {
4666 * We've sent a start unit command, or
4667 * something similar to a device that
4668 * may have caused its inquiry data to
4669 * change. So we re-scan the device to
4670 * refresh the inquiry data for it.
4672 xpt_scan_lun(newpath.periph, &newpath,
4673 CAM_EXPECT_INQ_CHANGE, NULL);
4675 xpt_release_path(&newpath);
4676 } else if (async_code == AC_LOST_DEVICE) {
4677 device->flags |= CAM_DEV_UNCONFIGURED;
4678 } else if (async_code == AC_TRANSFER_NEG) {
4679 struct ccb_trans_settings *settings;
4681 settings = (struct ccb_trans_settings *)async_arg;
4682 xpt_set_transfer_settings(settings, device,
4683 /*async_update*/TRUE);
4688 xpt_freeze_devq(struct cam_path *path, u_int count)
4691 struct ccb_hdr *ccbh;
4696 path->device->qfrozen_cnt += count;
4699 * Mark the last CCB in the queue as needing
4700 * to be requeued if the driver hasn't
4701 * changed it's state yet. This fixes a race
4702 * where a ccb is just about to be queued to
4703 * a controller driver when it's interrupt routine
4704 * freezes the queue. To completly close the
4705 * hole, controller drives must check to see
4706 * if a ccb's status is still CAM_REQ_INPROG
4707 * under spl protection just before they queue
4708 * the CCB. See ahc_action/ahc_freeze_devq for
4711 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4712 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4713 ccbh->status = CAM_REQUEUE_REQ;
4715 return (path->device->qfrozen_cnt);
4719 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4723 sim->devq->send_queue.qfrozen_cnt += count;
4724 if (sim->devq->active_dev != NULL) {
4725 struct ccb_hdr *ccbh;
4727 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4729 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4730 ccbh->status = CAM_REQUEUE_REQ;
4732 return (sim->devq->send_queue.qfrozen_cnt);
4736 xpt_release_devq_timeout(void *arg)
4738 struct cam_ed *device;
4740 device = (struct cam_ed *)arg;
4742 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4746 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4750 xpt_release_devq_device(path->device, count, run_queue);
4754 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4762 if (dev->qfrozen_cnt > 0) {
4764 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4765 dev->qfrozen_cnt -= count;
4766 if (dev->qfrozen_cnt == 0) {
4769 * No longer need to wait for a successful
4770 * command completion.
4772 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4775 * Remove any timeouts that might be scheduled
4776 * to release this queue.
4778 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4779 untimeout(xpt_release_devq_timeout, dev,
4781 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4785 * Now that we are unfrozen schedule the
4786 * device so any pending transactions are
4789 if ((dev->ccbq.queue.entries > 0)
4790 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4791 && (run_queue != 0)) {
4798 xpt_run_dev_sendq(dev->target->bus);
4803 xpt_release_simq(struct cam_sim *sim, int run_queue)
4810 sendq = &(sim->devq->send_queue);
4812 if (sendq->qfrozen_cnt > 0) {
4814 sendq->qfrozen_cnt--;
4815 if (sendq->qfrozen_cnt == 0) {
4819 * If there is a timeout scheduled to release this
4820 * sim queue, remove it. The queue frozen count is
4823 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4824 untimeout(xpt_release_simq_timeout, sim,
4826 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4828 bus = xpt_find_bus(sim->path_id);
4833 * Now that we are unfrozen run the send queue.
4835 xpt_run_dev_sendq(bus);
4837 xpt_release_bus(bus);
4845 xpt_release_simq_timeout(void *arg)
4847 struct cam_sim *sim;
4849 sim = (struct cam_sim *)arg;
4850 xpt_release_simq(sim, /* run_queue */ TRUE);
4854 xpt_done(union ccb *done_ccb)
4860 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4861 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4863 * Queue up the request for handling by our SWI handler
4864 * any of the "non-immediate" type of ccbs.
4866 switch (done_ccb->ccb_h.path->periph->type) {
4867 case CAM_PERIPH_BIO:
4868 mtx_lock(&cam_bioq_lock);
4869 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4871 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4872 mtx_unlock(&cam_bioq_lock);
4873 swi_sched(cambio_ih, 0);
4876 panic("unknown periph type %d",
4877 done_ccb->ccb_h.path->periph->type);
4890 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4895 xpt_alloc_ccb_nowait()
4901 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
4906 xpt_free_ccb(union ccb *free_ccb)
4908 free(free_ccb, M_CAMXPT);
4913 /* Private XPT functions */
4916 * Get a CAM control block for the caller. Charge the structure to the device
4917 * referenced by the path. If the this device has no 'credits' then the
4918 * device already has the maximum number of outstanding operations under way
4919 * and we return NULL. If we don't have sufficient resources to allocate more
4920 * ccbs, we also return NULL.
4923 xpt_get_ccb(struct cam_ed *device)
4929 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4930 new_ccb = xpt_alloc_ccb_nowait();
4931 if (new_ccb == NULL) {
4935 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4936 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4940 cam_ccbq_take_opening(&device->ccbq);
4941 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4947 xpt_release_bus(struct cam_eb *bus)
4952 if ((--bus->refcount == 0)
4953 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4954 TAILQ_REMOVE(&xpt_busses, bus, links);
4957 free(bus, M_CAMXPT);
4962 static struct cam_et *
4963 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4965 struct cam_et *target;
4967 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4968 if (target != NULL) {
4969 struct cam_et *cur_target;
4971 TAILQ_INIT(&target->ed_entries);
4973 target->target_id = target_id;
4974 target->refcount = 1;
4975 target->generation = 0;
4976 timevalclear(&target->last_reset);
4978 * Hold a reference to our parent bus so it
4979 * will not go away before we do.
4983 /* Insertion sort into our bus's target list */
4984 cur_target = TAILQ_FIRST(&bus->et_entries);
4985 while (cur_target != NULL && cur_target->target_id < target_id)
4986 cur_target = TAILQ_NEXT(cur_target, links);
4988 if (cur_target != NULL) {
4989 TAILQ_INSERT_BEFORE(cur_target, target, links);
4991 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4999 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
5004 if ((--target->refcount == 0)
5005 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
5006 TAILQ_REMOVE(&bus->et_entries, target, links);
5009 free(target, M_CAMXPT);
5010 xpt_release_bus(bus);
5015 static struct cam_ed *
5016 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5018 struct cam_path path;
5019 struct cam_ed *device;
5020 struct cam_devq *devq;
5023 if (SIM_DEAD(bus->sim))
5026 /* Make space for us in the device queue on our bus */
5027 devq = bus->sim->devq;
5028 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5030 if (status != CAM_REQ_CMP) {
5033 device = (struct cam_ed *)malloc(sizeof(*device),
5034 M_CAMXPT, M_NOWAIT);
5037 if (device != NULL) {
5038 struct cam_ed *cur_device;
5040 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5041 device->alloc_ccb_entry.device = device;
5042 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5043 device->send_ccb_entry.device = device;
5044 device->target = target;
5045 device->lun_id = lun_id;
5046 /* Initialize our queues */
5047 if (camq_init(&device->drvq, 0) != 0) {
5048 free(device, M_CAMXPT);
5051 if (cam_ccbq_init(&device->ccbq,
5052 bus->sim->max_dev_openings) != 0) {
5053 camq_fini(&device->drvq);
5054 free(device, M_CAMXPT);
5057 SLIST_INIT(&device->asyncs);
5058 SLIST_INIT(&device->periphs);
5059 device->generation = 0;
5060 device->owner = NULL;
5062 * Take the default quirk entry until we have inquiry
5063 * data and can determine a better quirk to use.
5065 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5066 bzero(&device->inq_data, sizeof(device->inq_data));
5067 device->inq_flags = 0;
5068 device->queue_flags = 0;
5069 device->serial_num = NULL;
5070 device->serial_num_len = 0;
5071 device->qfrozen_cnt = 0;
5072 device->flags = CAM_DEV_UNCONFIGURED;
5073 device->tag_delay_count = 0;
5074 device->tag_saved_openings = 0;
5075 device->refcount = 1;
5076 callout_handle_init(&device->c_handle);
5079 * Hold a reference to our parent target so it
5080 * will not go away before we do.
5085 * XXX should be limited by number of CCBs this bus can
5088 xpt_max_ccbs += device->ccbq.devq_openings;
5089 /* Insertion sort into our target's device list */
5090 cur_device = TAILQ_FIRST(&target->ed_entries);
5091 while (cur_device != NULL && cur_device->lun_id < lun_id)
5092 cur_device = TAILQ_NEXT(cur_device, links);
5093 if (cur_device != NULL) {
5094 TAILQ_INSERT_BEFORE(cur_device, device, links);
5096 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5098 target->generation++;
5099 if (lun_id != CAM_LUN_WILDCARD) {
5100 xpt_compile_path(&path,
5105 xpt_devise_transport(&path);
5106 xpt_release_path(&path);
5113 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5114 struct cam_ed *device)
5119 if ((--device->refcount == 0)
5120 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5121 struct cam_devq *devq;
5123 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5124 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5125 panic("Removing device while still queued for ccbs");
5127 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5128 untimeout(xpt_release_devq_timeout, device,
5131 TAILQ_REMOVE(&target->ed_entries, device,links);
5132 target->generation++;
5133 xpt_max_ccbs -= device->ccbq.devq_openings;
5134 if (!SIM_DEAD(bus->sim)) {
5135 /* Release our slot in the devq */
5136 devq = bus->sim->devq;
5137 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5140 camq_fini(&device->drvq);
5141 camq_fini(&device->ccbq.queue);
5142 free(device, M_CAMXPT);
5143 xpt_release_target(bus, target);
5149 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5159 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5160 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5161 if (result == CAM_REQ_CMP && (diff < 0)) {
5162 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5164 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5165 || (dev->inq_flags & SID_CmdQue) != 0)
5166 dev->tag_saved_openings = newopenings;
5167 /* Adjust the global limit */
5168 xpt_max_ccbs += diff;
5173 static struct cam_eb *
5174 xpt_find_bus(path_id_t path_id)
5178 for (bus = TAILQ_FIRST(&xpt_busses);
5180 bus = TAILQ_NEXT(bus, links)) {
5181 if (bus->path_id == path_id) {
5189 static struct cam_et *
5190 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5192 struct cam_et *target;
5194 for (target = TAILQ_FIRST(&bus->et_entries);
5196 target = TAILQ_NEXT(target, links)) {
5197 if (target->target_id == target_id) {
5205 static struct cam_ed *
5206 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5208 struct cam_ed *device;
5210 for (device = TAILQ_FIRST(&target->ed_entries);
5212 device = TAILQ_NEXT(device, links)) {
5213 if (device->lun_id == lun_id) {
5222 union ccb *request_ccb;
5223 struct ccb_pathinq *cpi;
5225 } xpt_scan_bus_info;
5228 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5229 * As the scan progresses, xpt_scan_bus is used as the
5230 * callback on completion function.
5233 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5235 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5236 ("xpt_scan_bus\n"));
5237 switch (request_ccb->ccb_h.func_code) {
5240 xpt_scan_bus_info *scan_info;
5241 union ccb *work_ccb;
5242 struct cam_path *path;
5247 /* Find out the characteristics of the bus */
5248 work_ccb = xpt_alloc_ccb();
5249 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5250 request_ccb->ccb_h.pinfo.priority);
5251 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5252 xpt_action(work_ccb);
5253 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5254 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5255 xpt_free_ccb(work_ccb);
5256 xpt_done(request_ccb);
5260 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5262 * Can't scan the bus on an adapter that
5263 * cannot perform the initiator role.
5265 request_ccb->ccb_h.status = CAM_REQ_CMP;
5266 xpt_free_ccb(work_ccb);
5267 xpt_done(request_ccb);
5271 /* Save some state for use while we probe for devices */
5272 scan_info = (xpt_scan_bus_info *)
5273 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5274 scan_info->request_ccb = request_ccb;
5275 scan_info->cpi = &work_ccb->cpi;
5277 /* Cache on our stack so we can work asynchronously */
5278 max_target = scan_info->cpi->max_target;
5279 initiator_id = scan_info->cpi->initiator_id;
5283 * We can scan all targets in parallel, or do it sequentially.
5285 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5287 scan_info->counter = 0;
5289 scan_info->counter = scan_info->cpi->max_target + 1;
5290 if (scan_info->cpi->initiator_id < scan_info->counter) {
5291 scan_info->counter--;
5295 for (i = 0; i <= max_target; i++) {
5297 if (i == initiator_id)
5300 status = xpt_create_path(&path, xpt_periph,
5301 request_ccb->ccb_h.path_id,
5303 if (status != CAM_REQ_CMP) {
5304 printf("xpt_scan_bus: xpt_create_path failed"
5305 " with status %#x, bus scan halted\n",
5307 free(scan_info, M_TEMP);
5308 request_ccb->ccb_h.status = status;
5309 xpt_free_ccb(work_ccb);
5310 xpt_done(request_ccb);
5313 work_ccb = xpt_alloc_ccb();
5314 xpt_setup_ccb(&work_ccb->ccb_h, path,
5315 request_ccb->ccb_h.pinfo.priority);
5316 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5317 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5318 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5319 work_ccb->crcn.flags = request_ccb->crcn.flags;
5320 xpt_action(work_ccb);
5327 struct cam_path *path;
5328 xpt_scan_bus_info *scan_info;
5330 target_id_t target_id;
5333 /* Reuse the same CCB to query if a device was really found */
5334 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5335 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5336 request_ccb->ccb_h.pinfo.priority);
5337 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5339 path_id = request_ccb->ccb_h.path_id;
5340 target_id = request_ccb->ccb_h.target_id;
5341 lun_id = request_ccb->ccb_h.target_lun;
5342 xpt_action(request_ccb);
5344 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5345 struct cam_ed *device;
5346 struct cam_et *target;
5350 * If we already probed lun 0 successfully, or
5351 * we have additional configured luns on this
5352 * target that might have "gone away", go onto
5355 target = request_ccb->ccb_h.path->target;
5357 * We may touch devices that we don't
5358 * hold references too, so ensure they
5359 * don't disappear out from under us.
5360 * The target above is referenced by the
5361 * path in the request ccb.
5365 device = TAILQ_FIRST(&target->ed_entries);
5366 if (device != NULL) {
5367 phl = CAN_SRCH_HI_SPARSE(device);
5368 if (device->lun_id == 0)
5369 device = TAILQ_NEXT(device, links);
5372 if ((lun_id != 0) || (device != NULL)) {
5373 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5377 struct cam_ed *device;
5379 device = request_ccb->ccb_h.path->device;
5381 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5382 /* Try the next lun */
5383 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5384 || CAN_SRCH_HI_DENSE(device))
5390 * Free the current request path- we're done with it.
5392 xpt_free_path(request_ccb->ccb_h.path);
5395 * Check to see if we scan any further luns.
5397 if (lun_id == request_ccb->ccb_h.target_lun
5398 || lun_id > scan_info->cpi->max_lun) {
5403 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5404 scan_info->counter++;
5405 if (scan_info->counter ==
5406 scan_info->cpi->initiator_id) {
5407 scan_info->counter++;
5409 if (scan_info->counter >=
5410 scan_info->cpi->max_target+1) {
5414 scan_info->counter--;
5415 if (scan_info->counter == 0) {
5420 xpt_free_ccb(request_ccb);
5421 xpt_free_ccb((union ccb *)scan_info->cpi);
5422 request_ccb = scan_info->request_ccb;
5423 free(scan_info, M_TEMP);
5424 request_ccb->ccb_h.status = CAM_REQ_CMP;
5425 xpt_done(request_ccb);
5429 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5432 status = xpt_create_path(&path, xpt_periph,
5433 scan_info->request_ccb->ccb_h.path_id,
5434 scan_info->counter, 0);
5435 if (status != CAM_REQ_CMP) {
5436 printf("xpt_scan_bus: xpt_create_path failed"
5437 " with status %#x, bus scan halted\n",
5439 xpt_free_ccb(request_ccb);
5440 xpt_free_ccb((union ccb *)scan_info->cpi);
5441 request_ccb = scan_info->request_ccb;
5442 free(scan_info, M_TEMP);
5443 request_ccb->ccb_h.status = status;
5444 xpt_done(request_ccb);
5447 xpt_setup_ccb(&request_ccb->ccb_h, path,
5448 request_ccb->ccb_h.pinfo.priority);
5449 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5450 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5451 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5452 request_ccb->crcn.flags =
5453 scan_info->request_ccb->crcn.flags;
5455 status = xpt_create_path(&path, xpt_periph,
5456 path_id, target_id, lun_id);
5457 if (status != CAM_REQ_CMP) {
5458 printf("xpt_scan_bus: xpt_create_path failed "
5459 "with status %#x, halting LUN scan\n",
5463 xpt_setup_ccb(&request_ccb->ccb_h, path,
5464 request_ccb->ccb_h.pinfo.priority);
5465 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5466 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5467 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5468 request_ccb->crcn.flags =
5469 scan_info->request_ccb->crcn.flags;
5471 xpt_action(request_ccb);
5481 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */
5485 PROBE_TUR_FOR_NEGOTIATION,
5486 PROBE_INQUIRY_BASIC_DV1,
5487 PROBE_INQUIRY_BASIC_DV2,
5492 PROBE_INQUIRY_CKSUM = 0x01,
5493 PROBE_SERIAL_CKSUM = 0x02,
5494 PROBE_NO_ANNOUNCE = 0x04
5498 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5499 probe_action action;
5500 union ccb saved_ccb;
5503 u_int8_t digest[16];
5507 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5508 cam_flags flags, union ccb *request_ccb)
5510 struct ccb_pathinq cpi;
5512 struct cam_path *new_path;
5513 struct cam_periph *old_periph;
5516 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5517 ("xpt_scan_lun\n"));
5519 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5520 cpi.ccb_h.func_code = XPT_PATH_INQ;
5521 xpt_action((union ccb *)&cpi);
5523 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5524 if (request_ccb != NULL) {
5525 request_ccb->ccb_h.status = cpi.ccb_h.status;
5526 xpt_done(request_ccb);
5531 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5533 * Can't scan the bus on an adapter that
5534 * cannot perform the initiator role.
5536 if (request_ccb != NULL) {
5537 request_ccb->ccb_h.status = CAM_REQ_CMP;
5538 xpt_done(request_ccb);
5543 if (request_ccb == NULL) {
5544 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5545 if (request_ccb == NULL) {
5546 xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
5547 "can't continue\n");
5550 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5551 if (new_path == NULL) {
5552 xpt_print(path, "xpt_scan_lun: can't allocate path, "
5553 "can't continue\n");
5554 free(request_ccb, M_TEMP);
5557 status = xpt_compile_path(new_path, xpt_periph,
5559 path->target->target_id,
5560 path->device->lun_id);
5562 if (status != CAM_REQ_CMP) {
5563 xpt_print(path, "xpt_scan_lun: can't compile path, "
5564 "can't continue\n");
5565 free(request_ccb, M_TEMP);
5566 free(new_path, M_TEMP);
5569 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5570 request_ccb->ccb_h.cbfcnp = xptscandone;
5571 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5572 request_ccb->crcn.flags = flags;
5576 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5579 softc = (probe_softc *)old_periph->softc;
5580 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5583 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5584 probestart, "probe",
5586 request_ccb->ccb_h.path, NULL, 0,
5589 if (status != CAM_REQ_CMP) {
5590 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
5591 "returned an error, can't continue probe\n");
5592 request_ccb->ccb_h.status = status;
5593 xpt_done(request_ccb);
5600 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5602 xpt_release_path(done_ccb->ccb_h.path);
5603 free(done_ccb->ccb_h.path, M_TEMP);
5604 free(done_ccb, M_TEMP);
5608 proberegister(struct cam_periph *periph, void *arg)
5610 union ccb *request_ccb; /* CCB representing the probe request */
5613 request_ccb = (union ccb *)arg;
5614 if (periph == NULL) {
5615 printf("proberegister: periph was NULL!!\n");
5616 return(CAM_REQ_CMP_ERR);
5619 if (request_ccb == NULL) {
5620 printf("proberegister: no probe CCB, "
5621 "can't register device\n");
5622 return(CAM_REQ_CMP_ERR);
5625 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5627 if (softc == NULL) {
5628 printf("proberegister: Unable to probe new device. "
5629 "Unable to allocate softc\n");
5630 return(CAM_REQ_CMP_ERR);
5632 TAILQ_INIT(&softc->request_ccbs);
5633 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5636 periph->softc = softc;
5637 cam_periph_acquire(periph);
5639 * Ensure we've waited at least a bus settle
5640 * delay before attempting to probe the device.
5641 * For HBAs that don't do bus resets, this won't make a difference.
5643 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5645 probeschedule(periph);
5646 return(CAM_REQ_CMP);
5650 probeschedule(struct cam_periph *periph)
5652 struct ccb_pathinq cpi;
5656 softc = (probe_softc *)periph->softc;
5657 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5659 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5660 cpi.ccb_h.func_code = XPT_PATH_INQ;
5661 xpt_action((union ccb *)&cpi);
5664 * If a device has gone away and another device, or the same one,
5665 * is back in the same place, it should have a unit attention
5666 * condition pending. It will not report the unit attention in
5667 * response to an inquiry, which may leave invalid transfer
5668 * negotiations in effect. The TUR will reveal the unit attention
5669 * condition. Only send the TUR for lun 0, since some devices
5670 * will get confused by commands other than inquiry to non-existent
5671 * luns. If you think a device has gone away start your scan from
5672 * lun 0. This will insure that any bogus transfer settings are
5675 * If we haven't seen the device before and the controller supports
5676 * some kind of transfer negotiation, negotiate with the first
5677 * sent command if no bus reset was performed at startup. This
5678 * ensures that the device is not confused by transfer negotiation
5679 * settings left over by loader or BIOS action.
5681 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5682 && (ccb->ccb_h.target_lun == 0)) {
5683 softc->action = PROBE_TUR;
5684 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5685 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5686 proberequestdefaultnegotiation(periph);
5687 softc->action = PROBE_INQUIRY;
5689 softc->action = PROBE_INQUIRY;
5692 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5693 softc->flags |= PROBE_NO_ANNOUNCE;
5695 softc->flags &= ~PROBE_NO_ANNOUNCE;
5697 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5701 probestart(struct cam_periph *periph, union ccb *start_ccb)
5703 /* Probe the device that our peripheral driver points to */
5704 struct ccb_scsiio *csio;
5707 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5709 softc = (probe_softc *)periph->softc;
5710 csio = &start_ccb->csio;
5712 switch (softc->action) {
5714 case PROBE_TUR_FOR_NEGOTIATION:
5717 scsi_test_unit_ready(csio,
5726 case PROBE_FULL_INQUIRY:
5727 case PROBE_INQUIRY_BASIC_DV1:
5728 case PROBE_INQUIRY_BASIC_DV2:
5731 struct scsi_inquiry_data *inq_buf;
5733 inq_buf = &periph->path->device->inq_data;
5736 * If the device is currently configured, we calculate an
5737 * MD5 checksum of the inquiry data, and if the serial number
5738 * length is greater than 0, add the serial number data
5739 * into the checksum as well. Once the inquiry and the
5740 * serial number check finish, we attempt to figure out
5741 * whether we still have the same device.
5743 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5745 MD5Init(&softc->context);
5746 MD5Update(&softc->context, (unsigned char *)inq_buf,
5747 sizeof(struct scsi_inquiry_data));
5748 softc->flags |= PROBE_INQUIRY_CKSUM;
5749 if (periph->path->device->serial_num_len > 0) {
5750 MD5Update(&softc->context,
5751 periph->path->device->serial_num,
5752 periph->path->device->serial_num_len);
5753 softc->flags |= PROBE_SERIAL_CKSUM;
5755 MD5Final(softc->digest, &softc->context);
5758 if (softc->action == PROBE_INQUIRY)
5759 inquiry_len = SHORT_INQUIRY_LENGTH;
5761 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
5764 * Some parallel SCSI devices fail to send an
5765 * ignore wide residue message when dealing with
5766 * odd length inquiry requests. Round up to be
5769 inquiry_len = roundup2(inquiry_len, 2);
5771 if (softc->action == PROBE_INQUIRY_BASIC_DV1
5772 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
5773 inq_buf = malloc(inquiry_len, M_TEMP, M_NOWAIT);
5775 if (inq_buf == NULL) {
5776 xpt_print(periph->path, "malloc failure- skipping Basic"
5777 "Domain Validation\n");
5778 softc->action = PROBE_DV_EXIT;
5779 scsi_test_unit_ready(csio,
5791 (u_int8_t *)inq_buf,
5796 /*timeout*/60 * 1000);
5799 case PROBE_MODE_SENSE:
5804 mode_buf_len = sizeof(struct scsi_mode_header_6)
5805 + sizeof(struct scsi_mode_blk_desc)
5806 + sizeof(struct scsi_control_page);
5807 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5808 if (mode_buf != NULL) {
5809 scsi_mode_sense(csio,
5814 SMS_PAGE_CTRL_CURRENT,
5815 SMS_CONTROL_MODE_PAGE,
5822 xpt_print(periph->path, "Unable to mode sense control page - "
5823 "malloc failure\n");
5824 softc->action = PROBE_SERIAL_NUM;
5827 case PROBE_SERIAL_NUM:
5829 struct scsi_vpd_unit_serial_number *serial_buf;
5830 struct cam_ed* device;
5833 device = periph->path->device;
5834 device->serial_num = NULL;
5835 device->serial_num_len = 0;
5837 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5838 serial_buf = (struct scsi_vpd_unit_serial_number *)
5839 malloc(sizeof(*serial_buf), M_TEMP,
5842 if (serial_buf != NULL) {
5847 (u_int8_t *)serial_buf,
5848 sizeof(*serial_buf),
5850 SVPD_UNIT_SERIAL_NUMBER,
5852 /*timeout*/60 * 1000);
5856 * We'll have to do without, let our probedone
5857 * routine finish up for us.
5859 start_ccb->csio.data_ptr = NULL;
5860 probedone(periph, start_ccb);
5864 xpt_action(start_ccb);
5868 proberequestdefaultnegotiation(struct cam_periph *periph)
5870 struct ccb_trans_settings cts;
5872 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5873 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5874 cts.type = CTS_TYPE_USER_SETTINGS;
5875 xpt_action((union ccb *)&cts);
5876 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5879 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5880 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5881 xpt_action((union ccb *)&cts);
5885 * Backoff Negotiation Code- only pertinent for SPI devices.
5888 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
5890 struct ccb_trans_settings cts;
5891 struct ccb_trans_settings_spi *spi;
5893 memset(&cts, 0, sizeof (cts));
5894 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5895 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5896 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5897 xpt_action((union ccb *)&cts);
5898 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5900 xpt_print(periph->path,
5901 "failed to get current device settings\n");
5905 if (cts.transport != XPORT_SPI) {
5907 xpt_print(periph->path, "not SPI transport\n");
5911 spi = &cts.xport_specific.spi;
5914 * We cannot renegotiate sync rate if we don't have one.
5916 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
5918 xpt_print(periph->path, "no sync rate known\n");
5924 * We'll assert that we don't have to touch PPR options- the
5925 * SIM will see what we do with period and offset and adjust
5926 * the PPR options as appropriate.
5930 * A sync rate with unknown or zero offset is nonsensical.
5931 * A sync period of zero means Async.
5933 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
5934 || spi->sync_offset == 0 || spi->sync_period == 0) {
5936 xpt_print(periph->path, "no sync rate available\n");
5941 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
5942 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5943 ("hit async: giving up on DV\n"));
5949 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
5950 * We don't try to remember 'last' settings to see if the SIM actually
5951 * gets into the speed we want to set. We check on the SIM telling
5952 * us that a requested speed is bad, but otherwise don't try and
5953 * check the speed due to the asynchronous and handshake nature
5956 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
5959 if (spi->sync_period >= 0xf) {
5960 spi->sync_period = 0;
5961 spi->sync_offset = 0;
5962 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5963 ("setting to async for DV\n"));
5965 * Once we hit async, we don't want to try
5966 * any more settings.
5968 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
5969 } else if (bootverbose) {
5970 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5971 ("DV: period 0x%x\n", spi->sync_period));
5972 printf("setting period to 0x%x\n", spi->sync_period);
5974 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5975 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5976 xpt_action((union ccb *)&cts);
5977 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5980 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5981 ("DV: failed to set period 0x%x\n", spi->sync_period));
5982 if (spi->sync_period == 0) {
5990 probedone(struct cam_periph *periph, union ccb *done_ccb)
5993 struct cam_path *path;
5996 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5998 softc = (probe_softc *)periph->softc;
5999 path = done_ccb->ccb_h.path;
6000 priority = done_ccb->ccb_h.pinfo.priority;
6002 switch (softc->action) {
6005 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6007 if (cam_periph_error(done_ccb, 0,
6008 SF_NO_PRINT, NULL) == ERESTART)
6010 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
6011 /* Don't wedge the queue */
6012 xpt_release_devq(done_ccb->ccb_h.path,
6016 softc->action = PROBE_INQUIRY;
6017 xpt_release_ccb(done_ccb);
6018 xpt_schedule(periph, priority);
6022 case PROBE_FULL_INQUIRY:
6024 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6025 struct scsi_inquiry_data *inq_buf;
6026 u_int8_t periph_qual;
6028 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
6029 inq_buf = &path->device->inq_data;
6031 periph_qual = SID_QUAL(inq_buf);
6033 switch(periph_qual) {
6034 case SID_QUAL_LU_CONNECTED:
6039 * We conservatively request only
6040 * SHORT_INQUIRY_LEN bytes of inquiry
6041 * information during our first try
6042 * at sending an INQUIRY. If the device
6043 * has more information to give,
6044 * perform a second request specifying
6045 * the amount of information the device
6046 * is willing to give.
6048 len = inq_buf->additional_length
6049 + offsetof(struct scsi_inquiry_data,
6050 additional_length) + 1;
6051 if (softc->action == PROBE_INQUIRY
6052 && len > SHORT_INQUIRY_LENGTH) {
6053 softc->action = PROBE_FULL_INQUIRY;
6054 xpt_release_ccb(done_ccb);
6055 xpt_schedule(periph, priority);
6059 xpt_find_quirk(path->device);
6061 xpt_devise_transport(path);
6062 if (INQ_DATA_TQ_ENABLED(inq_buf))
6063 softc->action = PROBE_MODE_SENSE;
6065 softc->action = PROBE_SERIAL_NUM;
6067 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6069 xpt_release_ccb(done_ccb);
6070 xpt_schedule(periph, priority);
6076 } else if (cam_periph_error(done_ccb, 0,
6077 done_ccb->ccb_h.target_lun > 0
6078 ? SF_RETRY_UA|SF_QUIET_IR
6080 &softc->saved_ccb) == ERESTART) {
6082 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6083 /* Don't wedge the queue */
6084 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6088 * If we get to this point, we got an error status back
6089 * from the inquiry and the error status doesn't require
6090 * automatically retrying the command. Therefore, the
6091 * inquiry failed. If we had inquiry information before
6092 * for this device, but this latest inquiry command failed,
6093 * the device has probably gone away. If this device isn't
6094 * already marked unconfigured, notify the peripheral
6095 * drivers that this device is no more.
6097 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
6098 /* Send the async notification. */
6099 xpt_async(AC_LOST_DEVICE, path, NULL);
6101 xpt_release_ccb(done_ccb);
6104 case PROBE_MODE_SENSE:
6106 struct ccb_scsiio *csio;
6107 struct scsi_mode_header_6 *mode_hdr;
6109 csio = &done_ccb->csio;
6110 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
6111 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6112 struct scsi_control_page *page;
6115 offset = ((u_int8_t *)&mode_hdr[1])
6116 + mode_hdr->blk_desc_len;
6117 page = (struct scsi_control_page *)offset;
6118 path->device->queue_flags = page->queue_flags;
6119 } else if (cam_periph_error(done_ccb, 0,
6120 SF_RETRY_UA|SF_NO_PRINT,
6121 &softc->saved_ccb) == ERESTART) {
6123 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6124 /* Don't wedge the queue */
6125 xpt_release_devq(done_ccb->ccb_h.path,
6126 /*count*/1, /*run_queue*/TRUE);
6128 xpt_release_ccb(done_ccb);
6129 free(mode_hdr, M_TEMP);
6130 softc->action = PROBE_SERIAL_NUM;
6131 xpt_schedule(periph, priority);
6134 case PROBE_SERIAL_NUM:
6136 struct ccb_scsiio *csio;
6137 struct scsi_vpd_unit_serial_number *serial_buf;
6144 csio = &done_ccb->csio;
6145 priority = done_ccb->ccb_h.pinfo.priority;
6147 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
6149 /* Clean up from previous instance of this device */
6150 if (path->device->serial_num != NULL) {
6151 free(path->device->serial_num, M_CAMXPT);
6152 path->device->serial_num = NULL;
6153 path->device->serial_num_len = 0;
6156 if (serial_buf == NULL) {
6158 * Don't process the command as it was never sent
6160 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
6161 && (serial_buf->length > 0)) {
6164 path->device->serial_num =
6165 (u_int8_t *)malloc((serial_buf->length + 1),
6166 M_CAMXPT, M_NOWAIT);
6167 if (path->device->serial_num != NULL) {
6168 bcopy(serial_buf->serial_num,
6169 path->device->serial_num,
6170 serial_buf->length);
6171 path->device->serial_num_len =
6173 path->device->serial_num[serial_buf->length]
6176 } else if (cam_periph_error(done_ccb, 0,
6177 SF_RETRY_UA|SF_NO_PRINT,
6178 &softc->saved_ccb) == ERESTART) {
6180 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6181 /* Don't wedge the queue */
6182 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6187 * Let's see if we have seen this device before.
6189 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6191 u_int8_t digest[16];
6196 (unsigned char *)&path->device->inq_data,
6197 sizeof(struct scsi_inquiry_data));
6200 MD5Update(&context, serial_buf->serial_num,
6201 serial_buf->length);
6203 MD5Final(digest, &context);
6204 if (bcmp(softc->digest, digest, 16) == 0)
6208 * XXX Do we need to do a TUR in order to ensure
6209 * that the device really hasn't changed???
6212 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6213 xpt_async(AC_LOST_DEVICE, path, NULL);
6215 if (serial_buf != NULL)
6216 free(serial_buf, M_TEMP);
6220 * Now that we have all the necessary
6221 * information to safely perform transfer
6222 * negotiations... Controllers don't perform
6223 * any negotiation or tagged queuing until
6224 * after the first XPT_SET_TRAN_SETTINGS ccb is
6225 * received. So, on a new device, just retrieve
6226 * the user settings, and set them as the current
6227 * settings to set the device up.
6229 proberequestdefaultnegotiation(periph);
6230 xpt_release_ccb(done_ccb);
6233 * Perform a TUR to allow the controller to
6234 * perform any necessary transfer negotiation.
6236 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6237 xpt_schedule(periph, priority);
6240 xpt_release_ccb(done_ccb);
6243 case PROBE_TUR_FOR_NEGOTIATION:
6245 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6246 /* Don't wedge the queue */
6247 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6251 * Do Domain Validation for lun 0 on devices that claim
6252 * to support Synchronous Transfer modes.
6254 if (softc->action == PROBE_TUR_FOR_NEGOTIATION
6255 && done_ccb->ccb_h.target_lun == 0
6256 && (path->device->inq_data.flags & SID_Sync) != 0
6257 && (path->device->flags & CAM_DEV_IN_DV) == 0) {
6258 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6259 ("Begin Domain Validation\n"));
6260 path->device->flags |= CAM_DEV_IN_DV;
6261 xpt_release_ccb(done_ccb);
6262 softc->action = PROBE_INQUIRY_BASIC_DV1;
6263 xpt_schedule(periph, priority);
6266 if (softc->action == PROBE_DV_EXIT) {
6267 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6268 ("Leave Domain Validation\n"));
6270 path->device->flags &=
6271 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
6272 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6273 /* Inform the XPT that a new device has been found */
6274 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6275 xpt_action(done_ccb);
6276 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6279 xpt_release_ccb(done_ccb);
6281 case PROBE_INQUIRY_BASIC_DV1:
6282 case PROBE_INQUIRY_BASIC_DV2:
6284 struct scsi_inquiry_data *nbuf;
6285 struct ccb_scsiio *csio;
6287 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6288 /* Don't wedge the queue */
6289 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6292 csio = &done_ccb->csio;
6293 nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
6294 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
6296 "inquiry data fails comparison at DV%d step\n",
6297 softc->action == PROBE_INQUIRY_BASIC_DV1? 1 : 2);
6298 if (proberequestbackoff(periph, path->device)) {
6299 path->device->flags &= ~CAM_DEV_IN_DV;
6300 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6303 softc->action = PROBE_DV_EXIT;
6306 xpt_release_ccb(done_ccb);
6307 xpt_schedule(periph, priority);
6311 if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
6312 softc->action = PROBE_INQUIRY_BASIC_DV2;
6313 xpt_release_ccb(done_ccb);
6314 xpt_schedule(periph, priority);
6317 if (softc->action == PROBE_DV_EXIT) {
6318 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6319 ("Leave Domain Validation Successfully\n"));
6321 path->device->flags &=
6322 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
6323 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6324 /* Inform the XPT that a new device has been found */
6325 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6326 xpt_action(done_ccb);
6327 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6330 xpt_release_ccb(done_ccb);
6334 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6335 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6336 done_ccb->ccb_h.status = CAM_REQ_CMP;
6338 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6339 cam_periph_invalidate(periph);
6340 cam_periph_release(periph);
6342 probeschedule(periph);
6347 probecleanup(struct cam_periph *periph)
6349 free(periph->softc, M_TEMP);
6353 xpt_find_quirk(struct cam_ed *device)
6357 match = cam_quirkmatch((caddr_t)&device->inq_data,
6358 (caddr_t)xpt_quirk_table,
6359 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6360 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6363 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6365 device->quirk = (struct xpt_quirk_entry *)match;
6369 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
6374 error = sysctl_handle_int(oidp, &bool, sizeof(bool), req);
6375 if (error != 0 || req->newptr == NULL)
6377 if (bool == 0 || bool == 1) {
6387 xpt_devise_transport(struct cam_path *path)
6389 struct ccb_pathinq cpi;
6390 struct ccb_trans_settings cts;
6391 struct scsi_inquiry_data *inq_buf;
6393 /* Get transport information from the SIM */
6394 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6395 cpi.ccb_h.func_code = XPT_PATH_INQ;
6396 xpt_action((union ccb *)&cpi);
6399 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6400 inq_buf = &path->device->inq_data;
6401 path->device->protocol = PROTO_SCSI;
6402 path->device->protocol_version =
6403 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6404 path->device->transport = cpi.transport;
6405 path->device->transport_version = cpi.transport_version;
6408 * Any device not using SPI3 features should
6409 * be considered SPI2 or lower.
6411 if (inq_buf != NULL) {
6412 if (path->device->transport == XPORT_SPI
6413 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6414 && path->device->transport_version > 2)
6415 path->device->transport_version = 2;
6417 struct cam_ed* otherdev;
6419 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6421 otherdev = TAILQ_NEXT(otherdev, links)) {
6422 if (otherdev != path->device)
6426 if (otherdev != NULL) {
6428 * Initially assume the same versioning as
6429 * prior luns for this target.
6431 path->device->protocol_version =
6432 otherdev->protocol_version;
6433 path->device->transport_version =
6434 otherdev->transport_version;
6436 /* Until we know better, opt for safty */
6437 path->device->protocol_version = 2;
6438 if (path->device->transport == XPORT_SPI)
6439 path->device->transport_version = 2;
6441 path->device->transport_version = 0;
6447 * For a device compliant with SPC-2 we should be able
6448 * to determine the transport version supported by
6449 * scrutinizing the version descriptors in the
6453 /* Tell the controller what we think */
6454 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6455 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6456 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6457 cts.transport = path->device->transport;
6458 cts.transport_version = path->device->transport_version;
6459 cts.protocol = path->device->protocol;
6460 cts.protocol_version = path->device->protocol_version;
6461 cts.proto_specific.valid = 0;
6462 cts.xport_specific.valid = 0;
6463 xpt_action((union ccb *)&cts);
6467 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6470 struct ccb_pathinq cpi;
6471 struct ccb_trans_settings cur_cts;
6472 struct ccb_trans_settings_scsi *scsi;
6473 struct ccb_trans_settings_scsi *cur_scsi;
6474 struct cam_sim *sim;
6475 struct scsi_inquiry_data *inq_data;
6477 if (device == NULL) {
6478 cts->ccb_h.status = CAM_PATH_INVALID;
6479 xpt_done((union ccb *)cts);
6483 if (cts->protocol == PROTO_UNKNOWN
6484 || cts->protocol == PROTO_UNSPECIFIED) {
6485 cts->protocol = device->protocol;
6486 cts->protocol_version = device->protocol_version;
6489 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6490 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6491 cts->protocol_version = device->protocol_version;
6493 if (cts->protocol != device->protocol) {
6494 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
6495 cts->protocol, device->protocol);
6496 cts->protocol = device->protocol;
6499 if (cts->protocol_version > device->protocol_version) {
6501 xpt_print(cts->ccb_h.path, "Down reving Protocol "
6502 "Version from %d to %d?\n", cts->protocol_version,
6503 device->protocol_version);
6505 cts->protocol_version = device->protocol_version;
6508 if (cts->transport == XPORT_UNKNOWN
6509 || cts->transport == XPORT_UNSPECIFIED) {
6510 cts->transport = device->transport;
6511 cts->transport_version = device->transport_version;
6514 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6515 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6516 cts->transport_version = device->transport_version;
6518 if (cts->transport != device->transport) {
6519 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
6520 cts->transport, device->transport);
6521 cts->transport = device->transport;
6524 if (cts->transport_version > device->transport_version) {
6526 xpt_print(cts->ccb_h.path, "Down reving Transport "
6527 "Version from %d to %d?\n", cts->transport_version,
6528 device->transport_version);
6530 cts->transport_version = device->transport_version;
6533 sim = cts->ccb_h.path->bus->sim;
6536 * Nothing more of interest to do unless
6537 * this is a device connected via the
6540 if (cts->protocol != PROTO_SCSI) {
6541 if (async_update == FALSE)
6542 (*(sim->sim_action))(sim, (union ccb *)cts);
6546 inq_data = &device->inq_data;
6547 scsi = &cts->proto_specific.scsi;
6548 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6549 cpi.ccb_h.func_code = XPT_PATH_INQ;
6550 xpt_action((union ccb *)&cpi);
6552 /* SCSI specific sanity checking */
6553 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6554 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
6555 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6556 || (device->quirk->mintags == 0)) {
6558 * Can't tag on hardware that doesn't support tags,
6559 * doesn't have it enabled, or has broken tag support.
6561 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6564 if (async_update == FALSE) {
6566 * Perform sanity checking against what the
6567 * controller and device can do.
6569 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6570 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6571 cur_cts.type = cts->type;
6572 xpt_action((union ccb *)&cur_cts);
6573 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6576 cur_scsi = &cur_cts.proto_specific.scsi;
6577 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6578 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6579 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6581 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6582 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6585 /* SPI specific sanity checking */
6586 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6588 struct ccb_trans_settings_spi *spi;
6589 struct ccb_trans_settings_spi *cur_spi;
6591 spi = &cts->xport_specific.spi;
6593 cur_spi = &cur_cts.xport_specific.spi;
6595 /* Fill in any gaps in what the user gave us */
6596 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6597 spi->sync_period = cur_spi->sync_period;
6598 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6599 spi->sync_period = 0;
6600 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6601 spi->sync_offset = cur_spi->sync_offset;
6602 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6603 spi->sync_offset = 0;
6604 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6605 spi->ppr_options = cur_spi->ppr_options;
6606 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6607 spi->ppr_options = 0;
6608 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6609 spi->bus_width = cur_spi->bus_width;
6610 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6612 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6613 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6614 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6616 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6617 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6618 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6619 && (inq_data->flags & SID_Sync) == 0
6620 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6621 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6622 || (spi->sync_offset == 0)
6623 || (spi->sync_period == 0)) {
6625 spi->sync_period = 0;
6626 spi->sync_offset = 0;
6629 switch (spi->bus_width) {
6630 case MSG_EXT_WDTR_BUS_32_BIT:
6631 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6632 || (inq_data->flags & SID_WBus32) != 0
6633 || cts->type == CTS_TYPE_USER_SETTINGS)
6634 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6636 /* Fall Through to 16-bit */
6637 case MSG_EXT_WDTR_BUS_16_BIT:
6638 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6639 || (inq_data->flags & SID_WBus16) != 0
6640 || cts->type == CTS_TYPE_USER_SETTINGS)
6641 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6642 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6645 /* Fall Through to 8-bit */
6646 default: /* New bus width?? */
6647 case MSG_EXT_WDTR_BUS_8_BIT:
6648 /* All targets can do this */
6649 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6653 spi3caps = cpi.xport_specific.spi.ppr_options;
6654 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6655 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6656 spi3caps &= inq_data->spi3data;
6658 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6659 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6661 if ((spi3caps & SID_SPI_IUS) == 0)
6662 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6664 if ((spi3caps & SID_SPI_QAS) == 0)
6665 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6667 /* No SPI Transfer settings are allowed unless we are wide */
6668 if (spi->bus_width == 0)
6669 spi->ppr_options = 0;
6671 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6673 * Can't tag queue without disconnection.
6675 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6676 scsi->valid |= CTS_SCSI_VALID_TQ;
6680 * If we are currently performing tagged transactions to
6681 * this device and want to change its negotiation parameters,
6682 * go non-tagged for a bit to give the controller a chance to
6683 * negotiate unhampered by tag messages.
6685 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6686 && (device->inq_flags & SID_CmdQue) != 0
6687 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6688 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6689 CTS_SPI_VALID_SYNC_OFFSET|
6690 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6691 xpt_toggle_tags(cts->ccb_h.path);
6694 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6695 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6699 * If we are transitioning from tags to no-tags or
6700 * vice-versa, we need to carefully freeze and restart
6701 * the queue so that we don't overlap tagged and non-tagged
6702 * commands. We also temporarily stop tags if there is
6703 * a change in transfer negotiation settings to allow
6704 * "tag-less" negotiation.
6706 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6707 || (device->inq_flags & SID_CmdQue) != 0)
6708 device_tagenb = TRUE;
6710 device_tagenb = FALSE;
6712 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6713 && device_tagenb == FALSE)
6714 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6715 && device_tagenb == TRUE)) {
6717 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6719 * Delay change to use tags until after a
6720 * few commands have gone to this device so
6721 * the controller has time to perform transfer
6722 * negotiations without tagged messages getting
6725 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6726 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6728 struct ccb_relsim crs;
6730 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6731 device->inq_flags &= ~SID_CmdQue;
6732 xpt_dev_ccbq_resize(cts->ccb_h.path,
6733 sim->max_dev_openings);
6734 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6735 device->tag_delay_count = 0;
6737 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6739 crs.ccb_h.func_code = XPT_REL_SIMQ;
6740 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6742 = crs.release_timeout
6745 xpt_action((union ccb *)&crs);
6749 if (async_update == FALSE)
6750 (*(sim->sim_action))(sim, (union ccb *)cts);
6755 xpt_toggle_tags(struct cam_path *path)
6760 * Give controllers a chance to renegotiate
6761 * before starting tag operations. We
6762 * "toggle" tagged queuing off then on
6763 * which causes the tag enable command delay
6764 * counter to come into effect.
6767 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6768 || ((dev->inq_flags & SID_CmdQue) != 0
6769 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6770 struct ccb_trans_settings cts;
6772 xpt_setup_ccb(&cts.ccb_h, path, 1);
6773 cts.protocol = PROTO_SCSI;
6774 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6775 cts.transport = XPORT_UNSPECIFIED;
6776 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6777 cts.proto_specific.scsi.flags = 0;
6778 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6779 xpt_set_transfer_settings(&cts, path->device,
6780 /*async_update*/TRUE);
6781 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6782 xpt_set_transfer_settings(&cts, path->device,
6783 /*async_update*/TRUE);
6788 xpt_start_tags(struct cam_path *path)
6790 struct ccb_relsim crs;
6791 struct cam_ed *device;
6792 struct cam_sim *sim;
6795 device = path->device;
6796 sim = path->bus->sim;
6797 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6798 xpt_freeze_devq(path, /*count*/1);
6799 device->inq_flags |= SID_CmdQue;
6800 if (device->tag_saved_openings != 0)
6801 newopenings = device->tag_saved_openings;
6803 newopenings = min(device->quirk->maxtags,
6804 sim->max_tagged_dev_openings);
6805 xpt_dev_ccbq_resize(path, newopenings);
6806 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6807 crs.ccb_h.func_code = XPT_REL_SIMQ;
6808 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6810 = crs.release_timeout
6813 xpt_action((union ccb *)&crs);
6816 static int busses_to_config;
6817 static int busses_to_reset;
6820 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6822 if (bus->path_id != CAM_XPT_PATH_ID) {
6823 struct cam_path path;
6824 struct ccb_pathinq cpi;
6828 xpt_compile_path(&path, NULL, bus->path_id,
6829 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6830 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6831 cpi.ccb_h.func_code = XPT_PATH_INQ;
6832 xpt_action((union ccb *)&cpi);
6833 can_negotiate = cpi.hba_inquiry;
6834 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6835 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6838 xpt_release_path(&path);
6845 xptconfigfunc(struct cam_eb *bus, void *arg)
6847 struct cam_path *path;
6848 union ccb *work_ccb;
6850 if (bus->path_id != CAM_XPT_PATH_ID) {
6854 work_ccb = xpt_alloc_ccb();
6855 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6856 CAM_TARGET_WILDCARD,
6857 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6858 printf("xptconfigfunc: xpt_create_path failed with "
6859 "status %#x for bus %d\n", status, bus->path_id);
6860 printf("xptconfigfunc: halting bus configuration\n");
6861 xpt_free_ccb(work_ccb);
6863 xpt_finishconfig(xpt_periph, NULL);
6866 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6867 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6868 xpt_action(work_ccb);
6869 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6870 printf("xptconfigfunc: CPI failed on bus %d "
6871 "with status %d\n", bus->path_id,
6872 work_ccb->ccb_h.status);
6873 xpt_finishconfig(xpt_periph, work_ccb);
6877 can_negotiate = work_ccb->cpi.hba_inquiry;
6878 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6879 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6880 && (can_negotiate != 0)) {
6881 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6882 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6883 work_ccb->ccb_h.cbfcnp = NULL;
6884 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6885 ("Resetting Bus\n"));
6886 xpt_action(work_ccb);
6887 xpt_finishconfig(xpt_periph, work_ccb);
6889 /* Act as though we performed a successful BUS RESET */
6890 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6891 xpt_finishconfig(xpt_periph, work_ccb);
6899 xpt_config(void *arg)
6902 * Now that interrupts are enabled, go find our devices
6906 /* Setup debugging flags and path */
6907 #ifdef CAM_DEBUG_FLAGS
6908 cam_dflags = CAM_DEBUG_FLAGS;
6909 #else /* !CAM_DEBUG_FLAGS */
6910 cam_dflags = CAM_DEBUG_NONE;
6911 #endif /* CAM_DEBUG_FLAGS */
6912 #ifdef CAM_DEBUG_BUS
6913 if (cam_dflags != CAM_DEBUG_NONE) {
6914 if (xpt_create_path(&cam_dpath, xpt_periph,
6915 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6916 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6917 printf("xpt_config: xpt_create_path() failed for debug"
6918 " target %d:%d:%d, debugging disabled\n",
6919 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6920 cam_dflags = CAM_DEBUG_NONE;
6924 #else /* !CAM_DEBUG_BUS */
6926 #endif /* CAM_DEBUG_BUS */
6927 #endif /* CAMDEBUG */
6930 * Scan all installed busses.
6932 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6934 if (busses_to_config == 0) {
6935 /* Call manually because we don't have any busses */
6936 xpt_finishconfig(xpt_periph, NULL);
6938 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6939 printf("Waiting %d seconds for SCSI "
6940 "devices to settle\n", scsi_delay/1000);
6942 xpt_for_all_busses(xptconfigfunc, NULL);
6947 * If the given device only has one peripheral attached to it, and if that
6948 * peripheral is the passthrough driver, announce it. This insures that the
6949 * user sees some sort of announcement for every peripheral in their system.
6952 xptpassannouncefunc(struct cam_ed *device, void *arg)
6954 struct cam_periph *periph;
6957 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6958 periph = SLIST_NEXT(periph, periph_links), i++);
6960 periph = SLIST_FIRST(&device->periphs);
6962 && (strncmp(periph->periph_name, "pass", 4) == 0))
6963 xpt_announce_periph(periph, NULL);
6969 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6971 struct periph_driver **p_drv;
6974 if (done_ccb != NULL) {
6975 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6976 ("xpt_finishconfig\n"));
6977 switch(done_ccb->ccb_h.func_code) {
6979 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6980 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6981 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6982 done_ccb->crcn.flags = 0;
6983 xpt_action(done_ccb);
6989 xpt_free_path(done_ccb->ccb_h.path);
6995 if (busses_to_config == 0) {
6996 /* Register all the peripheral drivers */
6997 /* XXX This will have to change when we have loadable modules */
6998 p_drv = periph_drivers;
6999 for (i = 0; p_drv[i] != NULL; i++) {
7000 (*p_drv[i]->init)();
7004 * Check for devices with no "standard" peripheral driver
7005 * attached. For any devices like that, announce the
7006 * passthrough driver so the user will see something.
7008 xpt_for_all_devices(xptpassannouncefunc, NULL);
7010 /* Release our hook so that the boot can continue. */
7011 config_intrhook_disestablish(xpt_config_hook);
7012 free(xpt_config_hook, M_TEMP);
7013 xpt_config_hook = NULL;
7015 if (done_ccb != NULL)
7016 xpt_free_ccb(done_ccb);
7020 xptaction(struct cam_sim *sim, union ccb *work_ccb)
7022 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
7024 switch (work_ccb->ccb_h.func_code) {
7025 /* Common cases first */
7026 case XPT_PATH_INQ: /* Path routing inquiry */
7028 struct ccb_pathinq *cpi;
7030 cpi = &work_ccb->cpi;
7031 cpi->version_num = 1; /* XXX??? */
7032 cpi->hba_inquiry = 0;
7033 cpi->target_sprt = 0;
7035 cpi->hba_eng_cnt = 0;
7036 cpi->max_target = 0;
7038 cpi->initiator_id = 0;
7039 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
7040 strncpy(cpi->hba_vid, "", HBA_IDLEN);
7041 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
7042 cpi->unit_number = sim->unit_number;
7043 cpi->bus_id = sim->bus_id;
7044 cpi->base_transfer_speed = 0;
7045 cpi->protocol = PROTO_UNSPECIFIED;
7046 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
7047 cpi->transport = XPORT_UNSPECIFIED;
7048 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
7049 cpi->ccb_h.status = CAM_REQ_CMP;
7054 work_ccb->ccb_h.status = CAM_REQ_INVALID;
7061 * The xpt as a "controller" has no interrupt sources, so polling
7065 xptpoll(struct cam_sim *sim)
7070 camisr(void *V_queue)
7072 cam_isrq_t *oqueue = V_queue;
7075 struct ccb_hdr *ccb_h;
7078 * Transfer the ccb_bioq list to a temporary list so we can operate
7079 * on it without needing to lock/unlock on every loop. The concat
7080 * function with re-init the real list for us.
7083 mtx_lock(&cam_bioq_lock);
7085 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7086 mtx_unlock(&cam_bioq_lock);
7088 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7091 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7092 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7095 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7100 if (ccb_h->flags & CAM_HIGH_POWER) {
7101 struct highpowerlist *hphead;
7102 union ccb *send_ccb;
7104 hphead = &highpowerq;
7106 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7109 * Increment the count since this command is done.
7114 * Any high powered commands queued up?
7116 if (send_ccb != NULL) {
7118 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7120 xpt_release_devq(send_ccb->ccb_h.path,
7121 /*count*/1, /*runqueue*/TRUE);
7124 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7127 dev = ccb_h->path->device;
7130 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7132 if (!SIM_DEAD(ccb_h->path->bus->sim)) {
7133 ccb_h->path->bus->sim->devq->send_active--;
7134 ccb_h->path->bus->sim->devq->send_openings++;
7138 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7139 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7140 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7141 && (dev->ccbq.dev_active == 0))) {
7143 xpt_release_devq(ccb_h->path, /*count*/1,
7147 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7148 && (--dev->tag_delay_count == 0))
7149 xpt_start_tags(ccb_h->path);
7151 if ((dev->ccbq.queue.entries > 0)
7152 && (dev->qfrozen_cnt == 0)
7153 && (device_is_send_queued(dev) == 0)) {
7154 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7159 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7160 xpt_release_simq(ccb_h->path->bus->sim,
7162 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7166 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7167 && (ccb_h->status & CAM_DEV_QFRZN)) {
7168 xpt_release_devq(ccb_h->path, /*count*/1,
7170 ccb_h->status &= ~CAM_DEV_QFRZN;
7172 xpt_run_dev_sendq(ccb_h->path->bus);
7175 /* Call the peripheral driver's callback */
7176 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7178 /* Raise IPL for while test */
7185 dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7188 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7193 dead_sim_poll(struct cam_sim *sim)