2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
43 #include <sys/interrupt.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
51 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
68 /* Datastructures internal to the xpt layer */
69 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
72 * Definition of an async handler callback block. These are used to add
73 * SIMs and peripherals to the async callback lists.
76 SLIST_ENTRY(async_node) links;
77 u_int32_t event_enable; /* Async Event enables */
78 void (*callback)(void *arg, u_int32_t code,
79 struct cam_path *path, void *args);
83 SLIST_HEAD(async_list, async_node);
84 SLIST_HEAD(periph_list, cam_periph);
85 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
88 * This is the maximum number of high powered commands (e.g. start unit)
89 * that can be outstanding at a particular time.
91 #ifndef CAM_MAX_HIGHPOWER
92 #define CAM_MAX_HIGHPOWER 4
95 /* number of high powered commands that can go through right now */
96 static int num_highpower = CAM_MAX_HIGHPOWER;
99 * Structure for queueing a device in a run queue.
100 * There is one run queue for allocating new ccbs,
101 * and another for sending ccbs to the controller.
103 struct cam_ed_qinfo {
105 struct cam_ed *device;
109 * The CAM EDT (Existing Device Table) contains the device information for
110 * all devices for all busses in the system. The table contains a
111 * cam_ed structure for each device on the bus.
114 TAILQ_ENTRY(cam_ed) links;
115 struct cam_ed_qinfo alloc_ccb_entry;
116 struct cam_ed_qinfo send_ccb_entry;
117 struct cam_et *target;
120 * Queue of type drivers wanting to do
121 * work on this device.
123 struct cam_ccbq ccbq; /* Queue of pending ccbs */
124 struct async_list asyncs; /* Async callback info for this B/T/L */
125 struct periph_list periphs; /* All attached devices */
126 u_int generation; /* Generation number */
127 struct cam_periph *owner; /* Peripheral driver's ownership tag */
128 struct xpt_quirk_entry *quirk; /* Oddities about this device */
129 /* Storage for the inquiry data */
130 #ifdef CAM_NEW_TRAN_CODE
132 u_int protocol_version;
134 u_int transport_version;
135 #endif /* CAM_NEW_TRAN_CODE */
136 struct scsi_inquiry_data inq_data;
137 u_int8_t inq_flags; /*
138 * Current settings for inquiry flags.
139 * This allows us to override settings
140 * like disconnection and tagged
141 * queuing for a device.
143 u_int8_t queue_flags; /* Queue flags from the control page */
144 u_int8_t serial_num_len;
145 u_int8_t *serial_num;
146 u_int32_t qfrozen_cnt;
148 #define CAM_DEV_UNCONFIGURED 0x01
149 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
150 #define CAM_DEV_REL_ON_COMPLETE 0x04
151 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
152 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
153 #define CAM_DEV_TAG_AFTER_COUNT 0x20
154 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
155 u_int32_t tag_delay_count;
156 #define CAM_TAG_DELAY_COUNT 5
157 u_int32_t tag_saved_openings;
159 struct callout_handle c_handle;
163 * Each target is represented by an ET (Existing Target). These
164 * entries are created when a target is successfully probed with an
165 * identify, and removed when a device fails to respond after a number
166 * of retries, or a bus rescan finds the device missing.
169 TAILQ_HEAD(, cam_ed) ed_entries;
170 TAILQ_ENTRY(cam_et) links;
172 target_id_t target_id;
175 struct timeval last_reset;
179 * Each bus is represented by an EB (Existing Bus). These entries
180 * are created by calls to xpt_bus_register and deleted by calls to
181 * xpt_bus_deregister.
184 TAILQ_HEAD(, cam_et) et_entries;
185 TAILQ_ENTRY(cam_eb) links;
188 struct timeval last_reset;
190 #define CAM_EB_RUNQ_SCHEDULED 0x01
196 struct cam_periph *periph;
198 struct cam_et *target;
199 struct cam_ed *device;
202 struct xpt_quirk_entry {
203 struct scsi_inquiry_pattern inq_pat;
205 #define CAM_QUIRK_NOLUNS 0x01
206 #define CAM_QUIRK_NOSERIAL 0x02
207 #define CAM_QUIRK_HILUNS 0x04
208 #define CAM_QUIRK_NOHILUNS 0x08
213 static int cam_srch_hi = 0;
214 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
215 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
216 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
217 sysctl_cam_search_luns, "I",
218 "allow search above LUN 7 for SCSI3 and greater devices");
220 #define CAM_SCSI2_MAXLUN 8
222 * If we're not quirked to search <= the first 8 luns
223 * and we are either quirked to search above lun 8,
224 * or we're > SCSI-2 and we've enabled hilun searching,
225 * or we're > SCSI-2 and the last lun was a success,
226 * we can look for luns above lun 8.
228 #define CAN_SRCH_HI_SPARSE(dv) \
229 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
230 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
231 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
233 #define CAN_SRCH_HI_DENSE(dv) \
234 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
235 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
236 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
244 u_int32_t generation;
247 static const char quantum[] = "QUANTUM";
248 static const char sony[] = "SONY";
249 static const char west_digital[] = "WDIGTL";
250 static const char samsung[] = "SAMSUNG";
251 static const char seagate[] = "SEAGATE";
252 static const char microp[] = "MICROP";
254 static struct xpt_quirk_entry xpt_quirk_table[] =
257 /* Reports QUEUE FULL for temporary resource shortages */
258 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
259 /*quirks*/0, /*mintags*/24, /*maxtags*/32
262 /* Reports QUEUE FULL for temporary resource shortages */
263 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
264 /*quirks*/0, /*mintags*/24, /*maxtags*/32
267 /* Reports QUEUE FULL for temporary resource shortages */
268 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
269 /*quirks*/0, /*mintags*/24, /*maxtags*/32
272 /* Broken tagged queuing drive */
273 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
274 /*quirks*/0, /*mintags*/0, /*maxtags*/0
277 /* Broken tagged queuing drive */
278 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
279 /*quirks*/0, /*mintags*/0, /*maxtags*/0
282 /* Broken tagged queuing drive */
283 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
284 /*quirks*/0, /*mintags*/0, /*maxtags*/0
288 * Unfortunately, the Quantum Atlas III has the same
289 * problem as the Atlas II drives above.
290 * Reported by: "Johan Granlund" <johan@granlund.nu>
292 * For future reference, the drive with the problem was:
293 * QUANTUM QM39100TD-SW N1B0
295 * It's possible that Quantum will fix the problem in later
296 * firmware revisions. If that happens, the quirk entry
297 * will need to be made specific to the firmware revisions
301 /* Reports QUEUE FULL for temporary resource shortages */
302 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
303 /*quirks*/0, /*mintags*/24, /*maxtags*/32
307 * 18 Gig Atlas III, same problem as the 9G version.
308 * Reported by: Andre Albsmeier
309 * <andre.albsmeier@mchp.siemens.de>
311 * For future reference, the drive with the problem was:
312 * QUANTUM QM318000TD-S N491
314 /* Reports QUEUE FULL for temporary resource shortages */
315 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
316 /*quirks*/0, /*mintags*/24, /*maxtags*/32
320 * Broken tagged queuing drive
321 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
322 * and: Martin Renters <martin@tdc.on.ca>
324 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
325 /*quirks*/0, /*mintags*/0, /*maxtags*/0
328 * The Seagate Medalist Pro drives have very poor write
329 * performance with anything more than 2 tags.
331 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
332 * Drive: <SEAGATE ST36530N 1444>
334 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
335 * Drive: <SEAGATE ST34520W 1281>
337 * No one has actually reported that the 9G version
338 * (ST39140*) of the Medalist Pro has the same problem, but
339 * we're assuming that it does because the 4G and 6.5G
340 * versions of the drive are broken.
343 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
344 /*quirks*/0, /*mintags*/2, /*maxtags*/2
347 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
348 /*quirks*/0, /*mintags*/2, /*maxtags*/2
351 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
352 /*quirks*/0, /*mintags*/2, /*maxtags*/2
356 * Slow when tagged queueing is enabled. Write performance
357 * steadily drops off with more and more concurrent
358 * transactions. Best sequential write performance with
359 * tagged queueing turned off and write caching turned on.
362 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
363 * Drive: DCAS-34330 w/ "S65A" firmware.
365 * The drive with the problem had the "S65A" firmware
366 * revision, and has also been reported (by Stephen J.
367 * Roznowski <sjr@home.net>) for a drive with the "S61A"
370 * Although no one has reported problems with the 2 gig
371 * version of the DCAS drive, the assumption is that it
372 * has the same problems as the 4 gig version. Therefore
373 * this quirk entries disables tagged queueing for all
376 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
377 /*quirks*/0, /*mintags*/0, /*maxtags*/0
380 /* Broken tagged queuing drive */
381 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
382 /*quirks*/0, /*mintags*/0, /*maxtags*/0
385 /* Broken tagged queuing drive */
386 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
387 /*quirks*/0, /*mintags*/0, /*maxtags*/0
391 * Broken tagged queuing drive.
393 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
396 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
397 /*quirks*/0, /*mintags*/0, /*maxtags*/0
401 * Slow when tagged queueing is enabled. (1.5MB/sec versus
403 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
404 * Best performance with these drives is achieved with
405 * tagged queueing turned off, and write caching turned on.
407 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
408 /*quirks*/0, /*mintags*/0, /*maxtags*/0
412 * Slow when tagged queueing is enabled. (1.5MB/sec versus
414 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
415 * Best performance with these drives is achieved with
416 * tagged queueing turned off, and write caching turned on.
418 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
419 /*quirks*/0, /*mintags*/0, /*maxtags*/0
423 * Doesn't handle queue full condition correctly,
424 * so we need to limit maxtags to what the device
425 * can handle instead of determining this automatically.
427 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
428 /*quirks*/0, /*mintags*/2, /*maxtags*/32
431 /* Really only one LUN */
432 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
433 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
436 /* I can't believe we need a quirk for DPT volumes. */
437 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
438 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
439 /*mintags*/0, /*maxtags*/255
443 * Many Sony CDROM drives don't like multi-LUN probing.
445 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
446 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
450 * This drive doesn't like multiple LUN probing.
451 * Submitted by: Parag Patel <parag@cgt.com>
453 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
454 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
457 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
458 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
462 * The 8200 doesn't like multi-lun probing, and probably
463 * don't like serial number requests either.
466 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
469 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
473 * Let's try the same as above, but for a drive that says
474 * it's an IPL-6860 but is actually an EXB 8200.
477 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
480 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
484 * These Hitachi drives don't like multi-lun probing.
485 * The PR submitter has a DK319H, but says that the Linux
486 * kernel has a similar work-around for the DK312 and DK314,
487 * so all DK31* drives are quirked here.
489 * Submitted by: Paul Haddad <paul@pth.com>
491 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
492 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
496 * The Hitachi CJ series with J8A8 firmware apparantly has
497 * problems with tagged commands.
499 * Reported by: amagai@nue.org
501 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
502 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
506 * These are the large storage arrays.
507 * Submitted by: William Carrel <william.carrel@infospace.com>
509 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
510 CAM_QUIRK_HILUNS, 2, 1024
514 * This old revision of the TDC3600 is also SCSI-1, and
515 * hangs upon serial number probing.
518 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
521 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
525 * Maxtor Personal Storage 3000XT (Firewire)
526 * hangs upon serial number probing.
529 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
532 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
536 * Would repond to all LUNs if asked for.
539 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
542 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
546 * Would repond to all LUNs if asked for.
549 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
552 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
555 /* Submitted by: Matthew Dodd <winter@jurai.net> */
556 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
557 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
560 /* Submitted by: Matthew Dodd <winter@jurai.net> */
561 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
562 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
565 /* TeraSolutions special settings for TRC-22 RAID */
566 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
567 /*quirks*/0, /*mintags*/55, /*maxtags*/255
570 /* Veritas Storage Appliance */
571 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
572 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
576 * Would respond to all LUNs. Device type and removable
577 * flag are jumper-selectable.
579 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
582 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
585 /* EasyRAID E5A aka. areca ARC-6010 */
586 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
587 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
590 /* Default tagged queuing parameters for all devices */
592 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
593 /*vendor*/"*", /*product*/"*", /*revision*/"*"
595 /*quirks*/0, /*mintags*/2, /*maxtags*/255
599 static const int xpt_quirk_table_size =
600 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
604 DM_RET_FLAG_MASK = 0x0f,
607 DM_RET_DESCEND = 0x20,
609 DM_RET_ACTION_MASK = 0xf0
617 } xpt_traverse_depth;
619 struct xpt_traverse_config {
620 xpt_traverse_depth depth;
625 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
626 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
627 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
628 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
629 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
631 /* Transport layer configuration information */
632 static struct xpt_softc xsoftc;
634 /* Queues for our software interrupt handler */
635 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
636 static cam_isrq_t cam_bioq;
637 static struct mtx cam_bioq_lock;
639 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
640 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
641 static u_int xpt_max_ccbs; /*
642 * Maximum size of ccb pool. Modified as
643 * devices are added/removed or have their
644 * opening counts changed.
646 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
648 struct cam_periph *xpt_periph;
650 static periph_init_t xpt_periph_init;
652 static periph_init_t probe_periph_init;
654 static struct periph_driver xpt_driver =
656 xpt_periph_init, "xpt",
657 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
660 static struct periph_driver probe_driver =
662 probe_periph_init, "probe",
663 TAILQ_HEAD_INITIALIZER(probe_driver.units)
666 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
667 PERIPHDRIVER_DECLARE(probe, probe_driver);
670 static d_open_t xptopen;
671 static d_close_t xptclose;
672 static d_ioctl_t xptioctl;
674 static struct cdevsw xpt_cdevsw = {
675 .d_version = D_VERSION,
676 .d_flags = D_NEEDGIANT,
683 static struct intr_config_hook *xpt_config_hook;
685 /* Registered busses */
686 static TAILQ_HEAD(,cam_eb) xpt_busses;
687 static u_int bus_generation;
689 /* Storage for debugging datastructures */
691 struct cam_path *cam_dpath;
692 u_int32_t cam_dflags;
693 u_int32_t cam_debug_delay;
696 /* Pointers to software interrupt handlers */
697 static void *cambio_ih;
699 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
700 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
704 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
705 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
706 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
708 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
709 || defined(CAM_DEBUG_LUN)
711 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
712 || !defined(CAM_DEBUG_LUN)
713 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
715 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
716 #else /* !CAMDEBUG */
717 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
718 #endif /* CAMDEBUG */
719 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
721 /* Our boot-time initialization hook */
722 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
724 static moduledata_t cam_moduledata = {
726 cam_module_event_handler,
730 static void xpt_init(void *);
732 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
733 MODULE_VERSION(cam, 1);
736 static cam_status xpt_compile_path(struct cam_path *new_path,
737 struct cam_periph *perph,
739 target_id_t target_id,
742 static void xpt_release_path(struct cam_path *path);
744 static void xpt_async_bcast(struct async_list *async_head,
745 u_int32_t async_code,
746 struct cam_path *path,
748 static void xpt_dev_async(u_int32_t async_code,
750 struct cam_et *target,
751 struct cam_ed *device,
753 static path_id_t xptnextfreepathid(void);
754 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
755 static union ccb *xpt_get_ccb(struct cam_ed *device);
756 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
757 u_int32_t new_priority);
758 static void xpt_run_dev_allocq(struct cam_eb *bus);
759 static void xpt_run_dev_sendq(struct cam_eb *bus);
760 static timeout_t xpt_release_devq_timeout;
761 static timeout_t xpt_release_simq_timeout;
762 static void xpt_release_bus(struct cam_eb *bus);
763 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
765 static struct cam_et*
766 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
767 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
768 static struct cam_ed*
769 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
771 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
772 struct cam_ed *device);
773 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
774 static struct cam_eb*
775 xpt_find_bus(path_id_t path_id);
776 static struct cam_et*
777 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
778 static struct cam_ed*
779 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
780 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
781 static void xpt_scan_lun(struct cam_periph *periph,
782 struct cam_path *path, cam_flags flags,
784 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
785 static xpt_busfunc_t xptconfigbuscountfunc;
786 static xpt_busfunc_t xptconfigfunc;
787 static void xpt_config(void *arg);
788 static xpt_devicefunc_t xptpassannouncefunc;
789 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
790 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
791 static void xptpoll(struct cam_sim *sim);
792 static void camisr(void *);
794 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
795 static void xptasync(struct cam_periph *periph,
796 u_int32_t code, cam_path *path);
798 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
799 u_int num_patterns, struct cam_eb *bus);
800 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
802 struct cam_ed *device);
803 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
805 struct cam_periph *periph);
806 static xpt_busfunc_t xptedtbusfunc;
807 static xpt_targetfunc_t xptedttargetfunc;
808 static xpt_devicefunc_t xptedtdevicefunc;
809 static xpt_periphfunc_t xptedtperiphfunc;
810 static xpt_pdrvfunc_t xptplistpdrvfunc;
811 static xpt_periphfunc_t xptplistperiphfunc;
812 static int xptedtmatch(struct ccb_dev_match *cdm);
813 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
814 static int xptbustraverse(struct cam_eb *start_bus,
815 xpt_busfunc_t *tr_func, void *arg);
816 static int xpttargettraverse(struct cam_eb *bus,
817 struct cam_et *start_target,
818 xpt_targetfunc_t *tr_func, void *arg);
819 static int xptdevicetraverse(struct cam_et *target,
820 struct cam_ed *start_device,
821 xpt_devicefunc_t *tr_func, void *arg);
822 static int xptperiphtraverse(struct cam_ed *device,
823 struct cam_periph *start_periph,
824 xpt_periphfunc_t *tr_func, void *arg);
825 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
826 xpt_pdrvfunc_t *tr_func, void *arg);
827 static int xptpdperiphtraverse(struct periph_driver **pdrv,
828 struct cam_periph *start_periph,
829 xpt_periphfunc_t *tr_func,
831 static xpt_busfunc_t xptdefbusfunc;
832 static xpt_targetfunc_t xptdeftargetfunc;
833 static xpt_devicefunc_t xptdefdevicefunc;
834 static xpt_periphfunc_t xptdefperiphfunc;
835 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
837 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
840 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
843 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
846 static xpt_devicefunc_t xptsetasyncfunc;
847 static xpt_busfunc_t xptsetasyncbusfunc;
848 static cam_status xptregister(struct cam_periph *periph,
850 static cam_status proberegister(struct cam_periph *periph,
852 static void probeschedule(struct cam_periph *probe_periph);
853 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
854 static void proberequestdefaultnegotiation(struct cam_periph *periph);
855 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
856 static void probecleanup(struct cam_periph *periph);
857 static void xpt_find_quirk(struct cam_ed *device);
858 #ifdef CAM_NEW_TRAN_CODE
859 static void xpt_devise_transport(struct cam_path *path);
860 #endif /* CAM_NEW_TRAN_CODE */
861 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
862 struct cam_ed *device,
864 static void xpt_toggle_tags(struct cam_path *path);
865 static void xpt_start_tags(struct cam_path *path);
866 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
868 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
870 static __inline int periph_is_queued(struct cam_periph *periph);
871 static __inline int device_is_alloc_queued(struct cam_ed *device);
872 static __inline int device_is_send_queued(struct cam_ed *device);
873 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
876 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
880 if (dev->ccbq.devq_openings > 0) {
881 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
882 cam_ccbq_resize(&dev->ccbq,
883 dev->ccbq.dev_openings
884 + dev->ccbq.dev_active);
885 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
888 * The priority of a device waiting for CCB resources
889 * is that of the the highest priority peripheral driver
892 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
893 &dev->alloc_ccb_entry.pinfo,
894 CAMQ_GET_HEAD(&dev->drvq)->priority);
903 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
907 if (dev->ccbq.dev_openings > 0) {
909 * The priority of a device waiting for controller
910 * resources is that of the the highest priority CCB
914 xpt_schedule_dev(&bus->sim->devq->send_queue,
915 &dev->send_ccb_entry.pinfo,
916 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
924 periph_is_queued(struct cam_periph *periph)
926 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
930 device_is_alloc_queued(struct cam_ed *device)
932 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
936 device_is_send_queued(struct cam_ed *device)
938 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
942 dev_allocq_is_runnable(struct cam_devq *devq)
946 * Have space to do more work.
947 * Allowed to do work.
949 return ((devq->alloc_queue.qfrozen_cnt == 0)
950 && (devq->alloc_queue.entries > 0)
951 && (devq->alloc_openings > 0));
957 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
967 xptdone(struct cam_periph *periph, union ccb *done_ccb)
969 /* Caller will release the CCB */
970 wakeup(&done_ccb->ccb_h.cbfcnp);
974 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
978 unit = minor(dev) & 0xff;
981 * Only allow read-write access.
983 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
987 * We don't allow nonblocking access.
989 if ((flags & O_NONBLOCK) != 0) {
990 printf("xpt%d: can't do nonblocking access\n", unit);
995 * We only have one transport layer right now. If someone accesses
996 * us via something other than minor number 1, point out their
1000 printf("xptopen: got invalid xpt unit %d\n", unit);
1004 /* Mark ourselves open */
1005 xsoftc.flags |= XPT_FLAG_OPEN;
1011 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1015 unit = minor(dev) & 0xff;
1018 * We only have one transport layer right now. If someone accesses
1019 * us via something other than minor number 1, point out their
1023 printf("xptclose: got invalid xpt unit %d\n", unit);
1027 /* Mark ourselves closed */
1028 xsoftc.flags &= ~XPT_FLAG_OPEN;
1034 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1039 unit = minor(dev) & 0xff;
1042 * We only have one transport layer right now. If someone accesses
1043 * us via something other than minor number 1, point out their
1047 printf("xptioctl: got invalid xpt unit %d\n", unit);
1053 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1054 * to accept CCB types that don't quite make sense to send through a
1055 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1058 case CAMIOCOMMAND: {
1062 inccb = (union ccb *)addr;
1064 switch(inccb->ccb_h.func_code) {
1067 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1068 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1077 ccb = xpt_alloc_ccb();
1080 * Create a path using the bus, target, and lun the
1083 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1084 inccb->ccb_h.path_id,
1085 inccb->ccb_h.target_id,
1086 inccb->ccb_h.target_lun) !=
1092 /* Ensure all of our fields are correct */
1093 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1094 inccb->ccb_h.pinfo.priority);
1095 xpt_merge_ccb(ccb, inccb);
1096 ccb->ccb_h.cbfcnp = xptdone;
1097 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1098 bcopy(ccb, inccb, sizeof(union ccb));
1099 xpt_free_path(ccb->ccb_h.path);
1107 * This is an immediate CCB, so it's okay to
1108 * allocate it on the stack.
1112 * Create a path using the bus, target, and lun the
1115 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1116 inccb->ccb_h.path_id,
1117 inccb->ccb_h.target_id,
1118 inccb->ccb_h.target_lun) !=
1123 /* Ensure all of our fields are correct */
1124 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1125 inccb->ccb_h.pinfo.priority);
1126 xpt_merge_ccb(&ccb, inccb);
1127 ccb.ccb_h.cbfcnp = xptdone;
1129 bcopy(&ccb, inccb, sizeof(union ccb));
1130 xpt_free_path(ccb.ccb_h.path);
1134 case XPT_DEV_MATCH: {
1135 struct cam_periph_map_info mapinfo;
1136 struct cam_path *old_path;
1139 * We can't deal with physical addresses for this
1140 * type of transaction.
1142 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1148 * Save this in case the caller had it set to
1149 * something in particular.
1151 old_path = inccb->ccb_h.path;
1154 * We really don't need a path for the matching
1155 * code. The path is needed because of the
1156 * debugging statements in xpt_action(). They
1157 * assume that the CCB has a valid path.
1159 inccb->ccb_h.path = xpt_periph->path;
1161 bzero(&mapinfo, sizeof(mapinfo));
1164 * Map the pattern and match buffers into kernel
1165 * virtual address space.
1167 error = cam_periph_mapmem(inccb, &mapinfo);
1170 inccb->ccb_h.path = old_path;
1175 * This is an immediate CCB, we can send it on directly.
1180 * Map the buffers back into user space.
1182 cam_periph_unmapmem(inccb, &mapinfo);
1184 inccb->ccb_h.path = old_path;
1196 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1197 * with the periphal driver name and unit name filled in. The other
1198 * fields don't really matter as input. The passthrough driver name
1199 * ("pass"), and unit number are passed back in the ccb. The current
1200 * device generation number, and the index into the device peripheral
1201 * driver list, and the status are also passed back. Note that
1202 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1203 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1204 * (or rather should be) impossible for the device peripheral driver
1205 * list to change since we look at the whole thing in one pass, and
1206 * we do it with splcam protection.
1209 case CAMGETPASSTHRU: {
1211 struct cam_periph *periph;
1212 struct periph_driver **p_drv;
1215 u_int cur_generation;
1216 int base_periph_found;
1220 ccb = (union ccb *)addr;
1221 unit = ccb->cgdl.unit_number;
1222 name = ccb->cgdl.periph_name;
1224 * Every 100 devices, we want to drop our spl protection to
1225 * give the software interrupt handler a chance to run.
1226 * Most systems won't run into this check, but this should
1227 * avoid starvation in the software interrupt handler in
1232 ccb = (union ccb *)addr;
1234 base_periph_found = 0;
1237 * Sanity check -- make sure we don't get a null peripheral
1240 if (*ccb->cgdl.periph_name == '\0') {
1245 /* Keep the list from changing while we traverse it */
1248 cur_generation = xsoftc.generation;
1250 /* first find our driver in the list of drivers */
1251 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1252 if (strcmp((*p_drv)->driver_name, name) == 0)
1255 if (*p_drv == NULL) {
1257 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1258 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1259 *ccb->cgdl.periph_name = '\0';
1260 ccb->cgdl.unit_number = 0;
1266 * Run through every peripheral instance of this driver
1267 * and check to see whether it matches the unit passed
1268 * in by the user. If it does, get out of the loops and
1269 * find the passthrough driver associated with that
1270 * peripheral driver.
1272 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1273 periph = TAILQ_NEXT(periph, unit_links)) {
1275 if (periph->unit_number == unit) {
1277 } else if (--splbreaknum == 0) {
1281 if (cur_generation != xsoftc.generation)
1286 * If we found the peripheral driver that the user passed
1287 * in, go through all of the peripheral drivers for that
1288 * particular device and look for a passthrough driver.
1290 if (periph != NULL) {
1291 struct cam_ed *device;
1294 base_periph_found = 1;
1295 device = periph->path->device;
1296 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1298 periph = SLIST_NEXT(periph, periph_links), i++) {
1300 * Check to see whether we have a
1301 * passthrough device or not.
1303 if (strcmp(periph->periph_name, "pass") == 0) {
1305 * Fill in the getdevlist fields.
1307 strcpy(ccb->cgdl.periph_name,
1308 periph->periph_name);
1309 ccb->cgdl.unit_number =
1310 periph->unit_number;
1311 if (SLIST_NEXT(periph, periph_links))
1313 CAM_GDEVLIST_MORE_DEVS;
1316 CAM_GDEVLIST_LAST_DEVICE;
1317 ccb->cgdl.generation =
1319 ccb->cgdl.index = i;
1321 * Fill in some CCB header fields
1322 * that the user may want.
1324 ccb->ccb_h.path_id =
1325 periph->path->bus->path_id;
1326 ccb->ccb_h.target_id =
1327 periph->path->target->target_id;
1328 ccb->ccb_h.target_lun =
1329 periph->path->device->lun_id;
1330 ccb->ccb_h.status = CAM_REQ_CMP;
1337 * If the periph is null here, one of two things has
1338 * happened. The first possibility is that we couldn't
1339 * find the unit number of the particular peripheral driver
1340 * that the user is asking about. e.g. the user asks for
1341 * the passthrough driver for "da11". We find the list of
1342 * "da" peripherals all right, but there is no unit 11.
1343 * The other possibility is that we went through the list
1344 * of peripheral drivers attached to the device structure,
1345 * but didn't find one with the name "pass". Either way,
1346 * we return ENOENT, since we couldn't find something.
1348 if (periph == NULL) {
1349 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1350 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1351 *ccb->cgdl.periph_name = '\0';
1352 ccb->cgdl.unit_number = 0;
1355 * It is unfortunate that this is even necessary,
1356 * but there are many, many clueless users out there.
1357 * If this is true, the user is looking for the
1358 * passthrough driver, but doesn't have one in his
1361 if (base_periph_found == 1) {
1362 printf("xptioctl: pass driver is not in the "
1364 printf("xptioctl: put \"device pass0\" in "
1365 "your kernel config file\n");
1380 cam_module_event_handler(module_t mod, int what, void *arg)
1382 if (what == MOD_LOAD) {
1384 } else if (what == MOD_UNLOAD) {
1393 /* Functions accessed by the peripheral drivers */
1398 struct cam_sim *xpt_sim;
1399 struct cam_path *path;
1400 struct cam_devq *devq;
1403 TAILQ_INIT(&xpt_busses);
1404 TAILQ_INIT(&cam_bioq);
1405 SLIST_INIT(&ccb_freeq);
1406 STAILQ_INIT(&highpowerq);
1408 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1411 * The xpt layer is, itself, the equivelent of a SIM.
1412 * Allow 16 ccbs in the ccb pool for it. This should
1413 * give decent parallelism when we probe busses and
1414 * perform other XPT functions.
1416 devq = cam_simq_alloc(16);
1417 xpt_sim = cam_sim_alloc(xptaction,
1422 /*max_dev_transactions*/0,
1423 /*max_tagged_dev_transactions*/0,
1427 xpt_bus_register(xpt_sim, /*bus #*/0);
1430 * Looking at the XPT from the SIM layer, the XPT is
1431 * the equivelent of a peripheral driver. Allocate
1432 * a peripheral driver entry for us.
1434 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1435 CAM_TARGET_WILDCARD,
1436 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1437 printf("xpt_init: xpt_create_path failed with status %#x,"
1438 " failing attach\n", status);
1442 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1443 path, NULL, 0, NULL);
1444 xpt_free_path(path);
1446 xpt_sim->softc = xpt_periph;
1449 * Register a callback for when interrupts are enabled.
1452 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1453 M_TEMP, M_NOWAIT | M_ZERO);
1454 if (xpt_config_hook == NULL) {
1455 printf("xpt_init: Cannot malloc config hook "
1456 "- failing attach\n");
1460 xpt_config_hook->ich_func = xpt_config;
1461 if (config_intrhook_establish(xpt_config_hook) != 0) {
1462 free (xpt_config_hook, M_TEMP);
1463 printf("xpt_init: config_intrhook_establish failed "
1464 "- failing attach\n");
1467 /* Install our software interrupt handlers */
1468 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1472 xptregister(struct cam_periph *periph, void *arg)
1474 if (periph == NULL) {
1475 printf("xptregister: periph was NULL!!\n");
1476 return(CAM_REQ_CMP_ERR);
1479 periph->softc = NULL;
1481 xpt_periph = periph;
1483 return(CAM_REQ_CMP);
1487 xpt_add_periph(struct cam_periph *periph)
1489 struct cam_ed *device;
1491 struct periph_list *periph_head;
1495 device = periph->path->device;
1497 periph_head = &device->periphs;
1499 status = CAM_REQ_CMP;
1501 if (device != NULL) {
1505 * Make room for this peripheral
1506 * so it will fit in the queue
1507 * when it's scheduled to run
1510 status = camq_resize(&device->drvq,
1511 device->drvq.array_size + 1);
1513 device->generation++;
1515 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1520 xsoftc.generation++;
1526 xpt_remove_periph(struct cam_periph *periph)
1528 struct cam_ed *device;
1532 device = periph->path->device;
1534 if (device != NULL) {
1536 struct periph_list *periph_head;
1538 periph_head = &device->periphs;
1540 /* Release the slot for this peripheral */
1542 camq_resize(&device->drvq, device->drvq.array_size - 1);
1544 device->generation++;
1546 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1551 xsoftc.generation++;
1555 #ifdef CAM_NEW_TRAN_CODE
1558 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1560 struct ccb_pathinq cpi;
1561 struct ccb_trans_settings cts;
1562 struct cam_path *path;
1570 path = periph->path;
1572 * To ensure that this is printed in one piece,
1573 * mask out CAM interrupts.
1576 printf("%s%d at %s%d bus %d target %d lun %d\n",
1577 periph->periph_name, periph->unit_number,
1578 path->bus->sim->sim_name,
1579 path->bus->sim->unit_number,
1580 path->bus->sim->bus_id,
1581 path->target->target_id,
1582 path->device->lun_id);
1583 printf("%s%d: ", periph->periph_name, periph->unit_number);
1584 scsi_print_inquiry(&path->device->inq_data);
1585 if (bootverbose && path->device->serial_num_len > 0) {
1586 /* Don't wrap the screen - print only the first 60 chars */
1587 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1588 periph->unit_number, path->device->serial_num);
1590 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1591 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1592 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1593 xpt_action((union ccb*)&cts);
1595 /* Ask the SIM for its base transfer speed */
1596 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1597 cpi.ccb_h.func_code = XPT_PATH_INQ;
1598 xpt_action((union ccb *)&cpi);
1600 speed = cpi.base_transfer_speed;
1602 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1603 struct ccb_trans_settings_spi *spi;
1605 spi = &cts.xport_specific.spi;
1606 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1607 && spi->sync_offset != 0) {
1608 freq = scsi_calc_syncsrate(spi->sync_period);
1612 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1613 speed *= (0x01 << spi->bus_width);
1616 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1617 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1618 if (fc->valid & CTS_FC_VALID_SPEED) {
1619 speed = fc->bitrate;
1625 printf("%s%d: %d.%03dMB/s transfers",
1626 periph->periph_name, periph->unit_number,
1629 printf("%s%d: %dKB/s transfers", periph->periph_name,
1630 periph->unit_number, speed);
1631 /* Report additional information about SPI connections */
1632 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1633 struct ccb_trans_settings_spi *spi;
1635 spi = &cts.xport_specific.spi;
1637 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1639 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1643 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1644 && spi->bus_width > 0) {
1650 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1651 } else if (freq != 0) {
1655 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1656 struct ccb_trans_settings_fc *fc;
1658 fc = &cts.xport_specific.fc;
1659 if (fc->valid & CTS_FC_VALID_WWNN)
1660 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1661 if (fc->valid & CTS_FC_VALID_WWPN)
1662 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1663 if (fc->valid & CTS_FC_VALID_PORT)
1664 printf(" PortID 0x%x", fc->port);
1667 if (path->device->inq_flags & SID_CmdQue
1668 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1669 printf("\n%s%d: Tagged Queueing Enabled",
1670 periph->periph_name, periph->unit_number);
1675 * We only want to print the caller's announce string if they've
1678 if (announce_string != NULL)
1679 printf("%s%d: %s\n", periph->periph_name,
1680 periph->unit_number, announce_string);
1683 #else /* CAM_NEW_TRAN_CODE */
1685 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1689 struct cam_path *path;
1690 struct ccb_trans_settings cts;
1694 path = periph->path;
1696 * To ensure that this is printed in one piece,
1697 * mask out CAM interrupts.
1700 printf("%s%d at %s%d bus %d target %d lun %d\n",
1701 periph->periph_name, periph->unit_number,
1702 path->bus->sim->sim_name,
1703 path->bus->sim->unit_number,
1704 path->bus->sim->bus_id,
1705 path->target->target_id,
1706 path->device->lun_id);
1707 printf("%s%d: ", periph->periph_name, periph->unit_number);
1708 scsi_print_inquiry(&path->device->inq_data);
1710 && (path->device->serial_num_len > 0)) {
1711 /* Don't wrap the screen - print only the first 60 chars */
1712 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1713 periph->unit_number, path->device->serial_num);
1715 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1716 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1717 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1718 xpt_action((union ccb*)&cts);
1719 if (cts.ccb_h.status == CAM_REQ_CMP) {
1723 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1724 && cts.sync_offset != 0) {
1725 freq = scsi_calc_syncsrate(cts.sync_period);
1728 struct ccb_pathinq cpi;
1730 /* Ask the SIM for its base transfer speed */
1731 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1732 cpi.ccb_h.func_code = XPT_PATH_INQ;
1733 xpt_action((union ccb *)&cpi);
1735 speed = cpi.base_transfer_speed;
1738 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1739 speed *= (0x01 << cts.bus_width);
1742 printf("%s%d: %d.%03dMB/s transfers",
1743 periph->periph_name, periph->unit_number,
1746 printf("%s%d: %dKB/s transfers", periph->periph_name,
1747 periph->unit_number, speed);
1748 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1749 && cts.sync_offset != 0) {
1750 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1751 freq % 1000, cts.sync_offset);
1753 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1754 && cts.bus_width > 0) {
1755 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1756 && cts.sync_offset != 0) {
1761 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1762 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1763 && cts.sync_offset != 0) {
1767 if (path->device->inq_flags & SID_CmdQue
1768 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1769 printf(", Tagged Queueing Enabled");
1773 } else if (path->device->inq_flags & SID_CmdQue
1774 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1775 printf("%s%d: Tagged Queueing Enabled\n",
1776 periph->periph_name, periph->unit_number);
1780 * We only want to print the caller's announce string if they've
1783 if (announce_string != NULL)
1784 printf("%s%d: %s\n", periph->periph_name,
1785 periph->unit_number, announce_string);
1789 #endif /* CAM_NEW_TRAN_CODE */
1791 static dev_match_ret
1792 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1795 dev_match_ret retval;
1798 retval = DM_RET_NONE;
1801 * If we aren't given something to match against, that's an error.
1804 return(DM_RET_ERROR);
1807 * If there are no match entries, then this bus matches no
1810 if ((patterns == NULL) || (num_patterns == 0))
1811 return(DM_RET_DESCEND | DM_RET_COPY);
1813 for (i = 0; i < num_patterns; i++) {
1814 struct bus_match_pattern *cur_pattern;
1817 * If the pattern in question isn't for a bus node, we
1818 * aren't interested. However, we do indicate to the
1819 * calling routine that we should continue descending the
1820 * tree, since the user wants to match against lower-level
1823 if (patterns[i].type != DEV_MATCH_BUS) {
1824 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1825 retval |= DM_RET_DESCEND;
1829 cur_pattern = &patterns[i].pattern.bus_pattern;
1832 * If they want to match any bus node, we give them any
1835 if (cur_pattern->flags == BUS_MATCH_ANY) {
1836 /* set the copy flag */
1837 retval |= DM_RET_COPY;
1840 * If we've already decided on an action, go ahead
1843 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1848 * Not sure why someone would do this...
1850 if (cur_pattern->flags == BUS_MATCH_NONE)
1853 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1854 && (cur_pattern->path_id != bus->path_id))
1857 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1858 && (cur_pattern->bus_id != bus->sim->bus_id))
1861 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1862 && (cur_pattern->unit_number != bus->sim->unit_number))
1865 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1866 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1871 * If we get to this point, the user definitely wants
1872 * information on this bus. So tell the caller to copy the
1875 retval |= DM_RET_COPY;
1878 * If the return action has been set to descend, then we
1879 * know that we've already seen a non-bus matching
1880 * expression, therefore we need to further descend the tree.
1881 * This won't change by continuing around the loop, so we
1882 * go ahead and return. If we haven't seen a non-bus
1883 * matching expression, we keep going around the loop until
1884 * we exhaust the matching expressions. We'll set the stop
1885 * flag once we fall out of the loop.
1887 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1892 * If the return action hasn't been set to descend yet, that means
1893 * we haven't seen anything other than bus matching patterns. So
1894 * tell the caller to stop descending the tree -- the user doesn't
1895 * want to match against lower level tree elements.
1897 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1898 retval |= DM_RET_STOP;
1903 static dev_match_ret
1904 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1905 struct cam_ed *device)
1907 dev_match_ret retval;
1910 retval = DM_RET_NONE;
1913 * If we aren't given something to match against, that's an error.
1916 return(DM_RET_ERROR);
1919 * If there are no match entries, then this device matches no
1922 if ((patterns == NULL) || (num_patterns == 0))
1923 return(DM_RET_DESCEND | DM_RET_COPY);
1925 for (i = 0; i < num_patterns; i++) {
1926 struct device_match_pattern *cur_pattern;
1929 * If the pattern in question isn't for a device node, we
1930 * aren't interested.
1932 if (patterns[i].type != DEV_MATCH_DEVICE) {
1933 if ((patterns[i].type == DEV_MATCH_PERIPH)
1934 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1935 retval |= DM_RET_DESCEND;
1939 cur_pattern = &patterns[i].pattern.device_pattern;
1942 * If they want to match any device node, we give them any
1945 if (cur_pattern->flags == DEV_MATCH_ANY) {
1946 /* set the copy flag */
1947 retval |= DM_RET_COPY;
1951 * If we've already decided on an action, go ahead
1954 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1959 * Not sure why someone would do this...
1961 if (cur_pattern->flags == DEV_MATCH_NONE)
1964 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1965 && (cur_pattern->path_id != device->target->bus->path_id))
1968 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1969 && (cur_pattern->target_id != device->target->target_id))
1972 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1973 && (cur_pattern->target_lun != device->lun_id))
1976 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1977 && (cam_quirkmatch((caddr_t)&device->inq_data,
1978 (caddr_t)&cur_pattern->inq_pat,
1979 1, sizeof(cur_pattern->inq_pat),
1980 scsi_static_inquiry_match) == NULL))
1984 * If we get to this point, the user definitely wants
1985 * information on this device. So tell the caller to copy
1988 retval |= DM_RET_COPY;
1991 * If the return action has been set to descend, then we
1992 * know that we've already seen a peripheral matching
1993 * expression, therefore we need to further descend the tree.
1994 * This won't change by continuing around the loop, so we
1995 * go ahead and return. If we haven't seen a peripheral
1996 * matching expression, we keep going around the loop until
1997 * we exhaust the matching expressions. We'll set the stop
1998 * flag once we fall out of the loop.
2000 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
2005 * If the return action hasn't been set to descend yet, that means
2006 * we haven't seen any peripheral matching patterns. So tell the
2007 * caller to stop descending the tree -- the user doesn't want to
2008 * match against lower level tree elements.
2010 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
2011 retval |= DM_RET_STOP;
2017 * Match a single peripheral against any number of match patterns.
2019 static dev_match_ret
2020 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
2021 struct cam_periph *periph)
2023 dev_match_ret retval;
2027 * If we aren't given something to match against, that's an error.
2030 return(DM_RET_ERROR);
2033 * If there are no match entries, then this peripheral matches no
2036 if ((patterns == NULL) || (num_patterns == 0))
2037 return(DM_RET_STOP | DM_RET_COPY);
2040 * There aren't any nodes below a peripheral node, so there's no
2041 * reason to descend the tree any further.
2043 retval = DM_RET_STOP;
2045 for (i = 0; i < num_patterns; i++) {
2046 struct periph_match_pattern *cur_pattern;
2049 * If the pattern in question isn't for a peripheral, we
2050 * aren't interested.
2052 if (patterns[i].type != DEV_MATCH_PERIPH)
2055 cur_pattern = &patterns[i].pattern.periph_pattern;
2058 * If they want to match on anything, then we will do so.
2060 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2061 /* set the copy flag */
2062 retval |= DM_RET_COPY;
2065 * We've already set the return action to stop,
2066 * since there are no nodes below peripherals in
2073 * Not sure why someone would do this...
2075 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2078 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2079 && (cur_pattern->path_id != periph->path->bus->path_id))
2083 * For the target and lun id's, we have to make sure the
2084 * target and lun pointers aren't NULL. The xpt peripheral
2085 * has a wildcard target and device.
2087 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2088 && ((periph->path->target == NULL)
2089 ||(cur_pattern->target_id != periph->path->target->target_id)))
2092 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2093 && ((periph->path->device == NULL)
2094 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2097 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2098 && (cur_pattern->unit_number != periph->unit_number))
2101 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2102 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2107 * If we get to this point, the user definitely wants
2108 * information on this peripheral. So tell the caller to
2109 * copy the data out.
2111 retval |= DM_RET_COPY;
2114 * The return action has already been set to stop, since
2115 * peripherals don't have any nodes below them in the EDT.
2121 * If we get to this point, the peripheral that was passed in
2122 * doesn't match any of the patterns.
2128 xptedtbusfunc(struct cam_eb *bus, void *arg)
2130 struct ccb_dev_match *cdm;
2131 dev_match_ret retval;
2133 cdm = (struct ccb_dev_match *)arg;
2136 * If our position is for something deeper in the tree, that means
2137 * that we've already seen this node. So, we keep going down.
2139 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2140 && (cdm->pos.cookie.bus == bus)
2141 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2142 && (cdm->pos.cookie.target != NULL))
2143 retval = DM_RET_DESCEND;
2145 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2148 * If we got an error, bail out of the search.
2150 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2151 cdm->status = CAM_DEV_MATCH_ERROR;
2156 * If the copy flag is set, copy this bus out.
2158 if (retval & DM_RET_COPY) {
2161 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2162 sizeof(struct dev_match_result));
2165 * If we don't have enough space to put in another
2166 * match result, save our position and tell the
2167 * user there are more devices to check.
2169 if (spaceleft < sizeof(struct dev_match_result)) {
2170 bzero(&cdm->pos, sizeof(cdm->pos));
2171 cdm->pos.position_type =
2172 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2174 cdm->pos.cookie.bus = bus;
2175 cdm->pos.generations[CAM_BUS_GENERATION]=
2177 cdm->status = CAM_DEV_MATCH_MORE;
2180 j = cdm->num_matches;
2182 cdm->matches[j].type = DEV_MATCH_BUS;
2183 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2184 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2185 cdm->matches[j].result.bus_result.unit_number =
2186 bus->sim->unit_number;
2187 strncpy(cdm->matches[j].result.bus_result.dev_name,
2188 bus->sim->sim_name, DEV_IDLEN);
2192 * If the user is only interested in busses, there's no
2193 * reason to descend to the next level in the tree.
2195 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2199 * If there is a target generation recorded, check it to
2200 * make sure the target list hasn't changed.
2202 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2203 && (bus == cdm->pos.cookie.bus)
2204 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2205 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2206 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2208 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2212 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2213 && (cdm->pos.cookie.bus == bus)
2214 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2215 && (cdm->pos.cookie.target != NULL))
2216 return(xpttargettraverse(bus,
2217 (struct cam_et *)cdm->pos.cookie.target,
2218 xptedttargetfunc, arg));
2220 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2224 xptedttargetfunc(struct cam_et *target, void *arg)
2226 struct ccb_dev_match *cdm;
2228 cdm = (struct ccb_dev_match *)arg;
2231 * If there is a device list generation recorded, check it to
2232 * make sure the device list hasn't changed.
2234 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2235 && (cdm->pos.cookie.bus == target->bus)
2236 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2237 && (cdm->pos.cookie.target == target)
2238 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2239 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2240 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2241 target->generation)) {
2242 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2246 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2247 && (cdm->pos.cookie.bus == target->bus)
2248 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2249 && (cdm->pos.cookie.target == target)
2250 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2251 && (cdm->pos.cookie.device != NULL))
2252 return(xptdevicetraverse(target,
2253 (struct cam_ed *)cdm->pos.cookie.device,
2254 xptedtdevicefunc, arg));
2256 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2260 xptedtdevicefunc(struct cam_ed *device, void *arg)
2263 struct ccb_dev_match *cdm;
2264 dev_match_ret retval;
2266 cdm = (struct ccb_dev_match *)arg;
2269 * If our position is for something deeper in the tree, that means
2270 * that we've already seen this node. So, we keep going down.
2272 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2273 && (cdm->pos.cookie.device == device)
2274 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2275 && (cdm->pos.cookie.periph != NULL))
2276 retval = DM_RET_DESCEND;
2278 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2281 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2282 cdm->status = CAM_DEV_MATCH_ERROR;
2287 * If the copy flag is set, copy this device out.
2289 if (retval & DM_RET_COPY) {
2292 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2293 sizeof(struct dev_match_result));
2296 * If we don't have enough space to put in another
2297 * match result, save our position and tell the
2298 * user there are more devices to check.
2300 if (spaceleft < sizeof(struct dev_match_result)) {
2301 bzero(&cdm->pos, sizeof(cdm->pos));
2302 cdm->pos.position_type =
2303 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2304 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2306 cdm->pos.cookie.bus = device->target->bus;
2307 cdm->pos.generations[CAM_BUS_GENERATION]=
2309 cdm->pos.cookie.target = device->target;
2310 cdm->pos.generations[CAM_TARGET_GENERATION] =
2311 device->target->bus->generation;
2312 cdm->pos.cookie.device = device;
2313 cdm->pos.generations[CAM_DEV_GENERATION] =
2314 device->target->generation;
2315 cdm->status = CAM_DEV_MATCH_MORE;
2318 j = cdm->num_matches;
2320 cdm->matches[j].type = DEV_MATCH_DEVICE;
2321 cdm->matches[j].result.device_result.path_id =
2322 device->target->bus->path_id;
2323 cdm->matches[j].result.device_result.target_id =
2324 device->target->target_id;
2325 cdm->matches[j].result.device_result.target_lun =
2327 bcopy(&device->inq_data,
2328 &cdm->matches[j].result.device_result.inq_data,
2329 sizeof(struct scsi_inquiry_data));
2331 /* Let the user know whether this device is unconfigured */
2332 if (device->flags & CAM_DEV_UNCONFIGURED)
2333 cdm->matches[j].result.device_result.flags =
2334 DEV_RESULT_UNCONFIGURED;
2336 cdm->matches[j].result.device_result.flags =
2341 * If the user isn't interested in peripherals, don't descend
2342 * the tree any further.
2344 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2348 * If there is a peripheral list generation recorded, make sure
2349 * it hasn't changed.
2351 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2352 && (device->target->bus == cdm->pos.cookie.bus)
2353 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2354 && (device->target == cdm->pos.cookie.target)
2355 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2356 && (device == cdm->pos.cookie.device)
2357 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2358 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2359 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2360 device->generation)){
2361 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2365 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2366 && (cdm->pos.cookie.bus == device->target->bus)
2367 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2368 && (cdm->pos.cookie.target == device->target)
2369 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2370 && (cdm->pos.cookie.device == device)
2371 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2372 && (cdm->pos.cookie.periph != NULL))
2373 return(xptperiphtraverse(device,
2374 (struct cam_periph *)cdm->pos.cookie.periph,
2375 xptedtperiphfunc, arg));
2377 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2381 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2383 struct ccb_dev_match *cdm;
2384 dev_match_ret retval;
2386 cdm = (struct ccb_dev_match *)arg;
2388 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2390 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2391 cdm->status = CAM_DEV_MATCH_ERROR;
2396 * If the copy flag is set, copy this peripheral out.
2398 if (retval & DM_RET_COPY) {
2401 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2402 sizeof(struct dev_match_result));
2405 * If we don't have enough space to put in another
2406 * match result, save our position and tell the
2407 * user there are more devices to check.
2409 if (spaceleft < sizeof(struct dev_match_result)) {
2410 bzero(&cdm->pos, sizeof(cdm->pos));
2411 cdm->pos.position_type =
2412 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2413 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2416 cdm->pos.cookie.bus = periph->path->bus;
2417 cdm->pos.generations[CAM_BUS_GENERATION]=
2419 cdm->pos.cookie.target = periph->path->target;
2420 cdm->pos.generations[CAM_TARGET_GENERATION] =
2421 periph->path->bus->generation;
2422 cdm->pos.cookie.device = periph->path->device;
2423 cdm->pos.generations[CAM_DEV_GENERATION] =
2424 periph->path->target->generation;
2425 cdm->pos.cookie.periph = periph;
2426 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2427 periph->path->device->generation;
2428 cdm->status = CAM_DEV_MATCH_MORE;
2432 j = cdm->num_matches;
2434 cdm->matches[j].type = DEV_MATCH_PERIPH;
2435 cdm->matches[j].result.periph_result.path_id =
2436 periph->path->bus->path_id;
2437 cdm->matches[j].result.periph_result.target_id =
2438 periph->path->target->target_id;
2439 cdm->matches[j].result.periph_result.target_lun =
2440 periph->path->device->lun_id;
2441 cdm->matches[j].result.periph_result.unit_number =
2442 periph->unit_number;
2443 strncpy(cdm->matches[j].result.periph_result.periph_name,
2444 periph->periph_name, DEV_IDLEN);
2451 xptedtmatch(struct ccb_dev_match *cdm)
2455 cdm->num_matches = 0;
2458 * Check the bus list generation. If it has changed, the user
2459 * needs to reset everything and start over.
2461 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2462 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2463 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2464 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2468 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2469 && (cdm->pos.cookie.bus != NULL))
2470 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2471 xptedtbusfunc, cdm);
2473 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2476 * If we get back 0, that means that we had to stop before fully
2477 * traversing the EDT. It also means that one of the subroutines
2478 * has set the status field to the proper value. If we get back 1,
2479 * we've fully traversed the EDT and copied out any matching entries.
2482 cdm->status = CAM_DEV_MATCH_LAST;
2488 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2490 struct ccb_dev_match *cdm;
2492 cdm = (struct ccb_dev_match *)arg;
2494 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2495 && (cdm->pos.cookie.pdrv == pdrv)
2496 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2497 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2498 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2499 (*pdrv)->generation)) {
2500 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2504 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2505 && (cdm->pos.cookie.pdrv == pdrv)
2506 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2507 && (cdm->pos.cookie.periph != NULL))
2508 return(xptpdperiphtraverse(pdrv,
2509 (struct cam_periph *)cdm->pos.cookie.periph,
2510 xptplistperiphfunc, arg));
2512 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2516 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2518 struct ccb_dev_match *cdm;
2519 dev_match_ret retval;
2521 cdm = (struct ccb_dev_match *)arg;
2523 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2525 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2526 cdm->status = CAM_DEV_MATCH_ERROR;
2531 * If the copy flag is set, copy this peripheral out.
2533 if (retval & DM_RET_COPY) {
2536 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2537 sizeof(struct dev_match_result));
2540 * If we don't have enough space to put in another
2541 * match result, save our position and tell the
2542 * user there are more devices to check.
2544 if (spaceleft < sizeof(struct dev_match_result)) {
2545 struct periph_driver **pdrv;
2548 bzero(&cdm->pos, sizeof(cdm->pos));
2549 cdm->pos.position_type =
2550 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2554 * This may look a bit non-sensical, but it is
2555 * actually quite logical. There are very few
2556 * peripheral drivers, and bloating every peripheral
2557 * structure with a pointer back to its parent
2558 * peripheral driver linker set entry would cost
2559 * more in the long run than doing this quick lookup.
2561 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2562 if (strcmp((*pdrv)->driver_name,
2563 periph->periph_name) == 0)
2567 if (*pdrv == NULL) {
2568 cdm->status = CAM_DEV_MATCH_ERROR;
2572 cdm->pos.cookie.pdrv = pdrv;
2574 * The periph generation slot does double duty, as
2575 * does the periph pointer slot. They are used for
2576 * both edt and pdrv lookups and positioning.
2578 cdm->pos.cookie.periph = periph;
2579 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2580 (*pdrv)->generation;
2581 cdm->status = CAM_DEV_MATCH_MORE;
2585 j = cdm->num_matches;
2587 cdm->matches[j].type = DEV_MATCH_PERIPH;
2588 cdm->matches[j].result.periph_result.path_id =
2589 periph->path->bus->path_id;
2592 * The transport layer peripheral doesn't have a target or
2595 if (periph->path->target)
2596 cdm->matches[j].result.periph_result.target_id =
2597 periph->path->target->target_id;
2599 cdm->matches[j].result.periph_result.target_id = -1;
2601 if (periph->path->device)
2602 cdm->matches[j].result.periph_result.target_lun =
2603 periph->path->device->lun_id;
2605 cdm->matches[j].result.periph_result.target_lun = -1;
2607 cdm->matches[j].result.periph_result.unit_number =
2608 periph->unit_number;
2609 strncpy(cdm->matches[j].result.periph_result.periph_name,
2610 periph->periph_name, DEV_IDLEN);
2617 xptperiphlistmatch(struct ccb_dev_match *cdm)
2621 cdm->num_matches = 0;
2624 * At this point in the edt traversal function, we check the bus
2625 * list generation to make sure that no busses have been added or
2626 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2627 * For the peripheral driver list traversal function, however, we
2628 * don't have to worry about new peripheral driver types coming or
2629 * going; they're in a linker set, and therefore can't change
2630 * without a recompile.
2633 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2634 && (cdm->pos.cookie.pdrv != NULL))
2635 ret = xptpdrvtraverse(
2636 (struct periph_driver **)cdm->pos.cookie.pdrv,
2637 xptplistpdrvfunc, cdm);
2639 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2642 * If we get back 0, that means that we had to stop before fully
2643 * traversing the peripheral driver tree. It also means that one of
2644 * the subroutines has set the status field to the proper value. If
2645 * we get back 1, we've fully traversed the EDT and copied out any
2649 cdm->status = CAM_DEV_MATCH_LAST;
2655 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2657 struct cam_eb *bus, *next_bus;
2662 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2665 next_bus = TAILQ_NEXT(bus, links);
2667 retval = tr_func(bus, arg);
2676 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2677 xpt_targetfunc_t *tr_func, void *arg)
2679 struct cam_et *target, *next_target;
2683 for (target = (start_target ? start_target :
2684 TAILQ_FIRST(&bus->et_entries));
2685 target != NULL; target = next_target) {
2687 next_target = TAILQ_NEXT(target, links);
2689 retval = tr_func(target, arg);
2699 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2700 xpt_devicefunc_t *tr_func, void *arg)
2702 struct cam_ed *device, *next_device;
2706 for (device = (start_device ? start_device :
2707 TAILQ_FIRST(&target->ed_entries));
2709 device = next_device) {
2711 next_device = TAILQ_NEXT(device, links);
2713 retval = tr_func(device, arg);
2723 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2724 xpt_periphfunc_t *tr_func, void *arg)
2726 struct cam_periph *periph, *next_periph;
2731 for (periph = (start_periph ? start_periph :
2732 SLIST_FIRST(&device->periphs));
2734 periph = next_periph) {
2736 next_periph = SLIST_NEXT(periph, periph_links);
2738 retval = tr_func(periph, arg);
2747 xptpdrvtraverse(struct periph_driver **start_pdrv,
2748 xpt_pdrvfunc_t *tr_func, void *arg)
2750 struct periph_driver **pdrv;
2756 * We don't traverse the peripheral driver list like we do the
2757 * other lists, because it is a linker set, and therefore cannot be
2758 * changed during runtime. If the peripheral driver list is ever
2759 * re-done to be something other than a linker set (i.e. it can
2760 * change while the system is running), the list traversal should
2761 * be modified to work like the other traversal functions.
2763 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2764 *pdrv != NULL; pdrv++) {
2765 retval = tr_func(pdrv, arg);
2775 xptpdperiphtraverse(struct periph_driver **pdrv,
2776 struct cam_periph *start_periph,
2777 xpt_periphfunc_t *tr_func, void *arg)
2779 struct cam_periph *periph, *next_periph;
2784 for (periph = (start_periph ? start_periph :
2785 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2786 periph = next_periph) {
2788 next_periph = TAILQ_NEXT(periph, unit_links);
2790 retval = tr_func(periph, arg);
2798 xptdefbusfunc(struct cam_eb *bus, void *arg)
2800 struct xpt_traverse_config *tr_config;
2802 tr_config = (struct xpt_traverse_config *)arg;
2804 if (tr_config->depth == XPT_DEPTH_BUS) {
2805 xpt_busfunc_t *tr_func;
2807 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2809 return(tr_func(bus, tr_config->tr_arg));
2811 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2815 xptdeftargetfunc(struct cam_et *target, void *arg)
2817 struct xpt_traverse_config *tr_config;
2819 tr_config = (struct xpt_traverse_config *)arg;
2821 if (tr_config->depth == XPT_DEPTH_TARGET) {
2822 xpt_targetfunc_t *tr_func;
2824 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2826 return(tr_func(target, tr_config->tr_arg));
2828 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2832 xptdefdevicefunc(struct cam_ed *device, void *arg)
2834 struct xpt_traverse_config *tr_config;
2836 tr_config = (struct xpt_traverse_config *)arg;
2838 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2839 xpt_devicefunc_t *tr_func;
2841 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2843 return(tr_func(device, tr_config->tr_arg));
2845 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2849 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2851 struct xpt_traverse_config *tr_config;
2852 xpt_periphfunc_t *tr_func;
2854 tr_config = (struct xpt_traverse_config *)arg;
2856 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2859 * Unlike the other default functions, we don't check for depth
2860 * here. The peripheral driver level is the last level in the EDT,
2861 * so if we're here, we should execute the function in question.
2863 return(tr_func(periph, tr_config->tr_arg));
2867 * Execute the given function for every bus in the EDT.
2870 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2872 struct xpt_traverse_config tr_config;
2874 tr_config.depth = XPT_DEPTH_BUS;
2875 tr_config.tr_func = tr_func;
2876 tr_config.tr_arg = arg;
2878 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2883 * Execute the given function for every target in the EDT.
2886 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2888 struct xpt_traverse_config tr_config;
2890 tr_config.depth = XPT_DEPTH_TARGET;
2891 tr_config.tr_func = tr_func;
2892 tr_config.tr_arg = arg;
2894 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2896 #endif /* notusedyet */
2899 * Execute the given function for every device in the EDT.
2902 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2904 struct xpt_traverse_config tr_config;
2906 tr_config.depth = XPT_DEPTH_DEVICE;
2907 tr_config.tr_func = tr_func;
2908 tr_config.tr_arg = arg;
2910 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2915 * Execute the given function for every peripheral in the EDT.
2918 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2920 struct xpt_traverse_config tr_config;
2922 tr_config.depth = XPT_DEPTH_PERIPH;
2923 tr_config.tr_func = tr_func;
2924 tr_config.tr_arg = arg;
2926 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2928 #endif /* notusedyet */
2931 xptsetasyncfunc(struct cam_ed *device, void *arg)
2933 struct cam_path path;
2934 struct ccb_getdev cgd;
2935 struct async_node *cur_entry;
2937 cur_entry = (struct async_node *)arg;
2940 * Don't report unconfigured devices (Wildcard devs,
2941 * devices only for target mode, device instances
2942 * that have been invalidated but are waiting for
2943 * their last reference count to be released).
2945 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2948 xpt_compile_path(&path,
2950 device->target->bus->path_id,
2951 device->target->target_id,
2953 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2954 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2955 xpt_action((union ccb *)&cgd);
2956 cur_entry->callback(cur_entry->callback_arg,
2959 xpt_release_path(&path);
2965 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2967 struct cam_path path;
2968 struct ccb_pathinq cpi;
2969 struct async_node *cur_entry;
2971 cur_entry = (struct async_node *)arg;
2973 xpt_compile_path(&path, /*periph*/NULL,
2975 CAM_TARGET_WILDCARD,
2977 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2978 cpi.ccb_h.func_code = XPT_PATH_INQ;
2979 xpt_action((union ccb *)&cpi);
2980 cur_entry->callback(cur_entry->callback_arg,
2983 xpt_release_path(&path);
2989 xpt_action(union ccb *start_ccb)
2995 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2997 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2999 iopl = splsoftcam();
3000 switch (start_ccb->ccb_h.func_code) {
3003 #ifdef CAM_NEW_TRAN_CODE
3004 struct cam_ed *device;
3005 #endif /* CAM_NEW_TRAN_CODE */
3007 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3008 struct cam_path *path;
3010 path = start_ccb->ccb_h.path;
3014 * For the sake of compatibility with SCSI-1
3015 * devices that may not understand the identify
3016 * message, we include lun information in the
3017 * second byte of all commands. SCSI-1 specifies
3018 * that luns are a 3 bit value and reserves only 3
3019 * bits for lun information in the CDB. Later
3020 * revisions of the SCSI spec allow for more than 8
3021 * luns, but have deprecated lun information in the
3022 * CDB. So, if the lun won't fit, we must omit.
3024 * Also be aware that during initial probing for devices,
3025 * the inquiry information is unknown but initialized to 0.
3026 * This means that this code will be exercised while probing
3027 * devices with an ANSI revision greater than 2.
3029 #ifdef CAM_NEW_TRAN_CODE
3030 device = start_ccb->ccb_h.path->device;
3031 if (device->protocol_version <= SCSI_REV_2
3032 #else /* CAM_NEW_TRAN_CODE */
3033 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
3034 #endif /* CAM_NEW_TRAN_CODE */
3035 && start_ccb->ccb_h.target_lun < 8
3036 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
3038 start_ccb->csio.cdb_io.cdb_bytes[1] |=
3039 start_ccb->ccb_h.target_lun << 5;
3041 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3042 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3043 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3044 &path->device->inq_data),
3045 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3046 cdb_str, sizeof(cdb_str))));
3050 case XPT_CONT_TARGET_IO:
3051 start_ccb->csio.sense_resid = 0;
3052 start_ccb->csio.resid = 0;
3057 struct cam_path *path;
3061 path = start_ccb->ccb_h.path;
3064 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3065 if (path->device->qfrozen_cnt == 0)
3066 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3071 xpt_run_dev_sendq(path->bus);
3074 case XPT_SET_TRAN_SETTINGS:
3076 xpt_set_transfer_settings(&start_ccb->cts,
3077 start_ccb->ccb_h.path->device,
3078 /*async_update*/FALSE);
3081 case XPT_CALC_GEOMETRY:
3083 struct cam_sim *sim;
3085 /* Filter out garbage */
3086 if (start_ccb->ccg.block_size == 0
3087 || start_ccb->ccg.volume_size == 0) {
3088 start_ccb->ccg.cylinders = 0;
3089 start_ccb->ccg.heads = 0;
3090 start_ccb->ccg.secs_per_track = 0;
3091 start_ccb->ccb_h.status = CAM_REQ_CMP;
3096 * In a PC-98 system, geometry translation depens on
3097 * the "real" device geometry obtained from mode page 4.
3098 * SCSI geometry translation is performed in the
3099 * initialization routine of the SCSI BIOS and the result
3100 * stored in host memory. If the translation is available
3101 * in host memory, use it. If not, rely on the default
3102 * translation the device driver performs.
3104 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3105 start_ccb->ccb_h.status = CAM_REQ_CMP;
3109 sim = start_ccb->ccb_h.path->bus->sim;
3110 (*(sim->sim_action))(sim, start_ccb);
3115 union ccb* abort_ccb;
3118 abort_ccb = start_ccb->cab.abort_ccb;
3119 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3121 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3122 struct cam_ccbq *ccbq;
3124 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3125 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3126 abort_ccb->ccb_h.status =
3127 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3128 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3130 xpt_done(abort_ccb);
3132 start_ccb->ccb_h.status = CAM_REQ_CMP;
3135 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3136 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3138 * We've caught this ccb en route to
3139 * the SIM. Flag it for abort and the
3140 * SIM will do so just before starting
3141 * real work on the CCB.
3143 abort_ccb->ccb_h.status =
3144 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3145 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3146 start_ccb->ccb_h.status = CAM_REQ_CMP;
3150 if (XPT_FC_IS_QUEUED(abort_ccb)
3151 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3153 * It's already completed but waiting
3154 * for our SWI to get to it.
3156 start_ccb->ccb_h.status = CAM_UA_ABORT;
3160 * If we weren't able to take care of the abort request
3161 * in the XPT, pass the request down to the SIM for processing.
3165 case XPT_ACCEPT_TARGET_IO:
3167 case XPT_IMMED_NOTIFY:
3168 case XPT_NOTIFY_ACK:
3169 case XPT_GET_TRAN_SETTINGS:
3172 struct cam_sim *sim;
3174 sim = start_ccb->ccb_h.path->bus->sim;
3175 (*(sim->sim_action))(sim, start_ccb);
3180 struct cam_sim *sim;
3182 sim = start_ccb->ccb_h.path->bus->sim;
3183 (*(sim->sim_action))(sim, start_ccb);
3186 case XPT_PATH_STATS:
3187 start_ccb->cpis.last_reset =
3188 start_ccb->ccb_h.path->bus->last_reset;
3189 start_ccb->ccb_h.status = CAM_REQ_CMP;
3196 dev = start_ccb->ccb_h.path->device;
3198 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3199 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3201 struct ccb_getdev *cgd;
3205 cgd = &start_ccb->cgd;
3206 bus = cgd->ccb_h.path->bus;
3207 tar = cgd->ccb_h.path->target;
3208 cgd->inq_data = dev->inq_data;
3209 cgd->ccb_h.status = CAM_REQ_CMP;
3210 cgd->serial_num_len = dev->serial_num_len;
3211 if ((dev->serial_num_len > 0)
3212 && (dev->serial_num != NULL))
3213 bcopy(dev->serial_num, cgd->serial_num,
3214 dev->serial_num_len);
3219 case XPT_GDEV_STATS:
3224 dev = start_ccb->ccb_h.path->device;
3226 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3227 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3229 struct ccb_getdevstats *cgds;
3233 cgds = &start_ccb->cgds;
3234 bus = cgds->ccb_h.path->bus;
3235 tar = cgds->ccb_h.path->target;
3236 cgds->dev_openings = dev->ccbq.dev_openings;
3237 cgds->dev_active = dev->ccbq.dev_active;
3238 cgds->devq_openings = dev->ccbq.devq_openings;
3239 cgds->devq_queued = dev->ccbq.queue.entries;
3240 cgds->held = dev->ccbq.held;
3241 cgds->last_reset = tar->last_reset;
3242 cgds->maxtags = dev->quirk->maxtags;
3243 cgds->mintags = dev->quirk->mintags;
3244 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3245 cgds->last_reset = bus->last_reset;
3246 cgds->ccb_h.status = CAM_REQ_CMP;
3253 struct cam_periph *nperiph;
3254 struct periph_list *periph_head;
3255 struct ccb_getdevlist *cgdl;
3258 struct cam_ed *device;
3265 * Don't want anyone mucking with our data.
3268 device = start_ccb->ccb_h.path->device;
3269 periph_head = &device->periphs;
3270 cgdl = &start_ccb->cgdl;
3273 * Check and see if the list has changed since the user
3274 * last requested a list member. If so, tell them that the
3275 * list has changed, and therefore they need to start over
3276 * from the beginning.
3278 if ((cgdl->index != 0) &&
3279 (cgdl->generation != device->generation)) {
3280 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3286 * Traverse the list of peripherals and attempt to find
3287 * the requested peripheral.
3289 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3290 (nperiph != NULL) && (i <= cgdl->index);
3291 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3292 if (i == cgdl->index) {
3293 strncpy(cgdl->periph_name,
3294 nperiph->periph_name,
3296 cgdl->unit_number = nperiph->unit_number;
3301 cgdl->status = CAM_GDEVLIST_ERROR;
3306 if (nperiph == NULL)
3307 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3309 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3312 cgdl->generation = device->generation;
3315 cgdl->ccb_h.status = CAM_REQ_CMP;
3321 dev_pos_type position_type;
3322 struct ccb_dev_match *cdm;
3324 cdm = &start_ccb->cdm;
3327 * Prevent EDT changes while we traverse it.
3331 * There are two ways of getting at information in the EDT.
3332 * The first way is via the primary EDT tree. It starts
3333 * with a list of busses, then a list of targets on a bus,
3334 * then devices/luns on a target, and then peripherals on a
3335 * device/lun. The "other" way is by the peripheral driver
3336 * lists. The peripheral driver lists are organized by
3337 * peripheral driver. (obviously) So it makes sense to
3338 * use the peripheral driver list if the user is looking
3339 * for something like "da1", or all "da" devices. If the
3340 * user is looking for something on a particular bus/target
3341 * or lun, it's generally better to go through the EDT tree.
3344 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3345 position_type = cdm->pos.position_type;
3349 position_type = CAM_DEV_POS_NONE;
3351 for (i = 0; i < cdm->num_patterns; i++) {
3352 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3353 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3354 position_type = CAM_DEV_POS_EDT;
3359 if (cdm->num_patterns == 0)
3360 position_type = CAM_DEV_POS_EDT;
3361 else if (position_type == CAM_DEV_POS_NONE)
3362 position_type = CAM_DEV_POS_PDRV;
3365 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3366 case CAM_DEV_POS_EDT:
3369 case CAM_DEV_POS_PDRV:
3370 xptperiphlistmatch(cdm);
3373 cdm->status = CAM_DEV_MATCH_ERROR;
3379 if (cdm->status == CAM_DEV_MATCH_ERROR)
3380 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3382 start_ccb->ccb_h.status = CAM_REQ_CMP;
3388 struct ccb_setasync *csa;
3389 struct async_node *cur_entry;
3390 struct async_list *async_head;
3394 csa = &start_ccb->csa;
3395 added = csa->event_enable;
3396 async_head = &csa->ccb_h.path->device->asyncs;
3399 * If there is already an entry for us, simply
3403 cur_entry = SLIST_FIRST(async_head);
3404 while (cur_entry != NULL) {
3405 if ((cur_entry->callback_arg == csa->callback_arg)
3406 && (cur_entry->callback == csa->callback))
3408 cur_entry = SLIST_NEXT(cur_entry, links);
3411 if (cur_entry != NULL) {
3413 * If the request has no flags set,
3416 added &= ~cur_entry->event_enable;
3417 if (csa->event_enable == 0) {
3418 SLIST_REMOVE(async_head, cur_entry,
3420 csa->ccb_h.path->device->refcount--;
3421 free(cur_entry, M_CAMXPT);
3423 cur_entry->event_enable = csa->event_enable;
3426 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3428 if (cur_entry == NULL) {
3430 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3433 cur_entry->event_enable = csa->event_enable;
3434 cur_entry->callback_arg = csa->callback_arg;
3435 cur_entry->callback = csa->callback;
3436 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3437 csa->ccb_h.path->device->refcount++;
3440 if ((added & AC_FOUND_DEVICE) != 0) {
3442 * Get this peripheral up to date with all
3443 * the currently existing devices.
3445 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3447 if ((added & AC_PATH_REGISTERED) != 0) {
3449 * Get this peripheral up to date with all
3450 * the currently existing busses.
3452 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3455 start_ccb->ccb_h.status = CAM_REQ_CMP;
3460 struct ccb_relsim *crs;
3464 crs = &start_ccb->crs;
3465 dev = crs->ccb_h.path->device;
3468 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3474 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3476 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3478 /* Don't ever go below one opening */
3479 if (crs->openings > 0) {
3480 xpt_dev_ccbq_resize(crs->ccb_h.path,
3484 xpt_print_path(crs->ccb_h.path);
3485 printf("tagged openings "
3493 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3495 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3498 * Just extend the old timeout and decrement
3499 * the freeze count so that a single timeout
3500 * is sufficient for releasing the queue.
3502 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3503 untimeout(xpt_release_devq_timeout,
3504 dev, dev->c_handle);
3507 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3511 timeout(xpt_release_devq_timeout,
3513 (crs->release_timeout * hz) / 1000);
3515 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3519 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3521 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3523 * Decrement the freeze count so that a single
3524 * completion is still sufficient to unfreeze
3527 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3530 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3531 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3535 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3537 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3538 || (dev->ccbq.dev_active == 0)) {
3540 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3543 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3544 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3549 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3551 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3554 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3555 start_ccb->ccb_h.status = CAM_REQ_CMP;
3559 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3562 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3563 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3571 #ifdef CAM_DEBUG_DELAY
3572 cam_debug_delay = CAM_DEBUG_DELAY;
3574 cam_dflags = start_ccb->cdbg.flags;
3575 if (cam_dpath != NULL) {
3576 xpt_free_path(cam_dpath);
3580 if (cam_dflags != CAM_DEBUG_NONE) {
3581 if (xpt_create_path(&cam_dpath, xpt_periph,
3582 start_ccb->ccb_h.path_id,
3583 start_ccb->ccb_h.target_id,
3584 start_ccb->ccb_h.target_lun) !=
3586 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3587 cam_dflags = CAM_DEBUG_NONE;
3589 start_ccb->ccb_h.status = CAM_REQ_CMP;
3590 xpt_print_path(cam_dpath);
3591 printf("debugging flags now %x\n", cam_dflags);
3595 start_ccb->ccb_h.status = CAM_REQ_CMP;
3598 #else /* !CAMDEBUG */
3599 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3600 #endif /* CAMDEBUG */
3604 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3605 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3606 start_ccb->ccb_h.status = CAM_REQ_CMP;
3613 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3620 xpt_polled_action(union ccb *start_ccb)
3624 struct cam_sim *sim;
3625 struct cam_devq *devq;
3630 timeout = start_ccb->ccb_h.timeout;
3631 sim = start_ccb->ccb_h.path->bus->sim;
3633 dev = start_ccb->ccb_h.path->device;
3638 * Steal an opening so that no other queued requests
3639 * can get it before us while we simulate interrupts.
3641 dev->ccbq.devq_openings--;
3642 dev->ccbq.dev_openings--;
3644 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3645 && (--timeout > 0)) {
3647 (*(sim->sim_poll))(sim);
3651 dev->ccbq.devq_openings++;
3652 dev->ccbq.dev_openings++;
3655 xpt_action(start_ccb);
3656 while(--timeout > 0) {
3657 (*(sim->sim_poll))(sim);
3659 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3666 * XXX Is it worth adding a sim_timeout entry
3667 * point so we can attempt recovery? If
3668 * this is only used for dumps, I don't think
3671 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3674 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3680 * Schedule a peripheral driver to receive a ccb when it's
3681 * target device has space for more transactions.
3684 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3686 struct cam_ed *device;
3692 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3693 device = perph->path->device;
3695 if (periph_is_queued(perph)) {
3696 /* Simply reorder based on new priority */
3697 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3698 (" change priority to %d\n", new_priority));
3699 if (new_priority < perph->pinfo.priority) {
3700 camq_change_priority(&device->drvq,
3706 /* New entry on the queue */
3707 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3708 (" added periph to queue\n"));
3709 perph->pinfo.priority = new_priority;
3710 perph->pinfo.generation = ++device->drvq.generation;
3711 camq_insert(&device->drvq, &perph->pinfo);
3712 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3716 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3717 (" calling xpt_run_devq\n"));
3718 xpt_run_dev_allocq(perph->path->bus);
3724 * Schedule a device to run on a given queue.
3725 * If the device was inserted as a new entry on the queue,
3726 * return 1 meaning the device queue should be run. If we
3727 * were already queued, implying someone else has already
3728 * started the queue, return 0 so the caller doesn't attempt
3729 * to run the queue. Must be run at either splsoftcam
3730 * (or splcam since that encompases splsoftcam).
3733 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3734 u_int32_t new_priority)
3737 u_int32_t old_priority;
3739 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3741 old_priority = pinfo->priority;
3744 * Are we already queued?
3746 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3747 /* Simply reorder based on new priority */
3748 if (new_priority < old_priority) {
3749 camq_change_priority(queue, pinfo->index,
3751 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3752 ("changed priority to %d\n",
3757 /* New entry on the queue */
3758 if (new_priority < old_priority)
3759 pinfo->priority = new_priority;
3761 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3762 ("Inserting onto queue\n"));
3763 pinfo->generation = ++queue->generation;
3764 camq_insert(queue, pinfo);
3771 xpt_run_dev_allocq(struct cam_eb *bus)
3773 struct cam_devq *devq;
3776 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3777 devq = bus->sim->devq;
3779 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3780 (" qfrozen_cnt == 0x%x, entries == %d, "
3781 "openings == %d, active == %d\n",
3782 devq->alloc_queue.qfrozen_cnt,
3783 devq->alloc_queue.entries,
3784 devq->alloc_openings,
3785 devq->alloc_active));
3788 devq->alloc_queue.qfrozen_cnt++;
3789 while ((devq->alloc_queue.entries > 0)
3790 && (devq->alloc_openings > 0)
3791 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3792 struct cam_ed_qinfo *qinfo;
3793 struct cam_ed *device;
3794 union ccb *work_ccb;
3795 struct cam_periph *drv;
3798 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3800 device = qinfo->device;
3802 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3803 ("running device %p\n", device));
3805 drvq = &device->drvq;
3808 if (drvq->entries <= 0) {
3809 panic("xpt_run_dev_allocq: "
3810 "Device on queue without any work to do");
3813 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3814 devq->alloc_openings--;
3815 devq->alloc_active++;
3816 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3818 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3819 drv->pinfo.priority);
3820 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3821 ("calling periph start\n"));
3822 drv->periph_start(drv, work_ccb);
3825 * Malloc failure in alloc_ccb
3828 * XXX add us to a list to be run from free_ccb
3829 * if we don't have any ccbs active on this
3830 * device queue otherwise we may never get run
3836 /* Raise IPL for possible insertion and test at top of loop */
3839 if (drvq->entries > 0) {
3840 /* We have more work. Attempt to reschedule */
3841 xpt_schedule_dev_allocq(bus, device);
3844 devq->alloc_queue.qfrozen_cnt--;
3849 xpt_run_dev_sendq(struct cam_eb *bus)
3851 struct cam_devq *devq;
3854 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3856 devq = bus->sim->devq;
3859 devq->send_queue.qfrozen_cnt++;
3862 while ((devq->send_queue.entries > 0)
3863 && (devq->send_openings > 0)) {
3864 struct cam_ed_qinfo *qinfo;
3865 struct cam_ed *device;
3866 union ccb *work_ccb;
3867 struct cam_sim *sim;
3871 if (devq->send_queue.qfrozen_cnt > 1) {
3876 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3878 device = qinfo->device;
3881 * If the device has been "frozen", don't attempt
3884 if (device->qfrozen_cnt > 0) {
3889 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3890 ("running device %p\n", device));
3892 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3893 if (work_ccb == NULL) {
3894 printf("device on run queue with no ccbs???\n");
3899 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3901 if (num_highpower <= 0) {
3903 * We got a high power command, but we
3904 * don't have any available slots. Freeze
3905 * the device queue until we have a slot
3908 device->qfrozen_cnt++;
3909 STAILQ_INSERT_TAIL(&highpowerq,
3917 * Consume a high power slot while
3923 devq->active_dev = device;
3924 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3926 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3929 devq->send_openings--;
3930 devq->send_active++;
3932 if (device->ccbq.queue.entries > 0)
3933 xpt_schedule_dev_sendq(bus, device);
3935 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3937 * The client wants to freeze the queue
3938 * after this CCB is sent.
3941 device->qfrozen_cnt++;
3947 /* In Target mode, the peripheral driver knows best... */
3948 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3949 if ((device->inq_flags & SID_CmdQue) != 0
3950 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3951 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3954 * Clear this in case of a retried CCB that
3955 * failed due to a rejected tag.
3957 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3961 * Device queues can be shared among multiple sim instances
3962 * that reside on different busses. Use the SIM in the queue
3963 * CCB's path, rather than the one in the bus that was passed
3964 * into this function.
3966 sim = work_ccb->ccb_h.path->bus->sim;
3967 (*(sim->sim_action))(sim, work_ccb);
3970 devq->active_dev = NULL;
3972 /* Raise IPL for possible insertion and test at top of loop */
3977 devq->send_queue.qfrozen_cnt--;
3982 * This function merges stuff from the slave ccb into the master ccb, while
3983 * keeping important fields in the master ccb constant.
3986 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3991 * Pull fields that are valid for peripheral drivers to set
3992 * into the master CCB along with the CCB "payload".
3994 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3995 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3996 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3997 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3998 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3999 sizeof(union ccb) - sizeof(struct ccb_hdr));
4003 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
4007 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
4008 ccb_h->pinfo.priority = priority;
4010 ccb_h->path_id = path->bus->path_id;
4012 ccb_h->target_id = path->target->target_id;
4014 ccb_h->target_id = CAM_TARGET_WILDCARD;
4016 ccb_h->target_lun = path->device->lun_id;
4017 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4019 ccb_h->target_lun = CAM_TARGET_WILDCARD;
4021 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4025 /* Path manipulation functions */
4027 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4028 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4030 struct cam_path *path;
4035 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
4038 status = CAM_RESRC_UNAVAIL;
4041 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4042 if (status != CAM_REQ_CMP) {
4043 free(path, M_CAMXPT);
4046 *new_path_ptr = path;
4051 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4052 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4055 struct cam_et *target;
4056 struct cam_ed *device;
4060 status = CAM_REQ_CMP; /* Completed without error */
4061 target = NULL; /* Wildcarded */
4062 device = NULL; /* Wildcarded */
4065 * We will potentially modify the EDT, so block interrupts
4066 * that may attempt to create cam paths.
4069 bus = xpt_find_bus(path_id);
4071 status = CAM_PATH_INVALID;
4073 target = xpt_find_target(bus, target_id);
4074 if (target == NULL) {
4076 struct cam_et *new_target;
4078 new_target = xpt_alloc_target(bus, target_id);
4079 if (new_target == NULL) {
4080 status = CAM_RESRC_UNAVAIL;
4082 target = new_target;
4085 if (target != NULL) {
4086 device = xpt_find_device(target, lun_id);
4087 if (device == NULL) {
4089 struct cam_ed *new_device;
4091 new_device = xpt_alloc_device(bus,
4094 if (new_device == NULL) {
4095 status = CAM_RESRC_UNAVAIL;
4097 device = new_device;
4105 * Only touch the user's data if we are successful.
4107 if (status == CAM_REQ_CMP) {
4108 new_path->periph = perph;
4109 new_path->bus = bus;
4110 new_path->target = target;
4111 new_path->device = device;
4112 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4115 xpt_release_device(bus, target, device);
4117 xpt_release_target(bus, target);
4119 xpt_release_bus(bus);
4125 xpt_release_path(struct cam_path *path)
4127 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4128 if (path->device != NULL) {
4129 xpt_release_device(path->bus, path->target, path->device);
4130 path->device = NULL;
4132 if (path->target != NULL) {
4133 xpt_release_target(path->bus, path->target);
4134 path->target = NULL;
4136 if (path->bus != NULL) {
4137 xpt_release_bus(path->bus);
4143 xpt_free_path(struct cam_path *path)
4147 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4148 xpt_release_path(path);
4149 free(path, M_CAMXPT);
4154 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4155 * in path1, 2 for match with wildcards in path2.
4158 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4164 if (path1->bus != path2->bus) {
4165 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4167 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4172 if (path1->target != path2->target) {
4173 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4176 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4181 if (path1->device != path2->device) {
4182 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4185 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4194 xpt_print_path(struct cam_path *path)
4199 printf("(nopath): ");
4201 if (path->periph != NULL)
4202 printf("(%s%d:", path->periph->periph_name,
4203 path->periph->unit_number);
4205 printf("(noperiph:");
4207 if (path->bus != NULL)
4208 printf("%s%d:%d:", path->bus->sim->sim_name,
4209 path->bus->sim->unit_number,
4210 path->bus->sim->bus_id);
4214 if (path->target != NULL)
4215 printf("%d:", path->target->target_id);
4219 if (path->device != NULL)
4220 printf("%d): ", path->device->lun_id);
4227 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4233 sbuf_new(&sb, str, str_len, 0);
4236 sbuf_printf(&sb, "(nopath): ");
4238 if (path->periph != NULL)
4239 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4240 path->periph->unit_number);
4242 sbuf_printf(&sb, "(noperiph:");
4244 if (path->bus != NULL)
4245 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4246 path->bus->sim->unit_number,
4247 path->bus->sim->bus_id);
4249 sbuf_printf(&sb, "nobus:");
4251 if (path->target != NULL)
4252 sbuf_printf(&sb, "%d:", path->target->target_id);
4254 sbuf_printf(&sb, "X:");
4256 if (path->device != NULL)
4257 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4259 sbuf_printf(&sb, "X): ");
4263 return(sbuf_len(&sb));
4267 xpt_path_path_id(struct cam_path *path)
4271 return(path->bus->path_id);
4275 xpt_path_target_id(struct cam_path *path)
4279 if (path->target != NULL)
4280 return (path->target->target_id);
4282 return (CAM_TARGET_WILDCARD);
4286 xpt_path_lun_id(struct cam_path *path)
4290 if (path->device != NULL)
4291 return (path->device->lun_id);
4293 return (CAM_LUN_WILDCARD);
4297 xpt_path_sim(struct cam_path *path)
4301 return (path->bus->sim);
4305 xpt_path_periph(struct cam_path *path)
4309 return (path->periph);
4313 * Release a CAM control block for the caller. Remit the cost of the structure
4314 * to the device referenced by the path. If the this device had no 'credits'
4315 * and peripheral drivers have registered async callbacks for this notification
4319 xpt_release_ccb(union ccb *free_ccb)
4322 struct cam_path *path;
4323 struct cam_ed *device;
4328 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4329 path = free_ccb->ccb_h.path;
4330 device = path->device;
4333 cam_ccbq_release_opening(&device->ccbq);
4334 if (xpt_ccb_count > xpt_max_ccbs) {
4335 xpt_free_ccb(free_ccb);
4338 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4340 bus->sim->devq->alloc_openings++;
4341 bus->sim->devq->alloc_active--;
4342 /* XXX Turn this into an inline function - xpt_run_device?? */
4343 if ((device_is_alloc_queued(device) == 0)
4344 && (device->drvq.entries > 0)) {
4345 xpt_schedule_dev_allocq(bus, device);
4348 if (dev_allocq_is_runnable(bus->sim->devq))
4349 xpt_run_dev_allocq(bus);
4352 /* Functions accessed by SIM drivers */
4355 * A sim structure, listing the SIM entry points and instance
4356 * identification info is passed to xpt_bus_register to hook the SIM
4357 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4358 * for this new bus and places it in the array of busses and assigns
4359 * it a path_id. The path_id may be influenced by "hard wiring"
4360 * information specified by the user. Once interrupt services are
4361 * availible, the bus will be probed.
4364 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4366 struct cam_eb *new_bus;
4367 struct cam_eb *old_bus;
4368 struct ccb_pathinq cpi;
4374 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4375 M_CAMXPT, M_NOWAIT);
4376 if (new_bus == NULL) {
4377 /* Couldn't satisfy request */
4378 return (CAM_RESRC_UNAVAIL);
4381 if (strcmp(sim->sim_name, "xpt") != 0) {
4384 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4387 TAILQ_INIT(&new_bus->et_entries);
4388 new_bus->path_id = sim->path_id;
4390 timevalclear(&new_bus->last_reset);
4392 new_bus->refcount = 1; /* Held until a bus_deregister event */
4393 new_bus->generation = 0;
4395 old_bus = TAILQ_FIRST(&xpt_busses);
4396 while (old_bus != NULL
4397 && old_bus->path_id < new_bus->path_id)
4398 old_bus = TAILQ_NEXT(old_bus, links);
4399 if (old_bus != NULL)
4400 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4402 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4406 /* Notify interested parties */
4407 if (sim->path_id != CAM_XPT_PATH_ID) {
4408 struct cam_path path;
4410 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4411 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4412 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4413 cpi.ccb_h.func_code = XPT_PATH_INQ;
4414 xpt_action((union ccb *)&cpi);
4415 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4416 xpt_release_path(&path);
4418 return (CAM_SUCCESS);
4422 xpt_bus_deregister(path_id_t pathid)
4424 struct cam_path bus_path;
4429 status = xpt_compile_path(&bus_path, NULL, pathid,
4430 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4431 if (status != CAM_REQ_CMP)
4434 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4435 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4437 /* Release the reference count held while registered. */
4438 xpt_release_bus(bus_path.bus);
4439 xpt_release_path(&bus_path);
4441 return (CAM_REQ_CMP);
4445 xptnextfreepathid(void)
4452 bus = TAILQ_FIRST(&xpt_busses);
4454 /* Find an unoccupied pathid */
4456 && bus->path_id <= pathid) {
4457 if (bus->path_id == pathid)
4459 bus = TAILQ_NEXT(bus, links);
4463 * Ensure that this pathid is not reserved for
4464 * a bus that may be registered in the future.
4466 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4468 /* Start the search over */
4475 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4482 pathid = CAM_XPT_PATH_ID;
4483 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4485 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4486 if (strcmp(dname, "scbus")) {
4487 /* Avoid a bit of foot shooting. */
4490 if (dunit < 0) /* unwired?! */
4492 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4493 if (sim_bus == val) {
4497 } else if (sim_bus == 0) {
4498 /* Unspecified matches bus 0 */
4502 printf("Ambiguous scbus configuration for %s%d "
4503 "bus %d, cannot wire down. The kernel "
4504 "config entry for scbus%d should "
4505 "specify a controller bus.\n"
4506 "Scbus will be assigned dynamically.\n",
4507 sim_name, sim_unit, sim_bus, dunit);
4512 if (pathid == CAM_XPT_PATH_ID)
4513 pathid = xptnextfreepathid();
4518 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4521 struct cam_et *target, *next_target;
4522 struct cam_ed *device, *next_device;
4527 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4530 * Most async events come from a CAM interrupt context. In
4531 * a few cases, the error recovery code at the peripheral layer,
4532 * which may run from our SWI or a process context, may signal
4533 * deferred events with a call to xpt_async. Ensure async
4534 * notifications are serialized by blocking cam interrupts.
4540 if (async_code == AC_BUS_RESET) {
4544 /* Update our notion of when the last reset occurred */
4545 microtime(&bus->last_reset);
4549 for (target = TAILQ_FIRST(&bus->et_entries);
4551 target = next_target) {
4553 next_target = TAILQ_NEXT(target, links);
4555 if (path->target != target
4556 && path->target->target_id != CAM_TARGET_WILDCARD
4557 && target->target_id != CAM_TARGET_WILDCARD)
4560 if (async_code == AC_SENT_BDR) {
4563 /* Update our notion of when the last reset occurred */
4565 microtime(&path->target->last_reset);
4569 for (device = TAILQ_FIRST(&target->ed_entries);
4571 device = next_device) {
4573 next_device = TAILQ_NEXT(device, links);
4575 if (path->device != device
4576 && path->device->lun_id != CAM_LUN_WILDCARD
4577 && device->lun_id != CAM_LUN_WILDCARD)
4580 xpt_dev_async(async_code, bus, target,
4583 xpt_async_bcast(&device->asyncs, async_code,
4589 * If this wasn't a fully wildcarded async, tell all
4590 * clients that want all async events.
4592 if (bus != xpt_periph->path->bus)
4593 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4599 xpt_async_bcast(struct async_list *async_head,
4600 u_int32_t async_code,
4601 struct cam_path *path, void *async_arg)
4603 struct async_node *cur_entry;
4605 cur_entry = SLIST_FIRST(async_head);
4606 while (cur_entry != NULL) {
4607 struct async_node *next_entry;
4609 * Grab the next list entry before we call the current
4610 * entry's callback. This is because the callback function
4611 * can delete its async callback entry.
4613 next_entry = SLIST_NEXT(cur_entry, links);
4614 if ((cur_entry->event_enable & async_code) != 0)
4615 cur_entry->callback(cur_entry->callback_arg,
4618 cur_entry = next_entry;
4623 * Handle any per-device event notifications that require action by the XPT.
4626 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4627 struct cam_ed *device, void *async_arg)
4630 struct cam_path newpath;
4633 * We only need to handle events for real devices.
4635 if (target->target_id == CAM_TARGET_WILDCARD
4636 || device->lun_id == CAM_LUN_WILDCARD)
4640 * We need our own path with wildcards expanded to
4641 * handle certain types of events.
4643 if ((async_code == AC_SENT_BDR)
4644 || (async_code == AC_BUS_RESET)
4645 || (async_code == AC_INQ_CHANGED))
4646 status = xpt_compile_path(&newpath, NULL,
4651 status = CAM_REQ_CMP_ERR;
4653 if (status == CAM_REQ_CMP) {
4656 * Allow transfer negotiation to occur in a
4657 * tag free environment.
4659 if (async_code == AC_SENT_BDR
4660 || async_code == AC_BUS_RESET)
4661 xpt_toggle_tags(&newpath);
4663 if (async_code == AC_INQ_CHANGED) {
4665 * We've sent a start unit command, or
4666 * something similar to a device that
4667 * may have caused its inquiry data to
4668 * change. So we re-scan the device to
4669 * refresh the inquiry data for it.
4671 xpt_scan_lun(newpath.periph, &newpath,
4672 CAM_EXPECT_INQ_CHANGE, NULL);
4674 xpt_release_path(&newpath);
4675 } else if (async_code == AC_LOST_DEVICE) {
4676 device->flags |= CAM_DEV_UNCONFIGURED;
4677 } else if (async_code == AC_TRANSFER_NEG) {
4678 struct ccb_trans_settings *settings;
4680 settings = (struct ccb_trans_settings *)async_arg;
4681 xpt_set_transfer_settings(settings, device,
4682 /*async_update*/TRUE);
4687 xpt_freeze_devq(struct cam_path *path, u_int count)
4690 struct ccb_hdr *ccbh;
4695 path->device->qfrozen_cnt += count;
4698 * Mark the last CCB in the queue as needing
4699 * to be requeued if the driver hasn't
4700 * changed it's state yet. This fixes a race
4701 * where a ccb is just about to be queued to
4702 * a controller driver when it's interrupt routine
4703 * freezes the queue. To completly close the
4704 * hole, controller drives must check to see
4705 * if a ccb's status is still CAM_REQ_INPROG
4706 * under spl protection just before they queue
4707 * the CCB. See ahc_action/ahc_freeze_devq for
4710 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4711 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4712 ccbh->status = CAM_REQUEUE_REQ;
4714 return (path->device->qfrozen_cnt);
4718 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4722 sim->devq->send_queue.qfrozen_cnt += count;
4723 if (sim->devq->active_dev != NULL) {
4724 struct ccb_hdr *ccbh;
4726 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4728 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4729 ccbh->status = CAM_REQUEUE_REQ;
4731 return (sim->devq->send_queue.qfrozen_cnt);
4735 xpt_release_devq_timeout(void *arg)
4737 struct cam_ed *device;
4739 device = (struct cam_ed *)arg;
4741 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4745 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4749 xpt_release_devq_device(path->device, count, run_queue);
4753 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4761 if (dev->qfrozen_cnt > 0) {
4763 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4764 dev->qfrozen_cnt -= count;
4765 if (dev->qfrozen_cnt == 0) {
4768 * No longer need to wait for a successful
4769 * command completion.
4771 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4774 * Remove any timeouts that might be scheduled
4775 * to release this queue.
4777 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4778 untimeout(xpt_release_devq_timeout, dev,
4780 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4784 * Now that we are unfrozen schedule the
4785 * device so any pending transactions are
4788 if ((dev->ccbq.queue.entries > 0)
4789 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4790 && (run_queue != 0)) {
4797 xpt_run_dev_sendq(dev->target->bus);
4802 xpt_release_simq(struct cam_sim *sim, int run_queue)
4809 sendq = &(sim->devq->send_queue);
4811 if (sendq->qfrozen_cnt > 0) {
4813 sendq->qfrozen_cnt--;
4814 if (sendq->qfrozen_cnt == 0) {
4818 * If there is a timeout scheduled to release this
4819 * sim queue, remove it. The queue frozen count is
4822 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4823 untimeout(xpt_release_simq_timeout, sim,
4825 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4827 bus = xpt_find_bus(sim->path_id);
4832 * Now that we are unfrozen run the send queue.
4834 xpt_run_dev_sendq(bus);
4836 xpt_release_bus(bus);
4844 xpt_release_simq_timeout(void *arg)
4846 struct cam_sim *sim;
4848 sim = (struct cam_sim *)arg;
4849 xpt_release_simq(sim, /* run_queue */ TRUE);
4853 xpt_done(union ccb *done_ccb)
4859 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4860 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4862 * Queue up the request for handling by our SWI handler
4863 * any of the "non-immediate" type of ccbs.
4865 switch (done_ccb->ccb_h.path->periph->type) {
4866 case CAM_PERIPH_BIO:
4867 mtx_lock(&cam_bioq_lock);
4868 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4870 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4871 mtx_unlock(&cam_bioq_lock);
4872 swi_sched(cambio_ih, 0);
4875 panic("unknown periph type %d",
4876 done_ccb->ccb_h.path->periph->type);
4889 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4894 xpt_alloc_ccb_nowait()
4900 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
4905 xpt_free_ccb(union ccb *free_ccb)
4907 free(free_ccb, M_CAMXPT);
4912 /* Private XPT functions */
4915 * Get a CAM control block for the caller. Charge the structure to the device
4916 * referenced by the path. If the this device has no 'credits' then the
4917 * device already has the maximum number of outstanding operations under way
4918 * and we return NULL. If we don't have sufficient resources to allocate more
4919 * ccbs, we also return NULL.
4922 xpt_get_ccb(struct cam_ed *device)
4928 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4929 new_ccb = xpt_alloc_ccb_nowait();
4930 if (new_ccb == NULL) {
4934 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4935 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4939 cam_ccbq_take_opening(&device->ccbq);
4940 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4946 xpt_release_bus(struct cam_eb *bus)
4951 if ((--bus->refcount == 0)
4952 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4953 TAILQ_REMOVE(&xpt_busses, bus, links);
4956 free(bus, M_CAMXPT);
4961 static struct cam_et *
4962 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4964 struct cam_et *target;
4966 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4967 if (target != NULL) {
4968 struct cam_et *cur_target;
4970 TAILQ_INIT(&target->ed_entries);
4972 target->target_id = target_id;
4973 target->refcount = 1;
4974 target->generation = 0;
4975 timevalclear(&target->last_reset);
4977 * Hold a reference to our parent bus so it
4978 * will not go away before we do.
4982 /* Insertion sort into our bus's target list */
4983 cur_target = TAILQ_FIRST(&bus->et_entries);
4984 while (cur_target != NULL && cur_target->target_id < target_id)
4985 cur_target = TAILQ_NEXT(cur_target, links);
4987 if (cur_target != NULL) {
4988 TAILQ_INSERT_BEFORE(cur_target, target, links);
4990 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4998 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
5003 if ((--target->refcount == 0)
5004 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
5005 TAILQ_REMOVE(&bus->et_entries, target, links);
5008 free(target, M_CAMXPT);
5009 xpt_release_bus(bus);
5014 static struct cam_ed *
5015 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5017 #ifdef CAM_NEW_TRAN_CODE
5018 struct cam_path path;
5019 #endif /* CAM_NEW_TRAN_CODE */
5020 struct cam_ed *device;
5021 struct cam_devq *devq;
5024 /* Make space for us in the device queue on our bus */
5025 devq = bus->sim->devq;
5026 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5028 if (status != CAM_REQ_CMP) {
5031 device = (struct cam_ed *)malloc(sizeof(*device),
5032 M_CAMXPT, M_NOWAIT);
5035 if (device != NULL) {
5036 struct cam_ed *cur_device;
5038 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5039 device->alloc_ccb_entry.device = device;
5040 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5041 device->send_ccb_entry.device = device;
5042 device->target = target;
5043 device->lun_id = lun_id;
5044 /* Initialize our queues */
5045 if (camq_init(&device->drvq, 0) != 0) {
5046 free(device, M_CAMXPT);
5049 if (cam_ccbq_init(&device->ccbq,
5050 bus->sim->max_dev_openings) != 0) {
5051 camq_fini(&device->drvq);
5052 free(device, M_CAMXPT);
5055 SLIST_INIT(&device->asyncs);
5056 SLIST_INIT(&device->periphs);
5057 device->generation = 0;
5058 device->owner = NULL;
5060 * Take the default quirk entry until we have inquiry
5061 * data and can determine a better quirk to use.
5063 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5064 bzero(&device->inq_data, sizeof(device->inq_data));
5065 device->inq_flags = 0;
5066 device->queue_flags = 0;
5067 device->serial_num = NULL;
5068 device->serial_num_len = 0;
5069 device->qfrozen_cnt = 0;
5070 device->flags = CAM_DEV_UNCONFIGURED;
5071 device->tag_delay_count = 0;
5072 device->tag_saved_openings = 0;
5073 device->refcount = 1;
5074 callout_handle_init(&device->c_handle);
5077 * Hold a reference to our parent target so it
5078 * will not go away before we do.
5083 * XXX should be limited by number of CCBs this bus can
5086 xpt_max_ccbs += device->ccbq.devq_openings;
5087 /* Insertion sort into our target's device list */
5088 cur_device = TAILQ_FIRST(&target->ed_entries);
5089 while (cur_device != NULL && cur_device->lun_id < lun_id)
5090 cur_device = TAILQ_NEXT(cur_device, links);
5091 if (cur_device != NULL) {
5092 TAILQ_INSERT_BEFORE(cur_device, device, links);
5094 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5096 target->generation++;
5097 #ifdef CAM_NEW_TRAN_CODE
5098 if (lun_id != CAM_LUN_WILDCARD) {
5099 xpt_compile_path(&path,
5104 xpt_devise_transport(&path);
5105 xpt_release_path(&path);
5107 #endif /* CAM_NEW_TRAN_CODE */
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 /* Release our slot in the devq */
5135 devq = bus->sim->devq;
5136 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5138 camq_fini(&device->drvq);
5139 camq_fini(&device->ccbq.queue);
5140 free(device, M_CAMXPT);
5141 xpt_release_target(bus, target);
5147 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5157 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5158 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5159 if (result == CAM_REQ_CMP && (diff < 0)) {
5160 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5162 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5163 || (dev->inq_flags & SID_CmdQue) != 0)
5164 dev->tag_saved_openings = newopenings;
5165 /* Adjust the global limit */
5166 xpt_max_ccbs += diff;
5171 static struct cam_eb *
5172 xpt_find_bus(path_id_t path_id)
5176 for (bus = TAILQ_FIRST(&xpt_busses);
5178 bus = TAILQ_NEXT(bus, links)) {
5179 if (bus->path_id == path_id) {
5187 static struct cam_et *
5188 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5190 struct cam_et *target;
5192 for (target = TAILQ_FIRST(&bus->et_entries);
5194 target = TAILQ_NEXT(target, links)) {
5195 if (target->target_id == target_id) {
5203 static struct cam_ed *
5204 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5206 struct cam_ed *device;
5208 for (device = TAILQ_FIRST(&target->ed_entries);
5210 device = TAILQ_NEXT(device, links)) {
5211 if (device->lun_id == lun_id) {
5220 union ccb *request_ccb;
5221 struct ccb_pathinq *cpi;
5223 } xpt_scan_bus_info;
5226 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5227 * As the scan progresses, xpt_scan_bus is used as the
5228 * callback on completion function.
5231 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5233 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5234 ("xpt_scan_bus\n"));
5235 switch (request_ccb->ccb_h.func_code) {
5238 xpt_scan_bus_info *scan_info;
5239 union ccb *work_ccb;
5240 struct cam_path *path;
5245 /* Find out the characteristics of the bus */
5246 work_ccb = xpt_alloc_ccb();
5247 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5248 request_ccb->ccb_h.pinfo.priority);
5249 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5250 xpt_action(work_ccb);
5251 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5252 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5253 xpt_free_ccb(work_ccb);
5254 xpt_done(request_ccb);
5258 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5260 * Can't scan the bus on an adapter that
5261 * cannot perform the initiator role.
5263 request_ccb->ccb_h.status = CAM_REQ_CMP;
5264 xpt_free_ccb(work_ccb);
5265 xpt_done(request_ccb);
5269 /* Save some state for use while we probe for devices */
5270 scan_info = (xpt_scan_bus_info *)
5271 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5272 scan_info->request_ccb = request_ccb;
5273 scan_info->cpi = &work_ccb->cpi;
5275 /* Cache on our stack so we can work asynchronously */
5276 max_target = scan_info->cpi->max_target;
5277 initiator_id = scan_info->cpi->initiator_id;
5280 * Don't count the initiator if the
5281 * initiator is addressable.
5283 scan_info->pending_count = max_target + 1;
5284 if (initiator_id <= max_target)
5285 scan_info->pending_count--;
5287 for (i = 0; i <= max_target; i++) {
5289 if (i == initiator_id)
5292 status = xpt_create_path(&path, xpt_periph,
5293 request_ccb->ccb_h.path_id,
5295 if (status != CAM_REQ_CMP) {
5296 printf("xpt_scan_bus: xpt_create_path failed"
5297 " with status %#x, bus scan halted\n",
5301 work_ccb = xpt_alloc_ccb();
5302 xpt_setup_ccb(&work_ccb->ccb_h, path,
5303 request_ccb->ccb_h.pinfo.priority);
5304 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5305 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5306 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5307 work_ccb->crcn.flags = request_ccb->crcn.flags;
5308 xpt_action(work_ccb);
5314 xpt_scan_bus_info *scan_info;
5316 target_id_t target_id;
5319 /* Reuse the same CCB to query if a device was really found */
5320 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5321 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5322 request_ccb->ccb_h.pinfo.priority);
5323 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5325 path_id = request_ccb->ccb_h.path_id;
5326 target_id = request_ccb->ccb_h.target_id;
5327 lun_id = request_ccb->ccb_h.target_lun;
5328 xpt_action(request_ccb);
5330 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5331 struct cam_ed *device;
5332 struct cam_et *target;
5336 * If we already probed lun 0 successfully, or
5337 * we have additional configured luns on this
5338 * target that might have "gone away", go onto
5341 target = request_ccb->ccb_h.path->target;
5343 * We may touch devices that we don't
5344 * hold references too, so ensure they
5345 * don't disappear out from under us.
5346 * The target above is referenced by the
5347 * path in the request ccb.
5351 device = TAILQ_FIRST(&target->ed_entries);
5352 if (device != NULL) {
5353 phl = CAN_SRCH_HI_SPARSE(device);
5354 if (device->lun_id == 0)
5355 device = TAILQ_NEXT(device, links);
5358 if ((lun_id != 0) || (device != NULL)) {
5359 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5363 struct cam_ed *device;
5365 device = request_ccb->ccb_h.path->device;
5367 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5368 /* Try the next lun */
5369 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5370 || CAN_SRCH_HI_DENSE(device))
5375 xpt_free_path(request_ccb->ccb_h.path);
5378 if ((lun_id == request_ccb->ccb_h.target_lun)
5379 || lun_id > scan_info->cpi->max_lun) {
5382 xpt_free_ccb(request_ccb);
5383 scan_info->pending_count--;
5384 if (scan_info->pending_count == 0) {
5385 xpt_free_ccb((union ccb *)scan_info->cpi);
5386 request_ccb = scan_info->request_ccb;
5387 free(scan_info, M_TEMP);
5388 request_ccb->ccb_h.status = CAM_REQ_CMP;
5389 xpt_done(request_ccb);
5392 /* Try the next device */
5393 struct cam_path *path;
5396 status = xpt_create_path(&path, xpt_periph,
5397 path_id, target_id, lun_id);
5398 if (status != CAM_REQ_CMP) {
5399 printf("xpt_scan_bus: xpt_create_path failed "
5400 "with status %#x, halting LUN scan\n",
5402 xpt_free_ccb(request_ccb);
5403 scan_info->pending_count--;
5404 if (scan_info->pending_count == 0) {
5406 (union ccb *)scan_info->cpi);
5407 request_ccb = scan_info->request_ccb;
5408 free(scan_info, M_TEMP);
5409 request_ccb->ccb_h.status = CAM_REQ_CMP;
5410 xpt_done(request_ccb);
5414 xpt_setup_ccb(&request_ccb->ccb_h, path,
5415 request_ccb->ccb_h.pinfo.priority);
5416 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5417 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5418 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5419 request_ccb->crcn.flags =
5420 scan_info->request_ccb->crcn.flags;
5421 xpt_action(request_ccb);
5436 PROBE_TUR_FOR_NEGOTIATION
5440 PROBE_INQUIRY_CKSUM = 0x01,
5441 PROBE_SERIAL_CKSUM = 0x02,
5442 PROBE_NO_ANNOUNCE = 0x04
5446 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5447 probe_action action;
5448 union ccb saved_ccb;
5451 u_int8_t digest[16];
5455 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5456 cam_flags flags, union ccb *request_ccb)
5458 struct ccb_pathinq cpi;
5460 struct cam_path *new_path;
5461 struct cam_periph *old_periph;
5464 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5465 ("xpt_scan_lun\n"));
5467 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5468 cpi.ccb_h.func_code = XPT_PATH_INQ;
5469 xpt_action((union ccb *)&cpi);
5471 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5472 if (request_ccb != NULL) {
5473 request_ccb->ccb_h.status = cpi.ccb_h.status;
5474 xpt_done(request_ccb);
5479 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5481 * Can't scan the bus on an adapter that
5482 * cannot perform the initiator role.
5484 if (request_ccb != NULL) {
5485 request_ccb->ccb_h.status = CAM_REQ_CMP;
5486 xpt_done(request_ccb);
5491 if (request_ccb == NULL) {
5492 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5493 if (request_ccb == NULL) {
5494 xpt_print_path(path);
5495 printf("xpt_scan_lun: can't allocate CCB, can't "
5499 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5500 if (new_path == NULL) {
5501 xpt_print_path(path);
5502 printf("xpt_scan_lun: can't allocate path, can't "
5504 free(request_ccb, M_TEMP);
5507 status = xpt_compile_path(new_path, xpt_periph,
5509 path->target->target_id,
5510 path->device->lun_id);
5512 if (status != CAM_REQ_CMP) {
5513 xpt_print_path(path);
5514 printf("xpt_scan_lun: can't compile path, can't "
5516 free(request_ccb, M_TEMP);
5517 free(new_path, M_TEMP);
5520 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5521 request_ccb->ccb_h.cbfcnp = xptscandone;
5522 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5523 request_ccb->crcn.flags = flags;
5527 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5530 softc = (probe_softc *)old_periph->softc;
5531 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5534 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5535 probestart, "probe",
5537 request_ccb->ccb_h.path, NULL, 0,
5540 if (status != CAM_REQ_CMP) {
5541 xpt_print_path(path);
5542 printf("xpt_scan_lun: cam_alloc_periph returned an "
5543 "error, can't continue probe\n");
5544 request_ccb->ccb_h.status = status;
5545 xpt_done(request_ccb);
5552 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5554 xpt_release_path(done_ccb->ccb_h.path);
5555 free(done_ccb->ccb_h.path, M_TEMP);
5556 free(done_ccb, M_TEMP);
5560 proberegister(struct cam_periph *periph, void *arg)
5562 union ccb *request_ccb; /* CCB representing the probe request */
5565 request_ccb = (union ccb *)arg;
5566 if (periph == NULL) {
5567 printf("proberegister: periph was NULL!!\n");
5568 return(CAM_REQ_CMP_ERR);
5571 if (request_ccb == NULL) {
5572 printf("proberegister: no probe CCB, "
5573 "can't register device\n");
5574 return(CAM_REQ_CMP_ERR);
5577 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5579 if (softc == NULL) {
5580 printf("proberegister: Unable to probe new device. "
5581 "Unable to allocate softc\n");
5582 return(CAM_REQ_CMP_ERR);
5584 TAILQ_INIT(&softc->request_ccbs);
5585 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5588 periph->softc = softc;
5589 cam_periph_acquire(periph);
5591 * Ensure we've waited at least a bus settle
5592 * delay before attempting to probe the device.
5593 * For HBAs that don't do bus resets, this won't make a difference.
5595 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5597 probeschedule(periph);
5598 return(CAM_REQ_CMP);
5602 probeschedule(struct cam_periph *periph)
5604 struct ccb_pathinq cpi;
5608 softc = (probe_softc *)periph->softc;
5609 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5611 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5612 cpi.ccb_h.func_code = XPT_PATH_INQ;
5613 xpt_action((union ccb *)&cpi);
5616 * If a device has gone away and another device, or the same one,
5617 * is back in the same place, it should have a unit attention
5618 * condition pending. It will not report the unit attention in
5619 * response to an inquiry, which may leave invalid transfer
5620 * negotiations in effect. The TUR will reveal the unit attention
5621 * condition. Only send the TUR for lun 0, since some devices
5622 * will get confused by commands other than inquiry to non-existent
5623 * luns. If you think a device has gone away start your scan from
5624 * lun 0. This will insure that any bogus transfer settings are
5627 * If we haven't seen the device before and the controller supports
5628 * some kind of transfer negotiation, negotiate with the first
5629 * sent command if no bus reset was performed at startup. This
5630 * ensures that the device is not confused by transfer negotiation
5631 * settings left over by loader or BIOS action.
5633 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5634 && (ccb->ccb_h.target_lun == 0)) {
5635 softc->action = PROBE_TUR;
5636 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5637 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5638 proberequestdefaultnegotiation(periph);
5639 softc->action = PROBE_INQUIRY;
5641 softc->action = PROBE_INQUIRY;
5644 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5645 softc->flags |= PROBE_NO_ANNOUNCE;
5647 softc->flags &= ~PROBE_NO_ANNOUNCE;
5649 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5653 probestart(struct cam_periph *periph, union ccb *start_ccb)
5655 /* Probe the device that our peripheral driver points to */
5656 struct ccb_scsiio *csio;
5659 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5661 softc = (probe_softc *)periph->softc;
5662 csio = &start_ccb->csio;
5664 switch (softc->action) {
5666 case PROBE_TUR_FOR_NEGOTIATION:
5668 scsi_test_unit_ready(csio,
5677 case PROBE_FULL_INQUIRY:
5680 struct scsi_inquiry_data *inq_buf;
5682 inq_buf = &periph->path->device->inq_data;
5684 * If the device is currently configured, we calculate an
5685 * MD5 checksum of the inquiry data, and if the serial number
5686 * length is greater than 0, add the serial number data
5687 * into the checksum as well. Once the inquiry and the
5688 * serial number check finish, we attempt to figure out
5689 * whether we still have the same device.
5691 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5693 MD5Init(&softc->context);
5694 MD5Update(&softc->context, (unsigned char *)inq_buf,
5695 sizeof(struct scsi_inquiry_data));
5696 softc->flags |= PROBE_INQUIRY_CKSUM;
5697 if (periph->path->device->serial_num_len > 0) {
5698 MD5Update(&softc->context,
5699 periph->path->device->serial_num,
5700 periph->path->device->serial_num_len);
5701 softc->flags |= PROBE_SERIAL_CKSUM;
5703 MD5Final(softc->digest, &softc->context);
5706 if (softc->action == PROBE_INQUIRY)
5707 inquiry_len = SHORT_INQUIRY_LENGTH;
5709 inquiry_len = inq_buf->additional_length
5710 + offsetof(struct scsi_inquiry_data,
5711 additional_length) + 1;
5714 * Some parallel SCSI devices fail to send an
5715 * ignore wide residue message when dealing with
5716 * odd length inquiry requests. Round up to be
5719 inquiry_len = roundup2(inquiry_len, 2);
5725 (u_int8_t *)inq_buf,
5730 /*timeout*/60 * 1000);
5733 case PROBE_MODE_SENSE:
5738 mode_buf_len = sizeof(struct scsi_mode_header_6)
5739 + sizeof(struct scsi_mode_blk_desc)
5740 + sizeof(struct scsi_control_page);
5741 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5742 if (mode_buf != NULL) {
5743 scsi_mode_sense(csio,
5748 SMS_PAGE_CTRL_CURRENT,
5749 SMS_CONTROL_MODE_PAGE,
5756 xpt_print_path(periph->path);
5757 printf("Unable to mode sense control page - malloc failure\n");
5758 softc->action = PROBE_SERIAL_NUM;
5761 case PROBE_SERIAL_NUM:
5763 struct scsi_vpd_unit_serial_number *serial_buf;
5764 struct cam_ed* device;
5767 device = periph->path->device;
5768 device->serial_num = NULL;
5769 device->serial_num_len = 0;
5771 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5772 serial_buf = (struct scsi_vpd_unit_serial_number *)
5773 malloc(sizeof(*serial_buf), M_TEMP,
5776 if (serial_buf != NULL) {
5781 (u_int8_t *)serial_buf,
5782 sizeof(*serial_buf),
5784 SVPD_UNIT_SERIAL_NUMBER,
5786 /*timeout*/60 * 1000);
5790 * We'll have to do without, let our probedone
5791 * routine finish up for us.
5793 start_ccb->csio.data_ptr = NULL;
5794 probedone(periph, start_ccb);
5798 xpt_action(start_ccb);
5802 proberequestdefaultnegotiation(struct cam_periph *periph)
5804 struct ccb_trans_settings cts;
5806 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5807 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5808 #ifdef CAM_NEW_TRAN_CODE
5809 cts.type = CTS_TYPE_USER_SETTINGS;
5810 #else /* CAM_NEW_TRAN_CODE */
5811 cts.flags = CCB_TRANS_USER_SETTINGS;
5812 #endif /* CAM_NEW_TRAN_CODE */
5813 xpt_action((union ccb *)&cts);
5814 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5815 #ifdef CAM_NEW_TRAN_CODE
5816 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5817 #else /* CAM_NEW_TRAN_CODE */
5818 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5819 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5820 #endif /* CAM_NEW_TRAN_CODE */
5821 xpt_action((union ccb *)&cts);
5825 probedone(struct cam_periph *periph, union ccb *done_ccb)
5828 struct cam_path *path;
5831 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5833 softc = (probe_softc *)periph->softc;
5834 path = done_ccb->ccb_h.path;
5835 priority = done_ccb->ccb_h.pinfo.priority;
5837 switch (softc->action) {
5840 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5842 if (cam_periph_error(done_ccb, 0,
5843 SF_NO_PRINT, NULL) == ERESTART)
5845 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5846 /* Don't wedge the queue */
5847 xpt_release_devq(done_ccb->ccb_h.path,
5851 softc->action = PROBE_INQUIRY;
5852 xpt_release_ccb(done_ccb);
5853 xpt_schedule(periph, priority);
5857 case PROBE_FULL_INQUIRY:
5859 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5860 struct scsi_inquiry_data *inq_buf;
5861 u_int8_t periph_qual;
5863 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5864 inq_buf = &path->device->inq_data;
5866 periph_qual = SID_QUAL(inq_buf);
5868 switch(periph_qual) {
5869 case SID_QUAL_LU_CONNECTED:
5874 * We conservatively request only
5875 * SHORT_INQUIRY_LEN bytes of inquiry
5876 * information during our first try
5877 * at sending an INQUIRY. If the device
5878 * has more information to give,
5879 * perform a second request specifying
5880 * the amount of information the device
5881 * is willing to give.
5883 len = inq_buf->additional_length
5884 + offsetof(struct scsi_inquiry_data,
5885 additional_length) + 1;
5886 if (softc->action == PROBE_INQUIRY
5887 && len > SHORT_INQUIRY_LENGTH) {
5888 softc->action = PROBE_FULL_INQUIRY;
5889 xpt_release_ccb(done_ccb);
5890 xpt_schedule(periph, priority);
5894 xpt_find_quirk(path->device);
5896 #ifdef CAM_NEW_TRAN_CODE
5897 xpt_devise_transport(path);
5898 #endif /* CAM_NEW_TRAN_CODE */
5899 if ((inq_buf->flags & SID_CmdQue) != 0)
5900 softc->action = PROBE_MODE_SENSE;
5902 softc->action = PROBE_SERIAL_NUM;
5904 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5906 xpt_release_ccb(done_ccb);
5907 xpt_schedule(periph, priority);
5913 } else if (cam_periph_error(done_ccb, 0,
5914 done_ccb->ccb_h.target_lun > 0
5915 ? SF_RETRY_UA|SF_QUIET_IR
5917 &softc->saved_ccb) == ERESTART) {
5919 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5920 /* Don't wedge the queue */
5921 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5925 * If we get to this point, we got an error status back
5926 * from the inquiry and the error status doesn't require
5927 * automatically retrying the command. Therefore, the
5928 * inquiry failed. If we had inquiry information before
5929 * for this device, but this latest inquiry command failed,
5930 * the device has probably gone away. If this device isn't
5931 * already marked unconfigured, notify the peripheral
5932 * drivers that this device is no more.
5934 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5935 /* Send the async notification. */
5936 xpt_async(AC_LOST_DEVICE, path, NULL);
5938 xpt_release_ccb(done_ccb);
5941 case PROBE_MODE_SENSE:
5943 struct ccb_scsiio *csio;
5944 struct scsi_mode_header_6 *mode_hdr;
5946 csio = &done_ccb->csio;
5947 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5948 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5949 struct scsi_control_page *page;
5952 offset = ((u_int8_t *)&mode_hdr[1])
5953 + mode_hdr->blk_desc_len;
5954 page = (struct scsi_control_page *)offset;
5955 path->device->queue_flags = page->queue_flags;
5956 } else if (cam_periph_error(done_ccb, 0,
5957 SF_RETRY_UA|SF_NO_PRINT,
5958 &softc->saved_ccb) == ERESTART) {
5960 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5961 /* Don't wedge the queue */
5962 xpt_release_devq(done_ccb->ccb_h.path,
5963 /*count*/1, /*run_queue*/TRUE);
5965 xpt_release_ccb(done_ccb);
5966 free(mode_hdr, M_TEMP);
5967 softc->action = PROBE_SERIAL_NUM;
5968 xpt_schedule(periph, priority);
5971 case PROBE_SERIAL_NUM:
5973 struct ccb_scsiio *csio;
5974 struct scsi_vpd_unit_serial_number *serial_buf;
5981 csio = &done_ccb->csio;
5982 priority = done_ccb->ccb_h.pinfo.priority;
5984 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5986 /* Clean up from previous instance of this device */
5987 if (path->device->serial_num != NULL) {
5988 free(path->device->serial_num, M_CAMXPT);
5989 path->device->serial_num = NULL;
5990 path->device->serial_num_len = 0;
5993 if (serial_buf == NULL) {
5995 * Don't process the command as it was never sent
5997 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5998 && (serial_buf->length > 0)) {
6001 path->device->serial_num =
6002 (u_int8_t *)malloc((serial_buf->length + 1),
6003 M_CAMXPT, M_NOWAIT);
6004 if (path->device->serial_num != NULL) {
6005 bcopy(serial_buf->serial_num,
6006 path->device->serial_num,
6007 serial_buf->length);
6008 path->device->serial_num_len =
6010 path->device->serial_num[serial_buf->length]
6013 } else if (cam_periph_error(done_ccb, 0,
6014 SF_RETRY_UA|SF_NO_PRINT,
6015 &softc->saved_ccb) == ERESTART) {
6017 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6018 /* Don't wedge the queue */
6019 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6024 * Let's see if we have seen this device before.
6026 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6028 u_int8_t digest[16];
6033 (unsigned char *)&path->device->inq_data,
6034 sizeof(struct scsi_inquiry_data));
6037 MD5Update(&context, serial_buf->serial_num,
6038 serial_buf->length);
6040 MD5Final(digest, &context);
6041 if (bcmp(softc->digest, digest, 16) == 0)
6045 * XXX Do we need to do a TUR in order to ensure
6046 * that the device really hasn't changed???
6049 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6050 xpt_async(AC_LOST_DEVICE, path, NULL);
6052 if (serial_buf != NULL)
6053 free(serial_buf, M_TEMP);
6057 * Now that we have all the necessary
6058 * information to safely perform transfer
6059 * negotiations... Controllers don't perform
6060 * any negotiation or tagged queuing until
6061 * after the first XPT_SET_TRAN_SETTINGS ccb is
6062 * received. So, on a new device, just retreive
6063 * the user settings, and set them as the current
6064 * settings to set the device up.
6066 proberequestdefaultnegotiation(periph);
6067 xpt_release_ccb(done_ccb);
6070 * Perform a TUR to allow the controller to
6071 * perform any necessary transfer negotiation.
6073 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6074 xpt_schedule(periph, priority);
6077 xpt_release_ccb(done_ccb);
6080 case PROBE_TUR_FOR_NEGOTIATION:
6081 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6082 /* Don't wedge the queue */
6083 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6087 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6089 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6090 /* Inform the XPT that a new device has been found */
6091 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6092 xpt_action(done_ccb);
6094 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6097 xpt_release_ccb(done_ccb);
6100 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6101 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6102 done_ccb->ccb_h.status = CAM_REQ_CMP;
6104 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6105 cam_periph_invalidate(periph);
6106 cam_periph_release(periph);
6108 probeschedule(periph);
6113 probecleanup(struct cam_periph *periph)
6115 free(periph->softc, M_TEMP);
6119 xpt_find_quirk(struct cam_ed *device)
6123 match = cam_quirkmatch((caddr_t)&device->inq_data,
6124 (caddr_t)xpt_quirk_table,
6125 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6126 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6129 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6131 device->quirk = (struct xpt_quirk_entry *)match;
6135 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
6140 error = sysctl_handle_int(oidp, &bool, sizeof(bool), req);
6141 if (error != 0 || req->newptr == NULL)
6143 if (bool == 0 || bool == 1) {
6151 #ifdef CAM_NEW_TRAN_CODE
6154 xpt_devise_transport(struct cam_path *path)
6156 struct ccb_pathinq cpi;
6157 struct ccb_trans_settings cts;
6158 struct scsi_inquiry_data *inq_buf;
6160 /* Get transport information from the SIM */
6161 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6162 cpi.ccb_h.func_code = XPT_PATH_INQ;
6163 xpt_action((union ccb *)&cpi);
6166 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6167 inq_buf = &path->device->inq_data;
6168 path->device->protocol = PROTO_SCSI;
6169 path->device->protocol_version =
6170 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6171 path->device->transport = cpi.transport;
6172 path->device->transport_version = cpi.transport_version;
6175 * Any device not using SPI3 features should
6176 * be considered SPI2 or lower.
6178 if (inq_buf != NULL) {
6179 if (path->device->transport == XPORT_SPI
6180 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6181 && path->device->transport_version > 2)
6182 path->device->transport_version = 2;
6184 struct cam_ed* otherdev;
6186 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6188 otherdev = TAILQ_NEXT(otherdev, links)) {
6189 if (otherdev != path->device)
6193 if (otherdev != NULL) {
6195 * Initially assume the same versioning as
6196 * prior luns for this target.
6198 path->device->protocol_version =
6199 otherdev->protocol_version;
6200 path->device->transport_version =
6201 otherdev->transport_version;
6203 /* Until we know better, opt for safty */
6204 path->device->protocol_version = 2;
6205 if (path->device->transport == XPORT_SPI)
6206 path->device->transport_version = 2;
6208 path->device->transport_version = 0;
6214 * For a device compliant with SPC-2 we should be able
6215 * to determine the transport version supported by
6216 * scrutinizing the version descriptors in the
6220 /* Tell the controller what we think */
6221 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6222 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6223 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6224 cts.transport = path->device->transport;
6225 cts.transport_version = path->device->transport_version;
6226 cts.protocol = path->device->protocol;
6227 cts.protocol_version = path->device->protocol_version;
6228 cts.proto_specific.valid = 0;
6229 cts.xport_specific.valid = 0;
6230 xpt_action((union ccb *)&cts);
6234 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6237 struct ccb_pathinq cpi;
6238 struct ccb_trans_settings cur_cts;
6239 struct ccb_trans_settings_scsi *scsi;
6240 struct ccb_trans_settings_scsi *cur_scsi;
6241 struct cam_sim *sim;
6242 struct scsi_inquiry_data *inq_data;
6244 if (device == NULL) {
6245 cts->ccb_h.status = CAM_PATH_INVALID;
6246 xpt_done((union ccb *)cts);
6250 if (cts->protocol == PROTO_UNKNOWN
6251 || cts->protocol == PROTO_UNSPECIFIED) {
6252 cts->protocol = device->protocol;
6253 cts->protocol_version = device->protocol_version;
6256 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6257 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6258 cts->protocol_version = device->protocol_version;
6260 if (cts->protocol != device->protocol) {
6261 xpt_print_path(cts->ccb_h.path);
6262 printf("Uninitialized Protocol %x:%x?\n",
6263 cts->protocol, device->protocol);
6264 cts->protocol = device->protocol;
6267 if (cts->protocol_version > device->protocol_version) {
6269 xpt_print_path(cts->ccb_h.path);
6270 printf("Down reving Protocol Version from %d to %d?\n",
6271 cts->protocol_version, device->protocol_version);
6273 cts->protocol_version = device->protocol_version;
6276 if (cts->transport == XPORT_UNKNOWN
6277 || cts->transport == XPORT_UNSPECIFIED) {
6278 cts->transport = device->transport;
6279 cts->transport_version = device->transport_version;
6282 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6283 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6284 cts->transport_version = device->transport_version;
6286 if (cts->transport != device->transport) {
6287 xpt_print_path(cts->ccb_h.path);
6288 printf("Uninitialized Transport %x:%x?\n",
6289 cts->transport, device->transport);
6290 cts->transport = device->transport;
6293 if (cts->transport_version > device->transport_version) {
6295 xpt_print_path(cts->ccb_h.path);
6296 printf("Down reving Transport Version from %d to %d?\n",
6297 cts->transport_version,
6298 device->transport_version);
6300 cts->transport_version = device->transport_version;
6303 sim = cts->ccb_h.path->bus->sim;
6306 * Nothing more of interest to do unless
6307 * this is a device connected via the
6310 if (cts->protocol != PROTO_SCSI) {
6311 if (async_update == FALSE)
6312 (*(sim->sim_action))(sim, (union ccb *)cts);
6316 inq_data = &device->inq_data;
6317 scsi = &cts->proto_specific.scsi;
6318 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6319 cpi.ccb_h.func_code = XPT_PATH_INQ;
6320 xpt_action((union ccb *)&cpi);
6322 /* SCSI specific sanity checking */
6323 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6324 || (inq_data->flags & SID_CmdQue) == 0
6325 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6326 || (device->quirk->mintags == 0)) {
6328 * Can't tag on hardware that doesn't support tags,
6329 * doesn't have it enabled, or has broken tag support.
6331 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6334 if (async_update == FALSE) {
6336 * Perform sanity checking against what the
6337 * controller and device can do.
6339 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6340 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6341 cur_cts.type = cts->type;
6342 xpt_action((union ccb *)&cur_cts);
6344 cur_scsi = &cur_cts.proto_specific.scsi;
6345 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6346 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6347 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6349 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6350 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6353 /* SPI specific sanity checking */
6354 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6356 struct ccb_trans_settings_spi *spi;
6357 struct ccb_trans_settings_spi *cur_spi;
6359 spi = &cts->xport_specific.spi;
6361 cur_spi = &cur_cts.xport_specific.spi;
6363 /* Fill in any gaps in what the user gave us */
6364 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6365 spi->sync_period = cur_spi->sync_period;
6366 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6367 spi->sync_period = 0;
6368 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6369 spi->sync_offset = cur_spi->sync_offset;
6370 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6371 spi->sync_offset = 0;
6372 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6373 spi->ppr_options = cur_spi->ppr_options;
6374 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6375 spi->ppr_options = 0;
6376 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6377 spi->bus_width = cur_spi->bus_width;
6378 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6380 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6381 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6382 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6384 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6385 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6386 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6387 && (inq_data->flags & SID_Sync) == 0
6388 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6389 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6390 || (cur_spi->sync_offset == 0)
6391 || (cur_spi->sync_period == 0)) {
6393 spi->sync_period = 0;
6394 spi->sync_offset = 0;
6397 switch (spi->bus_width) {
6398 case MSG_EXT_WDTR_BUS_32_BIT:
6399 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6400 || (inq_data->flags & SID_WBus32) != 0
6401 || cts->type == CTS_TYPE_USER_SETTINGS)
6402 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6404 /* Fall Through to 16-bit */
6405 case MSG_EXT_WDTR_BUS_16_BIT:
6406 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6407 || (inq_data->flags & SID_WBus16) != 0
6408 || cts->type == CTS_TYPE_USER_SETTINGS)
6409 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6410 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6413 /* Fall Through to 8-bit */
6414 default: /* New bus width?? */
6415 case MSG_EXT_WDTR_BUS_8_BIT:
6416 /* All targets can do this */
6417 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6421 spi3caps = cpi.xport_specific.spi.ppr_options;
6422 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6423 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6424 spi3caps &= inq_data->spi3data;
6426 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6427 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6429 if ((spi3caps & SID_SPI_IUS) == 0)
6430 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6432 if ((spi3caps & SID_SPI_QAS) == 0)
6433 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6435 /* No SPI Transfer settings are allowed unless we are wide */
6436 if (spi->bus_width == 0)
6437 spi->ppr_options = 0;
6439 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6441 * Can't tag queue without disconnection.
6443 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6444 scsi->valid |= CTS_SCSI_VALID_TQ;
6448 * If we are currently performing tagged transactions to
6449 * this device and want to change its negotiation parameters,
6450 * go non-tagged for a bit to give the controller a chance to
6451 * negotiate unhampered by tag messages.
6453 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6454 && (device->inq_flags & SID_CmdQue) != 0
6455 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6456 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6457 CTS_SPI_VALID_SYNC_OFFSET|
6458 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6459 xpt_toggle_tags(cts->ccb_h.path);
6462 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6463 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6467 * If we are transitioning from tags to no-tags or
6468 * vice-versa, we need to carefully freeze and restart
6469 * the queue so that we don't overlap tagged and non-tagged
6470 * commands. We also temporarily stop tags if there is
6471 * a change in transfer negotiation settings to allow
6472 * "tag-less" negotiation.
6474 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6475 || (device->inq_flags & SID_CmdQue) != 0)
6476 device_tagenb = TRUE;
6478 device_tagenb = FALSE;
6480 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6481 && device_tagenb == FALSE)
6482 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6483 && device_tagenb == TRUE)) {
6485 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6487 * Delay change to use tags until after a
6488 * few commands have gone to this device so
6489 * the controller has time to perform transfer
6490 * negotiations without tagged messages getting
6493 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6494 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6496 struct ccb_relsim crs;
6498 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6499 device->inq_flags &= ~SID_CmdQue;
6500 xpt_dev_ccbq_resize(cts->ccb_h.path,
6501 sim->max_dev_openings);
6502 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6503 device->tag_delay_count = 0;
6505 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6507 crs.ccb_h.func_code = XPT_REL_SIMQ;
6508 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6510 = crs.release_timeout
6513 xpt_action((union ccb *)&crs);
6517 if (async_update == FALSE)
6518 (*(sim->sim_action))(sim, (union ccb *)cts);
6521 #else /* CAM_NEW_TRAN_CODE */
6524 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6527 struct cam_sim *sim;
6530 sim = cts->ccb_h.path->bus->sim;
6531 if (async_update == FALSE) {
6532 struct scsi_inquiry_data *inq_data;
6533 struct ccb_pathinq cpi;
6534 struct ccb_trans_settings cur_cts;
6536 if (device == NULL) {
6537 cts->ccb_h.status = CAM_PATH_INVALID;
6538 xpt_done((union ccb *)cts);
6543 * Perform sanity checking against what the
6544 * controller and device can do.
6546 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6547 cpi.ccb_h.func_code = XPT_PATH_INQ;
6548 xpt_action((union ccb *)&cpi);
6549 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6550 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6551 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6552 xpt_action((union ccb *)&cur_cts);
6553 inq_data = &device->inq_data;
6555 /* Fill in any gaps in what the user gave us */
6556 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6557 cts->sync_period = cur_cts.sync_period;
6558 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6559 cts->sync_offset = cur_cts.sync_offset;
6560 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6561 cts->bus_width = cur_cts.bus_width;
6562 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6563 cts->flags &= ~CCB_TRANS_DISC_ENB;
6564 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6566 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6567 cts->flags &= ~CCB_TRANS_TAG_ENB;
6568 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6571 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6572 && (inq_data->flags & SID_Sync) == 0)
6573 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6574 || (cts->sync_offset == 0)
6575 || (cts->sync_period == 0)) {
6577 cts->sync_period = 0;
6578 cts->sync_offset = 0;
6579 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6580 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6581 && cts->sync_period <= 0x9) {
6583 * Don't allow DT transmission rates if the
6584 * device does not support it.
6586 cts->sync_period = 0xa;
6589 switch (cts->bus_width) {
6590 case MSG_EXT_WDTR_BUS_32_BIT:
6591 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6592 || (inq_data->flags & SID_WBus32) != 0)
6593 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6595 /* FALLTHROUGH to 16-bit */
6596 case MSG_EXT_WDTR_BUS_16_BIT:
6597 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6598 || (inq_data->flags & SID_WBus16) != 0)
6599 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6600 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6603 /* FALLTHROUGH to 8-bit */
6604 default: /* New bus width?? */
6605 case MSG_EXT_WDTR_BUS_8_BIT:
6606 /* All targets can do this */
6607 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6611 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6613 * Can't tag queue without disconnection.
6615 cts->flags &= ~CCB_TRANS_TAG_ENB;
6616 cts->valid |= CCB_TRANS_TQ_VALID;
6619 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6620 || (inq_data->flags & SID_CmdQue) == 0
6621 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6622 || (device->quirk->mintags == 0)) {
6624 * Can't tag on hardware that doesn't support,
6625 * doesn't have it enabled, or has broken tag support.
6627 cts->flags &= ~CCB_TRANS_TAG_ENB;
6632 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6636 * If we are transitioning from tags to no-tags or
6637 * vice-versa, we need to carefully freeze and restart
6638 * the queue so that we don't overlap tagged and non-tagged
6639 * commands. We also temporarily stop tags if there is
6640 * a change in transfer negotiation settings to allow
6641 * "tag-less" negotiation.
6643 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6644 || (device->inq_flags & SID_CmdQue) != 0)
6645 device_tagenb = TRUE;
6647 device_tagenb = FALSE;
6649 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6650 && device_tagenb == FALSE)
6651 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6652 && device_tagenb == TRUE)) {
6654 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6656 * Delay change to use tags until after a
6657 * few commands have gone to this device so
6658 * the controller has time to perform transfer
6659 * negotiations without tagged messages getting
6662 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6663 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6665 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6667 device->inq_flags &= ~SID_CmdQue;
6668 xpt_dev_ccbq_resize(cts->ccb_h.path,
6669 sim->max_dev_openings);
6670 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6671 device->tag_delay_count = 0;
6676 if (async_update == FALSE) {
6678 * If we are currently performing tagged transactions to
6679 * this device and want to change its negotiation parameters,
6680 * go non-tagged for a bit to give the controller a chance to
6681 * negotiate unhampered by tag messages.
6683 if ((device->inq_flags & SID_CmdQue) != 0
6684 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6685 CCB_TRANS_SYNC_OFFSET_VALID|
6686 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6687 xpt_toggle_tags(cts->ccb_h.path);
6689 (*(sim->sim_action))(sim, (union ccb *)cts);
6693 struct ccb_relsim crs;
6695 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6697 crs.ccb_h.func_code = XPT_REL_SIMQ;
6698 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6700 = crs.release_timeout
6703 xpt_action((union ccb *)&crs);
6708 #endif /* CAM_NEW_TRAN_CODE */
6711 xpt_toggle_tags(struct cam_path *path)
6716 * Give controllers a chance to renegotiate
6717 * before starting tag operations. We
6718 * "toggle" tagged queuing off then on
6719 * which causes the tag enable command delay
6720 * counter to come into effect.
6723 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6724 || ((dev->inq_flags & SID_CmdQue) != 0
6725 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6726 struct ccb_trans_settings cts;
6728 xpt_setup_ccb(&cts.ccb_h, path, 1);
6729 #ifdef CAM_NEW_TRAN_CODE
6730 cts.protocol = PROTO_SCSI;
6731 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6732 cts.transport = XPORT_UNSPECIFIED;
6733 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6734 cts.proto_specific.scsi.flags = 0;
6735 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6736 #else /* CAM_NEW_TRAN_CODE */
6738 cts.valid = CCB_TRANS_TQ_VALID;
6739 #endif /* CAM_NEW_TRAN_CODE */
6740 xpt_set_transfer_settings(&cts, path->device,
6741 /*async_update*/TRUE);
6742 #ifdef CAM_NEW_TRAN_CODE
6743 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6744 #else /* CAM_NEW_TRAN_CODE */
6745 cts.flags = CCB_TRANS_TAG_ENB;
6746 #endif /* CAM_NEW_TRAN_CODE */
6747 xpt_set_transfer_settings(&cts, path->device,
6748 /*async_update*/TRUE);
6753 xpt_start_tags(struct cam_path *path)
6755 struct ccb_relsim crs;
6756 struct cam_ed *device;
6757 struct cam_sim *sim;
6760 device = path->device;
6761 sim = path->bus->sim;
6762 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6763 xpt_freeze_devq(path, /*count*/1);
6764 device->inq_flags |= SID_CmdQue;
6765 if (device->tag_saved_openings != 0)
6766 newopenings = device->tag_saved_openings;
6768 newopenings = min(device->quirk->maxtags,
6769 sim->max_tagged_dev_openings);
6770 xpt_dev_ccbq_resize(path, newopenings);
6771 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6772 crs.ccb_h.func_code = XPT_REL_SIMQ;
6773 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6775 = crs.release_timeout
6778 xpt_action((union ccb *)&crs);
6781 static int busses_to_config;
6782 static int busses_to_reset;
6785 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6787 if (bus->path_id != CAM_XPT_PATH_ID) {
6788 struct cam_path path;
6789 struct ccb_pathinq cpi;
6793 xpt_compile_path(&path, NULL, bus->path_id,
6794 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6795 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6796 cpi.ccb_h.func_code = XPT_PATH_INQ;
6797 xpt_action((union ccb *)&cpi);
6798 can_negotiate = cpi.hba_inquiry;
6799 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6800 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6803 xpt_release_path(&path);
6810 xptconfigfunc(struct cam_eb *bus, void *arg)
6812 struct cam_path *path;
6813 union ccb *work_ccb;
6815 if (bus->path_id != CAM_XPT_PATH_ID) {
6819 work_ccb = xpt_alloc_ccb();
6820 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6821 CAM_TARGET_WILDCARD,
6822 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6823 printf("xptconfigfunc: xpt_create_path failed with "
6824 "status %#x for bus %d\n", status, bus->path_id);
6825 printf("xptconfigfunc: halting bus configuration\n");
6826 xpt_free_ccb(work_ccb);
6828 xpt_finishconfig(xpt_periph, NULL);
6831 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6832 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6833 xpt_action(work_ccb);
6834 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6835 printf("xptconfigfunc: CPI failed on bus %d "
6836 "with status %d\n", bus->path_id,
6837 work_ccb->ccb_h.status);
6838 xpt_finishconfig(xpt_periph, work_ccb);
6842 can_negotiate = work_ccb->cpi.hba_inquiry;
6843 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6844 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6845 && (can_negotiate != 0)) {
6846 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6847 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6848 work_ccb->ccb_h.cbfcnp = NULL;
6849 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6850 ("Resetting Bus\n"));
6851 xpt_action(work_ccb);
6852 xpt_finishconfig(xpt_periph, work_ccb);
6854 /* Act as though we performed a successful BUS RESET */
6855 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6856 xpt_finishconfig(xpt_periph, work_ccb);
6864 xpt_config(void *arg)
6867 * Now that interrupts are enabled, go find our devices
6871 /* Setup debugging flags and path */
6872 #ifdef CAM_DEBUG_FLAGS
6873 cam_dflags = CAM_DEBUG_FLAGS;
6874 #else /* !CAM_DEBUG_FLAGS */
6875 cam_dflags = CAM_DEBUG_NONE;
6876 #endif /* CAM_DEBUG_FLAGS */
6877 #ifdef CAM_DEBUG_BUS
6878 if (cam_dflags != CAM_DEBUG_NONE) {
6879 if (xpt_create_path(&cam_dpath, xpt_periph,
6880 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6881 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6882 printf("xpt_config: xpt_create_path() failed for debug"
6883 " target %d:%d:%d, debugging disabled\n",
6884 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6885 cam_dflags = CAM_DEBUG_NONE;
6889 #else /* !CAM_DEBUG_BUS */
6891 #endif /* CAM_DEBUG_BUS */
6892 #endif /* CAMDEBUG */
6895 * Scan all installed busses.
6897 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6899 if (busses_to_config == 0) {
6900 /* Call manually because we don't have any busses */
6901 xpt_finishconfig(xpt_periph, NULL);
6903 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6904 printf("Waiting %d seconds for SCSI "
6905 "devices to settle\n", scsi_delay/1000);
6907 xpt_for_all_busses(xptconfigfunc, NULL);
6912 * If the given device only has one peripheral attached to it, and if that
6913 * peripheral is the passthrough driver, announce it. This insures that the
6914 * user sees some sort of announcement for every peripheral in their system.
6917 xptpassannouncefunc(struct cam_ed *device, void *arg)
6919 struct cam_periph *periph;
6922 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6923 periph = SLIST_NEXT(periph, periph_links), i++);
6925 periph = SLIST_FIRST(&device->periphs);
6927 && (strncmp(periph->periph_name, "pass", 4) == 0))
6928 xpt_announce_periph(periph, NULL);
6934 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6936 struct periph_driver **p_drv;
6939 if (done_ccb != NULL) {
6940 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6941 ("xpt_finishconfig\n"));
6942 switch(done_ccb->ccb_h.func_code) {
6944 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6945 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6946 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6947 done_ccb->crcn.flags = 0;
6948 xpt_action(done_ccb);
6954 xpt_free_path(done_ccb->ccb_h.path);
6960 if (busses_to_config == 0) {
6961 /* Register all the peripheral drivers */
6962 /* XXX This will have to change when we have loadable modules */
6963 p_drv = periph_drivers;
6964 for (i = 0; p_drv[i] != NULL; i++) {
6965 (*p_drv[i]->init)();
6969 * Check for devices with no "standard" peripheral driver
6970 * attached. For any devices like that, announce the
6971 * passthrough driver so the user will see something.
6973 xpt_for_all_devices(xptpassannouncefunc, NULL);
6975 /* Release our hook so that the boot can continue. */
6976 config_intrhook_disestablish(xpt_config_hook);
6977 free(xpt_config_hook, M_TEMP);
6978 xpt_config_hook = NULL;
6980 if (done_ccb != NULL)
6981 xpt_free_ccb(done_ccb);
6985 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6987 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6989 switch (work_ccb->ccb_h.func_code) {
6990 /* Common cases first */
6991 case XPT_PATH_INQ: /* Path routing inquiry */
6993 struct ccb_pathinq *cpi;
6995 cpi = &work_ccb->cpi;
6996 cpi->version_num = 1; /* XXX??? */
6997 cpi->hba_inquiry = 0;
6998 cpi->target_sprt = 0;
7000 cpi->hba_eng_cnt = 0;
7001 cpi->max_target = 0;
7003 cpi->initiator_id = 0;
7004 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
7005 strncpy(cpi->hba_vid, "", HBA_IDLEN);
7006 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
7007 cpi->unit_number = sim->unit_number;
7008 cpi->bus_id = sim->bus_id;
7009 cpi->base_transfer_speed = 0;
7010 #ifdef CAM_NEW_TRAN_CODE
7011 cpi->protocol = PROTO_UNSPECIFIED;
7012 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
7013 cpi->transport = XPORT_UNSPECIFIED;
7014 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
7015 #endif /* CAM_NEW_TRAN_CODE */
7016 cpi->ccb_h.status = CAM_REQ_CMP;
7021 work_ccb->ccb_h.status = CAM_REQ_INVALID;
7028 * The xpt as a "controller" has no interrupt sources, so polling
7032 xptpoll(struct cam_sim *sim)
7037 camisr(void *V_queue)
7039 cam_isrq_t *oqueue = V_queue;
7042 struct ccb_hdr *ccb_h;
7045 * Transfer the ccb_bioq list to a temporary list so we can operate
7046 * on it without needing to lock/unlock on every loop. The concat
7047 * function with re-init the real list for us.
7050 mtx_lock(&cam_bioq_lock);
7052 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7053 mtx_unlock(&cam_bioq_lock);
7055 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7058 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7059 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7062 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7067 if (ccb_h->flags & CAM_HIGH_POWER) {
7068 struct highpowerlist *hphead;
7069 union ccb *send_ccb;
7071 hphead = &highpowerq;
7073 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7076 * Increment the count since this command is done.
7081 * Any high powered commands queued up?
7083 if (send_ccb != NULL) {
7085 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7087 xpt_release_devq(send_ccb->ccb_h.path,
7088 /*count*/1, /*runqueue*/TRUE);
7091 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7094 dev = ccb_h->path->device;
7097 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7099 ccb_h->path->bus->sim->devq->send_active--;
7100 ccb_h->path->bus->sim->devq->send_openings++;
7103 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7104 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7105 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7106 && (dev->ccbq.dev_active == 0))) {
7108 xpt_release_devq(ccb_h->path, /*count*/1,
7112 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7113 && (--dev->tag_delay_count == 0))
7114 xpt_start_tags(ccb_h->path);
7116 if ((dev->ccbq.queue.entries > 0)
7117 && (dev->qfrozen_cnt == 0)
7118 && (device_is_send_queued(dev) == 0)) {
7119 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7124 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7125 xpt_release_simq(ccb_h->path->bus->sim,
7127 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7131 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7132 && (ccb_h->status & CAM_DEV_QFRZN)) {
7133 xpt_release_devq(ccb_h->path, /*count*/1,
7135 ccb_h->status &= ~CAM_DEV_QFRZN;
7137 xpt_run_dev_sendq(ccb_h->path->bus);
7140 /* Call the peripheral driver's callback */
7141 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7143 /* Raise IPL for while test */